blob: 2a0a89f0b25a19f1d52e42de54fe4ce4ea820bb3 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
Sean Callanancc427fa2011-07-30 02:42:06 +000012#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000013#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000014
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000016#include "lldb/Core/ModuleList.h"
17#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000020#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000022#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000023#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000024#include "lldb/Utility/Scalar.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000026#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Sean Callanan4dbb2712015-09-25 20:35:58 +000028#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Pavel Labathe1d18752018-06-07 10:04:44 +000029#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000030
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000031#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000032#include "lldb/Host/Host.h"
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +000033#include "lldb/Host/Symbols.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000034
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000035#include "lldb/Interpreter/OptionValueFileSpecList.h"
36#include "lldb/Interpreter/OptionValueProperties.h"
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000039#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000040#include "lldb/Symbol/ClangUtil.h"
41#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000042#include "lldb/Symbol/CompilerDecl.h"
43#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000044#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000045#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000048#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000049#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/VariableList.h"
51
Jim Ingham0e0984e2015-09-02 01:06:46 +000052#include "lldb/Target/Language.h"
53
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"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000057#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "DWARFDebugAbbrev.h"
59#include "DWARFDebugAranges.h"
60#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000062#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000064#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "DWARFFormValue.h"
Pavel 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
Zachary Turner7d86ee52017-03-08 17:56:08 +000074#include "llvm/Support/FileSystem.h"
75
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076#include <map>
77
Matthew Gardinere81df3b2014-08-26 06:57:23 +000078#include <ctype.h>
79#include <string.h>
80
Greg Clayton62742b12010-11-11 01:09:45 +000081//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000082
83#ifdef ENABLE_DEBUG_PRINTF
84#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000085#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000086#else
87#define DEBUG_PRINTF(fmt, ...)
88#endif
89
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090using namespace lldb;
91using namespace lldb_private;
92
Kate Stoneb9c1b512016-09-06 20:57:50 +000093// static inline bool
94// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000095//{
96// switch (tag)
97// {
98// default:
99// break;
100// case DW_TAG_subprogram:
101// case DW_TAG_inlined_subroutine:
102// case DW_TAG_class_type:
103// case DW_TAG_structure_type:
104// case DW_TAG_union_type:
105// return true;
106// }
107// return false;
108//}
109//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000110
111namespace {
112
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000113static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000115 {},
Pavel Labath9337b412018-06-06 11:35:23 +0000116 "If the DW_AT_comp_dir matches any of these paths the symbolic "
117 "links will be resolved at DWARF parse time."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000118 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
Pavel Labath9337b412018-06-06 11:35:23 +0000119 "Ignore indexes present in the object files and always index DWARF "
Tatyana Krasnukhae40db052018-09-27 07:11:58 +0000120 "manually."}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000121
Pavel Labath9337b412018-06-06 11:35:23 +0000122enum {
123 ePropertySymLinkPaths,
124 ePropertyIgnoreIndexes,
125};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127class PluginProperties : public Properties {
128public:
129 static ConstString GetSettingName() {
130 return SymbolFileDWARF::GetPluginNameStatic();
131 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 PluginProperties() {
134 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
135 m_collection_sp->Initialize(g_properties);
136 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 FileSpecList &GetSymLinkPaths() {
139 OptionValueFileSpecList *option_value =
140 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
141 nullptr, true, ePropertySymLinkPaths);
142 assert(option_value);
143 return option_value->GetCurrentValue();
144 }
Pavel Labath9337b412018-06-06 11:35:23 +0000145
146 bool IgnoreFileIndexes() const {
147 return m_collection_sp->GetPropertyAtIndexAsBoolean(
148 nullptr, ePropertyIgnoreIndexes, false);
149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
155 static const auto g_settings_sp(std::make_shared<PluginProperties>());
156 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000157}
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
162 if (!path_from_dwarf || !path_from_dwarf[0]) {
163 return path_from_dwarf;
164 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 const char *colon_pos = strchr(path_from_dwarf, ':');
167 if (nullptr == colon_pos) {
168 return path_from_dwarf;
169 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 const char *slash_pos = strchr(path_from_dwarf, '/');
172 if (slash_pos && (slash_pos < colon_pos)) {
173 return path_from_dwarf;
174 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000175
Adrian Prantl05097242018-04-30 16:49:04 +0000176 // check whether we have a windows path, and so the first character is a
177 // drive-letter not a hostname.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
179 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
180 return path_from_dwarf;
181 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 return colon_pos + 1;
184}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000185
Greg Clayton776cd7a2018-04-27 15:45:58 +0000186static FileSpec resolveCompDir(const char *path_from_dwarf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 if (!path_from_dwarf)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000188 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189
190 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
191 // Remove the host part if present.
192 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
193 if (!local_path)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000194 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195
196 bool is_symlink = false;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000197 // Always normalize our compile unit directory to get rid of redundant
198 // slashes and other path anomalies before we use it for path prepending
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000199 FileSpec local_spec(local_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
201 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
202 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
Greg Clayton776cd7a2018-04-27 15:45:58 +0000203 local_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204
205 if (!is_symlink)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000206 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207
Zachary Turner7d86ee52017-03-08 17:56:08 +0000208 namespace fs = llvm::sys::fs;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000209 if (fs::get_file_type(local_spec.GetPath(), false) !=
Zachary Turner7d86ee52017-03-08 17:56:08 +0000210 fs::file_type::symlink_file)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000211 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212
Greg Clayton776cd7a2018-04-27 15:45:58 +0000213 FileSpec resolved_symlink;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000214 const auto error = FileSystem::Instance().Readlink(local_spec, resolved_symlink);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 if (error.Success())
Greg Clayton776cd7a2018-04-27 15:45:58 +0000216 return resolved_symlink;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217
Greg Clayton776cd7a2018-04-27 15:45:58 +0000218 return local_spec;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000219}
220
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000221DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000222 return nullptr;
223}
224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225void SymbolFileDWARF::Initialize() {
226 LogChannelDWARF::Initialize();
227 PluginManager::RegisterPlugin(GetPluginNameStatic(),
228 GetPluginDescriptionStatic(), CreateInstance,
229 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000230}
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
233 if (!PluginManager::GetSettingForSymbolFilePlugin(
234 debugger, PluginProperties::GetSettingName())) {
235 const bool is_global_setting = true;
236 PluginManager::CreateSettingForSymbolFilePlugin(
237 debugger, GetGlobalPluginProperties()->GetValueProperties(),
238 ConstString("Properties for the dwarf symbol-file plug-in."),
239 is_global_setting);
240 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241}
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243void SymbolFileDWARF::Terminate() {
244 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000245 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
249 static ConstString g_name("dwarf");
250 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
254 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
258 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259}
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000262 // This method can be called without going through the symbol vendor so we
263 // need to lock the module.
264 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
266 if (debug_map_symfile)
267 return debug_map_symfile->GetTypeList();
268 else
269 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000270}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
272 dw_offset_t max_die_offset, uint32_t type_mask,
273 TypeSet &type_set) {
274 if (die) {
275 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 if (die_offset >= max_die_offset)
278 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (die_offset >= min_die_offset) {
281 const dw_tag_t tag = die.Tag();
282
283 bool add_type = false;
284
285 switch (tag) {
286 case DW_TAG_array_type:
287 add_type = (type_mask & eTypeClassArray) != 0;
288 break;
289 case DW_TAG_unspecified_type:
290 case DW_TAG_base_type:
291 add_type = (type_mask & eTypeClassBuiltin) != 0;
292 break;
293 case DW_TAG_class_type:
294 add_type = (type_mask & eTypeClassClass) != 0;
295 break;
296 case DW_TAG_structure_type:
297 add_type = (type_mask & eTypeClassStruct) != 0;
298 break;
299 case DW_TAG_union_type:
300 add_type = (type_mask & eTypeClassUnion) != 0;
301 break;
302 case DW_TAG_enumeration_type:
303 add_type = (type_mask & eTypeClassEnumeration) != 0;
304 break;
305 case DW_TAG_subroutine_type:
306 case DW_TAG_subprogram:
307 case DW_TAG_inlined_subroutine:
308 add_type = (type_mask & eTypeClassFunction) != 0;
309 break;
310 case DW_TAG_pointer_type:
311 add_type = (type_mask & eTypeClassPointer) != 0;
312 break;
313 case DW_TAG_rvalue_reference_type:
314 case DW_TAG_reference_type:
315 add_type = (type_mask & eTypeClassReference) != 0;
316 break;
317 case DW_TAG_typedef:
318 add_type = (type_mask & eTypeClassTypedef) != 0;
319 break;
320 case DW_TAG_ptr_to_member_type:
321 add_type = (type_mask & eTypeClassMemberPointer) != 0;
322 break;
323 }
324
325 if (add_type) {
326 const bool assert_not_being_parsed = true;
327 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
328 if (type) {
329 if (type_set.find(type) == type_set.end())
330 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000331 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000333 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334
335 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
336 child_die = child_die.GetSibling()) {
337 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
338 }
339 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000343 TypeClass type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000344
345{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000346 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000350 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (sc_scope)
352 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 if (comp_unit) {
355 dwarf_cu = GetDWARFCompileUnit(comp_unit);
356 if (dwarf_cu == 0)
357 return 0;
358 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
359 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
360 } else {
361 DWARFDebugInfo *info = DebugInfo();
362 if (info) {
363 const size_t num_cus = info->GetNumCompileUnits();
364 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
365 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
366 if (dwarf_cu) {
367 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000368 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
372
373 std::set<CompilerType> compiler_type_set;
374 size_t num_types_added = 0;
375 for (Type *type : type_set) {
376 CompilerType compiler_type = type->GetForwardCompilerType();
377 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
378 compiler_type_set.insert(compiler_type);
379 type_list.Insert(type->shared_from_this());
380 ++num_types_added;
381 }
382 }
383 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000384}
385
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386//----------------------------------------------------------------------
387// Gets the first parent that is a lexical block, function or inlined
388// subroutine, or compile unit.
389//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000390DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
392 DWARFDIE die;
393 for (die = child_die.GetParent(); die; die = die.GetParent()) {
394 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 switch (tag) {
397 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000398 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 case DW_TAG_subprogram:
400 case DW_TAG_inlined_subroutine:
401 case DW_TAG_lexical_block:
402 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 }
405 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406}
407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000409 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
410 << 32), // Used by SymbolFileDWARFDebugMap to
411 // when this class parses .o files to
412 // contain the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
414 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
415 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
George Rimar6e357122018-10-10 08:11:15 +0000416 m_data_debug_ranges(), m_data_debug_rnglists(), m_data_debug_str(),
417 m_data_apple_names(), m_data_apple_types(), m_data_apple_namespaces(),
418 m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
420 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422SymbolFileDWARF::~SymbolFileDWARF() {}
423
424static const ConstString &GetDWARFMachOSegmentName() {
425 static ConstString g_dwarf_section_name("__DWARF");
426 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427}
428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
430 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
431 if (debug_map_symfile)
432 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
433 else
434 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000435}
436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
438 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
439 TypeSystem *type_system;
440 if (debug_map_symfile) {
441 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
442 } else {
443 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
444 if (type_system)
445 type_system->SetSymbolFile(this);
446 }
447 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000451 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 ModuleSP module_sp(m_obj_file->GetModule());
453 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000454 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000455 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000459 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 }
461
Pavel Labath9337b412018-06-06 11:35:23 +0000462 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
463 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
464 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
465 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
466 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
467 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468
Pavel Labath9337b412018-06-06 11:35:23 +0000469 m_index = AppleDWARFIndex::Create(
470 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
471 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472
Pavel Labath9337b412018-06-06 11:35:23 +0000473 if (m_index)
474 return;
475
476 DWARFDataExtractor debug_names;
477 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
478 if (debug_names.GetByteSize() > 0) {
479 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
480 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
481 debug_names, get_debug_str_data(),
482 DebugInfo());
483 if (index_or) {
484 m_index = std::move(*index_or);
485 return;
486 }
487 LLDB_LOG_ERROR(log, index_or.takeError(),
488 "Unable to read .debug_names data: {0}");
489 }
490 }
491
492 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
493 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494}
495
496bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000497 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498}
499
500uint32_t SymbolFileDWARF::CalculateAbilities() {
501 uint32_t abilities = 0;
502 if (m_obj_file != NULL) {
503 const Section *section = NULL;
504 const SectionList *section_list = m_obj_file->GetSectionList();
505 if (section_list == NULL)
506 return 0;
507
Adrian Prantl05097242018-04-30 16:49:04 +0000508 // On non Apple platforms we might have .debug_types debug info that is
509 // created by using "-fdebug-types-section". LLDB currently will try to
510 // load this debug info, but it causes crashes during debugging when types
511 // are missing since it doesn't know how to parse the info in the
512 // .debug_types type units. This causes all complex debug info types to be
513 // unresolved. Because this causes LLDB to crash and since it really
514 // doesn't provide a solid debuggiung experience, we should disable trying
515 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000516 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
517 m_obj_file->GetModule()->ReportWarning(
518 "lldb doesn’t support .debug_types debug info");
519 return 0;
520 }
521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 uint64_t debug_abbrev_file_size = 0;
523 uint64_t debug_info_file_size = 0;
524 uint64_t debug_line_file_size = 0;
525
526 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
527
528 if (section)
529 section_list = &section->GetChildren();
530
531 section =
532 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
533 if (section != NULL) {
534 debug_info_file_size = section->GetFileSize();
535
536 section =
537 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
538 .get();
539 if (section)
540 debug_abbrev_file_size = section->GetFileSize();
541
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000542 DWARFDebugAbbrev *abbrev = DebugAbbrev();
543 if (abbrev) {
544 std::set<dw_form_t> invalid_forms;
545 abbrev->GetUnsupportedForms(invalid_forms);
546 if (!invalid_forms.empty()) {
547 StreamString error;
548 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
549 for (auto form : invalid_forms)
550 error.Printf(" %#x", form);
551 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
552 return 0;
553 }
554 }
555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 section =
557 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
558 .get();
559 if (section)
560 debug_line_file_size = section->GetFileSize();
561 } else {
562 const char *symfile_dir_cstr =
563 m_obj_file->GetFileSpec().GetDirectory().GetCString();
564 if (symfile_dir_cstr) {
565 if (strcasestr(symfile_dir_cstr, ".dsym")) {
566 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000567 // We have a dSYM file that didn't have a any debug info. If the
568 // string table has a size of 1, then it was made from an
569 // executable with no debug info, or from an executable that was
570 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 section =
572 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
573 .get();
574 if (section && section->GetFileSize() == 1) {
575 m_obj_file->GetModule()->ReportWarning(
576 "empty dSYM file detected, dSYM was created with an "
577 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583
584 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
585 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
586 LocalVariables | VariableTypes;
587
588 if (debug_line_file_size > 0)
589 abilities |= LineTables;
590 }
591 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}
593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594const DWARFDataExtractor &
595SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
596 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000597 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
598 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
599 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
604 DWARFDataExtractor &data) {
605 ModuleSP module_sp(m_obj_file->GetModule());
606 const SectionList *section_list = module_sp->GetSectionList();
607 if (section_list) {
608 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
609 if (section_sp) {
610 // See if we memory mapped the DWARF segment?
611 if (m_dwarf_data.GetByteSize()) {
612 data.SetData(m_dwarf_data, section_sp->GetOffset(),
613 section_sp->GetFileSize());
614 } else {
615 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
616 data.Clear();
617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}
621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
623 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
624 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625}
626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
628 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
629}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
632 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
633 m_data_debug_aranges);
634}
635
636const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
637 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
638}
639
640const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
641 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
642}
643
644const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
645 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
646}
647
George Rimarc6c7bfc2018-09-13 17:06:47 +0000648const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
649 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
650}
651
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
653 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
654}
655
George Rimare4dee262018-10-23 09:46:15 +0000656const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
657 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
658 if (debugLocData.GetByteSize() > 0)
659 return debugLocData;
660 return get_debug_loclists_data();
661}
662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
664 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
665}
666
George Rimare4dee262018-10-23 09:46:15 +0000667const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
668 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
669 m_data_debug_loclists);
670}
671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
673 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
674 m_data_debug_ranges);
675}
676
George Rimar6e357122018-10-10 08:11:15 +0000677const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
678 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
679 m_data_debug_rnglists);
680}
681
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
683 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
684}
685
686const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
687 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
688 m_data_debug_str_offsets);
689}
690
Greg Clayton2550ca12018-05-08 17:19:24 +0000691const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
692 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
693}
694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
696 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
697}
698
699const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
700 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
701}
702
703const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
704 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
705 m_data_apple_namespaces);
706}
707
708const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
709 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
710}
711
Jan Kratochvile4777a92018-04-29 19:47:48 +0000712const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
713 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
714 m_data_gnu_debugaltlink);
715}
716
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
718 if (m_abbr.get() == NULL) {
719 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
720 if (debug_abbrev_data.GetByteSize() > 0) {
721 m_abbr.reset(new DWARFDebugAbbrev());
722 if (m_abbr.get())
723 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 }
726 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727}
728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
730 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731}
732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
734 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000735 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
736 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
737 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 if (get_debug_info_data().GetByteSize() > 0) {
739 m_info.reset(new DWARFDebugInfo());
740 if (m_info.get()) {
741 m_info->SetDwarfData(this);
742 }
Greg Clayton1f746072012-08-29 21:13:06 +0000743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 }
745 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
749 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750}
751
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000752DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
754 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000755 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756
757 DWARFDebugInfo *info = DebugInfo();
758 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000759 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
760 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000762 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
764 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
765 dwarf_cu->SetUserData(comp_unit);
766 return dwarf_cu;
767 }
768 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769}
770
George Rimar7e7f9c12018-10-25 10:25:45 +0000771DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000773 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
774 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
775 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000776
777 if (get_debug_ranges_data().GetByteSize() > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000779 else if (get_debug_rnglists_data().GetByteSize() > 0)
780 m_ranges.reset(new DWARFDebugRngLists());
781
782 if (m_ranges.get())
783 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 }
785 return m_ranges.get();
786}
787
George Rimar7e7f9c12018-10-25 10:25:45 +0000788const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 return m_ranges.get();
790}
791
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000792lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 uint32_t cu_idx) {
794 CompUnitSP cu_sp;
795 if (dwarf_cu) {
796 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
797 if (comp_unit) {
798 // We already parsed this compile unit, had out a shared pointer to it
799 cu_sp = comp_unit->shared_from_this();
800 } else {
801 if (dwarf_cu->GetSymbolFileDWARF() != this) {
802 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
803 cu_idx);
804 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
805 // Let the debug map create the compile unit
806 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
807 dwarf_cu->SetUserData(cu_sp.get());
808 } else {
809 ModuleSP module_sp(m_obj_file->GetModule());
810 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000811 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 if (cu_die) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000813 FileSpec cu_file_spec(cu_die.GetName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 if (cu_file_spec) {
815 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000816 // resolve the file. This can be expensive e.g. when the source
817 // files are
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818 // NFS mounted.
819 if (cu_file_spec.IsRelative()) {
820 const char *cu_comp_dir{
821 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
822 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
823 }
824
825 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000826 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 remapped_file))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000828 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 }
830
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000831 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
833
834 bool is_optimized = dwarf_cu->GetIsOptimized();
835 cu_sp.reset(new CompileUnit(
836 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
837 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
838 if (cu_sp) {
839 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000840 // try and get the first entry in the supports files from the
841 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 if (!cu_file_spec) {
843 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
844 if (cu_file_spec) {
845 (FileSpec &)(*cu_sp) = cu_file_spec;
846 // Also fix the invalid file spec which was copied from the
847 // compile unit.
848 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
849 }
850 }
851
852 dwarf_cu->SetUserData(cu_sp.get());
853
854 // Figure out the compile unit index if we weren't given one
855 if (cu_idx == UINT32_MAX)
856 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
857
858 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
859 cu_idx, cu_sp);
860 }
861 }
862 }
863 }
864 }
865 }
866 return cu_sp;
867}
868
869uint32_t SymbolFileDWARF::GetNumCompileUnits() {
870 DWARFDebugInfo *info = DebugInfo();
871 if (info)
872 return info->GetNumCompileUnits();
873 return 0;
874}
875
876CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000877 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 CompUnitSP cu_sp;
879 DWARFDebugInfo *info = DebugInfo();
880 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000881 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 if (dwarf_cu)
883 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
884 }
885 return cu_sp;
886}
887
Zachary Turner863f8c12019-01-11 18:03:20 +0000888Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
889 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000890 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 if (die.IsValid()) {
892 TypeSystem *type_system =
893 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
894
895 if (type_system) {
896 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
897 if (dwarf_ast)
Zachary Turner863f8c12019-01-11 18:03:20 +0000898 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 }
900 }
901 return nullptr;
902}
903
904bool SymbolFileDWARF::FixupAddress(Address &addr) {
905 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
906 if (debug_map_symfile) {
907 return debug_map_symfile->LinkOSOAddress(addr);
908 }
909 // This is a normal DWARF file, no address fixups need to happen
910 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000911}
Zachary Turner863f8c12019-01-11 18:03:20 +0000912lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000913 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000914 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 if (dwarf_cu)
916 return dwarf_cu->GetLanguageType();
917 else
918 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000919}
920
Zachary Turner863f8c12019-01-11 18:03:20 +0000921size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000922 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 size_t functions_added = 0;
Zachary Turner863f8c12019-01-11 18:03:20 +0000924 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 if (dwarf_cu) {
926 DWARFDIECollection function_dies;
927 const size_t num_functions =
928 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
929 size_t func_idx;
930 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
931 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
Zachary Turner863f8c12019-01-11 18:03:20 +0000932 if (comp_unit.FindFunctionByUID(die.GetID()).get() == NULL) {
933 if (ParseFunction(comp_unit, die))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 ++functions_added;
935 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937 // FixupTypes();
938 }
939 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940}
941
Zachary Turner863f8c12019-01-11 18:03:20 +0000942bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
943 FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000944 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000945 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000947 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 if (cu_die) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000950 FileSpec cu_comp_dir = resolveCompDir(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
952 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
953 DW_AT_stmt_list, DW_INVALID_OFFSET);
954 if (stmt_list != DW_INVALID_OFFSET) {
955 // All file indexes in DWARF are one based and a file of index zero is
956 // supposed to be the compile unit itself.
Zachary Turner863f8c12019-01-11 18:03:20 +0000957 support_files.Append(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958 return DWARFDebugLine::ParseSupportFiles(
Zachary Turner863f8c12019-01-11 18:03:20 +0000959 comp_unit.GetModule(), get_debug_line_data(), cu_comp_dir,
George Rimarc6c7bfc2018-09-13 17:06:47 +0000960 stmt_list, support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 }
964 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965}
966
Zachary Turner863f8c12019-01-11 18:03:20 +0000967bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000968 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +0000969 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 if (dwarf_cu)
971 return dwarf_cu->GetIsOptimized();
972 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000973}
974
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975bool SymbolFileDWARF::ParseImportedModules(
976 const lldb_private::SymbolContext &sc,
977 std::vector<lldb_private::ConstString> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000978 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000980 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 if (dwarf_cu) {
982 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
983 sc.comp_unit->GetLanguage())) {
984 UpdateExternalModuleListIfNeeded();
985
986 if (sc.comp_unit) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000987 const DWARFDIE die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988
989 if (die) {
990 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
991 child_die = child_die.GetSibling()) {
992 if (child_die.Tag() == DW_TAG_imported_declaration) {
993 if (DWARFDIE module_die =
994 child_die.GetReferencedDIE(DW_AT_import)) {
995 if (module_die.Tag() == DW_TAG_module) {
996 if (const char *name = module_die.GetAttributeValueAsString(
997 DW_AT_name, nullptr)) {
998 ConstString const_name(name);
999 imported_modules.push_back(const_name);
1000 }
Sean Callananb0300a42016-01-14 21:46:09 +00001001 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 }
Sean Callananb0300a42016-01-14 21:46:09 +00001003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001005 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 } else {
1007 for (const auto &pair : m_external_type_modules) {
1008 imported_modules.push_back(pair.first);
1009 }
1010 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001011 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 }
1013 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001014}
1015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016struct ParseDWARFLineTableCallbackInfo {
1017 LineTable *line_table;
1018 std::unique_ptr<LineSequence> sequence_ap;
1019 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020};
1021
1022//----------------------------------------------------------------------
1023// ParseStatementTableCallback
1024//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025static void ParseDWARFLineTableCallback(dw_offset_t offset,
1026 const DWARFDebugLine::State &state,
1027 void *userData) {
1028 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1029 // Just started parsing the line table
1030 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1031 // Done parsing line table, nothing to do for the cleanup
1032 } else {
1033 ParseDWARFLineTableCallbackInfo *info =
1034 (ParseDWARFLineTableCallbackInfo *)userData;
1035 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036
Adrian Prantl05097242018-04-30 16:49:04 +00001037 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 if (!info->sequence_ap.get()) {
1039 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1040 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 line_table->AppendLineEntryToSequence(
1043 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1044 state.column, state.file, state.is_stmt, state.basic_block,
1045 state.prologue_end, state.epilogue_begin, state.end_sequence);
1046 if (state.end_sequence) {
1047 // First, put the current sequence into the line table.
1048 line_table->InsertSequence(info->sequence_ap.get());
1049 // Then, empty it to prepare for the next sequence.
1050 info->sequence_ap->Clear();
1051 }
1052 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053}
1054
Zachary Turner863f8c12019-01-11 18:03:20 +00001055bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001056 ASSERT_MODULE_LOCK(this);
Zachary Turner863f8c12019-01-11 18:03:20 +00001057 if (comp_unit.GetLineTable() != NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059
Zachary Turner863f8c12019-01-11 18:03:20 +00001060 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +00001062 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 if (dwarf_cu_die) {
1064 const dw_offset_t cu_line_offset =
1065 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1066 DW_INVALID_OFFSET);
1067 if (cu_line_offset != DW_INVALID_OFFSET) {
Zachary Turner863f8c12019-01-11 18:03:20 +00001068 std::unique_ptr<LineTable> line_table_ap(new LineTable(&comp_unit));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 if (line_table_ap.get()) {
1070 ParseDWARFLineTableCallbackInfo info;
1071 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 /*
1074 * MIPS:
1075 * The SymbolContext may not have a valid target, thus we may not be
1076 * able
1077 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1078 * #0
1079 * for MIPS. Use ArchSpec to clear the bit #0.
1080 */
Pavel Labathf760f5a2019-01-03 10:37:19 +00001081 switch (GetObjectFile()->GetArchitecture().GetMachine()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 case llvm::Triple::mips:
1083 case llvm::Triple::mipsel:
1084 case llvm::Triple::mips64:
1085 case llvm::Triple::mips64el:
1086 info.addr_mask = ~((lldb::addr_t)1);
1087 break;
1088 default:
1089 info.addr_mask = ~((lldb::addr_t)0);
1090 break;
1091 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 lldb::offset_t offset = cu_line_offset;
1094 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1095 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001096 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1098 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001099 // We have an object file that has a line table with addresses that
1100 // are not linked. We need to link the line table and convert the
1101 // addresses that are relative to the .o file into addresses for
1102 // the main executable.
Zachary Turner863f8c12019-01-11 18:03:20 +00001103 comp_unit.SetLineTable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1105 } else {
Zachary Turner863f8c12019-01-11 18:03:20 +00001106 comp_unit.SetLineTable(line_table_ap.release());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 return true;
1108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 }
1113 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114}
1115
Siva Chandrad8335e92015-12-16 00:22:08 +00001116lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1118 auto iter = m_debug_macros_map.find(*offset);
1119 if (iter != m_debug_macros_map.end())
1120 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001121
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1123 if (debug_macro_data.GetByteSize() == 0)
1124 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1127 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 const DWARFDebugMacroHeader &header =
1130 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1131 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1132 header.OffsetIs64Bit(), offset, this,
1133 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001136}
1137
Zachary Turner863f8c12019-01-11 18:03:20 +00001138bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001139 ASSERT_MODULE_LOCK(this);
Siva Chandrad8335e92015-12-16 00:22:08 +00001140
Zachary Turner863f8c12019-01-11 18:03:20 +00001141 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001143 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144
Jan Kratochvil93afb052018-05-24 20:51:13 +00001145 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 if (!dwarf_cu_die)
1147 return false;
1148
1149 lldb::offset_t sect_offset =
1150 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1151 if (sect_offset == DW_INVALID_OFFSET)
1152 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1153 DW_INVALID_OFFSET);
1154 if (sect_offset == DW_INVALID_OFFSET)
1155 return false;
1156
Zachary Turner863f8c12019-01-11 18:03:20 +00001157 comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158
1159 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001160}
1161
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001162size_t SymbolFileDWARF::ParseBlocksRecursive(
1163 lldb_private::CompileUnit &comp_unit, Block *parent_block,
1164 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 size_t blocks_added = 0;
1166 DWARFDIE die = orig_die;
1167 while (die) {
1168 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001169
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 switch (tag) {
1171 case DW_TAG_inlined_subroutine:
1172 case DW_TAG_subprogram:
1173 case DW_TAG_lexical_block: {
1174 Block *block = NULL;
1175 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001176 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1177 // inlined functions. These will be parsed on their own as separate
1178 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179
1180 if (depth > 0)
1181 break;
1182
1183 block = parent_block;
1184 } else {
1185 BlockSP block_sp(new Block(die.GetID()));
1186 parent_block->AddChild(block_sp);
1187 block = block_sp.get();
1188 }
1189 DWARFRangeList ranges;
1190 const char *name = NULL;
1191 const char *mangled_name = NULL;
1192
1193 int decl_file = 0;
1194 int decl_line = 0;
1195 int decl_column = 0;
1196 int call_file = 0;
1197 int call_line = 0;
1198 int call_column = 0;
1199 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1200 decl_line, decl_column, call_file, call_line,
1201 call_column, nullptr)) {
1202 if (tag == DW_TAG_subprogram) {
1203 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1204 subprogram_low_pc = ranges.GetMinRangeBase(0);
1205 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001206 // We get called here for inlined subroutines in two ways. The first
1207 // time is when we are making the Function object for this inlined
1208 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001210 // need to adjust the containing address. The second time is when we
1211 // are parsing the blocks inside the function that contains the
1212 // inlined concrete instance. Since these will be blocks inside the
1213 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1215 subprogram_low_pc = ranges.GetMinRangeBase(0);
1216 }
1217 }
1218
1219 const size_t num_ranges = ranges.GetSize();
1220 for (size_t i = 0; i < num_ranges; ++i) {
1221 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1222 const addr_t range_base = range.GetRangeBase();
1223 if (range_base >= subprogram_low_pc)
1224 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1225 range.GetByteSize()));
1226 else {
1227 GetObjectFile()->GetModule()->ReportError(
1228 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1229 ") which has a base that is less than the function's low PC "
1230 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1231 "start of this error message",
1232 block->GetID(), range_base, range.GetRangeEnd(),
1233 subprogram_low_pc);
1234 }
1235 }
1236 block->FinalizeRanges();
1237
1238 if (tag != DW_TAG_subprogram &&
1239 (name != NULL || mangled_name != NULL)) {
1240 std::unique_ptr<Declaration> decl_ap;
1241 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1242 decl_ap.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001243 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 decl_line, decl_column));
1245
1246 std::unique_ptr<Declaration> call_ap;
1247 if (call_file != 0 || call_line != 0 || call_column != 0)
1248 call_ap.reset(new Declaration(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001249 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 call_line, call_column));
1251
1252 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1253 call_ap.get());
1254 }
1255
1256 ++blocks_added;
1257
1258 if (die.HasChildren()) {
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001259 blocks_added +=
1260 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1261 subprogram_low_pc, depth + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 }
1263 }
1264 } break;
1265 default:
1266 break;
1267 }
1268
Adrian Prantl05097242018-04-30 16:49:04 +00001269 // Only parse siblings of the block if we are not at depth zero. A depth of
1270 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1271 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272
1273 if (depth == 0)
1274 die.Clear();
1275 else
1276 die = die.GetSibling();
1277 }
1278 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001279}
1280
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1282 if (parent_die) {
1283 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1284 die = die.GetSibling()) {
1285 dw_tag_t tag = die.Tag();
1286 bool check_virtuality = false;
1287 switch (tag) {
1288 case DW_TAG_inheritance:
1289 case DW_TAG_subprogram:
1290 check_virtuality = true;
1291 break;
1292 default:
1293 break;
1294 }
1295 if (check_virtuality) {
1296 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1297 return true;
1298 }
1299 }
1300 }
1301 return false;
1302}
1303
1304void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1305 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1306 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1307 std::vector<DWARFDIE> decl_ctx_die_list =
1308 ast_parser->GetDIEForDeclContext(decl_ctx);
1309
1310 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1311 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1312 decl = decl.GetSibling())
1313 ast_parser->GetDeclForUIDFromDWARF(decl);
1314}
1315
1316SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001317 // This method can be called without going through the symbol vendor so we
1318 // need to lock the module.
1319 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001320 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1321 // must make sure we use the correct DWARF file when resolving things. On
1322 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1323 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1324 // references to other DWARF objects and we must be ready to receive a
1325 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 // instance.
1327 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1328 if (debug_map)
1329 return debug_map->GetSymbolFileByOSOIndex(
1330 debug_map->GetOSOIndexFromUserID(uid));
1331 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001332}
1333
1334DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001336 // This method can be called without going through the symbol vendor so we
1337 // need to lock the module.
1338 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001339 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1340 // must make sure we use the correct DWARF file when resolving things. On
1341 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1342 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1343 // references to other DWARF objects and we must be ready to receive a
1344 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 // instance.
1346 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1347 if (dwarf)
1348 return dwarf->GetDIE(DIERef(uid, dwarf));
1349 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001350}
1351
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001353 // This method can be called without going through the symbol vendor so we
1354 // need to lock the module.
1355 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001356 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1357 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1358 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 DWARFDIE die = GetDIEFromUID(type_uid);
1360 if (die)
1361 return die.GetDecl();
1362 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001363}
1364
Greg Clayton99558cc42015-08-24 23:46:31 +00001365CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001367 // This method can be called without going through the symbol vendor so we
1368 // need to lock the module.
1369 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001370 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1371 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1372 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 DWARFDIE die = GetDIEFromUID(type_uid);
1374 if (die)
1375 return die.GetDeclContext();
1376 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001377}
1378
Greg Clayton99558cc42015-08-24 23:46:31 +00001379CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001381 // This method can be called without going through the symbol vendor so we
1382 // need to lock the module.
1383 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001384 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1385 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1386 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387 DWARFDIE die = GetDIEFromUID(type_uid);
1388 if (die)
1389 return die.GetContainingDeclContext();
1390 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391}
1392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001394 // This method can be called without going through the symbol vendor so we
1395 // need to lock the module.
1396 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001397 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1398 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1399 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 DWARFDIE type_die = GetDIEFromUID(type_uid);
1401 if (type_die)
1402 return type_die.ResolveType();
1403 else
1404 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
Adrian Prantleca07c52018-11-05 20:49:07 +00001407llvm::Optional<SymbolFile::ArrayInfo>
1408SymbolFileDWARF::GetDynamicArrayInfoForUID(
1409 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1410 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1411 DWARFDIE type_die = GetDIEFromUID(type_uid);
1412 if (type_die)
1413 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1414 else
1415 return llvm::None;
1416}
1417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1419 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001420}
1421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1423 bool assert_not_being_parsed) {
1424 if (die) {
1425 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1426 if (log)
1427 GetObjectFile()->GetModule()->LogMessage(
1428 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1429 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001430
Adrian Prantl05097242018-04-30 16:49:04 +00001431 // We might be coming in in the middle of a type tree (a class within a
1432 // class, an enum within a class), so parse any needed parent DIEs before
1433 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1435 if (decl_ctx_die) {
1436 if (log) {
1437 switch (decl_ctx_die.Tag()) {
1438 case DW_TAG_structure_type:
1439 case DW_TAG_union_type:
1440 case DW_TAG_class_type: {
1441 // Get the type, which could be a forward declaration
1442 if (log)
1443 GetObjectFile()->GetModule()->LogMessage(
1444 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1445 "resolve parent forward type for 0x%8.8x",
1446 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1447 decl_ctx_die.GetOffset());
1448 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001449
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 default:
1451 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001454 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 return ResolveType(die);
1456 }
1457 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001458}
1459
Greg Clayton6beaaa62011-01-17 03:46:26 +00001460// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001461// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1462// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463bool SymbolFileDWARF::HasForwardDeclForClangType(
1464 const CompilerType &compiler_type) {
1465 CompilerType compiler_type_no_qualifiers =
1466 ClangUtil::RemoveFastQualifiers(compiler_type);
1467 if (GetForwardDeclClangTypeToDie().count(
1468 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1469 return true;
1470 }
1471 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 ClangASTContext *clang_type_system =
1474 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1475 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001476 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 DWARFASTParserClang *ast_parser =
1478 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1479 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001480}
1481
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001483 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001484
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 ClangASTContext *clang_type_system =
1486 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1487 if (clang_type_system) {
1488 DWARFASTParserClang *ast_parser =
1489 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1490 if (ast_parser &&
1491 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1492 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1493 }
Jim Inghamc3549282012-01-11 02:21:12 +00001494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 // We have a struct/union/class/enum that needs to be fully resolved.
1496 CompilerType compiler_type_no_qualifiers =
1497 ClangUtil::RemoveFastQualifiers(compiler_type);
1498 auto die_it = GetForwardDeclClangTypeToDie().find(
1499 compiler_type_no_qualifiers.GetOpaqueQualType());
1500 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1501 // We have already resolved this type...
1502 return true;
1503 }
1504
1505 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1506 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001507 // Once we start resolving this type, remove it from the forward
1508 // declaration map in case anyone child members or other types require this
1509 // type to get resolved. The type will get resolved when all of the calls
1510 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 GetForwardDeclClangTypeToDie().erase(die_it);
1512
1513 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1514
1515 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1516 DWARF_LOG_TYPE_COMPLETION));
1517 if (log)
1518 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1519 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1520 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1521 type->GetName().AsCString());
1522 assert(compiler_type);
1523 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1524 if (dwarf_ast)
1525 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1526 }
1527 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001528}
1529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1531 bool assert_not_being_parsed,
1532 bool resolve_function_context) {
1533 if (die) {
1534 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1535
1536 if (assert_not_being_parsed) {
1537 if (type != DIE_IS_BEING_PARSED)
1538 return type;
1539
1540 GetObjectFile()->GetModule()->ReportError(
1541 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1542 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1543
1544 } else
1545 return type;
1546 }
1547 return nullptr;
1548}
1549
1550CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001551SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 uint32_t cu_idx) {
1553 // Check if the symbol vendor already knows about this compile unit?
1554 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001555 // The symbol vendor doesn't know about this compile unit, we need to parse
1556 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1558 }
1559 return (CompileUnit *)dwarf_cu->GetUserData();
1560}
1561
1562size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1563 DIEArray &method_die_offsets) {
1564 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001565 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 return method_die_offsets.size();
1567}
1568
1569bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1570 sc.Clear(false);
1571
1572 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1575
1576 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1577 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001578 sc.function = ParseFunction(*sc.comp_unit, die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579
1580 if (sc.function) {
1581 sc.module_sp = sc.function->CalculateSymbolContextModule();
1582 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584 }
1585
1586 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587}
1588
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1590 UpdateExternalModuleListIfNeeded();
1591 const auto &pos = m_external_type_modules.find(name);
1592 if (pos != m_external_type_modules.end())
1593 return pos->second;
1594 else
1595 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001596}
1597
Greg Clayton2f869fe2016-03-30 20:14:35 +00001598DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1600 DWARFDebugInfo *debug_info = DebugInfo();
1601 if (debug_info)
1602 return debug_info->GetDIE(die_ref);
1603 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001604 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001605}
1606
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607std::unique_ptr<SymbolFileDWARFDwo>
1608SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001609 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001611 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612 // info files for modules.
1613 if (GetDebugMapSymfile())
1614 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 const char *dwo_name = cu_die.GetAttributeValueAsString(
1617 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1618 if (!dwo_name)
1619 return nullptr;
1620
Tamas Berghammer963ce482017-08-25 13:56:14 +00001621 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1622 if (dwp_symfile) {
1623 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1624 DW_AT_GNU_dwo_id, 0);
1625 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1626 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1627 if (dwo_symfile)
1628 return dwo_symfile;
1629 }
1630
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001631 FileSpec dwo_file(dwo_name);
1632 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 if (dwo_file.IsRelative()) {
1634 const char *comp_dir = cu_die.GetAttributeValueAsString(
1635 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1636 if (!comp_dir)
1637 return nullptr;
1638
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001639 dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1640 FileSystem::Instance().Resolve(dwo_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 dwo_file.AppendPathComponent(dwo_name);
1642 }
1643
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001644 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 return nullptr;
1646
1647 const lldb::offset_t file_offset = 0;
1648 DataBufferSP dwo_file_data_sp;
1649 lldb::offset_t dwo_file_data_offset = 0;
1650 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1651 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001652 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1653 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 if (dwo_obj_file == nullptr)
1655 return nullptr;
1656
1657 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001658}
1659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1661 if (m_fetched_external_modules)
1662 return;
1663 m_fetched_external_modules = true;
1664
1665 DWARFDebugInfo *debug_info = DebugInfo();
1666
1667 const uint32_t num_compile_units = GetNumCompileUnits();
1668 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001669 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670
Jan Kratochvil93afb052018-05-24 20:51:13 +00001671 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001672 if (die && !die.HasChildren()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1674
1675 if (name) {
1676 ConstString const_name(name);
1677 if (m_external_type_modules.find(const_name) ==
1678 m_external_type_modules.end()) {
1679 ModuleSP module_sp;
1680 const char *dwo_path =
1681 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1682 if (dwo_path) {
1683 ModuleSpec dwo_module_spec;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001684 dwo_module_spec.GetFileSpec().SetFile(dwo_path,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001685 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1687 const char *comp_dir =
1688 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1689 if (comp_dir) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001690 dwo_module_spec.GetFileSpec().SetFile(comp_dir,
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001691 FileSpec::Style::native);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001692 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1694 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001695 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 dwo_module_spec.GetArchitecture() =
1697 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001698
Adrian Prantl05097242018-04-30 16:49:04 +00001699 // When LLDB loads "external" modules it looks at the presence of
1700 // DW_AT_GNU_dwo_name. However, when the already created module
1701 // (corresponding to .dwo itself) is being processed, it will see
1702 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1703 // dwo file) and will try to call ModuleList::GetSharedModule
1704 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1705 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1706 // DW_AT_comp_dir. In this case the method
1707 // ModuleList::GetSharedModule will fail and the warning will be
1708 // printed. However, as one can notice in this case we don't
1709 // actually need to try to load the already loaded module
1710 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001711 if (m_obj_file->GetFileSpec()
1712 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001713 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001714 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1715 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1716 continue;
1717 }
1718
Zachary Turner97206d52017-05-12 04:51:55 +00001719 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 dwo_module_spec, module_sp, NULL, NULL, NULL);
1721 if (!module_sp) {
1722 GetObjectFile()->GetModule()->ReportWarning(
1723 "0x%8.8x: unable to locate module needed for external types: "
1724 "%s\nerror: %s\nDebugging will be degraded due to missing "
1725 "types. Rebuilding your project will regenerate the needed "
1726 "module files.",
1727 die.GetOffset(),
1728 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1729 error.AsCString("unknown error"));
1730 }
1731 }
1732 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 }
1737}
1738
1739SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1740 if (!m_global_aranges_ap) {
1741 m_global_aranges_ap.reset(new GlobalVariableMap());
1742
1743 ModuleSP module_sp = GetObjectFile()->GetModule();
1744 if (module_sp) {
1745 const size_t num_cus = module_sp->GetNumCompileUnits();
1746 for (size_t i = 0; i < num_cus; ++i) {
1747 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1748 if (cu_sp) {
1749 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1750 if (globals_sp) {
1751 const size_t num_globals = globals_sp->GetSize();
1752 for (size_t g = 0; g < num_globals; ++g) {
1753 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1754 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1755 const DWARFExpression &location = var_sp->LocationExpression();
1756 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001757 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001758 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1759 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760 if (location_result.GetValueType() ==
1761 Value::eValueTypeFileAddress) {
1762 lldb::addr_t file_addr =
1763 location_result.GetScalar().ULongLong();
1764 lldb::addr_t byte_size = 1;
1765 if (var_sp->GetType())
1766 byte_size = var_sp->GetType()->GetByteSize();
1767 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1768 file_addr, byte_size, var_sp.get()));
1769 }
1770 }
1771 }
1772 }
1773 }
1774 }
1775 }
1776 }
1777 m_global_aranges_ap->Sort();
1778 }
1779 return *m_global_aranges_ap;
1780}
1781
1782uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001783 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001785 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1786 Timer scoped_timer(func_cat,
1787 "SymbolFileDWARF::"
1788 "ResolveSymbolContext (so_addr = { "
1789 "section = %p, offset = 0x%" PRIx64
1790 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 static_cast<void *>(so_addr.GetSection().get()),
1792 so_addr.GetOffset(), resolve_scope);
1793 uint32_t resolved = 0;
1794 if (resolve_scope &
1795 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1796 eSymbolContextLineEntry | eSymbolContextVariable)) {
1797 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1798
1799 DWARFDebugInfo *debug_info = DebugInfo();
1800 if (debug_info) {
1801 const dw_offset_t cu_offset =
1802 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1803 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001804 // Global variables are not in the compile unit address ranges. The
1805 // only way to currently find global variables is to iterate over the
1806 // .debug_pubnames or the __apple_names table and find all items in
1807 // there that point to DW_TAG_variable DIEs and then find the address
1808 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001809 if (resolve_scope & eSymbolContextVariable) {
1810 GlobalVariableMap &map = GetGlobalAranges();
1811 const GlobalVariableMap::Entry *entry =
1812 map.FindEntryThatContains(file_vm_addr);
1813 if (entry && entry->data) {
1814 Variable *variable = entry->data;
1815 SymbolContextScope *scc = variable->GetSymbolContextScope();
1816 if (scc) {
1817 scc->CalculateSymbolContext(&sc);
1818 sc.variable = variable;
1819 }
1820 return sc.GetResolvedMask();
1821 }
1822 }
1823 } else {
1824 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001825 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001826 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1827 if (dwarf_cu) {
1828 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1829 if (sc.comp_unit) {
1830 resolved |= eSymbolContextCompUnit;
1831
1832 bool force_check_line_table = false;
1833 if (resolve_scope &
1834 (eSymbolContextFunction | eSymbolContextBlock)) {
1835 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1836 DWARFDIE block_die;
1837 if (function_die) {
1838 sc.function =
1839 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1840 if (sc.function == NULL)
Zachary Turner863f8c12019-01-11 18:03:20 +00001841 sc.function = ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842
1843 if (sc.function && (resolve_scope & eSymbolContextBlock))
1844 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1845 } else {
1846 // We might have had a compile unit that had discontiguous
1847 // address ranges where the gaps are symbols that don't have
1848 // any debug info. Discontiguous compile unit address ranges
1849 // should only happen when there aren't other functions from
1850 // other compile units in these gaps. This helps keep the size
1851 // of the aranges down.
1852 force_check_line_table = true;
1853 }
1854
1855 if (sc.function != NULL) {
1856 resolved |= eSymbolContextFunction;
1857
1858 if (resolve_scope & eSymbolContextBlock) {
1859 Block &block = sc.function->GetBlock(true);
1860
1861 if (block_die)
1862 sc.block = block.FindBlockByID(block_die.GetID());
1863 else
1864 sc.block = block.FindBlockByID(function_die.GetID());
1865 if (sc.block)
1866 resolved |= eSymbolContextBlock;
1867 }
1868 }
1869 }
1870
1871 if ((resolve_scope & eSymbolContextLineEntry) ||
1872 force_check_line_table) {
1873 LineTable *line_table = sc.comp_unit->GetLineTable();
1874 if (line_table != NULL) {
1875 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001876 // terms of this debug file if there is no debug map, or it
1877 // will be an address in the .o file which needs to be fixed up
1878 // to be in terms of the debug map executable. Either way,
1879 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880 Address exe_so_addr(so_addr);
1881 if (FixupAddress(exe_so_addr)) {
1882 if (line_table->FindLineEntryByAddress(exe_so_addr,
1883 sc.line_entry)) {
1884 resolved |= eSymbolContextLineEntry;
1885 }
1886 }
1887 }
1888 }
1889
1890 if (force_check_line_table &&
1891 !(resolved & eSymbolContextLineEntry)) {
1892 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001893 // address ranges where the gaps are symbols that don't have any
1894 // debug info. Discontiguous compile unit address ranges should
1895 // only happen when there aren't other functions from other
1896 // compile units in these gaps. This helps keep the size of the
1897 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001898 sc.comp_unit = NULL;
1899 resolved &= ~eSymbolContextCompUnit;
1900 }
1901 } else {
1902 GetObjectFile()->GetModule()->ReportWarning(
1903 "0x%8.8x: compile unit %u failed to create a valid "
1904 "lldb_private::CompileUnit class.",
1905 cu_offset, cu_idx);
1906 }
1907 }
1908 }
1909 }
1910 }
1911 return resolved;
1912}
1913
1914uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1915 uint32_t line,
1916 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001917 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918 SymbolContextList &sc_list) {
1919 const uint32_t prev_size = sc_list.GetSize();
1920 if (resolve_scope & eSymbolContextCompUnit) {
1921 DWARFDebugInfo *debug_info = DebugInfo();
1922 if (debug_info) {
1923 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001924 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925
1926 for (cu_idx = 0;
1927 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1928 ++cu_idx) {
1929 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1930 const bool full_match = (bool)file_spec.GetDirectory();
1931 bool file_spec_matches_cu_file_spec =
1932 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1933 if (check_inlines || file_spec_matches_cu_file_spec) {
1934 SymbolContext sc(m_obj_file->GetModule());
1935 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1936 if (sc.comp_unit) {
1937 uint32_t file_idx = UINT32_MAX;
1938
Adrian Prantl05097242018-04-30 16:49:04 +00001939 // If we are looking for inline functions only and we don't find it
1940 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941 if (check_inlines) {
1942 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1943 1, file_spec, true);
1944 if (file_idx == UINT32_MAX)
1945 continue;
1946 }
1947
1948 if (line != 0) {
1949 LineTable *line_table = sc.comp_unit->GetLineTable();
1950
1951 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001952 // We will have already looked up the file index if we are
1953 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954 if (!check_inlines)
1955 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1956 1, file_spec, true);
1957
1958 if (file_idx != UINT32_MAX) {
1959 uint32_t found_line;
1960 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1961 0, file_idx, line, false, &sc.line_entry);
1962 found_line = sc.line_entry.line;
1963
1964 while (line_idx != UINT32_MAX) {
1965 sc.function = NULL;
1966 sc.block = NULL;
1967 if (resolve_scope &
1968 (eSymbolContextFunction | eSymbolContextBlock)) {
1969 const lldb::addr_t file_vm_addr =
1970 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1971 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1972 DWARFDIE function_die =
1973 dwarf_cu->LookupAddress(file_vm_addr);
1974 DWARFDIE block_die;
1975 if (function_die) {
1976 sc.function =
1977 sc.comp_unit
1978 ->FindFunctionByUID(function_die.GetID())
1979 .get();
1980 if (sc.function == NULL)
1981 sc.function =
Zachary Turner863f8c12019-01-11 18:03:20 +00001982 ParseFunction(*sc.comp_unit, function_die);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001983
1984 if (sc.function &&
1985 (resolve_scope & eSymbolContextBlock))
1986 block_die =
1987 function_die.LookupDeepestBlock(file_vm_addr);
1988 }
1989
1990 if (sc.function != NULL) {
1991 Block &block = sc.function->GetBlock(true);
1992
1993 if (block_die)
1994 sc.block = block.FindBlockByID(block_die.GetID());
1995 else if (function_die)
1996 sc.block =
1997 block.FindBlockByID(function_die.GetID());
1998 }
1999 }
2000 }
2001
2002 sc_list.Append(sc);
2003 line_idx = line_table->FindLineEntryIndexByFileIndex(
2004 line_idx + 1, file_idx, found_line, true,
2005 &sc.line_entry);
2006 }
2007 }
2008 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2009 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00002010 // sites by file and line and if the file spec matches that of
2011 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012 sc_list.Append(sc);
2013 }
2014 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2015 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00002016 // sites by file and line and if the file spec matches that of
2017 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 sc_list.Append(sc);
2019 }
2020
2021 if (!check_inlines)
2022 break;
2023 }
2024 }
2025 }
2026 }
2027 }
2028 return sc_list.GetSize() - prev_size;
2029}
2030
Jim Ingham7fca8c02017-04-28 00:51:06 +00002031void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002032 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00002033 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034}
2035
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002036std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
2037 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
2038 if (module_sp)
2039 return module_sp->GetMutex();
2040 return GetObjectFile()->GetModule()->GetMutex();
2041}
2042
Kate Stoneb9c1b512016-09-06 20:57:50 +00002043bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2044 const lldb_private::CompilerDeclContext *decl_ctx) {
2045 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00002046 // Invalid namespace decl which means we aren't matching only things in
2047 // this symbol file, so return true to indicate it matches this symbol
2048 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 return true;
2050 }
2051
2052 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2053 TypeSystem *type_system = GetTypeSystemForLanguage(
2054 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2055 if (decl_ctx_type_system == type_system)
2056 return true; // The type systems match, return true
2057
2058 // The namespace AST was valid, and it does not match...
2059 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2060
2061 if (log)
2062 GetObjectFile()->GetModule()->LogMessage(
2063 log, "Valid namespace does not match symbol file");
2064
2065 return false;
2066}
2067
2068uint32_t SymbolFileDWARF::FindGlobalVariables(
2069 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002070 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002071 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2072
2073 if (log)
2074 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002075 log,
2076 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2077 "parent_decl_ctx=%p, max_matches=%u, variables)",
2078 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002079 max_matches);
2080
2081 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2082 return 0;
2083
2084 DWARFDebugInfo *info = DebugInfo();
2085 if (info == NULL)
2086 return 0;
2087
Pavel Labath34cda142018-05-31 09:46:26 +00002088 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089 const uint32_t original_size = variables.GetSize();
2090
Pavel Labathe1d18752018-06-07 10:04:44 +00002091 llvm::StringRef basename;
2092 llvm::StringRef context;
2093
2094 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2095 context, basename))
2096 basename = name.GetStringRef();
2097
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002099 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002100 const size_t num_die_matches = die_offsets.size();
2101 if (num_die_matches) {
2102 SymbolContext sc;
2103 sc.module_sp = m_obj_file->GetModule();
2104 assert(sc.module_sp);
2105
Pavel Labathe1d18752018-06-07 10:04:44 +00002106 // Loop invariant: Variables up to this index have been checked for context
2107 // matches.
2108 uint32_t pruned_idx = original_size;
2109
Kate Stoneb9c1b512016-09-06 20:57:50 +00002110 bool done = false;
2111 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2112 const DIERef &die_ref = die_offsets[i];
2113 DWARFDIE die = GetDIE(die_ref);
2114
2115 if (die) {
2116 switch (die.Tag()) {
2117 default:
2118 case DW_TAG_subprogram:
2119 case DW_TAG_inlined_subroutine:
2120 case DW_TAG_try_block:
2121 case DW_TAG_catch_block:
2122 break;
2123
2124 case DW_TAG_variable: {
2125 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2126
2127 if (parent_decl_ctx) {
2128 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2129 if (dwarf_ast) {
2130 CompilerDeclContext actual_parent_decl_ctx =
2131 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2132 if (!actual_parent_decl_ctx ||
2133 actual_parent_decl_ctx != *parent_decl_ctx)
2134 continue;
2135 }
2136 }
2137
2138 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2139 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002140 while (pruned_idx < variables.GetSize()) {
2141 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2142 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2143 ++pruned_idx;
2144 else
2145 variables.RemoveVariableAtIndex(pruned_idx);
2146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147
2148 if (variables.GetSize() - original_size >= max_matches)
2149 done = true;
2150 } break;
2151 }
2152 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002153 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2154 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 }
2156 }
2157 }
2158
2159 // Return the number of variable that were appended to the list
2160 const uint32_t num_matches = variables.GetSize() - original_size;
2161 if (log && num_matches > 0) {
2162 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002163 log,
2164 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2165 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2166 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 max_matches, num_matches);
2168 }
2169 return num_matches;
2170}
2171
2172uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002173 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174 VariableList &variables) {
2175 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2176
2177 if (log) {
2178 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002179 log,
2180 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2181 "max_matches=%u, variables)",
2182 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 }
2184
2185 DWARFDebugInfo *info = DebugInfo();
2186 if (info == NULL)
2187 return 0;
2188
Pavel Labath34cda142018-05-31 09:46:26 +00002189 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 const uint32_t original_size = variables.GetSize();
2191
2192 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002193 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194
2195 SymbolContext sc;
2196 sc.module_sp = m_obj_file->GetModule();
2197 assert(sc.module_sp);
2198
2199 const size_t num_matches = die_offsets.size();
2200 if (num_matches) {
2201 for (size_t i = 0; i < num_matches; ++i) {
2202 const DIERef &die_ref = die_offsets[i];
2203 DWARFDIE die = GetDIE(die_ref);
2204
2205 if (die) {
2206 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2207
2208 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2209
2210 if (variables.GetSize() - original_size >= max_matches)
2211 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002212 } else
2213 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002214 }
2215 }
2216
2217 // Return the number of variable that were appended to the list
2218 return variables.GetSize() - original_size;
2219}
2220
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2222 bool include_inlines,
2223 SymbolContextList &sc_list) {
2224 SymbolContext sc;
2225
2226 if (!orig_die)
2227 return false;
2228
2229 // If we were passed a die that is not a function, just return false...
2230 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2231 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2232 return false;
2233
2234 DWARFDIE die = orig_die;
2235 DWARFDIE inlined_die;
2236 if (die.Tag() == DW_TAG_inlined_subroutine) {
2237 inlined_die = die;
2238
2239 while (1) {
2240 die = die.GetParent();
2241
2242 if (die) {
2243 if (die.Tag() == DW_TAG_subprogram)
2244 break;
2245 } else
2246 break;
2247 }
2248 }
2249 assert(die && die.Tag() == DW_TAG_subprogram);
2250 if (GetFunction(die, sc)) {
2251 Address addr;
2252 // Parse all blocks if needed
2253 if (inlined_die) {
2254 Block &function_block = sc.function->GetBlock(true);
2255 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2256 if (sc.block == NULL)
2257 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00002258 if (sc.block == NULL || !sc.block->GetStartAddress(addr))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259 addr.Clear();
2260 } else {
2261 sc.block = NULL;
2262 addr = sc.function->GetAddressRange().GetBaseAddress();
2263 }
2264
2265 if (addr.IsValid()) {
2266 sc_list.Append(sc);
2267 return true;
2268 }
2269 }
2270
2271 return false;
2272}
2273
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2275 const DWARFDIE &die) {
2276 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002277 // parent decl context isn't valid, we aren't trying to look for any
2278 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002279 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2280 return true;
2281
2282 if (die) {
2283 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2284 if (dwarf_ast) {
2285 CompilerDeclContext actual_decl_ctx =
2286 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2287 if (actual_decl_ctx)
2288 return actual_decl_ctx == *decl_ctx;
2289 }
2290 }
2291 return false;
2292}
2293
Zachary Turner117b1fa2018-10-25 20:45:40 +00002294uint32_t SymbolFileDWARF::FindFunctions(
2295 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2296 FunctionNameType name_type_mask, bool include_inlines, bool append,
2297 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002298 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2299 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002300 name.AsCString());
2301
2302 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002303 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002304 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2305
2306 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2307
2308 if (log) {
2309 GetObjectFile()->GetModule()->LogMessage(
2310 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2311 "name_type_mask=0x%x, append=%u, sc_list)",
2312 name.GetCString(), name_type_mask, append);
2313 }
2314
2315 // If we aren't appending the results to this list, then clear the list
2316 if (!append)
2317 sc_list.Clear();
2318
2319 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2320 return 0;
2321
2322 // If name is empty then we won't find anything.
2323 if (name.IsEmpty())
2324 return 0;
2325
Adrian Prantl05097242018-04-30 16:49:04 +00002326 // Remember how many sc_list are in the list before we search in case we are
2327 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328
Kate Stoneb9c1b512016-09-06 20:57:50 +00002329 const uint32_t original_size = sc_list.GetSize();
2330
2331 DWARFDebugInfo *info = DebugInfo();
2332 if (info == NULL)
2333 return 0;
2334
Pavel Labath5af11ab2018-06-05 10:33:56 +00002335 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2336 DIEArray offsets;
2337 CompilerDeclContext empty_decl_ctx;
2338 if (!parent_decl_ctx)
2339 parent_decl_ctx = &empty_decl_ctx;
2340
2341 std::vector<DWARFDIE> dies;
2342 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2343 for (const DWARFDIE &die: dies) {
2344 if (resolved_dies.insert(die.GetDIE()).second)
2345 ResolveFunction(die, include_inlines, sc_list);
2346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347
2348 // Return the number of variable that were appended to the list
2349 const uint32_t num_matches = sc_list.GetSize() - original_size;
2350
2351 if (log && num_matches > 0) {
2352 GetObjectFile()->GetModule()->LogMessage(
2353 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2354 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2355 "%u",
2356 name.GetCString(), name_type_mask, include_inlines, append,
2357 num_matches);
2358 }
2359 return num_matches;
2360}
2361
2362uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2363 bool include_inlines, bool append,
2364 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002365 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2366 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002367 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002368
2369 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2370
2371 if (log) {
2372 GetObjectFile()->GetModule()->LogMessage(
2373 log,
2374 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002375 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002376 }
2377
2378 // If we aren't appending the results to this list, then clear the list
2379 if (!append)
2380 sc_list.Clear();
2381
Pavel Labathb13f0332018-05-21 14:12:52 +00002382 DWARFDebugInfo *info = DebugInfo();
2383 if (!info)
2384 return 0;
2385
Adrian Prantl05097242018-04-30 16:49:04 +00002386 // Remember how many sc_list are in the list before we search in case we are
2387 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002388 uint32_t original_size = sc_list.GetSize();
2389
Pavel Labath6de9c792018-06-05 12:13:22 +00002390 DIEArray offsets;
2391 m_index->GetFunctions(regex, offsets);
2392
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002393 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2394 for (DIERef ref : offsets) {
2395 DWARFDIE die = info->GetDIE(ref);
2396 if (!die) {
2397 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2398 continue;
2399 }
2400 if (resolved_dies.insert(die.GetDIE()).second)
2401 ResolveFunction(die, include_inlines, sc_list);
2402 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403
2404 // Return the number of variable that were appended to the list
2405 return sc_list.GetSize() - original_size;
2406}
2407
2408void SymbolFileDWARF::GetMangledNamesForFunction(
2409 const std::string &scope_qualified_name,
2410 std::vector<ConstString> &mangled_names) {
2411 DWARFDebugInfo *info = DebugInfo();
2412 uint32_t num_comp_units = 0;
2413 if (info)
2414 num_comp_units = info->GetNumCompileUnits();
2415
2416 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002417 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002418 if (cu == nullptr)
2419 continue;
2420
2421 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2422 if (dwo)
2423 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2424 }
2425
2426 NameToOffsetMap::iterator iter =
2427 m_function_scope_qualified_name_map.find(scope_qualified_name);
2428 if (iter == m_function_scope_qualified_name_map.end())
2429 return;
2430
2431 DIERefSetSP set_sp = (*iter).second;
2432 std::set<DIERef>::iterator set_iter;
2433 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2434 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2435 mangled_names.push_back(ConstString(die.GetMangledName()));
2436 }
2437}
2438
2439uint32_t SymbolFileDWARF::FindTypes(
Zachary Turner576495e2019-01-14 22:41:21 +00002440 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2441 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002442 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2443 TypeMap &types) {
2444 // If we aren't appending the results to this list, then clear the list
2445 if (!append)
2446 types.Clear();
2447
2448 // Make sure we haven't already searched this SymbolFile before...
2449 if (searched_symbol_files.count(this))
2450 return 0;
2451 else
2452 searched_symbol_files.insert(this);
2453
2454 DWARFDebugInfo *info = DebugInfo();
2455 if (info == NULL)
2456 return 0;
2457
2458 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2459
2460 if (log) {
2461 if (parent_decl_ctx)
2462 GetObjectFile()->GetModule()->LogMessage(
2463 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2464 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2465 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2466 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2467 else
2468 GetObjectFile()->GetModule()->LogMessage(
2469 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2470 "NULL, append=%u, max_matches=%u, type_list)",
2471 name.GetCString(), append, max_matches);
2472 }
2473
2474 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2475 return 0;
2476
2477 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002478 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479 const size_t num_die_matches = die_offsets.size();
2480
2481 if (num_die_matches) {
2482 const uint32_t initial_types_size = types.GetSize();
2483 for (size_t i = 0; i < num_die_matches; ++i) {
2484 const DIERef &die_ref = die_offsets[i];
2485 DWARFDIE die = GetDIE(die_ref);
2486
2487 if (die) {
2488 if (!DIEInDeclContext(parent_decl_ctx, die))
2489 continue; // The containing decl contexts don't match
2490
2491 Type *matching_type = ResolveType(die, true, true);
2492 if (matching_type) {
2493 // We found a type pointer, now find the shared pointer form our type
2494 // list
2495 types.InsertUnique(matching_type->shared_from_this());
2496 if (types.GetSize() >= max_matches)
2497 break;
2498 }
2499 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002500 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2501 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002502 }
2503 }
2504 const uint32_t num_matches = types.GetSize() - initial_types_size;
2505 if (log && num_matches) {
2506 if (parent_decl_ctx) {
2507 GetObjectFile()->GetModule()->LogMessage(
2508 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2509 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2510 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2511 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2512 num_matches);
2513 } else {
2514 GetObjectFile()->GetModule()->LogMessage(
2515 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2516 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2517 name.GetCString(), append, max_matches, num_matches);
2518 }
2519 }
2520 return num_matches;
2521 } else {
2522 UpdateExternalModuleListIfNeeded();
2523
2524 for (const auto &pair : m_external_type_modules) {
2525 ModuleSP external_module_sp = pair.second;
2526 if (external_module_sp) {
2527 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2528 if (sym_vendor) {
2529 const uint32_t num_external_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002530 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches,
2531 searched_symbol_files, types);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532 if (num_external_matches)
2533 return num_external_matches;
2534 }
2535 }
2536 }
2537 }
2538
2539 return 0;
2540}
2541
2542size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2543 bool append, TypeMap &types) {
2544 if (!append)
2545 types.Clear();
2546
2547 if (context.empty())
2548 return 0;
2549
Kate Stoneb9c1b512016-09-06 20:57:50 +00002550 ConstString name = context.back().name;
2551
2552 if (!name)
2553 return 0;
2554
Pavel Labathb13f0332018-05-21 14:12:52 +00002555 DIEArray die_offsets;
2556 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002557 const size_t num_die_matches = die_offsets.size();
2558
2559 if (num_die_matches) {
2560 size_t num_matches = 0;
2561 for (size_t i = 0; i < num_die_matches; ++i) {
2562 const DIERef &die_ref = die_offsets[i];
2563 DWARFDIE die = GetDIE(die_ref);
2564
2565 if (die) {
2566 std::vector<CompilerContext> die_context;
Adrian Prantl95280c9482019-01-07 22:47:17 +00002567 die.GetDeclContext(die_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 if (die_context != context)
2569 continue;
2570
2571 Type *matching_type = ResolveType(die, true, true);
2572 if (matching_type) {
2573 // We found a type pointer, now find the shared pointer form our type
2574 // list
2575 types.InsertUnique(matching_type->shared_from_this());
2576 ++num_matches;
2577 }
2578 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002579 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2580 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002581 }
2582 }
2583 return num_matches;
2584 }
2585 return 0;
2586}
2587
2588CompilerDeclContext
Zachary Turnerc0a246a2019-01-14 22:41:00 +00002589SymbolFileDWARF::FindNamespace(const ConstString &name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 const CompilerDeclContext *parent_decl_ctx) {
2591 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2592
2593 if (log) {
2594 GetObjectFile()->GetModule()->LogMessage(
2595 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2596 name.GetCString());
2597 }
2598
2599 CompilerDeclContext namespace_decl_ctx;
2600
2601 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2602 return namespace_decl_ctx;
2603
2604 DWARFDebugInfo *info = DebugInfo();
2605 if (info) {
2606 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002607 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002608 const size_t num_matches = die_offsets.size();
2609 if (num_matches) {
2610 for (size_t i = 0; i < num_matches; ++i) {
2611 const DIERef &die_ref = die_offsets[i];
2612 DWARFDIE die = GetDIE(die_ref);
2613
2614 if (die) {
2615 if (!DIEInDeclContext(parent_decl_ctx, die))
2616 continue; // The containing decl contexts don't match
2617
2618 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2619 if (dwarf_ast) {
2620 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2621 if (namespace_decl_ctx)
2622 break;
2623 }
2624 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002625 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2626 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002627 }
2628 }
2629 }
2630 }
2631 if (log && namespace_decl_ctx) {
2632 GetObjectFile()->GetModule()->LogMessage(
2633 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2634 "CompilerDeclContext(%p/%p) \"%s\"",
2635 name.GetCString(),
2636 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2637 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2638 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2639 }
2640
2641 return namespace_decl_ctx;
2642}
2643
2644TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2645 bool resolve_function_context) {
2646 TypeSP type_sp;
2647 if (die) {
2648 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2649 if (type_ptr == NULL) {
2650 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2651 assert(lldb_cu);
2652 SymbolContext sc(lldb_cu);
2653 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2654 while (parent_die != nullptr) {
2655 if (parent_die->Tag() == DW_TAG_subprogram)
2656 break;
2657 parent_die = parent_die->GetParent();
2658 }
2659 SymbolContext sc_backup = sc;
2660 if (resolve_function_context && parent_die != nullptr &&
2661 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2662 sc = sc_backup;
2663
2664 type_sp = ParseType(sc, die, NULL);
2665 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2666 // Grab the existing type from the master types lists
2667 type_sp = type_ptr->shared_from_this();
2668 }
2669 }
2670 return type_sp;
2671}
2672
2673DWARFDIE
2674SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2675 if (orig_die) {
2676 DWARFDIE die = orig_die;
2677
2678 while (die) {
2679 // If this is the original DIE that we are searching for a declaration
2680 // for, then don't look in the cache as we don't want our own decl
2681 // context to be our decl context...
2682 if (orig_die != die) {
2683 switch (die.Tag()) {
2684 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002685 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002686 case DW_TAG_namespace:
2687 case DW_TAG_structure_type:
2688 case DW_TAG_union_type:
2689 case DW_TAG_class_type:
2690 case DW_TAG_lexical_block:
2691 case DW_TAG_subprogram:
2692 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002693 case DW_TAG_inlined_subroutine: {
2694 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2695 if (abs_die) {
2696 return abs_die;
2697 }
2698 break;
2699 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 default:
2701 break;
2702 }
2703 }
2704
2705 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2706 if (spec_die) {
2707 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2708 if (decl_ctx_die)
2709 return decl_ctx_die;
2710 }
2711
2712 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2713 if (abs_die) {
2714 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2715 if (decl_ctx_die)
2716 return decl_ctx_die;
2717 }
2718
2719 die = die.GetParent();
2720 }
2721 }
2722 return DWARFDIE();
2723}
2724
2725Symbol *
2726SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2727 Symbol *objc_class_symbol = NULL;
2728 if (m_obj_file) {
2729 Symtab *symtab = m_obj_file->GetSymtab();
2730 if (symtab) {
2731 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2732 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2733 Symtab::eVisibilityAny);
2734 }
2735 }
2736 return objc_class_symbol;
2737}
2738
2739// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002740// they don't then we can end up looking through all class types for a complete
2741// type and never find the full definition. We need to know if this attribute
2742// is supported, so we determine this here and cache th result. We also need to
2743// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002744// DWARF file
2745// if we are doing darwin DWARF in .o file debugging.
2746bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002747 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002748 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2749 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2750 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2751 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2752 else {
2753 DWARFDebugInfo *debug_info = DebugInfo();
2754 const uint32_t num_compile_units = GetNumCompileUnits();
2755 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002756 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002757 if (dwarf_cu != cu &&
2758 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2759 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2760 break;
2761 }
2762 }
2763 }
2764 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2765 GetDebugMapSymfile())
2766 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2767 }
2768 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002769}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002770
2771// This function can be used when a DIE is found that is a forward declaration
2772// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002773TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2774 const DWARFDIE &die, const ConstString &type_name,
2775 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002776
Kate Stoneb9c1b512016-09-06 20:57:50 +00002777 TypeSP type_sp;
2778
2779 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2780 return type_sp;
2781
2782 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002783 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002784
2785 const size_t num_matches = die_offsets.size();
2786
2787 if (num_matches) {
2788 for (size_t i = 0; i < num_matches; ++i) {
2789 const DIERef &die_ref = die_offsets[i];
2790 DWARFDIE type_die = GetDIE(die_ref);
2791
2792 if (type_die) {
2793 bool try_resolving_type = false;
2794
Adrian Prantl05097242018-04-30 16:49:04 +00002795 // Don't try and resolve the DIE we are looking for with the DIE
2796 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 if (type_die != die) {
2798 switch (type_die.Tag()) {
2799 case DW_TAG_class_type:
2800 case DW_TAG_structure_type:
2801 try_resolving_type = true;
2802 break;
2803 default:
2804 break;
2805 }
2806 }
2807
2808 if (try_resolving_type) {
2809 if (must_be_implementation &&
2810 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2811 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2812 DW_AT_APPLE_objc_complete_type, 0);
2813
2814 if (try_resolving_type) {
2815 Type *resolved_type = ResolveType(type_die, false, true);
2816 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2817 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2818 " (cu 0x%8.8" PRIx64 ")\n",
2819 die.GetID(),
2820 m_obj_file->GetFileSpec().GetFilename().AsCString(
2821 "<Unknown>"),
2822 type_die.GetID(), type_cu->GetID());
2823
2824 if (die)
2825 GetDIEToType()[die.GetDIE()] = resolved_type;
2826 type_sp = resolved_type->shared_from_this();
2827 break;
2828 }
2829 }
2830 }
2831 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002832 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2833 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 }
2835 }
2836 }
2837 return type_sp;
2838}
Greg Claytona8022fa2012-04-24 21:22:41 +00002839
Greg Clayton80c26302012-02-05 06:12:47 +00002840//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002841// This function helps to ensure that the declaration contexts match for two
2842// different DIEs. Often times debug information will refer to a forward
2843// declaration of a type (the equivalent of "struct my_struct;". There will
2844// often be a declaration of that type elsewhere that has the full definition.
2845// When we go looking for the full type "my_struct", we will find one or more
2846// matches in the accelerator tables and we will then need to make sure the
2847// type was in the same declaration context as the original DIE. This function
2848// can efficiently compare two DIEs and will return true when the declaration
2849// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002850//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002851bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2852 const DWARFDIE &die2) {
2853 if (die1 == die2)
2854 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002855
Kate Stoneb9c1b512016-09-06 20:57:50 +00002856 DWARFDIECollection decl_ctx_1;
2857 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002858 // The declaration DIE stack is a stack of the declaration context DIEs all
2859 // the way back to the compile unit. If a type "T" is declared inside a class
2860 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2861 // namespace "lldb", and the namespace is in a compile unit, there will be a
2862 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002863 //
2864 // [0] DW_TAG_class_type for "B"
2865 // [1] DW_TAG_class_type for "A"
2866 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002867 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002868 //
Adrian Prantl05097242018-04-30 16:49:04 +00002869 // We grab both contexts and make sure that everything matches all the way
2870 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002871
2872 // First lets grab the decl contexts for both DIEs
2873 die1.GetDeclContextDIEs(decl_ctx_1);
2874 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00002875 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876 const size_t count1 = decl_ctx_1.Size();
2877 const size_t count2 = decl_ctx_2.Size();
2878 if (count1 != count2)
2879 return false;
2880
Adrian Prantl05097242018-04-30 16:49:04 +00002881 // Make sure the DW_TAG values match all the way back up the compile unit. If
2882 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002883 DWARFDIE decl_ctx_die1;
2884 DWARFDIE decl_ctx_die2;
2885 size_t i;
2886 for (i = 0; i < count1; i++) {
2887 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2888 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2889 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2890 return false;
2891 }
Greg Clayton890ff562012-02-02 05:48:16 +00002892#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002893
Kate Stoneb9c1b512016-09-06 20:57:50 +00002894 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002895 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2896 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2897 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002898 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2899 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2900 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002901
Greg Clayton890ff562012-02-02 05:48:16 +00002902#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002903 // Always skip the compile unit when comparing by only iterating up to "count
2904 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002905 for (i = 0; i < count1 - 1; i++) {
2906 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2907 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2908 const char *name1 = decl_ctx_die1.GetName();
2909 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002910 // If the string was from a DW_FORM_strp, then the pointer will often be
2911 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002912 if (name1 == name2)
2913 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002914
Adrian Prantl05097242018-04-30 16:49:04 +00002915 // Name pointers are not equal, so only compare the strings if both are not
2916 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 if (name1 && name2) {
2918 // If the strings don't compare, we are done...
2919 if (strcmp(name1, name2) != 0)
2920 return false;
2921 } else {
2922 // One name was NULL while the other wasn't
2923 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002925 }
Adrian Prantl05097242018-04-30 16:49:04 +00002926 // We made it through all of the checks and the declaration contexts are
2927 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002929}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002930
Kate Stoneb9c1b512016-09-06 20:57:50 +00002931TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2932 const DWARFDeclContext &dwarf_decl_ctx) {
2933 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002934
Kate Stoneb9c1b512016-09-06 20:57:50 +00002935 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2936 if (dwarf_decl_ctx_count > 0) {
2937 const ConstString type_name(dwarf_decl_ctx[0].name);
2938 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002939
Kate Stoneb9c1b512016-09-06 20:57:50 +00002940 if (type_name) {
2941 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2942 DWARF_LOG_LOOKUPS));
2943 if (log) {
2944 GetObjectFile()->GetModule()->LogMessage(
2945 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2946 "s, qualified-name='%s')",
2947 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2948 dwarf_decl_ctx.GetQualifiedName());
2949 }
2950
2951 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002952 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002953 const size_t num_matches = die_offsets.size();
2954
Adrian Prantl05097242018-04-30 16:49:04 +00002955 // Get the type system that we are looking to find a type for. We will
2956 // use this to ensure any matches we find are in a language that this
2957 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2959 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2960 ? nullptr
2961 : GetTypeSystemForLanguage(language);
2962
2963 if (num_matches) {
2964 for (size_t i = 0; i < num_matches; ++i) {
2965 const DIERef &die_ref = die_offsets[i];
2966 DWARFDIE type_die = GetDIE(die_ref);
2967
2968 if (type_die) {
2969 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002970 // looking for. We don't want to find a "Foo" type from Java if we
2971 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002972 if (type_system &&
2973 !type_system->SupportsLanguage(type_die.GetLanguage()))
2974 continue;
2975 bool try_resolving_type = false;
2976
2977 // Don't try and resolve the DIE we are looking for with the DIE
2978 // itself!
2979 const dw_tag_t type_tag = type_die.Tag();
2980 // Make sure the tags match
2981 if (type_tag == tag) {
2982 // The tags match, lets try resolving this type
2983 try_resolving_type = true;
2984 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002985 // The tags don't match, but we need to watch our for a forward
2986 // declaration for a struct and ("struct foo") ends up being a
2987 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002988 switch (type_tag) {
2989 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002990 // We had a "class foo", see if we ended up with a "struct foo
2991 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 try_resolving_type = (tag == DW_TAG_structure_type);
2993 break;
2994 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002995 // We had a "struct foo", see if we ended up with a "class foo
2996 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997 try_resolving_type = (tag == DW_TAG_class_type);
2998 break;
2999 default:
Adrian Prantl05097242018-04-30 16:49:04 +00003000 // Tags don't match, don't event try to resolve using this type
3001 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002 break;
3003 }
3004 }
3005
3006 if (try_resolving_type) {
3007 DWARFDeclContext type_dwarf_decl_ctx;
3008 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3009
3010 if (log) {
3011 GetObjectFile()->GetModule()->LogMessage(
3012 log, "SymbolFileDWARF::"
3013 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3014 "qualified-name='%s') trying die=0x%8.8x (%s)",
3015 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3016 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3017 type_dwarf_decl_ctx.GetQualifiedName());
3018 }
3019
3020 // Make sure the decl contexts match all the way up
3021 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3022 Type *resolved_type = ResolveType(type_die, false);
3023 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3024 type_sp = resolved_type->shared_from_this();
3025 break;
3026 }
3027 }
3028 } else {
3029 if (log) {
3030 std::string qualified_name;
3031 type_die.GetQualifiedName(qualified_name);
3032 GetObjectFile()->GetModule()->LogMessage(
3033 log, "SymbolFileDWARF::"
3034 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3035 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3036 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3037 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3038 qualified_name.c_str());
3039 }
3040 }
3041 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00003042 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
3043 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044 }
3045 }
3046 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003048 }
3049 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003050}
3051
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3053 bool *type_is_new_ptr) {
3054 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003055
Kate Stoneb9c1b512016-09-06 20:57:50 +00003056 if (die) {
3057 TypeSystem *type_system =
3058 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003059
Kate Stoneb9c1b512016-09-06 20:57:50 +00003060 if (type_system) {
3061 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3062 if (dwarf_ast) {
3063 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3064 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3065 if (type_sp) {
3066 TypeList *type_list = GetTypeList();
3067 if (type_list)
3068 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003069
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070 if (die.Tag() == DW_TAG_subprogram) {
3071 DIERef die_ref = die.GetDIERef();
3072 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3073 .GetScopeQualifiedName()
3074 .AsCString(""));
3075 if (scope_qualified_name.size()) {
3076 NameToOffsetMap::iterator iter =
3077 m_function_scope_qualified_name_map.find(
3078 scope_qualified_name);
3079 if (iter != m_function_scope_qualified_name_map.end())
3080 (*iter).second->insert(die_ref);
3081 else {
3082 DIERefSetSP new_set(new std::set<DIERef>);
3083 new_set->insert(die_ref);
3084 m_function_scope_qualified_name_map.emplace(
3085 std::make_pair(scope_qualified_name, new_set));
3086 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003087 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003088 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003090 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003092 }
3093
3094 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095}
3096
Kate Stoneb9c1b512016-09-06 20:57:50 +00003097size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3098 const DWARFDIE &orig_die,
3099 bool parse_siblings, bool parse_children) {
3100 size_t types_added = 0;
3101 DWARFDIE die = orig_die;
3102 while (die) {
3103 bool type_is_new = false;
3104 if (ParseType(sc, die, &type_is_new).get()) {
3105 if (type_is_new)
3106 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003108
3109 if (parse_children && die.HasChildren()) {
3110 if (die.Tag() == DW_TAG_subprogram) {
3111 SymbolContext child_sc(sc);
3112 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3113 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3114 } else
3115 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3116 }
3117
3118 if (parse_siblings)
3119 die = die.GetSibling();
3120 else
3121 die.Clear();
3122 }
3123 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003124}
3125
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003126size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003127 ASSERT_MODULE_LOCK(this);
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003128 CompileUnit *comp_unit = func.GetCompileUnit();
3129 lldbassert(comp_unit);
3130
3131 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3132 if (!dwarf_cu)
3133 return 0;
3134
Kate Stoneb9c1b512016-09-06 20:57:50 +00003135 size_t functions_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00003136 const dw_offset_t function_die_offset = func.GetID();
3137 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3138 if (function_die) {
3139 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3140 LLDB_INVALID_ADDRESS, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003142
Kate Stoneb9c1b512016-09-06 20:57:50 +00003143 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003144}
3145
Zachary Turner863f8c12019-01-11 18:03:20 +00003146size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003147 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003148 size_t types_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003149 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003150 if (dwarf_cu) {
Zachary Turnerac0d41c2019-01-10 20:57:50 +00003151 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3152 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3153 SymbolContext sc;
3154 sc.comp_unit = &comp_unit;
3155 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003158
Kate Stoneb9c1b512016-09-06 20:57:50 +00003159 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003160}
3161
Kate Stoneb9c1b512016-09-06 20:57:50 +00003162size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003163 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164 if (sc.comp_unit != NULL) {
3165 DWARFDebugInfo *info = DebugInfo();
3166 if (info == NULL)
3167 return 0;
3168
3169 if (sc.function) {
3170 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3171
3172 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3173 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3174 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3175 const size_t num_variables = ParseVariables(
3176 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3177
3178 // Let all blocks know they have parse all their variables
3179 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3180 return num_variables;
3181 }
3182 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003183 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184
3185 if (dwarf_cu == NULL)
3186 return 0;
3187
3188 uint32_t vars_added = 0;
3189 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3190
3191 if (variables.get() == NULL) {
3192 variables.reset(new VariableList());
3193 sc.comp_unit->SetVariableList(variables);
3194
3195 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003196 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197 const size_t num_matches = die_offsets.size();
3198 if (num_matches) {
3199 for (size_t i = 0; i < num_matches; ++i) {
3200 const DIERef &die_ref = die_offsets[i];
3201 DWARFDIE die = GetDIE(die_ref);
3202 if (die) {
3203 VariableSP var_sp(
3204 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3205 if (var_sp) {
3206 variables->AddVariableIfUnique(var_sp);
3207 ++vars_added;
3208 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003209 } else
3210 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003211 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003213 }
3214 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003215 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003216 }
3217 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003218}
3219
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3221 const DWARFDIE &die,
3222 const lldb::addr_t func_low_pc) {
3223 if (die.GetDWARF() != this)
3224 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003225
Kate Stoneb9c1b512016-09-06 20:57:50 +00003226 VariableSP var_sp;
3227 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003228 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003229
Kate Stoneb9c1b512016-09-06 20:57:50 +00003230 var_sp = GetDIEToVariable()[die.GetDIE()];
3231 if (var_sp)
3232 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003233
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 const dw_tag_t tag = die.Tag();
3235 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003236
Kate Stoneb9c1b512016-09-06 20:57:50 +00003237 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3238 (tag == DW_TAG_formal_parameter && sc.function)) {
3239 DWARFAttributes attributes;
3240 const size_t num_attributes = die.GetAttributes(attributes);
3241 DWARFDIE spec_die;
3242 if (num_attributes > 0) {
3243 const char *name = NULL;
3244 const char *mangled = NULL;
3245 Declaration decl;
3246 uint32_t i;
3247 DWARFFormValue type_die_form;
3248 DWARFExpression location(die.GetCU());
3249 bool is_external = false;
3250 bool is_artificial = false;
3251 bool location_is_const_value_data = false;
3252 bool has_explicit_location = false;
3253 DWARFFormValue const_value;
3254 Variable::RangeList scope_ranges;
3255 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003256
Kate Stoneb9c1b512016-09-06 20:57:50 +00003257 for (i = 0; i < num_attributes; ++i) {
3258 dw_attr_t attr = attributes.AttributeAtIndex(i);
3259 DWARFFormValue form_value;
3260
3261 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3262 switch (attr) {
3263 case DW_AT_decl_file:
3264 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3265 form_value.Unsigned()));
3266 break;
3267 case DW_AT_decl_line:
3268 decl.SetLine(form_value.Unsigned());
3269 break;
3270 case DW_AT_decl_column:
3271 decl.SetColumn(form_value.Unsigned());
3272 break;
3273 case DW_AT_name:
3274 name = form_value.AsCString();
3275 break;
3276 case DW_AT_linkage_name:
3277 case DW_AT_MIPS_linkage_name:
3278 mangled = form_value.AsCString();
3279 break;
3280 case DW_AT_type:
3281 type_die_form = form_value;
3282 break;
3283 case DW_AT_external:
3284 is_external = form_value.Boolean();
3285 break;
3286 case DW_AT_const_value:
3287 // If we have already found a DW_AT_location attribute, ignore this
3288 // attribute.
3289 if (!has_explicit_location) {
3290 location_is_const_value_data = true;
3291 // The constant value will be either a block, a data value or a
3292 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003293 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003294 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3295 // Retrieve the value as a block expression.
3296 uint32_t block_offset =
3297 form_value.BlockData() - debug_info_data.GetDataStart();
3298 uint32_t block_length = form_value.Unsigned();
3299 location.CopyOpcodeData(module, debug_info_data, block_offset,
3300 block_length);
3301 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3302 // Retrieve the value as a data expression.
3303 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3304 DWARFFormValue::GetFixedFormSizesForAddressSize(
3305 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3306 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3307 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3308 uint32_t data_length =
3309 fixed_form_sizes.GetSize(form_value.Form());
3310 if (data_length == 0) {
3311 const uint8_t *data_pointer = form_value.BlockData();
3312 if (data_pointer) {
3313 form_value.Unsigned();
3314 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3315 // we need to get the byte size of the type later after we
3316 // create the variable
3317 const_value = form_value;
3318 }
3319 } else
3320 location.CopyOpcodeData(module, debug_info_data, data_offset,
3321 data_length);
3322 } else {
3323 // Retrieve the value as a string expression.
3324 if (form_value.Form() == DW_FORM_strp) {
3325 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3326 DWARFFormValue::GetFixedFormSizesForAddressSize(
3327 attributes.CompileUnitAtIndex(i)
3328 ->GetAddressByteSize(),
3329 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3330 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3331 uint32_t data_length =
3332 fixed_form_sizes.GetSize(form_value.Form());
3333 location.CopyOpcodeData(module, debug_info_data, data_offset,
3334 data_length);
3335 } else {
3336 const char *str = form_value.AsCString();
3337 uint32_t string_offset =
3338 str - (const char *)debug_info_data.GetDataStart();
3339 uint32_t string_length = strlen(str) + 1;
3340 location.CopyOpcodeData(module, debug_info_data,
3341 string_offset, string_length);
3342 }
3343 }
3344 }
3345 break;
3346 case DW_AT_location: {
3347 location_is_const_value_data = false;
3348 has_explicit_location = true;
3349 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003350 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003351
3352 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003353 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003354 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003355 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003356 } else {
George Rimare4dee262018-10-23 09:46:15 +00003357 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003358 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3359
3360 size_t loc_list_length = DWARFExpression::LocationListSize(
3361 die.GetCU(), debug_loc_data, debug_loc_offset);
3362 if (loc_list_length > 0) {
3363 location.CopyOpcodeData(module, debug_loc_data,
3364 debug_loc_offset, loc_list_length);
3365 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3366 location.SetLocationListSlide(
3367 func_low_pc -
3368 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3369 }
3370 }
3371 } break;
3372 case DW_AT_specification:
3373 spec_die = GetDIE(DIERef(form_value));
3374 break;
3375 case DW_AT_start_scope: {
3376 if (form_value.Form() == DW_FORM_sec_offset) {
3377 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003378 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3379 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003380 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003381 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003382 } else {
3383 // TODO: Handle the case when DW_AT_start_scope have form
3384 // constant. The
3385 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003386 // behavior in case the enclosing block have a non coninious
3387 // address range and the DW_AT_start_scope entry have a form
3388 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003389 GetObjectFile()->GetModule()->ReportWarning(
3390 "0x%8.8" PRIx64
3391 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3392 die.GetID(), form_value.Form());
3393 }
3394
3395 scope_ranges.Sort();
3396 scope_ranges.CombineConsecutiveRanges();
3397 } break;
3398 case DW_AT_artificial:
3399 is_artificial = form_value.Boolean();
3400 break;
3401 case DW_AT_accessibility:
3402 break; // accessibility =
3403 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3404 case DW_AT_declaration:
3405 case DW_AT_description:
3406 case DW_AT_endianity:
3407 case DW_AT_segment:
3408 case DW_AT_visibility:
3409 default:
3410 case DW_AT_abstract_origin:
3411 case DW_AT_sibling:
3412 break;
3413 }
3414 }
3415 }
3416
3417 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3418 const dw_tag_t parent_tag = die.GetParent().Tag();
3419 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003420 (parent_tag == DW_TAG_compile_unit ||
3421 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003422 (parent_context_die.Tag() == DW_TAG_class_type ||
3423 parent_context_die.Tag() == DW_TAG_structure_type);
3424
3425 ValueType scope = eValueTypeInvalid;
3426
3427 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3428 SymbolContextScope *symbol_context_scope = NULL;
3429
Sam McCall30621392016-11-22 11:40:25 +00003430 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003431 if (!mangled) {
3432 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003433 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3434 // of global variables with commands like "frame var j". For
3435 // example, if j were an int variable holding a value 4 and
3436 // declared in a namespace B which in turn is contained in a
3437 // namespace A, the command "frame var j" returns
3438 // "(int) A::B::j = 4".
3439 // If the compiler does not emit a linkage name, we should be
3440 // able to generate a fully qualified name from the
3441 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003442 if ((parent_tag == DW_TAG_compile_unit ||
3443 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003444 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3445 DWARFDeclContext decl_ctx;
3446
3447 die.GetDWARFDeclContext(decl_ctx);
3448 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3449 }
3450 }
3451
Kate Stoneb9c1b512016-09-06 20:57:50 +00003452 if (tag == DW_TAG_formal_parameter)
3453 scope = eValueTypeVariableArgument;
3454 else {
Sam McCall30621392016-11-22 11:40:25 +00003455 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3456 // or static variable, so we have to do a little digging:
3457 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3458 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3459 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3460 // Clang likes to combine small global variables into the same symbol
3461 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3462 // so we need to look through the whole expression.
3463 bool is_static_lifetime =
3464 has_explicit_mangled ||
3465 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003466 // Check if the location has a DW_OP_addr with any address value...
3467 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3468 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003469 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003470 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3471 if (op_error) {
3472 StreamString strm;
3473 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3474 NULL);
3475 GetObjectFile()->GetModule()->ReportError(
3476 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003477 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003478 }
Sam McCall30621392016-11-22 11:40:25 +00003479 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3480 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003481 }
3482 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003483 if (debug_map_symfile)
3484 // Set the module of the expression to the linked module
3485 // instead of the oject file so the relocated address can be
3486 // found there.
3487 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003488
Sam McCall30621392016-11-22 11:40:25 +00003489 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003490 if (is_external)
3491 scope = eValueTypeVariableGlobal;
3492 else
3493 scope = eValueTypeVariableStatic;
3494
3495 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003496 // When leaving the DWARF in the .o files on darwin, when we have a
3497 // global variable that wasn't initialized, the .o file might not
3498 // have allocated a virtual address for the global variable. In
3499 // this case it will have created a symbol for the global variable
3500 // that is undefined/data and external and the value will be the
3501 // byte size of the variable. When we do the address map in
3502 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3503 // do some magic here so we can get the correct address for our
3504 // global variable. The address for all of these entries will be
3505 // zero, and there will be an undefined symbol in this object file,
3506 // and the executable will have a matching symbol with a good
3507 // address. So here we dig up the correct address and replace it in
3508 // the location for the variable, and set the variable's symbol
3509 // context scope to be that of the main executable so the file
3510 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003511 bool linked_oso_file_addr = false;
3512 if (is_external && location_DW_OP_addr == 0) {
3513 // we have a possible uninitialized extern global
3514 ConstString const_name(mangled ? mangled : name);
3515 ObjectFile *debug_map_objfile =
3516 debug_map_symfile->GetObjectFile();
3517 if (debug_map_objfile) {
3518 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3519 if (debug_map_symtab) {
3520 Symbol *exe_symbol =
3521 debug_map_symtab->FindFirstSymbolWithNameAndType(
3522 const_name, eSymbolTypeData, Symtab::eDebugYes,
3523 Symtab::eVisibilityExtern);
3524 if (exe_symbol) {
3525 if (exe_symbol->ValueIsAddress()) {
3526 const addr_t exe_file_addr =
3527 exe_symbol->GetAddressRef().GetFileAddress();
3528 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3529 if (location.Update_DW_OP_addr(exe_file_addr)) {
3530 linked_oso_file_addr = true;
3531 symbol_context_scope = exe_symbol;
3532 }
3533 }
3534 }
3535 }
3536 }
3537 }
3538 }
3539
3540 if (!linked_oso_file_addr) {
3541 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003542 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003543 const lldb::addr_t exe_file_addr =
3544 debug_map_symfile->LinkOSOFileAddress(this,
3545 location_DW_OP_addr);
3546 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3547 // Update the file address for this variable
3548 location.Update_DW_OP_addr(exe_file_addr);
3549 } else {
3550 // Variable didn't make it into the final executable
3551 return var_sp;
3552 }
3553 }
3554 }
3555 } else {
3556 if (location_is_const_value_data)
3557 scope = eValueTypeVariableStatic;
3558 else {
3559 scope = eValueTypeVariableLocal;
3560 if (debug_map_symfile) {
3561 // We need to check for TLS addresses that we need to fixup
3562 if (location.ContainsThreadLocalStorage()) {
3563 location.LinkThreadLocalStorage(
3564 debug_map_symfile->GetObjectFile()->GetModule(),
3565 [this, debug_map_symfile](
3566 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3567 return debug_map_symfile->LinkOSOFileAddress(
3568 this, unlinked_file_addr);
3569 });
3570 scope = eValueTypeVariableThreadLocal;
3571 }
3572 }
3573 }
3574 }
3575 }
3576
3577 if (symbol_context_scope == NULL) {
3578 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003579 case DW_TAG_subprogram:
3580 case DW_TAG_inlined_subroutine:
3581 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003582 if (sc.function) {
3583 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3584 sc_parent_die.GetID());
3585 if (symbol_context_scope == NULL)
3586 symbol_context_scope = sc.function;
3587 }
3588 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003589
Kate Stoneb9c1b512016-09-06 20:57:50 +00003590 default:
3591 symbol_context_scope = sc.comp_unit;
3592 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003594 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003595
Kate Stoneb9c1b512016-09-06 20:57:50 +00003596 if (symbol_context_scope) {
3597 SymbolFileTypeSP type_sp(
3598 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3599
3600 if (const_value.Form() && type_sp && type_sp->GetType())
3601 location.CopyOpcodeData(const_value.Unsigned(),
3602 type_sp->GetType()->GetByteSize(),
3603 die.GetCU()->GetAddressByteSize());
3604
3605 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3606 symbol_context_scope, scope_ranges, &decl,
3607 location, is_external, is_artificial,
3608 is_static_member));
3609
3610 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3611 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003612 // Not ready to parse this variable yet. It might be a global or static
3613 // variable that is in a function scope and the function in the symbol
3614 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003615 return var_sp;
3616 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003617 }
Adrian Prantl05097242018-04-30 16:49:04 +00003618 // Cache var_sp even if NULL (the variable was just a specification or was
3619 // missing vital information to be able to be displayed in the debugger
3620 // (missing location due to optimization, etc)) so we don't re-parse this
3621 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003622 GetDIEToVariable()[die.GetDIE()] = var_sp;
3623 if (spec_die)
3624 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3625 }
3626 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003627}
3628
Kate Stoneb9c1b512016-09-06 20:57:50 +00003629DWARFDIE
3630SymbolFileDWARF::FindBlockContainingSpecification(
3631 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3632 // Give the concrete function die specified by "func_die_offset", find the
3633 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3634 // to "spec_block_die_offset"
3635 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3636 spec_block_die_offset);
3637}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003638
Kate Stoneb9c1b512016-09-06 20:57:50 +00003639DWARFDIE
3640SymbolFileDWARF::FindBlockContainingSpecification(
3641 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3642 if (die) {
3643 switch (die.Tag()) {
3644 case DW_TAG_subprogram:
3645 case DW_TAG_inlined_subroutine:
3646 case DW_TAG_lexical_block: {
3647 if (die.GetAttributeValueAsReference(
3648 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3649 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003650
Kate Stoneb9c1b512016-09-06 20:57:50 +00003651 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3652 DW_INVALID_OFFSET) ==
3653 spec_block_die_offset)
3654 return die;
3655 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003656 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003657
3658 // Give the concrete function die specified by "func_die_offset", find the
3659 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3660 // to "spec_block_die_offset"
3661 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3662 child_die = child_die.GetSibling()) {
3663 DWARFDIE result_die =
3664 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3665 if (result_die)
3666 return result_die;
3667 }
3668 }
3669
3670 return DWARFDIE();
3671}
3672
3673size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3674 const DWARFDIE &orig_die,
3675 const lldb::addr_t func_low_pc,
3676 bool parse_siblings, bool parse_children,
3677 VariableList *cc_variable_list) {
3678 if (!orig_die)
3679 return 0;
3680
3681 VariableListSP variable_list_sp;
3682
3683 size_t vars_added = 0;
3684 DWARFDIE die = orig_die;
3685 while (die) {
3686 dw_tag_t tag = die.Tag();
3687
3688 // Check to see if we have already parsed this variable or constant?
3689 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3690 if (var_sp) {
3691 if (cc_variable_list)
3692 cc_variable_list->AddVariableIfUnique(var_sp);
3693 } else {
3694 // We haven't already parsed it, lets do that now.
3695 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3696 (tag == DW_TAG_formal_parameter && sc.function)) {
3697 if (variable_list_sp.get() == NULL) {
3698 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3699 dw_tag_t parent_tag = sc_parent_die.Tag();
3700 switch (parent_tag) {
3701 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003702 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003703 if (sc.comp_unit != NULL) {
3704 variable_list_sp = sc.comp_unit->GetVariableList(false);
3705 if (variable_list_sp.get() == NULL) {
3706 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003707 }
3708 } else {
3709 GetObjectFile()->GetModule()->ReportError(
3710 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3711 "symbol context for 0x%8.8" PRIx64
3712 " %s.\n",
3713 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3714 orig_die.GetID(), orig_die.GetTagAsCString());
3715 }
3716 break;
3717
3718 case DW_TAG_subprogram:
3719 case DW_TAG_inlined_subroutine:
3720 case DW_TAG_lexical_block:
3721 if (sc.function != NULL) {
3722 // Check to see if we already have parsed the variables for the
3723 // given scope
3724
3725 Block *block = sc.function->GetBlock(true).FindBlockByID(
3726 sc_parent_die.GetID());
3727 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003728 // This must be a specification or abstract origin with a
3729 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003730 // to find the concrete block so we can correctly add the
3731 // variable to it
3732 const DWARFDIE concrete_block_die =
3733 FindBlockContainingSpecification(
3734 DIERef(sc.function->GetID(), this),
3735 sc_parent_die.GetOffset());
3736 if (concrete_block_die)
3737 block = sc.function->GetBlock(true).FindBlockByID(
3738 concrete_block_die.GetID());
3739 }
3740
3741 if (block != NULL) {
3742 const bool can_create = false;
3743 variable_list_sp = block->GetBlockVariableList(can_create);
3744 if (variable_list_sp.get() == NULL) {
3745 variable_list_sp.reset(new VariableList());
3746 block->SetVariableList(variable_list_sp);
3747 }
3748 }
3749 }
3750 break;
3751
3752 default:
3753 GetObjectFile()->GetModule()->ReportError(
3754 "didn't find appropriate parent DIE for variable list for "
3755 "0x%8.8" PRIx64 " %s.\n",
3756 orig_die.GetID(), orig_die.GetTagAsCString());
3757 break;
3758 }
3759 }
3760
3761 if (variable_list_sp) {
3762 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3763 if (var_sp) {
3764 variable_list_sp->AddVariableIfUnique(var_sp);
3765 if (cc_variable_list)
3766 cc_variable_list->AddVariableIfUnique(var_sp);
3767 ++vars_added;
3768 }
3769 }
3770 }
3771 }
3772
3773 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3774
3775 if (!skip_children && parse_children && die.HasChildren()) {
3776 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3777 true, cc_variable_list);
3778 }
3779
3780 if (parse_siblings)
3781 die = die.GetSibling();
3782 else
3783 die.Clear();
3784 }
3785 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786}
3787
Vedant Kumar4b36f792018-10-05 23:23:15 +00003788/// Collect call graph edges present in a function DIE.
3789static std::vector<lldb_private::CallEdge>
3790CollectCallEdges(DWARFDIE function_die) {
3791 // Check if the function has a supported call site-related attribute.
3792 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3793 uint64_t has_call_edges =
3794 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3795 if (!has_call_edges)
3796 return {};
3797
3798 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3799 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3800 function_die.GetPubname());
3801
3802 // Scan the DIE for TAG_call_site entries.
3803 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3804 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3805 // For now, assume that all entries are nested directly under the subprogram
3806 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3807 std::vector<CallEdge> call_edges;
3808 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3809 child = child.GetSibling()) {
3810 if (child.Tag() != DW_TAG_call_site)
3811 continue;
3812
3813 // Extract DW_AT_call_origin (the call target's DIE).
3814 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3815 if (!call_origin.IsValid()) {
3816 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3817 function_die.GetPubname());
3818 continue;
3819 }
3820
3821 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3822 // available. It should only ever be unavailable for tail call edges, in
3823 // which case use LLDB_INVALID_ADDRESS.
3824 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3825 LLDB_INVALID_ADDRESS);
3826
3827 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3828 call_origin.GetPubname(), return_pc);
3829 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3830 }
3831 return call_edges;
3832}
3833
3834std::vector<lldb_private::CallEdge>
3835SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3836 DWARFDIE func_die = GetDIEFromUID(func_id.GetID());
3837 if (func_die.IsValid())
3838 return CollectCallEdges(func_die);
3839 return {};
3840}
3841
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003842//------------------------------------------------------------------
3843// PluginInterface protocol
3844//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003845ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3846
3847uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3848
Pavel Labath9337b412018-06-06 11:35:23 +00003849void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850
Zachary Turner49110232018-11-05 17:40:28 +00003851void SymbolFileDWARF::DumpClangAST(Stream &s) {
3852 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3853 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts);
3854 if (!clang)
3855 return;
3856 clang->Dump(s);
3857}
3858
Kate Stoneb9c1b512016-09-06 20:57:50 +00003859SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3860 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3861 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3862 if (module_sp) {
3863 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3864 if (sym_vendor)
3865 m_debug_map_symfile =
3866 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003867 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003868 }
3869 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003870}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003871
3872DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003873SymbolFileDWARF::GetLocationListFormat() const {
George Rimare4dee262018-10-23 09:46:15 +00003874 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
3875 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003876 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003877}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003878
3879SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3880 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003881 ModuleSpec module_spec;
3882 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3883 module_spec.GetSymbolFileSpec() =
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00003884 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp");
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003885 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003886 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003887 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3888 dwp_filespec);
3889 }
3890 });
3891 return m_dwp_symfile.get();
3892}