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