-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtracer.cpp
136 lines (102 loc) · 3.45 KB
/
memtracer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include "pin.H"
static UINT32 icount = 0;
UINT32 icount_lastmem;
UINT32 sincelast;
UINT32 memrefs; // number of memory accesses so far
#define MAX_REFS 200000000 // we want to limit the size of the log files so we will limit trace files to 2M lines
FILE * trace;
// This function is called before every instruction is executed
VOID docount() { icount++; }
// update lastmem info and write to file (load)
VOID RecordMemRead(VOID * ip, VOID * addr)
{
if(icount == icount_lastmem) sincelast = 0;
else {
sincelast = icount - icount_lastmem - 1;
icount_lastmem = icount;
}
fprintf(trace,"l %p %p %u\n", ip, addr, sincelast);
++memrefs;
if(memrefs > MAX_REFS) {
cout << "Maximum number of memops reached. Stopping instrumentation... " << endl;
fclose(trace);
PIN_Detach();
}
}
// Print a memory write record
VOID RecordMemWrite(VOID * ip, VOID * addr)
{
if(icount == icount_lastmem) sincelast = 0;
else {
sincelast = icount - icount_lastmem - 1;
icount_lastmem = icount;
}
fprintf(trace,"s %p %p %u\n", ip, addr, sincelast);
++memrefs;
if(memrefs > MAX_REFS) {
cout << "Maximum number of memops reached. Stopping instrumentation... " << endl;
fclose(trace);
PIN_Detach();
}
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
// Insert a call to docount before every instruction, no arguments are passed
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
// instruments loads using a predicated call, i.e.
// the call happens iff the load will be actually executed
// (this does not matter for ia32 but arm and ipf have predicated instructions)
if (INS_IsMemoryRead(ins))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYREAD_EA,
IARG_END);
}
// instruments stores using a predicated call, i.e.
// the call happens iff the store will be actually executed
if (INS_IsMemoryWrite(ins))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_INST_PTR,
IARG_MEMORYWRITE_EA,
IARG_END);
}
}
VOID Fini(INT32 code, VOID *v)
{
fclose(trace);
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
cerr << "This tool counts the number of dynamic instructions executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
/* argc, argv are the entire command line: pin -t <toolname> -- ... */
/* ===================================================================== */
int main(int argc, char * argv[])
{
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
//OutFile.open(KnobOutputFile.Value().c_str());
trace = fopen("mem.trace", "w");
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(Instruction, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}