blob: c1d5506f0577a2531c1c771702de586bfc584002 [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[] = {
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +0000117#define LLDB_PROPERTIES_symbolfiledwarf
118#include "Properties.inc"
119};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000120
Pavel Labath9337b412018-06-06 11:35:23 +0000121enum {
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +0000122#define LLDB_PROPERTIES_symbolfiledwarf
123#include "PropertiesEnum.inc"
Pavel Labath9337b412018-06-06 11:35:23 +0000124};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126class PluginProperties : public Properties {
127public:
128 static ConstString GetSettingName() {
129 return SymbolFileDWARF::GetPluginNameStatic();
130 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 PluginProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000133 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 m_collection_sp->Initialize(g_properties);
135 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000136
Frederic Rissacbf0052019-04-23 20:17:04 +0000137 FileSpecList GetSymLinkPaths() {
138 const OptionValueFileSpecList *option_value =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
140 nullptr, true, ePropertySymLinkPaths);
141 assert(option_value);
142 return option_value->GetCurrentValue();
143 }
Pavel Labath9337b412018-06-06 11:35:23 +0000144
145 bool IgnoreFileIndexes() const {
146 return m_collection_sp->GetPropertyAtIndexAsBoolean(
147 nullptr, ePropertyIgnoreIndexes, false);
148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
154 static const auto g_settings_sp(std::make_shared<PluginProperties>());
155 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000159
Frederic Rissacbf0052019-04-23 20:17:04 +0000160FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
Pavel Labath7d36d722019-01-16 12:30:41 +0000161 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164void SymbolFileDWARF::Initialize() {
165 LogChannelDWARF::Initialize();
166 PluginManager::RegisterPlugin(GetPluginNameStatic(),
167 GetPluginDescriptionStatic(), CreateInstance,
168 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000169}
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
172 if (!PluginManager::GetSettingForSymbolFilePlugin(
173 debugger, PluginProperties::GetSettingName())) {
174 const bool is_global_setting = true;
175 PluginManager::CreateSettingForSymbolFilePlugin(
176 debugger, GetGlobalPluginProperties()->GetValueProperties(),
177 ConstString("Properties for the dwarf symbol-file plug-in."),
178 is_global_setting);
179 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182void SymbolFileDWARF::Terminate() {
183 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000184 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
188 static ConstString g_name("dwarf");
189 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
193 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194}
195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
Pavel Labathff9b4262019-05-17 08:26:58 +0000197 return new SymbolFileDWARF(obj_file,
198 /*dwo_section_list*/ nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199}
200
Pavel Labathf46e8972019-07-25 08:22:05 +0000201TypeList &SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000202 // This method can be called without going through the symbol vendor so we
203 // need to lock the module.
204 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf46e8972019-07-25 08:22:05 +0000205 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 return debug_map_symfile->GetTypeList();
Pavel Labathf46e8972019-07-25 08:22:05 +0000207 return SymbolFile::GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000208}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
210 dw_offset_t max_die_offset, uint32_t type_mask,
211 TypeSet &type_set) {
212 if (die) {
213 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 if (die_offset >= max_die_offset)
216 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (die_offset >= min_die_offset) {
219 const dw_tag_t tag = die.Tag();
220
221 bool add_type = false;
222
223 switch (tag) {
224 case DW_TAG_array_type:
225 add_type = (type_mask & eTypeClassArray) != 0;
226 break;
227 case DW_TAG_unspecified_type:
228 case DW_TAG_base_type:
229 add_type = (type_mask & eTypeClassBuiltin) != 0;
230 break;
231 case DW_TAG_class_type:
232 add_type = (type_mask & eTypeClassClass) != 0;
233 break;
234 case DW_TAG_structure_type:
235 add_type = (type_mask & eTypeClassStruct) != 0;
236 break;
237 case DW_TAG_union_type:
238 add_type = (type_mask & eTypeClassUnion) != 0;
239 break;
240 case DW_TAG_enumeration_type:
241 add_type = (type_mask & eTypeClassEnumeration) != 0;
242 break;
243 case DW_TAG_subroutine_type:
244 case DW_TAG_subprogram:
245 case DW_TAG_inlined_subroutine:
246 add_type = (type_mask & eTypeClassFunction) != 0;
247 break;
248 case DW_TAG_pointer_type:
249 add_type = (type_mask & eTypeClassPointer) != 0;
250 break;
251 case DW_TAG_rvalue_reference_type:
252 case DW_TAG_reference_type:
253 add_type = (type_mask & eTypeClassReference) != 0;
254 break;
255 case DW_TAG_typedef:
256 add_type = (type_mask & eTypeClassTypedef) != 0;
257 break;
258 case DW_TAG_ptr_to_member_type:
259 add_type = (type_mask & eTypeClassMemberPointer) != 0;
260 break;
261 }
262
263 if (add_type) {
264 const bool assert_not_being_parsed = true;
265 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
266 if (type) {
267 if (type_set.find(type) == type_set.end())
268 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000269 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272
273 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
274 child_die = child_die.GetSibling()) {
275 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
276 }
277 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000278}
279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000281 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000282
283{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000284 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000286
Konrad Kleine248a1302019-05-23 11:14:47 +0000287 CompileUnit *comp_unit = nullptr;
288 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (sc_scope)
290 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 if (comp_unit) {
293 dwarf_cu = GetDWARFCompileUnit(comp_unit);
Konrad Kleine248a1302019-05-23 11:14:47 +0000294 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 return 0;
296 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000297 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 } else {
299 DWARFDebugInfo *info = DebugInfo();
300 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000301 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000303 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 if (dwarf_cu) {
305 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000306 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 }
310
311 std::set<CompilerType> compiler_type_set;
312 size_t num_types_added = 0;
313 for (Type *type : type_set) {
314 CompilerType compiler_type = type->GetForwardCompilerType();
315 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
316 compiler_type_set.insert(compiler_type);
317 type_list.Insert(type->shared_from_this());
318 ++num_types_added;
319 }
320 }
321 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000322}
323
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324// Gets the first parent that is a lexical block, function or inlined
325// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000326DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
328 DWARFDIE die;
329 for (die = child_die.GetParent(); die; die = die.GetParent()) {
330 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 switch (tag) {
333 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000334 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 case DW_TAG_subprogram:
336 case DW_TAG_inlined_subroutine:
337 case DW_TAG_lexical_block:
338 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 }
341 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342}
343
Pavel Labathff9b4262019-05-17 08:26:58 +0000344SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile,
345 SectionList *dwo_section_list)
Pavel Labathf4014e12019-05-16 11:07:58 +0000346 : SymbolFile(objfile),
347 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
348 // when this class parses .o files to
349 // contain the .o file index/ID
Konrad Kleine248a1302019-05-23 11:14:47 +0000350 m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
Pavel Labathff9b4262019-05-17 08:26:58 +0000351 m_context(objfile->GetModule()->GetSectionList(), dwo_section_list),
Pavel Labath00e39682019-06-14 14:12:25 +0000352 m_data_debug_loc(), m_abbr(), m_info(), m_fetched_external_modules(false),
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000353 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356SymbolFileDWARF::~SymbolFileDWARF() {}
357
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000358static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 static ConstString g_dwarf_section_name("__DWARF");
360 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361}
362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
364 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
365 if (debug_map_symfile)
366 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
367 else
368 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000369}
370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
372 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
373 TypeSystem *type_system;
374 if (debug_map_symfile) {
375 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
376 } else {
377 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
378 if (type_system)
379 type_system->SetSymbolFile(this);
380 }
381 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000382}
383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000385 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386
Pavel Labath9337b412018-06-06 11:35:23 +0000387 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
388 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
389 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
390 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
391 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
392 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393
Pavel Labath9337b412018-06-06 11:35:23 +0000394 m_index = AppleDWARFIndex::Create(
395 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
Pavel Labath0261b942019-05-20 08:38:47 +0000396 apple_types, apple_objc, m_context.getOrLoadStrData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397
Pavel Labath9337b412018-06-06 11:35:23 +0000398 if (m_index)
399 return;
400
401 DWARFDataExtractor debug_names;
402 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
403 if (debug_names.GetByteSize() > 0) {
404 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
Pavel Labath0261b942019-05-20 08:38:47 +0000405 DebugNamesDWARFIndex::Create(
406 *GetObjectFile()->GetModule(), debug_names,
407 m_context.getOrLoadStrData(), DebugInfo());
Pavel Labath9337b412018-06-06 11:35:23 +0000408 if (index_or) {
409 m_index = std::move(*index_or);
410 return;
411 }
412 LLDB_LOG_ERROR(log, index_or.takeError(),
413 "Unable to read .debug_names data: {0}");
414 }
415 }
416
417 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
418 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419}
420
421bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000422 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423}
424
425uint32_t SymbolFileDWARF::CalculateAbilities() {
426 uint32_t abilities = 0;
Konrad Kleine248a1302019-05-23 11:14:47 +0000427 if (m_obj_file != nullptr) {
428 const Section *section = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 const SectionList *section_list = m_obj_file->GetSectionList();
Konrad Kleine248a1302019-05-23 11:14:47 +0000430 if (section_list == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 return 0;
432
433 uint64_t debug_abbrev_file_size = 0;
434 uint64_t debug_info_file_size = 0;
435 uint64_t debug_line_file_size = 0;
436
437 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
438
439 if (section)
440 section_list = &section->GetChildren();
441
442 section =
443 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
Konrad Kleine248a1302019-05-23 11:14:47 +0000444 if (section != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 debug_info_file_size = section->GetFileSize();
446
447 section =
448 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
449 .get();
450 if (section)
451 debug_abbrev_file_size = section->GetFileSize();
452
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000453 DWARFDebugAbbrev *abbrev = DebugAbbrev();
454 if (abbrev) {
455 std::set<dw_form_t> invalid_forms;
456 abbrev->GetUnsupportedForms(invalid_forms);
457 if (!invalid_forms.empty()) {
458 StreamString error;
459 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
460 for (auto form : invalid_forms)
461 error.Printf(" %#x", form);
462 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
463 return 0;
464 }
465 }
466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 section =
468 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
469 .get();
470 if (section)
471 debug_line_file_size = section->GetFileSize();
472 } else {
473 const char *symfile_dir_cstr =
474 m_obj_file->GetFileSpec().GetDirectory().GetCString();
475 if (symfile_dir_cstr) {
476 if (strcasestr(symfile_dir_cstr, ".dsym")) {
477 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000478 // We have a dSYM file that didn't have a any debug info. If the
479 // string table has a size of 1, then it was made from an
480 // executable with no debug info, or from an executable that was
481 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 section =
483 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
484 .get();
485 if (section && section->GetFileSize() == 1) {
486 m_obj_file->GetModule()->ReportWarning(
487 "empty dSYM file detected, dSYM was created with an "
488 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494
495 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
496 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
497 LocalVariables | VariableTypes;
498
499 if (debug_line_file_size > 0)
500 abilities |= LineTables;
501 }
502 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505const DWARFDataExtractor &
506SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
507 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000508 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
509 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
510 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512}
513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
515 DWARFDataExtractor &data) {
516 ModuleSP module_sp(m_obj_file->GetModule());
517 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000518 if (!section_list)
519 return;
520
521 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
522 if (!section_sp)
523 return;
524
525 data.Clear();
526 m_obj_file->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527}
528
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000529const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
530 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
531 if (debugLocData.GetByteSize() > 0)
532 return debugLocData;
533 return get_debug_loclists_data();
534}
535
536const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
537 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
538}
539
540const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
541 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
542 m_data_debug_loclists);
543}
544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000546 if (m_abbr)
547 return m_abbr.get();
548
Pavel Labath0261b942019-05-20 08:38:47 +0000549 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000550 if (debug_abbrev_data.GetByteSize() == 0)
551 return nullptr;
552
553 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
554 llvm::Error error = abbr->parse(debug_abbrev_data);
555 if (error) {
556 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
557 LLDB_LOG_ERROR(log, std::move(error),
558 "Unable to read .debug_abbrev section: {0}");
559 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000561
562 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
567 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000571 if (m_info == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000572 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
573 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
574 static_cast<void *>(this));
Pavel Labath6a2eb362019-06-14 13:01:16 +0000575 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0)
576 m_info = llvm::make_unique<DWARFDebugInfo>(*this, m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
578 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
582 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000585DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
587 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000588 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589
Alex Langford5ada8872019-06-08 00:55:03 +0000590 DWARFDebugInfo *info = DebugInfo();
591 if (info) {
592 // The compile unit ID is the index of the DWARF unit.
593 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
594 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
595 dwarf_cu->SetUserData(comp_unit);
596 return dwarf_cu;
597 }
598 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599}
600
Pavel Labathd67b5502019-07-26 13:15:28 +0000601DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000602 if (!m_ranges) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000603 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
604 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
605 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000606
Pavel Labath00e39682019-06-14 14:12:25 +0000607 if (m_context.getOrLoadRangesData().GetByteSize() > 0)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000608 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000609
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000610 if (m_ranges)
Pavel Labath00e39682019-06-14 14:12:25 +0000611 m_ranges->Extract(m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 }
613 return m_ranges.get();
614}
615
Pavel Labathd67b5502019-07-26 13:15:28 +0000616DWARFDebugRngLists *SymbolFileDWARF::GetDebugRngLists() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000617 if (!m_rnglists) {
618 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
619 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
620 static_cast<void *>(this));
621
Pavel Labath00e39682019-06-14 14:12:25 +0000622 if (m_context.getOrLoadRngListsData().GetByteSize() > 0)
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000623 m_rnglists.reset(new DWARFDebugRngLists());
624
625 if (m_rnglists)
Pavel Labath00e39682019-06-14 14:12:25 +0000626 m_rnglists->Extract(m_context);
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000627 }
628 return m_rnglists.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629}
630
Pavel Labathad17e282019-06-13 11:22:47 +0000631lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 CompUnitSP cu_sp;
Pavel Labathad17e282019-06-13 11:22:47 +0000633 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
634 if (comp_unit) {
635 // We already parsed this compile unit, had out a shared pointer to it
636 cu_sp = comp_unit->shared_from_this();
637 } else {
Pavel Labath6a2eb362019-06-14 13:01:16 +0000638 if (&dwarf_cu.GetSymbolFileDWARF() != this) {
639 return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu);
Pavel Labathad17e282019-06-13 11:22:47 +0000640 } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
641 // Let the debug map create the compile unit
642 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
643 dwarf_cu.SetUserData(cu_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 } else {
Pavel Labathad17e282019-06-13 11:22:47 +0000645 ModuleSP module_sp(m_obj_file->GetModule());
646 if (module_sp) {
647 const DWARFDIE cu_die = dwarf_cu.DIE();
648 if (cu_die) {
649 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
650 if (cu_file_spec) {
651 // If we have a full path to the compile unit, we don't need to
652 // resolve the file. This can be expensive e.g. when the source
653 // files are NFS mounted.
654 cu_file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655
Pavel Labathad17e282019-06-13 11:22:47 +0000656 std::string remapped_file;
657 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
658 remapped_file))
659 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 }
Pavel Labathad17e282019-06-13 11:22:47 +0000661
662 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
663 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
664
665 bool is_optimized = dwarf_cu.GetIsOptimized();
666 BuildCuTranslationTable();
667 cu_sp = std::make_shared<CompileUnit>(
668 module_sp, &dwarf_cu, cu_file_spec,
669 *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
670 is_optimized ? eLazyBoolYes : eLazyBoolNo);
671
672 dwarf_cu.SetUserData(cu_sp.get());
673
Pavel Labathe0119902019-07-23 09:24:02 +0000674 SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 }
676 }
677 }
678 }
679 return cu_sp;
680}
681
Pavel Labathad17e282019-06-13 11:22:47 +0000682void SymbolFileDWARF::BuildCuTranslationTable() {
683 if (!m_lldb_cu_to_dwarf_unit.empty())
684 return;
685
686 DWARFDebugInfo *info = DebugInfo();
687 if (!info)
688 return;
689
690 if (!info->ContainsTypeUnits()) {
691 // We can use a 1-to-1 mapping. No need to build a translation table.
692 return;
693 }
694 for (uint32_t i = 0, num = info->GetNumUnits(); i < num; ++i) {
695 if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info->GetUnitAtIndex(i))) {
696 cu->SetID(m_lldb_cu_to_dwarf_unit.size());
697 m_lldb_cu_to_dwarf_unit.push_back(i);
698 }
699 }
700}
701
702llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
703 BuildCuTranslationTable();
704 if (m_lldb_cu_to_dwarf_unit.empty())
705 return cu_idx;
706 if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
707 return llvm::None;
708 return m_lldb_cu_to_dwarf_unit[cu_idx];
709}
710
Pavel Labathe0119902019-07-23 09:24:02 +0000711uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000713 if (!info)
714 return 0;
715 BuildCuTranslationTable();
716 return m_lldb_cu_to_dwarf_unit.empty() ? info->GetNumUnits()
717 : m_lldb_cu_to_dwarf_unit.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718}
719
720CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000721 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000723 if (!info)
724 return {};
725
726 if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
727 if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
728 info->GetUnitAtIndex(*dwarf_idx)))
729 return ParseCompileUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 }
Pavel Labathad17e282019-06-13 11:22:47 +0000731 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732}
733
Zachary Turner863f8c12019-01-11 18:03:20 +0000734Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
735 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000736 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 if (die.IsValid()) {
738 TypeSystem *type_system =
739 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
740
741 if (type_system) {
742 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
743 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000744 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 }
746 }
747 return nullptr;
748}
749
750bool SymbolFileDWARF::FixupAddress(Address &addr) {
751 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
752 if (debug_map_symfile) {
753 return debug_map_symfile->LinkOSOAddress(addr);
754 }
755 // This is a normal DWARF file, no address fixups need to happen
756 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000757}
Zachary Turner863f8c12019-01-11 18:03:20 +0000758lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000759 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000760 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 if (dwarf_cu)
762 return dwarf_cu->GetLanguageType();
763 else
764 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000765}
766
Zachary Turner863f8c12019-01-11 18:03:20 +0000767size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000768 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000769 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000770 if (!dwarf_cu)
771 return 0;
772
773 size_t functions_added = 0;
774 std::vector<DWARFDIE> function_dies;
775 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
776 for (const DWARFDIE &die : function_dies) {
777 if (comp_unit.FindFunctionByUID(die.GetID()))
778 continue;
779 if (ParseFunction(comp_unit, die))
780 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000782 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Zachary Turner863f8c12019-01-11 18:03:20 +0000786bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
787 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000788 ASSERT_MODULE_LOCK(this);
Pavel Labathad17e282019-06-13 11:22:47 +0000789 if (DWARFUnit *unit = GetDWARFCompileUnit(&comp_unit)) {
Pavel Labathca9c3de2019-06-12 11:29:50 +0000790 const dw_offset_t stmt_list = unit->GetLineTableOffset();
791 if (stmt_list != DW_INVALID_OFFSET) {
792 // All file indexes in DWARF are one based and a file of index zero is
793 // supposed to be the compile unit itself.
794 support_files.Append(comp_unit);
795 return DWARFDebugLine::ParseSupportFiles(comp_unit.GetModule(),
796 m_context.getOrLoadLineData(),
797 stmt_list, support_files, unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000798 }
799 }
800 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801}
802
Pavel Labathca9c3de2019-06-12 11:29:50 +0000803FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) {
Pavel Labathad17e282019-06-13 11:22:47 +0000804 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) {
805 if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu))
806 return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx);
807 return FileSpec();
808 }
809
810 auto &tu = llvm::cast<DWARFTypeUnit>(unit);
811 return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx);
Pavel Labathca9c3de2019-06-12 11:29:50 +0000812}
813
814const FileSpecList &
815SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) {
816 static FileSpecList empty_list;
817
818 dw_offset_t offset = tu.GetLineTableOffset();
819 if (offset == DW_INVALID_OFFSET ||
820 offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() ||
821 offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey())
822 return empty_list;
823
824 // Many type units can share a line table, so parse the support file list
825 // once, and cache it based on the offset field.
826 auto iter_bool = m_type_unit_support_files.try_emplace(offset);
827 FileSpecList &list = iter_bool.first->second;
828 if (iter_bool.second) {
Jonas Devliegheref893d5b2019-07-18 20:19:24 +0000829 list.EmplaceBack();
Pavel Labathca9c3de2019-06-12 11:29:50 +0000830 DWARFDebugLine::ParseSupportFiles(GetObjectFile()->GetModule(),
831 m_context.getOrLoadLineData(), offset,
832 list, &tu);
833 }
834 return list;
835}
836
Zachary Turner863f8c12019-01-11 18:03:20 +0000837bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000838 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000839 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 if (dwarf_cu)
841 return dwarf_cu->GetIsOptimized();
842 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000843}
844
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845bool SymbolFileDWARF::ParseImportedModules(
846 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000847 std::vector<SourceModule> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000848 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000850 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000851 if (!dwarf_cu)
852 return false;
853 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
854 sc.comp_unit->GetLanguage()))
855 return false;
856 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000858 const DWARFDIE die = dwarf_cu->DIE();
859 if (!die)
860 return false;
861
862 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
863 child_die = child_die.GetSibling()) {
864 if (child_die.Tag() != DW_TAG_imported_declaration)
865 continue;
866
867 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
868 if (module_die.Tag() != DW_TAG_module)
869 continue;
870
871 if (const char *name =
872 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
873 SourceModule module;
874 module.path.push_back(ConstString(name));
875
876 DWARFDIE parent_die = module_die;
877 while ((parent_die = parent_die.GetParent())) {
878 if (parent_die.Tag() != DW_TAG_module)
879 break;
880 if (const char *name =
881 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
882 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000884 std::reverse(module.path.begin(), module.path.end());
885 if (const char *include_path = module_die.GetAttributeValueAsString(
886 DW_AT_LLVM_include_path, nullptr))
887 module.search_path = ConstString(include_path);
888 if (const char *sysroot = module_die.GetAttributeValueAsString(
889 DW_AT_LLVM_isysroot, nullptr))
890 module.sysroot = ConstString(sysroot);
891 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000894 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897struct ParseDWARFLineTableCallbackInfo {
898 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000899 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901};
902
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904static void ParseDWARFLineTableCallback(dw_offset_t offset,
905 const DWARFDebugLine::State &state,
906 void *userData) {
907 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
908 // Just started parsing the line table
909 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
910 // Done parsing line table, nothing to do for the cleanup
911 } else {
912 ParseDWARFLineTableCallbackInfo *info =
913 (ParseDWARFLineTableCallbackInfo *)userData;
914 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915
Adrian Prantl05097242018-04-30 16:49:04 +0000916 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 if (!info->sequence_up) {
918 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
919 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000922 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 state.column, state.file, state.is_stmt, state.basic_block,
924 state.prologue_end, state.epilogue_begin, state.end_sequence);
925 if (state.end_sequence) {
926 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000927 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 }
931 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932}
933
Zachary Turner863f8c12019-01-11 18:03:20 +0000934bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000935 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +0000936 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938
Zachary Turner863f8c12019-01-11 18:03:20 +0000939 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000940 if (dwarf_cu) {
941 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
942 if (dwarf_cu_die) {
943 const dw_offset_t cu_line_offset =
944 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
945 DW_INVALID_OFFSET);
946 if (cu_line_offset != DW_INVALID_OFFSET) {
947 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
948 if (line_table_up) {
949 ParseDWARFLineTableCallbackInfo info;
950 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000951
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000952 /*
953 * MIPS:
954 * The SymbolContext may not have a valid target, thus we may not be
955 * able
956 * to call Address::GetOpcodeLoadAddress() which would clear the bit
957 * #0
958 * for MIPS. Use ArchSpec to clear the bit #0.
959 */
960 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
961 case llvm::Triple::mips:
962 case llvm::Triple::mipsel:
963 case llvm::Triple::mips64:
964 case llvm::Triple::mips64el:
965 info.addr_mask = ~((lldb::addr_t)1);
966 break;
967 default:
968 info.addr_mask = ~((lldb::addr_t)0);
969 break;
970 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000971
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000972 lldb::offset_t offset = cu_line_offset;
Pavel Labath0261b942019-05-20 08:38:47 +0000973 DWARFDebugLine::ParseStatementTable(
974 m_context.getOrLoadLineData(), &offset,
975 ParseDWARFLineTableCallback, &info, dwarf_cu);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000976 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
977 if (debug_map_symfile) {
978 // We have an object file that has a line table with addresses that
979 // are not linked. We need to link the line table and convert the
980 // addresses that are relative to the .o file into addresses for
981 // the main executable.
982 comp_unit.SetLineTable(
983 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
984 } else {
985 comp_unit.SetLineTable(line_table_up.release());
986 return true;
987 }
988 }
989 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 }
992 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
Siva Chandrad8335e92015-12-16 00:22:08 +0000995lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
997 auto iter = m_debug_macros_map.find(*offset);
998 if (iter != m_debug_macros_map.end())
999 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001000
Pavel Labath0261b942019-05-20 08:38:47 +00001001 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001002 if (debug_macro_data.GetByteSize() == 0)
1003 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1006 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001009 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +00001010 DWARFDebugMacroEntry::ReadMacroEntries(
1011 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
1012 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001015}
1016
Zachary Turner863f8c12019-01-11 18:03:20 +00001017bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001018 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001019
Zachary Turner863f8c12019-01-11 18:03:20 +00001020 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001022 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023
Jan Kratochvil93afb052018-05-24 20:51:13 +00001024 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 if (!dwarf_cu_die)
1026 return false;
1027
1028 lldb::offset_t sect_offset =
1029 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1030 if (sect_offset == DW_INVALID_OFFSET)
1031 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1032 DW_INVALID_OFFSET);
1033 if (sect_offset == DW_INVALID_OFFSET)
1034 return false;
1035
Zachary Turner863f8c12019-01-11 18:03:20 +00001036 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037
1038 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001039}
1040
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001041size_t SymbolFileDWARF::ParseBlocksRecursive(
1042 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1043 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 size_t blocks_added = 0;
1045 DWARFDIE die = orig_die;
1046 while (die) {
1047 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001048
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 switch (tag) {
1050 case DW_TAG_inlined_subroutine:
1051 case DW_TAG_subprogram:
1052 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001053 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001055 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1056 // inlined functions. These will be parsed on their own as separate
1057 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058
1059 if (depth > 0)
1060 break;
1061
1062 block = parent_block;
1063 } else {
1064 BlockSP block_sp(new Block(die.GetID()));
1065 parent_block->AddChild(block_sp);
1066 block = block_sp.get();
1067 }
1068 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001069 const char *name = nullptr;
1070 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071
1072 int decl_file = 0;
1073 int decl_line = 0;
1074 int decl_column = 0;
1075 int call_file = 0;
1076 int call_line = 0;
1077 int call_column = 0;
1078 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1079 decl_line, decl_column, call_file, call_line,
1080 call_column, nullptr)) {
1081 if (tag == DW_TAG_subprogram) {
1082 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1083 subprogram_low_pc = ranges.GetMinRangeBase(0);
1084 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001085 // We get called here for inlined subroutines in two ways. The first
1086 // time is when we are making the Function object for this inlined
1087 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001089 // need to adjust the containing address. The second time is when we
1090 // are parsing the blocks inside the function that contains the
1091 // inlined concrete instance. Since these will be blocks inside the
1092 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1094 subprogram_low_pc = ranges.GetMinRangeBase(0);
1095 }
1096 }
1097
1098 const size_t num_ranges = ranges.GetSize();
1099 for (size_t i = 0; i < num_ranges; ++i) {
1100 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1101 const addr_t range_base = range.GetRangeBase();
1102 if (range_base >= subprogram_low_pc)
1103 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1104 range.GetByteSize()));
1105 else {
1106 GetObjectFile()->GetModule()->ReportError(
1107 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1108 ") which has a base that is less than the function's low PC "
1109 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1110 "start of this error message",
1111 block->GetID(), range_base, range.GetRangeEnd(),
1112 subprogram_low_pc);
1113 }
1114 }
1115 block->FinalizeRanges();
1116
1117 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001118 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001119 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001121 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001122 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 decl_line, decl_column));
1124
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001125 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001127 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001128 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 call_line, call_column));
1130
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001131 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1132 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 }
1134
1135 ++blocks_added;
1136
1137 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001138 blocks_added +=
1139 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1140 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 }
1142 }
1143 } break;
1144 default:
1145 break;
1146 }
1147
Adrian Prantl05097242018-04-30 16:49:04 +00001148 // Only parse siblings of the block if we are not at depth zero. A depth of
1149 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1150 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151
1152 if (depth == 0)
1153 die.Clear();
1154 else
1155 die = die.GetSibling();
1156 }
1157 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001158}
1159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1161 if (parent_die) {
1162 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1163 die = die.GetSibling()) {
1164 dw_tag_t tag = die.Tag();
1165 bool check_virtuality = false;
1166 switch (tag) {
1167 case DW_TAG_inheritance:
1168 case DW_TAG_subprogram:
1169 check_virtuality = true;
1170 break;
1171 default:
1172 break;
1173 }
1174 if (check_virtuality) {
1175 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1176 return true;
1177 }
1178 }
1179 }
1180 return false;
1181}
1182
1183void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1184 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1185 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1186 std::vector<DWARFDIE> decl_ctx_die_list =
1187 ast_parser->GetDIEForDeclContext(decl_ctx);
1188
1189 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1190 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1191 decl = decl.GetSibling())
1192 ast_parser->GetDeclForUIDFromDWARF(decl);
1193}
1194
Pavel Labath3b926982019-06-21 07:56:50 +00001195user_id_t SymbolFileDWARF::GetUID(DIERef ref) {
1196 if (GetDebugMapSymfile())
1197 return GetID() | ref.die_offset();
1198
1199 return user_id_t(GetDwoNum().getValueOr(0x7fffffff)) << 32 |
1200 ref.die_offset() |
1201 (lldb::user_id_t(ref.section() == DIERef::Section::DebugTypes) << 63);
1202}
1203
Pavel Labath67b45ac2019-06-19 07:32:39 +00001204llvm::Optional<SymbolFileDWARF::DecodedUID>
1205SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001206 // This method can be called without going through the symbol vendor so we
1207 // need to lock the module.
1208 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001209 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1210 // must make sure we use the correct DWARF file when resolving things. On
1211 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1212 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1213 // references to other DWARF objects and we must be ready to receive a
1214 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001216 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1217 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labath67b45ac2019-06-19 07:32:39 +00001219 return DecodedUID{
Pavel Labath3b926982019-06-21 07:56:50 +00001220 *dwarf, {llvm::None, DIERef::Section::DebugInfo, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001221 }
Pavel Labath381ba9a2019-05-13 08:58:34 +00001222 dw_offset_t die_offset = uid;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001223 if (die_offset == DW_INVALID_OFFSET)
Pavel Labath67b45ac2019-06-19 07:32:39 +00001224 return llvm::None;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001225
Pavel Labath3b926982019-06-21 07:56:50 +00001226 DIERef::Section section =
1227 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1228
1229 llvm::Optional<uint32_t> dwo_num = uid >> 32 & 0x7fffffff;
1230 if (*dwo_num == 0x7fffffff)
1231 dwo_num = llvm::None;
1232
1233 return DecodedUID{*this, {dwo_num, section, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001234}
1235
1236DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001237SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001238 // This method can be called without going through the symbol vendor so we
1239 // need to lock the module.
1240 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001241
Pavel Labath67b45ac2019-06-19 07:32:39 +00001242 llvm::Optional<DecodedUID> decoded = DecodeUID(uid);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001243
Pavel Labath67b45ac2019-06-19 07:32:39 +00001244 if (decoded)
1245 return decoded->dwarf.GetDIE(decoded->ref);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001248}
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001251 // This method can be called without going through the symbol vendor so we
1252 // need to lock the module.
1253 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001254 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001255 // SymbolFileDWARF::GetDIE(). See comments inside the
1256 // SymbolFileDWARF::GetDIE() for details.
1257 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 return die.GetDecl();
1259 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001260}
1261
Greg Clayton99558cc42015-08-24 23:46:31 +00001262CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001264 // This method can be called without going through the symbol vendor so we
1265 // need to lock the module.
1266 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001267 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001268 // SymbolFileDWARF::GetDIE(). See comments inside the
1269 // SymbolFileDWARF::GetDIE() for details.
1270 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 return die.GetDeclContext();
1272 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001273}
1274
Greg Clayton99558cc42015-08-24 23:46:31 +00001275CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001277 // This method can be called without going through the symbol vendor so we
1278 // need to lock the module.
1279 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001280 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001281 // SymbolFileDWARF::GetDIE(). See comments inside the
1282 // SymbolFileDWARF::GetDIE() for details.
1283 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 return die.GetContainingDeclContext();
1285 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286}
1287
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001289 // This method can be called without going through the symbol vendor so we
1290 // need to lock the module.
1291 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001292 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001293 // SymbolFileDWARF::GetDIE(). See comments inside the
1294 // SymbolFileDWARF::GetDIE() for details.
1295 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 return type_die.ResolveType();
1297 else
1298 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299}
1300
Adrian Prantleca07c52018-11-05 20:49:07 +00001301llvm::Optional<SymbolFile::ArrayInfo>
1302SymbolFileDWARF::GetDynamicArrayInfoForUID(
1303 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1304 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001305 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001306 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1307 else
1308 return llvm::None;
1309}
1310
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1312 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001313}
1314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1316 bool assert_not_being_parsed) {
1317 if (die) {
1318 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1319 if (log)
1320 GetObjectFile()->GetModule()->LogMessage(
1321 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1322 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001323
Adrian Prantl05097242018-04-30 16:49:04 +00001324 // We might be coming in in the middle of a type tree (a class within a
1325 // class, an enum within a class), so parse any needed parent DIEs before
1326 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1328 if (decl_ctx_die) {
1329 if (log) {
1330 switch (decl_ctx_die.Tag()) {
1331 case DW_TAG_structure_type:
1332 case DW_TAG_union_type:
1333 case DW_TAG_class_type: {
1334 // Get the type, which could be a forward declaration
1335 if (log)
1336 GetObjectFile()->GetModule()->LogMessage(
1337 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1338 "resolve parent forward type for 0x%8.8x",
1339 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1340 decl_ctx_die.GetOffset());
1341 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001342
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 default:
1344 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001347 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348 return ResolveType(die);
1349 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001350 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001351}
1352
Greg Clayton6beaaa62011-01-17 03:46:26 +00001353// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001354// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1355// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356bool SymbolFileDWARF::HasForwardDeclForClangType(
1357 const CompilerType &compiler_type) {
1358 CompilerType compiler_type_no_qualifiers =
1359 ClangUtil::RemoveFastQualifiers(compiler_type);
1360 if (GetForwardDeclClangTypeToDie().count(
1361 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1362 return true;
1363 }
1364 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001365
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 ClangASTContext *clang_type_system =
1367 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1368 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001369 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 DWARFASTParserClang *ast_parser =
1371 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1372 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001373}
1374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001376 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 ClangASTContext *clang_type_system =
1379 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1380 if (clang_type_system) {
1381 DWARFASTParserClang *ast_parser =
1382 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1383 if (ast_parser &&
1384 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1385 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1386 }
Jim Inghamc3549282012-01-11 02:21:12 +00001387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 // We have a struct/union/class/enum that needs to be fully resolved.
1389 CompilerType compiler_type_no_qualifiers =
1390 ClangUtil::RemoveFastQualifiers(compiler_type);
1391 auto die_it = GetForwardDeclClangTypeToDie().find(
1392 compiler_type_no_qualifiers.GetOpaqueQualType());
1393 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1394 // We have already resolved this type...
1395 return true;
1396 }
1397
1398 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1399 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001400 // Once we start resolving this type, remove it from the forward
1401 // declaration map in case anyone child members or other types require this
1402 // type to get resolved. The type will get resolved when all of the calls
1403 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 GetForwardDeclClangTypeToDie().erase(die_it);
1405
1406 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1407
1408 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1409 DWARF_LOG_TYPE_COMPLETION));
1410 if (log)
1411 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1412 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1413 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1414 type->GetName().AsCString());
1415 assert(compiler_type);
1416 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1417 if (dwarf_ast)
1418 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1419 }
1420 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001421}
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1424 bool assert_not_being_parsed,
1425 bool resolve_function_context) {
1426 if (die) {
1427 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1428
1429 if (assert_not_being_parsed) {
1430 if (type != DIE_IS_BEING_PARSED)
1431 return type;
1432
1433 GetObjectFile()->GetModule()->ReportError(
1434 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1435 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1436
1437 } else
1438 return type;
1439 }
1440 return nullptr;
1441}
1442
Pavel Labathad17e282019-06-13 11:22:47 +00001443CompileUnit *
1444SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001446 if (dwarf_cu.GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001447 // The symbol vendor doesn't know about this compile unit, we need to parse
1448 // and add it to the symbol vendor object.
Pavel Labath519ef6a2019-05-27 10:10:59 +00001449 return ParseCompileUnit(dwarf_cu).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 }
Pavel Labathad17e282019-06-13 11:22:47 +00001451 return (CompileUnit *)dwarf_cu.GetUserData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452}
1453
1454size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1455 DIEArray &method_die_offsets) {
1456 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001457 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 return method_die_offsets.size();
1459}
1460
1461bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1462 sc.Clear(false);
1463
Pavel Labathad17e282019-06-13 11:22:47 +00001464 if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001466 sc.comp_unit =
1467 GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468
1469 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001470 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001471 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472
1473 if (sc.function) {
1474 sc.module_sp = sc.function->CalculateSymbolContextModule();
1475 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 }
1478
1479 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480}
1481
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1483 UpdateExternalModuleListIfNeeded();
1484 const auto &pos = m_external_type_modules.find(name);
1485 if (pos != m_external_type_modules.end())
1486 return pos->second;
1487 else
1488 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001489}
1490
Greg Clayton2f869fe2016-03-30 20:14:35 +00001491DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
Pavel Labath3b926982019-06-21 07:56:50 +00001493 if (die_ref.dwo_num()) {
1494 return DebugInfo()
1495 ->GetUnitAtIndex(*die_ref.dwo_num())
1496 ->GetDwoSymbolFile()
1497 ->GetDIE(die_ref);
1498 }
1499
1500
Kate Stoneb9c1b512016-09-06 20:57:50 +00001501 DWARFDebugInfo *debug_info = DebugInfo();
1502 if (debug_info)
1503 return debug_info->GetDIE(die_ref);
1504 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001505 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001506}
1507
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508std::unique_ptr<SymbolFileDWARFDwo>
1509SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Pavel Labathad17e282019-06-13 11:22:47 +00001510 DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001512 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 // info files for modules.
1514 if (GetDebugMapSymfile())
1515 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001516
Pavel Labathad17e282019-06-13 11:22:47 +00001517 DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit);
1518 // Only compile units can be split into two parts.
1519 if (!dwarf_cu)
1520 return nullptr;
1521
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001522 const char *dwo_name =
Pavel Labathad17e282019-06-13 11:22:47 +00001523 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524 if (!dwo_name)
1525 return nullptr;
1526
Tamas Berghammer963ce482017-08-25 13:56:14 +00001527 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1528 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001529 uint64_t dwo_id =
Pavel Labathad17e282019-06-13 11:22:47 +00001530 cu_die.GetAttributeValueAsUnsigned(dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001531 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
Pavel Labathad17e282019-06-13 11:22:47 +00001532 dwp_symfile->GetSymbolFileForDwoId(*dwarf_cu, dwo_id);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001533 if (dwo_symfile)
1534 return dwo_symfile;
1535 }
1536
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001537 FileSpec dwo_file(dwo_name);
1538 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001540 const char *comp_dir =
Pavel Labathad17e282019-06-13 11:22:47 +00001541 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 if (!comp_dir)
1543 return nullptr;
1544
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001545 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1546 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 dwo_file.AppendPathComponent(dwo_name);
1548 }
1549
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001550 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 return nullptr;
1552
1553 const lldb::offset_t file_offset = 0;
1554 DataBufferSP dwo_file_data_sp;
1555 lldb::offset_t dwo_file_data_offset = 0;
1556 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1557 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001558 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1559 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 if (dwo_obj_file == nullptr)
1561 return nullptr;
1562
Pavel Labathad17e282019-06-13 11:22:47 +00001563 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001564}
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1567 if (m_fetched_external_modules)
1568 return;
1569 m_fetched_external_modules = true;
1570
1571 DWARFDebugInfo *debug_info = DebugInfo();
1572
1573 const uint32_t num_compile_units = GetNumCompileUnits();
1574 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001575 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576
Jan Kratochvil93afb052018-05-24 20:51:13 +00001577 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001578 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1580
1581 if (name) {
1582 ConstString const_name(name);
1583 if (m_external_type_modules.find(const_name) ==
1584 m_external_type_modules.end()) {
1585 ModuleSP module_sp;
1586 const char *dwo_path =
1587 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1588 if (dwo_path) {
1589 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001590 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001591 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1593 const char *comp_dir =
1594 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1595 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001596 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001597 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001598 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1600 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001601 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 dwo_module_spec.GetArchitecture() =
1603 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001604
Adrian Prantl05097242018-04-30 16:49:04 +00001605 // When LLDB loads "external" modules it looks at the presence of
1606 // DW_AT_GNU_dwo_name. However, when the already created module
1607 // (corresponding to .dwo itself) is being processed, it will see
1608 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1609 // dwo file) and will try to call ModuleList::GetSharedModule
1610 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1611 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1612 // DW_AT_comp_dir. In this case the method
1613 // ModuleList::GetSharedModule will fail and the warning will be
1614 // printed. However, as one can notice in this case we don't
1615 // actually need to try to load the already loaded module
1616 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001617 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001618 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1619 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1620 continue;
1621 }
1622
Zachary Turner97206d52017-05-12 04:51:55 +00001623 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001624 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 if (!module_sp) {
1626 GetObjectFile()->GetModule()->ReportWarning(
1627 "0x%8.8x: unable to locate module needed for external types: "
1628 "%s\nerror: %s\nDebugging will be degraded due to missing "
1629 "types. Rebuilding your project will regenerate the needed "
1630 "module files.",
1631 die.GetOffset(),
1632 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1633 error.AsCString("unknown error"));
1634 }
1635 }
1636 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001637 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 }
1641}
1642
1643SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001644 if (!m_global_aranges_up) {
1645 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646
1647 ModuleSP module_sp = GetObjectFile()->GetModule();
1648 if (module_sp) {
1649 const size_t num_cus = module_sp->GetNumCompileUnits();
1650 for (size_t i = 0; i < num_cus; ++i) {
1651 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1652 if (cu_sp) {
1653 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1654 if (globals_sp) {
1655 const size_t num_globals = globals_sp->GetSize();
1656 for (size_t g = 0; g < num_globals; ++g) {
1657 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1658 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1659 const DWARFExpression &location = var_sp->LocationExpression();
1660 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001661 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001662 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1663 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 if (location_result.GetValueType() ==
1665 Value::eValueTypeFileAddress) {
1666 lldb::addr_t file_addr =
1667 location_result.GetScalar().ULongLong();
1668 lldb::addr_t byte_size = 1;
1669 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001670 byte_size =
1671 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001672 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 file_addr, byte_size, var_sp.get()));
1674 }
1675 }
1676 }
1677 }
1678 }
1679 }
1680 }
1681 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001682 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001684 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685}
1686
1687uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001688 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001690 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1691 Timer scoped_timer(func_cat,
1692 "SymbolFileDWARF::"
1693 "ResolveSymbolContext (so_addr = { "
1694 "section = %p, offset = 0x%" PRIx64
1695 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 static_cast<void *>(so_addr.GetSection().get()),
1697 so_addr.GetOffset(), resolve_scope);
1698 uint32_t resolved = 0;
1699 if (resolve_scope &
1700 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1701 eSymbolContextLineEntry | eSymbolContextVariable)) {
1702 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1703
1704 DWARFDebugInfo *debug_info = DebugInfo();
1705 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001706 llvm::Expected<DWARFDebugAranges &> aranges =
1707 debug_info->GetCompileUnitAranges();
1708 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001709 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001710 LLDB_LOG_ERROR(log, aranges.takeError(),
1711 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1712 "aranges. {0}");
1713 return 0;
1714 }
1715
1716 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001717 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001718 // Global variables are not in the compile unit address ranges. The
1719 // only way to currently find global variables is to iterate over the
1720 // .debug_pubnames or the __apple_names table and find all items in
1721 // there that point to DW_TAG_variable DIEs and then find the address
1722 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723 if (resolve_scope & eSymbolContextVariable) {
1724 GlobalVariableMap &map = GetGlobalAranges();
1725 const GlobalVariableMap::Entry *entry =
1726 map.FindEntryThatContains(file_vm_addr);
1727 if (entry && entry->data) {
1728 Variable *variable = entry->data;
1729 SymbolContextScope *scc = variable->GetSymbolContextScope();
1730 if (scc) {
1731 scc->CalculateSymbolContext(&sc);
1732 sc.variable = variable;
1733 }
1734 return sc.GetResolvedMask();
1735 }
1736 }
1737 } else {
1738 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathad17e282019-06-13 11:22:47 +00001739 if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>(
1740 debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1741 cu_offset, &cu_idx))) {
1742 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743 if (sc.comp_unit) {
1744 resolved |= eSymbolContextCompUnit;
1745
1746 bool force_check_line_table = false;
1747 if (resolve_scope &
1748 (eSymbolContextFunction | eSymbolContextBlock)) {
1749 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1750 DWARFDIE block_die;
1751 if (function_die) {
1752 sc.function =
1753 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001754 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001755 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756
1757 if (sc.function && (resolve_scope & eSymbolContextBlock))
1758 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1759 } else {
1760 // We might have had a compile unit that had discontiguous
1761 // address ranges where the gaps are symbols that don't have
1762 // any debug info. Discontiguous compile unit address ranges
1763 // should only happen when there aren't other functions from
1764 // other compile units in these gaps. This helps keep the size
1765 // of the aranges down.
1766 force_check_line_table = true;
1767 }
1768
Konrad Kleine248a1302019-05-23 11:14:47 +00001769 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 resolved |= eSymbolContextFunction;
1771
1772 if (resolve_scope & eSymbolContextBlock) {
1773 Block &block = sc.function->GetBlock(true);
1774
1775 if (block_die)
1776 sc.block = block.FindBlockByID(block_die.GetID());
1777 else
1778 sc.block = block.FindBlockByID(function_die.GetID());
1779 if (sc.block)
1780 resolved |= eSymbolContextBlock;
1781 }
1782 }
1783 }
1784
1785 if ((resolve_scope & eSymbolContextLineEntry) ||
1786 force_check_line_table) {
1787 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001788 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001790 // terms of this debug file if there is no debug map, or it
1791 // will be an address in the .o file which needs to be fixed up
1792 // to be in terms of the debug map executable. Either way,
1793 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 Address exe_so_addr(so_addr);
1795 if (FixupAddress(exe_so_addr)) {
1796 if (line_table->FindLineEntryByAddress(exe_so_addr,
1797 sc.line_entry)) {
1798 resolved |= eSymbolContextLineEntry;
1799 }
1800 }
1801 }
1802 }
1803
1804 if (force_check_line_table &&
1805 !(resolved & eSymbolContextLineEntry)) {
1806 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001807 // address ranges where the gaps are symbols that don't have any
1808 // debug info. Discontiguous compile unit address ranges should
1809 // only happen when there aren't other functions from other
1810 // compile units in these gaps. This helps keep the size of the
1811 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001812 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 resolved &= ~eSymbolContextCompUnit;
1814 }
1815 } else {
1816 GetObjectFile()->GetModule()->ReportWarning(
1817 "0x%8.8x: compile unit %u failed to create a valid "
1818 "lldb_private::CompileUnit class.",
1819 cu_offset, cu_idx);
1820 }
1821 }
1822 }
1823 }
1824 }
1825 return resolved;
1826}
1827
1828uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1829 uint32_t line,
1830 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001831 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 SymbolContextList &sc_list) {
1833 const uint32_t prev_size = sc_list.GetSize();
1834 if (resolve_scope & eSymbolContextCompUnit) {
Pavel Labathad17e282019-06-13 11:22:47 +00001835 for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus;
1836 ++cu_idx) {
1837 CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get();
1838 if (!dc_cu)
1839 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840
Pavel Labathad17e282019-06-13 11:22:47 +00001841 const bool full_match = (bool)file_spec.GetDirectory();
1842 bool file_spec_matches_cu_file_spec =
1843 FileSpec::Equal(file_spec, *dc_cu, full_match);
1844 if (check_inlines || file_spec_matches_cu_file_spec) {
1845 SymbolContext sc(m_obj_file->GetModule());
1846 sc.comp_unit = dc_cu;
1847 uint32_t file_idx = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848
Pavel Labathad17e282019-06-13 11:22:47 +00001849 // If we are looking for inline functions only and we don't find it
1850 // in the support files, we are done.
1851 if (check_inlines) {
1852 file_idx =
1853 sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true);
1854 if (file_idx == UINT32_MAX)
1855 continue;
1856 }
1857
1858 if (line != 0) {
1859 LineTable *line_table = sc.comp_unit->GetLineTable();
1860
1861 if (line_table != nullptr && line != 0) {
1862 // We will have already looked up the file index if we are
1863 // searching for inline entries.
1864 if (!check_inlines)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1866 1, file_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867
Pavel Labathad17e282019-06-13 11:22:47 +00001868 if (file_idx != UINT32_MAX) {
1869 uint32_t found_line;
1870 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1871 0, file_idx, line, false, &sc.line_entry);
1872 found_line = sc.line_entry.line;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873
Pavel Labathad17e282019-06-13 11:22:47 +00001874 while (line_idx != UINT32_MAX) {
1875 sc.function = nullptr;
1876 sc.block = nullptr;
1877 if (resolve_scope &
1878 (eSymbolContextFunction | eSymbolContextBlock)) {
1879 const lldb::addr_t file_vm_addr =
1880 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1881 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1882 DWARFDIE function_die =
1883 GetDWARFCompileUnit(dc_cu)->LookupAddress(file_vm_addr);
1884 DWARFDIE block_die;
1885 if (function_die) {
1886 sc.function =
1887 sc.comp_unit->FindFunctionByUID(function_die.GetID())
1888 .get();
1889 if (sc.function == nullptr)
1890 sc.function =
1891 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001892
Pavel Labathad17e282019-06-13 11:22:47 +00001893 if (sc.function && (resolve_scope & eSymbolContextBlock))
1894 block_die =
1895 function_die.LookupDeepestBlock(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896 }
1897
Pavel Labathad17e282019-06-13 11:22:47 +00001898 if (sc.function != nullptr) {
1899 Block &block = sc.function->GetBlock(true);
1900
1901 if (block_die)
1902 sc.block = block.FindBlockByID(block_die.GetID());
1903 else if (function_die)
1904 sc.block = block.FindBlockByID(function_die.GetID());
1905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 }
1907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001908
Pavel Labathad17e282019-06-13 11:22:47 +00001909 sc_list.Append(sc);
1910 line_idx = line_table->FindLineEntryIndexByFileIndex(
1911 line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1912 }
1913 }
1914 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1915 // only append the context if we aren't looking for inline call
1916 // sites by file and line and if the file spec matches that of
1917 // the compile unit
1918 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919 }
Pavel Labathad17e282019-06-13 11:22:47 +00001920 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1921 // only append the context if we aren't looking for inline call
1922 // sites by file and line and if the file spec matches that of
1923 // the compile unit
1924 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925 }
Pavel Labathad17e282019-06-13 11:22:47 +00001926
1927 if (!check_inlines)
1928 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 }
1930 }
1931 }
1932 return sc_list.GetSize() - prev_size;
1933}
1934
Jim Ingham7fca8c02017-04-28 00:51:06 +00001935void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001936 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001937 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938}
1939
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001940std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1941 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1942 if (module_sp)
1943 return module_sp->GetMutex();
1944 return GetObjectFile()->GetModule()->GetMutex();
1945}
1946
Kate Stoneb9c1b512016-09-06 20:57:50 +00001947bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1948 const lldb_private::CompilerDeclContext *decl_ctx) {
1949 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001950 // Invalid namespace decl which means we aren't matching only things in
1951 // this symbol file, so return true to indicate it matches this symbol
1952 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 return true;
1954 }
1955
1956 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1957 TypeSystem *type_system = GetTypeSystemForLanguage(
1958 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1959 if (decl_ctx_type_system == type_system)
1960 return true; // The type systems match, return true
1961
1962 // The namespace AST was valid, and it does not match...
1963 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1964
1965 if (log)
1966 GetObjectFile()->GetModule()->LogMessage(
1967 log, "Valid namespace does not match symbol file");
1968
1969 return false;
1970}
1971
1972uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001973 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00001974 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1976
1977 if (log)
1978 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00001979 log,
1980 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
1981 "parent_decl_ctx=%p, max_matches=%u, variables)",
1982 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001983 max_matches);
1984
1985 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1986 return 0;
1987
1988 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00001989 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990 return 0;
1991
Pavel Labath34cda142018-05-31 09:46:26 +00001992 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993 const uint32_t original_size = variables.GetSize();
1994
Pavel Labathe1d18752018-06-07 10:04:44 +00001995 llvm::StringRef basename;
1996 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00001997 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00001998
1999 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2000 context, basename))
2001 basename = name.GetStringRef();
2002
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002004 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002005 const size_t num_die_matches = die_offsets.size();
2006 if (num_die_matches) {
2007 SymbolContext sc;
2008 sc.module_sp = m_obj_file->GetModule();
2009 assert(sc.module_sp);
2010
Pavel Labathe1d18752018-06-07 10:04:44 +00002011 // Loop invariant: Variables up to this index have been checked for context
2012 // matches.
2013 uint32_t pruned_idx = original_size;
2014
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 bool done = false;
2016 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2017 const DIERef &die_ref = die_offsets[i];
2018 DWARFDIE die = GetDIE(die_ref);
2019
2020 if (die) {
2021 switch (die.Tag()) {
2022 default:
2023 case DW_TAG_subprogram:
2024 case DW_TAG_inlined_subroutine:
2025 case DW_TAG_try_block:
2026 case DW_TAG_catch_block:
2027 break;
2028
2029 case DW_TAG_variable: {
Pavel Labathad17e282019-06-13 11:22:47 +00002030 auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2031 if (!dwarf_cu)
2032 continue;
2033 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034
2035 if (parent_decl_ctx) {
2036 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2037 if (dwarf_ast) {
2038 CompilerDeclContext actual_parent_decl_ctx =
2039 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2040 if (!actual_parent_decl_ctx ||
2041 actual_parent_decl_ctx != *parent_decl_ctx)
2042 continue;
2043 }
2044 }
2045
2046 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2047 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002048 while (pruned_idx < variables.GetSize()) {
2049 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002050 if (name_is_mangled ||
2051 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002052 ++pruned_idx;
2053 else
2054 variables.RemoveVariableAtIndex(pruned_idx);
2055 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002056
2057 if (variables.GetSize() - original_size >= max_matches)
2058 done = true;
2059 } break;
2060 }
2061 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002062 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002063 }
2064 }
2065 }
2066
2067 // Return the number of variable that were appended to the list
2068 const uint32_t num_matches = variables.GetSize() - original_size;
2069 if (log && num_matches > 0) {
2070 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002071 log,
2072 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2073 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2074 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002075 max_matches, num_matches);
2076 }
2077 return num_matches;
2078}
2079
2080uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002081 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002082 VariableList &variables) {
2083 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2084
2085 if (log) {
2086 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002087 log,
2088 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2089 "max_matches=%u, variables)",
2090 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 }
2092
2093 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002094 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002095 return 0;
2096
Pavel Labath34cda142018-05-31 09:46:26 +00002097 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 const uint32_t original_size = variables.GetSize();
2099
2100 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002101 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002102
2103 SymbolContext sc;
2104 sc.module_sp = m_obj_file->GetModule();
2105 assert(sc.module_sp);
2106
2107 const size_t num_matches = die_offsets.size();
2108 if (num_matches) {
2109 for (size_t i = 0; i < num_matches; ++i) {
2110 const DIERef &die_ref = die_offsets[i];
2111 DWARFDIE die = GetDIE(die_ref);
2112
2113 if (die) {
Pavel Labathad17e282019-06-13 11:22:47 +00002114 DWARFCompileUnit *dwarf_cu =
2115 llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2116 if (!dwarf_cu)
2117 continue;
2118 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119
2120 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2121
2122 if (variables.GetSize() - original_size >= max_matches)
2123 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002124 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00002125 m_index->ReportInvalidDIERef(die_ref, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 }
2127 }
2128
2129 // Return the number of variable that were appended to the list
2130 return variables.GetSize() - original_size;
2131}
2132
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2134 bool include_inlines,
2135 SymbolContextList &sc_list) {
2136 SymbolContext sc;
2137
2138 if (!orig_die)
2139 return false;
2140
2141 // If we were passed a die that is not a function, just return false...
2142 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2143 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2144 return false;
2145
2146 DWARFDIE die = orig_die;
2147 DWARFDIE inlined_die;
2148 if (die.Tag() == DW_TAG_inlined_subroutine) {
2149 inlined_die = die;
2150
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002151 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 die = die.GetParent();
2153
2154 if (die) {
2155 if (die.Tag() == DW_TAG_subprogram)
2156 break;
2157 } else
2158 break;
2159 }
2160 }
2161 assert(die && die.Tag() == DW_TAG_subprogram);
2162 if (GetFunction(die, sc)) {
2163 Address addr;
2164 // Parse all blocks if needed
2165 if (inlined_die) {
2166 Block &function_block = sc.function->GetBlock(true);
2167 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002168 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002170 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171 addr.Clear();
2172 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002173 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174 addr = sc.function->GetAddressRange().GetBaseAddress();
2175 }
2176
2177 if (addr.IsValid()) {
2178 sc_list.Append(sc);
2179 return true;
2180 }
2181 }
2182
2183 return false;
2184}
2185
Kate Stoneb9c1b512016-09-06 20:57:50 +00002186bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2187 const DWARFDIE &die) {
2188 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002189 // parent decl context isn't valid, we aren't trying to look for any
2190 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002191 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2192 return true;
2193
2194 if (die) {
2195 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2196 if (dwarf_ast) {
2197 CompilerDeclContext actual_decl_ctx =
2198 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2199 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002200 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 }
2202 }
2203 return false;
2204}
2205
Zachary Turner117b1fa2018-10-25 20:45:40 +00002206uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002207 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002208 FunctionNameType name_type_mask, bool include_inlines, bool append,
2209 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002210 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2211 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 name.AsCString());
2213
2214 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002215 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2217
2218 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2219
2220 if (log) {
2221 GetObjectFile()->GetModule()->LogMessage(
2222 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2223 "name_type_mask=0x%x, append=%u, sc_list)",
2224 name.GetCString(), name_type_mask, append);
2225 }
2226
2227 // If we aren't appending the results to this list, then clear the list
2228 if (!append)
2229 sc_list.Clear();
2230
2231 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2232 return 0;
2233
2234 // If name is empty then we won't find anything.
2235 if (name.IsEmpty())
2236 return 0;
2237
Adrian Prantl05097242018-04-30 16:49:04 +00002238 // Remember how many sc_list are in the list before we search in case we are
2239 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241 const uint32_t original_size = sc_list.GetSize();
2242
Pavel Labath5af11ab2018-06-05 10:33:56 +00002243 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2244 DIEArray offsets;
2245 CompilerDeclContext empty_decl_ctx;
2246 if (!parent_decl_ctx)
2247 parent_decl_ctx = &empty_decl_ctx;
2248
2249 std::vector<DWARFDIE> dies;
Pavel Labath3b926982019-06-21 07:56:50 +00002250 m_index->GetFunctions(name, *this, *parent_decl_ctx, name_type_mask, dies);
Pavel Labath5af11ab2018-06-05 10:33:56 +00002251 for (const DWARFDIE &die: dies) {
2252 if (resolved_dies.insert(die.GetDIE()).second)
2253 ResolveFunction(die, include_inlines, sc_list);
2254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002255
2256 // Return the number of variable that were appended to the list
2257 const uint32_t num_matches = sc_list.GetSize() - original_size;
2258
2259 if (log && num_matches > 0) {
2260 GetObjectFile()->GetModule()->LogMessage(
2261 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2262 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2263 "%u",
2264 name.GetCString(), name_type_mask, include_inlines, append,
2265 num_matches);
2266 }
2267 return num_matches;
2268}
2269
2270uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2271 bool include_inlines, bool append,
2272 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002273 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2274 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002275 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002276
2277 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2278
2279 if (log) {
2280 GetObjectFile()->GetModule()->LogMessage(
2281 log,
2282 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002283 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002284 }
2285
2286 // If we aren't appending the results to this list, then clear the list
2287 if (!append)
2288 sc_list.Clear();
2289
Pavel Labathb13f0332018-05-21 14:12:52 +00002290 DWARFDebugInfo *info = DebugInfo();
2291 if (!info)
2292 return 0;
2293
Adrian Prantl05097242018-04-30 16:49:04 +00002294 // Remember how many sc_list are in the list before we search in case we are
2295 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002296 uint32_t original_size = sc_list.GetSize();
2297
Pavel Labath6de9c792018-06-05 12:13:22 +00002298 DIEArray offsets;
2299 m_index->GetFunctions(regex, offsets);
2300
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002301 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2302 for (DIERef ref : offsets) {
2303 DWARFDIE die = info->GetDIE(ref);
2304 if (!die) {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002305 m_index->ReportInvalidDIERef(ref, regex.GetText());
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002306 continue;
2307 }
2308 if (resolved_dies.insert(die.GetDIE()).second)
2309 ResolveFunction(die, include_inlines, sc_list);
2310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311
2312 // Return the number of variable that were appended to the list
2313 return sc_list.GetSize() - original_size;
2314}
2315
2316void SymbolFileDWARF::GetMangledNamesForFunction(
2317 const std::string &scope_qualified_name,
2318 std::vector<ConstString> &mangled_names) {
2319 DWARFDebugInfo *info = DebugInfo();
2320 uint32_t num_comp_units = 0;
2321 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002322 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002323
2324 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002325 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326 if (cu == nullptr)
2327 continue;
2328
2329 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2330 if (dwo)
2331 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2332 }
2333
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002334 for (lldb::user_id_t uid :
2335 m_function_scope_qualified_name_map.lookup(scope_qualified_name)) {
2336 DWARFDIE die = GetDIE(uid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 mangled_names.push_back(ConstString(die.GetMangledName()));
2338 }
2339}
2340
2341uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002342 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002343 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002344 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2345 TypeMap &types) {
2346 // If we aren't appending the results to this list, then clear the list
2347 if (!append)
2348 types.Clear();
2349
2350 // Make sure we haven't already searched this SymbolFile before...
2351 if (searched_symbol_files.count(this))
2352 return 0;
2353 else
2354 searched_symbol_files.insert(this);
2355
2356 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002357 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 return 0;
2359
2360 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2361
2362 if (log) {
2363 if (parent_decl_ctx)
2364 GetObjectFile()->GetModule()->LogMessage(
2365 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2366 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2367 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2368 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2369 else
2370 GetObjectFile()->GetModule()->LogMessage(
2371 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2372 "NULL, append=%u, max_matches=%u, type_list)",
2373 name.GetCString(), append, max_matches);
2374 }
2375
2376 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2377 return 0;
2378
2379 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002380 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002381 const size_t num_die_matches = die_offsets.size();
2382
2383 if (num_die_matches) {
2384 const uint32_t initial_types_size = types.GetSize();
2385 for (size_t i = 0; i < num_die_matches; ++i) {
2386 const DIERef &die_ref = die_offsets[i];
2387 DWARFDIE die = GetDIE(die_ref);
2388
2389 if (die) {
2390 if (!DIEInDeclContext(parent_decl_ctx, die))
2391 continue; // The containing decl contexts don't match
2392
2393 Type *matching_type = ResolveType(die, true, true);
2394 if (matching_type) {
2395 // We found a type pointer, now find the shared pointer form our type
2396 // list
2397 types.InsertUnique(matching_type->shared_from_this());
2398 if (types.GetSize() >= max_matches)
2399 break;
2400 }
2401 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002402 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403 }
2404 }
2405 const uint32_t num_matches = types.GetSize() - initial_types_size;
2406 if (log && num_matches) {
2407 if (parent_decl_ctx) {
2408 GetObjectFile()->GetModule()->LogMessage(
2409 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2410 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2411 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2412 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2413 num_matches);
2414 } else {
2415 GetObjectFile()->GetModule()->LogMessage(
2416 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2417 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2418 name.GetCString(), append, max_matches, num_matches);
2419 }
2420 }
2421 return num_matches;
2422 } else {
2423 UpdateExternalModuleListIfNeeded();
2424
2425 for (const auto &pair : m_external_type_modules) {
2426 ModuleSP external_module_sp = pair.second;
2427 if (external_module_sp) {
2428 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2429 if (sym_vendor) {
2430 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002431 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2432 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002433 if (num_external_matches)
2434 return num_external_matches;
2435 }
2436 }
2437 }
2438 }
2439
2440 return 0;
2441}
2442
2443size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2444 bool append, TypeMap &types) {
2445 if (!append)
2446 types.Clear();
2447
2448 if (context.empty())
2449 return 0;
2450
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451 ConstString name = context.back().name;
2452
2453 if (!name)
2454 return 0;
2455
Pavel Labathb13f0332018-05-21 14:12:52 +00002456 DIEArray die_offsets;
2457 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002458 const size_t num_die_matches = die_offsets.size();
2459
2460 if (num_die_matches) {
2461 size_t num_matches = 0;
2462 for (size_t i = 0; i < num_die_matches; ++i) {
2463 const DIERef &die_ref = die_offsets[i];
2464 DWARFDIE die = GetDIE(die_ref);
2465
2466 if (die) {
2467 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002468 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002469 if (die_context != context)
2470 continue;
2471
2472 Type *matching_type = ResolveType(die, true, true);
2473 if (matching_type) {
2474 // We found a type pointer, now find the shared pointer form our type
2475 // list
2476 types.InsertUnique(matching_type->shared_from_this());
2477 ++num_matches;
2478 }
2479 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002480 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002481 }
2482 }
2483 return num_matches;
2484 }
2485 return 0;
2486}
2487
2488CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002489SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002490 const CompilerDeclContext *parent_decl_ctx) {
2491 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2492
2493 if (log) {
2494 GetObjectFile()->GetModule()->LogMessage(
2495 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2496 name.GetCString());
2497 }
2498
2499 CompilerDeclContext namespace_decl_ctx;
2500
2501 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2502 return namespace_decl_ctx;
2503
2504 DWARFDebugInfo *info = DebugInfo();
2505 if (info) {
2506 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002507 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002508 const size_t num_matches = die_offsets.size();
2509 if (num_matches) {
2510 for (size_t i = 0; i < num_matches; ++i) {
2511 const DIERef &die_ref = die_offsets[i];
2512 DWARFDIE die = GetDIE(die_ref);
2513
2514 if (die) {
2515 if (!DIEInDeclContext(parent_decl_ctx, die))
2516 continue; // The containing decl contexts don't match
2517
2518 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2519 if (dwarf_ast) {
2520 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2521 if (namespace_decl_ctx)
2522 break;
2523 }
2524 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002525 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002526 }
2527 }
2528 }
2529 }
2530 if (log && namespace_decl_ctx) {
2531 GetObjectFile()->GetModule()->LogMessage(
2532 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2533 "CompilerDeclContext(%p/%p) \"%s\"",
2534 name.GetCString(),
2535 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2536 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2537 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2538 }
2539
2540 return namespace_decl_ctx;
2541}
2542
2543TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2544 bool resolve_function_context) {
2545 TypeSP type_sp;
2546 if (die) {
2547 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002548 if (type_ptr == nullptr) {
Pavel Labathad17e282019-06-13 11:22:47 +00002549 SymbolContextScope *scope;
2550 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()))
2551 scope = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2552 else
2553 scope = GetObjectFile()->GetModule().get();
2554 assert(scope);
2555 SymbolContext sc(scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002556 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2557 while (parent_die != nullptr) {
2558 if (parent_die->Tag() == DW_TAG_subprogram)
2559 break;
2560 parent_die = parent_die->GetParent();
2561 }
2562 SymbolContext sc_backup = sc;
2563 if (resolve_function_context && parent_die != nullptr &&
2564 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2565 sc = sc_backup;
2566
Konrad Kleine248a1302019-05-23 11:14:47 +00002567 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2569 // Grab the existing type from the master types lists
2570 type_sp = type_ptr->shared_from_this();
2571 }
2572 }
2573 return type_sp;
2574}
2575
2576DWARFDIE
2577SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2578 if (orig_die) {
2579 DWARFDIE die = orig_die;
2580
2581 while (die) {
2582 // If this is the original DIE that we are searching for a declaration
2583 // for, then don't look in the cache as we don't want our own decl
2584 // context to be our decl context...
2585 if (orig_die != die) {
2586 switch (die.Tag()) {
2587 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002588 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002589 case DW_TAG_namespace:
2590 case DW_TAG_structure_type:
2591 case DW_TAG_union_type:
2592 case DW_TAG_class_type:
2593 case DW_TAG_lexical_block:
2594 case DW_TAG_subprogram:
2595 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002596 case DW_TAG_inlined_subroutine: {
2597 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2598 if (abs_die) {
2599 return abs_die;
2600 }
2601 break;
2602 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002603 default:
2604 break;
2605 }
2606 }
2607
2608 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2609 if (spec_die) {
2610 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2611 if (decl_ctx_die)
2612 return decl_ctx_die;
2613 }
2614
2615 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2616 if (abs_die) {
2617 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2618 if (decl_ctx_die)
2619 return decl_ctx_die;
2620 }
2621
2622 die = die.GetParent();
2623 }
2624 }
2625 return DWARFDIE();
2626}
2627
2628Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002629SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002630 Symbol *objc_class_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002631 if (m_obj_file) {
2632 Symtab *symtab = m_obj_file->GetSymtab();
2633 if (symtab) {
2634 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2635 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2636 Symtab::eVisibilityAny);
2637 }
2638 }
2639 return objc_class_symbol;
2640}
2641
2642// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002643// they don't then we can end up looking through all class types for a complete
2644// type and never find the full definition. We need to know if this attribute
2645// is supported, so we determine this here and cache th result. We also need to
2646// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002647// DWARF file
2648// if we are doing darwin DWARF in .o file debugging.
2649bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002650 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002651 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2652 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2653 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2654 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2655 else {
2656 DWARFDebugInfo *debug_info = DebugInfo();
2657 const uint32_t num_compile_units = GetNumCompileUnits();
2658 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002659 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002660 if (dwarf_cu != cu &&
2661 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2662 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2663 break;
2664 }
2665 }
2666 }
2667 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2668 GetDebugMapSymfile())
2669 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2670 }
2671 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002672}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002673
2674// This function can be used when a DIE is found that is a forward declaration
2675// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002676TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002677 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002678 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002679
Kate Stoneb9c1b512016-09-06 20:57:50 +00002680 TypeSP type_sp;
2681
2682 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2683 return type_sp;
2684
2685 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002686 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687
2688 const size_t num_matches = die_offsets.size();
2689
2690 if (num_matches) {
2691 for (size_t i = 0; i < num_matches; ++i) {
2692 const DIERef &die_ref = die_offsets[i];
2693 DWARFDIE type_die = GetDIE(die_ref);
2694
2695 if (type_die) {
2696 bool try_resolving_type = false;
2697
Adrian Prantl05097242018-04-30 16:49:04 +00002698 // Don't try and resolve the DIE we are looking for with the DIE
2699 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 if (type_die != die) {
2701 switch (type_die.Tag()) {
2702 case DW_TAG_class_type:
2703 case DW_TAG_structure_type:
2704 try_resolving_type = true;
2705 break;
2706 default:
2707 break;
2708 }
2709 }
2710
2711 if (try_resolving_type) {
2712 if (must_be_implementation &&
2713 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2714 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2715 DW_AT_APPLE_objc_complete_type, 0);
2716
2717 if (try_resolving_type) {
2718 Type *resolved_type = ResolveType(type_die, false, true);
2719 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2720 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2721 " (cu 0x%8.8" PRIx64 ")\n",
2722 die.GetID(),
2723 m_obj_file->GetFileSpec().GetFilename().AsCString(
2724 "<Unknown>"),
2725 type_die.GetID(), type_cu->GetID());
2726
2727 if (die)
2728 GetDIEToType()[die.GetDIE()] = resolved_type;
2729 type_sp = resolved_type->shared_from_this();
2730 break;
2731 }
2732 }
2733 }
2734 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002735 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002736 }
2737 }
2738 }
2739 return type_sp;
2740}
Greg Claytona8022fa2012-04-24 21:22:41 +00002741
Adrian Prantl05097242018-04-30 16:49:04 +00002742// This function helps to ensure that the declaration contexts match for two
2743// different DIEs. Often times debug information will refer to a forward
2744// declaration of a type (the equivalent of "struct my_struct;". There will
2745// often be a declaration of that type elsewhere that has the full definition.
2746// When we go looking for the full type "my_struct", we will find one or more
2747// matches in the accelerator tables and we will then need to make sure the
2748// type was in the same declaration context as the original DIE. This function
2749// can efficiently compare two DIEs and will return true when the declaration
2750// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002751bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2752 const DWARFDIE &die2) {
2753 if (die1 == die2)
2754 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002755
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002756 std::vector<DWARFDIE> decl_ctx_1;
2757 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002758 // The declaration DIE stack is a stack of the declaration context DIEs all
2759 // the way back to the compile unit. If a type "T" is declared inside a class
2760 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2761 // namespace "lldb", and the namespace is in a compile unit, there will be a
2762 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002763 //
2764 // [0] DW_TAG_class_type for "B"
2765 // [1] DW_TAG_class_type for "A"
2766 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002767 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002768 //
Adrian Prantl05097242018-04-30 16:49:04 +00002769 // We grab both contexts and make sure that everything matches all the way
2770 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002771
2772 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002773 decl_ctx_1 = die1.GetDeclContextDIEs();
2774 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002775 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002776 const size_t count1 = decl_ctx_1.size();
2777 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002778 if (count1 != count2)
2779 return false;
2780
Adrian Prantl05097242018-04-30 16:49:04 +00002781 // Make sure the DW_TAG values match all the way back up the compile unit. If
2782 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002783 DWARFDIE decl_ctx_die1;
2784 DWARFDIE decl_ctx_die2;
2785 size_t i;
2786 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002787 decl_ctx_die1 = decl_ctx_1[i];
2788 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002789 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2790 return false;
2791 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002792#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002793
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002795 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2796 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2797 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002798 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002799 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2800 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002801
Greg Clayton890ff562012-02-02 05:48:16 +00002802#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002803 // Always skip the compile unit when comparing by only iterating up to "count
2804 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002806 decl_ctx_die1 = decl_ctx_1[i];
2807 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 const char *name1 = decl_ctx_die1.GetName();
2809 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002810 // If the string was from a DW_FORM_strp, then the pointer will often be
2811 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002812 if (name1 == name2)
2813 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002814
Adrian Prantl05097242018-04-30 16:49:04 +00002815 // Name pointers are not equal, so only compare the strings if both are not
2816 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817 if (name1 && name2) {
2818 // If the strings don't compare, we are done...
2819 if (strcmp(name1, name2) != 0)
2820 return false;
2821 } else {
2822 // One name was NULL while the other wasn't
2823 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825 }
Adrian Prantl05097242018-04-30 16:49:04 +00002826 // We made it through all of the checks and the declaration contexts are
2827 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002828 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002829}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002830
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2832 const DWARFDeclContext &dwarf_decl_ctx) {
2833 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002834
Kate Stoneb9c1b512016-09-06 20:57:50 +00002835 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2836 if (dwarf_decl_ctx_count > 0) {
2837 const ConstString type_name(dwarf_decl_ctx[0].name);
2838 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002839
Kate Stoneb9c1b512016-09-06 20:57:50 +00002840 if (type_name) {
2841 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2842 DWARF_LOG_LOOKUPS));
2843 if (log) {
2844 GetObjectFile()->GetModule()->LogMessage(
2845 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2846 "s, qualified-name='%s')",
2847 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2848 dwarf_decl_ctx.GetQualifiedName());
2849 }
2850
2851 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002852 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 const size_t num_matches = die_offsets.size();
2854
Adrian Prantl05097242018-04-30 16:49:04 +00002855 // Get the type system that we are looking to find a type for. We will
2856 // use this to ensure any matches we find are in a language that this
2857 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2859 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2860 ? nullptr
2861 : GetTypeSystemForLanguage(language);
2862
2863 if (num_matches) {
2864 for (size_t i = 0; i < num_matches; ++i) {
2865 const DIERef &die_ref = die_offsets[i];
2866 DWARFDIE type_die = GetDIE(die_ref);
2867
2868 if (type_die) {
2869 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002870 // looking for. We don't want to find a "Foo" type from Java if we
2871 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002872 if (type_system &&
2873 !type_system->SupportsLanguage(type_die.GetLanguage()))
2874 continue;
2875 bool try_resolving_type = false;
2876
2877 // Don't try and resolve the DIE we are looking for with the DIE
2878 // itself!
2879 const dw_tag_t type_tag = type_die.Tag();
2880 // Make sure the tags match
2881 if (type_tag == tag) {
2882 // The tags match, lets try resolving this type
2883 try_resolving_type = true;
2884 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002885 // The tags don't match, but we need to watch our for a forward
2886 // declaration for a struct and ("struct foo") ends up being a
2887 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002888 switch (type_tag) {
2889 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002890 // We had a "class foo", see if we ended up with a "struct foo
2891 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002892 try_resolving_type = (tag == DW_TAG_structure_type);
2893 break;
2894 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002895 // We had a "struct foo", see if we ended up with a "class foo
2896 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002897 try_resolving_type = (tag == DW_TAG_class_type);
2898 break;
2899 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002900 // Tags don't match, don't event try to resolve using this type
2901 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002902 break;
2903 }
2904 }
2905
2906 if (try_resolving_type) {
2907 DWARFDeclContext type_dwarf_decl_ctx;
2908 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2909
2910 if (log) {
2911 GetObjectFile()->GetModule()->LogMessage(
2912 log, "SymbolFileDWARF::"
2913 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2914 "qualified-name='%s') trying die=0x%8.8x (%s)",
2915 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2916 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2917 type_dwarf_decl_ctx.GetQualifiedName());
2918 }
2919
2920 // Make sure the decl contexts match all the way up
2921 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2922 Type *resolved_type = ResolveType(type_die, false);
2923 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2924 type_sp = resolved_type->shared_from_this();
2925 break;
2926 }
2927 }
2928 } else {
2929 if (log) {
2930 std::string qualified_name;
2931 type_die.GetQualifiedName(qualified_name);
2932 GetObjectFile()->GetModule()->LogMessage(
2933 log, "SymbolFileDWARF::"
2934 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2935 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2936 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2937 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2938 qualified_name.c_str());
2939 }
2940 }
2941 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002942 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002943 }
2944 }
2945 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002946 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002947 }
2948 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002949}
2950
Kate Stoneb9c1b512016-09-06 20:57:50 +00002951TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2952 bool *type_is_new_ptr) {
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002953 if (!die)
2954 return {};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002955
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002956 TypeSystem *type_system =
2957 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
2958 if (!type_system)
2959 return {};
Greg Clayton6071e6f2015-08-26 22:57:51 +00002960
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002961 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2962 if (!dwarf_ast)
2963 return {};
Siva Chandra9293fc42016-01-07 23:32:34 +00002964
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002965 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
2966 TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
2967 if (type_sp) {
Pavel Labathf46e8972019-07-25 08:22:05 +00002968 GetTypeList().Insert(type_sp);
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002969
2970 if (die.Tag() == DW_TAG_subprogram) {
2971 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2972 .GetScopeQualifiedName()
2973 .AsCString(""));
2974 if (scope_qualified_name.size()) {
2975 m_function_scope_qualified_name_map[scope_qualified_name].insert(
2976 die.GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002978 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002979 }
2980
2981 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002982}
2983
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
2985 const DWARFDIE &orig_die,
2986 bool parse_siblings, bool parse_children) {
2987 size_t types_added = 0;
2988 DWARFDIE die = orig_die;
2989 while (die) {
2990 bool type_is_new = false;
2991 if (ParseType(sc, die, &type_is_new).get()) {
2992 if (type_is_new)
2993 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002995
2996 if (parse_children && die.HasChildren()) {
2997 if (die.Tag() == DW_TAG_subprogram) {
2998 SymbolContext child_sc(sc);
2999 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3000 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3001 } else
3002 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3003 }
3004
3005 if (parse_siblings)
3006 die = die.GetSibling();
3007 else
3008 die.Clear();
3009 }
3010 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011}
3012
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003013size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003014 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003015 CompileUnit *comp_unit = func.GetCompileUnit();
3016 lldbassert(comp_unit);
3017
3018 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3019 if (!dwarf_cu)
3020 return 0;
3021
Kate Stoneb9c1b512016-09-06 20:57:50 +00003022 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003023 const dw_offset_t function_die_offset = func.GetID();
3024 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3025 if (function_die) {
3026 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3027 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003029
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003031}
3032
Zachary Turner863f8c12019-01-11 18:03:20 +00003033size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003034 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003035 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003036 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003038 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3039 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3040 SymbolContext sc;
3041 sc.comp_unit = &comp_unit;
3042 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045
Kate Stoneb9c1b512016-09-06 20:57:50 +00003046 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003047}
3048
Kate Stoneb9c1b512016-09-06 20:57:50 +00003049size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003050 ASSERT_MODULE_LOCK(this);
Konrad Kleine248a1302019-05-23 11:14:47 +00003051 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003053 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003054 return 0;
3055
3056 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003057 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003058
3059 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3060 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3061 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3062 const size_t num_variables = ParseVariables(
3063 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3064
3065 // Let all blocks know they have parse all their variables
3066 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3067 return num_variables;
3068 }
3069 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003070 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071
Konrad Kleine248a1302019-05-23 11:14:47 +00003072 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003073 return 0;
3074
3075 uint32_t vars_added = 0;
3076 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3077
Konrad Kleine248a1302019-05-23 11:14:47 +00003078 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003079 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003080 sc.comp_unit->SetVariableList(variables);
3081
3082 DIEArray die_offsets;
Pavel Labath3b926982019-06-21 07:56:50 +00003083 m_index->GetGlobalVariables(dwarf_cu->GetNonSkeletonUnit(),
3084 die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003085 const size_t num_matches = die_offsets.size();
3086 if (num_matches) {
3087 for (size_t i = 0; i < num_matches; ++i) {
3088 const DIERef &die_ref = die_offsets[i];
3089 DWARFDIE die = GetDIE(die_ref);
3090 if (die) {
3091 VariableSP var_sp(
3092 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3093 if (var_sp) {
3094 variables->AddVariableIfUnique(var_sp);
3095 ++vars_added;
3096 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003097 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00003098 m_index->ReportInvalidDIERef(die_ref, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003099 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003101 }
3102 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003104 }
3105 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003106}
3107
Kate Stoneb9c1b512016-09-06 20:57:50 +00003108VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3109 const DWARFDIE &die,
3110 const lldb::addr_t func_low_pc) {
3111 if (die.GetDWARF() != this)
3112 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003113
Kate Stoneb9c1b512016-09-06 20:57:50 +00003114 VariableSP var_sp;
3115 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003116 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003117
Kate Stoneb9c1b512016-09-06 20:57:50 +00003118 var_sp = GetDIEToVariable()[die.GetDIE()];
3119 if (var_sp)
3120 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003121
Kate Stoneb9c1b512016-09-06 20:57:50 +00003122 const dw_tag_t tag = die.Tag();
3123 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003124
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3126 (tag == DW_TAG_formal_parameter && sc.function)) {
3127 DWARFAttributes attributes;
3128 const size_t num_attributes = die.GetAttributes(attributes);
3129 DWARFDIE spec_die;
3130 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003131 const char *name = nullptr;
3132 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003133 Declaration decl;
3134 uint32_t i;
3135 DWARFFormValue type_die_form;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003136 DWARFExpression location;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003137 bool is_external = false;
3138 bool is_artificial = false;
3139 bool location_is_const_value_data = false;
3140 bool has_explicit_location = false;
3141 DWARFFormValue const_value;
3142 Variable::RangeList scope_ranges;
3143 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003144
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145 for (i = 0; i < num_attributes; ++i) {
3146 dw_attr_t attr = attributes.AttributeAtIndex(i);
3147 DWARFFormValue form_value;
3148
3149 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3150 switch (attr) {
3151 case DW_AT_decl_file:
3152 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3153 form_value.Unsigned()));
3154 break;
3155 case DW_AT_decl_line:
3156 decl.SetLine(form_value.Unsigned());
3157 break;
3158 case DW_AT_decl_column:
3159 decl.SetColumn(form_value.Unsigned());
3160 break;
3161 case DW_AT_name:
3162 name = form_value.AsCString();
3163 break;
3164 case DW_AT_linkage_name:
3165 case DW_AT_MIPS_linkage_name:
3166 mangled = form_value.AsCString();
3167 break;
3168 case DW_AT_type:
3169 type_die_form = form_value;
3170 break;
3171 case DW_AT_external:
3172 is_external = form_value.Boolean();
3173 break;
3174 case DW_AT_const_value:
3175 // If we have already found a DW_AT_location attribute, ignore this
3176 // attribute.
3177 if (!has_explicit_location) {
3178 location_is_const_value_data = true;
3179 // The constant value will be either a block, a data value or a
3180 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003181 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003182 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3183 // Retrieve the value as a block expression.
3184 uint32_t block_offset =
3185 form_value.BlockData() - debug_info_data.GetDataStart();
3186 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003187 location = DWARFExpression(module, debug_info_data, die.GetCU(),
3188 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003189 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3190 // Retrieve the value as a data expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003191 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003192 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003193 location =
3194 DWARFExpression(module, debug_info_data, die.GetCU(),
3195 data_offset, *data_length);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003196 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197 const uint8_t *data_pointer = form_value.BlockData();
3198 if (data_pointer) {
3199 form_value.Unsigned();
3200 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3201 // we need to get the byte size of the type later after we
3202 // create the variable
3203 const_value = form_value;
3204 }
Greg Clayton6aad81c2019-05-24 22:08:50 +00003205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206 } else {
3207 // Retrieve the value as a string expression.
3208 if (form_value.Form() == DW_FORM_strp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003209 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003210 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003211 location =
3212 DWARFExpression(module, debug_info_data, die.GetCU(),
3213 data_offset, *data_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003214 } else {
3215 const char *str = form_value.AsCString();
3216 uint32_t string_offset =
3217 str - (const char *)debug_info_data.GetDataStart();
3218 uint32_t string_length = strlen(str) + 1;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003219 location =
3220 DWARFExpression(module, debug_info_data, die.GetCU(),
3221 string_offset, string_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222 }
3223 }
3224 }
3225 break;
3226 case DW_AT_location: {
3227 location_is_const_value_data = false;
3228 has_explicit_location = true;
3229 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003230 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003231
3232 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003233 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003235 location = DWARFExpression(module, data, die.GetCU(),
3236 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003237 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003238 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003239 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3240
3241 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003242 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003243 if (loc_list_length > 0) {
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003244 location = DWARFExpression(module, debug_loc_data, die.GetCU(),
3245 debug_loc_offset, loc_list_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003246 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3247 location.SetLocationListSlide(
3248 func_low_pc -
3249 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3250 }
3251 }
3252 } break;
3253 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003254 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003255 break;
Pavel Labath78cfe1e2019-05-29 09:22:36 +00003256 case DW_AT_start_scope:
3257 // TODO: Implement this.
3258 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003259 case DW_AT_artificial:
3260 is_artificial = form_value.Boolean();
3261 break;
3262 case DW_AT_accessibility:
3263 break; // accessibility =
3264 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3265 case DW_AT_declaration:
3266 case DW_AT_description:
3267 case DW_AT_endianity:
3268 case DW_AT_segment:
3269 case DW_AT_visibility:
3270 default:
3271 case DW_AT_abstract_origin:
3272 case DW_AT_sibling:
3273 break;
3274 }
3275 }
3276 }
3277
3278 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3279 const dw_tag_t parent_tag = die.GetParent().Tag();
3280 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003281 (parent_tag == DW_TAG_compile_unit ||
3282 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003283 (parent_context_die.Tag() == DW_TAG_class_type ||
3284 parent_context_die.Tag() == DW_TAG_structure_type);
3285
3286 ValueType scope = eValueTypeInvalid;
3287
3288 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003289 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290
Sam McCall30621392016-11-22 11:40:25 +00003291 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003292 if (!mangled) {
3293 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003294 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3295 // of global variables with commands like "frame var j". For
3296 // example, if j were an int variable holding a value 4 and
3297 // declared in a namespace B which in turn is contained in a
3298 // namespace A, the command "frame var j" returns
3299 // "(int) A::B::j = 4".
3300 // If the compiler does not emit a linkage name, we should be
3301 // able to generate a fully qualified name from the
3302 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003303 if ((parent_tag == DW_TAG_compile_unit ||
3304 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003305 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3306 DWARFDeclContext decl_ctx;
3307
3308 die.GetDWARFDeclContext(decl_ctx);
3309 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3310 }
3311 }
3312
Kate Stoneb9c1b512016-09-06 20:57:50 +00003313 if (tag == DW_TAG_formal_parameter)
3314 scope = eValueTypeVariableArgument;
3315 else {
Sam McCall30621392016-11-22 11:40:25 +00003316 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3317 // or static variable, so we have to do a little digging:
3318 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3319 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3320 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3321 // Clang likes to combine small global variables into the same symbol
3322 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3323 // so we need to look through the whole expression.
3324 bool is_static_lifetime =
3325 has_explicit_mangled ||
3326 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 // Check if the location has a DW_OP_addr with any address value...
3328 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3329 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003330 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003331 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3332 if (op_error) {
3333 StreamString strm;
3334 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003335 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003336 GetObjectFile()->GetModule()->ReportError(
3337 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003338 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003339 }
Sam McCall30621392016-11-22 11:40:25 +00003340 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3341 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003342 }
3343 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003344 if (debug_map_symfile)
3345 // Set the module of the expression to the linked module
3346 // instead of the oject file so the relocated address can be
3347 // found there.
3348 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003349
Sam McCall30621392016-11-22 11:40:25 +00003350 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003351 if (is_external)
3352 scope = eValueTypeVariableGlobal;
3353 else
3354 scope = eValueTypeVariableStatic;
3355
3356 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003357 // When leaving the DWARF in the .o files on darwin, when we have a
3358 // global variable that wasn't initialized, the .o file might not
3359 // have allocated a virtual address for the global variable. In
3360 // this case it will have created a symbol for the global variable
3361 // that is undefined/data and external and the value will be the
3362 // byte size of the variable. When we do the address map in
3363 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3364 // do some magic here so we can get the correct address for our
3365 // global variable. The address for all of these entries will be
3366 // zero, and there will be an undefined symbol in this object file,
3367 // and the executable will have a matching symbol with a good
3368 // address. So here we dig up the correct address and replace it in
3369 // the location for the variable, and set the variable's symbol
3370 // context scope to be that of the main executable so the file
3371 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003372 bool linked_oso_file_addr = false;
3373 if (is_external && location_DW_OP_addr == 0) {
3374 // we have a possible uninitialized extern global
3375 ConstString const_name(mangled ? mangled : name);
3376 ObjectFile *debug_map_objfile =
3377 debug_map_symfile->GetObjectFile();
3378 if (debug_map_objfile) {
3379 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3380 if (debug_map_symtab) {
3381 Symbol *exe_symbol =
3382 debug_map_symtab->FindFirstSymbolWithNameAndType(
3383 const_name, eSymbolTypeData, Symtab::eDebugYes,
3384 Symtab::eVisibilityExtern);
3385 if (exe_symbol) {
3386 if (exe_symbol->ValueIsAddress()) {
3387 const addr_t exe_file_addr =
3388 exe_symbol->GetAddressRef().GetFileAddress();
3389 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3390 if (location.Update_DW_OP_addr(exe_file_addr)) {
3391 linked_oso_file_addr = true;
3392 symbol_context_scope = exe_symbol;
3393 }
3394 }
3395 }
3396 }
3397 }
3398 }
3399 }
3400
3401 if (!linked_oso_file_addr) {
3402 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003403 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003404 const lldb::addr_t exe_file_addr =
3405 debug_map_symfile->LinkOSOFileAddress(this,
3406 location_DW_OP_addr);
3407 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3408 // Update the file address for this variable
3409 location.Update_DW_OP_addr(exe_file_addr);
3410 } else {
3411 // Variable didn't make it into the final executable
3412 return var_sp;
3413 }
3414 }
3415 }
3416 } else {
3417 if (location_is_const_value_data)
3418 scope = eValueTypeVariableStatic;
3419 else {
3420 scope = eValueTypeVariableLocal;
3421 if (debug_map_symfile) {
3422 // We need to check for TLS addresses that we need to fixup
3423 if (location.ContainsThreadLocalStorage()) {
3424 location.LinkThreadLocalStorage(
3425 debug_map_symfile->GetObjectFile()->GetModule(),
3426 [this, debug_map_symfile](
3427 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3428 return debug_map_symfile->LinkOSOFileAddress(
3429 this, unlinked_file_addr);
3430 });
3431 scope = eValueTypeVariableThreadLocal;
3432 }
3433 }
3434 }
3435 }
3436 }
3437
Konrad Kleine248a1302019-05-23 11:14:47 +00003438 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003439 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003440 case DW_TAG_subprogram:
3441 case DW_TAG_inlined_subroutine:
3442 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003443 if (sc.function) {
3444 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3445 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003446 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003447 symbol_context_scope = sc.function;
3448 }
3449 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003450
Kate Stoneb9c1b512016-09-06 20:57:50 +00003451 default:
3452 symbol_context_scope = sc.comp_unit;
3453 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003454 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003455 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003456
Kate Stoneb9c1b512016-09-06 20:57:50 +00003457 if (symbol_context_scope) {
3458 SymbolFileTypeSP type_sp(
Pavel Labath67b45ac2019-06-19 07:32:39 +00003459 new SymbolFileType(*this, GetUID(type_die_form.Reference())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003460
3461 if (const_value.Form() && type_sp && type_sp->GetType())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003462 location.UpdateValue(const_value.Unsigned(),
3463 type_sp->GetType()->GetByteSize().getValueOr(0),
3464 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003465
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003466 var_sp = std::make_shared<Variable>(
3467 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3468 scope_ranges, &decl, location, is_external, is_artificial,
3469 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003470
3471 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3472 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003473 // Not ready to parse this variable yet. It might be a global or static
3474 // variable that is in a function scope and the function in the symbol
3475 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003476 return var_sp;
3477 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003478 }
Adrian Prantl05097242018-04-30 16:49:04 +00003479 // Cache var_sp even if NULL (the variable was just a specification or was
3480 // missing vital information to be able to be displayed in the debugger
3481 // (missing location due to optimization, etc)) so we don't re-parse this
3482 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003483 GetDIEToVariable()[die.GetDIE()] = var_sp;
3484 if (spec_die)
3485 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3486 }
3487 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003488}
3489
Kate Stoneb9c1b512016-09-06 20:57:50 +00003490DWARFDIE
3491SymbolFileDWARF::FindBlockContainingSpecification(
3492 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3493 // Give the concrete function die specified by "func_die_offset", find the
3494 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3495 // to "spec_block_die_offset"
3496 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3497 spec_block_die_offset);
3498}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003499
Kate Stoneb9c1b512016-09-06 20:57:50 +00003500DWARFDIE
3501SymbolFileDWARF::FindBlockContainingSpecification(
3502 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3503 if (die) {
3504 switch (die.Tag()) {
3505 case DW_TAG_subprogram:
3506 case DW_TAG_inlined_subroutine:
3507 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003508 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3509 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003510 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003511
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003512 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003513 spec_block_die_offset)
3514 return die;
3515 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003517
3518 // Give the concrete function die specified by "func_die_offset", find the
3519 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3520 // to "spec_block_die_offset"
3521 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3522 child_die = child_die.GetSibling()) {
3523 DWARFDIE result_die =
3524 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3525 if (result_die)
3526 return result_die;
3527 }
3528 }
3529
3530 return DWARFDIE();
3531}
3532
3533size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3534 const DWARFDIE &orig_die,
3535 const lldb::addr_t func_low_pc,
3536 bool parse_siblings, bool parse_children,
3537 VariableList *cc_variable_list) {
3538 if (!orig_die)
3539 return 0;
3540
3541 VariableListSP variable_list_sp;
3542
3543 size_t vars_added = 0;
3544 DWARFDIE die = orig_die;
3545 while (die) {
3546 dw_tag_t tag = die.Tag();
3547
3548 // Check to see if we have already parsed this variable or constant?
3549 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3550 if (var_sp) {
3551 if (cc_variable_list)
3552 cc_variable_list->AddVariableIfUnique(var_sp);
3553 } else {
3554 // We haven't already parsed it, lets do that now.
3555 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3556 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003557 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003558 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3559 dw_tag_t parent_tag = sc_parent_die.Tag();
3560 switch (parent_tag) {
3561 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003562 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003563 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003564 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003565 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003566 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003567 }
3568 } else {
3569 GetObjectFile()->GetModule()->ReportError(
3570 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3571 "symbol context for 0x%8.8" PRIx64
3572 " %s.\n",
3573 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3574 orig_die.GetID(), orig_die.GetTagAsCString());
3575 }
3576 break;
3577
3578 case DW_TAG_subprogram:
3579 case DW_TAG_inlined_subroutine:
3580 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003581 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003582 // Check to see if we already have parsed the variables for the
3583 // given scope
3584
3585 Block *block = sc.function->GetBlock(true).FindBlockByID(
3586 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003587 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003588 // This must be a specification or abstract origin with a
3589 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003590 // to find the concrete block so we can correctly add the
3591 // variable to it
3592 const DWARFDIE concrete_block_die =
3593 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003594 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003595 sc_parent_die.GetOffset());
3596 if (concrete_block_die)
3597 block = sc.function->GetBlock(true).FindBlockByID(
3598 concrete_block_die.GetID());
3599 }
3600
Konrad Kleine248a1302019-05-23 11:14:47 +00003601 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003602 const bool can_create = false;
3603 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003604 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003605 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003606 block->SetVariableList(variable_list_sp);
3607 }
3608 }
3609 }
3610 break;
3611
3612 default:
3613 GetObjectFile()->GetModule()->ReportError(
3614 "didn't find appropriate parent DIE for variable list for "
3615 "0x%8.8" PRIx64 " %s.\n",
3616 orig_die.GetID(), orig_die.GetTagAsCString());
3617 break;
3618 }
3619 }
3620
3621 if (variable_list_sp) {
3622 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3623 if (var_sp) {
3624 variable_list_sp->AddVariableIfUnique(var_sp);
3625 if (cc_variable_list)
3626 cc_variable_list->AddVariableIfUnique(var_sp);
3627 ++vars_added;
3628 }
3629 }
3630 }
3631 }
3632
Konrad Kleine248a1302019-05-23 11:14:47 +00003633 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003634
3635 if (!skip_children && parse_children && die.HasChildren()) {
3636 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3637 true, cc_variable_list);
3638 }
3639
3640 if (parse_siblings)
3641 die = die.GetSibling();
3642 else
3643 die.Clear();
3644 }
3645 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003646}
3647
Vedant Kumar4b36f792018-10-05 23:23:15 +00003648/// Collect call graph edges present in a function DIE.
3649static std::vector<lldb_private::CallEdge>
3650CollectCallEdges(DWARFDIE function_die) {
3651 // Check if the function has a supported call site-related attribute.
3652 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3653 uint64_t has_call_edges =
3654 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3655 if (!has_call_edges)
3656 return {};
3657
3658 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3659 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3660 function_die.GetPubname());
3661
3662 // Scan the DIE for TAG_call_site entries.
3663 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3664 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3665 // For now, assume that all entries are nested directly under the subprogram
3666 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3667 std::vector<CallEdge> call_edges;
3668 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3669 child = child.GetSibling()) {
3670 if (child.Tag() != DW_TAG_call_site)
3671 continue;
3672
3673 // Extract DW_AT_call_origin (the call target's DIE).
3674 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3675 if (!call_origin.IsValid()) {
3676 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3677 function_die.GetPubname());
3678 continue;
3679 }
3680
3681 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3682 // available. It should only ever be unavailable for tail call edges, in
3683 // which case use LLDB_INVALID_ADDRESS.
3684 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3685 LLDB_INVALID_ADDRESS);
3686
3687 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3688 call_origin.GetPubname(), return_pc);
3689 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3690 }
3691 return call_edges;
3692}
3693
3694std::vector<lldb_private::CallEdge>
3695SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003696 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003697 if (func_die.IsValid())
3698 return CollectCallEdges(func_die);
3699 return {};
3700}
3701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003702// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003703ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3704
3705uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3706
Pavel Labathe0119902019-07-23 09:24:02 +00003707void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
3708 SymbolFile::Dump(s);
3709 m_index->Dump(s);
3710}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003711
Zachary Turner49110232018-11-05 17:40:28 +00003712void SymbolFileDWARF::DumpClangAST(Stream &s) {
3713 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3714 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3715 if (!clang)
3716 return;
3717 clang->Dump(s);
3718}
3719
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003721 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003722 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3723 if (module_sp) {
3724 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3725 if (sym_vendor)
3726 m_debug_map_symfile =
3727 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003729 }
3730 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003731}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003732
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003733DWARFExpression::LocationListFormat
3734SymbolFileDWARF::GetLocationListFormat() const {
3735 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003736 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003737 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003738}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003739
3740SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3741 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003742 ModuleSpec module_spec;
3743 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3744 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003745 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003746
3747 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3748 FileSpec dwp_filespec =
3749 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003750 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003751 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3752 dwp_filespec);
3753 }
3754 });
3755 return m_dwp_symfile.get();
3756}