1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Top level management of symbols and debugging information. ---*/
5 /*--- debuginfo.c ---*/
6 /*--------------------------------------------------------------------*/
9 This file is part of Valgrind, a dynamic binary instrumentation
12 Copyright (C) 2000-2017 Julian Seward
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
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"
70 /* Set this to 1 to enable debug printing for the
71 should-we-load-debuginfo-now? finite state machine. */
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
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 /*------------------------------------------------------------*/
111 /*------------------------------------------------------------*/
113 static void caches__invalidate (void);
116 /*------------------------------------------------------------*/
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
) {
143 VG_(printf
)("Advancing current epoch to %u due to %s\n",
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
)) {
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
);
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
);
189 // Is this DebugInfo valid for the specified epoch?
190 static inline Bool
is_DI_valid_for_epoch ( const DebugInfo
* di
, DiEpoch ep
)
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
);
200 // Both valid. di is in Archived state.
201 return di
->first_epoch
.n
<= ep
.n
&& ep
.n
<= di
->last_epoch
.n
;
203 // First is valid, last is invalid. di is in Active state.
204 return di
->first_epoch
.n
<= ep
.n
;
207 vg_assert (!last_valid
); // First invalid, last valid is a bad state.
208 // Neither is valid. di is in Allocated state.
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
;
244 if (di0
== NULL
|| di0
== di
) break;
249 vg_assert(di0
== di
);
250 if (di0
!= NULL
&& di1
!= NULL
&& di2
!= NULL
) {
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
);
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
;
269 debugInfo_list
= di0
;
274 // Debugging helper for epochs
275 static void show_epochs ( const HChar
* msg
)
279 VG_(printf
)("\nDebugInfo epoch display, requested by \"%s\"\n", msg
);
280 VG_(printf
)(" Current epoch (note: 0 means \"invalid epoch\") = %u\n",
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
);
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
298 static ULong handle_counter
= 1;
300 /* Allocate and zero out a new DebugInfo record. */
302 DebugInfo
* alloc_DebugInfo( const HChar
* 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. */
321 = VG_(string_match
)( VG_(clo_trace_symtab_patt
), filename
);
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
);
334 /* Free a DebugInfo, and also all the stuff hanging off it. */
335 static void free_DebugInfo ( DebugInfo
* di
)
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
);
357 /* We have to visit all the entries so as to free up any
358 sec_names arrays that might exist. */
360 for (i
= 0; i
< n
; i
++) {
361 DiSym
* sym
= &di
->symtab
[i
];
363 ML_(dinfo_free
)(sym
->sec_names
);
366 ML_(dinfo_free
)(di
->symtab
);
370 VG_(deleteDedupPA
) (di
->strpool
);
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
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
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
);
409 DiAddrRange
* arange
= VG_(OSetGen_Next
)(scope
);
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
);
416 /* Nothing to free in var: all the pointer fields refer
417 to stuff either on an admin list, or in
420 VG_(deleteXA
)(arange
->vars
);
421 /* Don't free arange itself, as OSetGen_Destroy does
424 VG_(OSetGen_Destroy
)(scope
);
426 VG_(deleteXA
)(di
->varinfo
);
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
));
455 /* Found it; (remove from list and free it), or archive it. */
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",
462 di
->text_avma
+ di
->text_size
,
463 curr
->fsm
.filename
? curr
->fsm
.filename
466 vg_assert(*prev_next_ptr
== curr
);
468 *prev_next_ptr
= curr
->next
;
470 if (curr
->have_dinfo
) {
471 VG_(redir_notify_delete_DebugInfo
)( curr
);
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
);
479 free_DebugInfo(curr
);
483 prev_next_ptr
= &curr
->next
;
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
;
505 curr
= debugInfo_list
;
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
))) {
525 discard_or_archive_DebugInfo( curr
);
532 /* Does [s1,+len1) overlap [s2,+len2) ? Note: does not handle
533 wraparound at the end of the address space -- just asserts in that
535 static Bool
ranges_overlap (Addr s1
, SizeT len1
, Addr s2
, SizeT len2
)
538 if (len1
== 0 || len2
== 0)
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. */
547 if (e1
< s2
|| e2
< s1
) return False
;
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
)
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
))
571 /* Discard or archive all elements of debugInfo_list whose .mark bit is set.
573 static void discard_or_archive_marked_DebugInfos ( void )
579 curr
= debugInfo_list
;
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
));
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
))
608 di
->mark
= do_DebugInfos_overlap( di
, diRef
);
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
)
625 for (di
= debugInfo_list
; di
; di
= di
->next
) {
626 if (is_DebugInfo_archived(di
))
628 vg_assert(di
->fsm
.filename
);
629 if (0==VG_(strcmp
)(di
->fsm
.filename
, filename
))
633 di
= alloc_DebugInfo(filename
);
635 di
->next
= debugInfo_list
;
638 vg_assert(!is_DebugInfo_archived(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
;
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 */
665 /* degenerate case: r-x section is empty */
668 has_nonempty_rx
= True
;
670 /* normal case: r-x section is nonempty */
672 vg_assert(map
->size
> 0);
675 for (di2
= debugInfo_list
; di2
; di2
= di2
->next
) {
676 if (di2
== di
|| is_DebugInfo_archived(di2
))
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)
682 vg_assert(!ranges_overlap(map
->avma
, map
->size
,
683 map2
->avma
, map2
->size
));
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
)
699 /* degenerate case: all r-x sections are empty */
700 if (!has_nonempty_rx
) {
701 vg_assert(di
->cfsi_rd
== NULL
);
705 /* invariant (2) - cont. */
707 vg_assert(cfsi_fits
);
709 /* invariants (3) and (4) */
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
);
719 DiCfSI
* cfsip
= &di
->cfsi_rd
[i
-1];
720 vg_assert(cfsip
->base
+ cfsip
->len
<= cfsi
->base
);
724 vg_assert(di
->cfsi_used
== 0);
725 vg_assert(di
->cfsi_size
== 0);
730 /*--------------------------------------------------------------*/
732 /*--- TOP LEVEL: INITIALISE THE DEBUGINFO SYSTEM ---*/
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 /*--------------------------------------------------------------*/
751 /*--- TOP LEVEL: NOTIFICATION (ACQUIRE/DISCARD INFO) (LINUX) ---*/
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
;
769 /* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
770 static void show_DebugInfoMappings
771 ( const DebugInfo
* di
,
772 /*MOD*/XArray
* maps
/* XArray<DebugInfoMapping> */ )
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
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
);
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
);
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)
844 for (j
= i
+1; j
< n
; j
++) {
845 map_j
= VG_(indexXA
)(maps
, j
);
846 if (map_j
->size
== 0)
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. */
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
)
882 advance_current_DiEpoch("di_notify_ACHIEVE_ACCEPT_STATE");
884 vg_assert(di
->fsm
.filename
);
886 TRACE_SYMTAB("------ start ELF OBJECT "
887 "-------------------------"
888 "------------------------------\n");
889 TRACE_SYMTAB("------ name = %s\n", di
->fsm
.filename
);
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
);
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
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
;
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. */
950 vg_assert(di
->have_dinfo
== False
);
954 TRACE_SYMTAB("------ name = %s\n", di
->fsm
.filename
);
955 TRACE_SYMTAB("------ end ELF OBJECT "
956 "-------------------------"
957 "------------------------------\n");
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
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
;
988 Int actual_fd
, oflags
;
991 Bool debug
= (DEBUG_FSM
!= 0);
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
1000 seg
= VG_(am_find_nsegment
)(a
);
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
)) )
1019 /* If the file doesn't have a name, we're hosed. Give up. */
1020 filename
= VG_(am_get_filename
)( seg
);
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)
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
)) {
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");
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
))
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
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
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
;
1126 # error "Unknown platform"
1129 # if defined(VGP_x86_darwin) && DARWIN_VERS >= DARWIN_10_7
1130 is_ro_map
= seg
->hasR
&& !seg
->hasW
&& !seg
->hasX
;
1133 # if defined(VGO_solaris)
1134 is_rx_map
= seg
->hasR
&& seg
->hasX
&& !seg
->hasW
;
1135 is_rw_map
= seg
->hasR
&& seg
->hasW
;
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
))
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
;
1156 SysRes fd
= VG_(open
)( filename
, oflags
, 0 );
1157 if (sr_isError(fd
)) {
1158 if (sr_Err(fd
) != VKI_EACCES
) {
1160 VG_(memset
)(&fake_di
, 0, sizeof(fake_di
));
1161 fake_di
.fsm
.filename
= ML_(dinfo_strdup
)("di.debuginfo.nmm",
1163 ML_(symerr
)(&fake_di
, True
,
1164 "can't open file to inspect ELF header");
1168 actual_fd
= sr_Res(fd
);
1173 preadres
= VG_(pread
)( actual_fd
, buf1k
, sizeof(buf1k
), 0 );
1175 VG_(close
)( actual_fd
);
1178 if (sr_isError(preadres
)) {
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");
1185 if (sr_Res(preadres
) == 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
))
1193 # elif defined(VGO_darwin)
1194 if (!ML_(is_macho_object_file
)( buf1k
, (SizeT
)sr_Res(preadres
) ))
1197 # error "unknown OS"
1200 /* See if we have a DebugInfo for this filename. If not,
1202 di
= find_or_create_DebugInfo_for( filename
);
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
;
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.
1230 VG_(printf
)("di_notify_mmap-5: "
1231 "achieved accept state for %s\n", filename
);
1232 return di_notify_ACHIEVE_ACCEPT_STATE ( di
);
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
1242 /* Unmap is simpler - throw away any SegInfos intersecting
1244 void VG_(di_notify_munmap
)( Addr a
, SizeT len
)
1247 if (0) VG_(printf
)("DISCARD %#lx %#lx\n", a
, a
+len
);
1248 anyFound
= discard_syms_in_range(a
, len
);
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
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
);
1268 Bool anyFound
= discard_syms_in_range(a
, len
);
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
);
1287 VG_(printf
)("di_notify_vm_protect-0:\n");
1288 VG_(printf
)("di_notify_vm_protect-1: %#lx-%#lx %c%c%c\n",
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)
1297 if (do_nothing
/* wrong platform */) {
1299 VG_(printf
)("di_notify_vm_protect-2: wrong platform, "
1300 "doing nothing.\n");
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
1312 VG_(printf
)("di_notify_vm_protect-3: looking for existing DebugInfo*\n");
1314 DebugInfoMapping
*map
= NULL
;
1316 for (di
= debugInfo_list
; di
; di
= di
->next
) {
1317 vg_assert(di
->fsm
.filename
);
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
)
1334 continue; /* this isn't an upgrade of an r-- mapping */
1335 /* looks like we're in luck! */
1339 return; /* didn't find anything */
1342 VG_(printf
)("di_notify_vm_protect-4: found existing DebugInfo* at %p\n",
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
);
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
);
1356 di
->fsm
.have_ro_map
= True
;
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
) {
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
;
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, "
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
);
1403 return; /* stat failed ?! */
1405 obj_mtime
= stat_buf
.mtime
;
1407 /* and get its name into exename. */
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
);
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
] == '\\')
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
);
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
);
1468 /* It's not a fully qualified path, or neither $HOME nor $WINE
1469 are set (strange). Give up. */
1470 ML_(dinfo_free
)(pdbname
);
1475 /* Try s/exe/pdb/ if we don't have a valid 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
, '.');
1488 goto out
; /* there's no dot in the exe's name ?! */
1490 goto out
; /* hmm, path ends in "." */
1492 if ('A' <= dot
[1] && dot
[1] <= 'Z')
1493 VG_(strcpy
)(dot
, ".PDB");
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",
1505 if (VG_(clo_verbosity
) > 0)
1506 VG_(message
)(Vg_UserMsg
, "LOAD_PDB_DEBUGINFO: missing: %s\n", pdbname
);
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
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
);
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
);
1550 sres
= VG_(am_mmap_anon_float_valgrind
)( n_pdbimage
);
1551 if (sr_isError(sres
)) {
1552 VG_(close
)(fd_pdbimage
);
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
);
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
);
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
);
1608 if (pdbname
) ML_(dinfo_free
)(pdbname
);
1611 #endif /* defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) */
1614 /*------------------------------------------------------------*/
1616 /*--- TOP LEVEL: QUERYING EXISTING DEBUG INFO ---*/
1618 /*------------------------------------------------------------*/
1620 void VG_(di_discard_ALL_debuginfo
)( void )
1622 DebugInfo
*di
, *di2
;
1623 di
= debugInfo_list
;
1626 VG_(printf
)("XXX rm %p\n", di
);
1627 free_DebugInfo( di
);
1633 DebugInfoMapping
* ML_(find_rx_mapping
) ( DebugInfo
* di
, Addr lo
, Addr hi
)
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
;
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)
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
)
1689 DiInlLoc
*hinl
= NULL
;
1696 if (iipc
->curlevel
<= 0) {
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
];
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
1717 iipc
->curlevel
= di
->inltab
[iipc
->next_inltab
].level
;
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
)
1735 hi
= di
->inltab_used
-1;
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; }
1743 while (lo
<= di
->inltab_used
-1 && di
->inltab
[lo
].addr_lo
<= eip
)
1746 for (mid
= 0; mid
<= di
->inltab_used
-1; mid
++)
1747 if (eip
< di
->inltab
[mid
].addr_lo
)
1749 vg_assert (lo
- 1 == mid
- 1);
1754 InlIPCursor
* VG_(new_IIPC
)(DiEpoch ep
, Addr eip
)
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
1772 /* We start from the highest pos in inltab after which eip would
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
) {
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
)
1786 return NULL
; // No entry containing eip.
1788 /* We have found the highest entry containing eip.
1790 ret
= ML_(dinfo_zalloc
) ("dinfo.new_IIPC", sizeof(*ret
));
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
);
1812 void VG_(delete_IIPC
)(InlIPCursor
*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
,
1838 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
1840 if (!is_DI_valid_for_epoch(di
, ep
))
1844 /* Consider any symbol in the r-x mapped area to be text.
1845 See Comment_Regarding_Text_Range_Checks in storage.c for
1847 inRange
= di
->fsm
.have_rx_map
1848 && (ML_(find_rx_mapping
)(di
, ptr
, ptr
) != NULL
);
1850 inRange
= (di
->data_present
1851 && di
->data_size
> 0
1852 && di
->data_avma
<= ptr
1853 && ptr
< di
->data_avma
+ di
->data_size
)
1856 && di
->sdata_size
> 0
1857 && di
->sdata_avma
<= ptr
1858 && ptr
< di
->sdata_avma
+ di
->sdata_size
)
1862 && di
->bss_avma
<= ptr
1863 && ptr
< di
->bss_avma
+ di
->bss_size
)
1866 && di
->sbss_size
> 0
1867 && di
->sbss_avma
<= ptr
1868 && ptr
< di
->sbss_avma
+ di
->sbss_size
)
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
;
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
)
1898 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
1899 if (!is_DI_valid_for_epoch(di
, ep
))
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
;
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
1923 // (sym_epoch, sym_avma) are the hash table key.
1926 // Fields below here are not part of the key.
1927 const HChar
* sym_name
;
1928 PtrdiffT offset
: (sizeof(PtrdiffT
)*8)-1;
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
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. */
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
)) {
1995 search_all_symtabs ( ep
, a
, &di
, &sno
, findText
);
1998 se
->isText
= findText
;
1999 if (di
== NULL
|| a
== 0)
2000 se
->sym_name
= no_sym_name
;
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)) {
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
;
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
);
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
));
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)
2060 search_all_symtabs ( ep
, guest_code_addr
,
2062 True
/*consider text symbols only*/ );
2066 return GET_TOCPTR_AVMA(si
->symtab
[sno
].avmas
);
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
,
2081 /*match_anywhere_in_fun*/True
,
2082 /*show offset?*/False
,
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
,
2096 /*match_anywhere_in_fun*/True
,
2097 /*show offset?*/True
,
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
)
2112 res
= get_sym_name ( /*C++-demangle*/True
, /*Z-demangle*/True
,
2113 /*below-main-renaming*/True
,
2115 /*match_anywhere_in_fun*/False
,
2116 /*show offset?*/False
,
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
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
,
2134 /*match_anywhere_in_fun*/True
,
2135 /*show offset?*/False
,
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
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.
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
,
2162 /*match_anywhere_in_fun*/True
,
2163 /*show offset?*/False
,
2167 const DiInlLoc
*next_inl
= iipc
&& iipc
->next_inltab
>= 0
2168 ? & iipc
->di
->inltab
[iipc
->next_inltab
]
2170 vg_assert (next_inl
);
2171 // The function we are in is called by next_inl.
2172 *buf
= next_inl
->inlinedfn
;
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
,
2187 /*match_anywhere_in_sym*/True
,
2188 /*show offset?*/False
,
2193 Vg_FnNameKind
VG_(get_fnname_kind
) ( const HChar
* name
)
2195 if (VG_STREQ("main", name
)) {
2196 return Vg_FnNameMain
;
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
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
2211 # error "Unknown OS"
2214 return Vg_FnNameBelowMain
;
2217 return Vg_FnNameNormal
;
2221 Vg_FnNameKind
VG_(get_fnname_kind_from_IP
) ( DiEpoch ep
, Addr ip
)
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
);
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
,
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
)
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
))
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
;
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
;
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;
2303 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
2304 if (!is_DI_valid_for_epoch(di
, ep
))
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
);
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
)
2327 search_all_loctabs ( ep
, a
, &si
, &locno
);
2330 fndn_ix
= ML_(fndn_ix
) (si
, locno
);
2331 *filename
= ML_(fndn_ix2filename
) (si
, fndn_ix
);
2335 /* Map a code address to a line number. Returns True if successful. */
2336 Bool
VG_(get_linenum
)( DiEpoch ep
, Addr a
, UInt
* lineno
)
2340 search_all_loctabs ( ep
, a
, &si
, &locno
);
2343 *lineno
= si
->loctab
[locno
].lineno
;
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
)
2360 search_all_loctabs ( ep
, a
, &si
, &locno
);
2365 *filename
= ""; // this used to be not initialised....
2369 fndn_ix
= ML_(fndn_ix
)(si
, locno
);
2370 *filename
= ML_(fndn_ix2filename
) (si
, fndn_ix
);
2371 *lineno
= si
->loctab
[locno
].lineno
;
2374 /* caller wants directory info too .. */
2375 *dirname
= ML_(fndn_ix2dirname
) (si
, fndn_ix
);
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
2389 Bool
VG_(lookup_symbol_SLOW
)(DiEpoch ep
,
2390 const HChar
* sopatt
, const HChar
* name
,
2393 Bool require_pToc
= False
;
2395 const DebugInfo
* si
;
2397 # if defined(VG_PLAT_USES_PPCTOC)
2398 require_pToc
= True
;
2400 for (si
= debugInfo_list
; si
; si
= si
->next
) {
2402 VG_(printf
)("lookup_symbol_SLOW: considering %s\n", si
->soname
);
2403 if (!is_DI_valid_for_epoch(si
, ep
))
2405 if (!VG_(string_match
)(sopatt
, si
->soname
)) {
2407 VG_(printf
)(" ... skip\n");
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
;
2418 const HChar
** sec_names
= si
->symtab
[i
].sec_names
;
2420 vg_assert(sec_names
[0]);
2421 while (*sec_names
) {
2422 if (0==VG_(strcmp
)(name
, *sec_names
)
2424 ? GET_TOCPTR_AVMA(si
->symtab
[i
].avmas
) : True
)) {
2425 *avmas
= si
->symtab
[i
].avmas
;
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. */
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;
2452 *buf
= ML_(dinfo_realloc
)("putStr", *buf
, *bufsiz
);
2455 VG_(strcpy
)(*buf
+ n
, str
);
2460 /* Same as putStr, but escaping chars for XML output. */
2462 putStrEsc( SizeT n
, HChar
** buf
, SizeT
*bufsiz
, const HChar
* str
)
2466 for (; *str
!= 0; str
++) {
2469 n
= putStr( n
, buf
, bufsiz
, "&");
2472 n
= putStr( n
, buf
, bufsiz
, "<");
2475 n
= putStr( n
, buf
, bufsiz
, ">");
2480 n
= putStr( n
, buf
, bufsiz
, alt
);
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)
2497 HChar ibuf
[50]; // large enough
2500 // An InlIPCursor is associated with one specific DebugInfo. So if
2501 // it exists, make sure that it is valid for the specified DiEpoch.
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
;
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
);
2521 const DiInlLoc
*next_inl
= iipc
&& iipc
->next_inltab
>= 0
2522 ? & iipc
->di
->inltab
[iipc
->next_inltab
]
2524 vg_assert (next_inl
);
2525 // The function we are in is called by next_inl.
2526 buf_fn
= next_inl
->inlinedfn
;
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
);
2540 // The source for the highest level is in the loctab entry.
2541 know_srcloc
= VG_(get_filename_linenum
)(
2547 know_dirinfo
= buf_dirname
[0] != '\0';
2549 const DiInlLoc
*cur_inl
= iipc
&& iipc
->cur_inltab
>= 0
2550 ? & iipc
->di
->inltab
[iipc
->cur_inltab
]
2552 vg_assert (cur_inl
);
2554 know_dirinfo
= False
;
2556 // The fndn_ix and lineno for the caller of the inlined fn is in cur_inl.
2557 if (cur_inl
->fndn_ix
== 0) {
2560 FnDn
*fndn
= VG_(indexEltNumber
) (iipc
->di
->fndnpool
,
2562 if (fndn
->dirname
) {
2563 buf_dirname
= fndn
->dirname
;
2564 know_dirinfo
= True
;
2566 buf_srcloc
= fndn
->filename
;
2568 lineno
= cur_inl
->lineno
;
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. */
2581 VG_(sprintf
)(ibuf
,"<ip>0x%lX</ip>", eip
);
2582 APPEND(maybe_newline
);
2585 APPEND(maybe_newline
);
2587 APPEND_ESC(buf_obj
);
2591 APPEND(maybe_newline
);
2598 APPEND(maybe_newline
);
2600 APPEND_ESC(buf_dirname
);
2603 APPEND(maybe_newline
);
2605 APPEND_ESC(buf_srcloc
);
2607 APPEND(maybe_newline
);
2609 VG_(sprintf
)(ibuf
,"%u",lineno
);
2613 APPEND(maybe_newline2
);
2618 /* Print for humans to read */
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)
2629 VG_(sprintf
)(ibuf
,"0x%lX: ", eip
);
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) {
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
);
2651 dirname
= str
+ VG_(strlen
)(prefix
);
2655 /* remove leading "./" */
2656 if (dirname
[0] == '.' && dirname
[1] == '/')
2659 // do we have any interesting directory name to show? If so
2661 if (dirname
&& dirname
[0] != 0) {
2667 VG_(sprintf
)(ibuf
,"%u",lineno
);
2670 } else if (know_objname
) {
2674 } else if (know_fnname
) {
2675 // Nb: do this in two steps because "??)" is a trigraph!
2688 /*--------------------------------------------------------------*/
2690 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
2691 /*--- DWARF3 .eh_frame INFO ---*/
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
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. */
2713 const D3UnwindRegs
* uregs
;
2714 Addr min_accessible
;
2715 Addr max_accessible
;
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
))
2724 UWord
evalCfiExpr ( const XArray
* exprs
, Int ix
,
2725 const CfiExprEvalContext
* eec
, Bool
* ok
)
2730 vg_assert(sizeof(Addr
) == sizeof(UWord
));
2731 e
= VG_(indexXA
)( exprs
, ix
);
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
;
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
;
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
;
2791 # error "Unsupported arch"
2793 default: goto unhandled
;
2797 return e
->Cex
.Const
.con
;
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) {
2806 /* let's hope it doesn't trap! */
2807 return ML_(read_UWord
)((void *)a
);
2813 VG_(printf
)("\n\nevalCfiExpr: unhandled\n");
2814 ML_(ppCfiExpr
)( exprs
, ix
);
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
,
2841 static UWord n_search
= 0;
2842 static UWord n_steps
= 0;
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
) {
2853 if (!is_DI_valid_for_epoch(di
, curr_epoch
))
2856 /* Use the per-DebugInfo summary address ranges to skip
2857 inapplicable DebugInfos quickly. */
2858 if (di
->cfsi_used
== 0)
2860 if (ip
< di
->cfsi_minavma
|| ip
> di
->cfsi_maxavma
)
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
);
2874 break; /* found it */
2880 /* we didn't find it. */
2881 *diP
= (DebugInfo
*)1;
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.
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
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",
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
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
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
2946 struct { Addr ip
; DebugInfo
* di
; DiCfSI_m
* cfsi_m
; }
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
];
2960 static UWord n_q
= 0, n_m
= 0;
2962 if (0 == (n_q
& 0x1FFFFF))
2963 VG_(printf
)("QQQ %lu %lu\n", n_q
, n_m
);
2966 if (LIKELY(ce
->ip
== ip
) && LIKELY(ce
->di
!= NULL
)) {
2967 /* found an entry in the cache .. */
2969 /* not found in cache. Search and update. */
2974 find_DiCfSI( &ce
->di
, &ce
->cfsi_m
, ip
);
2977 if (UNLIKELY(ce
->di
== (DebugInfo
*)1)) {
2978 /* no DiCfSI for this address */
2981 /* found a DiCfSI for this address */
2986 Bool
VG_(has_CF_info
)(Addr a
)
2988 return cfsi_m_cache__find (a
) != NULL
;
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
;
3002 /* Compute the CFA. */
3004 switch (cfsi_m
->cfa_how
) {
3005 # if defined(VGA_x86) || defined(VGA_amd64)
3007 cfa
= cfsi_m
->cfa_off
+ uregs
->xsp
;
3010 cfa
= cfsi_m
->cfa_off
+ uregs
->xbp
;
3012 # elif defined(VGA_arm)
3013 case CFIC_ARM_R13REL
:
3014 cfa
= cfsi_m
->cfa_off
+ uregs
->r13
;
3016 case CFIC_ARM_R12REL
:
3017 cfa
= cfsi_m
->cfa_off
+ uregs
->r12
;
3019 case CFIC_ARM_R11REL
:
3020 cfa
= cfsi_m
->cfa_off
+ uregs
->r11
;
3022 case CFIC_ARM_R7REL
:
3023 cfa
= cfsi_m
->cfa_off
+ uregs
->r7
;
3025 # elif defined(VGA_s390x)
3027 cfa
= cfsi_m
->cfa_off
+ uregs
->sp
;
3029 case CFIR_MEMCFAREL
:
3031 Addr a
= uregs
->sp
+ cfsi_m
->cfa_off
;
3032 if (a
< min_accessible
|| a
> max_accessible
-sizeof(Addr
))
3034 cfa
= ML_(read_Addr
)((void *)a
);
3041 cfa
= cfsi_m
->cfa_off
+ uregs
->fp
;
3043 # elif defined(VGA_mips32) || defined(VGA_mips64)
3045 cfa
= cfsi_m
->cfa_off
+ uregs
->sp
;
3051 cfa
= cfsi_m
->cfa_off
+ uregs
->fp
;
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
;
3058 case CFIC_ARM64_X29REL
:
3059 cfa
= cfsi_m
->cfa_off
+ uregs
->x29
;
3062 # error "Unsupported arch"
3064 case CFIC_EXPR
: /* available on all archs */
3066 VG_(printf
)("CFIC_EXPR: ");
3067 ML_(ppCfiExpr
)(di
->cfsi_exprs
, cfsi_m
->cfa_off
);
3071 eec
.min_accessible
= min_accessible
;
3072 eec
.max_accessible
= max_accessible
;
3074 cfa
= evalCfiExpr(di
->cfsi_exprs
, cfsi_m
->cfa_off
, &eec
, &ok
);
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
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
;
3104 return compute_cfa(&uregs
,
3105 min_accessible
, max_accessible
, ce
->di
, ce
->cfsi_m
);
3107 #elif defined(VGA_s390x)
3108 { D3UnwindRegs uregs
;
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
;
3120 return compute_cfa(&uregs
,
3121 min_accessible
, max_accessible
, ce
->di
, ce
->cfsi_m
);
3125 return 0; /* indicates failure */
3129 void VG_(ppUnwindInfo
) (Addr from
, Addr to
)
3132 CFSI_m_CacheEnt
* ce
;
3134 CFSI_m_CacheEnt
* next_ce
;
3137 ce
= cfsi_m_cache__find(from
);
3139 while (from
<= to
) {
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
)
3147 VG_(printf
)("[%#lx .. %#lx]: no CFI info\n", ce_from
, from
-1);
3150 ML_(ppDiCfSI
)(di
->cfsi_exprs
,
3151 ce_from
, from
- ce_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,
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
)
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
;
3196 # error "Unknown arch"
3198 ce
= cfsi_m_cache__find(ipHere
);
3200 if (UNLIKELY(ce
== NULL
))
3201 return False
; /* no info. Nothing we can do. */
3204 cfsi_m
= ce
->cfsi_m
;
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))
3219 /* Now we know the CFA, use it to roll back the registers we're
3222 # define COMPUTE(_prev, _here, _how, _off) \
3225 case CFIR_UNKNOWN: \
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)) \
3234 _prev = ML_(read_Addr)((void *)a); \
3238 _prev = cfa + (Word)_off; \
3242 ML_(ppCfiExpr)(di->cfsi_exprs,_off); \
3243 eec.uregs = uregsHere; \
3244 eec.min_accessible = min_accessible; \
3245 eec.max_accessible = max_accessible; \
3247 _prev = evalCfiExpr(di->cfsi_exprs, _off, &eec, &ok ); \
3248 if (!ok) return False; \
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
);
3281 # error "Unknown arch"
3286 *uregsHere
= uregsPrev
;
3291 /*--------------------------------------------------------------*/
3293 /*--- TOP LEVEL: FOR UNWINDING THE STACK USING ---*/
3294 /*--- MSVC FPO INFO ---*/
3296 /*--------------------------------------------------------------*/
3298 Bool
VG_(use_FPO_info
) ( /*MOD*/Addr
* ipP
,
3302 Addr min_accessible
,
3303 Addr max_accessible
)
3306 const DebugInfo
* di
;
3307 FPO_DATA
* fpo
= NULL
;
3310 static UWord n_search
= 0;
3311 static UWord n_steps
= 0;
3314 if (0) VG_(printf
)("search FPO for %#lx\n", *ipP
);
3316 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
3319 if (!is_DI_valid_for_epoch(di
, ep
))
3322 /* Use the per-DebugInfo summary address ranges to skip
3323 inapplicable DebugInfos quickly. */
3324 if (di
->fpo
== NULL
)
3326 if (*ipP
< di
->fpo_minavma
|| *ipP
> di
->fpo_maxavma
)
3329 i
= ML_(search_one_fpotab
)( di
, *ipP
);
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
);
3349 if (0 && ((n_search
& 0x7FFFF) == 0))
3350 VG_(printf
)("VG_(use_FPO_info): %lu searches, "
3351 "%lu DebugInfos looked at\n",
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. */
3368 VG_(printf
)("found fpo: ");
3375 4*.cbRegs {%edi, %esi, %ebp, %ebx}
3381 Typical code looks like:
3382 sub $4*.cdwLocals,%esp
3383 Alternative to above for >=4KB (and sometimes for smaller):
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.
3392 Other once-only instructions often scheduled >here<.
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. */
3409 *ipP
= ML_(read_Addr
)((void *)(spHere
+ 4*(fpo
->cbRegs
+ fpo
->cdwLocals
)));
3410 *spP
= spHere
+ 4*(fpo
->cbRegs
+ fpo
->cdwLocals
+ 1
3412 *fpP
= ML_(read_Addr
)((void *)(spHere
+ 4*2));
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
)
3427 /*--------------------------------------------------------------*/
3429 /*--- TOP LEVEL: GENERATE DESCRIPTION OF DATA ADDRESSES ---*/
3430 /*--- FROM DWARF3 DEBUG INFO ---*/
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
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
3446 static void zterm_XA ( XArray
* dst
)
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
,
3463 const DebugInfo
* di
)
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 */
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
);
3493 /* ignore zero-sized vars; they can never match anything. */
3496 VG_(printf
)("VVVV: -> Fail (variable is zero sized)\n");
3500 res
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, regs
, di
);
3503 VG_(printf
)("VVVV: -> ");
3504 ML_(pp_GXResult
)( res
);
3508 if (res
.kind
== GXR_Addr
3509 && res
.word
<= data_addr
3510 && data_addr
< res
.word
+ var_szB
) {
3511 *offset
= data_addr
- res
.word
;
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
3525 static void format_message ( /*MOD*/XArray
* /* of HChar */ dn1
,
3526 /*MOD*/XArray
* /* of HChar */ dn2
,
3528 const DebugInfo
* di
,
3529 const DiVariable
* var
,
3530 PtrdiffT var_offset
,
3531 PtrdiffT residual_offset
,
3532 const XArray
* /*HChar*/ described
,
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.
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;
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
3588 "Location 0x%lx is %ld byte%s inside local var \"%pS\",",
3589 data_addr
, var_offset
, vo_plural
, var
->name
);
3593 "in frame #%d of thread %u", frameNo
, tid
);
3597 "Location 0x%lx is %ld byte%s inside local var \"%s\",",
3598 data_addr
, var_offset
, vo_plural
, var
->name
);
3600 "in frame #%d of thread %u", frameNo
, tid
);
3604 if ( frameNo
>= 0 && have_srcloc
&& (!have_descr
) ) {
3606 Location 0x7fefff6cf is 543 bytes inside local var "a"
3607 declared at dsyms7.c:17, in frame #1 of thread 1
3612 "Location 0x%lx is %ld byte%s inside local var \"%pS\"",
3613 data_addr
, var_offset
, vo_plural
, var
->name
);
3618 "declared at %pS:%d, in frame #%d of thread %u",
3619 fileName
, var
->lineNo
, frameNo
, tid
);
3621 // FIXME: also do <dir>
3623 " <file>%pS</file> <line>%d</line> ",
3624 fileName
, var
->lineNo
);
3628 "Location 0x%lx is %ld byte%s inside local var \"%s\"",
3629 data_addr
, var_offset
, vo_plural
, var
->name
);
3631 "declared at %s:%d, in frame #%d of thread %u",
3632 fileName
, var
->lineNo
, frameNo
, tid
);
3636 if ( frameNo
>= 0 && (!have_srcloc
) && have_descr
) {
3638 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2
3639 in frame #1 of thread 1
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)) );
3650 "in frame #%d of thread %u", frameNo
, tid
);
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)) );
3658 "in frame #%d of thread %u", frameNo
, tid
);
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 */
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)) );
3675 "declared at %pS:%d, in frame #%d of thread %u",
3676 fileName
, var
->lineNo
, frameNo
, tid
);
3678 // FIXME: also do <dir>
3680 " <file>%pS</file> <line>%d</line> ",
3681 fileName
, var
->lineNo
);
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)) );
3689 "declared at %s:%d, in frame #%d of thread %u",
3690 fileName
, var
->lineNo
, frameNo
, tid
);
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"
3702 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
3703 data_addr
, var_offset
, vo_plural
, var
->name
);
3707 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
3708 data_addr
, var_offset
, vo_plural
, var
->name
);
3712 if ( frameNo
>= -1 && have_srcloc
&& (!have_descr
) ) {
3714 Location 0x7fefff6cf is 543 bytes inside global var "a"
3715 declared at dsyms7.c:17
3720 "Location 0x%lx is %ld byte%s inside global var \"%pS\"",
3721 data_addr
, var_offset
, vo_plural
, var
->name
);
3726 "declared at %pS:%d",
3727 fileName
, var
->lineNo
);
3729 // FIXME: also do <dir>
3731 " <file>%pS</file> <line>%d</line> ",
3732 fileName
, var
->lineNo
);
3736 "Location 0x%lx is %ld byte%s inside global var \"%s\"",
3737 data_addr
, var_offset
, vo_plural
, var
->name
);
3739 "declared at %s:%d",
3740 fileName
, var
->lineNo
);
3744 if ( frameNo
>= -1 && (!have_srcloc
) && have_descr
) {
3746 Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
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)) );
3758 "a global variable");
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)) );
3766 "a global variable");
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 */
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)) );
3783 "a global variable declared at %pS:%d",
3784 fileName
, var
->lineNo
);
3786 // FIXME: also do <dir>
3788 " <file>%pS</file> <line>%d</line> ",
3789 fileName
, var
->lineNo
);
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)) );
3797 "a global variable declared at %s:%d",
3798 fileName
, var
->lineNo
);
3804 /* Zero terminate both strings */
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. */
3823 Bool
consider_vars_in_frame ( /*MOD*/XArray
* /* of HChar */ dname1
,
3824 /*MOD*/XArray
* /* of HChar */ dname2
,
3827 Addr ip
, Addr sp
, Addr fp
,
3828 /* shown to user: */
3829 ThreadId tid
, Int frameNo
)
3836 static UInt n_search
= 0;
3837 static UInt n_steps
= 0;
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
) {
3844 if (!is_DI_valid_for_epoch(di
, ep
))
3846 /* text segment missing? unlikely, but handle it .. */
3847 if (!di
->text_present
|| di
->text_size
== 0)
3849 /* Ok. So does this text mapping bracket the ip? */
3850 if (di
->text_avma
<= ip
&& ip
< di
->text_avma
+ di
->text_size
)
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. */
3860 if (0 && ((n_search
& 0x1) == 0))
3861 VG_(printf
)("consider_vars_in_frame: %u searches, "
3862 "%u DebugInfos looked at\n",
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? */
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
3887 /* "for each scope, working outwards ..." */
3888 for (i
= VG_(sizeXA
)(di
->varinfo
) - 1; i
>= 1; i
--) {
3891 DiAddrRange
* arange
;
3893 = *(OSet
**)VG_(indexXA
)( di
->varinfo
, i
);
3895 VG_(printf
)("QQQQ: considering scope %ld\n", (Word
)i
);
3898 /* Find the set of variables in this scope that
3899 bracket the program counter. */
3900 arange
= VG_(OSetGen_LookupWithCmp
)(
3902 ML_(cmp_for_DiAddrRange_range
)
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
;
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
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
);
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
,
3931 PtrdiffT residual_offset
= 0;
3932 XArray
* described
= ML_(describe_type
)( &residual_offset
,
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
);
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
3960 Bool
VG_(get_data_description
)(
3961 /*MOD*/ XArray
* /* of HChar */ dname1
,
3962 /*MOD*/ XArray
* /* of HChar */ dname2
,
3963 DiEpoch ep
, Addr data_addr
3967 Addr ips
[N_FRAMES
], sps
[N_FRAMES
], fps
[N_FRAMES
];
3970 Addr stack_min
, stack_max
;
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
3981 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
3985 DiAddrRange
* global_arange
;
3989 /* text segment missing? unlikely, but handle it .. */
3990 if (!di
->text_present
|| di
->text_size
== 0)
3992 /* any var info at all? */
3995 /* perhaps this object didn't contribute any vars at all? */
3996 if (VG_(sizeXA
)( di
->varinfo
) == 0)
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. */
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. */
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
)
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
++) {
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
4033 if (data_address_is_in_var( &offset
, di
->admin_tyents
, var
,
4034 NULL
/* RegSummary* */,
4036 PtrdiffT residual_offset
= 0;
4037 XArray
* described
= ML_(describe_type
)( &residual_offset
,
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
);
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? */
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
) {
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
,
4085 sps
[j
], fps
[j
], tid
, j
)) {
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
,
4112 sps
[j
], fps
[j
], tid
, j
)) {
4119 /* We didn't find anything useful. */
4127 //////////////////////////////////////////////////////////////////
4129 // Support for other kinds of queries to the Dwarf3 var info //
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
4140 void analyse_deps ( /*MOD*/XArray
* /* of FrameBlock */ blocks
,
4141 const XArray
* /* TyEnt */ tyents
,
4142 Addr ip
, const DebugInfo
* di
, const DiVariable
* var
,
4145 GXResult res_sp_6k
, res_sp_7k
, res_fp_6k
, res_fp_7k
;
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
);
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
)
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. */
4187 res_sp_6k
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, di
);
4192 res_sp_7k
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, di
);
4197 res_fp_6k
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, di
);
4202 res_fp_7k
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, 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
) {
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. */
4221 if (sp_delta
== 1024 && fp_delta
== 0) {
4222 regs
.sp
= regs
.fp
= 0;
4224 res
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, di
);
4225 vg_assert(res
.kind
== GXR_Addr
);
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
;
4232 block
.isVec
= isVec
;
4233 VG_(memset
)( &block
.name
[0], 0, sizeof(block
.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
);
4240 if (sp_delta
== 0 && fp_delta
== 1024) {
4241 regs
.sp
= regs
.fp
= 0;
4243 res
= ML_(evaluate_GX
)( var
->gexpr
, var
->fbGX
, ®s
, di
);
4244 vg_assert(res
.kind
== GXR_Addr
);
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
) );
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
);
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. */
4278 XArray
* res
= VG_(newXA
)( ML_(dinfo_zalloc
), "di.debuginfo.dgsbai.1",
4280 sizeof(StackBlock
) );
4282 static UInt n_search
= 0;
4283 static UInt n_steps
= 0;
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
) {
4290 /* text segment missing? unlikely, but handle it .. */
4291 if (!di
->text_present
|| di
->text_size
== 0)
4293 /* Ok. So does this text mapping bracket the ip? */
4294 if (di
->text_avma
<= ip
&& ip
< di
->text_avma
+ di
->text_size
)
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. */
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",
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? */
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
4328 /* "for each scope, working outwards ..." */
4329 for (i
= VG_(sizeXA
)(di
->varinfo
) - 1; i
>= 1; i
--) {
4332 DiAddrRange
* arange
;
4334 = *(OSet
**)VG_(indexXA
)( di
->varinfo
, i
);
4336 VG_(printf
)("QQQQ: considering scope %ld\n", (Word
)i
);
4339 /* Find the set of variables in this scope that
4340 bracket the program counter. */
4341 arange
= VG_(OSetGen_LookupWithCmp
)(
4343 ML_(cmp_for_DiAddrRange_range
)
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
;
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
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
);
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
);
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
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. */
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
)
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
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? */
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 */
4422 OSet
* /* of DiAddrInfo */ scope
4423 = *(OSet
**)VG_(indexXA
)( di
->varinfo
, scopeIx
);
4425 VG_(OSetGen_ResetIter
)(scope
);
4426 while ( (range
= VG_(OSetGen_Next
)(scope
)) ) {
4428 /* Iterate over each variable in the current address range */
4430 vg_assert(range
->vars
);
4431 nVars
= VG_(sizeXA
)( range
->vars
);
4432 for (varIx
= 0; varIx
< nVars
; varIx
++) {
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
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");
4458 /* Ok, it's a constant address. See if we want to collect
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
4476 ty
= ML_(TyEnts__index_by_cuOff
)( di
->admin_tyents
, NULL
,
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
),
4492 VG_(memset
)(&gb
, 0, sizeof(gb
));
4494 gb
.szB
= (SizeT
)mul
.ul
;
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++) */
4513 /*------------------------------------------------------------*/
4514 /*--- DebugInfo accessor functions ---*/
4515 /*------------------------------------------------------------*/
4517 const DebugInfo
* VG_(next_DebugInfo
)(const DebugInfo
* di
)
4520 return debugInfo_list
;
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
)
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
,
4596 /*OUT*/SymAVMAs
* avmas
,
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
4621 const HChar
* VG_(pp_SectKind
)( VgSectKind 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
4641 VgSectKind
VG_(DebugInfo_sect_kind
)( /*OUT*/const HChar
** objname
, Addr a
)
4644 VgSectKind res
= Vg_SectUnknown
;
4646 for (di
= debugInfo_list
; di
!= NULL
; di
= di
->next
) {
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
) {
4664 if (di
->data_present
4665 && di
->data_size
> 0
4666 && a
>= di
->data_avma
&& a
< di
->data_avma
+ di
->data_size
) {
4670 if (di
->sdata_present
4671 && di
->sdata_size
> 0
4672 && a
>= di
->sdata_avma
&& a
< di
->sdata_avma
+ di
->sdata_size
) {
4678 && a
>= di
->bss_avma
&& a
< di
->bss_avma
+ di
->bss_size
) {
4682 if (di
->sbss_present
4683 && di
->sbss_size
> 0
4684 && a
>= di
->sbss_avma
&& a
< di
->sbss_avma
+ di
->sbss_size
) {
4690 && a
>= di
->plt_avma
&& a
< di
->plt_avma
+ di
->plt_size
) {
4696 && a
>= di
->got_avma
&& a
< di
->got_avma
+ di
->got_size
) {
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
;
4708 && a
>= di
->opd_avma
&& a
< di
->opd_avma
+ di
->opd_size
) {
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
) );
4719 if (di
&& di
->fsm
.filename
) {
4720 *objname
= di
->fsm
.filename
;
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 /*--------------------------------------------------------------------*/
4745 /*--------------------------------------------------------------------*/