blob: f625e2bdedc123335c311410cf787151c65d23fb [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"
Pavel Labath465eae32019-08-06 09:12:42 +000046#include "lldb/Symbol/SymbolFile.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"
Siva Chandrad8335e92015-12-16 00:22:08 +000061#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000063#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064#include "DWARFFormValue.h"
Pavel Labathca9c3de2019-06-12 11:29:50 +000065#include "DWARFTypeUnit.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000066#include "DWARFUnit.h"
Pavel Labath9337b412018-06-06 11:35:23 +000067#include "DebugNamesDWARFIndex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068#include "LogChannelDWARF.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000069#include "ManualDWARFIndex.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000070#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000071#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000072#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073
Jonas Devlieghere235339352019-08-13 19:51:51 +000074#include "llvm/DebugInfo/DWARF/DWARFContext.h"
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
Jonas Devlieghere235339352019-08-13 19:51:51 +0000156} // namespace
157
158static const llvm::DWARFDebugLine::LineTable *
159ParseLLVMLineTable(lldb_private::DWARFContext &context,
160 llvm::DWARFDebugLine &line, dw_offset_t line_offset,
161 dw_offset_t unit_offset) {
162 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
163
164 llvm::DWARFDataExtractor data = context.getOrLoadLineData().GetAsLLVM();
165 llvm::DWARFContext &ctx = context.GetAsLLVM();
166 llvm::Expected<const llvm::DWARFDebugLine::LineTable *> line_table =
167 line.getOrParseLineTable(
168 data, line_offset, ctx, nullptr, [&](llvm::Error e) {
169 LLDB_LOG_ERROR(log, std::move(e),
170 "SymbolFileDWARF::ParseLineTable failed to parse");
171 });
172
173 if (!line_table) {
174 LLDB_LOG_ERROR(log, line_table.takeError(),
175 "SymbolFileDWARF::ParseLineTable failed to parse");
176 return nullptr;
177 }
178 return *line_table;
179}
180
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000181static FileSpecList ParseSupportFilesFromPrologue(
182 const lldb::ModuleSP &module,
183 const llvm::DWARFDebugLine::Prologue &prologue, FileSpec::Style style,
184 llvm::StringRef compile_dir = {}, FileSpec first_file = {}) {
Jonas Devlieghere235339352019-08-13 19:51:51 +0000185 FileSpecList support_files;
186 support_files.Append(first_file);
187
188 const size_t number_of_files = prologue.FileNames.size();
189 for (size_t idx = 1; idx <= number_of_files; ++idx) {
190 std::string original_file;
191 if (!prologue.getFileNameByIndex(
192 idx, compile_dir,
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000193 llvm::DILineInfoSpecifier::FileLineInfoKind::Default, original_file,
194 style)) {
Jonas Devlieghere235339352019-08-13 19:51:51 +0000195 // Always add an entry so the indexes remain correct.
196 support_files.EmplaceBack();
197 continue;
198 }
199
200 std::string remapped_file;
201 if (!prologue.getFileNameByIndex(
202 idx, compile_dir,
203 llvm::DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000204 remapped_file, style)) {
Jonas Devlieghere235339352019-08-13 19:51:51 +0000205 // Always add an entry so the indexes remain correct.
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000206 support_files.EmplaceBack(original_file, style);
Jonas Devlieghere235339352019-08-13 19:51:51 +0000207 continue;
208 }
209
210 module->RemapSourceFile(llvm::StringRef(original_file), remapped_file);
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000211 support_files.EmplaceBack(remapped_file, style);
Jonas Devlieghere235339352019-08-13 19:51:51 +0000212 }
213
214 return support_files;
215}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000216
Frederic Rissacbf0052019-04-23 20:17:04 +0000217FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
Pavel Labath7d36d722019-01-16 12:30:41 +0000218 return GetGlobalPluginProperties()->GetSymLinkPaths();
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221void SymbolFileDWARF::Initialize() {
222 LogChannelDWARF::Initialize();
223 PluginManager::RegisterPlugin(GetPluginNameStatic(),
224 GetPluginDescriptionStatic(), CreateInstance,
225 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000226}
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
229 if (!PluginManager::GetSettingForSymbolFilePlugin(
230 debugger, PluginProperties::GetSettingName())) {
231 const bool is_global_setting = true;
232 PluginManager::CreateSettingForSymbolFilePlugin(
233 debugger, GetGlobalPluginProperties()->GetValueProperties(),
234 ConstString("Properties for the dwarf symbol-file plug-in."),
235 is_global_setting);
236 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239void SymbolFileDWARF::Terminate() {
240 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000241 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
245 static ConstString g_name("dwarf");
246 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
250 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Pavel Labathd2deeb42019-07-31 08:25:25 +0000253SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFileSP objfile_sp) {
254 return new SymbolFileDWARF(std::move(objfile_sp),
Pavel Labathff9b4262019-05-17 08:26:58 +0000255 /*dwo_section_list*/ nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
Pavel Labathf46e8972019-07-25 08:22:05 +0000258TypeList &SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000259 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf46e8972019-07-25 08:22:05 +0000260 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 return debug_map_symfile->GetTypeList();
Pavel Labathf46e8972019-07-25 08:22:05 +0000262 return SymbolFile::GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000263}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
265 dw_offset_t max_die_offset, uint32_t type_mask,
266 TypeSet &type_set) {
267 if (die) {
268 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 if (die_offset >= max_die_offset)
271 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (die_offset >= min_die_offset) {
274 const dw_tag_t tag = die.Tag();
275
276 bool add_type = false;
277
278 switch (tag) {
279 case DW_TAG_array_type:
280 add_type = (type_mask & eTypeClassArray) != 0;
281 break;
282 case DW_TAG_unspecified_type:
283 case DW_TAG_base_type:
284 add_type = (type_mask & eTypeClassBuiltin) != 0;
285 break;
286 case DW_TAG_class_type:
287 add_type = (type_mask & eTypeClassClass) != 0;
288 break;
289 case DW_TAG_structure_type:
290 add_type = (type_mask & eTypeClassStruct) != 0;
291 break;
292 case DW_TAG_union_type:
293 add_type = (type_mask & eTypeClassUnion) != 0;
294 break;
295 case DW_TAG_enumeration_type:
296 add_type = (type_mask & eTypeClassEnumeration) != 0;
297 break;
298 case DW_TAG_subroutine_type:
299 case DW_TAG_subprogram:
300 case DW_TAG_inlined_subroutine:
301 add_type = (type_mask & eTypeClassFunction) != 0;
302 break;
303 case DW_TAG_pointer_type:
304 add_type = (type_mask & eTypeClassPointer) != 0;
305 break;
306 case DW_TAG_rvalue_reference_type:
307 case DW_TAG_reference_type:
308 add_type = (type_mask & eTypeClassReference) != 0;
309 break;
310 case DW_TAG_typedef:
311 add_type = (type_mask & eTypeClassTypedef) != 0;
312 break;
313 case DW_TAG_ptr_to_member_type:
314 add_type = (type_mask & eTypeClassMemberPointer) != 0;
315 break;
316 }
317
318 if (add_type) {
319 const bool assert_not_being_parsed = true;
320 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
321 if (type) {
322 if (type_set.find(type) == type_set.end())
323 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000324 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000326 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327
328 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
329 child_die = child_die.GetSibling()) {
330 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
331 }
332 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000333}
334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000336 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000337
338{
Pavel Labath656ddeb2019-07-30 08:20:05 +0000339 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000341
Konrad Kleine248a1302019-05-23 11:14:47 +0000342 CompileUnit *comp_unit = nullptr;
343 DWARFUnit *dwarf_cu = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (sc_scope)
345 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 if (comp_unit) {
348 dwarf_cu = GetDWARFCompileUnit(comp_unit);
Konrad Kleine248a1302019-05-23 11:14:47 +0000349 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 return 0;
351 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
Jan Kratochvil60562732019-05-10 17:14:37 +0000352 dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 } else {
354 DWARFDebugInfo *info = DebugInfo();
355 if (info) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000356 const size_t num_cus = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +0000358 dwarf_cu = info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 if (dwarf_cu) {
360 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000361 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000363 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 }
365
366 std::set<CompilerType> compiler_type_set;
367 size_t num_types_added = 0;
368 for (Type *type : type_set) {
369 CompilerType compiler_type = type->GetForwardCompilerType();
370 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
371 compiler_type_set.insert(compiler_type);
372 type_list.Insert(type->shared_from_this());
373 ++num_types_added;
374 }
375 }
376 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000377}
378
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379// Gets the first parent that is a lexical block, function or inlined
380// subroutine, or compile unit.
Greg Clayton6071e6f2015-08-26 22:57:51 +0000381DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
383 DWARFDIE die;
384 for (die = child_die.GetParent(); die; die = die.GetParent()) {
385 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 switch (tag) {
388 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000389 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 case DW_TAG_subprogram:
391 case DW_TAG_inlined_subroutine:
392 case DW_TAG_lexical_block:
393 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 }
396 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397}
398
Pavel Labathd2deeb42019-07-31 08:25:25 +0000399SymbolFileDWARF::SymbolFileDWARF(ObjectFileSP objfile_sp,
Pavel Labathff9b4262019-05-17 08:26:58 +0000400 SectionList *dwo_section_list)
Pavel Labathd2deeb42019-07-31 08:25:25 +0000401 : SymbolFile(std::move(objfile_sp)),
Pavel Labathf4014e12019-05-16 11:07:58 +0000402 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
403 // when this class parses .o files to
404 // contain the .o file index/ID
Konrad Kleine248a1302019-05-23 11:14:47 +0000405 m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
Pavel Labathd2deeb42019-07-31 08:25:25 +0000406 m_context(m_objfile_sp->GetModule()->GetSectionList(), dwo_section_list),
Pavel Labath00e39682019-06-14 14:12:25 +0000407 m_data_debug_loc(), m_abbr(), m_info(), m_fetched_external_modules(false),
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000408 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411SymbolFileDWARF::~SymbolFileDWARF() {}
412
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000413static ConstString GetDWARFMachOSegmentName() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 static ConstString g_dwarf_section_name("__DWARF");
415 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416}
417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
419 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
420 if (debug_map_symfile)
421 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
422 else
423 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000424}
425
Alex Langford0e252e32019-07-30 22:12:34 +0000426llvm::Expected<TypeSystem &>
427SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
428 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
429 return debug_map_symfile->GetTypeSystemForLanguage(language);
430
431 auto type_system_or_err =
Pavel Labathd2deeb42019-07-31 08:25:25 +0000432 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
Alex Langford0e252e32019-07-30 22:12:34 +0000433 if (type_system_or_err) {
434 type_system_or_err->SetSymbolFile(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 }
Alex Langford0e252e32019-07-30 22:12:34 +0000436 return type_system_or_err;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000440 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441
Pavel Labath9337b412018-06-06 11:35:23 +0000442 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
443 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
444 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
445 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
446 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
447 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448
Pavel Labath9337b412018-06-06 11:35:23 +0000449 m_index = AppleDWARFIndex::Create(
450 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
Pavel Labath0261b942019-05-20 08:38:47 +0000451 apple_types, apple_objc, m_context.getOrLoadStrData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452
Pavel Labath9337b412018-06-06 11:35:23 +0000453 if (m_index)
454 return;
455
456 DWARFDataExtractor debug_names;
457 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
458 if (debug_names.GetByteSize() > 0) {
459 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
Pavel Labath0261b942019-05-20 08:38:47 +0000460 DebugNamesDWARFIndex::Create(
461 *GetObjectFile()->GetModule(), debug_names,
462 m_context.getOrLoadStrData(), DebugInfo());
Pavel Labath9337b412018-06-06 11:35:23 +0000463 if (index_or) {
464 m_index = std::move(*index_or);
465 return;
466 }
467 LLDB_LOG_ERROR(log, index_or.takeError(),
468 "Unable to read .debug_names data: {0}");
469 }
470 }
471
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000472 m_index = std::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
Pavel Labath9337b412018-06-06 11:35:23 +0000473 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474}
475
476bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000477 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478}
479
480uint32_t SymbolFileDWARF::CalculateAbilities() {
481 uint32_t abilities = 0;
Pavel Labathd2deeb42019-07-31 08:25:25 +0000482 if (m_objfile_sp != nullptr) {
Konrad Kleine248a1302019-05-23 11:14:47 +0000483 const Section *section = nullptr;
Pavel Labathd2deeb42019-07-31 08:25:25 +0000484 const SectionList *section_list = m_objfile_sp->GetSectionList();
Konrad Kleine248a1302019-05-23 11:14:47 +0000485 if (section_list == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 return 0;
487
488 uint64_t debug_abbrev_file_size = 0;
489 uint64_t debug_info_file_size = 0;
490 uint64_t debug_line_file_size = 0;
491
492 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
493
494 if (section)
495 section_list = &section->GetChildren();
496
497 section =
498 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
Konrad Kleine248a1302019-05-23 11:14:47 +0000499 if (section != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 debug_info_file_size = section->GetFileSize();
501
502 section =
503 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
504 .get();
505 if (section)
506 debug_abbrev_file_size = section->GetFileSize();
507
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000508 DWARFDebugAbbrev *abbrev = DebugAbbrev();
509 if (abbrev) {
510 std::set<dw_form_t> invalid_forms;
511 abbrev->GetUnsupportedForms(invalid_forms);
512 if (!invalid_forms.empty()) {
513 StreamString error;
Jonas Devlieghere235339352019-08-13 19:51:51 +0000514 error.Printf("unsupported DW_FORM value%s:",
515 invalid_forms.size() > 1 ? "s" : "");
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000516 for (auto form : invalid_forms)
517 error.Printf(" %#x", form);
Pavel Labathd2deeb42019-07-31 08:25:25 +0000518 m_objfile_sp->GetModule()->ReportWarning(
519 "%s", error.GetString().str().c_str());
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000520 return 0;
521 }
522 }
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 section =
525 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
526 .get();
527 if (section)
528 debug_line_file_size = section->GetFileSize();
529 } else {
530 const char *symfile_dir_cstr =
Pavel Labathd2deeb42019-07-31 08:25:25 +0000531 m_objfile_sp->GetFileSpec().GetDirectory().GetCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 if (symfile_dir_cstr) {
533 if (strcasestr(symfile_dir_cstr, ".dsym")) {
Pavel Labathd2deeb42019-07-31 08:25:25 +0000534 if (m_objfile_sp->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000535 // We have a dSYM file that didn't have a any debug info. If the
536 // string table has a size of 1, then it was made from an
537 // executable with no debug info, or from an executable that was
538 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 section =
540 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
541 .get();
542 if (section && section->GetFileSize() == 1) {
Pavel Labathd2deeb42019-07-31 08:25:25 +0000543 m_objfile_sp->GetModule()->ReportWarning(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 "empty dSYM file detected, dSYM was created with an "
545 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000546 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551
552 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
553 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
554 LocalVariables | VariableTypes;
555
556 if (debug_line_file_size > 0)
557 abilities |= LineTables;
558 }
559 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562const DWARFDataExtractor &
563SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
564 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000565 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
566 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
567 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569}
570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
572 DWARFDataExtractor &data) {
Pavel Labathd2deeb42019-07-31 08:25:25 +0000573 ModuleSP module_sp(m_objfile_sp->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 const SectionList *section_list = module_sp->GetSectionList();
Zachary Turner6e665122019-03-20 20:49:25 +0000575 if (!section_list)
576 return;
577
578 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
579 if (!section_sp)
580 return;
581
582 data.Clear();
Pavel Labathd2deeb42019-07-31 08:25:25 +0000583 m_objfile_sp->ReadSectionData(section_sp.get(), data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000586const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
587 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
588 if (debugLocData.GetByteSize() > 0)
589 return debugLocData;
590 return get_debug_loclists_data();
591}
592
593const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
594 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
595}
596
597const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
598 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
599 m_data_debug_loclists);
600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000603 if (m_abbr)
604 return m_abbr.get();
605
Pavel Labath0261b942019-05-20 08:38:47 +0000606 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000607 if (debug_abbrev_data.GetByteSize() == 0)
608 return nullptr;
609
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000610 auto abbr = std::make_unique<DWARFDebugAbbrev>();
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000611 llvm::Error error = abbr->parse(debug_abbrev_data);
612 if (error) {
613 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
614 LLDB_LOG_ERROR(log, std::move(error),
615 "Unable to read .debug_abbrev section: {0}");
616 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 }
Zachary Turnerbb26a7e2019-03-14 19:05:55 +0000618
619 m_abbr = std::move(abbr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
624 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625}
626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
Konrad Kleine248a1302019-05-23 11:14:47 +0000628 if (m_info == nullptr) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000629 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
630 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
631 static_cast<void *>(this));
Pavel Labath6a2eb362019-06-14 13:01:16 +0000632 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0)
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000633 m_info = std::make_unique<DWARFDebugInfo>(*this, m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 }
635 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636}
637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
639 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640}
641
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000642DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
644 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000645 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646
Alex Langford5ada8872019-06-08 00:55:03 +0000647 DWARFDebugInfo *info = DebugInfo();
648 if (info) {
649 // The compile unit ID is the index of the DWARF unit.
650 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
651 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
652 dwarf_cu->SetUserData(comp_unit);
653 return dwarf_cu;
654 }
655 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656}
657
Pavel Labathd67b5502019-07-26 13:15:28 +0000658DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000659 if (!m_ranges) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000660 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
661 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
662 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000663
Pavel Labath00e39682019-06-14 14:12:25 +0000664 if (m_context.getOrLoadRangesData().GetByteSize() > 0)
Pavel Labath4c2ef9a2019-03-22 16:07:58 +0000665 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000666
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000667 if (m_ranges)
Pavel Labath00e39682019-06-14 14:12:25 +0000668 m_ranges->Extract(m_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670 return m_ranges.get();
671}
672
Pavel Labathd67b5502019-07-26 13:15:28 +0000673DWARFDebugRngLists *SymbolFileDWARF::GetDebugRngLists() {
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000674 if (!m_rnglists) {
675 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
676 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
677 static_cast<void *>(this));
678
Pavel Labath00e39682019-06-14 14:12:25 +0000679 if (m_context.getOrLoadRngListsData().GetByteSize() > 0)
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000680 m_rnglists.reset(new DWARFDebugRngLists());
681
682 if (m_rnglists)
Pavel Labath00e39682019-06-14 14:12:25 +0000683 m_rnglists->Extract(m_context);
Pavel Labath78cfe1e2019-05-29 09:22:36 +0000684 }
685 return m_rnglists.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686}
687
Pavel Labathad17e282019-06-13 11:22:47 +0000688lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689 CompUnitSP cu_sp;
Pavel Labathad17e282019-06-13 11:22:47 +0000690 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
691 if (comp_unit) {
692 // We already parsed this compile unit, had out a shared pointer to it
693 cu_sp = comp_unit->shared_from_this();
694 } else {
Pavel Labath6a2eb362019-06-14 13:01:16 +0000695 if (&dwarf_cu.GetSymbolFileDWARF() != this) {
696 return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu);
Pavel Labathad17e282019-06-13 11:22:47 +0000697 } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
698 // Let the debug map create the compile unit
699 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
700 dwarf_cu.SetUserData(cu_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701 } else {
Pavel Labathd2deeb42019-07-31 08:25:25 +0000702 ModuleSP module_sp(m_objfile_sp->GetModule());
Pavel Labathad17e282019-06-13 11:22:47 +0000703 if (module_sp) {
704 const DWARFDIE cu_die = dwarf_cu.DIE();
705 if (cu_die) {
706 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
707 if (cu_file_spec) {
708 // If we have a full path to the compile unit, we don't need to
709 // resolve the file. This can be expensive e.g. when the source
710 // files are NFS mounted.
711 cu_file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712
Pavel Labathad17e282019-06-13 11:22:47 +0000713 std::string remapped_file;
714 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
715 remapped_file))
716 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 }
Pavel Labathad17e282019-06-13 11:22:47 +0000718
719 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
720 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
721
722 bool is_optimized = dwarf_cu.GetIsOptimized();
723 BuildCuTranslationTable();
724 cu_sp = std::make_shared<CompileUnit>(
725 module_sp, &dwarf_cu, cu_file_spec,
726 *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
727 is_optimized ? eLazyBoolYes : eLazyBoolNo);
728
729 dwarf_cu.SetUserData(cu_sp.get());
730
Pavel Labathe0119902019-07-23 09:24:02 +0000731 SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 }
733 }
734 }
735 }
736 return cu_sp;
737}
738
Pavel Labathad17e282019-06-13 11:22:47 +0000739void SymbolFileDWARF::BuildCuTranslationTable() {
740 if (!m_lldb_cu_to_dwarf_unit.empty())
741 return;
742
743 DWARFDebugInfo *info = DebugInfo();
744 if (!info)
745 return;
746
747 if (!info->ContainsTypeUnits()) {
748 // We can use a 1-to-1 mapping. No need to build a translation table.
749 return;
750 }
751 for (uint32_t i = 0, num = info->GetNumUnits(); i < num; ++i) {
752 if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info->GetUnitAtIndex(i))) {
753 cu->SetID(m_lldb_cu_to_dwarf_unit.size());
754 m_lldb_cu_to_dwarf_unit.push_back(i);
755 }
756 }
757}
758
759llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
760 BuildCuTranslationTable();
761 if (m_lldb_cu_to_dwarf_unit.empty())
762 return cu_idx;
763 if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
764 return llvm::None;
765 return m_lldb_cu_to_dwarf_unit[cu_idx];
766}
767
Pavel Labathe0119902019-07-23 09:24:02 +0000768uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000770 if (!info)
771 return 0;
772 BuildCuTranslationTable();
773 return m_lldb_cu_to_dwarf_unit.empty() ? info->GetNumUnits()
774 : m_lldb_cu_to_dwarf_unit.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775}
776
777CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000778 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 DWARFDebugInfo *info = DebugInfo();
Pavel Labathad17e282019-06-13 11:22:47 +0000780 if (!info)
781 return {};
782
783 if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
784 if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
785 info->GetUnitAtIndex(*dwarf_idx)))
786 return ParseCompileUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 }
Pavel Labathad17e282019-06-13 11:22:47 +0000788 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789}
790
Zachary Turner863f8c12019-01-11 18:03:20 +0000791Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
792 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000793 ASSERT_MODULE_LOCK(this);
Alex Langford0e252e32019-07-30 22:12:34 +0000794 if (!die.IsValid())
795 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796
Alex Langford0e252e32019-07-30 22:12:34 +0000797 auto type_system_or_err =
798 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
799 if (auto err = type_system_or_err.takeError()) {
800 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
801 std::move(err), "Unable to parse function");
802 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 }
Alex Langford0e252e32019-07-30 22:12:34 +0000804 DWARFASTParser *dwarf_ast = type_system_or_err->GetDWARFParser();
805 if (!dwarf_ast)
806 return nullptr;
807
808 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809}
810
811bool SymbolFileDWARF::FixupAddress(Address &addr) {
812 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
813 if (debug_map_symfile) {
814 return debug_map_symfile->LinkOSOAddress(addr);
815 }
816 // This is a normal DWARF file, no address fixups need to happen
817 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000818}
Zachary Turner863f8c12019-01-11 18:03:20 +0000819lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000820 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000821 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 if (dwarf_cu)
823 return dwarf_cu->GetLanguageType();
824 else
825 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000826}
827
Zachary Turner863f8c12019-01-11 18:03:20 +0000828size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000829 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000830 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000831 if (!dwarf_cu)
832 return 0;
833
834 size_t functions_added = 0;
835 std::vector<DWARFDIE> function_dies;
836 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
837 for (const DWARFDIE &die : function_dies) {
838 if (comp_unit.FindFunctionByUID(die.GetID()))
839 continue;
840 if (ParseFunction(comp_unit, die))
841 ++functions_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 }
Zachary Turner0eaa6d52019-03-12 20:50:46 +0000843 // FixupTypes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845}
846
Zachary Turner863f8c12019-01-11 18:03:20 +0000847bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
848 FileSpecList &support_files) {
Jonas Devlieghere235339352019-08-13 19:51:51 +0000849 if (!comp_unit.GetLineTable())
850 ParseLineTable(comp_unit);
851 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852}
853
Pavel Labathca9c3de2019-06-12 11:29:50 +0000854FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) {
Pavel Labathad17e282019-06-13 11:22:47 +0000855 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) {
856 if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu))
857 return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx);
858 return FileSpec();
859 }
860
861 auto &tu = llvm::cast<DWARFTypeUnit>(unit);
862 return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx);
Pavel Labathca9c3de2019-06-12 11:29:50 +0000863}
864
865const FileSpecList &
866SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) {
867 static FileSpecList empty_list;
868
869 dw_offset_t offset = tu.GetLineTableOffset();
870 if (offset == DW_INVALID_OFFSET ||
871 offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() ||
872 offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey())
873 return empty_list;
874
875 // Many type units can share a line table, so parse the support file list
876 // once, and cache it based on the offset field.
877 auto iter_bool = m_type_unit_support_files.try_emplace(offset);
878 FileSpecList &list = iter_bool.first->second;
879 if (iter_bool.second) {
Jonas Devlieghere235339352019-08-13 19:51:51 +0000880 uint64_t line_table_offset = offset;
881 llvm::DWARFDataExtractor data = m_context.getOrLoadLineData().GetAsLLVM();
882 llvm::DWARFContext &ctx = m_context.GetAsLLVM();
883 llvm::DWARFDebugLine::Prologue prologue;
884 llvm::Error error = prologue.parse(data, &line_table_offset, ctx);
885 if (error) {
886 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
887 LLDB_LOG_ERROR(log, std::move(error),
888 "SymbolFileDWARF::GetTypeUnitSupportFiles failed to parse "
889 "the line table prologue");
890 } else {
Jonas Devliegherede0ce982019-08-15 23:53:15 +0000891 list = ParseSupportFilesFromPrologue(GetObjectFile()->GetModule(),
892 prologue, tu.GetPathStyle());
Jonas Devlieghere235339352019-08-13 19:51:51 +0000893 }
Pavel Labathca9c3de2019-06-12 11:29:50 +0000894 }
895 return list;
896}
897
Zachary Turner863f8c12019-01-11 18:03:20 +0000898bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000899 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000900 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 if (dwarf_cu)
902 return dwarf_cu->GetIsOptimized();
903 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000904}
905
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906bool SymbolFileDWARF::ParseImportedModules(
907 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000908 std::vector<SourceModule> &imported_modules) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000909 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000911 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000912 if (!dwarf_cu)
913 return false;
914 if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
915 sc.comp_unit->GetLanguage()))
916 return false;
917 UpdateExternalModuleListIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000919 const DWARFDIE die = dwarf_cu->DIE();
920 if (!die)
921 return false;
922
923 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
924 child_die = child_die.GetSibling()) {
925 if (child_die.Tag() != DW_TAG_imported_declaration)
926 continue;
927
928 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
929 if (module_die.Tag() != DW_TAG_module)
930 continue;
931
932 if (const char *name =
933 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
934 SourceModule module;
935 module.path.push_back(ConstString(name));
936
937 DWARFDIE parent_die = module_die;
938 while ((parent_die = parent_die.GetParent())) {
939 if (parent_die.Tag() != DW_TAG_module)
940 break;
941 if (const char *name =
942 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
943 module.path.push_back(ConstString(name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000945 std::reverse(module.path.begin(), module.path.end());
946 if (const char *include_path = module_die.GetAttributeValueAsString(
947 DW_AT_LLVM_include_path, nullptr))
948 module.search_path = ConstString(include_path);
949 if (const char *sysroot = module_die.GetAttributeValueAsString(
950 DW_AT_LLVM_isysroot, nullptr))
951 module.sysroot = ConstString(sysroot);
952 imported_modules.push_back(module);
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000953 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 }
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000955 return true;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000956}
957
Zachary Turner863f8c12019-01-11 18:03:20 +0000958bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Alex Langford07231732019-07-30 21:22:17 +0000959 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Konrad Kleine248a1302019-05-23 11:14:47 +0000960 if (comp_unit.GetLineTable() != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962
Zachary Turner863f8c12019-01-11 18:03:20 +0000963 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Jonas Devlieghere235339352019-08-13 19:51:51 +0000964 if (!dwarf_cu)
965 return false;
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000966
Jonas Devlieghere235339352019-08-13 19:51:51 +0000967 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
968 if (!dwarf_cu_die)
969 return false;
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000970
Jonas Devlieghere235339352019-08-13 19:51:51 +0000971 const dw_offset_t cu_line_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(
972 DW_AT_stmt_list, DW_INVALID_OFFSET);
973 if (cu_line_offset == DW_INVALID_OFFSET)
974 return false;
975
976 llvm::DWARFDebugLine line;
977 const llvm::DWARFDebugLine::LineTable *line_table = ParseLLVMLineTable(
978 m_context, line, cu_line_offset, dwarf_cu->GetOffset());
979
980 if (!line_table)
981 return false;
982
983 // FIXME: Rather than parsing the whole line table and then copying it over
984 // into LLDB, we should explore using a callback to populate the line table
985 // while we parse to reduce memory usage.
986 std::unique_ptr<LineTable> line_table_up =
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000987 std::make_unique<LineTable>(&comp_unit);
Jonas Devlieghere235339352019-08-13 19:51:51 +0000988 LineSequence *sequence = line_table_up->CreateLineSequenceContainer();
989 for (auto &row : line_table->Rows) {
990 line_table_up->AppendLineEntryToSequence(
991 sequence, row.Address.Address, row.Line, row.Column, row.File,
992 row.IsStmt, row.BasicBlock, row.PrologueEnd, row.EpilogueBegin,
993 row.EndSequence);
994 if (row.EndSequence) {
995 line_table_up->InsertSequence(sequence);
996 sequence = line_table_up->CreateLineSequenceContainer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 }
Jonas Devlieghere235339352019-08-13 19:51:51 +0000999
1000 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile()) {
1001 // We have an object file that has a line table with addresses that are not
1002 // linked. We need to link the line table and convert the addresses that
1003 // are relative to the .o file into addresses for the main executable.
1004 comp_unit.SetLineTable(
1005 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
1006 } else {
1007 comp_unit.SetLineTable(line_table_up.release());
1008 }
1009
1010 comp_unit.SetSupportFiles(ParseSupportFilesFromPrologue(
Jonas Devliegherede0ce982019-08-15 23:53:15 +00001011 comp_unit.GetModule(), line_table->Prologue, dwarf_cu->GetPathStyle(),
Jonas Devlieghere235339352019-08-13 19:51:51 +00001012 dwarf_cu->GetCompilationDirectory().GetCString(), FileSpec(comp_unit)));
1013
1014 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015}
1016
Siva Chandrad8335e92015-12-16 00:22:08 +00001017lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1019 auto iter = m_debug_macros_map.find(*offset);
1020 if (iter != m_debug_macros_map.end())
1021 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001022
Pavel Labath0261b942019-05-20 08:38:47 +00001023 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001024 if (debug_macro_data.GetByteSize() == 0)
1025 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001026
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1028 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001029
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 const DWARFDebugMacroHeader &header =
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00001031 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
Pavel Labath0261b942019-05-20 08:38:47 +00001032 DWARFDebugMacroEntry::ReadMacroEntries(
1033 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
1034 offset, this, debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001037}
1038
Zachary Turner863f8c12019-01-11 18:03:20 +00001039bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001040 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Siva Chandrad8335e92015-12-16 00:22:08 +00001041
Zachary Turner863f8c12019-01-11 18:03:20 +00001042 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001044 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045
Jan Kratochvil93afb052018-05-24 20:51:13 +00001046 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 if (!dwarf_cu_die)
1048 return false;
1049
1050 lldb::offset_t sect_offset =
1051 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1052 if (sect_offset == DW_INVALID_OFFSET)
1053 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1054 DW_INVALID_OFFSET);
1055 if (sect_offset == DW_INVALID_OFFSET)
1056 return false;
1057
Zachary Turner863f8c12019-01-11 18:03:20 +00001058 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059
1060 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001061}
1062
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001063size_t SymbolFileDWARF::ParseBlocksRecursive(
1064 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1065 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 size_t blocks_added = 0;
1067 DWARFDIE die = orig_die;
1068 while (die) {
1069 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 switch (tag) {
1072 case DW_TAG_inlined_subroutine:
1073 case DW_TAG_subprogram:
1074 case DW_TAG_lexical_block: {
Konrad Kleine248a1302019-05-23 11:14:47 +00001075 Block *block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001077 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1078 // inlined functions. These will be parsed on their own as separate
1079 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080
1081 if (depth > 0)
1082 break;
1083
1084 block = parent_block;
1085 } else {
1086 BlockSP block_sp(new Block(die.GetID()));
1087 parent_block->AddChild(block_sp);
1088 block = block_sp.get();
1089 }
1090 DWARFRangeList ranges;
Konrad Kleine248a1302019-05-23 11:14:47 +00001091 const char *name = nullptr;
1092 const char *mangled_name = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093
1094 int decl_file = 0;
1095 int decl_line = 0;
1096 int decl_column = 0;
1097 int call_file = 0;
1098 int call_line = 0;
1099 int call_column = 0;
1100 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1101 decl_line, decl_column, call_file, call_line,
1102 call_column, nullptr)) {
1103 if (tag == DW_TAG_subprogram) {
1104 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1105 subprogram_low_pc = ranges.GetMinRangeBase(0);
1106 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001107 // We get called here for inlined subroutines in two ways. The first
1108 // time is when we are making the Function object for this inlined
1109 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001111 // need to adjust the containing address. The second time is when we
1112 // are parsing the blocks inside the function that contains the
1113 // inlined concrete instance. Since these will be blocks inside the
1114 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1116 subprogram_low_pc = ranges.GetMinRangeBase(0);
1117 }
1118 }
1119
1120 const size_t num_ranges = ranges.GetSize();
1121 for (size_t i = 0; i < num_ranges; ++i) {
1122 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1123 const addr_t range_base = range.GetRangeBase();
1124 if (range_base >= subprogram_low_pc)
1125 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1126 range.GetByteSize()));
1127 else {
1128 GetObjectFile()->GetModule()->ReportError(
1129 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1130 ") which has a base that is less than the function's low PC "
1131 "0x%" PRIx64 ". Please file a bug and attach the file at the "
Jonas Devlieghere235339352019-08-13 19:51:51 +00001132 "start of this error message",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 block->GetID(), range_base, range.GetRangeEnd(),
1134 subprogram_low_pc);
1135 }
1136 }
1137 block->FinalizeRanges();
1138
1139 if (tag != DW_TAG_subprogram &&
Konrad Kleine248a1302019-05-23 11:14:47 +00001140 (name != nullptr || mangled_name != nullptr)) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001141 std::unique_ptr<Declaration> decl_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001143 decl_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001144 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 decl_line, decl_column));
1146
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001147 std::unique_ptr<Declaration> call_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 if (call_file != 0 || call_line != 0 || call_column != 0)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001149 call_up.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001150 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 call_line, call_column));
1152
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001153 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1154 call_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155 }
1156
1157 ++blocks_added;
1158
1159 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001160 blocks_added +=
1161 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1162 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 }
1164 }
1165 } break;
1166 default:
1167 break;
1168 }
1169
Adrian Prantl05097242018-04-30 16:49:04 +00001170 // Only parse siblings of the block if we are not at depth zero. A depth of
1171 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1172 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173
1174 if (depth == 0)
1175 die.Clear();
1176 else
1177 die = die.GetSibling();
1178 }
1179 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001180}
1181
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1183 if (parent_die) {
1184 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1185 die = die.GetSibling()) {
1186 dw_tag_t tag = die.Tag();
1187 bool check_virtuality = false;
1188 switch (tag) {
1189 case DW_TAG_inheritance:
1190 case DW_TAG_subprogram:
1191 check_virtuality = true;
1192 break;
1193 default:
1194 break;
1195 }
1196 if (check_virtuality) {
1197 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1198 return true;
1199 }
1200 }
1201 }
1202 return false;
1203}
1204
1205void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
Alex Langford0e252e32019-07-30 22:12:34 +00001206 auto *type_system = decl_ctx.GetTypeSystem();
1207 if (!type_system)
1208 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1210 std::vector<DWARFDIE> decl_ctx_die_list =
1211 ast_parser->GetDIEForDeclContext(decl_ctx);
1212
1213 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1214 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1215 decl = decl.GetSibling())
1216 ast_parser->GetDeclForUIDFromDWARF(decl);
1217}
1218
Pavel Labath3b926982019-06-21 07:56:50 +00001219user_id_t SymbolFileDWARF::GetUID(DIERef ref) {
1220 if (GetDebugMapSymfile())
1221 return GetID() | ref.die_offset();
1222
1223 return user_id_t(GetDwoNum().getValueOr(0x7fffffff)) << 32 |
1224 ref.die_offset() |
1225 (lldb::user_id_t(ref.section() == DIERef::Section::DebugTypes) << 63);
1226}
1227
Pavel Labath67b45ac2019-06-19 07:32:39 +00001228llvm::Optional<SymbolFileDWARF::DecodedUID>
1229SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001230 // This method can be called without going through the symbol vendor so we
1231 // need to lock the module.
1232 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001233 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1234 // must make sure we use the correct DWARF file when resolving things. On
1235 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1236 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1237 // references to other DWARF objects and we must be ready to receive a
1238 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 // instance.
Pavel Labath2841e6e2019-05-08 11:43:05 +00001240 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1241 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 debug_map->GetOSOIndexFromUserID(uid));
Pavel Labath67b45ac2019-06-19 07:32:39 +00001243 return DecodedUID{
Pavel Labath3b926982019-06-21 07:56:50 +00001244 *dwarf, {llvm::None, DIERef::Section::DebugInfo, dw_offset_t(uid)}};
Pavel Labath2841e6e2019-05-08 11:43:05 +00001245 }
Pavel Labath381ba9a2019-05-13 08:58:34 +00001246 dw_offset_t die_offset = uid;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001247 if (die_offset == DW_INVALID_OFFSET)
Pavel Labath67b45ac2019-06-19 07:32:39 +00001248 return llvm::None;
Pavel Labath381ba9a2019-05-13 08:58:34 +00001249
Pavel Labath3b926982019-06-21 07:56:50 +00001250 DIERef::Section section =
1251 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1252
1253 llvm::Optional<uint32_t> dwo_num = uid >> 32 & 0x7fffffff;
1254 if (*dwo_num == 0x7fffffff)
1255 dwo_num = llvm::None;
1256
1257 return DecodedUID{*this, {dwo_num, section, die_offset}};
Greg Clayton07c8c442016-04-25 23:39:19 +00001258}
1259
1260DWARFDIE
Pavel Labath2841e6e2019-05-08 11:43:05 +00001261SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001262 // This method can be called without going through the symbol vendor so we
1263 // need to lock the module.
1264 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001265
Pavel Labath67b45ac2019-06-19 07:32:39 +00001266 llvm::Optional<DecodedUID> decoded = DecodeUID(uid);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001267
Pavel Labath67b45ac2019-06-19 07:32:39 +00001268 if (decoded)
1269 return decoded->dwarf.GetDIE(decoded->ref);
Pavel Labath2841e6e2019-05-08 11:43:05 +00001270
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001272}
1273
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001275 // This method can be called without going through the symbol vendor so we
1276 // need to lock the module.
1277 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001278 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001279 // SymbolFileDWARF::GetDIE(). See comments inside the
1280 // SymbolFileDWARF::GetDIE() for details.
1281 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 return die.GetDecl();
1283 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001284}
1285
Greg Clayton99558cc42015-08-24 23:46:31 +00001286CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001288 // This method can be called without going through the symbol vendor so we
1289 // need to lock the module.
1290 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001291 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001292 // SymbolFileDWARF::GetDIE(). See comments inside the
1293 // SymbolFileDWARF::GetDIE() for details.
1294 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 return die.GetDeclContext();
1296 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001297}
1298
Greg Clayton99558cc42015-08-24 23:46:31 +00001299CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001301 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001302 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001303 // SymbolFileDWARF::GetDIE(). See comments inside the
1304 // SymbolFileDWARF::GetDIE() for details.
1305 if (DWARFDIE die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 return die.GetContainingDeclContext();
1307 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308}
1309
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001311 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001312 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
Pavel Labath2841e6e2019-05-08 11:43:05 +00001313 // SymbolFileDWARF::GetDIE(). See comments inside the
1314 // SymbolFileDWARF::GetDIE() for details.
1315 if (DWARFDIE type_die = GetDIE(type_uid))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316 return type_die.ResolveType();
1317 else
1318 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319}
1320
Adrian Prantleca07c52018-11-05 20:49:07 +00001321llvm::Optional<SymbolFile::ArrayInfo>
1322SymbolFileDWARF::GetDynamicArrayInfoForUID(
1323 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1324 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labath2841e6e2019-05-08 11:43:05 +00001325 if (DWARFDIE type_die = GetDIE(type_uid))
Adrian Prantleca07c52018-11-05 20:49:07 +00001326 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1327 else
1328 return llvm::None;
1329}
1330
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1332 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001333}
1334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1336 bool assert_not_being_parsed) {
1337 if (die) {
1338 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1339 if (log)
1340 GetObjectFile()->GetModule()->LogMessage(
1341 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1342 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001343
Adrian Prantl05097242018-04-30 16:49:04 +00001344 // We might be coming in in the middle of a type tree (a class within a
1345 // class, an enum within a class), so parse any needed parent DIEs before
1346 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1348 if (decl_ctx_die) {
1349 if (log) {
1350 switch (decl_ctx_die.Tag()) {
1351 case DW_TAG_structure_type:
1352 case DW_TAG_union_type:
1353 case DW_TAG_class_type: {
1354 // Get the type, which could be a forward declaration
1355 if (log)
1356 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00001357 log,
1358 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1359 "resolve parent forward type for 0x%8.8x",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1361 decl_ctx_die.GetOffset());
1362 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001363
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364 default:
1365 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001366 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001368 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 return ResolveType(die);
1370 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001371 return nullptr;
Greg Claytoncab36a32011-12-08 05:16:30 +00001372}
1373
Greg Clayton6beaaa62011-01-17 03:46:26 +00001374// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001375// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1376// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377bool SymbolFileDWARF::HasForwardDeclForClangType(
1378 const CompilerType &compiler_type) {
1379 CompilerType compiler_type_no_qualifiers =
1380 ClangUtil::RemoveFastQualifiers(compiler_type);
1381 if (GetForwardDeclClangTypeToDie().count(
1382 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1383 return true;
1384 }
1385 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001386
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387 ClangASTContext *clang_type_system =
1388 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1389 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001390 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 DWARFASTParserClang *ast_parser =
1392 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1393 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001394}
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001397 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001398
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 ClangASTContext *clang_type_system =
1400 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1401 if (clang_type_system) {
1402 DWARFASTParserClang *ast_parser =
1403 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1404 if (ast_parser &&
1405 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1406 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1407 }
Jim Inghamc3549282012-01-11 02:21:12 +00001408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 // We have a struct/union/class/enum that needs to be fully resolved.
1410 CompilerType compiler_type_no_qualifiers =
1411 ClangUtil::RemoveFastQualifiers(compiler_type);
1412 auto die_it = GetForwardDeclClangTypeToDie().find(
1413 compiler_type_no_qualifiers.GetOpaqueQualType());
1414 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1415 // We have already resolved this type...
1416 return true;
1417 }
1418
1419 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1420 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001421 // Once we start resolving this type, remove it from the forward
1422 // declaration map in case anyone child members or other types require this
1423 // type to get resolved. The type will get resolved when all of the calls
1424 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 GetForwardDeclClangTypeToDie().erase(die_it);
1426
1427 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1428
1429 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1430 DWARF_LOG_TYPE_COMPLETION));
1431 if (log)
1432 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1433 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1434 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1435 type->GetName().AsCString());
1436 assert(compiler_type);
1437 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1438 if (dwarf_ast)
1439 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1440 }
1441 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001442}
1443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1445 bool assert_not_being_parsed,
1446 bool resolve_function_context) {
1447 if (die) {
1448 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1449
1450 if (assert_not_being_parsed) {
1451 if (type != DIE_IS_BEING_PARSED)
1452 return type;
1453
1454 GetObjectFile()->GetModule()->ReportError(
1455 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1456 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1457
1458 } else
1459 return type;
1460 }
1461 return nullptr;
1462}
1463
Pavel Labathad17e282019-06-13 11:22:47 +00001464CompileUnit *
1465SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001467 if (dwarf_cu.GetUserData() == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00001468 // The symbol vendor doesn't know about this compile unit, we need to parse
1469 // and add it to the symbol vendor object.
Pavel Labath519ef6a2019-05-27 10:10:59 +00001470 return ParseCompileUnit(dwarf_cu).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 }
Pavel Labathad17e282019-06-13 11:22:47 +00001472 return (CompileUnit *)dwarf_cu.GetUserData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473}
1474
1475size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1476 DIEArray &method_die_offsets) {
1477 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001478 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 return method_die_offsets.size();
1480}
1481
1482bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1483 sc.Clear(false);
1484
Pavel Labathad17e282019-06-13 11:22:47 +00001485 if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486 // Check if the symbol vendor already knows about this compile unit?
Pavel Labathad17e282019-06-13 11:22:47 +00001487 sc.comp_unit =
1488 GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489
1490 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001491 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001492 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493
1494 if (sc.function) {
1495 sc.module_sp = sc.function->CalculateSymbolContextModule();
1496 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 }
1499
1500 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501}
1502
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1504 UpdateExternalModuleListIfNeeded();
1505 const auto &pos = m_external_type_modules.find(name);
1506 if (pos != m_external_type_modules.end())
1507 return pos->second;
1508 else
1509 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001510}
1511
Greg Clayton2f869fe2016-03-30 20:14:35 +00001512DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
Pavel Labath3b926982019-06-21 07:56:50 +00001514 if (die_ref.dwo_num()) {
1515 return DebugInfo()
1516 ->GetUnitAtIndex(*die_ref.dwo_num())
1517 ->GetDwoSymbolFile()
1518 ->GetDIE(die_ref);
1519 }
1520
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521 DWARFDebugInfo *debug_info = DebugInfo();
1522 if (debug_info)
1523 return debug_info->GetDIE(die_ref);
1524 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001525 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001526}
1527
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528std::unique_ptr<SymbolFileDWARFDwo>
1529SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Pavel Labathad17e282019-06-13 11:22:47 +00001530 DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001532 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 // info files for modules.
1534 if (GetDebugMapSymfile())
1535 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001536
Pavel Labathad17e282019-06-13 11:22:47 +00001537 DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit);
1538 // Only compile units can be split into two parts.
1539 if (!dwarf_cu)
1540 return nullptr;
1541
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001542 const char *dwo_name =
Pavel Labathad17e282019-06-13 11:22:47 +00001543 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 if (!dwo_name)
1545 return nullptr;
1546
Tamas Berghammer963ce482017-08-25 13:56:14 +00001547 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1548 if (dwp_symfile) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001549 uint64_t dwo_id =
Pavel Labathad17e282019-06-13 11:22:47 +00001550 cu_die.GetAttributeValueAsUnsigned(dwarf_cu, DW_AT_GNU_dwo_id, 0);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001551 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
Pavel Labathad17e282019-06-13 11:22:47 +00001552 dwp_symfile->GetSymbolFileForDwoId(*dwarf_cu, dwo_id);
Tamas Berghammer963ce482017-08-25 13:56:14 +00001553 if (dwo_symfile)
1554 return dwo_symfile;
1555 }
1556
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001557 FileSpec dwo_file(dwo_name);
1558 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 if (dwo_file.IsRelative()) {
Jan Kratochvilb7a19322019-05-21 17:38:56 +00001560 const char *comp_dir =
Pavel Labathad17e282019-06-13 11:22:47 +00001561 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001562 if (!comp_dir)
1563 return nullptr;
1564
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001565 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1566 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 dwo_file.AppendPathComponent(dwo_name);
1568 }
1569
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001570 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 return nullptr;
1572
1573 const lldb::offset_t file_offset = 0;
1574 DataBufferSP dwo_file_data_sp;
1575 lldb::offset_t dwo_file_data_offset = 0;
1576 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1577 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001578 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1579 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 if (dwo_obj_file == nullptr)
1581 return nullptr;
1582
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +00001583 return std::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001584}
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1587 if (m_fetched_external_modules)
1588 return;
1589 m_fetched_external_modules = true;
1590
1591 DWARFDebugInfo *debug_info = DebugInfo();
1592
1593 const uint32_t num_compile_units = GetNumCompileUnits();
1594 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00001595 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596
Jan Kratochvil93afb052018-05-24 20:51:13 +00001597 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001598 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1600
1601 if (name) {
1602 ConstString const_name(name);
1603 if (m_external_type_modules.find(const_name) ==
1604 m_external_type_modules.end()) {
1605 ModuleSP module_sp;
1606 const char *dwo_path =
1607 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1608 if (dwo_path) {
1609 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001610 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001611 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1613 const char *comp_dir =
1614 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1615 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001616 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001617 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001618 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1620 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001621 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 dwo_module_spec.GetArchitecture() =
Pavel Labathd2deeb42019-07-31 08:25:25 +00001623 m_objfile_sp->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001624
Adrian Prantl05097242018-04-30 16:49:04 +00001625 // When LLDB loads "external" modules it looks at the presence of
1626 // DW_AT_GNU_dwo_name. However, when the already created module
1627 // (corresponding to .dwo itself) is being processed, it will see
1628 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1629 // dwo file) and will try to call ModuleList::GetSharedModule
1630 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1631 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1632 // DW_AT_comp_dir. In this case the method
1633 // ModuleList::GetSharedModule will fail and the warning will be
1634 // printed. However, as one can notice in this case we don't
1635 // actually need to try to load the already loaded module
1636 // (corresponding to .dwo) so we simply skip it.
Pavel Labathd2deeb42019-07-31 08:25:25 +00001637 if (m_objfile_sp->GetFileSpec().GetFileNameExtension() == ".dwo" &&
1638 llvm::StringRef(m_objfile_sp->GetFileSpec().GetPath())
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001639 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1640 continue;
1641 }
1642
Zachary Turner97206d52017-05-12 04:51:55 +00001643 Status error = ModuleList::GetSharedModule(
Konrad Kleine248a1302019-05-23 11:14:47 +00001644 dwo_module_spec, module_sp, nullptr, nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 if (!module_sp) {
1646 GetObjectFile()->GetModule()->ReportWarning(
1647 "0x%8.8x: unable to locate module needed for external types: "
1648 "%s\nerror: %s\nDebugging will be degraded due to missing "
1649 "types. Rebuilding your project will regenerate the needed "
1650 "module files.",
1651 die.GetOffset(),
1652 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1653 error.AsCString("unknown error"));
1654 }
1655 }
1656 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001659 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 }
1661}
1662
1663SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001664 if (!m_global_aranges_up) {
1665 m_global_aranges_up.reset(new GlobalVariableMap());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666
1667 ModuleSP module_sp = GetObjectFile()->GetModule();
1668 if (module_sp) {
1669 const size_t num_cus = module_sp->GetNumCompileUnits();
1670 for (size_t i = 0; i < num_cus; ++i) {
1671 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1672 if (cu_sp) {
1673 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1674 if (globals_sp) {
1675 const size_t num_globals = globals_sp->GetSize();
1676 for (size_t g = 0; g < num_globals; ++g) {
1677 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1678 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1679 const DWARFExpression &location = var_sp->LocationExpression();
1680 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001681 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001682 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1683 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 if (location_result.GetValueType() ==
1685 Value::eValueTypeFileAddress) {
1686 lldb::addr_t file_addr =
1687 location_result.GetScalar().ULongLong();
1688 lldb::addr_t byte_size = 1;
1689 if (var_sp->GetType())
Adrian Prantld13777a2019-01-29 17:52:34 +00001690 byte_size =
1691 var_sp->GetType()->GetByteSize().getValueOr(0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001692 m_global_aranges_up->Append(GlobalVariableMap::Entry(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 file_addr, byte_size, var_sp.get()));
1694 }
1695 }
1696 }
1697 }
1698 }
1699 }
1700 }
1701 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001702 m_global_aranges_up->Sort();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001704 return *m_global_aranges_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705}
1706
1707uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001708 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001710 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00001711 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1712 Timer scoped_timer(func_cat,
1713 "SymbolFileDWARF::"
1714 "ResolveSymbolContext (so_addr = { "
1715 "section = %p, offset = 0x%" PRIx64
1716 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001717 static_cast<void *>(so_addr.GetSection().get()),
1718 so_addr.GetOffset(), resolve_scope);
1719 uint32_t resolved = 0;
1720 if (resolve_scope &
1721 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1722 eSymbolContextLineEntry | eSymbolContextVariable)) {
1723 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1724
1725 DWARFDebugInfo *debug_info = DebugInfo();
1726 if (debug_info) {
Zachary Turner1cbbab92019-03-15 17:32:05 +00001727 llvm::Expected<DWARFDebugAranges &> aranges =
1728 debug_info->GetCompileUnitAranges();
1729 if (!aranges) {
Zachary Turner611d1f92019-03-19 20:08:56 +00001730 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Zachary Turner1cbbab92019-03-15 17:32:05 +00001731 LLDB_LOG_ERROR(log, aranges.takeError(),
1732 "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1733 "aranges. {0}");
1734 return 0;
1735 }
1736
1737 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001739 // Global variables are not in the compile unit address ranges. The
1740 // only way to currently find global variables is to iterate over the
1741 // .debug_pubnames or the __apple_names table and find all items in
1742 // there that point to DW_TAG_variable DIEs and then find the address
1743 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 if (resolve_scope & eSymbolContextVariable) {
1745 GlobalVariableMap &map = GetGlobalAranges();
1746 const GlobalVariableMap::Entry *entry =
1747 map.FindEntryThatContains(file_vm_addr);
1748 if (entry && entry->data) {
1749 Variable *variable = entry->data;
1750 SymbolContextScope *scc = variable->GetSymbolContextScope();
1751 if (scc) {
1752 scc->CalculateSymbolContext(&sc);
1753 sc.variable = variable;
1754 }
1755 return sc.GetResolvedMask();
1756 }
1757 }
1758 } else {
1759 uint32_t cu_idx = DW_INVALID_INDEX;
Pavel Labathad17e282019-06-13 11:22:47 +00001760 if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>(
1761 debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
1762 cu_offset, &cu_idx))) {
1763 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 if (sc.comp_unit) {
1765 resolved |= eSymbolContextCompUnit;
1766
1767 bool force_check_line_table = false;
1768 if (resolve_scope &
1769 (eSymbolContextFunction | eSymbolContextBlock)) {
1770 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1771 DWARFDIE block_die;
1772 if (function_die) {
1773 sc.function =
1774 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
Konrad Kleine248a1302019-05-23 11:14:47 +00001775 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +00001776 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777
1778 if (sc.function && (resolve_scope & eSymbolContextBlock))
1779 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1780 } else {
1781 // We might have had a compile unit that had discontiguous
1782 // address ranges where the gaps are symbols that don't have
1783 // any debug info. Discontiguous compile unit address ranges
1784 // should only happen when there aren't other functions from
1785 // other compile units in these gaps. This helps keep the size
1786 // of the aranges down.
1787 force_check_line_table = true;
1788 }
1789
Konrad Kleine248a1302019-05-23 11:14:47 +00001790 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 resolved |= eSymbolContextFunction;
1792
1793 if (resolve_scope & eSymbolContextBlock) {
1794 Block &block = sc.function->GetBlock(true);
1795
1796 if (block_die)
1797 sc.block = block.FindBlockByID(block_die.GetID());
1798 else
1799 sc.block = block.FindBlockByID(function_die.GetID());
1800 if (sc.block)
1801 resolved |= eSymbolContextBlock;
1802 }
1803 }
1804 }
1805
1806 if ((resolve_scope & eSymbolContextLineEntry) ||
1807 force_check_line_table) {
1808 LineTable *line_table = sc.comp_unit->GetLineTable();
Konrad Kleine248a1302019-05-23 11:14:47 +00001809 if (line_table != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001811 // terms of this debug file if there is no debug map, or it
1812 // will be an address in the .o file which needs to be fixed up
1813 // to be in terms of the debug map executable. Either way,
1814 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001815 Address exe_so_addr(so_addr);
1816 if (FixupAddress(exe_so_addr)) {
1817 if (line_table->FindLineEntryByAddress(exe_so_addr,
1818 sc.line_entry)) {
1819 resolved |= eSymbolContextLineEntry;
1820 }
1821 }
1822 }
1823 }
1824
1825 if (force_check_line_table &&
1826 !(resolved & eSymbolContextLineEntry)) {
1827 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001828 // address ranges where the gaps are symbols that don't have any
1829 // debug info. Discontiguous compile unit address ranges should
1830 // only happen when there aren't other functions from other
1831 // compile units in these gaps. This helps keep the size of the
1832 // aranges down.
Konrad Kleine248a1302019-05-23 11:14:47 +00001833 sc.comp_unit = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834 resolved &= ~eSymbolContextCompUnit;
1835 }
1836 } else {
1837 GetObjectFile()->GetModule()->ReportWarning(
1838 "0x%8.8x: compile unit %u failed to create a valid "
1839 "lldb_private::CompileUnit class.",
1840 cu_offset, cu_idx);
1841 }
1842 }
1843 }
1844 }
1845 }
1846 return resolved;
1847}
1848
1849uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1850 uint32_t line,
1851 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001852 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001854 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855 const uint32_t prev_size = sc_list.GetSize();
1856 if (resolve_scope & eSymbolContextCompUnit) {
Pavel Labathad17e282019-06-13 11:22:47 +00001857 for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus;
1858 ++cu_idx) {
1859 CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get();
1860 if (!dc_cu)
1861 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001862
Pavel Labathad17e282019-06-13 11:22:47 +00001863 const bool full_match = (bool)file_spec.GetDirectory();
1864 bool file_spec_matches_cu_file_spec =
1865 FileSpec::Equal(file_spec, *dc_cu, full_match);
1866 if (check_inlines || file_spec_matches_cu_file_spec) {
Pavel Labathd2deeb42019-07-31 08:25:25 +00001867 SymbolContext sc(m_objfile_sp->GetModule());
Pavel Labathad17e282019-06-13 11:22:47 +00001868 sc.comp_unit = dc_cu;
1869 uint32_t file_idx = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001870
Pavel Labathad17e282019-06-13 11:22:47 +00001871 // If we are looking for inline functions only and we don't find it
1872 // in the support files, we are done.
1873 if (check_inlines) {
1874 file_idx =
1875 sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true);
1876 if (file_idx == UINT32_MAX)
1877 continue;
1878 }
1879
1880 if (line != 0) {
1881 LineTable *line_table = sc.comp_unit->GetLineTable();
1882
1883 if (line_table != nullptr && line != 0) {
1884 // We will have already looked up the file index if we are
1885 // searching for inline entries.
1886 if (!check_inlines)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1888 1, file_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001889
Pavel Labathad17e282019-06-13 11:22:47 +00001890 if (file_idx != UINT32_MAX) {
1891 uint32_t found_line;
1892 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1893 0, file_idx, line, false, &sc.line_entry);
1894 found_line = sc.line_entry.line;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895
Pavel Labathad17e282019-06-13 11:22:47 +00001896 while (line_idx != UINT32_MAX) {
1897 sc.function = nullptr;
1898 sc.block = nullptr;
1899 if (resolve_scope &
1900 (eSymbolContextFunction | eSymbolContextBlock)) {
1901 const lldb::addr_t file_vm_addr =
1902 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1903 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1904 DWARFDIE function_die =
1905 GetDWARFCompileUnit(dc_cu)->LookupAddress(file_vm_addr);
1906 DWARFDIE block_die;
1907 if (function_die) {
1908 sc.function =
1909 sc.comp_unit->FindFunctionByUID(function_die.GetID())
1910 .get();
1911 if (sc.function == nullptr)
1912 sc.function =
1913 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914
Pavel Labathad17e282019-06-13 11:22:47 +00001915 if (sc.function && (resolve_scope & eSymbolContextBlock))
1916 block_die =
1917 function_die.LookupDeepestBlock(file_vm_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918 }
1919
Pavel Labathad17e282019-06-13 11:22:47 +00001920 if (sc.function != nullptr) {
1921 Block &block = sc.function->GetBlock(true);
1922
1923 if (block_die)
1924 sc.block = block.FindBlockByID(block_die.GetID());
1925 else if (function_die)
1926 sc.block = block.FindBlockByID(function_die.GetID());
1927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928 }
1929 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001930
Pavel Labathad17e282019-06-13 11:22:47 +00001931 sc_list.Append(sc);
1932 line_idx = line_table->FindLineEntryIndexByFileIndex(
1933 line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1934 }
1935 }
1936 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1937 // only append the context if we aren't looking for inline call
1938 // sites by file and line and if the file spec matches that of
1939 // the compile unit
1940 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941 }
Pavel Labathad17e282019-06-13 11:22:47 +00001942 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1943 // only append the context if we aren't looking for inline call
1944 // sites by file and line and if the file spec matches that of
1945 // the compile unit
1946 sc_list.Append(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001947 }
Pavel Labathad17e282019-06-13 11:22:47 +00001948
1949 if (!check_inlines)
1950 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001951 }
1952 }
1953 }
1954 return sc_list.GetSize() - prev_size;
1955}
1956
Jim Ingham7fca8c02017-04-28 00:51:06 +00001957void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001958 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001959 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960}
1961
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001962std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
1963 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
1964 if (module_sp)
1965 return module_sp->GetMutex();
1966 return GetObjectFile()->GetModule()->GetMutex();
1967}
1968
Kate Stoneb9c1b512016-09-06 20:57:50 +00001969bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1970 const lldb_private::CompilerDeclContext *decl_ctx) {
1971 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001972 // Invalid namespace decl which means we aren't matching only things in
1973 // this symbol file, so return true to indicate it matches this symbol
1974 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975 return true;
1976 }
1977
1978 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
Alex Langford0e252e32019-07-30 22:12:34 +00001979 auto type_system_or_err = GetTypeSystemForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 decl_ctx_type_system->GetMinimumLanguage(nullptr));
Alex Langford0e252e32019-07-30 22:12:34 +00001981 if (auto err = type_system_or_err.takeError()) {
1982 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
1983 std::move(err),
1984 "Unable to match namespace decl using TypeSystem");
1985 return false;
1986 }
1987
1988 if (decl_ctx_type_system == &type_system_or_err.get())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001989 return true; // The type systems match, return true
1990
1991 // The namespace AST was valid, and it does not match...
1992 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1993
1994 if (log)
1995 GetObjectFile()->GetModule()->LogMessage(
1996 log, "Valid namespace does not match symbol file");
1997
1998 return false;
1999}
2000
2001uint32_t SymbolFileDWARF::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002002 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002003 uint32_t max_matches, VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002004 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002005 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2006
2007 if (log)
2008 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002009 log,
2010 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2011 "parent_decl_ctx=%p, max_matches=%u, variables)",
2012 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 max_matches);
2014
2015 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2016 return 0;
2017
2018 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002019 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 return 0;
2021
Pavel Labath34cda142018-05-31 09:46:26 +00002022 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 const uint32_t original_size = variables.GetSize();
2024
Pavel Labathe1d18752018-06-07 10:04:44 +00002025 llvm::StringRef basename;
2026 llvm::StringRef context;
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002027 bool name_is_mangled = (bool)Mangled(name);
Pavel Labathe1d18752018-06-07 10:04:44 +00002028
2029 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2030 context, basename))
2031 basename = name.GetStringRef();
2032
Kate Stoneb9c1b512016-09-06 20:57:50 +00002033 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002034 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002035 const size_t num_die_matches = die_offsets.size();
2036 if (num_die_matches) {
2037 SymbolContext sc;
Pavel Labathd2deeb42019-07-31 08:25:25 +00002038 sc.module_sp = m_objfile_sp->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 assert(sc.module_sp);
2040
Pavel Labathe1d18752018-06-07 10:04:44 +00002041 // Loop invariant: Variables up to this index have been checked for context
2042 // matches.
2043 uint32_t pruned_idx = original_size;
2044
Kate Stoneb9c1b512016-09-06 20:57:50 +00002045 bool done = false;
2046 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2047 const DIERef &die_ref = die_offsets[i];
2048 DWARFDIE die = GetDIE(die_ref);
2049
2050 if (die) {
2051 switch (die.Tag()) {
2052 default:
2053 case DW_TAG_subprogram:
2054 case DW_TAG_inlined_subroutine:
2055 case DW_TAG_try_block:
2056 case DW_TAG_catch_block:
2057 break;
2058
2059 case DW_TAG_variable: {
Pavel Labathad17e282019-06-13 11:22:47 +00002060 auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2061 if (!dwarf_cu)
2062 continue;
2063 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002064
2065 if (parent_decl_ctx) {
2066 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2067 if (dwarf_ast) {
2068 CompilerDeclContext actual_parent_decl_ctx =
2069 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2070 if (!actual_parent_decl_ctx ||
2071 actual_parent_decl_ctx != *parent_decl_ctx)
2072 continue;
2073 }
2074 }
2075
2076 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2077 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002078 while (pruned_idx < variables.GetSize()) {
2079 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
Kuba Mraceke5e9a6b2019-04-18 00:15:44 +00002080 if (name_is_mangled ||
2081 var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
Pavel Labathe1d18752018-06-07 10:04:44 +00002082 ++pruned_idx;
2083 else
2084 variables.RemoveVariableAtIndex(pruned_idx);
2085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002086
2087 if (variables.GetSize() - original_size >= max_matches)
2088 done = true;
2089 } break;
2090 }
2091 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002092 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093 }
2094 }
2095 }
2096
2097 // Return the number of variable that were appended to the list
2098 const uint32_t num_matches = variables.GetSize() - original_size;
2099 if (log && num_matches > 0) {
2100 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002101 log,
2102 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2103 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2104 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105 max_matches, num_matches);
2106 }
2107 return num_matches;
2108}
2109
2110uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002111 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002112 VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002113 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2115
2116 if (log) {
2117 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002118 log,
2119 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2120 "max_matches=%u, variables)",
2121 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 }
2123
2124 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002125 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 return 0;
2127
Pavel Labath34cda142018-05-31 09:46:26 +00002128 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129 const uint32_t original_size = variables.GetSize();
2130
2131 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002132 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133
2134 SymbolContext sc;
Pavel Labathd2deeb42019-07-31 08:25:25 +00002135 sc.module_sp = m_objfile_sp->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002136 assert(sc.module_sp);
2137
2138 const size_t num_matches = die_offsets.size();
2139 if (num_matches) {
2140 for (size_t i = 0; i < num_matches; ++i) {
2141 const DIERef &die_ref = die_offsets[i];
2142 DWARFDIE die = GetDIE(die_ref);
2143
2144 if (die) {
Pavel Labathad17e282019-06-13 11:22:47 +00002145 DWARFCompileUnit *dwarf_cu =
2146 llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2147 if (!dwarf_cu)
2148 continue;
2149 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150
2151 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2152
2153 if (variables.GetSize() - original_size >= max_matches)
2154 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002155 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00002156 m_index->ReportInvalidDIERef(die_ref, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 }
2158 }
2159
2160 // Return the number of variable that were appended to the list
2161 return variables.GetSize() - original_size;
2162}
2163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2165 bool include_inlines,
2166 SymbolContextList &sc_list) {
2167 SymbolContext sc;
2168
2169 if (!orig_die)
2170 return false;
2171
2172 // If we were passed a die that is not a function, just return false...
2173 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2174 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2175 return false;
2176
2177 DWARFDIE die = orig_die;
2178 DWARFDIE inlined_die;
2179 if (die.Tag() == DW_TAG_inlined_subroutine) {
2180 inlined_die = die;
2181
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002182 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 die = die.GetParent();
2184
2185 if (die) {
2186 if (die.Tag() == DW_TAG_subprogram)
2187 break;
2188 } else
2189 break;
2190 }
2191 }
2192 assert(die && die.Tag() == DW_TAG_subprogram);
2193 if (GetFunction(die, sc)) {
2194 Address addr;
2195 // Parse all blocks if needed
2196 if (inlined_die) {
2197 Block &function_block = sc.function->GetBlock(true);
2198 sc.block = function_block.FindBlockByID(inlined_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00002199 if (sc.block == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002200 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Konrad Kleine248a1302019-05-23 11:14:47 +00002201 if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202 addr.Clear();
2203 } else {
Konrad Kleine248a1302019-05-23 11:14:47 +00002204 sc.block = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205 addr = sc.function->GetAddressRange().GetBaseAddress();
2206 }
2207
2208 if (addr.IsValid()) {
2209 sc_list.Append(sc);
2210 return true;
2211 }
2212 }
2213
2214 return false;
2215}
2216
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2218 const DWARFDIE &die) {
2219 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002220 // parent decl context isn't valid, we aren't trying to look for any
2221 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2223 return true;
2224
2225 if (die) {
2226 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2227 if (dwarf_ast) {
2228 CompilerDeclContext actual_decl_ctx =
2229 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2230 if (actual_decl_ctx)
Raphael Isemanna9469972019-03-12 07:45:04 +00002231 return decl_ctx->IsContainedInLookup(actual_decl_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002232 }
2233 }
2234 return false;
2235}
2236
Zachary Turner117b1fa2018-10-25 20:45:40 +00002237uint32_t SymbolFileDWARF::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002238 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00002239 FunctionNameType name_type_mask, bool include_inlines, bool append,
2240 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002241 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00002242 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2243 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 name.AsCString());
2245
2246 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002247 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2249
2250 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2251
2252 if (log) {
2253 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002254 log,
2255 "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2256 "name_type_mask=0x%x, append=%u, sc_list)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 name.GetCString(), name_type_mask, append);
2258 }
2259
2260 // If we aren't appending the results to this list, then clear the list
2261 if (!append)
2262 sc_list.Clear();
2263
2264 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2265 return 0;
2266
2267 // If name is empty then we won't find anything.
2268 if (name.IsEmpty())
2269 return 0;
2270
Adrian Prantl05097242018-04-30 16:49:04 +00002271 // Remember how many sc_list are in the list before we search in case we are
2272 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274 const uint32_t original_size = sc_list.GetSize();
2275
Pavel Labath5af11ab2018-06-05 10:33:56 +00002276 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2277 DIEArray offsets;
2278 CompilerDeclContext empty_decl_ctx;
2279 if (!parent_decl_ctx)
2280 parent_decl_ctx = &empty_decl_ctx;
2281
2282 std::vector<DWARFDIE> dies;
Pavel Labath3b926982019-06-21 07:56:50 +00002283 m_index->GetFunctions(name, *this, *parent_decl_ctx, name_type_mask, dies);
Jonas Devlieghere235339352019-08-13 19:51:51 +00002284 for (const DWARFDIE &die : dies) {
Pavel Labath5af11ab2018-06-05 10:33:56 +00002285 if (resolved_dies.insert(die.GetDIE()).second)
2286 ResolveFunction(die, include_inlines, sc_list);
2287 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288
2289 // Return the number of variable that were appended to the list
2290 const uint32_t num_matches = sc_list.GetSize() - original_size;
2291
2292 if (log && num_matches > 0) {
2293 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002294 log,
2295 "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2296 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2297 "%u",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002298 name.GetCString(), name_type_mask, include_inlines, append,
2299 num_matches);
2300 }
2301 return num_matches;
2302}
2303
2304uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2305 bool include_inlines, bool append,
2306 SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002307 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathf9d16472017-05-15 13:02:37 +00002308 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2309 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002310 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311
2312 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2313
2314 if (log) {
2315 GetObjectFile()->GetModule()->LogMessage(
2316 log,
2317 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002318 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319 }
2320
2321 // If we aren't appending the results to this list, then clear the list
2322 if (!append)
2323 sc_list.Clear();
2324
Pavel Labathb13f0332018-05-21 14:12:52 +00002325 DWARFDebugInfo *info = DebugInfo();
2326 if (!info)
2327 return 0;
2328
Adrian Prantl05097242018-04-30 16:49:04 +00002329 // Remember how many sc_list are in the list before we search in case we are
2330 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002331 uint32_t original_size = sc_list.GetSize();
2332
Pavel Labath6de9c792018-06-05 12:13:22 +00002333 DIEArray offsets;
2334 m_index->GetFunctions(regex, offsets);
2335
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002336 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2337 for (DIERef ref : offsets) {
2338 DWARFDIE die = info->GetDIE(ref);
2339 if (!die) {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002340 m_index->ReportInvalidDIERef(ref, regex.GetText());
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002341 continue;
2342 }
2343 if (resolved_dies.insert(die.GetDIE()).second)
2344 ResolveFunction(die, include_inlines, sc_list);
2345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346
2347 // Return the number of variable that were appended to the list
2348 return sc_list.GetSize() - original_size;
2349}
2350
2351void SymbolFileDWARF::GetMangledNamesForFunction(
2352 const std::string &scope_qualified_name,
2353 std::vector<ConstString> &mangled_names) {
2354 DWARFDebugInfo *info = DebugInfo();
2355 uint32_t num_comp_units = 0;
2356 if (info)
Jan Kratochvil60562732019-05-10 17:14:37 +00002357 num_comp_units = info->GetNumUnits();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358
2359 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002360 DWARFUnit *cu = info->GetUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 if (cu == nullptr)
2362 continue;
2363
2364 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2365 if (dwo)
2366 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2367 }
2368
Pavel Labatha71ce4f2019-06-17 07:32:56 +00002369 for (lldb::user_id_t uid :
2370 m_function_scope_qualified_name_map.lookup(scope_qualified_name)) {
2371 DWARFDIE die = GetDIE(uid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 mangled_names.push_back(ConstString(die.GetMangledName()));
2373 }
2374}
2375
2376uint32_t SymbolFileDWARF::FindTypes(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002377 ConstString name, const CompilerDeclContext *parent_decl_ctx, bool append,
2378 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002379 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2380 TypeMap &types) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002381 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002382 // If we aren't appending the results to this list, then clear the list
2383 if (!append)
2384 types.Clear();
2385
2386 // Make sure we haven't already searched this SymbolFile before...
2387 if (searched_symbol_files.count(this))
2388 return 0;
2389 else
2390 searched_symbol_files.insert(this);
2391
2392 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00002393 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002394 return 0;
2395
2396 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2397
2398 if (log) {
2399 if (parent_decl_ctx)
2400 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002401 log,
2402 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2403 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2405 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2406 else
2407 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002408 log,
2409 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2410 "NULL, append=%u, max_matches=%u, type_list)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411 name.GetCString(), append, max_matches);
2412 }
2413
2414 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2415 return 0;
2416
2417 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002418 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419 const size_t num_die_matches = die_offsets.size();
2420
2421 if (num_die_matches) {
2422 const uint32_t initial_types_size = types.GetSize();
2423 for (size_t i = 0; i < num_die_matches; ++i) {
2424 const DIERef &die_ref = die_offsets[i];
2425 DWARFDIE die = GetDIE(die_ref);
2426
2427 if (die) {
2428 if (!DIEInDeclContext(parent_decl_ctx, die))
2429 continue; // The containing decl contexts don't match
2430
2431 Type *matching_type = ResolveType(die, true, true);
2432 if (matching_type) {
2433 // We found a type pointer, now find the shared pointer form our type
2434 // list
2435 types.InsertUnique(matching_type->shared_from_this());
2436 if (types.GetSize() >= max_matches)
2437 break;
2438 }
2439 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002440 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002441 }
2442 }
2443 const uint32_t num_matches = types.GetSize() - initial_types_size;
2444 if (log && num_matches) {
2445 if (parent_decl_ctx) {
2446 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002447 log,
2448 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2449 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2451 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2452 num_matches);
2453 } else {
2454 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002455 log,
2456 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2457 "= NULL, append=%u, max_matches=%u, type_list) => %u",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002458 name.GetCString(), append, max_matches, num_matches);
2459 }
2460 }
Adrian Prantlfe9eaadd2019-08-08 21:16:01 +00002461 }
2462
2463 // Next search through the reachable Clang modules. This only applies for
2464 // DWARF objects compiled with -gmodules that haven't been processed by
2465 // dsymutil.
2466 if (num_die_matches < max_matches) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467 UpdateExternalModuleListIfNeeded();
2468
2469 for (const auto &pair : m_external_type_modules) {
2470 ModuleSP external_module_sp = pair.second;
2471 if (external_module_sp) {
Pavel Labath465eae32019-08-06 09:12:42 +00002472 SymbolFile *sym_file = external_module_sp->GetSymbolFile();
2473 if (sym_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002474 const uint32_t num_external_matches =
Pavel Labath465eae32019-08-06 09:12:42 +00002475 sym_file->FindTypes(name, parent_decl_ctx, append, max_matches,
2476 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477 if (num_external_matches)
2478 return num_external_matches;
2479 }
2480 }
2481 }
2482 }
2483
Adrian Prantlfe9eaadd2019-08-08 21:16:01 +00002484 return num_die_matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002485}
2486
Adrian Prantl330ae19a2019-08-21 18:06:56 +00002487size_t SymbolFileDWARF::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
Adrian Prantlaa97a892019-08-22 21:45:58 +00002488 LanguageSet languages, bool append,
2489 TypeMap &types) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002490 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002491 if (!append)
2492 types.Clear();
2493
Adrian Prantl330ae19a2019-08-21 18:06:56 +00002494 if (pattern.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 return 0;
2496
Adrian Prantl330ae19a2019-08-21 18:06:56 +00002497 ConstString name = pattern.back().name;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002498
2499 if (!name)
2500 return 0;
2501
Pavel Labathb13f0332018-05-21 14:12:52 +00002502 DIEArray die_offsets;
2503 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002504 const size_t num_die_matches = die_offsets.size();
2505
Adrian Prantl69cef322019-08-17 00:38:58 +00002506 size_t num_matches = 0;
2507 for (size_t i = 0; i < num_die_matches; ++i) {
2508 const DIERef &die_ref = die_offsets[i];
2509 DWARFDIE die = GetDIE(die_ref);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002510
Adrian Prantl69cef322019-08-17 00:38:58 +00002511 if (die) {
Adrian Prantlaa97a892019-08-22 21:45:58 +00002512 if (!languages[die.GetCU()->GetLanguageType()])
2513 continue;
2514
Adrian Prantl330ae19a2019-08-21 18:06:56 +00002515 llvm::SmallVector<CompilerContext, 4> die_context;
Adrian Prantl69cef322019-08-17 00:38:58 +00002516 die.GetDeclContext(die_context);
Adrian Prantl330ae19a2019-08-21 18:06:56 +00002517 if (!contextMatches(die_context, pattern))
Adrian Prantl69cef322019-08-17 00:38:58 +00002518 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002519
Adrian Prantl69cef322019-08-17 00:38:58 +00002520 Type *matching_type = ResolveType(die, true, true);
2521 if (matching_type) {
2522 // We found a type pointer, now find the shared pointer form our type
2523 // list
2524 types.InsertUnique(matching_type->shared_from_this());
2525 ++num_matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002526 }
Adrian Prantl69cef322019-08-17 00:38:58 +00002527 } else {
2528 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002530 }
Adrian Prantl69cef322019-08-17 00:38:58 +00002531 return num_matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532}
2533
2534CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002535SymbolFileDWARF::FindNamespace(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002536 const CompilerDeclContext *parent_decl_ctx) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00002537 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002538 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2539
2540 if (log) {
2541 GetObjectFile()->GetModule()->LogMessage(
2542 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2543 name.GetCString());
2544 }
2545
2546 CompilerDeclContext namespace_decl_ctx;
2547
2548 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2549 return namespace_decl_ctx;
2550
2551 DWARFDebugInfo *info = DebugInfo();
2552 if (info) {
2553 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002554 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002555 const size_t num_matches = die_offsets.size();
2556 if (num_matches) {
2557 for (size_t i = 0; i < num_matches; ++i) {
2558 const DIERef &die_ref = die_offsets[i];
2559 DWARFDIE die = GetDIE(die_ref);
2560
2561 if (die) {
2562 if (!DIEInDeclContext(parent_decl_ctx, die))
2563 continue; // The containing decl contexts don't match
2564
2565 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2566 if (dwarf_ast) {
2567 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2568 if (namespace_decl_ctx)
2569 break;
2570 }
2571 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002572 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573 }
2574 }
2575 }
2576 }
2577 if (log && namespace_decl_ctx) {
2578 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002579 log,
2580 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2581 "CompilerDeclContext(%p/%p) \"%s\"",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002582 name.GetCString(),
2583 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2584 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2585 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2586 }
2587
2588 return namespace_decl_ctx;
2589}
2590
2591TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2592 bool resolve_function_context) {
2593 TypeSP type_sp;
2594 if (die) {
2595 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
Konrad Kleine248a1302019-05-23 11:14:47 +00002596 if (type_ptr == nullptr) {
Pavel Labathad17e282019-06-13 11:22:47 +00002597 SymbolContextScope *scope;
2598 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()))
2599 scope = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2600 else
2601 scope = GetObjectFile()->GetModule().get();
2602 assert(scope);
2603 SymbolContext sc(scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002604 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2605 while (parent_die != nullptr) {
2606 if (parent_die->Tag() == DW_TAG_subprogram)
2607 break;
2608 parent_die = parent_die->GetParent();
2609 }
2610 SymbolContext sc_backup = sc;
2611 if (resolve_function_context && parent_die != nullptr &&
2612 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2613 sc = sc_backup;
2614
Konrad Kleine248a1302019-05-23 11:14:47 +00002615 type_sp = ParseType(sc, die, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2617 // Grab the existing type from the master types lists
2618 type_sp = type_ptr->shared_from_this();
2619 }
2620 }
2621 return type_sp;
2622}
2623
2624DWARFDIE
2625SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2626 if (orig_die) {
2627 DWARFDIE die = orig_die;
2628
2629 while (die) {
2630 // If this is the original DIE that we are searching for a declaration
2631 // for, then don't look in the cache as we don't want our own decl
2632 // context to be our decl context...
2633 if (orig_die != die) {
2634 switch (die.Tag()) {
2635 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002636 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002637 case DW_TAG_namespace:
2638 case DW_TAG_structure_type:
2639 case DW_TAG_union_type:
2640 case DW_TAG_class_type:
2641 case DW_TAG_lexical_block:
2642 case DW_TAG_subprogram:
2643 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002644 case DW_TAG_inlined_subroutine: {
2645 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2646 if (abs_die) {
2647 return abs_die;
2648 }
2649 break;
2650 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002651 default:
2652 break;
2653 }
2654 }
2655
2656 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2657 if (spec_die) {
2658 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2659 if (decl_ctx_die)
2660 return decl_ctx_die;
2661 }
2662
2663 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2664 if (abs_die) {
2665 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2666 if (decl_ctx_die)
2667 return decl_ctx_die;
2668 }
2669
2670 die = die.GetParent();
2671 }
2672 }
2673 return DWARFDIE();
2674}
2675
Jonas Devlieghere235339352019-08-13 19:51:51 +00002676Symbol *SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
Konrad Kleine248a1302019-05-23 11:14:47 +00002677 Symbol *objc_class_symbol = nullptr;
Pavel Labathd2deeb42019-07-31 08:25:25 +00002678 if (m_objfile_sp) {
2679 Symtab *symtab = m_objfile_sp->GetSymtab();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002680 if (symtab) {
2681 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2682 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2683 Symtab::eVisibilityAny);
2684 }
2685 }
2686 return objc_class_symbol;
2687}
2688
2689// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002690// they don't then we can end up looking through all class types for a complete
2691// type and never find the full definition. We need to know if this attribute
2692// is supported, so we determine this here and cache th result. We also need to
2693// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002694// DWARF file
2695// if we are doing darwin DWARF in .o file debugging.
Jonas Devlieghere235339352019-08-13 19:51:51 +00002696bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002697 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2698 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2699 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2700 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2701 else {
2702 DWARFDebugInfo *debug_info = DebugInfo();
2703 const uint32_t num_compile_units = GetNumCompileUnits();
2704 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvil60562732019-05-10 17:14:37 +00002705 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002706 if (dwarf_cu != cu &&
2707 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2708 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2709 break;
2710 }
2711 }
2712 }
2713 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2714 GetDebugMapSymfile())
2715 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2716 }
2717 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002718}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002719
2720// This function can be used when a DIE is found that is a forward declaration
2721// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002722TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002723 const DWARFDIE &die, ConstString type_name, bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002724
Kate Stoneb9c1b512016-09-06 20:57:50 +00002725 TypeSP type_sp;
2726
2727 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2728 return type_sp;
2729
2730 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002731 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002732
2733 const size_t num_matches = die_offsets.size();
2734
2735 if (num_matches) {
2736 for (size_t i = 0; i < num_matches; ++i) {
2737 const DIERef &die_ref = die_offsets[i];
2738 DWARFDIE type_die = GetDIE(die_ref);
2739
2740 if (type_die) {
2741 bool try_resolving_type = false;
2742
Adrian Prantl05097242018-04-30 16:49:04 +00002743 // Don't try and resolve the DIE we are looking for with the DIE
2744 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002745 if (type_die != die) {
2746 switch (type_die.Tag()) {
2747 case DW_TAG_class_type:
2748 case DW_TAG_structure_type:
2749 try_resolving_type = true;
2750 break;
2751 default:
2752 break;
2753 }
2754 }
2755
2756 if (try_resolving_type) {
2757 if (must_be_implementation &&
2758 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2759 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2760 DW_AT_APPLE_objc_complete_type, 0);
2761
2762 if (try_resolving_type) {
2763 Type *resolved_type = ResolveType(type_die, false, true);
2764 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2765 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2766 " (cu 0x%8.8" PRIx64 ")\n",
2767 die.GetID(),
Pavel Labathd2deeb42019-07-31 08:25:25 +00002768 m_objfile_sp->GetFileSpec().GetFilename().AsCString(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002769 "<Unknown>"),
2770 type_die.GetID(), type_cu->GetID());
2771
2772 if (die)
2773 GetDIEToType()[die.GetDIE()] = resolved_type;
2774 type_sp = resolved_type->shared_from_this();
2775 break;
2776 }
2777 }
2778 }
2779 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002780 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002781 }
2782 }
2783 }
2784 return type_sp;
2785}
Greg Claytona8022fa2012-04-24 21:22:41 +00002786
Adrian Prantl05097242018-04-30 16:49:04 +00002787// This function helps to ensure that the declaration contexts match for two
2788// different DIEs. Often times debug information will refer to a forward
2789// declaration of a type (the equivalent of "struct my_struct;". There will
2790// often be a declaration of that type elsewhere that has the full definition.
2791// When we go looking for the full type "my_struct", we will find one or more
2792// matches in the accelerator tables and we will then need to make sure the
2793// type was in the same declaration context as the original DIE. This function
2794// can efficiently compare two DIEs and will return true when the declaration
2795// context matches, and false when they don't.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2797 const DWARFDIE &die2) {
2798 if (die1 == die2)
2799 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002800
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002801 std::vector<DWARFDIE> decl_ctx_1;
2802 std::vector<DWARFDIE> decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002803 // The declaration DIE stack is a stack of the declaration context DIEs all
2804 // the way back to the compile unit. If a type "T" is declared inside a class
2805 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2806 // namespace "lldb", and the namespace is in a compile unit, there will be a
2807 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 //
2809 // [0] DW_TAG_class_type for "B"
2810 // [1] DW_TAG_class_type for "A"
2811 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002812 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 //
Adrian Prantl05097242018-04-30 16:49:04 +00002814 // We grab both contexts and make sure that everything matches all the way
2815 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816
2817 // First lets grab the decl contexts for both DIEs
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002818 decl_ctx_1 = die1.GetDeclContextDIEs();
2819 decl_ctx_2 = die2.GetDeclContextDIEs();
Adrian Prantl05097242018-04-30 16:49:04 +00002820 // Make sure the context arrays have the same size, otherwise we are done
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002821 const size_t count1 = decl_ctx_1.size();
2822 const size_t count2 = decl_ctx_2.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 if (count1 != count2)
2824 return false;
2825
Adrian Prantl05097242018-04-30 16:49:04 +00002826 // Make sure the DW_TAG values match all the way back up the compile unit. If
2827 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002828 DWARFDIE decl_ctx_die1;
2829 DWARFDIE decl_ctx_die2;
2830 size_t i;
2831 for (i = 0; i < count1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002832 decl_ctx_die1 = decl_ctx_1[i];
2833 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2835 return false;
2836 }
Adrian Prantl28f74662019-03-07 00:14:20 +00002837#ifndef NDEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002838
Kate Stoneb9c1b512016-09-06 20:57:50 +00002839 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002840 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2841 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2842 // function.
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002843 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002844 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2845 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002846
Greg Clayton890ff562012-02-02 05:48:16 +00002847#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002848 // Always skip the compile unit when comparing by only iterating up to "count
2849 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850 for (i = 0; i < count1 - 1; i++) {
Zachary Turner0eaa6d52019-03-12 20:50:46 +00002851 decl_ctx_die1 = decl_ctx_1[i];
2852 decl_ctx_die2 = decl_ctx_2[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 const char *name1 = decl_ctx_die1.GetName();
2854 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002855 // If the string was from a DW_FORM_strp, then the pointer will often be
2856 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002857 if (name1 == name2)
2858 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002859
Adrian Prantl05097242018-04-30 16:49:04 +00002860 // Name pointers are not equal, so only compare the strings if both are not
2861 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002862 if (name1 && name2) {
2863 // If the strings don't compare, we are done...
2864 if (strcmp(name1, name2) != 0)
2865 return false;
2866 } else {
2867 // One name was NULL while the other wasn't
2868 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 }
Adrian Prantl05097242018-04-30 16:49:04 +00002871 // We made it through all of the checks and the declaration contexts are
2872 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002873 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002874}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2877 const DWARFDeclContext &dwarf_decl_ctx) {
2878 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002879
Kate Stoneb9c1b512016-09-06 20:57:50 +00002880 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2881 if (dwarf_decl_ctx_count > 0) {
2882 const ConstString type_name(dwarf_decl_ctx[0].name);
2883 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002884
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 if (type_name) {
2886 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2887 DWARF_LOG_LOOKUPS));
2888 if (log) {
2889 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002890 log,
2891 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2892 "s, qualified-name='%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002893 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2894 dwarf_decl_ctx.GetQualifiedName());
2895 }
2896
2897 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002898 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002899 const size_t num_matches = die_offsets.size();
2900
Adrian Prantl05097242018-04-30 16:49:04 +00002901 // Get the type system that we are looking to find a type for. We will
2902 // use this to ensure any matches we find are in a language that this
2903 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002904 const LanguageType language = dwarf_decl_ctx.GetLanguage();
Alex Langford0e252e32019-07-30 22:12:34 +00002905 TypeSystem *type_system = nullptr;
2906 if (language != eLanguageTypeUnknown) {
2907 auto type_system_or_err = GetTypeSystemForLanguage(language);
2908 if (auto err = type_system_or_err.takeError()) {
2909 LLDB_LOG_ERROR(
2910 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
2911 std::move(err), "Cannot get TypeSystem for language {}",
2912 Language::GetNameForLanguageType(language));
2913 } else {
2914 type_system = &type_system_or_err.get();
2915 }
2916 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 if (num_matches) {
2918 for (size_t i = 0; i < num_matches; ++i) {
2919 const DIERef &die_ref = die_offsets[i];
2920 DWARFDIE type_die = GetDIE(die_ref);
2921
2922 if (type_die) {
2923 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002924 // looking for. We don't want to find a "Foo" type from Java if we
2925 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 if (type_system &&
2927 !type_system->SupportsLanguage(type_die.GetLanguage()))
2928 continue;
2929 bool try_resolving_type = false;
2930
2931 // Don't try and resolve the DIE we are looking for with the DIE
2932 // itself!
2933 const dw_tag_t type_tag = type_die.Tag();
2934 // Make sure the tags match
2935 if (type_tag == tag) {
2936 // The tags match, lets try resolving this type
2937 try_resolving_type = true;
2938 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002939 // The tags don't match, but we need to watch our for a forward
2940 // declaration for a struct and ("struct foo") ends up being a
2941 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002942 switch (type_tag) {
2943 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002944 // We had a "class foo", see if we ended up with a "struct foo
2945 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002946 try_resolving_type = (tag == DW_TAG_structure_type);
2947 break;
2948 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002949 // We had a "struct foo", see if we ended up with a "class foo
2950 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002951 try_resolving_type = (tag == DW_TAG_class_type);
2952 break;
2953 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002954 // Tags don't match, don't event try to resolve using this type
2955 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002956 break;
2957 }
2958 }
2959
2960 if (try_resolving_type) {
2961 DWARFDeclContext type_dwarf_decl_ctx;
2962 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2963
2964 if (log) {
2965 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002966 log,
2967 "SymbolFileDWARF::"
2968 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2969 "qualified-name='%s') trying die=0x%8.8x (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002970 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2971 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2972 type_dwarf_decl_ctx.GetQualifiedName());
2973 }
2974
2975 // Make sure the decl contexts match all the way up
2976 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2977 Type *resolved_type = ResolveType(type_die, false);
2978 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2979 type_sp = resolved_type->shared_from_this();
2980 break;
2981 }
2982 }
2983 } else {
2984 if (log) {
2985 std::string qualified_name;
2986 type_die.GetQualifiedName(qualified_name);
2987 GetObjectFile()->GetModule()->LogMessage(
Jonas Devlieghere235339352019-08-13 19:51:51 +00002988 log,
2989 "SymbolFileDWARF::"
2990 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2991 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2993 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2994 qualified_name.c_str());
2995 }
2996 }
2997 } else {
Pavel Labath78b2cf72019-06-14 12:01:18 +00002998 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002999 }
3000 }
3001 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003002 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003003 }
3004 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003005}
3006
Kate Stoneb9c1b512016-09-06 20:57:50 +00003007TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3008 bool *type_is_new_ptr) {
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003009 if (!die)
3010 return {};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011
Alex Langford0e252e32019-07-30 22:12:34 +00003012 auto type_system_or_err =
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003013 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Alex Langford0e252e32019-07-30 22:12:34 +00003014 if (auto err = type_system_or_err.takeError()) {
3015 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
3016 std::move(err), "Unable to parse type");
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003017 return {};
Alex Langford0e252e32019-07-30 22:12:34 +00003018 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003019
Alex Langford0e252e32019-07-30 22:12:34 +00003020 DWARFASTParser *dwarf_ast = type_system_or_err->GetDWARFParser();
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003021 if (!dwarf_ast)
3022 return {};
Siva Chandra9293fc42016-01-07 23:32:34 +00003023
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003024 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3025 TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3026 if (type_sp) {
Pavel Labathf46e8972019-07-25 08:22:05 +00003027 GetTypeList().Insert(type_sp);
Pavel Labatha71ce4f2019-06-17 07:32:56 +00003028
3029 if (die.Tag() == DW_TAG_subprogram) {
3030 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3031 .GetScopeQualifiedName()
3032 .AsCString(""));
3033 if (scope_qualified_name.size()) {
3034 m_function_scope_qualified_name_map[scope_qualified_name].insert(
3035 die.GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003036 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003038 }
3039
3040 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003041}
3042
Kate Stoneb9c1b512016-09-06 20:57:50 +00003043size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3044 const DWARFDIE &orig_die,
3045 bool parse_siblings, bool parse_children) {
3046 size_t types_added = 0;
3047 DWARFDIE die = orig_die;
3048 while (die) {
3049 bool type_is_new = false;
3050 if (ParseType(sc, die, &type_is_new).get()) {
3051 if (type_is_new)
3052 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003054
3055 if (parse_children && die.HasChildren()) {
3056 if (die.Tag() == DW_TAG_subprogram) {
3057 SymbolContext child_sc(sc);
3058 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3059 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3060 } else
3061 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3062 }
3063
3064 if (parse_siblings)
3065 die = die.GetSibling();
3066 else
3067 die.Clear();
3068 }
3069 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003070}
3071
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003072size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003073 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003074 CompileUnit *comp_unit = func.GetCompileUnit();
3075 lldbassert(comp_unit);
3076
3077 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3078 if (!dwarf_cu)
3079 return 0;
3080
Kate Stoneb9c1b512016-09-06 20:57:50 +00003081 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003082 const dw_offset_t function_die_offset = func.GetID();
3083 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3084 if (function_die) {
3085 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3086 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003087 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003088
Kate Stoneb9c1b512016-09-06 20:57:50 +00003089 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003090}
3091
Zachary Turner863f8c12019-01-11 18:03:20 +00003092size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003093 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003094 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003095 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003096 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003097 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3098 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3099 SymbolContext sc;
3100 sc.comp_unit = &comp_unit;
3101 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003104
Kate Stoneb9c1b512016-09-06 20:57:50 +00003105 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003106}
3107
Kate Stoneb9c1b512016-09-06 20:57:50 +00003108size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00003109 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Konrad Kleine248a1302019-05-23 11:14:47 +00003110 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003111 DWARFDebugInfo *info = DebugInfo();
Konrad Kleine248a1302019-05-23 11:14:47 +00003112 if (info == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113 return 0;
3114
3115 if (sc.function) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003116 DWARFDIE function_die = GetDIE(sc.function->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003117
3118 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3119 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3120 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3121 const size_t num_variables = ParseVariables(
3122 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3123
3124 // Let all blocks know they have parse all their variables
3125 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3126 return num_variables;
3127 }
3128 } else if (sc.comp_unit) {
Jan Kratochvil60562732019-05-10 17:14:37 +00003129 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003130
Konrad Kleine248a1302019-05-23 11:14:47 +00003131 if (dwarf_cu == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132 return 0;
3133
3134 uint32_t vars_added = 0;
3135 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3136
Konrad Kleine248a1302019-05-23 11:14:47 +00003137 if (variables.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003138 variables = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139 sc.comp_unit->SetVariableList(variables);
3140
3141 DIEArray die_offsets;
Pavel Labath3b926982019-06-21 07:56:50 +00003142 m_index->GetGlobalVariables(dwarf_cu->GetNonSkeletonUnit(),
3143 die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 const size_t num_matches = die_offsets.size();
3145 if (num_matches) {
3146 for (size_t i = 0; i < num_matches; ++i) {
3147 const DIERef &die_ref = die_offsets[i];
3148 DWARFDIE die = GetDIE(die_ref);
3149 if (die) {
3150 VariableSP var_sp(
3151 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3152 if (var_sp) {
3153 variables->AddVariableIfUnique(var_sp);
3154 ++vars_added;
3155 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003156 } else
Pavel Labath78b2cf72019-06-14 12:01:18 +00003157 m_index->ReportInvalidDIERef(die_ref, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003158 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 }
3161 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163 }
3164 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003165}
3166
Kate Stoneb9c1b512016-09-06 20:57:50 +00003167VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3168 const DWARFDIE &die,
3169 const lldb::addr_t func_low_pc) {
3170 if (die.GetDWARF() != this)
3171 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003172
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 VariableSP var_sp;
3174 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003175 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003176
Kate Stoneb9c1b512016-09-06 20:57:50 +00003177 var_sp = GetDIEToVariable()[die.GetDIE()];
3178 if (var_sp)
3179 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003180
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 const dw_tag_t tag = die.Tag();
3182 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003183
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3185 (tag == DW_TAG_formal_parameter && sc.function)) {
3186 DWARFAttributes attributes;
3187 const size_t num_attributes = die.GetAttributes(attributes);
3188 DWARFDIE spec_die;
3189 if (num_attributes > 0) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003190 const char *name = nullptr;
3191 const char *mangled = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003192 Declaration decl;
3193 uint32_t i;
3194 DWARFFormValue type_die_form;
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003195 DWARFExpression location;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003196 bool is_external = false;
3197 bool is_artificial = false;
3198 bool location_is_const_value_data = false;
3199 bool has_explicit_location = false;
3200 DWARFFormValue const_value;
3201 Variable::RangeList scope_ranges;
3202 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003203
Kate Stoneb9c1b512016-09-06 20:57:50 +00003204 for (i = 0; i < num_attributes; ++i) {
3205 dw_attr_t attr = attributes.AttributeAtIndex(i);
3206 DWARFFormValue form_value;
3207
3208 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3209 switch (attr) {
3210 case DW_AT_decl_file:
3211 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3212 form_value.Unsigned()));
3213 break;
3214 case DW_AT_decl_line:
3215 decl.SetLine(form_value.Unsigned());
3216 break;
3217 case DW_AT_decl_column:
3218 decl.SetColumn(form_value.Unsigned());
3219 break;
3220 case DW_AT_name:
3221 name = form_value.AsCString();
3222 break;
3223 case DW_AT_linkage_name:
3224 case DW_AT_MIPS_linkage_name:
3225 mangled = form_value.AsCString();
3226 break;
3227 case DW_AT_type:
3228 type_die_form = form_value;
3229 break;
3230 case DW_AT_external:
3231 is_external = form_value.Boolean();
3232 break;
3233 case DW_AT_const_value:
3234 // If we have already found a DW_AT_location attribute, ignore this
3235 // attribute.
3236 if (!has_explicit_location) {
3237 location_is_const_value_data = true;
3238 // The constant value will be either a block, a data value or a
3239 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003240 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3242 // Retrieve the value as a block expression.
3243 uint32_t block_offset =
3244 form_value.BlockData() - debug_info_data.GetDataStart();
3245 uint32_t block_length = form_value.Unsigned();
Pavel Labathe588b8b2019-08-27 07:49:41 +00003246 location = DWARFExpression(
3247 module,
3248 DataExtractor(debug_info_data, block_offset, block_length),
3249 die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003250 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3251 // Retrieve the value as a data expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003252 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003253 if (auto data_length = form_value.GetFixedSize())
Pavel Labathe588b8b2019-08-27 07:49:41 +00003254 location = DWARFExpression(
3255 module,
3256 DataExtractor(debug_info_data, data_offset, *data_length),
3257 die.GetCU());
Greg Clayton6aad81c2019-05-24 22:08:50 +00003258 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003259 const uint8_t *data_pointer = form_value.BlockData();
3260 if (data_pointer) {
3261 form_value.Unsigned();
3262 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3263 // we need to get the byte size of the type later after we
3264 // create the variable
3265 const_value = form_value;
3266 }
Greg Clayton6aad81c2019-05-24 22:08:50 +00003267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268 } else {
3269 // Retrieve the value as a string expression.
3270 if (form_value.Form() == DW_FORM_strp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003271 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Greg Clayton6aad81c2019-05-24 22:08:50 +00003272 if (auto data_length = form_value.GetFixedSize())
Pavel Labathe588b8b2019-08-27 07:49:41 +00003273 location = DWARFExpression(module,
3274 DataExtractor(debug_info_data,
3275 data_offset,
3276 *data_length),
3277 die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003278 } else {
3279 const char *str = form_value.AsCString();
3280 uint32_t string_offset =
3281 str - (const char *)debug_info_data.GetDataStart();
3282 uint32_t string_length = strlen(str) + 1;
Pavel Labathe588b8b2019-08-27 07:49:41 +00003283 location = DWARFExpression(module,
3284 DataExtractor(debug_info_data,
3285 string_offset,
3286 string_length),
3287 die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003288 }
3289 }
3290 }
3291 break;
3292 case DW_AT_location: {
3293 location_is_const_value_data = false;
3294 has_explicit_location = true;
3295 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003296 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003297
3298 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003299 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003300 uint32_t block_length = form_value.Unsigned();
Pavel Labathe588b8b2019-08-27 07:49:41 +00003301 location = DWARFExpression(
3302 module, DataExtractor(data, block_offset, block_length),
3303 die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003304 } else {
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003305 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003306 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3307
3308 size_t loc_list_length = DWARFExpression::LocationListSize(
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003309 die.GetCU(), debug_loc_data, debug_loc_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003310 if (loc_list_length > 0) {
Pavel Labathe588b8b2019-08-27 07:49:41 +00003311 location = DWARFExpression(module,
3312 DataExtractor(debug_loc_data,
3313 debug_loc_offset,
3314 loc_list_length),
3315 die.GetCU());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3317 location.SetLocationListSlide(
3318 func_low_pc -
3319 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3320 }
3321 }
3322 } break;
3323 case DW_AT_specification:
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003324 spec_die = form_value.Reference();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003325 break;
Pavel Labath78cfe1e2019-05-29 09:22:36 +00003326 case DW_AT_start_scope:
3327 // TODO: Implement this.
3328 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003329 case DW_AT_artificial:
3330 is_artificial = form_value.Boolean();
3331 break;
3332 case DW_AT_accessibility:
3333 break; // accessibility =
3334 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3335 case DW_AT_declaration:
3336 case DW_AT_description:
3337 case DW_AT_endianity:
3338 case DW_AT_segment:
3339 case DW_AT_visibility:
3340 default:
3341 case DW_AT_abstract_origin:
3342 case DW_AT_sibling:
3343 break;
3344 }
3345 }
3346 }
3347
3348 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3349 const dw_tag_t parent_tag = die.GetParent().Tag();
3350 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003351 (parent_tag == DW_TAG_compile_unit ||
3352 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353 (parent_context_die.Tag() == DW_TAG_class_type ||
3354 parent_context_die.Tag() == DW_TAG_structure_type);
3355
3356 ValueType scope = eValueTypeInvalid;
3357
3358 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Konrad Kleine248a1302019-05-23 11:14:47 +00003359 SymbolContextScope *symbol_context_scope = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360
Sam McCall30621392016-11-22 11:40:25 +00003361 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003362 if (!mangled) {
3363 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003364 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3365 // of global variables with commands like "frame var j". For
3366 // example, if j were an int variable holding a value 4 and
3367 // declared in a namespace B which in turn is contained in a
3368 // namespace A, the command "frame var j" returns
3369 // "(int) A::B::j = 4".
3370 // If the compiler does not emit a linkage name, we should be
3371 // able to generate a fully qualified name from the
3372 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003373 if ((parent_tag == DW_TAG_compile_unit ||
3374 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003375 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3376 DWARFDeclContext decl_ctx;
3377
3378 die.GetDWARFDeclContext(decl_ctx);
3379 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3380 }
3381 }
3382
Kate Stoneb9c1b512016-09-06 20:57:50 +00003383 if (tag == DW_TAG_formal_parameter)
3384 scope = eValueTypeVariableArgument;
3385 else {
Sam McCall30621392016-11-22 11:40:25 +00003386 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3387 // or static variable, so we have to do a little digging:
3388 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3389 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3390 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3391 // Clang likes to combine small global variables into the same symbol
3392 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3393 // so we need to look through the whole expression.
3394 bool is_static_lifetime =
3395 has_explicit_mangled ||
3396 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003397 // Check if the location has a DW_OP_addr with any address value...
3398 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3399 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003400 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003401 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3402 if (op_error) {
3403 StreamString strm;
3404 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
Konrad Kleine248a1302019-05-23 11:14:47 +00003405 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003406 GetObjectFile()->GetModule()->ReportError(
3407 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003408 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003409 }
Sam McCall30621392016-11-22 11:40:25 +00003410 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3411 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003412 }
3413 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003414 if (debug_map_symfile)
3415 // Set the module of the expression to the linked module
3416 // instead of the oject file so the relocated address can be
3417 // found there.
3418 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003419
Sam McCall30621392016-11-22 11:40:25 +00003420 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003421 if (is_external)
3422 scope = eValueTypeVariableGlobal;
3423 else
3424 scope = eValueTypeVariableStatic;
3425
3426 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003427 // When leaving the DWARF in the .o files on darwin, when we have a
3428 // global variable that wasn't initialized, the .o file might not
3429 // have allocated a virtual address for the global variable. In
3430 // this case it will have created a symbol for the global variable
3431 // that is undefined/data and external and the value will be the
3432 // byte size of the variable. When we do the address map in
3433 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3434 // do some magic here so we can get the correct address for our
3435 // global variable. The address for all of these entries will be
3436 // zero, and there will be an undefined symbol in this object file,
3437 // and the executable will have a matching symbol with a good
3438 // address. So here we dig up the correct address and replace it in
3439 // the location for the variable, and set the variable's symbol
3440 // context scope to be that of the main executable so the file
3441 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003442 bool linked_oso_file_addr = false;
3443 if (is_external && location_DW_OP_addr == 0) {
3444 // we have a possible uninitialized extern global
3445 ConstString const_name(mangled ? mangled : name);
3446 ObjectFile *debug_map_objfile =
3447 debug_map_symfile->GetObjectFile();
3448 if (debug_map_objfile) {
3449 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3450 if (debug_map_symtab) {
3451 Symbol *exe_symbol =
3452 debug_map_symtab->FindFirstSymbolWithNameAndType(
3453 const_name, eSymbolTypeData, Symtab::eDebugYes,
3454 Symtab::eVisibilityExtern);
3455 if (exe_symbol) {
3456 if (exe_symbol->ValueIsAddress()) {
3457 const addr_t exe_file_addr =
3458 exe_symbol->GetAddressRef().GetFileAddress();
3459 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3460 if (location.Update_DW_OP_addr(exe_file_addr)) {
3461 linked_oso_file_addr = true;
3462 symbol_context_scope = exe_symbol;
3463 }
3464 }
3465 }
3466 }
3467 }
3468 }
3469 }
3470
3471 if (!linked_oso_file_addr) {
3472 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003473 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003474 const lldb::addr_t exe_file_addr =
3475 debug_map_symfile->LinkOSOFileAddress(this,
3476 location_DW_OP_addr);
3477 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3478 // Update the file address for this variable
3479 location.Update_DW_OP_addr(exe_file_addr);
3480 } else {
3481 // Variable didn't make it into the final executable
3482 return var_sp;
3483 }
3484 }
3485 }
3486 } else {
3487 if (location_is_const_value_data)
3488 scope = eValueTypeVariableStatic;
3489 else {
3490 scope = eValueTypeVariableLocal;
3491 if (debug_map_symfile) {
3492 // We need to check for TLS addresses that we need to fixup
3493 if (location.ContainsThreadLocalStorage()) {
3494 location.LinkThreadLocalStorage(
3495 debug_map_symfile->GetObjectFile()->GetModule(),
3496 [this, debug_map_symfile](
3497 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3498 return debug_map_symfile->LinkOSOFileAddress(
3499 this, unlinked_file_addr);
3500 });
3501 scope = eValueTypeVariableThreadLocal;
3502 }
3503 }
3504 }
3505 }
3506 }
3507
Konrad Kleine248a1302019-05-23 11:14:47 +00003508 if (symbol_context_scope == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003509 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003510 case DW_TAG_subprogram:
3511 case DW_TAG_inlined_subroutine:
3512 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003513 if (sc.function) {
3514 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3515 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003516 if (symbol_context_scope == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003517 symbol_context_scope = sc.function;
3518 }
3519 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003520
Kate Stoneb9c1b512016-09-06 20:57:50 +00003521 default:
3522 symbol_context_scope = sc.comp_unit;
3523 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003524 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003525 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003526
Kate Stoneb9c1b512016-09-06 20:57:50 +00003527 if (symbol_context_scope) {
3528 SymbolFileTypeSP type_sp(
Pavel Labath67b45ac2019-06-19 07:32:39 +00003529 new SymbolFileType(*this, GetUID(type_die_form.Reference())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003530
3531 if (const_value.Form() && type_sp && type_sp->GetType())
Jonas Devlieghere04a087a2019-05-28 17:34:05 +00003532 location.UpdateValue(const_value.Unsigned(),
3533 type_sp->GetType()->GetByteSize().getValueOr(0),
3534 die.GetCU()->GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003535
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003536 var_sp = std::make_shared<Variable>(
3537 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3538 scope_ranges, &decl, location, is_external, is_artificial,
3539 is_static_member);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003540
3541 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3542 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003543 // Not ready to parse this variable yet. It might be a global or static
3544 // variable that is in a function scope and the function in the symbol
3545 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003546 return var_sp;
3547 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003548 }
Adrian Prantl05097242018-04-30 16:49:04 +00003549 // Cache var_sp even if NULL (the variable was just a specification or was
3550 // missing vital information to be able to be displayed in the debugger
3551 // (missing location due to optimization, etc)) so we don't re-parse this
3552 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003553 GetDIEToVariable()[die.GetDIE()] = var_sp;
3554 if (spec_die)
3555 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3556 }
3557 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003558}
3559
Kate Stoneb9c1b512016-09-06 20:57:50 +00003560DWARFDIE
3561SymbolFileDWARF::FindBlockContainingSpecification(
3562 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3563 // Give the concrete function die specified by "func_die_offset", find the
3564 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3565 // to "spec_block_die_offset"
3566 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3567 spec_block_die_offset);
3568}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003569
Kate Stoneb9c1b512016-09-06 20:57:50 +00003570DWARFDIE
3571SymbolFileDWARF::FindBlockContainingSpecification(
3572 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3573 if (die) {
3574 switch (die.Tag()) {
3575 case DW_TAG_subprogram:
3576 case DW_TAG_inlined_subroutine:
3577 case DW_TAG_lexical_block: {
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003578 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3579 spec_block_die_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003580 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003581
Jan Kratochvile8a039d2019-05-15 19:22:33 +00003582 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00003583 spec_block_die_offset)
3584 return die;
3585 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003586 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587
3588 // Give the concrete function die specified by "func_die_offset", find the
3589 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3590 // to "spec_block_die_offset"
3591 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3592 child_die = child_die.GetSibling()) {
3593 DWARFDIE result_die =
3594 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3595 if (result_die)
3596 return result_die;
3597 }
3598 }
3599
3600 return DWARFDIE();
3601}
3602
3603size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3604 const DWARFDIE &orig_die,
3605 const lldb::addr_t func_low_pc,
3606 bool parse_siblings, bool parse_children,
3607 VariableList *cc_variable_list) {
3608 if (!orig_die)
3609 return 0;
3610
3611 VariableListSP variable_list_sp;
3612
3613 size_t vars_added = 0;
3614 DWARFDIE die = orig_die;
3615 while (die) {
3616 dw_tag_t tag = die.Tag();
3617
3618 // Check to see if we have already parsed this variable or constant?
3619 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3620 if (var_sp) {
3621 if (cc_variable_list)
3622 cc_variable_list->AddVariableIfUnique(var_sp);
3623 } else {
3624 // We haven't already parsed it, lets do that now.
3625 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3626 (tag == DW_TAG_formal_parameter && sc.function)) {
Konrad Kleine248a1302019-05-23 11:14:47 +00003627 if (variable_list_sp.get() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003628 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3629 dw_tag_t parent_tag = sc_parent_die.Tag();
3630 switch (parent_tag) {
3631 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003632 case DW_TAG_partial_unit:
Konrad Kleine248a1302019-05-23 11:14:47 +00003633 if (sc.comp_unit != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003634 variable_list_sp = sc.comp_unit->GetVariableList(false);
Konrad Kleine248a1302019-05-23 11:14:47 +00003635 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003636 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003637 }
3638 } else {
3639 GetObjectFile()->GetModule()->ReportError(
3640 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
Jonas Devlieghere235339352019-08-13 19:51:51 +00003641 "symbol context for 0x%8.8" PRIx64 " %s.\n",
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3643 orig_die.GetID(), orig_die.GetTagAsCString());
3644 }
3645 break;
3646
3647 case DW_TAG_subprogram:
3648 case DW_TAG_inlined_subroutine:
3649 case DW_TAG_lexical_block:
Konrad Kleine248a1302019-05-23 11:14:47 +00003650 if (sc.function != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003651 // Check to see if we already have parsed the variables for the
3652 // given scope
3653
3654 Block *block = sc.function->GetBlock(true).FindBlockByID(
3655 sc_parent_die.GetID());
Konrad Kleine248a1302019-05-23 11:14:47 +00003656 if (block == nullptr) {
Adrian Prantl05097242018-04-30 16:49:04 +00003657 // This must be a specification or abstract origin with a
3658 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003659 // to find the concrete block so we can correctly add the
3660 // variable to it
3661 const DWARFDIE concrete_block_die =
3662 FindBlockContainingSpecification(
Pavel Labath2841e6e2019-05-08 11:43:05 +00003663 GetDIE(sc.function->GetID()),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003664 sc_parent_die.GetOffset());
3665 if (concrete_block_die)
3666 block = sc.function->GetBlock(true).FindBlockByID(
3667 concrete_block_die.GetID());
3668 }
3669
Konrad Kleine248a1302019-05-23 11:14:47 +00003670 if (block != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003671 const bool can_create = false;
3672 variable_list_sp = block->GetBlockVariableList(can_create);
Konrad Kleine248a1302019-05-23 11:14:47 +00003673 if (variable_list_sp.get() == nullptr) {
Jonas Devlieghere796ac802019-02-11 23:13:08 +00003674 variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003675 block->SetVariableList(variable_list_sp);
3676 }
3677 }
3678 }
3679 break;
3680
3681 default:
3682 GetObjectFile()->GetModule()->ReportError(
3683 "didn't find appropriate parent DIE for variable list for "
3684 "0x%8.8" PRIx64 " %s.\n",
3685 orig_die.GetID(), orig_die.GetTagAsCString());
3686 break;
3687 }
3688 }
3689
3690 if (variable_list_sp) {
3691 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3692 if (var_sp) {
3693 variable_list_sp->AddVariableIfUnique(var_sp);
3694 if (cc_variable_list)
3695 cc_variable_list->AddVariableIfUnique(var_sp);
3696 ++vars_added;
3697 }
3698 }
3699 }
3700 }
3701
Konrad Kleine248a1302019-05-23 11:14:47 +00003702 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003703
3704 if (!skip_children && parse_children && die.HasChildren()) {
3705 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3706 true, cc_variable_list);
3707 }
3708
3709 if (parse_siblings)
3710 die = die.GetSibling();
3711 else
3712 die.Clear();
3713 }
3714 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003715}
3716
Vedant Kumar4b36f792018-10-05 23:23:15 +00003717/// Collect call graph edges present in a function DIE.
3718static std::vector<lldb_private::CallEdge>
3719CollectCallEdges(DWARFDIE function_die) {
3720 // Check if the function has a supported call site-related attribute.
3721 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3722 uint64_t has_call_edges =
3723 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3724 if (!has_call_edges)
3725 return {};
3726
3727 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3728 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3729 function_die.GetPubname());
3730
3731 // Scan the DIE for TAG_call_site entries.
3732 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3733 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3734 // For now, assume that all entries are nested directly under the subprogram
3735 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3736 std::vector<CallEdge> call_edges;
3737 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3738 child = child.GetSibling()) {
3739 if (child.Tag() != DW_TAG_call_site)
3740 continue;
3741
3742 // Extract DW_AT_call_origin (the call target's DIE).
3743 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3744 if (!call_origin.IsValid()) {
3745 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3746 function_die.GetPubname());
3747 continue;
3748 }
3749
3750 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3751 // available. It should only ever be unavailable for tail call edges, in
3752 // which case use LLDB_INVALID_ADDRESS.
3753 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3754 LLDB_INVALID_ADDRESS);
3755
3756 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3757 call_origin.GetPubname(), return_pc);
3758 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3759 }
3760 return call_edges;
3761}
3762
3763std::vector<lldb_private::CallEdge>
3764SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
Pavel Labath2841e6e2019-05-08 11:43:05 +00003765 DWARFDIE func_die = GetDIE(func_id.GetID());
Vedant Kumar4b36f792018-10-05 23:23:15 +00003766 if (func_die.IsValid())
3767 return CollectCallEdges(func_die);
3768 return {};
3769}
3770
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003771// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00003772ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3773
3774uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3775
Pavel Labathe0119902019-07-23 09:24:02 +00003776void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
3777 SymbolFile::Dump(s);
3778 m_index->Dump(s);
3779}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003780
Zachary Turner49110232018-11-05 17:40:28 +00003781void SymbolFileDWARF::DumpClangAST(Stream &s) {
Alex Langford0e252e32019-07-30 22:12:34 +00003782 auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3783 if (!ts_or_err)
3784 return;
Jonas Devlieghere235339352019-08-13 19:51:51 +00003785 ClangASTContext *clang =
3786 llvm::dyn_cast_or_null<ClangASTContext>(&ts_or_err.get());
Zachary Turner49110232018-11-05 17:40:28 +00003787 if (!clang)
3788 return;
3789 clang->Dump(s);
3790}
3791
Kate Stoneb9c1b512016-09-06 20:57:50 +00003792SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
Konrad Kleine248a1302019-05-23 11:14:47 +00003793 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003794 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3795 if (module_sp) {
Pavel Labath23f70e82019-08-02 08:16:35 +00003796 m_debug_map_symfile =
3797 (SymbolFileDWARFDebugMap *)module_sp->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003798 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003799 }
3800 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003801}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003802
Pavel Labath4c2ef9a2019-03-22 16:07:58 +00003803DWARFExpression::LocationListFormat
3804SymbolFileDWARF::GetLocationListFormat() const {
3805 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
George Rimare4dee262018-10-23 09:46:15 +00003806 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003807 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003808}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003809
3810SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3811 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003812 ModuleSpec module_spec;
Pavel Labathd2deeb42019-07-31 08:25:25 +00003813 module_spec.GetFileSpec() = m_objfile_sp->GetFileSpec();
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003814 module_spec.GetSymbolFileSpec() =
Pavel Labathd2deeb42019-07-31 08:25:25 +00003815 FileSpec(m_objfile_sp->GetFileSpec().GetPath() + ".dwp");
Zachary Turnera89ce432019-03-06 18:20:23 +00003816
3817 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3818 FileSpec dwp_filespec =
3819 Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003820 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003821 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3822 dwp_filespec);
3823 }
3824 });
3825 return m_dwp_symfile.get();
3826}