]>
Commit | Line | Data |
---|---|---|
440f755a | 1 | // C++ interface to dwfl |
4c5d9906 | 2 | // Copyright (C) 2005-2013 Red Hat Inc. |
440f755a JS |
3 | // Copyright (C) 2005-2007 Intel Corporation. |
4 | // Copyright (C) 2008 James.Bottomley@HansenPartnership.com | |
5 | // | |
6 | // This file is part of systemtap, and is free software. You can | |
7 | // redistribute it and/or modify it under the terms of the GNU General | |
8 | // Public License (GPL); either version 2, or (at your option) any | |
9 | // later version. | |
10 | ||
11 | #include "dwflpp.h" | |
12 | #include "config.h" | |
13 | #include "staptree.h" | |
14 | #include "elaborate.h" | |
15 | #include "tapsets.h" | |
16 | #include "task_finder.h" | |
17 | #include "translate.h" | |
18 | #include "session.h" | |
19 | #include "util.h" | |
20 | #include "buildrun.h" | |
21 | #include "dwarf_wrappers.h" | |
22 | #include "auto_free.h" | |
23 | #include "hash.h" | |
2ed04863 | 24 | #include "rpm_finder.h" |
3db9c843 | 25 | #include "setupdwfl.h" |
440f755a JS |
26 | |
27 | #include <cstdlib> | |
28 | #include <algorithm> | |
29 | #include <deque> | |
30 | #include <iostream> | |
31 | #include <map> | |
440f755a JS |
32 | #include <set> |
33 | #include <sstream> | |
34 | #include <stdexcept> | |
35 | #include <vector> | |
36 | #include <cstdarg> | |
37 | #include <cassert> | |
38 | #include <iomanip> | |
39 | #include <cerrno> | |
40 | ||
41 | extern "C" { | |
42 | #include <fcntl.h> | |
43 | #include <elfutils/libdwfl.h> | |
44 | #include <elfutils/libdw.h> | |
45 | #include <dwarf.h> | |
46 | #include <elf.h> | |
47 | #include <obstack.h> | |
48 | #include <regex.h> | |
49 | #include <glob.h> | |
50 | #include <fnmatch.h> | |
51 | #include <stdio.h> | |
52 | #include <sys/types.h> | |
53 | ||
54 | #include "loc2c.h" | |
55 | #define __STDC_FORMAT_MACROS | |
56 | #include <inttypes.h> | |
57 | } | |
58 | ||
ae4163d9 MW |
59 | // Older glibc elf.h don't know about this new constant. |
60 | #ifndef STB_GNU_UNIQUE | |
61 | #define STB_GNU_UNIQUE 10 | |
62 | #endif | |
63 | ||
440f755a | 64 | |
9aa8ffce JS |
65 | // debug flag to compare to the uncached version from libdw |
66 | // #define DEBUG_DWFLPP_GETSCOPES 1 | |
67 | ||
68 | ||
440f755a JS |
69 | using namespace std; |
70 | using namespace __gnu_cxx; | |
71 | ||
72 | ||
73 | static string TOK_KERNEL("kernel"); | |
74 | ||
75 | ||
ae2552da | 76 | dwflpp::dwflpp(systemtap_session & session, const string& name, bool kernel_p): |
d0b4a5ff | 77 | sess(session), module(NULL), module_bias(0), mod_info(NULL), |
68983551 | 78 | module_start(0), module_end(0), cu(NULL), |
74fe61bc LB |
79 | module_dwarf(NULL), function(NULL), blacklist_func(), blacklist_func_ret(), |
80 | blacklist_file(), blacklist_enabled(false) | |
440f755a | 81 | { |
ae2552da | 82 | if (kernel_p) |
2eb99c62 | 83 | setup_kernel(name, session); |
440f755a | 84 | else |
0c16d512 JS |
85 | { |
86 | vector<string> modules; | |
87 | modules.push_back(name); | |
88 | setup_user(modules); | |
89 | } | |
90 | } | |
91 | ||
59c11f91 MW |
92 | dwflpp::dwflpp(systemtap_session & session, const vector<string>& names, |
93 | bool kernel_p): | |
0c16d512 | 94 | sess(session), module(NULL), module_bias(0), mod_info(NULL), |
68983551 | 95 | module_start(0), module_end(0), cu(NULL), |
729455a7 | 96 | module_dwarf(NULL), function(NULL), blacklist_enabled(false) |
0c16d512 | 97 | { |
59c11f91 MW |
98 | if (kernel_p) |
99 | setup_kernel(names); | |
100 | else | |
101 | setup_user(names); | |
440f755a JS |
102 | } |
103 | ||
440f755a JS |
104 | dwflpp::~dwflpp() |
105 | { | |
c9efa5c9 JS |
106 | delete_map(module_cu_cache); |
107 | delete_map(cu_function_cache); | |
4df79aaf | 108 | delete_map(mod_function_cache); |
c9efa5c9 JS |
109 | delete_map(cu_inl_function_cache); |
110 | delete_map(global_alias_cache); | |
111 | delete_map(cu_die_parent_cache); | |
9aa8ffce | 112 | |
68983551 | 113 | dwfl_ptr.reset(); |
435f53a7 FCE |
114 | // NB: don't "delete mod_info;", as that may be shared |
115 | // between dwflpp instances, and are stored in | |
116 | // session.module_cache[] anyway. | |
117 | } | |
118 | ||
119 | ||
120 | module_cache::~module_cache () | |
121 | { | |
122 | delete_map(cache); | |
440f755a JS |
123 | } |
124 | ||
125 | ||
440f755a JS |
126 | void |
127 | dwflpp::get_module_dwarf(bool required, bool report) | |
128 | { | |
129 | module_dwarf = dwfl_module_getdwarf(module, &module_bias); | |
130 | mod_info->dwarf_status = (module_dwarf ? info_present : info_absent); | |
131 | if (!module_dwarf && report) | |
132 | { | |
f9bbd346 | 133 | string msg = _("cannot find "); |
440f755a JS |
134 | if (module_name == "") |
135 | msg += "kernel"; | |
136 | else | |
137 | msg += string("module ") + module_name; | |
138 | msg += " debuginfo"; | |
139 | ||
140 | int i = dwfl_errno(); | |
141 | if (i) | |
142 | msg += string(": ") + dwfl_errmsg (i); | |
143 | ||
555fa669 FCE |
144 | msg += " [man warning::debuginfo]"; |
145 | ||
2ed04863 WC |
146 | /* add module_name to list to find rpm */ |
147 | find_debug_rpms(sess, module_name.c_str()); | |
148 | ||
440f755a | 149 | if (required) |
dc09353a | 150 | throw SEMANTIC_ERROR (msg); |
2713ea24 CM |
151 | else |
152 | sess.print_warning(msg); | |
440f755a JS |
153 | } |
154 | } | |
155 | ||
156 | ||
157 | void | |
158 | dwflpp::focus_on_module(Dwfl_Module * m, module_info * mi) | |
159 | { | |
160 | module = m; | |
161 | mod_info = mi; | |
162 | if (m) | |
163 | { | |
f517ee24 JS |
164 | module_name = dwfl_module_info(module, NULL, &module_start, &module_end, |
165 | NULL, NULL, NULL, NULL) ?: "module"; | |
440f755a JS |
166 | } |
167 | else | |
168 | { | |
169 | assert(mi && mi->name && mi->name == TOK_KERNEL); | |
170 | module_name = mi->name; | |
171 | module_start = 0; | |
172 | module_end = 0; | |
173 | module_bias = mi->bias; | |
174 | } | |
175 | ||
176 | // Reset existing pointers and names | |
177 | ||
178 | module_dwarf = NULL; | |
179 | ||
440f755a JS |
180 | cu = NULL; |
181 | ||
182 | function_name.clear(); | |
183 | function = NULL; | |
184 | } | |
185 | ||
186 | ||
187 | void | |
188 | dwflpp::focus_on_cu(Dwarf_Die * c) | |
189 | { | |
190 | assert(c); | |
191 | assert(module); | |
192 | ||
193 | cu = c; | |
440f755a JS |
194 | |
195 | // Reset existing pointers and names | |
196 | function_name.clear(); | |
197 | function = NULL; | |
198 | } | |
199 | ||
200 | ||
54417494 JS |
201 | string |
202 | dwflpp::cu_name(void) | |
203 | { | |
204 | return dwarf_diename(cu) ?: "<unknown source>"; | |
205 | } | |
206 | ||
207 | ||
440f755a JS |
208 | void |
209 | dwflpp::focus_on_function(Dwarf_Die * f) | |
210 | { | |
211 | assert(f); | |
212 | assert(module); | |
213 | assert(cu); | |
214 | ||
215 | function = f; | |
f517ee24 | 216 | function_name = dwarf_diename(function) ?: "function"; |
440f755a JS |
217 | } |
218 | ||
219 | ||
1f8592d1 MW |
220 | /* Return the Dwarf_Die for the given address in the current module. |
221 | * The address should be in the module address address space (this | |
222 | * function will take care of any dw bias). | |
223 | */ | |
1adf8ef1 JS |
224 | Dwarf_Die * |
225 | dwflpp::query_cu_containing_address(Dwarf_Addr a) | |
440f755a JS |
226 | { |
227 | Dwarf_Addr bias; | |
68983551 | 228 | assert(dwfl_ptr.get()->dwfl); |
b6fa229b | 229 | assert(module); |
440f755a | 230 | get_module_dwarf(); |
b6fa229b | 231 | |
440f755a | 232 | Dwarf_Die* cudie = dwfl_module_addrdie(module, a, &bias); |
440f755a | 233 | assert(bias == module_bias); |
1adf8ef1 | 234 | return cudie; |
440f755a JS |
235 | } |
236 | ||
237 | ||
440f755a | 238 | bool |
5f4c8c6e | 239 | dwflpp::module_name_matches(const string& pattern) |
440f755a JS |
240 | { |
241 | bool t = (fnmatch(pattern.c_str(), module_name.c_str(), 0) == 0); | |
242 | if (t && sess.verbose>3) | |
f9bbd346 LB |
243 | clog << _F("pattern '%s' matches module '%s'\n", |
244 | pattern.c_str(), module_name.c_str()); | |
044a265a FCE |
245 | if (!t && sess.verbose>4) |
246 | clog << _F("pattern '%s' does not match module '%s'\n", | |
247 | pattern.c_str(), module_name.c_str()); | |
248 | ||
440f755a JS |
249 | return t; |
250 | } | |
251 | ||
252 | ||
253 | bool | |
665e1256 | 254 | dwflpp::name_has_wildcard (const string& pattern) |
440f755a JS |
255 | { |
256 | return (pattern.find('*') != string::npos || | |
257 | pattern.find('?') != string::npos || | |
258 | pattern.find('[') != string::npos); | |
259 | } | |
260 | ||
261 | ||
262 | bool | |
5f4c8c6e | 263 | dwflpp::module_name_final_match(const string& pattern) |
440f755a JS |
264 | { |
265 | // Assume module_name_matches(). Can there be any more matches? | |
266 | // Not unless the pattern is a wildcard, since module names are | |
267 | // presumed unique. | |
268 | return !name_has_wildcard(pattern); | |
269 | } | |
270 | ||
271 | ||
272 | bool | |
5f4c8c6e | 273 | dwflpp::function_name_matches_pattern(const string& name, const string& pattern) |
440f755a JS |
274 | { |
275 | bool t = (fnmatch(pattern.c_str(), name.c_str(), 0) == 0); | |
276 | if (t && sess.verbose>3) | |
f9bbd346 | 277 | clog << _F("pattern '%s' matches function '%s'\n", pattern.c_str(), name.c_str()); |
440f755a JS |
278 | return t; |
279 | } | |
280 | ||
281 | ||
282 | bool | |
5f4c8c6e | 283 | dwflpp::function_name_matches(const string& pattern) |
440f755a JS |
284 | { |
285 | assert(function); | |
286 | return function_name_matches_pattern(function_name, pattern); | |
287 | } | |
288 | ||
289 | ||
7d6d0afc | 290 | bool |
c646240d | 291 | dwflpp::function_scope_matches(const vector<string>& scopes) |
7d6d0afc JS |
292 | { |
293 | // walk up the containing scopes | |
294 | Dwarf_Die* die = function; | |
295 | for (int i = scopes.size() - 1; i >= 0; --i) | |
296 | { | |
297 | die = get_parent_scope(die); | |
298 | ||
299 | // check if this scope matches, and prepend it if so | |
300 | // NB: a NULL die is the global scope, compared as "" | |
301 | string name = dwarf_diename(die) ?: ""; | |
302 | if (name_has_wildcard(scopes[i]) ? | |
303 | function_name_matches_pattern(name, scopes[i]) : | |
304 | name == scopes[i]) | |
305 | function_name = name + "::" + function_name; | |
306 | else | |
307 | return false; | |
308 | ||
309 | // make sure there's no more if we're at the global scope | |
310 | if (!die && i > 0) | |
311 | return false; | |
312 | } | |
313 | return true; | |
314 | } | |
315 | ||
316 | ||
440f755a | 317 | void |
2eb99c62 | 318 | dwflpp::setup_kernel(const string& name, systemtap_session & s, bool debuginfo_needed) |
440f755a | 319 | { |
440f755a JS |
320 | if (! sess.module_cache) |
321 | sess.module_cache = new module_cache (); | |
322 | ||
3db9c843 | 323 | unsigned offline_search_matches = 0; |
68983551 | 324 | dwfl_ptr = setup_dwfl_kernel(name, &offline_search_matches, sess); |
440f755a | 325 | |
3db9c843 | 326 | if (offline_search_matches < 1) |
ae2552da FCE |
327 | { |
328 | if (debuginfo_needed) { | |
329 | // Suggest a likely kernel dir to find debuginfo rpm for | |
05fb3e0c | 330 | string dir = string(sess.sysroot + "/lib/modules/" + sess.kernel_release ); |
ae2552da FCE |
331 | find_debug_rpms(sess, dir.c_str()); |
332 | } | |
dc09353a | 333 | throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'", |
f9bbd346 | 334 | sess.architecture.c_str(), sess.kernel_build_tree.c_str())); |
209dd533 | 335 | } |
2eb99c62 CM |
336 | Dwfl *dwfl = dwfl_ptr.get()->dwfl; |
337 | if (dwfl != NULL) | |
338 | { | |
339 | ptrdiff_t off = 0; | |
340 | do | |
341 | { | |
e19ebcf7 | 342 | assert_no_interrupts(); |
2eb99c62 CM |
343 | off = dwfl_getmodules (dwfl, &add_module_build_id_to_hash, &s, off); |
344 | } | |
345 | while (off > 0); | |
346 | dwfl_assert("dwfl_getmodules", off == 0); | |
347 | } | |
440f755a | 348 | |
27646582 | 349 | build_blacklist(); |
440f755a JS |
350 | } |
351 | ||
59c11f91 MW |
352 | void |
353 | dwflpp::setup_kernel(const vector<string> &names, bool debuginfo_needed) | |
354 | { | |
355 | if (! sess.module_cache) | |
356 | sess.module_cache = new module_cache (); | |
357 | ||
358 | unsigned offline_search_matches = 0; | |
359 | set<string> offline_search_names(names.begin(), names.end()); | |
68983551 MW |
360 | dwfl_ptr = setup_dwfl_kernel(offline_search_names, |
361 | &offline_search_matches, | |
362 | sess); | |
59c11f91 MW |
363 | |
364 | if (offline_search_matches < offline_search_names.size()) | |
365 | { | |
366 | if (debuginfo_needed) { | |
367 | // Suggest a likely kernel dir to find debuginfo rpm for | |
05fb3e0c | 368 | string dir = string(sess.sysroot + "/lib/modules/" + sess.kernel_release ); |
59c11f91 MW |
369 | find_debug_rpms(sess, dir.c_str()); |
370 | } | |
dc09353a | 371 | throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'", |
f9bbd346 | 372 | sess.architecture.c_str(), sess.kernel_build_tree.c_str())); |
59c11f91 MW |
373 | } |
374 | ||
375 | build_blacklist(); | |
376 | } | |
377 | ||
440f755a JS |
378 | |
379 | void | |
0c16d512 | 380 | dwflpp::setup_user(const vector<string>& modules, bool debuginfo_needed) |
440f755a JS |
381 | { |
382 | if (! sess.module_cache) | |
383 | sess.module_cache = new module_cache (); | |
384 | ||
5f8ca04f | 385 | vector<string>::const_iterator it = modules.begin(); |
e1e8b44e | 386 | dwfl_ptr = setup_dwfl_user(it, modules.end(), debuginfo_needed, sess); |
5f8ca04f | 387 | if (debuginfo_needed && it != modules.end()) |
ce0f6648 LB |
388 | dwfl_assert (string(_F("missing process %s %s debuginfo", |
389 | (*it).c_str(), sess.architecture.c_str())), | |
390 | dwfl_ptr.get()->dwfl); | |
440f755a JS |
391 | } |
392 | ||
06de3a04 JL |
393 | template<> void |
394 | dwflpp::iterate_over_modules<void>(int (*callback)(Dwfl_Module*, | |
395 | void**, | |
396 | const char*, | |
397 | Dwarf_Addr, | |
398 | void*), | |
399 | void *data) | |
440f755a | 400 | { |
68983551 | 401 | dwfl_getmodules (dwfl_ptr.get()->dwfl, callback, data, 0); |
0e14e079 | 402 | |
440f755a JS |
403 | // Don't complain if we exited dwfl_getmodules early. |
404 | // This could be a $target variable error that will be | |
405 | // reported soon anyway. | |
406 | // dwfl_assert("dwfl_getmodules", off == 0); | |
407 | ||
408 | // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the | |
409 | // "kernel" module is not reported in the loop above. However, we | |
410 | // may be able to make do with symbol table data. | |
411 | } | |
412 | ||
413 | ||
5c378838 JL |
414 | template<> void |
415 | dwflpp::iterate_over_cus<void>(int (*callback)(Dwarf_Die*, void*), | |
416 | void *data, | |
417 | bool want_types) | |
440f755a JS |
418 | { |
419 | get_module_dwarf(false); | |
420 | Dwarf *dw = module_dwarf; | |
421 | if (!dw) return; | |
422 | ||
423 | vector<Dwarf_Die>* v = module_cu_cache[dw]; | |
424 | if (v == 0) | |
425 | { | |
426 | v = new vector<Dwarf_Die>; | |
427 | module_cu_cache[dw] = v; | |
428 | ||
429 | Dwarf_Off off = 0; | |
430 | size_t cuhl; | |
431 | Dwarf_Off noff; | |
432 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) | |
433 | { | |
e19ebcf7 | 434 | assert_no_interrupts(); |
440f755a JS |
435 | Dwarf_Die die_mem; |
436 | Dwarf_Die *die; | |
437 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
dee830d9 MW |
438 | /* Skip partial units. */ |
439 | if (dwarf_tag (die) == DW_TAG_compile_unit) | |
440 | v->push_back (*die); /* copy */ | |
440f755a JS |
441 | off = noff; |
442 | } | |
443 | } | |
444 | ||
337b7c44 TT |
445 | if (want_types && module_tus_read.find(dw) == module_tus_read.end()) |
446 | { | |
447 | // Process type units. | |
448 | Dwarf_Off off = 0; | |
449 | size_t cuhl; | |
450 | Dwarf_Off noff; | |
451 | uint64_t type_signature; | |
452 | while (dwarf_next_unit (dw, off, &noff, &cuhl, NULL, NULL, NULL, NULL, | |
453 | &type_signature, NULL) == 0) | |
454 | { | |
e19ebcf7 | 455 | assert_no_interrupts(); |
337b7c44 TT |
456 | Dwarf_Die die_mem; |
457 | Dwarf_Die *die; | |
458 | die = dwarf_offdie_types (dw, off + cuhl, &die_mem); | |
dee830d9 MW |
459 | /* Skip partial units. */ |
460 | if (dwarf_tag (die) == DW_TAG_type_unit) | |
461 | v->push_back (*die); /* copy */ | |
337b7c44 TT |
462 | off = noff; |
463 | } | |
464 | module_tus_read.insert(dw); | |
465 | } | |
466 | ||
c1c5bb9a | 467 | for (vector<Dwarf_Die>::iterator i = v->begin(); i != v->end(); ++i) |
440f755a | 468 | { |
c1c5bb9a | 469 | int rc = (*callback)(&*i, data); |
e19ebcf7 | 470 | assert_no_interrupts(); |
985892de | 471 | if (rc != DWARF_CB_OK) |
c1c5bb9a | 472 | break; |
440f755a JS |
473 | } |
474 | } | |
475 | ||
476 | ||
477 | bool | |
478 | dwflpp::func_is_inline() | |
479 | { | |
480 | assert (function); | |
481 | return dwarf_func_inline (function) != 0; | |
482 | } | |
483 | ||
484 | ||
4bda987e SC |
485 | bool |
486 | dwflpp::func_is_exported() | |
487 | { | |
488 | const char *name = dwarf_linkage_name (function) ?: dwarf_diename (function); | |
489 | ||
490 | assert (function); | |
491 | ||
492 | int syms = dwfl_module_getsymtab (module); | |
493 | dwfl_assert (_("Getting symbols"), syms >= 0); | |
494 | ||
495 | for (int i = 0; i < syms; i++) | |
496 | { | |
497 | GElf_Sym sym; | |
498 | GElf_Word shndxp; | |
499 | const char *symname = dwfl_module_getsym(module, i, &sym, &shndxp); | |
500 | if (symname | |
501 | && strcmp (name, symname) == 0) | |
502 | { | |
503 | if (GELF_ST_TYPE(sym.st_info) == STT_FUNC | |
504 | && (GELF_ST_BIND(sym.st_info) == STB_GLOBAL | |
5c204fc3 SC |
505 | || GELF_ST_BIND(sym.st_info) == STB_WEAK |
506 | || GELF_ST_BIND(sym.st_info) == STB_GNU_UNIQUE)) | |
4bda987e SC |
507 | return true; |
508 | else | |
509 | return false; | |
510 | } | |
511 | } | |
512 | return false; | |
513 | } | |
514 | ||
8d7a7bd9 JS |
515 | void |
516 | dwflpp::cache_inline_instances (Dwarf_Die* die) | |
440f755a | 517 | { |
8d7a7bd9 JS |
518 | // If this is an inline instance, link it back to its origin |
519 | Dwarf_Die origin; | |
520 | if (dwarf_tag(die) == DW_TAG_inlined_subroutine && | |
521 | dwarf_attr_die(die, DW_AT_abstract_origin, &origin)) | |
522 | { | |
523 | vector<Dwarf_Die>*& v = cu_inl_function_cache[origin.addr]; | |
524 | if (!v) | |
525 | v = new vector<Dwarf_Die>; | |
526 | v->push_back(*die); | |
527 | } | |
528 | ||
529 | // Recurse through other scopes that may contain inlines | |
530 | Dwarf_Die child, import; | |
531 | if (dwarf_child(die, &child) == 0) | |
532 | do | |
533 | { | |
534 | switch (dwarf_tag (&child)) | |
535 | { | |
536 | // tags that could contain inlines | |
537 | case DW_TAG_compile_unit: | |
538 | case DW_TAG_module: | |
539 | case DW_TAG_lexical_block: | |
540 | case DW_TAG_with_stmt: | |
541 | case DW_TAG_catch_block: | |
542 | case DW_TAG_try_block: | |
543 | case DW_TAG_entry_point: | |
544 | case DW_TAG_inlined_subroutine: | |
545 | case DW_TAG_subprogram: | |
546 | cache_inline_instances(&child); | |
547 | break; | |
548 | ||
549 | // imported dies should be followed | |
550 | case DW_TAG_imported_unit: | |
551 | if (dwarf_attr_die(&child, DW_AT_import, &import)) | |
552 | cache_inline_instances(&import); | |
553 | break; | |
554 | ||
555 | // nothing to do for other tags | |
556 | default: | |
557 | break; | |
558 | } | |
559 | } | |
560 | while (dwarf_siblingof(&child, &child) == 0); | |
440f755a JS |
561 | } |
562 | ||
563 | ||
3956fccb JL |
564 | template<> void |
565 | dwflpp::iterate_over_inline_instances<void>(int (*callback)(Dwarf_Die*, void*), | |
566 | void *data) | |
440f755a JS |
567 | { |
568 | assert (function); | |
569 | assert (func_is_inline ()); | |
570 | ||
8d7a7bd9 JS |
571 | if (cu_inl_function_cache_done.insert(cu->addr).second) |
572 | cache_inline_instances(cu); | |
573 | ||
d089f5b2 | 574 | vector<Dwarf_Die>* v = cu_inl_function_cache[function->addr]; |
8d7a7bd9 JS |
575 | if (!v) |
576 | return; | |
440f755a | 577 | |
c1c5bb9a | 578 | for (vector<Dwarf_Die>::iterator i = v->begin(); i != v->end(); ++i) |
440f755a | 579 | { |
c1c5bb9a | 580 | int rc = (*callback)(&*i, data); |
e19ebcf7 | 581 | assert_no_interrupts(); |
985892de | 582 | if (rc != DWARF_CB_OK) |
c1c5bb9a | 583 | break; |
440f755a JS |
584 | } |
585 | } | |
586 | ||
587 | ||
9aa8ffce JS |
588 | void |
589 | dwflpp::cache_die_parents(cu_die_parent_cache_t* parents, Dwarf_Die* die) | |
590 | { | |
591 | // Record and recurse through DIEs we care about | |
592 | Dwarf_Die child, import; | |
593 | if (dwarf_child(die, &child) == 0) | |
594 | do | |
595 | { | |
596 | switch (dwarf_tag (&child)) | |
597 | { | |
598 | // normal tags to recurse | |
599 | case DW_TAG_compile_unit: | |
600 | case DW_TAG_module: | |
601 | case DW_TAG_lexical_block: | |
602 | case DW_TAG_with_stmt: | |
603 | case DW_TAG_catch_block: | |
604 | case DW_TAG_try_block: | |
605 | case DW_TAG_entry_point: | |
606 | case DW_TAG_inlined_subroutine: | |
607 | case DW_TAG_subprogram: | |
7d6d0afc JS |
608 | case DW_TAG_namespace: |
609 | case DW_TAG_class_type: | |
610 | case DW_TAG_structure_type: | |
9aa8ffce JS |
611 | parents->insert(make_pair(child.addr, *die)); |
612 | cache_die_parents(parents, &child); | |
613 | break; | |
614 | ||
615 | // record only, nothing to recurse | |
616 | case DW_TAG_label: | |
617 | parents->insert(make_pair(child.addr, *die)); | |
618 | break; | |
619 | ||
620 | // imported dies should be followed | |
621 | case DW_TAG_imported_unit: | |
622 | if (dwarf_attr_die(&child, DW_AT_import, &import)) | |
623 | { | |
624 | parents->insert(make_pair(import.addr, *die)); | |
625 | cache_die_parents(parents, &import); | |
626 | } | |
627 | break; | |
628 | ||
629 | // nothing to do for other tags | |
630 | default: | |
631 | break; | |
632 | } | |
633 | } | |
634 | while (dwarf_siblingof(&child, &child) == 0); | |
635 | } | |
636 | ||
637 | ||
729455a7 JS |
638 | cu_die_parent_cache_t* |
639 | dwflpp::get_die_parents() | |
9aa8ffce JS |
640 | { |
641 | assert (cu); | |
642 | ||
729455a7 | 643 | cu_die_parent_cache_t *& parents = cu_die_parent_cache[cu->addr]; |
9aa8ffce JS |
644 | if (!parents) |
645 | { | |
646 | parents = new cu_die_parent_cache_t; | |
9aa8ffce JS |
647 | cache_die_parents(parents, cu); |
648 | if (sess.verbose > 4) | |
f9bbd346 LB |
649 | clog << _F("die parent cache %s:%s size %zu", module_name.c_str(), |
650 | cu_name().c_str(), parents->size()) << endl; | |
9aa8ffce | 651 | } |
729455a7 JS |
652 | return parents; |
653 | } | |
654 | ||
655 | ||
656 | vector<Dwarf_Die> | |
657 | dwflpp::getscopes_die(Dwarf_Die* die) | |
658 | { | |
659 | cu_die_parent_cache_t *parents = get_die_parents(); | |
9aa8ffce JS |
660 | |
661 | vector<Dwarf_Die> scopes; | |
729455a7 JS |
662 | Dwarf_Die *scope = die; |
663 | cu_die_parent_cache_t::iterator it; | |
664 | do | |
665 | { | |
666 | scopes.push_back(*scope); | |
667 | it = parents->find(scope->addr); | |
668 | scope = &it->second; | |
669 | } | |
670 | while (it != parents->end()); | |
9aa8ffce JS |
671 | |
672 | #ifdef DEBUG_DWFLPP_GETSCOPES | |
729455a7 | 673 | Dwarf_Die *dscopes = NULL; |
9aa8ffce JS |
674 | int nscopes = dwarf_getscopes_die(die, &dscopes); |
675 | ||
676 | assert(nscopes == (int)scopes.size()); | |
677 | for (unsigned i = 0; i < scopes.size(); ++i) | |
678 | assert(scopes[i].addr == dscopes[i].addr); | |
679 | free(dscopes); | |
680 | #endif | |
681 | ||
682 | return scopes; | |
683 | } | |
684 | ||
685 | ||
729455a7 JS |
686 | std::vector<Dwarf_Die> |
687 | dwflpp::getscopes(Dwarf_Die* die) | |
688 | { | |
689 | cu_die_parent_cache_t *parents = get_die_parents(); | |
690 | ||
691 | vector<Dwarf_Die> scopes; | |
692 | ||
693 | Dwarf_Die origin; | |
694 | Dwarf_Die *scope = die; | |
695 | cu_die_parent_cache_t::iterator it; | |
696 | do | |
697 | { | |
698 | scopes.push_back(*scope); | |
699 | if (dwarf_tag(scope) == DW_TAG_inlined_subroutine && | |
700 | dwarf_attr_die(scope, DW_AT_abstract_origin, &origin)) | |
701 | scope = &origin; | |
702 | ||
703 | it = parents->find(scope->addr); | |
704 | scope = &it->second; | |
705 | } | |
706 | while (it != parents->end()); | |
707 | ||
708 | #ifdef DEBUG_DWFLPP_GETSCOPES | |
709 | // there isn't an exact libdw equivalent, but if dwarf_getscopes on the | |
710 | // entrypc returns the same first die, then all the scopes should match | |
711 | Dwarf_Addr pc; | |
712 | if (die_entrypc(die, &pc)) | |
713 | { | |
714 | Dwarf_Die *dscopes = NULL; | |
715 | int nscopes = dwarf_getscopes(cu, pc, &dscopes); | |
716 | if (nscopes > 0 && dscopes[0].addr == die->addr) | |
717 | { | |
718 | assert(nscopes == (int)scopes.size()); | |
719 | for (unsigned i = 0; i < scopes.size(); ++i) | |
720 | assert(scopes[i].addr == dscopes[i].addr); | |
721 | } | |
722 | free(dscopes); | |
723 | } | |
724 | #endif | |
725 | ||
726 | return scopes; | |
727 | } | |
728 | ||
729 | ||
730 | std::vector<Dwarf_Die> | |
731 | dwflpp::getscopes(Dwarf_Addr pc) | |
732 | { | |
733 | // The die_parent_cache doesn't help us without knowing where the pc is | |
734 | // contained, so we have to do this one the old fashioned way. | |
735 | ||
736 | assert (cu); | |
737 | ||
738 | vector<Dwarf_Die> scopes; | |
739 | ||
740 | Dwarf_Die* dwarf_scopes; | |
741 | int nscopes = dwarf_getscopes(cu, pc, &dwarf_scopes); | |
742 | if (nscopes > 0) | |
743 | { | |
744 | scopes.assign(dwarf_scopes, dwarf_scopes + nscopes); | |
745 | free(dwarf_scopes); | |
746 | } | |
747 | ||
748 | #ifdef DEBUG_DWFLPP_GETSCOPES | |
749 | // check that getscopes on the starting die gets the same result | |
750 | if (!scopes.empty()) | |
751 | { | |
752 | vector<Dwarf_Die> other = getscopes(&scopes[0]); | |
753 | assert(scopes.size() == other.size()); | |
754 | for (unsigned i = 0; i < scopes.size(); ++i) | |
755 | assert(scopes[i].addr == other[i].addr); | |
756 | } | |
757 | #endif | |
758 | ||
759 | return scopes; | |
760 | } | |
761 | ||
762 | ||
7d6d0afc JS |
763 | Dwarf_Die* |
764 | dwflpp::get_parent_scope(Dwarf_Die* die) | |
765 | { | |
766 | Dwarf_Die specification; | |
767 | if (dwarf_attr_die(die, DW_AT_specification, &specification)) | |
768 | die = &specification; | |
769 | ||
770 | cu_die_parent_cache_t *parents = get_die_parents(); | |
771 | cu_die_parent_cache_t::iterator it = parents->find(die->addr); | |
772 | while (it != parents->end()) | |
773 | { | |
774 | Dwarf_Die* scope = &it->second; | |
775 | switch (dwarf_tag (scope)) | |
776 | { | |
777 | case DW_TAG_namespace: | |
778 | case DW_TAG_class_type: | |
779 | case DW_TAG_structure_type: | |
780 | return scope; | |
781 | ||
782 | default: | |
783 | break; | |
784 | } | |
785 | it = parents->find(scope->addr); | |
786 | } | |
787 | return NULL; | |
788 | } | |
789 | ||
a44a7cb5 JS |
790 | static const char* |
791 | cache_type_prefix(Dwarf_Die* type) | |
792 | { | |
793 | switch (dwarf_tag(type)) | |
794 | { | |
795 | case DW_TAG_enumeration_type: | |
796 | return "enum "; | |
797 | case DW_TAG_structure_type: | |
798 | case DW_TAG_class_type: | |
799 | // treating struct/class as equals | |
800 | return "struct "; | |
801 | case DW_TAG_union_type: | |
802 | return "union "; | |
803 | } | |
804 | return ""; | |
805 | } | |
7d6d0afc | 806 | |
87eeec94 MW |
807 | /* GCC might generate a struct/class without DW_AT_declaration, |
808 | but that only contains members which have DW_AT_declaration | |
809 | set. We aren't interested in those. PR14434 (GCC bug #54181). */ | |
810 | static bool | |
811 | has_only_decl_members (Dwarf_Die *die) | |
812 | { | |
dee830d9 | 813 | Dwarf_Die child, import; |
87eeec94 MW |
814 | if (dwarf_child(die, &child) != 0) |
815 | return false; /* no members */ | |
816 | ||
817 | do | |
818 | { | |
819 | if (! dwarf_hasattr(&child, DW_AT_declaration)) | |
820 | return false; /* real member found. */ | |
821 | int tag = dwarf_tag(&child); | |
822 | if ((tag == DW_TAG_namespace | |
823 | || tag == DW_TAG_structure_type | |
824 | || tag == DW_TAG_class_type) | |
825 | && ! has_only_decl_members (&child)) | |
826 | return false; /* real grand child member found. */ | |
dee830d9 MW |
827 | |
828 | // Unlikely to ever happen, but if there is an imported unit | |
829 | // then check its children as if they are children of this DIE. | |
830 | if (tag == DW_TAG_imported_unit | |
831 | && dwarf_attr_die(&child, DW_AT_import, &import) | |
832 | && ! has_only_decl_members (&import)) | |
833 | return false; | |
87eeec94 MW |
834 | } |
835 | while (dwarf_siblingof(&child, &child) == 0); | |
836 | ||
837 | return true; /* Tried all children and grandchildren. */ | |
838 | } | |
839 | ||
440f755a | 840 | int |
3805a31e JS |
841 | dwflpp::global_alias_caching_callback(Dwarf_Die *die, bool has_inner_types, |
842 | const string& prefix, void *arg) | |
440f755a | 843 | { |
b7478964 | 844 | cu_type_cache_t *cache = static_cast<cu_type_cache_t*>(arg); |
440f755a JS |
845 | const char *name = dwarf_diename(die); |
846 | ||
87eeec94 MW |
847 | if (!name || dwarf_hasattr(die, DW_AT_declaration) |
848 | || has_only_decl_members(die)) | |
440f755a JS |
849 | return DWARF_CB_OK; |
850 | ||
3805a31e JS |
851 | int tag = dwarf_tag(die); |
852 | if (has_inner_types && (tag == DW_TAG_namespace | |
853 | || tag == DW_TAG_structure_type | |
854 | || tag == DW_TAG_class_type)) | |
855 | iterate_over_types(die, has_inner_types, prefix + name + "::", | |
856 | global_alias_caching_callback, arg); | |
857 | ||
858 | if (tag != DW_TAG_namespace) | |
859 | { | |
860 | string type_name = prefix + cache_type_prefix(die) + name; | |
861 | if (cache->find(type_name) == cache->end()) | |
862 | (*cache)[type_name] = *die; | |
863 | } | |
440f755a JS |
864 | |
865 | return DWARF_CB_OK; | |
866 | } | |
867 | ||
063906a9 | 868 | int |
5c378838 | 869 | dwflpp::global_alias_caching_callback_cus(Dwarf_Die *die, dwflpp *dw) |
063906a9 MW |
870 | { |
871 | mod_cu_type_cache_t *global_alias_cache; | |
5c378838 | 872 | global_alias_cache = &dw->global_alias_cache; |
063906a9 MW |
873 | |
874 | cu_type_cache_t *v = (*global_alias_cache)[die->addr]; | |
875 | if (v != 0) | |
876 | return DWARF_CB_OK; | |
877 | ||
878 | v = new cu_type_cache_t; | |
879 | (*global_alias_cache)[die->addr] = v; | |
880 | iterate_over_globals(die, global_alias_caching_callback, v); | |
881 | ||
882 | return DWARF_CB_OK; | |
883 | } | |
884 | ||
885 | Dwarf_Die * | |
a44a7cb5 | 886 | dwflpp::declaration_resolve_other_cus(const string& name) |
063906a9 | 887 | { |
337b7c44 | 888 | iterate_over_cus(global_alias_caching_callback_cus, this, true); |
063906a9 MW |
889 | for (mod_cu_type_cache_t::iterator i = global_alias_cache.begin(); |
890 | i != global_alias_cache.end(); ++i) | |
891 | { | |
892 | cu_type_cache_t *v = (*i).second; | |
893 | if (v->find(name) != v->end()) | |
894 | return & ((*v)[name]); | |
895 | } | |
896 | ||
897 | return NULL; | |
898 | } | |
440f755a JS |
899 | |
900 | Dwarf_Die * | |
a44a7cb5 | 901 | dwflpp::declaration_resolve(const string& name) |
440f755a | 902 | { |
b7478964 | 903 | cu_type_cache_t *v = global_alias_cache[cu->addr]; |
440f755a JS |
904 | if (v == 0) // need to build the cache, just once per encountered module/cu |
905 | { | |
b7478964 | 906 | v = new cu_type_cache_t; |
54558065 | 907 | global_alias_cache[cu->addr] = v; |
063906a9 | 908 | iterate_over_globals(cu, global_alias_caching_callback, v); |
440f755a | 909 | if (sess.verbose > 4) |
f9bbd346 LB |
910 | clog << _F("global alias cache %s:%s size %zu", module_name.c_str(), |
911 | cu_name().c_str(), v->size()) << endl; | |
440f755a JS |
912 | } |
913 | ||
914 | // XXX: it may be desirable to search other modules' declarations | |
915 | // too, in case a module/shared-library processes a | |
916 | // forward-declared pointer type only, where the actual definition | |
917 | // may only be in vmlinux or the application. | |
918 | ||
440f755a | 919 | if (v->find(name) == v->end()) |
063906a9 | 920 | return declaration_resolve_other_cus(name); |
440f755a JS |
921 | |
922 | return & ((*v)[name]); | |
923 | } | |
924 | ||
a44a7cb5 JS |
925 | Dwarf_Die * |
926 | dwflpp::declaration_resolve(Dwarf_Die *type) | |
927 | { | |
928 | const char* name = dwarf_diename(type); | |
929 | if (!name) | |
930 | return NULL; | |
931 | ||
932 | string type_name = cache_type_prefix(type) + string(name); | |
933 | return declaration_resolve(type_name); | |
934 | } | |
935 | ||
440f755a JS |
936 | |
937 | int | |
5c378838 | 938 | dwflpp::cu_function_caching_callback (Dwarf_Die* func, cu_function_cache_t *v) |
440f755a | 939 | { |
440f755a JS |
940 | const char *name = dwarf_diename(func); |
941 | if (!name) | |
942 | return DWARF_CB_OK; | |
943 | ||
b7478964 | 944 | v->insert(make_pair(string(name), *func)); |
440f755a JS |
945 | return DWARF_CB_OK; |
946 | } | |
947 | ||
948 | ||
4df79aaf | 949 | int |
5c378838 | 950 | dwflpp::mod_function_caching_callback (Dwarf_Die* cu, cu_function_cache_t *v) |
4df79aaf | 951 | { |
5c378838 JL |
952 | // need to cast callback to func which accepts void* |
953 | dwarf_getfuncs (cu, (int (*)(Dwarf_Die*, void*))cu_function_caching_callback, | |
954 | v, 0); | |
4df79aaf JS |
955 | return DWARF_CB_OK; |
956 | } | |
957 | ||
958 | ||
7d007451 JL |
959 | template<> int |
960 | dwflpp::iterate_over_functions<void>(int (*callback)(Dwarf_Die*, void*), | |
961 | void *data, const string& function) | |
440f755a JS |
962 | { |
963 | int rc = DWARF_CB_OK; | |
964 | assert (module); | |
965 | assert (cu); | |
966 | ||
54558065 | 967 | cu_function_cache_t *v = cu_function_cache[cu->addr]; |
440f755a JS |
968 | if (v == 0) |
969 | { | |
970 | v = new cu_function_cache_t; | |
54558065 | 971 | cu_function_cache[cu->addr] = v; |
5c378838 JL |
972 | // need to cast callback to func which accepts void* |
973 | dwarf_getfuncs (cu, (int (*)(Dwarf_Die*, void*))cu_function_caching_callback, | |
974 | v, 0); | |
440f755a | 975 | if (sess.verbose > 4) |
f9bbd346 LB |
976 | clog << _F("function cache %s:%s size %zu", module_name.c_str(), |
977 | cu_name().c_str(), v->size()) << endl; | |
1c6b77e5 | 978 | mod_info->update_symtab(v); |
440f755a JS |
979 | } |
980 | ||
b7478964 JS |
981 | cu_function_cache_t::iterator it; |
982 | cu_function_cache_range_t range = v->equal_range(function); | |
983 | if (range.first != range.second) | |
440f755a | 984 | { |
b7478964 JS |
985 | for (it = range.first; it != range.second; ++it) |
986 | { | |
987 | Dwarf_Die& die = it->second; | |
988 | if (sess.verbose > 4) | |
f9bbd346 LB |
989 | clog << _F("function cache %s:%s hit %s", module_name.c_str(), |
990 | cu_name().c_str(), function.c_str()) << endl; | |
7d007451 | 991 | rc = (*callback)(& die, data); |
b7478964 JS |
992 | if (rc != DWARF_CB_OK) break; |
993 | } | |
440f755a | 994 | } |
1ffb8bd1 JS |
995 | else if (startswith(function, "_Z")) |
996 | { | |
997 | // C++ names are mangled starting with a "_Z" prefix. Most of the time | |
998 | // we can discover the mangled name from a die's MIPS_linkage_name | |
999 | // attribute, so we read that to match against the user's function | |
1000 | // pattern. Note that this isn't perfect, as not all will have that | |
1001 | // attribute (notably ctors and dtors), but we do what we can... | |
1002 | for (it = v->begin(); it != v->end(); ++it) | |
1003 | { | |
1004 | if (pending_interrupts) return DWARF_CB_ABORT; | |
1005 | Dwarf_Die& die = it->second; | |
157f5dd0 | 1006 | const char* linkage_name = NULL; |
f450a7e3 | 1007 | if ((linkage_name = dwarf_linkage_name (&die)) |
1ffb8bd1 JS |
1008 | && function_name_matches_pattern (linkage_name, function)) |
1009 | { | |
1010 | if (sess.verbose > 4) | |
f9bbd346 LB |
1011 | clog << _F("function cache %s:%s match %s vs %s", module_name.c_str(), |
1012 | cu_name().c_str(), linkage_name, function.c_str()) << endl; | |
1ffb8bd1 | 1013 | |
7d007451 | 1014 | rc = (*callback)(& die, data); |
1ffb8bd1 JS |
1015 | if (rc != DWARF_CB_OK) break; |
1016 | } | |
1017 | } | |
1018 | } | |
5f4c8c6e | 1019 | else if (name_has_wildcard (function)) |
440f755a | 1020 | { |
c1c5bb9a | 1021 | for (it = v->begin(); it != v->end(); ++it) |
440f755a | 1022 | { |
85007c04 | 1023 | if (pending_interrupts) return DWARF_CB_ABORT; |
1c6b77e5 JS |
1024 | const string& func_name = it->first; |
1025 | Dwarf_Die& die = it->second; | |
5f4c8c6e | 1026 | if (function_name_matches_pattern (func_name, function)) |
440f755a JS |
1027 | { |
1028 | if (sess.verbose > 4) | |
f9bbd346 LB |
1029 | clog << _F("function cache %s:%s match %s vs %s", module_name.c_str(), |
1030 | cu_name().c_str(), func_name.c_str(), function.c_str()) << endl; | |
440f755a | 1031 | |
7d007451 | 1032 | rc = (*callback)(& die, data); |
440f755a JS |
1033 | if (rc != DWARF_CB_OK) break; |
1034 | } | |
1035 | } | |
1036 | } | |
1ffb8bd1 | 1037 | else // not a linkage name or wildcard and no match in this CU |
440f755a JS |
1038 | { |
1039 | // do nothing | |
1040 | } | |
1041 | return rc; | |
1042 | } | |
1043 | ||
1044 | ||
7d007451 JL |
1045 | template<> int |
1046 | dwflpp::iterate_single_function<void>(int (*callback)(Dwarf_Die*, void*), | |
1047 | void *data, const string& function) | |
4df79aaf JS |
1048 | { |
1049 | int rc = DWARF_CB_OK; | |
1050 | assert (module); | |
1051 | ||
1052 | get_module_dwarf(false); | |
1053 | if (!module_dwarf) | |
1054 | return rc; | |
1055 | ||
1056 | cu_function_cache_t *v = mod_function_cache[module_dwarf]; | |
1057 | if (v == 0) | |
1058 | { | |
1059 | v = new cu_function_cache_t; | |
1060 | mod_function_cache[module_dwarf] = v; | |
337b7c44 | 1061 | iterate_over_cus (mod_function_caching_callback, v, false); |
4df79aaf | 1062 | if (sess.verbose > 4) |
f9bbd346 LB |
1063 | clog << _F("module function cache %s size %zu", module_name.c_str(), |
1064 | v->size()) << endl; | |
d1fa8b15 | 1065 | mod_info->update_symtab(v); |
4df79aaf JS |
1066 | } |
1067 | ||
1068 | cu_function_cache_t::iterator it; | |
1069 | cu_function_cache_range_t range = v->equal_range(function); | |
1070 | if (range.first != range.second) | |
1071 | { | |
1072 | for (it = range.first; it != range.second; ++it) | |
1073 | { | |
1074 | Dwarf_Die cu_mem; | |
1075 | Dwarf_Die& die = it->second; | |
1076 | if (sess.verbose > 4) | |
f9bbd346 LB |
1077 | clog << _F("module function cache %s hit %s", module_name.c_str(), |
1078 | function.c_str()) << endl; | |
4df79aaf JS |
1079 | |
1080 | // since we're iterating out of cu-context, we need each focus | |
1081 | focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL)); | |
1082 | ||
7d007451 | 1083 | rc = (*callback)(& die, data); |
4df79aaf JS |
1084 | if (rc != DWARF_CB_OK) break; |
1085 | } | |
1086 | } | |
1087 | ||
1088 | // undo the focus_on_cu | |
1089 | this->cu = NULL; | |
1090 | this->function_name.clear(); | |
1091 | this->function = NULL; | |
1092 | ||
1093 | return rc; | |
1094 | } | |
1095 | ||
1096 | ||
440f755a JS |
1097 | /* This basically only goes one level down from the compile unit so it |
1098 | * only picks up top level stuff (i.e. nothing in a lower scope) */ | |
1099 | int | |
063906a9 | 1100 | dwflpp::iterate_over_globals (Dwarf_Die *cu_die, |
3805a31e JS |
1101 | int (* callback)(Dwarf_Die *, bool, |
1102 | const string&, void *), | |
440f755a | 1103 | void * data) |
3805a31e JS |
1104 | { |
1105 | assert (cu_die); | |
337b7c44 | 1106 | assert (dwarf_tag(cu_die) == DW_TAG_compile_unit |
dee830d9 MW |
1107 | || dwarf_tag(cu_die) == DW_TAG_type_unit |
1108 | || dwarf_tag(cu_die) == DW_TAG_partial_unit); | |
1109 | ||
1110 | // Ignore partial_unit, if they get imported by a real unit, then | |
1111 | // iterate_over_types will traverse them. | |
1112 | if (dwarf_tag(cu_die) == DW_TAG_partial_unit) | |
1113 | return DWARF_CB_OK; | |
3805a31e JS |
1114 | |
1115 | // If this is C++, recurse for any inner types | |
1116 | bool has_inner_types = dwarf_srclang(cu_die) == DW_LANG_C_plus_plus; | |
1117 | ||
1118 | return iterate_over_types(cu_die, has_inner_types, "", callback, data); | |
1119 | } | |
1120 | ||
1121 | ||
1122 | int | |
1123 | dwflpp::iterate_over_types (Dwarf_Die *top_die, | |
1124 | bool has_inner_types, | |
1125 | const string& prefix, | |
1126 | int (* callback)(Dwarf_Die *, bool, | |
1127 | const string&, void *), | |
1128 | void * data) | |
440f755a JS |
1129 | { |
1130 | int rc = DWARF_CB_OK; | |
dee830d9 | 1131 | Dwarf_Die die, import; |
440f755a | 1132 | |
3805a31e | 1133 | assert (top_die); |
440f755a | 1134 | |
3805a31e | 1135 | if (dwarf_child(top_die, &die) != 0) |
440f755a JS |
1136 | return rc; |
1137 | ||
1138 | do | |
1139 | /* We're only currently looking for named types, | |
1140 | * although other types of declarations exist */ | |
1141 | switch (dwarf_tag(&die)) | |
1142 | { | |
1143 | case DW_TAG_base_type: | |
1144 | case DW_TAG_enumeration_type: | |
1145 | case DW_TAG_structure_type: | |
9c119951 | 1146 | case DW_TAG_class_type: |
440f755a JS |
1147 | case DW_TAG_typedef: |
1148 | case DW_TAG_union_type: | |
3805a31e JS |
1149 | case DW_TAG_namespace: |
1150 | rc = (*callback)(&die, has_inner_types, prefix, data); | |
440f755a | 1151 | break; |
dee830d9 MW |
1152 | |
1153 | case DW_TAG_imported_unit: | |
1154 | // Follow the imported_unit and iterate over its contents | |
1155 | // (either a partial_unit or a full compile_unit), all its | |
1156 | // children should be treated as if they appear in this place. | |
1157 | if (dwarf_attr_die(&die, DW_AT_import, &import)) | |
1158 | rc = iterate_over_types(&import, has_inner_types, prefix, | |
1159 | callback, data); | |
1160 | break; | |
440f755a JS |
1161 | } |
1162 | while (rc == DWARF_CB_OK && dwarf_siblingof(&die, &die) == 0); | |
1163 | ||
1164 | return rc; | |
1165 | } | |
1166 | ||
1167 | ||
fea74777 SC |
1168 | /* For each notes section in the current module call 'callback', use |
1169 | * 'data' for the notes buffer and pass 'object' back in case | |
1170 | * 'callback' is a method */ | |
1171 | ||
1172 | int | |
1173 | dwflpp::iterate_over_notes (void *object, void (*callback)(void *object, int type, const char *data, size_t len)) | |
1174 | { | |
1175 | Dwarf_Addr bias; | |
1189063f MW |
1176 | // Note we really want the actual elf file, not the dwarf .debug file. |
1177 | // Older binutils had a bug where they mangled the SHT_NOTE type during | |
1178 | // --keep-debug. | |
1179 | Elf* elf = dwfl_module_getelf (module, &bias); | |
fea74777 SC |
1180 | size_t shstrndx; |
1181 | if (elf_getshdrstrndx (elf, &shstrndx)) | |
1182 | return elf_errno(); | |
1183 | ||
fea74777 SC |
1184 | Elf_Scn *scn = NULL; |
1185 | ||
1186 | vector<Dwarf_Die> notes; | |
1187 | ||
1188 | while ((scn = elf_nextscn (elf, scn)) != NULL) | |
1189 | { | |
1190 | GElf_Shdr shdr; | |
1191 | if (gelf_getshdr (scn, &shdr) == NULL) | |
1192 | continue; | |
1193 | switch (shdr.sh_type) | |
1194 | { | |
1195 | case SHT_NOTE: | |
1196 | if (!(shdr.sh_flags & SHF_ALLOC)) | |
1197 | { | |
fea74777 SC |
1198 | Elf_Data *data = elf_getdata (scn, NULL); |
1199 | size_t next; | |
1200 | GElf_Nhdr nhdr; | |
1201 | size_t name_off; | |
1202 | size_t desc_off; | |
1203 | for (size_t offset = 0; | |
1204 | (next = gelf_getnote (data, offset, &nhdr, &name_off, &desc_off)) > 0; | |
1205 | offset = next) | |
1206 | (*callback) (object, nhdr.n_type, (const char*)((long)(data->d_buf) + (long)desc_off), nhdr.n_descsz); | |
1207 | } | |
1208 | break; | |
1209 | } | |
1210 | } | |
1211 | return 0; | |
1212 | } | |
1213 | ||
1214 | ||
5d5bd369 SC |
1215 | /* For each entry in the .dynamic section in the current module call 'callback' |
1216 | * returning 'object' in case 'callback' is a method */ | |
84c84ac4 SC |
1217 | |
1218 | void | |
6bbcd339 | 1219 | dwflpp::iterate_over_libraries (void (*callback)(void *object, const char *arg), void *q) |
84c84ac4 SC |
1220 | { |
1221 | std::set<std::string> added; | |
1222 | string interpreter; | |
1223 | ||
1224 | assert (this->module_name.length() != 0); | |
1225 | ||
1226 | Dwarf_Addr bias; | |
6bbcd339 SC |
1227 | // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias)) |
1228 | Elf *elf = dwfl_module_getelf (module, &bias); | |
c1e8b3af | 1229 | // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils |
e050d62f | 1230 | // needs libelf from elfutils 0.144+ |
c1e8b3af | 1231 | for (int i = 0; ; i++) |
84c84ac4 SC |
1232 | { |
1233 | GElf_Phdr mem; | |
c1e8b3af SC |
1234 | GElf_Phdr *phdr; |
1235 | phdr = gelf_getphdr (elf, i, &mem); | |
1236 | if (phdr == NULL) | |
1237 | break; | |
84c84ac4 SC |
1238 | if (phdr->p_type == PT_INTERP) |
1239 | { | |
1240 | size_t maxsize; | |
1241 | char *filedata = elf_rawfile (elf, &maxsize); | |
1242 | ||
1243 | if (filedata != NULL && phdr->p_offset < maxsize) | |
1244 | interpreter = (char*) (filedata + phdr->p_offset); | |
84c84ac4 SC |
1245 | break; |
1246 | } | |
1247 | } | |
1248 | ||
6bbcd339 SC |
1249 | if (interpreter.length() == 0) |
1250 | return; | |
58502ae4 JS |
1251 | // If it gets cumbersome to maintain this whitelist, we could just check for |
1252 | // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin | |
1253 | // would install untrustworthy loaders in those paths. | |
4ff68987 FCE |
1254 | // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD |
1255 | if (interpreter != "/lib/ld.so.1" // s390, ppc | |
1256 | && interpreter != "/lib/ld64.so.1" // s390x, ppc64 | |
1257 | && interpreter != "/lib64/ld64.so.1" | |
1258 | && interpreter != "/lib/ld-linux-ia64.so.2" // ia64 | |
1259 | && interpreter != "/emul/ia32-linux/lib/ld-linux.so.2" | |
1260 | && interpreter != "/lib64/ld-linux-x86-64.so.2" // x8664 | |
1261 | && interpreter != "/lib/ld-linux.so.2" // x86 | |
1262 | && interpreter != "/lib/ld-linux.so.3" // arm | |
1263 | && interpreter != "/lib/ld-linux-armhf.so.3" // arm | |
1264 | ) | |
d785c8d1 | 1265 | { |
2713ea24 | 1266 | sess.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s", |
d785c8d1 FCE |
1267 | module_name.c_str(), interpreter.c_str())); |
1268 | return; | |
1269 | } | |
84c84ac4 | 1270 | |
58502ae4 JS |
1271 | vector<string> ldd_command; |
1272 | ldd_command.push_back("/usr/bin/env"); | |
1273 | ldd_command.push_back("LD_TRACE_LOADED_OBJECTS=1"); | |
1274 | ldd_command.push_back("LD_WARN=yes"); | |
1275 | ldd_command.push_back("LD_BIND_NOW=yes"); | |
1276 | ldd_command.push_back(interpreter); | |
1277 | ldd_command.push_back(module_name); | |
1278 | ||
1279 | FILE *fp; | |
1280 | int child_fd; | |
1281 | pid_t child = stap_spawn_piped(sess.verbose, ldd_command, NULL, &child_fd); | |
1282 | if (child <= 0 || !(fp = fdopen(child_fd, "r"))) | |
1283 | clog << _F("library iteration on %s failed: %s", | |
1284 | module_name.c_str(), strerror(errno)) << endl; | |
84c84ac4 SC |
1285 | else |
1286 | { | |
1287 | while (1) // this parsing loop borrowed from add_unwindsym_ldd | |
1288 | { | |
1289 | char linebuf[256]; | |
1290 | char *soname = 0; | |
1291 | char *shlib = 0; | |
1292 | unsigned long int addr = 0; | |
1293 | ||
1294 | char *line = fgets (linebuf, 256, fp); | |
1295 | if (line == 0) break; // EOF or error | |
1296 | ||
62a4021d FCE |
1297 | #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7) |
1298 | #define MS_FMT "%ms" | |
1299 | #else | |
1300 | #define MS_FMT "%as" | |
1301 | #endif | |
84c84ac4 | 1302 | // Try soname => shlib (0xaddr) |
62a4021d | 1303 | int nf = sscanf (line, MS_FMT " => " MS_FMT " (0x%lx)", |
84c84ac4 SC |
1304 | &soname, &shlib, &addr); |
1305 | if (nf != 3 || shlib[0] != '/') | |
1306 | { | |
1307 | // Try shlib (0xaddr) | |
62a4021d | 1308 | nf = sscanf (line, " " MS_FMT " (0x%lx)", &shlib, &addr); |
84c84ac4 SC |
1309 | if (nf != 2 || shlib[0] != '/') |
1310 | continue; // fewer than expected fields, or bad shlib. | |
1311 | } | |
1312 | ||
1313 | if (added.find (shlib) == added.end()) | |
1314 | { | |
1315 | if (sess.verbose > 2) | |
1316 | { | |
1317 | clog << _F("Added -d '%s", shlib); | |
1318 | if (nf == 3) | |
1319 | clog << _F("' due to '%s'", soname); | |
1320 | else | |
1321 | clog << "'"; | |
1322 | clog << endl; | |
1323 | } | |
1324 | added.insert (shlib); | |
1325 | } | |
1326 | ||
1327 | free (soname); | |
1328 | free (shlib); | |
1329 | } | |
2713ea24 CM |
1330 | if ((fclose(fp) || stap_waitpid(sess.verbose, child))) |
1331 | sess.print_warning("failed to read libraries from " + module_name + ": " + strerror(errno)); | |
84c84ac4 SC |
1332 | } |
1333 | ||
1334 | for (std::set<std::string>::iterator it = added.begin(); | |
1335 | it != added.end(); | |
1336 | it++) | |
1337 | { | |
1338 | string modname = *it; | |
5d5bd369 | 1339 | (callback) (q, modname.c_str()); |
84c84ac4 SC |
1340 | } |
1341 | } | |
1342 | ||
1343 | ||
576eaefe SC |
1344 | /* For each plt section in the current module call 'callback', pass the plt entry |
1345 | * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */ | |
1346 | ||
1347 | int | |
1348 | dwflpp::iterate_over_plt (void *object, void (*callback)(void *object, const char *name, size_t addr)) | |
1349 | { | |
1350 | Dwarf_Addr load_addr; | |
1351 | // Note we really want the actual elf file, not the dwarf .debug file. | |
1352 | Elf* elf = dwfl_module_getelf (module, &load_addr); | |
1353 | size_t shstrndx; | |
1354 | assert (elf_getshdrstrndx (elf, &shstrndx) >= 0); | |
1355 | ||
1356 | // Get the load address | |
1357 | for (int i = 0; ; i++) | |
1358 | { | |
1359 | GElf_Phdr mem; | |
1360 | GElf_Phdr *phdr; | |
1361 | phdr = gelf_getphdr (elf, i, &mem); | |
1362 | if (phdr == NULL) | |
1363 | break; | |
1364 | if (phdr->p_type == PT_LOAD) | |
1365 | { | |
1366 | load_addr = phdr->p_vaddr; | |
1367 | break; | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | // Get the plt section header | |
1372 | Elf_Scn *scn = NULL; | |
1373 | GElf_Shdr *plt_shdr = NULL; | |
1374 | GElf_Shdr plt_shdr_mem; | |
1375 | while ((scn = elf_nextscn (elf, scn))) | |
1376 | { | |
1acfc030 JS |
1377 | GElf_Shdr *shdr = gelf_getshdr (scn, &plt_shdr_mem); |
1378 | assert (shdr != NULL); | |
1379 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".plt") == 0) | |
1380 | { | |
1381 | plt_shdr = shdr; | |
1382 | break; | |
1383 | } | |
576eaefe | 1384 | } |
1acfc030 JS |
1385 | if (plt_shdr == NULL) |
1386 | return 0; | |
1387 | ||
576eaefe SC |
1388 | // Layout of the plt section |
1389 | int plt0_entry_size; | |
1390 | int plt_entry_size; | |
1391 | GElf_Ehdr ehdr_mem; | |
1392 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
1393 | switch (em->e_machine) | |
1394 | { | |
4e309f0b | 1395 | case EM_386: plt0_entry_size = 16; plt_entry_size = 16; break; |
576eaefe | 1396 | case EM_X86_64: plt0_entry_size = 16; plt_entry_size = 16; break; |
13b528f4 | 1397 | case EM_ARM: plt0_entry_size = 20; plt_entry_size = 12; break; |
576eaefe SC |
1398 | case EM_PPC64: |
1399 | case EM_S390: | |
1400 | case EM_PPC: | |
1401 | default: | |
dc09353a | 1402 | throw SEMANTIC_ERROR(".plt is not supported on this architecture"); |
576eaefe SC |
1403 | } |
1404 | ||
1405 | scn = NULL; | |
1406 | while ((scn = elf_nextscn (elf, scn))) | |
1407 | { | |
1408 | GElf_Shdr shdr_mem; | |
1409 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); | |
e245d57f SC |
1410 | bool have_rela = false; |
1411 | bool have_rel = false; | |
4e309f0b | 1412 | |
576eaefe SC |
1413 | if (shdr == NULL) |
1414 | continue; | |
1415 | assert (shdr != NULL); | |
1416 | ||
4e309f0b SC |
1417 | if ((have_rela = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rela.plt") == 0)) |
1418 | || (have_rel = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rel.plt") == 0))) | |
576eaefe SC |
1419 | { |
1420 | /* Get the data of the section. */ | |
1421 | Elf_Data *data = elf_getdata (scn, NULL); | |
1422 | assert (data != NULL); | |
1423 | /* Get the symbol table information. */ | |
1424 | Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link); | |
1425 | GElf_Shdr symshdr_mem; | |
1426 | GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); | |
1427 | assert (symshdr != NULL); | |
1428 | Elf_Data *symdata = elf_getdata (symscn, NULL); | |
1429 | assert (symdata != NULL); | |
1430 | ||
1431 | unsigned int nsyms = shdr->sh_size / shdr->sh_entsize; | |
1432 | ||
1433 | for (unsigned int cnt = 0; cnt < nsyms; ++cnt) | |
1434 | { | |
1435 | GElf_Ehdr ehdr_mem; | |
1436 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
1437 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
1438 | ||
4e309f0b | 1439 | GElf_Rela relamem; |
ffffb6b5 | 1440 | GElf_Rela *rela = NULL; |
4e309f0b | 1441 | GElf_Rel relmem; |
ffffb6b5 | 1442 | GElf_Rel *rel = NULL; |
4e309f0b SC |
1443 | if (have_rela) |
1444 | { | |
1445 | rela = gelf_getrela (data, cnt, &relamem); | |
1446 | assert (rela != NULL); | |
1447 | } | |
1448 | else if (have_rel) | |
1449 | { | |
1450 | rel = gelf_getrel (data, cnt, &relmem); | |
1451 | assert (rel != NULL); | |
1452 | } | |
576eaefe SC |
1453 | GElf_Sym symmem; |
1454 | Elf32_Word xndx; | |
1455 | Elf_Data *xndxdata = NULL; | |
4e309f0b SC |
1456 | GElf_Sym *sym = |
1457 | gelf_getsymshndx (symdata, xndxdata, | |
1458 | GELF_R_SYM (have_rela ? rela->r_info : rel->r_info), | |
1459 | &symmem, &xndx); | |
576eaefe SC |
1460 | assert (sym != NULL); |
1461 | Dwarf_Addr addr = plt_shdr->sh_offset + plt0_entry_size + cnt * plt_entry_size; | |
1462 | ||
1463 | if (elf_strptr (elf, symshdr->sh_link, sym->st_name)) | |
1464 | (*callback) (object, elf_strptr (elf, symshdr->sh_link, sym->st_name), addr + load_addr); | |
1465 | } | |
1466 | break; // while scn | |
1467 | } | |
1468 | } | |
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | ||
440f755a JS |
1473 | // This little test routine represents an unfortunate breakdown in |
1474 | // abstraction between dwflpp (putatively, a layer right on top of | |
1475 | // elfutils), and dwarf_query (interpreting a systemtap probe point). | |
1476 | // It arises because we sometimes try to fix up slightly-off | |
1477 | // .statement() probes (something we find out in fairly low-level). | |
1478 | // | |
1479 | // An alternative would be to put some more intelligence into query_cu(), | |
1480 | // and have it print additional suggestions after finding that | |
1481 | // q->dw.iterate_over_srcfile_lines resulted in no new finished_results. | |
1482 | ||
1483 | bool | |
1484 | dwflpp::has_single_line_record (dwarf_query * q, char const * srcfile, int lineno) | |
1485 | { | |
1486 | if (lineno < 0) | |
1487 | return false; | |
1488 | ||
35c68acb SC |
1489 | Dwarf_Line **srcsp = NULL; |
1490 | size_t nsrcs = 0; | |
440f755a | 1491 | |
35c68acb SC |
1492 | dwarf_assert ("dwarf_getsrc_file", |
1493 | dwarf_getsrc_file (module_dwarf, | |
1494 | srcfile, lineno, 0, | |
1495 | &srcsp, &nsrcs)); | |
440f755a | 1496 | |
35c68acb SC |
1497 | if (nsrcs != 1) |
1498 | { | |
1499 | if (sess.verbose>4) | |
1500 | clog << _F("alternative line %d rejected: nsrcs=%zu", lineno, nsrcs) << endl; | |
1501 | return false; | |
1502 | } | |
440f755a | 1503 | |
35c68acb SC |
1504 | // We also try to filter out lines that leave the selected |
1505 | // functions (if any). | |
440f755a | 1506 | |
35c68acb SC |
1507 | dwarf_line_t line(srcsp[0]); |
1508 | Dwarf_Addr addr = line.addr(); | |
440f755a | 1509 | |
35c68acb SC |
1510 | func_info_map_t *filtered_functions = get_filtered_functions(q); |
1511 | for (func_info_map_t::iterator i = filtered_functions->begin(); | |
1512 | i != filtered_functions->end(); ++i) | |
1513 | { | |
1514 | if (die_has_pc (i->die, addr)) | |
1515 | { | |
1516 | if (sess.verbose>4) | |
1517 | clog << _F("alternative line %d accepted: fn=%s", lineno, i->name.c_str()) << endl; | |
1518 | return true; | |
1519 | } | |
1520 | } | |
440f755a | 1521 | |
35c68acb SC |
1522 | inline_instance_map_t *filtered_inlines = get_filtered_inlines(q); |
1523 | for (inline_instance_map_t::iterator i = filtered_inlines->begin(); | |
1524 | i != filtered_inlines->end(); ++i) | |
1525 | { | |
1526 | if (die_has_pc (i->die, addr)) | |
1527 | { | |
1528 | if (sess.verbose>4) | |
1529 | clog << _F("alternative line %d accepted: ifn=%s", lineno, i->name.c_str()) << endl; | |
1530 | return true; | |
1531 | } | |
1532 | } | |
440f755a | 1533 | |
35c68acb SC |
1534 | if (sess.verbose>4) |
1535 | //TRANSLATORS: given line number leaves (is beyond) given function. | |
1536 | clog << _F("alternative line %d rejected: leaves selected fns", lineno) << endl; | |
1537 | return false; | |
440f755a JS |
1538 | } |
1539 | ||
1540 | ||
1541 | void | |
1542 | dwflpp::iterate_over_srcfile_lines (char const * srcfile, | |
1543 | int lines[2], | |
1544 | bool need_single_match, | |
1545 | enum line_t line_type, | |
1546 | void (* callback) (const dwarf_line_t& line, | |
1547 | void * arg), | |
9b988eff | 1548 | const std::string& func_pattern, |
440f755a JS |
1549 | void *data) |
1550 | { | |
1551 | Dwarf_Line **srcsp = NULL; | |
1552 | size_t nsrcs = 0; | |
1553 | dwarf_query * q = static_cast<dwarf_query *>(data); | |
1554 | int lineno = lines[0]; | |
1555 | auto_free_ref<Dwarf_Line**> free_srcsp(srcsp); | |
1556 | ||
1557 | get_module_dwarf(); | |
e679283a WH |
1558 | if (!this->function) |
1559 | return; | |
440f755a JS |
1560 | |
1561 | if (line_type == RELATIVE) | |
1562 | { | |
1563 | Dwarf_Addr addr; | |
1564 | Dwarf_Line *line; | |
1565 | int line_number; | |
1566 | ||
1bbf3f90 JS |
1567 | if (!die_entrypc(this->function, &addr)) |
1568 | return; | |
6a3a5e98 SC |
1569 | |
1570 | if (addr != 0) | |
1571 | { | |
1572 | line = dwarf_getsrc_die (this->cu, addr); | |
1573 | dwarf_assert ("dwarf_getsrc_die", line == NULL); | |
1574 | dwarf_assert ("dwarf_lineno", dwarf_lineno (line, &line_number)); | |
1575 | } | |
1576 | else if (dwarf_decl_line (this->function, &line_number) != 0) | |
1577 | { | |
1578 | // use DW_AT_decl_line as a fallback method | |
1579 | Dwarf_Attribute type_attr; | |
1580 | Dwarf_Word constant; | |
1581 | if (dwarf_attr_integrate (this->function, DW_AT_decl_line, &type_attr)) | |
1582 | { | |
1583 | dwarf_formudata (&type_attr, &constant); | |
1584 | line_number = constant; | |
1585 | } | |
1586 | else | |
1587 | return; | |
1588 | } | |
440f755a JS |
1589 | lineno += line_number; |
1590 | } | |
78e2a671 DT |
1591 | else if (line_type == WILDCARD) { |
1592 | if (name_has_wildcard(func_pattern)) /* PR14774: look at whole file if function name is wildcard */ | |
1593 | lineno = 0; | |
1594 | else | |
1595 | function_line (&lineno); | |
1596 | } | |
1123a74a WH |
1597 | else if (line_type == RANGE) { /* correct lineno */ |
1598 | int start_lineno; | |
1599 | ||
9b988eff WH |
1600 | if (name_has_wildcard(func_pattern)) /* PR10294: wider range like statement("*@foo.c") */ |
1601 | start_lineno = lineno; | |
1602 | else | |
1603 | function_line (&start_lineno); | |
1123a74a WH |
1604 | lineno = lineno < start_lineno ? start_lineno : lineno; |
1605 | if (lineno > lines[1]) { /* invalid line range */ | |
1606 | stringstream advice; | |
f9bbd346 | 1607 | advice << _("Invalid line range (") << lines[0] << "-" << lines[1] << ")"; |
1123a74a | 1608 | if (start_lineno > lines[1]) |
f9bbd346 | 1609 | advice << _(", the end line number ") << lines[1] << " < " << start_lineno; |
dc09353a | 1610 | throw SEMANTIC_ERROR (advice.str()); |
1123a74a WH |
1611 | } |
1612 | } | |
1613 | ||
440f755a JS |
1614 | |
1615 | for (int l = lineno; ; l = l + 1) | |
1616 | { | |
1617 | set<int> lines_probed; | |
1618 | pair<set<int>::iterator,bool> line_probed; | |
1123a74a WH |
1619 | int ret = 0; |
1620 | ||
e19ebcf7 | 1621 | assert_no_interrupts(); |
b8c632dc | 1622 | |
35c68acb | 1623 | nsrcs = 0; |
1123a74a WH |
1624 | ret = dwarf_getsrc_file (module_dwarf, srcfile, l, 0, |
1625 | &srcsp, &nsrcs); | |
8aaef2fb JS |
1626 | if (ret != 0) /* tolerate invalid line number */ |
1627 | break; | |
1123a74a | 1628 | |
440f755a JS |
1629 | if (line_type == WILDCARD || line_type == RANGE) |
1630 | { | |
1631 | Dwarf_Addr line_addr; | |
1123a74a | 1632 | |
440f755a | 1633 | dwarf_lineno (srcsp [0], &lineno); |
1123a74a WH |
1634 | /* Maybe lineno will exceed the input end */ |
1635 | if (line_type == RANGE && lineno > lines[1]) | |
1636 | break; | |
440f755a JS |
1637 | line_probed = lines_probed.insert(lineno); |
1638 | if (lineno != l || line_probed.second == false || nsrcs > 1) | |
1639 | continue; | |
1640 | dwarf_lineaddr (srcsp [0], &line_addr); | |
9b988eff | 1641 | if (!function_name_matches(func_pattern) && dwarf_haspc (function, line_addr) != 1) |
440f755a JS |
1642 | break; |
1643 | } | |
1644 | ||
1645 | // NB: Formerly, we used to filter, because: | |
1646 | ||
1647 | // dwarf_getsrc_file gets one *near hits* for line numbers, not | |
1648 | // exact matches. For example, an existing file but a nonexistent | |
1649 | // line number will be rounded up to the next definition in that | |
1650 | // file. This may be similar to the GDB breakpoint algorithm, but | |
1651 | // we don't want to be so fuzzy in systemtap land. So we filter. | |
1652 | ||
1653 | // But we now see the error of our ways, and skip this filtering. | |
1654 | ||
1655 | // XXX: the code also fails to match e.g. inline function | |
1656 | // definitions when the srcfile is a header file rather than the | |
1657 | // CU name. | |
1658 | ||
1659 | size_t remaining_nsrcs = nsrcs; | |
1660 | ||
1661 | if (need_single_match && remaining_nsrcs > 1) | |
1662 | { | |
1663 | // We wanted a single line record (a unique address for the | |
1664 | // line) and we got a bunch of line records. We're going to | |
1665 | // skip this probe (throw an exception) but before we throw | |
1666 | // we're going to look around a bit to see if there's a low or | |
1667 | // high line number nearby which *doesn't* have this problem, | |
1668 | // so we can give the user some advice. | |
1669 | ||
1670 | int lo_try = -1; | |
1671 | int hi_try = -1; | |
1672 | for (size_t i = 1; i < 6; ++i) | |
1673 | { | |
1674 | if (lo_try == -1 && has_single_line_record(q, srcfile, lineno - i)) | |
1675 | lo_try = lineno - i; | |
1676 | ||
1677 | if (hi_try == -1 && has_single_line_record(q, srcfile, lineno + i)) | |
1678 | hi_try = lineno + i; | |
1679 | } | |
1680 | ||
1681 | stringstream advice; | |
4c5d9906 | 1682 | advice << _F("multiple addresses for %s:%d [man error::dwarf]", srcfile, lineno); |
440f755a JS |
1683 | if (lo_try > 0 || hi_try > 0) |
1684 | { | |
f9bbd346 LB |
1685 | //TRANSLATORS: Here we are trying to advise what source file |
1686 | //TRANSLATORS: to attempt. | |
1687 | advice << _(" (try "); | |
440f755a JS |
1688 | if (lo_try > 0) |
1689 | advice << srcfile << ":" << lo_try; | |
1690 | if (lo_try > 0 && hi_try > 0) | |
f9bbd346 | 1691 | advice << _(" or "); |
440f755a JS |
1692 | if (hi_try > 0) |
1693 | advice << srcfile << ":" << hi_try; | |
1694 | advice << ")"; | |
1695 | } | |
dc09353a | 1696 | throw SEMANTIC_ERROR (advice.str()); |
440f755a JS |
1697 | } |
1698 | ||
1699 | for (size_t i = 0; i < nsrcs; ++i) | |
1700 | { | |
e19ebcf7 | 1701 | assert_no_interrupts(); |
440f755a JS |
1702 | if (srcsp [i]) // skip over mismatched lines |
1703 | callback (dwarf_line_t(srcsp[i]), data); | |
1704 | } | |
1705 | ||
1706 | if (line_type == ABSOLUTE || line_type == RELATIVE) | |
1707 | break; | |
1708 | else if (line_type == RANGE && l == lines[1]) | |
1709 | break; | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | ||
1714 | void | |
1715 | dwflpp::iterate_over_labels (Dwarf_Die *begin_die, | |
8096dd7d | 1716 | const string& sym, |
f09d0d1e | 1717 | const string& function, |
8096dd7d | 1718 | dwarf_query *q, |
440f755a | 1719 | void (* callback)(const string &, |
8096dd7d | 1720 | const char *, |
440f755a JS |
1721 | const char *, |
1722 | int, | |
1723 | Dwarf_Die *, | |
1724 | Dwarf_Addr, | |
f09d0d1e | 1725 | dwarf_query *)) |
440f755a | 1726 | { |
440f755a JS |
1727 | get_module_dwarf(); |
1728 | ||
dee830d9 | 1729 | Dwarf_Die die, import; |
f09d0d1e | 1730 | const char *name; |
440f755a JS |
1731 | int res = dwarf_child (begin_die, &die); |
1732 | if (res != 0) | |
1733 | return; // die without children, bail out. | |
1734 | ||
440f755a JS |
1735 | do |
1736 | { | |
f09d0d1e | 1737 | switch (dwarf_tag(&die)) |
440f755a JS |
1738 | { |
1739 | case DW_TAG_label: | |
f09d0d1e JS |
1740 | name = dwarf_diename (&die); |
1741 | if (name && | |
8096dd7d JS |
1742 | (name == sym |
1743 | || (name_has_wildcard(sym) | |
1744 | && function_name_matches_pattern (name, sym)))) | |
1745 | { | |
8096dd7d JS |
1746 | // Don't try to be smart. Just drop no addr labels. |
1747 | Dwarf_Addr stmt_addr; | |
1748 | if (dwarf_lowpc (&die, &stmt_addr) == 0) | |
1749 | { | |
f09d0d1e JS |
1750 | // Get the file/line number for this label |
1751 | int dline; | |
c60517ca | 1752 | const char *file = dwarf_decl_file (&die) ?: "<unknown source>"; |
f09d0d1e JS |
1753 | dwarf_decl_line (&die, &dline); |
1754 | ||
9aa8ffce JS |
1755 | vector<Dwarf_Die> scopes = getscopes_die(&die); |
1756 | if (scopes.size() > 1) | |
f5958c8f JS |
1757 | { |
1758 | Dwarf_Die scope; | |
2713ea24 | 1759 | if (!inner_die_containing_pc(scopes[1], stmt_addr, scope)) |
2a43d5db | 1760 | { |
7e456f65 LB |
1761 | sess.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not " |
1762 | "contained by its scope '%s' (dieoffset: %s) -- bad" | |
4c5d9906 | 1763 | " debuginfo? [man error::dwarf]", name, lex_cast_hex(stmt_addr).c_str(), |
7e456f65 LB |
1764 | lex_cast_hex(dwarf_dieoffset(&die)).c_str(), |
1765 | (dwarf_diename(&scope) ?: "<unknown>"), | |
1766 | lex_cast_hex(dwarf_dieoffset(&scope)).c_str())); | |
2a43d5db | 1767 | } |
f5958c8f JS |
1768 | callback(function, name, file, dline, |
1769 | &scope, stmt_addr, q); | |
1770 | } | |
8096dd7d JS |
1771 | } |
1772 | } | |
440f755a | 1773 | break; |
8096dd7d | 1774 | |
440f755a | 1775 | case DW_TAG_subprogram: |
30868aad | 1776 | case DW_TAG_inlined_subroutine: |
f09d0d1e JS |
1777 | // Stay within our filtered function |
1778 | break; | |
1779 | ||
dee830d9 MW |
1780 | case DW_TAG_imported_unit: |
1781 | // Iterate over the children of the imported unit as if they | |
1782 | // were inserted in place. | |
1783 | if (dwarf_attr_die(&die, DW_AT_import, &import)) | |
1784 | iterate_over_labels (&import, sym, function, q, callback); | |
1785 | break; | |
1786 | ||
440f755a JS |
1787 | default: |
1788 | if (dwarf_haschildren (&die)) | |
f09d0d1e | 1789 | iterate_over_labels (&die, sym, function, q, callback); |
8096dd7d | 1790 | break; |
440f755a JS |
1791 | } |
1792 | } | |
1793 | while (dwarf_siblingof (&die, &die) == 0); | |
1794 | } | |
1795 | ||
67959c62 JL |
1796 | // Mini 'query-like' struct to help us navigate callbacks during |
1797 | // external function resolution | |
1798 | struct external_function_query { | |
1799 | dwflpp* dw; | |
1800 | const char* name; | |
1801 | Dwarf_Die die; | |
1802 | Dwarf_Addr addr; | |
1803 | bool resolved; | |
1804 | external_function_query(dwflpp* dw, const char* name): | |
1805 | dw(dw), name(name), resolved(false) {} | |
1806 | }; | |
1807 | ||
1808 | int | |
5c378838 | 1809 | dwflpp::external_function_cu_callback (Dwarf_Die* cu, external_function_query *efq) |
67959c62 | 1810 | { |
67959c62 JL |
1811 | efq->dw->focus_on_cu(cu); |
1812 | return efq->dw->iterate_over_functions(external_function_func_callback, | |
1813 | efq, efq->name); | |
1814 | } | |
1815 | ||
1816 | int | |
7d007451 | 1817 | dwflpp::external_function_func_callback (Dwarf_Die* func, external_function_query *efq) |
67959c62 | 1818 | { |
67959c62 JL |
1819 | Dwarf_Attribute external; |
1820 | Dwarf_Addr func_addr; | |
1821 | if (dwarf_attr_integrate(func, DW_AT_external, &external) != NULL && | |
1822 | dwarf_lowpc(func, &func_addr) == 0) | |
1823 | { | |
1824 | efq->die = *func; | |
1825 | efq->addr = func_addr; | |
1826 | efq->resolved = true; | |
1827 | return DWARF_CB_ABORT; // we found it so stop here | |
1828 | } | |
1829 | return DWARF_CB_OK; | |
1830 | } | |
1831 | ||
1832 | void | |
1833 | dwflpp::iterate_over_callees (Dwarf_Die *begin_die, | |
1834 | const string& sym, | |
1835 | long recursion_depth, | |
1836 | dwarf_query *q, | |
1837 | void (* callback)(const char *, | |
1838 | const char *, | |
1839 | int, | |
1840 | Dwarf_Die *, | |
1841 | Dwarf_Addr, | |
1842 | stack<Dwarf_Addr>*, | |
1843 | dwarf_query *), | |
1844 | stack<Dwarf_Addr> *callers) | |
1845 | { | |
1846 | get_module_dwarf(); | |
1847 | ||
1848 | Dwarf_Die die, import; | |
1849 | ||
1850 | // DIE of abstract_origin found in die | |
1851 | Dwarf_Die origin; | |
1852 | ||
1853 | // callee's entry pc (= where we'll probe) | |
1854 | Dwarf_Addr func_addr; | |
1855 | ||
1856 | // caller's unwind pc during call (to match against bt for filtering) | |
1857 | Dwarf_Addr caller_uw_addr; | |
1858 | ||
1859 | Dwarf_Attribute attr; | |
1860 | ||
1861 | int dline; | |
1862 | const char *name, *file; | |
1863 | if (dwarf_child(begin_die, &die) != 0) | |
1864 | return; // die without children, bail out. | |
1865 | ||
1866 | bool free_callers = false; | |
1867 | if (callers == NULL) /* first call */ | |
1868 | { | |
1869 | callers = new stack<Dwarf_Addr>(); | |
1870 | free_callers = true; | |
1871 | } | |
1872 | ||
1873 | do | |
1874 | { | |
1875 | bool inlined = false; | |
1876 | switch (dwarf_tag(&die)) | |
1877 | { | |
1878 | case DW_TAG_inlined_subroutine: | |
1879 | inlined = true; | |
1880 | case DW_TAG_GNU_call_site: | |
1881 | name = dwarf_diename (&die); | |
1882 | if (!name) | |
1883 | continue; | |
1884 | if (name != sym) | |
1885 | { | |
1886 | if (!name_has_wildcard(sym)) | |
1887 | continue; | |
1888 | if (!function_name_matches_pattern(name, sym)) | |
1889 | continue; | |
1890 | } | |
1891 | ||
1892 | /* In both cases (call sites and inlines), we want the | |
1893 | * abstract_origin. The difference is that in inlines, the addr is | |
1894 | * in the die itself, whereas for call sites, the addr is in the | |
1895 | * abstract_origin's die. | |
1896 | * Note that in the case of inlines, we're only calling back | |
1897 | * for that inline instance, not all. This is what we want, since | |
1898 | * it will only be triggered when 'called' from the target func, | |
1899 | * which is something we have to emulate for non-inlined funcs | |
1900 | * (which is the purpose of the caller_uw_addr below) */ | |
1901 | if (dwarf_attr_die(&die, DW_AT_abstract_origin, &origin) == NULL) | |
1902 | continue; | |
1903 | ||
1904 | // the low_pc of the die in either cases is the pc that would | |
1905 | // show up in a backtrace (inlines are a special case in which | |
1906 | // the die's low_pc is also the abstract_origin's low_pc = the | |
1907 | // 'start' of the inline instance) | |
1908 | if (dwarf_lowpc(&die, &caller_uw_addr) != 0) | |
1909 | continue; | |
1910 | ||
1911 | if (inlined) | |
1912 | func_addr = caller_uw_addr; | |
1913 | else if (dwarf_lowpc(&origin, &func_addr) != 0) | |
1914 | { | |
1915 | // function doesn't have a low_pc, is it external? | |
1916 | if (dwarf_attr_integrate(&origin, DW_AT_external, | |
1917 | &attr) != NULL) | |
1918 | { | |
1919 | // let's iterate over the CUs and find it. NB: it's | |
1920 | // possible we could have also done this by creating a | |
1921 | // probe point with .exported tacked on and rerunning it | |
1922 | // through derive_probe(). But since we're already on the | |
1923 | // dwflpp side of things, and we already have access to | |
1924 | // everything we need, let's try to be self-sufficient. | |
1925 | ||
1926 | // remember old focus | |
1927 | Dwarf_Die *old_cu = cu; | |
1928 | ||
1929 | external_function_query efq(this, dwarf_linkage_name(&origin) ?: name); | |
1930 | iterate_over_cus(external_function_cu_callback, &efq, false); | |
1931 | ||
1932 | // restore focus | |
1933 | cu = old_cu; | |
1934 | ||
1935 | if (!efq.resolved) // did we resolve it? | |
1936 | continue; | |
1937 | ||
1938 | func_addr = efq.addr; | |
1939 | origin = efq.die; | |
1940 | } | |
1941 | // non-external function without low_pc, jump ship | |
1942 | else continue; | |
1943 | } | |
1944 | ||
1945 | // We now have the addr to probe in func_addr, and the DIE | |
1946 | // from which to obtain file/line info in origin | |
1947 | ||
1948 | // Get the file/line number for this callee | |
1949 | file = dwarf_decl_file (&origin) ?: "<unknown source>"; | |
1950 | dwarf_decl_line (&origin, &dline); | |
1951 | ||
1952 | // add as a caller to match against | |
1953 | if (!inlined) | |
1954 | callers->push(caller_uw_addr); | |
1955 | ||
1956 | callback(name, file, dline, inlined ? &die : &origin, | |
1957 | func_addr, callers, q); | |
1958 | ||
1959 | // If it's a tail call, print a warning that it may not be caught | |
1960 | if (!inlined | |
1961 | && dwarf_attr_integrate(&die, DW_AT_GNU_tail_call, &attr) != NULL) | |
1962 | sess.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: " | |
1963 | ".callee probe may not fire. Try placing the probe " | |
1964 | "directly on the callee function instead.", name, | |
1965 | dwarf_diename(begin_die))); | |
1966 | ||
1967 | if (recursion_depth > 1) // .callees(N) | |
1968 | iterate_over_callees(inlined ? &die : &origin, | |
1969 | sym, recursion_depth-1, q, | |
1970 | callback, callers); | |
1971 | ||
1972 | if (!inlined) | |
1973 | callers->pop(); | |
1974 | break; | |
1975 | ||
1976 | case DW_TAG_subprogram: | |
1977 | break; // don't leave our filtered func | |
1978 | ||
1979 | case DW_TAG_imported_unit: | |
1980 | // Iterate over the children of the imported unit as if they | |
1981 | // were inserted in place. | |
1982 | if (dwarf_attr_die(&die, DW_AT_import, &import)) | |
1983 | iterate_over_callees (&import, sym, recursion_depth, q, callback, callers); | |
1984 | break; | |
1985 | ||
1986 | default: | |
1987 | if (dwarf_haschildren (&die)) | |
1988 | iterate_over_callees (&die, sym, recursion_depth, q, callback, callers); | |
1989 | break; | |
1990 | } | |
1991 | } | |
1992 | while (dwarf_siblingof (&die, &die) == 0); | |
1993 | ||
1994 | if (free_callers && callers != NULL) | |
1995 | delete callers; | |
1996 | } | |
1997 | ||
440f755a JS |
1998 | |
1999 | void | |
2000 | dwflpp::collect_srcfiles_matching (string const & pattern, | |
bd25380d | 2001 | set<string> & filtered_srcfiles) |
440f755a JS |
2002 | { |
2003 | assert (module); | |
2004 | assert (cu); | |
2005 | ||
2006 | size_t nfiles; | |
2007 | Dwarf_Files *srcfiles; | |
2008 | ||
2009 | // PR 5049: implicit * in front of given path pattern. | |
2010 | // NB: fnmatch() is used without FNM_PATHNAME. | |
2011 | string prefixed_pattern = string("*/") + pattern; | |
2012 | ||
2013 | dwarf_assert ("dwarf_getsrcfiles", | |
2014 | dwarf_getsrcfiles (cu, &srcfiles, &nfiles)); | |
2015 | { | |
2016 | for (size_t i = 0; i < nfiles; ++i) | |
2017 | { | |
2018 | char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL); | |
2019 | if (fnmatch (pattern.c_str(), fname, 0) == 0 || | |
2020 | fnmatch (prefixed_pattern.c_str(), fname, 0) == 0) | |
2021 | { | |
2022 | filtered_srcfiles.insert (fname); | |
2023 | if (sess.verbose>2) | |
f9bbd346 | 2024 | clog << _F("selected source file '%s'\n", fname); |
440f755a JS |
2025 | } |
2026 | } | |
2027 | } | |
2028 | } | |
2029 | ||
2030 | ||
2031 | void | |
2032 | dwflpp::resolve_prologue_endings (func_info_map_t & funcs) | |
2033 | { | |
2034 | // This heuristic attempts to pick the first address that has a | |
2035 | // source line distinct from the function declaration's. In a | |
2036 | // perfect world, this would be the first statement *past* the | |
2037 | // prologue. | |
2038 | ||
2039 | assert(module); | |
2040 | assert(cu); | |
2041 | ||
2042 | size_t nlines = 0; | |
2043 | Dwarf_Lines *lines = NULL; | |
2044 | ||
2045 | /* trouble cases: | |
2046 | malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue | |
2047 | sys_get?id in kernel/timer.c no-prologue | |
2048 | sys_exit_group tail-recursive | |
2049 | {do_,}sys_open extra-long-prologue (gcc 3.4) | |
2050 | cpu_to_logical_apicid NULL-decl_file | |
2051 | */ | |
2052 | ||
2053 | // Fetch all srcline records, sorted by address. | |
2054 | dwarf_assert ("dwarf_getsrclines", | |
2055 | dwarf_getsrclines(cu, &lines, &nlines)); | |
2056 | // XXX: free lines[] later, but how? | |
2057 | ||
2058 | for(func_info_map_t::iterator it = funcs.begin(); it != funcs.end(); it++) | |
2059 | { | |
2060 | #if 0 /* someday */ | |
2061 | Dwarf_Addr* bkpts = 0; | |
2062 | int n = dwarf_entry_breakpoints (& it->die, & bkpts); | |
2063 | // ... | |
2064 | free (bkpts); | |
2065 | #endif | |
2066 | ||
2067 | Dwarf_Addr entrypc = it->entrypc; | |
2068 | Dwarf_Addr highpc; // NB: highpc is exclusive: [entrypc,highpc) | |
2069 | dwfl_assert ("dwarf_highpc", dwarf_highpc (& it->die, | |
2070 | & highpc)); | |
2071 | ||
2072 | if (it->decl_file == 0) it->decl_file = ""; | |
2073 | ||
2074 | unsigned entrypc_srcline_idx = 0; | |
2075 | dwarf_line_t entrypc_srcline; | |
2076 | // open-code binary search for exact match | |
2077 | { | |
2078 | unsigned l = 0, h = nlines; | |
2079 | while (l < h) | |
2080 | { | |
2081 | entrypc_srcline_idx = (l + h) / 2; | |
2082 | const dwarf_line_t lr(dwarf_onesrcline(lines, | |
2083 | entrypc_srcline_idx)); | |
2084 | Dwarf_Addr addr = lr.addr(); | |
2085 | if (addr == entrypc) { entrypc_srcline = lr; break; } | |
2086 | else if (l + 1 == h) { break; } // ran off bottom of tree | |
2087 | else if (addr < entrypc) { l = entrypc_srcline_idx; } | |
2088 | else { h = entrypc_srcline_idx; } | |
2089 | } | |
2090 | } | |
2091 | if (!entrypc_srcline) | |
2092 | { | |
2093 | if (sess.verbose > 2) | |
f9bbd346 LB |
2094 | clog << _F("missing entrypc dwarf line record for function '%s'\n", |
2095 | it->name.c_str()); | |
440f755a JS |
2096 | // This is probably an inlined function. We'll end up using |
2097 | // its lowpc as a probe address. | |
2098 | continue; | |
2099 | } | |
2100 | ||
48390b53 FCE |
2101 | if (entrypc == 0) |
2102 | { | |
2103 | if (sess.verbose > 2) | |
f9bbd346 LB |
2104 | clog << _F("null entrypc dwarf line record for function '%s'\n", |
2105 | it->name.c_str()); | |
48390b53 FCE |
2106 | // This is probably an inlined function. We'll skip this instance; |
2107 | // it is messed up. | |
2108 | continue; | |
2109 | } | |
2110 | ||
440f755a | 2111 | if (sess.verbose>2) |
2a97f50b | 2112 | clog << _F("searching for prologue of function '%s' %#" PRIx64 "-%#" PRIx64 |
f9bbd346 LB |
2113 | "@%s:%d\n", it->name.c_str(), entrypc, highpc, it->decl_file, |
2114 | it->decl_line); | |
440f755a JS |
2115 | |
2116 | // Now we go searching for the first line record that has a | |
2117 | // file/line different from the one in the declaration. | |
2118 | // Normally, this will be the next one. BUT: | |
2119 | // | |
2120 | // We may have to skip a few because some old compilers plop | |
2121 | // in dummy line records for longer prologues. If we go too | |
2122 | // far (addr >= highpc), we take the previous one. Or, it may | |
2123 | // be the first one, if the function had no prologue, and thus | |
2124 | // the entrypc maps to a statement in the body rather than the | |
2125 | // declaration. | |
2126 | ||
2127 | unsigned postprologue_srcline_idx = entrypc_srcline_idx; | |
2128 | bool ranoff_end = false; | |
2129 | while (postprologue_srcline_idx < nlines) | |
2130 | { | |
2131 | dwarf_line_t lr(dwarf_onesrcline(lines, postprologue_srcline_idx)); | |
2132 | Dwarf_Addr postprologue_addr = lr.addr(); | |
2133 | const char* postprologue_file = lr.linesrc(); | |
2134 | int postprologue_lineno = lr.lineno(); | |
2135 | ||
2136 | if (sess.verbose>2) | |
2a97f50b | 2137 | clog << _F("checking line record %#" PRIx64 "@%s:%d\n", postprologue_addr, |
f9bbd346 | 2138 | postprologue_file, postprologue_lineno); |
440f755a JS |
2139 | |
2140 | if (postprologue_addr >= highpc) | |
2141 | { | |
2142 | ranoff_end = true; | |
2143 | postprologue_srcline_idx --; | |
2144 | continue; | |
2145 | } | |
2146 | if (ranoff_end || | |
2147 | (strcmp (postprologue_file, it->decl_file) || // We have a winner! | |
2148 | (postprologue_lineno != it->decl_line))) | |
2149 | { | |
2150 | it->prologue_end = postprologue_addr; | |
2151 | ||
2152 | if (sess.verbose>2) | |
2153 | { | |
f9bbd346 | 2154 | clog << _F("prologue found function '%s'", it->name.c_str()); |
440f755a | 2155 | // Add a little classification datum |
ce0f6648 | 2156 | //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free) |
f9bbd346 | 2157 | if (postprologue_srcline_idx == entrypc_srcline_idx) clog << _(" (naked)"); |
ce0f6648 | 2158 | //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free) |
f9bbd346 | 2159 | if (ranoff_end) clog << _(" (tail-call?)"); |
440f755a JS |
2160 | clog << " = 0x" << hex << postprologue_addr << dec << "\n"; |
2161 | } | |
2162 | ||
2163 | break; | |
2164 | } | |
2165 | ||
2166 | // Let's try the next srcline. | |
2167 | postprologue_srcline_idx ++; | |
2168 | } // loop over srclines | |
2169 | ||
2170 | // if (strlen(it->decl_file) == 0) it->decl_file = NULL; | |
2171 | ||
2172 | } // loop over functions | |
2173 | ||
2174 | // XXX: how to free lines? | |
2175 | } | |
2176 | ||
2177 | ||
2178 | bool | |
2179 | dwflpp::function_entrypc (Dwarf_Addr * addr) | |
2180 | { | |
2181 | assert (function); | |
1bbf3f90 JS |
2182 | // PR10574: reject 0, which tends to be eliminated COMDAT |
2183 | return (dwarf_entrypc (function, addr) == 0 && *addr != 0); | |
440f755a JS |
2184 | } |
2185 | ||
2186 | ||
2187 | bool | |
2188 | dwflpp::die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr) | |
2189 | { | |
2190 | int rc = 0; | |
2191 | string lookup_method; | |
2192 | ||
2193 | * addr = 0; | |
2194 | ||
2195 | lookup_method = "dwarf_entrypc"; | |
2196 | rc = dwarf_entrypc (die, addr); | |
2197 | ||
440f755a JS |
2198 | if (rc) |
2199 | { | |
2200 | lookup_method = "dwarf_ranges"; | |
2201 | ||
2202 | Dwarf_Addr base; | |
2203 | Dwarf_Addr begin; | |
2204 | Dwarf_Addr end; | |
2205 | ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end); | |
2206 | if (offset < 0) rc = -1; | |
2207 | else if (offset > 0) | |
2208 | { | |
2209 | * addr = begin; | |
2210 | rc = 0; | |
2211 | ||
2212 | // Now we need to check that there are no more ranges | |
2213 | // associated with this function, which could conceivably | |
2214 | // happen if a function is inlined, then pieces of it are | |
2215 | // split amongst different conditional branches. It's not | |
2216 | // obvious which of them to favour. As a heuristic, we | |
2217 | // pick the beginning of the first range, and ignore the | |
2218 | // others (but with a warning). | |
2219 | ||
2220 | unsigned extra = 0; | |
2221 | while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0) | |
2222 | extra ++; | |
2223 | if (extra) | |
f9bbd346 | 2224 | lookup_method += _F(", ignored %s more", lex_cast(extra).c_str()); |
440f755a JS |
2225 | } |
2226 | } | |
2227 | ||
4ad42007 FCE |
2228 | // PR10574: reject subprograms where the entrypc address turns out |
2229 | // to be 0, since they tend to correspond to duplicate-eliminated | |
2230 | // COMDAT copies of C++ functions. | |
2231 | if (rc == 0 && *addr == 0) | |
2232 | { | |
f9bbd346 | 2233 | lookup_method += _(" (skip comdat)"); |
4ad42007 FCE |
2234 | rc = 1; |
2235 | } | |
2236 | ||
440f755a | 2237 | if (sess.verbose > 2) |
e045bb07 | 2238 | clog << _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64 " (rc %d)", lookup_method.c_str(), |
f9bbd346 | 2239 | lex_cast_hex(dwarf_dieoffset(die)).c_str(), *addr, rc) << endl; |
4ad42007 | 2240 | |
440f755a JS |
2241 | return (rc == 0); |
2242 | } | |
2243 | ||
2244 | ||
2245 | void | |
2246 | dwflpp::function_die (Dwarf_Die *d) | |
2247 | { | |
2248 | assert (function); | |
2249 | *d = *function; | |
2250 | } | |
2251 | ||
2252 | ||
2253 | void | |
2254 | dwflpp::function_file (char const ** c) | |
2255 | { | |
2256 | assert (function); | |
2257 | assert (c); | |
c60517ca | 2258 | *c = dwarf_decl_file (function) ?: "<unknown source>"; |
440f755a JS |
2259 | } |
2260 | ||
2261 | ||
2262 | void | |
2263 | dwflpp::function_line (int *linep) | |
2264 | { | |
2265 | assert (function); | |
2266 | dwarf_decl_line (function, linep); | |
2267 | } | |
2268 | ||
2269 | ||
2270 | bool | |
2271 | dwflpp::die_has_pc (Dwarf_Die & die, Dwarf_Addr pc) | |
2272 | { | |
2273 | int res = dwarf_haspc (&die, pc); | |
2274 | // dwarf_ranges will return -1 if a function die has no DW_AT_ranges | |
2275 | // if (res == -1) | |
2276 | // dwarf_assert ("dwarf_haspc", res); | |
2277 | return res == 1; | |
2278 | } | |
2279 | ||
2280 | ||
2a43d5db | 2281 | bool |
f5958c8f JS |
2282 | dwflpp::inner_die_containing_pc(Dwarf_Die& scope, Dwarf_Addr addr, |
2283 | Dwarf_Die& result) | |
2284 | { | |
793f98d5 JS |
2285 | result = scope; |
2286 | ||
2287 | // Sometimes we're in a bad scope to begin with -- just let it be. This can | |
2288 | // happen for example if the compiler outputs a label PC that's just outside | |
2289 | // the lexical scope. We can't really do anything about that, but variables | |
2290 | // will probably not be accessible in this case. | |
f5958c8f | 2291 | if (!die_has_pc(scope, addr)) |
2a43d5db | 2292 | return false; |
f5958c8f | 2293 | |
dee830d9 | 2294 | Dwarf_Die child, import; |
f5958c8f JS |
2295 | int rc = dwarf_child(&result, &child); |
2296 | while (rc == 0) | |
2297 | { | |
2298 | switch (dwarf_tag (&child)) | |
2299 | { | |
dee830d9 MW |
2300 | case DW_TAG_imported_unit: |
2301 | // The children of the imported unit need to be treated as if | |
2302 | // they are inserted here. So look inside and set result if | |
2303 | // found. | |
2304 | if (dwarf_attr_die(&child, DW_AT_import, &import)) | |
2305 | { | |
2306 | Dwarf_Die import_result; | |
2307 | if (inner_die_containing_pc(import, addr, import_result)) | |
2308 | { | |
2309 | result = import_result; | |
2310 | return true; | |
2311 | } | |
2312 | } | |
2313 | break; | |
2314 | ||
f5958c8f JS |
2315 | // lexical tags to recurse within the same starting scope |
2316 | // NB: this intentionally doesn't cross into inlines! | |
2317 | case DW_TAG_lexical_block: | |
2318 | case DW_TAG_with_stmt: | |
2319 | case DW_TAG_catch_block: | |
2320 | case DW_TAG_try_block: | |
2321 | case DW_TAG_entry_point: | |
2322 | if (die_has_pc(child, addr)) | |
2323 | { | |
2324 | result = child; | |
2325 | rc = dwarf_child(&result, &child); | |
2326 | continue; | |
2327 | } | |
2328 | } | |
2329 | rc = dwarf_siblingof(&child, &child); | |
2330 | } | |
2a43d5db | 2331 | return true; |
f5958c8f JS |
2332 | } |
2333 | ||
2334 | ||
440f755a JS |
2335 | void |
2336 | dwflpp::loc2c_error (void *, const char *fmt, ...) | |
2337 | { | |
2338 | const char *msg = "?"; | |
2339 | char *tmp = NULL; | |
2340 | int rc; | |
2341 | va_list ap; | |
2342 | va_start (ap, fmt); | |
2343 | rc = vasprintf (& tmp, fmt, ap); | |
2344 | if (rc < 0) | |
2345 | msg = "?"; | |
2346 | else | |
2347 | msg = tmp; | |
2348 | va_end (ap); | |
dc09353a | 2349 | throw SEMANTIC_ERROR (msg); |
440f755a JS |
2350 | } |
2351 | ||
2352 | ||
2353 | // This function generates code used for addressing computations of | |
2354 | // target variables. | |
2355 | void | |
2356 | dwflpp::emit_address (struct obstack *pool, Dwarf_Addr address) | |
2357 | { | |
2d7232ca | 2358 | int n = dwfl_module_relocations (module); |
440f755a JS |
2359 | dwfl_assert ("dwfl_module_relocations", n >= 0); |
2360 | Dwarf_Addr reloc_address = address; | |
2d7232ca MW |
2361 | const char *secname = ""; |
2362 | if (n > 1) | |
2363 | { | |
2364 | int i = dwfl_module_relocate_address (module, &reloc_address); | |
2365 | dwfl_assert ("dwfl_module_relocate_address", i >= 0); | |
2366 | secname = dwfl_module_relocation_info (module, i, NULL); | |
2367 | } | |
440f755a JS |
2368 | |
2369 | if (sess.verbose > 2) | |
2370 | { | |
2a97f50b | 2371 | clog << _F("emit dwarf addr %#" PRIx64 " => module %s section %s relocaddr %#" PRIx64, |
2d7232ca MW |
2372 | address, module_name.c_str (), (secname ?: "null"), |
2373 | reloc_address) << endl; | |
440f755a JS |
2374 | } |
2375 | ||
2376 | if (n > 0 && !(n == 1 && secname == NULL)) | |
2377 | { | |
2378 | dwfl_assert ("dwfl_module_relocation_info", secname); | |
2379 | if (n > 1 || secname[0] != '\0') | |
2380 | { | |
2381 | // This gives us the module name, and section name within the | |
2382 | // module, for a kernel module (or other ET_REL module object). | |
f685b2fe | 2383 | obstack_printf (pool, "({ unsigned long addr = 0; "); |
a049e342 | 2384 | obstack_printf (pool, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", |
2d7232ca | 2385 | module_name.c_str(), secname, reloc_address); |
440f755a JS |
2386 | obstack_printf (pool, "addr; })"); |
2387 | } | |
2388 | else if (n == 1 && module_name == TOK_KERNEL && secname[0] == '\0') | |
2389 | { | |
2390 | // elfutils' way of telling us that this is a relocatable kernel address, which we | |
2391 | // need to treat the same way here as dwarf_query::add_probe_point does: _stext. | |
2392 | address -= sess.sym_stext; | |
2393 | secname = "_stext"; | |
f685b2fe MW |
2394 | // Note we "cache" the result here through a static because the |
2395 | // kernel will never move after being loaded (unlike modules and | |
2396 | // user-space dynamic share libraries). | |
440f755a | 2397 | obstack_printf (pool, "({ static unsigned long addr = 0; "); |
a049e342 | 2398 | obstack_printf (pool, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", |
2d7232ca | 2399 | module_name.c_str(), secname, address); // PR10000 NB: not reloc_address |
440f755a JS |
2400 | obstack_printf (pool, "addr; })"); |
2401 | } | |
2402 | else | |
2403 | { | |
7f085e3f | 2404 | obstack_printf (pool, "/* pragma:vma */"); |
f685b2fe | 2405 | obstack_printf (pool, "({ unsigned long addr = 0; "); |
a049e342 | 2406 | obstack_printf (pool, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64 ", current); ", |
5bca76a8 | 2407 | resolve_path(module_name.c_str()).c_str(), address); |
a295050e | 2408 | obstack_printf (pool, "addr; })"); |
440f755a JS |
2409 | } |
2410 | } | |
2411 | else | |
2412 | obstack_printf (pool, "%#" PRIx64 "UL", address); // assume as constant | |
2413 | } | |
2414 | ||
2415 | ||
2416 | void | |
2417 | dwflpp::loc2c_emit_address (void *arg, struct obstack *pool, | |
2418 | Dwarf_Addr address) | |
2419 | { | |
c94efd63 | 2420 | static_cast<dwflpp *>(arg)->emit_address (pool, address); |
440f755a JS |
2421 | } |
2422 | ||
2423 | ||
2424 | void | |
2b4f27bd | 2425 | dwflpp::get_locals(vector<Dwarf_Die>& scopes, set<string>& locals) |
440f755a | 2426 | { |
729455a7 JS |
2427 | // XXX Shouldn't this be walking up to outer scopes too? |
2428 | ||
2b4f27bd | 2429 | get_locals_die(scopes[0], locals); |
dee830d9 MW |
2430 | } |
2431 | ||
2432 | void | |
2b4f27bd | 2433 | dwflpp::get_locals_die(Dwarf_Die& die, set<string>& locals) |
dee830d9 | 2434 | { |
440f755a | 2435 | // Try to get the first child of die. |
dee830d9 MW |
2436 | Dwarf_Die child, import; |
2437 | if (dwarf_child (&die, &child) == 0) | |
440f755a JS |
2438 | { |
2439 | do | |
2440 | { | |
2441 | const char *name; | |
2442 | // Output each sibling's name (that is a variable or | |
2443 | // parameter) to 'o'. | |
2444 | switch (dwarf_tag (&child)) | |
2445 | { | |
2446 | case DW_TAG_variable: | |
2447 | case DW_TAG_formal_parameter: | |
2448 | name = dwarf_diename (&child); | |
2449 | if (name) | |
2b4f27bd | 2450 | locals.insert(string("$") + name); |
440f755a | 2451 | break; |
dee830d9 MW |
2452 | case DW_TAG_imported_unit: |
2453 | // Treat the imported unit children as if they are | |
2454 | // children of the given DIE. | |
2455 | if (dwarf_attr_die(&child, DW_AT_import, &import)) | |
2b4f27bd | 2456 | get_locals_die (import, locals); |
dee830d9 | 2457 | break; |
440f755a JS |
2458 | default: |
2459 | break; | |
2460 | } | |
2461 | } | |
2462 | while (dwarf_siblingof (&child, &child) == 0); | |
2463 | } | |
2464 | } | |
2465 | ||
2466 | ||
2467 | Dwarf_Attribute * | |
729455a7 | 2468 | dwflpp::find_variable_and_frame_base (vector<Dwarf_Die>& scopes, |
440f755a JS |
2469 | Dwarf_Addr pc, |
2470 | string const & local, | |
2471 | const target_symbol *e, | |
2472 | Dwarf_Die *vardie, | |
2473 | Dwarf_Attribute *fb_attr_mem) | |
2474 | { | |
729455a7 | 2475 | Dwarf_Die *scope_die = &scopes[0]; |
440f755a JS |
2476 | Dwarf_Attribute *fb_attr = NULL; |
2477 | ||
2478 | assert (cu); | |
2479 | ||
729455a7 | 2480 | int declaring_scope = dwarf_getscopevar (&scopes[0], scopes.size(), |
440f755a JS |
2481 | local.c_str(), |
2482 | 0, NULL, 0, 0, | |
2483 | vardie); | |
2484 | if (declaring_scope < 0) | |
2485 | { | |
2b4f27bd JL |
2486 | set<string> locals; |
2487 | get_locals(scopes, locals); | |
2488 | string sugs = levenshtein_suggest(local, locals); // probably not that many, so no limit | |
bd1fcbad | 2489 | if (pc) |
dc09353a | 2490 | throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)", |
3d9381fc | 2491 | local.c_str(), |
54bc3392 MW |
2492 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(), |
2493 | module_name.c_str(), | |
3d9381fc MW |
2494 | lex_cast_hex(pc).c_str(), |
2495 | (scope_die == NULL) ? "" : _("in"), | |
2496 | (dwarf_diename(scope_die) ?: "<unknown>"), | |
2497 | (dwarf_diename(cu) ?: "<unknown>"), | |
2b4f27bd | 2498 | (sugs.empty() |
3d9381fc | 2499 | ? (_("<no alternatives>")) |
2b4f27bd | 2500 | : (_("alternatives: ") + sugs + ")")).c_str()), |
3d9381fc MW |
2501 | e->tok); |
2502 | else | |
dc09353a | 2503 | throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)", |
3d9381fc | 2504 | local.c_str(), |
54bc3392 MW |
2505 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(), |
2506 | module_name.c_str(), | |
3d9381fc MW |
2507 | (scope_die == NULL) ? "" : _("in"), |
2508 | (dwarf_diename(scope_die) ?: "<unknown>"), | |
bd1fcbad | 2509 | cu_name().c_str(), |
2b4f27bd | 2510 | (sugs.empty() |
3d9381fc | 2511 | ? (_("<no alternatives>")) |
2b4f27bd | 2512 | : (_("alternatives: ") + sugs + ")")).c_str()), |
3d9381fc | 2513 | e->tok); |
440f755a JS |
2514 | } |
2515 | ||
4d51e8ca MW |
2516 | /* Some GCC versions would output duplicate external variables, one |
2517 | without a location attribute. If so, try to find the other if it | |
2518 | exists in the same scope. See GCC PR51410. */ | |
2519 | Dwarf_Attribute attr_mem; | |
2520 | if (dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem) == NULL | |
2521 | && dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem) == NULL | |
2522 | && dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem) != NULL | |
2523 | && dwarf_tag(&scopes[declaring_scope]) == DW_TAG_compile_unit) | |
2524 | { | |
ea484b93 | 2525 | Dwarf_Die orig_vardie = *vardie; |
4d51e8ca MW |
2526 | bool alt_found = false; |
2527 | if (dwarf_child(&scopes[declaring_scope], vardie) == 0) | |
2528 | do | |
2529 | { | |
dee830d9 MW |
2530 | // Note, not handling DW_TAG_imported_unit, assuming GCC |
2531 | // version is recent enough to not need this workaround if | |
2532 | // we would see an imported unit. | |
4d51e8ca MW |
2533 | if (dwarf_tag (vardie) == DW_TAG_variable |
2534 | && strcmp (dwarf_diename (vardie), local.c_str ()) == 0 | |
2535 | && (dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem) | |
2536 | != NULL) | |
2537 | && ((dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem) | |
2538 | != NULL) | |
2539 | || (dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem) | |
2540 | != NULL))) | |
2541 | alt_found = true; | |
2542 | } | |
2543 | while (!alt_found && dwarf_siblingof(vardie, vardie) == 0); | |
2544 | ||
2545 | if (! alt_found) | |
ea484b93 | 2546 | *vardie = orig_vardie; |
4d51e8ca MW |
2547 | } |
2548 | ||
3965e105 | 2549 | // Global vars don't need (cannot use) frame base in location descriptor. |
bd1fcbad | 2550 | if (pc == 0) |
3965e105 MW |
2551 | return NULL; |
2552 | ||
7bce6f87 MW |
2553 | /* We start out walking the "lexical scopes" as returned by |
2554 | * as returned by dwarf_getscopes for the address, starting with the | |
2555 | * declaring_scope that the variable was found in. | |
2556 | */ | |
729455a7 JS |
2557 | vector<Dwarf_Die> physcopes, *fbscopes = &scopes; |
2558 | for (size_t inner = declaring_scope; | |
2559 | inner < fbscopes->size() && fb_attr == NULL; | |
7bce6f87 | 2560 | ++inner) |
440f755a | 2561 | { |
729455a7 JS |
2562 | Dwarf_Die& scope = (*fbscopes)[inner]; |
2563 | switch (dwarf_tag (&scope)) | |
440f755a JS |
2564 | { |
2565 | default: | |
2566 | continue; | |
2567 | case DW_TAG_subprogram: | |
2568 | case DW_TAG_entry_point: | |
729455a7 | 2569 | fb_attr = dwarf_attr_integrate (&scope, |
7bce6f87 MW |
2570 | DW_AT_frame_base, |
2571 | fb_attr_mem); | |
2572 | break; | |
2573 | case DW_TAG_inlined_subroutine: | |
2574 | /* Unless we already are going through the "pyshical die tree", | |
2575 | * we now need to start walking the die tree where this | |
2576 | * subroutine is inlined to find the appropriate frame base. */ | |
2577 | if (declaring_scope != -1) | |
2578 | { | |
729455a7 JS |
2579 | physcopes = getscopes_die(&scope); |
2580 | if (physcopes.empty()) | |
dc09353a | 2581 | throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine", |
f9bbd346 | 2582 | local.c_str()), e->tok); |
729455a7 | 2583 | fbscopes = &physcopes; |
7bce6f87 MW |
2584 | inner = 0; // zero is current scope, for look will increase. |
2585 | declaring_scope = -1; | |
2586 | } | |
440f755a JS |
2587 | break; |
2588 | } | |
2589 | } | |
7bce6f87 | 2590 | |
440f755a JS |
2591 | return fb_attr; |
2592 | } | |
2593 | ||
2594 | ||
2595 | struct location * | |
2596 | dwflpp::translate_location(struct obstack *pool, | |
00730da1 MW |
2597 | Dwarf_Attribute *attr, Dwarf_Die *die, |
2598 | Dwarf_Addr pc, | |
440f755a JS |
2599 | Dwarf_Attribute *fb_attr, |
2600 | struct location **tail, | |
2601 | const target_symbol *e) | |
2602 | { | |
619d9aaf MW |
2603 | |
2604 | /* DW_AT_data_member_location, can be either constant offsets | |
e050d62f | 2605 | (struct member fields), or full blown location expressions. */ |
619d9aaf | 2606 | |
03ba05b6 MW |
2607 | /* There is no location expression, but a constant value instead. */ |
2608 | if (dwarf_whatattr (attr) == DW_AT_const_value) | |
2609 | { | |
2610 | *tail = c_translate_constant (pool, &loc2c_error, this, | |
2611 | &loc2c_emit_address, 0, pc, attr); | |
2612 | return *tail; | |
2613 | } | |
2614 | ||
440f755a JS |
2615 | Dwarf_Op *expr; |
2616 | size_t len; | |
2617 | ||
2618 | /* PR9768: formerly, we added pc+module_bias here. However, that bias value | |
2619 | is not present in the pc value by the time we get it, so adding it would | |
2620 | result in false negatives of variable reachibility. In other instances | |
2621 | further below, the c_translate_FOO functions, the module_bias value used | |
2622 | to be passed in, but instead should now be zero for the same reason. */ | |
2623 | ||
45b02a36 | 2624 | retry: |
440f755a JS |
2625 | switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1)) |
2626 | { | |
2627 | case 1: /* Should always happen. */ | |
2628 | if (len > 0) | |
2629 | break; | |
2630 | /* Fall through. */ | |
2631 | ||
45b02a36 FCE |
2632 | case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */ |
2633 | { | |
2634 | Dwarf_Addr pc2 = pr15123_retry_addr (pc, die); | |
2635 | if (pc2 != 0) { | |
2636 | pc = pc2; | |
2637 | goto retry; | |
2638 | } | |
2639 | } | |
2640 | ||
2641 | /* FALLTHROUGH */ | |
dc09353a | 2642 | throw SEMANTIC_ERROR (_F("not accessible at this address [man error::dwarf] (%s, dieoffset: %s)", |
f9bbd346 LB |
2643 | lex_cast_hex(pc).c_str(), lex_cast_hex(dwarf_dieoffset(die)).c_str()), |
2644 | e->tok); | |
440f755a JS |
2645 | |
2646 | default: /* Shouldn't happen. */ | |
2647 | case -1: | |
dc09353a | 2648 | throw SEMANTIC_ERROR (_F("dwarf_getlocation_addr failed [man error::dwarf] , %s", dwarf_errmsg(-1)), e->tok); |
440f755a JS |
2649 | } |
2650 | ||
3965e105 | 2651 | Dwarf_Op *cfa_ops; |
87748e2b MW |
2652 | // pc is in the dw address space of the current module, which is what |
2653 | // c_translate_location expects. get_cfa_ops wants the global dwfl address. | |
3965e105 | 2654 | // cfa_ops only make sense for locals. |
bd1fcbad | 2655 | if (pc) |
3965e105 MW |
2656 | { |
2657 | Dwarf_Addr addr = pc + module_bias; | |
2658 | cfa_ops = get_cfa_ops (addr); | |
2659 | } | |
2660 | else | |
2661 | cfa_ops = NULL; | |
2662 | ||
440f755a JS |
2663 | return c_translate_location (pool, &loc2c_error, this, |
2664 | &loc2c_emit_address, | |
2665 | 1, 0 /* PR9768 */, | |
24c7957b | 2666 | pc, attr, expr, len, tail, fb_attr, cfa_ops); |
440f755a JS |
2667 | } |
2668 | ||
2669 | ||
2670 | void | |
f5f03f5d | 2671 | dwflpp::get_members(Dwarf_Die *typedie, set<string>& members, set<string> &dupes) |
440f755a | 2672 | { |
28a494df | 2673 | const int typetag = dwarf_tag (typedie); |
440f755a | 2674 | |
dee830d9 MW |
2675 | /* compile and partial unit included for recursion through |
2676 | imported_unit below. */ | |
9c119951 JS |
2677 | if (typetag != DW_TAG_structure_type && |
2678 | typetag != DW_TAG_class_type && | |
dee830d9 MW |
2679 | typetag != DW_TAG_union_type && |
2680 | typetag != DW_TAG_compile_unit && | |
2681 | typetag != DW_TAG_partial_unit) | |
440f755a | 2682 | { |
f5f03f5d JL |
2683 | throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union", |
2684 | dwarf_type_name(typedie).c_str())); | |
440f755a JS |
2685 | } |
2686 | ||
2687 | // Try to get the first child of vardie. | |
dee830d9 | 2688 | Dwarf_Die die_mem, import; |
440f755a | 2689 | Dwarf_Die *die = &die_mem; |
28a494df | 2690 | switch (dwarf_child (typedie, die)) |
440f755a JS |
2691 | { |
2692 | case 1: // No children. | |
f5f03f5d | 2693 | throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie).c_str())); |
440f755a JS |
2694 | |
2695 | case -1: // Error. | |
2696 | default: // Shouldn't happen. | |
f5f03f5d JL |
2697 | throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie).c_str(), |
2698 | dwarf_errmsg(-1))); | |
440f755a JS |
2699 | |
2700 | case 0: // Success. | |
2701 | break; | |
2702 | } | |
2703 | ||
f5f03f5d | 2704 | // Add each sibling's name to members set |
9c119951 | 2705 | do |
440f755a | 2706 | { |
9c119951 | 2707 | int tag = dwarf_tag(die); |
dee830d9 MW |
2708 | |
2709 | /* The children of an imported_unit should be treated as members too. */ | |
2710 | if (tag == DW_TAG_imported_unit | |
2711 | && dwarf_attr_die(die, DW_AT_import, &import)) | |
f5f03f5d | 2712 | get_members(&import, members, dupes); |
dee830d9 | 2713 | |
9c119951 JS |
2714 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
2715 | continue; | |
2716 | ||
23d106b9 | 2717 | const char *member = dwarf_diename (die) ; |
440f755a | 2718 | |
9c119951 | 2719 | if ( tag == DW_TAG_member && member != NULL ) |
1de6dd7a JS |
2720 | { |
2721 | // Only output if this is new, to avoid inheritance dupes. | |
2722 | if (dupes.insert(member).second) | |
f5f03f5d | 2723 | members.insert(member); |
1de6dd7a | 2724 | } |
440f755a JS |
2725 | else |
2726 | { | |
f1c8f8a5 | 2727 | Dwarf_Die temp_die; |
3d1ad340 | 2728 | if (!dwarf_attr_die (die, DW_AT_type, &temp_die)) |
440f755a | 2729 | { |
f1c8f8a5 JS |
2730 | string source = dwarf_decl_file(die) ?: "<unknown source>"; |
2731 | int line = -1; | |
2732 | dwarf_decl_line(die, &line); | |
f5f03f5d JL |
2733 | throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous " |
2734 | "member at %s:%d", source.c_str(), line)); | |
440f755a JS |
2735 | } |
2736 | ||
f5f03f5d | 2737 | get_members(&temp_die, members, dupes); |
440f755a JS |
2738 | } |
2739 | ||
440f755a | 2740 | } |
9c119951 | 2741 | while (dwarf_siblingof (die, die) == 0); |
440f755a JS |
2742 | } |
2743 | ||
2744 | ||
2745 | bool | |
c67847a0 | 2746 | dwflpp::find_struct_member(const target_symbol::component& c, |
440f755a | 2747 | Dwarf_Die *parentdie, |
440f755a | 2748 | Dwarf_Die *memberdie, |
00730da1 | 2749 | vector<Dwarf_Die>& dies, |
440f755a JS |
2750 | vector<Dwarf_Attribute>& locs) |
2751 | { | |
2752 | Dwarf_Attribute attr; | |
b57ba9b8 | 2753 | Dwarf_Die die; |
440f755a | 2754 | |
e5a573c0 JS |
2755 | /* With inheritance, a subclass may mask member names of parent classes, so |
2756 | * our search among the inheritance tree must be breadth-first rather than | |
2757 | * depth-first (recursive). The parentdie is still our starting point. */ | |
2758 | deque<Dwarf_Die> inheritees(1, *parentdie); | |
2759 | for (; !inheritees.empty(); inheritees.pop_front()) | |
440f755a | 2760 | { |
e5a573c0 | 2761 | switch (dwarf_child (&inheritees.front(), &die)) |
440f755a | 2762 | { |
e5a573c0 JS |
2763 | case 0: /* First child found. */ |
2764 | break; | |
2765 | case 1: /* No children. */ | |
2766 | continue; | |
2767 | case -1: /* Error. */ | |
2768 | default: /* Shouldn't happen */ | |
dc09353a | 2769 | throw SEMANTIC_ERROR (dwarf_type_name(&inheritees.front()) + ": " |
e5a573c0 JS |
2770 | + string (dwarf_errmsg (-1)), |
2771 | c.tok); | |
440f755a | 2772 | } |
e5a573c0 JS |
2773 | |
2774 | do | |
440f755a | 2775 | { |
e5a573c0 | 2776 | int tag = dwarf_tag(&die); |
dee830d9 MW |
2777 | /* recurse into imported units as if they are anonymoust structs */ |
2778 | Dwarf_Die import; | |
2779 | if (tag == DW_TAG_imported_unit | |
2780 | && dwarf_attr_die(&die, DW_AT_import, &import) | |
2781 | && find_struct_member(c, &import, memberdie, dies, locs)) | |
2782 | goto success; | |
2783 | ||
e5a573c0 JS |
2784 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
2785 | continue; | |
2786 | ||
2787 | const char *name = dwarf_diename(&die); | |
2788 | if (tag == DW_TAG_inheritance) | |
2789 | { | |
2790 | /* Remember inheritee for breadth-first search. */ | |
2791 | Dwarf_Die inheritee; | |
2792 | if (dwarf_attr_die (&die, DW_AT_type, &inheritee)) | |
2793 | inheritees.push_back(inheritee); | |
2794 | } | |
2795 | else if (name == NULL) | |
2796 | { | |
2797 | /* Need to recurse for anonymous structs/unions. */ | |
2798 | Dwarf_Die subdie; | |
2799 | if (dwarf_attr_die (&die, DW_AT_type, &subdie) && | |
2800 | find_struct_member(c, &subdie, memberdie, dies, locs)) | |
2801 | goto success; | |
2802 | } | |
2803 | else if (name == c.member) | |
2804 | { | |
2805 | *memberdie = die; | |
2806 | goto success; | |
2807 | } | |
440f755a | 2808 | } |
e5a573c0 | 2809 | while (dwarf_siblingof (&die, &die) == 0); |
440f755a | 2810 | } |
440f755a JS |
2811 | |
2812 | return false; | |
2813 | ||
2814 | success: | |
2815 | /* As we unwind the recursion, we need to build the chain of | |
2816 | * locations that got to the final answer. */ | |
2817 | if (dwarf_attr_integrate (&die, DW_AT_data_member_location, &attr)) | |
00730da1 MW |
2818 | { |
2819 | dies.insert(dies.begin(), die); | |
2820 | locs.insert(locs.begin(), attr); | |
2821 | } | |
440f755a JS |
2822 | |
2823 | /* Union members don't usually have a location, | |
2824 | * but just use the containing union's location. */ | |
2825 | else if (dwarf_tag(parentdie) != DW_TAG_union_type) | |
dc09353a | 2826 | throw SEMANTIC_ERROR (_F("no location for field '%s':%s", |
f9bbd346 | 2827 | c.member.c_str(), dwarf_errmsg(-1)), c.tok); |
440f755a JS |
2828 | |
2829 | return true; | |
2830 | } | |
2831 | ||
2832 | ||
6ce303b8 JS |
2833 | static inline void |
2834 | dwarf_die_type (Dwarf_Die *die, Dwarf_Die *typedie_mem, const token *tok=NULL) | |
2835 | { | |
2836 | if (!dwarf_attr_die (die, DW_AT_type, typedie_mem)) | |
dc09353a | 2837 | throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok); |
6ce303b8 JS |
2838 | } |
2839 | ||
2840 | ||
2841 | void | |
440f755a JS |
2842 | dwflpp::translate_components(struct obstack *pool, |
2843 | struct location **tail, | |
2844 | Dwarf_Addr pc, | |
2845 | const target_symbol *e, | |
2846 | Dwarf_Die *vardie, | |
f3b5366d JS |
2847 | Dwarf_Die *typedie, |
2848 | unsigned first) | |
440f755a | 2849 | { |
f3b5366d | 2850 | unsigned i = first; |
440f755a JS |
2851 | while (i < e->components.size()) |
2852 | { | |
c67847a0 JS |
2853 | const target_symbol::component& c = e->components[i]; |
2854 | ||
440f755a JS |
2855 | /* XXX: This would be desirable, but we don't get the target_symbol token, |
2856 | and printing that gives us the file:line number too early anyway. */ | |
2857 | #if 0 | |
2858 | // Emit a marker to note which field is being access-attempted, to give | |
2859 | // better error messages if deref() fails. | |
aca66a36 | 2860 | string piece = string(...target_symbol token...) + string ("#") + lex_cast(components[i].second); |
440f755a JS |
2861 | obstack_printf (pool, "c->last_stmt = %s;", lex_cast_qstring(piece).c_str()); |
2862 | #endif | |
2863 | ||
6ce303b8 | 2864 | switch (dwarf_tag (typedie)) |
440f755a JS |
2865 | { |
2866 | case DW_TAG_typedef: | |
2867 | case DW_TAG_const_type: | |
2868 | case DW_TAG_volatile_type: | |
2869 | /* Just iterate on the referent type. */ | |
6ce303b8 | 2870 | dwarf_die_type (typedie, typedie, c.tok); |
440f755a JS |
2871 | break; |
2872 | ||
9c119951 JS |
2873 | case DW_TAG_reference_type: |
2874 | case DW_TAG_rvalue_reference_type: | |
5f36109e JS |
2875 | if (pool) |
2876 | c_translate_pointer (pool, 1, 0 /* PR9768*/, typedie, tail); | |
6ce303b8 | 2877 | dwarf_die_type (typedie, typedie, c.tok); |
9c119951 JS |
2878 | break; |
2879 | ||
440f755a | 2880 | case DW_TAG_pointer_type: |
c4dddae6 | 2881 | /* A pointer with no type is a void* -- can't dereference it. */ |
6ce303b8 | 2882 | if (!dwarf_hasattr_integrate (typedie, DW_AT_type)) |
dc09353a | 2883 | throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c).c_str(), |
f9bbd346 | 2884 | dwarf_type_name(typedie).c_str()), c.tok); |
c4dddae6 | 2885 | |
5f36109e JS |
2886 | if (pool) |
2887 | c_translate_pointer (pool, 1, 0 /* PR9768*/, typedie, tail); | |
6fda2dff JS |
2888 | if (c.type != target_symbol::comp_literal_array_index && |
2889 | c.type != target_symbol::comp_expression_array_index) | |
6ce303b8 JS |
2890 | { |
2891 | dwarf_die_type (typedie, typedie, c.tok); | |
2892 | break; | |
2893 | } | |
d52761f8 | 2894 | /* else fall through as an array access */ |
440f755a JS |
2895 | |
2896 | case DW_TAG_array_type: | |
c67847a0 | 2897 | if (c.type == target_symbol::comp_literal_array_index) |
5f36109e JS |
2898 | { |
2899 | if (pool) | |
2900 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, | |
2901 | NULL, c.num_index); | |
2902 | } | |
6fda2dff JS |
2903 | else if (c.type == target_symbol::comp_expression_array_index) |
2904 | { | |
0386bbcf | 2905 | string index = "STAP_ARG_index" + lex_cast(i); |
5f36109e JS |
2906 | if (pool) |
2907 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, | |
2908 | index.c_str(), 0); | |
6fda2dff | 2909 | } |
440f755a | 2910 | else |
dc09353a | 2911 | throw SEMANTIC_ERROR (_F("invalid access '%s' for array type", |
f9bbd346 | 2912 | lex_cast(c).c_str()), c.tok); |
6ce303b8 JS |
2913 | |
2914 | dwarf_die_type (typedie, typedie, c.tok); | |
2915 | *vardie = *typedie; | |
2916 | ++i; | |
440f755a JS |
2917 | break; |
2918 | ||
2919 | case DW_TAG_structure_type: | |
2920 | case DW_TAG_union_type: | |
9c119951 | 2921 | case DW_TAG_class_type: |
c67847a0 | 2922 | if (c.type != target_symbol::comp_struct_member) |
dc09353a | 2923 | throw SEMANTIC_ERROR (_F("invalid access '%s' for %s", |
f9bbd346 | 2924 | lex_cast(c).c_str(), dwarf_type_name(typedie).c_str())); |
c67847a0 | 2925 | |
6ce303b8 | 2926 | if (dwarf_hasattr(typedie, DW_AT_declaration)) |
440f755a | 2927 | { |
a44a7cb5 | 2928 | Dwarf_Die *tmpdie = declaration_resolve(typedie); |
440f755a | 2929 | if (tmpdie == NULL) |
dc09353a | 2930 | throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie).c_str()), c.tok); |
6ce303b8 | 2931 | *typedie = *tmpdie; |
440f755a JS |
2932 | } |
2933 | ||
2934 | { | |
00730da1 | 2935 | vector<Dwarf_Die> dies; |
440f755a | 2936 | vector<Dwarf_Attribute> locs; |
00730da1 | 2937 | if (!find_struct_member(c, typedie, vardie, dies, locs)) |
440f755a | 2938 | { |
72c5ecc2 JS |
2939 | /* Add a file:line hint for anonymous types */ |
2940 | string source; | |
6ce303b8 | 2941 | if (!dwarf_hasattr_integrate(typedie, DW_AT_name)) |
72c5ecc2 JS |
2942 | { |
2943 | int line; | |
6ce303b8 JS |
2944 | const char *file = dwarf_decl_file(typedie); |
2945 | if (file && dwarf_decl_line(typedie, &line) == 0) | |
72c5ecc2 | 2946 | source = " (" + string(file) + ":" |
aca66a36 | 2947 | + lex_cast(line) + ")"; |
72c5ecc2 JS |
2948 | } |
2949 | ||
f5f03f5d JL |
2950 | set<string> members, member_dupes; |
2951 | get_members(typedie, members, member_dupes); | |
2952 | string sugs = levenshtein_suggest(c.member, members); | |
2953 | if (!sugs.empty()) | |
2954 | sugs = " (alternatives: " + sugs + ")"; | |
dc09353a | 2955 | throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c.member.c_str(), |
f9bbd346 | 2956 | dwarf_type_name(typedie).c_str(), source.c_str(), |
f5f03f5d | 2957 | sugs.c_str()), c.tok); |
440f755a JS |
2958 | } |
2959 | ||
2960 | for (unsigned j = 0; j < locs.size(); ++j) | |
5f36109e | 2961 | if (pool) |
00730da1 MW |
2962 | translate_location (pool, &locs[j], &dies[j], |
2963 | pc, NULL, tail, e); | |
440f755a JS |
2964 | } |
2965 | ||
6ce303b8 | 2966 | dwarf_die_type (vardie, typedie, c.tok); |
440f755a JS |
2967 | ++i; |
2968 | break; | |
2969 | ||
2970 | case DW_TAG_enumeration_type: | |
440f755a | 2971 | case DW_TAG_base_type: |
dc09353a | 2972 | throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c).c_str(), |
f9bbd346 | 2973 | dwarf_type_name(typedie).c_str()), c.tok); |
440f755a | 2974 | break; |
f1c8f8a5 | 2975 | |
440f755a | 2976 | case -1: |
dc09353a | 2977 | throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c.tok); |
440f755a JS |
2978 | break; |
2979 | ||
2980 | default: | |
dc09353a | 2981 | throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie).c_str(), |
f9bbd346 | 2982 | lex_cast(dwarf_tag(typedie)).c_str()), c.tok); |
440f755a JS |
2983 | break; |
2984 | } | |
440f755a | 2985 | } |
440f755a JS |
2986 | } |
2987 | ||
2988 | ||
6ce303b8 JS |
2989 | void |
2990 | dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die *typedie, | |
2991 | Dwarf_Die *innerdie, | |
440f755a JS |
2992 | const target_symbol *e) |
2993 | { | |
6ce303b8 JS |
2994 | int typetag = dwarf_tag (typedie); |
2995 | *innerdie = *typedie; | |
2996 | while (typetag == DW_TAG_typedef || | |
2997 | typetag == DW_TAG_const_type || | |
2998 | typetag == DW_TAG_volatile_type) | |
440f755a | 2999 | { |
6ce303b8 | 3000 | if (!dwarf_attr_die (innerdie, DW_AT_type, innerdie)) |
dc09353a | 3001 | throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e->tok); |
6ce303b8 | 3002 | typetag = dwarf_tag (innerdie); |
440f755a | 3003 | } |
440f755a JS |
3004 | } |
3005 | ||
3006 | ||
3007 | void | |
3008 | dwflpp::translate_final_fetch_or_store (struct obstack *pool, | |
3009 | struct location **tail, | |
822a6a3d | 3010 | Dwarf_Addr /*module_bias*/, |
6ce303b8 JS |
3011 | Dwarf_Die *vardie, |
3012 | Dwarf_Die *start_typedie, | |
440f755a JS |
3013 | bool lvalue, |
3014 | const target_symbol *e, | |
3015 | string &, | |
3016 | string &, | |
3017 | exp_type & ty) | |
3018 | { | |
3019 | /* First boil away any qualifiers associated with the type DIE of | |
3020 | the final location to be accessed. */ | |
3021 | ||
6ce303b8 JS |
3022 | Dwarf_Die typedie_mem, *typedie = &typedie_mem; |
3023 | resolve_unqualified_inner_typedie (start_typedie, typedie, e); | |
440f755a | 3024 | |
03c75a4a JS |
3025 | /* If we're looking for an address, then we can just provide what |
3026 | we computed to this point, without using a fetch/store. */ | |
3027 | if (e->addressof) | |
3028 | { | |
3029 | if (lvalue) | |
dc09353a | 3030 | throw SEMANTIC_ERROR (_("cannot write to member address"), e->tok); |
03c75a4a | 3031 | |
6ce303b8 | 3032 | if (dwarf_hasattr_integrate (vardie, DW_AT_bit_offset)) |
dc09353a | 3033 | throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e->tok); |
03c75a4a | 3034 | |
0386bbcf | 3035 | c_translate_addressof (pool, 1, 0, vardie, typedie, tail, "STAP_RETVALUE"); |
03c75a4a JS |
3036 | ty = pe_long; |
3037 | return; | |
3038 | } | |
3039 | ||
440f755a JS |
3040 | /* Then switch behavior depending on the type of fetch/store we |
3041 | want, and the type and pointer-ness of the final location. */ | |
3042 | ||
6ce303b8 | 3043 | int typetag = dwarf_tag (typedie); |
440f755a JS |
3044 | switch (typetag) |
3045 | { | |
3046 | default: | |
dc09353a | 3047 | throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag).c_str(), |
f9bbd346 | 3048 | dwarf_type_name(typedie).c_str()), e->tok); |
440f755a JS |
3049 | break; |
3050 | ||
3051 | case DW_TAG_structure_type: | |
9c119951 | 3052 | case DW_TAG_class_type: |
440f755a | 3053 | case DW_TAG_union_type: |
dc09353a | 3054 | throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member", |
f9bbd346 | 3055 | dwarf_type_name(typedie).c_str()), e->tok); |
440f755a JS |
3056 | break; |
3057 | ||
440f755a JS |
3058 | case DW_TAG_base_type: |
3059 | ||
3060 | // Reject types we can't handle in systemtap | |
3061 | { | |
440f755a JS |
3062 | Dwarf_Attribute encoding_attr; |
3063 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
3064 | dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding, &encoding_attr), | |
3065 | & encoding); | |
822a6a3d | 3066 | if (encoding == (Dwarf_Word) -1) |
440f755a JS |
3067 | { |
3068 | // clog << "bad type1 " << encoding << " diestr" << endl; | |
dc09353a | 3069 | throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding).c_str(), |
f9bbd346 | 3070 | dwarf_type_name(typedie).c_str()), e->tok); |
440f755a JS |
3071 | } |
3072 | ||
3073 | if (encoding == DW_ATE_float | |
3074 | || encoding == DW_ATE_complex_float | |
3075 | /* XXX || many others? */) | |
3076 | { | |
3077 | // clog << "bad type " << encoding << " diestr" << endl; | |
dc09353a | 3078 | throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding).c_str(), |
f9bbd346 | 3079 | dwarf_type_name(typedie).c_str()), e->tok); |
440f755a JS |
3080 | } |
3081 | } | |
5c7ef8e7 MW |
3082 | // Fallthrough. enumeration_types are always scalar. |
3083 | case DW_TAG_enumeration_type: | |
440f755a JS |
3084 | |
3085 | ty = pe_long; | |
3086 | if (lvalue) | |
6ce303b8 | 3087 | c_translate_store (pool, 1, 0 /* PR9768 */, vardie, typedie, tail, |
0386bbcf | 3088 | "STAP_ARG_value"); |
440f755a | 3089 | else |
6ce303b8 | 3090 | c_translate_fetch (pool, 1, 0 /* PR9768 */, vardie, typedie, tail, |
0386bbcf | 3091 | "STAP_RETVALUE"); |
440f755a JS |
3092 | break; |
3093 | ||
3094 | case DW_TAG_array_type: | |
3095 | case DW_TAG_pointer_type: | |
9c119951 JS |
3096 | case DW_TAG_reference_type: |
3097 | case DW_TAG_rvalue_reference_type: | |
440f755a | 3098 | |
440f755a JS |
3099 | if (lvalue) |
3100 | { | |
3101 | ty = pe_long; | |
3102 | if (typetag == DW_TAG_array_type) | |
dc09353a | 3103 | throw SEMANTIC_ERROR (_("cannot write to array address"), e->tok); |
9c119951 JS |
3104 | if (typetag == DW_TAG_reference_type || |
3105 | typetag == DW_TAG_rvalue_reference_type) | |
dc09353a | 3106 | throw SEMANTIC_ERROR (_("cannot write to reference"), e->tok); |
440f755a JS |
3107 | assert (typetag == DW_TAG_pointer_type); |
3108 | c_translate_pointer_store (pool, 1, 0 /* PR9768 */, typedie, tail, | |
0386bbcf | 3109 | "STAP_ARG_value"); |
440f755a JS |
3110 | } |
3111 | else | |
3112 | { | |
3113 | // We have the pointer: cast it to an integral type via &(*(...)) | |
3114 | ||
3115 | // NB: per bug #1187, at one point char*-like types were | |
3116 | // automagically converted here to systemtap string values. | |
3117 | // For several reasons, this was taken back out, leaving | |
3118 | // pointer-to-string "conversion" (copying) to tapset functions. | |
3119 | ||
3120 | ty = pe_long; | |
3121 | if (typetag == DW_TAG_array_type) | |
3122 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, NULL, 0); | |
3123 | else | |
3124 | c_translate_pointer (pool, 1, 0 /* PR9768 */, typedie, tail); | |
6ce303b8 | 3125 | c_translate_addressof (pool, 1, 0 /* PR9768 */, NULL, NULL, tail, |
0386bbcf | 3126 | "STAP_RETVALUE"); |
440f755a | 3127 | } |
440f755a JS |
3128 | break; |
3129 | } | |
3130 | } | |
3131 | ||
3132 | ||
3133 | string | |
3134 | dwflpp::express_as_string (string prelude, | |
3135 | string postlude, | |
3136 | struct location *head) | |
3137 | { | |
e7899657 FCE |
3138 | size_t bufsz = 0; |
3139 | char *buf = 0; // NB: it would leak to pre-allocate a buffer here | |
440f755a JS |
3140 | FILE *memstream = open_memstream (&buf, &bufsz); |
3141 | assert(memstream); | |
3142 | ||
3143 | fprintf(memstream, "{\n"); | |
3144 | fprintf(memstream, "%s", prelude.c_str()); | |
ebaa9618 | 3145 | |
85dfc5c8 RM |
3146 | unsigned int stack_depth; |
3147 | bool deref = c_emit_location (memstream, head, 1, &stack_depth); | |
ebaa9618 JS |
3148 | |
3149 | // Ensure that DWARF keeps loc2c to a "reasonable" stack size | |
3150 | // 32 intptr_t leads to max 256 bytes on the stack | |
3151 | if (stack_depth > 32) | |
dc09353a | 3152 | throw SEMANTIC_ERROR("oversized DWARF stack"); |
ebaa9618 | 3153 | |
440f755a JS |
3154 | fprintf(memstream, "%s", postlude.c_str()); |
3155 | fprintf(memstream, " goto out;\n"); | |
3156 | ||
3157 | // dummy use of deref_fault label, to disable warning if deref() not used | |
3158 | fprintf(memstream, "if (0) goto deref_fault;\n"); | |
3159 | ||
3160 | // XXX: deref flag not reliable; emit fault label unconditionally | |
3161 | (void) deref; | |
3162 | fprintf(memstream, | |
3163 | "deref_fault:\n" | |
3164 | " goto out;\n"); | |
3165 | fprintf(memstream, "}\n"); | |
3166 | ||
3167 | fclose (memstream); | |
3168 | string result(buf); | |
3169 | free (buf); | |
3170 | return result; | |
3171 | } | |
3172 | ||
228af5c4 MW |
3173 | Dwarf_Addr |
3174 | dwflpp::vardie_from_symtable (Dwarf_Die *vardie, Dwarf_Addr *addr) | |
3175 | { | |
f450a7e3 | 3176 | const char *name = dwarf_linkage_name (vardie) ?: dwarf_diename (vardie); |
b0490786 | 3177 | |
228af5c4 | 3178 | if (sess.verbose > 2) |
f9bbd346 | 3179 | clog << _F("finding symtable address for %s\n", name); |
228af5c4 MW |
3180 | |
3181 | *addr = 0; | |
3182 | int syms = dwfl_module_getsymtab (module); | |
f9bbd346 | 3183 | dwfl_assert (_("Getting symbols"), syms >= 0); |
228af5c4 MW |
3184 | |
3185 | for (int i = 0; *addr == 0 && i < syms; i++) | |
3186 | { | |
3187 | GElf_Sym sym; | |
3188 | GElf_Word shndxp; | |
3189 | const char *symname = dwfl_module_getsym(module, i, &sym, &shndxp); | |
3190 | if (symname | |
3191 | && ! strcmp (name, symname) | |
3192 | && sym.st_shndx != SHN_UNDEF | |
d124d4de MW |
3193 | && (GELF_ST_TYPE (sym.st_info) == STT_NOTYPE // PR13284 |
3194 | || GELF_ST_TYPE (sym.st_info) == STT_OBJECT)) | |
228af5c4 MW |
3195 | *addr = sym.st_value; |
3196 | } | |
3197 | ||
2d7232ca MW |
3198 | // Don't relocate for the kernel, or kernel modules we handle those |
3199 | // specially in emit_address. | |
3200 | if (dwfl_module_relocations (module) == 1 && module_name != TOK_KERNEL) | |
3201 | dwfl_module_relocate_address (module, addr); | |
3202 | ||
228af5c4 | 3203 | if (sess.verbose > 2) |
2a97f50b | 3204 | clog << _F("found %s @%#" PRIx64 "\n", name, *addr); |
228af5c4 MW |
3205 | |
3206 | return *addr; | |
3207 | } | |
440f755a JS |
3208 | |
3209 | string | |
729455a7 | 3210 | dwflpp::literal_stmt_for_local (vector<Dwarf_Die>& scopes, |
440f755a JS |
3211 | Dwarf_Addr pc, |
3212 | string const & local, | |
3213 | const target_symbol *e, | |
3214 | bool lvalue, | |
3215 | exp_type & ty) | |
3216 | { | |
3217 | Dwarf_Die vardie; | |
3218 | Dwarf_Attribute fb_attr_mem, *fb_attr = NULL; | |
3219 | ||
729455a7 | 3220 | fb_attr = find_variable_and_frame_base (scopes, pc, local, e, |
440f755a JS |
3221 | &vardie, &fb_attr_mem); |
3222 | ||
3223 | if (sess.verbose>2) | |
3d9381fc | 3224 | { |
bd1fcbad | 3225 | if (pc) |
3d9381fc MW |
3226 | clog << _F("finding location for local '%s' near address %#" PRIx64 |
3227 | ", module bias %#" PRIx64 "\n", local.c_str(), pc, | |
3228 | module_bias); | |
3229 | else | |
3230 | clog << _F("finding location for global '%s' in CU '%s'\n", | |
bd1fcbad | 3231 | local.c_str(), cu_name().c_str()); |
3d9381fc MW |
3232 | } |
3233 | ||
440f755a | 3234 | |
228af5c4 MW |
3235 | #define obstack_chunk_alloc malloc |
3236 | #define obstack_chunk_free free | |
3237 | ||
3238 | struct obstack pool; | |
3239 | obstack_init (&pool); | |
3240 | struct location *tail = NULL; | |
3241 | ||
3242 | /* Given $foo->bar->baz[NN], translate the location of foo. */ | |
3243 | ||
3244 | struct location *head; | |
3245 | ||
440f755a | 3246 | Dwarf_Attribute attr_mem; |
03ba05b6 MW |
3247 | if (dwarf_attr_integrate (&vardie, DW_AT_const_value, &attr_mem) == NULL |
3248 | && dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL) | |
440f755a | 3249 | { |
228af5c4 | 3250 | Dwarf_Op addr_loc; |
e9483a80 | 3251 | memset(&addr_loc, 0, sizeof(Dwarf_Op)); |
228af5c4 MW |
3252 | addr_loc.atom = DW_OP_addr; |
3253 | // If it is an external variable try the symbol table. PR10622. | |
3254 | if (dwarf_attr_integrate (&vardie, DW_AT_external, &attr_mem) != NULL | |
3255 | && vardie_from_symtable (&vardie, &addr_loc.number) != 0) | |
3256 | { | |
3257 | head = c_translate_location (&pool, &loc2c_error, this, | |
3258 | &loc2c_emit_address, | |
3259 | 1, 0, pc, | |
3260 | NULL, &addr_loc, 1, &tail, NULL, NULL); | |
3261 | } | |
3262 | else | |
dc09353a | 3263 | throw SEMANTIC_ERROR (_F("failed to retrieve location attribute for '%s' [man error::dwarf] (dieoffset: %s)", |
f9bbd346 | 3264 | local.c_str(), lex_cast_hex(dwarf_dieoffset(&vardie)).c_str()), e->tok); |
440f755a | 3265 | } |
228af5c4 | 3266 | else |
00730da1 | 3267 | head = translate_location (&pool, &attr_mem, &vardie, pc, fb_attr, &tail, e); |
440f755a | 3268 | |
6ce303b8 JS |
3269 | /* Translate the ->bar->baz[NN] parts. */ |
3270 | ||
3271 | Dwarf_Die typedie; | |
3272 | if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL) | |
dc09353a | 3273 | throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf] (dieoffset: %s)", local.c_str(), lex_cast_hex(dwarf_dieoffset(&vardie)).c_str()), e->tok); |
440f755a | 3274 | |
6ce303b8 | 3275 | translate_components (&pool, &tail, pc, e, &vardie, &typedie); |
440f755a JS |
3276 | |
3277 | /* Translate the assignment part, either | |
3278 | x = $foo->bar->baz[NN] | |
3279 | or | |
3280 | $foo->bar->baz[NN] = x | |
3281 | */ | |
3282 | ||
3283 | string prelude, postlude; | |
3284 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
6ce303b8 | 3285 | &vardie, &typedie, lvalue, e, |
440f755a JS |
3286 | prelude, postlude, ty); |
3287 | ||
3288 | /* Write the translation to a string. */ | |
e7899657 FCE |
3289 | string result = express_as_string(prelude, postlude, head); |
3290 | obstack_free (&pool, 0); | |
3291 | return result; | |
440f755a JS |
3292 | } |
3293 | ||
5f36109e JS |
3294 | Dwarf_Die* |
3295 | dwflpp::type_die_for_local (vector<Dwarf_Die>& scopes, | |
3296 | Dwarf_Addr pc, | |
3297 | string const & local, | |
3298 | const target_symbol *e, | |
3299 | Dwarf_Die *typedie) | |
3300 | { | |
3301 | Dwarf_Die vardie; | |
3302 | Dwarf_Attribute attr_mem; | |
3303 | ||
3304 | find_variable_and_frame_base (scopes, pc, local, e, &vardie, &attr_mem); | |
3305 | ||
3306 | if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL) | |
dc09353a | 3307 | throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local.c_str()), e->tok); |
5f36109e JS |
3308 | |
3309 | translate_components (NULL, NULL, pc, e, &vardie, typedie); | |
3310 | return typedie; | |
3311 | } | |
3312 | ||
440f755a JS |
3313 | |
3314 | string | |
3315 | dwflpp::literal_stmt_for_return (Dwarf_Die *scope_die, | |
3316 | Dwarf_Addr pc, | |
3317 | const target_symbol *e, | |
3318 | bool lvalue, | |
3319 | exp_type & ty) | |
3320 | { | |
3321 | if (sess.verbose>2) | |
f9bbd346 LB |
3322 | clog << _F("literal_stmt_for_return: finding return value for %s (%s)\n", |
3323 | (dwarf_diename(scope_die) ?: "<unknown>"), (dwarf_diename(cu) ?: "<unknown>")); | |
440f755a JS |
3324 | |
3325 | struct obstack pool; | |
3326 | obstack_init (&pool); | |
3327 | struct location *tail = NULL; | |
3328 | ||
3329 | /* Given $return->bar->baz[NN], translate the location of return. */ | |
3330 | const Dwarf_Op *locops; | |
3331 | int nlocops = dwfl_module_return_value_location (module, scope_die, | |
3332 | &locops); | |
3333 | if (nlocops < 0) | |
3334 | { | |
dc09353a | 3335 | throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)", |
f9bbd346 LB |
3336 | (dwarf_diename(scope_die) ?: "<unknown>"), |
3337 | (dwarf_diename(cu) ?: "<unknown>")), e->tok); | |
440f755a JS |
3338 | } |
3339 | // the function has no return value (e.g. "void" in C) | |
3340 | else if (nlocops == 0) | |
3341 | { | |
dc09353a | 3342 | throw SEMANTIC_ERROR(_F("function %s (%s) has no return value", |
f9bbd346 LB |
3343 | (dwarf_diename(scope_die) ?: "<unknown>"), |
3344 | (dwarf_diename(cu) ?: "<unknown>")), e->tok); | |
440f755a JS |
3345 | } |
3346 | ||
3347 | struct location *head = c_translate_location (&pool, &loc2c_error, this, | |
3348 | &loc2c_emit_address, | |
3349 | 1, 0 /* PR9768 */, | |
24c7957b | 3350 | pc, NULL, locops, nlocops, |
00b01a99 | 3351 | &tail, NULL, NULL); |
440f755a JS |
3352 | |
3353 | /* Translate the ->bar->baz[NN] parts. */ | |
3354 | ||
6ce303b8 JS |
3355 | Dwarf_Die vardie = *scope_die, typedie; |
3356 | if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL) | |
dc09353a | 3357 | throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)", |
f9bbd346 LB |
3358 | (dwarf_diename(&vardie) ?: "<unknown>"), |
3359 | (dwarf_diename(cu) ?: "<unknown>")), e->tok); | |
440f755a | 3360 | |
6ce303b8 | 3361 | translate_components (&pool, &tail, pc, e, &vardie, &typedie); |
440f755a JS |
3362 | |
3363 | /* Translate the assignment part, either | |
3364 | x = $return->bar->baz[NN] | |
3365 | or | |
3366 | $return->bar->baz[NN] = x | |
3367 | */ | |
3368 | ||
3369 | string prelude, postlude; | |
3370 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
6ce303b8 | 3371 | &vardie, &typedie, lvalue, e, |
440f755a JS |
3372 | prelude, postlude, ty); |
3373 | ||
3374 | /* Write the translation to a string. */ | |
e7899657 FCE |
3375 | string result = express_as_string(prelude, postlude, head); |
3376 | obstack_free (&pool, 0); | |
3377 | return result; | |
440f755a JS |
3378 | } |
3379 | ||
5f36109e JS |
3380 | Dwarf_Die* |
3381 | dwflpp::type_die_for_return (Dwarf_Die *scope_die, | |
3382 | Dwarf_Addr pc, | |
3383 | const target_symbol *e, | |
3384 | Dwarf_Die *typedie) | |
3385 | { | |
3386 | Dwarf_Die vardie = *scope_die; | |
3387 | if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL) | |
dc09353a | 3388 | throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)", |
f9bbd346 LB |
3389 | (dwarf_diename(&vardie) ?: "<unknown>"), |
3390 | (dwarf_diename(cu) ?: "<unknown>")), e->tok); | |
5f36109e JS |
3391 | |
3392 | translate_components (NULL, NULL, pc, e, &vardie, typedie); | |
3393 | return typedie; | |
3394 | } | |
3395 | ||
440f755a JS |
3396 | |
3397 | string | |
6ce303b8 | 3398 | dwflpp::literal_stmt_for_pointer (Dwarf_Die *start_typedie, |
440f755a JS |
3399 | const target_symbol *e, |
3400 | bool lvalue, | |
3401 | exp_type & ty) | |
3402 | { | |
3403 | if (sess.verbose>2) | |
f9bbd346 LB |
3404 | clog << _F("literal_stmt_for_pointer: finding value for %s (%s)\n", |
3405 | dwarf_type_name(start_typedie).c_str(), (dwarf_diename(cu) ?: "<unknown>")); | |
440f755a JS |
3406 | |
3407 | struct obstack pool; | |
3408 | obstack_init (&pool); | |
3409 | struct location *head = c_translate_argument (&pool, &loc2c_error, this, | |
3410 | &loc2c_emit_address, | |
0386bbcf | 3411 | 1, "STAP_ARG_pointer"); |
440f755a JS |
3412 | struct location *tail = head; |
3413 | ||
3414 | /* Translate the ->bar->baz[NN] parts. */ | |
3415 | ||
f3b5366d | 3416 | unsigned first = 0; |
6ce303b8 | 3417 | Dwarf_Die typedie = *start_typedie, vardie = typedie; |
f3b5366d | 3418 | |
0386bbcf SM |
3419 | /* As a special case when typedie is not an array or pointer, we can |
3420 | * allow array indexing on STAP_ARG_pointer instead (since we do | |
3421 | * know the pointee type and can determine its size). PR11556. */ | |
f3b5366d JS |
3422 | const target_symbol::component* c = |
3423 | e->components.empty() ? NULL : &e->components[0]; | |
3424 | if (c && (c->type == target_symbol::comp_literal_array_index || | |
3425 | c->type == target_symbol::comp_expression_array_index)) | |
3426 | { | |
3427 | resolve_unqualified_inner_typedie (&typedie, &typedie, e); | |
3428 | int typetag = dwarf_tag (&typedie); | |
3429 | if (typetag != DW_TAG_pointer_type && | |
3430 | typetag != DW_TAG_array_type) | |
3431 | { | |
3432 | if (c->type == target_symbol::comp_literal_array_index) | |
3433 | c_translate_array_pointer (&pool, 1, &typedie, &tail, NULL, c->num_index); | |
3434 | else | |
0386bbcf | 3435 | c_translate_array_pointer (&pool, 1, &typedie, &tail, "STAP_ARG_index0", 0); |
f3b5366d JS |
3436 | ++first; |
3437 | } | |
3438 | } | |
3439 | ||
3440 | /* Now translate the rest normally. */ | |
3441 | ||
3442 | translate_components (&pool, &tail, 0, e, &vardie, &typedie, first); | |
440f755a JS |
3443 | |
3444 | /* Translate the assignment part, either | |
0386bbcf | 3445 | x = (STAP_ARG_pointer)->bar->baz[NN] |
440f755a | 3446 | or |
0386bbcf | 3447 | (STAP_ARG_pointer)->bar->baz[NN] = x |
440f755a JS |
3448 | */ |
3449 | ||
3450 | string prelude, postlude; | |
3451 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
6ce303b8 | 3452 | &vardie, &typedie, lvalue, e, |
440f755a JS |
3453 | prelude, postlude, ty); |
3454 | ||
3455 | /* Write the translation to a string. */ | |
e7899657 FCE |
3456 | string result = express_as_string(prelude, postlude, head); |
3457 | obstack_free (&pool, 0); | |
3458 | return result; | |
440f755a JS |
3459 | } |
3460 | ||
5f36109e JS |
3461 | Dwarf_Die* |
3462 | dwflpp::type_die_for_pointer (Dwarf_Die *start_typedie, | |
3463 | const target_symbol *e, | |
3464 | Dwarf_Die *typedie) | |
3465 | { | |
3466 | unsigned first = 0; | |
3467 | *typedie = *start_typedie; | |
3468 | Dwarf_Die vardie = *typedie; | |
3469 | ||
3470 | /* Handle the same PR11556 case as above. */ | |
3471 | const target_symbol::component* c = | |
3472 | e->components.empty() ? NULL : &e->components[0]; | |
3473 | if (c && (c->type == target_symbol::comp_literal_array_index || | |
3474 | c->type == target_symbol::comp_expression_array_index)) | |
3475 | { | |
3476 | resolve_unqualified_inner_typedie (typedie, typedie, e); | |
3477 | int typetag = dwarf_tag (typedie); | |
3478 | if (typetag != DW_TAG_pointer_type && | |
3479 | typetag != DW_TAG_array_type) | |
3480 | ++first; | |
3481 | } | |
3482 | ||
3483 | translate_components (NULL, NULL, 0, e, &vardie, typedie, first); | |
3484 | return typedie; | |
3485 | } | |
3486 | ||
440f755a | 3487 | |
27646582 JS |
3488 | static bool |
3489 | in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr) | |
3490 | { | |
3491 | if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0) | |
3492 | { | |
3493 | // If the probe point address is anywhere in the __kprobes | |
3494 | // address range, we can't use this probe point. | |
3495 | if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end) | |
3496 | return true; | |
3497 | } | |
3498 | return false; | |
3499 | } | |
3500 | ||
3501 | ||
3502 | bool | |
3503 | dwflpp::blacklisted_p(const string& funcname, | |
3504 | const string& filename, | |
3505 | int, | |
3506 | const string& module, | |
27646582 JS |
3507 | Dwarf_Addr addr, |
3508 | bool has_return) | |
3509 | { | |
3510 | if (!blacklist_enabled) | |
3511 | return false; // no blacklist for userspace | |
3512 | ||
6ac012fc FCE |
3513 | bool blacklisted = false; |
3514 | ||
3515 | // check against section blacklist | |
789448a3 | 3516 | string section = get_blacklist_section(addr); |
ac08441a FCE |
3517 | // PR6503: modules don't need special init/exit treatment |
3518 | if (module == TOK_KERNEL && !regexec (&blacklist_section, section.c_str(), 0, NULL, 0)) | |
27646582 | 3519 | { |
6ac012fc | 3520 | blacklisted = true; |
27646582 | 3521 | if (sess.verbose>1) |
6ac012fc | 3522 | clog << _(" init/exit"); |
27646582 JS |
3523 | } |
3524 | ||
3525 | // Check for function marked '__kprobes'. | |
3526 | if (module == TOK_KERNEL && in_kprobes_function(sess, addr)) | |
3527 | { | |
6ac012fc | 3528 | blacklisted = true; |
27646582 | 3529 | if (sess.verbose>1) |
6ac012fc | 3530 | clog << _(" __kprobes"); |
27646582 JS |
3531 | } |
3532 | ||
6ac012fc | 3533 | // Check probe point against file/function blacklists. |
27646582 JS |
3534 | int goodfn = regexec (&blacklist_func, funcname.c_str(), 0, NULL, 0); |
3535 | if (has_return) | |
3536 | goodfn = goodfn && regexec (&blacklist_func_ret, funcname.c_str(), 0, NULL, 0); | |
3537 | int goodfile = regexec (&blacklist_file, filename.c_str(), 0, NULL, 0); | |
3538 | ||
3539 | if (! (goodfn && goodfile)) | |
3540 | { | |
6ac012fc FCE |
3541 | blacklisted = true; |
3542 | if (sess.verbose>1) | |
3543 | clog << _(" file/function blacklist"); | |
27646582 JS |
3544 | } |
3545 | ||
6ac012fc FCE |
3546 | if (sess.guru_mode && blacklisted) |
3547 | { | |
3548 | blacklisted = false; | |
3549 | if (sess.verbose>1) | |
3550 | clog << _(" - not skipped (guru mode enabled)"); | |
3551 | } | |
3552 | ||
3553 | if (blacklisted && sess.verbose>1) | |
3554 | clog << _(" - skipped"); | |
3555 | ||
27646582 | 3556 | // This probe point is not blacklisted. |
6ac012fc | 3557 | return blacklisted; |
27646582 JS |
3558 | } |
3559 | ||
3560 | ||
3561 | void | |
3562 | dwflpp::build_blacklist() | |
3563 | { | |
3564 | // We build up the regexps in these strings | |
3565 | ||
3566 | // Add ^ anchors at the front; $ will be added just before regcomp. | |
3567 | ||
3568 | string blfn = "^("; | |
3569 | string blfn_ret = "^("; | |
3570 | string blfile = "^("; | |
178ac3f6 JS |
3571 | string blsection = "^("; |
3572 | ||
3573 | blsection += "\\.init\\."; // first alternative, no "|" | |
3574 | blsection += "|\\.exit\\."; | |
3575 | blsection += "|\\.devinit\\."; | |
3576 | blsection += "|\\.devexit\\."; | |
3577 | blsection += "|\\.cpuinit\\."; | |
3578 | blsection += "|\\.cpuexit\\."; | |
3579 | blsection += "|\\.meminit\\."; | |
3580 | blsection += "|\\.memexit\\."; | |
27646582 | 3581 | |
44a7e76a MW |
3582 | /* NOTE all include/asm .h blfile patterns might need "full path" |
3583 | so prefix those with '.*' - see PR13108 and PR13112. */ | |
b969d16b JS |
3584 | blfile += "kernel/kprobes\\.c"; // first alternative, no "|" |
3585 | blfile += "|arch/.*/kernel/kprobes\\.c"; | |
44a7e76a | 3586 | blfile += "|.*/include/asm/io\\.h"; |
4a507da2 | 3587 | blfile += "|.*/include/asm/io_64\\.h"; |
44a7e76a | 3588 | blfile += "|.*/include/asm/bitops\\.h"; |
b969d16b | 3589 | blfile += "|drivers/ide/ide-iops\\.c"; |
1b438943 MW |
3590 | // paravirt ops |
3591 | blfile += "|arch/.*/kernel/paravirt\\.c"; | |
4a507da2 | 3592 | blfile += "|.*/include/asm/paravirt\\.h"; |
27646582 JS |
3593 | |
3594 | // XXX: it would be nice if these blacklisted functions were pulled | |
3595 | // in dynamically, instead of being statically defined here. | |
3596 | // Perhaps it could be populated from script files. A "noprobe | |
3597 | // kernel.function("...")" construct might do the trick. | |
3598 | ||
3599 | // Most of these are marked __kprobes in newer kernels. We list | |
3600 | // them here (anyway) so the translator can block them on older | |
3601 | // kernels that don't have the __kprobes function decorator. This | |
3602 | // also allows detection of problems at translate- rather than | |
3603 | // run-time. | |
3604 | ||
3605 | blfn += "atomic_notifier_call_chain"; // first blfn; no "|" | |
3606 | blfn += "|default_do_nmi"; | |
3607 | blfn += "|__die"; | |
3608 | blfn += "|die_nmi"; | |
3609 | blfn += "|do_debug"; | |
3610 | blfn += "|do_general_protection"; | |
3611 | blfn += "|do_int3"; | |
3612 | blfn += "|do_IRQ"; | |
3613 | blfn += "|do_page_fault"; | |
3614 | blfn += "|do_sparc64_fault"; | |
3615 | blfn += "|do_trap"; | |
3616 | blfn += "|dummy_nmi_callback"; | |
3617 | blfn += "|flush_icache_range"; | |
3618 | blfn += "|ia64_bad_break"; | |
3619 | blfn += "|ia64_do_page_fault"; | |
3620 | blfn += "|ia64_fault"; | |
3621 | blfn += "|io_check_error"; | |
3622 | blfn += "|mem_parity_error"; | |
3623 | blfn += "|nmi_watchdog_tick"; | |
3624 | blfn += "|notifier_call_chain"; | |
3625 | blfn += "|oops_begin"; | |
3626 | blfn += "|oops_end"; | |
3627 | blfn += "|program_check_exception"; | |
3628 | blfn += "|single_step_exception"; | |
3629 | blfn += "|sync_regs"; | |
3630 | blfn += "|unhandled_fault"; | |
3631 | blfn += "|unknown_nmi_error"; | |
48aad766 FCE |
3632 | blfn += "|xen_[gs]et_debugreg"; |
3633 | blfn += "|xen_irq_.*"; | |
3634 | blfn += "|xen_.*_fl_direct.*"; | |
10d96538 TJL |
3635 | blfn += "|check_events"; |
3636 | blfn += "|xen_adjust_exception_frame"; | |
48aad766 FCE |
3637 | blfn += "|xen_iret.*"; |
3638 | blfn += "|xen_sysret64.*"; | |
3639 | blfn += "|test_ti_thread_flag.*"; | |
10d96538 TJL |
3640 | blfn += "|inat_get_opcode_attribute"; |
3641 | blfn += "|system_call_after_swapgs"; | |
5db0d1ab TJL |
3642 | blfn += "|HYPERVISOR_[gs]et_debugreg"; |
3643 | blfn += "|HYPERVISOR_event_channel_op"; | |
3644 | blfn += "|hash_64"; | |
3645 | blfn += "|hash_ptr"; | |
3646 | blfn += "|native_set_pte"; | |
27646582 JS |
3647 | |
3648 | // Lots of locks | |
65d79153 MW |
3649 | blfn += "|.*raw_.*_lock.*"; |
3650 | blfn += "|.*raw_.*_unlock.*"; | |
3651 | blfn += "|.*raw_.*_trylock.*"; | |
3652 | blfn += "|.*read_lock.*"; | |
3653 | blfn += "|.*read_unlock.*"; | |
3654 | blfn += "|.*read_trylock.*"; | |
3655 | blfn += "|.*write_lock.*"; | |
3656 | blfn += "|.*write_unlock.*"; | |
3657 | blfn += "|.*write_trylock.*"; | |
3658 | blfn += "|.*write_seqlock.*"; | |
3659 | blfn += "|.*write_sequnlock.*"; | |
3660 | blfn += "|.*spin_lock.*"; | |
3661 | blfn += "|.*spin_unlock.*"; | |
3662 | blfn += "|.*spin_trylock.*"; | |
3663 | blfn += "|.*spin_is_locked.*"; | |
3664 | blfn += "|rwsem_.*lock.*"; | |
27646582 JS |
3665 | blfn += "|.*mutex_.*lock.*"; |
3666 | blfn += "|raw_.*"; | |
27646582 JS |
3667 | |
3668 | // atomic functions | |
3669 | blfn += "|atomic_.*"; | |
3670 | blfn += "|atomic64_.*"; | |
3671 | ||
3672 | // few other problematic cases | |
3673 | blfn += "|get_bh"; | |
3674 | blfn += "|put_bh"; | |
3675 | ||
3676 | // Experimental | |
3677 | blfn += "|.*apic.*|.*APIC.*"; | |
3678 | blfn += "|.*softirq.*"; | |
3679 | blfn += "|.*IRQ.*"; | |
3680 | blfn += "|.*_intr.*"; | |
3681 | blfn += "|__delay"; | |
3682 | blfn += "|.*kernel_text.*"; | |
3683 | blfn += "|get_current"; | |
3684 | blfn += "|current_.*"; | |
3685 | blfn += "|.*exception_tables.*"; | |
3686 | blfn += "|.*setup_rt_frame.*"; | |
3687 | ||
3688 | // PR 5759, CONFIG_PREEMPT kernels | |
3689 | blfn += "|.*preempt_count.*"; | |
3690 | blfn += "|preempt_schedule"; | |
3691 | ||
3692 | // These functions don't return, so return probes would never be recovered | |
3693 | blfn_ret += "do_exit"; // no "|" | |
3694 | blfn_ret += "|sys_exit"; | |
3695 | blfn_ret += "|sys_exit_group"; | |
3696 | ||
3697 | // __switch_to changes "current" on x86_64 and i686, so return probes | |
3698 | // would cause kernel panic, and it is marked as "__kprobes" on x86_64 | |
3699 | if (sess.architecture == "x86_64") | |
3700 | blfn += "|__switch_to"; | |
3701 | if (sess.architecture == "i686") | |
3702 | blfn_ret += "|__switch_to"; | |
3703 | ||
08ee70c3 FCE |
3704 | // RHEL6 pre-beta 2.6.32-19.el6 |
3705 | blfn += "|special_mapping_.*"; | |
2fd5495a FCE |
3706 | blfn += "|.*_pte_.*"; // or "|smaps_pte_range"; |
3707 | blfile += "|fs/seq_file\\.c"; | |
08ee70c3 | 3708 | |
27646582 JS |
3709 | blfn += ")$"; |
3710 | blfn_ret += ")$"; | |
3711 | blfile += ")$"; | |
178ac3f6 | 3712 | blsection += ")"; // NB: no $, sections match just the beginning |
27646582 JS |
3713 | |
3714 | if (sess.verbose > 2) | |
3715 | { | |
f9bbd346 | 3716 | clog << _("blacklist regexps:") << endl; |
27646582 JS |
3717 | clog << "blfn: " << blfn << endl; |
3718 | clog << "blfn_ret: " << blfn_ret << endl; | |
3719 | clog << "blfile: " << blfile << endl; | |
178ac3f6 | 3720 | clog << "blsection: " << blsection << endl; |
27646582 JS |
3721 | } |
3722 | ||
3723 | int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED); | |
dc09353a | 3724 | if (rc) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed")); |
27646582 | 3725 | rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED); |
dc09353a | 3726 | if (rc) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed")); |
27646582 | 3727 | rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED); |
dc09353a | 3728 | if (rc) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed")); |
178ac3f6 | 3729 | rc = regcomp (& blacklist_section, blsection.c_str(), REG_NOSUB|REG_EXTENDED); |
dc09353a | 3730 | if (rc) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed")); |
27646582 JS |
3731 | |
3732 | blacklist_enabled = true; | |
3733 | } | |
3734 | ||
3735 | ||
3736 | string | |
3737 | dwflpp::get_blacklist_section(Dwarf_Addr addr) | |
3738 | { | |
3739 | string blacklist_section; | |
3740 | Dwarf_Addr bias; | |
3741 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
3742 | // because dwfl_module_getelf can force costly section relocations | |
3743 | // we don't really need, while either will do for this purpose. | |
3744 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (module, &bias)) | |
3745 | ?: dwfl_module_getelf (module, &bias)); | |
3746 | ||
3747 | Dwarf_Addr offset = addr - bias; | |
3748 | if (elf) | |
3749 | { | |
3750 | Elf_Scn* scn = 0; | |
3751 | size_t shstrndx; | |
fcc30d6d | 3752 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); |
27646582 JS |
3753 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
3754 | { | |
3755 | GElf_Shdr shdr_mem; | |
3756 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); | |
3757 | if (! shdr) | |
3758 | continue; // XXX error? | |
3759 | ||
3760 | if (!(shdr->sh_flags & SHF_ALLOC)) | |
3761 | continue; | |
3762 | ||
3763 | GElf_Addr start = shdr->sh_addr; | |
3764 | GElf_Addr end = start + shdr->sh_size; | |
3765 | if (! (offset >= start && offset < end)) | |
3766 | continue; | |
3767 | ||
3768 | blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name); | |
3769 | break; | |
3770 | } | |
3771 | } | |
3772 | return blacklist_section; | |
3773 | } | |
3774 | ||
3775 | ||
fea74777 SC |
3776 | /* Find the section named 'section_name' in the current module |
3777 | * returning the section header using 'shdr_mem' */ | |
3778 | ||
3779 | GElf_Shdr * | |
448a86b7 | 3780 | dwflpp::get_section(string section_name, GElf_Shdr *shdr_mem, Elf **elf_ret) |
fea74777 SC |
3781 | { |
3782 | GElf_Shdr *shdr = NULL; | |
3783 | Elf* elf; | |
3784 | Dwarf_Addr bias; | |
3785 | size_t shstrndx; | |
3786 | ||
3787 | // Explicitly look in the main elf file first. | |
3788 | elf = dwfl_module_getelf (module, &bias); | |
3789 | Elf_Scn *probe_scn = NULL; | |
3790 | ||
3791 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); | |
3792 | ||
3793 | bool have_section = false; | |
3794 | ||
3795 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
3796 | { | |
3797 | shdr = gelf_getshdr (probe_scn, shdr_mem); | |
3798 | assert (shdr != NULL); | |
3799 | ||
3800 | if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name) | |
3801 | { | |
3802 | have_section = true; | |
3803 | break; | |
3804 | } | |
3805 | } | |
3806 | ||
3807 | // Older versions may put the section in the debuginfo dwarf file, | |
3808 | // so check if it actually exists, if not take a look in the debuginfo file | |
3809 | if (! have_section || (have_section && shdr->sh_type == SHT_NOBITS)) | |
3810 | { | |
3811 | elf = dwarf_getelf (dwfl_module_getdwarf (module, &bias)); | |
3812 | if (! elf) | |
448a86b7 | 3813 | return NULL; |
fea74777 SC |
3814 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); |
3815 | probe_scn = NULL; | |
3816 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
3817 | { | |
3818 | shdr = gelf_getshdr (probe_scn, shdr_mem); | |
3819 | if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name) | |
3820 | { | |
3821 | have_section = true; | |
3822 | break; | |
3823 | } | |
3824 | } | |
3825 | } | |
3826 | ||
3827 | if (!have_section) | |
3828 | return NULL; | |
448a86b7 JS |
3829 | |
3830 | if (elf_ret) | |
3831 | *elf_ret = elf; | |
3832 | return shdr; | |
fea74777 SC |
3833 | } |
3834 | ||
3835 | ||
27646582 | 3836 | Dwarf_Addr |
789448a3 | 3837 | dwflpp::relocate_address(Dwarf_Addr dw_addr, string& reloc_section) |
27646582 | 3838 | { |
d2309c6c MW |
3839 | // PR10273 |
3840 | // libdw address, so adjust for bias gotten from dwfl_module_getdwarf | |
3841 | Dwarf_Addr reloc_addr = dw_addr + module_bias; | |
27646582 JS |
3842 | if (!module) |
3843 | { | |
3844 | assert(module_name == TOK_KERNEL); | |
3845 | reloc_section = ""; | |
27646582 JS |
3846 | } |
3847 | else if (dwfl_module_relocations (module) > 0) | |
3848 | { | |
3849 | // This is a relocatable module; libdwfl already knows its | |
3850 | // sections, so we can relativize addr. | |
3851 | int idx = dwfl_module_relocate_address (module, &reloc_addr); | |
3852 | const char* r_s = dwfl_module_relocation_info (module, idx, NULL); | |
3853 | if (r_s) | |
3854 | reloc_section = r_s; | |
27646582 JS |
3855 | |
3856 | if (reloc_section == "" && dwfl_module_relocations (module) == 1) | |
27646582 | 3857 | reloc_section = ".dynamic"; |
27646582 JS |
3858 | } |
3859 | else | |
789448a3 | 3860 | reloc_section = ".absolute"; |
27646582 JS |
3861 | return reloc_addr; |
3862 | } | |
3863 | ||
88eaee9f MW |
3864 | /* Returns the call frame address operations for the given program counter |
3865 | * in the libdw address space. | |
3866 | */ | |
00b01a99 MW |
3867 | Dwarf_Op * |
3868 | dwflpp::get_cfa_ops (Dwarf_Addr pc) | |
3869 | { | |
3870 | Dwarf_Op *cfa_ops = NULL; | |
3871 | ||
88eaee9f MW |
3872 | if (sess.verbose > 2) |
3873 | clog << "get_cfa_ops @0x" << hex << pc << dec | |
3874 | << ", module_start @0x" << hex << module_start << dec << endl; | |
3875 | ||
6a38401c | 3876 | // Try debug_frame first, then fall back on eh_frame. |
87748e2b MW |
3877 | size_t cfa_nops = 0; |
3878 | Dwarf_Addr bias = 0; | |
3879 | Dwarf_Frame *frame = NULL; | |
00b01a99 MW |
3880 | Dwarf_CFI *cfi = dwfl_module_dwarf_cfi (module, &bias); |
3881 | if (cfi != NULL) | |
3882 | { | |
88eaee9f MW |
3883 | if (sess.verbose > 3) |
3884 | clog << "got dwarf cfi bias: 0x" << hex << bias << dec << endl; | |
87748e2b | 3885 | if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0) |
97f529ab | 3886 | dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops); |
88eaee9f MW |
3887 | else if (sess.verbose > 3) |
3888 | clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl; | |
00b01a99 | 3889 | } |
88eaee9f MW |
3890 | else if (sess.verbose > 3) |
3891 | clog << "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl; | |
3892 | ||
00b01a99 MW |
3893 | if (cfa_ops == NULL) |
3894 | { | |
3895 | cfi = dwfl_module_eh_cfi (module, &bias); | |
3896 | if (cfi != NULL) | |
3897 | { | |
88eaee9f MW |
3898 | if (sess.verbose > 3) |
3899 | clog << "got eh cfi bias: 0x" << hex << bias << dec << endl; | |
00b01a99 | 3900 | Dwarf_Frame *frame = NULL; |
87748e2b | 3901 | if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0) |
97f529ab | 3902 | dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops); |
88eaee9f MW |
3903 | else if (sess.verbose > 3) |
3904 | clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl; | |
00b01a99 | 3905 | } |
88eaee9f MW |
3906 | else if (sess.verbose > 3) |
3907 | clog << "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl; | |
3908 | ||
00b01a99 | 3909 | } |
00b01a99 | 3910 | |
88eaee9f | 3911 | if (sess.verbose > 2) |
87748e2b MW |
3912 | { |
3913 | if (cfa_ops == NULL) | |
f9bbd346 | 3914 | clog << _("not found cfa") << endl; |
87748e2b MW |
3915 | else |
3916 | { | |
3917 | Dwarf_Addr frame_start, frame_end; | |
3918 | bool frame_signalp; | |
3919 | int info = dwarf_frame_info (frame, &frame_start, &frame_end, | |
3920 | &frame_signalp); | |
2a97f50b | 3921 | clog << _F("found cfa, info: %d [start: %#" PRIx64 ", end: %#" PRIx64 |
f9bbd346 | 3922 | ", nops: %zu", info, frame_start, frame_end, cfa_nops) << endl; |
87748e2b MW |
3923 | } |
3924 | } | |
88eaee9f | 3925 | |
00b01a99 MW |
3926 | return cfa_ops; |
3927 | } | |
c8ad0687 | 3928 | |
2eb99c62 CM |
3929 | int |
3930 | dwflpp::add_module_build_id_to_hash (Dwfl_Module *m, | |
3931 | void **userdata __attribute__ ((unused)), | |
3932 | const char *name, | |
3933 | Dwarf_Addr base, | |
3934 | void *arg) | |
3935 | { | |
3936 | string modname = name; | |
3937 | systemtap_session * s = (systemtap_session *)arg; | |
3938 | if (pending_interrupts) | |
3939 | return DWARF_CB_ABORT; | |
3940 | ||
3941 | // Extract the build ID | |
3942 | const unsigned char *bits; | |
3943 | GElf_Addr vaddr; | |
3944 | int bits_length = dwfl_module_build_id(m, &bits, &vaddr); | |
3945 | if(bits_length > 0) | |
3946 | { | |
3947 | // Convert the binary bits to a hex string | |
3948 | string hex = hex_dump(bits, bits_length); | |
3949 | ||
3950 | // Store the build ID in the session | |
3951 | s->build_ids.push_back(hex); | |
3952 | } | |
3953 | ||
3954 | return DWARF_CB_OK; | |
3955 | } | |
3956 | ||
45b02a36 FCE |
3957 | |
3958 | ||
3959 | // Perform PR15123 heuristic for given variable at given address. | |
3960 | // Return alternate pc address to do location-list lookup at, or 0 if | |
3961 | // inapplicable. | |
3962 | // | |
3963 | Dwarf_Addr | |
3964 | dwflpp::pr15123_retry_addr (Dwarf_Addr pc, Dwarf_Die* die) | |
3965 | { | |
3966 | // For PR15123, we'd like to detect the situation where the | |
3967 | // incoming PC may point to a couple-of-byte instruction | |
3968 | // sequence that gcc emits for CFLAGS=-mfentry, and where | |
3969 | // context variables are in fact available throughout, *but* due | |
3970 | // to the bug, the dwarf debuginfo location-list only starts a | |
3971 | // few instructions later. Prologue searching does not resolve | |
3972 | // this as a line-record is in place at the -mfentry prologue. | |
3973 | // | |
3974 | // Detecting this is complicated because ... | |
3975 | // - we only want to do this if -mfentry was actually used | |
3976 | // - if <pc> points to the a function entry point | |
3977 | // - if the architecture is familiar enough that we can have a | |
3978 | // hard-coded constant to skip over the prologue. | |
3979 | // | |
98df4dae VB |
3980 | // Otherwise, we could give a false-positive - return corrupted |
3981 | // data. | |
3982 | // | |
3983 | // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches | |
3984 | // was used. Without it, set the PR15123_ASSUME_MFENTRY environment | |
3985 | // variable to override the -mfentry test. | |
45b02a36 FCE |
3986 | |
3987 | if (getenv ("PR15123_DISABLE")) | |
3988 | return 0; | |
3989 | ||
3990 | Dwarf_Die cudie; | |
3991 | Dwarf_Attribute cudie_producer; | |
3992 | dwarf_diecu (die, &cudie, NULL, NULL); | |
3993 | if (! dwarf_attr_integrate(&cudie, DW_AT_producer, &cudie_producer)) | |
3994 | return 0; | |
3995 | ||
98df4dae VB |
3996 | if (!getenv ("PR15123_ASSUME_MFENTRY")) { |
3997 | const char* producer = dwarf_formstring(&cudie_producer); | |
3998 | if (!producer) | |
3999 | return 0; | |
4000 | if (! strstr(producer, "-mfentry")) | |
4001 | return 0; | |
4002 | } | |
45b02a36 FCE |
4003 | |
4004 | // Determine if this pc maps to the beginning of a | |
4005 | // real function (not some inlined doppelganger. This | |
4006 | // is made tricker by this->function may not be | |
4007 | // pointing at the right DIE (say e.g. stap encountered | |
4008 | // the inlined copy first, so was focus_on_function'd). | |
4009 | vector<Dwarf_Die> scopes = getscopes(pc); | |
4010 | if (scopes.size() == 0) | |
4011 | return 0; | |
4012 | ||
4013 | Dwarf_Die outer_function_die = scopes[0]; | |
4014 | Dwarf_Addr entrypc; | |
1bbf3f90 JS |
4015 | if (!die_entrypc(& outer_function_die, &entrypc) || entrypc != pc) |
4016 | return 0; // (will fail on retry, so we won't loop more than once) | |
45b02a36 FCE |
4017 | |
4018 | if (sess.architecture == "i386" || | |
4019 | sess.architecture == "x86_64") { | |
4020 | /* pull the trigger */ | |
4021 | if (sess.verbose > 2) | |
4022 | clog << _("retrying variable location-list lookup at address pc+5\n"); | |
4023 | return pc + 5; | |
4024 | } | |
4025 | ||
4026 | return 0; | |
4027 | } | |
4028 | ||
c5810d31 LB |
4029 | bool |
4030 | dwflpp::has_gnu_debugdata () | |
4031 | { | |
4032 | Dwarf_Addr load_addr; | |
4033 | // Note we really want the actual elf file, not the dwarf .debug file. | |
4034 | Elf* elf = dwfl_module_getelf (module, &load_addr); | |
4035 | size_t shstrndx; | |
4036 | assert (elf_getshdrstrndx (elf, &shstrndx) >= 0); | |
4037 | ||
4038 | // Get the gnu_debugdata section header | |
4039 | Elf_Scn *scn = NULL; | |
4040 | GElf_Shdr *gnu_debugdata_shdr = NULL; | |
4041 | GElf_Shdr gnu_debugdata_shdr_mem; | |
4042 | while ((scn = elf_nextscn (elf, scn))) | |
4043 | { | |
4044 | gnu_debugdata_shdr = gelf_getshdr (scn, &gnu_debugdata_shdr_mem); | |
4045 | assert (gnu_debugdata_shdr != NULL); | |
4046 | if (strcmp (elf_strptr (elf, shstrndx, gnu_debugdata_shdr->sh_name), ".gnu_debugdata") == 0) | |
4047 | return true; | |
4048 | } | |
4049 | return false; | |
4050 | } | |
4051 | ||
45b02a36 | 4052 | |
440f755a | 4053 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |