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