-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinLoopCov.cpp
More file actions
158 lines (119 loc) · 3.87 KB
/
pinLoopCov.cpp
File metadata and controls
158 lines (119 loc) · 3.87 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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <map>
#include <getopt.h>
#include <vector>
#include <set>
#include <string>
#include <stdlib.h>
#include <climits>
#include <getopt.h>
using namespace std;
#include "pin.H"
#include "instlib.H"
bool verbose = true;
ADDRINT textStart,
textEnd;
BBL prevBbl;
INS prevInsn;
ADDRINT prevAddr;
set <string> skipFunctions;
void initSkipFunctions(){
skipFunctions.insert(".plt");
skipFunctions.insert("__libc_start_main@plt");
skipFunctions.insert(".init");
skipFunctions.insert("init");
skipFunctions.insert("_init");
skipFunctions.insert("start");
skipFunctions.insert("_start");
skipFunctions.insert("fini");
skipFunctions.insert("_fini");
skipFunctions.insert("register_tm_clones");
skipFunctions.insert("deregister_tm_clones");
skipFunctions.insert("frame_dummy");
skipFunctions.insert("__do_global_ctors_aux");
skipFunctions.insert("__do_global_dtors_aux");
skipFunctions.insert("__libc_csu_init");
skipFunctions.insert("__libc_csu_fini");
skipFunctions.insert("__libc_csu_fini");
skipFunctions.insert("__libc_start_main");
skipFunctions.insert("__gmon_start__");
skipFunctions.insert("__cxa_atexit");
skipFunctions.insert("__cxa_finalize");
skipFunctions.insert("__assert_fail");
skipFunctions.insert("free");
skipFunctions.insert("fnmatch");
skipFunctions.insert("readlinkat");
skipFunctions.insert("malloc");
skipFunctions.insert("calloc");
skipFunctions.insert("realloc");
skipFunctions.insert("__x86.get_pc_thunk.bx");
}
void Trace(TRACE trace, void *v){
BBL bbl = TRACE_BblHead(trace);
for (; BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
ADDRINT addr = BBL_Address(bbl);
if (addr < textStart || addr > textEnd)
continue;
//cout << hex << BBL_Address(prevBbl) << " " << BBL_Address(bbl) << endl;
INS insn = BBL_InsTail(bbl);
/*
if ((addr <= prevAddr) && INS_IsDirectBranch(insn)){
cout << INS_IsDirectBranch(insn) << " ";
cout << INS_IsRet(insn) << " ";
cout << prevAddr << "," << addr << " ";
cout << INS_Disassemble(prevInsn) << endl;
}
*/
cout << addr << " " << INS_Disassemble(insn) << endl;
//prevInsn = insn;
//prevAddr = INS_Address(insn);
//INS ins = BBL_InsTail(bbl);
//cout << "[LoopCov] insn: " << hex << BBL_Address(bbl) << " " << INS_Disassemble(ins) << endl;
}
}
/* Retrieve .TEXT section boundaries. */
void getTextBounds(void *ptr){
IMG img = APP_ImgHead();
for(SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec)){
if (SEC_IsExecutable(sec) && SEC_Name(sec) == ".text"){
ADDRINT start = SEC_Address(sec);
ADDRINT size = SEC_Size(sec);
if (start != 0){
textStart = start;
textEnd = start + size;
}
if (getenv("LOOPCOV_DEBUG")){
cout << "[LoopCov] section: " << SEC_Name(sec) << endl;
cout << "[LoopCov] start: " << hex << textStart << endl;
cout << "[LoopCov] end: " << hex << textEnd << endl;
}
return;
}
}
}
int main(int argc, char **argv)
{
// Reset output log, if present.
ofstream outfile;
outfile.open("/tmp/pinLoopCov.log", ios::trunc);
outfile.close();
// Initialize pin.
if (PIN_Init(argc, argv) == 1)
{
cout << "Error PIN_Init!" << endl;
return 1;
}
initSkipFunctions();
PIN_InitSymbols();
PIN_AddApplicationStartFunction(getTextBounds,0);
TRACE_AddInstrumentFunction(Trace, 0);
/* Start the program, never returns. */
PIN_StartProgram();
return 0;
}