blob: 463af1af9a1cf2cbdf285adb8efeac4a8774409a [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
Pavel Labath519ef6a2019-05-27 10:10:59 +0000643lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 CompUnitSP cu_sp;
645 if (dwarf_cu) {
646 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
647 if (comp_unit) {
648 // We already parsed this compile unit, had out a shared pointer to it
649 cu_sp = comp_unit->shared_from_this();
650 } else {
651 if (dwarf_cu->GetSymbolFileDWARF() != this) {
Pavel Labath519ef6a2019-05-27 10:10:59 +0000652 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
654 // Let the debug map create the compile unit
655 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
656 dwarf_cu->SetUserData(cu_sp.get());
657 } else {
658 ModuleSP module_sp(m_obj_file->GetModule());
659 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000660 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 if (cu_die) {
Pavel Labath7d36d722019-01-16 12:30:41 +0000662 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (cu_file_spec) {
664 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000665 // resolve the file. This can be expensive e.g. when the source
Pavel Labath7d36d722019-01-16 12:30:41 +0000666 // files are NFS mounted.
667 cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668
669 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000670 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000672 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 }
674
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000675 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
677
678 bool is_optimized = dwarf_cu->GetIsOptimized();
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000679 cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000681 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
Pavel Labath519ef6a2019-05-27 10:10:59 +0000682
683 // If we just created a compile unit with an invalid file spec,
684 // try and get the first entry in the supports files from the
685 // line table as that should be the compile unit.
686 if (!cu_file_spec) {
687 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
688 if (cu_file_spec) {
689 (FileSpec &)(*cu_sp) = cu_file_spec;
690 // Also fix the invalid file spec which was copied from the
691 // compile unit.
692 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 }
Pavel Labath519ef6a2019-05-27 10:10:59 +0000695
696 dwarf_cu->SetUserData(cu_sp.get());
697
698 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
699 dwarf_cu->GetID(), cu_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700 }
701 }
702 }
703 }
704 }
705 return cu_sp;
706}
707
708uint32_t SymbolFileDWARF::GetNumCompileUnits() {
709 DWARFDebugInfo *info = DebugInfo();
710 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +0000711 return info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 return 0;
713}
714
715CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000716 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 CompUnitSP cu_sp;
718 DWARFDebugInfo *info = DebugInfo();
719 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000720 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 if (dwarf_cu)
Pavel Labath519ef6a2019-05-27 10:10:59 +0000722 cu_sp = ParseCompileUnit(dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 }
724 return cu_sp;
725}
726
Zachary Turner863f8c12019-01-11 18:03:20 +0000727Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
728 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000729 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 if (die.IsValid()) {
731 TypeSystem *type_system =
732 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
733
734 if (type_system) {
735 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
736 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000737 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 }
739 }
740 return nullptr;
741}
742
743bool SymbolFileDWARF::FixupAddress(Address &addr) {
744 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
745 if (debug_map_symfile) {
746 return debug_map_symfile->LinkOSOAddress(addr);
747 }
748 // This is a normal DWARF file, no address fixups need to happen
749 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000750}
Zachary Turner863f8c12019-01-11 18:03:20 +0000751lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000752 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000753 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 if (dwarf_cu)
755 return dwarf_cu->GetLanguageType();
756 else
757 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000758}
759
Zachary Turner863f8c12019-01-11 18:03:20 +0000760size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000761 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000762 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000763 if (!dwarf_cu)
764 return 0;
765
766 size_t functions_added = 0;
767 std::vector<DWARFDIE> function_dies;
768 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
769 for (const DWARFDIE &die : function_dies) {
770 if (comp_unit.FindFunctionByUID(die.GetID()))
771 continue;
772 if (ParseFunction(comp_unit, die))
773 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000775 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777}
778
Zachary Turner863f8c12019-01-11 18:03:20 +0000779bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
780 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000781 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000782 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000783 if (dwarf_cu) {
784 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000786 if (cu_die) {
787 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
788 DW_AT_stmt_list, DW_INVALID_OFFSET);
789 if (stmt_list != DW_INVALID_OFFSET) {
790 // All file indexes in DWARF are one based and a file of index zero is
791 // supposed to be the compile unit itself.
792 support_files.Append(comp_unit);
793 return DWARFDebugLine::ParseSupportFiles(
Pavel Labath0261b942019-05-20 08:38:47 +0000794 comp_unit.GetModule(), m_context.getOrLoadLineData(), stmt_list,
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000795 support_files, dwarf_cu);
796 }
797 }
798 }
799 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800}
801
Zachary Turner863f8c12019-01-11 18:03:20 +0000802bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000803 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000804 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 if (dwarf_cu)
806 return dwarf_cu->GetIsOptimized();
807 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000808}
809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810bool SymbolFileDWARF::ParseImportedModules(
811 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000812 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000813 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000815 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000816 if (!dwarf_cu)
817 return false;
818 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
819 sc.comp_unit->GetLanguage()))
820 return false;
821 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000823 const DWARFDIE die = dwarf_cu->DIE();
824 if (!die)
825 return false;
826
827 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
828 child_die = child_die.GetSibling()) {
829 if (child_die.Tag() != DW_TAG_imported_declaration)
830 continue;
831
832 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
833 if (module_die.Tag() != DW_TAG_module)
834 continue;
835
836 if (const char *name =
837 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
838 SourceModule module;
839 module.path.push_back(ConstString(name));
840
841 DWARFDIE parent_die = module_die;
842 while ((parent_die = parent_die.GetParent())) {
843 if (parent_die.Tag() != DW_TAG_module)
844 break;
845 if (const char *name =
846 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
847 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000849 std::reverse(module.path.begin(), module.path.end());
850 if (const char *include_path = module_die.GetAttributeValueAsString(
851 DW_AT_LLVM_include_path, nullptr))
852 module.search_path = ConstString(include_path);
853 if (const char *sysroot = module_die.GetAttributeValueAsString(
854 DW_AT_LLVM_isysroot, nullptr))
855 module.sysroot = ConstString(sysroot);
856 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000857 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000859 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000860}
861
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862struct ParseDWARFLineTableCallbackInfo {
863 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000864 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866};
867
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869static void ParseDWARFLineTableCallback(dw_offset_t offset,
870 const DWARFDebugLine::State &state,
871 void *userData) {
872 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
873 // Just started parsing the line table
874 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
875 // Done parsing line table, nothing to do for the cleanup
876 } else {
877 ParseDWARFLineTableCallbackInfo *info =
878 (ParseDWARFLineTableCallbackInfo *)userData;
879 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880
Adrian Prantl05097242018-04-30 16:49:04 +0000881 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000882 if (!info->sequence_up) {
883 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
884 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000887 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 state.column, state.file, state.is_stmt, state.basic_block,
889 state.prologue_end, state.epilogue_begin, state.end_sequence);
890 if (state.end_sequence) {
891 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000892 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000894 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 }
896 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897}
898
Zachary Turner863f8c12019-01-11 18:03:20 +0000899bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000900 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +0000901 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903
Zachary Turner863f8c12019-01-11 18:03:20 +0000904 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000905 if (dwarf_cu) {
906 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
907 if (dwarf_cu_die) {
908 const dw_offset_t cu_line_offset =
909 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
910 DW_INVALID_OFFSET);
911 if (cu_line_offset != DW_INVALID_OFFSET) {
912 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
913 if (line_table_up) {
914 ParseDWARFLineTableCallbackInfo info;
915 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000916
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000917 /*
918 * MIPS:
919 * The SymbolContext may not have a valid target, thus we may not be
920 * able
921 * to call Address::GetOpcodeLoadAddress() which would clear the bit
922 * #0
923 * for MIPS. Use ArchSpec to clear the bit #0.
924 */
925 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
926 case llvm::Triple::mips:
927 case llvm::Triple::mipsel:
928 case llvm::Triple::mips64:
929 case llvm::Triple::mips64el:
930 info.addr_mask = ~((lldb::addr_t)1);
931 break;
932 default:
933 info.addr_mask = ~((lldb::addr_t)0);
934 break;
935 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000936
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000937 lldb::offset_t offset = cu_line_offset;
Pavel Labath0261b942019-05-20 08:38:47 +0000938 DWARFDebugLine::ParseStatementTable(
939 m_context.getOrLoadLineData(), &offset,
940 ParseDWARFLineTableCallback, &info, dwarf_cu);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000941 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
942 if (debug_map_symfile) {
943 // We have an object file that has a line table with addresses that
944 // are not linked. We need to link the line table and convert the
945 // addresses that are relative to the .o file into addresses for
946 // the main executable.
947 comp_unit.SetLineTable(
948 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
949 } else {
950 comp_unit.SetLineTable(line_table_up.release());
951 return true;
952 }
953 }
954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 }
957 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958}
959
Siva Chandrad8335e92015-12-16 00:22:08 +0000960lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
962 auto iter = m_debug_macros_map.find(*offset);
963 if (iter != m_debug_macros_map.end())
964 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +0000965
Pavel Labath0261b942019-05-20 08:38:47 +0000966 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000967 if (debug_macro_data.GetByteSize() == 0)
968 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
971 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +0000972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000974 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +0000975 DWARFDebugMacroEntry::ReadMacroEntries(
976 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
977 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +0000978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +0000980}
981
Zachary Turner863f8c12019-01-11 18:03:20 +0000982bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000983 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +0000984
Zachary Turner863f8c12019-01-11 18:03:20 +0000985 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +0000987 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988
Jan Kratochvil93afb052018-05-24 20:51:13 +0000989 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 if (!dwarf_cu_die)
991 return false;
992
993 lldb::offset_t sect_offset =
994 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
995 if (sect_offset == DW_INVALID_OFFSET)
996 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
997 DW_INVALID_OFFSET);
998 if (sect_offset == DW_INVALID_OFFSET)
999 return false;
1000
Zachary Turner863f8c12019-01-11 18:03:20 +00001001 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002
1003 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001004}
1005
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001006size_t SymbolFileDWARF::ParseBlocksRecursive(
1007 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1008 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 size_t blocks_added = 0;
1010 DWARFDIE die = orig_die;
1011 while (die) {
1012 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 switch (tag) {
1015 case DW_TAG_inlined_subroutine:
1016 case DW_TAG_subprogram:
1017 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001018 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001020 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1021 // inlined functions. These will be parsed on their own as separate
1022 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023
1024 if (depth > 0)
1025 break;
1026
1027 block = parent_block;
1028 } else {
1029 BlockSP block_sp(new Block(die.GetID()));
1030 parent_block->AddChild(block_sp);
1031 block = block_sp.get();
1032 }
1033 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001034 const char *name = nullptr;
1035 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036
1037 int decl_file = 0;
1038 int decl_line = 0;
1039 int decl_column = 0;
1040 int call_file = 0;
1041 int call_line = 0;
1042 int call_column = 0;
1043 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1044 decl_line, decl_column, call_file, call_line,
1045 call_column, nullptr)) {
1046 if (tag == DW_TAG_subprogram) {
1047 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1048 subprogram_low_pc = ranges.GetMinRangeBase(0);
1049 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001050 // We get called here for inlined subroutines in two ways. The first
1051 // time is when we are making the Function object for this inlined
1052 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001054 // need to adjust the containing address. The second time is when we
1055 // are parsing the blocks inside the function that contains the
1056 // inlined concrete instance. Since these will be blocks inside the
1057 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1059 subprogram_low_pc = ranges.GetMinRangeBase(0);
1060 }
1061 }
1062
1063 const size_t num_ranges = ranges.GetSize();
1064 for (size_t i = 0; i < num_ranges; ++i) {
1065 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1066 const addr_t range_base = range.GetRangeBase();
1067 if (range_base >= subprogram_low_pc)
1068 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1069 range.GetByteSize()));
1070 else {
1071 GetObjectFile()->GetModule()->ReportError(
1072 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1073 ") which has a base that is less than the function's low PC "
1074 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1075 "start of this error message",
1076 block->GetID(), range_base, range.GetRangeEnd(),
1077 subprogram_low_pc);
1078 }
1079 }
1080 block->FinalizeRanges();
1081
1082 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001083 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001084 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001086 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001087 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 decl_line, decl_column));
1089
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001090 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001092 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001093 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 call_line, call_column));
1095
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001096 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1097 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 }
1099
1100 ++blocks_added;
1101
1102 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001103 blocks_added +=
1104 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1105 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 }
1107 }
1108 } break;
1109 default:
1110 break;
1111 }
1112
Adrian Prantl05097242018-04-30 16:49:04 +00001113 // Only parse siblings of the block if we are not at depth zero. A depth of
1114 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1115 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116
1117 if (depth == 0)
1118 die.Clear();
1119 else
1120 die = die.GetSibling();
1121 }
1122 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001123}
1124
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1126 if (parent_die) {
1127 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1128 die = die.GetSibling()) {
1129 dw_tag_t tag = die.Tag();
1130 bool check_virtuality = false;
1131 switch (tag) {
1132 case DW_TAG_inheritance:
1133 case DW_TAG_subprogram:
1134 check_virtuality = true;
1135 break;
1136 default:
1137 break;
1138 }
1139 if (check_virtuality) {
1140 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1141 return true;
1142 }
1143 }
1144 }
1145 return false;
1146}
1147
1148void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1149 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1150 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1151 std::vector<DWARFDIE> decl_ctx_die_list =
1152 ast_parser->GetDIEForDeclContext(decl_ctx);
1153
1154 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1155 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1156 decl = decl.GetSibling())
1157 ast_parser->GetDeclForUIDFromDWARF(decl);
1158}
1159
Pavel Labath2841e6e2019-05-08 11:43:05 +00001160SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001161 // This method can be called without going through the symbol vendor so we
1162 // need to lock the module.
1163 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001164 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1165 // must make sure we use the correct DWARF file when resolving things. On
1166 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1167 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1168 // references to other DWARF objects and we must be ready to receive a
1169 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001171 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1172 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labathf4014e12019-05-16 11:07:58 +00001174 return {dwarf, {DIERef::Section::DebugInfo, DW_INVALID_OFFSET, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001175 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001176 DIERef::Section section =
1177 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1178 uint32_t dwarf_id = uid >> 32 & 0x7fffffff;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001179 dw_offset_t die_offset = uid;
1180
1181 if (die_offset == DW_INVALID_OFFSET)
1182 return {nullptr, DIERef()};
1183
1184 SymbolFileDWARF *dwarf = this;
1185 if (DebugInfo()) {
1186 if (DWARFUnit *unit = DebugInfo()->GetUnitAtIndex(dwarf_id)) {
1187 if (unit->GetDwoSymbolFile())
1188 dwarf = unit->GetDwoSymbolFile();
1189 }
1190 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001191 return {dwarf, {section, DW_INVALID_OFFSET, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001192}
1193
1194DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001195SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001196 // This method can be called without going through the symbol vendor so we
1197 // need to lock the module.
1198 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001199
1200 DecodedUID decoded = DecodeUID(uid);
1201
1202 if (decoded.dwarf)
1203 return decoded.dwarf->GetDIE(decoded.ref);
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001206}
1207
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001209 // This method can be called without going through the symbol vendor so we
1210 // need to lock the module.
1211 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001212 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001213 // SymbolFileDWARF::GetDIE(). See comments inside the
1214 // SymbolFileDWARF::GetDIE() for details.
1215 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 return die.GetDecl();
1217 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001218}
1219
Greg Clayton99558cc42015-08-24 23:46:31 +00001220CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001222 // This method can be called without going through the symbol vendor so we
1223 // need to lock the module.
1224 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001225 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001226 // SymbolFileDWARF::GetDIE(). See comments inside the
1227 // SymbolFileDWARF::GetDIE() for details.
1228 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 return die.GetDeclContext();
1230 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001231}
1232
Greg Clayton99558cc42015-08-24 23:46:31 +00001233CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001235 // This method can be called without going through the symbol vendor so we
1236 // need to lock the module.
1237 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001238 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001239 // SymbolFileDWARF::GetDIE(). See comments inside the
1240 // SymbolFileDWARF::GetDIE() for details.
1241 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 return die.GetContainingDeclContext();
1243 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244}
1245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001247 // This method can be called without going through the symbol vendor so we
1248 // need to lock the module.
1249 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001250 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001251 // SymbolFileDWARF::GetDIE(). See comments inside the
1252 // SymbolFileDWARF::GetDIE() for details.
1253 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 return type_die.ResolveType();
1255 else
1256 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257}
1258
Adrian Prantleca07c52018-11-05 20:49:07 +00001259llvm::Optional<SymbolFile::ArrayInfo>
1260SymbolFileDWARF::GetDynamicArrayInfoForUID(
1261 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1262 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001263 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001264 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1265 else
1266 return llvm::None;
1267}
1268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1270 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001271}
1272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1274 bool assert_not_being_parsed) {
1275 if (die) {
1276 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1277 if (log)
1278 GetObjectFile()->GetModule()->LogMessage(
1279 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1280 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001281
Adrian Prantl05097242018-04-30 16:49:04 +00001282 // We might be coming in in the middle of a type tree (a class within a
1283 // class, an enum within a class), so parse any needed parent DIEs before
1284 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1286 if (decl_ctx_die) {
1287 if (log) {
1288 switch (decl_ctx_die.Tag()) {
1289 case DW_TAG_structure_type:
1290 case DW_TAG_union_type:
1291 case DW_TAG_class_type: {
1292 // Get the type, which could be a forward declaration
1293 if (log)
1294 GetObjectFile()->GetModule()->LogMessage(
1295 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1296 "resolve parent forward type for 0x%8.8x",
1297 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1298 decl_ctx_die.GetOffset());
1299 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 default:
1302 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001303 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001305 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 return ResolveType(die);
1307 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001308 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001309}
1310
Greg Clayton6beaaa62011-01-17 03:46:26 +00001311// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001312// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1313// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314bool SymbolFileDWARF::HasForwardDeclForClangType(
1315 const CompilerType &compiler_type) {
1316 CompilerType compiler_type_no_qualifiers =
1317 ClangUtil::RemoveFastQualifiers(compiler_type);
1318 if (GetForwardDeclClangTypeToDie().count(
1319 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1320 return true;
1321 }
1322 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 ClangASTContext *clang_type_system =
1325 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1326 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001327 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 DWARFASTParserClang *ast_parser =
1329 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1330 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001331}
1332
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001334 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001335
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 ClangASTContext *clang_type_system =
1337 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1338 if (clang_type_system) {
1339 DWARFASTParserClang *ast_parser =
1340 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1341 if (ast_parser &&
1342 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1343 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1344 }
Jim Inghamc3549282012-01-11 02:21:12 +00001345
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 // We have a struct/union/class/enum that needs to be fully resolved.
1347 CompilerType compiler_type_no_qualifiers =
1348 ClangUtil::RemoveFastQualifiers(compiler_type);
1349 auto die_it = GetForwardDeclClangTypeToDie().find(
1350 compiler_type_no_qualifiers.GetOpaqueQualType());
1351 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1352 // We have already resolved this type...
1353 return true;
1354 }
1355
1356 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1357 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001358 // Once we start resolving this type, remove it from the forward
1359 // declaration map in case anyone child members or other types require this
1360 // type to get resolved. The type will get resolved when all of the calls
1361 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 GetForwardDeclClangTypeToDie().erase(die_it);
1363
1364 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1365
1366 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1367 DWARF_LOG_TYPE_COMPLETION));
1368 if (log)
1369 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1370 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1371 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1372 type->GetName().AsCString());
1373 assert(compiler_type);
1374 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1375 if (dwarf_ast)
1376 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1377 }
1378 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001379}
1380
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1382 bool assert_not_being_parsed,
1383 bool resolve_function_context) {
1384 if (die) {
1385 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1386
1387 if (assert_not_being_parsed) {
1388 if (type != DIE_IS_BEING_PARSED)
1389 return type;
1390
1391 GetObjectFile()->GetModule()->ReportError(
1392 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1393 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1394
1395 } else
1396 return type;
1397 }
1398 return nullptr;
1399}
1400
Pavel Labath519ef6a2019-05-27 10:10:59 +00001401CompileUnit *SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 // Check if the symbol vendor already knows about this compile unit?
Konrad Kleine248a1302019-05-23 11:14:47 +00001403 if (dwarf_cu->GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001404 // The symbol vendor doesn't know about this compile unit, we need to parse
1405 // and add it to the symbol vendor object.
Pavel Labath519ef6a2019-05-27 10:10:59 +00001406 return ParseCompileUnit(dwarf_cu).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407 }
1408 return (CompileUnit *)dwarf_cu->GetUserData();
1409}
1410
1411size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1412 DIEArray &method_die_offsets) {
1413 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001414 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 return method_die_offsets.size();
1416}
1417
1418bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1419 sc.Clear(false);
1420
1421 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 // Check if the symbol vendor already knows about this compile unit?
Pavel Labath519ef6a2019-05-27 10:10:59 +00001423 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424
1425 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001426 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001427 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428
1429 if (sc.function) {
1430 sc.module_sp = sc.function->CalculateSymbolContextModule();
1431 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 }
1434
1435 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436}
1437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1439 UpdateExternalModuleListIfNeeded();
1440 const auto &pos = m_external_type_modules.find(name);
1441 if (pos != m_external_type_modules.end())
1442 return pos->second;
1443 else
1444 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001445}
1446
Greg Clayton2f869fe2016-03-30 20:14:35 +00001447DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1449 DWARFDebugInfo *debug_info = DebugInfo();
1450 if (debug_info)
1451 return debug_info->GetDIE(die_ref);
1452 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001453 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001454}
1455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456std::unique_ptr<SymbolFileDWARFDwo>
1457SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001458 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001460 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 // info files for modules.
1462 if (GetDebugMapSymfile())
1463 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001464
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001465 const char *dwo_name =
1466 cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467 if (!dwo_name)
1468 return nullptr;
1469
Tamas Berghammer963ce482017-08-25 13:56:14 +00001470 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1471 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001472 uint64_t dwo_id =
1473 cu_die.GetAttributeValueAsUnsigned(&dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001474 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1475 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1476 if (dwo_symfile)
1477 return dwo_symfile;
1478 }
1479
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001480 FileSpec dwo_file(dwo_name);
1481 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001483 const char *comp_dir =
1484 cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 if (!comp_dir)
1486 return nullptr;
1487
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001488 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1489 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 dwo_file.AppendPathComponent(dwo_name);
1491 }
1492
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001493 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 return nullptr;
1495
1496 const lldb::offset_t file_offset = 0;
1497 DataBufferSP dwo_file_data_sp;
1498 lldb::offset_t dwo_file_data_offset = 0;
1499 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1500 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001501 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1502 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 if (dwo_obj_file == nullptr)
1504 return nullptr;
1505
1506 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001507}
1508
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1510 if (m_fetched_external_modules)
1511 return;
1512 m_fetched_external_modules = true;
1513
1514 DWARFDebugInfo *debug_info = DebugInfo();
1515
1516 const uint32_t num_compile_units = GetNumCompileUnits();
1517 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001518 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519
Jan Kratochvil93afb052018-05-24 20:51:13 +00001520 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001521 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1523
1524 if (name) {
1525 ConstString const_name(name);
1526 if (m_external_type_modules.find(const_name) ==
1527 m_external_type_modules.end()) {
1528 ModuleSP module_sp;
1529 const char *dwo_path =
1530 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1531 if (dwo_path) {
1532 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001533 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001534 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1536 const char *comp_dir =
1537 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1538 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001539 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001540 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001541 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1543 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001544 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 dwo_module_spec.GetArchitecture() =
1546 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001547
Adrian Prantl05097242018-04-30 16:49:04 +00001548 // When LLDB loads "external" modules it looks at the presence of
1549 // DW_AT_GNU_dwo_name. However, when the already created module
1550 // (corresponding to .dwo itself) is being processed, it will see
1551 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1552 // dwo file) and will try to call ModuleList::GetSharedModule
1553 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1554 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1555 // DW_AT_comp_dir. In this case the method
1556 // ModuleList::GetSharedModule will fail and the warning will be
1557 // printed. However, as one can notice in this case we don't
1558 // actually need to try to load the already loaded module
1559 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001560 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001561 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1562 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1563 continue;
1564 }
1565
Zachary Turner97206d52017-05-12 04:51:55 +00001566 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001567 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 if (!module_sp) {
1569 GetObjectFile()->GetModule()->ReportWarning(
1570 "0x%8.8x: unable to locate module needed for external types: "
1571 "%s\nerror: %s\nDebugging will be degraded due to missing "
1572 "types. Rebuilding your project will regenerate the needed "
1573 "module files.",
1574 die.GetOffset(),
1575 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1576 error.AsCString("unknown error"));
1577 }
1578 }
1579 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 }
1584}
1585
1586SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001587 if (!m_global_aranges_up) {
1588 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589
1590 ModuleSP module_sp = GetObjectFile()->GetModule();
1591 if (module_sp) {
1592 const size_t num_cus = module_sp->GetNumCompileUnits();
1593 for (size_t i = 0; i < num_cus; ++i) {
1594 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1595 if (cu_sp) {
1596 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1597 if (globals_sp) {
1598 const size_t num_globals = globals_sp->GetSize();
1599 for (size_t g = 0; g < num_globals; ++g) {
1600 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1601 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1602 const DWARFExpression &location = var_sp->LocationExpression();
1603 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001604 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001605 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1606 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607 if (location_result.GetValueType() ==
1608 Value::eValueTypeFileAddress) {
1609 lldb::addr_t file_addr =
1610 location_result.GetScalar().ULongLong();
1611 lldb::addr_t byte_size = 1;
1612 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001613 byte_size =
1614 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001615 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 file_addr, byte_size, var_sp.get()));
1617 }
1618 }
1619 }
1620 }
1621 }
1622 }
1623 }
1624 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001625 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001627 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628}
1629
1630uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001631 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001633 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1634 Timer scoped_timer(func_cat,
1635 "SymbolFileDWARF::"
1636 "ResolveSymbolContext (so_addr = { "
1637 "section = %p, offset = 0x%" PRIx64
1638 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001639 static_cast<void *>(so_addr.GetSection().get()),
1640 so_addr.GetOffset(), resolve_scope);
1641 uint32_t resolved = 0;
1642 if (resolve_scope &
1643 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1644 eSymbolContextLineEntry | eSymbolContextVariable)) {
1645 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1646
1647 DWARFDebugInfo *debug_info = DebugInfo();
1648 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001649 llvm::Expected<DWARFDebugAranges &> aranges =
1650 debug_info->GetCompileUnitAranges();
1651 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001652 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001653 LLDB_LOG_ERROR(log, aranges.takeError(),
1654 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1655 "aranges. {0}");
1656 return 0;
1657 }
1658
1659 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001661 // Global variables are not in the compile unit address ranges. The
1662 // only way to currently find global variables is to iterate over the
1663 // .debug_pubnames or the __apple_names table and find all items in
1664 // there that point to DW_TAG_variable DIEs and then find the address
1665 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666 if (resolve_scope & eSymbolContextVariable) {
1667 GlobalVariableMap &map = GetGlobalAranges();
1668 const GlobalVariableMap::Entry *entry =
1669 map.FindEntryThatContains(file_vm_addr);
1670 if (entry && entry->data) {
1671 Variable *variable = entry->data;
1672 SymbolContextScope *scc = variable->GetSymbolContextScope();
1673 if (scc) {
1674 scc->CalculateSymbolContext(&sc);
1675 sc.variable = variable;
1676 }
1677 return sc.GetResolvedMask();
1678 }
1679 }
1680 } else {
1681 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathf4014e12019-05-16 11:07:58 +00001682 DWARFUnit *dwarf_cu = debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1683 cu_offset, &cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 if (dwarf_cu) {
Pavel Labath519ef6a2019-05-27 10:10:59 +00001685 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 if (sc.comp_unit) {
1687 resolved |= eSymbolContextCompUnit;
1688
1689 bool force_check_line_table = false;
1690 if (resolve_scope &
1691 (eSymbolContextFunction | eSymbolContextBlock)) {
1692 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1693 DWARFDIE block_die;
1694 if (function_die) {
1695 sc.function =
1696 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001697 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001698 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699
1700 if (sc.function && (resolve_scope & eSymbolContextBlock))
1701 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1702 } else {
1703 // We might have had a compile unit that had discontiguous
1704 // address ranges where the gaps are symbols that don't have
1705 // any debug info. Discontiguous compile unit address ranges
1706 // should only happen when there aren't other functions from
1707 // other compile units in these gaps. This helps keep the size
1708 // of the aranges down.
1709 force_check_line_table = true;
1710 }
1711
Konrad Kleine248a1302019-05-23 11:14:47 +00001712 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713 resolved |= eSymbolContextFunction;
1714
1715 if (resolve_scope & eSymbolContextBlock) {
1716 Block &block = sc.function->GetBlock(true);
1717
1718 if (block_die)
1719 sc.block = block.FindBlockByID(block_die.GetID());
1720 else
1721 sc.block = block.FindBlockByID(function_die.GetID());
1722 if (sc.block)
1723 resolved |= eSymbolContextBlock;
1724 }
1725 }
1726 }
1727
1728 if ((resolve_scope & eSymbolContextLineEntry) ||
1729 force_check_line_table) {
1730 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001731 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001733 // terms of this debug file if there is no debug map, or it
1734 // will be an address in the .o file which needs to be fixed up
1735 // to be in terms of the debug map executable. Either way,
1736 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737 Address exe_so_addr(so_addr);
1738 if (FixupAddress(exe_so_addr)) {
1739 if (line_table->FindLineEntryByAddress(exe_so_addr,
1740 sc.line_entry)) {
1741 resolved |= eSymbolContextLineEntry;
1742 }
1743 }
1744 }
1745 }
1746
1747 if (force_check_line_table &&
1748 !(resolved & eSymbolContextLineEntry)) {
1749 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001750 // address ranges where the gaps are symbols that don't have any
1751 // debug info. Discontiguous compile unit address ranges should
1752 // only happen when there aren't other functions from other
1753 // compile units in these gaps. This helps keep the size of the
1754 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001755 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756 resolved &= ~eSymbolContextCompUnit;
1757 }
1758 } else {
1759 GetObjectFile()->GetModule()->ReportWarning(
1760 "0x%8.8x: compile unit %u failed to create a valid "
1761 "lldb_private::CompileUnit class.",
1762 cu_offset, cu_idx);
1763 }
1764 }
1765 }
1766 }
1767 }
1768 return resolved;
1769}
1770
1771uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1772 uint32_t line,
1773 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001774 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 SymbolContextList &sc_list) {
1776 const uint32_t prev_size = sc_list.GetSize();
1777 if (resolve_scope & eSymbolContextCompUnit) {
1778 DWARFDebugInfo *debug_info = DebugInfo();
1779 if (debug_info) {
1780 uint32_t cu_idx;
Konrad Kleine248a1302019-05-23 11:14:47 +00001781 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782
Konrad Kleine248a1302019-05-23 11:14:47 +00001783 for (cu_idx = 0;
1784 (dwarf_cu = debug_info->GetUnitAtIndex(cu_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 ++cu_idx) {
Pavel Labath519ef6a2019-05-27 10:10:59 +00001786 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 const bool full_match = (bool)file_spec.GetDirectory();
1788 bool file_spec_matches_cu_file_spec =
Konrad Kleine248a1302019-05-23 11:14:47 +00001789 dc_cu != nullptr && FileSpec::Equal(file_spec, *dc_cu, full_match);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001790 if (check_inlines || file_spec_matches_cu_file_spec) {
1791 SymbolContext sc(m_obj_file->GetModule());
Pavel Labath519ef6a2019-05-27 10:10:59 +00001792 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001793 if (sc.comp_unit) {
1794 uint32_t file_idx = UINT32_MAX;
1795
Adrian Prantl05097242018-04-30 16:49:04 +00001796 // If we are looking for inline functions only and we don't find it
1797 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001798 if (check_inlines) {
1799 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1800 1, file_spec, true);
1801 if (file_idx == UINT32_MAX)
1802 continue;
1803 }
1804
1805 if (line != 0) {
1806 LineTable *line_table = sc.comp_unit->GetLineTable();
1807
Konrad Kleine248a1302019-05-23 11:14:47 +00001808 if (line_table != nullptr && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001809 // We will have already looked up the file index if we are
1810 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 if (!check_inlines)
1812 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1813 1, file_spec, true);
1814
1815 if (file_idx != UINT32_MAX) {
1816 uint32_t found_line;
1817 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1818 0, file_idx, line, false, &sc.line_entry);
1819 found_line = sc.line_entry.line;
1820
1821 while (line_idx != UINT32_MAX) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001822 sc.function = nullptr;
1823 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 if (resolve_scope &
1825 (eSymbolContextFunction | eSymbolContextBlock)) {
1826 const lldb::addr_t file_vm_addr =
1827 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1828 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1829 DWARFDIE function_die =
1830 dwarf_cu->LookupAddress(file_vm_addr);
1831 DWARFDIE block_die;
1832 if (function_die) {
1833 sc.function =
1834 sc.comp_unit
1835 ->FindFunctionByUID(function_die.GetID())
1836 .get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001837 if (sc.function == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001838 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001839 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840
1841 if (sc.function &&
1842 (resolve_scope & eSymbolContextBlock))
1843 block_die =
1844 function_die.LookupDeepestBlock(file_vm_addr);
1845 }
1846
Konrad Kleine248a1302019-05-23 11:14:47 +00001847 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 Block &block = sc.function->GetBlock(true);
1849
1850 if (block_die)
1851 sc.block = block.FindBlockByID(block_die.GetID());
1852 else if (function_die)
1853 sc.block =
1854 block.FindBlockByID(function_die.GetID());
1855 }
1856 }
1857 }
1858
1859 sc_list.Append(sc);
1860 line_idx = line_table->FindLineEntryIndexByFileIndex(
1861 line_idx + 1, file_idx, found_line, true,
1862 &sc.line_entry);
1863 }
1864 }
1865 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1866 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001867 // sites by file and line and if the file spec matches that of
1868 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 sc_list.Append(sc);
1870 }
1871 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1872 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001873 // sites by file and line and if the file spec matches that of
1874 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875 sc_list.Append(sc);
1876 }
1877
1878 if (!check_inlines)
1879 break;
1880 }
1881 }
1882 }
1883 }
1884 }
1885 return sc_list.GetSize() - prev_size;
1886}
1887
Jim Ingham7fca8c02017-04-28 00:51:06 +00001888void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001889 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001890 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891}
1892
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001893std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1894 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1895 if (module_sp)
1896 return module_sp->GetMutex();
1897 return GetObjectFile()->GetModule()->GetMutex();
1898}
1899
Kate Stoneb9c1b512016-09-06 20:57:50 +00001900bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1901 const lldb_private::CompilerDeclContext *decl_ctx) {
1902 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001903 // Invalid namespace decl which means we aren't matching only things in
1904 // this symbol file, so return true to indicate it matches this symbol
1905 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 return true;
1907 }
1908
1909 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1910 TypeSystem *type_system = GetTypeSystemForLanguage(
1911 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1912 if (decl_ctx_type_system == type_system)
1913 return true; // The type systems match, return true
1914
1915 // The namespace AST was valid, and it does not match...
1916 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1917
1918 if (log)
1919 GetObjectFile()->GetModule()->LogMessage(
1920 log, "Valid namespace does not match symbol file");
1921
1922 return false;
1923}
1924
1925uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001926 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00001927 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1929
1930 if (log)
1931 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00001932 log,
1933 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
1934 "parent_decl_ctx=%p, max_matches=%u, variables)",
1935 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001936 max_matches);
1937
1938 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1939 return 0;
1940
1941 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00001942 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001943 return 0;
1944
Pavel Labath34cda142018-05-31 09:46:26 +00001945 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 const uint32_t original_size = variables.GetSize();
1947
Pavel Labathe1d18752018-06-07 10:04:44 +00001948 llvm::StringRef basename;
1949 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00001950 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00001951
1952 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
1953 context, basename))
1954 basename = name.GetStringRef();
1955
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00001957 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001958 const size_t num_die_matches = die_offsets.size();
1959 if (num_die_matches) {
1960 SymbolContext sc;
1961 sc.module_sp = m_obj_file->GetModule();
1962 assert(sc.module_sp);
1963
Pavel Labathe1d18752018-06-07 10:04:44 +00001964 // Loop invariant: Variables up to this index have been checked for context
1965 // matches.
1966 uint32_t pruned_idx = original_size;
1967
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968 bool done = false;
1969 for (size_t i = 0; i < num_die_matches && !done; ++i) {
1970 const DIERef &die_ref = die_offsets[i];
1971 DWARFDIE die = GetDIE(die_ref);
1972
1973 if (die) {
1974 switch (die.Tag()) {
1975 default:
1976 case DW_TAG_subprogram:
1977 case DW_TAG_inlined_subroutine:
1978 case DW_TAG_try_block:
1979 case DW_TAG_catch_block:
1980 break;
1981
1982 case DW_TAG_variable: {
Pavel Labath519ef6a2019-05-27 10:10:59 +00001983 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001984
1985 if (parent_decl_ctx) {
1986 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1987 if (dwarf_ast) {
1988 CompilerDeclContext actual_parent_decl_ctx =
1989 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
1990 if (!actual_parent_decl_ctx ||
1991 actual_parent_decl_ctx != *parent_decl_ctx)
1992 continue;
1993 }
1994 }
1995
1996 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
1997 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00001998 while (pruned_idx < variables.GetSize()) {
1999 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002000 if (name_is_mangled ||
2001 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002002 ++pruned_idx;
2003 else
2004 variables.RemoveVariableAtIndex(pruned_idx);
2005 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006
2007 if (variables.GetSize() - original_size >= max_matches)
2008 done = true;
2009 } break;
2010 }
2011 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002012 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2013 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002014 }
2015 }
2016 }
2017
2018 // Return the number of variable that were appended to the list
2019 const uint32_t num_matches = variables.GetSize() - original_size;
2020 if (log && num_matches > 0) {
2021 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002022 log,
2023 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2024 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2025 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002026 max_matches, num_matches);
2027 }
2028 return num_matches;
2029}
2030
2031uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002032 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002033 VariableList &variables) {
2034 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2035
2036 if (log) {
2037 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002038 log,
2039 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2040 "max_matches=%u, variables)",
2041 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042 }
2043
2044 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002045 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002046 return 0;
2047
Pavel Labath34cda142018-05-31 09:46:26 +00002048 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 const uint32_t original_size = variables.GetSize();
2050
2051 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002052 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002053
2054 SymbolContext sc;
2055 sc.module_sp = m_obj_file->GetModule();
2056 assert(sc.module_sp);
2057
2058 const size_t num_matches = die_offsets.size();
2059 if (num_matches) {
2060 for (size_t i = 0; i < num_matches; ++i) {
2061 const DIERef &die_ref = die_offsets[i];
2062 DWARFDIE die = GetDIE(die_ref);
2063
2064 if (die) {
Pavel Labath519ef6a2019-05-27 10:10:59 +00002065 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002066
2067 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2068
2069 if (variables.GetSize() - original_size >= max_matches)
2070 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002071 } else
2072 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002073 }
2074 }
2075
2076 // Return the number of variable that were appended to the list
2077 return variables.GetSize() - original_size;
2078}
2079
Kate Stoneb9c1b512016-09-06 20:57:50 +00002080bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2081 bool include_inlines,
2082 SymbolContextList &sc_list) {
2083 SymbolContext sc;
2084
2085 if (!orig_die)
2086 return false;
2087
2088 // If we were passed a die that is not a function, just return false...
2089 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2090 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2091 return false;
2092
2093 DWARFDIE die = orig_die;
2094 DWARFDIE inlined_die;
2095 if (die.Tag() == DW_TAG_inlined_subroutine) {
2096 inlined_die = die;
2097
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002098 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002099 die = die.GetParent();
2100
2101 if (die) {
2102 if (die.Tag() == DW_TAG_subprogram)
2103 break;
2104 } else
2105 break;
2106 }
2107 }
2108 assert(die && die.Tag() == DW_TAG_subprogram);
2109 if (GetFunction(die, sc)) {
2110 Address addr;
2111 // Parse all blocks if needed
2112 if (inlined_die) {
2113 Block &function_block = sc.function->GetBlock(true);
2114 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002115 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002117 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002118 addr.Clear();
2119 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002120 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121 addr = sc.function->GetAddressRange().GetBaseAddress();
2122 }
2123
2124 if (addr.IsValid()) {
2125 sc_list.Append(sc);
2126 return true;
2127 }
2128 }
2129
2130 return false;
2131}
2132
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2134 const DWARFDIE &die) {
2135 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002136 // parent decl context isn't valid, we aren't trying to look for any
2137 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002138 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2139 return true;
2140
2141 if (die) {
2142 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2143 if (dwarf_ast) {
2144 CompilerDeclContext actual_decl_ctx =
2145 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2146 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002147 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 }
2149 }
2150 return false;
2151}
2152
Zachary Turner117b1fa2018-10-25 20:45:40 +00002153uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002154 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002155 FunctionNameType name_type_mask, bool include_inlines, bool append,
2156 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002157 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2158 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 name.AsCString());
2160
2161 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002162 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2164
2165 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2166
2167 if (log) {
2168 GetObjectFile()->GetModule()->LogMessage(
2169 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2170 "name_type_mask=0x%x, append=%u, sc_list)",
2171 name.GetCString(), name_type_mask, append);
2172 }
2173
2174 // If we aren't appending the results to this list, then clear the list
2175 if (!append)
2176 sc_list.Clear();
2177
2178 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2179 return 0;
2180
2181 // If name is empty then we won't find anything.
2182 if (name.IsEmpty())
2183 return 0;
2184
Adrian Prantl05097242018-04-30 16:49:04 +00002185 // Remember how many sc_list are in the list before we search in case we are
2186 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 const uint32_t original_size = sc_list.GetSize();
2189
2190 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002191 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002192 return 0;
2193
Pavel Labath5af11ab2018-06-05 10:33:56 +00002194 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2195 DIEArray offsets;
2196 CompilerDeclContext empty_decl_ctx;
2197 if (!parent_decl_ctx)
2198 parent_decl_ctx = &empty_decl_ctx;
2199
2200 std::vector<DWARFDIE> dies;
2201 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2202 for (const DWARFDIE &die: dies) {
2203 if (resolved_dies.insert(die.GetDIE()).second)
2204 ResolveFunction(die, include_inlines, sc_list);
2205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206
2207 // Return the number of variable that were appended to the list
2208 const uint32_t num_matches = sc_list.GetSize() - original_size;
2209
2210 if (log && num_matches > 0) {
2211 GetObjectFile()->GetModule()->LogMessage(
2212 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2213 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2214 "%u",
2215 name.GetCString(), name_type_mask, include_inlines, append,
2216 num_matches);
2217 }
2218 return num_matches;
2219}
2220
2221uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2222 bool include_inlines, bool append,
2223 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002224 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2225 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002226 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227
2228 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2229
2230 if (log) {
2231 GetObjectFile()->GetModule()->LogMessage(
2232 log,
2233 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002234 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002235 }
2236
2237 // If we aren't appending the results to this list, then clear the list
2238 if (!append)
2239 sc_list.Clear();
2240
Pavel Labathb13f0332018-05-21 14:12:52 +00002241 DWARFDebugInfo *info = DebugInfo();
2242 if (!info)
2243 return 0;
2244
Adrian Prantl05097242018-04-30 16:49:04 +00002245 // Remember how many sc_list are in the list before we search in case we are
2246 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 uint32_t original_size = sc_list.GetSize();
2248
Pavel Labath6de9c792018-06-05 12:13:22 +00002249 DIEArray offsets;
2250 m_index->GetFunctions(regex, offsets);
2251
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002252 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2253 for (DIERef ref : offsets) {
2254 DWARFDIE die = info->GetDIE(ref);
2255 if (!die) {
2256 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2257 continue;
2258 }
2259 if (resolved_dies.insert(die.GetDIE()).second)
2260 ResolveFunction(die, include_inlines, sc_list);
2261 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262
2263 // Return the number of variable that were appended to the list
2264 return sc_list.GetSize() - original_size;
2265}
2266
2267void SymbolFileDWARF::GetMangledNamesForFunction(
2268 const std::string &scope_qualified_name,
2269 std::vector<ConstString> &mangled_names) {
2270 DWARFDebugInfo *info = DebugInfo();
2271 uint32_t num_comp_units = 0;
2272 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002273 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274
2275 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002276 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002277 if (cu == nullptr)
2278 continue;
2279
2280 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2281 if (dwo)
2282 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2283 }
2284
2285 NameToOffsetMap::iterator iter =
2286 m_function_scope_qualified_name_map.find(scope_qualified_name);
2287 if (iter == m_function_scope_qualified_name_map.end())
2288 return;
2289
2290 DIERefSetSP set_sp = (*iter).second;
2291 std::set<DIERef>::iterator set_iter;
2292 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2293 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2294 mangled_names.push_back(ConstString(die.GetMangledName()));
2295 }
2296}
2297
2298uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002299 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002300 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2302 TypeMap &types) {
2303 // If we aren't appending the results to this list, then clear the list
2304 if (!append)
2305 types.Clear();
2306
2307 // Make sure we haven't already searched this SymbolFile before...
2308 if (searched_symbol_files.count(this))
2309 return 0;
2310 else
2311 searched_symbol_files.insert(this);
2312
2313 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002314 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002315 return 0;
2316
2317 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2318
2319 if (log) {
2320 if (parent_decl_ctx)
2321 GetObjectFile()->GetModule()->LogMessage(
2322 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2323 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2324 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2325 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2326 else
2327 GetObjectFile()->GetModule()->LogMessage(
2328 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2329 "NULL, append=%u, max_matches=%u, type_list)",
2330 name.GetCString(), append, max_matches);
2331 }
2332
2333 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2334 return 0;
2335
2336 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002337 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002338 const size_t num_die_matches = die_offsets.size();
2339
2340 if (num_die_matches) {
2341 const uint32_t initial_types_size = types.GetSize();
2342 for (size_t i = 0; i < num_die_matches; ++i) {
2343 const DIERef &die_ref = die_offsets[i];
2344 DWARFDIE die = GetDIE(die_ref);
2345
2346 if (die) {
2347 if (!DIEInDeclContext(parent_decl_ctx, die))
2348 continue; // The containing decl contexts don't match
2349
2350 Type *matching_type = ResolveType(die, true, true);
2351 if (matching_type) {
2352 // We found a type pointer, now find the shared pointer form our type
2353 // list
2354 types.InsertUnique(matching_type->shared_from_this());
2355 if (types.GetSize() >= max_matches)
2356 break;
2357 }
2358 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002359 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2360 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 }
2362 }
2363 const uint32_t num_matches = types.GetSize() - initial_types_size;
2364 if (log && num_matches) {
2365 if (parent_decl_ctx) {
2366 GetObjectFile()->GetModule()->LogMessage(
2367 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2368 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2369 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2370 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2371 num_matches);
2372 } else {
2373 GetObjectFile()->GetModule()->LogMessage(
2374 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2375 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2376 name.GetCString(), append, max_matches, num_matches);
2377 }
2378 }
2379 return num_matches;
2380 } else {
2381 UpdateExternalModuleListIfNeeded();
2382
2383 for (const auto &pair : m_external_type_modules) {
2384 ModuleSP external_module_sp = pair.second;
2385 if (external_module_sp) {
2386 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2387 if (sym_vendor) {
2388 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002389 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2390 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002391 if (num_external_matches)
2392 return num_external_matches;
2393 }
2394 }
2395 }
2396 }
2397
2398 return 0;
2399}
2400
2401size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2402 bool append, TypeMap &types) {
2403 if (!append)
2404 types.Clear();
2405
2406 if (context.empty())
2407 return 0;
2408
Kate Stoneb9c1b512016-09-06 20:57:50 +00002409 ConstString name = context.back().name;
2410
2411 if (!name)
2412 return 0;
2413
Pavel Labathb13f0332018-05-21 14:12:52 +00002414 DIEArray die_offsets;
2415 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002416 const size_t num_die_matches = die_offsets.size();
2417
2418 if (num_die_matches) {
2419 size_t num_matches = 0;
2420 for (size_t i = 0; i < num_die_matches; ++i) {
2421 const DIERef &die_ref = die_offsets[i];
2422 DWARFDIE die = GetDIE(die_ref);
2423
2424 if (die) {
2425 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002426 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002427 if (die_context != context)
2428 continue;
2429
2430 Type *matching_type = ResolveType(die, true, true);
2431 if (matching_type) {
2432 // We found a type pointer, now find the shared pointer form our type
2433 // list
2434 types.InsertUnique(matching_type->shared_from_this());
2435 ++num_matches;
2436 }
2437 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002438 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2439 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002440 }
2441 }
2442 return num_matches;
2443 }
2444 return 0;
2445}
2446
2447CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002448SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002449 const CompilerDeclContext *parent_decl_ctx) {
2450 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2451
2452 if (log) {
2453 GetObjectFile()->GetModule()->LogMessage(
2454 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2455 name.GetCString());
2456 }
2457
2458 CompilerDeclContext namespace_decl_ctx;
2459
2460 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2461 return namespace_decl_ctx;
2462
2463 DWARFDebugInfo *info = DebugInfo();
2464 if (info) {
2465 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002466 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467 const size_t num_matches = die_offsets.size();
2468 if (num_matches) {
2469 for (size_t i = 0; i < num_matches; ++i) {
2470 const DIERef &die_ref = die_offsets[i];
2471 DWARFDIE die = GetDIE(die_ref);
2472
2473 if (die) {
2474 if (!DIEInDeclContext(parent_decl_ctx, die))
2475 continue; // The containing decl contexts don't match
2476
2477 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2478 if (dwarf_ast) {
2479 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2480 if (namespace_decl_ctx)
2481 break;
2482 }
2483 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002484 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2485 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002486 }
2487 }
2488 }
2489 }
2490 if (log && namespace_decl_ctx) {
2491 GetObjectFile()->GetModule()->LogMessage(
2492 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2493 "CompilerDeclContext(%p/%p) \"%s\"",
2494 name.GetCString(),
2495 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2496 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2497 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2498 }
2499
2500 return namespace_decl_ctx;
2501}
2502
2503TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2504 bool resolve_function_context) {
2505 TypeSP type_sp;
2506 if (die) {
2507 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002508 if (type_ptr == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002509 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2510 assert(lldb_cu);
2511 SymbolContext sc(lldb_cu);
2512 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2513 while (parent_die != nullptr) {
2514 if (parent_die->Tag() == DW_TAG_subprogram)
2515 break;
2516 parent_die = parent_die->GetParent();
2517 }
2518 SymbolContext sc_backup = sc;
2519 if (resolve_function_context && parent_die != nullptr &&
2520 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2521 sc = sc_backup;
2522
Konrad Kleine248a1302019-05-23 11:14:47 +00002523 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002524 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2525 // Grab the existing type from the master types lists
2526 type_sp = type_ptr->shared_from_this();
2527 }
2528 }
2529 return type_sp;
2530}
2531
2532DWARFDIE
2533SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2534 if (orig_die) {
2535 DWARFDIE die = orig_die;
2536
2537 while (die) {
2538 // If this is the original DIE that we are searching for a declaration
2539 // for, then don't look in the cache as we don't want our own decl
2540 // context to be our decl context...
2541 if (orig_die != die) {
2542 switch (die.Tag()) {
2543 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002544 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002545 case DW_TAG_namespace:
2546 case DW_TAG_structure_type:
2547 case DW_TAG_union_type:
2548 case DW_TAG_class_type:
2549 case DW_TAG_lexical_block:
2550 case DW_TAG_subprogram:
2551 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002552 case DW_TAG_inlined_subroutine: {
2553 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2554 if (abs_die) {
2555 return abs_die;
2556 }
2557 break;
2558 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002559 default:
2560 break;
2561 }
2562 }
2563
2564 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2565 if (spec_die) {
2566 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2567 if (decl_ctx_die)
2568 return decl_ctx_die;
2569 }
2570
2571 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2572 if (abs_die) {
2573 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2574 if (decl_ctx_die)
2575 return decl_ctx_die;
2576 }
2577
2578 die = die.GetParent();
2579 }
2580 }
2581 return DWARFDIE();
2582}
2583
2584Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002585SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002586 Symbol *objc_class_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002587 if (m_obj_file) {
2588 Symtab *symtab = m_obj_file->GetSymtab();
2589 if (symtab) {
2590 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2591 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2592 Symtab::eVisibilityAny);
2593 }
2594 }
2595 return objc_class_symbol;
2596}
2597
2598// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002599// they don't then we can end up looking through all class types for a complete
2600// type and never find the full definition. We need to know if this attribute
2601// is supported, so we determine this here and cache th result. We also need to
2602// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002603// DWARF file
2604// if we are doing darwin DWARF in .o file debugging.
2605bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002606 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002607 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2608 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2609 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2610 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2611 else {
2612 DWARFDebugInfo *debug_info = DebugInfo();
2613 const uint32_t num_compile_units = GetNumCompileUnits();
2614 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002615 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 if (dwarf_cu != cu &&
2617 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2618 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2619 break;
2620 }
2621 }
2622 }
2623 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2624 GetDebugMapSymfile())
2625 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2626 }
2627 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002628}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002629
2630// This function can be used when a DIE is found that is a forward declaration
2631// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002632TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002633 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002634 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002635
Kate Stoneb9c1b512016-09-06 20:57:50 +00002636 TypeSP type_sp;
2637
2638 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2639 return type_sp;
2640
2641 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002642 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643
2644 const size_t num_matches = die_offsets.size();
2645
2646 if (num_matches) {
2647 for (size_t i = 0; i < num_matches; ++i) {
2648 const DIERef &die_ref = die_offsets[i];
2649 DWARFDIE type_die = GetDIE(die_ref);
2650
2651 if (type_die) {
2652 bool try_resolving_type = false;
2653
Adrian Prantl05097242018-04-30 16:49:04 +00002654 // Don't try and resolve the DIE we are looking for with the DIE
2655 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002656 if (type_die != die) {
2657 switch (type_die.Tag()) {
2658 case DW_TAG_class_type:
2659 case DW_TAG_structure_type:
2660 try_resolving_type = true;
2661 break;
2662 default:
2663 break;
2664 }
2665 }
2666
2667 if (try_resolving_type) {
2668 if (must_be_implementation &&
2669 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2670 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2671 DW_AT_APPLE_objc_complete_type, 0);
2672
2673 if (try_resolving_type) {
2674 Type *resolved_type = ResolveType(type_die, false, true);
2675 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2676 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2677 " (cu 0x%8.8" PRIx64 ")\n",
2678 die.GetID(),
2679 m_obj_file->GetFileSpec().GetFilename().AsCString(
2680 "<Unknown>"),
2681 type_die.GetID(), type_cu->GetID());
2682
2683 if (die)
2684 GetDIEToType()[die.GetDIE()] = resolved_type;
2685 type_sp = resolved_type->shared_from_this();
2686 break;
2687 }
2688 }
2689 }
2690 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002691 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2692 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 }
2694 }
2695 }
2696 return type_sp;
2697}
Greg Claytona8022fa2012-04-24 21:22:41 +00002698
Adrian Prantl05097242018-04-30 16:49:04 +00002699// This function helps to ensure that the declaration contexts match for two
2700// different DIEs. Often times debug information will refer to a forward
2701// declaration of a type (the equivalent of "struct my_struct;". There will
2702// often be a declaration of that type elsewhere that has the full definition.
2703// When we go looking for the full type "my_struct", we will find one or more
2704// matches in the accelerator tables and we will then need to make sure the
2705// type was in the same declaration context as the original DIE. This function
2706// can efficiently compare two DIEs and will return true when the declaration
2707// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002708bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2709 const DWARFDIE &die2) {
2710 if (die1 == die2)
2711 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002712
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002713 std::vector<DWARFDIE> decl_ctx_1;
2714 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002715 // The declaration DIE stack is a stack of the declaration context DIEs all
2716 // the way back to the compile unit. If a type "T" is declared inside a class
2717 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2718 // namespace "lldb", and the namespace is in a compile unit, there will be a
2719 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 //
2721 // [0] DW_TAG_class_type for "B"
2722 // [1] DW_TAG_class_type for "A"
2723 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002724 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002725 //
Adrian Prantl05097242018-04-30 16:49:04 +00002726 // We grab both contexts and make sure that everything matches all the way
2727 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002728
2729 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002730 decl_ctx_1 = die1.GetDeclContextDIEs();
2731 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002732 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002733 const size_t count1 = decl_ctx_1.size();
2734 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 if (count1 != count2)
2736 return false;
2737
Adrian Prantl05097242018-04-30 16:49:04 +00002738 // Make sure the DW_TAG values match all the way back up the compile unit. If
2739 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002740 DWARFDIE decl_ctx_die1;
2741 DWARFDIE decl_ctx_die2;
2742 size_t i;
2743 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002744 decl_ctx_die1 = decl_ctx_1[i];
2745 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002746 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2747 return false;
2748 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002749#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002750
Kate Stoneb9c1b512016-09-06 20:57:50 +00002751 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002752 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2753 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2754 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002755 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002756 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2757 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002758
Greg Clayton890ff562012-02-02 05:48:16 +00002759#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002760 // Always skip the compile unit when comparing by only iterating up to "count
2761 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002762 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002763 decl_ctx_die1 = decl_ctx_1[i];
2764 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002765 const char *name1 = decl_ctx_die1.GetName();
2766 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002767 // If the string was from a DW_FORM_strp, then the pointer will often be
2768 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002769 if (name1 == name2)
2770 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002771
Adrian Prantl05097242018-04-30 16:49:04 +00002772 // Name pointers are not equal, so only compare the strings if both are not
2773 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002774 if (name1 && name2) {
2775 // If the strings don't compare, we are done...
2776 if (strcmp(name1, name2) != 0)
2777 return false;
2778 } else {
2779 // One name was NULL while the other wasn't
2780 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002781 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002782 }
Adrian Prantl05097242018-04-30 16:49:04 +00002783 // We made it through all of the checks and the declaration contexts are
2784 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002785 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002786}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002787
Kate Stoneb9c1b512016-09-06 20:57:50 +00002788TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2789 const DWARFDeclContext &dwarf_decl_ctx) {
2790 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002791
Kate Stoneb9c1b512016-09-06 20:57:50 +00002792 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2793 if (dwarf_decl_ctx_count > 0) {
2794 const ConstString type_name(dwarf_decl_ctx[0].name);
2795 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002796
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 if (type_name) {
2798 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2799 DWARF_LOG_LOOKUPS));
2800 if (log) {
2801 GetObjectFile()->GetModule()->LogMessage(
2802 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2803 "s, qualified-name='%s')",
2804 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2805 dwarf_decl_ctx.GetQualifiedName());
2806 }
2807
2808 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002809 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002810 const size_t num_matches = die_offsets.size();
2811
Adrian Prantl05097242018-04-30 16:49:04 +00002812 // Get the type system that we are looking to find a type for. We will
2813 // use this to ensure any matches we find are in a language that this
2814 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002815 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2816 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2817 ? nullptr
2818 : GetTypeSystemForLanguage(language);
2819
2820 if (num_matches) {
2821 for (size_t i = 0; i < num_matches; ++i) {
2822 const DIERef &die_ref = die_offsets[i];
2823 DWARFDIE type_die = GetDIE(die_ref);
2824
2825 if (type_die) {
2826 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002827 // looking for. We don't want to find a "Foo" type from Java if we
2828 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002829 if (type_system &&
2830 !type_system->SupportsLanguage(type_die.GetLanguage()))
2831 continue;
2832 bool try_resolving_type = false;
2833
2834 // Don't try and resolve the DIE we are looking for with the DIE
2835 // itself!
2836 const dw_tag_t type_tag = type_die.Tag();
2837 // Make sure the tags match
2838 if (type_tag == tag) {
2839 // The tags match, lets try resolving this type
2840 try_resolving_type = true;
2841 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002842 // The tags don't match, but we need to watch our for a forward
2843 // declaration for a struct and ("struct foo") ends up being a
2844 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002845 switch (type_tag) {
2846 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002847 // We had a "class foo", see if we ended up with a "struct foo
2848 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002849 try_resolving_type = (tag == DW_TAG_structure_type);
2850 break;
2851 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002852 // We had a "struct foo", see if we ended up with a "class foo
2853 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 try_resolving_type = (tag == DW_TAG_class_type);
2855 break;
2856 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002857 // Tags don't match, don't event try to resolve using this type
2858 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002859 break;
2860 }
2861 }
2862
2863 if (try_resolving_type) {
2864 DWARFDeclContext type_dwarf_decl_ctx;
2865 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2866
2867 if (log) {
2868 GetObjectFile()->GetModule()->LogMessage(
2869 log, "SymbolFileDWARF::"
2870 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2871 "qualified-name='%s') trying die=0x%8.8x (%s)",
2872 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2873 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2874 type_dwarf_decl_ctx.GetQualifiedName());
2875 }
2876
2877 // Make sure the decl contexts match all the way up
2878 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2879 Type *resolved_type = ResolveType(type_die, false);
2880 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2881 type_sp = resolved_type->shared_from_this();
2882 break;
2883 }
2884 }
2885 } else {
2886 if (log) {
2887 std::string qualified_name;
2888 type_die.GetQualifiedName(qualified_name);
2889 GetObjectFile()->GetModule()->LogMessage(
2890 log, "SymbolFileDWARF::"
2891 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2892 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2893 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2894 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2895 qualified_name.c_str());
2896 }
2897 }
2898 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002899 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2900 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002901 }
2902 }
2903 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002905 }
2906 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002907}
2908
Kate Stoneb9c1b512016-09-06 20:57:50 +00002909TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2910 bool *type_is_new_ptr) {
2911 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002912
Kate Stoneb9c1b512016-09-06 20:57:50 +00002913 if (die) {
2914 TypeSystem *type_system =
2915 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00002916
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 if (type_system) {
2918 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2919 if (dwarf_ast) {
2920 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
2921 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
2922 if (type_sp) {
2923 TypeList *type_list = GetTypeList();
2924 if (type_list)
2925 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00002926
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 if (die.Tag() == DW_TAG_subprogram) {
2928 DIERef die_ref = die.GetDIERef();
2929 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2930 .GetScopeQualifiedName()
2931 .AsCString(""));
2932 if (scope_qualified_name.size()) {
2933 NameToOffsetMap::iterator iter =
2934 m_function_scope_qualified_name_map.find(
2935 scope_qualified_name);
2936 if (iter != m_function_scope_qualified_name_map.end())
2937 (*iter).second->insert(die_ref);
2938 else {
2939 DIERefSetSP new_set(new std::set<DIERef>);
2940 new_set->insert(die_ref);
2941 m_function_scope_qualified_name_map.emplace(
2942 std::make_pair(scope_qualified_name, new_set));
2943 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002944 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002945 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00002946 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002947 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002948 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002949 }
2950
2951 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002952}
2953
Kate Stoneb9c1b512016-09-06 20:57:50 +00002954size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
2955 const DWARFDIE &orig_die,
2956 bool parse_siblings, bool parse_children) {
2957 size_t types_added = 0;
2958 DWARFDIE die = orig_die;
2959 while (die) {
2960 bool type_is_new = false;
2961 if (ParseType(sc, die, &type_is_new).get()) {
2962 if (type_is_new)
2963 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002965
2966 if (parse_children && die.HasChildren()) {
2967 if (die.Tag() == DW_TAG_subprogram) {
2968 SymbolContext child_sc(sc);
2969 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
2970 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
2971 } else
2972 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
2973 }
2974
2975 if (parse_siblings)
2976 die = die.GetSibling();
2977 else
2978 die.Clear();
2979 }
2980 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002981}
2982
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00002983size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002984 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00002985 CompileUnit *comp_unit = func.GetCompileUnit();
2986 lldbassert(comp_unit);
2987
2988 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
2989 if (!dwarf_cu)
2990 return 0;
2991
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00002993 const dw_offset_t function_die_offset = func.GetID();
2994 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
2995 if (function_die) {
2996 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
2997 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002998 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002999
Kate Stoneb9c1b512016-09-06 20:57:50 +00003000 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001}
3002
Zachary Turner863f8c12019-01-11 18:03:20 +00003003size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003004 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003005 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003006 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003007 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003008 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3009 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3010 SymbolContext sc;
3011 sc.comp_unit = &comp_unit;
3012 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003013 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003014 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003015
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003017}
3018
Kate Stoneb9c1b512016-09-06 20:57:50 +00003019size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003020 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +00003021 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003022 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003023 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003024 return 0;
3025
3026 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003027 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028
3029 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3030 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3031 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3032 const size_t num_variables = ParseVariables(
3033 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3034
3035 // Let all blocks know they have parse all their variables
3036 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3037 return num_variables;
3038 }
3039 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003040 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003041
Konrad Kleine248a1302019-05-23 11:14:47 +00003042 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003043 return 0;
3044
3045 uint32_t vars_added = 0;
3046 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3047
Konrad Kleine248a1302019-05-23 11:14:47 +00003048 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003049 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050 sc.comp_unit->SetVariableList(variables);
3051
3052 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003053 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003054 const size_t num_matches = die_offsets.size();
3055 if (num_matches) {
3056 for (size_t i = 0; i < num_matches; ++i) {
3057 const DIERef &die_ref = die_offsets[i];
3058 DWARFDIE die = GetDIE(die_ref);
3059 if (die) {
3060 VariableSP var_sp(
3061 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3062 if (var_sp) {
3063 variables->AddVariableIfUnique(var_sp);
3064 ++vars_added;
3065 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003066 } else
3067 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070 }
3071 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003073 }
3074 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075}
3076
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3078 const DWARFDIE &die,
3079 const lldb::addr_t func_low_pc) {
3080 if (die.GetDWARF() != this)
3081 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003082
Kate Stoneb9c1b512016-09-06 20:57:50 +00003083 VariableSP var_sp;
3084 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003085 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003086
Kate Stoneb9c1b512016-09-06 20:57:50 +00003087 var_sp = GetDIEToVariable()[die.GetDIE()];
3088 if (var_sp)
3089 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003090
Kate Stoneb9c1b512016-09-06 20:57:50 +00003091 const dw_tag_t tag = die.Tag();
3092 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003093
Kate Stoneb9c1b512016-09-06 20:57:50 +00003094 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3095 (tag == DW_TAG_formal_parameter && sc.function)) {
3096 DWARFAttributes attributes;
3097 const size_t num_attributes = die.GetAttributes(attributes);
3098 DWARFDIE spec_die;
3099 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003100 const char *name = nullptr;
3101 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003102 Declaration decl;
3103 uint32_t i;
3104 DWARFFormValue type_die_form;
3105 DWARFExpression location(die.GetCU());
3106 bool is_external = false;
3107 bool is_artificial = false;
3108 bool location_is_const_value_data = false;
3109 bool has_explicit_location = false;
3110 DWARFFormValue const_value;
3111 Variable::RangeList scope_ranges;
3112 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003113
Kate Stoneb9c1b512016-09-06 20:57:50 +00003114 for (i = 0; i < num_attributes; ++i) {
3115 dw_attr_t attr = attributes.AttributeAtIndex(i);
3116 DWARFFormValue form_value;
3117
3118 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3119 switch (attr) {
3120 case DW_AT_decl_file:
3121 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3122 form_value.Unsigned()));
3123 break;
3124 case DW_AT_decl_line:
3125 decl.SetLine(form_value.Unsigned());
3126 break;
3127 case DW_AT_decl_column:
3128 decl.SetColumn(form_value.Unsigned());
3129 break;
3130 case DW_AT_name:
3131 name = form_value.AsCString();
3132 break;
3133 case DW_AT_linkage_name:
3134 case DW_AT_MIPS_linkage_name:
3135 mangled = form_value.AsCString();
3136 break;
3137 case DW_AT_type:
3138 type_die_form = form_value;
3139 break;
3140 case DW_AT_external:
3141 is_external = form_value.Boolean();
3142 break;
3143 case DW_AT_const_value:
3144 // If we have already found a DW_AT_location attribute, ignore this
3145 // attribute.
3146 if (!has_explicit_location) {
3147 location_is_const_value_data = true;
3148 // The constant value will be either a block, a data value or a
3149 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003150 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3152 // Retrieve the value as a block expression.
3153 uint32_t block_offset =
3154 form_value.BlockData() - debug_info_data.GetDataStart();
3155 uint32_t block_length = form_value.Unsigned();
3156 location.CopyOpcodeData(module, debug_info_data, block_offset,
3157 block_length);
3158 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3159 // Retrieve the value as a data expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003161 if (auto data_length = form_value.GetFixedSize())
3162 location.CopyOpcodeData(module, debug_info_data, data_offset,
3163 *data_length);
3164 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003165 const uint8_t *data_pointer = form_value.BlockData();
3166 if (data_pointer) {
3167 form_value.Unsigned();
3168 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3169 // we need to get the byte size of the type later after we
3170 // create the variable
3171 const_value = form_value;
3172 }
Greg Clayton6aad81c2019-05-24 22:08:50 +00003173 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003174 } else {
3175 // Retrieve the value as a string expression.
3176 if (form_value.Form() == DW_FORM_strp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003177 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003178 if (auto data_length = form_value.GetFixedSize())
3179 location.CopyOpcodeData(module, debug_info_data,
3180 data_offset, *data_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 } else {
3182 const char *str = form_value.AsCString();
3183 uint32_t string_offset =
3184 str - (const char *)debug_info_data.GetDataStart();
3185 uint32_t string_length = strlen(str) + 1;
3186 location.CopyOpcodeData(module, debug_info_data,
3187 string_offset, string_length);
3188 }
3189 }
3190 }
3191 break;
3192 case DW_AT_location: {
3193 location_is_const_value_data = false;
3194 has_explicit_location = true;
3195 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003196 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197
3198 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003199 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003200 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003201 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003202 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003203 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003204 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3205
3206 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003207 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003208 if (loc_list_length > 0) {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003209 location.CopyOpcodeData(module, debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003210 debug_loc_offset, loc_list_length);
3211 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3212 location.SetLocationListSlide(
3213 func_low_pc -
3214 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3215 }
3216 }
3217 } break;
3218 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003219 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220 break;
3221 case DW_AT_start_scope: {
3222 if (form_value.Form() == DW_FORM_sec_offset) {
3223 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003224 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3225 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003226 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003227 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003228 } else {
3229 // TODO: Handle the case when DW_AT_start_scope have form
3230 // constant. The
3231 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003232 // behavior in case the enclosing block have a non coninious
3233 // address range and the DW_AT_start_scope entry have a form
3234 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003235 GetObjectFile()->GetModule()->ReportWarning(
3236 "0x%8.8" PRIx64
3237 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3238 die.GetID(), form_value.Form());
3239 }
3240
3241 scope_ranges.Sort();
3242 scope_ranges.CombineConsecutiveRanges();
3243 } break;
3244 case DW_AT_artificial:
3245 is_artificial = form_value.Boolean();
3246 break;
3247 case DW_AT_accessibility:
3248 break; // accessibility =
3249 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3250 case DW_AT_declaration:
3251 case DW_AT_description:
3252 case DW_AT_endianity:
3253 case DW_AT_segment:
3254 case DW_AT_visibility:
3255 default:
3256 case DW_AT_abstract_origin:
3257 case DW_AT_sibling:
3258 break;
3259 }
3260 }
3261 }
3262
3263 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3264 const dw_tag_t parent_tag = die.GetParent().Tag();
3265 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003266 (parent_tag == DW_TAG_compile_unit ||
3267 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268 (parent_context_die.Tag() == DW_TAG_class_type ||
3269 parent_context_die.Tag() == DW_TAG_structure_type);
3270
3271 ValueType scope = eValueTypeInvalid;
3272
3273 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003274 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003275
Sam McCall30621392016-11-22 11:40:25 +00003276 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003277 if (!mangled) {
3278 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003279 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3280 // of global variables with commands like "frame var j". For
3281 // example, if j were an int variable holding a value 4 and
3282 // declared in a namespace B which in turn is contained in a
3283 // namespace A, the command "frame var j" returns
3284 // "(int) A::B::j = 4".
3285 // If the compiler does not emit a linkage name, we should be
3286 // able to generate a fully qualified name from the
3287 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003288 if ((parent_tag == DW_TAG_compile_unit ||
3289 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3291 DWARFDeclContext decl_ctx;
3292
3293 die.GetDWARFDeclContext(decl_ctx);
3294 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3295 }
3296 }
3297
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 if (tag == DW_TAG_formal_parameter)
3299 scope = eValueTypeVariableArgument;
3300 else {
Sam McCall30621392016-11-22 11:40:25 +00003301 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3302 // or static variable, so we have to do a little digging:
3303 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3304 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3305 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3306 // Clang likes to combine small global variables into the same symbol
3307 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3308 // so we need to look through the whole expression.
3309 bool is_static_lifetime =
3310 has_explicit_mangled ||
3311 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003312 // Check if the location has a DW_OP_addr with any address value...
3313 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3314 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003315 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3317 if (op_error) {
3318 StreamString strm;
3319 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003320 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003321 GetObjectFile()->GetModule()->ReportError(
3322 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003323 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003324 }
Sam McCall30621392016-11-22 11:40:25 +00003325 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3326 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 }
3328 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003329 if (debug_map_symfile)
3330 // Set the module of the expression to the linked module
3331 // instead of the oject file so the relocated address can be
3332 // found there.
3333 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003334
Sam McCall30621392016-11-22 11:40:25 +00003335 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003336 if (is_external)
3337 scope = eValueTypeVariableGlobal;
3338 else
3339 scope = eValueTypeVariableStatic;
3340
3341 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003342 // When leaving the DWARF in the .o files on darwin, when we have a
3343 // global variable that wasn't initialized, the .o file might not
3344 // have allocated a virtual address for the global variable. In
3345 // this case it will have created a symbol for the global variable
3346 // that is undefined/data and external and the value will be the
3347 // byte size of the variable. When we do the address map in
3348 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3349 // do some magic here so we can get the correct address for our
3350 // global variable. The address for all of these entries will be
3351 // zero, and there will be an undefined symbol in this object file,
3352 // and the executable will have a matching symbol with a good
3353 // address. So here we dig up the correct address and replace it in
3354 // the location for the variable, and set the variable's symbol
3355 // context scope to be that of the main executable so the file
3356 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003357 bool linked_oso_file_addr = false;
3358 if (is_external && location_DW_OP_addr == 0) {
3359 // we have a possible uninitialized extern global
3360 ConstString const_name(mangled ? mangled : name);
3361 ObjectFile *debug_map_objfile =
3362 debug_map_symfile->GetObjectFile();
3363 if (debug_map_objfile) {
3364 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3365 if (debug_map_symtab) {
3366 Symbol *exe_symbol =
3367 debug_map_symtab->FindFirstSymbolWithNameAndType(
3368 const_name, eSymbolTypeData, Symtab::eDebugYes,
3369 Symtab::eVisibilityExtern);
3370 if (exe_symbol) {
3371 if (exe_symbol->ValueIsAddress()) {
3372 const addr_t exe_file_addr =
3373 exe_symbol->GetAddressRef().GetFileAddress();
3374 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3375 if (location.Update_DW_OP_addr(exe_file_addr)) {
3376 linked_oso_file_addr = true;
3377 symbol_context_scope = exe_symbol;
3378 }
3379 }
3380 }
3381 }
3382 }
3383 }
3384 }
3385
3386 if (!linked_oso_file_addr) {
3387 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003388 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003389 const lldb::addr_t exe_file_addr =
3390 debug_map_symfile->LinkOSOFileAddress(this,
3391 location_DW_OP_addr);
3392 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3393 // Update the file address for this variable
3394 location.Update_DW_OP_addr(exe_file_addr);
3395 } else {
3396 // Variable didn't make it into the final executable
3397 return var_sp;
3398 }
3399 }
3400 }
3401 } else {
3402 if (location_is_const_value_data)
3403 scope = eValueTypeVariableStatic;
3404 else {
3405 scope = eValueTypeVariableLocal;
3406 if (debug_map_symfile) {
3407 // We need to check for TLS addresses that we need to fixup
3408 if (location.ContainsThreadLocalStorage()) {
3409 location.LinkThreadLocalStorage(
3410 debug_map_symfile->GetObjectFile()->GetModule(),
3411 [this, debug_map_symfile](
3412 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3413 return debug_map_symfile->LinkOSOFileAddress(
3414 this, unlinked_file_addr);
3415 });
3416 scope = eValueTypeVariableThreadLocal;
3417 }
3418 }
3419 }
3420 }
3421 }
3422
Konrad Kleine248a1302019-05-23 11:14:47 +00003423 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003424 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003425 case DW_TAG_subprogram:
3426 case DW_TAG_inlined_subroutine:
3427 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 if (sc.function) {
3429 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3430 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003431 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003432 symbol_context_scope = sc.function;
3433 }
3434 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003435
Kate Stoneb9c1b512016-09-06 20:57:50 +00003436 default:
3437 symbol_context_scope = sc.comp_unit;
3438 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003440 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003441
Kate Stoneb9c1b512016-09-06 20:57:50 +00003442 if (symbol_context_scope) {
3443 SymbolFileTypeSP type_sp(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003444 new SymbolFileType(*this, GetUID(DIERef(type_die_form))));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003445
3446 if (const_value.Form() && type_sp && type_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00003447 location.CopyOpcodeData(
3448 const_value.Unsigned(),
3449 type_sp->GetType()->GetByteSize().getValueOr(0),
3450 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003451
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003452 var_sp = std::make_shared<Variable>(
3453 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3454 scope_ranges, &decl, location, is_external, is_artificial,
3455 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003456
3457 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3458 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003459 // Not ready to parse this variable yet. It might be a global or static
3460 // variable that is in a function scope and the function in the symbol
3461 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003462 return var_sp;
3463 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003464 }
Adrian Prantl05097242018-04-30 16:49:04 +00003465 // Cache var_sp even if NULL (the variable was just a specification or was
3466 // missing vital information to be able to be displayed in the debugger
3467 // (missing location due to optimization, etc)) so we don't re-parse this
3468 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003469 GetDIEToVariable()[die.GetDIE()] = var_sp;
3470 if (spec_die)
3471 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3472 }
3473 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003474}
3475
Kate Stoneb9c1b512016-09-06 20:57:50 +00003476DWARFDIE
3477SymbolFileDWARF::FindBlockContainingSpecification(
3478 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3479 // Give the concrete function die specified by "func_die_offset", find the
3480 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3481 // to "spec_block_die_offset"
3482 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3483 spec_block_die_offset);
3484}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003485
Kate Stoneb9c1b512016-09-06 20:57:50 +00003486DWARFDIE
3487SymbolFileDWARF::FindBlockContainingSpecification(
3488 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3489 if (die) {
3490 switch (die.Tag()) {
3491 case DW_TAG_subprogram:
3492 case DW_TAG_inlined_subroutine:
3493 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003494 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3495 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003496 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003497
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003498 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003499 spec_block_die_offset)
3500 return die;
3501 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003502 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003503
3504 // Give the concrete function die specified by "func_die_offset", find the
3505 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3506 // to "spec_block_die_offset"
3507 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3508 child_die = child_die.GetSibling()) {
3509 DWARFDIE result_die =
3510 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3511 if (result_die)
3512 return result_die;
3513 }
3514 }
3515
3516 return DWARFDIE();
3517}
3518
3519size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3520 const DWARFDIE &orig_die,
3521 const lldb::addr_t func_low_pc,
3522 bool parse_siblings, bool parse_children,
3523 VariableList *cc_variable_list) {
3524 if (!orig_die)
3525 return 0;
3526
3527 VariableListSP variable_list_sp;
3528
3529 size_t vars_added = 0;
3530 DWARFDIE die = orig_die;
3531 while (die) {
3532 dw_tag_t tag = die.Tag();
3533
3534 // Check to see if we have already parsed this variable or constant?
3535 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3536 if (var_sp) {
3537 if (cc_variable_list)
3538 cc_variable_list->AddVariableIfUnique(var_sp);
3539 } else {
3540 // We haven't already parsed it, lets do that now.
3541 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3542 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003543 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003544 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3545 dw_tag_t parent_tag = sc_parent_die.Tag();
3546 switch (parent_tag) {
3547 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003548 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003549 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003550 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003551 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003552 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003553 }
3554 } else {
3555 GetObjectFile()->GetModule()->ReportError(
3556 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3557 "symbol context for 0x%8.8" PRIx64
3558 " %s.\n",
3559 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3560 orig_die.GetID(), orig_die.GetTagAsCString());
3561 }
3562 break;
3563
3564 case DW_TAG_subprogram:
3565 case DW_TAG_inlined_subroutine:
3566 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003567 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568 // Check to see if we already have parsed the variables for the
3569 // given scope
3570
3571 Block *block = sc.function->GetBlock(true).FindBlockByID(
3572 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003573 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003574 // This must be a specification or abstract origin with a
3575 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003576 // to find the concrete block so we can correctly add the
3577 // variable to it
3578 const DWARFDIE concrete_block_die =
3579 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003580 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003581 sc_parent_die.GetOffset());
3582 if (concrete_block_die)
3583 block = sc.function->GetBlock(true).FindBlockByID(
3584 concrete_block_die.GetID());
3585 }
3586
Konrad Kleine248a1302019-05-23 11:14:47 +00003587 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003588 const bool can_create = false;
3589 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003590 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003591 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003592 block->SetVariableList(variable_list_sp);
3593 }
3594 }
3595 }
3596 break;
3597
3598 default:
3599 GetObjectFile()->GetModule()->ReportError(
3600 "didn't find appropriate parent DIE for variable list for "
3601 "0x%8.8" PRIx64 " %s.\n",
3602 orig_die.GetID(), orig_die.GetTagAsCString());
3603 break;
3604 }
3605 }
3606
3607 if (variable_list_sp) {
3608 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3609 if (var_sp) {
3610 variable_list_sp->AddVariableIfUnique(var_sp);
3611 if (cc_variable_list)
3612 cc_variable_list->AddVariableIfUnique(var_sp);
3613 ++vars_added;
3614 }
3615 }
3616 }
3617 }
3618
Konrad Kleine248a1302019-05-23 11:14:47 +00003619 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003620
3621 if (!skip_children && parse_children && die.HasChildren()) {
3622 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3623 true, cc_variable_list);
3624 }
3625
3626 if (parse_siblings)
3627 die = die.GetSibling();
3628 else
3629 die.Clear();
3630 }
3631 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003632}
3633
Vedant Kumar4b36f792018-10-05 23:23:15 +00003634/// Collect call graph edges present in a function DIE.
3635static std::vector<lldb_private::CallEdge>
3636CollectCallEdges(DWARFDIE function_die) {
3637 // Check if the function has a supported call site-related attribute.
3638 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3639 uint64_t has_call_edges =
3640 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3641 if (!has_call_edges)
3642 return {};
3643
3644 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3645 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3646 function_die.GetPubname());
3647
3648 // Scan the DIE for TAG_call_site entries.
3649 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3650 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3651 // For now, assume that all entries are nested directly under the subprogram
3652 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3653 std::vector<CallEdge> call_edges;
3654 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3655 child = child.GetSibling()) {
3656 if (child.Tag() != DW_TAG_call_site)
3657 continue;
3658
3659 // Extract DW_AT_call_origin (the call target's DIE).
3660 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3661 if (!call_origin.IsValid()) {
3662 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3663 function_die.GetPubname());
3664 continue;
3665 }
3666
3667 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3668 // available. It should only ever be unavailable for tail call edges, in
3669 // which case use LLDB_INVALID_ADDRESS.
3670 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3671 LLDB_INVALID_ADDRESS);
3672
3673 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3674 call_origin.GetPubname(), return_pc);
3675 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3676 }
3677 return call_edges;
3678}
3679
3680std::vector<lldb_private::CallEdge>
3681SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003682 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003683 if (func_die.IsValid())
3684 return CollectCallEdges(func_die);
3685 return {};
3686}
3687
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003688// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003689ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3690
3691uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3692
Pavel Labath9337b412018-06-06 11:35:23 +00003693void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003694
Zachary Turner49110232018-11-05 17:40:28 +00003695void SymbolFileDWARF::DumpClangAST(Stream &s) {
3696 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3697 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3698 if (!clang)
3699 return;
3700 clang->Dump(s);
3701}
3702
Kate Stoneb9c1b512016-09-06 20:57:50 +00003703SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003704 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003705 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3706 if (module_sp) {
3707 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3708 if (sym_vendor)
3709 m_debug_map_symfile =
3710 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003712 }
3713 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003714}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003715
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003716DWARFExpression::LocationListFormat
3717SymbolFileDWARF::GetLocationListFormat() const {
3718 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003719 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003721}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003722
3723SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3724 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003725 ModuleSpec module_spec;
3726 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3727 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003728 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003729
3730 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3731 FileSpec dwp_filespec =
3732 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003733 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003734 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3735 dwp_filespec);
3736 }
3737 });
3738 return m_dwp_symfile.get();
3739}