Debian Codegraph @

stapdyn/dynsdt.cxx

raw
1 // dyninst example program
2 // Copyright (C) 2012 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9
10 #include <cstdlib>
11 #include <string>
12 #include <vector>
13
14 extern "C" {
15 #define __STDC_LIMIT_MACROS
16 #define __STDC_CONSTANT_MACROS
17 #define __STDC_FORMAT_MACROS
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <err.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include <gelf.h>
27 #include <libelf.h>
28 }
29
30 #include <dyninst/BPatch.h>
31 #include <dyninst/BPatch_function.h>
32 #include <dyninst/BPatch_object.h>
33 #include <dyninst/BPatch_point.h>
34
35 #include "dynutil.h"
36 #include "../staputil.h"
37
38
39 using namespace std;
40
41 struct sdt_point {
42 string provider;
43 string name;
44 vector<pair<int, string> > args;
45 GElf_Addr pc_offset;
46 GElf_Addr sem_offset;
47 };
48
49
50 static vector<pair<int, string> >
51 split_sdt_args(const string& args)
52 {
53 vector<pair<int, string> > vargs;
54 size_t size_idx = 0;
55 size_t at_idx = args.find('@');
56 while (at_idx != string::npos)
57 {
58 size_t size_idx2 = string::npos;
59 size_t at_idx2 = args.find('@', at_idx + 1);
60 if (at_idx2 != string::npos)
61 {
62 size_idx2 = args.rfind(' ', at_idx2);
63 if (size_idx2 == string::npos || size_idx2 <= size_idx)
64 size_idx2 = at_idx2 = string::npos;
65 else
66 ++size_idx2;
67 }
68
69 string size_str = args.substr(size_idx, at_idx - size_idx);
70 int size = atoi(size_str.c_str());
71 string arg = args.substr(at_idx + 1, size_idx2 - at_idx - 2);
72 vargs.push_back(make_pair(size, arg));
73
74 size_idx = size_idx2;
75 at_idx = at_idx2;
76 }
77 return vargs;
78 }
79
80
81 static vector<sdt_point>
82 find_sdt(const string& file)
83 {
84 int fd = -1;
85 size_t shstrndx;
86 GElf_Addr sdt_base_addr = 0;
87 GElf_Off sdt_base_offset = 0;
88 GElf_Addr sdt_probes_addr = 0;
89 GElf_Off sdt_probes_offset = 0;
90 GElf_Off sdt_probes_virt_offset = 0;
91 Elf* elf = NULL;
92 Elf_Scn* scn = NULL;
93 vector<sdt_point> points;
94
95 fd = open(file.c_str(), O_RDONLY);
96 if (fd < 0)
97 {
98 warn("can't open %s", file.c_str());
99 goto ret;
100 }
101
102 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
103 if (!elf)
104 {
105 warn("can't read elf %s", file.c_str());
106 goto ret_fd;
107 }
108
109 elf_getshdrstrndx (elf, &shstrndx);
110
111 scn = NULL;
112 while ((scn = elf_nextscn(elf, scn)))
113 {
114 GElf_Shdr shdr;
115 if (gelf_getshdr (scn, &shdr) == NULL)
116 continue;
117 if (shdr.sh_type != SHT_PROGBITS)
118 continue;
119 if (!(shdr.sh_flags & SHF_ALLOC))
120 continue;
121
122 const char* sh_name = elf_strptr(elf, shstrndx, shdr.sh_name);
123 if (sh_name && !strcmp(".stapsdt.base", sh_name))
124 {
125 sdt_base_addr = shdr.sh_addr;
126 sdt_base_offset = shdr.sh_offset;
127 warnx("SDT base addr:%#" PRIx64 " offset:%#" PRIx64 "", sdt_base_addr, sdt_base_offset);
128 }
129 if (sh_name && !strcmp(".probes", sh_name))
130 {
131 sdt_probes_addr = shdr.sh_addr;
132 sdt_probes_offset = shdr.sh_offset;
133 warnx("SDT probes addr:%#" PRIx64 " offset:%#" PRIx64 "", sdt_probes_addr, sdt_probes_offset);
134 }
135 }
136
137 // Data is usually loaded in a separate segment from text, with padding
138 // between, so this computes the extra padding to subtract from semaphores.
139 if (sdt_probes_offset)
140 sdt_probes_virt_offset = (sdt_probes_addr - sdt_probes_offset) -
141 (sdt_base_addr - sdt_base_offset);
142
143 scn = NULL;
144 while ((scn = elf_nextscn(elf, scn)))
145 {
146 GElf_Shdr shdr;
147 if (gelf_getshdr (scn, &shdr) == NULL)
148 continue;
149 if (shdr.sh_type != SHT_NOTE)
150 continue;
151 if (shdr.sh_flags & SHF_ALLOC)
152 continue;
153
154 Elf_Data *data = elf_getdata (scn, NULL);
155 size_t next;
156 GElf_Nhdr nhdr;
157 size_t name_off;
158 size_t desc_off;
159 for (size_t offset = 0;
160 (next = gelf_getnote (data, offset, &nhdr, &name_off, &desc_off)) > 0;
161 offset = next)
162 {
163 char* cdata = ((char*)data->d_buf);
164 if (strcmp(cdata + name_off, "stapsdt") || nhdr.n_type != 3)
165 continue;
166
167 union {
168 Elf64_Addr a64[3];
169 Elf32_Addr a32[3];
170 } buf;
171 Elf_Data dst =
172 {
173 &buf, ELF_T_ADDR, EV_CURRENT,
174 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
175 };
176 assert (dst.d_size <= sizeof buf);
177
178 if (nhdr.n_descsz < dst.d_size + 3)
179 continue;
180
181 Elf_Data src =
182 {
183 cdata + desc_off, ELF_T_ADDR, EV_CURRENT,
184 dst.d_size, 0, 0
185 };
186
187 if (gelf_xlatetom (elf, &dst, &src,
188 elf_getident (elf, NULL)[EI_DATA]) == NULL)
189 warnx ("gelf_xlatetom: %s", elf_errmsg (-1));
190
191 sdt_point p;
192
193 const char* provider = cdata + desc_off + dst.d_size;
194 p.provider = provider;
195
196 const char* name = provider + p.provider.size() + 1;
197 p.name = name;
198
199 const char* args = name + p.name.size() + 1;
200 if (args < cdata + desc_off + nhdr.n_descsz)
201 p.args = split_sdt_args(args);
202
203
204 GElf_Addr base_ref;
205 if (gelf_getclass (elf) == ELFCLASS32)
206 {
207 p.pc_offset = buf.a32[0];
208 base_ref = buf.a32[1];
209 p.sem_offset = buf.a32[2];
210 }
211 else
212 {
213 p.pc_offset = buf.a64[0];
214 base_ref = buf.a64[1];
215 p.sem_offset = buf.a64[2];
216 }
217
218 // Convert to its relative position in the file
219 p.pc_offset += sdt_base_offset - base_ref;
220 if (p.sem_offset)
221 p.sem_offset += sdt_base_offset - base_ref - sdt_probes_virt_offset;
222
223 stringstream joined_args;
224 for (size_t i = 0; i < p.args.size(); ++i)
225 {
226 if (i > 0)
227 joined_args << ", ";
228 joined_args << p.args[i].first << "@\"" << p.args[i].second << "\"";
229 }
230 warnx("SDT offset:%#" PRIx64 " semaphore:%#" PRIx64 " %s:%s(%s)",
231 p.pc_offset, p.sem_offset,
232 p.provider.c_str(), p.name.c_str(),
233 joined_args.str().c_str());
234
235 points.push_back(p);
236 }
237 }
238
239 elf_end(elf);
240 ret_fd:
241 close(fd);
242 ret:
243 return points;
244 }
245
246
247 static void
248 instrument_sdt(BPatch_process* process,
249 BPatch_object* object,
250 sdt_point& p)
251 {
252 BPatch_image* image = process->getImage();
253
254 Dyninst::Address address = object->fileOffsetToAddr(p.pc_offset);
255 if (address == BPatch_object::E_OUT_OF_BOUNDS)
256 {
257 warnx("couldn't convert %s:%s at %#" PRIx64 " to an address",
258 p.provider.c_str(), p.name.c_str(), p.pc_offset);
259 return;
260 }
261
262 vector<BPatch_point*> points;
263 object->findPoints(address, points);
264 if (points.empty())
265 {
266 warnx("couldn't find %s:%s at %#" PRIx64 " -> %#lx",
267 p.provider.c_str(), p.name.c_str(), p.pc_offset, address);
268 return;
269 }
270
271 ostringstream format;
272 format << "SDT %s:%s";
273 BPatch_Vector<BPatch_register> regs;
274 process->getRegisters(regs);
275 for (size_t i = 0; i < regs.size(); ++i)
276 if (regs[i].name()[2] == 'x')
277 format << " " << regs[i].name() << ":%#lx";
278 format << "\n";
279
280 vector<BPatch_snippet *> printfArgs;
281 printfArgs.push_back(new BPatch_constExpr(format.str().c_str()));
282 printfArgs.push_back(new BPatch_constExpr(p.provider.c_str()));
283 printfArgs.push_back(new BPatch_constExpr(p.name.c_str()));
284 for (size_t i = 0; i < regs.size(); ++i)
285 if (regs[i].name()[2] == 'x')
286 printfArgs.push_back(new BPatch_registerExpr(regs[i]));
287
288 vector<BPatch_function *> printfFuncs;
289 image->findFunction("printf", printfFuncs);
290 BPatch_funcCallExpr printfCall(*(printfFuncs[0]), printfArgs);
291
292 warnx("inserting %s:%s at %#" PRIx64 " -> %#lx [%zu]",
293 p.provider.c_str(), p.name.c_str(), p.pc_offset, address, points.size());
294 process->insertSnippet(printfCall, points);
295
296 if (p.sem_offset)
297 {
298 Dyninst::Address sem_address = object->fileOffsetToAddr(p.sem_offset);
299 if (sem_address == BPatch_object::E_OUT_OF_BOUNDS)
300 warnx("couldn't convert %s:%s semaphore %#" PRIx64 " to an address",
301 p.provider.c_str(), p.name.c_str(), p.sem_offset);
302 else
303 {
304 warnx("incrementing semaphore for %s:%s at %#" PRIx64 " -> %#lx",
305 p.provider.c_str(), p.name.c_str(), p.sem_offset, sem_address);
306
307 BPatch_type *sem_type = image->findType("unsigned short");
308 BPatch_variableExpr *semaphore =
309 process->createVariable(sem_address, sem_type);
310
311 BPatch_arithExpr addOne(BPatch_assign, *semaphore,
312 BPatch_arithExpr(BPatch_plus, *semaphore,
313 BPatch_constExpr(1)));
314 process->oneTimeCode(addOne);
315 }
316 }
317 }
318
319
320 static void
321 instrument_sdt_points(BPatch_process* process,
322 BPatch_object* object,
323 vector<sdt_point>& points)
324 {
325 process->beginInsertionSet();
326 for (size_t i = 0; i < points.size(); ++i)
327 instrument_sdt(process, object, points[i]);
328 process->finalizeInsertionSet(false);
329 }
330
331
332 int
333 main(int argc, const char* argv[])
334 {
335 if (argc < 2)
336 errx(1, "usage: %s PROGRAM [ARGS..]", argv[0]);
337
338 warnx("got args:");
339 for (int i = 1; i < argc; ++i)
340 warnx(" [%i] %s", i - 1, argv[i]);
341
342 if (!check_dyninst_rt())
343 return 1;
344
345 if (!check_dyninst_sebools())
346 return 1;
347
348 BPatch patch;
349
350 warnx("creating the process");
351 string fullpath = find_executable(argv[1]);
352 BPatch_process *app = patch.processCreate(fullpath.c_str(), &argv[1]);
353 BPatch_image *image = app->getImage();
354
355 warnx("scanning the process for sdt with BPatch_object");
356 vector<BPatch_object *> objects;
357 image->getObjects(objects);
358 for (size_t i = 0; i < objects.size(); ++i)
359 {
360 BPatch_object* obj = objects[i];
361 string file = obj->pathName();
362 warnx(" found mapped object %s", file.c_str());
363
364 vector<sdt_point> sdt_points = find_sdt(file);
365 if (sdt_points.empty())
366 continue;
367
368 warnx("inserting sdt instrumentation in %s", file.c_str());
369 instrument_sdt_points(app, obj, sdt_points);
370 }
371
372 warnx("**** running the process");
373 app->continueExecution();
374 while (!app->isTerminated()) {
375 patch.waitForStatusChange();
376 }
377
378 warnx("done!");
379 return check_dyninst_exit(app) ? EXIT_SUCCESS : EXIT_FAILURE;
380 }
381
382 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */