-
Notifications
You must be signed in to change notification settings - Fork 7
/
xref_finder.cpp
74 lines (61 loc) · 1.6 KB
/
xref_finder.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
/* xref_finder.cpp
Log unique function calls
*/
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <string.h>
#include "pin.H"
// trace log
FILE * trace;
// state set
set<string> calls;
// handles logging for each call passed to it
VOID RecordCall(VOID * ip, VOID * addr)
{
char keyedpair[32];
_snprintf(keyedpair, 18, "%p:%p", ip, addr);
if (calls.count(keyedpair) == 0)
{
// Efficiency note: while it would be more efficient to write out
// the contents of the "calls" set when the program executes (in the
// Fini() function), we take a hit to reliability. If the application
// terminates unexpectedly, we might not get our log output.
fprintf(trace,"%s\n", keyedpair);
calls.insert(keyedpair);
}
}
// determines whether or not the instruction is a call
VOID Instruction(INS ins, VOID *v)
{
if (INS_IsCall(ins))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordCall,
IARG_INST_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
}
}
// called before the application exits
VOID Fini(INT32 code, VOID *v)
{
fclose(trace);
}
// print help information
INT32 Usage()
{
PIN_ERROR( "This Pintool prints a trace of calls and destinations\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
// derp
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv)) return Usage();
trace = fopen("xrefs_omg.out", "w");
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;
}