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.
146 lines
3.9 KiB
146 lines
3.9 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.
|
|
*/
|
|
|
|
/*! @file
|
|
* This file contains an ISA-portable PIN tool for tracing calls to malloc
|
|
*/
|
|
|
|
#include "pin.H"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
using std::cout;
|
|
using std::ios;
|
|
using std::hex;
|
|
using std::cerr;
|
|
using std::endl;
|
|
using std::string;
|
|
|
|
/* ===================================================================== */
|
|
/* 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");
|
|
|
|
/* ===================================================================== */
|
|
/* Print Help Message */
|
|
/* ===================================================================== */
|
|
|
|
INT32 Usage()
|
|
{
|
|
cerr <<
|
|
"This tool produces a trace of calls to malloc.\n"
|
|
"\n";
|
|
|
|
cerr << KNOB_BASE::StringKnobSummary();
|
|
|
|
cerr << endl;
|
|
|
|
return -1;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
|
|
VOID Arg1Before(CHAR * name, ADDRINT size)
|
|
{
|
|
TraceFile << name << "(" << size << ")" << endl;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
|
|
VOID MallocAfter(ADDRINT ret)
|
|
{
|
|
TraceFile << " returns " << ret << endl;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
|
|
VOID Image(IMG img, VOID *v)
|
|
{
|
|
RTN mallocRtn = RTN_FindByName(img, MALLOC);
|
|
if (RTN_Valid(mallocRtn))
|
|
{
|
|
RTN_Open(mallocRtn);
|
|
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);
|
|
}
|
|
|
|
RTN freeRtn = RTN_FindByName(img, FREE);
|
|
if (RTN_Valid(freeRtn))
|
|
{
|
|
RTN_Open(freeRtn);
|
|
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();
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
/* Main */
|
|
/* ===================================================================== */
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
PIN_InitSymbols();
|
|
|
|
if( PIN_Init(argc,argv) )
|
|
{
|
|
return Usage();
|
|
}
|
|
|
|
|
|
TraceFile.open(KnobOutputFile.Value().c_str());
|
|
|
|
TraceFile << hex;
|
|
TraceFile.setf(ios::showbase);
|
|
|
|
cout << hex;
|
|
cout.setf(ios::showbase);
|
|
|
|
IMG_AddInstrumentFunction(Image, 0);
|
|
PIN_AddFiniFunction(Fini, 0);
|
|
|
|
// Never returns
|
|
PIN_StartProgram();
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
/* eof */
|
|
/* ===================================================================== */
|