blob: 563305028e23491f9dbb284f62306ec091b8f713 [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
12// Other libraries and framework includes
Sean Callanancc427fa2011-07-30 02:42:06 +000013#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000017#include "lldb/Core/ModuleList.h"
18#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000021#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000023#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000025#include "lldb/Utility/Scalar.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000027#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Sean Callanan4dbb2712015-09-25 20:35:58 +000029#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Pavel Labathe1d18752018-06-07 10:04:44 +000030#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000031
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000032#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000033#include "lldb/Host/Host.h"
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +000034#include "lldb/Host/Symbols.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000035
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000036#include "lldb/Interpreter/OptionValueFileSpecList.h"
37#include "lldb/Interpreter/OptionValueProperties.h"
38
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000040#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000041#include "lldb/Symbol/ClangUtil.h"
42#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000043#include "lldb/Symbol/CompilerDecl.h"
44#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000045#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000046#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Symbol/ObjectFile.h"
48#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000049#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000050#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Symbol/VariableList.h"
52
Jim Ingham0e0984e2015-09-02 01:06:46 +000053#include "lldb/Target/Language.h"
54
Pavel Labathb13f0332018-05-21 14:12:52 +000055#include "AppleDWARFIndex.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000056#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000057#include "DWARFASTParserClang.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000058#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "DWARFDebugAbbrev.h"
60#include "DWARFDebugAranges.h"
61#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000063#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000065#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "DWARFFormValue.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000067#include "DWARFUnit.h"
Pavel Labath9337b412018-06-06 11:35:23 +000068#include "DebugNamesDWARFIndex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "LogChannelDWARF.h"
Pavel Labathb13f0332018-05-21 14:12:52 +000070#include "ManualDWARFIndex.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000071#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000072#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000073#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Zachary Turner7d86ee52017-03-08 17:56:08 +000075#include "llvm/Support/FileSystem.h"
76
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077#include <map>
78
Matthew Gardinere81df3b2014-08-26 06:57:23 +000079#include <ctype.h>
80#include <string.h>
81
Greg Clayton62742b12010-11-11 01:09:45 +000082//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000083
84#ifdef ENABLE_DEBUG_PRINTF
85#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000086#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000087#else
88#define DEBUG_PRINTF(fmt, ...)
89#endif
90
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091using namespace lldb;
92using namespace lldb_private;
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094// static inline bool
95// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000096//{
97// switch (tag)
98// {
99// default:
100// break;
101// case DW_TAG_subprogram:
102// case DW_TAG_inlined_subroutine:
103// case DW_TAG_class_type:
104// case DW_TAG_structure_type:
105// case DW_TAG_union_type:
106// return true;
107// }
108// return false;
109//}
110//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000111
112namespace {
113
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000114static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000116 {},
Pavel Labath9337b412018-06-06 11:35:23 +0000117 "If the DW_AT_comp_dir matches any of these paths the symbolic "
118 "links will be resolved at DWARF parse time."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000119 {"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
Pavel Labath9337b412018-06-06 11:35:23 +0000120 "Ignore indexes present in the object files and always index DWARF "
Tatyana Krasnukhae40db052018-09-27 07:11:58 +0000121 "manually."}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000122
Pavel Labath9337b412018-06-06 11:35:23 +0000123enum {
124 ePropertySymLinkPaths,
125 ePropertyIgnoreIndexes,
126};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128class PluginProperties : public Properties {
129public:
130 static ConstString GetSettingName() {
131 return SymbolFileDWARF::GetPluginNameStatic();
132 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 PluginProperties() {
135 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
136 m_collection_sp->Initialize(g_properties);
137 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 FileSpecList &GetSymLinkPaths() {
140 OptionValueFileSpecList *option_value =
141 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
142 nullptr, true, ePropertySymLinkPaths);
143 assert(option_value);
144 return option_value->GetCurrentValue();
145 }
Pavel Labath9337b412018-06-06 11:35:23 +0000146
147 bool IgnoreFileIndexes() const {
148 return m_collection_sp->GetPropertyAtIndexAsBoolean(
149 nullptr, ePropertyIgnoreIndexes, false);
150 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
156 static const auto g_settings_sp(std::make_shared<PluginProperties>());
157 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
163 if (!path_from_dwarf || !path_from_dwarf[0]) {
164 return path_from_dwarf;
165 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 const char *colon_pos = strchr(path_from_dwarf, ':');
168 if (nullptr == colon_pos) {
169 return path_from_dwarf;
170 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 const char *slash_pos = strchr(path_from_dwarf, '/');
173 if (slash_pos && (slash_pos < colon_pos)) {
174 return path_from_dwarf;
175 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000176
Adrian Prantl05097242018-04-30 16:49:04 +0000177 // check whether we have a windows path, and so the first character is a
178 // drive-letter not a hostname.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
180 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
181 return path_from_dwarf;
182 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 return colon_pos + 1;
185}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000186
Greg Clayton776cd7a2018-04-27 15:45:58 +0000187static FileSpec resolveCompDir(const char *path_from_dwarf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 if (!path_from_dwarf)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000189 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190
191 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
192 // Remove the host part if present.
193 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
194 if (!local_path)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000195 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196
197 bool is_symlink = false;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000198 // Always normalize our compile unit directory to get rid of redundant
199 // slashes and other path anomalies before we use it for path prepending
200 FileSpec local_spec(local_path, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
202 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
203 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
Greg Clayton776cd7a2018-04-27 15:45:58 +0000204 local_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205
206 if (!is_symlink)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000207 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208
Zachary Turner7d86ee52017-03-08 17:56:08 +0000209 namespace fs = llvm::sys::fs;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000210 if (fs::get_file_type(local_spec.GetPath(), false) !=
Zachary Turner7d86ee52017-03-08 17:56:08 +0000211 fs::file_type::symlink_file)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000212 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213
Greg Clayton776cd7a2018-04-27 15:45:58 +0000214 FileSpec resolved_symlink;
215 const auto error = FileSystem::Readlink(local_spec, resolved_symlink);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 if (error.Success())
Greg Clayton776cd7a2018-04-27 15:45:58 +0000217 return resolved_symlink;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218
Greg Clayton776cd7a2018-04-27 15:45:58 +0000219 return local_spec;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000220}
221
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000222DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000223 return nullptr;
224}
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226void SymbolFileDWARF::Initialize() {
227 LogChannelDWARF::Initialize();
228 PluginManager::RegisterPlugin(GetPluginNameStatic(),
229 GetPluginDescriptionStatic(), CreateInstance,
230 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
234 if (!PluginManager::GetSettingForSymbolFilePlugin(
235 debugger, PluginProperties::GetSettingName())) {
236 const bool is_global_setting = true;
237 PluginManager::CreateSettingForSymbolFilePlugin(
238 debugger, GetGlobalPluginProperties()->GetValueProperties(),
239 ConstString("Properties for the dwarf symbol-file plug-in."),
240 is_global_setting);
241 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244void SymbolFileDWARF::Terminate() {
245 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000246 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
250 static ConstString g_name("dwarf");
251 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
255 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
259 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262TypeList *SymbolFileDWARF::GetTypeList() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000263 // This method can be called without going through the symbol vendor so we
264 // need to lock the module.
265 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
267 if (debug_map_symfile)
268 return debug_map_symfile->GetTypeList();
269 else
270 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000271}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
273 dw_offset_t max_die_offset, uint32_t type_mask,
274 TypeSet &type_set) {
275 if (die) {
276 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (die_offset >= max_die_offset)
279 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 if (die_offset >= min_die_offset) {
282 const dw_tag_t tag = die.Tag();
283
284 bool add_type = false;
285
286 switch (tag) {
287 case DW_TAG_array_type:
288 add_type = (type_mask & eTypeClassArray) != 0;
289 break;
290 case DW_TAG_unspecified_type:
291 case DW_TAG_base_type:
292 add_type = (type_mask & eTypeClassBuiltin) != 0;
293 break;
294 case DW_TAG_class_type:
295 add_type = (type_mask & eTypeClassClass) != 0;
296 break;
297 case DW_TAG_structure_type:
298 add_type = (type_mask & eTypeClassStruct) != 0;
299 break;
300 case DW_TAG_union_type:
301 add_type = (type_mask & eTypeClassUnion) != 0;
302 break;
303 case DW_TAG_enumeration_type:
304 add_type = (type_mask & eTypeClassEnumeration) != 0;
305 break;
306 case DW_TAG_subroutine_type:
307 case DW_TAG_subprogram:
308 case DW_TAG_inlined_subroutine:
309 add_type = (type_mask & eTypeClassFunction) != 0;
310 break;
311 case DW_TAG_pointer_type:
312 add_type = (type_mask & eTypeClassPointer) != 0;
313 break;
314 case DW_TAG_rvalue_reference_type:
315 case DW_TAG_reference_type:
316 add_type = (type_mask & eTypeClassReference) != 0;
317 break;
318 case DW_TAG_typedef:
319 add_type = (type_mask & eTypeClassTypedef) != 0;
320 break;
321 case DW_TAG_ptr_to_member_type:
322 add_type = (type_mask & eTypeClassMemberPointer) != 0;
323 break;
324 }
325
326 if (add_type) {
327 const bool assert_not_being_parsed = true;
328 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
329 if (type) {
330 if (type_set.find(type) == type_set.end())
331 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000334 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335
336 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
337 child_die = child_die.GetSibling()) {
338 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
339 }
340 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000341}
342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
344 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000345
346{
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000347 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000351 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 if (sc_scope)
353 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 if (comp_unit) {
356 dwarf_cu = GetDWARFCompileUnit(comp_unit);
357 if (dwarf_cu == 0)
358 return 0;
359 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
360 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
361 } else {
362 DWARFDebugInfo *info = DebugInfo();
363 if (info) {
364 const size_t num_cus = info->GetNumCompileUnits();
365 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
366 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
367 if (dwarf_cu) {
368 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000369 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000371 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 }
373
374 std::set<CompilerType> compiler_type_set;
375 size_t num_types_added = 0;
376 for (Type *type : type_set) {
377 CompilerType compiler_type = type->GetForwardCompilerType();
378 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
379 compiler_type_set.insert(compiler_type);
380 type_list.Insert(type->shared_from_this());
381 ++num_types_added;
382 }
383 }
384 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000385}
386
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387//----------------------------------------------------------------------
388// Gets the first parent that is a lexical block, function or inlined
389// subroutine, or compile unit.
390//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000391DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
393 DWARFDIE die;
394 for (die = child_die.GetParent(); die; die = die.GetParent()) {
395 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 switch (tag) {
398 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000399 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 case DW_TAG_subprogram:
401 case DW_TAG_inlined_subroutine:
402 case DW_TAG_lexical_block:
403 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 }
406 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407}
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000410 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
411 << 32), // Used by SymbolFileDWARFDebugMap to
412 // when this class parses .o files to
413 // contain the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
415 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
416 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
George Rimar6e357122018-10-10 08:11:15 +0000417 m_data_debug_ranges(), m_data_debug_rnglists(), m_data_debug_str(),
418 m_data_apple_names(), m_data_apple_types(), m_data_apple_namespaces(),
419 m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
421 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423SymbolFileDWARF::~SymbolFileDWARF() {}
424
425static const ConstString &GetDWARFMachOSegmentName() {
426 static ConstString g_dwarf_section_name("__DWARF");
427 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428}
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
431 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
432 if (debug_map_symfile)
433 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
434 else
435 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000436}
437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
439 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
440 TypeSystem *type_system;
441 if (debug_map_symfile) {
442 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
443 } else {
444 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
445 if (type_system)
446 type_system->SetSymbolFile(this);
447 }
448 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000449}
450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000452 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 ModuleSP module_sp(m_obj_file->GetModule());
454 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000455 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000456 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000460 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 }
462
Pavel Labath9337b412018-06-06 11:35:23 +0000463 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
464 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
465 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
466 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
467 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
468 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469
Pavel Labath9337b412018-06-06 11:35:23 +0000470 m_index = AppleDWARFIndex::Create(
471 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
472 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473
Pavel Labath9337b412018-06-06 11:35:23 +0000474 if (m_index)
475 return;
476
477 DWARFDataExtractor debug_names;
478 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
479 if (debug_names.GetByteSize() > 0) {
480 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
481 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
482 debug_names, get_debug_str_data(),
483 DebugInfo());
484 if (index_or) {
485 m_index = std::move(*index_or);
486 return;
487 }
488 LLDB_LOG_ERROR(log, index_or.takeError(),
489 "Unable to read .debug_names data: {0}");
490 }
491 }
492
493 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
494 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495}
496
497bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000498 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499}
500
501uint32_t SymbolFileDWARF::CalculateAbilities() {
502 uint32_t abilities = 0;
503 if (m_obj_file != NULL) {
504 const Section *section = NULL;
505 const SectionList *section_list = m_obj_file->GetSectionList();
506 if (section_list == NULL)
507 return 0;
508
Adrian Prantl05097242018-04-30 16:49:04 +0000509 // On non Apple platforms we might have .debug_types debug info that is
510 // created by using "-fdebug-types-section". LLDB currently will try to
511 // load this debug info, but it causes crashes during debugging when types
512 // are missing since it doesn't know how to parse the info in the
513 // .debug_types type units. This causes all complex debug info types to be
514 // unresolved. Because this causes LLDB to crash and since it really
515 // doesn't provide a solid debuggiung experience, we should disable trying
516 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000517 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
518 m_obj_file->GetModule()->ReportWarning(
519 "lldb doesn’t support .debug_types debug info");
520 return 0;
521 }
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 uint64_t debug_abbrev_file_size = 0;
524 uint64_t debug_info_file_size = 0;
525 uint64_t debug_line_file_size = 0;
526
527 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
528
529 if (section)
530 section_list = &section->GetChildren();
531
532 section =
533 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
534 if (section != NULL) {
535 debug_info_file_size = section->GetFileSize();
536
537 section =
538 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
539 .get();
540 if (section)
541 debug_abbrev_file_size = section->GetFileSize();
542
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000543 DWARFDebugAbbrev *abbrev = DebugAbbrev();
544 if (abbrev) {
545 std::set<dw_form_t> invalid_forms;
546 abbrev->GetUnsupportedForms(invalid_forms);
547 if (!invalid_forms.empty()) {
548 StreamString error;
549 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
550 for (auto form : invalid_forms)
551 error.Printf(" %#x", form);
552 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
553 return 0;
554 }
555 }
556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 section =
558 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
559 .get();
560 if (section)
561 debug_line_file_size = section->GetFileSize();
562 } else {
563 const char *symfile_dir_cstr =
564 m_obj_file->GetFileSpec().GetDirectory().GetCString();
565 if (symfile_dir_cstr) {
566 if (strcasestr(symfile_dir_cstr, ".dsym")) {
567 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000568 // We have a dSYM file that didn't have a any debug info. If the
569 // string table has a size of 1, then it was made from an
570 // executable with no debug info, or from an executable that was
571 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 section =
573 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
574 .get();
575 if (section && section->GetFileSize() == 1) {
576 m_obj_file->GetModule()->ReportWarning(
577 "empty dSYM file detected, dSYM was created with an "
578 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584
585 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
586 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
587 LocalVariables | VariableTypes;
588
589 if (debug_line_file_size > 0)
590 abilities |= LineTables;
591 }
592 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593}
594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595const DWARFDataExtractor &
596SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
597 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000598 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
599 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
600 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
605 DWARFDataExtractor &data) {
606 ModuleSP module_sp(m_obj_file->GetModule());
607 const SectionList *section_list = module_sp->GetSectionList();
608 if (section_list) {
609 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
610 if (section_sp) {
611 // See if we memory mapped the DWARF segment?
612 if (m_dwarf_data.GetByteSize()) {
613 data.SetData(m_dwarf_data, section_sp->GetOffset(),
614 section_sp->GetFileSize());
615 } else {
616 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
617 data.Clear();
618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
624 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
625 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626}
627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
629 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
630}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
633 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
634 m_data_debug_aranges);
635}
636
637const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
638 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
639}
640
641const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
642 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
643}
644
645const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
646 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
647}
648
George Rimarc6c7bfc2018-09-13 17:06:47 +0000649const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
650 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
651}
652
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
654 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
655}
656
657const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
658 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
659}
660
661const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
662 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
663 m_data_debug_ranges);
664}
665
George Rimar6e357122018-10-10 08:11:15 +0000666const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
667 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
668 m_data_debug_rnglists);
669}
670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
672 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
673}
674
675const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
676 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
677 m_data_debug_str_offsets);
678}
679
Greg Clayton2550ca12018-05-08 17:19:24 +0000680const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
681 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
682}
683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
685 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
686}
687
688const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
689 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
690}
691
692const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
693 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
694 m_data_apple_namespaces);
695}
696
697const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
698 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
699}
700
Jan Kratochvile4777a92018-04-29 19:47:48 +0000701const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
702 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
703 m_data_gnu_debugaltlink);
704}
705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
707 if (m_abbr.get() == NULL) {
708 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
709 if (debug_abbrev_data.GetByteSize() > 0) {
710 m_abbr.reset(new DWARFDebugAbbrev());
711 if (m_abbr.get())
712 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 }
715 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
719 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720}
721
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
723 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000724 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
725 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
726 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 if (get_debug_info_data().GetByteSize() > 0) {
728 m_info.reset(new DWARFDebugInfo());
729 if (m_info.get()) {
730 m_info->SetDwarfData(this);
731 }
Greg Clayton1f746072012-08-29 21:13:06 +0000732 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
734 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735}
736
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
738 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000741DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
743 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000744 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745
746 DWARFDebugInfo *info = DebugInfo();
747 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000748 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
749 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000751 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
753 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
754 dwarf_cu->SetUserData(comp_unit);
755 return dwarf_cu;
756 }
757 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
761 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000762 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
763 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
764 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000765
766 if (get_debug_ranges_data().GetByteSize() > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000768 else if (get_debug_rnglists_data().GetByteSize() > 0)
769 m_ranges.reset(new DWARFDebugRngLists());
770
771 if (m_ranges.get())
772 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 }
774 return m_ranges.get();
775}
776
777const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
778 return m_ranges.get();
779}
780
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000781lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 uint32_t cu_idx) {
783 CompUnitSP cu_sp;
784 if (dwarf_cu) {
785 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
786 if (comp_unit) {
787 // We already parsed this compile unit, had out a shared pointer to it
788 cu_sp = comp_unit->shared_from_this();
789 } else {
790 if (dwarf_cu->GetSymbolFileDWARF() != this) {
791 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
792 cu_idx);
793 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
794 // Let the debug map create the compile unit
795 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
796 dwarf_cu->SetUserData(cu_sp.get());
797 } else {
798 ModuleSP module_sp(m_obj_file->GetModule());
799 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000800 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 if (cu_die) {
802 FileSpec cu_file_spec{cu_die.GetName(), false};
803 if (cu_file_spec) {
804 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000805 // resolve the file. This can be expensive e.g. when the source
806 // files are
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 // NFS mounted.
808 if (cu_file_spec.IsRelative()) {
809 const char *cu_comp_dir{
810 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
811 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
812 }
813
814 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000815 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 remapped_file))
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000817 cu_file_spec.SetFile(remapped_file, false,
818 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 }
820
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000821 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
823
824 bool is_optimized = dwarf_cu->GetIsOptimized();
825 cu_sp.reset(new CompileUnit(
826 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
827 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
828 if (cu_sp) {
829 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000830 // try and get the first entry in the supports files from the
831 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 if (!cu_file_spec) {
833 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
834 if (cu_file_spec) {
835 (FileSpec &)(*cu_sp) = cu_file_spec;
836 // Also fix the invalid file spec which was copied from the
837 // compile unit.
838 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
839 }
840 }
841
842 dwarf_cu->SetUserData(cu_sp.get());
843
844 // Figure out the compile unit index if we weren't given one
845 if (cu_idx == UINT32_MAX)
846 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
847
848 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
849 cu_idx, cu_sp);
850 }
851 }
852 }
853 }
854 }
855 }
856 return cu_sp;
857}
858
859uint32_t SymbolFileDWARF::GetNumCompileUnits() {
860 DWARFDebugInfo *info = DebugInfo();
861 if (info)
862 return info->GetNumCompileUnits();
863 return 0;
864}
865
866CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000867 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 CompUnitSP cu_sp;
869 DWARFDebugInfo *info = DebugInfo();
870 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000871 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 if (dwarf_cu)
873 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
874 }
875 return cu_sp;
876}
877
878Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
879 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000880 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 if (die.IsValid()) {
882 TypeSystem *type_system =
883 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
884
885 if (type_system) {
886 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
887 if (dwarf_ast)
888 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
889 }
890 }
891 return nullptr;
892}
893
894bool SymbolFileDWARF::FixupAddress(Address &addr) {
895 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
896 if (debug_map_symfile) {
897 return debug_map_symfile->LinkOSOAddress(addr);
898 }
899 // This is a normal DWARF file, no address fixups need to happen
900 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000901}
Greg Clayton1f746072012-08-29 21:13:06 +0000902lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000904 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000906 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 if (dwarf_cu)
908 return dwarf_cu->GetLanguageType();
909 else
910 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000911}
912
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000914 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 assert(sc.comp_unit);
916 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000917 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 if (dwarf_cu) {
919 DWARFDIECollection function_dies;
920 const size_t num_functions =
921 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
922 size_t func_idx;
923 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
924 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
925 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
926 if (ParseCompileUnitFunction(sc, die))
927 ++functions_added;
928 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 // FixupTypes();
931 }
932 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933}
934
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
936 const SymbolContext &sc, FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000937 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000939 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000941 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 if (cu_die) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000944 FileSpec cu_comp_dir = resolveCompDir(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
946 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
947 DW_AT_stmt_list, DW_INVALID_OFFSET);
948 if (stmt_list != DW_INVALID_OFFSET) {
949 // All file indexes in DWARF are one based and a file of index zero is
950 // supposed to be the compile unit itself.
951 support_files.Append(*sc.comp_unit);
952 return DWARFDebugLine::ParseSupportFiles(
953 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
George Rimarc6c7bfc2018-09-13 17:06:47 +0000954 stmt_list, support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 }
958 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959}
960
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
962 const lldb_private::SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000963 ASSERT_MODULE_LOCK(this);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000964 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 if (dwarf_cu)
966 return dwarf_cu->GetIsOptimized();
967 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000968}
969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970bool SymbolFileDWARF::ParseImportedModules(
971 const lldb_private::SymbolContext &sc,
972 std::vector<lldb_private::ConstString> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000973 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000975 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 if (dwarf_cu) {
977 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
978 sc.comp_unit->GetLanguage())) {
979 UpdateExternalModuleListIfNeeded();
980
981 if (sc.comp_unit) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000982 const DWARFDIE die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983
984 if (die) {
985 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
986 child_die = child_die.GetSibling()) {
987 if (child_die.Tag() == DW_TAG_imported_declaration) {
988 if (DWARFDIE module_die =
989 child_die.GetReferencedDIE(DW_AT_import)) {
990 if (module_die.Tag() == DW_TAG_module) {
991 if (const char *name = module_die.GetAttributeValueAsString(
992 DW_AT_name, nullptr)) {
993 ConstString const_name(name);
994 imported_modules.push_back(const_name);
995 }
Sean Callananb0300a42016-01-14 21:46:09 +0000996 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 }
Sean Callananb0300a42016-01-14 21:46:09 +0000998 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001000 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 } else {
1002 for (const auto &pair : m_external_type_modules) {
1003 imported_modules.push_back(pair.first);
1004 }
1005 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001006 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 }
1008 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001009}
1010
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011struct ParseDWARFLineTableCallbackInfo {
1012 LineTable *line_table;
1013 std::unique_ptr<LineSequence> sequence_ap;
1014 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015};
1016
1017//----------------------------------------------------------------------
1018// ParseStatementTableCallback
1019//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020static void ParseDWARFLineTableCallback(dw_offset_t offset,
1021 const DWARFDebugLine::State &state,
1022 void *userData) {
1023 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1024 // Just started parsing the line table
1025 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1026 // Done parsing line table, nothing to do for the cleanup
1027 } else {
1028 ParseDWARFLineTableCallbackInfo *info =
1029 (ParseDWARFLineTableCallbackInfo *)userData;
1030 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031
Adrian Prantl05097242018-04-30 16:49:04 +00001032 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 if (!info->sequence_ap.get()) {
1034 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1035 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 line_table->AppendLineEntryToSequence(
1038 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1039 state.column, state.file, state.is_stmt, state.basic_block,
1040 state.prologue_end, state.epilogue_begin, state.end_sequence);
1041 if (state.end_sequence) {
1042 // First, put the current sequence into the line table.
1043 line_table->InsertSequence(info->sequence_ap.get());
1044 // Then, empty it to prepare for the next sequence.
1045 info->sequence_ap->Clear();
1046 }
1047 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001048}
1049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001051 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 assert(sc.comp_unit);
1053 if (sc.comp_unit->GetLineTable() != NULL)
1054 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001056 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +00001058 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 if (dwarf_cu_die) {
1060 const dw_offset_t cu_line_offset =
1061 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1062 DW_INVALID_OFFSET);
1063 if (cu_line_offset != DW_INVALID_OFFSET) {
1064 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1065 if (line_table_ap.get()) {
1066 ParseDWARFLineTableCallbackInfo info;
1067 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 /*
1070 * MIPS:
1071 * The SymbolContext may not have a valid target, thus we may not be
1072 * able
1073 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1074 * #0
1075 * for MIPS. Use ArchSpec to clear the bit #0.
1076 */
1077 ArchSpec arch;
1078 GetObjectFile()->GetArchitecture(arch);
1079 switch (arch.GetMachine()) {
1080 case llvm::Triple::mips:
1081 case llvm::Triple::mipsel:
1082 case llvm::Triple::mips64:
1083 case llvm::Triple::mips64el:
1084 info.addr_mask = ~((lldb::addr_t)1);
1085 break;
1086 default:
1087 info.addr_mask = ~((lldb::addr_t)0);
1088 break;
1089 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001090
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 lldb::offset_t offset = cu_line_offset;
1092 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1093 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001094 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1096 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001097 // We have an object file that has a line table with addresses that
1098 // are not linked. We need to link the line table and convert the
1099 // addresses that are relative to the .o file into addresses for
1100 // the main executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 sc.comp_unit->SetLineTable(
1102 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1103 } else {
1104 sc.comp_unit->SetLineTable(line_table_ap.release());
1105 return true;
1106 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 }
1111 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112}
1113
Siva Chandrad8335e92015-12-16 00:22:08 +00001114lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1116 auto iter = m_debug_macros_map.find(*offset);
1117 if (iter != m_debug_macros_map.end())
1118 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1121 if (debug_macro_data.GetByteSize() == 0)
1122 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001123
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1125 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001126
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127 const DWARFDebugMacroHeader &header =
1128 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1129 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1130 header.OffsetIs64Bit(), offset, this,
1131 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001132
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001134}
1135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001137 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001139
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001140 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001142 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143
Jan Kratochvil93afb052018-05-24 20:51:13 +00001144 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 if (!dwarf_cu_die)
1146 return false;
1147
1148 lldb::offset_t sect_offset =
1149 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1150 if (sect_offset == DW_INVALID_OFFSET)
1151 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1152 DW_INVALID_OFFSET);
1153 if (sect_offset == DW_INVALID_OFFSET)
1154 return false;
1155
1156 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1157
1158 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001159}
1160
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1162 Block *parent_block,
1163 const DWARFDIE &orig_die,
1164 addr_t subprogram_low_pc,
1165 uint32_t depth) {
1166 size_t blocks_added = 0;
1167 DWARFDIE die = orig_die;
1168 while (die) {
1169 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 switch (tag) {
1172 case DW_TAG_inlined_subroutine:
1173 case DW_TAG_subprogram:
1174 case DW_TAG_lexical_block: {
1175 Block *block = NULL;
1176 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001177 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1178 // inlined functions. These will be parsed on their own as separate
1179 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180
1181 if (depth > 0)
1182 break;
1183
1184 block = parent_block;
1185 } else {
1186 BlockSP block_sp(new Block(die.GetID()));
1187 parent_block->AddChild(block_sp);
1188 block = block_sp.get();
1189 }
1190 DWARFRangeList ranges;
1191 const char *name = NULL;
1192 const char *mangled_name = NULL;
1193
1194 int decl_file = 0;
1195 int decl_line = 0;
1196 int decl_column = 0;
1197 int call_file = 0;
1198 int call_line = 0;
1199 int call_column = 0;
1200 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1201 decl_line, decl_column, call_file, call_line,
1202 call_column, nullptr)) {
1203 if (tag == DW_TAG_subprogram) {
1204 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1205 subprogram_low_pc = ranges.GetMinRangeBase(0);
1206 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001207 // We get called here for inlined subroutines in two ways. The first
1208 // time is when we are making the Function object for this inlined
1209 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001211 // need to adjust the containing address. The second time is when we
1212 // are parsing the blocks inside the function that contains the
1213 // inlined concrete instance. Since these will be blocks inside the
1214 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1216 subprogram_low_pc = ranges.GetMinRangeBase(0);
1217 }
1218 }
1219
1220 const size_t num_ranges = ranges.GetSize();
1221 for (size_t i = 0; i < num_ranges; ++i) {
1222 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1223 const addr_t range_base = range.GetRangeBase();
1224 if (range_base >= subprogram_low_pc)
1225 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1226 range.GetByteSize()));
1227 else {
1228 GetObjectFile()->GetModule()->ReportError(
1229 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1230 ") which has a base that is less than the function's low PC "
1231 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1232 "start of this error message",
1233 block->GetID(), range_base, range.GetRangeEnd(),
1234 subprogram_low_pc);
1235 }
1236 }
1237 block->FinalizeRanges();
1238
1239 if (tag != DW_TAG_subprogram &&
1240 (name != NULL || mangled_name != NULL)) {
1241 std::unique_ptr<Declaration> decl_ap;
1242 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1243 decl_ap.reset(new Declaration(
1244 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1245 decl_line, decl_column));
1246
1247 std::unique_ptr<Declaration> call_ap;
1248 if (call_file != 0 || call_line != 0 || call_column != 0)
1249 call_ap.reset(new Declaration(
1250 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1251 call_line, call_column));
1252
1253 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1254 call_ap.get());
1255 }
1256
1257 ++blocks_added;
1258
1259 if (die.HasChildren()) {
1260 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1261 subprogram_low_pc, depth + 1);
1262 }
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
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1408 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001409}
1410
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1412 bool assert_not_being_parsed) {
1413 if (die) {
1414 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1415 if (log)
1416 GetObjectFile()->GetModule()->LogMessage(
1417 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1418 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001419
Adrian Prantl05097242018-04-30 16:49:04 +00001420 // We might be coming in in the middle of a type tree (a class within a
1421 // class, an enum within a class), so parse any needed parent DIEs before
1422 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1424 if (decl_ctx_die) {
1425 if (log) {
1426 switch (decl_ctx_die.Tag()) {
1427 case DW_TAG_structure_type:
1428 case DW_TAG_union_type:
1429 case DW_TAG_class_type: {
1430 // Get the type, which could be a forward declaration
1431 if (log)
1432 GetObjectFile()->GetModule()->LogMessage(
1433 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1434 "resolve parent forward type for 0x%8.8x",
1435 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1436 decl_ctx_die.GetOffset());
1437 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001438
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 default:
1440 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001441 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001443 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 return ResolveType(die);
1445 }
1446 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001447}
1448
Greg Clayton6beaaa62011-01-17 03:46:26 +00001449// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001450// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1451// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452bool SymbolFileDWARF::HasForwardDeclForClangType(
1453 const CompilerType &compiler_type) {
1454 CompilerType compiler_type_no_qualifiers =
1455 ClangUtil::RemoveFastQualifiers(compiler_type);
1456 if (GetForwardDeclClangTypeToDie().count(
1457 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1458 return true;
1459 }
1460 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001461
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 ClangASTContext *clang_type_system =
1463 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1464 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001465 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 DWARFASTParserClang *ast_parser =
1467 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1468 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001469}
1470
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001472 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001473
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 ClangASTContext *clang_type_system =
1475 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1476 if (clang_type_system) {
1477 DWARFASTParserClang *ast_parser =
1478 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1479 if (ast_parser &&
1480 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1481 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1482 }
Jim Inghamc3549282012-01-11 02:21:12 +00001483
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484 // We have a struct/union/class/enum that needs to be fully resolved.
1485 CompilerType compiler_type_no_qualifiers =
1486 ClangUtil::RemoveFastQualifiers(compiler_type);
1487 auto die_it = GetForwardDeclClangTypeToDie().find(
1488 compiler_type_no_qualifiers.GetOpaqueQualType());
1489 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1490 // We have already resolved this type...
1491 return true;
1492 }
1493
1494 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1495 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001496 // Once we start resolving this type, remove it from the forward
1497 // declaration map in case anyone child members or other types require this
1498 // type to get resolved. The type will get resolved when all of the calls
1499 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 GetForwardDeclClangTypeToDie().erase(die_it);
1501
1502 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1503
1504 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1505 DWARF_LOG_TYPE_COMPLETION));
1506 if (log)
1507 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1508 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1509 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1510 type->GetName().AsCString());
1511 assert(compiler_type);
1512 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1513 if (dwarf_ast)
1514 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1515 }
1516 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001517}
1518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1520 bool assert_not_being_parsed,
1521 bool resolve_function_context) {
1522 if (die) {
1523 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1524
1525 if (assert_not_being_parsed) {
1526 if (type != DIE_IS_BEING_PARSED)
1527 return type;
1528
1529 GetObjectFile()->GetModule()->ReportError(
1530 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1531 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1532
1533 } else
1534 return type;
1535 }
1536 return nullptr;
1537}
1538
1539CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001540SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 uint32_t cu_idx) {
1542 // Check if the symbol vendor already knows about this compile unit?
1543 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001544 // The symbol vendor doesn't know about this compile unit, we need to parse
1545 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1547 }
1548 return (CompileUnit *)dwarf_cu->GetUserData();
1549}
1550
1551size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1552 DIEArray &method_die_offsets) {
1553 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001554 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 return method_die_offsets.size();
1556}
1557
1558bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1559 sc.Clear(false);
1560
1561 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1564
1565 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1566 if (sc.function == NULL)
1567 sc.function = ParseCompileUnitFunction(sc, die);
1568
1569 if (sc.function) {
1570 sc.module_sp = sc.function->CalculateSymbolContextModule();
1571 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 }
1574
1575 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001576}
1577
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1579 UpdateExternalModuleListIfNeeded();
1580 const auto &pos = m_external_type_modules.find(name);
1581 if (pos != m_external_type_modules.end())
1582 return pos->second;
1583 else
1584 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001585}
1586
Greg Clayton2f869fe2016-03-30 20:14:35 +00001587DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1589 DWARFDebugInfo *debug_info = DebugInfo();
1590 if (debug_info)
1591 return debug_info->GetDIE(die_ref);
1592 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001593 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001594}
1595
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596std::unique_ptr<SymbolFileDWARFDwo>
1597SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001598 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001600 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 // info files for modules.
1602 if (GetDebugMapSymfile())
1603 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 const char *dwo_name = cu_die.GetAttributeValueAsString(
1606 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1607 if (!dwo_name)
1608 return nullptr;
1609
Tamas Berghammer963ce482017-08-25 13:56:14 +00001610 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1611 if (dwp_symfile) {
1612 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1613 DW_AT_GNU_dwo_id, 0);
1614 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1615 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1616 if (dwo_symfile)
1617 return dwo_symfile;
1618 }
1619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 FileSpec dwo_file(dwo_name, true);
1621 if (dwo_file.IsRelative()) {
1622 const char *comp_dir = cu_die.GetAttributeValueAsString(
1623 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1624 if (!comp_dir)
1625 return nullptr;
1626
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001627 dwo_file.SetFile(comp_dir, true, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 dwo_file.AppendPathComponent(dwo_name);
1629 }
1630
1631 if (!dwo_file.Exists())
1632 return nullptr;
1633
1634 const lldb::offset_t file_offset = 0;
1635 DataBufferSP dwo_file_data_sp;
1636 lldb::offset_t dwo_file_data_offset = 0;
1637 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1638 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1639 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1640 if (dwo_obj_file == nullptr)
1641 return nullptr;
1642
1643 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001644}
1645
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1647 if (m_fetched_external_modules)
1648 return;
1649 m_fetched_external_modules = true;
1650
1651 DWARFDebugInfo *debug_info = DebugInfo();
1652
1653 const uint32_t num_compile_units = GetNumCompileUnits();
1654 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001655 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656
Jan Kratochvil93afb052018-05-24 20:51:13 +00001657 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 if (die && die.HasChildren() == false) {
1659 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1660
1661 if (name) {
1662 ConstString const_name(name);
1663 if (m_external_type_modules.find(const_name) ==
1664 m_external_type_modules.end()) {
1665 ModuleSP module_sp;
1666 const char *dwo_path =
1667 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1668 if (dwo_path) {
1669 ModuleSpec dwo_module_spec;
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001670 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false,
1671 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1673 const char *comp_dir =
1674 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1675 if (comp_dir) {
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001676 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true,
1677 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1679 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001680 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681 dwo_module_spec.GetArchitecture() =
1682 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001683
Adrian Prantl05097242018-04-30 16:49:04 +00001684 // When LLDB loads "external" modules it looks at the presence of
1685 // DW_AT_GNU_dwo_name. However, when the already created module
1686 // (corresponding to .dwo itself) is being processed, it will see
1687 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1688 // dwo file) and will try to call ModuleList::GetSharedModule
1689 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1690 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1691 // DW_AT_comp_dir. In this case the method
1692 // ModuleList::GetSharedModule will fail and the warning will be
1693 // printed. However, as one can notice in this case we don't
1694 // actually need to try to load the already loaded module
1695 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001696 if (m_obj_file->GetFileSpec()
1697 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001698 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001699 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1700 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1701 continue;
1702 }
1703
Zachary Turner97206d52017-05-12 04:51:55 +00001704 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705 dwo_module_spec, module_sp, NULL, NULL, NULL);
1706 if (!module_sp) {
1707 GetObjectFile()->GetModule()->ReportWarning(
1708 "0x%8.8x: unable to locate module needed for external types: "
1709 "%s\nerror: %s\nDebugging will be degraded due to missing "
1710 "types. Rebuilding your project will regenerate the needed "
1711 "module files.",
1712 die.GetOffset(),
1713 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1714 error.AsCString("unknown error"));
1715 }
1716 }
1717 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001718 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001719 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 }
1722}
1723
1724SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1725 if (!m_global_aranges_ap) {
1726 m_global_aranges_ap.reset(new GlobalVariableMap());
1727
1728 ModuleSP module_sp = GetObjectFile()->GetModule();
1729 if (module_sp) {
1730 const size_t num_cus = module_sp->GetNumCompileUnits();
1731 for (size_t i = 0; i < num_cus; ++i) {
1732 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1733 if (cu_sp) {
1734 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1735 if (globals_sp) {
1736 const size_t num_globals = globals_sp->GetSize();
1737 for (size_t g = 0; g < num_globals; ++g) {
1738 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1739 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1740 const DWARFExpression &location = var_sp->LocationExpression();
1741 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001742 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001743 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1744 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745 if (location_result.GetValueType() ==
1746 Value::eValueTypeFileAddress) {
1747 lldb::addr_t file_addr =
1748 location_result.GetScalar().ULongLong();
1749 lldb::addr_t byte_size = 1;
1750 if (var_sp->GetType())
1751 byte_size = var_sp->GetType()->GetByteSize();
1752 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1753 file_addr, byte_size, var_sp.get()));
1754 }
1755 }
1756 }
1757 }
1758 }
1759 }
1760 }
1761 }
1762 m_global_aranges_ap->Sort();
1763 }
1764 return *m_global_aranges_ap;
1765}
1766
1767uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1768 uint32_t resolve_scope,
1769 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001770 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1771 Timer scoped_timer(func_cat,
1772 "SymbolFileDWARF::"
1773 "ResolveSymbolContext (so_addr = { "
1774 "section = %p, offset = 0x%" PRIx64
1775 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 static_cast<void *>(so_addr.GetSection().get()),
1777 so_addr.GetOffset(), resolve_scope);
1778 uint32_t resolved = 0;
1779 if (resolve_scope &
1780 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1781 eSymbolContextLineEntry | eSymbolContextVariable)) {
1782 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1783
1784 DWARFDebugInfo *debug_info = DebugInfo();
1785 if (debug_info) {
1786 const dw_offset_t cu_offset =
1787 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1788 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001789 // Global variables are not in the compile unit address ranges. The
1790 // only way to currently find global variables is to iterate over the
1791 // .debug_pubnames or the __apple_names table and find all items in
1792 // there that point to DW_TAG_variable DIEs and then find the address
1793 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 if (resolve_scope & eSymbolContextVariable) {
1795 GlobalVariableMap &map = GetGlobalAranges();
1796 const GlobalVariableMap::Entry *entry =
1797 map.FindEntryThatContains(file_vm_addr);
1798 if (entry && entry->data) {
1799 Variable *variable = entry->data;
1800 SymbolContextScope *scc = variable->GetSymbolContextScope();
1801 if (scc) {
1802 scc->CalculateSymbolContext(&sc);
1803 sc.variable = variable;
1804 }
1805 return sc.GetResolvedMask();
1806 }
1807 }
1808 } else {
1809 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001810 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1812 if (dwarf_cu) {
1813 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1814 if (sc.comp_unit) {
1815 resolved |= eSymbolContextCompUnit;
1816
1817 bool force_check_line_table = false;
1818 if (resolve_scope &
1819 (eSymbolContextFunction | eSymbolContextBlock)) {
1820 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1821 DWARFDIE block_die;
1822 if (function_die) {
1823 sc.function =
1824 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1825 if (sc.function == NULL)
1826 sc.function = ParseCompileUnitFunction(sc, function_die);
1827
1828 if (sc.function && (resolve_scope & eSymbolContextBlock))
1829 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1830 } else {
1831 // We might have had a compile unit that had discontiguous
1832 // address ranges where the gaps are symbols that don't have
1833 // any debug info. Discontiguous compile unit address ranges
1834 // should only happen when there aren't other functions from
1835 // other compile units in these gaps. This helps keep the size
1836 // of the aranges down.
1837 force_check_line_table = true;
1838 }
1839
1840 if (sc.function != NULL) {
1841 resolved |= eSymbolContextFunction;
1842
1843 if (resolve_scope & eSymbolContextBlock) {
1844 Block &block = sc.function->GetBlock(true);
1845
1846 if (block_die)
1847 sc.block = block.FindBlockByID(block_die.GetID());
1848 else
1849 sc.block = block.FindBlockByID(function_die.GetID());
1850 if (sc.block)
1851 resolved |= eSymbolContextBlock;
1852 }
1853 }
1854 }
1855
1856 if ((resolve_scope & eSymbolContextLineEntry) ||
1857 force_check_line_table) {
1858 LineTable *line_table = sc.comp_unit->GetLineTable();
1859 if (line_table != NULL) {
1860 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001861 // terms of this debug file if there is no debug map, or it
1862 // will be an address in the .o file which needs to be fixed up
1863 // to be in terms of the debug map executable. Either way,
1864 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865 Address exe_so_addr(so_addr);
1866 if (FixupAddress(exe_so_addr)) {
1867 if (line_table->FindLineEntryByAddress(exe_so_addr,
1868 sc.line_entry)) {
1869 resolved |= eSymbolContextLineEntry;
1870 }
1871 }
1872 }
1873 }
1874
1875 if (force_check_line_table &&
1876 !(resolved & eSymbolContextLineEntry)) {
1877 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001878 // address ranges where the gaps are symbols that don't have any
1879 // debug info. Discontiguous compile unit address ranges should
1880 // only happen when there aren't other functions from other
1881 // compile units in these gaps. This helps keep the size of the
1882 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001883 sc.comp_unit = NULL;
1884 resolved &= ~eSymbolContextCompUnit;
1885 }
1886 } else {
1887 GetObjectFile()->GetModule()->ReportWarning(
1888 "0x%8.8x: compile unit %u failed to create a valid "
1889 "lldb_private::CompileUnit class.",
1890 cu_offset, cu_idx);
1891 }
1892 }
1893 }
1894 }
1895 }
1896 return resolved;
1897}
1898
1899uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1900 uint32_t line,
1901 bool check_inlines,
1902 uint32_t resolve_scope,
1903 SymbolContextList &sc_list) {
1904 const uint32_t prev_size = sc_list.GetSize();
1905 if (resolve_scope & eSymbolContextCompUnit) {
1906 DWARFDebugInfo *debug_info = DebugInfo();
1907 if (debug_info) {
1908 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001909 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910
1911 for (cu_idx = 0;
1912 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1913 ++cu_idx) {
1914 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1915 const bool full_match = (bool)file_spec.GetDirectory();
1916 bool file_spec_matches_cu_file_spec =
1917 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1918 if (check_inlines || file_spec_matches_cu_file_spec) {
1919 SymbolContext sc(m_obj_file->GetModule());
1920 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1921 if (sc.comp_unit) {
1922 uint32_t file_idx = UINT32_MAX;
1923
Adrian Prantl05097242018-04-30 16:49:04 +00001924 // If we are looking for inline functions only and we don't find it
1925 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926 if (check_inlines) {
1927 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1928 1, file_spec, true);
1929 if (file_idx == UINT32_MAX)
1930 continue;
1931 }
1932
1933 if (line != 0) {
1934 LineTable *line_table = sc.comp_unit->GetLineTable();
1935
1936 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001937 // We will have already looked up the file index if we are
1938 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001939 if (!check_inlines)
1940 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1941 1, file_spec, true);
1942
1943 if (file_idx != UINT32_MAX) {
1944 uint32_t found_line;
1945 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1946 0, file_idx, line, false, &sc.line_entry);
1947 found_line = sc.line_entry.line;
1948
1949 while (line_idx != UINT32_MAX) {
1950 sc.function = NULL;
1951 sc.block = NULL;
1952 if (resolve_scope &
1953 (eSymbolContextFunction | eSymbolContextBlock)) {
1954 const lldb::addr_t file_vm_addr =
1955 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1956 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1957 DWARFDIE function_die =
1958 dwarf_cu->LookupAddress(file_vm_addr);
1959 DWARFDIE block_die;
1960 if (function_die) {
1961 sc.function =
1962 sc.comp_unit
1963 ->FindFunctionByUID(function_die.GetID())
1964 .get();
1965 if (sc.function == NULL)
1966 sc.function =
1967 ParseCompileUnitFunction(sc, function_die);
1968
1969 if (sc.function &&
1970 (resolve_scope & eSymbolContextBlock))
1971 block_die =
1972 function_die.LookupDeepestBlock(file_vm_addr);
1973 }
1974
1975 if (sc.function != NULL) {
1976 Block &block = sc.function->GetBlock(true);
1977
1978 if (block_die)
1979 sc.block = block.FindBlockByID(block_die.GetID());
1980 else if (function_die)
1981 sc.block =
1982 block.FindBlockByID(function_die.GetID());
1983 }
1984 }
1985 }
1986
1987 sc_list.Append(sc);
1988 line_idx = line_table->FindLineEntryIndexByFileIndex(
1989 line_idx + 1, file_idx, found_line, true,
1990 &sc.line_entry);
1991 }
1992 }
1993 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1994 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001995 // sites by file and line and if the file spec matches that of
1996 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001997 sc_list.Append(sc);
1998 }
1999 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2000 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00002001 // sites by file and line and if the file spec matches that of
2002 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 sc_list.Append(sc);
2004 }
2005
2006 if (!check_inlines)
2007 break;
2008 }
2009 }
2010 }
2011 }
2012 }
2013 return sc_list.GetSize() - prev_size;
2014}
2015
Jim Ingham7fca8c02017-04-28 00:51:06 +00002016void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002017 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00002018 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002019}
2020
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002021std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
2022 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
2023 if (module_sp)
2024 return module_sp->GetMutex();
2025 return GetObjectFile()->GetModule()->GetMutex();
2026}
2027
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2029 const lldb_private::CompilerDeclContext *decl_ctx) {
2030 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00002031 // Invalid namespace decl which means we aren't matching only things in
2032 // this symbol file, so return true to indicate it matches this symbol
2033 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034 return true;
2035 }
2036
2037 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2038 TypeSystem *type_system = GetTypeSystemForLanguage(
2039 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2040 if (decl_ctx_type_system == type_system)
2041 return true; // The type systems match, return true
2042
2043 // The namespace AST was valid, and it does not match...
2044 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2045
2046 if (log)
2047 GetObjectFile()->GetModule()->LogMessage(
2048 log, "Valid namespace does not match symbol file");
2049
2050 return false;
2051}
2052
2053uint32_t SymbolFileDWARF::FindGlobalVariables(
2054 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002055 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002056 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2057
2058 if (log)
2059 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002060 log,
2061 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2062 "parent_decl_ctx=%p, max_matches=%u, variables)",
2063 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002064 max_matches);
2065
2066 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2067 return 0;
2068
2069 DWARFDebugInfo *info = DebugInfo();
2070 if (info == NULL)
2071 return 0;
2072
Pavel Labath34cda142018-05-31 09:46:26 +00002073 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002074 const uint32_t original_size = variables.GetSize();
2075
Pavel Labathe1d18752018-06-07 10:04:44 +00002076 llvm::StringRef basename;
2077 llvm::StringRef context;
2078
2079 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2080 context, basename))
2081 basename = name.GetStringRef();
2082
Kate Stoneb9c1b512016-09-06 20:57:50 +00002083 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002084 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 const size_t num_die_matches = die_offsets.size();
2086 if (num_die_matches) {
2087 SymbolContext sc;
2088 sc.module_sp = m_obj_file->GetModule();
2089 assert(sc.module_sp);
2090
Pavel Labathe1d18752018-06-07 10:04:44 +00002091 // Loop invariant: Variables up to this index have been checked for context
2092 // matches.
2093 uint32_t pruned_idx = original_size;
2094
Kate Stoneb9c1b512016-09-06 20:57:50 +00002095 bool done = false;
2096 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2097 const DIERef &die_ref = die_offsets[i];
2098 DWARFDIE die = GetDIE(die_ref);
2099
2100 if (die) {
2101 switch (die.Tag()) {
2102 default:
2103 case DW_TAG_subprogram:
2104 case DW_TAG_inlined_subroutine:
2105 case DW_TAG_try_block:
2106 case DW_TAG_catch_block:
2107 break;
2108
2109 case DW_TAG_variable: {
2110 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2111
2112 if (parent_decl_ctx) {
2113 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2114 if (dwarf_ast) {
2115 CompilerDeclContext actual_parent_decl_ctx =
2116 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2117 if (!actual_parent_decl_ctx ||
2118 actual_parent_decl_ctx != *parent_decl_ctx)
2119 continue;
2120 }
2121 }
2122
2123 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2124 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002125 while (pruned_idx < variables.GetSize()) {
2126 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2127 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2128 ++pruned_idx;
2129 else
2130 variables.RemoveVariableAtIndex(pruned_idx);
2131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002132
2133 if (variables.GetSize() - original_size >= max_matches)
2134 done = true;
2135 } break;
2136 }
2137 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002138 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2139 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140 }
2141 }
2142 }
2143
2144 // Return the number of variable that were appended to the list
2145 const uint32_t num_matches = variables.GetSize() - original_size;
2146 if (log && num_matches > 0) {
2147 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002148 log,
2149 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2150 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2151 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 max_matches, num_matches);
2153 }
2154 return num_matches;
2155}
2156
2157uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002158 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 VariableList &variables) {
2160 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2161
2162 if (log) {
2163 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002164 log,
2165 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2166 "max_matches=%u, variables)",
2167 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 }
2169
2170 DWARFDebugInfo *info = DebugInfo();
2171 if (info == NULL)
2172 return 0;
2173
Pavel Labath34cda142018-05-31 09:46:26 +00002174 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002175 const uint32_t original_size = variables.GetSize();
2176
2177 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002178 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002179
2180 SymbolContext sc;
2181 sc.module_sp = m_obj_file->GetModule();
2182 assert(sc.module_sp);
2183
2184 const size_t num_matches = die_offsets.size();
2185 if (num_matches) {
2186 for (size_t i = 0; i < num_matches; ++i) {
2187 const DIERef &die_ref = die_offsets[i];
2188 DWARFDIE die = GetDIE(die_ref);
2189
2190 if (die) {
2191 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2192
2193 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2194
2195 if (variables.GetSize() - original_size >= max_matches)
2196 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002197 } else
2198 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199 }
2200 }
2201
2202 // Return the number of variable that were appended to the list
2203 return variables.GetSize() - original_size;
2204}
2205
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2207 bool include_inlines,
2208 SymbolContextList &sc_list) {
2209 SymbolContext sc;
2210
2211 if (!orig_die)
2212 return false;
2213
2214 // If we were passed a die that is not a function, just return false...
2215 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2216 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2217 return false;
2218
2219 DWARFDIE die = orig_die;
2220 DWARFDIE inlined_die;
2221 if (die.Tag() == DW_TAG_inlined_subroutine) {
2222 inlined_die = die;
2223
2224 while (1) {
2225 die = die.GetParent();
2226
2227 if (die) {
2228 if (die.Tag() == DW_TAG_subprogram)
2229 break;
2230 } else
2231 break;
2232 }
2233 }
2234 assert(die && die.Tag() == DW_TAG_subprogram);
2235 if (GetFunction(die, sc)) {
2236 Address addr;
2237 // Parse all blocks if needed
2238 if (inlined_die) {
2239 Block &function_block = sc.function->GetBlock(true);
2240 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2241 if (sc.block == NULL)
2242 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2243 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2244 addr.Clear();
2245 } else {
2246 sc.block = NULL;
2247 addr = sc.function->GetAddressRange().GetBaseAddress();
2248 }
2249
2250 if (addr.IsValid()) {
2251 sc_list.Append(sc);
2252 return true;
2253 }
2254 }
2255
2256 return false;
2257}
2258
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2260 const DWARFDIE &die) {
2261 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002262 // parent decl context isn't valid, we aren't trying to look for any
2263 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2265 return true;
2266
2267 if (die) {
2268 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2269 if (dwarf_ast) {
2270 CompilerDeclContext actual_decl_ctx =
2271 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2272 if (actual_decl_ctx)
2273 return actual_decl_ctx == *decl_ctx;
2274 }
2275 }
2276 return false;
2277}
2278
2279uint32_t
2280SymbolFileDWARF::FindFunctions(const ConstString &name,
2281 const CompilerDeclContext *parent_decl_ctx,
2282 uint32_t name_type_mask, bool include_inlines,
2283 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002284 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2285 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286 name.AsCString());
2287
2288 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002289 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2291
2292 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2293
2294 if (log) {
2295 GetObjectFile()->GetModule()->LogMessage(
2296 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2297 "name_type_mask=0x%x, append=%u, sc_list)",
2298 name.GetCString(), name_type_mask, append);
2299 }
2300
2301 // If we aren't appending the results to this list, then clear the list
2302 if (!append)
2303 sc_list.Clear();
2304
2305 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2306 return 0;
2307
2308 // If name is empty then we won't find anything.
2309 if (name.IsEmpty())
2310 return 0;
2311
Adrian Prantl05097242018-04-30 16:49:04 +00002312 // Remember how many sc_list are in the list before we search in case we are
2313 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314
Kate Stoneb9c1b512016-09-06 20:57:50 +00002315 const uint32_t original_size = sc_list.GetSize();
2316
2317 DWARFDebugInfo *info = DebugInfo();
2318 if (info == NULL)
2319 return 0;
2320
Pavel Labath5af11ab2018-06-05 10:33:56 +00002321 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2322 DIEArray offsets;
2323 CompilerDeclContext empty_decl_ctx;
2324 if (!parent_decl_ctx)
2325 parent_decl_ctx = &empty_decl_ctx;
2326
2327 std::vector<DWARFDIE> dies;
2328 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2329 for (const DWARFDIE &die: dies) {
2330 if (resolved_dies.insert(die.GetDIE()).second)
2331 ResolveFunction(die, include_inlines, sc_list);
2332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002333
2334 // Return the number of variable that were appended to the list
2335 const uint32_t num_matches = sc_list.GetSize() - original_size;
2336
2337 if (log && num_matches > 0) {
2338 GetObjectFile()->GetModule()->LogMessage(
2339 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2340 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2341 "%u",
2342 name.GetCString(), name_type_mask, include_inlines, append,
2343 num_matches);
2344 }
2345 return num_matches;
2346}
2347
2348uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2349 bool include_inlines, bool append,
2350 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002351 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2352 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002353 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002354
2355 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2356
2357 if (log) {
2358 GetObjectFile()->GetModule()->LogMessage(
2359 log,
2360 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002361 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362 }
2363
2364 // If we aren't appending the results to this list, then clear the list
2365 if (!append)
2366 sc_list.Clear();
2367
Pavel Labathb13f0332018-05-21 14:12:52 +00002368 DWARFDebugInfo *info = DebugInfo();
2369 if (!info)
2370 return 0;
2371
Adrian Prantl05097242018-04-30 16:49:04 +00002372 // Remember how many sc_list are in the list before we search in case we are
2373 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002374 uint32_t original_size = sc_list.GetSize();
2375
Pavel Labath6de9c792018-06-05 12:13:22 +00002376 DIEArray offsets;
2377 m_index->GetFunctions(regex, offsets);
2378
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002379 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2380 for (DIERef ref : offsets) {
2381 DWARFDIE die = info->GetDIE(ref);
2382 if (!die) {
2383 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2384 continue;
2385 }
2386 if (resolved_dies.insert(die.GetDIE()).second)
2387 ResolveFunction(die, include_inlines, sc_list);
2388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002389
2390 // Return the number of variable that were appended to the list
2391 return sc_list.GetSize() - original_size;
2392}
2393
2394void SymbolFileDWARF::GetMangledNamesForFunction(
2395 const std::string &scope_qualified_name,
2396 std::vector<ConstString> &mangled_names) {
2397 DWARFDebugInfo *info = DebugInfo();
2398 uint32_t num_comp_units = 0;
2399 if (info)
2400 num_comp_units = info->GetNumCompileUnits();
2401
2402 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002403 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404 if (cu == nullptr)
2405 continue;
2406
2407 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2408 if (dwo)
2409 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2410 }
2411
2412 NameToOffsetMap::iterator iter =
2413 m_function_scope_qualified_name_map.find(scope_qualified_name);
2414 if (iter == m_function_scope_qualified_name_map.end())
2415 return;
2416
2417 DIERefSetSP set_sp = (*iter).second;
2418 std::set<DIERef>::iterator set_iter;
2419 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2420 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2421 mangled_names.push_back(ConstString(die.GetMangledName()));
2422 }
2423}
2424
2425uint32_t SymbolFileDWARF::FindTypes(
2426 const SymbolContext &sc, const ConstString &name,
2427 const CompilerDeclContext *parent_decl_ctx, bool append,
2428 uint32_t max_matches,
2429 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2430 TypeMap &types) {
2431 // If we aren't appending the results to this list, then clear the list
2432 if (!append)
2433 types.Clear();
2434
2435 // Make sure we haven't already searched this SymbolFile before...
2436 if (searched_symbol_files.count(this))
2437 return 0;
2438 else
2439 searched_symbol_files.insert(this);
2440
2441 DWARFDebugInfo *info = DebugInfo();
2442 if (info == NULL)
2443 return 0;
2444
2445 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2446
2447 if (log) {
2448 if (parent_decl_ctx)
2449 GetObjectFile()->GetModule()->LogMessage(
2450 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2451 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2452 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2453 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2454 else
2455 GetObjectFile()->GetModule()->LogMessage(
2456 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2457 "NULL, append=%u, max_matches=%u, type_list)",
2458 name.GetCString(), append, max_matches);
2459 }
2460
2461 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2462 return 0;
2463
2464 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002465 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002466 const size_t num_die_matches = die_offsets.size();
2467
2468 if (num_die_matches) {
2469 const uint32_t initial_types_size = types.GetSize();
2470 for (size_t i = 0; i < num_die_matches; ++i) {
2471 const DIERef &die_ref = die_offsets[i];
2472 DWARFDIE die = GetDIE(die_ref);
2473
2474 if (die) {
2475 if (!DIEInDeclContext(parent_decl_ctx, die))
2476 continue; // The containing decl contexts don't match
2477
2478 Type *matching_type = ResolveType(die, true, true);
2479 if (matching_type) {
2480 // We found a type pointer, now find the shared pointer form our type
2481 // list
2482 types.InsertUnique(matching_type->shared_from_this());
2483 if (types.GetSize() >= max_matches)
2484 break;
2485 }
2486 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002487 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2488 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002489 }
2490 }
2491 const uint32_t num_matches = types.GetSize() - initial_types_size;
2492 if (log && num_matches) {
2493 if (parent_decl_ctx) {
2494 GetObjectFile()->GetModule()->LogMessage(
2495 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2496 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2497 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2498 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2499 num_matches);
2500 } else {
2501 GetObjectFile()->GetModule()->LogMessage(
2502 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2503 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2504 name.GetCString(), append, max_matches, num_matches);
2505 }
2506 }
2507 return num_matches;
2508 } else {
2509 UpdateExternalModuleListIfNeeded();
2510
2511 for (const auto &pair : m_external_type_modules) {
2512 ModuleSP external_module_sp = pair.second;
2513 if (external_module_sp) {
2514 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2515 if (sym_vendor) {
2516 const uint32_t num_external_matches =
2517 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2518 max_matches, searched_symbol_files, types);
2519 if (num_external_matches)
2520 return num_external_matches;
2521 }
2522 }
2523 }
2524 }
2525
2526 return 0;
2527}
2528
2529size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2530 bool append, TypeMap &types) {
2531 if (!append)
2532 types.Clear();
2533
2534 if (context.empty())
2535 return 0;
2536
Kate Stoneb9c1b512016-09-06 20:57:50 +00002537 ConstString name = context.back().name;
2538
2539 if (!name)
2540 return 0;
2541
Pavel Labathb13f0332018-05-21 14:12:52 +00002542 DIEArray die_offsets;
2543 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 const size_t num_die_matches = die_offsets.size();
2545
2546 if (num_die_matches) {
2547 size_t num_matches = 0;
2548 for (size_t i = 0; i < num_die_matches; ++i) {
2549 const DIERef &die_ref = die_offsets[i];
2550 DWARFDIE die = GetDIE(die_ref);
2551
2552 if (die) {
2553 std::vector<CompilerContext> die_context;
2554 die.GetDWOContext(die_context);
2555 if (die_context != context)
2556 continue;
2557
2558 Type *matching_type = ResolveType(die, true, true);
2559 if (matching_type) {
2560 // We found a type pointer, now find the shared pointer form our type
2561 // list
2562 types.InsertUnique(matching_type->shared_from_this());
2563 ++num_matches;
2564 }
2565 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002566 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2567 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 }
2569 }
2570 return num_matches;
2571 }
2572 return 0;
2573}
2574
2575CompilerDeclContext
2576SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2577 const CompilerDeclContext *parent_decl_ctx) {
2578 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2579
2580 if (log) {
2581 GetObjectFile()->GetModule()->LogMessage(
2582 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2583 name.GetCString());
2584 }
2585
2586 CompilerDeclContext namespace_decl_ctx;
2587
2588 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2589 return namespace_decl_ctx;
2590
2591 DWARFDebugInfo *info = DebugInfo();
2592 if (info) {
2593 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002594 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002595 const size_t num_matches = die_offsets.size();
2596 if (num_matches) {
2597 for (size_t i = 0; i < num_matches; ++i) {
2598 const DIERef &die_ref = die_offsets[i];
2599 DWARFDIE die = GetDIE(die_ref);
2600
2601 if (die) {
2602 if (!DIEInDeclContext(parent_decl_ctx, die))
2603 continue; // The containing decl contexts don't match
2604
2605 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2606 if (dwarf_ast) {
2607 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2608 if (namespace_decl_ctx)
2609 break;
2610 }
2611 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002612 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2613 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002614 }
2615 }
2616 }
2617 }
2618 if (log && namespace_decl_ctx) {
2619 GetObjectFile()->GetModule()->LogMessage(
2620 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2621 "CompilerDeclContext(%p/%p) \"%s\"",
2622 name.GetCString(),
2623 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2624 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2625 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2626 }
2627
2628 return namespace_decl_ctx;
2629}
2630
2631TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2632 bool resolve_function_context) {
2633 TypeSP type_sp;
2634 if (die) {
2635 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2636 if (type_ptr == NULL) {
2637 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2638 assert(lldb_cu);
2639 SymbolContext sc(lldb_cu);
2640 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2641 while (parent_die != nullptr) {
2642 if (parent_die->Tag() == DW_TAG_subprogram)
2643 break;
2644 parent_die = parent_die->GetParent();
2645 }
2646 SymbolContext sc_backup = sc;
2647 if (resolve_function_context && parent_die != nullptr &&
2648 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2649 sc = sc_backup;
2650
2651 type_sp = ParseType(sc, die, NULL);
2652 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2653 // Grab the existing type from the master types lists
2654 type_sp = type_ptr->shared_from_this();
2655 }
2656 }
2657 return type_sp;
2658}
2659
2660DWARFDIE
2661SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2662 if (orig_die) {
2663 DWARFDIE die = orig_die;
2664
2665 while (die) {
2666 // If this is the original DIE that we are searching for a declaration
2667 // for, then don't look in the cache as we don't want our own decl
2668 // context to be our decl context...
2669 if (orig_die != die) {
2670 switch (die.Tag()) {
2671 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002672 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002673 case DW_TAG_namespace:
2674 case DW_TAG_structure_type:
2675 case DW_TAG_union_type:
2676 case DW_TAG_class_type:
2677 case DW_TAG_lexical_block:
2678 case DW_TAG_subprogram:
2679 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002680 case DW_TAG_inlined_subroutine: {
2681 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2682 if (abs_die) {
2683 return abs_die;
2684 }
2685 break;
2686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687 default:
2688 break;
2689 }
2690 }
2691
2692 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2693 if (spec_die) {
2694 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2695 if (decl_ctx_die)
2696 return decl_ctx_die;
2697 }
2698
2699 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2700 if (abs_die) {
2701 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2702 if (decl_ctx_die)
2703 return decl_ctx_die;
2704 }
2705
2706 die = die.GetParent();
2707 }
2708 }
2709 return DWARFDIE();
2710}
2711
2712Symbol *
2713SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2714 Symbol *objc_class_symbol = NULL;
2715 if (m_obj_file) {
2716 Symtab *symtab = m_obj_file->GetSymtab();
2717 if (symtab) {
2718 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2719 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2720 Symtab::eVisibilityAny);
2721 }
2722 }
2723 return objc_class_symbol;
2724}
2725
2726// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002727// they don't then we can end up looking through all class types for a complete
2728// type and never find the full definition. We need to know if this attribute
2729// is supported, so we determine this here and cache th result. We also need to
2730// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002731// DWARF file
2732// if we are doing darwin DWARF in .o file debugging.
2733bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002734 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2736 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2737 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2738 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2739 else {
2740 DWARFDebugInfo *debug_info = DebugInfo();
2741 const uint32_t num_compile_units = GetNumCompileUnits();
2742 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002743 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002744 if (dwarf_cu != cu &&
2745 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2746 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2747 break;
2748 }
2749 }
2750 }
2751 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2752 GetDebugMapSymfile())
2753 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2754 }
2755 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002756}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002757
2758// This function can be used when a DIE is found that is a forward declaration
2759// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002760TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2761 const DWARFDIE &die, const ConstString &type_name,
2762 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002763
Kate Stoneb9c1b512016-09-06 20:57:50 +00002764 TypeSP type_sp;
2765
2766 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2767 return type_sp;
2768
2769 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002770 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002771
2772 const size_t num_matches = die_offsets.size();
2773
2774 if (num_matches) {
2775 for (size_t i = 0; i < num_matches; ++i) {
2776 const DIERef &die_ref = die_offsets[i];
2777 DWARFDIE type_die = GetDIE(die_ref);
2778
2779 if (type_die) {
2780 bool try_resolving_type = false;
2781
Adrian Prantl05097242018-04-30 16:49:04 +00002782 // Don't try and resolve the DIE we are looking for with the DIE
2783 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002784 if (type_die != die) {
2785 switch (type_die.Tag()) {
2786 case DW_TAG_class_type:
2787 case DW_TAG_structure_type:
2788 try_resolving_type = true;
2789 break;
2790 default:
2791 break;
2792 }
2793 }
2794
2795 if (try_resolving_type) {
2796 if (must_be_implementation &&
2797 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2798 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2799 DW_AT_APPLE_objc_complete_type, 0);
2800
2801 if (try_resolving_type) {
2802 Type *resolved_type = ResolveType(type_die, false, true);
2803 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2804 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2805 " (cu 0x%8.8" PRIx64 ")\n",
2806 die.GetID(),
2807 m_obj_file->GetFileSpec().GetFilename().AsCString(
2808 "<Unknown>"),
2809 type_die.GetID(), type_cu->GetID());
2810
2811 if (die)
2812 GetDIEToType()[die.GetDIE()] = resolved_type;
2813 type_sp = resolved_type->shared_from_this();
2814 break;
2815 }
2816 }
2817 }
2818 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002819 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2820 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 }
2822 }
2823 }
2824 return type_sp;
2825}
Greg Claytona8022fa2012-04-24 21:22:41 +00002826
Greg Clayton80c26302012-02-05 06:12:47 +00002827//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002828// This function helps to ensure that the declaration contexts match for two
2829// different DIEs. Often times debug information will refer to a forward
2830// declaration of a type (the equivalent of "struct my_struct;". There will
2831// often be a declaration of that type elsewhere that has the full definition.
2832// When we go looking for the full type "my_struct", we will find one or more
2833// matches in the accelerator tables and we will then need to make sure the
2834// type was in the same declaration context as the original DIE. This function
2835// can efficiently compare two DIEs and will return true when the declaration
2836// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002837//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2839 const DWARFDIE &die2) {
2840 if (die1 == die2)
2841 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002842
Kate Stoneb9c1b512016-09-06 20:57:50 +00002843 DWARFDIECollection decl_ctx_1;
2844 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002845 // The declaration DIE stack is a stack of the declaration context DIEs all
2846 // the way back to the compile unit. If a type "T" is declared inside a class
2847 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2848 // namespace "lldb", and the namespace is in a compile unit, there will be a
2849 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850 //
2851 // [0] DW_TAG_class_type for "B"
2852 // [1] DW_TAG_class_type for "A"
2853 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002854 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002855 //
Adrian Prantl05097242018-04-30 16:49:04 +00002856 // We grab both contexts and make sure that everything matches all the way
2857 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858
2859 // First lets grab the decl contexts for both DIEs
2860 die1.GetDeclContextDIEs(decl_ctx_1);
2861 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00002862 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00002863 const size_t count1 = decl_ctx_1.Size();
2864 const size_t count2 = decl_ctx_2.Size();
2865 if (count1 != count2)
2866 return false;
2867
Adrian Prantl05097242018-04-30 16:49:04 +00002868 // Make sure the DW_TAG values match all the way back up the compile unit. If
2869 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 DWARFDIE decl_ctx_die1;
2871 DWARFDIE decl_ctx_die2;
2872 size_t i;
2873 for (i = 0; i < count1; i++) {
2874 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2875 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2876 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2877 return false;
2878 }
Greg Clayton890ff562012-02-02 05:48:16 +00002879#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002880
Kate Stoneb9c1b512016-09-06 20:57:50 +00002881 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002882 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2883 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2884 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002885 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2886 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2887 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002888
Greg Clayton890ff562012-02-02 05:48:16 +00002889#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002890 // Always skip the compile unit when comparing by only iterating up to "count
2891 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002892 for (i = 0; i < count1 - 1; i++) {
2893 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2894 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2895 const char *name1 = decl_ctx_die1.GetName();
2896 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002897 // If the string was from a DW_FORM_strp, then the pointer will often be
2898 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002899 if (name1 == name2)
2900 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002901
Adrian Prantl05097242018-04-30 16:49:04 +00002902 // Name pointers are not equal, so only compare the strings if both are not
2903 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002904 if (name1 && name2) {
2905 // If the strings don't compare, we are done...
2906 if (strcmp(name1, name2) != 0)
2907 return false;
2908 } else {
2909 // One name was NULL while the other wasn't
2910 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002912 }
Adrian Prantl05097242018-04-30 16:49:04 +00002913 // We made it through all of the checks and the declaration contexts are
2914 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002915 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002916}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002917
Kate Stoneb9c1b512016-09-06 20:57:50 +00002918TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2919 const DWARFDeclContext &dwarf_decl_ctx) {
2920 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002921
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2923 if (dwarf_decl_ctx_count > 0) {
2924 const ConstString type_name(dwarf_decl_ctx[0].name);
2925 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002926
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 if (type_name) {
2928 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2929 DWARF_LOG_LOOKUPS));
2930 if (log) {
2931 GetObjectFile()->GetModule()->LogMessage(
2932 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2933 "s, qualified-name='%s')",
2934 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2935 dwarf_decl_ctx.GetQualifiedName());
2936 }
2937
2938 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002939 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002940 const size_t num_matches = die_offsets.size();
2941
Adrian Prantl05097242018-04-30 16:49:04 +00002942 // Get the type system that we are looking to find a type for. We will
2943 // use this to ensure any matches we find are in a language that this
2944 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002945 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2946 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2947 ? nullptr
2948 : GetTypeSystemForLanguage(language);
2949
2950 if (num_matches) {
2951 for (size_t i = 0; i < num_matches; ++i) {
2952 const DIERef &die_ref = die_offsets[i];
2953 DWARFDIE type_die = GetDIE(die_ref);
2954
2955 if (type_die) {
2956 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002957 // looking for. We don't want to find a "Foo" type from Java if we
2958 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002959 if (type_system &&
2960 !type_system->SupportsLanguage(type_die.GetLanguage()))
2961 continue;
2962 bool try_resolving_type = false;
2963
2964 // Don't try and resolve the DIE we are looking for with the DIE
2965 // itself!
2966 const dw_tag_t type_tag = type_die.Tag();
2967 // Make sure the tags match
2968 if (type_tag == tag) {
2969 // The tags match, lets try resolving this type
2970 try_resolving_type = true;
2971 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002972 // The tags don't match, but we need to watch our for a forward
2973 // declaration for a struct and ("struct foo") ends up being a
2974 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002975 switch (type_tag) {
2976 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002977 // We had a "class foo", see if we ended up with a "struct foo
2978 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002979 try_resolving_type = (tag == DW_TAG_structure_type);
2980 break;
2981 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002982 // We had a "struct foo", see if we ended up with a "class foo
2983 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984 try_resolving_type = (tag == DW_TAG_class_type);
2985 break;
2986 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002987 // Tags don't match, don't event try to resolve using this type
2988 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002989 break;
2990 }
2991 }
2992
2993 if (try_resolving_type) {
2994 DWARFDeclContext type_dwarf_decl_ctx;
2995 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2996
2997 if (log) {
2998 GetObjectFile()->GetModule()->LogMessage(
2999 log, "SymbolFileDWARF::"
3000 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3001 "qualified-name='%s') trying die=0x%8.8x (%s)",
3002 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3003 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3004 type_dwarf_decl_ctx.GetQualifiedName());
3005 }
3006
3007 // Make sure the decl contexts match all the way up
3008 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3009 Type *resolved_type = ResolveType(type_die, false);
3010 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3011 type_sp = resolved_type->shared_from_this();
3012 break;
3013 }
3014 }
3015 } else {
3016 if (log) {
3017 std::string qualified_name;
3018 type_die.GetQualifiedName(qualified_name);
3019 GetObjectFile()->GetModule()->LogMessage(
3020 log, "SymbolFileDWARF::"
3021 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3022 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3023 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3024 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3025 qualified_name.c_str());
3026 }
3027 }
3028 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00003029 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
3030 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031 }
3032 }
3033 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003034 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003035 }
3036 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003037}
3038
Kate Stoneb9c1b512016-09-06 20:57:50 +00003039TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3040 bool *type_is_new_ptr) {
3041 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042
Kate Stoneb9c1b512016-09-06 20:57:50 +00003043 if (die) {
3044 TypeSystem *type_system =
3045 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003046
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 if (type_system) {
3048 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3049 if (dwarf_ast) {
3050 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3051 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3052 if (type_sp) {
3053 TypeList *type_list = GetTypeList();
3054 if (type_list)
3055 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003056
Kate Stoneb9c1b512016-09-06 20:57:50 +00003057 if (die.Tag() == DW_TAG_subprogram) {
3058 DIERef die_ref = die.GetDIERef();
3059 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3060 .GetScopeQualifiedName()
3061 .AsCString(""));
3062 if (scope_qualified_name.size()) {
3063 NameToOffsetMap::iterator iter =
3064 m_function_scope_qualified_name_map.find(
3065 scope_qualified_name);
3066 if (iter != m_function_scope_qualified_name_map.end())
3067 (*iter).second->insert(die_ref);
3068 else {
3069 DIERefSetSP new_set(new std::set<DIERef>);
3070 new_set->insert(die_ref);
3071 m_function_scope_qualified_name_map.emplace(
3072 std::make_pair(scope_qualified_name, new_set));
3073 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003075 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003079 }
3080
3081 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003082}
3083
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3085 const DWARFDIE &orig_die,
3086 bool parse_siblings, bool parse_children) {
3087 size_t types_added = 0;
3088 DWARFDIE die = orig_die;
3089 while (die) {
3090 bool type_is_new = false;
3091 if (ParseType(sc, die, &type_is_new).get()) {
3092 if (type_is_new)
3093 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003095
3096 if (parse_children && die.HasChildren()) {
3097 if (die.Tag() == DW_TAG_subprogram) {
3098 SymbolContext child_sc(sc);
3099 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3100 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3101 } else
3102 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3103 }
3104
3105 if (parse_siblings)
3106 die = die.GetSibling();
3107 else
3108 die.Clear();
3109 }
3110 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003111}
3112
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003114 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003115 assert(sc.comp_unit && sc.function);
3116 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003117 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003118 if (dwarf_cu) {
3119 const dw_offset_t function_die_offset = sc.function->GetID();
3120 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3121 if (function_die) {
3122 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3123 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003124 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003126
Kate Stoneb9c1b512016-09-06 20:57:50 +00003127 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003128}
3129
Kate Stoneb9c1b512016-09-06 20:57:50 +00003130size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003131 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132 // At least a compile unit must be valid
3133 assert(sc.comp_unit);
3134 size_t types_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003135 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003136 if (dwarf_cu) {
3137 if (sc.function) {
3138 dw_offset_t function_die_offset = sc.function->GetID();
3139 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3140 if (func_die && func_die.HasChildren()) {
3141 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3142 }
3143 } else {
3144 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3145 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3146 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3147 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003150
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003152}
3153
Kate Stoneb9c1b512016-09-06 20:57:50 +00003154size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003155 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156 if (sc.comp_unit != NULL) {
3157 DWARFDebugInfo *info = DebugInfo();
3158 if (info == NULL)
3159 return 0;
3160
3161 if (sc.function) {
3162 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3163
3164 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3165 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3166 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3167 const size_t num_variables = ParseVariables(
3168 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3169
3170 // Let all blocks know they have parse all their variables
3171 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3172 return num_variables;
3173 }
3174 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003175 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003176
3177 if (dwarf_cu == NULL)
3178 return 0;
3179
3180 uint32_t vars_added = 0;
3181 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3182
3183 if (variables.get() == NULL) {
3184 variables.reset(new VariableList());
3185 sc.comp_unit->SetVariableList(variables);
3186
3187 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003188 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003189 const size_t num_matches = die_offsets.size();
3190 if (num_matches) {
3191 for (size_t i = 0; i < num_matches; ++i) {
3192 const DIERef &die_ref = die_offsets[i];
3193 DWARFDIE die = GetDIE(die_ref);
3194 if (die) {
3195 VariableSP var_sp(
3196 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3197 if (var_sp) {
3198 variables->AddVariableIfUnique(var_sp);
3199 ++vars_added;
3200 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003201 } else
3202 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003205 }
3206 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003207 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003208 }
3209 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003210}
3211
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3213 const DWARFDIE &die,
3214 const lldb::addr_t func_low_pc) {
3215 if (die.GetDWARF() != this)
3216 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003217
Kate Stoneb9c1b512016-09-06 20:57:50 +00003218 VariableSP var_sp;
3219 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003220 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003221
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222 var_sp = GetDIEToVariable()[die.GetDIE()];
3223 if (var_sp)
3224 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003225
Kate Stoneb9c1b512016-09-06 20:57:50 +00003226 const dw_tag_t tag = die.Tag();
3227 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003228
Kate Stoneb9c1b512016-09-06 20:57:50 +00003229 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3230 (tag == DW_TAG_formal_parameter && sc.function)) {
3231 DWARFAttributes attributes;
3232 const size_t num_attributes = die.GetAttributes(attributes);
3233 DWARFDIE spec_die;
3234 if (num_attributes > 0) {
3235 const char *name = NULL;
3236 const char *mangled = NULL;
3237 Declaration decl;
3238 uint32_t i;
3239 DWARFFormValue type_die_form;
3240 DWARFExpression location(die.GetCU());
3241 bool is_external = false;
3242 bool is_artificial = false;
3243 bool location_is_const_value_data = false;
3244 bool has_explicit_location = false;
3245 DWARFFormValue const_value;
3246 Variable::RangeList scope_ranges;
3247 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003248
Kate Stoneb9c1b512016-09-06 20:57:50 +00003249 for (i = 0; i < num_attributes; ++i) {
3250 dw_attr_t attr = attributes.AttributeAtIndex(i);
3251 DWARFFormValue form_value;
3252
3253 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3254 switch (attr) {
3255 case DW_AT_decl_file:
3256 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3257 form_value.Unsigned()));
3258 break;
3259 case DW_AT_decl_line:
3260 decl.SetLine(form_value.Unsigned());
3261 break;
3262 case DW_AT_decl_column:
3263 decl.SetColumn(form_value.Unsigned());
3264 break;
3265 case DW_AT_name:
3266 name = form_value.AsCString();
3267 break;
3268 case DW_AT_linkage_name:
3269 case DW_AT_MIPS_linkage_name:
3270 mangled = form_value.AsCString();
3271 break;
3272 case DW_AT_type:
3273 type_die_form = form_value;
3274 break;
3275 case DW_AT_external:
3276 is_external = form_value.Boolean();
3277 break;
3278 case DW_AT_const_value:
3279 // If we have already found a DW_AT_location attribute, ignore this
3280 // attribute.
3281 if (!has_explicit_location) {
3282 location_is_const_value_data = true;
3283 // The constant value will be either a block, a data value or a
3284 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003285 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3287 // Retrieve the value as a block expression.
3288 uint32_t block_offset =
3289 form_value.BlockData() - debug_info_data.GetDataStart();
3290 uint32_t block_length = form_value.Unsigned();
3291 location.CopyOpcodeData(module, debug_info_data, block_offset,
3292 block_length);
3293 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3294 // Retrieve the value as a data expression.
3295 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3296 DWARFFormValue::GetFixedFormSizesForAddressSize(
3297 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3298 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3299 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3300 uint32_t data_length =
3301 fixed_form_sizes.GetSize(form_value.Form());
3302 if (data_length == 0) {
3303 const uint8_t *data_pointer = form_value.BlockData();
3304 if (data_pointer) {
3305 form_value.Unsigned();
3306 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3307 // we need to get the byte size of the type later after we
3308 // create the variable
3309 const_value = form_value;
3310 }
3311 } else
3312 location.CopyOpcodeData(module, debug_info_data, data_offset,
3313 data_length);
3314 } else {
3315 // Retrieve the value as a string expression.
3316 if (form_value.Form() == DW_FORM_strp) {
3317 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3318 DWARFFormValue::GetFixedFormSizesForAddressSize(
3319 attributes.CompileUnitAtIndex(i)
3320 ->GetAddressByteSize(),
3321 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3322 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3323 uint32_t data_length =
3324 fixed_form_sizes.GetSize(form_value.Form());
3325 location.CopyOpcodeData(module, debug_info_data, data_offset,
3326 data_length);
3327 } else {
3328 const char *str = form_value.AsCString();
3329 uint32_t string_offset =
3330 str - (const char *)debug_info_data.GetDataStart();
3331 uint32_t string_length = strlen(str) + 1;
3332 location.CopyOpcodeData(module, debug_info_data,
3333 string_offset, string_length);
3334 }
3335 }
3336 }
3337 break;
3338 case DW_AT_location: {
3339 location_is_const_value_data = false;
3340 has_explicit_location = true;
3341 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003342 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003343
3344 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003345 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003346 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003347 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003348 } else {
3349 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3350 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3351
3352 size_t loc_list_length = DWARFExpression::LocationListSize(
3353 die.GetCU(), debug_loc_data, debug_loc_offset);
3354 if (loc_list_length > 0) {
3355 location.CopyOpcodeData(module, debug_loc_data,
3356 debug_loc_offset, loc_list_length);
3357 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3358 location.SetLocationListSlide(
3359 func_low_pc -
3360 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3361 }
3362 }
3363 } break;
3364 case DW_AT_specification:
3365 spec_die = GetDIE(DIERef(form_value));
3366 break;
3367 case DW_AT_start_scope: {
3368 if (form_value.Form() == DW_FORM_sec_offset) {
3369 DWARFRangeList dwarf_scope_ranges;
3370 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003371 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3372 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373 dwarf_scope_ranges);
3374
3375 // All DW_AT_start_scope are relative to the base address of the
3376 // compile unit. We add the compile unit base address to make
3377 // sure all the addresses are properly fixed up.
George Rimar60b6f142018-10-10 08:49:17 +00003378 dwarf_scope_ranges.Slide(die.GetCU()->GetBaseAddress());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003379 } else {
3380 // TODO: Handle the case when DW_AT_start_scope have form
3381 // constant. The
3382 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003383 // behavior in case the enclosing block have a non coninious
3384 // address range and the DW_AT_start_scope entry have a form
3385 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003386 GetObjectFile()->GetModule()->ReportWarning(
3387 "0x%8.8" PRIx64
3388 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3389 die.GetID(), form_value.Form());
3390 }
3391
3392 scope_ranges.Sort();
3393 scope_ranges.CombineConsecutiveRanges();
3394 } break;
3395 case DW_AT_artificial:
3396 is_artificial = form_value.Boolean();
3397 break;
3398 case DW_AT_accessibility:
3399 break; // accessibility =
3400 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3401 case DW_AT_declaration:
3402 case DW_AT_description:
3403 case DW_AT_endianity:
3404 case DW_AT_segment:
3405 case DW_AT_visibility:
3406 default:
3407 case DW_AT_abstract_origin:
3408 case DW_AT_sibling:
3409 break;
3410 }
3411 }
3412 }
3413
3414 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3415 const dw_tag_t parent_tag = die.GetParent().Tag();
3416 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003417 (parent_tag == DW_TAG_compile_unit ||
3418 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003419 (parent_context_die.Tag() == DW_TAG_class_type ||
3420 parent_context_die.Tag() == DW_TAG_structure_type);
3421
3422 ValueType scope = eValueTypeInvalid;
3423
3424 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3425 SymbolContextScope *symbol_context_scope = NULL;
3426
Sam McCall30621392016-11-22 11:40:25 +00003427 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 if (!mangled) {
3429 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003430 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3431 // of global variables with commands like "frame var j". For
3432 // example, if j were an int variable holding a value 4 and
3433 // declared in a namespace B which in turn is contained in a
3434 // namespace A, the command "frame var j" returns
3435 // "(int) A::B::j = 4".
3436 // If the compiler does not emit a linkage name, we should be
3437 // able to generate a fully qualified name from the
3438 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003439 if ((parent_tag == DW_TAG_compile_unit ||
3440 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003441 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3442 DWARFDeclContext decl_ctx;
3443
3444 die.GetDWARFDeclContext(decl_ctx);
3445 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3446 }
3447 }
3448
Kate Stoneb9c1b512016-09-06 20:57:50 +00003449 if (tag == DW_TAG_formal_parameter)
3450 scope = eValueTypeVariableArgument;
3451 else {
Sam McCall30621392016-11-22 11:40:25 +00003452 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3453 // or static variable, so we have to do a little digging:
3454 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3455 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3456 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3457 // Clang likes to combine small global variables into the same symbol
3458 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3459 // so we need to look through the whole expression.
3460 bool is_static_lifetime =
3461 has_explicit_mangled ||
3462 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003463 // Check if the location has a DW_OP_addr with any address value...
3464 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3465 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003466 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003467 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3468 if (op_error) {
3469 StreamString strm;
3470 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3471 NULL);
3472 GetObjectFile()->GetModule()->ReportError(
3473 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003474 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003475 }
Sam McCall30621392016-11-22 11:40:25 +00003476 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3477 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003478 }
3479 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003480 if (debug_map_symfile)
3481 // Set the module of the expression to the linked module
3482 // instead of the oject file so the relocated address can be
3483 // found there.
3484 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003485
Sam McCall30621392016-11-22 11:40:25 +00003486 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003487 if (is_external)
3488 scope = eValueTypeVariableGlobal;
3489 else
3490 scope = eValueTypeVariableStatic;
3491
3492 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003493 // When leaving the DWARF in the .o files on darwin, when we have a
3494 // global variable that wasn't initialized, the .o file might not
3495 // have allocated a virtual address for the global variable. In
3496 // this case it will have created a symbol for the global variable
3497 // that is undefined/data and external and the value will be the
3498 // byte size of the variable. When we do the address map in
3499 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3500 // do some magic here so we can get the correct address for our
3501 // global variable. The address for all of these entries will be
3502 // zero, and there will be an undefined symbol in this object file,
3503 // and the executable will have a matching symbol with a good
3504 // address. So here we dig up the correct address and replace it in
3505 // the location for the variable, and set the variable's symbol
3506 // context scope to be that of the main executable so the file
3507 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003508 bool linked_oso_file_addr = false;
3509 if (is_external && location_DW_OP_addr == 0) {
3510 // we have a possible uninitialized extern global
3511 ConstString const_name(mangled ? mangled : name);
3512 ObjectFile *debug_map_objfile =
3513 debug_map_symfile->GetObjectFile();
3514 if (debug_map_objfile) {
3515 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3516 if (debug_map_symtab) {
3517 Symbol *exe_symbol =
3518 debug_map_symtab->FindFirstSymbolWithNameAndType(
3519 const_name, eSymbolTypeData, Symtab::eDebugYes,
3520 Symtab::eVisibilityExtern);
3521 if (exe_symbol) {
3522 if (exe_symbol->ValueIsAddress()) {
3523 const addr_t exe_file_addr =
3524 exe_symbol->GetAddressRef().GetFileAddress();
3525 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3526 if (location.Update_DW_OP_addr(exe_file_addr)) {
3527 linked_oso_file_addr = true;
3528 symbol_context_scope = exe_symbol;
3529 }
3530 }
3531 }
3532 }
3533 }
3534 }
3535 }
3536
3537 if (!linked_oso_file_addr) {
3538 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003539 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003540 const lldb::addr_t exe_file_addr =
3541 debug_map_symfile->LinkOSOFileAddress(this,
3542 location_DW_OP_addr);
3543 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3544 // Update the file address for this variable
3545 location.Update_DW_OP_addr(exe_file_addr);
3546 } else {
3547 // Variable didn't make it into the final executable
3548 return var_sp;
3549 }
3550 }
3551 }
3552 } else {
3553 if (location_is_const_value_data)
3554 scope = eValueTypeVariableStatic;
3555 else {
3556 scope = eValueTypeVariableLocal;
3557 if (debug_map_symfile) {
3558 // We need to check for TLS addresses that we need to fixup
3559 if (location.ContainsThreadLocalStorage()) {
3560 location.LinkThreadLocalStorage(
3561 debug_map_symfile->GetObjectFile()->GetModule(),
3562 [this, debug_map_symfile](
3563 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3564 return debug_map_symfile->LinkOSOFileAddress(
3565 this, unlinked_file_addr);
3566 });
3567 scope = eValueTypeVariableThreadLocal;
3568 }
3569 }
3570 }
3571 }
3572 }
3573
3574 if (symbol_context_scope == NULL) {
3575 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003576 case DW_TAG_subprogram:
3577 case DW_TAG_inlined_subroutine:
3578 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003579 if (sc.function) {
3580 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3581 sc_parent_die.GetID());
3582 if (symbol_context_scope == NULL)
3583 symbol_context_scope = sc.function;
3584 }
3585 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003586
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587 default:
3588 symbol_context_scope = sc.comp_unit;
3589 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003592
Kate Stoneb9c1b512016-09-06 20:57:50 +00003593 if (symbol_context_scope) {
3594 SymbolFileTypeSP type_sp(
3595 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3596
3597 if (const_value.Form() && type_sp && type_sp->GetType())
3598 location.CopyOpcodeData(const_value.Unsigned(),
3599 type_sp->GetType()->GetByteSize(),
3600 die.GetCU()->GetAddressByteSize());
3601
3602 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3603 symbol_context_scope, scope_ranges, &decl,
3604 location, is_external, is_artificial,
3605 is_static_member));
3606
3607 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3608 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003609 // Not ready to parse this variable yet. It might be a global or static
3610 // variable that is in a function scope and the function in the symbol
3611 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003612 return var_sp;
3613 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003614 }
Adrian Prantl05097242018-04-30 16:49:04 +00003615 // Cache var_sp even if NULL (the variable was just a specification or was
3616 // missing vital information to be able to be displayed in the debugger
3617 // (missing location due to optimization, etc)) so we don't re-parse this
3618 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619 GetDIEToVariable()[die.GetDIE()] = var_sp;
3620 if (spec_die)
3621 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3622 }
3623 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003624}
3625
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626DWARFDIE
3627SymbolFileDWARF::FindBlockContainingSpecification(
3628 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3629 // Give the concrete function die specified by "func_die_offset", find the
3630 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3631 // to "spec_block_die_offset"
3632 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3633 spec_block_die_offset);
3634}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003635
Kate Stoneb9c1b512016-09-06 20:57:50 +00003636DWARFDIE
3637SymbolFileDWARF::FindBlockContainingSpecification(
3638 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3639 if (die) {
3640 switch (die.Tag()) {
3641 case DW_TAG_subprogram:
3642 case DW_TAG_inlined_subroutine:
3643 case DW_TAG_lexical_block: {
3644 if (die.GetAttributeValueAsReference(
3645 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3646 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003647
Kate Stoneb9c1b512016-09-06 20:57:50 +00003648 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3649 DW_INVALID_OFFSET) ==
3650 spec_block_die_offset)
3651 return die;
3652 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003654
3655 // Give the concrete function die specified by "func_die_offset", find the
3656 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3657 // to "spec_block_die_offset"
3658 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3659 child_die = child_die.GetSibling()) {
3660 DWARFDIE result_die =
3661 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3662 if (result_die)
3663 return result_die;
3664 }
3665 }
3666
3667 return DWARFDIE();
3668}
3669
3670size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3671 const DWARFDIE &orig_die,
3672 const lldb::addr_t func_low_pc,
3673 bool parse_siblings, bool parse_children,
3674 VariableList *cc_variable_list) {
3675 if (!orig_die)
3676 return 0;
3677
3678 VariableListSP variable_list_sp;
3679
3680 size_t vars_added = 0;
3681 DWARFDIE die = orig_die;
3682 while (die) {
3683 dw_tag_t tag = die.Tag();
3684
3685 // Check to see if we have already parsed this variable or constant?
3686 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3687 if (var_sp) {
3688 if (cc_variable_list)
3689 cc_variable_list->AddVariableIfUnique(var_sp);
3690 } else {
3691 // We haven't already parsed it, lets do that now.
3692 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3693 (tag == DW_TAG_formal_parameter && sc.function)) {
3694 if (variable_list_sp.get() == NULL) {
3695 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3696 dw_tag_t parent_tag = sc_parent_die.Tag();
3697 switch (parent_tag) {
3698 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003699 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003700 if (sc.comp_unit != NULL) {
3701 variable_list_sp = sc.comp_unit->GetVariableList(false);
3702 if (variable_list_sp.get() == NULL) {
3703 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704 }
3705 } else {
3706 GetObjectFile()->GetModule()->ReportError(
3707 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3708 "symbol context for 0x%8.8" PRIx64
3709 " %s.\n",
3710 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3711 orig_die.GetID(), orig_die.GetTagAsCString());
3712 }
3713 break;
3714
3715 case DW_TAG_subprogram:
3716 case DW_TAG_inlined_subroutine:
3717 case DW_TAG_lexical_block:
3718 if (sc.function != NULL) {
3719 // Check to see if we already have parsed the variables for the
3720 // given scope
3721
3722 Block *block = sc.function->GetBlock(true).FindBlockByID(
3723 sc_parent_die.GetID());
3724 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003725 // This must be a specification or abstract origin with a
3726 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003727 // to find the concrete block so we can correctly add the
3728 // variable to it
3729 const DWARFDIE concrete_block_die =
3730 FindBlockContainingSpecification(
3731 DIERef(sc.function->GetID(), this),
3732 sc_parent_die.GetOffset());
3733 if (concrete_block_die)
3734 block = sc.function->GetBlock(true).FindBlockByID(
3735 concrete_block_die.GetID());
3736 }
3737
3738 if (block != NULL) {
3739 const bool can_create = false;
3740 variable_list_sp = block->GetBlockVariableList(can_create);
3741 if (variable_list_sp.get() == NULL) {
3742 variable_list_sp.reset(new VariableList());
3743 block->SetVariableList(variable_list_sp);
3744 }
3745 }
3746 }
3747 break;
3748
3749 default:
3750 GetObjectFile()->GetModule()->ReportError(
3751 "didn't find appropriate parent DIE for variable list for "
3752 "0x%8.8" PRIx64 " %s.\n",
3753 orig_die.GetID(), orig_die.GetTagAsCString());
3754 break;
3755 }
3756 }
3757
3758 if (variable_list_sp) {
3759 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3760 if (var_sp) {
3761 variable_list_sp->AddVariableIfUnique(var_sp);
3762 if (cc_variable_list)
3763 cc_variable_list->AddVariableIfUnique(var_sp);
3764 ++vars_added;
3765 }
3766 }
3767 }
3768 }
3769
3770 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3771
3772 if (!skip_children && parse_children && die.HasChildren()) {
3773 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3774 true, cc_variable_list);
3775 }
3776
3777 if (parse_siblings)
3778 die = die.GetSibling();
3779 else
3780 die.Clear();
3781 }
3782 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003783}
3784
Vedant Kumar4b36f792018-10-05 23:23:15 +00003785/// Collect call graph edges present in a function DIE.
3786static std::vector<lldb_private::CallEdge>
3787CollectCallEdges(DWARFDIE function_die) {
3788 // Check if the function has a supported call site-related attribute.
3789 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3790 uint64_t has_call_edges =
3791 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3792 if (!has_call_edges)
3793 return {};
3794
3795 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3796 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3797 function_die.GetPubname());
3798
3799 // Scan the DIE for TAG_call_site entries.
3800 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3801 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3802 // For now, assume that all entries are nested directly under the subprogram
3803 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3804 std::vector<CallEdge> call_edges;
3805 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3806 child = child.GetSibling()) {
3807 if (child.Tag() != DW_TAG_call_site)
3808 continue;
3809
3810 // Extract DW_AT_call_origin (the call target's DIE).
3811 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3812 if (!call_origin.IsValid()) {
3813 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3814 function_die.GetPubname());
3815 continue;
3816 }
3817
3818 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3819 // available. It should only ever be unavailable for tail call edges, in
3820 // which case use LLDB_INVALID_ADDRESS.
3821 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3822 LLDB_INVALID_ADDRESS);
3823
3824 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3825 call_origin.GetPubname(), return_pc);
3826 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3827 }
3828 return call_edges;
3829}
3830
3831std::vector<lldb_private::CallEdge>
3832SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3833 DWARFDIE func_die = GetDIEFromUID(func_id.GetID());
3834 if (func_die.IsValid())
3835 return CollectCallEdges(func_die);
3836 return {};
3837}
3838
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003839//------------------------------------------------------------------
3840// PluginInterface protocol
3841//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003842ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3843
3844uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3845
Pavel Labath9337b412018-06-06 11:35:23 +00003846void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003847
Kate Stoneb9c1b512016-09-06 20:57:50 +00003848SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3849 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3850 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3851 if (module_sp) {
3852 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3853 if (sym_vendor)
3854 m_debug_map_symfile =
3855 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003857 }
3858 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003859}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003860
3861DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003862SymbolFileDWARF::GetLocationListFormat() const {
3863 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003864}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003865
3866SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3867 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003868 ModuleSpec module_spec;
3869 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3870 module_spec.GetSymbolFileSpec() =
3871 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
3872 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Tamas Berghammer963ce482017-08-25 13:56:14 +00003873 if (dwp_filespec.Exists()) {
3874 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3875 dwp_filespec);
3876 }
3877 });
3878 return m_dwp_symfile.get();
3879}