1 //===-- DWARFDebugInfo.cpp --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "SymbolFileDWARF.h" 11 12 #include <algorithm> 13 #include <set> 14 15 #include "lldb/Core/RegularExpression.h" 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 19 #include "DWARFDebugAranges.h" 20 #include "DWARFDebugInfo.h" 21 #include "DWARFCompileUnit.h" 22 #include "DWARFDebugAranges.h" 23 #include "DWARFDebugInfoEntry.h" 24 #include "DWARFFormValue.h" 25 #include "LogChannelDWARF.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 using namespace std; 30 31 //---------------------------------------------------------------------- 32 // Constructor 33 //---------------------------------------------------------------------- 34 DWARFDebugInfo::DWARFDebugInfo() : 35 m_dwarf2Data(NULL), 36 m_compile_units(), 37 m_cu_aranges_ap () 38 { 39 } 40 41 //---------------------------------------------------------------------- 42 // SetDwarfData 43 //---------------------------------------------------------------------- 44 void 45 DWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data) 46 { 47 m_dwarf2Data = dwarf2Data; 48 m_compile_units.clear(); 49 } 50 51 52 DWARFDebugAranges & 53 DWARFDebugInfo::GetCompileUnitAranges () 54 { 55 if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data) 56 { 57 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); 58 59 m_cu_aranges_ap.reset (new DWARFDebugAranges()); 60 const DataExtractor &debug_aranges_data = m_dwarf2Data->get_debug_aranges_data(); 61 if (debug_aranges_data.GetByteSize() > 0) 62 { 63 if (log) 64 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s/%s\" from .debug_aranges", 65 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetDirectory().GetCString(), 66 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetFilename().GetCString()); 67 m_cu_aranges_ap->Extract (debug_aranges_data); 68 69 } 70 else 71 { 72 if (log) 73 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s/%s\" by parsing", 74 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetDirectory().GetCString(), 75 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetFilename().GetCString()); 76 const uint32_t num_compile_units = GetNumCompileUnits(); 77 uint32_t idx; 78 const bool clear_dies_if_already_not_parsed = true; 79 for (idx = 0; idx < num_compile_units; ++idx) 80 { 81 DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx); 82 if (cu) 83 cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get(), clear_dies_if_already_not_parsed); 84 } 85 } 86 87 const bool minimize = true; 88 m_cu_aranges_ap->Sort (minimize); 89 } 90 return *m_cu_aranges_ap.get(); 91 } 92 93 94 //---------------------------------------------------------------------- 95 // LookupAddress 96 //---------------------------------------------------------------------- 97 bool 98 DWARFDebugInfo::LookupAddress 99 ( 100 const dw_addr_t address, 101 const dw_offset_t hint_die_offset, 102 DWARFCompileUnitSP& cu_sp, 103 DWARFDebugInfoEntry** function_die, 104 DWARFDebugInfoEntry** block_die 105 ) 106 { 107 108 if (hint_die_offset != DW_INVALID_OFFSET) 109 cu_sp = GetCompileUnit(hint_die_offset); 110 else 111 { 112 DWARFDebugAranges &cu_aranges = GetCompileUnitAranges (); 113 const dw_offset_t cu_offset = cu_aranges.FindAddress (address); 114 cu_sp = GetCompileUnit(cu_offset); 115 } 116 117 if (cu_sp.get()) 118 { 119 if (cu_sp->LookupAddress(address, function_die, block_die)) 120 return true; 121 cu_sp.reset(); 122 } 123 else 124 { 125 // The hint_die_offset may have been a pointer to the actual item that 126 // we are looking for 127 DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp); 128 if (die_ptr) 129 { 130 if (cu_sp.get()) 131 { 132 if (function_die || block_die) 133 return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die); 134 135 // We only wanted the compile unit that contained this address 136 return true; 137 } 138 } 139 } 140 return false; 141 } 142 143 144 void 145 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded() 146 { 147 if (m_compile_units.empty()) 148 { 149 if (m_dwarf2Data != NULL) 150 { 151 uint32_t offset = 0; 152 const DataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data(); 153 while (debug_info_data.ValidOffset(offset)) 154 { 155 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data)); 156 // Out of memory? 157 if (cu_sp.get() == NULL) 158 break; 159 160 if (cu_sp->Extract(debug_info_data, &offset) == false) 161 break; 162 163 m_compile_units.push_back(cu_sp); 164 165 offset = cu_sp->GetNextCompileUnitOffset(); 166 } 167 } 168 } 169 } 170 171 uint32_t 172 DWARFDebugInfo::GetNumCompileUnits() 173 { 174 ParseCompileUnitHeadersIfNeeded(); 175 return m_compile_units.size(); 176 } 177 178 DWARFCompileUnit* 179 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx) 180 { 181 DWARFCompileUnit* cu = NULL; 182 if (idx < GetNumCompileUnits()) 183 cu = m_compile_units[idx].get(); 184 return cu; 185 } 186 187 bool 188 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const 189 { 190 // Not a verify efficient function, but it is handy for use in assertions 191 // to make sure that a compile unit comes from a debug information file. 192 CompileUnitColl::const_iterator end_pos = m_compile_units.end(); 193 CompileUnitColl::const_iterator pos; 194 195 for (pos = m_compile_units.begin(); pos != end_pos; ++pos) 196 { 197 if (pos->get() == cu) 198 return true; 199 } 200 return false; 201 } 202 203 204 static bool CompileUnitOffsetLessThan (const DWARFCompileUnitSP& a, const DWARFCompileUnitSP& b) 205 { 206 return a->GetOffset() < b->GetOffset(); 207 } 208 209 210 static int 211 CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem) 212 { 213 const dw_offset_t key_cu_offset = *(dw_offset_t*) key; 214 const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset(); 215 if (key_cu_offset < cu_offset) 216 return -1; 217 if (key_cu_offset > cu_offset) 218 return 1; 219 return 0; 220 } 221 222 DWARFCompileUnitSP 223 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr) 224 { 225 DWARFCompileUnitSP cu_sp; 226 uint32_t cu_idx = DW_INVALID_INDEX; 227 if (cu_offset != DW_INVALID_OFFSET) 228 { 229 ParseCompileUnitHeadersIfNeeded(); 230 231 DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset); 232 if (match) 233 { 234 cu_sp = *match; 235 cu_idx = match - &m_compile_units[0]; 236 } 237 } 238 if (idx_ptr) 239 *idx_ptr = cu_idx; 240 return cu_sp; 241 } 242 243 DWARFCompileUnitSP 244 DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset) 245 { 246 DWARFCompileUnitSP cu_sp; 247 if (die_offset != DW_INVALID_OFFSET) 248 { 249 ParseCompileUnitHeadersIfNeeded(); 250 251 CompileUnitColl::const_iterator end_pos = m_compile_units.end(); 252 CompileUnitColl::const_iterator pos; 253 254 for (pos = m_compile_units.begin(); pos != end_pos; ++pos) 255 { 256 dw_offset_t cu_start_offset = (*pos)->GetOffset(); 257 dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset(); 258 if (cu_start_offset <= die_offset && die_offset < cu_end_offset) 259 { 260 cu_sp = *pos; 261 break; 262 } 263 } 264 } 265 return cu_sp; 266 } 267 268 //---------------------------------------------------------------------- 269 // Compare function DWARFDebugAranges::Range structures 270 //---------------------------------------------------------------------- 271 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2) 272 { 273 return die1.GetOffset() < die2.GetOffset(); 274 } 275 276 277 //---------------------------------------------------------------------- 278 // GetDIE() 279 // 280 // Get the DIE (Debug Information Entry) with the specified offset. 281 //---------------------------------------------------------------------- 282 DWARFDebugInfoEntry* 283 DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr) 284 { 285 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset)); 286 if (cu_sp_ptr) 287 *cu_sp_ptr = cu_sp; 288 if (cu_sp.get()) 289 return cu_sp->GetDIEPtr(die_offset); 290 return NULL; // Not found in any compile units 291 } 292 293 DWARFDebugInfoEntry* 294 DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle) 295 { 296 assert (cu_handle); 297 DWARFDebugInfoEntry* die = NULL; 298 if (*cu_handle) 299 die = (*cu_handle)->GetDIEPtr(die_offset); 300 301 if (die == NULL) 302 { 303 DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset)); 304 if (cu_sp.get()) 305 { 306 *cu_handle = cu_sp.get(); 307 die = cu_sp->GetDIEPtr(die_offset); 308 } 309 } 310 if (die == NULL) 311 *cu_handle = NULL; 312 return die; 313 } 314 315 316 const DWARFDebugInfoEntry* 317 DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr) 318 { 319 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset)); 320 if (cu_sp_ptr) 321 *cu_sp_ptr = cu_sp; 322 if (cu_sp.get()) 323 return cu_sp->GetDIEPtrContainingOffset(die_offset); 324 325 return NULL; // Not found in any compile units 326 327 } 328 329 //---------------------------------------------------------------------- 330 // DWARFDebugInfo_ParseCallback 331 // 332 // A callback function for the static DWARFDebugInfo::Parse() function 333 // that gets parses all compile units and DIE's into an internate 334 // representation for further modification. 335 //---------------------------------------------------------------------- 336 337 static dw_offset_t 338 DWARFDebugInfo_ParseCallback 339 ( 340 SymbolFileDWARF* dwarf2Data, 341 DWARFCompileUnitSP& cu_sp, 342 DWARFDebugInfoEntry* die, 343 const dw_offset_t next_offset, 344 const uint32_t curr_depth, 345 void* userData 346 ) 347 { 348 DWARFDebugInfo* debug_info = (DWARFDebugInfo*)userData; 349 DWARFCompileUnit* cu = cu_sp.get(); 350 if (die) 351 { 352 cu->AddDIE(*die); 353 } 354 else if (cu) 355 { 356 debug_info->AddCompileUnit(cu_sp); 357 } 358 359 // Just return the current offset to parse the next CU or DIE entry 360 return next_offset; 361 } 362 363 //---------------------------------------------------------------------- 364 // AddCompileUnit 365 //---------------------------------------------------------------------- 366 void 367 DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu) 368 { 369 m_compile_units.push_back(cu); 370 } 371 372 /* 373 void 374 DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die) 375 { 376 m_die_array.push_back(die); 377 } 378 */ 379 380 381 382 383 //---------------------------------------------------------------------- 384 // Parse 385 // 386 // Parses the .debug_info section and uses the .debug_abbrev section 387 // and various other sections in the SymbolFileDWARF class and calls the 388 // supplied callback function each time a compile unit header, or debug 389 // information entry is successfully parsed. This function can be used 390 // for different tasks such as parsing the file contents into a 391 // structured data, dumping, verifying and much more. 392 //---------------------------------------------------------------------- 393 void 394 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData) 395 { 396 if (dwarf2Data) 397 { 398 uint32_t offset = 0; 399 uint32_t depth = 0; 400 DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data)); 401 if (cu.get() == NULL) 402 return; 403 DWARFDebugInfoEntry die; 404 405 while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset)) 406 { 407 const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset(); 408 409 depth = 0; 410 // Call the callback function with no DIE pointer for the compile unit 411 // and get the offset that we are to continue to parse from 412 offset = callback(dwarf2Data, cu, NULL, offset, depth, userData); 413 414 // Make sure we are within our compile unit 415 if (offset < next_cu_offset) 416 { 417 // We are in our compile unit, parse starting at the offset 418 // we were told to parse 419 bool done = false; 420 while (!done && die.Extract(dwarf2Data, cu.get(), &offset)) 421 { 422 // Call the callback function with DIE pointer that falls within the compile unit 423 offset = callback(dwarf2Data, cu, &die, offset, depth, userData); 424 425 if (die.IsNULL()) 426 { 427 if (depth) 428 --depth; 429 else 430 done = true; // We are done with this compile unit! 431 } 432 else if (die.HasChildren()) 433 ++depth; 434 } 435 } 436 437 // Make sure the offset returned is valid, and if not stop parsing. 438 // Returning DW_INVALID_OFFSET from this callback is a good way to end 439 // all parsing 440 if (!dwarf2Data->get_debug_info_data().ValidOffset(offset)) 441 break; 442 443 // See if during the callback anyone retained a copy of the compile 444 // unit other than ourselves and if so, let whomever did own the object 445 // and create a new one for our own use! 446 if (!cu.unique()) 447 cu.reset(new DWARFCompileUnit(dwarf2Data)); 448 449 450 // Make sure we start on a proper 451 offset = next_cu_offset; 452 } 453 } 454 } 455 456 typedef struct DumpInfo 457 { 458 DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) : 459 strm(init_strm), 460 die_offset(off), 461 recurse_depth(depth), 462 found_depth(UINT32_MAX), 463 found_die(false), 464 ancestors() 465 { 466 } 467 Stream* strm; 468 const uint32_t die_offset; 469 const uint32_t recurse_depth; 470 uint32_t found_depth; 471 bool found_die; 472 std::vector<DWARFDebugInfoEntry> ancestors; 473 474 DISALLOW_COPY_AND_ASSIGN(DumpInfo); 475 } DumpInfo; 476 477 //---------------------------------------------------------------------- 478 // DumpCallback 479 // 480 // A callback function for the static DWARFDebugInfo::Parse() function 481 // that gets called each time a compile unit header or debug information 482 // entry is successfully parsed. 483 // 484 // This function dump DWARF information and obey recurse depth and 485 // whether a single DIE is to be dumped (or all of the data). 486 //---------------------------------------------------------------------- 487 static dw_offset_t DumpCallback 488 ( 489 SymbolFileDWARF* dwarf2Data, 490 DWARFCompileUnitSP& cu_sp, 491 DWARFDebugInfoEntry* die, 492 const dw_offset_t next_offset, 493 const uint32_t curr_depth, 494 void* userData 495 ) 496 { 497 DumpInfo* dumpInfo = (DumpInfo*)userData; 498 499 const DWARFCompileUnit* cu = cu_sp.get(); 500 501 Stream *s = dumpInfo->strm; 502 bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors); 503 504 if (die) 505 { 506 // Are we dumping everything? 507 if (dumpInfo->die_offset == DW_INVALID_OFFSET) 508 { 509 // Yes we are dumping everything. Obey our recurse level though 510 if (curr_depth < dumpInfo->recurse_depth) 511 die->Dump(dwarf2Data, cu, *s, 0); 512 } 513 else 514 { 515 // We are dumping a specific DIE entry by offset 516 if (dumpInfo->die_offset == die->GetOffset()) 517 { 518 // We found the DIE we were looking for, dump it! 519 if (show_parents) 520 { 521 s->SetIndentLevel(0); 522 const uint32_t num_ancestors = dumpInfo->ancestors.size(); 523 if (num_ancestors > 0) 524 { 525 for (uint32_t i=0; i<num_ancestors-1; ++i) 526 { 527 dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0); 528 s->IndentMore(); 529 } 530 } 531 } 532 533 dumpInfo->found_depth = curr_depth; 534 535 die->Dump(dwarf2Data, cu, *s, 0); 536 537 // Note that we found the DIE we were looking for 538 dumpInfo->found_die = true; 539 540 // Since we are dumping a single DIE, if there are no children we are done! 541 if (!die->HasChildren() || dumpInfo->recurse_depth == 0) 542 return DW_INVALID_OFFSET; // Return an invalid address to end parsing 543 } 544 else if (dumpInfo->found_die) 545 { 546 // Are we done with all the children? 547 if (curr_depth <= dumpInfo->found_depth) 548 return DW_INVALID_OFFSET; 549 550 // We have already found our DIE and are printing it's children. Obey 551 // our recurse depth and return an invalid offset if we get done 552 // dumping all the the children 553 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth) 554 die->Dump(dwarf2Data, cu, *s, 0); 555 } 556 else if (dumpInfo->die_offset > die->GetOffset()) 557 { 558 if (show_parents) 559 dumpInfo->ancestors.back() = *die; 560 } 561 } 562 563 // Keep up with our indent level 564 if (die->IsNULL()) 565 { 566 if (show_parents) 567 dumpInfo->ancestors.pop_back(); 568 569 if (curr_depth <= 1) 570 return cu->GetNextCompileUnitOffset(); 571 else 572 s->IndentLess(); 573 } 574 else if (die->HasChildren()) 575 { 576 if (show_parents) 577 { 578 DWARFDebugInfoEntry null_die; 579 dumpInfo->ancestors.push_back(null_die); 580 } 581 s->IndentMore(); 582 } 583 } 584 else 585 { 586 if (cu == NULL) 587 s->PutCString("NULL - cu"); 588 // We have a compile unit, reset our indent level to zero just in case 589 s->SetIndentLevel(0); 590 591 // See if we are dumping everything? 592 if (dumpInfo->die_offset == DW_INVALID_OFFSET) 593 { 594 // We are dumping everything 595 cu->Dump(s); 596 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit 597 } 598 else 599 { 600 if (show_parents) 601 { 602 dumpInfo->ancestors.clear(); 603 dumpInfo->ancestors.resize(1); 604 } 605 606 // We are dumping only a single DIE possibly with it's children and 607 // we must find it's compile unit before we can dump it properly 608 if (dumpInfo->die_offset < cu->GetFirstDIEOffset()) 609 { 610 // Not found, maybe the DIE offset provided wasn't correct? 611 // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl; 612 return DW_INVALID_OFFSET; 613 } 614 else 615 { 616 // See if the DIE is in this compile unit? 617 if (dumpInfo->die_offset < cu->GetNextCompileUnitOffset()) 618 { 619 // This DIE is in this compile unit! 620 if (s->GetVerbose()) 621 cu->Dump(s); // Dump the compile unit for the DIE in verbose mode 622 623 return next_offset; 624 // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE... 625 // return dumpInfo->die_offset; 626 } 627 else 628 { 629 // Skip to the next compile unit as the DIE isn't in the current one! 630 return cu->GetNextCompileUnitOffset(); 631 } 632 } 633 } 634 } 635 636 // Just return the current offset to parse the next CU or DIE entry 637 return next_offset; 638 } 639 640 //---------------------------------------------------------------------- 641 // Dump 642 // 643 // Dump the information in the .debug_info section to the specified 644 // ostream. If die_offset is valid, a single DIE will be dumped. If the 645 // die_offset is invalid, all the DWARF information will be dumped. Both 646 // cases will obey a "recurse_depth" or how deep to traverse into the 647 // children of each DIE entry. A recurse_depth of zero will dump all 648 // compile unit headers. A recurse_depth of 1 will dump all compile unit 649 // headers and the DW_TAG_compile unit tags. A depth of 2 will also 650 // dump all types and functions. 651 //---------------------------------------------------------------------- 652 void 653 DWARFDebugInfo::Dump 654 ( 655 Stream *s, 656 SymbolFileDWARF* dwarf2Data, 657 const uint32_t die_offset, 658 const uint32_t recurse_depth 659 ) 660 { 661 DumpInfo dumpInfo(s, die_offset, recurse_depth); 662 s->PutCString(".debug_info contents"); 663 if (dwarf2Data->get_debug_info_data().GetByteSize() > 0) 664 { 665 if (die_offset == DW_INVALID_OFFSET) 666 s->PutCString(":\n"); 667 else 668 { 669 s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset); 670 if (recurse_depth != UINT32_MAX) 671 s->Printf(" recursing %u levels deep.", recurse_depth); 672 s->EOL(); 673 } 674 } 675 else 676 { 677 s->PutCString(": < EMPTY >\n"); 678 return; 679 } 680 DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo); 681 } 682 683 684 //---------------------------------------------------------------------- 685 // Dump 686 // 687 // Dump the contents of this DWARFDebugInfo object as has been parsed 688 // and/or modified after it has been parsed. 689 //---------------------------------------------------------------------- 690 void 691 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth) 692 { 693 DumpInfo dumpInfo(s, die_offset, recurse_depth); 694 695 s->PutCString("Dumping .debug_info section from internal representation\n"); 696 697 CompileUnitColl::const_iterator pos; 698 uint32_t curr_depth = 0; 699 ParseCompileUnitHeadersIfNeeded(); 700 for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos) 701 { 702 const DWARFCompileUnitSP& cu_sp = *pos; 703 DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo); 704 cu_sp->DIE()->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth); 705 } 706 } 707 708 709 //---------------------------------------------------------------------- 710 // FindCallbackString 711 // 712 // A callback function for the static DWARFDebugInfo::Parse() function 713 // that gets called each time a compile unit header or debug information 714 // entry is successfully parsed. 715 // 716 // This function will find the die_offset of any items whose DW_AT_name 717 // matches the given string 718 //---------------------------------------------------------------------- 719 typedef struct FindCallbackStringInfoTag 720 { 721 const char* name; 722 bool ignore_case; 723 RegularExpression* regex; 724 vector<dw_offset_t>& die_offsets; 725 } FindCallbackStringInfo; 726 727 static dw_offset_t FindCallbackString 728 ( 729 SymbolFileDWARF* dwarf2Data, 730 DWARFCompileUnitSP& cu_sp, 731 DWARFDebugInfoEntry* die, 732 const dw_offset_t next_offset, 733 const uint32_t curr_depth, 734 void* userData 735 ) 736 { 737 FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData; 738 const DWARFCompileUnit* cu = cu_sp.get(); 739 740 if (die) 741 { 742 const char* die_name = die->GetName(dwarf2Data, cu); 743 if (die_name) 744 { 745 if (info->regex) 746 { 747 if (info->regex->Execute(die_name)) 748 info->die_offsets.push_back(die->GetOffset()); 749 } 750 else 751 { 752 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0) 753 info->die_offsets.push_back(die->GetOffset()); 754 } 755 } 756 } 757 758 // Just return the current offset to parse the next CU or DIE entry 759 return next_offset; 760 } 761 762 //---------------------------------------------------------------------- 763 // Find 764 // 765 // Finds all DIE that have a specific DW_AT_name attribute by manually 766 // searching through the debug information (not using the 767 // .debug_pubnames section). The string must match the entire name 768 // and case sensitive searches are an option. 769 //---------------------------------------------------------------------- 770 bool 771 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const 772 { 773 die_offsets.clear(); 774 if (name && name[0]) 775 { 776 FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets }; 777 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info); 778 } 779 return !die_offsets.empty(); 780 } 781 782 //---------------------------------------------------------------------- 783 // Find 784 // 785 // Finds all DIE that have a specific DW_AT_name attribute by manually 786 // searching through the debug information (not using the 787 // .debug_pubnames section). The string must match the supplied regular 788 // expression. 789 //---------------------------------------------------------------------- 790 bool 791 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const 792 { 793 die_offsets.clear(); 794 FindCallbackStringInfo info = { NULL, false, &re, die_offsets }; 795 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info); 796 return !die_offsets.empty(); 797 } 798