blob: cd13b4c72c4eaab37d86c5240caa5a0de56e1740 [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/Scalar.h"
21#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000022#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000024#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/RegularExpression.h"
26#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"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000030
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000031#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000032#include "lldb/Host/Host.h"
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +000033#include "lldb/Host/Symbols.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000034
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000035#include "lldb/Interpreter/OptionValueFileSpecList.h"
36#include "lldb/Interpreter/OptionValueProperties.h"
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000039#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000040#include "lldb/Symbol/ClangUtil.h"
41#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000042#include "lldb/Symbol/CompilerDecl.h"
43#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000044#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000045#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000048#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000049#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/VariableList.h"
51
Jim Inghamaa816b82015-09-02 01:59:14 +000052#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
53#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000054
Jim Ingham0e0984e2015-09-02 01:06:46 +000055#include "lldb/Target/Language.h"
56
Francis Ricci7ddfe8e2017-09-19 15:38:30 +000057#include "lldb/Host/TaskPool.h"
Tamas Berghammer2ff88702015-10-23 10:34:49 +000058
Greg Clayton261ac3f2015-08-28 01:01:03 +000059#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000060#include "DWARFASTParserClang.h"
Jan Kratochvilc4d65752018-03-18 20:11:02 +000061#include "DWARFUnit.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000062#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFDebugAbbrev.h"
64#include "DWARFDebugAranges.h"
65#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000067#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000069#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070#include "DWARFFormValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000072#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000073#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000074#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
Zachary Turner7d86ee52017-03-08 17:56:08 +000076#include "llvm/Support/FileSystem.h"
77
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078#include <map>
79
Matthew Gardinere81df3b2014-08-26 06:57:23 +000080#include <ctype.h>
81#include <string.h>
82
Greg Clayton62742b12010-11-11 01:09:45 +000083//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000084
85#ifdef ENABLE_DEBUG_PRINTF
86#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000087#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000088#else
89#define DEBUG_PRINTF(fmt, ...)
90#endif
91
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092using namespace lldb;
93using namespace lldb_private;
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095// static inline bool
96// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000097//{
98// switch (tag)
99// {
100// default:
101// break;
102// case DW_TAG_subprogram:
103// case DW_TAG_inlined_subroutine:
104// case DW_TAG_class_type:
105// case DW_TAG_structure_type:
106// case DW_TAG_union_type:
107// return true;
108// }
109// return false;
110//}
111//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000112
113namespace {
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115PropertyDefinition g_properties[] = {
116 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
117 nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic "
118 "links will be resolved at DWARF parse time."},
119 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121enum { ePropertySymLinkPaths };
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123class PluginProperties : public Properties {
124public:
125 static ConstString GetSettingName() {
126 return SymbolFileDWARF::GetPluginNameStatic();
127 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 PluginProperties() {
130 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
131 m_collection_sp->Initialize(g_properties);
132 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 FileSpecList &GetSymLinkPaths() {
135 OptionValueFileSpecList *option_value =
136 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
137 nullptr, true, ePropertySymLinkPaths);
138 assert(option_value);
139 return option_value->GetCurrentValue();
140 }
141};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
146 static const auto g_settings_sp(std::make_shared<PluginProperties>());
147 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
153 if (!path_from_dwarf || !path_from_dwarf[0]) {
154 return path_from_dwarf;
155 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 const char *colon_pos = strchr(path_from_dwarf, ':');
158 if (nullptr == colon_pos) {
159 return path_from_dwarf;
160 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 const char *slash_pos = strchr(path_from_dwarf, '/');
163 if (slash_pos && (slash_pos < colon_pos)) {
164 return path_from_dwarf;
165 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000166
Adrian Prantl05097242018-04-30 16:49:04 +0000167 // check whether we have a windows path, and so the first character is a
168 // drive-letter not a hostname.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
170 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
171 return path_from_dwarf;
172 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 return colon_pos + 1;
175}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000176
Greg Clayton776cd7a2018-04-27 15:45:58 +0000177static FileSpec resolveCompDir(const char *path_from_dwarf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 if (!path_from_dwarf)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000179 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180
181 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
182 // Remove the host part if present.
183 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
184 if (!local_path)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000185 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186
187 bool is_symlink = false;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000188 // Always normalize our compile unit directory to get rid of redundant
189 // slashes and other path anomalies before we use it for path prepending
190 FileSpec local_spec(local_path, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
192 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
193 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
Greg Clayton776cd7a2018-04-27 15:45:58 +0000194 local_spec, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195
196 if (!is_symlink)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000197 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198
Zachary Turner7d86ee52017-03-08 17:56:08 +0000199 namespace fs = llvm::sys::fs;
Greg Clayton776cd7a2018-04-27 15:45:58 +0000200 if (fs::get_file_type(local_spec.GetPath(), false) !=
Zachary Turner7d86ee52017-03-08 17:56:08 +0000201 fs::file_type::symlink_file)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000202 return local_spec;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203
Greg Clayton776cd7a2018-04-27 15:45:58 +0000204 FileSpec resolved_symlink;
205 const auto error = FileSystem::Readlink(local_spec, resolved_symlink);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 if (error.Success())
Greg Clayton776cd7a2018-04-27 15:45:58 +0000207 return resolved_symlink;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208
Greg Clayton776cd7a2018-04-27 15:45:58 +0000209 return local_spec;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000210}
211
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000212DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000213 return nullptr;
214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216void SymbolFileDWARF::Initialize() {
217 LogChannelDWARF::Initialize();
218 PluginManager::RegisterPlugin(GetPluginNameStatic(),
219 GetPluginDescriptionStatic(), CreateInstance,
220 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
224 if (!PluginManager::GetSettingForSymbolFilePlugin(
225 debugger, PluginProperties::GetSettingName())) {
226 const bool is_global_setting = true;
227 PluginManager::CreateSettingForSymbolFilePlugin(
228 debugger, GetGlobalPluginProperties()->GetValueProperties(),
229 ConstString("Properties for the dwarf symbol-file plug-in."),
230 is_global_setting);
231 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234void SymbolFileDWARF::Terminate() {
235 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000236 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
240 static ConstString g_name("dwarf");
241 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
245 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
249 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252TypeList *SymbolFileDWARF::GetTypeList() {
253 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
254 if (debug_map_symfile)
255 return debug_map_symfile->GetTypeList();
256 else
257 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000258}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
260 dw_offset_t max_die_offset, uint32_t type_mask,
261 TypeSet &type_set) {
262 if (die) {
263 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (die_offset >= max_die_offset)
266 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 if (die_offset >= min_die_offset) {
269 const dw_tag_t tag = die.Tag();
270
271 bool add_type = false;
272
273 switch (tag) {
274 case DW_TAG_array_type:
275 add_type = (type_mask & eTypeClassArray) != 0;
276 break;
277 case DW_TAG_unspecified_type:
278 case DW_TAG_base_type:
279 add_type = (type_mask & eTypeClassBuiltin) != 0;
280 break;
281 case DW_TAG_class_type:
282 add_type = (type_mask & eTypeClassClass) != 0;
283 break;
284 case DW_TAG_structure_type:
285 add_type = (type_mask & eTypeClassStruct) != 0;
286 break;
287 case DW_TAG_union_type:
288 add_type = (type_mask & eTypeClassUnion) != 0;
289 break;
290 case DW_TAG_enumeration_type:
291 add_type = (type_mask & eTypeClassEnumeration) != 0;
292 break;
293 case DW_TAG_subroutine_type:
294 case DW_TAG_subprogram:
295 case DW_TAG_inlined_subroutine:
296 add_type = (type_mask & eTypeClassFunction) != 0;
297 break;
298 case DW_TAG_pointer_type:
299 add_type = (type_mask & eTypeClassPointer) != 0;
300 break;
301 case DW_TAG_rvalue_reference_type:
302 case DW_TAG_reference_type:
303 add_type = (type_mask & eTypeClassReference) != 0;
304 break;
305 case DW_TAG_typedef:
306 add_type = (type_mask & eTypeClassTypedef) != 0;
307 break;
308 case DW_TAG_ptr_to_member_type:
309 add_type = (type_mask & eTypeClassMemberPointer) != 0;
310 break;
311 }
312
313 if (add_type) {
314 const bool assert_not_being_parsed = true;
315 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
316 if (type) {
317 if (type_set.find(type) == type_set.end())
318 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000319 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000321 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322
323 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
324 child_die = child_die.GetSibling()) {
325 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
326 }
327 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000328}
329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
331 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000332
333{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 CompileUnit *comp_unit = NULL;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000337 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 if (sc_scope)
339 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 if (comp_unit) {
342 dwarf_cu = GetDWARFCompileUnit(comp_unit);
343 if (dwarf_cu == 0)
344 return 0;
345 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
346 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
347 } else {
348 DWARFDebugInfo *info = DebugInfo();
349 if (info) {
350 const size_t num_cus = info->GetNumCompileUnits();
351 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
352 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
353 if (dwarf_cu) {
354 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000355 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000357 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 }
359
360 std::set<CompilerType> compiler_type_set;
361 size_t num_types_added = 0;
362 for (Type *type : type_set) {
363 CompilerType compiler_type = type->GetForwardCompilerType();
364 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
365 compiler_type_set.insert(compiler_type);
366 type_list.Insert(type->shared_from_this());
367 ++num_types_added;
368 }
369 }
370 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000371}
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373//----------------------------------------------------------------------
374// Gets the first parent that is a lexical block, function or inlined
375// subroutine, or compile unit.
376//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000377DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
379 DWARFDIE die;
380 for (die = child_die.GetParent(); die; die = die.GetParent()) {
381 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 switch (tag) {
384 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +0000385 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 case DW_TAG_subprogram:
387 case DW_TAG_inlined_subroutine:
388 case DW_TAG_lexical_block:
389 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 }
392 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}
394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Davide Italiano98f00212018-01-31 15:17:47 +0000396 : SymbolFile(objfile),
397 UserID(uint64_t(DW_INVALID_OFFSET) << 32), // Used by SymbolFileDWARFDebugMap to when
398 // this class parses .o files to contain
399 // the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
401 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
402 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
403 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
404 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
405 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
406 m_apple_objc_ap(), m_function_basename_index(),
407 m_function_fullname_index(), m_function_method_index(),
408 m_function_selector_index(), m_objc_class_selectors_index(),
409 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
410 m_using_apple_tables(false), m_fetched_external_modules(false),
411 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
412 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414SymbolFileDWARF::~SymbolFileDWARF() {}
415
416static const ConstString &GetDWARFMachOSegmentName() {
417 static ConstString g_dwarf_section_name("__DWARF");
418 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
422 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
423 if (debug_map_symfile)
424 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
425 else
426 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000427}
428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
430 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
431 TypeSystem *type_system;
432 if (debug_map_symfile) {
433 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
434 } else {
435 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
436 if (type_system)
437 type_system->SetSymbolFile(this);
438 }
439 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000440}
441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442void SymbolFileDWARF::InitializeObject() {
443 ModuleSP module_sp(m_obj_file->GetModule());
444 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000445 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000446 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000450 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
452
453 get_apple_names_data();
454 if (m_data_apple_names.m_data.GetByteSize() > 0) {
455 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
456 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000457 if (m_apple_names_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000458 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000459 else
460 m_apple_names_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 }
462 get_apple_types_data();
463 if (m_data_apple_types.m_data.GetByteSize() > 0) {
464 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
465 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000466 if (m_apple_types_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000467 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000468 else
469 m_apple_types_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 }
471
472 get_apple_namespaces_data();
473 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
474 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
475 m_data_apple_namespaces.m_data, get_debug_str_data(),
476 ".apple_namespaces"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000477 if (m_apple_namespaces_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000478 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000479 else
480 m_apple_namespaces_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 }
482
483 get_apple_objc_data();
484 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
485 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
486 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000487 if (m_apple_objc_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000488 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000489 else
490 m_apple_objc_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 }
492}
493
494bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
495 return version == 2 || version == 3 || version == 4;
496}
497
498uint32_t SymbolFileDWARF::CalculateAbilities() {
499 uint32_t abilities = 0;
500 if (m_obj_file != NULL) {
501 const Section *section = NULL;
502 const SectionList *section_list = m_obj_file->GetSectionList();
503 if (section_list == NULL)
504 return 0;
505
Adrian Prantl05097242018-04-30 16:49:04 +0000506 // On non Apple platforms we might have .debug_types debug info that is
507 // created by using "-fdebug-types-section". LLDB currently will try to
508 // load this debug info, but it causes crashes during debugging when types
509 // are missing since it doesn't know how to parse the info in the
510 // .debug_types type units. This causes all complex debug info types to be
511 // unresolved. Because this causes LLDB to crash and since it really
512 // doesn't provide a solid debuggiung experience, we should disable trying
513 // to debug this kind of DWARF until support gets added or deprecated.
Greg Claytonea5df102017-07-24 18:40:33 +0000514 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
515 m_obj_file->GetModule()->ReportWarning(
516 "lldb doesn’t support .debug_types debug info");
517 return 0;
518 }
519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 uint64_t debug_abbrev_file_size = 0;
521 uint64_t debug_info_file_size = 0;
522 uint64_t debug_line_file_size = 0;
523
524 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
525
526 if (section)
527 section_list = &section->GetChildren();
528
529 section =
530 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
531 if (section != NULL) {
532 debug_info_file_size = section->GetFileSize();
533
534 section =
535 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
536 .get();
537 if (section)
538 debug_abbrev_file_size = section->GetFileSize();
539
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000540 DWARFDebugAbbrev *abbrev = DebugAbbrev();
541 if (abbrev) {
542 std::set<dw_form_t> invalid_forms;
543 abbrev->GetUnsupportedForms(invalid_forms);
544 if (!invalid_forms.empty()) {
545 StreamString error;
546 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
547 for (auto form : invalid_forms)
548 error.Printf(" %#x", form);
549 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
550 return 0;
551 }
552 }
553
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 section =
555 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
556 .get();
557 if (section)
558 debug_line_file_size = section->GetFileSize();
559 } else {
560 const char *symfile_dir_cstr =
561 m_obj_file->GetFileSpec().GetDirectory().GetCString();
562 if (symfile_dir_cstr) {
563 if (strcasestr(symfile_dir_cstr, ".dsym")) {
564 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
Adrian Prantl05097242018-04-30 16:49:04 +0000565 // We have a dSYM file that didn't have a any debug info. If the
566 // string table has a size of 1, then it was made from an
567 // executable with no debug info, or from an executable that was
568 // stripped.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 section =
570 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
571 .get();
572 if (section && section->GetFileSize() == 1) {
573 m_obj_file->GetModule()->ReportWarning(
574 "empty dSYM file detected, dSYM was created with an "
575 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581
582 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
583 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
584 LocalVariables | VariableTypes;
585
586 if (debug_line_file_size > 0)
587 abilities |= LineTables;
588 }
589 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590}
591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592const DWARFDataExtractor &
593SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
594 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000595 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
596 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
597 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599}
600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
602 DWARFDataExtractor &data) {
603 ModuleSP module_sp(m_obj_file->GetModule());
604 const SectionList *section_list = module_sp->GetSectionList();
605 if (section_list) {
606 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
607 if (section_sp) {
608 // See if we memory mapped the DWARF segment?
609 if (m_dwarf_data.GetByteSize()) {
610 data.SetData(m_dwarf_data, section_sp->GetOffset(),
611 section_sp->GetFileSize());
612 } else {
613 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
614 data.Clear();
615 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618}
619
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
621 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
622 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623}
624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
626 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
627}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
630 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
631 m_data_debug_aranges);
632}
633
634const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
635 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
636}
637
638const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
639 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
640}
641
642const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
643 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
644}
645
646const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
647 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
648}
649
650const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
651 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
652}
653
654const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
655 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
656 m_data_debug_ranges);
657}
658
659const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
660 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
661}
662
663const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
664 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
665 m_data_debug_str_offsets);
666}
667
Greg Clayton2550ca12018-05-08 17:19:24 +0000668const DWARFDataExtractor &SymbolFileDWARF::get_debug_types_data() {
669 return GetCachedSectionData(eSectionTypeDWARFDebugTypes, m_data_debug_types);
670}
671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
673 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
674}
675
676const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
677 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
678}
679
680const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
681 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
682 m_data_apple_namespaces);
683}
684
685const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
686 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
687}
688
Jan Kratochvile4777a92018-04-29 19:47:48 +0000689const DWARFDataExtractor &SymbolFileDWARF::get_gnu_debugaltlink() {
690 return GetCachedSectionData(eSectionTypeDWARFGNUDebugAltLink,
691 m_data_gnu_debugaltlink);
692}
693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
695 if (m_abbr.get() == NULL) {
696 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
697 if (debug_abbrev_data.GetByteSize() > 0) {
698 m_abbr.reset(new DWARFDebugAbbrev());
699 if (m_abbr.get())
700 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 }
703 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704}
705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
707 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708}
709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
711 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000712 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
713 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
714 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 if (get_debug_info_data().GetByteSize() > 0) {
716 m_info.reset(new DWARFDebugInfo());
717 if (m_info.get()) {
718 m_info->SetDwarfData(this);
719 }
Greg Clayton1f746072012-08-29 21:13:06 +0000720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 }
722 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723}
724
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
726 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727}
728
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000729DWARFUnit *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
731 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000732 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733
734 DWARFDebugInfo *info = DebugInfo();
735 if (info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000736 // Just a normal DWARF file whose user ID for the compile unit is the DWARF
737 // offset itself
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000739 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
741 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
742 dwarf_cu->SetUserData(comp_unit);
743 return dwarf_cu;
744 }
745 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
749 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000750 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
751 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
752 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 if (get_debug_ranges_data().GetByteSize() > 0) {
754 m_ranges.reset(new DWARFDebugRanges());
755 if (m_ranges.get())
756 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000757 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 }
759 return m_ranges.get();
760}
761
762const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
763 return m_ranges.get();
764}
765
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000766lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 uint32_t cu_idx) {
768 CompUnitSP cu_sp;
769 if (dwarf_cu) {
770 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
771 if (comp_unit) {
772 // We already parsed this compile unit, had out a shared pointer to it
773 cu_sp = comp_unit->shared_from_this();
774 } else {
775 if (dwarf_cu->GetSymbolFileDWARF() != this) {
776 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
777 cu_idx);
778 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
779 // Let the debug map create the compile unit
780 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
781 dwarf_cu->SetUserData(cu_sp.get());
782 } else {
783 ModuleSP module_sp(m_obj_file->GetModule());
784 if (module_sp) {
Jan Kratochvild9508922018-04-14 11:12:52 +0000785 const DWARFDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 if (cu_die) {
787 FileSpec cu_file_spec{cu_die.GetName(), false};
788 if (cu_file_spec) {
789 // If we have a full path to the compile unit, we don't need to
Adrian Prantl05097242018-04-30 16:49:04 +0000790 // resolve the file. This can be expensive e.g. when the source
791 // files are
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 // NFS mounted.
793 if (cu_file_spec.IsRelative()) {
794 const char *cu_comp_dir{
795 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
796 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
797 }
798
799 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000800 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 remapped_file))
802 cu_file_spec.SetFile(remapped_file, false);
803 }
804
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000805 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
807
808 bool is_optimized = dwarf_cu->GetIsOptimized();
809 cu_sp.reset(new CompileUnit(
810 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
811 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
812 if (cu_sp) {
813 // If we just created a compile unit with an invalid file spec,
Adrian Prantl05097242018-04-30 16:49:04 +0000814 // try and get the first entry in the supports files from the
815 // line table as that should be the compile unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 if (!cu_file_spec) {
817 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
818 if (cu_file_spec) {
819 (FileSpec &)(*cu_sp) = cu_file_spec;
820 // Also fix the invalid file spec which was copied from the
821 // compile unit.
822 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
823 }
824 }
825
826 dwarf_cu->SetUserData(cu_sp.get());
827
828 // Figure out the compile unit index if we weren't given one
829 if (cu_idx == UINT32_MAX)
830 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
831
832 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
833 cu_idx, cu_sp);
834 }
835 }
836 }
837 }
838 }
839 }
840 return cu_sp;
841}
842
843uint32_t SymbolFileDWARF::GetNumCompileUnits() {
844 DWARFDebugInfo *info = DebugInfo();
845 if (info)
846 return info->GetNumCompileUnits();
847 return 0;
848}
849
850CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
851 CompUnitSP cu_sp;
852 DWARFDebugInfo *info = DebugInfo();
853 if (info) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000854 DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 if (dwarf_cu)
856 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
857 }
858 return cu_sp;
859}
860
861Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
862 const DWARFDIE &die) {
863 if (die.IsValid()) {
864 TypeSystem *type_system =
865 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
866
867 if (type_system) {
868 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
869 if (dwarf_ast)
870 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
871 }
872 }
873 return nullptr;
874}
875
876bool SymbolFileDWARF::FixupAddress(Address &addr) {
877 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
878 if (debug_map_symfile) {
879 return debug_map_symfile->LinkOSOAddress(addr);
880 }
881 // This is a normal DWARF file, no address fixups need to happen
882 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000883}
Greg Clayton1f746072012-08-29 21:13:06 +0000884lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
886 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000887 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 if (dwarf_cu)
889 return dwarf_cu->GetLanguageType();
890 else
891 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
895 assert(sc.comp_unit);
896 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000897 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 if (dwarf_cu) {
899 DWARFDIECollection function_dies;
900 const size_t num_functions =
901 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
902 size_t func_idx;
903 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
904 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
905 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
906 if (ParseCompileUnitFunction(sc, die))
907 ++functions_added;
908 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 // FixupTypes();
911 }
912 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913}
914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
916 const SymbolContext &sc, FileSpecList &support_files) {
917 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000918 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 if (dwarf_cu) {
Jan Kratochvild9508922018-04-14 11:12:52 +0000920 const DWARFDIE cu_die = dwarf_cu->GetUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 if (cu_die) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000923 FileSpec cu_comp_dir = resolveCompDir(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
925 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
926 DW_AT_stmt_list, DW_INVALID_OFFSET);
927 if (stmt_list != DW_INVALID_OFFSET) {
928 // All file indexes in DWARF are one based and a file of index zero is
929 // supposed to be the compile unit itself.
930 support_files.Append(*sc.comp_unit);
931 return DWARFDebugLine::ParseSupportFiles(
932 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
933 stmt_list, support_files);
934 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 }
937 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938}
939
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
941 const lldb_private::SymbolContext &sc) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000942 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 if (dwarf_cu)
944 return dwarf_cu->GetIsOptimized();
945 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000946}
947
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948bool SymbolFileDWARF::ParseImportedModules(
949 const lldb_private::SymbolContext &sc,
950 std::vector<lldb_private::ConstString> &imported_modules) {
951 assert(sc.comp_unit);
Jan Kratochvilc4d65752018-03-18 20:11:02 +0000952 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 if (dwarf_cu) {
954 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
955 sc.comp_unit->GetLanguage())) {
956 UpdateExternalModuleListIfNeeded();
957
958 if (sc.comp_unit) {
Jan Kratochvild9508922018-04-14 11:12:52 +0000959 const DWARFDIE die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960
961 if (die) {
962 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
963 child_die = child_die.GetSibling()) {
964 if (child_die.Tag() == DW_TAG_imported_declaration) {
965 if (DWARFDIE module_die =
966 child_die.GetReferencedDIE(DW_AT_import)) {
967 if (module_die.Tag() == DW_TAG_module) {
968 if (const char *name = module_die.GetAttributeValueAsString(
969 DW_AT_name, nullptr)) {
970 ConstString const_name(name);
971 imported_modules.push_back(const_name);
972 }
Sean Callananb0300a42016-01-14 21:46:09 +0000973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 }
Sean Callananb0300a42016-01-14 21:46:09 +0000975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000977 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 } else {
979 for (const auto &pair : m_external_type_modules) {
980 imported_modules.push_back(pair.first);
981 }
982 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 }
985 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000986}
987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988struct ParseDWARFLineTableCallbackInfo {
989 LineTable *line_table;
990 std::unique_ptr<LineSequence> sequence_ap;
991 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992};
993
994//----------------------------------------------------------------------
995// ParseStatementTableCallback
996//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997static void ParseDWARFLineTableCallback(dw_offset_t offset,
998 const DWARFDebugLine::State &state,
999 void *userData) {
1000 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
1001 // Just started parsing the line table
1002 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
1003 // Done parsing line table, nothing to do for the cleanup
1004 } else {
1005 ParseDWARFLineTableCallbackInfo *info =
1006 (ParseDWARFLineTableCallbackInfo *)userData;
1007 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008
Adrian Prantl05097242018-04-30 16:49:04 +00001009 // If this is our first time here, we need to create a sequence container.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 if (!info->sequence_ap.get()) {
1011 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1012 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 line_table->AppendLineEntryToSequence(
1015 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1016 state.column, state.file, state.is_stmt, state.basic_block,
1017 state.prologue_end, state.epilogue_begin, state.end_sequence);
1018 if (state.end_sequence) {
1019 // First, put the current sequence into the line table.
1020 line_table->InsertSequence(info->sequence_ap.get());
1021 // Then, empty it to prepare for the next sequence.
1022 info->sequence_ap->Clear();
1023 }
1024 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025}
1026
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1028 assert(sc.comp_unit);
1029 if (sc.comp_unit->GetLineTable() != NULL)
1030 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001032 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 if (dwarf_cu) {
Jan Kratochvild9508922018-04-14 11:12:52 +00001034 const DWARFDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 if (dwarf_cu_die) {
1036 const dw_offset_t cu_line_offset =
1037 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1038 DW_INVALID_OFFSET);
1039 if (cu_line_offset != DW_INVALID_OFFSET) {
1040 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1041 if (line_table_ap.get()) {
1042 ParseDWARFLineTableCallbackInfo info;
1043 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 /*
1046 * MIPS:
1047 * The SymbolContext may not have a valid target, thus we may not be
1048 * able
1049 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1050 * #0
1051 * for MIPS. Use ArchSpec to clear the bit #0.
1052 */
1053 ArchSpec arch;
1054 GetObjectFile()->GetArchitecture(arch);
1055 switch (arch.GetMachine()) {
1056 case llvm::Triple::mips:
1057 case llvm::Triple::mipsel:
1058 case llvm::Triple::mips64:
1059 case llvm::Triple::mips64el:
1060 info.addr_mask = ~((lldb::addr_t)1);
1061 break;
1062 default:
1063 info.addr_mask = ~((lldb::addr_t)0);
1064 break;
1065 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 lldb::offset_t offset = cu_line_offset;
1068 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1069 ParseDWARFLineTableCallback,
1070 &info);
1071 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1072 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00001073 // We have an object file that has a line table with addresses that
1074 // are not linked. We need to link the line table and convert the
1075 // addresses that are relative to the .o file into addresses for
1076 // the main executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 sc.comp_unit->SetLineTable(
1078 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1079 } else {
1080 sc.comp_unit->SetLineTable(line_table_ap.release());
1081 return true;
1082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 }
1087 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088}
1089
Siva Chandrad8335e92015-12-16 00:22:08 +00001090lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1092 auto iter = m_debug_macros_map.find(*offset);
1093 if (iter != m_debug_macros_map.end())
1094 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001095
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1097 if (debug_macro_data.GetByteSize() == 0)
1098 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1101 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001102
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 const DWARFDebugMacroHeader &header =
1104 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1105 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1106 header.OffsetIs64Bit(), offset, this,
1107 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001110}
1111
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1113 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001114
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001115 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001117 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118
Jan Kratochvild9508922018-04-14 11:12:52 +00001119 const DWARFDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (!dwarf_cu_die)
1121 return false;
1122
1123 lldb::offset_t sect_offset =
1124 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1125 if (sect_offset == DW_INVALID_OFFSET)
1126 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1127 DW_INVALID_OFFSET);
1128 if (sect_offset == DW_INVALID_OFFSET)
1129 return false;
1130
1131 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1132
1133 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001134}
1135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1137 Block *parent_block,
1138 const DWARFDIE &orig_die,
1139 addr_t subprogram_low_pc,
1140 uint32_t depth) {
1141 size_t blocks_added = 0;
1142 DWARFDIE die = orig_die;
1143 while (die) {
1144 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 switch (tag) {
1147 case DW_TAG_inlined_subroutine:
1148 case DW_TAG_subprogram:
1149 case DW_TAG_lexical_block: {
1150 Block *block = NULL;
1151 if (tag == DW_TAG_subprogram) {
Adrian Prantl05097242018-04-30 16:49:04 +00001152 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1153 // inlined functions. These will be parsed on their own as separate
1154 // entities.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155
1156 if (depth > 0)
1157 break;
1158
1159 block = parent_block;
1160 } else {
1161 BlockSP block_sp(new Block(die.GetID()));
1162 parent_block->AddChild(block_sp);
1163 block = block_sp.get();
1164 }
1165 DWARFRangeList ranges;
1166 const char *name = NULL;
1167 const char *mangled_name = NULL;
1168
1169 int decl_file = 0;
1170 int decl_line = 0;
1171 int decl_column = 0;
1172 int call_file = 0;
1173 int call_line = 0;
1174 int call_column = 0;
1175 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1176 decl_line, decl_column, call_file, call_line,
1177 call_column, nullptr)) {
1178 if (tag == DW_TAG_subprogram) {
1179 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1180 subprogram_low_pc = ranges.GetMinRangeBase(0);
1181 } else if (tag == DW_TAG_inlined_subroutine) {
Adrian Prantl05097242018-04-30 16:49:04 +00001182 // We get called here for inlined subroutines in two ways. The first
1183 // time is when we are making the Function object for this inlined
1184 // concrete instance. Since we're creating a top level block at
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
Adrian Prantl05097242018-04-30 16:49:04 +00001186 // need to adjust the containing address. The second time is when we
1187 // are parsing the blocks inside the function that contains the
1188 // inlined concrete instance. Since these will be blocks inside the
1189 // containing "real" function the offset will be for that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1191 subprogram_low_pc = ranges.GetMinRangeBase(0);
1192 }
1193 }
1194
1195 const size_t num_ranges = ranges.GetSize();
1196 for (size_t i = 0; i < num_ranges; ++i) {
1197 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1198 const addr_t range_base = range.GetRangeBase();
1199 if (range_base >= subprogram_low_pc)
1200 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1201 range.GetByteSize()));
1202 else {
1203 GetObjectFile()->GetModule()->ReportError(
1204 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1205 ") which has a base that is less than the function's low PC "
1206 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1207 "start of this error message",
1208 block->GetID(), range_base, range.GetRangeEnd(),
1209 subprogram_low_pc);
1210 }
1211 }
1212 block->FinalizeRanges();
1213
1214 if (tag != DW_TAG_subprogram &&
1215 (name != NULL || mangled_name != NULL)) {
1216 std::unique_ptr<Declaration> decl_ap;
1217 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1218 decl_ap.reset(new Declaration(
1219 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1220 decl_line, decl_column));
1221
1222 std::unique_ptr<Declaration> call_ap;
1223 if (call_file != 0 || call_line != 0 || call_column != 0)
1224 call_ap.reset(new Declaration(
1225 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1226 call_line, call_column));
1227
1228 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1229 call_ap.get());
1230 }
1231
1232 ++blocks_added;
1233
1234 if (die.HasChildren()) {
1235 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1236 subprogram_low_pc, depth + 1);
1237 }
1238 }
1239 } break;
1240 default:
1241 break;
1242 }
1243
Adrian Prantl05097242018-04-30 16:49:04 +00001244 // Only parse siblings of the block if we are not at depth zero. A depth of
1245 // zero indicates we are currently parsing the top level DW_TAG_subprogram
1246 // DIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247
1248 if (depth == 0)
1249 die.Clear();
1250 else
1251 die = die.GetSibling();
1252 }
1253 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001254}
1255
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1257 if (parent_die) {
1258 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1259 die = die.GetSibling()) {
1260 dw_tag_t tag = die.Tag();
1261 bool check_virtuality = false;
1262 switch (tag) {
1263 case DW_TAG_inheritance:
1264 case DW_TAG_subprogram:
1265 check_virtuality = true;
1266 break;
1267 default:
1268 break;
1269 }
1270 if (check_virtuality) {
1271 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1272 return true;
1273 }
1274 }
1275 }
1276 return false;
1277}
1278
1279void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1280 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1281 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1282 std::vector<DWARFDIE> decl_ctx_die_list =
1283 ast_parser->GetDIEForDeclContext(decl_ctx);
1284
1285 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1286 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1287 decl = decl.GetSibling())
1288 ast_parser->GetDeclForUIDFromDWARF(decl);
1289}
1290
1291SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001292 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1293 // must make sure we use the correct DWARF file when resolving things. On
1294 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1295 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1296 // references to other DWARF objects and we must be ready to receive a
1297 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 // instance.
1299 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1300 if (debug_map)
1301 return debug_map->GetSymbolFileByOSOIndex(
1302 debug_map->GetOSOIndexFromUserID(uid));
1303 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001304}
1305
1306DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001308 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1309 // must make sure we use the correct DWARF file when resolving things. On
1310 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1311 // SymbolFileDWARF classes, one for each .o file. We can often end up with
1312 // references to other DWARF objects and we must be ready to receive a
1313 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314 // instance.
1315 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1316 if (dwarf)
1317 return dwarf->GetDIE(DIERef(uid, dwarf));
1318 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001319}
1320
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001322 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1323 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1324 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 DWARFDIE die = GetDIEFromUID(type_uid);
1326 if (die)
1327 return die.GetDecl();
1328 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001329}
1330
Greg Clayton99558cc42015-08-24 23:46:31 +00001331CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001333 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1334 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1335 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 DWARFDIE die = GetDIEFromUID(type_uid);
1337 if (die)
1338 return die.GetDeclContext();
1339 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001340}
1341
Greg Clayton99558cc42015-08-24 23:46:31 +00001342CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001344 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1345 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1346 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 DWARFDIE die = GetDIEFromUID(type_uid);
1348 if (die)
1349 return die.GetContainingDeclContext();
1350 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351}
1352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001354 // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1355 // SymbolFileDWARF::GetDIEFromUID(). See comments inside the
1356 // SymbolFileDWARF::GetDIEFromUID() for details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 DWARFDIE type_die = GetDIEFromUID(type_uid);
1358 if (type_die)
1359 return type_die.ResolveType();
1360 else
1361 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362}
1363
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1365 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001366}
1367
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1369 bool assert_not_being_parsed) {
1370 if (die) {
1371 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1372 if (log)
1373 GetObjectFile()->GetModule()->LogMessage(
1374 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1375 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001376
Adrian Prantl05097242018-04-30 16:49:04 +00001377 // We might be coming in in the middle of a type tree (a class within a
1378 // class, an enum within a class), so parse any needed parent DIEs before
1379 // we get to this one...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1381 if (decl_ctx_die) {
1382 if (log) {
1383 switch (decl_ctx_die.Tag()) {
1384 case DW_TAG_structure_type:
1385 case DW_TAG_union_type:
1386 case DW_TAG_class_type: {
1387 // Get the type, which could be a forward declaration
1388 if (log)
1389 GetObjectFile()->GetModule()->LogMessage(
1390 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1391 "resolve parent forward type for 0x%8.8x",
1392 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1393 decl_ctx_die.GetOffset());
1394 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 default:
1397 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 return ResolveType(die);
1402 }
1403 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001404}
1405
Greg Clayton6beaaa62011-01-17 03:46:26 +00001406// This function is used when SymbolFileDWARFDebugMap owns a bunch of
Adrian Prantl05097242018-04-30 16:49:04 +00001407// SymbolFileDWARF objects to detect if this DWARF file is the one that can
1408// resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409bool SymbolFileDWARF::HasForwardDeclForClangType(
1410 const CompilerType &compiler_type) {
1411 CompilerType compiler_type_no_qualifiers =
1412 ClangUtil::RemoveFastQualifiers(compiler_type);
1413 if (GetForwardDeclClangTypeToDie().count(
1414 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1415 return true;
1416 }
1417 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 ClangASTContext *clang_type_system =
1420 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1421 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001422 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 DWARFASTParserClang *ast_parser =
1424 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1425 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1429 std::lock_guard<std::recursive_mutex> guard(
1430 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 ClangASTContext *clang_type_system =
1433 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1434 if (clang_type_system) {
1435 DWARFASTParserClang *ast_parser =
1436 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1437 if (ast_parser &&
1438 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1439 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1440 }
Jim Inghamc3549282012-01-11 02:21:12 +00001441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 // We have a struct/union/class/enum that needs to be fully resolved.
1443 CompilerType compiler_type_no_qualifiers =
1444 ClangUtil::RemoveFastQualifiers(compiler_type);
1445 auto die_it = GetForwardDeclClangTypeToDie().find(
1446 compiler_type_no_qualifiers.GetOpaqueQualType());
1447 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1448 // We have already resolved this type...
1449 return true;
1450 }
1451
1452 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1453 if (dwarf_die) {
Adrian Prantl05097242018-04-30 16:49:04 +00001454 // Once we start resolving this type, remove it from the forward
1455 // declaration map in case anyone child members or other types require this
1456 // type to get resolved. The type will get resolved when all of the calls
1457 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 GetForwardDeclClangTypeToDie().erase(die_it);
1459
1460 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1461
1462 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1463 DWARF_LOG_TYPE_COMPLETION));
1464 if (log)
1465 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1466 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1467 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1468 type->GetName().AsCString());
1469 assert(compiler_type);
1470 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1471 if (dwarf_ast)
1472 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1473 }
1474 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001475}
1476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1478 bool assert_not_being_parsed,
1479 bool resolve_function_context) {
1480 if (die) {
1481 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1482
1483 if (assert_not_being_parsed) {
1484 if (type != DIE_IS_BEING_PARSED)
1485 return type;
1486
1487 GetObjectFile()->GetModule()->ReportError(
1488 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1489 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1490
1491 } else
1492 return type;
1493 }
1494 return nullptr;
1495}
1496
1497CompileUnit *
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001498SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 uint32_t cu_idx) {
1500 // Check if the symbol vendor already knows about this compile unit?
1501 if (dwarf_cu->GetUserData() == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00001502 // The symbol vendor doesn't know about this compile unit, we need to parse
1503 // and add it to the symbol vendor object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1505 }
1506 return (CompileUnit *)dwarf_cu->GetUserData();
1507}
1508
1509size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1510 DIEArray &method_die_offsets) {
1511 method_die_offsets.clear();
1512 if (m_using_apple_tables) {
1513 if (m_apple_objc_ap.get())
Pavel Labathb39fca92018-02-23 17:49:26 +00001514 m_apple_objc_ap->FindByName(class_name.GetStringRef(),
1515 method_die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 } else {
1517 if (!m_indexed)
1518 Index();
1519
1520 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1521 }
1522 return method_die_offsets.size();
1523}
1524
1525bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1526 sc.Clear(false);
1527
1528 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1531
1532 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1533 if (sc.function == NULL)
1534 sc.function = ParseCompileUnitFunction(sc, die);
1535
1536 if (sc.function) {
1537 sc.module_sp = sc.function->CalculateSymbolContextModule();
1538 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 }
1541
1542 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543}
1544
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1546 UpdateExternalModuleListIfNeeded();
1547 const auto &pos = m_external_type_modules.find(name);
1548 if (pos != m_external_type_modules.end())
1549 return pos->second;
1550 else
1551 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001552}
1553
Greg Clayton2f869fe2016-03-30 20:14:35 +00001554DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1556 DWARFDebugInfo *debug_info = DebugInfo();
1557 if (debug_info)
1558 return debug_info->GetDIE(die_ref);
1559 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001560 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001561}
1562
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563std::unique_ptr<SymbolFileDWARFDwo>
1564SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001565 DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001567 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 // info files for modules.
1569 if (GetDebugMapSymfile())
1570 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001571
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572 const char *dwo_name = cu_die.GetAttributeValueAsString(
1573 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1574 if (!dwo_name)
1575 return nullptr;
1576
Tamas Berghammer963ce482017-08-25 13:56:14 +00001577 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1578 if (dwp_symfile) {
1579 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1580 DW_AT_GNU_dwo_id, 0);
1581 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1582 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1583 if (dwo_symfile)
1584 return dwo_symfile;
1585 }
1586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 FileSpec dwo_file(dwo_name, true);
1588 if (dwo_file.IsRelative()) {
1589 const char *comp_dir = cu_die.GetAttributeValueAsString(
1590 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1591 if (!comp_dir)
1592 return nullptr;
1593
1594 dwo_file.SetFile(comp_dir, true);
1595 dwo_file.AppendPathComponent(dwo_name);
1596 }
1597
1598 if (!dwo_file.Exists())
1599 return nullptr;
1600
1601 const lldb::offset_t file_offset = 0;
1602 DataBufferSP dwo_file_data_sp;
1603 lldb::offset_t dwo_file_data_offset = 0;
1604 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1605 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1606 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1607 if (dwo_obj_file == nullptr)
1608 return nullptr;
1609
1610 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001611}
1612
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1614 if (m_fetched_external_modules)
1615 return;
1616 m_fetched_external_modules = true;
1617
1618 DWARFDebugInfo *debug_info = DebugInfo();
1619
1620 const uint32_t num_compile_units = GetNumCompileUnits();
1621 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001622 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623
Jan Kratochvild9508922018-04-14 11:12:52 +00001624 const DWARFDIE die = dwarf_cu->GetUnitDIEOnly();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 if (die && die.HasChildren() == false) {
1626 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1627
1628 if (name) {
1629 ConstString const_name(name);
1630 if (m_external_type_modules.find(const_name) ==
1631 m_external_type_modules.end()) {
1632 ModuleSP module_sp;
1633 const char *dwo_path =
1634 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1635 if (dwo_path) {
1636 ModuleSpec dwo_module_spec;
1637 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1638 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1639 const char *comp_dir =
1640 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1641 if (comp_dir) {
1642 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1643 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1644 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646 dwo_module_spec.GetArchitecture() =
1647 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001648
Adrian Prantl05097242018-04-30 16:49:04 +00001649 // When LLDB loads "external" modules it looks at the presence of
1650 // DW_AT_GNU_dwo_name. However, when the already created module
1651 // (corresponding to .dwo itself) is being processed, it will see
1652 // the presence of DW_AT_GNU_dwo_name (which contains the name of
1653 // dwo file) and will try to call ModuleList::GetSharedModule
1654 // again. In some cases (i.e. for empty files) Clang 4.0 generates
1655 // a *.dwo file which has DW_AT_GNU_dwo_name, but no
1656 // DW_AT_comp_dir. In this case the method
1657 // ModuleList::GetSharedModule will fail and the warning will be
1658 // printed. However, as one can notice in this case we don't
1659 // actually need to try to load the already loaded module
1660 // (corresponding to .dwo) so we simply skip it.
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001661 if (m_obj_file->GetFileSpec()
1662 .GetFileNameExtension()
1663 .GetStringRef() == "dwo" &&
1664 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1665 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1666 continue;
1667 }
1668
Zachary Turner97206d52017-05-12 04:51:55 +00001669 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 dwo_module_spec, module_sp, NULL, NULL, NULL);
1671 if (!module_sp) {
1672 GetObjectFile()->GetModule()->ReportWarning(
1673 "0x%8.8x: unable to locate module needed for external types: "
1674 "%s\nerror: %s\nDebugging will be degraded due to missing "
1675 "types. Rebuilding your project will regenerate the needed "
1676 "module files.",
1677 die.GetOffset(),
1678 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1679 error.AsCString("unknown error"));
1680 }
1681 }
1682 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001683 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 }
1687}
1688
1689SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1690 if (!m_global_aranges_ap) {
1691 m_global_aranges_ap.reset(new GlobalVariableMap());
1692
1693 ModuleSP module_sp = GetObjectFile()->GetModule();
1694 if (module_sp) {
1695 const size_t num_cus = module_sp->GetNumCompileUnits();
1696 for (size_t i = 0; i < num_cus; ++i) {
1697 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1698 if (cu_sp) {
1699 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1700 if (globals_sp) {
1701 const size_t num_globals = globals_sp->GetSize();
1702 for (size_t g = 0; g < num_globals; ++g) {
1703 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1704 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1705 const DWARFExpression &location = var_sp->LocationExpression();
1706 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001707 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001708 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1709 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 if (location_result.GetValueType() ==
1711 Value::eValueTypeFileAddress) {
1712 lldb::addr_t file_addr =
1713 location_result.GetScalar().ULongLong();
1714 lldb::addr_t byte_size = 1;
1715 if (var_sp->GetType())
1716 byte_size = var_sp->GetType()->GetByteSize();
1717 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1718 file_addr, byte_size, var_sp.get()));
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 }
1727 m_global_aranges_ap->Sort();
1728 }
1729 return *m_global_aranges_ap;
1730}
1731
1732uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1733 uint32_t resolve_scope,
1734 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001735 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1736 Timer scoped_timer(func_cat,
1737 "SymbolFileDWARF::"
1738 "ResolveSymbolContext (so_addr = { "
1739 "section = %p, offset = 0x%" PRIx64
1740 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 static_cast<void *>(so_addr.GetSection().get()),
1742 so_addr.GetOffset(), resolve_scope);
1743 uint32_t resolved = 0;
1744 if (resolve_scope &
1745 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1746 eSymbolContextLineEntry | eSymbolContextVariable)) {
1747 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1748
1749 DWARFDebugInfo *debug_info = DebugInfo();
1750 if (debug_info) {
1751 const dw_offset_t cu_offset =
1752 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1753 if (cu_offset == DW_INVALID_OFFSET) {
Adrian Prantl05097242018-04-30 16:49:04 +00001754 // Global variables are not in the compile unit address ranges. The
1755 // only way to currently find global variables is to iterate over the
1756 // .debug_pubnames or the __apple_names table and find all items in
1757 // there that point to DW_TAG_variable DIEs and then find the address
1758 // that matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759 if (resolve_scope & eSymbolContextVariable) {
1760 GlobalVariableMap &map = GetGlobalAranges();
1761 const GlobalVariableMap::Entry *entry =
1762 map.FindEntryThatContains(file_vm_addr);
1763 if (entry && entry->data) {
1764 Variable *variable = entry->data;
1765 SymbolContextScope *scc = variable->GetSymbolContextScope();
1766 if (scc) {
1767 scc->CalculateSymbolContext(&sc);
1768 sc.variable = variable;
1769 }
1770 return sc.GetResolvedMask();
1771 }
1772 }
1773 } else {
1774 uint32_t cu_idx = DW_INVALID_INDEX;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001775 DWARFUnit *dwarf_cu =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1777 if (dwarf_cu) {
1778 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1779 if (sc.comp_unit) {
1780 resolved |= eSymbolContextCompUnit;
1781
1782 bool force_check_line_table = false;
1783 if (resolve_scope &
1784 (eSymbolContextFunction | eSymbolContextBlock)) {
1785 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1786 DWARFDIE block_die;
1787 if (function_die) {
1788 sc.function =
1789 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1790 if (sc.function == NULL)
1791 sc.function = ParseCompileUnitFunction(sc, function_die);
1792
1793 if (sc.function && (resolve_scope & eSymbolContextBlock))
1794 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1795 } else {
1796 // We might have had a compile unit that had discontiguous
1797 // address ranges where the gaps are symbols that don't have
1798 // any debug info. Discontiguous compile unit address ranges
1799 // should only happen when there aren't other functions from
1800 // other compile units in these gaps. This helps keep the size
1801 // of the aranges down.
1802 force_check_line_table = true;
1803 }
1804
1805 if (sc.function != NULL) {
1806 resolved |= eSymbolContextFunction;
1807
1808 if (resolve_scope & eSymbolContextBlock) {
1809 Block &block = sc.function->GetBlock(true);
1810
1811 if (block_die)
1812 sc.block = block.FindBlockByID(block_die.GetID());
1813 else
1814 sc.block = block.FindBlockByID(function_die.GetID());
1815 if (sc.block)
1816 resolved |= eSymbolContextBlock;
1817 }
1818 }
1819 }
1820
1821 if ((resolve_scope & eSymbolContextLineEntry) ||
1822 force_check_line_table) {
1823 LineTable *line_table = sc.comp_unit->GetLineTable();
1824 if (line_table != NULL) {
1825 // And address that makes it into this function should be in
Adrian Prantl05097242018-04-30 16:49:04 +00001826 // terms of this debug file if there is no debug map, or it
1827 // will be an address in the .o file which needs to be fixed up
1828 // to be in terms of the debug map executable. Either way,
1829 // calling FixupAddress() will work for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 Address exe_so_addr(so_addr);
1831 if (FixupAddress(exe_so_addr)) {
1832 if (line_table->FindLineEntryByAddress(exe_so_addr,
1833 sc.line_entry)) {
1834 resolved |= eSymbolContextLineEntry;
1835 }
1836 }
1837 }
1838 }
1839
1840 if (force_check_line_table &&
1841 !(resolved & eSymbolContextLineEntry)) {
1842 // We might have had a compile unit that had discontiguous
Adrian Prantl05097242018-04-30 16:49:04 +00001843 // address ranges where the gaps are symbols that don't have any
1844 // debug info. Discontiguous compile unit address ranges should
1845 // only happen when there aren't other functions from other
1846 // compile units in these gaps. This helps keep the size of the
1847 // aranges down.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 sc.comp_unit = NULL;
1849 resolved &= ~eSymbolContextCompUnit;
1850 }
1851 } else {
1852 GetObjectFile()->GetModule()->ReportWarning(
1853 "0x%8.8x: compile unit %u failed to create a valid "
1854 "lldb_private::CompileUnit class.",
1855 cu_offset, cu_idx);
1856 }
1857 }
1858 }
1859 }
1860 }
1861 return resolved;
1862}
1863
1864uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1865 uint32_t line,
1866 bool check_inlines,
1867 uint32_t resolve_scope,
1868 SymbolContextList &sc_list) {
1869 const uint32_t prev_size = sc_list.GetSize();
1870 if (resolve_scope & eSymbolContextCompUnit) {
1871 DWARFDebugInfo *debug_info = DebugInfo();
1872 if (debug_info) {
1873 uint32_t cu_idx;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001874 DWARFUnit *dwarf_cu = NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875
1876 for (cu_idx = 0;
1877 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1878 ++cu_idx) {
1879 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1880 const bool full_match = (bool)file_spec.GetDirectory();
1881 bool file_spec_matches_cu_file_spec =
1882 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1883 if (check_inlines || file_spec_matches_cu_file_spec) {
1884 SymbolContext sc(m_obj_file->GetModule());
1885 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1886 if (sc.comp_unit) {
1887 uint32_t file_idx = UINT32_MAX;
1888
Adrian Prantl05097242018-04-30 16:49:04 +00001889 // If we are looking for inline functions only and we don't find it
1890 // in the support files, we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891 if (check_inlines) {
1892 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1893 1, file_spec, true);
1894 if (file_idx == UINT32_MAX)
1895 continue;
1896 }
1897
1898 if (line != 0) {
1899 LineTable *line_table = sc.comp_unit->GetLineTable();
1900
1901 if (line_table != NULL && line != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001902 // We will have already looked up the file index if we are
1903 // searching for inline entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001904 if (!check_inlines)
1905 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1906 1, file_spec, true);
1907
1908 if (file_idx != UINT32_MAX) {
1909 uint32_t found_line;
1910 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1911 0, file_idx, line, false, &sc.line_entry);
1912 found_line = sc.line_entry.line;
1913
1914 while (line_idx != UINT32_MAX) {
1915 sc.function = NULL;
1916 sc.block = NULL;
1917 if (resolve_scope &
1918 (eSymbolContextFunction | eSymbolContextBlock)) {
1919 const lldb::addr_t file_vm_addr =
1920 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1921 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1922 DWARFDIE function_die =
1923 dwarf_cu->LookupAddress(file_vm_addr);
1924 DWARFDIE block_die;
1925 if (function_die) {
1926 sc.function =
1927 sc.comp_unit
1928 ->FindFunctionByUID(function_die.GetID())
1929 .get();
1930 if (sc.function == NULL)
1931 sc.function =
1932 ParseCompileUnitFunction(sc, function_die);
1933
1934 if (sc.function &&
1935 (resolve_scope & eSymbolContextBlock))
1936 block_die =
1937 function_die.LookupDeepestBlock(file_vm_addr);
1938 }
1939
1940 if (sc.function != NULL) {
1941 Block &block = sc.function->GetBlock(true);
1942
1943 if (block_die)
1944 sc.block = block.FindBlockByID(block_die.GetID());
1945 else if (function_die)
1946 sc.block =
1947 block.FindBlockByID(function_die.GetID());
1948 }
1949 }
1950 }
1951
1952 sc_list.Append(sc);
1953 line_idx = line_table->FindLineEntryIndexByFileIndex(
1954 line_idx + 1, file_idx, found_line, true,
1955 &sc.line_entry);
1956 }
1957 }
1958 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1959 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001960 // sites by file and line and if the file spec matches that of
1961 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962 sc_list.Append(sc);
1963 }
1964 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1965 // only append the context if we aren't looking for inline call
Adrian Prantl05097242018-04-30 16:49:04 +00001966 // sites by file and line and if the file spec matches that of
1967 // the compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968 sc_list.Append(sc);
1969 }
1970
1971 if (!check_inlines)
1972 break;
1973 }
1974 }
1975 }
1976 }
1977 }
1978 return sc_list.GetSize() - prev_size;
1979}
1980
Jim Ingham7fca8c02017-04-28 00:51:06 +00001981void SymbolFileDWARF::PreloadSymbols() {
1982 std::lock_guard<std::recursive_mutex> guard(
1983 GetObjectFile()->GetModule()->GetMutex());
1984 Index();
1985}
1986
Kate Stoneb9c1b512016-09-06 20:57:50 +00001987void SymbolFileDWARF::Index() {
1988 if (m_indexed)
1989 return;
1990 m_indexed = true;
Pavel Labathf9d16472017-05-15 13:02:37 +00001991 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001992 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +00001993 func_cat, "SymbolFileDWARF::Index (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
1995
1996 DWARFDebugInfo *debug_info = DebugInfo();
1997 if (debug_info) {
1998 const uint32_t num_compile_units = GetNumCompileUnits();
1999 if (num_compile_units == 0)
2000 return;
2001
2002 std::vector<NameToDIE> function_basename_index(num_compile_units);
2003 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2004 std::vector<NameToDIE> function_method_index(num_compile_units);
2005 std::vector<NameToDIE> function_selector_index(num_compile_units);
2006 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2007 std::vector<NameToDIE> global_index(num_compile_units);
2008 std::vector<NameToDIE> type_index(num_compile_units);
2009 std::vector<NameToDIE> namespace_index(num_compile_units);
2010
Pavel Labath5c913e92017-05-05 11:16:59 +00002011 // std::vector<bool> might be implemented using bit test-and-set, so use
2012 // uint8_t instead.
2013 std::vector<uint8_t> clear_cu_dies(num_compile_units, false);
Zachary Turner3bc714b2017-03-02 00:05:25 +00002014 auto parser_fn = [debug_info, &function_basename_index,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 &function_fullname_index, &function_method_index,
2016 &function_selector_index, &objc_class_selectors_index,
2017 &global_index, &type_index,
Pavel Labath92f66772017-06-07 16:28:08 +00002018 &namespace_index](size_t cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002019 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 if (dwarf_cu) {
2021 dwarf_cu->Index(
2022 function_basename_index[cu_idx], function_fullname_index[cu_idx],
2023 function_method_index[cu_idx], function_selector_index[cu_idx],
2024 objc_class_selectors_index[cu_idx], global_index[cu_idx],
2025 type_index[cu_idx], namespace_index[cu_idx]);
2026 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002027 };
2028
Pavel Labath92f66772017-06-07 16:28:08 +00002029 auto extract_fn = [debug_info, &clear_cu_dies](size_t cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002030 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 if (dwarf_cu) {
Adrian Prantl05097242018-04-30 16:49:04 +00002032 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the DIEs
2033 // for a compile unit have already been parsed.
Pavel Labath5c913e92017-05-05 11:16:59 +00002034 if (dwarf_cu->ExtractDIEsIfNeeded(false) > 1)
2035 clear_cu_dies[cu_idx] = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002037 };
2038
2039 // Create a task runner that extracts dies for each DWARF compile unit in a
2040 // separate thread
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 //----------------------------------------------------------------------
2042 // First figure out which compile units didn't have their DIEs already
2043 // parsed and remember this. If no DIEs were parsed prior to this index
Adrian Prantl05097242018-04-30 16:49:04 +00002044 // function call, we are going to want to clear the CU dies after we are
2045 // done indexing to make sure we don't pull in all DWARF dies, but we need
2046 // to wait until all compile units have been indexed in case a DIE in one
2047 // compile unit refers to another and the indexes accesses those DIEs.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002048 //----------------------------------------------------------------------
Pavel Labath5c913e92017-05-05 11:16:59 +00002049 TaskMapOverInt(0, num_compile_units, extract_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002050
2051 // Now create a task runner that can index each DWARF compile unit in a
Adrian Prantl05097242018-04-30 16:49:04 +00002052 // separate thread so we can index quickly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002053
Pavel Labath5c913e92017-05-05 11:16:59 +00002054 TaskMapOverInt(0, num_compile_units, parser_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055
Pavel Labath5c913e92017-05-05 11:16:59 +00002056 auto finalize_fn = [](NameToDIE &index, std::vector<NameToDIE> &srcs) {
2057 for (auto &src : srcs)
2058 index.Append(src);
2059 index.Finalize();
2060 };
Kate Stoneb9c1b512016-09-06 20:57:50 +00002061
Pavel Labath5c913e92017-05-05 11:16:59 +00002062 TaskPool::RunTasks(
2063 [&]() {
2064 finalize_fn(m_function_basename_index, function_basename_index);
2065 },
2066 [&]() {
2067 finalize_fn(m_function_fullname_index, function_fullname_index);
2068 },
2069 [&]() { finalize_fn(m_function_method_index, function_method_index); },
2070 [&]() {
2071 finalize_fn(m_function_selector_index, function_selector_index);
2072 },
2073 [&]() {
2074 finalize_fn(m_objc_class_selectors_index, objc_class_selectors_index);
2075 },
2076 [&]() { finalize_fn(m_global_index, global_index); },
2077 [&]() { finalize_fn(m_type_index, type_index); },
2078 [&]() { finalize_fn(m_namespace_index, namespace_index); });
Kate Stoneb9c1b512016-09-06 20:57:50 +00002079
2080 //----------------------------------------------------------------------
2081 // Keep memory down by clearing DIEs for any compile units if indexing
2082 // caused us to load the compile unit's DIEs.
2083 //----------------------------------------------------------------------
2084 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2085 if (clear_cu_dies[cu_idx])
2086 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2087 }
2088
2089#if defined(ENABLE_DEBUG_PRINTF)
2090 StreamFile s(stdout, false);
2091 s.Printf("DWARF index for '%s':",
2092 GetObjectFile()->GetFileSpec().GetPath().c_str());
2093 s.Printf("\nFunction basenames:\n");
2094 m_function_basename_index.Dump(&s);
2095 s.Printf("\nFunction fullnames:\n");
2096 m_function_fullname_index.Dump(&s);
2097 s.Printf("\nFunction methods:\n");
2098 m_function_method_index.Dump(&s);
2099 s.Printf("\nFunction selectors:\n");
2100 m_function_selector_index.Dump(&s);
2101 s.Printf("\nObjective C class selectors:\n");
2102 m_objc_class_selectors_index.Dump(&s);
2103 s.Printf("\nGlobals and statics:\n");
2104 m_global_index.Dump(&s);
2105 s.Printf("\nTypes:\n");
2106 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002107 s.Printf("\nNamespaces:\n");
2108 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002109#endif
2110 }
2111}
2112
2113bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2114 const lldb_private::CompilerDeclContext *decl_ctx) {
2115 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +00002116 // Invalid namespace decl which means we aren't matching only things in
2117 // this symbol file, so return true to indicate it matches this symbol
2118 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119 return true;
2120 }
2121
2122 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2123 TypeSystem *type_system = GetTypeSystemForLanguage(
2124 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2125 if (decl_ctx_type_system == type_system)
2126 return true; // The type systems match, return true
2127
2128 // The namespace AST was valid, and it does not match...
2129 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2130
2131 if (log)
2132 GetObjectFile()->GetModule()->LogMessage(
2133 log, "Valid namespace does not match symbol file");
2134
2135 return false;
2136}
2137
2138uint32_t SymbolFileDWARF::FindGlobalVariables(
2139 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2140 bool append, uint32_t max_matches, VariableList &variables) {
2141 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2142
2143 if (log)
2144 GetObjectFile()->GetModule()->LogMessage(
2145 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2146 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2147 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2148 max_matches);
2149
2150 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2151 return 0;
2152
2153 DWARFDebugInfo *info = DebugInfo();
2154 if (info == NULL)
2155 return 0;
2156
2157 // If we aren't appending the results to this list, then clear the list
2158 if (!append)
2159 variables.Clear();
2160
Adrian Prantl05097242018-04-30 16:49:04 +00002161 // Remember how many variables are in the list before we search in case we
2162 // are appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 const uint32_t original_size = variables.GetSize();
2164
2165 DIEArray die_offsets;
2166
2167 if (m_using_apple_tables) {
2168 if (m_apple_names_ap.get()) {
2169 const char *name_cstr = name.GetCString();
2170 llvm::StringRef basename;
2171 llvm::StringRef context;
2172
2173 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2174 basename))
2175 basename = name_cstr;
2176
Pavel Labathb39fca92018-02-23 17:49:26 +00002177 m_apple_names_ap->FindByName(basename, die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 }
2179 } else {
2180 // Index the DWARF if we haven't already
2181 if (!m_indexed)
2182 Index();
2183
2184 m_global_index.Find(name, die_offsets);
2185 }
2186
2187 const size_t num_die_matches = die_offsets.size();
2188 if (num_die_matches) {
2189 SymbolContext sc;
2190 sc.module_sp = m_obj_file->GetModule();
2191 assert(sc.module_sp);
2192
2193 bool done = false;
2194 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2195 const DIERef &die_ref = die_offsets[i];
2196 DWARFDIE die = GetDIE(die_ref);
2197
2198 if (die) {
2199 switch (die.Tag()) {
2200 default:
2201 case DW_TAG_subprogram:
2202 case DW_TAG_inlined_subroutine:
2203 case DW_TAG_try_block:
2204 case DW_TAG_catch_block:
2205 break;
2206
2207 case DW_TAG_variable: {
2208 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2209
2210 if (parent_decl_ctx) {
2211 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2212 if (dwarf_ast) {
2213 CompilerDeclContext actual_parent_decl_ctx =
2214 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2215 if (!actual_parent_decl_ctx ||
2216 actual_parent_decl_ctx != *parent_decl_ctx)
2217 continue;
2218 }
2219 }
2220
2221 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2222 &variables);
2223
2224 if (variables.GetSize() - original_size >= max_matches)
2225 done = true;
2226 } break;
2227 }
2228 } else {
2229 if (m_using_apple_tables) {
2230 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2231 "the DWARF debug information has been modified (.apple_names "
2232 "accelerator table had bad die 0x%8.8x for '%s')\n",
2233 die_ref.die_offset, name.GetCString());
2234 }
2235 }
2236 }
2237 }
2238
2239 // Return the number of variable that were appended to the list
2240 const uint32_t num_matches = variables.GetSize() - original_size;
2241 if (log && num_matches > 0) {
2242 GetObjectFile()->GetModule()->LogMessage(
2243 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2244 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2245 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2246 max_matches, num_matches);
2247 }
2248 return num_matches;
2249}
2250
2251uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2252 bool append, uint32_t max_matches,
2253 VariableList &variables) {
2254 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2255
2256 if (log) {
2257 GetObjectFile()->GetModule()->LogMessage(
2258 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2259 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002260 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002261 }
2262
2263 DWARFDebugInfo *info = DebugInfo();
2264 if (info == NULL)
2265 return 0;
2266
2267 // If we aren't appending the results to this list, then clear the list
2268 if (!append)
2269 variables.Clear();
2270
Adrian Prantl05097242018-04-30 16:49:04 +00002271 // Remember how many variables are in the list before we search in case we
2272 // are appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273 const uint32_t original_size = variables.GetSize();
2274
2275 DIEArray die_offsets;
2276
2277 if (m_using_apple_tables) {
2278 if (m_apple_names_ap.get()) {
2279 DWARFMappedHash::DIEInfoArray hash_data_array;
2280 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2281 hash_data_array))
2282 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2283 }
2284 } else {
2285 // Index the DWARF if we haven't already
2286 if (!m_indexed)
2287 Index();
2288
2289 m_global_index.Find(regex, die_offsets);
2290 }
2291
2292 SymbolContext sc;
2293 sc.module_sp = m_obj_file->GetModule();
2294 assert(sc.module_sp);
2295
2296 const size_t num_matches = die_offsets.size();
2297 if (num_matches) {
2298 for (size_t i = 0; i < num_matches; ++i) {
2299 const DIERef &die_ref = die_offsets[i];
2300 DWARFDIE die = GetDIE(die_ref);
2301
2302 if (die) {
2303 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2304
2305 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2306
2307 if (variables.GetSize() - original_size >= max_matches)
2308 break;
2309 } else {
2310 if (m_using_apple_tables) {
2311 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2312 "the DWARF debug information has been modified (.apple_names "
2313 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002314 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002315 }
2316 }
2317 }
2318 }
2319
2320 // Return the number of variable that were appended to the list
2321 return variables.GetSize() - original_size;
2322}
2323
2324bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2325 bool include_inlines,
2326 SymbolContextList &sc_list) {
2327 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2328 return ResolveFunction(die, include_inlines, sc_list);
2329}
2330
2331bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2332 bool include_inlines,
2333 SymbolContextList &sc_list) {
2334 SymbolContext sc;
2335
2336 if (!orig_die)
2337 return false;
2338
2339 // If we were passed a die that is not a function, just return false...
2340 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2341 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2342 return false;
2343
2344 DWARFDIE die = orig_die;
2345 DWARFDIE inlined_die;
2346 if (die.Tag() == DW_TAG_inlined_subroutine) {
2347 inlined_die = die;
2348
2349 while (1) {
2350 die = die.GetParent();
2351
2352 if (die) {
2353 if (die.Tag() == DW_TAG_subprogram)
2354 break;
2355 } else
2356 break;
2357 }
2358 }
2359 assert(die && die.Tag() == DW_TAG_subprogram);
2360 if (GetFunction(die, sc)) {
2361 Address addr;
2362 // Parse all blocks if needed
2363 if (inlined_die) {
2364 Block &function_block = sc.function->GetBlock(true);
2365 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2366 if (sc.block == NULL)
2367 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2368 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2369 addr.Clear();
2370 } else {
2371 sc.block = NULL;
2372 addr = sc.function->GetAddressRange().GetBaseAddress();
2373 }
2374
2375 if (addr.IsValid()) {
2376 sc_list.Append(sc);
2377 return true;
2378 }
2379 }
2380
2381 return false;
2382}
2383
2384void SymbolFileDWARF::FindFunctions(const ConstString &name,
2385 const NameToDIE &name_to_die,
2386 bool include_inlines,
2387 SymbolContextList &sc_list) {
2388 DIEArray die_offsets;
2389 if (name_to_die.Find(name, die_offsets)) {
2390 ParseFunctions(die_offsets, include_inlines, sc_list);
2391 }
2392}
2393
2394void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2395 const NameToDIE &name_to_die,
2396 bool include_inlines,
2397 SymbolContextList &sc_list) {
2398 DIEArray die_offsets;
2399 if (name_to_die.Find(regex, die_offsets)) {
2400 ParseFunctions(die_offsets, include_inlines, sc_list);
2401 }
2402}
2403
2404void SymbolFileDWARF::FindFunctions(
2405 const RegularExpression &regex,
2406 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2407 SymbolContextList &sc_list) {
2408 DIEArray die_offsets;
2409 DWARFMappedHash::DIEInfoArray hash_data_array;
2410 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2411 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2412 ParseFunctions(die_offsets, include_inlines, sc_list);
2413 }
2414}
2415
2416void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2417 bool include_inlines,
2418 SymbolContextList &sc_list) {
2419 const size_t num_matches = die_offsets.size();
2420 if (num_matches) {
2421 for (size_t i = 0; i < num_matches; ++i)
2422 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2423 }
2424}
2425
2426bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2427 const DWARFDIE &die) {
2428 // If we have no parent decl context to match this DIE matches, and if the
Adrian Prantl05097242018-04-30 16:49:04 +00002429 // parent decl context isn't valid, we aren't trying to look for any
2430 // particular decl context so any die matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2432 return true;
2433
2434 if (die) {
2435 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2436 if (dwarf_ast) {
2437 CompilerDeclContext actual_decl_ctx =
2438 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2439 if (actual_decl_ctx)
2440 return actual_decl_ctx == *decl_ctx;
2441 }
2442 }
2443 return false;
2444}
2445
2446uint32_t
2447SymbolFileDWARF::FindFunctions(const ConstString &name,
2448 const CompilerDeclContext *parent_decl_ctx,
2449 uint32_t name_type_mask, bool include_inlines,
2450 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002451 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2452 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002453 name.AsCString());
2454
2455 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002456 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002457 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2458
2459 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2460
2461 if (log) {
2462 GetObjectFile()->GetModule()->LogMessage(
2463 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2464 "name_type_mask=0x%x, append=%u, sc_list)",
2465 name.GetCString(), name_type_mask, append);
2466 }
2467
2468 // If we aren't appending the results to this list, then clear the list
2469 if (!append)
2470 sc_list.Clear();
2471
2472 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2473 return 0;
2474
2475 // If name is empty then we won't find anything.
2476 if (name.IsEmpty())
2477 return 0;
2478
Adrian Prantl05097242018-04-30 16:49:04 +00002479 // Remember how many sc_list are in the list before we search in case we are
2480 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002481
Kate Stoneb9c1b512016-09-06 20:57:50 +00002482 const uint32_t original_size = sc_list.GetSize();
2483
2484 DWARFDebugInfo *info = DebugInfo();
2485 if (info == NULL)
2486 return 0;
2487
2488 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2489 if (m_using_apple_tables) {
2490 if (m_apple_names_ap.get()) {
2491
2492 DIEArray die_offsets;
2493
2494 uint32_t num_matches = 0;
2495
2496 if (name_type_mask & eFunctionNameTypeFull) {
2497 // If they asked for the full name, match what they typed. At some
Adrian Prantl05097242018-04-30 16:49:04 +00002498 // point we may want to canonicalize this (strip double spaces, etc.
2499 // For now, we just add all the dies that we find by exact match.
Pavel Labathb39fca92018-02-23 17:49:26 +00002500 num_matches =
2501 m_apple_names_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002502 for (uint32_t i = 0; i < num_matches; i++) {
2503 const DIERef &die_ref = die_offsets[i];
2504 DWARFDIE die = info->GetDIE(die_ref);
2505 if (die) {
2506 if (!DIEInDeclContext(parent_decl_ctx, die))
2507 continue; // The containing decl contexts don't match
2508
2509 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2510 if (ResolveFunction(die, include_inlines, sc_list))
2511 resolved_dies.insert(die.GetDIE());
2512 }
2513 } else {
2514 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2515 "the DWARF debug information has been modified (.apple_names "
2516 "accelerator table had bad die 0x%8.8x for '%s')",
Pavel Labathb39fca92018-02-23 17:49:26 +00002517 die_ref.die_offset, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002518 }
2519 }
2520 }
2521
2522 if (name_type_mask & eFunctionNameTypeSelector) {
2523 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2524 return 0; // no selectors in namespaces
2525
Pavel Labathb39fca92018-02-23 17:49:26 +00002526 num_matches =
2527 m_apple_names_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002528 // Now make sure these are actually ObjC methods. In this case we can
Adrian Prantl05097242018-04-30 16:49:04 +00002529 // simply look up the name, and if it is an ObjC method name, we're
2530 // good.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002531
2532 for (uint32_t i = 0; i < num_matches; i++) {
2533 const DIERef &die_ref = die_offsets[i];
2534 DWARFDIE die = info->GetDIE(die_ref);
2535 if (die) {
2536 const char *die_name = die.GetName();
2537 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2538 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2539 if (ResolveFunction(die, include_inlines, sc_list))
2540 resolved_dies.insert(die.GetDIE());
2541 }
2542 }
2543 } else {
2544 GetObjectFile()->GetModule()->ReportError(
2545 "the DWARF debug information has been modified (.apple_names "
2546 "accelerator table had bad die 0x%8.8x for '%s')",
Pavel Labathb39fca92018-02-23 17:49:26 +00002547 die_ref.die_offset, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002548 }
2549 }
2550 die_offsets.clear();
2551 }
2552
2553 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2554 name_type_mask & eFunctionNameTypeBase) {
2555 // The apple_names table stores just the "base name" of C++ methods in
Adrian Prantl05097242018-04-30 16:49:04 +00002556 // the table. So we have to extract the base name, look that up, and
2557 // if there is any other information in the name we were passed in we
2558 // have to post-filter based on that.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002559
2560 // FIXME: Arrange the logic above so that we don't calculate the base
2561 // name twice:
Pavel Labathb39fca92018-02-23 17:49:26 +00002562 num_matches =
2563 m_apple_names_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002564
2565 for (uint32_t i = 0; i < num_matches; i++) {
2566 const DIERef &die_ref = die_offsets[i];
2567 DWARFDIE die = info->GetDIE(die_ref);
2568 if (die) {
2569 if (!DIEInDeclContext(parent_decl_ctx, die))
2570 continue; // The containing decl contexts don't match
2571
2572 // If we get to here, the die is good, and we should add it:
2573 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2574 ResolveFunction(die, include_inlines, sc_list)) {
2575 bool keep_die = true;
2576 if ((name_type_mask &
2577 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2578 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002579 // We are looking for either basenames or methods, so we need
2580 // to trim out the ones we won't want by looking at the type
Kate Stoneb9c1b512016-09-06 20:57:50 +00002581 SymbolContext sc;
2582 if (sc_list.GetLastContext(sc)) {
2583 if (sc.block) {
2584 // We have an inlined function
2585 } else if (sc.function) {
2586 Type *type = sc.function->GetType();
2587
2588 if (type) {
2589 CompilerDeclContext decl_ctx =
2590 GetDeclContextContainingUID(type->GetID());
2591 if (decl_ctx.IsStructUnionOrClass()) {
2592 if (name_type_mask & eFunctionNameTypeBase) {
2593 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2594 keep_die = false;
2595 }
2596 } else {
2597 if (name_type_mask & eFunctionNameTypeMethod) {
2598 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2599 keep_die = false;
2600 }
2601 }
2602 } else {
2603 GetObjectFile()->GetModule()->ReportWarning(
2604 "function at die offset 0x%8.8x had no function type",
2605 die_ref.die_offset);
2606 }
2607 }
2608 }
2609 }
2610 if (keep_die)
2611 resolved_dies.insert(die.GetDIE());
2612 }
2613 } else {
2614 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2615 "the DWARF debug information has been modified (.apple_names "
2616 "accelerator table had bad die 0x%8.8x for '%s')",
Pavel Labathb39fca92018-02-23 17:49:26 +00002617 die_ref.die_offset, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002618 }
2619 }
2620 die_offsets.clear();
2621 }
2622 }
2623 } else {
2624
2625 // Index the DWARF if we haven't already
2626 if (!m_indexed)
2627 Index();
2628
2629 if (name_type_mask & eFunctionNameTypeFull) {
2630 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2631
2632 // FIXME Temporary workaround for global/anonymous namespace
Adrian Prantl05097242018-04-30 16:49:04 +00002633 // functions debugging FreeBSD and Linux binaries. If we didn't find any
2634 // functions in the global namespace try looking in the basename index
2635 // but ignore any returned functions that have a namespace but keep
2636 // functions which have an anonymous namespace
Kate Stoneb9c1b512016-09-06 20:57:50 +00002637 // TODO: The arch in the object file isn't correct for MSVC
Adrian Prantl05097242018-04-30 16:49:04 +00002638 // binaries on windows, we should find a way to make it correct and
2639 // handle those symbols as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640 if (sc_list.GetSize() == original_size) {
2641 ArchSpec arch;
2642 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002643 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002644 SymbolContextList temp_sc_list;
2645 FindFunctions(name, m_function_basename_index, include_inlines,
2646 temp_sc_list);
2647 SymbolContext sc;
2648 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2649 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2650 ConstString mangled_name =
2651 sc.GetFunctionName(Mangled::ePreferMangled);
2652 ConstString demangled_name =
2653 sc.GetFunctionName(Mangled::ePreferDemangled);
2654 // Mangled names on Linux and FreeBSD are of the form:
2655 // _ZN18function_namespace13function_nameEv.
2656 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2657 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2658 21)) {
2659 sc_list.Append(sc);
2660 }
2661 }
2662 }
2663 }
2664 }
2665 }
2666 DIEArray die_offsets;
2667 if (name_type_mask & eFunctionNameTypeBase) {
2668 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2669 for (uint32_t i = 0; i < num_base; i++) {
2670 DWARFDIE die = info->GetDIE(die_offsets[i]);
2671 if (die) {
2672 if (!DIEInDeclContext(parent_decl_ctx, die))
2673 continue; // The containing decl contexts don't match
2674
2675 // If we get to here, the die is good, and we should add it:
2676 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2677 if (ResolveFunction(die, include_inlines, sc_list))
2678 resolved_dies.insert(die.GetDIE());
2679 }
2680 }
2681 }
2682 die_offsets.clear();
2683 }
2684
2685 if (name_type_mask & eFunctionNameTypeMethod) {
2686 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2687 return 0; // no methods in namespaces
2688
2689 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2690 {
2691 for (uint32_t i = 0; i < num_base; i++) {
2692 DWARFDIE die = info->GetDIE(die_offsets[i]);
2693 if (die) {
2694 // If we get to here, the die is good, and we should add it:
2695 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2696 if (ResolveFunction(die, include_inlines, sc_list))
2697 resolved_dies.insert(die.GetDIE());
2698 }
2699 }
2700 }
2701 }
2702 die_offsets.clear();
2703 }
2704
2705 if ((name_type_mask & eFunctionNameTypeSelector) &&
2706 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2707 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2708 }
2709 }
2710
2711 // Return the number of variable that were appended to the list
2712 const uint32_t num_matches = sc_list.GetSize() - original_size;
2713
2714 if (log && num_matches > 0) {
2715 GetObjectFile()->GetModule()->LogMessage(
2716 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2717 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2718 "%u",
2719 name.GetCString(), name_type_mask, include_inlines, append,
2720 num_matches);
2721 }
2722 return num_matches;
2723}
2724
2725uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2726 bool include_inlines, bool append,
2727 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002728 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2729 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002730 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002731
2732 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2733
2734 if (log) {
2735 GetObjectFile()->GetModule()->LogMessage(
2736 log,
2737 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002738 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002739 }
2740
2741 // If we aren't appending the results to this list, then clear the list
2742 if (!append)
2743 sc_list.Clear();
2744
Adrian Prantl05097242018-04-30 16:49:04 +00002745 // Remember how many sc_list are in the list before we search in case we are
2746 // appending the results to a variable list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002747 uint32_t original_size = sc_list.GetSize();
2748
2749 if (m_using_apple_tables) {
2750 if (m_apple_names_ap.get())
2751 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2752 } else {
2753 // Index the DWARF if we haven't already
2754 if (!m_indexed)
2755 Index();
2756
2757 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2758
2759 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2760 }
2761
2762 // Return the number of variable that were appended to the list
2763 return sc_list.GetSize() - original_size;
2764}
2765
2766void SymbolFileDWARF::GetMangledNamesForFunction(
2767 const std::string &scope_qualified_name,
2768 std::vector<ConstString> &mangled_names) {
2769 DWARFDebugInfo *info = DebugInfo();
2770 uint32_t num_comp_units = 0;
2771 if (info)
2772 num_comp_units = info->GetNumCompileUnits();
2773
2774 for (uint32_t i = 0; i < num_comp_units; i++) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002775 DWARFUnit *cu = info->GetCompileUnitAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002776 if (cu == nullptr)
2777 continue;
2778
2779 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2780 if (dwo)
2781 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2782 }
2783
2784 NameToOffsetMap::iterator iter =
2785 m_function_scope_qualified_name_map.find(scope_qualified_name);
2786 if (iter == m_function_scope_qualified_name_map.end())
2787 return;
2788
2789 DIERefSetSP set_sp = (*iter).second;
2790 std::set<DIERef>::iterator set_iter;
2791 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2792 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2793 mangled_names.push_back(ConstString(die.GetMangledName()));
2794 }
2795}
2796
2797uint32_t SymbolFileDWARF::FindTypes(
2798 const SymbolContext &sc, const ConstString &name,
2799 const CompilerDeclContext *parent_decl_ctx, bool append,
2800 uint32_t max_matches,
2801 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2802 TypeMap &types) {
2803 // If we aren't appending the results to this list, then clear the list
2804 if (!append)
2805 types.Clear();
2806
2807 // Make sure we haven't already searched this SymbolFile before...
2808 if (searched_symbol_files.count(this))
2809 return 0;
2810 else
2811 searched_symbol_files.insert(this);
2812
2813 DWARFDebugInfo *info = DebugInfo();
2814 if (info == NULL)
2815 return 0;
2816
2817 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2818
2819 if (log) {
2820 if (parent_decl_ctx)
2821 GetObjectFile()->GetModule()->LogMessage(
2822 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2823 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2824 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2825 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2826 else
2827 GetObjectFile()->GetModule()->LogMessage(
2828 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2829 "NULL, append=%u, max_matches=%u, type_list)",
2830 name.GetCString(), append, max_matches);
2831 }
2832
2833 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2834 return 0;
2835
2836 DIEArray die_offsets;
2837
2838 if (m_using_apple_tables) {
2839 if (m_apple_types_ap.get()) {
Pavel Labathb39fca92018-02-23 17:49:26 +00002840 m_apple_types_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841 }
2842 } else {
2843 if (!m_indexed)
2844 Index();
2845
2846 m_type_index.Find(name, die_offsets);
2847 }
2848
2849 const size_t num_die_matches = die_offsets.size();
2850
2851 if (num_die_matches) {
2852 const uint32_t initial_types_size = types.GetSize();
2853 for (size_t i = 0; i < num_die_matches; ++i) {
2854 const DIERef &die_ref = die_offsets[i];
2855 DWARFDIE die = GetDIE(die_ref);
2856
2857 if (die) {
2858 if (!DIEInDeclContext(parent_decl_ctx, die))
2859 continue; // The containing decl contexts don't match
2860
2861 Type *matching_type = ResolveType(die, true, true);
2862 if (matching_type) {
2863 // We found a type pointer, now find the shared pointer form our type
2864 // list
2865 types.InsertUnique(matching_type->shared_from_this());
2866 if (types.GetSize() >= max_matches)
2867 break;
2868 }
2869 } else {
2870 if (m_using_apple_tables) {
2871 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2872 "the DWARF debug information has been modified (.apple_types "
2873 "accelerator table had bad die 0x%8.8x for '%s')\n",
2874 die_ref.die_offset, name.GetCString());
2875 }
2876 }
2877 }
2878 const uint32_t num_matches = types.GetSize() - initial_types_size;
2879 if (log && num_matches) {
2880 if (parent_decl_ctx) {
2881 GetObjectFile()->GetModule()->LogMessage(
2882 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2883 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2884 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2885 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2886 num_matches);
2887 } else {
2888 GetObjectFile()->GetModule()->LogMessage(
2889 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2890 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2891 name.GetCString(), append, max_matches, num_matches);
2892 }
2893 }
2894 return num_matches;
2895 } else {
2896 UpdateExternalModuleListIfNeeded();
2897
2898 for (const auto &pair : m_external_type_modules) {
2899 ModuleSP external_module_sp = pair.second;
2900 if (external_module_sp) {
2901 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2902 if (sym_vendor) {
2903 const uint32_t num_external_matches =
2904 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2905 max_matches, searched_symbol_files, types);
2906 if (num_external_matches)
2907 return num_external_matches;
2908 }
2909 }
2910 }
2911 }
2912
2913 return 0;
2914}
2915
2916size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2917 bool append, TypeMap &types) {
2918 if (!append)
2919 types.Clear();
2920
2921 if (context.empty())
2922 return 0;
2923
2924 DIEArray die_offsets;
2925
2926 ConstString name = context.back().name;
2927
2928 if (!name)
2929 return 0;
2930
2931 if (m_using_apple_tables) {
2932 if (m_apple_types_ap.get()) {
Pavel Labathb39fca92018-02-23 17:49:26 +00002933 m_apple_types_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002934 }
2935 } else {
2936 if (!m_indexed)
2937 Index();
2938
2939 m_type_index.Find(name, die_offsets);
2940 }
2941
2942 const size_t num_die_matches = die_offsets.size();
2943
2944 if (num_die_matches) {
2945 size_t num_matches = 0;
2946 for (size_t i = 0; i < num_die_matches; ++i) {
2947 const DIERef &die_ref = die_offsets[i];
2948 DWARFDIE die = GetDIE(die_ref);
2949
2950 if (die) {
2951 std::vector<CompilerContext> die_context;
2952 die.GetDWOContext(die_context);
2953 if (die_context != context)
2954 continue;
2955
2956 Type *matching_type = ResolveType(die, true, true);
2957 if (matching_type) {
2958 // We found a type pointer, now find the shared pointer form our type
2959 // list
2960 types.InsertUnique(matching_type->shared_from_this());
2961 ++num_matches;
2962 }
2963 } else {
2964 if (m_using_apple_tables) {
2965 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2966 "the DWARF debug information has been modified (.apple_types "
2967 "accelerator table had bad die 0x%8.8x for '%s')\n",
2968 die_ref.die_offset, name.GetCString());
2969 }
2970 }
2971 }
2972 return num_matches;
2973 }
2974 return 0;
2975}
2976
2977CompilerDeclContext
2978SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2979 const CompilerDeclContext *parent_decl_ctx) {
2980 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2981
2982 if (log) {
2983 GetObjectFile()->GetModule()->LogMessage(
2984 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2985 name.GetCString());
2986 }
2987
2988 CompilerDeclContext namespace_decl_ctx;
2989
2990 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2991 return namespace_decl_ctx;
2992
2993 DWARFDebugInfo *info = DebugInfo();
2994 if (info) {
2995 DIEArray die_offsets;
2996
Adrian Prantl05097242018-04-30 16:49:04 +00002997 // Index if we already haven't to make sure the compile units get indexed
2998 // and make their global DIE index list
Kate Stoneb9c1b512016-09-06 20:57:50 +00002999 if (m_using_apple_tables) {
3000 if (m_apple_namespaces_ap.get()) {
Pavel Labathb39fca92018-02-23 17:49:26 +00003001 m_apple_namespaces_ap->FindByName(name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002 }
3003 } else {
3004 if (!m_indexed)
3005 Index();
3006
3007 m_namespace_index.Find(name, die_offsets);
3008 }
3009
3010 const size_t num_matches = die_offsets.size();
3011 if (num_matches) {
3012 for (size_t i = 0; i < num_matches; ++i) {
3013 const DIERef &die_ref = die_offsets[i];
3014 DWARFDIE die = GetDIE(die_ref);
3015
3016 if (die) {
3017 if (!DIEInDeclContext(parent_decl_ctx, die))
3018 continue; // The containing decl contexts don't match
3019
3020 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3021 if (dwarf_ast) {
3022 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
3023 if (namespace_decl_ctx)
3024 break;
3025 }
3026 } else {
3027 if (m_using_apple_tables) {
3028 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3029 "the DWARF debug information has been modified "
3030 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
3031 "'%s')\n",
3032 die_ref.die_offset, name.GetCString());
3033 }
3034 }
3035 }
3036 }
3037 }
3038 if (log && namespace_decl_ctx) {
3039 GetObjectFile()->GetModule()->LogMessage(
3040 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
3041 "CompilerDeclContext(%p/%p) \"%s\"",
3042 name.GetCString(),
3043 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
3044 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
3045 namespace_decl_ctx.GetName().AsCString("<NULL>"));
3046 }
3047
3048 return namespace_decl_ctx;
3049}
3050
3051TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3052 bool resolve_function_context) {
3053 TypeSP type_sp;
3054 if (die) {
3055 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3056 if (type_ptr == NULL) {
3057 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3058 assert(lldb_cu);
3059 SymbolContext sc(lldb_cu);
3060 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3061 while (parent_die != nullptr) {
3062 if (parent_die->Tag() == DW_TAG_subprogram)
3063 break;
3064 parent_die = parent_die->GetParent();
3065 }
3066 SymbolContext sc_backup = sc;
3067 if (resolve_function_context && parent_die != nullptr &&
3068 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3069 sc = sc_backup;
3070
3071 type_sp = ParseType(sc, die, NULL);
3072 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3073 // Grab the existing type from the master types lists
3074 type_sp = type_ptr->shared_from_this();
3075 }
3076 }
3077 return type_sp;
3078}
3079
3080DWARFDIE
3081SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3082 if (orig_die) {
3083 DWARFDIE die = orig_die;
3084
3085 while (die) {
3086 // If this is the original DIE that we are searching for a declaration
3087 // for, then don't look in the cache as we don't want our own decl
3088 // context to be our decl context...
3089 if (orig_die != die) {
3090 switch (die.Tag()) {
3091 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003092 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003093 case DW_TAG_namespace:
3094 case DW_TAG_structure_type:
3095 case DW_TAG_union_type:
3096 case DW_TAG_class_type:
3097 case DW_TAG_lexical_block:
3098 case DW_TAG_subprogram:
3099 return die;
Sean Callananb4945782017-04-24 22:11:10 +00003100 case DW_TAG_inlined_subroutine: {
3101 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3102 if (abs_die) {
3103 return abs_die;
3104 }
3105 break;
3106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003107 default:
3108 break;
3109 }
3110 }
3111
3112 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3113 if (spec_die) {
3114 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3115 if (decl_ctx_die)
3116 return decl_ctx_die;
3117 }
3118
3119 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3120 if (abs_die) {
3121 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3122 if (decl_ctx_die)
3123 return decl_ctx_die;
3124 }
3125
3126 die = die.GetParent();
3127 }
3128 }
3129 return DWARFDIE();
3130}
3131
3132Symbol *
3133SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3134 Symbol *objc_class_symbol = NULL;
3135 if (m_obj_file) {
3136 Symtab *symtab = m_obj_file->GetSymtab();
3137 if (symtab) {
3138 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3139 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3140 Symtab::eVisibilityAny);
3141 }
3142 }
3143 return objc_class_symbol;
3144}
3145
3146// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
Adrian Prantl05097242018-04-30 16:49:04 +00003147// they don't then we can end up looking through all class types for a complete
3148// type and never find the full definition. We need to know if this attribute
3149// is supported, so we determine this here and cache th result. We also need to
3150// worry about the debug map
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151// DWARF file
3152// if we are doing darwin DWARF in .o file debugging.
3153bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003154 DWARFUnit *cu) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3156 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3157 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3158 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3159 else {
3160 DWARFDebugInfo *debug_info = DebugInfo();
3161 const uint32_t num_compile_units = GetNumCompileUnits();
3162 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003163 DWARFUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164 if (dwarf_cu != cu &&
3165 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3166 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3167 break;
3168 }
3169 }
3170 }
3171 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3172 GetDebugMapSymfile())
3173 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3174 }
3175 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003176}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003177
3178// This function can be used when a DIE is found that is a forward declaration
3179// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003180TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3181 const DWARFDIE &die, const ConstString &type_name,
3182 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003183
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184 TypeSP type_sp;
3185
3186 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3187 return type_sp;
3188
3189 DIEArray die_offsets;
3190
3191 if (m_using_apple_tables) {
3192 if (m_apple_types_ap.get()) {
Pavel Labathb39fca92018-02-23 17:49:26 +00003193 m_apple_types_ap->FindCompleteObjCClassByName(
3194 type_name.GetStringRef(), die_offsets, must_be_implementation);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003195 }
3196 } else {
3197 if (!m_indexed)
3198 Index();
3199
3200 m_type_index.Find(type_name, die_offsets);
3201 }
3202
3203 const size_t num_matches = die_offsets.size();
3204
3205 if (num_matches) {
3206 for (size_t i = 0; i < num_matches; ++i) {
3207 const DIERef &die_ref = die_offsets[i];
3208 DWARFDIE type_die = GetDIE(die_ref);
3209
3210 if (type_die) {
3211 bool try_resolving_type = false;
3212
Adrian Prantl05097242018-04-30 16:49:04 +00003213 // Don't try and resolve the DIE we are looking for with the DIE
3214 // itself!
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 if (type_die != die) {
3216 switch (type_die.Tag()) {
3217 case DW_TAG_class_type:
3218 case DW_TAG_structure_type:
3219 try_resolving_type = true;
3220 break;
3221 default:
3222 break;
3223 }
3224 }
3225
3226 if (try_resolving_type) {
3227 if (must_be_implementation &&
3228 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3229 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3230 DW_AT_APPLE_objc_complete_type, 0);
3231
3232 if (try_resolving_type) {
3233 Type *resolved_type = ResolveType(type_die, false, true);
3234 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3235 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3236 " (cu 0x%8.8" PRIx64 ")\n",
3237 die.GetID(),
3238 m_obj_file->GetFileSpec().GetFilename().AsCString(
3239 "<Unknown>"),
3240 type_die.GetID(), type_cu->GetID());
3241
3242 if (die)
3243 GetDIEToType()[die.GetDIE()] = resolved_type;
3244 type_sp = resolved_type->shared_from_this();
3245 break;
3246 }
3247 }
3248 }
3249 } else {
3250 if (m_using_apple_tables) {
3251 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3252 "the DWARF debug information has been modified (.apple_types "
3253 "accelerator table had bad die 0x%8.8x for '%s')\n",
3254 die_ref.die_offset, type_name.GetCString());
3255 }
3256 }
3257 }
3258 }
3259 return type_sp;
3260}
Greg Claytona8022fa2012-04-24 21:22:41 +00003261
Greg Clayton80c26302012-02-05 06:12:47 +00003262//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00003263// This function helps to ensure that the declaration contexts match for two
3264// different DIEs. Often times debug information will refer to a forward
3265// declaration of a type (the equivalent of "struct my_struct;". There will
3266// often be a declaration of that type elsewhere that has the full definition.
3267// When we go looking for the full type "my_struct", we will find one or more
3268// matches in the accelerator tables and we will then need to make sure the
3269// type was in the same declaration context as the original DIE. This function
3270// can efficiently compare two DIEs and will return true when the declaration
3271// context matches, and false when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003272//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003273bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3274 const DWARFDIE &die2) {
3275 if (die1 == die2)
3276 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003277
Kate Stoneb9c1b512016-09-06 20:57:50 +00003278 DWARFDIECollection decl_ctx_1;
3279 DWARFDIECollection decl_ctx_2;
Adrian Prantl05097242018-04-30 16:49:04 +00003280 // The declaration DIE stack is a stack of the declaration context DIEs all
3281 // the way back to the compile unit. If a type "T" is declared inside a class
3282 // "B", and class "B" is declared inside a class "A" and class "A" is in a
3283 // namespace "lldb", and the namespace is in a compile unit, there will be a
3284 // stack of DIEs:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003285 //
3286 // [0] DW_TAG_class_type for "B"
3287 // [1] DW_TAG_class_type for "A"
3288 // [2] DW_TAG_namespace for "lldb"
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003289 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290 //
Adrian Prantl05097242018-04-30 16:49:04 +00003291 // We grab both contexts and make sure that everything matches all the way
3292 // back to the compiler unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293
3294 // First lets grab the decl contexts for both DIEs
3295 die1.GetDeclContextDIEs(decl_ctx_1);
3296 die2.GetDeclContextDIEs(decl_ctx_2);
Adrian Prantl05097242018-04-30 16:49:04 +00003297 // Make sure the context arrays have the same size, otherwise we are done
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 const size_t count1 = decl_ctx_1.Size();
3299 const size_t count2 = decl_ctx_2.Size();
3300 if (count1 != count2)
3301 return false;
3302
Adrian Prantl05097242018-04-30 16:49:04 +00003303 // Make sure the DW_TAG values match all the way back up the compile unit. If
3304 // they don't, then we are done.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003305 DWARFDIE decl_ctx_die1;
3306 DWARFDIE decl_ctx_die2;
3307 size_t i;
3308 for (i = 0; i < count1; i++) {
3309 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3310 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3311 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3312 return false;
3313 }
Greg Clayton890ff562012-02-02 05:48:16 +00003314#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003315
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 // Make sure the top item in the decl context die array is always
Adrian Prantl05097242018-04-30 16:49:04 +00003317 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
3318 // something went wrong in the DWARFDIE::GetDeclContextDIEs()
3319 // function.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003320 dw_tag_t cu_tag = decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag();
3321 UNUSED_IF_ASSERT_DISABLED(cu_tag);
3322 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003323
Greg Clayton890ff562012-02-02 05:48:16 +00003324#endif
Adrian Prantl05097242018-04-30 16:49:04 +00003325 // Always skip the compile unit when comparing by only iterating up to "count
3326 // - 1". Here we compare the names as we go.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 for (i = 0; i < count1 - 1; i++) {
3328 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3329 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3330 const char *name1 = decl_ctx_die1.GetName();
3331 const char *name2 = decl_ctx_die2.GetName();
Adrian Prantl05097242018-04-30 16:49:04 +00003332 // If the string was from a DW_FORM_strp, then the pointer will often be
3333 // the same!
Kate Stoneb9c1b512016-09-06 20:57:50 +00003334 if (name1 == name2)
3335 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003336
Adrian Prantl05097242018-04-30 16:49:04 +00003337 // Name pointers are not equal, so only compare the strings if both are not
3338 // NULL.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003339 if (name1 && name2) {
3340 // If the strings don't compare, we are done...
3341 if (strcmp(name1, name2) != 0)
3342 return false;
3343 } else {
3344 // One name was NULL while the other wasn't
3345 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003347 }
Adrian Prantl05097242018-04-30 16:49:04 +00003348 // We made it through all of the checks and the declaration contexts are
3349 // equal.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003350 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003351}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003352
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3354 const DWARFDeclContext &dwarf_decl_ctx) {
3355 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003356
Kate Stoneb9c1b512016-09-06 20:57:50 +00003357 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3358 if (dwarf_decl_ctx_count > 0) {
3359 const ConstString type_name(dwarf_decl_ctx[0].name);
3360 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003361
Kate Stoneb9c1b512016-09-06 20:57:50 +00003362 if (type_name) {
3363 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3364 DWARF_LOG_LOOKUPS));
3365 if (log) {
3366 GetObjectFile()->GetModule()->LogMessage(
3367 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3368 "s, qualified-name='%s')",
3369 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3370 dwarf_decl_ctx.GetQualifiedName());
3371 }
3372
3373 DIEArray die_offsets;
3374
3375 if (m_using_apple_tables) {
3376 if (m_apple_types_ap.get()) {
3377 const bool has_tag =
3378 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3379 DWARFMappedHash::eAtomTypeTag);
3380 const bool has_qualified_name_hash =
3381 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3382 DWARFMappedHash::eAtomTypeQualNameHash);
3383 if (has_tag && has_qualified_name_hash) {
3384 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
Pavel Labathb39fca92018-02-23 17:49:26 +00003385 const uint32_t qualified_name_hash = llvm::djbHash(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003386 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003387 GetObjectFile()->GetModule()->LogMessage(
3388 log, "FindByNameAndTagAndQualifiedNameHash()");
3389 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
Pavel Labathb39fca92018-02-23 17:49:26 +00003390 type_name.GetStringRef(), tag, qualified_name_hash,
3391 die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003392 } else if (has_tag) {
3393 if (log)
3394 GetObjectFile()->GetModule()->LogMessage(log,
3395 "FindByNameAndTag()");
Pavel Labathb39fca92018-02-23 17:49:26 +00003396 m_apple_types_ap->FindByNameAndTag(type_name.GetStringRef(), tag,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003397 die_offsets);
3398 } else {
Pavel Labathb39fca92018-02-23 17:49:26 +00003399 m_apple_types_ap->FindByName(type_name.GetStringRef(), die_offsets);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003400 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003402 } else {
3403 if (!m_indexed)
3404 Index();
3405
3406 m_type_index.Find(type_name, die_offsets);
3407 }
3408
3409 const size_t num_matches = die_offsets.size();
3410
Adrian Prantl05097242018-04-30 16:49:04 +00003411 // Get the type system that we are looking to find a type for. We will
3412 // use this to ensure any matches we find are in a language that this
3413 // type system supports
Kate Stoneb9c1b512016-09-06 20:57:50 +00003414 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3415 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3416 ? nullptr
3417 : GetTypeSystemForLanguage(language);
3418
3419 if (num_matches) {
3420 for (size_t i = 0; i < num_matches; ++i) {
3421 const DIERef &die_ref = die_offsets[i];
3422 DWARFDIE type_die = GetDIE(die_ref);
3423
3424 if (type_die) {
3425 // Make sure type_die's langauge matches the type system we are
Adrian Prantl05097242018-04-30 16:49:04 +00003426 // looking for. We don't want to find a "Foo" type from Java if we
3427 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 if (type_system &&
3429 !type_system->SupportsLanguage(type_die.GetLanguage()))
3430 continue;
3431 bool try_resolving_type = false;
3432
3433 // Don't try and resolve the DIE we are looking for with the DIE
3434 // itself!
3435 const dw_tag_t type_tag = type_die.Tag();
3436 // Make sure the tags match
3437 if (type_tag == tag) {
3438 // The tags match, lets try resolving this type
3439 try_resolving_type = true;
3440 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003441 // The tags don't match, but we need to watch our for a forward
3442 // declaration for a struct and ("struct foo") ends up being a
3443 // class ("class foo { ... };") or vice versa.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003444 switch (type_tag) {
3445 case DW_TAG_class_type:
Adrian Prantl05097242018-04-30 16:49:04 +00003446 // We had a "class foo", see if we ended up with a "struct foo
3447 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00003448 try_resolving_type = (tag == DW_TAG_structure_type);
3449 break;
3450 case DW_TAG_structure_type:
Adrian Prantl05097242018-04-30 16:49:04 +00003451 // We had a "struct foo", see if we ended up with a "class foo
3452 // { ... };"
Kate Stoneb9c1b512016-09-06 20:57:50 +00003453 try_resolving_type = (tag == DW_TAG_class_type);
3454 break;
3455 default:
Adrian Prantl05097242018-04-30 16:49:04 +00003456 // Tags don't match, don't event try to resolve using this type
3457 // whose name matches....
Kate Stoneb9c1b512016-09-06 20:57:50 +00003458 break;
3459 }
3460 }
3461
3462 if (try_resolving_type) {
3463 DWARFDeclContext type_dwarf_decl_ctx;
3464 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3465
3466 if (log) {
3467 GetObjectFile()->GetModule()->LogMessage(
3468 log, "SymbolFileDWARF::"
3469 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3470 "qualified-name='%s') trying die=0x%8.8x (%s)",
3471 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3472 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3473 type_dwarf_decl_ctx.GetQualifiedName());
3474 }
3475
3476 // Make sure the decl contexts match all the way up
3477 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3478 Type *resolved_type = ResolveType(type_die, false);
3479 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3480 type_sp = resolved_type->shared_from_this();
3481 break;
3482 }
3483 }
3484 } else {
3485 if (log) {
3486 std::string qualified_name;
3487 type_die.GetQualifiedName(qualified_name);
3488 GetObjectFile()->GetModule()->LogMessage(
3489 log, "SymbolFileDWARF::"
3490 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3491 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3492 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3493 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3494 qualified_name.c_str());
3495 }
3496 }
3497 } else {
3498 if (m_using_apple_tables) {
3499 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3500 "the DWARF debug information has been modified (.apple_types "
3501 "accelerator table had bad die 0x%8.8x for '%s')\n",
3502 die_ref.die_offset, type_name.GetCString());
3503 }
3504 }
3505 }
3506 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003508 }
3509 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003510}
3511
Kate Stoneb9c1b512016-09-06 20:57:50 +00003512TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3513 bool *type_is_new_ptr) {
3514 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003515
Kate Stoneb9c1b512016-09-06 20:57:50 +00003516 if (die) {
3517 TypeSystem *type_system =
3518 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003519
Kate Stoneb9c1b512016-09-06 20:57:50 +00003520 if (type_system) {
3521 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3522 if (dwarf_ast) {
3523 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3524 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3525 if (type_sp) {
3526 TypeList *type_list = GetTypeList();
3527 if (type_list)
3528 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003529
Kate Stoneb9c1b512016-09-06 20:57:50 +00003530 if (die.Tag() == DW_TAG_subprogram) {
3531 DIERef die_ref = die.GetDIERef();
3532 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3533 .GetScopeQualifiedName()
3534 .AsCString(""));
3535 if (scope_qualified_name.size()) {
3536 NameToOffsetMap::iterator iter =
3537 m_function_scope_qualified_name_map.find(
3538 scope_qualified_name);
3539 if (iter != m_function_scope_qualified_name_map.end())
3540 (*iter).second->insert(die_ref);
3541 else {
3542 DIERefSetSP new_set(new std::set<DIERef>);
3543 new_set->insert(die_ref);
3544 m_function_scope_qualified_name_map.emplace(
3545 std::make_pair(scope_qualified_name, new_set));
3546 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003547 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003548 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003549 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003550 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003552 }
3553
3554 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003555}
3556
Kate Stoneb9c1b512016-09-06 20:57:50 +00003557size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3558 const DWARFDIE &orig_die,
3559 bool parse_siblings, bool parse_children) {
3560 size_t types_added = 0;
3561 DWARFDIE die = orig_die;
3562 while (die) {
3563 bool type_is_new = false;
3564 if (ParseType(sc, die, &type_is_new).get()) {
3565 if (type_is_new)
3566 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568
3569 if (parse_children && die.HasChildren()) {
3570 if (die.Tag() == DW_TAG_subprogram) {
3571 SymbolContext child_sc(sc);
3572 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3573 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3574 } else
3575 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3576 }
3577
3578 if (parse_siblings)
3579 die = die.GetSibling();
3580 else
3581 die.Clear();
3582 }
3583 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003584}
3585
Kate Stoneb9c1b512016-09-06 20:57:50 +00003586size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3587 assert(sc.comp_unit && sc.function);
3588 size_t functions_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003589 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003590 if (dwarf_cu) {
3591 const dw_offset_t function_die_offset = sc.function->GetID();
3592 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3593 if (function_die) {
3594 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3595 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003598
Kate Stoneb9c1b512016-09-06 20:57:50 +00003599 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003600}
3601
Kate Stoneb9c1b512016-09-06 20:57:50 +00003602size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3603 // At least a compile unit must be valid
3604 assert(sc.comp_unit);
3605 size_t types_added = 0;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003606 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003607 if (dwarf_cu) {
3608 if (sc.function) {
3609 dw_offset_t function_die_offset = sc.function->GetID();
3610 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3611 if (func_die && func_die.HasChildren()) {
3612 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3613 }
3614 } else {
3615 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3616 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3617 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003619 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003620 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003621
Kate Stoneb9c1b512016-09-06 20:57:50 +00003622 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003623}
3624
Kate Stoneb9c1b512016-09-06 20:57:50 +00003625size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3626 if (sc.comp_unit != NULL) {
3627 DWARFDebugInfo *info = DebugInfo();
3628 if (info == NULL)
3629 return 0;
3630
3631 if (sc.function) {
3632 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3633
3634 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3635 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3636 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3637 const size_t num_variables = ParseVariables(
3638 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3639
3640 // Let all blocks know they have parse all their variables
3641 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3642 return num_variables;
3643 }
3644 } else if (sc.comp_unit) {
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003645 DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003646
3647 if (dwarf_cu == NULL)
3648 return 0;
3649
3650 uint32_t vars_added = 0;
3651 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3652
3653 if (variables.get() == NULL) {
3654 variables.reset(new VariableList());
3655 sc.comp_unit->SetVariableList(variables);
3656
3657 DIEArray die_offsets;
3658 if (m_using_apple_tables) {
3659 if (m_apple_names_ap.get()) {
3660 DWARFMappedHash::DIEInfoArray hash_data_array;
3661 if (m_apple_names_ap->AppendAllDIEsInRange(
3662 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3663 hash_data_array)) {
3664 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003665 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003666 }
3667 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00003668 // Index if we already haven't to make sure the compile units get
3669 // indexed and make their global DIE index list
Kate Stoneb9c1b512016-09-06 20:57:50 +00003670 if (!m_indexed)
3671 Index();
3672
3673 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3674 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003675 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003676
Kate Stoneb9c1b512016-09-06 20:57:50 +00003677 const size_t num_matches = die_offsets.size();
3678 if (num_matches) {
3679 for (size_t i = 0; i < num_matches; ++i) {
3680 const DIERef &die_ref = die_offsets[i];
3681 DWARFDIE die = GetDIE(die_ref);
3682 if (die) {
3683 VariableSP var_sp(
3684 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3685 if (var_sp) {
3686 variables->AddVariableIfUnique(var_sp);
3687 ++vars_added;
3688 }
3689 } else {
3690 if (m_using_apple_tables) {
3691 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3692 "the DWARF debug information has been modified "
3693 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3694 die_ref.die_offset);
3695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003697 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003699 }
3700 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003702 }
3703 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003704}
3705
Kate Stoneb9c1b512016-09-06 20:57:50 +00003706VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3707 const DWARFDIE &die,
3708 const lldb::addr_t func_low_pc) {
3709 if (die.GetDWARF() != this)
3710 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003711
Kate Stoneb9c1b512016-09-06 20:57:50 +00003712 VariableSP var_sp;
3713 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003714 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003715
Kate Stoneb9c1b512016-09-06 20:57:50 +00003716 var_sp = GetDIEToVariable()[die.GetDIE()];
3717 if (var_sp)
3718 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003719
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720 const dw_tag_t tag = die.Tag();
3721 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003722
Kate Stoneb9c1b512016-09-06 20:57:50 +00003723 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3724 (tag == DW_TAG_formal_parameter && sc.function)) {
3725 DWARFAttributes attributes;
3726 const size_t num_attributes = die.GetAttributes(attributes);
3727 DWARFDIE spec_die;
3728 if (num_attributes > 0) {
3729 const char *name = NULL;
3730 const char *mangled = NULL;
3731 Declaration decl;
3732 uint32_t i;
3733 DWARFFormValue type_die_form;
3734 DWARFExpression location(die.GetCU());
3735 bool is_external = false;
3736 bool is_artificial = false;
3737 bool location_is_const_value_data = false;
3738 bool has_explicit_location = false;
3739 DWARFFormValue const_value;
3740 Variable::RangeList scope_ranges;
3741 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003742
Kate Stoneb9c1b512016-09-06 20:57:50 +00003743 for (i = 0; i < num_attributes; ++i) {
3744 dw_attr_t attr = attributes.AttributeAtIndex(i);
3745 DWARFFormValue form_value;
3746
3747 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3748 switch (attr) {
3749 case DW_AT_decl_file:
3750 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3751 form_value.Unsigned()));
3752 break;
3753 case DW_AT_decl_line:
3754 decl.SetLine(form_value.Unsigned());
3755 break;
3756 case DW_AT_decl_column:
3757 decl.SetColumn(form_value.Unsigned());
3758 break;
3759 case DW_AT_name:
3760 name = form_value.AsCString();
3761 break;
3762 case DW_AT_linkage_name:
3763 case DW_AT_MIPS_linkage_name:
3764 mangled = form_value.AsCString();
3765 break;
3766 case DW_AT_type:
3767 type_die_form = form_value;
3768 break;
3769 case DW_AT_external:
3770 is_external = form_value.Boolean();
3771 break;
3772 case DW_AT_const_value:
3773 // If we have already found a DW_AT_location attribute, ignore this
3774 // attribute.
3775 if (!has_explicit_location) {
3776 location_is_const_value_data = true;
3777 // The constant value will be either a block, a data value or a
3778 // string.
3779 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3780 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3781 // Retrieve the value as a block expression.
3782 uint32_t block_offset =
3783 form_value.BlockData() - debug_info_data.GetDataStart();
3784 uint32_t block_length = form_value.Unsigned();
3785 location.CopyOpcodeData(module, debug_info_data, block_offset,
3786 block_length);
3787 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3788 // Retrieve the value as a data expression.
3789 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3790 DWARFFormValue::GetFixedFormSizesForAddressSize(
3791 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3792 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3793 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3794 uint32_t data_length =
3795 fixed_form_sizes.GetSize(form_value.Form());
3796 if (data_length == 0) {
3797 const uint8_t *data_pointer = form_value.BlockData();
3798 if (data_pointer) {
3799 form_value.Unsigned();
3800 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3801 // we need to get the byte size of the type later after we
3802 // create the variable
3803 const_value = form_value;
3804 }
3805 } else
3806 location.CopyOpcodeData(module, debug_info_data, data_offset,
3807 data_length);
3808 } else {
3809 // Retrieve the value as a string expression.
3810 if (form_value.Form() == DW_FORM_strp) {
3811 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3812 DWARFFormValue::GetFixedFormSizesForAddressSize(
3813 attributes.CompileUnitAtIndex(i)
3814 ->GetAddressByteSize(),
3815 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3816 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3817 uint32_t data_length =
3818 fixed_form_sizes.GetSize(form_value.Form());
3819 location.CopyOpcodeData(module, debug_info_data, data_offset,
3820 data_length);
3821 } else {
3822 const char *str = form_value.AsCString();
3823 uint32_t string_offset =
3824 str - (const char *)debug_info_data.GetDataStart();
3825 uint32_t string_length = strlen(str) + 1;
3826 location.CopyOpcodeData(module, debug_info_data,
3827 string_offset, string_length);
3828 }
3829 }
3830 }
3831 break;
3832 case DW_AT_location: {
3833 location_is_const_value_data = false;
3834 has_explicit_location = true;
3835 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3836 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3837
3838 uint32_t block_offset =
3839 form_value.BlockData() - debug_info_data.GetDataStart();
3840 uint32_t block_length = form_value.Unsigned();
3841 location.CopyOpcodeData(module, get_debug_info_data(),
3842 block_offset, block_length);
3843 } else {
3844 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3845 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3846
3847 size_t loc_list_length = DWARFExpression::LocationListSize(
3848 die.GetCU(), debug_loc_data, debug_loc_offset);
3849 if (loc_list_length > 0) {
3850 location.CopyOpcodeData(module, debug_loc_data,
3851 debug_loc_offset, loc_list_length);
3852 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3853 location.SetLocationListSlide(
3854 func_low_pc -
3855 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3856 }
3857 }
3858 } break;
3859 case DW_AT_specification:
3860 spec_die = GetDIE(DIERef(form_value));
3861 break;
3862 case DW_AT_start_scope: {
3863 if (form_value.Form() == DW_FORM_sec_offset) {
3864 DWARFRangeList dwarf_scope_ranges;
3865 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003866 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3867 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003868 dwarf_scope_ranges);
3869
3870 // All DW_AT_start_scope are relative to the base address of the
3871 // compile unit. We add the compile unit base address to make
3872 // sure all the addresses are properly fixed up.
3873 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3874 i < count; ++i) {
3875 const DWARFRangeList::Entry &range =
3876 dwarf_scope_ranges.GetEntryRef(i);
3877 scope_ranges.Append(range.GetRangeBase() +
3878 die.GetCU()->GetBaseAddress(),
3879 range.GetByteSize());
3880 }
3881 } else {
3882 // TODO: Handle the case when DW_AT_start_scope have form
3883 // constant. The
3884 // dwarf spec is a bit ambiguous about what is the expected
Adrian Prantl05097242018-04-30 16:49:04 +00003885 // behavior in case the enclosing block have a non coninious
3886 // address range and the DW_AT_start_scope entry have a form
3887 // constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003888 GetObjectFile()->GetModule()->ReportWarning(
3889 "0x%8.8" PRIx64
3890 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3891 die.GetID(), form_value.Form());
3892 }
3893
3894 scope_ranges.Sort();
3895 scope_ranges.CombineConsecutiveRanges();
3896 } break;
3897 case DW_AT_artificial:
3898 is_artificial = form_value.Boolean();
3899 break;
3900 case DW_AT_accessibility:
3901 break; // accessibility =
3902 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3903 case DW_AT_declaration:
3904 case DW_AT_description:
3905 case DW_AT_endianity:
3906 case DW_AT_segment:
3907 case DW_AT_visibility:
3908 default:
3909 case DW_AT_abstract_origin:
3910 case DW_AT_sibling:
3911 break;
3912 }
3913 }
3914 }
3915
3916 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3917 const dw_tag_t parent_tag = die.GetParent().Tag();
3918 bool is_static_member =
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003919 (parent_tag == DW_TAG_compile_unit ||
3920 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003921 (parent_context_die.Tag() == DW_TAG_class_type ||
3922 parent_context_die.Tag() == DW_TAG_structure_type);
3923
3924 ValueType scope = eValueTypeInvalid;
3925
3926 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3927 SymbolContextScope *symbol_context_scope = NULL;
3928
Sam McCall30621392016-11-22 11:40:25 +00003929 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003930 if (!mangled) {
3931 // LLDB relies on the mangled name (DW_TAG_linkage_name or
Adrian Prantl05097242018-04-30 16:49:04 +00003932 // DW_AT_MIPS_linkage_name) to generate fully qualified names
3933 // of global variables with commands like "frame var j". For
3934 // example, if j were an int variable holding a value 4 and
3935 // declared in a namespace B which in turn is contained in a
3936 // namespace A, the command "frame var j" returns
3937 // "(int) A::B::j = 4".
3938 // If the compiler does not emit a linkage name, we should be
3939 // able to generate a fully qualified name from the
3940 // declaration context.
Jan Kratochvil55c84b12018-04-30 16:04:32 +00003941 if ((parent_tag == DW_TAG_compile_unit ||
3942 parent_tag == DW_TAG_partial_unit) &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00003943 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3944 DWARFDeclContext decl_ctx;
3945
3946 die.GetDWARFDeclContext(decl_ctx);
3947 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3948 }
3949 }
3950
Kate Stoneb9c1b512016-09-06 20:57:50 +00003951 if (tag == DW_TAG_formal_parameter)
3952 scope = eValueTypeVariableArgument;
3953 else {
Sam McCall30621392016-11-22 11:40:25 +00003954 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3955 // or static variable, so we have to do a little digging:
3956 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3957 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3958 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3959 // Clang likes to combine small global variables into the same symbol
3960 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3961 // so we need to look through the whole expression.
3962 bool is_static_lifetime =
3963 has_explicit_mangled ||
3964 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003965 // Check if the location has a DW_OP_addr with any address value...
3966 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3967 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003968 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003969 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3970 if (op_error) {
3971 StreamString strm;
3972 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3973 NULL);
3974 GetObjectFile()->GetModule()->ReportError(
3975 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003976 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003977 }
Sam McCall30621392016-11-22 11:40:25 +00003978 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3979 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003980 }
3981 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3982
Sam McCall30621392016-11-22 11:40:25 +00003983 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003984 if (is_external)
3985 scope = eValueTypeVariableGlobal;
3986 else
3987 scope = eValueTypeVariableStatic;
3988
3989 if (debug_map_symfile) {
Adrian Prantl05097242018-04-30 16:49:04 +00003990 // When leaving the DWARF in the .o files on darwin, when we have a
3991 // global variable that wasn't initialized, the .o file might not
3992 // have allocated a virtual address for the global variable. In
3993 // this case it will have created a symbol for the global variable
3994 // that is undefined/data and external and the value will be the
3995 // byte size of the variable. When we do the address map in
3996 // SymbolFileDWARFDebugMap we rely on having an address, we need to
3997 // do some magic here so we can get the correct address for our
3998 // global variable. The address for all of these entries will be
3999 // zero, and there will be an undefined symbol in this object file,
4000 // and the executable will have a matching symbol with a good
4001 // address. So here we dig up the correct address and replace it in
4002 // the location for the variable, and set the variable's symbol
4003 // context scope to be that of the main executable so the file
4004 // address will resolve correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00004005 bool linked_oso_file_addr = false;
4006 if (is_external && location_DW_OP_addr == 0) {
4007 // we have a possible uninitialized extern global
4008 ConstString const_name(mangled ? mangled : name);
4009 ObjectFile *debug_map_objfile =
4010 debug_map_symfile->GetObjectFile();
4011 if (debug_map_objfile) {
4012 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
4013 if (debug_map_symtab) {
4014 Symbol *exe_symbol =
4015 debug_map_symtab->FindFirstSymbolWithNameAndType(
4016 const_name, eSymbolTypeData, Symtab::eDebugYes,
4017 Symtab::eVisibilityExtern);
4018 if (exe_symbol) {
4019 if (exe_symbol->ValueIsAddress()) {
4020 const addr_t exe_file_addr =
4021 exe_symbol->GetAddressRef().GetFileAddress();
4022 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4023 if (location.Update_DW_OP_addr(exe_file_addr)) {
4024 linked_oso_file_addr = true;
4025 symbol_context_scope = exe_symbol;
4026 }
4027 }
4028 }
4029 }
4030 }
4031 }
4032 }
4033
4034 if (!linked_oso_file_addr) {
4035 // The DW_OP_addr is not zero, but it contains a .o file address
Adrian Prantl05097242018-04-30 16:49:04 +00004036 // which needs to be linked up correctly.
Kate Stoneb9c1b512016-09-06 20:57:50 +00004037 const lldb::addr_t exe_file_addr =
4038 debug_map_symfile->LinkOSOFileAddress(this,
4039 location_DW_OP_addr);
4040 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4041 // Update the file address for this variable
4042 location.Update_DW_OP_addr(exe_file_addr);
4043 } else {
4044 // Variable didn't make it into the final executable
4045 return var_sp;
4046 }
4047 }
4048 }
4049 } else {
4050 if (location_is_const_value_data)
4051 scope = eValueTypeVariableStatic;
4052 else {
4053 scope = eValueTypeVariableLocal;
4054 if (debug_map_symfile) {
4055 // We need to check for TLS addresses that we need to fixup
4056 if (location.ContainsThreadLocalStorage()) {
4057 location.LinkThreadLocalStorage(
4058 debug_map_symfile->GetObjectFile()->GetModule(),
4059 [this, debug_map_symfile](
4060 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4061 return debug_map_symfile->LinkOSOFileAddress(
4062 this, unlinked_file_addr);
4063 });
4064 scope = eValueTypeVariableThreadLocal;
4065 }
4066 }
4067 }
4068 }
4069 }
4070
4071 if (symbol_context_scope == NULL) {
4072 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004073 case DW_TAG_subprogram:
4074 case DW_TAG_inlined_subroutine:
4075 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004076 if (sc.function) {
4077 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4078 sc_parent_die.GetID());
4079 if (symbol_context_scope == NULL)
4080 symbol_context_scope = sc.function;
4081 }
4082 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004083
Kate Stoneb9c1b512016-09-06 20:57:50 +00004084 default:
4085 symbol_context_scope = sc.comp_unit;
4086 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004087 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004088 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004089
Kate Stoneb9c1b512016-09-06 20:57:50 +00004090 if (symbol_context_scope) {
4091 SymbolFileTypeSP type_sp(
4092 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4093
4094 if (const_value.Form() && type_sp && type_sp->GetType())
4095 location.CopyOpcodeData(const_value.Unsigned(),
4096 type_sp->GetType()->GetByteSize(),
4097 die.GetCU()->GetAddressByteSize());
4098
4099 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4100 symbol_context_scope, scope_ranges, &decl,
4101 location, is_external, is_artificial,
4102 is_static_member));
4103
4104 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4105 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00004106 // Not ready to parse this variable yet. It might be a global or static
4107 // variable that is in a function scope and the function in the symbol
4108 // context wasn't filled in yet
Kate Stoneb9c1b512016-09-06 20:57:50 +00004109 return var_sp;
4110 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004111 }
Adrian Prantl05097242018-04-30 16:49:04 +00004112 // Cache var_sp even if NULL (the variable was just a specification or was
4113 // missing vital information to be able to be displayed in the debugger
4114 // (missing location due to optimization, etc)) so we don't re-parse this
4115 // DIE over and over later...
Kate Stoneb9c1b512016-09-06 20:57:50 +00004116 GetDIEToVariable()[die.GetDIE()] = var_sp;
4117 if (spec_die)
4118 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4119 }
4120 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004121}
4122
Kate Stoneb9c1b512016-09-06 20:57:50 +00004123DWARFDIE
4124SymbolFileDWARF::FindBlockContainingSpecification(
4125 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4126 // Give the concrete function die specified by "func_die_offset", find the
4127 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4128 // to "spec_block_die_offset"
4129 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4130 spec_block_die_offset);
4131}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004132
Kate Stoneb9c1b512016-09-06 20:57:50 +00004133DWARFDIE
4134SymbolFileDWARF::FindBlockContainingSpecification(
4135 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4136 if (die) {
4137 switch (die.Tag()) {
4138 case DW_TAG_subprogram:
4139 case DW_TAG_inlined_subroutine:
4140 case DW_TAG_lexical_block: {
4141 if (die.GetAttributeValueAsReference(
4142 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4143 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004144
Kate Stoneb9c1b512016-09-06 20:57:50 +00004145 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4146 DW_INVALID_OFFSET) ==
4147 spec_block_die_offset)
4148 return die;
4149 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004150 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004151
4152 // Give the concrete function die specified by "func_die_offset", find the
4153 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4154 // to "spec_block_die_offset"
4155 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4156 child_die = child_die.GetSibling()) {
4157 DWARFDIE result_die =
4158 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4159 if (result_die)
4160 return result_die;
4161 }
4162 }
4163
4164 return DWARFDIE();
4165}
4166
4167size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4168 const DWARFDIE &orig_die,
4169 const lldb::addr_t func_low_pc,
4170 bool parse_siblings, bool parse_children,
4171 VariableList *cc_variable_list) {
4172 if (!orig_die)
4173 return 0;
4174
4175 VariableListSP variable_list_sp;
4176
4177 size_t vars_added = 0;
4178 DWARFDIE die = orig_die;
4179 while (die) {
4180 dw_tag_t tag = die.Tag();
4181
4182 // Check to see if we have already parsed this variable or constant?
4183 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4184 if (var_sp) {
4185 if (cc_variable_list)
4186 cc_variable_list->AddVariableIfUnique(var_sp);
4187 } else {
4188 // We haven't already parsed it, lets do that now.
4189 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4190 (tag == DW_TAG_formal_parameter && sc.function)) {
4191 if (variable_list_sp.get() == NULL) {
4192 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4193 dw_tag_t parent_tag = sc_parent_die.Tag();
4194 switch (parent_tag) {
4195 case DW_TAG_compile_unit:
Jan Kratochvil55c84b12018-04-30 16:04:32 +00004196 case DW_TAG_partial_unit:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004197 if (sc.comp_unit != NULL) {
4198 variable_list_sp = sc.comp_unit->GetVariableList(false);
4199 if (variable_list_sp.get() == NULL) {
4200 variable_list_sp.reset(new VariableList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004201 }
4202 } else {
4203 GetObjectFile()->GetModule()->ReportError(
4204 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4205 "symbol context for 0x%8.8" PRIx64
4206 " %s.\n",
4207 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4208 orig_die.GetID(), orig_die.GetTagAsCString());
4209 }
4210 break;
4211
4212 case DW_TAG_subprogram:
4213 case DW_TAG_inlined_subroutine:
4214 case DW_TAG_lexical_block:
4215 if (sc.function != NULL) {
4216 // Check to see if we already have parsed the variables for the
4217 // given scope
4218
4219 Block *block = sc.function->GetBlock(true).FindBlockByID(
4220 sc_parent_die.GetID());
4221 if (block == NULL) {
Adrian Prantl05097242018-04-30 16:49:04 +00004222 // This must be a specification or abstract origin with a
4223 // concrete block counterpart in the current function. We need
Kate Stoneb9c1b512016-09-06 20:57:50 +00004224 // to find the concrete block so we can correctly add the
4225 // variable to it
4226 const DWARFDIE concrete_block_die =
4227 FindBlockContainingSpecification(
4228 DIERef(sc.function->GetID(), this),
4229 sc_parent_die.GetOffset());
4230 if (concrete_block_die)
4231 block = sc.function->GetBlock(true).FindBlockByID(
4232 concrete_block_die.GetID());
4233 }
4234
4235 if (block != NULL) {
4236 const bool can_create = false;
4237 variable_list_sp = block->GetBlockVariableList(can_create);
4238 if (variable_list_sp.get() == NULL) {
4239 variable_list_sp.reset(new VariableList());
4240 block->SetVariableList(variable_list_sp);
4241 }
4242 }
4243 }
4244 break;
4245
4246 default:
4247 GetObjectFile()->GetModule()->ReportError(
4248 "didn't find appropriate parent DIE for variable list for "
4249 "0x%8.8" PRIx64 " %s.\n",
4250 orig_die.GetID(), orig_die.GetTagAsCString());
4251 break;
4252 }
4253 }
4254
4255 if (variable_list_sp) {
4256 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4257 if (var_sp) {
4258 variable_list_sp->AddVariableIfUnique(var_sp);
4259 if (cc_variable_list)
4260 cc_variable_list->AddVariableIfUnique(var_sp);
4261 ++vars_added;
4262 }
4263 }
4264 }
4265 }
4266
4267 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4268
4269 if (!skip_children && parse_children && die.HasChildren()) {
4270 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4271 true, cc_variable_list);
4272 }
4273
4274 if (parse_siblings)
4275 die = die.GetSibling();
4276 else
4277 die.Clear();
4278 }
4279 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004280}
4281
4282//------------------------------------------------------------------
4283// PluginInterface protocol
4284//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004285ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4286
4287uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4288
4289void SymbolFileDWARF::DumpIndexes() {
4290 StreamFile s(stdout, false);
4291
4292 s.Printf(
4293 "DWARF index for (%s) '%s':",
4294 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4295 GetObjectFile()->GetFileSpec().GetPath().c_str());
4296 s.Printf("\nFunction basenames:\n");
4297 m_function_basename_index.Dump(&s);
4298 s.Printf("\nFunction fullnames:\n");
4299 m_function_fullname_index.Dump(&s);
4300 s.Printf("\nFunction methods:\n");
4301 m_function_method_index.Dump(&s);
4302 s.Printf("\nFunction selectors:\n");
4303 m_function_selector_index.Dump(&s);
4304 s.Printf("\nObjective C class selectors:\n");
4305 m_objc_class_selectors_index.Dump(&s);
4306 s.Printf("\nGlobals and statics:\n");
4307 m_global_index.Dump(&s);
4308 s.Printf("\nTypes:\n");
4309 m_type_index.Dump(&s);
4310 s.Printf("\nNamespaces:\n");
4311 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004312}
4313
Kate Stoneb9c1b512016-09-06 20:57:50 +00004314SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4315 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4316 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4317 if (module_sp) {
4318 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4319 if (sym_vendor)
4320 m_debug_map_symfile =
4321 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004322 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004323 }
4324 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004325}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004326
4327DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004328SymbolFileDWARF::GetLocationListFormat() const {
4329 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004330}
Tamas Berghammer963ce482017-08-25 13:56:14 +00004331
4332SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
4333 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00004334 ModuleSpec module_spec;
4335 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
4336 module_spec.GetSymbolFileSpec() =
4337 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
4338 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Tamas Berghammer963ce482017-08-25 13:56:14 +00004339 if (dwp_filespec.Exists()) {
4340 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
4341 dwp_filespec);
4342 }
4343 });
4344 return m_dwp_symfile.get();
4345}