/*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 Similar to Probes/malloctrace.C, but puts replacement functions in the application namespace */ #include "pin.H" #include #include #include #include using namespace std; /* ===================================================================== */ /* Global Variables */ /* ===================================================================== */ ofstream TraceFile; /* ===================================================================== */ /* Commandline Switches */ /* ===================================================================== */ KNOB KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "malloctrace2.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; } typedef typeof(malloc) * MallocType; typedef typeof(free) * FreeType; typedef typeof(dlopen) * DlopenType; typedef typeof(dlsym) * DlsymType; MallocType MallocWrapper = 0; FreeType FreeWrapper = 0; DlopenType AppDlopen = 0; DlsymType AppDlsym = 0; /* ===================================================================== */ // Called every time a new image is loaded // Look for routines that we want to probe VOID ImageLoad(IMG img, VOID *v) { if (strstr(IMG_Name(img).c_str(), "libdl.so")) { TraceFile << "Found libdl.so " << IMG_Name(img) << endl; // Get the function pointer for the application dlopen RTN dlopenRtn = RTN_FindByName(img, "__dlopen_check"); ASSERTX(RTN_Valid(dlopenRtn)); AppDlopen = DlopenType(RTN_Funptr(dlopenRtn)); // Get the function pointer for the application dlsym RTN dlsymRtn = RTN_FindByName(img, "dlsym"); ASSERTX(RTN_Valid(dlsymRtn)); AppDlsym = DlsymType(RTN_Funptr(dlsymRtn)); // inject mallocwrappers.so into application by executing application dlopen void * mallocTraceHandle = AppDlopen("mallocwrappers.so", RTLD_LAZY); ASSERTX(mallocTraceHandle); // Get function pointers for the wrappers MallocWrapper = MallocType(AppDlsym(mallocTraceHandle, "mallocWrapper")); FreeWrapper = FreeType(AppDlsym(mallocTraceHandle, "freeWrapper")); } if (strstr(IMG_Name(img).c_str(), "libc.so")) { TraceFile << "Found libc.so " << IMG_Name(img) << endl; ASSERTX(MallocWrapper && FreeWrapper); // Replace malloc and free in application libc with wrappers in mallocwrappers.so RTN mallocRtn = RTN_FindByName(img, "malloc"); ASSERTX(RTN_Valid(mallocRtn)); RTN_ReplaceWithUninstrumentedRoutine(mallocRtn, AFUNPTR(MallocWrapper)); RTN freeRtn = RTN_FindByName(img, "free"); ASSERTX(RTN_Valid(freeRtn)); RTN_ReplaceWithUninstrumentedRoutine(freeRtn, AFUNPTR(FreeWrapper)); } } /* ===================================================================== */ 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 */ /* ===================================================================== */