blob: 27752da86eb8611ad0042fc7d0d459517eb0a98d [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
Robert Flackeb83fab2015-05-15 18:59:59 +000016#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000018#include "lldb/Core/ModuleList.h"
19#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Scalar.h"
22#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000023#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/Value.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"
33
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000034#include "lldb/Interpreter/OptionValueFileSpecList.h"
35#include "lldb/Interpreter/OptionValueProperties.h"
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000038#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000039#include "lldb/Symbol/ClangUtil.h"
40#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000041#include "lldb/Symbol/CompilerDecl.h"
42#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000043#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000044#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000047#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000048#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Symbol/VariableList.h"
50
Jim Inghamaa816b82015-09-02 01:59:14 +000051#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
52#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000053
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
55
Francis Ricci7ddfe8e2017-09-19 15:38:30 +000056#include "lldb/Host/TaskPool.h"
Tamas Berghammer2ff88702015-10-23 10:34:49 +000057
Greg Clayton261ac3f2015-08-28 01:01:03 +000058#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000059#include "DWARFASTParserClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "DWARFCompileUnit.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000061#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "DWARFDebugAbbrev.h"
63#include "DWARFDebugAranges.h"
64#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000066#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "DWARFDebugPubnames.h"
68#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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 // check whether we have a windows path, and so the first character
168 // is a drive-letter not a hostname.
169 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177static const char *resolveCompDir(const char *path_from_dwarf) {
178 if (!path_from_dwarf)
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000179 return nullptr;
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)
185 return nullptr;
186
187 bool is_symlink = false;
188 FileSpec local_path_spec(local_path, false);
189 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
190 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
191 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
192 local_path_spec, true);
193
194 if (!is_symlink)
195 return local_path;
196
Zachary Turner7d86ee52017-03-08 17:56:08 +0000197 namespace fs = llvm::sys::fs;
198 if (fs::get_file_type(local_path_spec.GetPath(), false) !=
199 fs::file_type::symlink_file)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 return local_path;
201
202 FileSpec resolved_local_path_spec;
203 const auto error =
204 FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
205 if (error.Success())
206 return resolved_local_path_spec.GetCString();
207
208 return nullptr;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211void SymbolFileDWARF::Initialize() {
212 LogChannelDWARF::Initialize();
213 PluginManager::RegisterPlugin(GetPluginNameStatic(),
214 GetPluginDescriptionStatic(), CreateInstance,
215 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000216}
217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
219 if (!PluginManager::GetSettingForSymbolFilePlugin(
220 debugger, PluginProperties::GetSettingName())) {
221 const bool is_global_setting = true;
222 PluginManager::CreateSettingForSymbolFilePlugin(
223 debugger, GetGlobalPluginProperties()->GetValueProperties(),
224 ConstString("Properties for the dwarf symbol-file plug-in."),
225 is_global_setting);
226 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227}
228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229void SymbolFileDWARF::Terminate() {
230 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000231 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
235 static ConstString g_name("dwarf");
236 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
240 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241}
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
244 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247TypeList *SymbolFileDWARF::GetTypeList() {
248 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
249 if (debug_map_symfile)
250 return debug_map_symfile->GetTypeList();
251 else
252 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000253}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
255 dw_offset_t max_die_offset, uint32_t type_mask,
256 TypeSet &type_set) {
257 if (die) {
258 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (die_offset >= max_die_offset)
261 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 if (die_offset >= min_die_offset) {
264 const dw_tag_t tag = die.Tag();
265
266 bool add_type = false;
267
268 switch (tag) {
269 case DW_TAG_array_type:
270 add_type = (type_mask & eTypeClassArray) != 0;
271 break;
272 case DW_TAG_unspecified_type:
273 case DW_TAG_base_type:
274 add_type = (type_mask & eTypeClassBuiltin) != 0;
275 break;
276 case DW_TAG_class_type:
277 add_type = (type_mask & eTypeClassClass) != 0;
278 break;
279 case DW_TAG_structure_type:
280 add_type = (type_mask & eTypeClassStruct) != 0;
281 break;
282 case DW_TAG_union_type:
283 add_type = (type_mask & eTypeClassUnion) != 0;
284 break;
285 case DW_TAG_enumeration_type:
286 add_type = (type_mask & eTypeClassEnumeration) != 0;
287 break;
288 case DW_TAG_subroutine_type:
289 case DW_TAG_subprogram:
290 case DW_TAG_inlined_subroutine:
291 add_type = (type_mask & eTypeClassFunction) != 0;
292 break;
293 case DW_TAG_pointer_type:
294 add_type = (type_mask & eTypeClassPointer) != 0;
295 break;
296 case DW_TAG_rvalue_reference_type:
297 case DW_TAG_reference_type:
298 add_type = (type_mask & eTypeClassReference) != 0;
299 break;
300 case DW_TAG_typedef:
301 add_type = (type_mask & eTypeClassTypedef) != 0;
302 break;
303 case DW_TAG_ptr_to_member_type:
304 add_type = (type_mask & eTypeClassMemberPointer) != 0;
305 break;
306 }
307
308 if (add_type) {
309 const bool assert_not_being_parsed = true;
310 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
311 if (type) {
312 if (type_set.find(type) == type_set.end())
313 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317
318 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
319 child_die = child_die.GetSibling()) {
320 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
321 }
322 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000323}
324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
326 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000327
328{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 CompileUnit *comp_unit = NULL;
332 DWARFCompileUnit *dwarf_cu = NULL;
333 if (sc_scope)
334 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (comp_unit) {
337 dwarf_cu = GetDWARFCompileUnit(comp_unit);
338 if (dwarf_cu == 0)
339 return 0;
340 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
341 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
342 } else {
343 DWARFDebugInfo *info = DebugInfo();
344 if (info) {
345 const size_t num_cus = info->GetNumCompileUnits();
346 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
347 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
348 if (dwarf_cu) {
349 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000350 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000352 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
354
355 std::set<CompilerType> compiler_type_set;
356 size_t num_types_added = 0;
357 for (Type *type : type_set) {
358 CompilerType compiler_type = type->GetForwardCompilerType();
359 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
360 compiler_type_set.insert(compiler_type);
361 type_list.Insert(type->shared_from_this());
362 ++num_types_added;
363 }
364 }
365 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000366}
367
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368//----------------------------------------------------------------------
369// Gets the first parent that is a lexical block, function or inlined
370// subroutine, or compile unit.
371//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000372DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
374 DWARFDIE die;
375 for (die = child_die.GetParent(); die; die = die.GetParent()) {
376 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 switch (tag) {
379 case DW_TAG_compile_unit:
380 case DW_TAG_subprogram:
381 case DW_TAG_inlined_subroutine:
382 case DW_TAG_lexical_block:
383 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 }
386 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
390 : SymbolFile(objfile), UserID(0), // Used by SymbolFileDWARFDebugMap to when
391 // this class parses .o files to contain
392 // the .o file index/ID
393 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
394 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
395 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
396 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
397 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
398 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
399 m_apple_objc_ap(), m_function_basename_index(),
400 m_function_fullname_index(), m_function_method_index(),
401 m_function_selector_index(), m_objc_class_selectors_index(),
402 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
403 m_using_apple_tables(false), m_fetched_external_modules(false),
404 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
405 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407SymbolFileDWARF::~SymbolFileDWARF() {}
408
409static const ConstString &GetDWARFMachOSegmentName() {
410 static ConstString g_dwarf_section_name("__DWARF");
411 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
415 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
416 if (debug_map_symfile)
417 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
418 else
419 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000420}
421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
423 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
424 TypeSystem *type_system;
425 if (debug_map_symfile) {
426 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
427 } else {
428 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
429 if (type_system)
430 type_system->SetSymbolFile(this);
431 }
432 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000433}
434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435void SymbolFileDWARF::InitializeObject() {
436 ModuleSP module_sp(m_obj_file->GetModule());
437 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000438 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000439 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
441
442 // Memory map the DWARF mach-o segment so we have everything mmap'ed
443 // to keep our heap memory usage down.
444 if (section)
445 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
446 }
447
448 get_apple_names_data();
449 if (m_data_apple_names.m_data.GetByteSize() > 0) {
450 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
451 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000452 if (m_apple_names_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000453 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000454 else
455 m_apple_names_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 }
457 get_apple_types_data();
458 if (m_data_apple_types.m_data.GetByteSize() > 0) {
459 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
460 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000461 if (m_apple_types_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000462 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000463 else
464 m_apple_types_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 }
466
467 get_apple_namespaces_data();
468 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
469 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
470 m_data_apple_namespaces.m_data, get_debug_str_data(),
471 ".apple_namespaces"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000472 if (m_apple_namespaces_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000473 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000474 else
475 m_apple_namespaces_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 }
477
478 get_apple_objc_data();
479 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
480 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
481 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000482 if (m_apple_objc_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000483 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000484 else
485 m_apple_objc_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 }
487}
488
489bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
490 return version == 2 || version == 3 || version == 4;
491}
492
493uint32_t SymbolFileDWARF::CalculateAbilities() {
494 uint32_t abilities = 0;
495 if (m_obj_file != NULL) {
496 const Section *section = NULL;
497 const SectionList *section_list = m_obj_file->GetSectionList();
498 if (section_list == NULL)
499 return 0;
500
Greg Claytonea5df102017-07-24 18:40:33 +0000501 // On non Apple platforms we might have .debug_types debug info that
502 // is created by using "-fdebug-types-section". LLDB currently will try
503 // to load this debug info, but it causes crashes during debugging when
504 // types are missing since it doesn't know how to parse the info in
505 // the .debug_types type units. This causes all complex debug info
506 // types to be unresolved. Because this causes LLDB to crash and since
507 // it really doesn't provide a solid debuggiung experience, we should
508 // disable trying to debug this kind of DWARF until support gets
509 // added or deprecated.
510 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
511 m_obj_file->GetModule()->ReportWarning(
512 "lldb doesn’t support .debug_types debug info");
513 return 0;
514 }
515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 uint64_t debug_abbrev_file_size = 0;
517 uint64_t debug_info_file_size = 0;
518 uint64_t debug_line_file_size = 0;
519
520 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
521
522 if (section)
523 section_list = &section->GetChildren();
524
525 section =
526 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
527 if (section != NULL) {
528 debug_info_file_size = section->GetFileSize();
529
530 section =
531 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
532 .get();
533 if (section)
534 debug_abbrev_file_size = section->GetFileSize();
535
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000536 DWARFDebugAbbrev *abbrev = DebugAbbrev();
537 if (abbrev) {
538 std::set<dw_form_t> invalid_forms;
539 abbrev->GetUnsupportedForms(invalid_forms);
540 if (!invalid_forms.empty()) {
541 StreamString error;
542 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
543 for (auto form : invalid_forms)
544 error.Printf(" %#x", form);
545 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
546 return 0;
547 }
548 }
549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 section =
551 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
552 .get();
553 if (section)
554 debug_line_file_size = section->GetFileSize();
555 } else {
556 const char *symfile_dir_cstr =
557 m_obj_file->GetFileSpec().GetDirectory().GetCString();
558 if (symfile_dir_cstr) {
559 if (strcasestr(symfile_dir_cstr, ".dsym")) {
560 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
561 // We have a dSYM file that didn't have a any debug info.
562 // If the string table has a size of 1, then it was made from
563 // an executable with no debug info, or from an executable that
564 // was stripped.
565 section =
566 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
567 .get();
568 if (section && section->GetFileSize() == 1) {
569 m_obj_file->GetModule()->ReportWarning(
570 "empty dSYM file detected, dSYM was created with an "
571 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577
578 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
579 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
580 LocalVariables | VariableTypes;
581
582 if (debug_line_file_size > 0)
583 abilities |= LineTables;
584 }
585 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586}
587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588const DWARFDataExtractor &
589SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
590 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000591 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
592 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
593 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
598 DWARFDataExtractor &data) {
599 ModuleSP module_sp(m_obj_file->GetModule());
600 const SectionList *section_list = module_sp->GetSectionList();
601 if (section_list) {
602 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
603 if (section_sp) {
604 // See if we memory mapped the DWARF segment?
605 if (m_dwarf_data.GetByteSize()) {
606 data.SetData(m_dwarf_data, section_sp->GetOffset(),
607 section_sp->GetFileSize());
608 } else {
609 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
610 data.Clear();
611 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614}
615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
617 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
618 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619}
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
622 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
623}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
626 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
627 m_data_debug_aranges);
628}
629
630const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
631 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
632}
633
634const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
635 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
636}
637
638const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
639 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
640}
641
642const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
643 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
644}
645
646const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
647 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
648}
649
650const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
651 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
652 m_data_debug_ranges);
653}
654
655const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
656 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
657}
658
659const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
660 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
661 m_data_debug_str_offsets);
662}
663
664const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
665 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
666}
667
668const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
669 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
670}
671
672const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
673 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
674 m_data_apple_namespaces);
675}
676
677const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
678 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
679}
680
681DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
682 if (m_abbr.get() == NULL) {
683 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
684 if (debug_abbrev_data.GetByteSize() > 0) {
685 m_abbr.reset(new DWARFDebugAbbrev());
686 if (m_abbr.get())
687 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689 }
690 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691}
692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
694 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695}
696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
698 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000699 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
700 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
701 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 if (get_debug_info_data().GetByteSize() > 0) {
703 m_info.reset(new DWARFDebugInfo());
704 if (m_info.get()) {
705 m_info->SetDwarfData(this);
706 }
Greg Clayton1f746072012-08-29 21:13:06 +0000707 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 }
709 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710}
711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
713 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714}
715
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716DWARFCompileUnit *
717SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
718 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000719 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720
721 DWARFDebugInfo *info = DebugInfo();
722 if (info) {
723 // Just a normal DWARF file whose user ID for the compile unit is
724 // the DWARF offset itself
725
726 DWARFCompileUnit *dwarf_cu =
727 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
728 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
729 dwarf_cu->SetUserData(comp_unit);
730 return dwarf_cu;
731 }
732 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733}
734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
736 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000737 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
738 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
739 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 if (get_debug_ranges_data().GetByteSize() > 0) {
741 m_ranges.reset(new DWARFDebugRanges());
742 if (m_ranges.get())
743 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 }
746 return m_ranges.get();
747}
748
749const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
750 return m_ranges.get();
751}
752
753lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit *dwarf_cu,
754 uint32_t cu_idx) {
755 CompUnitSP cu_sp;
756 if (dwarf_cu) {
757 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
758 if (comp_unit) {
759 // We already parsed this compile unit, had out a shared pointer to it
760 cu_sp = comp_unit->shared_from_this();
761 } else {
762 if (dwarf_cu->GetSymbolFileDWARF() != this) {
763 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
764 cu_idx);
765 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
766 // Let the debug map create the compile unit
767 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
768 dwarf_cu->SetUserData(cu_sp.get());
769 } else {
770 ModuleSP module_sp(m_obj_file->GetModule());
771 if (module_sp) {
772 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
773 if (cu_die) {
774 FileSpec cu_file_spec{cu_die.GetName(), false};
775 if (cu_file_spec) {
776 // If we have a full path to the compile unit, we don't need to
777 // resolve
778 // the file. This can be expensive e.g. when the source files are
779 // NFS mounted.
780 if (cu_file_spec.IsRelative()) {
781 const char *cu_comp_dir{
782 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
783 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
784 }
785
786 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000787 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 remapped_file))
789 cu_file_spec.SetFile(remapped_file, false);
790 }
791
792 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(
793 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
794
795 bool is_optimized = dwarf_cu->GetIsOptimized();
796 cu_sp.reset(new CompileUnit(
797 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
798 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
799 if (cu_sp) {
800 // If we just created a compile unit with an invalid file spec,
801 // try and get the
802 // first entry in the supports files from the line table as that
803 // should be the
804 // compile unit.
805 if (!cu_file_spec) {
806 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
807 if (cu_file_spec) {
808 (FileSpec &)(*cu_sp) = cu_file_spec;
809 // Also fix the invalid file spec which was copied from the
810 // compile unit.
811 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
812 }
813 }
814
815 dwarf_cu->SetUserData(cu_sp.get());
816
817 // Figure out the compile unit index if we weren't given one
818 if (cu_idx == UINT32_MAX)
819 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
820
821 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
822 cu_idx, cu_sp);
823 }
824 }
825 }
826 }
827 }
828 }
829 return cu_sp;
830}
831
832uint32_t SymbolFileDWARF::GetNumCompileUnits() {
833 DWARFDebugInfo *info = DebugInfo();
834 if (info)
835 return info->GetNumCompileUnits();
836 return 0;
837}
838
839CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
840 CompUnitSP cu_sp;
841 DWARFDebugInfo *info = DebugInfo();
842 if (info) {
843 DWARFCompileUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
844 if (dwarf_cu)
845 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
846 }
847 return cu_sp;
848}
849
850Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
851 const DWARFDIE &die) {
852 if (die.IsValid()) {
853 TypeSystem *type_system =
854 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
855
856 if (type_system) {
857 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
858 if (dwarf_ast)
859 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
860 }
861 }
862 return nullptr;
863}
864
865bool SymbolFileDWARF::FixupAddress(Address &addr) {
866 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
867 if (debug_map_symfile) {
868 return debug_map_symfile->LinkOSOAddress(addr);
869 }
870 // This is a normal DWARF file, no address fixups need to happen
871 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000872}
Greg Clayton1f746072012-08-29 21:13:06 +0000873lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
875 assert(sc.comp_unit);
876 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
877 if (dwarf_cu)
878 return dwarf_cu->GetLanguageType();
879 else
880 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
884 assert(sc.comp_unit);
885 size_t functions_added = 0;
886 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
887 if (dwarf_cu) {
888 DWARFDIECollection function_dies;
889 const size_t num_functions =
890 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
891 size_t func_idx;
892 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
893 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
894 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
895 if (ParseCompileUnitFunction(sc, die))
896 ++functions_added;
897 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 // FixupTypes();
900 }
901 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902}
903
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
905 const SymbolContext &sc, FileSpecList &support_files) {
906 assert(sc.comp_unit);
907 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
908 if (dwarf_cu) {
909 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 if (cu_die) {
912 const char *cu_comp_dir = resolveCompDir(
913 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
914 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
915 DW_AT_stmt_list, DW_INVALID_OFFSET);
916 if (stmt_list != DW_INVALID_OFFSET) {
917 // All file indexes in DWARF are one based and a file of index zero is
918 // supposed to be the compile unit itself.
919 support_files.Append(*sc.comp_unit);
920 return DWARFDebugLine::ParseSupportFiles(
921 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
922 stmt_list, support_files);
923 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 }
926 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927}
928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
930 const lldb_private::SymbolContext &sc) {
931 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
932 if (dwarf_cu)
933 return dwarf_cu->GetIsOptimized();
934 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000935}
936
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937bool SymbolFileDWARF::ParseImportedModules(
938 const lldb_private::SymbolContext &sc,
939 std::vector<lldb_private::ConstString> &imported_modules) {
940 assert(sc.comp_unit);
941 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
942 if (dwarf_cu) {
943 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
944 sc.comp_unit->GetLanguage())) {
945 UpdateExternalModuleListIfNeeded();
946
947 if (sc.comp_unit) {
948 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
949
950 if (die) {
951 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
952 child_die = child_die.GetSibling()) {
953 if (child_die.Tag() == DW_TAG_imported_declaration) {
954 if (DWARFDIE module_die =
955 child_die.GetReferencedDIE(DW_AT_import)) {
956 if (module_die.Tag() == DW_TAG_module) {
957 if (const char *name = module_die.GetAttributeValueAsString(
958 DW_AT_name, nullptr)) {
959 ConstString const_name(name);
960 imported_modules.push_back(const_name);
961 }
Sean Callananb0300a42016-01-14 21:46:09 +0000962 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 }
Sean Callananb0300a42016-01-14 21:46:09 +0000964 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000966 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 } else {
968 for (const auto &pair : m_external_type_modules) {
969 imported_modules.push_back(pair.first);
970 }
971 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000972 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 }
974 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000975}
976
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977struct ParseDWARFLineTableCallbackInfo {
978 LineTable *line_table;
979 std::unique_ptr<LineSequence> sequence_ap;
980 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981};
982
983//----------------------------------------------------------------------
984// ParseStatementTableCallback
985//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986static void ParseDWARFLineTableCallback(dw_offset_t offset,
987 const DWARFDebugLine::State &state,
988 void *userData) {
989 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
990 // Just started parsing the line table
991 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
992 // Done parsing line table, nothing to do for the cleanup
993 } else {
994 ParseDWARFLineTableCallbackInfo *info =
995 (ParseDWARFLineTableCallbackInfo *)userData;
996 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 // If this is our first time here, we need to create a
999 // sequence container.
1000 if (!info->sequence_ap.get()) {
1001 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1002 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 line_table->AppendLineEntryToSequence(
1005 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1006 state.column, state.file, state.is_stmt, state.basic_block,
1007 state.prologue_end, state.epilogue_begin, state.end_sequence);
1008 if (state.end_sequence) {
1009 // First, put the current sequence into the line table.
1010 line_table->InsertSequence(info->sequence_ap.get());
1011 // Then, empty it to prepare for the next sequence.
1012 info->sequence_ap->Clear();
1013 }
1014 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015}
1016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1018 assert(sc.comp_unit);
1019 if (sc.comp_unit->GetLineTable() != NULL)
1020 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1023 if (dwarf_cu) {
1024 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1025 if (dwarf_cu_die) {
1026 const dw_offset_t cu_line_offset =
1027 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1028 DW_INVALID_OFFSET);
1029 if (cu_line_offset != DW_INVALID_OFFSET) {
1030 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1031 if (line_table_ap.get()) {
1032 ParseDWARFLineTableCallbackInfo info;
1033 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 /*
1036 * MIPS:
1037 * The SymbolContext may not have a valid target, thus we may not be
1038 * able
1039 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1040 * #0
1041 * for MIPS. Use ArchSpec to clear the bit #0.
1042 */
1043 ArchSpec arch;
1044 GetObjectFile()->GetArchitecture(arch);
1045 switch (arch.GetMachine()) {
1046 case llvm::Triple::mips:
1047 case llvm::Triple::mipsel:
1048 case llvm::Triple::mips64:
1049 case llvm::Triple::mips64el:
1050 info.addr_mask = ~((lldb::addr_t)1);
1051 break;
1052 default:
1053 info.addr_mask = ~((lldb::addr_t)0);
1054 break;
1055 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001056
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 lldb::offset_t offset = cu_line_offset;
1058 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1059 ParseDWARFLineTableCallback,
1060 &info);
1061 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1062 if (debug_map_symfile) {
1063 // We have an object file that has a line table with addresses
1064 // that are not linked. We need to link the line table and convert
1065 // the addresses that are relative to the .o file into addresses
1066 // for the main executable.
1067 sc.comp_unit->SetLineTable(
1068 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1069 } else {
1070 sc.comp_unit->SetLineTable(line_table_ap.release());
1071 return true;
1072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 }
1077 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078}
1079
Siva Chandrad8335e92015-12-16 00:22:08 +00001080lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1082 auto iter = m_debug_macros_map.find(*offset);
1083 if (iter != m_debug_macros_map.end())
1084 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001085
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1087 if (debug_macro_data.GetByteSize() == 0)
1088 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001089
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1091 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 const DWARFDebugMacroHeader &header =
1094 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1095 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1096 header.OffsetIs64Bit(), offset, this,
1097 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001098
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001100}
1101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1103 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001104
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1106 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001107 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108
1109 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1110 if (!dwarf_cu_die)
1111 return false;
1112
1113 lldb::offset_t sect_offset =
1114 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1115 if (sect_offset == DW_INVALID_OFFSET)
1116 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1117 DW_INVALID_OFFSET);
1118 if (sect_offset == DW_INVALID_OFFSET)
1119 return false;
1120
1121 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1122
1123 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001124}
1125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1127 Block *parent_block,
1128 const DWARFDIE &orig_die,
1129 addr_t subprogram_low_pc,
1130 uint32_t depth) {
1131 size_t blocks_added = 0;
1132 DWARFDIE die = orig_die;
1133 while (die) {
1134 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 switch (tag) {
1137 case DW_TAG_inlined_subroutine:
1138 case DW_TAG_subprogram:
1139 case DW_TAG_lexical_block: {
1140 Block *block = NULL;
1141 if (tag == DW_TAG_subprogram) {
1142 // Skip any DW_TAG_subprogram DIEs that are inside
1143 // of a normal or inlined functions. These will be
1144 // parsed on their own as separate entities.
1145
1146 if (depth > 0)
1147 break;
1148
1149 block = parent_block;
1150 } else {
1151 BlockSP block_sp(new Block(die.GetID()));
1152 parent_block->AddChild(block_sp);
1153 block = block_sp.get();
1154 }
1155 DWARFRangeList ranges;
1156 const char *name = NULL;
1157 const char *mangled_name = NULL;
1158
1159 int decl_file = 0;
1160 int decl_line = 0;
1161 int decl_column = 0;
1162 int call_file = 0;
1163 int call_line = 0;
1164 int call_column = 0;
1165 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1166 decl_line, decl_column, call_file, call_line,
1167 call_column, nullptr)) {
1168 if (tag == DW_TAG_subprogram) {
1169 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1170 subprogram_low_pc = ranges.GetMinRangeBase(0);
1171 } else if (tag == DW_TAG_inlined_subroutine) {
1172 // We get called here for inlined subroutines in two ways.
1173 // The first time is when we are making the Function object
1174 // for this inlined concrete instance. Since we're creating a top
1175 // level block at
1176 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1177 // need to
1178 // adjust the containing address.
1179 // The second time is when we are parsing the blocks inside the
1180 // function that contains
1181 // the inlined concrete instance. Since these will be blocks inside
1182 // the containing "real"
1183 // function the offset will be for that function.
1184 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1185 subprogram_low_pc = ranges.GetMinRangeBase(0);
1186 }
1187 }
1188
1189 const size_t num_ranges = ranges.GetSize();
1190 for (size_t i = 0; i < num_ranges; ++i) {
1191 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1192 const addr_t range_base = range.GetRangeBase();
1193 if (range_base >= subprogram_low_pc)
1194 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1195 range.GetByteSize()));
1196 else {
1197 GetObjectFile()->GetModule()->ReportError(
1198 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1199 ") which has a base that is less than the function's low PC "
1200 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1201 "start of this error message",
1202 block->GetID(), range_base, range.GetRangeEnd(),
1203 subprogram_low_pc);
1204 }
1205 }
1206 block->FinalizeRanges();
1207
1208 if (tag != DW_TAG_subprogram &&
1209 (name != NULL || mangled_name != NULL)) {
1210 std::unique_ptr<Declaration> decl_ap;
1211 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1212 decl_ap.reset(new Declaration(
1213 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1214 decl_line, decl_column));
1215
1216 std::unique_ptr<Declaration> call_ap;
1217 if (call_file != 0 || call_line != 0 || call_column != 0)
1218 call_ap.reset(new Declaration(
1219 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1220 call_line, call_column));
1221
1222 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1223 call_ap.get());
1224 }
1225
1226 ++blocks_added;
1227
1228 if (die.HasChildren()) {
1229 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1230 subprogram_low_pc, depth + 1);
1231 }
1232 }
1233 } break;
1234 default:
1235 break;
1236 }
1237
1238 // Only parse siblings of the block if we are not at depth zero. A depth
1239 // of zero indicates we are currently parsing the top level
1240 // DW_TAG_subprogram DIE
1241
1242 if (depth == 0)
1243 die.Clear();
1244 else
1245 die = die.GetSibling();
1246 }
1247 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001248}
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1251 if (parent_die) {
1252 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1253 die = die.GetSibling()) {
1254 dw_tag_t tag = die.Tag();
1255 bool check_virtuality = false;
1256 switch (tag) {
1257 case DW_TAG_inheritance:
1258 case DW_TAG_subprogram:
1259 check_virtuality = true;
1260 break;
1261 default:
1262 break;
1263 }
1264 if (check_virtuality) {
1265 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1266 return true;
1267 }
1268 }
1269 }
1270 return false;
1271}
1272
1273void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1274 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1275 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1276 std::vector<DWARFDIE> decl_ctx_die_list =
1277 ast_parser->GetDIEForDeclContext(decl_ctx);
1278
1279 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1280 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1281 decl = decl.GetSibling())
1282 ast_parser->GetDeclForUIDFromDWARF(decl);
1283}
1284
1285SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1286 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1287 // we must make sure we use the correct DWARF file when resolving things.
1288 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1289 // SymbolFileDWARF classes, one for each .o file. We can often end up
1290 // with references to other DWARF objects and we must be ready to receive
1291 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1292 // instance.
1293 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1294 if (debug_map)
1295 return debug_map->GetSymbolFileByOSOIndex(
1296 debug_map->GetOSOIndexFromUserID(uid));
1297 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001298}
1299
1300DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1302 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1303 // we must make sure we use the correct DWARF file when resolving things.
1304 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1305 // SymbolFileDWARF classes, one for each .o file. We can often end up
1306 // with references to other DWARF objects and we must be ready to receive
1307 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1308 // instance.
1309 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1310 if (dwarf)
1311 return dwarf->GetDIE(DIERef(uid, dwarf));
1312 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001313}
1314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1316 // Anytime we have a lldb::user_id_t, we must get the DIE by
1317 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1318 // the SymbolFileDWARF::GetDIEFromUID() for details.
1319 DWARFDIE die = GetDIEFromUID(type_uid);
1320 if (die)
1321 return die.GetDecl();
1322 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001323}
1324
Greg Clayton99558cc42015-08-24 23:46:31 +00001325CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1327 // Anytime we have a lldb::user_id_t, we must get the DIE by
1328 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1329 // the SymbolFileDWARF::GetDIEFromUID() for details.
1330 DWARFDIE die = GetDIEFromUID(type_uid);
1331 if (die)
1332 return die.GetDeclContext();
1333 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001334}
1335
Greg Clayton99558cc42015-08-24 23:46:31 +00001336CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1338 // Anytime we have a lldb::user_id_t, we must get the DIE by
1339 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1340 // the SymbolFileDWARF::GetDIEFromUID() for details.
1341 DWARFDIE die = GetDIEFromUID(type_uid);
1342 if (die)
1343 return die.GetContainingDeclContext();
1344 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345}
1346
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1348 // Anytime we have a lldb::user_id_t, we must get the DIE by
1349 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1350 // the SymbolFileDWARF::GetDIEFromUID() for details.
1351 DWARFDIE type_die = GetDIEFromUID(type_uid);
1352 if (type_die)
1353 return type_die.ResolveType();
1354 else
1355 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356}
1357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1359 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001360}
1361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1363 bool assert_not_being_parsed) {
1364 if (die) {
1365 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1366 if (log)
1367 GetObjectFile()->GetModule()->LogMessage(
1368 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1369 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001370
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 // We might be coming in in the middle of a type tree (a class
1372 // within a class, an enum within a class), so parse any needed
1373 // parent DIEs before we get to this one...
1374 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1375 if (decl_ctx_die) {
1376 if (log) {
1377 switch (decl_ctx_die.Tag()) {
1378 case DW_TAG_structure_type:
1379 case DW_TAG_union_type:
1380 case DW_TAG_class_type: {
1381 // Get the type, which could be a forward declaration
1382 if (log)
1383 GetObjectFile()->GetModule()->LogMessage(
1384 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1385 "resolve parent forward type for 0x%8.8x",
1386 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1387 decl_ctx_die.GetOffset());
1388 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 default:
1391 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001392 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 return ResolveType(die);
1396 }
1397 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001398}
1399
Greg Clayton6beaaa62011-01-17 03:46:26 +00001400// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1401// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001402// can resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403bool SymbolFileDWARF::HasForwardDeclForClangType(
1404 const CompilerType &compiler_type) {
1405 CompilerType compiler_type_no_qualifiers =
1406 ClangUtil::RemoveFastQualifiers(compiler_type);
1407 if (GetForwardDeclClangTypeToDie().count(
1408 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1409 return true;
1410 }
1411 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 ClangASTContext *clang_type_system =
1414 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1415 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001416 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 DWARFASTParserClang *ast_parser =
1418 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1419 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001420}
1421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1423 std::lock_guard<std::recursive_mutex> guard(
1424 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001425
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 ClangASTContext *clang_type_system =
1427 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1428 if (clang_type_system) {
1429 DWARFASTParserClang *ast_parser =
1430 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1431 if (ast_parser &&
1432 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1433 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1434 }
Jim Inghamc3549282012-01-11 02:21:12 +00001435
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 // We have a struct/union/class/enum that needs to be fully resolved.
1437 CompilerType compiler_type_no_qualifiers =
1438 ClangUtil::RemoveFastQualifiers(compiler_type);
1439 auto die_it = GetForwardDeclClangTypeToDie().find(
1440 compiler_type_no_qualifiers.GetOpaqueQualType());
1441 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1442 // We have already resolved this type...
1443 return true;
1444 }
1445
1446 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1447 if (dwarf_die) {
1448 // Once we start resolving this type, remove it from the forward declaration
1449 // map in case anyone child members or other types require this type to get
1450 // resolved.
1451 // The type will get resolved when all of the calls to
1452 // SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1453 // are done.
1454 GetForwardDeclClangTypeToDie().erase(die_it);
1455
1456 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1457
1458 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1459 DWARF_LOG_TYPE_COMPLETION));
1460 if (log)
1461 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1462 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1463 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1464 type->GetName().AsCString());
1465 assert(compiler_type);
1466 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1467 if (dwarf_ast)
1468 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1469 }
1470 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001471}
1472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1474 bool assert_not_being_parsed,
1475 bool resolve_function_context) {
1476 if (die) {
1477 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1478
1479 if (assert_not_being_parsed) {
1480 if (type != DIE_IS_BEING_PARSED)
1481 return type;
1482
1483 GetObjectFile()->GetModule()->ReportError(
1484 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1485 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1486
1487 } else
1488 return type;
1489 }
1490 return nullptr;
1491}
1492
1493CompileUnit *
1494SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit *dwarf_cu,
1495 uint32_t cu_idx) {
1496 // Check if the symbol vendor already knows about this compile unit?
1497 if (dwarf_cu->GetUserData() == NULL) {
1498 // The symbol vendor doesn't know about this compile unit, we
1499 // need to parse and add it to the symbol vendor object.
1500 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1501 }
1502 return (CompileUnit *)dwarf_cu->GetUserData();
1503}
1504
1505size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1506 DIEArray &method_die_offsets) {
1507 method_die_offsets.clear();
1508 if (m_using_apple_tables) {
1509 if (m_apple_objc_ap.get())
1510 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1511 } else {
1512 if (!m_indexed)
1513 Index();
1514
1515 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1516 }
1517 return method_die_offsets.size();
1518}
1519
1520bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1521 sc.Clear(false);
1522
1523 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1526
1527 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1528 if (sc.function == NULL)
1529 sc.function = ParseCompileUnitFunction(sc, die);
1530
1531 if (sc.function) {
1532 sc.module_sp = sc.function->CalculateSymbolContextModule();
1533 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 }
1536
1537 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538}
1539
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1541 UpdateExternalModuleListIfNeeded();
1542 const auto &pos = m_external_type_modules.find(name);
1543 if (pos != m_external_type_modules.end())
1544 return pos->second;
1545 else
1546 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001547}
1548
Greg Clayton2f869fe2016-03-30 20:14:35 +00001549DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1551 DWARFDebugInfo *debug_info = DebugInfo();
1552 if (debug_info)
1553 return debug_info->GetDIE(die_ref);
1554 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001555 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001556}
1557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558std::unique_ptr<SymbolFileDWARFDwo>
1559SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1560 DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1561 // If we are using a dSYM file, we never want the standard DWO files since
1562 // the -gmodule support uses the same DWO machanism to specify full debug
1563 // info files for modules.
1564 if (GetDebugMapSymfile())
1565 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001566
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 const char *dwo_name = cu_die.GetAttributeValueAsString(
1568 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1569 if (!dwo_name)
1570 return nullptr;
1571
Tamas Berghammer963ce482017-08-25 13:56:14 +00001572 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1573 if (dwp_symfile) {
1574 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1575 DW_AT_GNU_dwo_id, 0);
1576 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1577 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1578 if (dwo_symfile)
1579 return dwo_symfile;
1580 }
1581
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 FileSpec dwo_file(dwo_name, true);
1583 if (dwo_file.IsRelative()) {
1584 const char *comp_dir = cu_die.GetAttributeValueAsString(
1585 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1586 if (!comp_dir)
1587 return nullptr;
1588
1589 dwo_file.SetFile(comp_dir, true);
1590 dwo_file.AppendPathComponent(dwo_name);
1591 }
1592
1593 if (!dwo_file.Exists())
1594 return nullptr;
1595
1596 const lldb::offset_t file_offset = 0;
1597 DataBufferSP dwo_file_data_sp;
1598 lldb::offset_t dwo_file_data_offset = 0;
1599 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1600 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1601 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1602 if (dwo_obj_file == nullptr)
1603 return nullptr;
1604
1605 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001606}
1607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1609 if (m_fetched_external_modules)
1610 return;
1611 m_fetched_external_modules = true;
1612
1613 DWARFDebugInfo *debug_info = DebugInfo();
1614
1615 const uint32_t num_compile_units = GetNumCompileUnits();
1616 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1617 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1618
1619 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1620 if (die && die.HasChildren() == false) {
1621 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1622
1623 if (name) {
1624 ConstString const_name(name);
1625 if (m_external_type_modules.find(const_name) ==
1626 m_external_type_modules.end()) {
1627 ModuleSP module_sp;
1628 const char *dwo_path =
1629 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1630 if (dwo_path) {
1631 ModuleSpec dwo_module_spec;
1632 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1633 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1634 const char *comp_dir =
1635 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1636 if (comp_dir) {
1637 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1638 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1639 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 dwo_module_spec.GetArchitecture() =
1642 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001643
1644 // When LLDB loads "external" modules it looks at the
1645 // presence of DW_AT_GNU_dwo_name.
1646 // However, when the already created module
1647 // (corresponding to .dwo itself) is being processed,
1648 // it will see the presence of DW_AT_GNU_dwo_name
1649 // (which contains the name of dwo file) and
1650 // will try to call ModuleList::GetSharedModule again.
1651 // In some cases (i.e. for empty files) Clang 4.0
1652 // generates a *.dwo file which has DW_AT_GNU_dwo_name,
1653 // but no DW_AT_comp_dir. In this case the method
1654 // ModuleList::GetSharedModule will fail and
1655 // the warning will be printed. However, as one can notice
1656 // in this case we don't actually need to try to load the already
1657 // loaded module (corresponding to .dwo) so we simply skip it.
1658 if (m_obj_file->GetFileSpec()
1659 .GetFileNameExtension()
1660 .GetStringRef() == "dwo" &&
1661 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1662 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1663 continue;
1664 }
1665
Zachary Turner97206d52017-05-12 04:51:55 +00001666 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 dwo_module_spec, module_sp, NULL, NULL, NULL);
1668 if (!module_sp) {
1669 GetObjectFile()->GetModule()->ReportWarning(
1670 "0x%8.8x: unable to locate module needed for external types: "
1671 "%s\nerror: %s\nDebugging will be degraded due to missing "
1672 "types. Rebuilding your project will regenerate the needed "
1673 "module files.",
1674 die.GetOffset(),
1675 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1676 error.AsCString("unknown error"));
1677 }
1678 }
1679 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001680 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 }
1684}
1685
1686SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1687 if (!m_global_aranges_ap) {
1688 m_global_aranges_ap.reset(new GlobalVariableMap());
1689
1690 ModuleSP module_sp = GetObjectFile()->GetModule();
1691 if (module_sp) {
1692 const size_t num_cus = module_sp->GetNumCompileUnits();
1693 for (size_t i = 0; i < num_cus; ++i) {
1694 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1695 if (cu_sp) {
1696 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1697 if (globals_sp) {
1698 const size_t num_globals = globals_sp->GetSize();
1699 for (size_t g = 0; g < num_globals; ++g) {
1700 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1701 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1702 const DWARFExpression &location = var_sp->LocationExpression();
1703 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001704 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001705 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1706 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 if (location_result.GetValueType() ==
1708 Value::eValueTypeFileAddress) {
1709 lldb::addr_t file_addr =
1710 location_result.GetScalar().ULongLong();
1711 lldb::addr_t byte_size = 1;
1712 if (var_sp->GetType())
1713 byte_size = var_sp->GetType()->GetByteSize();
1714 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1715 file_addr, byte_size, var_sp.get()));
1716 }
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723 }
1724 m_global_aranges_ap->Sort();
1725 }
1726 return *m_global_aranges_ap;
1727}
1728
1729uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1730 uint32_t resolve_scope,
1731 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001732 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1733 Timer scoped_timer(func_cat,
1734 "SymbolFileDWARF::"
1735 "ResolveSymbolContext (so_addr = { "
1736 "section = %p, offset = 0x%" PRIx64
1737 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 static_cast<void *>(so_addr.GetSection().get()),
1739 so_addr.GetOffset(), resolve_scope);
1740 uint32_t resolved = 0;
1741 if (resolve_scope &
1742 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1743 eSymbolContextLineEntry | eSymbolContextVariable)) {
1744 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1745
1746 DWARFDebugInfo *debug_info = DebugInfo();
1747 if (debug_info) {
1748 const dw_offset_t cu_offset =
1749 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1750 if (cu_offset == DW_INVALID_OFFSET) {
1751 // Global variables are not in the compile unit address ranges. The only
1752 // way to
1753 // currently find global variables is to iterate over the
1754 // .debug_pubnames or the
1755 // __apple_names table and find all items in there that point to
1756 // DW_TAG_variable
1757 // DIEs and then find the address that matches.
1758 if (resolve_scope & eSymbolContextVariable) {
1759 GlobalVariableMap &map = GetGlobalAranges();
1760 const GlobalVariableMap::Entry *entry =
1761 map.FindEntryThatContains(file_vm_addr);
1762 if (entry && entry->data) {
1763 Variable *variable = entry->data;
1764 SymbolContextScope *scc = variable->GetSymbolContextScope();
1765 if (scc) {
1766 scc->CalculateSymbolContext(&sc);
1767 sc.variable = variable;
1768 }
1769 return sc.GetResolvedMask();
1770 }
1771 }
1772 } else {
1773 uint32_t cu_idx = DW_INVALID_INDEX;
1774 DWARFCompileUnit *dwarf_cu =
1775 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1776 if (dwarf_cu) {
1777 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1778 if (sc.comp_unit) {
1779 resolved |= eSymbolContextCompUnit;
1780
1781 bool force_check_line_table = false;
1782 if (resolve_scope &
1783 (eSymbolContextFunction | eSymbolContextBlock)) {
1784 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1785 DWARFDIE block_die;
1786 if (function_die) {
1787 sc.function =
1788 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1789 if (sc.function == NULL)
1790 sc.function = ParseCompileUnitFunction(sc, function_die);
1791
1792 if (sc.function && (resolve_scope & eSymbolContextBlock))
1793 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1794 } else {
1795 // We might have had a compile unit that had discontiguous
1796 // address ranges where the gaps are symbols that don't have
1797 // any debug info. Discontiguous compile unit address ranges
1798 // should only happen when there aren't other functions from
1799 // other compile units in these gaps. This helps keep the size
1800 // of the aranges down.
1801 force_check_line_table = true;
1802 }
1803
1804 if (sc.function != NULL) {
1805 resolved |= eSymbolContextFunction;
1806
1807 if (resolve_scope & eSymbolContextBlock) {
1808 Block &block = sc.function->GetBlock(true);
1809
1810 if (block_die)
1811 sc.block = block.FindBlockByID(block_die.GetID());
1812 else
1813 sc.block = block.FindBlockByID(function_die.GetID());
1814 if (sc.block)
1815 resolved |= eSymbolContextBlock;
1816 }
1817 }
1818 }
1819
1820 if ((resolve_scope & eSymbolContextLineEntry) ||
1821 force_check_line_table) {
1822 LineTable *line_table = sc.comp_unit->GetLineTable();
1823 if (line_table != NULL) {
1824 // And address that makes it into this function should be in
1825 // terms
1826 // of this debug file if there is no debug map, or it will be an
1827 // address in the .o file which needs to be fixed up to be in
1828 // terms
1829 // of the debug map executable. Either way, calling
1830 // FixupAddress()
1831 // will work for us.
1832 Address exe_so_addr(so_addr);
1833 if (FixupAddress(exe_so_addr)) {
1834 if (line_table->FindLineEntryByAddress(exe_so_addr,
1835 sc.line_entry)) {
1836 resolved |= eSymbolContextLineEntry;
1837 }
1838 }
1839 }
1840 }
1841
1842 if (force_check_line_table &&
1843 !(resolved & eSymbolContextLineEntry)) {
1844 // We might have had a compile unit that had discontiguous
1845 // address ranges where the gaps are symbols that don't have
1846 // any debug info. Discontiguous compile unit address ranges
1847 // should only happen when there aren't other functions from
1848 // other compile units in these gaps. This helps keep the size
1849 // of the aranges down.
1850 sc.comp_unit = NULL;
1851 resolved &= ~eSymbolContextCompUnit;
1852 }
1853 } else {
1854 GetObjectFile()->GetModule()->ReportWarning(
1855 "0x%8.8x: compile unit %u failed to create a valid "
1856 "lldb_private::CompileUnit class.",
1857 cu_offset, cu_idx);
1858 }
1859 }
1860 }
1861 }
1862 }
1863 return resolved;
1864}
1865
1866uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1867 uint32_t line,
1868 bool check_inlines,
1869 uint32_t resolve_scope,
1870 SymbolContextList &sc_list) {
1871 const uint32_t prev_size = sc_list.GetSize();
1872 if (resolve_scope & eSymbolContextCompUnit) {
1873 DWARFDebugInfo *debug_info = DebugInfo();
1874 if (debug_info) {
1875 uint32_t cu_idx;
1876 DWARFCompileUnit *dwarf_cu = NULL;
1877
1878 for (cu_idx = 0;
1879 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1880 ++cu_idx) {
1881 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1882 const bool full_match = (bool)file_spec.GetDirectory();
1883 bool file_spec_matches_cu_file_spec =
1884 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1885 if (check_inlines || file_spec_matches_cu_file_spec) {
1886 SymbolContext sc(m_obj_file->GetModule());
1887 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1888 if (sc.comp_unit) {
1889 uint32_t file_idx = UINT32_MAX;
1890
1891 // If we are looking for inline functions only and we don't
1892 // find it in the support files, we are done.
1893 if (check_inlines) {
1894 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1895 1, file_spec, true);
1896 if (file_idx == UINT32_MAX)
1897 continue;
1898 }
1899
1900 if (line != 0) {
1901 LineTable *line_table = sc.comp_unit->GetLineTable();
1902
1903 if (line_table != NULL && line != 0) {
1904 // We will have already looked up the file index if
1905 // we are searching for inline entries.
1906 if (!check_inlines)
1907 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1908 1, file_spec, true);
1909
1910 if (file_idx != UINT32_MAX) {
1911 uint32_t found_line;
1912 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1913 0, file_idx, line, false, &sc.line_entry);
1914 found_line = sc.line_entry.line;
1915
1916 while (line_idx != UINT32_MAX) {
1917 sc.function = NULL;
1918 sc.block = NULL;
1919 if (resolve_scope &
1920 (eSymbolContextFunction | eSymbolContextBlock)) {
1921 const lldb::addr_t file_vm_addr =
1922 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1923 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1924 DWARFDIE function_die =
1925 dwarf_cu->LookupAddress(file_vm_addr);
1926 DWARFDIE block_die;
1927 if (function_die) {
1928 sc.function =
1929 sc.comp_unit
1930 ->FindFunctionByUID(function_die.GetID())
1931 .get();
1932 if (sc.function == NULL)
1933 sc.function =
1934 ParseCompileUnitFunction(sc, function_die);
1935
1936 if (sc.function &&
1937 (resolve_scope & eSymbolContextBlock))
1938 block_die =
1939 function_die.LookupDeepestBlock(file_vm_addr);
1940 }
1941
1942 if (sc.function != NULL) {
1943 Block &block = sc.function->GetBlock(true);
1944
1945 if (block_die)
1946 sc.block = block.FindBlockByID(block_die.GetID());
1947 else if (function_die)
1948 sc.block =
1949 block.FindBlockByID(function_die.GetID());
1950 }
1951 }
1952 }
1953
1954 sc_list.Append(sc);
1955 line_idx = line_table->FindLineEntryIndexByFileIndex(
1956 line_idx + 1, file_idx, found_line, true,
1957 &sc.line_entry);
1958 }
1959 }
1960 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1961 // only append the context if we aren't looking for inline call
1962 // sites
1963 // by file and line and if the file spec matches that of the
1964 // compile unit
1965 sc_list.Append(sc);
1966 }
1967 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1968 // only append the context if we aren't looking for inline call
1969 // sites
1970 // by file and line and if the file spec matches that of the
1971 // compile unit
1972 sc_list.Append(sc);
1973 }
1974
1975 if (!check_inlines)
1976 break;
1977 }
1978 }
1979 }
1980 }
1981 }
1982 return sc_list.GetSize() - prev_size;
1983}
1984
Jim Ingham7fca8c02017-04-28 00:51:06 +00001985void SymbolFileDWARF::PreloadSymbols() {
1986 std::lock_guard<std::recursive_mutex> guard(
1987 GetObjectFile()->GetModule()->GetMutex());
1988 Index();
1989}
1990
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991void SymbolFileDWARF::Index() {
1992 if (m_indexed)
1993 return;
1994 m_indexed = true;
Pavel Labathf9d16472017-05-15 13:02:37 +00001995 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001996 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +00001997 func_cat, "SymbolFileDWARF::Index (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
1999
2000 DWARFDebugInfo *debug_info = DebugInfo();
2001 if (debug_info) {
2002 const uint32_t num_compile_units = GetNumCompileUnits();
2003 if (num_compile_units == 0)
2004 return;
2005
2006 std::vector<NameToDIE> function_basename_index(num_compile_units);
2007 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2008 std::vector<NameToDIE> function_method_index(num_compile_units);
2009 std::vector<NameToDIE> function_selector_index(num_compile_units);
2010 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2011 std::vector<NameToDIE> global_index(num_compile_units);
2012 std::vector<NameToDIE> type_index(num_compile_units);
2013 std::vector<NameToDIE> namespace_index(num_compile_units);
2014
Pavel Labath5c913e92017-05-05 11:16:59 +00002015 // std::vector<bool> might be implemented using bit test-and-set, so use
2016 // uint8_t instead.
2017 std::vector<uint8_t> clear_cu_dies(num_compile_units, false);
Zachary Turner3bc714b2017-03-02 00:05:25 +00002018 auto parser_fn = [debug_info, &function_basename_index,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002019 &function_fullname_index, &function_method_index,
2020 &function_selector_index, &objc_class_selectors_index,
2021 &global_index, &type_index,
Pavel Labath92f66772017-06-07 16:28:08 +00002022 &namespace_index](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2024 if (dwarf_cu) {
2025 dwarf_cu->Index(
2026 function_basename_index[cu_idx], function_fullname_index[cu_idx],
2027 function_method_index[cu_idx], function_selector_index[cu_idx],
2028 objc_class_selectors_index[cu_idx], global_index[cu_idx],
2029 type_index[cu_idx], namespace_index[cu_idx]);
2030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 };
2032
Pavel Labath92f66772017-06-07 16:28:08 +00002033 auto extract_fn = [debug_info, &clear_cu_dies](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2035 if (dwarf_cu) {
2036 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the
2037 // DIEs for a compile unit have already been parsed.
Pavel Labath5c913e92017-05-05 11:16:59 +00002038 if (dwarf_cu->ExtractDIEsIfNeeded(false) > 1)
2039 clear_cu_dies[cu_idx] = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 };
2042
2043 // Create a task runner that extracts dies for each DWARF compile unit in a
2044 // separate thread
Kate Stoneb9c1b512016-09-06 20:57:50 +00002045 //----------------------------------------------------------------------
2046 // First figure out which compile units didn't have their DIEs already
2047 // parsed and remember this. If no DIEs were parsed prior to this index
2048 // function call, we are going to want to clear the CU dies after we
2049 // are done indexing to make sure we don't pull in all DWARF dies, but
2050 // we need to wait until all compile units have been indexed in case
2051 // a DIE in one compile unit refers to another and the indexes accesses
2052 // those DIEs.
2053 //----------------------------------------------------------------------
Pavel Labath5c913e92017-05-05 11:16:59 +00002054 TaskMapOverInt(0, num_compile_units, extract_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055
2056 // Now create a task runner that can index each DWARF compile unit in a
2057 // separate
2058 // thread so we can index quickly.
2059
Pavel Labath5c913e92017-05-05 11:16:59 +00002060 TaskMapOverInt(0, num_compile_units, parser_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002061
Pavel Labath5c913e92017-05-05 11:16:59 +00002062 auto finalize_fn = [](NameToDIE &index, std::vector<NameToDIE> &srcs) {
2063 for (auto &src : srcs)
2064 index.Append(src);
2065 index.Finalize();
2066 };
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067
Pavel Labath5c913e92017-05-05 11:16:59 +00002068 TaskPool::RunTasks(
2069 [&]() {
2070 finalize_fn(m_function_basename_index, function_basename_index);
2071 },
2072 [&]() {
2073 finalize_fn(m_function_fullname_index, function_fullname_index);
2074 },
2075 [&]() { finalize_fn(m_function_method_index, function_method_index); },
2076 [&]() {
2077 finalize_fn(m_function_selector_index, function_selector_index);
2078 },
2079 [&]() {
2080 finalize_fn(m_objc_class_selectors_index, objc_class_selectors_index);
2081 },
2082 [&]() { finalize_fn(m_global_index, global_index); },
2083 [&]() { finalize_fn(m_type_index, type_index); },
2084 [&]() { finalize_fn(m_namespace_index, namespace_index); });
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085
2086 //----------------------------------------------------------------------
2087 // Keep memory down by clearing DIEs for any compile units if indexing
2088 // caused us to load the compile unit's DIEs.
2089 //----------------------------------------------------------------------
2090 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2091 if (clear_cu_dies[cu_idx])
2092 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2093 }
2094
2095#if defined(ENABLE_DEBUG_PRINTF)
2096 StreamFile s(stdout, false);
2097 s.Printf("DWARF index for '%s':",
2098 GetObjectFile()->GetFileSpec().GetPath().c_str());
2099 s.Printf("\nFunction basenames:\n");
2100 m_function_basename_index.Dump(&s);
2101 s.Printf("\nFunction fullnames:\n");
2102 m_function_fullname_index.Dump(&s);
2103 s.Printf("\nFunction methods:\n");
2104 m_function_method_index.Dump(&s);
2105 s.Printf("\nFunction selectors:\n");
2106 m_function_selector_index.Dump(&s);
2107 s.Printf("\nObjective C class selectors:\n");
2108 m_objc_class_selectors_index.Dump(&s);
2109 s.Printf("\nGlobals and statics:\n");
2110 m_global_index.Dump(&s);
2111 s.Printf("\nTypes:\n");
2112 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002113 s.Printf("\nNamespaces:\n");
2114 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002115#endif
2116 }
2117}
2118
2119bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2120 const lldb_private::CompilerDeclContext *decl_ctx) {
2121 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
2122 // Invalid namespace decl which means we aren't matching only things
2123 // in this symbol file, so return true to indicate it matches this
2124 // symbol file.
2125 return true;
2126 }
2127
2128 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2129 TypeSystem *type_system = GetTypeSystemForLanguage(
2130 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2131 if (decl_ctx_type_system == type_system)
2132 return true; // The type systems match, return true
2133
2134 // The namespace AST was valid, and it does not match...
2135 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2136
2137 if (log)
2138 GetObjectFile()->GetModule()->LogMessage(
2139 log, "Valid namespace does not match symbol file");
2140
2141 return false;
2142}
2143
2144uint32_t SymbolFileDWARF::FindGlobalVariables(
2145 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2146 bool append, uint32_t max_matches, VariableList &variables) {
2147 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2148
2149 if (log)
2150 GetObjectFile()->GetModule()->LogMessage(
2151 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2152 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2153 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2154 max_matches);
2155
2156 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2157 return 0;
2158
2159 DWARFDebugInfo *info = DebugInfo();
2160 if (info == NULL)
2161 return 0;
2162
2163 // If we aren't appending the results to this list, then clear the list
2164 if (!append)
2165 variables.Clear();
2166
2167 // Remember how many variables are in the list before we search in case
2168 // we are appending the results to a variable list.
2169 const uint32_t original_size = variables.GetSize();
2170
2171 DIEArray die_offsets;
2172
2173 if (m_using_apple_tables) {
2174 if (m_apple_names_ap.get()) {
2175 const char *name_cstr = name.GetCString();
2176 llvm::StringRef basename;
2177 llvm::StringRef context;
2178
2179 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2180 basename))
2181 basename = name_cstr;
2182
2183 m_apple_names_ap->FindByName(basename.data(), die_offsets);
2184 }
2185 } else {
2186 // Index the DWARF if we haven't already
2187 if (!m_indexed)
2188 Index();
2189
2190 m_global_index.Find(name, die_offsets);
2191 }
2192
2193 const size_t num_die_matches = die_offsets.size();
2194 if (num_die_matches) {
2195 SymbolContext sc;
2196 sc.module_sp = m_obj_file->GetModule();
2197 assert(sc.module_sp);
2198
2199 bool done = false;
2200 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2201 const DIERef &die_ref = die_offsets[i];
2202 DWARFDIE die = GetDIE(die_ref);
2203
2204 if (die) {
2205 switch (die.Tag()) {
2206 default:
2207 case DW_TAG_subprogram:
2208 case DW_TAG_inlined_subroutine:
2209 case DW_TAG_try_block:
2210 case DW_TAG_catch_block:
2211 break;
2212
2213 case DW_TAG_variable: {
2214 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2215
2216 if (parent_decl_ctx) {
2217 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2218 if (dwarf_ast) {
2219 CompilerDeclContext actual_parent_decl_ctx =
2220 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2221 if (!actual_parent_decl_ctx ||
2222 actual_parent_decl_ctx != *parent_decl_ctx)
2223 continue;
2224 }
2225 }
2226
2227 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2228 &variables);
2229
2230 if (variables.GetSize() - original_size >= max_matches)
2231 done = true;
2232 } break;
2233 }
2234 } else {
2235 if (m_using_apple_tables) {
2236 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2237 "the DWARF debug information has been modified (.apple_names "
2238 "accelerator table had bad die 0x%8.8x for '%s')\n",
2239 die_ref.die_offset, name.GetCString());
2240 }
2241 }
2242 }
2243 }
2244
2245 // Return the number of variable that were appended to the list
2246 const uint32_t num_matches = variables.GetSize() - original_size;
2247 if (log && num_matches > 0) {
2248 GetObjectFile()->GetModule()->LogMessage(
2249 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2250 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2251 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2252 max_matches, num_matches);
2253 }
2254 return num_matches;
2255}
2256
2257uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2258 bool append, uint32_t max_matches,
2259 VariableList &variables) {
2260 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2261
2262 if (log) {
2263 GetObjectFile()->GetModule()->LogMessage(
2264 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2265 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002266 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002267 }
2268
2269 DWARFDebugInfo *info = DebugInfo();
2270 if (info == NULL)
2271 return 0;
2272
2273 // If we aren't appending the results to this list, then clear the list
2274 if (!append)
2275 variables.Clear();
2276
2277 // Remember how many variables are in the list before we search in case
2278 // we are appending the results to a variable list.
2279 const uint32_t original_size = variables.GetSize();
2280
2281 DIEArray die_offsets;
2282
2283 if (m_using_apple_tables) {
2284 if (m_apple_names_ap.get()) {
2285 DWARFMappedHash::DIEInfoArray hash_data_array;
2286 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2287 hash_data_array))
2288 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2289 }
2290 } else {
2291 // Index the DWARF if we haven't already
2292 if (!m_indexed)
2293 Index();
2294
2295 m_global_index.Find(regex, die_offsets);
2296 }
2297
2298 SymbolContext sc;
2299 sc.module_sp = m_obj_file->GetModule();
2300 assert(sc.module_sp);
2301
2302 const size_t num_matches = die_offsets.size();
2303 if (num_matches) {
2304 for (size_t i = 0; i < num_matches; ++i) {
2305 const DIERef &die_ref = die_offsets[i];
2306 DWARFDIE die = GetDIE(die_ref);
2307
2308 if (die) {
2309 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2310
2311 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2312
2313 if (variables.GetSize() - original_size >= max_matches)
2314 break;
2315 } else {
2316 if (m_using_apple_tables) {
2317 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2318 "the DWARF debug information has been modified (.apple_names "
2319 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002320 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321 }
2322 }
2323 }
2324 }
2325
2326 // Return the number of variable that were appended to the list
2327 return variables.GetSize() - original_size;
2328}
2329
2330bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2331 bool include_inlines,
2332 SymbolContextList &sc_list) {
2333 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2334 return ResolveFunction(die, include_inlines, sc_list);
2335}
2336
2337bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2338 bool include_inlines,
2339 SymbolContextList &sc_list) {
2340 SymbolContext sc;
2341
2342 if (!orig_die)
2343 return false;
2344
2345 // If we were passed a die that is not a function, just return false...
2346 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2347 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2348 return false;
2349
2350 DWARFDIE die = orig_die;
2351 DWARFDIE inlined_die;
2352 if (die.Tag() == DW_TAG_inlined_subroutine) {
2353 inlined_die = die;
2354
2355 while (1) {
2356 die = die.GetParent();
2357
2358 if (die) {
2359 if (die.Tag() == DW_TAG_subprogram)
2360 break;
2361 } else
2362 break;
2363 }
2364 }
2365 assert(die && die.Tag() == DW_TAG_subprogram);
2366 if (GetFunction(die, sc)) {
2367 Address addr;
2368 // Parse all blocks if needed
2369 if (inlined_die) {
2370 Block &function_block = sc.function->GetBlock(true);
2371 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2372 if (sc.block == NULL)
2373 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2374 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2375 addr.Clear();
2376 } else {
2377 sc.block = NULL;
2378 addr = sc.function->GetAddressRange().GetBaseAddress();
2379 }
2380
2381 if (addr.IsValid()) {
2382 sc_list.Append(sc);
2383 return true;
2384 }
2385 }
2386
2387 return false;
2388}
2389
2390void SymbolFileDWARF::FindFunctions(const ConstString &name,
2391 const NameToDIE &name_to_die,
2392 bool include_inlines,
2393 SymbolContextList &sc_list) {
2394 DIEArray die_offsets;
2395 if (name_to_die.Find(name, die_offsets)) {
2396 ParseFunctions(die_offsets, include_inlines, sc_list);
2397 }
2398}
2399
2400void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2401 const NameToDIE &name_to_die,
2402 bool include_inlines,
2403 SymbolContextList &sc_list) {
2404 DIEArray die_offsets;
2405 if (name_to_die.Find(regex, die_offsets)) {
2406 ParseFunctions(die_offsets, include_inlines, sc_list);
2407 }
2408}
2409
2410void SymbolFileDWARF::FindFunctions(
2411 const RegularExpression &regex,
2412 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2413 SymbolContextList &sc_list) {
2414 DIEArray die_offsets;
2415 DWARFMappedHash::DIEInfoArray hash_data_array;
2416 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2417 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2418 ParseFunctions(die_offsets, include_inlines, sc_list);
2419 }
2420}
2421
2422void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2423 bool include_inlines,
2424 SymbolContextList &sc_list) {
2425 const size_t num_matches = die_offsets.size();
2426 if (num_matches) {
2427 for (size_t i = 0; i < num_matches; ++i)
2428 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2429 }
2430}
2431
2432bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2433 const DWARFDIE &die) {
2434 // If we have no parent decl context to match this DIE matches, and if the
2435 // parent
2436 // decl context isn't valid, we aren't trying to look for any particular decl
2437 // context so any die matches.
2438 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2439 return true;
2440
2441 if (die) {
2442 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2443 if (dwarf_ast) {
2444 CompilerDeclContext actual_decl_ctx =
2445 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2446 if (actual_decl_ctx)
2447 return actual_decl_ctx == *decl_ctx;
2448 }
2449 }
2450 return false;
2451}
2452
2453uint32_t
2454SymbolFileDWARF::FindFunctions(const ConstString &name,
2455 const CompilerDeclContext *parent_decl_ctx,
2456 uint32_t name_type_mask, bool include_inlines,
2457 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002458 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2459 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002460 name.AsCString());
2461
2462 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002463 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2465
2466 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2467
2468 if (log) {
2469 GetObjectFile()->GetModule()->LogMessage(
2470 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2471 "name_type_mask=0x%x, append=%u, sc_list)",
2472 name.GetCString(), name_type_mask, append);
2473 }
2474
2475 // If we aren't appending the results to this list, then clear the list
2476 if (!append)
2477 sc_list.Clear();
2478
2479 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2480 return 0;
2481
2482 // If name is empty then we won't find anything.
2483 if (name.IsEmpty())
2484 return 0;
2485
2486 // Remember how many sc_list are in the list before we search in case
2487 // we are appending the results to a variable list.
2488
2489 const char *name_cstr = name.GetCString();
2490
2491 const uint32_t original_size = sc_list.GetSize();
2492
2493 DWARFDebugInfo *info = DebugInfo();
2494 if (info == NULL)
2495 return 0;
2496
2497 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2498 if (m_using_apple_tables) {
2499 if (m_apple_names_ap.get()) {
2500
2501 DIEArray die_offsets;
2502
2503 uint32_t num_matches = 0;
2504
2505 if (name_type_mask & eFunctionNameTypeFull) {
2506 // If they asked for the full name, match what they typed. At some
2507 // point we may
2508 // want to canonicalize this (strip double spaces, etc. For now, we
2509 // just add all the
2510 // dies that we find by exact match.
2511 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2512 for (uint32_t i = 0; i < num_matches; i++) {
2513 const DIERef &die_ref = die_offsets[i];
2514 DWARFDIE die = info->GetDIE(die_ref);
2515 if (die) {
2516 if (!DIEInDeclContext(parent_decl_ctx, die))
2517 continue; // The containing decl contexts don't match
2518
2519 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2520 if (ResolveFunction(die, include_inlines, sc_list))
2521 resolved_dies.insert(die.GetDIE());
2522 }
2523 } else {
2524 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2525 "the DWARF debug information has been modified (.apple_names "
2526 "accelerator table had bad die 0x%8.8x for '%s')",
2527 die_ref.die_offset, name_cstr);
2528 }
2529 }
2530 }
2531
2532 if (name_type_mask & eFunctionNameTypeSelector) {
2533 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2534 return 0; // no selectors in namespaces
2535
2536 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2537 // Now make sure these are actually ObjC methods. In this case we can
2538 // simply look up the name,
2539 // and if it is an ObjC method name, we're good.
2540
2541 for (uint32_t i = 0; i < num_matches; i++) {
2542 const DIERef &die_ref = die_offsets[i];
2543 DWARFDIE die = info->GetDIE(die_ref);
2544 if (die) {
2545 const char *die_name = die.GetName();
2546 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2547 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2548 if (ResolveFunction(die, include_inlines, sc_list))
2549 resolved_dies.insert(die.GetDIE());
2550 }
2551 }
2552 } else {
2553 GetObjectFile()->GetModule()->ReportError(
2554 "the DWARF debug information has been modified (.apple_names "
2555 "accelerator table had bad die 0x%8.8x for '%s')",
2556 die_ref.die_offset, name_cstr);
2557 }
2558 }
2559 die_offsets.clear();
2560 }
2561
2562 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2563 name_type_mask & eFunctionNameTypeBase) {
2564 // The apple_names table stores just the "base name" of C++ methods in
2565 // the table. So we have to
2566 // extract the base name, look that up, and if there is any other
2567 // information in the name we were
2568 // passed in we have to post-filter based on that.
2569
2570 // FIXME: Arrange the logic above so that we don't calculate the base
2571 // name twice:
2572 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2573
2574 for (uint32_t i = 0; i < num_matches; i++) {
2575 const DIERef &die_ref = die_offsets[i];
2576 DWARFDIE die = info->GetDIE(die_ref);
2577 if (die) {
2578 if (!DIEInDeclContext(parent_decl_ctx, die))
2579 continue; // The containing decl contexts don't match
2580
2581 // If we get to here, the die is good, and we should add it:
2582 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2583 ResolveFunction(die, include_inlines, sc_list)) {
2584 bool keep_die = true;
2585 if ((name_type_mask &
2586 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2587 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
2588 // We are looking for either basenames or methods, so we need to
2589 // trim out the ones we won't want by looking at the type
2590 SymbolContext sc;
2591 if (sc_list.GetLastContext(sc)) {
2592 if (sc.block) {
2593 // We have an inlined function
2594 } else if (sc.function) {
2595 Type *type = sc.function->GetType();
2596
2597 if (type) {
2598 CompilerDeclContext decl_ctx =
2599 GetDeclContextContainingUID(type->GetID());
2600 if (decl_ctx.IsStructUnionOrClass()) {
2601 if (name_type_mask & eFunctionNameTypeBase) {
2602 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2603 keep_die = false;
2604 }
2605 } else {
2606 if (name_type_mask & eFunctionNameTypeMethod) {
2607 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2608 keep_die = false;
2609 }
2610 }
2611 } else {
2612 GetObjectFile()->GetModule()->ReportWarning(
2613 "function at die offset 0x%8.8x had no function type",
2614 die_ref.die_offset);
2615 }
2616 }
2617 }
2618 }
2619 if (keep_die)
2620 resolved_dies.insert(die.GetDIE());
2621 }
2622 } else {
2623 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2624 "the DWARF debug information has been modified (.apple_names "
2625 "accelerator table had bad die 0x%8.8x for '%s')",
2626 die_ref.die_offset, name_cstr);
2627 }
2628 }
2629 die_offsets.clear();
2630 }
2631 }
2632 } else {
2633
2634 // Index the DWARF if we haven't already
2635 if (!m_indexed)
2636 Index();
2637
2638 if (name_type_mask & eFunctionNameTypeFull) {
2639 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2640
2641 // FIXME Temporary workaround for global/anonymous namespace
2642 // functions debugging FreeBSD and Linux binaries.
2643 // If we didn't find any functions in the global namespace try
2644 // looking in the basename index but ignore any returned
2645 // functions that have a namespace but keep functions which
2646 // have an anonymous namespace
2647 // TODO: The arch in the object file isn't correct for MSVC
2648 // binaries on windows, we should find a way to make it
2649 // correct and handle those symbols as well.
2650 if (sc_list.GetSize() == original_size) {
2651 ArchSpec arch;
2652 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002653 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002654 SymbolContextList temp_sc_list;
2655 FindFunctions(name, m_function_basename_index, include_inlines,
2656 temp_sc_list);
2657 SymbolContext sc;
2658 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2659 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2660 ConstString mangled_name =
2661 sc.GetFunctionName(Mangled::ePreferMangled);
2662 ConstString demangled_name =
2663 sc.GetFunctionName(Mangled::ePreferDemangled);
2664 // Mangled names on Linux and FreeBSD are of the form:
2665 // _ZN18function_namespace13function_nameEv.
2666 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2667 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2668 21)) {
2669 sc_list.Append(sc);
2670 }
2671 }
2672 }
2673 }
2674 }
2675 }
2676 DIEArray die_offsets;
2677 if (name_type_mask & eFunctionNameTypeBase) {
2678 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2679 for (uint32_t i = 0; i < num_base; i++) {
2680 DWARFDIE die = info->GetDIE(die_offsets[i]);
2681 if (die) {
2682 if (!DIEInDeclContext(parent_decl_ctx, die))
2683 continue; // The containing decl contexts don't match
2684
2685 // If we get to here, the die is good, and we should add it:
2686 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2687 if (ResolveFunction(die, include_inlines, sc_list))
2688 resolved_dies.insert(die.GetDIE());
2689 }
2690 }
2691 }
2692 die_offsets.clear();
2693 }
2694
2695 if (name_type_mask & eFunctionNameTypeMethod) {
2696 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2697 return 0; // no methods in namespaces
2698
2699 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2700 {
2701 for (uint32_t i = 0; i < num_base; i++) {
2702 DWARFDIE die = info->GetDIE(die_offsets[i]);
2703 if (die) {
2704 // If we get to here, the die is good, and we should add it:
2705 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2706 if (ResolveFunction(die, include_inlines, sc_list))
2707 resolved_dies.insert(die.GetDIE());
2708 }
2709 }
2710 }
2711 }
2712 die_offsets.clear();
2713 }
2714
2715 if ((name_type_mask & eFunctionNameTypeSelector) &&
2716 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2717 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2718 }
2719 }
2720
2721 // Return the number of variable that were appended to the list
2722 const uint32_t num_matches = sc_list.GetSize() - original_size;
2723
2724 if (log && num_matches > 0) {
2725 GetObjectFile()->GetModule()->LogMessage(
2726 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2727 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2728 "%u",
2729 name.GetCString(), name_type_mask, include_inlines, append,
2730 num_matches);
2731 }
2732 return num_matches;
2733}
2734
2735uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2736 bool include_inlines, bool append,
2737 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002738 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2739 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002740 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002741
2742 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2743
2744 if (log) {
2745 GetObjectFile()->GetModule()->LogMessage(
2746 log,
2747 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002748 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002749 }
2750
2751 // If we aren't appending the results to this list, then clear the list
2752 if (!append)
2753 sc_list.Clear();
2754
2755 // Remember how many sc_list are in the list before we search in case
2756 // we are appending the results to a variable list.
2757 uint32_t original_size = sc_list.GetSize();
2758
2759 if (m_using_apple_tables) {
2760 if (m_apple_names_ap.get())
2761 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2762 } else {
2763 // Index the DWARF if we haven't already
2764 if (!m_indexed)
2765 Index();
2766
2767 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2768
2769 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2770 }
2771
2772 // Return the number of variable that were appended to the list
2773 return sc_list.GetSize() - original_size;
2774}
2775
2776void SymbolFileDWARF::GetMangledNamesForFunction(
2777 const std::string &scope_qualified_name,
2778 std::vector<ConstString> &mangled_names) {
2779 DWARFDebugInfo *info = DebugInfo();
2780 uint32_t num_comp_units = 0;
2781 if (info)
2782 num_comp_units = info->GetNumCompileUnits();
2783
2784 for (uint32_t i = 0; i < num_comp_units; i++) {
2785 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2786 if (cu == nullptr)
2787 continue;
2788
2789 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2790 if (dwo)
2791 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2792 }
2793
2794 NameToOffsetMap::iterator iter =
2795 m_function_scope_qualified_name_map.find(scope_qualified_name);
2796 if (iter == m_function_scope_qualified_name_map.end())
2797 return;
2798
2799 DIERefSetSP set_sp = (*iter).second;
2800 std::set<DIERef>::iterator set_iter;
2801 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2802 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2803 mangled_names.push_back(ConstString(die.GetMangledName()));
2804 }
2805}
2806
2807uint32_t SymbolFileDWARF::FindTypes(
2808 const SymbolContext &sc, const ConstString &name,
2809 const CompilerDeclContext *parent_decl_ctx, bool append,
2810 uint32_t max_matches,
2811 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2812 TypeMap &types) {
2813 // If we aren't appending the results to this list, then clear the list
2814 if (!append)
2815 types.Clear();
2816
2817 // Make sure we haven't already searched this SymbolFile before...
2818 if (searched_symbol_files.count(this))
2819 return 0;
2820 else
2821 searched_symbol_files.insert(this);
2822
2823 DWARFDebugInfo *info = DebugInfo();
2824 if (info == NULL)
2825 return 0;
2826
2827 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2828
2829 if (log) {
2830 if (parent_decl_ctx)
2831 GetObjectFile()->GetModule()->LogMessage(
2832 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2833 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2834 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2835 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2836 else
2837 GetObjectFile()->GetModule()->LogMessage(
2838 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2839 "NULL, append=%u, max_matches=%u, type_list)",
2840 name.GetCString(), append, max_matches);
2841 }
2842
2843 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2844 return 0;
2845
2846 DIEArray die_offsets;
2847
2848 if (m_using_apple_tables) {
2849 if (m_apple_types_ap.get()) {
2850 const char *name_cstr = name.GetCString();
2851 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2852 }
2853 } else {
2854 if (!m_indexed)
2855 Index();
2856
2857 m_type_index.Find(name, die_offsets);
2858 }
2859
2860 const size_t num_die_matches = die_offsets.size();
2861
2862 if (num_die_matches) {
2863 const uint32_t initial_types_size = types.GetSize();
2864 for (size_t i = 0; i < num_die_matches; ++i) {
2865 const DIERef &die_ref = die_offsets[i];
2866 DWARFDIE die = GetDIE(die_ref);
2867
2868 if (die) {
2869 if (!DIEInDeclContext(parent_decl_ctx, die))
2870 continue; // The containing decl contexts don't match
2871
2872 Type *matching_type = ResolveType(die, true, true);
2873 if (matching_type) {
2874 // We found a type pointer, now find the shared pointer form our type
2875 // list
2876 types.InsertUnique(matching_type->shared_from_this());
2877 if (types.GetSize() >= max_matches)
2878 break;
2879 }
2880 } else {
2881 if (m_using_apple_tables) {
2882 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2883 "the DWARF debug information has been modified (.apple_types "
2884 "accelerator table had bad die 0x%8.8x for '%s')\n",
2885 die_ref.die_offset, name.GetCString());
2886 }
2887 }
2888 }
2889 const uint32_t num_matches = types.GetSize() - initial_types_size;
2890 if (log && num_matches) {
2891 if (parent_decl_ctx) {
2892 GetObjectFile()->GetModule()->LogMessage(
2893 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2894 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2895 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2896 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2897 num_matches);
2898 } else {
2899 GetObjectFile()->GetModule()->LogMessage(
2900 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2901 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2902 name.GetCString(), append, max_matches, num_matches);
2903 }
2904 }
2905 return num_matches;
2906 } else {
2907 UpdateExternalModuleListIfNeeded();
2908
2909 for (const auto &pair : m_external_type_modules) {
2910 ModuleSP external_module_sp = pair.second;
2911 if (external_module_sp) {
2912 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2913 if (sym_vendor) {
2914 const uint32_t num_external_matches =
2915 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2916 max_matches, searched_symbol_files, types);
2917 if (num_external_matches)
2918 return num_external_matches;
2919 }
2920 }
2921 }
2922 }
2923
2924 return 0;
2925}
2926
2927size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2928 bool append, TypeMap &types) {
2929 if (!append)
2930 types.Clear();
2931
2932 if (context.empty())
2933 return 0;
2934
2935 DIEArray die_offsets;
2936
2937 ConstString name = context.back().name;
2938
2939 if (!name)
2940 return 0;
2941
2942 if (m_using_apple_tables) {
2943 if (m_apple_types_ap.get()) {
2944 const char *name_cstr = name.GetCString();
2945 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2946 }
2947 } else {
2948 if (!m_indexed)
2949 Index();
2950
2951 m_type_index.Find(name, die_offsets);
2952 }
2953
2954 const size_t num_die_matches = die_offsets.size();
2955
2956 if (num_die_matches) {
2957 size_t num_matches = 0;
2958 for (size_t i = 0; i < num_die_matches; ++i) {
2959 const DIERef &die_ref = die_offsets[i];
2960 DWARFDIE die = GetDIE(die_ref);
2961
2962 if (die) {
2963 std::vector<CompilerContext> die_context;
2964 die.GetDWOContext(die_context);
2965 if (die_context != context)
2966 continue;
2967
2968 Type *matching_type = ResolveType(die, true, true);
2969 if (matching_type) {
2970 // We found a type pointer, now find the shared pointer form our type
2971 // list
2972 types.InsertUnique(matching_type->shared_from_this());
2973 ++num_matches;
2974 }
2975 } else {
2976 if (m_using_apple_tables) {
2977 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2978 "the DWARF debug information has been modified (.apple_types "
2979 "accelerator table had bad die 0x%8.8x for '%s')\n",
2980 die_ref.die_offset, name.GetCString());
2981 }
2982 }
2983 }
2984 return num_matches;
2985 }
2986 return 0;
2987}
2988
2989CompilerDeclContext
2990SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2991 const CompilerDeclContext *parent_decl_ctx) {
2992 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2993
2994 if (log) {
2995 GetObjectFile()->GetModule()->LogMessage(
2996 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2997 name.GetCString());
2998 }
2999
3000 CompilerDeclContext namespace_decl_ctx;
3001
3002 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
3003 return namespace_decl_ctx;
3004
3005 DWARFDebugInfo *info = DebugInfo();
3006 if (info) {
3007 DIEArray die_offsets;
3008
3009 // Index if we already haven't to make sure the compile units
3010 // get indexed and make their global DIE index list
3011 if (m_using_apple_tables) {
3012 if (m_apple_namespaces_ap.get()) {
3013 const char *name_cstr = name.GetCString();
3014 m_apple_namespaces_ap->FindByName(name_cstr, die_offsets);
3015 }
3016 } else {
3017 if (!m_indexed)
3018 Index();
3019
3020 m_namespace_index.Find(name, die_offsets);
3021 }
3022
3023 const size_t num_matches = die_offsets.size();
3024 if (num_matches) {
3025 for (size_t i = 0; i < num_matches; ++i) {
3026 const DIERef &die_ref = die_offsets[i];
3027 DWARFDIE die = GetDIE(die_ref);
3028
3029 if (die) {
3030 if (!DIEInDeclContext(parent_decl_ctx, die))
3031 continue; // The containing decl contexts don't match
3032
3033 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3034 if (dwarf_ast) {
3035 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
3036 if (namespace_decl_ctx)
3037 break;
3038 }
3039 } else {
3040 if (m_using_apple_tables) {
3041 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3042 "the DWARF debug information has been modified "
3043 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
3044 "'%s')\n",
3045 die_ref.die_offset, name.GetCString());
3046 }
3047 }
3048 }
3049 }
3050 }
3051 if (log && namespace_decl_ctx) {
3052 GetObjectFile()->GetModule()->LogMessage(
3053 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
3054 "CompilerDeclContext(%p/%p) \"%s\"",
3055 name.GetCString(),
3056 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
3057 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
3058 namespace_decl_ctx.GetName().AsCString("<NULL>"));
3059 }
3060
3061 return namespace_decl_ctx;
3062}
3063
3064TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3065 bool resolve_function_context) {
3066 TypeSP type_sp;
3067 if (die) {
3068 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3069 if (type_ptr == NULL) {
3070 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3071 assert(lldb_cu);
3072 SymbolContext sc(lldb_cu);
3073 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3074 while (parent_die != nullptr) {
3075 if (parent_die->Tag() == DW_TAG_subprogram)
3076 break;
3077 parent_die = parent_die->GetParent();
3078 }
3079 SymbolContext sc_backup = sc;
3080 if (resolve_function_context && parent_die != nullptr &&
3081 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3082 sc = sc_backup;
3083
3084 type_sp = ParseType(sc, die, NULL);
3085 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3086 // Grab the existing type from the master types lists
3087 type_sp = type_ptr->shared_from_this();
3088 }
3089 }
3090 return type_sp;
3091}
3092
3093DWARFDIE
3094SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3095 if (orig_die) {
3096 DWARFDIE die = orig_die;
3097
3098 while (die) {
3099 // If this is the original DIE that we are searching for a declaration
3100 // for, then don't look in the cache as we don't want our own decl
3101 // context to be our decl context...
3102 if (orig_die != die) {
3103 switch (die.Tag()) {
3104 case DW_TAG_compile_unit:
3105 case DW_TAG_namespace:
3106 case DW_TAG_structure_type:
3107 case DW_TAG_union_type:
3108 case DW_TAG_class_type:
3109 case DW_TAG_lexical_block:
3110 case DW_TAG_subprogram:
3111 return die;
Sean Callananb4945782017-04-24 22:11:10 +00003112 case DW_TAG_inlined_subroutine: {
3113 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3114 if (abs_die) {
3115 return abs_die;
3116 }
3117 break;
3118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003119 default:
3120 break;
3121 }
3122 }
3123
3124 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3125 if (spec_die) {
3126 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3127 if (decl_ctx_die)
3128 return decl_ctx_die;
3129 }
3130
3131 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3132 if (abs_die) {
3133 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3134 if (decl_ctx_die)
3135 return decl_ctx_die;
3136 }
3137
3138 die = die.GetParent();
3139 }
3140 }
3141 return DWARFDIE();
3142}
3143
3144Symbol *
3145SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3146 Symbol *objc_class_symbol = NULL;
3147 if (m_obj_file) {
3148 Symtab *symtab = m_obj_file->GetSymtab();
3149 if (symtab) {
3150 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3151 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3152 Symtab::eVisibilityAny);
3153 }
3154 }
3155 return objc_class_symbol;
3156}
3157
3158// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
3159// they don't
3160// then we can end up looking through all class types for a complete type and
3161// never find
3162// the full definition. We need to know if this attribute is supported, so we
3163// determine
3164// this here and cache th result. We also need to worry about the debug map
3165// DWARF file
3166// if we are doing darwin DWARF in .o file debugging.
3167bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
3168 DWARFCompileUnit *cu) {
3169 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3170 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3171 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3172 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3173 else {
3174 DWARFDebugInfo *debug_info = DebugInfo();
3175 const uint32_t num_compile_units = GetNumCompileUnits();
3176 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
3177 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3178 if (dwarf_cu != cu &&
3179 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3180 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3181 break;
3182 }
3183 }
3184 }
3185 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3186 GetDebugMapSymfile())
3187 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3188 }
3189 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003190}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003191
3192// This function can be used when a DIE is found that is a forward declaration
3193// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3195 const DWARFDIE &die, const ConstString &type_name,
3196 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003197
Kate Stoneb9c1b512016-09-06 20:57:50 +00003198 TypeSP type_sp;
3199
3200 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3201 return type_sp;
3202
3203 DIEArray die_offsets;
3204
3205 if (m_using_apple_tables) {
3206 if (m_apple_types_ap.get()) {
3207 const char *name_cstr = type_name.GetCString();
3208 m_apple_types_ap->FindCompleteObjCClassByName(name_cstr, die_offsets,
3209 must_be_implementation);
3210 }
3211 } else {
3212 if (!m_indexed)
3213 Index();
3214
3215 m_type_index.Find(type_name, die_offsets);
3216 }
3217
3218 const size_t num_matches = die_offsets.size();
3219
3220 if (num_matches) {
3221 for (size_t i = 0; i < num_matches; ++i) {
3222 const DIERef &die_ref = die_offsets[i];
3223 DWARFDIE type_die = GetDIE(die_ref);
3224
3225 if (type_die) {
3226 bool try_resolving_type = false;
3227
3228 // Don't try and resolve the DIE we are looking for with the DIE itself!
3229 if (type_die != die) {
3230 switch (type_die.Tag()) {
3231 case DW_TAG_class_type:
3232 case DW_TAG_structure_type:
3233 try_resolving_type = true;
3234 break;
3235 default:
3236 break;
3237 }
3238 }
3239
3240 if (try_resolving_type) {
3241 if (must_be_implementation &&
3242 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3243 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3244 DW_AT_APPLE_objc_complete_type, 0);
3245
3246 if (try_resolving_type) {
3247 Type *resolved_type = ResolveType(type_die, false, true);
3248 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3249 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3250 " (cu 0x%8.8" PRIx64 ")\n",
3251 die.GetID(),
3252 m_obj_file->GetFileSpec().GetFilename().AsCString(
3253 "<Unknown>"),
3254 type_die.GetID(), type_cu->GetID());
3255
3256 if (die)
3257 GetDIEToType()[die.GetDIE()] = resolved_type;
3258 type_sp = resolved_type->shared_from_this();
3259 break;
3260 }
3261 }
3262 }
3263 } else {
3264 if (m_using_apple_tables) {
3265 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3266 "the DWARF debug information has been modified (.apple_types "
3267 "accelerator table had bad die 0x%8.8x for '%s')\n",
3268 die_ref.die_offset, type_name.GetCString());
3269 }
3270 }
3271 }
3272 }
3273 return type_sp;
3274}
Greg Claytona8022fa2012-04-24 21:22:41 +00003275
Greg Clayton80c26302012-02-05 06:12:47 +00003276//----------------------------------------------------------------------
3277// This function helps to ensure that the declaration contexts match for
Kate Stoneb9c1b512016-09-06 20:57:50 +00003278// two different DIEs. Often times debug information will refer to a
Greg Clayton80c26302012-02-05 06:12:47 +00003279// forward declaration of a type (the equivalent of "struct my_struct;".
3280// There will often be a declaration of that type elsewhere that has the
3281// full definition. When we go looking for the full type "my_struct", we
3282// will find one or more matches in the accelerator tables and we will
Kate Stoneb9c1b512016-09-06 20:57:50 +00003283// then need to make sure the type was in the same declaration context
Greg Clayton80c26302012-02-05 06:12:47 +00003284// as the original DIE. This function can efficiently compare two DIEs
3285// and will return true when the declaration context matches, and false
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286// when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003287//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003288bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3289 const DWARFDIE &die2) {
3290 if (die1 == die2)
3291 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003292
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293 DWARFDIECollection decl_ctx_1;
3294 DWARFDIECollection decl_ctx_2;
3295 // The declaration DIE stack is a stack of the declaration context
3296 // DIEs all the way back to the compile unit. If a type "T" is
3297 // declared inside a class "B", and class "B" is declared inside
3298 // a class "A" and class "A" is in a namespace "lldb", and the
3299 // namespace is in a compile unit, there will be a stack of DIEs:
3300 //
3301 // [0] DW_TAG_class_type for "B"
3302 // [1] DW_TAG_class_type for "A"
3303 // [2] DW_TAG_namespace for "lldb"
3304 // [3] DW_TAG_compile_unit for the source file.
3305 //
3306 // We grab both contexts and make sure that everything matches
3307 // all the way back to the compiler unit.
3308
3309 // First lets grab the decl contexts for both DIEs
3310 die1.GetDeclContextDIEs(decl_ctx_1);
3311 die2.GetDeclContextDIEs(decl_ctx_2);
3312 // Make sure the context arrays have the same size, otherwise
3313 // we are done
3314 const size_t count1 = decl_ctx_1.Size();
3315 const size_t count2 = decl_ctx_2.Size();
3316 if (count1 != count2)
3317 return false;
3318
3319 // Make sure the DW_TAG values match all the way back up the
3320 // compile unit. If they don't, then we are done.
3321 DWARFDIE decl_ctx_die1;
3322 DWARFDIE decl_ctx_die2;
3323 size_t i;
3324 for (i = 0; i < count1; i++) {
3325 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3326 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3327 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3328 return false;
3329 }
Greg Clayton890ff562012-02-02 05:48:16 +00003330#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003331
Kate Stoneb9c1b512016-09-06 20:57:50 +00003332 // Make sure the top item in the decl context die array is always
3333 // DW_TAG_compile_unit. If it isn't then something went wrong in
3334 // the DWARFDIE::GetDeclContextDIEs() function...
3335 assert(decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003336
Greg Clayton890ff562012-02-02 05:48:16 +00003337#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00003338 // Always skip the compile unit when comparing by only iterating up to
3339 // "count - 1". Here we compare the names as we go.
3340 for (i = 0; i < count1 - 1; i++) {
3341 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3342 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3343 const char *name1 = decl_ctx_die1.GetName();
3344 const char *name2 = decl_ctx_die2.GetName();
3345 // If the string was from a DW_FORM_strp, then the pointer will often
3346 // be the same!
3347 if (name1 == name2)
3348 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003349
Kate Stoneb9c1b512016-09-06 20:57:50 +00003350 // Name pointers are not equal, so only compare the strings
3351 // if both are not NULL.
3352 if (name1 && name2) {
3353 // If the strings don't compare, we are done...
3354 if (strcmp(name1, name2) != 0)
3355 return false;
3356 } else {
3357 // One name was NULL while the other wasn't
3358 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003359 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360 }
3361 // We made it through all of the checks and the declaration contexts
3362 // are equal.
3363 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003364}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003365
Kate Stoneb9c1b512016-09-06 20:57:50 +00003366TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3367 const DWARFDeclContext &dwarf_decl_ctx) {
3368 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003369
Kate Stoneb9c1b512016-09-06 20:57:50 +00003370 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3371 if (dwarf_decl_ctx_count > 0) {
3372 const ConstString type_name(dwarf_decl_ctx[0].name);
3373 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003374
Kate Stoneb9c1b512016-09-06 20:57:50 +00003375 if (type_name) {
3376 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3377 DWARF_LOG_LOOKUPS));
3378 if (log) {
3379 GetObjectFile()->GetModule()->LogMessage(
3380 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3381 "s, qualified-name='%s')",
3382 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3383 dwarf_decl_ctx.GetQualifiedName());
3384 }
3385
3386 DIEArray die_offsets;
3387
3388 if (m_using_apple_tables) {
3389 if (m_apple_types_ap.get()) {
3390 const bool has_tag =
3391 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3392 DWARFMappedHash::eAtomTypeTag);
3393 const bool has_qualified_name_hash =
3394 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3395 DWARFMappedHash::eAtomTypeQualNameHash);
3396 if (has_tag && has_qualified_name_hash) {
3397 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3398 const uint32_t qualified_name_hash =
3399 MappedHash::HashStringUsingDJB(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003400 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003401 GetObjectFile()->GetModule()->LogMessage(
3402 log, "FindByNameAndTagAndQualifiedNameHash()");
3403 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
3404 type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3405 } else if (has_tag) {
3406 if (log)
3407 GetObjectFile()->GetModule()->LogMessage(log,
3408 "FindByNameAndTag()");
3409 m_apple_types_ap->FindByNameAndTag(type_name.GetCString(), tag,
3410 die_offsets);
3411 } else {
3412 m_apple_types_ap->FindByName(type_name.GetCString(), die_offsets);
3413 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003415 } else {
3416 if (!m_indexed)
3417 Index();
3418
3419 m_type_index.Find(type_name, die_offsets);
3420 }
3421
3422 const size_t num_matches = die_offsets.size();
3423
3424 // Get the type system that we are looking to find a type for. We will use
3425 // this
3426 // to ensure any matches we find are in a language that this type system
3427 // supports
3428 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3429 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3430 ? nullptr
3431 : GetTypeSystemForLanguage(language);
3432
3433 if (num_matches) {
3434 for (size_t i = 0; i < num_matches; ++i) {
3435 const DIERef &die_ref = die_offsets[i];
3436 DWARFDIE type_die = GetDIE(die_ref);
3437
3438 if (type_die) {
3439 // Make sure type_die's langauge matches the type system we are
3440 // looking for.
3441 // We don't want to find a "Foo" type from Java if we are looking
3442 // for a "Foo"
3443 // type for C, C++, ObjC, or ObjC++.
3444 if (type_system &&
3445 !type_system->SupportsLanguage(type_die.GetLanguage()))
3446 continue;
3447 bool try_resolving_type = false;
3448
3449 // Don't try and resolve the DIE we are looking for with the DIE
3450 // itself!
3451 const dw_tag_t type_tag = type_die.Tag();
3452 // Make sure the tags match
3453 if (type_tag == tag) {
3454 // The tags match, lets try resolving this type
3455 try_resolving_type = true;
3456 } else {
3457 // The tags don't match, but we need to watch our for a
3458 // forward declaration for a struct and ("struct foo")
3459 // ends up being a class ("class foo { ... };") or
3460 // vice versa.
3461 switch (type_tag) {
3462 case DW_TAG_class_type:
3463 // We had a "class foo", see if we ended up with a "struct foo {
3464 // ... };"
3465 try_resolving_type = (tag == DW_TAG_structure_type);
3466 break;
3467 case DW_TAG_structure_type:
3468 // We had a "struct foo", see if we ended up with a "class foo {
3469 // ... };"
3470 try_resolving_type = (tag == DW_TAG_class_type);
3471 break;
3472 default:
3473 // Tags don't match, don't event try to resolve
3474 // using this type whose name matches....
3475 break;
3476 }
3477 }
3478
3479 if (try_resolving_type) {
3480 DWARFDeclContext type_dwarf_decl_ctx;
3481 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3482
3483 if (log) {
3484 GetObjectFile()->GetModule()->LogMessage(
3485 log, "SymbolFileDWARF::"
3486 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3487 "qualified-name='%s') trying die=0x%8.8x (%s)",
3488 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3489 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3490 type_dwarf_decl_ctx.GetQualifiedName());
3491 }
3492
3493 // Make sure the decl contexts match all the way up
3494 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3495 Type *resolved_type = ResolveType(type_die, false);
3496 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3497 type_sp = resolved_type->shared_from_this();
3498 break;
3499 }
3500 }
3501 } else {
3502 if (log) {
3503 std::string qualified_name;
3504 type_die.GetQualifiedName(qualified_name);
3505 GetObjectFile()->GetModule()->LogMessage(
3506 log, "SymbolFileDWARF::"
3507 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3508 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3509 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3510 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3511 qualified_name.c_str());
3512 }
3513 }
3514 } else {
3515 if (m_using_apple_tables) {
3516 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3517 "the DWARF debug information has been modified (.apple_types "
3518 "accelerator table had bad die 0x%8.8x for '%s')\n",
3519 die_ref.die_offset, type_name.GetCString());
3520 }
3521 }
3522 }
3523 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003524 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003525 }
3526 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003527}
3528
Kate Stoneb9c1b512016-09-06 20:57:50 +00003529TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3530 bool *type_is_new_ptr) {
3531 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003532
Kate Stoneb9c1b512016-09-06 20:57:50 +00003533 if (die) {
3534 TypeSystem *type_system =
3535 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003536
Kate Stoneb9c1b512016-09-06 20:57:50 +00003537 if (type_system) {
3538 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3539 if (dwarf_ast) {
3540 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3541 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3542 if (type_sp) {
3543 TypeList *type_list = GetTypeList();
3544 if (type_list)
3545 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003546
Kate Stoneb9c1b512016-09-06 20:57:50 +00003547 if (die.Tag() == DW_TAG_subprogram) {
3548 DIERef die_ref = die.GetDIERef();
3549 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3550 .GetScopeQualifiedName()
3551 .AsCString(""));
3552 if (scope_qualified_name.size()) {
3553 NameToOffsetMap::iterator iter =
3554 m_function_scope_qualified_name_map.find(
3555 scope_qualified_name);
3556 if (iter != m_function_scope_qualified_name_map.end())
3557 (*iter).second->insert(die_ref);
3558 else {
3559 DIERefSetSP new_set(new std::set<DIERef>);
3560 new_set->insert(die_ref);
3561 m_function_scope_qualified_name_map.emplace(
3562 std::make_pair(scope_qualified_name, new_set));
3563 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003565 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003567 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569 }
3570
3571 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003572}
3573
Kate Stoneb9c1b512016-09-06 20:57:50 +00003574size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3575 const DWARFDIE &orig_die,
3576 bool parse_siblings, bool parse_children) {
3577 size_t types_added = 0;
3578 DWARFDIE die = orig_die;
3579 while (die) {
3580 bool type_is_new = false;
3581 if (ParseType(sc, die, &type_is_new).get()) {
3582 if (type_is_new)
3583 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003585
3586 if (parse_children && die.HasChildren()) {
3587 if (die.Tag() == DW_TAG_subprogram) {
3588 SymbolContext child_sc(sc);
3589 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3590 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3591 } else
3592 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3593 }
3594
3595 if (parse_siblings)
3596 die = die.GetSibling();
3597 else
3598 die.Clear();
3599 }
3600 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003601}
3602
Kate Stoneb9c1b512016-09-06 20:57:50 +00003603size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3604 assert(sc.comp_unit && sc.function);
3605 size_t functions_added = 0;
3606 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3607 if (dwarf_cu) {
3608 const dw_offset_t function_die_offset = sc.function->GetID();
3609 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3610 if (function_die) {
3611 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3612 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003613 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003614 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003615
Kate Stoneb9c1b512016-09-06 20:57:50 +00003616 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617}
3618
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3620 // At least a compile unit must be valid
3621 assert(sc.comp_unit);
3622 size_t types_added = 0;
3623 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3624 if (dwarf_cu) {
3625 if (sc.function) {
3626 dw_offset_t function_die_offset = sc.function->GetID();
3627 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3628 if (func_die && func_die.HasChildren()) {
3629 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3630 }
3631 } else {
3632 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3633 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3634 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3635 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003637 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003638
Kate Stoneb9c1b512016-09-06 20:57:50 +00003639 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640}
3641
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3643 if (sc.comp_unit != NULL) {
3644 DWARFDebugInfo *info = DebugInfo();
3645 if (info == NULL)
3646 return 0;
3647
3648 if (sc.function) {
3649 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3650
3651 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3652 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3653 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3654 const size_t num_variables = ParseVariables(
3655 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3656
3657 // Let all blocks know they have parse all their variables
3658 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3659 return num_variables;
3660 }
3661 } else if (sc.comp_unit) {
3662 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3663
3664 if (dwarf_cu == NULL)
3665 return 0;
3666
3667 uint32_t vars_added = 0;
3668 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3669
3670 if (variables.get() == NULL) {
3671 variables.reset(new VariableList());
3672 sc.comp_unit->SetVariableList(variables);
3673
3674 DIEArray die_offsets;
3675 if (m_using_apple_tables) {
3676 if (m_apple_names_ap.get()) {
3677 DWARFMappedHash::DIEInfoArray hash_data_array;
3678 if (m_apple_names_ap->AppendAllDIEsInRange(
3679 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3680 hash_data_array)) {
3681 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003683 }
3684 } else {
3685 // Index if we already haven't to make sure the compile units
3686 // get indexed and make their global DIE index list
3687 if (!m_indexed)
3688 Index();
3689
3690 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3691 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003692 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003693
Kate Stoneb9c1b512016-09-06 20:57:50 +00003694 const size_t num_matches = die_offsets.size();
3695 if (num_matches) {
3696 for (size_t i = 0; i < num_matches; ++i) {
3697 const DIERef &die_ref = die_offsets[i];
3698 DWARFDIE die = GetDIE(die_ref);
3699 if (die) {
3700 VariableSP var_sp(
3701 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3702 if (var_sp) {
3703 variables->AddVariableIfUnique(var_sp);
3704 ++vars_added;
3705 }
3706 } else {
3707 if (m_using_apple_tables) {
3708 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3709 "the DWARF debug information has been modified "
3710 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3711 die_ref.die_offset);
3712 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003713 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003714 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003715 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003716 }
3717 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003718 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003719 }
3720 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003721}
3722
Kate Stoneb9c1b512016-09-06 20:57:50 +00003723VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3724 const DWARFDIE &die,
3725 const lldb::addr_t func_low_pc) {
3726 if (die.GetDWARF() != this)
3727 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003728
Kate Stoneb9c1b512016-09-06 20:57:50 +00003729 VariableSP var_sp;
3730 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003731 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003732
Kate Stoneb9c1b512016-09-06 20:57:50 +00003733 var_sp = GetDIEToVariable()[die.GetDIE()];
3734 if (var_sp)
3735 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003736
Kate Stoneb9c1b512016-09-06 20:57:50 +00003737 const dw_tag_t tag = die.Tag();
3738 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003739
Kate Stoneb9c1b512016-09-06 20:57:50 +00003740 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3741 (tag == DW_TAG_formal_parameter && sc.function)) {
3742 DWARFAttributes attributes;
3743 const size_t num_attributes = die.GetAttributes(attributes);
3744 DWARFDIE spec_die;
3745 if (num_attributes > 0) {
3746 const char *name = NULL;
3747 const char *mangled = NULL;
3748 Declaration decl;
3749 uint32_t i;
3750 DWARFFormValue type_die_form;
3751 DWARFExpression location(die.GetCU());
3752 bool is_external = false;
3753 bool is_artificial = false;
3754 bool location_is_const_value_data = false;
3755 bool has_explicit_location = false;
3756 DWARFFormValue const_value;
3757 Variable::RangeList scope_ranges;
3758 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003759
Kate Stoneb9c1b512016-09-06 20:57:50 +00003760 for (i = 0; i < num_attributes; ++i) {
3761 dw_attr_t attr = attributes.AttributeAtIndex(i);
3762 DWARFFormValue form_value;
3763
3764 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3765 switch (attr) {
3766 case DW_AT_decl_file:
3767 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3768 form_value.Unsigned()));
3769 break;
3770 case DW_AT_decl_line:
3771 decl.SetLine(form_value.Unsigned());
3772 break;
3773 case DW_AT_decl_column:
3774 decl.SetColumn(form_value.Unsigned());
3775 break;
3776 case DW_AT_name:
3777 name = form_value.AsCString();
3778 break;
3779 case DW_AT_linkage_name:
3780 case DW_AT_MIPS_linkage_name:
3781 mangled = form_value.AsCString();
3782 break;
3783 case DW_AT_type:
3784 type_die_form = form_value;
3785 break;
3786 case DW_AT_external:
3787 is_external = form_value.Boolean();
3788 break;
3789 case DW_AT_const_value:
3790 // If we have already found a DW_AT_location attribute, ignore this
3791 // attribute.
3792 if (!has_explicit_location) {
3793 location_is_const_value_data = true;
3794 // The constant value will be either a block, a data value or a
3795 // string.
3796 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3797 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3798 // Retrieve the value as a block expression.
3799 uint32_t block_offset =
3800 form_value.BlockData() - debug_info_data.GetDataStart();
3801 uint32_t block_length = form_value.Unsigned();
3802 location.CopyOpcodeData(module, debug_info_data, block_offset,
3803 block_length);
3804 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3805 // Retrieve the value as a data expression.
3806 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3807 DWARFFormValue::GetFixedFormSizesForAddressSize(
3808 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3809 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3810 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3811 uint32_t data_length =
3812 fixed_form_sizes.GetSize(form_value.Form());
3813 if (data_length == 0) {
3814 const uint8_t *data_pointer = form_value.BlockData();
3815 if (data_pointer) {
3816 form_value.Unsigned();
3817 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3818 // we need to get the byte size of the type later after we
3819 // create the variable
3820 const_value = form_value;
3821 }
3822 } else
3823 location.CopyOpcodeData(module, debug_info_data, data_offset,
3824 data_length);
3825 } else {
3826 // Retrieve the value as a string expression.
3827 if (form_value.Form() == DW_FORM_strp) {
3828 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3829 DWARFFormValue::GetFixedFormSizesForAddressSize(
3830 attributes.CompileUnitAtIndex(i)
3831 ->GetAddressByteSize(),
3832 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3833 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3834 uint32_t data_length =
3835 fixed_form_sizes.GetSize(form_value.Form());
3836 location.CopyOpcodeData(module, debug_info_data, data_offset,
3837 data_length);
3838 } else {
3839 const char *str = form_value.AsCString();
3840 uint32_t string_offset =
3841 str - (const char *)debug_info_data.GetDataStart();
3842 uint32_t string_length = strlen(str) + 1;
3843 location.CopyOpcodeData(module, debug_info_data,
3844 string_offset, string_length);
3845 }
3846 }
3847 }
3848 break;
3849 case DW_AT_location: {
3850 location_is_const_value_data = false;
3851 has_explicit_location = true;
3852 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3853 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3854
3855 uint32_t block_offset =
3856 form_value.BlockData() - debug_info_data.GetDataStart();
3857 uint32_t block_length = form_value.Unsigned();
3858 location.CopyOpcodeData(module, get_debug_info_data(),
3859 block_offset, block_length);
3860 } else {
3861 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3862 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3863
3864 size_t loc_list_length = DWARFExpression::LocationListSize(
3865 die.GetCU(), debug_loc_data, debug_loc_offset);
3866 if (loc_list_length > 0) {
3867 location.CopyOpcodeData(module, debug_loc_data,
3868 debug_loc_offset, loc_list_length);
3869 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3870 location.SetLocationListSlide(
3871 func_low_pc -
3872 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3873 }
3874 }
3875 } break;
3876 case DW_AT_specification:
3877 spec_die = GetDIE(DIERef(form_value));
3878 break;
3879 case DW_AT_start_scope: {
3880 if (form_value.Form() == DW_FORM_sec_offset) {
3881 DWARFRangeList dwarf_scope_ranges;
3882 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003883 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3884 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003885 dwarf_scope_ranges);
3886
3887 // All DW_AT_start_scope are relative to the base address of the
3888 // compile unit. We add the compile unit base address to make
3889 // sure all the addresses are properly fixed up.
3890 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3891 i < count; ++i) {
3892 const DWARFRangeList::Entry &range =
3893 dwarf_scope_ranges.GetEntryRef(i);
3894 scope_ranges.Append(range.GetRangeBase() +
3895 die.GetCU()->GetBaseAddress(),
3896 range.GetByteSize());
3897 }
3898 } else {
3899 // TODO: Handle the case when DW_AT_start_scope have form
3900 // constant. The
3901 // dwarf spec is a bit ambiguous about what is the expected
3902 // behavior in
3903 // case the enclosing block have a non coninious address range and
3904 // the
3905 // DW_AT_start_scope entry have a form constant.
3906 GetObjectFile()->GetModule()->ReportWarning(
3907 "0x%8.8" PRIx64
3908 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3909 die.GetID(), form_value.Form());
3910 }
3911
3912 scope_ranges.Sort();
3913 scope_ranges.CombineConsecutiveRanges();
3914 } break;
3915 case DW_AT_artificial:
3916 is_artificial = form_value.Boolean();
3917 break;
3918 case DW_AT_accessibility:
3919 break; // accessibility =
3920 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3921 case DW_AT_declaration:
3922 case DW_AT_description:
3923 case DW_AT_endianity:
3924 case DW_AT_segment:
3925 case DW_AT_visibility:
3926 default:
3927 case DW_AT_abstract_origin:
3928 case DW_AT_sibling:
3929 break;
3930 }
3931 }
3932 }
3933
3934 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3935 const dw_tag_t parent_tag = die.GetParent().Tag();
3936 bool is_static_member =
3937 parent_tag == DW_TAG_compile_unit &&
3938 (parent_context_die.Tag() == DW_TAG_class_type ||
3939 parent_context_die.Tag() == DW_TAG_structure_type);
3940
3941 ValueType scope = eValueTypeInvalid;
3942
3943 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3944 SymbolContextScope *symbol_context_scope = NULL;
3945
Sam McCall30621392016-11-22 11:40:25 +00003946 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003947 if (!mangled) {
3948 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3949 // DW_AT_MIPS_linkage_name) to
3950 // generate fully qualified names of global variables with commands like
3951 // "frame var j".
3952 // For example, if j were an int variable holding a value 4 and declared
3953 // in a namespace
3954 // B which in turn is contained in a namespace A, the command "frame var
3955 // j" returns
3956 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we
3957 // should be able
3958 // to generate a fully qualified name from the declaration context.
3959 if (parent_tag == DW_TAG_compile_unit &&
3960 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3961 DWARFDeclContext decl_ctx;
3962
3963 die.GetDWARFDeclContext(decl_ctx);
3964 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3965 }
3966 }
3967
Kate Stoneb9c1b512016-09-06 20:57:50 +00003968 if (tag == DW_TAG_formal_parameter)
3969 scope = eValueTypeVariableArgument;
3970 else {
Sam McCall30621392016-11-22 11:40:25 +00003971 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3972 // or static variable, so we have to do a little digging:
3973 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3974 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3975 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3976 // Clang likes to combine small global variables into the same symbol
3977 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3978 // so we need to look through the whole expression.
3979 bool is_static_lifetime =
3980 has_explicit_mangled ||
3981 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003982 // Check if the location has a DW_OP_addr with any address value...
3983 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3984 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003985 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003986 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3987 if (op_error) {
3988 StreamString strm;
3989 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3990 NULL);
3991 GetObjectFile()->GetModule()->ReportError(
3992 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003993 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003994 }
Sam McCall30621392016-11-22 11:40:25 +00003995 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3996 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003997 }
3998 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3999
Sam McCall30621392016-11-22 11:40:25 +00004000 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004001 if (is_external)
4002 scope = eValueTypeVariableGlobal;
4003 else
4004 scope = eValueTypeVariableStatic;
4005
4006 if (debug_map_symfile) {
4007 // When leaving the DWARF in the .o files on darwin,
4008 // when we have a global variable that wasn't initialized,
4009 // the .o file might not have allocated a virtual
4010 // address for the global variable. In this case it will
4011 // have created a symbol for the global variable
4012 // that is undefined/data and external and the value will
4013 // be the byte size of the variable. When we do the
4014 // address map in SymbolFileDWARFDebugMap we rely on
4015 // having an address, we need to do some magic here
4016 // so we can get the correct address for our global
4017 // variable. The address for all of these entries
4018 // will be zero, and there will be an undefined symbol
4019 // in this object file, and the executable will have
4020 // a matching symbol with a good address. So here we
4021 // dig up the correct address and replace it in the
4022 // location for the variable, and set the variable's
4023 // symbol context scope to be that of the main executable
4024 // so the file address will resolve correctly.
4025 bool linked_oso_file_addr = false;
4026 if (is_external && location_DW_OP_addr == 0) {
4027 // we have a possible uninitialized extern global
4028 ConstString const_name(mangled ? mangled : name);
4029 ObjectFile *debug_map_objfile =
4030 debug_map_symfile->GetObjectFile();
4031 if (debug_map_objfile) {
4032 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
4033 if (debug_map_symtab) {
4034 Symbol *exe_symbol =
4035 debug_map_symtab->FindFirstSymbolWithNameAndType(
4036 const_name, eSymbolTypeData, Symtab::eDebugYes,
4037 Symtab::eVisibilityExtern);
4038 if (exe_symbol) {
4039 if (exe_symbol->ValueIsAddress()) {
4040 const addr_t exe_file_addr =
4041 exe_symbol->GetAddressRef().GetFileAddress();
4042 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4043 if (location.Update_DW_OP_addr(exe_file_addr)) {
4044 linked_oso_file_addr = true;
4045 symbol_context_scope = exe_symbol;
4046 }
4047 }
4048 }
4049 }
4050 }
4051 }
4052 }
4053
4054 if (!linked_oso_file_addr) {
4055 // The DW_OP_addr is not zero, but it contains a .o file address
4056 // which
4057 // needs to be linked up correctly.
4058 const lldb::addr_t exe_file_addr =
4059 debug_map_symfile->LinkOSOFileAddress(this,
4060 location_DW_OP_addr);
4061 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4062 // Update the file address for this variable
4063 location.Update_DW_OP_addr(exe_file_addr);
4064 } else {
4065 // Variable didn't make it into the final executable
4066 return var_sp;
4067 }
4068 }
4069 }
4070 } else {
4071 if (location_is_const_value_data)
4072 scope = eValueTypeVariableStatic;
4073 else {
4074 scope = eValueTypeVariableLocal;
4075 if (debug_map_symfile) {
4076 // We need to check for TLS addresses that we need to fixup
4077 if (location.ContainsThreadLocalStorage()) {
4078 location.LinkThreadLocalStorage(
4079 debug_map_symfile->GetObjectFile()->GetModule(),
4080 [this, debug_map_symfile](
4081 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4082 return debug_map_symfile->LinkOSOFileAddress(
4083 this, unlinked_file_addr);
4084 });
4085 scope = eValueTypeVariableThreadLocal;
4086 }
4087 }
4088 }
4089 }
4090 }
4091
4092 if (symbol_context_scope == NULL) {
4093 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004094 case DW_TAG_subprogram:
4095 case DW_TAG_inlined_subroutine:
4096 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004097 if (sc.function) {
4098 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4099 sc_parent_die.GetID());
4100 if (symbol_context_scope == NULL)
4101 symbol_context_scope = sc.function;
4102 }
4103 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004104
Kate Stoneb9c1b512016-09-06 20:57:50 +00004105 default:
4106 symbol_context_scope = sc.comp_unit;
4107 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004109 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004110
Kate Stoneb9c1b512016-09-06 20:57:50 +00004111 if (symbol_context_scope) {
4112 SymbolFileTypeSP type_sp(
4113 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4114
4115 if (const_value.Form() && type_sp && type_sp->GetType())
4116 location.CopyOpcodeData(const_value.Unsigned(),
4117 type_sp->GetType()->GetByteSize(),
4118 die.GetCU()->GetAddressByteSize());
4119
4120 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4121 symbol_context_scope, scope_ranges, &decl,
4122 location, is_external, is_artificial,
4123 is_static_member));
4124
4125 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4126 } else {
4127 // Not ready to parse this variable yet. It might be a global
4128 // or static variable that is in a function scope and the function
4129 // in the symbol context wasn't filled in yet
4130 return var_sp;
4131 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004133 // Cache var_sp even if NULL (the variable was just a specification or
4134 // was missing vital information to be able to be displayed in the debugger
4135 // (missing location due to optimization, etc)) so we don't re-parse
4136 // this DIE over and over later...
4137 GetDIEToVariable()[die.GetDIE()] = var_sp;
4138 if (spec_die)
4139 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4140 }
4141 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004142}
4143
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144DWARFDIE
4145SymbolFileDWARF::FindBlockContainingSpecification(
4146 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4147 // Give the concrete function die specified by "func_die_offset", find the
4148 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4149 // to "spec_block_die_offset"
4150 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4151 spec_block_die_offset);
4152}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004153
Kate Stoneb9c1b512016-09-06 20:57:50 +00004154DWARFDIE
4155SymbolFileDWARF::FindBlockContainingSpecification(
4156 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4157 if (die) {
4158 switch (die.Tag()) {
4159 case DW_TAG_subprogram:
4160 case DW_TAG_inlined_subroutine:
4161 case DW_TAG_lexical_block: {
4162 if (die.GetAttributeValueAsReference(
4163 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4164 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004165
Kate Stoneb9c1b512016-09-06 20:57:50 +00004166 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4167 DW_INVALID_OFFSET) ==
4168 spec_block_die_offset)
4169 return die;
4170 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004172
4173 // Give the concrete function die specified by "func_die_offset", find the
4174 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4175 // to "spec_block_die_offset"
4176 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4177 child_die = child_die.GetSibling()) {
4178 DWARFDIE result_die =
4179 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4180 if (result_die)
4181 return result_die;
4182 }
4183 }
4184
4185 return DWARFDIE();
4186}
4187
4188size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4189 const DWARFDIE &orig_die,
4190 const lldb::addr_t func_low_pc,
4191 bool parse_siblings, bool parse_children,
4192 VariableList *cc_variable_list) {
4193 if (!orig_die)
4194 return 0;
4195
4196 VariableListSP variable_list_sp;
4197
4198 size_t vars_added = 0;
4199 DWARFDIE die = orig_die;
4200 while (die) {
4201 dw_tag_t tag = die.Tag();
4202
4203 // Check to see if we have already parsed this variable or constant?
4204 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4205 if (var_sp) {
4206 if (cc_variable_list)
4207 cc_variable_list->AddVariableIfUnique(var_sp);
4208 } else {
4209 // We haven't already parsed it, lets do that now.
4210 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4211 (tag == DW_TAG_formal_parameter && sc.function)) {
4212 if (variable_list_sp.get() == NULL) {
4213 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4214 dw_tag_t parent_tag = sc_parent_die.Tag();
4215 switch (parent_tag) {
4216 case DW_TAG_compile_unit:
4217 if (sc.comp_unit != NULL) {
4218 variable_list_sp = sc.comp_unit->GetVariableList(false);
4219 if (variable_list_sp.get() == NULL) {
4220 variable_list_sp.reset(new VariableList());
4221 sc.comp_unit->SetVariableList(variable_list_sp);
4222 }
4223 } else {
4224 GetObjectFile()->GetModule()->ReportError(
4225 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4226 "symbol context for 0x%8.8" PRIx64
4227 " %s.\n",
4228 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4229 orig_die.GetID(), orig_die.GetTagAsCString());
4230 }
4231 break;
4232
4233 case DW_TAG_subprogram:
4234 case DW_TAG_inlined_subroutine:
4235 case DW_TAG_lexical_block:
4236 if (sc.function != NULL) {
4237 // Check to see if we already have parsed the variables for the
4238 // given scope
4239
4240 Block *block = sc.function->GetBlock(true).FindBlockByID(
4241 sc_parent_die.GetID());
4242 if (block == NULL) {
4243 // This must be a specification or abstract origin with
4244 // a concrete block counterpart in the current function. We need
4245 // to find the concrete block so we can correctly add the
4246 // variable to it
4247 const DWARFDIE concrete_block_die =
4248 FindBlockContainingSpecification(
4249 DIERef(sc.function->GetID(), this),
4250 sc_parent_die.GetOffset());
4251 if (concrete_block_die)
4252 block = sc.function->GetBlock(true).FindBlockByID(
4253 concrete_block_die.GetID());
4254 }
4255
4256 if (block != NULL) {
4257 const bool can_create = false;
4258 variable_list_sp = block->GetBlockVariableList(can_create);
4259 if (variable_list_sp.get() == NULL) {
4260 variable_list_sp.reset(new VariableList());
4261 block->SetVariableList(variable_list_sp);
4262 }
4263 }
4264 }
4265 break;
4266
4267 default:
4268 GetObjectFile()->GetModule()->ReportError(
4269 "didn't find appropriate parent DIE for variable list for "
4270 "0x%8.8" PRIx64 " %s.\n",
4271 orig_die.GetID(), orig_die.GetTagAsCString());
4272 break;
4273 }
4274 }
4275
4276 if (variable_list_sp) {
4277 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4278 if (var_sp) {
4279 variable_list_sp->AddVariableIfUnique(var_sp);
4280 if (cc_variable_list)
4281 cc_variable_list->AddVariableIfUnique(var_sp);
4282 ++vars_added;
4283 }
4284 }
4285 }
4286 }
4287
4288 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4289
4290 if (!skip_children && parse_children && die.HasChildren()) {
4291 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4292 true, cc_variable_list);
4293 }
4294
4295 if (parse_siblings)
4296 die = die.GetSibling();
4297 else
4298 die.Clear();
4299 }
4300 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301}
4302
4303//------------------------------------------------------------------
4304// PluginInterface protocol
4305//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004306ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4307
4308uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4309
4310void SymbolFileDWARF::DumpIndexes() {
4311 StreamFile s(stdout, false);
4312
4313 s.Printf(
4314 "DWARF index for (%s) '%s':",
4315 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4316 GetObjectFile()->GetFileSpec().GetPath().c_str());
4317 s.Printf("\nFunction basenames:\n");
4318 m_function_basename_index.Dump(&s);
4319 s.Printf("\nFunction fullnames:\n");
4320 m_function_fullname_index.Dump(&s);
4321 s.Printf("\nFunction methods:\n");
4322 m_function_method_index.Dump(&s);
4323 s.Printf("\nFunction selectors:\n");
4324 m_function_selector_index.Dump(&s);
4325 s.Printf("\nObjective C class selectors:\n");
4326 m_objc_class_selectors_index.Dump(&s);
4327 s.Printf("\nGlobals and statics:\n");
4328 m_global_index.Dump(&s);
4329 s.Printf("\nTypes:\n");
4330 m_type_index.Dump(&s);
4331 s.Printf("\nNamespaces:\n");
4332 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004333}
4334
Kate Stoneb9c1b512016-09-06 20:57:50 +00004335SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4336 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4337 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4338 if (module_sp) {
4339 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4340 if (sym_vendor)
4341 m_debug_map_symfile =
4342 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004343 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004344 }
4345 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004346}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004347
4348DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004349SymbolFileDWARF::GetLocationListFormat() const {
4350 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004351}
Tamas Berghammer963ce482017-08-25 13:56:14 +00004352
4353SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
4354 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
4355 FileSpec dwp_filespec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
4356 if (dwp_filespec.Exists()) {
4357 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
4358 dwp_filespec);
4359 }
4360 });
4361 return m_dwp_symfile.get();
4362}