Skip to content

Commit 604d098

Browse files
committed
Added instruction start/stop for logging
It is now possible to specify an offset that, after it is called, will begin the logging process. Alternatively, you can specify an offset that after it is hit, will stop logging.
1 parent c2b9f5d commit 604d098

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

IDAPinLogger.cpp

+74-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -- nc.exe -l -v -p 999
2222
2323
Log all hits in supporting module (note case sensitive!):
2424
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+
2530
*/
2631
#include "pin.H"
2732
#include <iostream>
33+
#include <sstream>
2834
#include <fstream>
35+
#include <iomanip>
2936

3037
namespace WINDOWS {
3138
#include "Windows.h"
@@ -42,14 +49,22 @@ unsigned int moduleSize = 0;
4249
// Hit Count Variables.
4350
FILE *IDAInsLogFile;
4451
WINDOWS::BYTE *logBuffer;
45-
52+
//Log start/stop variables
53+
ADDRINT gLogStart = -1;
54+
ADDRINT gLogStop = -1;
55+
bool gLogging = true;
4656
/* ===================================================================== */
4757
// Command line switches
4858
/* ===================================================================== */
4959
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
5060
"o", "", "specify file name for IDAPinLogger output");
5161
KNOB<string> KnobModuleToLog(KNOB_MODE_WRITEONCE, "pintool",
5262
"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+
5368

5469
INT32 Usage()
5570
{
@@ -62,9 +77,25 @@ INT32 Usage()
6277
/* ===================================================================== */
6378
// Analysis routines
6479
/* ===================================================================== */
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+
6592
VOID IncrementCount(UINT32 eip)
6693
{
6794
unsigned int idx = eip - moduleStart;
95+
if (gLogging == false)
96+
{
97+
return;
98+
}
6899
// if we get called more than 255 times, well, stop.
69100
if (logBuffer[idx] >= 255)
70101
{
@@ -93,6 +124,16 @@ VOID ImageLoad(IMG img, VOID *v)
93124
moduleStart = IMG_LowAddress(img);
94125
moduleEnd = IMG_HighAddress(img);
95126
moduleSize = moduleEnd - moduleStart;
127+
if (gLogStart != -1)
128+
{
129+
gLogStart += moduleStart;
130+
}
131+
132+
if (gLogStop != -1)
133+
{
134+
gLogStop += moduleStart;
135+
}
136+
96137
std::cerr << "Module size is: " << moduleSize << endl;
97138
logBuffer = (WINDOWS::BYTE *)calloc(moduleSize,sizeof(WINDOWS::BYTE));
98139
if (logBuffer == NULL)
@@ -107,6 +148,24 @@ VOID ImageLoad(IMG img, VOID *v)
107148
VOID Instruction(INS ins, VOID *v)
108149
{
109150
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+
110169
if (loc >= moduleStart && loc <= moduleEnd)
111170
{
112171
INS_InsertCall(ins,
@@ -122,6 +181,7 @@ VOID Fini(INT32 code, VOID *v)
122181
{
123182
std::cerr << "Writing instruction hits to disk." << endl;
124183
fwrite(logBuffer, sizeof(WINDOWS::BYTE), moduleSize, IDAInsLogFile);
184+
125185
fflush(IDAInsLogFile);
126186
fclose(IDAInsLogFile);
127187
free(logBuffer);
@@ -144,6 +204,19 @@ int main(int argc, char *argv[])
144204
std::cerr << "Need an outfile file dingus." << endl;
145205
return -1;
146206
}
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+
}
147220

148221
IMG_AddInstrumentFunction(ImageLoad, 0);
149222
INS_AddInstrumentFunction(Instruction, 0);

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ then needs to be imported into IDA Pro using dereko's (?) loadlog.py IDAPython s
1212
I feel this method is a bit more simpiler then his (and I was having problems with
1313
certain executables). But who knows maybe i'm doing something wrong :).
1414

15+
Update: It is now possible to specify when logging should begin/end by giving an offset
16+
to an instruction where, after it is called logging should start. Also by specifying
17+
an end instruction, logging will stop. This makes it much easier to identify code paths
18+
that are taken after a certain point with in the applications life cycle.
19+
1520

1621
How to build:
1722
-------------------------
@@ -33,4 +38,7 @@ c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -- nc.exe -l -v -p 999
3338

3439
Log all hits in supporting module (note case sensitive!):
3540

36-
c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -m KERNEL32.DLL -- nc.exe -l -v -p 999
41+
c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -m KERNEL32.DLL -- nc.exe -l -v -p 999
42+
43+
Only start logging hits after instruction at base+0x991c is hit, stop logging after base+0x4242 is hit:
44+
c:\pin\pin.exe -t C:\pin\IDAPinLogger.dll -s 0x991c -e 0x4242 -- nc.exe -l -v -p 999

0 commit comments

Comments
 (0)