1//===-- DWARFDebugInfo.cpp --------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12#include <algorithm>
13#include <set>
14
15#include "lldb/Core/RegularExpression.h"
16#include "lldb/Core/Stream.h"
17#include "lldb/Symbol/ObjectFile.h"
18
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"
26
27using namespace lldb;
28using namespace lldb_private;
29using namespace std;
30
31//----------------------------------------------------------------------
32// Constructor
33//----------------------------------------------------------------------
34DWARFDebugInfo::DWARFDebugInfo() :
35    m_dwarf2Data(NULL),
36    m_compile_units(),
37    m_cu_aranges_ap ()
38{
39}
40
41//----------------------------------------------------------------------
42// SetDwarfData
43//----------------------------------------------------------------------
44void
45DWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data)
46{
47    m_dwarf2Data = dwarf2Data;
48    m_compile_units.clear();
49}
50
51
52DWARFDebugAranges &
53DWARFDebugInfo::GetCompileUnitAranges ()
54{
55    if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data)
56    {
57        Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
58
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)
62        {
63            if (log)
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);
67
68        }
69
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++)
73        {
74            dw_offset_t offset = m_cu_aranges_ap.get()->OffsetAtIndex(n);
75            if (offset != DW_INVALID_OFFSET)
76                cus_with_data.insert (offset);
77        }
78
79        // Manually build arange data for everything that wasn't in the .debug_aranges table.
80        bool printed = false;
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)
84        {
85            DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
86
87            dw_offset_t offset = cu->GetOffset();
88            if (cus_with_data.find(offset) == cus_with_data.end())
89            {
90                if (log)
91                {
92                    if (!printed)
93                        log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
94                                     m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
95                    printed = true;
96                }
97                cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get(), clear_dies_if_already_not_parsed);
98            }
99        }
100
101        const bool minimize = true;
102        m_cu_aranges_ap->Sort (minimize);
103    }
104    return *m_cu_aranges_ap.get();
105}
106
107
108//----------------------------------------------------------------------
109// LookupAddress
110//----------------------------------------------------------------------
111bool
112DWARFDebugInfo::LookupAddress
113(
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
119)
120{
121
122    if (hint_die_offset != DW_INVALID_OFFSET)
123        cu_sp = GetCompileUnit(hint_die_offset);
124    else
125    {
126        DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
127        const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
128        cu_sp = GetCompileUnit(cu_offset);
129    }
130
131    if (cu_sp.get())
132    {
133        if (cu_sp->LookupAddress(address, function_die, block_die))
134            return true;
135        cu_sp.reset();
136    }
137    else
138    {
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);
142        if (die_ptr)
143        {
144            if (cu_sp.get())
145            {
146                if (function_die || block_die)
147                    return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
148
149                // We only wanted the compile unit that contained this address
150                return true;
151            }
152        }
153    }
154    return false;
155}
156
157
158void
159DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
160{
161    if (m_compile_units.empty())
162    {
163        if (m_dwarf2Data != NULL)
164        {
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))
168            {
169                DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
170                // Out of memory?
171                if (cu_sp.get() == NULL)
172                    break;
173
174                if (cu_sp->Extract(debug_info_data, &offset) == false)
175                    break;
176
177                m_compile_units.push_back(cu_sp);
178
179                offset = cu_sp->GetNextCompileUnitOffset();
180            }
181        }
182    }
183}
184
185size_t
186DWARFDebugInfo::GetNumCompileUnits()
187{
188    ParseCompileUnitHeadersIfNeeded();
189    return m_compile_units.size();
190}
191
192DWARFCompileUnit*
193DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
194{
195    DWARFCompileUnit* cu = NULL;
196    if (idx < GetNumCompileUnits())
197        cu = m_compile_units[idx].get();
198    return cu;
199}
200
201bool
202DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
203{
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;
208
209    for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
210    {
211        if (pos->get() == cu)
212            return true;
213    }
214    return false;
215}
216
217
218static bool CompileUnitOffsetLessThan (const DWARFCompileUnitSP& a, const DWARFCompileUnitSP& b)
219{
220    return a->GetOffset() < b->GetOffset();
221}
222
223
224static int
225CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem)
226{
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)
230        return -1;
231    if (key_cu_offset > cu_offset)
232        return 1;
233    return 0;
234}
235
236DWARFCompileUnitSP
237DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
238{
239    DWARFCompileUnitSP cu_sp;
240    uint32_t cu_idx = DW_INVALID_INDEX;
241    if (cu_offset != DW_INVALID_OFFSET)
242    {
243        ParseCompileUnitHeadersIfNeeded();
244
245        DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset);
246        if (match)
247        {
248            cu_sp = *match;
249            cu_idx = match - &m_compile_units[0];
250        }
251    }
252    if (idx_ptr)
253        *idx_ptr = cu_idx;
254    return cu_sp;
255}
256
257DWARFCompileUnitSP
258DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
259{
260    DWARFCompileUnitSP cu_sp;
261    if (die_offset != DW_INVALID_OFFSET)
262    {
263        ParseCompileUnitHeadersIfNeeded();
264
265        CompileUnitColl::const_iterator end_pos = m_compile_units.end();
266        CompileUnitColl::const_iterator pos;
267
268        for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
269        {
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)
273            {
274                cu_sp = *pos;
275                break;
276            }
277        }
278    }
279    return cu_sp;
280}
281
282//----------------------------------------------------------------------
283// Compare function DWARFDebugAranges::Range structures
284//----------------------------------------------------------------------
285static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
286{
287    return die1.GetOffset() < die2.GetOffset();
288}
289
290
291//----------------------------------------------------------------------
292// GetDIE()
293//
294// Get the DIE (Debug Information Entry) with the specified offset.
295//----------------------------------------------------------------------
296DWARFDebugInfoEntry*
297DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
298{
299    DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
300    if (cu_sp_ptr)
301        *cu_sp_ptr = cu_sp;
302    if (cu_sp.get())
303        return cu_sp->GetDIEPtr(die_offset);
304    return NULL;    // Not found in any compile units
305}
306
307DWARFDebugInfoEntry*
308DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle)
309{
310    assert (cu_handle);
311    DWARFDebugInfoEntry* die = NULL;
312    if (*cu_handle)
313        die = (*cu_handle)->GetDIEPtr(die_offset);
314
315    if (die == NULL)
316    {
317        DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset));
318        if (cu_sp.get())
319        {
320            *cu_handle = cu_sp.get();
321            die = cu_sp->GetDIEPtr(die_offset);
322        }
323    }
324    if (die == NULL)
325        *cu_handle = NULL;
326    return die;
327}
328
329
330const DWARFDebugInfoEntry*
331DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
332{
333    DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
334    if (cu_sp_ptr)
335        *cu_sp_ptr = cu_sp;
336    if (cu_sp.get())
337        return cu_sp->GetDIEPtrContainingOffset(die_offset);
338
339    return NULL;    // Not found in any compile units
340
341}
342
343//----------------------------------------------------------------------
344// DWARFDebugInfo_ParseCallback
345//
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//----------------------------------------------------------------------
350
351static dw_offset_t
352DWARFDebugInfo_ParseCallback
353(
354    SymbolFileDWARF* dwarf2Data,
355    DWARFCompileUnitSP& cu_sp,
356    DWARFDebugInfoEntry* die,
357    const dw_offset_t next_offset,
358    const uint32_t curr_depth,
359    void* userData
360)
361{
362    DWARFDebugInfo* debug_info = (DWARFDebugInfo*)userData;
363    DWARFCompileUnit* cu = cu_sp.get();
364    if (die)
365    {
366        cu->AddDIE(*die);
367    }
368    else if (cu)
369    {
370        debug_info->AddCompileUnit(cu_sp);
371    }
372
373    // Just return the current offset to parse the next CU or DIE entry
374    return next_offset;
375}
376
377//----------------------------------------------------------------------
378// AddCompileUnit
379//----------------------------------------------------------------------
380void
381DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
382{
383    m_compile_units.push_back(cu);
384}
385
386/*
387void
388DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
389{
390    m_die_array.push_back(die);
391}
392*/
393
394
395
396
397//----------------------------------------------------------------------
398// 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