1 //===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolFileDWARF.h"
12 // Other libraries and framework includes
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/Threading.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/ModuleList.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Scalar.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Core/Value.h"
24 #include "lldb/Utility/ArchSpec.h"
25 #include "lldb/Utility/RegularExpression.h"
26 #include "lldb/Utility/StreamString.h"
27 #include "lldb/Utility/Timer.h"
29 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
30 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
32 #include "lldb/Host/FileSystem.h"
33 #include "lldb/Host/Host.h"
34 #include "lldb/Host/Symbols.h"
36 #include "lldb/Interpreter/OptionValueFileSpecList.h"
37 #include "lldb/Interpreter/OptionValueProperties.h"
39 #include "lldb/Symbol/Block.h"
40 #include "lldb/Symbol/ClangASTContext.h"
41 #include "lldb/Symbol/ClangUtil.h"
42 #include "lldb/Symbol/CompileUnit.h"
43 #include "lldb/Symbol/CompilerDecl.h"
44 #include "lldb/Symbol/CompilerDeclContext.h"
45 #include "lldb/Symbol/DebugMacros.h"
46 #include "lldb/Symbol/LineTable.h"
47 #include "lldb/Symbol/ObjectFile.h"
48 #include "lldb/Symbol/SymbolVendor.h"
49 #include "lldb/Symbol/TypeMap.h"
50 #include "lldb/Symbol/TypeSystem.h"
51 #include "lldb/Symbol/VariableList.h"
53 #include "lldb/Target/Language.h"
55 #include "AppleDWARFIndex.h"
56 #include "DWARFASTParser.h"
57 #include "DWARFASTParserClang.h"
58 #include "DWARFDIECollection.h"
59 #include "DWARFDebugAbbrev.h"
60 #include "DWARFDebugAranges.h"
61 #include "DWARFDebugInfo.h"
62 #include "DWARFDebugLine.h"
63 #include "DWARFDebugMacro.h"
64 #include "DWARFDebugRanges.h"
65 #include "DWARFDeclContext.h"
66 #include "DWARFFormValue.h"
67 #include "DWARFUnit.h"
68 #include "DebugNamesDWARFIndex.h"
69 #include "LogChannelDWARF.h"
70 #include "ManualDWARFIndex.h"
71 #include "SymbolFileDWARFDebugMap.h"
72 #include "SymbolFileDWARFDwo.h"
73 #include "SymbolFileDWARFDwp.h"
75 #include "llvm/Support/FileSystem.h"
82 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
84 #ifdef ENABLE_DEBUG_PRINTF
86 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
88 #define DEBUG_PRINTF(fmt, ...)
92 using namespace lldb_private;
95 // child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
101 // case DW_TAG_subprogram:
102 // case DW_TAG_inlined_subroutine:
103 // case DW_TAG_class_type:
104 // case DW_TAG_structure_type:
105 // case DW_TAG_union_type:
114 PropertyDefinition g_properties[] = {
115 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
117 "If the DW_AT_comp_dir matches any of these paths the symbolic "
118 "links will be resolved at DWARF parse time."},
119 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr,
121 "Ignore indexes present in the object files and always index DWARF "
123 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr},
127 ePropertySymLinkPaths,
128 ePropertyIgnoreIndexes,
131 class PluginProperties : public Properties {
133 static ConstString GetSettingName() {
134 return SymbolFileDWARF::GetPluginNameStatic();
138 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
139 m_collection_sp->Initialize(g_properties);
142 FileSpecList &GetSymLinkPaths() {
143 OptionValueFileSpecList *option_value =
144 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
145 nullptr, true, ePropertySymLinkPaths);
146 assert(option_value);
147 return option_value->GetCurrentValue();
150 bool IgnoreFileIndexes() const {
151 return m_collection_sp->GetPropertyAtIndexAsBoolean(
152 nullptr, ePropertyIgnoreIndexes, false);
156 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
158 static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
159 static const auto g_settings_sp(std::make_shared<PluginProperties>());
160 return g_settings_sp;
163 } // anonymous namespace end
165 static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
166 if (!path_from_dwarf || !path_from_dwarf[0]) {
167 return path_from_dwarf;
170 const char *colon_pos = strchr(path_from_dwarf, ':');
171 if (nullptr == colon_pos) {
172 return path_from_dwarf;
175 const char *slash_pos = strchr(path_from_dwarf, '/');
176 if (slash_pos && (slash_pos < colon_pos)) {
177 return path_from_dwarf;
180 // check whether we have a windows path, and so the first character is a
181 // drive-letter not a hostname.
182 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
183 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
184 return path_from_dwarf;
187 return colon_pos + 1;
190 static FileSpec resolveCompDir(const char *path_from_dwarf) {
191 if (!path_from_dwarf)
194 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
195 // Remove the host part if present.
196 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
200 bool is_symlink = false;
201 // Always normalize our compile unit directory to get rid of redundant
202 // slashes and other path anomalies before we use it for path prepending
203 FileSpec local_spec(local_path, false);
204 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
205 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
206 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
212 namespace fs = llvm::sys::fs;
213 if (fs::get_file_type(local_spec.GetPath(), false) !=
214 fs::file_type::symlink_file)
217 FileSpec resolved_symlink;
218 const auto error = FileSystem::Readlink(local_spec, resolved_symlink);
220 return resolved_symlink;
225 DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
229 void SymbolFileDWARF::Initialize() {
230 LogChannelDWARF::Initialize();
231 PluginManager::RegisterPlugin(GetPluginNameStatic(),
232 GetPluginDescriptionStatic(), CreateInstance,
236 void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
237 if (!PluginManager::GetSettingForSymbolFilePlugin(
238 debugger, PluginProperties::GetSettingName())) {
239 const bool is_global_setting = true;
240 PluginManager::CreateSettingForSymbolFilePlugin(
241 debugger, GetGlobalPluginProperties()->GetValueProperties(),
242 ConstString("Properties for the dwarf symbol-file plug-in."),
247 void SymbolFileDWARF::Terminate() {
248 PluginManager::UnregisterPlugin(CreateInstance);
249 LogChannelDWARF::Terminate();
252 lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
253 static ConstString g_name("dwarf");
257 const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
258 return "DWARF and DWARF3 debug symbol file reader.";
261 SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
262 return new SymbolFileDWARF(obj_file);
265 TypeList *SymbolFileDWARF::GetTypeList() {
266 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
267 if (debug_map_symfile)
268 return debug_map_symfile->GetTypeList();
270 return m_obj_file->GetModule()->GetTypeList();
272 void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
273 dw_offset_t max_die_offset, uint32_t type_mask,
276 const dw_offset_t die_offset = die.GetOffset();
278 if (die_offset >= max_die_offset)
281 if (die_offset >= min_die_offset) {
282 const dw_tag_t tag = die.Tag();
284 bool add_type = false;
287 case DW_TAG_array_type:
288 add_type = (type_mask & eTypeClassArray) != 0;
290 case DW_TAG_unspecified_type:
291 case DW_TAG_base_type:
292 add_type = (type_mask & eTypeClassBuiltin) != 0;
294 case DW_TAG_class_type:
295 add_type = (type_mask & eTypeClassClass) != 0;
297 case DW_TAG_structure_type:
298 add_type = (type_mask & eTypeClassStruct) != 0;
300 case DW_TAG_union_type:
301 add_type = (type_mask & eTypeClassUnion) != 0;
303 case DW_TAG_enumeration_type:
304 add_type = (type_mask & eTypeClassEnumeration) != 0;
306 case DW_TAG_subroutine_type:
307 case DW_TAG_subprogram:
308 case DW_TAG_inlined_subroutine:
309 add_type = (type_mask & eTypeClassFunction) != 0;
311 case DW_TAG_pointer_type:
312 add_type = (type_mask & eTypeClassPointer) != 0;
314 case DW_TAG_rvalue_reference_type:
315 case DW_TAG_reference_type:
316 add_type = (type_mask & eTypeClassReference) != 0;
319 add_type = (type_mask & eTypeClassTypedef) != 0;
321 case DW_TAG_ptr_to_member_type:
322 add_type = (type_mask & eTypeClassMemberPointer) != 0;
327 const bool assert_not_being_parsed = true;
328 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
330 if (type_set.find(type) == type_set.end())
331 type_set.insert(type);
336 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
337 child_die = child_die.GetSibling()) {
338 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
343 size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
344 uint32_t type_mask, TypeList &type_list)
349 CompileUnit *comp_unit = NULL;
350 DWARFUnit *dwarf_cu = NULL;
352 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
355 dwarf_cu = GetDWARFCompileUnit(comp_unit);
358 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
359 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
361 DWARFDebugInfo *info = DebugInfo();
363 const size_t num_cus = info->GetNumCompileUnits();
364 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
365 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
367 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
373 std::set<CompilerType> compiler_type_set;
374 size_t num_types_added = 0;
375 for (Type *type : type_set) {
376 CompilerType compiler_type = type->GetForwardCompilerType();
377 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
378 compiler_type_set.insert(compiler_type);
379 type_list.Insert(type->shared_from_this());
383 return num_types_added;
386 //----------------------------------------------------------------------
387 // Gets the first parent that is a lexical block, function or inlined
388 // subroutine, or compile unit.
389 //----------------------------------------------------------------------
391 SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
393 for (die = child_die.GetParent(); die; die = die.GetParent()) {
394 dw_tag_t tag = die.Tag();
397 case DW_TAG_compile_unit:
398 case DW_TAG_partial_unit:
399 case DW_TAG_subprogram:
400 case DW_TAG_inlined_subroutine:
401 case DW_TAG_lexical_block:
408 SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
409 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
410 << 32), // Used by SymbolFileDWARFDebugMap to
411 // when this class parses .o files to
412 // contain the .o file index/ID
413 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
414 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
415 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
416 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
417 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
418 m_line(), m_fetched_external_modules(false),
419 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
420 m_unique_ast_type_map() {}
422 SymbolFileDWARF::~SymbolFileDWARF() {}
424 static const ConstString &GetDWARFMachOSegmentName() {
425 static ConstString g_dwarf_section_name("__DWARF");
426 return g_dwarf_section_name;
429 UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
430 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
431 if (debug_map_symfile)
432 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
434 return m_unique_ast_type_map;
437 TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
438 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
439 TypeSystem *type_system;
440 if (debug_map_symfile) {
441 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
443 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
445 type_system->SetSymbolFile(this);
450 void SymbolFileDWARF::InitializeObject() {
451 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
452 ModuleSP module_sp(m_obj_file->GetModule());
454 const SectionList *section_list = module_sp->GetSectionList();
456 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
459 m_obj_file->ReadSectionData(section, m_dwarf_data);
462 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
463 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
464 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
465 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
466 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
467 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
469 m_index = AppleDWARFIndex::Create(
470 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
471 apple_types, apple_objc, get_debug_str_data());
476 DWARFDataExtractor debug_names;
477 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
478 if (debug_names.GetByteSize() > 0) {
479 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
480 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
481 debug_names, get_debug_str_data(),
484 m_index = std::move(*index_or);
487 LLDB_LOG_ERROR(log, index_or.takeError(),
488 "Unable to read .debug_names data: {0}");
492 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
496 bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
497 return version == 2 || version == 3 || version == 4;
500 uint32_t SymbolFileDWARF::CalculateAbilities() {
501 uint32_t abilities = 0;
502 if (m_obj_file != NULL) {
503 const Section *section = NULL;
504 const SectionList *section_list = m_obj_file->GetSectionList();
505 if (section_list == NULL)
508 // On non Apple platforms we might have .debug_types debug info that is
509 // created by using "-fdebug-types-section". LLDB currently will try to
510 // load this debug info, but it causes crashes during debugging when types
511 // are missing since it doesn't know how to parse the info in the
512 // .debug_types type units. This causes all complex debug info types to be
513 // unresolved. Because this causes LLDB to crash and since it really
514 // doesn't provide a solid debuggiung experience, we should disable trying
515 // to debug this kind of DWARF until support gets added or deprecated.
516 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
517 m_obj_file->GetModule()->ReportWarning(
518 "lldb doesn’t support .debug_types debug info");
522 uint64_t debug_abbrev_file_size = 0;
523 uint64_t debug_info_file_size = 0;
524 uint64_t debug_line_file_size = 0;
526 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
529 section_list = §ion->GetChildren();
532 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
533 if (section != NULL) {
534 debug_info_file_size = section->GetFileSize();
537 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
540 debug_abbrev_file_size = section->GetFileSize();
542 DWARFDebugAbbrev *abbrev = DebugAbbrev();
544 std::set<dw_form_t> invalid_forms;
545 abbrev->GetUnsupportedForms(invalid_forms);
546 if (!invalid_forms.empty()) {
548 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
549 for (auto form : invalid_forms)
550 error.Printf(" %#x", form);
551 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
557 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
560 debug_line_file_size = section->GetFileSize();
562 const char *symfile_dir_cstr =
563 m_obj_file->GetFileSpec().GetDirectory().GetCString();
564 if (symfile_dir_cstr) {
565 if (strcasestr(symfile_dir_cstr, ".dsym")) {
566 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
567 // We have a dSYM file that didn't have a any debug info. If the
568 // string table has a size of 1, then it was made from an
569 // executable with no debug info, or from an executable that was
572 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
574 if (section && section->GetFileSize() == 1) {
575 m_obj_file->GetModule()->ReportWarning(
576 "empty dSYM file detected, dSYM was created with an "
577 "executable with no debug info.");
584 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
585 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
586 LocalVariables | VariableTypes;
588 if (debug_line_file_size > 0)
589 abilities |= LineTables;
594 const DWARFDataExtractor &
595 SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
596 DWARFDataSegment &data_segment) {
597 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
598 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
600 return data_segment.m_data;
603 void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
604 DWARFDataExtractor &data) {
605 ModuleSP module_sp(m_obj_file->GetModule());
606 const SectionList *section_list = module_sp->GetSectionList();
608 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
610 // See if we memory mapped the DWARF segment?
611 if (m_dwarf_data.GetByteSize()) {
612 data.SetData(m_dwarf_data, section_sp->GetOffset(),
613 section_sp->GetFileSize());
615 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
622 const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
623 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
624 m_data_debug_abbrev);
627 const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
628 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
631 const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
632 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
633 m_data_debug_aranges);
636 const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
637 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
640 const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
641 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
644 const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
645 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
648 const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
649 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
652 const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
653 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
656 const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
657 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
658 m_data_debug_ranges);
661 const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
662 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
665 const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
666 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
667 m_data_debug_str_offsets);
670 const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
671 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
674 const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
675 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
678 const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
679 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
682 const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
683 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
684 m_data_apple_namespaces);
687 const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
688 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
691 const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
692 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
693 m_data_gnu_debugaltlink);
696 DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
697 if (m_abbr.get() == NULL) {
698 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
699 if (debug_abbrev_data.GetByteSize() > 0) {
700 m_abbr.reset(new DWARFDebugAbbrev());
702 m_abbr->Parse(debug_abbrev_data);
708 const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
712 DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
713 if (m_info.get() == NULL) {
714 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
715 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
716 static_cast<void *>(this));
717 if (get_debug_info_data().GetByteSize() > 0) {
718 m_info.reset(new DWARFDebugInfo());
720 m_info->SetDwarfData(this);
727 const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
732 SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
736 DWARFDebugInfo *info = DebugInfo();
738 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
741 DWARFUnit *dwarf_cu =
742 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
743 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
744 dwarf_cu->SetUserData(comp_unit);
750 DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
751 if (m_ranges.get() == NULL) {
752 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
753 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
754 static_cast<void *>(this));
755 if (get_debug_ranges_data().GetByteSize() > 0) {
756 m_ranges.reset(new DWARFDebugRanges());
758 m_ranges->Extract(this);
761 return m_ranges.get();
764 const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
765 return m_ranges.get();
768 lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
772 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
774 // We already parsed this compile unit, had out a shared pointer to it
775 cu_sp = comp_unit->shared_from_this();
777 if (dwarf_cu->GetSymbolFileDWARF() != this) {
778 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
780 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
781 // Let the debug map create the compile unit
782 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
783 dwarf_cu->SetUserData(cu_sp.get());
785 ModuleSP module_sp(m_obj_file->GetModule());
787 const DWARFDIE cu_die = dwarf_cu->DIE();
789 FileSpec cu_file_spec{cu_die.GetName(), false};
791 // If we have a full path to the compile unit, we don't need to
792 // resolve the file. This can be expensive e.g. when the source
795 if (cu_file_spec.IsRelative()) {
796 const char *cu_comp_dir{
797 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
798 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
801 std::string remapped_file;
802 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
804 cu_file_spec.SetFile(remapped_file, false,
805 FileSpec::Style::native);
808 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
809 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
811 bool is_optimized = dwarf_cu->GetIsOptimized();
812 cu_sp.reset(new CompileUnit(
813 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
814 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
816 // If we just created a compile unit with an invalid file spec,
817 // try and get the first entry in the supports files from the
818 // line table as that should be the compile unit.
820 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
822 (FileSpec &)(*cu_sp) = cu_file_spec;
823 // Also fix the invalid file spec which was copied from the
825 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
829 dwarf_cu->SetUserData(cu_sp.get());
831 // Figure out the compile unit index if we weren't given one
832 if (cu_idx == UINT32_MAX)
833 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
835 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
846 uint32_t SymbolFileDWARF::GetNumCompileUnits() {
847 DWARFDebugInfo *info = DebugInfo();
849 return info->GetNumCompileUnits();
853 CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
855 DWARFDebugInfo *info = DebugInfo();
857 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
859 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
864 Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
865 const DWARFDIE &die) {
867 TypeSystem *type_system =
868 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
871 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
873 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
879 bool SymbolFileDWARF::FixupAddress(Address &addr) {
880 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
881 if (debug_map_symfile) {
882 return debug_map_symfile->LinkOSOAddress(addr);
884 // This is a normal DWARF file, no address fixups need to happen
888 SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
889 assert(sc.comp_unit);
890 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
892 return dwarf_cu->GetLanguageType();
894 return eLanguageTypeUnknown;
897 size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
898 assert(sc.comp_unit);
899 size_t functions_added = 0;
900 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
902 DWARFDIECollection function_dies;
903 const size_t num_functions =
904 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
906 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
907 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
908 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
909 if (ParseCompileUnitFunction(sc, die))
915 return functions_added;
918 bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
919 const SymbolContext &sc, FileSpecList &support_files) {
920 assert(sc.comp_unit);
921 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
923 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
926 FileSpec cu_comp_dir = resolveCompDir(
927 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
928 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
929 DW_AT_stmt_list, DW_INVALID_OFFSET);
930 if (stmt_list != DW_INVALID_OFFSET) {
931 // All file indexes in DWARF are one based and a file of index zero is
932 // supposed to be the compile unit itself.
933 support_files.Append(*sc.comp_unit);
934 return DWARFDebugLine::ParseSupportFiles(
935 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
936 stmt_list, support_files);
943 bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
944 const lldb_private::SymbolContext &sc) {
945 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
947 return dwarf_cu->GetIsOptimized();
951 bool SymbolFileDWARF::ParseImportedModules(
952 const lldb_private::SymbolContext &sc,
953 std::vector<lldb_private::ConstString> &imported_modules) {
954 assert(sc.comp_unit);
955 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
957 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
958 sc.comp_unit->GetLanguage())) {
959 UpdateExternalModuleListIfNeeded();
962 const DWARFDIE die = dwarf_cu->DIE();
965 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
966 child_die = child_die.GetSibling()) {
967 if (child_die.Tag() == DW_TAG_imported_declaration) {
968 if (DWARFDIE module_die =
969 child_die.GetReferencedDIE(DW_AT_import)) {
970 if (module_die.Tag() == DW_TAG_module) {
971 if (const char *name = module_die.GetAttributeValueAsString(
972 DW_AT_name, nullptr)) {
973 ConstString const_name(name);
974 imported_modules.push_back(const_name);
982 for (const auto &pair : m_external_type_modules) {
983 imported_modules.push_back(pair.first);
991 struct ParseDWARFLineTableCallbackInfo {
992 LineTable *line_table;
993 std::unique_ptr<LineSequence> sequence_ap;
994 lldb::addr_t addr_mask;
997 //----------------------------------------------------------------------
998 // ParseStatementTableCallback
999 //----------------------------------------------------------------------
1000 static void ParseDWARFLineTableCallback(dw_offset_t offset,
1001 const DWARFDebugLine::State &state,
1003 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1004 // Just started parsing the line table
1005 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1006 // Done parsing line table, nothing to do for the cleanup
1008 ParseDWARFLineTableCallbackInfo *info =
1009 (ParseDWARFLineTableCallbackInfo *)userData;
1010 LineTable *line_table = info->line_table;
1012 // If this is our first time here, we need to create a sequence container.
1013 if (!info->sequence_ap.get()) {
1014 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1015 assert(info->sequence_ap.get());
1017 line_table->AppendLineEntryToSequence(
1018 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1019 state.column, state.file, state.is_stmt, state.basic_block,
1020 state.prologue_end, state.epilogue_begin, state.end_sequence);
1021 if (state.end_sequence) {
1022 // First, put the current sequence into the line table.
1023 line_table->InsertSequence(info->sequence_ap.get());
1024 // Then, empty it to prepare for the next sequence.
1025 info->sequence_ap->Clear();
1030 bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1031 assert(sc.comp_unit);
1032 if (sc.comp_unit->GetLineTable() != NULL)
1035 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1037 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
1039 const dw_offset_t cu_line_offset =
1040 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1042 if (cu_line_offset != DW_INVALID_OFFSET) {
1043 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1044 if (line_table_ap.get()) {
1045 ParseDWARFLineTableCallbackInfo info;
1046 info.line_table = line_table_ap.get();
1050 * The SymbolContext may not have a valid target, thus we may not be
1052 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1054 * for MIPS. Use ArchSpec to clear the bit #0.
1057 GetObjectFile()->GetArchitecture(arch);
1058 switch (arch.GetMachine()) {
1059 case llvm::Triple::mips:
1060 case llvm::Triple::mipsel:
1061 case llvm::Triple::mips64:
1062 case llvm::Triple::mips64el:
1063 info.addr_mask = ~((lldb::addr_t)1);
1066 info.addr_mask = ~((lldb::addr_t)0);
1070 lldb::offset_t offset = cu_line_offset;
1071 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1072 ParseDWARFLineTableCallback,
1074 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1075 if (debug_map_symfile) {
1076 // We have an object file that has a line table with addresses that
1077 // are not linked. We need to link the line table and convert the
1078 // addresses that are relative to the .o file into addresses for
1079 // the main executable.
1080 sc.comp_unit->SetLineTable(
1081 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1083 sc.comp_unit->SetLineTable(line_table_ap.release());
1093 lldb_private::DebugMacrosSP
1094 SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1095 auto iter = m_debug_macros_map.find(*offset);
1096 if (iter != m_debug_macros_map.end())
1097 return iter->second;
1099 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1100 if (debug_macro_data.GetByteSize() == 0)
1101 return DebugMacrosSP();
1103 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1104 m_debug_macros_map[*offset] = debug_macros_sp;
1106 const DWARFDebugMacroHeader &header =
1107 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1108 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1109 header.OffsetIs64Bit(), offset, this,
1112 return debug_macros_sp;
1115 bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1116 assert(sc.comp_unit);
1118 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1119 if (dwarf_cu == nullptr)
1122 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
1126 lldb::offset_t sect_offset =
1127 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1128 if (sect_offset == DW_INVALID_OFFSET)
1129 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1131 if (sect_offset == DW_INVALID_OFFSET)
1134 sc.comp_unit->SetDebugMacros(ParseDebugMacros(§_offset));
1139 size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1140 Block *parent_block,
1141 const DWARFDIE &orig_die,
1142 addr_t subprogram_low_pc,
1144 size_t blocks_added = 0;
1145 DWARFDIE die = orig_die;
1147 dw_tag_t tag = die.Tag();
1150 case DW_TAG_inlined_subroutine:
1151 case DW_TAG_subprogram:
1152 case DW_TAG_lexical_block: {
1153 Block *block = NULL;
1154 if (tag == DW_TAG_subprogram) {
1155 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1156 // inlined functions. These will be parsed on their own as separate
1162 block = parent_block;
1164 BlockSP block_sp(new Block(die.GetID()));
1165 parent_block->AddChild(block_sp);
1166 block = block_sp.get();
1168 DWARFRangeList ranges;
1169 const char *name = NULL;
1170 const char *mangled_name = NULL;
1174 int decl_column = 0;
1177 int call_column = 0;
1178 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1179 decl_line, decl_column, call_file, call_line,
1180 call_column, nullptr)) {
1181 if (tag == DW_TAG_subprogram) {
1182 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1183 subprogram_low_pc = ranges.GetMinRangeBase(0);
1184 } else if (tag == DW_TAG_inlined_subroutine) {
1185 // We get called here for inlined subroutines in two ways. The first
1186 // time is when we are making the Function object for this inlined
1187 // concrete instance. Since we're creating a top level block at
1188 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1189 // need to adjust the containing address. The second time is when we
1190 // are parsing the blocks inside the function that contains the
1191 // inlined concrete instance. Since these will be blocks inside the
1192 // containing "real" function the offset will be for that function.
1193 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1194 subprogram_low_pc = ranges.GetMinRangeBase(0);
1198 const size_t num_ranges = ranges.GetSize();
1199 for (size_t i = 0; i < num_ranges; ++i) {
1200 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1201 const addr_t range_base = range.GetRangeBase();
1202 if (range_base >= subprogram_low_pc)
1203 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1204 range.GetByteSize()));
1206 GetObjectFile()->GetModule()->ReportError(
1207 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1208 ") which has a base that is less than the function's low PC "
1209 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1210 "start of this error message",
1211 block->GetID(), range_base, range.GetRangeEnd(),
1215 block->FinalizeRanges();
1217 if (tag != DW_TAG_subprogram &&
1218 (name != NULL || mangled_name != NULL)) {
1219 std::unique_ptr<Declaration> decl_ap;
1220 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1221 decl_ap.reset(new Declaration(
1222 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1223 decl_line, decl_column));
1225 std::unique_ptr<Declaration> call_ap;
1226 if (call_file != 0 || call_line != 0 || call_column != 0)
1227 call_ap.reset(new Declaration(
1228 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1229 call_line, call_column));
1231 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1237 if (die.HasChildren()) {
1238 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1239 subprogram_low_pc, depth + 1);
1247 // Only parse siblings of the block if we are not at depth zero. A depth of
1248 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1254 die = die.GetSibling();
1256 return blocks_added;
1259 bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1261 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1262 die = die.GetSibling()) {
1263 dw_tag_t tag = die.Tag();
1264 bool check_virtuality = false;
1266 case DW_TAG_inheritance:
1267 case DW_TAG_subprogram:
1268 check_virtuality = true;
1273 if (check_virtuality) {
1274 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1282 void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1283 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1284 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1285 std::vector<DWARFDIE> decl_ctx_die_list =
1286 ast_parser->GetDIEForDeclContext(decl_ctx);
1288 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1289 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1290 decl = decl.GetSibling())
1291 ast_parser->GetDeclForUIDFromDWARF(decl);
1294 SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1295 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1296 // must make sure we use the correct DWARF file when resolving things. On
1297 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1298 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1299 // references to other DWARF objects and we must be ready to receive a
1300 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1302 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1304 return debug_map->GetSymbolFileByOSOIndex(
1305 debug_map->GetOSOIndexFromUserID(uid));
1310 SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1311 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1312 // must make sure we use the correct DWARF file when resolving things. On
1313 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1314 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1315 // references to other DWARF objects and we must be ready to receive a
1316 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1318 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1320 return dwarf->GetDIE(DIERef(uid, dwarf));
1324 CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1325 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1326 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1327 // SymbolFileDWARF::GetDIEFromUID() for details.
1328 DWARFDIE die = GetDIEFromUID(type_uid);
1330 return die.GetDecl();
1331 return CompilerDecl();
1335 SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1336 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1337 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1338 // SymbolFileDWARF::GetDIEFromUID() for details.
1339 DWARFDIE die = GetDIEFromUID(type_uid);
1341 return die.GetDeclContext();
1342 return CompilerDeclContext();
1346 SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1347 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1348 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1349 // SymbolFileDWARF::GetDIEFromUID() for details.
1350 DWARFDIE die = GetDIEFromUID(type_uid);
1352 return die.GetContainingDeclContext();
1353 return CompilerDeclContext();
1356 Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1357 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1358 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1359 // SymbolFileDWARF::GetDIEFromUID() for details.
1360 DWARFDIE type_die = GetDIEFromUID(type_uid);
1362 return type_die.ResolveType();
1367 Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1368 return ResolveType(GetDIE(die_ref), true);
1371 Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1372 bool assert_not_being_parsed) {
1374 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1376 GetObjectFile()->GetModule()->LogMessage(
1377 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1378 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1380 // We might be coming in in the middle of a type tree (a class within a
1381 // class, an enum within a class), so parse any needed parent DIEs before
1382 // we get to this one...
1383 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1386 switch (decl_ctx_die.Tag()) {
1387 case DW_TAG_structure_type:
1388 case DW_TAG_union_type:
1389 case DW_TAG_class_type: {
1390 // Get the type, which could be a forward declaration
1392 GetObjectFile()->GetModule()->LogMessage(
1393 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1394 "resolve parent forward type for 0x%8.8x",
1395 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1396 decl_ctx_die.GetOffset());
1404 return ResolveType(die);
1409 // This function is used when SymbolFileDWARFDebugMap owns a bunch of
1410 // SymbolFileDWARF objects to detect if this DWARF file is the one that can
1411 // resolve a compiler_type.
1412 bool SymbolFileDWARF::HasForwardDeclForClangType(
1413 const CompilerType &compiler_type) {
1414 CompilerType compiler_type_no_qualifiers =
1415 ClangUtil::RemoveFastQualifiers(compiler_type);
1416 if (GetForwardDeclClangTypeToDie().count(
1417 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1420 TypeSystem *type_system = compiler_type.GetTypeSystem();
1422 ClangASTContext *clang_type_system =
1423 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1424 if (!clang_type_system)
1426 DWARFASTParserClang *ast_parser =
1427 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1428 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
1431 bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1432 std::lock_guard<std::recursive_mutex> guard(
1433 GetObjectFile()->GetModule()->GetMutex());
1435 ClangASTContext *clang_type_system =
1436 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1437 if (clang_type_system) {
1438 DWARFASTParserClang *ast_parser =
1439 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1441 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1442 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1445 // We have a struct/union/class/enum that needs to be fully resolved.
1446 CompilerType compiler_type_no_qualifiers =
1447 ClangUtil::RemoveFastQualifiers(compiler_type);
1448 auto die_it = GetForwardDeclClangTypeToDie().find(
1449 compiler_type_no_qualifiers.GetOpaqueQualType());
1450 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1451 // We have already resolved this type...
1455 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1457 // Once we start resolving this type, remove it from the forward
1458 // declaration map in case anyone child members or other types require this
1459 // type to get resolved. The type will get resolved when all of the calls
1460 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
1461 GetForwardDeclClangTypeToDie().erase(die_it);
1463 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1465 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1466 DWARF_LOG_TYPE_COMPLETION));
1468 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1469 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1470 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1471 type->GetName().AsCString());
1472 assert(compiler_type);
1473 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1475 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1480 Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1481 bool assert_not_being_parsed,
1482 bool resolve_function_context) {
1484 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1486 if (assert_not_being_parsed) {
1487 if (type != DIE_IS_BEING_PARSED)
1490 GetObjectFile()->GetModule()->ReportError(
1491 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1492 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1501 SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
1503 // Check if the symbol vendor already knows about this compile unit?
1504 if (dwarf_cu->GetUserData() == NULL) {
1505 // The symbol vendor doesn't know about this compile unit, we need to parse
1506 // and add it to the symbol vendor object.
1507 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1509 return (CompileUnit *)dwarf_cu->GetUserData();
1512 size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1513 DIEArray &method_die_offsets) {
1514 method_die_offsets.clear();
1515 m_index->GetObjCMethods(class_name, method_die_offsets);
1516 return method_die_offsets.size();
1519 bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1523 // Check if the symbol vendor already knows about this compile unit?
1524 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1526 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1527 if (sc.function == NULL)
1528 sc.function = ParseCompileUnitFunction(sc, die);
1531 sc.module_sp = sc.function->CalculateSymbolContextModule();
1539 lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1540 UpdateExternalModuleListIfNeeded();
1541 const auto &pos = m_external_type_modules.find(name);
1542 if (pos != m_external_type_modules.end())
1545 return lldb::ModuleSP();
1549 SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1550 DWARFDebugInfo *debug_info = DebugInfo();
1552 return debug_info->GetDIE(die_ref);
1557 std::unique_ptr<SymbolFileDWARFDwo>
1558 SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1559 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1560 // If we are using a dSYM file, we never want the standard DWO files since
1561 // the -gmodules support uses the same DWO machanism to specify full debug
1562 // info files for modules.
1563 if (GetDebugMapSymfile())
1566 const char *dwo_name = cu_die.GetAttributeValueAsString(
1567 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1571 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1573 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1574 DW_AT_GNU_dwo_id, 0);
1575 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1576 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1581 FileSpec dwo_file(dwo_name, true);
1582 if (dwo_file.IsRelative()) {
1583 const char *comp_dir = cu_die.GetAttributeValueAsString(
1584 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1588 dwo_file.SetFile(comp_dir, true, FileSpec::Style::native);
1589 dwo_file.AppendPathComponent(dwo_name);
1592 if (!dwo_file.Exists())
1595 const lldb::offset_t file_offset = 0;
1596 DataBufferSP dwo_file_data_sp;
1597 lldb::offset_t dwo_file_data_offset = 0;
1598 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1599 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1600 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1601 if (dwo_obj_file == nullptr)
1604 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
1607 void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1608 if (m_fetched_external_modules)
1610 m_fetched_external_modules = true;
1612 DWARFDebugInfo *debug_info = DebugInfo();
1614 const uint32_t num_compile_units = GetNumCompileUnits();
1615 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1616 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1618 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
1619 if (die && die.HasChildren() == false) {
1620 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1623 ConstString const_name(name);
1624 if (m_external_type_modules.find(const_name) ==
1625 m_external_type_modules.end()) {
1627 const char *dwo_path =
1628 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1630 ModuleSpec dwo_module_spec;
1631 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false,
1632 FileSpec::Style::native);
1633 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1634 const char *comp_dir =
1635 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1637 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true,
1638 FileSpec::Style::native);
1639 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1642 dwo_module_spec.GetArchitecture() =
1643 m_obj_file->GetModule()->GetArchitecture();
1645 // When LLDB loads "external" modules it looks at the presence of
1646 // DW_AT_GNU_dwo_name. However, when the already created module
1647 // (corresponding to .dwo itself) is being processed, it will see
1648 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1649 // dwo file) and will try to call ModuleList::GetSharedModule
1650 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1651 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1652 // DW_AT_comp_dir. In this case the method
1653 // ModuleList::GetSharedModule will fail and the warning will be
1654 // printed. However, as one can notice in this case we don't
1655 // actually need to try to load the already loaded module
1656 // (corresponding to .dwo) so we simply skip it.
1657 if (m_obj_file->GetFileSpec()
1658 .GetFileNameExtension()
1659 .GetStringRef() == ".dwo" &&
1660 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1661 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1665 Status error = ModuleList::GetSharedModule(
1666 dwo_module_spec, module_sp, NULL, NULL, NULL);
1668 GetObjectFile()->GetModule()->ReportWarning(
1669 "0x%8.8x: unable to locate module needed for external types: "
1670 "%s\nerror: %s\nDebugging will be degraded due to missing "
1671 "types. Rebuilding your project will regenerate the needed "
1674 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1675 error.AsCString("unknown error"));
1678 m_external_type_modules[const_name] = module_sp;
1685 SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1686 if (!m_global_aranges_ap) {
1687 m_global_aranges_ap.reset(new GlobalVariableMap());
1689 ModuleSP module_sp = GetObjectFile()->GetModule();
1691 const size_t num_cus = module_sp->GetNumCompileUnits();
1692 for (size_t i = 0; i < num_cus; ++i) {
1693 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1695 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1697 const size_t num_globals = globals_sp->GetSize();
1698 for (size_t g = 0; g < num_globals; ++g) {
1699 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1700 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1701 const DWARFExpression &location = var_sp->LocationExpression();
1702 Value location_result;
1704 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1705 nullptr, location_result, &error)) {
1706 if (location_result.GetValueType() ==
1707 Value::eValueTypeFileAddress) {
1708 lldb::addr_t file_addr =
1709 location_result.GetScalar().ULongLong();
1710 lldb::addr_t byte_size = 1;
1711 if (var_sp->GetType())
1712 byte_size = var_sp->GetType()->GetByteSize();
1713 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1714 file_addr, byte_size, var_sp.get()));
1723 m_global_aranges_ap->Sort();
1725 return *m_global_aranges_ap;
1728 uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1729 uint32_t resolve_scope,
1730 SymbolContext &sc) {
1731 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1732 Timer scoped_timer(func_cat,
1734 "ResolveSymbolContext (so_addr = { "
1735 "section = %p, offset = 0x%" PRIx64
1736 " }, resolve_scope = 0x%8.8x)",
1737 static_cast<void *>(so_addr.GetSection().get()),
1738 so_addr.GetOffset(), resolve_scope);
1739 uint32_t resolved = 0;
1741 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1742 eSymbolContextLineEntry | eSymbolContextVariable)) {
1743 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1745 DWARFDebugInfo *debug_info = DebugInfo();
1747 const dw_offset_t cu_offset =
1748 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1749 if (cu_offset == DW_INVALID_OFFSET) {
1750 // Global variables are not in the compile unit address ranges. The
1751 // only way to currently find global variables is to iterate over the
1752 // .debug_pubnames or the __apple_names table and find all items in
1753 // there that point to DW_TAG_variable DIEs and then find the address
1755 if (resolve_scope & eSymbolContextVariable) {
1756 GlobalVariableMap &map = GetGlobalAranges();
1757 const GlobalVariableMap::Entry *entry =
1758 map.FindEntryThatContains(file_vm_addr);
1759 if (entry && entry->data) {
1760 Variable *variable = entry->data;
1761 SymbolContextScope *scc = variable->GetSymbolContextScope();
1763 scc->CalculateSymbolContext(&sc);
1764 sc.variable = variable;
1766 return sc.GetResolvedMask();
1770 uint32_t cu_idx = DW_INVALID_INDEX;
1771 DWARFUnit *dwarf_cu =
1772 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1774 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1776 resolved |= eSymbolContextCompUnit;
1778 bool force_check_line_table = false;
1780 (eSymbolContextFunction | eSymbolContextBlock)) {
1781 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1785 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1786 if (sc.function == NULL)
1787 sc.function = ParseCompileUnitFunction(sc, function_die);
1789 if (sc.function && (resolve_scope & eSymbolContextBlock))
1790 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1792 // We might have had a compile unit that had discontiguous
1793 // address ranges where the gaps are symbols that don't have
1794 // any debug info. Discontiguous compile unit address ranges
1795 // should only happen when there aren't other functions from
1796 // other compile units in these gaps. This helps keep the size
1797 // of the aranges down.
1798 force_check_line_table = true;
1801 if (sc.function != NULL) {
1802 resolved |= eSymbolContextFunction;
1804 if (resolve_scope & eSymbolContextBlock) {
1805 Block &block = sc.function->GetBlock(true);
1808 sc.block = block.FindBlockByID(block_die.GetID());
1810 sc.block = block.FindBlockByID(function_die.GetID());
1812 resolved |= eSymbolContextBlock;
1817 if ((resolve_scope & eSymbolContextLineEntry) ||
1818 force_check_line_table) {
1819 LineTable *line_table = sc.comp_unit->GetLineTable();
1820 if (line_table != NULL) {
1821 // And address that makes it into this function should be in
1822 // terms of this debug file if there is no debug map, or it
1823 // will be an address in the .o file which needs to be fixed up
1824 // to be in terms of the debug map executable. Either way,
1825 // calling FixupAddress() will work for us.
1826 Address exe_so_addr(so_addr);
1827 if (FixupAddress(exe_so_addr)) {
1828 if (line_table->FindLineEntryByAddress(exe_so_addr,
1830 resolved |= eSymbolContextLineEntry;
1836 if (force_check_line_table &&
1837 !(resolved & eSymbolContextLineEntry)) {
1838 // We might have had a compile unit that had discontiguous
1839 // address ranges where the gaps are symbols that don't have any
1840 // debug info. Discontiguous compile unit address ranges should
1841 // only happen when there aren't other functions from other
1842 // compile units in these gaps. This helps keep the size of the
1844 sc.comp_unit = NULL;
1845 resolved &= ~eSymbolContextCompUnit;
1848 GetObjectFile()->GetModule()->ReportWarning(
1849 "0x%8.8x: compile unit %u failed to create a valid "
1850 "lldb_private::CompileUnit class.",
1860 uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1863 uint32_t resolve_scope,
1864 SymbolContextList &sc_list) {
1865 const uint32_t prev_size = sc_list.GetSize();
1866 if (resolve_scope & eSymbolContextCompUnit) {
1867 DWARFDebugInfo *debug_info = DebugInfo();
1870 DWARFUnit *dwarf_cu = NULL;
1873 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1875 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1876 const bool full_match = (bool)file_spec.GetDirectory();
1877 bool file_spec_matches_cu_file_spec =
1878 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1879 if (check_inlines || file_spec_matches_cu_file_spec) {
1880 SymbolContext sc(m_obj_file->GetModule());
1881 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1883 uint32_t file_idx = UINT32_MAX;
1885 // If we are looking for inline functions only and we don't find it
1886 // in the support files, we are done.
1887 if (check_inlines) {
1888 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1889 1, file_spec, true);
1890 if (file_idx == UINT32_MAX)
1895 LineTable *line_table = sc.comp_unit->GetLineTable();
1897 if (line_table != NULL && line != 0) {
1898 // We will have already looked up the file index if we are
1899 // searching for inline entries.
1901 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1902 1, file_spec, true);
1904 if (file_idx != UINT32_MAX) {
1905 uint32_t found_line;
1906 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1907 0, file_idx, line, false, &sc.line_entry);
1908 found_line = sc.line_entry.line;
1910 while (line_idx != UINT32_MAX) {
1914 (eSymbolContextFunction | eSymbolContextBlock)) {
1915 const lldb::addr_t file_vm_addr =
1916 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1917 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1918 DWARFDIE function_die =
1919 dwarf_cu->LookupAddress(file_vm_addr);
1924 ->FindFunctionByUID(function_die.GetID())
1926 if (sc.function == NULL)
1928 ParseCompileUnitFunction(sc, function_die);
1931 (resolve_scope & eSymbolContextBlock))
1933 function_die.LookupDeepestBlock(file_vm_addr);
1936 if (sc.function != NULL) {
1937 Block &block = sc.function->GetBlock(true);
1940 sc.block = block.FindBlockByID(block_die.GetID());
1941 else if (function_die)
1943 block.FindBlockByID(function_die.GetID());
1949 line_idx = line_table->FindLineEntryIndexByFileIndex(
1950 line_idx + 1, file_idx, found_line, true,
1954 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1955 // only append the context if we aren't looking for inline call
1956 // sites by file and line and if the file spec matches that of
1960 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1961 // only append the context if we aren't looking for inline call
1962 // sites by file and line and if the file spec matches that of
1974 return sc_list.GetSize() - prev_size;
1977 void SymbolFileDWARF::PreloadSymbols() {
1978 std::lock_guard<std::recursive_mutex> guard(
1979 GetObjectFile()->GetModule()->GetMutex());
1983 bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1984 const lldb_private::CompilerDeclContext *decl_ctx) {
1985 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
1986 // Invalid namespace decl which means we aren't matching only things in
1987 // this symbol file, so return true to indicate it matches this symbol
1992 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1993 TypeSystem *type_system = GetTypeSystemForLanguage(
1994 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1995 if (decl_ctx_type_system == type_system)
1996 return true; // The type systems match, return true
1998 // The namespace AST was valid, and it does not match...
1999 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2002 GetObjectFile()->GetModule()->LogMessage(
2003 log, "Valid namespace does not match symbol file");
2008 uint32_t SymbolFileDWARF::FindGlobalVariables(
2009 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2010 uint32_t max_matches, VariableList &variables) {
2011 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2014 GetObjectFile()->GetModule()->LogMessage(
2016 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2017 "parent_decl_ctx=%p, max_matches=%u, variables)",
2018 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2021 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2024 DWARFDebugInfo *info = DebugInfo();
2028 // Remember how many variables are in the list before we search.
2029 const uint32_t original_size = variables.GetSize();
2031 llvm::StringRef basename;
2032 llvm::StringRef context;
2034 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2036 basename = name.GetStringRef();
2038 DIEArray die_offsets;
2039 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
2040 const size_t num_die_matches = die_offsets.size();
2041 if (num_die_matches) {
2043 sc.module_sp = m_obj_file->GetModule();
2044 assert(sc.module_sp);
2046 // Loop invariant: Variables up to this index have been checked for context
2048 uint32_t pruned_idx = original_size;
2051 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2052 const DIERef &die_ref = die_offsets[i];
2053 DWARFDIE die = GetDIE(die_ref);
2056 switch (die.Tag()) {
2058 case DW_TAG_subprogram:
2059 case DW_TAG_inlined_subroutine:
2060 case DW_TAG_try_block:
2061 case DW_TAG_catch_block:
2064 case DW_TAG_variable: {
2065 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2067 if (parent_decl_ctx) {
2068 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2070 CompilerDeclContext actual_parent_decl_ctx =
2071 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2072 if (!actual_parent_decl_ctx ||
2073 actual_parent_decl_ctx != *parent_decl_ctx)
2078 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2080 while (pruned_idx < variables.GetSize()) {
2081 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2082 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2085 variables.RemoveVariableAtIndex(pruned_idx);
2088 if (variables.GetSize() - original_size >= max_matches)
2093 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2094 name.GetStringRef());
2099 // Return the number of variable that were appended to the list
2100 const uint32_t num_matches = variables.GetSize() - original_size;
2101 if (log && num_matches > 0) {
2102 GetObjectFile()->GetModule()->LogMessage(
2104 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2105 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2106 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2107 max_matches, num_matches);
2112 uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression ®ex,
2113 uint32_t max_matches,
2114 VariableList &variables) {
2115 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2118 GetObjectFile()->GetModule()->LogMessage(
2120 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2121 "max_matches=%u, variables)",
2122 regex.GetText().str().c_str(), max_matches);
2125 DWARFDebugInfo *info = DebugInfo();
2129 // Remember how many variables are in the list before we search.
2130 const uint32_t original_size = variables.GetSize();
2132 DIEArray die_offsets;
2133 m_index->GetGlobalVariables(regex, die_offsets);
2136 sc.module_sp = m_obj_file->GetModule();
2137 assert(sc.module_sp);
2139 const size_t num_matches = die_offsets.size();
2141 for (size_t i = 0; i < num_matches; ++i) {
2142 const DIERef &die_ref = die_offsets[i];
2143 DWARFDIE die = GetDIE(die_ref);
2146 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2148 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2150 if (variables.GetSize() - original_size >= max_matches)
2153 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
2157 // Return the number of variable that were appended to the list
2158 return variables.GetSize() - original_size;
2161 bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2162 bool include_inlines,
2163 SymbolContextList &sc_list) {
2169 // If we were passed a die that is not a function, just return false...
2170 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2171 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2174 DWARFDIE die = orig_die;
2175 DWARFDIE inlined_die;
2176 if (die.Tag() == DW_TAG_inlined_subroutine) {
2180 die = die.GetParent();
2183 if (die.Tag() == DW_TAG_subprogram)
2189 assert(die && die.Tag() == DW_TAG_subprogram);
2190 if (GetFunction(die, sc)) {
2192 // Parse all blocks if needed
2194 Block &function_block = sc.function->GetBlock(true);
2195 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2196 if (sc.block == NULL)
2197 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2198 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2202 addr = sc.function->GetAddressRange().GetBaseAddress();
2205 if (addr.IsValid()) {
2214 bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2215 const DWARFDIE &die) {
2216 // If we have no parent decl context to match this DIE matches, and if the
2217 // parent decl context isn't valid, we aren't trying to look for any
2218 // particular decl context so any die matches.
2219 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2223 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2225 CompilerDeclContext actual_decl_ctx =
2226 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2227 if (actual_decl_ctx)
2228 return actual_decl_ctx == *decl_ctx;
2235 SymbolFileDWARF::FindFunctions(const ConstString &name,
2236 const CompilerDeclContext *parent_decl_ctx,
2237 uint32_t name_type_mask, bool include_inlines,
2238 bool append, SymbolContextList &sc_list) {
2239 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2240 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
2243 // eFunctionNameTypeAuto should be pre-resolved by a call to
2244 // Module::LookupInfo::LookupInfo()
2245 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2247 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2250 GetObjectFile()->GetModule()->LogMessage(
2251 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2252 "name_type_mask=0x%x, append=%u, sc_list)",
2253 name.GetCString(), name_type_mask, append);
2256 // If we aren't appending the results to this list, then clear the list
2260 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2263 // If name is empty then we won't find anything.
2267 // Remember how many sc_list are in the list before we search in case we are
2268 // appending the results to a variable list.
2270 const uint32_t original_size = sc_list.GetSize();
2272 DWARFDebugInfo *info = DebugInfo();
2276 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2278 CompilerDeclContext empty_decl_ctx;
2279 if (!parent_decl_ctx)
2280 parent_decl_ctx = &empty_decl_ctx;
2282 std::vector<DWARFDIE> dies;
2283 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2284 for (const DWARFDIE &die: dies) {
2285 if (resolved_dies.insert(die.GetDIE()).second)
2286 ResolveFunction(die, include_inlines, sc_list);
2289 // Return the number of variable that were appended to the list
2290 const uint32_t num_matches = sc_list.GetSize() - original_size;
2292 if (log && num_matches > 0) {
2293 GetObjectFile()->GetModule()->LogMessage(
2294 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2295 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2297 name.GetCString(), name_type_mask, include_inlines, append,
2303 uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression ®ex,
2304 bool include_inlines, bool append,
2305 SymbolContextList &sc_list) {
2306 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2307 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
2308 regex.GetText().str().c_str());
2310 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2313 GetObjectFile()->GetModule()->LogMessage(
2315 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2316 regex.GetText().str().c_str(), append);
2319 // If we aren't appending the results to this list, then clear the list
2323 DWARFDebugInfo *info = DebugInfo();
2327 // Remember how many sc_list are in the list before we search in case we are
2328 // appending the results to a variable list.
2329 uint32_t original_size = sc_list.GetSize();
2332 m_index->GetFunctions(regex, offsets);
2334 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2335 for (DIERef ref : offsets) {
2336 DWARFDIE die = info->GetDIE(ref);
2338 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2341 if (resolved_dies.insert(die.GetDIE()).second)
2342 ResolveFunction(die, include_inlines, sc_list);
2345 // Return the number of variable that were appended to the list
2346 return sc_list.GetSize() - original_size;
2349 void SymbolFileDWARF::GetMangledNamesForFunction(
2350 const std::string &scope_qualified_name,
2351 std::vector<ConstString> &mangled_names) {
2352 DWARFDebugInfo *info = DebugInfo();
2353 uint32_t num_comp_units = 0;
2355 num_comp_units = info->GetNumCompileUnits();
2357 for (uint32_t i = 0; i < num_comp_units; i++) {
2358 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
2362 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2364 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2367 NameToOffsetMap::iterator iter =
2368 m_function_scope_qualified_name_map.find(scope_qualified_name);
2369 if (iter == m_function_scope_qualified_name_map.end())
2372 DIERefSetSP set_sp = (*iter).second;
2373 std::set<DIERef>::iterator set_iter;
2374 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2375 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2376 mangled_names.push_back(ConstString(die.GetMangledName()));
2380 uint32_t SymbolFileDWARF::FindTypes(
2381 const SymbolContext &sc, const ConstString &name,
2382 const CompilerDeclContext *parent_decl_ctx, bool append,
2383 uint32_t max_matches,
2384 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2386 // If we aren't appending the results to this list, then clear the list
2390 // Make sure we haven't already searched this SymbolFile before...
2391 if (searched_symbol_files.count(this))
2394 searched_symbol_files.insert(this);
2396 DWARFDebugInfo *info = DebugInfo();
2400 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2403 if (parent_decl_ctx)
2404 GetObjectFile()->GetModule()->LogMessage(
2405 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2406 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2407 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2408 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2410 GetObjectFile()->GetModule()->LogMessage(
2411 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2412 "NULL, append=%u, max_matches=%u, type_list)",
2413 name.GetCString(), append, max_matches);
2416 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2419 DIEArray die_offsets;
2420 m_index->GetTypes(name, die_offsets);
2421 const size_t num_die_matches = die_offsets.size();
2423 if (num_die_matches) {
2424 const uint32_t initial_types_size = types.GetSize();
2425 for (size_t i = 0; i < num_die_matches; ++i) {
2426 const DIERef &die_ref = die_offsets[i];
2427 DWARFDIE die = GetDIE(die_ref);
2430 if (!DIEInDeclContext(parent_decl_ctx, die))
2431 continue; // The containing decl contexts don't match
2433 Type *matching_type = ResolveType(die, true, true);
2434 if (matching_type) {
2435 // We found a type pointer, now find the shared pointer form our type
2437 types.InsertUnique(matching_type->shared_from_this());
2438 if (types.GetSize() >= max_matches)
2442 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2443 name.GetStringRef());
2446 const uint32_t num_matches = types.GetSize() - initial_types_size;
2447 if (log && num_matches) {
2448 if (parent_decl_ctx) {
2449 GetObjectFile()->GetModule()->LogMessage(
2450 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2451 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2452 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2453 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2456 GetObjectFile()->GetModule()->LogMessage(
2457 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2458 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2459 name.GetCString(), append, max_matches, num_matches);
2464 UpdateExternalModuleListIfNeeded();
2466 for (const auto &pair : m_external_type_modules) {
2467 ModuleSP external_module_sp = pair.second;
2468 if (external_module_sp) {
2469 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2471 const uint32_t num_external_matches =
2472 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2473 max_matches, searched_symbol_files, types);
2474 if (num_external_matches)
2475 return num_external_matches;
2484 size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2485 bool append, TypeMap &types) {
2489 if (context.empty())
2492 ConstString name = context.back().name;
2497 DIEArray die_offsets;
2498 m_index->GetTypes(name, die_offsets);
2499 const size_t num_die_matches = die_offsets.size();
2501 if (num_die_matches) {
2502 size_t num_matches = 0;
2503 for (size_t i = 0; i < num_die_matches; ++i) {
2504 const DIERef &die_ref = die_offsets[i];
2505 DWARFDIE die = GetDIE(die_ref);
2508 std::vector<CompilerContext> die_context;
2509 die.GetDWOContext(die_context);
2510 if (die_context != context)
2513 Type *matching_type = ResolveType(die, true, true);
2514 if (matching_type) {
2515 // We found a type pointer, now find the shared pointer form our type
2517 types.InsertUnique(matching_type->shared_from_this());
2521 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2522 name.GetStringRef());
2531 SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2532 const CompilerDeclContext *parent_decl_ctx) {
2533 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2536 GetObjectFile()->GetModule()->LogMessage(
2537 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2541 CompilerDeclContext namespace_decl_ctx;
2543 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2544 return namespace_decl_ctx;
2546 DWARFDebugInfo *info = DebugInfo();
2548 DIEArray die_offsets;
2549 m_index->GetNamespaces(name, die_offsets);
2550 const size_t num_matches = die_offsets.size();
2552 for (size_t i = 0; i < num_matches; ++i) {
2553 const DIERef &die_ref = die_offsets[i];
2554 DWARFDIE die = GetDIE(die_ref);
2557 if (!DIEInDeclContext(parent_decl_ctx, die))
2558 continue; // The containing decl contexts don't match
2560 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2562 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2563 if (namespace_decl_ctx)
2567 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2568 name.GetStringRef());
2573 if (log && namespace_decl_ctx) {
2574 GetObjectFile()->GetModule()->LogMessage(
2575 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2576 "CompilerDeclContext(%p/%p) \"%s\"",
2578 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2579 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2580 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2583 return namespace_decl_ctx;
2586 TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2587 bool resolve_function_context) {
2590 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2591 if (type_ptr == NULL) {
2592 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2594 SymbolContext sc(lldb_cu);
2595 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2596 while (parent_die != nullptr) {
2597 if (parent_die->Tag() == DW_TAG_subprogram)
2599 parent_die = parent_die->GetParent();
2601 SymbolContext sc_backup = sc;
2602 if (resolve_function_context && parent_die != nullptr &&
2603 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2606 type_sp = ParseType(sc, die, NULL);
2607 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2608 // Grab the existing type from the master types lists
2609 type_sp = type_ptr->shared_from_this();
2616 SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2618 DWARFDIE die = orig_die;
2621 // If this is the original DIE that we are searching for a declaration
2622 // for, then don't look in the cache as we don't want our own decl
2623 // context to be our decl context...
2624 if (orig_die != die) {
2625 switch (die.Tag()) {
2626 case DW_TAG_compile_unit:
2627 case DW_TAG_partial_unit:
2628 case DW_TAG_namespace:
2629 case DW_TAG_structure_type:
2630 case DW_TAG_union_type:
2631 case DW_TAG_class_type:
2632 case DW_TAG_lexical_block:
2633 case DW_TAG_subprogram:
2635 case DW_TAG_inlined_subroutine: {
2636 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2647 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2649 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2651 return decl_ctx_die;
2654 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2656 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2658 return decl_ctx_die;
2661 die = die.GetParent();
2668 SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2669 Symbol *objc_class_symbol = NULL;
2671 Symtab *symtab = m_obj_file->GetSymtab();
2673 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2674 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2675 Symtab::eVisibilityAny);
2678 return objc_class_symbol;
2681 // Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
2682 // they don't then we can end up looking through all class types for a complete
2683 // type and never find the full definition. We need to know if this attribute
2684 // is supported, so we determine this here and cache th result. We also need to
2685 // worry about the debug map
2687 // if we are doing darwin DWARF in .o file debugging.
2688 bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
2690 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2691 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2692 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2693 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2695 DWARFDebugInfo *debug_info = DebugInfo();
2696 const uint32_t num_compile_units = GetNumCompileUnits();
2697 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2698 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2699 if (dwarf_cu != cu &&
2700 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2701 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2706 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2707 GetDebugMapSymfile())
2708 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2710 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
2713 // This function can be used when a DIE is found that is a forward declaration
2714 // DIE and we want to try and find a type that has the complete definition.
2715 TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2716 const DWARFDIE &die, const ConstString &type_name,
2717 bool must_be_implementation) {
2721 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2724 DIEArray die_offsets;
2725 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
2727 const size_t num_matches = die_offsets.size();
2730 for (size_t i = 0; i < num_matches; ++i) {
2731 const DIERef &die_ref = die_offsets[i];
2732 DWARFDIE type_die = GetDIE(die_ref);
2735 bool try_resolving_type = false;
2737 // Don't try and resolve the DIE we are looking for with the DIE
2739 if (type_die != die) {
2740 switch (type_die.Tag()) {
2741 case DW_TAG_class_type:
2742 case DW_TAG_structure_type:
2743 try_resolving_type = true;
2750 if (try_resolving_type) {
2751 if (must_be_implementation &&
2752 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2753 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2754 DW_AT_APPLE_objc_complete_type, 0);
2756 if (try_resolving_type) {
2757 Type *resolved_type = ResolveType(type_die, false, true);
2758 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2759 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2760 " (cu 0x%8.8" PRIx64 ")\n",
2762 m_obj_file->GetFileSpec().GetFilename().AsCString(
2764 type_die.GetID(), type_cu->GetID());
2767 GetDIEToType()[die.GetDIE()] = resolved_type;
2768 type_sp = resolved_type->shared_from_this();
2774 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2775 type_name.GetStringRef());
2782 //----------------------------------------------------------------------
2783 // This function helps to ensure that the declaration contexts match for two
2784 // different DIEs. Often times debug information will refer to a forward
2785 // declaration of a type (the equivalent of "struct my_struct;". There will
2786 // often be a declaration of that type elsewhere that has the full definition.
2787 // When we go looking for the full type "my_struct", we will find one or more
2788 // matches in the accelerator tables and we will then need to make sure the
2789 // type was in the same declaration context as the original DIE. This function
2790 // can efficiently compare two DIEs and will return true when the declaration
2791 // context matches, and false when they don't.
2792 //----------------------------------------------------------------------
2793 bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2794 const DWARFDIE &die2) {
2798 DWARFDIECollection decl_ctx_1;
2799 DWARFDIECollection decl_ctx_2;
2800 // The declaration DIE stack is a stack of the declaration context DIEs all
2801 // the way back to the compile unit. If a type "T" is declared inside a class
2802 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2803 // namespace "lldb", and the namespace is in a compile unit, there will be a
2806 // [0] DW_TAG_class_type for "B"
2807 // [1] DW_TAG_class_type for "A"
2808 // [2] DW_TAG_namespace for "lldb"
2809 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
2811 // We grab both contexts and make sure that everything matches all the way
2812 // back to the compiler unit.
2814 // First lets grab the decl contexts for both DIEs
2815 die1.GetDeclContextDIEs(decl_ctx_1);
2816 die2.GetDeclContextDIEs(decl_ctx_2);
2817 // Make sure the context arrays have the same size, otherwise we are done
2818 const size_t count1 = decl_ctx_1.Size();
2819 const size_t count2 = decl_ctx_2.Size();
2820 if (count1 != count2)
2823 // Make sure the DW_TAG values match all the way back up the compile unit. If
2824 // they don't, then we are done.
2825 DWARFDIE decl_ctx_die1;
2826 DWARFDIE decl_ctx_die2;
2828 for (i = 0; i < count1; i++) {
2829 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2830 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2831 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2834 #if defined LLDB_CONFIGURATION_DEBUG
2836 // Make sure the top item in the decl context die array is always
2837 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2838 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2840 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2841 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2842 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
2845 // Always skip the compile unit when comparing by only iterating up to "count
2846 // - 1". Here we compare the names as we go.
2847 for (i = 0; i < count1 - 1; i++) {
2848 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2849 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2850 const char *name1 = decl_ctx_die1.GetName();
2851 const char *name2 = decl_ctx_die2.GetName();
2852 // If the string was from a DW_FORM_strp, then the pointer will often be
2857 // Name pointers are not equal, so only compare the strings if both are not
2859 if (name1 && name2) {
2860 // If the strings don't compare, we are done...
2861 if (strcmp(name1, name2) != 0)
2864 // One name was NULL while the other wasn't
2868 // We made it through all of the checks and the declaration contexts are
2873 TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2874 const DWARFDeclContext &dwarf_decl_ctx) {
2877 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2878 if (dwarf_decl_ctx_count > 0) {
2879 const ConstString type_name(dwarf_decl_ctx[0].name);
2880 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
2883 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2884 DWARF_LOG_LOOKUPS));
2886 GetObjectFile()->GetModule()->LogMessage(
2887 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2888 "s, qualified-name='%s')",
2889 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2890 dwarf_decl_ctx.GetQualifiedName());
2893 DIEArray die_offsets;
2894 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
2895 const size_t num_matches = die_offsets.size();
2897 // Get the type system that we are looking to find a type for. We will
2898 // use this to ensure any matches we find are in a language that this
2899 // type system supports
2900 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2901 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2903 : GetTypeSystemForLanguage(language);
2906 for (size_t i = 0; i < num_matches; ++i) {
2907 const DIERef &die_ref = die_offsets[i];
2908 DWARFDIE type_die = GetDIE(die_ref);
2911 // Make sure type_die's langauge matches the type system we are
2912 // looking for. We don't want to find a "Foo" type from Java if we
2913 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
2915 !type_system->SupportsLanguage(type_die.GetLanguage()))
2917 bool try_resolving_type = false;
2919 // Don't try and resolve the DIE we are looking for with the DIE
2921 const dw_tag_t type_tag = type_die.Tag();
2922 // Make sure the tags match
2923 if (type_tag == tag) {
2924 // The tags match, lets try resolving this type
2925 try_resolving_type = true;
2927 // The tags don't match, but we need to watch our for a forward
2928 // declaration for a struct and ("struct foo") ends up being a
2929 // class ("class foo { ... };") or vice versa.
2931 case DW_TAG_class_type:
2932 // We had a "class foo", see if we ended up with a "struct foo
2934 try_resolving_type = (tag == DW_TAG_structure_type);
2936 case DW_TAG_structure_type:
2937 // We had a "struct foo", see if we ended up with a "class foo
2939 try_resolving_type = (tag == DW_TAG_class_type);
2942 // Tags don't match, don't event try to resolve using this type
2943 // whose name matches....
2948 if (try_resolving_type) {
2949 DWARFDeclContext type_dwarf_decl_ctx;
2950 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2953 GetObjectFile()->GetModule()->LogMessage(
2954 log, "SymbolFileDWARF::"
2955 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2956 "qualified-name='%s') trying die=0x%8.8x (%s)",
2957 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2958 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2959 type_dwarf_decl_ctx.GetQualifiedName());
2962 // Make sure the decl contexts match all the way up
2963 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2964 Type *resolved_type = ResolveType(type_die, false);
2965 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2966 type_sp = resolved_type->shared_from_this();
2972 std::string qualified_name;
2973 type_die.GetQualifiedName(qualified_name);
2974 GetObjectFile()->GetModule()->LogMessage(
2975 log, "SymbolFileDWARF::"
2976 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2977 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2978 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2979 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2980 qualified_name.c_str());
2984 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2985 type_name.GetStringRef());
2994 TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2995 bool *type_is_new_ptr) {
2999 TypeSystem *type_system =
3000 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
3003 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3005 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3006 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3008 TypeList *type_list = GetTypeList();
3010 type_list->Insert(type_sp);
3012 if (die.Tag() == DW_TAG_subprogram) {
3013 DIERef die_ref = die.GetDIERef();
3014 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3015 .GetScopeQualifiedName()
3017 if (scope_qualified_name.size()) {
3018 NameToOffsetMap::iterator iter =
3019 m_function_scope_qualified_name_map.find(
3020 scope_qualified_name);
3021 if (iter != m_function_scope_qualified_name_map.end())
3022 (*iter).second->insert(die_ref);
3024 DIERefSetSP new_set(new std::set<DIERef>);
3025 new_set->insert(die_ref);
3026 m_function_scope_qualified_name_map.emplace(
3027 std::make_pair(scope_qualified_name, new_set));
3039 size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3040 const DWARFDIE &orig_die,
3041 bool parse_siblings, bool parse_children) {
3042 size_t types_added = 0;
3043 DWARFDIE die = orig_die;
3045 bool type_is_new = false;
3046 if (ParseType(sc, die, &type_is_new).get()) {
3051 if (parse_children && die.HasChildren()) {
3052 if (die.Tag() == DW_TAG_subprogram) {
3053 SymbolContext child_sc(sc);
3054 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3055 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3057 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3061 die = die.GetSibling();
3068 size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3069 assert(sc.comp_unit && sc.function);
3070 size_t functions_added = 0;
3071 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3073 const dw_offset_t function_die_offset = sc.function->GetID();
3074 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3076 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3077 LLDB_INVALID_ADDRESS, 0);
3081 return functions_added;
3084 size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3085 // At least a compile unit must be valid
3086 assert(sc.comp_unit);
3087 size_t types_added = 0;
3088 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3091 dw_offset_t function_die_offset = sc.function->GetID();
3092 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3093 if (func_die && func_die.HasChildren()) {
3094 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3097 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3098 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3099 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3107 size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3108 if (sc.comp_unit != NULL) {
3109 DWARFDebugInfo *info = DebugInfo();
3114 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3116 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3117 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3118 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3119 const size_t num_variables = ParseVariables(
3120 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3122 // Let all blocks know they have parse all their variables
3123 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3124 return num_variables;
3126 } else if (sc.comp_unit) {
3127 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3129 if (dwarf_cu == NULL)
3132 uint32_t vars_added = 0;
3133 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3135 if (variables.get() == NULL) {
3136 variables.reset(new VariableList());
3137 sc.comp_unit->SetVariableList(variables);
3139 DIEArray die_offsets;
3140 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
3141 const size_t num_matches = die_offsets.size();
3143 for (size_t i = 0; i < num_matches; ++i) {
3144 const DIERef &die_ref = die_offsets[i];
3145 DWARFDIE die = GetDIE(die_ref);
3148 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3150 variables->AddVariableIfUnique(var_sp);
3154 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
3164 VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3165 const DWARFDIE &die,
3166 const lldb::addr_t func_low_pc) {
3167 if (die.GetDWARF() != this)
3168 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
3174 var_sp = GetDIEToVariable()[die.GetDIE()];
3176 return var_sp; // Already been parsed!
3178 const dw_tag_t tag = die.Tag();
3179 ModuleSP module = GetObjectFile()->GetModule();
3181 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3182 (tag == DW_TAG_formal_parameter && sc.function)) {
3183 DWARFAttributes attributes;
3184 const size_t num_attributes = die.GetAttributes(attributes);
3186 if (num_attributes > 0) {
3187 const char *name = NULL;
3188 const char *mangled = NULL;
3191 DWARFFormValue type_die_form;
3192 DWARFExpression location(die.GetCU());
3193 bool is_external = false;
3194 bool is_artificial = false;
3195 bool location_is_const_value_data = false;
3196 bool has_explicit_location = false;
3197 DWARFFormValue const_value;
3198 Variable::RangeList scope_ranges;
3199 // AccessType accessibility = eAccessNone;
3201 for (i = 0; i < num_attributes; ++i) {
3202 dw_attr_t attr = attributes.AttributeAtIndex(i);
3203 DWARFFormValue form_value;
3205 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3207 case DW_AT_decl_file:
3208 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3209 form_value.Unsigned()));
3211 case DW_AT_decl_line:
3212 decl.SetLine(form_value.Unsigned());
3214 case DW_AT_decl_column:
3215 decl.SetColumn(form_value.Unsigned());
3218 name = form_value.AsCString();
3220 case DW_AT_linkage_name:
3221 case DW_AT_MIPS_linkage_name:
3222 mangled = form_value.AsCString();
3225 type_die_form = form_value;
3227 case DW_AT_external:
3228 is_external = form_value.Boolean();
3230 case DW_AT_const_value:
3231 // If we have already found a DW_AT_location attribute, ignore this
3233 if (!has_explicit_location) {
3234 location_is_const_value_data = true;
3235 // The constant value will be either a block, a data value or a
3237 auto debug_info_data = die.GetData();
3238 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3239 // Retrieve the value as a block expression.
3240 uint32_t block_offset =
3241 form_value.BlockData() - debug_info_data.GetDataStart();
3242 uint32_t block_length = form_value.Unsigned();
3243 location.CopyOpcodeData(module, debug_info_data, block_offset,
3245 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3246 // Retrieve the value as a data expression.
3247 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3248 DWARFFormValue::GetFixedFormSizesForAddressSize(
3249 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3250 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3251 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3252 uint32_t data_length =
3253 fixed_form_sizes.GetSize(form_value.Form());
3254 if (data_length == 0) {
3255 const uint8_t *data_pointer = form_value.BlockData();
3257 form_value.Unsigned();
3258 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3259 // we need to get the byte size of the type later after we
3260 // create the variable
3261 const_value = form_value;
3264 location.CopyOpcodeData(module, debug_info_data, data_offset,
3267 // Retrieve the value as a string expression.
3268 if (form_value.Form() == DW_FORM_strp) {
3269 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3270 DWARFFormValue::GetFixedFormSizesForAddressSize(
3271 attributes.CompileUnitAtIndex(i)
3272 ->GetAddressByteSize(),
3273 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3274 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3275 uint32_t data_length =
3276 fixed_form_sizes.GetSize(form_value.Form());
3277 location.CopyOpcodeData(module, debug_info_data, data_offset,
3280 const char *str = form_value.AsCString();
3281 uint32_t string_offset =
3282 str - (const char *)debug_info_data.GetDataStart();
3283 uint32_t string_length = strlen(str) + 1;
3284 location.CopyOpcodeData(module, debug_info_data,
3285 string_offset, string_length);
3290 case DW_AT_location: {
3291 location_is_const_value_data = false;
3292 has_explicit_location = true;
3293 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3294 auto data = die.GetData();
3296 uint32_t block_offset =
3297 form_value.BlockData() - data.GetDataStart();
3298 uint32_t block_length = form_value.Unsigned();
3299 location.CopyOpcodeData(module, data, block_offset, block_length);
3301 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3302 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3304 size_t loc_list_length = DWARFExpression::LocationListSize(
3305 die.GetCU(), debug_loc_data, debug_loc_offset);
3306 if (loc_list_length > 0) {
3307 location.CopyOpcodeData(module, debug_loc_data,
3308 debug_loc_offset, loc_list_length);
3309 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3310 location.SetLocationListSlide(
3312 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3316 case DW_AT_specification:
3317 spec_die = GetDIE(DIERef(form_value));
3319 case DW_AT_start_scope: {
3320 if (form_value.Form() == DW_FORM_sec_offset) {
3321 DWARFRangeList dwarf_scope_ranges;
3322 const DWARFDebugRanges *debug_ranges = DebugRanges();
3323 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3324 form_value.Unsigned(),
3325 dwarf_scope_ranges);
3327 // All DW_AT_start_scope are relative to the base address of the
3328 // compile unit. We add the compile unit base address to make
3329 // sure all the addresses are properly fixed up.
3330 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3332 const DWARFRangeList::Entry &range =
3333 dwarf_scope_ranges.GetEntryRef(i);
3334 scope_ranges.Append(range.GetRangeBase() +
3335 die.GetCU()->GetBaseAddress(),
3336 range.GetByteSize());
3339 // TODO: Handle the case when DW_AT_start_scope have form
3341 // dwarf spec is a bit ambiguous about what is the expected
3342 // behavior in case the enclosing block have a non coninious
3343 // address range and the DW_AT_start_scope entry have a form
3345 GetObjectFile()->GetModule()->ReportWarning(
3347 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3348 die.GetID(), form_value.Form());
3351 scope_ranges.Sort();
3352 scope_ranges.CombineConsecutiveRanges();
3354 case DW_AT_artificial:
3355 is_artificial = form_value.Boolean();
3357 case DW_AT_accessibility:
3358 break; // accessibility =
3359 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3360 case DW_AT_declaration:
3361 case DW_AT_description:
3362 case DW_AT_endianity:
3364 case DW_AT_visibility:
3366 case DW_AT_abstract_origin:
3373 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3374 const dw_tag_t parent_tag = die.GetParent().Tag();
3375 bool is_static_member =
3376 (parent_tag == DW_TAG_compile_unit ||
3377 parent_tag == DW_TAG_partial_unit) &&
3378 (parent_context_die.Tag() == DW_TAG_class_type ||
3379 parent_context_die.Tag() == DW_TAG_structure_type);
3381 ValueType scope = eValueTypeInvalid;
3383 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3384 SymbolContextScope *symbol_context_scope = NULL;
3386 bool has_explicit_mangled = mangled != nullptr;
3388 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3389 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3390 // of global variables with commands like "frame var j". For
3391 // example, if j were an int variable holding a value 4 and
3392 // declared in a namespace B which in turn is contained in a
3393 // namespace A, the command "frame var j" returns
3394 // "(int) A::B::j = 4".
3395 // If the compiler does not emit a linkage name, we should be
3396 // able to generate a fully qualified name from the
3397 // declaration context.
3398 if ((parent_tag == DW_TAG_compile_unit ||
3399 parent_tag == DW_TAG_partial_unit) &&
3400 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3401 DWARFDeclContext decl_ctx;
3403 die.GetDWARFDeclContext(decl_ctx);
3404 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3408 if (tag == DW_TAG_formal_parameter)
3409 scope = eValueTypeVariableArgument;
3411 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3412 // or static variable, so we have to do a little digging:
3413 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3414 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3415 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3416 // Clang likes to combine small global variables into the same symbol
3417 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3418 // so we need to look through the whole expression.
3419 bool is_static_lifetime =
3420 has_explicit_mangled ||
3421 (has_explicit_location && !location.IsValid());
3422 // Check if the location has a DW_OP_addr with any address value...
3423 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3424 if (!location_is_const_value_data) {
3425 bool op_error = false;
3426 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3429 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3431 GetObjectFile()->GetModule()->ReportError(
3432 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
3433 die.GetTagAsCString(), strm.GetData());
3435 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3436 is_static_lifetime = true;
3438 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3440 if (is_static_lifetime) {
3442 scope = eValueTypeVariableGlobal;
3444 scope = eValueTypeVariableStatic;
3446 if (debug_map_symfile) {
3447 // When leaving the DWARF in the .o files on darwin, when we have a
3448 // global variable that wasn't initialized, the .o file might not
3449 // have allocated a virtual address for the global variable. In
3450 // this case it will have created a symbol for the global variable
3451 // that is undefined/data and external and the value will be the
3452 // byte size of the variable. When we do the address map in
3453 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3454 // do some magic here so we can get the correct address for our
3455 // global variable. The address for all of these entries will be
3456 // zero, and there will be an undefined symbol in this object file,
3457 // and the executable will have a matching symbol with a good
3458 // address. So here we dig up the correct address and replace it in
3459 // the location for the variable, and set the variable's symbol
3460 // context scope to be that of the main executable so the file
3461 // address will resolve correctly.
3462 bool linked_oso_file_addr = false;
3463 if (is_external && location_DW_OP_addr == 0) {
3464 // we have a possible uninitialized extern global
3465 ConstString const_name(mangled ? mangled : name);
3466 ObjectFile *debug_map_objfile =
3467 debug_map_symfile->GetObjectFile();
3468 if (debug_map_objfile) {
3469 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3470 if (debug_map_symtab) {
3471 Symbol *exe_symbol =
3472 debug_map_symtab->FindFirstSymbolWithNameAndType(
3473 const_name, eSymbolTypeData, Symtab::eDebugYes,
3474 Symtab::eVisibilityExtern);
3476 if (exe_symbol->ValueIsAddress()) {
3477 const addr_t exe_file_addr =
3478 exe_symbol->GetAddressRef().GetFileAddress();
3479 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3480 if (location.Update_DW_OP_addr(exe_file_addr)) {
3481 linked_oso_file_addr = true;
3482 symbol_context_scope = exe_symbol;
3491 if (!linked_oso_file_addr) {
3492 // The DW_OP_addr is not zero, but it contains a .o file address
3493 // which needs to be linked up correctly.
3494 const lldb::addr_t exe_file_addr =
3495 debug_map_symfile->LinkOSOFileAddress(this,
3496 location_DW_OP_addr);
3497 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3498 // Update the file address for this variable
3499 location.Update_DW_OP_addr(exe_file_addr);
3501 // Variable didn't make it into the final executable
3507 if (location_is_const_value_data)
3508 scope = eValueTypeVariableStatic;
3510 scope = eValueTypeVariableLocal;
3511 if (debug_map_symfile) {
3512 // We need to check for TLS addresses that we need to fixup
3513 if (location.ContainsThreadLocalStorage()) {
3514 location.LinkThreadLocalStorage(
3515 debug_map_symfile->GetObjectFile()->GetModule(),
3516 [this, debug_map_symfile](
3517 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3518 return debug_map_symfile->LinkOSOFileAddress(
3519 this, unlinked_file_addr);
3521 scope = eValueTypeVariableThreadLocal;
3528 if (symbol_context_scope == NULL) {
3529 switch (parent_tag) {
3530 case DW_TAG_subprogram:
3531 case DW_TAG_inlined_subroutine:
3532 case DW_TAG_lexical_block:
3534 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3535 sc_parent_die.GetID());
3536 if (symbol_context_scope == NULL)
3537 symbol_context_scope = sc.function;
3542 symbol_context_scope = sc.comp_unit;
3547 if (symbol_context_scope) {
3548 SymbolFileTypeSP type_sp(
3549 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3551 if (const_value.Form() && type_sp && type_sp->GetType())
3552 location.CopyOpcodeData(const_value.Unsigned(),
3553 type_sp->GetType()->GetByteSize(),
3554 die.GetCU()->GetAddressByteSize());
3556 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3557 symbol_context_scope, scope_ranges, &decl,
3558 location, is_external, is_artificial,
3561 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3563 // Not ready to parse this variable yet. It might be a global or static
3564 // variable that is in a function scope and the function in the symbol
3565 // context wasn't filled in yet
3569 // Cache var_sp even if NULL (the variable was just a specification or was
3570 // missing vital information to be able to be displayed in the debugger
3571 // (missing location due to optimization, etc)) so we don't re-parse this
3572 // DIE over and over later...
3573 GetDIEToVariable()[die.GetDIE()] = var_sp;
3575 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3581 SymbolFileDWARF::FindBlockContainingSpecification(
3582 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3583 // Give the concrete function die specified by "func_die_offset", find the
3584 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3585 // to "spec_block_die_offset"
3586 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3587 spec_block_die_offset);
3591 SymbolFileDWARF::FindBlockContainingSpecification(
3592 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3594 switch (die.Tag()) {
3595 case DW_TAG_subprogram:
3596 case DW_TAG_inlined_subroutine:
3597 case DW_TAG_lexical_block: {
3598 if (die.GetAttributeValueAsReference(
3599 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3602 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3603 DW_INVALID_OFFSET) ==
3604 spec_block_die_offset)
3609 // Give the concrete function die specified by "func_die_offset", find the
3610 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3611 // to "spec_block_die_offset"
3612 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3613 child_die = child_die.GetSibling()) {
3614 DWARFDIE result_die =
3615 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3624 size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3625 const DWARFDIE &orig_die,
3626 const lldb::addr_t func_low_pc,
3627 bool parse_siblings, bool parse_children,
3628 VariableList *cc_variable_list) {
3632 VariableListSP variable_list_sp;
3634 size_t vars_added = 0;
3635 DWARFDIE die = orig_die;
3637 dw_tag_t tag = die.Tag();
3639 // Check to see if we have already parsed this variable or constant?
3640 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3642 if (cc_variable_list)
3643 cc_variable_list->AddVariableIfUnique(var_sp);
3645 // We haven't already parsed it, lets do that now.
3646 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3647 (tag == DW_TAG_formal_parameter && sc.function)) {
3648 if (variable_list_sp.get() == NULL) {
3649 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3650 dw_tag_t parent_tag = sc_parent_die.Tag();
3651 switch (parent_tag) {
3652 case DW_TAG_compile_unit:
3653 case DW_TAG_partial_unit:
3654 if (sc.comp_unit != NULL) {
3655 variable_list_sp = sc.comp_unit->GetVariableList(false);
3656 if (variable_list_sp.get() == NULL) {
3657 variable_list_sp.reset(new VariableList());
3660 GetObjectFile()->GetModule()->ReportError(
3661 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3662 "symbol context for 0x%8.8" PRIx64
3664 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3665 orig_die.GetID(), orig_die.GetTagAsCString());
3669 case DW_TAG_subprogram:
3670 case DW_TAG_inlined_subroutine:
3671 case DW_TAG_lexical_block:
3672 if (sc.function != NULL) {
3673 // Check to see if we already have parsed the variables for the
3676 Block *block = sc.function->GetBlock(true).FindBlockByID(
3677 sc_parent_die.GetID());
3678 if (block == NULL) {
3679 // This must be a specification or abstract origin with a
3680 // concrete block counterpart in the current function. We need
3681 // to find the concrete block so we can correctly add the
3683 const DWARFDIE concrete_block_die =
3684 FindBlockContainingSpecification(
3685 DIERef(sc.function->GetID(), this),
3686 sc_parent_die.GetOffset());
3687 if (concrete_block_die)
3688 block = sc.function->GetBlock(true).FindBlockByID(
3689 concrete_block_die.GetID());
3692 if (block != NULL) {
3693 const bool can_create = false;
3694 variable_list_sp = block->GetBlockVariableList(can_create);
3695 if (variable_list_sp.get() == NULL) {
3696 variable_list_sp.reset(new VariableList());
3697 block->SetVariableList(variable_list_sp);
3704 GetObjectFile()->GetModule()->ReportError(
3705 "didn't find appropriate parent DIE for variable list for "
3706 "0x%8.8" PRIx64 " %s.\n",
3707 orig_die.GetID(), orig_die.GetTagAsCString());
3712 if (variable_list_sp) {
3713 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3715 variable_list_sp->AddVariableIfUnique(var_sp);
3716 if (cc_variable_list)
3717 cc_variable_list->AddVariableIfUnique(var_sp);
3724 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3726 if (!skip_children && parse_children && die.HasChildren()) {
3727 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3728 true, cc_variable_list);
3732 die = die.GetSibling();
3739 //------------------------------------------------------------------
3740 // PluginInterface protocol
3741 //------------------------------------------------------------------
3742 ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3744 uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3746 void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
3748 SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3749 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3750 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3752 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3754 m_debug_map_symfile =
3755 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
3758 return m_debug_map_symfile;
3761 DWARFExpression::LocationListFormat
3762 SymbolFileDWARF::GetLocationListFormat() const {
3763 return DWARFExpression::RegularLocationList;
3766 SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3767 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
3768 ModuleSpec module_spec;
3769 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3770 module_spec.GetSymbolFileSpec() =
3771 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
3772 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
3773 if (dwp_filespec.Exists()) {
3774 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3778 return m_dwp_symfile.get();