Debian Codegraph @

test/pptrace/comparison/instrument/pin/my_pin.cpp

raw
1 /*BEGIN_LEGAL
2 Intel Open Source License
3
4 Copyright (c) 2002-2012 Intel Corporation. All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer. Redistributions
12 in binary form must reproduce the above copyright notice, this list of
13 conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution. Neither the name of
15 the Intel Corporation nor the names of its contributors may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
23 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 END_LEGAL */
31
32 /*
33 * This tool counts the number of times a routine is executed and
34 * the number of instructions executed in a routine
35 */
36
37 #include <fstream>
38 #include <iomanip>
39 #include <iostream>
40 #include <string.h>
41 #include "pin.H"
42 #include "instrument.h"
43
44 // record one event before function foo is called
45
46 /* ===================================================================== */
47 /* Print Help Message */
48 /* ===================================================================== */
49
50 INT32 Usage() {
51 cerr << "This Pintool counts the number of times a routine is executed"
52 << endl;
53 cerr << "and the number of instructions executed in a routine" << endl;
54 cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
55 return -1;
56 }
57
58 int instrument_function(IMG img, const char* function_name, AFUNPTR callback) {
59 __init();
60
61 RTN rtn = RTN_FindByName(img, function_name);
62 if (rtn == RTN_Invalid()) {
63 //printf("RTN_FindByName failed\n");
64 return -1;
65 }
66
67 RTN_Open(rtn);
68 RTN_InsertCall(rtn, IPOINT_BEFORE, callback, IARG_UINT32, 1, IARG_END);
69 RTN_InsertCall(rtn, IPOINT_AFTER, callback, IARG_UINT32, 2, IARG_END);
70 RTN_Close(rtn);
71 printf("callback inserted for function %s\n", function_name);
72 return 0;
73 }
74
75 void my_function(int n) {
76 //printf("my_function was called with param %d\n", n);
77 record_event0(1);
78 }
79
80 void my_fini(void* arg) {
81 __conclude();
82 }
83
84 VOID ImageLoad(IMG img, VOID *v) {
85 printf("Loading %s, Image id = %d\n", IMG_Name(img).c_str(), IMG_Id(img));
86 //instrument_function(img, "foo", AFUNPTR(record_event0));
87 instrument_function(img, "foo", AFUNPTR(my_function));
88 }
89
90 /* ===================================================================== */
91 /* Main */
92 /* ===================================================================== */
93
94 int main(int argc, char * argv[]) {
95 // Initialize symbol table code, needed for rtn instrumentation
96 PIN_InitSymbols();
97
98 // Initialize pin
99 if (PIN_Init(argc, argv))
100 return Usage();
101
102 IMG img = IMG_Open(argv[argc - 1]);
103 if (img == IMG_Invalid()) {
104 printf("Can't open file %s\n", argv[argc - 1]);
105 return 1;
106 }
107
108 PIN_AddFiniFunction(FINI_CALLBACK(my_fini), NULL);
109
110 IMG_AddInstrumentFunction(ImageLoad, 0);
111
112 // Start the program, never returns
113 PIN_StartProgram();
114 return 0;
115 }