debuginfo.c revision 86781fabbfc019b752f9605e487cfce77b2a592a
1
2/*--------------------------------------------------------------------*/
3/*--- Top level management of symbols and debugging information.   ---*/
4/*---                                                  debuginfo.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8   This file is part of Valgrind, a dynamic binary instrumentation
9   framework.
10
11   Copyright (C) 2000-2010 Julian Seward
12      jseward@acm.org
13
14   This program is free software; you can redistribute it and/or
15   modify it under the terms of the GNU General Public License as
16   published by the Free Software Foundation; either version 2 of the
17   License, or (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; if not, write to the Free Software
26   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27   02111-1307, USA.
28
29   The GNU General Public License is contained in the file COPYING.
30*/
31
32#include "pub_core_basics.h"
33#include "pub_core_vki.h"
34#include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy
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_stacktrace.h" // VG_(get_StackTrace) XXX: circular dependency
51#include "pub_core_ume.h"
52
53#include "priv_misc.h"           /* dinfo_zalloc/free */
54#include "priv_d3basics.h"       /* ML_(pp_GX) */
55#include "priv_tytypes.h"
56#include "priv_storage.h"
57#include "priv_readdwarf.h"
58#include "priv_readstabs.h"
59#if defined(VGO_linux)
60# include "priv_readelf.h"
61# include "priv_readdwarf3.h"
62# include "priv_readpdb.h"
63#elif defined(VGO_darwin)
64# include "priv_readmacho.h"
65# include "priv_readpdb.h"
66#endif
67
68
69/*------------------------------------------------------------*/
70/*--- The _svma / _avma / _image / _bias naming scheme     ---*/
71/*------------------------------------------------------------*/
72
73/* JRS 11 Jan 07: I find the different kinds of addresses involved in
74   debuginfo reading confusing.  Recently I arrived at some
75   terminology which makes it clearer (to me, at least).  There are 3
76   kinds of address used in the debuginfo reading process:
77
78   stated VMAs - the address where (eg) a .so says a symbol is, that
79                 is, what it tells you if you consider the .so in
80                 isolation
81
82   actual VMAs - the address where (eg) said symbol really wound up
83                 after the .so was mapped into memory
84
85   image addresses - pointers into the copy of the .so (etc)
86                     transiently mmaped aboard whilst we read its info
87
88   Additionally I use the term 'bias' to denote the difference
89   between stated and actual VMAs for a given entity.
90
91   This terminology is not used consistently, but a start has been
92   made.  readelf.c and the call-frame info reader in readdwarf.c now
93   use it.  Specifically, various variables and structure fields have
94   been annotated with _avma / _svma / _image / _bias.  In places _img
95   is used instead of _image for the sake of brevity.
96*/
97
98
99/*------------------------------------------------------------*/
100/*--- fwdses                                               ---*/
101/*------------------------------------------------------------*/
102
103static void cfsi_cache__invalidate ( void );
104
105
106/*------------------------------------------------------------*/
107/*--- Root structure                                       ---*/
108/*------------------------------------------------------------*/
109
110/* The root structure for the entire debug info system.  It is a
111   linked list of DebugInfos. */
112static DebugInfo* debugInfo_list = NULL;
113
114
115/* Find 'di' in the debugInfo_list and move it one step closer the the
116   front of the list, so as to make subsequent searches for it
117   cheaper.  When used in a controlled way, makes a major improvement
118   in some DebugInfo-search-intensive situations, most notably stack
119   unwinding on amd64-linux. */
120static void move_DebugInfo_one_step_forward ( DebugInfo* di )
121{
122   DebugInfo *di0, *di1, *di2;
123   if (di == debugInfo_list)
124      return; /* already at head of list */
125   vg_assert(di != NULL);
126   di0 = debugInfo_list;
127   di1 = NULL;
128   di2 = NULL;
129   while (True) {
130      if (di0 == NULL || di0 == di) break;
131      di2 = di1;
132      di1 = di0;
133      di0 = di0->next;
134   }
135   vg_assert(di0 == di);
136   if (di0 != NULL && di1 != NULL && di2 != NULL) {
137      DebugInfo* tmp;
138      /* di0 points to di, di1 to its predecessor, and di2 to di1's
139         predecessor.  Swap di0 and di1, that is, move di0 one step
140         closer to the start of the list. */
141      vg_assert(di2->next == di1);
142      vg_assert(di1->next == di0);
143      tmp = di0->next;
144      di2->next = di0;
145      di0->next = di1;
146      di1->next = tmp;
147   }
148   else
149   if (di0 != NULL && di1 != NULL && di2 == NULL) {
150      /* it's second in the list. */
151      vg_assert(debugInfo_list == di1);
152      vg_assert(di1->next == di0);
153      di1->next = di0->next;
154      di0->next = di1;
155      debugInfo_list = di0;
156   }
157}
158
159
160/*------------------------------------------------------------*/
161/*--- Notification (acquire/discard) helpers               ---*/
162/*------------------------------------------------------------*/
163
164/* Gives out unique abstract handles for allocated DebugInfos.  See
165   comment in priv_storage.h, declaration of struct _DebugInfo, for
166   details. */
167static ULong handle_counter = 1;
168
169/* Allocate and zero out a new DebugInfo record. */
170static
171DebugInfo* alloc_DebugInfo( const UChar* filename )
172{
173   Bool       traceme;
174   DebugInfo* di;
175
176   vg_assert(filename);
177
178   di = ML_(dinfo_zalloc)("di.debuginfo.aDI.1", sizeof(DebugInfo));
179   di->handle       = handle_counter++;
180   di->fsm.filename = ML_(dinfo_strdup)("di.debuginfo.aDI.2", filename);
181
182   /* Everything else -- pointers, sizes, arrays -- is zeroed by
183      ML_(dinfo_zalloc).  Now set up the debugging-output flags. */
184   traceme
185      = VG_(string_match)( VG_(clo_trace_symtab_patt), filename );
186   if (traceme) {
187      di->trace_symtab = VG_(clo_trace_symtab);
188      di->trace_cfi    = VG_(clo_trace_cfi);
189      di->ddump_syms   = VG_(clo_debug_dump_syms);
190      di->ddump_line   = VG_(clo_debug_dump_line);
191      di->ddump_frames = VG_(clo_debug_dump_frames);
192   }
193
194   return di;
195}
196
197
198/* Free a DebugInfo, and also all the stuff hanging off it. */
199static void free_DebugInfo ( DebugInfo* di )
200{
201   Word i, j, n;
202   struct strchunk *chunk, *next;
203   TyEnt* ent;
204   GExpr* gexpr;
205
206   vg_assert(di != NULL);
207   if (di->fsm.filename) ML_(dinfo_free)(di->fsm.filename);
208   if (di->loctab)       ML_(dinfo_free)(di->loctab);
209   if (di->cfsi)         ML_(dinfo_free)(di->cfsi);
210   if (di->cfsi_exprs)   VG_(deleteXA)(di->cfsi_exprs);
211   if (di->fpo)          ML_(dinfo_free)(di->fpo);
212
213   if (di->symtab) {
214      /* We have to visit all the entries so as to free up any
215         sec_names arrays that might exist. */
216      n = di->symtab_used;
217      for (i = 0; i < n; i++) {
218         DiSym* sym = &di->symtab[i];
219         if (sym->sec_names)
220            ML_(dinfo_free)(sym->sec_names);
221      }
222      /* and finally .. */
223      ML_(dinfo_free)(di->symtab);
224   }
225
226   for (chunk = di->strchunks; chunk != NULL; chunk = next) {
227      next = chunk->next;
228      ML_(dinfo_free)(chunk);
229   }
230
231   /* Delete the two admin arrays.  These lists exist primarily so
232      that we can visit each object exactly once when we need to
233      delete them. */
234   if (di->admin_tyents) {
235      n = VG_(sizeXA)(di->admin_tyents);
236      for (i = 0; i < n; i++) {
237         ent = (TyEnt*)VG_(indexXA)(di->admin_tyents, i);
238         /* Dump anything hanging off this ent */
239         ML_(TyEnt__make_EMPTY)(ent);
240      }
241      VG_(deleteXA)(di->admin_tyents);
242      di->admin_tyents = NULL;
243   }
244
245   if (di->admin_gexprs) {
246      n = VG_(sizeXA)(di->admin_gexprs);
247      for (i = 0; i < n; i++) {
248         gexpr = *(GExpr**)VG_(indexXA)(di->admin_gexprs, i);
249         ML_(dinfo_free)(gexpr);
250      }
251      VG_(deleteXA)(di->admin_gexprs);
252      di->admin_gexprs = NULL;
253   }
254
255   /* Dump the variable info.  This is kinda complex: we must take
256      care not to free items which reside in either the admin lists
257      (as we have just freed them) or which reside in the DebugInfo's
258      string table. */
259   if (di->varinfo) {
260      for (i = 0; i < VG_(sizeXA)(di->varinfo); i++) {
261         OSet* scope = *(OSet**)VG_(indexXA)(di->varinfo, i);
262         if (!scope) continue;
263         /* iterate over all entries in 'scope' */
264         VG_(OSetGen_ResetIter)(scope);
265         while (True) {
266            DiAddrRange* arange = VG_(OSetGen_Next)(scope);
267            if (!arange) break;
268            /* for each var in 'arange' */
269            vg_assert(arange->vars);
270            for (j = 0; j < VG_(sizeXA)( arange->vars ); j++) {
271               DiVariable* var = (DiVariable*)VG_(indexXA)(arange->vars,j);
272               vg_assert(var);
273               /* Nothing to free in var: all the pointer fields refer
274                  to stuff either on an admin list, or in
275                  .strchunks */
276            }
277            VG_(deleteXA)(arange->vars);
278            /* Don't free arange itself, as OSetGen_Destroy does
279               that */
280         }
281         VG_(OSetGen_Destroy)(scope);
282      }
283      VG_(deleteXA)(di->varinfo);
284   }
285
286   ML_(dinfo_free)(di);
287}
288
289
290/* 'si' is a member of debugInfo_list.  Find it, remove it from the
291   list, notify m_redir that this has happened, and free all storage
292   reachable from it.
293*/
294static void discard_DebugInfo ( DebugInfo* di )
295{
296   HChar* reason = "munmap";
297
298   DebugInfo** prev_next_ptr = &debugInfo_list;
299   DebugInfo*  curr          =  debugInfo_list;
300
301   while (curr) {
302      if (curr == di) {
303         /* Found it;  remove from list and free it. */
304         if (curr->have_dinfo
305             && (VG_(clo_verbosity) > 1 || VG_(clo_trace_redir)))
306            VG_(message)(Vg_DebugMsg,
307                         "Discarding syms at %#lx-%#lx in %s due to %s()\n",
308                         di->text_avma,
309                         di->text_avma + di->text_size,
310                         curr->fsm.filename ? curr->fsm.filename
311                                            : (UChar*)"???",
312                         reason);
313         vg_assert(*prev_next_ptr == curr);
314         *prev_next_ptr = curr->next;
315         if (curr->have_dinfo)
316            VG_(redir_notify_delete_DebugInfo)( curr );
317         free_DebugInfo(curr);
318         return;
319      }
320      prev_next_ptr = &curr->next;
321      curr          =  curr->next;
322   }
323
324   /* Not found. */
325}
326
327
328/* Repeatedly scan debugInfo_list, looking for DebugInfos with text
329   AVMAs intersecting [start,start+length), and call discard_DebugInfo
330   to get rid of them.  This modifies the list, hence the multiple
331   iterations.  Returns True iff any such DebugInfos were found.
332*/
333static Bool discard_syms_in_range ( Addr start, SizeT length )
334{
335   Bool       anyFound = False;
336   Bool       found;
337   DebugInfo* curr;
338
339   while (True) {
340      found = False;
341
342      curr = debugInfo_list;
343      while (True) {
344         if (curr == NULL)
345            break;
346         if (curr->text_present
347             && curr->text_size > 0
348             && (start+length - 1 < curr->text_avma
349                 || curr->text_avma + curr->text_size - 1 < start)) {
350            /* no overlap */
351	 } else {
352	    found = True;
353	    break;
354	 }
355	 curr = curr->next;
356      }
357
358      if (!found) break;
359      anyFound = True;
360      discard_DebugInfo( curr );
361   }
362
363   return anyFound;
364}
365
366
367/* Does [s1,+len1) overlap [s2,+len2) ?  Note: does not handle
368   wraparound at the end of the address space -- just asserts in that
369   case. */
370static Bool ranges_overlap (Addr s1, SizeT len1, Addr s2, SizeT len2 )
371{
372   Addr e1, e2;
373   if (len1 == 0 || len2 == 0)
374      return False;
375   e1 = s1 + len1 - 1;
376   e2 = s2 + len2 - 1;
377   /* Assert that we don't have wraparound.  If we do it would imply
378      that file sections are getting mapped around the end of the
379      address space, which sounds unlikely. */
380   vg_assert(s1 <= e1);
381   vg_assert(s2 <= e2);
382   if (e1 < s2 || e2 < s1) return False;
383   return True;
384}
385
386
387/* Do the basic rx_ and rw_ mappings of the two DebugInfos overlap in
388   any way? */
389static Bool do_DebugInfos_overlap ( DebugInfo* di1, DebugInfo* di2 )
390{
391   vg_assert(di1);
392   vg_assert(di2);
393
394   if (di1->fsm.have_rx_map && di2->fsm.have_rx_map
395       && ranges_overlap(di1->fsm.rx_map_avma, di1->fsm.rx_map_size,
396                         di2->fsm.rx_map_avma, di2->fsm.rx_map_size))
397      return True;
398
399   if (di1->fsm.have_rx_map && di2->fsm.have_rw_map
400       && ranges_overlap(di1->fsm.rx_map_avma, di1->fsm.rx_map_size,
401                         di2->fsm.rw_map_avma, di2->fsm.rw_map_size))
402      return True;
403
404   if (di1->fsm.have_rw_map && di2->fsm.have_rx_map
405       && ranges_overlap(di1->fsm.rw_map_avma, di1->fsm.rw_map_size,
406                         di2->fsm.rx_map_avma, di2->fsm.rx_map_size))
407      return True;
408
409   if (di1->fsm.have_rw_map && di2->fsm.have_rw_map
410       && ranges_overlap(di1->fsm.rw_map_avma, di1->fsm.rw_map_size,
411                         di2->fsm.rw_map_avma, di2->fsm.rw_map_size))
412      return True;
413
414   return False;
415}
416
417
418/* Discard all elements of debugInfo_list whose .mark bit is set.
419*/
420static void discard_marked_DebugInfos ( void )
421{
422   DebugInfo* curr;
423
424   while (True) {
425
426      curr = debugInfo_list;
427      while (True) {
428         if (!curr)
429            break;
430         if (curr->mark)
431            break;
432	 curr = curr->next;
433      }
434
435      if (!curr) break;
436      discard_DebugInfo( curr );
437
438   }
439}
440
441
442/* Discard any elements of debugInfo_list which overlap with diRef.
443   Clearly diRef must have its rx_ and rw_ mapping information set to
444   something sane. */
445static void discard_DebugInfos_which_overlap_with ( DebugInfo* diRef )
446{
447   DebugInfo* di;
448   /* Mark all the DebugInfos in debugInfo_list that need to be
449      deleted.  First, clear all the mark bits; then set them if they
450      overlap with siRef.  Since siRef itself is in this list we at
451      least expect its own mark bit to be set. */
452   for (di = debugInfo_list; di; di = di->next) {
453      di->mark = do_DebugInfos_overlap( di, diRef );
454      if (di == diRef) {
455         vg_assert(di->mark);
456         di->mark = False;
457      }
458   }
459   discard_marked_DebugInfos();
460}
461
462
463/* Find the existing DebugInfo for |filename| or if not found, create
464   one.  In the latter case |filename| is strdup'd into VG_AR_DINFO,
465   and the new DebugInfo is added to debugInfo_list. */
466static DebugInfo* find_or_create_DebugInfo_for ( UChar* filename )
467{
468   DebugInfo* di;
469   vg_assert(filename);
470   for (di = debugInfo_list; di; di = di->next) {
471      vg_assert(di->fsm.filename);
472      if (0==VG_(strcmp)(di->fsm.filename, filename))
473         break;
474   }
475   if (!di) {
476      di = alloc_DebugInfo(filename);
477      vg_assert(di);
478      di->next = debugInfo_list;
479      debugInfo_list = di;
480   }
481   return di;
482}
483
484
485/* Debuginfo reading for 'di' has just been successfully completed.
486   Check that the invariants stated in
487   "Comment_on_IMPORTANT_CFSI_REPRESENTATIONAL_INVARIANTS" in
488   priv_storage.h are observed. */
489static void check_CFSI_related_invariants ( DebugInfo* di )
490{
491   DebugInfo* di2 = NULL;
492   vg_assert(di);
493   /* This fn isn't called until after debuginfo for this object has
494      been successfully read.  And that shouldn't happen until we have
495      both a r-x and rw- mapping for the object.  Hence: */
496   vg_assert(di->fsm.have_rx_map);
497   vg_assert(di->fsm.have_rw_map);
498   /* degenerate case: r-x section is empty */
499   if (di->fsm.rx_map_size == 0) {
500      vg_assert(di->cfsi == NULL);
501      return;
502   }
503   /* normal case: r-x section is nonempty */
504   /* invariant (0) */
505   vg_assert(di->fsm.rx_map_size > 0);
506   /* invariant (1) */
507   for (di2 = debugInfo_list; di2; di2 = di2->next) {
508      if (di2 == di)
509         continue;
510      if (di2->fsm.rx_map_size == 0)
511         continue;
512      vg_assert(
513         di->fsm.rx_map_avma + di->fsm.rx_map_size <= di2->fsm.rx_map_avma
514         || di2->fsm.rx_map_avma + di2->fsm.rx_map_size <= di->fsm.rx_map_avma
515      );
516   }
517   di2 = NULL;
518   /* invariant (2) */
519   if (di->cfsi) {
520      vg_assert(di->cfsi_minavma <= di->cfsi_maxavma); /* duh! */
521      vg_assert(di->cfsi_minavma >= di->fsm.rx_map_avma);
522      vg_assert(di->cfsi_maxavma < di->fsm.rx_map_avma + di->fsm.rx_map_size);
523   }
524   /* invariants (3) and (4) */
525   if (di->cfsi) {
526      Word i;
527      vg_assert(di->cfsi_used > 0);
528      vg_assert(di->cfsi_size > 0);
529      for (i = 0; i < di->cfsi_used; i++) {
530         DiCfSI* cfsi = &di->cfsi[i];
531         vg_assert(cfsi->len > 0);
532         vg_assert(cfsi->base >= di->cfsi_minavma);
533         vg_assert(cfsi->base + cfsi->len - 1 <= di->cfsi_maxavma);
534         if (i > 0) {
535            DiCfSI* cfsip = &di->cfsi[i-1];
536            vg_assert(cfsip->base + cfsip->len <= cfsi->base);
537         }
538      }
539   } else {
540      vg_assert(di->cfsi_used == 0);
541      vg_assert(di->cfsi_size == 0);
542   }
543}
544
545
546/*--------------------------------------------------------------*/
547/*---                                                        ---*/
548/*--- TOP LEVEL: INITIALISE THE DEBUGINFO SYSTEM             ---*/
549/*---                                                        ---*/
550/*--------------------------------------------------------------*/
551
552void VG_(di_initialise) ( void )
553{
554   /* There's actually very little to do here, since everything
555      centers around the DebugInfos in debugInfo_list, they are
556      created and destroyed on demand, and each one is treated more or
557      less independently. */
558   vg_assert(debugInfo_list == NULL);
559
560   /* flush the CFI fast query cache. */
561   cfsi_cache__invalidate();
562}
563
564
565/*--------------------------------------------------------------*/
566/*---                                                        ---*/
567/*--- TOP LEVEL: NOTIFICATION (ACQUIRE/DISCARD INFO) (LINUX) ---*/
568/*---                                                        ---*/
569/*--------------------------------------------------------------*/
570
571#if defined(VGO_linux)  ||  defined(VGO_darwin)
572
573/* The debug info system is driven by notifications that a text
574   segment has been mapped in, or unmapped, or when sections change
575   permission.  It's all a bit kludgey and basically means watching
576   syscalls, trying to second-guess when the system's dynamic linker
577   is done with mapping in a new object for execution.  This is all
578   tracked using the DebugInfoFSM struct for the object.  Anyway, once
579   we finally decide we've got to an accept state, this section then
580   will acquire whatever info is available for the corresponding
581   object.  This section contains the notification handlers, which
582   update the FSM and determine when an accept state has been reached.
583*/
584
585/* When the sequence of observations causes a DebugInfoFSM to move
586   into the accept state, call here to actually get the debuginfo read
587   in.  Returns a ULong whose purpose is described in comments
588   preceding VG_(di_notify_mmap) just below.
589*/
590static ULong di_notify_ACHIEVE_ACCEPT_STATE ( struct _DebugInfo* di )
591{
592   ULong di_handle;
593   Bool  ok;
594
595   vg_assert(di->fsm.filename);
596   TRACE_SYMTAB("\n");
597   TRACE_SYMTAB("------ start ELF OBJECT "
598                "------------------------------\n");
599   TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
600   TRACE_SYMTAB("\n");
601
602   /* We're going to read symbols and debug info for the avma
603      ranges [rx_map_avma, +rx_map_size) and [rw_map_avma,
604      +rw_map_size).  First get rid of any other DebugInfos which
605      overlap either of those ranges (to avoid total confusion). */
606   discard_DebugInfos_which_overlap_with( di );
607
608   /* .. and acquire new info. */
609#  if defined(VGO_linux)
610   ok = ML_(read_elf_debug_info)( di );
611#  elif defined(VGO_darwin)
612   ok = ML_(read_macho_debug_info)( di );
613#  else
614#    error "unknown OS"
615#  endif
616
617   if (ok) {
618
619      TRACE_SYMTAB("\n------ Canonicalising the "
620                   "acquired info ------\n");
621      /* invalidate the CFI unwind cache. */
622      cfsi_cache__invalidate();
623      /* prepare read data for use */
624      ML_(canonicaliseTables)( di );
625      /* notify m_redir about it */
626      TRACE_SYMTAB("\n------ Notifying m_redir ------\n");
627      VG_(redir_notify_new_DebugInfo)( di );
628      /* Note that we succeeded */
629      di->have_dinfo = True;
630      tl_assert(di->handle > 0);
631      di_handle = di->handle;
632      /* Check invariants listed in
633         Comment_on_IMPORTANT_REPRESENTATIONAL_INVARIANTS in
634         priv_storage.h. */
635      check_CFSI_related_invariants(di);
636
637   } else {
638      TRACE_SYMTAB("\n------ ELF reading failed ------\n");
639      /* Something went wrong (eg. bad ELF file).  Should we delete
640         this DebugInfo?  No - it contains info on the rw/rx
641         mappings, at least. */
642      di_handle = 0;
643      vg_assert(di->have_dinfo == False);
644   }
645
646   TRACE_SYMTAB("\n");
647   TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
648   TRACE_SYMTAB("------ end ELF OBJECT "
649                "------------------------------\n");
650   TRACE_SYMTAB("\n");
651
652   return di_handle;
653}
654
655
656/* Notify the debuginfo system about a new mapping.  This is the way
657   new debug information gets loaded.  If allow_SkFileV is True, it
658   will try load debug info if the mapping at 'a' belongs to Valgrind;
659   whereas normally (False) it will not do that.  This allows us to
660   carefully control when the thing will read symbols from the
661   Valgrind executable itself.
662
663   If a call to VG_(di_notify_mmap) causes debug info to be read, then
664   the returned ULong is an abstract handle which can later be used to
665   refer to the debuginfo read as a result of this specific mapping,
666   in later queries to m_debuginfo.  In this case the handle value
667   will be one or above.  If the returned value is zero, no debug info
668   was read. */
669
670ULong VG_(di_notify_mmap)( Addr a, Bool allow_SkFileV )
671{
672   NSegment const * seg;
673   HChar*     filename;
674   Bool       is_rx_map, is_rw_map, is_ro_map;
675   DebugInfo* di;
676   SysRes     fd;
677   Int        nread, oflags;
678   HChar      buf1k[1024];
679   Bool       debug = False;
680   SysRes     statres;
681   struct vg_stat statbuf;
682
683   /* In short, figure out if this mapping is of interest to us, and
684      if so, try to guess what ld.so is doing and when/if we should
685      read debug info. */
686   seg = VG_(am_find_nsegment)(a);
687   vg_assert(seg);
688
689   if (debug)
690      VG_(printf)("di_notify_mmap-1: %#lx-%#lx %c%c%c\n",
691                  seg->start, seg->end,
692                  seg->hasR ? 'r' : '-',
693                  seg->hasW ? 'w' : '-',seg->hasX ? 'x' : '-' );
694
695   /* guaranteed by aspacemgr-linux.c, sane_NSegment() */
696   vg_assert(seg->end > seg->start);
697
698   /* Ignore non-file mappings */
699   if ( ! (seg->kind == SkFileC
700           || (seg->kind == SkFileV && allow_SkFileV)) )
701      return 0;
702
703   /* If the file doesn't have a name, we're hosed.  Give up. */
704   filename = VG_(am_get_filename)( (NSegment*)seg );
705   if (!filename)
706      return 0;
707
708   if (debug)
709      VG_(printf)("di_notify_mmap-2: %s\n", filename);
710
711   /* Only try to read debug information from regular files.  */
712   statres = VG_(stat)(filename, &statbuf);
713
714   /* stat dereferences symlinks, so we don't expect it to succeed and
715      yet produce something that is a symlink. */
716   vg_assert(sr_isError(statres) || ! VKI_S_ISLNK(statbuf.mode));
717
718   /* Don't let the stat call fail silently.  Filter out some known
719      sources of noise before complaining, though. */
720   if (sr_isError(statres)) {
721      DebugInfo fake_di;
722      Bool quiet = VG_(strstr)(filename, "/var/run/nscd/") != NULL;
723      if (!quiet && VG_(clo_verbosity) > 1) {
724         VG_(memset)(&fake_di, 0, sizeof(fake_di));
725         fake_di.fsm.filename = filename;
726         ML_(symerr)(&fake_di, True, "failed to stat64/stat this file");
727      }
728      return 0;
729   }
730
731   /* Finally, the point of all this stattery: if it's not a regular file,
732      don't try to read debug info from it. */
733   if (! VKI_S_ISREG(statbuf.mode))
734      return 0;
735
736   /* no uses of statbuf below here. */
737
738   /* Now we have to guess if this is a text-like mapping, a data-like
739      mapping, neither or both.  The rules are:
740
741        text if:   x86-linux    r and x
742                   other-linux  r and x and not w
743
744        data if:   x86-linux    r and w
745                   other-linux  r and w and not x
746
747      Background: On x86-linux, objects are typically mapped twice:
748
749      1b8fb000-1b8ff000 r-xp 00000000 08:02 4471477 vgpreload_memcheck.so
750      1b8ff000-1b900000 rw-p 00004000 08:02 4471477 vgpreload_memcheck.so
751
752      whereas ppc32-linux mysteriously does this:
753
754      118a6000-118ad000 r-xp 00000000 08:05 14209428 vgpreload_memcheck.so
755      118ad000-118b6000 ---p 00007000 08:05 14209428 vgpreload_memcheck.so
756      118b6000-118bd000 rwxp 00000000 08:05 14209428 vgpreload_memcheck.so
757
758      The third mapping should not be considered to have executable
759      code in.  Therefore a test which works for both is: r and x and
760      NOT w.  Reading symbols from the rwx segment -- which overlaps
761      the r-x segment in the file -- causes the redirection mechanism
762      to redirect to addresses in that third segment, which is wrong
763      and causes crashes.
764
765      JRS 28 Dec 05: unfortunately icc 8.1 on x86 has been seen to
766      produce executables with a single rwx segment rather than a
767      (r-x,rw-) pair. That means the rules have to be modified thusly:
768
769      x86-linux:   consider if r and x
770      all others:  consider if r and x and not w
771
772      2009 Aug 16: apply similar kludge to ppc32-linux.
773      See http://bugs.kde.org/show_bug.cgi?id=190820
774
775      There are two modes on s390x: with and without the noexec kernel
776      parameter. Together with some older kernels, this leads to several
777      variants:
778      executable: r and x
779      data:       r and w and x
780      or
781      executable: r and x
782      data:       r and w
783   */
784   is_rx_map = False;
785   is_rw_map = False;
786   is_ro_map = False;
787
788#  if defined(VGA_x86) || defined(VGA_ppc32)
789   is_rx_map = seg->hasR && seg->hasX;
790   is_rw_map = seg->hasR && seg->hasW;
791#  elif defined(VGA_amd64) || defined(VGA_ppc64) || defined(VGA_arm)
792   is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
793   is_rw_map = seg->hasR && seg->hasW && !seg->hasX;
794#  elif defined(VGP_s390x_linux)
795   is_rx_map = seg->hasR && seg->hasX && !seg->hasW;
796   is_rw_map = seg->hasR && seg->hasW;
797#  else
798#    error "Unknown platform"
799#  endif
800
801#  if defined(VGP_x86_darwin) && DARWIN_VERS == DARWIN_10_7
802   is_ro_map = seg->hasR && !seg->hasW && !seg->hasX;
803#  endif
804
805   if (debug)
806      VG_(printf)("di_notify_mmap-3: is_rx_map %d, is_rw_map %d\n",
807                  (Int)is_rx_map, (Int)is_rw_map);
808
809   /* Ignore mappings with permissions we can't possibly be interested in. */
810   if (!(is_rx_map || is_rw_map || is_ro_map))
811      return 0;
812
813   /* Peer at the first few bytes of the file, to see if it is an ELF */
814   /* object file. Ignore the file if we do not have read permission. */
815   VG_(memset)(buf1k, 0, sizeof(buf1k));
816   oflags = VKI_O_RDONLY;
817#  if defined(VKI_O_LARGEFILE)
818   oflags |= VKI_O_LARGEFILE;
819#  endif
820   fd = VG_(open)( filename, oflags, 0 );
821   if (sr_isError(fd)) {
822      if (sr_Err(fd) != VKI_EACCES) {
823         DebugInfo fake_di;
824         VG_(memset)(&fake_di, 0, sizeof(fake_di));
825         fake_di.fsm.filename = filename;
826         ML_(symerr)(&fake_di, True, "can't open file to inspect ELF header");
827      }
828      return 0;
829   }
830   nread = VG_(read)( sr_Res(fd), buf1k, sizeof(buf1k) );
831   VG_(close)( sr_Res(fd) );
832
833   if (nread == 0)
834      return 0;
835   if (nread < 0) {
836      DebugInfo fake_di;
837      VG_(memset)(&fake_di, 0, sizeof(fake_di));
838      fake_di.fsm.filename = filename;
839      ML_(symerr)(&fake_di, True, "can't read file to inspect ELF header");
840      return 0;
841   }
842   vg_assert(nread > 0 && nread <= sizeof(buf1k) );
843
844   /* We're only interested in mappings of object files. */
845#  if defined(VGO_linux)
846   if (!ML_(is_elf_object_file)( buf1k, (SizeT)nread ))
847      return 0;
848#  elif defined(VGO_darwin)
849   if (!ML_(is_macho_object_file)( buf1k, (SizeT)nread ))
850      return 0;
851#  else
852#    error "unknown OS"
853#  endif
854
855   /* See if we have a DebugInfo for this filename.  If not,
856      create one. */
857   di = find_or_create_DebugInfo_for( filename );
858   vg_assert(di);
859
860   if (is_rx_map) {
861      /* We have a text-like mapping.  Note the details. */
862      if (!di->fsm.have_rx_map) {
863         di->fsm.have_rx_map = True;
864         di->fsm.rx_map_avma = a;
865         di->fsm.rx_map_size = seg->end + 1 - seg->start;
866         di->fsm.rx_map_foff = seg->offset;
867      } else {
868         /* FIXME: complain about a second text-like mapping */
869      }
870   }
871
872   if (is_rw_map) {
873      /* We have a data-like mapping.  Note the details. */
874      if (!di->fsm.have_rw_map) {
875         di->fsm.have_rw_map = True;
876         di->fsm.rw_map_avma = a;
877         di->fsm.rw_map_size = seg->end + 1 - seg->start;
878         di->fsm.rw_map_foff = seg->offset;
879      } else {
880         /* FIXME: complain about a second data-like mapping */
881      }
882   }
883
884   if (is_ro_map) {
885      /* We have a r-- mapping.  Note the details (OSX 10.7, 32-bit only) */
886      if (!di->fsm.have_ro_map) {
887         di->fsm.have_ro_map = True;
888         di->fsm.ro_map_avma = a;
889         di->fsm.ro_map_size = seg->end + 1 - seg->start;
890         di->fsm.ro_map_foff = seg->offset;
891      } else {
892         /* FIXME: complain about a second r-- mapping */
893      }
894   }
895
896   /* So, finally, are we in an accept state? */
897   if (di->fsm.have_rx_map && di->fsm.have_rw_map && !di->have_dinfo) {
898      /* Ok, so, finally, we found what we need, and we haven't
899         already read debuginfo for this object.  So let's do so now.
900         Yee-ha! */
901      return di_notify_ACHIEVE_ACCEPT_STATE ( di );
902   } else {
903      /* If we don't have an rx and rw mapping, or if we already have
904         debuginfo for this mapping for whatever reason, go no
905         further. */
906      return 0;
907   }
908}
909
910
911/* Unmap is simpler - throw away any SegInfos intersecting
912   [a, a+len).  */
913void VG_(di_notify_munmap)( Addr a, SizeT len )
914{
915   Bool anyFound;
916   if (0) VG_(printf)("DISCARD %#lx %#lx\n", a, a+len);
917   anyFound = discard_syms_in_range(a, len);
918   if (anyFound)
919      cfsi_cache__invalidate();
920}
921
922
923/* Uh, this doesn't do anything at all.  IIRC glibc (or ld.so, I don't
924   remember) does a bunch of mprotects on itself, and if we follow
925   through here, it causes the debug info for that object to get
926   discarded. */
927void VG_(di_notify_mprotect)( Addr a, SizeT len, UInt prot )
928{
929   Bool exe_ok = toBool(prot & VKI_PROT_EXEC);
930#  if defined(VGA_x86)
931   exe_ok = exe_ok || toBool(prot & VKI_PROT_READ);
932#  endif
933   if (0 && !exe_ok) {
934      Bool anyFound = discard_syms_in_range(a, len);
935      if (anyFound)
936         cfsi_cache__invalidate();
937   }
938}
939
940
941/* This is a MacOSX 10.7 32-bit only special.  See comments on the
942   declaration of struct _DebugInfoFSM for details. */
943void VG_(di_notify_vm_protect)( Addr a, SizeT len, UInt prot )
944{
945   Bool do_nothing = True;
946#  if defined(VGP_x86_darwin) && DARWIN_VERS == DARWIN_10_7
947   do_nothing = False;
948#  endif
949   if (do_nothing /* wrong platform */)
950      return;
951
952   Bool r_ok = toBool(prot & VKI_PROT_READ);
953   Bool w_ok = toBool(prot & VKI_PROT_WRITE);
954   Bool x_ok = toBool(prot & VKI_PROT_EXEC);
955   if (! (r_ok && !w_ok && x_ok))
956      return; /* not an upgrade to r-x */
957
958   /* Find a DebugInfo containing a FSM that has [a, +len) previously
959      observed as a r-- mapping, plus some other rw- mapping.  If such
960      is found, conclude we're in an accept state and read debuginfo
961      accordingly. */
962   DebugInfo* di;
963   for (di = debugInfo_list; di; di = di->next) {
964      vg_assert(di->fsm.filename);
965      if (di->have_dinfo)
966         continue; /* already have debuginfo for this object */
967      if (!di->fsm.have_ro_map)
968         continue; /* need to have a r-- mapping for this object */
969      if (di->fsm.have_rx_map)
970         continue; /* rx- mapping already exists */
971      if (!di->fsm.have_rw_map)
972         continue; /* need to have a rw- mapping */
973      if (di->fsm.ro_map_avma != a || di->fsm.ro_map_size != len)
974         continue; /* this isn't an upgrade of the r-- mapping */
975      /* looks like we're in luck! */
976      break;
977   }
978   if (di == NULL)
979      return; /* didn't find anything */
980
981   /* Do the upgrade.  Copy the RO map info into the RX map info and
982      pretend we never saw the RO map at all. */
983   vg_assert(di->fsm.have_rw_map);
984   vg_assert(di->fsm.have_ro_map);
985   vg_assert(!di->fsm.have_rx_map);
986
987   di->fsm.have_rx_map = True;
988   di->fsm.rx_map_avma = di->fsm.ro_map_avma;
989   di->fsm.rx_map_size = di->fsm.ro_map_size;
990   di->fsm.rx_map_foff = di->fsm.ro_map_foff;
991
992   di->fsm.have_ro_map = False;
993   di->fsm.ro_map_avma = 0;
994   di->fsm.ro_map_size = 0;
995   di->fsm.ro_map_foff = 0;
996
997   /* And since we're now in an accept state, read debuginfo.  Finally. */
998   ULong di_handle __attribute__((unused))
999      = di_notify_ACHIEVE_ACCEPT_STATE( di );
1000   /* di_handle is ignored. That's not a problem per se -- it just
1001      means nobody will ever be able to refer to this debuginfo by
1002      handle since nobody will know what the handle value is. */
1003}
1004
1005
1006/*--------- PDB (windows debug info) reading --------- */
1007
1008/* this should really return ULong, as per VG_(di_notify_mmap). */
1009void VG_(di_notify_pdb_debuginfo)( Int fd_obj, Addr avma_obj,
1010                                   SizeT total_size,
1011                                   PtrdiffT unknown_purpose__reloc )
1012{
1013   Int    i, r, sz_exename;
1014   ULong  obj_mtime, pdb_mtime;
1015   Char   exename[VKI_PATH_MAX];
1016   Char*  pdbname = NULL;
1017   Char*  dot;
1018   SysRes sres;
1019   Int    fd_pdbimage;
1020   SizeT  n_pdbimage;
1021   struct vg_stat stat_buf;
1022
1023   if (VG_(clo_verbosity) > 0) {
1024      VG_(message)(Vg_UserMsg, "\n");
1025      VG_(message)(Vg_UserMsg,
1026         "LOAD_PDB_DEBUGINFO: clreq:   fd=%d, avma=%#lx, total_size=%lu, "
1027         "uu_reloc=%#lx\n",
1028         fd_obj, avma_obj, total_size, unknown_purpose__reloc
1029      );
1030   }
1031
1032   /* 'fd' refers to the .exe/.dll we're dealing with.  Get its modification
1033      time into obj_mtime. */
1034   r = VG_(fstat)(fd_obj, &stat_buf);
1035   if (r == -1)
1036      goto out; /* stat failed ?! */
1037   vg_assert(r == 0);
1038   obj_mtime = stat_buf.mtime;
1039
1040   /* and get its name into exename[]. */
1041   vg_assert(VKI_PATH_MAX > 100); /* to ensure /proc/self/fd/%d is safe */
1042   VG_(memset)(exename, 0, sizeof(exename));
1043   VG_(sprintf)(exename, "/proc/self/fd/%d", fd_obj);
1044   /* convert exename from a symlink to real name .. overwrites the
1045      old contents of the buffer.  Ick. */
1046   sz_exename = VG_(readlink)(exename, exename, sizeof(exename)-2 );
1047   if (sz_exename == -1)
1048      goto out; /* readlink failed ?! */
1049   vg_assert(sz_exename >= 0 && sz_exename < sizeof(exename));
1050   vg_assert(exename[sizeof(exename)-1] == 0);
1051
1052   if (VG_(clo_verbosity) > 0) {
1053      VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: objname: %s\n", exename);
1054   }
1055
1056   /* Try to get the PDB file name from the executable. */
1057   pdbname = ML_(find_name_of_pdb_file)(exename);
1058   if (pdbname) {
1059      vg_assert(VG_(strlen)(pdbname) >= 5); /* 5 = strlen("X.pdb") */
1060      /* So we successfully extracted a name from the PE file.  But it's
1061         likely to be of the form
1062            e:\foo\bar\xyzzy\wibble.pdb
1063         and we need to change it into something we can actually open
1064         in Wine-world, which basically means turning it into
1065            $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1066         We also take into account $WINEPREFIX, if it is set.
1067         For the moment, if the name isn't fully qualified, just forget it
1068         (we'd have to root around to find where the pdb actually is)
1069      */
1070      /* Change all the backslashes to forward slashes */
1071      for (i = 0; pdbname[i]; i++) {
1072         if (pdbname[i] == '\\')
1073            pdbname[i] = '/';
1074      }
1075      Bool is_quald
1076         = ('a' <= VG_(tolower)(pdbname[0]) && VG_(tolower)(pdbname[0]) <= 'z')
1077           && pdbname[1] == ':'
1078           && pdbname[2] == '/';
1079      HChar* home = VG_(getenv)("HOME");
1080      HChar* wpfx = VG_(getenv)("WINEPREFIX");
1081      if (is_quald && wpfx) {
1082         /* Change e:/foo/bar/xyzzy/wibble.pdb
1083                to $WINEPREFIX/drive_e/foo/bar/xyzzy/wibble.pdb
1084         */
1085         Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(wpfx) + 50/*misc*/;
1086         HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.1", mashedSzB);
1087         VG_(sprintf)(mashed, "%s/drive_%c%s",
1088                      wpfx, pdbname[0], &pdbname[2]);
1089         vg_assert(mashed[mashedSzB-1] == 0);
1090         ML_(dinfo_free)(pdbname);
1091         pdbname = mashed;
1092      }
1093      else if (is_quald && home && !wpfx) {
1094         /* Change e:/foo/bar/xyzzy/wibble.pdb
1095                to $HOME/.wine/drive_e/foo/bar/xyzzy/wibble.pdb
1096         */
1097         Int mashedSzB = VG_(strlen)(pdbname) + VG_(strlen)(home) + 50/*misc*/;
1098         HChar* mashed = ML_(dinfo_zalloc)("di.debuginfo.dnpdi.2", mashedSzB);
1099         VG_(sprintf)(mashed, "%s/.wine/drive_%c%s",
1100                      home, pdbname[0], &pdbname[2]);
1101         vg_assert(mashed[mashedSzB-1] == 0);
1102         ML_(dinfo_free)(pdbname);
1103         pdbname = mashed;
1104      } else {
1105         /* It's not a fully qualified path, or neither $HOME nor $WINE
1106            are set (strange).  Give up. */
1107         ML_(dinfo_free)(pdbname);
1108         pdbname = NULL;
1109      }
1110   }
1111
1112   /* Try s/exe/pdb/ if we don't have a valid pdbname. */
1113   if (!pdbname) {
1114      /* Try to find a matching PDB file from which to read debuginfo.
1115         Windows PE files have symbol tables and line number information,
1116         but MSVC doesn't seem to use them. */
1117      /* Why +5 ?  Because in the worst case, we could find a dot as the
1118         last character of pdbname, and we'd then put "pdb" right after
1119         it, hence extending it a bit. */
1120      pdbname = ML_(dinfo_zalloc)("di.debuginfo.lpd1", sz_exename+5);
1121      VG_(strcpy)(pdbname, exename);
1122      vg_assert(pdbname[sz_exename+5-1] == 0);
1123      dot = VG_(strrchr)(pdbname, '.');
1124      if (!dot)
1125         goto out; /* there's no dot in the exe's name ?! */
1126      if (dot[1] == 0)
1127         goto out; /* hmm, path ends in "." */
1128
1129      if ('A' <= dot[1] && dot[1] <= 'Z')
1130         VG_(strcpy)(dot, ".PDB");
1131      else
1132         VG_(strcpy)(dot, ".pdb");
1133
1134      vg_assert(pdbname[sz_exename+5-1] == 0);
1135   }
1136
1137   /* See if we can find it, and check it's in-dateness. */
1138   sres = VG_(stat)(pdbname, &stat_buf);
1139   if (sr_isError(sres)) {
1140      VG_(message)(Vg_UserMsg, "Warning: Missing or un-stat-able %s\n",
1141                               pdbname);
1142   if (VG_(clo_verbosity) > 0)
1143      VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: missing: %s\n", pdbname);
1144      goto out;
1145   }
1146   pdb_mtime = stat_buf.mtime;
1147
1148   if (obj_mtime > pdb_mtime + 60ULL) {
1149      /* PDB file is older than PE file.  Really, the PDB should be
1150         newer than the PE, but that doesn't always seem to be the
1151         case.  Allow the PDB to be up to one minute older.
1152         Otherwise, it's probably out of date, in which case ignore it
1153         or we will either (a) print wrong stack traces or more likely
1154         (b) crash.
1155      */
1156      VG_(message)(Vg_UserMsg,
1157                   "Warning:       %s (mtime = %llu)\n"
1158                   " is older than %s (mtime = %llu)\n",
1159                   pdbname, pdb_mtime, exename, obj_mtime);
1160   }
1161
1162   sres = VG_(open)(pdbname, VKI_O_RDONLY, 0);
1163   if (sr_isError(sres)) {
1164      VG_(message)(Vg_UserMsg, "Warning: Can't open %s\n", pdbname);
1165      goto out;
1166   }
1167
1168   /* Looks promising; go on to try and read stuff from it.  But don't
1169      mmap the file.  Instead mmap free space and read the file into
1170      it.  This is because files on CIFS filesystems that are mounted
1171      '-o directio' can't be mmap'd, and that mount option is needed
1172      to make CIFS work reliably.  (See
1173      http://www.nabble.com/Corrupted-data-on-write-to-
1174                            Windows-2003-Server-t2782623.html)
1175      This is slower, but at least it works reliably. */
1176   fd_pdbimage = sr_Res(sres);
1177   n_pdbimage  = stat_buf.size;
1178   if (n_pdbimage == 0 || n_pdbimage > 0x7FFFFFFF) {
1179      // 0x7FFFFFFF: why?  Because the VG_(read) just below only
1180      // can deal with a signed int as the size of data to read,
1181      // so we can't reliably check for read failure for files
1182      // greater than that size.  Hence just skip them; we're
1183      // unlikely to encounter a PDB that large anyway.
1184      VG_(close)(fd_pdbimage);
1185      goto out;
1186   }
1187   sres = VG_(am_mmap_anon_float_valgrind)( n_pdbimage );
1188   if (sr_isError(sres)) {
1189      VG_(close)(fd_pdbimage);
1190      goto out;
1191   }
1192
1193   void* pdbimage = (void*)sr_Res(sres);
1194   r = VG_(read)( fd_pdbimage, pdbimage, (Int)n_pdbimage );
1195   if (r < 0 || r != (Int)n_pdbimage) {
1196      VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1197      VG_(close)(fd_pdbimage);
1198      goto out;
1199   }
1200
1201   if (VG_(clo_verbosity) > 0)
1202      VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: pdbname: %s\n", pdbname);
1203
1204   /* play safe; always invalidate the CFI cache.  I don't know if
1205      this is necessary, but anyway .. */
1206   cfsi_cache__invalidate();
1207   /* dump old info for this range, if any */
1208   discard_syms_in_range( avma_obj, total_size );
1209
1210   { DebugInfo* di = find_or_create_DebugInfo_for(exename);
1211
1212     /* this di must be new, since we just nuked any old stuff in the range */
1213     vg_assert(di && !di->fsm.have_rx_map && !di->fsm.have_rw_map);
1214     vg_assert(!di->have_dinfo);
1215
1216     /* don't set up any of the di-> fields; let
1217        ML_(read_pdb_debug_info) do it. */
1218     ML_(read_pdb_debug_info)( di, avma_obj, unknown_purpose__reloc,
1219                               pdbimage, n_pdbimage, pdbname, pdb_mtime );
1220     // JRS fixme: take notice of return value from read_pdb_debug_info,
1221     // and handle failure
1222     vg_assert(di->have_dinfo); // fails if PDB read failed
1223     VG_(am_munmap_valgrind)( (Addr)pdbimage, n_pdbimage );
1224     VG_(close)(fd_pdbimage);
1225
1226     if (VG_(clo_verbosity) > 0) {
1227        VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: done:    "
1228                                 "%lu syms, %lu src locs, %lu fpo recs\n",
1229                     di->symtab_used, di->loctab_used, di->fpo_size);
1230     }
1231   }
1232
1233  out:
1234   if (pdbname) ML_(dinfo_free)(pdbname);
1235}
1236
1237#endif /* defined(VGO_linux) || defined(VGO_darwin) */
1238
1239
1240/*------------------------------------------------------------*/
1241/*---                                                      ---*/
1242/*--- TOP LEVEL: QUERYING EXISTING DEBUG INFO              ---*/
1243/*---                                                      ---*/
1244/*------------------------------------------------------------*/
1245
1246void VG_(di_discard_ALL_debuginfo)( void )
1247{
1248   DebugInfo *di, *di2;
1249   di = debugInfo_list;
1250   while (di) {
1251      di2 = di->next;
1252      VG_(printf)("XXX rm %p\n", di);
1253      free_DebugInfo( di );
1254      di = di2;
1255   }
1256}
1257
1258
1259/*------------------------------------------------------------*/
1260/*--- Use of symbol table & location info to create        ---*/
1261/*--- plausible-looking stack dumps.                       ---*/
1262/*------------------------------------------------------------*/
1263
1264/* Search all symtabs that we know about to locate ptr.  If found, set
1265   *pdi to the relevant DebugInfo, and *symno to the symtab entry
1266   *number within that.  If not found, *psi is set to NULL.
1267   If findText==True,  only text symbols are searched for.
1268   If findText==False, only data symbols are searched for.
1269*/
1270static void search_all_symtabs ( Addr ptr, /*OUT*/DebugInfo** pdi,
1271                                           /*OUT*/Word* symno,
1272                                 Bool match_anywhere_in_sym,
1273                                 Bool findText )
1274{
1275   Word       sno;
1276   DebugInfo* di;
1277   Bool       inRange;
1278
1279   for (di = debugInfo_list; di != NULL; di = di->next) {
1280
1281      if (findText) {
1282         /* Consider any symbol in the r-x mapped area to be text.
1283            See Comment_Regarding_Text_Range_Checks in storage.c for
1284            details. */
1285         inRange = di->fsm.have_rx_map
1286                   && di->fsm.rx_map_size > 0
1287                   && di->fsm.rx_map_avma <= ptr
1288                   && ptr < di->fsm.rx_map_avma + di->fsm.rx_map_size;
1289      } else {
1290         inRange = (di->data_present
1291                    && di->data_size > 0
1292                    && di->data_avma <= ptr
1293                    && ptr < di->data_avma + di->data_size)
1294                   ||
1295                   (di->sdata_present
1296                    && di->sdata_size > 0
1297                    && di->sdata_avma <= ptr
1298                    && ptr < di->sdata_avma + di->sdata_size)
1299                   ||
1300                   (di->bss_present
1301                    && di->bss_size > 0
1302                    && di->bss_avma <= ptr
1303                    && ptr < di->bss_avma + di->bss_size)
1304                   ||
1305                   (di->sbss_present
1306                    && di->sbss_size > 0
1307                    && di->sbss_avma <= ptr
1308                    && ptr < di->sbss_avma + di->sbss_size)
1309                   ||
1310                   (di->rodata_present
1311                    && di->rodata_size > 0
1312                    && di->rodata_avma <= ptr
1313                    && ptr < di->rodata_avma + di->rodata_size);
1314      }
1315
1316      if (!inRange) continue;
1317
1318      sno = ML_(search_one_symtab) (
1319               di, ptr, match_anywhere_in_sym, findText );
1320      if (sno == -1) goto not_found;
1321      *symno = sno;
1322      *pdi = di;
1323      return;
1324
1325   }
1326  not_found:
1327   *pdi = NULL;
1328}
1329
1330
1331/* Search all loctabs that we know about to locate ptr.  If found, set
1332   *pdi to the relevant DebugInfo, and *locno to the loctab entry
1333   *number within that.  If not found, *pdi is set to NULL. */
1334static void search_all_loctabs ( Addr ptr, /*OUT*/DebugInfo** pdi,
1335                                           /*OUT*/Word* locno )
1336{
1337   Word       lno;
1338   DebugInfo* di;
1339   for (di = debugInfo_list; di != NULL; di = di->next) {
1340      if (di->text_present
1341          && di->text_size > 0
1342          && di->text_avma <= ptr
1343          && ptr < di->text_avma + di->text_size) {
1344         lno = ML_(search_one_loctab) ( di, ptr );
1345         if (lno == -1) goto not_found;
1346         *locno = lno;
1347         *pdi = di;
1348         return;
1349      }
1350   }
1351  not_found:
1352   *pdi = NULL;
1353}
1354
1355
1356/* The whole point of this whole big deal: map a code address to a
1357   plausible symbol name.  Returns False if no idea; otherwise True.
1358   Caller supplies buf and nbuf.  If do_cxx_demangling is False, don't do
1359   C++ demangling, regardless of VG_(clo_demangle) -- probably because the
1360   call has come from VG_(get_fnname_raw)().  findText
1361   indicates whether we're looking for a text symbol or a data symbol
1362   -- caller must choose one kind or the other. */
1363static
1364Bool get_sym_name ( Bool do_cxx_demangling, Bool do_z_demangling,
1365                    Bool do_below_main_renaming,
1366                    Addr a, Char* buf, Int nbuf,
1367                    Bool match_anywhere_in_sym, Bool show_offset,
1368                    Bool findText, /*OUT*/PtrdiffT* offsetP )
1369{
1370   DebugInfo* di;
1371   Word       sno;
1372   PtrdiffT   offset;
1373
1374   search_all_symtabs ( a, &di, &sno, match_anywhere_in_sym, findText );
1375   if (di == NULL)
1376      return False;
1377
1378   vg_assert(di->symtab[sno].pri_name);
1379   VG_(demangle) ( do_cxx_demangling, do_z_demangling,
1380                   di->symtab[sno].pri_name, buf, nbuf );
1381
1382   /* Do the below-main hack */
1383   // To reduce the endless nuisance of multiple different names
1384   // for "the frame below main()" screwing up the testsuite, change all
1385   // known incarnations of said into a single name, "(below main)", if
1386   // --show-below-main=yes.
1387   if ( do_below_main_renaming && ! VG_(clo_show_below_main) &&
1388        Vg_FnNameBelowMain == VG_(get_fnname_kind)(buf) )
1389   {
1390      VG_(strncpy_safely)(buf, "(below main)", nbuf);
1391   }
1392   offset = a - di->symtab[sno].addr;
1393   if (offsetP) *offsetP = offset;
1394
1395   if (show_offset && offset != 0) {
1396      Char     buf2[12];
1397      Char*    symend = buf + VG_(strlen)(buf);
1398      Char*    end = buf + nbuf;
1399      Int      len;
1400
1401      len = VG_(sprintf)(buf2, "%c%ld",
1402			 offset < 0 ? '-' : '+',
1403			 offset < 0 ? -offset : offset);
1404      vg_assert(len < (Int)sizeof(buf2));
1405
1406      if (len < (end - symend)) {
1407	 Char *cp = buf2;
1408	 VG_(memcpy)(symend, cp, len+1);
1409      }
1410   }
1411
1412   buf[nbuf-1] = 0; /* paranoia */
1413
1414   return True;
1415}
1416
1417/* ppc64-linux only: find the TOC pointer (R2 value) that should be in
1418   force at the entry point address of the function containing
1419   guest_code_addr.  Returns 0 if not known. */
1420Addr VG_(get_tocptr) ( Addr guest_code_addr )
1421{
1422   DebugInfo* si;
1423   Word       sno;
1424   search_all_symtabs ( guest_code_addr,
1425                        &si, &sno,
1426                        True/*match_anywhere_in_fun*/,
1427                        True/*consider text symbols only*/ );
1428   if (si == NULL)
1429      return 0;
1430   else
1431      return si->symtab[sno].tocptr;
1432}
1433
1434/* This is available to tools... always demangle C++ names,
1435   match anywhere in function, but don't show offsets. */
1436Bool VG_(get_fnname) ( Addr a, Char* buf, Int nbuf )
1437{
1438   return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
1439                         /*below-main-renaming*/True,
1440                         a, buf, nbuf,
1441                         /*match_anywhere_in_fun*/True,
1442                         /*show offset?*/False,
1443                         /*text syms only*/True,
1444                         /*offsetP*/NULL );
1445}
1446
1447/* This is available to tools... always demangle C++ names,
1448   match anywhere in function, and show offset if nonzero. */
1449Bool VG_(get_fnname_w_offset) ( Addr a, Char* buf, Int nbuf )
1450{
1451   return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
1452                         /*below-main-renaming*/True,
1453                         a, buf, nbuf,
1454                         /*match_anywhere_in_fun*/True,
1455                         /*show offset?*/True,
1456                         /*text syms only*/True,
1457                         /*offsetP*/NULL );
1458}
1459
1460/* This is available to tools... always demangle C++ names,
1461   only succeed if 'a' matches first instruction of function,
1462   and don't show offsets. */
1463Bool VG_(get_fnname_if_entry) ( Addr a, Char* buf, Int nbuf )
1464{
1465   return get_sym_name ( /*C++-demangle*/True, /*Z-demangle*/True,
1466                         /*below-main-renaming*/True,
1467                         a, buf, nbuf,
1468                         /*match_anywhere_in_fun*/False,
1469                         /*show offset?*/False,
1470                         /*text syms only*/True,
1471                         /*offsetP*/NULL );
1472}
1473
1474/* This is only available to core... don't C++-demangle, don't Z-demangle,
1475   don't rename below-main, match anywhere in function, and don't show
1476   offsets. */
1477Bool VG_(get_fnname_raw) ( Addr a, Char* buf, Int nbuf )
1478{
1479   return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
1480                         /*below-main-renaming*/False,
1481                         a, buf, nbuf,
1482                         /*match_anywhere_in_fun*/True,
1483                         /*show offset?*/False,
1484                         /*text syms only*/True,
1485                         /*offsetP*/NULL );
1486}
1487
1488/* This is only available to core... don't demangle C++ names, but do
1489   do Z-demangling and below-main-renaming, match anywhere in function, and
1490   don't show offsets. */
1491Bool VG_(get_fnname_no_cxx_demangle) ( Addr a, Char* buf, Int nbuf )
1492{
1493   return get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/True,
1494                         /*below-main-renaming*/True,
1495                         a, buf, nbuf,
1496                         /*match_anywhere_in_fun*/True,
1497                         /*show offset?*/False,
1498                         /*text syms only*/True,
1499                         /*offsetP*/NULL );
1500}
1501
1502Vg_FnNameKind VG_(get_fnname_kind) ( Char* name )
1503{
1504   if (VG_STREQ("main", name)) {
1505      return Vg_FnNameMain;
1506
1507   } else if (
1508#      if defined(VGO_linux)
1509       VG_STREQ("__libc_start_main",  name) ||  // glibc glibness
1510       VG_STREQ("generic_start_main", name) ||  // Yellow Dog doggedness
1511#      elif defined(VGO_darwin)
1512       // See readmacho.c for an explanation of this.
1513       VG_STREQ("start_according_to_valgrind", name) ||  // Darwin, darling
1514#      else
1515#        error "Unknown OS"
1516#      endif
1517       0) {
1518      return Vg_FnNameBelowMain;
1519
1520   } else {
1521      return Vg_FnNameNormal;
1522   }
1523}
1524
1525Vg_FnNameKind VG_(get_fnname_kind_from_IP) ( Addr ip )
1526{
1527   // We don't need a big buffer;  all the special names are small.
1528   #define BUFLEN 50
1529   Char buf[50];
1530
1531   // We don't demangle, because it's faster not to, and the special names
1532   // we're looking for won't be demangled.
1533   if (VG_(get_fnname_raw) ( ip, buf, BUFLEN )) {
1534      buf[BUFLEN-1] = '\0';      // paranoia
1535      return VG_(get_fnname_kind)(buf);
1536   } else {
1537      return Vg_FnNameNormal;    // Don't know the name, treat it as normal.
1538   }
1539}
1540
1541/* Looks up data_addr in the collection of data symbols, and if found
1542   puts its name (or as much as will fit) into dname[0 .. n_dname-1],
1543   which is guaranteed to be zero terminated.  Also data_addr's offset
1544   from the symbol start is put into *offset. */
1545Bool VG_(get_datasym_and_offset)( Addr data_addr,
1546                                  /*OUT*/Char* dname, Int n_dname,
1547                                  /*OUT*/PtrdiffT* offset )
1548{
1549   Bool ok;
1550   vg_assert(n_dname > 1);
1551   ok = get_sym_name ( /*C++-demangle*/False, /*Z-demangle*/False,
1552                       /*below-main-renaming*/False,
1553                       data_addr, dname, n_dname,
1554                       /*match_anywhere_in_sym*/True,
1555                       /*show offset?*/False,
1556                       /*data syms only please*/False,
1557                       offset );
1558   if (!ok)
1559      return False;
1560   dname[n_dname-1] = 0;
1561   return True;
1562}
1563
1564/* Map a code address to the name of a shared object file or the
1565   executable.  Returns False if no idea; otherwise True.  Doesn't
1566   require debug info.  Caller supplies buf and nbuf. */
1567Bool VG_(get_objname) ( Addr a, Char* buf, Int nbuf )
1568{
1569   DebugInfo* di;
1570   const NSegment *seg;
1571   HChar* filename;
1572   vg_assert(nbuf > 0);
1573   /* Look in the debugInfo_list to find the name.  In most cases we
1574      expect this to produce a result. */
1575   for (di = debugInfo_list; di != NULL; di = di->next) {
1576      if (di->text_present
1577          && di->text_size > 0
1578          && di->text_avma <= a
1579          && a < di->text_avma + di->text_size) {
1580         VG_(strncpy_safely)(buf, di->fsm.filename, nbuf);
1581         buf[nbuf-1] = 0;
1582         return True;
1583      }
1584   }
1585   /* Last-ditch fallback position: if we don't find the address in
1586      the debugInfo_list, ask the address space manager whether it
1587      knows the name of the file associated with this mapping.  This
1588      allows us to print the names of exe/dll files in the stack trace
1589      when running programs under wine. */
1590   if ( (seg = VG_(am_find_nsegment(a))) != NULL
1591        && (filename = VG_(am_get_filename)(seg)) != NULL ) {
1592      VG_(strncpy_safely)(buf, filename, nbuf);
1593      return True;
1594   }
1595   return False;
1596}
1597
1598/* Map a code address to its DebugInfo.  Returns NULL if not found.  Doesn't
1599   require debug info. */
1600DebugInfo* VG_(find_DebugInfo) ( Addr a )
1601{
1602   static UWord n_search = 0;
1603   DebugInfo* di;
1604   n_search++;
1605   for (di = debugInfo_list; di != NULL; di = di->next) {
1606      if (di->text_present
1607          && di->text_size > 0
1608          && di->text_avma <= a
1609          && a < di->text_avma + di->text_size) {
1610         if (0 == (n_search & 0xF))
1611            move_DebugInfo_one_step_forward( di );
1612         return di;
1613      }
1614   }
1615   return NULL;
1616}
1617
1618/* Map a code address to a filename.  Returns True if successful.  */
1619Bool VG_(get_filename)( Addr a, Char* filename, Int n_filename )
1620{
1621   DebugInfo* si;
1622   Word       locno;
1623   search_all_loctabs ( a, &si, &locno );
1624   if (si == NULL)
1625      return False;
1626   VG_(strncpy_safely)(filename, si->loctab[locno].filename, n_filename);
1627   return True;
1628}
1629
1630/* Map a code address to a line number.  Returns True if successful. */
1631Bool VG_(get_linenum)( Addr a, UInt* lineno )
1632{
1633   DebugInfo* si;
1634   Word       locno;
1635   search_all_loctabs ( a, &si, &locno );
1636   if (si == NULL)
1637      return False;
1638   *lineno = si->loctab[locno].lineno;
1639
1640   return True;
1641}
1642
1643/* Map a code address to a filename/line number/dir name info.
1644   See prototype for detailed description of behaviour.
1645*/
1646Bool VG_(get_filename_linenum) ( Addr a,
1647                                 /*OUT*/Char* filename, Int n_filename,
1648                                 /*OUT*/Char* dirname,  Int n_dirname,
1649                                 /*OUT*/Bool* dirname_available,
1650                                 /*OUT*/UInt* lineno )
1651{
1652   DebugInfo* si;
1653   Word       locno;
1654
1655   vg_assert( (dirname == NULL && dirname_available == NULL)
1656              ||
1657              (dirname != NULL && dirname_available != NULL) );
1658
1659   search_all_loctabs ( a, &si, &locno );
1660   if (si == NULL) {
1661      if (dirname_available) {
1662         *dirname_available = False;
1663         *dirname = 0;
1664      }
1665      return False;
1666   }
1667
1668   VG_(strncpy_safely)(filename, si->loctab[locno].filename, n_filename);
1669   *lineno = si->loctab[locno].lineno;
1670
1671   if (dirname) {
1672      /* caller wants directory info too .. */
1673      vg_assert(n_dirname > 0);
1674      if (si->loctab[locno].dirname) {
1675         /* .. and we have some */
1676         *dirname_available = True;
1677         VG_(strncpy_safely)(dirname, si->loctab[locno].dirname,
1678                                      n_dirname);
1679      } else {
1680         /* .. but we don't have any */
1681         *dirname_available = False;
1682         *dirname = 0;
1683      }
1684   }
1685
1686   return True;
1687}
1688
1689
1690/* Map a function name to its entry point and toc pointer.  Is done by
1691   sequential search of all symbol tables, so is very slow.  To
1692   mitigate the worst performance effects, you may specify a soname
1693   pattern, and only objects matching that pattern are searched.
1694   Therefore specify "*" to search all the objects.  On TOC-afflicted
1695   platforms, a symbol is deemed to be found only if it has a nonzero
1696   TOC pointer.  */
1697Bool VG_(lookup_symbol_SLOW)(UChar* sopatt, UChar* name,
1698                             Addr* pEnt, Addr* pToc)
1699{
1700   Bool     require_pToc = False;
1701   Int      i;
1702   DebugInfo* si;
1703   Bool     debug = False;
1704#  if defined(VG_PLAT_USES_PPCTOC)
1705   require_pToc = True;
1706#  endif
1707   for (si = debugInfo_list; si; si = si->next) {
1708      if (debug)
1709         VG_(printf)("lookup_symbol_SLOW: considering %s\n", si->soname);
1710      if (!VG_(string_match)(sopatt, si->soname)) {
1711         if (debug)
1712            VG_(printf)(" ... skip\n");
1713         continue;
1714      }
1715      for (i = 0; i < si->symtab_used; i++) {
1716         UChar* pri_name = si->symtab[i].pri_name;
1717         tl_assert(pri_name);
1718         if (0==VG_(strcmp)(name, pri_name)
1719             && (require_pToc ? si->symtab[i].tocptr : True)) {
1720            *pEnt = si->symtab[i].addr;
1721            *pToc = si->symtab[i].tocptr;
1722            return True;
1723         }
1724         UChar** sec_names = si->symtab[i].sec_names;
1725         if (sec_names) {
1726            tl_assert(sec_names[0]);
1727            while (*sec_names) {
1728               if (0==VG_(strcmp)(name, *sec_names)
1729                   && (require_pToc ? si->symtab[i].tocptr : True)) {
1730                  *pEnt = si->symtab[i].addr;
1731                  *pToc = si->symtab[i].tocptr;
1732                  return True;
1733               }
1734               sec_names++;
1735            }
1736         }
1737      }
1738   }
1739   return False;
1740}
1741
1742
1743/* VG_(describe_IP): print into buf info on code address, function
1744   name and filename. */
1745
1746/* Copy str into buf starting at n, but not going past buf[n_buf-1]
1747   and always ensuring that buf is zero-terminated. */
1748
1749static Int putStr ( Int n, Int n_buf, Char* buf, Char* str )
1750{
1751   vg_assert(n_buf > 0);
1752   vg_assert(n >= 0 && n < n_buf);
1753   for (; n < n_buf-1 && *str != 0; n++,str++)
1754      buf[n] = *str;
1755   vg_assert(n >= 0 && n < n_buf);
1756   buf[n] = '\0';
1757   return n;
1758}
1759
1760/* Same as putStr, but escaping chars for XML output, and
1761   also not adding more than count chars to n_buf. */
1762
1763static Int putStrEsc ( Int n, Int n_buf, Int count, Char* buf, Char* str )
1764{
1765   Char alt[2];
1766   vg_assert(n_buf > 0);
1767   vg_assert(count >= 0 && count < n_buf);
1768   vg_assert(n >= 0 && n < n_buf);
1769   for (; *str != 0; str++) {
1770      vg_assert(count >= 0);
1771      if (count <= 0)
1772         goto done;
1773      switch (*str) {
1774         case '&':
1775            if (count < 5) goto done;
1776            n = putStr( n, n_buf, buf, "&amp;");
1777            count -= 5;
1778            break;
1779         case '<':
1780            if (count < 4) goto done;
1781            n = putStr( n, n_buf, buf, "&lt;");
1782            count -= 4;
1783            break;
1784         case '>':
1785            if (count < 4) goto done;
1786            n = putStr( n, n_buf, buf, "&gt;");
1787            count -= 4;
1788            break;
1789         default:
1790            if (count < 1) goto done;
1791            alt[0] = *str;
1792            alt[1] = 0;
1793            n = putStr( n, n_buf, buf, alt );
1794            count -= 1;
1795            break;
1796      }
1797   }
1798  done:
1799   vg_assert(count >= 0); /* should not go -ve in loop */
1800   vg_assert(n >= 0 && n < n_buf);
1801   return n;
1802}
1803
1804Char* VG_(describe_IP)(Addr eip, Char* buf, Int n_buf)
1805{
1806#  define APPEND(_str) \
1807      n = putStr(n, n_buf, buf, _str)
1808#  define APPEND_ESC(_count,_str) \
1809      n = putStrEsc(n, n_buf, (_count), buf, (_str))
1810#  define BUF_LEN    4096
1811
1812   UInt  lineno;
1813   UChar ibuf[50];
1814   Int   n = 0;
1815
1816   static UChar buf_fn[BUF_LEN];
1817   static UChar buf_obj[BUF_LEN];
1818   static UChar buf_srcloc[BUF_LEN];
1819   static UChar buf_dirname[BUF_LEN];
1820   buf_fn[0] = buf_obj[0] = buf_srcloc[0] = buf_dirname[0] = 0;
1821
1822   Bool  know_dirinfo = False;
1823   Bool  know_fnname  = VG_(clo_sym_offsets)
1824                        ? VG_(get_fnname_w_offset) (eip, buf_fn, BUF_LEN)
1825                        : VG_(get_fnname) (eip, buf_fn, BUF_LEN);
1826   Bool  know_objname = VG_(get_objname)(eip, buf_obj, BUF_LEN);
1827   Bool  know_srcloc  = VG_(get_filename_linenum)(
1828                           eip,
1829                           buf_srcloc,  BUF_LEN,
1830                           buf_dirname, BUF_LEN, &know_dirinfo,
1831                           &lineno
1832                        );
1833   buf_fn     [ sizeof(buf_fn)-1      ]  = 0;
1834   buf_obj    [ sizeof(buf_obj)-1     ]  = 0;
1835   buf_srcloc [ sizeof(buf_srcloc)-1  ]  = 0;
1836   buf_dirname[ sizeof(buf_dirname)-1 ]  = 0;
1837
1838   if (VG_(clo_xml)) {
1839
1840      Bool   human_readable = True;
1841      HChar* maybe_newline  = human_readable ? "\n      " : "";
1842      HChar* maybe_newline2 = human_readable ? "\n    "   : "";
1843
1844      /* Print in XML format, dumping in as much info as we know.
1845         Ensure all tags are balanced even if the individual strings
1846         are too long.  Allocate 1/10 of BUF_LEN to the object name,
1847         6/10s to the function name, 1/10 to the directory name and
1848         1/10 to the file name, leaving 1/10 for all the fixed-length
1849         stuff. */
1850      APPEND("<frame>");
1851      VG_(sprintf)(ibuf,"<ip>0x%llX</ip>", (ULong)eip);
1852      APPEND(maybe_newline);
1853      APPEND(ibuf);
1854      if (know_objname) {
1855         APPEND(maybe_newline);
1856         APPEND("<obj>");
1857         APPEND_ESC(1*BUF_LEN/10, buf_obj);
1858         APPEND("</obj>");
1859      }
1860      if (know_fnname) {
1861         APPEND(maybe_newline);
1862         APPEND("<fn>");
1863         APPEND_ESC(6*BUF_LEN/10, buf_fn);
1864         APPEND("</fn>");
1865      }
1866      if (know_srcloc) {
1867         if (know_dirinfo) {
1868            APPEND(maybe_newline);
1869            APPEND("<dir>");
1870            APPEND_ESC(1*BUF_LEN/10, buf_dirname);
1871            APPEND("</dir>");
1872         }
1873         APPEND(maybe_newline);
1874         APPEND("<file>");
1875         APPEND_ESC(1*BUF_LEN/10, buf_srcloc);
1876         APPEND("</file>");
1877         APPEND(maybe_newline);
1878         APPEND("<line>");
1879         VG_(sprintf)(ibuf,"%d",lineno);
1880         APPEND(ibuf);
1881         APPEND("</line>");
1882      }
1883      APPEND(maybe_newline2);
1884      APPEND("</frame>");
1885
1886   } else {
1887
1888      /* Print for humans to read */
1889      //
1890      // Possible forms:
1891      //
1892      //   0x80483BF: really (a.c:20)
1893      //   0x80483BF: really (in /foo/a.out)
1894      //   0x80483BF: really (in ???)
1895      //   0x80483BF: ??? (in /foo/a.out)
1896      //   0x80483BF: ??? (a.c:20)
1897      //   0x80483BF: ???
1898      //
1899      VG_(sprintf)(ibuf,"0x%llX: ", (ULong)eip);
1900      APPEND(ibuf);
1901      if (know_fnname) {
1902         APPEND(buf_fn);
1903      } else {
1904         APPEND("???");
1905      }
1906      if (know_srcloc) {
1907         APPEND(" (");
1908         // Get the directory name, if any, possibly pruned, into dirname.
1909         UChar* dirname = NULL;
1910         if (VG_(clo_n_fullpath_after) > 0) {
1911            Int i;
1912            dirname = buf_dirname;
1913            // Remove leading prefixes from the dirname.
1914            // If user supplied --fullpath-after=foo, this will remove
1915            // a leading string which matches '.*foo' (not greedy).
1916            for (i = 0; i < VG_(clo_n_fullpath_after); i++) {
1917               UChar* prefix = VG_(clo_fullpath_after)[i];
1918               UChar* str    = VG_(strstr)(dirname, prefix);
1919               if (str) {
1920                  dirname = str + VG_(strlen)(prefix);
1921                  break;
1922               }
1923            }
1924            /* remove leading "./" */
1925            if (dirname[0] == '.' && dirname[1] == '/')
1926               dirname += 2;
1927         }
1928         // do we have any interesting directory name to show?  If so
1929         // add it in.
1930         if (dirname && dirname[0] != 0) {
1931            APPEND(dirname);
1932            APPEND("/");
1933         }
1934         APPEND(buf_srcloc);
1935         APPEND(":");
1936         VG_(sprintf)(ibuf,"%d",lineno);
1937         APPEND(ibuf);
1938         APPEND(")");
1939      } else if (know_objname) {
1940         APPEND(" (in ");
1941         APPEND(buf_obj);
1942         APPEND(")");
1943      } else if (know_fnname) {
1944         // Nb: do this in two steps because "??)" is a trigraph!
1945         APPEND(" (in ???");
1946         APPEND(")");
1947      }
1948
1949   }
1950   return buf;
1951
1952#  undef APPEND
1953#  undef APPEND_ESC
1954#  undef BUF_LEN
1955}
1956
1957
1958/*--------------------------------------------------------------*/
1959/*---                                                        ---*/
1960/*--- TOP LEVEL: FOR UNWINDING THE STACK USING               ---*/
1961/*---            DWARF3 .eh_frame INFO                       ---*/
1962/*---                                                        ---*/
1963/*--------------------------------------------------------------*/
1964
1965/* Gather up all the constant pieces of info needed to evaluate
1966   a CfiExpr into one convenient struct. */
1967typedef
1968   struct {
1969      D3UnwindRegs* uregs;
1970      Addr          min_accessible;
1971      Addr          max_accessible;
1972   }
1973   CfiExprEvalContext;
1974
1975/* Evaluate the CfiExpr rooted at ix in exprs given the context eec.
1976   *ok is set to False on failure, but not to True on success.  The
1977   caller must set it to True before calling. */
1978__attribute__((noinline))
1979static
1980UWord evalCfiExpr ( XArray* exprs, Int ix,
1981                    CfiExprEvalContext* eec, Bool* ok )
1982{
1983   UWord wL, wR;
1984   Addr  a;
1985   CfiExpr* e;
1986   vg_assert(sizeof(Addr) == sizeof(UWord));
1987   e = VG_(indexXA)( exprs, ix );
1988   switch (e->tag) {
1989      case Cex_Binop:
1990         wL = evalCfiExpr( exprs, e->Cex.Binop.ixL, eec, ok );
1991         if (!(*ok)) return 0;
1992         wR = evalCfiExpr( exprs, e->Cex.Binop.ixR, eec, ok );
1993         if (!(*ok)) return 0;
1994         switch (e->Cex.Binop.op) {
1995            case Cop_Add: return wL + wR;
1996            case Cop_Sub: return wL - wR;
1997            case Cop_And: return wL & wR;
1998            case Cop_Mul: return wL * wR;
1999            case Cop_Shl: return wL << wR;
2000            case Cop_Shr: return wL >> wR;
2001            case Cop_Eq: return wL == wR ? 1 : 0;
2002            case Cop_Ge: return (Word) wL >= (Word) wR ? 1 : 0;
2003            case Cop_Gt: return (Word) wL > (Word) wR ? 1 : 0;
2004            case Cop_Le: return (Word) wL <= (Word) wR ? 1 : 0;
2005            case Cop_Lt: return (Word) wL < (Word) wR ? 1 : 0;
2006            case Cop_Ne: return wL != wR ? 1 : 0;
2007            default: goto unhandled;
2008         }
2009         /*NOTREACHED*/
2010      case Cex_CfiReg:
2011         switch (e->Cex.CfiReg.reg) {
2012#           if defined(VGA_x86) || defined(VGA_amd64)
2013            case Creg_IA_IP: return eec->uregs->xip;
2014            case Creg_IA_SP: return eec->uregs->xsp;
2015            case Creg_IA_BP: return eec->uregs->xbp;
2016#           elif defined(VGA_arm)
2017            case Creg_ARM_R15: return eec->uregs->r15;
2018            case Creg_ARM_R14: return eec->uregs->r14;
2019            case Creg_ARM_R13: return eec->uregs->r13;
2020            case Creg_ARM_R12: return eec->uregs->r12;
2021#           elif defined(VGA_s390x)
2022            case Creg_IA_IP: return eec->uregs->ia;
2023            case Creg_IA_SP: return eec->uregs->sp;
2024            case Creg_IA_BP: return eec->uregs->fp;
2025            case Creg_S390_R14: return eec->uregs->lr;
2026#           elif defined(VGA_ppc32) || defined(VGA_ppc64)
2027#           else
2028#             error "Unsupported arch"
2029#           endif
2030            default: goto unhandled;
2031         }
2032         /*NOTREACHED*/
2033      case Cex_Const:
2034         return e->Cex.Const.con;
2035      case Cex_Deref:
2036         a = evalCfiExpr( exprs, e->Cex.Deref.ixAddr, eec, ok );
2037         if (!(*ok)) return 0;
2038         if (a < eec->min_accessible
2039             || (a + sizeof(UWord) - 1) > eec->max_accessible) {
2040            *ok = False;
2041            return 0;
2042         }
2043         /* let's hope it doesn't trap! */
2044         return ML_(read_UWord)((void *)a);
2045      default:
2046         goto unhandled;
2047   }
2048   /*NOTREACHED*/
2049  unhandled:
2050   VG_(printf)("\n\nevalCfiExpr: unhandled\n");
2051   ML_(ppCfiExpr)( exprs, ix );
2052   VG_(printf)("\n");
2053   vg_assert(0);
2054   /*NOTREACHED*/
2055   return 0;
2056}
2057
2058
2059/* Search all the DebugInfos in the entire system, to find the DiCfSI
2060   that pertains to 'ip'.
2061
2062   If found, set *diP to the DebugInfo in which it resides, and
2063   *ixP to the index in that DebugInfo's cfsi array.
2064
2065   If not found, set *diP to (DebugInfo*)1 and *ixP to zero.
2066*/
2067__attribute__((noinline))
2068static void find_DiCfSI ( /*OUT*/DebugInfo** diP,
2069                          /*OUT*/Word* ixP,
2070                          Addr ip )
2071{
2072   DebugInfo* di;
2073   Word       i = -1;
2074
2075   static UWord n_search = 0;
2076   static UWord n_steps = 0;
2077   n_search++;
2078
2079   if (0) VG_(printf)("search for %#lx\n", ip);
2080
2081   for (di = debugInfo_list; di != NULL; di = di->next) {
2082      Word j;
2083      n_steps++;
2084
2085      /* Use the per-DebugInfo summary address ranges to skip
2086         inapplicable DebugInfos quickly. */
2087      if (di->cfsi_used == 0)
2088         continue;
2089      if (ip < di->cfsi_minavma || ip > di->cfsi_maxavma)
2090         continue;
2091
2092      /* It might be in this DebugInfo.  Search it. */
2093      j = ML_(search_one_cfitab)( di, ip );
2094      vg_assert(j >= -1 && j < (Word)di->cfsi_used);
2095
2096      if (j != -1) {
2097         i = j;
2098         break; /* found it */
2099      }
2100   }
2101
2102   if (i == -1) {
2103
2104      /* we didn't find it. */
2105      *diP = (DebugInfo*)1;
2106      *ixP = 0;
2107
2108   } else {
2109
2110      /* found it. */
2111      /* ensure that di is 4-aligned (at least), so it can't possibly
2112         be equal to (DebugInfo*)1. */
2113      vg_assert(di && VG_IS_4_ALIGNED(di));
2114      vg_assert(i >= 0 && i < di->cfsi_used);
2115      *diP = di;
2116      *ixP = i;
2117
2118      /* Start of performance-enhancing hack: once every 64 (chosen
2119         hackily after profiling) successful searches, move the found
2120         DebugInfo one step closer to the start of the list.  This
2121         makes future searches cheaper.  For starting konqueror on
2122         amd64, this in fact reduces the total amount of searching
2123         done by the above find-the-right-DebugInfo loop by more than
2124         a factor of 20. */
2125      if ((n_search & 0xF) == 0) {
2126         /* Move di one step closer to the start of the list. */
2127         move_DebugInfo_one_step_forward( di );
2128      }
2129      /* End of performance-enhancing hack. */
2130
2131      if (0 && ((n_search & 0x7FFFF) == 0))
2132         VG_(printf)("find_DiCfSI: %lu searches, "
2133                     "%lu DebugInfos looked at\n",
2134                     n_search, n_steps);
2135
2136   }
2137
2138}
2139
2140
2141/* Now follows a mechanism for caching queries to find_DiCfSI, since
2142   they are extremely frequent on amd64-linux, during stack unwinding.
2143
2144   Each cache entry binds an ip value to a (di, ix) pair.  Possible
2145   values:
2146
2147   di is non-null, ix >= 0  ==>  cache slot in use, "di->cfsi[ix]"
2148   di is (DebugInfo*)1      ==>  cache slot in use, no associated di
2149   di is NULL               ==>  cache slot not in use
2150
2151   Hence simply zeroing out the entire cache invalidates all
2152   entries.
2153
2154   Why not map ip values directly to DiCfSI*'s?  Because this would
2155   cause problems if/when the cfsi array is moved due to resizing.
2156   Instead we cache .cfsi array index value, which should be invariant
2157   across resizing.  (That said, I don't think the current
2158   implementation will resize whilst during queries, since the DiCfSI
2159   records are added all at once, when the debuginfo for an object is
2160   read, and is not changed ever thereafter. */
2161
2162#define N_CFSI_CACHE 511
2163
2164typedef
2165   struct { Addr ip; DebugInfo* di; Word ix; }
2166   CFSICacheEnt;
2167
2168static CFSICacheEnt cfsi_cache[N_CFSI_CACHE];
2169
2170static void cfsi_cache__invalidate ( void ) {
2171   VG_(memset)(&cfsi_cache, 0, sizeof(cfsi_cache));
2172}
2173
2174
2175static inline CFSICacheEnt* cfsi_cache__find ( Addr ip )
2176{
2177   UWord         hash = ip % N_CFSI_CACHE;
2178   CFSICacheEnt* ce = &cfsi_cache[hash];
2179   static UWord  n_q = 0, n_m = 0;
2180
2181   n_q++;
2182   if (0 && 0 == (n_q & 0x1FFFFF))
2183      VG_(printf)("QQQ %lu %lu\n", n_q, n_m);
2184
2185   if (LIKELY(ce->ip == ip) && LIKELY(ce->di != NULL)) {
2186      /* found an entry in the cache .. */
2187   } else {
2188      /* not found in cache.  Search and update. */
2189      n_m++;
2190      ce->ip = ip;
2191      find_DiCfSI( &ce->di, &ce->ix, ip );
2192   }
2193
2194   if (UNLIKELY(ce->di == (DebugInfo*)1)) {
2195      /* no DiCfSI for this address */
2196      return NULL;
2197   } else {
2198      /* found a DiCfSI for this address */
2199      return ce;
2200   }
2201}
2202
2203
2204inline
2205static Addr compute_cfa ( D3UnwindRegs* uregs,
2206                          Addr min_accessible, Addr max_accessible,
2207                          DebugInfo* di, DiCfSI* cfsi )
2208{
2209   CfiExprEvalContext eec;
2210   Addr               cfa;
2211   Bool               ok;
2212
2213   /* Compute the CFA. */
2214   cfa = 0;
2215   switch (cfsi->cfa_how) {
2216#     if defined(VGA_x86) || defined(VGA_amd64)
2217      case CFIC_IA_SPREL:
2218         cfa = cfsi->cfa_off + uregs->xsp;
2219         break;
2220      case CFIC_IA_BPREL:
2221         cfa = cfsi->cfa_off + uregs->xbp;
2222         break;
2223#     elif defined(VGA_arm)
2224      case CFIC_ARM_R13REL:
2225         cfa = cfsi->cfa_off + uregs->r13;
2226         break;
2227      case CFIC_ARM_R12REL:
2228         cfa = cfsi->cfa_off + uregs->r12;
2229         break;
2230      case CFIC_ARM_R11REL:
2231         cfa = cfsi->cfa_off + uregs->r11;
2232         break;
2233      case CFIC_ARM_R7REL:
2234         cfa = cfsi->cfa_off + uregs->r7;
2235         break;
2236#     elif defined(VGA_s390x)
2237      case CFIC_IA_SPREL:
2238         cfa = cfsi->cfa_off + uregs->sp;
2239         break;
2240      case CFIR_MEMCFAREL:
2241      {
2242         Addr a = uregs->sp + cfsi->cfa_off;
2243         if (a < min_accessible || a > max_accessible-sizeof(Addr))
2244            break;
2245         cfa = ML_(read_Addr)((void *)a);
2246         break;
2247      }
2248      case CFIR_SAME:
2249         cfa = uregs->fp;
2250         break;
2251      case CFIC_IA_BPREL:
2252         cfa = cfsi->cfa_off + uregs->fp;
2253         break;
2254#     elif defined(VGA_ppc32) || defined(VGA_ppc64)
2255#     else
2256#       error "Unsupported arch"
2257#     endif
2258      case CFIC_EXPR: /* available on all archs */
2259         if (0) {
2260            VG_(printf)("CFIC_EXPR: ");
2261            ML_(ppCfiExpr)(di->cfsi_exprs, cfsi->cfa_off);
2262            VG_(printf)("\n");
2263         }
2264         eec.uregs          = uregs;
2265         eec.min_accessible = min_accessible;
2266         eec.max_accessible = max_accessible;
2267         ok = True;
2268         cfa = evalCfiExpr(di->cfsi_exprs, cfsi->cfa_off, &eec, &ok );
2269         if (!ok) return 0;
2270         break;
2271      default:
2272         vg_assert(0);
2273   }
2274   return cfa;
2275}
2276
2277
2278/* Get the call frame address (CFA) given an IP/SP/FP triple. */
2279/* NOTE: This function may rearrange the order of entries in the
2280   DebugInfo list. */
2281Addr ML_(get_CFA) ( Addr ip, Addr sp, Addr fp,
2282                    Addr min_accessible, Addr max_accessible )
2283{
2284   CFSICacheEnt* ce;
2285   DebugInfo*    di;
2286   DiCfSI*       cfsi __attribute__((unused));
2287
2288   ce = cfsi_cache__find(ip);
2289
2290   if (UNLIKELY(ce == NULL))
2291      return 0; /* no info.  Nothing we can do. */
2292
2293   di = ce->di;
2294   cfsi = &di->cfsi[ ce->ix ];
2295
2296   /* Temporary impedance-matching kludge so that this keeps working
2297      on x86-linux and amd64-linux. */
2298#  if defined(VGA_x86) || defined(VGA_amd64)
2299   { D3UnwindRegs uregs;
2300     uregs.xip = ip;
2301     uregs.xsp = sp;
2302     uregs.xbp = fp;
2303     return compute_cfa(&uregs,
2304                        min_accessible,  max_accessible, di, cfsi);
2305   }
2306#elif defined(VGA_s390x)
2307   { D3UnwindRegs uregs;
2308     uregs.ia = ip;
2309     uregs.sp = sp;
2310     uregs.fp = fp;
2311     return compute_cfa(&uregs,
2312                        min_accessible,  max_accessible, di, cfsi);
2313   }
2314
2315#  else
2316   return 0; /* indicates failure */
2317#  endif
2318}
2319
2320
2321/* The main function for DWARF2/3 CFI-based stack unwinding.  Given a
2322   set of registers in UREGS, modify it to hold the register values
2323   for the previous frame, if possible.  Returns True if successful.
2324   If not successful, *UREGS is not changed.
2325
2326   For x86 and amd64, the unwound registers are: {E,R}IP,
2327   {E,R}SP, {E,R}BP.
2328
2329   For arm, the unwound registers are: R7 R11 R12 R13 R14 R15.
2330*/
2331Bool VG_(use_CF_info) ( /*MOD*/D3UnwindRegs* uregsHere,
2332                        Addr min_accessible,
2333                        Addr max_accessible )
2334{
2335   DebugInfo*         di;
2336   DiCfSI*            cfsi = NULL;
2337   Addr               cfa, ipHere = 0;
2338   CFSICacheEnt*      ce;
2339   CfiExprEvalContext eec __attribute__((unused));
2340   D3UnwindRegs       uregsPrev;
2341
2342#  if defined(VGA_x86) || defined(VGA_amd64)
2343   ipHere = uregsHere->xip;
2344#  elif defined(VGA_arm)
2345   ipHere = uregsHere->r15;
2346#  elif defined(VGA_s390x)
2347   ipHere = uregsHere->ia;
2348#  elif defined(VGA_ppc32) || defined(VGA_ppc64)
2349#  else
2350#    error "Unknown arch"
2351#  endif
2352   ce = cfsi_cache__find(ipHere);
2353
2354   if (UNLIKELY(ce == NULL))
2355      return False; /* no info.  Nothing we can do. */
2356
2357   di = ce->di;
2358   cfsi = &di->cfsi[ ce->ix ];
2359
2360   if (0) {
2361      VG_(printf)("found cfisi: ");
2362      ML_(ppDiCfSI)(di->cfsi_exprs, cfsi);
2363   }
2364
2365   VG_(bzero_inline)(&uregsPrev, sizeof(uregsPrev));
2366
2367   /* First compute the CFA. */
2368   cfa = compute_cfa(uregsHere,
2369                     min_accessible, max_accessible, di, cfsi);
2370   if (UNLIKELY(cfa == 0))
2371      return False;
2372
2373   /* Now we know the CFA, use it to roll back the registers we're
2374      interested in. */
2375
2376#  define COMPUTE(_prev, _here, _how, _off)             \
2377      do {                                              \
2378         switch (_how) {                                \
2379            case CFIR_UNKNOWN:                          \
2380               return False;                            \
2381            case CFIR_SAME:                             \
2382               _prev = _here; break;                    \
2383            case CFIR_MEMCFAREL: {                      \
2384               Addr a = cfa + (Word)_off;               \
2385               if (a < min_accessible                   \
2386                   || a > max_accessible-sizeof(Addr))  \
2387                  return False;                         \
2388               _prev = ML_(read_Addr)((void *)a);       \
2389               break;                                   \
2390            }                                           \
2391            case CFIR_CFAREL:                           \
2392               _prev = cfa + (Word)_off;                \
2393               break;                                   \
2394            case CFIR_EXPR:                             \
2395               if (0)                                   \
2396                  ML_(ppCfiExpr)(di->cfsi_exprs,_off);  \
2397               eec.uregs = uregsHere;                   \
2398               eec.min_accessible = min_accessible;     \
2399               eec.max_accessible = max_accessible;     \
2400               Bool ok = True;                          \
2401               _prev = evalCfiExpr(di->cfsi_exprs, _off, &eec, &ok ); \
2402               if (!ok) return False;                   \
2403               break;                                   \
2404            default:                                    \
2405               vg_assert(0);                            \
2406         }                                              \
2407      } while (0)
2408
2409#  if defined(VGA_x86) || defined(VGA_amd64)
2410   COMPUTE(uregsPrev.xip, uregsHere->xip, cfsi->ra_how, cfsi->ra_off);
2411   COMPUTE(uregsPrev.xsp, uregsHere->xsp, cfsi->sp_how, cfsi->sp_off);
2412   COMPUTE(uregsPrev.xbp, uregsHere->xbp, cfsi->bp_how, cfsi->bp_off);
2413#  elif defined(VGA_arm)
2414   COMPUTE(uregsPrev.r15, uregsHere->r15, cfsi->ra_how,  cfsi->ra_off);
2415   COMPUTE(uregsPrev.r14, uregsHere->r14, cfsi->r14_how, cfsi->r14_off);
2416   COMPUTE(uregsPrev.r13, uregsHere->r13, cfsi->r13_how, cfsi->r13_off);
2417   COMPUTE(uregsPrev.r12, uregsHere->r12, cfsi->r12_how, cfsi->r12_off);
2418   COMPUTE(uregsPrev.r11, uregsHere->r11, cfsi->r11_how, cfsi->r11_off);
2419   COMPUTE(uregsPrev.r7,  uregsHere->r7,  cfsi->r7_how,  cfsi->r7_off);
2420#  elif defined(VGA_s390x)
2421   COMPUTE(uregsPrev.ia, uregsHere->ia, cfsi->ra_how, cfsi->ra_off);
2422   COMPUTE(uregsPrev.sp, uregsHere->sp, cfsi->sp_how, cfsi->sp_off);
2423   COMPUTE(uregsPrev.fp, uregsHere->fp, cfsi->fp_how, cfsi->fp_off);
2424#  elif defined(VGA_ppc32) || defined(VGA_ppc64)
2425#  else
2426#    error "Unknown arch"
2427#  endif
2428
2429#  undef COMPUTE
2430
2431   *uregsHere = uregsPrev;
2432   return True;
2433}
2434
2435
2436/*--------------------------------------------------------------*/
2437/*---                                                        ---*/
2438/*--- TOP LEVEL: FOR UNWINDING THE STACK USING               ---*/
2439/*---            MSVC FPO INFO                               ---*/
2440/*---                                                        ---*/
2441/*--------------------------------------------------------------*/
2442
2443Bool VG_(use_FPO_info) ( /*MOD*/Addr* ipP,
2444                         /*MOD*/Addr* spP,
2445                         /*MOD*/Addr* fpP,
2446                         Addr min_accessible,
2447                         Addr max_accessible )
2448{
2449   Word       i;
2450   DebugInfo* di;
2451   FPO_DATA*  fpo = NULL;
2452   Addr       spHere;
2453
2454   static UWord n_search = 0;
2455   static UWord n_steps = 0;
2456   n_search++;
2457
2458   if (0) VG_(printf)("search FPO for %#lx\n", *ipP);
2459
2460   for (di = debugInfo_list; di != NULL; di = di->next) {
2461      n_steps++;
2462
2463      /* Use the per-DebugInfo summary address ranges to skip
2464         inapplicable DebugInfos quickly. */
2465      if (di->fpo == NULL)
2466         continue;
2467      if (*ipP < di->fpo_minavma || *ipP > di->fpo_maxavma)
2468         continue;
2469
2470      i = ML_(search_one_fpotab)( di, *ipP );
2471      if (i != -1) {
2472         Word j;
2473         if (0) {
2474            /* debug printing only */
2475            VG_(printf)("look for %#lx  size %ld i %ld\n",
2476                        *ipP, di->fpo_size, i);
2477            for (j = 0; j < di->fpo_size; j++)
2478               VG_(printf)("[%02ld] %#x %d\n",
2479                            j, di->fpo[j].ulOffStart, di->fpo[j].cbProcSize);
2480         }
2481         vg_assert(i >= 0 && i < di->fpo_size);
2482         fpo = &di->fpo[i];
2483         break;
2484      }
2485   }
2486
2487   if (fpo == NULL)
2488      return False;
2489
2490   if (0 && ((n_search & 0x7FFFF) == 0))
2491      VG_(printf)("VG_(use_FPO_info): %lu searches, "
2492                  "%lu DebugInfos looked at\n",
2493                  n_search, n_steps);
2494
2495
2496   /* Start of performance-enhancing hack: once every 64 (chosen
2497      hackily after profiling) successful searches, move the found
2498      DebugInfo one step closer to the start of the list.  This makes
2499      future searches cheaper.  For starting konqueror on amd64, this
2500      in fact reduces the total amount of searching done by the above
2501      find-the-right-DebugInfo loop by more than a factor of 20. */
2502   if ((n_search & 0x3F) == 0) {
2503      /* Move si one step closer to the start of the list. */
2504      //move_DebugInfo_one_step_forward( di );
2505   }
2506   /* End of performance-enhancing hack. */
2507
2508   if (0) {
2509      VG_(printf)("found fpo: ");
2510      //ML_(ppFPO)(fpo);
2511   }
2512
2513   /*
2514   Stack layout is:
2515   %esp->
2516      4*.cbRegs  {%edi, %esi, %ebp, %ebx}
2517      4*.cdwLocals
2518      return_pc
2519      4*.cdwParams
2520   prior_%esp->
2521
2522   Typical code looks like:
2523      sub $4*.cdwLocals,%esp
2524         Alternative to above for >=4KB (and sometimes for smaller):
2525            mov $size,%eax
2526            call __chkstk  # WinNT performs page-by-page probe!
2527               __chkstk is much like alloc(), except that on return
2528               %eax= 5+ &CALL.  Thus it could be used as part of
2529               Position Independent Code to locate the Global Offset Table.
2530      push %ebx
2531      push %ebp
2532      push %esi
2533         Other once-only instructions often scheduled >here<.
2534      push %edi
2535
2536   If the pc is within the first .cbProlog bytes of the function,
2537   then you must disassemble to see how many registers have been pushed,
2538   because instructions in the prolog may be scheduled for performance.
2539   The order of PUSH is always %ebx, %ebp, %esi, %edi, with trailing
2540   registers not pushed when .cbRegs < 4.  This seems somewhat strange
2541   because %ebp is the register whose usage you want to minimize,
2542   yet it is in the first half of the PUSH list.
2543
2544   I don't know what happens when the compiler constructs an outgoing CALL.
2545   %esp could move if outgoing parameters are PUSHed, and this affects
2546   traceback for errors during the PUSHes. */
2547
2548   spHere = *spP;
2549
2550   *ipP = ML_(read_Addr)((void *)(spHere + 4*(fpo->cbRegs + fpo->cdwLocals)));
2551   *spP =                         spHere + 4*(fpo->cbRegs + fpo->cdwLocals + 1
2552                                                          + fpo->cdwParams);
2553   *fpP = ML_(read_Addr)((void *)(spHere + 4*2));
2554   return True;
2555}
2556
2557
2558/*--------------------------------------------------------------*/
2559/*---                                                        ---*/
2560/*--- TOP LEVEL: GENERATE DESCRIPTION OF DATA ADDRESSES      ---*/
2561/*---            FROM DWARF3 DEBUG INFO                      ---*/
2562/*---                                                        ---*/
2563/*--------------------------------------------------------------*/
2564
2565/* Try to make p2XA(dst, fmt, args..) turn into
2566   VG_(xaprintf_no_f_c)(dst, fmt, args) without having to resort to
2567   vararg macros.  As usual with everything to do with varargs, it's
2568   an ugly hack.
2569
2570   //#define p2XA(dstxa, format, args...)
2571   //   VG_(xaprintf_no_f_c)(dstxa, format, ##args)
2572*/
2573#define  p2XA  VG_(xaprintf_no_f_c)
2574
2575/* Add a zero-terminating byte to DST, which must be an XArray* of
2576   HChar. */
2577static void zterm_XA ( XArray* dst )
2578{
2579   HChar zero = 0;
2580   (void) VG_(addBytesToXA)( dst, &zero, 1 );
2581}
2582
2583
2584/* Evaluate the location expression/list for var, to see whether or
2585   not data_addr falls within the variable.  If so also return the
2586   offset of data_addr from the start of the variable.  Note that
2587   regs, which supplies ip,sp,fp values, will be NULL for global
2588   variables, and non-NULL for local variables. */
2589static Bool data_address_is_in_var ( /*OUT*/PtrdiffT* offset,
2590                                     XArray* /* TyEnt */ tyents,
2591                                     DiVariable*   var,
2592                                     RegSummary*   regs,
2593                                     Addr          data_addr,
2594                                     const DebugInfo* di )
2595{
2596   MaybeULong mul;
2597   SizeT      var_szB;
2598   GXResult   res;
2599   Bool       show = False;
2600
2601   vg_assert(var->name);
2602   vg_assert(var->gexpr);
2603
2604   /* Figure out how big the variable is. */
2605   mul = ML_(sizeOfType)(tyents, var->typeR);
2606   /* If this var has a type whose size is unknown, zero, or
2607      impossibly large, it should never have been added.  ML_(addVar)
2608      should have rejected it. */
2609   vg_assert(mul.b == True);
2610   vg_assert(mul.ul > 0);
2611   if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
2612   /* After this point, we assume we can truncate mul.ul to a host word
2613      safely (without loss of info). */
2614
2615   var_szB = (SizeT)mul.ul; /* NB: truncate to host word */
2616
2617   if (show) {
2618      VG_(printf)("VVVV: data_address_%#lx_is_in_var: %s :: ",
2619                  data_addr, var->name );
2620      ML_(pp_TyEnt_C_ishly)( tyents, var->typeR );
2621      VG_(printf)("\n");
2622   }
2623
2624   /* ignore zero-sized vars; they can never match anything. */
2625   if (var_szB == 0) {
2626      if (show)
2627         VG_(printf)("VVVV: -> Fail (variable is zero sized)\n");
2628      return False;
2629   }
2630
2631   res = ML_(evaluate_GX)( var->gexpr, var->fbGX, regs, di );
2632
2633   if (show) {
2634      VG_(printf)("VVVV: -> ");
2635      ML_(pp_GXResult)( res );
2636      VG_(printf)("\n");
2637   }
2638
2639   if (res.kind == GXR_Addr
2640       && res.word <= data_addr
2641       && data_addr < res.word + var_szB) {
2642      *offset = data_addr - res.word;
2643      return True;
2644   } else {
2645      return False;
2646   }
2647}
2648
2649
2650/* Format the acquired information into DN(AME)1 and DN(AME)2, which
2651   are XArray*s of HChar, that have been initialised by the caller.
2652   Resulting strings will be zero terminated.  Information is
2653   formatted in an understandable way.  Not so easy.  If frameNo is
2654   -1, this is assumed to be a global variable; else a local
2655   variable. */
2656static void format_message ( /*MOD*/XArray* /* of HChar */ dn1,
2657                             /*MOD*/XArray* /* of HChar */ dn2,
2658                             Addr     data_addr,
2659                             DiVariable* var,
2660                             PtrdiffT var_offset,
2661                             PtrdiffT residual_offset,
2662                             XArray* /*UChar*/ described,
2663                             Int      frameNo,
2664                             ThreadId tid )
2665{
2666   Bool   have_descr, have_srcloc;
2667   Bool   xml       = VG_(clo_xml);
2668   UChar* vo_plural = var_offset == 1 ? "" : "s";
2669   UChar* ro_plural = residual_offset == 1 ? "" : "s";
2670   UChar* basetag   = "auxwhat"; /* a constant */
2671   UChar tagL[32], tagR[32], xagL[32], xagR[32];
2672
2673   if (frameNo < -1) {
2674      vg_assert(0); /* Not allowed */
2675   }
2676   else if (frameNo == -1) {
2677      vg_assert(tid == VG_INVALID_THREADID);
2678   }
2679   else /* (frameNo >= 0) */ {
2680      vg_assert(tid != VG_INVALID_THREADID);
2681   }
2682
2683   vg_assert(dn1 && dn2);
2684   vg_assert(described);
2685   vg_assert(var && var->name);
2686   have_descr = VG_(sizeXA)(described) > 0
2687                && *(UChar*)VG_(indexXA)(described,0) != '\0';
2688   have_srcloc = var->fileName && var->lineNo > 0;
2689
2690   tagL[0] = tagR[0] = xagL[0] = xagR[0] = 0;
2691   if (xml) {
2692      VG_(sprintf)(tagL, "<%s>",   basetag); // <auxwhat>
2693      VG_(sprintf)(tagR, "</%s>",  basetag); // </auxwhat>
2694      VG_(sprintf)(xagL, "<x%s>",  basetag); // <xauxwhat>
2695      VG_(sprintf)(xagR, "</x%s>", basetag); // </xauxwhat>
2696   }
2697
2698#  define TAGL(_xa) p2XA(_xa, "%s", tagL)
2699#  define TAGR(_xa) p2XA(_xa, "%s", tagR)
2700#  define XAGL(_xa) p2XA(_xa, "%s", xagL)
2701#  define XAGR(_xa) p2XA(_xa, "%s", xagR)
2702#  define TXTL(_xa) p2XA(_xa, "%s", "<text>")
2703#  define TXTR(_xa) p2XA(_xa, "%s", "</text>")
2704
2705   /* ------ local cases ------ */
2706
2707   if ( frameNo >= 0 && (!have_srcloc) && (!have_descr) ) {
2708      /* no srcloc, no description:
2709         Location 0x7fefff6cf is 543 bytes inside local var "a",
2710         in frame #1 of thread 1
2711      */
2712      if (xml) {
2713         TAGL( dn1 );
2714         p2XA( dn1,
2715               "Location 0x%lx is %lu byte%s inside local var \"%t\",",
2716               data_addr, var_offset, vo_plural, var->name );
2717         TAGR( dn1 );
2718         TAGL( dn2 );
2719         p2XA( dn2,
2720               "in frame #%d of thread %d", frameNo, (Int)tid );
2721         TAGR( dn2 );
2722      } else {
2723         p2XA( dn1,
2724               "Location 0x%lx is %lu byte%s inside local var \"%s\",",
2725               data_addr, var_offset, vo_plural, var->name );
2726         p2XA( dn2,
2727               "in frame #%d of thread %d", frameNo, (Int)tid );
2728      }
2729   }
2730   else
2731   if ( frameNo >= 0 && have_srcloc && (!have_descr) ) {
2732      /* no description:
2733         Location 0x7fefff6cf is 543 bytes inside local var "a"
2734         declared at dsyms7.c:17, in frame #1 of thread 1
2735      */
2736      if (xml) {
2737         TAGL( dn1 );
2738         p2XA( dn1,
2739               "Location 0x%lx is %lu byte%s inside local var \"%t\"",
2740               data_addr, var_offset, vo_plural, var->name );
2741         TAGR( dn1 );
2742         XAGL( dn2 );
2743         TXTL( dn2 );
2744         p2XA( dn2,
2745               "declared at %t:%d, in frame #%d of thread %d",
2746               var->fileName, var->lineNo, frameNo, (Int)tid );
2747         TXTR( dn2 );
2748         // FIXME: also do <dir>
2749         p2XA( dn2,
2750               " <file>%t</file> <line>%d</line> ",
2751               var->fileName, var->lineNo );
2752         XAGR( dn2 );
2753      } else {
2754         p2XA( dn1,
2755               "Location 0x%lx is %lu byte%s inside local var \"%s\"",
2756               data_addr, var_offset, vo_plural, var->name );
2757         p2XA( dn2,
2758               "declared at %s:%d, in frame #%d of thread %d",
2759               var->fileName, var->lineNo, frameNo, (Int)tid );
2760      }
2761   }
2762   else
2763   if ( frameNo >= 0 && (!have_srcloc) && have_descr ) {
2764      /* no srcloc:
2765         Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2
2766         in frame #1 of thread 1
2767      */
2768      if (xml) {
2769         TAGL( dn1 );
2770         p2XA( dn1,
2771               "Location 0x%lx is %lu byte%s inside %t%t",
2772               data_addr, residual_offset, ro_plural, var->name,
2773               (HChar*)(VG_(indexXA)(described,0)) );
2774         TAGR( dn1 );
2775         TAGL( dn2 );
2776         p2XA( dn2,
2777               "in frame #%d of thread %d", frameNo, (Int)tid );
2778         TAGR( dn2 );
2779      } else {
2780         p2XA( dn1,
2781               "Location 0x%lx is %lu byte%s inside %s%s",
2782               data_addr, residual_offset, ro_plural, var->name,
2783               (HChar*)(VG_(indexXA)(described,0)) );
2784         p2XA( dn2,
2785               "in frame #%d of thread %d", frameNo, (Int)tid );
2786      }
2787   }
2788   else
2789   if ( frameNo >= 0 && have_srcloc && have_descr ) {
2790      /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
2791         declared at dsyms7.c:17, in frame #1 of thread 1 */
2792      if (xml) {
2793         TAGL( dn1 );
2794         p2XA( dn1,
2795               "Location 0x%lx is %lu byte%s inside %t%t,",
2796               data_addr, residual_offset, ro_plural, var->name,
2797               (HChar*)(VG_(indexXA)(described,0)) );
2798         TAGR( dn1 );
2799         XAGL( dn2 );
2800         TXTL( dn2 );
2801         p2XA( dn2,
2802               "declared at %t:%d, in frame #%d of thread %d",
2803               var->fileName, var->lineNo, frameNo, (Int)tid );
2804         TXTR( dn2 );
2805         // FIXME: also do <dir>
2806         p2XA( dn2,
2807               " <file>%t</file> <line>%d</line> ",
2808               var->fileName, var->lineNo );
2809         XAGR( dn2 );
2810      } else {
2811         p2XA( dn1,
2812               "Location 0x%lx is %lu byte%s inside %s%s,",
2813               data_addr, residual_offset, ro_plural, var->name,
2814               (HChar*)(VG_(indexXA)(described,0)) );
2815         p2XA( dn2,
2816               "declared at %s:%d, in frame #%d of thread %d",
2817               var->fileName, var->lineNo, frameNo, (Int)tid );
2818      }
2819   }
2820   else
2821   /* ------ global cases ------ */
2822   if ( frameNo >= -1 && (!have_srcloc) && (!have_descr) ) {
2823      /* no srcloc, no description:
2824         Location 0x7fefff6cf is 543 bytes inside global var "a"
2825      */
2826      if (xml) {
2827         TAGL( dn1 );
2828         p2XA( dn1,
2829               "Location 0x%lx is %lu byte%s inside global var \"%t\"",
2830               data_addr, var_offset, vo_plural, var->name );
2831         TAGR( dn1 );
2832      } else {
2833         p2XA( dn1,
2834               "Location 0x%lx is %lu byte%s inside global var \"%s\"",
2835               data_addr, var_offset, vo_plural, var->name );
2836      }
2837   }
2838   else
2839   if ( frameNo >= -1 && have_srcloc && (!have_descr) ) {
2840      /* no description:
2841         Location 0x7fefff6cf is 543 bytes inside global var "a"
2842         declared at dsyms7.c:17
2843      */
2844      if (xml) {
2845         TAGL( dn1 );
2846         p2XA( dn1,
2847               "Location 0x%lx is %lu byte%s inside global var \"%t\"",
2848               data_addr, var_offset, vo_plural, var->name );
2849         TAGR( dn1 );
2850         XAGL( dn2 );
2851         TXTL( dn2 );
2852         p2XA( dn2,
2853               "declared at %t:%d",
2854               var->fileName, var->lineNo);
2855         TXTR( dn2 );
2856         // FIXME: also do <dir>
2857         p2XA( dn2,
2858               " <file>%t</file> <line>%d</line> ",
2859               var->fileName, var->lineNo );
2860         XAGR( dn2 );
2861      } else {
2862         p2XA( dn1,
2863               "Location 0x%lx is %lu byte%s inside global var \"%s\"",
2864               data_addr, var_offset, vo_plural, var->name );
2865         p2XA( dn2,
2866               "declared at %s:%d",
2867               var->fileName, var->lineNo);
2868      }
2869   }
2870   else
2871   if ( frameNo >= -1 && (!have_srcloc) && have_descr ) {
2872      /* no srcloc:
2873         Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
2874         a global variable
2875      */
2876      if (xml) {
2877         TAGL( dn1 );
2878         p2XA( dn1,
2879               "Location 0x%lx is %lu byte%s inside %t%t,",
2880               data_addr, residual_offset, ro_plural, var->name,
2881               (HChar*)(VG_(indexXA)(described,0)) );
2882         TAGR( dn1 );
2883         TAGL( dn2 );
2884         p2XA( dn2,
2885               "a global variable");
2886         TAGR( dn2 );
2887      } else {
2888         p2XA( dn1,
2889               "Location 0x%lx is %lu byte%s inside %s%s,",
2890               data_addr, residual_offset, ro_plural, var->name,
2891               (char*)(VG_(indexXA)(described,0)) );
2892         p2XA( dn2,
2893               "a global variable");
2894      }
2895   }
2896   else
2897   if ( frameNo >= -1 && have_srcloc && have_descr ) {
2898      /* Location 0x7fefff6cf is 2 bytes inside a[3].xyzzy[21].c2,
2899         a global variable declared at dsyms7.c:17 */
2900      if (xml) {
2901         TAGL( dn1 );
2902         p2XA( dn1,
2903               "Location 0x%lx is %lu byte%s inside %t%t,",
2904               data_addr, residual_offset, ro_plural, var->name,
2905               (HChar*)(VG_(indexXA)(described,0)) );
2906         TAGR( dn1 );
2907         XAGL( dn2 );
2908         TXTL( dn2 );
2909         p2XA( dn2,
2910               "a global variable declared at %t:%d",
2911               var->fileName, var->lineNo);
2912         TXTR( dn2 );
2913         // FIXME: also do <dir>
2914         p2XA( dn2,
2915               " <file>%t</file> <line>%d</line> ",
2916               var->fileName, var->lineNo );
2917         XAGR( dn2 );
2918      } else {
2919         p2XA( dn1,
2920               "Location 0x%lx is %lu byte%s inside %s%s,",
2921               data_addr, residual_offset, ro_plural, var->name,
2922               (HChar*)(VG_(indexXA)(described,0)) );
2923         p2XA( dn2,
2924               "a global variable declared at %s:%d",
2925               var->fileName, var->lineNo);
2926      }
2927   }
2928   else
2929      vg_assert(0);
2930
2931   /* Zero terminate both strings */
2932   zterm_XA( dn1 );
2933   zterm_XA( dn2 );
2934
2935#  undef TAGL
2936#  undef TAGR
2937#  undef XAGL
2938#  undef XAGR
2939#  undef TXTL
2940#  undef TXTR
2941}
2942
2943
2944/* Determine if data_addr is a local variable in the frame
2945   characterised by (ip,sp,fp), and if so write its description at the
2946   ends of DNAME{1,2}, which are XArray*s of HChar, that have been
2947   initialised by the caller, zero terminate both, and return True.
2948   If it's not a local variable in said frame, return False. */
2949static
2950Bool consider_vars_in_frame ( /*MOD*/XArray* /* of HChar */ dname1,
2951                              /*MOD*/XArray* /* of HChar */ dname2,
2952                              Addr data_addr,
2953                              Addr ip, Addr sp, Addr fp,
2954                              /* shown to user: */
2955                              ThreadId tid, Int frameNo )
2956{
2957   Word       i;
2958   DebugInfo* di;
2959   RegSummary regs;
2960   Bool debug = False;
2961
2962   static UInt n_search = 0;
2963   static UInt n_steps = 0;
2964   n_search++;
2965   if (debug)
2966      VG_(printf)("QQQQ: cvif: ip,sp,fp %#lx,%#lx,%#lx\n", ip,sp,fp);
2967   /* first, find the DebugInfo that pertains to 'ip'. */
2968   for (di = debugInfo_list; di; di = di->next) {
2969      n_steps++;
2970      /* text segment missing? unlikely, but handle it .. */
2971      if (!di->text_present || di->text_size == 0)
2972         continue;
2973      /* Ok.  So does this text mapping bracket the ip? */
2974      if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
2975         break;
2976   }
2977
2978   /* Didn't find it.  Strange -- means ip is a code address outside
2979      of any mapped text segment.  Unlikely but not impossible -- app
2980      could be generating code to run. */
2981   if (!di)
2982      return False;
2983
2984   if (0 && ((n_search & 0x1) == 0))
2985      VG_(printf)("consider_vars_in_frame: %u searches, "
2986                  "%u DebugInfos looked at\n",
2987                  n_search, n_steps);
2988   /* Start of performance-enhancing hack: once every ??? (chosen
2989      hackily after profiling) successful searches, move the found
2990      DebugInfo one step closer to the start of the list.  This makes
2991      future searches cheaper. */
2992   if ((n_search & 0xFFFF) == 0) {
2993      /* Move si one step closer to the start of the list. */
2994      move_DebugInfo_one_step_forward( di );
2995   }
2996   /* End of performance-enhancing hack. */
2997
2998   /* any var info at all? */
2999   if (!di->varinfo)
3000      return False;
3001
3002   /* Work through the scopes from most deeply nested outwards,
3003      looking for code address ranges that bracket 'ip'.  The
3004      variables on each such address range found are in scope right
3005      now.  Don't descend to level zero as that is the global
3006      scope. */
3007   regs.ip = ip;
3008   regs.sp = sp;
3009   regs.fp = fp;
3010
3011   /* "for each scope, working outwards ..." */
3012   for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
3013      XArray*      vars;
3014      Word         j;
3015      DiAddrRange* arange;
3016      OSet*        this_scope
3017         = *(OSet**)VG_(indexXA)( di->varinfo, i );
3018      if (debug)
3019         VG_(printf)("QQQQ:   considering scope %ld\n", (Word)i);
3020      if (!this_scope)
3021         continue;
3022      /* Find the set of variables in this scope that
3023         bracket the program counter. */
3024      arange = VG_(OSetGen_LookupWithCmp)(
3025                  this_scope, &ip,
3026                  ML_(cmp_for_DiAddrRange_range)
3027               );
3028      if (!arange)
3029         continue;
3030      /* stay sane */
3031      vg_assert(arange->aMin <= arange->aMax);
3032      /* It must bracket the ip we asked for, else
3033         ML_(cmp_for_DiAddrRange_range) is somehow broken. */
3034      vg_assert(arange->aMin <= ip && ip <= arange->aMax);
3035      /* It must have an attached XArray of DiVariables. */
3036      vars = arange->vars;
3037      vg_assert(vars);
3038      /* But it mustn't cover the entire address range.  We only
3039         expect that to happen for the global scope (level 0), which
3040         we're not looking at here.  Except, it may cover the entire
3041         address range, but in that case the vars array must be
3042         empty. */
3043      vg_assert(! (arange->aMin == (Addr)0
3044                   && arange->aMax == ~(Addr)0
3045                   && VG_(sizeXA)(vars) > 0) );
3046      for (j = 0; j < VG_(sizeXA)( vars ); j++) {
3047         DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
3048         PtrdiffT    offset;
3049         if (debug)
3050            VG_(printf)("QQQQ:    var:name=%s %#lx-%#lx %#lx\n",
3051                        var->name,arange->aMin,arange->aMax,ip);
3052         if (data_address_is_in_var( &offset, di->admin_tyents,
3053                                     var, &regs,
3054                                     data_addr, di )) {
3055            PtrdiffT residual_offset = 0;
3056            XArray* described = ML_(describe_type)( &residual_offset,
3057                                                    di->admin_tyents,
3058                                                    var->typeR, offset );
3059            format_message( dname1, dname2,
3060                            data_addr, var, offset, residual_offset,
3061                            described, frameNo, tid );
3062            VG_(deleteXA)( described );
3063            return True;
3064         }
3065      }
3066   }
3067
3068   return False;
3069}
3070
3071/* Try to form some description of DATA_ADDR by looking at the DWARF3
3072   debug info we have.  This considers all global variables, and all
3073   frames in the stacks of all threads.  Result is written at the ends
3074   of DNAME{1,2}V, which are XArray*s of HChar, that have been
3075   initialised by the caller, and True is returned.  If no description
3076   is created, False is returned.  Regardless of the return value,
3077   DNAME{1,2}V are guaranteed to be zero terminated after the call.
3078
3079   Note that after the call, DNAME{1,2} may have more than one
3080   trailing zero, so callers should establish the useful text length
3081   using VG_(strlen) on the contents, rather than VG_(sizeXA) on the
3082   XArray itself.
3083*/
3084Bool VG_(get_data_description)(
3085        /*MOD*/ void* /* really, XArray* of HChar */ dname1v,
3086        /*MOD*/ void* /* really, XArray* of HChar */ dname2v,
3087        Addr data_addr
3088     )
3089{
3090#  define N_FRAMES 8
3091   Addr ips[N_FRAMES], sps[N_FRAMES], fps[N_FRAMES];
3092   UInt n_frames;
3093
3094   Addr       stack_min, stack_max;
3095   ThreadId   tid;
3096   Bool       found;
3097   DebugInfo* di;
3098   Word       j;
3099
3100   XArray*    dname1 = (XArray*)dname1v;
3101   XArray*    dname2 = (XArray*)dname2v;
3102
3103   if (0) VG_(printf)("get_data_description: dataaddr %#lx\n", data_addr);
3104   /* First, see if data_addr is (or is part of) a global variable.
3105      Loop over the DebugInfos we have.  Check data_addr against the
3106      outermost scope of all of them, as that should be a global
3107      scope. */
3108   for (di = debugInfo_list; di != NULL; di = di->next) {
3109      OSet*        global_scope;
3110      Word         gs_size;
3111      Addr         zero;
3112      DiAddrRange* global_arange;
3113      Word         i;
3114      XArray*      vars;
3115
3116      /* text segment missing? unlikely, but handle it .. */
3117      if (!di->text_present || di->text_size == 0)
3118         continue;
3119      /* any var info at all? */
3120      if (!di->varinfo)
3121         continue;
3122      /* perhaps this object didn't contribute any vars at all? */
3123      if (VG_(sizeXA)( di->varinfo ) == 0)
3124         continue;
3125      global_scope = *(OSet**)VG_(indexXA)( di->varinfo, 0 );
3126      vg_assert(global_scope);
3127      gs_size = VG_(OSetGen_Size)( global_scope );
3128      /* The global scope might be completely empty if this
3129         compilation unit declared locals but nothing global. */
3130      if (gs_size == 0)
3131          continue;
3132      /* But if it isn't empty, then it must contain exactly one
3133         element, which covers the entire address range. */
3134      vg_assert(gs_size == 1);
3135      /* Fish out the global scope and check it is as expected. */
3136      zero = 0;
3137      global_arange
3138         = VG_(OSetGen_Lookup)( global_scope, &zero );
3139      /* The global range from (Addr)0 to ~(Addr)0 must exist */
3140      vg_assert(global_arange);
3141      vg_assert(global_arange->aMin == (Addr)0
3142                && global_arange->aMax == ~(Addr)0);
3143      /* Any vars in this range? */
3144      if (!global_arange->vars)
3145         continue;
3146      /* Ok, there are some vars in the global scope of this
3147         DebugInfo.  Wade through them and see if the data addresses
3148         of any of them bracket data_addr. */
3149      vars = global_arange->vars;
3150      for (i = 0; i < VG_(sizeXA)( vars ); i++) {
3151         PtrdiffT offset;
3152         DiVariable* var = (DiVariable*)VG_(indexXA)( vars, i );
3153         vg_assert(var->name);
3154         /* Note we use a NULL RegSummary* here.  It can't make any
3155            sense for a global variable to have a location expression
3156            which depends on a SP/FP/IP value.  So don't supply any.
3157            This means, if the evaluation of the location
3158            expression/list requires a register, we have to let it
3159            fail. */
3160         if (data_address_is_in_var( &offset, di->admin_tyents, var,
3161                                     NULL/* RegSummary* */,
3162                                     data_addr, di )) {
3163            PtrdiffT residual_offset = 0;
3164            XArray* described = ML_(describe_type)( &residual_offset,
3165                                                    di->admin_tyents,
3166                                                    var->typeR, offset );
3167            format_message( dname1, dname2,
3168                            data_addr, var, offset, residual_offset,
3169                            described, -1/*frameNo*/,
3170                            VG_INVALID_THREADID );
3171            VG_(deleteXA)( described );
3172            zterm_XA( dname1 );
3173            zterm_XA( dname2 );
3174            return True;
3175         }
3176      }
3177   }
3178
3179   /* Ok, well it's not a global variable.  So now let's snoop around
3180      in the stacks of all the threads.  First try to figure out which
3181      thread's stack data_addr is in. */
3182
3183   /* --- KLUDGE --- Try examining the top frame of all thread stacks.
3184      This finds variables which are not stack allocated but are not
3185      globally visible either; specifically it appears to pick up
3186      variables which are visible only within a compilation unit.
3187      These will have the address range of the compilation unit and
3188      tend to live at Scope level 1. */
3189   VG_(thread_stack_reset_iter)(&tid);
3190   while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) {
3191      if (stack_min >= stack_max)
3192         continue; /* ignore obviously stupid cases */
3193      if (consider_vars_in_frame( dname1, dname2,
3194                                  data_addr,
3195                                  VG_(get_IP)(tid),
3196                                  VG_(get_SP)(tid),
3197                                  VG_(get_FP)(tid), tid, 0 )) {
3198         zterm_XA( dname1 );
3199         zterm_XA( dname2 );
3200         return True;
3201      }
3202   }
3203   /* --- end KLUDGE --- */
3204
3205   /* Perhaps it's on a thread's stack? */
3206   found = False;
3207   VG_(thread_stack_reset_iter)(&tid);
3208   while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) {
3209      if (stack_min >= stack_max)
3210         continue; /* ignore obviously stupid cases */
3211      if (stack_min - VG_STACK_REDZONE_SZB <= data_addr
3212          && data_addr <= stack_max) {
3213         found = True;
3214         break;
3215      }
3216   }
3217   if (!found) {
3218      zterm_XA( dname1 );
3219      zterm_XA( dname2 );
3220      return False;
3221   }
3222
3223   /* We conclude data_addr is in thread tid's stack.  Unwind the
3224      stack to get a bunch of (ip,sp,fp) triples describing the
3225      frames, and for each frame, consider the local variables. */
3226   n_frames = VG_(get_StackTrace)( tid, ips, N_FRAMES,
3227                                   sps, fps, 0/*first_ip_delta*/ );
3228
3229   /* As a result of KLUDGE above, starting the loop at j = 0
3230      duplicates examination of the top frame and so isn't necessary.
3231      Oh well. */
3232   vg_assert(n_frames >= 0 && n_frames <= N_FRAMES);
3233   for (j = 0; j < n_frames; j++) {
3234      if (consider_vars_in_frame( dname1, dname2,
3235                                  data_addr,
3236                                  ips[j],
3237                                  sps[j], fps[j], tid, j )) {
3238         zterm_XA( dname1 );
3239         zterm_XA( dname2 );
3240         return True;
3241      }
3242      /* Now, it appears that gcc sometimes appears to produce
3243         location lists whose ranges don't actually cover the call
3244         instruction, even though the address of the variable in
3245         question is passed as a parameter in the call.  AFAICS this
3246         is simply a bug in gcc - how can the variable be claimed not
3247         exist in memory (on the stack) for the duration of a call in
3248         which its address is passed?  But anyway, in the particular
3249         case I investigated (memcheck/tests/varinfo6.c, call to croak
3250         on line 2999, local var budget declared at line 3115
3251         appearing not to exist across the call to mainSort on line
3252         3143, "gcc.orig (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)" on
3253         amd64), the variable's location list does claim it exists
3254         starting at the first byte of the first instruction after the
3255         call instruction.  So, call consider_vars_in_frame a second
3256         time, but this time add 1 to the IP.  GDB handles this
3257         example with no difficulty, which leads me to believe that
3258         either (1) I misunderstood something, or (2) GDB has an
3259         equivalent kludge. */
3260      if (j > 0 /* this is a non-innermost frame */
3261          && consider_vars_in_frame( dname1, dname2,
3262                                     data_addr,
3263                                     ips[j] + 1,
3264                                     sps[j], fps[j], tid, j )) {
3265         zterm_XA( dname1 );
3266         zterm_XA( dname2 );
3267         return True;
3268      }
3269   }
3270
3271   /* We didn't find anything useful. */
3272   zterm_XA( dname1 );
3273   zterm_XA( dname2 );
3274   return False;
3275#  undef N_FRAMES
3276}
3277
3278
3279//////////////////////////////////////////////////////////////////
3280//                                                              //
3281// Support for other kinds of queries to the Dwarf3 var info    //
3282//                                                              //
3283//////////////////////////////////////////////////////////////////
3284
3285/* Figure out if the variable 'var' has a location that is linearly
3286   dependent on a stack pointer value, or a frame pointer value, and
3287   if it is, add a description of it to 'blocks'.  Otherwise ignore
3288   it.  If 'arrays_only' is True, also ignore it unless it has an
3289   array type. */
3290
3291static
3292void analyse_deps ( /*MOD*/XArray* /* of FrameBlock */ blocks,
3293                    XArray* /* TyEnt */ tyents,
3294                    Addr ip, const DebugInfo* di, DiVariable* var,
3295                    Bool arrays_only )
3296{
3297   GXResult   res_sp_6k, res_sp_7k, res_fp_6k, res_fp_7k;
3298   RegSummary regs;
3299   MaybeULong mul;
3300   Bool       isVec;
3301   TyEnt*     ty;
3302
3303   Bool debug = False;
3304   if (0&&debug)
3305      VG_(printf)("adeps: var %s\n", var->name );
3306
3307   /* Figure out how big the variable is. */
3308   mul = ML_(sizeOfType)(tyents, var->typeR);
3309   /* If this var has a type whose size is unknown, zero, or
3310      impossibly large, it should never have been added.  ML_(addVar)
3311      should have rejected it. */
3312   vg_assert(mul.b == True);
3313   vg_assert(mul.ul > 0);
3314   if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
3315   /* After this point, we assume we can truncate mul.ul to a host word
3316      safely (without loss of info). */
3317
3318   /* skip if non-array and we're only interested in arrays */
3319   ty = ML_(TyEnts__index_by_cuOff)( tyents, NULL, var->typeR );
3320   vg_assert(ty);
3321   vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
3322   if (ty->tag == Te_UNKNOWN)
3323      return; /* perhaps we should complain in this case? */
3324   isVec = ty->tag == Te_TyArray;
3325   if (arrays_only && !isVec)
3326      return;
3327
3328   if (0) {ML_(pp_TyEnt_C_ishly)(tyents, var->typeR);
3329           VG_(printf)("  %s\n", var->name);}
3330
3331   /* Do some test evaluations of the variable's location expression,
3332      in order to guess whether it is sp-relative, fp-relative, or
3333      none.  A crude hack, which can be interpreted roughly as finding
3334      the first derivative of the location expression w.r.t. the
3335      supplied frame and stack pointer values. */
3336   regs.fp   = 0;
3337   regs.ip   = ip;
3338   regs.sp   = 6 * 1024;
3339   res_sp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3340
3341   regs.fp   = 0;
3342   regs.ip   = ip;
3343   regs.sp   = 7 * 1024;
3344   res_sp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3345
3346   regs.fp   = 6 * 1024;
3347   regs.ip   = ip;
3348   regs.sp   = 0;
3349   res_fp_6k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3350
3351   regs.fp   = 7 * 1024;
3352   regs.ip   = ip;
3353   regs.sp   = 0;
3354   res_fp_7k = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3355
3356   vg_assert(res_sp_6k.kind == res_sp_7k.kind);
3357   vg_assert(res_sp_6k.kind == res_fp_6k.kind);
3358   vg_assert(res_sp_6k.kind == res_fp_7k.kind);
3359
3360   if (res_sp_6k.kind == GXR_Addr) {
3361      StackBlock block;
3362      GXResult res;
3363      UWord sp_delta = res_sp_7k.word - res_sp_6k.word;
3364      UWord fp_delta = res_fp_7k.word - res_fp_6k.word;
3365      tl_assert(sp_delta == 0 || sp_delta == 1024);
3366      tl_assert(fp_delta == 0 || fp_delta == 1024);
3367
3368      if (sp_delta == 0 && fp_delta == 0) {
3369         /* depends neither on sp nor fp, so it can't be a stack
3370            local.  Ignore it. */
3371      }
3372      else
3373      if (sp_delta == 1024 && fp_delta == 0) {
3374         regs.sp = regs.fp = 0;
3375         regs.ip = ip;
3376         res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3377         tl_assert(res.kind == GXR_Addr);
3378         if (debug)
3379         VG_(printf)("   %5ld .. %5ld (sp) %s\n",
3380                     res.word, res.word + ((UWord)mul.ul) - 1, var->name);
3381         block.base  = res.word;
3382         block.szB   = (SizeT)mul.ul;
3383         block.spRel = True;
3384         block.isVec = isVec;
3385         VG_(memset)( &block.name[0], 0, sizeof(block.name) );
3386         if (var->name)
3387            VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
3388         block.name[ sizeof(block.name)-1 ] = 0;
3389         VG_(addToXA)( blocks, &block );
3390      }
3391      else
3392      if (sp_delta == 0 && fp_delta == 1024) {
3393         regs.sp = regs.fp = 0;
3394         regs.ip = ip;
3395         res = ML_(evaluate_GX)( var->gexpr, var->fbGX, &regs, di );
3396         tl_assert(res.kind == GXR_Addr);
3397         if (debug)
3398         VG_(printf)("   %5ld .. %5ld (FP) %s\n",
3399                     res.word, res.word + ((UWord)mul.ul) - 1, var->name);
3400         block.base  = res.word;
3401         block.szB   = (SizeT)mul.ul;
3402         block.spRel = False;
3403         block.isVec = isVec;
3404         VG_(memset)( &block.name[0], 0, sizeof(block.name) );
3405         if (var->name)
3406            VG_(strncpy)( &block.name[0], var->name, sizeof(block.name)-1 );
3407         block.name[ sizeof(block.name)-1 ] = 0;
3408         VG_(addToXA)( blocks, &block );
3409      }
3410      else {
3411         vg_assert(0);
3412      }
3413   }
3414}
3415
3416
3417/* Get an XArray of StackBlock which describe the stack (auto) blocks
3418   for this ip.  The caller is expected to free the XArray at some
3419   point.  If 'arrays_only' is True, only array-typed blocks are
3420   returned; otherwise blocks of all types are returned. */
3421
3422void* /* really, XArray* of StackBlock */
3423      VG_(di_get_stack_blocks_at_ip)( Addr ip, Bool arrays_only )
3424{
3425   /* This is a derivation of consider_vars_in_frame() above. */
3426   Word       i;
3427   DebugInfo* di;
3428   Bool debug = False;
3429
3430   XArray* res = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dgsbai.1",
3431                             ML_(dinfo_free),
3432                             sizeof(StackBlock) );
3433
3434   static UInt n_search = 0;
3435   static UInt n_steps = 0;
3436   n_search++;
3437   if (debug)
3438      VG_(printf)("QQQQ: dgsbai: ip %#lx\n", ip);
3439   /* first, find the DebugInfo that pertains to 'ip'. */
3440   for (di = debugInfo_list; di; di = di->next) {
3441      n_steps++;
3442      /* text segment missing? unlikely, but handle it .. */
3443      if (!di->text_present || di->text_size == 0)
3444         continue;
3445      /* Ok.  So does this text mapping bracket the ip? */
3446      if (di->text_avma <= ip && ip < di->text_avma + di->text_size)
3447         break;
3448   }
3449
3450   /* Didn't find it.  Strange -- means ip is a code address outside
3451      of any mapped text segment.  Unlikely but not impossible -- app
3452      could be generating code to run. */
3453   if (!di)
3454      return res; /* currently empty */
3455
3456   if (0 && ((n_search & 0x1) == 0))
3457      VG_(printf)("VG_(di_get_stack_blocks_at_ip): %u searches, "
3458                  "%u DebugInfos looked at\n",
3459                  n_search, n_steps);
3460   /* Start of performance-enhancing hack: once every ??? (chosen
3461      hackily after profiling) successful searches, move the found
3462      DebugInfo one step closer to the start of the list.  This makes
3463      future searches cheaper. */
3464   if ((n_search & 0xFFFF) == 0) {
3465      /* Move si one step closer to the start of the list. */
3466      move_DebugInfo_one_step_forward( di );
3467   }
3468   /* End of performance-enhancing hack. */
3469
3470   /* any var info at all? */
3471   if (!di->varinfo)
3472      return res; /* currently empty */
3473
3474   /* Work through the scopes from most deeply nested outwards,
3475      looking for code address ranges that bracket 'ip'.  The
3476      variables on each such address range found are in scope right
3477      now.  Don't descend to level zero as that is the global
3478      scope. */
3479
3480   /* "for each scope, working outwards ..." */
3481   for (i = VG_(sizeXA)(di->varinfo) - 1; i >= 1; i--) {
3482      XArray*      vars;
3483      Word         j;
3484      DiAddrRange* arange;
3485      OSet*        this_scope
3486         = *(OSet**)VG_(indexXA)( di->varinfo, i );
3487      if (debug)
3488         VG_(printf)("QQQQ:   considering scope %ld\n", (Word)i);
3489      if (!this_scope)
3490         continue;
3491      /* Find the set of variables in this scope that
3492         bracket the program counter. */
3493      arange = VG_(OSetGen_LookupWithCmp)(
3494                  this_scope, &ip,
3495                  ML_(cmp_for_DiAddrRange_range)
3496               );
3497      if (!arange)
3498         continue;
3499      /* stay sane */
3500      vg_assert(arange->aMin <= arange->aMax);
3501      /* It must bracket the ip we asked for, else
3502         ML_(cmp_for_DiAddrRange_range) is somehow broken. */
3503      vg_assert(arange->aMin <= ip && ip <= arange->aMax);
3504      /* It must have an attached XArray of DiVariables. */
3505      vars = arange->vars;
3506      vg_assert(vars);
3507      /* But it mustn't cover the entire address range.  We only
3508         expect that to happen for the global scope (level 0), which
3509         we're not looking at here.  Except, it may cover the entire
3510         address range, but in that case the vars array must be
3511         empty. */
3512      vg_assert(! (arange->aMin == (Addr)0
3513                   && arange->aMax == ~(Addr)0
3514                   && VG_(sizeXA)(vars) > 0) );
3515      for (j = 0; j < VG_(sizeXA)( vars ); j++) {
3516         DiVariable* var = (DiVariable*)VG_(indexXA)( vars, j );
3517         if (debug)
3518            VG_(printf)("QQQQ:    var:name=%s %#lx-%#lx %#lx\n",
3519                        var->name,arange->aMin,arange->aMax,ip);
3520         analyse_deps( res, di->admin_tyents, ip,
3521                       di, var, arrays_only );
3522      }
3523   }
3524
3525   return res;
3526}
3527
3528
3529/* Get an array of GlobalBlock which describe the global blocks owned
3530   by the shared object characterised by the given di_handle.  Asserts
3531   if the handle is invalid.  The caller is responsible for freeing
3532   the array at some point.  If 'arrays_only' is True, only
3533   array-typed blocks are returned; otherwise blocks of all types are
3534   returned. */
3535
3536void* /* really, XArray* of GlobalBlock */
3537      VG_(di_get_global_blocks_from_dihandle) ( ULong di_handle,
3538                                                Bool  arrays_only )
3539{
3540   /* This is a derivation of consider_vars_in_frame() above. */
3541
3542   DebugInfo* di;
3543   XArray* gvars; /* XArray* of GlobalBlock */
3544   Word nScopes, scopeIx;
3545
3546   /* The first thing to do is find the DebugInfo that
3547      pertains to 'di_handle'. */
3548   tl_assert(di_handle > 0);
3549   for (di = debugInfo_list; di; di = di->next) {
3550      if (di->handle == di_handle)
3551         break;
3552   }
3553
3554   /* If this fails, we were unable to find any DebugInfo with the
3555      given handle.  This is considered an error on the part of the
3556      caller. */
3557   tl_assert(di != NULL);
3558
3559   /* we'll put the collected variables in here. */
3560   gvars = VG_(newXA)( ML_(dinfo_zalloc), "di.debuginfo.dggbfd.1",
3561                       ML_(dinfo_free), sizeof(GlobalBlock) );
3562   tl_assert(gvars);
3563
3564   /* any var info at all? */
3565   if (!di->varinfo)
3566      return gvars;
3567
3568   /* we'll iterate over all the variables we can find, even if
3569      it seems senseless to visit stack-allocated variables */
3570   /* Iterate over all scopes */
3571   nScopes = VG_(sizeXA)( di->varinfo );
3572   for (scopeIx = 0; scopeIx < nScopes; scopeIx++) {
3573
3574      /* Iterate over each (code) address range at the current scope */
3575      DiAddrRange* range;
3576      OSet* /* of DiAddrInfo */ scope
3577         = *(OSet**)VG_(indexXA)( di->varinfo, scopeIx );
3578      tl_assert(scope);
3579      VG_(OSetGen_ResetIter)(scope);
3580      while ( (range = VG_(OSetGen_Next)(scope)) ) {
3581
3582         /* Iterate over each variable in the current address range */
3583         Word nVars, varIx;
3584         tl_assert(range->vars);
3585         nVars = VG_(sizeXA)( range->vars );
3586         for (varIx = 0; varIx < nVars; varIx++) {
3587
3588            Bool        isVec;
3589            GXResult    res;
3590            MaybeULong  mul;
3591            GlobalBlock gb;
3592            TyEnt*      ty;
3593            DiVariable* var = VG_(indexXA)( range->vars, varIx );
3594            tl_assert(var->name);
3595            if (0) VG_(printf)("at depth %ld var %s ", scopeIx, var->name );
3596
3597            /* Now figure out if this variable has a constant address
3598               (that is, independent of FP, SP, phase of moon, etc),
3599               and if so, what the address is.  Any variable with a
3600               constant address is deemed to be a global so we collect
3601               it. */
3602            if (0) { VG_(printf)("EVAL: "); ML_(pp_GX)(var->gexpr);
3603                     VG_(printf)("\n"); }
3604            res = ML_(evaluate_trivial_GX)( var->gexpr, di );
3605
3606            /* Not a constant address => not interesting */
3607            if (res.kind != GXR_Addr) {
3608               if (0) VG_(printf)("FAIL\n");
3609               continue;
3610            }
3611
3612            /* Ok, it's a constant address.  See if we want to collect
3613               it. */
3614            if (0) VG_(printf)("%#lx\n", res.word);
3615
3616            /* Figure out how big the variable is. */
3617            mul = ML_(sizeOfType)(di->admin_tyents, var->typeR);
3618
3619            /* If this var has a type whose size is unknown, zero, or
3620               impossibly large, it should never have been added.
3621               ML_(addVar) should have rejected it. */
3622            vg_assert(mul.b == True);
3623            vg_assert(mul.ul > 0);
3624            if (sizeof(void*) == 4) vg_assert(mul.ul < (1ULL << 32));
3625            /* After this point, we assume we can truncate mul.ul to a
3626               host word safely (without loss of info). */
3627
3628            /* skip if non-array and we're only interested in
3629               arrays */
3630            ty = ML_(TyEnts__index_by_cuOff)( di->admin_tyents, NULL,
3631                                              var->typeR );
3632            vg_assert(ty);
3633            vg_assert(ty->tag == Te_UNKNOWN || ML_(TyEnt__is_type)(ty));
3634            if (ty->tag == Te_UNKNOWN)
3635               continue; /* perhaps we should complain in this case? */
3636
3637            isVec = ty->tag == Te_TyArray;
3638            if (arrays_only && !isVec) continue;
3639
3640            /* Ok, so collect it! */
3641            tl_assert(var->name);
3642            tl_assert(di->soname);
3643            if (0) VG_(printf)("XXXX %s %s %d\n", var->name,
3644                                var->fileName?(HChar*)var->fileName
3645                                             :"??",var->lineNo);
3646            VG_(memset)(&gb, 0, sizeof(gb));
3647            gb.addr  = res.word;
3648            gb.szB   = (SizeT)mul.ul;
3649            gb.isVec = isVec;
3650            VG_(strncpy)(&gb.name[0], var->name, sizeof(gb.name)-1);
3651            VG_(strncpy)(&gb.soname[0], di->soname, sizeof(gb.soname)-1);
3652            tl_assert(gb.name[ sizeof(gb.name)-1 ] == 0);
3653            tl_assert(gb.soname[ sizeof(gb.soname)-1 ] == 0);
3654
3655            VG_(addToXA)( gvars, &gb );
3656
3657         } /* for (varIx = 0; varIx < nVars; varIx++) */
3658
3659      } /* while ( (range = VG_(OSetGen_Next)(scope)) ) */
3660
3661   } /* for (scopeIx = 0; scopeIx < nScopes; scopeIx++) */
3662
3663   return gvars;
3664}
3665
3666
3667/*------------------------------------------------------------*/
3668/*--- DebugInfo accessor functions                         ---*/
3669/*------------------------------------------------------------*/
3670
3671const DebugInfo* VG_(next_DebugInfo)(const DebugInfo* di)
3672{
3673   if (di == NULL)
3674      return debugInfo_list;
3675   return di->next;
3676}
3677
3678Addr VG_(DebugInfo_get_text_avma)(const DebugInfo* di)
3679{
3680   return di->text_present ? di->text_avma : 0;
3681}
3682
3683SizeT VG_(DebugInfo_get_text_size)(const DebugInfo* di)
3684{
3685   return di->text_present ? di->text_size : 0;
3686}
3687
3688Addr VG_(DebugInfo_get_plt_avma)(const DebugInfo* di)
3689{
3690   return di->plt_present ? di->plt_avma : 0;
3691}
3692
3693SizeT VG_(DebugInfo_get_plt_size)(const DebugInfo* di)
3694{
3695   return di->plt_present ? di->plt_size : 0;
3696}
3697
3698Addr VG_(DebugInfo_get_gotplt_avma)(const DebugInfo* di)
3699{
3700   return di->gotplt_present ? di->gotplt_avma : 0;
3701}
3702
3703SizeT VG_(DebugInfo_get_gotplt_size)(const DebugInfo* di)
3704{
3705   return di->gotplt_present ? di->gotplt_size : 0;
3706}
3707
3708const UChar* VG_(DebugInfo_get_soname)(const DebugInfo* di)
3709{
3710   return di->soname;
3711}
3712
3713const UChar* VG_(DebugInfo_get_filename)(const DebugInfo* di)
3714{
3715   return di->fsm.filename;
3716}
3717
3718PtrdiffT VG_(DebugInfo_get_text_bias)(const DebugInfo* di)
3719{
3720   return di->text_present ? di->text_bias : 0;
3721}
3722
3723Int VG_(DebugInfo_syms_howmany) ( const DebugInfo *si )
3724{
3725   return si->symtab_used;
3726}
3727
3728void VG_(DebugInfo_syms_getidx) ( const DebugInfo *si,
3729                                        Int idx,
3730                                  /*OUT*/Addr*    avma,
3731                                  /*OUT*/Addr*    tocptr,
3732                                  /*OUT*/UInt*    size,
3733                                  /*OUT*/UChar**  pri_name,
3734                                  /*OUT*/UChar*** sec_names,
3735                                  /*OUT*/Bool*    isText,
3736                                  /*OUT*/Bool*    isIFunc )
3737{
3738   vg_assert(idx >= 0 && idx < si->symtab_used);
3739   if (avma)      *avma      = si->symtab[idx].addr;
3740   if (tocptr)    *tocptr    = si->symtab[idx].tocptr;
3741   if (size)      *size      = si->symtab[idx].size;
3742   if (pri_name)  *pri_name  = si->symtab[idx].pri_name;
3743   if (sec_names) *sec_names = si->symtab[idx].sec_names;
3744   if (isText)    *isText    = si->symtab[idx].isText;
3745   if (isIFunc)   *isIFunc   = si->symtab[idx].isIFunc;
3746}
3747
3748
3749/*------------------------------------------------------------*/
3750/*--- SectKind query functions                             ---*/
3751/*------------------------------------------------------------*/
3752
3753/* Convert a VgSectKind to a string, which must be copied if you want
3754   to change it. */
3755const HChar* VG_(pp_SectKind)( VgSectKind kind )
3756{
3757   switch (kind) {
3758      case Vg_SectUnknown: return "Unknown";
3759      case Vg_SectText:    return "Text";
3760      case Vg_SectData:    return "Data";
3761      case Vg_SectBSS:     return "BSS";
3762      case Vg_SectGOT:     return "GOT";
3763      case Vg_SectPLT:     return "PLT";
3764      case Vg_SectOPD:     return "OPD";
3765      case Vg_SectGOTPLT:  return "GOTPLT";
3766      default:             vg_assert(0);
3767   }
3768}
3769
3770/* Given an address 'a', make a guess of which section of which object
3771   it comes from.  If name is non-NULL, then the last n_name-1
3772   characters of the object's name is put in name[0 .. n_name-2], and
3773   name[n_name-1] is set to zero (guaranteed zero terminated). */
3774
3775VgSectKind VG_(DebugInfo_sect_kind)( /*OUT*/UChar* name, SizeT n_name,
3776                                     Addr a)
3777{
3778   DebugInfo* di;
3779   VgSectKind res = Vg_SectUnknown;
3780
3781   for (di = debugInfo_list; di != NULL; di = di->next) {
3782
3783      if (0)
3784         VG_(printf)(
3785            "addr=%#lx di=%p %s got=%#lx,%ld plt=%#lx,%ld "
3786            "data=%#lx,%ld bss=%#lx,%ld\n",
3787            a, di, di->fsm.filename,
3788            di->got_avma,  di->got_size,
3789            di->plt_avma,  di->plt_size,
3790            di->data_avma, di->data_size,
3791            di->bss_avma,  di->bss_size);
3792
3793      if (di->text_present
3794          && di->text_size > 0
3795          && a >= di->text_avma && a < di->text_avma + di->text_size) {
3796         res = Vg_SectText;
3797         break;
3798      }
3799      if (di->data_present
3800          && di->data_size > 0
3801          && a >= di->data_avma && a < di->data_avma + di->data_size) {
3802         res = Vg_SectData;
3803         break;
3804      }
3805      if (di->sdata_present
3806          && di->sdata_size > 0
3807          && a >= di->sdata_avma && a < di->sdata_avma + di->sdata_size) {
3808         res = Vg_SectData;
3809         break;
3810      }
3811      if (di->bss_present
3812          && di->bss_size > 0
3813          && a >= di->bss_avma && a < di->bss_avma + di->bss_size) {
3814         res = Vg_SectBSS;
3815         break;
3816      }
3817      if (di->sbss_present
3818          && di->sbss_size > 0
3819          && a >= di->sbss_avma && a < di->sbss_avma + di->sbss_size) {
3820         res = Vg_SectBSS;
3821         break;
3822      }
3823      if (di->plt_present
3824          && di->plt_size > 0
3825          && a >= di->plt_avma && a < di->plt_avma + di->plt_size) {
3826         res = Vg_SectPLT;
3827         break;
3828      }
3829      if (di->got_present
3830          && di->got_size > 0
3831          && a >= di->got_avma && a < di->got_avma + di->got_size) {
3832         res = Vg_SectGOT;
3833         break;
3834      }
3835      if (di->gotplt_present
3836          && di->gotplt_size > 0
3837          && a >= di->gotplt_avma && a < di->gotplt_avma + di->gotplt_size) {
3838         res = Vg_SectGOTPLT;
3839         break;
3840      }
3841      if (di->opd_present
3842          && di->opd_size > 0
3843          && a >= di->opd_avma && a < di->opd_avma + di->opd_size) {
3844         res = Vg_SectOPD;
3845         break;
3846      }
3847      /* we could also check for .eh_frame, if anyone really cares */
3848   }
3849
3850   vg_assert( (di == NULL && res == Vg_SectUnknown)
3851              || (di != NULL && res != Vg_SectUnknown) );
3852
3853   if (name) {
3854
3855      vg_assert(n_name >= 8);
3856
3857      if (di && di->fsm.filename) {
3858         Int i, j;
3859         Int fnlen = VG_(strlen)(di->fsm.filename);
3860         Int start_at = 1 + fnlen - n_name;
3861         if (start_at < 0) start_at = 0;
3862         vg_assert(start_at < fnlen);
3863         i = start_at; j = 0;
3864         while (True) {
3865            vg_assert(j >= 0 && j < n_name);
3866            vg_assert(i >= 0 && i <= fnlen);
3867            name[j] = di->fsm.filename[i];
3868            if (di->fsm.filename[i] == 0) break;
3869            i++; j++;
3870         }
3871         vg_assert(i == fnlen);
3872      } else {
3873         VG_(snprintf)(name, n_name, "%s", "???");
3874      }
3875
3876      name[n_name-1] = 0;
3877   }
3878
3879   return res;
3880
3881}
3882
3883/*--------------------------------------------------------------------*/
3884/*--- end                                                          ---*/
3885/*--------------------------------------------------------------------*/
3886