blob: f895c9e376ce3be64b3a514d9c727d9ef04460bb [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 "
121 "manually."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000122 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, {}, nullptr},
Pavel Labath9337b412018-06-06 11:35:23 +0000123};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000124
Pavel Labath9337b412018-06-06 11:35:23 +0000125enum {
126 ePropertySymLinkPaths,
127 ePropertyIgnoreIndexes,
128};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130class PluginProperties : public Properties {
131public:
132 static ConstString GetSettingName() {
133 return SymbolFileDWARF::GetPluginNameStatic();
134 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 PluginProperties() {
137 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
138 m_collection_sp->Initialize(g_properties);
139 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 FileSpecList &GetSymLinkPaths() {
142 OptionValueFileSpecList *option_value =
143 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
144 nullptr, true, ePropertySymLinkPaths);
145 assert(option_value);
146 return option_value->GetCurrentValue();
147 }
Pavel Labath9337b412018-06-06 11:35:23 +0000148
149 bool IgnoreFileIndexes() const {
150 return m_collection_sp->GetPropertyAtIndexAsBoolean(
151 nullptr, ePropertyIgnoreIndexes, false);
152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
158 static const auto g_settings_sp(std::make_shared<PluginProperties>());
159 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
165 if (!path_from_dwarf || !path_from_dwarf[0]) {
166 return path_from_dwarf;
167 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 const char *colon_pos = strchr(path_from_dwarf, ':');
170 if (nullptr == colon_pos) {
171 return path_from_dwarf;
172 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 const char *slash_pos = strchr(path_from_dwarf, '/');
175 if (slash_pos && (slash_pos < colon_pos)) {
176 return path_from_dwarf;
177 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000178
Adrian Prantl05097242018-04-30 16:49:04 +0000179 // check whether we have a windows path, and so the first character is a
180 // drive-letter not a hostname.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
182 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
183 return path_from_dwarf;
184 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 return colon_pos + 1;
187}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000188
Greg Clayton776cd7a2018-04-27 15:45:58 +0000189static FileSpec resolveCompDir(const char *path_from_dwarf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 if (!path_from_dwarf)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000191 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192
193 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
194 // Remove the host part if present.
195 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
196 if (!local_path)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000197 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198
199 bool is_symlink = false;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000200 // Always normalize our compile unit directory to get rid of redundant
201 // slashes and other path anomalies before we use it for path prepending
202 FileSpec local_spec(local_path, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
204 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
205 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
Greg Clayton776cd7a2018-04-27 15:45:58 +0000206 local_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207
208 if (!is_symlink)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000209 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210
Zachary Turner7d86ee52017-03-08 17:56:08 +0000211 namespace fs = llvm::sys::fs;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000212 if (fs::get_file_type(local_spec.GetPath(), false) !=
Zachary Turner7d86ee52017-03-08 17:56:08 +0000213 fs::file_type::symlink_file)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000214 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215
Greg Clayton776cd7a2018-04-27 15:45:58 +0000216 FileSpec resolved_symlink;
217 const auto error = FileSystem::Readlink(local_spec, resolved_symlink);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (error.Success())
Greg Clayton776cd7a2018-04-27 15:45:58 +0000219 return resolved_symlink;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220
Greg Clayton776cd7a2018-04-27 15:45:58 +0000221 return local_spec;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000222}
223
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000224DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000225 return nullptr;
226}
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228void SymbolFileDWARF::Initialize() {
229 LogChannelDWARF::Initialize();
230 PluginManager::RegisterPlugin(GetPluginNameStatic(),
231 GetPluginDescriptionStatic(), CreateInstance,
232 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
236 if (!PluginManager::GetSettingForSymbolFilePlugin(
237 debugger, PluginProperties::GetSettingName())) {
238 const bool is_global_setting = true;
239 PluginManager::CreateSettingForSymbolFilePlugin(
240 debugger, GetGlobalPluginProperties()->GetValueProperties(),
241 ConstString("Properties for the dwarf symbol-file plug-in."),
242 is_global_setting);
243 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246void SymbolFileDWARF::Terminate() {
247 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000248 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249}
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
252 static ConstString g_name("dwarf");
253 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
257 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258}
259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
261 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264TypeList *SymbolFileDWARF::GetTypeList() {
265 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
266 if (debug_map_symfile)
267 return debug_map_symfile->GetTypeList();
268 else
269 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000270}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
272 dw_offset_t max_die_offset, uint32_t type_mask,
273 TypeSet &type_set) {
274 if (die) {
275 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 if (die_offset >= max_die_offset)
278 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (die_offset >= min_die_offset) {
281 const dw_tag_t tag = die.Tag();
282
283 bool add_type = false;
284
285 switch (tag) {
286 case DW_TAG_array_type:
287 add_type = (type_mask & eTypeClassArray) != 0;
288 break;
289 case DW_TAG_unspecified_type:
290 case DW_TAG_base_type:
291 add_type = (type_mask & eTypeClassBuiltin) != 0;
292 break;
293 case DW_TAG_class_type:
294 add_type = (type_mask & eTypeClassClass) != 0;
295 break;
296 case DW_TAG_structure_type:
297 add_type = (type_mask & eTypeClassStruct) != 0;
298 break;
299 case DW_TAG_union_type:
300 add_type = (type_mask & eTypeClassUnion) != 0;
301 break;
302 case DW_TAG_enumeration_type:
303 add_type = (type_mask & eTypeClassEnumeration) != 0;
304 break;
305 case DW_TAG_subroutine_type:
306 case DW_TAG_subprogram:
307 case DW_TAG_inlined_subroutine:
308 add_type = (type_mask & eTypeClassFunction) != 0;
309 break;
310 case DW_TAG_pointer_type:
311 add_type = (type_mask & eTypeClassPointer) != 0;
312 break;
313 case DW_TAG_rvalue_reference_type:
314 case DW_TAG_reference_type:
315 add_type = (type_mask & eTypeClassReference) != 0;
316 break;
317 case DW_TAG_typedef:
318 add_type = (type_mask & eTypeClassTypedef) != 0;
319 break;
320 case DW_TAG_ptr_to_member_type:
321 add_type = (type_mask & eTypeClassMemberPointer) != 0;
322 break;
323 }
324
325 if (add_type) {
326 const bool assert_not_being_parsed = true;
327 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
328 if (type) {
329 if (type_set.find(type) == type_set.end())
330 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000331 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000333 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334
335 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
336 child_die = child_die.GetSibling()) {
337 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
338 }
339 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
343 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000344
345{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000349 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 if (sc_scope)
351 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 if (comp_unit) {
354 dwarf_cu = GetDWARFCompileUnit(comp_unit);
355 if (dwarf_cu == 0)
356 return 0;
357 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
358 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
359 } else {
360 DWARFDebugInfo *info = DebugInfo();
361 if (info) {
362 const size_t num_cus = info->GetNumCompileUnits();
363 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
364 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
365 if (dwarf_cu) {
366 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000367 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000369 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 }
371
372 std::set<CompilerType> compiler_type_set;
373 size_t num_types_added = 0;
374 for (Type *type : type_set) {
375 CompilerType compiler_type = type->GetForwardCompilerType();
376 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
377 compiler_type_set.insert(compiler_type);
378 type_list.Insert(type->shared_from_this());
379 ++num_types_added;
380 }
381 }
382 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000383}
384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385//----------------------------------------------------------------------
386// Gets the first parent that is a lexical block, function or inlined
387// subroutine, or compile unit.
388//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000389DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
391 DWARFDIE die;
392 for (die = child_die.GetParent(); die; die = die.GetParent()) {
393 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 switch (tag) {
396 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000397 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 case DW_TAG_subprogram:
399 case DW_TAG_inlined_subroutine:
400 case DW_TAG_lexical_block:
401 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 }
404 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405}
406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Pavel Labathb13f0332018-05-21 14:12:52 +0000408 : SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
409 << 32), // Used by SymbolFileDWARFDebugMap to
410 // when this class parses .o files to
411 // contain the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
413 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
414 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
415 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
416 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
Pavel Labathb13f0332018-05-21 14:12:52 +0000417 m_line(), m_fetched_external_modules(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
419 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421SymbolFileDWARF::~SymbolFileDWARF() {}
422
423static const ConstString &GetDWARFMachOSegmentName() {
424 static ConstString g_dwarf_section_name("__DWARF");
425 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
429 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
430 if (debug_map_symfile)
431 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
432 else
433 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000434}
435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
437 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
438 TypeSystem *type_system;
439 if (debug_map_symfile) {
440 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
441 } else {
442 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
443 if (type_system)
444 type_system->SetSymbolFile(this);
445 }
446 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449void SymbolFileDWARF::InitializeObject() {
Pavel Labath9337b412018-06-06 11:35:23 +0000450 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 ModuleSP module_sp(m_obj_file->GetModule());
452 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000453 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000454 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000458 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 }
460
Pavel Labath9337b412018-06-06 11:35:23 +0000461 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
462 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
463 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
464 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
465 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
466 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467
Pavel Labath9337b412018-06-06 11:35:23 +0000468 m_index = AppleDWARFIndex::Create(
469 *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
470 apple_types, apple_objc, get_debug_str_data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471
Pavel Labath9337b412018-06-06 11:35:23 +0000472 if (m_index)
473 return;
474
475 DWARFDataExtractor debug_names;
476 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
477 if (debug_names.GetByteSize() > 0) {
478 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
479 DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
480 debug_names, get_debug_str_data(),
481 DebugInfo());
482 if (index_or) {
483 m_index = std::move(*index_or);
484 return;
485 }
486 LLDB_LOG_ERROR(log, index_or.takeError(),
487 "Unable to read .debug_names data: {0}");
488 }
489 }
490
491 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
492 DebugInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493}
494
495bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
George Rimarc6c7bfc2018-09-13 17:06:47 +0000496 return version >= 2 && version <= 5;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497}
498
499uint32_t SymbolFileDWARF::CalculateAbilities() {
500 uint32_t abilities = 0;
501 if (m_obj_file != NULL) {
502 const Section *section = NULL;
503 const SectionList *section_list = m_obj_file->GetSectionList();
504 if (section_list == NULL)
505 return 0;
506
Adrian Prantl05097242018-04-30 16:49:04 +0000507 // On non Apple platforms we might have .debug_types debug info that is
508 // created by using "-fdebug-types-section". LLDB currently will try to
509 // load this debug info, but it causes crashes during debugging when types
510 // are missing since it doesn't know how to parse the info in the
511 // .debug_types type units. This causes all complex debug info types to be
512 // unresolved. Because this causes LLDB to crash and since it really
513 // doesn't provide a solid debuggiung experience, we should disable trying
514 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000515 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
516 m_obj_file->GetModule()->ReportWarning(
517 "lldb doesn’t support .debug_types debug info");
518 return 0;
519 }
520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 uint64_t debug_abbrev_file_size = 0;
522 uint64_t debug_info_file_size = 0;
523 uint64_t debug_line_file_size = 0;
524
525 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
526
527 if (section)
528 section_list = &section->GetChildren();
529
530 section =
531 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
532 if (section != NULL) {
533 debug_info_file_size = section->GetFileSize();
534
535 section =
536 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
537 .get();
538 if (section)
539 debug_abbrev_file_size = section->GetFileSize();
540
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000541 DWARFDebugAbbrev *abbrev = DebugAbbrev();
542 if (abbrev) {
543 std::set<dw_form_t> invalid_forms;
544 abbrev->GetUnsupportedForms(invalid_forms);
545 if (!invalid_forms.empty()) {
546 StreamString error;
547 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
548 for (auto form : invalid_forms)
549 error.Printf(" %#x", form);
550 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
551 return 0;
552 }
553 }
554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 section =
556 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
557 .get();
558 if (section)
559 debug_line_file_size = section->GetFileSize();
560 } else {
561 const char *symfile_dir_cstr =
562 m_obj_file->GetFileSpec().GetDirectory().GetCString();
563 if (symfile_dir_cstr) {
564 if (strcasestr(symfile_dir_cstr, ".dsym")) {
565 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000566 // We have a dSYM file that didn't have a any debug info. If the
567 // string table has a size of 1, then it was made from an
568 // executable with no debug info, or from an executable that was
569 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 section =
571 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
572 .get();
573 if (section && section->GetFileSize() == 1) {
574 m_obj_file->GetModule()->ReportWarning(
575 "empty dSYM file detected, dSYM was created with an "
576 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000577 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +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
583 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
584 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
585 LocalVariables | VariableTypes;
586
587 if (debug_line_file_size > 0)
588 abilities |= LineTables;
589 }
590 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591}
592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593const DWARFDataExtractor &
594SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
595 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000596 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
597 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
598 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
603 DWARFDataExtractor &data) {
604 ModuleSP module_sp(m_obj_file->GetModule());
605 const SectionList *section_list = module_sp->GetSectionList();
606 if (section_list) {
607 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
608 if (section_sp) {
609 // See if we memory mapped the DWARF segment?
610 if (m_dwarf_data.GetByteSize()) {
611 data.SetData(m_dwarf_data, section_sp->GetOffset(),
612 section_sp->GetFileSize());
613 } else {
614 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
615 data.Clear();
616 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619}
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
622 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
623 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624}
625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
627 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
628}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
631 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
632 m_data_debug_aranges);
633}
634
635const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
636 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
637}
638
639const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
640 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
641}
642
643const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
644 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
645}
646
George Rimarc6c7bfc2018-09-13 17:06:47 +0000647const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_str_data() {
648 return GetCachedSectionData(eSectionTypeDWARFDebugLineStr, m_data_debug_line_str);
649}
650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
652 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
653}
654
655const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
656 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
657}
658
659const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
660 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
661 m_data_debug_ranges);
662}
663
664const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
665 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
666}
667
668const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
669 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
670 m_data_debug_str_offsets);
671}
672
Greg Clayton2550ca12018-05-08 17:19:24 +0000673const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
674 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
675}
676
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
678 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
679}
680
681const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
682 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
683}
684
685const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
686 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
687 m_data_apple_namespaces);
688}
689
690const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
691 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
692}
693
Jan Kratochvile4777a92018-04-29 19:47:48 +0000694const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
695 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
696 m_data_gnu_debugaltlink);
697}
698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
700 if (m_abbr.get() == NULL) {
701 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
702 if (debug_abbrev_data.GetByteSize() > 0) {
703 m_abbr.reset(new DWARFDebugAbbrev());
704 if (m_abbr.get())
705 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 }
708 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709}
710
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
712 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713}
714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
716 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000717 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
718 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
719 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 if (get_debug_info_data().GetByteSize() > 0) {
721 m_info.reset(new DWARFDebugInfo());
722 if (m_info.get()) {
723 m_info->SetDwarfData(this);
724 }
Greg Clayton1f746072012-08-29 21:13:06 +0000725 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 }
727 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728}
729
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
731 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732}
733
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000734DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
736 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000737 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738
739 DWARFDebugInfo *info = DebugInfo();
740 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000741 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
742 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000744 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
746 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
747 dwarf_cu->SetUserData(comp_unit);
748 return dwarf_cu;
749 }
750 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751}
752
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
754 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000755 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
756 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
757 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 if (get_debug_ranges_data().GetByteSize() > 0) {
759 m_ranges.reset(new DWARFDebugRanges());
760 if (m_ranges.get())
761 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000762 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 }
764 return m_ranges.get();
765}
766
767const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
768 return m_ranges.get();
769}
770
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000771lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 uint32_t cu_idx) {
773 CompUnitSP cu_sp;
774 if (dwarf_cu) {
775 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
776 if (comp_unit) {
777 // We already parsed this compile unit, had out a shared pointer to it
778 cu_sp = comp_unit->shared_from_this();
779 } else {
780 if (dwarf_cu->GetSymbolFileDWARF() != this) {
781 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
782 cu_idx);
783 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
784 // Let the debug map create the compile unit
785 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
786 dwarf_cu->SetUserData(cu_sp.get());
787 } else {
788 ModuleSP module_sp(m_obj_file->GetModule());
789 if (module_sp) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000790 const DWARFDIE cu_die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 if (cu_die) {
792 FileSpec cu_file_spec{cu_die.GetName(), false};
793 if (cu_file_spec) {
794 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000795 // resolve the file. This can be expensive e.g. when the source
796 // files are
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 // NFS mounted.
798 if (cu_file_spec.IsRelative()) {
799 const char *cu_comp_dir{
800 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
801 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
802 }
803
804 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000805 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 remapped_file))
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000807 cu_file_spec.SetFile(remapped_file, false,
808 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 }
810
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000811 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
813
814 bool is_optimized = dwarf_cu->GetIsOptimized();
815 cu_sp.reset(new CompileUnit(
816 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
817 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
818 if (cu_sp) {
819 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000820 // try and get the first entry in the supports files from the
821 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 if (!cu_file_spec) {
823 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
824 if (cu_file_spec) {
825 (FileSpec &)(*cu_sp) = cu_file_spec;
826 // Also fix the invalid file spec which was copied from the
827 // compile unit.
828 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
829 }
830 }
831
832 dwarf_cu->SetUserData(cu_sp.get());
833
834 // Figure out the compile unit index if we weren't given one
835 if (cu_idx == UINT32_MAX)
836 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
837
838 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
839 cu_idx, cu_sp);
840 }
841 }
842 }
843 }
844 }
845 }
846 return cu_sp;
847}
848
849uint32_t SymbolFileDWARF::GetNumCompileUnits() {
850 DWARFDebugInfo *info = DebugInfo();
851 if (info)
852 return info->GetNumCompileUnits();
853 return 0;
854}
855
856CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
857 CompUnitSP cu_sp;
858 DWARFDebugInfo *info = DebugInfo();
859 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000860 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 if (dwarf_cu)
862 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
863 }
864 return cu_sp;
865}
866
867Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
868 const DWARFDIE &die) {
869 if (die.IsValid()) {
870 TypeSystem *type_system =
871 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
872
873 if (type_system) {
874 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
875 if (dwarf_ast)
876 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
877 }
878 }
879 return nullptr;
880}
881
882bool SymbolFileDWARF::FixupAddress(Address &addr) {
883 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
884 if (debug_map_symfile) {
885 return debug_map_symfile->LinkOSOAddress(addr);
886 }
887 // This is a normal DWARF file, no address fixups need to happen
888 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000889}
Greg Clayton1f746072012-08-29 21:13:06 +0000890lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
892 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000893 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 if (dwarf_cu)
895 return dwarf_cu->GetLanguageType();
896 else
897 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000898}
899
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
901 assert(sc.comp_unit);
902 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000903 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 if (dwarf_cu) {
905 DWARFDIECollection function_dies;
906 const size_t num_functions =
907 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
908 size_t func_idx;
909 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
910 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
911 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
912 if (ParseCompileUnitFunction(sc, die))
913 ++functions_added;
914 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 // FixupTypes();
917 }
918 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919}
920
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
922 const SymbolContext &sc, FileSpecList &support_files) {
923 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000924 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000926 const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 if (cu_die) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000929 FileSpec cu_comp_dir = resolveCompDir(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
931 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
932 DW_AT_stmt_list, DW_INVALID_OFFSET);
933 if (stmt_list != DW_INVALID_OFFSET) {
934 // All file indexes in DWARF are one based and a file of index zero is
935 // supposed to be the compile unit itself.
936 support_files.Append(*sc.comp_unit);
937 return DWARFDebugLine::ParseSupportFiles(
938 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
George Rimarc6c7bfc2018-09-13 17:06:47 +0000939 stmt_list, support_files, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942 }
943 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944}
945
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
947 const lldb_private::SymbolContext &sc) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000948 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 if (dwarf_cu)
950 return dwarf_cu->GetIsOptimized();
951 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000952}
953
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954bool SymbolFileDWARF::ParseImportedModules(
955 const lldb_private::SymbolContext &sc,
956 std::vector<lldb_private::ConstString> &imported_modules) {
957 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000958 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 if (dwarf_cu) {
960 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
961 sc.comp_unit->GetLanguage())) {
962 UpdateExternalModuleListIfNeeded();
963
964 if (sc.comp_unit) {
Jan Kratochvil93afb052018-05-24 20:51:13 +0000965 const DWARFDIE die = dwarf_cu->DIE();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966
967 if (die) {
968 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
969 child_die = child_die.GetSibling()) {
970 if (child_die.Tag() == DW_TAG_imported_declaration) {
971 if (DWARFDIE module_die =
972 child_die.GetReferencedDIE(DW_AT_import)) {
973 if (module_die.Tag() == DW_TAG_module) {
974 if (const char *name = module_die.GetAttributeValueAsString(
975 DW_AT_name, nullptr)) {
976 ConstString const_name(name);
977 imported_modules.push_back(const_name);
978 }
Sean Callananb0300a42016-01-14 21:46:09 +0000979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 }
Sean Callananb0300a42016-01-14 21:46:09 +0000981 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 } else {
985 for (const auto &pair : m_external_type_modules) {
986 imported_modules.push_back(pair.first);
987 }
988 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 }
991 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000992}
993
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994struct ParseDWARFLineTableCallbackInfo {
995 LineTable *line_table;
996 std::unique_ptr<LineSequence> sequence_ap;
997 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998};
999
1000//----------------------------------------------------------------------
1001// ParseStatementTableCallback
1002//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003static void ParseDWARFLineTableCallback(dw_offset_t offset,
1004 const DWARFDebugLine::State &state,
1005 void *userData) {
1006 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1007 // Just started parsing the line table
1008 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1009 // Done parsing line table, nothing to do for the cleanup
1010 } else {
1011 ParseDWARFLineTableCallbackInfo *info =
1012 (ParseDWARFLineTableCallbackInfo *)userData;
1013 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014
Adrian Prantl05097242018-04-30 16:49:04 +00001015 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 if (!info->sequence_ap.get()) {
1017 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1018 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 line_table->AppendLineEntryToSequence(
1021 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1022 state.column, state.file, state.is_stmt, state.basic_block,
1023 state.prologue_end, state.epilogue_begin, state.end_sequence);
1024 if (state.end_sequence) {
1025 // First, put the current sequence into the line table.
1026 line_table->InsertSequence(info->sequence_ap.get());
1027 // Then, empty it to prepare for the next sequence.
1028 info->sequence_ap->Clear();
1029 }
1030 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031}
1032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1034 assert(sc.comp_unit);
1035 if (sc.comp_unit->GetLineTable() != NULL)
1036 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001038 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 if (dwarf_cu) {
Jan Kratochvil93afb052018-05-24 20:51:13 +00001040 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 if (dwarf_cu_die) {
1042 const dw_offset_t cu_line_offset =
1043 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1044 DW_INVALID_OFFSET);
1045 if (cu_line_offset != DW_INVALID_OFFSET) {
1046 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1047 if (line_table_ap.get()) {
1048 ParseDWARFLineTableCallbackInfo info;
1049 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 /*
1052 * MIPS:
1053 * The SymbolContext may not have a valid target, thus we may not be
1054 * able
1055 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1056 * #0
1057 * for MIPS. Use ArchSpec to clear the bit #0.
1058 */
1059 ArchSpec arch;
1060 GetObjectFile()->GetArchitecture(arch);
1061 switch (arch.GetMachine()) {
1062 case llvm::Triple::mips:
1063 case llvm::Triple::mipsel:
1064 case llvm::Triple::mips64:
1065 case llvm::Triple::mips64el:
1066 info.addr_mask = ~((lldb::addr_t)1);
1067 break;
1068 default:
1069 info.addr_mask = ~((lldb::addr_t)0);
1070 break;
1071 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 lldb::offset_t offset = cu_line_offset;
1074 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1075 ParseDWARFLineTableCallback,
George Rimarc6c7bfc2018-09-13 17:06:47 +00001076 &info, dwarf_cu);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1078 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001079 // We have an object file that has a line table with addresses that
1080 // are not linked. We need to link the line table and convert the
1081 // addresses that are relative to the .o file into addresses for
1082 // the main executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 sc.comp_unit->SetLineTable(
1084 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1085 } else {
1086 sc.comp_unit->SetLineTable(line_table_ap.release());
1087 return true;
1088 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 }
1093 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094}
1095
Siva Chandrad8335e92015-12-16 00:22:08 +00001096lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1098 auto iter = m_debug_macros_map.find(*offset);
1099 if (iter != m_debug_macros_map.end())
1100 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1103 if (debug_macro_data.GetByteSize() == 0)
1104 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001105
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1107 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 const DWARFDebugMacroHeader &header =
1110 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1111 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1112 header.OffsetIs64Bit(), offset, this,
1113 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001114
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001116}
1117
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1119 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001120
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001121 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001123 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124
Jan Kratochvil93afb052018-05-24 20:51:13 +00001125 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 if (!dwarf_cu_die)
1127 return false;
1128
1129 lldb::offset_t sect_offset =
1130 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1131 if (sect_offset == DW_INVALID_OFFSET)
1132 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1133 DW_INVALID_OFFSET);
1134 if (sect_offset == DW_INVALID_OFFSET)
1135 return false;
1136
1137 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1138
1139 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001140}
1141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1143 Block *parent_block,
1144 const DWARFDIE &orig_die,
1145 addr_t subprogram_low_pc,
1146 uint32_t depth) {
1147 size_t blocks_added = 0;
1148 DWARFDIE die = orig_die;
1149 while (die) {
1150 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001151
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 switch (tag) {
1153 case DW_TAG_inlined_subroutine:
1154 case DW_TAG_subprogram:
1155 case DW_TAG_lexical_block: {
1156 Block *block = NULL;
1157 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001158 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1159 // inlined functions. These will be parsed on their own as separate
1160 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161
1162 if (depth > 0)
1163 break;
1164
1165 block = parent_block;
1166 } else {
1167 BlockSP block_sp(new Block(die.GetID()));
1168 parent_block->AddChild(block_sp);
1169 block = block_sp.get();
1170 }
1171 DWARFRangeList ranges;
1172 const char *name = NULL;
1173 const char *mangled_name = NULL;
1174
1175 int decl_file = 0;
1176 int decl_line = 0;
1177 int decl_column = 0;
1178 int call_file = 0;
1179 int call_line = 0;
1180 int call_column = 0;
1181 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1182 decl_line, decl_column, call_file, call_line,
1183 call_column, nullptr)) {
1184 if (tag == DW_TAG_subprogram) {
1185 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1186 subprogram_low_pc = ranges.GetMinRangeBase(0);
1187 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001188 // We get called here for inlined subroutines in two ways. The first
1189 // time is when we are making the Function object for this inlined
1190 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001192 // need to adjust the containing address. The second time is when we
1193 // are parsing the blocks inside the function that contains the
1194 // inlined concrete instance. Since these will be blocks inside the
1195 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1197 subprogram_low_pc = ranges.GetMinRangeBase(0);
1198 }
1199 }
1200
1201 const size_t num_ranges = ranges.GetSize();
1202 for (size_t i = 0; i < num_ranges; ++i) {
1203 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1204 const addr_t range_base = range.GetRangeBase();
1205 if (range_base >= subprogram_low_pc)
1206 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1207 range.GetByteSize()));
1208 else {
1209 GetObjectFile()->GetModule()->ReportError(
1210 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1211 ") which has a base that is less than the function's low PC "
1212 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1213 "start of this error message",
1214 block->GetID(), range_base, range.GetRangeEnd(),
1215 subprogram_low_pc);
1216 }
1217 }
1218 block->FinalizeRanges();
1219
1220 if (tag != DW_TAG_subprogram &&
1221 (name != NULL || mangled_name != NULL)) {
1222 std::unique_ptr<Declaration> decl_ap;
1223 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1224 decl_ap.reset(new Declaration(
1225 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1226 decl_line, decl_column));
1227
1228 std::unique_ptr<Declaration> call_ap;
1229 if (call_file != 0 || call_line != 0 || call_column != 0)
1230 call_ap.reset(new Declaration(
1231 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1232 call_line, call_column));
1233
1234 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1235 call_ap.get());
1236 }
1237
1238 ++blocks_added;
1239
1240 if (die.HasChildren()) {
1241 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1242 subprogram_low_pc, depth + 1);
1243 }
1244 }
1245 } break;
1246 default:
1247 break;
1248 }
1249
Adrian Prantl05097242018-04-30 16:49:04 +00001250 // Only parse siblings of the block if we are not at depth zero. A depth of
1251 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1252 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253
1254 if (depth == 0)
1255 die.Clear();
1256 else
1257 die = die.GetSibling();
1258 }
1259 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001260}
1261
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1263 if (parent_die) {
1264 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1265 die = die.GetSibling()) {
1266 dw_tag_t tag = die.Tag();
1267 bool check_virtuality = false;
1268 switch (tag) {
1269 case DW_TAG_inheritance:
1270 case DW_TAG_subprogram:
1271 check_virtuality = true;
1272 break;
1273 default:
1274 break;
1275 }
1276 if (check_virtuality) {
1277 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1278 return true;
1279 }
1280 }
1281 }
1282 return false;
1283}
1284
1285void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1286 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1287 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1288 std::vector<DWARFDIE> decl_ctx_die_list =
1289 ast_parser->GetDIEForDeclContext(decl_ctx);
1290
1291 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1292 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1293 decl = decl.GetSibling())
1294 ast_parser->GetDeclForUIDFromDWARF(decl);
1295}
1296
1297SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001298 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1299 // must make sure we use the correct DWARF file when resolving things. On
1300 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1301 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1302 // references to other DWARF objects and we must be ready to receive a
1303 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 // instance.
1305 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1306 if (debug_map)
1307 return debug_map->GetSymbolFileByOSOIndex(
1308 debug_map->GetOSOIndexFromUserID(uid));
1309 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001310}
1311
1312DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001314 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1315 // must make sure we use the correct DWARF file when resolving things. On
1316 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1317 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1318 // references to other DWARF objects and we must be ready to receive a
1319 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 // instance.
1321 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1322 if (dwarf)
1323 return dwarf->GetDIE(DIERef(uid, dwarf));
1324 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001325}
1326
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001328 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1329 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1330 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 DWARFDIE die = GetDIEFromUID(type_uid);
1332 if (die)
1333 return die.GetDecl();
1334 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001335}
1336
Greg Clayton99558cc42015-08-24 23:46:31 +00001337CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001339 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1340 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1341 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 DWARFDIE die = GetDIEFromUID(type_uid);
1343 if (die)
1344 return die.GetDeclContext();
1345 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001346}
1347
Greg Clayton99558cc42015-08-24 23:46:31 +00001348CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001350 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1351 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1352 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 DWARFDIE die = GetDIEFromUID(type_uid);
1354 if (die)
1355 return die.GetContainingDeclContext();
1356 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357}
1358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001360 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1361 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1362 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 DWARFDIE type_die = GetDIEFromUID(type_uid);
1364 if (type_die)
1365 return type_die.ResolveType();
1366 else
1367 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368}
1369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1371 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001372}
1373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1375 bool assert_not_being_parsed) {
1376 if (die) {
1377 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1378 if (log)
1379 GetObjectFile()->GetModule()->LogMessage(
1380 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1381 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001382
Adrian Prantl05097242018-04-30 16:49:04 +00001383 // We might be coming in in the middle of a type tree (a class within a
1384 // class, an enum within a class), so parse any needed parent DIEs before
1385 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1387 if (decl_ctx_die) {
1388 if (log) {
1389 switch (decl_ctx_die.Tag()) {
1390 case DW_TAG_structure_type:
1391 case DW_TAG_union_type:
1392 case DW_TAG_class_type: {
1393 // Get the type, which could be a forward declaration
1394 if (log)
1395 GetObjectFile()->GetModule()->LogMessage(
1396 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1397 "resolve parent forward type for 0x%8.8x",
1398 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1399 decl_ctx_die.GetOffset());
1400 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001401
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 default:
1403 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407 return ResolveType(die);
1408 }
1409 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001410}
1411
Greg Clayton6beaaa62011-01-17 03:46:26 +00001412// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001413// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1414// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415bool SymbolFileDWARF::HasForwardDeclForClangType(
1416 const CompilerType &compiler_type) {
1417 CompilerType compiler_type_no_qualifiers =
1418 ClangUtil::RemoveFastQualifiers(compiler_type);
1419 if (GetForwardDeclClangTypeToDie().count(
1420 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1421 return true;
1422 }
1423 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 ClangASTContext *clang_type_system =
1426 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1427 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001428 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 DWARFASTParserClang *ast_parser =
1430 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1431 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001432}
1433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1435 std::lock_guard<std::recursive_mutex> guard(
1436 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 ClangASTContext *clang_type_system =
1439 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1440 if (clang_type_system) {
1441 DWARFASTParserClang *ast_parser =
1442 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1443 if (ast_parser &&
1444 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1445 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1446 }
Jim Inghamc3549282012-01-11 02:21:12 +00001447
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 // We have a struct/union/class/enum that needs to be fully resolved.
1449 CompilerType compiler_type_no_qualifiers =
1450 ClangUtil::RemoveFastQualifiers(compiler_type);
1451 auto die_it = GetForwardDeclClangTypeToDie().find(
1452 compiler_type_no_qualifiers.GetOpaqueQualType());
1453 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1454 // We have already resolved this type...
1455 return true;
1456 }
1457
1458 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1459 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001460 // Once we start resolving this type, remove it from the forward
1461 // declaration map in case anyone child members or other types require this
1462 // type to get resolved. The type will get resolved when all of the calls
1463 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464 GetForwardDeclClangTypeToDie().erase(die_it);
1465
1466 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1467
1468 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1469 DWARF_LOG_TYPE_COMPLETION));
1470 if (log)
1471 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1472 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1473 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1474 type->GetName().AsCString());
1475 assert(compiler_type);
1476 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1477 if (dwarf_ast)
1478 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1479 }
1480 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001481}
1482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1484 bool assert_not_being_parsed,
1485 bool resolve_function_context) {
1486 if (die) {
1487 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1488
1489 if (assert_not_being_parsed) {
1490 if (type != DIE_IS_BEING_PARSED)
1491 return type;
1492
1493 GetObjectFile()->GetModule()->ReportError(
1494 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1495 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1496
1497 } else
1498 return type;
1499 }
1500 return nullptr;
1501}
1502
1503CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001504SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 uint32_t cu_idx) {
1506 // Check if the symbol vendor already knows about this compile unit?
1507 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001508 // The symbol vendor doesn't know about this compile unit, we need to parse
1509 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1511 }
1512 return (CompileUnit *)dwarf_cu->GetUserData();
1513}
1514
1515size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1516 DIEArray &method_die_offsets) {
1517 method_die_offsets.clear();
Pavel Labathb13f0332018-05-21 14:12:52 +00001518 m_index->GetObjCMethods(class_name, method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 return method_die_offsets.size();
1520}
1521
1522bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1523 sc.Clear(false);
1524
1525 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001526 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1528
1529 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1530 if (sc.function == NULL)
1531 sc.function = ParseCompileUnitFunction(sc, die);
1532
1533 if (sc.function) {
1534 sc.module_sp = sc.function->CalculateSymbolContextModule();
1535 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 }
1538
1539 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540}
1541
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1543 UpdateExternalModuleListIfNeeded();
1544 const auto &pos = m_external_type_modules.find(name);
1545 if (pos != m_external_type_modules.end())
1546 return pos->second;
1547 else
1548 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001549}
1550
Greg Clayton2f869fe2016-03-30 20:14:35 +00001551DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1553 DWARFDebugInfo *debug_info = DebugInfo();
1554 if (debug_info)
1555 return debug_info->GetDIE(die_ref);
1556 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001557 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001558}
1559
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560std::unique_ptr<SymbolFileDWARFDwo>
1561SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001562 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001564 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 // info files for modules.
1566 if (GetDebugMapSymfile())
1567 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 const char *dwo_name = cu_die.GetAttributeValueAsString(
1570 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1571 if (!dwo_name)
1572 return nullptr;
1573
Tamas Berghammer963ce482017-08-25 13:56:14 +00001574 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1575 if (dwp_symfile) {
1576 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1577 DW_AT_GNU_dwo_id, 0);
1578 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1579 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1580 if (dwo_symfile)
1581 return dwo_symfile;
1582 }
1583
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584 FileSpec dwo_file(dwo_name, true);
1585 if (dwo_file.IsRelative()) {
1586 const char *comp_dir = cu_die.GetAttributeValueAsString(
1587 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1588 if (!comp_dir)
1589 return nullptr;
1590
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001591 dwo_file.SetFile(comp_dir, true, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 dwo_file.AppendPathComponent(dwo_name);
1593 }
1594
1595 if (!dwo_file.Exists())
1596 return nullptr;
1597
1598 const lldb::offset_t file_offset = 0;
1599 DataBufferSP dwo_file_data_sp;
1600 lldb::offset_t dwo_file_data_offset = 0;
1601 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1602 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1603 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1604 if (dwo_obj_file == nullptr)
1605 return nullptr;
1606
1607 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001608}
1609
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1611 if (m_fetched_external_modules)
1612 return;
1613 m_fetched_external_modules = true;
1614
1615 DWARFDebugInfo *debug_info = DebugInfo();
1616
1617 const uint32_t num_compile_units = GetNumCompileUnits();
1618 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001619 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620
Jan Kratochvil93afb052018-05-24 20:51:13 +00001621 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 if (die && die.HasChildren() == false) {
1623 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1624
1625 if (name) {
1626 ConstString const_name(name);
1627 if (m_external_type_modules.find(const_name) ==
1628 m_external_type_modules.end()) {
1629 ModuleSP module_sp;
1630 const char *dwo_path =
1631 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1632 if (dwo_path) {
1633 ModuleSpec dwo_module_spec;
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001634 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false,
1635 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1637 const char *comp_dir =
1638 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1639 if (comp_dir) {
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001640 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true,
1641 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1643 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001644 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 dwo_module_spec.GetArchitecture() =
1646 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001647
Adrian Prantl05097242018-04-30 16:49:04 +00001648 // When LLDB loads "external" modules it looks at the presence of
1649 // DW_AT_GNU_dwo_name. However, when the already created module
1650 // (corresponding to .dwo itself) is being processed, it will see
1651 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1652 // dwo file) and will try to call ModuleList::GetSharedModule
1653 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1654 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1655 // DW_AT_comp_dir. In this case the method
1656 // ModuleList::GetSharedModule will fail and the warning will be
1657 // printed. However, as one can notice in this case we don't
1658 // actually need to try to load the already loaded module
1659 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001660 if (m_obj_file->GetFileSpec()
1661 .GetFileNameExtension()
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00001662 .GetStringRef() == ".dwo" &&
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001663 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1664 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1665 continue;
1666 }
1667
Zachary Turner97206d52017-05-12 04:51:55 +00001668 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 dwo_module_spec, module_sp, NULL, NULL, NULL);
1670 if (!module_sp) {
1671 GetObjectFile()->GetModule()->ReportWarning(
1672 "0x%8.8x: unable to locate module needed for external types: "
1673 "%s\nerror: %s\nDebugging will be degraded due to missing "
1674 "types. Rebuilding your project will regenerate the needed "
1675 "module files.",
1676 die.GetOffset(),
1677 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1678 error.AsCString("unknown error"));
1679 }
1680 }
1681 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 }
1686}
1687
1688SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1689 if (!m_global_aranges_ap) {
1690 m_global_aranges_ap.reset(new GlobalVariableMap());
1691
1692 ModuleSP module_sp = GetObjectFile()->GetModule();
1693 if (module_sp) {
1694 const size_t num_cus = module_sp->GetNumCompileUnits();
1695 for (size_t i = 0; i < num_cus; ++i) {
1696 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1697 if (cu_sp) {
1698 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1699 if (globals_sp) {
1700 const size_t num_globals = globals_sp->GetSize();
1701 for (size_t g = 0; g < num_globals; ++g) {
1702 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1703 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1704 const DWARFExpression &location = var_sp->LocationExpression();
1705 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001706 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001707 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1708 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 if (location_result.GetValueType() ==
1710 Value::eValueTypeFileAddress) {
1711 lldb::addr_t file_addr =
1712 location_result.GetScalar().ULongLong();
1713 lldb::addr_t byte_size = 1;
1714 if (var_sp->GetType())
1715 byte_size = var_sp->GetType()->GetByteSize();
1716 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1717 file_addr, byte_size, var_sp.get()));
1718 }
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 m_global_aranges_ap->Sort();
1727 }
1728 return *m_global_aranges_ap;
1729}
1730
1731uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1732 uint32_t resolve_scope,
1733 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001734 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1735 Timer scoped_timer(func_cat,
1736 "SymbolFileDWARF::"
1737 "ResolveSymbolContext (so_addr = { "
1738 "section = %p, offset = 0x%" PRIx64
1739 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 static_cast<void *>(so_addr.GetSection().get()),
1741 so_addr.GetOffset(), resolve_scope);
1742 uint32_t resolved = 0;
1743 if (resolve_scope &
1744 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1745 eSymbolContextLineEntry | eSymbolContextVariable)) {
1746 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1747
1748 DWARFDebugInfo *debug_info = DebugInfo();
1749 if (debug_info) {
1750 const dw_offset_t cu_offset =
1751 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1752 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001753 // Global variables are not in the compile unit address ranges. The
1754 // only way to currently find global variables is to iterate over the
1755 // .debug_pubnames or the __apple_names table and find all items in
1756 // there that point to DW_TAG_variable DIEs and then find the address
1757 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 if (resolve_scope & eSymbolContextVariable) {
1759 GlobalVariableMap &map = GetGlobalAranges();
1760 const GlobalVariableMap::Entry *entry =
1761 map.FindEntryThatContains(file_vm_addr);
1762 if (entry && entry->data) {
1763 Variable *variable = entry->data;
1764 SymbolContextScope *scc = variable->GetSymbolContextScope();
1765 if (scc) {
1766 scc->CalculateSymbolContext(&sc);
1767 sc.variable = variable;
1768 }
1769 return sc.GetResolvedMask();
1770 }
1771 }
1772 } else {
1773 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001774 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1776 if (dwarf_cu) {
1777 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1778 if (sc.comp_unit) {
1779 resolved |= eSymbolContextCompUnit;
1780
1781 bool force_check_line_table = false;
1782 if (resolve_scope &
1783 (eSymbolContextFunction | eSymbolContextBlock)) {
1784 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1785 DWARFDIE block_die;
1786 if (function_die) {
1787 sc.function =
1788 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1789 if (sc.function == NULL)
1790 sc.function = ParseCompileUnitFunction(sc, function_die);
1791
1792 if (sc.function && (resolve_scope & eSymbolContextBlock))
1793 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1794 } else {
1795 // We might have had a compile unit that had discontiguous
1796 // address ranges where the gaps are symbols that don't have
1797 // any debug info. Discontiguous compile unit address ranges
1798 // should only happen when there aren't other functions from
1799 // other compile units in these gaps. This helps keep the size
1800 // of the aranges down.
1801 force_check_line_table = true;
1802 }
1803
1804 if (sc.function != NULL) {
1805 resolved |= eSymbolContextFunction;
1806
1807 if (resolve_scope & eSymbolContextBlock) {
1808 Block &block = sc.function->GetBlock(true);
1809
1810 if (block_die)
1811 sc.block = block.FindBlockByID(block_die.GetID());
1812 else
1813 sc.block = block.FindBlockByID(function_die.GetID());
1814 if (sc.block)
1815 resolved |= eSymbolContextBlock;
1816 }
1817 }
1818 }
1819
1820 if ((resolve_scope & eSymbolContextLineEntry) ||
1821 force_check_line_table) {
1822 LineTable *line_table = sc.comp_unit->GetLineTable();
1823 if (line_table != NULL) {
1824 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001825 // terms of this debug file if there is no debug map, or it
1826 // will be an address in the .o file which needs to be fixed up
1827 // to be in terms of the debug map executable. Either way,
1828 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001829 Address exe_so_addr(so_addr);
1830 if (FixupAddress(exe_so_addr)) {
1831 if (line_table->FindLineEntryByAddress(exe_so_addr,
1832 sc.line_entry)) {
1833 resolved |= eSymbolContextLineEntry;
1834 }
1835 }
1836 }
1837 }
1838
1839 if (force_check_line_table &&
1840 !(resolved & eSymbolContextLineEntry)) {
1841 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001842 // address ranges where the gaps are symbols that don't have any
1843 // debug info. Discontiguous compile unit address ranges should
1844 // only happen when there aren't other functions from other
1845 // compile units in these gaps. This helps keep the size of the
1846 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001847 sc.comp_unit = NULL;
1848 resolved &= ~eSymbolContextCompUnit;
1849 }
1850 } else {
1851 GetObjectFile()->GetModule()->ReportWarning(
1852 "0x%8.8x: compile unit %u failed to create a valid "
1853 "lldb_private::CompileUnit class.",
1854 cu_offset, cu_idx);
1855 }
1856 }
1857 }
1858 }
1859 }
1860 return resolved;
1861}
1862
1863uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1864 uint32_t line,
1865 bool check_inlines,
1866 uint32_t resolve_scope,
1867 SymbolContextList &sc_list) {
1868 const uint32_t prev_size = sc_list.GetSize();
1869 if (resolve_scope & eSymbolContextCompUnit) {
1870 DWARFDebugInfo *debug_info = DebugInfo();
1871 if (debug_info) {
1872 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001873 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874
1875 for (cu_idx = 0;
1876 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1877 ++cu_idx) {
1878 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1879 const bool full_match = (bool)file_spec.GetDirectory();
1880 bool file_spec_matches_cu_file_spec =
1881 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1882 if (check_inlines || file_spec_matches_cu_file_spec) {
1883 SymbolContext sc(m_obj_file->GetModule());
1884 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1885 if (sc.comp_unit) {
1886 uint32_t file_idx = UINT32_MAX;
1887
Adrian Prantl05097242018-04-30 16:49:04 +00001888 // If we are looking for inline functions only and we don't find it
1889 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 if (check_inlines) {
1891 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1892 1, file_spec, true);
1893 if (file_idx == UINT32_MAX)
1894 continue;
1895 }
1896
1897 if (line != 0) {
1898 LineTable *line_table = sc.comp_unit->GetLineTable();
1899
1900 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001901 // We will have already looked up the file index if we are
1902 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 if (!check_inlines)
1904 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1905 1, file_spec, true);
1906
1907 if (file_idx != UINT32_MAX) {
1908 uint32_t found_line;
1909 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1910 0, file_idx, line, false, &sc.line_entry);
1911 found_line = sc.line_entry.line;
1912
1913 while (line_idx != UINT32_MAX) {
1914 sc.function = NULL;
1915 sc.block = NULL;
1916 if (resolve_scope &
1917 (eSymbolContextFunction | eSymbolContextBlock)) {
1918 const lldb::addr_t file_vm_addr =
1919 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1920 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1921 DWARFDIE function_die =
1922 dwarf_cu->LookupAddress(file_vm_addr);
1923 DWARFDIE block_die;
1924 if (function_die) {
1925 sc.function =
1926 sc.comp_unit
1927 ->FindFunctionByUID(function_die.GetID())
1928 .get();
1929 if (sc.function == NULL)
1930 sc.function =
1931 ParseCompileUnitFunction(sc, function_die);
1932
1933 if (sc.function &&
1934 (resolve_scope & eSymbolContextBlock))
1935 block_die =
1936 function_die.LookupDeepestBlock(file_vm_addr);
1937 }
1938
1939 if (sc.function != NULL) {
1940 Block &block = sc.function->GetBlock(true);
1941
1942 if (block_die)
1943 sc.block = block.FindBlockByID(block_die.GetID());
1944 else if (function_die)
1945 sc.block =
1946 block.FindBlockByID(function_die.GetID());
1947 }
1948 }
1949 }
1950
1951 sc_list.Append(sc);
1952 line_idx = line_table->FindLineEntryIndexByFileIndex(
1953 line_idx + 1, file_idx, found_line, true,
1954 &sc.line_entry);
1955 }
1956 }
1957 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1958 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001959 // sites by file and line and if the file spec matches that of
1960 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961 sc_list.Append(sc);
1962 }
1963 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1964 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001965 // sites by file and line and if the file spec matches that of
1966 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001967 sc_list.Append(sc);
1968 }
1969
1970 if (!check_inlines)
1971 break;
1972 }
1973 }
1974 }
1975 }
1976 }
1977 return sc_list.GetSize() - prev_size;
1978}
1979
Jim Ingham7fca8c02017-04-28 00:51:06 +00001980void SymbolFileDWARF::PreloadSymbols() {
1981 std::lock_guard<std::recursive_mutex> guard(
1982 GetObjectFile()->GetModule()->GetMutex());
Pavel Labathb13f0332018-05-21 14:12:52 +00001983 m_index->Preload();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001984}
1985
1986bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
1987 const lldb_private::CompilerDeclContext *decl_ctx) {
1988 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001989 // Invalid namespace decl which means we aren't matching only things in
1990 // this symbol file, so return true to indicate it matches this symbol
1991 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001992 return true;
1993 }
1994
1995 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1996 TypeSystem *type_system = GetTypeSystemForLanguage(
1997 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1998 if (decl_ctx_type_system == type_system)
1999 return true; // The type systems match, return true
2000
2001 // The namespace AST was valid, and it does not match...
2002 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2003
2004 if (log)
2005 GetObjectFile()->GetModule()->LogMessage(
2006 log, "Valid namespace does not match symbol file");
2007
2008 return false;
2009}
2010
2011uint32_t SymbolFileDWARF::FindGlobalVariables(
2012 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +00002013 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002014 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2015
2016 if (log)
2017 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002018 log,
2019 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2020 "parent_decl_ctx=%p, max_matches=%u, variables)",
2021 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022 max_matches);
2023
2024 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2025 return 0;
2026
2027 DWARFDebugInfo *info = DebugInfo();
2028 if (info == NULL)
2029 return 0;
2030
Pavel Labath34cda142018-05-31 09:46:26 +00002031 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002032 const uint32_t original_size = variables.GetSize();
2033
Pavel Labathe1d18752018-06-07 10:04:44 +00002034 llvm::StringRef basename;
2035 llvm::StringRef context;
2036
2037 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2038 context, basename))
2039 basename = name.GetStringRef();
2040
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 DIEArray die_offsets;
Pavel Labathe1d18752018-06-07 10:04:44 +00002042 m_index->GetGlobalVariables(ConstString(basename), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002043 const size_t num_die_matches = die_offsets.size();
2044 if (num_die_matches) {
2045 SymbolContext sc;
2046 sc.module_sp = m_obj_file->GetModule();
2047 assert(sc.module_sp);
2048
Pavel Labathe1d18752018-06-07 10:04:44 +00002049 // Loop invariant: Variables up to this index have been checked for context
2050 // matches.
2051 uint32_t pruned_idx = original_size;
2052
Kate Stoneb9c1b512016-09-06 20:57:50 +00002053 bool done = false;
2054 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2055 const DIERef &die_ref = die_offsets[i];
2056 DWARFDIE die = GetDIE(die_ref);
2057
2058 if (die) {
2059 switch (die.Tag()) {
2060 default:
2061 case DW_TAG_subprogram:
2062 case DW_TAG_inlined_subroutine:
2063 case DW_TAG_try_block:
2064 case DW_TAG_catch_block:
2065 break;
2066
2067 case DW_TAG_variable: {
2068 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2069
2070 if (parent_decl_ctx) {
2071 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2072 if (dwarf_ast) {
2073 CompilerDeclContext actual_parent_decl_ctx =
2074 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2075 if (!actual_parent_decl_ctx ||
2076 actual_parent_decl_ctx != *parent_decl_ctx)
2077 continue;
2078 }
2079 }
2080
2081 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2082 &variables);
Pavel Labathe1d18752018-06-07 10:04:44 +00002083 while (pruned_idx < variables.GetSize()) {
2084 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2085 if (var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2086 ++pruned_idx;
2087 else
2088 variables.RemoveVariableAtIndex(pruned_idx);
2089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002090
2091 if (variables.GetSize() - original_size >= max_matches)
2092 done = true;
2093 } break;
2094 }
2095 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002096 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2097 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 }
2099 }
2100 }
2101
2102 // Return the number of variable that were appended to the list
2103 const uint32_t num_matches = variables.GetSize() - original_size;
2104 if (log && num_matches > 0) {
2105 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002106 log,
2107 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2108 "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2109 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002110 max_matches, num_matches);
2111 }
2112 return num_matches;
2113}
2114
2115uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00002116 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117 VariableList &variables) {
2118 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2119
2120 if (log) {
2121 GetObjectFile()->GetModule()->LogMessage(
Pavel Labath34cda142018-05-31 09:46:26 +00002122 log,
2123 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2124 "max_matches=%u, variables)",
2125 regex.GetText().str().c_str(), max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 }
2127
2128 DWARFDebugInfo *info = DebugInfo();
2129 if (info == NULL)
2130 return 0;
2131
Pavel Labath34cda142018-05-31 09:46:26 +00002132 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133 const uint32_t original_size = variables.GetSize();
2134
2135 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002136 m_index->GetGlobalVariables(regex, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137
2138 SymbolContext sc;
2139 sc.module_sp = m_obj_file->GetModule();
2140 assert(sc.module_sp);
2141
2142 const size_t num_matches = die_offsets.size();
2143 if (num_matches) {
2144 for (size_t i = 0; i < num_matches; ++i) {
2145 const DIERef &die_ref = die_offsets[i];
2146 DWARFDIE die = GetDIE(die_ref);
2147
2148 if (die) {
2149 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2150
2151 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2152
2153 if (variables.GetSize() - original_size >= max_matches)
2154 break;
Pavel Labathb13f0332018-05-21 14:12:52 +00002155 } else
2156 m_index->ReportInvalidDIEOffset(die_ref.die_offset, regex.GetText());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 }
2158 }
2159
2160 // Return the number of variable that were appended to the list
2161 return variables.GetSize() - original_size;
2162}
2163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2165 bool include_inlines,
2166 SymbolContextList &sc_list) {
2167 SymbolContext sc;
2168
2169 if (!orig_die)
2170 return false;
2171
2172 // If we were passed a die that is not a function, just return false...
2173 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2174 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2175 return false;
2176
2177 DWARFDIE die = orig_die;
2178 DWARFDIE inlined_die;
2179 if (die.Tag() == DW_TAG_inlined_subroutine) {
2180 inlined_die = die;
2181
2182 while (1) {
2183 die = die.GetParent();
2184
2185 if (die) {
2186 if (die.Tag() == DW_TAG_subprogram)
2187 break;
2188 } else
2189 break;
2190 }
2191 }
2192 assert(die && die.Tag() == DW_TAG_subprogram);
2193 if (GetFunction(die, sc)) {
2194 Address addr;
2195 // Parse all blocks if needed
2196 if (inlined_die) {
2197 Block &function_block = sc.function->GetBlock(true);
2198 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2199 if (sc.block == NULL)
2200 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2201 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2202 addr.Clear();
2203 } else {
2204 sc.block = NULL;
2205 addr = sc.function->GetAddressRange().GetBaseAddress();
2206 }
2207
2208 if (addr.IsValid()) {
2209 sc_list.Append(sc);
2210 return true;
2211 }
2212 }
2213
2214 return false;
2215}
2216
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2218 const DWARFDIE &die) {
2219 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002220 // parent decl context isn't valid, we aren't trying to look for any
2221 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2223 return true;
2224
2225 if (die) {
2226 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2227 if (dwarf_ast) {
2228 CompilerDeclContext actual_decl_ctx =
2229 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2230 if (actual_decl_ctx)
2231 return actual_decl_ctx == *decl_ctx;
2232 }
2233 }
2234 return false;
2235}
2236
2237uint32_t
2238SymbolFileDWARF::FindFunctions(const ConstString &name,
2239 const CompilerDeclContext *parent_decl_ctx,
2240 uint32_t name_type_mask, bool include_inlines,
2241 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002242 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2243 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 name.AsCString());
2245
2246 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002247 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2249
2250 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2251
2252 if (log) {
2253 GetObjectFile()->GetModule()->LogMessage(
2254 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2255 "name_type_mask=0x%x, append=%u, sc_list)",
2256 name.GetCString(), name_type_mask, append);
2257 }
2258
2259 // If we aren't appending the results to this list, then clear the list
2260 if (!append)
2261 sc_list.Clear();
2262
2263 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2264 return 0;
2265
2266 // If name is empty then we won't find anything.
2267 if (name.IsEmpty())
2268 return 0;
2269
Adrian Prantl05097242018-04-30 16:49:04 +00002270 // Remember how many sc_list are in the list before we search in case we are
2271 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273 const uint32_t original_size = sc_list.GetSize();
2274
2275 DWARFDebugInfo *info = DebugInfo();
2276 if (info == NULL)
2277 return 0;
2278
Pavel Labath5af11ab2018-06-05 10:33:56 +00002279 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2280 DIEArray offsets;
2281 CompilerDeclContext empty_decl_ctx;
2282 if (!parent_decl_ctx)
2283 parent_decl_ctx = &empty_decl_ctx;
2284
2285 std::vector<DWARFDIE> dies;
2286 m_index->GetFunctions(name, *info, *parent_decl_ctx, name_type_mask, dies);
2287 for (const DWARFDIE &die: dies) {
2288 if (resolved_dies.insert(die.GetDIE()).second)
2289 ResolveFunction(die, include_inlines, sc_list);
2290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291
2292 // Return the number of variable that were appended to the list
2293 const uint32_t num_matches = sc_list.GetSize() - original_size;
2294
2295 if (log && num_matches > 0) {
2296 GetObjectFile()->GetModule()->LogMessage(
2297 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2298 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2299 "%u",
2300 name.GetCString(), name_type_mask, include_inlines, append,
2301 num_matches);
2302 }
2303 return num_matches;
2304}
2305
2306uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2307 bool include_inlines, bool append,
2308 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002309 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2310 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002311 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002312
2313 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2314
2315 if (log) {
2316 GetObjectFile()->GetModule()->LogMessage(
2317 log,
2318 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002319 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002320 }
2321
2322 // If we aren't appending the results to this list, then clear the list
2323 if (!append)
2324 sc_list.Clear();
2325
Pavel Labathb13f0332018-05-21 14:12:52 +00002326 DWARFDebugInfo *info = DebugInfo();
2327 if (!info)
2328 return 0;
2329
Adrian Prantl05097242018-04-30 16:49:04 +00002330 // Remember how many sc_list are in the list before we search in case we are
2331 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332 uint32_t original_size = sc_list.GetSize();
2333
Pavel Labath6de9c792018-06-05 12:13:22 +00002334 DIEArray offsets;
2335 m_index->GetFunctions(regex, offsets);
2336
Pavel Labatha3ee1e72018-06-08 10:31:55 +00002337 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2338 for (DIERef ref : offsets) {
2339 DWARFDIE die = info->GetDIE(ref);
2340 if (!die) {
2341 m_index->ReportInvalidDIEOffset(ref.die_offset, regex.GetText());
2342 continue;
2343 }
2344 if (resolved_dies.insert(die.GetDIE()).second)
2345 ResolveFunction(die, include_inlines, sc_list);
2346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347
2348 // Return the number of variable that were appended to the list
2349 return sc_list.GetSize() - original_size;
2350}
2351
2352void SymbolFileDWARF::GetMangledNamesForFunction(
2353 const std::string &scope_qualified_name,
2354 std::vector<ConstString> &mangled_names) {
2355 DWARFDebugInfo *info = DebugInfo();
2356 uint32_t num_comp_units = 0;
2357 if (info)
2358 num_comp_units = info->GetNumCompileUnits();
2359
2360 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002361 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362 if (cu == nullptr)
2363 continue;
2364
2365 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2366 if (dwo)
2367 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2368 }
2369
2370 NameToOffsetMap::iterator iter =
2371 m_function_scope_qualified_name_map.find(scope_qualified_name);
2372 if (iter == m_function_scope_qualified_name_map.end())
2373 return;
2374
2375 DIERefSetSP set_sp = (*iter).second;
2376 std::set<DIERef>::iterator set_iter;
2377 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2378 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2379 mangled_names.push_back(ConstString(die.GetMangledName()));
2380 }
2381}
2382
2383uint32_t SymbolFileDWARF::FindTypes(
2384 const SymbolContext &sc, const ConstString &name,
2385 const CompilerDeclContext *parent_decl_ctx, bool append,
2386 uint32_t max_matches,
2387 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2388 TypeMap &types) {
2389 // If we aren't appending the results to this list, then clear the list
2390 if (!append)
2391 types.Clear();
2392
2393 // Make sure we haven't already searched this SymbolFile before...
2394 if (searched_symbol_files.count(this))
2395 return 0;
2396 else
2397 searched_symbol_files.insert(this);
2398
2399 DWARFDebugInfo *info = DebugInfo();
2400 if (info == NULL)
2401 return 0;
2402
2403 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2404
2405 if (log) {
2406 if (parent_decl_ctx)
2407 GetObjectFile()->GetModule()->LogMessage(
2408 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2409 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2410 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2411 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2412 else
2413 GetObjectFile()->GetModule()->LogMessage(
2414 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2415 "NULL, append=%u, max_matches=%u, type_list)",
2416 name.GetCString(), append, max_matches);
2417 }
2418
2419 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2420 return 0;
2421
2422 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002423 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 const size_t num_die_matches = die_offsets.size();
2425
2426 if (num_die_matches) {
2427 const uint32_t initial_types_size = types.GetSize();
2428 for (size_t i = 0; i < num_die_matches; ++i) {
2429 const DIERef &die_ref = die_offsets[i];
2430 DWARFDIE die = GetDIE(die_ref);
2431
2432 if (die) {
2433 if (!DIEInDeclContext(parent_decl_ctx, die))
2434 continue; // The containing decl contexts don't match
2435
2436 Type *matching_type = ResolveType(die, true, true);
2437 if (matching_type) {
2438 // We found a type pointer, now find the shared pointer form our type
2439 // list
2440 types.InsertUnique(matching_type->shared_from_this());
2441 if (types.GetSize() >= max_matches)
2442 break;
2443 }
2444 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002445 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2446 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002447 }
2448 }
2449 const uint32_t num_matches = types.GetSize() - initial_types_size;
2450 if (log && num_matches) {
2451 if (parent_decl_ctx) {
2452 GetObjectFile()->GetModule()->LogMessage(
2453 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2454 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2455 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2456 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2457 num_matches);
2458 } else {
2459 GetObjectFile()->GetModule()->LogMessage(
2460 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2461 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2462 name.GetCString(), append, max_matches, num_matches);
2463 }
2464 }
2465 return num_matches;
2466 } else {
2467 UpdateExternalModuleListIfNeeded();
2468
2469 for (const auto &pair : m_external_type_modules) {
2470 ModuleSP external_module_sp = pair.second;
2471 if (external_module_sp) {
2472 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2473 if (sym_vendor) {
2474 const uint32_t num_external_matches =
2475 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2476 max_matches, searched_symbol_files, types);
2477 if (num_external_matches)
2478 return num_external_matches;
2479 }
2480 }
2481 }
2482 }
2483
2484 return 0;
2485}
2486
2487size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2488 bool append, TypeMap &types) {
2489 if (!append)
2490 types.Clear();
2491
2492 if (context.empty())
2493 return 0;
2494
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 ConstString name = context.back().name;
2496
2497 if (!name)
2498 return 0;
2499
Pavel Labathb13f0332018-05-21 14:12:52 +00002500 DIEArray die_offsets;
2501 m_index->GetTypes(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002502 const size_t num_die_matches = die_offsets.size();
2503
2504 if (num_die_matches) {
2505 size_t num_matches = 0;
2506 for (size_t i = 0; i < num_die_matches; ++i) {
2507 const DIERef &die_ref = die_offsets[i];
2508 DWARFDIE die = GetDIE(die_ref);
2509
2510 if (die) {
2511 std::vector<CompilerContext> die_context;
2512 die.GetDWOContext(die_context);
2513 if (die_context != context)
2514 continue;
2515
2516 Type *matching_type = ResolveType(die, true, true);
2517 if (matching_type) {
2518 // We found a type pointer, now find the shared pointer form our type
2519 // list
2520 types.InsertUnique(matching_type->shared_from_this());
2521 ++num_matches;
2522 }
2523 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002524 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2525 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002526 }
2527 }
2528 return num_matches;
2529 }
2530 return 0;
2531}
2532
2533CompilerDeclContext
2534SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2535 const CompilerDeclContext *parent_decl_ctx) {
2536 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2537
2538 if (log) {
2539 GetObjectFile()->GetModule()->LogMessage(
2540 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2541 name.GetCString());
2542 }
2543
2544 CompilerDeclContext namespace_decl_ctx;
2545
2546 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2547 return namespace_decl_ctx;
2548
2549 DWARFDebugInfo *info = DebugInfo();
2550 if (info) {
2551 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002552 m_index->GetNamespaces(name, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002553 const size_t num_matches = die_offsets.size();
2554 if (num_matches) {
2555 for (size_t i = 0; i < num_matches; ++i) {
2556 const DIERef &die_ref = die_offsets[i];
2557 DWARFDIE die = GetDIE(die_ref);
2558
2559 if (die) {
2560 if (!DIEInDeclContext(parent_decl_ctx, die))
2561 continue; // The containing decl contexts don't match
2562
2563 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2564 if (dwarf_ast) {
2565 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2566 if (namespace_decl_ctx)
2567 break;
2568 }
2569 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002570 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2571 name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002572 }
2573 }
2574 }
2575 }
2576 if (log && namespace_decl_ctx) {
2577 GetObjectFile()->GetModule()->LogMessage(
2578 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2579 "CompilerDeclContext(%p/%p) \"%s\"",
2580 name.GetCString(),
2581 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2582 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2583 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2584 }
2585
2586 return namespace_decl_ctx;
2587}
2588
2589TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2590 bool resolve_function_context) {
2591 TypeSP type_sp;
2592 if (die) {
2593 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2594 if (type_ptr == NULL) {
2595 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
2596 assert(lldb_cu);
2597 SymbolContext sc(lldb_cu);
2598 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2599 while (parent_die != nullptr) {
2600 if (parent_die->Tag() == DW_TAG_subprogram)
2601 break;
2602 parent_die = parent_die->GetParent();
2603 }
2604 SymbolContext sc_backup = sc;
2605 if (resolve_function_context && parent_die != nullptr &&
2606 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2607 sc = sc_backup;
2608
2609 type_sp = ParseType(sc, die, NULL);
2610 } else if (type_ptr != DIE_IS_BEING_PARSED) {
2611 // Grab the existing type from the master types lists
2612 type_sp = type_ptr->shared_from_this();
2613 }
2614 }
2615 return type_sp;
2616}
2617
2618DWARFDIE
2619SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2620 if (orig_die) {
2621 DWARFDIE die = orig_die;
2622
2623 while (die) {
2624 // If this is the original DIE that we are searching for a declaration
2625 // for, then don't look in the cache as we don't want our own decl
2626 // context to be our decl context...
2627 if (orig_die != die) {
2628 switch (die.Tag()) {
2629 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002630 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002631 case DW_TAG_namespace:
2632 case DW_TAG_structure_type:
2633 case DW_TAG_union_type:
2634 case DW_TAG_class_type:
2635 case DW_TAG_lexical_block:
2636 case DW_TAG_subprogram:
2637 return die;
Sean Callananb4945782017-04-24 22:11:10 +00002638 case DW_TAG_inlined_subroutine: {
2639 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2640 if (abs_die) {
2641 return abs_die;
2642 }
2643 break;
2644 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002645 default:
2646 break;
2647 }
2648 }
2649
2650 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2651 if (spec_die) {
2652 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2653 if (decl_ctx_die)
2654 return decl_ctx_die;
2655 }
2656
2657 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2658 if (abs_die) {
2659 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2660 if (decl_ctx_die)
2661 return decl_ctx_die;
2662 }
2663
2664 die = die.GetParent();
2665 }
2666 }
2667 return DWARFDIE();
2668}
2669
2670Symbol *
2671SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
2672 Symbol *objc_class_symbol = NULL;
2673 if (m_obj_file) {
2674 Symtab *symtab = m_obj_file->GetSymtab();
2675 if (symtab) {
2676 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2677 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2678 Symtab::eVisibilityAny);
2679 }
2680 }
2681 return objc_class_symbol;
2682}
2683
2684// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00002685// they don't then we can end up looking through all class types for a complete
2686// type and never find the full definition. We need to know if this attribute
2687// is supported, so we determine this here and cache th result. We also need to
2688// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00002689// DWARF file
2690// if we are doing darwin DWARF in .o file debugging.
2691bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002692 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2694 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2695 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2696 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2697 else {
2698 DWARFDebugInfo *debug_info = DebugInfo();
2699 const uint32_t num_compile_units = GetNumCompileUnits();
2700 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002701 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 if (dwarf_cu != cu &&
2703 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2704 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2705 break;
2706 }
2707 }
2708 }
2709 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2710 GetDebugMapSymfile())
2711 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2712 }
2713 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00002714}
Greg Clayton901c5ca2011-12-03 04:40:03 +00002715
2716// This function can be used when a DIE is found that is a forward declaration
2717// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002718TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2719 const DWARFDIE &die, const ConstString &type_name,
2720 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00002721
Kate Stoneb9c1b512016-09-06 20:57:50 +00002722 TypeSP type_sp;
2723
2724 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2725 return type_sp;
2726
2727 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002728 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002729
2730 const size_t num_matches = die_offsets.size();
2731
2732 if (num_matches) {
2733 for (size_t i = 0; i < num_matches; ++i) {
2734 const DIERef &die_ref = die_offsets[i];
2735 DWARFDIE type_die = GetDIE(die_ref);
2736
2737 if (type_die) {
2738 bool try_resolving_type = false;
2739
Adrian Prantl05097242018-04-30 16:49:04 +00002740 // Don't try and resolve the DIE we are looking for with the DIE
2741 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002742 if (type_die != die) {
2743 switch (type_die.Tag()) {
2744 case DW_TAG_class_type:
2745 case DW_TAG_structure_type:
2746 try_resolving_type = true;
2747 break;
2748 default:
2749 break;
2750 }
2751 }
2752
2753 if (try_resolving_type) {
2754 if (must_be_implementation &&
2755 type_die.Supports_DW_AT_APPLE_objc_complete_type())
2756 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2757 DW_AT_APPLE_objc_complete_type, 0);
2758
2759 if (try_resolving_type) {
2760 Type *resolved_type = ResolveType(type_die, false, true);
2761 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2762 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2763 " (cu 0x%8.8" PRIx64 ")\n",
2764 die.GetID(),
2765 m_obj_file->GetFileSpec().GetFilename().AsCString(
2766 "<Unknown>"),
2767 type_die.GetID(), type_cu->GetID());
2768
2769 if (die)
2770 GetDIEToType()[die.GetDIE()] = resolved_type;
2771 type_sp = resolved_type->shared_from_this();
2772 break;
2773 }
2774 }
2775 }
2776 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002777 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2778 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002779 }
2780 }
2781 }
2782 return type_sp;
2783}
Greg Claytona8022fa2012-04-24 21:22:41 +00002784
Greg Clayton80c26302012-02-05 06:12:47 +00002785//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002786// This function helps to ensure that the declaration contexts match for two
2787// different DIEs. Often times debug information will refer to a forward
2788// declaration of a type (the equivalent of "struct my_struct;". There will
2789// often be a declaration of that type elsewhere that has the full definition.
2790// When we go looking for the full type "my_struct", we will find one or more
2791// matches in the accelerator tables and we will then need to make sure the
2792// type was in the same declaration context as the original DIE. This function
2793// can efficiently compare two DIEs and will return true when the declaration
2794// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00002795//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2797 const DWARFDIE &die2) {
2798 if (die1 == die2)
2799 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00002800
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 DWARFDIECollection decl_ctx_1;
2802 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00002803 // The declaration DIE stack is a stack of the declaration context DIEs all
2804 // the way back to the compile unit. If a type "T" is declared inside a class
2805 // "B", and class "B" is declared inside a class "A" and class "A" is in a
2806 // namespace "lldb", and the namespace is in a compile unit, there will be a
2807 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 //
2809 // [0] DW_TAG_class_type for "B"
2810 // [1] DW_TAG_class_type for "A"
2811 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002812 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 //
Adrian Prantl05097242018-04-30 16:49:04 +00002814 // We grab both contexts and make sure that everything matches all the way
2815 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816
2817 // First lets grab the decl contexts for both DIEs
2818 die1.GetDeclContextDIEs(decl_ctx_1);
2819 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00002820 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 const size_t count1 = decl_ctx_1.Size();
2822 const size_t count2 = decl_ctx_2.Size();
2823 if (count1 != count2)
2824 return false;
2825
Adrian Prantl05097242018-04-30 16:49:04 +00002826 // Make sure the DW_TAG values match all the way back up the compile unit. If
2827 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002828 DWARFDIE decl_ctx_die1;
2829 DWARFDIE decl_ctx_die2;
2830 size_t i;
2831 for (i = 0; i < count1; i++) {
2832 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2833 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2834 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2835 return false;
2836 }
Greg Clayton890ff562012-02-02 05:48:16 +00002837#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00002838
Kate Stoneb9c1b512016-09-06 20:57:50 +00002839 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00002840 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2841 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2842 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00002843 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
2844 UNUSED_IF_ASSERT_DISABLED(cu_tag);
2845 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00002846
Greg Clayton890ff562012-02-02 05:48:16 +00002847#endif
Adrian Prantl05097242018-04-30 16:49:04 +00002848 // Always skip the compile unit when comparing by only iterating up to "count
2849 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850 for (i = 0; i < count1 - 1; i++) {
2851 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
2852 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
2853 const char *name1 = decl_ctx_die1.GetName();
2854 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00002855 // If the string was from a DW_FORM_strp, then the pointer will often be
2856 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00002857 if (name1 == name2)
2858 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00002859
Adrian Prantl05097242018-04-30 16:49:04 +00002860 // Name pointers are not equal, so only compare the strings if both are not
2861 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002862 if (name1 && name2) {
2863 // If the strings don't compare, we are done...
2864 if (strcmp(name1, name2) != 0)
2865 return false;
2866 } else {
2867 // One name was NULL while the other wasn't
2868 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00002869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 }
Adrian Prantl05097242018-04-30 16:49:04 +00002871 // We made it through all of the checks and the declaration contexts are
2872 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002873 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00002874}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2877 const DWARFDeclContext &dwarf_decl_ctx) {
2878 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002879
Kate Stoneb9c1b512016-09-06 20:57:50 +00002880 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2881 if (dwarf_decl_ctx_count > 0) {
2882 const ConstString type_name(dwarf_decl_ctx[0].name);
2883 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00002884
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 if (type_name) {
2886 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2887 DWARF_LOG_LOOKUPS));
2888 if (log) {
2889 GetObjectFile()->GetModule()->LogMessage(
2890 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2891 "s, qualified-name='%s')",
2892 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2893 dwarf_decl_ctx.GetQualifiedName());
2894 }
2895
2896 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00002897 m_index->GetTypes(dwarf_decl_ctx, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002898 const size_t num_matches = die_offsets.size();
2899
Adrian Prantl05097242018-04-30 16:49:04 +00002900 // Get the type system that we are looking to find a type for. We will
2901 // use this to ensure any matches we find are in a language that this
2902 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 const LanguageType language = dwarf_decl_ctx.GetLanguage();
2904 TypeSystem *type_system = (language == eLanguageTypeUnknown)
2905 ? nullptr
2906 : GetTypeSystemForLanguage(language);
2907
2908 if (num_matches) {
2909 for (size_t i = 0; i < num_matches; ++i) {
2910 const DIERef &die_ref = die_offsets[i];
2911 DWARFDIE type_die = GetDIE(die_ref);
2912
2913 if (type_die) {
2914 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00002915 // looking for. We don't want to find a "Foo" type from Java if we
2916 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 if (type_system &&
2918 !type_system->SupportsLanguage(type_die.GetLanguage()))
2919 continue;
2920 bool try_resolving_type = false;
2921
2922 // Don't try and resolve the DIE we are looking for with the DIE
2923 // itself!
2924 const dw_tag_t type_tag = type_die.Tag();
2925 // Make sure the tags match
2926 if (type_tag == tag) {
2927 // The tags match, lets try resolving this type
2928 try_resolving_type = true;
2929 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00002930 // The tags don't match, but we need to watch our for a forward
2931 // declaration for a struct and ("struct foo") ends up being a
2932 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002933 switch (type_tag) {
2934 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002935 // We had a "class foo", see if we ended up with a "struct foo
2936 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002937 try_resolving_type = (tag == DW_TAG_structure_type);
2938 break;
2939 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00002940 // We had a "struct foo", see if we ended up with a "class foo
2941 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00002942 try_resolving_type = (tag == DW_TAG_class_type);
2943 break;
2944 default:
Adrian Prantl05097242018-04-30 16:49:04 +00002945 // Tags don't match, don't event try to resolve using this type
2946 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00002947 break;
2948 }
2949 }
2950
2951 if (try_resolving_type) {
2952 DWARFDeclContext type_dwarf_decl_ctx;
2953 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
2954
2955 if (log) {
2956 GetObjectFile()->GetModule()->LogMessage(
2957 log, "SymbolFileDWARF::"
2958 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2959 "qualified-name='%s') trying die=0x%8.8x (%s)",
2960 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2961 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2962 type_dwarf_decl_ctx.GetQualifiedName());
2963 }
2964
2965 // Make sure the decl contexts match all the way up
2966 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
2967 Type *resolved_type = ResolveType(type_die, false);
2968 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
2969 type_sp = resolved_type->shared_from_this();
2970 break;
2971 }
2972 }
2973 } else {
2974 if (log) {
2975 std::string qualified_name;
2976 type_die.GetQualifiedName(qualified_name);
2977 GetObjectFile()->GetModule()->LogMessage(
2978 log, "SymbolFileDWARF::"
2979 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2980 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2981 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2982 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2983 qualified_name.c_str());
2984 }
2985 }
2986 } else {
Pavel Labathb13f0332018-05-21 14:12:52 +00002987 m_index->ReportInvalidDIEOffset(die_ref.die_offset,
2988 type_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002989 }
2990 }
2991 }
Greg Claytona8022fa2012-04-24 21:22:41 +00002992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002993 }
2994 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00002995}
2996
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2998 bool *type_is_new_ptr) {
2999 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 if (die) {
3002 TypeSystem *type_system =
3003 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003004
Kate Stoneb9c1b512016-09-06 20:57:50 +00003005 if (type_system) {
3006 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3007 if (dwarf_ast) {
3008 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3009 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3010 if (type_sp) {
3011 TypeList *type_list = GetTypeList();
3012 if (type_list)
3013 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003014
Kate Stoneb9c1b512016-09-06 20:57:50 +00003015 if (die.Tag() == DW_TAG_subprogram) {
3016 DIERef die_ref = die.GetDIERef();
3017 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3018 .GetScopeQualifiedName()
3019 .AsCString(""));
3020 if (scope_qualified_name.size()) {
3021 NameToOffsetMap::iterator iter =
3022 m_function_scope_qualified_name_map.find(
3023 scope_qualified_name);
3024 if (iter != m_function_scope_qualified_name_map.end())
3025 (*iter).second->insert(die_ref);
3026 else {
3027 DIERefSetSP new_set(new std::set<DIERef>);
3028 new_set->insert(die_ref);
3029 m_function_scope_qualified_name_map.emplace(
3030 std::make_pair(scope_qualified_name, new_set));
3031 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003034 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003035 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037 }
3038
3039 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040}
3041
Kate Stoneb9c1b512016-09-06 20:57:50 +00003042size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3043 const DWARFDIE &orig_die,
3044 bool parse_siblings, bool parse_children) {
3045 size_t types_added = 0;
3046 DWARFDIE die = orig_die;
3047 while (die) {
3048 bool type_is_new = false;
3049 if (ParseType(sc, die, &type_is_new).get()) {
3050 if (type_is_new)
3051 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053
3054 if (parse_children && die.HasChildren()) {
3055 if (die.Tag() == DW_TAG_subprogram) {
3056 SymbolContext child_sc(sc);
3057 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3058 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3059 } else
3060 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3061 }
3062
3063 if (parse_siblings)
3064 die = die.GetSibling();
3065 else
3066 die.Clear();
3067 }
3068 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003069}
3070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3072 assert(sc.comp_unit && sc.function);
3073 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003074 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003075 if (dwarf_cu) {
3076 const dw_offset_t function_die_offset = sc.function->GetID();
3077 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3078 if (function_die) {
3079 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3080 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003083
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003085}
3086
Kate Stoneb9c1b512016-09-06 20:57:50 +00003087size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3088 // At least a compile unit must be valid
3089 assert(sc.comp_unit);
3090 size_t types_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003091 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003092 if (dwarf_cu) {
3093 if (sc.function) {
3094 dw_offset_t function_die_offset = sc.function->GetID();
3095 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3096 if (func_die && func_die.HasChildren()) {
3097 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3098 }
3099 } else {
3100 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3101 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3102 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003105 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003106
Kate Stoneb9c1b512016-09-06 20:57:50 +00003107 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003108}
3109
Kate Stoneb9c1b512016-09-06 20:57:50 +00003110size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3111 if (sc.comp_unit != NULL) {
3112 DWARFDebugInfo *info = DebugInfo();
3113 if (info == NULL)
3114 return 0;
3115
3116 if (sc.function) {
3117 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3118
3119 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3120 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3121 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3122 const size_t num_variables = ParseVariables(
3123 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3124
3125 // Let all blocks know they have parse all their variables
3126 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3127 return num_variables;
3128 }
3129 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003130 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003131
3132 if (dwarf_cu == NULL)
3133 return 0;
3134
3135 uint32_t vars_added = 0;
3136 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3137
3138 if (variables.get() == NULL) {
3139 variables.reset(new VariableList());
3140 sc.comp_unit->SetVariableList(variables);
3141
3142 DIEArray die_offsets;
Pavel Labathb13f0332018-05-21 14:12:52 +00003143 m_index->GetGlobalVariables(*dwarf_cu, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 const size_t num_matches = die_offsets.size();
3145 if (num_matches) {
3146 for (size_t i = 0; i < num_matches; ++i) {
3147 const DIERef &die_ref = die_offsets[i];
3148 DWARFDIE die = GetDIE(die_ref);
3149 if (die) {
3150 VariableSP var_sp(
3151 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3152 if (var_sp) {
3153 variables->AddVariableIfUnique(var_sp);
3154 ++vars_added;
3155 }
Pavel Labathb13f0332018-05-21 14:12:52 +00003156 } else
3157 m_index->ReportInvalidDIEOffset(die_ref.die_offset, "");
Kate Stoneb9c1b512016-09-06 20:57:50 +00003158 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 }
3161 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163 }
3164 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003165}
3166
Kate Stoneb9c1b512016-09-06 20:57:50 +00003167VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3168 const DWARFDIE &die,
3169 const lldb::addr_t func_low_pc) {
3170 if (die.GetDWARF() != this)
3171 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003172
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 VariableSP var_sp;
3174 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003175 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003176
Kate Stoneb9c1b512016-09-06 20:57:50 +00003177 var_sp = GetDIEToVariable()[die.GetDIE()];
3178 if (var_sp)
3179 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003180
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 const dw_tag_t tag = die.Tag();
3182 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003183
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3185 (tag == DW_TAG_formal_parameter && sc.function)) {
3186 DWARFAttributes attributes;
3187 const size_t num_attributes = die.GetAttributes(attributes);
3188 DWARFDIE spec_die;
3189 if (num_attributes > 0) {
3190 const char *name = NULL;
3191 const char *mangled = NULL;
3192 Declaration decl;
3193 uint32_t i;
3194 DWARFFormValue type_die_form;
3195 DWARFExpression location(die.GetCU());
3196 bool is_external = false;
3197 bool is_artificial = false;
3198 bool location_is_const_value_data = false;
3199 bool has_explicit_location = false;
3200 DWARFFormValue const_value;
3201 Variable::RangeList scope_ranges;
3202 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003203
Kate Stoneb9c1b512016-09-06 20:57:50 +00003204 for (i = 0; i < num_attributes; ++i) {
3205 dw_attr_t attr = attributes.AttributeAtIndex(i);
3206 DWARFFormValue form_value;
3207
3208 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3209 switch (attr) {
3210 case DW_AT_decl_file:
3211 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3212 form_value.Unsigned()));
3213 break;
3214 case DW_AT_decl_line:
3215 decl.SetLine(form_value.Unsigned());
3216 break;
3217 case DW_AT_decl_column:
3218 decl.SetColumn(form_value.Unsigned());
3219 break;
3220 case DW_AT_name:
3221 name = form_value.AsCString();
3222 break;
3223 case DW_AT_linkage_name:
3224 case DW_AT_MIPS_linkage_name:
3225 mangled = form_value.AsCString();
3226 break;
3227 case DW_AT_type:
3228 type_die_form = form_value;
3229 break;
3230 case DW_AT_external:
3231 is_external = form_value.Boolean();
3232 break;
3233 case DW_AT_const_value:
3234 // If we have already found a DW_AT_location attribute, ignore this
3235 // attribute.
3236 if (!has_explicit_location) {
3237 location_is_const_value_data = true;
3238 // The constant value will be either a block, a data value or a
3239 // string.
Greg Claytonf56c30d2018-05-09 16:42:53 +00003240 auto debug_info_data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3242 // Retrieve the value as a block expression.
3243 uint32_t block_offset =
3244 form_value.BlockData() - debug_info_data.GetDataStart();
3245 uint32_t block_length = form_value.Unsigned();
3246 location.CopyOpcodeData(module, debug_info_data, block_offset,
3247 block_length);
3248 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3249 // Retrieve the value as a data expression.
3250 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3251 DWARFFormValue::GetFixedFormSizesForAddressSize(
3252 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3253 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3254 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3255 uint32_t data_length =
3256 fixed_form_sizes.GetSize(form_value.Form());
3257 if (data_length == 0) {
3258 const uint8_t *data_pointer = form_value.BlockData();
3259 if (data_pointer) {
3260 form_value.Unsigned();
3261 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3262 // we need to get the byte size of the type later after we
3263 // create the variable
3264 const_value = form_value;
3265 }
3266 } else
3267 location.CopyOpcodeData(module, debug_info_data, data_offset,
3268 data_length);
3269 } else {
3270 // Retrieve the value as a string expression.
3271 if (form_value.Form() == DW_FORM_strp) {
3272 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3273 DWARFFormValue::GetFixedFormSizesForAddressSize(
3274 attributes.CompileUnitAtIndex(i)
3275 ->GetAddressByteSize(),
3276 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3277 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3278 uint32_t data_length =
3279 fixed_form_sizes.GetSize(form_value.Form());
3280 location.CopyOpcodeData(module, debug_info_data, data_offset,
3281 data_length);
3282 } else {
3283 const char *str = form_value.AsCString();
3284 uint32_t string_offset =
3285 str - (const char *)debug_info_data.GetDataStart();
3286 uint32_t string_length = strlen(str) + 1;
3287 location.CopyOpcodeData(module, debug_info_data,
3288 string_offset, string_length);
3289 }
3290 }
3291 }
3292 break;
3293 case DW_AT_location: {
3294 location_is_const_value_data = false;
3295 has_explicit_location = true;
3296 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
Greg Claytonf56c30d2018-05-09 16:42:53 +00003297 auto data = die.GetData();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298
3299 uint32_t block_offset =
Greg Claytonf56c30d2018-05-09 16:42:53 +00003300 form_value.BlockData() - data.GetDataStart();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003301 uint32_t block_length = form_value.Unsigned();
Greg Claytonf56c30d2018-05-09 16:42:53 +00003302 location.CopyOpcodeData(module, data, block_offset, block_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003303 } else {
3304 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3305 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3306
3307 size_t loc_list_length = DWARFExpression::LocationListSize(
3308 die.GetCU(), debug_loc_data, debug_loc_offset);
3309 if (loc_list_length > 0) {
3310 location.CopyOpcodeData(module, debug_loc_data,
3311 debug_loc_offset, loc_list_length);
3312 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3313 location.SetLocationListSlide(
3314 func_low_pc -
3315 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3316 }
3317 }
3318 } break;
3319 case DW_AT_specification:
3320 spec_die = GetDIE(DIERef(form_value));
3321 break;
3322 case DW_AT_start_scope: {
3323 if (form_value.Form() == DW_FORM_sec_offset) {
3324 DWARFRangeList dwarf_scope_ranges;
3325 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003326 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3327 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003328 dwarf_scope_ranges);
3329
3330 // All DW_AT_start_scope are relative to the base address of the
3331 // compile unit. We add the compile unit base address to make
3332 // sure all the addresses are properly fixed up.
3333 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3334 i < count; ++i) {
3335 const DWARFRangeList::Entry &range =
3336 dwarf_scope_ranges.GetEntryRef(i);
3337 scope_ranges.Append(range.GetRangeBase() +
3338 die.GetCU()->GetBaseAddress(),
3339 range.GetByteSize());
3340 }
3341 } else {
3342 // TODO: Handle the case when DW_AT_start_scope have form
3343 // constant. The
3344 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003345 // behavior in case the enclosing block have a non coninious
3346 // address range and the DW_AT_start_scope entry have a form
3347 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003348 GetObjectFile()->GetModule()->ReportWarning(
3349 "0x%8.8" PRIx64
3350 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3351 die.GetID(), form_value.Form());
3352 }
3353
3354 scope_ranges.Sort();
3355 scope_ranges.CombineConsecutiveRanges();
3356 } break;
3357 case DW_AT_artificial:
3358 is_artificial = form_value.Boolean();
3359 break;
3360 case DW_AT_accessibility:
3361 break; // accessibility =
3362 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3363 case DW_AT_declaration:
3364 case DW_AT_description:
3365 case DW_AT_endianity:
3366 case DW_AT_segment:
3367 case DW_AT_visibility:
3368 default:
3369 case DW_AT_abstract_origin:
3370 case DW_AT_sibling:
3371 break;
3372 }
3373 }
3374 }
3375
3376 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3377 const dw_tag_t parent_tag = die.GetParent().Tag();
3378 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003379 (parent_tag == DW_TAG_compile_unit ||
3380 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003381 (parent_context_die.Tag() == DW_TAG_class_type ||
3382 parent_context_die.Tag() == DW_TAG_structure_type);
3383
3384 ValueType scope = eValueTypeInvalid;
3385
3386 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3387 SymbolContextScope *symbol_context_scope = NULL;
3388
Sam McCall30621392016-11-22 11:40:25 +00003389 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003390 if (!mangled) {
3391 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003392 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3393 // of global variables with commands like "frame var j". For
3394 // example, if j were an int variable holding a value 4 and
3395 // declared in a namespace B which in turn is contained in a
3396 // namespace A, the command "frame var j" returns
3397 // "(int) A::B::j = 4".
3398 // If the compiler does not emit a linkage name, we should be
3399 // able to generate a fully qualified name from the
3400 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003401 if ((parent_tag == DW_TAG_compile_unit ||
3402 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003403 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3404 DWARFDeclContext decl_ctx;
3405
3406 die.GetDWARFDeclContext(decl_ctx);
3407 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3408 }
3409 }
3410
Kate Stoneb9c1b512016-09-06 20:57:50 +00003411 if (tag == DW_TAG_formal_parameter)
3412 scope = eValueTypeVariableArgument;
3413 else {
Sam McCall30621392016-11-22 11:40:25 +00003414 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3415 // or static variable, so we have to do a little digging:
3416 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3417 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3418 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3419 // Clang likes to combine small global variables into the same symbol
3420 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3421 // so we need to look through the whole expression.
3422 bool is_static_lifetime =
3423 has_explicit_mangled ||
3424 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003425 // Check if the location has a DW_OP_addr with any address value...
3426 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3427 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003428 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003429 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3430 if (op_error) {
3431 StreamString strm;
3432 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3433 NULL);
3434 GetObjectFile()->GetModule()->ReportError(
3435 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003436 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003437 }
Sam McCall30621392016-11-22 11:40:25 +00003438 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3439 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003440 }
3441 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3442
Sam McCall30621392016-11-22 11:40:25 +00003443 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003444 if (is_external)
3445 scope = eValueTypeVariableGlobal;
3446 else
3447 scope = eValueTypeVariableStatic;
3448
3449 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003450 // When leaving the DWARF in the .o files on darwin, when we have a
3451 // global variable that wasn't initialized, the .o file might not
3452 // have allocated a virtual address for the global variable. In
3453 // this case it will have created a symbol for the global variable
3454 // that is undefined/data and external and the value will be the
3455 // byte size of the variable. When we do the address map in
3456 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3457 // do some magic here so we can get the correct address for our
3458 // global variable. The address for all of these entries will be
3459 // zero, and there will be an undefined symbol in this object file,
3460 // and the executable will have a matching symbol with a good
3461 // address. So here we dig up the correct address and replace it in
3462 // the location for the variable, and set the variable's symbol
3463 // context scope to be that of the main executable so the file
3464 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003465 bool linked_oso_file_addr = false;
3466 if (is_external && location_DW_OP_addr == 0) {
3467 // we have a possible uninitialized extern global
3468 ConstString const_name(mangled ? mangled : name);
3469 ObjectFile *debug_map_objfile =
3470 debug_map_symfile->GetObjectFile();
3471 if (debug_map_objfile) {
3472 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3473 if (debug_map_symtab) {
3474 Symbol *exe_symbol =
3475 debug_map_symtab->FindFirstSymbolWithNameAndType(
3476 const_name, eSymbolTypeData, Symtab::eDebugYes,
3477 Symtab::eVisibilityExtern);
3478 if (exe_symbol) {
3479 if (exe_symbol->ValueIsAddress()) {
3480 const addr_t exe_file_addr =
3481 exe_symbol->GetAddressRef().GetFileAddress();
3482 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3483 if (location.Update_DW_OP_addr(exe_file_addr)) {
3484 linked_oso_file_addr = true;
3485 symbol_context_scope = exe_symbol;
3486 }
3487 }
3488 }
3489 }
3490 }
3491 }
3492 }
3493
3494 if (!linked_oso_file_addr) {
3495 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00003496 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003497 const lldb::addr_t exe_file_addr =
3498 debug_map_symfile->LinkOSOFileAddress(this,
3499 location_DW_OP_addr);
3500 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3501 // Update the file address for this variable
3502 location.Update_DW_OP_addr(exe_file_addr);
3503 } else {
3504 // Variable didn't make it into the final executable
3505 return var_sp;
3506 }
3507 }
3508 }
3509 } else {
3510 if (location_is_const_value_data)
3511 scope = eValueTypeVariableStatic;
3512 else {
3513 scope = eValueTypeVariableLocal;
3514 if (debug_map_symfile) {
3515 // We need to check for TLS addresses that we need to fixup
3516 if (location.ContainsThreadLocalStorage()) {
3517 location.LinkThreadLocalStorage(
3518 debug_map_symfile->GetObjectFile()->GetModule(),
3519 [this, debug_map_symfile](
3520 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3521 return debug_map_symfile->LinkOSOFileAddress(
3522 this, unlinked_file_addr);
3523 });
3524 scope = eValueTypeVariableThreadLocal;
3525 }
3526 }
3527 }
3528 }
3529 }
3530
3531 if (symbol_context_scope == NULL) {
3532 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00003533 case DW_TAG_subprogram:
3534 case DW_TAG_inlined_subroutine:
3535 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003536 if (sc.function) {
3537 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
3538 sc_parent_die.GetID());
3539 if (symbol_context_scope == NULL)
3540 symbol_context_scope = sc.function;
3541 }
3542 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003543
Kate Stoneb9c1b512016-09-06 20:57:50 +00003544 default:
3545 symbol_context_scope = sc.comp_unit;
3546 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00003547 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003548 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003549
Kate Stoneb9c1b512016-09-06 20:57:50 +00003550 if (symbol_context_scope) {
3551 SymbolFileTypeSP type_sp(
3552 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
3553
3554 if (const_value.Form() && type_sp && type_sp->GetType())
3555 location.CopyOpcodeData(const_value.Unsigned(),
3556 type_sp->GetType()->GetByteSize(),
3557 die.GetCU()->GetAddressByteSize());
3558
3559 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
3560 symbol_context_scope, scope_ranges, &decl,
3561 location, is_external, is_artificial,
3562 is_static_member));
3563
3564 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
3565 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003566 // Not ready to parse this variable yet. It might be a global or static
3567 // variable that is in a function scope and the function in the symbol
3568 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569 return var_sp;
3570 }
Greg Claytonc662ec82011-06-17 22:10:16 +00003571 }
Adrian Prantl05097242018-04-30 16:49:04 +00003572 // Cache var_sp even if NULL (the variable was just a specification or was
3573 // missing vital information to be able to be displayed in the debugger
3574 // (missing location due to optimization, etc)) so we don't re-parse this
3575 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00003576 GetDIEToVariable()[die.GetDIE()] = var_sp;
3577 if (spec_die)
3578 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3579 }
3580 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00003581}
3582
Kate Stoneb9c1b512016-09-06 20:57:50 +00003583DWARFDIE
3584SymbolFileDWARF::FindBlockContainingSpecification(
3585 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3586 // Give the concrete function die specified by "func_die_offset", find the
3587 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3588 // to "spec_block_die_offset"
3589 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
3590 spec_block_die_offset);
3591}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592
Kate Stoneb9c1b512016-09-06 20:57:50 +00003593DWARFDIE
3594SymbolFileDWARF::FindBlockContainingSpecification(
3595 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3596 if (die) {
3597 switch (die.Tag()) {
3598 case DW_TAG_subprogram:
3599 case DW_TAG_inlined_subroutine:
3600 case DW_TAG_lexical_block: {
3601 if (die.GetAttributeValueAsReference(
3602 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
3603 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00003604
Kate Stoneb9c1b512016-09-06 20:57:50 +00003605 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
3606 DW_INVALID_OFFSET) ==
3607 spec_block_die_offset)
3608 return die;
3609 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003610 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003611
3612 // Give the concrete function die specified by "func_die_offset", find the
3613 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3614 // to "spec_block_die_offset"
3615 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3616 child_die = child_die.GetSibling()) {
3617 DWARFDIE result_die =
3618 FindBlockContainingSpecification(child_die, spec_block_die_offset);
3619 if (result_die)
3620 return result_die;
3621 }
3622 }
3623
3624 return DWARFDIE();
3625}
3626
3627size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3628 const DWARFDIE &orig_die,
3629 const lldb::addr_t func_low_pc,
3630 bool parse_siblings, bool parse_children,
3631 VariableList *cc_variable_list) {
3632 if (!orig_die)
3633 return 0;
3634
3635 VariableListSP variable_list_sp;
3636
3637 size_t vars_added = 0;
3638 DWARFDIE die = orig_die;
3639 while (die) {
3640 dw_tag_t tag = die.Tag();
3641
3642 // Check to see if we have already parsed this variable or constant?
3643 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3644 if (var_sp) {
3645 if (cc_variable_list)
3646 cc_variable_list->AddVariableIfUnique(var_sp);
3647 } else {
3648 // We haven't already parsed it, lets do that now.
3649 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3650 (tag == DW_TAG_formal_parameter && sc.function)) {
3651 if (variable_list_sp.get() == NULL) {
3652 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3653 dw_tag_t parent_tag = sc_parent_die.Tag();
3654 switch (parent_tag) {
3655 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003656 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003657 if (sc.comp_unit != NULL) {
3658 variable_list_sp = sc.comp_unit->GetVariableList(false);
3659 if (variable_list_sp.get() == NULL) {
3660 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003661 }
3662 } else {
3663 GetObjectFile()->GetModule()->ReportError(
3664 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3665 "symbol context for 0x%8.8" PRIx64
3666 " %s.\n",
3667 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3668 orig_die.GetID(), orig_die.GetTagAsCString());
3669 }
3670 break;
3671
3672 case DW_TAG_subprogram:
3673 case DW_TAG_inlined_subroutine:
3674 case DW_TAG_lexical_block:
3675 if (sc.function != NULL) {
3676 // Check to see if we already have parsed the variables for the
3677 // given scope
3678
3679 Block *block = sc.function->GetBlock(true).FindBlockByID(
3680 sc_parent_die.GetID());
3681 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00003682 // This must be a specification or abstract origin with a
3683 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00003684 // to find the concrete block so we can correctly add the
3685 // variable to it
3686 const DWARFDIE concrete_block_die =
3687 FindBlockContainingSpecification(
3688 DIERef(sc.function->GetID(), this),
3689 sc_parent_die.GetOffset());
3690 if (concrete_block_die)
3691 block = sc.function->GetBlock(true).FindBlockByID(
3692 concrete_block_die.GetID());
3693 }
3694
3695 if (block != NULL) {
3696 const bool can_create = false;
3697 variable_list_sp = block->GetBlockVariableList(can_create);
3698 if (variable_list_sp.get() == NULL) {
3699 variable_list_sp.reset(new VariableList());
3700 block->SetVariableList(variable_list_sp);
3701 }
3702 }
3703 }
3704 break;
3705
3706 default:
3707 GetObjectFile()->GetModule()->ReportError(
3708 "didn't find appropriate parent DIE for variable list for "
3709 "0x%8.8" PRIx64 " %s.\n",
3710 orig_die.GetID(), orig_die.GetTagAsCString());
3711 break;
3712 }
3713 }
3714
3715 if (variable_list_sp) {
3716 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3717 if (var_sp) {
3718 variable_list_sp->AddVariableIfUnique(var_sp);
3719 if (cc_variable_list)
3720 cc_variable_list->AddVariableIfUnique(var_sp);
3721 ++vars_added;
3722 }
3723 }
3724 }
3725 }
3726
3727 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
3728
3729 if (!skip_children && parse_children && die.HasChildren()) {
3730 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3731 true, cc_variable_list);
3732 }
3733
3734 if (parse_siblings)
3735 die = die.GetSibling();
3736 else
3737 die.Clear();
3738 }
3739 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003740}
3741
3742//------------------------------------------------------------------
3743// PluginInterface protocol
3744//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003745ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3746
3747uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3748
Pavel Labath9337b412018-06-06 11:35:23 +00003749void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003750
Kate Stoneb9c1b512016-09-06 20:57:50 +00003751SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3752 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
3753 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3754 if (module_sp) {
3755 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
3756 if (sym_vendor)
3757 m_debug_map_symfile =
3758 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00003759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003760 }
3761 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00003762}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003763
3764DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00003765SymbolFileDWARF::GetLocationListFormat() const {
3766 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00003767}
Tamas Berghammer963ce482017-08-25 13:56:14 +00003768
3769SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
3770 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00003771 ModuleSpec module_spec;
3772 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
3773 module_spec.GetSymbolFileSpec() =
3774 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
3775 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Tamas Berghammer963ce482017-08-25 13:56:14 +00003776 if (dwp_filespec.Exists()) {
3777 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
3778 dwp_filespec);
3779 }
3780 });
3781 return m_dwp_symfile.get();
3782}