You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
4.7 KiB
153 lines
4.7 KiB
/*
|
|
* Copyright 2002-2019 Intel Corporation.
|
|
*
|
|
* This software is provided to you as Sample Source Code as defined in the accompanying
|
|
* End User License Agreement for the Intel(R) Software Development Products ("Agreement")
|
|
* section 1.L.
|
|
*
|
|
* This software and the related documents are provided as is, with no express or implied
|
|
* warranties, other than those that are expressly stated in the License.
|
|
*/
|
|
|
|
|
|
#include "pin.H"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
using std::hex;
|
|
using std::cerr;
|
|
using std::string;
|
|
using std::ios;
|
|
using std::endl;
|
|
|
|
/* ===================================================================== */
|
|
/* 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 and the return value.
|
|
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)Arg1Before,
|
|
IARG_ADDRINT, MALLOC,
|
|
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
|
|
IARG_END);
|
|
RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)MallocAfter,
|
|
IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
|
|
|
|
RTN_Close(mallocRtn);
|
|
}
|
|
|
|
// Find the free() function.
|
|
RTN freeRtn = RTN_FindByName(img, FREE);
|
|
if (RTN_Valid(freeRtn))
|
|
{
|
|
RTN_Open(freeRtn);
|
|
// Instrument free() to print the input argument value.
|
|
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)Arg1Before,
|
|
IARG_ADDRINT, FREE,
|
|
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
|
|
IARG_END);
|
|
RTN_Close(freeRtn);
|
|
}
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
|
|
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_StartProgram();
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
/* eof */
|
|
/* ===================================================================== */
|