blob: 584f5a2f268ce47950d4bb66125b1fda6765e59e [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"
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +000032#include "lldb/Host/Symbols.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000033
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000034#include "lldb/Interpreter/OptionValueFileSpecList.h"
35#include "lldb/Interpreter/OptionValueProperties.h"
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000038#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000039#include "lldb/Symbol/ClangUtil.h"
40#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000041#include "lldb/Symbol/CompilerDecl.h"
42#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000043#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000044#include "lldb/Symbol/LineTable.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"
52
Pavel Labathb13f0332018-05-21 14:12:52 +000053#include "AppleDWARFIndex.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000054#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000055#include "DWARFASTParserClang.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000056#include "DWARFDIECollection.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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075#include <map>
76
Matthew Gardinere81df3b2014-08-26 06:57:23 +000077#include <ctype.h>
78#include <string.h>
79
Greg Clayton62742b12010-11-11 01:09:45 +000080//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000081
82#ifdef ENABLE_DEBUG_PRINTF
83#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000084#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000085#else
86#define DEBUG_PRINTF(fmt, ...)
87#endif
88
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089using namespace lldb;
90using namespace lldb_private;
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092// static inline bool
93// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000094//{
95// switch (tag)
96// {
97// default:
98// break;
99// case DW_TAG_subprogram:
100// case DW_TAG_inlined_subroutine:
101// case DW_TAG_class_type:
102// case DW_TAG_structure_type:
103// case DW_TAG_union_type:
104// return true;
105// }
106// return false;
107//}
108//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000109
110namespace {
111
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000112static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000114 {},
Pavel Labath9337b412018-06-06 11:35:23 +0000115 "If the DW_AT_comp_dir matches any of these paths the symbolic "
116 "links will be resolved at DWARF parse time."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000117 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
Pavel Labath9337b412018-06-06 11:35:23 +0000118 "Ignore indexes present in the object files and always index DWARF "
Tatyana Krasnukhae40db052018-09-27 07:11:58 +0000119 "manually."}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000120
Pavel Labath9337b412018-06-06 11:35:23 +0000121enum {
122 ePropertySymLinkPaths,
123 ePropertyIgnoreIndexes,
124};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126class PluginProperties : public Properties {
127public:
128 static ConstString GetSettingName() {
129 return SymbolFileDWARF::GetPluginNameStatic();
130 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 PluginProperties() {
133 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
134 m_collection_sp->Initialize(g_properties);
135 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 FileSpecList &GetSymLinkPaths() {
138 OptionValueFileSpecList *option_value =
139 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
140 nullptr, true, ePropertySymLinkPaths);
141 assert(option_value);
142 return option_value->GetCurrentValue();
143 }
Pavel Labath9337b412018-06-06 11:35:23 +0000144
145 bool IgnoreFileIndexes() const {
146 return m_collection_sp->GetPropertyAtIndexAsBoolean(
147 nullptr, ePropertyIgnoreIndexes, false);
148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
154 static const auto g_settings_sp(std::make_shared<PluginProperties>());
155 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000159
Pavel Labath7d36d722019-01-16 12:30:41 +0000160const FileSpecList &SymbolFileDWARF::GetSymlinkPaths() {
161 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000162}
163
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000164DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000165 return nullptr;
166}
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168void SymbolFileDWARF::Initialize() {
169 LogChannelDWARF::Initialize();
170 PluginManager::RegisterPlugin(GetPluginNameStatic(),
171 GetPluginDescriptionStatic(), CreateInstance,
172 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
176 if (!PluginManager::GetSettingForSymbolFilePlugin(
177 debugger, PluginProperties::GetSettingName())) {
178 const bool is_global_setting = true;
179 PluginManager::CreateSettingForSymbolFilePlugin(
180 debugger, GetGlobalPluginProperties()->GetValueProperties(),
181 ConstString("Properties for the dwarf symbol-file plug-in."),
182 is_global_setting);
183 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184}
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186void SymbolFileDWARF::Terminate() {
187 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000188 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189}
190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
192 static ConstString g_name("dwarf");
193 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194}
195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
197 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
201 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000205 // This method can be called without going through the symbol vendor so we
206 // need to lock the module.
207 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
209 if (debug_map_symfile)
210 return debug_map_symfile->GetTypeList();
211 else
212 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000213}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
215 dw_offset_t max_die_offset, uint32_t type_mask,
216 TypeSet &type_set) {
217 if (die) {
218 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 if (die_offset >= max_die_offset)
221 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 if (die_offset >= min_die_offset) {
224 const dw_tag_t tag = die.Tag();
225
226 bool add_type = false;
227
228 switch (tag) {
229 case DW_TAG_array_type:
230 add_type = (type_mask & eTypeClassArray) != 0;
231 break;
232 case DW_TAG_unspecified_type:
233 case DW_TAG_base_type:
234 add_type = (type_mask & eTypeClassBuiltin) != 0;
235 break;
236 case DW_TAG_class_type:
237 add_type = (type_mask & eTypeClassClass) != 0;
238 break;
239 case DW_TAG_structure_type:
240 add_type = (type_mask & eTypeClassStruct) != 0;
241 break;
242 case DW_TAG_union_type:
243 add_type = (type_mask & eTypeClassUnion) != 0;
244 break;
245 case DW_TAG_enumeration_type:
246 add_type = (type_mask & eTypeClassEnumeration) != 0;
247 break;
248 case DW_TAG_subroutine_type:
249 case DW_TAG_subprogram:
250 case DW_TAG_inlined_subroutine:
251 add_type = (type_mask & eTypeClassFunction) != 0;
252 break;
253 case DW_TAG_pointer_type:
254 add_type = (type_mask & eTypeClassPointer) != 0;
255 break;
256 case DW_TAG_rvalue_reference_type:
257 case DW_TAG_reference_type:
258 add_type = (type_mask & eTypeClassReference) != 0;
259 break;
260 case DW_TAG_typedef:
261 add_type = (type_mask & eTypeClassTypedef) != 0;
262 break;
263 case DW_TAG_ptr_to_member_type:
264 add_type = (type_mask & eTypeClassMemberPointer) != 0;
265 break;
266 }
267
268 if (add_type) {
269 const bool assert_not_being_parsed = true;
270 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
271 if (type) {
272 if (type_set.find(type) == type_set.end())
273 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000274 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277
278 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
279 child_die = child_die.GetSibling()) {
280 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
281 }
282 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000283}
284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000286 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000287
288{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000289 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000293 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 if (sc_scope)
295 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 if (comp_unit) {
298 dwarf_cu = GetDWARFCompileUnit(comp_unit);
299 if (dwarf_cu == 0)
300 return 0;
301 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
302 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
303 } else {
304 DWARFDebugInfo *info = DebugInfo();
305 if (info) {
306 const size_t num_cus = info->GetNumCompileUnits();
307 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
308 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
309 if (dwarf_cu) {
310 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000311 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 }
315
316 std::set<CompilerType> compiler_type_set;
317 size_t num_types_added = 0;
318 for (Type *type : type_set) {
319 CompilerType compiler_type = type->GetForwardCompilerType();
320 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
321 compiler_type_set.insert(compiler_type);
322 type_list.Insert(type->shared_from_this());
323 ++num_types_added;
324 }
325 }
326 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000327}
328
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329//----------------------------------------------------------------------
330// Gets the first parent that is a lexical block, function or inlined
331// subroutine, or compile unit.
332//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000333DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
335 DWARFDIE die;
336 for (die = child_die.GetParent(); die; die = die.GetParent()) {
337 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 switch (tag) {
340 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000341 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 case DW_TAG_subprogram:
343 case DW_TAG_inlined_subroutine:
344 case DW_TAG_lexical_block:
345 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 }
348 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000352 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
353 << 32), // Used by SymbolFileDWARFDebugMap to
354 // when this class parses .o files to
355 // contain the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
357 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
358 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
George Rimar6e357122018-10-10 08:11:15 +0000359 m_data_debug_ranges(), m_data_debug_rnglists(), m_data_debug_str(),
360 m_data_apple_names(), m_data_apple_types(), m_data_apple_namespaces(),
361 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
367static const ConstString &GetDWARFMachOSegmentName() {
368 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 ModuleSP module_sp(m_obj_file->GetModule());
396 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000397 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000398 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000402 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 }
404
Pavel Labath9337b412018-06-06 11:35:23 +0000405 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
406 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
407 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
408 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
409 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
410 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411
Pavel Labath9337b412018-06-06 11:35:23 +0000412 m_index = AppleDWARFIndex::Create(
413 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
414 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415
Pavel Labath9337b412018-06-06 11:35:23 +0000416 if (m_index)
417 return;
418
419 DWARFDataExtractor debug_names;
420 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
421 if (debug_names.GetByteSize() > 0) {
422 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
423 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
424 debug_names, get_debug_str_data(),
425 DebugInfo());
426 if (index_or) {
427 m_index = std::move(*index_or);
428 return;
429 }
430 LLDB_LOG_ERROR(log, index_or.takeError(),
431 "Unable to read .debug_names data: {0}");
432 }
433 }
434
435 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
436 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437}
438
439bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000440 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441}
442
443uint32_t SymbolFileDWARF::CalculateAbilities() {
444 uint32_t abilities = 0;
445 if (m_obj_file != NULL) {
446 const Section *section = NULL;
447 const SectionList *section_list = m_obj_file->GetSectionList();
448 if (section_list == NULL)
449 return 0;
450
Adrian Prantl05097242018-04-30 16:49:04 +0000451 // On non Apple platforms we might have .debug_types debug info that is
452 // created by using "-fdebug-types-section". LLDB currently will try to
453 // load this debug info, but it causes crashes during debugging when types
454 // are missing since it doesn't know how to parse the info in the
455 // .debug_types type units. This causes all complex debug info types to be
456 // unresolved. Because this causes LLDB to crash and since it really
457 // doesn't provide a solid debuggiung experience, we should disable trying
458 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000459 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
460 m_obj_file->GetModule()->ReportWarning(
461 "lldb doesn’t support .debug_types debug info");
462 return 0;
463 }
464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 uint64_t debug_abbrev_file_size = 0;
466 uint64_t debug_info_file_size = 0;
467 uint64_t debug_line_file_size = 0;
468
469 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
470
471 if (section)
472 section_list = &section->GetChildren();
473
474 section =
475 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
476 if (section != NULL) {
477 debug_info_file_size = section->GetFileSize();
478
479 section =
480 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
481 .get();
482 if (section)
483 debug_abbrev_file_size = section->GetFileSize();
484
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000485 DWARFDebugAbbrev *abbrev = DebugAbbrev();
486 if (abbrev) {
487 std::set<dw_form_t> invalid_forms;
488 abbrev->GetUnsupportedForms(invalid_forms);
489 if (!invalid_forms.empty()) {
490 StreamString error;
491 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
492 for (auto form : invalid_forms)
493 error.Printf(" %#x", form);
494 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
495 return 0;
496 }
497 }
498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 section =
500 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
501 .get();
502 if (section)
503 debug_line_file_size = section->GetFileSize();
504 } else {
505 const char *symfile_dir_cstr =
506 m_obj_file->GetFileSpec().GetDirectory().GetCString();
507 if (symfile_dir_cstr) {
508 if (strcasestr(symfile_dir_cstr, ".dsym")) {
509 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000510 // We have a dSYM file that didn't have a any debug info. If the
511 // string table has a size of 1, then it was made from an
512 // executable with no debug info, or from an executable that was
513 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 section =
515 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
516 .get();
517 if (section && section->GetFileSize() == 1) {
518 m_obj_file->GetModule()->ReportWarning(
519 "empty dSYM file detected, dSYM was created with an "
520 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526
527 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
528 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
529 LocalVariables | VariableTypes;
530
531 if (debug_line_file_size > 0)
532 abilities |= LineTables;
533 }
534 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537const DWARFDataExtractor &
538SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
539 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000540 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
541 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
542 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544}
545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
547 DWARFDataExtractor &data) {
548 ModuleSP module_sp(m_obj_file->GetModule());
549 const SectionList *section_list = module_sp->GetSectionList();
550 if (section_list) {
551 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
552 if (section_sp) {
553 // See if we memory mapped the DWARF segment?
554 if (m_dwarf_data.GetByteSize()) {
555 data.SetData(m_dwarf_data, section_sp->GetOffset(),
556 section_sp->GetFileSize());
557 } else {
558 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
559 data.Clear();
560 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563}
564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
566 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
567 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
571 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
572}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
575 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
576 m_data_debug_aranges);
577}
578
579const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
580 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
581}
582
583const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
584 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
585}
586
587const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
588 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
589}
590
George Rimarc6c7bfc2018-09-13 17:06:47 +0000591const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
592 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
593}
594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
596 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
597}
598
George Rimare4dee262018-10-23 09:46:15 +0000599const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
600 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
601 if (debugLocData.GetByteSize() > 0)
602 return debugLocData;
603 return get_debug_loclists_data();
604}
605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
607 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
608}
609
George Rimare4dee262018-10-23 09:46:15 +0000610const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
611 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
612 m_data_debug_loclists);
613}
614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
616 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
617 m_data_debug_ranges);
618}
619
George Rimar6e357122018-10-10 08:11:15 +0000620const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
621 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
622 m_data_debug_rnglists);
623}
624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
626 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
627}
628
629const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
630 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
631 m_data_debug_str_offsets);
632}
633
Greg Clayton2550ca12018-05-08 17:19:24 +0000634const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
635 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
636}
637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
639 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
640}
641
642const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
643 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
644}
645
646const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
647 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
648 m_data_apple_namespaces);
649}
650
651const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
652 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
653}
654
Jan Kratochvile4777a92018-04-29 19:47:48 +0000655const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
656 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
657 m_data_gnu_debugaltlink);
658}
659
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
661 if (m_abbr.get() == NULL) {
662 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
663 if (debug_abbrev_data.GetByteSize() > 0) {
664 m_abbr.reset(new DWARFDebugAbbrev());
665 if (m_abbr.get())
666 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 }
669 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670}
671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
673 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674}
675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
677 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000678 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
679 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
680 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 if (get_debug_info_data().GetByteSize() > 0) {
682 m_info.reset(new DWARFDebugInfo());
683 if (m_info.get()) {
684 m_info->SetDwarfData(this);
685 }
Greg Clayton1f746072012-08-29 21:13:06 +0000686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 }
688 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689}
690
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
692 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693}
694
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000695DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
697 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000698 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699
700 DWARFDebugInfo *info = DebugInfo();
701 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000702 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
703 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000705 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
707 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
708 dwarf_cu->SetUserData(comp_unit);
709 return dwarf_cu;
710 }
711 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712}
713
George Rimar7e7f9c12018-10-25 10:25:45 +0000714DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000716 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
717 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
718 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000719
720 if (get_debug_ranges_data().GetByteSize() > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000722 else if (get_debug_rnglists_data().GetByteSize() > 0)
723 m_ranges.reset(new DWARFDebugRngLists());
724
725 if (m_ranges.get())
726 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 }
728 return m_ranges.get();
729}
730
George Rimar7e7f9c12018-10-25 10:25:45 +0000731const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 return m_ranges.get();
733}
734
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000735lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 uint32_t cu_idx) {
737 CompUnitSP cu_sp;
738 if (dwarf_cu) {
739 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
740 if (comp_unit) {
741 // We already parsed this compile unit, had out a shared pointer to it
742 cu_sp = comp_unit->shared_from_this();
743 } else {
744 if (dwarf_cu->GetSymbolFileDWARF() != this) {
745 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
746 cu_idx);
747 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
748 // Let the debug map create the compile unit
749 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
750 dwarf_cu->SetUserData(cu_sp.get());
751 } else {
752 ModuleSP module_sp(m_obj_file->GetModule());
753 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000754 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 if (cu_die) {
Pavel Labath7d36d722019-01-16 12:30:41 +0000756 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 if (cu_file_spec) {
758 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000759 // resolve the file. This can be expensive e.g. when the source
Pavel Labath7d36d722019-01-16 12:30:41 +0000760 // files are NFS mounted.
761 cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762
763 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000764 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000766 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 }
768
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000769 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
771
772 bool is_optimized = dwarf_cu->GetIsOptimized();
773 cu_sp.reset(new CompileUnit(
774 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
775 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
776 if (cu_sp) {
777 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000778 // try and get the first entry in the supports files from the
779 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 if (!cu_file_spec) {
781 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
782 if (cu_file_spec) {
783 (FileSpec &)(*cu_sp) = cu_file_spec;
784 // Also fix the invalid file spec which was copied from the
785 // compile unit.
786 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
787 }
788 }
789
790 dwarf_cu->SetUserData(cu_sp.get());
791
792 // Figure out the compile unit index if we weren't given one
793 if (cu_idx == UINT32_MAX)
794 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
795
796 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
797 cu_idx, cu_sp);
798 }
799 }
800 }
801 }
802 }
803 }
804 return cu_sp;
805}
806
807uint32_t SymbolFileDWARF::GetNumCompileUnits() {
808 DWARFDebugInfo *info = DebugInfo();
809 if (info)
810 return info->GetNumCompileUnits();
811 return 0;
812}
813
814CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000815 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 CompUnitSP cu_sp;
817 DWARFDebugInfo *info = DebugInfo();
818 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000819 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 if (dwarf_cu)
821 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
822 }
823 return cu_sp;
824}
825
Zachary Turner863f8c12019-01-11 18:03:20 +0000826Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
827 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000828 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 if (die.IsValid()) {
830 TypeSystem *type_system =
831 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
832
833 if (type_system) {
834 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
835 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000836 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 }
838 }
839 return nullptr;
840}
841
842bool SymbolFileDWARF::FixupAddress(Address &addr) {
843 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
844 if (debug_map_symfile) {
845 return debug_map_symfile->LinkOSOAddress(addr);
846 }
847 // This is a normal DWARF file, no address fixups need to happen
848 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000849}
Zachary Turner863f8c12019-01-11 18:03:20 +0000850lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000851 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000852 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 if (dwarf_cu)
854 return dwarf_cu->GetLanguageType();
855 else
856 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000857}
858
Zachary Turner863f8c12019-01-11 18:03:20 +0000859size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000860 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 size_t functions_added = 0;
Zachary Turner863f8c12019-01-11 18:03:20 +0000862 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 if (dwarf_cu) {
864 DWARFDIECollection function_dies;
865 const size_t num_functions =
866 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
867 size_t func_idx;
868 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
869 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
Zachary Turner863f8c12019-01-11 18:03:20 +0000870 if (comp_unit.FindFunctionByUID(die.GetID()).get() == NULL) {
871 if (ParseFunction(comp_unit, die))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 ++functions_added;
873 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 // FixupTypes();
876 }
877 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878}
879
Zachary Turner863f8c12019-01-11 18:03:20 +0000880bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
881 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000882 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000883 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000885 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887 if (cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
889 DW_AT_stmt_list, DW_INVALID_OFFSET);
890 if (stmt_list != DW_INVALID_OFFSET) {
891 // All file indexes in DWARF are one based and a file of index zero is
892 // supposed to be the compile unit itself.
Zachary Turner863f8c12019-01-11 18:03:20 +0000893 support_files.Append(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 return DWARFDebugLine::ParseSupportFiles(
Pavel Labath7d36d722019-01-16 12:30:41 +0000895 comp_unit.GetModule(), get_debug_line_data(), stmt_list,
896 support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 }
900 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901}
902
Zachary Turner863f8c12019-01-11 18:03:20 +0000903bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000904 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000905 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 if (dwarf_cu)
907 return dwarf_cu->GetIsOptimized();
908 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000909}
910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911bool SymbolFileDWARF::ParseImportedModules(
912 const lldb_private::SymbolContext &sc,
913 std::vector<lldb_private::ConstString> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000914 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000916 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 if (dwarf_cu) {
918 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
919 sc.comp_unit->GetLanguage())) {
920 UpdateExternalModuleListIfNeeded();
921
922 if (sc.comp_unit) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000923 const DWARFDIE die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924
925 if (die) {
926 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
927 child_die = child_die.GetSibling()) {
928 if (child_die.Tag() == DW_TAG_imported_declaration) {
929 if (DWARFDIE module_die =
930 child_die.GetReferencedDIE(DW_AT_import)) {
931 if (module_die.Tag() == DW_TAG_module) {
932 if (const char *name = module_die.GetAttributeValueAsString(
933 DW_AT_name, nullptr)) {
934 ConstString const_name(name);
935 imported_modules.push_back(const_name);
936 }
Sean Callananb0300a42016-01-14 21:46:09 +0000937 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 }
Sean Callananb0300a42016-01-14 21:46:09 +0000939 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942 } else {
943 for (const auto &pair : m_external_type_modules) {
944 imported_modules.push_back(pair.first);
945 }
946 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000947 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 }
949 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000950}
951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952struct ParseDWARFLineTableCallbackInfo {
953 LineTable *line_table;
954 std::unique_ptr<LineSequence> sequence_ap;
955 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956};
957
958//----------------------------------------------------------------------
959// ParseStatementTableCallback
960//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961static void ParseDWARFLineTableCallback(dw_offset_t offset,
962 const DWARFDebugLine::State &state,
963 void *userData) {
964 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
965 // Just started parsing the line table
966 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
967 // Done parsing line table, nothing to do for the cleanup
968 } else {
969 ParseDWARFLineTableCallbackInfo *info =
970 (ParseDWARFLineTableCallbackInfo *)userData;
971 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972
Adrian Prantl05097242018-04-30 16:49:04 +0000973 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 if (!info->sequence_ap.get()) {
975 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
976 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 line_table->AppendLineEntryToSequence(
979 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
980 state.column, state.file, state.is_stmt, state.basic_block,
981 state.prologue_end, state.epilogue_begin, state.end_sequence);
982 if (state.end_sequence) {
983 // First, put the current sequence into the line table.
984 line_table->InsertSequence(info->sequence_ap.get());
985 // Then, empty it to prepare for the next sequence.
986 info->sequence_ap->Clear();
987 }
988 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989}
990
Zachary Turner863f8c12019-01-11 18:03:20 +0000991bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000992 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000993 if (comp_unit.GetLineTable() != NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995
Zachary Turner863f8c12019-01-11 18:03:20 +0000996 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000998 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 if (dwarf_cu_die) {
1000 const dw_offset_t cu_line_offset =
1001 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1002 DW_INVALID_OFFSET);
1003 if (cu_line_offset != DW_INVALID_OFFSET) {
Zachary Turner863f8c12019-01-11 18:03:20 +00001004 std::unique_ptr<LineTable> line_table_ap(new LineTable(&comp_unit));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 if (line_table_ap.get()) {
1006 ParseDWARFLineTableCallbackInfo info;
1007 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001008
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 /*
1010 * MIPS:
1011 * The SymbolContext may not have a valid target, thus we may not be
1012 * able
1013 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1014 * #0
1015 * for MIPS. Use ArchSpec to clear the bit #0.
1016 */
Pavel Labathf760f5a2019-01-03 10:37:19 +00001017 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 case llvm::Triple::mips:
1019 case llvm::Triple::mipsel:
1020 case llvm::Triple::mips64:
1021 case llvm::Triple::mips64el:
1022 info.addr_mask = ~((lldb::addr_t)1);
1023 break;
1024 default:
1025 info.addr_mask = ~((lldb::addr_t)0);
1026 break;
1027 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001028
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 lldb::offset_t offset = cu_line_offset;
1030 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1031 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001032 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1034 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001035 // We have an object file that has a line table with addresses that
1036 // are not linked. We need to link the line table and convert the
1037 // addresses that are relative to the .o file into addresses for
1038 // the main executable.
Zachary Turner863f8c12019-01-11 18:03:20 +00001039 comp_unit.SetLineTable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1041 } else {
Zachary Turner863f8c12019-01-11 18:03:20 +00001042 comp_unit.SetLineTable(line_table_ap.release());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 return true;
1044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 }
1049 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050}
1051
Siva Chandrad8335e92015-12-16 00:22:08 +00001052lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1054 auto iter = m_debug_macros_map.find(*offset);
1055 if (iter != m_debug_macros_map.end())
1056 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1059 if (debug_macro_data.GetByteSize() == 0)
1060 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1063 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 const DWARFDebugMacroHeader &header =
1066 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1067 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1068 header.OffsetIs64Bit(), offset, this,
1069 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001072}
1073
Zachary Turner863f8c12019-01-11 18:03:20 +00001074bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001075 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001076
Zachary Turner863f8c12019-01-11 18:03:20 +00001077 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001079 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080
Jan Kratochvil93afb052018-05-24 20:51:13 +00001081 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 if (!dwarf_cu_die)
1083 return false;
1084
1085 lldb::offset_t sect_offset =
1086 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1087 if (sect_offset == DW_INVALID_OFFSET)
1088 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1089 DW_INVALID_OFFSET);
1090 if (sect_offset == DW_INVALID_OFFSET)
1091 return false;
1092
Zachary Turner863f8c12019-01-11 18:03:20 +00001093 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094
1095 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001096}
1097
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001098size_t SymbolFileDWARF::ParseBlocksRecursive(
1099 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1100 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 size_t blocks_added = 0;
1102 DWARFDIE die = orig_die;
1103 while (die) {
1104 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001105
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 switch (tag) {
1107 case DW_TAG_inlined_subroutine:
1108 case DW_TAG_subprogram:
1109 case DW_TAG_lexical_block: {
1110 Block *block = NULL;
1111 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001112 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1113 // inlined functions. These will be parsed on their own as separate
1114 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115
1116 if (depth > 0)
1117 break;
1118
1119 block = parent_block;
1120 } else {
1121 BlockSP block_sp(new Block(die.GetID()));
1122 parent_block->AddChild(block_sp);
1123 block = block_sp.get();
1124 }
1125 DWARFRangeList ranges;
1126 const char *name = NULL;
1127 const char *mangled_name = NULL;
1128
1129 int decl_file = 0;
1130 int decl_line = 0;
1131 int decl_column = 0;
1132 int call_file = 0;
1133 int call_line = 0;
1134 int call_column = 0;
1135 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1136 decl_line, decl_column, call_file, call_line,
1137 call_column, nullptr)) {
1138 if (tag == DW_TAG_subprogram) {
1139 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1140 subprogram_low_pc = ranges.GetMinRangeBase(0);
1141 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001142 // We get called here for inlined subroutines in two ways. The first
1143 // time is when we are making the Function object for this inlined
1144 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001146 // need to adjust the containing address. The second time is when we
1147 // are parsing the blocks inside the function that contains the
1148 // inlined concrete instance. Since these will be blocks inside the
1149 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1151 subprogram_low_pc = ranges.GetMinRangeBase(0);
1152 }
1153 }
1154
1155 const size_t num_ranges = ranges.GetSize();
1156 for (size_t i = 0; i < num_ranges; ++i) {
1157 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1158 const addr_t range_base = range.GetRangeBase();
1159 if (range_base >= subprogram_low_pc)
1160 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1161 range.GetByteSize()));
1162 else {
1163 GetObjectFile()->GetModule()->ReportError(
1164 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1165 ") which has a base that is less than the function's low PC "
1166 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1167 "start of this error message",
1168 block->GetID(), range_base, range.GetRangeEnd(),
1169 subprogram_low_pc);
1170 }
1171 }
1172 block->FinalizeRanges();
1173
1174 if (tag != DW_TAG_subprogram &&
1175 (name != NULL || mangled_name != NULL)) {
1176 std::unique_ptr<Declaration> decl_ap;
1177 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1178 decl_ap.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001179 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 decl_line, decl_column));
1181
1182 std::unique_ptr<Declaration> call_ap;
1183 if (call_file != 0 || call_line != 0 || call_column != 0)
1184 call_ap.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001185 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 call_line, call_column));
1187
1188 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1189 call_ap.get());
1190 }
1191
1192 ++blocks_added;
1193
1194 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001195 blocks_added +=
1196 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1197 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 }
1199 }
1200 } break;
1201 default:
1202 break;
1203 }
1204
Adrian Prantl05097242018-04-30 16:49:04 +00001205 // Only parse siblings of the block if we are not at depth zero. A depth of
1206 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1207 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208
1209 if (depth == 0)
1210 die.Clear();
1211 else
1212 die = die.GetSibling();
1213 }
1214 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001215}
1216
Kate Stoneb9c1b512016-09-06 20:57:50 +00001217bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1218 if (parent_die) {
1219 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1220 die = die.GetSibling()) {
1221 dw_tag_t tag = die.Tag();
1222 bool check_virtuality = false;
1223 switch (tag) {
1224 case DW_TAG_inheritance:
1225 case DW_TAG_subprogram:
1226 check_virtuality = true;
1227 break;
1228 default:
1229 break;
1230 }
1231 if (check_virtuality) {
1232 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1233 return true;
1234 }
1235 }
1236 }
1237 return false;
1238}
1239
1240void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1241 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1242 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1243 std::vector<DWARFDIE> decl_ctx_die_list =
1244 ast_parser->GetDIEForDeclContext(decl_ctx);
1245
1246 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1247 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1248 decl = decl.GetSibling())
1249 ast_parser->GetDeclForUIDFromDWARF(decl);
1250}
1251
1252SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001253 // This method can be called without going through the symbol vendor so we
1254 // need to lock the module.
1255 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001256 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1257 // must make sure we use the correct DWARF file when resolving things. On
1258 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1259 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1260 // references to other DWARF objects and we must be ready to receive a
1261 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 // instance.
1263 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1264 if (debug_map)
1265 return debug_map->GetSymbolFileByOSOIndex(
1266 debug_map->GetOSOIndexFromUserID(uid));
1267 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001268}
1269
1270DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001272 // This method can be called without going through the symbol vendor so we
1273 // need to lock the module.
1274 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001275 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1276 // must make sure we use the correct DWARF file when resolving things. On
1277 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1278 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1279 // references to other DWARF objects and we must be ready to receive a
1280 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 // instance.
1282 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1283 if (dwarf)
1284 return dwarf->GetDIE(DIERef(uid, dwarf));
1285 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001286}
1287
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001289 // This method can be called without going through the symbol vendor so we
1290 // need to lock the module.
1291 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001292 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1293 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1294 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 DWARFDIE die = GetDIEFromUID(type_uid);
1296 if (die)
1297 return die.GetDecl();
1298 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001299}
1300
Greg Clayton99558cc42015-08-24 23:46:31 +00001301CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001303 // This method can be called without going through the symbol vendor so we
1304 // need to lock the module.
1305 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001306 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1307 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1308 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 DWARFDIE die = GetDIEFromUID(type_uid);
1310 if (die)
1311 return die.GetDeclContext();
1312 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001313}
1314
Greg Clayton99558cc42015-08-24 23:46:31 +00001315CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001317 // This method can be called without going through the symbol vendor so we
1318 // need to lock the module.
1319 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001320 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1321 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1322 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 DWARFDIE die = GetDIEFromUID(type_uid);
1324 if (die)
1325 return die.GetContainingDeclContext();
1326 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327}
1328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001330 // This method can be called without going through the symbol vendor so we
1331 // need to lock the module.
1332 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001333 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1334 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1335 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 DWARFDIE type_die = GetDIEFromUID(type_uid);
1337 if (type_die)
1338 return type_die.ResolveType();
1339 else
1340 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341}
1342
Adrian Prantleca07c52018-11-05 20:49:07 +00001343llvm::Optional<SymbolFile::ArrayInfo>
1344SymbolFileDWARF::GetDynamicArrayInfoForUID(
1345 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1346 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1347 DWARFDIE type_die = GetDIEFromUID(type_uid);
1348 if (type_die)
1349 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1350 else
1351 return llvm::None;
1352}
1353
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1355 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001356}
1357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1359 bool assert_not_being_parsed) {
1360 if (die) {
1361 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1362 if (log)
1363 GetObjectFile()->GetModule()->LogMessage(
1364 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1365 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001366
Adrian Prantl05097242018-04-30 16:49:04 +00001367 // We might be coming in in the middle of a type tree (a class within a
1368 // class, an enum within a class), so parse any needed parent DIEs before
1369 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1371 if (decl_ctx_die) {
1372 if (log) {
1373 switch (decl_ctx_die.Tag()) {
1374 case DW_TAG_structure_type:
1375 case DW_TAG_union_type:
1376 case DW_TAG_class_type: {
1377 // Get the type, which could be a forward declaration
1378 if (log)
1379 GetObjectFile()->GetModule()->LogMessage(
1380 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1381 "resolve parent forward type for 0x%8.8x",
1382 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1383 decl_ctx_die.GetOffset());
1384 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001385
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 default:
1387 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 return ResolveType(die);
1392 }
1393 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001394}
1395
Greg Clayton6beaaa62011-01-17 03:46:26 +00001396// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001397// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1398// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399bool SymbolFileDWARF::HasForwardDeclForClangType(
1400 const CompilerType &compiler_type) {
1401 CompilerType compiler_type_no_qualifiers =
1402 ClangUtil::RemoveFastQualifiers(compiler_type);
1403 if (GetForwardDeclClangTypeToDie().count(
1404 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1405 return true;
1406 }
1407 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 ClangASTContext *clang_type_system =
1410 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1411 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001412 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 DWARFASTParserClang *ast_parser =
1414 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1415 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001416}
1417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001419 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001420
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 ClangASTContext *clang_type_system =
1422 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1423 if (clang_type_system) {
1424 DWARFASTParserClang *ast_parser =
1425 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1426 if (ast_parser &&
1427 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1428 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1429 }
Jim Inghamc3549282012-01-11 02:21:12 +00001430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 // We have a struct/union/class/enum that needs to be fully resolved.
1432 CompilerType compiler_type_no_qualifiers =
1433 ClangUtil::RemoveFastQualifiers(compiler_type);
1434 auto die_it = GetForwardDeclClangTypeToDie().find(
1435 compiler_type_no_qualifiers.GetOpaqueQualType());
1436 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1437 // We have already resolved this type...
1438 return true;
1439 }
1440
1441 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1442 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001443 // Once we start resolving this type, remove it from the forward
1444 // declaration map in case anyone child members or other types require this
1445 // type to get resolved. The type will get resolved when all of the calls
1446 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 GetForwardDeclClangTypeToDie().erase(die_it);
1448
1449 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1450
1451 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1452 DWARF_LOG_TYPE_COMPLETION));
1453 if (log)
1454 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1455 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1456 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1457 type->GetName().AsCString());
1458 assert(compiler_type);
1459 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1460 if (dwarf_ast)
1461 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1462 }
1463 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001464}
1465
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1467 bool assert_not_being_parsed,
1468 bool resolve_function_context) {
1469 if (die) {
1470 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1471
1472 if (assert_not_being_parsed) {
1473 if (type != DIE_IS_BEING_PARSED)
1474 return type;
1475
1476 GetObjectFile()->GetModule()->ReportError(
1477 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1478 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1479
1480 } else
1481 return type;
1482 }
1483 return nullptr;
1484}
1485
1486CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001487SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 uint32_t cu_idx) {
1489 // Check if the symbol vendor already knows about this compile unit?
1490 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001491 // The symbol vendor doesn't know about this compile unit, we need to parse
1492 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1494 }
1495 return (CompileUnit *)dwarf_cu->GetUserData();
1496}
1497
1498size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1499 DIEArray &method_die_offsets) {
1500 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001501 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 return method_die_offsets.size();
1503}
1504
1505bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1506 sc.Clear(false);
1507
1508 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1511
1512 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1513 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001514 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515
1516 if (sc.function) {
1517 sc.module_sp = sc.function->CalculateSymbolContextModule();
1518 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 }
1521
1522 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523}
1524
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1526 UpdateExternalModuleListIfNeeded();
1527 const auto &pos = m_external_type_modules.find(name);
1528 if (pos != m_external_type_modules.end())
1529 return pos->second;
1530 else
1531 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001532}
1533
Greg Clayton2f869fe2016-03-30 20:14:35 +00001534DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1536 DWARFDebugInfo *debug_info = DebugInfo();
1537 if (debug_info)
1538 return debug_info->GetDIE(die_ref);
1539 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001540 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001541}
1542
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543std::unique_ptr<SymbolFileDWARFDwo>
1544SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001545 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001547 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 // info files for modules.
1549 if (GetDebugMapSymfile())
1550 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001551
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 const char *dwo_name = cu_die.GetAttributeValueAsString(
1553 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1554 if (!dwo_name)
1555 return nullptr;
1556
Tamas Berghammer963ce482017-08-25 13:56:14 +00001557 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1558 if (dwp_symfile) {
1559 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1560 DW_AT_GNU_dwo_id, 0);
1561 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1562 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1563 if (dwo_symfile)
1564 return dwo_symfile;
1565 }
1566
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001567 FileSpec dwo_file(dwo_name);
1568 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 if (dwo_file.IsRelative()) {
1570 const char *comp_dir = cu_die.GetAttributeValueAsString(
1571 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1572 if (!comp_dir)
1573 return nullptr;
1574
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001575 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1576 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 dwo_file.AppendPathComponent(dwo_name);
1578 }
1579
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001580 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 return nullptr;
1582
1583 const lldb::offset_t file_offset = 0;
1584 DataBufferSP dwo_file_data_sp;
1585 lldb::offset_t dwo_file_data_offset = 0;
1586 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1587 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001588 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1589 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 if (dwo_obj_file == nullptr)
1591 return nullptr;
1592
1593 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001594}
1595
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1597 if (m_fetched_external_modules)
1598 return;
1599 m_fetched_external_modules = true;
1600
1601 DWARFDebugInfo *debug_info = DebugInfo();
1602
1603 const uint32_t num_compile_units = GetNumCompileUnits();
1604 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001605 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606
Jan Kratochvil93afb052018-05-24 20:51:13 +00001607 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001608 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1610
1611 if (name) {
1612 ConstString const_name(name);
1613 if (m_external_type_modules.find(const_name) ==
1614 m_external_type_modules.end()) {
1615 ModuleSP module_sp;
1616 const char *dwo_path =
1617 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1618 if (dwo_path) {
1619 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001620 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001621 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1623 const char *comp_dir =
1624 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1625 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001626 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001627 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001628 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1630 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001631 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 dwo_module_spec.GetArchitecture() =
1633 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001634
Adrian Prantl05097242018-04-30 16:49:04 +00001635 // When LLDB loads "external" modules it looks at the presence of
1636 // DW_AT_GNU_dwo_name. However, when the already created module
1637 // (corresponding to .dwo itself) is being processed, it will see
1638 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1639 // dwo file) and will try to call ModuleList::GetSharedModule
1640 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1641 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1642 // DW_AT_comp_dir. In this case the method
1643 // ModuleList::GetSharedModule will fail and the warning will be
1644 // printed. However, as one can notice in this case we don't
1645 // actually need to try to load the already loaded module
1646 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001647 if (m_obj_file->GetFileSpec()
1648 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001649 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001650 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1651 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1652 continue;
1653 }
1654
Zachary Turner97206d52017-05-12 04:51:55 +00001655 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 dwo_module_spec, module_sp, NULL, NULL, NULL);
1657 if (!module_sp) {
1658 GetObjectFile()->GetModule()->ReportWarning(
1659 "0x%8.8x: unable to locate module needed for external types: "
1660 "%s\nerror: %s\nDebugging will be degraded due to missing "
1661 "types. Rebuilding your project will regenerate the needed "
1662 "module files.",
1663 die.GetOffset(),
1664 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1665 error.AsCString("unknown error"));
1666 }
1667 }
1668 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001669 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 }
1673}
1674
1675SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1676 if (!m_global_aranges_ap) {
1677 m_global_aranges_ap.reset(new GlobalVariableMap());
1678
1679 ModuleSP module_sp = GetObjectFile()->GetModule();
1680 if (module_sp) {
1681 const size_t num_cus = module_sp->GetNumCompileUnits();
1682 for (size_t i = 0; i < num_cus; ++i) {
1683 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1684 if (cu_sp) {
1685 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1686 if (globals_sp) {
1687 const size_t num_globals = globals_sp->GetSize();
1688 for (size_t g = 0; g < num_globals; ++g) {
1689 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1690 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1691 const DWARFExpression &location = var_sp->LocationExpression();
1692 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001693 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001694 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1695 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 if (location_result.GetValueType() ==
1697 Value::eValueTypeFileAddress) {
1698 lldb::addr_t file_addr =
1699 location_result.GetScalar().ULongLong();
1700 lldb::addr_t byte_size = 1;
1701 if (var_sp->GetType())
1702 byte_size = var_sp->GetType()->GetByteSize();
1703 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1704 file_addr, byte_size, var_sp.get()));
1705 }
1706 }
1707 }
1708 }
1709 }
1710 }
1711 }
1712 }
1713 m_global_aranges_ap->Sort();
1714 }
1715 return *m_global_aranges_ap;
1716}
1717
1718uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001719 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001721 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1722 Timer scoped_timer(func_cat,
1723 "SymbolFileDWARF::"
1724 "ResolveSymbolContext (so_addr = { "
1725 "section = %p, offset = 0x%" PRIx64
1726 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727 static_cast<void *>(so_addr.GetSection().get()),
1728 so_addr.GetOffset(), resolve_scope);
1729 uint32_t resolved = 0;
1730 if (resolve_scope &
1731 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1732 eSymbolContextLineEntry | eSymbolContextVariable)) {
1733 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1734
1735 DWARFDebugInfo *debug_info = DebugInfo();
1736 if (debug_info) {
1737 const dw_offset_t cu_offset =
1738 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1739 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001740 // Global variables are not in the compile unit address ranges. The
1741 // only way to currently find global variables is to iterate over the
1742 // .debug_pubnames or the __apple_names table and find all items in
1743 // there that point to DW_TAG_variable DIEs and then find the address
1744 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745 if (resolve_scope & eSymbolContextVariable) {
1746 GlobalVariableMap &map = GetGlobalAranges();
1747 const GlobalVariableMap::Entry *entry =
1748 map.FindEntryThatContains(file_vm_addr);
1749 if (entry && entry->data) {
1750 Variable *variable = entry->data;
1751 SymbolContextScope *scc = variable->GetSymbolContextScope();
1752 if (scc) {
1753 scc->CalculateSymbolContext(&sc);
1754 sc.variable = variable;
1755 }
1756 return sc.GetResolvedMask();
1757 }
1758 }
1759 } else {
1760 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001761 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1763 if (dwarf_cu) {
1764 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1765 if (sc.comp_unit) {
1766 resolved |= eSymbolContextCompUnit;
1767
1768 bool force_check_line_table = false;
1769 if (resolve_scope &
1770 (eSymbolContextFunction | eSymbolContextBlock)) {
1771 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1772 DWARFDIE block_die;
1773 if (function_die) {
1774 sc.function =
1775 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1776 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001777 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001778
1779 if (sc.function && (resolve_scope & eSymbolContextBlock))
1780 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1781 } else {
1782 // We might have had a compile unit that had discontiguous
1783 // address ranges where the gaps are symbols that don't have
1784 // any debug info. Discontiguous compile unit address ranges
1785 // should only happen when there aren't other functions from
1786 // other compile units in these gaps. This helps keep the size
1787 // of the aranges down.
1788 force_check_line_table = true;
1789 }
1790
1791 if (sc.function != NULL) {
1792 resolved |= eSymbolContextFunction;
1793
1794 if (resolve_scope & eSymbolContextBlock) {
1795 Block &block = sc.function->GetBlock(true);
1796
1797 if (block_die)
1798 sc.block = block.FindBlockByID(block_die.GetID());
1799 else
1800 sc.block = block.FindBlockByID(function_die.GetID());
1801 if (sc.block)
1802 resolved |= eSymbolContextBlock;
1803 }
1804 }
1805 }
1806
1807 if ((resolve_scope & eSymbolContextLineEntry) ||
1808 force_check_line_table) {
1809 LineTable *line_table = sc.comp_unit->GetLineTable();
1810 if (line_table != NULL) {
1811 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001812 // terms of this debug file if there is no debug map, or it
1813 // will be an address in the .o file which needs to be fixed up
1814 // to be in terms of the debug map executable. Either way,
1815 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 Address exe_so_addr(so_addr);
1817 if (FixupAddress(exe_so_addr)) {
1818 if (line_table->FindLineEntryByAddress(exe_so_addr,
1819 sc.line_entry)) {
1820 resolved |= eSymbolContextLineEntry;
1821 }
1822 }
1823 }
1824 }
1825
1826 if (force_check_line_table &&
1827 !(resolved & eSymbolContextLineEntry)) {
1828 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001829 // address ranges where the gaps are symbols that don't have any
1830 // debug info. Discontiguous compile unit address ranges should
1831 // only happen when there aren't other functions from other
1832 // compile units in these gaps. This helps keep the size of the
1833 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834 sc.comp_unit = NULL;
1835 resolved &= ~eSymbolContextCompUnit;
1836 }
1837 } else {
1838 GetObjectFile()->GetModule()->ReportWarning(
1839 "0x%8.8x: compile unit %u failed to create a valid "
1840 "lldb_private::CompileUnit class.",
1841 cu_offset, cu_idx);
1842 }
1843 }
1844 }
1845 }
1846 }
1847 return resolved;
1848}
1849
1850uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1851 uint32_t line,
1852 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001853 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854 SymbolContextList &sc_list) {
1855 const uint32_t prev_size = sc_list.GetSize();
1856 if (resolve_scope & eSymbolContextCompUnit) {
1857 DWARFDebugInfo *debug_info = DebugInfo();
1858 if (debug_info) {
1859 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001860 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861
1862 for (cu_idx = 0;
1863 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1864 ++cu_idx) {
1865 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1866 const bool full_match = (bool)file_spec.GetDirectory();
1867 bool file_spec_matches_cu_file_spec =
1868 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1869 if (check_inlines || file_spec_matches_cu_file_spec) {
1870 SymbolContext sc(m_obj_file->GetModule());
1871 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1872 if (sc.comp_unit) {
1873 uint32_t file_idx = UINT32_MAX;
1874
Adrian Prantl05097242018-04-30 16:49:04 +00001875 // If we are looking for inline functions only and we don't find it
1876 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877 if (check_inlines) {
1878 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1879 1, file_spec, true);
1880 if (file_idx == UINT32_MAX)
1881 continue;
1882 }
1883
1884 if (line != 0) {
1885 LineTable *line_table = sc.comp_unit->GetLineTable();
1886
1887 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001888 // We will have already looked up the file index if we are
1889 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 if (!check_inlines)
1891 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1892 1, file_spec, true);
1893
1894 if (file_idx != UINT32_MAX) {
1895 uint32_t found_line;
1896 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1897 0, file_idx, line, false, &sc.line_entry);
1898 found_line = sc.line_entry.line;
1899
1900 while (line_idx != UINT32_MAX) {
1901 sc.function = NULL;
1902 sc.block = NULL;
1903 if (resolve_scope &
1904 (eSymbolContextFunction | eSymbolContextBlock)) {
1905 const lldb::addr_t file_vm_addr =
1906 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1907 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1908 DWARFDIE function_die =
1909 dwarf_cu->LookupAddress(file_vm_addr);
1910 DWARFDIE block_die;
1911 if (function_die) {
1912 sc.function =
1913 sc.comp_unit
1914 ->FindFunctionByUID(function_die.GetID())
1915 .get();
1916 if (sc.function == NULL)
1917 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001918 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919
1920 if (sc.function &&
1921 (resolve_scope & eSymbolContextBlock))
1922 block_die =
1923 function_die.LookupDeepestBlock(file_vm_addr);
1924 }
1925
1926 if (sc.function != NULL) {
1927 Block &block = sc.function->GetBlock(true);
1928
1929 if (block_die)
1930 sc.block = block.FindBlockByID(block_die.GetID());
1931 else if (function_die)
1932 sc.block =
1933 block.FindBlockByID(function_die.GetID());
1934 }
1935 }
1936 }
1937
1938 sc_list.Append(sc);
1939 line_idx = line_table->FindLineEntryIndexByFileIndex(
1940 line_idx + 1, file_idx, found_line, true,
1941 &sc.line_entry);
1942 }
1943 }
1944 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1945 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001946 // sites by file and line and if the file spec matches that of
1947 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001948 sc_list.Append(sc);
1949 }
1950 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1951 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001952 // sites by file and line and if the file spec matches that of
1953 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954 sc_list.Append(sc);
1955 }
1956
1957 if (!check_inlines)
1958 break;
1959 }
1960 }
1961 }
1962 }
1963 }
1964 return sc_list.GetSize() - prev_size;
1965}
1966
Jim Ingham7fca8c02017-04-28 00:51:06 +00001967void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001968 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001969 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970}
1971
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001972std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1973 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1974 if (module_sp)
1975 return module_sp->GetMutex();
1976 return GetObjectFile()->GetModule()->GetMutex();
1977}
1978
Kate Stoneb9c1b512016-09-06 20:57:50 +00001979bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1980 const lldb_private::CompilerDeclContext *decl_ctx) {
1981 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001982 // Invalid namespace decl which means we aren't matching only things in
1983 // this symbol file, so return true to indicate it matches this symbol
1984 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001985 return true;
1986 }
1987
1988 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1989 TypeSystem *type_system = GetTypeSystemForLanguage(
1990 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1991 if (decl_ctx_type_system == type_system)
1992 return true; // The type systems match, return true
1993
1994 // The namespace AST was valid, and it does not match...
1995 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1996
1997 if (log)
1998 GetObjectFile()->GetModule()->LogMessage(
1999 log, "Valid namespace does not match symbol file");
2000
2001 return false;
2002}
2003
2004uint32_t SymbolFileDWARF::FindGlobalVariables(
2005 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002006 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002007 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2008
2009 if (log)
2010 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002011 log,
2012 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2013 "parent_decl_ctx=%p, max_matches=%u, variables)",
2014 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 max_matches);
2016
2017 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2018 return 0;
2019
2020 DWARFDebugInfo *info = DebugInfo();
2021 if (info == NULL)
2022 return 0;
2023
Pavel Labath34cda142018-05-31 09:46:26 +00002024 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002025 const uint32_t original_size = variables.GetSize();
2026
Pavel Labathe1d18752018-06-07 10:04:44 +00002027 llvm::StringRef basename;
2028 llvm::StringRef context;
2029
2030 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2031 context, basename))
2032 basename = name.GetStringRef();
2033
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002035 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 const size_t num_die_matches = die_offsets.size();
2037 if (num_die_matches) {
2038 SymbolContext sc;
2039 sc.module_sp = m_obj_file->GetModule();
2040 assert(sc.module_sp);
2041
Pavel Labathe1d18752018-06-07 10:04:44 +00002042 // Loop invariant: Variables up to this index have been checked for context
2043 // matches.
2044 uint32_t pruned_idx = original_size;
2045
Kate Stoneb9c1b512016-09-06 20:57:50 +00002046 bool done = false;
2047 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2048 const DIERef &die_ref = die_offsets[i];
2049 DWARFDIE die = GetDIE(die_ref);
2050
2051 if (die) {
2052 switch (die.Tag()) {
2053 default:
2054 case DW_TAG_subprogram:
2055 case DW_TAG_inlined_subroutine:
2056 case DW_TAG_try_block:
2057 case DW_TAG_catch_block:
2058 break;
2059
2060 case DW_TAG_variable: {
2061 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2062
2063 if (parent_decl_ctx) {
2064 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2065 if (dwarf_ast) {
2066 CompilerDeclContext actual_parent_decl_ctx =
2067 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2068 if (!actual_parent_decl_ctx ||
2069 actual_parent_decl_ctx != *parent_decl_ctx)
2070 continue;
2071 }
2072 }
2073
2074 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2075 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002076 while (pruned_idx < variables.GetSize()) {
2077 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2078 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2079 ++pruned_idx;
2080 else
2081 variables.RemoveVariableAtIndex(pruned_idx);
2082 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002083
2084 if (variables.GetSize() - original_size >= max_matches)
2085 done = true;
2086 } break;
2087 }
2088 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002089 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2090 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 }
2092 }
2093 }
2094
2095 // Return the number of variable that were appended to the list
2096 const uint32_t num_matches = variables.GetSize() - original_size;
2097 if (log && num_matches > 0) {
2098 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002099 log,
2100 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2101 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2102 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002103 max_matches, num_matches);
2104 }
2105 return num_matches;
2106}
2107
2108uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002109 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002110 VariableList &variables) {
2111 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2112
2113 if (log) {
2114 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002115 log,
2116 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2117 "max_matches=%u, variables)",
2118 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119 }
2120
2121 DWARFDebugInfo *info = DebugInfo();
2122 if (info == NULL)
2123 return 0;
2124
Pavel Labath34cda142018-05-31 09:46:26 +00002125 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 const uint32_t original_size = variables.GetSize();
2127
2128 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002129 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002130
2131 SymbolContext sc;
2132 sc.module_sp = m_obj_file->GetModule();
2133 assert(sc.module_sp);
2134
2135 const size_t num_matches = die_offsets.size();
2136 if (num_matches) {
2137 for (size_t i = 0; i < num_matches; ++i) {
2138 const DIERef &die_ref = die_offsets[i];
2139 DWARFDIE die = GetDIE(die_ref);
2140
2141 if (die) {
2142 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2143
2144 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2145
2146 if (variables.GetSize() - original_size >= max_matches)
2147 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002148 } else
2149 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150 }
2151 }
2152
2153 // Return the number of variable that were appended to the list
2154 return variables.GetSize() - original_size;
2155}
2156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2158 bool include_inlines,
2159 SymbolContextList &sc_list) {
2160 SymbolContext sc;
2161
2162 if (!orig_die)
2163 return false;
2164
2165 // If we were passed a die that is not a function, just return false...
2166 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2167 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2168 return false;
2169
2170 DWARFDIE die = orig_die;
2171 DWARFDIE inlined_die;
2172 if (die.Tag() == DW_TAG_inlined_subroutine) {
2173 inlined_die = die;
2174
2175 while (1) {
2176 die = die.GetParent();
2177
2178 if (die) {
2179 if (die.Tag() == DW_TAG_subprogram)
2180 break;
2181 } else
2182 break;
2183 }
2184 }
2185 assert(die && die.Tag() == DW_TAG_subprogram);
2186 if (GetFunction(die, sc)) {
2187 Address addr;
2188 // Parse all blocks if needed
2189 if (inlined_die) {
2190 Block &function_block = sc.function->GetBlock(true);
2191 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2192 if (sc.block == NULL)
2193 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00002194 if (sc.block == NULL || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002195 addr.Clear();
2196 } else {
2197 sc.block = NULL;
2198 addr = sc.function->GetAddressRange().GetBaseAddress();
2199 }
2200
2201 if (addr.IsValid()) {
2202 sc_list.Append(sc);
2203 return true;
2204 }
2205 }
2206
2207 return false;
2208}
2209
Kate Stoneb9c1b512016-09-06 20:57:50 +00002210bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2211 const DWARFDIE &die) {
2212 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002213 // parent decl context isn't valid, we aren't trying to look for any
2214 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2216 return true;
2217
2218 if (die) {
2219 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2220 if (dwarf_ast) {
2221 CompilerDeclContext actual_decl_ctx =
2222 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2223 if (actual_decl_ctx)
2224 return actual_decl_ctx == *decl_ctx;
2225 }
2226 }
2227 return false;
2228}
2229
Zachary Turner117b1fa2018-10-25 20:45:40 +00002230uint32_t SymbolFileDWARF::FindFunctions(
2231 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2232 FunctionNameType name_type_mask, bool include_inlines, bool append,
2233 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002234 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2235 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002236 name.AsCString());
2237
2238 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002239 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2241
2242 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2243
2244 if (log) {
2245 GetObjectFile()->GetModule()->LogMessage(
2246 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2247 "name_type_mask=0x%x, append=%u, sc_list)",
2248 name.GetCString(), name_type_mask, append);
2249 }
2250
2251 // If we aren't appending the results to this list, then clear the list
2252 if (!append)
2253 sc_list.Clear();
2254
2255 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2256 return 0;
2257
2258 // If name is empty then we won't find anything.
2259 if (name.IsEmpty())
2260 return 0;
2261
Adrian Prantl05097242018-04-30 16:49:04 +00002262 // Remember how many sc_list are in the list before we search in case we are
2263 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264
Kate Stoneb9c1b512016-09-06 20:57:50 +00002265 const uint32_t original_size = sc_list.GetSize();
2266
2267 DWARFDebugInfo *info = DebugInfo();
2268 if (info == NULL)
2269 return 0;
2270
Pavel Labath5af11ab2018-06-05 10:33:56 +00002271 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2272 DIEArray offsets;
2273 CompilerDeclContext empty_decl_ctx;
2274 if (!parent_decl_ctx)
2275 parent_decl_ctx = &empty_decl_ctx;
2276
2277 std::vector<DWARFDIE> dies;
2278 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2279 for (const DWARFDIE &die: dies) {
2280 if (resolved_dies.insert(die.GetDIE()).second)
2281 ResolveFunction(die, include_inlines, sc_list);
2282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283
2284 // Return the number of variable that were appended to the list
2285 const uint32_t num_matches = sc_list.GetSize() - original_size;
2286
2287 if (log && num_matches > 0) {
2288 GetObjectFile()->GetModule()->LogMessage(
2289 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2290 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2291 "%u",
2292 name.GetCString(), name_type_mask, include_inlines, append,
2293 num_matches);
2294 }
2295 return num_matches;
2296}
2297
2298uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2299 bool include_inlines, bool append,
2300 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002301 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2302 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002303 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002304
2305 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2306
2307 if (log) {
2308 GetObjectFile()->GetModule()->LogMessage(
2309 log,
2310 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002311 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002312 }
2313
2314 // If we aren't appending the results to this list, then clear the list
2315 if (!append)
2316 sc_list.Clear();
2317
Pavel Labathb13f0332018-05-21 14:12:52 +00002318 DWARFDebugInfo *info = DebugInfo();
2319 if (!info)
2320 return 0;
2321
Adrian Prantl05097242018-04-30 16:49:04 +00002322 // Remember how many sc_list are in the list before we search in case we are
2323 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 uint32_t original_size = sc_list.GetSize();
2325
Pavel Labath6de9c792018-06-05 12:13:22 +00002326 DIEArray offsets;
2327 m_index->GetFunctions(regex, offsets);
2328
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002329 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2330 for (DIERef ref : offsets) {
2331 DWARFDIE die = info->GetDIE(ref);
2332 if (!die) {
2333 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2334 continue;
2335 }
2336 if (resolved_dies.insert(die.GetDIE()).second)
2337 ResolveFunction(die, include_inlines, sc_list);
2338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002339
2340 // Return the number of variable that were appended to the list
2341 return sc_list.GetSize() - original_size;
2342}
2343
2344void SymbolFileDWARF::GetMangledNamesForFunction(
2345 const std::string &scope_qualified_name,
2346 std::vector<ConstString> &mangled_names) {
2347 DWARFDebugInfo *info = DebugInfo();
2348 uint32_t num_comp_units = 0;
2349 if (info)
2350 num_comp_units = info->GetNumCompileUnits();
2351
2352 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002353 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002354 if (cu == nullptr)
2355 continue;
2356
2357 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2358 if (dwo)
2359 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2360 }
2361
2362 NameToOffsetMap::iterator iter =
2363 m_function_scope_qualified_name_map.find(scope_qualified_name);
2364 if (iter == m_function_scope_qualified_name_map.end())
2365 return;
2366
2367 DIERefSetSP set_sp = (*iter).second;
2368 std::set<DIERef>::iterator set_iter;
2369 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2370 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2371 mangled_names.push_back(ConstString(die.GetMangledName()));
2372 }
2373}
2374
2375uint32_t SymbolFileDWARF::FindTypes(
Zachary Turner576495e2019-01-14 22:41:21 +00002376 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2377 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002378 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2379 TypeMap &types) {
2380 // If we aren't appending the results to this list, then clear the list
2381 if (!append)
2382 types.Clear();
2383
2384 // Make sure we haven't already searched this SymbolFile before...
2385 if (searched_symbol_files.count(this))
2386 return 0;
2387 else
2388 searched_symbol_files.insert(this);
2389
2390 DWARFDebugInfo *info = DebugInfo();
2391 if (info == NULL)
2392 return 0;
2393
2394 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2395
2396 if (log) {
2397 if (parent_decl_ctx)
2398 GetObjectFile()->GetModule()->LogMessage(
2399 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2400 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2401 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2402 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2403 else
2404 GetObjectFile()->GetModule()->LogMessage(
2405 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2406 "NULL, append=%u, max_matches=%u, type_list)",
2407 name.GetCString(), append, max_matches);
2408 }
2409
2410 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2411 return 0;
2412
2413 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002414 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 const size_t num_die_matches = die_offsets.size();
2416
2417 if (num_die_matches) {
2418 const uint32_t initial_types_size = types.GetSize();
2419 for (size_t i = 0; i < num_die_matches; ++i) {
2420 const DIERef &die_ref = die_offsets[i];
2421 DWARFDIE die = GetDIE(die_ref);
2422
2423 if (die) {
2424 if (!DIEInDeclContext(parent_decl_ctx, die))
2425 continue; // The containing decl contexts don't match
2426
2427 Type *matching_type = ResolveType(die, true, true);
2428 if (matching_type) {
2429 // We found a type pointer, now find the shared pointer form our type
2430 // list
2431 types.InsertUnique(matching_type->shared_from_this());
2432 if (types.GetSize() >= max_matches)
2433 break;
2434 }
2435 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002436 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2437 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002438 }
2439 }
2440 const uint32_t num_matches = types.GetSize() - initial_types_size;
2441 if (log && num_matches) {
2442 if (parent_decl_ctx) {
2443 GetObjectFile()->GetModule()->LogMessage(
2444 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2445 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2446 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2447 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2448 num_matches);
2449 } else {
2450 GetObjectFile()->GetModule()->LogMessage(
2451 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2452 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2453 name.GetCString(), append, max_matches, num_matches);
2454 }
2455 }
2456 return num_matches;
2457 } else {
2458 UpdateExternalModuleListIfNeeded();
2459
2460 for (const auto &pair : m_external_type_modules) {
2461 ModuleSP external_module_sp = pair.second;
2462 if (external_module_sp) {
2463 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2464 if (sym_vendor) {
2465 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002466 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2467 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 if (num_external_matches)
2469 return num_external_matches;
2470 }
2471 }
2472 }
2473 }
2474
2475 return 0;
2476}
2477
2478size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2479 bool append, TypeMap &types) {
2480 if (!append)
2481 types.Clear();
2482
2483 if (context.empty())
2484 return 0;
2485
Kate Stoneb9c1b512016-09-06 20:57:50 +00002486 ConstString name = context.back().name;
2487
2488 if (!name)
2489 return 0;
2490
Pavel Labathb13f0332018-05-21 14:12:52 +00002491 DIEArray die_offsets;
2492 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 const size_t num_die_matches = die_offsets.size();
2494
2495 if (num_die_matches) {
2496 size_t num_matches = 0;
2497 for (size_t i = 0; i < num_die_matches; ++i) {
2498 const DIERef &die_ref = die_offsets[i];
2499 DWARFDIE die = GetDIE(die_ref);
2500
2501 if (die) {
2502 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002503 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002504 if (die_context != context)
2505 continue;
2506
2507 Type *matching_type = ResolveType(die, true, true);
2508 if (matching_type) {
2509 // We found a type pointer, now find the shared pointer form our type
2510 // list
2511 types.InsertUnique(matching_type->shared_from_this());
2512 ++num_matches;
2513 }
2514 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002515 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2516 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002517 }
2518 }
2519 return num_matches;
2520 }
2521 return 0;
2522}
2523
2524CompilerDeclContext
Zachary Turnerc0a246a2019-01-14 22:41:00 +00002525SymbolFileDWARF::FindNamespace(const ConstString &name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002526 const CompilerDeclContext *parent_decl_ctx) {
2527 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2528
2529 if (log) {
2530 GetObjectFile()->GetModule()->LogMessage(
2531 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2532 name.GetCString());
2533 }
2534
2535 CompilerDeclContext namespace_decl_ctx;
2536
2537 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2538 return namespace_decl_ctx;
2539
2540 DWARFDebugInfo *info = DebugInfo();
2541 if (info) {
2542 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002543 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 const size_t num_matches = die_offsets.size();
2545 if (num_matches) {
2546 for (size_t i = 0; i < num_matches; ++i) {
2547 const DIERef &die_ref = die_offsets[i];
2548 DWARFDIE die = GetDIE(die_ref);
2549
2550 if (die) {
2551 if (!DIEInDeclContext(parent_decl_ctx, die))
2552 continue; // The containing decl contexts don't match
2553
2554 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2555 if (dwarf_ast) {
2556 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2557 if (namespace_decl_ctx)
2558 break;
2559 }
2560 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002561 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2562 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002563 }
2564 }
2565 }
2566 }
2567 if (log && namespace_decl_ctx) {
2568 GetObjectFile()->GetModule()->LogMessage(
2569 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2570 "CompilerDeclContext(%p/%p) \"%s\"",
2571 name.GetCString(),
2572 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2573 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2574 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2575 }
2576
2577 return namespace_decl_ctx;
2578}
2579
2580TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2581 bool resolve_function_context) {
2582 TypeSP type_sp;
2583 if (die) {
2584 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2585 if (type_ptr == NULL) {
2586 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2587 assert(lldb_cu);
2588 SymbolContext sc(lldb_cu);
2589 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2590 while (parent_die != nullptr) {
2591 if (parent_die->Tag() == DW_TAG_subprogram)
2592 break;
2593 parent_die = parent_die->GetParent();
2594 }
2595 SymbolContext sc_backup = sc;
2596 if (resolve_function_context && parent_die != nullptr &&
2597 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2598 sc = sc_backup;
2599
2600 type_sp = ParseType(sc, die, NULL);
2601 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2602 // Grab the existing type from the master types lists
2603 type_sp = type_ptr->shared_from_this();
2604 }
2605 }
2606 return type_sp;
2607}
2608
2609DWARFDIE
2610SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2611 if (orig_die) {
2612 DWARFDIE die = orig_die;
2613
2614 while (die) {
2615 // If this is the original DIE that we are searching for a declaration
2616 // for, then don't look in the cache as we don't want our own decl
2617 // context to be our decl context...
2618 if (orig_die != die) {
2619 switch (die.Tag()) {
2620 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002621 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002622 case DW_TAG_namespace:
2623 case DW_TAG_structure_type:
2624 case DW_TAG_union_type:
2625 case DW_TAG_class_type:
2626 case DW_TAG_lexical_block:
2627 case DW_TAG_subprogram:
2628 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002629 case DW_TAG_inlined_subroutine: {
2630 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2631 if (abs_die) {
2632 return abs_die;
2633 }
2634 break;
2635 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002636 default:
2637 break;
2638 }
2639 }
2640
2641 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2642 if (spec_die) {
2643 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2644 if (decl_ctx_die)
2645 return decl_ctx_die;
2646 }
2647
2648 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2649 if (abs_die) {
2650 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2651 if (decl_ctx_die)
2652 return decl_ctx_die;
2653 }
2654
2655 die = die.GetParent();
2656 }
2657 }
2658 return DWARFDIE();
2659}
2660
2661Symbol *
2662SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2663 Symbol *objc_class_symbol = NULL;
2664 if (m_obj_file) {
2665 Symtab *symtab = m_obj_file->GetSymtab();
2666 if (symtab) {
2667 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2668 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2669 Symtab::eVisibilityAny);
2670 }
2671 }
2672 return objc_class_symbol;
2673}
2674
2675// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002676// they don't then we can end up looking through all class types for a complete
2677// type and never find the full definition. We need to know if this attribute
2678// is supported, so we determine this here and cache th result. We also need to
2679// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002680// DWARF file
2681// if we are doing darwin DWARF in .o file debugging.
2682bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002683 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002684 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2685 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2686 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2687 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2688 else {
2689 DWARFDebugInfo *debug_info = DebugInfo();
2690 const uint32_t num_compile_units = GetNumCompileUnits();
2691 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002692 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 if (dwarf_cu != cu &&
2694 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2695 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2696 break;
2697 }
2698 }
2699 }
2700 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2701 GetDebugMapSymfile())
2702 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2703 }
2704 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002705}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002706
2707// This function can be used when a DIE is found that is a forward declaration
2708// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002709TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2710 const DWARFDIE &die, const ConstString &type_name,
2711 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002712
Kate Stoneb9c1b512016-09-06 20:57:50 +00002713 TypeSP type_sp;
2714
2715 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2716 return type_sp;
2717
2718 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002719 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720
2721 const size_t num_matches = die_offsets.size();
2722
2723 if (num_matches) {
2724 for (size_t i = 0; i < num_matches; ++i) {
2725 const DIERef &die_ref = die_offsets[i];
2726 DWARFDIE type_die = GetDIE(die_ref);
2727
2728 if (type_die) {
2729 bool try_resolving_type = false;
2730
Adrian Prantl05097242018-04-30 16:49:04 +00002731 // Don't try and resolve the DIE we are looking for with the DIE
2732 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002733 if (type_die != die) {
2734 switch (type_die.Tag()) {
2735 case DW_TAG_class_type:
2736 case DW_TAG_structure_type:
2737 try_resolving_type = true;
2738 break;
2739 default:
2740 break;
2741 }
2742 }
2743
2744 if (try_resolving_type) {
2745 if (must_be_implementation &&
2746 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2747 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2748 DW_AT_APPLE_objc_complete_type, 0);
2749
2750 if (try_resolving_type) {
2751 Type *resolved_type = ResolveType(type_die, false, true);
2752 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2753 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2754 " (cu 0x%8.8" PRIx64 ")\n",
2755 die.GetID(),
2756 m_obj_file->GetFileSpec().GetFilename().AsCString(
2757 "<Unknown>"),
2758 type_die.GetID(), type_cu->GetID());
2759
2760 if (die)
2761 GetDIEToType()[die.GetDIE()] = resolved_type;
2762 type_sp = resolved_type->shared_from_this();
2763 break;
2764 }
2765 }
2766 }
2767 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002768 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2769 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002770 }
2771 }
2772 }
2773 return type_sp;
2774}
Greg Claytona8022fa2012-04-24 21:22:41 +00002775
Greg Clayton80c26302012-02-05 06:12:47 +00002776//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002777// This function helps to ensure that the declaration contexts match for two
2778// different DIEs. Often times debug information will refer to a forward
2779// declaration of a type (the equivalent of "struct my_struct;". There will
2780// often be a declaration of that type elsewhere that has the full definition.
2781// When we go looking for the full type "my_struct", we will find one or more
2782// matches in the accelerator tables and we will then need to make sure the
2783// type was in the same declaration context as the original DIE. This function
2784// can efficiently compare two DIEs and will return true when the declaration
2785// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002786//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002787bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2788 const DWARFDIE &die2) {
2789 if (die1 == die2)
2790 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002791
Kate Stoneb9c1b512016-09-06 20:57:50 +00002792 DWARFDIECollection decl_ctx_1;
2793 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002794 // The declaration DIE stack is a stack of the declaration context DIEs all
2795 // the way back to the compile unit. If a type "T" is declared inside a class
2796 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2797 // namespace "lldb", and the namespace is in a compile unit, there will be a
2798 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002799 //
2800 // [0] DW_TAG_class_type for "B"
2801 // [1] DW_TAG_class_type for "A"
2802 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002803 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002804 //
Adrian Prantl05097242018-04-30 16:49:04 +00002805 // We grab both contexts and make sure that everything matches all the way
2806 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807
2808 // First lets grab the decl contexts for both DIEs
2809 die1.GetDeclContextDIEs(decl_ctx_1);
2810 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00002811 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00002812 const size_t count1 = decl_ctx_1.Size();
2813 const size_t count2 = decl_ctx_2.Size();
2814 if (count1 != count2)
2815 return false;
2816
Adrian Prantl05097242018-04-30 16:49:04 +00002817 // Make sure the DW_TAG values match all the way back up the compile unit. If
2818 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002819 DWARFDIE decl_ctx_die1;
2820 DWARFDIE decl_ctx_die2;
2821 size_t i;
2822 for (i = 0; i < count1; i++) {
2823 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2824 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2825 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2826 return false;
2827 }
Greg Clayton890ff562012-02-02 05:48:16 +00002828#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002829
Kate Stoneb9c1b512016-09-06 20:57:50 +00002830 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002831 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2832 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2833 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002834 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2835 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2836 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002837
Greg Clayton890ff562012-02-02 05:48:16 +00002838#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002839 // Always skip the compile unit when comparing by only iterating up to "count
2840 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841 for (i = 0; i < count1 - 1; i++) {
2842 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2843 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2844 const char *name1 = decl_ctx_die1.GetName();
2845 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002846 // If the string was from a DW_FORM_strp, then the pointer will often be
2847 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002848 if (name1 == name2)
2849 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002850
Adrian Prantl05097242018-04-30 16:49:04 +00002851 // Name pointers are not equal, so only compare the strings if both are not
2852 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 if (name1 && name2) {
2854 // If the strings don't compare, we are done...
2855 if (strcmp(name1, name2) != 0)
2856 return false;
2857 } else {
2858 // One name was NULL while the other wasn't
2859 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002860 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002861 }
Adrian Prantl05097242018-04-30 16:49:04 +00002862 // We made it through all of the checks and the declaration contexts are
2863 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002864 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002865}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002866
Kate Stoneb9c1b512016-09-06 20:57:50 +00002867TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2868 const DWARFDeclContext &dwarf_decl_ctx) {
2869 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002870
Kate Stoneb9c1b512016-09-06 20:57:50 +00002871 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2872 if (dwarf_decl_ctx_count > 0) {
2873 const ConstString type_name(dwarf_decl_ctx[0].name);
2874 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876 if (type_name) {
2877 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2878 DWARF_LOG_LOOKUPS));
2879 if (log) {
2880 GetObjectFile()->GetModule()->LogMessage(
2881 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2882 "s, qualified-name='%s')",
2883 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2884 dwarf_decl_ctx.GetQualifiedName());
2885 }
2886
2887 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002888 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 const size_t num_matches = die_offsets.size();
2890
Adrian Prantl05097242018-04-30 16:49:04 +00002891 // Get the type system that we are looking to find a type for. We will
2892 // use this to ensure any matches we find are in a language that this
2893 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002894 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2895 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2896 ? nullptr
2897 : GetTypeSystemForLanguage(language);
2898
2899 if (num_matches) {
2900 for (size_t i = 0; i < num_matches; ++i) {
2901 const DIERef &die_ref = die_offsets[i];
2902 DWARFDIE type_die = GetDIE(die_ref);
2903
2904 if (type_die) {
2905 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002906 // looking for. We don't want to find a "Foo" type from Java if we
2907 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002908 if (type_system &&
2909 !type_system->SupportsLanguage(type_die.GetLanguage()))
2910 continue;
2911 bool try_resolving_type = false;
2912
2913 // Don't try and resolve the DIE we are looking for with the DIE
2914 // itself!
2915 const dw_tag_t type_tag = type_die.Tag();
2916 // Make sure the tags match
2917 if (type_tag == tag) {
2918 // The tags match, lets try resolving this type
2919 try_resolving_type = true;
2920 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002921 // The tags don't match, but we need to watch our for a forward
2922 // declaration for a struct and ("struct foo") ends up being a
2923 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 switch (type_tag) {
2925 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002926 // We had a "class foo", see if we ended up with a "struct foo
2927 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928 try_resolving_type = (tag == DW_TAG_structure_type);
2929 break;
2930 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002931 // We had a "struct foo", see if we ended up with a "class foo
2932 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002933 try_resolving_type = (tag == DW_TAG_class_type);
2934 break;
2935 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002936 // Tags don't match, don't event try to resolve using this type
2937 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002938 break;
2939 }
2940 }
2941
2942 if (try_resolving_type) {
2943 DWARFDeclContext type_dwarf_decl_ctx;
2944 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2945
2946 if (log) {
2947 GetObjectFile()->GetModule()->LogMessage(
2948 log, "SymbolFileDWARF::"
2949 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2950 "qualified-name='%s') trying die=0x%8.8x (%s)",
2951 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2952 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2953 type_dwarf_decl_ctx.GetQualifiedName());
2954 }
2955
2956 // Make sure the decl contexts match all the way up
2957 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2958 Type *resolved_type = ResolveType(type_die, false);
2959 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2960 type_sp = resolved_type->shared_from_this();
2961 break;
2962 }
2963 }
2964 } else {
2965 if (log) {
2966 std::string qualified_name;
2967 type_die.GetQualifiedName(qualified_name);
2968 GetObjectFile()->GetModule()->LogMessage(
2969 log, "SymbolFileDWARF::"
2970 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2971 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2972 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2973 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2974 qualified_name.c_str());
2975 }
2976 }
2977 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002978 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2979 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002980 }
2981 }
2982 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984 }
2985 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002986}
2987
Kate Stoneb9c1b512016-09-06 20:57:50 +00002988TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2989 bool *type_is_new_ptr) {
2990 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002991
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 if (die) {
2993 TypeSystem *type_system =
2994 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00002995
Kate Stoneb9c1b512016-09-06 20:57:50 +00002996 if (type_system) {
2997 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2998 if (dwarf_ast) {
2999 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3000 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3001 if (type_sp) {
3002 TypeList *type_list = GetTypeList();
3003 if (type_list)
3004 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003005
Kate Stoneb9c1b512016-09-06 20:57:50 +00003006 if (die.Tag() == DW_TAG_subprogram) {
3007 DIERef die_ref = die.GetDIERef();
3008 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3009 .GetScopeQualifiedName()
3010 .AsCString(""));
3011 if (scope_qualified_name.size()) {
3012 NameToOffsetMap::iterator iter =
3013 m_function_scope_qualified_name_map.find(
3014 scope_qualified_name);
3015 if (iter != m_function_scope_qualified_name_map.end())
3016 (*iter).second->insert(die_ref);
3017 else {
3018 DIERefSetSP new_set(new std::set<DIERef>);
3019 new_set->insert(die_ref);
3020 m_function_scope_qualified_name_map.emplace(
3021 std::make_pair(scope_qualified_name, new_set));
3022 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003024 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003025 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003026 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028 }
3029
3030 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003031}
3032
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3034 const DWARFDIE &orig_die,
3035 bool parse_siblings, bool parse_children) {
3036 size_t types_added = 0;
3037 DWARFDIE die = orig_die;
3038 while (die) {
3039 bool type_is_new = false;
3040 if (ParseType(sc, die, &type_is_new).get()) {
3041 if (type_is_new)
3042 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044
3045 if (parse_children && die.HasChildren()) {
3046 if (die.Tag() == DW_TAG_subprogram) {
3047 SymbolContext child_sc(sc);
3048 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3049 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3050 } else
3051 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3052 }
3053
3054 if (parse_siblings)
3055 die = die.GetSibling();
3056 else
3057 die.Clear();
3058 }
3059 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060}
3061
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003062size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003063 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003064 CompileUnit *comp_unit = func.GetCompileUnit();
3065 lldbassert(comp_unit);
3066
3067 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3068 if (!dwarf_cu)
3069 return 0;
3070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003072 const dw_offset_t function_die_offset = func.GetID();
3073 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3074 if (function_die) {
3075 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3076 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003078
Kate Stoneb9c1b512016-09-06 20:57:50 +00003079 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003080}
3081
Zachary Turner863f8c12019-01-11 18:03:20 +00003082size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003083 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003085 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003086 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003087 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3088 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3089 SymbolContext sc;
3090 sc.comp_unit = &comp_unit;
3091 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003093 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094
Kate Stoneb9c1b512016-09-06 20:57:50 +00003095 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003096}
3097
Kate Stoneb9c1b512016-09-06 20:57:50 +00003098size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003099 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003100 if (sc.comp_unit != NULL) {
3101 DWARFDebugInfo *info = DebugInfo();
3102 if (info == NULL)
3103 return 0;
3104
3105 if (sc.function) {
3106 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3107
3108 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3109 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3110 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3111 const size_t num_variables = ParseVariables(
3112 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3113
3114 // Let all blocks know they have parse all their variables
3115 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3116 return num_variables;
3117 }
3118 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003119 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003120
3121 if (dwarf_cu == NULL)
3122 return 0;
3123
3124 uint32_t vars_added = 0;
3125 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3126
3127 if (variables.get() == NULL) {
3128 variables.reset(new VariableList());
3129 sc.comp_unit->SetVariableList(variables);
3130
3131 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003132 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003133 const size_t num_matches = die_offsets.size();
3134 if (num_matches) {
3135 for (size_t i = 0; i < num_matches; ++i) {
3136 const DIERef &die_ref = die_offsets[i];
3137 DWARFDIE die = GetDIE(die_ref);
3138 if (die) {
3139 VariableSP var_sp(
3140 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3141 if (var_sp) {
3142 variables->AddVariableIfUnique(var_sp);
3143 ++vars_added;
3144 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003145 } else
3146 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149 }
3150 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152 }
3153 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003154}
3155
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3157 const DWARFDIE &die,
3158 const lldb::addr_t func_low_pc) {
3159 if (die.GetDWARF() != this)
3160 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003161
Kate Stoneb9c1b512016-09-06 20:57:50 +00003162 VariableSP var_sp;
3163 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003164 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003165
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166 var_sp = GetDIEToVariable()[die.GetDIE()];
3167 if (var_sp)
3168 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003169
Kate Stoneb9c1b512016-09-06 20:57:50 +00003170 const dw_tag_t tag = die.Tag();
3171 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003172
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3174 (tag == DW_TAG_formal_parameter && sc.function)) {
3175 DWARFAttributes attributes;
3176 const size_t num_attributes = die.GetAttributes(attributes);
3177 DWARFDIE spec_die;
3178 if (num_attributes > 0) {
3179 const char *name = NULL;
3180 const char *mangled = NULL;
3181 Declaration decl;
3182 uint32_t i;
3183 DWARFFormValue type_die_form;
3184 DWARFExpression location(die.GetCU());
3185 bool is_external = false;
3186 bool is_artificial = false;
3187 bool location_is_const_value_data = false;
3188 bool has_explicit_location = false;
3189 DWARFFormValue const_value;
3190 Variable::RangeList scope_ranges;
3191 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003192
Kate Stoneb9c1b512016-09-06 20:57:50 +00003193 for (i = 0; i < num_attributes; ++i) {
3194 dw_attr_t attr = attributes.AttributeAtIndex(i);
3195 DWARFFormValue form_value;
3196
3197 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3198 switch (attr) {
3199 case DW_AT_decl_file:
3200 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3201 form_value.Unsigned()));
3202 break;
3203 case DW_AT_decl_line:
3204 decl.SetLine(form_value.Unsigned());
3205 break;
3206 case DW_AT_decl_column:
3207 decl.SetColumn(form_value.Unsigned());
3208 break;
3209 case DW_AT_name:
3210 name = form_value.AsCString();
3211 break;
3212 case DW_AT_linkage_name:
3213 case DW_AT_MIPS_linkage_name:
3214 mangled = form_value.AsCString();
3215 break;
3216 case DW_AT_type:
3217 type_die_form = form_value;
3218 break;
3219 case DW_AT_external:
3220 is_external = form_value.Boolean();
3221 break;
3222 case DW_AT_const_value:
3223 // If we have already found a DW_AT_location attribute, ignore this
3224 // attribute.
3225 if (!has_explicit_location) {
3226 location_is_const_value_data = true;
3227 // The constant value will be either a block, a data value or a
3228 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003229 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003230 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3231 // Retrieve the value as a block expression.
3232 uint32_t block_offset =
3233 form_value.BlockData() - debug_info_data.GetDataStart();
3234 uint32_t block_length = form_value.Unsigned();
3235 location.CopyOpcodeData(module, debug_info_data, block_offset,
3236 block_length);
3237 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3238 // Retrieve the value as a data expression.
3239 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3240 DWARFFormValue::GetFixedFormSizesForAddressSize(
3241 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3242 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3243 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3244 uint32_t data_length =
3245 fixed_form_sizes.GetSize(form_value.Form());
3246 if (data_length == 0) {
3247 const uint8_t *data_pointer = form_value.BlockData();
3248 if (data_pointer) {
3249 form_value.Unsigned();
3250 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3251 // we need to get the byte size of the type later after we
3252 // create the variable
3253 const_value = form_value;
3254 }
3255 } else
3256 location.CopyOpcodeData(module, debug_info_data, data_offset,
3257 data_length);
3258 } else {
3259 // Retrieve the value as a string expression.
3260 if (form_value.Form() == DW_FORM_strp) {
3261 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3262 DWARFFormValue::GetFixedFormSizesForAddressSize(
3263 attributes.CompileUnitAtIndex(i)
3264 ->GetAddressByteSize(),
3265 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3266 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3267 uint32_t data_length =
3268 fixed_form_sizes.GetSize(form_value.Form());
3269 location.CopyOpcodeData(module, debug_info_data, data_offset,
3270 data_length);
3271 } else {
3272 const char *str = form_value.AsCString();
3273 uint32_t string_offset =
3274 str - (const char *)debug_info_data.GetDataStart();
3275 uint32_t string_length = strlen(str) + 1;
3276 location.CopyOpcodeData(module, debug_info_data,
3277 string_offset, string_length);
3278 }
3279 }
3280 }
3281 break;
3282 case DW_AT_location: {
3283 location_is_const_value_data = false;
3284 has_explicit_location = true;
3285 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003286 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003287
3288 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003289 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003291 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003292 } else {
George Rimare4dee262018-10-23 09:46:15 +00003293 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003294 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3295
3296 size_t loc_list_length = DWARFExpression::LocationListSize(
3297 die.GetCU(), debug_loc_data, debug_loc_offset);
3298 if (loc_list_length > 0) {
3299 location.CopyOpcodeData(module, debug_loc_data,
3300 debug_loc_offset, loc_list_length);
3301 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3302 location.SetLocationListSlide(
3303 func_low_pc -
3304 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3305 }
3306 }
3307 } break;
3308 case DW_AT_specification:
3309 spec_die = GetDIE(DIERef(form_value));
3310 break;
3311 case DW_AT_start_scope: {
3312 if (form_value.Form() == DW_FORM_sec_offset) {
3313 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003314 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3315 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003316 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003317 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003318 } else {
3319 // TODO: Handle the case when DW_AT_start_scope have form
3320 // constant. The
3321 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003322 // behavior in case the enclosing block have a non coninious
3323 // address range and the DW_AT_start_scope entry have a form
3324 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003325 GetObjectFile()->GetModule()->ReportWarning(
3326 "0x%8.8" PRIx64
3327 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3328 die.GetID(), form_value.Form());
3329 }
3330
3331 scope_ranges.Sort();
3332 scope_ranges.CombineConsecutiveRanges();
3333 } break;
3334 case DW_AT_artificial:
3335 is_artificial = form_value.Boolean();
3336 break;
3337 case DW_AT_accessibility:
3338 break; // accessibility =
3339 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3340 case DW_AT_declaration:
3341 case DW_AT_description:
3342 case DW_AT_endianity:
3343 case DW_AT_segment:
3344 case DW_AT_visibility:
3345 default:
3346 case DW_AT_abstract_origin:
3347 case DW_AT_sibling:
3348 break;
3349 }
3350 }
3351 }
3352
3353 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3354 const dw_tag_t parent_tag = die.GetParent().Tag();
3355 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003356 (parent_tag == DW_TAG_compile_unit ||
3357 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003358 (parent_context_die.Tag() == DW_TAG_class_type ||
3359 parent_context_die.Tag() == DW_TAG_structure_type);
3360
3361 ValueType scope = eValueTypeInvalid;
3362
3363 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3364 SymbolContextScope *symbol_context_scope = NULL;
3365
Sam McCall30621392016-11-22 11:40:25 +00003366 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003367 if (!mangled) {
3368 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003369 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3370 // of global variables with commands like "frame var j". For
3371 // example, if j were an int variable holding a value 4 and
3372 // declared in a namespace B which in turn is contained in a
3373 // namespace A, the command "frame var j" returns
3374 // "(int) A::B::j = 4".
3375 // If the compiler does not emit a linkage name, we should be
3376 // able to generate a fully qualified name from the
3377 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003378 if ((parent_tag == DW_TAG_compile_unit ||
3379 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003380 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3381 DWARFDeclContext decl_ctx;
3382
3383 die.GetDWARFDeclContext(decl_ctx);
3384 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3385 }
3386 }
3387
Kate Stoneb9c1b512016-09-06 20:57:50 +00003388 if (tag == DW_TAG_formal_parameter)
3389 scope = eValueTypeVariableArgument;
3390 else {
Sam McCall30621392016-11-22 11:40:25 +00003391 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3392 // or static variable, so we have to do a little digging:
3393 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3394 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3395 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3396 // Clang likes to combine small global variables into the same symbol
3397 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3398 // so we need to look through the whole expression.
3399 bool is_static_lifetime =
3400 has_explicit_mangled ||
3401 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003402 // Check if the location has a DW_OP_addr with any address value...
3403 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3404 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003405 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003406 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3407 if (op_error) {
3408 StreamString strm;
3409 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3410 NULL);
3411 GetObjectFile()->GetModule()->ReportError(
3412 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003413 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003414 }
Sam McCall30621392016-11-22 11:40:25 +00003415 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3416 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003417 }
3418 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003419 if (debug_map_symfile)
3420 // Set the module of the expression to the linked module
3421 // instead of the oject file so the relocated address can be
3422 // found there.
3423 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003424
Sam McCall30621392016-11-22 11:40:25 +00003425 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003426 if (is_external)
3427 scope = eValueTypeVariableGlobal;
3428 else
3429 scope = eValueTypeVariableStatic;
3430
3431 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003432 // When leaving the DWARF in the .o files on darwin, when we have a
3433 // global variable that wasn't initialized, the .o file might not
3434 // have allocated a virtual address for the global variable. In
3435 // this case it will have created a symbol for the global variable
3436 // that is undefined/data and external and the value will be the
3437 // byte size of the variable. When we do the address map in
3438 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3439 // do some magic here so we can get the correct address for our
3440 // global variable. The address for all of these entries will be
3441 // zero, and there will be an undefined symbol in this object file,
3442 // and the executable will have a matching symbol with a good
3443 // address. So here we dig up the correct address and replace it in
3444 // the location for the variable, and set the variable's symbol
3445 // context scope to be that of the main executable so the file
3446 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003447 bool linked_oso_file_addr = false;
3448 if (is_external && location_DW_OP_addr == 0) {
3449 // we have a possible uninitialized extern global
3450 ConstString const_name(mangled ? mangled : name);
3451 ObjectFile *debug_map_objfile =
3452 debug_map_symfile->GetObjectFile();
3453 if (debug_map_objfile) {
3454 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3455 if (debug_map_symtab) {
3456 Symbol *exe_symbol =
3457 debug_map_symtab->FindFirstSymbolWithNameAndType(
3458 const_name, eSymbolTypeData, Symtab::eDebugYes,
3459 Symtab::eVisibilityExtern);
3460 if (exe_symbol) {
3461 if (exe_symbol->ValueIsAddress()) {
3462 const addr_t exe_file_addr =
3463 exe_symbol->GetAddressRef().GetFileAddress();
3464 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3465 if (location.Update_DW_OP_addr(exe_file_addr)) {
3466 linked_oso_file_addr = true;
3467 symbol_context_scope = exe_symbol;
3468 }
3469 }
3470 }
3471 }
3472 }
3473 }
3474 }
3475
3476 if (!linked_oso_file_addr) {
3477 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003478 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003479 const lldb::addr_t exe_file_addr =
3480 debug_map_symfile->LinkOSOFileAddress(this,
3481 location_DW_OP_addr);
3482 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3483 // Update the file address for this variable
3484 location.Update_DW_OP_addr(exe_file_addr);
3485 } else {
3486 // Variable didn't make it into the final executable
3487 return var_sp;
3488 }
3489 }
3490 }
3491 } else {
3492 if (location_is_const_value_data)
3493 scope = eValueTypeVariableStatic;
3494 else {
3495 scope = eValueTypeVariableLocal;
3496 if (debug_map_symfile) {
3497 // We need to check for TLS addresses that we need to fixup
3498 if (location.ContainsThreadLocalStorage()) {
3499 location.LinkThreadLocalStorage(
3500 debug_map_symfile->GetObjectFile()->GetModule(),
3501 [this, debug_map_symfile](
3502 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3503 return debug_map_symfile->LinkOSOFileAddress(
3504 this, unlinked_file_addr);
3505 });
3506 scope = eValueTypeVariableThreadLocal;
3507 }
3508 }
3509 }
3510 }
3511 }
3512
3513 if (symbol_context_scope == NULL) {
3514 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003515 case DW_TAG_subprogram:
3516 case DW_TAG_inlined_subroutine:
3517 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003518 if (sc.function) {
3519 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3520 sc_parent_die.GetID());
3521 if (symbol_context_scope == NULL)
3522 symbol_context_scope = sc.function;
3523 }
3524 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003525
Kate Stoneb9c1b512016-09-06 20:57:50 +00003526 default:
3527 symbol_context_scope = sc.comp_unit;
3528 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003529 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003530 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003531
Kate Stoneb9c1b512016-09-06 20:57:50 +00003532 if (symbol_context_scope) {
3533 SymbolFileTypeSP type_sp(
3534 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3535
3536 if (const_value.Form() && type_sp && type_sp->GetType())
3537 location.CopyOpcodeData(const_value.Unsigned(),
3538 type_sp->GetType()->GetByteSize(),
3539 die.GetCU()->GetAddressByteSize());
3540
3541 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3542 symbol_context_scope, scope_ranges, &decl,
3543 location, is_external, is_artificial,
3544 is_static_member));
3545
3546 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3547 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003548 // Not ready to parse this variable yet. It might be a global or static
3549 // variable that is in a function scope and the function in the symbol
3550 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003551 return var_sp;
3552 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003553 }
Adrian Prantl05097242018-04-30 16:49:04 +00003554 // Cache var_sp even if NULL (the variable was just a specification or was
3555 // missing vital information to be able to be displayed in the debugger
3556 // (missing location due to optimization, etc)) so we don't re-parse this
3557 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003558 GetDIEToVariable()[die.GetDIE()] = var_sp;
3559 if (spec_die)
3560 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3561 }
3562 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003563}
3564
Kate Stoneb9c1b512016-09-06 20:57:50 +00003565DWARFDIE
3566SymbolFileDWARF::FindBlockContainingSpecification(
3567 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3568 // Give the concrete function die specified by "func_die_offset", find the
3569 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3570 // to "spec_block_die_offset"
3571 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3572 spec_block_die_offset);
3573}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003574
Kate Stoneb9c1b512016-09-06 20:57:50 +00003575DWARFDIE
3576SymbolFileDWARF::FindBlockContainingSpecification(
3577 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3578 if (die) {
3579 switch (die.Tag()) {
3580 case DW_TAG_subprogram:
3581 case DW_TAG_inlined_subroutine:
3582 case DW_TAG_lexical_block: {
3583 if (die.GetAttributeValueAsReference(
3584 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3585 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003586
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3588 DW_INVALID_OFFSET) ==
3589 spec_block_die_offset)
3590 return die;
3591 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003593
3594 // Give the concrete function die specified by "func_die_offset", find the
3595 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3596 // to "spec_block_die_offset"
3597 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3598 child_die = child_die.GetSibling()) {
3599 DWARFDIE result_die =
3600 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3601 if (result_die)
3602 return result_die;
3603 }
3604 }
3605
3606 return DWARFDIE();
3607}
3608
3609size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3610 const DWARFDIE &orig_die,
3611 const lldb::addr_t func_low_pc,
3612 bool parse_siblings, bool parse_children,
3613 VariableList *cc_variable_list) {
3614 if (!orig_die)
3615 return 0;
3616
3617 VariableListSP variable_list_sp;
3618
3619 size_t vars_added = 0;
3620 DWARFDIE die = orig_die;
3621 while (die) {
3622 dw_tag_t tag = die.Tag();
3623
3624 // Check to see if we have already parsed this variable or constant?
3625 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3626 if (var_sp) {
3627 if (cc_variable_list)
3628 cc_variable_list->AddVariableIfUnique(var_sp);
3629 } else {
3630 // We haven't already parsed it, lets do that now.
3631 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3632 (tag == DW_TAG_formal_parameter && sc.function)) {
3633 if (variable_list_sp.get() == NULL) {
3634 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3635 dw_tag_t parent_tag = sc_parent_die.Tag();
3636 switch (parent_tag) {
3637 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003638 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003639 if (sc.comp_unit != NULL) {
3640 variable_list_sp = sc.comp_unit->GetVariableList(false);
3641 if (variable_list_sp.get() == NULL) {
3642 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003643 }
3644 } else {
3645 GetObjectFile()->GetModule()->ReportError(
3646 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3647 "symbol context for 0x%8.8" PRIx64
3648 " %s.\n",
3649 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3650 orig_die.GetID(), orig_die.GetTagAsCString());
3651 }
3652 break;
3653
3654 case DW_TAG_subprogram:
3655 case DW_TAG_inlined_subroutine:
3656 case DW_TAG_lexical_block:
3657 if (sc.function != NULL) {
3658 // Check to see if we already have parsed the variables for the
3659 // given scope
3660
3661 Block *block = sc.function->GetBlock(true).FindBlockByID(
3662 sc_parent_die.GetID());
3663 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003664 // This must be a specification or abstract origin with a
3665 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003666 // to find the concrete block so we can correctly add the
3667 // variable to it
3668 const DWARFDIE concrete_block_die =
3669 FindBlockContainingSpecification(
3670 DIERef(sc.function->GetID(), this),
3671 sc_parent_die.GetOffset());
3672 if (concrete_block_die)
3673 block = sc.function->GetBlock(true).FindBlockByID(
3674 concrete_block_die.GetID());
3675 }
3676
3677 if (block != NULL) {
3678 const bool can_create = false;
3679 variable_list_sp = block->GetBlockVariableList(can_create);
3680 if (variable_list_sp.get() == NULL) {
3681 variable_list_sp.reset(new VariableList());
3682 block->SetVariableList(variable_list_sp);
3683 }
3684 }
3685 }
3686 break;
3687
3688 default:
3689 GetObjectFile()->GetModule()->ReportError(
3690 "didn't find appropriate parent DIE for variable list for "
3691 "0x%8.8" PRIx64 " %s.\n",
3692 orig_die.GetID(), orig_die.GetTagAsCString());
3693 break;
3694 }
3695 }
3696
3697 if (variable_list_sp) {
3698 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3699 if (var_sp) {
3700 variable_list_sp->AddVariableIfUnique(var_sp);
3701 if (cc_variable_list)
3702 cc_variable_list->AddVariableIfUnique(var_sp);
3703 ++vars_added;
3704 }
3705 }
3706 }
3707 }
3708
3709 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3710
3711 if (!skip_children && parse_children && die.HasChildren()) {
3712 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3713 true, cc_variable_list);
3714 }
3715
3716 if (parse_siblings)
3717 die = die.GetSibling();
3718 else
3719 die.Clear();
3720 }
3721 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722}
3723
Vedant Kumar4b36f792018-10-05 23:23:15 +00003724/// Collect call graph edges present in a function DIE.
3725static std::vector<lldb_private::CallEdge>
3726CollectCallEdges(DWARFDIE function_die) {
3727 // Check if the function has a supported call site-related attribute.
3728 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3729 uint64_t has_call_edges =
3730 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3731 if (!has_call_edges)
3732 return {};
3733
3734 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3735 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3736 function_die.GetPubname());
3737
3738 // Scan the DIE for TAG_call_site entries.
3739 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3740 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3741 // For now, assume that all entries are nested directly under the subprogram
3742 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3743 std::vector<CallEdge> call_edges;
3744 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3745 child = child.GetSibling()) {
3746 if (child.Tag() != DW_TAG_call_site)
3747 continue;
3748
3749 // Extract DW_AT_call_origin (the call target's DIE).
3750 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3751 if (!call_origin.IsValid()) {
3752 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3753 function_die.GetPubname());
3754 continue;
3755 }
3756
3757 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3758 // available. It should only ever be unavailable for tail call edges, in
3759 // which case use LLDB_INVALID_ADDRESS.
3760 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3761 LLDB_INVALID_ADDRESS);
3762
3763 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3764 call_origin.GetPubname(), return_pc);
3765 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3766 }
3767 return call_edges;
3768}
3769
3770std::vector<lldb_private::CallEdge>
3771SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3772 DWARFDIE func_die = GetDIEFromUID(func_id.GetID());
3773 if (func_die.IsValid())
3774 return CollectCallEdges(func_die);
3775 return {};
3776}
3777
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003778//------------------------------------------------------------------
3779// PluginInterface protocol
3780//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003781ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3782
3783uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3784
Pavel Labath9337b412018-06-06 11:35:23 +00003785void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786
Zachary Turner49110232018-11-05 17:40:28 +00003787void SymbolFileDWARF::DumpClangAST(Stream &s) {
3788 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3789 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3790 if (!clang)
3791 return;
3792 clang->Dump(s);
3793}
3794
Kate Stoneb9c1b512016-09-06 20:57:50 +00003795SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3796 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3797 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3798 if (module_sp) {
3799 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3800 if (sym_vendor)
3801 m_debug_map_symfile =
3802 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003804 }
3805 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003806}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003807
3808DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003809SymbolFileDWARF::GetLocationListFormat() const {
George Rimare4dee262018-10-23 09:46:15 +00003810 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
3811 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003812 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003813}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003814
3815SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3816 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003817 ModuleSpec module_spec;
3818 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3819 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003820 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003821 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003822 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003823 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3824 dwp_filespec);
3825 }
3826 });
3827 return m_dwp_symfile.get();
3828}