-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddtrace.cpp
More file actions
140 lines (117 loc) · 4.15 KB
/
addtrace.cpp
File metadata and controls
140 lines (117 loc) · 4.15 KB
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
137
138
139
140
#include "pin.H"
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::cerr;
using std::endl;
#define BILLION 1000000000
std::ostream* out = &cerr;
UINT64 fastForwardIns; // number of instruction to fast forward
UINT64 maxIns; // maximum number of instructions to simulate
UINT64 icount = 0; //number of dynamically executed instructions
KNOB< std::string > KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "HW1.out", "specify file name for HW1 output");
KNOB< UINT64 > KnobFastForwardCount(KNOB_MODE_WRITEONCE, "pintool", "f", "0", "Number of instructions to fast forward");
UINT64 x = 0;
int ctr = 0;
//Fast-forward logic
VOID InsCount(void)
{
if ( x>=BILLION ){
ctr++;
cerr << "COUNT = " << ctr << endl;
x = 0;
}
x++;
icount++;
}
ADDRINT FastForward(void)
{
return (icount >= fastForwardIns && icount < maxIns);
}
ADDRINT Terminate(void)
{
return (icount >= maxIns);
}
void MyExitRoutine() {
*out << "Total ins = " << icount << endl;
exit(0);
}
// Print a memory read record
VOID RecordMemRead(VOID * addr, UINT32 size)
{
*out << (ADDRINT)addr << " " << size << "\n";
}
// Print a memory write record
VOID RecordMemWrite(VOID * addr, UINT32 size)
{
*out << (ADDRINT)addr << " " << size << "\n";
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
// Instruments memory accesses using a predicated call, i.e.
// the instrumentation is called iff the instruction will actually be executed.
//
// On the IA-32 and Intel(R) 64 architectures conditional moves and REP
// prefixed instructions appear as predicated instructions in Pin.
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR) Terminate, IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR) MyExitRoutine, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) InsCount, IARG_END);
UINT32 memOperands = INS_MemoryOperandCount(ins);
// Iterate over each memory operand of the instruction.
for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp))
{
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR) FastForward, IARG_END);
INS_InsertThenPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_MEMORYOP_EA, memOp,
IARG_UINT32, INS_MemoryOperandSize(ins, memOp),
IARG_END);
}
// Note that in some architectures a single memory operand can be
// both read and written (for instance incl (%eax) on IA-32)
// In that case we instrument it once for read and once for write.
if (INS_MemoryOperandIsWritten(ins, memOp))
{
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR) FastForward, IARG_END);
INS_InsertThenPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_MEMORYOP_EA, memOp,
IARG_UINT32, INS_MemoryOperandSize(ins, memOp),
IARG_END);
}
}
}
VOID Fini(INT32 code, VOID *v)
{
cerr << "FINISHED" << endl;
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR( "This Pintool prints a trace of memory addresses\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv)) return Usage();
fastForwardIns = KnobFastForwardCount.Value() * BILLION;
maxIns = fastForwardIns + BILLION;
std::string fileName = KnobOutputFile.Value();
if (!fileName.empty()) {
out = new std::ofstream(fileName.c_str());
}
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
// Never returns
PIN_StartProgram();
return 0;
}