| 1 |
/*BEGIN_LEGAL |
| 2 |
Intel Open Source License |
| 3 |
|
| 4 |
Copyright (c) 2002-2012 Intel Corporation. All rights reserved. |
| 5 |
|
| 6 |
Redistribution and use in source and binary forms, with or without |
| 7 |
modification, are permitted provided that the following conditions are |
| 8 |
met: |
| 9 |
|
| 10 |
Redistributions of source code must retain the above copyright notice, |
| 11 |
this list of conditions and the following disclaimer. Redistributions |
| 12 |
in binary form must reproduce the above copyright notice, this list of |
| 13 |
conditions and the following disclaimer in the documentation and/or |
| 14 |
other materials provided with the distribution. Neither the name of |
| 15 |
the Intel Corporation nor the names of its contributors may be used to |
| 16 |
endorse or promote products derived from this software without |
| 17 |
specific prior written permission. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR |
| 23 |
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
END_LEGAL */ |
| 31 |
/* |
| 32 |
* This tool counts the number of times a routine is executed and |
| 33 |
* the number of instructions executed in a routine |
| 34 |
*/ |
| 35 |
|
| 36 |
#include <fstream> |
| 37 |
#include <iomanip> |
| 38 |
#include <iostream> |
| 39 |
#include <string.h> |
| 40 |
#include "pin.H" |
| 41 |
#include "instrument.h" |
| 42 |
|
| 43 |
// record one event before function foo is called |
| 44 |
|
| 45 |
/* ===================================================================== */ |
| 46 |
/* Print Help Message */ |
| 47 |
/* ===================================================================== */ |
| 48 |
|
| 49 |
INT32 Usage() { |
| 50 |
cerr << "This Pintool counts the number of times a routine is executed" |
| 51 |
<< endl; |
| 52 |
cerr << "and the number of instructions executed in a routine" << endl; |
| 53 |
cerr << endl << KNOB_BASE::StringKnobSummary() << endl; |
| 54 |
return -1; |
| 55 |
} |
| 56 |
|
| 57 |
int instrument_function(IMG img, const char* function_name, AFUNPTR callback) { |
| 58 |
__init(); |
| 59 |
|
| 60 |
RTN rtn = RTN_FindByName(img, function_name); |
| 61 |
if (rtn == RTN_Invalid()) { |
| 62 |
//printf("RTN_FindByName failed\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
RTN_Open(rtn); |
| 67 |
RTN_InsertCall(rtn, IPOINT_BEFORE, callback, IARG_UINT32, 1, IARG_END); |
| 68 |
RTN_InsertCall(rtn, IPOINT_AFTER, callback, IARG_UINT32, 2, IARG_END); |
| 69 |
RTN_Close(rtn); |
| 70 |
printf("callback inserted for function %s\n", function_name); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
void my_function(int n) { |
| 75 |
//printf("my_function was called with param %d\n", n); |
| 76 |
record_event0(1); |
| 77 |
} |
| 78 |
|
| 79 |
void my_fini(void* arg) { |
| 80 |
__conclude(); |
| 81 |
} |
| 82 |
|
| 83 |
VOID ImageLoad(IMG img, VOID *v) { |
| 84 |
printf("Loading %s, Image id = %d\n", IMG_Name(img).c_str(), IMG_Id(img)); |
| 85 |
//instrument_function(img, "foo", AFUNPTR(record_event0)); |
| 86 |
instrument_function(img, "compute_", AFUNPTR(my_function)); |
| 87 |
instrument_function(img, "dist_", AFUNPTR(my_function)); |
| 88 |
instrument_function(img, "initialize_", AFUNPTR(my_function)); |
| 89 |
instrument_function(img, "timestamp_", AFUNPTR(my_function)); |
| 90 |
instrument_function(img, "update_", AFUNPTR(my_function)); |
| 91 |
} |
| 92 |
|
| 93 |
/* ===================================================================== */ |
| 94 |
/* Main */ |
| 95 |
/* ===================================================================== */ |
| 96 |
|
| 97 |
int main(int argc, char * argv[]) { |
| 98 |
// Initialize symbol table code, needed for rtn instrumentation |
| 99 |
PIN_InitSymbols(); |
| 100 |
|
| 101 |
// Initialize pin |
| 102 |
if (PIN_Init(argc, argv)) |
| 103 |
return Usage(); |
| 104 |
|
| 105 |
IMG img = IMG_Open(argv[argc - 1]); |
| 106 |
if (img == IMG_Invalid()) { |
| 107 |
printf("Can't open file %s\n", argv[argc - 1]); |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
PIN_AddFiniFunction(FINI_CALLBACK(my_fini), NULL); |
| 112 |
|
| 113 |
IMG_AddInstrumentFunction(ImageLoad, 0); |
| 114 |
|
| 115 |
// Start the program, never returns |
| 116 |
PIN_StartProgram(); |
| 117 |
return 0; |
| 118 |
} |