blob: 8a21e1ec55514c494c57b728812bb77c087b2c0a [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DWARFCompileUnit.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 "DWARFCompileUnit.h"
11
Greg Claytone33bdd32010-09-03 23:26:12 +000012#include "lldb/Core/Mangled.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Stream.h"
14#include "lldb/Core/Timer.h"
15
16#include "DWARFDebugAbbrev.h"
17#include "DWARFDebugAranges.h"
Greg Clayton12bec712010-06-28 21:30:43 +000018#include "DWARFDebugInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "DWARFDIECollection.h"
20#include "DWARFFormValue.h"
21#include "LogChannelDWARF.h"
22#include "SymbolFileDWARF.h"
23
24using namespace lldb_private;
25using namespace std;
26
27extern int g_verbose;
28
Greg Claytonbef15832010-07-14 00:18:15 +000029DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) :
30 m_dwarf2Data ( dwarf2Data ),
Chris Lattner24943d22010-06-08 16:52:24 +000031 m_offset ( DW_INVALID_OFFSET ),
32 m_length ( 0 ),
33 m_version ( 0 ),
34 m_abbrevs ( NULL ),
35 m_addr_size ( DWARFCompileUnit::GetDefaultAddressSize() ),
36 m_base_addr ( 0 ),
37 m_die_array (),
38 m_aranges_ap (),
39 m_user_data ( NULL )
40{
41}
42
43void
44DWARFCompileUnit::Clear()
45{
46 m_offset = DW_INVALID_OFFSET;
47 m_length = 0;
48 m_version = 0;
49 m_abbrevs = NULL;
50 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize();
51 m_base_addr = 0;
52 m_die_array.clear();
53 m_aranges_ap.reset();
54 m_user_data = NULL;
55}
56
57bool
58DWARFCompileUnit::Extract(const DataExtractor &debug_info, uint32_t* offset_ptr)
59{
60 Clear();
61
62 m_offset = *offset_ptr;
63
64 if (debug_info.ValidOffset(*offset_ptr))
65 {
66 dw_offset_t abbr_offset;
67 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
68 m_length = debug_info.GetU32(offset_ptr);
69 m_version = debug_info.GetU16(offset_ptr);
70 abbr_offset = debug_info.GetU32(offset_ptr);
71 m_addr_size = debug_info.GetU8 (offset_ptr);
72
73 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
74 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
75 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
76 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
77
78 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
79 {
80 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
81 return true;
82 }
83
84 // reset the offset to where we tried to parse from if anything went wrong
85 *offset_ptr = m_offset;
86 }
87
88 return false;
89}
90
91
92dw_offset_t
93DWARFCompileUnit::Extract(dw_offset_t offset, const DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs)
94{
95 Clear();
96
97 m_offset = offset;
98
99 if (debug_info_data.ValidOffset(offset))
100 {
101 m_length = debug_info_data.GetU32(&offset);
102 m_version = debug_info_data.GetU16(&offset);
103 bool abbrevs_OK = debug_info_data.GetU32(&offset) == abbrevs->GetOffset();
104 m_abbrevs = abbrevs;
105 m_addr_size = debug_info_data.GetU8 (&offset);
106
107 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
108 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
109
110 if (version_OK && addr_size_OK && abbrevs_OK && debug_info_data.ValidOffset(offset))
111 return offset;
112 }
113 return DW_INVALID_OFFSET;
114}
115
116void
117DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
118{
119 if (m_die_array.size() > 1)
120 {
121 // std::vectors never get any smaller when resized to a smaller size,
122 // or when clear() or erase() are called, the size will report that it
123 // is smaller, but the memory allocated remains intact (call capacity()
124 // to see this). So we need to create a temporary vector and swap the
125 // contents which will cause just the internal pointers to be swapped
126 // so that when "tmp_array" goes out of scope, it will destroy the
127 // contents.
128
129 // Save at least the compile unit DIE
130 DWARFDebugInfoEntry::collection tmp_array;
131 m_die_array.swap(tmp_array);
132 if (keep_compile_unit_die)
133 m_die_array.push_back(tmp_array.front());
134 }
135}
136
137//----------------------------------------------------------------------
138// ParseCompileUnitDIEsIfNeeded
139//
140// Parses a compile unit and indexes its DIEs if it already hasn't been
141// done.
142//----------------------------------------------------------------------
143size_t
144DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
145{
146 const size_t initial_die_array_size = m_die_array.size();
147 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
148 return 0; // Already parsed
149
150 Timer scoped_timer (__PRETTY_FUNCTION__,
151 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
152 m_offset,
153 cu_die_only);
154
155 // Set the offset to that of the first DIE
156 uint32_t offset = GetFirstDIEOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000157 DWARFDebugInfoEntry die;
158 // Keep a flat array of the DIE for binary lookup by DIE offset
159 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
160// if (log)
161// log->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x",
162// cu->GetOffset(),
163// cu->GetLength(),
164// cu->GetVersion(),
165// cu->GetAbbrevOffset(),
166// cu->GetAddressByteSize());
167
168 uint32_t depth = 0;
169 // We are in our compile unit, parse starting at the offset
170 // we were told to parse
171 while (die.Extract(m_dwarf2Data, this, &offset))
172 {
173 if (log)
174 log->Printf("0x%8.8x: %*.*s%s%s",
175 die.GetOffset(),
176 depth * 2, depth * 2, "",
177 DW_TAG_value_to_name (die.Tag()),
178 die.HasChildren() ? " *" : "");
179 if (cu_die_only)
180 {
181 AddDIE(die);
182 return 1;
183 }
184 else if (depth == 0 && initial_die_array_size == 1)
185 {
186 // Don't append the CU die as we already did that
187 }
188 else
189 {
190 AddDIE(die);
191 }
192
193 const DWARFAbbreviationDeclaration* abbrDecl = die.GetAbbreviationDeclarationPtr();
194 if (abbrDecl)
195 {
196 // Normal DIE
197 if (abbrDecl->HasChildren())
198 ++depth;
199 }
200 else
201 {
202 // NULL DIE.
203 if (depth > 0)
204 --depth;
205 else
206 break; // We are done with this compile unit!
207 }
208
Greg Clayton54e7afa2010-07-09 20:39:50 +0000209 assert(offset <= GetNextCompileUnitOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
211 SetDIERelations();
212 return m_die_array.size();
213}
214
215
216dw_offset_t
217DWARFCompileUnit::GetAbbrevOffset() const
218{
219 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
220}
221
222
223
224bool
225DWARFCompileUnit::Verify(Stream *s) const
226{
227 const DataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
228 bool valid_offset = debug_info.ValidOffset(m_offset);
229 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
230 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
231 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
232 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
233 bool verbose = s->GetVerbose();
234 if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
235 {
236 if (verbose)
237 s->Printf(" 0x%8.8x: OK\n", m_offset);
238 return true;
239 }
240 else
241 {
242 s->Printf(" 0x%8.8x: ", m_offset);
243
244 m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
245 s->EOL();
246 if (valid_offset)
247 {
248 if (!length_OK)
249 s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
250 if (!version_OK)
251 s->Printf(" The 16 bit compile unit header version is not supported.\n");
252 if (!abbr_offset_OK)
253 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
254 if (!addr_size_OK)
255 s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size);
256 }
257 else
258 s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n");
259 }
260 return false;
261}
262
263
264void
265DWARFCompileUnit::Dump(Stream *s) const
266{
267 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n",
268 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
269}
270
271
272static uint8_t g_default_addr_size = 4;
273
274uint8_t
275DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
276{
277 if (cu)
278 return cu->GetAddressByteSize();
279 return DWARFCompileUnit::GetDefaultAddressSize();
280}
281
282uint8_t
283DWARFCompileUnit::GetDefaultAddressSize()
284{
285 return g_default_addr_size;
286}
287
288void
289DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
290{
291 g_default_addr_size = addr_size;
292}
293
294bool
295DWARFCompileUnit::LookupAddress
296(
297 const dw_addr_t address,
298 DWARFDebugInfoEntry** function_die_handle,
299 DWARFDebugInfoEntry** block_die_handle
300)
301{
302 bool success = false;
303
304 if (function_die_handle != NULL && DIE())
305 {
306 if (m_aranges_ap.get() == NULL)
307 {
308 m_aranges_ap.reset(new DWARFDebugAranges());
309 m_die_array.front().BuildFunctionAddressRangeTable(m_dwarf2Data, this, m_aranges_ap.get());
310 }
311
312 // Re-check the aranges auto pointer contents in case it was created above
313 if (m_aranges_ap.get() != NULL)
314 {
315 *function_die_handle = GetDIEPtr(m_aranges_ap->FindAddress(address));
316 if (*function_die_handle != NULL)
317 {
318 success = true;
319 if (block_die_handle != NULL)
320 {
321 DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild();
322 while (child)
323 {
324 if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle))
325 break;
326 child = child->GetSibling();
327 }
328 }
329 }
330 }
331 }
332 return success;
333}
334
335//----------------------------------------------------------------------
336// SetDIERelations()
337//
338// We read in all of the DIE entries into our flat list of DIE entries
339// and now we need to go back through all of them and set the parent,
340// sibling and child pointers for quick DIE navigation.
341//----------------------------------------------------------------------
342void
343DWARFCompileUnit::SetDIERelations()
344{
345#if 0
346 // Compute average bytes per DIE
347 //
348 // We can figure out what the average number of bytes per DIE is
349 // to help us pre-allocate the correct number of m_die_array
350 // entries so we don't end up doing a lot of memory copies as we
351 // are creating our DIE array when parsing
352 //
353 // Enable this code by changing "#if 0" above to "#if 1" and running
354 // the dsymutil or dwarfdump with a bunch of dwarf files and see what
355 // the running average ends up being in the stdout log.
356 static size_t g_total_cu_debug_info_size = 0;
357 static size_t g_total_num_dies = 0;
358 static size_t g_min_bytes_per_die = UINT_MAX;
359 static size_t g_max_bytes_per_die = 0;
360 const size_t num_dies = m_die_array.size();
361 const size_t cu_debug_info_size = GetDebugInfoSize();
362 const size_t bytes_per_die = cu_debug_info_size / num_dies;
363 if (g_min_bytes_per_die > bytes_per_die)
364 g_min_bytes_per_die = bytes_per_die;
365 if (g_max_bytes_per_die < bytes_per_die)
366 g_max_bytes_per_die = bytes_per_die;
367 if (g_total_cu_debug_info_size == 0)
368 {
369 cout << " min max avg" << endl
370 << "n dies cu size bpd bpd bpd bpd" << endl
371 << "------ -------- --- === === ===" << endl;
372 }
373 g_total_cu_debug_info_size += cu_debug_info_size;
374 g_total_num_dies += num_dies;
375 const size_t avg_bytes_per_die = g_total_cu_debug_info_size / g_total_num_dies;
376 cout
377 << DECIMAL_WIDTH(6) << num_dies << ' '
378 << DECIMAL_WIDTH(8) << cu_debug_info_size << ' '
379 << DECIMAL_WIDTH(3) << bytes_per_die << ' '
380 << DECIMAL_WIDTH(3) << g_min_bytes_per_die << ' '
381 << DECIMAL_WIDTH(3) << g_max_bytes_per_die << ' '
382 << DECIMAL_WIDTH(3) << avg_bytes_per_die
383 << endl;
384#endif
385 if (m_die_array.empty())
386 return;
387 DWARFDebugInfoEntry* die_array_begin = &m_die_array.front();
388 DWARFDebugInfoEntry* die_array_end = &m_die_array.back();
389 DWARFDebugInfoEntry* curr_die;
390 // We purposely are skipping the last element in the array in the loop below
391 // so that we can always have a valid next item
392 for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die)
393 {
394 // Since our loop doesn't include the last element, we can always
395 // safely access the next die in the array.
396 DWARFDebugInfoEntry* next_die = curr_die + 1;
397
398 const DWARFAbbreviationDeclaration* curr_die_abbrev = curr_die->GetAbbreviationDeclarationPtr();
399
400 if (curr_die_abbrev)
401 {
402 // Normal DIE
403 if (curr_die_abbrev->HasChildren())
404 next_die->SetParent(curr_die);
405 else
406 curr_die->SetSibling(next_die);
407 }
408 else
409 {
410 // NULL DIE that terminates a sibling chain
411 DWARFDebugInfoEntry* parent = curr_die->GetParent();
412 if (parent)
413 parent->SetSibling(next_die);
414 }
415 }
416
417 // Since we skipped the last element, we need to fix it up!
418 if (die_array_begin < die_array_end)
419 curr_die->SetParent(die_array_begin);
420
421#if 0
422 // The code below will dump the DIE relations in case any modification
423 // is done to the above code. This dump can be used in a diff to make
424 // sure that no functionality is lost.
425 {
426 DWARFDebugInfoEntry::const_iterator pos;
427 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
428 puts("offset parent sibling child");
429 puts("-------- -------- -------- --------");
430 for (pos = m_die_array.begin(); pos != end; ++pos)
431 {
432 const DWARFDebugInfoEntry& die_ref = *pos;
433 const DWARFDebugInfoEntry* p = die_ref.GetParent();
434 const DWARFDebugInfoEntry* s = die_ref.GetSibling();
435 const DWARFDebugInfoEntry* c = die_ref.GetFirstChild();
436 printf("%.8x: %.8x %.8x %.8x\n", die_ref.GetOffset(),
437 p ? p->GetOffset() : 0,
438 s ? s->GetOffset() : 0,
439 c ? c->GetOffset() : 0);
440 }
441 }
442#endif
443
444}
445//----------------------------------------------------------------------
446// Compare function DWARFDebugAranges::Range structures
447//----------------------------------------------------------------------
448static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
449{
450 return die1.GetOffset() < die2.GetOffset();
451}
452
453//----------------------------------------------------------------------
454// GetDIEPtr()
455//
456// Get the DIE (Debug Information Entry) with the specified offset.
457//----------------------------------------------------------------------
458DWARFDebugInfoEntry*
459DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset)
460{
461 if (die_offset != DW_INVALID_OFFSET)
462 {
463 ExtractDIEsIfNeeded (false);
464 DWARFDebugInfoEntry compare_die;
465 compare_die.SetOffset(die_offset);
466 DWARFDebugInfoEntry::iterator end = m_die_array.end();
467 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
468 if (pos != end)
469 {
470 if (die_offset == (*pos).GetOffset())
471 return &(*pos);
472 }
473 }
474 return NULL; // Not found in any compile units
475}
476
477//----------------------------------------------------------------------
478// GetDIEPtrContainingOffset()
479//
480// Get the DIE (Debug Information Entry) that contains the specified
481// .debug_info offset.
482//----------------------------------------------------------------------
483const DWARFDebugInfoEntry*
484DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset)
485{
486 if (die_offset != DW_INVALID_OFFSET)
487 {
488 ExtractDIEsIfNeeded (false);
489 DWARFDebugInfoEntry compare_die;
490 compare_die.SetOffset(die_offset);
491 DWARFDebugInfoEntry::iterator end = m_die_array.end();
492 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
493 if (pos != end)
494 {
495 if (die_offset >= (*pos).GetOffset())
496 {
497 DWARFDebugInfoEntry::iterator next = pos + 1;
498 if (next != end)
499 {
500 if (die_offset < (*next).GetOffset())
501 return &(*pos);
502 }
503 }
504 }
505 }
506 return NULL; // Not found in any compile units
507}
508
509
510
511size_t
512DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
513{
514 size_t old_size = dies.Size();
515 DWARFDebugInfoEntry::const_iterator pos;
516 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
517 for (pos = m_die_array.begin(); pos != end; ++pos)
518 {
519 if (pos->Tag() == tag)
520 dies.Insert(&(*pos));
521 }
522
523 // Return the number of DIEs added to the collection
524 return dies.Size() - old_size;
525}
526
527void
528DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
529{
530 m_global_die_indexes.push_back (die_idx);
531}
532
533
534void
535DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
536{
537 // Indexes to all file level global and static variables
538 m_global_die_indexes;
539
540 if (m_die_array.empty())
541 return;
542
543 const DWARFDebugInfoEntry* first_die = &m_die_array[0];
544 const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
545 if (first_die <= die && die < end)
546 m_global_die_indexes.push_back (die - first_die);
547}
548
549
550void
551DWARFCompileUnit::Index
552(
Greg Clayton12bec712010-06-28 21:30:43 +0000553 lldb_private::UniqueCStringMap<dw_offset_t>& base_name_to_function_die,
554 lldb_private::UniqueCStringMap<dw_offset_t>& full_name_to_function_die,
555 lldb_private::UniqueCStringMap<dw_offset_t>& method_name_to_function_die,
556 lldb_private::UniqueCStringMap<dw_offset_t>& selector_name_to_function_die,
557 lldb_private::UniqueCStringMap<dw_offset_t>& name_to_type_die,
558 lldb_private::UniqueCStringMap<dw_offset_t>& name_to_global_die
Chris Lattner24943d22010-06-08 16:52:24 +0000559)
560{
Chris Lattner24943d22010-06-08 16:52:24 +0000561 const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data();
562
563 DWARFDebugInfoEntry::const_iterator pos;
564 DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin();
565 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
566 for (pos = begin; pos != end; ++pos)
567 {
568 const DWARFDebugInfoEntry &die = *pos;
569
570 const dw_tag_t tag = die.Tag();
571
572 switch (tag)
573 {
574 case DW_TAG_subprogram:
575 case DW_TAG_inlined_subroutine:
576 case DW_TAG_base_type:
577 case DW_TAG_class_type:
578 case DW_TAG_constant:
579 case DW_TAG_enumeration_type:
580 case DW_TAG_string_type:
581 case DW_TAG_subroutine_type:
582 case DW_TAG_structure_type:
583 case DW_TAG_union_type:
584 case DW_TAG_typedef:
585 case DW_TAG_namespace:
586 case DW_TAG_variable:
587 break;
588
589 default:
590 continue;
591 }
592
593 DWARFDebugInfoEntry::Attributes attributes;
594 const char *name = NULL;
Greg Claytone33bdd32010-09-03 23:26:12 +0000595 Mangled mangled;
Chris Lattner24943d22010-06-08 16:52:24 +0000596 bool is_variable = false;
597 bool is_declaration = false;
598 bool is_artificial = false;
599 bool has_address = false;
600 bool has_location = false;
601 bool is_global_or_static_variable = false;
Greg Clayton12bec712010-06-28 21:30:43 +0000602 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
Chris Lattner24943d22010-06-08 16:52:24 +0000603 const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, attributes);
604 if (num_attributes > 0)
605 {
606 uint32_t i;
Chris Lattner24943d22010-06-08 16:52:24 +0000607
608 is_variable = tag == DW_TAG_variable;
609
610 for (i=0; i<num_attributes; ++i)
611 {
612 dw_attr_t attr = attributes.AttributeAtIndex(i);
613 DWARFFormValue form_value;
614 switch (attr)
615 {
616 case DW_AT_name:
617 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
618 name = form_value.AsCString(debug_str);
619 break;
620
621 case DW_AT_declaration:
622 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
623 is_declaration = form_value.Unsigned() != 0;
624 break;
625
626 case DW_AT_artificial:
627 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
628 is_artificial = form_value.Unsigned() != 0;
629 break;
630
631 case DW_AT_MIPS_linkage_name:
632 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
Greg Claytone33bdd32010-09-03 23:26:12 +0000633 mangled.GetMangledName().SetCString(form_value.AsCString(debug_str));
Chris Lattner24943d22010-06-08 16:52:24 +0000634 break;
635
636 case DW_AT_low_pc:
637 case DW_AT_ranges:
638 case DW_AT_entry_pc:
639 has_address = true;
640 break;
641
642 case DW_AT_location:
643 has_location = true;
644 if (tag == DW_TAG_variable)
645 {
646 const DWARFDebugInfoEntry* parent_die = die.GetParent();
647 while ( parent_die != NULL )
648 {
649 switch (parent_die->Tag())
650 {
651 case DW_TAG_subprogram:
652 case DW_TAG_lexical_block:
653 case DW_TAG_inlined_subroutine:
654 // Even if this is a function level static, we don't add it. We could theoretically
655 // add these if we wanted to by introspecting into the DW_AT_location and seeing
656 // if the location describes a hard coded address, but we dont want the performance
657 // penalty of that right now.
658 is_global_or_static_variable = false;
659// if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
660// {
661// // If we have valid block data, then we have location expression bytes
662// // that are fixed (not a location list).
663// const uint8_t *block_data = form_value.BlockData();
664// if (block_data)
665// {
666// uint32_t block_length = form_value.Unsigned();
667// if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
668// {
669// if (block_data[0] == DW_OP_addr)
670// add_die = true;
671// }
672// }
673// }
674 parent_die = NULL; // Terminate the while loop.
675 break;
676
677 case DW_TAG_compile_unit:
678 is_global_or_static_variable = true;
679 parent_die = NULL; // Terminate the while loop.
680 break;
681
682 default:
683 parent_die = parent_die->GetParent(); // Keep going in the while loop.
684 break;
685 }
686 }
687 }
688 break;
Greg Clayton12bec712010-06-28 21:30:43 +0000689
690 case DW_AT_specification:
691 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
692 specification_die_offset = form_value.Reference(this);
693 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000694 }
695 }
696 }
697
698 switch (tag)
699 {
700 case DW_TAG_subprogram:
701 if (has_address)
702 {
Greg Clayton12bec712010-06-28 21:30:43 +0000703 if (name)
Chris Lattner24943d22010-06-08 16:52:24 +0000704 {
705 if ((name[0] == '-' || name[0] == '+') && name[1] == '[')
706 {
707 int name_len = strlen (name);
708 // Objective C methods must have at least:
709 // "-[" or "+[" prefix
710 // One character for a class name
711 // One character for the space between the class name
712 // One character for the method name
713 // "]" suffix
714 if (name_len >= 6 && name[name_len - 1] == ']')
715 {
716 const char *method_name = strchr (name, ' ');
717 if (method_name)
718 {
719 // Skip the space
720 ++method_name;
721 // Extract the objective C basename and add it to the
722 // accelerator tables
723 size_t method_name_len = name_len - (method_name - name) - 1;
724 ConstString method_const_str (method_name, method_name_len);
Greg Clayton12bec712010-06-28 21:30:43 +0000725 selector_name_to_function_die.Append(method_const_str.AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000726 }
727 }
728 }
Greg Clayton12bec712010-06-28 21:30:43 +0000729 // If we have a mangled name, then the DW_AT_name attribute
730 // is usually the method name without the class or any parameters
731 const DWARFDebugInfoEntry *parent = die.GetParent();
732 bool is_method = false;
733 if (parent)
734 {
Greg Claytonbef15832010-07-14 00:18:15 +0000735 dw_tag_t parent_tag = parent->Tag();
736 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
Greg Clayton12bec712010-06-28 21:30:43 +0000737 {
738 is_method = true;
739 }
740 else
741 {
742 if (mangled && specification_die_offset != DW_INVALID_OFFSET)
743 {
744 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL);
745 if (specification_die)
746 {
747 parent = specification_die->GetParent();
748 if (parent)
749 {
Greg Claytonbef15832010-07-14 00:18:15 +0000750 parent_tag = parent->Tag();
Greg Clayton12bec712010-06-28 21:30:43 +0000751
Greg Claytonbef15832010-07-14 00:18:15 +0000752 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
Greg Clayton12bec712010-06-28 21:30:43 +0000753 is_method = true;
754 }
755 }
756 }
757 }
758 }
759
760 if (is_method)
761 method_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
762 else
763 base_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000764 }
Greg Claytone33bdd32010-09-03 23:26:12 +0000765 if (mangled.GetMangledName())
766 full_name_to_function_die.Append(mangled.GetMangledName().AsCString(), die.GetOffset());
767 if (mangled.GetDemangledName())
768 full_name_to_function_die.Append(mangled.GetDemangledName().AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000769 }
770 break;
771
772 case DW_TAG_inlined_subroutine:
773 if (has_address)
774 {
Greg Clayton12bec712010-06-28 21:30:43 +0000775 if (name)
776 base_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
Greg Claytone33bdd32010-09-03 23:26:12 +0000777 if (mangled.GetMangledName())
778 full_name_to_function_die.Append(mangled.GetMangledName().AsCString(), die.GetOffset());
779 if (mangled.GetDemangledName())
780 full_name_to_function_die.Append(mangled.GetDemangledName().AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000781 }
782 break;
783
784 case DW_TAG_base_type:
785 case DW_TAG_class_type:
786 case DW_TAG_constant:
787 case DW_TAG_enumeration_type:
788 case DW_TAG_string_type:
789 case DW_TAG_subroutine_type:
790 case DW_TAG_structure_type:
791 case DW_TAG_union_type:
792 case DW_TAG_typedef:
793 case DW_TAG_namespace:
794 if (name && is_declaration == false)
795 {
796 name_to_type_die.Append(ConstString(name).AsCString(), die.GetOffset());
797 }
798 break;
799
800 case DW_TAG_variable:
801 if (name && has_location && is_global_or_static_variable)
802 {
803 AddGlobalDIEByIndex (std::distance (begin, pos));
804 name_to_global_die.Append(ConstString(name).AsCString(), die.GetOffset());
805 }
806 break;
807
808 default:
809 continue;
810 }
811 }
812}
813
814