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