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.

129 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.
*/
#include <stdio.h>
#include "pin.H"
using std::string;
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "malloc_mt.out", "specify output file name");
//==============================================================
// Analysis Routines
//==============================================================
// Note: threadid+1 is used as an argument to the PIN_GetLock()
// routine as a debugging aid. This is the value that
// the lock is set to, so it must be non-zero.
// lock serializes access to the output file.
FILE * out;
PIN_LOCK pinLock;
// Note that opening a file in a callback is only supported on Linux systems.
// See buffer-win.cpp for how to work around this issue on Windows.
//
// This routine is executed every time a thread is created.
VOID ThreadStart(THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
PIN_GetLock(&pinLock, threadid+1);
fprintf(out, "thread begin %d\n",threadid);
fflush(out);
PIN_ReleaseLock(&pinLock);
}
// This routine is executed every time a thread is destroyed.
VOID ThreadFini(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v)
{
PIN_GetLock(&pinLock, threadid+1);
fprintf(out, "thread end %d code %d\n",threadid, code);
fflush(out);
PIN_ReleaseLock(&pinLock);
}
// This routine is executed each time malloc is called.
VOID BeforeMalloc( int size, THREADID threadid )
{
PIN_GetLock(&pinLock, threadid+1);
fprintf(out, "thread %d entered malloc(%d)\n", threadid, size);
fflush(out);
PIN_ReleaseLock(&pinLock);
}
//====================================================================
// Instrumentation Routines
//====================================================================
// This routine is executed for each image.
VOID ImageLoad(IMG img, VOID *)
{
RTN rtn = RTN_FindByName(img, "malloc");
if ( RTN_Valid( rtn ))
{
RTN_Open(rtn);
RTN_InsertCall(rtn, IPOINT_BEFORE, AFUNPTR(BeforeMalloc),
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_THREAD_ID, IARG_END);
RTN_Close(rtn);
}
}
// This routine is executed once at the end.
VOID Fini(INT32 code, VOID *v)
{
fclose(out);
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR("This Pintool prints a trace of malloc calls in the guest application\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(INT32 argc, CHAR **argv)
{
// Initialize the pin lock
PIN_InitLock(&pinLock);
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
PIN_InitSymbols();
out = fopen(KnobOutputFile.Value().c_str(), "w");
// Register ImageLoad to be called when each image is loaded.
IMG_AddInstrumentFunction(ImageLoad, 0);
// Register Analysis routines to be called when a thread begins/ends
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Never returns
PIN_StartProgram();
return 0;
}