@@ -22,10 +22,17 @@ c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -- nc.exe -l -v -p 999
22
22
23
23
Log all hits in supporting module (note case sensitive!):
24
24
c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -m KERNEL32.DLL -- nc.exe -l -v -p 999
25
+
26
+ Only start logging hits after instruction at base+0x991c is hit, stop logging after base+0x4242 is hit:
27
+ c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -s 0x991c -e 0x4242 -- nc.exe -l -v -p 999
28
+
29
+
25
30
*/
26
31
#include " pin.H"
27
32
#include < iostream>
33
+ #include < sstream>
28
34
#include < fstream>
35
+ #include < iomanip>
29
36
30
37
namespace WINDOWS {
31
38
#include " Windows.h"
@@ -42,14 +49,22 @@ unsigned int moduleSize = 0;
42
49
// Hit Count Variables.
43
50
FILE *IDAInsLogFile;
44
51
WINDOWS::BYTE *logBuffer;
45
-
52
+ // Log start/stop variables
53
+ ADDRINT gLogStart = -1 ;
54
+ ADDRINT gLogStop = -1 ;
55
+ bool gLogging = true ;
46
56
/* ===================================================================== */
47
57
// Command line switches
48
58
/* ===================================================================== */
49
59
KNOB<string> KnobOutputFile (KNOB_MODE_WRITEONCE, " pintool" ,
50
60
" o" , " " , " specify file name for IDAPinLogger output" );
51
61
KNOB<string> KnobModuleToLog (KNOB_MODE_WRITEONCE, " pintool" ,
52
62
" m" , " " , " specify the module to record instruction visits." );
63
+ KNOB<string> KnobLogStart (KNOB_MODE_WRITEONCE, " pintool" ,
64
+ " s" , " " , " specify instruction offset (will be auto added to base) that will start logging." );
65
+ KNOB<string> KnobLogStop (KNOB_MODE_WRITEONCE, " pintool" ,
66
+ " e" , " " , " specify instruction offset (will be auto added to base) that will stop logging." );
67
+
53
68
54
69
INT32 Usage ()
55
70
{
@@ -62,9 +77,25 @@ INT32 Usage()
62
77
/* ===================================================================== */
63
78
// Analysis routines
64
79
/* ===================================================================== */
80
+ VOID StartLogging (UINT32 eip)
81
+ {
82
+ gLogging = true ;
83
+ std::cerr << " Logging Started due to log start instruction hit." << endl;
84
+ }
85
+
86
+ VOID StopLogging (UINT32 eip)
87
+ {
88
+ gLogging = false ;
89
+ std::cerr << " Logging Stopped due to log stop instruction hit." << endl;
90
+ }
91
+
65
92
VOID IncrementCount (UINT32 eip)
66
93
{
67
94
unsigned int idx = eip - moduleStart;
95
+ if (gLogging == false )
96
+ {
97
+ return ;
98
+ }
68
99
// if we get called more than 255 times, well, stop.
69
100
if (logBuffer[idx] >= 255 )
70
101
{
@@ -93,6 +124,16 @@ VOID ImageLoad(IMG img, VOID *v)
93
124
moduleStart = IMG_LowAddress (img);
94
125
moduleEnd = IMG_HighAddress (img);
95
126
moduleSize = moduleEnd - moduleStart;
127
+ if (gLogStart != -1 )
128
+ {
129
+ gLogStart += moduleStart;
130
+ }
131
+
132
+ if (gLogStop != -1 )
133
+ {
134
+ gLogStop += moduleStart;
135
+ }
136
+
96
137
std::cerr << " Module size is: " << moduleSize << endl;
97
138
logBuffer = (WINDOWS::BYTE *)calloc (moduleSize,sizeof (WINDOWS::BYTE));
98
139
if (logBuffer == NULL )
@@ -107,6 +148,24 @@ VOID ImageLoad(IMG img, VOID *v)
107
148
VOID Instruction (INS ins, VOID *v)
108
149
{
109
150
ADDRINT loc = INS_Address (ins);
151
+ if (gLogStart != -1 && loc == gLogStart )
152
+ {
153
+ INS_InsertCall (ins,
154
+ IPOINT_BEFORE,
155
+ (AFUNPTR)StartLogging,
156
+ IARG_INST_PTR,
157
+ IARG_END);
158
+ }
159
+
160
+ if (gLogStop != -1 && loc == gLogStop )
161
+ {
162
+ INS_InsertCall (ins,
163
+ IPOINT_BEFORE,
164
+ (AFUNPTR)StopLogging,
165
+ IARG_INST_PTR,
166
+ IARG_END);
167
+ }
168
+
110
169
if (loc >= moduleStart && loc <= moduleEnd)
111
170
{
112
171
INS_InsertCall (ins,
@@ -122,6 +181,7 @@ VOID Fini(INT32 code, VOID *v)
122
181
{
123
182
std::cerr << " Writing instruction hits to disk." << endl;
124
183
fwrite (logBuffer, sizeof (WINDOWS::BYTE), moduleSize, IDAInsLogFile);
184
+
125
185
fflush (IDAInsLogFile);
126
186
fclose (IDAInsLogFile);
127
187
free (logBuffer);
@@ -144,6 +204,19 @@ int main(int argc, char *argv[])
144
204
std::cerr << " Need an outfile file dingus." << endl;
145
205
return -1 ;
146
206
}
207
+
208
+ if (!KnobLogStart.Value ().empty ())
209
+ {
210
+ stringstream converter (KnobLogStart.Value ());
211
+ converter >> hex >> gLogStart ;
212
+ gLogging = false ;
213
+ }
214
+
215
+ if (!KnobLogStop.Value ().empty ())
216
+ {
217
+ stringstream converter (KnobLogStop.Value ());
218
+ converter >> hex >> gLogStop ;
219
+ }
147
220
148
221
IMG_AddInstrumentFunction (ImageLoad, 0 );
149
222
INS_AddInstrumentFunction (Instruction, 0 );
0 commit comments