blob: 9c3ee41ad5712229cc64f8d09b0858c131d8d2ff [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolFileDWARF.h"
10
Sean Callanancc427fa2011-07-30 02:42:06 +000011#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000012#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000013
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000015#include "lldb/Core/ModuleList.h"
16#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000019#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000021#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000022#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000023#include "lldb/Utility/Scalar.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000025#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanan4dbb2712015-09-25 20:35:58 +000027#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Pavel Labathe1d18752018-06-07 10:04:44 +000028#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000029
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000030#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000031#include "lldb/Host/Host.h"
32
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000033#include "lldb/Interpreter/OptionValueFileSpecList.h"
34#include "lldb/Interpreter/OptionValueProperties.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000037#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000038#include "lldb/Symbol/ClangUtil.h"
39#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000040#include "lldb/Symbol/CompilerDecl.h"
41#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000042#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000043#include "lldb/Symbol/LineTable.h"
Zachary Turner80552912019-02-27 21:42:10 +000044#include "lldb/Symbol/LocateSymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000047#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000048#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Symbol/VariableList.h"
50
Jim Ingham0e0984e2015-09-02 01:06:46 +000051#include "lldb/Target/Language.h"
Zachary Turnera89ce432019-03-06 18:20:23 +000052#include "lldb/Target/Target.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000053
Pavel Labathb13f0332018-05-21 14:12:52 +000054#include "AppleDWARFIndex.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000055#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000056#include "DWARFASTParserClang.h"
Pavel Labathca9c3de2019-06-12 11:29:50 +000057#include "DWARFCompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "DWARFDebugAbbrev.h"
59#include "DWARFDebugAranges.h"
60#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000062#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000064#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "DWARFFormValue.h"
Pavel Labathca9c3de2019-06-12 11:29:50 +000066#include "DWARFTypeUnit.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000067#include "DWARFUnit.h"
Pavel Labath9337b412018-06-06 11:35:23 +000068#include "DebugNamesDWARFIndex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "LogChannelDWARF.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000070#include "ManualDWARFIndex.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000071#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000072#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000073#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Zachary Turner7d86ee52017-03-08 17:56:08 +000075#include "llvm/Support/FileSystem.h"
76
Adrian Prantl0f30a3b2019-02-13 18:10:41 +000077#include <algorithm>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078#include <map>
Jonas Devlieghere796ac802019-02-11 23:13:08 +000079#include <memory>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080
Matthew Gardinere81df3b2014-08-26 06:57:23 +000081#include <ctype.h>
82#include <string.h>
83
Greg Clayton62742b12010-11-11 01:09:45 +000084//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000085
86#ifdef ENABLE_DEBUG_PRINTF
87#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000088#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000089#else
90#define DEBUG_PRINTF(fmt, ...)
91#endif
92
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093using namespace lldb;
94using namespace lldb_private;
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096// static inline bool
97// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000098//{
99// switch (tag)
100// {
101// default:
102// break;
103// case DW_TAG_subprogram:
104// case DW_TAG_inlined_subroutine:
105// case DW_TAG_class_type:
106// case DW_TAG_structure_type:
107// case DW_TAG_union_type:
108// return true;
109// }
110// return false;
111//}
112//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000113
114namespace {
115
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000116static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000118 {},
Pavel Labath9337b412018-06-06 11:35:23 +0000119 "If the DW_AT_comp_dir matches any of these paths the symbolic "
120 "links will be resolved at DWARF parse time."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000121 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
Pavel Labath9337b412018-06-06 11:35:23 +0000122 "Ignore indexes present in the object files and always index DWARF "
Tatyana Krasnukhae40db052018-09-27 07:11:58 +0000123 "manually."}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000124
Pavel Labath9337b412018-06-06 11:35:23 +0000125enum {
126 ePropertySymLinkPaths,
127 ePropertyIgnoreIndexes,
128};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130class PluginProperties : public Properties {
131public:
132 static ConstString GetSettingName() {
133 return SymbolFileDWARF::GetPluginNameStatic();
134 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 PluginProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000137 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 m_collection_sp->Initialize(g_properties);
139 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000140
Frederic Rissacbf0052019-04-23 20:17:04 +0000141 FileSpecList GetSymLinkPaths() {
142 const OptionValueFileSpecList *option_value =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
144 nullptr, true, ePropertySymLinkPaths);
145 assert(option_value);
146 return option_value->GetCurrentValue();
147 }
Pavel Labath9337b412018-06-06 11:35:23 +0000148
149 bool IgnoreFileIndexes() const {
150 return m_collection_sp->GetPropertyAtIndexAsBoolean(
151 nullptr, ePropertyIgnoreIndexes, false);
152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
158 static const auto g_settings_sp(std::make_shared<PluginProperties>());
159 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000163
Frederic Rissacbf0052019-04-23 20:17:04 +0000164FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
Pavel Labath7d36d722019-01-16 12:30:41 +0000165 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000166}
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) {
Pavel Labathff9b4262019-05-17 08:26:58 +0000201 return new SymbolFileDWARF(obj_file,
202 /*dwo_section_list*/ nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000206 // This method can be called without going through the symbol vendor so we
207 // need to lock the module.
208 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
210 if (debug_map_symfile)
211 return debug_map_symfile->GetTypeList();
212 else
213 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000214}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
216 dw_offset_t max_die_offset, uint32_t type_mask,
217 TypeSet &type_set) {
218 if (die) {
219 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (die_offset >= max_die_offset)
222 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (die_offset >= min_die_offset) {
225 const dw_tag_t tag = die.Tag();
226
227 bool add_type = false;
228
229 switch (tag) {
230 case DW_TAG_array_type:
231 add_type = (type_mask & eTypeClassArray) != 0;
232 break;
233 case DW_TAG_unspecified_type:
234 case DW_TAG_base_type:
235 add_type = (type_mask & eTypeClassBuiltin) != 0;
236 break;
237 case DW_TAG_class_type:
238 add_type = (type_mask & eTypeClassClass) != 0;
239 break;
240 case DW_TAG_structure_type:
241 add_type = (type_mask & eTypeClassStruct) != 0;
242 break;
243 case DW_TAG_union_type:
244 add_type = (type_mask & eTypeClassUnion) != 0;
245 break;
246 case DW_TAG_enumeration_type:
247 add_type = (type_mask & eTypeClassEnumeration) != 0;
248 break;
249 case DW_TAG_subroutine_type:
250 case DW_TAG_subprogram:
251 case DW_TAG_inlined_subroutine:
252 add_type = (type_mask & eTypeClassFunction) != 0;
253 break;
254 case DW_TAG_pointer_type:
255 add_type = (type_mask & eTypeClassPointer) != 0;
256 break;
257 case DW_TAG_rvalue_reference_type:
258 case DW_TAG_reference_type:
259 add_type = (type_mask & eTypeClassReference) != 0;
260 break;
261 case DW_TAG_typedef:
262 add_type = (type_mask & eTypeClassTypedef) != 0;
263 break;
264 case DW_TAG_ptr_to_member_type:
265 add_type = (type_mask & eTypeClassMemberPointer) != 0;
266 break;
267 }
268
269 if (add_type) {
270 const bool assert_not_being_parsed = true;
271 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
272 if (type) {
273 if (type_set.find(type) == type_set.end())
274 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000275 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000277 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278
279 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
280 child_die = child_die.GetSibling()) {
281 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
282 }
283 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000284}
285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000287 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000288
289{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000290 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000292
Konrad Kleine248a1302019-05-23 11:14:47 +0000293 CompileUnit *comp_unit = nullptr;
294 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 if (sc_scope)
296 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 if (comp_unit) {
299 dwarf_cu = GetDWARFCompileUnit(comp_unit);
Konrad Kleine248a1302019-05-23 11:14:47 +0000300 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 return 0;
302 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000303 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 } else {
305 DWARFDebugInfo *info = DebugInfo();
306 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000307 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000309 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 if (dwarf_cu) {
311 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000312 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 }
316
317 std::set<CompilerType> compiler_type_set;
318 size_t num_types_added = 0;
319 for (Type *type : type_set) {
320 CompilerType compiler_type = type->GetForwardCompilerType();
321 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
322 compiler_type_set.insert(compiler_type);
323 type_list.Insert(type->shared_from_this());
324 ++num_types_added;
325 }
326 }
327 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000328}
329
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330// Gets the first parent that is a lexical block, function or inlined
331// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000332DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
334 DWARFDIE die;
335 for (die = child_die.GetParent(); die; die = die.GetParent()) {
336 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 switch (tag) {
339 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000340 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 case DW_TAG_subprogram:
342 case DW_TAG_inlined_subroutine:
343 case DW_TAG_lexical_block:
344 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 }
347 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348}
349
Pavel Labathff9b4262019-05-17 08:26:58 +0000350SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile,
351 SectionList *dwo_section_list)
Pavel Labathf4014e12019-05-16 11:07:58 +0000352 : SymbolFile(objfile),
353 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
354 // when this class parses .o files to
355 // contain the .o file index/ID
Konrad Kleine248a1302019-05-23 11:14:47 +0000356 m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
Pavel Labathff9b4262019-05-17 08:26:58 +0000357 m_context(objfile->GetModule()->GetSectionList(), dwo_section_list),
Pavel Labath0261b942019-05-20 08:38:47 +0000358 m_data_debug_loc(), m_data_debug_ranges(), m_data_debug_rnglists(),
Jonas Devlieghered02da8f2019-05-28 22:33:30 +0000359 m_abbr(), m_info(), m_fetched_external_modules(false),
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000360 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363SymbolFileDWARF::~SymbolFileDWARF() {}
364
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000365static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 static ConstString g_dwarf_section_name("__DWARF");
367 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368}
369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
371 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
372 if (debug_map_symfile)
373 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
374 else
375 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000376}
377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
379 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
380 TypeSystem *type_system;
381 if (debug_map_symfile) {
382 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
383 } else {
384 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
385 if (type_system)
386 type_system->SetSymbolFile(this);
387 }
388 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000389}
390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000392 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393
Pavel Labath9337b412018-06-06 11:35:23 +0000394 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
395 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
396 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
397 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
398 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
399 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400
Pavel Labath9337b412018-06-06 11:35:23 +0000401 m_index = AppleDWARFIndex::Create(
402 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
Pavel Labath0261b942019-05-20 08:38:47 +0000403 apple_types, apple_objc, m_context.getOrLoadStrData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404
Pavel Labath9337b412018-06-06 11:35:23 +0000405 if (m_index)
406 return;
407
408 DWARFDataExtractor debug_names;
409 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
410 if (debug_names.GetByteSize() > 0) {
411 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
Pavel Labath0261b942019-05-20 08:38:47 +0000412 DebugNamesDWARFIndex::Create(
413 *GetObjectFile()->GetModule(), debug_names,
414 m_context.getOrLoadStrData(), DebugInfo());
Pavel Labath9337b412018-06-06 11:35:23 +0000415 if (index_or) {
416 m_index = std::move(*index_or);
417 return;
418 }
419 LLDB_LOG_ERROR(log, index_or.takeError(),
420 "Unable to read .debug_names data: {0}");
421 }
422 }
423
424 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
425 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426}
427
428bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000429 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430}
431
432uint32_t SymbolFileDWARF::CalculateAbilities() {
433 uint32_t abilities = 0;
Konrad Kleine248a1302019-05-23 11:14:47 +0000434 if (m_obj_file != nullptr) {
435 const Section *section = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 const SectionList *section_list = m_obj_file->GetSectionList();
Konrad Kleine248a1302019-05-23 11:14:47 +0000437 if (section_list == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 return 0;
439
440 uint64_t debug_abbrev_file_size = 0;
441 uint64_t debug_info_file_size = 0;
442 uint64_t debug_line_file_size = 0;
443
444 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
445
446 if (section)
447 section_list = &section->GetChildren();
448
449 section =
450 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
Konrad Kleine248a1302019-05-23 11:14:47 +0000451 if (section != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 debug_info_file_size = section->GetFileSize();
453
454 section =
455 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
456 .get();
457 if (section)
458 debug_abbrev_file_size = section->GetFileSize();
459
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000460 DWARFDebugAbbrev *abbrev = DebugAbbrev();
461 if (abbrev) {
462 std::set<dw_form_t> invalid_forms;
463 abbrev->GetUnsupportedForms(invalid_forms);
464 if (!invalid_forms.empty()) {
465 StreamString error;
466 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
467 for (auto form : invalid_forms)
468 error.Printf(" %#x", form);
469 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
470 return 0;
471 }
472 }
473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 section =
475 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
476 .get();
477 if (section)
478 debug_line_file_size = section->GetFileSize();
479 } else {
480 const char *symfile_dir_cstr =
481 m_obj_file->GetFileSpec().GetDirectory().GetCString();
482 if (symfile_dir_cstr) {
483 if (strcasestr(symfile_dir_cstr, ".dsym")) {
484 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000485 // We have a dSYM file that didn't have a any debug info. If the
486 // string table has a size of 1, then it was made from an
487 // executable with no debug info, or from an executable that was
488 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 section =
490 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
491 .get();
492 if (section && section->GetFileSize() == 1) {
493 m_obj_file->GetModule()->ReportWarning(
494 "empty dSYM file detected, dSYM was created with an "
495 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501
502 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
503 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
504 LocalVariables | VariableTypes;
505
506 if (debug_line_file_size > 0)
507 abilities |= LineTables;
508 }
509 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512const DWARFDataExtractor &
513SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
514 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000515 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
516 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
517 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519}
520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
522 DWARFDataExtractor &data) {
523 ModuleSP module_sp(m_obj_file->GetModule());
524 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000525 if (!section_list)
526 return;
527
528 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
529 if (!section_sp)
530 return;
531
532 data.Clear();
533 m_obj_file->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534}
535
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000536const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
537 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
538 if (debugLocData.GetByteSize() > 0)
539 return debugLocData;
540 return get_debug_loclists_data();
541}
542
543const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
544 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
545}
546
547const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
548 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
549 m_data_debug_loclists);
550}
551
552const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
553 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
554 m_data_debug_ranges);
555}
556
557const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
558 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
559 m_data_debug_rnglists);
560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000563 if (m_abbr)
564 return m_abbr.get();
565
Pavel Labath0261b942019-05-20 08:38:47 +0000566 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000567 if (debug_abbrev_data.GetByteSize() == 0)
568 return nullptr;
569
570 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
571 llvm::Error error = abbr->parse(debug_abbrev_data);
572 if (error) {
573 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
574 LLDB_LOG_ERROR(log, std::move(error),
575 "Unable to read .debug_abbrev section: {0}");
576 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000578
579 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
584 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000588 if (m_info == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000589 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
590 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
591 static_cast<void *>(this));
Pavel Labath6a2eb362019-06-14 13:01:16 +0000592 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0)
593 m_info = llvm::make_unique<DWARFDebugInfo>(*this, m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 }
595 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596}
597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
599 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000602DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
604 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000605 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606
Alex Langford5ada8872019-06-08 00:55:03 +0000607 DWARFDebugInfo *info = DebugInfo();
608 if (info) {
609 // The compile unit ID is the index of the DWARF unit.
610 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
611 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
612 dwarf_cu->SetUserData(comp_unit);
613 return dwarf_cu;
614 }
615 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616}
617
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000618DWARFDebugRangesBase *SymbolFileDWARF::GetDebugRanges() {
619 if (!m_ranges) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000620 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
621 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
622 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000623
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000624 if (get_debug_ranges_data().GetByteSize() > 0)
625 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000626
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000627 if (m_ranges)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000628 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 }
630 return m_ranges.get();
631}
632
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000633DWARFDebugRangesBase *SymbolFileDWARF::GetDebugRngLists() {
634 if (!m_rnglists) {
635 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
636 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
637 static_cast<void *>(this));
638
639 if (get_debug_rnglists_data().GetByteSize() > 0)
640 m_rnglists.reset(new DWARFDebugRngLists());
641
642 if (m_rnglists)
643 m_rnglists->Extract(this);
644 }
645 return m_rnglists.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646}
647
Pavel Labathad17e282019-06-13 11:22:47 +0000648lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 CompUnitSP cu_sp;
Pavel Labathad17e282019-06-13 11:22:47 +0000650 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
651 if (comp_unit) {
652 // We already parsed this compile unit, had out a shared pointer to it
653 cu_sp = comp_unit->shared_from_this();
654 } else {
Pavel Labath6a2eb362019-06-14 13:01:16 +0000655 if (&dwarf_cu.GetSymbolFileDWARF() != this) {
656 return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu);
Pavel Labathad17e282019-06-13 11:22:47 +0000657 } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
658 // Let the debug map create the compile unit
659 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
660 dwarf_cu.SetUserData(cu_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 } else {
Pavel Labathad17e282019-06-13 11:22:47 +0000662 ModuleSP module_sp(m_obj_file->GetModule());
663 if (module_sp) {
664 const DWARFDIE cu_die = dwarf_cu.DIE();
665 if (cu_die) {
666 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
667 if (cu_file_spec) {
668 // If we have a full path to the compile unit, we don't need to
669 // resolve the file. This can be expensive e.g. when the source
670 // files are NFS mounted.
671 cu_file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672
Pavel Labathad17e282019-06-13 11:22:47 +0000673 std::string remapped_file;
674 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
675 remapped_file))
676 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 }
Pavel Labathad17e282019-06-13 11:22:47 +0000678
679 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
680 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
681
682 bool is_optimized = dwarf_cu.GetIsOptimized();
683 BuildCuTranslationTable();
684 cu_sp = std::make_shared<CompileUnit>(
685 module_sp, &dwarf_cu, cu_file_spec,
686 *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
687 is_optimized ? eLazyBoolYes : eLazyBoolNo);
688
689 dwarf_cu.SetUserData(cu_sp.get());
690
691 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
692 dwarf_cu.GetID(), cu_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 }
694 }
695 }
696 }
697 return cu_sp;
698}
699
Pavel Labathad17e282019-06-13 11:22:47 +0000700void SymbolFileDWARF::BuildCuTranslationTable() {
701 if (!m_lldb_cu_to_dwarf_unit.empty())
702 return;
703
704 DWARFDebugInfo *info = DebugInfo();
705 if (!info)
706 return;
707
708 if (!info->ContainsTypeUnits()) {
709 // We can use a 1-to-1 mapping. No need to build a translation table.
710 return;
711 }
712 for (uint32_t i = 0, num = info->GetNumUnits(); i < num; ++i) {
713 if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info->GetUnitAtIndex(i))) {
714 cu->SetID(m_lldb_cu_to_dwarf_unit.size());
715 m_lldb_cu_to_dwarf_unit.push_back(i);
716 }
717 }
718}
719
720llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
721 BuildCuTranslationTable();
722 if (m_lldb_cu_to_dwarf_unit.empty())
723 return cu_idx;
724 if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
725 return llvm::None;
726 return m_lldb_cu_to_dwarf_unit[cu_idx];
727}
728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729uint32_t SymbolFileDWARF::GetNumCompileUnits() {
730 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000731 if (!info)
732 return 0;
733 BuildCuTranslationTable();
734 return m_lldb_cu_to_dwarf_unit.empty() ? info->GetNumUnits()
735 : m_lldb_cu_to_dwarf_unit.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736}
737
738CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000739 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000741 if (!info)
742 return {};
743
744 if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
745 if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
746 info->GetUnitAtIndex(*dwarf_idx)))
747 return ParseCompileUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 }
Pavel Labathad17e282019-06-13 11:22:47 +0000749 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750}
751
Zachary Turner863f8c12019-01-11 18:03:20 +0000752Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
753 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000754 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 if (die.IsValid()) {
756 TypeSystem *type_system =
757 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
758
759 if (type_system) {
760 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
761 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000762 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 }
764 }
765 return nullptr;
766}
767
768bool SymbolFileDWARF::FixupAddress(Address &addr) {
769 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
770 if (debug_map_symfile) {
771 return debug_map_symfile->LinkOSOAddress(addr);
772 }
773 // This is a normal DWARF file, no address fixups need to happen
774 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000775}
Zachary Turner863f8c12019-01-11 18:03:20 +0000776lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000777 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000778 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 if (dwarf_cu)
780 return dwarf_cu->GetLanguageType();
781 else
782 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000783}
784
Zachary Turner863f8c12019-01-11 18:03:20 +0000785size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000786 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000787 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000788 if (!dwarf_cu)
789 return 0;
790
791 size_t functions_added = 0;
792 std::vector<DWARFDIE> function_dies;
793 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
794 for (const DWARFDIE &die : function_dies) {
795 if (comp_unit.FindFunctionByUID(die.GetID()))
796 continue;
797 if (ParseFunction(comp_unit, die))
798 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000800 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802}
803
Zachary Turner863f8c12019-01-11 18:03:20 +0000804bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
805 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000806 ASSERT_MODULE_LOCK(this);
Pavel Labathad17e282019-06-13 11:22:47 +0000807 if (DWARFUnit *unit = GetDWARFCompileUnit(&comp_unit)) {
Pavel Labathca9c3de2019-06-12 11:29:50 +0000808 const dw_offset_t stmt_list = unit->GetLineTableOffset();
809 if (stmt_list != DW_INVALID_OFFSET) {
810 // All file indexes in DWARF are one based and a file of index zero is
811 // supposed to be the compile unit itself.
812 support_files.Append(comp_unit);
813 return DWARFDebugLine::ParseSupportFiles(comp_unit.GetModule(),
814 m_context.getOrLoadLineData(),
815 stmt_list, support_files, unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000816 }
817 }
818 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819}
820
Pavel Labathca9c3de2019-06-12 11:29:50 +0000821FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) {
Pavel Labathad17e282019-06-13 11:22:47 +0000822 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) {
823 if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu))
824 return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx);
825 return FileSpec();
826 }
827
828 auto &tu = llvm::cast<DWARFTypeUnit>(unit);
829 return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx);
Pavel Labathca9c3de2019-06-12 11:29:50 +0000830}
831
832const FileSpecList &
833SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) {
834 static FileSpecList empty_list;
835
836 dw_offset_t offset = tu.GetLineTableOffset();
837 if (offset == DW_INVALID_OFFSET ||
838 offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() ||
839 offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey())
840 return empty_list;
841
842 // Many type units can share a line table, so parse the support file list
843 // once, and cache it based on the offset field.
844 auto iter_bool = m_type_unit_support_files.try_emplace(offset);
845 FileSpecList &list = iter_bool.first->second;
846 if (iter_bool.second) {
847 list.Append(FileSpec());
848 DWARFDebugLine::ParseSupportFiles(GetObjectFile()->GetModule(),
849 m_context.getOrLoadLineData(), offset,
850 list, &tu);
851 }
852 return list;
853}
854
Zachary Turner863f8c12019-01-11 18:03:20 +0000855bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000856 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000857 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 if (dwarf_cu)
859 return dwarf_cu->GetIsOptimized();
860 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000861}
862
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863bool SymbolFileDWARF::ParseImportedModules(
864 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000865 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000866 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000868 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000869 if (!dwarf_cu)
870 return false;
871 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
872 sc.comp_unit->GetLanguage()))
873 return false;
874 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000876 const DWARFDIE die = dwarf_cu->DIE();
877 if (!die)
878 return false;
879
880 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
881 child_die = child_die.GetSibling()) {
882 if (child_die.Tag() != DW_TAG_imported_declaration)
883 continue;
884
885 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
886 if (module_die.Tag() != DW_TAG_module)
887 continue;
888
889 if (const char *name =
890 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
891 SourceModule module;
892 module.path.push_back(ConstString(name));
893
894 DWARFDIE parent_die = module_die;
895 while ((parent_die = parent_die.GetParent())) {
896 if (parent_die.Tag() != DW_TAG_module)
897 break;
898 if (const char *name =
899 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
900 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000902 std::reverse(module.path.begin(), module.path.end());
903 if (const char *include_path = module_die.GetAttributeValueAsString(
904 DW_AT_LLVM_include_path, nullptr))
905 module.search_path = ConstString(include_path);
906 if (const char *sysroot = module_die.GetAttributeValueAsString(
907 DW_AT_LLVM_isysroot, nullptr))
908 module.sysroot = ConstString(sysroot);
909 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000912 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000913}
914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915struct ParseDWARFLineTableCallbackInfo {
916 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919};
920
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922static void ParseDWARFLineTableCallback(dw_offset_t offset,
923 const DWARFDebugLine::State &state,
924 void *userData) {
925 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
926 // Just started parsing the line table
927 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
928 // Done parsing line table, nothing to do for the cleanup
929 } else {
930 ParseDWARFLineTableCallbackInfo *info =
931 (ParseDWARFLineTableCallbackInfo *)userData;
932 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933
Adrian Prantl05097242018-04-30 16:49:04 +0000934 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000935 if (!info->sequence_up) {
936 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
937 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000940 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 state.column, state.file, state.is_stmt, state.basic_block,
942 state.prologue_end, state.epilogue_begin, state.end_sequence);
943 if (state.end_sequence) {
944 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000945 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000947 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 }
949 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950}
951
Zachary Turner863f8c12019-01-11 18:03:20 +0000952bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000953 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +0000954 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956
Zachary Turner863f8c12019-01-11 18:03:20 +0000957 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000958 if (dwarf_cu) {
959 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
960 if (dwarf_cu_die) {
961 const dw_offset_t cu_line_offset =
962 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
963 DW_INVALID_OFFSET);
964 if (cu_line_offset != DW_INVALID_OFFSET) {
965 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
966 if (line_table_up) {
967 ParseDWARFLineTableCallbackInfo info;
968 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000969
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000970 /*
971 * MIPS:
972 * The SymbolContext may not have a valid target, thus we may not be
973 * able
974 * to call Address::GetOpcodeLoadAddress() which would clear the bit
975 * #0
976 * for MIPS. Use ArchSpec to clear the bit #0.
977 */
978 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
979 case llvm::Triple::mips:
980 case llvm::Triple::mipsel:
981 case llvm::Triple::mips64:
982 case llvm::Triple::mips64el:
983 info.addr_mask = ~((lldb::addr_t)1);
984 break;
985 default:
986 info.addr_mask = ~((lldb::addr_t)0);
987 break;
988 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000989
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000990 lldb::offset_t offset = cu_line_offset;
Pavel Labath0261b942019-05-20 08:38:47 +0000991 DWARFDebugLine::ParseStatementTable(
992 m_context.getOrLoadLineData(), &offset,
993 ParseDWARFLineTableCallback, &info, dwarf_cu);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000994 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
995 if (debug_map_symfile) {
996 // We have an object file that has a line table with addresses that
997 // are not linked. We need to link the line table and convert the
998 // addresses that are relative to the .o file into addresses for
999 // the main executable.
1000 comp_unit.SetLineTable(
1001 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
1002 } else {
1003 comp_unit.SetLineTable(line_table_up.release());
1004 return true;
1005 }
1006 }
1007 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 }
1010 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011}
1012
Siva Chandrad8335e92015-12-16 00:22:08 +00001013lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1015 auto iter = m_debug_macros_map.find(*offset);
1016 if (iter != m_debug_macros_map.end())
1017 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001018
Pavel Labath0261b942019-05-20 08:38:47 +00001019 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001020 if (debug_macro_data.GetByteSize() == 0)
1021 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1024 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001027 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +00001028 DWARFDebugMacroEntry::ReadMacroEntries(
1029 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
1030 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001033}
1034
Zachary Turner863f8c12019-01-11 18:03:20 +00001035bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001036 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001037
Zachary Turner863f8c12019-01-11 18:03:20 +00001038 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001040 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041
Jan Kratochvil93afb052018-05-24 20:51:13 +00001042 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 if (!dwarf_cu_die)
1044 return false;
1045
1046 lldb::offset_t sect_offset =
1047 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1048 if (sect_offset == DW_INVALID_OFFSET)
1049 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1050 DW_INVALID_OFFSET);
1051 if (sect_offset == DW_INVALID_OFFSET)
1052 return false;
1053
Zachary Turner863f8c12019-01-11 18:03:20 +00001054 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055
1056 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001057}
1058
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001059size_t SymbolFileDWARF::ParseBlocksRecursive(
1060 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1061 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 size_t blocks_added = 0;
1063 DWARFDIE die = orig_die;
1064 while (die) {
1065 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 switch (tag) {
1068 case DW_TAG_inlined_subroutine:
1069 case DW_TAG_subprogram:
1070 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001071 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001073 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1074 // inlined functions. These will be parsed on their own as separate
1075 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076
1077 if (depth > 0)
1078 break;
1079
1080 block = parent_block;
1081 } else {
1082 BlockSP block_sp(new Block(die.GetID()));
1083 parent_block->AddChild(block_sp);
1084 block = block_sp.get();
1085 }
1086 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001087 const char *name = nullptr;
1088 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089
1090 int decl_file = 0;
1091 int decl_line = 0;
1092 int decl_column = 0;
1093 int call_file = 0;
1094 int call_line = 0;
1095 int call_column = 0;
1096 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1097 decl_line, decl_column, call_file, call_line,
1098 call_column, nullptr)) {
1099 if (tag == DW_TAG_subprogram) {
1100 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1101 subprogram_low_pc = ranges.GetMinRangeBase(0);
1102 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001103 // We get called here for inlined subroutines in two ways. The first
1104 // time is when we are making the Function object for this inlined
1105 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001107 // need to adjust the containing address. The second time is when we
1108 // are parsing the blocks inside the function that contains the
1109 // inlined concrete instance. Since these will be blocks inside the
1110 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1112 subprogram_low_pc = ranges.GetMinRangeBase(0);
1113 }
1114 }
1115
1116 const size_t num_ranges = ranges.GetSize();
1117 for (size_t i = 0; i < num_ranges; ++i) {
1118 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1119 const addr_t range_base = range.GetRangeBase();
1120 if (range_base >= subprogram_low_pc)
1121 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1122 range.GetByteSize()));
1123 else {
1124 GetObjectFile()->GetModule()->ReportError(
1125 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1126 ") which has a base that is less than the function's low PC "
1127 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1128 "start of this error message",
1129 block->GetID(), range_base, range.GetRangeEnd(),
1130 subprogram_low_pc);
1131 }
1132 }
1133 block->FinalizeRanges();
1134
1135 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001136 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001137 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001139 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001140 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 decl_line, decl_column));
1142
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001143 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001145 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001146 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 call_line, call_column));
1148
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001149 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1150 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 }
1152
1153 ++blocks_added;
1154
1155 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001156 blocks_added +=
1157 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1158 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 }
1160 }
1161 } break;
1162 default:
1163 break;
1164 }
1165
Adrian Prantl05097242018-04-30 16:49:04 +00001166 // Only parse siblings of the block if we are not at depth zero. A depth of
1167 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1168 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169
1170 if (depth == 0)
1171 die.Clear();
1172 else
1173 die = die.GetSibling();
1174 }
1175 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001176}
1177
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1179 if (parent_die) {
1180 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1181 die = die.GetSibling()) {
1182 dw_tag_t tag = die.Tag();
1183 bool check_virtuality = false;
1184 switch (tag) {
1185 case DW_TAG_inheritance:
1186 case DW_TAG_subprogram:
1187 check_virtuality = true;
1188 break;
1189 default:
1190 break;
1191 }
1192 if (check_virtuality) {
1193 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1194 return true;
1195 }
1196 }
1197 }
1198 return false;
1199}
1200
1201void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1202 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1203 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1204 std::vector<DWARFDIE> decl_ctx_die_list =
1205 ast_parser->GetDIEForDeclContext(decl_ctx);
1206
1207 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1208 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1209 decl = decl.GetSibling())
1210 ast_parser->GetDeclForUIDFromDWARF(decl);
1211}
1212
Pavel Labath2841e6e2019-05-08 11:43:05 +00001213SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001214 // This method can be called without going through the symbol vendor so we
1215 // need to lock the module.
1216 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001217 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1218 // must make sure we use the correct DWARF file when resolving things. On
1219 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1220 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1221 // references to other DWARF objects and we must be ready to receive a
1222 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001224 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1225 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labathf4014e12019-05-16 11:07:58 +00001227 return {dwarf, {DIERef::Section::DebugInfo, DW_INVALID_OFFSET, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001228 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001229 DIERef::Section section =
1230 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1231 uint32_t dwarf_id = uid >> 32 & 0x7fffffff;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001232 dw_offset_t die_offset = uid;
1233
1234 if (die_offset == DW_INVALID_OFFSET)
1235 return {nullptr, DIERef()};
1236
1237 SymbolFileDWARF *dwarf = this;
1238 if (DebugInfo()) {
1239 if (DWARFUnit *unit = DebugInfo()->GetUnitAtIndex(dwarf_id)) {
1240 if (unit->GetDwoSymbolFile())
1241 dwarf = unit->GetDwoSymbolFile();
1242 }
1243 }
Pavel Labathf4014e12019-05-16 11:07:58 +00001244 return {dwarf, {section, DW_INVALID_OFFSET, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001245}
1246
1247DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001248SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001249 // This method can be called without going through the symbol vendor so we
1250 // need to lock the module.
1251 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001252
1253 DecodedUID decoded = DecodeUID(uid);
1254
1255 if (decoded.dwarf)
1256 return decoded.dwarf->GetDIE(decoded.ref);
1257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001259}
1260
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001262 // This method can be called without going through the symbol vendor so we
1263 // need to lock the module.
1264 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001265 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001266 // SymbolFileDWARF::GetDIE(). See comments inside the
1267 // SymbolFileDWARF::GetDIE() for details.
1268 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 return die.GetDecl();
1270 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001271}
1272
Greg Clayton99558cc42015-08-24 23:46:31 +00001273CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001275 // This method can be called without going through the symbol vendor so we
1276 // need to lock the module.
1277 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001278 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001279 // SymbolFileDWARF::GetDIE(). See comments inside the
1280 // SymbolFileDWARF::GetDIE() for details.
1281 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 return die.GetDeclContext();
1283 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001284}
1285
Greg Clayton99558cc42015-08-24 23:46:31 +00001286CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001288 // This method can be called without going through the symbol vendor so we
1289 // need to lock the module.
1290 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001291 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001292 // SymbolFileDWARF::GetDIE(). See comments inside the
1293 // SymbolFileDWARF::GetDIE() for details.
1294 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 return die.GetContainingDeclContext();
1296 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297}
1298
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001300 // This method can be called without going through the symbol vendor so we
1301 // need to lock the module.
1302 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001303 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001304 // SymbolFileDWARF::GetDIE(). See comments inside the
1305 // SymbolFileDWARF::GetDIE() for details.
1306 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 return type_die.ResolveType();
1308 else
1309 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310}
1311
Adrian Prantleca07c52018-11-05 20:49:07 +00001312llvm::Optional<SymbolFile::ArrayInfo>
1313SymbolFileDWARF::GetDynamicArrayInfoForUID(
1314 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1315 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001316 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001317 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1318 else
1319 return llvm::None;
1320}
1321
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1323 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001324}
1325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1327 bool assert_not_being_parsed) {
1328 if (die) {
1329 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1330 if (log)
1331 GetObjectFile()->GetModule()->LogMessage(
1332 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1333 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001334
Adrian Prantl05097242018-04-30 16:49:04 +00001335 // We might be coming in in the middle of a type tree (a class within a
1336 // class, an enum within a class), so parse any needed parent DIEs before
1337 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1339 if (decl_ctx_die) {
1340 if (log) {
1341 switch (decl_ctx_die.Tag()) {
1342 case DW_TAG_structure_type:
1343 case DW_TAG_union_type:
1344 case DW_TAG_class_type: {
1345 // Get the type, which could be a forward declaration
1346 if (log)
1347 GetObjectFile()->GetModule()->LogMessage(
1348 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1349 "resolve parent forward type for 0x%8.8x",
1350 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1351 decl_ctx_die.GetOffset());
1352 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001353
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354 default:
1355 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001356 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001358 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 return ResolveType(die);
1360 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001361 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001362}
1363
Greg Clayton6beaaa62011-01-17 03:46:26 +00001364// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001365// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1366// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367bool SymbolFileDWARF::HasForwardDeclForClangType(
1368 const CompilerType &compiler_type) {
1369 CompilerType compiler_type_no_qualifiers =
1370 ClangUtil::RemoveFastQualifiers(compiler_type);
1371 if (GetForwardDeclClangTypeToDie().count(
1372 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1373 return true;
1374 }
1375 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 ClangASTContext *clang_type_system =
1378 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1379 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001380 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 DWARFASTParserClang *ast_parser =
1382 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1383 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001384}
1385
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001387 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001388
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 ClangASTContext *clang_type_system =
1390 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1391 if (clang_type_system) {
1392 DWARFASTParserClang *ast_parser =
1393 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1394 if (ast_parser &&
1395 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1396 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1397 }
Jim Inghamc3549282012-01-11 02:21:12 +00001398
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 // We have a struct/union/class/enum that needs to be fully resolved.
1400 CompilerType compiler_type_no_qualifiers =
1401 ClangUtil::RemoveFastQualifiers(compiler_type);
1402 auto die_it = GetForwardDeclClangTypeToDie().find(
1403 compiler_type_no_qualifiers.GetOpaqueQualType());
1404 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1405 // We have already resolved this type...
1406 return true;
1407 }
1408
1409 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1410 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001411 // Once we start resolving this type, remove it from the forward
1412 // declaration map in case anyone child members or other types require this
1413 // type to get resolved. The type will get resolved when all of the calls
1414 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 GetForwardDeclClangTypeToDie().erase(die_it);
1416
1417 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1418
1419 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1420 DWARF_LOG_TYPE_COMPLETION));
1421 if (log)
1422 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1423 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1424 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1425 type->GetName().AsCString());
1426 assert(compiler_type);
1427 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1428 if (dwarf_ast)
1429 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1430 }
1431 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001432}
1433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1435 bool assert_not_being_parsed,
1436 bool resolve_function_context) {
1437 if (die) {
1438 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1439
1440 if (assert_not_being_parsed) {
1441 if (type != DIE_IS_BEING_PARSED)
1442 return type;
1443
1444 GetObjectFile()->GetModule()->ReportError(
1445 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1446 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1447
1448 } else
1449 return type;
1450 }
1451 return nullptr;
1452}
1453
Pavel Labathad17e282019-06-13 11:22:47 +00001454CompileUnit *
1455SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001457 if (dwarf_cu.GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001458 // The symbol vendor doesn't know about this compile unit, we need to parse
1459 // and add it to the symbol vendor object.
Pavel Labath519ef6a2019-05-27 10:10:59 +00001460 return ParseCompileUnit(dwarf_cu).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 }
Pavel Labathad17e282019-06-13 11:22:47 +00001462 return (CompileUnit *)dwarf_cu.GetUserData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463}
1464
1465size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1466 DIEArray &method_die_offsets) {
1467 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001468 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469 return method_die_offsets.size();
1470}
1471
1472bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1473 sc.Clear(false);
1474
Pavel Labathad17e282019-06-13 11:22:47 +00001475 if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001477 sc.comp_unit =
1478 GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479
1480 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001481 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001482 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483
1484 if (sc.function) {
1485 sc.module_sp = sc.function->CalculateSymbolContextModule();
1486 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 }
1489
1490 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491}
1492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1494 UpdateExternalModuleListIfNeeded();
1495 const auto &pos = m_external_type_modules.find(name);
1496 if (pos != m_external_type_modules.end())
1497 return pos->second;
1498 else
1499 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001500}
1501
Greg Clayton2f869fe2016-03-30 20:14:35 +00001502DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1504 DWARFDebugInfo *debug_info = DebugInfo();
1505 if (debug_info)
1506 return debug_info->GetDIE(die_ref);
1507 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001508 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001509}
1510
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511std::unique_ptr<SymbolFileDWARFDwo>
1512SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Pavel Labathad17e282019-06-13 11:22:47 +00001513 DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001515 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 // info files for modules.
1517 if (GetDebugMapSymfile())
1518 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001519
Pavel Labathad17e282019-06-13 11:22:47 +00001520 DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit);
1521 // Only compile units can be split into two parts.
1522 if (!dwarf_cu)
1523 return nullptr;
1524
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001525 const char *dwo_name =
Pavel Labathad17e282019-06-13 11:22:47 +00001526 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 if (!dwo_name)
1528 return nullptr;
1529
Tamas Berghammer963ce482017-08-25 13:56:14 +00001530 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1531 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001532 uint64_t dwo_id =
Pavel Labathad17e282019-06-13 11:22:47 +00001533 cu_die.GetAttributeValueAsUnsigned(dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001534 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
Pavel Labathad17e282019-06-13 11:22:47 +00001535 dwp_symfile->GetSymbolFileForDwoId(*dwarf_cu, dwo_id);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001536 if (dwo_symfile)
1537 return dwo_symfile;
1538 }
1539
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001540 FileSpec dwo_file(dwo_name);
1541 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001543 const char *comp_dir =
Pavel Labathad17e282019-06-13 11:22:47 +00001544 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 if (!comp_dir)
1546 return nullptr;
1547
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001548 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1549 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 dwo_file.AppendPathComponent(dwo_name);
1551 }
1552
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001553 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 return nullptr;
1555
1556 const lldb::offset_t file_offset = 0;
1557 DataBufferSP dwo_file_data_sp;
1558 lldb::offset_t dwo_file_data_offset = 0;
1559 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1560 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001561 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1562 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 if (dwo_obj_file == nullptr)
1564 return nullptr;
1565
Pavel Labathad17e282019-06-13 11:22:47 +00001566 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001567}
1568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1570 if (m_fetched_external_modules)
1571 return;
1572 m_fetched_external_modules = true;
1573
1574 DWARFDebugInfo *debug_info = DebugInfo();
1575
1576 const uint32_t num_compile_units = GetNumCompileUnits();
1577 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001578 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579
Jan Kratochvil93afb052018-05-24 20:51:13 +00001580 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001581 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1583
1584 if (name) {
1585 ConstString const_name(name);
1586 if (m_external_type_modules.find(const_name) ==
1587 m_external_type_modules.end()) {
1588 ModuleSP module_sp;
1589 const char *dwo_path =
1590 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1591 if (dwo_path) {
1592 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001593 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001594 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1596 const char *comp_dir =
1597 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1598 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001599 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001600 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001601 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1603 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001604 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 dwo_module_spec.GetArchitecture() =
1606 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001607
Adrian Prantl05097242018-04-30 16:49:04 +00001608 // When LLDB loads "external" modules it looks at the presence of
1609 // DW_AT_GNU_dwo_name. However, when the already created module
1610 // (corresponding to .dwo itself) is being processed, it will see
1611 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1612 // dwo file) and will try to call ModuleList::GetSharedModule
1613 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1614 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1615 // DW_AT_comp_dir. In this case the method
1616 // ModuleList::GetSharedModule will fail and the warning will be
1617 // printed. However, as one can notice in this case we don't
1618 // actually need to try to load the already loaded module
1619 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001620 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001621 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1622 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1623 continue;
1624 }
1625
Zachary Turner97206d52017-05-12 04:51:55 +00001626 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001627 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 if (!module_sp) {
1629 GetObjectFile()->GetModule()->ReportWarning(
1630 "0x%8.8x: unable to locate module needed for external types: "
1631 "%s\nerror: %s\nDebugging will be degraded due to missing "
1632 "types. Rebuilding your project will regenerate the needed "
1633 "module files.",
1634 die.GetOffset(),
1635 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1636 error.AsCString("unknown error"));
1637 }
1638 }
1639 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 }
1644}
1645
1646SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001647 if (!m_global_aranges_up) {
1648 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649
1650 ModuleSP module_sp = GetObjectFile()->GetModule();
1651 if (module_sp) {
1652 const size_t num_cus = module_sp->GetNumCompileUnits();
1653 for (size_t i = 0; i < num_cus; ++i) {
1654 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1655 if (cu_sp) {
1656 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1657 if (globals_sp) {
1658 const size_t num_globals = globals_sp->GetSize();
1659 for (size_t g = 0; g < num_globals; ++g) {
1660 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1661 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1662 const DWARFExpression &location = var_sp->LocationExpression();
1663 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001664 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001665 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1666 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 if (location_result.GetValueType() ==
1668 Value::eValueTypeFileAddress) {
1669 lldb::addr_t file_addr =
1670 location_result.GetScalar().ULongLong();
1671 lldb::addr_t byte_size = 1;
1672 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001673 byte_size =
1674 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001675 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676 file_addr, byte_size, var_sp.get()));
1677 }
1678 }
1679 }
1680 }
1681 }
1682 }
1683 }
1684 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001685 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001687 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001688}
1689
1690uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001691 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001693 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1694 Timer scoped_timer(func_cat,
1695 "SymbolFileDWARF::"
1696 "ResolveSymbolContext (so_addr = { "
1697 "section = %p, offset = 0x%" PRIx64
1698 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699 static_cast<void *>(so_addr.GetSection().get()),
1700 so_addr.GetOffset(), resolve_scope);
1701 uint32_t resolved = 0;
1702 if (resolve_scope &
1703 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1704 eSymbolContextLineEntry | eSymbolContextVariable)) {
1705 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1706
1707 DWARFDebugInfo *debug_info = DebugInfo();
1708 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001709 llvm::Expected<DWARFDebugAranges &> aranges =
1710 debug_info->GetCompileUnitAranges();
1711 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001712 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001713 LLDB_LOG_ERROR(log, aranges.takeError(),
1714 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1715 "aranges. {0}");
1716 return 0;
1717 }
1718
1719 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001721 // Global variables are not in the compile unit address ranges. The
1722 // only way to currently find global variables is to iterate over the
1723 // .debug_pubnames or the __apple_names table and find all items in
1724 // there that point to DW_TAG_variable DIEs and then find the address
1725 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 if (resolve_scope & eSymbolContextVariable) {
1727 GlobalVariableMap &map = GetGlobalAranges();
1728 const GlobalVariableMap::Entry *entry =
1729 map.FindEntryThatContains(file_vm_addr);
1730 if (entry && entry->data) {
1731 Variable *variable = entry->data;
1732 SymbolContextScope *scc = variable->GetSymbolContextScope();
1733 if (scc) {
1734 scc->CalculateSymbolContext(&sc);
1735 sc.variable = variable;
1736 }
1737 return sc.GetResolvedMask();
1738 }
1739 }
1740 } else {
1741 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathad17e282019-06-13 11:22:47 +00001742 if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>(
1743 debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1744 cu_offset, &cu_idx))) {
1745 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 if (sc.comp_unit) {
1747 resolved |= eSymbolContextCompUnit;
1748
1749 bool force_check_line_table = false;
1750 if (resolve_scope &
1751 (eSymbolContextFunction | eSymbolContextBlock)) {
1752 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1753 DWARFDIE block_die;
1754 if (function_die) {
1755 sc.function =
1756 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001757 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001758 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759
1760 if (sc.function && (resolve_scope & eSymbolContextBlock))
1761 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1762 } else {
1763 // We might have had a compile unit that had discontiguous
1764 // address ranges where the gaps are symbols that don't have
1765 // any debug info. Discontiguous compile unit address ranges
1766 // should only happen when there aren't other functions from
1767 // other compile units in these gaps. This helps keep the size
1768 // of the aranges down.
1769 force_check_line_table = true;
1770 }
1771
Konrad Kleine248a1302019-05-23 11:14:47 +00001772 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 resolved |= eSymbolContextFunction;
1774
1775 if (resolve_scope & eSymbolContextBlock) {
1776 Block &block = sc.function->GetBlock(true);
1777
1778 if (block_die)
1779 sc.block = block.FindBlockByID(block_die.GetID());
1780 else
1781 sc.block = block.FindBlockByID(function_die.GetID());
1782 if (sc.block)
1783 resolved |= eSymbolContextBlock;
1784 }
1785 }
1786 }
1787
1788 if ((resolve_scope & eSymbolContextLineEntry) ||
1789 force_check_line_table) {
1790 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001791 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001792 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001793 // terms of this debug file if there is no debug map, or it
1794 // will be an address in the .o file which needs to be fixed up
1795 // to be in terms of the debug map executable. Either way,
1796 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797 Address exe_so_addr(so_addr);
1798 if (FixupAddress(exe_so_addr)) {
1799 if (line_table->FindLineEntryByAddress(exe_so_addr,
1800 sc.line_entry)) {
1801 resolved |= eSymbolContextLineEntry;
1802 }
1803 }
1804 }
1805 }
1806
1807 if (force_check_line_table &&
1808 !(resolved & eSymbolContextLineEntry)) {
1809 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001810 // address ranges where the gaps are symbols that don't have any
1811 // debug info. Discontiguous compile unit address ranges should
1812 // only happen when there aren't other functions from other
1813 // compile units in these gaps. This helps keep the size of the
1814 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001815 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 resolved &= ~eSymbolContextCompUnit;
1817 }
1818 } else {
1819 GetObjectFile()->GetModule()->ReportWarning(
1820 "0x%8.8x: compile unit %u failed to create a valid "
1821 "lldb_private::CompileUnit class.",
1822 cu_offset, cu_idx);
1823 }
1824 }
1825 }
1826 }
1827 }
1828 return resolved;
1829}
1830
1831uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1832 uint32_t line,
1833 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001834 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835 SymbolContextList &sc_list) {
1836 const uint32_t prev_size = sc_list.GetSize();
1837 if (resolve_scope & eSymbolContextCompUnit) {
Pavel Labathad17e282019-06-13 11:22:47 +00001838 for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus;
1839 ++cu_idx) {
1840 CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get();
1841 if (!dc_cu)
1842 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001843
Pavel Labathad17e282019-06-13 11:22:47 +00001844 const bool full_match = (bool)file_spec.GetDirectory();
1845 bool file_spec_matches_cu_file_spec =
1846 FileSpec::Equal(file_spec, *dc_cu, full_match);
1847 if (check_inlines || file_spec_matches_cu_file_spec) {
1848 SymbolContext sc(m_obj_file->GetModule());
1849 sc.comp_unit = dc_cu;
1850 uint32_t file_idx = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851
Pavel Labathad17e282019-06-13 11:22:47 +00001852 // If we are looking for inline functions only and we don't find it
1853 // in the support files, we are done.
1854 if (check_inlines) {
1855 file_idx =
1856 sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true);
1857 if (file_idx == UINT32_MAX)
1858 continue;
1859 }
1860
1861 if (line != 0) {
1862 LineTable *line_table = sc.comp_unit->GetLineTable();
1863
1864 if (line_table != nullptr && line != 0) {
1865 // We will have already looked up the file index if we are
1866 // searching for inline entries.
1867 if (!check_inlines)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1869 1, file_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001870
Pavel Labathad17e282019-06-13 11:22:47 +00001871 if (file_idx != UINT32_MAX) {
1872 uint32_t found_line;
1873 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1874 0, file_idx, line, false, &sc.line_entry);
1875 found_line = sc.line_entry.line;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001876
Pavel Labathad17e282019-06-13 11:22:47 +00001877 while (line_idx != UINT32_MAX) {
1878 sc.function = nullptr;
1879 sc.block = nullptr;
1880 if (resolve_scope &
1881 (eSymbolContextFunction | eSymbolContextBlock)) {
1882 const lldb::addr_t file_vm_addr =
1883 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1884 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1885 DWARFDIE function_die =
1886 GetDWARFCompileUnit(dc_cu)->LookupAddress(file_vm_addr);
1887 DWARFDIE block_die;
1888 if (function_die) {
1889 sc.function =
1890 sc.comp_unit->FindFunctionByUID(function_die.GetID())
1891 .get();
1892 if (sc.function == nullptr)
1893 sc.function =
1894 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895
Pavel Labathad17e282019-06-13 11:22:47 +00001896 if (sc.function && (resolve_scope & eSymbolContextBlock))
1897 block_die =
1898 function_die.LookupDeepestBlock(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899 }
1900
Pavel Labathad17e282019-06-13 11:22:47 +00001901 if (sc.function != nullptr) {
1902 Block &block = sc.function->GetBlock(true);
1903
1904 if (block_die)
1905 sc.block = block.FindBlockByID(block_die.GetID());
1906 else if (function_die)
1907 sc.block = block.FindBlockByID(function_die.GetID());
1908 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001909 }
1910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911
Pavel Labathad17e282019-06-13 11:22:47 +00001912 sc_list.Append(sc);
1913 line_idx = line_table->FindLineEntryIndexByFileIndex(
1914 line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1915 }
1916 }
1917 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1918 // only append the context if we aren't looking for inline call
1919 // sites by file and line and if the file spec matches that of
1920 // the compile unit
1921 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001922 }
Pavel Labathad17e282019-06-13 11:22:47 +00001923 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1924 // only append the context if we aren't looking for inline call
1925 // sites by file and line and if the file spec matches that of
1926 // the compile unit
1927 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928 }
Pavel Labathad17e282019-06-13 11:22:47 +00001929
1930 if (!check_inlines)
1931 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001932 }
1933 }
1934 }
1935 return sc_list.GetSize() - prev_size;
1936}
1937
Jim Ingham7fca8c02017-04-28 00:51:06 +00001938void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001939 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001940 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941}
1942
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001943std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1944 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1945 if (module_sp)
1946 return module_sp->GetMutex();
1947 return GetObjectFile()->GetModule()->GetMutex();
1948}
1949
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1951 const lldb_private::CompilerDeclContext *decl_ctx) {
1952 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001953 // Invalid namespace decl which means we aren't matching only things in
1954 // this symbol file, so return true to indicate it matches this symbol
1955 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 return true;
1957 }
1958
1959 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1960 TypeSystem *type_system = GetTypeSystemForLanguage(
1961 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1962 if (decl_ctx_type_system == type_system)
1963 return true; // The type systems match, return true
1964
1965 // The namespace AST was valid, and it does not match...
1966 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1967
1968 if (log)
1969 GetObjectFile()->GetModule()->LogMessage(
1970 log, "Valid namespace does not match symbol file");
1971
1972 return false;
1973}
1974
1975uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001976 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00001977 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1979
1980 if (log)
1981 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00001982 log,
1983 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
1984 "parent_decl_ctx=%p, max_matches=%u, variables)",
1985 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001986 max_matches);
1987
1988 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1989 return 0;
1990
1991 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00001992 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993 return 0;
1994
Pavel Labath34cda142018-05-31 09:46:26 +00001995 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001996 const uint32_t original_size = variables.GetSize();
1997
Pavel Labathe1d18752018-06-07 10:04:44 +00001998 llvm::StringRef basename;
1999 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002000 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00002001
2002 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2003 context, basename))
2004 basename = name.GetStringRef();
2005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002007 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002008 const size_t num_die_matches = die_offsets.size();
2009 if (num_die_matches) {
2010 SymbolContext sc;
2011 sc.module_sp = m_obj_file->GetModule();
2012 assert(sc.module_sp);
2013
Pavel Labathe1d18752018-06-07 10:04:44 +00002014 // Loop invariant: Variables up to this index have been checked for context
2015 // matches.
2016 uint32_t pruned_idx = original_size;
2017
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 bool done = false;
2019 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2020 const DIERef &die_ref = die_offsets[i];
2021 DWARFDIE die = GetDIE(die_ref);
2022
2023 if (die) {
2024 switch (die.Tag()) {
2025 default:
2026 case DW_TAG_subprogram:
2027 case DW_TAG_inlined_subroutine:
2028 case DW_TAG_try_block:
2029 case DW_TAG_catch_block:
2030 break;
2031
2032 case DW_TAG_variable: {
Pavel Labathad17e282019-06-13 11:22:47 +00002033 auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2034 if (!dwarf_cu)
2035 continue;
2036 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002037
2038 if (parent_decl_ctx) {
2039 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2040 if (dwarf_ast) {
2041 CompilerDeclContext actual_parent_decl_ctx =
2042 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2043 if (!actual_parent_decl_ctx ||
2044 actual_parent_decl_ctx != *parent_decl_ctx)
2045 continue;
2046 }
2047 }
2048
2049 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2050 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002051 while (pruned_idx < variables.GetSize()) {
2052 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002053 if (name_is_mangled ||
2054 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002055 ++pruned_idx;
2056 else
2057 variables.RemoveVariableAtIndex(pruned_idx);
2058 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002059
2060 if (variables.GetSize() - original_size >= max_matches)
2061 done = true;
2062 } break;
2063 }
2064 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002065 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002066 }
2067 }
2068 }
2069
2070 // Return the number of variable that were appended to the list
2071 const uint32_t num_matches = variables.GetSize() - original_size;
2072 if (log && num_matches > 0) {
2073 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002074 log,
2075 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2076 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2077 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002078 max_matches, num_matches);
2079 }
2080 return num_matches;
2081}
2082
2083uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002084 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 VariableList &variables) {
2086 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2087
2088 if (log) {
2089 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002090 log,
2091 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2092 "max_matches=%u, variables)",
2093 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094 }
2095
2096 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002097 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 return 0;
2099
Pavel Labath34cda142018-05-31 09:46:26 +00002100 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101 const uint32_t original_size = variables.GetSize();
2102
2103 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002104 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105
2106 SymbolContext sc;
2107 sc.module_sp = m_obj_file->GetModule();
2108 assert(sc.module_sp);
2109
2110 const size_t num_matches = die_offsets.size();
2111 if (num_matches) {
2112 for (size_t i = 0; i < num_matches; ++i) {
2113 const DIERef &die_ref = die_offsets[i];
2114 DWARFDIE die = GetDIE(die_ref);
2115
2116 if (die) {
Pavel Labathad17e282019-06-13 11:22:47 +00002117 DWARFCompileUnit *dwarf_cu =
2118 llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2119 if (!dwarf_cu)
2120 continue;
2121 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122
2123 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2124
2125 if (variables.GetSize() - original_size >= max_matches)
2126 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002127 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00002128 m_index->ReportInvalidDIERef(die_ref, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129 }
2130 }
2131
2132 // Return the number of variable that were appended to the list
2133 return variables.GetSize() - original_size;
2134}
2135
Kate Stoneb9c1b512016-09-06 20:57:50 +00002136bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2137 bool include_inlines,
2138 SymbolContextList &sc_list) {
2139 SymbolContext sc;
2140
2141 if (!orig_die)
2142 return false;
2143
2144 // If we were passed a die that is not a function, just return false...
2145 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2146 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2147 return false;
2148
2149 DWARFDIE die = orig_die;
2150 DWARFDIE inlined_die;
2151 if (die.Tag() == DW_TAG_inlined_subroutine) {
2152 inlined_die = die;
2153
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002154 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 die = die.GetParent();
2156
2157 if (die) {
2158 if (die.Tag() == DW_TAG_subprogram)
2159 break;
2160 } else
2161 break;
2162 }
2163 }
2164 assert(die && die.Tag() == DW_TAG_subprogram);
2165 if (GetFunction(die, sc)) {
2166 Address addr;
2167 // Parse all blocks if needed
2168 if (inlined_die) {
2169 Block &function_block = sc.function->GetBlock(true);
2170 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002171 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002172 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002173 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174 addr.Clear();
2175 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002176 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 addr = sc.function->GetAddressRange().GetBaseAddress();
2178 }
2179
2180 if (addr.IsValid()) {
2181 sc_list.Append(sc);
2182 return true;
2183 }
2184 }
2185
2186 return false;
2187}
2188
Kate Stoneb9c1b512016-09-06 20:57:50 +00002189bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2190 const DWARFDIE &die) {
2191 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002192 // parent decl context isn't valid, we aren't trying to look for any
2193 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2195 return true;
2196
2197 if (die) {
2198 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2199 if (dwarf_ast) {
2200 CompilerDeclContext actual_decl_ctx =
2201 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2202 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002203 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002204 }
2205 }
2206 return false;
2207}
2208
Zachary Turner117b1fa2018-10-25 20:45:40 +00002209uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002210 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002211 FunctionNameType name_type_mask, bool include_inlines, bool append,
2212 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002213 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2214 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 name.AsCString());
2216
2217 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002218 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2220
2221 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2222
2223 if (log) {
2224 GetObjectFile()->GetModule()->LogMessage(
2225 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2226 "name_type_mask=0x%x, append=%u, sc_list)",
2227 name.GetCString(), name_type_mask, append);
2228 }
2229
2230 // If we aren't appending the results to this list, then clear the list
2231 if (!append)
2232 sc_list.Clear();
2233
2234 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2235 return 0;
2236
2237 // If name is empty then we won't find anything.
2238 if (name.IsEmpty())
2239 return 0;
2240
Adrian Prantl05097242018-04-30 16:49:04 +00002241 // Remember how many sc_list are in the list before we search in case we are
2242 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 const uint32_t original_size = sc_list.GetSize();
2245
2246 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002247 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248 return 0;
2249
Pavel Labath5af11ab2018-06-05 10:33:56 +00002250 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2251 DIEArray offsets;
2252 CompilerDeclContext empty_decl_ctx;
2253 if (!parent_decl_ctx)
2254 parent_decl_ctx = &empty_decl_ctx;
2255
2256 std::vector<DWARFDIE> dies;
2257 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2258 for (const DWARFDIE &die: dies) {
2259 if (resolved_dies.insert(die.GetDIE()).second)
2260 ResolveFunction(die, include_inlines, sc_list);
2261 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262
2263 // Return the number of variable that were appended to the list
2264 const uint32_t num_matches = sc_list.GetSize() - original_size;
2265
2266 if (log && num_matches > 0) {
2267 GetObjectFile()->GetModule()->LogMessage(
2268 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2269 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2270 "%u",
2271 name.GetCString(), name_type_mask, include_inlines, append,
2272 num_matches);
2273 }
2274 return num_matches;
2275}
2276
2277uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2278 bool include_inlines, bool append,
2279 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002280 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2281 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002282 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283
2284 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2285
2286 if (log) {
2287 GetObjectFile()->GetModule()->LogMessage(
2288 log,
2289 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002290 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 }
2292
2293 // If we aren't appending the results to this list, then clear the list
2294 if (!append)
2295 sc_list.Clear();
2296
Pavel Labathb13f0332018-05-21 14:12:52 +00002297 DWARFDebugInfo *info = DebugInfo();
2298 if (!info)
2299 return 0;
2300
Adrian Prantl05097242018-04-30 16:49:04 +00002301 // Remember how many sc_list are in the list before we search in case we are
2302 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002303 uint32_t original_size = sc_list.GetSize();
2304
Pavel Labath6de9c792018-06-05 12:13:22 +00002305 DIEArray offsets;
2306 m_index->GetFunctions(regex, offsets);
2307
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002308 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2309 for (DIERef ref : offsets) {
2310 DWARFDIE die = info->GetDIE(ref);
2311 if (!die) {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002312 m_index->ReportInvalidDIERef(ref, regex.GetText());
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002313 continue;
2314 }
2315 if (resolved_dies.insert(die.GetDIE()).second)
2316 ResolveFunction(die, include_inlines, sc_list);
2317 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002318
2319 // Return the number of variable that were appended to the list
2320 return sc_list.GetSize() - original_size;
2321}
2322
2323void SymbolFileDWARF::GetMangledNamesForFunction(
2324 const std::string &scope_qualified_name,
2325 std::vector<ConstString> &mangled_names) {
2326 DWARFDebugInfo *info = DebugInfo();
2327 uint32_t num_comp_units = 0;
2328 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002329 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330
2331 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002332 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002333 if (cu == nullptr)
2334 continue;
2335
2336 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2337 if (dwo)
2338 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2339 }
2340
2341 NameToOffsetMap::iterator iter =
2342 m_function_scope_qualified_name_map.find(scope_qualified_name);
2343 if (iter == m_function_scope_qualified_name_map.end())
2344 return;
2345
2346 DIERefSetSP set_sp = (*iter).second;
2347 std::set<DIERef>::iterator set_iter;
2348 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2349 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2350 mangled_names.push_back(ConstString(die.GetMangledName()));
2351 }
2352}
2353
2354uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002355 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002356 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002357 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2358 TypeMap &types) {
2359 // If we aren't appending the results to this list, then clear the list
2360 if (!append)
2361 types.Clear();
2362
2363 // Make sure we haven't already searched this SymbolFile before...
2364 if (searched_symbol_files.count(this))
2365 return 0;
2366 else
2367 searched_symbol_files.insert(this);
2368
2369 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002370 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002371 return 0;
2372
2373 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2374
2375 if (log) {
2376 if (parent_decl_ctx)
2377 GetObjectFile()->GetModule()->LogMessage(
2378 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2379 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2380 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2381 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2382 else
2383 GetObjectFile()->GetModule()->LogMessage(
2384 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2385 "NULL, append=%u, max_matches=%u, type_list)",
2386 name.GetCString(), append, max_matches);
2387 }
2388
2389 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2390 return 0;
2391
2392 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002393 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002394 const size_t num_die_matches = die_offsets.size();
2395
2396 if (num_die_matches) {
2397 const uint32_t initial_types_size = types.GetSize();
2398 for (size_t i = 0; i < num_die_matches; ++i) {
2399 const DIERef &die_ref = die_offsets[i];
2400 DWARFDIE die = GetDIE(die_ref);
2401
2402 if (die) {
2403 if (!DIEInDeclContext(parent_decl_ctx, die))
2404 continue; // The containing decl contexts don't match
2405
2406 Type *matching_type = ResolveType(die, true, true);
2407 if (matching_type) {
2408 // We found a type pointer, now find the shared pointer form our type
2409 // list
2410 types.InsertUnique(matching_type->shared_from_this());
2411 if (types.GetSize() >= max_matches)
2412 break;
2413 }
2414 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002415 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002416 }
2417 }
2418 const uint32_t num_matches = types.GetSize() - initial_types_size;
2419 if (log && num_matches) {
2420 if (parent_decl_ctx) {
2421 GetObjectFile()->GetModule()->LogMessage(
2422 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2423 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2424 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2425 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2426 num_matches);
2427 } else {
2428 GetObjectFile()->GetModule()->LogMessage(
2429 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2430 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2431 name.GetCString(), append, max_matches, num_matches);
2432 }
2433 }
2434 return num_matches;
2435 } else {
2436 UpdateExternalModuleListIfNeeded();
2437
2438 for (const auto &pair : m_external_type_modules) {
2439 ModuleSP external_module_sp = pair.second;
2440 if (external_module_sp) {
2441 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2442 if (sym_vendor) {
2443 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002444 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2445 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446 if (num_external_matches)
2447 return num_external_matches;
2448 }
2449 }
2450 }
2451 }
2452
2453 return 0;
2454}
2455
2456size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2457 bool append, TypeMap &types) {
2458 if (!append)
2459 types.Clear();
2460
2461 if (context.empty())
2462 return 0;
2463
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 ConstString name = context.back().name;
2465
2466 if (!name)
2467 return 0;
2468
Pavel Labathb13f0332018-05-21 14:12:52 +00002469 DIEArray die_offsets;
2470 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471 const size_t num_die_matches = die_offsets.size();
2472
2473 if (num_die_matches) {
2474 size_t num_matches = 0;
2475 for (size_t i = 0; i < num_die_matches; ++i) {
2476 const DIERef &die_ref = die_offsets[i];
2477 DWARFDIE die = GetDIE(die_ref);
2478
2479 if (die) {
2480 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002481 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002482 if (die_context != context)
2483 continue;
2484
2485 Type *matching_type = ResolveType(die, true, true);
2486 if (matching_type) {
2487 // We found a type pointer, now find the shared pointer form our type
2488 // list
2489 types.InsertUnique(matching_type->shared_from_this());
2490 ++num_matches;
2491 }
2492 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002493 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002494 }
2495 }
2496 return num_matches;
2497 }
2498 return 0;
2499}
2500
2501CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002502SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002503 const CompilerDeclContext *parent_decl_ctx) {
2504 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2505
2506 if (log) {
2507 GetObjectFile()->GetModule()->LogMessage(
2508 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2509 name.GetCString());
2510 }
2511
2512 CompilerDeclContext namespace_decl_ctx;
2513
2514 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2515 return namespace_decl_ctx;
2516
2517 DWARFDebugInfo *info = DebugInfo();
2518 if (info) {
2519 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002520 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002521 const size_t num_matches = die_offsets.size();
2522 if (num_matches) {
2523 for (size_t i = 0; i < num_matches; ++i) {
2524 const DIERef &die_ref = die_offsets[i];
2525 DWARFDIE die = GetDIE(die_ref);
2526
2527 if (die) {
2528 if (!DIEInDeclContext(parent_decl_ctx, die))
2529 continue; // The containing decl contexts don't match
2530
2531 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2532 if (dwarf_ast) {
2533 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2534 if (namespace_decl_ctx)
2535 break;
2536 }
2537 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002538 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539 }
2540 }
2541 }
2542 }
2543 if (log && namespace_decl_ctx) {
2544 GetObjectFile()->GetModule()->LogMessage(
2545 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2546 "CompilerDeclContext(%p/%p) \"%s\"",
2547 name.GetCString(),
2548 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2549 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2550 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2551 }
2552
2553 return namespace_decl_ctx;
2554}
2555
2556TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2557 bool resolve_function_context) {
2558 TypeSP type_sp;
2559 if (die) {
2560 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002561 if (type_ptr == nullptr) {
Pavel Labathad17e282019-06-13 11:22:47 +00002562 SymbolContextScope *scope;
2563 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()))
2564 scope = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2565 else
2566 scope = GetObjectFile()->GetModule().get();
2567 assert(scope);
2568 SymbolContext sc(scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2570 while (parent_die != nullptr) {
2571 if (parent_die->Tag() == DW_TAG_subprogram)
2572 break;
2573 parent_die = parent_die->GetParent();
2574 }
2575 SymbolContext sc_backup = sc;
2576 if (resolve_function_context && parent_die != nullptr &&
2577 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2578 sc = sc_backup;
2579
Konrad Kleine248a1302019-05-23 11:14:47 +00002580 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002581 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2582 // Grab the existing type from the master types lists
2583 type_sp = type_ptr->shared_from_this();
2584 }
2585 }
2586 return type_sp;
2587}
2588
2589DWARFDIE
2590SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2591 if (orig_die) {
2592 DWARFDIE die = orig_die;
2593
2594 while (die) {
2595 // If this is the original DIE that we are searching for a declaration
2596 // for, then don't look in the cache as we don't want our own decl
2597 // context to be our decl context...
2598 if (orig_die != die) {
2599 switch (die.Tag()) {
2600 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002601 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002602 case DW_TAG_namespace:
2603 case DW_TAG_structure_type:
2604 case DW_TAG_union_type:
2605 case DW_TAG_class_type:
2606 case DW_TAG_lexical_block:
2607 case DW_TAG_subprogram:
2608 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002609 case DW_TAG_inlined_subroutine: {
2610 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2611 if (abs_die) {
2612 return abs_die;
2613 }
2614 break;
2615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 default:
2617 break;
2618 }
2619 }
2620
2621 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2622 if (spec_die) {
2623 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2624 if (decl_ctx_die)
2625 return decl_ctx_die;
2626 }
2627
2628 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2629 if (abs_die) {
2630 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2631 if (decl_ctx_die)
2632 return decl_ctx_die;
2633 }
2634
2635 die = die.GetParent();
2636 }
2637 }
2638 return DWARFDIE();
2639}
2640
2641Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002642SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002643 Symbol *objc_class_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002644 if (m_obj_file) {
2645 Symtab *symtab = m_obj_file->GetSymtab();
2646 if (symtab) {
2647 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2648 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2649 Symtab::eVisibilityAny);
2650 }
2651 }
2652 return objc_class_symbol;
2653}
2654
2655// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002656// they don't then we can end up looking through all class types for a complete
2657// type and never find the full definition. We need to know if this attribute
2658// is supported, so we determine this here and cache th result. We also need to
2659// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002660// DWARF file
2661// if we are doing darwin DWARF in .o file debugging.
2662bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002663 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002664 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2665 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2666 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2667 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2668 else {
2669 DWARFDebugInfo *debug_info = DebugInfo();
2670 const uint32_t num_compile_units = GetNumCompileUnits();
2671 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002672 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002673 if (dwarf_cu != cu &&
2674 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2675 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2676 break;
2677 }
2678 }
2679 }
2680 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2681 GetDebugMapSymfile())
2682 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2683 }
2684 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002685}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002686
2687// This function can be used when a DIE is found that is a forward declaration
2688// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002689TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002690 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002692
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 TypeSP type_sp;
2694
2695 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2696 return type_sp;
2697
2698 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002699 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700
2701 const size_t num_matches = die_offsets.size();
2702
2703 if (num_matches) {
2704 for (size_t i = 0; i < num_matches; ++i) {
2705 const DIERef &die_ref = die_offsets[i];
2706 DWARFDIE type_die = GetDIE(die_ref);
2707
2708 if (type_die) {
2709 bool try_resolving_type = false;
2710
Adrian Prantl05097242018-04-30 16:49:04 +00002711 // Don't try and resolve the DIE we are looking for with the DIE
2712 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002713 if (type_die != die) {
2714 switch (type_die.Tag()) {
2715 case DW_TAG_class_type:
2716 case DW_TAG_structure_type:
2717 try_resolving_type = true;
2718 break;
2719 default:
2720 break;
2721 }
2722 }
2723
2724 if (try_resolving_type) {
2725 if (must_be_implementation &&
2726 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2727 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2728 DW_AT_APPLE_objc_complete_type, 0);
2729
2730 if (try_resolving_type) {
2731 Type *resolved_type = ResolveType(type_die, false, true);
2732 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2733 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2734 " (cu 0x%8.8" PRIx64 ")\n",
2735 die.GetID(),
2736 m_obj_file->GetFileSpec().GetFilename().AsCString(
2737 "<Unknown>"),
2738 type_die.GetID(), type_cu->GetID());
2739
2740 if (die)
2741 GetDIEToType()[die.GetDIE()] = resolved_type;
2742 type_sp = resolved_type->shared_from_this();
2743 break;
2744 }
2745 }
2746 }
2747 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002748 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002749 }
2750 }
2751 }
2752 return type_sp;
2753}
Greg Claytona8022fa2012-04-24 21:22:41 +00002754
Adrian Prantl05097242018-04-30 16:49:04 +00002755// This function helps to ensure that the declaration contexts match for two
2756// different DIEs. Often times debug information will refer to a forward
2757// declaration of a type (the equivalent of "struct my_struct;". There will
2758// often be a declaration of that type elsewhere that has the full definition.
2759// When we go looking for the full type "my_struct", we will find one or more
2760// matches in the accelerator tables and we will then need to make sure the
2761// type was in the same declaration context as the original DIE. This function
2762// can efficiently compare two DIEs and will return true when the declaration
2763// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002764bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2765 const DWARFDIE &die2) {
2766 if (die1 == die2)
2767 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002768
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002769 std::vector<DWARFDIE> decl_ctx_1;
2770 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002771 // The declaration DIE stack is a stack of the declaration context DIEs all
2772 // the way back to the compile unit. If a type "T" is declared inside a class
2773 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2774 // namespace "lldb", and the namespace is in a compile unit, there will be a
2775 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002776 //
2777 // [0] DW_TAG_class_type for "B"
2778 // [1] DW_TAG_class_type for "A"
2779 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002780 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002781 //
Adrian Prantl05097242018-04-30 16:49:04 +00002782 // We grab both contexts and make sure that everything matches all the way
2783 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002784
2785 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002786 decl_ctx_1 = die1.GetDeclContextDIEs();
2787 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002788 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002789 const size_t count1 = decl_ctx_1.size();
2790 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791 if (count1 != count2)
2792 return false;
2793
Adrian Prantl05097242018-04-30 16:49:04 +00002794 // Make sure the DW_TAG values match all the way back up the compile unit. If
2795 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 DWARFDIE decl_ctx_die1;
2797 DWARFDIE decl_ctx_die2;
2798 size_t i;
2799 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002800 decl_ctx_die1 = decl_ctx_1[i];
2801 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002802 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2803 return false;
2804 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002805#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002806
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002808 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2809 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2810 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002811 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002812 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2813 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002814
Greg Clayton890ff562012-02-02 05:48:16 +00002815#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002816 // Always skip the compile unit when comparing by only iterating up to "count
2817 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002818 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002819 decl_ctx_die1 = decl_ctx_1[i];
2820 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 const char *name1 = decl_ctx_die1.GetName();
2822 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002823 // If the string was from a DW_FORM_strp, then the pointer will often be
2824 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825 if (name1 == name2)
2826 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002827
Adrian Prantl05097242018-04-30 16:49:04 +00002828 // Name pointers are not equal, so only compare the strings if both are not
2829 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002830 if (name1 && name2) {
2831 // If the strings don't compare, we are done...
2832 if (strcmp(name1, name2) != 0)
2833 return false;
2834 } else {
2835 // One name was NULL while the other wasn't
2836 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002837 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 }
Adrian Prantl05097242018-04-30 16:49:04 +00002839 // We made it through all of the checks and the declaration contexts are
2840 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002842}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002843
Kate Stoneb9c1b512016-09-06 20:57:50 +00002844TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2845 const DWARFDeclContext &dwarf_decl_ctx) {
2846 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002847
Kate Stoneb9c1b512016-09-06 20:57:50 +00002848 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2849 if (dwarf_decl_ctx_count > 0) {
2850 const ConstString type_name(dwarf_decl_ctx[0].name);
2851 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002852
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 if (type_name) {
2854 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2855 DWARF_LOG_LOOKUPS));
2856 if (log) {
2857 GetObjectFile()->GetModule()->LogMessage(
2858 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2859 "s, qualified-name='%s')",
2860 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2861 dwarf_decl_ctx.GetQualifiedName());
2862 }
2863
2864 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002865 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002866 const size_t num_matches = die_offsets.size();
2867
Adrian Prantl05097242018-04-30 16:49:04 +00002868 // Get the type system that we are looking to find a type for. We will
2869 // use this to ensure any matches we find are in a language that this
2870 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002871 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2872 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2873 ? nullptr
2874 : GetTypeSystemForLanguage(language);
2875
2876 if (num_matches) {
2877 for (size_t i = 0; i < num_matches; ++i) {
2878 const DIERef &die_ref = die_offsets[i];
2879 DWARFDIE type_die = GetDIE(die_ref);
2880
2881 if (type_die) {
2882 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002883 // looking for. We don't want to find a "Foo" type from Java if we
2884 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 if (type_system &&
2886 !type_system->SupportsLanguage(type_die.GetLanguage()))
2887 continue;
2888 bool try_resolving_type = false;
2889
2890 // Don't try and resolve the DIE we are looking for with the DIE
2891 // itself!
2892 const dw_tag_t type_tag = type_die.Tag();
2893 // Make sure the tags match
2894 if (type_tag == tag) {
2895 // The tags match, lets try resolving this type
2896 try_resolving_type = true;
2897 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002898 // The tags don't match, but we need to watch our for a forward
2899 // declaration for a struct and ("struct foo") ends up being a
2900 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002901 switch (type_tag) {
2902 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002903 // We had a "class foo", see if we ended up with a "struct foo
2904 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002905 try_resolving_type = (tag == DW_TAG_structure_type);
2906 break;
2907 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002908 // We had a "struct foo", see if we ended up with a "class foo
2909 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002910 try_resolving_type = (tag == DW_TAG_class_type);
2911 break;
2912 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002913 // Tags don't match, don't event try to resolve using this type
2914 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002915 break;
2916 }
2917 }
2918
2919 if (try_resolving_type) {
2920 DWARFDeclContext type_dwarf_decl_ctx;
2921 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2922
2923 if (log) {
2924 GetObjectFile()->GetModule()->LogMessage(
2925 log, "SymbolFileDWARF::"
2926 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2927 "qualified-name='%s') trying die=0x%8.8x (%s)",
2928 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2929 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2930 type_dwarf_decl_ctx.GetQualifiedName());
2931 }
2932
2933 // Make sure the decl contexts match all the way up
2934 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2935 Type *resolved_type = ResolveType(type_die, false);
2936 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2937 type_sp = resolved_type->shared_from_this();
2938 break;
2939 }
2940 }
2941 } else {
2942 if (log) {
2943 std::string qualified_name;
2944 type_die.GetQualifiedName(qualified_name);
2945 GetObjectFile()->GetModule()->LogMessage(
2946 log, "SymbolFileDWARF::"
2947 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2948 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2949 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2950 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2951 qualified_name.c_str());
2952 }
2953 }
2954 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002955 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002956 }
2957 }
2958 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002960 }
2961 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002962}
2963
Kate Stoneb9c1b512016-09-06 20:57:50 +00002964TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2965 bool *type_is_new_ptr) {
2966 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002967
Kate Stoneb9c1b512016-09-06 20:57:50 +00002968 if (die) {
2969 TypeSystem *type_system =
2970 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00002971
Kate Stoneb9c1b512016-09-06 20:57:50 +00002972 if (type_system) {
2973 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2974 if (dwarf_ast) {
2975 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
2976 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
2977 if (type_sp) {
2978 TypeList *type_list = GetTypeList();
2979 if (type_list)
2980 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00002981
Kate Stoneb9c1b512016-09-06 20:57:50 +00002982 if (die.Tag() == DW_TAG_subprogram) {
2983 DIERef die_ref = die.GetDIERef();
2984 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2985 .GetScopeQualifiedName()
2986 .AsCString(""));
2987 if (scope_qualified_name.size()) {
2988 NameToOffsetMap::iterator iter =
2989 m_function_scope_qualified_name_map.find(
2990 scope_qualified_name);
2991 if (iter != m_function_scope_qualified_name_map.end())
2992 (*iter).second->insert(die_ref);
2993 else {
2994 DIERefSetSP new_set(new std::set<DIERef>);
2995 new_set->insert(die_ref);
2996 m_function_scope_qualified_name_map.emplace(
2997 std::make_pair(scope_qualified_name, new_set));
2998 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002999 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003000 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003001 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003004 }
3005
3006 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003007}
3008
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3010 const DWARFDIE &orig_die,
3011 bool parse_siblings, bool parse_children) {
3012 size_t types_added = 0;
3013 DWARFDIE die = orig_die;
3014 while (die) {
3015 bool type_is_new = false;
3016 if (ParseType(sc, die, &type_is_new).get()) {
3017 if (type_is_new)
3018 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003019 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003020
3021 if (parse_children && die.HasChildren()) {
3022 if (die.Tag() == DW_TAG_subprogram) {
3023 SymbolContext child_sc(sc);
3024 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3025 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3026 } else
3027 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3028 }
3029
3030 if (parse_siblings)
3031 die = die.GetSibling();
3032 else
3033 die.Clear();
3034 }
3035 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003036}
3037
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003038size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003039 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003040 CompileUnit *comp_unit = func.GetCompileUnit();
3041 lldbassert(comp_unit);
3042
3043 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3044 if (!dwarf_cu)
3045 return 0;
3046
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003048 const dw_offset_t function_die_offset = func.GetID();
3049 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3050 if (function_die) {
3051 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3052 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003054
Kate Stoneb9c1b512016-09-06 20:57:50 +00003055 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003056}
3057
Zachary Turner863f8c12019-01-11 18:03:20 +00003058size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003059 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003060 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003061 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003062 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003063 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3064 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3065 SymbolContext sc;
3066 sc.comp_unit = &comp_unit;
3067 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003069 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003072}
3073
Kate Stoneb9c1b512016-09-06 20:57:50 +00003074size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003075 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +00003076 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003078 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003079 return 0;
3080
3081 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003082 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003083
3084 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3085 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3086 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3087 const size_t num_variables = ParseVariables(
3088 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3089
3090 // Let all blocks know they have parse all their variables
3091 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3092 return num_variables;
3093 }
3094 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003095 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003096
Konrad Kleine248a1302019-05-23 11:14:47 +00003097 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003098 return 0;
3099
3100 uint32_t vars_added = 0;
3101 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3102
Konrad Kleine248a1302019-05-23 11:14:47 +00003103 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003104 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003105 sc.comp_unit->SetVariableList(variables);
3106
3107 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003108 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003109 const size_t num_matches = die_offsets.size();
3110 if (num_matches) {
3111 for (size_t i = 0; i < num_matches; ++i) {
3112 const DIERef &die_ref = die_offsets[i];
3113 DWARFDIE die = GetDIE(die_ref);
3114 if (die) {
3115 VariableSP var_sp(
3116 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3117 if (var_sp) {
3118 variables->AddVariableIfUnique(var_sp);
3119 ++vars_added;
3120 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003121 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00003122 m_index->ReportInvalidDIERef(die_ref, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003124 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125 }
3126 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003128 }
3129 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003130}
3131
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3133 const DWARFDIE &die,
3134 const lldb::addr_t func_low_pc) {
3135 if (die.GetDWARF() != this)
3136 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003137
Kate Stoneb9c1b512016-09-06 20:57:50 +00003138 VariableSP var_sp;
3139 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003140 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003141
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142 var_sp = GetDIEToVariable()[die.GetDIE()];
3143 if (var_sp)
3144 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003145
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 const dw_tag_t tag = die.Tag();
3147 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003148
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3150 (tag == DW_TAG_formal_parameter && sc.function)) {
3151 DWARFAttributes attributes;
3152 const size_t num_attributes = die.GetAttributes(attributes);
3153 DWARFDIE spec_die;
3154 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003155 const char *name = nullptr;
3156 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 Declaration decl;
3158 uint32_t i;
3159 DWARFFormValue type_die_form;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003160 DWARFExpression location;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003161 bool is_external = false;
3162 bool is_artificial = false;
3163 bool location_is_const_value_data = false;
3164 bool has_explicit_location = false;
3165 DWARFFormValue const_value;
3166 Variable::RangeList scope_ranges;
3167 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003168
Kate Stoneb9c1b512016-09-06 20:57:50 +00003169 for (i = 0; i < num_attributes; ++i) {
3170 dw_attr_t attr = attributes.AttributeAtIndex(i);
3171 DWARFFormValue form_value;
3172
3173 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3174 switch (attr) {
3175 case DW_AT_decl_file:
3176 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3177 form_value.Unsigned()));
3178 break;
3179 case DW_AT_decl_line:
3180 decl.SetLine(form_value.Unsigned());
3181 break;
3182 case DW_AT_decl_column:
3183 decl.SetColumn(form_value.Unsigned());
3184 break;
3185 case DW_AT_name:
3186 name = form_value.AsCString();
3187 break;
3188 case DW_AT_linkage_name:
3189 case DW_AT_MIPS_linkage_name:
3190 mangled = form_value.AsCString();
3191 break;
3192 case DW_AT_type:
3193 type_die_form = form_value;
3194 break;
3195 case DW_AT_external:
3196 is_external = form_value.Boolean();
3197 break;
3198 case DW_AT_const_value:
3199 // If we have already found a DW_AT_location attribute, ignore this
3200 // attribute.
3201 if (!has_explicit_location) {
3202 location_is_const_value_data = true;
3203 // The constant value will be either a block, a data value or a
3204 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003205 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3207 // Retrieve the value as a block expression.
3208 uint32_t block_offset =
3209 form_value.BlockData() - debug_info_data.GetDataStart();
3210 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003211 location = DWARFExpression(module, debug_info_data, die.GetCU(),
3212 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003213 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3214 // Retrieve the value as a data expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003216 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003217 location =
3218 DWARFExpression(module, debug_info_data, die.GetCU(),
3219 data_offset, *data_length);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003220 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003221 const uint8_t *data_pointer = form_value.BlockData();
3222 if (data_pointer) {
3223 form_value.Unsigned();
3224 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3225 // we need to get the byte size of the type later after we
3226 // create the variable
3227 const_value = form_value;
3228 }
Greg Clayton6aad81c2019-05-24 22:08:50 +00003229 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003230 } else {
3231 // Retrieve the value as a string expression.
3232 if (form_value.Form() == DW_FORM_strp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003233 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003234 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003235 location =
3236 DWARFExpression(module, debug_info_data, die.GetCU(),
3237 data_offset, *data_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 } else {
3239 const char *str = form_value.AsCString();
3240 uint32_t string_offset =
3241 str - (const char *)debug_info_data.GetDataStart();
3242 uint32_t string_length = strlen(str) + 1;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003243 location =
3244 DWARFExpression(module, debug_info_data, die.GetCU(),
3245 string_offset, string_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003246 }
3247 }
3248 }
3249 break;
3250 case DW_AT_location: {
3251 location_is_const_value_data = false;
3252 has_explicit_location = true;
3253 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003254 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003255
3256 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003257 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003258 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003259 location = DWARFExpression(module, data, die.GetCU(),
3260 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003261 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003262 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003263 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3264
3265 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003266 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003267 if (loc_list_length > 0) {
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003268 location = DWARFExpression(module, debug_loc_data, die.GetCU(),
3269 debug_loc_offset, loc_list_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003270 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3271 location.SetLocationListSlide(
3272 func_low_pc -
3273 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3274 }
3275 }
3276 } break;
3277 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003278 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003279 break;
Pavel Labath78cfe1e2019-05-29 09:22:36 +00003280 case DW_AT_start_scope:
3281 // TODO: Implement this.
3282 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003283 case DW_AT_artificial:
3284 is_artificial = form_value.Boolean();
3285 break;
3286 case DW_AT_accessibility:
3287 break; // accessibility =
3288 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3289 case DW_AT_declaration:
3290 case DW_AT_description:
3291 case DW_AT_endianity:
3292 case DW_AT_segment:
3293 case DW_AT_visibility:
3294 default:
3295 case DW_AT_abstract_origin:
3296 case DW_AT_sibling:
3297 break;
3298 }
3299 }
3300 }
3301
3302 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3303 const dw_tag_t parent_tag = die.GetParent().Tag();
3304 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003305 (parent_tag == DW_TAG_compile_unit ||
3306 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003307 (parent_context_die.Tag() == DW_TAG_class_type ||
3308 parent_context_die.Tag() == DW_TAG_structure_type);
3309
3310 ValueType scope = eValueTypeInvalid;
3311
3312 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003313 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003314
Sam McCall30621392016-11-22 11:40:25 +00003315 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 if (!mangled) {
3317 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003318 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3319 // of global variables with commands like "frame var j". For
3320 // example, if j were an int variable holding a value 4 and
3321 // declared in a namespace B which in turn is contained in a
3322 // namespace A, the command "frame var j" returns
3323 // "(int) A::B::j = 4".
3324 // If the compiler does not emit a linkage name, we should be
3325 // able to generate a fully qualified name from the
3326 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003327 if ((parent_tag == DW_TAG_compile_unit ||
3328 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003329 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3330 DWARFDeclContext decl_ctx;
3331
3332 die.GetDWARFDeclContext(decl_ctx);
3333 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3334 }
3335 }
3336
Kate Stoneb9c1b512016-09-06 20:57:50 +00003337 if (tag == DW_TAG_formal_parameter)
3338 scope = eValueTypeVariableArgument;
3339 else {
Sam McCall30621392016-11-22 11:40:25 +00003340 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3341 // or static variable, so we have to do a little digging:
3342 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3343 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3344 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3345 // Clang likes to combine small global variables into the same symbol
3346 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3347 // so we need to look through the whole expression.
3348 bool is_static_lifetime =
3349 has_explicit_mangled ||
3350 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003351 // Check if the location has a DW_OP_addr with any address value...
3352 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3353 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003354 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003355 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3356 if (op_error) {
3357 StreamString strm;
3358 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003359 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360 GetObjectFile()->GetModule()->ReportError(
3361 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003362 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003363 }
Sam McCall30621392016-11-22 11:40:25 +00003364 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3365 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003366 }
3367 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003368 if (debug_map_symfile)
3369 // Set the module of the expression to the linked module
3370 // instead of the oject file so the relocated address can be
3371 // found there.
3372 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373
Sam McCall30621392016-11-22 11:40:25 +00003374 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003375 if (is_external)
3376 scope = eValueTypeVariableGlobal;
3377 else
3378 scope = eValueTypeVariableStatic;
3379
3380 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003381 // When leaving the DWARF in the .o files on darwin, when we have a
3382 // global variable that wasn't initialized, the .o file might not
3383 // have allocated a virtual address for the global variable. In
3384 // this case it will have created a symbol for the global variable
3385 // that is undefined/data and external and the value will be the
3386 // byte size of the variable. When we do the address map in
3387 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3388 // do some magic here so we can get the correct address for our
3389 // global variable. The address for all of these entries will be
3390 // zero, and there will be an undefined symbol in this object file,
3391 // and the executable will have a matching symbol with a good
3392 // address. So here we dig up the correct address and replace it in
3393 // the location for the variable, and set the variable's symbol
3394 // context scope to be that of the main executable so the file
3395 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003396 bool linked_oso_file_addr = false;
3397 if (is_external && location_DW_OP_addr == 0) {
3398 // we have a possible uninitialized extern global
3399 ConstString const_name(mangled ? mangled : name);
3400 ObjectFile *debug_map_objfile =
3401 debug_map_symfile->GetObjectFile();
3402 if (debug_map_objfile) {
3403 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3404 if (debug_map_symtab) {
3405 Symbol *exe_symbol =
3406 debug_map_symtab->FindFirstSymbolWithNameAndType(
3407 const_name, eSymbolTypeData, Symtab::eDebugYes,
3408 Symtab::eVisibilityExtern);
3409 if (exe_symbol) {
3410 if (exe_symbol->ValueIsAddress()) {
3411 const addr_t exe_file_addr =
3412 exe_symbol->GetAddressRef().GetFileAddress();
3413 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3414 if (location.Update_DW_OP_addr(exe_file_addr)) {
3415 linked_oso_file_addr = true;
3416 symbol_context_scope = exe_symbol;
3417 }
3418 }
3419 }
3420 }
3421 }
3422 }
3423 }
3424
3425 if (!linked_oso_file_addr) {
3426 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003427 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 const lldb::addr_t exe_file_addr =
3429 debug_map_symfile->LinkOSOFileAddress(this,
3430 location_DW_OP_addr);
3431 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3432 // Update the file address for this variable
3433 location.Update_DW_OP_addr(exe_file_addr);
3434 } else {
3435 // Variable didn't make it into the final executable
3436 return var_sp;
3437 }
3438 }
3439 }
3440 } else {
3441 if (location_is_const_value_data)
3442 scope = eValueTypeVariableStatic;
3443 else {
3444 scope = eValueTypeVariableLocal;
3445 if (debug_map_symfile) {
3446 // We need to check for TLS addresses that we need to fixup
3447 if (location.ContainsThreadLocalStorage()) {
3448 location.LinkThreadLocalStorage(
3449 debug_map_symfile->GetObjectFile()->GetModule(),
3450 [this, debug_map_symfile](
3451 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3452 return debug_map_symfile->LinkOSOFileAddress(
3453 this, unlinked_file_addr);
3454 });
3455 scope = eValueTypeVariableThreadLocal;
3456 }
3457 }
3458 }
3459 }
3460 }
3461
Konrad Kleine248a1302019-05-23 11:14:47 +00003462 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003463 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003464 case DW_TAG_subprogram:
3465 case DW_TAG_inlined_subroutine:
3466 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003467 if (sc.function) {
3468 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3469 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003470 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003471 symbol_context_scope = sc.function;
3472 }
3473 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003474
Kate Stoneb9c1b512016-09-06 20:57:50 +00003475 default:
3476 symbol_context_scope = sc.comp_unit;
3477 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003479 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003480
Kate Stoneb9c1b512016-09-06 20:57:50 +00003481 if (symbol_context_scope) {
3482 SymbolFileTypeSP type_sp(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003483 new SymbolFileType(*this, GetUID(DIERef(type_die_form))));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003484
3485 if (const_value.Form() && type_sp && type_sp->GetType())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003486 location.UpdateValue(const_value.Unsigned(),
3487 type_sp->GetType()->GetByteSize().getValueOr(0),
3488 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003489
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003490 var_sp = std::make_shared<Variable>(
3491 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3492 scope_ranges, &decl, location, is_external, is_artificial,
3493 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003494
3495 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3496 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003497 // Not ready to parse this variable yet. It might be a global or static
3498 // variable that is in a function scope and the function in the symbol
3499 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003500 return var_sp;
3501 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003502 }
Adrian Prantl05097242018-04-30 16:49:04 +00003503 // Cache var_sp even if NULL (the variable was just a specification or was
3504 // missing vital information to be able to be displayed in the debugger
3505 // (missing location due to optimization, etc)) so we don't re-parse this
3506 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003507 GetDIEToVariable()[die.GetDIE()] = var_sp;
3508 if (spec_die)
3509 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3510 }
3511 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003512}
3513
Kate Stoneb9c1b512016-09-06 20:57:50 +00003514DWARFDIE
3515SymbolFileDWARF::FindBlockContainingSpecification(
3516 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3517 // Give the concrete function die specified by "func_die_offset", find the
3518 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3519 // to "spec_block_die_offset"
3520 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3521 spec_block_die_offset);
3522}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003523
Kate Stoneb9c1b512016-09-06 20:57:50 +00003524DWARFDIE
3525SymbolFileDWARF::FindBlockContainingSpecification(
3526 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3527 if (die) {
3528 switch (die.Tag()) {
3529 case DW_TAG_subprogram:
3530 case DW_TAG_inlined_subroutine:
3531 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003532 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3533 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003534 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003535
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003536 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003537 spec_block_die_offset)
3538 return die;
3539 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003541
3542 // Give the concrete function die specified by "func_die_offset", find the
3543 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3544 // to "spec_block_die_offset"
3545 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3546 child_die = child_die.GetSibling()) {
3547 DWARFDIE result_die =
3548 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3549 if (result_die)
3550 return result_die;
3551 }
3552 }
3553
3554 return DWARFDIE();
3555}
3556
3557size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3558 const DWARFDIE &orig_die,
3559 const lldb::addr_t func_low_pc,
3560 bool parse_siblings, bool parse_children,
3561 VariableList *cc_variable_list) {
3562 if (!orig_die)
3563 return 0;
3564
3565 VariableListSP variable_list_sp;
3566
3567 size_t vars_added = 0;
3568 DWARFDIE die = orig_die;
3569 while (die) {
3570 dw_tag_t tag = die.Tag();
3571
3572 // Check to see if we have already parsed this variable or constant?
3573 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3574 if (var_sp) {
3575 if (cc_variable_list)
3576 cc_variable_list->AddVariableIfUnique(var_sp);
3577 } else {
3578 // We haven't already parsed it, lets do that now.
3579 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3580 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003581 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003582 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3583 dw_tag_t parent_tag = sc_parent_die.Tag();
3584 switch (parent_tag) {
3585 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003586 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003587 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003588 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003589 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003590 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591 }
3592 } else {
3593 GetObjectFile()->GetModule()->ReportError(
3594 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3595 "symbol context for 0x%8.8" PRIx64
3596 " %s.\n",
3597 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3598 orig_die.GetID(), orig_die.GetTagAsCString());
3599 }
3600 break;
3601
3602 case DW_TAG_subprogram:
3603 case DW_TAG_inlined_subroutine:
3604 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003605 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003606 // Check to see if we already have parsed the variables for the
3607 // given scope
3608
3609 Block *block = sc.function->GetBlock(true).FindBlockByID(
3610 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003611 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003612 // This must be a specification or abstract origin with a
3613 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003614 // to find the concrete block so we can correctly add the
3615 // variable to it
3616 const DWARFDIE concrete_block_die =
3617 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003618 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619 sc_parent_die.GetOffset());
3620 if (concrete_block_die)
3621 block = sc.function->GetBlock(true).FindBlockByID(
3622 concrete_block_die.GetID());
3623 }
3624
Konrad Kleine248a1302019-05-23 11:14:47 +00003625 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626 const bool can_create = false;
3627 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003628 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003629 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003630 block->SetVariableList(variable_list_sp);
3631 }
3632 }
3633 }
3634 break;
3635
3636 default:
3637 GetObjectFile()->GetModule()->ReportError(
3638 "didn't find appropriate parent DIE for variable list for "
3639 "0x%8.8" PRIx64 " %s.\n",
3640 orig_die.GetID(), orig_die.GetTagAsCString());
3641 break;
3642 }
3643 }
3644
3645 if (variable_list_sp) {
3646 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3647 if (var_sp) {
3648 variable_list_sp->AddVariableIfUnique(var_sp);
3649 if (cc_variable_list)
3650 cc_variable_list->AddVariableIfUnique(var_sp);
3651 ++vars_added;
3652 }
3653 }
3654 }
3655 }
3656
Konrad Kleine248a1302019-05-23 11:14:47 +00003657 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003658
3659 if (!skip_children && parse_children && die.HasChildren()) {
3660 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3661 true, cc_variable_list);
3662 }
3663
3664 if (parse_siblings)
3665 die = die.GetSibling();
3666 else
3667 die.Clear();
3668 }
3669 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003670}
3671
Vedant Kumar4b36f792018-10-05 23:23:15 +00003672/// Collect call graph edges present in a function DIE.
3673static std::vector<lldb_private::CallEdge>
3674CollectCallEdges(DWARFDIE function_die) {
3675 // Check if the function has a supported call site-related attribute.
3676 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3677 uint64_t has_call_edges =
3678 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3679 if (!has_call_edges)
3680 return {};
3681
3682 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3683 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3684 function_die.GetPubname());
3685
3686 // Scan the DIE for TAG_call_site entries.
3687 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3688 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3689 // For now, assume that all entries are nested directly under the subprogram
3690 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3691 std::vector<CallEdge> call_edges;
3692 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3693 child = child.GetSibling()) {
3694 if (child.Tag() != DW_TAG_call_site)
3695 continue;
3696
3697 // Extract DW_AT_call_origin (the call target's DIE).
3698 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3699 if (!call_origin.IsValid()) {
3700 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3701 function_die.GetPubname());
3702 continue;
3703 }
3704
3705 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3706 // available. It should only ever be unavailable for tail call edges, in
3707 // which case use LLDB_INVALID_ADDRESS.
3708 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3709 LLDB_INVALID_ADDRESS);
3710
3711 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3712 call_origin.GetPubname(), return_pc);
3713 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3714 }
3715 return call_edges;
3716}
3717
3718std::vector<lldb_private::CallEdge>
3719SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003720 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003721 if (func_die.IsValid())
3722 return CollectCallEdges(func_die);
3723 return {};
3724}
3725
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003727ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3728
3729uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3730
Pavel Labath9337b412018-06-06 11:35:23 +00003731void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003732
Zachary Turner49110232018-11-05 17:40:28 +00003733void SymbolFileDWARF::DumpClangAST(Stream &s) {
3734 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3735 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3736 if (!clang)
3737 return;
3738 clang->Dump(s);
3739}
3740
Kate Stoneb9c1b512016-09-06 20:57:50 +00003741SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003742 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003743 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3744 if (module_sp) {
3745 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3746 if (sym_vendor)
3747 m_debug_map_symfile =
3748 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003749 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003750 }
3751 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003752}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003753
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003754DWARFExpression::LocationListFormat
3755SymbolFileDWARF::GetLocationListFormat() const {
3756 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003757 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003758 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003759}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003760
3761SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3762 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003763 ModuleSpec module_spec;
3764 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3765 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003766 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003767
3768 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3769 FileSpec dwp_filespec =
3770 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003771 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003772 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3773 dwp_filespec);
3774 }
3775 });
3776 return m_dwp_symfile.get();
3777}