-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_tracer.cpp
327 lines (234 loc) · 8.1 KB
/
branch_tracer.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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <stddef.h>
#include "pin.H"
#include "portability.H"
#include <inttypes.h>
//#include "gzstream.h"
using namespace std;
PIN_LOCK lock;
INT32 numThreads = 0;
const INT32 MaxNumThreads = 4;
struct THREAD_DATA
{
UINT64 _count;
UINT64 _icount_last;
UINT64 _sincelast;
UINT64 _branch;
};
THREAD_DATA t_icount[MaxNumThreads];
static UINT32 icount = 0;
UINT32 icount_lastmem;
UINT64 sincelast=0;
//VOID docount() { icount++; }
VOID docount(THREADID tid) {
t_icount[tid]._count++;
}
int brcount(THREADID tid) {
t_icount[tid]._branch++;
//return t_icount[tid]._branch;
if(t_icount[tid]._count == t_icount[tid]._icount_last) sincelast = 0;
else {
sincelast = t_icount[tid]._count - t_icount[tid]._icount_last - 1;
t_icount[tid]._icount_last = t_icount[tid]._count;
}
}
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "buffer.out", "output file");
/*
* The ID of the buffer
*/
BUFFER_ID bufId;
/*
* Thread specific data
*/
TLS_KEY mlog_key;
/*
* Number of OS pages for the buffer
*/
#define NUM_BUF_PAGES 1024
/*
* Record of memory references. Rather than having two separate
* buffers for reads and writes, we just use one struct that includes a
* flag for type.
*/
struct COND_BR
{
ADDRINT pc;
BOOL taken;
UINT32 size;
ADDRINT target;
};
/*
* MLOG - thread specific data that is not handled by the buffering API.
*/
class MLOG
{
public:
MLOG(THREADID tid);
~MLOG();
VOID DumpBufferToFile( struct COND_BR * reference, UINT64 numElements, THREADID tid );
private:
//ofstream _ofile;
FILE* _ofile;
// FILE * trace[2];
};
MLOG::MLOG(THREADID tid)
{
// string filename = KnobOutputFile.Value() + "." + decstr(getpid_portable()) + "." + decstr(tid);
//string filename = "bzip2 -c > " + KnobOutputFile.Value() + "." + decstr(getpid_portable()) + "." + decstr(tid);
// _ofile[tid] = popen(filename.c_str(),"w");
//_ofile.open(filename.c_str());
//string filename = "bzip2 -c > " + KnobOutputFile.Value() + "." + decstr(getpid_portable()) + "." + decstr(tid);
//string filename = "xz -2 -c > " + KnobOutputFile.Value() + "." + decstr(getpid_portable()) + "." + decstr(tid);
string filename = "xz -2 -c > " + KnobOutputFile.Value() + "." + decstr(tid);
// cout << filename << " " << sizeof(ADDRINT) <<endl;
//FILE* temp = popen(filename.c_str(),"w");
//_ofile(temp);
_ofile = popen(filename.c_str(),"w");
if ( ! _ofile )
{
cerr << "Error: could not open output file." << endl;
exit(1);
}
//_ofile << hex;
}
MLOG::~MLOG()
{
// _ofile.close();
}
VOID MLOG::DumpBufferToFile( struct COND_BR * reference, UINT64 numElements, THREADID tid )
{
char taken;
for(UINT64 i=0; i<numElements; i++, reference++)
{
if (reference->taken == 1)
taken='T';
else
taken='N';
if (reference->pc != 0)
//_ofile << reference->read << " " << static_cast<UINT32> (reference->pc) << " " << static_cast<UINT32> (reference->ea) << " " << reference->size << endl;
// _ofile << reference->read << " " << hex << (reference->pc) << " " << hex << (reference->ea) << " " << dec << reference->size << endl;
fprintf(_ofile, "%p %c %u\n",reference->pc, taken, reference->size);
// fprintf(_ofile, "%c",ld_st);
//char str[] = sprintf (str, "%c %p %p %u", ld_st, reference->pc, reference->ea, reference->size);
//fwrite(str , 1 , sizeof(str) , trace[i] );
}
}
/*
* Insert code to write data to a thread-specific buffer for instructions
* that access memory.
*/
VOID Trace(TRACE trace, VOID *v)
{
for(BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl=BBL_Next(bbl))
{
for(INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins=INS_Next(ins))
{
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount,IARG_THREAD_ID, IARG_END);
if (INS_Category(ins) == XED_CATEGORY_COND_BR)
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)brcount,IARG_THREAD_ID, IARG_END);
// if(INS_IsBranchOrCall(ins))
/* if(icount == icount_lastmem) sincelast = 0;
else {
sincelast = icount - icount_lastmem - 1;
icount_lastmem = icount;
}
*/
INS_InsertFillBuffer(ins, IPOINT_BEFORE, bufId,
IARG_INST_PTR, offsetof(struct COND_BR, pc),
IARG_BRANCH_TAKEN,offsetof(struct COND_BR, taken),
IARG_UINT32, sincelast, offsetof(struct COND_BR, size),
IARG_END);
}
}
}
}
/**************************************************************************
*
* Callback Routines
*
**************************************************************************/
VOID * BufferFull(BUFFER_ID id, THREADID tid, const CONTEXT *ctxt, VOID *buf,
UINT64 numElements, VOID *v)
{
struct COND_BR * reference=(struct COND_BR*)buf;
MLOG * mlog = static_cast<MLOG*>( PIN_GetThreadData( mlog_key, tid ) );
mlog->DumpBufferToFile( reference, numElements, tid );
return buf;
}
/*
* Note that opening a file in a callback is only supported on Linux systems.
* See buffer-win.cpp for how to work around this issue on Windows.
*/
VOID ThreadStart(THREADID tid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
// There is a new MLOG for every thread. Opens the output file.
MLOG * mlog = new MLOG(tid);
// A thread will need to look up its MLOG, so save pointer in TLS
PIN_SetThreadData(mlog_key, mlog, tid);
}
VOID ThreadFini(THREADID tid, const CONTEXT *ctxt, INT32 code, VOID *v)
{
MLOG * mlog = static_cast<MLOG*>(PIN_GetThreadData(mlog_key, tid));
delete mlog;
cout << "Count[" << decstr(tid) << "]= " << t_icount[tid]._count << endl;
cout << "Branch[" << decstr(tid) << "]= " << t_icount[tid]._branch << endl;
PIN_SetThreadData(mlog_key, 0, tid);
}
/* ===================================================================== */
/* 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();
// Initialize PIN library. Print help message if -h(elp) is specified
// in the command line or the command line is invalid
if( PIN_Init(argc,argv) )
{
return Usage();
}
// Initialize the memory reference buffer;
// set up the callback to process the buffer.
//
bufId = PIN_DefineTraceBuffer(sizeof(struct COND_BR), NUM_BUF_PAGES,
BufferFull, 0);
if(bufId == BUFFER_ID_INVALID)
{
cerr << "Error: could not allocate initial buffer" << endl;
return 1;
}
// Initialize thread-specific data not handled by buffering api.
mlog_key = PIN_CreateThreadDataKey(0);
//OutFile.open(KnobOutputFile.Value().c_str());
// trace = fopen("mem.trace", "w");
// add an instrumentation function
TRACE_AddInstrumentFunction(Trace, 0);
// Register Instruction to be called to instrument instructions
// INS_AddInstrumentFunction(Instruction, 0);
// add callbacks
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
// Register Fini to be called when the application exits
// PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}