blob: bb45c921e3cb6b475beb95758e845699c907eb41 [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
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +0000116#define LLDB_PROPERTIES_symbolfiledwarf
Jordan Rupprecht6a253d32019-07-29 17:22:10 +0000117#include "SymbolFileDWARFProperties.inc"
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000118
Pavel Labath9337b412018-06-06 11:35:23 +0000119enum {
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +0000120#define LLDB_PROPERTIES_symbolfiledwarf
Jordan Rupprecht6a253d32019-07-29 17:22:10 +0000121#include "SymbolFileDWARFPropertiesEnum.inc"
Pavel Labath9337b412018-06-06 11:35:23 +0000122};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124class PluginProperties : public Properties {
125public:
126 static ConstString GetSettingName() {
127 return SymbolFileDWARF::GetPluginNameStatic();
128 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 PluginProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000131 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Jonas Devliegherea8ea5952019-07-29 16:41:30 +0000132 m_collection_sp->Initialize(g_symbolfiledwarf_properties);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000134
Frederic Rissacbf0052019-04-23 20:17:04 +0000135 FileSpecList GetSymLinkPaths() {
136 const OptionValueFileSpecList *option_value =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
138 nullptr, true, ePropertySymLinkPaths);
139 assert(option_value);
140 return option_value->GetCurrentValue();
141 }
Pavel Labath9337b412018-06-06 11:35:23 +0000142
143 bool IgnoreFileIndexes() const {
144 return m_collection_sp->GetPropertyAtIndexAsBoolean(
145 nullptr, ePropertyIgnoreIndexes, false);
146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
152 static const auto g_settings_sp(std::make_shared<PluginProperties>());
153 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000157
Frederic Rissacbf0052019-04-23 20:17:04 +0000158FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
Pavel Labath7d36d722019-01-16 12:30:41 +0000159 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162void SymbolFileDWARF::Initialize() {
163 LogChannelDWARF::Initialize();
164 PluginManager::RegisterPlugin(GetPluginNameStatic(),
165 GetPluginDescriptionStatic(), CreateInstance,
166 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
170 if (!PluginManager::GetSettingForSymbolFilePlugin(
171 debugger, PluginProperties::GetSettingName())) {
172 const bool is_global_setting = true;
173 PluginManager::CreateSettingForSymbolFilePlugin(
174 debugger, GetGlobalPluginProperties()->GetValueProperties(),
175 ConstString("Properties for the dwarf symbol-file plug-in."),
176 is_global_setting);
177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180void SymbolFileDWARF::Terminate() {
181 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000182 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
186 static ConstString g_name("dwarf");
187 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188}
189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
191 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
Pavel Labathff9b4262019-05-17 08:26:58 +0000195 return new SymbolFileDWARF(obj_file,
196 /*dwo_section_list*/ nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197}
198
Pavel Labathf46e8972019-07-25 08:22:05 +0000199TypeList &SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000200 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf46e8972019-07-25 08:22:05 +0000201 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 return debug_map_symfile->GetTypeList();
Pavel Labathf46e8972019-07-25 08:22:05 +0000203 return SymbolFile::GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000204}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
206 dw_offset_t max_die_offset, uint32_t type_mask,
207 TypeSet &type_set) {
208 if (die) {
209 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (die_offset >= max_die_offset)
212 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 if (die_offset >= min_die_offset) {
215 const dw_tag_t tag = die.Tag();
216
217 bool add_type = false;
218
219 switch (tag) {
220 case DW_TAG_array_type:
221 add_type = (type_mask & eTypeClassArray) != 0;
222 break;
223 case DW_TAG_unspecified_type:
224 case DW_TAG_base_type:
225 add_type = (type_mask & eTypeClassBuiltin) != 0;
226 break;
227 case DW_TAG_class_type:
228 add_type = (type_mask & eTypeClassClass) != 0;
229 break;
230 case DW_TAG_structure_type:
231 add_type = (type_mask & eTypeClassStruct) != 0;
232 break;
233 case DW_TAG_union_type:
234 add_type = (type_mask & eTypeClassUnion) != 0;
235 break;
236 case DW_TAG_enumeration_type:
237 add_type = (type_mask & eTypeClassEnumeration) != 0;
238 break;
239 case DW_TAG_subroutine_type:
240 case DW_TAG_subprogram:
241 case DW_TAG_inlined_subroutine:
242 add_type = (type_mask & eTypeClassFunction) != 0;
243 break;
244 case DW_TAG_pointer_type:
245 add_type = (type_mask & eTypeClassPointer) != 0;
246 break;
247 case DW_TAG_rvalue_reference_type:
248 case DW_TAG_reference_type:
249 add_type = (type_mask & eTypeClassReference) != 0;
250 break;
251 case DW_TAG_typedef:
252 add_type = (type_mask & eTypeClassTypedef) != 0;
253 break;
254 case DW_TAG_ptr_to_member_type:
255 add_type = (type_mask & eTypeClassMemberPointer) != 0;
256 break;
257 }
258
259 if (add_type) {
260 const bool assert_not_being_parsed = true;
261 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
262 if (type) {
263 if (type_set.find(type) == type_set.end())
264 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268
269 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
270 child_die = child_die.GetSibling()) {
271 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
272 }
273 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000274}
275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000277 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000278
279{
Pavel Labath656ddeb2019-07-30 08:20:05 +0000280 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000282
Konrad Kleine248a1302019-05-23 11:14:47 +0000283 CompileUnit *comp_unit = nullptr;
284 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 if (sc_scope)
286 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 if (comp_unit) {
289 dwarf_cu = GetDWARFCompileUnit(comp_unit);
Konrad Kleine248a1302019-05-23 11:14:47 +0000290 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 return 0;
292 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000293 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 } else {
295 DWARFDebugInfo *info = DebugInfo();
296 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000297 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000299 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 if (dwarf_cu) {
301 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000302 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000304 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 }
306
307 std::set<CompilerType> compiler_type_set;
308 size_t num_types_added = 0;
309 for (Type *type : type_set) {
310 CompilerType compiler_type = type->GetForwardCompilerType();
311 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
312 compiler_type_set.insert(compiler_type);
313 type_list.Insert(type->shared_from_this());
314 ++num_types_added;
315 }
316 }
317 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000318}
319
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320// Gets the first parent that is a lexical block, function or inlined
321// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000322DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
324 DWARFDIE die;
325 for (die = child_die.GetParent(); die; die = die.GetParent()) {
326 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 switch (tag) {
329 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000330 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 case DW_TAG_subprogram:
332 case DW_TAG_inlined_subroutine:
333 case DW_TAG_lexical_block:
334 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 }
337 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338}
339
Pavel Labathff9b4262019-05-17 08:26:58 +0000340SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile,
341 SectionList *dwo_section_list)
Pavel Labathf4014e12019-05-16 11:07:58 +0000342 : SymbolFile(objfile),
343 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
344 // when this class parses .o files to
345 // contain the .o file index/ID
Konrad Kleine248a1302019-05-23 11:14:47 +0000346 m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
Pavel Labathff9b4262019-05-17 08:26:58 +0000347 m_context(objfile->GetModule()->GetSectionList(), dwo_section_list),
Pavel Labath00e39682019-06-14 14:12:25 +0000348 m_data_debug_loc(), m_abbr(), m_info(), m_fetched_external_modules(false),
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000349 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352SymbolFileDWARF::~SymbolFileDWARF() {}
353
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000354static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 static ConstString g_dwarf_section_name("__DWARF");
356 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
360 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
361 if (debug_map_symfile)
362 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
363 else
364 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000365}
366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
368 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
369 TypeSystem *type_system;
370 if (debug_map_symfile) {
371 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
372 } else {
373 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
374 if (type_system)
375 type_system->SetSymbolFile(this);
376 }
377 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378}
379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000381 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382
Pavel Labath9337b412018-06-06 11:35:23 +0000383 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
384 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
385 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
386 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
387 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
388 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389
Pavel Labath9337b412018-06-06 11:35:23 +0000390 m_index = AppleDWARFIndex::Create(
391 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
Pavel Labath0261b942019-05-20 08:38:47 +0000392 apple_types, apple_objc, m_context.getOrLoadStrData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393
Pavel Labath9337b412018-06-06 11:35:23 +0000394 if (m_index)
395 return;
396
397 DWARFDataExtractor debug_names;
398 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
399 if (debug_names.GetByteSize() > 0) {
400 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
Pavel Labath0261b942019-05-20 08:38:47 +0000401 DebugNamesDWARFIndex::Create(
402 *GetObjectFile()->GetModule(), debug_names,
403 m_context.getOrLoadStrData(), DebugInfo());
Pavel Labath9337b412018-06-06 11:35:23 +0000404 if (index_or) {
405 m_index = std::move(*index_or);
406 return;
407 }
408 LLDB_LOG_ERROR(log, index_or.takeError(),
409 "Unable to read .debug_names data: {0}");
410 }
411 }
412
413 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
414 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415}
416
417bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000418 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419}
420
421uint32_t SymbolFileDWARF::CalculateAbilities() {
422 uint32_t abilities = 0;
Konrad Kleine248a1302019-05-23 11:14:47 +0000423 if (m_obj_file != nullptr) {
424 const Section *section = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 const SectionList *section_list = m_obj_file->GetSectionList();
Konrad Kleine248a1302019-05-23 11:14:47 +0000426 if (section_list == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 return 0;
428
429 uint64_t debug_abbrev_file_size = 0;
430 uint64_t debug_info_file_size = 0;
431 uint64_t debug_line_file_size = 0;
432
433 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
434
435 if (section)
436 section_list = &section->GetChildren();
437
438 section =
439 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
Konrad Kleine248a1302019-05-23 11:14:47 +0000440 if (section != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 debug_info_file_size = section->GetFileSize();
442
443 section =
444 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
445 .get();
446 if (section)
447 debug_abbrev_file_size = section->GetFileSize();
448
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000449 DWARFDebugAbbrev *abbrev = DebugAbbrev();
450 if (abbrev) {
451 std::set<dw_form_t> invalid_forms;
452 abbrev->GetUnsupportedForms(invalid_forms);
453 if (!invalid_forms.empty()) {
454 StreamString error;
455 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
456 for (auto form : invalid_forms)
457 error.Printf(" %#x", form);
458 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
459 return 0;
460 }
461 }
462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 section =
464 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
465 .get();
466 if (section)
467 debug_line_file_size = section->GetFileSize();
468 } else {
469 const char *symfile_dir_cstr =
470 m_obj_file->GetFileSpec().GetDirectory().GetCString();
471 if (symfile_dir_cstr) {
472 if (strcasestr(symfile_dir_cstr, ".dsym")) {
473 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000474 // We have a dSYM file that didn't have a any debug info. If the
475 // string table has a size of 1, then it was made from an
476 // executable with no debug info, or from an executable that was
477 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 section =
479 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
480 .get();
481 if (section && section->GetFileSize() == 1) {
482 m_obj_file->GetModule()->ReportWarning(
483 "empty dSYM file detected, dSYM was created with an "
484 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000485 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490
491 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
492 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
493 LocalVariables | VariableTypes;
494
495 if (debug_line_file_size > 0)
496 abilities |= LineTables;
497 }
498 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499}
500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501const DWARFDataExtractor &
502SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
503 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000504 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
505 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
506 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508}
509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
511 DWARFDataExtractor &data) {
512 ModuleSP module_sp(m_obj_file->GetModule());
513 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000514 if (!section_list)
515 return;
516
517 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
518 if (!section_sp)
519 return;
520
521 data.Clear();
522 m_obj_file->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523}
524
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000525const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
526 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
527 if (debugLocData.GetByteSize() > 0)
528 return debugLocData;
529 return get_debug_loclists_data();
530}
531
532const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
533 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
534}
535
536const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
537 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
538 m_data_debug_loclists);
539}
540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000542 if (m_abbr)
543 return m_abbr.get();
544
Pavel Labath0261b942019-05-20 08:38:47 +0000545 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000546 if (debug_abbrev_data.GetByteSize() == 0)
547 return nullptr;
548
549 auto abbr = llvm::make_unique<DWARFDebugAbbrev>();
550 llvm::Error error = abbr->parse(debug_abbrev_data);
551 if (error) {
552 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
553 LLDB_LOG_ERROR(log, std::move(error),
554 "Unable to read .debug_abbrev section: {0}");
555 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000557
558 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
563 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000567 if (m_info == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000568 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
569 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
570 static_cast<void *>(this));
Pavel Labath6a2eb362019-06-14 13:01:16 +0000571 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0)
572 m_info = llvm::make_unique<DWARFDebugInfo>(*this, m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 }
574 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575}
576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
578 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000581DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
583 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000584 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585
Alex Langford5ada8872019-06-08 00:55:03 +0000586 DWARFDebugInfo *info = DebugInfo();
587 if (info) {
588 // The compile unit ID is the index of the DWARF unit.
589 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
590 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
591 dwarf_cu->SetUserData(comp_unit);
592 return dwarf_cu;
593 }
594 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595}
596
Pavel Labathd67b5502019-07-26 13:15:28 +0000597DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000598 if (!m_ranges) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000599 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
600 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
601 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000602
Pavel Labath00e39682019-06-14 14:12:25 +0000603 if (m_context.getOrLoadRangesData().GetByteSize() > 0)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000604 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000605
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000606 if (m_ranges)
Pavel Labath00e39682019-06-14 14:12:25 +0000607 m_ranges->Extract(m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
609 return m_ranges.get();
610}
611
Pavel Labathd67b5502019-07-26 13:15:28 +0000612DWARFDebugRngLists *SymbolFileDWARF::GetDebugRngLists() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000613 if (!m_rnglists) {
614 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
615 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
616 static_cast<void *>(this));
617
Pavel Labath00e39682019-06-14 14:12:25 +0000618 if (m_context.getOrLoadRngListsData().GetByteSize() > 0)
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000619 m_rnglists.reset(new DWARFDebugRngLists());
620
621 if (m_rnglists)
Pavel Labath00e39682019-06-14 14:12:25 +0000622 m_rnglists->Extract(m_context);
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000623 }
624 return m_rnglists.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625}
626
Pavel Labathad17e282019-06-13 11:22:47 +0000627lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 CompUnitSP cu_sp;
Pavel Labathad17e282019-06-13 11:22:47 +0000629 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
630 if (comp_unit) {
631 // We already parsed this compile unit, had out a shared pointer to it
632 cu_sp = comp_unit->shared_from_this();
633 } else {
Pavel Labath6a2eb362019-06-14 13:01:16 +0000634 if (&dwarf_cu.GetSymbolFileDWARF() != this) {
635 return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu);
Pavel Labathad17e282019-06-13 11:22:47 +0000636 } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
637 // Let the debug map create the compile unit
638 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
639 dwarf_cu.SetUserData(cu_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 } else {
Pavel Labathad17e282019-06-13 11:22:47 +0000641 ModuleSP module_sp(m_obj_file->GetModule());
642 if (module_sp) {
643 const DWARFDIE cu_die = dwarf_cu.DIE();
644 if (cu_die) {
645 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
646 if (cu_file_spec) {
647 // If we have a full path to the compile unit, we don't need to
648 // resolve the file. This can be expensive e.g. when the source
649 // files are NFS mounted.
650 cu_file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651
Pavel Labathad17e282019-06-13 11:22:47 +0000652 std::string remapped_file;
653 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
654 remapped_file))
655 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 }
Pavel Labathad17e282019-06-13 11:22:47 +0000657
658 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
659 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
660
661 bool is_optimized = dwarf_cu.GetIsOptimized();
662 BuildCuTranslationTable();
663 cu_sp = std::make_shared<CompileUnit>(
664 module_sp, &dwarf_cu, cu_file_spec,
665 *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
666 is_optimized ? eLazyBoolYes : eLazyBoolNo);
667
668 dwarf_cu.SetUserData(cu_sp.get());
669
Pavel Labathe0119902019-07-23 09:24:02 +0000670 SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 }
672 }
673 }
674 }
675 return cu_sp;
676}
677
Pavel Labathad17e282019-06-13 11:22:47 +0000678void SymbolFileDWARF::BuildCuTranslationTable() {
679 if (!m_lldb_cu_to_dwarf_unit.empty())
680 return;
681
682 DWARFDebugInfo *info = DebugInfo();
683 if (!info)
684 return;
685
686 if (!info->ContainsTypeUnits()) {
687 // We can use a 1-to-1 mapping. No need to build a translation table.
688 return;
689 }
690 for (uint32_t i = 0, num = info->GetNumUnits(); i < num; ++i) {
691 if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info->GetUnitAtIndex(i))) {
692 cu->SetID(m_lldb_cu_to_dwarf_unit.size());
693 m_lldb_cu_to_dwarf_unit.push_back(i);
694 }
695 }
696}
697
698llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
699 BuildCuTranslationTable();
700 if (m_lldb_cu_to_dwarf_unit.empty())
701 return cu_idx;
702 if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
703 return llvm::None;
704 return m_lldb_cu_to_dwarf_unit[cu_idx];
705}
706
Pavel Labathe0119902019-07-23 09:24:02 +0000707uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000709 if (!info)
710 return 0;
711 BuildCuTranslationTable();
712 return m_lldb_cu_to_dwarf_unit.empty() ? info->GetNumUnits()
713 : m_lldb_cu_to_dwarf_unit.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714}
715
716CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000717 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000719 if (!info)
720 return {};
721
722 if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
723 if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
724 info->GetUnitAtIndex(*dwarf_idx)))
725 return ParseCompileUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 }
Pavel Labathad17e282019-06-13 11:22:47 +0000727 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728}
729
Zachary Turner863f8c12019-01-11 18:03:20 +0000730Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
731 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000732 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 if (die.IsValid()) {
734 TypeSystem *type_system =
735 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
736
737 if (type_system) {
738 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
739 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000740 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 }
742 }
743 return nullptr;
744}
745
746bool SymbolFileDWARF::FixupAddress(Address &addr) {
747 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
748 if (debug_map_symfile) {
749 return debug_map_symfile->LinkOSOAddress(addr);
750 }
751 // This is a normal DWARF file, no address fixups need to happen
752 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000753}
Zachary Turner863f8c12019-01-11 18:03:20 +0000754lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000755 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000756 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 if (dwarf_cu)
758 return dwarf_cu->GetLanguageType();
759 else
760 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000761}
762
Zachary Turner863f8c12019-01-11 18:03:20 +0000763size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000764 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000765 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000766 if (!dwarf_cu)
767 return 0;
768
769 size_t functions_added = 0;
770 std::vector<DWARFDIE> function_dies;
771 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
772 for (const DWARFDIE &die : function_dies) {
773 if (comp_unit.FindFunctionByUID(die.GetID()))
774 continue;
775 if (ParseFunction(comp_unit, die))
776 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000778 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780}
781
Zachary Turner863f8c12019-01-11 18:03:20 +0000782bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
783 FileSpecList &support_files) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000784 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathad17e282019-06-13 11:22:47 +0000785 if (DWARFUnit *unit = GetDWARFCompileUnit(&comp_unit)) {
Pavel Labathca9c3de2019-06-12 11:29:50 +0000786 const dw_offset_t stmt_list = unit->GetLineTableOffset();
787 if (stmt_list != DW_INVALID_OFFSET) {
788 // All file indexes in DWARF are one based and a file of index zero is
789 // supposed to be the compile unit itself.
790 support_files.Append(comp_unit);
791 return DWARFDebugLine::ParseSupportFiles(comp_unit.GetModule(),
792 m_context.getOrLoadLineData(),
793 stmt_list, support_files, unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000794 }
795 }
796 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797}
798
Pavel Labathca9c3de2019-06-12 11:29:50 +0000799FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) {
Pavel Labathad17e282019-06-13 11:22:47 +0000800 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) {
801 if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu))
802 return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx);
803 return FileSpec();
804 }
805
806 auto &tu = llvm::cast<DWARFTypeUnit>(unit);
807 return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx);
Pavel Labathca9c3de2019-06-12 11:29:50 +0000808}
809
810const FileSpecList &
811SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) {
812 static FileSpecList empty_list;
813
814 dw_offset_t offset = tu.GetLineTableOffset();
815 if (offset == DW_INVALID_OFFSET ||
816 offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() ||
817 offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey())
818 return empty_list;
819
820 // Many type units can share a line table, so parse the support file list
821 // once, and cache it based on the offset field.
822 auto iter_bool = m_type_unit_support_files.try_emplace(offset);
823 FileSpecList &list = iter_bool.first->second;
824 if (iter_bool.second) {
Jonas Devliegheref893d5b2019-07-18 20:19:24 +0000825 list.EmplaceBack();
Pavel Labathca9c3de2019-06-12 11:29:50 +0000826 DWARFDebugLine::ParseSupportFiles(GetObjectFile()->GetModule(),
827 m_context.getOrLoadLineData(), offset,
828 list, &tu);
829 }
830 return list;
831}
832
Zachary Turner863f8c12019-01-11 18:03:20 +0000833bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000834 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000835 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 if (dwarf_cu)
837 return dwarf_cu->GetIsOptimized();
838 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000839}
840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841bool SymbolFileDWARF::ParseImportedModules(
842 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000843 std::vector<SourceModule> &imported_modules) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000844 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000846 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000847 if (!dwarf_cu)
848 return false;
849 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
850 sc.comp_unit->GetLanguage()))
851 return false;
852 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000854 const DWARFDIE die = dwarf_cu->DIE();
855 if (!die)
856 return false;
857
858 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
859 child_die = child_die.GetSibling()) {
860 if (child_die.Tag() != DW_TAG_imported_declaration)
861 continue;
862
863 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
864 if (module_die.Tag() != DW_TAG_module)
865 continue;
866
867 if (const char *name =
868 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
869 SourceModule module;
870 module.path.push_back(ConstString(name));
871
872 DWARFDIE parent_die = module_die;
873 while ((parent_die = parent_die.GetParent())) {
874 if (parent_die.Tag() != DW_TAG_module)
875 break;
876 if (const char *name =
877 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
878 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000880 std::reverse(module.path.begin(), module.path.end());
881 if (const char *include_path = module_die.GetAttributeValueAsString(
882 DW_AT_LLVM_include_path, nullptr))
883 module.search_path = ConstString(include_path);
884 if (const char *sysroot = module_die.GetAttributeValueAsString(
885 DW_AT_LLVM_isysroot, nullptr))
886 module.sysroot = ConstString(sysroot);
887 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000890 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000891}
892
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893struct ParseDWARFLineTableCallbackInfo {
894 LineTable *line_table;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000895 std::unique_ptr<LineSequence> sequence_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897};
898
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899// ParseStatementTableCallback
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900static void ParseDWARFLineTableCallback(dw_offset_t offset,
901 const DWARFDebugLine::State &state,
902 void *userData) {
903 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
904 // Just started parsing the line table
905 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
906 // Done parsing line table, nothing to do for the cleanup
907 } else {
908 ParseDWARFLineTableCallbackInfo *info =
909 (ParseDWARFLineTableCallbackInfo *)userData;
910 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911
Adrian Prantl05097242018-04-30 16:49:04 +0000912 // If this is our first time here, we need to create a sequence container.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000913 if (!info->sequence_up) {
914 info->sequence_up.reset(line_table->CreateLineSequenceContainer());
915 assert(info->sequence_up.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 line_table->AppendLineEntryToSequence(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000918 info->sequence_up.get(), state.address & info->addr_mask, state.line,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 state.column, state.file, state.is_stmt, state.basic_block,
920 state.prologue_end, state.epilogue_begin, state.end_sequence);
921 if (state.end_sequence) {
922 // First, put the current sequence into the line table.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000923 line_table->InsertSequence(info->sequence_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 // Then, empty it to prepare for the next sequence.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000925 info->sequence_up->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 }
927 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928}
929
Zachary Turner863f8c12019-01-11 18:03:20 +0000930bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Alex Langford07231732019-07-30 21:22:17 +0000931 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Konrad Kleine248a1302019-05-23 11:14:47 +0000932 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934
Zachary Turner863f8c12019-01-11 18:03:20 +0000935 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000936 if (dwarf_cu) {
937 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
938 if (dwarf_cu_die) {
939 const dw_offset_t cu_line_offset =
940 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
941 DW_INVALID_OFFSET);
942 if (cu_line_offset != DW_INVALID_OFFSET) {
943 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
944 if (line_table_up) {
945 ParseDWARFLineTableCallbackInfo info;
946 info.line_table = line_table_up.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000947
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000948 /*
949 * MIPS:
950 * The SymbolContext may not have a valid target, thus we may not be
951 * able
952 * to call Address::GetOpcodeLoadAddress() which would clear the bit
953 * #0
954 * for MIPS. Use ArchSpec to clear the bit #0.
955 */
956 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
957 case llvm::Triple::mips:
958 case llvm::Triple::mipsel:
959 case llvm::Triple::mips64:
960 case llvm::Triple::mips64el:
961 info.addr_mask = ~((lldb::addr_t)1);
962 break;
963 default:
964 info.addr_mask = ~((lldb::addr_t)0);
965 break;
966 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000967
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000968 lldb::offset_t offset = cu_line_offset;
Pavel Labath0261b942019-05-20 08:38:47 +0000969 DWARFDebugLine::ParseStatementTable(
970 m_context.getOrLoadLineData(), &offset,
971 ParseDWARFLineTableCallback, &info, dwarf_cu);
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000972 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
973 if (debug_map_symfile) {
974 // We have an object file that has a line table with addresses that
975 // are not linked. We need to link the line table and convert the
976 // addresses that are relative to the .o file into addresses for
977 // the main executable.
978 comp_unit.SetLineTable(
979 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
980 } else {
981 comp_unit.SetLineTable(line_table_up.release());
982 return true;
983 }
984 }
985 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 }
988 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989}
990
Siva Chandrad8335e92015-12-16 00:22:08 +0000991lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
993 auto iter = m_debug_macros_map.find(*offset);
994 if (iter != m_debug_macros_map.end())
995 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +0000996
Pavel Labath0261b942019-05-20 08:38:47 +0000997 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000998 if (debug_macro_data.GetByteSize() == 0)
999 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1002 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001005 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +00001006 DWARFDebugMacroEntry::ReadMacroEntries(
1007 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
1008 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001011}
1012
Zachary Turner863f8c12019-01-11 18:03:20 +00001013bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001014 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Siva Chandrad8335e92015-12-16 00:22:08 +00001015
Zachary Turner863f8c12019-01-11 18:03:20 +00001016 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001018 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019
Jan Kratochvil93afb052018-05-24 20:51:13 +00001020 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 if (!dwarf_cu_die)
1022 return false;
1023
1024 lldb::offset_t sect_offset =
1025 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1026 if (sect_offset == DW_INVALID_OFFSET)
1027 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1028 DW_INVALID_OFFSET);
1029 if (sect_offset == DW_INVALID_OFFSET)
1030 return false;
1031
Zachary Turner863f8c12019-01-11 18:03:20 +00001032 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033
1034 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001035}
1036
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001037size_t SymbolFileDWARF::ParseBlocksRecursive(
1038 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1039 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 size_t blocks_added = 0;
1041 DWARFDIE die = orig_die;
1042 while (die) {
1043 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 switch (tag) {
1046 case DW_TAG_inlined_subroutine:
1047 case DW_TAG_subprogram:
1048 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001049 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001051 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1052 // inlined functions. These will be parsed on their own as separate
1053 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054
1055 if (depth > 0)
1056 break;
1057
1058 block = parent_block;
1059 } else {
1060 BlockSP block_sp(new Block(die.GetID()));
1061 parent_block->AddChild(block_sp);
1062 block = block_sp.get();
1063 }
1064 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001065 const char *name = nullptr;
1066 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067
1068 int decl_file = 0;
1069 int decl_line = 0;
1070 int decl_column = 0;
1071 int call_file = 0;
1072 int call_line = 0;
1073 int call_column = 0;
1074 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1075 decl_line, decl_column, call_file, call_line,
1076 call_column, nullptr)) {
1077 if (tag == DW_TAG_subprogram) {
1078 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1079 subprogram_low_pc = ranges.GetMinRangeBase(0);
1080 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001081 // We get called here for inlined subroutines in two ways. The first
1082 // time is when we are making the Function object for this inlined
1083 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001085 // need to adjust the containing address. The second time is when we
1086 // are parsing the blocks inside the function that contains the
1087 // inlined concrete instance. Since these will be blocks inside the
1088 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1090 subprogram_low_pc = ranges.GetMinRangeBase(0);
1091 }
1092 }
1093
1094 const size_t num_ranges = ranges.GetSize();
1095 for (size_t i = 0; i < num_ranges; ++i) {
1096 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1097 const addr_t range_base = range.GetRangeBase();
1098 if (range_base >= subprogram_low_pc)
1099 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1100 range.GetByteSize()));
1101 else {
1102 GetObjectFile()->GetModule()->ReportError(
1103 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1104 ") which has a base that is less than the function's low PC "
1105 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1106 "start of this error message",
1107 block->GetID(), range_base, range.GetRangeEnd(),
1108 subprogram_low_pc);
1109 }
1110 }
1111 block->FinalizeRanges();
1112
1113 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001114 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001115 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001117 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001118 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 decl_line, decl_column));
1120
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001121 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001123 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001124 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 call_line, call_column));
1126
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001127 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1128 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 }
1130
1131 ++blocks_added;
1132
1133 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001134 blocks_added +=
1135 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1136 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 }
1138 }
1139 } break;
1140 default:
1141 break;
1142 }
1143
Adrian Prantl05097242018-04-30 16:49:04 +00001144 // Only parse siblings of the block if we are not at depth zero. A depth of
1145 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1146 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147
1148 if (depth == 0)
1149 die.Clear();
1150 else
1151 die = die.GetSibling();
1152 }
1153 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001154}
1155
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1157 if (parent_die) {
1158 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1159 die = die.GetSibling()) {
1160 dw_tag_t tag = die.Tag();
1161 bool check_virtuality = false;
1162 switch (tag) {
1163 case DW_TAG_inheritance:
1164 case DW_TAG_subprogram:
1165 check_virtuality = true;
1166 break;
1167 default:
1168 break;
1169 }
1170 if (check_virtuality) {
1171 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1172 return true;
1173 }
1174 }
1175 }
1176 return false;
1177}
1178
1179void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1180 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1181 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1182 std::vector<DWARFDIE> decl_ctx_die_list =
1183 ast_parser->GetDIEForDeclContext(decl_ctx);
1184
1185 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1186 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1187 decl = decl.GetSibling())
1188 ast_parser->GetDeclForUIDFromDWARF(decl);
1189}
1190
Pavel Labath3b926982019-06-21 07:56:50 +00001191user_id_t SymbolFileDWARF::GetUID(DIERef ref) {
1192 if (GetDebugMapSymfile())
1193 return GetID() | ref.die_offset();
1194
1195 return user_id_t(GetDwoNum().getValueOr(0x7fffffff)) << 32 |
1196 ref.die_offset() |
1197 (lldb::user_id_t(ref.section() == DIERef::Section::DebugTypes) << 63);
1198}
1199
Pavel Labath67b45ac2019-06-19 07:32:39 +00001200llvm::Optional<SymbolFileDWARF::DecodedUID>
1201SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001202 // This method can be called without going through the symbol vendor so we
1203 // need to lock the module.
1204 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001205 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1206 // must make sure we use the correct DWARF file when resolving things. On
1207 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1208 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1209 // references to other DWARF objects and we must be ready to receive a
1210 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001212 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1213 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labath67b45ac2019-06-19 07:32:39 +00001215 return DecodedUID{
Pavel Labath3b926982019-06-21 07:56:50 +00001216 *dwarf, {llvm::None, DIERef::Section::DebugInfo, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001217 }
Pavel Labath381ba9a2019-05-13 08:58:34 +00001218 dw_offset_t die_offset = uid;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001219 if (die_offset == DW_INVALID_OFFSET)
Pavel Labath67b45ac2019-06-19 07:32:39 +00001220 return llvm::None;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001221
Pavel Labath3b926982019-06-21 07:56:50 +00001222 DIERef::Section section =
1223 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1224
1225 llvm::Optional<uint32_t> dwo_num = uid >> 32 & 0x7fffffff;
1226 if (*dwo_num == 0x7fffffff)
1227 dwo_num = llvm::None;
1228
1229 return DecodedUID{*this, {dwo_num, section, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001230}
1231
1232DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001233SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001234 // This method can be called without going through the symbol vendor so we
1235 // need to lock the module.
1236 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001237
Pavel Labath67b45ac2019-06-19 07:32:39 +00001238 llvm::Optional<DecodedUID> decoded = DecodeUID(uid);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001239
Pavel Labath67b45ac2019-06-19 07:32:39 +00001240 if (decoded)
1241 return decoded->dwarf.GetDIE(decoded->ref);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001242
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001244}
1245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001247 // This method can be called without going through the symbol vendor so we
1248 // need to lock the module.
1249 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001250 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001251 // SymbolFileDWARF::GetDIE(). See comments inside the
1252 // SymbolFileDWARF::GetDIE() for details.
1253 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 return die.GetDecl();
1255 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001256}
1257
Greg Clayton99558cc42015-08-24 23:46:31 +00001258CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001260 // This method can be called without going through the symbol vendor so we
1261 // need to lock the module.
1262 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001263 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001264 // SymbolFileDWARF::GetDIE(). See comments inside the
1265 // SymbolFileDWARF::GetDIE() for details.
1266 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 return die.GetDeclContext();
1268 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001269}
1270
Greg Clayton99558cc42015-08-24 23:46:31 +00001271CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001273 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001274 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001275 // SymbolFileDWARF::GetDIE(). See comments inside the
1276 // SymbolFileDWARF::GetDIE() for details.
1277 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 return die.GetContainingDeclContext();
1279 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280}
1281
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001283 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001284 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001285 // SymbolFileDWARF::GetDIE(). See comments inside the
1286 // SymbolFileDWARF::GetDIE() for details.
1287 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288 return type_die.ResolveType();
1289 else
1290 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291}
1292
Adrian Prantleca07c52018-11-05 20:49:07 +00001293llvm::Optional<SymbolFile::ArrayInfo>
1294SymbolFileDWARF::GetDynamicArrayInfoForUID(
1295 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1296 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001297 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001298 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1299 else
1300 return llvm::None;
1301}
1302
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1304 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001305}
1306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1308 bool assert_not_being_parsed) {
1309 if (die) {
1310 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1311 if (log)
1312 GetObjectFile()->GetModule()->LogMessage(
1313 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1314 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001315
Adrian Prantl05097242018-04-30 16:49:04 +00001316 // We might be coming in in the middle of a type tree (a class within a
1317 // class, an enum within a class), so parse any needed parent DIEs before
1318 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1320 if (decl_ctx_die) {
1321 if (log) {
1322 switch (decl_ctx_die.Tag()) {
1323 case DW_TAG_structure_type:
1324 case DW_TAG_union_type:
1325 case DW_TAG_class_type: {
1326 // Get the type, which could be a forward declaration
1327 if (log)
1328 GetObjectFile()->GetModule()->LogMessage(
1329 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1330 "resolve parent forward type for 0x%8.8x",
1331 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1332 decl_ctx_die.GetOffset());
1333 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 default:
1336 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001337 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001339 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 return ResolveType(die);
1341 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001342 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001343}
1344
Greg Clayton6beaaa62011-01-17 03:46:26 +00001345// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001346// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1347// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348bool SymbolFileDWARF::HasForwardDeclForClangType(
1349 const CompilerType &compiler_type) {
1350 CompilerType compiler_type_no_qualifiers =
1351 ClangUtil::RemoveFastQualifiers(compiler_type);
1352 if (GetForwardDeclClangTypeToDie().count(
1353 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1354 return true;
1355 }
1356 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 ClangASTContext *clang_type_system =
1359 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1360 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001361 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 DWARFASTParserClang *ast_parser =
1363 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1364 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001365}
1366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001368 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 ClangASTContext *clang_type_system =
1371 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1372 if (clang_type_system) {
1373 DWARFASTParserClang *ast_parser =
1374 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1375 if (ast_parser &&
1376 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1377 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1378 }
Jim Inghamc3549282012-01-11 02:21:12 +00001379
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 // We have a struct/union/class/enum that needs to be fully resolved.
1381 CompilerType compiler_type_no_qualifiers =
1382 ClangUtil::RemoveFastQualifiers(compiler_type);
1383 auto die_it = GetForwardDeclClangTypeToDie().find(
1384 compiler_type_no_qualifiers.GetOpaqueQualType());
1385 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1386 // We have already resolved this type...
1387 return true;
1388 }
1389
1390 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1391 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001392 // Once we start resolving this type, remove it from the forward
1393 // declaration map in case anyone child members or other types require this
1394 // type to get resolved. The type will get resolved when all of the calls
1395 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 GetForwardDeclClangTypeToDie().erase(die_it);
1397
1398 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1399
1400 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1401 DWARF_LOG_TYPE_COMPLETION));
1402 if (log)
1403 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1404 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1405 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1406 type->GetName().AsCString());
1407 assert(compiler_type);
1408 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1409 if (dwarf_ast)
1410 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1411 }
1412 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1416 bool assert_not_being_parsed,
1417 bool resolve_function_context) {
1418 if (die) {
1419 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1420
1421 if (assert_not_being_parsed) {
1422 if (type != DIE_IS_BEING_PARSED)
1423 return type;
1424
1425 GetObjectFile()->GetModule()->ReportError(
1426 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1427 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1428
1429 } else
1430 return type;
1431 }
1432 return nullptr;
1433}
1434
Pavel Labathad17e282019-06-13 11:22:47 +00001435CompileUnit *
1436SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001438 if (dwarf_cu.GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001439 // The symbol vendor doesn't know about this compile unit, we need to parse
1440 // and add it to the symbol vendor object.
Pavel Labath519ef6a2019-05-27 10:10:59 +00001441 return ParseCompileUnit(dwarf_cu).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 }
Pavel Labathad17e282019-06-13 11:22:47 +00001443 return (CompileUnit *)dwarf_cu.GetUserData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444}
1445
1446size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1447 DIEArray &method_die_offsets) {
1448 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001449 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 return method_die_offsets.size();
1451}
1452
1453bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1454 sc.Clear(false);
1455
Pavel Labathad17e282019-06-13 11:22:47 +00001456 if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001458 sc.comp_unit =
1459 GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460
1461 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001462 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001463 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464
1465 if (sc.function) {
1466 sc.module_sp = sc.function->CalculateSymbolContextModule();
1467 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469 }
1470
1471 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472}
1473
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1475 UpdateExternalModuleListIfNeeded();
1476 const auto &pos = m_external_type_modules.find(name);
1477 if (pos != m_external_type_modules.end())
1478 return pos->second;
1479 else
1480 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001481}
1482
Greg Clayton2f869fe2016-03-30 20:14:35 +00001483DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
Pavel Labath3b926982019-06-21 07:56:50 +00001485 if (die_ref.dwo_num()) {
1486 return DebugInfo()
1487 ->GetUnitAtIndex(*die_ref.dwo_num())
1488 ->GetDwoSymbolFile()
1489 ->GetDIE(die_ref);
1490 }
1491
1492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 DWARFDebugInfo *debug_info = DebugInfo();
1494 if (debug_info)
1495 return debug_info->GetDIE(die_ref);
1496 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001497 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001498}
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500std::unique_ptr<SymbolFileDWARFDwo>
1501SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Pavel Labathad17e282019-06-13 11:22:47 +00001502 DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001504 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 // info files for modules.
1506 if (GetDebugMapSymfile())
1507 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001508
Pavel Labathad17e282019-06-13 11:22:47 +00001509 DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit);
1510 // Only compile units can be split into two parts.
1511 if (!dwarf_cu)
1512 return nullptr;
1513
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001514 const char *dwo_name =
Pavel Labathad17e282019-06-13 11:22:47 +00001515 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 if (!dwo_name)
1517 return nullptr;
1518
Tamas Berghammer963ce482017-08-25 13:56:14 +00001519 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1520 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001521 uint64_t dwo_id =
Pavel Labathad17e282019-06-13 11:22:47 +00001522 cu_die.GetAttributeValueAsUnsigned(dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001523 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
Pavel Labathad17e282019-06-13 11:22:47 +00001524 dwp_symfile->GetSymbolFileForDwoId(*dwarf_cu, dwo_id);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001525 if (dwo_symfile)
1526 return dwo_symfile;
1527 }
1528
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001529 FileSpec dwo_file(dwo_name);
1530 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001532 const char *comp_dir =
Pavel Labathad17e282019-06-13 11:22:47 +00001533 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534 if (!comp_dir)
1535 return nullptr;
1536
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001537 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1538 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 dwo_file.AppendPathComponent(dwo_name);
1540 }
1541
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001542 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 return nullptr;
1544
1545 const lldb::offset_t file_offset = 0;
1546 DataBufferSP dwo_file_data_sp;
1547 lldb::offset_t dwo_file_data_offset = 0;
1548 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1549 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001550 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1551 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 if (dwo_obj_file == nullptr)
1553 return nullptr;
1554
Pavel Labathad17e282019-06-13 11:22:47 +00001555 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001556}
1557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1559 if (m_fetched_external_modules)
1560 return;
1561 m_fetched_external_modules = true;
1562
1563 DWARFDebugInfo *debug_info = DebugInfo();
1564
1565 const uint32_t num_compile_units = GetNumCompileUnits();
1566 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001567 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568
Jan Kratochvil93afb052018-05-24 20:51:13 +00001569 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001570 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1572
1573 if (name) {
1574 ConstString const_name(name);
1575 if (m_external_type_modules.find(const_name) ==
1576 m_external_type_modules.end()) {
1577 ModuleSP module_sp;
1578 const char *dwo_path =
1579 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1580 if (dwo_path) {
1581 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001582 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001583 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1585 const char *comp_dir =
1586 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1587 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001588 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001589 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001590 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1592 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594 dwo_module_spec.GetArchitecture() =
1595 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001596
Adrian Prantl05097242018-04-30 16:49:04 +00001597 // When LLDB loads "external" modules it looks at the presence of
1598 // DW_AT_GNU_dwo_name. However, when the already created module
1599 // (corresponding to .dwo itself) is being processed, it will see
1600 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1601 // dwo file) and will try to call ModuleList::GetSharedModule
1602 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1603 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1604 // DW_AT_comp_dir. In this case the method
1605 // ModuleList::GetSharedModule will fail and the warning will be
1606 // printed. However, as one can notice in this case we don't
1607 // actually need to try to load the already loaded module
1608 // (corresponding to .dwo) so we simply skip it.
Raphael Isemannc53f8db2019-05-01 09:49:07 +00001609 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001610 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1611 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1612 continue;
1613 }
1614
Zachary Turner97206d52017-05-12 04:51:55 +00001615 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001616 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 if (!module_sp) {
1618 GetObjectFile()->GetModule()->ReportWarning(
1619 "0x%8.8x: unable to locate module needed for external types: "
1620 "%s\nerror: %s\nDebugging will be degraded due to missing "
1621 "types. Rebuilding your project will regenerate the needed "
1622 "module files.",
1623 die.GetOffset(),
1624 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1625 error.AsCString("unknown error"));
1626 }
1627 }
1628 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001629 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001631 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 }
1633}
1634
1635SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001636 if (!m_global_aranges_up) {
1637 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638
1639 ModuleSP module_sp = GetObjectFile()->GetModule();
1640 if (module_sp) {
1641 const size_t num_cus = module_sp->GetNumCompileUnits();
1642 for (size_t i = 0; i < num_cus; ++i) {
1643 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1644 if (cu_sp) {
1645 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1646 if (globals_sp) {
1647 const size_t num_globals = globals_sp->GetSize();
1648 for (size_t g = 0; g < num_globals; ++g) {
1649 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1650 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1651 const DWARFExpression &location = var_sp->LocationExpression();
1652 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001653 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001654 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1655 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 if (location_result.GetValueType() ==
1657 Value::eValueTypeFileAddress) {
1658 lldb::addr_t file_addr =
1659 location_result.GetScalar().ULongLong();
1660 lldb::addr_t byte_size = 1;
1661 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001662 byte_size =
1663 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001664 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 file_addr, byte_size, var_sp.get()));
1666 }
1667 }
1668 }
1669 }
1670 }
1671 }
1672 }
1673 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001674 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001676 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677}
1678
1679uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001680 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681 SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001682 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00001683 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1684 Timer scoped_timer(func_cat,
1685 "SymbolFileDWARF::"
1686 "ResolveSymbolContext (so_addr = { "
1687 "section = %p, offset = 0x%" PRIx64
1688 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 static_cast<void *>(so_addr.GetSection().get()),
1690 so_addr.GetOffset(), resolve_scope);
1691 uint32_t resolved = 0;
1692 if (resolve_scope &
1693 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1694 eSymbolContextLineEntry | eSymbolContextVariable)) {
1695 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1696
1697 DWARFDebugInfo *debug_info = DebugInfo();
1698 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001699 llvm::Expected<DWARFDebugAranges &> aranges =
1700 debug_info->GetCompileUnitAranges();
1701 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001702 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001703 LLDB_LOG_ERROR(log, aranges.takeError(),
1704 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1705 "aranges. {0}");
1706 return 0;
1707 }
1708
1709 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001711 // Global variables are not in the compile unit address ranges. The
1712 // only way to currently find global variables is to iterate over the
1713 // .debug_pubnames or the __apple_names table and find all items in
1714 // there that point to DW_TAG_variable DIEs and then find the address
1715 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 if (resolve_scope & eSymbolContextVariable) {
1717 GlobalVariableMap &map = GetGlobalAranges();
1718 const GlobalVariableMap::Entry *entry =
1719 map.FindEntryThatContains(file_vm_addr);
1720 if (entry && entry->data) {
1721 Variable *variable = entry->data;
1722 SymbolContextScope *scc = variable->GetSymbolContextScope();
1723 if (scc) {
1724 scc->CalculateSymbolContext(&sc);
1725 sc.variable = variable;
1726 }
1727 return sc.GetResolvedMask();
1728 }
1729 }
1730 } else {
1731 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathad17e282019-06-13 11:22:47 +00001732 if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>(
1733 debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1734 cu_offset, &cu_idx))) {
1735 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 if (sc.comp_unit) {
1737 resolved |= eSymbolContextCompUnit;
1738
1739 bool force_check_line_table = false;
1740 if (resolve_scope &
1741 (eSymbolContextFunction | eSymbolContextBlock)) {
1742 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1743 DWARFDIE block_die;
1744 if (function_die) {
1745 sc.function =
1746 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001747 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001748 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749
1750 if (sc.function && (resolve_scope & eSymbolContextBlock))
1751 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1752 } else {
1753 // We might have had a compile unit that had discontiguous
1754 // address ranges where the gaps are symbols that don't have
1755 // any debug info. Discontiguous compile unit address ranges
1756 // should only happen when there aren't other functions from
1757 // other compile units in these gaps. This helps keep the size
1758 // of the aranges down.
1759 force_check_line_table = true;
1760 }
1761
Konrad Kleine248a1302019-05-23 11:14:47 +00001762 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 resolved |= eSymbolContextFunction;
1764
1765 if (resolve_scope & eSymbolContextBlock) {
1766 Block &block = sc.function->GetBlock(true);
1767
1768 if (block_die)
1769 sc.block = block.FindBlockByID(block_die.GetID());
1770 else
1771 sc.block = block.FindBlockByID(function_die.GetID());
1772 if (sc.block)
1773 resolved |= eSymbolContextBlock;
1774 }
1775 }
1776 }
1777
1778 if ((resolve_scope & eSymbolContextLineEntry) ||
1779 force_check_line_table) {
1780 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001781 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001783 // terms of this debug file if there is no debug map, or it
1784 // will be an address in the .o file which needs to be fixed up
1785 // to be in terms of the debug map executable. Either way,
1786 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 Address exe_so_addr(so_addr);
1788 if (FixupAddress(exe_so_addr)) {
1789 if (line_table->FindLineEntryByAddress(exe_so_addr,
1790 sc.line_entry)) {
1791 resolved |= eSymbolContextLineEntry;
1792 }
1793 }
1794 }
1795 }
1796
1797 if (force_check_line_table &&
1798 !(resolved & eSymbolContextLineEntry)) {
1799 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001800 // address ranges where the gaps are symbols that don't have any
1801 // debug info. Discontiguous compile unit address ranges should
1802 // only happen when there aren't other functions from other
1803 // compile units in these gaps. This helps keep the size of the
1804 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001805 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001806 resolved &= ~eSymbolContextCompUnit;
1807 }
1808 } else {
1809 GetObjectFile()->GetModule()->ReportWarning(
1810 "0x%8.8x: compile unit %u failed to create a valid "
1811 "lldb_private::CompileUnit class.",
1812 cu_offset, cu_idx);
1813 }
1814 }
1815 }
1816 }
1817 }
1818 return resolved;
1819}
1820
1821uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1822 uint32_t line,
1823 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001824 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001826 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 const uint32_t prev_size = sc_list.GetSize();
1828 if (resolve_scope & eSymbolContextCompUnit) {
Pavel Labathad17e282019-06-13 11:22:47 +00001829 for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus;
1830 ++cu_idx) {
1831 CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get();
1832 if (!dc_cu)
1833 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834
Pavel Labathad17e282019-06-13 11:22:47 +00001835 const bool full_match = (bool)file_spec.GetDirectory();
1836 bool file_spec_matches_cu_file_spec =
1837 FileSpec::Equal(file_spec, *dc_cu, full_match);
1838 if (check_inlines || file_spec_matches_cu_file_spec) {
1839 SymbolContext sc(m_obj_file->GetModule());
1840 sc.comp_unit = dc_cu;
1841 uint32_t file_idx = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842
Pavel Labathad17e282019-06-13 11:22:47 +00001843 // If we are looking for inline functions only and we don't find it
1844 // in the support files, we are done.
1845 if (check_inlines) {
1846 file_idx =
1847 sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true);
1848 if (file_idx == UINT32_MAX)
1849 continue;
1850 }
1851
1852 if (line != 0) {
1853 LineTable *line_table = sc.comp_unit->GetLineTable();
1854
1855 if (line_table != nullptr && line != 0) {
1856 // We will have already looked up the file index if we are
1857 // searching for inline entries.
1858 if (!check_inlines)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1860 1, file_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861
Pavel Labathad17e282019-06-13 11:22:47 +00001862 if (file_idx != UINT32_MAX) {
1863 uint32_t found_line;
1864 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1865 0, file_idx, line, false, &sc.line_entry);
1866 found_line = sc.line_entry.line;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867
Pavel Labathad17e282019-06-13 11:22:47 +00001868 while (line_idx != UINT32_MAX) {
1869 sc.function = nullptr;
1870 sc.block = nullptr;
1871 if (resolve_scope &
1872 (eSymbolContextFunction | eSymbolContextBlock)) {
1873 const lldb::addr_t file_vm_addr =
1874 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1875 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1876 DWARFDIE function_die =
1877 GetDWARFCompileUnit(dc_cu)->LookupAddress(file_vm_addr);
1878 DWARFDIE block_die;
1879 if (function_die) {
1880 sc.function =
1881 sc.comp_unit->FindFunctionByUID(function_die.GetID())
1882 .get();
1883 if (sc.function == nullptr)
1884 sc.function =
1885 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886
Pavel Labathad17e282019-06-13 11:22:47 +00001887 if (sc.function && (resolve_scope & eSymbolContextBlock))
1888 block_die =
1889 function_die.LookupDeepestBlock(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 }
1891
Pavel Labathad17e282019-06-13 11:22:47 +00001892 if (sc.function != nullptr) {
1893 Block &block = sc.function->GetBlock(true);
1894
1895 if (block_die)
1896 sc.block = block.FindBlockByID(block_die.GetID());
1897 else if (function_die)
1898 sc.block = block.FindBlockByID(function_die.GetID());
1899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001900 }
1901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902
Pavel Labathad17e282019-06-13 11:22:47 +00001903 sc_list.Append(sc);
1904 line_idx = line_table->FindLineEntryIndexByFileIndex(
1905 line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1906 }
1907 }
1908 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1909 // only append the context if we aren't looking for inline call
1910 // sites by file and line and if the file spec matches that of
1911 // the compile unit
1912 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913 }
Pavel Labathad17e282019-06-13 11:22:47 +00001914 } 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
1921 if (!check_inlines)
1922 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923 }
1924 }
1925 }
1926 return sc_list.GetSize() - prev_size;
1927}
1928
Jim Ingham7fca8c02017-04-28 00:51:06 +00001929void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001930 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001931 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001932}
1933
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001934std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1935 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1936 if (module_sp)
1937 return module_sp->GetMutex();
1938 return GetObjectFile()->GetModule()->GetMutex();
1939}
1940
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1942 const lldb_private::CompilerDeclContext *decl_ctx) {
1943 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001944 // Invalid namespace decl which means we aren't matching only things in
1945 // this symbol file, so return true to indicate it matches this symbol
1946 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001947 return true;
1948 }
1949
1950 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1951 TypeSystem *type_system = GetTypeSystemForLanguage(
1952 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1953 if (decl_ctx_type_system == type_system)
1954 return true; // The type systems match, return true
1955
1956 // The namespace AST was valid, and it does not match...
1957 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1958
1959 if (log)
1960 GetObjectFile()->GetModule()->LogMessage(
1961 log, "Valid namespace does not match symbol file");
1962
1963 return false;
1964}
1965
1966uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001967 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00001968 uint32_t max_matches, VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001969 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1971
1972 if (log)
1973 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00001974 log,
1975 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
1976 "parent_decl_ctx=%p, max_matches=%u, variables)",
1977 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978 max_matches);
1979
1980 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1981 return 0;
1982
1983 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00001984 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001985 return 0;
1986
Pavel Labath34cda142018-05-31 09:46:26 +00001987 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988 const uint32_t original_size = variables.GetSize();
1989
Pavel Labathe1d18752018-06-07 10:04:44 +00001990 llvm::StringRef basename;
1991 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00001992 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00001993
1994 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
1995 context, basename))
1996 basename = name.GetStringRef();
1997
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00001999 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 const size_t num_die_matches = die_offsets.size();
2001 if (num_die_matches) {
2002 SymbolContext sc;
2003 sc.module_sp = m_obj_file->GetModule();
2004 assert(sc.module_sp);
2005
Pavel Labathe1d18752018-06-07 10:04:44 +00002006 // Loop invariant: Variables up to this index have been checked for context
2007 // matches.
2008 uint32_t pruned_idx = original_size;
2009
Kate Stoneb9c1b512016-09-06 20:57:50 +00002010 bool done = false;
2011 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2012 const DIERef &die_ref = die_offsets[i];
2013 DWARFDIE die = GetDIE(die_ref);
2014
2015 if (die) {
2016 switch (die.Tag()) {
2017 default:
2018 case DW_TAG_subprogram:
2019 case DW_TAG_inlined_subroutine:
2020 case DW_TAG_try_block:
2021 case DW_TAG_catch_block:
2022 break;
2023
2024 case DW_TAG_variable: {
Pavel Labathad17e282019-06-13 11:22:47 +00002025 auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2026 if (!dwarf_cu)
2027 continue;
2028 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029
2030 if (parent_decl_ctx) {
2031 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2032 if (dwarf_ast) {
2033 CompilerDeclContext actual_parent_decl_ctx =
2034 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2035 if (!actual_parent_decl_ctx ||
2036 actual_parent_decl_ctx != *parent_decl_ctx)
2037 continue;
2038 }
2039 }
2040
2041 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2042 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002043 while (pruned_idx < variables.GetSize()) {
2044 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002045 if (name_is_mangled ||
2046 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002047 ++pruned_idx;
2048 else
2049 variables.RemoveVariableAtIndex(pruned_idx);
2050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051
2052 if (variables.GetSize() - original_size >= max_matches)
2053 done = true;
2054 } break;
2055 }
2056 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002057 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002058 }
2059 }
2060 }
2061
2062 // Return the number of variable that were appended to the list
2063 const uint32_t num_matches = variables.GetSize() - original_size;
2064 if (log && num_matches > 0) {
2065 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002066 log,
2067 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2068 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2069 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002070 max_matches, num_matches);
2071 }
2072 return num_matches;
2073}
2074
2075uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002076 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002077 VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002078 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002079 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2080
2081 if (log) {
2082 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002083 log,
2084 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2085 "max_matches=%u, variables)",
2086 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002087 }
2088
2089 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002090 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 return 0;
2092
Pavel Labath34cda142018-05-31 09:46:26 +00002093 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094 const uint32_t original_size = variables.GetSize();
2095
2096 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002097 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098
2099 SymbolContext sc;
2100 sc.module_sp = m_obj_file->GetModule();
2101 assert(sc.module_sp);
2102
2103 const size_t num_matches = die_offsets.size();
2104 if (num_matches) {
2105 for (size_t i = 0; i < num_matches; ++i) {
2106 const DIERef &die_ref = die_offsets[i];
2107 DWARFDIE die = GetDIE(die_ref);
2108
2109 if (die) {
Pavel Labathad17e282019-06-13 11:22:47 +00002110 DWARFCompileUnit *dwarf_cu =
2111 llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2112 if (!dwarf_cu)
2113 continue;
2114 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002115
2116 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2117
2118 if (variables.GetSize() - original_size >= max_matches)
2119 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002120 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00002121 m_index->ReportInvalidDIERef(die_ref, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 }
2123 }
2124
2125 // Return the number of variable that were appended to the list
2126 return variables.GetSize() - original_size;
2127}
2128
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2130 bool include_inlines,
2131 SymbolContextList &sc_list) {
2132 SymbolContext sc;
2133
2134 if (!orig_die)
2135 return false;
2136
2137 // If we were passed a die that is not a function, just return false...
2138 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2139 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2140 return false;
2141
2142 DWARFDIE die = orig_die;
2143 DWARFDIE inlined_die;
2144 if (die.Tag() == DW_TAG_inlined_subroutine) {
2145 inlined_die = die;
2146
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002147 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 die = die.GetParent();
2149
2150 if (die) {
2151 if (die.Tag() == DW_TAG_subprogram)
2152 break;
2153 } else
2154 break;
2155 }
2156 }
2157 assert(die && die.Tag() == DW_TAG_subprogram);
2158 if (GetFunction(die, sc)) {
2159 Address addr;
2160 // Parse all blocks if needed
2161 if (inlined_die) {
2162 Block &function_block = sc.function->GetBlock(true);
2163 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002164 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002166 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 addr.Clear();
2168 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002169 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170 addr = sc.function->GetAddressRange().GetBaseAddress();
2171 }
2172
2173 if (addr.IsValid()) {
2174 sc_list.Append(sc);
2175 return true;
2176 }
2177 }
2178
2179 return false;
2180}
2181
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2183 const DWARFDIE &die) {
2184 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002185 // parent decl context isn't valid, we aren't trying to look for any
2186 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2188 return true;
2189
2190 if (die) {
2191 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2192 if (dwarf_ast) {
2193 CompilerDeclContext actual_decl_ctx =
2194 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2195 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002196 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197 }
2198 }
2199 return false;
2200}
2201
Zachary Turner117b1fa2018-10-25 20:45:40 +00002202uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002203 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002204 FunctionNameType name_type_mask, bool include_inlines, bool append,
2205 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002206 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00002207 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2208 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002209 name.AsCString());
2210
2211 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002212 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2214
2215 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2216
2217 if (log) {
2218 GetObjectFile()->GetModule()->LogMessage(
2219 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2220 "name_type_mask=0x%x, append=%u, sc_list)",
2221 name.GetCString(), name_type_mask, append);
2222 }
2223
2224 // If we aren't appending the results to this list, then clear the list
2225 if (!append)
2226 sc_list.Clear();
2227
2228 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2229 return 0;
2230
2231 // If name is empty then we won't find anything.
2232 if (name.IsEmpty())
2233 return 0;
2234
Adrian Prantl05097242018-04-30 16:49:04 +00002235 // Remember how many sc_list are in the list before we search in case we are
2236 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238 const uint32_t original_size = sc_list.GetSize();
2239
Pavel Labath5af11ab2018-06-05 10:33:56 +00002240 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2241 DIEArray offsets;
2242 CompilerDeclContext empty_decl_ctx;
2243 if (!parent_decl_ctx)
2244 parent_decl_ctx = &empty_decl_ctx;
2245
2246 std::vector<DWARFDIE> dies;
Pavel Labath3b926982019-06-21 07:56:50 +00002247 m_index->GetFunctions(name, *this, *parent_decl_ctx, name_type_mask, dies);
Pavel Labath5af11ab2018-06-05 10:33:56 +00002248 for (const DWARFDIE &die: dies) {
2249 if (resolved_dies.insert(die.GetDIE()).second)
2250 ResolveFunction(die, include_inlines, sc_list);
2251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252
2253 // Return the number of variable that were appended to the list
2254 const uint32_t num_matches = sc_list.GetSize() - original_size;
2255
2256 if (log && num_matches > 0) {
2257 GetObjectFile()->GetModule()->LogMessage(
2258 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2259 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2260 "%u",
2261 name.GetCString(), name_type_mask, include_inlines, append,
2262 num_matches);
2263 }
2264 return num_matches;
2265}
2266
2267uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2268 bool include_inlines, bool append,
2269 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002270 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00002271 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2272 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002273 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274
2275 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2276
2277 if (log) {
2278 GetObjectFile()->GetModule()->LogMessage(
2279 log,
2280 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002281 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 }
2283
2284 // If we aren't appending the results to this list, then clear the list
2285 if (!append)
2286 sc_list.Clear();
2287
Pavel Labathb13f0332018-05-21 14:12:52 +00002288 DWARFDebugInfo *info = DebugInfo();
2289 if (!info)
2290 return 0;
2291
Adrian Prantl05097242018-04-30 16:49:04 +00002292 // Remember how many sc_list are in the list before we search in case we are
2293 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 uint32_t original_size = sc_list.GetSize();
2295
Pavel Labath6de9c792018-06-05 12:13:22 +00002296 DIEArray offsets;
2297 m_index->GetFunctions(regex, offsets);
2298
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002299 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2300 for (DIERef ref : offsets) {
2301 DWARFDIE die = info->GetDIE(ref);
2302 if (!die) {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002303 m_index->ReportInvalidDIERef(ref, regex.GetText());
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002304 continue;
2305 }
2306 if (resolved_dies.insert(die.GetDIE()).second)
2307 ResolveFunction(die, include_inlines, sc_list);
2308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002309
2310 // Return the number of variable that were appended to the list
2311 return sc_list.GetSize() - original_size;
2312}
2313
2314void SymbolFileDWARF::GetMangledNamesForFunction(
2315 const std::string &scope_qualified_name,
2316 std::vector<ConstString> &mangled_names) {
2317 DWARFDebugInfo *info = DebugInfo();
2318 uint32_t num_comp_units = 0;
2319 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002320 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321
2322 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002323 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 if (cu == nullptr)
2325 continue;
2326
2327 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2328 if (dwo)
2329 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2330 }
2331
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002332 for (lldb::user_id_t uid :
2333 m_function_scope_qualified_name_map.lookup(scope_qualified_name)) {
2334 DWARFDIE die = GetDIE(uid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 mangled_names.push_back(ConstString(die.GetMangledName()));
2336 }
2337}
2338
2339uint32_t SymbolFileDWARF::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002340 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00002341 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002342 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2343 TypeMap &types) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002344 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345 // If we aren't appending the results to this list, then clear the list
2346 if (!append)
2347 types.Clear();
2348
2349 // Make sure we haven't already searched this SymbolFile before...
2350 if (searched_symbol_files.count(this))
2351 return 0;
2352 else
2353 searched_symbol_files.insert(this);
2354
2355 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002356 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002357 return 0;
2358
2359 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2360
2361 if (log) {
2362 if (parent_decl_ctx)
2363 GetObjectFile()->GetModule()->LogMessage(
2364 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2365 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2366 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2367 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2368 else
2369 GetObjectFile()->GetModule()->LogMessage(
2370 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2371 "NULL, append=%u, max_matches=%u, type_list)",
2372 name.GetCString(), append, max_matches);
2373 }
2374
2375 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2376 return 0;
2377
2378 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002379 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002380 const size_t num_die_matches = die_offsets.size();
2381
2382 if (num_die_matches) {
2383 const uint32_t initial_types_size = types.GetSize();
2384 for (size_t i = 0; i < num_die_matches; ++i) {
2385 const DIERef &die_ref = die_offsets[i];
2386 DWARFDIE die = GetDIE(die_ref);
2387
2388 if (die) {
2389 if (!DIEInDeclContext(parent_decl_ctx, die))
2390 continue; // The containing decl contexts don't match
2391
2392 Type *matching_type = ResolveType(die, true, true);
2393 if (matching_type) {
2394 // We found a type pointer, now find the shared pointer form our type
2395 // list
2396 types.InsertUnique(matching_type->shared_from_this());
2397 if (types.GetSize() >= max_matches)
2398 break;
2399 }
2400 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002401 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 }
2403 }
2404 const uint32_t num_matches = types.GetSize() - initial_types_size;
2405 if (log && num_matches) {
2406 if (parent_decl_ctx) {
2407 GetObjectFile()->GetModule()->LogMessage(
2408 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2409 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2410 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2411 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2412 num_matches);
2413 } else {
2414 GetObjectFile()->GetModule()->LogMessage(
2415 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2416 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2417 name.GetCString(), append, max_matches, num_matches);
2418 }
2419 }
2420 return num_matches;
2421 } else {
2422 UpdateExternalModuleListIfNeeded();
2423
2424 for (const auto &pair : m_external_type_modules) {
2425 ModuleSP external_module_sp = pair.second;
2426 if (external_module_sp) {
2427 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2428 if (sym_vendor) {
2429 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002430 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2431 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002432 if (num_external_matches)
2433 return num_external_matches;
2434 }
2435 }
2436 }
2437 }
2438
2439 return 0;
2440}
2441
2442size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2443 bool append, TypeMap &types) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002444 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445 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) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002491 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002492 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2493
2494 if (log) {
2495 GetObjectFile()->GetModule()->LogMessage(
2496 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2497 name.GetCString());
2498 }
2499
2500 CompilerDeclContext namespace_decl_ctx;
2501
2502 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2503 return namespace_decl_ctx;
2504
2505 DWARFDebugInfo *info = DebugInfo();
2506 if (info) {
2507 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002508 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002509 const size_t num_matches = die_offsets.size();
2510 if (num_matches) {
2511 for (size_t i = 0; i < num_matches; ++i) {
2512 const DIERef &die_ref = die_offsets[i];
2513 DWARFDIE die = GetDIE(die_ref);
2514
2515 if (die) {
2516 if (!DIEInDeclContext(parent_decl_ctx, die))
2517 continue; // The containing decl contexts don't match
2518
2519 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2520 if (dwarf_ast) {
2521 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2522 if (namespace_decl_ctx)
2523 break;
2524 }
2525 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002526 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 }
2528 }
2529 }
2530 }
2531 if (log && namespace_decl_ctx) {
2532 GetObjectFile()->GetModule()->LogMessage(
2533 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2534 "CompilerDeclContext(%p/%p) \"%s\"",
2535 name.GetCString(),
2536 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2537 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2538 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2539 }
2540
2541 return namespace_decl_ctx;
2542}
2543
2544TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2545 bool resolve_function_context) {
2546 TypeSP type_sp;
2547 if (die) {
2548 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002549 if (type_ptr == nullptr) {
Pavel Labathad17e282019-06-13 11:22:47 +00002550 SymbolContextScope *scope;
2551 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()))
2552 scope = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2553 else
2554 scope = GetObjectFile()->GetModule().get();
2555 assert(scope);
2556 SymbolContext sc(scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002557 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2558 while (parent_die != nullptr) {
2559 if (parent_die->Tag() == DW_TAG_subprogram)
2560 break;
2561 parent_die = parent_die->GetParent();
2562 }
2563 SymbolContext sc_backup = sc;
2564 if (resolve_function_context && parent_die != nullptr &&
2565 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2566 sc = sc_backup;
2567
Konrad Kleine248a1302019-05-23 11:14:47 +00002568 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2570 // Grab the existing type from the master types lists
2571 type_sp = type_ptr->shared_from_this();
2572 }
2573 }
2574 return type_sp;
2575}
2576
2577DWARFDIE
2578SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2579 if (orig_die) {
2580 DWARFDIE die = orig_die;
2581
2582 while (die) {
2583 // If this is the original DIE that we are searching for a declaration
2584 // for, then don't look in the cache as we don't want our own decl
2585 // context to be our decl context...
2586 if (orig_die != die) {
2587 switch (die.Tag()) {
2588 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002589 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 case DW_TAG_namespace:
2591 case DW_TAG_structure_type:
2592 case DW_TAG_union_type:
2593 case DW_TAG_class_type:
2594 case DW_TAG_lexical_block:
2595 case DW_TAG_subprogram:
2596 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002597 case DW_TAG_inlined_subroutine: {
2598 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2599 if (abs_die) {
2600 return abs_die;
2601 }
2602 break;
2603 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002604 default:
2605 break;
2606 }
2607 }
2608
2609 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2610 if (spec_die) {
2611 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2612 if (decl_ctx_die)
2613 return decl_ctx_die;
2614 }
2615
2616 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2617 if (abs_die) {
2618 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2619 if (decl_ctx_die)
2620 return decl_ctx_die;
2621 }
2622
2623 die = die.GetParent();
2624 }
2625 }
2626 return DWARFDIE();
2627}
2628
2629Symbol *
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002630SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002631 Symbol *objc_class_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002632 if (m_obj_file) {
2633 Symtab *symtab = m_obj_file->GetSymtab();
2634 if (symtab) {
2635 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2636 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2637 Symtab::eVisibilityAny);
2638 }
2639 }
2640 return objc_class_symbol;
2641}
2642
2643// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002644// they don't then we can end up looking through all class types for a complete
2645// type and never find the full definition. We need to know if this attribute
2646// is supported, so we determine this here and cache th result. We also need to
2647// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002648// DWARF file
2649// if we are doing darwin DWARF in .o file debugging.
2650bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002651 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2653 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2654 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2655 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2656 else {
2657 DWARFDebugInfo *debug_info = DebugInfo();
2658 const uint32_t num_compile_units = GetNumCompileUnits();
2659 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002660 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002661 if (dwarf_cu != cu &&
2662 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2663 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2664 break;
2665 }
2666 }
2667 }
2668 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2669 GetDebugMapSymfile())
2670 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2671 }
2672 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002673}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002674
2675// This function can be used when a DIE is found that is a forward declaration
2676// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002678 const DWARFDIE &die, ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002679 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002680
Kate Stoneb9c1b512016-09-06 20:57:50 +00002681 TypeSP type_sp;
2682
2683 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2684 return type_sp;
2685
2686 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002687 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002688
2689 const size_t num_matches = die_offsets.size();
2690
2691 if (num_matches) {
2692 for (size_t i = 0; i < num_matches; ++i) {
2693 const DIERef &die_ref = die_offsets[i];
2694 DWARFDIE type_die = GetDIE(die_ref);
2695
2696 if (type_die) {
2697 bool try_resolving_type = false;
2698
Adrian Prantl05097242018-04-30 16:49:04 +00002699 // Don't try and resolve the DIE we are looking for with the DIE
2700 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002701 if (type_die != die) {
2702 switch (type_die.Tag()) {
2703 case DW_TAG_class_type:
2704 case DW_TAG_structure_type:
2705 try_resolving_type = true;
2706 break;
2707 default:
2708 break;
2709 }
2710 }
2711
2712 if (try_resolving_type) {
2713 if (must_be_implementation &&
2714 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2715 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2716 DW_AT_APPLE_objc_complete_type, 0);
2717
2718 if (try_resolving_type) {
2719 Type *resolved_type = ResolveType(type_die, false, true);
2720 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2721 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2722 " (cu 0x%8.8" PRIx64 ")\n",
2723 die.GetID(),
2724 m_obj_file->GetFileSpec().GetFilename().AsCString(
2725 "<Unknown>"),
2726 type_die.GetID(), type_cu->GetID());
2727
2728 if (die)
2729 GetDIEToType()[die.GetDIE()] = resolved_type;
2730 type_sp = resolved_type->shared_from_this();
2731 break;
2732 }
2733 }
2734 }
2735 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002736 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002737 }
2738 }
2739 }
2740 return type_sp;
2741}
Greg Claytona8022fa2012-04-24 21:22:41 +00002742
Adrian Prantl05097242018-04-30 16:49:04 +00002743// This function helps to ensure that the declaration contexts match for two
2744// different DIEs. Often times debug information will refer to a forward
2745// declaration of a type (the equivalent of "struct my_struct;". There will
2746// often be a declaration of that type elsewhere that has the full definition.
2747// When we go looking for the full type "my_struct", we will find one or more
2748// matches in the accelerator tables and we will then need to make sure the
2749// type was in the same declaration context as the original DIE. This function
2750// can efficiently compare two DIEs and will return true when the declaration
2751// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002752bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2753 const DWARFDIE &die2) {
2754 if (die1 == die2)
2755 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002756
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002757 std::vector<DWARFDIE> decl_ctx_1;
2758 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002759 // The declaration DIE stack is a stack of the declaration context DIEs all
2760 // the way back to the compile unit. If a type "T" is declared inside a class
2761 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2762 // namespace "lldb", and the namespace is in a compile unit, there will be a
2763 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002764 //
2765 // [0] DW_TAG_class_type for "B"
2766 // [1] DW_TAG_class_type for "A"
2767 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002768 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002769 //
Adrian Prantl05097242018-04-30 16:49:04 +00002770 // We grab both contexts and make sure that everything matches all the way
2771 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002772
2773 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002774 decl_ctx_1 = die1.GetDeclContextDIEs();
2775 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002776 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002777 const size_t count1 = decl_ctx_1.size();
2778 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002779 if (count1 != count2)
2780 return false;
2781
Adrian Prantl05097242018-04-30 16:49:04 +00002782 // Make sure the DW_TAG values match all the way back up the compile unit. If
2783 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002784 DWARFDIE decl_ctx_die1;
2785 DWARFDIE decl_ctx_die2;
2786 size_t i;
2787 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002788 decl_ctx_die1 = decl_ctx_1[i];
2789 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002790 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2791 return false;
2792 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002793#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002794
Kate Stoneb9c1b512016-09-06 20:57:50 +00002795 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002796 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2797 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2798 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002799 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002800 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2801 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002802
Greg Clayton890ff562012-02-02 05:48:16 +00002803#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002804 // Always skip the compile unit when comparing by only iterating up to "count
2805 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002807 decl_ctx_die1 = decl_ctx_1[i];
2808 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002809 const char *name1 = decl_ctx_die1.GetName();
2810 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002811 // If the string was from a DW_FORM_strp, then the pointer will often be
2812 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 if (name1 == name2)
2814 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002815
Adrian Prantl05097242018-04-30 16:49:04 +00002816 // Name pointers are not equal, so only compare the strings if both are not
2817 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002818 if (name1 && name2) {
2819 // If the strings don't compare, we are done...
2820 if (strcmp(name1, name2) != 0)
2821 return false;
2822 } else {
2823 // One name was NULL while the other wasn't
2824 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002825 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002826 }
Adrian Prantl05097242018-04-30 16:49:04 +00002827 // We made it through all of the checks and the declaration contexts are
2828 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002829 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002830}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002831
Kate Stoneb9c1b512016-09-06 20:57:50 +00002832TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2833 const DWARFDeclContext &dwarf_decl_ctx) {
2834 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002835
Kate Stoneb9c1b512016-09-06 20:57:50 +00002836 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2837 if (dwarf_decl_ctx_count > 0) {
2838 const ConstString type_name(dwarf_decl_ctx[0].name);
2839 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002840
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841 if (type_name) {
2842 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2843 DWARF_LOG_LOOKUPS));
2844 if (log) {
2845 GetObjectFile()->GetModule()->LogMessage(
2846 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2847 "s, qualified-name='%s')",
2848 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2849 dwarf_decl_ctx.GetQualifiedName());
2850 }
2851
2852 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002853 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 const size_t num_matches = die_offsets.size();
2855
Adrian Prantl05097242018-04-30 16:49:04 +00002856 // Get the type system that we are looking to find a type for. We will
2857 // use this to ensure any matches we find are in a language that this
2858 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002859 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2860 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2861 ? nullptr
2862 : GetTypeSystemForLanguage(language);
2863
2864 if (num_matches) {
2865 for (size_t i = 0; i < num_matches; ++i) {
2866 const DIERef &die_ref = die_offsets[i];
2867 DWARFDIE type_die = GetDIE(die_ref);
2868
2869 if (type_die) {
2870 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002871 // looking for. We don't want to find a "Foo" type from Java if we
2872 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002873 if (type_system &&
2874 !type_system->SupportsLanguage(type_die.GetLanguage()))
2875 continue;
2876 bool try_resolving_type = false;
2877
2878 // Don't try and resolve the DIE we are looking for with the DIE
2879 // itself!
2880 const dw_tag_t type_tag = type_die.Tag();
2881 // Make sure the tags match
2882 if (type_tag == tag) {
2883 // The tags match, lets try resolving this type
2884 try_resolving_type = true;
2885 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002886 // The tags don't match, but we need to watch our for a forward
2887 // declaration for a struct and ("struct foo") ends up being a
2888 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 switch (type_tag) {
2890 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002891 // We had a "class foo", see if we ended up with a "struct foo
2892 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002893 try_resolving_type = (tag == DW_TAG_structure_type);
2894 break;
2895 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002896 // We had a "struct foo", see if we ended up with a "class foo
2897 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002898 try_resolving_type = (tag == DW_TAG_class_type);
2899 break;
2900 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002901 // Tags don't match, don't event try to resolve using this type
2902 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 break;
2904 }
2905 }
2906
2907 if (try_resolving_type) {
2908 DWARFDeclContext type_dwarf_decl_ctx;
2909 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2910
2911 if (log) {
2912 GetObjectFile()->GetModule()->LogMessage(
2913 log, "SymbolFileDWARF::"
2914 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2915 "qualified-name='%s') trying die=0x%8.8x (%s)",
2916 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2917 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2918 type_dwarf_decl_ctx.GetQualifiedName());
2919 }
2920
2921 // Make sure the decl contexts match all the way up
2922 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2923 Type *resolved_type = ResolveType(type_die, false);
2924 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2925 type_sp = resolved_type->shared_from_this();
2926 break;
2927 }
2928 }
2929 } else {
2930 if (log) {
2931 std::string qualified_name;
2932 type_die.GetQualifiedName(qualified_name);
2933 GetObjectFile()->GetModule()->LogMessage(
2934 log, "SymbolFileDWARF::"
2935 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2936 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2937 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2938 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2939 qualified_name.c_str());
2940 }
2941 }
2942 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002943 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002944 }
2945 }
2946 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002947 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002948 }
2949 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002950}
2951
Kate Stoneb9c1b512016-09-06 20:57:50 +00002952TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2953 bool *type_is_new_ptr) {
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002954 if (!die)
2955 return {};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002956
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002957 TypeSystem *type_system =
2958 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
2959 if (!type_system)
2960 return {};
Greg Clayton6071e6f2015-08-26 22:57:51 +00002961
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002962 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
2963 if (!dwarf_ast)
2964 return {};
Siva Chandra9293fc42016-01-07 23:32:34 +00002965
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002966 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
2967 TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
2968 if (type_sp) {
Pavel Labathf46e8972019-07-25 08:22:05 +00002969 GetTypeList().Insert(type_sp);
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002970
2971 if (die.Tag() == DW_TAG_subprogram) {
2972 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2973 .GetScopeQualifiedName()
2974 .AsCString(""));
2975 if (scope_qualified_name.size()) {
2976 m_function_scope_qualified_name_map[scope_qualified_name].insert(
2977 die.GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002978 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002980 }
2981
2982 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983}
2984
Kate Stoneb9c1b512016-09-06 20:57:50 +00002985size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
2986 const DWARFDIE &orig_die,
2987 bool parse_siblings, bool parse_children) {
2988 size_t types_added = 0;
2989 DWARFDIE die = orig_die;
2990 while (die) {
2991 bool type_is_new = false;
2992 if (ParseType(sc, die, &type_is_new).get()) {
2993 if (type_is_new)
2994 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002995 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002996
2997 if (parse_children && die.HasChildren()) {
2998 if (die.Tag() == DW_TAG_subprogram) {
2999 SymbolContext child_sc(sc);
3000 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3001 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3002 } else
3003 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3004 }
3005
3006 if (parse_siblings)
3007 die = die.GetSibling();
3008 else
3009 die.Clear();
3010 }
3011 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003012}
3013
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003014size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003015 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003016 CompileUnit *comp_unit = func.GetCompileUnit();
3017 lldbassert(comp_unit);
3018
3019 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3020 if (!dwarf_cu)
3021 return 0;
3022
Kate Stoneb9c1b512016-09-06 20:57:50 +00003023 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003024 const dw_offset_t function_die_offset = func.GetID();
3025 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3026 if (function_die) {
3027 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3028 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003030
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032}
3033
Zachary Turner863f8c12019-01-11 18:03:20 +00003034size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003035 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003036 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003037 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003038 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003039 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3040 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3041 SymbolContext sc;
3042 sc.comp_unit = &comp_unit;
3043 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003045 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003046
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003048}
3049
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003051 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Konrad Kleine248a1302019-05-23 11:14:47 +00003052 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003054 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003055 return 0;
3056
3057 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003058 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003059
3060 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3061 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3062 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3063 const size_t num_variables = ParseVariables(
3064 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3065
3066 // Let all blocks know they have parse all their variables
3067 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3068 return num_variables;
3069 }
3070 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003071 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003072
Konrad Kleine248a1302019-05-23 11:14:47 +00003073 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003074 return 0;
3075
3076 uint32_t vars_added = 0;
3077 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3078
Konrad Kleine248a1302019-05-23 11:14:47 +00003079 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003080 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003081 sc.comp_unit->SetVariableList(variables);
3082
3083 DIEArray die_offsets;
Pavel Labath3b926982019-06-21 07:56:50 +00003084 m_index->GetGlobalVariables(dwarf_cu->GetNonSkeletonUnit(),
3085 die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003086 const size_t num_matches = die_offsets.size();
3087 if (num_matches) {
3088 for (size_t i = 0; i < num_matches; ++i) {
3089 const DIERef &die_ref = die_offsets[i];
3090 DWARFDIE die = GetDIE(die_ref);
3091 if (die) {
3092 VariableSP var_sp(
3093 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3094 if (var_sp) {
3095 variables->AddVariableIfUnique(var_sp);
3096 ++vars_added;
3097 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003098 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00003099 m_index->ReportInvalidDIERef(die_ref, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003102 }
3103 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003105 }
3106 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003107}
3108
Kate Stoneb9c1b512016-09-06 20:57:50 +00003109VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3110 const DWARFDIE &die,
3111 const lldb::addr_t func_low_pc) {
3112 if (die.GetDWARF() != this)
3113 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003114
Kate Stoneb9c1b512016-09-06 20:57:50 +00003115 VariableSP var_sp;
3116 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003117 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003118
Kate Stoneb9c1b512016-09-06 20:57:50 +00003119 var_sp = GetDIEToVariable()[die.GetDIE()];
3120 if (var_sp)
3121 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003122
Kate Stoneb9c1b512016-09-06 20:57:50 +00003123 const dw_tag_t tag = die.Tag();
3124 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003125
Kate Stoneb9c1b512016-09-06 20:57:50 +00003126 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3127 (tag == DW_TAG_formal_parameter && sc.function)) {
3128 DWARFAttributes attributes;
3129 const size_t num_attributes = die.GetAttributes(attributes);
3130 DWARFDIE spec_die;
3131 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003132 const char *name = nullptr;
3133 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003134 Declaration decl;
3135 uint32_t i;
3136 DWARFFormValue type_die_form;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003137 DWARFExpression location;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003138 bool is_external = false;
3139 bool is_artificial = false;
3140 bool location_is_const_value_data = false;
3141 bool has_explicit_location = false;
3142 DWARFFormValue const_value;
3143 Variable::RangeList scope_ranges;
3144 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003145
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 for (i = 0; i < num_attributes; ++i) {
3147 dw_attr_t attr = attributes.AttributeAtIndex(i);
3148 DWARFFormValue form_value;
3149
3150 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3151 switch (attr) {
3152 case DW_AT_decl_file:
3153 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3154 form_value.Unsigned()));
3155 break;
3156 case DW_AT_decl_line:
3157 decl.SetLine(form_value.Unsigned());
3158 break;
3159 case DW_AT_decl_column:
3160 decl.SetColumn(form_value.Unsigned());
3161 break;
3162 case DW_AT_name:
3163 name = form_value.AsCString();
3164 break;
3165 case DW_AT_linkage_name:
3166 case DW_AT_MIPS_linkage_name:
3167 mangled = form_value.AsCString();
3168 break;
3169 case DW_AT_type:
3170 type_die_form = form_value;
3171 break;
3172 case DW_AT_external:
3173 is_external = form_value.Boolean();
3174 break;
3175 case DW_AT_const_value:
3176 // If we have already found a DW_AT_location attribute, ignore this
3177 // attribute.
3178 if (!has_explicit_location) {
3179 location_is_const_value_data = true;
3180 // The constant value will be either a block, a data value or a
3181 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003182 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003183 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3184 // Retrieve the value as a block expression.
3185 uint32_t block_offset =
3186 form_value.BlockData() - debug_info_data.GetDataStart();
3187 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003188 location = DWARFExpression(module, debug_info_data, die.GetCU(),
3189 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003190 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3191 // Retrieve the value as a data expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003192 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003193 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003194 location =
3195 DWARFExpression(module, debug_info_data, die.GetCU(),
3196 data_offset, *data_length);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003197 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003198 const uint8_t *data_pointer = form_value.BlockData();
3199 if (data_pointer) {
3200 form_value.Unsigned();
3201 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3202 // we need to get the byte size of the type later after we
3203 // create the variable
3204 const_value = form_value;
3205 }
Greg Clayton6aad81c2019-05-24 22:08:50 +00003206 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003207 } else {
3208 // Retrieve the value as a string expression.
3209 if (form_value.Form() == DW_FORM_strp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003210 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003211 if (auto data_length = form_value.GetFixedSize())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003212 location =
3213 DWARFExpression(module, debug_info_data, die.GetCU(),
3214 data_offset, *data_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 } else {
3216 const char *str = form_value.AsCString();
3217 uint32_t string_offset =
3218 str - (const char *)debug_info_data.GetDataStart();
3219 uint32_t string_length = strlen(str) + 1;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003220 location =
3221 DWARFExpression(module, debug_info_data, die.GetCU(),
3222 string_offset, string_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003223 }
3224 }
3225 }
3226 break;
3227 case DW_AT_location: {
3228 location_is_const_value_data = false;
3229 has_explicit_location = true;
3230 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003231 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003232
3233 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003234 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003235 uint32_t block_length = form_value.Unsigned();
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003236 location = DWARFExpression(module, data, die.GetCU(),
3237 block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003239 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003240 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3241
3242 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003243 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003244 if (loc_list_length > 0) {
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003245 location = DWARFExpression(module, debug_loc_data, die.GetCU(),
3246 debug_loc_offset, loc_list_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3248 location.SetLocationListSlide(
3249 func_low_pc -
3250 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3251 }
3252 }
3253 } break;
3254 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003255 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003256 break;
Pavel Labath78cfe1e2019-05-29 09:22:36 +00003257 case DW_AT_start_scope:
3258 // TODO: Implement this.
3259 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003260 case DW_AT_artificial:
3261 is_artificial = form_value.Boolean();
3262 break;
3263 case DW_AT_accessibility:
3264 break; // accessibility =
3265 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3266 case DW_AT_declaration:
3267 case DW_AT_description:
3268 case DW_AT_endianity:
3269 case DW_AT_segment:
3270 case DW_AT_visibility:
3271 default:
3272 case DW_AT_abstract_origin:
3273 case DW_AT_sibling:
3274 break;
3275 }
3276 }
3277 }
3278
3279 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3280 const dw_tag_t parent_tag = die.GetParent().Tag();
3281 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003282 (parent_tag == DW_TAG_compile_unit ||
3283 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003284 (parent_context_die.Tag() == DW_TAG_class_type ||
3285 parent_context_die.Tag() == DW_TAG_structure_type);
3286
3287 ValueType scope = eValueTypeInvalid;
3288
3289 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003290 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003291
Sam McCall30621392016-11-22 11:40:25 +00003292 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293 if (!mangled) {
3294 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003295 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3296 // of global variables with commands like "frame var j". For
3297 // example, if j were an int variable holding a value 4 and
3298 // declared in a namespace B which in turn is contained in a
3299 // namespace A, the command "frame var j" returns
3300 // "(int) A::B::j = 4".
3301 // If the compiler does not emit a linkage name, we should be
3302 // able to generate a fully qualified name from the
3303 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003304 if ((parent_tag == DW_TAG_compile_unit ||
3305 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003306 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3307 DWARFDeclContext decl_ctx;
3308
3309 die.GetDWARFDeclContext(decl_ctx);
3310 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3311 }
3312 }
3313
Kate Stoneb9c1b512016-09-06 20:57:50 +00003314 if (tag == DW_TAG_formal_parameter)
3315 scope = eValueTypeVariableArgument;
3316 else {
Sam McCall30621392016-11-22 11:40:25 +00003317 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3318 // or static variable, so we have to do a little digging:
3319 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3320 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3321 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3322 // Clang likes to combine small global variables into the same symbol
3323 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3324 // so we need to look through the whole expression.
3325 bool is_static_lifetime =
3326 has_explicit_mangled ||
3327 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003328 // Check if the location has a DW_OP_addr with any address value...
3329 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3330 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003331 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003332 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3333 if (op_error) {
3334 StreamString strm;
3335 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003336 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003337 GetObjectFile()->GetModule()->ReportError(
3338 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003339 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003340 }
Sam McCall30621392016-11-22 11:40:25 +00003341 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3342 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003343 }
3344 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003345 if (debug_map_symfile)
3346 // Set the module of the expression to the linked module
3347 // instead of the oject file so the relocated address can be
3348 // found there.
3349 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003350
Sam McCall30621392016-11-22 11:40:25 +00003351 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003352 if (is_external)
3353 scope = eValueTypeVariableGlobal;
3354 else
3355 scope = eValueTypeVariableStatic;
3356
3357 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003358 // When leaving the DWARF in the .o files on darwin, when we have a
3359 // global variable that wasn't initialized, the .o file might not
3360 // have allocated a virtual address for the global variable. In
3361 // this case it will have created a symbol for the global variable
3362 // that is undefined/data and external and the value will be the
3363 // byte size of the variable. When we do the address map in
3364 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3365 // do some magic here so we can get the correct address for our
3366 // global variable. The address for all of these entries will be
3367 // zero, and there will be an undefined symbol in this object file,
3368 // and the executable will have a matching symbol with a good
3369 // address. So here we dig up the correct address and replace it in
3370 // the location for the variable, and set the variable's symbol
3371 // context scope to be that of the main executable so the file
3372 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373 bool linked_oso_file_addr = false;
3374 if (is_external && location_DW_OP_addr == 0) {
3375 // we have a possible uninitialized extern global
3376 ConstString const_name(mangled ? mangled : name);
3377 ObjectFile *debug_map_objfile =
3378 debug_map_symfile->GetObjectFile();
3379 if (debug_map_objfile) {
3380 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3381 if (debug_map_symtab) {
3382 Symbol *exe_symbol =
3383 debug_map_symtab->FindFirstSymbolWithNameAndType(
3384 const_name, eSymbolTypeData, Symtab::eDebugYes,
3385 Symtab::eVisibilityExtern);
3386 if (exe_symbol) {
3387 if (exe_symbol->ValueIsAddress()) {
3388 const addr_t exe_file_addr =
3389 exe_symbol->GetAddressRef().GetFileAddress();
3390 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3391 if (location.Update_DW_OP_addr(exe_file_addr)) {
3392 linked_oso_file_addr = true;
3393 symbol_context_scope = exe_symbol;
3394 }
3395 }
3396 }
3397 }
3398 }
3399 }
3400 }
3401
3402 if (!linked_oso_file_addr) {
3403 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003404 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003405 const lldb::addr_t exe_file_addr =
3406 debug_map_symfile->LinkOSOFileAddress(this,
3407 location_DW_OP_addr);
3408 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3409 // Update the file address for this variable
3410 location.Update_DW_OP_addr(exe_file_addr);
3411 } else {
3412 // Variable didn't make it into the final executable
3413 return var_sp;
3414 }
3415 }
3416 }
3417 } else {
3418 if (location_is_const_value_data)
3419 scope = eValueTypeVariableStatic;
3420 else {
3421 scope = eValueTypeVariableLocal;
3422 if (debug_map_symfile) {
3423 // We need to check for TLS addresses that we need to fixup
3424 if (location.ContainsThreadLocalStorage()) {
3425 location.LinkThreadLocalStorage(
3426 debug_map_symfile->GetObjectFile()->GetModule(),
3427 [this, debug_map_symfile](
3428 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3429 return debug_map_symfile->LinkOSOFileAddress(
3430 this, unlinked_file_addr);
3431 });
3432 scope = eValueTypeVariableThreadLocal;
3433 }
3434 }
3435 }
3436 }
3437 }
3438
Konrad Kleine248a1302019-05-23 11:14:47 +00003439 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003440 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003441 case DW_TAG_subprogram:
3442 case DW_TAG_inlined_subroutine:
3443 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003444 if (sc.function) {
3445 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3446 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003447 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003448 symbol_context_scope = sc.function;
3449 }
3450 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003451
Kate Stoneb9c1b512016-09-06 20:57:50 +00003452 default:
3453 symbol_context_scope = sc.comp_unit;
3454 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003455 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003456 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003457
Kate Stoneb9c1b512016-09-06 20:57:50 +00003458 if (symbol_context_scope) {
3459 SymbolFileTypeSP type_sp(
Pavel Labath67b45ac2019-06-19 07:32:39 +00003460 new SymbolFileType(*this, GetUID(type_die_form.Reference())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003461
3462 if (const_value.Form() && type_sp && type_sp->GetType())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003463 location.UpdateValue(const_value.Unsigned(),
3464 type_sp->GetType()->GetByteSize().getValueOr(0),
3465 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003466
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003467 var_sp = std::make_shared<Variable>(
3468 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3469 scope_ranges, &decl, location, is_external, is_artificial,
3470 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003471
3472 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3473 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003474 // Not ready to parse this variable yet. It might be a global or static
3475 // variable that is in a function scope and the function in the symbol
3476 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003477 return var_sp;
3478 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003479 }
Adrian Prantl05097242018-04-30 16:49:04 +00003480 // Cache var_sp even if NULL (the variable was just a specification or was
3481 // missing vital information to be able to be displayed in the debugger
3482 // (missing location due to optimization, etc)) so we don't re-parse this
3483 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003484 GetDIEToVariable()[die.GetDIE()] = var_sp;
3485 if (spec_die)
3486 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3487 }
3488 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003489}
3490
Kate Stoneb9c1b512016-09-06 20:57:50 +00003491DWARFDIE
3492SymbolFileDWARF::FindBlockContainingSpecification(
3493 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3494 // Give the concrete function die specified by "func_die_offset", find the
3495 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3496 // to "spec_block_die_offset"
3497 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3498 spec_block_die_offset);
3499}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003500
Kate Stoneb9c1b512016-09-06 20:57:50 +00003501DWARFDIE
3502SymbolFileDWARF::FindBlockContainingSpecification(
3503 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3504 if (die) {
3505 switch (die.Tag()) {
3506 case DW_TAG_subprogram:
3507 case DW_TAG_inlined_subroutine:
3508 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003509 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3510 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003511 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003512
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003513 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003514 spec_block_die_offset)
3515 return die;
3516 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003518
3519 // Give the concrete function die specified by "func_die_offset", find the
3520 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3521 // to "spec_block_die_offset"
3522 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3523 child_die = child_die.GetSibling()) {
3524 DWARFDIE result_die =
3525 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3526 if (result_die)
3527 return result_die;
3528 }
3529 }
3530
3531 return DWARFDIE();
3532}
3533
3534size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3535 const DWARFDIE &orig_die,
3536 const lldb::addr_t func_low_pc,
3537 bool parse_siblings, bool parse_children,
3538 VariableList *cc_variable_list) {
3539 if (!orig_die)
3540 return 0;
3541
3542 VariableListSP variable_list_sp;
3543
3544 size_t vars_added = 0;
3545 DWARFDIE die = orig_die;
3546 while (die) {
3547 dw_tag_t tag = die.Tag();
3548
3549 // Check to see if we have already parsed this variable or constant?
3550 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3551 if (var_sp) {
3552 if (cc_variable_list)
3553 cc_variable_list->AddVariableIfUnique(var_sp);
3554 } else {
3555 // We haven't already parsed it, lets do that now.
3556 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3557 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003558 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003559 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3560 dw_tag_t parent_tag = sc_parent_die.Tag();
3561 switch (parent_tag) {
3562 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003563 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003564 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003565 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003566 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003567 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568 }
3569 } else {
3570 GetObjectFile()->GetModule()->ReportError(
3571 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3572 "symbol context for 0x%8.8" PRIx64
3573 " %s.\n",
3574 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3575 orig_die.GetID(), orig_die.GetTagAsCString());
3576 }
3577 break;
3578
3579 case DW_TAG_subprogram:
3580 case DW_TAG_inlined_subroutine:
3581 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003582 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003583 // Check to see if we already have parsed the variables for the
3584 // given scope
3585
3586 Block *block = sc.function->GetBlock(true).FindBlockByID(
3587 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003588 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003589 // This must be a specification or abstract origin with a
3590 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591 // to find the concrete block so we can correctly add the
3592 // variable to it
3593 const DWARFDIE concrete_block_die =
3594 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003595 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003596 sc_parent_die.GetOffset());
3597 if (concrete_block_die)
3598 block = sc.function->GetBlock(true).FindBlockByID(
3599 concrete_block_die.GetID());
3600 }
3601
Konrad Kleine248a1302019-05-23 11:14:47 +00003602 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003603 const bool can_create = false;
3604 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003605 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003606 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003607 block->SetVariableList(variable_list_sp);
3608 }
3609 }
3610 }
3611 break;
3612
3613 default:
3614 GetObjectFile()->GetModule()->ReportError(
3615 "didn't find appropriate parent DIE for variable list for "
3616 "0x%8.8" PRIx64 " %s.\n",
3617 orig_die.GetID(), orig_die.GetTagAsCString());
3618 break;
3619 }
3620 }
3621
3622 if (variable_list_sp) {
3623 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3624 if (var_sp) {
3625 variable_list_sp->AddVariableIfUnique(var_sp);
3626 if (cc_variable_list)
3627 cc_variable_list->AddVariableIfUnique(var_sp);
3628 ++vars_added;
3629 }
3630 }
3631 }
3632 }
3633
Konrad Kleine248a1302019-05-23 11:14:47 +00003634 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003635
3636 if (!skip_children && parse_children && die.HasChildren()) {
3637 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3638 true, cc_variable_list);
3639 }
3640
3641 if (parse_siblings)
3642 die = die.GetSibling();
3643 else
3644 die.Clear();
3645 }
3646 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003647}
3648
Vedant Kumar4b36f792018-10-05 23:23:15 +00003649/// Collect call graph edges present in a function DIE.
3650static std::vector<lldb_private::CallEdge>
3651CollectCallEdges(DWARFDIE function_die) {
3652 // Check if the function has a supported call site-related attribute.
3653 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3654 uint64_t has_call_edges =
3655 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3656 if (!has_call_edges)
3657 return {};
3658
3659 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3660 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3661 function_die.GetPubname());
3662
3663 // Scan the DIE for TAG_call_site entries.
3664 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3665 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3666 // For now, assume that all entries are nested directly under the subprogram
3667 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3668 std::vector<CallEdge> call_edges;
3669 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3670 child = child.GetSibling()) {
3671 if (child.Tag() != DW_TAG_call_site)
3672 continue;
3673
3674 // Extract DW_AT_call_origin (the call target's DIE).
3675 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3676 if (!call_origin.IsValid()) {
3677 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3678 function_die.GetPubname());
3679 continue;
3680 }
3681
3682 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3683 // available. It should only ever be unavailable for tail call edges, in
3684 // which case use LLDB_INVALID_ADDRESS.
3685 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3686 LLDB_INVALID_ADDRESS);
3687
3688 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3689 call_origin.GetPubname(), return_pc);
3690 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3691 }
3692 return call_edges;
3693}
3694
3695std::vector<lldb_private::CallEdge>
3696SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003697 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003698 if (func_die.IsValid())
3699 return CollectCallEdges(func_die);
3700 return {};
3701}
3702
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003703// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3705
3706uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3707
Pavel Labathe0119902019-07-23 09:24:02 +00003708void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
3709 SymbolFile::Dump(s);
3710 m_index->Dump(s);
3711}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003712
Zachary Turner49110232018-11-05 17:40:28 +00003713void SymbolFileDWARF::DumpClangAST(Stream &s) {
3714 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3715 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3716 if (!clang)
3717 return;
3718 clang->Dump(s);
3719}
3720
Kate Stoneb9c1b512016-09-06 20:57:50 +00003721SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003722 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003723 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3724 if (module_sp) {
3725 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3726 if (sym_vendor)
3727 m_debug_map_symfile =
3728 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003729 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003730 }
3731 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003732}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003733
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003734DWARFExpression::LocationListFormat
3735SymbolFileDWARF::GetLocationListFormat() const {
3736 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003737 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003738 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003739}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003740
3741SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3742 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003743 ModuleSpec module_spec;
3744 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3745 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003746 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003747
3748 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3749 FileSpec dwp_filespec =
3750 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003751 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003752 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3753 dwp_filespec);
3754 }
3755 });
3756 return m_dwp_symfile.get();
3757}