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