Created
February 7, 2016 22:26
-
-
Save mainland/7f264b4eb2eeccafc982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*BEGIN_LEGAL | |
Intel Open Source License | |
Copyright (c) 2002-2015 Intel Corporation. All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are | |
met: | |
Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. Redistributions | |
in binary form must reproduce the above copyright notice, this list of | |
conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. Neither the name of | |
the Intel Corporation nor the names of its contributors may be used to | |
endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR | |
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
END_LEGAL */ | |
#include "pin.H" | |
#include <iostream> | |
#include <fstream> | |
/* ===================================================================== */ | |
/* Names of malloc and free */ | |
/* ===================================================================== */ | |
#if defined(TARGET_MAC) | |
#define MALLOC "_malloc" | |
#define FREE "_free" | |
#else | |
#define MALLOC "malloc" | |
#define FREE "free" | |
#endif | |
/* ===================================================================== */ | |
/* Global Variables */ | |
/* ===================================================================== */ | |
std::ofstream TraceFile; | |
/* ===================================================================== */ | |
/* Commandline Switches */ | |
/* ===================================================================== */ | |
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", | |
"o", "malloctrace.out", "specify trace file name"); | |
/* ===================================================================== */ | |
/* ===================================================================== */ | |
/* Analysis routines */ | |
/* ===================================================================== */ | |
VOID Arg1Before(CHAR * name, ADDRINT size) | |
{ | |
TraceFile << name << "(" << size << ")" << endl; | |
} | |
VOID MallocAfter(ADDRINT ret) | |
{ | |
TraceFile << " returns " << ret << endl; | |
} | |
/* ===================================================================== */ | |
/* Instrumentation routines */ | |
/* ===================================================================== */ | |
VOID Image(IMG img, VOID *v) | |
{ | |
// Instrument the malloc() and free() functions. Print the input argument | |
// of each malloc() or free(), and the return value of malloc(). | |
// | |
// Find the malloc() function. | |
RTN mallocRtn = RTN_FindByName(img, MALLOC); | |
if (RTN_Valid(mallocRtn)) { | |
RTN_Open(mallocRtn); | |
// Instrument malloc() to print the input argument value. | |
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)Arg1Before, | |
IARG_ADDRINT, MALLOC, | |
IARG_FUNCARG_ENTRYPOINT_VALUE, 0, | |
IARG_END); | |
RTN_Close(mallocRtn); | |
} | |
} | |
/* ===================================================================== */ | |
VOID Fini(INT32 code, VOID *v) | |
{ | |
TraceFile.close(); | |
} | |
/* ===================================================================== */ | |
/* Print Help Message */ | |
/* ===================================================================== */ | |
INT32 Usage() | |
{ | |
cerr << "This tool produces a trace of calls to malloc." << endl; | |
cerr << endl << KNOB_BASE::StringKnobSummary() << endl; | |
return -1; | |
} | |
/* ===================================================================== */ | |
/* Main */ | |
/* ===================================================================== */ | |
int main(int argc, char *argv[]) | |
{ | |
// Initialize pin & symbol manager | |
PIN_InitSymbols(); | |
if(PIN_Init(argc,argv)) | |
return Usage(); | |
// Write to a file since cout and cerr maybe closed by the application | |
TraceFile.open(KnobOutputFile.Value().c_str()); | |
TraceFile << hex; | |
TraceFile.setf(ios::showbase); | |
// Register Image to be called to instrument functions. | |
IMG_AddInstrumentFunction(Image, 0); | |
PIN_AddFiniFunction(Fini, 0); | |
// Never returns | |
PIN_StartProgramProbed(); | |
return 0; | |
} | |
/* ===================================================================== */ | |
/* eof */ | |
/* ===================================================================== */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment