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