Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / coregrind / m_debuginfo / debuginfo.c
blobc8a6124a2cb9b485cec59f1accdb28bce08a09d3
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, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 02111-1307, USA.
30 The GNU General Public License is contained in the file COPYING.
33 #include "pub_core_basics.h"
34 #include "pub_core_vki.h"
35 #include "pub_core_threadstate.h"
36 #include "pub_core_debuginfo.h" /* self */
37 #include "pub_core_demangle.h"
38 #include "pub_core_libcbase.h"
39 #include "pub_core_libcassert.h"
40 #include "pub_core_libcprint.h"
41 #include "pub_core_libcfile.h"
42 #include "pub_core_libcproc.h" // VG_(getenv)
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)
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 debug printing for the
71 should-we-load-debuginfo-now? finite state machine. */
72 #define DEBUG_FSM 0
74 /* Set this to 1 to enable somewhat minimal debug printing for the
75 debuginfo-epoch machinery. */
76 #define DEBUG_EPOCHS 0
79 /*------------------------------------------------------------*/
80 /*--- The _svma / _avma / _image / _bias naming scheme ---*/
81 /*------------------------------------------------------------*/
83 /* JRS 11 Jan 07: I find the different kinds of addresses involved in
84 debuginfo reading confusing. Recently I arrived at some
85 terminology which makes it clearer (to me, at least). There are 3
86 kinds of address used in the debuginfo reading process:
88 stated VMAs - the address where (eg) a .so says a symbol is, that
89 is, what it tells you if you consider the .so in
90 isolation
92 actual VMAs - the address where (eg) said symbol really wound up
93 after the .so was mapped into memory
95 image addresses - pointers into the copy of the .so (etc)
96 transiently mmaped aboard whilst we read its info
98 Additionally I use the term 'bias' to denote the difference
99 between stated and actual VMAs for a given entity.
101 This terminology is not used consistently, but a start has been
102 made. readelf.c and the call-frame info reader in readdwarf.c now
103 use it. Specifically, various variables and structure fields have
104 been annotated with _avma / _svma / _image / _bias. In places _img
105 is used instead of _image for the sake of brevity.
109 /*------------------------------------------------------------*/
110 /*--- fwdses ---*/
111 /*------------------------------------------------------------*/
113 static void caches__invalidate (void);
116 /*------------------------------------------------------------*/
117 /*--- Epochs ---*/
118 /*------------------------------------------------------------*/
120 /* The DebugInfo epoch is incremented every time we either load debuginfo in
121 response to an object mapping, or an existing DebugInfo becomes
122 non-current (or will be discarded) due to an object unmap. By storing,
123 in each DebugInfo, the first and last epoch for which it is valid, we can
124 unambiguously identify the set of DebugInfos which should be used to
125 provide metadata for a code or data address, provided we know the epoch
126 to which that address pertains.
128 Note, this isn't the same as the "handle_counter" below. That only
129 advances when new DebugInfos are created. "current_epoch" advances both
130 at DebugInfo created and destruction-or-making-non-current.
133 // The value zero is reserved for indicating an invalid epoch number.
134 static UInt current_epoch = 1;
136 inline DiEpoch VG_(current_DiEpoch) ( void ) {
137 DiEpoch dep; dep.n = current_epoch; return dep;
140 static void advance_current_DiEpoch ( const HChar* msg ) {
141 current_epoch++;
142 if (DEBUG_EPOCHS)
143 VG_(printf)("Advancing current epoch to %u due to %s\n",
144 current_epoch, msg);
147 static inline Bool eq_DiEpoch ( DiEpoch dep1, DiEpoch dep2 ) {
148 return dep1.n == dep2.n && /*neither is invalid*/dep1.n != 0;
151 // Is this DebugInfo currently "allocated" (pre-use state, only FSM active) ?
152 static inline Bool is_DebugInfo_allocated ( const DebugInfo* di )
154 if (is_DiEpoch_INVALID(di->first_epoch)
155 && is_DiEpoch_INVALID(di->last_epoch)) {
156 return True;
157 } else {
158 return False;
162 // Is this DebugInfo currently "active" (valid for the current epoch) ?
163 static inline Bool is_DebugInfo_active ( const DebugInfo* di )
165 if (!is_DiEpoch_INVALID(di->first_epoch)
166 && is_DiEpoch_INVALID(di->last_epoch)) {
167 // Yes it is active. Sanity check ..
168 vg_assert(di->first_epoch.n <= current_epoch);
169 return True;
170 } else {
171 return False;
175 // Is this DebugInfo currently "archived" ?
176 static inline Bool is_DebugInfo_archived ( const DebugInfo* di )
178 if (!is_DiEpoch_INVALID(di->first_epoch)
179 && !is_DiEpoch_INVALID(di->last_epoch)) {
180 // Yes it is archived. Sanity checks ..
181 vg_assert(di->first_epoch.n <= di->last_epoch.n);
182 vg_assert(di->last_epoch.n <= current_epoch);
183 return True;
184 } else {
185 return False;
189 // Is this DebugInfo valid for the specified epoch?
190 static inline Bool is_DI_valid_for_epoch ( const DebugInfo* di, DiEpoch ep )
192 // Stay sane
193 vg_assert(ep.n > 0 && ep.n <= current_epoch);
195 Bool first_valid = !is_DiEpoch_INVALID(di->first_epoch);
196 Bool last_valid = !is_DiEpoch_INVALID(di->last_epoch);
198 if (first_valid) {
199 if (last_valid) {
200 // Both valid. di is in Archived state.
201 return di->first_epoch.n <= ep.n && ep.n <= di->last_epoch.n;
202 } else {
203 // First is valid, last is invalid. di is in Active state.
204 return di->first_epoch.n <= ep.n;
206 } else {
207 vg_assert (!last_valid); // First invalid, last valid is a bad state.
208 // Neither is valid. di is in Allocated state.
209 return False;
214 static inline UInt ROL32 ( UInt x, UInt n )
216 return (x << n) | (x >> (32-n));
220 /*------------------------------------------------------------*/
221 /*--- Root structure ---*/
222 /*------------------------------------------------------------*/
224 /* The root structure for the entire debug info system. It is a
225 linked list of DebugInfos. */
226 static DebugInfo* debugInfo_list = NULL;
229 /* Find 'di' in the debugInfo_list and move it one step closer to the
230 front of the list, so as to make subsequent searches for it
231 cheaper. When used in a controlled way, makes a major improvement
232 in some DebugInfo-search-intensive situations, most notably stack
233 unwinding on amd64-linux. */
234 static void move_DebugInfo_one_step_forward ( DebugInfo* di )
236 DebugInfo *di0, *di1, *di2;
237 if (di == debugInfo_list)
238 return; /* already at head of list */
239 vg_assert(di != NULL);
240 di0 = debugInfo_list;
241 di1 = NULL;
242 di2 = NULL;
243 while (True) {
244 if (di0 == NULL || di0 == di) break;
245 di2 = di1;
246 di1 = di0;
247 di0 = di0->next;
249 vg_assert(di0 == di);
250 if (di0 != NULL && di1 != NULL && di2 != NULL) {
251 DebugInfo* tmp;
252 /* di0 points to di, di1 to its predecessor, and di2 to di1's
253 predecessor. Swap di0 and di1, that is, move di0 one step
254 closer to the start of the list. */
255 vg_assert(di2->next == di1);
256 vg_assert(di1->next == di0);
257 tmp = di0->next;
258 di2->next = di0;
259 di0->next = di1;
260 di1->next = tmp;
262 else
263 if (di0 != NULL && di1 != NULL && di2 == NULL) {
264 /* it's second in the list. */
265 vg_assert(debugInfo_list == di1);
266 vg_assert(di1->next == di0);
267 di1->next = di0->next;
268 di0->next = di1;
269 debugInfo_list = di0;
274 // Debugging helper for epochs
275 static void show_epochs ( const HChar* msg )
277 if (DEBUG_EPOCHS) {
278 DebugInfo* di;
279 VG_(printf)("\nDebugInfo epoch display, requested by \"%s\"\n", msg);
280 VG_(printf)(" Current epoch (note: 0 means \"invalid epoch\") = %u\n",
281 current_epoch);
282 for (di = debugInfo_list; di; di = di->next) {
283 VG_(printf)(" [di=%p] first %u last %u %s\n",
284 di, di->first_epoch.n, di->last_epoch.n, di->fsm.filename);
286 VG_(printf)("\n");
291 /*------------------------------------------------------------*/
292 /*--- Notification (acquire/discard) helpers ---*/
293 /*------------------------------------------------------------*/
295 /* Gives out unique abstract handles for allocated DebugInfos. See
296 comment in priv_storage.h, declaration of struct _DebugInfo, for
297 details. */
298 static ULong handle_counter = 1;
300 /* Allocate and zero out a new DebugInfo record. */
301 static
302 DebugInfo* alloc_DebugInfo( const HChar* filename )
304 Bool traceme;
305 DebugInfo* di;
307 vg_assert(filename);
309 di = ML_(dinfo_zalloc)("di.debuginfo.aDI.1", sizeof(DebugInfo));
310 di->handle = handle_counter++;
311 di->first_epoch = DiEpoch_INVALID();
312 di->last_epoch = DiEpoch_INVALID();
313 di->fsm.filename = ML_(dinfo_strdup)("di.debuginfo.aDI.2", filename);
314 di->fsm.maps = VG_(newXA)(
315 ML_(dinfo_zalloc), "di.debuginfo.aDI.3",
316 ML_(dinfo_free), sizeof(DebugInfoMapping));
318 /* Everything else -- pointers, sizes, arrays -- is zeroed by
319 ML_(dinfo_zalloc). Now set up the debugging-output flags. */
320 traceme
321 = VG_(string_match)( VG_(clo_trace_symtab_patt), filename );
322 if (traceme) {
323 di->trace_symtab = VG_(clo_trace_symtab);
324 di->trace_cfi = VG_(clo_trace_cfi);
325 di->ddump_syms = VG_(clo_debug_dump_syms);
326 di->ddump_line = VG_(clo_debug_dump_line);
327 di->ddump_frames = VG_(clo_debug_dump_frames);
330 return di;
334 /* Free a DebugInfo, and also all the stuff hanging off it. */
335 static void free_DebugInfo ( DebugInfo* di )
337 Word i, j, n;
338 TyEnt* ent;
339 GExpr* gexpr;
341 vg_assert(di != NULL);
342 if (di->fsm.maps) VG_(deleteXA)(di->fsm.maps);
343 if (di->fsm.filename) ML_(dinfo_free)(di->fsm.filename);
344 if (di->fsm.dbgname) ML_(dinfo_free)(di->fsm.dbgname);
345 if (di->soname) ML_(dinfo_free)(di->soname);
346 if (di->loctab) ML_(dinfo_free)(di->loctab);
347 if (di->loctab_fndn_ix) ML_(dinfo_free)(di->loctab_fndn_ix);
348 if (di->inltab) ML_(dinfo_free)(di->inltab);
349 if (di->cfsi_base) ML_(dinfo_free)(di->cfsi_base);
350 if (di->cfsi_m_ix) ML_(dinfo_free)(di->cfsi_m_ix);
351 if (di->cfsi_rd) ML_(dinfo_free)(di->cfsi_rd);
352 if (di->cfsi_m_pool) VG_(deleteDedupPA)(di->cfsi_m_pool);
353 if (di->cfsi_exprs) VG_(deleteXA)(di->cfsi_exprs);
354 if (di->fpo) ML_(dinfo_free)(di->fpo);
356 if (di->symtab) {
357 /* We have to visit all the entries so as to free up any
358 sec_names arrays that might exist. */
359 n = di->symtab_used;
360 for (i = 0; i < n; i++) {
361 DiSym* sym = &di->symtab[i];
362 if (sym->sec_names)
363 ML_(dinfo_free)(sym->sec_names);
365 /* and finally .. */
366 ML_(dinfo_free)(di->symtab);
369 if (di->strpool)
370 VG_(deleteDedupPA) (di->strpool);
371 if (di->fndnpool)
372 VG_(deleteDedupPA) (di->fndnpool);
374 /* Delete the two admin arrays. These lists exist primarily so
375 that we can visit each object exactly once when we need to
376 delete them. */
377 if (di->admin_tyents) {
378 n = VG_(sizeXA)(di->admin_tyents);
379 for (i = 0; i < n; i++) {
380 ent = (TyEnt*)VG_(indexXA)(di->admin_tyents, i);
381 /* Dump anything hanging off this ent */
382 ML_(TyEnt__make_EMPTY)(ent);
384 VG_(deleteXA)(di->admin_tyents);
385 di->admin_tyents = NULL;
388 if (di->admin_gexprs) {
389 n = VG_(sizeXA)(di->admin_gexprs);
390 for (i = 0; i < n; i++) {
391 gexpr = *(GExpr**)VG_(indexXA)(di->admin_gexprs, i);
392 ML_(dinfo_free)(gexpr);
394 VG_(deleteXA)(di->admin_gexprs);
395 di->admin_gexprs = NULL;
398 /* Dump the variable info. This is kinda complex: we must take
399 care not to free items which reside in either the admin lists
400 (as we have just freed them) or which reside in the DebugInfo's
401 string table. */
402 if (di->varinfo) {
403 for (i = 0; i < VG_(sizeXA)(di->varinfo); i++) {
404 OSet* scope = *(OSet**)VG_(indexXA)(di->varinfo, i);
405 if (!scope) continue;
406 /* iterate over all entries in 'scope' */
407 VG_(OSetGen_ResetIter)(scope);
408 while (True) {
409 DiAddrRange* arange = VG_(OSetGen_Next)(scope);
410 if (!arange) break;
411 /* for each var in 'arange' */
412 vg_assert(arange->vars);
413 for (j = 0; j < VG_(sizeXA)( arange->vars ); j++) {
414 DiVariable* var = (DiVariable*)VG_(indexXA)(arange->vars,j);
415 vg_assert(var);
416 /* Nothing to free in var: all the pointer fields refer
417 to stuff either on an admin list, or in
418 .strpool */
420 VG_(deleteXA)(arange->vars);
421 /* Don't free arange itself, as OSetGen_Destroy does
422 that */
424 VG_(OSetGen_Destroy)(scope);
426 VG_(deleteXA)(di->varinfo);
429 ML_(dinfo_free)(di);
433 /* 'di' is a member of debugInfo_list. Find it, and either (remove it from
434 the list and free all storage reachable from it) or archive it.
435 Notify m_redir that this removal/archiving has happened.
437 Note that 'di' can't be archived. Is a DebugInfo is archived then we
438 want to hold on to it forever. This is asserted for.
440 Note also, we don't advance the current epoch here. That's the
441 responsibility of some (non-immediate) caller.
443 static void discard_or_archive_DebugInfo ( DebugInfo* di )
445 const HChar* reason = "munmap";
446 const Bool archive = VG_(clo_keep_debuginfo);
448 DebugInfo** prev_next_ptr = &debugInfo_list;
449 DebugInfo* curr = debugInfo_list;
451 /* It must be active! */
452 vg_assert( is_DebugInfo_active(di));
453 while (curr) {
454 if (curr == di) {
455 /* Found it; (remove from list and free it), or archive it. */
456 if (curr->have_dinfo
457 && (VG_(clo_verbosity) > 1 || VG_(clo_trace_redir)))
458 VG_(message)(Vg_DebugMsg,
459 "%s syms at %#lx-%#lx in %s due to %s()\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 reason);
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 } else {
479 free_DebugInfo(curr);
481 return;
483 prev_next_ptr = &curr->next;
484 curr = curr->next;
487 /* Not found. */
491 /* Repeatedly scan debugInfo_list, looking for DebugInfos with text
492 AVMAs intersecting [start,start+length), and call discard_DebugInfo
493 to get rid of them. This modifies the list, hence the multiple
494 iterations. Returns True iff any such DebugInfos were found.
496 static Bool discard_syms_in_range ( Addr start, SizeT length )
498 Bool anyFound = False;
499 Bool found;
500 DebugInfo* curr;
502 while (True) {
503 found = False;
505 curr = debugInfo_list;
506 while (True) {
507 if (curr == NULL)
508 break;
509 if (is_DebugInfo_archived(curr)
510 || !curr->text_present
511 || (curr->text_present
512 && curr->text_size > 0
513 && (start+length - 1 < curr->text_avma
514 || curr->text_avma + curr->text_size - 1 < start))) {
515 /* no overlap */
516 } else {
517 found = True;
518 break;
520 curr = curr->next;
523 if (!found) break;
524 anyFound = True;
525 discard_or_archive_DebugInfo( curr );
528 return anyFound;
532 /* Does [s1,+len1) overlap [s2,+len2) ? Note: does not handle
533 wraparound at the end of the address space -- just asserts in that
534 case. */
535 static Bool ranges_overlap (Addr s1, SizeT len1, Addr s2, SizeT len2 )
537 Addr e1, e2;
538 if (len1 == 0 || len2 == 0)
539 return False;
540 e1 = s1 + len1 - 1;
541 e2 = s2 + len2 - 1;
542 /* Assert that we don't have wraparound. If we do it would imply
543 that file sections are getting mapped around the end of the
544 address space, which sounds unlikely. */
545 vg_assert(s1 <= e1);
546 vg_assert(s2 <= e2);
547 if (e1 < s2 || e2 < s1) return False;
548 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;
567 return False;
571 /* Discard or archive all elements of debugInfo_list whose .mark bit is set.
573 static void discard_or_archive_marked_DebugInfos ( void )
575 DebugInfo* curr;
577 while (True) {
579 curr = debugInfo_list;
580 while (True) {
581 if (!curr)
582 break;
583 if (curr->mark)
584 break;
585 curr = curr->next;
588 if (!curr) break;
589 discard_or_archive_DebugInfo( curr );
595 /* Discard any elements of debugInfo_list which overlap with diRef.
596 Clearly diRef must have its mapping information set to something sane. */
597 static void discard_DebugInfos_which_overlap_with ( DebugInfo* diRef )
599 vg_assert(is_DebugInfo_allocated(diRef));
600 DebugInfo* di;
601 /* Mark all the DebugInfos in debugInfo_list that need to be
602 deleted. First, clear all the mark bits; then set them if they
603 overlap with siRef. Since siRef itself is in this list we at
604 least expect its own mark bit to be set. */
605 for (di = debugInfo_list; di; di = di->next) {
606 if (is_DebugInfo_archived(di))
607 continue;
608 di->mark = do_DebugInfos_overlap( di, diRef );
609 if (di == diRef) {
610 vg_assert(di->mark);
611 di->mark = False;
614 discard_or_archive_marked_DebugInfos();
618 /* Find the existing DebugInfo for |filename| or if not found, create
619 one. In the latter case |filename| is strdup'd into VG_AR_DINFO,
620 and the new DebugInfo is added to debugInfo_list. */
621 static DebugInfo* find_or_create_DebugInfo_for ( const HChar* filename )
623 DebugInfo* di;
624 vg_assert(filename);
625 for (di = debugInfo_list; di; di = di->next) {
626 if (is_DebugInfo_archived(di))
627 continue;
628 vg_assert(di->fsm.filename);
629 if (0==VG_(strcmp)(di->fsm.filename, filename))
630 break;
632 if (!di) {
633 di = alloc_DebugInfo(filename);
634 vg_assert(di);
635 di->next = debugInfo_list;
636 debugInfo_list = di;
638 vg_assert(!is_DebugInfo_archived(di));
639 return di;
643 /* Debuginfo reading for 'di' has just been successfully completed.
644 Check that the invariants stated in
645 "Comment_on_IMPORTANT_CFSI_REPRESENTATIONAL_INVARIANTS" in
646 priv_storage.h are observed. */
647 static void check_CFSI_related_invariants ( const DebugInfo* di )
649 DebugInfo* di2 = NULL;
650 Bool has_nonempty_rx = False;
651 Bool cfsi_fits = False;
652 Word i, j;
653 vg_assert(di);
654 /* This fn isn't called until after debuginfo for this object has
655 been successfully read. And that shouldn't happen until we have
656 both a r-x and rw- mapping for the object. Hence: */
657 vg_assert(di->fsm.have_rx_map);
658 vg_assert(di->fsm.have_rw_map);
659 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
660 const DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
661 /* We are interested in r-x mappings only */
662 if (!map->rx)
663 continue;
665 /* degenerate case: r-x section is empty */
666 if (map->size == 0)
667 continue;
668 has_nonempty_rx = True;
670 /* normal case: r-x section is nonempty */
671 /* invariant (0) */
672 vg_assert(map->size > 0);
674 /* invariant (1) */
675 for (di2 = debugInfo_list; di2; di2 = di2->next) {
676 if (di2 == di || is_DebugInfo_archived(di2))
677 continue;
678 for (j = 0; j < VG_(sizeXA)(di2->fsm.maps); j++) {
679 const DebugInfoMapping* map2 = VG_(indexXA)(di2->fsm.maps, j);
680 if (!map2->rx || map2->size == 0)
681 continue;
682 vg_assert(!ranges_overlap(map->avma, map->size,
683 map2->avma, map2->size));
686 di2 = NULL;
688 /* invariant (2) */
689 if (di->cfsi_rd) {
690 vg_assert(di->cfsi_minavma <= di->cfsi_maxavma); /* duh! */
691 /* Assume the csfi fits completely into one individual mapping
692 for now. This might need to be improved/reworked later. */
693 if (di->cfsi_minavma >= map->avma &&
694 di->cfsi_maxavma < map->avma + map->size)
695 cfsi_fits = True;
699 /* degenerate case: all r-x sections are empty */
700 if (!has_nonempty_rx) {
701 vg_assert(di->cfsi_rd == NULL);
702 return;
705 /* invariant (2) - cont. */
706 if (di->cfsi_rd)
707 vg_assert(cfsi_fits);
709 /* invariants (3) and (4) */
710 if (di->cfsi_rd) {
711 vg_assert(di->cfsi_used > 0);
712 vg_assert(di->cfsi_size > 0);
713 for (i = 0; i < di->cfsi_used; i++) {
714 DiCfSI* cfsi = &di->cfsi_rd[i];
715 vg_assert(cfsi->len > 0);
716 vg_assert(cfsi->base >= di->cfsi_minavma);
717 vg_assert(cfsi->base + cfsi->len - 1 <= di->cfsi_maxavma);
718 if (i > 0) {
719 DiCfSI* cfsip = &di->cfsi_rd[i-1];
720 vg_assert(cfsip->base + cfsip->len <= cfsi->base);
723 } else {
724 vg_assert(di->cfsi_used == 0);
725 vg_assert(di->cfsi_size == 0);
730 /*--------------------------------------------------------------*/
731 /*--- ---*/
732 /*--- TOP LEVEL: INITIALISE THE DEBUGINFO SYSTEM ---*/
733 /*--- ---*/
734 /*--------------------------------------------------------------*/
736 void VG_(di_initialise) ( void )
738 /* There's actually very little to do here, since everything
739 centers around the DebugInfos in debugInfo_list, they are
740 created and destroyed on demand, and each one is treated more or
741 less independently. */
742 vg_assert(debugInfo_list == NULL);
744 /* flush the debug info caches. */
745 caches__invalidate();
749 /*--------------------------------------------------------------*/
750 /*--- ---*/
751 /*--- TOP LEVEL: NOTIFICATION (ACQUIRE/DISCARD INFO) (LINUX) ---*/
752 /*--- ---*/
753 /*--------------------------------------------------------------*/
755 #if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris)
757 /* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
758 static Bool overlaps_DebugInfoMappings ( const DebugInfoMapping* map1,
759 const DebugInfoMapping* map2 )
761 vg_assert(map1 && map2 && map1 != map2);
762 vg_assert(map1->size != 0 && map2->size != 0);
763 if (map1->avma + map1->size <= map2->avma) return False;
764 if (map2->avma + map2->size <= map1->avma) return False;
765 return True;
769 /* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
770 static void show_DebugInfoMappings
771 ( const DebugInfo* di,
772 /*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
774 Word i, n;
775 vg_assert(maps);
776 n = VG_(sizeXA)(maps);
777 for (i = 0; i < n; i++) {
778 const DebugInfoMapping* map = VG_(indexXA)(maps, i);
779 TRACE_SYMTAB(" [%ld] avma 0x%-16lx size %-8lu "
780 "foff %-8lld %s %s %s\n",
781 i, map->avma, map->size, (Long)map->foff,
782 map->rx ? "rx" : "--",
783 map->rw ? "rw" : "--",
784 map->ro ? "ro" : "--");
789 /* Helper for di_notify_ACHIEVE_ACCEPT_STATE. This removes overlaps
790 in |maps|, in a fairly weak way, by truncating overlapping ends.
791 This may need to be strengthened in future. Currently it performs
792 a post-fixup check, so as least we can be sure that if this
793 function returns (rather than asserts) that |maps| is overlap
794 free. */
795 static void truncate_DebugInfoMapping_overlaps
796 ( const DebugInfo* di,
797 /*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
799 TRACE_SYMTAB("Un-de-overlapped _DebugInfoMappings:\n");
800 show_DebugInfoMappings(di, maps);
801 TRACE_SYMTAB("\n");
803 Word i, j, n;
804 DebugInfoMapping *map_i, *map_j;
806 n = VG_(sizeXA)(maps);
807 for (i = 0; i < n; i++) {
809 map_i = VG_(indexXA)(maps, i);
810 if (map_i->size == 0)
811 continue; // Hmm, mutancy. Shouldn't happen.
813 for (j = i+1; j < n; j++) {
815 map_j = VG_(indexXA)(maps, j);
816 if (map_j->size == 0)
817 continue; // Hmm, mutancy. Shouldn't happen.
819 /* map_j was observed later than map_i, since the entries are
820 in the XArray in the order in which they were observed.
821 If map_j starts inside map_i, trim map_i's end so it does
822 not overlap map_j. This reflects the reality that when
823 two mmaped areas overlap, the later mmap silently
824 overwrites the earlier mmap's mapping. */
825 if (map_j->avma >= map_i->avma
826 && map_j->avma < map_i->avma + map_i->size) {
827 SizeT map_i_newsize = map_j->avma - map_i->avma;
828 vg_assert(map_i_newsize < map_i->size);
829 map_i->size = map_i_newsize;
835 TRACE_SYMTAB("De-overlapped DebugInfoMappings:\n");
836 show_DebugInfoMappings(di, maps);
837 TRACE_SYMTAB("\n");
838 TRACE_SYMTAB("Checking that there are no remaining overlaps.\n");
840 for (i = 0; i < n; i++) {
841 map_i = VG_(indexXA)(maps, i);
842 if (map_i->size == 0)
843 continue;
844 for (j = i+1; j < n; j++) {
845 map_j = VG_(indexXA)(maps, j);
846 if (map_j->size == 0)
847 continue;
848 Bool overlap
849 = overlaps_DebugInfoMappings( map_i, map_j );
850 /* If the following assert ever fails, it means the de-overlapping
851 scheme above is too weak, and needs improvement. */
852 vg_assert(!overlap);
856 TRACE_SYMTAB("Check successful.\n");
860 /* The debug info system is driven by notifications that a text
861 segment has been mapped in, or unmapped, or when sections change
862 permission. It's all a bit kludgey and basically means watching
863 syscalls, trying to second-guess when the system's dynamic linker
864 is done with mapping in a new object for execution. This is all
865 tracked using the DebugInfoFSM struct for the object. Anyway, once
866 we finally decide we've got to an accept state, this section then
867 will acquire whatever info is available for the corresponding
868 object. This section contains the notification handlers, which
869 update the FSM and determine when an accept state has been reached.
872 /* When the sequence of observations causes a DebugInfoFSM to move
873 into the accept state, call here to actually get the debuginfo read
874 in. Returns a ULong whose purpose is described in comments
875 preceding VG_(di_notify_mmap) just below.
877 static ULong di_notify_ACHIEVE_ACCEPT_STATE ( struct _DebugInfo* di )
879 ULong di_handle;
880 Bool ok;
882 advance_current_DiEpoch("di_notify_ACHIEVE_ACCEPT_STATE");
884 vg_assert(di->fsm.filename);
885 TRACE_SYMTAB("\n");
886 TRACE_SYMTAB("------ start ELF OBJECT "
887 "-------------------------"
888 "------------------------------\n");
889 TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
890 TRACE_SYMTAB("\n");
892 /* We're going to read symbols and debug info for the avma
893 ranges specified in the _DebugInfoFsm mapping array. First
894 get rid of any other DebugInfos which overlap any of those
895 ranges (to avoid total confusion). But only those valid in
896 the current epoch. We don't want to discard archived DebugInfos. */
897 discard_DebugInfos_which_overlap_with( di );
899 /* The DebugInfoMappings that now exist in the FSM may involve
900 overlaps. This confuses ML_(read_elf_debug_info), and may cause
901 it to compute wrong biases. So de-overlap them now.
902 See http://bugzilla.mozilla.org/show_bug.cgi?id=788974 */
903 truncate_DebugInfoMapping_overlaps( di, di->fsm.maps );
905 /* And acquire new info. */
906 # if defined(VGO_linux) || defined(VGO_solaris)
907 ok = ML_(read_elf_debug_info)( di );
908 # elif defined(VGO_darwin)
909 ok = ML_(read_macho_debug_info)( di );
910 # else
911 # error "unknown OS"
912 # endif
914 if (ok) {
916 TRACE_SYMTAB("\n------ Canonicalising the "
917 "acquired info ------\n");
918 /* invalidate the debug info caches. */
919 caches__invalidate();
920 /* prepare read data for use */
921 ML_(canonicaliseTables)( di );
922 /* Check invariants listed in
923 Comment_on_IMPORTANT_REPRESENTATIONAL_INVARIANTS in
924 priv_storage.h. */
925 check_CFSI_related_invariants(di);
926 ML_(finish_CFSI_arrays)(di);
928 // Mark di's first epoch point as a valid epoch. Because its
929 // last_epoch value is still invalid, this changes di's state from
930 // "allocated" to "active".
931 vg_assert(is_DebugInfo_allocated(di));
932 di->first_epoch = VG_(current_DiEpoch)();
933 vg_assert(is_DebugInfo_active(di));
934 show_epochs("di_notify_ACHIEVE_ACCEPT_STATE success");
936 /* notify m_redir about it */
937 TRACE_SYMTAB("\n------ Notifying m_redir ------\n");
938 VG_(redir_notify_new_DebugInfo)( di );
939 /* Note that we succeeded */
940 di->have_dinfo = True;
941 vg_assert(di->handle > 0);
942 di_handle = di->handle;
944 } else {
945 TRACE_SYMTAB("\n------ ELF reading failed ------\n");
946 /* Something went wrong (eg. bad ELF file). Should we delete
947 this DebugInfo? No - it contains info on the rw/rx
948 mappings, at least. */
949 di_handle = 0;
950 vg_assert(di->have_dinfo == False);
953 TRACE_SYMTAB("\n");
954 TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
955 TRACE_SYMTAB("------ end ELF OBJECT "
956 "-------------------------"
957 "------------------------------\n");
958 TRACE_SYMTAB("\n");
960 return di_handle;
964 /* Notify the debuginfo system about a new mapping. This is the way
965 new debug information gets loaded. If allow_SkFileV is True, it
966 will try load debug info if the mapping at 'a' belongs to Valgrind;
967 whereas normally (False) it will not do that. This allows us to
968 carefully control when the thing will read symbols from the
969 Valgrind executable itself.
971 If use_fd is not -1, that is used instead of the filename; this
972 avoids perturbing fcntl locks, which are released by simply
973 re-opening and closing the same file (even via different fd!).
975 If a call to VG_(di_notify_mmap) causes debug info to be read, then
976 the returned ULong is an abstract handle which can later be used to
977 refer to the debuginfo read as a result of this specific mapping,
978 in later queries to m_debuginfo. In this case the handle value
979 will be one or above. If the returned value is zero, no debug info
980 was read. */
982 ULong VG_(di_notify_mmap)( Addr a, Bool allow_SkFileV, Int use_fd )
984 NSegment const * seg;
985 const HChar* filename;
986 Bool is_rx_map, is_rw_map, is_ro_map;
987 DebugInfo* di;
988 Int actual_fd, oflags;
989 SysRes preadres;
990 HChar buf1k[1024];
991 Bool debug = (DEBUG_FSM != 0);
992 SysRes statres;
993 struct vg_stat statbuf;
995 vg_assert(use_fd >= -1);
997 /* In short, figure out if this mapping is of interest to us, and
998 if so, try to guess what ld.so is doing and when/if we should
999 read debug info. */
1000 seg = VG_(am_find_nsegment)(a);
1001 vg_assert(seg);
1003 if (debug) {
1004 VG_(printf)("di_notify_mmap-0:\n");
1005 VG_(printf)("di_notify_mmap-1: %#lx-%#lx %c%c%c\n",
1006 seg->start, seg->end,
1007 seg->hasR ? 'r' : '-',
1008 seg->hasW ? 'w' : '-',seg->hasX ? 'x' : '-' );
1011 /* guaranteed by aspacemgr-linux.c, sane_NSegment() */
1012 vg_assert(seg->end > seg->start);
1014 /* Ignore non-file mappings */
1015 if ( ! (seg->kind == SkFileC
1016 || (seg->kind == SkFileV && allow_SkFileV)) )
1017 return 0;
1019 /* If the file doesn't have a name, we're hosed. Give up. */
1020 filename = VG_(am_get_filename)( seg );
1021 if (!filename)
1022 return 0;
1025 * Cannot read from these magic files:
1026 * --20208-- WARNING: Serious error when reading debug info
1027 * --20208-- When reading debug info from /proc/xen/privcmd:
1028 * --20208-- can't read file to inspect ELF header
1030 if (VG_(strncmp)(filename, "/proc/xen/", 10) == 0)
1031 return 0;
1033 if (debug)
1034 VG_(printf)("di_notify_mmap-2: %s\n", filename);
1036 /* Only try to read debug information from regular files. */
1037 statres = VG_(stat)(filename, &statbuf);
1039 /* stat dereferences symlinks, so we don't expect it to succeed and
1040 yet produce something that is a symlink. */
1041 vg_assert(sr_isError(statres) || ! VKI_S_ISLNK(statbuf.mode));
1043 /* Don't let the stat call fail silently. Filter out some known
1044 sources of noise before complaining, though. */
1045 if (sr_isError(statres)) {
1046 DebugInfo fake_di;
1047 Bool quiet = VG_(strstr)(filename, "/var/run/nscd/") != NULL
1048 || VG_(strstr)(filename, "/dev/shm/") != NULL;
1049 if (!quiet && VG_(clo_verbosity) > 1) {
1050 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1051 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm", filename);
1052 ML_(symerr)(&fake_di, True, "failed to stat64/stat this file");
1054 return 0;
1057 /* Finally, the point of all this stattery: if it's not a regular file,
1058 don't try to read debug info from it. */
1059 if (! VKI_S_ISREG(statbuf.mode))
1060 return 0;
1062 /* no uses of statbuf below here. */
1064 /* Now we have to guess if this is a text-like mapping, a data-like
1065 mapping, neither or both. The rules are:
1067 text if: x86-linux r and x
1068 other-linux r and x and not w
1070 data if: x86-linux r and w
1071 other-linux r and w and not x
1073 Background: On x86-linux, objects are typically mapped twice:
1075 1b8fb000-1b8ff000 r-xp 00000000 08:02 4471477 vgpreload_memcheck.so
1076 1b8ff000-1b900000 rw-p 00004000 08:02 4471477 vgpreload_memcheck.so
1078 whereas ppc32-linux mysteriously does this:
1080 118a6000-118ad000 r-xp 00000000 08:05 14209428 vgpreload_memcheck.so
1081 118ad000-118b6000 ---p 00007000 08:05 14209428 vgpreload_memcheck.so
1082 118b6000-118bd000 rwxp 00000000 08:05 14209428 vgpreload_memcheck.so
1084 The third mapping should not be considered to have executable
1085 code in. Therefore a test which works for both is: r and x and
1086 NOT w. Reading symbols from the rwx segment -- which overlaps
1087 the r-x segment in the file -- causes the redirection mechanism
1088 to redirect to addresses in that third segment, which is wrong
1089 and causes crashes.
1091 JRS 28 Dec 05: unfortunately icc 8.1 on x86 has been seen to
1092 produce executables with a single rwx segment rather than a
1093 (r-x,rw-) pair. That means the rules have to be modified thusly:
1095 x86-linux: consider if r and x
1096 all others: consider if r and x and not w
1098 2009 Aug 16: apply similar kludge to ppc32-linux.
1099 See http://bugs.kde.org/show_bug.cgi?id=190820
1101 There are two modes on s390x: with and without the noexec kernel
1102 parameter. Together with some older kernels, this leads to several
1103 variants:
1104 executable: r and x
1105 data: r and w and x
1107 executable: r and x
1108 data: r and w
1110 is_rx_map = False;
1111 is_rw_map = False;
1112 is_ro_map = False;
1114 # if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_mips32) \
1115 || defined(VGA_mips64)
1116 is_rx_map = seg->hasR && seg->hasX;
1117 is_rw_map = seg->hasR && seg->hasW;
1118 # elif defined(VGA_amd64) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
1119 || defined(VGA_arm) || defined(VGA_arm64)
1120 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1121 is_rw_map = seg->hasR && seg->hasW && !seg->hasX;
1122 # elif defined(VGP_s390x_linux)
1123 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1124 is_rw_map = seg->hasR && seg->hasW;
1125 # else
1126 # error "Unknown platform"
1127 # endif
1129 # if defined(VGP_x86_darwin) && DARWIN_VERS >= DARWIN_10_7
1130 is_ro_map = seg->hasR && !seg->hasW && !seg->hasX;
1131 # endif
1133 # if defined(VGO_solaris)
1134 is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
1135 is_rw_map = seg->hasR && seg->hasW;
1136 # endif
1138 if (debug)
1139 VG_(printf)("di_notify_mmap-3: "
1140 "is_rx_map %d, is_rw_map %d, is_ro_map %d\n",
1141 (Int)is_rx_map, (Int)is_rw_map, (Int)is_ro_map);
1143 /* Ignore mappings with permissions we can't possibly be interested in. */
1144 if (!(is_rx_map || is_rw_map || is_ro_map))
1145 return 0;
1147 /* Peer at the first few bytes of the file, to see if it is an ELF */
1148 /* object file. Ignore the file if we do not have read permission. */
1149 VG_(memset)(buf1k, 0, sizeof(buf1k));
1150 oflags = VKI_O_RDONLY;
1151 # if defined(VKI_O_LARGEFILE)
1152 oflags |= VKI_O_LARGEFILE;
1153 # endif
1155 if (use_fd == -1) {
1156 SysRes fd = VG_(open)( filename, oflags, 0 );
1157 if (sr_isError(fd)) {
1158 if (sr_Err(fd) != VKI_EACCES) {
1159 DebugInfo fake_di;
1160 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1161 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm",
1162 filename);
1163 ML_(symerr)(&fake_di, True,
1164 "can't open file to inspect ELF header");
1166 return 0;
1168 actual_fd = sr_Res(fd);
1169 } else {
1170 actual_fd = use_fd;
1173 preadres = VG_(pread)( actual_fd, buf1k, sizeof(buf1k), 0 );
1174 if (use_fd == -1) {
1175 VG_(close)( actual_fd );
1178 if (sr_isError(preadres)) {
1179 DebugInfo fake_di;
1180 VG_(memset)(&fake_di, 0, sizeof(fake_di));
1181 fake_di.fsm.filename = ML_(dinfo_strdup)("di.debuginfo.nmm", filename);
1182 ML_(symerr)(&fake_di, True, "can't read file to inspect ELF header");
1183 return 0;
1185 if (sr_Res(preadres) == 0)
1186 return 0;
1187 vg_assert(sr_Res(preadres) > 0 && sr_Res(preadres) <= sizeof(buf1k) );
1189 /* We're only interested in mappings of object files. */
1190 # if defined(VGO_linux) || defined(VGO_solaris)
1191 if (!ML_(is_elf_object_file)( buf1k, (SizeT)sr_Res(preadres), False ))
1192 return 0;
1193 # elif defined(VGO_darwin)
1194 if (!ML_(is_macho_object_file)( buf1k, (SizeT)sr_Res(preadres) ))
1195 return 0;
1196 # else
1197 # error "unknown OS"
1198 # endif
1200 /* See if we have a DebugInfo for this filename. If not,
1201 create one. */
1202 di = find_or_create_DebugInfo_for( filename );
1203 vg_assert(di);
1205 if (debug)
1206 VG_(printf)("di_notify_mmap-4: "
1207 "noting details in DebugInfo* at %p\n", di);
1209 /* Note the details about the mapping. */
1210 DebugInfoMapping map;
1211 map.avma = seg->start;
1212 map.size = seg->end + 1 - seg->start;
1213 map.foff = seg->offset;
1214 map.rx = is_rx_map;
1215 map.rw = is_rw_map;
1216 map.ro = is_ro_map;
1217 VG_(addToXA)(di->fsm.maps, &map);
1219 /* Update flags about what kind of mappings we've already seen. */
1220 di->fsm.have_rx_map |= is_rx_map;
1221 di->fsm.have_rw_map |= is_rw_map;
1222 di->fsm.have_ro_map |= is_ro_map;
1224 /* So, finally, are we in an accept state? */
1225 if (di->fsm.have_rx_map && di->fsm.have_rw_map && !di->have_dinfo) {
1226 /* Ok, so, finally, we found what we need, and we haven't
1227 already read debuginfo for this object. So let's do so now.
1228 Yee-ha! */
1229 if (debug)
1230 VG_(printf)("di_notify_mmap-5: "
1231 "achieved accept state for %s\n", filename);
1232 return di_notify_ACHIEVE_ACCEPT_STATE ( di );
1233 } else {
1234 /* If we don't have an rx and rw mapping, or if we already have
1235 debuginfo for this mapping for whatever reason, go no
1236 further. */
1237 return 0;
1242 /* Unmap is simpler - throw away any SegInfos intersecting
1243 [a, a+len). */
1244 void VG_(di_notify_munmap)( Addr a, SizeT len )
1246 Bool anyFound;
1247 if (0) VG_(printf)("DISCARD %#lx %#lx\n", a, a+len);
1248 anyFound = discard_syms_in_range(a, len);
1249 if (anyFound) {
1250 caches__invalidate();
1251 advance_current_DiEpoch("VG_(di_notify_munmap)");
1252 show_epochs("VG_(di_notify_munmap)");
1257 /* Uh, this doesn't do anything at all. IIRC glibc (or ld.so, I don't
1258 remember) does a bunch of mprotects on itself, and if we follow
1259 through here, it causes the debug info for that object to get
1260 discarded. */
1261 void VG_(di_notify_mprotect)( Addr a, SizeT len, UInt prot )
1263 Bool exe_ok = toBool(prot & VKI_PROT_EXEC);
1264 # if defined(VGA_x86)
1265 exe_ok = exe_ok || toBool(prot & VKI_PROT_READ);
1266 # endif
1267 if (0 && !exe_ok) {
1268 Bool anyFound = discard_syms_in_range(a, len);
1269 if (anyFound) {
1270 caches__invalidate();
1271 advance_current_DiEpoch("VG_(di_notify_mprotect)");
1277 /* This is a MacOSX >= 10.7 32-bit only special. See comments on the
1278 declaration of struct _DebugInfoFSM for details. */
1279 void VG_(di_notify_vm_protect)( Addr a, SizeT len, UInt prot )
1281 Bool debug = (DEBUG_FSM != 0);
1283 Bool r_ok = toBool(prot & VKI_PROT_READ);
1284 Bool w_ok = toBool(prot & VKI_PROT_WRITE);
1285 Bool x_ok = toBool(prot & VKI_PROT_EXEC);
1286 if (debug) {
1287 VG_(printf)("di_notify_vm_protect-0:\n");
1288 VG_(printf)("di_notify_vm_protect-1: %#lx-%#lx %c%c%c\n",
1289 a, a + len - 1,
1290 r_ok ? 'r' : '-', w_ok ? 'w' : '-', x_ok ? 'x' : '-' );
1293 Bool do_nothing = True;
1294 # if defined(VGP_x86_darwin) && (DARWIN_VERS >= DARWIN_10_7)
1295 do_nothing = False;
1296 # endif
1297 if (do_nothing /* wrong platform */) {
1298 if (debug)
1299 VG_(printf)("di_notify_vm_protect-2: wrong platform, "
1300 "doing nothing.\n");
1301 return;
1304 if (! (r_ok && !w_ok && x_ok))
1305 return; /* not an upgrade to r-x */
1307 /* Find a DebugInfo containing a FSM that has [a, +len) previously
1308 observed as a r-- mapping, plus some other rw- mapping. If such
1309 is found, conclude we're in an accept state and read debuginfo
1310 accordingly. */
1311 if (debug)
1312 VG_(printf)("di_notify_vm_protect-3: looking for existing DebugInfo*\n");
1313 DebugInfo* di;
1314 DebugInfoMapping *map = NULL;
1315 Word i;
1316 for (di = debugInfo_list; di; di = di->next) {
1317 vg_assert(di->fsm.filename);
1318 if (di->have_dinfo)
1319 continue; /* already have debuginfo for this object */
1320 if (!di->fsm.have_ro_map)
1321 continue; /* need to have a r-- mapping for this object */
1322 if (di->fsm.have_rx_map)
1323 continue; /* rx- mapping already exists */
1324 if (!di->fsm.have_rw_map)
1325 continue; /* need to have a rw- mapping */
1326 /* Try to find a mapping matching the memory area. */
1327 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1328 map = VG_(indexXA)(di->fsm.maps, i);
1329 if (map->ro && map->avma == a && map->size == len)
1330 break;
1331 map = NULL;
1333 if (!map)
1334 continue; /* this isn't an upgrade of an r-- mapping */
1335 /* looks like we're in luck! */
1336 break;
1338 if (di == NULL)
1339 return; /* didn't find anything */
1341 if (debug)
1342 VG_(printf)("di_notify_vm_protect-4: found existing DebugInfo* at %p\n",
1343 di);
1345 /* Do the upgrade. Simply update the flags of the mapping
1346 and pretend we never saw the RO map at all. */
1347 vg_assert(di->fsm.have_ro_map);
1348 map->rx = True;
1349 map->ro = False;
1350 di->fsm.have_rx_map = True;
1351 di->fsm.have_ro_map = False;
1352 /* See if there are any more ro mappings */
1353 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1354 map = VG_(indexXA)(di->fsm.maps, i);
1355 if (map->ro) {
1356 di->fsm.have_ro_map = True;
1357 break;
1361 /* Check if we're now in an accept state and read debuginfo. Finally. */
1362 if (di->fsm.have_rx_map && di->fsm.have_rw_map && !di->have_dinfo) {
1363 if (debug)
1364 VG_(printf)("di_notify_vm_protect-5: "
1365 "achieved accept state for %s\n", di->fsm.filename);
1366 ULong di_handle __attribute__((unused))
1367 = di_notify_ACHIEVE_ACCEPT_STATE( di );
1368 /* di_handle is ignored. That's not a problem per se -- it just
1369 means nobody will ever be able to refer to this debuginfo by
1370 handle since nobody will know what the handle value is. */
1375 /*--------- PDB (windows debug info) reading --------- */
1377 /* this should really return ULong, as per VG_(di_notify_mmap). */
1378 void VG_(di_notify_pdb_debuginfo)( Int fd_obj, Addr avma_obj,
1379 SizeT total_size, PtrdiffT bias_obj )
1381 Int i, r, sz_exename;
1382 ULong obj_mtime, pdb_mtime;
1383 HChar* pdbname = NULL;
1384 HChar* dot;
1385 SysRes sres;
1386 Int fd_pdbimage;
1387 SizeT n_pdbimage;
1388 struct vg_stat stat_buf;
1390 if (VG_(clo_verbosity) > 0) {
1391 VG_(message)(Vg_UserMsg, "\n");
1392 VG_(message)(Vg_UserMsg,
1393 "LOAD_PDB_DEBUGINFO: clreq: fd=%d, avma=%#lx, total_size=%lu, "
1394 "bias=%#lx\n",
1395 fd_obj, avma_obj, total_size, (UWord)bias_obj
1399 /* 'fd' refers to the .exe/.dll we're dealing with. Get its modification
1400 time into obj_mtime. */
1401 r = VG_(fstat)(fd_obj, &stat_buf);
1402 if (r == -1)
1403 return; /* stat failed ?! */
1404 vg_assert(r == 0);
1405 obj_mtime = stat_buf.mtime;
1407 /* and get its name into exename. */
1408 const HChar *exe;
1409 if (! VG_(resolve_filename)(fd_obj, &exe))
1410 return; /* failed */
1411 sz_exename = VG_(strlen)(exe);
1412 HChar exename[sz_exename + 1];
1413 VG_(strcpy)(exename, exe); // make a copy on the stack
1415 if (VG_(clo_verbosity) > 0) {
1416 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: objname: %s\n", exename);
1419 /* Try to get the PDB file name from the executable. */
1420 pdbname = ML_(find_name_of_pdb_file)(exename);
1421 if (pdbname) {
1422 vg_assert(VG_(strlen)(pdbname) >= 5); /* 5 = strlen("X.pdb") */
1423 /* So we successfully extracted a name from the PE file. But it's
1424 likely to be of the form
1425 e:\foo\bar\xyzzy\wibble.pdb
1426 and we need to change it into something we can actually open
1427 in Wine-world, which basically means turning it into
1428 $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1429 We also take into account $WINEPREFIX, if it is set.
1430 For the moment, if the name isn't fully qualified, just forget it
1431 (we'd have to root around to find where the pdb actually is)
1433 /* Change all the backslashes to forward slashes */
1434 for (i = 0; pdbname[i]; i++) {
1435 if (pdbname[i] == '\\')
1436 pdbname[i] = '/';
1438 Bool is_quald
1439 = ('a' <= VG_(tolower)(pdbname[0]) && VG_(tolower)(pdbname[0]) <= 'z')
1440 && pdbname[1] == ':'
1441 && pdbname[2] == '/';
1442 HChar* home = VG_(getenv)("HOME");
1443 HChar* wpfx = VG_(getenv)("WINEPREFIX");
1444 if (is_quald && wpfx) {
1445 /* Change e:/foo/bar/xyzzy/wibble.pdb
1446 to $WINEPREFIX/drive_e/foo/bar/xyzzy/wibble.pdb
1448 Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(wpfx) + 50/*misc*/;
1449 HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.1", mashedSzB);
1450 VG_(snprintf)(mashed, mashedSzB, "%s/drive_%c%s",
1451 wpfx, pdbname[0], &pdbname[2]);
1452 vg_assert(mashed[mashedSzB-1] == 0);
1453 ML_(dinfo_free)(pdbname);
1454 pdbname = mashed;
1456 else if (is_quald && home && !wpfx) {
1457 /* Change e:/foo/bar/xyzzy/wibble.pdb
1458 to $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1460 Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(home) + 50/*misc*/;
1461 HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.2", mashedSzB);
1462 VG_(snprintf)(mashed, mashedSzB, "%s/.wine/drive_%c%s",
1463 home, pdbname[0], &pdbname[2]);
1464 vg_assert(mashed[mashedSzB-1] == 0);
1465 ML_(dinfo_free)(pdbname);
1466 pdbname = mashed;
1467 } else {
1468 /* It's not a fully qualified path, or neither $HOME nor $WINE
1469 are set (strange). Give up. */
1470 ML_(dinfo_free)(pdbname);
1471 pdbname = NULL;
1475 /* Try s/exe/pdb/ if we don't have a valid pdbname. */
1476 if (!pdbname) {
1477 /* Try to find a matching PDB file from which to read debuginfo.
1478 Windows PE files have symbol tables and line number information,
1479 but MSVC doesn't seem to use them. */
1480 /* Why +5 ? Because in the worst case, we could find a dot as the
1481 last character of pdbname, and we'd then put "pdb" right after
1482 it, hence extending it a bit. */
1483 pdbname = ML_(dinfo_zalloc)("di.debuginfo.lpd1", sz_exename+5);
1484 VG_(strcpy)(pdbname, exename);
1485 vg_assert(pdbname[sz_exename+5-1] == 0);
1486 dot = VG_(strrchr)(pdbname, '.');
1487 if (!dot)
1488 goto out; /* there's no dot in the exe's name ?! */
1489 if (dot[1] == 0)
1490 goto out; /* hmm, path ends in "." */
1492 if ('A' <= dot[1] && dot[1] <= 'Z')
1493 VG_(strcpy)(dot, ".PDB");
1494 else
1495 VG_(strcpy)(dot, ".pdb");
1497 vg_assert(pdbname[sz_exename+5-1] == 0);
1500 /* See if we can find it, and check it's in-dateness. */
1501 sres = VG_(stat)(pdbname, &stat_buf);
1502 if (sr_isError(sres)) {
1503 VG_(message)(Vg_UserMsg, "Warning: Missing or un-stat-able %s\n",
1504 pdbname);
1505 if (VG_(clo_verbosity) > 0)
1506 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: missing: %s\n", pdbname);
1507 goto out;
1509 pdb_mtime = stat_buf.mtime;
1511 if (obj_mtime > pdb_mtime + 60ULL) {
1512 /* PDB file is older than PE file. Really, the PDB should be
1513 newer than the PE, but that doesn't always seem to be the
1514 case. Allow the PDB to be up to one minute older.
1515 Otherwise, it's probably out of date, in which case ignore it
1516 or we will either (a) print wrong stack traces or more likely
1517 (b) crash.
1519 VG_(message)(Vg_UserMsg,
1520 "Warning: %s (mtime = %llu)\n"
1521 " is older than %s (mtime = %llu)\n",
1522 pdbname, pdb_mtime, exename, obj_mtime);
1525 sres = VG_(open)(pdbname, VKI_O_RDONLY, 0);
1526 if (sr_isError(sres)) {
1527 VG_(message)(Vg_UserMsg, "Warning: Can't open %s\n", pdbname);
1528 goto out;
1531 /* Looks promising; go on to try and read stuff from it. But don't
1532 mmap the file. Instead mmap free space and read the file into
1533 it. This is because files on CIFS filesystems that are mounted
1534 '-o directio' can't be mmap'd, and that mount option is needed
1535 to make CIFS work reliably. (See
1536 http://www.nabble.com/Corrupted-data-on-write-to-
1537 Windows-2003-Server-t2782623.html)
1538 This is slower, but at least it works reliably. */
1539 fd_pdbimage = sr_Res(sres);
1540 n_pdbimage = stat_buf.size;
1541 if (n_pdbimage == 0 || n_pdbimage > 0x7FFFFFFF) {
1542 // 0x7FFFFFFF: why? Because the VG_(read) just below only
1543 // can deal with a signed int as the size of data to read,
1544 // so we can't reliably check for read failure for files
1545 // greater than that size. Hence just skip them; we're
1546 // unlikely to encounter a PDB that large anyway.
1547 VG_(close)(fd_pdbimage);
1548 goto out;
1550 sres = VG_(am_mmap_anon_float_valgrind)( n_pdbimage );
1551 if (sr_isError(sres)) {
1552 VG_(close)(fd_pdbimage);
1553 goto out;
1556 void* pdbimage = (void*)sr_Res(sres);
1557 r = VG_(read)( fd_pdbimage, pdbimage, (Int)n_pdbimage );
1558 if (r < 0 || r != (Int)n_pdbimage) {
1559 VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1560 VG_(close)(fd_pdbimage);
1561 goto out;
1564 if (VG_(clo_verbosity) > 0)
1565 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: pdbname: %s\n", pdbname);
1567 /* play safe; always invalidate the debug info caches. I don't know if
1568 this is necessary, but anyway .. */
1569 caches__invalidate();
1570 /* dump old info for this range, if any */
1571 discard_syms_in_range( avma_obj, total_size );
1572 advance_current_DiEpoch("VG_(di_notify_pdb_debuginfo)");
1574 { DebugInfo* di = find_or_create_DebugInfo_for(exename);
1576 /* this di must be new, since we just nuked any old stuff in the range */
1577 vg_assert(di && !di->fsm.have_rx_map && !di->fsm.have_rw_map);
1578 vg_assert(!di->have_dinfo);
1580 /* don't set up any of the di-> fields; let
1581 ML_(read_pdb_debug_info) do it. */
1582 if (ML_(read_pdb_debug_info)( di, avma_obj, bias_obj,
1583 pdbimage, n_pdbimage, pdbname, pdb_mtime )) {
1584 vg_assert(di->have_dinfo); // fails if PDB read failed
1585 if (VG_(clo_verbosity) > 0) {
1586 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: done: "
1587 "%lu syms, %lu src locs, %lu fpo recs\n",
1588 di->symtab_used, di->loctab_used, di->fpo_size);
1590 } else {
1591 VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: failed loading info "
1592 "from %s\n", pdbname);
1593 /* We cannot make any sense of this pdb, so (force) discard it,
1594 even if VG_(clo_keep_debuginfo) is True. */
1595 const Bool save_clo_keep_debuginfo = VG_(clo_keep_debuginfo);
1596 VG_(clo_keep_debuginfo) = False;
1597 // The below will assert if di is not active. Not too sure what
1598 // the state of di in this failed loading state.
1599 discard_or_archive_DebugInfo (di);
1600 VG_(clo_keep_debuginfo) = save_clo_keep_debuginfo;
1602 VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1603 VG_(close)(fd_pdbimage);
1607 out:
1608 if (pdbname) ML_(dinfo_free)(pdbname);
1611 #endif /* defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) */
1614 /*------------------------------------------------------------*/
1615 /*--- ---*/
1616 /*--- TOP LEVEL: QUERYING EXISTING DEBUG INFO ---*/
1617 /*--- ---*/
1618 /*------------------------------------------------------------*/
1620 void VG_(di_discard_ALL_debuginfo)( void )
1622 DebugInfo *di, *di2;
1623 di = debugInfo_list;
1624 while (di) {
1625 di2 = di->next;
1626 VG_(printf)("XXX rm %p\n", di);
1627 free_DebugInfo( di );
1628 di = di2;
1633 DebugInfoMapping* ML_(find_rx_mapping) ( DebugInfo* di, Addr lo, Addr hi )
1635 Word i;
1636 vg_assert(lo <= hi);
1638 /* Optimization: Try to use the last matched rx mapping first */
1639 if ( di->last_rx_map
1640 && lo >= di->last_rx_map->avma
1641 && hi < di->last_rx_map->avma + di->last_rx_map->size)
1642 return di->last_rx_map;
1644 for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
1645 DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
1646 if ( map->rx && map->size > 0
1647 && lo >= map->avma && hi < map->avma + map->size) {
1648 di->last_rx_map = map;
1649 return map;
1653 return NULL;
1656 /*------------------------------------------------------------*/
1657 /*--- Types and functions for inlined IP cursor ---*/
1658 /*------------------------------------------------------------*/
1660 struct _InlIPCursor {
1661 Addr eip; // Cursor used to describe calls at eip.
1662 DebugInfo* di; // DebugInfo describing inlined calls at eip
1664 Word inltab_lopos; // The inlined fn calls covering eip are in
1665 Word inltab_hipos; // di->inltab[inltab_lopos..inltab_hipos].
1666 // Note that not all inlined fn calls in this range
1667 // are necessarily covering eip.
1669 Int curlevel; // Current level to describe.
1670 // 0 means to describe eip itself.
1671 Word cur_inltab; // inltab pos for call inlined at current level.
1672 Word next_inltab; // inltab pos for call inlined at next (towards main)
1673 // level.
1676 static Bool is_top(const InlIPCursor *iipc)
1678 return !iipc || iipc->cur_inltab == -1;
1681 static Bool is_bottom(const InlIPCursor *iipc)
1683 return !iipc || iipc->next_inltab == -1;
1686 Bool VG_(next_IIPC)(InlIPCursor *iipc)
1688 Word i;
1689 DiInlLoc *hinl = NULL;
1690 Word hinl_pos = -1;
1691 DebugInfo *di;
1693 if (iipc == NULL)
1694 return False;
1696 if (iipc->curlevel <= 0) {
1697 iipc->curlevel--;
1698 return False;
1701 di = iipc->di;
1702 for (i = iipc->inltab_lopos; i <= iipc->inltab_hipos; i++) {
1703 if (di->inltab[i].addr_lo <= iipc->eip
1704 && iipc->eip < di->inltab[i].addr_hi
1705 && di->inltab[i].level < iipc->curlevel
1706 && (!hinl || hinl->level < di->inltab[i].level)) {
1707 hinl = &di->inltab[i];
1708 hinl_pos = i;
1712 iipc->cur_inltab = iipc->next_inltab;
1713 iipc->next_inltab = hinl_pos;
1714 if (iipc->next_inltab < 0)
1715 iipc->curlevel = 0; // no inlined call anymore, describe eip itself
1716 else
1717 iipc->curlevel = di->inltab[iipc->next_inltab].level;
1719 return True;
1722 /* Forward */
1723 static void search_all_loctabs ( DiEpoch ep, Addr ptr,
1724 /*OUT*/DebugInfo** pdi, /*OUT*/Word* locno );
1726 /* Returns the position after which eip would be inserted in inltab.
1727 (-1 if eip should be inserted before position 0).
1728 This is the highest position with an addr_lo <= eip.
1729 As inltab is sorted on addr_lo, dichotomic search can be done
1730 (note that inltab might have duplicates addr_lo). */
1731 static Word inltab_insert_pos (DebugInfo *di, Addr eip)
1733 Word mid,
1734 lo = 0,
1735 hi = di->inltab_used-1;
1736 while (lo <= hi) {
1737 mid = (lo + hi) / 2;
1738 if (eip < di->inltab[mid].addr_lo) { hi = mid-1; continue; }
1739 if (eip > di->inltab[mid].addr_lo) { lo = mid+1; continue; }
1740 lo = mid; break;
1743 while (lo <= di->inltab_used-1 && di->inltab[lo].addr_lo <= eip)
1744 lo++;
1745 #if 0
1746 for (mid = 0; mid <= di->inltab_used-1; mid++)
1747 if (eip < di->inltab[mid].addr_lo)
1748 break;
1749 vg_assert (lo - 1 == mid - 1);
1750 #endif
1751 return lo - 1;
1754 InlIPCursor* VG_(new_IIPC)(DiEpoch ep, Addr eip)
1756 DebugInfo* di;
1757 Word locno;
1758 Word i;
1759 InlIPCursor *ret;
1760 Bool avail;
1762 if (!VG_(clo_read_inline_info))
1763 return NULL; // No way we can find inlined calls.
1765 /* Search the DebugInfo for (ep, eip) */
1766 search_all_loctabs ( ep, eip, &di, &locno );
1767 if (di == NULL || di->inltab_used == 0)
1768 return NULL; // No di (with inltab) containing eip.
1770 /* Search the entry in di->inltab with the highest addr_lo that
1771 contains eip. */
1772 /* We start from the highest pos in inltab after which eip would
1773 be inserted. */
1774 for (i = inltab_insert_pos (di, eip); i >= 0; i--) {
1775 if (di->inltab[i].addr_lo <= eip && eip < di->inltab[i].addr_hi) {
1776 break;
1778 /* Stop the backward scan when reaching an addr_lo which
1779 cannot anymore contain eip : we know that all ranges before
1780 i also cannot contain eip. */
1781 if (di->inltab[i].addr_lo < eip - di->maxinl_codesz)
1782 return NULL;
1785 if (i < 0)
1786 return NULL; // No entry containing eip.
1788 /* We have found the highest entry containing eip.
1789 Build a cursor. */
1790 ret = ML_(dinfo_zalloc) ("dinfo.new_IIPC", sizeof(*ret));
1791 ret->eip = eip;
1792 ret->di = di;
1793 ret->inltab_hipos = i;
1794 for (i = ret->inltab_hipos - 1; i >= 0; i--) {
1796 if (di->inltab[i].addr_lo < eip - di->maxinl_codesz)
1797 break; /* Similar stop backward scan logic as above. */
1799 ret->inltab_lopos = i + 1;
1800 ret->curlevel = MAX_LEVEL;
1801 ret->cur_inltab = -1;
1802 ret->next_inltab = -1;
1804 /* MAX_LEVEL is higher than any stored level. We can use
1805 VG_(next_IIPC) to get to the 'real' first highest call level. */
1806 avail = VG_(next_IIPC) (ret);
1807 vg_assert (avail);
1809 return ret;
1812 void VG_(delete_IIPC)(InlIPCursor *iipc)
1814 if (iipc)
1815 ML_(dinfo_free)( iipc );
1819 /*------------------------------------------------------------*/
1820 /*--- Use of symbol table & location info to create ---*/
1821 /*--- plausible-looking stack dumps. ---*/
1822 /*------------------------------------------------------------*/
1824 /* Search all symtabs that we know about to locate ptr. If found, set
1825 *pdi to the relevant DebugInfo, and *symno to the symtab entry
1826 *number within that. If not found, *psi is set to NULL.
1827 If findText==True, only text symbols are searched for.
1828 If findText==False, only data symbols are searched for.
1830 static void search_all_symtabs ( DiEpoch ep, Addr ptr,
1831 /*OUT*/DebugInfo** pdi, /*OUT*/Word* symno,
1832 Bool findText )
1834 Word sno;
1835 DebugInfo* di;
1836 Bool inRange;
1838 for (di = debugInfo_list; di != NULL; di = di->next) {
1840 if (!is_DI_valid_for_epoch(di, ep))
1841 continue;
1843 if (findText) {
1844 /* Consider any symbol in the r-x mapped area to be text.
1845 See Comment_Regarding_Text_Range_Checks in storage.c for
1846 details. */
1847 inRange = di->fsm.have_rx_map
1848 && (ML_(find_rx_mapping)(di, ptr, ptr) != NULL);
1849 } else {
1850 inRange = (di->data_present
1851 && di->data_size > 0
1852 && di->data_avma <= ptr
1853 && ptr < di->data_avma + di->data_size)
1855 (di->sdata_present
1856 && di->sdata_size > 0
1857 && di->sdata_avma <= ptr
1858 && ptr < di->sdata_avma + di->sdata_size)
1860 (di->bss_present
1861 && di->bss_size > 0
1862 && di->bss_avma <= ptr
1863 && ptr < di->bss_avma + di->bss_size)
1865 (di->sbss_present
1866 && di->sbss_size > 0
1867 && di->sbss_avma <= ptr
1868 && ptr < di->sbss_avma + di->sbss_size)
1870 (di->rodata_present
1871 && di->rodata_size > 0
1872 && di->rodata_avma <= ptr
1873 && ptr < di->rodata_avma + di->rodata_size);
1876 if (!inRange) continue;
1878 sno = ML_(search_one_symtab) ( di, ptr, findText );
1879 if (sno == -1) goto not_found;
1880 *symno = sno;
1881 *pdi = di;
1882 return;
1885 not_found:
1886 *pdi = NULL;
1890 /* Search all loctabs that we know about to locate ptr at epoch ep. If
1891 *found, set pdi to the relevant DebugInfo, and *locno to the loctab entry
1892 *number within that. If not found, *pdi is set to NULL. */
1893 static void search_all_loctabs ( DiEpoch ep, Addr ptr,
1894 /*OUT*/DebugInfo** pdi, /*OUT*/Word* locno )
1896 Word lno;
1897 DebugInfo* di;
1898 for (di = debugInfo_list; di != NULL; di = di->next) {
1899 if (!is_DI_valid_for_epoch(di, ep))
1900 continue;
1901 if (di->text_present
1902 && di->text_size > 0
1903 && di->text_avma <= ptr
1904 && ptr < di->text_avma + di->text_size) {
1905 lno = ML_(search_one_loctab) ( di, ptr );
1906 if (lno == -1) goto not_found;
1907 *locno = lno;
1908 *pdi = di;
1909 return;
1912 not_found:
1913 *pdi = NULL;
1916 /* Caching of queries to symbol names. */
1917 // Prime number, giving about 6Kbytes cache on 32 bits,
1918 // 12Kbytes cache on 64 bits.
1919 #define N_SYM_NAME_CACHE 509
1921 typedef
1922 struct {
1923 // (sym_epoch, sym_avma) are the hash table key.
1924 DiEpoch sym_epoch;
1925 Addr sym_avma;
1926 // Fields below here are not part of the key.
1927 const HChar* sym_name;
1928 PtrdiffT offset : (sizeof(PtrdiffT)*8)-1;
1929 Bool isText : 1;
1931 Sym_Name_CacheEnt;
1932 /* Sym_Name_CacheEnt associates a queried (epoch, address) pair to the sym
1933 name found. By nature, if a sym name was found, it means the searched
1934 address stored in the cache is an avma (see e.g. search_all_symtabs).
1935 Note however that the caller is responsible to work with 'avma' addresses
1936 e.g. when calling VG_(get_fnname) : m_debuginfo.c has no way to
1937 differentiate an 'svma a' from an 'avma a'. It is however unlikely that
1938 svma would percolate outside of this module. */
1940 static Sym_Name_CacheEnt sym_name_cache[N_SYM_NAME_CACHE];
1942 static const HChar* no_sym_name = "<<<noname>>>";
1943 /* We need a special marker for the address 0 : a not used entry has
1944 a zero sym_avma. So, if ever the 0 address is really queried, we need
1945 to be able to detect there is no sym name for this address.
1946 If on some platforms, 0 is associated to a symbol, the cache would
1947 work properly. */
1949 static void sym_name_cache__invalidate ( void ) {
1950 VG_(memset)(&sym_name_cache, 0, sizeof(sym_name_cache));
1951 sym_name_cache[0].sym_name = no_sym_name;
1954 /* The whole point of this whole big deal: map an (epoch, code address) pair
1955 to a plausible symbol name. Returns False if no idea; otherwise True.
1957 Caller supplies buf. If do_cxx_demangling is False, don't do
1958 C++ demangling, regardless of VG_(clo_demangle) -- probably because the
1959 call has come from VG_(get_fnname_raw)(). findText
1960 indicates whether we're looking for a text symbol or a data symbol
1961 -- caller must choose one kind or the other.
1963 NOTE: See IMPORTANT COMMENT above about persistence and ownership
1964 in pub_tool_debuginfo.h
1965 get_sym_name and the fact it calls the demangler is the main reason
1966 for non persistence of the information returned by m_debuginfo.c
1967 functions : the string returned in *BUF is persistent as long as
1968 (1) the DebugInfo it belongs to is not discarded
1969 (2) the demangler is not invoked again
1970 Also, the returned string is owned by "somebody else". Callers must
1971 not free it or modify it. */
1972 static
1973 Bool get_sym_name ( Bool do_cxx_demangling, Bool do_z_demangling,
1974 Bool do_below_main_renaming,
1975 DiEpoch ep, Addr a, const HChar** buf,
1976 Bool match_anywhere_in_sym, Bool show_offset,
1977 Bool findText, /*OUT*/PtrdiffT* offsetP )
1979 // Compute the hash from 'ep' and 'a'. The latter contains lots of
1980 // significant bits, but 'ep' is expected to be a small number, typically
1981 // less than 500. So rotate it around a bit in the hope of spreading the
1982 // bits out somewhat.
1983 vg_assert(!is_DiEpoch_INVALID(ep));
1984 UWord hash = a ^ (UWord)(ep.n ^ ROL32(ep.n, 5)
1985 ^ ROL32(ep.n, 13) ^ ROL32(ep.n, 19));
1986 hash %= N_SYM_NAME_CACHE;
1988 Sym_Name_CacheEnt* se = &sym_name_cache[hash];
1990 if (UNLIKELY(se->sym_epoch.n != ep.n || se->sym_avma != a
1991 || se->isText != findText)) {
1992 DebugInfo* di;
1993 Word sno;
1995 search_all_symtabs ( ep, a, &di, &sno, findText );
1996 se->sym_epoch = ep;
1997 se->sym_avma = a;
1998 se->isText = findText;
1999 if (di == NULL || a == 0)
2000 se->sym_name = no_sym_name;
2001 else {
2002 vg_assert(di->symtab[sno].pri_name);
2003 se->sym_name = di->symtab[sno].pri_name;
2004 se->offset = a - di->symtab[sno].avmas.main;
2008 if (se->sym_name == no_sym_name
2009 || (!match_anywhere_in_sym && se->offset != 0)) {
2010 *buf = "";
2011 return False;
2014 VG_(demangle) ( do_cxx_demangling, do_z_demangling,
2015 se->sym_name, buf );
2017 /* Do the below-main hack */
2018 // To reduce the endless nuisance of multiple different names
2019 // for "the frame below main()" screwing up the testsuite, change all
2020 // known incarnations of said into a single name, "(below main)", if
2021 // --show-below-main=yes.
2022 if ( do_below_main_renaming && ! VG_(clo_show_below_main)
2023 && Vg_FnNameBelowMain == VG_(get_fnname_kind)(*buf) )
2025 *buf = "(below main)";
2028 if (offsetP) *offsetP = se->offset;
2030 if (show_offset && se->offset != 0) {
2031 static HChar *bufwo; // buf with offset
2032 static SizeT bufwo_szB;
2033 SizeT need, len;
2035 len = VG_(strlen)(*buf);
2036 need = len + 1 + 19 + 1;
2037 if (need > bufwo_szB) {
2038 bufwo = ML_(dinfo_realloc)("get_sym_size", bufwo, need);
2039 bufwo_szB = need;
2042 VG_(strcpy)(bufwo, *buf);
2043 VG_(sprintf)(bufwo + len, "%c%ld",
2044 se->offset < 0 ? '-' : '+',
2045 (PtrdiffT) (se->offset < 0 ? -se->offset : se->offset));
2046 *buf = bufwo;
2049 return True;
2052 /* ppc64be-linux only: find the TOC pointer (R2 value) that should be in
2053 force at the entry point address of the function containing
2054 guest_code_addr. Returns 0 if not known. */
2055 Addr VG_(get_tocptr) ( DiEpoch ep, Addr guest_code_addr )
2057 #if defined(VGA_ppc64be) || defined(VGA_ppc64le)
2058 DebugInfo* si;
2059 Word sno;
2060 search_all_symtabs ( ep, guest_code_addr,
2061 &si, &sno,
2062 True/*consider text symbols only*/ );
2063 if (si == NULL)
2064 return 0;
2065 else
2066 return GET_TOCPTR_AVMA(si->symtab[sno].avmas);
2067 #else
2068 return 0;
2069 #endif
2072 /* This is available to tools... always demangle C++ names,
2073 match anywhere in function, but don't show offsets.
2074 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2075 in pub_tool_debuginfo.h */
2076 Bool VG_(get_fnname) ( DiEpoch ep, Addr a, const HChar** buf )
2078 return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2079 /*below-main-renaming*/True,
2080 ep, a, buf,
2081 /*match_anywhere_in_fun*/True,
2082 /*show offset?*/False,
2083 /*text sym*/True,
2084 /*offsetP*/NULL );
2087 /* This is available to tools... always demangle C++ names,
2088 match anywhere in function, and show offset if nonzero.
2089 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2090 in pub_tool_debuginfo.h */
2091 Bool VG_(get_fnname_w_offset) ( DiEpoch ep, Addr a, const HChar** buf )
2093 return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2094 /*below-main-renaming*/True,
2095 ep, a, buf,
2096 /*match_anywhere_in_fun*/True,
2097 /*show offset?*/True,
2098 /*text sym*/True,
2099 /*offsetP*/NULL );
2102 /* This is available to tools... always demangle C++ names,
2103 only succeed if 'a' matches first instruction of function,
2104 and don't show offsets.
2105 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2106 in pub_tool_debuginfo.h */
2107 Bool VG_(get_fnname_if_entry) ( DiEpoch ep, Addr a, const HChar** buf )
2109 const HChar *tmp;
2110 Bool res;
2112 res = get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
2113 /*below-main-renaming*/True,
2114 ep, a, &tmp,
2115 /*match_anywhere_in_fun*/False,
2116 /*show offset?*/False,
2117 /*text sym*/True,
2118 /*offsetP*/NULL );
2119 if (res)
2120 *buf = tmp;
2121 return res;
2124 /* This is only available to core... don't C++-demangle, don't Z-demangle,
2125 don't rename below-main, match anywhere in function, and don't show
2126 offsets.
2127 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2128 in pub_tool_debuginfo.h */
2129 Bool VG_(get_fnname_raw) ( DiEpoch ep, Addr a, const HChar** buf )
2131 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2132 /*below-main-renaming*/False,
2133 ep, a, buf,
2134 /*match_anywhere_in_fun*/True,
2135 /*show offset?*/False,
2136 /*text sym*/True,
2137 /*offsetP*/NULL );
2140 /* This is only available to core... don't demangle C++ names, but do
2141 do Z-demangling and below-main-renaming, match anywhere in function, and
2142 don't show offsets.
2143 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2144 in pub_tool_debuginfo.h */
2145 Bool VG_(get_fnname_no_cxx_demangle) ( DiEpoch ep, Addr a, const HChar** buf,
2146 const InlIPCursor* iipc )
2148 // All the callers of VG_(get_fnname_no_cxx_demangle) must build
2149 // the iipc with the same ep as provided to VG_(get_fnname_no_cxx_demangle).
2150 // So, if we have an iipc, iipc->di must be valid in the provided ep.
2151 // Functionally, we could equally use iipc->di->first_epoch or ep, as
2152 // all the inlined fn calls will be described by the same di.
2153 if (iipc) {
2154 vg_assert(is_DI_valid_for_epoch(iipc->di, ep));
2157 if (is_bottom(iipc)) {
2158 // At the bottom (towards main), we describe the fn at eip.
2159 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/True,
2160 /*below-main-renaming*/True,
2161 ep, a, buf,
2162 /*match_anywhere_in_fun*/True,
2163 /*show offset?*/False,
2164 /*text sym*/True,
2165 /*offsetP*/NULL );
2166 } else {
2167 const DiInlLoc *next_inl = iipc && iipc->next_inltab >= 0
2168 ? & iipc->di->inltab[iipc->next_inltab]
2169 : NULL;
2170 vg_assert (next_inl);
2171 // The function we are in is called by next_inl.
2172 *buf = next_inl->inlinedfn;
2173 return True;
2177 /* mips-linux only: find the offset of current address. This is needed for
2178 stack unwinding for MIPS.
2180 Bool VG_(get_inst_offset_in_function)( DiEpoch ep, Addr a,
2181 /*OUT*/PtrdiffT* offset )
2183 const HChar *fnname;
2184 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2185 /*below-main-renaming*/False,
2186 ep, a, &fnname,
2187 /*match_anywhere_in_sym*/True,
2188 /*show offset?*/False,
2189 /*text sym*/True,
2190 offset );
2193 Vg_FnNameKind VG_(get_fnname_kind) ( const HChar* name )
2195 if (VG_STREQ("main", name)) {
2196 return Vg_FnNameMain;
2198 } else if (
2199 # if defined(VGO_linux)
2200 VG_STREQ("__libc_start_main", name) || // glibc glibness
2201 VG_STREQ("generic_start_main", name) || // Yellow Dog doggedness
2202 # if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
2203 VG_STREQ("generic_start_main.isra.0", name) || // ppc glibness
2204 # endif
2205 # elif defined(VGO_darwin)
2206 // See readmacho.c for an explanation of this.
2207 VG_STREQ("start_according_to_valgrind", name) || // Darwin, darling
2208 # elif defined(VGO_solaris)
2209 VG_STREQ("_start", name) || // main() is called directly from _start
2210 # else
2211 # error "Unknown OS"
2212 # endif
2213 0) {
2214 return Vg_FnNameBelowMain;
2216 } else {
2217 return Vg_FnNameNormal;
2221 Vg_FnNameKind VG_(get_fnname_kind_from_IP) ( DiEpoch ep, Addr ip )
2223 const HChar *buf;
2225 // We don't demangle, because it's faster not to, and the special names
2226 // we're looking for won't be mangled.
2227 if (VG_(get_fnname_raw) ( ep, ip, &buf )) {
2229 return VG_(get_fnname_kind)(buf);
2230 } else {
2231 return Vg_FnNameNormal; // Don't know the name, treat it as normal.
2235 /* Looks up data_addr in the collection of data symbols, and if found
2236 puts a pointer to its name into dname. The name is zero terminated.
2237 Also data_addr's offset from the symbol start is put into *offset.
2238 NOTE: See IMPORTANT COMMENT above about persistence and ownership
2239 in pub_tool_debuginfo.h */
2240 Bool VG_(get_datasym_and_offset)( DiEpoch ep, Addr data_addr,
2241 /*OUT*/const HChar** dname,
2242 /*OUT*/PtrdiffT* offset )
2244 return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
2245 /*below-main-renaming*/False,
2246 ep, data_addr, dname,
2247 /*match_anywhere_in_sym*/True,
2248 /*show offset?*/False,
2249 /*text sym*/False,
2250 offset );
2253 /* Map a code address to the name of a shared object file or the
2254 executable. Returns False if no idea; otherwise True.
2255 Note: the string returned in *BUF is persistent as long as
2256 (1) the DebugInfo it belongs to is not discarded
2257 (2) the segment containing the address is not merged with another segment
2259 Bool VG_(get_objname) ( DiEpoch ep, Addr a, const HChar** objname )
2261 DebugInfo* di;
2262 const NSegment *seg;
2263 const HChar* filename;
2265 /* Look in the debugInfo_list to find the name. In most cases we
2266 expect this to produce a result. */
2267 for (di = debugInfo_list; di != NULL; di = di->next) {
2268 if (!is_DI_valid_for_epoch(di, ep))
2269 continue;
2270 if (di->text_present
2271 && di->text_size > 0
2272 && di->text_avma <= a
2273 && a < di->text_avma + di->text_size) {
2274 *objname = di->fsm.filename;
2275 return True;
2278 /* Last-ditch fallback position: if we don't find the address in
2279 the debugInfo_list, ask the address space manager whether it
2280 knows the name of the file associated with this mapping. This
2281 allows us to print the names of exe/dll files in the stack trace
2282 when running programs under wine.
2284 Restrict this to the case where 'ep' is the current epoch, though, so
2285 that we don't return information about this epoch when the caller was
2286 enquiring about a different one. */
2287 if ( eq_DiEpoch(ep, VG_(current_DiEpoch)())
2288 && (seg = VG_(am_find_nsegment)(a)) != NULL
2289 && (filename = VG_(am_get_filename)(seg)) != NULL ) {
2290 *objname = filename;
2291 return True;
2293 return False;
2296 /* Map a code address to its DebugInfo. Returns NULL if not found. Doesn't
2297 require debug info. */
2298 DebugInfo* VG_(find_DebugInfo) ( DiEpoch ep, Addr a )
2300 static UWord n_search = 0;
2301 DebugInfo* di;
2302 n_search++;
2303 for (di = debugInfo_list; di != NULL; di = di->next) {
2304 if (!is_DI_valid_for_epoch(di, ep))
2305 continue;
2306 if (di->text_present
2307 && di->text_size > 0
2308 && di->text_avma <= a
2309 && a < di->text_avma + di->text_size) {
2310 if (0 == (n_search & 0xF))
2311 move_DebugInfo_one_step_forward( di );
2312 return di;
2315 return NULL;
2318 /* Map a code address to a filename. Returns True if successful. The
2319 returned string is persistent as long as the DebugInfo to which it
2320 belongs is not discarded. */
2321 Bool VG_(get_filename)( DiEpoch ep, Addr a, const HChar** filename )
2323 DebugInfo* si;
2324 Word locno;
2325 UInt fndn_ix;
2327 search_all_loctabs ( ep, a, &si, &locno );
2328 if (si == NULL)
2329 return False;
2330 fndn_ix = ML_(fndn_ix) (si, locno);
2331 *filename = ML_(fndn_ix2filename) (si, fndn_ix);
2332 return True;
2335 /* Map a code address to a line number. Returns True if successful. */
2336 Bool VG_(get_linenum)( DiEpoch ep, Addr a, UInt* lineno )
2338 DebugInfo* si;
2339 Word locno;
2340 search_all_loctabs ( ep, a, &si, &locno );
2341 if (si == NULL)
2342 return False;
2343 *lineno = si->loctab[locno].lineno;
2345 return True;
2348 /* Map a code address to a filename/line number/dir name info.
2349 See prototype for detailed description of behaviour.
2351 Bool VG_(get_filename_linenum) ( DiEpoch ep, Addr a,
2352 /*OUT*/const HChar** filename,
2353 /*OUT*/const HChar** dirname,
2354 /*OUT*/UInt* lineno )
2356 DebugInfo* si;
2357 Word locno;
2358 UInt fndn_ix;
2360 search_all_loctabs ( ep, a, &si, &locno );
2361 if (si == NULL) {
2362 if (dirname) {
2363 *dirname = "";
2365 *filename = ""; // this used to be not initialised....
2366 return False;
2369 fndn_ix = ML_(fndn_ix)(si, locno);
2370 *filename = ML_(fndn_ix2filename) (si, fndn_ix);
2371 *lineno = si->loctab[locno].lineno;
2373 if (dirname) {
2374 /* caller wants directory info too .. */
2375 *dirname = ML_(fndn_ix2dirname) (si, fndn_ix);
2378 return True;
2382 /* Map a function name to its entry point and toc pointer. Is done by
2383 sequential search of all symbol tables, so is very slow. To
2384 mitigate the worst performance effects, you may specify a soname
2385 pattern, and only objects matching that pattern are searched.
2386 Therefore specify "*" to search all the objects. On TOC-afflicted
2387 platforms, a symbol is deemed to be found only if it has a nonzero
2388 TOC pointer. */
2389 Bool VG_(lookup_symbol_SLOW)(DiEpoch ep,
2390 const HChar* sopatt, const HChar* name,
2391 SymAVMAs* avmas)
2393 Bool require_pToc = False;
2394 Int i;
2395 const DebugInfo* si;
2396 Bool debug = False;
2397 # if defined(VG_PLAT_USES_PPCTOC)
2398 require_pToc = True;
2399 # endif
2400 for (si = debugInfo_list; si; si = si->next) {
2401 if (debug)
2402 VG_(printf)("lookup_symbol_SLOW: considering %s\n", si->soname);
2403 if (!is_DI_valid_for_epoch(si, ep))
2404 continue;
2405 if (!VG_(string_match)(sopatt, si->soname)) {
2406 if (debug)
2407 VG_(printf)(" ... skip\n");
2408 continue;
2410 for (i = 0; i < si->symtab_used; i++) {
2411 const HChar* pri_name = si->symtab[i].pri_name;
2412 vg_assert(pri_name);
2413 if (0==VG_(strcmp)(name, pri_name)
2414 && (require_pToc ? GET_TOCPTR_AVMA(si->symtab[i].avmas) : True)) {
2415 *avmas = si->symtab[i].avmas;
2416 return True;
2418 const HChar** sec_names = si->symtab[i].sec_names;
2419 if (sec_names) {
2420 vg_assert(sec_names[0]);
2421 while (*sec_names) {
2422 if (0==VG_(strcmp)(name, *sec_names)
2423 && (require_pToc
2424 ? GET_TOCPTR_AVMA(si->symtab[i].avmas) : True)) {
2425 *avmas = si->symtab[i].avmas;
2426 return True;
2428 sec_names++;
2433 return False;
2437 /* VG_(describe_IP): return info on code address, function name and
2438 filename. The returned string is allocated in a static buffer and will
2439 be overwritten in the next invocation. */
2441 /* Copy str into *buf starting at n, ensuring that buf is zero-terminated.
2442 Return the index of the terminating null character. */
2443 static SizeT
2444 putStr( SizeT n, HChar** buf, SizeT *bufsiz, const HChar* str )
2446 SizeT slen = VG_(strlen)(str);
2447 SizeT need = n + slen + 1;
2449 if (need > *bufsiz) {
2450 if (need < 256) need = 256;
2451 *bufsiz = need;
2452 *buf = ML_(dinfo_realloc)("putStr", *buf, *bufsiz);
2455 VG_(strcpy)(*buf + n, str);
2457 return n + slen;
2460 /* Same as putStr, but escaping chars for XML output. */
2461 static SizeT
2462 putStrEsc( SizeT n, HChar** buf, SizeT *bufsiz, const HChar* str )
2464 HChar alt[2];
2466 for (; *str != 0; str++) {
2467 switch (*str) {
2468 case '&':
2469 n = putStr( n, buf, bufsiz, "&amp;");
2470 break;
2471 case '<':
2472 n = putStr( n, buf, bufsiz, "&lt;");
2473 break;
2474 case '>':
2475 n = putStr( n, buf, bufsiz, "&gt;");
2476 break;
2477 default:
2478 alt[0] = *str;
2479 alt[1] = 0;
2480 n = putStr( n, buf, bufsiz, alt );
2481 break;
2484 return n;
2487 const HChar* VG_(describe_IP)(DiEpoch ep, Addr eip, const InlIPCursor *iipc)
2489 static HChar *buf = NULL;
2490 static SizeT bufsiz = 0;
2491 # define APPEND(_str) \
2492 n = putStr(n, &buf, &bufsiz, _str)
2493 # define APPEND_ESC(_str) \
2494 n = putStrEsc(n, &buf, &bufsiz, _str)
2496 UInt lineno;
2497 HChar ibuf[50]; // large enough
2498 SizeT n = 0;
2500 // An InlIPCursor is associated with one specific DebugInfo. So if
2501 // it exists, make sure that it is valid for the specified DiEpoch.
2502 vg_assert (!iipc
2503 || (is_DI_valid_for_epoch(iipc->di, ep) && iipc->eip == eip));
2505 const HChar *buf_fn;
2506 const HChar *buf_obj;
2507 const HChar *buf_srcloc;
2508 const HChar *buf_dirname;
2510 Bool know_dirinfo;
2511 Bool know_fnname;
2512 Bool know_objname;
2513 Bool know_srcloc;
2515 if (is_bottom(iipc)) {
2516 // At the bottom (towards main), we describe the fn at eip.
2517 know_fnname = VG_(clo_sym_offsets)
2518 ? VG_(get_fnname_w_offset) (ep, eip, &buf_fn)
2519 : VG_(get_fnname) (ep, eip, &buf_fn);
2520 } else {
2521 const DiInlLoc *next_inl = iipc && iipc->next_inltab >= 0
2522 ? & iipc->di->inltab[iipc->next_inltab]
2523 : NULL;
2524 vg_assert (next_inl);
2525 // The function we are in is called by next_inl.
2526 buf_fn = next_inl->inlinedfn;
2527 know_fnname = True;
2529 // INLINED????
2530 // ??? Can we compute an offset for an inlined fn call ?
2531 // ??? Offset from what ? The beginning of the inl info ?
2532 // ??? But that is not necessarily the beginning of the fn
2533 // ??? as e.g. an inlined fn call can be in several ranges.
2534 // ??? Currently never showing an offset.
2537 know_objname = VG_(get_objname)(ep, eip, &buf_obj);
2539 if (is_top(iipc)) {
2540 // The source for the highest level is in the loctab entry.
2541 know_srcloc = VG_(get_filename_linenum)(
2542 ep, eip,
2543 &buf_srcloc,
2544 &buf_dirname,
2545 &lineno
2547 know_dirinfo = buf_dirname[0] != '\0';
2548 } else {
2549 const DiInlLoc *cur_inl = iipc && iipc->cur_inltab >= 0
2550 ? & iipc->di->inltab[iipc->cur_inltab]
2551 : NULL;
2552 vg_assert (cur_inl);
2554 know_dirinfo = False;
2555 buf_dirname = "";
2556 // The fndn_ix and lineno for the caller of the inlined fn is in cur_inl.
2557 if (cur_inl->fndn_ix == 0) {
2558 buf_srcloc = "???";
2559 } else {
2560 FnDn *fndn = VG_(indexEltNumber) (iipc->di->fndnpool,
2561 cur_inl->fndn_ix);
2562 if (fndn->dirname) {
2563 buf_dirname = fndn->dirname;
2564 know_dirinfo = True;
2566 buf_srcloc = fndn->filename;
2568 lineno = cur_inl->lineno;
2569 know_srcloc = True;
2572 if (VG_(clo_xml)) {
2574 Bool human_readable = True;
2575 const HChar* maybe_newline = human_readable ? "\n " : "";
2576 const HChar* maybe_newline2 = human_readable ? "\n " : "";
2578 /* Print in XML format, dumping in as much info as we know.
2579 Ensure all tags are balanced. */
2580 APPEND("<frame>");
2581 VG_(sprintf)(ibuf,"<ip>0x%lX</ip>", eip);
2582 APPEND(maybe_newline);
2583 APPEND(ibuf);
2584 if (know_objname) {
2585 APPEND(maybe_newline);
2586 APPEND("<obj>");
2587 APPEND_ESC(buf_obj);
2588 APPEND("</obj>");
2590 if (know_fnname) {
2591 APPEND(maybe_newline);
2592 APPEND("<fn>");
2593 APPEND_ESC(buf_fn);
2594 APPEND("</fn>");
2596 if (know_srcloc) {
2597 if (know_dirinfo) {
2598 APPEND(maybe_newline);
2599 APPEND("<dir>");
2600 APPEND_ESC(buf_dirname);
2601 APPEND("</dir>");
2603 APPEND(maybe_newline);
2604 APPEND("<file>");
2605 APPEND_ESC(buf_srcloc);
2606 APPEND("</file>");
2607 APPEND(maybe_newline);
2608 APPEND("<line>");
2609 VG_(sprintf)(ibuf,"%u",lineno);
2610 APPEND(ibuf);
2611 APPEND("</line>");
2613 APPEND(maybe_newline2);
2614 APPEND("</frame>");
2616 } else {
2618 /* Print for humans to read */
2620 // Possible forms:
2622 // 0x80483BF: really (a.c:20)
2623 // 0x80483BF: really (in /foo/a.out)
2624 // 0x80483BF: really (in ???)
2625 // 0x80483BF: ??? (in /foo/a.out)
2626 // 0x80483BF: ??? (a.c:20)
2627 // 0x80483BF: ???
2629 VG_(sprintf)(ibuf,"0x%lX: ", eip);
2630 APPEND(ibuf);
2631 if (know_fnname) {
2632 APPEND(buf_fn);
2633 } else {
2634 APPEND("???");
2636 if (know_srcloc) {
2637 APPEND(" (");
2638 // Get the directory name, if any, possibly pruned, into dirname.
2639 const HChar* dirname = NULL;
2640 if (know_dirinfo && VG_(sizeXA)(VG_(clo_fullpath_after)) > 0) {
2641 Int i;
2642 dirname = buf_dirname;
2643 // Remove leading prefixes from the dirname.
2644 // If user supplied --fullpath-after=foo, this will remove
2645 // a leading string which matches '.*foo' (not greedy).
2646 for (i = 0; i < VG_(sizeXA)(VG_(clo_fullpath_after)); i++) {
2647 const HChar* prefix =
2648 *(HChar**) VG_(indexXA)( VG_(clo_fullpath_after), i );
2649 HChar* str = VG_(strstr)(dirname, prefix);
2650 if (str) {
2651 dirname = str + VG_(strlen)(prefix);
2652 break;
2655 /* remove leading "./" */
2656 if (dirname[0] == '.' && dirname[1] == '/')
2657 dirname += 2;
2659 // do we have any interesting directory name to show? If so
2660 // add it in.
2661 if (dirname && dirname[0] != 0) {
2662 APPEND(dirname);
2663 APPEND("/");
2665 APPEND(buf_srcloc);
2666 APPEND(":");
2667 VG_(sprintf)(ibuf,"%u",lineno);
2668 APPEND(ibuf);
2669 APPEND(")");
2670 } else if (know_objname) {
2671 APPEND(" (in ");
2672 APPEND(buf_obj);
2673 APPEND(")");
2674 } else if (know_fnname) {
2675 // Nb: do this in two steps because "??)" is a trigraph!
2676 APPEND(" (in ???");
2677 APPEND(")");
2681 return buf;
2683 # undef APPEND
2684 # undef APPEND_ESC
2688 /*--------------------------------------------------------------*/
2689 /*--- ---*/
2690 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
2691 /*--- DWARF3 .eh_frame INFO ---*/
2692 /*--- ---*/
2693 /*--------------------------------------------------------------*/
2695 /* Note that the CFI machinery pertains to unwinding the stack "right now".
2696 There is no support for unwinding stack images obtained from some time in
2697 the past. That means that:
2699 (1) We only deal with CFI from DebugInfos that are valid for the current
2700 debuginfo epoch. Unlike in the rest of the file, there is no
2701 epoch-awareness.
2703 (2) We assume that the CFI cache will be invalidated every time the the
2704 epoch changes. This is done by ensuring (in the file above) that
2705 every call to advance_current_DiEpoch has a call to
2706 caches__invalidate alongside it.
2709 /* Gather up all the constant pieces of info needed to evaluate
2710 a CfiExpr into one convenient struct. */
2711 typedef
2712 struct {
2713 const D3UnwindRegs* uregs;
2714 Addr min_accessible;
2715 Addr max_accessible;
2717 CfiExprEvalContext;
2719 /* Evaluate the CfiExpr rooted at ix in exprs given the context eec.
2720 *ok is set to False on failure, but not to True on success. The
2721 caller must set it to True before calling. */
2722 __attribute__((noinline))
2723 static
2724 UWord evalCfiExpr ( const XArray* exprs, Int ix,
2725 const CfiExprEvalContext* eec, Bool* ok )
2727 UWord w, wL, wR;
2728 Addr a;
2729 const CfiExpr* e;
2730 vg_assert(sizeof(Addr) == sizeof(UWord));
2731 e = VG_(indexXA)( exprs, ix );
2732 switch (e->tag) {
2733 case Cex_Unop:
2734 w = evalCfiExpr( exprs, e->Cex.Unop.ix, eec, ok );
2735 if (!(*ok)) return 0;
2736 switch (e->Cex.Unop.op) {
2737 case Cunop_Abs: return (Word) w < 0 ? - w : w;
2738 case Cunop_Neg: return - (Word) w;
2739 case Cunop_Not: return ~ w;
2740 default: goto unhandled;
2742 /*NOTREACHED*/
2743 case Cex_Binop:
2744 wL = evalCfiExpr( exprs, e->Cex.Binop.ixL, eec, ok );
2745 if (!(*ok)) return 0;
2746 wR = evalCfiExpr( exprs, e->Cex.Binop.ixR, eec, ok );
2747 if (!(*ok)) return 0;
2748 switch (e->Cex.Binop.op) {
2749 case Cbinop_Add: return wL + wR;
2750 case Cbinop_Sub: return wL - wR;
2751 case Cbinop_And: return wL & wR;
2752 case Cbinop_Mul: return wL * wR;
2753 case Cbinop_Shl: return wL << wR;
2754 case Cbinop_Shr: return wL >> wR;
2755 case Cbinop_Eq: return wL == wR ? 1 : 0;
2756 case Cbinop_Ge: return (Word) wL >= (Word) wR ? 1 : 0;
2757 case Cbinop_Gt: return (Word) wL > (Word) wR ? 1 : 0;
2758 case Cbinop_Le: return (Word) wL <= (Word) wR ? 1 : 0;
2759 case Cbinop_Lt: return (Word) wL < (Word) wR ? 1 : 0;
2760 case Cbinop_Ne: return wL != wR ? 1 : 0;
2761 default: goto unhandled;
2763 /*NOTREACHED*/
2764 case Cex_CfiReg:
2765 switch (e->Cex.CfiReg.reg) {
2766 # if defined(VGA_x86) || defined(VGA_amd64)
2767 case Creg_IA_IP: return eec->uregs->xip;
2768 case Creg_IA_SP: return eec->uregs->xsp;
2769 case Creg_IA_BP: return eec->uregs->xbp;
2770 # elif defined(VGA_arm)
2771 case Creg_ARM_R15: return eec->uregs->r15;
2772 case Creg_ARM_R14: return eec->uregs->r14;
2773 case Creg_ARM_R13: return eec->uregs->r13;
2774 case Creg_ARM_R12: return eec->uregs->r12;
2775 case Creg_ARM_R7: return eec->uregs->r7;
2776 # elif defined(VGA_s390x)
2777 case Creg_S390_IA: return eec->uregs->ia;
2778 case Creg_S390_SP: return eec->uregs->sp;
2779 case Creg_S390_FP: return eec->uregs->fp;
2780 case Creg_S390_LR: return eec->uregs->lr;
2781 # elif defined(VGA_mips32) || defined(VGA_mips64)
2782 case Creg_IA_IP: return eec->uregs->pc;
2783 case Creg_IA_SP: return eec->uregs->sp;
2784 case Creg_IA_BP: return eec->uregs->fp;
2785 case Creg_MIPS_RA: return eec->uregs->ra;
2786 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) \
2787 || defined(VGA_ppc64le)
2788 # elif defined(VGP_arm64_linux)
2789 case Creg_ARM64_X30: return eec->uregs->x30;
2790 # else
2791 # error "Unsupported arch"
2792 # endif
2793 default: goto unhandled;
2795 /*NOTREACHED*/
2796 case Cex_Const:
2797 return e->Cex.Const.con;
2798 case Cex_Deref:
2799 a = evalCfiExpr( exprs, e->Cex.Deref.ixAddr, eec, ok );
2800 if (!(*ok)) return 0;
2801 if (a < eec->min_accessible
2802 || a > eec->max_accessible - sizeof(UWord) + 1) {
2803 *ok = False;
2804 return 0;
2806 /* let's hope it doesn't trap! */
2807 return ML_(read_UWord)((void *)a);
2808 default:
2809 goto unhandled;
2811 /*NOTREACHED*/
2812 unhandled:
2813 VG_(printf)("\n\nevalCfiExpr: unhandled\n");
2814 ML_(ppCfiExpr)( exprs, ix );
2815 VG_(printf)("\n");
2816 vg_assert(0);
2817 /*NOTREACHED*/
2818 return 0;
2822 /* Search all the DebugInfos in the entire system, to find the DiCfSI_m
2823 that pertains to 'ip'.
2825 If found, set *diP to the DebugInfo in which it resides, and
2826 *cfsi_mP to the cfsi_m pointer in that DebugInfo's cfsi_m_pool.
2828 If not found, set *diP to (DebugInfo*)1 and *cfsi_mP to zero.
2830 Per comments at the top of this section, we only look for CFI in
2831 DebugInfos that are valid for the current epoch.
2833 __attribute__((noinline))
2834 static void find_DiCfSI ( /*OUT*/DebugInfo** diP,
2835 /*OUT*/DiCfSI_m** cfsi_mP,
2836 Addr ip )
2838 DebugInfo* di;
2839 Word i = -1;
2841 static UWord n_search = 0;
2842 static UWord n_steps = 0;
2843 n_search++;
2845 if (0) VG_(printf)("search for %#lx\n", ip);
2847 DiEpoch curr_epoch = VG_(current_DiEpoch)();
2849 for (di = debugInfo_list; di != NULL; di = di->next) {
2850 Word j;
2851 n_steps++;
2853 if (!is_DI_valid_for_epoch(di, curr_epoch))
2854 continue;
2856 /* Use the per-DebugInfo summary address ranges to skip
2857 inapplicable DebugInfos quickly. */
2858 if (di->cfsi_used == 0)
2859 continue;
2860 if (ip < di->cfsi_minavma || ip > di->cfsi_maxavma)
2861 continue;
2863 // This di must be active (because we have explicitly chosen not to
2864 // allow unwinding stacks that pertain to some past epoch). It can't
2865 // be archived or not-yet-active.
2866 vg_assert(is_DebugInfo_active(di));
2868 /* It might be in this DebugInfo. Search it. */
2869 j = ML_(search_one_cfitab)( di, ip );
2870 vg_assert(j >= -1 && j < (Word)di->cfsi_used);
2872 if (j != -1) {
2873 i = j;
2874 break; /* found it */
2878 if (i == -1) {
2880 /* we didn't find it. */
2881 *diP = (DebugInfo*)1;
2882 *cfsi_mP = 0;
2884 } else {
2886 /* found a di corresponding to ip. */
2887 /* ensure that di is 4-aligned (at least), so it can't possibly
2888 be equal to (DebugInfo*)1. */
2889 vg_assert(di && VG_IS_4_ALIGNED(di));
2890 *cfsi_mP = ML_(get_cfsi_m) (di, i);
2891 if (*cfsi_mP == NULL) {
2892 // This is a cfsi hole. Report no cfi information found.
2893 *diP = (DebugInfo*)1;
2894 // But we will still perform the hack below.
2895 } else {
2896 *diP = di;
2899 /* Start of performance-enhancing hack: once every 64 (chosen
2900 hackily after profiling) successful searches, move the found
2901 DebugInfo one step closer to the start of the list. This
2902 makes future searches cheaper. For starting konqueror on
2903 amd64, this in fact reduces the total amount of searching
2904 done by the above find-the-right-DebugInfo loop by more than
2905 a factor of 20. */
2906 if ((n_search & 0xF) == 0) {
2907 /* Move di one step closer to the start of the list. */
2908 move_DebugInfo_one_step_forward( di );
2910 /* End of performance-enhancing hack. */
2912 if (0 && ((n_search & 0x7FFFF) == 0))
2913 VG_(printf)("find_DiCfSI: %lu searches, "
2914 "%lu DebugInfos looked at\n",
2915 n_search, n_steps);
2922 /* Now follows a mechanism for caching queries to find_DiCfSI, since
2923 they are extremely frequent on amd64-linux, during stack unwinding.
2925 Each cache entry binds an ip value to a (di, cfsi_m*) pair. Possible
2926 values:
2928 di is non-null, cfsi_m* >= 0 ==> cache slot in use, "cfsi_m*"
2929 di is (DebugInfo*)1 ==> cache slot in use, no associated di
2930 di is NULL ==> cache slot not in use
2932 Hence simply zeroing out the entire cache invalidates all
2933 entries.
2935 We can map an ip value directly to a (di, cfsi_m*) pair as
2936 once a DebugInfo is read, adding new DiCfSI_m* is not possible
2937 anymore, as the cfsi_m_pool is frozen once the reading is terminated.
2938 Also, the cache is invalidated when new debuginfo is read due to
2939 an mmap or some debuginfo is discarded due to an munmap. */
2941 // Prime number, giving about 6Kbytes cache on 32 bits,
2942 // 12Kbytes cache on 64 bits.
2943 #define N_CFSI_M_CACHE 509
2945 typedef
2946 struct { Addr ip; DebugInfo* di; DiCfSI_m* cfsi_m; }
2947 CFSI_m_CacheEnt;
2949 static CFSI_m_CacheEnt cfsi_m_cache[N_CFSI_M_CACHE];
2951 static void cfsi_m_cache__invalidate ( void ) {
2952 VG_(memset)(&cfsi_m_cache, 0, sizeof(cfsi_m_cache));
2955 static inline CFSI_m_CacheEnt* cfsi_m_cache__find ( Addr ip )
2957 UWord hash = ip % N_CFSI_M_CACHE;
2958 CFSI_m_CacheEnt* ce = &cfsi_m_cache[hash];
2959 # ifdef N_Q_M_STATS
2960 static UWord n_q = 0, n_m = 0;
2961 n_q++;
2962 if (0 == (n_q & 0x1FFFFF))
2963 VG_(printf)("QQQ %lu %lu\n", n_q, n_m);
2964 # endif
2966 if (LIKELY(ce->ip == ip) && LIKELY(ce->di != NULL)) {
2967 /* found an entry in the cache .. */
2968 } else {
2969 /* not found in cache. Search and update. */
2970 # ifdef N_Q_M_STATS
2971 n_m++;
2972 # endif
2973 ce->ip = ip;
2974 find_DiCfSI( &ce->di, &ce->cfsi_m, ip );
2977 if (UNLIKELY(ce->di == (DebugInfo*)1)) {
2978 /* no DiCfSI for this address */
2979 return NULL;
2980 } else {
2981 /* found a DiCfSI for this address */
2982 return ce;
2986 Bool VG_(has_CF_info)(Addr a)
2988 return cfsi_m_cache__find (a) != NULL;
2993 inline
2994 static Addr compute_cfa ( const D3UnwindRegs* uregs,
2995 Addr min_accessible, Addr max_accessible,
2996 const DebugInfo* di, const DiCfSI_m* cfsi_m )
2998 CfiExprEvalContext eec;
2999 Addr cfa;
3000 Bool ok;
3002 /* Compute the CFA. */
3003 cfa = 0;
3004 switch (cfsi_m->cfa_how) {
3005 # if defined(VGA_x86) || defined(VGA_amd64)
3006 case CFIC_IA_SPREL:
3007 cfa = cfsi_m->cfa_off + uregs->xsp;
3008 break;
3009 case CFIC_IA_BPREL:
3010 cfa = cfsi_m->cfa_off + uregs->xbp;
3011 break;
3012 # elif defined(VGA_arm)
3013 case CFIC_ARM_R13REL:
3014 cfa = cfsi_m->cfa_off + uregs->r13;
3015 break;
3016 case CFIC_ARM_R12REL:
3017 cfa = cfsi_m->cfa_off + uregs->r12;
3018 break;
3019 case CFIC_ARM_R11REL:
3020 cfa = cfsi_m->cfa_off + uregs->r11;
3021 break;
3022 case CFIC_ARM_R7REL:
3023 cfa = cfsi_m->cfa_off + uregs->r7;
3024 break;
3025 # elif defined(VGA_s390x)
3026 case CFIC_IA_SPREL:
3027 cfa = cfsi_m->cfa_off + uregs->sp;
3028 break;
3029 case CFIR_MEMCFAREL:
3031 Addr a = uregs->sp + cfsi_m->cfa_off;
3032 if (a < min_accessible || a > max_accessible-sizeof(Addr))
3033 break;
3034 cfa = ML_(read_Addr)((void *)a);
3035 break;
3037 case CFIR_SAME:
3038 cfa = uregs->fp;
3039 break;
3040 case CFIC_IA_BPREL:
3041 cfa = cfsi_m->cfa_off + uregs->fp;
3042 break;
3043 # elif defined(VGA_mips32) || defined(VGA_mips64)
3044 case CFIC_IA_SPREL:
3045 cfa = cfsi_m->cfa_off + uregs->sp;
3046 break;
3047 case CFIR_SAME:
3048 cfa = uregs->fp;
3049 break;
3050 case CFIC_IA_BPREL:
3051 cfa = cfsi_m->cfa_off + uregs->fp;
3052 break;
3053 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3054 # elif defined(VGP_arm64_linux)
3055 case CFIC_ARM64_SPREL:
3056 cfa = cfsi_m->cfa_off + uregs->sp;
3057 break;
3058 case CFIC_ARM64_X29REL:
3059 cfa = cfsi_m->cfa_off + uregs->x29;
3060 break;
3061 # else
3062 # error "Unsupported arch"
3063 # endif
3064 case CFIC_EXPR: /* available on all archs */
3065 if (0) {
3066 VG_(printf)("CFIC_EXPR: ");
3067 ML_(ppCfiExpr)(di->cfsi_exprs, cfsi_m->cfa_off);
3068 VG_(printf)("\n");
3070 eec.uregs = uregs;
3071 eec.min_accessible = min_accessible;
3072 eec.max_accessible = max_accessible;
3073 ok = True;
3074 cfa = evalCfiExpr(di->cfsi_exprs, cfsi_m->cfa_off, &eec, &ok );
3075 if (!ok) return 0;
3076 break;
3077 default:
3078 vg_assert(0);
3080 return cfa;
3084 /* Get the call frame address (CFA) given an IP/SP/FP triple. */
3085 /* NOTE: This function may rearrange the order of entries in the
3086 DebugInfo list. */
3087 Addr ML_(get_CFA) ( Addr ip, Addr sp, Addr fp,
3088 Addr min_accessible, Addr max_accessible )
3090 CFSI_m_CacheEnt* ce;
3092 ce = cfsi_m_cache__find(ip);
3094 if (UNLIKELY(ce == NULL))
3095 return 0; /* no info. Nothing we can do. */
3097 /* Temporary impedance-matching kludge so that this keeps working
3098 on x86-linux and amd64-linux. */
3099 # if defined(VGA_x86) || defined(VGA_amd64)
3100 { D3UnwindRegs uregs;
3101 uregs.xip = ip;
3102 uregs.xsp = sp;
3103 uregs.xbp = fp;
3104 return compute_cfa(&uregs,
3105 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3107 #elif defined(VGA_s390x)
3108 { D3UnwindRegs uregs;
3109 uregs.ia = ip;
3110 uregs.sp = sp;
3111 uregs.fp = fp;
3112 return compute_cfa(&uregs,
3113 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3115 #elif defined(VGA_mips32) || defined(VGA_mips64)
3116 { D3UnwindRegs uregs;
3117 uregs.pc = ip;
3118 uregs.sp = sp;
3119 uregs.fp = fp;
3120 return compute_cfa(&uregs,
3121 min_accessible, max_accessible, ce->di, ce->cfsi_m);
3124 # else
3125 return 0; /* indicates failure */
3126 # endif
3129 void VG_(ppUnwindInfo) (Addr from, Addr to)
3131 DebugInfo* di;
3132 CFSI_m_CacheEnt* ce;
3133 Addr ce_from;
3134 CFSI_m_CacheEnt* next_ce;
3137 ce = cfsi_m_cache__find(from);
3138 ce_from = from;
3139 while (from <= to) {
3140 from++;
3141 next_ce = cfsi_m_cache__find(from);
3142 if ((ce == NULL && next_ce != NULL)
3143 || (ce != NULL && next_ce == NULL)
3144 || (ce != NULL && next_ce != NULL && ce->cfsi_m != next_ce->cfsi_m)
3145 || from > to) {
3146 if (ce == NULL) {
3147 VG_(printf)("[%#lx .. %#lx]: no CFI info\n", ce_from, from-1);
3148 } else {
3149 di = ce->di;
3150 ML_(ppDiCfSI)(di->cfsi_exprs,
3151 ce_from, from - ce_from,
3152 ce->cfsi_m);
3154 ce = next_ce;
3155 ce_from = from;
3161 /* The main function for DWARF2/3 CFI-based stack unwinding. Given a
3162 set of registers in UREGS, modify it to hold the register values
3163 for the previous frame, if possible. Returns True if successful.
3164 If not successful, *UREGS is not changed.
3166 For x86 and amd64, the unwound registers are: {E,R}IP,
3167 {E,R}SP, {E,R}BP.
3169 For arm, the unwound registers are: R7 R11 R12 R13 R14 R15.
3171 For arm64, the unwound registers are: X29(FP) X30(LR) SP PC.
3173 Bool VG_(use_CF_info) ( /*MOD*/D3UnwindRegs* uregsHere,
3174 Addr min_accessible,
3175 Addr max_accessible )
3177 DebugInfo* di;
3178 DiCfSI_m* cfsi_m = NULL;
3179 Addr cfa, ipHere = 0;
3180 CFSI_m_CacheEnt* ce;
3181 CfiExprEvalContext eec __attribute__((unused));
3182 D3UnwindRegs uregsPrev;
3184 # if defined(VGA_x86) || defined(VGA_amd64)
3185 ipHere = uregsHere->xip;
3186 # elif defined(VGA_arm)
3187 ipHere = uregsHere->r15;
3188 # elif defined(VGA_s390x)
3189 ipHere = uregsHere->ia;
3190 # elif defined(VGA_mips32) || defined(VGA_mips64)
3191 ipHere = uregsHere->pc;
3192 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3193 # elif defined(VGP_arm64_linux)
3194 ipHere = uregsHere->pc;
3195 # else
3196 # error "Unknown arch"
3197 # endif
3198 ce = cfsi_m_cache__find(ipHere);
3200 if (UNLIKELY(ce == NULL))
3201 return False; /* no info. Nothing we can do. */
3203 di = ce->di;
3204 cfsi_m = ce->cfsi_m;
3206 if (0) {
3207 VG_(printf)("found cfsi_m (but printing fake base/len): ");
3208 ML_(ppDiCfSI)(di->cfsi_exprs, 0, 0, cfsi_m);
3211 VG_(bzero_inline)(&uregsPrev, sizeof(uregsPrev));
3213 /* First compute the CFA. */
3214 cfa = compute_cfa(uregsHere,
3215 min_accessible, max_accessible, di, cfsi_m);
3216 if (UNLIKELY(cfa == 0))
3217 return False;
3219 /* Now we know the CFA, use it to roll back the registers we're
3220 interested in. */
3222 # define COMPUTE(_prev, _here, _how, _off) \
3223 do { \
3224 switch (_how) { \
3225 case CFIR_UNKNOWN: \
3226 return False; \
3227 case CFIR_SAME: \
3228 _prev = _here; break; \
3229 case CFIR_MEMCFAREL: { \
3230 Addr a = cfa + (Word)_off; \
3231 if (a < min_accessible \
3232 || a > max_accessible-sizeof(Addr)) \
3233 return False; \
3234 _prev = ML_(read_Addr)((void *)a); \
3235 break; \
3237 case CFIR_CFAREL: \
3238 _prev = cfa + (Word)_off; \
3239 break; \
3240 case CFIR_EXPR: \
3241 if (0) \
3242 ML_(ppCfiExpr)(di->cfsi_exprs,_off); \
3243 eec.uregs = uregsHere; \
3244 eec.min_accessible = min_accessible; \
3245 eec.max_accessible = max_accessible; \
3246 Bool ok = True; \
3247 _prev = evalCfiExpr(di->cfsi_exprs, _off, &eec, &ok ); \
3248 if (!ok) return False; \
3249 break; \
3250 default: \
3251 vg_assert(0); \
3253 } while (0)
3255 # if defined(VGA_x86) || defined(VGA_amd64)
3256 COMPUTE(uregsPrev.xip, uregsHere->xip, cfsi_m->ra_how, cfsi_m->ra_off);
3257 COMPUTE(uregsPrev.xsp, uregsHere->xsp, cfsi_m->sp_how, cfsi_m->sp_off);
3258 COMPUTE(uregsPrev.xbp, uregsHere->xbp, cfsi_m->bp_how, cfsi_m->bp_off);
3259 # elif defined(VGA_arm)
3260 COMPUTE(uregsPrev.r15, uregsHere->r15, cfsi_m->ra_how, cfsi_m->ra_off);
3261 COMPUTE(uregsPrev.r14, uregsHere->r14, cfsi_m->r14_how, cfsi_m->r14_off);
3262 COMPUTE(uregsPrev.r13, uregsHere->r13, cfsi_m->r13_how, cfsi_m->r13_off);
3263 COMPUTE(uregsPrev.r12, uregsHere->r12, cfsi_m->r12_how, cfsi_m->r12_off);
3264 COMPUTE(uregsPrev.r11, uregsHere->r11, cfsi_m->r11_how, cfsi_m->r11_off);
3265 COMPUTE(uregsPrev.r7, uregsHere->r7, cfsi_m->r7_how, cfsi_m->r7_off);
3266 # elif defined(VGA_s390x)
3267 COMPUTE(uregsPrev.ia, uregsHere->ia, cfsi_m->ra_how, cfsi_m->ra_off);
3268 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3269 COMPUTE(uregsPrev.fp, uregsHere->fp, cfsi_m->fp_how, cfsi_m->fp_off);
3270 # elif defined(VGA_mips32) || defined(VGA_mips64)
3271 COMPUTE(uregsPrev.pc, uregsHere->pc, cfsi_m->ra_how, cfsi_m->ra_off);
3272 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3273 COMPUTE(uregsPrev.fp, uregsHere->fp, cfsi_m->fp_how, cfsi_m->fp_off);
3274 # elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
3275 # elif defined(VGP_arm64_linux)
3276 COMPUTE(uregsPrev.pc, uregsHere->pc, cfsi_m->ra_how, cfsi_m->ra_off);
3277 COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi_m->sp_how, cfsi_m->sp_off);
3278 COMPUTE(uregsPrev.x30, uregsHere->x30, cfsi_m->x30_how, cfsi_m->x30_off);
3279 COMPUTE(uregsPrev.x29, uregsHere->x29, cfsi_m->x29_how, cfsi_m->x29_off);
3280 # else
3281 # error "Unknown arch"
3282 # endif
3284 # undef COMPUTE
3286 *uregsHere = uregsPrev;
3287 return True;
3291 /*--------------------------------------------------------------*/
3292 /*--- ---*/
3293 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
3294 /*--- MSVC FPO INFO ---*/
3295 /*--- ---*/
3296 /*--------------------------------------------------------------*/
3298 Bool VG_(use_FPO_info) ( /*MOD*/Addr* ipP,
3299 /*MOD*/Addr* spP,
3300 /*MOD*/Addr* fpP,
3301 DiEpoch ep,
3302 Addr min_accessible,
3303 Addr max_accessible )
3305 Word i;
3306 const DebugInfo* di;
3307 FPO_DATA* fpo = NULL;
3308 Addr spHere;
3310 static UWord n_search = 0;
3311 static UWord n_steps = 0;
3312 n_search++;
3314 if (0) VG_(printf)("search FPO for %#lx\n", *ipP);
3316 for (di = debugInfo_list; di != NULL; di = di->next) {
3317 n_steps++;
3319 if (!is_DI_valid_for_epoch(di, ep))
3320 continue;
3322 /* Use the per-DebugInfo summary address ranges to skip
3323 inapplicable DebugInfos quickly. */
3324 if (di->fpo == NULL)
3325 continue;
3326 if (*ipP < di->fpo_minavma || *ipP > di->fpo_maxavma)
3327 continue;
3329 i = ML_(search_one_fpotab)( di, *ipP );
3330 if (i != -1) {
3331 Word j;
3332 if (0) {
3333 /* debug printing only */
3334 VG_(printf)("look for %#lx size %lu i %ld\n",
3335 *ipP, di->fpo_size, i);
3336 for (j = 0; j < di->fpo_size; j++)
3337 VG_(printf)("[%02ld] %#x %u\n",
3338 j, di->fpo[j].ulOffStart, di->fpo[j].cbProcSize);
3340 vg_assert(i >= 0 && i < di->fpo_size);
3341 fpo = &di->fpo[i];
3342 break;
3346 if (fpo == NULL)
3347 return False;
3349 if (0 && ((n_search & 0x7FFFF) == 0))
3350 VG_(printf)("VG_(use_FPO_info): %lu searches, "
3351 "%lu DebugInfos looked at\n",
3352 n_search, n_steps);
3355 /* Start of performance-enhancing hack: once every 64 (chosen
3356 hackily after profiling) successful searches, move the found
3357 DebugInfo one step closer to the start of the list. This makes
3358 future searches cheaper. For starting konqueror on amd64, this
3359 in fact reduces the total amount of searching done by the above
3360 find-the-right-DebugInfo loop by more than a factor of 20. */
3361 if ((n_search & 0x3F) == 0) {
3362 /* Move si one step closer to the start of the list. */
3363 //move_DebugInfo_one_step_forward( di );
3365 /* End of performance-enhancing hack. */
3367 if (0) {
3368 VG_(printf)("found fpo: ");
3369 //ML_(ppFPO)(fpo);
3373 Stack layout is:
3374 %esp->
3375 4*.cbRegs {%edi, %esi, %ebp, %ebx}
3376 4*.cdwLocals
3377 return_pc
3378 4*.cdwParams
3379 prior_%esp->
3381 Typical code looks like:
3382 sub $4*.cdwLocals,%esp
3383 Alternative to above for >=4KB (and sometimes for smaller):
3384 mov $size,%eax
3385 call __chkstk # WinNT performs page-by-page probe!
3386 __chkstk is much like alloc(), except that on return
3387 %eax= 5+ &CALL. Thus it could be used as part of
3388 Position Independent Code to locate the Global Offset Table.
3389 push %ebx
3390 push %ebp
3391 push %esi
3392 Other once-only instructions often scheduled >here<.
3393 push %edi
3395 If the pc is within the first .cbProlog bytes of the function,
3396 then you must disassemble to see how many registers have been pushed,
3397 because instructions in the prolog may be scheduled for performance.
3398 The order of PUSH is always %ebx, %ebp, %esi, %edi, with trailing
3399 registers not pushed when .cbRegs < 4. This seems somewhat strange
3400 because %ebp is the register whose usage you want to minimize,
3401 yet it is in the first half of the PUSH list.
3403 I don't know what happens when the compiler constructs an outgoing CALL.
3404 %esp could move if outgoing parameters are PUSHed, and this affects
3405 traceback for errors during the PUSHes. */
3407 spHere = *spP;
3409 *ipP = ML_(read_Addr)((void *)(spHere + 4*(fpo->cbRegs + fpo->cdwLocals)));
3410 *spP = spHere + 4*(fpo->cbRegs + fpo->cdwLocals + 1
3411 + fpo->cdwParams);
3412 *fpP = ML_(read_Addr)((void *)(spHere + 4*2));
3413 return True;
3416 Bool VG_(FPO_info_present)(void)
3418 const DebugInfo* di;
3419 for (di = debugInfo_list; di != NULL; di = di->next) {
3420 if (di->fpo != NULL)
3421 return True;
3423 return False;
3427 /*--------------------------------------------------------------*/
3428 /*--- ---*/
3429 /*--- TOP LEVEL: GENERATE DESCRIPTION OF DATA ADDRESSES ---*/
3430 /*--- FROM DWARF3 DEBUG INFO ---*/
3431 /*--- ---*/
3432 /*--------------------------------------------------------------*/
3434 /* Try to make p2XA(dst, fmt, args..) turn into
3435 VG_(xaprintf)(dst, fmt, args) without having to resort to
3436 vararg macros. As usual with everything to do with varargs, it's
3437 an ugly hack.
3439 //#define p2XA(dstxa, format, args...)
3440 // VG_(xaprintf)(dstxa, format, ##args)
3442 #define p2XA VG_(xaprintf)
3444 /* Add a zero-terminating byte to DST, which must be an XArray* of
3445 HChar. */
3446 static void zterm_XA ( XArray* dst )
3448 HChar zero = 0;
3449 (void) VG_(addBytesToXA)( dst, &zero, 1 );
3453 /* Evaluate the location expression/list for var, to see whether or
3454 not data_addr falls within the variable. If so also return the
3455 offset of data_addr from the start of the variable. Note that
3456 regs, which supplies ip,sp,fp values, will be NULL for global
3457 variables, and non-NULL for local variables. */
3458 static Bool data_address_is_in_var ( /*OUT*/PtrdiffT* offset,
3459 const XArray* /* TyEnt */ tyents,
3460 const DiVariable* var,
3461 const RegSummary* regs,
3462 Addr data_addr,
3463 const DebugInfo* di )
3465 MaybeULong mul;
3466 SizeT var_szB;
3467 GXResult res;
3468 Bool show = False;
3470 vg_assert(var->name);
3471 vg_assert(var->gexpr);
3473 /* Figure out how big the variable is. */
3474 mul = ML_(sizeOfType)(tyents, var->typeR);
3475 /* If this var has a type whose size is unknown, zero, or
3476 impossibly large, it should never have been added. ML_(addVar)
3477 should have rejected it. */
3478 vg_assert(mul.b == True);
3479 vg_assert(mul.ul > 0);
3480 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
3481 /* After this point, we assume we can truncate mul.ul to a host word
3482 safely (without loss of info). */
3484 var_szB = (SizeT)mul.ul; /* NB: truncate to host word */
3486 if (show) {
3487 VG_(printf)("VVVV: data_address_%#lx_is_in_var: %s :: ",
3488 data_addr, var->name );
3489 ML_(pp_TyEnt_C_ishly)( tyents, var->typeR );
3490 VG_(printf)("\n");
3493 /* ignore zero-sized vars; they can never match anything. */
3494 if (var_szB == 0) {
3495 if (show)
3496 VG_(printf)("VVVV: -> Fail (variable is zero sized)\n");
3497 return False;
3500 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, regs, di );
3502 if (show) {
3503 VG_(printf)("VVVV: -> ");
3504 ML_(pp_GXResult)( res );
3505 VG_(printf)("\n");
3508 if (res.kind == GXR_Addr
3509 && res.word <= data_addr
3510 && data_addr < res.word + var_szB) {
3511 *offset = data_addr - res.word;
3512 return True;
3513 } else {
3514 return False;
3519 /* Format the acquired information into DN(AME)1 and DN(AME)2, which
3520 are XArray*s of HChar, that have been initialised by the caller.
3521 Resulting strings will be zero terminated. Information is
3522 formatted in an understandable way. Not so easy. If frameNo is
3523 -1, this is assumed to be a global variable; else a local
3524 variable. */
3525 static void format_message ( /*MOD*/XArray* /* of HChar */ dn1,
3526 /*MOD*/XArray* /* of HChar */ dn2,
3527 Addr data_addr,
3528 const DebugInfo* di,
3529 const DiVariable* var,
3530 PtrdiffT var_offset,
3531 PtrdiffT residual_offset,
3532 const XArray* /*HChar*/ described,
3533 Int frameNo,
3534 ThreadId tid )
3536 Bool have_descr, have_srcloc;
3537 Bool xml = VG_(clo_xml);
3538 const HChar* vo_plural = var_offset == 1 ? "" : "s";
3539 const HChar* ro_plural = residual_offset == 1 ? "" : "s";
3540 const HChar* basetag = "auxwhat"; /* a constant */
3541 HChar tagL[32], tagR[32], xagL[32], xagR[32];
3542 const HChar *fileName = ML_(fndn_ix2filename)(di, var->fndn_ix);
3543 // fileName will be "???" if var->fndn_ix == 0.
3544 // fileName will only be used if have_descr is True.
3546 if (frameNo < -1) {
3547 vg_assert(0); /* Not allowed */
3549 else if (frameNo == -1) {
3550 vg_assert(tid == VG_INVALID_THREADID);
3552 else /* (frameNo >= 0) */ {
3553 vg_assert(tid != VG_INVALID_THREADID);
3556 vg_assert(dn1 && dn2);
3557 vg_assert(described);
3558 vg_assert(var && var->name);
3559 have_descr = VG_(sizeXA)(described) > 0
3560 && *(HChar*)VG_(indexXA)(described,0) != '\0';
3561 have_srcloc = var->fndn_ix > 0 && var->lineNo > 0;
3563 tagL[0] = tagR[0] = xagL[0] = xagR[0] = 0;
3564 if (xml) {
3565 VG_(sprintf)(tagL, "<%s>", basetag); // <auxwhat>
3566 VG_(sprintf)(tagR, "</%s>", basetag); // </auxwhat>
3567 VG_(sprintf)(xagL, "<x%s>", basetag); // <xauxwhat>
3568 VG_(sprintf)(xagR, "</x%s>", basetag); // </xauxwhat>
3571 # define TAGL(_xa) p2XA(_xa, "%s", tagL)
3572 # define TAGR(_xa) p2XA(_xa, "%s", tagR)
3573 # define XAGL(_xa) p2XA(_xa, "%s", xagL)
3574 # define XAGR(_xa) p2XA(_xa, "%s", xagR)
3575 # define TXTL(_xa) p2XA(_xa, "%s", "<text>")
3576 # define TXTR(_xa) p2XA(_xa, "%s", "</text>")
3578 /* ------ local cases ------ */
3580 if ( frameNo >= 0 && (!have_srcloc) && (!have_descr) ) {
3581 /* no srcloc, no description:
3582 Location 0x7fefff6cf is 543 bytes inside local var "a",
3583 in frame #1 of thread 1
3585 if (xml) {
3586 TAGL( dn1 );
3587 p2XA( dn1,
3588 "Location 0x%lx is %ld byte%s inside local var \"%pS\",",
3589 data_addr, var_offset, vo_plural, var->name );
3590 TAGR( dn1 );
3591 TAGL( dn2 );
3592 p2XA( dn2,
3593 "in frame #%d of thread %u", frameNo, tid );
3594 TAGR( dn2 );
3595 } else {
3596 p2XA( dn1,
3597 "Location 0x%lx is %ld byte%s inside local var \"%s\",",
3598 data_addr, var_offset, vo_plural, var->name );
3599 p2XA( dn2,
3600 "in frame #%d of thread %u", frameNo, tid );
3603 else
3604 if ( frameNo >= 0 && have_srcloc && (!have_descr) ) {
3605 /* no description:
3606 Location 0x7fefff6cf is 543 bytes inside local var "a"
3607 declared at dsyms7.c:17, in frame #1 of thread 1
3609 if (xml) {
3610 TAGL( dn1 );
3611 p2XA( dn1,
3612 "Location 0x%lx is %ld byte%s inside local var \"%pS\"",
3613 data_addr, var_offset, vo_plural, var->name );
3614 TAGR( dn1 );
3615 XAGL( dn2 );
3616 TXTL( dn2 );
3617 p2XA( dn2,
3618 "declared at %pS:%d, in frame #%d of thread %u",
3619 fileName, var->lineNo, frameNo, tid );
3620 TXTR( dn2 );
3621 // FIXME: also do <dir>
3622 p2XA( dn2,
3623 " <file>%pS</file> <line>%d</line> ",
3624 fileName, var->lineNo );
3625 XAGR( dn2 );
3626 } else {
3627 p2XA( dn1,
3628 "Location 0x%lx is %ld byte%s inside local var \"%s\"",
3629 data_addr, var_offset, vo_plural, var->name );
3630 p2XA( dn2,
3631 "declared at %s:%d, in frame #%d of thread %u",
3632 fileName, var->lineNo, frameNo, tid );
3635 else
3636 if ( frameNo >= 0 && (!have_srcloc) && have_descr ) {
3637 /* no srcloc:
3638 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2
3639 in frame #1 of thread 1
3641 if (xml) {
3642 TAGL( dn1 );
3643 p2XA( dn1,
3644 "Location 0x%lx is %ld byte%s inside %pS%pS",
3645 data_addr, residual_offset, ro_plural, var->name,
3646 (HChar*)(VG_(indexXA)(described,0)) );
3647 TAGR( dn1 );
3648 TAGL( dn2 );
3649 p2XA( dn2,
3650 "in frame #%d of thread %u", frameNo, tid );
3651 TAGR( dn2 );
3652 } else {
3653 p2XA( dn1,
3654 "Location 0x%lx is %ld byte%s inside %s%s",
3655 data_addr, residual_offset, ro_plural, var->name,
3656 (HChar*)(VG_(indexXA)(described,0)) );
3657 p2XA( dn2,
3658 "in frame #%d of thread %u", frameNo, tid );
3661 else
3662 if ( frameNo >= 0 && have_srcloc && have_descr ) {
3663 /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
3664 declared at dsyms7.c:17, in frame #1 of thread 1 */
3665 if (xml) {
3666 TAGL( dn1 );
3667 p2XA( dn1,
3668 "Location 0x%lx is %ld byte%s inside %pS%pS,",
3669 data_addr, residual_offset, ro_plural, var->name,
3670 (HChar*)(VG_(indexXA)(described,0)) );
3671 TAGR( dn1 );
3672 XAGL( dn2 );
3673 TXTL( dn2 );
3674 p2XA( dn2,
3675 "declared at %pS:%d, in frame #%d of thread %u",
3676 fileName, var->lineNo, frameNo, tid );
3677 TXTR( dn2 );
3678 // FIXME: also do <dir>
3679 p2XA( dn2,
3680 " <file>%pS</file> <line>%d</line> ",
3681 fileName, var->lineNo );
3682 XAGR( dn2 );
3683 } else {
3684 p2XA( dn1,
3685 "Location 0x%lx is %ld byte%s inside %s%s,",
3686 data_addr, residual_offset, ro_plural, var->name,
3687 (HChar*)(VG_(indexXA)(described,0)) );
3688 p2XA( dn2,
3689 "declared at %s:%d, in frame #%d of thread %u",
3690 fileName, var->lineNo, frameNo, tid );
3693 else
3694 /* ------ global cases ------ */
3695 if ( frameNo >= -1 && (!have_srcloc) && (!have_descr) ) {
3696 /* no srcloc, no description:
3697 Location 0x7fefff6cf is 543 bytes inside global var "a"
3699 if (xml) {
3700 TAGL( dn1 );
3701 p2XA( dn1,
3702 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
3703 data_addr, var_offset, vo_plural, var->name );
3704 TAGR( dn1 );
3705 } else {
3706 p2XA( dn1,
3707 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
3708 data_addr, var_offset, vo_plural, var->name );
3711 else
3712 if ( frameNo >= -1 && have_srcloc && (!have_descr) ) {
3713 /* no description:
3714 Location 0x7fefff6cf is 543 bytes inside global var "a"
3715 declared at dsyms7.c:17
3717 if (xml) {
3718 TAGL( dn1 );
3719 p2XA( dn1,
3720 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
3721 data_addr, var_offset, vo_plural, var->name );
3722 TAGR( dn1 );
3723 XAGL( dn2 );
3724 TXTL( dn2 );
3725 p2XA( dn2,
3726 "declared at %pS:%d",
3727 fileName, var->lineNo);
3728 TXTR( dn2 );
3729 // FIXME: also do <dir>
3730 p2XA( dn2,
3731 " <file>%pS</file> <line>%d</line> ",
3732 fileName, var->lineNo );
3733 XAGR( dn2 );
3734 } else {
3735 p2XA( dn1,
3736 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
3737 data_addr, var_offset, vo_plural, var->name );
3738 p2XA( dn2,
3739 "declared at %s:%d",
3740 fileName, var->lineNo);
3743 else
3744 if ( frameNo >= -1 && (!have_srcloc) && have_descr ) {
3745 /* no srcloc:
3746 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
3747 a global variable
3749 if (xml) {
3750 TAGL( dn1 );
3751 p2XA( dn1,
3752 "Location 0x%lx is %ld byte%s inside %pS%pS,",
3753 data_addr, residual_offset, ro_plural, var->name,
3754 (HChar*)(VG_(indexXA)(described,0)) );
3755 TAGR( dn1 );
3756 TAGL( dn2 );
3757 p2XA( dn2,
3758 "a global variable");
3759 TAGR( dn2 );
3760 } else {
3761 p2XA( dn1,
3762 "Location 0x%lx is %ld byte%s inside %s%s,",
3763 data_addr, residual_offset, ro_plural, var->name,
3764 (HChar*)(VG_(indexXA)(described,0)) );
3765 p2XA( dn2,
3766 "a global variable");
3769 else
3770 if ( frameNo >= -1 && have_srcloc && have_descr ) {
3771 /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
3772 a global variable declared at dsyms7.c:17 */
3773 if (xml) {
3774 TAGL( dn1 );
3775 p2XA( dn1,
3776 "Location 0x%lx is %ld byte%s inside %pS%pS,",
3777 data_addr, residual_offset, ro_plural, var->name,
3778 (HChar*)(VG_(indexXA)(described,0)) );
3779 TAGR( dn1 );
3780 XAGL( dn2 );
3781 TXTL( dn2 );
3782 p2XA( dn2,
3783 "a global variable declared at %pS:%d",
3784 fileName, var->lineNo);
3785 TXTR( dn2 );
3786 // FIXME: also do <dir>
3787 p2XA( dn2,
3788 " <file>%pS</file> <line>%d</line> ",
3789 fileName, var->lineNo );
3790 XAGR( dn2 );
3791 } else {
3792 p2XA( dn1,
3793 "Location 0x%lx is %ld byte%s inside %s%s,",
3794 data_addr, residual_offset, ro_plural, var->name,
3795 (HChar*)(VG_(indexXA)(described,0)) );
3796 p2XA( dn2,
3797 "a global variable declared at %s:%d",
3798 fileName, var->lineNo);
3801 else
3802 vg_assert(0);
3804 /* Zero terminate both strings */
3805 zterm_XA( dn1 );
3806 zterm_XA( dn2 );
3808 # undef TAGL
3809 # undef TAGR
3810 # undef XAGL
3811 # undef XAGR
3812 # undef TXTL
3813 # undef TXTR
3817 /* Determine if data_addr is a local variable in the frame
3818 characterised by (ip,sp,fp), and if so write its description at the
3819 ends of DNAME{1,2}, which are XArray*s of HChar, that have been
3820 initialised by the caller, zero terminate both, and return True.
3821 If it's not a local variable in said frame, return False. */
3822 static
3823 Bool consider_vars_in_frame ( /*MOD*/XArray* /* of HChar */ dname1,
3824 /*MOD*/XArray* /* of HChar */ dname2,
3825 DiEpoch ep,
3826 Addr data_addr,
3827 Addr ip, Addr sp, Addr fp,
3828 /* shown to user: */
3829 ThreadId tid, Int frameNo )
3831 Word i;
3832 DebugInfo* di;
3833 RegSummary regs;
3834 Bool debug = False;
3836 static UInt n_search = 0;
3837 static UInt n_steps = 0;
3838 n_search++;
3839 if (debug)
3840 VG_(printf)("QQQQ: cvif: ip,sp,fp %#lx,%#lx,%#lx\n", ip,sp,fp);
3841 /* first, find the DebugInfo that pertains to 'ip'. */
3842 for (di = debugInfo_list; di; di = di->next) {
3843 n_steps++;
3844 if (!is_DI_valid_for_epoch(di, ep))
3845 continue;
3846 /* text segment missing? unlikely, but handle it .. */
3847 if (!di->text_present || di->text_size == 0)
3848 continue;
3849 /* Ok. So does this text mapping bracket the ip? */
3850 if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
3851 break;
3854 /* Didn't find it. Strange -- means ip is a code address outside
3855 of any mapped text segment. Unlikely but not impossible -- app
3856 could be generating code to run. */
3857 if (!di)
3858 return False;
3860 if (0 && ((n_search & 0x1) == 0))
3861 VG_(printf)("consider_vars_in_frame: %u searches, "
3862 "%u DebugInfos looked at\n",
3863 n_search, n_steps);
3864 /* Start of performance-enhancing hack: once every ??? (chosen
3865 hackily after profiling) successful searches, move the found
3866 DebugInfo one step closer to the start of the list. This makes
3867 future searches cheaper. */
3868 if ((n_search & 0xFFFF) == 0) {
3869 /* Move si one step closer to the start of the list. */
3870 move_DebugInfo_one_step_forward( di );
3872 /* End of performance-enhancing hack. */
3874 /* any var info at all? */
3875 if (!di->varinfo)
3876 return False;
3878 /* Work through the scopes from most deeply nested outwards,
3879 looking for code address ranges that bracket 'ip'. The
3880 variables on each such address range found are in scope right
3881 now. Don't descend to level zero as that is the global
3882 scope. */
3883 regs.ip = ip;
3884 regs.sp = sp;
3885 regs.fp = fp;
3887 /* "for each scope, working outwards ..." */
3888 for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
3889 XArray* vars;
3890 Word j;
3891 DiAddrRange* arange;
3892 OSet* this_scope
3893 = *(OSet**)VG_(indexXA)( di->varinfo, i );
3894 if (debug)
3895 VG_(printf)("QQQQ: considering scope %ld\n", (Word)i);
3896 if (!this_scope)
3897 continue;
3898 /* Find the set of variables in this scope that
3899 bracket the program counter. */
3900 arange = VG_(OSetGen_LookupWithCmp)(
3901 this_scope, &ip,
3902 ML_(cmp_for_DiAddrRange_range)
3904 if (!arange)
3905 continue;
3906 /* stay sane */
3907 vg_assert(arange->aMin <= arange->aMax);
3908 /* It must bracket the ip we asked for, else
3909 ML_(cmp_for_DiAddrRange_range) is somehow broken. */
3910 vg_assert(arange->aMin <= ip && ip <= arange->aMax);
3911 /* It must have an attached XArray of DiVariables. */
3912 vars = arange->vars;
3913 vg_assert(vars);
3914 /* But it mustn't cover the entire address range. We only
3915 expect that to happen for the global scope (level 0), which
3916 we're not looking at here. Except, it may cover the entire
3917 address range, but in that case the vars array must be
3918 empty. */
3919 vg_assert(! (arange->aMin == (Addr)0
3920 && arange->aMax == ~(Addr)0
3921 && VG_(sizeXA)(vars) > 0) );
3922 for (j = 0; j < VG_(sizeXA)( vars ); j++) {
3923 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
3924 PtrdiffT offset;
3925 if (debug)
3926 VG_(printf)("QQQQ: var:name=%s %#lx-%#lx %#lx\n",
3927 var->name,arange->aMin,arange->aMax,ip);
3928 if (data_address_is_in_var( &offset, di->admin_tyents,
3929 var, &regs,
3930 data_addr, di )) {
3931 PtrdiffT residual_offset = 0;
3932 XArray* described = ML_(describe_type)( &residual_offset,
3933 di->admin_tyents,
3934 var->typeR, offset );
3935 format_message( dname1, dname2,
3936 data_addr, di, var, offset, residual_offset,
3937 described, frameNo, tid );
3938 VG_(deleteXA)( described );
3939 return True;
3944 return False;
3947 /* Try to form some description of DATA_ADDR by looking at the DWARF3
3948 debug info we have. This considers all global variables, and 8
3949 frames in the stacks of all threads. Result is written at the ends
3950 of DNAME{1,2}V, which are XArray*s of HChar, that have been
3951 initialised by the caller, and True is returned. If no description
3952 is created, False is returned. Regardless of the return value,
3953 DNAME{1,2}V are guaranteed to be zero terminated after the call.
3955 Note that after the call, DNAME{1,2} may have more than one
3956 trailing zero, so callers should establish the useful text length
3957 using VG_(strlen) on the contents, rather than VG_(sizeXA) on the
3958 XArray itself.
3960 Bool VG_(get_data_description)(
3961 /*MOD*/ XArray* /* of HChar */ dname1,
3962 /*MOD*/ XArray* /* of HChar */ dname2,
3963 DiEpoch ep, Addr data_addr
3966 # define N_FRAMES 8
3967 Addr ips[N_FRAMES], sps[N_FRAMES], fps[N_FRAMES];
3968 UInt n_frames;
3970 Addr stack_min, stack_max;
3971 ThreadId tid;
3972 Bool found;
3973 DebugInfo* di;
3974 Word j;
3976 if (0) VG_(printf)("get_data_description: dataaddr %#lx\n", data_addr);
3977 /* First, see if data_addr is (or is part of) a global variable.
3978 Loop over the DebugInfos we have. Check data_addr against the
3979 outermost scope of all of them, as that should be a global
3980 scope. */
3981 for (di = debugInfo_list; di != NULL; di = di->next) {
3982 OSet* global_scope;
3983 Word gs_size;
3984 Addr zero;
3985 DiAddrRange* global_arange;
3986 Word i;
3987 XArray* vars;
3989 /* text segment missing? unlikely, but handle it .. */
3990 if (!di->text_present || di->text_size == 0)
3991 continue;
3992 /* any var info at all? */
3993 if (!di->varinfo)
3994 continue;
3995 /* perhaps this object didn't contribute any vars at all? */
3996 if (VG_(sizeXA)( di->varinfo ) == 0)
3997 continue;
3998 global_scope = *(OSet**)VG_(indexXA)( di->varinfo, 0 );
3999 vg_assert(global_scope);
4000 gs_size = VG_(OSetGen_Size)( global_scope );
4001 /* The global scope might be completely empty if this
4002 compilation unit declared locals but nothing global. */
4003 if (gs_size == 0)
4004 continue;
4005 /* But if it isn't empty, then it must contain exactly one
4006 element, which covers the entire address range. */
4007 vg_assert(gs_size == 1);
4008 /* Fish out the global scope and check it is as expected. */
4009 zero = 0;
4010 global_arange
4011 = VG_(OSetGen_Lookup)( global_scope, &zero );
4012 /* The global range from (Addr)0 to ~(Addr)0 must exist */
4013 vg_assert(global_arange);
4014 vg_assert(global_arange->aMin == (Addr)0
4015 && global_arange->aMax == ~(Addr)0);
4016 /* Any vars in this range? */
4017 if (!global_arange->vars)
4018 continue;
4019 /* Ok, there are some vars in the global scope of this
4020 DebugInfo. Wade through them and see if the data addresses
4021 of any of them bracket data_addr. */
4022 vars = global_arange->vars;
4023 for (i = 0; i < VG_(sizeXA)( vars ); i++) {
4024 PtrdiffT offset;
4025 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, i );
4026 vg_assert(var->name);
4027 /* Note we use a NULL RegSummary* here. It can't make any
4028 sense for a global variable to have a location expression
4029 which depends on a SP/FP/IP value. So don't supply any.
4030 This means, if the evaluation of the location
4031 expression/list requires a register, we have to let it
4032 fail. */
4033 if (data_address_is_in_var( &offset, di->admin_tyents, var,
4034 NULL/* RegSummary* */,
4035 data_addr, di )) {
4036 PtrdiffT residual_offset = 0;
4037 XArray* described = ML_(describe_type)( &residual_offset,
4038 di->admin_tyents,
4039 var->typeR, offset );
4040 format_message( dname1, dname2,
4041 data_addr, di, var, offset, residual_offset,
4042 described, -1/*frameNo*/,
4043 VG_INVALID_THREADID );
4044 VG_(deleteXA)( described );
4045 zterm_XA( dname1 );
4046 zterm_XA( dname2 );
4047 return True;
4052 /* Ok, well it's not a global variable. So now let's snoop around
4053 in the stacks of all the threads. First try to figure out which
4054 thread's stack data_addr is in. */
4056 /* Perhaps it's on a thread's stack? */
4057 found = False;
4058 VG_(thread_stack_reset_iter)(&tid);
4059 while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) {
4060 if (stack_min >= stack_max)
4061 continue; /* ignore obviously stupid cases */
4062 if (stack_min - VG_STACK_REDZONE_SZB <= data_addr
4063 && data_addr <= stack_max) {
4064 found = True;
4065 break;
4068 if (!found) {
4069 zterm_XA( dname1 );
4070 zterm_XA( dname2 );
4071 return False;
4074 /* We conclude data_addr is in thread tid's stack. Unwind the
4075 stack to get a bunch of (ip,sp,fp) triples describing the
4076 frames, and for each frame, consider the local variables. */
4077 n_frames = VG_(get_StackTrace)( tid, ips, N_FRAMES,
4078 sps, fps, 0/*first_ip_delta*/ );
4080 vg_assert(n_frames >= 0 && n_frames <= N_FRAMES);
4081 for (j = 0; j < n_frames; j++) {
4082 if (consider_vars_in_frame( dname1, dname2,
4083 ep, data_addr,
4084 ips[j],
4085 sps[j], fps[j], tid, j )) {
4086 zterm_XA( dname1 );
4087 zterm_XA( dname2 );
4088 return True;
4090 /* Now, it appears that gcc sometimes appears to produce
4091 location lists whose ranges don't actually cover the call
4092 instruction, even though the address of the variable in
4093 question is passed as a parameter in the call. AFAICS this
4094 is simply a bug in gcc - how can the variable be claimed not
4095 exist in memory (on the stack) for the duration of a call in
4096 which its address is passed? But anyway, in the particular
4097 case I investigated (memcheck/tests/varinfo6.c, call to croak
4098 on line 2999, local var budget declared at line 3115
4099 appearing not to exist across the call to mainSort on line
4100 3143, "gcc.orig (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)" on
4101 amd64), the variable's location list does claim it exists
4102 starting at the first byte of the first instruction after the
4103 call instruction. So, call consider_vars_in_frame a second
4104 time, but this time add 1 to the IP. GDB handles this
4105 example with no difficulty, which leads me to believe that
4106 either (1) I misunderstood something, or (2) GDB has an
4107 equivalent kludge. */
4108 if (j > 0 /* this is a non-innermost frame */
4109 && consider_vars_in_frame( dname1, dname2,
4110 ep, data_addr,
4111 ips[j] + 1,
4112 sps[j], fps[j], tid, j )) {
4113 zterm_XA( dname1 );
4114 zterm_XA( dname2 );
4115 return True;
4119 /* We didn't find anything useful. */
4120 zterm_XA( dname1 );
4121 zterm_XA( dname2 );
4122 return False;
4123 # undef N_FRAMES
4127 //////////////////////////////////////////////////////////////////
4128 // //
4129 // Support for other kinds of queries to the Dwarf3 var info //
4130 // //
4131 //////////////////////////////////////////////////////////////////
4133 /* Figure out if the variable 'var' has a location that is linearly
4134 dependent on a stack pointer value, or a frame pointer value, and
4135 if it is, add a description of it to 'blocks'. Otherwise ignore
4136 it. If 'arrays_only' is True, also ignore it unless it has an
4137 array type. */
4139 static
4140 void analyse_deps ( /*MOD*/XArray* /* of FrameBlock */ blocks,
4141 const XArray* /* TyEnt */ tyents,
4142 Addr ip, const DebugInfo* di, const DiVariable* var,
4143 Bool arrays_only )
4145 GXResult res_sp_6k, res_sp_7k, res_fp_6k, res_fp_7k;
4146 RegSummary regs;
4147 MaybeULong mul;
4148 Bool isVec;
4149 TyEnt* ty;
4151 Bool debug = False;
4152 if (0&&debug)
4153 VG_(printf)("adeps: var %s\n", var->name );
4155 /* Figure out how big the variable is. */
4156 mul = ML_(sizeOfType)(tyents, var->typeR);
4157 /* If this var has a type whose size is unknown, zero, or
4158 impossibly large, it should never have been added. ML_(addVar)
4159 should have rejected it. */
4160 vg_assert(mul.b == True);
4161 vg_assert(mul.ul > 0);
4162 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
4163 /* After this point, we assume we can truncate mul.ul to a host word
4164 safely (without loss of info). */
4166 /* skip if non-array and we're only interested in arrays */
4167 ty = ML_(TyEnts__index_by_cuOff)( tyents, NULL, var->typeR );
4168 vg_assert(ty);
4169 vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
4170 if (ty->tag == Te_UNKNOWN)
4171 return; /* perhaps we should complain in this case? */
4172 isVec = ty->tag == Te_TyArray;
4173 if (arrays_only && !isVec)
4174 return;
4176 if (0) {ML_(pp_TyEnt_C_ishly)(tyents, var->typeR);
4177 VG_(printf)(" %s\n", var->name);}
4179 /* Do some test evaluations of the variable's location expression,
4180 in order to guess whether it is sp-relative, fp-relative, or
4181 none. A crude hack, which can be interpreted roughly as finding
4182 the first derivative of the location expression w.r.t. the
4183 supplied frame and stack pointer values. */
4184 regs.fp = 0;
4185 regs.ip = ip;
4186 regs.sp = 6 * 1024;
4187 res_sp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4189 regs.fp = 0;
4190 regs.ip = ip;
4191 regs.sp = 7 * 1024;
4192 res_sp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4194 regs.fp = 6 * 1024;
4195 regs.ip = ip;
4196 regs.sp = 0;
4197 res_fp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4199 regs.fp = 7 * 1024;
4200 regs.ip = ip;
4201 regs.sp = 0;
4202 res_fp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4204 vg_assert(res_sp_6k.kind == res_sp_7k.kind);
4205 vg_assert(res_sp_6k.kind == res_fp_6k.kind);
4206 vg_assert(res_sp_6k.kind == res_fp_7k.kind);
4208 if (res_sp_6k.kind == GXR_Addr) {
4209 StackBlock block;
4210 GXResult res;
4211 UWord sp_delta = res_sp_7k.word - res_sp_6k.word;
4212 UWord fp_delta = res_fp_7k.word - res_fp_6k.word;
4213 vg_assert(sp_delta == 0 || sp_delta == 1024);
4214 vg_assert(fp_delta == 0 || fp_delta == 1024);
4216 if (sp_delta == 0 && fp_delta == 0) {
4217 /* depends neither on sp nor fp, so it can't be a stack
4218 local. Ignore it. */
4220 else
4221 if (sp_delta == 1024 && fp_delta == 0) {
4222 regs.sp = regs.fp = 0;
4223 regs.ip = ip;
4224 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4225 vg_assert(res.kind == GXR_Addr);
4226 if (debug)
4227 VG_(printf)(" %5lu .. %5llu (sp) %s\n",
4228 res.word, res.word + mul.ul - 1, var->name);
4229 block.base = res.word;
4230 block.szB = (SizeT)mul.ul;
4231 block.spRel = True;
4232 block.isVec = isVec;
4233 VG_(memset)( &block.name[0], 0, sizeof(block.name) );
4234 if (var->name)
4235 VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
4236 block.name[ sizeof(block.name)-1 ] = 0;
4237 VG_(addToXA)( blocks, &block );
4239 else
4240 if (sp_delta == 0 && fp_delta == 1024) {
4241 regs.sp = regs.fp = 0;
4242 regs.ip = ip;
4243 res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
4244 vg_assert(res.kind == GXR_Addr);
4245 if (debug)
4246 VG_(printf)(" %5lu .. %5llu (FP) %s\n",
4247 res.word, res.word + mul.ul - 1, var->name);
4248 block.base = res.word;
4249 block.szB = (SizeT)mul.ul;
4250 block.spRel = False;
4251 block.isVec = isVec;
4252 VG_(memset)( &block.name[0], 0, sizeof(block.name) );
4253 if (var->name)
4254 VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
4255 block.name[ sizeof(block.name)-1 ] = 0;
4256 VG_(addToXA)( blocks, &block );
4258 else {
4259 vg_assert(0);
4265 /* Get an XArray of StackBlock which describe the stack (auto) blocks
4266 for this ip. The caller is expected to free the XArray at some
4267 point. If 'arrays_only' is True, only array-typed blocks are
4268 returned; otherwise blocks of all types are returned. */
4270 XArray* /* of StackBlock */
4271 VG_(di_get_stack_blocks_at_ip)( Addr ip, Bool arrays_only )
4273 /* This is a derivation of consider_vars_in_frame() above. */
4274 Word i;
4275 DebugInfo* di;
4276 Bool debug = False;
4278 XArray* res = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dgsbai.1",
4279 ML_(dinfo_free),
4280 sizeof(StackBlock) );
4282 static UInt n_search = 0;
4283 static UInt n_steps = 0;
4284 n_search++;
4285 if (debug)
4286 VG_(printf)("QQQQ: dgsbai: ip %#lx\n", ip);
4287 /* first, find the DebugInfo that pertains to 'ip'. */
4288 for (di = debugInfo_list; di; di = di->next) {
4289 n_steps++;
4290 /* text segment missing? unlikely, but handle it .. */
4291 if (!di->text_present || di->text_size == 0)
4292 continue;
4293 /* Ok. So does this text mapping bracket the ip? */
4294 if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
4295 break;
4298 /* Didn't find it. Strange -- means ip is a code address outside
4299 of any mapped text segment. Unlikely but not impossible -- app
4300 could be generating code to run. */
4301 if (!di)
4302 return res; /* currently empty */
4304 if (0 && ((n_search & 0x1) == 0))
4305 VG_(printf)("VG_(di_get_stack_blocks_at_ip): %u searches, "
4306 "%u DebugInfos looked at\n",
4307 n_search, n_steps);
4308 /* Start of performance-enhancing hack: once every ??? (chosen
4309 hackily after profiling) successful searches, move the found
4310 DebugInfo one step closer to the start of the list. This makes
4311 future searches cheaper. */
4312 if ((n_search & 0xFFFF) == 0) {
4313 /* Move si one step closer to the start of the list. */
4314 move_DebugInfo_one_step_forward( di );
4316 /* End of performance-enhancing hack. */
4318 /* any var info at all? */
4319 if (!di->varinfo)
4320 return res; /* currently empty */
4322 /* Work through the scopes from most deeply nested outwards,
4323 looking for code address ranges that bracket 'ip'. The
4324 variables on each such address range found are in scope right
4325 now. Don't descend to level zero as that is the global
4326 scope. */
4328 /* "for each scope, working outwards ..." */
4329 for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
4330 XArray* vars;
4331 Word j;
4332 DiAddrRange* arange;
4333 OSet* this_scope
4334 = *(OSet**)VG_(indexXA)( di->varinfo, i );
4335 if (debug)
4336 VG_(printf)("QQQQ: considering scope %ld\n", (Word)i);
4337 if (!this_scope)
4338 continue;
4339 /* Find the set of variables in this scope that
4340 bracket the program counter. */
4341 arange = VG_(OSetGen_LookupWithCmp)(
4342 this_scope, &ip,
4343 ML_(cmp_for_DiAddrRange_range)
4345 if (!arange)
4346 continue;
4347 /* stay sane */
4348 vg_assert(arange->aMin <= arange->aMax);
4349 /* It must bracket the ip we asked for, else
4350 ML_(cmp_for_DiAddrRange_range) is somehow broken. */
4351 vg_assert(arange->aMin <= ip && ip <= arange->aMax);
4352 /* It must have an attached XArray of DiVariables. */
4353 vars = arange->vars;
4354 vg_assert(vars);
4355 /* But it mustn't cover the entire address range. We only
4356 expect that to happen for the global scope (level 0), which
4357 we're not looking at here. Except, it may cover the entire
4358 address range, but in that case the vars array must be
4359 empty. */
4360 vg_assert(! (arange->aMin == (Addr)0
4361 && arange->aMax == ~(Addr)0
4362 && VG_(sizeXA)(vars) > 0) );
4363 for (j = 0; j < VG_(sizeXA)( vars ); j++) {
4364 DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
4365 if (debug)
4366 VG_(printf)("QQQQ: var:name=%s %#lx-%#lx %#lx\n",
4367 var->name,arange->aMin,arange->aMax,ip);
4368 analyse_deps( res, di->admin_tyents, ip,
4369 di, var, arrays_only );
4373 return res;
4377 /* Get an array of GlobalBlock which describe the global blocks owned
4378 by the shared object characterised by the given di_handle. Asserts
4379 if the handle is invalid. The caller is responsible for freeing
4380 the array at some point. If 'arrays_only' is True, only
4381 array-typed blocks are returned; otherwise blocks of all types are
4382 returned. */
4384 XArray* /* of GlobalBlock */
4385 VG_(di_get_global_blocks_from_dihandle) ( ULong di_handle, Bool arrays_only )
4387 /* This is a derivation of consider_vars_in_frame() above. */
4389 DebugInfo* di;
4390 XArray* gvars; /* XArray* of GlobalBlock */
4391 Word nScopes, scopeIx;
4393 /* The first thing to do is find the DebugInfo that
4394 pertains to 'di_handle'. */
4395 vg_assert(di_handle > 0);
4396 for (di = debugInfo_list; di; di = di->next) {
4397 if (di->handle == di_handle)
4398 break;
4401 /* If this fails, we were unable to find any DebugInfo with the
4402 given handle. This is considered an error on the part of the
4403 caller. */
4404 vg_assert(di != NULL);
4406 /* we'll put the collected variables in here. */
4407 gvars = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dggbfd.1",
4408 ML_(dinfo_free), sizeof(GlobalBlock) );
4410 /* any var info at all? */
4411 if (!di->varinfo)
4412 return gvars;
4414 /* we'll iterate over all the variables we can find, even if
4415 it seems senseless to visit stack-allocated variables */
4416 /* Iterate over all scopes */
4417 nScopes = VG_(sizeXA)( di->varinfo );
4418 for (scopeIx = 0; scopeIx < nScopes; scopeIx++) {
4420 /* Iterate over each (code) address range at the current scope */
4421 DiAddrRange* range;
4422 OSet* /* of DiAddrInfo */ scope
4423 = *(OSet**)VG_(indexXA)( di->varinfo, scopeIx );
4424 vg_assert(scope);
4425 VG_(OSetGen_ResetIter)(scope);
4426 while ( (range = VG_(OSetGen_Next)(scope)) ) {
4428 /* Iterate over each variable in the current address range */
4429 Word nVars, varIx;
4430 vg_assert(range->vars);
4431 nVars = VG_(sizeXA)( range->vars );
4432 for (varIx = 0; varIx < nVars; varIx++) {
4434 Bool isVec;
4435 GXResult res;
4436 MaybeULong mul;
4437 GlobalBlock gb;
4438 TyEnt* ty;
4439 DiVariable* var = VG_(indexXA)( range->vars, varIx );
4440 vg_assert(var->name);
4441 if (0) VG_(printf)("at depth %ld var %s ", scopeIx, var->name );
4443 /* Now figure out if this variable has a constant address
4444 (that is, independent of FP, SP, phase of moon, etc),
4445 and if so, what the address is. Any variable with a
4446 constant address is deemed to be a global so we collect
4447 it. */
4448 if (0) { VG_(printf)("EVAL: "); ML_(pp_GX)(var->gexpr);
4449 VG_(printf)("\n"); }
4450 res = ML_(evaluate_trivial_GX)( var->gexpr, di );
4452 /* Not a constant address => not interesting */
4453 if (res.kind != GXR_Addr) {
4454 if (0) VG_(printf)("FAIL\n");
4455 continue;
4458 /* Ok, it's a constant address. See if we want to collect
4459 it. */
4460 if (0) VG_(printf)("%#lx\n", res.word);
4462 /* Figure out how big the variable is. */
4463 mul = ML_(sizeOfType)(di->admin_tyents, var->typeR);
4465 /* If this var has a type whose size is unknown, zero, or
4466 impossibly large, it should never have been added.
4467 ML_(addVar) should have rejected it. */
4468 vg_assert(mul.b == True);
4469 vg_assert(mul.ul > 0);
4470 if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
4471 /* After this point, we assume we can truncate mul.ul to a
4472 host word safely (without loss of info). */
4474 /* skip if non-array and we're only interested in
4475 arrays */
4476 ty = ML_(TyEnts__index_by_cuOff)( di->admin_tyents, NULL,
4477 var->typeR );
4478 vg_assert(ty);
4479 vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
4480 if (ty->tag == Te_UNKNOWN)
4481 continue; /* perhaps we should complain in this case? */
4483 isVec = ty->tag == Te_TyArray;
4484 if (arrays_only && !isVec) continue;
4486 /* Ok, so collect it! */
4487 vg_assert(var->name);
4488 vg_assert(di->soname);
4489 if (0) VG_(printf)("XXXX %s %s %d\n", var->name,
4490 ML_(fndn_ix2filename)(di, var->fndn_ix),
4491 var->lineNo);
4492 VG_(memset)(&gb, 0, sizeof(gb));
4493 gb.addr = res.word;
4494 gb.szB = (SizeT)mul.ul;
4495 gb.isVec = isVec;
4496 VG_(strncpy)(&gb.name[0], var->name, sizeof(gb.name)-1);
4497 VG_(strncpy)(&gb.soname[0], di->soname, sizeof(gb.soname)-1);
4498 vg_assert(gb.name[ sizeof(gb.name)-1 ] == 0);
4499 vg_assert(gb.soname[ sizeof(gb.soname)-1 ] == 0);
4501 VG_(addToXA)( gvars, &gb );
4503 } /* for (varIx = 0; varIx < nVars; varIx++) */
4505 } /* while ( (range = VG_(OSetGen_Next)(scope)) ) */
4507 } /* for (scopeIx = 0; scopeIx < nScopes; scopeIx++) */
4509 return gvars;
4513 /*------------------------------------------------------------*/
4514 /*--- DebugInfo accessor functions ---*/
4515 /*------------------------------------------------------------*/
4517 const DebugInfo* VG_(next_DebugInfo)(const DebugInfo* di)
4519 if (di == NULL)
4520 return debugInfo_list;
4521 return di->next;
4524 Addr VG_(DebugInfo_get_text_avma)(const DebugInfo* di)
4526 return di->text_present ? di->text_avma : 0;
4529 SizeT VG_(DebugInfo_get_text_size)(const DebugInfo* di)
4531 return di->text_present ? di->text_size : 0;
4534 Addr VG_(DebugInfo_get_bss_avma)(const DebugInfo* di)
4536 return di->bss_present ? di->bss_avma : 0;
4539 SizeT VG_(DebugInfo_get_bss_size)(const DebugInfo* di)
4541 return di->bss_present ? di->bss_size : 0;
4544 Addr VG_(DebugInfo_get_plt_avma)(const DebugInfo* di)
4546 return di->plt_present ? di->plt_avma : 0;
4549 SizeT VG_(DebugInfo_get_plt_size)(const DebugInfo* di)
4551 return di->plt_present ? di->plt_size : 0;
4554 Addr VG_(DebugInfo_get_gotplt_avma)(const DebugInfo* di)
4556 return di->gotplt_present ? di->gotplt_avma : 0;
4559 SizeT VG_(DebugInfo_get_gotplt_size)(const DebugInfo* di)
4561 return di->gotplt_present ? di->gotplt_size : 0;
4564 Addr VG_(DebugInfo_get_got_avma)(const DebugInfo* di)
4566 return di->got_present ? di->got_avma : 0;
4569 SizeT VG_(DebugInfo_get_got_size)(const DebugInfo* di)
4571 return di->got_present ? di->got_size : 0;
4574 const HChar* VG_(DebugInfo_get_soname)(const DebugInfo* di)
4576 return di->soname;
4579 const HChar* VG_(DebugInfo_get_filename)(const DebugInfo* di)
4581 return di->fsm.filename;
4584 PtrdiffT VG_(DebugInfo_get_text_bias)(const DebugInfo* di)
4586 return di->text_present ? di->text_bias : 0;
4589 Int VG_(DebugInfo_syms_howmany) ( const DebugInfo *si )
4591 return si->symtab_used;
4594 void VG_(DebugInfo_syms_getidx) ( const DebugInfo *si,
4595 Int idx,
4596 /*OUT*/SymAVMAs* avmas,
4597 /*OUT*/UInt* size,
4598 /*OUT*/const HChar** pri_name,
4599 /*OUT*/const HChar*** sec_names,
4600 /*OUT*/Bool* isText,
4601 /*OUT*/Bool* isIFunc,
4602 /*OUT*/Bool* isGlobal )
4604 vg_assert(idx >= 0 && idx < si->symtab_used);
4605 if (avmas) *avmas = si->symtab[idx].avmas;
4606 if (size) *size = si->symtab[idx].size;
4607 if (pri_name) *pri_name = si->symtab[idx].pri_name;
4608 if (sec_names) *sec_names = si->symtab[idx].sec_names;
4609 if (isText) *isText = si->symtab[idx].isText;
4610 if (isIFunc) *isIFunc = si->symtab[idx].isIFunc;
4611 if (isGlobal) *isGlobal = si->symtab[idx].isGlobal;
4615 /*------------------------------------------------------------*/
4616 /*--- SectKind query functions ---*/
4617 /*------------------------------------------------------------*/
4619 /* Convert a VgSectKind to a string, which must be copied if you want
4620 to change it. */
4621 const HChar* VG_(pp_SectKind)( VgSectKind kind )
4623 switch (kind) {
4624 case Vg_SectUnknown: return "Unknown";
4625 case Vg_SectText: return "Text";
4626 case Vg_SectData: return "Data";
4627 case Vg_SectBSS: return "BSS";
4628 case Vg_SectGOT: return "GOT";
4629 case Vg_SectPLT: return "PLT";
4630 case Vg_SectOPD: return "OPD";
4631 case Vg_SectGOTPLT: return "GOTPLT";
4632 default: vg_assert(0);
4636 /* Given an address 'a', make a guess of which section of which object
4637 it comes from. If name is non-NULL, then the object's name is put
4638 in *name. The returned name, if any, should be saved away, if there is
4639 a chance that a debug-info will be discarded and the name is being
4640 used later on. */
4641 VgSectKind VG_(DebugInfo_sect_kind)( /*OUT*/const HChar** objname, Addr a)
4643 DebugInfo* di;
4644 VgSectKind res = Vg_SectUnknown;
4646 for (di = debugInfo_list; di != NULL; di = di->next) {
4648 if (0)
4649 VG_(printf)(
4650 "addr=%#lx di=%p %s got=%#lx,%lu plt=%#lx,%lu "
4651 "data=%#lx,%lu bss=%#lx,%lu\n",
4652 a, di, di->fsm.filename,
4653 di->got_avma, di->got_size,
4654 di->plt_avma, di->plt_size,
4655 di->data_avma, di->data_size,
4656 di->bss_avma, di->bss_size);
4658 if (di->text_present
4659 && di->text_size > 0
4660 && a >= di->text_avma && a < di->text_avma + di->text_size) {
4661 res = Vg_SectText;
4662 break;
4664 if (di->data_present
4665 && di->data_size > 0
4666 && a >= di->data_avma && a < di->data_avma + di->data_size) {
4667 res = Vg_SectData;
4668 break;
4670 if (di->sdata_present
4671 && di->sdata_size > 0
4672 && a >= di->sdata_avma && a < di->sdata_avma + di->sdata_size) {
4673 res = Vg_SectData;
4674 break;
4676 if (di->bss_present
4677 && di->bss_size > 0
4678 && a >= di->bss_avma && a < di->bss_avma + di->bss_size) {
4679 res = Vg_SectBSS;
4680 break;
4682 if (di->sbss_present
4683 && di->sbss_size > 0
4684 && a >= di->sbss_avma && a < di->sbss_avma + di->sbss_size) {
4685 res = Vg_SectBSS;
4686 break;
4688 if (di->plt_present
4689 && di->plt_size > 0
4690 && a >= di->plt_avma && a < di->plt_avma + di->plt_size) {
4691 res = Vg_SectPLT;
4692 break;
4694 if (di->got_present
4695 && di->got_size > 0
4696 && a >= di->got_avma && a < di->got_avma + di->got_size) {
4697 res = Vg_SectGOT;
4698 break;
4700 if (di->gotplt_present
4701 && di->gotplt_size > 0
4702 && a >= di->gotplt_avma && a < di->gotplt_avma + di->gotplt_size) {
4703 res = Vg_SectGOTPLT;
4704 break;
4706 if (di->opd_present
4707 && di->opd_size > 0
4708 && a >= di->opd_avma && a < di->opd_avma + di->opd_size) {
4709 res = Vg_SectOPD;
4710 break;
4712 /* we could also check for .eh_frame, if anyone really cares */
4715 vg_assert( (di == NULL && res == Vg_SectUnknown)
4716 || (di != NULL && res != Vg_SectUnknown) );
4718 if (objname) {
4719 if (di && di->fsm.filename) {
4720 *objname = di->fsm.filename;
4721 } else {
4722 *objname = "???";
4726 return res;
4730 static UInt debuginfo_generation = 0;
4732 UInt VG_(debuginfo_generation) (void)
4734 return debuginfo_generation;
4737 static void caches__invalidate ( void ) {
4738 cfsi_m_cache__invalidate();
4739 sym_name_cache__invalidate();
4740 debuginfo_generation++;
4743 /*--------------------------------------------------------------------*/
4744 /*--- end ---*/
4745 /*--------------------------------------------------------------------*/