|
| 1 | + |
| 2 | +/*! @file |
| 3 | + * This is an example of the PIN tool that demonstrates some basic PIN APIs |
| 4 | + * and could serve as the starting point for developing your first PIN tool |
| 5 | + */ |
| 6 | + |
| 7 | +#include "pin.H" |
| 8 | +#include <iostream> |
| 9 | +#include <fstream> |
| 10 | + |
| 11 | +/* ================================================================== */ |
| 12 | +// Global variables |
| 13 | +/* ================================================================== */ |
| 14 | + |
| 15 | +UINT64 insCount = 0; //number of dynamically executed instructions |
| 16 | +UINT64 bblCount = 0; //number of dynamically executed basic blocks |
| 17 | +UINT64 threadCount = 0; //total number of threads, including main thread |
| 18 | + |
| 19 | +std::ostream * out = &cerr; |
| 20 | + |
| 21 | +/* ===================================================================== */ |
| 22 | +// Command line switches |
| 23 | +/* ===================================================================== */ |
| 24 | +KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", |
| 25 | + "o", "", "specify file name for MyPinTool output"); |
| 26 | + |
| 27 | +KNOB<BOOL> KnobCount(KNOB_MODE_WRITEONCE, "pintool", |
| 28 | + "count", "1", "count instructions, basic blocks and threads in the application"); |
| 29 | + |
| 30 | + |
| 31 | +/* ===================================================================== */ |
| 32 | +// Utilities |
| 33 | +/* ===================================================================== */ |
| 34 | + |
| 35 | +/*! |
| 36 | + * Print out help message. |
| 37 | + */ |
| 38 | +INT32 Usage() |
| 39 | +{ |
| 40 | + cerr << "This tool prints out the number of dynamically executed " << endl << |
| 41 | + "instructions, basic blocks and threads in the application." << endl << endl; |
| 42 | + |
| 43 | + cerr << KNOB_BASE::StringKnobSummary() << endl; |
| 44 | + |
| 45 | + return -1; |
| 46 | +} |
| 47 | + |
| 48 | +/* ===================================================================== */ |
| 49 | +// Analysis routines |
| 50 | +/* ===================================================================== */ |
| 51 | + |
| 52 | +/*! |
| 53 | + * Increase counter of the executed basic blocks and instructions. |
| 54 | + * This function is called for every basic block when it is about to be executed. |
| 55 | + * @param[in] numInstInBbl number of instructions in the basic block |
| 56 | + * @note use atomic operations for multi-threaded applications |
| 57 | + */ |
| 58 | +VOID CountBbl(UINT32 numInstInBbl) |
| 59 | +{ |
| 60 | + bblCount++; |
| 61 | + insCount += numInstInBbl; |
| 62 | +} |
| 63 | + |
| 64 | +/* ===================================================================== */ |
| 65 | +// Instrumentation callbacks |
| 66 | +/* ===================================================================== */ |
| 67 | + |
| 68 | +/*! |
| 69 | + * Insert call to the CountBbl() analysis routine before every basic block |
| 70 | + * of the trace. |
| 71 | + * This function is called every time a new trace is encountered. |
| 72 | + * @param[in] trace trace to be instrumented |
| 73 | + * @param[in] v value specified by the tool in the TRACE_AddInstrumentFunction |
| 74 | + * function call |
| 75 | + */ |
| 76 | +VOID Trace(TRACE trace, VOID *v) |
| 77 | +{ |
| 78 | + // Visit every basic block in the trace |
| 79 | + for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) |
| 80 | + { |
| 81 | + // Insert a call to CountBbl() before every basic bloc, passing the number of instructions |
| 82 | + BBL_InsertCall(bbl, IPOINT_BEFORE, (AFUNPTR)CountBbl, IARG_UINT32, BBL_NumIns(bbl), IARG_END); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/*! |
| 87 | + * Increase counter of threads in the application. |
| 88 | + * This function is called for every thread created by the application when it is |
| 89 | + * about to start running (including the root thread). |
| 90 | + * @param[in] threadIndex ID assigned by PIN to the new thread |
| 91 | + * @param[in] ctxt initial register state for the new thread |
| 92 | + * @param[in] flags thread creation flags (OS specific) |
| 93 | + * @param[in] v value specified by the tool in the |
| 94 | + * PIN_AddThreadStartFunction function call |
| 95 | + */ |
| 96 | +VOID ThreadStart(THREADID threadIndex, CONTEXT *ctxt, INT32 flags, VOID *v) |
| 97 | +{ |
| 98 | + threadCount++; |
| 99 | +} |
| 100 | + |
| 101 | +/*! |
| 102 | + * Print out analysis results. |
| 103 | + * This function is called when the application exits. |
| 104 | + * @param[in] code exit code of the application |
| 105 | + * @param[in] v value specified by the tool in the |
| 106 | + * PIN_AddFiniFunction function call |
| 107 | + */ |
| 108 | +VOID Fini(INT32 code, VOID *v) |
| 109 | +{ |
| 110 | + *out << "===============================================" << endl; |
| 111 | + *out << "MyPinTool analysis results: " << endl; |
| 112 | + *out << "Number of instructions: " << insCount << endl; |
| 113 | + *out << "Number of basic blocks: " << bblCount << endl; |
| 114 | + *out << "Number of threads: " << threadCount << endl; |
| 115 | + *out << "===============================================" << endl; |
| 116 | +} |
| 117 | + |
| 118 | +/*! |
| 119 | + * The main procedure of the tool. |
| 120 | + * This function is called when the application image is loaded but not yet started. |
| 121 | + * @param[in] argc total number of elements in the argv array |
| 122 | + * @param[in] argv array of command line arguments, |
| 123 | + * including pin -t <toolname> -- ... |
| 124 | + */ |
| 125 | +int main(int argc, char *argv[]) |
| 126 | +{ |
| 127 | + // Initialize PIN library. Print help message if -h(elp) is specified |
| 128 | + // in the command line or the command line is invalid |
| 129 | + if( PIN_Init(argc,argv) ) |
| 130 | + { |
| 131 | + return Usage(); |
| 132 | + } |
| 133 | + |
| 134 | + string fileName = KnobOutputFile.Value(); |
| 135 | + |
| 136 | + if (!fileName.empty()) { out = new std::ofstream(fileName.c_str());} |
| 137 | + |
| 138 | + if (KnobCount) |
| 139 | + { |
| 140 | + // Register function to be called to instrument traces |
| 141 | + TRACE_AddInstrumentFunction(Trace, 0); |
| 142 | + |
| 143 | + // Register function to be called for every thread before it starts running |
| 144 | + PIN_AddThreadStartFunction(ThreadStart, 0); |
| 145 | + |
| 146 | + // Register function to be called when the application exits |
| 147 | + PIN_AddFiniFunction(Fini, 0); |
| 148 | + } |
| 149 | + |
| 150 | + cerr << "===============================================" << endl; |
| 151 | + cerr << "This application is instrumented by MyPinTool" << endl; |
| 152 | + if (!KnobOutputFile.Value().empty()) |
| 153 | + { |
| 154 | + cerr << "See file " << KnobOutputFile.Value() << " for analysis results" << endl; |
| 155 | + } |
| 156 | + cerr << "===============================================" << endl; |
| 157 | + |
| 158 | + // Start the program, never returns |
| 159 | + PIN_StartProgram(); |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +/* ===================================================================== */ |
| 165 | +/* eof */ |
| 166 | +/* ===================================================================== */ |
0 commit comments