Repository navigation
Expand file tree
/
Copy pathinstcount.cpp
More file actions
198 lines (160 loc) · 4.84 KB
/
Copy pathinstcount.cpp
File metadata and controls
198 lines (160 loc) · 4.84 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
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
#include<iostream>
#include<fstream>
#include <set>
#include <map>
#include <string>
#include "pin.H"
#include "utils.h"
#include "instselector.h"
#include "libload.h"
//#include "faultinjection.h"
//#include "commonvars.h"
//#define INCLUDEALLINST
#define NOBRANCHES
//#define NOSTACKFRAMEOP
//#define ONLYFP
KNOB<string> instcount_file(KNOB_MODE_WRITEONCE, "pintool",
"o", "pin.instcount.txt", "specify instruction count file name");
static UINT64 fi_all = 0;
static UINT64 fi_ccs = 0;
static UINT64 fi_sp = 0;
static UINT64 fi_bp = 0;
VOID countAllInst() {fi_all++;}
VOID countCCSInst() {fi_ccs++;}
VOID countSPInst() {fi_sp++;}
VOID countBPInst() { fi_bp++;}
// Pin calls this function every time a new instruction is encountered
VOID CountInst(INS ins, VOID *v)
{
if(!is_inlib.Value()) {
if (!isValidInst(ins))
return;
}
int flag = 0;
if(is_inlib.Value())
{
if (!RTN_Valid(INS_Rtn(ins))) { // some library instructions do not have rtn !?
LOG("Invalid RTN " + INS_Disassemble(ins) + "\n");
return;
}
std::string image = IMG_Name(SEC_Img(RTN_Sec(INS_Rtn(ins))));
//std::cout << image.c_str() << std::endl;
for (std::vector<std::string>::iterator it = libs.begin(); it != libs.end(); ++it) {
// std::cout << "it " << *it << std::endl;
if (image.find(*it) != std::string::npos) {
flag = 1;
}
}
if (flag == 0) {
return;
}
}
//LOG("I am in "+RTN_Name(INS_Rtn(ins))+"\n");
//LOG("I am in "+IMG_Name(SEC_Img(RTN_Sec(INS_Rtn(ins))))+"\n");
#ifdef INCLUDEALLINST
int numW = INS_MaxNumWRegs(ins), mayChangeControlFlow = 0;
if(!INS_HasFallThrough(ins))
mayChangeControlFlow = 1;
for(int i =0; i < numW; i++){
reg = INS_RegW(ins, i);
if(reg == REG_RIP || reg == REG_EIP || reg == REG_IP) // conditional branches
{ mayChangeControlFlow = 1; break;}
}
if(mayChangeControlFlow) {
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)countAllInst,
IARG_END);
//LOG("No through\n");
}
else{
INS_InsertPredicatedCall(
ins, IPOINT_AFTER, (AFUNPTR)countAllInst,
IARG_END);
// LOG("ins SP:" + INS_Disassemble(ins) + "\n");
// LOG("reg:" + REG_StringShort(reg) +"\n");
//LOG(numW+"\n");
}
#else
#ifdef NOBRANCHES
if(INS_IsBranch(ins) || !INS_HasFallThrough(ins)) {
LOG("instcount: branch/ret inst: " + INS_Disassemble(ins) + "\n");
return;
}
#endif
// NOSTACKFRAMEOP must be used together with NOBRANCHES, IsStackWrite
// has a bug that does not put pop into the list
#ifdef NOSTACKFRAMEOP
if(INS_IsStackWrite(ins) || OPCODE_StringShort(INS_Opcode(ins)) == "POP") {
LOG("instcount: stack frame change inst: " + INS_Disassemble(ins) + "\n");
return;
}
#endif
#ifdef ONLYFP
int numW = INS_MaxNumWRegs(ins);
bool hasfp = false;
for (int i = 0; i < numW; i++){
if (reg_map.isFloatReg(reg)) {
hasfp = true;
break;
}
}
if (!hasfp){
return;
}
#endif
// select instruction based on instruction type
if(!isInstFITarget(ins))
return;
INS_InsertPredicatedCall(
ins, IPOINT_AFTER, (AFUNPTR)countAllInst,
IARG_END);
#endif
}
// bool mayChangeControlFlow(INS ins){
// REG reg;
// if(!INS_HasFallThrough(ins))
// return true;
// int numW = INS_MaxNumWRegs(ins);
// for(int i =0; i < numW; i++){
// if(reg == REG_RIP || reg == REG_EIP || reg == REG_IP) // conditional branches
// return true;
// }
// return false;
// }
// 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
ofstream OutFile;
OutFile.open(instcount_file.Value().c_str());
OutFile.setf(ios::showbase);
OutFile <<"AllInst:"<< fi_all << endl;
OutFile <<"CCSavedInst:"<< fi_ccs << endl;
OutFile << "SPInst:"<< fi_sp << endl;
OutFile << "FPInst:"<< fi_bp << 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;
}
int main(int argc, char * argv[])
{
PIN_InitSymbols();
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
configInstSelector();
parseLibNames(libnames.Value());
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(CountInst, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}