-
Notifications
You must be signed in to change notification settings - Fork 0
/
brtrace.cpp
74 lines (60 loc) · 2.03 KB
/
brtrace.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
#include <stdio.h>
#include "pin.H"
FILE * trace;
#define Print2File
// This function is called before every instruction is executed
// and prints the IP
VOID printip(VOID *ip, VOID *target, BOOL taken)
{
#ifdef Print2File
fprintf(trace, "%p %p %s\n", ip, target, (taken? "Y": "N"));
#else
printf("%p %p %s\n", ip, target, (taken? "Y": "N"));
#endif
}
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
// Insert a call to printip before every instruction, and pass it the IP
if (INS_IsBranch(ins))
{
// IARG_INST_PTR, IARG_BRANCH_TARGET_ADDR, and IARG_BRANCH_TAKEN
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)printip,
IARG_INST_PTR, IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);
}
}
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
#ifdef Print2File
fprintf(trace, "#eof\n");
fclose(trace);
#endif
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR("This Pintool works on branch prediction\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char * argv[])
{
#ifdef Print2File
trace = fopen("brtrace.out", "w");
#endif
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(Instruction, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}