/*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; static char* replacedfunname = "__pthread_create_2_1"; typedef int (*REPLACEFUNTYPE)(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg); static AFUNPTR orig_fptr = 0; class CHANNEL { public: THREAD_STARTROUTINE _startroutine; VOID* _arg; CHANNEL(): _startroutine(0), _arg(0) {} VOID Setup(THREAD_STARTROUTINE sr, VOID* a) { _startroutine = sr; _arg = a; } }; //typedef void *(*STARTROUTINE_TYPE)(void *); // Note: we should dynamically allocate and free channel, as shown below in the commented out "new channel" and "delete channel". // However, calling "new" and "delete" will invoke RC's thread local // storage support (when libpinpthread is used), which won't work here because // my_pthread_create() and my_wrapper_run() are using the application stack, not the pin stack // for they arenot jitted. static CHANNEL channel; void* my_wrapper_run(void* wrapper_arg) { printf("TOOL: Inside my_wrapper_run()\n"); fflush(stdout); CHANNEL* channel = reinterpret_cast (wrapper_arg); void* result = PIN_JitThreadStartRoutine(channel->_startroutine, channel->_arg); #if 0 delete channel; #endif printf("TOOL: after PIN_JitThreadStartRoutine()\n"); fflush(stdout); return result; } int my_pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg) { printf("TOOL: Inside my_pthread_create()\n"); fflush(stdout); REPLACEFUNTYPE fun = reinterpret_cast(orig_fptr); // channel must be allocated on the heap instead of on the stack frame on // my_pthread_create() since channel could be dereferenced in PIN_JitThreadStartRoutine() // after we return from my_pthread_create() #if 1 channel.Setup(start_routine, arg); #else CHANNEL* channel = new CHANNEL(start_routine, arg); ASSERTX(channel); #endif return fun(thread, attr, my_wrapper_run, &channel); } VOID ImageLoad(IMG img, VOID * v) { //printf("TOOL: img %s is loaded\n", IMG_Name(img).c_str()); //fflush(stdout); RTN rtn = RTN_FindByName(img, replacedfunname); if (RTN_Valid(rtn)) { //printf("TOOL: Pthread create found\n"); //fflush(stdout); RTN_ReplacedByNativeCallToFun(rtn, reinterpret_cast(my_pthread_create)); orig_fptr = RTN_Funptr(rtn); } } VOID Fini(INT32 code, VOID *) { fprintf(stderr, "\nTOOL: Done\n"); } int main(INT32 argc, CHAR **argv) { PIN_InitSymbols(); PIN_Init(argc, argv); IMG_AddInstrumentFunction(ImageLoad, 0); PIN_AddFiniFunction(Fini, 0); // Never returns PIN_StartProgram(); return 0; }