Debian Codegraph @

test/pptrace/comparison/instrument/dyninst/my_dyninst.cpp

raw
1 /* -*- c-file-style: "GNU" -*- */
2 /*
3 * Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
4 * See COPYING in top-level directory.
5 */
6
7 #include <stdio.h>
8 #include <stdint.h>
9 #include "BPatch.h"
10 #include "BPatch_function.h"
11 #include "BPatch_basicBlock.h"
12
13 #include <stdio.h>
14 #include <stdint.h>
15 #include "BPatch.h"
16 #include "BPatch_callbacks.h"
17 #include "BPatch_function.h"
18
19 #define CREATE_BINARY 1
20 //#define LIB_EZTRACE_SO "/home/trahay/Documents/recherche/articles/PROPER_2012/programmes/instrument/libinstrument/libinstrument.so"
21 //#define LIB_EZTRACE_SO "/home/trahay/Documents/recherche/articles/PROPER_2012/programmes/instrument/libinstrument/libinstrument.so"
22
23 // describe a function to instrument
24 struct eztrace_dyninst_func {
25 char* fname;
26 uint32_t code_entry;
27 uint32_t code_exit;
28 struct eztrace_dyninst_func *next;
29 };
30 typedef struct eztrace_dyninst_func *eztrace_dyninst_func_t;
31
32 // list of registered functions
33 eztrace_dyninst_func_t func_list = NULL;
34
35 static int __intercept_function(BPatch_addressSpace *app,
36 const char* function_name, uint32_t code_entry,
37 uint32_t code_exit);
38
39 static int nb_func = 0;
40
41 // register a function
42 extern "C" void eztrace_dyninst_register(const char* fname, uint32_t code_entry,
43 uint32_t code_exit) {
44 struct eztrace_dyninst_func *new_func =
45 (struct eztrace_dyninst_func *) malloc(sizeof(struct eztrace_dyninst_func));
46
47 asprintf(&new_func->fname, "%s", fname);
48 new_func->code_entry = code_entry;
49 new_func->code_exit = code_exit;
50
51 if (func_list)
52 new_func->next = func_list;
53 else
54 new_func->next = NULL;
55 func_list = new_func;
56 }
57
58 // instrument all the registered functions
59 extern "C" int eztrace_dyninst_instrument(BPatch_addressSpace *app) {
60 int ret = 0;
61 eztrace_dyninst_func_t func = func_list;
62
63 ret = 0;
64 while (func != NULL) {
65 ret += __intercept_function(app, func->fname, func->code_entry,
66 func->code_exit);
67 func = func->next;
68 }
69 return ret;
70 }
71
72 extern "C" int eztrace_dyninst_nb_function_to_register() {
73 eztrace_dyninst_func_t func = func_list;
74 int ret = 0;
75
76 while (func && func->next != func_list) {
77 func = func->next;
78 ret++;
79 }
80 return ret;
81 }
82
83 // Instrument a function: eztrace_code0(code_entry) is called at the
84 // beginning of the function and eztrace_code0(code_entry) is called
85 // at the end of the function.
86 // If code_entry or code_exit is null, the corresponding call to
87 // eztrace_code0 is skipped
88 static int __intercept_function(BPatch_addressSpace *app,
89 const char* function_name, uint32_t code_entry,
90 uint32_t code_exit) {
91 BPatch_image *appImage;
92 BPatch_Vector<BPatch_point*> *points;
93 BPatch_Vector<BPatch_function *> functions;
94
95 BPatch_Vector<BPatch_function *> record_event0_ptr;
96
97 appImage = app->getImage();
98
99 // search for record_event0 function
100 BPatch_Vector<BPatch_module*> *loaded_modules = appImage->getModules();
101 printf("Threre are %d modules\n", loaded_modules->size());
102
103 for (int i = 0; i < loaded_modules->size(); i++) {
104 BPatch_module* cur_mod = (*loaded_modules)[i];
105
106 char mod_name[80];
107 cur_mod->getName(mod_name, 80);
108
109 cur_mod->findFunction("record_event0", record_event0_ptr, false);
110
111 if (!record_event0_ptr.size()) {
112 printf("\tfunction record_event0 not found in module %s\n", mod_name);
113 } else {
114 printf("Found ! \n");
115 break;
116 }
117 }
118
119 if (!record_event0_ptr.size()) {
120 printf("Cannot find record_event0 function\n");
121 return -1;
122 }
123
124 printf("PLOP\n");
125
126 for (int i = 0; i < loaded_modules->size(); i++) {
127 BPatch_module* cur_mod = (*loaded_modules)[i];
128
129 char mod_name[80];
130 cur_mod->getName(mod_name, 80);
131
132 cur_mod->findFunction(function_name, functions, false);
133
134 if (!functions.size()) {
135 printf("\tfunction %s not found in module %s\n", function_name, mod_name);
136 } else {
137 printf("Found %s! \n", function_name);
138 break;
139 }
140 }
141
142 if (!functions.size()) {
143 fprintf(stderr, "warning: cannot find function %s in executable\n",
144 function_name);
145 return 0;
146 }
147
148 // Instrument the entry of the function
149 if (code_entry) {
150 // We need to call eztrace_generic(code, nb_param, param1, param2, ...)
151 points = functions[0]->findPoint(BPatch_entry);
152 BPatch_Vector<BPatch_snippet*> dummyArgs;
153
154 // Create the parameter (code_entry)
155 BPatch_constExpr code(code_entry);
156 dummyArgs.push_back(&code);
157
158 // Create the function call
159 #if 0
160 BPatch_Vector<BPatch_function *> funcs;
161 appImage->findFunction("record_event0", funcs);
162 BPatch_function *dummyFunc = funcs[0];
163
164 BPatch_funcCallExpr dummyCall(*dummyFunc, dummyArgs);
165 #else
166 BPatch_funcCallExpr dummyCall(*record_event0_ptr[0], dummyArgs);
167 #endif
168
169 //Insert the function call at the point
170 app->insertSnippet(dummyCall, *points);
171 }
172
173 // Instrument the exit of the function
174 if (code_exit) {
175 // the function parameters are not available here, so we have to
176 // call eztrace_code0(code)
177
178 points = functions[0]->findPoint(BPatch_exit);
179 // Create the parameter (code_entry)
180 BPatch_Vector<BPatch_snippet*> dummyArgs;
181 BPatch_constExpr code(code_exit);
182 dummyArgs.push_back(&code);
183
184 // Create the function call
185 #if 0
186 BPatch_Vector<BPatch_function *> funcs;
187 appImage->findFunction("record_event0", funcs);
188 BPatch_function *dummyFunc = funcs[0];
189
190 BPatch_funcCallExpr dummyCall(*dummyFunc, dummyArgs);
191 #else
192 BPatch_funcCallExpr dummyCall(*record_event0_ptr[0], dummyArgs);
193 #endif
194
195 //Insert the function call at the point
196 app->insertSnippet(dummyCall, *points);
197 }
198 return 1;
199 }
200
201 // instrument all the registered functions
202 extern "C" int eztrace_dyninst_instrument(BPatch_addressSpace *app);
203
204 static BPatch bpatch;
205 static BPatch_addressSpace *app = NULL;
206 static BPatch_binaryEdit *appBin = NULL;
207
208 int main(int argc, char**argv) {
209 if (argc != 3) {
210 printf("usage: %s orig_prog new_prog\n", argv[0]);
211 return 1;
212 }
213
214 char* file = argv[1];
215 char* newFile = argv[2];
216 bool ret;
217
218 eztrace_dyninst_register("foo", 1, 2);
219
220 if (!eztrace_dyninst_nb_function_to_register()) {
221 printf("0 functions instrumented\n");
222 return 1;
223 }
224
225 #ifdef CREATE_BINARY
226 //Create the BPatch_addressSpace and BPatch_binaryEdit
227 appBin = bpatch.openBinary(file, true);
228 if (!appBin) {
229 fprintf(stderr, "Cannot open %s\n", file);
230 return -1;
231 }
232 app = static_cast<BPatch_addressSpace *>(appBin);
233
234 if (!app->loadLibrary(LIB_EZTRACE_SO)) {
235 printf("Cannot load %s\n", LIB_EZTRACE_SO);
236 return 1;
237 }
238
239 #else
240 // run the program
241 BPatch_process *appProc = bpatch.processCreate(file, NULL);
242 if(!appProc) {
243 printf("Cannot load program %s\n", file);
244 }
245
246 if(! appProc->loadLibrary(LIB_EZTRACE_SO, true)) {
247 printf("Cannot load %s\n", LIB_EZTRACE_SO);
248 return 1;
249 }
250
251 app = static_cast<BPatch_addressSpace *>(appProc);
252
253 #endif
254
255 // Instrument all the specified functions
256 int nb_inst = eztrace_dyninst_instrument(app);
257 printf("%d functions instrumented\n", nb_inst);
258 if (!nb_inst)
259 return 1;
260
261 #ifdef CREATE_BINARY
262 if (appBin != NULL) {
263 //Write a new instrumented executable
264 appBin->writeFile(newFile);
265 } else {
266 fprintf(stderr, "cannot write %s\n", newFile);
267 return -1;
268 }
269 #else
270
271 appProc->continueExecution();
272 while(!appProc->isTerminated()) {
273 bpatch.waitForStatusChange();
274 }
275 #endif
276 return 0;
277 }