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 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
109 if (m_compile_units.empty())
111 if (m_dwarf2Data != NULL)
113 lldb::offset_t offset = 0;
114 const DWARFDataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data();
115 while (debug_info_data.ValidOffset(offset))
117 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
119 if (cu_sp.get() == NULL)
122 if (cu_sp->Extract(debug_info_data, &offset) == false)
125 m_compile_units.push_back(cu_sp);
127 offset = cu_sp->GetNextCompileUnitOffset();
134 DWARFDebugInfo::GetNumCompileUnits()
136 ParseCompileUnitHeadersIfNeeded();
137 return m_compile_units.size();
141 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
143 DWARFCompileUnit* cu = NULL;
144 if (idx < GetNumCompileUnits())
145 cu = m_compile_units[idx].get();
150 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
152 // Not a verify efficient function, but it is handy for use in assertions
153 // to make sure that a compile unit comes from a debug information file.
154 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
155 CompileUnitColl::const_iterator pos;
157 for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
159 if (pos->get() == cu)
166 DWARFDebugInfo::OffsetLessThanCompileUnitOffset (dw_offset_t offset, const DWARFCompileUnitSP& cu_sp)
168 return offset < cu_sp->GetOffset();
172 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
174 DWARFCompileUnitSP cu_sp;
175 uint32_t cu_idx = DW_INVALID_INDEX;
176 if (cu_offset != DW_INVALID_OFFSET)
178 ParseCompileUnitHeadersIfNeeded();
180 // Watch out for single compile unit executable as they are pretty common
181 const size_t num_cus = m_compile_units.size();
184 if (m_compile_units[0]->GetOffset() == cu_offset)
186 cu_sp = m_compile_units[0];
192 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
193 CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
194 CompileUnitColl::const_iterator pos = std::upper_bound(begin_pos, end_pos, cu_offset, OffsetLessThanCompileUnitOffset);
195 if (pos != begin_pos)
198 if ((*pos)->GetOffset() == cu_offset)
201 cu_idx = std::distance(begin_pos, pos);
212 DWARFDebugInfo::GetCompileUnitContainingDIE (const DIERef& die_ref)
214 dw_offset_t search_offset = die_ref.die_offset;
215 bool is_cu_offset = false;
216 if (m_dwarf2Data->GetID() == 0 && die_ref.cu_offset != DW_INVALID_OFFSET)
219 search_offset = die_ref.cu_offset;
222 DWARFCompileUnitSP cu_sp;
223 if (search_offset != DW_INVALID_OFFSET)
225 ParseCompileUnitHeadersIfNeeded();
227 // Watch out for single compile unit executable as they are pretty common
228 const size_t num_cus = m_compile_units.size();
231 if ((is_cu_offset && m_compile_units[0]->GetOffset() == search_offset) ||
232 (!is_cu_offset && m_compile_units[0]->ContainsDIEOffset(search_offset)))
234 cu_sp = m_compile_units[0];
239 CompileUnitColl::const_iterator end_pos = m_compile_units.end();
240 CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
241 CompileUnitColl::const_iterator pos = std::upper_bound(begin_pos, end_pos, search_offset, OffsetLessThanCompileUnitOffset);
242 if (pos != begin_pos)
245 if ((is_cu_offset && (*pos)->GetOffset() == search_offset) ||
246 (!is_cu_offset && (*pos)->ContainsDIEOffset(search_offset)))
256 //----------------------------------------------------------------------
259 // Get the DIE (Debug Information Entry) with the specified offset.
260 //----------------------------------------------------------------------
262 DWARFDebugInfo::GetDIE(const DIERef& die_ref)
264 DWARFCompileUnit *cu = GetCompileUnitContainingDIE(die_ref);
266 return cu->GetDIE (die_ref.die_offset);
267 return DWARFDIE(); // Not found
270 //----------------------------------------------------------------------
273 // Parses the .debug_info section and uses the .debug_abbrev section
274 // and various other sections in the SymbolFileDWARF class and calls the
275 // supplied callback function each time a compile unit header, or debug
276 // information entry is successfully parsed. This function can be used
277 // for different tasks such as parsing the file contents into a
278 // structured data, dumping, verifying and much more.
279 //----------------------------------------------------------------------
281 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
285 lldb::offset_t offset = 0;
287 DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
288 if (cu.get() == NULL)
290 DWARFDebugInfoEntry die;
292 while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
294 const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
297 // Call the callback function with no DIE pointer for the compile unit
298 // and get the offset that we are to continue to parse from
299 offset = callback(dwarf2Data, cu.get(), NULL, offset, depth, userData);
301 // Make sure we are within our compile unit
302 if (offset < next_cu_offset)
304 // We are in our compile unit, parse starting at the offset
305 // we were told to parse
307 while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
309 // Call the callback function with DIE pointer that falls within the compile unit
310 offset = callback(dwarf2Data, cu.get(), &die, offset, depth, userData);
317 done = true; // We are done with this compile unit!
319 else if (die.HasChildren())
324 // Make sure the offset returned is valid, and if not stop parsing.
325 // Returning DW_INVALID_OFFSET from this callback is a good way to end
327 if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
330 // See if during the callback anyone retained a copy of the compile
331 // unit other than ourselves and if so, let whomever did own the object
332 // and create a new one for our own use!
334 cu.reset(new DWARFCompileUnit(dwarf2Data));
337 // Make sure we start on a proper
338 offset = next_cu_offset;
343 typedef struct DumpInfo
345 DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
348 recurse_depth(depth),
349 found_depth(UINT32_MAX),
355 const uint32_t die_offset;
356 const uint32_t recurse_depth;
357 uint32_t found_depth;
359 std::vector<DWARFDebugInfoEntry> ancestors;
361 DISALLOW_COPY_AND_ASSIGN(DumpInfo);
364 //----------------------------------------------------------------------
367 // A callback function for the static DWARFDebugInfo::Parse() function
368 // that gets called each time a compile unit header or debug information
369 // entry is successfully parsed.
371 // This function dump DWARF information and obey recurse depth and
372 // whether a single DIE is to be dumped (or all of the data).
373 //----------------------------------------------------------------------
374 static dw_offset_t DumpCallback
376 SymbolFileDWARF* dwarf2Data,
377 DWARFCompileUnit* cu,
378 DWARFDebugInfoEntry* die,
379 const dw_offset_t next_offset,
380 const uint32_t curr_depth,
384 DumpInfo* dumpInfo = (DumpInfo*)userData;
385 Stream *s = dumpInfo->strm;
386 bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
390 // Are we dumping everything?
391 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
393 // Yes we are dumping everything. Obey our recurse level though
394 if (curr_depth < dumpInfo->recurse_depth)
395 die->Dump(dwarf2Data, cu, *s, 0);
399 // We are dumping a specific DIE entry by offset
400 if (dumpInfo->die_offset == die->GetOffset())
402 // We found the DIE we were looking for, dump it!
405 s->SetIndentLevel(0);
406 const uint32_t num_ancestors = dumpInfo->ancestors.size();
407 if (num_ancestors > 0)
409 for (uint32_t i=0; i<num_ancestors-1; ++i)
411 dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
417 dumpInfo->found_depth = curr_depth;
419 die->Dump(dwarf2Data, cu, *s, 0);
421 // Note that we found the DIE we were looking for
422 dumpInfo->found_die = true;
424 // Since we are dumping a single DIE, if there are no children we are done!
425 if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
426 return DW_INVALID_OFFSET; // Return an invalid address to end parsing
428 else if (dumpInfo->found_die)
430 // Are we done with all the children?
431 if (curr_depth <= dumpInfo->found_depth)
432 return DW_INVALID_OFFSET;
434 // We have already found our DIE and are printing it's children. Obey
435 // our recurse depth and return an invalid offset if we get done
436 // dumping all of the children
437 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
438 die->Dump(dwarf2Data, cu, *s, 0);
440 else if (dumpInfo->die_offset > die->GetOffset())
443 dumpInfo->ancestors.back() = *die;
447 // Keep up with our indent level
451 dumpInfo->ancestors.pop_back();
454 return cu->GetNextCompileUnitOffset();
458 else if (die->HasChildren())
462 DWARFDebugInfoEntry null_die;
463 dumpInfo->ancestors.push_back(null_die);
471 s->PutCString("NULL - cu");
472 // We have a compile unit, reset our indent level to zero just in case
473 s->SetIndentLevel(0);
475 // See if we are dumping everything?
476 if (dumpInfo->die_offset == DW_INVALID_OFFSET)
478 // We are dumping everything
482 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
486 return DW_INVALID_OFFSET;
493 dumpInfo->ancestors.clear();
494 dumpInfo->ancestors.resize(1);
497 // We are dumping only a single DIE possibly with it's children and
498 // we must find it's compile unit before we can dump it properly
499 if (cu && dumpInfo->die_offset < cu->GetFirstDIEOffset())
501 // Not found, maybe the DIE offset provided wasn't correct?
502 // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl;
503 return DW_INVALID_OFFSET;
507 // See if the DIE is in this compile unit?
508 if (cu && dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
510 // This DIE is in this compile unit!
512 cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
515 // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
516 // return dumpInfo->die_offset;
520 // Skip to the next compile unit as the DIE isn't in the current one!
523 return cu->GetNextCompileUnitOffset();
527 return DW_INVALID_OFFSET;
534 // Just return the current offset to parse the next CU or DIE entry
538 //----------------------------------------------------------------------
541 // Dump the information in the .debug_info section to the specified
542 // ostream. If die_offset is valid, a single DIE will be dumped. If the
543 // die_offset is invalid, all the DWARF information will be dumped. Both
544 // cases will obey a "recurse_depth" or how deep to traverse into the
545 // children of each DIE entry. A recurse_depth of zero will dump all
546 // compile unit headers. A recurse_depth of 1 will dump all compile unit
547 // headers and the DW_TAG_compile unit tags. A depth of 2 will also
548 // dump all types and functions.
549 //----------------------------------------------------------------------
554 SymbolFileDWARF* dwarf2Data,
555 const uint32_t die_offset,
556 const uint32_t recurse_depth
559 DumpInfo dumpInfo(s, die_offset, recurse_depth);
560 s->PutCString(".debug_info contents");
561 if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
563 if (die_offset == DW_INVALID_OFFSET)
564 s->PutCString(":\n");
567 s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
568 if (recurse_depth != UINT32_MAX)
569 s->Printf(" recursing %u levels deep.", recurse_depth);
575 s->PutCString(": < EMPTY >\n");
578 DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
582 //----------------------------------------------------------------------
585 // Dump the contents of this DWARFDebugInfo object as has been parsed
586 // and/or modified after it has been parsed.
587 //----------------------------------------------------------------------
589 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
591 DumpInfo dumpInfo(s, die_offset, recurse_depth);
593 s->PutCString("Dumping .debug_info section from internal representation\n");
595 CompileUnitColl::const_iterator pos;
596 uint32_t curr_depth = 0;
597 ParseCompileUnitHeadersIfNeeded();
598 for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos)
600 DWARFCompileUnit *cu = pos->get();
601 DumpCallback(m_dwarf2Data, cu, NULL, 0, curr_depth, &dumpInfo);
603 const DWARFDIE die = cu->DIE();
605 die.Dump(s, recurse_depth);
610 //----------------------------------------------------------------------
611 // FindCallbackString
613 // A callback function for the static DWARFDebugInfo::Parse() function
614 // that gets called each time a compile unit header or debug information
615 // entry is successfully parsed.
617 // This function will find the die_offset of any items whose DW_AT_name
618 // matches the given string
619 //----------------------------------------------------------------------
620 typedef struct FindCallbackStringInfoTag
624 RegularExpression* regex;
625 vector<dw_offset_t>& die_offsets;
626 } FindCallbackStringInfo;
628 static dw_offset_t FindCallbackString
630 SymbolFileDWARF* dwarf2Data,
631 DWARFCompileUnit* cu,
632 DWARFDebugInfoEntry* die,
633 const dw_offset_t next_offset,
634 const uint32_t curr_depth,
638 FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
642 const char* die_name = die->GetName(dwarf2Data, cu);
647 if (info->regex->Execute(die_name))
648 info->die_offsets.push_back(die->GetOffset());
652 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
653 info->die_offsets.push_back(die->GetOffset());
658 // Just return the current offset to parse the next CU or DIE entry
662 //----------------------------------------------------------------------
665 // Finds all DIE that have a specific DW_AT_name attribute by manually
666 // searching through the debug information (not using the
667 // .debug_pubnames section). The string must match the entire name
668 // and case sensitive searches are an option.
669 //----------------------------------------------------------------------
671 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
676 FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
677 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
679 return !die_offsets.empty();
682 //----------------------------------------------------------------------
685 // Finds all DIE that have a specific DW_AT_name attribute by manually
686 // searching through the debug information (not using the
687 // .debug_pubnames section). The string must match the supplied regular
689 //----------------------------------------------------------------------
691 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
694 FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
695 DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
696 return !die_offsets.empty();