/*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 */ #include #include "pin.H" #include using namespace std; #if defined(TARGET_MAC) static char* replacedfunname = "_addnums"; #else static char* replacedfunname = "addnums"; #endif typedef int (*REPLACEFUNTYPE)(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9); class NATIVE_FUN_ID { public: const CHAR* _funname; NATIVE_FUN_ID(const CHAR* s): _funname(s) {} bool operator == (const NATIVE_FUN_ID& other) const { return strcmp(_funname, other._funname)==0; } bool operator < (const NATIVE_FUN_ID& other) const { return strlen(_funname) < strlen(other._funname); } }; typedef map NATIVE_FUN_MAP; static NATIVE_FUN_MAP native_fun_map; int replacement_addnums(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9) { static bool first = TRUE; static REPLACEFUNTYPE fun; if (first) { // find from the map only the first time. After that, we use the saved result first = FALSE; NATIVE_FUN_MAP::const_iterator result = native_fun_map.find(NATIVE_FUN_ID(replacedfunname)); ASSERTX(result != native_fun_map.end()); fun = reinterpret_cast(result->second); } printf("Inside replacement_addnums()\n"); fflush(stdout); return fun(n0, n1, n2, n3, n4, n5, n6, n7, n8, n9); } VOID itc_callback_push(AFUNPTR replacedFun) { //printf("itc_callback_push() is called with replacedFun= %p\n", replacedFun); printf("itc_callback_push() is called\n"); fflush(stdout); } VOID ImageLoad(IMG img, VOID * v) { RTN rtn = RTN_FindByName(img, replacedfunname); if (RTN_Valid(rtn)) { // Complex replacememt will call itc_callback_push before calling the replaceent function; // simple replacement (RTN_ReplaceWithUninstrumentedRoutine()) won't RTN_ComplexReplaceWithUninstrumentedRoutine(rtn, reinterpret_cast(replacement_addnums)); const AFUNPTR fptr = RTN_Funptr(rtn); native_fun_map[NATIVE_FUN_ID(RTN_Name(rtn).c_str())] = fptr; } } VOID Fini(INT32 code, VOID *) { fprintf(stderr, "\nDone\n"); } int main(INT32 argc, CHAR **argv) { PIN_InitSymbols(); PIN_Init(argc, argv); // register itc_callback_push with Pin PIN_RegisterItcAuxCallBackPushFun(reinterpret_cast(itc_callback_push)); IMG_AddInstrumentFunction(ImageLoad, 0); PIN_AddFiniFunction(Fini, 0); // Never returns PIN_StartProgram(); return 0; }