blob: 13885a1248592a262092a97a40e790ffe2f23db9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.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// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
17#include "clang/Basic/Builtins.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/Specifiers.h"
23
24#include "lldb/Core/Module.h"
25#include "lldb/Core/PluginManager.h"
26#include "lldb/Core/RegularExpression.h"
27#include "lldb/Core/Scalar.h"
28#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000029#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Timer.h"
31#include "lldb/Core/Value.h"
32
33#include "lldb/Symbol/Block.h"
34#include "lldb/Symbol/CompileUnit.h"
35#include "lldb/Symbol/LineTable.h"
36#include "lldb/Symbol/ObjectFile.h"
37#include "lldb/Symbol/SymbolVendor.h"
38#include "lldb/Symbol/VariableList.h"
39
40#include "DWARFCompileUnit.h"
41#include "DWARFDebugAbbrev.h"
42#include "DWARFDebugAranges.h"
43#include "DWARFDebugInfo.h"
44#include "DWARFDebugInfoEntry.h"
45#include "DWARFDebugLine.h"
46#include "DWARFDebugPubnames.h"
47#include "DWARFDebugRanges.h"
48#include "DWARFDIECollection.h"
49#include "DWARFFormValue.h"
50#include "DWARFLocationList.h"
51#include "LogChannelDWARF.h"
52
53#include <map>
54
Greg Clayton594e5ed2010-09-27 21:07:38 +000055#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
57using namespace lldb;
58using namespace lldb_private;
59
60
Sean Callananc7fbf732010-08-06 00:32:49 +000061static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +000062DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063{
64 switch (dwarf_accessibility)
65 {
Sean Callananc7fbf732010-08-06 00:32:49 +000066 case DW_ACCESS_public: return eAccessPublic;
67 case DW_ACCESS_private: return eAccessPrivate;
68 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +000069 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 }
Sean Callananc7fbf732010-08-06 00:32:49 +000071 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
74void
75SymbolFileDWARF::Initialize()
76{
77 LogChannelDWARF::Initialize();
78 PluginManager::RegisterPlugin (GetPluginNameStatic(),
79 GetPluginDescriptionStatic(),
80 CreateInstance);
81}
82
83void
84SymbolFileDWARF::Terminate()
85{
86 PluginManager::UnregisterPlugin (CreateInstance);
87 LogChannelDWARF::Initialize();
88}
89
90
91const char *
92SymbolFileDWARF::GetPluginNameStatic()
93{
94 return "symbol-file.dwarf2";
95}
96
97const char *
98SymbolFileDWARF::GetPluginDescriptionStatic()
99{
100 return "DWARF and DWARF3 debug symbol file reader.";
101}
102
103
104SymbolFile*
105SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
106{
107 return new SymbolFileDWARF(obj_file);
108}
109
110//----------------------------------------------------------------------
111// Gets the first parent that is a lexical block, function or inlined
112// subroutine, or compile unit.
113//----------------------------------------------------------------------
114static const DWARFDebugInfoEntry *
115GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
116{
117 const DWARFDebugInfoEntry *die;
118 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
119 {
120 dw_tag_t tag = die->Tag();
121
122 switch (tag)
123 {
124 case DW_TAG_compile_unit:
125 case DW_TAG_subprogram:
126 case DW_TAG_inlined_subroutine:
127 case DW_TAG_lexical_block:
128 return die;
129 }
130 }
131 return NULL;
132}
133
134
135SymbolFileDWARF::SymbolFileDWARF(ObjectFile* ofile) :
136 SymbolFile(ofile),
137 m_flags(),
138 m_data_debug_abbrev(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 m_data_debug_frame(),
140 m_data_debug_info(),
141 m_data_debug_line(),
142 m_data_debug_loc(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 m_data_debug_ranges(),
144 m_data_debug_str(),
145 m_abbr(),
146 m_aranges(),
147 m_info(),
148 m_line(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000149 m_function_basename_index(),
150 m_function_fullname_index(),
151 m_function_method_index(),
152 m_function_selector_index(),
153 m_global_index(),
154 m_types_index(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 m_indexed(false),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000156 m_ranges()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157{
158}
159
160SymbolFileDWARF::~SymbolFileDWARF()
161{
162}
163
164bool
165SymbolFileDWARF::SupportedVersion(uint16_t version)
166{
167 return version == 2 || version == 3;
168}
169
170uint32_t
171SymbolFileDWARF::GetAbilities ()
172{
173 uint32_t abilities = 0;
174 if (m_obj_file != NULL)
175 {
176 const Section* section = NULL;
177 const SectionList *section_list = m_obj_file->GetSectionList();
178 if (section_list == NULL)
179 return 0;
180
181 uint64_t debug_abbrev_file_size = 0;
182 uint64_t debug_aranges_file_size = 0;
183 uint64_t debug_frame_file_size = 0;
184 uint64_t debug_info_file_size = 0;
185 uint64_t debug_line_file_size = 0;
186 uint64_t debug_loc_file_size = 0;
187 uint64_t debug_macinfo_file_size = 0;
188 uint64_t debug_pubnames_file_size = 0;
189 uint64_t debug_pubtypes_file_size = 0;
190 uint64_t debug_ranges_file_size = 0;
191 uint64_t debug_str_file_size = 0;
192
193 static ConstString g_dwarf_section_name ("__DWARF");
194
195 section = section_list->FindSectionByName(g_dwarf_section_name).get();
196
197 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000198 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
Greg Clayton4ceb9982010-07-21 22:54:26 +0000200 section_list = &section->GetChildren ();
201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
Greg Clayton4ceb9982010-07-21 22:54:26 +0000203 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 if (section != NULL)
205 {
206 debug_info_file_size = section->GetByteSize();
207
Greg Clayton4ceb9982010-07-21 22:54:26 +0000208 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 if (section)
210 debug_abbrev_file_size = section->GetByteSize();
211 else
212 m_flags.Set (flagsGotDebugAbbrevData);
213
Greg Clayton4ceb9982010-07-21 22:54:26 +0000214 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 if (section)
216 debug_aranges_file_size = section->GetByteSize();
217 else
218 m_flags.Set (flagsGotDebugArangesData);
219
Greg Clayton4ceb9982010-07-21 22:54:26 +0000220 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 if (section)
222 debug_frame_file_size = section->GetByteSize();
223 else
224 m_flags.Set (flagsGotDebugFrameData);
225
Greg Clayton4ceb9982010-07-21 22:54:26 +0000226 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 if (section)
228 debug_line_file_size = section->GetByteSize();
229 else
230 m_flags.Set (flagsGotDebugLineData);
231
Greg Clayton4ceb9982010-07-21 22:54:26 +0000232 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 if (section)
234 debug_loc_file_size = section->GetByteSize();
235 else
236 m_flags.Set (flagsGotDebugLocData);
237
Greg Clayton4ceb9982010-07-21 22:54:26 +0000238 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 if (section)
240 debug_macinfo_file_size = section->GetByteSize();
241 else
242 m_flags.Set (flagsGotDebugMacInfoData);
243
Greg Clayton4ceb9982010-07-21 22:54:26 +0000244 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 if (section)
246 debug_pubnames_file_size = section->GetByteSize();
247 else
248 m_flags.Set (flagsGotDebugPubNamesData);
249
Greg Clayton4ceb9982010-07-21 22:54:26 +0000250 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 if (section)
252 debug_pubtypes_file_size = section->GetByteSize();
253 else
254 m_flags.Set (flagsGotDebugPubTypesData);
255
Greg Clayton4ceb9982010-07-21 22:54:26 +0000256 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 if (section)
258 debug_ranges_file_size = section->GetByteSize();
259 else
260 m_flags.Set (flagsGotDebugRangesData);
261
Greg Clayton4ceb9982010-07-21 22:54:26 +0000262 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 if (section)
264 debug_str_file_size = section->GetByteSize();
265 else
266 m_flags.Set (flagsGotDebugStrData);
267 }
268
269 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
270 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
271
272 if (debug_line_file_size > 0)
273 abilities |= LineTables;
274
275 if (debug_aranges_file_size > 0)
276 abilities |= AddressAcceleratorTable;
277
278 if (debug_pubnames_file_size > 0)
279 abilities |= FunctionAcceleratorTable;
280
281 if (debug_pubtypes_file_size > 0)
282 abilities |= TypeAcceleratorTable;
283
284 if (debug_macinfo_file_size > 0)
285 abilities |= MacroInformation;
286
287 if (debug_frame_file_size > 0)
288 abilities |= CallFrameInformation;
289 }
290 return abilities;
291}
292
293const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000294SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295{
296 if (m_flags.IsClear (got_flag))
297 {
298 m_flags.Set (got_flag);
299 const SectionList *section_list = m_obj_file->GetSectionList();
300 if (section_list)
301 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000302 Section *section = section_list->FindSectionByType(sect_type, true).get();
303 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 {
305 // See if we memory mapped the DWARF segment?
306 if (m_dwarf_data.GetByteSize())
307 {
308 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
309 }
310 else
311 {
312 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
313 data.Clear();
314 }
315 }
316 }
317 }
318 return data;
319}
320
321const DataExtractor&
322SymbolFileDWARF::get_debug_abbrev_data()
323{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000324 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325}
326
327const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328SymbolFileDWARF::get_debug_frame_data()
329{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000330 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331}
332
333const DataExtractor&
334SymbolFileDWARF::get_debug_info_data()
335{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000336 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
339const DataExtractor&
340SymbolFileDWARF::get_debug_line_data()
341{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000342 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343}
344
345const DataExtractor&
346SymbolFileDWARF::get_debug_loc_data()
347{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000348 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
351const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352SymbolFileDWARF::get_debug_ranges_data()
353{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000354 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
357const DataExtractor&
358SymbolFileDWARF::get_debug_str_data()
359{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000360 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361}
362
363
364DWARFDebugAbbrev*
365SymbolFileDWARF::DebugAbbrev()
366{
367 if (m_abbr.get() == NULL)
368 {
369 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
370 if (debug_abbrev_data.GetByteSize() > 0)
371 {
372 m_abbr.reset(new DWARFDebugAbbrev());
373 if (m_abbr.get())
374 m_abbr->Parse(debug_abbrev_data);
375 }
376 }
377 return m_abbr.get();
378}
379
380const DWARFDebugAbbrev*
381SymbolFileDWARF::DebugAbbrev() const
382{
383 return m_abbr.get();
384}
385
386DWARFDebugAranges*
387SymbolFileDWARF::DebugAranges()
388{
Greg Clayton016a95e2010-09-14 02:20:48 +0000389 // It turns out that llvm-gcc doesn't generate .debug_aranges in .o files
390 // and we are already parsing all of the DWARF because the .debug_pubnames
391 // is useless (it only mentions symbols that are externally visible), so
392 // don't use the .debug_aranges section, we should be using a debug aranges
393 // we got from SymbolFileDWARF::Index().
394
395 if (!m_indexed)
396 Index();
397
398
399// if (m_aranges.get() == NULL)
400// {
401// Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
402// m_aranges.reset(new DWARFDebugAranges());
403// if (m_aranges.get())
404// {
405// const DataExtractor &debug_aranges_data = get_debug_aranges_data();
406// if (debug_aranges_data.GetByteSize() > 0)
407// m_aranges->Extract(debug_aranges_data);
408// else
409// m_aranges->Generate(this);
410// }
411// }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 return m_aranges.get();
413}
414
415const DWARFDebugAranges*
416SymbolFileDWARF::DebugAranges() const
417{
418 return m_aranges.get();
419}
420
421
422DWARFDebugInfo*
423SymbolFileDWARF::DebugInfo()
424{
425 if (m_info.get() == NULL)
426 {
427 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
428 if (get_debug_info_data().GetByteSize() > 0)
429 {
430 m_info.reset(new DWARFDebugInfo());
431 if (m_info.get())
432 {
433 m_info->SetDwarfData(this);
434 }
435 }
436 }
437 return m_info.get();
438}
439
440const DWARFDebugInfo*
441SymbolFileDWARF::DebugInfo() const
442{
443 return m_info.get();
444}
445
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446DWARFCompileUnit*
447SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
448{
449 DWARFDebugInfo* info = DebugInfo();
450 if (info)
451 return info->GetCompileUnit(cu_uid).get();
452 return NULL;
453}
454
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455
456DWARFDebugRanges*
457SymbolFileDWARF::DebugRanges()
458{
459 if (m_ranges.get() == NULL)
460 {
461 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
462 if (get_debug_ranges_data().GetByteSize() > 0)
463 {
464 m_ranges.reset(new DWARFDebugRanges());
465 if (m_ranges.get())
466 m_ranges->Extract(this);
467 }
468 }
469 return m_ranges.get();
470}
471
472const DWARFDebugRanges*
473SymbolFileDWARF::DebugRanges() const
474{
475 return m_ranges.get();
476}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477
478bool
Greg Clayton0fffff52010-09-24 05:15:53 +0000479SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480{
481 if (cu != NULL)
482 {
483 const DWARFDebugInfoEntry * cu_die = cu->GetCompileUnitDIEOnly ();
484 if (cu_die)
485 {
486 const char * cu_die_name = cu_die->GetName(this, cu);
487 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, cu, DW_AT_comp_dir, NULL);
Greg Clayton9e409562010-07-28 02:04:09 +0000488 LanguageType class_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 if (cu_die_name)
490 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000491 FileSpec cu_file_spec;
492
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 if (cu_die_name[0] == '/' || cu_comp_dir == NULL && cu_comp_dir[0])
494 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000495 // If we have a full path to the compile unit, we don't need to resolve
496 // the file. This can be expensive e.g. when the source files are NFS mounted.
497 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 }
499 else
500 {
501 std::string fullpath(cu_comp_dir);
502 if (*fullpath.rbegin() != '/')
503 fullpath += '/';
504 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000505 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 }
507
Jim Ingham0909e5f2010-09-16 00:57:33 +0000508 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), cu, cu_file_spec, cu->GetOffset(), class_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 if (compile_unit_sp.get())
510 {
511 cu->SetUserData(compile_unit_sp.get());
512 return true;
513 }
514 }
515 }
516 }
517 return false;
518}
519
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520uint32_t
521SymbolFileDWARF::GetNumCompileUnits()
522{
523 DWARFDebugInfo* info = DebugInfo();
524 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 return 0;
527}
528
529CompUnitSP
530SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
531{
532 CompUnitSP comp_unit;
533 DWARFDebugInfo* info = DebugInfo();
534 if (info)
535 {
536 DWARFCompileUnit* cu = info->GetCompileUnitAtIndex(cu_idx);
537 if (cu != NULL)
538 {
539 // Our symbol vendor shouldn't be asking us to add a compile unit that
540 // has already been added to it, which this DWARF plug-in knows as it
541 // stores the lldb compile unit (CompileUnit) pointer in each
542 // DWARFCompileUnit object when it gets added.
543 assert(cu->GetUserData() == NULL);
544 ParseCompileUnit(cu, comp_unit);
545 }
546 }
547 return comp_unit;
548}
549
550static void
551AddRangesToBlock
552(
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000553 Block& block,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 DWARFDebugRanges::RangeList& ranges,
555 addr_t block_base_addr
556)
557{
558 ranges.SubtractOffset (block_base_addr);
559 size_t range_idx = 0;
560 const DWARFDebugRanges::Range *debug_range;
561 for (range_idx = 0; (debug_range = ranges.RangeAtIndex(range_idx)) != NULL; range_idx++)
562 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000563 block.AddRange(debug_range->begin_offset, debug_range->end_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 }
565}
566
567
568Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000569SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570{
571 DWARFDebugRanges::RangeList func_ranges;
572 const char *name = NULL;
573 const char *mangled = NULL;
574 int decl_file = 0;
575 int decl_line = 0;
576 int decl_column = 0;
577 int call_file = 0;
578 int call_line = 0;
579 int call_column = 0;
580 DWARFExpression frame_base;
581
582 // Parse the function prototype as a type that can then be added to concrete function instance
583 ParseTypes (sc, dwarf_cu, die, false, false);
584 //FixupTypes();
585
586 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
587 {
588 // Union of all ranges in the function DIE (if the function is discontiguous)
589 AddressRange func_range;
590 lldb::addr_t lowest_func_addr = func_ranges.LowestAddress(0);
591 lldb::addr_t highest_func_addr = func_ranges.HighestAddress(0);
592 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
593 {
594 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
595 if (func_range.GetBaseAddress().IsValid())
596 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
597 }
598
599 if (func_range.GetBaseAddress().IsValid())
600 {
601 Mangled func_name;
602 if (mangled)
603 func_name.SetValue(mangled, true);
604 else if (name)
605 func_name.SetValue(name, false);
606
607 FunctionSP func_sp;
608 std::auto_ptr<Declaration> decl_ap;
609 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
610 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), decl_line, decl_column));
611
Greg Clayton594e5ed2010-09-27 21:07:38 +0000612 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613
614 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
615
616 func_range.GetBaseAddress().ResolveLinkedAddress();
617
618 func_sp.reset(new Function (sc.comp_unit,
619 die->GetOffset(), // UserID is the DIE offset
620 die->GetOffset(),
621 func_name,
622 func_type,
623 func_range)); // first address range
624
625 if (func_sp.get() != NULL)
626 {
627 func_sp->GetFrameBaseExpression() = frame_base;
628 sc.comp_unit->AddFunction(func_sp);
629 return func_sp.get();
630 }
631 }
632 }
633 return NULL;
634}
635
636size_t
637SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
638{
639 assert (sc.comp_unit);
640 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000641 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 if (dwarf_cu)
643 {
644 DWARFDIECollection function_dies;
645 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
646 size_t func_idx;
647 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
648 {
649 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
650 if (sc.comp_unit->FindFunctionByUID (die->GetOffset()).get() == NULL)
651 {
652 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
653 ++functions_added;
654 }
655 }
656 //FixupTypes();
657 }
658 return functions_added;
659}
660
661bool
662SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
663{
664 assert (sc.comp_unit);
665 DWARFCompileUnit* cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
666 assert (cu);
667 const DWARFDebugInfoEntry * cu_die = cu->GetCompileUnitDIEOnly();
668
669 if (cu_die)
670 {
671 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, cu, DW_AT_comp_dir, NULL);
672 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
673
674 // All file indexes in DWARF are one based and a file of index zero is
675 // supposed to be the compile unit itself.
676 support_files.Append (*sc.comp_unit);
677
678 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
679 }
680 return false;
681}
682
683struct ParseDWARFLineTableCallbackInfo
684{
685 LineTable* line_table;
686 const SectionList *section_list;
687 lldb::addr_t prev_sect_file_base_addr;
688 lldb::addr_t curr_sect_file_base_addr;
689 bool is_oso_for_debug_map;
690 bool prev_in_final_executable;
691 DWARFDebugLine::Row prev_row;
692 SectionSP prev_section_sp;
693 SectionSP curr_section_sp;
694};
695
696//----------------------------------------------------------------------
697// ParseStatementTableCallback
698//----------------------------------------------------------------------
699static void
700ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
701{
702 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
703 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
704 {
705 // Just started parsing the line table
706 }
707 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
708 {
709 // Done parsing line table, nothing to do for the cleanup
710 }
711 else
712 {
713 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
714 // We have a new row, lets append it
715
716 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
717 {
718 info->prev_section_sp = info->curr_section_sp;
719 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
720 // If this is an end sequence entry, then we subtract one from the
721 // address to make sure we get an address that is not the end of
722 // a section.
723 if (state.end_sequence && state.address != 0)
724 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
725 else
726 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
727
728 if (info->curr_section_sp.get())
729 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
730 else
731 info->curr_sect_file_base_addr = 0;
732 }
733 if (info->curr_section_sp.get())
734 {
735 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
736 // Check for the fancy section magic to determine if we
737
738 if (info->is_oso_for_debug_map)
739 {
740 // When this is a debug map object file that contains DWARF
741 // (referenced from an N_OSO debug map nlist entry) we will have
742 // a file address in the file range for our section from the
743 // original .o file, and a load address in the executable that
744 // contains the debug map.
745 //
746 // If the sections for the file range and load range are
747 // different, we have a remapped section for the function and
748 // this address is resolved. If they are the same, then the
749 // function for this address didn't make it into the final
750 // executable.
751 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
752
753 // If we are doing DWARF with debug map, then we need to carefully
754 // add each line table entry as there may be gaps as functions
755 // get moved around or removed.
756 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
757 {
758 if (info->prev_in_final_executable)
759 {
760 bool terminate_previous_entry = false;
761 if (!curr_in_final_executable)
762 {
763 // Check for the case where the previous line entry
764 // in a function made it into the final executable,
765 // yet the current line entry falls in a function
766 // that didn't. The line table used to be contiguous
767 // through this address range but now it isn't. We
768 // need to terminate the previous line entry so
769 // that we can reconstruct the line range correctly
770 // for it and to keep the line table correct.
771 terminate_previous_entry = true;
772 }
773 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
774 {
775 // Check for cases where the line entries used to be
776 // contiguous address ranges, but now they aren't.
777 // This can happen when order files specify the
778 // ordering of the functions.
779 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
780 Section *curr_sect = info->curr_section_sp.get();
781 Section *prev_sect = info->prev_section_sp.get();
782 assert (curr_sect->GetLinkedSection());
783 assert (prev_sect->GetLinkedSection());
784 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
785 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
786 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
787 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
788 if (object_file_addr_delta != linked_file_addr_delta)
789 terminate_previous_entry = true;
790 }
791
792 if (terminate_previous_entry)
793 {
794 line_table->InsertLineEntry (info->prev_section_sp,
795 state.address - info->prev_sect_file_base_addr,
796 info->prev_row.line,
797 info->prev_row.column,
798 info->prev_row.file,
799 false, // is_stmt
800 false, // basic_block
801 false, // state.prologue_end
802 false, // state.epilogue_begin
803 true); // end_sequence);
804 }
805 }
806 }
807
808 if (curr_in_final_executable)
809 {
810 line_table->InsertLineEntry (info->curr_section_sp,
811 curr_line_section_offset,
812 state.line,
813 state.column,
814 state.file,
815 state.is_stmt,
816 state.basic_block,
817 state.prologue_end,
818 state.epilogue_begin,
819 state.end_sequence);
820 info->prev_section_sp = info->curr_section_sp;
821 }
822 else
823 {
824 // If the current address didn't make it into the final
825 // executable, the current section will be the __text
826 // segment in the .o file, so we need to clear this so
827 // we can catch the next function that did make it into
828 // the final executable.
829 info->prev_section_sp.reset();
830 info->curr_section_sp.reset();
831 }
832
833 info->prev_in_final_executable = curr_in_final_executable;
834 }
835 else
836 {
837 // We are not in an object file that contains DWARF for an
838 // N_OSO, this is just a normal DWARF file. The DWARF spec
839 // guarantees that the addresses will be in increasing order
840 // so, since we store line tables in file address order, we
841 // can always just append the line entry without needing to
842 // search for the correct insertion point (we don't need to
843 // use LineEntry::InsertLineEntry()).
844 line_table->AppendLineEntry (info->curr_section_sp,
845 curr_line_section_offset,
846 state.line,
847 state.column,
848 state.file,
849 state.is_stmt,
850 state.basic_block,
851 state.prologue_end,
852 state.epilogue_begin,
853 state.end_sequence);
854 }
855 }
856
857 info->prev_row = state;
858 }
859}
860
861bool
862SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
863{
864 assert (sc.comp_unit);
865 if (sc.comp_unit->GetLineTable() != NULL)
866 return true;
867
868 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
869 if (dwarf_cu)
870 {
871 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
872 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
873 if (cu_line_offset != DW_INVALID_OFFSET)
874 {
875 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
876 if (line_table_ap.get())
877 {
878 ParseDWARFLineTableCallbackInfo info = { line_table_ap.get(), m_obj_file->GetSectionList(), 0, 0, m_flags.IsSet (flagsDWARFIsOSOForDebugMap), false};
879 uint32_t offset = cu_line_offset;
880 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
881 sc.comp_unit->SetLineTable(line_table_ap.release());
882 return true;
883 }
884 }
885 }
886 return false;
887}
888
889size_t
890SymbolFileDWARF::ParseFunctionBlocks
891(
892 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000893 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +0000894 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 const DWARFDebugInfoEntry *die,
896 addr_t subprogram_low_pc,
897 bool parse_siblings,
898 bool parse_children
899)
900{
901 size_t blocks_added = 0;
902 while (die != NULL)
903 {
904 dw_tag_t tag = die->Tag();
905
906 switch (tag)
907 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 case DW_TAG_inlined_subroutine:
Jim Inghamb0be4422010-08-12 01:20:14 +0000909 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 case DW_TAG_lexical_block:
911 {
912 DWARFDebugRanges::RangeList ranges;
913 const char *name = NULL;
914 const char *mangled_name = NULL;
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000915 Block *block = NULL;
916 if (tag != DW_TAG_subprogram)
917 {
918 BlockSP block_sp(new Block (die->GetOffset()));
919 parent_block->AddChild(block_sp);
920 block = block_sp.get();
921 }
922 else
923 {
924 block = parent_block;
925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 int decl_file = 0;
928 int decl_line = 0;
929 int decl_column = 0;
930 int call_file = 0;
931 int call_line = 0;
932 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000933 if (die->GetDIENamesAndRanges (this,
934 dwarf_cu,
935 name,
936 mangled_name,
937 ranges,
938 decl_file, decl_line, decl_column,
939 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 {
941 if (tag == DW_TAG_subprogram)
942 {
943 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
944 subprogram_low_pc = ranges.LowestAddress(0);
945 }
Jim Inghamb0be4422010-08-12 01:20:14 +0000946 else if (tag == DW_TAG_inlined_subroutine)
947 {
948 // We get called here for inlined subroutines in two ways.
949 // The first time is when we are making the Function object
950 // for this inlined concrete instance. Since we're creating a top level block at
951 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
952 // adjust the containing address.
953 // The second time is when we are parsing the blocks inside the function that contains
954 // the inlined concrete instance. Since these will be blocks inside the containing "real"
955 // function the offset will be for that function.
956 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
957 {
958 subprogram_low_pc = ranges.LowestAddress(0);
959 }
960 }
961
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000962 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963
964 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
965 {
966 std::auto_ptr<Declaration> decl_ap;
967 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +0000968 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
969 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970
971 std::auto_ptr<Declaration> call_ap;
972 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +0000973 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
974 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000976 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 }
978
979 ++blocks_added;
980
981 if (parse_children && die->HasChildren())
982 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000983 blocks_added += ParseFunctionBlocks (sc,
984 block,
985 dwarf_cu,
986 die->GetFirstChild(),
987 subprogram_low_pc,
988 true,
989 true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990 }
991 }
992 }
993 break;
994 default:
995 break;
996 }
997
998 if (parse_siblings)
999 die = die->GetSibling();
1000 else
1001 die = NULL;
1002 }
1003 return blocks_added;
1004}
1005
1006size_t
1007SymbolFileDWARF::ParseChildMembers
1008(
1009 const SymbolContext& sc,
1010 TypeSP& type_sp,
Greg Clayton0fffff52010-09-24 05:15:53 +00001011 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 const DWARFDebugInfoEntry *parent_die,
Greg Clayton9e409562010-07-28 02:04:09 +00001013 void *class_clang_type,
1014 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1016 std::vector<int>& member_accessibilities,
Sean Callananc7fbf732010-08-06 00:32:49 +00001017 AccessType& default_accessibility,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 bool &is_a_class
1019)
1020{
1021 if (parent_die == NULL)
1022 return 0;
1023
1024 TypeList* type_list = m_obj_file->GetModule()->GetTypeList();
1025
1026 size_t count = 0;
1027 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001028 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1029
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1031 {
1032 dw_tag_t tag = die->Tag();
1033
1034 switch (tag)
1035 {
1036 case DW_TAG_member:
1037 {
1038 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00001039 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 if (num_attributes > 0)
1041 {
1042 Declaration decl;
1043 DWARFExpression location;
1044 const char *name = NULL;
1045 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001046 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047 off_t member_offset = 0;
1048 size_t byte_size = 0;
1049 size_t bit_offset = 0;
1050 size_t bit_size = 0;
1051 uint32_t i;
1052 for (i=0; i<num_attributes; ++i)
1053 {
1054 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1055 DWARFFormValue form_value;
1056 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1057 {
1058 switch (attr)
1059 {
1060 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1061 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1062 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1063 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1064 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1065 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1066 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1067 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1068 case DW_AT_data_member_location:
1069 if (form_value.BlockData())
1070 {
1071 Value initialValue(0);
1072 Value memberOffset(0);
1073 const DataExtractor& debug_info_data = get_debug_info_data();
1074 uint32_t block_length = form_value.Unsigned();
1075 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1076 if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1077 {
1078 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1079 }
1080 }
1081 break;
1082
Greg Clayton8cf05932010-07-22 18:30:50 +00001083 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 case DW_AT_declaration:
1085 case DW_AT_description:
1086 case DW_AT_mutable:
1087 case DW_AT_visibility:
1088 default:
1089 case DW_AT_sibling:
1090 break;
1091 }
1092 }
1093 }
1094
1095 Type *member_type = ResolveTypeUID(encoding_uid);
1096 assert(member_type);
Sean Callananc7fbf732010-08-06 00:32:49 +00001097 if (accessibility == eAccessNone)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098 accessibility = default_accessibility;
1099 member_accessibilities.push_back(accessibility);
1100
1101 type_list->GetClangASTContext().AddFieldToRecordType (type_sp->GetOpaqueClangQualType(), name, member_type->GetOpaqueClangQualType(), accessibility, bit_size);
1102 }
1103 }
1104 break;
1105
1106 case DW_TAG_subprogram:
Greg Clayton0fffff52010-09-24 05:15:53 +00001107 // Let the type parsing code handle this one for us...
1108 ResolveType (dwarf_cu, die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 break;
1110
1111 case DW_TAG_inheritance:
1112 {
1113 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001114 if (default_accessibility == eAccessNone)
1115 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 // TODO: implement DW_TAG_inheritance type parsing
1117 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00001118 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 if (num_attributes > 0)
1120 {
1121 Declaration decl;
1122 DWARFExpression location;
1123 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001124 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125 bool is_virtual = false;
1126 bool is_base_of_class = true;
1127 off_t member_offset = 0;
1128 uint32_t i;
1129 for (i=0; i<num_attributes; ++i)
1130 {
1131 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1132 DWARFFormValue form_value;
1133 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1134 {
1135 switch (attr)
1136 {
1137 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1138 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1139 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1140 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1141 case DW_AT_data_member_location:
1142 if (form_value.BlockData())
1143 {
1144 Value initialValue(0);
1145 Value memberOffset(0);
1146 const DataExtractor& debug_info_data = get_debug_info_data();
1147 uint32_t block_length = form_value.Unsigned();
1148 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1149 if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1150 {
1151 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1152 }
1153 }
1154 break;
1155
1156 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001157 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 break;
1159
1160 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1161 default:
1162 case DW_AT_sibling:
1163 break;
1164 }
1165 }
1166 }
1167
1168 Type *base_class_dctype = ResolveTypeUID(encoding_uid);
1169 assert(base_class_dctype);
Greg Clayton9e409562010-07-28 02:04:09 +00001170
1171 if (class_language == eLanguageTypeObjC)
1172 {
1173 type_list->GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_dctype->GetOpaqueClangQualType());
1174 }
1175 else
1176 {
1177 base_classes.push_back (type_list->GetClangASTContext().CreateBaseClassSpecifier (base_class_dctype->GetOpaqueClangQualType(), accessibility, is_virtual, is_base_of_class));
1178 assert(base_classes.back());
1179 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 }
1181 }
1182 break;
1183
1184 default:
1185 break;
1186 }
1187 }
1188 return count;
1189}
1190
1191
1192clang::DeclContext*
1193SymbolFileDWARF::GetClangDeclContextForTypeUID (lldb::user_id_t type_uid)
1194{
1195 DWARFDebugInfo* debug_info = DebugInfo();
1196 if (debug_info)
1197 {
1198 DWARFCompileUnitSP cu_sp;
1199 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1200 if (die)
1201 return GetClangDeclContextForDIE (cu_sp.get(), die);
1202 }
1203 return NULL;
1204}
1205
1206Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001207SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208{
1209 DWARFDebugInfo* debug_info = DebugInfo();
1210 if (debug_info)
1211 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001212 DWARFCompileUnitSP cu_sp;
1213 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 if (type_die != NULL)
Greg Clayton594e5ed2010-09-27 21:07:38 +00001215 return ResolveType (cu_sp.get(), type_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 }
1217 return NULL;
1218}
1219
Greg Claytonc685f8e2010-09-15 04:15:46 +00001220Type*
1221SymbolFileDWARF::ResolveType (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* type_die)
1222{
1223 if (type_die != NULL)
1224 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001225 Type *type = m_die_to_type.lookup (type_die);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001226 if (type == NULL)
1227 {
1228 TypeSP owning_type_sp;
Greg Clayton594e5ed2010-09-27 21:07:38 +00001229 type = GetTypeForDIE(cu, type_die, owning_type_sp, 0, 0).get();
Greg Claytonc685f8e2010-09-15 04:15:46 +00001230 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00001231 assert (type != DIE_IS_BEING_PARSED);
1232 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001233 }
1234 return NULL;
1235}
1236
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237CompileUnit*
Greg Clayton0fffff52010-09-24 05:15:53 +00001238SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239{
1240 // Check if the symbol vendor already knows about this compile unit?
1241 if (cu->GetUserData() == NULL)
1242 {
1243 // The symbol vendor doesn't know about this compile unit, we
1244 // need to parse and add it to the symbol vendor object.
1245 CompUnitSP dc_cu;
1246 ParseCompileUnit(cu, dc_cu);
1247 if (dc_cu.get())
1248 {
1249 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00001250 if (cu_idx == UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 DebugInfo()->GetCompileUnit(cu->GetOffset(), &cu_idx);
1252
1253 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
1254 }
1255 }
1256 return (CompileUnit*)cu->GetUserData();
1257}
1258
1259bool
1260SymbolFileDWARF::GetFunction (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
1261{
1262 sc.Clear();
1263 // Check if the symbol vendor already knows about this compile unit?
1264 sc.module_sp = m_obj_file->GetModule()->GetSP();
Greg Clayton016a95e2010-09-14 02:20:48 +00001265 sc.comp_unit = GetCompUnitForDWARFCompUnit(cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266
1267 sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get();
1268 if (sc.function == NULL)
1269 sc.function = ParseCompileUnitFunction(sc, cu, func_die);
1270
1271 return sc.function != NULL;
1272}
1273
1274uint32_t
1275SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1276{
1277 Timer scoped_timer(__PRETTY_FUNCTION__,
1278 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1279 so_addr.GetSection(),
1280 so_addr.GetOffset(),
1281 resolve_scope);
1282 uint32_t resolved = 0;
1283 if (resolve_scope & ( eSymbolContextCompUnit |
1284 eSymbolContextFunction |
1285 eSymbolContextBlock |
1286 eSymbolContextLineEntry))
1287 {
1288 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1289
1290 DWARFDebugAranges* debug_aranges = DebugAranges();
1291 DWARFDebugInfo* debug_info = DebugInfo();
1292 if (debug_aranges)
1293 {
1294 dw_offset_t cu_offset = debug_aranges->FindAddress(file_vm_addr);
1295 if (cu_offset != DW_INVALID_OFFSET)
1296 {
1297 uint32_t cu_idx;
1298 DWARFCompileUnit* cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1299 if (cu)
1300 {
1301 sc.comp_unit = GetCompUnitForDWARFCompUnit(cu, cu_idx);
1302 assert(sc.comp_unit != NULL);
1303 resolved |= eSymbolContextCompUnit;
1304
1305 if (resolve_scope & eSymbolContextLineEntry)
1306 {
1307 LineTable *line_table = sc.comp_unit->GetLineTable();
1308 if (line_table == NULL)
1309 {
1310 if (ParseCompileUnitLineTable(sc))
1311 line_table = sc.comp_unit->GetLineTable();
1312 }
1313 if (line_table != NULL)
1314 {
1315 if (so_addr.IsLinkedAddress())
1316 {
1317 Address linked_addr (so_addr);
1318 linked_addr.ResolveLinkedAddress();
1319 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
1320 {
1321 resolved |= eSymbolContextLineEntry;
1322 }
1323 }
1324 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
1325 {
1326 resolved |= eSymbolContextLineEntry;
1327 }
1328 }
1329 }
1330
1331 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1332 {
1333 DWARFDebugInfoEntry *function_die = NULL;
1334 DWARFDebugInfoEntry *block_die = NULL;
1335 if (resolve_scope & eSymbolContextBlock)
1336 {
1337 cu->LookupAddress(file_vm_addr, &function_die, &block_die);
1338 }
1339 else
1340 {
1341 cu->LookupAddress(file_vm_addr, &function_die, NULL);
1342 }
1343
1344 if (function_die != NULL)
1345 {
1346 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1347 if (sc.function == NULL)
1348 sc.function = ParseCompileUnitFunction(sc, cu, function_die);
1349 }
1350
1351 if (sc.function != NULL)
1352 {
1353 resolved |= eSymbolContextFunction;
1354
1355 if (resolve_scope & eSymbolContextBlock)
1356 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001357 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358
1359 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001360 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001362 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363 if (sc.block)
1364 resolved |= eSymbolContextBlock;
1365 }
1366 }
1367 }
1368 }
1369 }
1370 }
1371 }
1372 return resolved;
1373}
1374
1375
1376
1377uint32_t
1378SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1379{
1380 const uint32_t prev_size = sc_list.GetSize();
1381 if (resolve_scope & eSymbolContextCompUnit)
1382 {
1383 DWARFDebugInfo* debug_info = DebugInfo();
1384 if (debug_info)
1385 {
1386 uint32_t cu_idx;
1387 DWARFCompileUnit* cu = NULL;
1388
1389 for (cu_idx = 0; (cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
1390 {
1391 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(cu, cu_idx);
1392 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
1393 if (check_inlines || file_spec_matches_cu_file_spec)
1394 {
1395 SymbolContext sc (m_obj_file->GetModule());
1396 sc.comp_unit = GetCompUnitForDWARFCompUnit(cu, cu_idx);
1397 assert(sc.comp_unit != NULL);
1398
1399 uint32_t file_idx = UINT32_MAX;
1400
1401 // If we are looking for inline functions only and we don't
1402 // find it in the support files, we are done.
1403 if (check_inlines)
1404 {
1405 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
1406 if (file_idx == UINT32_MAX)
1407 continue;
1408 }
1409
1410 if (line != 0)
1411 {
1412 LineTable *line_table = sc.comp_unit->GetLineTable();
1413
1414 if (line_table != NULL && line != 0)
1415 {
1416 // We will have already looked up the file index if
1417 // we are searching for inline entries.
1418 if (!check_inlines)
1419 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
1420
1421 if (file_idx != UINT32_MAX)
1422 {
1423 uint32_t found_line;
1424 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1425 found_line = sc.line_entry.line;
1426
Greg Clayton016a95e2010-09-14 02:20:48 +00001427 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 {
1429 sc.function = NULL;
1430 sc.block = NULL;
1431 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1432 {
1433 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1434 if (file_vm_addr != LLDB_INVALID_ADDRESS)
1435 {
1436 DWARFDebugInfoEntry *function_die = NULL;
1437 DWARFDebugInfoEntry *block_die = NULL;
1438 cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
1439
1440 if (function_die != NULL)
1441 {
1442 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1443 if (sc.function == NULL)
1444 sc.function = ParseCompileUnitFunction(sc, cu, function_die);
1445 }
1446
1447 if (sc.function != NULL)
1448 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001449 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001450
1451 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001452 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001454 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 }
1456 }
1457 }
1458
1459 sc_list.Append(sc);
1460 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1461 }
1462 }
1463 }
1464 else if (file_spec_matches_cu_file_spec && !check_inlines)
1465 {
1466 // only append the context if we aren't looking for inline call sites
1467 // by file and line and if the file spec matches that of the compile unit
1468 sc_list.Append(sc);
1469 }
1470 }
1471 else if (file_spec_matches_cu_file_spec && !check_inlines)
1472 {
1473 // only append the context if we aren't looking for inline call sites
1474 // by file and line and if the file spec matches that of the compile unit
1475 sc_list.Append(sc);
1476 }
1477
1478 if (!check_inlines)
1479 break;
1480 }
1481 }
1482 }
1483 }
1484 return sc_list.GetSize() - prev_size;
1485}
1486
1487void
1488SymbolFileDWARF::Index ()
1489{
1490 if (m_indexed)
1491 return;
1492 m_indexed = true;
1493 Timer scoped_timer (__PRETTY_FUNCTION__,
1494 "SymbolFileDWARF::Index (%s)",
1495 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1496
1497 DWARFDebugInfo* debug_info = DebugInfo();
1498 if (debug_info)
1499 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001500 m_aranges.reset(new DWARFDebugAranges());
1501
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 uint32_t cu_idx = 0;
1503 const uint32_t num_compile_units = GetNumCompileUnits();
1504 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1505 {
1506 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1507
1508 bool clear_dies = cu->ExtractDIEsIfNeeded (false) > 1;
1509
Greg Claytonc685f8e2010-09-15 04:15:46 +00001510 cu->Index (cu_idx,
1511 m_function_basename_index,
1512 m_function_fullname_index,
1513 m_function_method_index,
1514 m_function_selector_index,
1515 m_global_index,
1516 m_types_index,
Greg Clayton016a95e2010-09-14 02:20:48 +00001517 DebugRanges(),
1518 m_aranges.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519
1520 // Keep memory down by clearing DIEs if this generate function
1521 // caused them to be parsed
1522 if (clear_dies)
1523 cu->ClearDIEs (true);
1524 }
1525
Greg Clayton016a95e2010-09-14 02:20:48 +00001526 m_aranges->Sort();
Greg Claytonc685f8e2010-09-15 04:15:46 +00001527
1528#if 0
1529 StreamFile s(stdout);
1530 s.Printf("DWARF index for '%s':", GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1531 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
1532 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
1533 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
1534 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
1535 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
1536 s.Printf("\nTypes:\n"); m_types_index.Dump (&s);
1537#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538 }
1539}
1540
1541uint32_t
1542SymbolFileDWARF::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
1543{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001544 DWARFDebugInfo* info = DebugInfo();
1545 if (info == NULL)
1546 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001547
1548 // If we aren't appending the results to this list, then clear the list
1549 if (!append)
1550 variables.Clear();
1551
1552 // Remember how many variables are in the list before we search in case
1553 // we are appending the results to a variable list.
1554 const uint32_t original_size = variables.GetSize();
1555
1556 // Index the DWARF if we haven't already
1557 if (!m_indexed)
1558 Index ();
1559
Greg Claytonc685f8e2010-09-15 04:15:46 +00001560 SymbolContext sc;
1561 sc.module_sp = m_obj_file->GetModule()->GetSP();
1562 assert (sc.module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563
Greg Claytonc685f8e2010-09-15 04:15:46 +00001564 DWARFCompileUnit* cu = NULL;
1565 DWARFCompileUnit* prev_cu = NULL;
1566 const DWARFDebugInfoEntry* die = NULL;
1567 std::vector<NameToDIE::Info> die_info_array;
1568 const size_t num_matches = m_global_index.Find(name, die_info_array);
1569 for (size_t i=0; i<num_matches; ++i, prev_cu = cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001571 cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
1572
1573 if (cu != prev_cu)
1574 cu->ExtractDIEsIfNeeded (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575
Greg Claytonc685f8e2010-09-15 04:15:46 +00001576 die = cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577
Greg Claytonc685f8e2010-09-15 04:15:46 +00001578 sc.comp_unit = GetCompUnitForDWARFCompUnit(cu, UINT32_MAX);
1579 assert(sc.comp_unit != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001580
Greg Claytonc685f8e2010-09-15 04:15:46 +00001581 ParseVariables(sc, cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
1582
1583 if (variables.GetSize() - original_size >= max_matches)
1584 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 }
1586
1587 // Return the number of variable that were appended to the list
1588 return variables.GetSize() - original_size;
1589}
1590
1591uint32_t
1592SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
1593{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001594 DWARFDebugInfo* info = DebugInfo();
1595 if (info == NULL)
1596 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001597
1598 // If we aren't appending the results to this list, then clear the list
1599 if (!append)
1600 variables.Clear();
1601
1602 // Remember how many variables are in the list before we search in case
1603 // we are appending the results to a variable list.
1604 const uint32_t original_size = variables.GetSize();
1605
1606 // Index the DWARF if we haven't already
1607 if (!m_indexed)
1608 Index ();
1609
Greg Claytonc685f8e2010-09-15 04:15:46 +00001610 SymbolContext sc;
1611 sc.module_sp = m_obj_file->GetModule()->GetSP();
1612 assert (sc.module_sp);
1613
1614 DWARFCompileUnit* cu = NULL;
1615 DWARFCompileUnit* prev_cu = NULL;
1616 const DWARFDebugInfoEntry* die = NULL;
1617 std::vector<NameToDIE::Info> die_info_array;
1618 const size_t num_matches = m_global_index.Find(regex, die_info_array);
1619 for (size_t i=0; i<num_matches; ++i, prev_cu = cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001621 cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
1622
1623 if (cu != prev_cu)
1624 cu->ExtractDIEsIfNeeded (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001625
Greg Claytonc685f8e2010-09-15 04:15:46 +00001626 die = cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627
Greg Claytonc685f8e2010-09-15 04:15:46 +00001628 sc.comp_unit = GetCompUnitForDWARFCompUnit(cu, UINT32_MAX);
1629 assert(sc.comp_unit != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630
Greg Claytonc685f8e2010-09-15 04:15:46 +00001631 ParseVariables(sc, cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632
Greg Claytonc685f8e2010-09-15 04:15:46 +00001633 if (variables.GetSize() - original_size >= max_matches)
1634 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 }
1636
1637 // Return the number of variable that were appended to the list
1638 return variables.GetSize() - original_size;
1639}
1640
1641
Greg Clayton0c5cd902010-06-28 21:30:43 +00001642void
1643SymbolFileDWARF::FindFunctions
1644(
1645 const ConstString &name,
Greg Claytonc685f8e2010-09-15 04:15:46 +00001646 const NameToDIE &name_to_die,
Greg Clayton0c5cd902010-06-28 21:30:43 +00001647 SymbolContextList& sc_list
1648)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001649{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001650 DWARFDebugInfo* info = DebugInfo();
1651 if (info == NULL)
1652 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653
Greg Claytonc685f8e2010-09-15 04:15:46 +00001654 SymbolContext sc;
1655 sc.module_sp = m_obj_file->GetModule()->GetSP();
1656 assert (sc.module_sp);
1657
1658 DWARFCompileUnit* cu = NULL;
1659 DWARFCompileUnit* prev_cu = NULL;
1660 const DWARFDebugInfoEntry* die = NULL;
1661 std::vector<NameToDIE::Info> die_info_array;
1662 const size_t num_matches = name_to_die.Find(name, die_info_array);
1663 for (size_t i=0; i<num_matches; ++i, prev_cu = cu)
1664 {
1665 cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
1666
1667 if (cu != prev_cu)
1668 cu->ExtractDIEsIfNeeded (false);
1669
1670 die = cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
1671 if (GetFunction (cu, die, sc))
1672 {
1673 // We found the function, so we should find the line table
1674 // and line table entry as well
1675 LineTable *line_table = sc.comp_unit->GetLineTable();
1676 if (line_table == NULL)
1677 {
1678 if (ParseCompileUnitLineTable(sc))
1679 line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00001681 if (line_table != NULL)
1682 line_table->FindLineEntryByAddress (sc.function->GetAddressRange().GetBaseAddress(), sc.line_entry);
1683
1684 sc_list.Append(sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685 }
1686 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00001687}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688
Greg Claytonc685f8e2010-09-15 04:15:46 +00001689
1690void
1691SymbolFileDWARF::FindFunctions
1692(
1693 const RegularExpression &regex,
1694 const NameToDIE &name_to_die,
1695 SymbolContextList& sc_list
1696)
1697{
1698 DWARFDebugInfo* info = DebugInfo();
1699 if (info == NULL)
1700 return;
1701
1702 SymbolContext sc;
1703 sc.module_sp = m_obj_file->GetModule()->GetSP();
1704 assert (sc.module_sp);
1705
1706 DWARFCompileUnit* cu = NULL;
1707 DWARFCompileUnit* prev_cu = NULL;
1708 const DWARFDebugInfoEntry* die = NULL;
1709 std::vector<NameToDIE::Info> die_info_array;
1710 const size_t num_matches = name_to_die.Find(regex, die_info_array);
1711 for (size_t i=0; i<num_matches; ++i, prev_cu = cu)
1712 {
1713 cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
1714
1715 if (cu != prev_cu)
1716 cu->ExtractDIEsIfNeeded (false);
1717
1718 die = cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
1719 if (GetFunction (cu, die, sc))
1720 {
1721 // We found the function, so we should find the line table
1722 // and line table entry as well
1723 LineTable *line_table = sc.comp_unit->GetLineTable();
1724 if (line_table == NULL)
1725 {
1726 if (ParseCompileUnitLineTable(sc))
1727 line_table = sc.comp_unit->GetLineTable();
1728 }
1729 if (line_table != NULL)
1730 line_table->FindLineEntryByAddress (sc.function->GetAddressRange().GetBaseAddress(), sc.line_entry);
1731
1732 sc_list.Append(sc);
1733 }
1734 }
Greg Clayton0c5cd902010-06-28 21:30:43 +00001735}
1736
1737uint32_t
1738SymbolFileDWARF::FindFunctions
1739(
1740 const ConstString &name,
1741 uint32_t name_type_mask,
1742 bool append,
1743 SymbolContextList& sc_list
1744)
1745{
1746 Timer scoped_timer (__PRETTY_FUNCTION__,
1747 "SymbolFileDWARF::FindFunctions (name = '%s')",
1748 name.AsCString());
1749
Greg Clayton0c5cd902010-06-28 21:30:43 +00001750 // If we aren't appending the results to this list, then clear the list
1751 if (!append)
1752 sc_list.Clear();
1753
1754 // Remember how many sc_list are in the list before we search in case
1755 // we are appending the results to a variable list.
1756 uint32_t original_size = sc_list.GetSize();
1757
1758 // Index the DWARF if we haven't already
1759 if (!m_indexed)
1760 Index ();
1761
1762 if (name_type_mask & eFunctionNameTypeBase)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001763 FindFunctions (name, m_function_basename_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00001764
1765 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001766 FindFunctions (name, m_function_fullname_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00001767
1768 if (name_type_mask & eFunctionNameTypeMethod)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001769 FindFunctions (name, m_function_method_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00001770
1771 if (name_type_mask & eFunctionNameTypeSelector)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001772 FindFunctions (name, m_function_selector_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00001773
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774 // Return the number of variable that were appended to the list
1775 return sc_list.GetSize() - original_size;
1776}
1777
1778
1779uint32_t
1780SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
1781{
1782 Timer scoped_timer (__PRETTY_FUNCTION__,
1783 "SymbolFileDWARF::FindFunctions (regex = '%s')",
1784 regex.GetText());
1785
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786 // If we aren't appending the results to this list, then clear the list
1787 if (!append)
1788 sc_list.Clear();
1789
1790 // Remember how many sc_list are in the list before we search in case
1791 // we are appending the results to a variable list.
1792 uint32_t original_size = sc_list.GetSize();
1793
1794 // Index the DWARF if we haven't already
1795 if (!m_indexed)
1796 Index ();
1797
Greg Claytonc685f8e2010-09-15 04:15:46 +00001798 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799
Greg Claytonc685f8e2010-09-15 04:15:46 +00001800 FindFunctions (regex, m_function_fullname_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001801
1802 // Return the number of variable that were appended to the list
1803 return sc_list.GetSize() - original_size;
1804}
1805
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001806uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001807SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, bool append, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001808{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001809 DWARFDebugInfo* info = DebugInfo();
1810 if (info == NULL)
1811 return 0;
1812
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813 // If we aren't appending the results to this list, then clear the list
1814 if (!append)
1815 types.Clear();
1816
Greg Clayton6dbd3982010-09-15 05:51:24 +00001817 // Index if we already haven't to make sure the compile units
1818 // get indexed and make their global DIE index list
1819 if (!m_indexed)
1820 Index ();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001821
Greg Claytonc685f8e2010-09-15 04:15:46 +00001822 const uint32_t initial_types_size = types.GetSize();
1823 DWARFCompileUnit* cu = NULL;
1824 DWARFCompileUnit* prev_cu = NULL;
1825 const DWARFDebugInfoEntry* die = NULL;
1826 std::vector<NameToDIE::Info> die_info_array;
1827 const size_t num_matches = m_types_index.Find (name, die_info_array);
1828 for (size_t i=0; i<num_matches; ++i, prev_cu = cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001829 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001830 cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
1831
1832 if (cu != prev_cu)
1833 cu->ExtractDIEsIfNeeded (false);
1834
1835 die = cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
1836
1837 Type *matching_type = ResolveType (cu, die);
1838 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001839 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001840 // We found a type pointer, now find the shared pointer form our type list
1841 TypeSP type_sp (m_obj_file->GetModule()->GetTypeList()->FindType(matching_type->GetID()));
1842 assert (type_sp.get() != NULL);
1843 types.InsertUnique (type_sp);
1844 if (types.GetSize() >= max_matches)
1845 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001846 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00001848 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849}
1850
1851
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001852uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001853SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854{
1855 // Remember how many sc_list are in the list before we search in case
1856 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001857 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858
1859 const uint32_t num_die_offsets = die_offsets.size();
1860 // Parse all of the types we found from the pubtypes matches
1861 uint32_t i;
1862 uint32_t num_matches = 0;
1863 for (i = 0; i < num_die_offsets; ++i)
1864 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001865 Type *matching_type = ResolveTypeUID (die_offsets[i]);
1866 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001868 // We found a type pointer, now find the shared pointer form our type list
1869 TypeSP type_sp (m_obj_file->GetModule()->GetTypeList()->FindType(matching_type->GetID()));
1870 assert (type_sp.get() != NULL);
1871 types.InsertUnique (type_sp);
1872 ++num_matches;
1873 if (num_matches >= max_matches)
1874 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 }
1876 }
1877
1878 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001879 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880}
1881
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882
1883size_t
1884SymbolFileDWARF::ParseChildParameters
1885(
1886 const SymbolContext& sc,
1887 TypeSP& type_sp,
Greg Clayton0fffff52010-09-24 05:15:53 +00001888 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001889 const DWARFDebugInfoEntry *parent_die,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001890 bool skip_artificial,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891 TypeList* type_list,
1892 std::vector<void *>& function_param_types,
1893 std::vector<clang::ParmVarDecl*>& function_param_decls
1894)
1895{
1896 if (parent_die == NULL)
1897 return 0;
1898
Greg Claytond88d7592010-09-15 08:33:30 +00001899 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1900
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901 size_t count = 0;
1902 const DWARFDebugInfoEntry *die;
1903 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1904 {
1905 dw_tag_t tag = die->Tag();
1906 switch (tag)
1907 {
1908 case DW_TAG_formal_parameter:
1909 {
1910 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00001911 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912 if (num_attributes > 0)
1913 {
1914 const char *name = NULL;
1915 Declaration decl;
1916 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00001917 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918 // one of None, Auto, Register, Extern, Static, PrivateExtern
1919
Sean Callanane2ef6e32010-09-23 03:01:22 +00001920 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001921 uint32_t i;
1922 for (i=0; i<num_attributes; ++i)
1923 {
1924 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1925 DWARFFormValue form_value;
1926 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1927 {
1928 switch (attr)
1929 {
1930 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1931 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1932 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1933 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1934 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00001935 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 case DW_AT_location:
1937 // if (form_value.BlockData())
1938 // {
1939 // const DataExtractor& debug_info_data = debug_info();
1940 // uint32_t block_length = form_value.Unsigned();
1941 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
1942 // }
1943 // else
1944 // {
1945 // }
1946 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947 case DW_AT_const_value:
1948 case DW_AT_default_value:
1949 case DW_AT_description:
1950 case DW_AT_endianity:
1951 case DW_AT_is_optional:
1952 case DW_AT_segment:
1953 case DW_AT_variable_parameter:
1954 default:
1955 case DW_AT_abstract_origin:
1956 case DW_AT_sibling:
1957 break;
1958 }
1959 }
1960 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00001961
Greg Clayton0fffff52010-09-24 05:15:53 +00001962 bool skip = false;
1963 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001964 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001965 if (is_artificial)
1966 skip = true;
1967 else
1968 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969
Greg Clayton0fffff52010-09-24 05:15:53 +00001970 // HACK: Objective C formal parameters "self" and "_cmd"
1971 // are not marked as artificial in the DWARF...
1972 CompileUnit *cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
1973 if (cu && (cu->GetLanguage() == eLanguageTypeObjC || cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
1974 {
1975 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
1976 skip = true;
1977 }
1978 }
1979 }
1980
1981 if (!skip)
1982 {
1983 Type *type = ResolveTypeUID(param_type_die_offset);
1984 if (type)
1985 {
1986 function_param_types.push_back (type->GetOpaqueClangQualType());
1987
1988 clang::ParmVarDecl *param_var_decl = type_list->GetClangASTContext().CreateParameterDeclaration (name, type->GetOpaqueClangQualType(), storage);
1989 assert(param_var_decl);
1990 function_param_decls.push_back(param_var_decl);
1991 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992 }
1993 }
1994 }
1995 break;
1996
1997 default:
1998 break;
1999 }
2000 }
2001 return count;
2002}
2003
2004size_t
2005SymbolFileDWARF::ParseChildEnumerators
2006(
2007 const SymbolContext& sc,
2008 TypeSP& type_sp,
2009 void * enumerator_qual_type,
2010 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002011 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012 const DWARFDebugInfoEntry *parent_die
2013)
2014{
2015 if (parent_die == NULL)
2016 return 0;
2017
2018 size_t enumerators_added = 0;
2019 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00002020 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2023 {
2024 const dw_tag_t tag = die->Tag();
2025 if (tag == DW_TAG_enumerator)
2026 {
2027 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002028 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029 if (num_child_attributes > 0)
2030 {
2031 const char *name = NULL;
2032 bool got_value = false;
2033 int64_t enum_value = 0;
2034 Declaration decl;
2035
2036 uint32_t i;
2037 for (i=0; i<num_child_attributes; ++i)
2038 {
2039 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2040 DWARFFormValue form_value;
2041 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2042 {
2043 switch (attr)
2044 {
2045 case DW_AT_const_value:
2046 got_value = true;
2047 enum_value = form_value.Unsigned();
2048 break;
2049
2050 case DW_AT_name:
2051 name = form_value.AsCString(&get_debug_str_data());
2052 break;
2053
2054 case DW_AT_description:
2055 default:
2056 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2057 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2058 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2059 case DW_AT_sibling:
2060 break;
2061 }
2062 }
2063 }
2064
2065 if (name && name[0] && got_value)
2066 {
2067 TypeList* type_list = m_obj_file->GetModule()->GetTypeList();
2068 type_list->GetClangASTContext().AddEnumerationValueToEnumerationType (type_sp->GetOpaqueClangQualType(), enumerator_qual_type, decl, name, enum_value, enumerator_byte_size * 8);
2069 ++enumerators_added;
2070 }
2071 }
2072 }
2073 }
2074 return enumerators_added;
2075}
2076
2077void
2078SymbolFileDWARF::ParseChildArrayInfo
2079(
2080 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00002081 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082 const DWARFDebugInfoEntry *parent_die,
2083 int64_t& first_index,
2084 std::vector<uint64_t>& element_orders,
2085 uint32_t& byte_stride,
2086 uint32_t& bit_stride
2087)
2088{
2089 if (parent_die == NULL)
2090 return;
2091
2092 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00002093 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2095 {
2096 const dw_tag_t tag = die->Tag();
2097 switch (tag)
2098 {
2099 case DW_TAG_enumerator:
2100 {
2101 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002102 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103 if (num_child_attributes > 0)
2104 {
2105 const char *name = NULL;
2106 bool got_value = false;
2107 int64_t enum_value = 0;
2108
2109 uint32_t i;
2110 for (i=0; i<num_child_attributes; ++i)
2111 {
2112 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2113 DWARFFormValue form_value;
2114 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2115 {
2116 switch (attr)
2117 {
2118 case DW_AT_const_value:
2119 got_value = true;
2120 enum_value = form_value.Unsigned();
2121 break;
2122
2123 case DW_AT_name:
2124 name = form_value.AsCString(&get_debug_str_data());
2125 break;
2126
2127 case DW_AT_description:
2128 default:
2129 case DW_AT_decl_file:
2130 case DW_AT_decl_line:
2131 case DW_AT_decl_column:
2132 case DW_AT_sibling:
2133 break;
2134 }
2135 }
2136 }
2137 }
2138 }
2139 break;
2140
2141 case DW_TAG_subrange_type:
2142 {
2143 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002144 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002145 if (num_child_attributes > 0)
2146 {
2147 const char *name = NULL;
2148 bool got_value = false;
2149 uint64_t byte_size = 0;
2150 int64_t enum_value = 0;
2151 uint64_t num_elements = 0;
2152 uint64_t lower_bound = 0;
2153 uint64_t upper_bound = 0;
2154 uint32_t i;
2155 for (i=0; i<num_child_attributes; ++i)
2156 {
2157 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2158 DWARFFormValue form_value;
2159 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2160 {
2161 switch (attr)
2162 {
2163 case DW_AT_const_value:
2164 got_value = true;
2165 enum_value = form_value.Unsigned();
2166 break;
2167
2168 case DW_AT_name:
2169 name = form_value.AsCString(&get_debug_str_data());
2170 break;
2171
2172 case DW_AT_count:
2173 num_elements = form_value.Unsigned();
2174 break;
2175
2176 case DW_AT_bit_stride:
2177 bit_stride = form_value.Unsigned();
2178 break;
2179
2180 case DW_AT_byte_stride:
2181 byte_stride = form_value.Unsigned();
2182 break;
2183
2184 case DW_AT_byte_size:
2185 byte_size = form_value.Unsigned();
2186 break;
2187
2188 case DW_AT_lower_bound:
2189 lower_bound = form_value.Unsigned();
2190 break;
2191
2192 case DW_AT_upper_bound:
2193 upper_bound = form_value.Unsigned();
2194 break;
2195
2196 default:
2197 //printf("0x%8.8x: %-30s skipping attribute at 0x%8.8x: %s\n", die->GetOffset(), DW_TAG_value_to_name(tag), attributes.die_offsets[i], DW_AT_value_to_name(attr)); // remove this, debug only
2198
2199 case DW_AT_abstract_origin:
2200 case DW_AT_accessibility:
2201 case DW_AT_allocated:
2202 case DW_AT_associated:
2203 case DW_AT_data_location:
2204 case DW_AT_declaration:
2205 case DW_AT_description:
2206 case DW_AT_sibling:
2207 case DW_AT_threads_scaled:
2208 case DW_AT_type:
2209 case DW_AT_visibility:
2210 break;
2211 }
2212 }
2213 }
2214
2215 if (upper_bound > lower_bound)
2216 num_elements = upper_bound - lower_bound + 1;
2217
2218 if (num_elements > 0)
2219 element_orders.push_back (num_elements);
2220 }
2221 }
2222 break;
2223 }
2224 }
2225}
2226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002227TypeSP
Greg Clayton594e5ed2010-09-27 21:07:38 +00002228SymbolFileDWARF::GetTypeForDIE
2229(
2230 DWARFCompileUnit *cu,
2231 const DWARFDebugInfoEntry* die,
2232 TypeSP& owning_type_sp,
2233 int32_t child_type,
2234 uint32_t idx
2235)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002236{
2237 TypeSP type_sp;
2238 if (die != NULL)
2239 {
2240 assert(cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00002241 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242 if (type_ptr == NULL)
2243 {
2244 SymbolContext sc(GetCompUnitForDWARFCompUnit(cu));
2245 bool type_is_new = false;
2246 type_sp = ParseType(sc, cu, die, type_is_new);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247 if (owning_type_sp.get() == NULL)
2248 owning_type_sp = type_sp;
2249 }
2250 else if (type_ptr != DIE_IS_BEING_PARSED)
2251 {
2252 // Grab the existing type from the master types lists
2253 type_sp = m_obj_file->GetModule()->GetTypeList()->FindType(type_ptr->GetID());
2254 }
2255
2256 }
2257 return type_sp;
2258}
2259
2260clang::DeclContext *
2261SymbolFileDWARF::GetClangDeclContextForDIEOffset (dw_offset_t die_offset)
2262{
2263 if (die_offset != DW_INVALID_OFFSET)
2264 {
2265 DWARFCompileUnitSP cu_sp;
2266 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
2267 return GetClangDeclContextForDIE (cu_sp.get(), die);
2268 }
2269 return NULL;
2270}
2271
2272
2273
2274clang::DeclContext *
Greg Clayton0fffff52010-09-24 05:15:53 +00002275SymbolFileDWARF::GetClangDeclContextForDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002276{
2277 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die);
2278 if (pos != m_die_to_decl_ctx.end())
2279 return pos->second;
2280
2281 while (die != NULL)
2282 {
2283 switch (die->Tag())
2284 {
2285 case DW_TAG_namespace:
2286 {
2287 const char *namespace_name = die->GetAttributeValueAsString(this, cu, DW_AT_name, NULL);
2288 if (namespace_name)
2289 {
2290 TypeList* type_list = m_obj_file->GetModule()->GetTypeList();
2291 assert(type_list);
2292 Declaration decl; // TODO: fill in the decl object
2293 clang::NamespaceDecl *namespace_decl = type_list->GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextForDIE (cu, die->GetParent()));
2294 if (namespace_decl)
2295 m_die_to_decl_ctx[die] = (clang::DeclContext*)namespace_decl;
2296 return namespace_decl;
2297 }
2298 }
2299 break;
2300
2301 default:
2302 break;
2303 }
2304 clang::DeclContext *decl_ctx;
2305 decl_ctx = GetClangDeclContextForDIEOffset (die->GetAttributeValueAsUnsigned(this, cu, DW_AT_specification, DW_INVALID_OFFSET));
2306 if (decl_ctx)
2307 return decl_ctx;
2308
2309 decl_ctx = GetClangDeclContextForDIEOffset (die->GetAttributeValueAsUnsigned(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET));
2310 if (decl_ctx)
2311 return decl_ctx;
2312
2313 die = die->GetParent();
2314 }
2315 return NULL;
2316}
2317
2318TypeSP
Greg Clayton0fffff52010-09-24 05:15:53 +00002319SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool &type_is_new)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002320{
2321 TypeSP type_sp;
2322
Sean Callananc7fbf732010-08-06 00:32:49 +00002323 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002324 if (die != NULL)
2325 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002326 Type *type_ptr = m_die_to_type.lookup (die);
2327 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002328 {
2329 type_is_new = true;
2330
Greg Clayton594e5ed2010-09-27 21:07:38 +00002331 const dw_tag_t tag = die->Tag();
2332
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333 bool is_forward_declaration = false;
2334 DWARFDebugInfoEntry::Attributes attributes;
2335 const char *type_name_cstr = NULL;
2336 ConstString type_name_dbstr;
2337 Type::EncodingUIDType encoding_uid_type = Type::eIsTypeWithUID;
2338 void *clang_type = NULL;
2339
2340 TypeList* type_list = m_obj_file->GetModule()->GetTypeList();
2341 dw_attr_t attr;
2342
2343 switch (tag)
2344 {
2345 case DW_TAG_base_type:
2346 case DW_TAG_pointer_type:
2347 case DW_TAG_reference_type:
2348 case DW_TAG_typedef:
2349 case DW_TAG_const_type:
2350 case DW_TAG_restrict_type:
2351 case DW_TAG_volatile_type:
2352 {
2353 //printf("0x%8.8x: %s (ParesTypes)\n", die->GetOffset(), DW_TAG_value_to_name(tag));
2354 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00002355 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356
Greg Claytond88d7592010-09-15 08:33:30 +00002357 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 Declaration decl;
2359 uint32_t encoding = 0;
2360 size_t byte_size = 0;
2361 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
2362
2363 if (num_attributes > 0)
2364 {
2365 uint32_t i;
2366 for (i=0; i<num_attributes; ++i)
2367 {
2368 attr = attributes.AttributeAtIndex(i);
2369 DWARFFormValue form_value;
2370 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2371 {
2372 switch (attr)
2373 {
2374 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2375 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2376 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2377 case DW_AT_name:
2378 type_name_cstr = form_value.AsCString(&get_debug_str_data());
2379 type_name_dbstr.SetCString(type_name_cstr);
2380 break;
2381 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
2382 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
2383 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
2384 default:
2385 case DW_AT_sibling:
2386 break;
2387 }
2388 }
2389 }
2390 }
2391
2392 switch (tag)
2393 {
2394 default:
2395 case DW_TAG_base_type:
2396 clang_type = type_list->GetClangASTContext().GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr, encoding, byte_size * 8);
2397 break;
2398
2399 case DW_TAG_pointer_type:
2400 // The encoding_uid will be embedded into the
2401 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2402 encoding_uid_type = Type::ePointerToTypeWithUID;
2403 break;
2404
2405 case DW_TAG_reference_type:
2406 // The encoding_uid will be embedded into the
2407 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2408 encoding_uid_type = Type::eLValueReferenceToTypeWithUID;
2409 break;
2410
2411 case DW_TAG_typedef:
2412 // The encoding_uid will be embedded into the
2413 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2414 encoding_uid_type = Type::eTypedefToTypeWithUID;
2415 break;
2416
2417 case DW_TAG_const_type:
2418 // The encoding_uid will be embedded into the
2419 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2420 encoding_uid_type = Type::eIsConstTypeWithUID; //ClangASTContext::AddConstModifier (clang_type);
2421 break;
2422
2423 case DW_TAG_restrict_type:
2424 // The encoding_uid will be embedded into the
2425 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2426 encoding_uid_type = Type::eIsRestrictTypeWithUID; //ClangASTContext::AddRestrictModifier (clang_type);
2427 break;
2428
2429 case DW_TAG_volatile_type:
2430 // The encoding_uid will be embedded into the
2431 // Type object and will be looked up when the Type::GetOpaqueClangQualType()
2432 encoding_uid_type = Type::eIsVolatileTypeWithUID; //ClangASTContext::AddVolatileModifier (clang_type);
2433 break;
2434 }
2435
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002436 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
2437 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
2438 {
2439 static ConstString g_objc_type_name_id("id");
2440 static ConstString g_objc_type_name_Class("Class");
2441 static ConstString g_objc_type_name_selector("SEL");
2442
2443 if (type_name_dbstr == g_objc_type_name_id)
2444 {
2445 clang_type = type_list->GetClangASTContext().GetBuiltInType_objc_id();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002446 }
2447 else if (type_name_dbstr == g_objc_type_name_Class)
2448 {
2449 clang_type = type_list->GetClangASTContext().GetBuiltInType_objc_Class();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002450 }
2451 else if (type_name_dbstr == g_objc_type_name_selector)
2452 {
2453 clang_type = type_list->GetClangASTContext().GetBuiltInType_objc_selector();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002454 }
2455 }
2456
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002457 type_sp.reset( new Type(die->GetOffset(), this, type_name_dbstr, byte_size, NULL, encoding_uid, encoding_uid_type, &decl, clang_type));
2458
Greg Clayton594e5ed2010-09-27 21:07:38 +00002459 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002460
2461// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
2462// if (encoding_type != NULL)
2463// {
2464// if (encoding_type != DIE_IS_BEING_PARSED)
2465// type_sp->SetEncodingType(encoding_type);
2466// else
2467// m_indirect_fixups.push_back(type_sp.get());
2468// }
2469 }
2470 break;
2471
2472 case DW_TAG_structure_type:
2473 case DW_TAG_union_type:
2474 case DW_TAG_class_type:
2475 {
2476 //printf("0x%8.8x: %s (ParesTypes)\n", die->GetOffset(), DW_TAG_value_to_name(tag));
2477 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00002478 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002479
2480 size_t byte_size = 0;
Greg Clayton9e409562010-07-28 02:04:09 +00002481 LanguageType class_language = eLanguageTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002482 //bool struct_is_class = false;
2483 Declaration decl;
Greg Claytond88d7592010-09-15 08:33:30 +00002484 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002485 if (num_attributes > 0)
2486 {
2487 uint32_t i;
2488 for (i=0; i<num_attributes; ++i)
2489 {
2490 attr = attributes.AttributeAtIndex(i);
2491 DWARFFormValue form_value;
2492 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2493 {
2494 switch (attr)
2495 {
Greg Clayton9e409562010-07-28 02:04:09 +00002496 case DW_AT_decl_file:
2497 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
2498 break;
2499
2500 case DW_AT_decl_line:
2501 decl.SetLine(form_value.Unsigned());
2502 break;
2503
2504 case DW_AT_decl_column:
2505 decl.SetColumn(form_value.Unsigned());
2506 break;
2507
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002508 case DW_AT_name:
2509 type_name_cstr = form_value.AsCString(&get_debug_str_data());
2510 type_name_dbstr.SetCString(type_name_cstr);
2511 break;
Greg Clayton9e409562010-07-28 02:04:09 +00002512
2513 case DW_AT_byte_size:
2514 byte_size = form_value.Unsigned();
2515 break;
2516
2517 case DW_AT_accessibility:
2518 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2519 break;
2520
2521 case DW_AT_declaration:
2522 is_forward_declaration = form_value.Unsigned() != 0;
2523 break;
2524
2525 case DW_AT_APPLE_runtime_class:
2526 class_language = (LanguageType)form_value.Signed();
2527 break;
2528
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002529 case DW_AT_allocated:
2530 case DW_AT_associated:
2531 case DW_AT_data_location:
2532 case DW_AT_description:
2533 case DW_AT_start_scope:
2534 case DW_AT_visibility:
2535 default:
2536 case DW_AT_sibling:
2537 break;
2538 }
2539 }
2540 }
2541 }
2542
2543 int tag_decl_kind = -1;
Sean Callananc7fbf732010-08-06 00:32:49 +00002544 AccessType default_accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002545 if (tag == DW_TAG_structure_type)
2546 {
2547 tag_decl_kind = clang::TTK_Struct;
Sean Callananc7fbf732010-08-06 00:32:49 +00002548 default_accessibility = eAccessPublic;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002549 }
2550 else if (tag == DW_TAG_union_type)
2551 {
2552 tag_decl_kind = clang::TTK_Union;
Sean Callananc7fbf732010-08-06 00:32:49 +00002553 default_accessibility = eAccessPublic;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002554 }
2555 else if (tag == DW_TAG_class_type)
2556 {
2557 tag_decl_kind = clang::TTK_Class;
Sean Callananc7fbf732010-08-06 00:32:49 +00002558 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002559 }
2560
2561 assert (tag_decl_kind != -1);
Greg Clayton9e409562010-07-28 02:04:09 +00002562 clang_type = type_list->GetClangASTContext().CreateRecordType (type_name_cstr, tag_decl_kind, GetClangDeclContextForDIE (dwarf_cu, die), class_language);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002563
2564 m_die_to_decl_ctx[die] = ClangASTContext::GetDeclContextForType (clang_type);
2565 type_sp.reset( new Type(die->GetOffset(), this, type_name_dbstr, byte_size, NULL, LLDB_INVALID_UID, Type::eIsTypeWithUID, &decl, clang_type));
2566
Greg Clayton594e5ed2010-09-27 21:07:38 +00002567 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002568
2569// assert(type_sp.get());
2570// if (accessibility)
2571// type_sp->SetAccess(accessibility);
2572//
2573 type_list->GetClangASTContext().StartTagDeclarationDefinition (clang_type);
2574 if (die->HasChildren())
2575 {
2576 std::vector<clang::CXXBaseSpecifier *> base_classes;
2577 std::vector<int> member_accessibilities;
2578 bool is_a_class = false;
Greg Clayton9e409562010-07-28 02:04:09 +00002579 ParseChildMembers (sc,
2580 type_sp,
2581 dwarf_cu,
2582 die,
2583 clang_type,
2584 class_language,
2585 base_classes,
2586 member_accessibilities,
2587 default_accessibility,
2588 is_a_class);
2589
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002590 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2591 // need to tell the clang type it is actually a class.
Greg Clayton9e409562010-07-28 02:04:09 +00002592 if (class_language != eLanguageTypeObjC)
2593 {
2594 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2595 type_list->GetClangASTContext().SetTagTypeKind (clang_type, clang::TTK_Class);
2596 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002597
2598 // Since DW_TAG_structure_type gets used for both classes
2599 // and structures, we may need to set any DW_TAG_member
2600 // fields to have a "private" access if none was specified.
2601 // When we parsed the child members we tracked that actual
2602 // accessibility value for each DW_TAG_member in the
2603 // "member_accessibilities" array. If the value for the
2604 // member is zero, then it was set to the "default_accessibility"
2605 // which for structs was "public". Below we correct this
2606 // by setting any fields to "private" that weren't correctly
2607 // set.
2608 if (is_a_class && !member_accessibilities.empty())
2609 {
2610 // This is a class and all members that didn't have
2611 // their access specified are private.
Sean Callananc7fbf732010-08-06 00:32:49 +00002612 type_list->GetClangASTContext().SetDefaultAccessForRecordFields (clang_type, eAccessPrivate, &member_accessibilities.front(), member_accessibilities.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002613 }
2614
2615 if (!base_classes.empty())
2616 {
Greg Clayton471b31c2010-07-20 22:52:08 +00002617 type_list->GetClangASTContext().SetBaseClassesForClassType (clang_type, &base_classes.front(), base_classes.size());
2618
2619 // Clang will copy each CXXBaseSpecifier in "base_classes"
2620 // so we have to free them all.
2621 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(), base_classes.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002622 }
Greg Clayton0b42ac32010-07-02 01:29:13 +00002623
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002624 }
2625 type_list->GetClangASTContext().CompleteTagDeclarationDefinition (clang_type);
2626 }
2627 break;
2628
2629 case DW_TAG_enumeration_type:
2630 {
2631 //printf("0x%8.8x: %s (ParesTypes)\n", die->GetOffset(), DW_TAG_value_to_name(tag));
2632 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00002633 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002634
2635 size_t byte_size = 0;
2636 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
2637 Declaration decl;
2638
Greg Claytond88d7592010-09-15 08:33:30 +00002639 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002640 if (num_attributes > 0)
2641 {
2642 uint32_t i;
2643
2644 for (i=0; i<num_attributes; ++i)
2645 {
2646 attr = attributes.AttributeAtIndex(i);
2647 DWARFFormValue form_value;
2648 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2649 {
2650 switch (attr)
2651 {
2652 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2653 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2654 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2655 case DW_AT_name:
2656 type_name_cstr = form_value.AsCString(&get_debug_str_data());
2657 type_name_dbstr.SetCString(type_name_cstr);
2658 break;
2659 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
2660 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00002661 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002662 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
2663 case DW_AT_allocated:
2664 case DW_AT_associated:
2665 case DW_AT_bit_stride:
2666 case DW_AT_byte_stride:
2667 case DW_AT_data_location:
2668 case DW_AT_description:
2669 case DW_AT_start_scope:
2670 case DW_AT_visibility:
2671 case DW_AT_specification:
2672 case DW_AT_abstract_origin:
2673 case DW_AT_sibling:
2674 break;
2675 }
2676 }
2677 }
Greg Clayton83ff3892010-09-12 23:17:56 +00002678
2679 void *enumerator_qual_type = type_list->GetClangASTContext().GetBuiltinTypeForDWARFEncodingAndBitSize (NULL, DW_ATE_signed, byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002680
Greg Clayton83ff3892010-09-12 23:17:56 +00002681 clang_type = type_list->GetClangASTContext().CreateEnumerationType(decl, type_name_cstr, enumerator_qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002682 m_die_to_decl_ctx[die] = ClangASTContext::GetDeclContextForType (clang_type);
2683 type_sp.reset( new Type(die->GetOffset(), this, type_name_dbstr, byte_size, NULL, encoding_uid, Type::eIsTypeWithUID, &decl, clang_type));
2684
Greg Clayton594e5ed2010-09-27 21:07:38 +00002685 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002686
2687 if (die->HasChildren())
2688 {
2689 type_list->GetClangASTContext().StartTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002690 ParseChildEnumerators(sc, type_sp, enumerator_qual_type, byte_size, dwarf_cu, die);
2691 type_list->GetClangASTContext().CompleteTagDeclarationDefinition (clang_type);
2692 }
2693 }
2694 }
2695 break;
2696
Jim Inghamb0be4422010-08-12 01:20:14 +00002697 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002698 case DW_TAG_subprogram:
2699 case DW_TAG_subroutine_type:
2700 {
2701 //printf("0x%8.8x: %s (ParesTypes)\n", die->GetOffset(), DW_TAG_value_to_name(tag));
2702 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00002703 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002704
2705 const char *mangled = NULL;
2706 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
2707 Declaration decl;
Greg Claytona51ed9b2010-09-23 01:09:21 +00002708 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002709 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00002710 bool is_static = false;
2711 bool is_virtual = false;
2712
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002713 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00002714 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002715
2716
Greg Claytond88d7592010-09-15 08:33:30 +00002717 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002718 if (num_attributes > 0)
2719 {
2720 uint32_t i;
2721 for (i=0; i<num_attributes; ++i)
2722 {
Greg Clayton0fffff52010-09-24 05:15:53 +00002723 const dw_attr_t attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002724 DWARFFormValue form_value;
2725 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2726 {
2727 switch (attr)
2728 {
2729 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2730 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2731 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2732 case DW_AT_name:
2733 type_name_cstr = form_value.AsCString(&get_debug_str_data());
2734 type_name_dbstr.SetCString(type_name_cstr);
2735 break;
2736
2737 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
2738 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00002739 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002740 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00002741 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
2742 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002743 case DW_AT_external:
2744 if (form_value.Unsigned())
2745 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00002746 if (storage == clang::SC_None)
2747 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002748 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00002749 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002750 }
2751 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002752
2753 case DW_AT_allocated:
2754 case DW_AT_associated:
2755 case DW_AT_address_class:
2756 case DW_AT_artificial:
2757 case DW_AT_calling_convention:
2758 case DW_AT_data_location:
2759 case DW_AT_elemental:
2760 case DW_AT_entry_pc:
2761 case DW_AT_explicit:
2762 case DW_AT_frame_base:
2763 case DW_AT_high_pc:
2764 case DW_AT_low_pc:
2765 case DW_AT_object_pointer:
2766 case DW_AT_prototyped:
2767 case DW_AT_pure:
2768 case DW_AT_ranges:
2769 case DW_AT_recursive:
2770 case DW_AT_return_addr:
2771 case DW_AT_segment:
2772 case DW_AT_specification:
2773 case DW_AT_start_scope:
2774 case DW_AT_static_link:
2775 case DW_AT_trampoline:
2776 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002777 case DW_AT_vtable_elem_location:
2778 case DW_AT_abstract_origin:
2779 case DW_AT_description:
2780 case DW_AT_sibling:
2781 break;
2782 }
2783 }
2784 }
2785
2786 void *return_clang_type = NULL;
2787 Type *func_type = ResolveTypeUID(type_die_offset);
2788 if (func_type)
2789 return_clang_type = func_type->GetOpaqueClangQualType();
2790 else
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002791 return_clang_type = type_list->GetClangASTContext().GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002792
2793 std::vector<void *> function_param_types;
2794 std::vector<clang::ParmVarDecl*> function_param_decls;
2795
2796 // Parse the function children for the parameters
Greg Clayton0fffff52010-09-24 05:15:53 +00002797 bool skip_artificial = true;
Greg Clayton594e5ed2010-09-27 21:07:38 +00002798 if (die->GetOffset() == 1340212) // REMOVE THIS BEFORE CHECKIN
2799 { // REMOVE THIS BEFORE CHECKIN
2800 printf("this one!\n"); // REMOVE THIS BEFORE CHECKIN
2801 } // REMOVE THIS BEFORE CHECKIN
Greg Clayton0fffff52010-09-24 05:15:53 +00002802 ParseChildParameters (sc, type_sp, dwarf_cu, die, skip_artificial, type_list, function_param_types, function_param_decls);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002803
Greg Clayton0fffff52010-09-24 05:15:53 +00002804 // clang_type will get the function prototype clang type after this call
Greg Claytona51ed9b2010-09-23 01:09:21 +00002805 clang_type = type_list->GetClangASTContext().CreateFunctionType (return_clang_type, &function_param_types[0], function_param_types.size(), is_variadic, type_quals);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002806 if (type_name_cstr)
2807 {
Greg Clayton0fffff52010-09-24 05:15:53 +00002808 bool type_handled = false;
2809 const DWARFDebugInfoEntry *parent_die = die->GetParent();
2810 if (tag == DW_TAG_subprogram)
2811 {
2812 if (type_name_cstr[1] == '[' && (type_name_cstr[0] == '-' || type_name_cstr[0] == '+'))
2813 {
2814 // We need to find the DW_TAG_class_type or
2815 // DW_TAG_struct_type by name so we can add this
2816 // as a member function of the class.
2817 const char *class_name_start = type_name_cstr + 2;
2818 const char *class_name_end = ::strchr (class_name_start, ' ');
2819 SymbolContext empty_sc;
2820 void *class_opaque_type = NULL;
2821 if (class_name_start < class_name_end)
2822 {
2823 ConstString class_name (class_name_start, class_name_end - class_name_start);
2824 TypeList types;
2825 const uint32_t match_count = FindTypes (empty_sc, class_name, true, UINT32_MAX, types);
2826 if (match_count > 0)
2827 {
2828 for (uint32_t i=0; i<match_count; ++i)
2829 {
2830 Type *type = types.GetTypeAtIndex (i).get();
2831 if (ClangASTContext::IsObjCClassType (type->GetOpaqueClangQualType()))
2832 {
2833 class_opaque_type = type->GetOpaqueClangQualType();
2834 break;
2835 }
2836 }
2837 }
2838 }
2839
2840 if (class_opaque_type)
2841 {
2842 clang::ObjCMethodDecl *objc_method_decl;
2843 objc_method_decl = type_list->GetClangASTContext().AddMethodToObjCObjectType (class_opaque_type,
2844 type_name_cstr,
2845 clang_type,
2846 accessibility);
2847 type_handled = objc_method_decl != NULL;
2848 }
2849 }
2850 else if (parent_die->Tag() == DW_TAG_class_type ||
2851 parent_die->Tag() == DW_TAG_structure_type)
2852 {
2853 // Look at the parent of this DIE and see if is is
2854 // a class or struct and see if this is actually a
2855 // C++ method
2856 Type *class_type = ResolveType (dwarf_cu, parent_die);
2857 if (class_type)
2858 {
2859 void *class_opaque_type = class_type->GetOpaqueClangQualType ();
2860 if (ClangASTContext::IsCXXClassType (class_opaque_type))
2861 {
2862 clang::CXXMethodDecl *cxx_method_decl;
2863 cxx_method_decl = type_list->GetClangASTContext().AddMethodToCXXRecordType (class_opaque_type,
2864 type_name_cstr,
2865 clang_type,
2866 accessibility,
2867 is_virtual,
2868 is_static,
2869 is_inline);
2870 type_handled = cxx_method_decl != NULL;
2871 }
2872 }
2873 }
2874 }
2875
2876 if (!type_handled)
2877 {
2878 // We just have a function that isn't part of a class
2879 clang::FunctionDecl *function_decl = type_list->GetClangASTContext().CreateFunctionDeclaration (type_name_cstr, clang_type, storage, is_inline);
2880
2881 // Add the decl to our DIE to decl context map
2882 assert (function_decl);
2883 m_die_to_decl_ctx[die] = function_decl;
2884 if (!function_param_decls.empty())
2885 type_list->GetClangASTContext().SetFunctionParameters (function_decl, &function_param_decls.front(), function_param_decls.size());
2886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002887 }
2888 type_sp.reset( new Type(die->GetOffset(), this, type_name_dbstr, 0, NULL, LLDB_INVALID_UID, Type::eIsTypeWithUID, &decl, clang_type));
2889
Greg Clayton594e5ed2010-09-27 21:07:38 +00002890 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002891 assert(type_sp.get());
2892 }
2893 }
2894 break;
2895
2896 case DW_TAG_array_type:
2897 {
2898 //printf("0x%8.8x: %s (ParesTypes)\n", die->GetOffset(), DW_TAG_value_to_name(tag));
2899 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00002900 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002901
2902 size_t byte_size = 0;
2903 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
2904 Declaration decl;
2905 int64_t first_index = 0;
2906 uint32_t byte_stride = 0;
2907 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00002908 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002909
2910 if (num_attributes > 0)
2911 {
2912 uint32_t i;
2913 for (i=0; i<num_attributes; ++i)
2914 {
2915 attr = attributes.AttributeAtIndex(i);
2916 DWARFFormValue form_value;
2917 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2918 {
2919 switch (attr)
2920 {
2921 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2922 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2923 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2924 case DW_AT_name:
2925 type_name_cstr = form_value.AsCString(&get_debug_str_data());
2926 type_name_dbstr.SetCString(type_name_cstr);
2927 break;
2928
2929 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
2930 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
2931 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
2932 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00002933 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002934 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
2935 case DW_AT_allocated:
2936 case DW_AT_associated:
2937 case DW_AT_data_location:
2938 case DW_AT_description:
2939 case DW_AT_ordering:
2940 case DW_AT_start_scope:
2941 case DW_AT_visibility:
2942 case DW_AT_specification:
2943 case DW_AT_abstract_origin:
2944 case DW_AT_sibling:
2945 break;
2946 }
2947 }
2948 }
2949
2950 Type *element_type = ResolveTypeUID(type_die_offset);
2951
2952 if (element_type)
2953 {
2954 std::vector<uint64_t> element_orders;
2955 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00002956 // We have an array that claims to have no members, lets give it at least one member...
2957 if (element_orders.empty())
2958 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002959 if (byte_stride == 0 && bit_stride == 0)
2960 byte_stride = element_type->GetByteSize();
2961 void *array_element_type = element_type->GetOpaqueClangQualType();
2962 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
2963 uint64_t num_elements = 0;
2964 std::vector<uint64_t>::const_reverse_iterator pos;
2965 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
2966 for (pos = element_orders.rbegin(); pos != end; ++pos)
2967 {
2968 num_elements = *pos;
2969 clang_type = type_list->GetClangASTContext().CreateArrayType (array_element_type, num_elements, num_elements * array_element_bit_stride);
2970 array_element_type = clang_type;
2971 array_element_bit_stride = array_element_bit_stride * num_elements;
2972 }
2973 ConstString empty_name;
2974 type_sp.reset( new Type(die->GetOffset(), this, empty_name, array_element_bit_stride / 8, NULL, LLDB_INVALID_UID, Type::eIsTypeWithUID, &decl, clang_type));
Greg Clayton594e5ed2010-09-27 21:07:38 +00002975 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002976 }
2977 }
2978 }
2979 break;
2980
Greg Clayton9b81a312010-06-12 01:20:30 +00002981 case DW_TAG_ptr_to_member_type:
2982 {
2983 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
2984 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
2985
Greg Claytond88d7592010-09-15 08:33:30 +00002986 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00002987
2988 if (num_attributes > 0) {
2989 uint32_t i;
2990 for (i=0; i<num_attributes; ++i)
2991 {
2992 attr = attributes.AttributeAtIndex(i);
2993 DWARFFormValue form_value;
2994 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2995 {
2996 switch (attr)
2997 {
2998 case DW_AT_type:
2999 type_die_offset = form_value.Reference(dwarf_cu); break;
3000 case DW_AT_containing_type:
3001 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
3002 }
3003 }
3004 }
3005
3006 Type *pointee_type = ResolveTypeUID(type_die_offset);
3007 Type *class_type = ResolveTypeUID(containing_type_die_offset);
3008
3009 void *pointee_clang_type = pointee_type->GetOpaqueClangQualType();
Greg Clayton1ba811d2010-06-12 15:33:14 +00003010 void *class_clang_type = class_type->GetOpaqueClangQualType();
Greg Clayton9b81a312010-06-12 01:20:30 +00003011
Greg Claytonb1320972010-07-14 00:18:15 +00003012 clang_type = type_list->GetClangASTContext().CreateMemberPointerType(pointee_clang_type, class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00003013
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003014 size_t byte_size = ClangASTType::GetClangTypeBitWidth (type_list->GetClangASTContext().getASTContext(), clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00003015
3016 type_sp.reset( new Type(die->GetOffset(), this, type_name_dbstr, byte_size, NULL, LLDB_INVALID_UID, Type::eIsTypeWithUID, NULL, clang_type));
Greg Clayton594e5ed2010-09-27 21:07:38 +00003017 m_die_to_type[die] = type_sp.get();
Greg Clayton9b81a312010-06-12 01:20:30 +00003018 }
3019
3020 break;
3021 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003022 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00003023 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003024 break;
3025 }
3026
3027 if (type_sp.get())
3028 {
3029 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
3030 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
3031
3032 SymbolContextScope * symbol_context_scope = NULL;
3033 if (sc_parent_tag == DW_TAG_compile_unit)
3034 {
3035 symbol_context_scope = sc.comp_unit;
3036 }
3037 else if (sc.function != NULL)
3038 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003039 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040 if (symbol_context_scope == NULL)
3041 symbol_context_scope = sc.function;
3042 }
3043
3044 if (symbol_context_scope != NULL)
3045 {
3046 type_sp->SetSymbolContextScope(symbol_context_scope);
3047 }
3048
3049// if (udt_sp.get())
3050// {
3051// if (is_forward_declaration)
3052// udt_sp->GetFlags().Set(UserDefType::flagIsForwardDefinition);
3053// type_sp->SetUserDefinedType(udt_sp);
3054// }
3055
3056 if (type_sp.unique())
3057 {
3058 // We are ready to put this type into the uniqued list up at the module level
3059 TypeSP uniqued_type_sp(m_obj_file->GetModule()->GetTypeList()->InsertUnique(type_sp));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060 type_sp = uniqued_type_sp;
Greg Clayton594e5ed2010-09-27 21:07:38 +00003061 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003062 }
3063 }
3064 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00003065 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00003067 type_sp = m_obj_file->GetModule()->GetTypeList()->FindType(type_ptr->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 }
3069 }
3070 return type_sp;
3071}
3072
3073size_t
Greg Clayton0fffff52010-09-24 05:15:53 +00003074SymbolFileDWARF::ParseTypes (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool parse_siblings, bool parse_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075{
3076 size_t types_added = 0;
3077 while (die != NULL)
3078 {
3079 bool type_is_new = false;
3080 if (ParseType(sc, dwarf_cu, die, type_is_new).get())
3081 {
3082 if (type_is_new)
3083 ++types_added;
3084 }
3085
3086 if (parse_children && die->HasChildren())
3087 {
3088 if (die->Tag() == DW_TAG_subprogram)
3089 {
3090 SymbolContext child_sc(sc);
3091 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get();
3092 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
3093 }
3094 else
3095 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
3096 }
3097
3098 if (parse_siblings)
3099 die = die->GetSibling();
3100 else
3101 die = NULL;
3102 }
3103 return types_added;
3104}
3105
3106
3107size_t
3108SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3109{
3110 assert(sc.comp_unit && sc.function);
3111 size_t functions_added = 0;
3112 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
3113 if (dwarf_cu)
3114 {
3115 dw_offset_t function_die_offset = sc.function->GetID();
3116 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
3117 if (function_die)
3118 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003119 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, false, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003120 }
3121 }
3122
3123 return functions_added;
3124}
3125
3126
3127size_t
3128SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3129{
3130 // At least a compile unit must be valid
3131 assert(sc.comp_unit);
3132 size_t types_added = 0;
3133 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
3134 if (dwarf_cu)
3135 {
3136 if (sc.function)
3137 {
3138 dw_offset_t function_die_offset = sc.function->GetID();
3139 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
3140 if (func_die && func_die->HasChildren())
3141 {
3142 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
3143 }
3144 }
3145 else
3146 {
3147 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
3148 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
3149 {
3150 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
3151 }
3152 }
3153 }
3154
3155 return types_added;
3156}
3157
3158size_t
3159SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3160{
3161 if (sc.comp_unit != NULL)
3162 {
3163 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
3164
3165 if (dwarf_cu == NULL)
3166 return 0;
3167
3168 if (sc.function)
3169 {
3170 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00003171
3172 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
3173 assert (func_lo_pc != DW_INVALID_ADDRESS);
3174
3175 return ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003176 }
3177 else if (sc.comp_unit)
3178 {
3179 uint32_t vars_added = 0;
3180 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3181
3182 if (variables.get() == NULL)
3183 {
3184 variables.reset(new VariableList());
3185 sc.comp_unit->SetVariableList(variables);
3186
3187 // Index if we already haven't to make sure the compile units
3188 // get indexed and make their global DIE index list
3189 if (!m_indexed)
3190 Index ();
3191
Greg Claytonc685f8e2010-09-15 04:15:46 +00003192
3193 std::vector<NameToDIE::Info> global_die_info_array;
3194 const size_t num_globals = m_global_index.FindAllEntriesForCompileUnitWithIndex (sc.comp_unit->GetID(), global_die_info_array);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003195 for (size_t idx=0; idx<num_globals; ++idx)
3196 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00003197 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, dwarf_cu->GetDIEAtIndexUnchecked(global_die_info_array[idx].die_idx), LLDB_INVALID_ADDRESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003198 if (var_sp)
3199 {
3200 variables->AddVariable(var_sp);
3201 ++vars_added;
3202 }
3203 }
3204 }
3205 return vars_added;
3206 }
3207 }
3208 return 0;
3209}
3210
3211
3212VariableSP
3213SymbolFileDWARF::ParseVariableDIE
3214(
3215 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003216 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00003217 const DWARFDebugInfoEntry *die,
3218 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003219)
3220{
3221
3222 VariableSP var_sp;
3223
3224 const dw_tag_t tag = die->Tag();
3225 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003226 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003227 if (num_attributes > 0)
3228 {
3229 const char *name = NULL;
Greg Claytona134cc12010-09-13 02:37:44 +00003230 const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003231 Declaration decl;
3232 uint32_t i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003233 Type *var_type = NULL;
3234 DWARFExpression location;
3235 bool is_external = false;
3236 bool is_artificial = false;
Sean Callananc7fbf732010-08-06 00:32:49 +00003237 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003238
3239 for (i=0; i<num_attributes; ++i)
3240 {
3241 dw_attr_t attr = attributes.AttributeAtIndex(i);
3242 DWARFFormValue form_value;
3243 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3244 {
3245 switch (attr)
3246 {
3247 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3248 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3249 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3250 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytona134cc12010-09-13 02:37:44 +00003251 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton594e5ed2010-09-27 21:07:38 +00003252 case DW_AT_type: var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003253 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
3254 case DW_AT_location:
3255 {
3256 if (form_value.BlockData())
3257 {
3258 const DataExtractor& debug_info_data = get_debug_info_data();
3259
3260 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3261 uint32_t block_length = form_value.Unsigned();
Greg Clayton016a95e2010-09-14 02:20:48 +00003262 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003263 }
3264 else
3265 {
3266 const DataExtractor& debug_loc_data = get_debug_loc_data();
3267 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3268
3269 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
3270 if (loc_list_length > 0)
3271 {
Greg Clayton016a95e2010-09-14 02:20:48 +00003272 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
3273 assert (func_low_pc != LLDB_INVALID_ADDRESS);
3274 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003275 }
3276 }
3277 }
3278 break;
3279
3280 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton8cf05932010-07-22 18:30:50 +00003281 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003282 case DW_AT_const_value:
3283 case DW_AT_declaration:
3284 case DW_AT_description:
3285 case DW_AT_endianity:
3286 case DW_AT_segment:
3287 case DW_AT_start_scope:
3288 case DW_AT_visibility:
3289 default:
3290 case DW_AT_abstract_origin:
3291 case DW_AT_sibling:
3292 case DW_AT_specification:
3293 break;
3294 }
3295 }
3296 }
3297
3298 if (location.IsValid())
3299 {
3300 assert(var_type != DIE_IS_BEING_PARSED);
3301
Greg Claytona134cc12010-09-13 02:37:44 +00003302 ConstString var_name;
3303 if (mangled)
3304 {
3305 Mangled mangled_var_name (mangled, true);
3306 var_name = mangled_var_name.GetDemangledName();
3307 }
3308
3309 if (!var_name && name)
3310 {
3311 var_name.SetCString(name);
3312 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003313
3314 ValueType scope = eValueTypeInvalid;
3315
3316 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
3317 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
3318
3319 if (tag == DW_TAG_formal_parameter)
3320 scope = eValueTypeVariableArgument;
3321 else if (is_external || parent_tag == DW_TAG_compile_unit)
3322 scope = eValueTypeVariableGlobal;
3323 else
3324 scope = eValueTypeVariableLocal;
3325
3326 SymbolContextScope * symbol_context_scope = NULL;
3327 if (parent_tag == DW_TAG_compile_unit)
3328 {
3329 symbol_context_scope = sc.comp_unit;
3330 }
3331 else if (sc.function != NULL)
3332 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003333 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003334 if (symbol_context_scope == NULL)
3335 symbol_context_scope = sc.function;
3336 }
3337
3338 assert(symbol_context_scope != NULL);
3339 var_sp.reset (new Variable(die->GetOffset(),
3340 var_name,
3341 var_type,
3342 scope,
3343 symbol_context_scope,
3344 &decl,
3345 location,
3346 is_external,
3347 is_artificial));
Greg Clayton594e5ed2010-09-27 21:07:38 +00003348
3349 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003350 }
3351 }
3352 return var_sp;
3353}
3354
3355size_t
3356SymbolFileDWARF::ParseVariables
3357(
3358 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003359 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00003360 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003361 const DWARFDebugInfoEntry *orig_die,
3362 bool parse_siblings,
3363 bool parse_children,
3364 VariableList* cc_variable_list
3365)
3366{
3367 if (orig_die == NULL)
3368 return 0;
3369
3370 size_t vars_added = 0;
3371 const DWARFDebugInfoEntry *die = orig_die;
3372 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
3373 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
3374 VariableListSP variables;
3375 switch (parent_tag)
3376 {
3377 case DW_TAG_compile_unit:
3378 if (sc.comp_unit != NULL)
3379 {
3380 variables = sc.comp_unit->GetVariableList(false);
3381 if (variables.get() == NULL)
3382 {
3383 variables.reset(new VariableList());
3384 sc.comp_unit->SetVariableList(variables);
3385 }
3386 }
3387 else
3388 {
3389 assert(!"Parent DIE was a compile unit, yet we don't have a valid compile unit in the symbol context...");
3390 vars_added = 0;
3391 }
3392 break;
3393
3394 case DW_TAG_subprogram:
3395 case DW_TAG_inlined_subroutine:
3396 case DW_TAG_lexical_block:
3397 if (sc.function != NULL)
3398 {
3399 // Check to see if we already have parsed the variables for the given scope
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003400
3401 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
3402 assert (block != NULL);
3403 variables = block->GetVariableList(false, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003404 if (variables.get() == NULL)
3405 {
3406 variables.reset(new VariableList());
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003407 block->SetVariableList(variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003408 }
3409 }
3410 else
3411 {
3412 assert(!"Parent DIE was a function or block, yet we don't have a function in the symbol context...");
3413 vars_added = 0;
3414 }
3415 break;
3416
3417 default:
3418 assert(!"Didn't find appropriate parent DIE for variable list...");
3419 break;
3420 }
3421
3422 // We need to have a variable list at this point that we can add variables to
3423 assert(variables.get());
3424
3425 while (die != NULL)
3426 {
3427 dw_tag_t tag = die->Tag();
3428
3429 // Check to see if we have already parsed this variable or constant?
Greg Clayton594e5ed2010-09-27 21:07:38 +00003430 if (m_die_to_variable_sp[die].get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003431 {
3432 // We haven't already parsed it, lets do that now.
3433 if ((tag == DW_TAG_variable) ||
3434 (tag == DW_TAG_constant) ||
3435 (tag == DW_TAG_formal_parameter && sc.function))
3436 {
Greg Clayton016a95e2010-09-14 02:20:48 +00003437 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003438 if (var_sp)
3439 {
3440 variables->AddVariable(var_sp);
3441 ++vars_added;
3442 }
3443 }
3444 }
3445
3446 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3447
3448 if (!skip_children && parse_children && die->HasChildren())
3449 {
Greg Clayton016a95e2010-09-14 02:20:48 +00003450 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003451 //vars_added += ParseVariables(sc, dwarf_cu, die->GetFirstChild(), parse_siblings, parse_children);
3452 }
3453
3454 if (parse_siblings)
3455 die = die->GetSibling();
3456 else
3457 die = NULL;
3458 }
3459
3460 if (cc_variable_list)
3461 {
3462 cc_variable_list->AddVariables(variables.get());
3463 }
3464
3465 return vars_added;
3466}
3467
3468//------------------------------------------------------------------
3469// PluginInterface protocol
3470//------------------------------------------------------------------
3471const char *
3472SymbolFileDWARF::GetPluginName()
3473{
3474 return "SymbolFileDWARF";
3475}
3476
3477const char *
3478SymbolFileDWARF::GetShortPluginName()
3479{
3480 return GetPluginNameStatic();
3481}
3482
3483uint32_t
3484SymbolFileDWARF::GetPluginVersion()
3485{
3486 return 1;
3487}
3488
3489void
3490SymbolFileDWARF::GetPluginCommandHelp (const char *command, Stream *strm)
3491{
3492}
3493
3494Error
3495SymbolFileDWARF::ExecutePluginCommand (Args &command, Stream *strm)
3496{
3497 Error error;
3498 error.SetErrorString("No plug-in command are currently supported.");
3499 return error;
3500}
3501
3502Log *
3503SymbolFileDWARF::EnablePluginLogging (Stream *strm, Args &command)
3504{
3505 return NULL;
3506}
3507