-
Notifications
You must be signed in to change notification settings - Fork 0
/
inscount0.cpp
120 lines (98 loc) · 3.82 KB
/
inscount0.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
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2002-2019 Intel Corporation.
*
* This software is provided to you as Sample Source Code as defined in the accompanying
* End User License Agreement for the Intel(R) Software Development Products ("Agreement")
* section 1.L.
*
* This software and the related documents are provided as is, with no express or implied
* warranties, other than those that are expressly stated in the License.
*/
#include <iostream>
#include <fstream>
#include "pin.H"
using std::cerr;
using std::ofstream;
using std::ios;
using std::string;
using std::endl;
using std::cout;
ofstream OutFile;
// The running count of instructions is kept here
// make it static to help the compiler optimize docount
static UINT64 icount = 0;
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"a", "0", "specify output file name");
static long long int target_addr= 0;
// This function is called before every instruction is executed
VOID docount(void *ip) {
icount++;
}
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
// Insert a call to docount before every instruction, no arguments are passed
long long int addr = (long long int)INS_Address(ins);
if((addr >> 44 & 0xf)== 0x7 || (addr >> 28 & 0xf) == 0xf)
return;
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}
VOID Instruction_TARGET(INS ins, VOID *v)
{
// Insert a call to docount before every instruction, no arguments are passed
long long int addr = (long long int)INS_Address(ins);
if((addr >> 44 & 0xf)== 0x7 || (addr >> 28 & 0xf) == 0xf)
return;
if(addr == target_addr)
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}
VOID Instruction_TARGET_PIE(INS ins, VOID *v)
{
// Insert a call to docount before every instruction, no arguments are passed
long long int addr = (long long int)INS_Address(ins);
if((addr >> 44 & 0xf)== 0x7 || (addr >> 28 & 0xf) == 0xf)
return;
if( (addr&0xFFF) == target_addr)
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}
// 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
// OutFile.setf(ios::showbase);
// OutFile << KnobOutputFile.Value().c_str() << ":" << icount << endl;
// OutFile.close();
cout << "Count" << ":" << icount << endl;
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
cerr << "This tool counts the number of dynamic instructions executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
/* argc, argv are the entire command line: pin -t <toolname> -- ... */
/* ===================================================================== */
int main(int argc, char * argv[])
{
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
//OutFile.open("inscount0.out",ios::app);
target_addr = atoll(KnobOutputFile.Value().c_str());
if(target_addr == 0)
INS_AddInstrumentFunction(Instruction, 0);
else if(target_addr <= 0xFFF)
INS_AddInstrumentFunction(Instruction_TARGET_PIE, 0);
else
INS_AddInstrumentFunction(Instruction_TARGET, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}