-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tool1.cpp
324 lines (260 loc) · 8.58 KB
/
Tool1.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <iostream>
#include <fstream>
#include "pin.H"
#include <fstream>
/*
#if defined(TARGET_MAC)
#define MALLOC "_malloc"
#define FREE "_free"
#else
#define MALLOC "malloc"
#define FREE "free"
#endif
*/
#define DUMMY_FIXED_PIM_B "_dummy_fixed_pim_begin"
#define DUMMY_FIXED_PIM_E "_dummy_fixed_pim_end"
#define DUMMY_GEN_PIM_B "_dummy_gen_pim_begin"
#define DUMMY_GEN_PIM_E "_dummy_gen_pim_end"
ofstream OutFile;
static UINT64 icount = 0;
bool fixed_pim_flag = false;
bool gen_pim_flag = false;
UINT64 reg_mem_ref = 0;
UINT64 reg_comp_ref = 0;
UINT64 fixed_pim_count = 0;
UINT64 gen_pim_mem_ref = 0;
UINT64 gen_pim_comp_ref = 0;
/*
VOID docount() {
icount++;
}
VOID FixPimCount()
{
fixed_pim_count++;
}
VOID GenPimMemCount()
{
gen_pim_mem_ref++;
}
VOID GenPimCoRef()
{
gen_pim_comp_ref++;
}
VOID RegMemRef()
{
reg_mem_ref++;
}
VOID RegCoRef()
{
reg_comp_ref++;
}
*/
VOID fixed_pim_flag_set1(bool fixed_pim_flag)
{
fixed_pim_flag = true;
}
VOID fixed_pim_flag_set2(bool fixed_pim_flag)
{
fixed_pim_flag = false;
}
VOID gen_pim_flag_set1(bool gen_pim_flag)
{
gen_pim_flag = true;
}
VOID gen_pim_flag_set2(bool gen_pim_flag)
{
gen_pim_flag = false;
}
VOID Image(IMG img, VOID *v)
{
RTN fixed_pim_b = RTN_FindByName(img, DUMMY_FIXED_PIM_B);
if (RTN_Valid(fixed_pim_b))
{
RTN_Open(fixed_pim_b);
// Instrument DUMMY_FIXED_PIM_B
RTN_InsertCall(fixed_pim_b, IPOINT_AFTER, (AFUNPTR)fixed_pim_flag_set1,
IARG_END);
RTN_Close(fixed_pim_b);
}
RTN fixed_pim_e = RTN_FindByName(img, DUMMY_FIXED_PIM_E);
if (RTN_Valid(fixed_pim_e))
{
RTN_Open(fixed_pim_e);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(fixed_pim_e, IPOINT_AFTER, (AFUNPTR) fixed_pim_flag_set2,
IARG_END);
RTN_Close(fixed_pim_e);
}
RTN gen_pim_b = RTN_FindByName(img, DUMMY_GEN_PIM_B);
if (RTN_Valid(gen_pim_b))
{
RTN_Open(gen_pim_b);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(gen_pim_b , IPOINT_AFTER, (AFUNPTR)gen_pim_flag_set1,
IARG_END);
RTN_Close(gen_pim_b);
}
RTN gen_pim_e = RTN_FindByName(img, DUMMY_GEN_PIM_E);
if (RTN_Valid(gen_pim_e))
{
RTN_Open(gen_pim_e);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(gen_pim_e, IPOINT_AFTER, (AFUNPTR)gen_pim_flag_set2,
IARG_END);
RTN_Close(gen_pim_e);
}
/*
UINT32 count = 0;
for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec))
{
for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn))
{
// Prepare for processing of RTN, an RTN is not broken up into BBLs,
// it is merely a sequence of INSs
RTN_Open(rtn);
for (INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
{
count++;
}
// to preserve space, release data associated with RTN after we have processed it
RTN_Close(rtn);
}
}
fprintf(stderr, "Image %s has %d instructions\n", IMG_Name(img).c_str(), count);
*/
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
if(fixed_pim_flag)
{
fixed_pim_count++;
return;
}
UINT32 memOperands = INS_MemoryOperandCount(ins);
if(gen_pim_flag)
{
if(memOperands != 0)
gen_pim_mem_ref++;
else
gen_pim_comp_ref++;
return;
}
if(memOperands != 0)
reg_mem_ref++;
else
reg_comp_ref++;
return;
#if 0
UINT32 memOperands = INS_MemoryOperandCount(ins);
RTN rtn = INS_Rtn(ins);
if ( RTN_Valid(rtn) && fixed_pim_flag)
{
FixPimCount();
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)FixPimCount, IARG_END);
// icount++;//++insNoRtnDiscoveredCount;
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)InsNativeCount, IARG_END);
}
else if ( RTN_IsDynamic(rtn) && gen_pim_flag)
{
for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if ((INS_MemoryOperandIsRead(ins, memOp) || INS_MemoryOperandIsWritten(ins, memOp)) )
{
/* INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)GenPimMemCount,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_END);*/
GenPimMemCount();
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)GenPimMemCount, IARG_END);
}
else {
GenPimCoRef();
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)GenPimCoRef, IARG_END);
}
}
}
else
{
for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp) || INS_MemoryOperandIsWritten(ins, memOp))
{
/*INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RegMemRef,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_END);*/
RegMemRef();
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegMemRef, IARG_END);
}
else
{ RegCoRef();
// INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegCoRef, IARG_END);
}
}
}
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)FixPimCount, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)GenPimMemCount, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)GenPimCoRef, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegMemRef, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegCoRef, IARG_END);
#endif
}
// Pin calls this function every time a new instruction is encountered
/*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);
}*/
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "toolf.out", "specify output file name");
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
// Write to a file since cout and cerr maybe closed by the application
OutFile << "===============================================" << endl;
OutFile << "reg_mem_ref= " << reg_mem_ref << endl;
OutFile << "reg_comp_ref= "<<reg_comp_ref << endl;
OutFile << "fixed_pim_count= "<<fixed_pim_count << endl;
// *OutFile << "===============================================" << endl;
OutFile << "gen_pim_mem_ref= "<<gen_pim_mem_ref << endl;
OutFile << "gen_pim_comp_ref= "<<gen_pim_comp_ref << endl;
OutFile << "Count: " << icount << endl;
OutFile << "===============================================" << endl;
//OutFile << " Count " << icount<<" reg_mem_ref= " << reg_mem_ref <<" reg_comp_ref= "<<reg_comp_ref<< " fixed_pim_count= "<<fixed_pim_count<<" gen_pim_mem_ref= "<<gen_pim_mem_ref<<" gen_pim_comp_ref= "<<gen_pim_comp_ref<<endl;
OutFile.close();
}
/* ===================================================================== */
/* 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());
OutFile.setf(ios::showbase);
PIN_InitSymbols();
// Register ImageLoad to be called to instrument instructions
IMG_AddInstrumentFunction(Image, 0);
// 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;
}