Skip to content

Commit 84ed05f

Browse files
committed
Adding our pin tool
1 parent bfb50fe commit 84ed05f

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
*.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
2+
/*! @file
3+
* This is an example of the PIN tool that demonstrates some basic PIN APIs
4+
* and could serve as the starting point for developing your first PIN tool
5+
*/
6+
7+
#include "pin.H"
8+
#include <iostream>
9+
#include <fstream>
10+
11+
/* ================================================================== */
12+
// Global variables
13+
/* ================================================================== */
14+
15+
UINT64 insCount = 0; //number of dynamically executed instructions
16+
UINT64 bblCount = 0; //number of dynamically executed basic blocks
17+
UINT64 threadCount = 0; //total number of threads, including main thread
18+
19+
std::ostream * out = &cerr;
20+
21+
/* ===================================================================== */
22+
// Command line switches
23+
/* ===================================================================== */
24+
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
25+
"o", "", "specify file name for MyPinTool output");
26+
27+
KNOB<BOOL> KnobCount(KNOB_MODE_WRITEONCE, "pintool",
28+
"count", "1", "count instructions, basic blocks and threads in the application");
29+
30+
31+
/* ===================================================================== */
32+
// Utilities
33+
/* ===================================================================== */
34+
35+
/*!
36+
* Print out help message.
37+
*/
38+
INT32 Usage()
39+
{
40+
cerr << "This tool prints out the number of dynamically executed " << endl <<
41+
"instructions, basic blocks and threads in the application." << endl << endl;
42+
43+
cerr << KNOB_BASE::StringKnobSummary() << endl;
44+
45+
return -1;
46+
}
47+
48+
/* ===================================================================== */
49+
// Analysis routines
50+
/* ===================================================================== */
51+
52+
/*!
53+
* Increase counter of the executed basic blocks and instructions.
54+
* This function is called for every basic block when it is about to be executed.
55+
* @param[in] numInstInBbl number of instructions in the basic block
56+
* @note use atomic operations for multi-threaded applications
57+
*/
58+
VOID CountBbl(UINT32 numInstInBbl)
59+
{
60+
bblCount++;
61+
insCount += numInstInBbl;
62+
}
63+
64+
/* ===================================================================== */
65+
// Instrumentation callbacks
66+
/* ===================================================================== */
67+
68+
/*!
69+
* Insert call to the CountBbl() analysis routine before every basic block
70+
* of the trace.
71+
* This function is called every time a new trace is encountered.
72+
* @param[in] trace trace to be instrumented
73+
* @param[in] v value specified by the tool in the TRACE_AddInstrumentFunction
74+
* function call
75+
*/
76+
VOID Trace(TRACE trace, VOID *v)
77+
{
78+
// Visit every basic block in the trace
79+
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
80+
{
81+
// Insert a call to CountBbl() before every basic bloc, passing the number of instructions
82+
BBL_InsertCall(bbl, IPOINT_BEFORE, (AFUNPTR)CountBbl, IARG_UINT32, BBL_NumIns(bbl), IARG_END);
83+
}
84+
}
85+
86+
/*!
87+
* Increase counter of threads in the application.
88+
* This function is called for every thread created by the application when it is
89+
* about to start running (including the root thread).
90+
* @param[in] threadIndex ID assigned by PIN to the new thread
91+
* @param[in] ctxt initial register state for the new thread
92+
* @param[in] flags thread creation flags (OS specific)
93+
* @param[in] v value specified by the tool in the
94+
* PIN_AddThreadStartFunction function call
95+
*/
96+
VOID ThreadStart(THREADID threadIndex, CONTEXT *ctxt, INT32 flags, VOID *v)
97+
{
98+
threadCount++;
99+
}
100+
101+
/*!
102+
* Print out analysis results.
103+
* This function is called when the application exits.
104+
* @param[in] code exit code of the application
105+
* @param[in] v value specified by the tool in the
106+
* PIN_AddFiniFunction function call
107+
*/
108+
VOID Fini(INT32 code, VOID *v)
109+
{
110+
*out << "===============================================" << endl;
111+
*out << "MyPinTool analysis results: " << endl;
112+
*out << "Number of instructions: " << insCount << endl;
113+
*out << "Number of basic blocks: " << bblCount << endl;
114+
*out << "Number of threads: " << threadCount << endl;
115+
*out << "===============================================" << endl;
116+
}
117+
118+
/*!
119+
* The main procedure of the tool.
120+
* This function is called when the application image is loaded but not yet started.
121+
* @param[in] argc total number of elements in the argv array
122+
* @param[in] argv array of command line arguments,
123+
* including pin -t <toolname> -- ...
124+
*/
125+
int main(int argc, char *argv[])
126+
{
127+
// Initialize PIN library. Print help message if -h(elp) is specified
128+
// in the command line or the command line is invalid
129+
if( PIN_Init(argc,argv) )
130+
{
131+
return Usage();
132+
}
133+
134+
string fileName = KnobOutputFile.Value();
135+
136+
if (!fileName.empty()) { out = new std::ofstream(fileName.c_str());}
137+
138+
if (KnobCount)
139+
{
140+
// Register function to be called to instrument traces
141+
TRACE_AddInstrumentFunction(Trace, 0);
142+
143+
// Register function to be called for every thread before it starts running
144+
PIN_AddThreadStartFunction(ThreadStart, 0);
145+
146+
// Register function to be called when the application exits
147+
PIN_AddFiniFunction(Fini, 0);
148+
}
149+
150+
cerr << "===============================================" << endl;
151+
cerr << "This application is instrumented by MyPinTool" << endl;
152+
if (!KnobOutputFile.Value().empty())
153+
{
154+
cerr << "See file " << KnobOutputFile.Value() << " for analysis results" << endl;
155+
}
156+
cerr << "===============================================" << endl;
157+
158+
// Start the program, never returns
159+
PIN_StartProgram();
160+
161+
return 0;
162+
}
163+
164+
/* ===================================================================== */
165+
/* eof */
166+
/* ===================================================================== */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##############################################################
2+
#
3+
# DO NOT EDIT THIS FILE!
4+
#
5+
##############################################################
6+
7+
# If the tool is built out of the kit, PIN_ROOT must be specified in the make invocation and point to the kit root.
8+
ifdef PIN_ROOT
9+
CONFIG_ROOT := $(PIN_ROOT)/source/tools/Config
10+
else
11+
CONFIG_ROOT := ../Config
12+
endif
13+
include $(CONFIG_ROOT)/makefile.config
14+
include makefile.rules
15+
include $(TOOLS_ROOT)/Config/makefile.default.rules
16+
17+
##############################################################
18+
#
19+
# DO NOT EDIT THIS FILE!
20+
#
21+
##############################################################
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
##############################################################
2+
#
3+
# This file includes all the test targets as well as all the
4+
# non-default build rules and test recipes.
5+
#
6+
##############################################################
7+
8+
9+
##############################################################
10+
#
11+
# Test targets
12+
#
13+
##############################################################
14+
15+
###### Place all generic definitions here ######
16+
17+
# This defines tests which run tools of the same name. This is simply for convenience to avoid
18+
# defining the test name twice (once in TOOL_ROOTS and again in TEST_ROOTS).
19+
# Tests defined here should not be defined in TOOL_ROOTS and TEST_ROOTS.
20+
TEST_TOOL_ROOTS := MyMemTracer
21+
22+
# This defines the tests to be run that were not already defined in TEST_TOOL_ROOTS.
23+
TEST_ROOTS :=
24+
25+
# This defines the tools which will be run during the the tests, and were not already defined in
26+
# TEST_TOOL_ROOTS.
27+
TOOL_ROOTS :=
28+
29+
# This defines the static analysis tools which will be run during the the tests. They should not
30+
# be defined in TEST_TOOL_ROOTS. If a test with the same name exists, it should be defined in
31+
# TEST_ROOTS.
32+
# Note: Static analysis tools are in fact executables linked with the Pin Static Analysis Library.
33+
# This library provides a subset of the Pin APIs which allows the tool to perform static analysis
34+
# of an application or dll. Pin itself is not used when this tool runs.
35+
SA_TOOL_ROOTS :=
36+
37+
# This defines all the applications that will be run during the tests.
38+
APP_ROOTS :=
39+
40+
# This defines any additional object files that need to be compiled.
41+
OBJECT_ROOTS :=
42+
43+
# This defines any additional dlls (shared objects), other than the pintools, that need to be compiled.
44+
DLL_ROOTS :=
45+
46+
# This defines any static libraries (archives), that need to be built.
47+
LIB_ROOTS :=
48+
49+
###### Define the sanity subset ######
50+
51+
# This defines the list of tests that should run in sanity. It should include all the tests listed in
52+
# TEST_TOOL_ROOTS and TEST_ROOTS excluding only unstable tests.
53+
SANITY_SUBSET := $(TEST_TOOL_ROOTS) $(TEST_ROOTS)
54+
55+
56+
##############################################################
57+
#
58+
# Test recipes
59+
#
60+
##############################################################
61+
62+
# This section contains recipes for tests other than the default.
63+
# See makefile.default.rules for the default test rules.
64+
# All tests in this section should adhere to the naming convention: <testname>.test
65+
66+
67+
##############################################################
68+
#
69+
# Build rules
70+
#
71+
##############################################################
72+
73+
# This section contains the build rules for all binaries that have special build rules.
74+
# See makefile.default.rules for the default build rules.

0 commit comments

Comments
 (0)