blob: c692e8bf18d0877869b4fe1ff7248dfc73e3c304 [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) {
Pavel Labathff9b4262019-05-17 08:26:58 +0000203 return new SymbolFileDWARF(obj_file,
204 /*dwo_section_list*/ nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000208 // This method can be called without going through the symbol vendor so we
209 // need to lock the module.
210 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
212 if (debug_map_symfile)
213 return debug_map_symfile->GetTypeList();
214 else
215 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000216}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
218 dw_offset_t max_die_offset, uint32_t type_mask,
219 TypeSet &type_set) {
220 if (die) {
221 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 if (die_offset >= max_die_offset)
224 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 if (die_offset >= min_die_offset) {
227 const dw_tag_t tag = die.Tag();
228
229 bool add_type = false;
230
231 switch (tag) {
232 case DW_TAG_array_type:
233 add_type = (type_mask & eTypeClassArray) != 0;
234 break;
235 case DW_TAG_unspecified_type:
236 case DW_TAG_base_type:
237 add_type = (type_mask & eTypeClassBuiltin) != 0;
238 break;
239 case DW_TAG_class_type:
240 add_type = (type_mask & eTypeClassClass) != 0;
241 break;
242 case DW_TAG_structure_type:
243 add_type = (type_mask & eTypeClassStruct) != 0;
244 break;
245 case DW_TAG_union_type:
246 add_type = (type_mask & eTypeClassUnion) != 0;
247 break;
248 case DW_TAG_enumeration_type:
249 add_type = (type_mask & eTypeClassEnumeration) != 0;
250 break;
251 case DW_TAG_subroutine_type:
252 case DW_TAG_subprogram:
253 case DW_TAG_inlined_subroutine:
254 add_type = (type_mask & eTypeClassFunction) != 0;
255 break;
256 case DW_TAG_pointer_type:
257 add_type = (type_mask & eTypeClassPointer) != 0;
258 break;
259 case DW_TAG_rvalue_reference_type:
260 case DW_TAG_reference_type:
261 add_type = (type_mask & eTypeClassReference) != 0;
262 break;
263 case DW_TAG_typedef:
264 add_type = (type_mask & eTypeClassTypedef) != 0;
265 break;
266 case DW_TAG_ptr_to_member_type:
267 add_type = (type_mask & eTypeClassMemberPointer) != 0;
268 break;
269 }
270
271 if (add_type) {
272 const bool assert_not_being_parsed = true;
273 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
274 if (type) {
275 if (type_set.find(type) == type_set.end())
276 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000277 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280
281 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
282 child_die = child_die.GetSibling()) {
283 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
284 }
285 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000286}
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000289 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000290
291{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000292 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000294
Konrad Kleine248a1302019-05-23 11:14:47 +0000295 CompileUnit *comp_unit = nullptr;
296 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 if (sc_scope)
298 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 if (comp_unit) {
301 dwarf_cu = GetDWARFCompileUnit(comp_unit);
Konrad Kleine248a1302019-05-23 11:14:47 +0000302 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 return 0;
304 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000305 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 } else {
307 DWARFDebugInfo *info = DebugInfo();
308 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000309 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000311 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 if (dwarf_cu) {
313 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 }
318
319 std::set<CompilerType> compiler_type_set;
320 size_t num_types_added = 0;
321 for (Type *type : type_set) {
322 CompilerType compiler_type = type->GetForwardCompilerType();
323 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
324 compiler_type_set.insert(compiler_type);
325 type_list.Insert(type->shared_from_this());
326 ++num_types_added;
327 }
328 }
329 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000330}
331
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332// Gets the first parent that is a lexical block, function or inlined
333// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000334DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
336 DWARFDIE die;
337 for (die = child_die.GetParent(); die; die = die.GetParent()) {
338 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 switch (tag) {
341 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000342 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 case DW_TAG_subprogram:
344 case DW_TAG_inlined_subroutine:
345 case DW_TAG_lexical_block:
346 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 }
349 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Pavel Labathff9b4262019-05-17 08:26:58 +0000352SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile,
353 SectionList *dwo_section_list)
Pavel Labathf4014e12019-05-16 11:07:58 +0000354 : SymbolFile(objfile),
355 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
356 // when this class parses .o files to
357 // contain the .o file index/ID
Konrad Kleine248a1302019-05-23 11:14:47 +0000358 m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
Pavel Labathff9b4262019-05-17 08:26:58 +0000359 m_context(objfile->GetModule()->GetSectionList(), dwo_section_list),
Pavel Labath0261b942019-05-20 08:38:47 +0000360 m_data_debug_loc(), m_data_debug_ranges(), m_data_debug_rnglists(),
Pavel Labathff9b4262019-05-17 08:26:58 +0000361 m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
363 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365SymbolFileDWARF::~SymbolFileDWARF() {}
366
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000367static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 static ConstString g_dwarf_section_name("__DWARF");
369 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370}
371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
373 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
374 if (debug_map_symfile)
375 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
376 else
377 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378}
379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
381 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
382 TypeSystem *type_system;
383 if (debug_map_symfile) {
384 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
385 } else {
386 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
387 if (type_system)
388 type_system->SetSymbolFile(this);
389 }
390 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000391}
392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000394 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395
Pavel Labath9337b412018-06-06 11:35:23 +0000396 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
397 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
398 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
399 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
400 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
401 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402
Pavel Labath9337b412018-06-06 11:35:23 +0000403 m_index = AppleDWARFIndex::Create(
404 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
Pavel Labath0261b942019-05-20 08:38:47 +0000405 apple_types, apple_objc, m_context.getOrLoadStrData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406
Pavel Labath9337b412018-06-06 11:35:23 +0000407 if (m_index)
408 return;
409
410 DWARFDataExtractor debug_names;
411 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
412 if (debug_names.GetByteSize() > 0) {
413 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
Pavel Labath0261b942019-05-20 08:38:47 +0000414 DebugNamesDWARFIndex::Create(
415 *GetObjectFile()->GetModule(), debug_names,
416 m_context.getOrLoadStrData(), DebugInfo());
Pavel Labath9337b412018-06-06 11:35:23 +0000417 if (index_or) {
418 m_index = std::move(*index_or);
419 return;
420 }
421 LLDB_LOG_ERROR(log, index_or.takeError(),
422 "Unable to read .debug_names data: {0}");
423 }
424 }
425
426 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
427 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428}
429
430bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000431 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432}
433
434uint32_t SymbolFileDWARF::CalculateAbilities() {
435 uint32_t abilities = 0;
Konrad Kleine248a1302019-05-23 11:14:47 +0000436 if (m_obj_file != nullptr) {
437 const Section *section = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 const SectionList *section_list = m_obj_file->GetSectionList();
Konrad Kleine248a1302019-05-23 11:14:47 +0000439 if (section_list == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 return 0;
441
442 uint64_t debug_abbrev_file_size = 0;
443 uint64_t debug_info_file_size = 0;
444 uint64_t debug_line_file_size = 0;
445
446 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
447
448 if (section)
449 section_list = &section->GetChildren();
450
451 section =
452 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
Konrad Kleine248a1302019-05-23 11:14:47 +0000453 if (section != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 debug_info_file_size = section->GetFileSize();
455
456 section =
457 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
458 .get();
459 if (section)
460 debug_abbrev_file_size = section->GetFileSize();
461
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000462 DWARFDebugAbbrev *abbrev = DebugAbbrev();
463 if (abbrev) {
464 std::set<dw_form_t> invalid_forms;
465 abbrev->GetUnsupportedForms(invalid_forms);
466 if (!invalid_forms.empty()) {
467 StreamString error;
468 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
469 for (auto form : invalid_forms)
470 error.Printf(" %#x", form);
471 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
472 return 0;
473 }
474 }
475
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 section =
477 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
478 .get();
479 if (section)
480 debug_line_file_size = section->GetFileSize();
481 } else {
482 const char *symfile_dir_cstr =
483 m_obj_file->GetFileSpec().GetDirectory().GetCString();
484 if (symfile_dir_cstr) {
485 if (strcasestr(symfile_dir_cstr, ".dsym")) {
486 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000487 // We have a dSYM file that didn't have a any debug info. If the
488 // string table has a size of 1, then it was made from an
489 // executable with no debug info, or from an executable that was
490 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 section =
492 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
493 .get();
494 if (section && section->GetFileSize() == 1) {
495 m_obj_file->GetModule()->ReportWarning(
496 "empty dSYM file detected, dSYM was created with an "
497 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503
504 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
505 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
506 LocalVariables | VariableTypes;
507
508 if (debug_line_file_size > 0)
509 abilities |= LineTables;
510 }
511 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512}
513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514const DWARFDataExtractor &
515SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
516 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000517 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
518 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
519 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521}
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
524 DWARFDataExtractor &data) {
525 ModuleSP module_sp(m_obj_file->GetModule());
526 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000527 if (!section_list)
528 return;
529
530 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
531 if (!section_sp)
532 return;
533
534 data.Clear();
535 m_obj_file->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000538const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
539 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
540 if (debugLocData.GetByteSize() > 0)
541 return debugLocData;
542 return get_debug_loclists_data();
543}
544
545const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
546 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
547}
548
549const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
550 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
551 m_data_debug_loclists);
552}
553
554const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
555 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
556 m_data_debug_ranges);
557}
558
559const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
560 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
561 m_data_debug_rnglists);
562}
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000565 if (m_abbr)
566 return m_abbr.get();
567
Pavel Labath0261b942019-05-20 08:38:47 +0000568 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000569 if (debug_abbrev_data.GetByteSize() == 0)
570 return nullptr;
571
572 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
573 llvm::Error error = abbr->parse(debug_abbrev_data);
574 if (error) {
575 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
576 LLDB_LOG_ERROR(log, std::move(error),
577 "Unable to read .debug_abbrev section: {0}");
578 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000580
581 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
586 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000590 if (m_info == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000591 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
592 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
593 static_cast<void *>(this));
Pavel Labathff9b4262019-05-17 08:26:58 +0000594 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0) {
Zachary Turner6e665122019-03-20 20:49:25 +0000595 m_info = llvm::make_unique<DWARFDebugInfo>(m_context);
596 m_info->SetDwarfData(this);
Greg Clayton1f746072012-08-29 21:13:06 +0000597 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 }
599 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
603 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604}
605
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000606DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
608 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000609 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610
611 DWARFDebugInfo *info = DebugInfo();
612 if (info) {
Pavel Labath2a0cfcc2019-05-06 07:45:28 +0000613 // The compile unit ID is the index of the DWARF unit.
Jan Kratochvil60562732019-05-10 17:14:37 +0000614 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +0000615 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 dwarf_cu->SetUserData(comp_unit);
617 return dwarf_cu;
618 }
Konrad Kleine248a1302019-05-23 11:14:47 +0000619 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}
621
George Rimar7e7f9c12018-10-25 10:25:45 +0000622DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000623 if (m_ranges == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000624 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
625 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
626 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000627
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000628 if (get_debug_ranges_data().GetByteSize() > 0)
629 m_ranges.reset(new DWARFDebugRanges());
630 else if (get_debug_rnglists_data().GetByteSize() > 0)
631 m_ranges.reset(new DWARFDebugRngLists());
George Rimar6e357122018-10-10 08:11:15 +0000632
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000633 if (m_ranges)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000634 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 }
636 return m_ranges.get();
637}
638
George Rimar7e7f9c12018-10-25 10:25:45 +0000639const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 return m_ranges.get();
641}
642
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000643lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 uint32_t cu_idx) {
645 CompUnitSP cu_sp;
646 if (dwarf_cu) {
647 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
648 if (comp_unit) {
649 // We already parsed this compile unit, had out a shared pointer to it
650 cu_sp = comp_unit->shared_from_this();
651 } else {
652 if (dwarf_cu->GetSymbolFileDWARF() != this) {
653 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
654 cu_idx);
655 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
656 // Let the debug map create the compile unit
657 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
658 dwarf_cu->SetUserData(cu_sp.get());
659 } else {
660 ModuleSP module_sp(m_obj_file->GetModule());
661 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000662 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (cu_die) {
Pavel Labath7d36d722019-01-16 12:30:41 +0000664 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 if (cu_file_spec) {
666 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000667 // resolve the file. This can be expensive e.g. when the source
Pavel Labath7d36d722019-01-16 12:30:41 +0000668 // files are NFS mounted.
669 cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670
671 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000672 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000674 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 }
676
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000677 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
679
680 bool is_optimized = dwarf_cu->GetIsOptimized();
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000681 cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000683 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 if (cu_sp) {
685 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000686 // try and get the first entry in the supports files from the
687 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 if (!cu_file_spec) {
689 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
690 if (cu_file_spec) {
691 (FileSpec &)(*cu_sp) = cu_file_spec;
692 // Also fix the invalid file spec which was copied from the
693 // compile unit.
694 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
695 }
696 }
697
698 dwarf_cu->SetUserData(cu_sp.get());
699
700 // Figure out the compile unit index if we weren't given one
701 if (cu_idx == UINT32_MAX)
Pavel Labath2a0cfcc2019-05-06 07:45:28 +0000702 cu_idx = dwarf_cu->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703
704 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
705 cu_idx, cu_sp);
706 }
707 }
708 }
709 }
710 }
711 }
712 return cu_sp;
713}
714
715uint32_t SymbolFileDWARF::GetNumCompileUnits() {
716 DWARFDebugInfo *info = DebugInfo();
717 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +0000718 return info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 return 0;
720}
721
722CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000723 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 CompUnitSP cu_sp;
725 DWARFDebugInfo *info = DebugInfo();
726 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000727 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 if (dwarf_cu)
729 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
730 }
731 return cu_sp;
732}
733
Zachary Turner863f8c12019-01-11 18:03:20 +0000734Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
735 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000736 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 if (die.IsValid()) {
738 TypeSystem *type_system =
739 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
740
741 if (type_system) {
742 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
743 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000744 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 }
746 }
747 return nullptr;
748}
749
750bool SymbolFileDWARF::FixupAddress(Address &addr) {
751 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
752 if (debug_map_symfile) {
753 return debug_map_symfile->LinkOSOAddress(addr);
754 }
755 // This is a normal DWARF file, no address fixups need to happen
756 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000757}
Zachary Turner863f8c12019-01-11 18:03:20 +0000758lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000759 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000760 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 if (dwarf_cu)
762 return dwarf_cu->GetLanguageType();
763 else
764 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000765}
766
Zachary Turner863f8c12019-01-11 18:03:20 +0000767size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000768 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000769 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000770 if (!dwarf_cu)
771 return 0;
772
773 size_t functions_added = 0;
774 std::vector<DWARFDIE> function_dies;
775 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
776 for (const DWARFDIE &die : function_dies) {
777 if (comp_unit.FindFunctionByUID(die.GetID()))
778 continue;
779 if (ParseFunction(comp_unit, die))
780 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000782 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Zachary Turner863f8c12019-01-11 18:03:20 +0000786bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
787 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000788 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000789 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000790 if (dwarf_cu) {
791 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000793 if (cu_die) {
794 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
795 DW_AT_stmt_list, DW_INVALID_OFFSET);
796 if (stmt_list != DW_INVALID_OFFSET) {
797 // All file indexes in DWARF are one based and a file of index zero is
798 // supposed to be the compile unit itself.
799 support_files.Append(comp_unit);
800 return DWARFDebugLine::ParseSupportFiles(
Pavel Labath0261b942019-05-20 08:38:47 +0000801 comp_unit.GetModule(), m_context.getOrLoadLineData(), stmt_list,
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000802 support_files, dwarf_cu);
803 }
804 }
805 }
806 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807}
808
Zachary Turner863f8c12019-01-11 18:03:20 +0000809bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000810 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000811 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 if (dwarf_cu)
813 return dwarf_cu->GetIsOptimized();
814 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000815}
816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817bool SymbolFileDWARF::ParseImportedModules(
818 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000819 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000820 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000822 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000823 if (!dwarf_cu)
824 return false;
825 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
826 sc.comp_unit->GetLanguage()))
827 return false;
828 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000830 const DWARFDIE die = dwarf_cu->DIE();
831 if (!die)
832 return false;
833
834 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
835 child_die = child_die.GetSibling()) {
836 if (child_die.Tag() != DW_TAG_imported_declaration)
837 continue;
838
839 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
840 if (module_die.Tag() != DW_TAG_module)
841 continue;
842
843 if (const char *name =
844 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
845 SourceModule module;
846 module.path.push_back(ConstString(name));
847
848 DWARFDIE parent_die = module_die;
849 while ((parent_die = parent_die.GetParent())) {
850 if (parent_die.Tag() != DW_TAG_module)
851 break;
852 if (const char *name =
853 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
854 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000856 std::reverse(module.path.begin(), module.path.end());
857 if (const char *include_path = module_die.GetAttributeValueAsString(
858 DW_AT_LLVM_include_path, nullptr))
859 module.search_path = ConstString(include_path);
860 if (const char *sysroot = module_die.GetAttributeValueAsString(
861 DW_AT_LLVM_isysroot, nullptr))
862 module.sysroot = ConstString(sysroot);
863 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000866 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000867}
868
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869struct ParseDWARFLineTableCallbackInfo {
870 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000871 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873};
874
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876static void ParseDWARFLineTableCallback(dw_offset_t offset,
877 const DWARFDebugLine::State &state,
878 void *userData) {
879 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
880 // Just started parsing the line table
881 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
882 // Done parsing line table, nothing to do for the cleanup
883 } else {
884 ParseDWARFLineTableCallbackInfo *info =
885 (ParseDWARFLineTableCallbackInfo *)userData;
886 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887
Adrian Prantl05097242018-04-30 16:49:04 +0000888 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 if (!info->sequence_up) {
890 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
891 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000894 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 state.column, state.file, state.is_stmt, state.basic_block,
896 state.prologue_end, state.epilogue_begin, state.end_sequence);
897 if (state.end_sequence) {
898 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000899 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000901 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 }
903 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904}
905
Zachary Turner863f8c12019-01-11 18:03:20 +0000906bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000907 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +0000908 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910
Zachary Turner863f8c12019-01-11 18:03:20 +0000911 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000912 if (dwarf_cu) {
913 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
914 if (dwarf_cu_die) {
915 const dw_offset_t cu_line_offset =
916 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
917 DW_INVALID_OFFSET);
918 if (cu_line_offset != DW_INVALID_OFFSET) {
919 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
920 if (line_table_up) {
921 ParseDWARFLineTableCallbackInfo info;
922 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000923
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000924 /*
925 * MIPS:
926 * The SymbolContext may not have a valid target, thus we may not be
927 * able
928 * to call Address::GetOpcodeLoadAddress() which would clear the bit
929 * #0
930 * for MIPS. Use ArchSpec to clear the bit #0.
931 */
932 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
933 case llvm::Triple::mips:
934 case llvm::Triple::mipsel:
935 case llvm::Triple::mips64:
936 case llvm::Triple::mips64el:
937 info.addr_mask = ~((lldb::addr_t)1);
938 break;
939 default:
940 info.addr_mask = ~((lldb::addr_t)0);
941 break;
942 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000943
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000944 lldb::offset_t offset = cu_line_offset;
Pavel Labath0261b942019-05-20 08:38:47 +0000945 DWARFDebugLine::ParseStatementTable(
946 m_context.getOrLoadLineData(), &offset,
947 ParseDWARFLineTableCallback, &info, dwarf_cu);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000948 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
949 if (debug_map_symfile) {
950 // We have an object file that has a line table with addresses that
951 // are not linked. We need to link the line table and convert the
952 // addresses that are relative to the .o file into addresses for
953 // the main executable.
954 comp_unit.SetLineTable(
955 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
956 } else {
957 comp_unit.SetLineTable(line_table_up.release());
958 return true;
959 }
960 }
961 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 }
964 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965}
966
Siva Chandrad8335e92015-12-16 00:22:08 +0000967lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
969 auto iter = m_debug_macros_map.find(*offset);
970 if (iter != m_debug_macros_map.end())
971 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +0000972
Pavel Labath0261b942019-05-20 08:38:47 +0000973 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000974 if (debug_macro_data.GetByteSize() == 0)
975 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +0000976
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
978 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +0000979
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000981 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +0000982 DWARFDebugMacroEntry::ReadMacroEntries(
983 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
984 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +0000985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +0000987}
988
Zachary Turner863f8c12019-01-11 18:03:20 +0000989bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000990 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +0000991
Zachary Turner863f8c12019-01-11 18:03:20 +0000992 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +0000994 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995
Jan Kratochvil93afb052018-05-24 20:51:13 +0000996 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 if (!dwarf_cu_die)
998 return false;
999
1000 lldb::offset_t sect_offset =
1001 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1002 if (sect_offset == DW_INVALID_OFFSET)
1003 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1004 DW_INVALID_OFFSET);
1005 if (sect_offset == DW_INVALID_OFFSET)
1006 return false;
1007
Zachary Turner863f8c12019-01-11 18:03:20 +00001008 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009
1010 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001011}
1012
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001013size_t SymbolFileDWARF::ParseBlocksRecursive(
1014 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1015 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 size_t blocks_added = 0;
1017 DWARFDIE die = orig_die;
1018 while (die) {
1019 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 switch (tag) {
1022 case DW_TAG_inlined_subroutine:
1023 case DW_TAG_subprogram:
1024 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001025 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001027 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1028 // inlined functions. These will be parsed on their own as separate
1029 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030
1031 if (depth > 0)
1032 break;
1033
1034 block = parent_block;
1035 } else {
1036 BlockSP block_sp(new Block(die.GetID()));
1037 parent_block->AddChild(block_sp);
1038 block = block_sp.get();
1039 }
1040 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001041 const char *name = nullptr;
1042 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043
1044 int decl_file = 0;
1045 int decl_line = 0;
1046 int decl_column = 0;
1047 int call_file = 0;
1048 int call_line = 0;
1049 int call_column = 0;
1050 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1051 decl_line, decl_column, call_file, call_line,
1052 call_column, nullptr)) {
1053 if (tag == DW_TAG_subprogram) {
1054 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1055 subprogram_low_pc = ranges.GetMinRangeBase(0);
1056 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001057 // We get called here for inlined subroutines in two ways. The first
1058 // time is when we are making the Function object for this inlined
1059 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001061 // need to adjust the containing address. The second time is when we
1062 // are parsing the blocks inside the function that contains the
1063 // inlined concrete instance. Since these will be blocks inside the
1064 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1066 subprogram_low_pc = ranges.GetMinRangeBase(0);
1067 }
1068 }
1069
1070 const size_t num_ranges = ranges.GetSize();
1071 for (size_t i = 0; i < num_ranges; ++i) {
1072 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1073 const addr_t range_base = range.GetRangeBase();
1074 if (range_base >= subprogram_low_pc)
1075 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1076 range.GetByteSize()));
1077 else {
1078 GetObjectFile()->GetModule()->ReportError(
1079 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1080 ") which has a base that is less than the function's low PC "
1081 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1082 "start of this error message",
1083 block->GetID(), range_base, range.GetRangeEnd(),
1084 subprogram_low_pc);
1085 }
1086 }
1087 block->FinalizeRanges();
1088
1089 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001090 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001091 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001093 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001094 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 decl_line, decl_column));
1096
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001097 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001099 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001100 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 call_line, call_column));
1102
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001103 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1104 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 }
1106
1107 ++blocks_added;
1108
1109 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001110 blocks_added +=
1111 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1112 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113 }
1114 }
1115 } break;
1116 default:
1117 break;
1118 }
1119
Adrian Prantl05097242018-04-30 16:49:04 +00001120 // Only parse siblings of the block if we are not at depth zero. A depth of
1121 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1122 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123
1124 if (depth == 0)
1125 die.Clear();
1126 else
1127 die = die.GetSibling();
1128 }
1129 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001130}
1131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1133 if (parent_die) {
1134 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1135 die = die.GetSibling()) {
1136 dw_tag_t tag = die.Tag();
1137 bool check_virtuality = false;
1138 switch (tag) {
1139 case DW_TAG_inheritance:
1140 case DW_TAG_subprogram:
1141 check_virtuality = true;
1142 break;
1143 default:
1144 break;
1145 }
1146 if (check_virtuality) {
1147 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1148 return true;
1149 }
1150 }
1151 }
1152 return false;
1153}
1154
1155void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1156 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1157 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1158 std::vector<DWARFDIE> decl_ctx_die_list =
1159 ast_parser->GetDIEForDeclContext(decl_ctx);
1160
1161 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1162 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1163 decl = decl.GetSibling())
1164 ast_parser->GetDeclForUIDFromDWARF(decl);
1165}
1166
Pavel Labath2841e6e2019-05-08 11:43:05 +00001167SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001168 // This method can be called without going through the symbol vendor so we
1169 // need to lock the module.
1170 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001171 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1172 // must make sure we use the correct DWARF file when resolving things. On
1173 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1174 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1175 // references to other DWARF objects and we must be ready to receive a
1176 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001178 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1179 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labathf4014e12019-05-16 11:07:58 +00001181 return {dwarf, {DIERef::Section::DebugInfo, DW_INVALID_OFFSET, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001182 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001183 DIERef::Section section =
1184 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1185 uint32_t dwarf_id = uid >> 32 & 0x7fffffff;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001186 dw_offset_t die_offset = uid;
1187
1188 if (die_offset == DW_INVALID_OFFSET)
1189 return {nullptr, DIERef()};
1190
1191 SymbolFileDWARF *dwarf = this;
1192 if (DebugInfo()) {
1193 if (DWARFUnit *unit = DebugInfo()->GetUnitAtIndex(dwarf_id)) {
1194 if (unit->GetDwoSymbolFile())
1195 dwarf = unit->GetDwoSymbolFile();
1196 }
1197 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001198 return {dwarf, {section, DW_INVALID_OFFSET, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001199}
1200
1201DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001202SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001203 // This method can be called without going through the symbol vendor so we
1204 // need to lock the module.
1205 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001206
1207 DecodedUID decoded = DecodeUID(uid);
1208
1209 if (decoded.dwarf)
1210 return decoded.dwarf->GetDIE(decoded.ref);
1211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001213}
1214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001216 // This method can be called without going through the symbol vendor so we
1217 // need to lock the module.
1218 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001219 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001220 // SymbolFileDWARF::GetDIE(). See comments inside the
1221 // SymbolFileDWARF::GetDIE() for details.
1222 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 return die.GetDecl();
1224 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001225}
1226
Greg Clayton99558cc42015-08-24 23:46:31 +00001227CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001229 // This method can be called without going through the symbol vendor so we
1230 // need to lock the module.
1231 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001232 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001233 // SymbolFileDWARF::GetDIE(). See comments inside the
1234 // SymbolFileDWARF::GetDIE() for details.
1235 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 return die.GetDeclContext();
1237 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001238}
1239
Greg Clayton99558cc42015-08-24 23:46:31 +00001240CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001242 // This method can be called without going through the symbol vendor so we
1243 // need to lock the module.
1244 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001245 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001246 // SymbolFileDWARF::GetDIE(). See comments inside the
1247 // SymbolFileDWARF::GetDIE() for details.
1248 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 return die.GetContainingDeclContext();
1250 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251}
1252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001254 // This method can be called without going through the symbol vendor so we
1255 // need to lock the module.
1256 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001257 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001258 // SymbolFileDWARF::GetDIE(). See comments inside the
1259 // SymbolFileDWARF::GetDIE() for details.
1260 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 return type_die.ResolveType();
1262 else
1263 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264}
1265
Adrian Prantleca07c52018-11-05 20:49:07 +00001266llvm::Optional<SymbolFile::ArrayInfo>
1267SymbolFileDWARF::GetDynamicArrayInfoForUID(
1268 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1269 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001270 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001271 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1272 else
1273 return llvm::None;
1274}
1275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1277 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001278}
1279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1281 bool assert_not_being_parsed) {
1282 if (die) {
1283 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1284 if (log)
1285 GetObjectFile()->GetModule()->LogMessage(
1286 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1287 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001288
Adrian Prantl05097242018-04-30 16:49:04 +00001289 // We might be coming in in the middle of a type tree (a class within a
1290 // class, an enum within a class), so parse any needed parent DIEs before
1291 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1293 if (decl_ctx_die) {
1294 if (log) {
1295 switch (decl_ctx_die.Tag()) {
1296 case DW_TAG_structure_type:
1297 case DW_TAG_union_type:
1298 case DW_TAG_class_type: {
1299 // Get the type, which could be a forward declaration
1300 if (log)
1301 GetObjectFile()->GetModule()->LogMessage(
1302 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1303 "resolve parent forward type for 0x%8.8x",
1304 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1305 decl_ctx_die.GetOffset());
1306 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 default:
1309 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001312 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 return ResolveType(die);
1314 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001315 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001316}
1317
Greg Clayton6beaaa62011-01-17 03:46:26 +00001318// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001319// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1320// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321bool SymbolFileDWARF::HasForwardDeclForClangType(
1322 const CompilerType &compiler_type) {
1323 CompilerType compiler_type_no_qualifiers =
1324 ClangUtil::RemoveFastQualifiers(compiler_type);
1325 if (GetForwardDeclClangTypeToDie().count(
1326 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1327 return true;
1328 }
1329 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001330
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 ClangASTContext *clang_type_system =
1332 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1333 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001334 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 DWARFASTParserClang *ast_parser =
1336 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1337 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001338}
1339
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001341 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001342
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 ClangASTContext *clang_type_system =
1344 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1345 if (clang_type_system) {
1346 DWARFASTParserClang *ast_parser =
1347 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1348 if (ast_parser &&
1349 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1350 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1351 }
Jim Inghamc3549282012-01-11 02:21:12 +00001352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 // We have a struct/union/class/enum that needs to be fully resolved.
1354 CompilerType compiler_type_no_qualifiers =
1355 ClangUtil::RemoveFastQualifiers(compiler_type);
1356 auto die_it = GetForwardDeclClangTypeToDie().find(
1357 compiler_type_no_qualifiers.GetOpaqueQualType());
1358 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1359 // We have already resolved this type...
1360 return true;
1361 }
1362
1363 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1364 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001365 // Once we start resolving this type, remove it from the forward
1366 // declaration map in case anyone child members or other types require this
1367 // type to get resolved. The type will get resolved when all of the calls
1368 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 GetForwardDeclClangTypeToDie().erase(die_it);
1370
1371 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1372
1373 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1374 DWARF_LOG_TYPE_COMPLETION));
1375 if (log)
1376 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1377 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1378 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1379 type->GetName().AsCString());
1380 assert(compiler_type);
1381 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1382 if (dwarf_ast)
1383 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1384 }
1385 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001386}
1387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1389 bool assert_not_being_parsed,
1390 bool resolve_function_context) {
1391 if (die) {
1392 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1393
1394 if (assert_not_being_parsed) {
1395 if (type != DIE_IS_BEING_PARSED)
1396 return type;
1397
1398 GetObjectFile()->GetModule()->ReportError(
1399 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1400 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1401
1402 } else
1403 return type;
1404 }
1405 return nullptr;
1406}
1407
1408CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001409SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410 uint32_t cu_idx) {
1411 // Check if the symbol vendor already knows about this compile unit?
Konrad Kleine248a1302019-05-23 11:14:47 +00001412 if (dwarf_cu->GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001413 // The symbol vendor doesn't know about this compile unit, we need to parse
1414 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1416 }
1417 return (CompileUnit *)dwarf_cu->GetUserData();
1418}
1419
1420size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1421 DIEArray &method_die_offsets) {
1422 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001423 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 return method_die_offsets.size();
1425}
1426
1427bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1428 sc.Clear(false);
1429
1430 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1433
1434 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001435 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001436 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437
1438 if (sc.function) {
1439 sc.module_sp = sc.function->CalculateSymbolContextModule();
1440 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 }
1443
1444 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445}
1446
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1448 UpdateExternalModuleListIfNeeded();
1449 const auto &pos = m_external_type_modules.find(name);
1450 if (pos != m_external_type_modules.end())
1451 return pos->second;
1452 else
1453 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001454}
1455
Greg Clayton2f869fe2016-03-30 20:14:35 +00001456DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1458 DWARFDebugInfo *debug_info = DebugInfo();
1459 if (debug_info)
1460 return debug_info->GetDIE(die_ref);
1461 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001462 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001463}
1464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465std::unique_ptr<SymbolFileDWARFDwo>
1466SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001467 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001469 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 // info files for modules.
1471 if (GetDebugMapSymfile())
1472 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001473
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001474 const char *dwo_name =
1475 cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476 if (!dwo_name)
1477 return nullptr;
1478
Tamas Berghammer963ce482017-08-25 13:56:14 +00001479 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1480 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001481 uint64_t dwo_id =
1482 cu_die.GetAttributeValueAsUnsigned(&dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001483 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1484 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1485 if (dwo_symfile)
1486 return dwo_symfile;
1487 }
1488
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001489 FileSpec dwo_file(dwo_name);
1490 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001492 const char *comp_dir =
1493 cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 if (!comp_dir)
1495 return nullptr;
1496
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001497 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1498 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 dwo_file.AppendPathComponent(dwo_name);
1500 }
1501
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001502 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 return nullptr;
1504
1505 const lldb::offset_t file_offset = 0;
1506 DataBufferSP dwo_file_data_sp;
1507 lldb::offset_t dwo_file_data_offset = 0;
1508 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1509 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001510 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1511 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 if (dwo_obj_file == nullptr)
1513 return nullptr;
1514
1515 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001516}
1517
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1519 if (m_fetched_external_modules)
1520 return;
1521 m_fetched_external_modules = true;
1522
1523 DWARFDebugInfo *debug_info = DebugInfo();
1524
1525 const uint32_t num_compile_units = GetNumCompileUnits();
1526 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001527 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528
Jan Kratochvil93afb052018-05-24 20:51:13 +00001529 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001530 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1532
1533 if (name) {
1534 ConstString const_name(name);
1535 if (m_external_type_modules.find(const_name) ==
1536 m_external_type_modules.end()) {
1537 ModuleSP module_sp;
1538 const char *dwo_path =
1539 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1540 if (dwo_path) {
1541 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001542 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001543 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1545 const char *comp_dir =
1546 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1547 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001548 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001549 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001550 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1552 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001553 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 dwo_module_spec.GetArchitecture() =
1555 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001556
Adrian Prantl05097242018-04-30 16:49:04 +00001557 // When LLDB loads "external" modules it looks at the presence of
1558 // DW_AT_GNU_dwo_name. However, when the already created module
1559 // (corresponding to .dwo itself) is being processed, it will see
1560 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1561 // dwo file) and will try to call ModuleList::GetSharedModule
1562 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1563 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1564 // DW_AT_comp_dir. In this case the method
1565 // ModuleList::GetSharedModule will fail and the warning will be
1566 // printed. However, as one can notice in this case we don't
1567 // actually need to try to load the already loaded module
1568 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001569 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001570 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1571 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1572 continue;
1573 }
1574
Zachary Turner97206d52017-05-12 04:51:55 +00001575 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001576 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 if (!module_sp) {
1578 GetObjectFile()->GetModule()->ReportWarning(
1579 "0x%8.8x: unable to locate module needed for external types: "
1580 "%s\nerror: %s\nDebugging will be degraded due to missing "
1581 "types. Rebuilding your project will regenerate the needed "
1582 "module files.",
1583 die.GetOffset(),
1584 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1585 error.AsCString("unknown error"));
1586 }
1587 }
1588 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001589 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 }
1593}
1594
1595SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001596 if (!m_global_aranges_up) {
1597 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598
1599 ModuleSP module_sp = GetObjectFile()->GetModule();
1600 if (module_sp) {
1601 const size_t num_cus = module_sp->GetNumCompileUnits();
1602 for (size_t i = 0; i < num_cus; ++i) {
1603 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1604 if (cu_sp) {
1605 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1606 if (globals_sp) {
1607 const size_t num_globals = globals_sp->GetSize();
1608 for (size_t g = 0; g < num_globals; ++g) {
1609 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1610 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1611 const DWARFExpression &location = var_sp->LocationExpression();
1612 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001613 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001614 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1615 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 if (location_result.GetValueType() ==
1617 Value::eValueTypeFileAddress) {
1618 lldb::addr_t file_addr =
1619 location_result.GetScalar().ULongLong();
1620 lldb::addr_t byte_size = 1;
1621 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001622 byte_size =
1623 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001624 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 file_addr, byte_size, var_sp.get()));
1626 }
1627 }
1628 }
1629 }
1630 }
1631 }
1632 }
1633 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001634 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001636 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637}
1638
1639uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001640 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001642 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1643 Timer scoped_timer(func_cat,
1644 "SymbolFileDWARF::"
1645 "ResolveSymbolContext (so_addr = { "
1646 "section = %p, offset = 0x%" PRIx64
1647 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 static_cast<void *>(so_addr.GetSection().get()),
1649 so_addr.GetOffset(), resolve_scope);
1650 uint32_t resolved = 0;
1651 if (resolve_scope &
1652 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1653 eSymbolContextLineEntry | eSymbolContextVariable)) {
1654 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1655
1656 DWARFDebugInfo *debug_info = DebugInfo();
1657 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001658 llvm::Expected<DWARFDebugAranges &> aranges =
1659 debug_info->GetCompileUnitAranges();
1660 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001661 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001662 LLDB_LOG_ERROR(log, aranges.takeError(),
1663 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1664 "aranges. {0}");
1665 return 0;
1666 }
1667
1668 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001670 // Global variables are not in the compile unit address ranges. The
1671 // only way to currently find global variables is to iterate over the
1672 // .debug_pubnames or the __apple_names table and find all items in
1673 // there that point to DW_TAG_variable DIEs and then find the address
1674 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 if (resolve_scope & eSymbolContextVariable) {
1676 GlobalVariableMap &map = GetGlobalAranges();
1677 const GlobalVariableMap::Entry *entry =
1678 map.FindEntryThatContains(file_vm_addr);
1679 if (entry && entry->data) {
1680 Variable *variable = entry->data;
1681 SymbolContextScope *scc = variable->GetSymbolContextScope();
1682 if (scc) {
1683 scc->CalculateSymbolContext(&sc);
1684 sc.variable = variable;
1685 }
1686 return sc.GetResolvedMask();
1687 }
1688 }
1689 } else {
1690 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathf4014e12019-05-16 11:07:58 +00001691 DWARFUnit *dwarf_cu = debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1692 cu_offset, &cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 if (dwarf_cu) {
1694 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1695 if (sc.comp_unit) {
1696 resolved |= eSymbolContextCompUnit;
1697
1698 bool force_check_line_table = false;
1699 if (resolve_scope &
1700 (eSymbolContextFunction | eSymbolContextBlock)) {
1701 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1702 DWARFDIE block_die;
1703 if (function_die) {
1704 sc.function =
1705 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001706 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001707 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708
1709 if (sc.function && (resolve_scope & eSymbolContextBlock))
1710 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1711 } else {
1712 // We might have had a compile unit that had discontiguous
1713 // address ranges where the gaps are symbols that don't have
1714 // any debug info. Discontiguous compile unit address ranges
1715 // should only happen when there aren't other functions from
1716 // other compile units in these gaps. This helps keep the size
1717 // of the aranges down.
1718 force_check_line_table = true;
1719 }
1720
Konrad Kleine248a1302019-05-23 11:14:47 +00001721 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 resolved |= eSymbolContextFunction;
1723
1724 if (resolve_scope & eSymbolContextBlock) {
1725 Block &block = sc.function->GetBlock(true);
1726
1727 if (block_die)
1728 sc.block = block.FindBlockByID(block_die.GetID());
1729 else
1730 sc.block = block.FindBlockByID(function_die.GetID());
1731 if (sc.block)
1732 resolved |= eSymbolContextBlock;
1733 }
1734 }
1735 }
1736
1737 if ((resolve_scope & eSymbolContextLineEntry) ||
1738 force_check_line_table) {
1739 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001740 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001742 // terms of this debug file if there is no debug map, or it
1743 // will be an address in the .o file which needs to be fixed up
1744 // to be in terms of the debug map executable. Either way,
1745 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 Address exe_so_addr(so_addr);
1747 if (FixupAddress(exe_so_addr)) {
1748 if (line_table->FindLineEntryByAddress(exe_so_addr,
1749 sc.line_entry)) {
1750 resolved |= eSymbolContextLineEntry;
1751 }
1752 }
1753 }
1754 }
1755
1756 if (force_check_line_table &&
1757 !(resolved & eSymbolContextLineEntry)) {
1758 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001759 // address ranges where the gaps are symbols that don't have any
1760 // debug info. Discontiguous compile unit address ranges should
1761 // only happen when there aren't other functions from other
1762 // compile units in these gaps. This helps keep the size of the
1763 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001764 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765 resolved &= ~eSymbolContextCompUnit;
1766 }
1767 } else {
1768 GetObjectFile()->GetModule()->ReportWarning(
1769 "0x%8.8x: compile unit %u failed to create a valid "
1770 "lldb_private::CompileUnit class.",
1771 cu_offset, cu_idx);
1772 }
1773 }
1774 }
1775 }
1776 }
1777 return resolved;
1778}
1779
1780uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1781 uint32_t line,
1782 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001783 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784 SymbolContextList &sc_list) {
1785 const uint32_t prev_size = sc_list.GetSize();
1786 if (resolve_scope & eSymbolContextCompUnit) {
1787 DWARFDebugInfo *debug_info = DebugInfo();
1788 if (debug_info) {
1789 uint32_t cu_idx;
Konrad Kleine248a1302019-05-23 11:14:47 +00001790 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791
Konrad Kleine248a1302019-05-23 11:14:47 +00001792 for (cu_idx = 0;
1793 (dwarf_cu = debug_info->GetUnitAtIndex(cu_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 ++cu_idx) {
1795 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1796 const bool full_match = (bool)file_spec.GetDirectory();
1797 bool file_spec_matches_cu_file_spec =
Konrad Kleine248a1302019-05-23 11:14:47 +00001798 dc_cu != nullptr && FileSpec::Equal(file_spec, *dc_cu, full_match);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 if (check_inlines || file_spec_matches_cu_file_spec) {
1800 SymbolContext sc(m_obj_file->GetModule());
1801 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1802 if (sc.comp_unit) {
1803 uint32_t file_idx = UINT32_MAX;
1804
Adrian Prantl05097242018-04-30 16:49:04 +00001805 // If we are looking for inline functions only and we don't find it
1806 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 if (check_inlines) {
1808 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1809 1, file_spec, true);
1810 if (file_idx == UINT32_MAX)
1811 continue;
1812 }
1813
1814 if (line != 0) {
1815 LineTable *line_table = sc.comp_unit->GetLineTable();
1816
Konrad Kleine248a1302019-05-23 11:14:47 +00001817 if (line_table != nullptr && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001818 // We will have already looked up the file index if we are
1819 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 if (!check_inlines)
1821 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1822 1, file_spec, true);
1823
1824 if (file_idx != UINT32_MAX) {
1825 uint32_t found_line;
1826 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1827 0, file_idx, line, false, &sc.line_entry);
1828 found_line = sc.line_entry.line;
1829
1830 while (line_idx != UINT32_MAX) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001831 sc.function = nullptr;
1832 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 if (resolve_scope &
1834 (eSymbolContextFunction | eSymbolContextBlock)) {
1835 const lldb::addr_t file_vm_addr =
1836 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1837 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1838 DWARFDIE function_die =
1839 dwarf_cu->LookupAddress(file_vm_addr);
1840 DWARFDIE block_die;
1841 if (function_die) {
1842 sc.function =
1843 sc.comp_unit
1844 ->FindFunctionByUID(function_die.GetID())
1845 .get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001846 if (sc.function == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001847 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001848 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001849
1850 if (sc.function &&
1851 (resolve_scope & eSymbolContextBlock))
1852 block_die =
1853 function_die.LookupDeepestBlock(file_vm_addr);
1854 }
1855
Konrad Kleine248a1302019-05-23 11:14:47 +00001856 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001857 Block &block = sc.function->GetBlock(true);
1858
1859 if (block_die)
1860 sc.block = block.FindBlockByID(block_die.GetID());
1861 else if (function_die)
1862 sc.block =
1863 block.FindBlockByID(function_die.GetID());
1864 }
1865 }
1866 }
1867
1868 sc_list.Append(sc);
1869 line_idx = line_table->FindLineEntryIndexByFileIndex(
1870 line_idx + 1, file_idx, found_line, true,
1871 &sc.line_entry);
1872 }
1873 }
1874 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1875 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001876 // sites by file and line and if the file spec matches that of
1877 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 sc_list.Append(sc);
1879 }
1880 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1881 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001882 // sites by file and line and if the file spec matches that of
1883 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884 sc_list.Append(sc);
1885 }
1886
1887 if (!check_inlines)
1888 break;
1889 }
1890 }
1891 }
1892 }
1893 }
1894 return sc_list.GetSize() - prev_size;
1895}
1896
Jim Ingham7fca8c02017-04-28 00:51:06 +00001897void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001898 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001899 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001900}
1901
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001902std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1903 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1904 if (module_sp)
1905 return module_sp->GetMutex();
1906 return GetObjectFile()->GetModule()->GetMutex();
1907}
1908
Kate Stoneb9c1b512016-09-06 20:57:50 +00001909bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1910 const lldb_private::CompilerDeclContext *decl_ctx) {
1911 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001912 // Invalid namespace decl which means we aren't matching only things in
1913 // this symbol file, so return true to indicate it matches this symbol
1914 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 return true;
1916 }
1917
1918 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1919 TypeSystem *type_system = GetTypeSystemForLanguage(
1920 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1921 if (decl_ctx_type_system == type_system)
1922 return true; // The type systems match, return true
1923
1924 // The namespace AST was valid, and it does not match...
1925 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1926
1927 if (log)
1928 GetObjectFile()->GetModule()->LogMessage(
1929 log, "Valid namespace does not match symbol file");
1930
1931 return false;
1932}
1933
1934uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001935 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00001936 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001937 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1938
1939 if (log)
1940 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00001941 log,
1942 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
1943 "parent_decl_ctx=%p, max_matches=%u, variables)",
1944 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945 max_matches);
1946
1947 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1948 return 0;
1949
1950 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00001951 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 return 0;
1953
Pavel Labath34cda142018-05-31 09:46:26 +00001954 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 const uint32_t original_size = variables.GetSize();
1956
Pavel Labathe1d18752018-06-07 10:04:44 +00001957 llvm::StringRef basename;
1958 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00001959 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00001960
1961 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
1962 context, basename))
1963 basename = name.GetStringRef();
1964
Kate Stoneb9c1b512016-09-06 20:57:50 +00001965 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00001966 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001967 const size_t num_die_matches = die_offsets.size();
1968 if (num_die_matches) {
1969 SymbolContext sc;
1970 sc.module_sp = m_obj_file->GetModule();
1971 assert(sc.module_sp);
1972
Pavel Labathe1d18752018-06-07 10:04:44 +00001973 // Loop invariant: Variables up to this index have been checked for context
1974 // matches.
1975 uint32_t pruned_idx = original_size;
1976
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 bool done = false;
1978 for (size_t i = 0; i < num_die_matches && !done; ++i) {
1979 const DIERef &die_ref = die_offsets[i];
1980 DWARFDIE die = GetDIE(die_ref);
1981
1982 if (die) {
1983 switch (die.Tag()) {
1984 default:
1985 case DW_TAG_subprogram:
1986 case DW_TAG_inlined_subroutine:
1987 case DW_TAG_try_block:
1988 case DW_TAG_catch_block:
1989 break;
1990
1991 case DW_TAG_variable: {
1992 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1993
1994 if (parent_decl_ctx) {
1995 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1996 if (dwarf_ast) {
1997 CompilerDeclContext actual_parent_decl_ctx =
1998 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
1999 if (!actual_parent_decl_ctx ||
2000 actual_parent_decl_ctx != *parent_decl_ctx)
2001 continue;
2002 }
2003 }
2004
2005 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2006 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002007 while (pruned_idx < variables.GetSize()) {
2008 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002009 if (name_is_mangled ||
2010 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002011 ++pruned_idx;
2012 else
2013 variables.RemoveVariableAtIndex(pruned_idx);
2014 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015
2016 if (variables.GetSize() - original_size >= max_matches)
2017 done = true;
2018 } break;
2019 }
2020 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002021 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2022 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 }
2024 }
2025 }
2026
2027 // Return the number of variable that were appended to the list
2028 const uint32_t num_matches = variables.GetSize() - original_size;
2029 if (log && num_matches > 0) {
2030 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002031 log,
2032 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2033 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2034 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002035 max_matches, num_matches);
2036 }
2037 return num_matches;
2038}
2039
2040uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002041 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042 VariableList &variables) {
2043 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2044
2045 if (log) {
2046 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002047 log,
2048 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2049 "max_matches=%u, variables)",
2050 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051 }
2052
2053 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002054 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055 return 0;
2056
Pavel Labath34cda142018-05-31 09:46:26 +00002057 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002058 const uint32_t original_size = variables.GetSize();
2059
2060 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002061 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002062
2063 SymbolContext sc;
2064 sc.module_sp = m_obj_file->GetModule();
2065 assert(sc.module_sp);
2066
2067 const size_t num_matches = die_offsets.size();
2068 if (num_matches) {
2069 for (size_t i = 0; i < num_matches; ++i) {
2070 const DIERef &die_ref = die_offsets[i];
2071 DWARFDIE die = GetDIE(die_ref);
2072
2073 if (die) {
2074 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2075
2076 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2077
2078 if (variables.GetSize() - original_size >= max_matches)
2079 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002080 } else
2081 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002082 }
2083 }
2084
2085 // Return the number of variable that were appended to the list
2086 return variables.GetSize() - original_size;
2087}
2088
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2090 bool include_inlines,
2091 SymbolContextList &sc_list) {
2092 SymbolContext sc;
2093
2094 if (!orig_die)
2095 return false;
2096
2097 // If we were passed a die that is not a function, just return false...
2098 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2099 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2100 return false;
2101
2102 DWARFDIE die = orig_die;
2103 DWARFDIE inlined_die;
2104 if (die.Tag() == DW_TAG_inlined_subroutine) {
2105 inlined_die = die;
2106
2107 while (1) {
2108 die = die.GetParent();
2109
2110 if (die) {
2111 if (die.Tag() == DW_TAG_subprogram)
2112 break;
2113 } else
2114 break;
2115 }
2116 }
2117 assert(die && die.Tag() == DW_TAG_subprogram);
2118 if (GetFunction(die, sc)) {
2119 Address addr;
2120 // Parse all blocks if needed
2121 if (inlined_die) {
2122 Block &function_block = sc.function->GetBlock(true);
2123 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002124 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002125 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002126 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127 addr.Clear();
2128 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002129 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002130 addr = sc.function->GetAddressRange().GetBaseAddress();
2131 }
2132
2133 if (addr.IsValid()) {
2134 sc_list.Append(sc);
2135 return true;
2136 }
2137 }
2138
2139 return false;
2140}
2141
Kate Stoneb9c1b512016-09-06 20:57:50 +00002142bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2143 const DWARFDIE &die) {
2144 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002145 // parent decl context isn't valid, we aren't trying to look for any
2146 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2148 return true;
2149
2150 if (die) {
2151 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2152 if (dwarf_ast) {
2153 CompilerDeclContext actual_decl_ctx =
2154 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2155 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002156 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 }
2158 }
2159 return false;
2160}
2161
Zachary Turner117b1fa2018-10-25 20:45:40 +00002162uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002163 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002164 FunctionNameType name_type_mask, bool include_inlines, bool append,
2165 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002166 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2167 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 name.AsCString());
2169
2170 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002171 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002172 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2173
2174 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2175
2176 if (log) {
2177 GetObjectFile()->GetModule()->LogMessage(
2178 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2179 "name_type_mask=0x%x, append=%u, sc_list)",
2180 name.GetCString(), name_type_mask, append);
2181 }
2182
2183 // If we aren't appending the results to this list, then clear the list
2184 if (!append)
2185 sc_list.Clear();
2186
2187 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2188 return 0;
2189
2190 // If name is empty then we won't find anything.
2191 if (name.IsEmpty())
2192 return 0;
2193
Adrian Prantl05097242018-04-30 16:49:04 +00002194 // Remember how many sc_list are in the list before we search in case we are
2195 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002196
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197 const uint32_t original_size = sc_list.GetSize();
2198
2199 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002200 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 return 0;
2202
Pavel Labath5af11ab2018-06-05 10:33:56 +00002203 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2204 DIEArray offsets;
2205 CompilerDeclContext empty_decl_ctx;
2206 if (!parent_decl_ctx)
2207 parent_decl_ctx = &empty_decl_ctx;
2208
2209 std::vector<DWARFDIE> dies;
2210 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2211 for (const DWARFDIE &die: dies) {
2212 if (resolved_dies.insert(die.GetDIE()).second)
2213 ResolveFunction(die, include_inlines, sc_list);
2214 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215
2216 // Return the number of variable that were appended to the list
2217 const uint32_t num_matches = sc_list.GetSize() - original_size;
2218
2219 if (log && num_matches > 0) {
2220 GetObjectFile()->GetModule()->LogMessage(
2221 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2222 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2223 "%u",
2224 name.GetCString(), name_type_mask, include_inlines, append,
2225 num_matches);
2226 }
2227 return num_matches;
2228}
2229
2230uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2231 bool include_inlines, bool append,
2232 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002233 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2234 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002235 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002236
2237 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2238
2239 if (log) {
2240 GetObjectFile()->GetModule()->LogMessage(
2241 log,
2242 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002243 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 }
2245
2246 // If we aren't appending the results to this list, then clear the list
2247 if (!append)
2248 sc_list.Clear();
2249
Pavel Labathb13f0332018-05-21 14:12:52 +00002250 DWARFDebugInfo *info = DebugInfo();
2251 if (!info)
2252 return 0;
2253
Adrian Prantl05097242018-04-30 16:49:04 +00002254 // Remember how many sc_list are in the list before we search in case we are
2255 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002256 uint32_t original_size = sc_list.GetSize();
2257
Pavel Labath6de9c792018-06-05 12:13:22 +00002258 DIEArray offsets;
2259 m_index->GetFunctions(regex, offsets);
2260
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002261 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2262 for (DIERef ref : offsets) {
2263 DWARFDIE die = info->GetDIE(ref);
2264 if (!die) {
2265 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2266 continue;
2267 }
2268 if (resolved_dies.insert(die.GetDIE()).second)
2269 ResolveFunction(die, include_inlines, sc_list);
2270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271
2272 // Return the number of variable that were appended to the list
2273 return sc_list.GetSize() - original_size;
2274}
2275
2276void SymbolFileDWARF::GetMangledNamesForFunction(
2277 const std::string &scope_qualified_name,
2278 std::vector<ConstString> &mangled_names) {
2279 DWARFDebugInfo *info = DebugInfo();
2280 uint32_t num_comp_units = 0;
2281 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002282 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283
2284 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002285 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286 if (cu == nullptr)
2287 continue;
2288
2289 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2290 if (dwo)
2291 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2292 }
2293
2294 NameToOffsetMap::iterator iter =
2295 m_function_scope_qualified_name_map.find(scope_qualified_name);
2296 if (iter == m_function_scope_qualified_name_map.end())
2297 return;
2298
2299 DIERefSetSP set_sp = (*iter).second;
2300 std::set<DIERef>::iterator set_iter;
2301 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2302 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2303 mangled_names.push_back(ConstString(die.GetMangledName()));
2304 }
2305}
2306
2307uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002308 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002309 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002310 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2311 TypeMap &types) {
2312 // If we aren't appending the results to this list, then clear the list
2313 if (!append)
2314 types.Clear();
2315
2316 // Make sure we haven't already searched this SymbolFile before...
2317 if (searched_symbol_files.count(this))
2318 return 0;
2319 else
2320 searched_symbol_files.insert(this);
2321
2322 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002323 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 return 0;
2325
2326 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2327
2328 if (log) {
2329 if (parent_decl_ctx)
2330 GetObjectFile()->GetModule()->LogMessage(
2331 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2332 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2333 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2334 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2335 else
2336 GetObjectFile()->GetModule()->LogMessage(
2337 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2338 "NULL, append=%u, max_matches=%u, type_list)",
2339 name.GetCString(), append, max_matches);
2340 }
2341
2342 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2343 return 0;
2344
2345 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002346 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347 const size_t num_die_matches = die_offsets.size();
2348
2349 if (num_die_matches) {
2350 const uint32_t initial_types_size = types.GetSize();
2351 for (size_t i = 0; i < num_die_matches; ++i) {
2352 const DIERef &die_ref = die_offsets[i];
2353 DWARFDIE die = GetDIE(die_ref);
2354
2355 if (die) {
2356 if (!DIEInDeclContext(parent_decl_ctx, die))
2357 continue; // The containing decl contexts don't match
2358
2359 Type *matching_type = ResolveType(die, true, true);
2360 if (matching_type) {
2361 // We found a type pointer, now find the shared pointer form our type
2362 // list
2363 types.InsertUnique(matching_type->shared_from_this());
2364 if (types.GetSize() >= max_matches)
2365 break;
2366 }
2367 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002368 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2369 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002370 }
2371 }
2372 const uint32_t num_matches = types.GetSize() - initial_types_size;
2373 if (log && num_matches) {
2374 if (parent_decl_ctx) {
2375 GetObjectFile()->GetModule()->LogMessage(
2376 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2377 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2378 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2379 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2380 num_matches);
2381 } else {
2382 GetObjectFile()->GetModule()->LogMessage(
2383 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2384 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2385 name.GetCString(), append, max_matches, num_matches);
2386 }
2387 }
2388 return num_matches;
2389 } else {
2390 UpdateExternalModuleListIfNeeded();
2391
2392 for (const auto &pair : m_external_type_modules) {
2393 ModuleSP external_module_sp = pair.second;
2394 if (external_module_sp) {
2395 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2396 if (sym_vendor) {
2397 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002398 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2399 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 if (num_external_matches)
2401 return num_external_matches;
2402 }
2403 }
2404 }
2405 }
2406
2407 return 0;
2408}
2409
2410size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2411 bool append, TypeMap &types) {
2412 if (!append)
2413 types.Clear();
2414
2415 if (context.empty())
2416 return 0;
2417
Kate Stoneb9c1b512016-09-06 20:57:50 +00002418 ConstString name = context.back().name;
2419
2420 if (!name)
2421 return 0;
2422
Pavel Labathb13f0332018-05-21 14:12:52 +00002423 DIEArray die_offsets;
2424 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002425 const size_t num_die_matches = die_offsets.size();
2426
2427 if (num_die_matches) {
2428 size_t num_matches = 0;
2429 for (size_t i = 0; i < num_die_matches; ++i) {
2430 const DIERef &die_ref = die_offsets[i];
2431 DWARFDIE die = GetDIE(die_ref);
2432
2433 if (die) {
2434 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002435 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 if (die_context != context)
2437 continue;
2438
2439 Type *matching_type = ResolveType(die, true, true);
2440 if (matching_type) {
2441 // We found a type pointer, now find the shared pointer form our type
2442 // list
2443 types.InsertUnique(matching_type->shared_from_this());
2444 ++num_matches;
2445 }
2446 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002447 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2448 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002449 }
2450 }
2451 return num_matches;
2452 }
2453 return 0;
2454}
2455
2456CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002457SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002458 const CompilerDeclContext *parent_decl_ctx) {
2459 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2460
2461 if (log) {
2462 GetObjectFile()->GetModule()->LogMessage(
2463 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2464 name.GetCString());
2465 }
2466
2467 CompilerDeclContext namespace_decl_ctx;
2468
2469 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2470 return namespace_decl_ctx;
2471
2472 DWARFDebugInfo *info = DebugInfo();
2473 if (info) {
2474 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002475 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002476 const size_t num_matches = die_offsets.size();
2477 if (num_matches) {
2478 for (size_t i = 0; i < num_matches; ++i) {
2479 const DIERef &die_ref = die_offsets[i];
2480 DWARFDIE die = GetDIE(die_ref);
2481
2482 if (die) {
2483 if (!DIEInDeclContext(parent_decl_ctx, die))
2484 continue; // The containing decl contexts don't match
2485
2486 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2487 if (dwarf_ast) {
2488 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2489 if (namespace_decl_ctx)
2490 break;
2491 }
2492 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002493 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2494 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 }
2496 }
2497 }
2498 }
2499 if (log && namespace_decl_ctx) {
2500 GetObjectFile()->GetModule()->LogMessage(
2501 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2502 "CompilerDeclContext(%p/%p) \"%s\"",
2503 name.GetCString(),
2504 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2505 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2506 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2507 }
2508
2509 return namespace_decl_ctx;
2510}
2511
2512TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2513 bool resolve_function_context) {
2514 TypeSP type_sp;
2515 if (die) {
2516 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002517 if (type_ptr == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002518 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2519 assert(lldb_cu);
2520 SymbolContext sc(lldb_cu);
2521 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2522 while (parent_die != nullptr) {
2523 if (parent_die->Tag() == DW_TAG_subprogram)
2524 break;
2525 parent_die = parent_die->GetParent();
2526 }
2527 SymbolContext sc_backup = sc;
2528 if (resolve_function_context && parent_die != nullptr &&
2529 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2530 sc = sc_backup;
2531
Konrad Kleine248a1302019-05-23 11:14:47 +00002532 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002533 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2534 // Grab the existing type from the master types lists
2535 type_sp = type_ptr->shared_from_this();
2536 }
2537 }
2538 return type_sp;
2539}
2540
2541DWARFDIE
2542SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2543 if (orig_die) {
2544 DWARFDIE die = orig_die;
2545
2546 while (die) {
2547 // If this is the original DIE that we are searching for a declaration
2548 // for, then don't look in the cache as we don't want our own decl
2549 // context to be our decl context...
2550 if (orig_die != die) {
2551 switch (die.Tag()) {
2552 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002553 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002554 case DW_TAG_namespace:
2555 case DW_TAG_structure_type:
2556 case DW_TAG_union_type:
2557 case DW_TAG_class_type:
2558 case DW_TAG_lexical_block:
2559 case DW_TAG_subprogram:
2560 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002561 case DW_TAG_inlined_subroutine: {
2562 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2563 if (abs_die) {
2564 return abs_die;
2565 }
2566 break;
2567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 default:
2569 break;
2570 }
2571 }
2572
2573 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2574 if (spec_die) {
2575 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2576 if (decl_ctx_die)
2577 return decl_ctx_die;
2578 }
2579
2580 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2581 if (abs_die) {
2582 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2583 if (decl_ctx_die)
2584 return decl_ctx_die;
2585 }
2586
2587 die = die.GetParent();
2588 }
2589 }
2590 return DWARFDIE();
2591}
2592
2593Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002594SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002595 Symbol *objc_class_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002596 if (m_obj_file) {
2597 Symtab *symtab = m_obj_file->GetSymtab();
2598 if (symtab) {
2599 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2600 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2601 Symtab::eVisibilityAny);
2602 }
2603 }
2604 return objc_class_symbol;
2605}
2606
2607// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002608// they don't then we can end up looking through all class types for a complete
2609// type and never find the full definition. We need to know if this attribute
2610// is supported, so we determine this here and cache th result. We also need to
2611// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002612// DWARF file
2613// if we are doing darwin DWARF in .o file debugging.
2614bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002615 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2617 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2618 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2619 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2620 else {
2621 DWARFDebugInfo *debug_info = DebugInfo();
2622 const uint32_t num_compile_units = GetNumCompileUnits();
2623 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002624 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002625 if (dwarf_cu != cu &&
2626 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2627 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2628 break;
2629 }
2630 }
2631 }
2632 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2633 GetDebugMapSymfile())
2634 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2635 }
2636 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002637}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002638
2639// This function can be used when a DIE is found that is a forward declaration
2640// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002641TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002642 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002644
Kate Stoneb9c1b512016-09-06 20:57:50 +00002645 TypeSP type_sp;
2646
2647 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2648 return type_sp;
2649
2650 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002651 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652
2653 const size_t num_matches = die_offsets.size();
2654
2655 if (num_matches) {
2656 for (size_t i = 0; i < num_matches; ++i) {
2657 const DIERef &die_ref = die_offsets[i];
2658 DWARFDIE type_die = GetDIE(die_ref);
2659
2660 if (type_die) {
2661 bool try_resolving_type = false;
2662
Adrian Prantl05097242018-04-30 16:49:04 +00002663 // Don't try and resolve the DIE we are looking for with the DIE
2664 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002665 if (type_die != die) {
2666 switch (type_die.Tag()) {
2667 case DW_TAG_class_type:
2668 case DW_TAG_structure_type:
2669 try_resolving_type = true;
2670 break;
2671 default:
2672 break;
2673 }
2674 }
2675
2676 if (try_resolving_type) {
2677 if (must_be_implementation &&
2678 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2679 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2680 DW_AT_APPLE_objc_complete_type, 0);
2681
2682 if (try_resolving_type) {
2683 Type *resolved_type = ResolveType(type_die, false, true);
2684 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2685 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2686 " (cu 0x%8.8" PRIx64 ")\n",
2687 die.GetID(),
2688 m_obj_file->GetFileSpec().GetFilename().AsCString(
2689 "<Unknown>"),
2690 type_die.GetID(), type_cu->GetID());
2691
2692 if (die)
2693 GetDIEToType()[die.GetDIE()] = resolved_type;
2694 type_sp = resolved_type->shared_from_this();
2695 break;
2696 }
2697 }
2698 }
2699 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002700 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2701 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 }
2703 }
2704 }
2705 return type_sp;
2706}
Greg Claytona8022fa2012-04-24 21:22:41 +00002707
Adrian Prantl05097242018-04-30 16:49:04 +00002708// This function helps to ensure that the declaration contexts match for two
2709// different DIEs. Often times debug information will refer to a forward
2710// declaration of a type (the equivalent of "struct my_struct;". There will
2711// often be a declaration of that type elsewhere that has the full definition.
2712// When we go looking for the full type "my_struct", we will find one or more
2713// matches in the accelerator tables and we will then need to make sure the
2714// type was in the same declaration context as the original DIE. This function
2715// can efficiently compare two DIEs and will return true when the declaration
2716// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002717bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2718 const DWARFDIE &die2) {
2719 if (die1 == die2)
2720 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002721
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002722 std::vector<DWARFDIE> decl_ctx_1;
2723 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002724 // The declaration DIE stack is a stack of the declaration context DIEs all
2725 // the way back to the compile unit. If a type "T" is declared inside a class
2726 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2727 // namespace "lldb", and the namespace is in a compile unit, there will be a
2728 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002729 //
2730 // [0] DW_TAG_class_type for "B"
2731 // [1] DW_TAG_class_type for "A"
2732 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002733 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002734 //
Adrian Prantl05097242018-04-30 16:49:04 +00002735 // We grab both contexts and make sure that everything matches all the way
2736 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002737
2738 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002739 decl_ctx_1 = die1.GetDeclContextDIEs();
2740 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002741 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002742 const size_t count1 = decl_ctx_1.size();
2743 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002744 if (count1 != count2)
2745 return false;
2746
Adrian Prantl05097242018-04-30 16:49:04 +00002747 // Make sure the DW_TAG values match all the way back up the compile unit. If
2748 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002749 DWARFDIE decl_ctx_die1;
2750 DWARFDIE decl_ctx_die2;
2751 size_t i;
2752 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002753 decl_ctx_die1 = decl_ctx_1[i];
2754 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002755 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2756 return false;
2757 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002758#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002759
Kate Stoneb9c1b512016-09-06 20:57:50 +00002760 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002761 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2762 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2763 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002764 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002765 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2766 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002767
Greg Clayton890ff562012-02-02 05:48:16 +00002768#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002769 // Always skip the compile unit when comparing by only iterating up to "count
2770 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002771 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002772 decl_ctx_die1 = decl_ctx_1[i];
2773 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002774 const char *name1 = decl_ctx_die1.GetName();
2775 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002776 // If the string was from a DW_FORM_strp, then the pointer will often be
2777 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002778 if (name1 == name2)
2779 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002780
Adrian Prantl05097242018-04-30 16:49:04 +00002781 // Name pointers are not equal, so only compare the strings if both are not
2782 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002783 if (name1 && name2) {
2784 // If the strings don't compare, we are done...
2785 if (strcmp(name1, name2) != 0)
2786 return false;
2787 } else {
2788 // One name was NULL while the other wasn't
2789 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002790 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791 }
Adrian Prantl05097242018-04-30 16:49:04 +00002792 // We made it through all of the checks and the declaration contexts are
2793 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002795}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002796
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2798 const DWARFDeclContext &dwarf_decl_ctx) {
2799 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002800
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2802 if (dwarf_decl_ctx_count > 0) {
2803 const ConstString type_name(dwarf_decl_ctx[0].name);
2804 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002805
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 if (type_name) {
2807 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2808 DWARF_LOG_LOOKUPS));
2809 if (log) {
2810 GetObjectFile()->GetModule()->LogMessage(
2811 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2812 "s, qualified-name='%s')",
2813 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2814 dwarf_decl_ctx.GetQualifiedName());
2815 }
2816
2817 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002818 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002819 const size_t num_matches = die_offsets.size();
2820
Adrian Prantl05097242018-04-30 16:49:04 +00002821 // Get the type system that we are looking to find a type for. We will
2822 // use this to ensure any matches we find are in a language that this
2823 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002824 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2825 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2826 ? nullptr
2827 : GetTypeSystemForLanguage(language);
2828
2829 if (num_matches) {
2830 for (size_t i = 0; i < num_matches; ++i) {
2831 const DIERef &die_ref = die_offsets[i];
2832 DWARFDIE type_die = GetDIE(die_ref);
2833
2834 if (type_die) {
2835 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002836 // looking for. We don't want to find a "Foo" type from Java if we
2837 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 if (type_system &&
2839 !type_system->SupportsLanguage(type_die.GetLanguage()))
2840 continue;
2841 bool try_resolving_type = false;
2842
2843 // Don't try and resolve the DIE we are looking for with the DIE
2844 // itself!
2845 const dw_tag_t type_tag = type_die.Tag();
2846 // Make sure the tags match
2847 if (type_tag == tag) {
2848 // The tags match, lets try resolving this type
2849 try_resolving_type = true;
2850 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002851 // The tags don't match, but we need to watch our for a forward
2852 // declaration for a struct and ("struct foo") ends up being a
2853 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 switch (type_tag) {
2855 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002856 // We had a "class foo", see if we ended up with a "struct foo
2857 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 try_resolving_type = (tag == DW_TAG_structure_type);
2859 break;
2860 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002861 // We had a "struct foo", see if we ended up with a "class foo
2862 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002863 try_resolving_type = (tag == DW_TAG_class_type);
2864 break;
2865 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002866 // Tags don't match, don't event try to resolve using this type
2867 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002868 break;
2869 }
2870 }
2871
2872 if (try_resolving_type) {
2873 DWARFDeclContext type_dwarf_decl_ctx;
2874 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2875
2876 if (log) {
2877 GetObjectFile()->GetModule()->LogMessage(
2878 log, "SymbolFileDWARF::"
2879 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2880 "qualified-name='%s') trying die=0x%8.8x (%s)",
2881 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2882 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2883 type_dwarf_decl_ctx.GetQualifiedName());
2884 }
2885
2886 // Make sure the decl contexts match all the way up
2887 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2888 Type *resolved_type = ResolveType(type_die, false);
2889 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2890 type_sp = resolved_type->shared_from_this();
2891 break;
2892 }
2893 }
2894 } else {
2895 if (log) {
2896 std::string qualified_name;
2897 type_die.GetQualifiedName(qualified_name);
2898 GetObjectFile()->GetModule()->LogMessage(
2899 log, "SymbolFileDWARF::"
2900 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2901 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2902 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2903 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2904 qualified_name.c_str());
2905 }
2906 }
2907 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002908 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2909 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002910 }
2911 }
2912 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002914 }
2915 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002916}
2917
Kate Stoneb9c1b512016-09-06 20:57:50 +00002918TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2919 bool *type_is_new_ptr) {
2920 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002921
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 if (die) {
2923 TypeSystem *type_system =
2924 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00002925
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 if (type_system) {
2927 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2928 if (dwarf_ast) {
2929 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
2930 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
2931 if (type_sp) {
2932 TypeList *type_list = GetTypeList();
2933 if (type_list)
2934 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00002935
Kate Stoneb9c1b512016-09-06 20:57:50 +00002936 if (die.Tag() == DW_TAG_subprogram) {
2937 DIERef die_ref = die.GetDIERef();
2938 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2939 .GetScopeQualifiedName()
2940 .AsCString(""));
2941 if (scope_qualified_name.size()) {
2942 NameToOffsetMap::iterator iter =
2943 m_function_scope_qualified_name_map.find(
2944 scope_qualified_name);
2945 if (iter != m_function_scope_qualified_name_map.end())
2946 (*iter).second->insert(die_ref);
2947 else {
2948 DIERefSetSP new_set(new std::set<DIERef>);
2949 new_set->insert(die_ref);
2950 m_function_scope_qualified_name_map.emplace(
2951 std::make_pair(scope_qualified_name, new_set));
2952 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002953 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002954 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00002955 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002956 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958 }
2959
2960 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002961}
2962
Kate Stoneb9c1b512016-09-06 20:57:50 +00002963size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
2964 const DWARFDIE &orig_die,
2965 bool parse_siblings, bool parse_children) {
2966 size_t types_added = 0;
2967 DWARFDIE die = orig_die;
2968 while (die) {
2969 bool type_is_new = false;
2970 if (ParseType(sc, die, &type_is_new).get()) {
2971 if (type_is_new)
2972 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974
2975 if (parse_children && die.HasChildren()) {
2976 if (die.Tag() == DW_TAG_subprogram) {
2977 SymbolContext child_sc(sc);
2978 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
2979 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
2980 } else
2981 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
2982 }
2983
2984 if (parse_siblings)
2985 die = die.GetSibling();
2986 else
2987 die.Clear();
2988 }
2989 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002990}
2991
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00002992size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002993 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00002994 CompileUnit *comp_unit = func.GetCompileUnit();
2995 lldbassert(comp_unit);
2996
2997 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
2998 if (!dwarf_cu)
2999 return 0;
3000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003002 const dw_offset_t function_die_offset = func.GetID();
3003 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3004 if (function_die) {
3005 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3006 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003007 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003008
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003010}
3011
Zachary Turner863f8c12019-01-11 18:03:20 +00003012size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003013 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003014 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003015 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003017 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3018 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3019 SymbolContext sc;
3020 sc.comp_unit = &comp_unit;
3021 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003022 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003023 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003024
Kate Stoneb9c1b512016-09-06 20:57:50 +00003025 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026}
3027
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003029 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +00003030 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003032 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033 return 0;
3034
3035 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003036 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037
3038 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3039 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3040 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3041 const size_t num_variables = ParseVariables(
3042 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3043
3044 // Let all blocks know they have parse all their variables
3045 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3046 return num_variables;
3047 }
3048 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003049 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050
Konrad Kleine248a1302019-05-23 11:14:47 +00003051 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052 return 0;
3053
3054 uint32_t vars_added = 0;
3055 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3056
Konrad Kleine248a1302019-05-23 11:14:47 +00003057 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003058 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003059 sc.comp_unit->SetVariableList(variables);
3060
3061 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003062 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003063 const size_t num_matches = die_offsets.size();
3064 if (num_matches) {
3065 for (size_t i = 0; i < num_matches; ++i) {
3066 const DIERef &die_ref = die_offsets[i];
3067 DWARFDIE die = GetDIE(die_ref);
3068 if (die) {
3069 VariableSP var_sp(
3070 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3071 if (var_sp) {
3072 variables->AddVariableIfUnique(var_sp);
3073 ++vars_added;
3074 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003075 } else
3076 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003079 }
3080 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003082 }
3083 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003084}
3085
Kate Stoneb9c1b512016-09-06 20:57:50 +00003086VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3087 const DWARFDIE &die,
3088 const lldb::addr_t func_low_pc) {
3089 if (die.GetDWARF() != this)
3090 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003091
Kate Stoneb9c1b512016-09-06 20:57:50 +00003092 VariableSP var_sp;
3093 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095
Kate Stoneb9c1b512016-09-06 20:57:50 +00003096 var_sp = GetDIEToVariable()[die.GetDIE()];
3097 if (var_sp)
3098 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003099
Kate Stoneb9c1b512016-09-06 20:57:50 +00003100 const dw_tag_t tag = die.Tag();
3101 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003102
Kate Stoneb9c1b512016-09-06 20:57:50 +00003103 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3104 (tag == DW_TAG_formal_parameter && sc.function)) {
3105 DWARFAttributes attributes;
3106 const size_t num_attributes = die.GetAttributes(attributes);
3107 DWARFDIE spec_die;
3108 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003109 const char *name = nullptr;
3110 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003111 Declaration decl;
3112 uint32_t i;
3113 DWARFFormValue type_die_form;
3114 DWARFExpression location(die.GetCU());
3115 bool is_external = false;
3116 bool is_artificial = false;
3117 bool location_is_const_value_data = false;
3118 bool has_explicit_location = false;
3119 DWARFFormValue const_value;
3120 Variable::RangeList scope_ranges;
3121 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003122
Kate Stoneb9c1b512016-09-06 20:57:50 +00003123 for (i = 0; i < num_attributes; ++i) {
3124 dw_attr_t attr = attributes.AttributeAtIndex(i);
3125 DWARFFormValue form_value;
3126
3127 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3128 switch (attr) {
3129 case DW_AT_decl_file:
3130 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3131 form_value.Unsigned()));
3132 break;
3133 case DW_AT_decl_line:
3134 decl.SetLine(form_value.Unsigned());
3135 break;
3136 case DW_AT_decl_column:
3137 decl.SetColumn(form_value.Unsigned());
3138 break;
3139 case DW_AT_name:
3140 name = form_value.AsCString();
3141 break;
3142 case DW_AT_linkage_name:
3143 case DW_AT_MIPS_linkage_name:
3144 mangled = form_value.AsCString();
3145 break;
3146 case DW_AT_type:
3147 type_die_form = form_value;
3148 break;
3149 case DW_AT_external:
3150 is_external = form_value.Boolean();
3151 break;
3152 case DW_AT_const_value:
3153 // If we have already found a DW_AT_location attribute, ignore this
3154 // attribute.
3155 if (!has_explicit_location) {
3156 location_is_const_value_data = true;
3157 // The constant value will be either a block, a data value or a
3158 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003159 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3161 // Retrieve the value as a block expression.
3162 uint32_t block_offset =
3163 form_value.BlockData() - debug_info_data.GetDataStart();
3164 uint32_t block_length = form_value.Unsigned();
3165 location.CopyOpcodeData(module, debug_info_data, block_offset,
3166 block_length);
3167 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3168 // Retrieve the value as a data expression.
3169 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3170 DWARFFormValue::GetFixedFormSizesForAddressSize(
Zachary Turner7e44a842019-03-12 20:51:05 +00003171 attributes.CompileUnitAtIndex(i)->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003172 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3173 uint32_t data_length =
3174 fixed_form_sizes.GetSize(form_value.Form());
3175 if (data_length == 0) {
3176 const uint8_t *data_pointer = form_value.BlockData();
3177 if (data_pointer) {
3178 form_value.Unsigned();
3179 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3180 // we need to get the byte size of the type later after we
3181 // create the variable
3182 const_value = form_value;
3183 }
3184 } else
3185 location.CopyOpcodeData(module, debug_info_data, data_offset,
3186 data_length);
3187 } else {
3188 // Retrieve the value as a string expression.
3189 if (form_value.Form() == DW_FORM_strp) {
3190 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3191 DWARFFormValue::GetFixedFormSizesForAddressSize(
3192 attributes.CompileUnitAtIndex(i)
Zachary Turner7e44a842019-03-12 20:51:05 +00003193 ->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3195 uint32_t data_length =
3196 fixed_form_sizes.GetSize(form_value.Form());
3197 location.CopyOpcodeData(module, debug_info_data, data_offset,
3198 data_length);
3199 } else {
3200 const char *str = form_value.AsCString();
3201 uint32_t string_offset =
3202 str - (const char *)debug_info_data.GetDataStart();
3203 uint32_t string_length = strlen(str) + 1;
3204 location.CopyOpcodeData(module, debug_info_data,
3205 string_offset, string_length);
3206 }
3207 }
3208 }
3209 break;
3210 case DW_AT_location: {
3211 location_is_const_value_data = false;
3212 has_explicit_location = true;
3213 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003214 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215
3216 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003217 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003218 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003219 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003221 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3223
3224 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003225 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003226 if (loc_list_length > 0) {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003227 location.CopyOpcodeData(module, debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003228 debug_loc_offset, loc_list_length);
3229 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3230 location.SetLocationListSlide(
3231 func_low_pc -
3232 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3233 }
3234 }
3235 } break;
3236 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003237 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 break;
3239 case DW_AT_start_scope: {
3240 if (form_value.Form() == DW_FORM_sec_offset) {
3241 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003242 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3243 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003244 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003245 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003246 } else {
3247 // TODO: Handle the case when DW_AT_start_scope have form
3248 // constant. The
3249 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003250 // behavior in case the enclosing block have a non coninious
3251 // address range and the DW_AT_start_scope entry have a form
3252 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003253 GetObjectFile()->GetModule()->ReportWarning(
3254 "0x%8.8" PRIx64
3255 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3256 die.GetID(), form_value.Form());
3257 }
3258
3259 scope_ranges.Sort();
3260 scope_ranges.CombineConsecutiveRanges();
3261 } break;
3262 case DW_AT_artificial:
3263 is_artificial = form_value.Boolean();
3264 break;
3265 case DW_AT_accessibility:
3266 break; // accessibility =
3267 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3268 case DW_AT_declaration:
3269 case DW_AT_description:
3270 case DW_AT_endianity:
3271 case DW_AT_segment:
3272 case DW_AT_visibility:
3273 default:
3274 case DW_AT_abstract_origin:
3275 case DW_AT_sibling:
3276 break;
3277 }
3278 }
3279 }
3280
3281 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3282 const dw_tag_t parent_tag = die.GetParent().Tag();
3283 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003284 (parent_tag == DW_TAG_compile_unit ||
3285 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286 (parent_context_die.Tag() == DW_TAG_class_type ||
3287 parent_context_die.Tag() == DW_TAG_structure_type);
3288
3289 ValueType scope = eValueTypeInvalid;
3290
3291 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003292 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293
Sam McCall30621392016-11-22 11:40:25 +00003294 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003295 if (!mangled) {
3296 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003297 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3298 // of global variables with commands like "frame var j". For
3299 // example, if j were an int variable holding a value 4 and
3300 // declared in a namespace B which in turn is contained in a
3301 // namespace A, the command "frame var j" returns
3302 // "(int) A::B::j = 4".
3303 // If the compiler does not emit a linkage name, we should be
3304 // able to generate a fully qualified name from the
3305 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003306 if ((parent_tag == DW_TAG_compile_unit ||
3307 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003308 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3309 DWARFDeclContext decl_ctx;
3310
3311 die.GetDWARFDeclContext(decl_ctx);
3312 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3313 }
3314 }
3315
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 if (tag == DW_TAG_formal_parameter)
3317 scope = eValueTypeVariableArgument;
3318 else {
Sam McCall30621392016-11-22 11:40:25 +00003319 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3320 // or static variable, so we have to do a little digging:
3321 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3322 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3323 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3324 // Clang likes to combine small global variables into the same symbol
3325 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3326 // so we need to look through the whole expression.
3327 bool is_static_lifetime =
3328 has_explicit_mangled ||
3329 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003330 // Check if the location has a DW_OP_addr with any address value...
3331 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3332 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003333 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003334 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3335 if (op_error) {
3336 StreamString strm;
3337 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003338 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003339 GetObjectFile()->GetModule()->ReportError(
3340 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003341 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003342 }
Sam McCall30621392016-11-22 11:40:25 +00003343 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3344 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003345 }
3346 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003347 if (debug_map_symfile)
3348 // Set the module of the expression to the linked module
3349 // instead of the oject file so the relocated address can be
3350 // found there.
3351 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003352
Sam McCall30621392016-11-22 11:40:25 +00003353 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003354 if (is_external)
3355 scope = eValueTypeVariableGlobal;
3356 else
3357 scope = eValueTypeVariableStatic;
3358
3359 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003360 // When leaving the DWARF in the .o files on darwin, when we have a
3361 // global variable that wasn't initialized, the .o file might not
3362 // have allocated a virtual address for the global variable. In
3363 // this case it will have created a symbol for the global variable
3364 // that is undefined/data and external and the value will be the
3365 // byte size of the variable. When we do the address map in
3366 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3367 // do some magic here so we can get the correct address for our
3368 // global variable. The address for all of these entries will be
3369 // zero, and there will be an undefined symbol in this object file,
3370 // and the executable will have a matching symbol with a good
3371 // address. So here we dig up the correct address and replace it in
3372 // the location for the variable, and set the variable's symbol
3373 // context scope to be that of the main executable so the file
3374 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003375 bool linked_oso_file_addr = false;
3376 if (is_external && location_DW_OP_addr == 0) {
3377 // we have a possible uninitialized extern global
3378 ConstString const_name(mangled ? mangled : name);
3379 ObjectFile *debug_map_objfile =
3380 debug_map_symfile->GetObjectFile();
3381 if (debug_map_objfile) {
3382 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3383 if (debug_map_symtab) {
3384 Symbol *exe_symbol =
3385 debug_map_symtab->FindFirstSymbolWithNameAndType(
3386 const_name, eSymbolTypeData, Symtab::eDebugYes,
3387 Symtab::eVisibilityExtern);
3388 if (exe_symbol) {
3389 if (exe_symbol->ValueIsAddress()) {
3390 const addr_t exe_file_addr =
3391 exe_symbol->GetAddressRef().GetFileAddress();
3392 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3393 if (location.Update_DW_OP_addr(exe_file_addr)) {
3394 linked_oso_file_addr = true;
3395 symbol_context_scope = exe_symbol;
3396 }
3397 }
3398 }
3399 }
3400 }
3401 }
3402 }
3403
3404 if (!linked_oso_file_addr) {
3405 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003406 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003407 const lldb::addr_t exe_file_addr =
3408 debug_map_symfile->LinkOSOFileAddress(this,
3409 location_DW_OP_addr);
3410 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3411 // Update the file address for this variable
3412 location.Update_DW_OP_addr(exe_file_addr);
3413 } else {
3414 // Variable didn't make it into the final executable
3415 return var_sp;
3416 }
3417 }
3418 }
3419 } else {
3420 if (location_is_const_value_data)
3421 scope = eValueTypeVariableStatic;
3422 else {
3423 scope = eValueTypeVariableLocal;
3424 if (debug_map_symfile) {
3425 // We need to check for TLS addresses that we need to fixup
3426 if (location.ContainsThreadLocalStorage()) {
3427 location.LinkThreadLocalStorage(
3428 debug_map_symfile->GetObjectFile()->GetModule(),
3429 [this, debug_map_symfile](
3430 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3431 return debug_map_symfile->LinkOSOFileAddress(
3432 this, unlinked_file_addr);
3433 });
3434 scope = eValueTypeVariableThreadLocal;
3435 }
3436 }
3437 }
3438 }
3439 }
3440
Konrad Kleine248a1302019-05-23 11:14:47 +00003441 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003442 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003443 case DW_TAG_subprogram:
3444 case DW_TAG_inlined_subroutine:
3445 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003446 if (sc.function) {
3447 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3448 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003449 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003450 symbol_context_scope = sc.function;
3451 }
3452 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003453
Kate Stoneb9c1b512016-09-06 20:57:50 +00003454 default:
3455 symbol_context_scope = sc.comp_unit;
3456 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003457 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003458 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003459
Kate Stoneb9c1b512016-09-06 20:57:50 +00003460 if (symbol_context_scope) {
3461 SymbolFileTypeSP type_sp(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003462 new SymbolFileType(*this, GetUID(DIERef(type_die_form))));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003463
3464 if (const_value.Form() && type_sp && type_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00003465 location.CopyOpcodeData(
3466 const_value.Unsigned(),
3467 type_sp->GetType()->GetByteSize().getValueOr(0),
3468 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003469
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003470 var_sp = std::make_shared<Variable>(
3471 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3472 scope_ranges, &decl, location, is_external, is_artificial,
3473 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003474
3475 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3476 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003477 // Not ready to parse this variable yet. It might be a global or static
3478 // variable that is in a function scope and the function in the symbol
3479 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003480 return var_sp;
3481 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003482 }
Adrian Prantl05097242018-04-30 16:49:04 +00003483 // Cache var_sp even if NULL (the variable was just a specification or was
3484 // missing vital information to be able to be displayed in the debugger
3485 // (missing location due to optimization, etc)) so we don't re-parse this
3486 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003487 GetDIEToVariable()[die.GetDIE()] = var_sp;
3488 if (spec_die)
3489 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3490 }
3491 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003492}
3493
Kate Stoneb9c1b512016-09-06 20:57:50 +00003494DWARFDIE
3495SymbolFileDWARF::FindBlockContainingSpecification(
3496 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3497 // Give the concrete function die specified by "func_die_offset", find the
3498 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3499 // to "spec_block_die_offset"
3500 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3501 spec_block_die_offset);
3502}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003503
Kate Stoneb9c1b512016-09-06 20:57:50 +00003504DWARFDIE
3505SymbolFileDWARF::FindBlockContainingSpecification(
3506 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3507 if (die) {
3508 switch (die.Tag()) {
3509 case DW_TAG_subprogram:
3510 case DW_TAG_inlined_subroutine:
3511 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003512 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3513 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003514 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003515
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003516 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003517 spec_block_die_offset)
3518 return die;
3519 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003521
3522 // Give the concrete function die specified by "func_die_offset", find the
3523 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3524 // to "spec_block_die_offset"
3525 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3526 child_die = child_die.GetSibling()) {
3527 DWARFDIE result_die =
3528 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3529 if (result_die)
3530 return result_die;
3531 }
3532 }
3533
3534 return DWARFDIE();
3535}
3536
3537size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3538 const DWARFDIE &orig_die,
3539 const lldb::addr_t func_low_pc,
3540 bool parse_siblings, bool parse_children,
3541 VariableList *cc_variable_list) {
3542 if (!orig_die)
3543 return 0;
3544
3545 VariableListSP variable_list_sp;
3546
3547 size_t vars_added = 0;
3548 DWARFDIE die = orig_die;
3549 while (die) {
3550 dw_tag_t tag = die.Tag();
3551
3552 // Check to see if we have already parsed this variable or constant?
3553 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3554 if (var_sp) {
3555 if (cc_variable_list)
3556 cc_variable_list->AddVariableIfUnique(var_sp);
3557 } else {
3558 // We haven't already parsed it, lets do that now.
3559 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3560 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003561 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003562 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3563 dw_tag_t parent_tag = sc_parent_die.Tag();
3564 switch (parent_tag) {
3565 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003566 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003567 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003569 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003570 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003571 }
3572 } else {
3573 GetObjectFile()->GetModule()->ReportError(
3574 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3575 "symbol context for 0x%8.8" PRIx64
3576 " %s.\n",
3577 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3578 orig_die.GetID(), orig_die.GetTagAsCString());
3579 }
3580 break;
3581
3582 case DW_TAG_subprogram:
3583 case DW_TAG_inlined_subroutine:
3584 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003585 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003586 // Check to see if we already have parsed the variables for the
3587 // given scope
3588
3589 Block *block = sc.function->GetBlock(true).FindBlockByID(
3590 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003591 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003592 // This must be a specification or abstract origin with a
3593 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003594 // to find the concrete block so we can correctly add the
3595 // variable to it
3596 const DWARFDIE concrete_block_die =
3597 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003598 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003599 sc_parent_die.GetOffset());
3600 if (concrete_block_die)
3601 block = sc.function->GetBlock(true).FindBlockByID(
3602 concrete_block_die.GetID());
3603 }
3604
Konrad Kleine248a1302019-05-23 11:14:47 +00003605 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003606 const bool can_create = false;
3607 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003608 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003609 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 block->SetVariableList(variable_list_sp);
3611 }
3612 }
3613 }
3614 break;
3615
3616 default:
3617 GetObjectFile()->GetModule()->ReportError(
3618 "didn't find appropriate parent DIE for variable list for "
3619 "0x%8.8" PRIx64 " %s.\n",
3620 orig_die.GetID(), orig_die.GetTagAsCString());
3621 break;
3622 }
3623 }
3624
3625 if (variable_list_sp) {
3626 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3627 if (var_sp) {
3628 variable_list_sp->AddVariableIfUnique(var_sp);
3629 if (cc_variable_list)
3630 cc_variable_list->AddVariableIfUnique(var_sp);
3631 ++vars_added;
3632 }
3633 }
3634 }
3635 }
3636
Konrad Kleine248a1302019-05-23 11:14:47 +00003637 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003638
3639 if (!skip_children && parse_children && die.HasChildren()) {
3640 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3641 true, cc_variable_list);
3642 }
3643
3644 if (parse_siblings)
3645 die = die.GetSibling();
3646 else
3647 die.Clear();
3648 }
3649 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003650}
3651
Vedant Kumar4b36f792018-10-05 23:23:15 +00003652/// Collect call graph edges present in a function DIE.
3653static std::vector<lldb_private::CallEdge>
3654CollectCallEdges(DWARFDIE function_die) {
3655 // Check if the function has a supported call site-related attribute.
3656 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3657 uint64_t has_call_edges =
3658 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3659 if (!has_call_edges)
3660 return {};
3661
3662 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3663 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3664 function_die.GetPubname());
3665
3666 // Scan the DIE for TAG_call_site entries.
3667 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3668 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3669 // For now, assume that all entries are nested directly under the subprogram
3670 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3671 std::vector<CallEdge> call_edges;
3672 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3673 child = child.GetSibling()) {
3674 if (child.Tag() != DW_TAG_call_site)
3675 continue;
3676
3677 // Extract DW_AT_call_origin (the call target's DIE).
3678 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3679 if (!call_origin.IsValid()) {
3680 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3681 function_die.GetPubname());
3682 continue;
3683 }
3684
3685 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3686 // available. It should only ever be unavailable for tail call edges, in
3687 // which case use LLDB_INVALID_ADDRESS.
3688 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3689 LLDB_INVALID_ADDRESS);
3690
3691 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3692 call_origin.GetPubname(), return_pc);
3693 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3694 }
3695 return call_edges;
3696}
3697
3698std::vector<lldb_private::CallEdge>
3699SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003700 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003701 if (func_die.IsValid())
3702 return CollectCallEdges(func_die);
3703 return {};
3704}
3705
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003706// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003707ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3708
3709uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3710
Pavel Labath9337b412018-06-06 11:35:23 +00003711void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003712
Zachary Turner49110232018-11-05 17:40:28 +00003713void SymbolFileDWARF::DumpClangAST(Stream &s) {
3714 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3715 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3716 if (!clang)
3717 return;
3718 clang->Dump(s);
3719}
3720
Kate Stoneb9c1b512016-09-06 20:57:50 +00003721SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003722 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003723 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3724 if (module_sp) {
3725 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3726 if (sym_vendor)
3727 m_debug_map_symfile =
3728 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003729 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003730 }
3731 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003732}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003733
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003734DWARFExpression::LocationListFormat
3735SymbolFileDWARF::GetLocationListFormat() const {
3736 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003737 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003738 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003739}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003740
3741SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3742 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003743 ModuleSpec module_spec;
3744 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3745 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003746 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003747
3748 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3749 FileSpec dwp_filespec =
3750 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003751 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003752 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3753 dwp_filespec);
3754 }
3755 });
3756 return m_dwp_symfile.get();
3757}