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 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace std;
30 
31 //----------------------------------------------------------------------
32 // Constructor
33 //----------------------------------------------------------------------
34 DWARFDebugInfo::DWARFDebugInfo() :
35     m_dwarf2Data(NULL),
36     m_compile_units(),
37     m_cu_aranges_ap ()
38 {
39 }
40 
41 //----------------------------------------------------------------------
42 // SetDwarfData
43 //----------------------------------------------------------------------
44 void
45 DWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data)
46 {
47     m_dwarf2Data = dwarf2Data;
48     m_compile_units.clear();
49 }
50 
51 
52 DWARFDebugAranges &
53 DWARFDebugInfo::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         for (size_t idx = 0; idx < num_compile_units; ++idx)
83         {
84             DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx);
85 
86             dw_offset_t offset = cu->GetOffset();
87             if (cus_with_data.find(offset) == cus_with_data.end())
88             {
89                 if (log)
90                 {
91                     if (!printed)
92                         log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
93                                      m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
94                     printed = true;
95                 }
96                 cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get());
97             }
98         }
99 
100         const bool minimize = true;
101         m_cu_aranges_ap->Sort (minimize);
102     }
103     return *m_cu_aranges_ap.get();
104 }
105 
106 
107 //----------------------------------------------------------------------
108 // LookupAddress
109 //----------------------------------------------------------------------
110 bool
111 DWARFDebugInfo::LookupAddress
112 (
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
118 )
119 {
120 
121     if (hint_die_offset != DW_INVALID_OFFSET)
122         cu_sp = GetCompileUnit(hint_die_offset);
123     else
124     {
125         DWARFDebugAranges &cu_aranges = GetCompileUnitAranges ();
126         const dw_offset_t cu_offset = cu_aranges.FindAddress (address);
127         cu_sp = GetCompileUnit(cu_offset);
128     }
129 
130     if (cu_sp.get())
131     {
132         if (cu_sp->LookupAddress(address, function_die, block_die))
133             return true;
134         cu_sp.reset();
135     }
136     else
137     {
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);
141         if (die_ptr)
142         {
143             if (cu_sp.get())
144             {
145                 if (function_die || block_die)
146                     return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die);
147 
148                 // We only wanted the compile unit that contained this address
149                 return true;
150             }
151         }
152     }
153     return false;
154 }
155 
156 
157 void
158 DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded()
159 {
160     if (m_compile_units.empty())
161     {
162         if (m_dwarf2Data != NULL)
163         {
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))
167             {
168                 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data));
169                 // Out of memory?
170                 if (cu_sp.get() == NULL)
171                     break;
172 
173                 if (cu_sp->Extract(debug_info_data, &offset) == false)
174                     break;
175 
176                 m_compile_units.push_back(cu_sp);
177 
178                 offset = cu_sp->GetNextCompileUnitOffset();
179             }
180         }
181     }
182 }
183 
184 size_t
185 DWARFDebugInfo::GetNumCompileUnits()
186 {
187     ParseCompileUnitHeadersIfNeeded();
188     return m_compile_units.size();
189 }
190 
191 DWARFCompileUnit*
192 DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx)
193 {
194     DWARFCompileUnit* cu = NULL;
195     if (idx < GetNumCompileUnits())
196         cu = m_compile_units[idx].get();
197     return cu;
198 }
199 
200 bool
201 DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const
202 {
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;
207 
208     for (pos = m_compile_units.begin(); pos != end_pos; ++pos)
209     {
210         if (pos->get() == cu)
211             return true;
212     }
213     return false;
214 }
215 
216 static bool
217 OffsetLessThanCompileUnitOffset (dw_offset_t offset, const DWARFCompileUnitSP& cu_sp)
218 {
219     return offset < cu_sp->GetOffset();
220 }
221 
222 DWARFCompileUnitSP
223 DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr)
224 {
225     DWARFCompileUnitSP cu_sp;
226     uint32_t cu_idx = DW_INVALID_INDEX;
227     if (cu_offset != DW_INVALID_OFFSET)
228     {
229         ParseCompileUnitHeadersIfNeeded();
230 
231         // Watch out for single compile unit executable as they are pretty common
232         const size_t num_cus = m_compile_units.size();
233         if (num_cus == 1)
234         {
235             if (m_compile_units[0]->GetOffset() == cu_offset)
236             {
237                 cu_sp = m_compile_units[0];
238                 cu_idx = 0;
239             }
240         }
241         else if (num_cus)
242         {
243             CompileUnitColl::const_iterator end_pos = m_compile_units.end();
244             CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
245             CompileUnitColl::const_iterator pos = std::upper_bound(begin_pos, end_pos, cu_offset, OffsetLessThanCompileUnitOffset);
246             if (pos != begin_pos)
247             {
248                 --pos;
249                 if ((*pos)->GetOffset() == cu_offset)
250                 {
251                     cu_sp = *pos;
252                     cu_idx = std::distance(begin_pos, pos);
253                 }
254             }
255         }
256     }
257     if (idx_ptr)
258         *idx_ptr = cu_idx;
259     return cu_sp;
260 }
261 
262 DWARFCompileUnitSP
263 DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset)
264 {
265     DWARFCompileUnitSP cu_sp;
266     if (die_offset != DW_INVALID_OFFSET)
267     {
268         ParseCompileUnitHeadersIfNeeded();
269 
270         // Watch out for single compile unit executable as they are pretty common
271         const size_t num_cus = m_compile_units.size();
272         if (num_cus == 1)
273         {
274             if (m_compile_units[0]->ContainsDIEOffset(die_offset))
275                 cu_sp = m_compile_units[0];
276         }
277         else if (num_cus)
278         {
279             CompileUnitColl::const_iterator end_pos = m_compile_units.end();
280             CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
281             CompileUnitColl::const_iterator pos = std::upper_bound(begin_pos, end_pos, die_offset, OffsetLessThanCompileUnitOffset);
282             if (pos != begin_pos)
283             {
284                 --pos;
285                 if ((*pos)->ContainsDIEOffset(die_offset))
286                     cu_sp = *pos;
287             }
288         }
289     }
290     return cu_sp;
291 }
292 
293 //----------------------------------------------------------------------
294 // GetDIE()
295 //
296 // Get the DIE (Debug Information Entry) with the specified offset.
297 //----------------------------------------------------------------------
298 DWARFDebugInfoEntry*
299 DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
300 {
301     DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
302     if (cu_sp_ptr)
303         *cu_sp_ptr = cu_sp;
304     if (cu_sp.get())
305         return cu_sp->GetDIEPtr(die_offset);
306     return NULL;    // Not found in any compile units
307 }
308 
309 DWARFDebugInfoEntry*
310 DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle)
311 {
312     assert (cu_handle);
313     DWARFDebugInfoEntry* die = NULL;
314     if (*cu_handle)
315         die = (*cu_handle)->GetDIEPtr(die_offset);
316 
317     if (die == NULL)
318     {
319         DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset));
320         if (cu_sp.get())
321         {
322             *cu_handle = cu_sp.get();
323             die = cu_sp->GetDIEPtr(die_offset);
324         }
325     }
326     if (die == NULL)
327         *cu_handle = NULL;
328     return die;
329 }
330 
331 
332 const DWARFDebugInfoEntry*
333 DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr)
334 {
335     DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset));
336     if (cu_sp_ptr)
337         *cu_sp_ptr = cu_sp;
338     if (cu_sp.get())
339         return cu_sp->GetDIEPtrContainingOffset(die_offset);
340 
341     return NULL;    // Not found in any compile units
342 
343 }
344 
345 //----------------------------------------------------------------------
346 // AddCompileUnit
347 //----------------------------------------------------------------------
348 void
349 DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu)
350 {
351     m_compile_units.push_back(cu);
352 }
353 
354 /*
355 void
356 DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die)
357 {
358     m_die_array.push_back(die);
359 }
360 */
361 
362 
363 
364 
365 //----------------------------------------------------------------------
366 // Parse
367 //
368 // Parses the .debug_info section and uses the .debug_abbrev section
369 // and various other sections in the SymbolFileDWARF class and calls the
370 // supplied callback function each time a compile unit header, or debug
371 // information entry is successfully parsed. This function can be used
372 // for different tasks such as parsing the file contents into a
373 // structured data, dumping, verifying and much more.
374 //----------------------------------------------------------------------
375 void
376 DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData)
377 {
378     if (dwarf2Data)
379     {
380         lldb::offset_t offset = 0;
381         uint32_t depth = 0;
382         DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data));
383         if (cu.get() == NULL)
384             return;
385         DWARFDebugInfoEntry die;
386 
387         while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset))
388         {
389             const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
390 
391             depth = 0;
392             // Call the callback function with no DIE pointer for the compile unit
393             // and get the offset that we are to continue to parse from
394             offset = callback(dwarf2Data, cu, NULL, offset, depth, userData);
395 
396             // Make sure we are within our compile unit
397             if (offset < next_cu_offset)
398             {
399                 // We are in our compile unit, parse starting at the offset
400                 // we were told to parse
401                 bool done = false;
402                 while (!done && die.Extract(dwarf2Data, cu.get(), &offset))
403                 {
404                     // Call the callback function with DIE pointer that falls within the compile unit
405                     offset = callback(dwarf2Data, cu, &die, offset, depth, userData);
406 
407                     if (die.IsNULL())
408                     {
409                         if (depth)
410                             --depth;
411                         else
412                             done = true;    // We are done with this compile unit!
413                     }
414                     else if (die.HasChildren())
415                         ++depth;
416                 }
417             }
418 
419             // Make sure the offset returned is valid, and if not stop parsing.
420             // Returning DW_INVALID_OFFSET from this callback is a good way to end
421             // all parsing
422             if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
423                 break;
424 
425             // See if during the callback anyone retained a copy of the compile
426             // unit other than ourselves and if so, let whomever did own the object
427             // and create a new one for our own use!
428             if (!cu.unique())
429                 cu.reset(new DWARFCompileUnit(dwarf2Data));
430 
431 
432             // Make sure we start on a proper
433             offset = next_cu_offset;
434         }
435     }
436 }
437 
438 typedef struct DumpInfo
439 {
440     DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) :
441         strm(init_strm),
442         die_offset(off),
443         recurse_depth(depth),
444         found_depth(UINT32_MAX),
445         found_die(false),
446         ancestors()
447     {
448     }
449     Stream* strm;
450     const uint32_t die_offset;
451     const uint32_t recurse_depth;
452     uint32_t found_depth;
453     bool found_die;
454     std::vector<DWARFDebugInfoEntry> ancestors;
455 
456     DISALLOW_COPY_AND_ASSIGN(DumpInfo);
457 } DumpInfo;
458 
459 //----------------------------------------------------------------------
460 // DumpCallback
461 //
462 // A callback function for the static DWARFDebugInfo::Parse() function
463 // that gets called each time a compile unit header or debug information
464 // entry is successfully parsed.
465 //
466 // This function dump DWARF information and obey recurse depth and
467 // whether a single DIE is to be dumped (or all of the data).
468 //----------------------------------------------------------------------
469 static dw_offset_t DumpCallback
470 (
471     SymbolFileDWARF* dwarf2Data,
472     DWARFCompileUnitSP& cu_sp,
473     DWARFDebugInfoEntry* die,
474     const dw_offset_t next_offset,
475     const uint32_t curr_depth,
476     void* userData
477 )
478 {
479     DumpInfo* dumpInfo = (DumpInfo*)userData;
480 
481     const DWARFCompileUnit* cu = cu_sp.get();
482 
483     Stream *s = dumpInfo->strm;
484     bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
485 
486     if (die)
487     {
488         // Are we dumping everything?
489         if (dumpInfo->die_offset == DW_INVALID_OFFSET)
490         {
491             // Yes we are dumping everything. Obey our recurse level though
492             if (curr_depth < dumpInfo->recurse_depth)
493                 die->Dump(dwarf2Data, cu, *s, 0);
494         }
495         else
496         {
497             // We are dumping a specific DIE entry by offset
498             if (dumpInfo->die_offset == die->GetOffset())
499             {
500                 // We found the DIE we were looking for, dump it!
501                 if (show_parents)
502                 {
503                     s->SetIndentLevel(0);
504                     const uint32_t num_ancestors = dumpInfo->ancestors.size();
505                     if (num_ancestors > 0)
506                     {
507                         for (uint32_t i=0; i<num_ancestors-1; ++i)
508                         {
509                             dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
510                             s->IndentMore();
511                         }
512                     }
513                 }
514 
515                 dumpInfo->found_depth = curr_depth;
516 
517                 die->Dump(dwarf2Data, cu, *s, 0);
518 
519                 // Note that we found the DIE we were looking for
520                 dumpInfo->found_die = true;
521 
522                 // Since we are dumping a single DIE, if there are no children we are done!
523                 if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
524                     return DW_INVALID_OFFSET;   // Return an invalid address to end parsing
525             }
526             else if (dumpInfo->found_die)
527             {
528                 // Are we done with all the children?
529                 if (curr_depth <= dumpInfo->found_depth)
530                     return DW_INVALID_OFFSET;
531 
532                 // We have already found our DIE and are printing it's children. Obey
533                 // our recurse depth and return an invalid offset if we get done
534                 // dumping all of the children
535                 if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
536                     die->Dump(dwarf2Data, cu, *s, 0);
537             }
538             else if (dumpInfo->die_offset > die->GetOffset())
539             {
540                 if (show_parents)
541                     dumpInfo->ancestors.back() = *die;
542             }
543         }
544 
545         // Keep up with our indent level
546         if (die->IsNULL())
547         {
548             if (show_parents)
549                 dumpInfo->ancestors.pop_back();
550 
551             if (curr_depth <= 1)
552                 return cu->GetNextCompileUnitOffset();
553             else
554                 s->IndentLess();
555         }
556         else if (die->HasChildren())
557         {
558             if (show_parents)
559             {
560                 DWARFDebugInfoEntry null_die;
561                 dumpInfo->ancestors.push_back(null_die);
562             }
563             s->IndentMore();
564         }
565     }
566     else
567     {
568         if (cu == NULL)
569             s->PutCString("NULL - cu");
570         // We have a compile unit, reset our indent level to zero just in case
571         s->SetIndentLevel(0);
572 
573         // See if we are dumping everything?
574         if (dumpInfo->die_offset == DW_INVALID_OFFSET)
575         {
576             // We are dumping everything
577             if (cu)
578             {
579                 cu->Dump(s);
580                 return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit
581             }
582             else
583             {
584                 return DW_INVALID_OFFSET;
585             }
586         }
587         else
588         {
589             if (show_parents)
590             {
591                 dumpInfo->ancestors.clear();
592                 dumpInfo->ancestors.resize(1);
593             }
594 
595             // We are dumping only a single DIE possibly with it's children and
596             // we must find it's compile unit before we can dump it properly
597             if (cu && dumpInfo->die_offset < cu->GetFirstDIEOffset())
598             {
599                 // Not found, maybe the DIE offset provided wasn't correct?
600             //  *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl;
601                 return DW_INVALID_OFFSET;
602             }
603             else
604             {
605                 // See if the DIE is in this compile unit?
606                 if (cu && dumpInfo->die_offset < cu->GetNextCompileUnitOffset())
607                 {
608                     // This DIE is in this compile unit!
609                     if (s->GetVerbose())
610                         cu->Dump(s); // Dump the compile unit for the DIE in verbose mode
611 
612                     return next_offset;
613                 //  // We found our compile unit that contains our DIE, just skip to dumping the requested DIE...
614                 //  return dumpInfo->die_offset;
615                 }
616                 else
617                 {
618                     // Skip to the next compile unit as the DIE isn't in the current one!
619                     if (cu)
620                     {
621                         return cu->GetNextCompileUnitOffset();
622                     }
623                     else
624                     {
625                         return DW_INVALID_OFFSET;
626                     }
627                 }
628             }
629         }
630     }
631 
632     // Just return the current offset to parse the next CU or DIE entry
633     return next_offset;
634 }
635 
636 //----------------------------------------------------------------------
637 // Dump
638 //
639 // Dump the information in the .debug_info section to the specified
640 // ostream. If die_offset is valid, a single DIE will be dumped. If the
641 // die_offset is invalid, all the DWARF information will be dumped. Both
642 // cases will obey a "recurse_depth" or how deep to traverse into the
643 // children of each DIE entry. A recurse_depth of zero will dump all
644 // compile unit headers. A recurse_depth of 1 will dump all compile unit
645 // headers and the DW_TAG_compile unit tags. A depth of 2 will also
646 // dump all types and functions.
647 //----------------------------------------------------------------------
648 void
649 DWARFDebugInfo::Dump
650 (
651     Stream *s,
652     SymbolFileDWARF* dwarf2Data,
653     const uint32_t die_offset,
654     const uint32_t recurse_depth
655 )
656 {
657     DumpInfo dumpInfo(s, die_offset, recurse_depth);
658     s->PutCString(".debug_info contents");
659     if (dwarf2Data->get_debug_info_data().GetByteSize() > 0)
660     {
661         if (die_offset == DW_INVALID_OFFSET)
662             s->PutCString(":\n");
663         else
664         {
665             s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
666             if (recurse_depth != UINT32_MAX)
667                 s->Printf(" recursing %u levels deep.", recurse_depth);
668             s->EOL();
669         }
670     }
671     else
672     {
673         s->PutCString(": < EMPTY >\n");
674         return;
675     }
676     DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
677 }
678 
679 
680 //----------------------------------------------------------------------
681 // Dump
682 //
683 // Dump the contents of this DWARFDebugInfo object as has been parsed
684 // and/or modified after it has been parsed.
685 //----------------------------------------------------------------------
686 void
687 DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth)
688 {
689     DumpInfo dumpInfo(s, die_offset, recurse_depth);
690 
691     s->PutCString("Dumping .debug_info section from internal representation\n");
692 
693     CompileUnitColl::const_iterator pos;
694     uint32_t curr_depth = 0;
695     ParseCompileUnitHeadersIfNeeded();
696     for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos)
697     {
698         const DWARFCompileUnitSP& cu_sp = *pos;
699         DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo);
700 
701         const DWARFDebugInfoEntry* die = cu_sp->DIE();
702         if (die)
703             die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth);
704     }
705 }
706 
707 
708 //----------------------------------------------------------------------
709 // FindCallbackString
710 //
711 // A callback function for the static DWARFDebugInfo::Parse() function
712 // that gets called each time a compile unit header or debug information
713 // entry is successfully parsed.
714 //
715 // This function will find the die_offset of any items whose DW_AT_name
716 // matches the given string
717 //----------------------------------------------------------------------
718 typedef struct FindCallbackStringInfoTag
719 {
720     const char* name;
721     bool ignore_case;
722     RegularExpression* regex;
723     vector<dw_offset_t>& die_offsets;
724 } FindCallbackStringInfo;
725 
726 static dw_offset_t FindCallbackString
727 (
728     SymbolFileDWARF* dwarf2Data,
729     DWARFCompileUnitSP& cu_sp,
730     DWARFDebugInfoEntry* die,
731     const dw_offset_t next_offset,
732     const uint32_t curr_depth,
733     void* userData
734 )
735 {
736     FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData;
737     const DWARFCompileUnit* cu = cu_sp.get();
738 
739     if (die)
740     {
741         const char* die_name = die->GetName(dwarf2Data, cu);
742         if (die_name)
743         {
744             if (info->regex)
745             {
746                 if (info->regex->Execute(die_name))
747                     info->die_offsets.push_back(die->GetOffset());
748             }
749             else
750             {
751                 if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0)
752                     info->die_offsets.push_back(die->GetOffset());
753             }
754         }
755     }
756 
757     // Just return the current offset to parse the next CU or DIE entry
758     return next_offset;
759 }
760 
761 //----------------------------------------------------------------------
762 // Find
763 //
764 // Finds all DIE that have a specific DW_AT_name attribute by manually
765 // searching through the debug information (not using the
766 // .debug_pubnames section). The string must match the entire name
767 // and case sensitive searches are an option.
768 //----------------------------------------------------------------------
769 bool
770 DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const
771 {
772     die_offsets.clear();
773     if (name && name[0])
774     {
775         FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets };
776         DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
777     }
778     return !die_offsets.empty();
779 }
780 
781 //----------------------------------------------------------------------
782 // Find
783 //
784 // Finds all DIE that have a specific DW_AT_name attribute by manually
785 // searching through the debug information (not using the
786 // .debug_pubnames section). The string must match the supplied regular
787 // expression.
788 //----------------------------------------------------------------------
789 bool
790 DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const
791 {
792     die_offsets.clear();
793     FindCallbackStringInfo info = { NULL, false, &re, die_offsets };
794     DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info);
795     return !die_offsets.empty();
796 }
797