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