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 for (size_t idx = 0; idx < num_compile_units; ++idx)
84 DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
86 dw_offset_t offset = cu->GetOffset();
87 if (cus_with_data.find(offset) == cus_with_data.end())
92 log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
93 m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
96 cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get());
100 const bool minimize = true;
101 m_cu_aranges_ap->Sort (minimize);
103 return *m_cu_aranges_ap.get();
107 //----------------------------------------------------------------------
109 //----------------------------------------------------------------------
111 DWARFDebugInfo::LookupAddress
113 const dw_addr_t address,
114 const dw_offset_t hint_die_offset,
115 DWARFCompileUnitSP& cu_sp,
116 DWARFDebugInfoEntry** function_die,
117 DWARFDebugInfoEntry** block_die
121 if (hint_die_offset != DW_INVALID_OFFSET)
122 cu_sp = GetCompileUnit(hint_die_offset);
125 DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
126 const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
127 cu_sp = GetCompileUnit(cu_offset);
132 if (cu_sp->LookupAddress(address, function_die, block_die))
138 // The hint_die_offset may have been a pointer to the actual item that
139 // we are looking for
140 DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp);
145 if (function_die || block_die)
146 return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
148 // We only wanted the compile unit that contained this address
158 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
160 if (m_compile_units.empty())
162 if (m_dwarf2Data != NULL)
164 lldb::offset_t offset = 0;
165 const DWARFDataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data();
166 while (debug_info_data.ValidOffset(offset))
168 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
170 if (cu_sp.get() == NULL)
173 if (cu_sp->Extract(debug_info_data, &offset) == false)
176 m_compile_units.push_back(cu_sp);
178 offset = cu_sp->GetNextCompileUnitOffset();
185 DWARFDebugInfo::GetNumCompileUnits()
187 ParseCompileUnitHeadersIfNeeded();
188 return m_compile_units.size();
192 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
194 DWARFCompileUnit* cu = NULL;
195 if (idx < GetNumCompileUnits())
196 cu = m_compile_units[idx].get();
201 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
203 // Not a verify efficient function, but it is handy for use in assertions
204 // to make sure that a compile unit comes from a debug information file.
205 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
206 CompileUnitColl::const_iterator pos;
208 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
210 if (pos->get() == cu)
217 CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem)
219 const dw_offset_t key_cu_offset = *(dw_offset_t*) key;
220 const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset();
221 if (key_cu_offset < cu_offset)
223 if (key_cu_offset > cu_offset)
229 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
231 DWARFCompileUnitSP cu_sp;
232 uint32_t cu_idx = DW_INVALID_INDEX;
233 if (cu_offset != DW_INVALID_OFFSET)
235 ParseCompileUnitHeadersIfNeeded();
237 DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset);
241 cu_idx = match - &m_compile_units[0];
250 DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
252 DWARFCompileUnitSP cu_sp;
253 if (die_offset != DW_INVALID_OFFSET)
255 ParseCompileUnitHeadersIfNeeded();
257 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
258 CompileUnitColl::const_iterator pos;
260 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
262 dw_offset_t cu_start_offset = (*pos)->GetOffset();
263 dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset();
264 if (cu_start_offset <= die_offset && die_offset < cu_end_offset)
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 //----------------------------------------------------------------------
328 //----------------------------------------------------------------------
330 DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
332 m_compile_units.push_back(cu);
337 DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
339 m_die_array.push_back(die);
346 //----------------------------------------------------------------------
349 // Parses the .debug_info section and uses the .debug_abbrev section
350 // and various other sections in the SymbolFileDWARF class and calls the
351 // supplied callback function each time a compile unit header, or debug
352 // information entry is successfully parsed. This function can be used
353 // for different tasks such as parsing the file contents into a
354 // structured data, dumping, verifying and much more.
355 //----------------------------------------------------------------------
357 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
361 lldb::offset_t offset = 0;
363 DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
364 if (cu.get() == NULL)
366 DWARFDebugInfoEntry die;
368 while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
370 const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
373 // Call the callback function with no DIE pointer for the compile unit
374 // and get the offset that we are to continue to parse from
375 offset = callback(dwarf2Data, cu, NULL, offset, depth, userData);
377 // Make sure we are within our compile unit
378 if (offset < next_cu_offset)
380 // We are in our compile unit, parse starting at the offset
381 // we were told to parse
383 while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
385 // Call the callback function with DIE pointer that falls within the compile unit
386 offset = callback(dwarf2Data, cu, &die, offset, depth, userData);
393 done = true; // We are done with this compile unit!
395 else if (die.HasChildren())
400 // Make sure the offset returned is valid, and if not stop parsing.
401 // Returning DW_INVALID_OFFSET from this callback is a good way to end
403 if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
406 // See if during the callback anyone retained a copy of the compile
407 // unit other than ourselves and if so, let whomever did own the object
408 // and create a new one for our own use!
410 cu.reset(new DWARFCompileUnit(dwarf2Data));
413 // Make sure we start on a proper
414 offset = next_cu_offset;
419 typedef struct DumpInfo
421 DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
424 recurse_depth(depth),
425 found_depth(UINT32_MAX),
431 const uint32_t die_offset;
432 const uint32_t recurse_depth;
433 uint32_t found_depth;
435 std::vector<DWARFDebugInfoEntry> ancestors;
437 DISALLOW_COPY_AND_ASSIGN(DumpInfo);
440 //----------------------------------------------------------------------
443 // A callback function for the static DWARFDebugInfo::Parse() function
444 // that gets called each time a compile unit header or debug information
445 // entry is successfully parsed.
447 // This function dump DWARF information and obey recurse depth and
448 // whether a single DIE is to be dumped (or all of the data).
449 //----------------------------------------------------------------------
450 static dw_offset_t DumpCallback
452 SymbolFileDWARF* dwarf2Data,
453 DWARFCompileUnitSP& cu_sp,
454 DWARFDebugInfoEntry* die,
455 const dw_offset_t next_offset,
456 const uint32_t curr_depth,
460 DumpInfo* dumpInfo = (DumpInfo*)userData;
462 const DWARFCompileUnit* cu = cu_sp.get();
464 Stream *s = dumpInfo->strm;
465 bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
469 // Are we dumping everything?
470 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
472 // Yes we are dumping everything. Obey our recurse level though
473 if (curr_depth < dumpInfo->recurse_depth)
474 die->Dump(dwarf2Data, cu, *s, 0);
478 // We are dumping a specific DIE entry by offset
479 if (dumpInfo->die_offset == die->GetOffset())
481 // We found the DIE we were looking for, dump it!
484 s->SetIndentLevel(0);
485 const uint32_t num_ancestors = dumpInfo->ancestors.size();
486 if (num_ancestors > 0)
488 for (uint32_t i=0; i<num_ancestors-1; ++i)
490 dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
496 dumpInfo->found_depth = curr_depth;
498 die->Dump(dwarf2Data, cu, *s, 0);
500 // Note that we found the DIE we were looking for
501 dumpInfo->found_die = true;
503 // Since we are dumping a single DIE, if there are no children we are done!
504 if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
505 return DW_INVALID_OFFSET; // Return an invalid address to end parsing
507 else if (dumpInfo->found_die)
509 // Are we done with all the children?
510 if (curr_depth <= dumpInfo->found_depth)
511 return DW_INVALID_OFFSET;
513 // We have already found our DIE and are printing it's children. Obey
514 // our recurse depth and return an invalid offset if we get done
515 // dumping all of the children
516 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
517 die->Dump(dwarf2Data, cu, *s, 0);
519 else if (dumpInfo->die_offset > die->GetOffset())
522 dumpInfo->ancestors.back() = *die;
526 // Keep up with our indent level
530 dumpInfo->ancestors.pop_back();
533 return cu->GetNextCompileUnitOffset();
537 else if (die->HasChildren())
541 DWARFDebugInfoEntry null_die;
542 dumpInfo->ancestors.push_back(null_die);
550 s->PutCString("NULL - cu");
551 // We have a compile unit, reset our indent level to zero just in case
552 s->SetIndentLevel(0);
554 // See if we are dumping everything?
555 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
557 // We are dumping everything
559 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
565 dumpInfo->ancestors.clear();
566 dumpInfo->ancestors.resize(1);
569 // We are dumping only a single DIE possibly with it's children and
570 // we must find it's compile unit before we can dump it properly
571 if (dumpInfo->die_offset < cu->GetFirstDIEOffset())
573 // Not found, maybe the DIE offset provided wasn't correct?
574 // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl;
575 return DW_INVALID_OFFSET;
579 // See if the DIE is in this compile unit?
580 if (dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
582 // This DIE is in this compile unit!
584 cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
587 // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
588 // return dumpInfo->die_offset;
592 // Skip to the next compile unit as the DIE isn't in the current one!
593 return cu->GetNextCompileUnitOffset();
599 // Just return the current offset to parse the next CU or DIE entry
603 //----------------------------------------------------------------------
606 // Dump the information in the .debug_info section to the specified
607 // ostream. If die_offset is valid, a single DIE will be dumped. If the
608 // die_offset is invalid, all the DWARF information will be dumped. Both
609 // cases will obey a "recurse_depth" or how deep to traverse into the
610 // children of each DIE entry. A recurse_depth of zero will dump all
611 // compile unit headers. A recurse_depth of 1 will dump all compile unit
612 // headers and the DW_TAG_compile unit tags. A depth of 2 will also
613 // dump all types and functions.
614 //----------------------------------------------------------------------
619 SymbolFileDWARF* dwarf2Data,
620 const uint32_t die_offset,
621 const uint32_t recurse_depth
624 DumpInfo dumpInfo(s, die_offset, recurse_depth);
625 s->PutCString(".debug_info contents");
626 if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
628 if (die_offset == DW_INVALID_OFFSET)
629 s->PutCString(":\n");
632 s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
633 if (recurse_depth != UINT32_MAX)
634 s->Printf(" recursing %u levels deep.", recurse_depth);
640 s->PutCString(": < EMPTY >\n");
643 DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
647 //----------------------------------------------------------------------
650 // Dump the contents of this DWARFDebugInfo object as has been parsed
651 // and/or modified after it has been parsed.
652 //----------------------------------------------------------------------
654 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
656 DumpInfo dumpInfo(s, die_offset, recurse_depth);
658 s->PutCString("Dumping .debug_info section from internal representation\n");
660 CompileUnitColl::const_iterator pos;
661 uint32_t curr_depth = 0;
662 ParseCompileUnitHeadersIfNeeded();
663 for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos)
665 const DWARFCompileUnitSP& cu_sp = *pos;
666 DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo);
668 const DWARFDebugInfoEntry* die = cu_sp->DIE();
670 die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth);
675 //----------------------------------------------------------------------
676 // FindCallbackString
678 // A callback function for the static DWARFDebugInfo::Parse() function
679 // that gets called each time a compile unit header or debug information
680 // entry is successfully parsed.
682 // This function will find the die_offset of any items whose DW_AT_name
683 // matches the given string
684 //----------------------------------------------------------------------
685 typedef struct FindCallbackStringInfoTag
689 RegularExpression* regex;
690 vector<dw_offset_t>& die_offsets;
691 } FindCallbackStringInfo;
693 static dw_offset_t FindCallbackString
695 SymbolFileDWARF* dwarf2Data,
696 DWARFCompileUnitSP& cu_sp,
697 DWARFDebugInfoEntry* die,
698 const dw_offset_t next_offset,
699 const uint32_t curr_depth,
703 FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
704 const DWARFCompileUnit* cu = cu_sp.get();
708 const char* die_name = die->GetName(dwarf2Data, cu);
713 if (info->regex->Execute(die_name))
714 info->die_offsets.push_back(die->GetOffset());
718 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
719 info->die_offsets.push_back(die->GetOffset());
724 // Just return the current offset to parse the next CU or DIE entry
728 //----------------------------------------------------------------------
731 // Finds all DIE that have a specific DW_AT_name attribute by manually
732 // searching through the debug information (not using the
733 // .debug_pubnames section). The string must match the entire name
734 // and case sensitive searches are an option.
735 //----------------------------------------------------------------------
737 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
742 FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
743 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
745 return !die_offsets.empty();
748 //----------------------------------------------------------------------
751 // Finds all DIE that have a specific DW_AT_name attribute by manually
752 // searching through the debug information (not using the
753 // .debug_pubnames section). The string must match the supplied regular
755 //----------------------------------------------------------------------
757 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
760 FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
761 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
762 return !die_offsets.empty();