1 //===-- DWARFDebugInfo.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolFileDWARF.h"
15 #include "lldb/Core/RegularExpression.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Symbol/ObjectFile.h"
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"
28 using namespace lldb_private;
31 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
34 DWARFDebugInfo::DWARFDebugInfo() :
41 //----------------------------------------------------------------------
43 //----------------------------------------------------------------------
45 DWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data)
47 m_dwarf2Data = dwarf2Data;
48 m_compile_units.clear();
53 DWARFDebugInfo::GetCompileUnitAranges ()
55 if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data)
57 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
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)
64 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" from .debug_aranges",
65 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
66 m_cu_aranges_ap->Extract (debug_aranges_data);
72 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
73 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
74 const size_t num_compile_units = GetNumCompileUnits();
75 const bool clear_dies_if_already_not_parsed = true;
76 for (size_t idx = 0; idx < num_compile_units; ++idx)
78 DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
80 cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get(), clear_dies_if_already_not_parsed);
84 const bool minimize = true;
85 m_cu_aranges_ap->Sort (minimize);
87 return *m_cu_aranges_ap.get();
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
95 DWARFDebugInfo::LookupAddress
97 const dw_addr_t address,
98 const dw_offset_t hint_die_offset,
99 DWARFCompileUnitSP& cu_sp,
100 DWARFDebugInfoEntry** function_die,
101 DWARFDebugInfoEntry** block_die
105 if (hint_die_offset != DW_INVALID_OFFSET)
106 cu_sp = GetCompileUnit(hint_die_offset);
109 DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
110 const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
111 cu_sp = GetCompileUnit(cu_offset);
116 if (cu_sp->LookupAddress(address, function_die, block_die))
122 // The hint_die_offset may have been a pointer to the actual item that
123 // we are looking for
124 DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp);
129 if (function_die || block_die)
130 return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
132 // We only wanted the compile unit that contained this address
142 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
144 if (m_compile_units.empty())
146 if (m_dwarf2Data != NULL)
148 lldb::offset_t offset = 0;
149 const DataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data();
150 while (debug_info_data.ValidOffset(offset))
152 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
154 if (cu_sp.get() == NULL)
157 if (cu_sp->Extract(debug_info_data, &offset) == false)
160 m_compile_units.push_back(cu_sp);
162 offset = cu_sp->GetNextCompileUnitOffset();
169 DWARFDebugInfo::GetNumCompileUnits()
171 ParseCompileUnitHeadersIfNeeded();
172 return m_compile_units.size();
176 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
178 DWARFCompileUnit* cu = NULL;
179 if (idx < GetNumCompileUnits())
180 cu = m_compile_units[idx].get();
185 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
187 // Not a verify efficient function, but it is handy for use in assertions
188 // to make sure that a compile unit comes from a debug information file.
189 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
190 CompileUnitColl::const_iterator pos;
192 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
194 if (pos->get() == cu)
201 static bool CompileUnitOffsetLessThan (const DWARFCompileUnitSP& a, const DWARFCompileUnitSP& b)
203 return a->GetOffset() < b->GetOffset();
208 CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem)
210 const dw_offset_t key_cu_offset = *(dw_offset_t*) key;
211 const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset();
212 if (key_cu_offset < cu_offset)
214 if (key_cu_offset > cu_offset)
220 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
222 DWARFCompileUnitSP cu_sp;
223 uint32_t cu_idx = DW_INVALID_INDEX;
224 if (cu_offset != DW_INVALID_OFFSET)
226 ParseCompileUnitHeadersIfNeeded();
228 DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset);
232 cu_idx = match - &m_compile_units[0];
241 DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
243 DWARFCompileUnitSP cu_sp;
244 if (die_offset != DW_INVALID_OFFSET)
246 ParseCompileUnitHeadersIfNeeded();
248 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
249 CompileUnitColl::const_iterator pos;
251 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
253 dw_offset_t cu_start_offset = (*pos)->GetOffset();
254 dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset();
255 if (cu_start_offset <= die_offset && die_offset < cu_end_offset)
265 //----------------------------------------------------------------------
266 // Compare function DWARFDebugAranges::Range structures
267 //----------------------------------------------------------------------
268 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
270 return die1.GetOffset() < die2.GetOffset();
274 //----------------------------------------------------------------------
277 // Get the DIE (Debug Information Entry) with the specified offset.
278 //----------------------------------------------------------------------
280 DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
282 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
286 return cu_sp->GetDIEPtr(die_offset);
287 return NULL; // Not found in any compile units
291 DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle)
294 DWARFDebugInfoEntry* die = NULL;
296 die = (*cu_handle)->GetDIEPtr(die_offset);
300 DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset));
303 *cu_handle = cu_sp.get();
304 die = cu_sp->GetDIEPtr(die_offset);
313 const DWARFDebugInfoEntry*
314 DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
316 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
320 return cu_sp->GetDIEPtrContainingOffset(die_offset);
322 return NULL; // Not found in any compile units
326 //----------------------------------------------------------------------
327 // DWARFDebugInfo_ParseCallback
329 // A callback function for the static DWARFDebugInfo::Parse() function
330 // that gets parses all compile units and DIE's into an internate
331 // representation for further modification.
332 //----------------------------------------------------------------------
335 DWARFDebugInfo_ParseCallback
337 SymbolFileDWARF* dwarf2Data,
338 DWARFCompileUnitSP& cu_sp,
339 DWARFDebugInfoEntry* die,
340 const dw_offset_t next_offset,
341 const uint32_t curr_depth,
345 DWARFDebugInfo* debug_info = (DWARFDebugInfo*)userData;
346 DWARFCompileUnit* cu = cu_sp.get();
353 debug_info->AddCompileUnit(cu_sp);
356 // Just return the current offset to parse the next CU or DIE entry
360 //----------------------------------------------------------------------
362 //----------------------------------------------------------------------
364 DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
366 m_compile_units.push_back(cu);
371 DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
373 m_die_array.push_back(die);
380 //----------------------------------------------------------------------
383 // Parses the .debug_info section and uses the .debug_abbrev section
384 // and various other sections in the SymbolFileDWARF class and calls the
385 // supplied callback function each time a compile unit header, or debug
386 // information entry is successfully parsed. This function can be used
387 // for different tasks such as parsing the file contents into a
388 // structured data, dumping, verifying and much more.
389 //----------------------------------------------------------------------
391 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
395 lldb::offset_t offset = 0;
397 DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
398 if (cu.get() == NULL)
400 DWARFDebugInfoEntry die;
402 while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
404 const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
407 // Call the callback function with no DIE pointer for the compile unit
408 // and get the offset that we are to continue to parse from
409 offset = callback(dwarf2Data, cu, NULL, offset, depth, userData);
411 // Make sure we are within our compile unit
412 if (offset < next_cu_offset)
414 // We are in our compile unit, parse starting at the offset
415 // we were told to parse
417 while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
419 // Call the callback function with DIE pointer that falls within the compile unit
420 offset = callback(dwarf2Data, cu, &die, offset, depth, userData);
427 done = true; // We are done with this compile unit!
429 else if (die.HasChildren())
434 // Make sure the offset returned is valid, and if not stop parsing.
435 // Returning DW_INVALID_OFFSET from this callback is a good way to end
437 if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
440 // See if during the callback anyone retained a copy of the compile
441 // unit other than ourselves and if so, let whomever did own the object
442 // and create a new one for our own use!
444 cu.reset(new DWARFCompileUnit(dwarf2Data));
447 // Make sure we start on a proper
448 offset = next_cu_offset;
453 typedef struct DumpInfo
455 DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
458 recurse_depth(depth),
459 found_depth(UINT32_MAX),
465 const uint32_t die_offset;
466 const uint32_t recurse_depth;
467 uint32_t found_depth;
469 std::vector<DWARFDebugInfoEntry> ancestors;
471 DISALLOW_COPY_AND_ASSIGN(DumpInfo);
474 //----------------------------------------------------------------------
477 // A callback function for the static DWARFDebugInfo::Parse() function
478 // that gets called each time a compile unit header or debug information
479 // entry is successfully parsed.
481 // This function dump DWARF information and obey recurse depth and
482 // whether a single DIE is to be dumped (or all of the data).
483 //----------------------------------------------------------------------
484 static dw_offset_t DumpCallback
486 SymbolFileDWARF* dwarf2Data,
487 DWARFCompileUnitSP& cu_sp,
488 DWARFDebugInfoEntry* die,
489 const dw_offset_t next_offset,
490 const uint32_t curr_depth,
494 DumpInfo* dumpInfo = (DumpInfo*)userData;
496 const DWARFCompileUnit* cu = cu_sp.get();
498 Stream *s = dumpInfo->strm;
499 bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
503 // Are we dumping everything?
504 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
506 // Yes we are dumping everything. Obey our recurse level though
507 if (curr_depth < dumpInfo->recurse_depth)
508 die->Dump(dwarf2Data, cu, *s, 0);
512 // We are dumping a specific DIE entry by offset
513 if (dumpInfo->die_offset == die->GetOffset())
515 // We found the DIE we were looking for, dump it!
518 s->SetIndentLevel(0);
519 const uint32_t num_ancestors = dumpInfo->ancestors.size();
520 if (num_ancestors > 0)
522 for (uint32_t i=0; i<num_ancestors-1; ++i)
524 dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
530 dumpInfo->found_depth = curr_depth;
532 die->Dump(dwarf2Data, cu, *s, 0);
534 // Note that we found the DIE we were looking for
535 dumpInfo->found_die = true;
537 // Since we are dumping a single DIE, if there are no children we are done!
538 if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
539 return DW_INVALID_OFFSET; // Return an invalid address to end parsing
541 else if (dumpInfo->found_die)
543 // Are we done with all the children?
544 if (curr_depth <= dumpInfo->found_depth)
545 return DW_INVALID_OFFSET;
547 // We have already found our DIE and are printing it's children. Obey
548 // our recurse depth and return an invalid offset if we get done
549 // dumping all the the children
550 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
551 die->Dump(dwarf2Data, cu, *s, 0);
553 else if (dumpInfo->die_offset > die->GetOffset())
556 dumpInfo->ancestors.back() = *die;
560 // Keep up with our indent level
564 dumpInfo->ancestors.pop_back();
567 return cu->GetNextCompileUnitOffset();
571 else if (die->HasChildren())
575 DWARFDebugInfoEntry null_die;
576 dumpInfo->ancestors.push_back(null_die);
584 s->PutCString("NULL - cu");
585 // We have a compile unit, reset our indent level to zero just in case
586 s->SetIndentLevel(0);
588 // See if we are dumping everything?
589 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
591 // We are dumping everything
593 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
599 dumpInfo->ancestors.clear();
600 dumpInfo->ancestors.resize(1);
603 // We are dumping only a single DIE possibly with it's children and
604 // we must find it's compile unit before we can dump it properly
605 if (dumpInfo->die_offset < cu->GetFirstDIEOffset())
607 // Not found, maybe the DIE offset provided wasn't correct?
608 // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl;
609 return DW_INVALID_OFFSET;
613 // See if the DIE is in this compile unit?
614 if (dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
616 // This DIE is in this compile unit!
618 cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
621 // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
622 // return dumpInfo->die_offset;
626 // Skip to the next compile unit as the DIE isn't in the current one!
627 return cu->GetNextCompileUnitOffset();
633 // Just return the current offset to parse the next CU or DIE entry
637 //----------------------------------------------------------------------
640 // Dump the information in the .debug_info section to the specified
641 // ostream. If die_offset is valid, a single DIE will be dumped. If the
642 // die_offset is invalid, all the DWARF information will be dumped. Both
643 // cases will obey a "recurse_depth" or how deep to traverse into the
644 // children of each DIE entry. A recurse_depth of zero will dump all
645 // compile unit headers. A recurse_depth of 1 will dump all compile unit
646 // headers and the DW_TAG_compile unit tags. A depth of 2 will also
647 // dump all types and functions.
648 //----------------------------------------------------------------------
653 SymbolFileDWARF* dwarf2Data,
654 const uint32_t die_offset,
655 const uint32_t recurse_depth
658 DumpInfo dumpInfo(s, die_offset, recurse_depth);
659 s->PutCString(".debug_info contents");
660 if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
662 if (die_offset == DW_INVALID_OFFSET)
663 s->PutCString(":\n");
666 s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
667 if (recurse_depth != UINT32_MAX)
668 s->Printf(" recursing %u levels deep.", recurse_depth);
674 s->PutCString(": < EMPTY >\n");
677 DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
681 //----------------------------------------------------------------------
684 // Dump the contents of this DWARFDebugInfo object as has been parsed
685 // and/or modified after it has been parsed.
686 //----------------------------------------------------------------------
688 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
690 DumpInfo dumpInfo(s, die_offset, recurse_depth);
692 s->PutCString("Dumping .debug_info section from internal representation\n");
694 CompileUnitColl::const_iterator pos;
695 uint32_t curr_depth = 0;
696 ParseCompileUnitHeadersIfNeeded();
697 for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos)
699 const DWARFCompileUnitSP& cu_sp = *pos;
700 DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo);
702 const DWARFDebugInfoEntry* die = cu_sp->DIE();
704 die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth);
709 //----------------------------------------------------------------------
710 // FindCallbackString
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.
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
723 RegularExpression* regex;
724 vector<dw_offset_t>& die_offsets;
725 } FindCallbackStringInfo;
727 static dw_offset_t FindCallbackString
729 SymbolFileDWARF* dwarf2Data,
730 DWARFCompileUnitSP& cu_sp,
731 DWARFDebugInfoEntry* die,
732 const dw_offset_t next_offset,
733 const uint32_t curr_depth,
737 FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
738 const DWARFCompileUnit* cu = cu_sp.get();
742 const char* die_name = die->GetName(dwarf2Data, cu);
747 if (info->regex->Execute(die_name))
748 info->die_offsets.push_back(die->GetOffset());
752 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
753 info->die_offsets.push_back(die->GetOffset());
758 // Just return the current offset to parse the next CU or DIE entry
762 //----------------------------------------------------------------------
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 //----------------------------------------------------------------------
771 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
776 FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
777 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
779 return !die_offsets.empty();
782 //----------------------------------------------------------------------
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
789 //----------------------------------------------------------------------
791 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
794 FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
795 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
796 return !die_offsets.empty();