arm64 regtest: bug489338 testcase needs to link with maths library on Linux
[valgrind.git] / coregrind / m_debuginfo / debuginfo.c
blobcc79429bd6016bd8ada99e113e42a357d7d9766b
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Top level management of symbols and debugging information. ---*/
5 /*--- debuginfo.c ---*/
6 /*--------------------------------------------------------------------*/
8 /*
9 This file is part of Valgrind, a dynamic binary instrumentation
10 framework.
12 Copyright (C) 2000-2017 Julian Seward
13 jseward@acm.org
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see <http://www.gnu.org/licenses/>.
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_threadstate.h"
34 #include "pub_core_debuginfo.h" /* self */
35 #include "pub_core_debuglog.h"
36 #include "pub_core_demangle.h"
37 #include "pub_core_libcbase.h"
38 #include "pub_core_libcassert.h"
39 #include "pub_core_libcprint.h"
40 #include "pub_core_libcfile.h"
41 #include "pub_core_libcproc.h" // VG_(getenv)
42 #include "pub_core_rangemap.h"
43 #include "pub_core_seqmatch.h"
44 #include "pub_core_options.h"
45 #include "pub_core_redir.h" // VG_(redir_notify_{new,delete}_SegInfo)
46 #include "pub_core_aspacemgr.h"
47 #include "pub_core_machine.h" // VG_PLAT_USES_PPCTOC
48 #include "pub_core_xarray.h"
49 #include "pub_core_oset.h"
50 #include "pub_core_execontext.h"
51 #include "pub_core_stacktrace.h" // VG_(get_StackTrace) XXX: circular dependency
52 #include "pub_core_ume.h"
54 #include "priv_misc.h" /* dinfo_zalloc/free */
55 #include "priv_image.h"
56 #include "priv_d3basics.h" /* ML_(pp_GX) */
57 #include "priv_tytypes.h"
58 #include "priv_storage.h"
59 #include "priv_readdwarf.h"
60 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
61 # include "priv_readelf.h"
62 # include "priv_readdwarf3.h"
63 # include "priv_readpdb.h"
64 #elif defined(VGO_darwin)
65 # include "priv_readmacho.h"
66 # include "priv_readpdb.h"
67 #endif
70 /* Set this to 1 to enable somewhat minimal debug printing for the
71 debuginfo-epoch machinery. */
72 #define DEBUG_EPOCHS 0
75 /*------------------------------------------------------------*/
76 /*--- The _svma / _avma / _image / _bias naming scheme ---*/
77 /*------------------------------------------------------------*/
79 /* JRS 11 Jan 07: I find the different kinds of addresses involved in
80 debuginfo reading confusing. Recently I arrived at some
81 terminology which makes it clearer (to me, at least). There are 3
82 kinds of address used in the debuginfo reading process:
84 stated VMAs - the address where (eg) a .so says a symbol is, that
85 is, what it tells you if you consider the .so in
86 isolation
88 actual VMAs - the address where (eg) said symbol really wound up
89 after the .so was mapped into memory
91 image addresses - pointers into the copy of the .so (etc)
92 transiently mmaped aboard whilst we read its info
94 Additionally I use the term 'bias' to denote the difference
95 between stated and actual VMAs for a given entity.
97 This terminology is not used consistently, but a start has been
98 made. readelf.c and the call-frame info reader in readdwarf.c now
99 use it. Specifically, various variables and structure fields have
100 been annotated with _avma / _svma / _image / _bias. In places _img
101 is used instead of _image for the sake of brevity.
105 /*------------------------------------------------------------*/
106 /*--- fwdses ---*/
107 /*------------------------------------------------------------*/
109 static void caches__invalidate (void);
112 /*------------------------------------------------------------*/
113 /*--- Epochs ---*/
114 /*------------------------------------------------------------*/
116 /* The DebugInfo epoch is incremented every time we either load debuginfo in
117 response to an object mapping, or an existing DebugInfo becomes
118 non-current (or will be discarded) due to an object unmap. By storing,
119 in each DebugInfo, the first and last epoch for which it is valid, we can
120 unambiguously identify the set of DebugInfos which should be used to
121 provide metadata for a code or data address, provided we know the epoch
122 to which that address pertains.
124 Note, this isn't the same as the "handle_counter" below. That only
125 advances when new DebugInfos are created. "current_epoch" advances both
126 at DebugInfo created and destruction-or-making-non-current.
129 // The value zero is reserved for indicating an invalid epoch number.
130 static UInt current_epoch = 1;
132 inline DiEpoch VG_(current_DiEpoch) ( void ) {
133 DiEpoch dep; dep.n = current_epoch; return dep;
136 static void advance_current_DiEpoch ( const HChar* msg ) {
137 current_epoch++;
138 if (DEBUG_EPOCHS)
139 VG_(printf)("Advancing current epoch to %u due to %s\n",
140 current_epoch, msg);
143 static inline Bool eq_DiEpoch ( DiEpoch dep1, DiEpoch dep2 ) {
144 return dep1.n == dep2.n && /*neither is invalid*/dep1.n != 0;
147 // Is this DebugInfo currently "allocated" (pre-use state, only FSM active) ?
148 static inline Bool is_DebugInfo_allocated ( const DebugInfo* di )
150 if (is_DiEpoch_INVALID(di->first_epoch)
151 && is_DiEpoch_INVALID(di->last_epoch)) {
152 return True;
153 } else {
154 return False;
158 // Is this DebugInfo currently "active" (valid for the current epoch) ?
159 static inline Bool is_DebugInfo_active ( const DebugInfo* di )
161 if (!is_DiEpoch_INVALID(di->first_epoch)
162 && is_DiEpoch_INVALID(di->last_epoch)) {
163 // Yes it is active. Sanity check ..
164 vg_assert(di->first_epoch.n <= current_epoch);
165 return True;
166 } else {
167 return False;
171 // Is this DebugInfo currently "archived" ?
172 static inline Bool is_DebugInfo_archived ( const DebugInfo* di )
174 if (!is_DiEpoch_INVALID(di->first_epoch)
175 && !is_DiEpoch_INVALID(di->last_epoch)) {
176 // Yes it is archived. Sanity checks ..
177 vg_assert(di->first_epoch.n <= di->last_epoch.n);
178 vg_assert(di->last_epoch.n <= current_epoch);
179 return True;
180 } else {
181 return False;
185 // Is this DebugInfo valid for the specified epoch?
186 static inline Bool is_DI_valid_for_epoch ( const DebugInfo* di, DiEpoch ep )
188 // Stay sane
189 vg_assert(ep.n > 0 && ep.n <= current_epoch);
191 Bool first_valid = !is_DiEpoch_INVALID(di->first_epoch);
192 Bool last_valid = !is_DiEpoch_INVALID(di->last_epoch);
194 if (first_valid) {
195 if (last_valid) {
196 // Both valid. di is in Archived state.
197 return di->first_epoch.n <= ep.n && ep.n <= di->last_epoch.n;
198 } else {
199 // First is valid, last is invalid. di is in Active state.
200 return di->first_epoch.n <= ep.n;
202 } else {
203 vg_assert (!last_valid); // First invalid, last valid is a bad state.
204 // Neither is valid. di is in Allocated state.
205 return False;
210 static inline UInt ROL32 ( UInt x, UInt n )
212 return (x << n) | (x >> (32-n));
216 /*------------------------------------------------------------*/
217 /*--- Root structure ---*/
218 /*------------------------------------------------------------*/
220 /* The root structure for the entire debug info system. It is a
221 linked list of DebugInfos. */
222 static DebugInfo* debugInfo_list = NULL;
225 /* Find 'di' in the debugInfo_list and move it one step closer to the
226 front of the list, so as to make subsequent searches for it
227 cheaper. When used in a controlled way, makes a major improvement
228 in some DebugInfo-search-intensive situations, most notably stack
229 unwinding on amd64-linux. */
230 static void move_DebugInfo_one_step_forward ( DebugInfo* di )
232 DebugInfo *di0, *di1, *di2;
233 if (di == debugInfo_list)
234 return; /* already at head of list */
235 vg_assert(di != NULL);
236 di0 = debugInfo_list;
237 di1 = NULL;
238 di2 = NULL;
239 while (True) {
240 if (di0 == NULL || di0 == di) break;
241 di2 = di1;
242 di1 = di0;
243 di0 = di0->next;
245 vg_assert(di0 == di);
246 if (di0 != NULL && di1 != NULL && di2 != NULL) {
247 DebugInfo* tmp;
248 /* di0 points to di, di1 to its predecessor, and di2 to di1's
249 predecessor. Swap di0 and di1, that is, move di0 one step
250 closer to the start of the list. */
251 vg_assert(di2->next == di1);
252 vg_assert(di1->next == di0);
253 tmp = di0->next;
254 di2->next = di0;
255 di0->next = di1;
256 di1->next = tmp;
258 else
259 if (di0 != NULL && di1 != NULL && di2 == NULL) {
260 /* it's second in the list. */
261 vg_assert(debugInfo_list == di1);
262 vg_assert(di1->next == di0);
263 di1->next = di0->next;
264 di0->next = di1;
265 debugInfo_list = di0;
270 // Debugging helper for epochs
271 static void show_epochs ( const HChar* msg )
273 if (DEBUG_EPOCHS) {
274 DebugInfo* di;
275 VG_(printf)("\nDebugInfo epoch display, requested by \"%s\"\n", msg);
276 VG_(printf)(" Current epoch (note: 0 means \"invalid epoch\") = %u\n",
277 current_epoch);
278 for (di = debugInfo_list; di; di = di->next) {
279 VG_(printf)(" [di=%p] first %u last %u %s\n",
280 di, di->first_epoch.n, di->last_epoch.n, di->fsm.filename);
282 VG_(printf)("\n");
287 /*------------------------------------------------------------*/
288 /*--- Notification (acquire/discard) helpers ---*/
289 /*------------------------------------------------------------*/
291 /* Gives out unique abstract handles for allocated DebugInfos. See
292 comment in priv_storage.h, declaration of struct _DebugInfo, for
293 details. */
294 static ULong handle_counter = 1;
296 /* Allocate and zero out a new DebugInfo record. */
297 static
298 DebugInfo* alloc_DebugInfo( const HChar* filename )
300 Bool traceme;
301 DebugInfo* di;
303 vg_assert(filename);
305 di = ML_(dinfo_zalloc)("di.debuginfo.aDI.1", sizeof(DebugInfo));
306 di->handle = handle_counter++;
307 di->first_epoch = DiEpoch_INVALID();
308 di->last_epoch = DiEpoch_INVALID();
309 di->fsm.filename = ML_(dinfo_strdup)("di.debuginfo.aDI.2", filename);
310 di->fsm.maps = VG_(newXA)(
311 ML_(dinfo_zalloc), "di.debuginfo.aDI.3",
312 ML_(dinfo_free), sizeof(DebugInfoMapping));
314 /* Everything else -- pointers, sizes, arrays -- is zeroed by
315 ML_(dinfo_zalloc). Now set up the debugging-output flags. */
316 traceme
317 = VG_(string_match)( VG_(clo_trace_symtab_patt), filename );
318 if (traceme) {
319 di->trace_symtab = VG_(clo_trace_symtab);
320 di->trace_cfi = VG_(clo_trace_cfi);
321 di->ddump_syms = VG_(clo_debug_dump_syms);
322 di->ddump_line = VG_(clo_debug_dump_line);
323 di->ddump_frames = VG_(clo_debug_dump_frames);
326 return di;
330 /* Free a DebugInfo, and also all the stuff hanging off it. */
331 static void free_DebugInfo ( DebugInfo* di )
333 Word i, j, n;
334 TyEnt* ent;
335 GExpr* gexpr;
337 vg_assert(di != NULL);
338 if (di->fsm.maps) VG_(deleteXA)(di->fsm.maps);
339 if (di->fsm.filename) ML_(dinfo_free)(di->fsm.filename);
340 if (di->fsm.dbgname) ML_(dinfo_free)(di->fsm.dbgname);
341 if (di->soname) ML_(dinfo_free)(di->soname);
342 if (di->loctab) ML_(dinfo_free)(di->loctab);
343 if (di->loctab_fndn_ix) ML_(dinfo_free)(di->loctab_fndn_ix);
344 if (di->inltab) ML_(dinfo_free)(di->inltab);
345 if (di->cfsi_base) ML_(dinfo_free)(di->cfsi_base);
346 if (di->cfsi_m_ix) ML_(dinfo_free)(di->cfsi_m_ix);
347 if (di->cfsi_rd) ML_(dinfo_free)(di->cfsi_rd);
348 if (di->cfsi_m_pool) VG_(deleteDedupPA)(di->cfsi_m_pool);
349 if (di->cfsi_exprs) VG_(deleteXA)(di->cfsi_exprs);
350 if (di->fpo) ML_(dinfo_free)(di->fpo);
352 if (di->symtab) {
353 /* We have to visit all the entries so as to free up any
354 sec_names arrays that might exist. */
355 n = di->symtab_used;
356 for (i = 0; i < n; i++) {
357 DiSym* sym = &di->symtab[i];
358 if (sym->sec_names)
359 ML_(dinfo_free)(sym->sec_names);
361 /* and finally .. */
362 ML_(dinfo_free)(di->symtab);
365 if (di->strpool)
366 VG_(deleteDedupPA) (di->strpool);
367 if (di->fndnpool)
368 VG_(deleteDedupPA) (di->fndnpool);
370 /* Delete the two admin arrays. These lists exist primarily so
371 that we can visit each object exactly once when we need to
372 delete them. */
373 if (di->admin_tyents) {
374 n = VG_(sizeXA)(di->admin_tyents);
375 for (i = 0; i < n; i++) {
376 ent = (TyEnt*)VG_(indexXA)(di->admin_tyents, i);
377 /* Dump anything hanging off this ent */
378 ML_(TyEnt__make_EMPTY)(ent);
380 VG_(deleteXA)(di->admin_tyents);
381 di->admin_tyents = NULL;
384 if (di->admin_gexprs) {
385 n = VG_(sizeXA)(di->admin_gexprs);
386 for (i = 0; i < n; i++) {
387 gexpr = *(GExpr**)VG_(indexXA)(di->admin_gexprs, i);
388 ML_(dinfo_free)(gexpr);
390 VG_(deleteXA)(di->admin_gexprs);
391 di->admin_gexprs = NULL;
394 /* Dump the variable info. This is kinda complex: we must take
395 care not to free items which reside in either the admin lists
396 (as we have just freed them) or which reside in the DebugInfo's
397 string table. */
398 if (di->varinfo) {
399 for (i = 0; i < VG_(sizeXA)(di->varinfo); i++) {
400 OSet* scope = *(OSet**)VG_(indexXA)(di->varinfo, i);
401 if (!scope) continue;
402 /* iterate over all entries in 'scope' */
403 VG_(OSetGen_ResetIter)(scope);
404 while (True) {
405 DiAddrRange* arange = VG_(OSetGen_Next)(scope);
406 if (!arange) break;
407 /* for each var in 'arange' */
408 vg_assert(arange->vars);
409 for (j = 0; j < VG_(sizeXA)( arange->vars ); j++) {
410 DiVariable* var = (DiVariable*)VG_(indexXA)(arange->vars,j);
411 vg_assert(var);
412 /* Nothing to free in var: all the pointer fields refer
413 to stuff either on an admin list, or in
414 .strpool */
416 VG_(deleteXA)(arange->vars);
417 /* Don't free arange itself, as OSetGen_Destroy does
418 that */
420 VG_(OSetGen_Destroy)(scope);
422 VG_(deleteXA)(di->varinfo);
425 ML_(dinfo_free)(di);
429 /* 'di' is a member of debugInfo_list. Find it, and either (remove it from
430 the list and free all storage reachable from it) or archive it.
431 Notify m_redir that this removal/archiving has happened.
433 Note that 'di' can't be archived. Is a DebugInfo is archived then we
434 want to hold on to it forever. This is asserted for.
436 Note also, we don't advance the current epoch here. That's the
437 responsibility of some (non-immediate) caller.
439 static void discard_or_archive_DebugInfo ( DebugInfo* di )
441 /* di->have_dinfo can be False when an object is mapped "ro"
442 and then unmapped before the debug info is loaded.
443 In other words, debugInfo_list might contain many di that have
444 no OS mappings, even if their fsm.maps still contain mappings.
445 Such (left over) mappings can overlap with real mappings.
446 Search for FSMMAPSNOTCLEANEDUP: below for more details. */
447 /* If a di has no dinfo, we can discard even if VG_(clo_keep_debuginfo). */
448 const Bool archive = VG_(clo_keep_debuginfo) && di->have_dinfo;
450 DebugInfo** prev_next_ptr = &debugInfo_list;
451 DebugInfo* curr = debugInfo_list;
453 /* If di->have_dinfo, then it must be active! */
454 vg_assert(!di->have_dinfo || is_DebugInfo_active(di));
455 while (curr) {
456 if (curr == di) {
457 /* Found it; (remove from list and free it), or archive it. */
458 if (VG_(clo_verbosity) > 1 || VG_(clo_trace_redir))
459 VG_(dmsg)("%s syms at %#lx-%#lx in %s (have_dinfo %d)\n",
460 archive ? "Archiving" : "Discarding",
461 di->text_avma,
462 di->text_avma + di->text_size,
463 curr->fsm.filename ? curr->fsm.filename
464 : "???",
465 curr->have_dinfo);
466 vg_assert(*prev_next_ptr == curr);
467 if (!archive) {
468 *prev_next_ptr = curr->next;
470 if (curr->have_dinfo) {
471 VG_(redir_notify_delete_DebugInfo)( curr );
473 if (archive) {
474 /* Adjust the epoch markers appropriately. */
475 di->last_epoch = VG_(current_DiEpoch)();
476 VG_(archive_ExeContext_in_range) (di->last_epoch,
477 di->text_avma, di->text_size);
478 vg_assert(is_DebugInfo_archived(di));
479 } else {
480 free_DebugInfo(curr);
482 return;
484 prev_next_ptr = &curr->next;
485 curr = curr->next;
488 /* Not found. */
492 /* Repeatedly scan debugInfo_list, looking for DebugInfos with text
493 AVMAs intersecting [start,start+length), and call discard_DebugInfo
494 to get rid of them. This modifies the list, hence the multiple
495 iterations. Returns True iff any such DebugInfos were found.
497 static Bool discard_syms_in_range ( Addr start, SizeT length )
499 Bool anyFound = False;
500 Bool found;
501 DebugInfo* curr;
503 while (True) {
504 found = False;
506 curr = debugInfo_list;
507 while (True) {
508 if (curr == NULL)
509 break;
510 if (is_DebugInfo_archived(curr)
511 || !curr->text_present
512 || (curr->text_present
513 && curr->text_size > 0
514 && (start+length - 1 < curr->text_avma
515 || curr->text_avma + curr->text_size - 1 < start))) {
516 /* no overlap */
517 } else {
518 found = True;
519 break;
521 curr = curr->next;
524 if (!found) break;
525 anyFound = True;
526 discard_or_archive_DebugInfo( curr );
529 return anyFound;
533 /* Does [s1,+len1) overlap [s2,+len2) ? Note: does not handle
534 wraparound at the end of the address space -- just asserts in that
535 case. */
536 static Bool ranges_overlap (Addr s1, SizeT len1, Addr s2, SizeT len2 )
538 Addr e1, e2;
539 if (len1 == 0 || len2 == 0)
540 return False;
541 e1 = s1 + len1 - 1;
542 e2 = s2 + len2 - 1;
543 /* Assert that we don't have wraparound. If we do it would imply
544 that file sections are getting mapped around the end of the
545 address space, which sounds unlikely. */
546 vg_assert(s1 <= e1);
547 vg_assert(s2 <= e2);
548 if (e1 < s2 || e2 < s1) return False;
549 return True;
552 /* Do the basic mappings of the two DebugInfos overlap in any way? */
553 static Bool do_DebugInfos_overlap ( const DebugInfo* di1, const DebugInfo* di2 )
555 Word i, j;
556 vg_assert(di1);
557 vg_assert(di2);
558 for (i = 0; i < VG_(sizeXA)(di1->fsm.maps); i++) {
559 const DebugInfoMapping* map1 = VG_(indexXA)(di1->fsm.maps, i);
560 for (j = 0; j < VG_(sizeXA)(di2->fsm.maps); j++) {
561 const DebugInfoMapping* map2 = VG_(indexXA)(di2->fsm.maps, j);
562 if (ranges_overlap(map1->avma, map1->size, map2->avma, map2->size)) {
563 return True;
568 return False;
572 /* Discard or archive all elements of debugInfo_list whose .mark bit is set.
574 static void discard_or_archive_marked_DebugInfos ( void )
576 DebugInfo* curr;
578 while (True) {
580 curr = debugInfo_list;
581 while (True) {
582 if (!curr)
583 break;
584 if (curr->mark)
585 break;
586 curr = curr->next;
589 if (!curr) break;
591 // If |curr| is going to remain in the debugInfo_list, and merely change
592 // state, then we need to clear its mark bit so we don't subsequently
593 // try to archive it again later. Possibly related to #393146.
594 if (VG_(clo_keep_debuginfo))
595 curr->mark = False;
597 discard_or_archive_DebugInfo( curr );
603 /* Discard any elements of debugInfo_list which overlap with diRef.
604 Clearly diRef must have its mapping information set to something sane. */
605 static void discard_DebugInfos_which_overlap_with ( DebugInfo* diRef )
607 vg_assert(is_DebugInfo_allocated(diRef));
608 DebugInfo* di;
609 /* Mark all the DebugInfos in debugInfo_list that need to be
610 deleted. First, clear all the mark bits; then set them if they
611 overlap with siRef. Since siRef itself is in this list we at
612 least expect its own mark bit to be set. */
613 for (di = debugInfo_list; di; di = di->next) {
614 di->mark = False;
615 if (is_DebugInfo_archived(di))
616 continue;
617 di->mark = do_DebugInfos_overlap( di, diRef );
618 if (di == diRef) {
619 vg_assert(di->mark);
620 di->mark = False;
623 discard_or_archive_marked_DebugInfos();
627 /* Find the existing DebugInfo for |filename| or if not found, create
628 one. In the latter case |filename| is strdup'd into VG_AR_DINFO,
629 and the new DebugInfo is added to debugInfo_list. */
630 static DebugInfo* find_or_create_DebugInfo_for ( const HChar* filename )
632 DebugInfo* di;
633 vg_assert(filename);
634 for (di = debugInfo_list; di; di = di->next) {
635 if (is_DebugInfo_archived(di))
636 continue;
637 vg_assert(di->fsm.filename);
638 if (0==VG_(strcmp)(di->fsm.filename, filename))
639 break;
641 if (!di) {
642 di = alloc_DebugInfo(filename);
643 vg_assert(di);
644 di->next = debugInfo_list;
645 debugInfo_list = di;
647 vg_assert(!is_DebugInfo_archived(di));
648 return di;
652 /* Debuginfo reading for 'di' has just been successfully completed.
653 Check that the invariants stated in
654 "Comment_on_IMPORTANT_CFSI_REPRESENTATIONAL_INVARIANTS" in
655 priv_storage.h are observed. */
656 static void check_CFSI_related_invariants ( const DebugInfo* di )
658 DebugInfo* di2 = NULL;
659 Bool has_nonempty_rx = False;
660 Word i, j;
661 const Bool debug = VG_(debugLog_getLevel)() >= 3;
663 vg_assert(di);
664 /* This fn isn't called until after debuginfo for this object has
665 been successfully read. And that shouldn't happen until we have
666 both a r-x and rw- mapping for the object. Hence: */
667 vg_assert(di->fsm.have_rx_map);
668 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
669 const DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
670 /* We are interested in r-x mappings only */
671 if (!map->rx)
672 continue;
674 /* degenerate case: r-x section is empty */
675 if (map->size == 0)
676 continue;
677 has_nonempty_rx = True;
679 /* normal case: r-x section is nonempty */
680 /* invariant (0) */
681 vg_assert(map->size > 0);
683 /* invariant (1) */
684 for (di2 = debugInfo_list; di2; di2 = di2->next) {
685 if (di2 == di || is_DebugInfo_archived(di2))
686 continue;
687 for (j = 0; j < VG_(sizeXA)(di2->fsm.maps); j++) {
688 const DebugInfoMapping* map2 = VG_(indexXA)(di2->fsm.maps, j);
689 if (!map2->rx || map2->size == 0)
690 continue;
691 vg_assert2(!ranges_overlap(map->avma, map->size,
692 map2->avma, map2->size),
693 "DiCfsi invariant (1) verification failed");
698 /* degenerate case: all r-x sections are empty */
699 if (!has_nonempty_rx) {
700 vg_assert(di->cfsi_rd == NULL);
701 return;
704 /* invariant (2) */
705 if (di->cfsi_rd) {
706 vg_assert(di->cfsi_minavma <= di->cfsi_maxavma); /* duh! */
707 /* It may be that the cfsi range doesn't fit into any one individual
708 mapping, but it is covered by the combination of all the mappings.
709 That's a bit tricky to establish. To do so, create a RangeMap with
710 the cfsi range as the single only non-zero mapping, then zero out all
711 the parts described by di->fsm.maps, and check that there's nothing
712 left. */
713 RangeMap* rm = VG_(newRangeMap)( ML_(dinfo_zalloc),
714 "di.debuginfo. cCri.1", ML_(dinfo_free),
715 /*initialVal*/0 );
716 VG_(bindRangeMap)(rm, di->cfsi_minavma, di->cfsi_maxavma, 1);
717 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
718 const DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
719 /* We are interested in r-x mappings only */
720 if (!map->rx)
721 continue;
722 if (map->size > 0)
723 VG_(bindRangeMap)(rm, map->avma, map->avma + map->size - 1, 0);
725 /* Typically, the range map contains one single range with value 0,
726 meaning that the cfsi range is entirely covered by the rx mappings.
727 However, in some cases, there are holes in the rx mappings
728 (see BZ #398028).
729 In such a case, check that no cfsi refers to these holes. */
730 Bool cfsi_fits = VG_(sizeRangeMap)(rm) >= 1;
731 // Check the ranges in the map.
732 for (Word ix = 0; ix < VG_(sizeRangeMap)(rm); ix++) {
733 UWord key_min = 0x55, key_max = 0x56, val = 0x57;
734 VG_(indexRangeMap)(&key_min, &key_max, &val, rm, ix);
735 if (debug)
736 VG_(dmsg)("cfsi range rx-mappings coverage check: %s %#lx-%#lx\n",
737 val == 1 ? "Uncovered" : "Covered",
738 key_min, key_max);
740 // Sanity-check the range-map operation
741 UWord check_key_min = 0x55, check_key_max = 0x56, check_val = 0x57;
742 VG_(lookupRangeMap)(&check_key_min, &check_key_max, &check_val, rm,
743 key_min + (key_max - key_min) / 2);
744 if (ix == 0)
745 vg_assert(key_min == (UWord)0);
746 if (ix == VG_(sizeRangeMap)(rm) - 1)
747 vg_assert(key_max == ~(UWord)0);
748 vg_assert(key_min == check_key_min);
749 vg_assert(key_max == check_key_max);
750 vg_assert(val == 0 || val == 1);
751 vg_assert(val == check_val);
753 if (val == 1) {
754 /* This is a part of cfsi_minavma .. cfsi_maxavma not covered.
755 Check no cfsi overlaps with this range. */
756 for (i = 0; i < di->cfsi_used; i++) {
757 DiCfSI* cfsi = &di->cfsi_rd[i];
758 vg_assert2(cfsi->base > key_max
759 || cfsi->base + cfsi->len - 1 < key_min,
760 "DiCfsi invariant (2) verification failed");
764 vg_assert(cfsi_fits);
766 VG_(deleteRangeMap)(rm);
769 /* invariants (3) and (4) */
770 if (di->cfsi_rd) {
771 vg_assert(di->cfsi_used > 0);
772 vg_assert(di->cfsi_size > 0);
773 for (i = 0; i < di->cfsi_used; i++) {
774 DiCfSI* cfsi = &di->cfsi_rd[i];
775 vg_assert(cfsi->len > 0);
776 vg_assert(cfsi->base >= di->cfsi_minavma);
777 vg_assert(cfsi->base + cfsi->len - 1 <= di->cfsi_maxavma);
778 if (i > 0) {
779 DiCfSI* cfsip = &di->cfsi_rd[i-1];
780 vg_assert(cfsip->base + cfsip->len <= cfsi->base);
783 } else {
784 vg_assert(di->cfsi_used == 0);
785 vg_assert(di->cfsi_size == 0);
790 /*--------------------------------------------------------------*/
791 /*--- ---*/
792 /*--- TOP LEVEL: INITIALISE THE DEBUGINFO SYSTEM ---*/
793 /*--- ---*/
794 /*--------------------------------------------------------------*/
796 void VG_(di_initialise) ( void )
798 /* There's actually very little to do here, since everything
799 centers around the DebugInfos in debugInfo_list, they are
800 created and destroyed on demand, and each one is treated more or
801 less independently. */
802 vg_assert(debugInfo_list == NULL);
804 /* flush the debug info caches. */
805 caches__invalidate();
809 /*--------------------------------------------------------------*/
810 /*--- ---*/
811 /*--- TOP LEVEL: NOTIFICATION (ACQUIRE/DISCARD INFO) (LINUX) ---*/
812 /*--- ---*/
813 /*--------------------------------------------------------------*/
815 #if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) || defined(VGO_freebsd)
817 /* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
818 static Bool overlaps_DebugInfoMappings ( const DebugInfoMapping* map1,
819 const DebugInfoMapping* map2 )
821 vg_assert(map1 && map2 && map1 != map2);
822 vg_assert(map1->size != 0 && map2->size != 0);
823 if (map1->avma + map1->size <= map2->avma) return False;
824 if (map2->avma + map2->size <= map1->avma) return False;
825 return True;
829 /* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
830 static void show_DebugInfoMappings
831 ( const DebugInfo* di,
832 /*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
834 Word i, n;
835 vg_assert(maps);
836 n = VG_(sizeXA)(maps);
837 for (i = 0; i < n; i++) {
838 const DebugInfoMapping* map = VG_(indexXA)(maps, i);
839 TRACE_SYMTAB(" [%ld] avma 0x%-16lx size %-8lu "
840 "foff %-8lld %s %s %s\n",
841 i, map->avma, map->size, (Long)map->foff,
842 map->rx ? "rx" : "--",
843 map->rw ? "rw" : "--",
844 map->ro ? "ro" : "--");
849 /* Helper for di_notify_ACHIEVE_ACCEPT_STATE. This removes overlaps
850 in |maps|, in a fairly weak way, by truncating overlapping ends.
851 This may need to be strengthened in future. Currently it performs
852 a post-fixup check, so as least we can be sure that if this
853 function returns (rather than asserts) that |maps| is overlap
854 free. */
855 static void truncate_DebugInfoMapping_overlaps
856 ( const DebugInfo* di,
857 /*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
859 TRACE_SYMTAB("Un-de-overlapped _DebugInfoMappings:\n");
860 show_DebugInfoMappings(di, maps);
861 TRACE_SYMTAB("\n");
863 Word i, j, n;
864 DebugInfoMapping *map_i, *map_j;
866 n = VG_(sizeXA)(maps);
867 for (i = 0; i < n; i++) {
869 map_i = VG_(indexXA)(maps, i);
870 if (map_i->size == 0)
871 continue; // Hmm, mutancy. Shouldn't happen.
873 for (j = i+1; j < n; j++) {
875 map_j = VG_(indexXA)(maps, j);
876 if (map_j->size == 0)
877 continue; // Hmm, mutancy. Shouldn't happen.
879 /* map_j was observed later than map_i, since the entries are
880 in the XArray in the order in which they were observed.
881 If map_j starts inside map_i, trim map_i's end so it does
882 not overlap map_j. This reflects the reality that when
883 two mmaped areas overlap, the later mmap silently
884 overwrites the earlier mmap's mapping. */
885 if (map_j->avma >= map_i->avma
886 && map_j->avma < map_i->avma + map_i->size) {
887 SizeT map_i_newsize = map_j->avma - map_i->avma;
888 vg_assert(map_i_newsize < map_i->size);
889 map_i->size = map_i_newsize;
895 TRACE_SYMTAB("De-overlapped DebugInfoMappings:\n");
896 show_DebugInfoMappings(di, maps);
897 TRACE_SYMTAB("\n");
898 TRACE_SYMTAB("Checking that there are no remaining overlaps.\n");
900 for (i = 0; i < n; i++) {
901 map_i = VG_(indexXA)(maps, i);
902 if (map_i->size == 0)
903 continue;
904 for (j = i+1; j < n; j++) {
905 map_j = VG_(indexXA)(maps, j);
906 if (map_j->size == 0)
907 continue;
908 Bool overlap
909 = overlaps_DebugInfoMappings( map_i, map_j );
910 /* If the following assert ever fails, it means the de-overlapping
911 scheme above is too weak, and needs improvement. */
912 vg_assert(!overlap);
916 TRACE_SYMTAB("Check successful.\n");
920 /* The debug info system is driven by notifications that a text
921 segment has been mapped in, or unmapped, or when sections change
922 permission. It's all a bit kludgey and basically means watching
923 syscalls, trying to second-guess when the system's dynamic linker
924 is done with mapping in a new object for execution. This is all
925 tracked using the DebugInfoFSM struct for the object. Anyway, once
926 we finally decide we've got to an accept state, this section then
927 will acquire whatever info is available for the corresponding
928 object. This section contains the notification handlers, which
929 update the FSM and determine when an accept state has been reached.
932 /* When the sequence of observations causes a DebugInfoFSM to move
933 into the accept state, call here to actually get the debuginfo read
934 in. Returns a ULong whose purpose is described in comments
935 preceding VG_(di_notify_mmap) just below.
937 static ULong di_notify_ACHIEVE_ACCEPT_STATE ( struct _DebugInfo* di )
939 ULong di_handle;
940 Bool ok;
942 advance_current_DiEpoch("di_notify_ACHIEVE_ACCEPT_STATE");
944 vg_assert(di->fsm.filename);
945 TRACE_SYMTAB("\n");
946 TRACE_SYMTAB("------ start ELF OBJECT "
947 "-------------------------"
948 "------------------------------\n");
949 TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
950 TRACE_SYMTAB("\n");
952 /* We're going to read symbols and debug info for the avma
953 ranges specified in the _DebugInfoFsm mapping array. First
954 get rid of any other DebugInfos which overlap any of those
955 ranges (to avoid total confusion). But only those valid in
956 the current epoch. We don't want to discard archived DebugInfos. */
957 discard_DebugInfos_which_overlap_with( di );
959 /* The DebugInfoMappings that now exist in the FSM may involve
960 overlaps. This confuses ML_(read_elf_*), and may cause
961 it to compute wrong biases. So de-overlap them now.
962 See http://bugzilla.mozilla.org/show_bug.cgi?id=788974 */
963 truncate_DebugInfoMapping_overlaps( di, di->fsm.maps );
965 /* And acquire new info. */
966 # if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
967 ok = ML_(read_elf_object)( di );
968 if (ok)
969 di->deferred = True;
970 # elif defined(VGO_darwin)
971 ok = ML_(read_macho_debug_info)( di );
972 # else
973 # error "unknown OS"
974 # endif
976 if (ok) {
978 TRACE_SYMTAB("\n------ Canonicalising the "
979 "acquired info ------\n");
980 /* invalidate the debug info caches. */
981 caches__invalidate();
982 /* prepare read data for use */
983 ML_(canonicaliseTables)( di );
984 /* Check invariants listed in
985 Comment_on_IMPORTANT_REPRESENTATIONAL_INVARIANTS in
986 priv_storage.h. */
987 check_CFSI_related_invariants(di);
988 ML_(finish_CFSI_arrays)(di);
990 // Mark di's first epoch point as a valid epoch. Because its
991 // last_epoch value is still invalid, this changes di's state from
992 // "allocated" to "active".
993 vg_assert(is_DebugInfo_allocated(di));
994 di->first_epoch = VG_(current_DiEpoch)();
995 vg_assert(is_DebugInfo_active(di));
996 show_epochs("di_notify_ACHIEVE_ACCEPT_STATE success");
998 /* notify m_redir about it */
999 TRACE_SYMTAB("\n------ Notifying m_redir ------\n");
1000 VG_(redir_notify_new_DebugInfo)( di );
1001 /* Note that we succeeded */
1002 di->have_dinfo = True;
1003 vg_assert(di->handle > 0);
1004 di_handle = di->handle;
1006 } else {
1007 TRACE_SYMTAB("\n------ ELF reading failed ------\n");
1008 /* Something went wrong (eg. bad ELF file). Should we delete
1009 this DebugInfo? No - it contains info on the rw/rx
1010 mappings, at least. */
1011 di_handle = 0;
1012 vg_assert(di->have_dinfo == False);
1015 TRACE_SYMTAB("\n");
1016 TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
1017 TRACE_SYMTAB("------ end ELF OBJECT "
1018 "-------------------------"
1019 "------------------------------\n");
1020 TRACE_SYMTAB("\n");
1022 return di_handle;
1026 /* Notify the debuginfo system about a new mapping. This is the way
1027 new debug information gets loaded.
1029 readelf -e will output something like
1031 Program Headers:
1032 Type Offset VirtAddr PhysAddr
1033 FileSiz MemSiz Flg Align
1034 PHDR 0x0000000000000040 0x0000000000200040 0x0000000000200040
1035 0x0000000000000268 0x0000000000000268 R 0x8
1036 INTERP 0x00000000000002a8 0x00000000002002a8 0x00000000002002a8
1037 0x0000000000000015 0x0000000000000015 R 0x1
1038 [Requesting program interpreter: /libexec/ld-elf.so.1]
1039 LOAD 0x0000000000000000 0x0000000000200000 0x0000000000200000
1040 0x0000000000002acc 0x0000000000002acc R 0x1000
1041 LOAD 0x0000000000002ad0 0x0000000000203ad0 0x0000000000203ad0
1042 0x0000000000004a70 0x0000000000004a70 R E 0x1000
1043 LOAD 0x0000000000007540 0x0000000000209540 0x0000000000209540
1044 0x00000000000001d8 0x00000000000001d8 RW 0x1000
1045 LOAD 0x0000000000007720 0x000000000020a720 0x000000000020a720
1046 0x00000000000002b8 0x00000000000005a0 RW 0x1000
1047 DYNAMIC 0x0000000000007570 0x0000000000209570 0x0000000000209570
1048 0x00000000000001a0 0x00000000000001a0 RW 0x8
1049 GNU_RELRO 0x0000000000007540 0x0000000000209540 0x0000000000209540
1050 0x00000000000001d8 0x00000000000001d8 R 0x1
1051 GNU_EH_FRAME 0x0000000000002334 0x0000000000202334 0x0000000000202334
1052 0x000000000000012c 0x000000000000012c R 0x4
1053 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
1054 0x0000000000000000 0x0000000000000000 RW 0
1055 NOTE 0x00000000000002c0 0x00000000002002c0 0x00000000002002c0
1056 0x0000000000000048 0x0000000000000048 R 0x4
1058 This function will be called for the "LOAD" segments above.
1060 This function gets called from 2 contexts
1062 "HOST TRIGGERED"
1064 1a. For the tool exe, called from valgrind_main. This is already
1065 mmap'd when the host starts so we look at something like the
1066 /proc filesystem to get the mapping after the event and build
1067 up the NSegments from that.
1069 1b. Then the host loads ld.so and the guest exe. This is done in
1070 the sequence
1071 load_client -> VG_(do_exec) -> VG_(do_exec_inner) ->
1072 exe_handlers->load_fn ( == VG_(load_ELF) )
1073 [or load_MACHO].
1075 This does the mmap'ing and creates the associated NSegments.
1077 The NSegments may get merged, (see maybe_merge_nsegments)
1078 so there could be more PT_LOADs than there are NSegments.
1079 VG_(di_notify_mmap) is called by iterating over the
1080 NSegments
1082 "GUEST TRIGGERED"
1084 2. When the guest loads any further shared libs (valgrind core and
1085 tool preload shared libraries, libc, other dependencies, dlopens)
1086 using mmap. The call will be from ML_(generic_PRE_sys_mmap) or
1087 a platform-specific variation.
1089 There are a few variations for syswraps/platforms.
1091 In this case the NSegment could possibly be merged,
1092 but that is irrelevant because di_notify_mmap is being
1093 called directly on the mmap result.
1095 If allow_SkFileV is True, it will try load debug info if the
1096 mapping at 'a' belongs to Valgrind; whereas normally (False)
1097 it will not do that. This allows us to carefully control when
1098 the thing will read symbols from the Valgrind executable itself.
1100 If use_fd is not -1, that is used instead of the filename; this
1101 avoids perturbing fcntl locks, which are released by simply
1102 re-opening and closing the same file (even via different fd!).
1104 Read-only mappings will be ignored.
1105 There may be 1 or 2 RW mappings.
1106 There will also be 1 RX mapping.
1108 If there is no RX or no RW mapping then we will not attempt to
1109 read debuginfo for the file.
1111 In order to know whether there are 1 or 2 RW mappings we
1112 need to check the ELF headers. And in the case that we
1113 detect 2 RW mappings we need to double check that they
1114 aren't contiguous in memory resulting in merged NSegemnts.
1116 This does not apply to Darwin which just checks the Mach-O header
1118 If a call to VG_(di_notify_mmap) causes debug info to be read, then
1119 the returned ULong is an abstract handle which can later be used to
1120 refer to the debuginfo read as a result of this specific mapping,
1121 in later queries to m_debuginfo. In this case the handle value
1122 will be one or above. If the returned value is zero, no debug info
1123 was read. */
1125 ULong VG_(di_notify_mmap)( Addr a, Bool allow_SkFileV, Int use_fd )
1127 NSegment const * seg;
1128 Int rw_load_count;
1129 const HChar* filename;
1130 Bool is_rx_map, is_rw_map, is_ro_map;
1132 DebugInfo* di;
1133 Int actual_fd, oflags;
1134 #if defined(VGO_darwin)
1135 SysRes preadres;
1136 // @todo PJF make this dynamic
1137 // that probably means reading the sizeofcmds from the mach_header then
1138 // allocating enough space for it
1139 // and then one day maybe doing something for fat binaries
1140 HChar buf4k[4096];
1141 #else
1142 Bool elf_ok;
1143 #endif
1144 #if defined(VGO_freebsd)
1145 static Bool first_fixed_file = True;
1146 #endif
1148 const Bool debug = VG_(debugLog_getLevel)() >= 3;
1149 SysRes statres;
1150 struct vg_stat statbuf;
1152 vg_assert(use_fd >= -1);
1154 /* In short, figure out if this mapping is of interest to us, and
1155 if so, try to guess what ld.so is doing and when/if we should
1156 read debug info. */
1157 seg = VG_(am_find_nsegment)(a);
1158 vg_assert(seg);
1160 if (debug) {
1161 VG_(dmsg)("di_notify_mmap-0:\n");
1162 VG_(dmsg)("di_notify_mmap-1: %#lx-%#lx %c%c%c\n",
1163 seg->start, seg->end,
1164 seg->hasR ? 'r' : '-',
1165 seg->hasW ? 'w' : '-',seg->hasX ? 'x' : '-' );
1168 /* guaranteed by aspacemgr-linux.c, sane_NSegment() */
1169 vg_assert(seg->end > seg->start);
1171 /* Ignore non-file mappings */
1172 if ( ! (seg->kind == SkFileC
1173 || (seg->kind == SkFileV && allow_SkFileV)) )
1174 return 0;
1176 /* If the file doesn't have a name, we're hosed. Give up. */
1177 filename = VG_(am_get_filename)( seg );
1178 if (!filename)
1179 return 0;
1182 * Cannot read from these magic files:
1183 * --20208-- WARNING: Serious error when reading debug info
1184 * --20208-- When reading debug info from /proc/xen/privcmd:
1185 * --20208-- can't read file to inspect ELF header
1187 if (VG_(strncmp)(filename, "/proc/xen/", 10) == 0)
1188 return 0;
1190 if (debug)
1191 VG_(dmsg)("di_notify_mmap-2: %s\n", filename);
1193 /* Only try to read debug information from regular files. */
1194 statres = VG_(stat)(filename, &statbuf);
1196 /* stat dereferences symlinks, so we don't expect it to succeed and
1197 yet produce something that is a symlink. */
1198 vg_assert(sr_isError(statres) || ! VKI_S_ISLNK(statbuf.mode));
1200 /* Don't let the stat call fail silently. Filter out some known
1201 sources of noise before complaining, though. */
1202 if (sr_isError(statres)) {
1203 DebugInfo fake_di;
1204 Bool quiet = VG_(strstr)(filename, "/var/run/nscd/") != NULL
1205 || VG_(strstr)(filename, "/dev/shm/") != NULL;
1206 if (!quiet && VG_(clo_verbosity) > 1) {
1207 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1208 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm", filename);
1209 ML_(symerr)(&fake_di, True, "failed to stat64/stat this file");
1211 return 0;
1214 /* Finally, the point of all this stattery: if it's not a regular file,
1215 don't try to read debug info from it. */
1216 if (! VKI_S_ISREG(statbuf.mode))
1217 return 0;
1219 /* no uses of statbuf below here. */
1221 /* Now we have to guess if this is a text-like mapping, a data-like
1222 mapping, neither or both. The rules are:
1224 text if: x86-linux r and x
1225 other-linux r and x and not w
1227 data if: x86-linux r and w
1228 other-linux r and w and not x
1230 Background: On x86-linux, objects are typically mapped twice:
1232 1b8fb000-1b8ff000 r-xp 00000000 08:02 4471477 vgpreload_memcheck.so
1233 1b8ff000-1b900000 rw-p 00004000 08:02 4471477 vgpreload_memcheck.so
1235 whereas ppc32-linux mysteriously does this:
1237 118a6000-118ad000 r-xp 00000000 08:05 14209428 vgpreload_memcheck.so
1238 118ad000-118b6000 ---p 00007000 08:05 14209428 vgpreload_memcheck.so
1239 118b6000-118bd000 rwxp 00000000 08:05 14209428 vgpreload_memcheck.so
1241 The third mapping should not be considered to have executable
1242 code in. Therefore a test which works for both is: r and x and
1243 NOT w. Reading symbols from the rwx segment -- which overlaps
1244 the r-x segment in the file -- causes the redirection mechanism
1245 to redirect to addresses in that third segment, which is wrong
1246 and causes crashes.
1248 JRS 28 Dec 05: unfortunately icc 8.1 on x86 has been seen to
1249 produce executables with a single rwx segment rather than a
1250 (r-x,rw-) pair. That means the rules have to be modified thusly:
1252 x86-linux: consider if r and x
1253 all others: consider if r and x and not w
1255 2009 Aug 16: apply similar kludge to ppc32-linux.
1256 See http://bugs.kde.org/show_bug.cgi?id=190820
1258 There are two modes on s390x: with and without the noexec kernel
1259 parameter. Together with some older kernels, this leads to several
1260 variants:
1261 executable: r and x
1262 data: r and w and x
1264 executable: r and x
1265 data: r and w
1267 is_rx_map = False;
1268 is_rw_map = False;
1269 is_ro_map = False;
1271 # if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_mips32) \
1272 || defined(VGA_mips64) || defined(VGA_nanomips)
1273 is_rx_map = seg->hasR && seg->hasX;
1274 is_rw_map = seg->hasR && seg->hasW;
1275 # elif defined(VGA_amd64) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
1276 || defined(VGA_arm) || defined(VGA_arm64)
1277 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1278 is_rw_map = seg->hasR && seg->hasW && !seg->hasX;
1279 # elif defined(VGP_s390x_linux)
1280 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1281 is_rw_map = seg->hasR && seg->hasW;
1282 # else
1283 # error "Unknown platform"
1284 # endif
1286 is_ro_map = seg->hasR && !seg->hasW && !seg->hasX;
1288 # if defined(VGO_solaris)
1289 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1290 is_rw_map = seg->hasR && seg->hasW;
1291 # endif
1293 if (debug)
1294 VG_(dmsg)("di_notify_mmap-3: "
1295 "is_rx_map %d, is_rw_map %d, is_ro_map %d\n",
1296 (Int)is_rx_map, (Int)is_rw_map, (Int)is_ro_map);
1298 /* Ignore mappings with permissions we can't possibly be interested in. */
1299 if (!(is_rx_map || is_rw_map || is_ro_map))
1300 return 0;
1302 #if defined(VGO_freebsd)
1303 /* Ignore non-fixed read-only mappings. The dynamic linker may be
1304 * mapping something for its own transient purposes. */
1305 if (!seg->isFF && is_ro_map) {
1306 if (first_fixed_file) {
1307 if (debug) {
1308 VG_(dmsg)("di_notify_mmap-4: first non-fixed ro map\n");
1310 first_fixed_file = False;
1311 } else {
1312 if (debug) {
1313 VG_(dmsg)("di_notify_mmap-5: not first non-fixed ro map, ignored\n");
1315 return 0;
1318 #endif
1320 #if defined(VGO_darwin)
1321 /* Peer at the first few bytes of the file, to see if it is an ELF */
1322 /* object file. Ignore the file if we do not have read permission. */
1323 VG_(memset)(buf4k, 0, sizeof(buf4k));
1324 #endif
1326 oflags = VKI_O_RDONLY;
1327 # if defined(VKI_O_LARGEFILE)
1328 oflags |= VKI_O_LARGEFILE;
1329 # endif
1331 if (use_fd == -1) {
1332 SysRes fd = VG_(open)( filename, oflags, 0 );
1333 if (sr_isError(fd)) {
1334 if (sr_Err(fd) != VKI_EACCES) {
1335 DebugInfo fake_di;
1336 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1337 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm",
1338 filename);
1339 ML_(symerr)(&fake_di, True,
1340 "can't open file to inspect ELF header");
1342 return 0;
1344 actual_fd = sr_Res(fd);
1345 } else {
1346 actual_fd = use_fd;
1349 #if defined(VGO_darwin)
1350 preadres = VG_(pread)( actual_fd, buf4k, sizeof(buf4k), 0 );
1351 if (use_fd == -1) {
1352 VG_(close)( actual_fd );
1355 if (sr_isError(preadres)) {
1356 DebugInfo fake_di;
1357 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1358 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm", filename);
1359 ML_(symerr)(&fake_di, True, "can't read file to inspect Mach-O headers");
1360 return 0;
1362 if (sr_Res(preadres) == 0)
1363 return 0;
1364 vg_assert(sr_Res(preadres) > 0 && sr_Res(preadres) <= sizeof(buf4k) );
1366 rw_load_count = 0;
1368 if (!ML_(check_macho_and_get_rw_loads)( buf4k, (SizeT)sr_Res(preadres), &rw_load_count ))
1369 return 0;
1370 #endif
1372 /* We're only interested in mappings of object files. */
1373 # if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
1375 rw_load_count = 0;
1377 elf_ok = ML_(check_elf_and_get_rw_loads) ( actual_fd, filename, &rw_load_count );
1379 if (use_fd == -1) {
1380 VG_(close)( actual_fd );
1383 if (!elf_ok) {
1384 return 0;
1387 # endif
1389 /* See if we have a DebugInfo for this filename. If not,
1390 create one. */
1391 di = find_or_create_DebugInfo_for( filename );
1392 vg_assert(di);
1394 /* Ignore all mappings for this filename once we've read debuginfo for it.
1395 This avoids the confusion of picking up "irrelevant" mappings in
1396 applications which mmap their objects outside of ld.so, for example
1397 Firefox's Gecko profiler.
1399 What happens in that case is: the application maps the object "ro" for
1400 whatever reason. We record the mapping di->fsm.maps. The application
1401 later unmaps the object. However, the mapping is not removed from
1402 di->fsm.maps. Later, when some other (unrelated) object is mapped (via
1403 ld.so) into that address space, we first unload any debuginfo that has a
1404 mapping intersecting that area. That means we will end up incorrectly
1405 unloading debuginfo for the object with the "irrelevant" mappings. This
1406 causes various problems, not least because it can unload the debuginfo
1407 for libc.so and so cause malloc intercepts to become un-intercepted.
1409 This fix assumes that all mappings made once we've read debuginfo for
1410 an object are irrelevant. I think that's OK, but need to check with
1411 mjw/thh. */
1412 if (di->have_dinfo) {
1413 if (debug)
1414 VG_(dmsg)("di_notify_mmap-4x: "
1415 "ignoring mapping because we already read debuginfo "
1416 "for DebugInfo* %p\n", di);
1417 return 0;
1420 if (debug)
1421 VG_(dmsg)("di_notify_mmap-4: "
1422 "noting details in DebugInfo* at %p\n", di);
1424 /* Note the details about the mapping. */
1425 DebugInfoMapping map;
1426 map.avma = seg->start;
1427 map.size = seg->end + 1 - seg->start;
1428 map.foff = seg->offset;
1429 #if defined(VGO_freebsd)
1430 map.ignore_foff = seg->ignore_offset;
1431 #endif
1432 map.rx = is_rx_map;
1433 map.rw = is_rw_map;
1434 map.ro = is_ro_map;
1435 VG_(addToXA)(di->fsm.maps, &map);
1437 /* Update flags about what kind of mappings we've already seen. */
1438 di->fsm.have_rx_map |= is_rx_map;
1439 /* This is a bit of a hack, using a Bool as a counter */
1440 if (is_rw_map)
1441 ++di->fsm.rw_map_count;
1442 di->fsm.have_ro_map |= is_ro_map;
1444 /* So, finally, are we in an accept state? */
1445 vg_assert(!di->have_dinfo);
1446 if (di->fsm.have_rx_map &&
1447 di->fsm.rw_map_count == rw_load_count) {
1448 /* Ok, so, finally, we found what we need, and we haven't
1449 already read debuginfo for this object. So let's do so now.
1450 Yee-ha! */
1452 if (debug)
1453 VG_(dmsg)("di_notify_mmap-5: "
1454 "achieved accept state for %s\n", filename);
1455 return di_notify_ACHIEVE_ACCEPT_STATE ( di );
1456 } else {
1457 /* If we don't have an rx and rw mapping, go no further. */
1458 if (debug)
1459 VG_(dmsg)("di_notify_mmap-6: "
1460 "no dinfo loaded %s (no rx or no rw mapping)\n", filename);
1461 return 0;
1465 /* Load DI if it hasn't already been been loaded. */
1466 void VG_(di_load_di)( DebugInfo *di )
1468 if (di->deferred) {
1469 di->deferred = False;
1470 #if defined(VGO_darwin)
1471 ML_(read_macho_debug_info) (di);
1472 #else
1473 ML_(read_elf_debug) (di);
1474 #endif
1475 ML_(canonicaliseTables)( di );
1477 /* Check invariants listed in
1478 Comment_on_IMPORTANT_REPRESENTATIONAL_INVARIANTS in
1479 priv_storage.h. */
1480 check_CFSI_related_invariants(di);
1481 ML_(finish_CFSI_arrays)(di);
1485 /* Load DI if it has a text segment containing A and DI hasn't already
1486 been loaded. */
1488 void VG_(load_di)( DebugInfo *di, Addr a)
1490 if (!di->text_present
1491 || di->text_size <= 0
1492 || di->text_avma > a
1493 || a >= di->text_avma + di->text_size)
1494 return;
1496 VG_(di_load_di)(di);
1499 /* Attempt to load DebugInfo with a text segment containing A,
1500 if such a debuginfo hasn't already been loaded. */
1502 void VG_(addr_load_di)( Addr a )
1504 DebugInfo *di;
1506 di = VG_(find_DebugInfo)(VG_(current_DiEpoch)(), a);
1507 if (di != NULL)
1508 VG_(di_load_di)(di);
1511 /* Unmap is simpler - throw away any SegInfos intersecting
1512 [a, a+len). */
1513 void VG_(di_notify_munmap)( Addr a, SizeT len )
1515 Bool anyFound;
1516 if (0) VG_(printf)("DISCARD %#lx %#lx\n", a, a+len);
1517 anyFound = discard_syms_in_range(a, len);
1518 if (anyFound) {
1519 caches__invalidate();
1520 advance_current_DiEpoch("VG_(di_notify_munmap)");
1521 show_epochs("VG_(di_notify_munmap)");
1526 /* Uh, this doesn't do anything at all. IIRC glibc (or ld.so, I don't
1527 remember) does a bunch of mprotects on itself, and if we follow
1528 through here, it causes the debug info for that object to get
1529 discarded. */
1530 void VG_(di_notify_mprotect)( Addr a, SizeT len, UInt prot )
1532 Bool exe_ok = toBool(prot & VKI_PROT_EXEC);
1533 # if defined(VGA_x86)
1534 exe_ok = exe_ok || toBool(prot & VKI_PROT_READ);
1535 # endif
1536 if (0 && !exe_ok) {
1537 Bool anyFound = discard_syms_in_range(a, len);
1538 if (anyFound) {
1539 caches__invalidate();
1540 advance_current_DiEpoch("VG_(di_notify_mprotect)");
1546 /* This is a MacOSX >= 10.7 32-bit only special. See comments on the
1547 declaration of struct _DebugInfoFSM for details. */
1548 void VG_(di_notify_vm_protect)( Addr a, SizeT len, UInt prot )
1550 const Bool debug = VG_(debugLog_getLevel)() >= 3;
1552 Bool r_ok = toBool(prot & VKI_PROT_READ);
1553 Bool w_ok = toBool(prot & VKI_PROT_WRITE);
1554 Bool x_ok = toBool(prot & VKI_PROT_EXEC);
1555 if (debug) {
1556 VG_(dmsg)("di_notify_vm_protect-0:\n");
1557 VG_(dmsg)("di_notify_vm_protect-1: %#lx-%#lx %c%c%c\n",
1558 a, a + len - 1,
1559 r_ok ? 'r' : '-', w_ok ? 'w' : '-', x_ok ? 'x' : '-' );
1562 Bool do_nothing = True;
1563 # if defined(VGP_x86_darwin) && (DARWIN_VERS >= DARWIN_10_7)
1564 do_nothing = False;
1565 # endif
1566 if (do_nothing /* wrong platform */) {
1567 if (debug)
1568 VG_(dmsg)("di_notify_vm_protect-2: wrong platform, "
1569 "doing nothing.\n");
1570 return;
1573 if (! (r_ok && !w_ok && x_ok))
1574 return; /* not an upgrade to r-x */
1576 /* Find a DebugInfo containing a FSM that has [a, +len) previously
1577 observed as a r-- mapping, plus some other rw- mapping. If such
1578 is found, conclude we're in an accept state and read debuginfo
1579 accordingly. */
1580 if (debug)
1581 VG_(dmsg)("di_notify_vm_protect-3: looking for existing DebugInfo*\n");
1582 DebugInfo* di;
1583 DebugInfoMapping *map = NULL;
1584 Word i;
1585 for (di = debugInfo_list; di; di = di->next) {
1586 vg_assert(di->fsm.filename);
1587 if (di->have_dinfo)
1588 continue; /* already have debuginfo for this object */
1589 if (!di->fsm.have_ro_map)
1590 continue; /* need to have a r-- mapping for this object */
1591 if (di->fsm.have_rx_map)
1592 continue; /* rx- mapping already exists */
1593 if (!di->fsm.rw_map_count)
1594 continue; /* need to have a rw- mapping */
1595 /* Try to find a mapping matching the memory area. */
1596 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1597 map = VG_(indexXA)(di->fsm.maps, i);
1598 if (map->ro && map->avma == a && map->size == len)
1599 break;
1600 map = NULL;
1602 if (!map)
1603 continue; /* this isn't an upgrade of an r-- mapping */
1604 /* looks like we're in luck! */
1605 break;
1607 if (di == NULL)
1608 return; /* didn't find anything */
1610 if (debug)
1611 VG_(dmsg)("di_notify_vm_protect-4: found existing DebugInfo* at %p\n",
1612 di);
1614 /* Do the upgrade. Simply update the flags of the mapping
1615 and pretend we never saw the RO map at all. */
1616 vg_assert(di->fsm.have_ro_map);
1617 map->rx = True;
1618 map->ro = False;
1619 di->fsm.have_rx_map = True;
1620 di->fsm.have_ro_map = False;
1621 /* See if there are any more ro mappings */
1622 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1623 map = VG_(indexXA)(di->fsm.maps, i);
1624 if (map->ro) {
1625 di->fsm.have_ro_map = True;
1626 break;
1630 /* Check if we're now in an accept state and read debuginfo. Finally. */
1631 if (di->fsm.have_rx_map && di->fsm.rw_map_count && !di->have_dinfo) {
1632 if (debug)
1633 VG_(dmsg)("di_notify_vm_protect-5: "
1634 "achieved accept state for %s\n", di->fsm.filename);
1635 ULong di_handle __attribute__((unused))
1636 = di_notify_ACHIEVE_ACCEPT_STATE( di );
1637 /* di_handle is ignored. That's not a problem per se -- it just
1638 means nobody will ever be able to refer to this debuginfo by
1639 handle since nobody will know what the handle value is. */
1644 /*--------- PDB (windows debug info) reading --------- */
1646 /* this should really return ULong, as per VG_(di_notify_mmap). */
1647 void VG_(di_notify_pdb_debuginfo)( Int fd_obj, Addr avma_obj,
1648 SizeT total_size, PtrdiffT bias_obj )
1650 Int i, r, sz_exename;
1651 ULong obj_mtime, pdb_mtime;
1652 HChar* pdbname = NULL;
1653 HChar* dot;
1654 SysRes sres;
1655 Int fd_pdbimage;
1656 SizeT n_pdbimage;
1657 struct vg_stat stat_buf;
1659 if (VG_(clo_verbosity) > 0) {
1660 VG_(message)(Vg_UserMsg, "\n");
1661 VG_(message)(Vg_UserMsg,
1662 "LOAD_PDB_DEBUGINFO: clreq: fd=%d, avma=%#lx, total_size=%lu, "
1663 "bias=%#lx\n",
1664 fd_obj, avma_obj, total_size, (UWord)bias_obj
1668 /* 'fd' refers to the .exe/.dll we're dealing with. Get its modification
1669 time into obj_mtime. */
1670 r = VG_(fstat)(fd_obj, &stat_buf);
1671 if (r == -1)
1672 return; /* stat failed ?! */
1673 vg_assert(r == 0);
1674 obj_mtime = stat_buf.mtime;
1676 /* and get its name into exename. */
1677 const HChar *exe;
1678 if (! VG_(resolve_filename)(fd_obj, &exe))
1679 return; /* failed */
1680 sz_exename = VG_(strlen)(exe);
1681 HChar exename[sz_exename + 1];
1682 VG_(strcpy)(exename, exe); // make a copy on the stack
1684 if (VG_(clo_verbosity) > 0) {
1685 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: objname: %s\n", exename);
1688 /* Try to get the PDB file name from the executable. */
1689 pdbname = ML_(find_name_of_pdb_file)(exename);
1690 if (pdbname) {
1691 vg_assert(VG_(strlen)(pdbname) >= 5); /* 5 = strlen("X.pdb") */
1692 /* So we successfully extracted a name from the PE file. But it's
1693 likely to be of the form
1694 e:\foo\bar\xyzzy\wibble.pdb
1695 and we need to change it into something we can actually open
1696 in Wine-world, which basically means turning it into
1697 $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1698 We also take into account $WINEPREFIX, if it is set.
1699 For the moment, if the name isn't fully qualified, just forget it
1700 (we'd have to root around to find where the pdb actually is)
1702 /* Change all the backslashes to forward slashes */
1703 for (i = 0; pdbname[i]; i++) {
1704 if (pdbname[i] == '\\')
1705 pdbname[i] = '/';
1707 Bool is_quald
1708 = ('a' <= VG_(tolower)(pdbname[0]) && VG_(tolower)(pdbname[0]) <= 'z')
1709 && pdbname[1] == ':'
1710 && pdbname[2] == '/';
1711 HChar* home = VG_(getenv)("HOME");
1712 HChar* wpfx = VG_(getenv)("WINEPREFIX");
1713 if (is_quald && wpfx) {
1714 /* Change e:/foo/bar/xyzzy/wibble.pdb
1715 to $WINEPREFIX/drive_e/foo/bar/xyzzy/wibble.pdb
1717 Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(wpfx) + 50/*misc*/;
1718 HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.1", mashedSzB);
1719 VG_(snprintf)(mashed, mashedSzB, "%s/drive_%c%s",
1720 wpfx, pdbname[0], &pdbname[2]);
1721 vg_assert(mashed[mashedSzB-1] == 0);
1722 ML_(dinfo_free)(pdbname);
1723 pdbname = mashed;
1725 else if (is_quald && home && !wpfx) {
1726 /* Change e:/foo/bar/xyzzy/wibble.pdb
1727 to $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1729 Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(home) + 50/*misc*/;
1730 HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.2", mashedSzB);
1731 VG_(snprintf)(mashed, mashedSzB, "%s/.wine/drive_%c%s",
1732 home, pdbname[0], &pdbname[2]);
1733 vg_assert(mashed[mashedSzB-1] == 0);
1734 ML_(dinfo_free)(pdbname);
1735 pdbname = mashed;
1736 } else {
1737 /* It's not a fully qualified path, or neither $HOME nor $WINE
1738 are set (strange). Give up. */
1739 ML_(dinfo_free)(pdbname);
1740 pdbname = NULL;
1744 /* Try s/exe/pdb/ if we don't have a valid pdbname. */
1745 if (!pdbname) {
1746 /* Try to find a matching PDB file from which to read debuginfo.
1747 Windows PE files have symbol tables and line number information,
1748 but MSVC doesn't seem to use them. */
1749 /* Why +5 ? Because in the worst case, we could find a dot as the
1750 last character of pdbname, and we'd then put "pdb" right after
1751 it, hence extending it a bit. */
1752 pdbname = ML_(dinfo_zalloc)("di.debuginfo.lpd1", sz_exename+5);
1753 VG_(strcpy)(pdbname, exename);
1754 vg_assert(pdbname[sz_exename+5-1] == 0);
1755 dot = VG_(strrchr)(pdbname, '.');
1756 if (!dot)
1757 goto out; /* there's no dot in the exe's name ?! */
1758 if (dot[1] == 0)
1759 goto out; /* hmm, path ends in "." */
1761 if ('A' <= dot[1] && dot[1] <= 'Z')
1762 VG_(strcpy)(dot, ".PDB");
1763 else
1764 VG_(strcpy)(dot, ".pdb");
1766 vg_assert(pdbname[sz_exename+5-1] == 0);
1769 /* See if we can find it, and check it's in-dateness. */
1770 sres = VG_(stat)(pdbname, &stat_buf);
1771 if (sr_isError(sres)) {
1772 VG_(message)(Vg_UserMsg, "Warning: Missing or un-stat-able %s\n",
1773 pdbname);
1774 if (VG_(clo_verbosity) > 0)
1775 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: missing: %s\n", pdbname);
1776 goto out;
1778 pdb_mtime = stat_buf.mtime;
1780 if (obj_mtime > pdb_mtime + 60ULL) {
1781 /* PDB file is older than PE file. Really, the PDB should be
1782 newer than the PE, but that doesn't always seem to be the
1783 case. Allow the PDB to be up to one minute older.
1784 Otherwise, it's probably out of date, in which case ignore it
1785 or we will either (a) print wrong stack traces or more likely
1786 (b) crash.
1788 VG_(message)(Vg_UserMsg,
1789 "Warning: %s (mtime = %llu)\n"
1790 " is older than %s (mtime = %llu)\n",
1791 pdbname, pdb_mtime, exename, obj_mtime);
1794 sres = VG_(open)(pdbname, VKI_O_RDONLY, 0);
1795 if (sr_isError(sres)) {
1796 VG_(message)(Vg_UserMsg, "Warning: Can't open %s\n", pdbname);
1797 goto out;
1800 /* Looks promising; go on to try and read stuff from it. But don't
1801 mmap the file. Instead mmap free space and read the file into
1802 it. This is because files on CIFS filesystems that are mounted
1803 '-o directio' can't be mmap'd, and that mount option is needed
1804 to make CIFS work reliably. (See
1805 http://www.nabble.com/Corrupted-data-on-write-to-
1806 Windows-2003-Server-t2782623.html)
1807 This is slower, but at least it works reliably. */
1808 fd_pdbimage = sr_Res(sres);
1809 n_pdbimage = stat_buf.size;
1810 if (n_pdbimage == 0 || n_pdbimage > 0x7FFFFFFF) {
1811 // 0x7FFFFFFF: why? Because the VG_(read) just below only
1812 // can deal with a signed int as the size of data to read,
1813 // so we can't reliably check for read failure for files
1814 // greater than that size. Hence just skip them; we're
1815 // unlikely to encounter a PDB that large anyway.
1816 VG_(close)(fd_pdbimage);
1817 goto out;
1819 sres = VG_(am_mmap_anon_float_valgrind)( n_pdbimage );
1820 if (sr_isError(sres)) {
1821 VG_(close)(fd_pdbimage);
1822 goto out;
1825 void* pdbimage = (void*)(Addr)sr_Res(sres);
1826 r = VG_(read)( fd_pdbimage, pdbimage, (Int)n_pdbimage );
1827 if (r < 0 || r != (Int)n_pdbimage) {
1828 VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1829 VG_(close)(fd_pdbimage);
1830 goto out;
1833 if (VG_(clo_verbosity) > 0)
1834 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: pdbname: %s\n", pdbname);
1836 /* play safe; always invalidate the debug info caches. I don't know if
1837 this is necessary, but anyway .. */
1838 caches__invalidate();
1839 /* dump old info for this range, if any */
1840 discard_syms_in_range( avma_obj, total_size );
1841 advance_current_DiEpoch("VG_(di_notify_pdb_debuginfo)");
1843 { DebugInfo* di = find_or_create_DebugInfo_for(exename);
1845 /* this di must be new, since we just nuked any old stuff in the range */
1846 vg_assert(di && !di->fsm.have_rx_map && !di->fsm.rw_map_count);
1847 vg_assert(!di->have_dinfo);
1849 /* don't set up any of the di-> fields; let
1850 ML_(read_pdb_debug_info) do it. */
1851 if (ML_(read_pdb_debug_info)( di, avma_obj, bias_obj,
1852 pdbimage, n_pdbimage, pdbname, pdb_mtime )) {
1853 vg_assert(di->have_dinfo); // fails if PDB read failed
1854 if (VG_(clo_verbosity) > 0) {
1855 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: done: "
1856 "%lu syms, %lu src locs, %lu fpo recs\n",
1857 di->symtab_used, di->loctab_used, di->fpo_size);
1859 } else {
1860 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: failed loading info "
1861 "from %s\n", pdbname);
1862 /* We cannot make any sense of this pdb, so (force) discard it,
1863 even if VG_(clo_keep_debuginfo) is True. */
1864 const Bool save_clo_keep_debuginfo = VG_(clo_keep_debuginfo);
1865 VG_(clo_keep_debuginfo) = False;
1866 // The below will assert if di is not active. Not too sure what
1867 // the state of di in this failed loading state.
1868 discard_or_archive_DebugInfo (di);
1869 VG_(clo_keep_debuginfo) = save_clo_keep_debuginfo;
1871 VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1872 VG_(close)(fd_pdbimage);
1876 out:
1877 if (pdbname) ML_(dinfo_free)(pdbname);
1880 #endif /* defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) || defined(VGO_freebsd) */
1883 /*------------------------------------------------------------*/
1884 /*--- ---*/
1885 /*--- TOP LEVEL: QUERYING EXISTING DEBUG INFO ---*/
1886 /*--- ---*/
1887 /*------------------------------------------------------------*/
1889 void VG_(di_discard_ALL_debuginfo)( void )
1891 DebugInfo *di, *di2;
1892 di = debugInfo_list;
1893 while (di) {
1894 di2 = di->next;
1895 VG_(printf)("XXX rm %p\n", di);
1896 free_DebugInfo( di );
1897 di = di2;
1902 DebugInfoMapping* ML_(find_rx_mapping) ( DebugInfo* di, Addr lo, Addr hi )
1904 Word i;
1905 vg_assert(lo <= hi);
1907 /* Optimization: Try to use the last matched rx mapping first */
1908 if ( di->last_rx_map
1909 && lo >= di->last_rx_map->avma
1910 && hi < di->last_rx_map->avma + di->last_rx_map->size)
1911 return di->last_rx_map;
1913 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1914 DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
1915 if ( map->rx && map->size > 0
1916 && lo >= map->avma && hi < map->avma + map->size) {
1917 di->last_rx_map = map;
1918 return map;
1922 return NULL;
1925 /*------------------------------------------------------------*/
1926 /*--- Types and functions for inlined IP cursor ---*/
1927 /*------------------------------------------------------------*/
1929 struct _InlIPCursor {
1930 Addr eip; // Cursor used to describe calls at eip.
1931 DebugInfo* di; // DebugInfo describing inlined calls at eip
1933 Word inltab_lopos; // The inlined fn calls covering eip are in
1934 Word inltab_hipos; // di->inltab[inltab_lopos..inltab_hipos].
1935 // Note that not all inlined fn calls in this range
1936 // are necessarily covering eip.
1938 Int curlevel; // Current level to describe.
1939 // 0 means to describe eip itself.
1940 Word cur_inltab; // inltab pos for call inlined at current level.
1941 Word next_inltab; // inltab pos for call inlined at next (towards main)
1942 // level.
1945 static Bool is_top(const InlIPCursor *iipc)
1947 return !iipc || iipc->cur_inltab == -1;
1950 static Bool is_bottom(const InlIPCursor *iipc)
1952 return !iipc || iipc->next_inltab == -1;
1955 Bool VG_(next_IIPC)(InlIPCursor *iipc)
1957 Word i;
1958 DiInlLoc *hinl = NULL;
1959 Word hinl_pos = -1;
1960 DebugInfo *di;
1962 if (iipc == NULL)
1963 return False;
1965 if (iipc->curlevel <= 0) {
1966 iipc->curlevel--;
1967 return False;
1970 di = iipc->di;
1971 for (i = iipc->inltab_lopos; i <= iipc->inltab_hipos; i++) {
1972 if (di->inltab[i].addr_lo <= iipc->eip
1973 && iipc->eip < di->inltab[i].addr_hi
1974 && di->inltab[i].level < iipc->curlevel
1975 && (!hinl || hinl->level < di->inltab[i].level)) {
1976 hinl = &di->inltab[i];
1977 hinl_pos = i;
1981 iipc->cur_inltab = iipc->next_inltab;
1982 iipc->next_inltab = hinl_pos;
1983 if (iipc->next_inltab < 0)
1984 iipc->curlevel = 0; // no inlined call anymore, describe eip itself
1985 else
1986 iipc->curlevel = di->inltab[iipc->next_inltab].level;
1988 return True;
1991 /* Forward */
1992 static void search_all_loctabs ( DiEpoch ep, Addr ptr,
1993 /*OUT*/DebugInfo** pdi, /*OUT*/Word* locno );
1995 /* Returns the position after which eip would be inserted in inltab.
1996 (-1 if eip should be inserted before position 0).
1997 This is the highest position with an addr_lo <= eip.
1998 As inltab is sorted on addr_lo, dichotomic search can be done
1999 (note that inltab might have duplicates addr_lo). */
2000 static Word inltab_insert_pos (DebugInfo *di, Addr eip)
2002 Word mid,
2003 lo = 0,
2004 hi = di->inltab_used-1;
2005 while (lo <= hi) {
2006 mid = (lo + hi) / 2;
2007 if (eip < di->inltab[mid].addr_lo) { hi = mid-1; continue; }
2008 if (eip > di->inltab[mid].addr_lo) { lo = mid+1; continue; }
2009 lo = mid; break;
2012 while (lo <= di->inltab_used-1 && di->inltab[lo].addr_lo <= eip)
2013 lo++;
2014 #if 0
2015 for (mid = 0; mid <= di->inltab_used-1; mid++)
2016 if (eip < di->inltab[mid].addr_lo)
2017 break;
2018 vg_assert (lo - 1 == mid - 1);
2019 #endif
2020 return lo - 1;
2023 InlIPCursor* VG_(new_IIPC)(DiEpoch ep, Addr eip)
2025 DebugInfo* di;
2026 Word locno;
2027 Word i;
2028 InlIPCursor *ret;
2029 Bool avail;
2031 if (!VG_(clo_read_inline_info))
2032 return NULL; // No way we can find inlined calls.
2034 /* Search the DebugInfo for (ep, eip) */
2035 search_all_loctabs ( ep, eip, &di, &locno );
2036 if (di == NULL || di->inltab_used == 0)
2037 return NULL; // No di (with inltab) containing eip.
2039 /* Search the entry in di->inltab with the highest addr_lo that
2040 contains eip. */
2041 /* We start from the highest pos in inltab after which eip would
2042 be inserted. */
2043 for (i = inltab_insert_pos (di, eip); i >= 0; i--) {
2044 if (di->inltab[i].addr_lo <= eip && eip < di->inltab[i].addr_hi) {
2045 break;
2047 /* Stop the backward scan when reaching an addr_lo which
2048 cannot anymore contain eip : we know that all ranges before
2049 i also cannot contain eip. */
2050 if (di->inltab[i].addr_lo < eip - di->maxinl_codesz)
2051 return NULL;
2054 if (i < 0)
2055 return NULL; // No entry containing eip.
2057 /* We have found the highest entry containing eip.
2058 Build a cursor. */
2059 ret = ML_(dinfo_zalloc) ("dinfo.new_IIPC", sizeof(*ret));
2060 ret->eip = eip;
2061 ret->di = di;
2062 ret->inltab_hipos = i;
2063 for (i = ret->inltab_hipos - 1; i >= 0; i--) {
2065 if (di->inltab[i].addr_lo < eip - di->maxinl_codesz)
2066 break; /* Similar stop backward scan logic as above. */
2068 ret->inltab_lopos = i + 1;
2069 ret->curlevel = MAX_LEVEL;
2070 ret->cur_inltab = -1;
2071 ret->next_inltab = -1;
2073 /* MAX_LEVEL is higher than any stored level. We can use
2074 VG_(next_IIPC) to get to the 'real' first highest call level. */
2075 avail = VG_(next_IIPC) (ret);
2076 vg_assert (avail);
2078 return ret;
2081 void VG_(delete_IIPC)(InlIPCursor *iipc)
2083 if (iipc)
2084 ML_(dinfo_free)( iipc );
2088 /*------------------------------------------------------------*/
2089 /*--- Use of symbol table & location info to create ---*/
2090 /*--- plausible-looking stack dumps. ---*/
2091 /*------------------------------------------------------------*/
2093 /* Search all symtabs that we know about to locate ptr. If found, set
2094 *pdi to the relevant DebugInfo, and *symno to the symtab entry
2095 *number within that. If not found, *psi is set to NULL.
2096 If findText==True, only text symbols are searched for.
2097 If findText==False, only data symbols are searched for.
2099 static void search_all_symtabs ( DiEpoch ep, Addr ptr,
2100 /*OUT*/DebugInfo** pdi, /*OUT*/Word* symno,
2101 Bool findText )
2103 Word sno;
2104 DebugInfo* di;
2105 Bool inRange;
2107 for (di = debugInfo_list; di != NULL; di = di->next) {
2109 if (!is_DI_valid_for_epoch(di, ep))
2110 continue;
2112 if (findText) {
2113 /* Consider any symbol in the r-x mapped area to be text.
2114 See Comment_Regarding_Text_Range_Checks in storage.c for
2115 details. */
2116 inRange = di->fsm.have_rx_map
2117 && (ML_(find_rx_mapping)(di, ptr, ptr) != NULL);
2118 } else {
2119 inRange = (di->data_present
2120 && di->data_size > 0
2121 && di->data_avma <= ptr
2122 && ptr < di->data_avma + di->data_size)
2124 (di->sdata_present
2125 && di->sdata_size > 0
2126 && di->sdata_avma <= ptr
2127 && ptr < di->sdata_avma + di->sdata_size)
2129 (di->bss_present
2130 && di->bss_size > 0
2131 && di->bss_avma <= ptr
2132 && ptr < di->bss_avma + di->bss_size)
2134 (di->sbss_present
2135 && di->sbss_size > 0
2136 && di->sbss_avma <= ptr
2137 && ptr < di->sbss_avma + di->sbss_size)
2139 (di->rodata_present
2140 && di->rodata_size > 0
2141 && di->rodata_avma <= ptr
2142 && ptr < di->rodata_avma + di->rodata_size);
2145 if (!inRange) continue;
2147 sno = ML_(search_one_symtab) ( di, ptr, findText );
2148 if (sno == -1) goto not_found;
2149 *symno = sno;
2150 *pdi = di;
2151 return;
2154 not_found:
2155 *pdi = NULL;
2159 /* Search all loctabs that we know about to locate ptr at epoch ep. If
2160 *found, set pdi to the relevant DebugInfo, and *locno to the loctab entry
2161 *number within that. If not found, *pdi is set to NULL. */
2162 static void search_all_loctabs ( DiEpoch ep, Addr ptr,
2163 /*OUT*/DebugInfo** pdi, /*OUT*/Word* locno )
2165 Word lno;
2166 DebugInfo* di;
2167 for (di = debugInfo_list; di != NULL; di = di->next) {
2168 if (!is_DI_valid_for_epoch(di, ep))
2169 continue;
2170 if (di->text_present
2171 && di->text_size > 0
2172 && di->text_avma <= ptr
2173 && ptr < di->text_avma + di->text_size) {
2174 lno = ML_(search_one_loctab) ( di, ptr );
2175 if (lno == -1) goto not_found;
2176 *locno = lno;
2177 *pdi = di;
2178 return;
2181 not_found:
2182 *pdi = NULL;
2185 /* Caching of queries to symbol names. */
2186 // Prime number, giving about 6Kbytes cache on 32 bits,
2187 // 12Kbytes cache on 64 bits.
2188 #define N_SYM_NAME_CACHE 509
2190 typedef
2191 struct {
2192 // (sym_epoch, sym_avma) are the hash table key.
2193 DiEpoch sym_epoch;
2194 Addr sym_avma;
2195 // Fields below here are not part of the key.
2196 const HChar* sym_name;
2197 PtrdiffT offset : (sizeof(PtrdiffT)*8)-1;
2198 Bool isText : 1;
2200 Sym_Name_CacheEnt;
2201 /* Sym_Name_CacheEnt associates a queried (epoch, address) pair to the sym
2202 name found. By nature, if a sym name was found, it means the searched
2203 address stored in the cache is an avma (see e.g. search_all_symtabs).
2204 Note however that the caller is responsible to work with 'avma' addresses
2205 e.g. when calling VG_(get_fnname) : m_debuginfo.c has no way to
2206 differentiate an 'svma a' from an 'avma a'. It is however unlikely that
2207 svma would percolate outside of this module. */
2209 static Sym_Name_CacheEnt sym_name_cache[N_SYM_NAME_CACHE];
2211 static const HChar* no_sym_name = "<<<noname>>>";
2212 /* We need a special marker for the address 0 : a not used entry has
2213 a zero sym_avma. So, if ever the 0 address is really queried, we need
2214 to be able to detect there is no sym name for this address.
2215 If on some platforms, 0 is associated to a symbol, the cache would
2216 work properly. */
2218 static void sym_name_cache__invalidate ( void ) {
2219 VG_(memset)(&sym_name_cache, 0, sizeof(sym_name_cache));
2220 sym_name_cache[0].sym_name = no_sym_name;
2223 /* The whole point of this whole big deal: map an (epoch, code address) pair
2224 to a plausible symbol name. Returns False if no idea; otherwise True.
2226 Caller supplies buf. If do_cxx_demangling is False, don't do
2227 C++ demangling, regardless of VG_(clo_demangle) -- probably because the
2228 call has come from VG_(get_fnname_raw)(). findText
2229 indicates whether we're looking for a text symbol or a data symbol
2230 -- caller must choose one kind or the other.
2232 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2233 in pub_tool_debuginfo.h
2234 get_sym_name and the fact it calls the demangler is the main reason
2235 for non persistence of the information returned by m_debuginfo.c
2236 functions : the string returned in *BUF is persistent as long as
2237 (1) the DebugInfo it belongs to is not discarded
2238 (2) the demangler is not invoked again
2239 Also, the returned string is owned by "somebody else". Callers must
2240 not free it or modify it. */
2241 static
2242 Bool get_sym_name ( Bool do_cxx_demangling, Bool do_z_demangling,
2243 Bool do_below_main_renaming,
2244 DiEpoch ep, Addr a, const HChar** buf,
2245 Bool match_anywhere_in_sym, Bool show_offset,
2246 Bool findText, /*OUT*/PtrdiffT* offsetP )
2248 // Compute the hash from 'ep' and 'a'. The latter contains lots of
2249 // significant bits, but 'ep' is expected to be a small number, typically
2250 // less than 500. So rotate it around a bit in the hope of spreading the
2251 // bits out somewhat.
2252 vg_assert(!is_DiEpoch_INVALID(ep));
2253 UWord hash = a ^ (UWord)(ep.n ^ ROL32(ep.n, 5)
2254 ^ ROL32(ep.n, 13) ^ ROL32(ep.n, 19));
2255 hash %= N_SYM_NAME_CACHE;
2257 Sym_Name_CacheEnt* se = &sym_name_cache[hash];
2259 if (UNLIKELY(se->sym_epoch.n != ep.n || se->sym_avma != a
2260 || se->isText != findText)) {
2261 DebugInfo* di;
2262 Word sno;
2264 search_all_symtabs ( ep, a, &di, &sno, findText );
2265 se->sym_epoch = ep;
2266 se->sym_avma = a;
2267 se->isText = findText;
2268 if (di == NULL || a == 0)
2269 se->sym_name = no_sym_name;
2270 else {
2271 vg_assert(di->symtab[sno].pri_name);
2272 se->sym_name = di->symtab[sno].pri_name;
2273 se->offset = a - di->symtab[sno].avmas.main;
2277 if (se->sym_name == no_sym_name
2278 || (!match_anywhere_in_sym && se->offset != 0)) {
2279 *buf = "";
2280 return False;
2283 VG_(demangle) ( do_cxx_demangling, do_z_demangling,
2284 se->sym_name, buf );
2286 /* Do the below-main hack */
2287 // To reduce the endless nuisance of multiple different names
2288 // for "the frame below main()" screwing up the testsuite, change all
2289 // known incarnations of said into a single name, "(below main)", if
2290 // --show-below-main=yes.
2291 if ( do_below_main_renaming && ! VG_(clo_show_below_main)
2292 && Vg_FnNameBelowMain == VG_(get_fnname_kind)(*buf) )
2294 *buf = "(below main)";
2297 if (offsetP) *offsetP = se->offset;
2299 if (show_offset && se->offset != 0) {
2300 static HChar *bufwo; // buf with offset
2301 static SizeT bufwo_szB;
2302 SizeT need, len;
2304 len = VG_(strlen)(*buf);
2305 need = len + 1 + 19 + 1;
2306 if (need > bufwo_szB) {
2307 bufwo = ML_(dinfo_realloc)("get_sym_size", bufwo, need);
2308 bufwo_szB = need;
2311 VG_(strcpy)(bufwo, *buf);
2312 VG_(sprintf)(bufwo + len, "%c%ld",
2313 se->offset < 0 ? '-' : '+',
2314 (PtrdiffT) (se->offset < 0 ? -se->offset : se->offset));
2315 *buf = bufwo;
2318 return True;
2321 /* ppc64be-linux only: find the TOC pointer (R2 value) that should be in
2322 force at the entry point address of the function containing
2323 guest_code_addr. Returns 0 if not known. */
2324 Addr VG_(get_tocptr) ( DiEpoch ep, Addr guest_code_addr )
2326 #if defined(VGA_ppc64be) || defined(VGA_ppc64le)
2327 DebugInfo* si;
2328 Word sno;
2329 search_all_symtabs ( ep, guest_code_addr,
2330 &si, &sno,
2331 True/*consider text symbols only*/ );
2332 if (si == NULL)
2333 return 0;
2334 else
2335 return GET_TOCPTR_AVMA(si->symtab[sno].avmas);
2336 #else
2337 return 0;
2338 #endif
2341 /* This is available to tools... always demangle C++ names,
2342 match anywhere in function, but don't show offsets.
2343 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2344 in pub_tool_debuginfo.h */
2345 Bool VG_(get_fnname) ( DiEpoch ep, Addr a, const HChar** buf )
2347 return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2348 /*below-main-renaming*/True,
2349 ep, a, buf,
2350 /*match_anywhere_in_fun*/True,
2351 /*show offset?*/False,
2352 /*text sym*/True,
2353 /*offsetP*/NULL );
2357 Bool VG_(get_fnname_inl) ( DiEpoch ep, Addr a, const HChar** buf,
2358 const InlIPCursor* iipc )
2360 if (iipc) {
2361 vg_assert(is_DI_valid_for_epoch(iipc->di, ep));
2364 if (is_bottom(iipc)) {
2365 return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2366 /*below-main-renaming*/True,
2367 ep, a, buf,
2368 /*match_anywhere_in_fun*/True,
2369 /*show offset?*/False,
2370 /*text sym*/True,
2371 /*offsetP*/NULL );
2372 } else {
2373 const DiInlLoc *next_inl = iipc && iipc->next_inltab >= 0
2374 ? & iipc->di->inltab[iipc->next_inltab]
2375 : NULL;
2376 vg_assert (next_inl);
2377 *buf = next_inl->inlinedfn;
2378 return True;
2382 /* This is available to tools... always demangle C++ names,
2383 match anywhere in function, and show offset if nonzero.
2384 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2385 in pub_tool_debuginfo.h */
2386 Bool VG_(get_fnname_w_offset) ( DiEpoch ep, Addr a, const HChar** buf )
2388 return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2389 /*below-main-renaming*/True,
2390 ep, a, buf,
2391 /*match_anywhere_in_fun*/True,
2392 /*show offset?*/True,
2393 /*text sym*/True,
2394 /*offsetP*/NULL );
2397 /* This is available to tools... always demangle C++ names,
2398 only succeed if 'a' matches first instruction of function,
2399 and don't show offsets.
2400 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2401 in pub_tool_debuginfo.h */
2402 Bool VG_(get_fnname_if_entry) ( DiEpoch ep, Addr a, const HChar** buf )
2404 const HChar *tmp;
2405 Bool res;
2407 res = get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2408 /*below-main-renaming*/True,
2409 ep, a, &tmp,
2410 /*match_anywhere_in_fun*/False,
2411 /*show offset?*/False,
2412 /*text sym*/True,
2413 /*offsetP*/NULL );
2414 if (res)
2415 *buf = tmp;
2416 return res;
2419 /* This is only available to core... don't C++-demangle, don't Z-demangle,
2420 don't rename below-main, match anywhere in function, and don't show
2421 offsets.
2422 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2423 in pub_tool_debuginfo.h */
2424 Bool VG_(get_fnname_raw) ( DiEpoch ep, Addr a, const HChar** buf )
2426 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2427 /*below-main-renaming*/False,
2428 ep, a, buf,
2429 /*match_anywhere_in_fun*/True,
2430 /*show offset?*/False,
2431 /*text sym*/True,
2432 /*offsetP*/NULL );
2435 /* This is only available to core... don't demangle C++ names, but do
2436 do Z-demangling and below-main-renaming, match anywhere in function, and
2437 don't show offsets.
2438 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2439 in pub_tool_debuginfo.h */
2440 Bool VG_(get_fnname_no_cxx_demangle) ( DiEpoch ep, Addr a, const HChar** buf,
2441 const InlIPCursor* iipc )
2443 // All the callers of VG_(get_fnname_no_cxx_demangle) must build
2444 // the iipc with the same ep as provided to VG_(get_fnname_no_cxx_demangle).
2445 // So, if we have an iipc, iipc->di must be valid in the provided ep.
2446 // Functionally, we could equally use iipc->di->first_epoch or ep, as
2447 // all the inlined fn calls will be described by the same di.
2448 if (iipc) {
2449 vg_assert(is_DI_valid_for_epoch(iipc->di, ep));
2452 if (is_bottom(iipc)) {
2453 // At the bottom (towards main), we describe the fn at eip.
2454 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/True,
2455 /*below-main-renaming*/True,
2456 ep, a, buf,
2457 /*match_anywhere_in_fun*/True,
2458 /*show offset?*/False,
2459 /*text sym*/True,
2460 /*offsetP*/NULL );
2461 } else {
2462 const DiInlLoc *next_inl = iipc && iipc->next_inltab >= 0
2463 ? & iipc->di->inltab[iipc->next_inltab]
2464 : NULL;
2465 vg_assert (next_inl);
2466 // The function we are in is called by next_inl.
2467 *buf = next_inl->inlinedfn;
2468 return True;
2472 /* mips-linux only: find the offset of current address. This is needed for
2473 stack unwinding for MIPS.
2475 Bool VG_(get_inst_offset_in_function)( DiEpoch ep, Addr a,
2476 /*OUT*/PtrdiffT* offset )
2478 const HChar *fnname;
2479 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2480 /*below-main-renaming*/False,
2481 ep, a, &fnname,
2482 /*match_anywhere_in_sym*/True,
2483 /*show offset?*/False,
2484 /*text sym*/True,
2485 offset );
2488 Vg_FnNameKind VG_(get_fnname_kind) ( const HChar* name )
2490 if (VG_STREQ("main", name)) {
2491 return Vg_FnNameMain;
2493 } else if (
2494 # if defined(VGO_linux)
2495 VG_STREQ("__libc_start_main", name) || // glibc glibness
2496 VG_STREQ("__libc_start_call_main", name) || // glibc glibness
2497 VG_STREQN(18, "__libc_start_main.", name) || // gcc optimization
2498 VG_STREQ("generic_start_main", name) || // Yellow Dog doggedness
2499 VG_STREQN(19, "generic_start_main.", name) || // gcc optimization
2500 VG_STREQ("_start", name) ||
2501 # elif defined(VGO_freebsd)
2502 VG_STREQ("_start", name) || // FreeBSD libc
2503 # elif defined(VGO_darwin)
2504 // See readmacho.c for an explanation of this.
2505 VG_STREQ("start_according_to_valgrind", name) || // Darwin, darling
2506 # elif defined(VGO_solaris)
2507 VG_STREQ("_start", name) || // main() is called directly from _start
2508 # else
2509 # error "Unknown OS"
2510 # endif
2511 0) {
2512 return Vg_FnNameBelowMain;
2514 } else {
2515 return Vg_FnNameNormal;
2519 Vg_FnNameKind VG_(get_fnname_kind_from_IP) ( DiEpoch ep, Addr ip )
2521 const HChar *buf;
2523 // We don't demangle, because it's faster not to, and the special names
2524 // we're looking for won't be mangled.
2525 if (VG_(get_fnname_raw) ( ep, ip, &buf )) {
2527 return VG_(get_fnname_kind)(buf);
2528 } else {
2529 return Vg_FnNameNormal; // Don't know the name, treat it as normal.
2533 /* Looks up data_addr in the collection of data symbols, and if found
2534 puts a pointer to its name into dname. The name is zero terminated.
2535 Also data_addr's offset from the symbol start is put into *offset.
2536 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2537 in pub_tool_debuginfo.h */
2538 Bool VG_(get_datasym_and_offset)( DiEpoch ep, Addr data_addr,
2539 /*OUT*/const HChar** dname,
2540 /*OUT*/PtrdiffT* offset )
2542 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2543 /*below-main-renaming*/False,
2544 ep, data_addr, dname,
2545 /*match_anywhere_in_sym*/True,
2546 /*show offset?*/False,
2547 /*text sym*/False,
2548 offset );
2551 /* Map a code address to the name of a shared object file or the
2552 executable. Returns False if no idea; otherwise True.
2553 Note: the string returned in *BUF is persistent as long as
2554 (1) the DebugInfo it belongs to is not discarded
2555 (2) the segment containing the address is not merged with another segment
2557 Bool VG_(get_objname) ( DiEpoch ep, Addr a, const HChar** objname )
2559 DebugInfo* di;
2560 const NSegment *seg;
2561 const HChar* filename;
2563 /* Look in the debugInfo_list to find the name. In most cases we
2564 expect this to produce a result. */
2565 for (di = debugInfo_list; di != NULL; di = di->next) {
2566 if (!is_DI_valid_for_epoch(di, ep))
2567 continue;
2568 if (di->text_present
2569 && di->text_size > 0
2570 && di->text_avma <= a
2571 && a < di->text_avma + di->text_size) {
2572 *objname = di->fsm.filename;
2573 return True;
2576 /* Last-ditch fallback position: if we don't find the address in
2577 the debugInfo_list, ask the address space manager whether it
2578 knows the name of the file associated with this mapping. This
2579 allows us to print the names of exe/dll files in the stack trace
2580 when running programs under wine.
2582 Restrict this to the case where 'ep' is the current epoch, though, so
2583 that we don't return information about this epoch when the caller was
2584 enquiring about a different one. */
2585 if ( eq_DiEpoch(ep, VG_(current_DiEpoch)())
2586 && (seg = VG_(am_find_nsegment)(a)) != NULL
2587 && (filename = VG_(am_get_filename)(seg)) != NULL ) {
2588 *objname = filename;
2589 return True;
2591 return False;
2594 /* Map a code address to its DebugInfo. Returns NULL if not found. Doesn't
2595 require debug info. */
2596 DebugInfo* VG_(find_DebugInfo) ( DiEpoch ep, Addr a )
2598 static UWord n_search = 0;
2599 DebugInfo* di;
2600 n_search++;
2601 for (di = debugInfo_list; di != NULL; di = di->next) {
2602 if (!is_DI_valid_for_epoch(di, ep))
2603 continue;
2604 if (di->text_present
2605 && di->text_size > 0
2606 && di->text_avma <= a
2607 && a < di->text_avma + di->text_size) {
2608 if (0 == (n_search & 0xF))
2609 move_DebugInfo_one_step_forward( di );
2610 return di;
2613 return NULL;
2616 /* Map a code address to a filename. Returns True if successful. The
2617 returned string is persistent as long as the DebugInfo to which it
2618 belongs is not discarded. */
2619 Bool VG_(get_filename)( DiEpoch ep, Addr a, const HChar** filename )
2621 DebugInfo* si;
2622 Word locno;
2623 UInt fndn_ix;
2625 search_all_loctabs ( ep, a, &si, &locno );
2626 if (si == NULL)
2627 return False;
2628 fndn_ix = ML_(fndn_ix) (si, locno);
2629 *filename = ML_(fndn_ix2filename) (si, fndn_ix);
2630 return True;
2633 /* Map a code address to a line number. Returns True if successful. */
2634 Bool VG_(get_linenum)( DiEpoch ep, Addr a, UInt* lineno )
2636 DebugInfo* si;
2637 Word locno;
2638 search_all_loctabs ( ep, a, &si, &locno );
2639 if (si == NULL)
2640 return False;
2641 *lineno = si->loctab[locno].lineno;
2643 return True;
2646 /* Map a code address to a filename/line number/dir name info.
2647 See prototype for detailed description of behaviour.
2649 Bool VG_(get_filename_linenum) ( DiEpoch ep, Addr a,
2650 /*OUT*/const HChar** filename,
2651 /*OUT*/const HChar** dirname,
2652 /*OUT*/UInt* lineno )
2654 DebugInfo* si;
2655 Word locno;
2656 UInt fndn_ix;
2658 search_all_loctabs ( ep, a, &si, &locno );
2659 if (si == NULL) {
2660 if (dirname) {
2661 *dirname = "";
2663 *filename = ""; // this used to be not initialised....
2664 return False;
2667 fndn_ix = ML_(fndn_ix)(si, locno);
2668 *filename = ML_(fndn_ix2filename) (si, fndn_ix);
2669 *lineno = si->loctab[locno].lineno;
2671 if (dirname) {
2672 /* caller wants directory info too .. */
2673 *dirname = ML_(fndn_ix2dirname) (si, fndn_ix);
2676 return True;
2680 /* Map a function name to its entry point and toc pointer. Is done by
2681 sequential search of all symbol tables, so is very slow. To
2682 mitigate the worst performance effects, you may specify a soname
2683 pattern, and only objects matching that pattern are searched.
2684 Therefore specify "*" to search all the objects. On TOC-afflicted
2685 platforms, a symbol is deemed to be found only if it has a nonzero
2686 TOC pointer. */
2687 Bool VG_(lookup_symbol_SLOW)(DiEpoch ep,
2688 const HChar* sopatt, const HChar* name,
2689 SymAVMAs* avmas)
2691 Bool require_pToc = False;
2692 Int i;
2693 const DebugInfo* si;
2694 Bool debug = False;
2695 # if defined(VG_PLAT_USES_PPCTOC)
2696 require_pToc = True;
2697 # endif
2698 for (si = debugInfo_list; si; si = si->next) {
2699 if (debug)
2700 VG_(printf)("lookup_symbol_SLOW: considering %s\n", si->soname);
2701 if (!is_DI_valid_for_epoch(si, ep))
2702 continue;
2703 if (!VG_(string_match)(sopatt, si->soname)) {
2704 if (debug)
2705 VG_(printf)(" ... skip\n");
2706 continue;
2708 for (i = 0; i < si->symtab_used; i++) {
2709 const HChar* pri_name = si->symtab[i].pri_name;
2710 vg_assert(pri_name);
2711 if (0==VG_(strcmp)(name, pri_name)
2712 && (require_pToc ? GET_TOCPTR_AVMA(si->symtab[i].avmas) : True)) {
2713 *avmas = si->symtab[i].avmas;
2714 return True;
2716 const HChar** sec_names = si->symtab[i].sec_names;
2717 if (sec_names) {
2718 vg_assert(sec_names[0]);
2719 while (*sec_names) {
2720 if (0==VG_(strcmp)(name, *sec_names)
2721 && (require_pToc
2722 ? GET_TOCPTR_AVMA(si->symtab[i].avmas) : True)) {
2723 *avmas = si->symtab[i].avmas;
2724 return True;
2726 sec_names++;
2731 return False;
2735 /* VG_(describe_IP): return info on code address, function name and
2736 filename. The returned string is allocated in a static buffer and will
2737 be overwritten in the next invocation. */
2739 /* Copy str into *buf starting at n, ensuring that buf is zero-terminated.
2740 Return the index of the terminating null character. */
2741 static SizeT
2742 putStr( SizeT n, HChar** buf, SizeT *bufsiz, const HChar* str )
2744 SizeT slen = VG_(strlen)(str);
2745 SizeT need = n + slen + 1;
2747 if (need > *bufsiz) {
2748 if (need < 256) need = 256;
2749 *bufsiz = need;
2750 *buf = ML_(dinfo_realloc)("putStr", *buf, *bufsiz);
2753 VG_(strcpy)(*buf + n, str);
2755 return n + slen;
2758 /* Same as putStr, but escaping chars for XML output. */
2759 static SizeT
2760 putStrEsc( SizeT n, HChar** buf, SizeT *bufsiz, const HChar* str )
2762 HChar alt[2];
2764 for (; *str != 0; str++) {
2765 switch (*str) {
2766 case '&':
2767 n = putStr( n, buf, bufsiz, "&amp;");
2768 break;
2769 case '<':
2770 n = putStr( n, buf, bufsiz, "&lt;");
2771 break;
2772 case '>':
2773 n = putStr( n, buf, bufsiz, "&gt;");
2774 break;
2775 default:
2776 alt[0] = *str;
2777 alt[1] = 0;
2778 n = putStr( n, buf, bufsiz, alt );
2779 break;
2782 return n;
2785 const HChar* VG_(describe_IP)(DiEpoch ep, Addr eip, const InlIPCursor *iipc)
2787 static HChar *buf = NULL;
2788 static SizeT bufsiz = 0;
2789 # define APPEND(_str) \
2790 n = putStr(n, &buf, &bufsiz, _str)
2791 # define APPEND_ESC(_str) \
2792 n = putStrEsc(n, &buf, &bufsiz, _str)
2794 UInt lineno;
2795 HChar ibuf[50]; // large enough
2796 SizeT n = 0;
2798 // An InlIPCursor is associated with one specific DebugInfo. So if
2799 // it exists, make sure that it is valid for the specified DiEpoch.
2800 vg_assert (!iipc
2801 || (is_DI_valid_for_epoch(iipc->di, ep) && iipc->eip == eip));
2803 const HChar *buf_fn;
2804 const HChar *buf_obj;
2805 const HChar *buf_srcloc;
2806 const HChar *buf_dirname;
2808 Bool know_dirinfo;
2809 Bool know_fnname;
2810 Bool know_objname;
2811 Bool know_srcloc;
2813 if (iipc && iipc->di)
2814 VG_(load_di) (iipc->di, eip);
2815 else
2816 VG_(addr_load_di) (eip);
2818 if (is_bottom(iipc)) {
2819 // At the bottom (towards main), we describe the fn at eip.
2820 know_fnname = VG_(clo_sym_offsets)
2821 ? VG_(get_fnname_w_offset) (ep, eip, &buf_fn)
2822 : VG_(get_fnname) (ep, eip, &buf_fn);
2823 } else {
2824 const DiInlLoc *next_inl = iipc && iipc->di && iipc->next_inltab >= 0
2825 ? & iipc->di->inltab[iipc->next_inltab]
2826 : NULL;
2827 vg_assert (next_inl);
2828 // The function we are in is called by next_inl.
2829 buf_fn = next_inl->inlinedfn;
2830 know_fnname = True;
2832 // INLINED????
2833 // ??? Can we compute an offset for an inlined fn call ?
2834 // ??? Offset from what ? The beginning of the inl info ?
2835 // ??? But that is not necessarily the beginning of the fn
2836 // ??? as e.g. an inlined fn call can be in several ranges.
2837 // ??? Currently never showing an offset.
2840 know_objname = VG_(get_objname)(ep, eip, &buf_obj);
2842 if (is_top(iipc)) {
2843 // The source for the highest level is in the loctab entry.
2844 know_srcloc = VG_(get_filename_linenum)(
2845 ep, eip,
2846 &buf_srcloc,
2847 &buf_dirname,
2848 &lineno
2850 know_dirinfo = buf_dirname[0] != '\0';
2851 } else {
2852 const DiInlLoc *cur_inl = iipc && iipc->di && iipc->cur_inltab >= 0
2853 ? & iipc->di->inltab[iipc->cur_inltab]
2854 : NULL;
2855 vg_assert (cur_inl);
2857 know_dirinfo = False;
2858 buf_dirname = "";
2859 // The fndn_ix and lineno for the caller of the inlined fn is in cur_inl.
2860 if (cur_inl->fndn_ix == 0) {
2861 buf_srcloc = "???";
2862 } else {
2863 FnDn *fndn = VG_(indexEltNumber) (iipc->di->fndnpool,
2864 cur_inl->fndn_ix);
2865 if (fndn->dirname) {
2866 buf_dirname = fndn->dirname;
2867 know_dirinfo = True;
2869 buf_srcloc = fndn->filename;
2871 lineno = cur_inl->lineno;
2872 know_srcloc = True;
2875 if (VG_(clo_xml)) {
2877 Bool human_readable = True;
2878 const HChar* maybe_newline = human_readable ? "\n " : "";
2879 const HChar* maybe_newline2 = human_readable ? "\n " : "";
2881 /* Print in XML format, dumping in as much info as we know.
2882 Ensure all tags are balanced. */
2883 APPEND("<frame>");
2884 VG_(sprintf)(ibuf,"<ip>0x%lX</ip>", eip);
2885 APPEND(maybe_newline);
2886 APPEND(ibuf);
2887 if (know_objname) {
2888 APPEND(maybe_newline);
2889 APPEND("<obj>");
2890 APPEND_ESC(buf_obj);
2891 APPEND("</obj>");
2893 if (know_fnname) {
2894 APPEND(maybe_newline);
2895 APPEND("<fn>");
2896 APPEND_ESC(buf_fn);
2897 APPEND("</fn>");
2899 if (know_srcloc) {
2900 if (know_dirinfo) {
2901 APPEND(maybe_newline);
2902 APPEND("<dir>");
2903 APPEND_ESC(buf_dirname);
2904 APPEND("</dir>");
2906 APPEND(maybe_newline);
2907 APPEND("<file>");
2908 APPEND_ESC(buf_srcloc);
2909 APPEND("</file>");
2910 APPEND(maybe_newline);
2911 APPEND("<line>");
2912 VG_(sprintf)(ibuf,"%u",lineno);
2913 APPEND(ibuf);
2914 APPEND("</line>");
2916 APPEND(maybe_newline2);
2917 APPEND("</frame>");
2919 } else {
2921 /* Print for humans to read */
2923 // Possible forms:
2925 // 0x80483BF: really (a.c:20)
2926 // 0x80483BF: really (in /foo/a.out)
2927 // 0x80483BF: really (in ???)
2928 // 0x80483BF: ??? (in /foo/a.out)
2929 // 0x80483BF: ??? (a.c:20)
2930 // 0x80483BF: ???
2932 VG_(sprintf)(ibuf,"0x%lX: ", eip);
2933 APPEND(ibuf);
2934 if (know_fnname) {
2935 APPEND(buf_fn);
2936 } else {
2937 APPEND("???");
2939 if (know_srcloc) {
2940 APPEND(" (");
2941 // Get the directory name, if any, possibly pruned, into dirname.
2942 const HChar* dirname = NULL;
2943 if (know_dirinfo && VG_(sizeXA)(VG_(clo_fullpath_after)) > 0) {
2944 Int i;
2945 dirname = buf_dirname;
2946 // Remove leading prefixes from the dirname.
2947 // If user supplied --fullpath-after=foo, this will remove
2948 // a leading string which matches '.*foo' (not greedy).
2949 for (i = 0; i < VG_(sizeXA)(VG_(clo_fullpath_after)); i++) {
2950 const HChar* prefix =
2951 *(HChar**) VG_(indexXA)( VG_(clo_fullpath_after), i );
2952 HChar* str = VG_(strstr)(dirname, prefix);
2953 if (str) {
2954 dirname = str + VG_(strlen)(prefix);
2955 break;
2958 /* remove leading "./" */
2959 if (dirname[0] == '.' && dirname[1] == '/')
2960 dirname += 2;
2962 // do we have any interesting directory name to show? If so
2963 // add it in.
2964 if (dirname && dirname[0] != 0) {
2965 APPEND(dirname);
2966 APPEND("/");
2968 APPEND(buf_srcloc);
2969 APPEND(":");
2970 VG_(sprintf)(ibuf,"%u",lineno);
2971 APPEND(ibuf);
2972 APPEND(")");
2973 } else if (know_objname) {
2974 APPEND(" (in ");
2975 APPEND(buf_obj);
2976 APPEND(")");
2977 } else if (know_fnname) {
2978 // Nb: do this in two steps because "??)" is a trigraph!
2979 APPEND(" (in ???");
2980 APPEND(")");
2984 return buf;
2986 # undef APPEND
2987 # undef APPEND_ESC
2991 /*--------------------------------------------------------------*/
2992 /*--- ---*/
2993 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
2994 /*--- DWARF3 .eh_frame INFO ---*/
2995 /*--- ---*/
2996 /*--------------------------------------------------------------*/
2998 /* Note that the CFI machinery pertains to unwinding the stack "right now".
2999 There is no support for unwinding stack images obtained from some time in
3000 the past. That means that:
3002 (1) We only deal with CFI from DebugInfos that are valid for the current
3003 debuginfo epoch. Unlike in the rest of the file, there is no
3004 epoch-awareness.
3006 (2) We assume that the CFI cache will be invalidated every time the the
3007 epoch changes. This is done by ensuring (in the file above) that
3008 every call to advance_current_DiEpoch has a call to
3009 caches__invalidate alongside it.
3012 /* Gather up all the constant pieces of info needed to evaluate
3013 a CfiExpr into one convenient struct. */
3014 typedef
3015 struct {
3016 const D3UnwindRegs* uregs;
3017 Addr min_accessible;
3018 Addr max_accessible;
3020 CfiExprEvalContext;
3022 /* Evaluate the CfiExpr rooted at ix in exprs given the context eec.
3023 *ok is set to False on failure, but not to True on success. The
3024 caller must set it to True before calling. */
3025 __attribute__((noinline))
3026 static
3027 UWord evalCfiExpr ( const XArray* exprs, Int ix,
3028 const CfiExprEvalContext* eec, Bool* ok )
3030 UWord w, wL, wR;
3031 Addr a;
3032 const CfiExpr* e;
3033 vg_assert(sizeof(Addr) == sizeof(UWord));
3034 e = VG_(indexXA)( exprs, ix );
3035 switch (e->tag) {
3036 case Cex_Unop:
3037 w = evalCfiExpr( exprs, e->Cex.Unop.ix, eec, ok );
3038 if (!(*ok)) return 0;
3039 switch (e->Cex.Unop.op) {
3040 case Cunop_Abs: return (Word) w < 0 ? - w : w;
3041 case Cunop_Neg: return - (Word) w;
3042 case Cunop_Not: return ~ w;
3043 default: goto unhandled;
3045 /*NOTREACHED*/
3046 case Cex_Binop:
3047 wL = evalCfiExpr( exprs, e->Cex.Binop.ixL, eec, ok );
3048 if (!(*ok)) return 0;
3049 wR = evalCfiExpr( exprs, e->Cex.Binop.ixR, eec, ok );
3050 if (!(*ok)) return 0;
3051 switch (e->Cex.Binop.op) {
3052 case Cbinop_Add: return wL + wR;
3053 case Cbinop_Sub: return wL - wR;
3054 case Cbinop_And: return wL & wR;
3055 case Cbinop_Mul: return wL * wR;
3056 case Cbinop_Shl: return wL << wR;
3057 case Cbinop_Shr: return wL >> wR;
3058 case Cbinop_Eq: return wL == wR ? 1 : 0;
3059 case Cbinop_Ge: return (Word) wL >= (Word) wR ? 1 : 0;
3060 case Cbinop_Gt: return (Word) wL > (Word) wR ? 1 : 0;
3061 case Cbinop_Le: return (Word) wL <= (Word) wR ? 1 : 0;
3062 case Cbinop_Lt: return (Word) wL < (Word) wR ? 1 : 0;
3063 case Cbinop_Ne: return wL != wR ? 1 : 0;
3064 default: goto unhandled;
3066 /*NOTREACHED*/
3067 case Cex_CfiReg:
3068 switch (e->Cex.CfiReg.reg) {
3069 # if defined(VGA_x86) || defined(VGA_amd64)
3070 case Creg_IA_IP: return eec->uregs->xip;
3071 case Creg_IA_SP: return eec->uregs->xsp;
3072 case Creg_IA_BP: return eec->uregs->xbp;
3073 # elif defined(VGA_arm)
3074 case Creg_ARM_R15: return eec->uregs->r15;
3075 case Creg_ARM_R14: return eec->uregs->r14;
3076 case Creg_ARM_R13: return eec->uregs->r13;
3077 case Creg_ARM_R12: return eec->uregs->r12;
3078 case Creg_ARM_R7: return eec->uregs->r7;
3079 # elif defined(VGA_s390x)
3080 case Creg_S390_IA: return eec->uregs->ia;
3081 case Creg_S390_SP: return eec->uregs->sp;
3082 case Creg_S390_FP: return eec->uregs->fp;
3083 case Creg_S390_LR: return eec->uregs->lr;
3084 # elif defined(VGA_mips32) || defined(VGA_mips64) \
3085 || defined(VGA_nanomips)
3086 case Creg_IA_IP: return eec->uregs->pc;
3087 case Creg_IA_SP: return eec->uregs->sp;
3088 case Creg_IA_BP: return eec->uregs->fp;
3089 case Creg_MIPS_RA: return eec->uregs->ra;
3090 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) \
3091 || defined(VGA_ppc64le)
3092 # elif defined(VGP_arm64_linux) || defined(VGP_arm64_freebsd)
3093 case Creg_ARM64_SP: return eec->uregs->sp;
3094 case Creg_ARM64_X30: return eec->uregs->x30;
3095 case Creg_ARM64_X29: return eec->uregs->x29;
3096 # else
3097 # error "Unsupported arch"
3098 # endif
3099 default: goto unhandled;
3101 /*NOTREACHED*/
3102 case Cex_Const:
3103 return e->Cex.Const.con;
3104 case Cex_Deref:
3105 a = evalCfiExpr( exprs, e->Cex.Deref.ixAddr, eec, ok );
3106 if (!(*ok)) return 0;
3107 if (a < eec->min_accessible
3108 || a > eec->max_accessible - sizeof(UWord) + 1) {
3109 *ok = False;
3110 return 0;
3112 /* let's hope it doesn't trap! */
3113 return ML_(read_UWord)((void *)a);
3114 default:
3115 goto unhandled;
3117 /*NOTREACHED*/
3118 unhandled:
3119 VG_(printf)("\n\nevalCfiExpr: unhandled\n");
3120 ML_(ppCfiExpr)( exprs, ix );
3121 VG_(printf)("\n");
3122 vg_assert(0);
3123 /*NOTREACHED*/
3124 return 0;
3128 /* Search all the DebugInfos in the entire system, to find the DiCfSI_m
3129 that pertains to 'ip'.
3131 If found, set *diP to the DebugInfo in which it resides, and
3132 *cfsi_mP to the cfsi_m pointer in that DebugInfo's cfsi_m_pool.
3134 If not found, set *diP to (DebugInfo*)1 and *cfsi_mP to zero.
3136 Per comments at the top of this section, we only look for CFI in
3137 DebugInfos that are valid for the current epoch.
3139 __attribute__((noinline))
3140 static void find_DiCfSI ( /*OUT*/DebugInfo** diP,
3141 /*OUT*/DiCfSI_m** cfsi_mP,
3142 Addr ip )
3144 DebugInfo* di;
3145 Word i = -1;
3147 static UWord n_search = 0;
3148 static UWord n_steps = 0;
3149 n_search++;
3151 if (0) VG_(printf)("search for %#lx\n", ip);
3153 DiEpoch curr_epoch = VG_(current_DiEpoch)();
3155 for (di = debugInfo_list; di != NULL; di = di->next) {
3156 Word j;
3157 n_steps++;
3159 if (!is_DI_valid_for_epoch(di, curr_epoch))
3160 continue;
3162 VG_(load_di)(di, ip);
3164 /* Use the per-DebugInfo summary address ranges to skip
3165 inapplicable DebugInfos quickly. */
3166 if (di->cfsi_used == 0)
3167 continue;
3168 if (ip < di->cfsi_minavma || ip > di->cfsi_maxavma)
3169 continue;
3171 // This di must be active (because we have explicitly chosen not to
3172 // allow unwinding stacks that pertain to some past epoch). It can't
3173 // be archived or not-yet-active.
3174 vg_assert(is_DebugInfo_active(di));
3176 /* It might be in this DebugInfo. Search it. */
3177 j = ML_(search_one_cfitab)( di, ip );
3178 vg_assert(j >= -1 && j < (Word)di->cfsi_used);
3180 if (j != -1) {
3181 i = j;
3182 break; /* found it */
3186 if (i == -1) {
3188 /* we didn't find it. */
3189 *diP = (DebugInfo*)1;
3190 *cfsi_mP = 0;
3192 } else {
3194 /* found a di corresponding to ip. */
3195 /* ensure that di is 4-aligned (at least), so it can't possibly
3196 be equal to (DebugInfo*)1. */
3197 vg_assert(di && VG_IS_4_ALIGNED(di));
3198 *cfsi_mP = ML_(get_cfsi_m) (di, i);
3199 if (*cfsi_mP == NULL) {
3200 // This is a cfsi hole. Report no cfi information found.
3201 *diP = (DebugInfo*)1;
3202 // But we will still perform the hack below.
3203 } else {
3204 *diP = di;
3207 /* Start of performance-enhancing hack: once every 64 (chosen
3208 hackily after profiling) successful searches, move the found
3209 DebugInfo one step closer to the start of the list. This
3210 makes future searches cheaper. For starting konqueror on
3211 amd64, this in fact reduces the total amount of searching
3212 done by the above find-the-right-DebugInfo loop by more than
3213 a factor of 20. */
3214 if ((n_search & 0xF) == 0) {
3215 /* Move di one step closer to the start of the list. */
3216 move_DebugInfo_one_step_forward( di );
3218 /* End of performance-enhancing hack. */
3220 if (0 && ((n_search & 0x7FFFF) == 0))
3221 VG_(printf)("find_DiCfSI: %lu searches, "
3222 "%lu DebugInfos looked at\n",
3223 n_search, n_steps);
3230 /* Now follows a mechanism for caching queries to find_DiCfSI, since
3231 they are extremely frequent on amd64-linux, during stack unwinding.
3233 Each cache entry binds an ip value to a (di, cfsi_m*) pair. Possible
3234 values:
3236 di is non-null, cfsi_m* >= 0 ==> cache slot in use, "cfsi_m*"
3237 di is (DebugInfo*)1 ==> cache slot in use, no associated di
3238 di is NULL ==> cache slot not in use
3240 Hence simply zeroing out the entire cache invalidates all
3241 entries.
3243 We can map an ip value directly to a (di, cfsi_m*) pair as
3244 once a DebugInfo is read, adding new DiCfSI_m* is not possible
3245 anymore, as the cfsi_m_pool is frozen once the reading is terminated.
3246 Also, the cache is invalidated when new debuginfo is read due to
3247 an mmap or some debuginfo is discarded due to an munmap. */
3249 // Prime number, giving about 6Kbytes cache on 32 bits,
3250 // 12Kbytes cache on 64 bits.
3251 #define N_CFSI_M_CACHE 509
3253 typedef
3254 struct { Addr ip; DebugInfo* di; DiCfSI_m* cfsi_m; }
3255 CFSI_m_CacheEnt;
3257 static CFSI_m_CacheEnt cfsi_m_cache[N_CFSI_M_CACHE];
3259 static void cfsi_m_cache__invalidate ( void ) {
3260 VG_(memset)(&cfsi_m_cache, 0, sizeof(cfsi_m_cache));
3263 static inline CFSI_m_CacheEnt* cfsi_m_cache__find ( Addr ip )
3265 UWord hash = ip % N_CFSI_M_CACHE;
3266 CFSI_m_CacheEnt* ce = &cfsi_m_cache[hash];
3267 # ifdef N_Q_M_STATS
3268 static UWord n_q = 0, n_m = 0;
3269 n_q++;
3270 if (0 == (n_q & 0x1FFFFF))
3271 VG_(printf)("QQQ %lu %lu\n", n_q, n_m);
3272 # endif
3274 if (LIKELY(ce->ip == ip) && LIKELY(ce->di != NULL)) {
3275 /* found an entry in the cache .. */
3276 } else {
3277 /* not found in cache. Search and update. */
3278 # ifdef N_Q_M_STATS
3279 n_m++;
3280 # endif
3281 ce->ip = ip;
3282 find_DiCfSI( &ce->di, &ce->cfsi_m, ip );
3285 if (UNLIKELY(ce->di == (DebugInfo*)1)) {
3286 /* no DiCfSI for this address */
3287 return NULL;
3288 } else {
3289 /* found a DiCfSI for this address */
3290 return ce;
3294 Bool VG_(has_CF_info)(Addr a)
3296 return cfsi_m_cache__find (a) != NULL;
3301 inline
3302 static Addr compute_cfa ( const D3UnwindRegs* uregs,
3303 Addr min_accessible, Addr max_accessible,
3304 const DebugInfo* di, const DiCfSI_m* cfsi_m )
3306 CfiExprEvalContext eec;
3307 Addr cfa;
3308 Bool ok;
3310 /* Compute the CFA. */
3311 cfa = 0;
3312 switch (cfsi_m->cfa_how) {
3313 # if defined(VGA_x86) || defined(VGA_amd64)
3314 case CFIC_IA_SPREL:
3315 cfa = cfsi_m->cfa_off + uregs->xsp;
3316 break;
3317 case CFIC_IA_BPREL:
3318 cfa = cfsi_m->cfa_off + uregs->xbp;
3319 break;
3320 # elif defined(VGA_arm)
3321 case CFIC_ARM_R13REL:
3322 cfa = cfsi_m->cfa_off + uregs->r13;
3323 break;
3324 case CFIC_ARM_R12REL:
3325 cfa = cfsi_m->cfa_off + uregs->r12;
3326 break;
3327 case CFIC_ARM_R11REL:
3328 cfa = cfsi_m->cfa_off + uregs->r11;
3329 break;
3330 case CFIC_ARM_R7REL:
3331 cfa = cfsi_m->cfa_off + uregs->r7;
3332 break;
3333 # elif defined(VGA_s390x)
3334 case CFIC_IA_SPREL:
3335 cfa = cfsi_m->cfa_off + uregs->sp;
3336 break;
3337 case CFIR_MEMCFAREL:
3339 Addr a = uregs->sp + cfsi_m->cfa_off;
3340 if (a < min_accessible || a > max_accessible-sizeof(Addr))
3341 break;
3342 cfa = ML_(read_Addr)((void *)a);
3343 break;
3345 case CFIR_SAME:
3346 cfa = uregs->fp;
3347 break;
3348 case CFIC_IA_BPREL:
3349 cfa = cfsi_m->cfa_off + uregs->fp;
3350 break;
3351 # elif defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)
3352 case CFIC_IA_SPREL:
3353 cfa = cfsi_m->cfa_off + uregs->sp;
3354 break;
3355 case CFIR_SAME:
3356 cfa = uregs->fp;
3357 break;
3358 case CFIC_IA_BPREL:
3359 cfa = cfsi_m->cfa_off + uregs->fp;
3360 break;
3361 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3362 # elif defined(VGP_arm64_linux)
3363 case CFIC_ARM64_SPREL:
3364 cfa = cfsi_m->cfa_off + uregs->sp;
3365 break;
3366 case CFIC_ARM64_X29REL:
3367 cfa = cfsi_m->cfa_off + uregs->x29;
3368 break;
3369 # elif defined(VGP_arm64_freebsd)
3370 case CFIC_ARM64_SPREL:
3371 cfa = cfsi_m->cfa_off + uregs->sp;
3372 break;
3373 case CFIC_ARM64_X29REL:
3374 cfa = cfsi_m->cfa_off + uregs->x29;
3375 break;
3377 # else
3378 # error "Unsupported arch"
3379 # endif
3380 case CFIC_EXPR: /* available on all archs */
3381 if (0) {
3382 VG_(printf)("CFIC_EXPR: ");
3383 ML_(ppCfiExpr)(di->cfsi_exprs, cfsi_m->cfa_off);
3384 VG_(printf)("\n");
3386 eec.uregs = uregs;
3387 eec.min_accessible = min_accessible;
3388 eec.max_accessible = max_accessible;
3389 ok = True;
3390 cfa = evalCfiExpr(di->cfsi_exprs, cfsi_m->cfa_off, &eec, &ok );
3391 if (!ok) return 0;
3392 break;
3393 default:
3394 vg_assert(0);
3396 return cfa;
3400 /* Get the call frame address (CFA) given an IP/SP/FP triple. */
3401 /* NOTE: This function may rearrange the order of entries in the
3402 DebugInfo list. */
3403 Addr ML_(get_CFA) ( Addr ip, Addr sp, Addr fp,
3404 Addr min_accessible, Addr max_accessible )
3406 CFSI_m_CacheEnt* ce;
3408 ce = cfsi_m_cache__find(ip);
3410 if (UNLIKELY(ce == NULL))
3411 return 0; /* no info. Nothing we can do. */
3413 /* Temporary impedance-matching kludge so that this keeps working
3414 on x86-linux and amd64-linux. */
3415 # if defined(VGA_x86) || defined(VGA_amd64)
3416 { D3UnwindRegs uregs;
3417 uregs.xip = ip;
3418 uregs.xsp = sp;
3419 uregs.xbp = fp;
3420 return compute_cfa(&uregs,
3421 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3423 #elif defined(VGA_s390x)
3424 { D3UnwindRegs uregs;
3425 uregs.ia = ip;
3426 uregs.sp = sp;
3427 uregs.fp = fp;
3428 /* JRS FIXME 3 Apr 2019: surely we can do better for f0..f7 */
3429 uregs.f0 = 0;
3430 uregs.f1 = 0;
3431 uregs.f2 = 0;
3432 uregs.f3 = 0;
3433 uregs.f4 = 0;
3434 uregs.f5 = 0;
3435 uregs.f6 = 0;
3436 uregs.f7 = 0;
3437 return compute_cfa(&uregs,
3438 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3440 #elif defined(VGA_mips32) || defined(VGA_mips64)
3441 { D3UnwindRegs uregs;
3442 uregs.pc = ip;
3443 uregs.sp = sp;
3444 uregs.fp = fp;
3445 return compute_cfa(&uregs,
3446 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3449 # else
3450 return 0; /* indicates failure */
3451 # endif
3454 void VG_(ppUnwindInfo) (Addr from, Addr to)
3456 DebugInfo* di;
3457 CFSI_m_CacheEnt* ce;
3458 Addr ce_from;
3459 CFSI_m_CacheEnt* next_ce;
3462 ce = cfsi_m_cache__find(from);
3463 ce_from = from;
3464 while (from <= to) {
3465 from++;
3466 next_ce = cfsi_m_cache__find(from);
3467 if ((ce == NULL && next_ce != NULL)
3468 || (ce != NULL && next_ce == NULL)
3469 || (ce != NULL && next_ce != NULL && ce->cfsi_m != next_ce->cfsi_m)
3470 || from > to) {
3471 if (ce == NULL) {
3472 VG_(printf)("[%#lx .. %#lx]: no CFI info\n", ce_from, from-1);
3473 } else {
3474 di = ce->di;
3475 ML_(ppDiCfSI)(di->cfsi_exprs,
3476 ce_from, from - ce_from,
3477 ce->cfsi_m);
3479 ce = next_ce;
3480 ce_from = from;
3486 /* The main function for DWARF2/3 CFI-based stack unwinding. Given a
3487 set of registers in UREGS, modify it to hold the register values
3488 for the previous frame, if possible. Returns True if successful.
3489 If not successful, *UREGS is not changed.
3491 For x86 and amd64, the unwound registers are: {E,R}IP,
3492 {E,R}SP, {E,R}BP.
3494 For arm, the unwound registers are: R7 R11 R12 R13 R14 R15.
3496 For arm64, the unwound registers are: X29(FP) X30(LR) SP PC.
3498 For s390, the unwound registers are: R11(FP) R14(LR) R15(SP) F0..F7 PC.
3500 Bool VG_(use_CF_info) ( /*MOD*/D3UnwindRegs* uregsHere,
3501 Addr min_accessible,
3502 Addr max_accessible )
3504 DebugInfo* di;
3505 DiCfSI_m* cfsi_m = NULL;
3506 Addr cfa, ipHere = 0;
3507 CFSI_m_CacheEnt* ce;
3508 CfiExprEvalContext eec __attribute__((unused));
3509 D3UnwindRegs uregsPrev;
3511 # if defined(VGA_x86) || defined(VGA_amd64)
3512 ipHere = uregsHere->xip;
3513 # elif defined(VGA_arm)
3514 ipHere = uregsHere->r15;
3515 # elif defined(VGA_s390x)
3516 ipHere = uregsHere->ia;
3517 # elif defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)
3518 ipHere = uregsHere->pc;
3519 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3520 # elif defined(VGP_arm64_linux)
3521 ipHere = uregsHere->pc;
3522 # elif defined(VGP_arm64_freebsd)
3523 ipHere = uregsHere->pc;
3524 # else
3525 # error "Unknown arch"
3526 # endif
3527 ce = cfsi_m_cache__find(ipHere);
3529 if (UNLIKELY(ce == NULL))
3530 return False; /* no info. Nothing we can do. */
3532 di = ce->di;
3533 cfsi_m = ce->cfsi_m;
3535 if (0) {
3536 VG_(printf)("found cfsi_m (but printing fake base/len): ");
3537 ML_(ppDiCfSI)(di->cfsi_exprs, 0, 0, cfsi_m);
3540 VG_(bzero_inline)(&uregsPrev, sizeof(uregsPrev));
3542 /* First compute the CFA. */
3543 cfa = compute_cfa(uregsHere,
3544 min_accessible, max_accessible, di, cfsi_m);
3545 if (UNLIKELY(cfa == 0))
3546 return False;
3548 /* Now we know the CFA, use it to roll back the registers we're
3549 interested in. */
3551 # if defined(VGA_mips64) && defined(VGABI_N32)
3552 # define READ_REGISTER(addr) ML_(read_ULong)((addr))
3553 # else
3554 # define READ_REGISTER(addr) ML_(read_Addr)((addr))
3555 # endif
3557 # if defined(VGA_s390x)
3558 const Bool is_s390x = True;
3559 const Addr old_S390X_F0 = uregsHere->f0;
3560 const Addr old_S390X_F1 = uregsHere->f1;
3561 const Addr old_S390X_F2 = uregsHere->f2;
3562 const Addr old_S390X_F3 = uregsHere->f3;
3563 const Addr old_S390X_F4 = uregsHere->f4;
3564 const Addr old_S390X_F5 = uregsHere->f5;
3565 const Addr old_S390X_F6 = uregsHere->f6;
3566 const Addr old_S390X_F7 = uregsHere->f7;
3567 # else
3568 const Bool is_s390x = False;
3569 const Addr old_S390X_F0 = 0;
3570 const Addr old_S390X_F1 = 0;
3571 const Addr old_S390X_F2 = 0;
3572 const Addr old_S390X_F3 = 0;
3573 const Addr old_S390X_F4 = 0;
3574 const Addr old_S390X_F5 = 0;
3575 const Addr old_S390X_F6 = 0;
3576 const Addr old_S390X_F7 = 0;
3577 # endif
3579 # define COMPUTE(_prev, _here, _how, _off) \
3580 do { \
3581 switch (_how) { \
3582 case CFIR_UNKNOWN: \
3583 return False; \
3584 case CFIR_SAME: \
3585 _prev = _here; break; \
3586 case CFIR_MEMCFAREL: { \
3587 Addr a = cfa + (Word)_off; \
3588 if (a < min_accessible \
3589 || a > max_accessible-sizeof(Addr)) \
3590 return False; \
3591 _prev = READ_REGISTER((void *)a); \
3592 break; \
3594 case CFIR_CFAREL: \
3595 _prev = cfa + (Word)_off; \
3596 break; \
3597 case CFIR_EXPR: \
3598 if (0) \
3599 ML_(ppCfiExpr)(di->cfsi_exprs,_off); \
3600 eec.uregs = uregsHere; \
3601 eec.min_accessible = min_accessible; \
3602 eec.max_accessible = max_accessible; \
3603 Bool ok = True; \
3604 _prev = evalCfiExpr(di->cfsi_exprs, _off, &eec, &ok ); \
3605 if (!ok) return False; \
3606 break; \
3607 case CFIR_S390X_F0: \
3608 if (is_s390x) { _prev = old_S390X_F0; break; } \
3609 vg_assert(0+0-0); \
3610 case CFIR_S390X_F1: \
3611 if (is_s390x) { _prev = old_S390X_F1; break; } \
3612 vg_assert(0+1-1); \
3613 case CFIR_S390X_F2: \
3614 if (is_s390x) { _prev = old_S390X_F2; break; } \
3615 vg_assert(0+2-2); \
3616 case CFIR_S390X_F3: \
3617 if (is_s390x) { _prev = old_S390X_F3; break; } \
3618 vg_assert(0+3-3); \
3619 case CFIR_S390X_F4: \
3620 if (is_s390x) { _prev = old_S390X_F4; break; } \
3621 vg_assert(0+4-4); \
3622 case CFIR_S390X_F5: \
3623 if (is_s390x) { _prev = old_S390X_F5; break; } \
3624 vg_assert(0+5-5); \
3625 case CFIR_S390X_F6: \
3626 if (is_s390x) { _prev = old_S390X_F6; break; } \
3627 vg_assert(0+6-6); \
3628 case CFIR_S390X_F7: \
3629 if (is_s390x) { _prev = old_S390X_F7; break; } \
3630 vg_assert(0+7-7); \
3631 default: \
3632 vg_assert(0*0); \
3634 } while (0)
3636 # if defined(VGA_x86) || defined(VGA_amd64)
3637 COMPUTE(uregsPrev.xip, uregsHere->xip, cfsi_m->ra_how, cfsi_m->ra_off);
3638 COMPUTE(uregsPrev.xsp, uregsHere->xsp, cfsi_m->sp_how, cfsi_m->sp_off);
3639 COMPUTE(uregsPrev.xbp, uregsHere->xbp, cfsi_m->bp_how, cfsi_m->bp_off);
3640 # elif defined(VGA_arm)
3641 COMPUTE(uregsPrev.r15, uregsHere->r15, cfsi_m->ra_how, cfsi_m->ra_off);
3642 COMPUTE(uregsPrev.r14, uregsHere->r14, cfsi_m->r14_how, cfsi_m->r14_off);
3643 COMPUTE(uregsPrev.r13, uregsHere->r13, cfsi_m->r13_how, cfsi_m->r13_off);
3644 COMPUTE(uregsPrev.r12, uregsHere->r12, cfsi_m->r12_how, cfsi_m->r12_off);
3645 COMPUTE(uregsPrev.r11, uregsHere->r11, cfsi_m->r11_how, cfsi_m->r11_off);
3646 COMPUTE(uregsPrev.r7, uregsHere->r7, cfsi_m->r7_how, cfsi_m->r7_off);
3647 # elif defined(VGA_s390x)
3648 COMPUTE(uregsPrev.ia, uregsHere->ia, cfsi_m->ra_how, cfsi_m->ra_off);
3649 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3650 COMPUTE(uregsPrev.fp, uregsHere->fp, cfsi_m->fp_how, cfsi_m->fp_off);
3651 COMPUTE(uregsPrev.f0, uregsHere->f0, cfsi_m->f0_how, cfsi_m->f0_off);
3652 COMPUTE(uregsPrev.f1, uregsHere->f1, cfsi_m->f1_how, cfsi_m->f1_off);
3653 COMPUTE(uregsPrev.f2, uregsHere->f2, cfsi_m->f2_how, cfsi_m->f2_off);
3654 COMPUTE(uregsPrev.f3, uregsHere->f3, cfsi_m->f3_how, cfsi_m->f3_off);
3655 COMPUTE(uregsPrev.f4, uregsHere->f4, cfsi_m->f4_how, cfsi_m->f4_off);
3656 COMPUTE(uregsPrev.f5, uregsHere->f5, cfsi_m->f5_how, cfsi_m->f5_off);
3657 COMPUTE(uregsPrev.f6, uregsHere->f6, cfsi_m->f6_how, cfsi_m->f6_off);
3658 COMPUTE(uregsPrev.f7, uregsHere->f7, cfsi_m->f7_how, cfsi_m->f7_off);
3659 # elif defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)
3660 COMPUTE(uregsPrev.pc, uregsHere->pc, cfsi_m->ra_how, cfsi_m->ra_off);
3661 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3662 COMPUTE(uregsPrev.fp, uregsHere->fp, cfsi_m->fp_how, cfsi_m->fp_off);
3663 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3664 # elif defined(VGP_arm64_linux) || defined(VGP_arm64_freebsd)
3665 COMPUTE(uregsPrev.pc, uregsHere->pc, cfsi_m->ra_how, cfsi_m->ra_off);
3666 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3667 COMPUTE(uregsPrev.x30, uregsHere->x30, cfsi_m->x30_how, cfsi_m->x30_off);
3668 COMPUTE(uregsPrev.x29, uregsHere->x29, cfsi_m->x29_how, cfsi_m->x29_off);
3669 # else
3670 # error "Unknown arch"
3671 # endif
3673 # undef READ_REGISTER
3674 # undef COMPUTE
3676 *uregsHere = uregsPrev;
3677 return True;
3681 /*--------------------------------------------------------------*/
3682 /*--- ---*/
3683 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
3684 /*--- MSVC FPO INFO ---*/
3685 /*--- ---*/
3686 /*--------------------------------------------------------------*/
3688 Bool VG_(use_FPO_info) ( /*MOD*/Addr* ipP,
3689 /*MOD*/Addr* spP,
3690 /*MOD*/Addr* fpP,
3691 DiEpoch ep,
3692 Addr min_accessible,
3693 Addr max_accessible )
3695 Word i;
3696 const DebugInfo* di;
3697 FPO_DATA* fpo = NULL;
3698 Addr spHere;
3700 static UWord n_search = 0;
3701 static UWord n_steps = 0;
3702 n_search++;
3704 if (0) VG_(printf)("search FPO for %#lx\n", *ipP);
3706 for (di = debugInfo_list; di != NULL; di = di->next) {
3707 n_steps++;
3709 if (!is_DI_valid_for_epoch(di, ep))
3710 continue;
3712 /* Use the per-DebugInfo summary address ranges to skip
3713 inapplicable DebugInfos quickly. */
3714 if (di->fpo == NULL)
3715 continue;
3716 if (*ipP < di->fpo_minavma || *ipP > di->fpo_maxavma)
3717 continue;
3719 i = ML_(search_one_fpotab)( di, *ipP );
3720 if (i != -1) {
3721 Word j;
3722 if (0) {
3723 /* debug printing only */
3724 VG_(printf)("look for %#lx size %lu i %ld\n",
3725 *ipP, di->fpo_size, i);
3726 for (j = 0; j < di->fpo_size; j++)
3727 VG_(printf)("[%02ld] %#x %u\n",
3728 j, di->fpo[j].ulOffStart, di->fpo[j].cbProcSize);
3730 vg_assert(i >= 0 && i < di->fpo_size);
3731 fpo = &di->fpo[i];
3732 break;
3736 if (fpo == NULL)
3737 return False;
3739 if (0 && ((n_search & 0x7FFFF) == 0))
3740 VG_(printf)("VG_(use_FPO_info): %lu searches, "
3741 "%lu DebugInfos looked at\n",
3742 n_search, n_steps);
3745 /* Start of performance-enhancing hack: once every 64 (chosen
3746 hackily after profiling) successful searches, move the found
3747 DebugInfo one step closer to the start of the list. This makes
3748 future searches cheaper. For starting konqueror on amd64, this
3749 in fact reduces the total amount of searching done by the above
3750 find-the-right-DebugInfo loop by more than a factor of 20. */
3751 if ((n_search & 0x3F) == 0) {
3752 /* Move si one step closer to the start of the list. */
3753 //move_DebugInfo_one_step_forward( di );
3755 /* End of performance-enhancing hack. */
3757 if (0) {
3758 VG_(printf)("found fpo: ");
3759 //ML_(ppFPO)(fpo);
3763 Stack layout is:
3764 %esp->
3765 4*.cbRegs {%edi, %esi, %ebp, %ebx}
3766 4*.cdwLocals
3767 return_pc
3768 4*.cdwParams
3769 prior_%esp->
3771 Typical code looks like:
3772 sub $4*.cdwLocals,%esp
3773 Alternative to above for >=4KB (and sometimes for smaller):
3774 mov $size,%eax
3775 call __chkstk # WinNT performs page-by-page probe!
3776 __chkstk is much like alloc(), except that on return
3777 %eax= 5+ &CALL. Thus it could be used as part of
3778 Position Independent Code to locate the Global Offset Table.
3779 push %ebx
3780 push %ebp
3781 push %esi
3782 Other once-only instructions often scheduled >here<.
3783 push %edi
3785 If the pc is within the first .cbProlog bytes of the function,
3786 then you must disassemble to see how many registers have been pushed,
3787 because instructions in the prolog may be scheduled for performance.
3788 The order of PUSH is always %ebx, %ebp, %esi, %edi, with trailing
3789 registers not pushed when .cbRegs < 4. This seems somewhat strange
3790 because %ebp is the register whose usage you want to minimize,
3791 yet it is in the first half of the PUSH list.
3793 I don't know what happens when the compiler constructs an outgoing CALL.
3794 %esp could move if outgoing parameters are PUSHed, and this affects
3795 traceback for errors during the PUSHes. */
3797 spHere = *spP;
3799 *ipP = ML_(read_Addr)((void *)(spHere + 4*(fpo->cbRegs + fpo->cdwLocals)));
3800 *spP = spHere + 4*(fpo->cbRegs + fpo->cdwLocals + 1
3801 + fpo->cdwParams);
3802 *fpP = ML_(read_Addr)((void *)(spHere + 4*2));
3803 return True;
3806 Bool VG_(FPO_info_present)(void)
3808 const DebugInfo* di;
3809 for (di = debugInfo_list; di != NULL; di = di->next) {
3810 if (di->fpo != NULL)
3811 return True;
3813 return False;
3817 /*--------------------------------------------------------------*/
3818 /*--- ---*/
3819 /*--- TOP LEVEL: GENERATE DESCRIPTION OF DATA ADDRESSES ---*/
3820 /*--- FROM DWARF3 DEBUG INFO ---*/
3821 /*--- ---*/
3822 /*--------------------------------------------------------------*/
3824 /* Try to make p2XA(dst, fmt, args..) turn into
3825 VG_(xaprintf)(dst, fmt, args) without having to resort to
3826 vararg macros. As usual with everything to do with varargs, it's
3827 an ugly hack.
3829 //#define p2XA(dstxa, format, args...)
3830 // VG_(xaprintf)(dstxa, format, ##args)
3832 #define p2XA VG_(xaprintf)
3834 /* Add a zero-terminating byte to DST, which must be an XArray* of
3835 HChar. */
3836 static void zterm_XA ( XArray* dst )
3838 HChar zero = 0;
3839 (void) VG_(addBytesToXA)( dst, &zero, 1 );
3843 /* Evaluate the location expression/list for var, to see whether or
3844 not data_addr falls within the variable. If so also return the
3845 offset of data_addr from the start of the variable. Note that
3846 regs, which supplies ip,sp,fp values, will be NULL for global
3847 variables, and non-NULL for local variables. */
3848 static Bool data_address_is_in_var ( /*OUT*/PtrdiffT* offset,
3849 const XArray* /* TyEnt */ tyents,
3850 const DiVariable* var,
3851 const RegSummary* regs,
3852 Addr data_addr,
3853 const DebugInfo* di )
3855 MaybeULong mul;
3856 SizeT var_szB;
3857 GXResult res;
3858 Bool show = False;
3860 vg_assert(var->name);
3861 vg_assert(var->gexpr);
3863 /* Figure out how big the variable is. */
3864 mul = ML_(sizeOfType)(tyents, var->typeR);
3865 /* If this var has a type whose size is unknown, zero, or
3866 impossibly large, it should never have been added. ML_(addVar)
3867 should have rejected it. */
3868 vg_assert(mul.b == True);
3869 vg_assert(mul.ul > 0);
3870 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
3871 /* After this point, we assume we can truncate mul.ul to a host word
3872 safely (without loss of info). */
3874 var_szB = (SizeT)mul.ul; /* NB: truncate to host word */
3876 if (show) {
3877 VG_(printf)("VVVV: data_address_%#lx_is_in_var: %s :: ",
3878 data_addr, var->name );
3879 ML_(pp_TyEnt_C_ishly)( tyents, var->typeR );
3880 VG_(printf)("\n");
3883 /* ignore zero-sized vars; they can never match anything. */
3884 if (var_szB == 0) {
3885 if (show)
3886 VG_(printf)("VVVV: -> Fail (variable is zero sized)\n");
3887 return False;
3890 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, regs, di );
3892 if (show) {
3893 VG_(printf)("VVVV: -> ");
3894 ML_(pp_GXResult)( res );
3895 VG_(printf)("\n");
3898 if (res.kind == GXR_Addr
3899 && res.word <= data_addr
3900 && data_addr < res.word + var_szB) {
3901 *offset = data_addr - res.word;
3902 return True;
3903 } else {
3904 return False;
3909 /* Format the acquired information into DN(AME)1 and DN(AME)2, which
3910 are XArray*s of HChar, that have been initialised by the caller.
3911 Resulting strings will be zero terminated. Information is
3912 formatted in an understandable way. Not so easy. If frameNo is
3913 -1, this is assumed to be a global variable; else a local
3914 variable. */
3915 static void format_message ( /*MOD*/XArray* /* of HChar */ dn1,
3916 /*MOD*/XArray* /* of HChar */ dn2,
3917 Addr data_addr,
3918 const DebugInfo* di,
3919 const DiVariable* var,
3920 PtrdiffT var_offset,
3921 PtrdiffT residual_offset,
3922 const XArray* /*HChar*/ described,
3923 Int frameNo,
3924 ThreadId tid )
3926 Bool have_descr, have_srcloc;
3927 Bool xml = VG_(clo_xml);
3928 const HChar* vo_plural = var_offset == 1 ? "" : "s";
3929 const HChar* ro_plural = residual_offset == 1 ? "" : "s";
3930 const HChar* basetag = "auxwhat"; /* a constant */
3931 HChar tagL[32], tagR[32], xagL[32], xagR[32];
3932 const HChar *fileName = ML_(fndn_ix2filename)(di, var->fndn_ix);
3933 // fileName will be "???" if var->fndn_ix == 0.
3934 // fileName will only be used if have_descr is True.
3936 if (frameNo < -1) {
3937 vg_assert(0); /* Not allowed */
3939 else if (frameNo == -1) {
3940 vg_assert(tid == VG_INVALID_THREADID);
3942 else /* (frameNo >= 0) */ {
3943 vg_assert(tid != VG_INVALID_THREADID);
3946 vg_assert(dn1 && dn2);
3947 vg_assert(described);
3948 vg_assert(var && var->name);
3949 have_descr = VG_(sizeXA)(described) > 0
3950 && *(HChar*)VG_(indexXA)(described,0) != '\0';
3951 have_srcloc = var->fndn_ix > 0 && var->lineNo > 0;
3953 tagL[0] = tagR[0] = xagL[0] = xagR[0] = 0;
3954 if (xml) {
3955 VG_(sprintf)(tagL, "<%s>", basetag); // <auxwhat>
3956 VG_(sprintf)(tagR, "</%s>", basetag); // </auxwhat>
3957 VG_(sprintf)(xagL, "<x%s>", basetag); // <xauxwhat>
3958 VG_(sprintf)(xagR, "</x%s>", basetag); // </xauxwhat>
3961 # define TAGL(_xa) p2XA(_xa, "%s", tagL)
3962 # define TAGR(_xa) p2XA(_xa, "%s", tagR)
3963 # define XAGL(_xa) p2XA(_xa, "%s", xagL)
3964 # define XAGR(_xa) p2XA(_xa, "%s", xagR)
3965 # define TXTL(_xa) p2XA(_xa, "%s", "<text>")
3966 # define TXTR(_xa) p2XA(_xa, "%s", "</text>")
3968 /* ------ local cases ------ */
3970 if ( frameNo >= 0 && (!have_srcloc) && (!have_descr) ) {
3971 /* no srcloc, no description:
3972 Location 0x7fefff6cf is 543 bytes inside local var "a",
3973 in frame #1 of thread 1
3975 if (xml) {
3976 TAGL( dn1 );
3977 p2XA( dn1,
3978 "Location 0x%lx is %ld byte%s inside local var \"%pS\",",
3979 data_addr, var_offset, vo_plural, var->name );
3980 TAGR( dn1 );
3981 TAGL( dn2 );
3982 p2XA( dn2,
3983 "in frame #%d of thread %u", frameNo, tid );
3984 TAGR( dn2 );
3985 } else {
3986 p2XA( dn1,
3987 "Location 0x%lx is %ld byte%s inside local var \"%s\",",
3988 data_addr, var_offset, vo_plural, var->name );
3989 p2XA( dn2,
3990 "in frame #%d of thread %u", frameNo, tid );
3993 else
3994 if ( frameNo >= 0 && have_srcloc && (!have_descr) ) {
3995 /* no description:
3996 Location 0x7fefff6cf is 543 bytes inside local var "a"
3997 declared at dsyms7.c:17, in frame #1 of thread 1
3999 if (xml) {
4000 TAGL( dn1 );
4001 p2XA( dn1,
4002 "Location 0x%lx is %ld byte%s inside local var \"%pS\"",
4003 data_addr, var_offset, vo_plural, var->name );
4004 TAGR( dn1 );
4005 XAGL( dn2 );
4006 TXTL( dn2 );
4007 p2XA( dn2,
4008 "declared at %pS:%d, in frame #%d of thread %u",
4009 fileName, var->lineNo, frameNo, tid );
4010 TXTR( dn2 );
4011 // FIXME: also do <dir>
4012 p2XA( dn2,
4013 " <file>%pS</file> <line>%d</line> ",
4014 fileName, var->lineNo );
4015 XAGR( dn2 );
4016 } else {
4017 p2XA( dn1,
4018 "Location 0x%lx is %ld byte%s inside local var \"%s\"",
4019 data_addr, var_offset, vo_plural, var->name );
4020 p2XA( dn2,
4021 "declared at %s:%d, in frame #%d of thread %u",
4022 fileName, var->lineNo, frameNo, tid );
4025 else
4026 if ( frameNo >= 0 && (!have_srcloc) && have_descr ) {
4027 /* no srcloc:
4028 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2
4029 in frame #1 of thread 1
4031 if (xml) {
4032 TAGL( dn1 );
4033 p2XA( dn1,
4034 "Location 0x%lx is %ld byte%s inside %pS%pS",
4035 data_addr, residual_offset, ro_plural, var->name,
4036 (HChar*)(VG_(indexXA)(described,0)) );
4037 TAGR( dn1 );
4038 TAGL( dn2 );
4039 p2XA( dn2,
4040 "in frame #%d of thread %u", frameNo, tid );
4041 TAGR( dn2 );
4042 } else {
4043 p2XA( dn1,
4044 "Location 0x%lx is %ld byte%s inside %s%s",
4045 data_addr, residual_offset, ro_plural, var->name,
4046 (HChar*)(VG_(indexXA)(described,0)) );
4047 p2XA( dn2,
4048 "in frame #%d of thread %u", frameNo, tid );
4051 else
4052 if ( frameNo >= 0 && have_srcloc && have_descr ) {
4053 /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
4054 declared at dsyms7.c:17, in frame #1 of thread 1 */
4055 if (xml) {
4056 TAGL( dn1 );
4057 p2XA( dn1,
4058 "Location 0x%lx is %ld byte%s inside %pS%pS,",
4059 data_addr, residual_offset, ro_plural, var->name,
4060 (HChar*)(VG_(indexXA)(described,0)) );
4061 TAGR( dn1 );
4062 XAGL( dn2 );
4063 TXTL( dn2 );
4064 p2XA( dn2,
4065 "declared at %pS:%d, in frame #%d of thread %u",
4066 fileName, var->lineNo, frameNo, tid );
4067 TXTR( dn2 );
4068 // FIXME: also do <dir>
4069 p2XA( dn2,
4070 " <file>%pS</file> <line>%d</line> ",
4071 fileName, var->lineNo );
4072 XAGR( dn2 );
4073 } else {
4074 p2XA( dn1,
4075 "Location 0x%lx is %ld byte%s inside %s%s,",
4076 data_addr, residual_offset, ro_plural, var->name,
4077 (HChar*)(VG_(indexXA)(described,0)) );
4078 p2XA( dn2,
4079 "declared at %s:%d, in frame #%d of thread %u",
4080 fileName, var->lineNo, frameNo, tid );
4083 else
4084 /* ------ global cases ------ */
4085 if ( frameNo >= -1 && (!have_srcloc) && (!have_descr) ) {
4086 /* no srcloc, no description:
4087 Location 0x7fefff6cf is 543 bytes inside global var "a"
4089 if (xml) {
4090 TAGL( dn1 );
4091 p2XA( dn1,
4092 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
4093 data_addr, var_offset, vo_plural, var->name );
4094 TAGR( dn1 );
4095 } else {
4096 p2XA( dn1,
4097 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
4098 data_addr, var_offset, vo_plural, var->name );
4101 else
4102 if ( frameNo >= -1 && have_srcloc && (!have_descr) ) {
4103 /* no description:
4104 Location 0x7fefff6cf is 543 bytes inside global var "a"
4105 declared at dsyms7.c:17
4107 if (xml) {
4108 TAGL( dn1 );
4109 p2XA( dn1,
4110 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
4111 data_addr, var_offset, vo_plural, var->name );
4112 TAGR( dn1 );
4113 XAGL( dn2 );
4114 TXTL( dn2 );
4115 p2XA( dn2,
4116 "declared at %pS:%d",
4117 fileName, var->lineNo);
4118 TXTR( dn2 );
4119 // FIXME: also do <dir>
4120 p2XA( dn2,
4121 " <file>%pS</file> <line>%d</line> ",
4122 fileName, var->lineNo );
4123 XAGR( dn2 );
4124 } else {
4125 p2XA( dn1,
4126 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
4127 data_addr, var_offset, vo_plural, var->name );
4128 p2XA( dn2,
4129 "declared at %s:%d",
4130 fileName, var->lineNo);
4133 else
4134 if ( frameNo >= -1 && (!have_srcloc) && have_descr ) {
4135 /* no srcloc:
4136 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
4137 a global variable
4139 if (xml) {
4140 TAGL( dn1 );
4141 p2XA( dn1,
4142 "Location 0x%lx is %ld byte%s inside %pS%pS,",
4143 data_addr, residual_offset, ro_plural, var->name,
4144 (HChar*)(VG_(indexXA)(described,0)) );
4145 TAGR( dn1 );
4146 TAGL( dn2 );
4147 p2XA( dn2,
4148 "a global variable");
4149 TAGR( dn2 );
4150 } else {
4151 p2XA( dn1,
4152 "Location 0x%lx is %ld byte%s inside %s%s,",
4153 data_addr, residual_offset, ro_plural, var->name,
4154 (HChar*)(VG_(indexXA)(described,0)) );
4155 p2XA( dn2,
4156 "a global variable");
4159 else
4160 if ( frameNo >= -1 && have_srcloc && have_descr ) {
4161 /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
4162 a global variable declared at dsyms7.c:17 */
4163 if (xml) {
4164 TAGL( dn1 );
4165 p2XA( dn1,
4166 "Location 0x%lx is %ld byte%s inside %pS%pS,",
4167 data_addr, residual_offset, ro_plural, var->name,
4168 (HChar*)(VG_(indexXA)(described,0)) );
4169 TAGR( dn1 );
4170 XAGL( dn2 );
4171 TXTL( dn2 );
4172 p2XA( dn2,
4173 "a global variable declared at %pS:%d",
4174 fileName, var->lineNo);
4175 TXTR( dn2 );
4176 // FIXME: also do <dir>
4177 p2XA( dn2,
4178 " <file>%pS</file> <line>%d</line> ",
4179 fileName, var->lineNo );
4180 XAGR( dn2 );
4181 } else {
4182 p2XA( dn1,
4183 "Location 0x%lx is %ld byte%s inside %s%s,",
4184 data_addr, residual_offset, ro_plural, var->name,
4185 (HChar*)(VG_(indexXA)(described,0)) );
4186 p2XA( dn2,
4187 "a global variable declared at %s:%d",
4188 fileName, var->lineNo);
4191 else
4192 vg_assert(0);
4194 /* Zero terminate both strings */
4195 zterm_XA( dn1 );
4196 zterm_XA( dn2 );
4198 # undef TAGL
4199 # undef TAGR
4200 # undef XAGL
4201 # undef XAGR
4202 # undef TXTL
4203 # undef TXTR
4207 /* Determine if data_addr is a local variable in the frame
4208 characterised by (ip,sp,fp), and if so write its description at the
4209 ends of DNAME{1,2}, which are XArray*s of HChar, that have been
4210 initialised by the caller, zero terminate both, and return True.
4211 If it's not a local variable in said frame, return False. */
4212 static
4213 Bool consider_vars_in_frame ( /*MOD*/XArray* /* of HChar */ dname1,
4214 /*MOD*/XArray* /* of HChar */ dname2,
4215 DiEpoch ep,
4216 Addr data_addr,
4217 Addr ip, Addr sp, Addr fp,
4218 /* shown to user: */
4219 ThreadId tid, Int frameNo )
4221 Word i;
4222 DebugInfo* di;
4223 RegSummary regs;
4224 Bool debug = False;
4226 static UInt n_search = 0;
4227 static UInt n_steps = 0;
4228 n_search++;
4229 if (debug)
4230 VG_(printf)("QQQQ: cvif: ip,sp,fp %#lx,%#lx,%#lx\n", ip,sp,fp);
4231 /* first, find the DebugInfo that pertains to 'ip'. */
4232 for (di = debugInfo_list; di; di = di->next) {
4233 n_steps++;
4234 if (!is_DI_valid_for_epoch(di, ep))
4235 continue;
4236 /* text segment missing? unlikely, but handle it .. */
4237 if (!di->text_present || di->text_size == 0)
4238 continue;
4239 /* Ok. So does this text mapping bracket the ip? */
4240 if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
4241 break;
4244 /* Didn't find it. Strange -- means ip is a code address outside
4245 of any mapped text segment. Unlikely but not impossible -- app
4246 could be generating code to run. */
4247 if (!di)
4248 return False;
4250 if (0 && ((n_search & 0x1) == 0))
4251 VG_(printf)("consider_vars_in_frame: %u searches, "
4252 "%u DebugInfos looked at\n",
4253 n_search, n_steps);
4254 /* Start of performance-enhancing hack: once every ??? (chosen
4255 hackily after profiling) successful searches, move the found
4256 DebugInfo one step closer to the start of the list. This makes
4257 future searches cheaper. */
4258 if ((n_search & 0xFFFF) == 0) {
4259 /* Move si one step closer to the start of the list. */
4260 move_DebugInfo_one_step_forward( di );
4262 /* End of performance-enhancing hack. */
4264 /* any var info at all? */
4265 if (!di->varinfo)
4266 return False;
4268 /* Work through the scopes from most deeply nested outwards,
4269 looking for code address ranges that bracket 'ip'. The
4270 variables on each such address range found are in scope right
4271 now. Don't descend to level zero as that is the global
4272 scope. */
4273 regs.ip = ip;
4274 regs.sp = sp;
4275 regs.fp = fp;
4277 /* "for each scope, working outwards ..." */
4278 for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
4279 XArray* vars;
4280 Word j;
4281 DiAddrRange* arange;
4282 OSet* this_scope
4283 = *(OSet**)VG_(indexXA)( di->varinfo, i );
4284 if (debug)
4285 VG_(printf)("QQQQ: considering scope %ld\n", (Word)i);
4286 if (!this_scope)
4287 continue;
4288 /* Find the set of variables in this scope that
4289 bracket the program counter. */
4290 arange = VG_(OSetGen_LookupWithCmp)(
4291 this_scope, &ip,
4292 ML_(cmp_for_DiAddrRange_range)
4294 if (!arange)
4295 continue;
4296 /* stay sane */
4297 vg_assert(arange->aMin <= arange->aMax);
4298 /* It must bracket the ip we asked for, else
4299 ML_(cmp_for_DiAddrRange_range) is somehow broken. */
4300 vg_assert(arange->aMin <= ip && ip <= arange->aMax);
4301 /* It must have an attached XArray of DiVariables. */
4302 vars = arange->vars;
4303 vg_assert(vars);
4304 /* But it mustn't cover the entire address range. We only
4305 expect that to happen for the global scope (level 0), which
4306 we're not looking at here. Except, it may cover the entire
4307 address range, but in that case the vars array must be
4308 empty. */
4309 vg_assert(! (arange->aMin == (Addr)0
4310 && arange->aMax == ~(Addr)0
4311 && VG_(sizeXA)(vars) > 0) );
4312 for (j = 0; j < VG_(sizeXA)( vars ); j++) {
4313 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
4314 PtrdiffT offset;
4315 if (debug)
4316 VG_(printf)("QQQQ: var:name=%s %#lx-%#lx %#lx\n",
4317 var->name,arange->aMin,arange->aMax,ip);
4318 if (data_address_is_in_var( &offset, di->admin_tyents,
4319 var, &regs,
4320 data_addr, di )) {
4321 PtrdiffT residual_offset = 0;
4322 XArray* described = ML_(describe_type)( &residual_offset,
4323 di->admin_tyents,
4324 var->typeR, offset );
4325 format_message( dname1, dname2,
4326 data_addr, di, var, offset, residual_offset,
4327 described, frameNo, tid );
4328 VG_(deleteXA)( described );
4329 return True;
4334 return False;
4337 /* Try to form some description of DATA_ADDR by looking at the DWARF3
4338 debug info we have. This considers all global variables, and 8
4339 frames in the stacks of all threads. Result is written at the ends
4340 of DNAME{1,2}V, which are XArray*s of HChar, that have been
4341 initialised by the caller, and True is returned. If no description
4342 is created, False is returned. Regardless of the return value,
4343 DNAME{1,2}V are guaranteed to be zero terminated after the call.
4345 Note that after the call, DNAME{1,2} may have more than one
4346 trailing zero, so callers should establish the useful text length
4347 using VG_(strlen) on the contents, rather than VG_(sizeXA) on the
4348 XArray itself.
4350 Bool VG_(get_data_description)(
4351 /*MOD*/ XArray* /* of HChar */ dname1,
4352 /*MOD*/ XArray* /* of HChar */ dname2,
4353 DiEpoch ep, Addr data_addr
4356 # define N_FRAMES 8
4357 Addr ips[N_FRAMES], sps[N_FRAMES], fps[N_FRAMES];
4358 UInt n_frames;
4360 Addr stack_min, stack_max;
4361 ThreadId tid;
4362 Bool found;
4363 DebugInfo* di;
4364 Word j;
4366 if (0) VG_(printf)("get_data_description: dataaddr %#lx\n", data_addr);
4367 /* First, see if data_addr is (or is part of) a global variable.
4368 Loop over the DebugInfos we have. Check data_addr against the
4369 outermost scope of all of them, as that should be a global
4370 scope. */
4371 for (di = debugInfo_list; di != NULL; di = di->next) {
4372 OSet* global_scope;
4373 Word gs_size;
4374 Addr zero;
4375 DiAddrRange* global_arange;
4376 Word i;
4377 XArray* vars;
4379 /* text segment missing? unlikely, but handle it .. */
4380 if (!di->text_present || di->text_size == 0)
4381 continue;
4382 /* any var info at all? */
4383 if (!di->varinfo)
4384 continue;
4385 /* perhaps this object didn't contribute any vars at all? */
4386 if (VG_(sizeXA)( di->varinfo ) == 0)
4387 continue;
4388 global_scope = *(OSet**)VG_(indexXA)( di->varinfo, 0 );
4389 vg_assert(global_scope);
4390 gs_size = VG_(OSetGen_Size)( global_scope );
4391 /* The global scope might be completely empty if this
4392 compilation unit declared locals but nothing global. */
4393 if (gs_size == 0)
4394 continue;
4395 /* But if it isn't empty, then it must contain exactly one
4396 element, which covers the entire address range. */
4397 vg_assert(gs_size == 1);
4398 /* Fish out the global scope and check it is as expected. */
4399 zero = 0;
4400 global_arange
4401 = VG_(OSetGen_Lookup)( global_scope, &zero );
4402 /* The global range from (Addr)0 to ~(Addr)0 must exist */
4403 vg_assert(global_arange);
4404 vg_assert(global_arange->aMin == (Addr)0
4405 && global_arange->aMax == ~(Addr)0);
4406 /* Any vars in this range? */
4407 if (!global_arange->vars)
4408 continue;
4409 /* Ok, there are some vars in the global scope of this
4410 DebugInfo. Wade through them and see if the data addresses
4411 of any of them bracket data_addr. */
4412 vars = global_arange->vars;
4413 for (i = 0; i < VG_(sizeXA)( vars ); i++) {
4414 PtrdiffT offset;
4415 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, i );
4416 vg_assert(var->name);
4417 /* Note we use a NULL RegSummary* here. It can't make any
4418 sense for a global variable to have a location expression
4419 which depends on a SP/FP/IP value. So don't supply any.
4420 This means, if the evaluation of the location
4421 expression/list requires a register, we have to let it
4422 fail. */
4423 if (data_address_is_in_var( &offset, di->admin_tyents, var,
4424 NULL/* RegSummary* */,
4425 data_addr, di )) {
4426 PtrdiffT residual_offset = 0;
4427 XArray* described = ML_(describe_type)( &residual_offset,
4428 di->admin_tyents,
4429 var->typeR, offset );
4430 format_message( dname1, dname2,
4431 data_addr, di, var, offset, residual_offset,
4432 described, -1/*frameNo*/,
4433 VG_INVALID_THREADID );
4434 VG_(deleteXA)( described );
4435 zterm_XA( dname1 );
4436 zterm_XA( dname2 );
4437 return True;
4442 /* Ok, well it's not a global variable. So now let's snoop around
4443 in the stacks of all the threads. First try to figure out which
4444 thread's stack data_addr is in. */
4446 /* Perhaps it's on a thread's stack? */
4447 found = False;
4448 VG_(thread_stack_reset_iter)(&tid);
4449 while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) {
4450 if (stack_min >= stack_max)
4451 continue; /* ignore obviously stupid cases */
4452 if (stack_min - VG_STACK_REDZONE_SZB <= data_addr
4453 && data_addr <= stack_max) {
4454 found = True;
4455 break;
4458 if (!found) {
4459 zterm_XA( dname1 );
4460 zterm_XA( dname2 );
4461 return False;
4464 /* We conclude data_addr is in thread tid's stack. Unwind the
4465 stack to get a bunch of (ip,sp,fp) triples describing the
4466 frames, and for each frame, consider the local variables. */
4467 n_frames = VG_(get_StackTrace)( tid, ips, N_FRAMES,
4468 sps, fps, 0/*first_ip_delta*/ );
4470 vg_assert(n_frames <= N_FRAMES);
4471 for (j = 0; j < n_frames; j++) {
4472 if (consider_vars_in_frame( dname1, dname2,
4473 ep, data_addr,
4474 ips[j],
4475 sps[j], fps[j], tid, j )) {
4476 zterm_XA( dname1 );
4477 zterm_XA( dname2 );
4478 return True;
4480 /* Now, it appears that gcc sometimes appears to produce
4481 location lists whose ranges don't actually cover the call
4482 instruction, even though the address of the variable in
4483 question is passed as a parameter in the call. AFAICS this
4484 is simply a bug in gcc - how can the variable be claimed not
4485 exist in memory (on the stack) for the duration of a call in
4486 which its address is passed? But anyway, in the particular
4487 case I investigated (memcheck/tests/varinfo6.c, call to croak
4488 on line 2999, local var budget declared at line 3115
4489 appearing not to exist across the call to mainSort on line
4490 3143, "gcc.orig (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)" on
4491 amd64), the variable's location list does claim it exists
4492 starting at the first byte of the first instruction after the
4493 call instruction. So, call consider_vars_in_frame a second
4494 time, but this time add 1 to the IP. GDB handles this
4495 example with no difficulty, which leads me to believe that
4496 either (1) I misunderstood something, or (2) GDB has an
4497 equivalent kludge. */
4498 if (j > 0 /* this is a non-innermost frame */
4499 && consider_vars_in_frame( dname1, dname2,
4500 ep, data_addr,
4501 ips[j] + 1,
4502 sps[j], fps[j], tid, j )) {
4503 zterm_XA( dname1 );
4504 zterm_XA( dname2 );
4505 return True;
4509 /* We didn't find anything useful. */
4510 zterm_XA( dname1 );
4511 zterm_XA( dname2 );
4512 return False;
4513 # undef N_FRAMES
4517 //////////////////////////////////////////////////////////////////
4518 // //
4519 // Support for other kinds of queries to the Dwarf3 var info //
4520 // //
4521 //////////////////////////////////////////////////////////////////
4523 /* Figure out if the variable 'var' has a location that is linearly
4524 dependent on a stack pointer value, or a frame pointer value, and
4525 if it is, add a description of it to 'blocks'. Otherwise ignore
4526 it. If 'arrays_only' is True, also ignore it unless it has an
4527 array type. */
4529 static
4530 void analyse_deps ( /*MOD*/XArray* /* of FrameBlock */ blocks,
4531 const XArray* /* TyEnt */ tyents,
4532 Addr ip, const DebugInfo* di, const DiVariable* var,
4533 Bool arrays_only )
4535 GXResult res_sp_6k, res_sp_7k, res_fp_6k, res_fp_7k;
4536 RegSummary regs;
4537 MaybeULong mul;
4538 Bool isVec;
4539 TyEnt* ty;
4541 Bool debug = False;
4542 if (0&&debug)
4543 VG_(printf)("adeps: var %s\n", var->name );
4545 /* Figure out how big the variable is. */
4546 mul = ML_(sizeOfType)(tyents, var->typeR);
4547 /* If this var has a type whose size is unknown, zero, or
4548 impossibly large, it should never have been added. ML_(addVar)
4549 should have rejected it. */
4550 vg_assert(mul.b == True);
4551 vg_assert(mul.ul > 0);
4552 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
4553 /* After this point, we assume we can truncate mul.ul to a host word
4554 safely (without loss of info). */
4556 /* skip if non-array and we're only interested in arrays */
4557 ty = ML_(TyEnts__index_by_cuOff)( tyents, NULL, var->typeR );
4558 vg_assert(ty);
4559 vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
4560 if (ty->tag == Te_UNKNOWN)
4561 return; /* perhaps we should complain in this case? */
4562 isVec = ty->tag == Te_TyArray;
4563 if (arrays_only && !isVec)
4564 return;
4566 if (0) {ML_(pp_TyEnt_C_ishly)(tyents, var->typeR);
4567 VG_(printf)(" %s\n", var->name);}
4569 /* Do some test evaluations of the variable's location expression,
4570 in order to guess whether it is sp-relative, fp-relative, or
4571 none. A crude hack, which can be interpreted roughly as finding
4572 the first derivative of the location expression w.r.t. the
4573 supplied frame and stack pointer values. */
4574 regs.fp = 0;
4575 regs.ip = ip;
4576 regs.sp = 6 * 1024;
4577 res_sp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4579 regs.fp = 0;
4580 regs.ip = ip;
4581 regs.sp = 7 * 1024;
4582 res_sp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4584 regs.fp = 6 * 1024;
4585 regs.ip = ip;
4586 regs.sp = 0;
4587 res_fp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4589 regs.fp = 7 * 1024;
4590 regs.ip = ip;
4591 regs.sp = 0;
4592 res_fp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4594 vg_assert(res_sp_6k.kind == res_sp_7k.kind);
4595 vg_assert(res_sp_6k.kind == res_fp_6k.kind);
4596 vg_assert(res_sp_6k.kind == res_fp_7k.kind);
4598 if (res_sp_6k.kind == GXR_Addr) {
4599 StackBlock block;
4600 GXResult res;
4601 UWord sp_delta = res_sp_7k.word - res_sp_6k.word;
4602 UWord fp_delta = res_fp_7k.word - res_fp_6k.word;
4603 vg_assert(sp_delta == 0 || sp_delta == 1024);
4604 vg_assert(fp_delta == 0 || fp_delta == 1024);
4606 if (sp_delta == 0 && fp_delta == 0) {
4607 /* depends neither on sp nor fp, so it can't be a stack
4608 local. Ignore it. */
4610 else
4611 if (sp_delta == 1024 && fp_delta == 0) {
4612 regs.sp = regs.fp = 0;
4613 regs.ip = ip;
4614 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4615 vg_assert(res.kind == GXR_Addr);
4616 if (debug)
4617 VG_(printf)(" %5lu .. %5llu (sp) %s\n",
4618 res.word, res.word + mul.ul - 1, var->name);
4619 block.base = res.word;
4620 block.szB = (SizeT)mul.ul;
4621 block.spRel = True;
4622 block.isVec = isVec;
4623 VG_(memset)( &block.name[0], 0, sizeof(block.name) );
4624 if (var->name)
4625 VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
4626 block.name[ sizeof(block.name)-1 ] = 0;
4627 VG_(addToXA)( blocks, &block );
4629 else
4630 if (sp_delta == 0 && fp_delta == 1024) {
4631 regs.sp = regs.fp = 0;
4632 regs.ip = ip;
4633 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4634 vg_assert(res.kind == GXR_Addr);
4635 if (debug)
4636 VG_(printf)(" %5lu .. %5llu (FP) %s\n",
4637 res.word, res.word + mul.ul - 1, var->name);
4638 block.base = res.word;
4639 block.szB = (SizeT)mul.ul;
4640 block.spRel = False;
4641 block.isVec = isVec;
4642 VG_(memset)( &block.name[0], 0, sizeof(block.name) );
4643 if (var->name)
4644 VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
4645 block.name[ sizeof(block.name)-1 ] = 0;
4646 VG_(addToXA)( blocks, &block );
4648 else {
4649 vg_assert(0);
4655 /* Get an XArray of StackBlock which describe the stack (auto) blocks
4656 for this ip. The caller is expected to free the XArray at some
4657 point. If 'arrays_only' is True, only array-typed blocks are
4658 returned; otherwise blocks of all types are returned. */
4660 XArray* /* of StackBlock */
4661 VG_(di_get_stack_blocks_at_ip)( Addr ip, Bool arrays_only )
4663 /* This is a derivation of consider_vars_in_frame() above. */
4664 Word i;
4665 DebugInfo* di;
4666 Bool debug = False;
4668 XArray* res = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dgsbai.1",
4669 ML_(dinfo_free),
4670 sizeof(StackBlock) );
4672 static UInt n_search = 0;
4673 static UInt n_steps = 0;
4674 n_search++;
4675 if (debug)
4676 VG_(printf)("QQQQ: dgsbai: ip %#lx\n", ip);
4677 /* first, find the DebugInfo that pertains to 'ip'. */
4678 for (di = debugInfo_list; di; di = di->next) {
4679 n_steps++;
4680 /* text segment missing? unlikely, but handle it .. */
4681 if (!di->text_present || di->text_size == 0)
4682 continue;
4683 /* Ok. So does this text mapping bracket the ip? */
4684 if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
4685 break;
4688 /* Didn't find it. Strange -- means ip is a code address outside
4689 of any mapped text segment. Unlikely but not impossible -- app
4690 could be generating code to run. */
4691 if (!di)
4692 return res; /* currently empty */
4694 if (0 && ((n_search & 0x1) == 0))
4695 VG_(printf)("VG_(di_get_stack_blocks_at_ip): %u searches, "
4696 "%u DebugInfos looked at\n",
4697 n_search, n_steps);
4698 /* Start of performance-enhancing hack: once every ??? (chosen
4699 hackily after profiling) successful searches, move the found
4700 DebugInfo one step closer to the start of the list. This makes
4701 future searches cheaper. */
4702 if ((n_search & 0xFFFF) == 0) {
4703 /* Move si one step closer to the start of the list. */
4704 move_DebugInfo_one_step_forward( di );
4706 /* End of performance-enhancing hack. */
4708 /* any var info at all? */
4709 if (!di->varinfo)
4710 return res; /* currently empty */
4712 /* Work through the scopes from most deeply nested outwards,
4713 looking for code address ranges that bracket 'ip'. The
4714 variables on each such address range found are in scope right
4715 now. Don't descend to level zero as that is the global
4716 scope. */
4718 /* "for each scope, working outwards ..." */
4719 for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
4720 XArray* vars;
4721 Word j;
4722 DiAddrRange* arange;
4723 OSet* this_scope
4724 = *(OSet**)VG_(indexXA)( di->varinfo, i );
4725 if (debug)
4726 VG_(printf)("QQQQ: considering scope %ld\n", (Word)i);
4727 if (!this_scope)
4728 continue;
4729 /* Find the set of variables in this scope that
4730 bracket the program counter. */
4731 arange = VG_(OSetGen_LookupWithCmp)(
4732 this_scope, &ip,
4733 ML_(cmp_for_DiAddrRange_range)
4735 if (!arange)
4736 continue;
4737 /* stay sane */
4738 vg_assert(arange->aMin <= arange->aMax);
4739 /* It must bracket the ip we asked for, else
4740 ML_(cmp_for_DiAddrRange_range) is somehow broken. */
4741 vg_assert(arange->aMin <= ip && ip <= arange->aMax);
4742 /* It must have an attached XArray of DiVariables. */
4743 vars = arange->vars;
4744 vg_assert(vars);
4745 /* But it mustn't cover the entire address range. We only
4746 expect that to happen for the global scope (level 0), which
4747 we're not looking at here. Except, it may cover the entire
4748 address range, but in that case the vars array must be
4749 empty. */
4750 vg_assert(! (arange->aMin == (Addr)0
4751 && arange->aMax == ~(Addr)0
4752 && VG_(sizeXA)(vars) > 0) );
4753 for (j = 0; j < VG_(sizeXA)( vars ); j++) {
4754 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
4755 if (debug)
4756 VG_(printf)("QQQQ: var:name=%s %#lx-%#lx %#lx\n",
4757 var->name,arange->aMin,arange->aMax,ip);
4758 analyse_deps( res, di->admin_tyents, ip,
4759 di, var, arrays_only );
4763 return res;
4767 /* Get an array of GlobalBlock which describe the global blocks owned
4768 by the shared object characterised by the given di_handle. Asserts
4769 if the handle is invalid. The caller is responsible for freeing
4770 the array at some point. If 'arrays_only' is True, only
4771 array-typed blocks are returned; otherwise blocks of all types are
4772 returned. */
4774 XArray* /* of GlobalBlock */
4775 VG_(di_get_global_blocks_from_dihandle) ( ULong di_handle, Bool arrays_only )
4777 /* This is a derivation of consider_vars_in_frame() above. */
4779 DebugInfo* di;
4780 XArray* gvars; /* XArray* of GlobalBlock */
4781 Word nScopes, scopeIx;
4783 /* The first thing to do is find the DebugInfo that
4784 pertains to 'di_handle'. */
4785 vg_assert(di_handle > 0);
4786 for (di = debugInfo_list; di; di = di->next) {
4787 if (di->handle == di_handle)
4788 break;
4791 /* If this fails, we were unable to find any DebugInfo with the
4792 given handle. This is considered an error on the part of the
4793 caller. */
4794 vg_assert(di != NULL);
4796 /* we'll put the collected variables in here. */
4797 gvars = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dggbfd.1",
4798 ML_(dinfo_free), sizeof(GlobalBlock) );
4800 /* any var info at all? */
4801 if (!di->varinfo)
4802 return gvars;
4804 /* we'll iterate over all the variables we can find, even if
4805 it seems senseless to visit stack-allocated variables */
4806 /* Iterate over all scopes */
4807 nScopes = VG_(sizeXA)( di->varinfo );
4808 for (scopeIx = 0; scopeIx < nScopes; scopeIx++) {
4810 /* Iterate over each (code) address range at the current scope */
4811 DiAddrRange* range;
4812 OSet* /* of DiAddrInfo */ scope
4813 = *(OSet**)VG_(indexXA)( di->varinfo, scopeIx );
4814 vg_assert(scope);
4815 VG_(OSetGen_ResetIter)(scope);
4816 while ( (range = VG_(OSetGen_Next)(scope)) ) {
4818 /* Iterate over each variable in the current address range */
4819 Word nVars, varIx;
4820 vg_assert(range->vars);
4821 nVars = VG_(sizeXA)( range->vars );
4822 for (varIx = 0; varIx < nVars; varIx++) {
4824 Bool isVec;
4825 GXResult res;
4826 MaybeULong mul;
4827 GlobalBlock gb;
4828 TyEnt* ty;
4829 DiVariable* var = VG_(indexXA)( range->vars, varIx );
4830 vg_assert(var->name);
4831 if (0) VG_(printf)("at depth %ld var %s ", scopeIx, var->name );
4833 /* Now figure out if this variable has a constant address
4834 (that is, independent of FP, SP, phase of moon, etc),
4835 and if so, what the address is. Any variable with a
4836 constant address is deemed to be a global so we collect
4837 it. */
4838 if (0) { VG_(printf)("EVAL: "); ML_(pp_GX)(var->gexpr);
4839 VG_(printf)("\n"); }
4840 res = ML_(evaluate_trivial_GX)( var->gexpr, di );
4842 /* Not a constant address => not interesting */
4843 if (res.kind != GXR_Addr) {
4844 if (0) VG_(printf)("FAIL\n");
4845 continue;
4848 /* Ok, it's a constant address. See if we want to collect
4849 it. */
4850 if (0) VG_(printf)("%#lx\n", res.word);
4852 /* Figure out how big the variable is. */
4853 mul = ML_(sizeOfType)(di->admin_tyents, var->typeR);
4855 /* If this var has a type whose size is unknown, zero, or
4856 impossibly large, it should never have been added.
4857 ML_(addVar) should have rejected it. */
4858 vg_assert(mul.b == True);
4859 vg_assert(mul.ul > 0);
4860 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
4861 /* After this point, we assume we can truncate mul.ul to a
4862 host word safely (without loss of info). */
4864 /* skip if non-array and we're only interested in
4865 arrays */
4866 ty = ML_(TyEnts__index_by_cuOff)( di->admin_tyents, NULL,
4867 var->typeR );
4868 vg_assert(ty);
4869 vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
4870 if (ty->tag == Te_UNKNOWN)
4871 continue; /* perhaps we should complain in this case? */
4873 isVec = ty->tag == Te_TyArray;
4874 if (arrays_only && !isVec) continue;
4876 /* Ok, so collect it! */
4877 vg_assert(var->name);
4878 vg_assert(di->soname);
4879 if (0) VG_(printf)("XXXX %s %s %d\n", var->name,
4880 ML_(fndn_ix2filename)(di, var->fndn_ix),
4881 var->lineNo);
4882 VG_(memset)(&gb, 0, sizeof(gb));
4883 gb.addr = res.word;
4884 gb.szB = (SizeT)mul.ul;
4885 gb.isVec = isVec;
4886 VG_(strncpy)(&gb.name[0], var->name, sizeof(gb.name)-1);
4887 VG_(strncpy)(&gb.soname[0], di->soname, sizeof(gb.soname)-1);
4888 vg_assert(gb.name[ sizeof(gb.name)-1 ] == 0);
4889 vg_assert(gb.soname[ sizeof(gb.soname)-1 ] == 0);
4891 VG_(addToXA)( gvars, &gb );
4893 } /* for (varIx = 0; varIx < nVars; varIx++) */
4895 } /* while ( (range = VG_(OSetGen_Next)(scope)) ) */
4897 } /* for (scopeIx = 0; scopeIx < nScopes; scopeIx++) */
4899 return gvars;
4903 /*------------------------------------------------------------*/
4904 /*--- DebugInfo accessor functions ---*/
4905 /*------------------------------------------------------------*/
4907 const DebugInfo* VG_(next_DebugInfo)(const DebugInfo* di)
4909 if (di == NULL)
4910 return debugInfo_list;
4911 return di->next;
4914 Addr VG_(DebugInfo_get_text_avma)(const DebugInfo* di)
4916 return di->text_present ? di->text_avma : 0;
4919 SizeT VG_(DebugInfo_get_text_size)(const DebugInfo* di)
4921 return di->text_present ? di->text_size : 0;
4924 Addr VG_(DebugInfo_get_bss_avma)(const DebugInfo* di)
4926 return di->bss_present ? di->bss_avma : 0;
4929 SizeT VG_(DebugInfo_get_bss_size)(const DebugInfo* di)
4931 return di->bss_present ? di->bss_size : 0;
4934 Addr VG_(DebugInfo_get_plt_avma)(const DebugInfo* di)
4936 return di->plt_present ? di->plt_avma : 0;
4939 SizeT VG_(DebugInfo_get_plt_size)(const DebugInfo* di)
4941 return di->plt_present ? di->plt_size : 0;
4944 Addr VG_(DebugInfo_get_gotplt_avma)(const DebugInfo* di)
4946 return di->gotplt_present ? di->gotplt_avma : 0;
4949 SizeT VG_(DebugInfo_get_gotplt_size)(const DebugInfo* di)
4951 return di->gotplt_present ? di->gotplt_size : 0;
4954 Addr VG_(DebugInfo_get_got_avma)(const DebugInfo* di)
4956 return di->got_present ? di->got_avma : 0;
4959 SizeT VG_(DebugInfo_get_got_size)(const DebugInfo* di)
4961 return di->got_present ? di->got_size : 0;
4964 const HChar* VG_(DebugInfo_get_soname)(const DebugInfo* di)
4966 return di->soname;
4969 const HChar* VG_(DebugInfo_get_filename)(const DebugInfo* di)
4971 return di->fsm.filename;
4974 PtrdiffT VG_(DebugInfo_get_text_bias)(const DebugInfo* di)
4976 return di->text_present ? di->text_bias : 0;
4979 Int VG_(DebugInfo_syms_howmany) ( const DebugInfo *si )
4981 return si->symtab_used;
4984 void VG_(DebugInfo_syms_getidx) ( const DebugInfo *si,
4985 Int idx,
4986 /*OUT*/SymAVMAs* avmas,
4987 /*OUT*/UInt* size,
4988 /*OUT*/const HChar** pri_name,
4989 /*OUT*/const HChar*** sec_names,
4990 /*OUT*/Bool* isText,
4991 /*OUT*/Bool* isIFunc,
4992 /*OUT*/Bool* isGlobal )
4994 vg_assert(idx >= 0 && idx < si->symtab_used);
4995 if (avmas) *avmas = si->symtab[idx].avmas;
4996 if (size) *size = si->symtab[idx].size;
4997 if (pri_name) *pri_name = si->symtab[idx].pri_name;
4998 if (sec_names) *sec_names = si->symtab[idx].sec_names;
4999 if (isText) *isText = si->symtab[idx].isText;
5000 if (isIFunc) *isIFunc = si->symtab[idx].isIFunc;
5001 if (isGlobal) *isGlobal = si->symtab[idx].isGlobal;
5005 /*------------------------------------------------------------*/
5006 /*--- SectKind query functions ---*/
5007 /*------------------------------------------------------------*/
5009 /* Convert a VgSectKind to a string, which must be copied if you want
5010 to change it. */
5011 const HChar* VG_(pp_SectKind)( VgSectKind kind )
5013 switch (kind) {
5014 case Vg_SectUnknown: return "Unknown";
5015 case Vg_SectText: return "Text";
5016 case Vg_SectData: return "Data";
5017 case Vg_SectBSS: return "BSS";
5018 case Vg_SectGOT: return "GOT";
5019 case Vg_SectPLT: return "PLT";
5020 case Vg_SectOPD: return "OPD";
5021 case Vg_SectGOTPLT: return "GOTPLT";
5022 default: vg_assert(0);
5026 /* Given an address 'a', make a guess of which section of which object
5027 it comes from. If name is non-NULL, then the object's name is put
5028 in *name. The returned name, if any, should be saved away, if there is
5029 a chance that a debug-info will be discarded and the name is being
5030 used later on. */
5031 VgSectKind VG_(DebugInfo_sect_kind)( /*OUT*/const HChar** objname, Addr a)
5033 DebugInfo* di;
5034 VgSectKind res = Vg_SectUnknown;
5036 for (di = debugInfo_list; di != NULL; di = di->next) {
5038 if (0)
5039 VG_(printf)(
5040 "addr=%#lx di=%p %s got=%#lx,%lu plt=%#lx,%lu "
5041 "data=%#lx,%lu bss=%#lx,%lu\n",
5042 a, di, di->fsm.filename,
5043 di->got_avma, di->got_size,
5044 di->plt_avma, di->plt_size,
5045 di->data_avma, di->data_size,
5046 di->bss_avma, di->bss_size);
5048 if (di->text_present
5049 && di->text_size > 0
5050 && a >= di->text_avma && a < di->text_avma + di->text_size) {
5051 res = Vg_SectText;
5052 break;
5054 if (di->data_present
5055 && di->data_size > 0
5056 && a >= di->data_avma && a < di->data_avma + di->data_size) {
5057 res = Vg_SectData;
5058 break;
5060 if (di->sdata_present
5061 && di->sdata_size > 0
5062 && a >= di->sdata_avma && a < di->sdata_avma + di->sdata_size) {
5063 res = Vg_SectData;
5064 break;
5066 if (di->bss_present
5067 && di->bss_size > 0
5068 && a >= di->bss_avma && a < di->bss_avma + di->bss_size) {
5069 res = Vg_SectBSS;
5070 break;
5072 if (di->sbss_present
5073 && di->sbss_size > 0
5074 && a >= di->sbss_avma && a < di->sbss_avma + di->sbss_size) {
5075 res = Vg_SectBSS;
5076 break;
5078 if (di->plt_present
5079 && di->plt_size > 0
5080 && a >= di->plt_avma && a < di->plt_avma + di->plt_size) {
5081 res = Vg_SectPLT;
5082 break;
5084 if (di->got_present
5085 && di->got_size > 0
5086 && a >= di->got_avma && a < di->got_avma + di->got_size) {
5087 res = Vg_SectGOT;
5088 break;
5090 if (di->gotplt_present
5091 && di->gotplt_size > 0
5092 && a >= di->gotplt_avma && a < di->gotplt_avma + di->gotplt_size) {
5093 res = Vg_SectGOTPLT;
5094 break;
5096 if (di->opd_present
5097 && di->opd_size > 0
5098 && a >= di->opd_avma && a < di->opd_avma + di->opd_size) {
5099 res = Vg_SectOPD;
5100 break;
5102 /* we could also check for .eh_frame, if anyone really cares */
5105 vg_assert( (di == NULL && res == Vg_SectUnknown)
5106 || (di != NULL && res != Vg_SectUnknown) );
5108 if (objname) {
5109 if (di && di->fsm.filename) {
5110 *objname = di->fsm.filename;
5111 } else {
5112 *objname = "???";
5116 return res;
5120 static UInt debuginfo_generation = 0;
5122 UInt VG_(debuginfo_generation) (void)
5124 return debuginfo_generation;
5127 static void caches__invalidate ( void ) {
5128 cfsi_m_cache__invalidate();
5129 sym_name_cache__invalidate();
5130 debuginfo_generation++;
5133 #if defined(VGO_freebsd)
5135 * Used by FreeBSD if we detect a syscall cap_enter. That
5136 * means capability mode, and lots of things won't work any more.
5137 * Like opening new file handles. So try to make the most of a bad job
5138 * and read all debuginfo in one go.
5140 void VG_(load_all_debuginfo) (void)
5142 for (DebugInfo* di = debugInfo_list; di; di = di->next) {
5143 VG_(di_load_di)(di);
5146 #endif
5148 /*--------------------------------------------------------------------*/
5149 /*--- end ---*/
5150 /*--------------------------------------------------------------------*/