-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproccount.cpp
229 lines (176 loc) · 5.58 KB
/
proccount.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
/* ---- This file is modified by Harinder Pal (2014MCS2123) --- */
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string.h>
#include "pin.H"
FILE * trace;
ofstream outFile;
//string prev;
string operation = "";
string fn;
VOID RecordMemRead(VOID * ip, VOID * addr, string fn)
{
//outFile << ip << " :R: " << addr;
//operation = operation + (string)ip;
outFile << ip << " :R: " << addr << endl;
//fprintf(trace,"%p: R %p\n", ip, addr);
}
/*VOID GetBasePtr(ADDRINT reg)
{
outFile << "reached" << reg << endl;
}*/
// Print a memory write record
VOID RecordMemWrite(VOID * ip, VOID * addr, string fn)
{
//fprintf(trace,"%p: W %p\n", ip, addr);
outFile << ip << " :W: " << addr << endl;
}
// Holds instruction count for a single procedure
/*typedef struct RtnCount
{
string _name;
string _image;
ADDRINT _address;
RTN _rtn;
UINT64 _rtnCount;
UINT64 _icount;
struct RtnCount * _next;
} RTN_COUNT;*/
/*typedef struct RtnName
{
string _name;
string _op;
struct RtnCount * _next;
} RTN_NAME;
RTN_NAME * RtnList = 0;*/
// Linked list of instruction counts for each routine
//RTN_COUNT * RtnList = 0;
// This function is called before every instruction is executed
/*VOID docount(UINT64 * counter)
{
(*counter)++;
}*/
/*const char * StripPath(const char * path)
{
const char * file = strrchr(path,'/');
if (file)
return file+1;
else
return path;
}*/
// Pin calls this function every time a new rtn is executed
VOID Routine(RTN rtn, VOID *v)
{
/*string current_prev = "None";
if(RTN_Prev(rtn) != RTN_Invalid()) {
current_prev = RTN_Name(RTN_Prev(rtn));
}*/
if(RTN_Name(rtn) == fn) {
//prev = RTN_Name(rtn);
//outFile << prev;
//outFile << "Inside: " << RTN_Name(rtn) << endl;
//fprintf(trace,"Hello: ");
/*RTN_NAME * rn = new RTN_NAME;
rn->_name = RTN_Name(rtn);
rn->_next = RtnList;
RtnList = rn;*/
RTN_Open(rtn);
//string fn = RTN_Name(rtn);
for (INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins)) {
//RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)GetBasePtr, IARG_REG_VALUE, REG_STACK_PTR, IARG_END);
UINT32 memOperands = INS_MemoryOperandCount(ins);
for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp))
{
INS_InsertCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_END);
}
if (INS_MemoryOperandIsWritten(ins, memOp))
{
INS_InsertCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp, IARG_END);
}
}
}
/* // Allocate a counter for this routine
RTN_COUNT * rc = new RTN_COUNT;
// The RTN goes away when the image is unloaded, so save it now
// because we need it in the fini
rc->_name = RTN_Name(rtn);
rc->_image = StripPath(IMG_Name(SEC_Img(RTN_Sec(rtn))).c_str());
rc->_address = RTN_Address(rtn);
rc->_icount = 0;
rc->_rtnCount = 0;
// Add to list of routines
rc->_next = RtnList;
RtnList = rc;
RTN_Open(rtn);
// Insert a call at the entry point of a routine to increment the call count
RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)docount, IARG_PTR, &(rc->_rtnCount), IARG_END);
// For each instruction of the routine
for (INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
{
// Insert a call to docount to increment the instruction counter for this rtn
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_PTR, &(rc->_icount), IARG_END);
}
*/
RTN_Close(rtn);
}
}
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
fprintf(trace, "#eof\n");
fclose(trace);
/*outFile << setw(23) << "Procedure" << " "
<< setw(15) << "Image" << " "
<< setw(18) << "Address" << " "
<< setw(12) << "Calls" << " "
<< setw(12) << "Instructions" << endl;
for (RTN_COUNT * rc = RtnList; rc; rc = rc->_next)
{
if (rc->_icount > 0)
outFile << setw(23) << rc->_name << " "
<< setw(15) << rc->_image << " "
<< setw(18) << hex << rc->_address << dec <<" "
<< setw(12) << rc->_rtnCount << " "
<< setw(12) << rc->_icount << endl;
}*/
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
//cerr << "This Pintool counts the number of times a routine is executed" << endl;
//cerr << "and the number of instructions executed in a routine" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char * argv[])
{
PIN_InitSymbols();
outFile.open("myFile.out");
//trace = fopen("proccount.out", "w");
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
fn = argv[argc-1];
outFile << fn << endl;
// Register Routine to be called to instrument rtn
RTN_AddInstrumentFunction(Routine, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}