blob: 1a852fcd220e2259983d4ec4f95814b37a8388e4 [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;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000215 const auto error = FileSystem::Instance().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,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000344 TypeClass 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
George Rimare4dee262018-10-23 09:46:15 +0000657const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
658 const DWARFDataExtractor &debugLocData = get_debug_loc_data();
659 if (debugLocData.GetByteSize() > 0)
660 return debugLocData;
661 return get_debug_loclists_data();
662}
663
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
665 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
666}
667
George Rimare4dee262018-10-23 09:46:15 +0000668const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
669 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
670 m_data_debug_loclists);
671}
672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
674 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
675 m_data_debug_ranges);
676}
677
George Rimar6e357122018-10-10 08:11:15 +0000678const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
679 return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
680 m_data_debug_rnglists);
681}
682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
684 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
685}
686
687const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
688 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
689 m_data_debug_str_offsets);
690}
691
Greg Clayton2550ca12018-05-08 17:19:24 +0000692const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
693 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
694}
695
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
697 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
698}
699
700const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
701 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
702}
703
704const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
705 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
706 m_data_apple_namespaces);
707}
708
709const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
710 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
711}
712
Jan Kratochvile4777a92018-04-29 19:47:48 +0000713const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
714 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
715 m_data_gnu_debugaltlink);
716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
719 if (m_abbr.get() == NULL) {
720 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
721 if (debug_abbrev_data.GetByteSize() > 0) {
722 m_abbr.reset(new DWARFDebugAbbrev());
723 if (m_abbr.get())
724 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 }
727 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728}
729
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
731 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732}
733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
735 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000736 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
737 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
738 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 if (get_debug_info_data().GetByteSize() > 0) {
740 m_info.reset(new DWARFDebugInfo());
741 if (m_info.get()) {
742 m_info->SetDwarfData(this);
743 }
Greg Clayton1f746072012-08-29 21:13:06 +0000744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 }
746 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747}
748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
750 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751}
752
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000753DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
755 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000756 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757
758 DWARFDebugInfo *info = DebugInfo();
759 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000760 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
761 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000763 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
765 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
766 dwarf_cu->SetUserData(comp_unit);
767 return dwarf_cu;
768 }
769 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770}
771
George Rimar7e7f9c12018-10-25 10:25:45 +0000772DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000774 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
775 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
776 static_cast<void *>(this));
George Rimar6e357122018-10-10 08:11:15 +0000777
778 if (get_debug_ranges_data().GetByteSize() > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 m_ranges.reset(new DWARFDebugRanges());
George Rimar6e357122018-10-10 08:11:15 +0000780 else if (get_debug_rnglists_data().GetByteSize() > 0)
781 m_ranges.reset(new DWARFDebugRngLists());
782
783 if (m_ranges.get())
784 m_ranges->Extract(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 }
786 return m_ranges.get();
787}
788
George Rimar7e7f9c12018-10-25 10:25:45 +0000789const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 return m_ranges.get();
791}
792
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000793lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 uint32_t cu_idx) {
795 CompUnitSP cu_sp;
796 if (dwarf_cu) {
797 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
798 if (comp_unit) {
799 // We already parsed this compile unit, had out a shared pointer to it
800 cu_sp = comp_unit->shared_from_this();
801 } else {
802 if (dwarf_cu->GetSymbolFileDWARF() != this) {
803 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
804 cu_idx);
805 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
806 // Let the debug map create the compile unit
807 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
808 dwarf_cu->SetUserData(cu_sp.get());
809 } else {
810 ModuleSP module_sp(m_obj_file->GetModule());
811 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000812 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813 if (cu_die) {
814 FileSpec cu_file_spec{cu_die.GetName(), false};
815 if (cu_file_spec) {
816 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000817 // resolve the file. This can be expensive e.g. when the source
818 // files are
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 // NFS mounted.
820 if (cu_file_spec.IsRelative()) {
821 const char *cu_comp_dir{
822 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
823 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
824 }
825
826 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000827 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 remapped_file))
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000829 cu_file_spec.SetFile(remapped_file, false,
830 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 }
832
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000833 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
835
836 bool is_optimized = dwarf_cu->GetIsOptimized();
837 cu_sp.reset(new CompileUnit(
838 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
839 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
840 if (cu_sp) {
841 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000842 // try and get the first entry in the supports files from the
843 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 if (!cu_file_spec) {
845 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
846 if (cu_file_spec) {
847 (FileSpec &)(*cu_sp) = cu_file_spec;
848 // Also fix the invalid file spec which was copied from the
849 // compile unit.
850 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
851 }
852 }
853
854 dwarf_cu->SetUserData(cu_sp.get());
855
856 // Figure out the compile unit index if we weren't given one
857 if (cu_idx == UINT32_MAX)
858 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
859
860 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
861 cu_idx, cu_sp);
862 }
863 }
864 }
865 }
866 }
867 }
868 return cu_sp;
869}
870
871uint32_t SymbolFileDWARF::GetNumCompileUnits() {
872 DWARFDebugInfo *info = DebugInfo();
873 if (info)
874 return info->GetNumCompileUnits();
875 return 0;
876}
877
878CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000879 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 CompUnitSP cu_sp;
881 DWARFDebugInfo *info = DebugInfo();
882 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000883 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 if (dwarf_cu)
885 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
886 }
887 return cu_sp;
888}
889
890Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
891 const DWARFDIE &die) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000892 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 if (die.IsValid()) {
894 TypeSystem *type_system =
895 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
896
897 if (type_system) {
898 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
899 if (dwarf_ast)
900 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
901 }
902 }
903 return nullptr;
904}
905
906bool SymbolFileDWARF::FixupAddress(Address &addr) {
907 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
908 if (debug_map_symfile) {
909 return debug_map_symfile->LinkOSOAddress(addr);
910 }
911 // This is a normal DWARF file, no address fixups need to happen
912 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000913}
Greg Clayton1f746072012-08-29 21:13:06 +0000914lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000916 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000918 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 if (dwarf_cu)
920 return dwarf_cu->GetLanguageType();
921 else
922 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000923}
924
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000926 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 assert(sc.comp_unit);
928 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000929 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 if (dwarf_cu) {
931 DWARFDIECollection function_dies;
932 const size_t num_functions =
933 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
934 size_t func_idx;
935 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
936 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
937 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
938 if (ParseCompileUnitFunction(sc, die))
939 ++functions_added;
940 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942 // FixupTypes();
943 }
944 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945}
946
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
948 const SymbolContext &sc, FileSpecList &support_files) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000949 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000951 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000953 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 if (cu_die) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000956 FileSpec cu_comp_dir = resolveCompDir(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
958 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
959 DW_AT_stmt_list, DW_INVALID_OFFSET);
960 if (stmt_list != DW_INVALID_OFFSET) {
961 // All file indexes in DWARF are one based and a file of index zero is
962 // supposed to be the compile unit itself.
963 support_files.Append(*sc.comp_unit);
964 return DWARFDebugLine::ParseSupportFiles(
965 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
George Rimarc6c7bfc2018-09-13 17:06:47 +0000966 stmt_list, support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 }
970 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971}
972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
974 const lldb_private::SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000975 ASSERT_MODULE_LOCK(this);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000976 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 if (dwarf_cu)
978 return dwarf_cu->GetIsOptimized();
979 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000980}
981
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982bool SymbolFileDWARF::ParseImportedModules(
983 const lldb_private::SymbolContext &sc,
984 std::vector<lldb_private::ConstString> &imported_modules) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000985 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000987 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 if (dwarf_cu) {
989 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
990 sc.comp_unit->GetLanguage())) {
991 UpdateExternalModuleListIfNeeded();
992
993 if (sc.comp_unit) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000994 const DWARFDIE die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995
996 if (die) {
997 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
998 child_die = child_die.GetSibling()) {
999 if (child_die.Tag() == DW_TAG_imported_declaration) {
1000 if (DWARFDIE module_die =
1001 child_die.GetReferencedDIE(DW_AT_import)) {
1002 if (module_die.Tag() == DW_TAG_module) {
1003 if (const char *name = module_die.GetAttributeValueAsString(
1004 DW_AT_name, nullptr)) {
1005 ConstString const_name(name);
1006 imported_modules.push_back(const_name);
1007 }
Sean Callananb0300a42016-01-14 21:46:09 +00001008 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 }
Sean Callananb0300a42016-01-14 21:46:09 +00001010 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001012 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 } else {
1014 for (const auto &pair : m_external_type_modules) {
1015 imported_modules.push_back(pair.first);
1016 }
1017 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001018 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 }
1020 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001021}
1022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023struct ParseDWARFLineTableCallbackInfo {
1024 LineTable *line_table;
1025 std::unique_ptr<LineSequence> sequence_ap;
1026 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027};
1028
1029//----------------------------------------------------------------------
1030// ParseStatementTableCallback
1031//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032static void ParseDWARFLineTableCallback(dw_offset_t offset,
1033 const DWARFDebugLine::State &state,
1034 void *userData) {
1035 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1036 // Just started parsing the line table
1037 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1038 // Done parsing line table, nothing to do for the cleanup
1039 } else {
1040 ParseDWARFLineTableCallbackInfo *info =
1041 (ParseDWARFLineTableCallbackInfo *)userData;
1042 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043
Adrian Prantl05097242018-04-30 16:49:04 +00001044 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 if (!info->sequence_ap.get()) {
1046 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1047 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001048 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 line_table->AppendLineEntryToSequence(
1050 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1051 state.column, state.file, state.is_stmt, state.basic_block,
1052 state.prologue_end, state.epilogue_begin, state.end_sequence);
1053 if (state.end_sequence) {
1054 // First, put the current sequence into the line table.
1055 line_table->InsertSequence(info->sequence_ap.get());
1056 // Then, empty it to prepare for the next sequence.
1057 info->sequence_ap->Clear();
1058 }
1059 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060}
1061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001063 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 assert(sc.comp_unit);
1065 if (sc.comp_unit->GetLineTable() != NULL)
1066 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001068 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +00001070 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 if (dwarf_cu_die) {
1072 const dw_offset_t cu_line_offset =
1073 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1074 DW_INVALID_OFFSET);
1075 if (cu_line_offset != DW_INVALID_OFFSET) {
1076 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1077 if (line_table_ap.get()) {
1078 ParseDWARFLineTableCallbackInfo info;
1079 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001080
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 /*
1082 * MIPS:
1083 * The SymbolContext may not have a valid target, thus we may not be
1084 * able
1085 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1086 * #0
1087 * for MIPS. Use ArchSpec to clear the bit #0.
1088 */
1089 ArchSpec arch;
1090 GetObjectFile()->GetArchitecture(arch);
1091 switch (arch.GetMachine()) {
1092 case llvm::Triple::mips:
1093 case llvm::Triple::mipsel:
1094 case llvm::Triple::mips64:
1095 case llvm::Triple::mips64el:
1096 info.addr_mask = ~((lldb::addr_t)1);
1097 break;
1098 default:
1099 info.addr_mask = ~((lldb::addr_t)0);
1100 break;
1101 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001102
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 lldb::offset_t offset = cu_line_offset;
1104 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1105 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001106 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1108 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001109 // We have an object file that has a line table with addresses that
1110 // are not linked. We need to link the line table and convert the
1111 // addresses that are relative to the .o file into addresses for
1112 // the main executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113 sc.comp_unit->SetLineTable(
1114 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1115 } else {
1116 sc.comp_unit->SetLineTable(line_table_ap.release());
1117 return true;
1118 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 }
1123 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124}
1125
Siva Chandrad8335e92015-12-16 00:22:08 +00001126lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1128 auto iter = m_debug_macros_map.find(*offset);
1129 if (iter != m_debug_macros_map.end())
1130 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1133 if (debug_macro_data.GetByteSize() == 0)
1134 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1137 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001138
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 const DWARFDebugMacroHeader &header =
1140 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1141 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1142 header.OffsetIs64Bit(), offset, this,
1143 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001146}
1147
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001149 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001151
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001152 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001154 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155
Jan Kratochvil93afb052018-05-24 20:51:13 +00001156 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 if (!dwarf_cu_die)
1158 return false;
1159
1160 lldb::offset_t sect_offset =
1161 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1162 if (sect_offset == DW_INVALID_OFFSET)
1163 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1164 DW_INVALID_OFFSET);
1165 if (sect_offset == DW_INVALID_OFFSET)
1166 return false;
1167
1168 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1169
1170 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001171}
1172
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1174 Block *parent_block,
1175 const DWARFDIE &orig_die,
1176 addr_t subprogram_low_pc,
1177 uint32_t depth) {
1178 size_t blocks_added = 0;
1179 DWARFDIE die = orig_die;
1180 while (die) {
1181 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 switch (tag) {
1184 case DW_TAG_inlined_subroutine:
1185 case DW_TAG_subprogram:
1186 case DW_TAG_lexical_block: {
1187 Block *block = NULL;
1188 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001189 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1190 // inlined functions. These will be parsed on their own as separate
1191 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192
1193 if (depth > 0)
1194 break;
1195
1196 block = parent_block;
1197 } else {
1198 BlockSP block_sp(new Block(die.GetID()));
1199 parent_block->AddChild(block_sp);
1200 block = block_sp.get();
1201 }
1202 DWARFRangeList ranges;
1203 const char *name = NULL;
1204 const char *mangled_name = NULL;
1205
1206 int decl_file = 0;
1207 int decl_line = 0;
1208 int decl_column = 0;
1209 int call_file = 0;
1210 int call_line = 0;
1211 int call_column = 0;
1212 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1213 decl_line, decl_column, call_file, call_line,
1214 call_column, nullptr)) {
1215 if (tag == DW_TAG_subprogram) {
1216 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1217 subprogram_low_pc = ranges.GetMinRangeBase(0);
1218 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001219 // We get called here for inlined subroutines in two ways. The first
1220 // time is when we are making the Function object for this inlined
1221 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001223 // need to adjust the containing address. The second time is when we
1224 // are parsing the blocks inside the function that contains the
1225 // inlined concrete instance. Since these will be blocks inside the
1226 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001227 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1228 subprogram_low_pc = ranges.GetMinRangeBase(0);
1229 }
1230 }
1231
1232 const size_t num_ranges = ranges.GetSize();
1233 for (size_t i = 0; i < num_ranges; ++i) {
1234 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1235 const addr_t range_base = range.GetRangeBase();
1236 if (range_base >= subprogram_low_pc)
1237 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1238 range.GetByteSize()));
1239 else {
1240 GetObjectFile()->GetModule()->ReportError(
1241 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1242 ") which has a base that is less than the function's low PC "
1243 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1244 "start of this error message",
1245 block->GetID(), range_base, range.GetRangeEnd(),
1246 subprogram_low_pc);
1247 }
1248 }
1249 block->FinalizeRanges();
1250
1251 if (tag != DW_TAG_subprogram &&
1252 (name != NULL || mangled_name != NULL)) {
1253 std::unique_ptr<Declaration> decl_ap;
1254 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1255 decl_ap.reset(new Declaration(
1256 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1257 decl_line, decl_column));
1258
1259 std::unique_ptr<Declaration> call_ap;
1260 if (call_file != 0 || call_line != 0 || call_column != 0)
1261 call_ap.reset(new Declaration(
1262 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1263 call_line, call_column));
1264
1265 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1266 call_ap.get());
1267 }
1268
1269 ++blocks_added;
1270
1271 if (die.HasChildren()) {
1272 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1273 subprogram_low_pc, depth + 1);
1274 }
1275 }
1276 } break;
1277 default:
1278 break;
1279 }
1280
Adrian Prantl05097242018-04-30 16:49:04 +00001281 // Only parse siblings of the block if we are not at depth zero. A depth of
1282 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1283 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284
1285 if (depth == 0)
1286 die.Clear();
1287 else
1288 die = die.GetSibling();
1289 }
1290 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001291}
1292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1294 if (parent_die) {
1295 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1296 die = die.GetSibling()) {
1297 dw_tag_t tag = die.Tag();
1298 bool check_virtuality = false;
1299 switch (tag) {
1300 case DW_TAG_inheritance:
1301 case DW_TAG_subprogram:
1302 check_virtuality = true;
1303 break;
1304 default:
1305 break;
1306 }
1307 if (check_virtuality) {
1308 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1309 return true;
1310 }
1311 }
1312 }
1313 return false;
1314}
1315
1316void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1317 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1318 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1319 std::vector<DWARFDIE> decl_ctx_die_list =
1320 ast_parser->GetDIEForDeclContext(decl_ctx);
1321
1322 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1323 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1324 decl = decl.GetSibling())
1325 ast_parser->GetDeclForUIDFromDWARF(decl);
1326}
1327
1328SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001329 // This method can be called without going through the symbol vendor so we
1330 // need to lock the module.
1331 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001332 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1333 // must make sure we use the correct DWARF file when resolving things. On
1334 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1335 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1336 // references to other DWARF objects and we must be ready to receive a
1337 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 // instance.
1339 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1340 if (debug_map)
1341 return debug_map->GetSymbolFileByOSOIndex(
1342 debug_map->GetOSOIndexFromUserID(uid));
1343 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001344}
1345
1346DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001348 // This method can be called without going through the symbol vendor so we
1349 // need to lock the module.
1350 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001351 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1352 // must make sure we use the correct DWARF file when resolving things. On
1353 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1354 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1355 // references to other DWARF objects and we must be ready to receive a
1356 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 // instance.
1358 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1359 if (dwarf)
1360 return dwarf->GetDIE(DIERef(uid, dwarf));
1361 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001362}
1363
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001365 // This method can be called without going through the symbol vendor so we
1366 // need to lock the module.
1367 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001368 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1369 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1370 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 DWARFDIE die = GetDIEFromUID(type_uid);
1372 if (die)
1373 return die.GetDecl();
1374 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001375}
1376
Greg Clayton99558cc42015-08-24 23:46:31 +00001377CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001379 // This method can be called without going through the symbol vendor so we
1380 // need to lock the module.
1381 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001382 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1383 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1384 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 DWARFDIE die = GetDIEFromUID(type_uid);
1386 if (die)
1387 return die.GetDeclContext();
1388 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001389}
1390
Greg Clayton99558cc42015-08-24 23:46:31 +00001391CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001393 // This method can be called without going through the symbol vendor so we
1394 // need to lock the module.
1395 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001396 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1397 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1398 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 DWARFDIE die = GetDIEFromUID(type_uid);
1400 if (die)
1401 return die.GetContainingDeclContext();
1402 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403}
1404
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001406 // This method can be called without going through the symbol vendor so we
1407 // need to lock the module.
1408 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Adrian Prantl05097242018-04-30 16:49:04 +00001409 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1410 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1411 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 DWARFDIE type_die = GetDIEFromUID(type_uid);
1413 if (type_die)
1414 return type_die.ResolveType();
1415 else
1416 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417}
1418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1420 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001421}
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1424 bool assert_not_being_parsed) {
1425 if (die) {
1426 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1427 if (log)
1428 GetObjectFile()->GetModule()->LogMessage(
1429 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1430 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001431
Adrian Prantl05097242018-04-30 16:49:04 +00001432 // We might be coming in in the middle of a type tree (a class within a
1433 // class, an enum within a class), so parse any needed parent DIEs before
1434 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1436 if (decl_ctx_die) {
1437 if (log) {
1438 switch (decl_ctx_die.Tag()) {
1439 case DW_TAG_structure_type:
1440 case DW_TAG_union_type:
1441 case DW_TAG_class_type: {
1442 // Get the type, which could be a forward declaration
1443 if (log)
1444 GetObjectFile()->GetModule()->LogMessage(
1445 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1446 "resolve parent forward type for 0x%8.8x",
1447 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1448 decl_ctx_die.GetOffset());
1449 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001450
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 default:
1452 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001453 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001455 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 return ResolveType(die);
1457 }
1458 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001459}
1460
Greg Clayton6beaaa62011-01-17 03:46:26 +00001461// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001462// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1463// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464bool SymbolFileDWARF::HasForwardDeclForClangType(
1465 const CompilerType &compiler_type) {
1466 CompilerType compiler_type_no_qualifiers =
1467 ClangUtil::RemoveFastQualifiers(compiler_type);
1468 if (GetForwardDeclClangTypeToDie().count(
1469 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1470 return true;
1471 }
1472 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001473
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 ClangASTContext *clang_type_system =
1475 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1476 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001477 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001478 DWARFASTParserClang *ast_parser =
1479 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1480 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001481}
1482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00001484 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 ClangASTContext *clang_type_system =
1487 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1488 if (clang_type_system) {
1489 DWARFASTParserClang *ast_parser =
1490 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1491 if (ast_parser &&
1492 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1493 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1494 }
Jim Inghamc3549282012-01-11 02:21:12 +00001495
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 // We have a struct/union/class/enum that needs to be fully resolved.
1497 CompilerType compiler_type_no_qualifiers =
1498 ClangUtil::RemoveFastQualifiers(compiler_type);
1499 auto die_it = GetForwardDeclClangTypeToDie().find(
1500 compiler_type_no_qualifiers.GetOpaqueQualType());
1501 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1502 // We have already resolved this type...
1503 return true;
1504 }
1505
1506 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1507 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001508 // Once we start resolving this type, remove it from the forward
1509 // declaration map in case anyone child members or other types require this
1510 // type to get resolved. The type will get resolved when all of the calls
1511 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 GetForwardDeclClangTypeToDie().erase(die_it);
1513
1514 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1515
1516 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1517 DWARF_LOG_TYPE_COMPLETION));
1518 if (log)
1519 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1520 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1521 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1522 type->GetName().AsCString());
1523 assert(compiler_type);
1524 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1525 if (dwarf_ast)
1526 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1527 }
1528 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001529}
1530
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1532 bool assert_not_being_parsed,
1533 bool resolve_function_context) {
1534 if (die) {
1535 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1536
1537 if (assert_not_being_parsed) {
1538 if (type != DIE_IS_BEING_PARSED)
1539 return type;
1540
1541 GetObjectFile()->GetModule()->ReportError(
1542 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1543 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1544
1545 } else
1546 return type;
1547 }
1548 return nullptr;
1549}
1550
1551CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001552SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 uint32_t cu_idx) {
1554 // Check if the symbol vendor already knows about this compile unit?
1555 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001556 // The symbol vendor doesn't know about this compile unit, we need to parse
1557 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1559 }
1560 return (CompileUnit *)dwarf_cu->GetUserData();
1561}
1562
1563size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1564 DIEArray &method_die_offsets) {
1565 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001566 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 return method_die_offsets.size();
1568}
1569
1570bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1571 sc.Clear(false);
1572
1573 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1576
1577 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1578 if (sc.function == NULL)
1579 sc.function = ParseCompileUnitFunction(sc, die);
1580
1581 if (sc.function) {
1582 sc.module_sp = sc.function->CalculateSymbolContextModule();
1583 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 }
1586
1587 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588}
1589
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1591 UpdateExternalModuleListIfNeeded();
1592 const auto &pos = m_external_type_modules.find(name);
1593 if (pos != m_external_type_modules.end())
1594 return pos->second;
1595 else
1596 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001597}
1598
Greg Clayton2f869fe2016-03-30 20:14:35 +00001599DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1601 DWARFDebugInfo *debug_info = DebugInfo();
1602 if (debug_info)
1603 return debug_info->GetDIE(die_ref);
1604 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001605 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001606}
1607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608std::unique_ptr<SymbolFileDWARFDwo>
1609SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001610 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001612 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 // info files for modules.
1614 if (GetDebugMapSymfile())
1615 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001616
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 const char *dwo_name = cu_die.GetAttributeValueAsString(
1618 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1619 if (!dwo_name)
1620 return nullptr;
1621
Tamas Berghammer963ce482017-08-25 13:56:14 +00001622 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1623 if (dwp_symfile) {
1624 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1625 DW_AT_GNU_dwo_id, 0);
1626 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1627 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1628 if (dwo_symfile)
1629 return dwo_symfile;
1630 }
1631
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 FileSpec dwo_file(dwo_name, true);
1633 if (dwo_file.IsRelative()) {
1634 const char *comp_dir = cu_die.GetAttributeValueAsString(
1635 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1636 if (!comp_dir)
1637 return nullptr;
1638
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001639 dwo_file.SetFile(comp_dir, true, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 dwo_file.AppendPathComponent(dwo_name);
1641 }
1642
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00001643 if (!FileSystem::Instance().Exists(dwo_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 return nullptr;
1645
1646 const lldb::offset_t file_offset = 0;
1647 DataBufferSP dwo_file_data_sp;
1648 lldb::offset_t dwo_file_data_offset = 0;
1649 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1650 GetObjectFile()->GetModule(), &dwo_file, file_offset,
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +00001651 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1652 dwo_file_data_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 if (dwo_obj_file == nullptr)
1654 return nullptr;
1655
1656 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001657}
1658
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1660 if (m_fetched_external_modules)
1661 return;
1662 m_fetched_external_modules = true;
1663
1664 DWARFDebugInfo *debug_info = DebugInfo();
1665
1666 const uint32_t num_compile_units = GetNumCompileUnits();
1667 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001668 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669
Jan Kratochvil93afb052018-05-24 20:51:13 +00001670 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 if (die && die.HasChildren() == false) {
1672 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1673
1674 if (name) {
1675 ConstString const_name(name);
1676 if (m_external_type_modules.find(const_name) ==
1677 m_external_type_modules.end()) {
1678 ModuleSP module_sp;
1679 const char *dwo_path =
1680 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1681 if (dwo_path) {
1682 ModuleSpec dwo_module_spec;
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001683 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false,
1684 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1686 const char *comp_dir =
1687 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1688 if (comp_dir) {
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001689 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true,
1690 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1692 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694 dwo_module_spec.GetArchitecture() =
1695 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001696
Adrian Prantl05097242018-04-30 16:49:04 +00001697 // When LLDB loads "external" modules it looks at the presence of
1698 // DW_AT_GNU_dwo_name. However, when the already created module
1699 // (corresponding to .dwo itself) is being processed, it will see
1700 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1701 // dwo file) and will try to call ModuleList::GetSharedModule
1702 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1703 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1704 // DW_AT_comp_dir. In this case the method
1705 // ModuleList::GetSharedModule will fail and the warning will be
1706 // printed. However, as one can notice in this case we don't
1707 // actually need to try to load the already loaded module
1708 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001709 if (m_obj_file->GetFileSpec()
1710 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001711 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001712 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1713 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1714 continue;
1715 }
1716
Zachary Turner97206d52017-05-12 04:51:55 +00001717 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 dwo_module_spec, module_sp, NULL, NULL, NULL);
1719 if (!module_sp) {
1720 GetObjectFile()->GetModule()->ReportWarning(
1721 "0x%8.8x: unable to locate module needed for external types: "
1722 "%s\nerror: %s\nDebugging will be degraded due to missing "
1723 "types. Rebuilding your project will regenerate the needed "
1724 "module files.",
1725 die.GetOffset(),
1726 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1727 error.AsCString("unknown error"));
1728 }
1729 }
1730 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001731 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 }
1735}
1736
1737SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1738 if (!m_global_aranges_ap) {
1739 m_global_aranges_ap.reset(new GlobalVariableMap());
1740
1741 ModuleSP module_sp = GetObjectFile()->GetModule();
1742 if (module_sp) {
1743 const size_t num_cus = module_sp->GetNumCompileUnits();
1744 for (size_t i = 0; i < num_cus; ++i) {
1745 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1746 if (cu_sp) {
1747 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1748 if (globals_sp) {
1749 const size_t num_globals = globals_sp->GetSize();
1750 for (size_t g = 0; g < num_globals; ++g) {
1751 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1752 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1753 const DWARFExpression &location = var_sp->LocationExpression();
1754 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001755 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001756 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1757 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 if (location_result.GetValueType() ==
1759 Value::eValueTypeFileAddress) {
1760 lldb::addr_t file_addr =
1761 location_result.GetScalar().ULongLong();
1762 lldb::addr_t byte_size = 1;
1763 if (var_sp->GetType())
1764 byte_size = var_sp->GetType()->GetByteSize();
1765 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1766 file_addr, byte_size, var_sp.get()));
1767 }
1768 }
1769 }
1770 }
1771 }
1772 }
1773 }
1774 }
1775 m_global_aranges_ap->Sort();
1776 }
1777 return *m_global_aranges_ap;
1778}
1779
1780uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +00001781 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001783 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1784 Timer scoped_timer(func_cat,
1785 "SymbolFileDWARF::"
1786 "ResolveSymbolContext (so_addr = { "
1787 "section = %p, offset = 0x%" PRIx64
1788 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 static_cast<void *>(so_addr.GetSection().get()),
1790 so_addr.GetOffset(), resolve_scope);
1791 uint32_t resolved = 0;
1792 if (resolve_scope &
1793 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1794 eSymbolContextLineEntry | eSymbolContextVariable)) {
1795 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1796
1797 DWARFDebugInfo *debug_info = DebugInfo();
1798 if (debug_info) {
1799 const dw_offset_t cu_offset =
1800 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1801 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001802 // Global variables are not in the compile unit address ranges. The
1803 // only way to currently find global variables is to iterate over the
1804 // .debug_pubnames or the __apple_names table and find all items in
1805 // there that point to DW_TAG_variable DIEs and then find the address
1806 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 if (resolve_scope & eSymbolContextVariable) {
1808 GlobalVariableMap &map = GetGlobalAranges();
1809 const GlobalVariableMap::Entry *entry =
1810 map.FindEntryThatContains(file_vm_addr);
1811 if (entry && entry->data) {
1812 Variable *variable = entry->data;
1813 SymbolContextScope *scc = variable->GetSymbolContextScope();
1814 if (scc) {
1815 scc->CalculateSymbolContext(&sc);
1816 sc.variable = variable;
1817 }
1818 return sc.GetResolvedMask();
1819 }
1820 }
1821 } else {
1822 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001823 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1825 if (dwarf_cu) {
1826 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1827 if (sc.comp_unit) {
1828 resolved |= eSymbolContextCompUnit;
1829
1830 bool force_check_line_table = false;
1831 if (resolve_scope &
1832 (eSymbolContextFunction | eSymbolContextBlock)) {
1833 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1834 DWARFDIE block_die;
1835 if (function_die) {
1836 sc.function =
1837 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1838 if (sc.function == NULL)
1839 sc.function = ParseCompileUnitFunction(sc, function_die);
1840
1841 if (sc.function && (resolve_scope & eSymbolContextBlock))
1842 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1843 } else {
1844 // We might have had a compile unit that had discontiguous
1845 // address ranges where the gaps are symbols that don't have
1846 // any debug info. Discontiguous compile unit address ranges
1847 // should only happen when there aren't other functions from
1848 // other compile units in these gaps. This helps keep the size
1849 // of the aranges down.
1850 force_check_line_table = true;
1851 }
1852
1853 if (sc.function != NULL) {
1854 resolved |= eSymbolContextFunction;
1855
1856 if (resolve_scope & eSymbolContextBlock) {
1857 Block &block = sc.function->GetBlock(true);
1858
1859 if (block_die)
1860 sc.block = block.FindBlockByID(block_die.GetID());
1861 else
1862 sc.block = block.FindBlockByID(function_die.GetID());
1863 if (sc.block)
1864 resolved |= eSymbolContextBlock;
1865 }
1866 }
1867 }
1868
1869 if ((resolve_scope & eSymbolContextLineEntry) ||
1870 force_check_line_table) {
1871 LineTable *line_table = sc.comp_unit->GetLineTable();
1872 if (line_table != NULL) {
1873 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001874 // terms of this debug file if there is no debug map, or it
1875 // will be an address in the .o file which needs to be fixed up
1876 // to be in terms of the debug map executable. Either way,
1877 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 Address exe_so_addr(so_addr);
1879 if (FixupAddress(exe_so_addr)) {
1880 if (line_table->FindLineEntryByAddress(exe_so_addr,
1881 sc.line_entry)) {
1882 resolved |= eSymbolContextLineEntry;
1883 }
1884 }
1885 }
1886 }
1887
1888 if (force_check_line_table &&
1889 !(resolved & eSymbolContextLineEntry)) {
1890 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001891 // address ranges where the gaps are symbols that don't have any
1892 // debug info. Discontiguous compile unit address ranges should
1893 // only happen when there aren't other functions from other
1894 // compile units in these gaps. This helps keep the size of the
1895 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896 sc.comp_unit = NULL;
1897 resolved &= ~eSymbolContextCompUnit;
1898 }
1899 } else {
1900 GetObjectFile()->GetModule()->ReportWarning(
1901 "0x%8.8x: compile unit %u failed to create a valid "
1902 "lldb_private::CompileUnit class.",
1903 cu_offset, cu_idx);
1904 }
1905 }
1906 }
1907 }
1908 }
1909 return resolved;
1910}
1911
1912uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1913 uint32_t line,
1914 bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +00001915 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001916 SymbolContextList &sc_list) {
1917 const uint32_t prev_size = sc_list.GetSize();
1918 if (resolve_scope & eSymbolContextCompUnit) {
1919 DWARFDebugInfo *debug_info = DebugInfo();
1920 if (debug_info) {
1921 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001922 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923
1924 for (cu_idx = 0;
1925 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1926 ++cu_idx) {
1927 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1928 const bool full_match = (bool)file_spec.GetDirectory();
1929 bool file_spec_matches_cu_file_spec =
1930 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1931 if (check_inlines || file_spec_matches_cu_file_spec) {
1932 SymbolContext sc(m_obj_file->GetModule());
1933 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1934 if (sc.comp_unit) {
1935 uint32_t file_idx = UINT32_MAX;
1936
Adrian Prantl05097242018-04-30 16:49:04 +00001937 // If we are looking for inline functions only and we don't find it
1938 // in the support files, we are done.
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 if (file_idx == UINT32_MAX)
1943 continue;
1944 }
1945
1946 if (line != 0) {
1947 LineTable *line_table = sc.comp_unit->GetLineTable();
1948
1949 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001950 // We will have already looked up the file index if we are
1951 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 if (!check_inlines)
1953 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1954 1, file_spec, true);
1955
1956 if (file_idx != UINT32_MAX) {
1957 uint32_t found_line;
1958 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1959 0, file_idx, line, false, &sc.line_entry);
1960 found_line = sc.line_entry.line;
1961
1962 while (line_idx != UINT32_MAX) {
1963 sc.function = NULL;
1964 sc.block = NULL;
1965 if (resolve_scope &
1966 (eSymbolContextFunction | eSymbolContextBlock)) {
1967 const lldb::addr_t file_vm_addr =
1968 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1969 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1970 DWARFDIE function_die =
1971 dwarf_cu->LookupAddress(file_vm_addr);
1972 DWARFDIE block_die;
1973 if (function_die) {
1974 sc.function =
1975 sc.comp_unit
1976 ->FindFunctionByUID(function_die.GetID())
1977 .get();
1978 if (sc.function == NULL)
1979 sc.function =
1980 ParseCompileUnitFunction(sc, function_die);
1981
1982 if (sc.function &&
1983 (resolve_scope & eSymbolContextBlock))
1984 block_die =
1985 function_die.LookupDeepestBlock(file_vm_addr);
1986 }
1987
1988 if (sc.function != NULL) {
1989 Block &block = sc.function->GetBlock(true);
1990
1991 if (block_die)
1992 sc.block = block.FindBlockByID(block_die.GetID());
1993 else if (function_die)
1994 sc.block =
1995 block.FindBlockByID(function_die.GetID());
1996 }
1997 }
1998 }
1999
2000 sc_list.Append(sc);
2001 line_idx = line_table->FindLineEntryIndexByFileIndex(
2002 line_idx + 1, file_idx, found_line, true,
2003 &sc.line_entry);
2004 }
2005 }
2006 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2007 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00002008 // sites by file and line and if the file spec matches that of
2009 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00002010 sc_list.Append(sc);
2011 }
2012 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2013 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00002014 // sites by file and line and if the file spec matches that of
2015 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00002016 sc_list.Append(sc);
2017 }
2018
2019 if (!check_inlines)
2020 break;
2021 }
2022 }
2023 }
2024 }
2025 }
2026 return sc_list.GetSize() - prev_size;
2027}
2028
Jim Ingham7fca8c02017-04-28 00:51:06 +00002029void SymbolFileDWARF::PreloadSymbols() {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002030 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00002031 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002032}
2033
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00002034std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
2035 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
2036 if (module_sp)
2037 return module_sp->GetMutex();
2038 return GetObjectFile()->GetModule()->GetMutex();
2039}
2040
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2042 const lldb_private::CompilerDeclContext *decl_ctx) {
2043 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00002044 // Invalid namespace decl which means we aren't matching only things in
2045 // this symbol file, so return true to indicate it matches this symbol
2046 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002047 return true;
2048 }
2049
2050 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2051 TypeSystem *type_system = GetTypeSystemForLanguage(
2052 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2053 if (decl_ctx_type_system == type_system)
2054 return true; // The type systems match, return true
2055
2056 // The namespace AST was valid, and it does not match...
2057 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2058
2059 if (log)
2060 GetObjectFile()->GetModule()->LogMessage(
2061 log, "Valid namespace does not match symbol file");
2062
2063 return false;
2064}
2065
2066uint32_t SymbolFileDWARF::FindGlobalVariables(
2067 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002068 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002069 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2070
2071 if (log)
2072 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002073 log,
2074 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2075 "parent_decl_ctx=%p, max_matches=%u, variables)",
2076 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002077 max_matches);
2078
2079 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2080 return 0;
2081
2082 DWARFDebugInfo *info = DebugInfo();
2083 if (info == NULL)
2084 return 0;
2085
Pavel Labath34cda142018-05-31 09:46:26 +00002086 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002087 const uint32_t original_size = variables.GetSize();
2088
Pavel Labathe1d18752018-06-07 10:04:44 +00002089 llvm::StringRef basename;
2090 llvm::StringRef context;
2091
2092 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2093 context, basename))
2094 basename = name.GetStringRef();
2095
Kate Stoneb9c1b512016-09-06 20:57:50 +00002096 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002097 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 const size_t num_die_matches = die_offsets.size();
2099 if (num_die_matches) {
2100 SymbolContext sc;
2101 sc.module_sp = m_obj_file->GetModule();
2102 assert(sc.module_sp);
2103
Pavel Labathe1d18752018-06-07 10:04:44 +00002104 // Loop invariant: Variables up to this index have been checked for context
2105 // matches.
2106 uint32_t pruned_idx = original_size;
2107
Kate Stoneb9c1b512016-09-06 20:57:50 +00002108 bool done = false;
2109 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2110 const DIERef &die_ref = die_offsets[i];
2111 DWARFDIE die = GetDIE(die_ref);
2112
2113 if (die) {
2114 switch (die.Tag()) {
2115 default:
2116 case DW_TAG_subprogram:
2117 case DW_TAG_inlined_subroutine:
2118 case DW_TAG_try_block:
2119 case DW_TAG_catch_block:
2120 break;
2121
2122 case DW_TAG_variable: {
2123 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2124
2125 if (parent_decl_ctx) {
2126 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2127 if (dwarf_ast) {
2128 CompilerDeclContext actual_parent_decl_ctx =
2129 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2130 if (!actual_parent_decl_ctx ||
2131 actual_parent_decl_ctx != *parent_decl_ctx)
2132 continue;
2133 }
2134 }
2135
2136 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2137 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002138 while (pruned_idx < variables.GetSize()) {
2139 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2140 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2141 ++pruned_idx;
2142 else
2143 variables.RemoveVariableAtIndex(pruned_idx);
2144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145
2146 if (variables.GetSize() - original_size >= max_matches)
2147 done = true;
2148 } break;
2149 }
2150 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002151 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2152 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 }
2154 }
2155 }
2156
2157 // Return the number of variable that were appended to the list
2158 const uint32_t num_matches = variables.GetSize() - original_size;
2159 if (log && num_matches > 0) {
2160 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002161 log,
2162 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2163 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2164 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165 max_matches, num_matches);
2166 }
2167 return num_matches;
2168}
2169
2170uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002171 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002172 VariableList &variables) {
2173 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2174
2175 if (log) {
2176 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002177 log,
2178 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2179 "max_matches=%u, variables)",
2180 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181 }
2182
2183 DWARFDebugInfo *info = DebugInfo();
2184 if (info == NULL)
2185 return 0;
2186
Pavel Labath34cda142018-05-31 09:46:26 +00002187 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 const uint32_t original_size = variables.GetSize();
2189
2190 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002191 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002192
2193 SymbolContext sc;
2194 sc.module_sp = m_obj_file->GetModule();
2195 assert(sc.module_sp);
2196
2197 const size_t num_matches = die_offsets.size();
2198 if (num_matches) {
2199 for (size_t i = 0; i < num_matches; ++i) {
2200 const DIERef &die_ref = die_offsets[i];
2201 DWARFDIE die = GetDIE(die_ref);
2202
2203 if (die) {
2204 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2205
2206 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2207
2208 if (variables.GetSize() - original_size >= max_matches)
2209 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002210 } else
2211 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 }
2213 }
2214
2215 // Return the number of variable that were appended to the list
2216 return variables.GetSize() - original_size;
2217}
2218
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2220 bool include_inlines,
2221 SymbolContextList &sc_list) {
2222 SymbolContext sc;
2223
2224 if (!orig_die)
2225 return false;
2226
2227 // If we were passed a die that is not a function, just return false...
2228 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2229 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2230 return false;
2231
2232 DWARFDIE die = orig_die;
2233 DWARFDIE inlined_die;
2234 if (die.Tag() == DW_TAG_inlined_subroutine) {
2235 inlined_die = die;
2236
2237 while (1) {
2238 die = die.GetParent();
2239
2240 if (die) {
2241 if (die.Tag() == DW_TAG_subprogram)
2242 break;
2243 } else
2244 break;
2245 }
2246 }
2247 assert(die && die.Tag() == DW_TAG_subprogram);
2248 if (GetFunction(die, sc)) {
2249 Address addr;
2250 // Parse all blocks if needed
2251 if (inlined_die) {
2252 Block &function_block = sc.function->GetBlock(true);
2253 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2254 if (sc.block == NULL)
2255 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2256 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2257 addr.Clear();
2258 } else {
2259 sc.block = NULL;
2260 addr = sc.function->GetAddressRange().GetBaseAddress();
2261 }
2262
2263 if (addr.IsValid()) {
2264 sc_list.Append(sc);
2265 return true;
2266 }
2267 }
2268
2269 return false;
2270}
2271
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2273 const DWARFDIE &die) {
2274 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002275 // parent decl context isn't valid, we aren't trying to look for any
2276 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002277 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2278 return true;
2279
2280 if (die) {
2281 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2282 if (dwarf_ast) {
2283 CompilerDeclContext actual_decl_ctx =
2284 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2285 if (actual_decl_ctx)
2286 return actual_decl_ctx == *decl_ctx;
2287 }
2288 }
2289 return false;
2290}
2291
Zachary Turner117b1fa2018-10-25 20:45:40 +00002292uint32_t SymbolFileDWARF::FindFunctions(
2293 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2294 FunctionNameType name_type_mask, bool include_inlines, bool append,
2295 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002296 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2297 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002298 name.AsCString());
2299
2300 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002301 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002302 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2303
2304 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2305
2306 if (log) {
2307 GetObjectFile()->GetModule()->LogMessage(
2308 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2309 "name_type_mask=0x%x, append=%u, sc_list)",
2310 name.GetCString(), name_type_mask, append);
2311 }
2312
2313 // If we aren't appending the results to this list, then clear the list
2314 if (!append)
2315 sc_list.Clear();
2316
2317 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2318 return 0;
2319
2320 // If name is empty then we won't find anything.
2321 if (name.IsEmpty())
2322 return 0;
2323
Adrian Prantl05097242018-04-30 16:49:04 +00002324 // Remember how many sc_list are in the list before we search in case we are
2325 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326
Kate Stoneb9c1b512016-09-06 20:57:50 +00002327 const uint32_t original_size = sc_list.GetSize();
2328
2329 DWARFDebugInfo *info = DebugInfo();
2330 if (info == NULL)
2331 return 0;
2332
Pavel Labath5af11ab2018-06-05 10:33:56 +00002333 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2334 DIEArray offsets;
2335 CompilerDeclContext empty_decl_ctx;
2336 if (!parent_decl_ctx)
2337 parent_decl_ctx = &empty_decl_ctx;
2338
2339 std::vector<DWARFDIE> dies;
2340 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2341 for (const DWARFDIE &die: dies) {
2342 if (resolved_dies.insert(die.GetDIE()).second)
2343 ResolveFunction(die, include_inlines, sc_list);
2344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345
2346 // Return the number of variable that were appended to the list
2347 const uint32_t num_matches = sc_list.GetSize() - original_size;
2348
2349 if (log && num_matches > 0) {
2350 GetObjectFile()->GetModule()->LogMessage(
2351 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2352 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2353 "%u",
2354 name.GetCString(), name_type_mask, include_inlines, append,
2355 num_matches);
2356 }
2357 return num_matches;
2358}
2359
2360uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2361 bool include_inlines, bool append,
2362 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002363 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2364 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002365 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366
2367 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2368
2369 if (log) {
2370 GetObjectFile()->GetModule()->LogMessage(
2371 log,
2372 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002373 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002374 }
2375
2376 // If we aren't appending the results to this list, then clear the list
2377 if (!append)
2378 sc_list.Clear();
2379
Pavel Labathb13f0332018-05-21 14:12:52 +00002380 DWARFDebugInfo *info = DebugInfo();
2381 if (!info)
2382 return 0;
2383
Adrian Prantl05097242018-04-30 16:49:04 +00002384 // Remember how many sc_list are in the list before we search in case we are
2385 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386 uint32_t original_size = sc_list.GetSize();
2387
Pavel Labath6de9c792018-06-05 12:13:22 +00002388 DIEArray offsets;
2389 m_index->GetFunctions(regex, offsets);
2390
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002391 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2392 for (DIERef ref : offsets) {
2393 DWARFDIE die = info->GetDIE(ref);
2394 if (!die) {
2395 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2396 continue;
2397 }
2398 if (resolved_dies.insert(die.GetDIE()).second)
2399 ResolveFunction(die, include_inlines, sc_list);
2400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002401
2402 // Return the number of variable that were appended to the list
2403 return sc_list.GetSize() - original_size;
2404}
2405
2406void SymbolFileDWARF::GetMangledNamesForFunction(
2407 const std::string &scope_qualified_name,
2408 std::vector<ConstString> &mangled_names) {
2409 DWARFDebugInfo *info = DebugInfo();
2410 uint32_t num_comp_units = 0;
2411 if (info)
2412 num_comp_units = info->GetNumCompileUnits();
2413
2414 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002415 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002416 if (cu == nullptr)
2417 continue;
2418
2419 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2420 if (dwo)
2421 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2422 }
2423
2424 NameToOffsetMap::iterator iter =
2425 m_function_scope_qualified_name_map.find(scope_qualified_name);
2426 if (iter == m_function_scope_qualified_name_map.end())
2427 return;
2428
2429 DIERefSetSP set_sp = (*iter).second;
2430 std::set<DIERef>::iterator set_iter;
2431 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2432 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2433 mangled_names.push_back(ConstString(die.GetMangledName()));
2434 }
2435}
2436
2437uint32_t SymbolFileDWARF::FindTypes(
2438 const SymbolContext &sc, const ConstString &name,
2439 const CompilerDeclContext *parent_decl_ctx, bool append,
2440 uint32_t max_matches,
2441 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2442 TypeMap &types) {
2443 // If we aren't appending the results to this list, then clear the list
2444 if (!append)
2445 types.Clear();
2446
2447 // Make sure we haven't already searched this SymbolFile before...
2448 if (searched_symbol_files.count(this))
2449 return 0;
2450 else
2451 searched_symbol_files.insert(this);
2452
2453 DWARFDebugInfo *info = DebugInfo();
2454 if (info == NULL)
2455 return 0;
2456
2457 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2458
2459 if (log) {
2460 if (parent_decl_ctx)
2461 GetObjectFile()->GetModule()->LogMessage(
2462 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2463 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2464 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2465 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2466 else
2467 GetObjectFile()->GetModule()->LogMessage(
2468 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2469 "NULL, append=%u, max_matches=%u, type_list)",
2470 name.GetCString(), append, max_matches);
2471 }
2472
2473 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2474 return 0;
2475
2476 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002477 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478 const size_t num_die_matches = die_offsets.size();
2479
2480 if (num_die_matches) {
2481 const uint32_t initial_types_size = types.GetSize();
2482 for (size_t i = 0; i < num_die_matches; ++i) {
2483 const DIERef &die_ref = die_offsets[i];
2484 DWARFDIE die = GetDIE(die_ref);
2485
2486 if (die) {
2487 if (!DIEInDeclContext(parent_decl_ctx, die))
2488 continue; // The containing decl contexts don't match
2489
2490 Type *matching_type = ResolveType(die, true, true);
2491 if (matching_type) {
2492 // We found a type pointer, now find the shared pointer form our type
2493 // list
2494 types.InsertUnique(matching_type->shared_from_this());
2495 if (types.GetSize() >= max_matches)
2496 break;
2497 }
2498 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002499 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2500 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002501 }
2502 }
2503 const uint32_t num_matches = types.GetSize() - initial_types_size;
2504 if (log && num_matches) {
2505 if (parent_decl_ctx) {
2506 GetObjectFile()->GetModule()->LogMessage(
2507 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2508 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2509 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2510 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2511 num_matches);
2512 } else {
2513 GetObjectFile()->GetModule()->LogMessage(
2514 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2515 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2516 name.GetCString(), append, max_matches, num_matches);
2517 }
2518 }
2519 return num_matches;
2520 } else {
2521 UpdateExternalModuleListIfNeeded();
2522
2523 for (const auto &pair : m_external_type_modules) {
2524 ModuleSP external_module_sp = pair.second;
2525 if (external_module_sp) {
2526 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2527 if (sym_vendor) {
2528 const uint32_t num_external_matches =
2529 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2530 max_matches, searched_symbol_files, types);
2531 if (num_external_matches)
2532 return num_external_matches;
2533 }
2534 }
2535 }
2536 }
2537
2538 return 0;
2539}
2540
2541size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2542 bool append, TypeMap &types) {
2543 if (!append)
2544 types.Clear();
2545
2546 if (context.empty())
2547 return 0;
2548
Kate Stoneb9c1b512016-09-06 20:57:50 +00002549 ConstString name = context.back().name;
2550
2551 if (!name)
2552 return 0;
2553
Pavel Labathb13f0332018-05-21 14:12:52 +00002554 DIEArray die_offsets;
2555 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002556 const size_t num_die_matches = die_offsets.size();
2557
2558 if (num_die_matches) {
2559 size_t num_matches = 0;
2560 for (size_t i = 0; i < num_die_matches; ++i) {
2561 const DIERef &die_ref = die_offsets[i];
2562 DWARFDIE die = GetDIE(die_ref);
2563
2564 if (die) {
2565 std::vector<CompilerContext> die_context;
2566 die.GetDWOContext(die_context);
2567 if (die_context != context)
2568 continue;
2569
2570 Type *matching_type = ResolveType(die, true, true);
2571 if (matching_type) {
2572 // We found a type pointer, now find the shared pointer form our type
2573 // list
2574 types.InsertUnique(matching_type->shared_from_this());
2575 ++num_matches;
2576 }
2577 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002578 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2579 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002580 }
2581 }
2582 return num_matches;
2583 }
2584 return 0;
2585}
2586
2587CompilerDeclContext
2588SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2589 const CompilerDeclContext *parent_decl_ctx) {
2590 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2591
2592 if (log) {
2593 GetObjectFile()->GetModule()->LogMessage(
2594 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2595 name.GetCString());
2596 }
2597
2598 CompilerDeclContext namespace_decl_ctx;
2599
2600 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2601 return namespace_decl_ctx;
2602
2603 DWARFDebugInfo *info = DebugInfo();
2604 if (info) {
2605 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002606 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002607 const size_t num_matches = die_offsets.size();
2608 if (num_matches) {
2609 for (size_t i = 0; i < num_matches; ++i) {
2610 const DIERef &die_ref = die_offsets[i];
2611 DWARFDIE die = GetDIE(die_ref);
2612
2613 if (die) {
2614 if (!DIEInDeclContext(parent_decl_ctx, die))
2615 continue; // The containing decl contexts don't match
2616
2617 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2618 if (dwarf_ast) {
2619 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2620 if (namespace_decl_ctx)
2621 break;
2622 }
2623 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002624 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2625 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 }
2627 }
2628 }
2629 }
2630 if (log && namespace_decl_ctx) {
2631 GetObjectFile()->GetModule()->LogMessage(
2632 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2633 "CompilerDeclContext(%p/%p) \"%s\"",
2634 name.GetCString(),
2635 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2636 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2637 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2638 }
2639
2640 return namespace_decl_ctx;
2641}
2642
2643TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2644 bool resolve_function_context) {
2645 TypeSP type_sp;
2646 if (die) {
2647 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2648 if (type_ptr == NULL) {
2649 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2650 assert(lldb_cu);
2651 SymbolContext sc(lldb_cu);
2652 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2653 while (parent_die != nullptr) {
2654 if (parent_die->Tag() == DW_TAG_subprogram)
2655 break;
2656 parent_die = parent_die->GetParent();
2657 }
2658 SymbolContext sc_backup = sc;
2659 if (resolve_function_context && parent_die != nullptr &&
2660 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2661 sc = sc_backup;
2662
2663 type_sp = ParseType(sc, die, NULL);
2664 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2665 // Grab the existing type from the master types lists
2666 type_sp = type_ptr->shared_from_this();
2667 }
2668 }
2669 return type_sp;
2670}
2671
2672DWARFDIE
2673SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2674 if (orig_die) {
2675 DWARFDIE die = orig_die;
2676
2677 while (die) {
2678 // If this is the original DIE that we are searching for a declaration
2679 // for, then don't look in the cache as we don't want our own decl
2680 // context to be our decl context...
2681 if (orig_die != die) {
2682 switch (die.Tag()) {
2683 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002684 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002685 case DW_TAG_namespace:
2686 case DW_TAG_structure_type:
2687 case DW_TAG_union_type:
2688 case DW_TAG_class_type:
2689 case DW_TAG_lexical_block:
2690 case DW_TAG_subprogram:
2691 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002692 case DW_TAG_inlined_subroutine: {
2693 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2694 if (abs_die) {
2695 return abs_die;
2696 }
2697 break;
2698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002699 default:
2700 break;
2701 }
2702 }
2703
2704 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2705 if (spec_die) {
2706 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2707 if (decl_ctx_die)
2708 return decl_ctx_die;
2709 }
2710
2711 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2712 if (abs_die) {
2713 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2714 if (decl_ctx_die)
2715 return decl_ctx_die;
2716 }
2717
2718 die = die.GetParent();
2719 }
2720 }
2721 return DWARFDIE();
2722}
2723
2724Symbol *
2725SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2726 Symbol *objc_class_symbol = NULL;
2727 if (m_obj_file) {
2728 Symtab *symtab = m_obj_file->GetSymtab();
2729 if (symtab) {
2730 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2731 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2732 Symtab::eVisibilityAny);
2733 }
2734 }
2735 return objc_class_symbol;
2736}
2737
2738// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002739// they don't then we can end up looking through all class types for a complete
2740// type and never find the full definition. We need to know if this attribute
2741// is supported, so we determine this here and cache th result. We also need to
2742// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002743// DWARF file
2744// if we are doing darwin DWARF in .o file debugging.
2745bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002746 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002747 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2748 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2749 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2750 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2751 else {
2752 DWARFDebugInfo *debug_info = DebugInfo();
2753 const uint32_t num_compile_units = GetNumCompileUnits();
2754 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002755 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002756 if (dwarf_cu != cu &&
2757 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2758 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2759 break;
2760 }
2761 }
2762 }
2763 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2764 GetDebugMapSymfile())
2765 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2766 }
2767 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002768}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002769
2770// This function can be used when a DIE is found that is a forward declaration
2771// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002772TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2773 const DWARFDIE &die, const ConstString &type_name,
2774 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002775
Kate Stoneb9c1b512016-09-06 20:57:50 +00002776 TypeSP type_sp;
2777
2778 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2779 return type_sp;
2780
2781 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002782 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002783
2784 const size_t num_matches = die_offsets.size();
2785
2786 if (num_matches) {
2787 for (size_t i = 0; i < num_matches; ++i) {
2788 const DIERef &die_ref = die_offsets[i];
2789 DWARFDIE type_die = GetDIE(die_ref);
2790
2791 if (type_die) {
2792 bool try_resolving_type = false;
2793
Adrian Prantl05097242018-04-30 16:49:04 +00002794 // Don't try and resolve the DIE we are looking for with the DIE
2795 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 if (type_die != die) {
2797 switch (type_die.Tag()) {
2798 case DW_TAG_class_type:
2799 case DW_TAG_structure_type:
2800 try_resolving_type = true;
2801 break;
2802 default:
2803 break;
2804 }
2805 }
2806
2807 if (try_resolving_type) {
2808 if (must_be_implementation &&
2809 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2810 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2811 DW_AT_APPLE_objc_complete_type, 0);
2812
2813 if (try_resolving_type) {
2814 Type *resolved_type = ResolveType(type_die, false, true);
2815 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2816 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2817 " (cu 0x%8.8" PRIx64 ")\n",
2818 die.GetID(),
2819 m_obj_file->GetFileSpec().GetFilename().AsCString(
2820 "<Unknown>"),
2821 type_die.GetID(), type_cu->GetID());
2822
2823 if (die)
2824 GetDIEToType()[die.GetDIE()] = resolved_type;
2825 type_sp = resolved_type->shared_from_this();
2826 break;
2827 }
2828 }
2829 }
2830 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002831 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2832 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002833 }
2834 }
2835 }
2836 return type_sp;
2837}
Greg Claytona8022fa2012-04-24 21:22:41 +00002838
Greg Clayton80c26302012-02-05 06:12:47 +00002839//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002840// This function helps to ensure that the declaration contexts match for two
2841// different DIEs. Often times debug information will refer to a forward
2842// declaration of a type (the equivalent of "struct my_struct;". There will
2843// often be a declaration of that type elsewhere that has the full definition.
2844// When we go looking for the full type "my_struct", we will find one or more
2845// matches in the accelerator tables and we will then need to make sure the
2846// type was in the same declaration context as the original DIE. This function
2847// can efficiently compare two DIEs and will return true when the declaration
2848// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002849//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2851 const DWARFDIE &die2) {
2852 if (die1 == die2)
2853 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002854
Kate Stoneb9c1b512016-09-06 20:57:50 +00002855 DWARFDIECollection decl_ctx_1;
2856 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002857 // The declaration DIE stack is a stack of the declaration context DIEs all
2858 // the way back to the compile unit. If a type "T" is declared inside a class
2859 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2860 // namespace "lldb", and the namespace is in a compile unit, there will be a
2861 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002862 //
2863 // [0] DW_TAG_class_type for "B"
2864 // [1] DW_TAG_class_type for "A"
2865 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002866 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002867 //
Adrian Prantl05097242018-04-30 16:49:04 +00002868 // We grab both contexts and make sure that everything matches all the way
2869 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870
2871 // First lets grab the decl contexts for both DIEs
2872 die1.GetDeclContextDIEs(decl_ctx_1);
2873 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00002874 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00002875 const size_t count1 = decl_ctx_1.Size();
2876 const size_t count2 = decl_ctx_2.Size();
2877 if (count1 != count2)
2878 return false;
2879
Adrian Prantl05097242018-04-30 16:49:04 +00002880 // Make sure the DW_TAG values match all the way back up the compile unit. If
2881 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002882 DWARFDIE decl_ctx_die1;
2883 DWARFDIE decl_ctx_die2;
2884 size_t i;
2885 for (i = 0; i < count1; i++) {
2886 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2887 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2888 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2889 return false;
2890 }
Greg Clayton890ff562012-02-02 05:48:16 +00002891#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002892
Kate Stoneb9c1b512016-09-06 20:57:50 +00002893 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002894 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2895 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2896 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002897 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2898 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2899 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002900
Greg Clayton890ff562012-02-02 05:48:16 +00002901#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002902 // Always skip the compile unit when comparing by only iterating up to "count
2903 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002904 for (i = 0; i < count1 - 1; i++) {
2905 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2906 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2907 const char *name1 = decl_ctx_die1.GetName();
2908 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002909 // If the string was from a DW_FORM_strp, then the pointer will often be
2910 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002911 if (name1 == name2)
2912 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002913
Adrian Prantl05097242018-04-30 16:49:04 +00002914 // Name pointers are not equal, so only compare the strings if both are not
2915 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002916 if (name1 && name2) {
2917 // If the strings don't compare, we are done...
2918 if (strcmp(name1, name2) != 0)
2919 return false;
2920 } else {
2921 // One name was NULL while the other wasn't
2922 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002923 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 }
Adrian Prantl05097242018-04-30 16:49:04 +00002925 // We made it through all of the checks and the declaration contexts are
2926 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002928}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002929
Kate Stoneb9c1b512016-09-06 20:57:50 +00002930TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2931 const DWARFDeclContext &dwarf_decl_ctx) {
2932 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002933
Kate Stoneb9c1b512016-09-06 20:57:50 +00002934 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2935 if (dwarf_decl_ctx_count > 0) {
2936 const ConstString type_name(dwarf_decl_ctx[0].name);
2937 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002938
Kate Stoneb9c1b512016-09-06 20:57:50 +00002939 if (type_name) {
2940 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2941 DWARF_LOG_LOOKUPS));
2942 if (log) {
2943 GetObjectFile()->GetModule()->LogMessage(
2944 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2945 "s, qualified-name='%s')",
2946 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2947 dwarf_decl_ctx.GetQualifiedName());
2948 }
2949
2950 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002951 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002952 const size_t num_matches = die_offsets.size();
2953
Adrian Prantl05097242018-04-30 16:49:04 +00002954 // Get the type system that we are looking to find a type for. We will
2955 // use this to ensure any matches we find are in a language that this
2956 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002957 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2958 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2959 ? nullptr
2960 : GetTypeSystemForLanguage(language);
2961
2962 if (num_matches) {
2963 for (size_t i = 0; i < num_matches; ++i) {
2964 const DIERef &die_ref = die_offsets[i];
2965 DWARFDIE type_die = GetDIE(die_ref);
2966
2967 if (type_die) {
2968 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002969 // looking for. We don't want to find a "Foo" type from Java if we
2970 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002971 if (type_system &&
2972 !type_system->SupportsLanguage(type_die.GetLanguage()))
2973 continue;
2974 bool try_resolving_type = false;
2975
2976 // Don't try and resolve the DIE we are looking for with the DIE
2977 // itself!
2978 const dw_tag_t type_tag = type_die.Tag();
2979 // Make sure the tags match
2980 if (type_tag == tag) {
2981 // The tags match, lets try resolving this type
2982 try_resolving_type = true;
2983 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002984 // The tags don't match, but we need to watch our for a forward
2985 // declaration for a struct and ("struct foo") ends up being a
2986 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002987 switch (type_tag) {
2988 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002989 // We had a "class foo", see if we ended up with a "struct foo
2990 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002991 try_resolving_type = (tag == DW_TAG_structure_type);
2992 break;
2993 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002994 // We had a "struct foo", see if we ended up with a "class foo
2995 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002996 try_resolving_type = (tag == DW_TAG_class_type);
2997 break;
2998 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002999 // Tags don't match, don't event try to resolve using this type
3000 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 break;
3002 }
3003 }
3004
3005 if (try_resolving_type) {
3006 DWARFDeclContext type_dwarf_decl_ctx;
3007 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3008
3009 if (log) {
3010 GetObjectFile()->GetModule()->LogMessage(
3011 log, "SymbolFileDWARF::"
3012 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3013 "qualified-name='%s') trying die=0x%8.8x (%s)",
3014 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3015 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3016 type_dwarf_decl_ctx.GetQualifiedName());
3017 }
3018
3019 // Make sure the decl contexts match all the way up
3020 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3021 Type *resolved_type = ResolveType(type_die, false);
3022 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3023 type_sp = resolved_type->shared_from_this();
3024 break;
3025 }
3026 }
3027 } else {
3028 if (log) {
3029 std::string qualified_name;
3030 type_die.GetQualifiedName(qualified_name);
3031 GetObjectFile()->GetModule()->LogMessage(
3032 log, "SymbolFileDWARF::"
3033 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3034 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3035 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3036 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3037 qualified_name.c_str());
3038 }
3039 }
3040 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00003041 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
3042 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003043 }
3044 }
3045 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003046 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 }
3048 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003049}
3050
Kate Stoneb9c1b512016-09-06 20:57:50 +00003051TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3052 bool *type_is_new_ptr) {
3053 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003054
Kate Stoneb9c1b512016-09-06 20:57:50 +00003055 if (die) {
3056 TypeSystem *type_system =
3057 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003058
Kate Stoneb9c1b512016-09-06 20:57:50 +00003059 if (type_system) {
3060 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3061 if (dwarf_ast) {
3062 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3063 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3064 if (type_sp) {
3065 TypeList *type_list = GetTypeList();
3066 if (type_list)
3067 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003068
Kate Stoneb9c1b512016-09-06 20:57:50 +00003069 if (die.Tag() == DW_TAG_subprogram) {
3070 DIERef die_ref = die.GetDIERef();
3071 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3072 .GetScopeQualifiedName()
3073 .AsCString(""));
3074 if (scope_qualified_name.size()) {
3075 NameToOffsetMap::iterator iter =
3076 m_function_scope_qualified_name_map.find(
3077 scope_qualified_name);
3078 if (iter != m_function_scope_qualified_name_map.end())
3079 (*iter).second->insert(die_ref);
3080 else {
3081 DIERefSetSP new_set(new std::set<DIERef>);
3082 new_set->insert(die_ref);
3083 m_function_scope_qualified_name_map.emplace(
3084 std::make_pair(scope_qualified_name, new_set));
3085 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003086 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003087 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003088 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003089 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003090 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003091 }
3092
3093 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094}
3095
Kate Stoneb9c1b512016-09-06 20:57:50 +00003096size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3097 const DWARFDIE &orig_die,
3098 bool parse_siblings, bool parse_children) {
3099 size_t types_added = 0;
3100 DWARFDIE die = orig_die;
3101 while (die) {
3102 bool type_is_new = false;
3103 if (ParseType(sc, die, &type_is_new).get()) {
3104 if (type_is_new)
3105 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003107
3108 if (parse_children && die.HasChildren()) {
3109 if (die.Tag() == DW_TAG_subprogram) {
3110 SymbolContext child_sc(sc);
3111 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3112 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3113 } else
3114 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3115 }
3116
3117 if (parse_siblings)
3118 die = die.GetSibling();
3119 else
3120 die.Clear();
3121 }
3122 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003123}
3124
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003126 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003127 assert(sc.comp_unit && sc.function);
3128 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003129 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003130 if (dwarf_cu) {
3131 const dw_offset_t function_die_offset = sc.function->GetID();
3132 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3133 if (function_die) {
3134 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3135 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003137 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003138
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003140}
3141
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003143 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 // At least a compile unit must be valid
3145 assert(sc.comp_unit);
3146 size_t types_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003147 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003148 if (dwarf_cu) {
3149 if (sc.function) {
3150 dw_offset_t function_die_offset = sc.function->GetID();
3151 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3152 if (func_die && func_die.HasChildren()) {
3153 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3154 }
3155 } else {
3156 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3157 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3158 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003160 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003164}
3165
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +00003167 ASSERT_MODULE_LOCK(this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003168 if (sc.comp_unit != NULL) {
3169 DWARFDebugInfo *info = DebugInfo();
3170 if (info == NULL)
3171 return 0;
3172
3173 if (sc.function) {
3174 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3175
3176 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3177 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3178 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3179 const size_t num_variables = ParseVariables(
3180 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3181
3182 // Let all blocks know they have parse all their variables
3183 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3184 return num_variables;
3185 }
3186 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003187 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003188
3189 if (dwarf_cu == NULL)
3190 return 0;
3191
3192 uint32_t vars_added = 0;
3193 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3194
3195 if (variables.get() == NULL) {
3196 variables.reset(new VariableList());
3197 sc.comp_unit->SetVariableList(variables);
3198
3199 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003200 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003201 const size_t num_matches = die_offsets.size();
3202 if (num_matches) {
3203 for (size_t i = 0; i < num_matches; ++i) {
3204 const DIERef &die_ref = die_offsets[i];
3205 DWARFDIE die = GetDIE(die_ref);
3206 if (die) {
3207 VariableSP var_sp(
3208 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3209 if (var_sp) {
3210 variables->AddVariableIfUnique(var_sp);
3211 ++vars_added;
3212 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003213 } else
3214 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003216 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003217 }
3218 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003219 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220 }
3221 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003222}
3223
Kate Stoneb9c1b512016-09-06 20:57:50 +00003224VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3225 const DWARFDIE &die,
3226 const lldb::addr_t func_low_pc) {
3227 if (die.GetDWARF() != this)
3228 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003229
Kate Stoneb9c1b512016-09-06 20:57:50 +00003230 VariableSP var_sp;
3231 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003232 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003233
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 var_sp = GetDIEToVariable()[die.GetDIE()];
3235 if (var_sp)
3236 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003237
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 const dw_tag_t tag = die.Tag();
3239 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003240
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3242 (tag == DW_TAG_formal_parameter && sc.function)) {
3243 DWARFAttributes attributes;
3244 const size_t num_attributes = die.GetAttributes(attributes);
3245 DWARFDIE spec_die;
3246 if (num_attributes > 0) {
3247 const char *name = NULL;
3248 const char *mangled = NULL;
3249 Declaration decl;
3250 uint32_t i;
3251 DWARFFormValue type_die_form;
3252 DWARFExpression location(die.GetCU());
3253 bool is_external = false;
3254 bool is_artificial = false;
3255 bool location_is_const_value_data = false;
3256 bool has_explicit_location = false;
3257 DWARFFormValue const_value;
3258 Variable::RangeList scope_ranges;
3259 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003260
Kate Stoneb9c1b512016-09-06 20:57:50 +00003261 for (i = 0; i < num_attributes; ++i) {
3262 dw_attr_t attr = attributes.AttributeAtIndex(i);
3263 DWARFFormValue form_value;
3264
3265 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3266 switch (attr) {
3267 case DW_AT_decl_file:
3268 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3269 form_value.Unsigned()));
3270 break;
3271 case DW_AT_decl_line:
3272 decl.SetLine(form_value.Unsigned());
3273 break;
3274 case DW_AT_decl_column:
3275 decl.SetColumn(form_value.Unsigned());
3276 break;
3277 case DW_AT_name:
3278 name = form_value.AsCString();
3279 break;
3280 case DW_AT_linkage_name:
3281 case DW_AT_MIPS_linkage_name:
3282 mangled = form_value.AsCString();
3283 break;
3284 case DW_AT_type:
3285 type_die_form = form_value;
3286 break;
3287 case DW_AT_external:
3288 is_external = form_value.Boolean();
3289 break;
3290 case DW_AT_const_value:
3291 // If we have already found a DW_AT_location attribute, ignore this
3292 // attribute.
3293 if (!has_explicit_location) {
3294 location_is_const_value_data = true;
3295 // The constant value will be either a block, a data value or a
3296 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003297 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3299 // Retrieve the value as a block expression.
3300 uint32_t block_offset =
3301 form_value.BlockData() - debug_info_data.GetDataStart();
3302 uint32_t block_length = form_value.Unsigned();
3303 location.CopyOpcodeData(module, debug_info_data, block_offset,
3304 block_length);
3305 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3306 // Retrieve the value as a data expression.
3307 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3308 DWARFFormValue::GetFixedFormSizesForAddressSize(
3309 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3310 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3311 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3312 uint32_t data_length =
3313 fixed_form_sizes.GetSize(form_value.Form());
3314 if (data_length == 0) {
3315 const uint8_t *data_pointer = form_value.BlockData();
3316 if (data_pointer) {
3317 form_value.Unsigned();
3318 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3319 // we need to get the byte size of the type later after we
3320 // create the variable
3321 const_value = form_value;
3322 }
3323 } else
3324 location.CopyOpcodeData(module, debug_info_data, data_offset,
3325 data_length);
3326 } else {
3327 // Retrieve the value as a string expression.
3328 if (form_value.Form() == DW_FORM_strp) {
3329 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3330 DWARFFormValue::GetFixedFormSizesForAddressSize(
3331 attributes.CompileUnitAtIndex(i)
3332 ->GetAddressByteSize(),
3333 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3334 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3335 uint32_t data_length =
3336 fixed_form_sizes.GetSize(form_value.Form());
3337 location.CopyOpcodeData(module, debug_info_data, data_offset,
3338 data_length);
3339 } else {
3340 const char *str = form_value.AsCString();
3341 uint32_t string_offset =
3342 str - (const char *)debug_info_data.GetDataStart();
3343 uint32_t string_length = strlen(str) + 1;
3344 location.CopyOpcodeData(module, debug_info_data,
3345 string_offset, string_length);
3346 }
3347 }
3348 }
3349 break;
3350 case DW_AT_location: {
3351 location_is_const_value_data = false;
3352 has_explicit_location = true;
3353 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003354 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003355
3356 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003357 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003358 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003359 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360 } else {
George Rimare4dee262018-10-23 09:46:15 +00003361 const DWARFDataExtractor &debug_loc_data = DebugLocData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003362 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3363
3364 size_t loc_list_length = DWARFExpression::LocationListSize(
3365 die.GetCU(), debug_loc_data, debug_loc_offset);
3366 if (loc_list_length > 0) {
3367 location.CopyOpcodeData(module, debug_loc_data,
3368 debug_loc_offset, loc_list_length);
3369 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3370 location.SetLocationListSlide(
3371 func_low_pc -
3372 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3373 }
3374 }
3375 } break;
3376 case DW_AT_specification:
3377 spec_die = GetDIE(DIERef(form_value));
3378 break;
3379 case DW_AT_start_scope: {
3380 if (form_value.Form() == DW_FORM_sec_offset) {
3381 DWARFRangeList dwarf_scope_ranges;
George Rimar7e7f9c12018-10-25 10:25:45 +00003382 const DWARFDebugRangesBase *debug_ranges = DebugRanges();
3383 debug_ranges->FindRanges(die.GetCU(),
Tamas Berghammer2540a552016-09-15 08:53:33 +00003384 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003385 dwarf_scope_ranges);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003386 } else {
3387 // TODO: Handle the case when DW_AT_start_scope have form
3388 // constant. The
3389 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003390 // behavior in case the enclosing block have a non coninious
3391 // address range and the DW_AT_start_scope entry have a form
3392 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003393 GetObjectFile()->GetModule()->ReportWarning(
3394 "0x%8.8" PRIx64
3395 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3396 die.GetID(), form_value.Form());
3397 }
3398
3399 scope_ranges.Sort();
3400 scope_ranges.CombineConsecutiveRanges();
3401 } break;
3402 case DW_AT_artificial:
3403 is_artificial = form_value.Boolean();
3404 break;
3405 case DW_AT_accessibility:
3406 break; // accessibility =
3407 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3408 case DW_AT_declaration:
3409 case DW_AT_description:
3410 case DW_AT_endianity:
3411 case DW_AT_segment:
3412 case DW_AT_visibility:
3413 default:
3414 case DW_AT_abstract_origin:
3415 case DW_AT_sibling:
3416 break;
3417 }
3418 }
3419 }
3420
3421 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3422 const dw_tag_t parent_tag = die.GetParent().Tag();
3423 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003424 (parent_tag == DW_TAG_compile_unit ||
3425 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003426 (parent_context_die.Tag() == DW_TAG_class_type ||
3427 parent_context_die.Tag() == DW_TAG_structure_type);
3428
3429 ValueType scope = eValueTypeInvalid;
3430
3431 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3432 SymbolContextScope *symbol_context_scope = NULL;
3433
Sam McCall30621392016-11-22 11:40:25 +00003434 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003435 if (!mangled) {
3436 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003437 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3438 // of global variables with commands like "frame var j". For
3439 // example, if j were an int variable holding a value 4 and
3440 // declared in a namespace B which in turn is contained in a
3441 // namespace A, the command "frame var j" returns
3442 // "(int) A::B::j = 4".
3443 // If the compiler does not emit a linkage name, we should be
3444 // able to generate a fully qualified name from the
3445 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003446 if ((parent_tag == DW_TAG_compile_unit ||
3447 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003448 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3449 DWARFDeclContext decl_ctx;
3450
3451 die.GetDWARFDeclContext(decl_ctx);
3452 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3453 }
3454 }
3455
Kate Stoneb9c1b512016-09-06 20:57:50 +00003456 if (tag == DW_TAG_formal_parameter)
3457 scope = eValueTypeVariableArgument;
3458 else {
Sam McCall30621392016-11-22 11:40:25 +00003459 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3460 // or static variable, so we have to do a little digging:
3461 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3462 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3463 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3464 // Clang likes to combine small global variables into the same symbol
3465 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3466 // so we need to look through the whole expression.
3467 bool is_static_lifetime =
3468 has_explicit_mangled ||
3469 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003470 // Check if the location has a DW_OP_addr with any address value...
3471 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3472 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003473 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003474 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3475 if (op_error) {
3476 StreamString strm;
3477 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3478 NULL);
3479 GetObjectFile()->GetModule()->ReportError(
3480 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003481 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003482 }
Sam McCall30621392016-11-22 11:40:25 +00003483 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3484 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003485 }
3486 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
Adrian Prantlf796e762018-10-02 17:50:42 +00003487 if (debug_map_symfile)
3488 // Set the module of the expression to the linked module
3489 // instead of the oject file so the relocated address can be
3490 // found there.
3491 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003492
Sam McCall30621392016-11-22 11:40:25 +00003493 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003494 if (is_external)
3495 scope = eValueTypeVariableGlobal;
3496 else
3497 scope = eValueTypeVariableStatic;
3498
3499 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003500 // When leaving the DWARF in the .o files on darwin, when we have a
3501 // global variable that wasn't initialized, the .o file might not
3502 // have allocated a virtual address for the global variable. In
3503 // this case it will have created a symbol for the global variable
3504 // that is undefined/data and external and the value will be the
3505 // byte size of the variable. When we do the address map in
3506 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3507 // do some magic here so we can get the correct address for our
3508 // global variable. The address for all of these entries will be
3509 // zero, and there will be an undefined symbol in this object file,
3510 // and the executable will have a matching symbol with a good
3511 // address. So here we dig up the correct address and replace it in
3512 // the location for the variable, and set the variable's symbol
3513 // context scope to be that of the main executable so the file
3514 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003515 bool linked_oso_file_addr = false;
3516 if (is_external && location_DW_OP_addr == 0) {
3517 // we have a possible uninitialized extern global
3518 ConstString const_name(mangled ? mangled : name);
3519 ObjectFile *debug_map_objfile =
3520 debug_map_symfile->GetObjectFile();
3521 if (debug_map_objfile) {
3522 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3523 if (debug_map_symtab) {
3524 Symbol *exe_symbol =
3525 debug_map_symtab->FindFirstSymbolWithNameAndType(
3526 const_name, eSymbolTypeData, Symtab::eDebugYes,
3527 Symtab::eVisibilityExtern);
3528 if (exe_symbol) {
3529 if (exe_symbol->ValueIsAddress()) {
3530 const addr_t exe_file_addr =
3531 exe_symbol->GetAddressRef().GetFileAddress();
3532 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3533 if (location.Update_DW_OP_addr(exe_file_addr)) {
3534 linked_oso_file_addr = true;
3535 symbol_context_scope = exe_symbol;
3536 }
3537 }
3538 }
3539 }
3540 }
3541 }
3542 }
3543
3544 if (!linked_oso_file_addr) {
3545 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003546 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003547 const lldb::addr_t exe_file_addr =
3548 debug_map_symfile->LinkOSOFileAddress(this,
3549 location_DW_OP_addr);
3550 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3551 // Update the file address for this variable
3552 location.Update_DW_OP_addr(exe_file_addr);
3553 } else {
3554 // Variable didn't make it into the final executable
3555 return var_sp;
3556 }
3557 }
3558 }
3559 } else {
3560 if (location_is_const_value_data)
3561 scope = eValueTypeVariableStatic;
3562 else {
3563 scope = eValueTypeVariableLocal;
3564 if (debug_map_symfile) {
3565 // We need to check for TLS addresses that we need to fixup
3566 if (location.ContainsThreadLocalStorage()) {
3567 location.LinkThreadLocalStorage(
3568 debug_map_symfile->GetObjectFile()->GetModule(),
3569 [this, debug_map_symfile](
3570 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3571 return debug_map_symfile->LinkOSOFileAddress(
3572 this, unlinked_file_addr);
3573 });
3574 scope = eValueTypeVariableThreadLocal;
3575 }
3576 }
3577 }
3578 }
3579 }
3580
3581 if (symbol_context_scope == NULL) {
3582 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003583 case DW_TAG_subprogram:
3584 case DW_TAG_inlined_subroutine:
3585 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003586 if (sc.function) {
3587 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3588 sc_parent_die.GetID());
3589 if (symbol_context_scope == NULL)
3590 symbol_context_scope = sc.function;
3591 }
3592 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003593
Kate Stoneb9c1b512016-09-06 20:57:50 +00003594 default:
3595 symbol_context_scope = sc.comp_unit;
3596 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003597 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003598 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003599
Kate Stoneb9c1b512016-09-06 20:57:50 +00003600 if (symbol_context_scope) {
3601 SymbolFileTypeSP type_sp(
3602 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3603
3604 if (const_value.Form() && type_sp && type_sp->GetType())
3605 location.CopyOpcodeData(const_value.Unsigned(),
3606 type_sp->GetType()->GetByteSize(),
3607 die.GetCU()->GetAddressByteSize());
3608
3609 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3610 symbol_context_scope, scope_ranges, &decl,
3611 location, is_external, is_artificial,
3612 is_static_member));
3613
3614 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3615 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003616 // Not ready to parse this variable yet. It might be a global or static
3617 // variable that is in a function scope and the function in the symbol
3618 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619 return var_sp;
3620 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003621 }
Adrian Prantl05097242018-04-30 16:49:04 +00003622 // Cache var_sp even if NULL (the variable was just a specification or was
3623 // missing vital information to be able to be displayed in the debugger
3624 // (missing location due to optimization, etc)) so we don't re-parse this
3625 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626 GetDIEToVariable()[die.GetDIE()] = var_sp;
3627 if (spec_die)
3628 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3629 }
3630 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003631}
3632
Kate Stoneb9c1b512016-09-06 20:57:50 +00003633DWARFDIE
3634SymbolFileDWARF::FindBlockContainingSpecification(
3635 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3636 // Give the concrete function die specified by "func_die_offset", find the
3637 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3638 // to "spec_block_die_offset"
3639 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3640 spec_block_die_offset);
3641}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003642
Kate Stoneb9c1b512016-09-06 20:57:50 +00003643DWARFDIE
3644SymbolFileDWARF::FindBlockContainingSpecification(
3645 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3646 if (die) {
3647 switch (die.Tag()) {
3648 case DW_TAG_subprogram:
3649 case DW_TAG_inlined_subroutine:
3650 case DW_TAG_lexical_block: {
3651 if (die.GetAttributeValueAsReference(
3652 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3653 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003654
Kate Stoneb9c1b512016-09-06 20:57:50 +00003655 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3656 DW_INVALID_OFFSET) ==
3657 spec_block_die_offset)
3658 return die;
3659 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003661
3662 // Give the concrete function die specified by "func_die_offset", find the
3663 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3664 // to "spec_block_die_offset"
3665 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3666 child_die = child_die.GetSibling()) {
3667 DWARFDIE result_die =
3668 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3669 if (result_die)
3670 return result_die;
3671 }
3672 }
3673
3674 return DWARFDIE();
3675}
3676
3677size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3678 const DWARFDIE &orig_die,
3679 const lldb::addr_t func_low_pc,
3680 bool parse_siblings, bool parse_children,
3681 VariableList *cc_variable_list) {
3682 if (!orig_die)
3683 return 0;
3684
3685 VariableListSP variable_list_sp;
3686
3687 size_t vars_added = 0;
3688 DWARFDIE die = orig_die;
3689 while (die) {
3690 dw_tag_t tag = die.Tag();
3691
3692 // Check to see if we have already parsed this variable or constant?
3693 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3694 if (var_sp) {
3695 if (cc_variable_list)
3696 cc_variable_list->AddVariableIfUnique(var_sp);
3697 } else {
3698 // We haven't already parsed it, lets do that now.
3699 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3700 (tag == DW_TAG_formal_parameter && sc.function)) {
3701 if (variable_list_sp.get() == NULL) {
3702 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3703 dw_tag_t parent_tag = sc_parent_die.Tag();
3704 switch (parent_tag) {
3705 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003706 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003707 if (sc.comp_unit != NULL) {
3708 variable_list_sp = sc.comp_unit->GetVariableList(false);
3709 if (variable_list_sp.get() == NULL) {
3710 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003711 }
3712 } else {
3713 GetObjectFile()->GetModule()->ReportError(
3714 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3715 "symbol context for 0x%8.8" PRIx64
3716 " %s.\n",
3717 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3718 orig_die.GetID(), orig_die.GetTagAsCString());
3719 }
3720 break;
3721
3722 case DW_TAG_subprogram:
3723 case DW_TAG_inlined_subroutine:
3724 case DW_TAG_lexical_block:
3725 if (sc.function != NULL) {
3726 // Check to see if we already have parsed the variables for the
3727 // given scope
3728
3729 Block *block = sc.function->GetBlock(true).FindBlockByID(
3730 sc_parent_die.GetID());
3731 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003732 // This must be a specification or abstract origin with a
3733 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003734 // to find the concrete block so we can correctly add the
3735 // variable to it
3736 const DWARFDIE concrete_block_die =
3737 FindBlockContainingSpecification(
3738 DIERef(sc.function->GetID(), this),
3739 sc_parent_die.GetOffset());
3740 if (concrete_block_die)
3741 block = sc.function->GetBlock(true).FindBlockByID(
3742 concrete_block_die.GetID());
3743 }
3744
3745 if (block != NULL) {
3746 const bool can_create = false;
3747 variable_list_sp = block->GetBlockVariableList(can_create);
3748 if (variable_list_sp.get() == NULL) {
3749 variable_list_sp.reset(new VariableList());
3750 block->SetVariableList(variable_list_sp);
3751 }
3752 }
3753 }
3754 break;
3755
3756 default:
3757 GetObjectFile()->GetModule()->ReportError(
3758 "didn't find appropriate parent DIE for variable list for "
3759 "0x%8.8" PRIx64 " %s.\n",
3760 orig_die.GetID(), orig_die.GetTagAsCString());
3761 break;
3762 }
3763 }
3764
3765 if (variable_list_sp) {
3766 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3767 if (var_sp) {
3768 variable_list_sp->AddVariableIfUnique(var_sp);
3769 if (cc_variable_list)
3770 cc_variable_list->AddVariableIfUnique(var_sp);
3771 ++vars_added;
3772 }
3773 }
3774 }
3775 }
3776
3777 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3778
3779 if (!skip_children && parse_children && die.HasChildren()) {
3780 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3781 true, cc_variable_list);
3782 }
3783
3784 if (parse_siblings)
3785 die = die.GetSibling();
3786 else
3787 die.Clear();
3788 }
3789 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003790}
3791
Vedant Kumar4b36f792018-10-05 23:23:15 +00003792/// Collect call graph edges present in a function DIE.
3793static std::vector<lldb_private::CallEdge>
3794CollectCallEdges(DWARFDIE function_die) {
3795 // Check if the function has a supported call site-related attribute.
3796 // TODO: In the future it may be worthwhile to support call_all_source_calls.
3797 uint64_t has_call_edges =
3798 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0);
3799 if (!has_call_edges)
3800 return {};
3801
3802 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3803 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3804 function_die.GetPubname());
3805
3806 // Scan the DIE for TAG_call_site entries.
3807 // TODO: A recursive scan of all blocks in the subprogram is needed in order
3808 // to be DWARF5-compliant. This may need to be done lazily to be performant.
3809 // For now, assume that all entries are nested directly under the subprogram
3810 // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3811 std::vector<CallEdge> call_edges;
3812 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3813 child = child.GetSibling()) {
3814 if (child.Tag() != DW_TAG_call_site)
3815 continue;
3816
3817 // Extract DW_AT_call_origin (the call target's DIE).
3818 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin);
3819 if (!call_origin.IsValid()) {
3820 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3821 function_die.GetPubname());
3822 continue;
3823 }
3824
3825 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3826 // available. It should only ever be unavailable for tail call edges, in
3827 // which case use LLDB_INVALID_ADDRESS.
3828 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc,
3829 LLDB_INVALID_ADDRESS);
3830
3831 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3832 call_origin.GetPubname(), return_pc);
3833 call_edges.emplace_back(call_origin.GetMangledName(), return_pc);
3834 }
3835 return call_edges;
3836}
3837
3838std::vector<lldb_private::CallEdge>
3839SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3840 DWARFDIE func_die = GetDIEFromUID(func_id.GetID());
3841 if (func_die.IsValid())
3842 return CollectCallEdges(func_die);
3843 return {};
3844}
3845
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003846//------------------------------------------------------------------
3847// PluginInterface protocol
3848//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003849ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3850
3851uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3852
Pavel Labath9337b412018-06-06 11:35:23 +00003853void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003854
Kate Stoneb9c1b512016-09-06 20:57:50 +00003855SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3856 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3857 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3858 if (module_sp) {
3859 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3860 if (sym_vendor)
3861 m_debug_map_symfile =
3862 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003863 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003864 }
3865 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003866}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003867
3868DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003869SymbolFileDWARF::GetLocationListFormat() const {
George Rimare4dee262018-10-23 09:46:15 +00003870 if (m_data_debug_loclists.m_data.GetByteSize() > 0)
3871 return DWARFExpression::LocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003872 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003873}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003874
3875SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3876 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003877 ModuleSpec module_spec;
3878 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3879 module_spec.GetSymbolFileSpec() =
3880 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
3881 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003882 if (FileSystem::Instance().Exists(dwp_filespec)) {
Tamas Berghammer963ce482017-08-25 13:56:14 +00003883 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3884 dwp_filespec);
3885 }
3886 });
3887 return m_dwp_symfile.get();
3888}