blob: 5593685b353b95ff7213aef765d23dbd4e39964b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolFileDWARF.h"
10
Sean Callanancc427fa2011-07-30 02:42:06 +000011#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000012#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000013
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000015#include "lldb/Core/ModuleList.h"
16#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000019#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000021#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000022#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000023#include "lldb/Utility/Scalar.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000025#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanan4dbb2712015-09-25 20:35:58 +000027#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Pavel Labathe1d18752018-06-07 10:04:44 +000028#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000029
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000030#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000031#include "lldb/Host/Host.h"
32
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000033#include "lldb/Interpreter/OptionValueFileSpecList.h"
34#include "lldb/Interpreter/OptionValueProperties.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000037#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000038#include "lldb/Symbol/ClangUtil.h"
39#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000040#include "lldb/Symbol/CompilerDecl.h"
41#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000042#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000043#include "lldb/Symbol/LineTable.h"
Zachary Turner80552912019-02-27 21:42:10 +000044#include "lldb/Symbol/LocateSymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000047#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000048#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Symbol/VariableList.h"
50
Jim Ingham0e0984e2015-09-02 01:06:46 +000051#include "lldb/Target/Language.h"
Zachary Turnera89ce432019-03-06 18:20:23 +000052#include "lldb/Target/Target.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000053
Pavel Labathb13f0332018-05-21 14:12:52 +000054#include "AppleDWARFIndex.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000055#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000056#include "DWARFASTParserClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "DWARFDebugAbbrev.h"
58#include "DWARFDebugAranges.h"
59#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000061#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000063#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064#include "DWARFFormValue.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000065#include "DWARFUnit.h"
Pavel Labath9337b412018-06-06 11:35:23 +000066#include "DebugNamesDWARFIndex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "LogChannelDWARF.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000068#include "ManualDWARFIndex.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000069#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000070#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000071#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Zachary Turner7d86ee52017-03-08 17:56:08 +000073#include "llvm/Support/FileSystem.h"
74
Adrian Prantl0f30a3b2019-02-13 18:10:41 +000075#include <algorithm>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076#include <map>
Jonas Devlieghere796ac802019-02-11 23:13:08 +000077#include <memory>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Matthew Gardinere81df3b2014-08-26 06:57:23 +000079#include <ctype.h>
80#include <string.h>
81
Greg Clayton62742b12010-11-11 01:09:45 +000082//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000083
84#ifdef ENABLE_DEBUG_PRINTF
85#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000086#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000087#else
88#define DEBUG_PRINTF(fmt, ...)
89#endif
90
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091using namespace lldb;
92using namespace lldb_private;
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094// static inline bool
95// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000096//{
97// switch (tag)
98// {
99// default:
100// break;
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:
106// return true;
107// }
108// return false;
109//}
110//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000111
112namespace {
113
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000114static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000116 {},
Pavel Labath9337b412018-06-06 11:35:23 +0000117 "If the DW_AT_comp_dir matches any of these paths the symbolic "
118 "links will be resolved at DWARF parse time."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000119 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
Pavel Labath9337b412018-06-06 11:35:23 +0000120 "Ignore indexes present in the object files and always index DWARF "
Tatyana Krasnukhae40db052018-09-27 07:11:58 +0000121 "manually."}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000122
Pavel Labath9337b412018-06-06 11:35:23 +0000123enum {
124 ePropertySymLinkPaths,
125 ePropertyIgnoreIndexes,
126};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128class PluginProperties : public Properties {
129public:
130 static ConstString GetSettingName() {
131 return SymbolFileDWARF::GetPluginNameStatic();
132 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 PluginProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000135 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 m_collection_sp->Initialize(g_properties);
137 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 FileSpecList &GetSymLinkPaths() {
140 OptionValueFileSpecList *option_value =
141 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
142 nullptr, true, ePropertySymLinkPaths);
143 assert(option_value);
144 return option_value->GetCurrentValue();
145 }
Pavel Labath9337b412018-06-06 11:35:23 +0000146
147 bool IgnoreFileIndexes() const {
148 return m_collection_sp->GetPropertyAtIndexAsBoolean(
149 nullptr, ePropertyIgnoreIndexes, false);
150 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
156 static const auto g_settings_sp(std::make_shared<PluginProperties>());
157 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000161
Pavel Labath7d36d722019-01-16 12:30:41 +0000162const FileSpecList &SymbolFileDWARF::GetSymlinkPaths() {
163 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000164}
165
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000166DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000167 return nullptr;
168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170void SymbolFileDWARF::Initialize() {
171 LogChannelDWARF::Initialize();
172 PluginManager::RegisterPlugin(GetPluginNameStatic(),
173 GetPluginDescriptionStatic(), CreateInstance,
174 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000175}
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
178 if (!PluginManager::GetSettingForSymbolFilePlugin(
179 debugger, PluginProperties::GetSettingName())) {
180 const bool is_global_setting = true;
181 PluginManager::CreateSettingForSymbolFilePlugin(
182 debugger, GetGlobalPluginProperties()->GetValueProperties(),
183 ConstString("Properties for the dwarf symbol-file plug-in."),
184 is_global_setting);
185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188void SymbolFileDWARF::Terminate() {
189 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000190 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191}
192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
194 static ConstString g_name("dwarf");
195 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
199 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
203 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204}
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000207 // This method can be called without going through the symbol vendor so we
208 // need to lock the module.
209 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
211 if (debug_map_symfile)
212 return debug_map_symfile->GetTypeList();
213 else
214 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000215}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
217 dw_offset_t max_die_offset, uint32_t type_mask,
218 TypeSet &type_set) {
219 if (die) {
220 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 if (die_offset >= max_die_offset)
223 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (die_offset >= min_die_offset) {
226 const dw_tag_t tag = die.Tag();
227
228 bool add_type = false;
229
230 switch (tag) {
231 case DW_TAG_array_type:
232 add_type = (type_mask & eTypeClassArray) != 0;
233 break;
234 case DW_TAG_unspecified_type:
235 case DW_TAG_base_type:
236 add_type = (type_mask & eTypeClassBuiltin) != 0;
237 break;
238 case DW_TAG_class_type:
239 add_type = (type_mask & eTypeClassClass) != 0;
240 break;
241 case DW_TAG_structure_type:
242 add_type = (type_mask & eTypeClassStruct) != 0;
243 break;
244 case DW_TAG_union_type:
245 add_type = (type_mask & eTypeClassUnion) != 0;
246 break;
247 case DW_TAG_enumeration_type:
248 add_type = (type_mask & eTypeClassEnumeration) != 0;
249 break;
250 case DW_TAG_subroutine_type:
251 case DW_TAG_subprogram:
252 case DW_TAG_inlined_subroutine:
253 add_type = (type_mask & eTypeClassFunction) != 0;
254 break;
255 case DW_TAG_pointer_type:
256 add_type = (type_mask & eTypeClassPointer) != 0;
257 break;
258 case DW_TAG_rvalue_reference_type:
259 case DW_TAG_reference_type:
260 add_type = (type_mask & eTypeClassReference) != 0;
261 break;
262 case DW_TAG_typedef:
263 add_type = (type_mask & eTypeClassTypedef) != 0;
264 break;
265 case DW_TAG_ptr_to_member_type:
266 add_type = (type_mask & eTypeClassMemberPointer) != 0;
267 break;
268 }
269
270 if (add_type) {
271 const bool assert_not_being_parsed = true;
272 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
273 if (type) {
274 if (type_set.find(type) == type_set.end())
275 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279
280 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
281 child_die = child_die.GetSibling()) {
282 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
283 }
284 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000285}
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000288 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000289
290{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000291 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000295 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 if (sc_scope)
297 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 if (comp_unit) {
300 dwarf_cu = GetDWARFCompileUnit(comp_unit);
301 if (dwarf_cu == 0)
302 return 0;
303 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
304 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
305 } else {
306 DWARFDebugInfo *info = DebugInfo();
307 if (info) {
308 const size_t num_cus = info->GetNumCompileUnits();
309 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
310 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
311 if (dwarf_cu) {
312 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 }
317
318 std::set<CompilerType> compiler_type_set;
319 size_t num_types_added = 0;
320 for (Type *type : type_set) {
321 CompilerType compiler_type = type->GetForwardCompilerType();
322 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
323 compiler_type_set.insert(compiler_type);
324 type_list.Insert(type->shared_from_this());
325 ++num_types_added;
326 }
327 }
328 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000329}
330
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331//----------------------------------------------------------------------
332// Gets the first parent that is a lexical block, function or inlined
333// subroutine, or compile unit.
334//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000335DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
337 DWARFDIE die;
338 for (die = child_die.GetParent(); die; die = die.GetParent()) {
339 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 switch (tag) {
342 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000343 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 case DW_TAG_subprogram:
345 case DW_TAG_inlined_subroutine:
346 case DW_TAG_lexical_block:
347 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 }
350 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000354 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
355 << 32), // Used by SymbolFileDWARFDebugMap to
356 // when this class parses .o files to
357 // contain the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
359 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
360 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
George Rimar6e357122018-10-10 08:11:15 +0000361 m_data_debug_ranges(), m_data_debug_rnglists(), m_data_debug_str(),
362 m_data_apple_names(), m_data_apple_types(), m_data_apple_namespaces(),
363 m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
365 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367SymbolFileDWARF::~SymbolFileDWARF() {}
368
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000369static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 static ConstString g_dwarf_section_name("__DWARF");
371 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
375 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
376 if (debug_map_symfile)
377 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
378 else
379 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380}
381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
383 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
384 TypeSystem *type_system;
385 if (debug_map_symfile) {
386 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
387 } else {
388 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
389 if (type_system)
390 type_system->SetSymbolFile(this);
391 }
392 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000393}
394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000396 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 ModuleSP module_sp(m_obj_file->GetModule());
398 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000399 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000400 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000404 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 }
406
Pavel Labath9337b412018-06-06 11:35:23 +0000407 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
408 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
409 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
410 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
411 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
412 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413
Pavel Labath9337b412018-06-06 11:35:23 +0000414 m_index = AppleDWARFIndex::Create(
415 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
416 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417
Pavel Labath9337b412018-06-06 11:35:23 +0000418 if (m_index)
419 return;
420
421 DWARFDataExtractor debug_names;
422 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
423 if (debug_names.GetByteSize() > 0) {
424 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
425 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
426 debug_names, get_debug_str_data(),
427 DebugInfo());
428 if (index_or) {
429 m_index = std::move(*index_or);
430 return;
431 }
432 LLDB_LOG_ERROR(log, index_or.takeError(),
433 "Unable to read .debug_names data: {0}");
434 }
435 }
436
437 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
438 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439}
440
441bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000442 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443}
444
445uint32_t SymbolFileDWARF::CalculateAbilities() {
446 uint32_t abilities = 0;
447 if (m_obj_file != NULL) {
448 const Section *section = NULL;
449 const SectionList *section_list = m_obj_file->GetSectionList();
450 if (section_list == NULL)
451 return 0;
452
Adrian Prantl05097242018-04-30 16:49:04 +0000453 // On non Apple platforms we might have .debug_types debug info that is
454 // created by using "-fdebug-types-section". LLDB currently will try to
455 // load this debug info, but it causes crashes during debugging when types
456 // are missing since it doesn't know how to parse the info in the
457 // .debug_types type units. This causes all complex debug info types to be
458 // unresolved. Because this causes LLDB to crash and since it really
459 // doesn't provide a solid debuggiung experience, we should disable trying
460 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000461 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
462 m_obj_file->GetModule()->ReportWarning(
463 "lldb doesn’t support .debug_types debug info");
464 return 0;
465 }
466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 uint64_t debug_abbrev_file_size = 0;
468 uint64_t debug_info_file_size = 0;
469 uint64_t debug_line_file_size = 0;
470
471 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
472
473 if (section)
474 section_list = &section->GetChildren();
475
476 section =
477 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
478 if (section != NULL) {
479 debug_info_file_size = section->GetFileSize();
480
481 section =
482 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
483 .get();
484 if (section)
485 debug_abbrev_file_size = section->GetFileSize();
486
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000487 DWARFDebugAbbrev *abbrev = DebugAbbrev();
488 if (abbrev) {
489 std::set<dw_form_t> invalid_forms;
490 abbrev->GetUnsupportedForms(invalid_forms);
491 if (!invalid_forms.empty()) {
492 StreamString error;
493 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
494 for (auto form : invalid_forms)
495 error.Printf(" %#x", form);
496 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
497 return 0;
498 }
499 }
500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 section =
502 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
503 .get();
504 if (section)
505 debug_line_file_size = section->GetFileSize();
506 } else {
507 const char *symfile_dir_cstr =
508 m_obj_file->GetFileSpec().GetDirectory().GetCString();
509 if (symfile_dir_cstr) {
510 if (strcasestr(symfile_dir_cstr, ".dsym")) {
511 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000512 // We have a dSYM file that didn't have a any debug info. If the
513 // string table has a size of 1, then it was made from an
514 // executable with no debug info, or from an executable that was
515 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 section =
517 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
518 .get();
519 if (section && section->GetFileSize() == 1) {
520 m_obj_file->GetModule()->ReportWarning(
521 "empty dSYM file detected, dSYM was created with an "
522 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528
529 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
530 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
531 LocalVariables | VariableTypes;
532
533 if (debug_line_file_size > 0)
534 abilities |= LineTables;
535 }
536 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537}
538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539const DWARFDataExtractor &
540SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
541 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000542 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
543 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
544 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546}
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
549 DWARFDataExtractor &data) {
550 ModuleSP module_sp(m_obj_file->GetModule());
551 const SectionList *section_list = module_sp->GetSectionList();
552 if (section_list) {
553 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
554 if (section_sp) {
555 // See if we memory mapped the DWARF segment?
556 if (m_dwarf_data.GetByteSize()) {
557 data.SetData(m_dwarf_data, section_sp->GetOffset(),
558 section_sp->GetFileSize());
559 } else {
560 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
561 data.Clear();
562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
568 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
569 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
573 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
574}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
577 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
578 m_data_debug_aranges);
579}
580
581const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
582 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
583}
584
585const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
586 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
587}
588
589const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
590 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
591}
592
George Rimarc6c7bfc2018-09-13 17:06:47 +0000593const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
594 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
598 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
599}
600
George Rimare4dee262018-10-23 09:46:15 +0000601const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
602 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
603 if (debugLocData.GetByteSize() > 0)
604 return debugLocData;
605 return get_debug_loclists_data();
606}
607
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
609 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
610}
611
George Rimare4dee262018-10-23 09:46:15 +0000612const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
613 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
614 m_data_debug_loclists);
615}
616
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
618 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
619 m_data_debug_ranges);
620}
621
George Rimar6e357122018-10-10 08:11:15 +0000622const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
623 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
624 m_data_debug_rnglists);
625}
626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
628 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
629}
630
631const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
632 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
633 m_data_debug_str_offsets);
634}
635
Greg Clayton2550ca12018-05-08 17:19:24 +0000636const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
637 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
638}
639
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
641 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
642}
643
644const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
645 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
646}
647
648const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
649 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
650 m_data_apple_namespaces);
651}
652
653const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
654 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
655}
656
Jan Kratochvile4777a92018-04-29 19:47:48 +0000657const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
658 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
659 m_data_gnu_debugaltlink);
660}
661
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000663 if (m_abbr)
664 return m_abbr.get();
665
666 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
667 if (debug_abbrev_data.GetByteSize() == 0)
668 return nullptr;
669
670 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
671 llvm::Error error = abbr->parse(debug_abbrev_data);
672 if (error) {
673 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
674 LLDB_LOG_ERROR(log, std::move(error),
675 "Unable to read .debug_abbrev section: {0}");
676 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000678
679 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681}
682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
684 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685}
686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000688 if (m_info == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000689 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
690 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
691 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 if (get_debug_info_data().GetByteSize() > 0) {
693 m_info.reset(new DWARFDebugInfo());
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000694 if (m_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 m_info->SetDwarfData(this);
696 }
Greg Clayton1f746072012-08-29 21:13:06 +0000697 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 }
699 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700}
701
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
703 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704}
705
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000706DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
708 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000709 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710
711 DWARFDebugInfo *info = DebugInfo();
712 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000713 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
714 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000716 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
718 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
719 dwarf_cu->SetUserData(comp_unit);
720 return dwarf_cu;
721 }
722 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723}
724
George Rimar7e7f9c12018-10-25 10:25:45 +0000725DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000726 if (m_ranges == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000727 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
728 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
729 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000730
731 if (get_debug_ranges_data().GetByteSize() > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000733 else if (get_debug_rnglists_data().GetByteSize() > 0)
734 m_ranges.reset(new DWARFDebugRngLists());
735
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000736 if (m_ranges)
George Rimar6e357122018-10-10 08:11:15 +0000737 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 }
739 return m_ranges.get();
740}
741
George Rimar7e7f9c12018-10-25 10:25:45 +0000742const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 return m_ranges.get();
744}
745
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000746lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 uint32_t cu_idx) {
748 CompUnitSP cu_sp;
749 if (dwarf_cu) {
750 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
751 if (comp_unit) {
752 // We already parsed this compile unit, had out a shared pointer to it
753 cu_sp = comp_unit->shared_from_this();
754 } else {
755 if (dwarf_cu->GetSymbolFileDWARF() != this) {
756 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
757 cu_idx);
758 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
759 // Let the debug map create the compile unit
760 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
761 dwarf_cu->SetUserData(cu_sp.get());
762 } else {
763 ModuleSP module_sp(m_obj_file->GetModule());
764 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000765 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 if (cu_die) {
Pavel Labath7d36d722019-01-16 12:30:41 +0000767 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 if (cu_file_spec) {
769 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000770 // resolve the file. This can be expensive e.g. when the source
Pavel Labath7d36d722019-01-16 12:30:41 +0000771 // files are NFS mounted.
772 cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773
774 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000775 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000777 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 }
779
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000780 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
782
783 bool is_optimized = dwarf_cu->GetIsOptimized();
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000784 cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000786 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 if (cu_sp) {
788 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000789 // try and get the first entry in the supports files from the
790 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 if (!cu_file_spec) {
792 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
793 if (cu_file_spec) {
794 (FileSpec &)(*cu_sp) = cu_file_spec;
795 // Also fix the invalid file spec which was copied from the
796 // compile unit.
797 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
798 }
799 }
800
801 dwarf_cu->SetUserData(cu_sp.get());
802
803 // Figure out the compile unit index if we weren't given one
804 if (cu_idx == UINT32_MAX)
805 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
806
807 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
808 cu_idx, cu_sp);
809 }
810 }
811 }
812 }
813 }
814 }
815 return cu_sp;
816}
817
818uint32_t SymbolFileDWARF::GetNumCompileUnits() {
819 DWARFDebugInfo *info = DebugInfo();
820 if (info)
821 return info->GetNumCompileUnits();
822 return 0;
823}
824
825CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000826 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 CompUnitSP cu_sp;
828 DWARFDebugInfo *info = DebugInfo();
829 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000830 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 if (dwarf_cu)
832 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
833 }
834 return cu_sp;
835}
836
Zachary Turner863f8c12019-01-11 18:03:20 +0000837Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
838 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000839 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 if (die.IsValid()) {
841 TypeSystem *type_system =
842 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
843
844 if (type_system) {
845 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
846 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000847 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 }
849 }
850 return nullptr;
851}
852
853bool SymbolFileDWARF::FixupAddress(Address &addr) {
854 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
855 if (debug_map_symfile) {
856 return debug_map_symfile->LinkOSOAddress(addr);
857 }
858 // This is a normal DWARF file, no address fixups need to happen
859 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000860}
Zachary Turner863f8c12019-01-11 18:03:20 +0000861lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000862 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000863 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 if (dwarf_cu)
865 return dwarf_cu->GetLanguageType();
866 else
867 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000868}
869
Zachary Turner863f8c12019-01-11 18:03:20 +0000870size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000871 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000872 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000873 if (!dwarf_cu)
874 return 0;
875
876 size_t functions_added = 0;
877 std::vector<DWARFDIE> function_dies;
878 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
879 for (const DWARFDIE &die : function_dies) {
880 if (comp_unit.FindFunctionByUID(die.GetID()))
881 continue;
882 if (ParseFunction(comp_unit, die))
883 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000885 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887}
888
Zachary Turner863f8c12019-01-11 18:03:20 +0000889bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
890 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000891 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000892 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000894 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 if (cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
898 DW_AT_stmt_list, DW_INVALID_OFFSET);
899 if (stmt_list != DW_INVALID_OFFSET) {
900 // All file indexes in DWARF are one based and a file of index zero is
901 // supposed to be the compile unit itself.
Zachary Turner863f8c12019-01-11 18:03:20 +0000902 support_files.Append(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903 return DWARFDebugLine::ParseSupportFiles(
Pavel Labath7d36d722019-01-16 12:30:41 +0000904 comp_unit.GetModule(), get_debug_line_data(), stmt_list,
905 support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 }
909 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910}
911
Zachary Turner863f8c12019-01-11 18:03:20 +0000912bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000913 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000914 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 if (dwarf_cu)
916 return dwarf_cu->GetIsOptimized();
917 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000918}
919
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920bool SymbolFileDWARF::ParseImportedModules(
921 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000922 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000923 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000925 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000926 if (!dwarf_cu)
927 return false;
928 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
929 sc.comp_unit->GetLanguage()))
930 return false;
931 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000933 if (!sc.comp_unit)
934 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000936 const DWARFDIE die = dwarf_cu->DIE();
937 if (!die)
938 return false;
939
940 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
941 child_die = child_die.GetSibling()) {
942 if (child_die.Tag() != DW_TAG_imported_declaration)
943 continue;
944
945 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
946 if (module_die.Tag() != DW_TAG_module)
947 continue;
948
949 if (const char *name =
950 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
951 SourceModule module;
952 module.path.push_back(ConstString(name));
953
954 DWARFDIE parent_die = module_die;
955 while ((parent_die = parent_die.GetParent())) {
956 if (parent_die.Tag() != DW_TAG_module)
957 break;
958 if (const char *name =
959 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
960 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000962 std::reverse(module.path.begin(), module.path.end());
963 if (const char *include_path = module_die.GetAttributeValueAsString(
964 DW_AT_LLVM_include_path, nullptr))
965 module.search_path = ConstString(include_path);
966 if (const char *sysroot = module_die.GetAttributeValueAsString(
967 DW_AT_LLVM_isysroot, nullptr))
968 module.sysroot = ConstString(sysroot);
969 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000970 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000972 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000973}
974
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975struct ParseDWARFLineTableCallbackInfo {
976 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000977 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979};
980
981//----------------------------------------------------------------------
982// ParseStatementTableCallback
983//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984static void ParseDWARFLineTableCallback(dw_offset_t offset,
985 const DWARFDebugLine::State &state,
986 void *userData) {
987 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
988 // Just started parsing the line table
989 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
990 // Done parsing line table, nothing to do for the cleanup
991 } else {
992 ParseDWARFLineTableCallbackInfo *info =
993 (ParseDWARFLineTableCallbackInfo *)userData;
994 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995
Adrian Prantl05097242018-04-30 16:49:04 +0000996 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000997 if (!info->sequence_up) {
998 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
999 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001002 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 state.column, state.file, state.is_stmt, state.basic_block,
1004 state.prologue_end, state.epilogue_begin, state.end_sequence);
1005 if (state.end_sequence) {
1006 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001007 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001009 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 }
1011 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012}
1013
Zachary Turner863f8c12019-01-11 18:03:20 +00001014bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001015 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +00001016 if (comp_unit.GetLineTable() != NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018
Zachary Turner863f8c12019-01-11 18:03:20 +00001019 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +00001021 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022 if (dwarf_cu_die) {
1023 const dw_offset_t cu_line_offset =
1024 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1025 DW_INVALID_OFFSET);
1026 if (cu_line_offset != DW_INVALID_OFFSET) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001027 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
1028 if (line_table_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 ParseDWARFLineTableCallbackInfo info;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001030 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 /*
1033 * MIPS:
1034 * The SymbolContext may not have a valid target, thus we may not be
1035 * able
1036 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1037 * #0
1038 * for MIPS. Use ArchSpec to clear the bit #0.
1039 */
Pavel Labathf760f5a2019-01-03 10:37:19 +00001040 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 case llvm::Triple::mips:
1042 case llvm::Triple::mipsel:
1043 case llvm::Triple::mips64:
1044 case llvm::Triple::mips64el:
1045 info.addr_mask = ~((lldb::addr_t)1);
1046 break;
1047 default:
1048 info.addr_mask = ~((lldb::addr_t)0);
1049 break;
1050 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 lldb::offset_t offset = cu_line_offset;
1053 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1054 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001055 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1057 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001058 // We have an object file that has a line table with addresses that
1059 // are not linked. We need to link the line table and convert the
1060 // addresses that are relative to the .o file into addresses for
1061 // the main executable.
Zachary Turner863f8c12019-01-11 18:03:20 +00001062 comp_unit.SetLineTable(
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001063 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 } else {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001065 comp_unit.SetLineTable(line_table_up.release());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 return true;
1067 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 }
1072 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073}
1074
Siva Chandrad8335e92015-12-16 00:22:08 +00001075lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1077 auto iter = m_debug_macros_map.find(*offset);
1078 if (iter != m_debug_macros_map.end())
1079 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001080
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1082 if (debug_macro_data.GetByteSize() == 0)
1083 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001084
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1086 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 const DWARFDebugMacroHeader &header =
1089 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1090 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1091 header.OffsetIs64Bit(), offset, this,
1092 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001095}
1096
Zachary Turner863f8c12019-01-11 18:03:20 +00001097bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001098 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001099
Zachary Turner863f8c12019-01-11 18:03:20 +00001100 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001102 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103
Jan Kratochvil93afb052018-05-24 20:51:13 +00001104 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 if (!dwarf_cu_die)
1106 return false;
1107
1108 lldb::offset_t sect_offset =
1109 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1110 if (sect_offset == DW_INVALID_OFFSET)
1111 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1112 DW_INVALID_OFFSET);
1113 if (sect_offset == DW_INVALID_OFFSET)
1114 return false;
1115
Zachary Turner863f8c12019-01-11 18:03:20 +00001116 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117
1118 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001119}
1120
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001121size_t SymbolFileDWARF::ParseBlocksRecursive(
1122 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1123 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 size_t blocks_added = 0;
1125 DWARFDIE die = orig_die;
1126 while (die) {
1127 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 switch (tag) {
1130 case DW_TAG_inlined_subroutine:
1131 case DW_TAG_subprogram:
1132 case DW_TAG_lexical_block: {
1133 Block *block = NULL;
1134 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001135 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1136 // inlined functions. These will be parsed on their own as separate
1137 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138
1139 if (depth > 0)
1140 break;
1141
1142 block = parent_block;
1143 } else {
1144 BlockSP block_sp(new Block(die.GetID()));
1145 parent_block->AddChild(block_sp);
1146 block = block_sp.get();
1147 }
1148 DWARFRangeList ranges;
1149 const char *name = NULL;
1150 const char *mangled_name = NULL;
1151
1152 int decl_file = 0;
1153 int decl_line = 0;
1154 int decl_column = 0;
1155 int call_file = 0;
1156 int call_line = 0;
1157 int call_column = 0;
1158 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1159 decl_line, decl_column, call_file, call_line,
1160 call_column, nullptr)) {
1161 if (tag == DW_TAG_subprogram) {
1162 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1163 subprogram_low_pc = ranges.GetMinRangeBase(0);
1164 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001165 // We get called here for inlined subroutines in two ways. The first
1166 // time is when we are making the Function object for this inlined
1167 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001169 // need to adjust the containing address. The second time is when we
1170 // are parsing the blocks inside the function that contains the
1171 // inlined concrete instance. Since these will be blocks inside the
1172 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1174 subprogram_low_pc = ranges.GetMinRangeBase(0);
1175 }
1176 }
1177
1178 const size_t num_ranges = ranges.GetSize();
1179 for (size_t i = 0; i < num_ranges; ++i) {
1180 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1181 const addr_t range_base = range.GetRangeBase();
1182 if (range_base >= subprogram_low_pc)
1183 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1184 range.GetByteSize()));
1185 else {
1186 GetObjectFile()->GetModule()->ReportError(
1187 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1188 ") which has a base that is less than the function's low PC "
1189 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1190 "start of this error message",
1191 block->GetID(), range_base, range.GetRangeEnd(),
1192 subprogram_low_pc);
1193 }
1194 }
1195 block->FinalizeRanges();
1196
1197 if (tag != DW_TAG_subprogram &&
1198 (name != NULL || mangled_name != NULL)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001199 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001201 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001202 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203 decl_line, decl_column));
1204
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001205 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001206 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001207 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001208 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 call_line, call_column));
1210
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001211 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1212 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 }
1214
1215 ++blocks_added;
1216
1217 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001218 blocks_added +=
1219 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1220 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 }
1222 }
1223 } break;
1224 default:
1225 break;
1226 }
1227
Adrian Prantl05097242018-04-30 16:49:04 +00001228 // Only parse siblings of the block if we are not at depth zero. A depth of
1229 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1230 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231
1232 if (depth == 0)
1233 die.Clear();
1234 else
1235 die = die.GetSibling();
1236 }
1237 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001238}
1239
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1241 if (parent_die) {
1242 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1243 die = die.GetSibling()) {
1244 dw_tag_t tag = die.Tag();
1245 bool check_virtuality = false;
1246 switch (tag) {
1247 case DW_TAG_inheritance:
1248 case DW_TAG_subprogram:
1249 check_virtuality = true;
1250 break;
1251 default:
1252 break;
1253 }
1254 if (check_virtuality) {
1255 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1256 return true;
1257 }
1258 }
1259 }
1260 return false;
1261}
1262
1263void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1264 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1265 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1266 std::vector<DWARFDIE> decl_ctx_die_list =
1267 ast_parser->GetDIEForDeclContext(decl_ctx);
1268
1269 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1270 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1271 decl = decl.GetSibling())
1272 ast_parser->GetDeclForUIDFromDWARF(decl);
1273}
1274
1275SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001276 // This method can be called without going through the symbol vendor so we
1277 // need to lock the module.
1278 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001279 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1280 // must make sure we use the correct DWARF file when resolving things. On
1281 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1282 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1283 // references to other DWARF objects and we must be ready to receive a
1284 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 // instance.
1286 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1287 if (debug_map)
1288 return debug_map->GetSymbolFileByOSOIndex(
1289 debug_map->GetOSOIndexFromUserID(uid));
1290 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001291}
1292
1293DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001295 // This method can be called without going through the symbol vendor so we
1296 // need to lock the module.
1297 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001298 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1299 // must make sure we use the correct DWARF file when resolving things. On
1300 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1301 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1302 // references to other DWARF objects and we must be ready to receive a
1303 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 // instance.
1305 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1306 if (dwarf)
1307 return dwarf->GetDIE(DIERef(uid, dwarf));
1308 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001309}
1310
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001312 // This method can be called without going through the symbol vendor so we
1313 // need to lock the module.
1314 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001315 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1316 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1317 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 DWARFDIE die = GetDIEFromUID(type_uid);
1319 if (die)
1320 return die.GetDecl();
1321 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001322}
1323
Greg Clayton99558cc42015-08-24 23:46:31 +00001324CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001326 // This method can be called without going through the symbol vendor so we
1327 // need to lock the module.
1328 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001329 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1330 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1331 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 DWARFDIE die = GetDIEFromUID(type_uid);
1333 if (die)
1334 return die.GetDeclContext();
1335 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001336}
1337
Greg Clayton99558cc42015-08-24 23:46:31 +00001338CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001340 // This method can be called without going through the symbol vendor so we
1341 // need to lock the module.
1342 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001343 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1344 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1345 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 DWARFDIE die = GetDIEFromUID(type_uid);
1347 if (die)
1348 return die.GetContainingDeclContext();
1349 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350}
1351
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001353 // This method can be called without going through the symbol vendor so we
1354 // need to lock the module.
1355 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001356 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1357 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1358 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 DWARFDIE type_die = GetDIEFromUID(type_uid);
1360 if (type_die)
1361 return type_die.ResolveType();
1362 else
1363 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364}
1365
Adrian Prantleca07c52018-11-05 20:49:07 +00001366llvm::Optional<SymbolFile::ArrayInfo>
1367SymbolFileDWARF::GetDynamicArrayInfoForUID(
1368 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1369 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1370 DWARFDIE type_die = GetDIEFromUID(type_uid);
1371 if (type_die)
1372 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1373 else
1374 return llvm::None;
1375}
1376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1378 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001379}
1380
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1382 bool assert_not_being_parsed) {
1383 if (die) {
1384 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1385 if (log)
1386 GetObjectFile()->GetModule()->LogMessage(
1387 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1388 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001389
Adrian Prantl05097242018-04-30 16:49:04 +00001390 // We might be coming in in the middle of a type tree (a class within a
1391 // class, an enum within a class), so parse any needed parent DIEs before
1392 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1394 if (decl_ctx_die) {
1395 if (log) {
1396 switch (decl_ctx_die.Tag()) {
1397 case DW_TAG_structure_type:
1398 case DW_TAG_union_type:
1399 case DW_TAG_class_type: {
1400 // Get the type, which could be a forward declaration
1401 if (log)
1402 GetObjectFile()->GetModule()->LogMessage(
1403 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1404 "resolve parent forward type for 0x%8.8x",
1405 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1406 decl_ctx_die.GetOffset());
1407 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 default:
1410 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001413 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 return ResolveType(die);
1415 }
1416 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001417}
1418
Greg Clayton6beaaa62011-01-17 03:46:26 +00001419// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001420// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1421// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422bool SymbolFileDWARF::HasForwardDeclForClangType(
1423 const CompilerType &compiler_type) {
1424 CompilerType compiler_type_no_qualifiers =
1425 ClangUtil::RemoveFastQualifiers(compiler_type);
1426 if (GetForwardDeclClangTypeToDie().count(
1427 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1428 return true;
1429 }
1430 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 ClangASTContext *clang_type_system =
1433 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1434 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001435 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 DWARFASTParserClang *ast_parser =
1437 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1438 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001439}
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001442 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 ClangASTContext *clang_type_system =
1445 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1446 if (clang_type_system) {
1447 DWARFASTParserClang *ast_parser =
1448 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1449 if (ast_parser &&
1450 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1451 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1452 }
Jim Inghamc3549282012-01-11 02:21:12 +00001453
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 // We have a struct/union/class/enum that needs to be fully resolved.
1455 CompilerType compiler_type_no_qualifiers =
1456 ClangUtil::RemoveFastQualifiers(compiler_type);
1457 auto die_it = GetForwardDeclClangTypeToDie().find(
1458 compiler_type_no_qualifiers.GetOpaqueQualType());
1459 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1460 // We have already resolved this type...
1461 return true;
1462 }
1463
1464 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1465 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001466 // Once we start resolving this type, remove it from the forward
1467 // declaration map in case anyone child members or other types require this
1468 // type to get resolved. The type will get resolved when all of the calls
1469 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 GetForwardDeclClangTypeToDie().erase(die_it);
1471
1472 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1473
1474 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1475 DWARF_LOG_TYPE_COMPLETION));
1476 if (log)
1477 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1478 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1479 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1480 type->GetName().AsCString());
1481 assert(compiler_type);
1482 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1483 if (dwarf_ast)
1484 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1485 }
1486 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001487}
1488
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1490 bool assert_not_being_parsed,
1491 bool resolve_function_context) {
1492 if (die) {
1493 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1494
1495 if (assert_not_being_parsed) {
1496 if (type != DIE_IS_BEING_PARSED)
1497 return type;
1498
1499 GetObjectFile()->GetModule()->ReportError(
1500 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1501 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1502
1503 } else
1504 return type;
1505 }
1506 return nullptr;
1507}
1508
1509CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001510SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 uint32_t cu_idx) {
1512 // Check if the symbol vendor already knows about this compile unit?
1513 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001514 // The symbol vendor doesn't know about this compile unit, we need to parse
1515 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1517 }
1518 return (CompileUnit *)dwarf_cu->GetUserData();
1519}
1520
1521size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1522 DIEArray &method_die_offsets) {
1523 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001524 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 return method_die_offsets.size();
1526}
1527
1528bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1529 sc.Clear(false);
1530
1531 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1534
1535 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1536 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001537 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538
1539 if (sc.function) {
1540 sc.module_sp = sc.function->CalculateSymbolContextModule();
1541 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 }
1544
1545 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546}
1547
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1549 UpdateExternalModuleListIfNeeded();
1550 const auto &pos = m_external_type_modules.find(name);
1551 if (pos != m_external_type_modules.end())
1552 return pos->second;
1553 else
1554 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001555}
1556
Greg Clayton2f869fe2016-03-30 20:14:35 +00001557DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1559 DWARFDebugInfo *debug_info = DebugInfo();
1560 if (debug_info)
1561 return debug_info->GetDIE(die_ref);
1562 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001563 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001564}
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566std::unique_ptr<SymbolFileDWARFDwo>
1567SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001568 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001570 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 // info files for modules.
1572 if (GetDebugMapSymfile())
1573 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 const char *dwo_name = cu_die.GetAttributeValueAsString(
1576 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1577 if (!dwo_name)
1578 return nullptr;
1579
Tamas Berghammer963ce482017-08-25 13:56:14 +00001580 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1581 if (dwp_symfile) {
1582 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1583 DW_AT_GNU_dwo_id, 0);
1584 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1585 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1586 if (dwo_symfile)
1587 return dwo_symfile;
1588 }
1589
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001590 FileSpec dwo_file(dwo_name);
1591 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 if (dwo_file.IsRelative()) {
1593 const char *comp_dir = cu_die.GetAttributeValueAsString(
1594 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1595 if (!comp_dir)
1596 return nullptr;
1597
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001598 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1599 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 dwo_file.AppendPathComponent(dwo_name);
1601 }
1602
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001603 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604 return nullptr;
1605
1606 const lldb::offset_t file_offset = 0;
1607 DataBufferSP dwo_file_data_sp;
1608 lldb::offset_t dwo_file_data_offset = 0;
1609 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1610 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001611 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1612 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 if (dwo_obj_file == nullptr)
1614 return nullptr;
1615
1616 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001617}
1618
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1620 if (m_fetched_external_modules)
1621 return;
1622 m_fetched_external_modules = true;
1623
1624 DWARFDebugInfo *debug_info = DebugInfo();
1625
1626 const uint32_t num_compile_units = GetNumCompileUnits();
1627 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001628 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629
Jan Kratochvil93afb052018-05-24 20:51:13 +00001630 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001631 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1633
1634 if (name) {
1635 ConstString const_name(name);
1636 if (m_external_type_modules.find(const_name) ==
1637 m_external_type_modules.end()) {
1638 ModuleSP module_sp;
1639 const char *dwo_path =
1640 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1641 if (dwo_path) {
1642 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001643 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001644 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1646 const char *comp_dir =
1647 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1648 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001649 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001650 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001651 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1653 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001654 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655 dwo_module_spec.GetArchitecture() =
1656 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001657
Adrian Prantl05097242018-04-30 16:49:04 +00001658 // When LLDB loads "external" modules it looks at the presence of
1659 // DW_AT_GNU_dwo_name. However, when the already created module
1660 // (corresponding to .dwo itself) is being processed, it will see
1661 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1662 // dwo file) and will try to call ModuleList::GetSharedModule
1663 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1664 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1665 // DW_AT_comp_dir. In this case the method
1666 // ModuleList::GetSharedModule will fail and the warning will be
1667 // printed. However, as one can notice in this case we don't
1668 // actually need to try to load the already loaded module
1669 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001670 if (m_obj_file->GetFileSpec()
1671 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001672 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001673 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1674 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1675 continue;
1676 }
1677
Zachary Turner97206d52017-05-12 04:51:55 +00001678 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679 dwo_module_spec, module_sp, NULL, NULL, NULL);
1680 if (!module_sp) {
1681 GetObjectFile()->GetModule()->ReportWarning(
1682 "0x%8.8x: unable to locate module needed for external types: "
1683 "%s\nerror: %s\nDebugging will be degraded due to missing "
1684 "types. Rebuilding your project will regenerate the needed "
1685 "module files.",
1686 die.GetOffset(),
1687 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1688 error.AsCString("unknown error"));
1689 }
1690 }
1691 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001694 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695 }
1696}
1697
1698SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001699 if (!m_global_aranges_up) {
1700 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701
1702 ModuleSP module_sp = GetObjectFile()->GetModule();
1703 if (module_sp) {
1704 const size_t num_cus = module_sp->GetNumCompileUnits();
1705 for (size_t i = 0; i < num_cus; ++i) {
1706 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1707 if (cu_sp) {
1708 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1709 if (globals_sp) {
1710 const size_t num_globals = globals_sp->GetSize();
1711 for (size_t g = 0; g < num_globals; ++g) {
1712 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1713 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1714 const DWARFExpression &location = var_sp->LocationExpression();
1715 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001716 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001717 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1718 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001719 if (location_result.GetValueType() ==
1720 Value::eValueTypeFileAddress) {
1721 lldb::addr_t file_addr =
1722 location_result.GetScalar().ULongLong();
1723 lldb::addr_t byte_size = 1;
1724 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001725 byte_size =
1726 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001727 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728 file_addr, byte_size, var_sp.get()));
1729 }
1730 }
1731 }
1732 }
1733 }
1734 }
1735 }
1736 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001737 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001739 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740}
1741
1742uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001743 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001745 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1746 Timer scoped_timer(func_cat,
1747 "SymbolFileDWARF::"
1748 "ResolveSymbolContext (so_addr = { "
1749 "section = %p, offset = 0x%" PRIx64
1750 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 static_cast<void *>(so_addr.GetSection().get()),
1752 so_addr.GetOffset(), resolve_scope);
1753 uint32_t resolved = 0;
1754 if (resolve_scope &
1755 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1756 eSymbolContextLineEntry | eSymbolContextVariable)) {
1757 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1758
1759 DWARFDebugInfo *debug_info = DebugInfo();
1760 if (debug_info) {
1761 const dw_offset_t cu_offset =
1762 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1763 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001764 // Global variables are not in the compile unit address ranges. The
1765 // only way to currently find global variables is to iterate over the
1766 // .debug_pubnames or the __apple_names table and find all items in
1767 // there that point to DW_TAG_variable DIEs and then find the address
1768 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 if (resolve_scope & eSymbolContextVariable) {
1770 GlobalVariableMap &map = GetGlobalAranges();
1771 const GlobalVariableMap::Entry *entry =
1772 map.FindEntryThatContains(file_vm_addr);
1773 if (entry && entry->data) {
1774 Variable *variable = entry->data;
1775 SymbolContextScope *scc = variable->GetSymbolContextScope();
1776 if (scc) {
1777 scc->CalculateSymbolContext(&sc);
1778 sc.variable = variable;
1779 }
1780 return sc.GetResolvedMask();
1781 }
1782 }
1783 } else {
1784 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001785 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1787 if (dwarf_cu) {
1788 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1789 if (sc.comp_unit) {
1790 resolved |= eSymbolContextCompUnit;
1791
1792 bool force_check_line_table = false;
1793 if (resolve_scope &
1794 (eSymbolContextFunction | eSymbolContextBlock)) {
1795 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1796 DWARFDIE block_die;
1797 if (function_die) {
1798 sc.function =
1799 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1800 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001801 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802
1803 if (sc.function && (resolve_scope & eSymbolContextBlock))
1804 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1805 } else {
1806 // We might have had a compile unit that had discontiguous
1807 // address ranges where the gaps are symbols that don't have
1808 // any debug info. Discontiguous compile unit address ranges
1809 // should only happen when there aren't other functions from
1810 // other compile units in these gaps. This helps keep the size
1811 // of the aranges down.
1812 force_check_line_table = true;
1813 }
1814
1815 if (sc.function != NULL) {
1816 resolved |= eSymbolContextFunction;
1817
1818 if (resolve_scope & eSymbolContextBlock) {
1819 Block &block = sc.function->GetBlock(true);
1820
1821 if (block_die)
1822 sc.block = block.FindBlockByID(block_die.GetID());
1823 else
1824 sc.block = block.FindBlockByID(function_die.GetID());
1825 if (sc.block)
1826 resolved |= eSymbolContextBlock;
1827 }
1828 }
1829 }
1830
1831 if ((resolve_scope & eSymbolContextLineEntry) ||
1832 force_check_line_table) {
1833 LineTable *line_table = sc.comp_unit->GetLineTable();
1834 if (line_table != NULL) {
1835 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001836 // terms of this debug file if there is no debug map, or it
1837 // will be an address in the .o file which needs to be fixed up
1838 // to be in terms of the debug map executable. Either way,
1839 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 Address exe_so_addr(so_addr);
1841 if (FixupAddress(exe_so_addr)) {
1842 if (line_table->FindLineEntryByAddress(exe_so_addr,
1843 sc.line_entry)) {
1844 resolved |= eSymbolContextLineEntry;
1845 }
1846 }
1847 }
1848 }
1849
1850 if (force_check_line_table &&
1851 !(resolved & eSymbolContextLineEntry)) {
1852 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001853 // address ranges where the gaps are symbols that don't have any
1854 // debug info. Discontiguous compile unit address ranges should
1855 // only happen when there aren't other functions from other
1856 // compile units in these gaps. This helps keep the size of the
1857 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001858 sc.comp_unit = NULL;
1859 resolved &= ~eSymbolContextCompUnit;
1860 }
1861 } else {
1862 GetObjectFile()->GetModule()->ReportWarning(
1863 "0x%8.8x: compile unit %u failed to create a valid "
1864 "lldb_private::CompileUnit class.",
1865 cu_offset, cu_idx);
1866 }
1867 }
1868 }
1869 }
1870 }
1871 return resolved;
1872}
1873
1874uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1875 uint32_t line,
1876 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001877 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 SymbolContextList &sc_list) {
1879 const uint32_t prev_size = sc_list.GetSize();
1880 if (resolve_scope & eSymbolContextCompUnit) {
1881 DWARFDebugInfo *debug_info = DebugInfo();
1882 if (debug_info) {
1883 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001884 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885
1886 for (cu_idx = 0;
1887 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1888 ++cu_idx) {
1889 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1890 const bool full_match = (bool)file_spec.GetDirectory();
1891 bool file_spec_matches_cu_file_spec =
1892 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1893 if (check_inlines || file_spec_matches_cu_file_spec) {
1894 SymbolContext sc(m_obj_file->GetModule());
1895 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1896 if (sc.comp_unit) {
1897 uint32_t file_idx = UINT32_MAX;
1898
Adrian Prantl05097242018-04-30 16:49:04 +00001899 // If we are looking for inline functions only and we don't find it
1900 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901 if (check_inlines) {
1902 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1903 1, file_spec, true);
1904 if (file_idx == UINT32_MAX)
1905 continue;
1906 }
1907
1908 if (line != 0) {
1909 LineTable *line_table = sc.comp_unit->GetLineTable();
1910
1911 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001912 // We will have already looked up the file index if we are
1913 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914 if (!check_inlines)
1915 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1916 1, file_spec, true);
1917
1918 if (file_idx != UINT32_MAX) {
1919 uint32_t found_line;
1920 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1921 0, file_idx, line, false, &sc.line_entry);
1922 found_line = sc.line_entry.line;
1923
1924 while (line_idx != UINT32_MAX) {
1925 sc.function = NULL;
1926 sc.block = NULL;
1927 if (resolve_scope &
1928 (eSymbolContextFunction | eSymbolContextBlock)) {
1929 const lldb::addr_t file_vm_addr =
1930 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1931 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1932 DWARFDIE function_die =
1933 dwarf_cu->LookupAddress(file_vm_addr);
1934 DWARFDIE block_die;
1935 if (function_die) {
1936 sc.function =
1937 sc.comp_unit
1938 ->FindFunctionByUID(function_die.GetID())
1939 .get();
1940 if (sc.function == NULL)
1941 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001942 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001943
1944 if (sc.function &&
1945 (resolve_scope & eSymbolContextBlock))
1946 block_die =
1947 function_die.LookupDeepestBlock(file_vm_addr);
1948 }
1949
1950 if (sc.function != NULL) {
1951 Block &block = sc.function->GetBlock(true);
1952
1953 if (block_die)
1954 sc.block = block.FindBlockByID(block_die.GetID());
1955 else if (function_die)
1956 sc.block =
1957 block.FindBlockByID(function_die.GetID());
1958 }
1959 }
1960 }
1961
1962 sc_list.Append(sc);
1963 line_idx = line_table->FindLineEntryIndexByFileIndex(
1964 line_idx + 1, file_idx, found_line, true,
1965 &sc.line_entry);
1966 }
1967 }
1968 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1969 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001970 // sites by file and line and if the file spec matches that of
1971 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001972 sc_list.Append(sc);
1973 }
1974 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1975 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001976 // sites by file and line and if the file spec matches that of
1977 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978 sc_list.Append(sc);
1979 }
1980
1981 if (!check_inlines)
1982 break;
1983 }
1984 }
1985 }
1986 }
1987 }
1988 return sc_list.GetSize() - prev_size;
1989}
1990
Jim Ingham7fca8c02017-04-28 00:51:06 +00001991void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001992 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001993 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994}
1995
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001996std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1997 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1998 if (module_sp)
1999 return module_sp->GetMutex();
2000 return GetObjectFile()->GetModule()->GetMutex();
2001}
2002
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2004 const lldb_private::CompilerDeclContext *decl_ctx) {
2005 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00002006 // Invalid namespace decl which means we aren't matching only things in
2007 // this symbol file, so return true to indicate it matches this symbol
2008 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002009 return true;
2010 }
2011
2012 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2013 TypeSystem *type_system = GetTypeSystemForLanguage(
2014 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2015 if (decl_ctx_type_system == type_system)
2016 return true; // The type systems match, return true
2017
2018 // The namespace AST was valid, and it does not match...
2019 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2020
2021 if (log)
2022 GetObjectFile()->GetModule()->LogMessage(
2023 log, "Valid namespace does not match symbol file");
2024
2025 return false;
2026}
2027
2028uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002029 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002030 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2032
2033 if (log)
2034 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002035 log,
2036 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2037 "parent_decl_ctx=%p, max_matches=%u, variables)",
2038 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 max_matches);
2040
2041 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2042 return 0;
2043
2044 DWARFDebugInfo *info = DebugInfo();
2045 if (info == NULL)
2046 return 0;
2047
Pavel Labath34cda142018-05-31 09:46:26 +00002048 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 const uint32_t original_size = variables.GetSize();
2050
Pavel Labathe1d18752018-06-07 10:04:44 +00002051 llvm::StringRef basename;
2052 llvm::StringRef context;
2053
2054 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2055 context, basename))
2056 basename = name.GetStringRef();
2057
Kate Stoneb9c1b512016-09-06 20:57:50 +00002058 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002059 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002060 const size_t num_die_matches = die_offsets.size();
2061 if (num_die_matches) {
2062 SymbolContext sc;
2063 sc.module_sp = m_obj_file->GetModule();
2064 assert(sc.module_sp);
2065
Pavel Labathe1d18752018-06-07 10:04:44 +00002066 // Loop invariant: Variables up to this index have been checked for context
2067 // matches.
2068 uint32_t pruned_idx = original_size;
2069
Kate Stoneb9c1b512016-09-06 20:57:50 +00002070 bool done = false;
2071 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2072 const DIERef &die_ref = die_offsets[i];
2073 DWARFDIE die = GetDIE(die_ref);
2074
2075 if (die) {
2076 switch (die.Tag()) {
2077 default:
2078 case DW_TAG_subprogram:
2079 case DW_TAG_inlined_subroutine:
2080 case DW_TAG_try_block:
2081 case DW_TAG_catch_block:
2082 break;
2083
2084 case DW_TAG_variable: {
2085 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2086
2087 if (parent_decl_ctx) {
2088 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2089 if (dwarf_ast) {
2090 CompilerDeclContext actual_parent_decl_ctx =
2091 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2092 if (!actual_parent_decl_ctx ||
2093 actual_parent_decl_ctx != *parent_decl_ctx)
2094 continue;
2095 }
2096 }
2097
2098 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2099 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002100 while (pruned_idx < variables.GetSize()) {
2101 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2102 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2103 ++pruned_idx;
2104 else
2105 variables.RemoveVariableAtIndex(pruned_idx);
2106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002107
2108 if (variables.GetSize() - original_size >= max_matches)
2109 done = true;
2110 } break;
2111 }
2112 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002113 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2114 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002115 }
2116 }
2117 }
2118
2119 // Return the number of variable that were appended to the list
2120 const uint32_t num_matches = variables.GetSize() - original_size;
2121 if (log && num_matches > 0) {
2122 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002123 log,
2124 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2125 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2126 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127 max_matches, num_matches);
2128 }
2129 return num_matches;
2130}
2131
2132uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002133 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134 VariableList &variables) {
2135 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2136
2137 if (log) {
2138 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002139 log,
2140 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2141 "max_matches=%u, variables)",
2142 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143 }
2144
2145 DWARFDebugInfo *info = DebugInfo();
2146 if (info == NULL)
2147 return 0;
2148
Pavel Labath34cda142018-05-31 09:46:26 +00002149 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150 const uint32_t original_size = variables.GetSize();
2151
2152 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002153 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154
2155 SymbolContext sc;
2156 sc.module_sp = m_obj_file->GetModule();
2157 assert(sc.module_sp);
2158
2159 const size_t num_matches = die_offsets.size();
2160 if (num_matches) {
2161 for (size_t i = 0; i < num_matches; ++i) {
2162 const DIERef &die_ref = die_offsets[i];
2163 DWARFDIE die = GetDIE(die_ref);
2164
2165 if (die) {
2166 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2167
2168 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2169
2170 if (variables.GetSize() - original_size >= max_matches)
2171 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002172 } else
2173 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174 }
2175 }
2176
2177 // Return the number of variable that were appended to the list
2178 return variables.GetSize() - original_size;
2179}
2180
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2182 bool include_inlines,
2183 SymbolContextList &sc_list) {
2184 SymbolContext sc;
2185
2186 if (!orig_die)
2187 return false;
2188
2189 // If we were passed a die that is not a function, just return false...
2190 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2191 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2192 return false;
2193
2194 DWARFDIE die = orig_die;
2195 DWARFDIE inlined_die;
2196 if (die.Tag() == DW_TAG_inlined_subroutine) {
2197 inlined_die = die;
2198
2199 while (1) {
2200 die = die.GetParent();
2201
2202 if (die) {
2203 if (die.Tag() == DW_TAG_subprogram)
2204 break;
2205 } else
2206 break;
2207 }
2208 }
2209 assert(die && die.Tag() == DW_TAG_subprogram);
2210 if (GetFunction(die, sc)) {
2211 Address addr;
2212 // Parse all blocks if needed
2213 if (inlined_die) {
2214 Block &function_block = sc.function->GetBlock(true);
2215 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2216 if (sc.block == NULL)
2217 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00002218 if (sc.block == NULL || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 addr.Clear();
2220 } else {
2221 sc.block = NULL;
2222 addr = sc.function->GetAddressRange().GetBaseAddress();
2223 }
2224
2225 if (addr.IsValid()) {
2226 sc_list.Append(sc);
2227 return true;
2228 }
2229 }
2230
2231 return false;
2232}
2233
Kate Stoneb9c1b512016-09-06 20:57:50 +00002234bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2235 const DWARFDIE &die) {
2236 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002237 // parent decl context isn't valid, we aren't trying to look for any
2238 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002239 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2240 return true;
2241
2242 if (die) {
2243 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2244 if (dwarf_ast) {
2245 CompilerDeclContext actual_decl_ctx =
2246 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2247 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002248 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002249 }
2250 }
2251 return false;
2252}
2253
Zachary Turner117b1fa2018-10-25 20:45:40 +00002254uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002255 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002256 FunctionNameType name_type_mask, bool include_inlines, bool append,
2257 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002258 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2259 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002260 name.AsCString());
2261
2262 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002263 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2265
2266 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2267
2268 if (log) {
2269 GetObjectFile()->GetModule()->LogMessage(
2270 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2271 "name_type_mask=0x%x, append=%u, sc_list)",
2272 name.GetCString(), name_type_mask, append);
2273 }
2274
2275 // If we aren't appending the results to this list, then clear the list
2276 if (!append)
2277 sc_list.Clear();
2278
2279 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2280 return 0;
2281
2282 // If name is empty then we won't find anything.
2283 if (name.IsEmpty())
2284 return 0;
2285
Adrian Prantl05097242018-04-30 16:49:04 +00002286 // Remember how many sc_list are in the list before we search in case we are
2287 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289 const uint32_t original_size = sc_list.GetSize();
2290
2291 DWARFDebugInfo *info = DebugInfo();
2292 if (info == NULL)
2293 return 0;
2294
Pavel Labath5af11ab2018-06-05 10:33:56 +00002295 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2296 DIEArray offsets;
2297 CompilerDeclContext empty_decl_ctx;
2298 if (!parent_decl_ctx)
2299 parent_decl_ctx = &empty_decl_ctx;
2300
2301 std::vector<DWARFDIE> dies;
2302 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2303 for (const DWARFDIE &die: dies) {
2304 if (resolved_dies.insert(die.GetDIE()).second)
2305 ResolveFunction(die, include_inlines, sc_list);
2306 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002307
2308 // Return the number of variable that were appended to the list
2309 const uint32_t num_matches = sc_list.GetSize() - original_size;
2310
2311 if (log && num_matches > 0) {
2312 GetObjectFile()->GetModule()->LogMessage(
2313 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2314 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2315 "%u",
2316 name.GetCString(), name_type_mask, include_inlines, append,
2317 num_matches);
2318 }
2319 return num_matches;
2320}
2321
2322uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2323 bool include_inlines, bool append,
2324 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002325 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2326 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002327 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328
2329 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2330
2331 if (log) {
2332 GetObjectFile()->GetModule()->LogMessage(
2333 log,
2334 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002335 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 }
2337
2338 // If we aren't appending the results to this list, then clear the list
2339 if (!append)
2340 sc_list.Clear();
2341
Pavel Labathb13f0332018-05-21 14:12:52 +00002342 DWARFDebugInfo *info = DebugInfo();
2343 if (!info)
2344 return 0;
2345
Adrian Prantl05097242018-04-30 16:49:04 +00002346 // Remember how many sc_list are in the list before we search in case we are
2347 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002348 uint32_t original_size = sc_list.GetSize();
2349
Pavel Labath6de9c792018-06-05 12:13:22 +00002350 DIEArray offsets;
2351 m_index->GetFunctions(regex, offsets);
2352
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002353 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2354 for (DIERef ref : offsets) {
2355 DWARFDIE die = info->GetDIE(ref);
2356 if (!die) {
2357 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2358 continue;
2359 }
2360 if (resolved_dies.insert(die.GetDIE()).second)
2361 ResolveFunction(die, include_inlines, sc_list);
2362 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002363
2364 // Return the number of variable that were appended to the list
2365 return sc_list.GetSize() - original_size;
2366}
2367
2368void SymbolFileDWARF::GetMangledNamesForFunction(
2369 const std::string &scope_qualified_name,
2370 std::vector<ConstString> &mangled_names) {
2371 DWARFDebugInfo *info = DebugInfo();
2372 uint32_t num_comp_units = 0;
2373 if (info)
2374 num_comp_units = info->GetNumCompileUnits();
2375
2376 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002377 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002378 if (cu == nullptr)
2379 continue;
2380
2381 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2382 if (dwo)
2383 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2384 }
2385
2386 NameToOffsetMap::iterator iter =
2387 m_function_scope_qualified_name_map.find(scope_qualified_name);
2388 if (iter == m_function_scope_qualified_name_map.end())
2389 return;
2390
2391 DIERefSetSP set_sp = (*iter).second;
2392 std::set<DIERef>::iterator set_iter;
2393 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2394 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2395 mangled_names.push_back(ConstString(die.GetMangledName()));
2396 }
2397}
2398
2399uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002400 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002401 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2403 TypeMap &types) {
2404 // If we aren't appending the results to this list, then clear the list
2405 if (!append)
2406 types.Clear();
2407
2408 // Make sure we haven't already searched this SymbolFile before...
2409 if (searched_symbol_files.count(this))
2410 return 0;
2411 else
2412 searched_symbol_files.insert(this);
2413
2414 DWARFDebugInfo *info = DebugInfo();
2415 if (info == NULL)
2416 return 0;
2417
2418 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2419
2420 if (log) {
2421 if (parent_decl_ctx)
2422 GetObjectFile()->GetModule()->LogMessage(
2423 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2424 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2425 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2426 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2427 else
2428 GetObjectFile()->GetModule()->LogMessage(
2429 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2430 "NULL, append=%u, max_matches=%u, type_list)",
2431 name.GetCString(), append, max_matches);
2432 }
2433
2434 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2435 return 0;
2436
2437 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002438 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002439 const size_t num_die_matches = die_offsets.size();
2440
2441 if (num_die_matches) {
2442 const uint32_t initial_types_size = types.GetSize();
2443 for (size_t i = 0; i < num_die_matches; ++i) {
2444 const DIERef &die_ref = die_offsets[i];
2445 DWARFDIE die = GetDIE(die_ref);
2446
2447 if (die) {
2448 if (!DIEInDeclContext(parent_decl_ctx, die))
2449 continue; // The containing decl contexts don't match
2450
2451 Type *matching_type = ResolveType(die, true, true);
2452 if (matching_type) {
2453 // We found a type pointer, now find the shared pointer form our type
2454 // list
2455 types.InsertUnique(matching_type->shared_from_this());
2456 if (types.GetSize() >= max_matches)
2457 break;
2458 }
2459 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002460 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2461 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462 }
2463 }
2464 const uint32_t num_matches = types.GetSize() - initial_types_size;
2465 if (log && num_matches) {
2466 if (parent_decl_ctx) {
2467 GetObjectFile()->GetModule()->LogMessage(
2468 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2469 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2470 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2471 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2472 num_matches);
2473 } else {
2474 GetObjectFile()->GetModule()->LogMessage(
2475 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2476 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2477 name.GetCString(), append, max_matches, num_matches);
2478 }
2479 }
2480 return num_matches;
2481 } else {
2482 UpdateExternalModuleListIfNeeded();
2483
2484 for (const auto &pair : m_external_type_modules) {
2485 ModuleSP external_module_sp = pair.second;
2486 if (external_module_sp) {
2487 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2488 if (sym_vendor) {
2489 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002490 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2491 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002492 if (num_external_matches)
2493 return num_external_matches;
2494 }
2495 }
2496 }
2497 }
2498
2499 return 0;
2500}
2501
2502size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2503 bool append, TypeMap &types) {
2504 if (!append)
2505 types.Clear();
2506
2507 if (context.empty())
2508 return 0;
2509
Kate Stoneb9c1b512016-09-06 20:57:50 +00002510 ConstString name = context.back().name;
2511
2512 if (!name)
2513 return 0;
2514
Pavel Labathb13f0332018-05-21 14:12:52 +00002515 DIEArray die_offsets;
2516 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002517 const size_t num_die_matches = die_offsets.size();
2518
2519 if (num_die_matches) {
2520 size_t num_matches = 0;
2521 for (size_t i = 0; i < num_die_matches; ++i) {
2522 const DIERef &die_ref = die_offsets[i];
2523 DWARFDIE die = GetDIE(die_ref);
2524
2525 if (die) {
2526 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002527 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002528 if (die_context != context)
2529 continue;
2530
2531 Type *matching_type = ResolveType(die, true, true);
2532 if (matching_type) {
2533 // We found a type pointer, now find the shared pointer form our type
2534 // list
2535 types.InsertUnique(matching_type->shared_from_this());
2536 ++num_matches;
2537 }
2538 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002539 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2540 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002541 }
2542 }
2543 return num_matches;
2544 }
2545 return 0;
2546}
2547
2548CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002549SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002550 const CompilerDeclContext *parent_decl_ctx) {
2551 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2552
2553 if (log) {
2554 GetObjectFile()->GetModule()->LogMessage(
2555 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2556 name.GetCString());
2557 }
2558
2559 CompilerDeclContext namespace_decl_ctx;
2560
2561 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2562 return namespace_decl_ctx;
2563
2564 DWARFDebugInfo *info = DebugInfo();
2565 if (info) {
2566 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002567 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 const size_t num_matches = die_offsets.size();
2569 if (num_matches) {
2570 for (size_t i = 0; i < num_matches; ++i) {
2571 const DIERef &die_ref = die_offsets[i];
2572 DWARFDIE die = GetDIE(die_ref);
2573
2574 if (die) {
2575 if (!DIEInDeclContext(parent_decl_ctx, die))
2576 continue; // The containing decl contexts don't match
2577
2578 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2579 if (dwarf_ast) {
2580 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2581 if (namespace_decl_ctx)
2582 break;
2583 }
2584 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002585 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2586 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002587 }
2588 }
2589 }
2590 }
2591 if (log && namespace_decl_ctx) {
2592 GetObjectFile()->GetModule()->LogMessage(
2593 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2594 "CompilerDeclContext(%p/%p) \"%s\"",
2595 name.GetCString(),
2596 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2597 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2598 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2599 }
2600
2601 return namespace_decl_ctx;
2602}
2603
2604TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2605 bool resolve_function_context) {
2606 TypeSP type_sp;
2607 if (die) {
2608 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2609 if (type_ptr == NULL) {
2610 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2611 assert(lldb_cu);
2612 SymbolContext sc(lldb_cu);
2613 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2614 while (parent_die != nullptr) {
2615 if (parent_die->Tag() == DW_TAG_subprogram)
2616 break;
2617 parent_die = parent_die->GetParent();
2618 }
2619 SymbolContext sc_backup = sc;
2620 if (resolve_function_context && parent_die != nullptr &&
2621 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2622 sc = sc_backup;
2623
2624 type_sp = ParseType(sc, die, NULL);
2625 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2626 // Grab the existing type from the master types lists
2627 type_sp = type_ptr->shared_from_this();
2628 }
2629 }
2630 return type_sp;
2631}
2632
2633DWARFDIE
2634SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2635 if (orig_die) {
2636 DWARFDIE die = orig_die;
2637
2638 while (die) {
2639 // If this is the original DIE that we are searching for a declaration
2640 // for, then don't look in the cache as we don't want our own decl
2641 // context to be our decl context...
2642 if (orig_die != die) {
2643 switch (die.Tag()) {
2644 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002645 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002646 case DW_TAG_namespace:
2647 case DW_TAG_structure_type:
2648 case DW_TAG_union_type:
2649 case DW_TAG_class_type:
2650 case DW_TAG_lexical_block:
2651 case DW_TAG_subprogram:
2652 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002653 case DW_TAG_inlined_subroutine: {
2654 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2655 if (abs_die) {
2656 return abs_die;
2657 }
2658 break;
2659 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002660 default:
2661 break;
2662 }
2663 }
2664
2665 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2666 if (spec_die) {
2667 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2668 if (decl_ctx_die)
2669 return decl_ctx_die;
2670 }
2671
2672 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2673 if (abs_die) {
2674 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2675 if (decl_ctx_die)
2676 return decl_ctx_die;
2677 }
2678
2679 die = die.GetParent();
2680 }
2681 }
2682 return DWARFDIE();
2683}
2684
2685Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002686SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687 Symbol *objc_class_symbol = NULL;
2688 if (m_obj_file) {
2689 Symtab *symtab = m_obj_file->GetSymtab();
2690 if (symtab) {
2691 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2692 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2693 Symtab::eVisibilityAny);
2694 }
2695 }
2696 return objc_class_symbol;
2697}
2698
2699// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002700// they don't then we can end up looking through all class types for a complete
2701// type and never find the full definition. We need to know if this attribute
2702// is supported, so we determine this here and cache th result. We also need to
2703// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002704// DWARF file
2705// if we are doing darwin DWARF in .o file debugging.
2706bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002707 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002708 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2709 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2710 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2711 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2712 else {
2713 DWARFDebugInfo *debug_info = DebugInfo();
2714 const uint32_t num_compile_units = GetNumCompileUnits();
2715 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002716 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002717 if (dwarf_cu != cu &&
2718 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2719 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2720 break;
2721 }
2722 }
2723 }
2724 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2725 GetDebugMapSymfile())
2726 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2727 }
2728 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002729}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002730
2731// This function can be used when a DIE is found that is a forward declaration
2732// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002733TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002734 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002736
Kate Stoneb9c1b512016-09-06 20:57:50 +00002737 TypeSP type_sp;
2738
2739 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2740 return type_sp;
2741
2742 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002743 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002744
2745 const size_t num_matches = die_offsets.size();
2746
2747 if (num_matches) {
2748 for (size_t i = 0; i < num_matches; ++i) {
2749 const DIERef &die_ref = die_offsets[i];
2750 DWARFDIE type_die = GetDIE(die_ref);
2751
2752 if (type_die) {
2753 bool try_resolving_type = false;
2754
Adrian Prantl05097242018-04-30 16:49:04 +00002755 // Don't try and resolve the DIE we are looking for with the DIE
2756 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002757 if (type_die != die) {
2758 switch (type_die.Tag()) {
2759 case DW_TAG_class_type:
2760 case DW_TAG_structure_type:
2761 try_resolving_type = true;
2762 break;
2763 default:
2764 break;
2765 }
2766 }
2767
2768 if (try_resolving_type) {
2769 if (must_be_implementation &&
2770 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2771 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2772 DW_AT_APPLE_objc_complete_type, 0);
2773
2774 if (try_resolving_type) {
2775 Type *resolved_type = ResolveType(type_die, false, true);
2776 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2777 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2778 " (cu 0x%8.8" PRIx64 ")\n",
2779 die.GetID(),
2780 m_obj_file->GetFileSpec().GetFilename().AsCString(
2781 "<Unknown>"),
2782 type_die.GetID(), type_cu->GetID());
2783
2784 if (die)
2785 GetDIEToType()[die.GetDIE()] = resolved_type;
2786 type_sp = resolved_type->shared_from_this();
2787 break;
2788 }
2789 }
2790 }
2791 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002792 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2793 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 }
2795 }
2796 }
2797 return type_sp;
2798}
Greg Claytona8022fa2012-04-24 21:22:41 +00002799
Greg Clayton80c26302012-02-05 06:12:47 +00002800//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002801// This function helps to ensure that the declaration contexts match for two
2802// different DIEs. Often times debug information will refer to a forward
2803// declaration of a type (the equivalent of "struct my_struct;". There will
2804// often be a declaration of that type elsewhere that has the full definition.
2805// When we go looking for the full type "my_struct", we will find one or more
2806// matches in the accelerator tables and we will then need to make sure the
2807// type was in the same declaration context as the original DIE. This function
2808// can efficiently compare two DIEs and will return true when the declaration
2809// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002810//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002811bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2812 const DWARFDIE &die2) {
2813 if (die1 == die2)
2814 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002815
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002816 std::vector<DWARFDIE> decl_ctx_1;
2817 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002818 // The declaration DIE stack is a stack of the declaration context DIEs all
2819 // the way back to the compile unit. If a type "T" is declared inside a class
2820 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2821 // namespace "lldb", and the namespace is in a compile unit, there will be a
2822 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 //
2824 // [0] DW_TAG_class_type for "B"
2825 // [1] DW_TAG_class_type for "A"
2826 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002827 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002828 //
Adrian Prantl05097242018-04-30 16:49:04 +00002829 // We grab both contexts and make sure that everything matches all the way
2830 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831
2832 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002833 decl_ctx_1 = die1.GetDeclContextDIEs();
2834 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002835 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002836 const size_t count1 = decl_ctx_1.size();
2837 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 if (count1 != count2)
2839 return false;
2840
Adrian Prantl05097242018-04-30 16:49:04 +00002841 // Make sure the DW_TAG values match all the way back up the compile unit. If
2842 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002843 DWARFDIE decl_ctx_die1;
2844 DWARFDIE decl_ctx_die2;
2845 size_t i;
2846 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002847 decl_ctx_die1 = decl_ctx_1[i];
2848 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002849 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2850 return false;
2851 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002852#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002853
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002855 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2856 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2857 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002858 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002859 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2860 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002861
Greg Clayton890ff562012-02-02 05:48:16 +00002862#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002863 // Always skip the compile unit when comparing by only iterating up to "count
2864 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002865 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002866 decl_ctx_die1 = decl_ctx_1[i];
2867 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002868 const char *name1 = decl_ctx_die1.GetName();
2869 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002870 // If the string was from a DW_FORM_strp, then the pointer will often be
2871 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002872 if (name1 == name2)
2873 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002874
Adrian Prantl05097242018-04-30 16:49:04 +00002875 // Name pointers are not equal, so only compare the strings if both are not
2876 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002877 if (name1 && name2) {
2878 // If the strings don't compare, we are done...
2879 if (strcmp(name1, name2) != 0)
2880 return false;
2881 } else {
2882 // One name was NULL while the other wasn't
2883 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002884 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 }
Adrian Prantl05097242018-04-30 16:49:04 +00002886 // We made it through all of the checks and the declaration contexts are
2887 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002888 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002889}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002890
Kate Stoneb9c1b512016-09-06 20:57:50 +00002891TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2892 const DWARFDeclContext &dwarf_decl_ctx) {
2893 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002894
Kate Stoneb9c1b512016-09-06 20:57:50 +00002895 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2896 if (dwarf_decl_ctx_count > 0) {
2897 const ConstString type_name(dwarf_decl_ctx[0].name);
2898 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002899
Kate Stoneb9c1b512016-09-06 20:57:50 +00002900 if (type_name) {
2901 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2902 DWARF_LOG_LOOKUPS));
2903 if (log) {
2904 GetObjectFile()->GetModule()->LogMessage(
2905 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2906 "s, qualified-name='%s')",
2907 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2908 dwarf_decl_ctx.GetQualifiedName());
2909 }
2910
2911 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002912 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002913 const size_t num_matches = die_offsets.size();
2914
Adrian Prantl05097242018-04-30 16:49:04 +00002915 // Get the type system that we are looking to find a type for. We will
2916 // use this to ensure any matches we find are in a language that this
2917 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002918 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2919 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2920 ? nullptr
2921 : GetTypeSystemForLanguage(language);
2922
2923 if (num_matches) {
2924 for (size_t i = 0; i < num_matches; ++i) {
2925 const DIERef &die_ref = die_offsets[i];
2926 DWARFDIE type_die = GetDIE(die_ref);
2927
2928 if (type_die) {
2929 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002930 // looking for. We don't want to find a "Foo" type from Java if we
2931 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002932 if (type_system &&
2933 !type_system->SupportsLanguage(type_die.GetLanguage()))
2934 continue;
2935 bool try_resolving_type = false;
2936
2937 // Don't try and resolve the DIE we are looking for with the DIE
2938 // itself!
2939 const dw_tag_t type_tag = type_die.Tag();
2940 // Make sure the tags match
2941 if (type_tag == tag) {
2942 // The tags match, lets try resolving this type
2943 try_resolving_type = true;
2944 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002945 // The tags don't match, but we need to watch our for a forward
2946 // declaration for a struct and ("struct foo") ends up being a
2947 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002948 switch (type_tag) {
2949 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002950 // We had a "class foo", see if we ended up with a "struct foo
2951 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002952 try_resolving_type = (tag == DW_TAG_structure_type);
2953 break;
2954 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002955 // We had a "struct foo", see if we ended up with a "class foo
2956 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002957 try_resolving_type = (tag == DW_TAG_class_type);
2958 break;
2959 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002960 // Tags don't match, don't event try to resolve using this type
2961 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002962 break;
2963 }
2964 }
2965
2966 if (try_resolving_type) {
2967 DWARFDeclContext type_dwarf_decl_ctx;
2968 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2969
2970 if (log) {
2971 GetObjectFile()->GetModule()->LogMessage(
2972 log, "SymbolFileDWARF::"
2973 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2974 "qualified-name='%s') trying die=0x%8.8x (%s)",
2975 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2976 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2977 type_dwarf_decl_ctx.GetQualifiedName());
2978 }
2979
2980 // Make sure the decl contexts match all the way up
2981 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2982 Type *resolved_type = ResolveType(type_die, false);
2983 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2984 type_sp = resolved_type->shared_from_this();
2985 break;
2986 }
2987 }
2988 } else {
2989 if (log) {
2990 std::string qualified_name;
2991 type_die.GetQualifiedName(qualified_name);
2992 GetObjectFile()->GetModule()->LogMessage(
2993 log, "SymbolFileDWARF::"
2994 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2995 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2996 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2997 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2998 qualified_name.c_str());
2999 }
3000 }
3001 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00003002 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
3003 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003004 }
3005 }
3006 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003007 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003008 }
3009 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003010}
3011
Kate Stoneb9c1b512016-09-06 20:57:50 +00003012TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3013 bool *type_is_new_ptr) {
3014 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003015
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 if (die) {
3017 TypeSystem *type_system =
3018 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003019
Kate Stoneb9c1b512016-09-06 20:57:50 +00003020 if (type_system) {
3021 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3022 if (dwarf_ast) {
3023 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3024 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3025 if (type_sp) {
3026 TypeList *type_list = GetTypeList();
3027 if (type_list)
3028 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003029
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030 if (die.Tag() == DW_TAG_subprogram) {
3031 DIERef die_ref = die.GetDIERef();
3032 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3033 .GetScopeQualifiedName()
3034 .AsCString(""));
3035 if (scope_qualified_name.size()) {
3036 NameToOffsetMap::iterator iter =
3037 m_function_scope_qualified_name_map.find(
3038 scope_qualified_name);
3039 if (iter != m_function_scope_qualified_name_map.end())
3040 (*iter).second->insert(die_ref);
3041 else {
3042 DIERefSetSP new_set(new std::set<DIERef>);
3043 new_set->insert(die_ref);
3044 m_function_scope_qualified_name_map.emplace(
3045 std::make_pair(scope_qualified_name, new_set));
3046 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003048 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003049 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052 }
3053
3054 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003055}
3056
Kate Stoneb9c1b512016-09-06 20:57:50 +00003057size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3058 const DWARFDIE &orig_die,
3059 bool parse_siblings, bool parse_children) {
3060 size_t types_added = 0;
3061 DWARFDIE die = orig_die;
3062 while (die) {
3063 bool type_is_new = false;
3064 if (ParseType(sc, die, &type_is_new).get()) {
3065 if (type_is_new)
3066 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068
3069 if (parse_children && die.HasChildren()) {
3070 if (die.Tag() == DW_TAG_subprogram) {
3071 SymbolContext child_sc(sc);
3072 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3073 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3074 } else
3075 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3076 }
3077
3078 if (parse_siblings)
3079 die = die.GetSibling();
3080 else
3081 die.Clear();
3082 }
3083 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003084}
3085
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003086size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003087 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003088 CompileUnit *comp_unit = func.GetCompileUnit();
3089 lldbassert(comp_unit);
3090
3091 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3092 if (!dwarf_cu)
3093 return 0;
3094
Kate Stoneb9c1b512016-09-06 20:57:50 +00003095 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003096 const dw_offset_t function_die_offset = func.GetID();
3097 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3098 if (function_die) {
3099 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3100 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003101 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003102
Kate Stoneb9c1b512016-09-06 20:57:50 +00003103 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003104}
3105
Zachary Turner863f8c12019-01-11 18:03:20 +00003106size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003107 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003108 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003109 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003110 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003111 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3112 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3113 SymbolContext sc;
3114 sc.comp_unit = &comp_unit;
3115 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003118
Kate Stoneb9c1b512016-09-06 20:57:50 +00003119 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003120}
3121
Kate Stoneb9c1b512016-09-06 20:57:50 +00003122size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003123 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003124 if (sc.comp_unit != NULL) {
3125 DWARFDebugInfo *info = DebugInfo();
3126 if (info == NULL)
3127 return 0;
3128
3129 if (sc.function) {
3130 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3131
3132 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3133 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3134 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3135 const size_t num_variables = ParseVariables(
3136 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3137
3138 // Let all blocks know they have parse all their variables
3139 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3140 return num_variables;
3141 }
3142 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003143 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144
3145 if (dwarf_cu == NULL)
3146 return 0;
3147
3148 uint32_t vars_added = 0;
3149 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3150
3151 if (variables.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003152 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153 sc.comp_unit->SetVariableList(variables);
3154
3155 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003156 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 const size_t num_matches = die_offsets.size();
3158 if (num_matches) {
3159 for (size_t i = 0; i < num_matches; ++i) {
3160 const DIERef &die_ref = die_offsets[i];
3161 DWARFDIE die = GetDIE(die_ref);
3162 if (die) {
3163 VariableSP var_sp(
3164 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3165 if (var_sp) {
3166 variables->AddVariableIfUnique(var_sp);
3167 ++vars_added;
3168 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003169 } else
3170 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003171 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003172 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 }
3174 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003176 }
3177 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003178}
3179
Kate Stoneb9c1b512016-09-06 20:57:50 +00003180VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3181 const DWARFDIE &die,
3182 const lldb::addr_t func_low_pc) {
3183 if (die.GetDWARF() != this)
3184 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003185
Kate Stoneb9c1b512016-09-06 20:57:50 +00003186 VariableSP var_sp;
3187 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003188 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003189
Kate Stoneb9c1b512016-09-06 20:57:50 +00003190 var_sp = GetDIEToVariable()[die.GetDIE()];
3191 if (var_sp)
3192 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003193
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194 const dw_tag_t tag = die.Tag();
3195 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003196
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3198 (tag == DW_TAG_formal_parameter && sc.function)) {
3199 DWARFAttributes attributes;
3200 const size_t num_attributes = die.GetAttributes(attributes);
3201 DWARFDIE spec_die;
3202 if (num_attributes > 0) {
3203 const char *name = NULL;
3204 const char *mangled = NULL;
3205 Declaration decl;
3206 uint32_t i;
3207 DWARFFormValue type_die_form;
3208 DWARFExpression location(die.GetCU());
3209 bool is_external = false;
3210 bool is_artificial = false;
3211 bool location_is_const_value_data = false;
3212 bool has_explicit_location = false;
3213 DWARFFormValue const_value;
3214 Variable::RangeList scope_ranges;
3215 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003216
Kate Stoneb9c1b512016-09-06 20:57:50 +00003217 for (i = 0; i < num_attributes; ++i) {
3218 dw_attr_t attr = attributes.AttributeAtIndex(i);
3219 DWARFFormValue form_value;
3220
3221 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3222 switch (attr) {
3223 case DW_AT_decl_file:
3224 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3225 form_value.Unsigned()));
3226 break;
3227 case DW_AT_decl_line:
3228 decl.SetLine(form_value.Unsigned());
3229 break;
3230 case DW_AT_decl_column:
3231 decl.SetColumn(form_value.Unsigned());
3232 break;
3233 case DW_AT_name:
3234 name = form_value.AsCString();
3235 break;
3236 case DW_AT_linkage_name:
3237 case DW_AT_MIPS_linkage_name:
3238 mangled = form_value.AsCString();
3239 break;
3240 case DW_AT_type:
3241 type_die_form = form_value;
3242 break;
3243 case DW_AT_external:
3244 is_external = form_value.Boolean();
3245 break;
3246 case DW_AT_const_value:
3247 // If we have already found a DW_AT_location attribute, ignore this
3248 // attribute.
3249 if (!has_explicit_location) {
3250 location_is_const_value_data = true;
3251 // The constant value will be either a block, a data value or a
3252 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003253 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003254 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3255 // Retrieve the value as a block expression.
3256 uint32_t block_offset =
3257 form_value.BlockData() - debug_info_data.GetDataStart();
3258 uint32_t block_length = form_value.Unsigned();
3259 location.CopyOpcodeData(module, debug_info_data, block_offset,
3260 block_length);
3261 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3262 // Retrieve the value as a data expression.
3263 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3264 DWARFFormValue::GetFixedFormSizesForAddressSize(
Zachary Turner7e44a842019-03-12 20:51:05 +00003265 attributes.CompileUnitAtIndex(i)->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003266 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3267 uint32_t data_length =
3268 fixed_form_sizes.GetSize(form_value.Form());
3269 if (data_length == 0) {
3270 const uint8_t *data_pointer = form_value.BlockData();
3271 if (data_pointer) {
3272 form_value.Unsigned();
3273 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3274 // we need to get the byte size of the type later after we
3275 // create the variable
3276 const_value = form_value;
3277 }
3278 } else
3279 location.CopyOpcodeData(module, debug_info_data, data_offset,
3280 data_length);
3281 } else {
3282 // Retrieve the value as a string expression.
3283 if (form_value.Form() == DW_FORM_strp) {
3284 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3285 DWARFFormValue::GetFixedFormSizesForAddressSize(
3286 attributes.CompileUnitAtIndex(i)
Zachary Turner7e44a842019-03-12 20:51:05 +00003287 ->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003288 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3289 uint32_t data_length =
3290 fixed_form_sizes.GetSize(form_value.Form());
3291 location.CopyOpcodeData(module, debug_info_data, data_offset,
3292 data_length);
3293 } else {
3294 const char *str = form_value.AsCString();
3295 uint32_t string_offset =
3296 str - (const char *)debug_info_data.GetDataStart();
3297 uint32_t string_length = strlen(str) + 1;
3298 location.CopyOpcodeData(module, debug_info_data,
3299 string_offset, string_length);
3300 }
3301 }
3302 }
3303 break;
3304 case DW_AT_location: {
3305 location_is_const_value_data = false;
3306 has_explicit_location = true;
3307 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003308 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003309
3310 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003311 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003312 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003313 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003314 } else {
George Rimare4dee262018-10-23 09:46:15 +00003315 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3317
3318 size_t loc_list_length = DWARFExpression::LocationListSize(
3319 die.GetCU(), debug_loc_data, debug_loc_offset);
3320 if (loc_list_length > 0) {
3321 location.CopyOpcodeData(module, debug_loc_data,
3322 debug_loc_offset, loc_list_length);
3323 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3324 location.SetLocationListSlide(
3325 func_low_pc -
3326 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3327 }
3328 }
3329 } break;
3330 case DW_AT_specification:
3331 spec_die = GetDIE(DIERef(form_value));
3332 break;
3333 case DW_AT_start_scope: {
3334 if (form_value.Form() == DW_FORM_sec_offset) {
3335 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003336 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3337 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003338 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003339 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003340 } else {
3341 // TODO: Handle the case when DW_AT_start_scope have form
3342 // constant. The
3343 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003344 // behavior in case the enclosing block have a non coninious
3345 // address range and the DW_AT_start_scope entry have a form
3346 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003347 GetObjectFile()->GetModule()->ReportWarning(
3348 "0x%8.8" PRIx64
3349 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3350 die.GetID(), form_value.Form());
3351 }
3352
3353 scope_ranges.Sort();
3354 scope_ranges.CombineConsecutiveRanges();
3355 } break;
3356 case DW_AT_artificial:
3357 is_artificial = form_value.Boolean();
3358 break;
3359 case DW_AT_accessibility:
3360 break; // accessibility =
3361 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3362 case DW_AT_declaration:
3363 case DW_AT_description:
3364 case DW_AT_endianity:
3365 case DW_AT_segment:
3366 case DW_AT_visibility:
3367 default:
3368 case DW_AT_abstract_origin:
3369 case DW_AT_sibling:
3370 break;
3371 }
3372 }
3373 }
3374
3375 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3376 const dw_tag_t parent_tag = die.GetParent().Tag();
3377 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003378 (parent_tag == DW_TAG_compile_unit ||
3379 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003380 (parent_context_die.Tag() == DW_TAG_class_type ||
3381 parent_context_die.Tag() == DW_TAG_structure_type);
3382
3383 ValueType scope = eValueTypeInvalid;
3384
3385 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3386 SymbolContextScope *symbol_context_scope = NULL;
3387
Sam McCall30621392016-11-22 11:40:25 +00003388 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003389 if (!mangled) {
3390 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003391 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3392 // of global variables with commands like "frame var j". For
3393 // example, if j were an int variable holding a value 4 and
3394 // declared in a namespace B which in turn is contained in a
3395 // namespace A, the command "frame var j" returns
3396 // "(int) A::B::j = 4".
3397 // If the compiler does not emit a linkage name, we should be
3398 // able to generate a fully qualified name from the
3399 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003400 if ((parent_tag == DW_TAG_compile_unit ||
3401 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003402 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3403 DWARFDeclContext decl_ctx;
3404
3405 die.GetDWARFDeclContext(decl_ctx);
3406 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3407 }
3408 }
3409
Kate Stoneb9c1b512016-09-06 20:57:50 +00003410 if (tag == DW_TAG_formal_parameter)
3411 scope = eValueTypeVariableArgument;
3412 else {
Sam McCall30621392016-11-22 11:40:25 +00003413 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3414 // or static variable, so we have to do a little digging:
3415 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3416 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3417 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3418 // Clang likes to combine small global variables into the same symbol
3419 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3420 // so we need to look through the whole expression.
3421 bool is_static_lifetime =
3422 has_explicit_mangled ||
3423 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003424 // Check if the location has a DW_OP_addr with any address value...
3425 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3426 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003427 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3429 if (op_error) {
3430 StreamString strm;
3431 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3432 NULL);
3433 GetObjectFile()->GetModule()->ReportError(
3434 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003435 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003436 }
Sam McCall30621392016-11-22 11:40:25 +00003437 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3438 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003439 }
3440 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003441 if (debug_map_symfile)
3442 // Set the module of the expression to the linked module
3443 // instead of the oject file so the relocated address can be
3444 // found there.
3445 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003446
Sam McCall30621392016-11-22 11:40:25 +00003447 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003448 if (is_external)
3449 scope = eValueTypeVariableGlobal;
3450 else
3451 scope = eValueTypeVariableStatic;
3452
3453 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003454 // When leaving the DWARF in the .o files on darwin, when we have a
3455 // global variable that wasn't initialized, the .o file might not
3456 // have allocated a virtual address for the global variable. In
3457 // this case it will have created a symbol for the global variable
3458 // that is undefined/data and external and the value will be the
3459 // byte size of the variable. When we do the address map in
3460 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3461 // do some magic here so we can get the correct address for our
3462 // global variable. The address for all of these entries will be
3463 // zero, and there will be an undefined symbol in this object file,
3464 // and the executable will have a matching symbol with a good
3465 // address. So here we dig up the correct address and replace it in
3466 // the location for the variable, and set the variable's symbol
3467 // context scope to be that of the main executable so the file
3468 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003469 bool linked_oso_file_addr = false;
3470 if (is_external && location_DW_OP_addr == 0) {
3471 // we have a possible uninitialized extern global
3472 ConstString const_name(mangled ? mangled : name);
3473 ObjectFile *debug_map_objfile =
3474 debug_map_symfile->GetObjectFile();
3475 if (debug_map_objfile) {
3476 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3477 if (debug_map_symtab) {
3478 Symbol *exe_symbol =
3479 debug_map_symtab->FindFirstSymbolWithNameAndType(
3480 const_name, eSymbolTypeData, Symtab::eDebugYes,
3481 Symtab::eVisibilityExtern);
3482 if (exe_symbol) {
3483 if (exe_symbol->ValueIsAddress()) {
3484 const addr_t exe_file_addr =
3485 exe_symbol->GetAddressRef().GetFileAddress();
3486 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3487 if (location.Update_DW_OP_addr(exe_file_addr)) {
3488 linked_oso_file_addr = true;
3489 symbol_context_scope = exe_symbol;
3490 }
3491 }
3492 }
3493 }
3494 }
3495 }
3496 }
3497
3498 if (!linked_oso_file_addr) {
3499 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003500 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003501 const lldb::addr_t exe_file_addr =
3502 debug_map_symfile->LinkOSOFileAddress(this,
3503 location_DW_OP_addr);
3504 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3505 // Update the file address for this variable
3506 location.Update_DW_OP_addr(exe_file_addr);
3507 } else {
3508 // Variable didn't make it into the final executable
3509 return var_sp;
3510 }
3511 }
3512 }
3513 } else {
3514 if (location_is_const_value_data)
3515 scope = eValueTypeVariableStatic;
3516 else {
3517 scope = eValueTypeVariableLocal;
3518 if (debug_map_symfile) {
3519 // We need to check for TLS addresses that we need to fixup
3520 if (location.ContainsThreadLocalStorage()) {
3521 location.LinkThreadLocalStorage(
3522 debug_map_symfile->GetObjectFile()->GetModule(),
3523 [this, debug_map_symfile](
3524 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3525 return debug_map_symfile->LinkOSOFileAddress(
3526 this, unlinked_file_addr);
3527 });
3528 scope = eValueTypeVariableThreadLocal;
3529 }
3530 }
3531 }
3532 }
3533 }
3534
3535 if (symbol_context_scope == NULL) {
3536 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003537 case DW_TAG_subprogram:
3538 case DW_TAG_inlined_subroutine:
3539 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003540 if (sc.function) {
3541 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3542 sc_parent_die.GetID());
3543 if (symbol_context_scope == NULL)
3544 symbol_context_scope = sc.function;
3545 }
3546 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003547
Kate Stoneb9c1b512016-09-06 20:57:50 +00003548 default:
3549 symbol_context_scope = sc.comp_unit;
3550 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003552 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003553
Kate Stoneb9c1b512016-09-06 20:57:50 +00003554 if (symbol_context_scope) {
3555 SymbolFileTypeSP type_sp(
3556 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3557
3558 if (const_value.Form() && type_sp && type_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00003559 location.CopyOpcodeData(
3560 const_value.Unsigned(),
3561 type_sp->GetType()->GetByteSize().getValueOr(0),
3562 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003563
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003564 var_sp = std::make_shared<Variable>(
3565 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3566 scope_ranges, &decl, location, is_external, is_artificial,
3567 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568
3569 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3570 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003571 // Not ready to parse this variable yet. It might be a global or static
3572 // variable that is in a function scope and the function in the symbol
3573 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003574 return var_sp;
3575 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003576 }
Adrian Prantl05097242018-04-30 16:49:04 +00003577 // Cache var_sp even if NULL (the variable was just a specification or was
3578 // missing vital information to be able to be displayed in the debugger
3579 // (missing location due to optimization, etc)) so we don't re-parse this
3580 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003581 GetDIEToVariable()[die.GetDIE()] = var_sp;
3582 if (spec_die)
3583 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3584 }
3585 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003586}
3587
Kate Stoneb9c1b512016-09-06 20:57:50 +00003588DWARFDIE
3589SymbolFileDWARF::FindBlockContainingSpecification(
3590 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3591 // Give the concrete function die specified by "func_die_offset", find the
3592 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3593 // to "spec_block_die_offset"
3594 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3595 spec_block_die_offset);
3596}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003597
Kate Stoneb9c1b512016-09-06 20:57:50 +00003598DWARFDIE
3599SymbolFileDWARF::FindBlockContainingSpecification(
3600 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3601 if (die) {
3602 switch (die.Tag()) {
3603 case DW_TAG_subprogram:
3604 case DW_TAG_inlined_subroutine:
3605 case DW_TAG_lexical_block: {
3606 if (die.GetAttributeValueAsReference(
3607 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3608 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003609
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3611 DW_INVALID_OFFSET) ==
3612 spec_block_die_offset)
3613 return die;
3614 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003616
3617 // Give the concrete function die specified by "func_die_offset", find the
3618 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3619 // to "spec_block_die_offset"
3620 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3621 child_die = child_die.GetSibling()) {
3622 DWARFDIE result_die =
3623 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3624 if (result_die)
3625 return result_die;
3626 }
3627 }
3628
3629 return DWARFDIE();
3630}
3631
3632size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3633 const DWARFDIE &orig_die,
3634 const lldb::addr_t func_low_pc,
3635 bool parse_siblings, bool parse_children,
3636 VariableList *cc_variable_list) {
3637 if (!orig_die)
3638 return 0;
3639
3640 VariableListSP variable_list_sp;
3641
3642 size_t vars_added = 0;
3643 DWARFDIE die = orig_die;
3644 while (die) {
3645 dw_tag_t tag = die.Tag();
3646
3647 // Check to see if we have already parsed this variable or constant?
3648 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3649 if (var_sp) {
3650 if (cc_variable_list)
3651 cc_variable_list->AddVariableIfUnique(var_sp);
3652 } else {
3653 // We haven't already parsed it, lets do that now.
3654 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3655 (tag == DW_TAG_formal_parameter && sc.function)) {
3656 if (variable_list_sp.get() == NULL) {
3657 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3658 dw_tag_t parent_tag = sc_parent_die.Tag();
3659 switch (parent_tag) {
3660 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003661 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003662 if (sc.comp_unit != NULL) {
3663 variable_list_sp = sc.comp_unit->GetVariableList(false);
3664 if (variable_list_sp.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003665 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003666 }
3667 } else {
3668 GetObjectFile()->GetModule()->ReportError(
3669 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3670 "symbol context for 0x%8.8" PRIx64
3671 " %s.\n",
3672 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3673 orig_die.GetID(), orig_die.GetTagAsCString());
3674 }
3675 break;
3676
3677 case DW_TAG_subprogram:
3678 case DW_TAG_inlined_subroutine:
3679 case DW_TAG_lexical_block:
3680 if (sc.function != NULL) {
3681 // Check to see if we already have parsed the variables for the
3682 // given scope
3683
3684 Block *block = sc.function->GetBlock(true).FindBlockByID(
3685 sc_parent_die.GetID());
3686 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003687 // This must be a specification or abstract origin with a
3688 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003689 // to find the concrete block so we can correctly add the
3690 // variable to it
3691 const DWARFDIE concrete_block_die =
3692 FindBlockContainingSpecification(
3693 DIERef(sc.function->GetID(), this),
3694 sc_parent_die.GetOffset());
3695 if (concrete_block_die)
3696 block = sc.function->GetBlock(true).FindBlockByID(
3697 concrete_block_die.GetID());
3698 }
3699
3700 if (block != NULL) {
3701 const bool can_create = false;
3702 variable_list_sp = block->GetBlockVariableList(can_create);
3703 if (variable_list_sp.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003704 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003705 block->SetVariableList(variable_list_sp);
3706 }
3707 }
3708 }
3709 break;
3710
3711 default:
3712 GetObjectFile()->GetModule()->ReportError(
3713 "didn't find appropriate parent DIE for variable list for "
3714 "0x%8.8" PRIx64 " %s.\n",
3715 orig_die.GetID(), orig_die.GetTagAsCString());
3716 break;
3717 }
3718 }
3719
3720 if (variable_list_sp) {
3721 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3722 if (var_sp) {
3723 variable_list_sp->AddVariableIfUnique(var_sp);
3724 if (cc_variable_list)
3725 cc_variable_list->AddVariableIfUnique(var_sp);
3726 ++vars_added;
3727 }
3728 }
3729 }
3730 }
3731
3732 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3733
3734 if (!skip_children && parse_children && die.HasChildren()) {
3735 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3736 true, cc_variable_list);
3737 }
3738
3739 if (parse_siblings)
3740 die = die.GetSibling();
3741 else
3742 die.Clear();
3743 }
3744 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003745}
3746
Vedant Kumar4b36f792018-10-05 23:23:15 +00003747/// Collect call graph edges present in a function DIE.
3748static std::vector<lldb_private::CallEdge>
3749CollectCallEdges(DWARFDIE function_die) {
3750 // Check if the function has a supported call site-related attribute.
3751 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3752 uint64_t has_call_edges =
3753 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3754 if (!has_call_edges)
3755 return {};
3756
3757 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3758 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3759 function_die.GetPubname());
3760
3761 // Scan the DIE for TAG_call_site entries.
3762 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3763 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3764 // For now, assume that all entries are nested directly under the subprogram
3765 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3766 std::vector<CallEdge> call_edges;
3767 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3768 child = child.GetSibling()) {
3769 if (child.Tag() != DW_TAG_call_site)
3770 continue;
3771
3772 // Extract DW_AT_call_origin (the call target's DIE).
3773 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3774 if (!call_origin.IsValid()) {
3775 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3776 function_die.GetPubname());
3777 continue;
3778 }
3779
3780 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3781 // available. It should only ever be unavailable for tail call edges, in
3782 // which case use LLDB_INVALID_ADDRESS.
3783 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3784 LLDB_INVALID_ADDRESS);
3785
3786 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3787 call_origin.GetPubname(), return_pc);
3788 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3789 }
3790 return call_edges;
3791}
3792
3793std::vector<lldb_private::CallEdge>
3794SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3795 DWARFDIE func_die = GetDIEFromUID(func_id.GetID());
3796 if (func_die.IsValid())
3797 return CollectCallEdges(func_die);
3798 return {};
3799}
3800
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003801//------------------------------------------------------------------
3802// PluginInterface protocol
3803//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003804ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3805
3806uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3807
Pavel Labath9337b412018-06-06 11:35:23 +00003808void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809
Zachary Turner49110232018-11-05 17:40:28 +00003810void SymbolFileDWARF::DumpClangAST(Stream &s) {
3811 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3812 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3813 if (!clang)
3814 return;
3815 clang->Dump(s);
3816}
3817
Kate Stoneb9c1b512016-09-06 20:57:50 +00003818SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3819 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3820 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3821 if (module_sp) {
3822 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3823 if (sym_vendor)
3824 m_debug_map_symfile =
3825 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003827 }
3828 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003829}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003830
3831DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003832SymbolFileDWARF::GetLocationListFormat() const {
George Rimare4dee262018-10-23 09:46:15 +00003833 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
3834 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003835 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003836}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003837
3838SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3839 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003840 ModuleSpec module_spec;
3841 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3842 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003843 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003844
3845 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3846 FileSpec dwp_filespec =
3847 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003848 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003849 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3850 dwp_filespec);
3851 }
3852 });
3853 return m_dwp_symfile.get();
3854}