/*BEGIN_LEGAL Intel Open Source License Copyright (c) 2002-2005 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 */ /* ===================================================================== */ /* @ORIGINAL_AUTHOR: Robert Cohn */ /* ===================================================================== */ /*! @file Demonstrates low cost api replacement using probes. We run the original program, but use probes to transfer control to out replacement functions. Generates a trace of malloc/free calls */ #include "pin.H" #include #include using namespace std; /* ===================================================================== */ /* Global Variables */ /* ===================================================================== */ ofstream TraceFile; /* ===================================================================== */ /* Commandline Switches */ /* ===================================================================== */ KNOB KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "malloctrace.out", "specify trace file name"); /* ===================================================================== */ INT32 Usage() { cerr << "This pin tool collects an instruction trace for debugging\n" "\n"; cerr << KNOB_BASE::StringKnobSummary(); cerr << endl; return -1; } void * MallocProbe(int size) { // Get a handle to the original malloc so we can call it // This handles the case when there are multiple malloc routines // Must be first line of routine typeof(malloc) * mallocWithoutReplacement = (typeof(malloc)*)PIN_RoutineWithoutReplacement(); void * ptr = mallocWithoutReplacement(size); TraceFile << "malloc(" << size << ") returns " << ptr << endl; return ptr; } void FreeProbe(void *p) { // Must be first line of routine typeof(free) * freeWithoutReplacement = (typeof(free)*)PIN_RoutineWithoutReplacement(); freeWithoutReplacement(p); TraceFile << "free(" << p << ")" << endl; } void ExitProbe(int code) { // Must be first line of routine typeof(exit) * exitWithoutReplacement = (typeof(exit)*)PIN_RoutineWithoutReplacement(); TraceFile << "## eof" << endl << flush; TraceFile.close(); exitWithoutReplacement(code); } /* ===================================================================== */ // Called every time a new image is loaded // Look for routines that we want to probe VOID ImageLoad(IMG img, VOID *v) { RTN mallocRtn = RTN_FindByName(img, "malloc"); if (RTN_Valid(mallocRtn)) { RTN_ReplaceWithUninstrumentedRoutine(mallocRtn, AFUNPTR(MallocProbe)); TraceFile << "Inserted probe for malloc:" << IMG_Name(img) << endl; } RTN freeRtn = RTN_FindByName(img, "free"); if (RTN_Valid(freeRtn)) { RTN_ReplaceWithUninstrumentedRoutine(freeRtn, AFUNPTR(FreeProbe)); TraceFile << "Inserted probe for free:" << IMG_Name(img) << endl; } RTN exitRtn = RTN_FindByName(img, "exit"); if (RTN_Valid(exitRtn)) { RTN_ReplaceWithUninstrumentedRoutine(exitRtn, AFUNPTR(ExitProbe)); TraceFile << "Inserted probe for exit:" << IMG_Name(img) << endl; } } /* ===================================================================== */ 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); IMG_AddInstrumentFunction(ImageLoad, 0); PIN_StartProgramProbed(); return 0; } /* ===================================================================== */ /* eof */ /* ===================================================================== */