blob: ae7e463766a49e95c23d37385eef147d46f0917b [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
Frederic Rissacbf0052019-04-23 20:17:04 +0000139 FileSpecList GetSymLinkPaths() {
140 const OptionValueFileSpecList *option_value =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 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
Frederic Rissacbf0052019-04-23 20:17:04 +0000162FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
Pavel Labath7d36d722019-01-16 12:30:41 +0000163 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(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000304 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 } else {
306 DWARFDebugInfo *info = DebugInfo();
307 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000308 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000310 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 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// Gets the first parent that is a lexical block, function or inlined
332// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000333DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
335 DWARFDIE die;
336 for (die = child_die.GetParent(); die; die = die.GetParent()) {
337 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 switch (tag) {
340 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000341 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 case DW_TAG_subprogram:
343 case DW_TAG_inlined_subroutine:
344 case DW_TAG_lexical_block:
345 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 }
348 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000352 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
353 << 32), // Used by SymbolFileDWARFDebugMap to
354 // when this class parses .o files to
355 // contain the .o file index/ID
Zachary Turner6e665122019-03-20 20:49:25 +0000356 m_debug_map_module_wp(), m_debug_map_symfile(NULL),
357 m_context(*objfile->GetModule()), m_data_debug_abbrev(),
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000358 m_data_debug_frame(), m_data_debug_info(), m_data_debug_line(),
359 m_data_debug_macro(), m_data_debug_loc(), m_data_debug_ranges(),
360 m_data_debug_rnglists(), m_data_debug_str(), m_data_apple_names(),
361 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
362 m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
364 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366SymbolFileDWARF::~SymbolFileDWARF() {}
367
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000368static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 static ConstString g_dwarf_section_name("__DWARF");
370 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371}
372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
374 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
375 if (debug_map_symfile)
376 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
377 else
378 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000379}
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
382 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
383 TypeSystem *type_system;
384 if (debug_map_symfile) {
385 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
386 } else {
387 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
388 if (type_system)
389 type_system->SetSymbolFile(this);
390 }
391 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000392}
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000395 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396
Pavel Labath9337b412018-06-06 11:35:23 +0000397 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
398 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
399 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
400 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
401 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
402 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403
Pavel Labath9337b412018-06-06 11:35:23 +0000404 m_index = AppleDWARFIndex::Create(
405 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
406 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407
Pavel Labath9337b412018-06-06 11:35:23 +0000408 if (m_index)
409 return;
410
411 DWARFDataExtractor debug_names;
412 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
413 if (debug_names.GetByteSize() > 0) {
414 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
415 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
416 debug_names, get_debug_str_data(),
417 DebugInfo());
418 if (index_or) {
419 m_index = std::move(*index_or);
420 return;
421 }
422 LLDB_LOG_ERROR(log, index_or.takeError(),
423 "Unable to read .debug_names data: {0}");
424 }
425 }
426
427 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
428 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429}
430
431bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000432 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433}
434
435uint32_t SymbolFileDWARF::CalculateAbilities() {
436 uint32_t abilities = 0;
437 if (m_obj_file != NULL) {
438 const Section *section = NULL;
439 const SectionList *section_list = m_obj_file->GetSectionList();
440 if (section_list == NULL)
441 return 0;
442
Adrian Prantl05097242018-04-30 16:49:04 +0000443 // On non Apple platforms we might have .debug_types debug info that is
444 // created by using "-fdebug-types-section". LLDB currently will try to
445 // load this debug info, but it causes crashes during debugging when types
446 // are missing since it doesn't know how to parse the info in the
447 // .debug_types type units. This causes all complex debug info types to be
448 // unresolved. Because this causes LLDB to crash and since it really
449 // doesn't provide a solid debuggiung experience, we should disable trying
450 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000451 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
452 m_obj_file->GetModule()->ReportWarning(
453 "lldb doesn’t support .debug_types debug info");
454 return 0;
455 }
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 uint64_t debug_abbrev_file_size = 0;
458 uint64_t debug_info_file_size = 0;
459 uint64_t debug_line_file_size = 0;
460
461 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
462
463 if (section)
464 section_list = &section->GetChildren();
465
466 section =
467 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
468 if (section != NULL) {
469 debug_info_file_size = section->GetFileSize();
470
471 section =
472 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
473 .get();
474 if (section)
475 debug_abbrev_file_size = section->GetFileSize();
476
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000477 DWARFDebugAbbrev *abbrev = DebugAbbrev();
478 if (abbrev) {
479 std::set<dw_form_t> invalid_forms;
480 abbrev->GetUnsupportedForms(invalid_forms);
481 if (!invalid_forms.empty()) {
482 StreamString error;
483 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
484 for (auto form : invalid_forms)
485 error.Printf(" %#x", form);
486 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
487 return 0;
488 }
489 }
490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 section =
492 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
493 .get();
494 if (section)
495 debug_line_file_size = section->GetFileSize();
496 } else {
497 const char *symfile_dir_cstr =
498 m_obj_file->GetFileSpec().GetDirectory().GetCString();
499 if (symfile_dir_cstr) {
500 if (strcasestr(symfile_dir_cstr, ".dsym")) {
501 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000502 // We have a dSYM file that didn't have a any debug info. If the
503 // string table has a size of 1, then it was made from an
504 // executable with no debug info, or from an executable that was
505 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 section =
507 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
508 .get();
509 if (section && section->GetFileSize() == 1) {
510 m_obj_file->GetModule()->ReportWarning(
511 "empty dSYM file detected, dSYM was created with an "
512 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518
519 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
520 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
521 LocalVariables | VariableTypes;
522
523 if (debug_line_file_size > 0)
524 abilities |= LineTables;
525 }
526 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527}
528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529const DWARFDataExtractor &
530SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
531 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000532 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
533 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
534 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
539 DWARFDataExtractor &data) {
540 ModuleSP module_sp(m_obj_file->GetModule());
541 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000542 if (!section_list)
543 return;
544
545 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
546 if (!section_sp)
547 return;
548
549 data.Clear();
550 m_obj_file->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
554 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
555 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556}
557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
559 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
560}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000562const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
563 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
567 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
568}
569
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000570const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
571 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
572}
573
574const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
575 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
576}
577
578const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
579 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
580}
581
582const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
583 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
584 if (debugLocData.GetByteSize() > 0)
585 return debugLocData;
586 return get_debug_loclists_data();
587}
588
589const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
590 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
591}
592
593const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
594 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
595 m_data_debug_loclists);
596}
597
598const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
599 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
600 m_data_debug_ranges);
601}
602
603const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
604 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
605 m_data_debug_rnglists);
606}
607
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
609 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
610}
611
612const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
613 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
614 m_data_debug_str_offsets);
615}
616
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000617const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
618 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
619}
620
621const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
622 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
623}
624
625const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
626 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
627}
628
629const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
630 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
631 m_data_apple_namespaces);
632}
633
634const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
635 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
636}
637
638const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
639 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
640 m_data_gnu_debugaltlink);
641}
642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000644 if (m_abbr)
645 return m_abbr.get();
646
647 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
648 if (debug_abbrev_data.GetByteSize() == 0)
649 return nullptr;
650
651 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
652 llvm::Error error = abbr->parse(debug_abbrev_data);
653 if (error) {
654 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
655 LLDB_LOG_ERROR(log, std::move(error),
656 "Unable to read .debug_abbrev section: {0}");
657 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000659
660 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662}
663
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
665 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000669 if (m_info == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000670 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
671 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
672 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 if (get_debug_info_data().GetByteSize() > 0) {
Zachary Turner6e665122019-03-20 20:49:25 +0000674 m_info = llvm::make_unique<DWARFDebugInfo>(m_context);
675 m_info->SetDwarfData(this);
Greg Clayton1f746072012-08-29 21:13:06 +0000676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 }
678 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679}
680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
682 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683}
684
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000685DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
687 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000688 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689
690 DWARFDebugInfo *info = DebugInfo();
691 if (info) {
Pavel Labath2a0cfcc2019-05-06 07:45:28 +0000692 // The compile unit ID is the index of the DWARF unit.
Jan Kratochvil60562732019-05-10 17:14:37 +0000693 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
695 dwarf_cu->SetUserData(comp_unit);
696 return dwarf_cu;
697 }
698 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699}
700
George Rimar7e7f9c12018-10-25 10:25:45 +0000701DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000702 if (m_ranges == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000703 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
704 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
705 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000706
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000707 if (get_debug_ranges_data().GetByteSize() > 0)
708 m_ranges.reset(new DWARFDebugRanges());
709 else if (get_debug_rnglists_data().GetByteSize() > 0)
710 m_ranges.reset(new DWARFDebugRngLists());
George Rimar6e357122018-10-10 08:11:15 +0000711
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000712 if (m_ranges)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000713 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 }
715 return m_ranges.get();
716}
717
George Rimar7e7f9c12018-10-25 10:25:45 +0000718const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 return m_ranges.get();
720}
721
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000722lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 uint32_t cu_idx) {
724 CompUnitSP cu_sp;
725 if (dwarf_cu) {
726 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
727 if (comp_unit) {
728 // We already parsed this compile unit, had out a shared pointer to it
729 cu_sp = comp_unit->shared_from_this();
730 } else {
731 if (dwarf_cu->GetSymbolFileDWARF() != this) {
732 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
733 cu_idx);
734 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
735 // Let the debug map create the compile unit
736 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
737 dwarf_cu->SetUserData(cu_sp.get());
738 } else {
739 ModuleSP module_sp(m_obj_file->GetModule());
740 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000741 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 if (cu_die) {
Pavel Labath7d36d722019-01-16 12:30:41 +0000743 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 if (cu_file_spec) {
745 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000746 // resolve the file. This can be expensive e.g. when the source
Pavel Labath7d36d722019-01-16 12:30:41 +0000747 // files are NFS mounted.
748 cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749
750 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000751 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000753 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
755
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000756 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
758
759 bool is_optimized = dwarf_cu->GetIsOptimized();
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000760 cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000762 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 if (cu_sp) {
764 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000765 // try and get the first entry in the supports files from the
766 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!cu_file_spec) {
768 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
769 if (cu_file_spec) {
770 (FileSpec &)(*cu_sp) = cu_file_spec;
771 // Also fix the invalid file spec which was copied from the
772 // compile unit.
773 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
774 }
775 }
776
777 dwarf_cu->SetUserData(cu_sp.get());
778
779 // Figure out the compile unit index if we weren't given one
780 if (cu_idx == UINT32_MAX)
Pavel Labath2a0cfcc2019-05-06 07:45:28 +0000781 cu_idx = dwarf_cu->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782
783 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
784 cu_idx, cu_sp);
785 }
786 }
787 }
788 }
789 }
790 }
791 return cu_sp;
792}
793
794uint32_t SymbolFileDWARF::GetNumCompileUnits() {
795 DWARFDebugInfo *info = DebugInfo();
796 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +0000797 return info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 return 0;
799}
800
801CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000802 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 CompUnitSP cu_sp;
804 DWARFDebugInfo *info = DebugInfo();
805 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000806 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 if (dwarf_cu)
808 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
809 }
810 return cu_sp;
811}
812
Zachary Turner863f8c12019-01-11 18:03:20 +0000813Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
814 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000815 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 if (die.IsValid()) {
817 TypeSystem *type_system =
818 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
819
820 if (type_system) {
821 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
822 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000823 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 }
825 }
826 return nullptr;
827}
828
829bool SymbolFileDWARF::FixupAddress(Address &addr) {
830 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
831 if (debug_map_symfile) {
832 return debug_map_symfile->LinkOSOAddress(addr);
833 }
834 // This is a normal DWARF file, no address fixups need to happen
835 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000836}
Zachary Turner863f8c12019-01-11 18:03:20 +0000837lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000838 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000839 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 if (dwarf_cu)
841 return dwarf_cu->GetLanguageType();
842 else
843 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000844}
845
Zachary Turner863f8c12019-01-11 18:03:20 +0000846size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000847 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000848 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000849 if (!dwarf_cu)
850 return 0;
851
852 size_t functions_added = 0;
853 std::vector<DWARFDIE> function_dies;
854 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
855 for (const DWARFDIE &die : function_dies) {
856 if (comp_unit.FindFunctionByUID(die.GetID()))
857 continue;
858 if (ParseFunction(comp_unit, die))
859 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000861 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863}
864
Zachary Turner863f8c12019-01-11 18:03:20 +0000865bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
866 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000867 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000868 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000869 if (dwarf_cu) {
870 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000872 if (cu_die) {
873 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
874 DW_AT_stmt_list, DW_INVALID_OFFSET);
875 if (stmt_list != DW_INVALID_OFFSET) {
876 // All file indexes in DWARF are one based and a file of index zero is
877 // supposed to be the compile unit itself.
878 support_files.Append(comp_unit);
879 return DWARFDebugLine::ParseSupportFiles(
880 comp_unit.GetModule(), get_debug_line_data(), stmt_list,
881 support_files, dwarf_cu);
882 }
883 }
884 }
885 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Zachary Turner863f8c12019-01-11 18:03:20 +0000888bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000889 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000890 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 if (dwarf_cu)
892 return dwarf_cu->GetIsOptimized();
893 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000894}
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896bool SymbolFileDWARF::ParseImportedModules(
897 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000898 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000899 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000901 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000902 if (!dwarf_cu)
903 return false;
904 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
905 sc.comp_unit->GetLanguage()))
906 return false;
907 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000909 const DWARFDIE die = dwarf_cu->DIE();
910 if (!die)
911 return false;
912
913 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
914 child_die = child_die.GetSibling()) {
915 if (child_die.Tag() != DW_TAG_imported_declaration)
916 continue;
917
918 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
919 if (module_die.Tag() != DW_TAG_module)
920 continue;
921
922 if (const char *name =
923 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
924 SourceModule module;
925 module.path.push_back(ConstString(name));
926
927 DWARFDIE parent_die = module_die;
928 while ((parent_die = parent_die.GetParent())) {
929 if (parent_die.Tag() != DW_TAG_module)
930 break;
931 if (const char *name =
932 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
933 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000935 std::reverse(module.path.begin(), module.path.end());
936 if (const char *include_path = module_die.GetAttributeValueAsString(
937 DW_AT_LLVM_include_path, nullptr))
938 module.search_path = ConstString(include_path);
939 if (const char *sysroot = module_die.GetAttributeValueAsString(
940 DW_AT_LLVM_isysroot, nullptr))
941 module.sysroot = ConstString(sysroot);
942 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000945 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000946}
947
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948struct ParseDWARFLineTableCallbackInfo {
949 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000950 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952};
953
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955static void ParseDWARFLineTableCallback(dw_offset_t offset,
956 const DWARFDebugLine::State &state,
957 void *userData) {
958 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
959 // Just started parsing the line table
960 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
961 // Done parsing line table, nothing to do for the cleanup
962 } else {
963 ParseDWARFLineTableCallbackInfo *info =
964 (ParseDWARFLineTableCallbackInfo *)userData;
965 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966
Adrian Prantl05097242018-04-30 16:49:04 +0000967 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000968 if (!info->sequence_up) {
969 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
970 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000973 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 state.column, state.file, state.is_stmt, state.basic_block,
975 state.prologue_end, state.epilogue_begin, state.end_sequence);
976 if (state.end_sequence) {
977 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000978 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000980 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 }
982 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983}
984
Zachary Turner863f8c12019-01-11 18:03:20 +0000985bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000986 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000987 if (comp_unit.GetLineTable() != NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989
Zachary Turner863f8c12019-01-11 18:03:20 +0000990 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000991 if (dwarf_cu) {
992 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
993 if (dwarf_cu_die) {
994 const dw_offset_t cu_line_offset =
995 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
996 DW_INVALID_OFFSET);
997 if (cu_line_offset != DW_INVALID_OFFSET) {
998 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
999 if (line_table_up) {
1000 ParseDWARFLineTableCallbackInfo info;
1001 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001002
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001003 /*
1004 * MIPS:
1005 * The SymbolContext may not have a valid target, thus we may not be
1006 * able
1007 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1008 * #0
1009 * for MIPS. Use ArchSpec to clear the bit #0.
1010 */
1011 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
1012 case llvm::Triple::mips:
1013 case llvm::Triple::mipsel:
1014 case llvm::Triple::mips64:
1015 case llvm::Triple::mips64el:
1016 info.addr_mask = ~((lldb::addr_t)1);
1017 break;
1018 default:
1019 info.addr_mask = ~((lldb::addr_t)0);
1020 break;
1021 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001022
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001023 lldb::offset_t offset = cu_line_offset;
1024 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1025 ParseDWARFLineTableCallback,
1026 &info, dwarf_cu);
1027 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1028 if (debug_map_symfile) {
1029 // We have an object file that has a line table with addresses that
1030 // are not linked. We need to link the line table and convert the
1031 // addresses that are relative to the .o file into addresses for
1032 // the main executable.
1033 comp_unit.SetLineTable(
1034 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
1035 } else {
1036 comp_unit.SetLineTable(line_table_up.release());
1037 return true;
1038 }
1039 }
1040 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 }
1043 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044}
1045
Siva Chandrad8335e92015-12-16 00:22:08 +00001046lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1048 auto iter = m_debug_macros_map.find(*offset);
1049 if (iter != m_debug_macros_map.end())
1050 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001051
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001052 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1053 if (debug_macro_data.GetByteSize() == 0)
1054 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1057 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001060 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1061 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1062 header.OffsetIs64Bit(), offset, this,
1063 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001066}
1067
Zachary Turner863f8c12019-01-11 18:03:20 +00001068bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001069 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001070
Zachary Turner863f8c12019-01-11 18:03:20 +00001071 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001073 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074
Jan Kratochvil93afb052018-05-24 20:51:13 +00001075 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 if (!dwarf_cu_die)
1077 return false;
1078
1079 lldb::offset_t sect_offset =
1080 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1081 if (sect_offset == DW_INVALID_OFFSET)
1082 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1083 DW_INVALID_OFFSET);
1084 if (sect_offset == DW_INVALID_OFFSET)
1085 return false;
1086
Zachary Turner863f8c12019-01-11 18:03:20 +00001087 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088
1089 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001090}
1091
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001092size_t SymbolFileDWARF::ParseBlocksRecursive(
1093 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1094 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 size_t blocks_added = 0;
1096 DWARFDIE die = orig_die;
1097 while (die) {
1098 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 switch (tag) {
1101 case DW_TAG_inlined_subroutine:
1102 case DW_TAG_subprogram:
1103 case DW_TAG_lexical_block: {
1104 Block *block = NULL;
1105 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001106 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1107 // inlined functions. These will be parsed on their own as separate
1108 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109
1110 if (depth > 0)
1111 break;
1112
1113 block = parent_block;
1114 } else {
1115 BlockSP block_sp(new Block(die.GetID()));
1116 parent_block->AddChild(block_sp);
1117 block = block_sp.get();
1118 }
1119 DWARFRangeList ranges;
1120 const char *name = NULL;
1121 const char *mangled_name = NULL;
1122
1123 int decl_file = 0;
1124 int decl_line = 0;
1125 int decl_column = 0;
1126 int call_file = 0;
1127 int call_line = 0;
1128 int call_column = 0;
1129 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1130 decl_line, decl_column, call_file, call_line,
1131 call_column, nullptr)) {
1132 if (tag == DW_TAG_subprogram) {
1133 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1134 subprogram_low_pc = ranges.GetMinRangeBase(0);
1135 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001136 // We get called here for inlined subroutines in two ways. The first
1137 // time is when we are making the Function object for this inlined
1138 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001140 // need to adjust the containing address. The second time is when we
1141 // are parsing the blocks inside the function that contains the
1142 // inlined concrete instance. Since these will be blocks inside the
1143 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1145 subprogram_low_pc = ranges.GetMinRangeBase(0);
1146 }
1147 }
1148
1149 const size_t num_ranges = ranges.GetSize();
1150 for (size_t i = 0; i < num_ranges; ++i) {
1151 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1152 const addr_t range_base = range.GetRangeBase();
1153 if (range_base >= subprogram_low_pc)
1154 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1155 range.GetByteSize()));
1156 else {
1157 GetObjectFile()->GetModule()->ReportError(
1158 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1159 ") which has a base that is less than the function's low PC "
1160 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1161 "start of this error message",
1162 block->GetID(), range_base, range.GetRangeEnd(),
1163 subprogram_low_pc);
1164 }
1165 }
1166 block->FinalizeRanges();
1167
1168 if (tag != DW_TAG_subprogram &&
1169 (name != NULL || mangled_name != NULL)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001170 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001172 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001173 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 decl_line, decl_column));
1175
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001176 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001178 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001179 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 call_line, call_column));
1181
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001182 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1183 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 }
1185
1186 ++blocks_added;
1187
1188 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001189 blocks_added +=
1190 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1191 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192 }
1193 }
1194 } break;
1195 default:
1196 break;
1197 }
1198
Adrian Prantl05097242018-04-30 16:49:04 +00001199 // Only parse siblings of the block if we are not at depth zero. A depth of
1200 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1201 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202
1203 if (depth == 0)
1204 die.Clear();
1205 else
1206 die = die.GetSibling();
1207 }
1208 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001209}
1210
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1212 if (parent_die) {
1213 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1214 die = die.GetSibling()) {
1215 dw_tag_t tag = die.Tag();
1216 bool check_virtuality = false;
1217 switch (tag) {
1218 case DW_TAG_inheritance:
1219 case DW_TAG_subprogram:
1220 check_virtuality = true;
1221 break;
1222 default:
1223 break;
1224 }
1225 if (check_virtuality) {
1226 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1227 return true;
1228 }
1229 }
1230 }
1231 return false;
1232}
1233
1234void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1235 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1236 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1237 std::vector<DWARFDIE> decl_ctx_die_list =
1238 ast_parser->GetDIEForDeclContext(decl_ctx);
1239
1240 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1241 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1242 decl = decl.GetSibling())
1243 ast_parser->GetDeclForUIDFromDWARF(decl);
1244}
1245
Pavel Labath2841e6e2019-05-08 11:43:05 +00001246SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001247 // This method can be called without going through the symbol vendor so we
1248 // need to lock the module.
1249 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001250 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1251 // must make sure we use the correct DWARF file when resolving things. On
1252 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1253 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1254 // references to other DWARF objects and we must be ready to receive a
1255 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001257 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1258 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labath2841e6e2019-05-08 11:43:05 +00001260 return {dwarf, {DW_INVALID_OFFSET, dw_offset_t(uid)}};
1261 }
Pavel Labath381ba9a2019-05-13 08:58:34 +00001262 uint32_t dwarf_id = uid >> 32;
1263 dw_offset_t die_offset = uid;
1264
1265 if (die_offset == DW_INVALID_OFFSET)
1266 return {nullptr, DIERef()};
1267
1268 SymbolFileDWARF *dwarf = this;
1269 if (DebugInfo()) {
1270 if (DWARFUnit *unit = DebugInfo()->GetUnitAtIndex(dwarf_id)) {
1271 if (unit->GetDwoSymbolFile())
1272 dwarf = unit->GetDwoSymbolFile();
1273 }
1274 }
1275 return {dwarf, {DW_INVALID_OFFSET, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001276}
1277
1278DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001279SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001280 // This method can be called without going through the symbol vendor so we
1281 // need to lock the module.
1282 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001283
1284 DecodedUID decoded = DecodeUID(uid);
1285
1286 if (decoded.dwarf)
1287 return decoded.dwarf->GetDIE(decoded.ref);
1288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001290}
1291
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001293 // This method can be called without going through the symbol vendor so we
1294 // need to lock the module.
1295 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001296 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001297 // SymbolFileDWARF::GetDIE(). See comments inside the
1298 // SymbolFileDWARF::GetDIE() for details.
1299 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 return die.GetDecl();
1301 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001302}
1303
Greg Clayton99558cc42015-08-24 23:46:31 +00001304CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001306 // This method can be called without going through the symbol vendor so we
1307 // need to lock the module.
1308 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001309 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001310 // SymbolFileDWARF::GetDIE(). See comments inside the
1311 // SymbolFileDWARF::GetDIE() for details.
1312 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 return die.GetDeclContext();
1314 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001315}
1316
Greg Clayton99558cc42015-08-24 23:46:31 +00001317CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001319 // This method can be called without going through the symbol vendor so we
1320 // need to lock the module.
1321 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001322 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001323 // SymbolFileDWARF::GetDIE(). See comments inside the
1324 // SymbolFileDWARF::GetDIE() for details.
1325 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 return die.GetContainingDeclContext();
1327 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328}
1329
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001331 // This method can be called without going through the symbol vendor so we
1332 // need to lock the module.
1333 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001334 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001335 // SymbolFileDWARF::GetDIE(). See comments inside the
1336 // SymbolFileDWARF::GetDIE() for details.
1337 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 return type_die.ResolveType();
1339 else
1340 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341}
1342
Adrian Prantleca07c52018-11-05 20:49:07 +00001343llvm::Optional<SymbolFile::ArrayInfo>
1344SymbolFileDWARF::GetDynamicArrayInfoForUID(
1345 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1346 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001347 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001348 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1349 else
1350 return llvm::None;
1351}
1352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1354 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001355}
1356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1358 bool assert_not_being_parsed) {
1359 if (die) {
1360 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1361 if (log)
1362 GetObjectFile()->GetModule()->LogMessage(
1363 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1364 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001365
Adrian Prantl05097242018-04-30 16:49:04 +00001366 // We might be coming in in the middle of a type tree (a class within a
1367 // class, an enum within a class), so parse any needed parent DIEs before
1368 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1370 if (decl_ctx_die) {
1371 if (log) {
1372 switch (decl_ctx_die.Tag()) {
1373 case DW_TAG_structure_type:
1374 case DW_TAG_union_type:
1375 case DW_TAG_class_type: {
1376 // Get the type, which could be a forward declaration
1377 if (log)
1378 GetObjectFile()->GetModule()->LogMessage(
1379 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1380 "resolve parent forward type for 0x%8.8x",
1381 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1382 decl_ctx_die.GetOffset());
1383 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001384
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 default:
1386 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001387 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 return ResolveType(die);
1391 }
1392 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001393}
1394
Greg Clayton6beaaa62011-01-17 03:46:26 +00001395// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001396// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1397// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398bool SymbolFileDWARF::HasForwardDeclForClangType(
1399 const CompilerType &compiler_type) {
1400 CompilerType compiler_type_no_qualifiers =
1401 ClangUtil::RemoveFastQualifiers(compiler_type);
1402 if (GetForwardDeclClangTypeToDie().count(
1403 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1404 return true;
1405 }
1406 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001407
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 ClangASTContext *clang_type_system =
1409 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1410 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001411 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 DWARFASTParserClang *ast_parser =
1413 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1414 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001415}
1416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001418 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001419
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 ClangASTContext *clang_type_system =
1421 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1422 if (clang_type_system) {
1423 DWARFASTParserClang *ast_parser =
1424 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1425 if (ast_parser &&
1426 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1427 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1428 }
Jim Inghamc3549282012-01-11 02:21:12 +00001429
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 // We have a struct/union/class/enum that needs to be fully resolved.
1431 CompilerType compiler_type_no_qualifiers =
1432 ClangUtil::RemoveFastQualifiers(compiler_type);
1433 auto die_it = GetForwardDeclClangTypeToDie().find(
1434 compiler_type_no_qualifiers.GetOpaqueQualType());
1435 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1436 // We have already resolved this type...
1437 return true;
1438 }
1439
1440 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1441 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001442 // Once we start resolving this type, remove it from the forward
1443 // declaration map in case anyone child members or other types require this
1444 // type to get resolved. The type will get resolved when all of the calls
1445 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 GetForwardDeclClangTypeToDie().erase(die_it);
1447
1448 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1449
1450 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1451 DWARF_LOG_TYPE_COMPLETION));
1452 if (log)
1453 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1454 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1455 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1456 type->GetName().AsCString());
1457 assert(compiler_type);
1458 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1459 if (dwarf_ast)
1460 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1461 }
1462 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001463}
1464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1466 bool assert_not_being_parsed,
1467 bool resolve_function_context) {
1468 if (die) {
1469 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1470
1471 if (assert_not_being_parsed) {
1472 if (type != DIE_IS_BEING_PARSED)
1473 return type;
1474
1475 GetObjectFile()->GetModule()->ReportError(
1476 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1477 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1478
1479 } else
1480 return type;
1481 }
1482 return nullptr;
1483}
1484
1485CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001486SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 uint32_t cu_idx) {
1488 // Check if the symbol vendor already knows about this compile unit?
1489 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001490 // The symbol vendor doesn't know about this compile unit, we need to parse
1491 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1493 }
1494 return (CompileUnit *)dwarf_cu->GetUserData();
1495}
1496
1497size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1498 DIEArray &method_die_offsets) {
1499 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001500 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001501 return method_die_offsets.size();
1502}
1503
1504bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1505 sc.Clear(false);
1506
1507 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1510
1511 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1512 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001513 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514
1515 if (sc.function) {
1516 sc.module_sp = sc.function->CalculateSymbolContextModule();
1517 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 }
1520
1521 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522}
1523
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1525 UpdateExternalModuleListIfNeeded();
1526 const auto &pos = m_external_type_modules.find(name);
1527 if (pos != m_external_type_modules.end())
1528 return pos->second;
1529 else
1530 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001531}
1532
Greg Clayton2f869fe2016-03-30 20:14:35 +00001533DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1535 DWARFDebugInfo *debug_info = DebugInfo();
1536 if (debug_info)
1537 return debug_info->GetDIE(die_ref);
1538 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001539 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001540}
1541
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542std::unique_ptr<SymbolFileDWARFDwo>
1543SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001544 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001546 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 // info files for modules.
1548 if (GetDebugMapSymfile())
1549 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001550
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 const char *dwo_name = cu_die.GetAttributeValueAsString(
1552 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1553 if (!dwo_name)
1554 return nullptr;
1555
Tamas Berghammer963ce482017-08-25 13:56:14 +00001556 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1557 if (dwp_symfile) {
1558 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1559 DW_AT_GNU_dwo_id, 0);
1560 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1561 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1562 if (dwo_symfile)
1563 return dwo_symfile;
1564 }
1565
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001566 FileSpec dwo_file(dwo_name);
1567 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 if (dwo_file.IsRelative()) {
1569 const char *comp_dir = cu_die.GetAttributeValueAsString(
1570 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1571 if (!comp_dir)
1572 return nullptr;
1573
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001574 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1575 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 dwo_file.AppendPathComponent(dwo_name);
1577 }
1578
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001579 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 return nullptr;
1581
1582 const lldb::offset_t file_offset = 0;
1583 DataBufferSP dwo_file_data_sp;
1584 lldb::offset_t dwo_file_data_offset = 0;
1585 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1586 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001587 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1588 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 if (dwo_obj_file == nullptr)
1590 return nullptr;
1591
1592 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001593}
1594
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1596 if (m_fetched_external_modules)
1597 return;
1598 m_fetched_external_modules = true;
1599
1600 DWARFDebugInfo *debug_info = DebugInfo();
1601
1602 const uint32_t num_compile_units = GetNumCompileUnits();
1603 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001604 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605
Jan Kratochvil93afb052018-05-24 20:51:13 +00001606 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001607 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1609
1610 if (name) {
1611 ConstString const_name(name);
1612 if (m_external_type_modules.find(const_name) ==
1613 m_external_type_modules.end()) {
1614 ModuleSP module_sp;
1615 const char *dwo_path =
1616 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1617 if (dwo_path) {
1618 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001619 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001620 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1622 const char *comp_dir =
1623 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1624 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001625 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001626 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001627 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1629 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001630 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 dwo_module_spec.GetArchitecture() =
1632 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001633
Adrian Prantl05097242018-04-30 16:49:04 +00001634 // When LLDB loads "external" modules it looks at the presence of
1635 // DW_AT_GNU_dwo_name. However, when the already created module
1636 // (corresponding to .dwo itself) is being processed, it will see
1637 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1638 // dwo file) and will try to call ModuleList::GetSharedModule
1639 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1640 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1641 // DW_AT_comp_dir. In this case the method
1642 // ModuleList::GetSharedModule will fail and the warning will be
1643 // printed. However, as one can notice in this case we don't
1644 // actually need to try to load the already loaded module
1645 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001646 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001647 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1648 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1649 continue;
1650 }
1651
Zachary Turner97206d52017-05-12 04:51:55 +00001652 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 dwo_module_spec, module_sp, NULL, NULL, NULL);
1654 if (!module_sp) {
1655 GetObjectFile()->GetModule()->ReportWarning(
1656 "0x%8.8x: unable to locate module needed for external types: "
1657 "%s\nerror: %s\nDebugging will be degraded due to missing "
1658 "types. Rebuilding your project will regenerate the needed "
1659 "module files.",
1660 die.GetOffset(),
1661 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1662 error.AsCString("unknown error"));
1663 }
1664 }
1665 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001666 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001668 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 }
1670}
1671
1672SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001673 if (!m_global_aranges_up) {
1674 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675
1676 ModuleSP module_sp = GetObjectFile()->GetModule();
1677 if (module_sp) {
1678 const size_t num_cus = module_sp->GetNumCompileUnits();
1679 for (size_t i = 0; i < num_cus; ++i) {
1680 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1681 if (cu_sp) {
1682 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1683 if (globals_sp) {
1684 const size_t num_globals = globals_sp->GetSize();
1685 for (size_t g = 0; g < num_globals; ++g) {
1686 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1687 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1688 const DWARFExpression &location = var_sp->LocationExpression();
1689 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001690 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001691 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1692 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 if (location_result.GetValueType() ==
1694 Value::eValueTypeFileAddress) {
1695 lldb::addr_t file_addr =
1696 location_result.GetScalar().ULongLong();
1697 lldb::addr_t byte_size = 1;
1698 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001699 byte_size =
1700 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001701 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 file_addr, byte_size, var_sp.get()));
1703 }
1704 }
1705 }
1706 }
1707 }
1708 }
1709 }
1710 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001711 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001713 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714}
1715
1716uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001717 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001719 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1720 Timer scoped_timer(func_cat,
1721 "SymbolFileDWARF::"
1722 "ResolveSymbolContext (so_addr = { "
1723 "section = %p, offset = 0x%" PRIx64
1724 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 static_cast<void *>(so_addr.GetSection().get()),
1726 so_addr.GetOffset(), resolve_scope);
1727 uint32_t resolved = 0;
1728 if (resolve_scope &
1729 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1730 eSymbolContextLineEntry | eSymbolContextVariable)) {
1731 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1732
1733 DWARFDebugInfo *debug_info = DebugInfo();
1734 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001735 llvm::Expected<DWARFDebugAranges &> aranges =
1736 debug_info->GetCompileUnitAranges();
1737 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001738 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001739 LLDB_LOG_ERROR(log, aranges.takeError(),
1740 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1741 "aranges. {0}");
1742 return 0;
1743 }
1744
1745 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001747 // Global variables are not in the compile unit address ranges. The
1748 // only way to currently find global variables is to iterate over the
1749 // .debug_pubnames or the __apple_names table and find all items in
1750 // there that point to DW_TAG_variable DIEs and then find the address
1751 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 if (resolve_scope & eSymbolContextVariable) {
1753 GlobalVariableMap &map = GetGlobalAranges();
1754 const GlobalVariableMap::Entry *entry =
1755 map.FindEntryThatContains(file_vm_addr);
1756 if (entry && entry->data) {
1757 Variable *variable = entry->data;
1758 SymbolContextScope *scc = variable->GetSymbolContextScope();
1759 if (scc) {
1760 scc->CalculateSymbolContext(&sc);
1761 sc.variable = variable;
1762 }
1763 return sc.GetResolvedMask();
1764 }
1765 }
1766 } else {
1767 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvil60562732019-05-10 17:14:37 +00001768 DWARFUnit *dwarf_cu = debug_info->GetUnitAtOffset(cu_offset, &cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 if (dwarf_cu) {
1770 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1771 if (sc.comp_unit) {
1772 resolved |= eSymbolContextCompUnit;
1773
1774 bool force_check_line_table = false;
1775 if (resolve_scope &
1776 (eSymbolContextFunction | eSymbolContextBlock)) {
1777 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1778 DWARFDIE block_die;
1779 if (function_die) {
1780 sc.function =
1781 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1782 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001783 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784
1785 if (sc.function && (resolve_scope & eSymbolContextBlock))
1786 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1787 } else {
1788 // We might have had a compile unit that had discontiguous
1789 // address ranges where the gaps are symbols that don't have
1790 // any debug info. Discontiguous compile unit address ranges
1791 // should only happen when there aren't other functions from
1792 // other compile units in these gaps. This helps keep the size
1793 // of the aranges down.
1794 force_check_line_table = true;
1795 }
1796
1797 if (sc.function != NULL) {
1798 resolved |= eSymbolContextFunction;
1799
1800 if (resolve_scope & eSymbolContextBlock) {
1801 Block &block = sc.function->GetBlock(true);
1802
1803 if (block_die)
1804 sc.block = block.FindBlockByID(block_die.GetID());
1805 else
1806 sc.block = block.FindBlockByID(function_die.GetID());
1807 if (sc.block)
1808 resolved |= eSymbolContextBlock;
1809 }
1810 }
1811 }
1812
1813 if ((resolve_scope & eSymbolContextLineEntry) ||
1814 force_check_line_table) {
1815 LineTable *line_table = sc.comp_unit->GetLineTable();
1816 if (line_table != NULL) {
1817 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001818 // terms of this debug file if there is no debug map, or it
1819 // will be an address in the .o file which needs to be fixed up
1820 // to be in terms of the debug map executable. Either way,
1821 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822 Address exe_so_addr(so_addr);
1823 if (FixupAddress(exe_so_addr)) {
1824 if (line_table->FindLineEntryByAddress(exe_so_addr,
1825 sc.line_entry)) {
1826 resolved |= eSymbolContextLineEntry;
1827 }
1828 }
1829 }
1830 }
1831
1832 if (force_check_line_table &&
1833 !(resolved & eSymbolContextLineEntry)) {
1834 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001835 // address ranges where the gaps are symbols that don't have any
1836 // debug info. Discontiguous compile unit address ranges should
1837 // only happen when there aren't other functions from other
1838 // compile units in these gaps. This helps keep the size of the
1839 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 sc.comp_unit = NULL;
1841 resolved &= ~eSymbolContextCompUnit;
1842 }
1843 } else {
1844 GetObjectFile()->GetModule()->ReportWarning(
1845 "0x%8.8x: compile unit %u failed to create a valid "
1846 "lldb_private::CompileUnit class.",
1847 cu_offset, cu_idx);
1848 }
1849 }
1850 }
1851 }
1852 }
1853 return resolved;
1854}
1855
1856uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1857 uint32_t line,
1858 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001859 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860 SymbolContextList &sc_list) {
1861 const uint32_t prev_size = sc_list.GetSize();
1862 if (resolve_scope & eSymbolContextCompUnit) {
1863 DWARFDebugInfo *debug_info = DebugInfo();
1864 if (debug_info) {
1865 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001866 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867
Jan Kratochvil60562732019-05-10 17:14:37 +00001868 for (cu_idx = 0; (dwarf_cu = debug_info->GetUnitAtIndex(cu_idx)) != NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 ++cu_idx) {
1870 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1871 const bool full_match = (bool)file_spec.GetDirectory();
1872 bool file_spec_matches_cu_file_spec =
1873 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1874 if (check_inlines || file_spec_matches_cu_file_spec) {
1875 SymbolContext sc(m_obj_file->GetModule());
1876 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1877 if (sc.comp_unit) {
1878 uint32_t file_idx = UINT32_MAX;
1879
Adrian Prantl05097242018-04-30 16:49:04 +00001880 // If we are looking for inline functions only and we don't find it
1881 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001882 if (check_inlines) {
1883 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1884 1, file_spec, true);
1885 if (file_idx == UINT32_MAX)
1886 continue;
1887 }
1888
1889 if (line != 0) {
1890 LineTable *line_table = sc.comp_unit->GetLineTable();
1891
1892 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001893 // We will have already looked up the file index if we are
1894 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895 if (!check_inlines)
1896 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1897 1, file_spec, true);
1898
1899 if (file_idx != UINT32_MAX) {
1900 uint32_t found_line;
1901 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1902 0, file_idx, line, false, &sc.line_entry);
1903 found_line = sc.line_entry.line;
1904
1905 while (line_idx != UINT32_MAX) {
1906 sc.function = NULL;
1907 sc.block = NULL;
1908 if (resolve_scope &
1909 (eSymbolContextFunction | eSymbolContextBlock)) {
1910 const lldb::addr_t file_vm_addr =
1911 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1912 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1913 DWARFDIE function_die =
1914 dwarf_cu->LookupAddress(file_vm_addr);
1915 DWARFDIE block_die;
1916 if (function_die) {
1917 sc.function =
1918 sc.comp_unit
1919 ->FindFunctionByUID(function_die.GetID())
1920 .get();
1921 if (sc.function == NULL)
1922 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001923 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001924
1925 if (sc.function &&
1926 (resolve_scope & eSymbolContextBlock))
1927 block_die =
1928 function_die.LookupDeepestBlock(file_vm_addr);
1929 }
1930
1931 if (sc.function != NULL) {
1932 Block &block = sc.function->GetBlock(true);
1933
1934 if (block_die)
1935 sc.block = block.FindBlockByID(block_die.GetID());
1936 else if (function_die)
1937 sc.block =
1938 block.FindBlockByID(function_die.GetID());
1939 }
1940 }
1941 }
1942
1943 sc_list.Append(sc);
1944 line_idx = line_table->FindLineEntryIndexByFileIndex(
1945 line_idx + 1, file_idx, found_line, true,
1946 &sc.line_entry);
1947 }
1948 }
1949 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1950 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001951 // sites by file and line and if the file spec matches that of
1952 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 sc_list.Append(sc);
1954 }
1955 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1956 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001957 // sites by file and line and if the file spec matches that of
1958 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001959 sc_list.Append(sc);
1960 }
1961
1962 if (!check_inlines)
1963 break;
1964 }
1965 }
1966 }
1967 }
1968 }
1969 return sc_list.GetSize() - prev_size;
1970}
1971
Jim Ingham7fca8c02017-04-28 00:51:06 +00001972void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001973 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001974 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975}
1976
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001977std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1978 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1979 if (module_sp)
1980 return module_sp->GetMutex();
1981 return GetObjectFile()->GetModule()->GetMutex();
1982}
1983
Kate Stoneb9c1b512016-09-06 20:57:50 +00001984bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1985 const lldb_private::CompilerDeclContext *decl_ctx) {
1986 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001987 // Invalid namespace decl which means we aren't matching only things in
1988 // this symbol file, so return true to indicate it matches this symbol
1989 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990 return true;
1991 }
1992
1993 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1994 TypeSystem *type_system = GetTypeSystemForLanguage(
1995 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1996 if (decl_ctx_type_system == type_system)
1997 return true; // The type systems match, return true
1998
1999 // The namespace AST was valid, and it does not match...
2000 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2001
2002 if (log)
2003 GetObjectFile()->GetModule()->LogMessage(
2004 log, "Valid namespace does not match symbol file");
2005
2006 return false;
2007}
2008
2009uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002010 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002011 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2013
2014 if (log)
2015 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002016 log,
2017 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2018 "parent_decl_ctx=%p, max_matches=%u, variables)",
2019 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 max_matches);
2021
2022 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2023 return 0;
2024
2025 DWARFDebugInfo *info = DebugInfo();
2026 if (info == NULL)
2027 return 0;
2028
Pavel Labath34cda142018-05-31 09:46:26 +00002029 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030 const uint32_t original_size = variables.GetSize();
2031
Pavel Labathe1d18752018-06-07 10:04:44 +00002032 llvm::StringRef basename;
2033 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002034 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00002035
2036 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2037 context, basename))
2038 basename = name.GetStringRef();
2039
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002041 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042 const size_t num_die_matches = die_offsets.size();
2043 if (num_die_matches) {
2044 SymbolContext sc;
2045 sc.module_sp = m_obj_file->GetModule();
2046 assert(sc.module_sp);
2047
Pavel Labathe1d18752018-06-07 10:04:44 +00002048 // Loop invariant: Variables up to this index have been checked for context
2049 // matches.
2050 uint32_t pruned_idx = original_size;
2051
Kate Stoneb9c1b512016-09-06 20:57:50 +00002052 bool done = false;
2053 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2054 const DIERef &die_ref = die_offsets[i];
2055 DWARFDIE die = GetDIE(die_ref);
2056
2057 if (die) {
2058 switch (die.Tag()) {
2059 default:
2060 case DW_TAG_subprogram:
2061 case DW_TAG_inlined_subroutine:
2062 case DW_TAG_try_block:
2063 case DW_TAG_catch_block:
2064 break;
2065
2066 case DW_TAG_variable: {
2067 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2068
2069 if (parent_decl_ctx) {
2070 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2071 if (dwarf_ast) {
2072 CompilerDeclContext actual_parent_decl_ctx =
2073 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2074 if (!actual_parent_decl_ctx ||
2075 actual_parent_decl_ctx != *parent_decl_ctx)
2076 continue;
2077 }
2078 }
2079
2080 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2081 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002082 while (pruned_idx < variables.GetSize()) {
2083 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002084 if (name_is_mangled ||
2085 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002086 ++pruned_idx;
2087 else
2088 variables.RemoveVariableAtIndex(pruned_idx);
2089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002090
2091 if (variables.GetSize() - original_size >= max_matches)
2092 done = true;
2093 } break;
2094 }
2095 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002096 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2097 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 }
2099 }
2100 }
2101
2102 // Return the number of variable that were appended to the list
2103 const uint32_t num_matches = variables.GetSize() - original_size;
2104 if (log && num_matches > 0) {
2105 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002106 log,
2107 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2108 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2109 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002110 max_matches, num_matches);
2111 }
2112 return num_matches;
2113}
2114
2115uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002116 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117 VariableList &variables) {
2118 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2119
2120 if (log) {
2121 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002122 log,
2123 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2124 "max_matches=%u, variables)",
2125 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 }
2127
2128 DWARFDebugInfo *info = DebugInfo();
2129 if (info == NULL)
2130 return 0;
2131
Pavel Labath34cda142018-05-31 09:46:26 +00002132 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133 const uint32_t original_size = variables.GetSize();
2134
2135 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002136 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137
2138 SymbolContext sc;
2139 sc.module_sp = m_obj_file->GetModule();
2140 assert(sc.module_sp);
2141
2142 const size_t num_matches = die_offsets.size();
2143 if (num_matches) {
2144 for (size_t i = 0; i < num_matches; ++i) {
2145 const DIERef &die_ref = die_offsets[i];
2146 DWARFDIE die = GetDIE(die_ref);
2147
2148 if (die) {
2149 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2150
2151 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2152
2153 if (variables.GetSize() - original_size >= max_matches)
2154 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002155 } else
2156 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 }
2158 }
2159
2160 // Return the number of variable that were appended to the list
2161 return variables.GetSize() - original_size;
2162}
2163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2165 bool include_inlines,
2166 SymbolContextList &sc_list) {
2167 SymbolContext sc;
2168
2169 if (!orig_die)
2170 return false;
2171
2172 // If we were passed a die that is not a function, just return false...
2173 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2174 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2175 return false;
2176
2177 DWARFDIE die = orig_die;
2178 DWARFDIE inlined_die;
2179 if (die.Tag() == DW_TAG_inlined_subroutine) {
2180 inlined_die = die;
2181
2182 while (1) {
2183 die = die.GetParent();
2184
2185 if (die) {
2186 if (die.Tag() == DW_TAG_subprogram)
2187 break;
2188 } else
2189 break;
2190 }
2191 }
2192 assert(die && die.Tag() == DW_TAG_subprogram);
2193 if (GetFunction(die, sc)) {
2194 Address addr;
2195 // Parse all blocks if needed
2196 if (inlined_die) {
2197 Block &function_block = sc.function->GetBlock(true);
2198 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2199 if (sc.block == NULL)
2200 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00002201 if (sc.block == NULL || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202 addr.Clear();
2203 } else {
2204 sc.block = NULL;
2205 addr = sc.function->GetAddressRange().GetBaseAddress();
2206 }
2207
2208 if (addr.IsValid()) {
2209 sc_list.Append(sc);
2210 return true;
2211 }
2212 }
2213
2214 return false;
2215}
2216
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2218 const DWARFDIE &die) {
2219 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002220 // parent decl context isn't valid, we aren't trying to look for any
2221 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2223 return true;
2224
2225 if (die) {
2226 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2227 if (dwarf_ast) {
2228 CompilerDeclContext actual_decl_ctx =
2229 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2230 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002231 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002232 }
2233 }
2234 return false;
2235}
2236
Zachary Turner117b1fa2018-10-25 20:45:40 +00002237uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002238 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002239 FunctionNameType name_type_mask, bool include_inlines, bool append,
2240 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002241 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2242 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 name.AsCString());
2244
2245 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002246 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2248
2249 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2250
2251 if (log) {
2252 GetObjectFile()->GetModule()->LogMessage(
2253 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2254 "name_type_mask=0x%x, append=%u, sc_list)",
2255 name.GetCString(), name_type_mask, append);
2256 }
2257
2258 // If we aren't appending the results to this list, then clear the list
2259 if (!append)
2260 sc_list.Clear();
2261
2262 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2263 return 0;
2264
2265 // If name is empty then we won't find anything.
2266 if (name.IsEmpty())
2267 return 0;
2268
Adrian Prantl05097242018-04-30 16:49:04 +00002269 // Remember how many sc_list are in the list before we search in case we are
2270 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272 const uint32_t original_size = sc_list.GetSize();
2273
2274 DWARFDebugInfo *info = DebugInfo();
2275 if (info == NULL)
2276 return 0;
2277
Pavel Labath5af11ab2018-06-05 10:33:56 +00002278 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2279 DIEArray offsets;
2280 CompilerDeclContext empty_decl_ctx;
2281 if (!parent_decl_ctx)
2282 parent_decl_ctx = &empty_decl_ctx;
2283
2284 std::vector<DWARFDIE> dies;
2285 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2286 for (const DWARFDIE &die: dies) {
2287 if (resolved_dies.insert(die.GetDIE()).second)
2288 ResolveFunction(die, include_inlines, sc_list);
2289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290
2291 // Return the number of variable that were appended to the list
2292 const uint32_t num_matches = sc_list.GetSize() - original_size;
2293
2294 if (log && num_matches > 0) {
2295 GetObjectFile()->GetModule()->LogMessage(
2296 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2297 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2298 "%u",
2299 name.GetCString(), name_type_mask, include_inlines, append,
2300 num_matches);
2301 }
2302 return num_matches;
2303}
2304
2305uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2306 bool include_inlines, bool append,
2307 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002308 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2309 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002310 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311
2312 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2313
2314 if (log) {
2315 GetObjectFile()->GetModule()->LogMessage(
2316 log,
2317 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002318 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319 }
2320
2321 // If we aren't appending the results to this list, then clear the list
2322 if (!append)
2323 sc_list.Clear();
2324
Pavel Labathb13f0332018-05-21 14:12:52 +00002325 DWARFDebugInfo *info = DebugInfo();
2326 if (!info)
2327 return 0;
2328
Adrian Prantl05097242018-04-30 16:49:04 +00002329 // Remember how many sc_list are in the list before we search in case we are
2330 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002331 uint32_t original_size = sc_list.GetSize();
2332
Pavel Labath6de9c792018-06-05 12:13:22 +00002333 DIEArray offsets;
2334 m_index->GetFunctions(regex, offsets);
2335
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002336 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2337 for (DIERef ref : offsets) {
2338 DWARFDIE die = info->GetDIE(ref);
2339 if (!die) {
2340 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2341 continue;
2342 }
2343 if (resolved_dies.insert(die.GetDIE()).second)
2344 ResolveFunction(die, include_inlines, sc_list);
2345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346
2347 // Return the number of variable that were appended to the list
2348 return sc_list.GetSize() - original_size;
2349}
2350
2351void SymbolFileDWARF::GetMangledNamesForFunction(
2352 const std::string &scope_qualified_name,
2353 std::vector<ConstString> &mangled_names) {
2354 DWARFDebugInfo *info = DebugInfo();
2355 uint32_t num_comp_units = 0;
2356 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002357 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358
2359 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002360 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 if (cu == nullptr)
2362 continue;
2363
2364 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2365 if (dwo)
2366 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2367 }
2368
2369 NameToOffsetMap::iterator iter =
2370 m_function_scope_qualified_name_map.find(scope_qualified_name);
2371 if (iter == m_function_scope_qualified_name_map.end())
2372 return;
2373
2374 DIERefSetSP set_sp = (*iter).second;
2375 std::set<DIERef>::iterator set_iter;
2376 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2377 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2378 mangled_names.push_back(ConstString(die.GetMangledName()));
2379 }
2380}
2381
2382uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002383 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002384 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002385 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2386 TypeMap &types) {
2387 // If we aren't appending the results to this list, then clear the list
2388 if (!append)
2389 types.Clear();
2390
2391 // Make sure we haven't already searched this SymbolFile before...
2392 if (searched_symbol_files.count(this))
2393 return 0;
2394 else
2395 searched_symbol_files.insert(this);
2396
2397 DWARFDebugInfo *info = DebugInfo();
2398 if (info == NULL)
2399 return 0;
2400
2401 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2402
2403 if (log) {
2404 if (parent_decl_ctx)
2405 GetObjectFile()->GetModule()->LogMessage(
2406 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2407 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2408 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2409 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2410 else
2411 GetObjectFile()->GetModule()->LogMessage(
2412 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2413 "NULL, append=%u, max_matches=%u, type_list)",
2414 name.GetCString(), append, max_matches);
2415 }
2416
2417 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2418 return 0;
2419
2420 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002421 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002422 const size_t num_die_matches = die_offsets.size();
2423
2424 if (num_die_matches) {
2425 const uint32_t initial_types_size = types.GetSize();
2426 for (size_t i = 0; i < num_die_matches; ++i) {
2427 const DIERef &die_ref = die_offsets[i];
2428 DWARFDIE die = GetDIE(die_ref);
2429
2430 if (die) {
2431 if (!DIEInDeclContext(parent_decl_ctx, die))
2432 continue; // The containing decl contexts don't match
2433
2434 Type *matching_type = ResolveType(die, true, true);
2435 if (matching_type) {
2436 // We found a type pointer, now find the shared pointer form our type
2437 // list
2438 types.InsertUnique(matching_type->shared_from_this());
2439 if (types.GetSize() >= max_matches)
2440 break;
2441 }
2442 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002443 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2444 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445 }
2446 }
2447 const uint32_t num_matches = types.GetSize() - initial_types_size;
2448 if (log && num_matches) {
2449 if (parent_decl_ctx) {
2450 GetObjectFile()->GetModule()->LogMessage(
2451 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2452 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2453 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2454 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2455 num_matches);
2456 } else {
2457 GetObjectFile()->GetModule()->LogMessage(
2458 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2459 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2460 name.GetCString(), append, max_matches, num_matches);
2461 }
2462 }
2463 return num_matches;
2464 } else {
2465 UpdateExternalModuleListIfNeeded();
2466
2467 for (const auto &pair : m_external_type_modules) {
2468 ModuleSP external_module_sp = pair.second;
2469 if (external_module_sp) {
2470 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2471 if (sym_vendor) {
2472 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002473 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2474 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002475 if (num_external_matches)
2476 return num_external_matches;
2477 }
2478 }
2479 }
2480 }
2481
2482 return 0;
2483}
2484
2485size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2486 bool append, TypeMap &types) {
2487 if (!append)
2488 types.Clear();
2489
2490 if (context.empty())
2491 return 0;
2492
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 ConstString name = context.back().name;
2494
2495 if (!name)
2496 return 0;
2497
Pavel Labathb13f0332018-05-21 14:12:52 +00002498 DIEArray die_offsets;
2499 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002500 const size_t num_die_matches = die_offsets.size();
2501
2502 if (num_die_matches) {
2503 size_t num_matches = 0;
2504 for (size_t i = 0; i < num_die_matches; ++i) {
2505 const DIERef &die_ref = die_offsets[i];
2506 DWARFDIE die = GetDIE(die_ref);
2507
2508 if (die) {
2509 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002510 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002511 if (die_context != context)
2512 continue;
2513
2514 Type *matching_type = ResolveType(die, true, true);
2515 if (matching_type) {
2516 // We found a type pointer, now find the shared pointer form our type
2517 // list
2518 types.InsertUnique(matching_type->shared_from_this());
2519 ++num_matches;
2520 }
2521 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002522 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2523 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002524 }
2525 }
2526 return num_matches;
2527 }
2528 return 0;
2529}
2530
2531CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002532SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002533 const CompilerDeclContext *parent_decl_ctx) {
2534 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2535
2536 if (log) {
2537 GetObjectFile()->GetModule()->LogMessage(
2538 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2539 name.GetCString());
2540 }
2541
2542 CompilerDeclContext namespace_decl_ctx;
2543
2544 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2545 return namespace_decl_ctx;
2546
2547 DWARFDebugInfo *info = DebugInfo();
2548 if (info) {
2549 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002550 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002551 const size_t num_matches = die_offsets.size();
2552 if (num_matches) {
2553 for (size_t i = 0; i < num_matches; ++i) {
2554 const DIERef &die_ref = die_offsets[i];
2555 DWARFDIE die = GetDIE(die_ref);
2556
2557 if (die) {
2558 if (!DIEInDeclContext(parent_decl_ctx, die))
2559 continue; // The containing decl contexts don't match
2560
2561 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2562 if (dwarf_ast) {
2563 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2564 if (namespace_decl_ctx)
2565 break;
2566 }
2567 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002568 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2569 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002570 }
2571 }
2572 }
2573 }
2574 if (log && namespace_decl_ctx) {
2575 GetObjectFile()->GetModule()->LogMessage(
2576 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2577 "CompilerDeclContext(%p/%p) \"%s\"",
2578 name.GetCString(),
2579 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2580 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2581 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2582 }
2583
2584 return namespace_decl_ctx;
2585}
2586
2587TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2588 bool resolve_function_context) {
2589 TypeSP type_sp;
2590 if (die) {
2591 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2592 if (type_ptr == NULL) {
2593 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2594 assert(lldb_cu);
2595 SymbolContext sc(lldb_cu);
2596 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2597 while (parent_die != nullptr) {
2598 if (parent_die->Tag() == DW_TAG_subprogram)
2599 break;
2600 parent_die = parent_die->GetParent();
2601 }
2602 SymbolContext sc_backup = sc;
2603 if (resolve_function_context && parent_die != nullptr &&
2604 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2605 sc = sc_backup;
2606
2607 type_sp = ParseType(sc, die, NULL);
2608 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2609 // Grab the existing type from the master types lists
2610 type_sp = type_ptr->shared_from_this();
2611 }
2612 }
2613 return type_sp;
2614}
2615
2616DWARFDIE
2617SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2618 if (orig_die) {
2619 DWARFDIE die = orig_die;
2620
2621 while (die) {
2622 // If this is the original DIE that we are searching for a declaration
2623 // for, then don't look in the cache as we don't want our own decl
2624 // context to be our decl context...
2625 if (orig_die != die) {
2626 switch (die.Tag()) {
2627 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002628 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002629 case DW_TAG_namespace:
2630 case DW_TAG_structure_type:
2631 case DW_TAG_union_type:
2632 case DW_TAG_class_type:
2633 case DW_TAG_lexical_block:
2634 case DW_TAG_subprogram:
2635 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002636 case DW_TAG_inlined_subroutine: {
2637 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2638 if (abs_die) {
2639 return abs_die;
2640 }
2641 break;
2642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643 default:
2644 break;
2645 }
2646 }
2647
2648 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2649 if (spec_die) {
2650 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2651 if (decl_ctx_die)
2652 return decl_ctx_die;
2653 }
2654
2655 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2656 if (abs_die) {
2657 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2658 if (decl_ctx_die)
2659 return decl_ctx_die;
2660 }
2661
2662 die = die.GetParent();
2663 }
2664 }
2665 return DWARFDIE();
2666}
2667
2668Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002669SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002670 Symbol *objc_class_symbol = NULL;
2671 if (m_obj_file) {
2672 Symtab *symtab = m_obj_file->GetSymtab();
2673 if (symtab) {
2674 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2675 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2676 Symtab::eVisibilityAny);
2677 }
2678 }
2679 return objc_class_symbol;
2680}
2681
2682// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002683// they don't then we can end up looking through all class types for a complete
2684// type and never find the full definition. We need to know if this attribute
2685// is supported, so we determine this here and cache th result. We also need to
2686// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687// DWARF file
2688// if we are doing darwin DWARF in .o file debugging.
2689bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002690 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2692 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2693 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2694 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2695 else {
2696 DWARFDebugInfo *debug_info = DebugInfo();
2697 const uint32_t num_compile_units = GetNumCompileUnits();
2698 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002699 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 if (dwarf_cu != cu &&
2701 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2702 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2703 break;
2704 }
2705 }
2706 }
2707 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2708 GetDebugMapSymfile())
2709 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2710 }
2711 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002712}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002713
2714// This function can be used when a DIE is found that is a forward declaration
2715// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002716TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002717 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002718 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002719
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 TypeSP type_sp;
2721
2722 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2723 return type_sp;
2724
2725 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002726 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002727
2728 const size_t num_matches = die_offsets.size();
2729
2730 if (num_matches) {
2731 for (size_t i = 0; i < num_matches; ++i) {
2732 const DIERef &die_ref = die_offsets[i];
2733 DWARFDIE type_die = GetDIE(die_ref);
2734
2735 if (type_die) {
2736 bool try_resolving_type = false;
2737
Adrian Prantl05097242018-04-30 16:49:04 +00002738 // Don't try and resolve the DIE we are looking for with the DIE
2739 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002740 if (type_die != die) {
2741 switch (type_die.Tag()) {
2742 case DW_TAG_class_type:
2743 case DW_TAG_structure_type:
2744 try_resolving_type = true;
2745 break;
2746 default:
2747 break;
2748 }
2749 }
2750
2751 if (try_resolving_type) {
2752 if (must_be_implementation &&
2753 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2754 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2755 DW_AT_APPLE_objc_complete_type, 0);
2756
2757 if (try_resolving_type) {
2758 Type *resolved_type = ResolveType(type_die, false, true);
2759 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2760 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2761 " (cu 0x%8.8" PRIx64 ")\n",
2762 die.GetID(),
2763 m_obj_file->GetFileSpec().GetFilename().AsCString(
2764 "<Unknown>"),
2765 type_die.GetID(), type_cu->GetID());
2766
2767 if (die)
2768 GetDIEToType()[die.GetDIE()] = resolved_type;
2769 type_sp = resolved_type->shared_from_this();
2770 break;
2771 }
2772 }
2773 }
2774 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002775 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2776 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002777 }
2778 }
2779 }
2780 return type_sp;
2781}
Greg Claytona8022fa2012-04-24 21:22:41 +00002782
Adrian Prantl05097242018-04-30 16:49:04 +00002783// This function helps to ensure that the declaration contexts match for two
2784// different DIEs. Often times debug information will refer to a forward
2785// declaration of a type (the equivalent of "struct my_struct;". There will
2786// often be a declaration of that type elsewhere that has the full definition.
2787// When we go looking for the full type "my_struct", we will find one or more
2788// matches in the accelerator tables and we will then need to make sure the
2789// type was in the same declaration context as the original DIE. This function
2790// can efficiently compare two DIEs and will return true when the declaration
2791// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002792bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2793 const DWARFDIE &die2) {
2794 if (die1 == die2)
2795 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002796
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002797 std::vector<DWARFDIE> decl_ctx_1;
2798 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002799 // The declaration DIE stack is a stack of the declaration context DIEs all
2800 // the way back to the compile unit. If a type "T" is declared inside a class
2801 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2802 // namespace "lldb", and the namespace is in a compile unit, there will be a
2803 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002804 //
2805 // [0] DW_TAG_class_type for "B"
2806 // [1] DW_TAG_class_type for "A"
2807 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002808 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002809 //
Adrian Prantl05097242018-04-30 16:49:04 +00002810 // We grab both contexts and make sure that everything matches all the way
2811 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002812
2813 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002814 decl_ctx_1 = die1.GetDeclContextDIEs();
2815 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002816 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002817 const size_t count1 = decl_ctx_1.size();
2818 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002819 if (count1 != count2)
2820 return false;
2821
Adrian Prantl05097242018-04-30 16:49:04 +00002822 // Make sure the DW_TAG values match all the way back up the compile unit. If
2823 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002824 DWARFDIE decl_ctx_die1;
2825 DWARFDIE decl_ctx_die2;
2826 size_t i;
2827 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002828 decl_ctx_die1 = decl_ctx_1[i];
2829 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002830 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2831 return false;
2832 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002833#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002834
Kate Stoneb9c1b512016-09-06 20:57:50 +00002835 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002836 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2837 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2838 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002839 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002840 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2841 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002842
Greg Clayton890ff562012-02-02 05:48:16 +00002843#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002844 // Always skip the compile unit when comparing by only iterating up to "count
2845 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002846 for (i = 0; i < count1 - 1; 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 const char *name1 = decl_ctx_die1.GetName();
2850 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002851 // If the string was from a DW_FORM_strp, then the pointer will often be
2852 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 if (name1 == name2)
2854 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002855
Adrian Prantl05097242018-04-30 16:49:04 +00002856 // Name pointers are not equal, so only compare the strings if both are not
2857 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 if (name1 && name2) {
2859 // If the strings don't compare, we are done...
2860 if (strcmp(name1, name2) != 0)
2861 return false;
2862 } else {
2863 // One name was NULL while the other wasn't
2864 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002866 }
Adrian Prantl05097242018-04-30 16:49:04 +00002867 // We made it through all of the checks and the declaration contexts are
2868 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002869 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002870}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002871
Kate Stoneb9c1b512016-09-06 20:57:50 +00002872TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2873 const DWARFDeclContext &dwarf_decl_ctx) {
2874 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2877 if (dwarf_decl_ctx_count > 0) {
2878 const ConstString type_name(dwarf_decl_ctx[0].name);
2879 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002880
Kate Stoneb9c1b512016-09-06 20:57:50 +00002881 if (type_name) {
2882 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2883 DWARF_LOG_LOOKUPS));
2884 if (log) {
2885 GetObjectFile()->GetModule()->LogMessage(
2886 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2887 "s, qualified-name='%s')",
2888 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2889 dwarf_decl_ctx.GetQualifiedName());
2890 }
2891
2892 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002893 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002894 const size_t num_matches = die_offsets.size();
2895
Adrian Prantl05097242018-04-30 16:49:04 +00002896 // Get the type system that we are looking to find a type for. We will
2897 // use this to ensure any matches we find are in a language that this
2898 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002899 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2900 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2901 ? nullptr
2902 : GetTypeSystemForLanguage(language);
2903
2904 if (num_matches) {
2905 for (size_t i = 0; i < num_matches; ++i) {
2906 const DIERef &die_ref = die_offsets[i];
2907 DWARFDIE type_die = GetDIE(die_ref);
2908
2909 if (type_die) {
2910 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002911 // looking for. We don't want to find a "Foo" type from Java if we
2912 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002913 if (type_system &&
2914 !type_system->SupportsLanguage(type_die.GetLanguage()))
2915 continue;
2916 bool try_resolving_type = false;
2917
2918 // Don't try and resolve the DIE we are looking for with the DIE
2919 // itself!
2920 const dw_tag_t type_tag = type_die.Tag();
2921 // Make sure the tags match
2922 if (type_tag == tag) {
2923 // The tags match, lets try resolving this type
2924 try_resolving_type = true;
2925 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002926 // The tags don't match, but we need to watch our for a forward
2927 // declaration for a struct and ("struct foo") ends up being a
2928 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002929 switch (type_tag) {
2930 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002931 // We had a "class foo", see if we ended up with a "struct foo
2932 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002933 try_resolving_type = (tag == DW_TAG_structure_type);
2934 break;
2935 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002936 // We had a "struct foo", see if we ended up with a "class foo
2937 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002938 try_resolving_type = (tag == DW_TAG_class_type);
2939 break;
2940 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002941 // Tags don't match, don't event try to resolve using this type
2942 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002943 break;
2944 }
2945 }
2946
2947 if (try_resolving_type) {
2948 DWARFDeclContext type_dwarf_decl_ctx;
2949 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2950
2951 if (log) {
2952 GetObjectFile()->GetModule()->LogMessage(
2953 log, "SymbolFileDWARF::"
2954 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2955 "qualified-name='%s') trying die=0x%8.8x (%s)",
2956 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2957 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2958 type_dwarf_decl_ctx.GetQualifiedName());
2959 }
2960
2961 // Make sure the decl contexts match all the way up
2962 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2963 Type *resolved_type = ResolveType(type_die, false);
2964 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2965 type_sp = resolved_type->shared_from_this();
2966 break;
2967 }
2968 }
2969 } else {
2970 if (log) {
2971 std::string qualified_name;
2972 type_die.GetQualifiedName(qualified_name);
2973 GetObjectFile()->GetModule()->LogMessage(
2974 log, "SymbolFileDWARF::"
2975 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2976 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2977 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2978 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2979 qualified_name.c_str());
2980 }
2981 }
2982 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002983 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2984 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002985 }
2986 }
2987 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002989 }
2990 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002991}
2992
Kate Stoneb9c1b512016-09-06 20:57:50 +00002993TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2994 bool *type_is_new_ptr) {
2995 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002996
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997 if (die) {
2998 TypeSystem *type_system =
2999 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 if (type_system) {
3002 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3003 if (dwarf_ast) {
3004 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3005 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3006 if (type_sp) {
3007 TypeList *type_list = GetTypeList();
3008 if (type_list)
3009 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003010
Kate Stoneb9c1b512016-09-06 20:57:50 +00003011 if (die.Tag() == DW_TAG_subprogram) {
3012 DIERef die_ref = die.GetDIERef();
3013 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3014 .GetScopeQualifiedName()
3015 .AsCString(""));
3016 if (scope_qualified_name.size()) {
3017 NameToOffsetMap::iterator iter =
3018 m_function_scope_qualified_name_map.find(
3019 scope_qualified_name);
3020 if (iter != m_function_scope_qualified_name_map.end())
3021 (*iter).second->insert(die_ref);
3022 else {
3023 DIERefSetSP new_set(new std::set<DIERef>);
3024 new_set->insert(die_ref);
3025 m_function_scope_qualified_name_map.emplace(
3026 std::make_pair(scope_qualified_name, new_set));
3027 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033 }
3034
3035 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003036}
3037
Kate Stoneb9c1b512016-09-06 20:57:50 +00003038size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3039 const DWARFDIE &orig_die,
3040 bool parse_siblings, bool parse_children) {
3041 size_t types_added = 0;
3042 DWARFDIE die = orig_die;
3043 while (die) {
3044 bool type_is_new = false;
3045 if (ParseType(sc, die, &type_is_new).get()) {
3046 if (type_is_new)
3047 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003048 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003049
3050 if (parse_children && die.HasChildren()) {
3051 if (die.Tag() == DW_TAG_subprogram) {
3052 SymbolContext child_sc(sc);
3053 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3054 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3055 } else
3056 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3057 }
3058
3059 if (parse_siblings)
3060 die = die.GetSibling();
3061 else
3062 die.Clear();
3063 }
3064 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003065}
3066
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003067size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003068 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003069 CompileUnit *comp_unit = func.GetCompileUnit();
3070 lldbassert(comp_unit);
3071
3072 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3073 if (!dwarf_cu)
3074 return 0;
3075
Kate Stoneb9c1b512016-09-06 20:57:50 +00003076 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003077 const dw_offset_t function_die_offset = func.GetID();
3078 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3079 if (function_die) {
3080 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3081 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003083
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003085}
3086
Zachary Turner863f8c12019-01-11 18:03:20 +00003087size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003088 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003089 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003090 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003091 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003092 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3093 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3094 SymbolContext sc;
3095 sc.comp_unit = &comp_unit;
3096 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003097 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003098 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003099
Kate Stoneb9c1b512016-09-06 20:57:50 +00003100 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003101}
3102
Kate Stoneb9c1b512016-09-06 20:57:50 +00003103size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003104 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003105 if (sc.comp_unit != NULL) {
3106 DWARFDebugInfo *info = DebugInfo();
3107 if (info == NULL)
3108 return 0;
3109
3110 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003111 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003112
3113 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3114 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3115 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3116 const size_t num_variables = ParseVariables(
3117 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3118
3119 // Let all blocks know they have parse all their variables
3120 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3121 return num_variables;
3122 }
3123 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003124 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125
3126 if (dwarf_cu == NULL)
3127 return 0;
3128
3129 uint32_t vars_added = 0;
3130 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3131
3132 if (variables.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003133 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003134 sc.comp_unit->SetVariableList(variables);
3135
3136 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003137 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003138 const size_t num_matches = die_offsets.size();
3139 if (num_matches) {
3140 for (size_t i = 0; i < num_matches; ++i) {
3141 const DIERef &die_ref = die_offsets[i];
3142 DWARFDIE die = GetDIE(die_ref);
3143 if (die) {
3144 VariableSP var_sp(
3145 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3146 if (var_sp) {
3147 variables->AddVariableIfUnique(var_sp);
3148 ++vars_added;
3149 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003150 } else
3151 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003154 }
3155 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 }
3158 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003159}
3160
Kate Stoneb9c1b512016-09-06 20:57:50 +00003161VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3162 const DWARFDIE &die,
3163 const lldb::addr_t func_low_pc) {
3164 if (die.GetDWARF() != this)
3165 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003166
Kate Stoneb9c1b512016-09-06 20:57:50 +00003167 VariableSP var_sp;
3168 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003169 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003170
Kate Stoneb9c1b512016-09-06 20:57:50 +00003171 var_sp = GetDIEToVariable()[die.GetDIE()];
3172 if (var_sp)
3173 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003174
Kate Stoneb9c1b512016-09-06 20:57:50 +00003175 const dw_tag_t tag = die.Tag();
3176 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003177
Kate Stoneb9c1b512016-09-06 20:57:50 +00003178 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3179 (tag == DW_TAG_formal_parameter && sc.function)) {
3180 DWARFAttributes attributes;
3181 const size_t num_attributes = die.GetAttributes(attributes);
3182 DWARFDIE spec_die;
3183 if (num_attributes > 0) {
3184 const char *name = NULL;
3185 const char *mangled = NULL;
3186 Declaration decl;
3187 uint32_t i;
3188 DWARFFormValue type_die_form;
3189 DWARFExpression location(die.GetCU());
3190 bool is_external = false;
3191 bool is_artificial = false;
3192 bool location_is_const_value_data = false;
3193 bool has_explicit_location = false;
3194 DWARFFormValue const_value;
3195 Variable::RangeList scope_ranges;
3196 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003197
Kate Stoneb9c1b512016-09-06 20:57:50 +00003198 for (i = 0; i < num_attributes; ++i) {
3199 dw_attr_t attr = attributes.AttributeAtIndex(i);
3200 DWARFFormValue form_value;
3201
3202 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3203 switch (attr) {
3204 case DW_AT_decl_file:
3205 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3206 form_value.Unsigned()));
3207 break;
3208 case DW_AT_decl_line:
3209 decl.SetLine(form_value.Unsigned());
3210 break;
3211 case DW_AT_decl_column:
3212 decl.SetColumn(form_value.Unsigned());
3213 break;
3214 case DW_AT_name:
3215 name = form_value.AsCString();
3216 break;
3217 case DW_AT_linkage_name:
3218 case DW_AT_MIPS_linkage_name:
3219 mangled = form_value.AsCString();
3220 break;
3221 case DW_AT_type:
3222 type_die_form = form_value;
3223 break;
3224 case DW_AT_external:
3225 is_external = form_value.Boolean();
3226 break;
3227 case DW_AT_const_value:
3228 // If we have already found a DW_AT_location attribute, ignore this
3229 // attribute.
3230 if (!has_explicit_location) {
3231 location_is_const_value_data = true;
3232 // The constant value will be either a block, a data value or a
3233 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003234 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003235 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3236 // Retrieve the value as a block expression.
3237 uint32_t block_offset =
3238 form_value.BlockData() - debug_info_data.GetDataStart();
3239 uint32_t block_length = form_value.Unsigned();
3240 location.CopyOpcodeData(module, debug_info_data, block_offset,
3241 block_length);
3242 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3243 // Retrieve the value as a data expression.
3244 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3245 DWARFFormValue::GetFixedFormSizesForAddressSize(
Zachary Turner7e44a842019-03-12 20:51:05 +00003246 attributes.CompileUnitAtIndex(i)->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3248 uint32_t data_length =
3249 fixed_form_sizes.GetSize(form_value.Form());
3250 if (data_length == 0) {
3251 const uint8_t *data_pointer = form_value.BlockData();
3252 if (data_pointer) {
3253 form_value.Unsigned();
3254 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3255 // we need to get the byte size of the type later after we
3256 // create the variable
3257 const_value = form_value;
3258 }
3259 } else
3260 location.CopyOpcodeData(module, debug_info_data, data_offset,
3261 data_length);
3262 } else {
3263 // Retrieve the value as a string expression.
3264 if (form_value.Form() == DW_FORM_strp) {
3265 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3266 DWARFFormValue::GetFixedFormSizesForAddressSize(
3267 attributes.CompileUnitAtIndex(i)
Zachary Turner7e44a842019-03-12 20:51:05 +00003268 ->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003269 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3270 uint32_t data_length =
3271 fixed_form_sizes.GetSize(form_value.Form());
3272 location.CopyOpcodeData(module, debug_info_data, data_offset,
3273 data_length);
3274 } else {
3275 const char *str = form_value.AsCString();
3276 uint32_t string_offset =
3277 str - (const char *)debug_info_data.GetDataStart();
3278 uint32_t string_length = strlen(str) + 1;
3279 location.CopyOpcodeData(module, debug_info_data,
3280 string_offset, string_length);
3281 }
3282 }
3283 }
3284 break;
3285 case DW_AT_location: {
3286 location_is_const_value_data = false;
3287 has_explicit_location = true;
3288 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003289 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290
3291 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003292 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003294 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003295 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003296 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003297 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3298
3299 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003300 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003301 if (loc_list_length > 0) {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003302 location.CopyOpcodeData(module, debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003303 debug_loc_offset, loc_list_length);
3304 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3305 location.SetLocationListSlide(
3306 func_low_pc -
3307 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3308 }
3309 }
3310 } break;
3311 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003312 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003313 break;
3314 case DW_AT_start_scope: {
3315 if (form_value.Form() == DW_FORM_sec_offset) {
3316 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003317 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3318 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003319 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003320 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003321 } else {
3322 // TODO: Handle the case when DW_AT_start_scope have form
3323 // constant. The
3324 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003325 // behavior in case the enclosing block have a non coninious
3326 // address range and the DW_AT_start_scope entry have a form
3327 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003328 GetObjectFile()->GetModule()->ReportWarning(
3329 "0x%8.8" PRIx64
3330 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3331 die.GetID(), form_value.Form());
3332 }
3333
3334 scope_ranges.Sort();
3335 scope_ranges.CombineConsecutiveRanges();
3336 } break;
3337 case DW_AT_artificial:
3338 is_artificial = form_value.Boolean();
3339 break;
3340 case DW_AT_accessibility:
3341 break; // accessibility =
3342 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3343 case DW_AT_declaration:
3344 case DW_AT_description:
3345 case DW_AT_endianity:
3346 case DW_AT_segment:
3347 case DW_AT_visibility:
3348 default:
3349 case DW_AT_abstract_origin:
3350 case DW_AT_sibling:
3351 break;
3352 }
3353 }
3354 }
3355
3356 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3357 const dw_tag_t parent_tag = die.GetParent().Tag();
3358 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003359 (parent_tag == DW_TAG_compile_unit ||
3360 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003361 (parent_context_die.Tag() == DW_TAG_class_type ||
3362 parent_context_die.Tag() == DW_TAG_structure_type);
3363
3364 ValueType scope = eValueTypeInvalid;
3365
3366 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3367 SymbolContextScope *symbol_context_scope = NULL;
3368
Sam McCall30621392016-11-22 11:40:25 +00003369 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003370 if (!mangled) {
3371 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003372 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3373 // of global variables with commands like "frame var j". For
3374 // example, if j were an int variable holding a value 4 and
3375 // declared in a namespace B which in turn is contained in a
3376 // namespace A, the command "frame var j" returns
3377 // "(int) A::B::j = 4".
3378 // If the compiler does not emit a linkage name, we should be
3379 // able to generate a fully qualified name from the
3380 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003381 if ((parent_tag == DW_TAG_compile_unit ||
3382 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003383 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3384 DWARFDeclContext decl_ctx;
3385
3386 die.GetDWARFDeclContext(decl_ctx);
3387 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3388 }
3389 }
3390
Kate Stoneb9c1b512016-09-06 20:57:50 +00003391 if (tag == DW_TAG_formal_parameter)
3392 scope = eValueTypeVariableArgument;
3393 else {
Sam McCall30621392016-11-22 11:40:25 +00003394 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3395 // or static variable, so we have to do a little digging:
3396 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3397 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3398 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3399 // Clang likes to combine small global variables into the same symbol
3400 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3401 // so we need to look through the whole expression.
3402 bool is_static_lifetime =
3403 has_explicit_mangled ||
3404 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003405 // Check if the location has a DW_OP_addr with any address value...
3406 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3407 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003408 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003409 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3410 if (op_error) {
3411 StreamString strm;
3412 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3413 NULL);
3414 GetObjectFile()->GetModule()->ReportError(
3415 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003416 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003417 }
Sam McCall30621392016-11-22 11:40:25 +00003418 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3419 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003420 }
3421 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003422 if (debug_map_symfile)
3423 // Set the module of the expression to the linked module
3424 // instead of the oject file so the relocated address can be
3425 // found there.
3426 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003427
Sam McCall30621392016-11-22 11:40:25 +00003428 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003429 if (is_external)
3430 scope = eValueTypeVariableGlobal;
3431 else
3432 scope = eValueTypeVariableStatic;
3433
3434 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003435 // When leaving the DWARF in the .o files on darwin, when we have a
3436 // global variable that wasn't initialized, the .o file might not
3437 // have allocated a virtual address for the global variable. In
3438 // this case it will have created a symbol for the global variable
3439 // that is undefined/data and external and the value will be the
3440 // byte size of the variable. When we do the address map in
3441 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3442 // do some magic here so we can get the correct address for our
3443 // global variable. The address for all of these entries will be
3444 // zero, and there will be an undefined symbol in this object file,
3445 // and the executable will have a matching symbol with a good
3446 // address. So here we dig up the correct address and replace it in
3447 // the location for the variable, and set the variable's symbol
3448 // context scope to be that of the main executable so the file
3449 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003450 bool linked_oso_file_addr = false;
3451 if (is_external && location_DW_OP_addr == 0) {
3452 // we have a possible uninitialized extern global
3453 ConstString const_name(mangled ? mangled : name);
3454 ObjectFile *debug_map_objfile =
3455 debug_map_symfile->GetObjectFile();
3456 if (debug_map_objfile) {
3457 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3458 if (debug_map_symtab) {
3459 Symbol *exe_symbol =
3460 debug_map_symtab->FindFirstSymbolWithNameAndType(
3461 const_name, eSymbolTypeData, Symtab::eDebugYes,
3462 Symtab::eVisibilityExtern);
3463 if (exe_symbol) {
3464 if (exe_symbol->ValueIsAddress()) {
3465 const addr_t exe_file_addr =
3466 exe_symbol->GetAddressRef().GetFileAddress();
3467 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3468 if (location.Update_DW_OP_addr(exe_file_addr)) {
3469 linked_oso_file_addr = true;
3470 symbol_context_scope = exe_symbol;
3471 }
3472 }
3473 }
3474 }
3475 }
3476 }
3477 }
3478
3479 if (!linked_oso_file_addr) {
3480 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003481 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003482 const lldb::addr_t exe_file_addr =
3483 debug_map_symfile->LinkOSOFileAddress(this,
3484 location_DW_OP_addr);
3485 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3486 // Update the file address for this variable
3487 location.Update_DW_OP_addr(exe_file_addr);
3488 } else {
3489 // Variable didn't make it into the final executable
3490 return var_sp;
3491 }
3492 }
3493 }
3494 } else {
3495 if (location_is_const_value_data)
3496 scope = eValueTypeVariableStatic;
3497 else {
3498 scope = eValueTypeVariableLocal;
3499 if (debug_map_symfile) {
3500 // We need to check for TLS addresses that we need to fixup
3501 if (location.ContainsThreadLocalStorage()) {
3502 location.LinkThreadLocalStorage(
3503 debug_map_symfile->GetObjectFile()->GetModule(),
3504 [this, debug_map_symfile](
3505 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3506 return debug_map_symfile->LinkOSOFileAddress(
3507 this, unlinked_file_addr);
3508 });
3509 scope = eValueTypeVariableThreadLocal;
3510 }
3511 }
3512 }
3513 }
3514 }
3515
3516 if (symbol_context_scope == NULL) {
3517 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003518 case DW_TAG_subprogram:
3519 case DW_TAG_inlined_subroutine:
3520 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003521 if (sc.function) {
3522 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3523 sc_parent_die.GetID());
3524 if (symbol_context_scope == NULL)
3525 symbol_context_scope = sc.function;
3526 }
3527 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003528
Kate Stoneb9c1b512016-09-06 20:57:50 +00003529 default:
3530 symbol_context_scope = sc.comp_unit;
3531 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003533 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003534
Kate Stoneb9c1b512016-09-06 20:57:50 +00003535 if (symbol_context_scope) {
3536 SymbolFileTypeSP type_sp(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003537 new SymbolFileType(*this, GetUID(DIERef(type_die_form))));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003538
3539 if (const_value.Form() && type_sp && type_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00003540 location.CopyOpcodeData(
3541 const_value.Unsigned(),
3542 type_sp->GetType()->GetByteSize().getValueOr(0),
3543 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003544
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003545 var_sp = std::make_shared<Variable>(
3546 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3547 scope_ranges, &decl, location, is_external, is_artificial,
3548 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003549
3550 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3551 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003552 // Not ready to parse this variable yet. It might be a global or static
3553 // variable that is in a function scope and the function in the symbol
3554 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003555 return var_sp;
3556 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003557 }
Adrian Prantl05097242018-04-30 16:49:04 +00003558 // Cache var_sp even if NULL (the variable was just a specification or was
3559 // missing vital information to be able to be displayed in the debugger
3560 // (missing location due to optimization, etc)) so we don't re-parse this
3561 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003562 GetDIEToVariable()[die.GetDIE()] = var_sp;
3563 if (spec_die)
3564 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3565 }
3566 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003567}
3568
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569DWARFDIE
3570SymbolFileDWARF::FindBlockContainingSpecification(
3571 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3572 // Give the concrete function die specified by "func_die_offset", find the
3573 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3574 // to "spec_block_die_offset"
3575 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3576 spec_block_die_offset);
3577}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003578
Kate Stoneb9c1b512016-09-06 20:57:50 +00003579DWARFDIE
3580SymbolFileDWARF::FindBlockContainingSpecification(
3581 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3582 if (die) {
3583 switch (die.Tag()) {
3584 case DW_TAG_subprogram:
3585 case DW_TAG_inlined_subroutine:
3586 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003587 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3588 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003589 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003590
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003591 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003592 spec_block_die_offset)
3593 return die;
3594 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003596
3597 // Give the concrete function die specified by "func_die_offset", find the
3598 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3599 // to "spec_block_die_offset"
3600 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3601 child_die = child_die.GetSibling()) {
3602 DWARFDIE result_die =
3603 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3604 if (result_die)
3605 return result_die;
3606 }
3607 }
3608
3609 return DWARFDIE();
3610}
3611
3612size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3613 const DWARFDIE &orig_die,
3614 const lldb::addr_t func_low_pc,
3615 bool parse_siblings, bool parse_children,
3616 VariableList *cc_variable_list) {
3617 if (!orig_die)
3618 return 0;
3619
3620 VariableListSP variable_list_sp;
3621
3622 size_t vars_added = 0;
3623 DWARFDIE die = orig_die;
3624 while (die) {
3625 dw_tag_t tag = die.Tag();
3626
3627 // Check to see if we have already parsed this variable or constant?
3628 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3629 if (var_sp) {
3630 if (cc_variable_list)
3631 cc_variable_list->AddVariableIfUnique(var_sp);
3632 } else {
3633 // We haven't already parsed it, lets do that now.
3634 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3635 (tag == DW_TAG_formal_parameter && sc.function)) {
3636 if (variable_list_sp.get() == NULL) {
3637 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3638 dw_tag_t parent_tag = sc_parent_die.Tag();
3639 switch (parent_tag) {
3640 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003641 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642 if (sc.comp_unit != NULL) {
3643 variable_list_sp = sc.comp_unit->GetVariableList(false);
3644 if (variable_list_sp.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003645 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003646 }
3647 } else {
3648 GetObjectFile()->GetModule()->ReportError(
3649 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3650 "symbol context for 0x%8.8" PRIx64
3651 " %s.\n",
3652 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3653 orig_die.GetID(), orig_die.GetTagAsCString());
3654 }
3655 break;
3656
3657 case DW_TAG_subprogram:
3658 case DW_TAG_inlined_subroutine:
3659 case DW_TAG_lexical_block:
3660 if (sc.function != NULL) {
3661 // Check to see if we already have parsed the variables for the
3662 // given scope
3663
3664 Block *block = sc.function->GetBlock(true).FindBlockByID(
3665 sc_parent_die.GetID());
3666 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003667 // This must be a specification or abstract origin with a
3668 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003669 // to find the concrete block so we can correctly add the
3670 // variable to it
3671 const DWARFDIE concrete_block_die =
3672 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003673 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003674 sc_parent_die.GetOffset());
3675 if (concrete_block_die)
3676 block = sc.function->GetBlock(true).FindBlockByID(
3677 concrete_block_die.GetID());
3678 }
3679
3680 if (block != NULL) {
3681 const bool can_create = false;
3682 variable_list_sp = block->GetBlockVariableList(can_create);
3683 if (variable_list_sp.get() == NULL) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003684 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003685 block->SetVariableList(variable_list_sp);
3686 }
3687 }
3688 }
3689 break;
3690
3691 default:
3692 GetObjectFile()->GetModule()->ReportError(
3693 "didn't find appropriate parent DIE for variable list for "
3694 "0x%8.8" PRIx64 " %s.\n",
3695 orig_die.GetID(), orig_die.GetTagAsCString());
3696 break;
3697 }
3698 }
3699
3700 if (variable_list_sp) {
3701 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3702 if (var_sp) {
3703 variable_list_sp->AddVariableIfUnique(var_sp);
3704 if (cc_variable_list)
3705 cc_variable_list->AddVariableIfUnique(var_sp);
3706 ++vars_added;
3707 }
3708 }
3709 }
3710 }
3711
3712 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3713
3714 if (!skip_children && parse_children && die.HasChildren()) {
3715 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3716 true, cc_variable_list);
3717 }
3718
3719 if (parse_siblings)
3720 die = die.GetSibling();
3721 else
3722 die.Clear();
3723 }
3724 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003725}
3726
Vedant Kumar4b36f792018-10-05 23:23:15 +00003727/// Collect call graph edges present in a function DIE.
3728static std::vector<lldb_private::CallEdge>
3729CollectCallEdges(DWARFDIE function_die) {
3730 // Check if the function has a supported call site-related attribute.
3731 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3732 uint64_t has_call_edges =
3733 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3734 if (!has_call_edges)
3735 return {};
3736
3737 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3738 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3739 function_die.GetPubname());
3740
3741 // Scan the DIE for TAG_call_site entries.
3742 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3743 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3744 // For now, assume that all entries are nested directly under the subprogram
3745 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3746 std::vector<CallEdge> call_edges;
3747 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3748 child = child.GetSibling()) {
3749 if (child.Tag() != DW_TAG_call_site)
3750 continue;
3751
3752 // Extract DW_AT_call_origin (the call target's DIE).
3753 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3754 if (!call_origin.IsValid()) {
3755 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3756 function_die.GetPubname());
3757 continue;
3758 }
3759
3760 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3761 // available. It should only ever be unavailable for tail call edges, in
3762 // which case use LLDB_INVALID_ADDRESS.
3763 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3764 LLDB_INVALID_ADDRESS);
3765
3766 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3767 call_origin.GetPubname(), return_pc);
3768 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3769 }
3770 return call_edges;
3771}
3772
3773std::vector<lldb_private::CallEdge>
3774SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003775 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003776 if (func_die.IsValid())
3777 return CollectCallEdges(func_die);
3778 return {};
3779}
3780
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003781// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003782ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3783
3784uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3785
Pavel Labath9337b412018-06-06 11:35:23 +00003786void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003787
Zachary Turner49110232018-11-05 17:40:28 +00003788void SymbolFileDWARF::DumpClangAST(Stream &s) {
3789 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3790 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3791 if (!clang)
3792 return;
3793 clang->Dump(s);
3794}
3795
Kate Stoneb9c1b512016-09-06 20:57:50 +00003796SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3797 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3798 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3799 if (module_sp) {
3800 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3801 if (sym_vendor)
3802 m_debug_map_symfile =
3803 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003804 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003805 }
3806 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003807}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003808
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003809DWARFExpression::LocationListFormat
3810SymbolFileDWARF::GetLocationListFormat() const {
3811 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003812 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003813 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003814}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003815
3816SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3817 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003818 ModuleSpec module_spec;
3819 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3820 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003821 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003822
3823 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3824 FileSpec dwp_filespec =
3825 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003826 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003827 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3828 dwp_filespec);
3829 }
3830 });
3831 return m_dwp_symfile.get();
3832}