/*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. Executes the replaced routine from the replacement function. Generates a trace of malloc/free calls */ #include "pin.H" #include #include using namespace std; /* ===================================================================== */ /* Global Variables */ /* ===================================================================== */ ofstream TraceFile; static void * (*pf_malloc)(size_t size); static void (*pf_free)(void * ptr); static void (*pf_exit)(int status); /* ===================================================================== */ /* Commandline Switches */ /* ===================================================================== */ KNOB KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "probemalloctrace.out", "specify trace file name"); /* ===================================================================== */ INT32 Usage() { cerr << "This pin tool replaces malloc, free and exit using probes.\n" "\n"; cerr << KNOB_BASE::StringKnobSummary(); cerr << endl; return -1; } void * MallocProbe(int size) { // Call the original malloc. We must call the function pointer returned from // RTN_ReplaceProbed to prevent this call from being redirected to the replacement // function. if (pf_malloc) { void * ptr = (pf_malloc)(size); TraceFile << "malloc(" << size << ") returns " << ptr << endl; return ptr; } return 0; } void FreeProbe(void *p) { // Call the original free. We must call the function pointer returned from // RTN_ReplaceProbed to prevent this call from being redirected to the replacement // function. if (pf_free) { (pf_free)(p); TraceFile << "free(" << p << ")" << endl; } } void ExitProbe(int code) { // Call the original exit. We must call the function pointer returned from // RTN_ReplaceProbed to prevent this call from being redirected to the replacement // function. TraceFile << "## eof" << endl << flush; TraceFile.close(); if (pf_exit) (pf_exit)(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)) { // Save the function pointer that points to the new location of // the entry point of the original malloc. pf_malloc = (void * (*)(size_t)) RTN_ReplaceProbed(mallocRtn, AFUNPTR(MallocProbe)); TraceFile << "Inserted probe for malloc:" << IMG_Name(img) << endl; } RTN freeRtn = RTN_FindByName(img, "free"); if (RTN_Valid(freeRtn)) { // Save the function pointer that points to the new location of // the entry point of the original free. pf_free = (void (*)(void *)) RTN_ReplaceProbed(freeRtn, AFUNPTR(FreeProbe)); TraceFile << "Inserted probe for free:" << IMG_Name(img) << endl; } RTN exitRtn = RTN_FindByName(img, "exit"); if (RTN_Valid(exitRtn)) { // Save the function pointer that points to the new location of // the entry point of the original exit. pf_exit = (void (*)(int)) RTN_ReplaceProbed(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 */ /* ===================================================================== */