DWARFDebugInfo.cpp revision 263363
11556Srgrimes//===-- DWARFDebugInfo.cpp --------------------------------------*- C++ -*-===//
21556Srgrimes//
31556Srgrimes//                     The LLVM Compiler Infrastructure
41556Srgrimes//
51556Srgrimes// This file is distributed under the University of Illinois Open Source
61556Srgrimes// License. See LICENSE.TXT for details.
71556Srgrimes//
81556Srgrimes//===----------------------------------------------------------------------===//
91556Srgrimes
101556Srgrimes#include "SymbolFileDWARF.h"
111556Srgrimes
121556Srgrimes#include <algorithm>
131556Srgrimes#include <set>
141556Srgrimes
151556Srgrimes#include "lldb/Core/RegularExpression.h"
161556Srgrimes#include "lldb/Core/Stream.h"
171556Srgrimes#include "lldb/Symbol/ObjectFile.h"
181556Srgrimes
191556Srgrimes#include "DWARFDebugAranges.h"
201556Srgrimes#include "DWARFDebugInfo.h"
211556Srgrimes#include "DWARFCompileUnit.h"
221556Srgrimes#include "DWARFDebugAranges.h"
231556Srgrimes#include "DWARFDebugInfoEntry.h"
241556Srgrimes#include "DWARFFormValue.h"
251556Srgrimes#include "LogChannelDWARF.h"
261556Srgrimes
271556Srgrimesusing namespace lldb;
281556Srgrimesusing namespace lldb_private;
291556Srgrimesusing namespace std;
301556Srgrimes
311556Srgrimes//----------------------------------------------------------------------
321556Srgrimes// Constructor
331556Srgrimes//----------------------------------------------------------------------
341556SrgrimesDWARFDebugInfo::DWARFDebugInfo() :
3536049Scharnier    m_dwarf2Data(NULL),
3636049Scharnier    m_compile_units(),
3736049Scharnier    m_cu_aranges_ap ()
381556Srgrimes{
3999110Sobrien}
4099110Sobrien
411556Srgrimes//----------------------------------------------------------------------
421556Srgrimes// SetDwarfData
431556Srgrimes//----------------------------------------------------------------------
441556Srgrimesvoid
4574567SacheDWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data)
46104548Stjr{
471556Srgrimes    m_dwarf2Data = dwarf2Data;
481556Srgrimes    m_compile_units.clear();
491556Srgrimes}
501556Srgrimes
511556Srgrimes
521556SrgrimesDWARFDebugAranges &
531556SrgrimesDWARFDebugInfo::GetCompileUnitAranges ()
541556Srgrimes{
551556Srgrimes    if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data)
561556Srgrimes    {
571556Srgrimes        Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
581556Srgrimes
591556Srgrimes        m_cu_aranges_ap.reset (new DWARFDebugAranges());
601556Srgrimes        const DWARFDataExtractor &debug_aranges_data = m_dwarf2Data->get_debug_aranges_data();
611556Srgrimes        if (debug_aranges_data.GetByteSize() > 0)
621556Srgrimes        {
639987Swollman            if (log)
6474567Sache                log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" from .debug_aranges",
6574567Sache                             m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
6674567Sache            m_cu_aranges_ap->Extract (debug_aranges_data);
6774567Sache
681556Srgrimes        }
6974567Sache
7074567Sache        // Make a list of all CUs represented by the arange data in the file.
711556Srgrimes        std::set<dw_offset_t> cus_with_data;
721556Srgrimes        for (size_t n=0;n<m_cu_aranges_ap.get()->GetNumRanges();n++)
731556Srgrimes        {
741556Srgrimes            dw_offset_t offset = m_cu_aranges_ap.get()->OffsetAtIndex(n);
751556Srgrimes            if (offset != DW_INVALID_OFFSET)
761556Srgrimes                cus_with_data.insert (offset);
7790113Simp        }
781556Srgrimes
7990113Simp        // Manually build arange data for everything that wasn't in the .debug_aranges table.
801556Srgrimes        bool printed = false;
811556Srgrimes        const size_t num_compile_units = GetNumCompileUnits();
82114583Smarkm        const bool clear_dies_if_already_not_parsed = true;
831556Srgrimes        for (size_t idx = 0; idx < num_compile_units; ++idx)
841556Srgrimes        {
851556Srgrimes            DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
861556Srgrimes
871556Srgrimes            dw_offset_t offset = cu->GetOffset();
8876351Skris            if (cus_with_data.find(offset) == cus_with_data.end())
8976351Skris            {
901556Srgrimes                if (log)
911556Srgrimes                {
921556Srgrimes                    if (!printed)
9374567Sache                        log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
9474567Sache                                     m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
951556Srgrimes                    printed = true;
961556Srgrimes                }
971556Srgrimes                cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get(), clear_dies_if_already_not_parsed);
981556Srgrimes            }
991556Srgrimes        }
1001556Srgrimes
10173345Sru        const bool minimize = true;
10273345Sru        m_cu_aranges_ap->Sort (minimize);
10373345Sru    }
10473345Sru    return *m_cu_aranges_ap.get();
10574567Sache}
10673345Sru
10774567Sache
1081556Srgrimes//----------------------------------------------------------------------
1091556Srgrimes// LookupAddress
1101556Srgrimes//----------------------------------------------------------------------
1111556Srgrimesbool
1121556SrgrimesDWARFDebugInfo::LookupAddress
1131556Srgrimes(
114202193Sed    const dw_addr_t address,
115202193Sed    const dw_offset_t hint_die_offset,
1161556Srgrimes    DWARFCompileUnitSP& cu_sp,
1171556Srgrimes    DWARFDebugInfoEntry** function_die,
1181556Srgrimes    DWARFDebugInfoEntry** block_die
1191556Srgrimes)
1201556Srgrimes{
1211556Srgrimes
12276351Skris    if (hint_die_offset != DW_INVALID_OFFSET)
12320420Ssteve        cu_sp = GetCompileUnit(hint_die_offset);
1241556Srgrimes    else
12576351Skris    {
12620420Ssteve        DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
1271556Srgrimes        const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
1281556Srgrimes        cu_sp = GetCompileUnit(cu_offset);
1291556Srgrimes    }
13076351Skris
1311556Srgrimes    if (cu_sp.get())
132104548Stjr    {
1331556Srgrimes        if (cu_sp->LookupAddress(address, function_die, block_die))
1341556Srgrimes            return true;
1351556Srgrimes        cu_sp.reset();
1361556Srgrimes    }
1371556Srgrimes    else
1381556Srgrimes    {
13976351Skris        // The hint_die_offset may have been a pointer to the actual item that
1401556Srgrimes        // we are looking for
14176351Skris        DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp);
1421556Srgrimes        if (die_ptr)
14376351Skris        {
1441556Srgrimes            if (cu_sp.get())
14576351Skris            {
14676351Skris                if (function_die || block_die)
1471556Srgrimes                    return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
1481556Srgrimes
1491556Srgrimes                // We only wanted the compile unit that contained this address
1501556Srgrimes                return true;
1511556Srgrimes            }
1521556Srgrimes        }
1531556Srgrimes    }
1541556Srgrimes    return false;
1551556Srgrimes}
15690113Simp
1571556Srgrimes
1581556Srgrimesvoid
1591556SrgrimesDWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
160114583Smarkm{
1611556Srgrimes    if (m_compile_units.empty())
16274567Sache    {
16374567Sache        if (m_dwarf2Data != NULL)
16474567Sache        {
16576017Skris            lldb::offset_t offset = 0;
16674567Sache            const DWARFDataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data();
16773345Sru            while (debug_info_data.ValidOffset(offset))
16874567Sache            {
1691556Srgrimes                DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
1701556Srgrimes                // Out of memory?
1711556Srgrimes                if (cu_sp.get() == NULL)
1721556Srgrimes                    break;
1731556Srgrimes
1741556Srgrimes                if (cu_sp->Extract(debug_info_data, &offset) == false)
1751556Srgrimes                    break;
1761556Srgrimes
1771556Srgrimes                m_compile_units.push_back(cu_sp);
1781556Srgrimes
1791556Srgrimes                offset = cu_sp->GetNextCompileUnitOffset();
1801556Srgrimes            }
1811556Srgrimes        }
1821556Srgrimes    }
18376351Skris}
18476351Skris
1851556Srgrimessize_t
1861556SrgrimesDWARFDebugInfo::GetNumCompileUnits()
18776351Skris{
1881556Srgrimes    ParseCompileUnitHeadersIfNeeded();
1891556Srgrimes    return m_compile_units.size();
1901556Srgrimes}
191114583Smarkm
1921556SrgrimesDWARFCompileUnit*
19390113SimpDWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
19490113Simp{
1951556Srgrimes    DWARFCompileUnit* cu = NULL;
1961556Srgrimes    if (idx < GetNumCompileUnits())
1971556Srgrimes        cu = m_compile_units[idx].get();
1981556Srgrimes    return cu;
1991556Srgrimes}
20076351Skris
20176351Skrisbool
20276351SkrisDWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
20376351Skris{
2041556Srgrimes    // Not a verify efficient function, but it is handy for use in assertions
2051556Srgrimes    // to make sure that a compile unit comes from a debug information file.
2061556Srgrimes    CompileUnitColl::const_iterator end_pos = m_compile_units.end();
2071556Srgrimes    CompileUnitColl::const_iterator pos;
2081556Srgrimes
2091556Srgrimes    for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
2101556Srgrimes    {
2111556Srgrimes        if (pos->get() == cu)
2121556Srgrimes            return true;
2131556Srgrimes    }
2141556Srgrimes    return false;
2151556Srgrimes}
2161556Srgrimes
21790113Simp
2181556Srgrimesstatic bool CompileUnitOffsetLessThan (const DWARFCompileUnitSP& a, const DWARFCompileUnitSP& b)
21990113Simp{
2201556Srgrimes    return a->GetOffset() < b->GetOffset();
2211556Srgrimes}
2221556Srgrimes
2231556Srgrimes
2241556Srgrimesstatic int
2251556SrgrimesCompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem)
2261556Srgrimes{
2271556Srgrimes    const dw_offset_t key_cu_offset = *(dw_offset_t*) key;
2281556Srgrimes    const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset();
2291556Srgrimes    if (key_cu_offset < cu_offset)
2301556Srgrimes        return -1;
2311556Srgrimes    if (key_cu_offset > cu_offset)
2321556Srgrimes        return 1;
2331556Srgrimes    return 0;
2341556Srgrimes}
2351556Srgrimes
2361556SrgrimesDWARFCompileUnitSP
2371556SrgrimesDWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
2381556Srgrimes{
2391556Srgrimes    DWARFCompileUnitSP cu_sp;
2401556Srgrimes    uint32_t cu_idx = DW_INVALID_INDEX;
2411556Srgrimes    if (cu_offset != DW_INVALID_OFFSET)
2421556Srgrimes    {
2431556Srgrimes        ParseCompileUnitHeadersIfNeeded();
2441556Srgrimes
2451556Srgrimes        DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset);
2461556Srgrimes        if (match)
2471556Srgrimes        {
2481556Srgrimes            cu_sp = *match;
2491556Srgrimes            cu_idx = match - &m_compile_units[0];
2501556Srgrimes        }
2511556Srgrimes    }
2521556Srgrimes    if (idx_ptr)
2531556Srgrimes        *idx_ptr = cu_idx;
2541556Srgrimes    return cu_sp;
2551556Srgrimes}
2561556Srgrimes
2571556SrgrimesDWARFCompileUnitSP
2581556SrgrimesDWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
2591556Srgrimes{
26090113Simp    DWARFCompileUnitSP cu_sp;
2611556Srgrimes    if (die_offset != DW_INVALID_OFFSET)
26290113Simp    {
2631556Srgrimes        ParseCompileUnitHeadersIfNeeded();
2648855Srgrimes
2651556Srgrimes        CompileUnitColl::const_iterator end_pos = m_compile_units.end();
2661556Srgrimes        CompileUnitColl::const_iterator pos;
2671556Srgrimes
2681556Srgrimes        for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
2691556Srgrimes        {
2701556Srgrimes            dw_offset_t cu_start_offset = (*pos)->GetOffset();
2711556Srgrimes            dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset();
2721556Srgrimes            if (cu_start_offset <= die_offset && die_offset < cu_end_offset)
2731556Srgrimes            {
2741556Srgrimes                cu_sp = *pos;
2751556Srgrimes                break;
2761556Srgrimes            }
2771556Srgrimes        }
2781556Srgrimes    }
2798855Srgrimes    return cu_sp;
2801556Srgrimes}
2811556Srgrimes
2821556Srgrimes//----------------------------------------------------------------------
2831556Srgrimes// Compare function DWARFDebugAranges::Range structures
2841556Srgrimes//----------------------------------------------------------------------
2851556Srgrimesstatic bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
2861556Srgrimes{
2871556Srgrimes    return die1.GetOffset() < die2.GetOffset();
2881556Srgrimes}
2891556Srgrimes
2901556Srgrimes
2911556Srgrimes//----------------------------------------------------------------------
2921556Srgrimes// GetDIE()
2931556Srgrimes//
2941556Srgrimes// Get the DIE (Debug Information Entry) with the specified offset.
2951556Srgrimes//----------------------------------------------------------------------
2961556SrgrimesDWARFDebugInfoEntry*
2971556SrgrimesDWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
2981556Srgrimes{
2991556Srgrimes    DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
3001556Srgrimes    if (cu_sp_ptr)
3011556Srgrimes        *cu_sp_ptr = cu_sp;
3021556Srgrimes    if (cu_sp.get())
3031556Srgrimes        return cu_sp->GetDIEPtr(die_offset);
3041556Srgrimes    return NULL;    // Not found in any compile units
3051556Srgrimes}
3061556Srgrimes
3071556SrgrimesDWARFDebugInfoEntry*
3081556SrgrimesDWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle)
3091556Srgrimes{
3101556Srgrimes    assert (cu_handle);
3111556Srgrimes    DWARFDebugInfoEntry* die = NULL;
3121556Srgrimes    if (*cu_handle)
3131556Srgrimes        die = (*cu_handle)->GetDIEPtr(die_offset);
31490113Simp
3151556Srgrimes    if (die == NULL)
31690113Simp    {
3171556Srgrimes        DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset));
3181556Srgrimes        if (cu_sp.get())
3191556Srgrimes        {
3201556Srgrimes            *cu_handle = cu_sp.get();
3211556Srgrimes            die = cu_sp->GetDIEPtr(die_offset);
3221556Srgrimes        }
3231556Srgrimes    }
3241556Srgrimes    if (die == NULL)
3251556Srgrimes        *cu_handle = NULL;
3261556Srgrimes    return die;
3271556Srgrimes}
3281556Srgrimes
3291556Srgrimes
3301556Srgrimesconst DWARFDebugInfoEntry*
3311556SrgrimesDWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
3321556Srgrimes{
3331556Srgrimes    DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
3341556Srgrimes    if (cu_sp_ptr)
3351556Srgrimes        *cu_sp_ptr = cu_sp;
3361556Srgrimes    if (cu_sp.get())
3371556Srgrimes        return cu_sp->GetDIEPtrContainingOffset(die_offset);
3381556Srgrimes
3391556Srgrimes    return NULL;    // Not found in any compile units
3401556Srgrimes
3411556Srgrimes}
3421556Srgrimes
3431556Srgrimes//----------------------------------------------------------------------
3441556Srgrimes// DWARFDebugInfo_ParseCallback
3451556Srgrimes//
3461556Srgrimes// A callback function for the static DWARFDebugInfo::Parse() function
3471556Srgrimes// that gets parses all compile units and DIE's into an internate
3481556Srgrimes// representation for further modification.
3491556Srgrimes//----------------------------------------------------------------------
3501556Srgrimes
3511556Srgrimesstatic dw_offset_t
3521556SrgrimesDWARFDebugInfo_ParseCallback
3531556Srgrimes(
3541556Srgrimes    SymbolFileDWARF* dwarf2Data,
3551556Srgrimes    DWARFCompileUnitSP& cu_sp,
3561556Srgrimes    DWARFDebugInfoEntry* die,
35790113Simp    const dw_offset_t next_offset,
3581556Srgrimes    const uint32_t curr_depth,
35990113Simp    void* userData
3601556Srgrimes)
3618855Srgrimes{
3621556Srgrimes    DWARFDebugInfo* debug_info = (DWARFDebugInfo*)userData;
3631556Srgrimes    DWARFCompileUnit* cu = cu_sp.get();
3641556Srgrimes    if (die)
3651556Srgrimes    {
3661556Srgrimes        cu->AddDIE(*die);
3671556Srgrimes    }
3681556Srgrimes    else if (cu)
3691556Srgrimes    {
3701556Srgrimes        debug_info->AddCompileUnit(cu_sp);
3711556Srgrimes    }
3721556Srgrimes
3731556Srgrimes    // Just return the current offset to parse the next CU or DIE entry
3741556Srgrimes    return next_offset;
3751556Srgrimes}
3768855Srgrimes
3771556Srgrimes//----------------------------------------------------------------------
3781556Srgrimes// AddCompileUnit
3791556Srgrimes//----------------------------------------------------------------------
3801556Srgrimesvoid
3811556SrgrimesDWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
3821556Srgrimes{
3831556Srgrimes    m_compile_units.push_back(cu);
3841556Srgrimes}
3851556Srgrimes
3861556Srgrimes/*
3871556Srgrimesvoid
3881556SrgrimesDWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
3891556Srgrimes{
3901556Srgrimes    m_die_array.push_back(die);
3911556Srgrimes}
3921556Srgrimes*/
3931556Srgrimes
3941556Srgrimes
3951556Srgrimes
3961556Srgrimes
3971556Srgrimes//----------------------------------------------------------------------
3981556Srgrimes// Parse
399//
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//----------------------------------------------------------------------
407void
408DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
409{
410    if (dwarf2Data)
411    {
412        lldb::offset_t offset = 0;
413        uint32_t depth = 0;
414        DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
415        if (cu.get() == NULL)
416            return;
417        DWARFDebugInfoEntry die;
418
419        while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
420        {
421            const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
422
423            depth = 0;
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);
427
428            // Make sure we are within our compile unit
429            if (offset < next_cu_offset)
430            {
431                // We are in our compile unit, parse starting at the offset
432                // we were told to parse
433                bool done = false;
434                while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
435                {
436                    // Call the callback function with DIE pointer that falls within the compile unit
437                    offset = callback(dwarf2Data, cu, &die, offset, depth, userData);
438
439                    if (die.IsNULL())
440                    {
441                        if (depth)
442                            --depth;
443                        else
444                            done = true;    // We are done with this compile unit!
445                    }
446                    else if (die.HasChildren())
447                        ++depth;
448                }
449            }
450
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
453            // all parsing
454            if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
455                break;
456
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!
460            if (!cu.unique())
461                cu.reset(new DWARFCompileUnit(dwarf2Data));
462
463
464            // Make sure we start on a proper
465            offset = next_cu_offset;
466        }
467    }
468}
469
470typedef struct DumpInfo
471{
472    DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
473        strm(init_strm),
474        die_offset(off),
475        recurse_depth(depth),
476        found_depth(UINT32_MAX),
477        found_die(false),
478        ancestors()
479    {
480    }
481    Stream* strm;
482    const uint32_t die_offset;
483    const uint32_t recurse_depth;
484    uint32_t found_depth;
485    bool found_die;
486    std::vector<DWARFDebugInfoEntry> ancestors;
487
488    DISALLOW_COPY_AND_ASSIGN(DumpInfo);
489} DumpInfo;
490
491//----------------------------------------------------------------------
492// DumpCallback
493//
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.
497//
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//----------------------------------------------------------------------
501static dw_offset_t DumpCallback
502(
503    SymbolFileDWARF* dwarf2Data,
504    DWARFCompileUnitSP& cu_sp,
505    DWARFDebugInfoEntry* die,
506    const dw_offset_t next_offset,
507    const uint32_t curr_depth,
508    void* userData
509)
510{
511    DumpInfo* dumpInfo = (DumpInfo*)userData;
512
513    const DWARFCompileUnit* cu = cu_sp.get();
514
515    Stream *s = dumpInfo->strm;
516    bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
517
518    if (die)
519    {
520        // Are we dumping everything?
521        if (dumpInfo->die_offset == DW_INVALID_OFFSET)
522        {
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);
526        }
527        else
528        {
529            // We are dumping a specific DIE entry by offset
530            if (dumpInfo->die_offset == die->GetOffset())
531            {
532                // We found the DIE we were looking for, dump it!
533                if (show_parents)
534                {
535                    s->SetIndentLevel(0);
536                    const uint32_t num_ancestors = dumpInfo->ancestors.size();
537                    if (num_ancestors > 0)
538                    {
539                        for (uint32_t i=0; i<num_ancestors-1; ++i)
540                        {
541                            dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
542                            s->IndentMore();
543                        }
544                    }
545                }
546
547                dumpInfo->found_depth = curr_depth;
548
549                die->Dump(dwarf2Data, cu, *s, 0);
550
551                // Note that we found the DIE we were looking for
552                dumpInfo->found_die = true;
553
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
557            }
558            else if (dumpInfo->found_die)
559            {
560                // Are we done with all the children?
561                if (curr_depth <= dumpInfo->found_depth)
562                    return DW_INVALID_OFFSET;
563
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);
569            }
570            else if (dumpInfo->die_offset > die->GetOffset())
571            {
572                if (show_parents)
573                    dumpInfo->ancestors.back() = *die;
574            }
575        }
576
577        // Keep up with our indent level
578        if (die->IsNULL())
579        {
580            if (show_parents)
581                dumpInfo->ancestors.pop_back();
582
583            if (curr_depth <= 1)
584                return cu->GetNextCompileUnitOffset();
585            else
586                s->IndentLess();
587        }
588        else if (die->HasChildren())
589        {
590            if (show_parents)
591            {
592                DWARFDebugInfoEntry null_die;
593                dumpInfo->ancestors.push_back(null_die);
594            }
595            s->IndentMore();
596        }
597    }
598    else
599    {
600        if (cu == NULL)
601            s->PutCString("NULL - cu");
602        // We have a compile unit, reset our indent level to zero just in case
603        s->SetIndentLevel(0);
604
605        // See if we are dumping everything?
606        if (dumpInfo->die_offset == DW_INVALID_OFFSET)
607        {
608            // We are dumping everything
609            cu->Dump(s);
610            return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
611        }
612        else
613        {
614            if (show_parents)
615            {
616                dumpInfo->ancestors.clear();
617                dumpInfo->ancestors.resize(1);
618            }
619
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())
623            {
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;
627            }
628            else
629            {
630                // See if the DIE is in this compile unit?
631                if (dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
632                {
633                    // This DIE is in this compile unit!
634                    if (s->GetVerbose())
635                        cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
636
637                    return next_offset;
638                //  // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
639                //  return dumpInfo->die_offset;
640                }
641                else
642                {
643                    // Skip to the next compile unit as the DIE isn't in the current one!
644                    return cu->GetNextCompileUnitOffset();
645                }
646            }
647        }
648    }
649
650    // Just return the current offset to parse the next CU or DIE entry
651    return next_offset;
652}
653
654//----------------------------------------------------------------------
655// Dump
656//
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//----------------------------------------------------------------------
666void
667DWARFDebugInfo::Dump
668(
669    Stream *s,
670    SymbolFileDWARF* dwarf2Data,
671    const uint32_t die_offset,
672    const uint32_t recurse_depth
673)
674{
675    DumpInfo dumpInfo(s, die_offset, recurse_depth);
676    s->PutCString(".debug_info contents");
677    if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
678    {
679        if (die_offset == DW_INVALID_OFFSET)
680            s->PutCString(":\n");
681        else
682        {
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);
686            s->EOL();
687        }
688    }
689    else
690    {
691        s->PutCString(": < EMPTY >\n");
692        return;
693    }
694    DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
695}
696
697
698//----------------------------------------------------------------------
699// Dump
700//
701// Dump the contents of this DWARFDebugInfo object as has been parsed
702// and/or modified after it has been parsed.
703//----------------------------------------------------------------------
704void
705DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
706{
707    DumpInfo dumpInfo(s, die_offset, recurse_depth);
708
709    s->PutCString("Dumping .debug_info section from internal representation\n");
710
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)
715    {
716        const DWARFCompileUnitSP& cu_sp = *pos;
717        DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo);
718
719        const DWARFDebugInfoEntry* die = cu_sp->DIE();
720        if (die)
721            die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth);
722    }
723}
724
725
726//----------------------------------------------------------------------
727// FindCallbackString
728//
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.
732//
733// This function will find the die_offset of any items whose DW_AT_name
734// matches the given string
735//----------------------------------------------------------------------
736typedef struct FindCallbackStringInfoTag
737{
738    const char* name;
739    bool ignore_case;
740    RegularExpression* regex;
741    vector<dw_offset_t>& die_offsets;
742} FindCallbackStringInfo;
743
744static dw_offset_t FindCallbackString
745(
746    SymbolFileDWARF* dwarf2Data,
747    DWARFCompileUnitSP& cu_sp,
748    DWARFDebugInfoEntry* die,
749    const dw_offset_t next_offset,
750    const uint32_t curr_depth,
751    void* userData
752)
753{
754    FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
755    const DWARFCompileUnit* cu = cu_sp.get();
756
757    if (die)
758    {
759        const char* die_name = die->GetName(dwarf2Data, cu);
760        if (die_name)
761        {
762            if (info->regex)
763            {
764                if (info->regex->Execute(die_name))
765                    info->die_offsets.push_back(die->GetOffset());
766            }
767            else
768            {
769                if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
770                    info->die_offsets.push_back(die->GetOffset());
771            }
772        }
773    }
774
775    // Just return the current offset to parse the next CU or DIE entry
776    return next_offset;
777}
778
779//----------------------------------------------------------------------
780// Find
781//
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//----------------------------------------------------------------------
787bool
788DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
789{
790    die_offsets.clear();
791    if (name && name[0])
792    {
793        FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
794        DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
795    }
796    return !die_offsets.empty();
797}
798
799//----------------------------------------------------------------------
800// Find
801//
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
805// expression.
806//----------------------------------------------------------------------
807bool
808DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
809{
810    die_offsets.clear();
811    FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
812    DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
813    return !die_offsets.empty();
814}
815