1 // Simple test for PIN.
2 // Prints the number of memory accesses.
3 // Run: $PIN_ROOT/pin -t `pwd`/simple_pin_test.so -- your_program
4 #include "pin.H"
5
6 #include <map>
7
8 // statistics
9 static long long dynamic_memory_access_count;
10 static int static_memory_access_count;
11
12 //---------- Instrumentation functions ---------
InsertBeforeEvent_MemoryAccess(ADDRINT pc)13 void InsertBeforeEvent_MemoryAccess(ADDRINT pc) {
14 dynamic_memory_access_count++;
15 }
16
17 //-------------- PIN callbacks ---------------
CallbackForTRACE(TRACE trace,void * v)18 void CallbackForTRACE(TRACE trace, void *v) {
19 for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
20 for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins)) {
21 if (INS_IsStackRead(ins) || INS_IsStackWrite(ins))
22 continue;
23 if (INS_IsMemoryRead(ins) || INS_IsMemoryWrite(ins)) {
24 static_memory_access_count++;
25 INS_InsertCall(ins, IPOINT_BEFORE,
26 (AFUNPTR)InsertBeforeEvent_MemoryAccess,
27 IARG_INST_PTR, IARG_END);
28 }
29 }
30 }
31 }
32
CallbackForFini(INT32 code,void * v)33 static void CallbackForFini(INT32 code, void *v) {
34 printf("accesses static : %d\n", static_memory_access_count);
35 printf("accesses dynamic : %lld\n", dynamic_memory_access_count);
36 }
37
38 //---------------- main ---------------
main(INT32 argc,CHAR ** argv)39 int main(INT32 argc, CHAR **argv) {
40 PIN_Init(argc, argv);
41 PIN_InitSymbols();
42 PIN_AddFiniFunction(CallbackForFini, 0);
43 TRACE_AddInstrumentFunction(CallbackForTRACE, 0);
44 PIN_StartProgram();
45 printf("accesses static : %d\n", static_memory_access_count);
46 return 0;
47 }
48