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 DWARFDataExtractor &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);
70 // Make a list of all CUs represented by the arange data in the file.
71 std::set<dw_offset_t> cus_with_data;
72 for (size_t n=0;n<m_cu_aranges_ap.get()->GetNumRanges();n++)
74 dw_offset_t offset = m_cu_aranges_ap.get()->OffsetAtIndex(n);
75 if (offset != DW_INVALID_OFFSET)
76 cus_with_data.insert (offset);
79 // Manually build arange data for everything that wasn't in the .debug_aranges table.
81 const size_t num_compile_units = GetNumCompileUnits();
82 const bool clear_dies_if_already_not_parsed = true;
83 for (size_t idx = 0; idx < num_compile_units; ++idx)
85 DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
87 dw_offset_t offset = cu->GetOffset();
88 if (cus_with_data.find(offset) == cus_with_data.end())
93 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
94 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
97 cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get(), clear_dies_if_already_not_parsed);
101 const bool minimize = true;
102 m_cu_aranges_ap->Sort (minimize);
104 return *m_cu_aranges_ap.get();
108 //----------------------------------------------------------------------
110 //----------------------------------------------------------------------
112 DWARFDebugInfo::LookupAddress
114 const dw_addr_t address,
115 const dw_offset_t hint_die_offset,
116 DWARFCompileUnitSP& cu_sp,
117 DWARFDebugInfoEntry** function_die,
118 DWARFDebugInfoEntry** block_die
122 if (hint_die_offset != DW_INVALID_OFFSET)
123 cu_sp = GetCompileUnit(hint_die_offset);
126 DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
127 const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
128 cu_sp = GetCompileUnit(cu_offset);
133 if (cu_sp->LookupAddress(address, function_die, block_die))
139 // The hint_die_offset may have been a pointer to the actual item that
140 // we are looking for
141 DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp);
146 if (function_die || block_die)
147 return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
149 // We only wanted the compile unit that contained this address
159 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
161 if (m_compile_units.empty())
163 if (m_dwarf2Data != NULL)
165 lldb::offset_t offset = 0;
166 const DWARFDataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data();
167 while (debug_info_data.ValidOffset(offset))
169 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
171 if (cu_sp.get() == NULL)
174 if (cu_sp->Extract(debug_info_data, &offset) == false)
177 m_compile_units.push_back(cu_sp);
179 offset = cu_sp->GetNextCompileUnitOffset();
186 DWARFDebugInfo::GetNumCompileUnits()
188 ParseCompileUnitHeadersIfNeeded();
189 return m_compile_units.size();
193 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
195 DWARFCompileUnit* cu = NULL;
196 if (idx < GetNumCompileUnits())
197 cu = m_compile_units[idx].get();
202 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
204 // Not a verify efficient function, but it is handy for use in assertions
205 // to make sure that a compile unit comes from a debug information file.
206 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
207 CompileUnitColl::const_iterator pos;
209 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
211 if (pos->get() == cu)
218 static bool CompileUnitOffsetLessThan (const DWARFCompileUnitSP& a, const DWARFCompileUnitSP& b)
220 return a->GetOffset() < b->GetOffset();
225 CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem)
227 const dw_offset_t key_cu_offset = *(dw_offset_t*) key;
228 const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset();
229 if (key_cu_offset < cu_offset)
231 if (key_cu_offset > cu_offset)
237 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
239 DWARFCompileUnitSP cu_sp;
240 uint32_t cu_idx = DW_INVALID_INDEX;
241 if (cu_offset != DW_INVALID_OFFSET)
243 ParseCompileUnitHeadersIfNeeded();
245 DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset);
249 cu_idx = match - &m_compile_units[0];
258 DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
260 DWARFCompileUnitSP cu_sp;
261 if (die_offset != DW_INVALID_OFFSET)
263 ParseCompileUnitHeadersIfNeeded();
265 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
266 CompileUnitColl::const_iterator pos;
268 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
270 dw_offset_t cu_start_offset = (*pos)->GetOffset();
271 dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset();
272 if (cu_start_offset <= die_offset && die_offset < cu_end_offset)
282 //----------------------------------------------------------------------
283 // Compare function DWARFDebugAranges::Range structures
284 //----------------------------------------------------------------------
285 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
287 return die1.GetOffset() < die2.GetOffset();
291 //----------------------------------------------------------------------
294 // Get the DIE (Debug Information Entry) with the specified offset.
295 //----------------------------------------------------------------------
297 DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
299 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
303 return cu_sp->GetDIEPtr(die_offset);
304 return NULL; // Not found in any compile units
308 DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle)
311 DWARFDebugInfoEntry* die = NULL;
313 die = (*cu_handle)->GetDIEPtr(die_offset);
317 DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset));
320 *cu_handle = cu_sp.get();
321 die = cu_sp->GetDIEPtr(die_offset);
330 const DWARFDebugInfoEntry*
331 DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
333 DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
337 return cu_sp->GetDIEPtrContainingOffset(die_offset);
339 return NULL; // Not found in any compile units
343 //----------------------------------------------------------------------
344 // DWARFDebugInfo_ParseCallback
346 // A callback function for the static DWARFDebugInfo::Parse() function
347 // that gets parses all compile units and DIE's into an internate
348 // representation for further modification.
349 //----------------------------------------------------------------------
352 DWARFDebugInfo_ParseCallback
354 SymbolFileDWARF* dwarf2Data,
355 DWARFCompileUnitSP& cu_sp,
356 DWARFDebugInfoEntry* die,
357 const dw_offset_t next_offset,
358 const uint32_t curr_depth,
362 DWARFDebugInfo* debug_info = (DWARFDebugInfo*)userData;
363 DWARFCompileUnit* cu = cu_sp.get();
370 debug_info->AddCompileUnit(cu_sp);
373 // Just return the current offset to parse the next CU or DIE entry
377 //----------------------------------------------------------------------
379 //----------------------------------------------------------------------
381 DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
383 m_compile_units.push_back(cu);
388 DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
390 m_die_array.push_back(die);
397 //----------------------------------------------------------------------
400 // Parses the .debug_info section and uses the .debug_abbrev section
401 // and various other sections in the SymbolFileDWARF class and calls the
402 // supplied callback function each time a compile unit header, or debug
403 // information entry is successfully parsed. This function can be used
404 // for different tasks such as parsing the file contents into a
405 // structured data, dumping, verifying and much more.
406 //----------------------------------------------------------------------
408 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
412 lldb::offset_t offset = 0;
414 DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
415 if (cu.get() == NULL)
417 DWARFDebugInfoEntry die;
419 while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
421 const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
424 // Call the callback function with no DIE pointer for the compile unit
425 // and get the offset that we are to continue to parse from
426 offset = callback(dwarf2Data, cu, NULL, offset, depth, userData);
428 // Make sure we are within our compile unit
429 if (offset < next_cu_offset)
431 // We are in our compile unit, parse starting at the offset
432 // we were told to parse
434 while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
436 // Call the callback function with DIE pointer that falls within the compile unit
437 offset = callback(dwarf2Data, cu, &die, offset, depth, userData);
444 done = true; // We are done with this compile unit!
446 else if (die.HasChildren())
451 // Make sure the offset returned is valid, and if not stop parsing.
452 // Returning DW_INVALID_OFFSET from this callback is a good way to end
454 if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
457 // See if during the callback anyone retained a copy of the compile
458 // unit other than ourselves and if so, let whomever did own the object
459 // and create a new one for our own use!
461 cu.reset(new DWARFCompileUnit(dwarf2Data));
464 // Make sure we start on a proper
465 offset = next_cu_offset;
470 typedef struct DumpInfo
472 DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
475 recurse_depth(depth),
476 found_depth(UINT32_MAX),
482 const uint32_t die_offset;
483 const uint32_t recurse_depth;
484 uint32_t found_depth;
486 std::vector<DWARFDebugInfoEntry> ancestors;
488 DISALLOW_COPY_AND_ASSIGN(DumpInfo);
491 //----------------------------------------------------------------------
494 // A callback function for the static DWARFDebugInfo::Parse() function
495 // that gets called each time a compile unit header or debug information
496 // entry is successfully parsed.
498 // This function dump DWARF information and obey recurse depth and
499 // whether a single DIE is to be dumped (or all of the data).
500 //----------------------------------------------------------------------
501 static dw_offset_t DumpCallback
503 SymbolFileDWARF* dwarf2Data,
504 DWARFCompileUnitSP& cu_sp,
505 DWARFDebugInfoEntry* die,
506 const dw_offset_t next_offset,
507 const uint32_t curr_depth,
511 DumpInfo* dumpInfo = (DumpInfo*)userData;
513 const DWARFCompileUnit* cu = cu_sp.get();
515 Stream *s = dumpInfo->strm;
516 bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
520 // Are we dumping everything?
521 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
523 // Yes we are dumping everything. Obey our recurse level though
524 if (curr_depth < dumpInfo->recurse_depth)
525 die->Dump(dwarf2Data, cu, *s, 0);
529 // We are dumping a specific DIE entry by offset
530 if (dumpInfo->die_offset == die->GetOffset())
532 // We found the DIE we were looking for, dump it!
535 s->SetIndentLevel(0);
536 const uint32_t num_ancestors = dumpInfo->ancestors.size();
537 if (num_ancestors > 0)
539 for (uint32_t i=0; i<num_ancestors-1; ++i)
541 dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
547 dumpInfo->found_depth = curr_depth;
549 die->Dump(dwarf2Data, cu, *s, 0);
551 // Note that we found the DIE we were looking for
552 dumpInfo->found_die = true;
554 // Since we are dumping a single DIE, if there are no children we are done!
555 if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
556 return DW_INVALID_OFFSET; // Return an invalid address to end parsing
558 else if (dumpInfo->found_die)
560 // Are we done with all the children?
561 if (curr_depth <= dumpInfo->found_depth)
562 return DW_INVALID_OFFSET;
564 // We have already found our DIE and are printing it's children. Obey
565 // our recurse depth and return an invalid offset if we get done
566 // dumping all the the children
567 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
568 die->Dump(dwarf2Data, cu, *s, 0);
570 else if (dumpInfo->die_offset > die->GetOffset())
573 dumpInfo->ancestors.back() = *die;
577 // Keep up with our indent level
581 dumpInfo->ancestors.pop_back();
584 return cu->GetNextCompileUnitOffset();
588 else if (die->HasChildren())
592 DWARFDebugInfoEntry null_die;
593 dumpInfo->ancestors.push_back(null_die);
601 s->PutCString("NULL - cu");
602 // We have a compile unit, reset our indent level to zero just in case
603 s->SetIndentLevel(0);
605 // See if we are dumping everything?
606 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
608 // We are dumping everything
610 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
616 dumpInfo->ancestors.clear();
617 dumpInfo->ancestors.resize(1);
620 // We are dumping only a single DIE possibly with it's children and
621 // we must find it's compile unit before we can dump it properly
622 if (dumpInfo->die_offset < cu->GetFirstDIEOffset())
624 // Not found, maybe the DIE offset provided wasn't correct?
625 // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl;
626 return DW_INVALID_OFFSET;
630 // See if the DIE is in this compile unit?
631 if (dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
633 // This DIE is in this compile unit!
635 cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
638 // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
639 // return dumpInfo->die_offset;
643 // Skip to the next compile unit as the DIE isn't in the current one!
644 return cu->GetNextCompileUnitOffset();
650 // Just return the current offset to parse the next CU or DIE entry
654 //----------------------------------------------------------------------
657 // Dump the information in the .debug_info section to the specified
658 // ostream. If die_offset is valid, a single DIE will be dumped. If the
659 // die_offset is invalid, all the DWARF information will be dumped. Both
660 // cases will obey a "recurse_depth" or how deep to traverse into the
661 // children of each DIE entry. A recurse_depth of zero will dump all
662 // compile unit headers. A recurse_depth of 1 will dump all compile unit
663 // headers and the DW_TAG_compile unit tags. A depth of 2 will also
664 // dump all types and functions.
665 //----------------------------------------------------------------------
670 SymbolFileDWARF* dwarf2Data,
671 const uint32_t die_offset,
672 const uint32_t recurse_depth
675 DumpInfo dumpInfo(s, die_offset, recurse_depth);
676 s->PutCString(".debug_info contents");
677 if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
679 if (die_offset == DW_INVALID_OFFSET)
680 s->PutCString(":\n");
683 s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
684 if (recurse_depth != UINT32_MAX)
685 s->Printf(" recursing %u levels deep.", recurse_depth);
691 s->PutCString(": < EMPTY >\n");
694 DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
698 //----------------------------------------------------------------------
701 // Dump the contents of this DWARFDebugInfo object as has been parsed
702 // and/or modified after it has been parsed.
703 //----------------------------------------------------------------------
705 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
707 DumpInfo dumpInfo(s, die_offset, recurse_depth);
709 s->PutCString("Dumping .debug_info section from internal representation\n");
711 CompileUnitColl::const_iterator pos;
712 uint32_t curr_depth = 0;
713 ParseCompileUnitHeadersIfNeeded();
714 for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos)
716 const DWARFCompileUnitSP& cu_sp = *pos;
717 DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo);
719 const DWARFDebugInfoEntry* die = cu_sp->DIE();
721 die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth);
726 //----------------------------------------------------------------------
727 // FindCallbackString
729 // A callback function for the static DWARFDebugInfo::Parse() function
730 // that gets called each time a compile unit header or debug information
731 // entry is successfully parsed.
733 // This function will find the die_offset of any items whose DW_AT_name
734 // matches the given string
735 //----------------------------------------------------------------------
736 typedef struct FindCallbackStringInfoTag
740 RegularExpression* regex;
741 vector<dw_offset_t>& die_offsets;
742 } FindCallbackStringInfo;
744 static dw_offset_t FindCallbackString
746 SymbolFileDWARF* dwarf2Data,
747 DWARFCompileUnitSP& cu_sp,
748 DWARFDebugInfoEntry* die,
749 const dw_offset_t next_offset,
750 const uint32_t curr_depth,
754 FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
755 const DWARFCompileUnit* cu = cu_sp.get();
759 const char* die_name = die->GetName(dwarf2Data, cu);
764 if (info->regex->Execute(die_name))
765 info->die_offsets.push_back(die->GetOffset());
769 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
770 info->die_offsets.push_back(die->GetOffset());
775 // Just return the current offset to parse the next CU or DIE entry
779 //----------------------------------------------------------------------
782 // Finds all DIE that have a specific DW_AT_name attribute by manually
783 // searching through the debug information (not using the
784 // .debug_pubnames section). The string must match the entire name
785 // and case sensitive searches are an option.
786 //----------------------------------------------------------------------
788 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
793 FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
794 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
796 return !die_offsets.empty();
799 //----------------------------------------------------------------------
802 // Finds all DIE that have a specific DW_AT_name attribute by manually
803 // searching through the debug information (not using the
804 // .debug_pubnames section). The string must match the supplied regular
806 //----------------------------------------------------------------------
808 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
811 FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
812 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
813 return !die_offsets.empty();