-
Notifications
You must be signed in to change notification settings - Fork 2
/
InsnProfile.cpp
executable file
·109 lines (94 loc) · 2.61 KB
/
InsnProfile.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
#include "pin.H"
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <vector>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
UINT32 numInsns = 0;
vector<ADDRINT> addr;
vector<string> disas;
vector<string> fname;
vector<string> srcloc;
vector<UINT64> icount;
/*
* utility method: strip the path out of a filename
*/
inline const char* stripPath (const char *fn)
{
const char *name = strrchr(fn, '/');
if (name) {
return name+1;
} else {
return fn;
}
}
/*
* analysis function (called whenever an instruction is executed)
*/
VOID incrementInsnCount (UINT32 id)
{
icount[id]++;
}
/*
* instrumentation procedure (called when a new instruction is executed for the
* first time)
*/
VOID instrumentInstruction (INS insn, VOID *v)
{
RTN func = INS_Rtn(insn);
if (RTN_Valid(func)) {
// save instruction address, disassembly, and function name
UINT32 newID = numInsns++;
addr.push_back(INS_Address(insn));
disas.push_back(INS_Disassemble(insn));
fname.push_back(PIN_UndecorateSymbolName(RTN_Name(INS_Rtn(insn)),
UNDECORATION_NAME_ONLY));
// save instruction source file and lineno (requires debug symbols)
INT32 line, col;
string fn;
PIN_GetSourceLocation(INS_Address(insn), &col, &line, &fn);
std::stringstream ss("");
ss << "[" << stripPath(fn.c_str()) << ":" << line << "]";
srcloc.push_back(ss.str());
// add instrumentation
icount.push_back(0);
INS_InsertCall(insn, IPOINT_BEFORE, (AFUNPTR)incrementInsnCount,
IARG_UINT32, newID, IARG_END);
}
}
/*
* output procedure (called when program exits)
*/
VOID printResults (INT32 code, VOID *v)
{
UINT64 totalExecs = 0;
for (UINT32 i = 0; i < numInsns; i++) {
if (icount[i] > 0) {
cout << std::setw(10) << icount[i] << " " << fname[i] << ": "
<< disas[i] << " src=" << srcloc[i] <<endl;
totalExecs += icount[i];
}
}
cout << "Total instruction execution count: " << totalExecs << endl;
}
int main(int argc, char *argv[])
{
// initialize Pin
if (PIN_Init(argc,argv)) {
cout << "Usage: pin -t <tool name> -- <exefile>" << endl;
return -1;
}
PIN_InitSymbols();
PIN_SetSyntaxATT();
// install Pin callbacks
INS_AddInstrumentFunction(instrumentInstruction, 0);
PIN_AddFiniFunction(printResults, 0);
// start the program (never returns!)
PIN_StartProgram();
return 0;
}