blob: d37837cd212a2d9b00c24f267645b83360d2f89d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
Sean Callanancc427fa2011-07-30 02:42:06 +000013#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000017#include "lldb/Core/ModuleList.h"
18#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Scalar.h"
21#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000022#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Value.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000024#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/RegularExpression.h"
26#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000027#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Sean Callanan4dbb2712015-09-25 20:35:58 +000029#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000030
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000031#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000032#include "lldb/Host/Host.h"
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +000033#include "lldb/Host/Symbols.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000034
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000035#include "lldb/Interpreter/OptionValueFileSpecList.h"
36#include "lldb/Interpreter/OptionValueProperties.h"
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000039#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000040#include "lldb/Symbol/ClangUtil.h"
41#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000042#include "lldb/Symbol/CompilerDecl.h"
43#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000044#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000045#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000048#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000049#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/VariableList.h"
51
Jim Inghamaa816b82015-09-02 01:59:14 +000052#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
53#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000054
Jim Ingham0e0984e2015-09-02 01:06:46 +000055#include "lldb/Target/Language.h"
56
Francis Ricci7ddfe8e2017-09-19 15:38:30 +000057#include "lldb/Host/TaskPool.h"
Tamas Berghammer2ff88702015-10-23 10:34:49 +000058
Greg Clayton261ac3f2015-08-28 01:01:03 +000059#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000060#include "DWARFASTParserClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFCompileUnit.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000062#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFDebugAbbrev.h"
64#include "DWARFDebugAranges.h"
65#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000067#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068#include "DWARFDebugPubnames.h"
69#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000070#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071#include "DWARFFormValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000073#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000074#include "SymbolFileDWARFDwo.h"
Tamas Berghammer963ce482017-08-25 13:56:14 +000075#include "SymbolFileDWARFDwp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Zachary Turner7d86ee52017-03-08 17:56:08 +000077#include "llvm/Support/FileSystem.h"
78
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079#include <map>
80
Matthew Gardinere81df3b2014-08-26 06:57:23 +000081#include <ctype.h>
82#include <string.h>
83
Greg Clayton62742b12010-11-11 01:09:45 +000084//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000085
86#ifdef ENABLE_DEBUG_PRINTF
87#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000088#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000089#else
90#define DEBUG_PRINTF(fmt, ...)
91#endif
92
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093using namespace lldb;
94using namespace lldb_private;
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096// static inline bool
97// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000098//{
99// switch (tag)
100// {
101// default:
102// break;
103// case DW_TAG_subprogram:
104// case DW_TAG_inlined_subroutine:
105// case DW_TAG_class_type:
106// case DW_TAG_structure_type:
107// case DW_TAG_union_type:
108// return true;
109// }
110// return false;
111//}
112//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000113
114namespace {
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116PropertyDefinition g_properties[] = {
117 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
118 nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic "
119 "links will be resolved at DWARF parse time."},
120 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122enum { ePropertySymLinkPaths };
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124class PluginProperties : public Properties {
125public:
126 static ConstString GetSettingName() {
127 return SymbolFileDWARF::GetPluginNameStatic();
128 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 PluginProperties() {
131 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
132 m_collection_sp->Initialize(g_properties);
133 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 FileSpecList &GetSymLinkPaths() {
136 OptionValueFileSpecList *option_value =
137 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
138 nullptr, true, ePropertySymLinkPaths);
139 assert(option_value);
140 return option_value->GetCurrentValue();
141 }
142};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
147 static const auto g_settings_sp(std::make_shared<PluginProperties>());
148 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
154 if (!path_from_dwarf || !path_from_dwarf[0]) {
155 return path_from_dwarf;
156 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 const char *colon_pos = strchr(path_from_dwarf, ':');
159 if (nullptr == colon_pos) {
160 return path_from_dwarf;
161 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 const char *slash_pos = strchr(path_from_dwarf, '/');
164 if (slash_pos && (slash_pos < colon_pos)) {
165 return path_from_dwarf;
166 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 // check whether we have a windows path, and so the first character
169 // is a drive-letter not a hostname.
170 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
171 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
172 return path_from_dwarf;
173 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 return colon_pos + 1;
176}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178static const char *resolveCompDir(const char *path_from_dwarf) {
179 if (!path_from_dwarf)
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000180 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181
182 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
183 // Remove the host part if present.
184 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
185 if (!local_path)
186 return nullptr;
187
188 bool is_symlink = false;
189 FileSpec local_path_spec(local_path, false);
190 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
191 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
192 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
193 local_path_spec, true);
194
195 if (!is_symlink)
196 return local_path;
197
Zachary Turner7d86ee52017-03-08 17:56:08 +0000198 namespace fs = llvm::sys::fs;
199 if (fs::get_file_type(local_path_spec.GetPath(), false) !=
200 fs::file_type::symlink_file)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 return local_path;
202
203 FileSpec resolved_local_path_spec;
204 const auto error =
205 FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
206 if (error.Success())
207 return resolved_local_path_spec.GetCString();
208
209 return nullptr;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000210}
211
Alexander Shaposhnikovf413c782017-11-17 20:50:54 +0000212DWARFCompileUnit *SymbolFileDWARF::GetBaseCompileUnit() {
213 return nullptr;
214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216void SymbolFileDWARF::Initialize() {
217 LogChannelDWARF::Initialize();
218 PluginManager::RegisterPlugin(GetPluginNameStatic(),
219 GetPluginDescriptionStatic(), CreateInstance,
220 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
224 if (!PluginManager::GetSettingForSymbolFilePlugin(
225 debugger, PluginProperties::GetSettingName())) {
226 const bool is_global_setting = true;
227 PluginManager::CreateSettingForSymbolFilePlugin(
228 debugger, GetGlobalPluginProperties()->GetValueProperties(),
229 ConstString("Properties for the dwarf symbol-file plug-in."),
230 is_global_setting);
231 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234void SymbolFileDWARF::Terminate() {
235 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000236 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
240 static ConstString g_name("dwarf");
241 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
245 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
249 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252TypeList *SymbolFileDWARF::GetTypeList() {
253 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
254 if (debug_map_symfile)
255 return debug_map_symfile->GetTypeList();
256 else
257 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000258}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
260 dw_offset_t max_die_offset, uint32_t type_mask,
261 TypeSet &type_set) {
262 if (die) {
263 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (die_offset >= max_die_offset)
266 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 if (die_offset >= min_die_offset) {
269 const dw_tag_t tag = die.Tag();
270
271 bool add_type = false;
272
273 switch (tag) {
274 case DW_TAG_array_type:
275 add_type = (type_mask & eTypeClassArray) != 0;
276 break;
277 case DW_TAG_unspecified_type:
278 case DW_TAG_base_type:
279 add_type = (type_mask & eTypeClassBuiltin) != 0;
280 break;
281 case DW_TAG_class_type:
282 add_type = (type_mask & eTypeClassClass) != 0;
283 break;
284 case DW_TAG_structure_type:
285 add_type = (type_mask & eTypeClassStruct) != 0;
286 break;
287 case DW_TAG_union_type:
288 add_type = (type_mask & eTypeClassUnion) != 0;
289 break;
290 case DW_TAG_enumeration_type:
291 add_type = (type_mask & eTypeClassEnumeration) != 0;
292 break;
293 case DW_TAG_subroutine_type:
294 case DW_TAG_subprogram:
295 case DW_TAG_inlined_subroutine:
296 add_type = (type_mask & eTypeClassFunction) != 0;
297 break;
298 case DW_TAG_pointer_type:
299 add_type = (type_mask & eTypeClassPointer) != 0;
300 break;
301 case DW_TAG_rvalue_reference_type:
302 case DW_TAG_reference_type:
303 add_type = (type_mask & eTypeClassReference) != 0;
304 break;
305 case DW_TAG_typedef:
306 add_type = (type_mask & eTypeClassTypedef) != 0;
307 break;
308 case DW_TAG_ptr_to_member_type:
309 add_type = (type_mask & eTypeClassMemberPointer) != 0;
310 break;
311 }
312
313 if (add_type) {
314 const bool assert_not_being_parsed = true;
315 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
316 if (type) {
317 if (type_set.find(type) == type_set.end())
318 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000319 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000321 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322
323 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
324 child_die = child_die.GetSibling()) {
325 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
326 }
327 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000328}
329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
331 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000332
333{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 CompileUnit *comp_unit = NULL;
337 DWARFCompileUnit *dwarf_cu = NULL;
338 if (sc_scope)
339 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 if (comp_unit) {
342 dwarf_cu = GetDWARFCompileUnit(comp_unit);
343 if (dwarf_cu == 0)
344 return 0;
345 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
346 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
347 } else {
348 DWARFDebugInfo *info = DebugInfo();
349 if (info) {
350 const size_t num_cus = info->GetNumCompileUnits();
351 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
352 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
353 if (dwarf_cu) {
354 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000355 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000357 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 }
359
360 std::set<CompilerType> compiler_type_set;
361 size_t num_types_added = 0;
362 for (Type *type : type_set) {
363 CompilerType compiler_type = type->GetForwardCompilerType();
364 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
365 compiler_type_set.insert(compiler_type);
366 type_list.Insert(type->shared_from_this());
367 ++num_types_added;
368 }
369 }
370 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000371}
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373//----------------------------------------------------------------------
374// Gets the first parent that is a lexical block, function or inlined
375// subroutine, or compile unit.
376//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000377DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
379 DWARFDIE die;
380 for (die = child_die.GetParent(); die; die = die.GetParent()) {
381 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 switch (tag) {
384 case DW_TAG_compile_unit:
385 case DW_TAG_subprogram:
386 case DW_TAG_inlined_subroutine:
387 case DW_TAG_lexical_block:
388 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 }
391 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
Davide Italiano98f00212018-01-31 15:17:47 +0000395 : SymbolFile(objfile),
396 UserID(uint64_t(DW_INVALID_OFFSET) << 32), // Used by SymbolFileDWARFDebugMap to when
397 // this class parses .o files to contain
398 // the .o file index/ID
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
400 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
401 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
402 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
403 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
404 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
405 m_apple_objc_ap(), m_function_basename_index(),
406 m_function_fullname_index(), m_function_method_index(),
407 m_function_selector_index(), m_objc_class_selectors_index(),
408 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
409 m_using_apple_tables(false), m_fetched_external_modules(false),
410 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
411 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413SymbolFileDWARF::~SymbolFileDWARF() {}
414
415static const ConstString &GetDWARFMachOSegmentName() {
416 static ConstString g_dwarf_section_name("__DWARF");
417 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418}
419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
421 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
422 if (debug_map_symfile)
423 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
424 else
425 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000426}
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
429 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
430 TypeSystem *type_system;
431 if (debug_map_symfile) {
432 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
433 } else {
434 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
435 if (type_system)
436 type_system->SetSymbolFile(this);
437 }
438 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000439}
440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441void SymbolFileDWARF::InitializeObject() {
442 ModuleSP module_sp(m_obj_file->GetModule());
443 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000444 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000445 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000449 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 }
451
452 get_apple_names_data();
453 if (m_data_apple_names.m_data.GetByteSize() > 0) {
454 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
455 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000456 if (m_apple_names_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000457 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000458 else
459 m_apple_names_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 }
461 get_apple_types_data();
462 if (m_data_apple_types.m_data.GetByteSize() > 0) {
463 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
464 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000465 if (m_apple_types_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000466 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000467 else
468 m_apple_types_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 }
470
471 get_apple_namespaces_data();
472 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
473 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
474 m_data_apple_namespaces.m_data, get_debug_str_data(),
475 ".apple_namespaces"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000476 if (m_apple_namespaces_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000477 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000478 else
479 m_apple_namespaces_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 }
481
482 get_apple_objc_data();
483 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
484 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
485 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000486 if (m_apple_objc_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000487 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000488 else
489 m_apple_objc_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 }
491}
492
493bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
494 return version == 2 || version == 3 || version == 4;
495}
496
497uint32_t SymbolFileDWARF::CalculateAbilities() {
498 uint32_t abilities = 0;
499 if (m_obj_file != NULL) {
500 const Section *section = NULL;
501 const SectionList *section_list = m_obj_file->GetSectionList();
502 if (section_list == NULL)
503 return 0;
504
Greg Claytonea5df102017-07-24 18:40:33 +0000505 // On non Apple platforms we might have .debug_types debug info that
506 // is created by using "-fdebug-types-section". LLDB currently will try
507 // to load this debug info, but it causes crashes during debugging when
508 // types are missing since it doesn't know how to parse the info in
509 // the .debug_types type units. This causes all complex debug info
510 // types to be unresolved. Because this causes LLDB to crash and since
511 // it really doesn't provide a solid debuggiung experience, we should
512 // disable trying to debug this kind of DWARF until support gets
513 // added or deprecated.
514 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
515 m_obj_file->GetModule()->ReportWarning(
516 "lldb doesn’t support .debug_types debug info");
517 return 0;
518 }
519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 uint64_t debug_abbrev_file_size = 0;
521 uint64_t debug_info_file_size = 0;
522 uint64_t debug_line_file_size = 0;
523
524 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
525
526 if (section)
527 section_list = &section->GetChildren();
528
529 section =
530 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
531 if (section != NULL) {
532 debug_info_file_size = section->GetFileSize();
533
534 section =
535 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
536 .get();
537 if (section)
538 debug_abbrev_file_size = section->GetFileSize();
539
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000540 DWARFDebugAbbrev *abbrev = DebugAbbrev();
541 if (abbrev) {
542 std::set<dw_form_t> invalid_forms;
543 abbrev->GetUnsupportedForms(invalid_forms);
544 if (!invalid_forms.empty()) {
545 StreamString error;
546 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
547 for (auto form : invalid_forms)
548 error.Printf(" %#x", form);
549 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
550 return 0;
551 }
552 }
553
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 section =
555 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
556 .get();
557 if (section)
558 debug_line_file_size = section->GetFileSize();
559 } else {
560 const char *symfile_dir_cstr =
561 m_obj_file->GetFileSpec().GetDirectory().GetCString();
562 if (symfile_dir_cstr) {
563 if (strcasestr(symfile_dir_cstr, ".dsym")) {
564 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
565 // We have a dSYM file that didn't have a any debug info.
566 // If the string table has a size of 1, then it was made from
567 // an executable with no debug info, or from an executable that
568 // was stripped.
569 section =
570 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
571 .get();
572 if (section && section->GetFileSize() == 1) {
573 m_obj_file->GetModule()->ReportWarning(
574 "empty dSYM file detected, dSYM was created with an "
575 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581
582 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
583 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
584 LocalVariables | VariableTypes;
585
586 if (debug_line_file_size > 0)
587 abilities |= LineTables;
588 }
589 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590}
591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592const DWARFDataExtractor &
593SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
594 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000595 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
596 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
597 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599}
600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
602 DWARFDataExtractor &data) {
603 ModuleSP module_sp(m_obj_file->GetModule());
604 const SectionList *section_list = module_sp->GetSectionList();
605 if (section_list) {
606 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
607 if (section_sp) {
608 // See if we memory mapped the DWARF segment?
609 if (m_dwarf_data.GetByteSize()) {
610 data.SetData(m_dwarf_data, section_sp->GetOffset(),
611 section_sp->GetFileSize());
612 } else {
613 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
614 data.Clear();
615 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618}
619
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
621 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
622 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623}
624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
626 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
627}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
630 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
631 m_data_debug_aranges);
632}
633
634const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
635 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
636}
637
638const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
639 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
640}
641
642const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
643 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
644}
645
646const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
647 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
648}
649
650const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
651 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
652}
653
654const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
655 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
656 m_data_debug_ranges);
657}
658
659const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
660 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
661}
662
663const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
664 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
665 m_data_debug_str_offsets);
666}
667
668const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
669 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
670}
671
672const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
673 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
674}
675
676const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
677 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
678 m_data_apple_namespaces);
679}
680
681const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
682 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
683}
684
685DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
686 if (m_abbr.get() == NULL) {
687 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
688 if (debug_abbrev_data.GetByteSize() > 0) {
689 m_abbr.reset(new DWARFDebugAbbrev());
690 if (m_abbr.get())
691 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 }
694 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695}
696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
698 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699}
700
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
702 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000703 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
704 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
705 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 if (get_debug_info_data().GetByteSize() > 0) {
707 m_info.reset(new DWARFDebugInfo());
708 if (m_info.get()) {
709 m_info->SetDwarfData(this);
710 }
Greg Clayton1f746072012-08-29 21:13:06 +0000711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 }
713 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714}
715
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
717 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718}
719
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720DWARFCompileUnit *
721SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
722 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000723 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724
725 DWARFDebugInfo *info = DebugInfo();
726 if (info) {
727 // Just a normal DWARF file whose user ID for the compile unit is
728 // the DWARF offset itself
729
730 DWARFCompileUnit *dwarf_cu =
731 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
732 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
733 dwarf_cu->SetUserData(comp_unit);
734 return dwarf_cu;
735 }
736 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737}
738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
740 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000741 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
742 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
743 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 if (get_debug_ranges_data().GetByteSize() > 0) {
745 m_ranges.reset(new DWARFDebugRanges());
746 if (m_ranges.get())
747 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000748 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 }
750 return m_ranges.get();
751}
752
753const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
754 return m_ranges.get();
755}
756
757lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit *dwarf_cu,
758 uint32_t cu_idx) {
759 CompUnitSP cu_sp;
760 if (dwarf_cu) {
761 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
762 if (comp_unit) {
763 // We already parsed this compile unit, had out a shared pointer to it
764 cu_sp = comp_unit->shared_from_this();
765 } else {
766 if (dwarf_cu->GetSymbolFileDWARF() != this) {
767 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
768 cu_idx);
769 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
770 // Let the debug map create the compile unit
771 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
772 dwarf_cu->SetUserData(cu_sp.get());
773 } else {
774 ModuleSP module_sp(m_obj_file->GetModule());
775 if (module_sp) {
776 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
777 if (cu_die) {
778 FileSpec cu_file_spec{cu_die.GetName(), false};
779 if (cu_file_spec) {
780 // If we have a full path to the compile unit, we don't need to
781 // resolve
782 // the file. This can be expensive e.g. when the source files are
783 // NFS mounted.
784 if (cu_file_spec.IsRelative()) {
785 const char *cu_comp_dir{
786 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
787 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
788 }
789
790 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000791 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 remapped_file))
793 cu_file_spec.SetFile(remapped_file, false);
794 }
795
796 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(
797 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
798
799 bool is_optimized = dwarf_cu->GetIsOptimized();
800 cu_sp.reset(new CompileUnit(
801 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
802 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
803 if (cu_sp) {
804 // If we just created a compile unit with an invalid file spec,
805 // try and get the
806 // first entry in the supports files from the line table as that
807 // should be the
808 // compile unit.
809 if (!cu_file_spec) {
810 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
811 if (cu_file_spec) {
812 (FileSpec &)(*cu_sp) = cu_file_spec;
813 // Also fix the invalid file spec which was copied from the
814 // compile unit.
815 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
816 }
817 }
818
819 dwarf_cu->SetUserData(cu_sp.get());
820
821 // Figure out the compile unit index if we weren't given one
822 if (cu_idx == UINT32_MAX)
823 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
824
825 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
826 cu_idx, cu_sp);
827 }
828 }
829 }
830 }
831 }
832 }
833 return cu_sp;
834}
835
836uint32_t SymbolFileDWARF::GetNumCompileUnits() {
837 DWARFDebugInfo *info = DebugInfo();
838 if (info)
839 return info->GetNumCompileUnits();
840 return 0;
841}
842
843CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
844 CompUnitSP cu_sp;
845 DWARFDebugInfo *info = DebugInfo();
846 if (info) {
847 DWARFCompileUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
848 if (dwarf_cu)
849 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
850 }
851 return cu_sp;
852}
853
854Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
855 const DWARFDIE &die) {
856 if (die.IsValid()) {
857 TypeSystem *type_system =
858 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
859
860 if (type_system) {
861 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
862 if (dwarf_ast)
863 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
864 }
865 }
866 return nullptr;
867}
868
869bool SymbolFileDWARF::FixupAddress(Address &addr) {
870 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
871 if (debug_map_symfile) {
872 return debug_map_symfile->LinkOSOAddress(addr);
873 }
874 // This is a normal DWARF file, no address fixups need to happen
875 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000876}
Greg Clayton1f746072012-08-29 21:13:06 +0000877lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
879 assert(sc.comp_unit);
880 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
881 if (dwarf_cu)
882 return dwarf_cu->GetLanguageType();
883 else
884 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000885}
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
888 assert(sc.comp_unit);
889 size_t functions_added = 0;
890 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
891 if (dwarf_cu) {
892 DWARFDIECollection function_dies;
893 const size_t num_functions =
894 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
895 size_t func_idx;
896 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
897 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
898 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
899 if (ParseCompileUnitFunction(sc, die))
900 ++functions_added;
901 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903 // FixupTypes();
904 }
905 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906}
907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
909 const SymbolContext &sc, FileSpecList &support_files) {
910 assert(sc.comp_unit);
911 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
912 if (dwarf_cu) {
913 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 if (cu_die) {
916 const char *cu_comp_dir = resolveCompDir(
917 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
918 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
919 DW_AT_stmt_list, DW_INVALID_OFFSET);
920 if (stmt_list != DW_INVALID_OFFSET) {
921 // All file indexes in DWARF are one based and a file of index zero is
922 // supposed to be the compile unit itself.
923 support_files.Append(*sc.comp_unit);
924 return DWARFDebugLine::ParseSupportFiles(
925 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
926 stmt_list, support_files);
927 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 }
930 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931}
932
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
934 const lldb_private::SymbolContext &sc) {
935 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
936 if (dwarf_cu)
937 return dwarf_cu->GetIsOptimized();
938 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000939}
940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941bool SymbolFileDWARF::ParseImportedModules(
942 const lldb_private::SymbolContext &sc,
943 std::vector<lldb_private::ConstString> &imported_modules) {
944 assert(sc.comp_unit);
945 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
946 if (dwarf_cu) {
947 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
948 sc.comp_unit->GetLanguage())) {
949 UpdateExternalModuleListIfNeeded();
950
951 if (sc.comp_unit) {
952 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
953
954 if (die) {
955 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
956 child_die = child_die.GetSibling()) {
957 if (child_die.Tag() == DW_TAG_imported_declaration) {
958 if (DWARFDIE module_die =
959 child_die.GetReferencedDIE(DW_AT_import)) {
960 if (module_die.Tag() == DW_TAG_module) {
961 if (const char *name = module_die.GetAttributeValueAsString(
962 DW_AT_name, nullptr)) {
963 ConstString const_name(name);
964 imported_modules.push_back(const_name);
965 }
Sean Callananb0300a42016-01-14 21:46:09 +0000966 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 }
Sean Callananb0300a42016-01-14 21:46:09 +0000968 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000970 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 } else {
972 for (const auto &pair : m_external_type_modules) {
973 imported_modules.push_back(pair.first);
974 }
975 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 }
978 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000979}
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981struct ParseDWARFLineTableCallbackInfo {
982 LineTable *line_table;
983 std::unique_ptr<LineSequence> sequence_ap;
984 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985};
986
987//----------------------------------------------------------------------
988// ParseStatementTableCallback
989//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990static void ParseDWARFLineTableCallback(dw_offset_t offset,
991 const DWARFDebugLine::State &state,
992 void *userData) {
993 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
994 // Just started parsing the line table
995 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
996 // Done parsing line table, nothing to do for the cleanup
997 } else {
998 ParseDWARFLineTableCallbackInfo *info =
999 (ParseDWARFLineTableCallbackInfo *)userData;
1000 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 // If this is our first time here, we need to create a
1003 // sequence container.
1004 if (!info->sequence_ap.get()) {
1005 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1006 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 line_table->AppendLineEntryToSequence(
1009 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1010 state.column, state.file, state.is_stmt, state.basic_block,
1011 state.prologue_end, state.epilogue_begin, state.end_sequence);
1012 if (state.end_sequence) {
1013 // First, put the current sequence into the line table.
1014 line_table->InsertSequence(info->sequence_ap.get());
1015 // Then, empty it to prepare for the next sequence.
1016 info->sequence_ap->Clear();
1017 }
1018 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019}
1020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1022 assert(sc.comp_unit);
1023 if (sc.comp_unit->GetLineTable() != NULL)
1024 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1027 if (dwarf_cu) {
1028 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1029 if (dwarf_cu_die) {
1030 const dw_offset_t cu_line_offset =
1031 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1032 DW_INVALID_OFFSET);
1033 if (cu_line_offset != DW_INVALID_OFFSET) {
1034 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1035 if (line_table_ap.get()) {
1036 ParseDWARFLineTableCallbackInfo info;
1037 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001038
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 /*
1040 * MIPS:
1041 * The SymbolContext may not have a valid target, thus we may not be
1042 * able
1043 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1044 * #0
1045 * for MIPS. Use ArchSpec to clear the bit #0.
1046 */
1047 ArchSpec arch;
1048 GetObjectFile()->GetArchitecture(arch);
1049 switch (arch.GetMachine()) {
1050 case llvm::Triple::mips:
1051 case llvm::Triple::mipsel:
1052 case llvm::Triple::mips64:
1053 case llvm::Triple::mips64el:
1054 info.addr_mask = ~((lldb::addr_t)1);
1055 break;
1056 default:
1057 info.addr_mask = ~((lldb::addr_t)0);
1058 break;
1059 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 lldb::offset_t offset = cu_line_offset;
1062 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1063 ParseDWARFLineTableCallback,
1064 &info);
1065 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1066 if (debug_map_symfile) {
1067 // We have an object file that has a line table with addresses
1068 // that are not linked. We need to link the line table and convert
1069 // the addresses that are relative to the .o file into addresses
1070 // for the main executable.
1071 sc.comp_unit->SetLineTable(
1072 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1073 } else {
1074 sc.comp_unit->SetLineTable(line_table_ap.release());
1075 return true;
1076 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 }
1081 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082}
1083
Siva Chandrad8335e92015-12-16 00:22:08 +00001084lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1086 auto iter = m_debug_macros_map.find(*offset);
1087 if (iter != m_debug_macros_map.end())
1088 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001089
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1091 if (debug_macro_data.GetByteSize() == 0)
1092 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1095 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001096
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 const DWARFDebugMacroHeader &header =
1098 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1099 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1100 header.OffsetIs64Bit(), offset, this,
1101 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001102
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001104}
1105
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1107 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1110 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001111 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112
1113 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1114 if (!dwarf_cu_die)
1115 return false;
1116
1117 lldb::offset_t sect_offset =
1118 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1119 if (sect_offset == DW_INVALID_OFFSET)
1120 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1121 DW_INVALID_OFFSET);
1122 if (sect_offset == DW_INVALID_OFFSET)
1123 return false;
1124
1125 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1126
1127 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001128}
1129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1131 Block *parent_block,
1132 const DWARFDIE &orig_die,
1133 addr_t subprogram_low_pc,
1134 uint32_t depth) {
1135 size_t blocks_added = 0;
1136 DWARFDIE die = orig_die;
1137 while (die) {
1138 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001139
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 switch (tag) {
1141 case DW_TAG_inlined_subroutine:
1142 case DW_TAG_subprogram:
1143 case DW_TAG_lexical_block: {
1144 Block *block = NULL;
1145 if (tag == DW_TAG_subprogram) {
1146 // Skip any DW_TAG_subprogram DIEs that are inside
1147 // of a normal or inlined functions. These will be
1148 // parsed on their own as separate entities.
1149
1150 if (depth > 0)
1151 break;
1152
1153 block = parent_block;
1154 } else {
1155 BlockSP block_sp(new Block(die.GetID()));
1156 parent_block->AddChild(block_sp);
1157 block = block_sp.get();
1158 }
1159 DWARFRangeList ranges;
1160 const char *name = NULL;
1161 const char *mangled_name = NULL;
1162
1163 int decl_file = 0;
1164 int decl_line = 0;
1165 int decl_column = 0;
1166 int call_file = 0;
1167 int call_line = 0;
1168 int call_column = 0;
1169 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1170 decl_line, decl_column, call_file, call_line,
1171 call_column, nullptr)) {
1172 if (tag == DW_TAG_subprogram) {
1173 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1174 subprogram_low_pc = ranges.GetMinRangeBase(0);
1175 } else if (tag == DW_TAG_inlined_subroutine) {
1176 // We get called here for inlined subroutines in two ways.
1177 // The first time is when we are making the Function object
1178 // for this inlined concrete instance. Since we're creating a top
1179 // level block at
1180 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1181 // need to
1182 // adjust the containing address.
1183 // The second time is when we are parsing the blocks inside the
1184 // function that contains
1185 // the inlined concrete instance. Since these will be blocks inside
1186 // the containing "real"
1187 // function the offset will be for that function.
1188 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1189 subprogram_low_pc = ranges.GetMinRangeBase(0);
1190 }
1191 }
1192
1193 const size_t num_ranges = ranges.GetSize();
1194 for (size_t i = 0; i < num_ranges; ++i) {
1195 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1196 const addr_t range_base = range.GetRangeBase();
1197 if (range_base >= subprogram_low_pc)
1198 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1199 range.GetByteSize()));
1200 else {
1201 GetObjectFile()->GetModule()->ReportError(
1202 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1203 ") which has a base that is less than the function's low PC "
1204 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1205 "start of this error message",
1206 block->GetID(), range_base, range.GetRangeEnd(),
1207 subprogram_low_pc);
1208 }
1209 }
1210 block->FinalizeRanges();
1211
1212 if (tag != DW_TAG_subprogram &&
1213 (name != NULL || mangled_name != NULL)) {
1214 std::unique_ptr<Declaration> decl_ap;
1215 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1216 decl_ap.reset(new Declaration(
1217 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1218 decl_line, decl_column));
1219
1220 std::unique_ptr<Declaration> call_ap;
1221 if (call_file != 0 || call_line != 0 || call_column != 0)
1222 call_ap.reset(new Declaration(
1223 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1224 call_line, call_column));
1225
1226 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1227 call_ap.get());
1228 }
1229
1230 ++blocks_added;
1231
1232 if (die.HasChildren()) {
1233 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1234 subprogram_low_pc, depth + 1);
1235 }
1236 }
1237 } break;
1238 default:
1239 break;
1240 }
1241
1242 // Only parse siblings of the block if we are not at depth zero. A depth
1243 // of zero indicates we are currently parsing the top level
1244 // DW_TAG_subprogram DIE
1245
1246 if (depth == 0)
1247 die.Clear();
1248 else
1249 die = die.GetSibling();
1250 }
1251 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001252}
1253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1255 if (parent_die) {
1256 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1257 die = die.GetSibling()) {
1258 dw_tag_t tag = die.Tag();
1259 bool check_virtuality = false;
1260 switch (tag) {
1261 case DW_TAG_inheritance:
1262 case DW_TAG_subprogram:
1263 check_virtuality = true;
1264 break;
1265 default:
1266 break;
1267 }
1268 if (check_virtuality) {
1269 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1270 return true;
1271 }
1272 }
1273 }
1274 return false;
1275}
1276
1277void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1278 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1279 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1280 std::vector<DWARFDIE> decl_ctx_die_list =
1281 ast_parser->GetDIEForDeclContext(decl_ctx);
1282
1283 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1284 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1285 decl = decl.GetSibling())
1286 ast_parser->GetDeclForUIDFromDWARF(decl);
1287}
1288
1289SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1290 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1291 // we must make sure we use the correct DWARF file when resolving things.
1292 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1293 // SymbolFileDWARF classes, one for each .o file. We can often end up
1294 // with references to other DWARF objects and we must be ready to receive
1295 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1296 // instance.
1297 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1298 if (debug_map)
1299 return debug_map->GetSymbolFileByOSOIndex(
1300 debug_map->GetOSOIndexFromUserID(uid));
1301 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001302}
1303
1304DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1306 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1307 // we must make sure we use the correct DWARF file when resolving things.
1308 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1309 // SymbolFileDWARF classes, one for each .o file. We can often end up
1310 // with references to other DWARF objects and we must be ready to receive
1311 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1312 // instance.
1313 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1314 if (dwarf)
1315 return dwarf->GetDIE(DIERef(uid, dwarf));
1316 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001317}
1318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1320 // Anytime we have a lldb::user_id_t, we must get the DIE by
1321 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1322 // the SymbolFileDWARF::GetDIEFromUID() for details.
1323 DWARFDIE die = GetDIEFromUID(type_uid);
1324 if (die)
1325 return die.GetDecl();
1326 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001327}
1328
Greg Clayton99558cc42015-08-24 23:46:31 +00001329CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1331 // Anytime we have a lldb::user_id_t, we must get the DIE by
1332 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1333 // the SymbolFileDWARF::GetDIEFromUID() for details.
1334 DWARFDIE die = GetDIEFromUID(type_uid);
1335 if (die)
1336 return die.GetDeclContext();
1337 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001338}
1339
Greg Clayton99558cc42015-08-24 23:46:31 +00001340CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1342 // Anytime we have a lldb::user_id_t, we must get the DIE by
1343 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1344 // the SymbolFileDWARF::GetDIEFromUID() for details.
1345 DWARFDIE die = GetDIEFromUID(type_uid);
1346 if (die)
1347 return die.GetContainingDeclContext();
1348 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349}
1350
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1352 // Anytime we have a lldb::user_id_t, we must get the DIE by
1353 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1354 // the SymbolFileDWARF::GetDIEFromUID() for details.
1355 DWARFDIE type_die = GetDIEFromUID(type_uid);
1356 if (type_die)
1357 return type_die.ResolveType();
1358 else
1359 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360}
1361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1363 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001364}
1365
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1367 bool assert_not_being_parsed) {
1368 if (die) {
1369 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1370 if (log)
1371 GetObjectFile()->GetModule()->LogMessage(
1372 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1373 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 // We might be coming in in the middle of a type tree (a class
1376 // within a class, an enum within a class), so parse any needed
1377 // parent DIEs before we get to this one...
1378 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1379 if (decl_ctx_die) {
1380 if (log) {
1381 switch (decl_ctx_die.Tag()) {
1382 case DW_TAG_structure_type:
1383 case DW_TAG_union_type:
1384 case DW_TAG_class_type: {
1385 // Get the type, which could be a forward declaration
1386 if (log)
1387 GetObjectFile()->GetModule()->LogMessage(
1388 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1389 "resolve parent forward type for 0x%8.8x",
1390 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1391 decl_ctx_die.GetOffset());
1392 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001393
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 default:
1395 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 return ResolveType(die);
1400 }
1401 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001402}
1403
Greg Clayton6beaaa62011-01-17 03:46:26 +00001404// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1405// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001406// can resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407bool SymbolFileDWARF::HasForwardDeclForClangType(
1408 const CompilerType &compiler_type) {
1409 CompilerType compiler_type_no_qualifiers =
1410 ClangUtil::RemoveFastQualifiers(compiler_type);
1411 if (GetForwardDeclClangTypeToDie().count(
1412 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1413 return true;
1414 }
1415 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 ClangASTContext *clang_type_system =
1418 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1419 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001420 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 DWARFASTParserClang *ast_parser =
1422 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1423 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001424}
1425
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1427 std::lock_guard<std::recursive_mutex> guard(
1428 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001429
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 ClangASTContext *clang_type_system =
1431 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1432 if (clang_type_system) {
1433 DWARFASTParserClang *ast_parser =
1434 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1435 if (ast_parser &&
1436 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1437 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1438 }
Jim Inghamc3549282012-01-11 02:21:12 +00001439
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440 // We have a struct/union/class/enum that needs to be fully resolved.
1441 CompilerType compiler_type_no_qualifiers =
1442 ClangUtil::RemoveFastQualifiers(compiler_type);
1443 auto die_it = GetForwardDeclClangTypeToDie().find(
1444 compiler_type_no_qualifiers.GetOpaqueQualType());
1445 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1446 // We have already resolved this type...
1447 return true;
1448 }
1449
1450 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1451 if (dwarf_die) {
1452 // Once we start resolving this type, remove it from the forward declaration
1453 // map in case anyone child members or other types require this type to get
1454 // resolved.
1455 // The type will get resolved when all of the calls to
1456 // SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1457 // are done.
1458 GetForwardDeclClangTypeToDie().erase(die_it);
1459
1460 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1461
1462 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1463 DWARF_LOG_TYPE_COMPLETION));
1464 if (log)
1465 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1466 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1467 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1468 type->GetName().AsCString());
1469 assert(compiler_type);
1470 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1471 if (dwarf_ast)
1472 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1473 }
1474 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001475}
1476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1478 bool assert_not_being_parsed,
1479 bool resolve_function_context) {
1480 if (die) {
1481 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1482
1483 if (assert_not_being_parsed) {
1484 if (type != DIE_IS_BEING_PARSED)
1485 return type;
1486
1487 GetObjectFile()->GetModule()->ReportError(
1488 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1489 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1490
1491 } else
1492 return type;
1493 }
1494 return nullptr;
1495}
1496
1497CompileUnit *
1498SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit *dwarf_cu,
1499 uint32_t cu_idx) {
1500 // Check if the symbol vendor already knows about this compile unit?
1501 if (dwarf_cu->GetUserData() == NULL) {
1502 // The symbol vendor doesn't know about this compile unit, we
1503 // need to parse and add it to the symbol vendor object.
1504 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1505 }
1506 return (CompileUnit *)dwarf_cu->GetUserData();
1507}
1508
1509size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1510 DIEArray &method_die_offsets) {
1511 method_die_offsets.clear();
1512 if (m_using_apple_tables) {
1513 if (m_apple_objc_ap.get())
1514 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1515 } else {
1516 if (!m_indexed)
1517 Index();
1518
1519 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1520 }
1521 return method_die_offsets.size();
1522}
1523
1524bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1525 sc.Clear(false);
1526
1527 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1530
1531 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1532 if (sc.function == NULL)
1533 sc.function = ParseCompileUnitFunction(sc, die);
1534
1535 if (sc.function) {
1536 sc.module_sp = sc.function->CalculateSymbolContextModule();
1537 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 }
1540
1541 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542}
1543
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1545 UpdateExternalModuleListIfNeeded();
1546 const auto &pos = m_external_type_modules.find(name);
1547 if (pos != m_external_type_modules.end())
1548 return pos->second;
1549 else
1550 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001551}
1552
Greg Clayton2f869fe2016-03-30 20:14:35 +00001553DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1555 DWARFDebugInfo *debug_info = DebugInfo();
1556 if (debug_info)
1557 return debug_info->GetDIE(die_ref);
1558 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001559 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001560}
1561
Kate Stoneb9c1b512016-09-06 20:57:50 +00001562std::unique_ptr<SymbolFileDWARFDwo>
1563SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1564 DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1565 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001566 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 // info files for modules.
1568 if (GetDebugMapSymfile())
1569 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 const char *dwo_name = cu_die.GetAttributeValueAsString(
1572 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1573 if (!dwo_name)
1574 return nullptr;
1575
Tamas Berghammer963ce482017-08-25 13:56:14 +00001576 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1577 if (dwp_symfile) {
1578 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1579 DW_AT_GNU_dwo_id, 0);
1580 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1581 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1582 if (dwo_symfile)
1583 return dwo_symfile;
1584 }
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 FileSpec dwo_file(dwo_name, true);
1587 if (dwo_file.IsRelative()) {
1588 const char *comp_dir = cu_die.GetAttributeValueAsString(
1589 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1590 if (!comp_dir)
1591 return nullptr;
1592
1593 dwo_file.SetFile(comp_dir, true);
1594 dwo_file.AppendPathComponent(dwo_name);
1595 }
1596
1597 if (!dwo_file.Exists())
1598 return nullptr;
1599
1600 const lldb::offset_t file_offset = 0;
1601 DataBufferSP dwo_file_data_sp;
1602 lldb::offset_t dwo_file_data_offset = 0;
1603 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1604 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1605 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1606 if (dwo_obj_file == nullptr)
1607 return nullptr;
1608
1609 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001610}
1611
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1613 if (m_fetched_external_modules)
1614 return;
1615 m_fetched_external_modules = true;
1616
1617 DWARFDebugInfo *debug_info = DebugInfo();
1618
1619 const uint32_t num_compile_units = GetNumCompileUnits();
1620 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1621 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1622
1623 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1624 if (die && die.HasChildren() == false) {
1625 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1626
1627 if (name) {
1628 ConstString const_name(name);
1629 if (m_external_type_modules.find(const_name) ==
1630 m_external_type_modules.end()) {
1631 ModuleSP module_sp;
1632 const char *dwo_path =
1633 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1634 if (dwo_path) {
1635 ModuleSpec dwo_module_spec;
1636 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1637 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1638 const char *comp_dir =
1639 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1640 if (comp_dir) {
1641 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1642 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1643 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001644 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 dwo_module_spec.GetArchitecture() =
1646 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001647
1648 // When LLDB loads "external" modules it looks at the
1649 // presence of DW_AT_GNU_dwo_name.
1650 // However, when the already created module
1651 // (corresponding to .dwo itself) is being processed,
1652 // it will see the presence of DW_AT_GNU_dwo_name
1653 // (which contains the name of dwo file) and
1654 // will try to call ModuleList::GetSharedModule again.
1655 // In some cases (i.e. for empty files) Clang 4.0
1656 // generates a *.dwo file which has DW_AT_GNU_dwo_name,
1657 // but no DW_AT_comp_dir. In this case the method
1658 // ModuleList::GetSharedModule will fail and
1659 // the warning will be printed. However, as one can notice
1660 // in this case we don't actually need to try to load the already
1661 // loaded module (corresponding to .dwo) so we simply skip it.
1662 if (m_obj_file->GetFileSpec()
1663 .GetFileNameExtension()
1664 .GetStringRef() == "dwo" &&
1665 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1666 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1667 continue;
1668 }
1669
Zachary Turner97206d52017-05-12 04:51:55 +00001670 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 dwo_module_spec, module_sp, NULL, NULL, NULL);
1672 if (!module_sp) {
1673 GetObjectFile()->GetModule()->ReportWarning(
1674 "0x%8.8x: unable to locate module needed for external types: "
1675 "%s\nerror: %s\nDebugging will be degraded due to missing "
1676 "types. Rebuilding your project will regenerate the needed "
1677 "module files.",
1678 die.GetOffset(),
1679 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1680 error.AsCString("unknown error"));
1681 }
1682 }
1683 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 }
1688}
1689
1690SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1691 if (!m_global_aranges_ap) {
1692 m_global_aranges_ap.reset(new GlobalVariableMap());
1693
1694 ModuleSP module_sp = GetObjectFile()->GetModule();
1695 if (module_sp) {
1696 const size_t num_cus = module_sp->GetNumCompileUnits();
1697 for (size_t i = 0; i < num_cus; ++i) {
1698 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1699 if (cu_sp) {
1700 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1701 if (globals_sp) {
1702 const size_t num_globals = globals_sp->GetSize();
1703 for (size_t g = 0; g < num_globals; ++g) {
1704 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1705 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1706 const DWARFExpression &location = var_sp->LocationExpression();
1707 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001708 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001709 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1710 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711 if (location_result.GetValueType() ==
1712 Value::eValueTypeFileAddress) {
1713 lldb::addr_t file_addr =
1714 location_result.GetScalar().ULongLong();
1715 lldb::addr_t byte_size = 1;
1716 if (var_sp->GetType())
1717 byte_size = var_sp->GetType()->GetByteSize();
1718 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1719 file_addr, byte_size, var_sp.get()));
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 }
1727 }
1728 m_global_aranges_ap->Sort();
1729 }
1730 return *m_global_aranges_ap;
1731}
1732
1733uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1734 uint32_t resolve_scope,
1735 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001736 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1737 Timer scoped_timer(func_cat,
1738 "SymbolFileDWARF::"
1739 "ResolveSymbolContext (so_addr = { "
1740 "section = %p, offset = 0x%" PRIx64
1741 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742 static_cast<void *>(so_addr.GetSection().get()),
1743 so_addr.GetOffset(), resolve_scope);
1744 uint32_t resolved = 0;
1745 if (resolve_scope &
1746 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1747 eSymbolContextLineEntry | eSymbolContextVariable)) {
1748 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1749
1750 DWARFDebugInfo *debug_info = DebugInfo();
1751 if (debug_info) {
1752 const dw_offset_t cu_offset =
1753 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1754 if (cu_offset == DW_INVALID_OFFSET) {
1755 // Global variables are not in the compile unit address ranges. The only
1756 // way to
1757 // currently find global variables is to iterate over the
1758 // .debug_pubnames or the
1759 // __apple_names table and find all items in there that point to
1760 // DW_TAG_variable
1761 // DIEs and then find the address that matches.
1762 if (resolve_scope & eSymbolContextVariable) {
1763 GlobalVariableMap &map = GetGlobalAranges();
1764 const GlobalVariableMap::Entry *entry =
1765 map.FindEntryThatContains(file_vm_addr);
1766 if (entry && entry->data) {
1767 Variable *variable = entry->data;
1768 SymbolContextScope *scc = variable->GetSymbolContextScope();
1769 if (scc) {
1770 scc->CalculateSymbolContext(&sc);
1771 sc.variable = variable;
1772 }
1773 return sc.GetResolvedMask();
1774 }
1775 }
1776 } else {
1777 uint32_t cu_idx = DW_INVALID_INDEX;
1778 DWARFCompileUnit *dwarf_cu =
1779 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1780 if (dwarf_cu) {
1781 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1782 if (sc.comp_unit) {
1783 resolved |= eSymbolContextCompUnit;
1784
1785 bool force_check_line_table = false;
1786 if (resolve_scope &
1787 (eSymbolContextFunction | eSymbolContextBlock)) {
1788 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1789 DWARFDIE block_die;
1790 if (function_die) {
1791 sc.function =
1792 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1793 if (sc.function == NULL)
1794 sc.function = ParseCompileUnitFunction(sc, function_die);
1795
1796 if (sc.function && (resolve_scope & eSymbolContextBlock))
1797 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1798 } else {
1799 // We might have had a compile unit that had discontiguous
1800 // address ranges where the gaps are symbols that don't have
1801 // any debug info. Discontiguous compile unit address ranges
1802 // should only happen when there aren't other functions from
1803 // other compile units in these gaps. This helps keep the size
1804 // of the aranges down.
1805 force_check_line_table = true;
1806 }
1807
1808 if (sc.function != NULL) {
1809 resolved |= eSymbolContextFunction;
1810
1811 if (resolve_scope & eSymbolContextBlock) {
1812 Block &block = sc.function->GetBlock(true);
1813
1814 if (block_die)
1815 sc.block = block.FindBlockByID(block_die.GetID());
1816 else
1817 sc.block = block.FindBlockByID(function_die.GetID());
1818 if (sc.block)
1819 resolved |= eSymbolContextBlock;
1820 }
1821 }
1822 }
1823
1824 if ((resolve_scope & eSymbolContextLineEntry) ||
1825 force_check_line_table) {
1826 LineTable *line_table = sc.comp_unit->GetLineTable();
1827 if (line_table != NULL) {
1828 // And address that makes it into this function should be in
1829 // terms
1830 // of this debug file if there is no debug map, or it will be an
1831 // address in the .o file which needs to be fixed up to be in
1832 // terms
1833 // of the debug map executable. Either way, calling
1834 // FixupAddress()
1835 // will work for us.
1836 Address exe_so_addr(so_addr);
1837 if (FixupAddress(exe_so_addr)) {
1838 if (line_table->FindLineEntryByAddress(exe_so_addr,
1839 sc.line_entry)) {
1840 resolved |= eSymbolContextLineEntry;
1841 }
1842 }
1843 }
1844 }
1845
1846 if (force_check_line_table &&
1847 !(resolved & eSymbolContextLineEntry)) {
1848 // We might have had a compile unit that had discontiguous
1849 // address ranges where the gaps are symbols that don't have
1850 // any debug info. Discontiguous compile unit address ranges
1851 // should only happen when there aren't other functions from
1852 // other compile units in these gaps. This helps keep the size
1853 // of the aranges down.
1854 sc.comp_unit = NULL;
1855 resolved &= ~eSymbolContextCompUnit;
1856 }
1857 } else {
1858 GetObjectFile()->GetModule()->ReportWarning(
1859 "0x%8.8x: compile unit %u failed to create a valid "
1860 "lldb_private::CompileUnit class.",
1861 cu_offset, cu_idx);
1862 }
1863 }
1864 }
1865 }
1866 }
1867 return resolved;
1868}
1869
1870uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1871 uint32_t line,
1872 bool check_inlines,
1873 uint32_t resolve_scope,
1874 SymbolContextList &sc_list) {
1875 const uint32_t prev_size = sc_list.GetSize();
1876 if (resolve_scope & eSymbolContextCompUnit) {
1877 DWARFDebugInfo *debug_info = DebugInfo();
1878 if (debug_info) {
1879 uint32_t cu_idx;
1880 DWARFCompileUnit *dwarf_cu = NULL;
1881
1882 for (cu_idx = 0;
1883 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1884 ++cu_idx) {
1885 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1886 const bool full_match = (bool)file_spec.GetDirectory();
1887 bool file_spec_matches_cu_file_spec =
1888 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1889 if (check_inlines || file_spec_matches_cu_file_spec) {
1890 SymbolContext sc(m_obj_file->GetModule());
1891 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1892 if (sc.comp_unit) {
1893 uint32_t file_idx = UINT32_MAX;
1894
1895 // If we are looking for inline functions only and we don't
1896 // find it in the support files, we are done.
1897 if (check_inlines) {
1898 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1899 1, file_spec, true);
1900 if (file_idx == UINT32_MAX)
1901 continue;
1902 }
1903
1904 if (line != 0) {
1905 LineTable *line_table = sc.comp_unit->GetLineTable();
1906
1907 if (line_table != NULL && line != 0) {
1908 // We will have already looked up the file index if
1909 // we are searching for inline entries.
1910 if (!check_inlines)
1911 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1912 1, file_spec, true);
1913
1914 if (file_idx != UINT32_MAX) {
1915 uint32_t found_line;
1916 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1917 0, file_idx, line, false, &sc.line_entry);
1918 found_line = sc.line_entry.line;
1919
1920 while (line_idx != UINT32_MAX) {
1921 sc.function = NULL;
1922 sc.block = NULL;
1923 if (resolve_scope &
1924 (eSymbolContextFunction | eSymbolContextBlock)) {
1925 const lldb::addr_t file_vm_addr =
1926 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1927 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1928 DWARFDIE function_die =
1929 dwarf_cu->LookupAddress(file_vm_addr);
1930 DWARFDIE block_die;
1931 if (function_die) {
1932 sc.function =
1933 sc.comp_unit
1934 ->FindFunctionByUID(function_die.GetID())
1935 .get();
1936 if (sc.function == NULL)
1937 sc.function =
1938 ParseCompileUnitFunction(sc, function_die);
1939
1940 if (sc.function &&
1941 (resolve_scope & eSymbolContextBlock))
1942 block_die =
1943 function_die.LookupDeepestBlock(file_vm_addr);
1944 }
1945
1946 if (sc.function != NULL) {
1947 Block &block = sc.function->GetBlock(true);
1948
1949 if (block_die)
1950 sc.block = block.FindBlockByID(block_die.GetID());
1951 else if (function_die)
1952 sc.block =
1953 block.FindBlockByID(function_die.GetID());
1954 }
1955 }
1956 }
1957
1958 sc_list.Append(sc);
1959 line_idx = line_table->FindLineEntryIndexByFileIndex(
1960 line_idx + 1, file_idx, found_line, true,
1961 &sc.line_entry);
1962 }
1963 }
1964 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1965 // only append the context if we aren't looking for inline call
1966 // sites
1967 // by file and line and if the file spec matches that of the
1968 // compile unit
1969 sc_list.Append(sc);
1970 }
1971 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1972 // only append the context if we aren't looking for inline call
1973 // sites
1974 // by file and line and if the file spec matches that of the
1975 // compile unit
1976 sc_list.Append(sc);
1977 }
1978
1979 if (!check_inlines)
1980 break;
1981 }
1982 }
1983 }
1984 }
1985 }
1986 return sc_list.GetSize() - prev_size;
1987}
1988
Jim Ingham7fca8c02017-04-28 00:51:06 +00001989void SymbolFileDWARF::PreloadSymbols() {
1990 std::lock_guard<std::recursive_mutex> guard(
1991 GetObjectFile()->GetModule()->GetMutex());
1992 Index();
1993}
1994
Kate Stoneb9c1b512016-09-06 20:57:50 +00001995void SymbolFileDWARF::Index() {
1996 if (m_indexed)
1997 return;
1998 m_indexed = true;
Pavel Labathf9d16472017-05-15 13:02:37 +00001999 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +00002001 func_cat, "SymbolFileDWARF::Index (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
2003
2004 DWARFDebugInfo *debug_info = DebugInfo();
2005 if (debug_info) {
2006 const uint32_t num_compile_units = GetNumCompileUnits();
2007 if (num_compile_units == 0)
2008 return;
2009
2010 std::vector<NameToDIE> function_basename_index(num_compile_units);
2011 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2012 std::vector<NameToDIE> function_method_index(num_compile_units);
2013 std::vector<NameToDIE> function_selector_index(num_compile_units);
2014 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2015 std::vector<NameToDIE> global_index(num_compile_units);
2016 std::vector<NameToDIE> type_index(num_compile_units);
2017 std::vector<NameToDIE> namespace_index(num_compile_units);
2018
Pavel Labath5c913e92017-05-05 11:16:59 +00002019 // std::vector<bool> might be implemented using bit test-and-set, so use
2020 // uint8_t instead.
2021 std::vector<uint8_t> clear_cu_dies(num_compile_units, false);
Zachary Turner3bc714b2017-03-02 00:05:25 +00002022 auto parser_fn = [debug_info, &function_basename_index,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 &function_fullname_index, &function_method_index,
2024 &function_selector_index, &objc_class_selectors_index,
2025 &global_index, &type_index,
Pavel Labath92f66772017-06-07 16:28:08 +00002026 &namespace_index](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002027 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2028 if (dwarf_cu) {
2029 dwarf_cu->Index(
2030 function_basename_index[cu_idx], function_fullname_index[cu_idx],
2031 function_method_index[cu_idx], function_selector_index[cu_idx],
2032 objc_class_selectors_index[cu_idx], global_index[cu_idx],
2033 type_index[cu_idx], namespace_index[cu_idx]);
2034 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002035 };
2036
Pavel Labath92f66772017-06-07 16:28:08 +00002037 auto extract_fn = [debug_info, &clear_cu_dies](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2039 if (dwarf_cu) {
2040 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the
2041 // DIEs for a compile unit have already been parsed.
Pavel Labath5c913e92017-05-05 11:16:59 +00002042 if (dwarf_cu->ExtractDIEsIfNeeded(false) > 1)
2043 clear_cu_dies[cu_idx] = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002045 };
2046
2047 // Create a task runner that extracts dies for each DWARF compile unit in a
2048 // separate thread
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 //----------------------------------------------------------------------
2050 // First figure out which compile units didn't have their DIEs already
2051 // parsed and remember this. If no DIEs were parsed prior to this index
2052 // function call, we are going to want to clear the CU dies after we
2053 // are done indexing to make sure we don't pull in all DWARF dies, but
2054 // we need to wait until all compile units have been indexed in case
2055 // a DIE in one compile unit refers to another and the indexes accesses
2056 // those DIEs.
2057 //----------------------------------------------------------------------
Pavel Labath5c913e92017-05-05 11:16:59 +00002058 TaskMapOverInt(0, num_compile_units, extract_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002059
2060 // Now create a task runner that can index each DWARF compile unit in a
2061 // separate
2062 // thread so we can index quickly.
2063
Pavel Labath5c913e92017-05-05 11:16:59 +00002064 TaskMapOverInt(0, num_compile_units, parser_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002065
Pavel Labath5c913e92017-05-05 11:16:59 +00002066 auto finalize_fn = [](NameToDIE &index, std::vector<NameToDIE> &srcs) {
2067 for (auto &src : srcs)
2068 index.Append(src);
2069 index.Finalize();
2070 };
Kate Stoneb9c1b512016-09-06 20:57:50 +00002071
Pavel Labath5c913e92017-05-05 11:16:59 +00002072 TaskPool::RunTasks(
2073 [&]() {
2074 finalize_fn(m_function_basename_index, function_basename_index);
2075 },
2076 [&]() {
2077 finalize_fn(m_function_fullname_index, function_fullname_index);
2078 },
2079 [&]() { finalize_fn(m_function_method_index, function_method_index); },
2080 [&]() {
2081 finalize_fn(m_function_selector_index, function_selector_index);
2082 },
2083 [&]() {
2084 finalize_fn(m_objc_class_selectors_index, objc_class_selectors_index);
2085 },
2086 [&]() { finalize_fn(m_global_index, global_index); },
2087 [&]() { finalize_fn(m_type_index, type_index); },
2088 [&]() { finalize_fn(m_namespace_index, namespace_index); });
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089
2090 //----------------------------------------------------------------------
2091 // Keep memory down by clearing DIEs for any compile units if indexing
2092 // caused us to load the compile unit's DIEs.
2093 //----------------------------------------------------------------------
2094 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2095 if (clear_cu_dies[cu_idx])
2096 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2097 }
2098
2099#if defined(ENABLE_DEBUG_PRINTF)
2100 StreamFile s(stdout, false);
2101 s.Printf("DWARF index for '%s':",
2102 GetObjectFile()->GetFileSpec().GetPath().c_str());
2103 s.Printf("\nFunction basenames:\n");
2104 m_function_basename_index.Dump(&s);
2105 s.Printf("\nFunction fullnames:\n");
2106 m_function_fullname_index.Dump(&s);
2107 s.Printf("\nFunction methods:\n");
2108 m_function_method_index.Dump(&s);
2109 s.Printf("\nFunction selectors:\n");
2110 m_function_selector_index.Dump(&s);
2111 s.Printf("\nObjective C class selectors:\n");
2112 m_objc_class_selectors_index.Dump(&s);
2113 s.Printf("\nGlobals and statics:\n");
2114 m_global_index.Dump(&s);
2115 s.Printf("\nTypes:\n");
2116 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002117 s.Printf("\nNamespaces:\n");
2118 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119#endif
2120 }
2121}
2122
2123bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2124 const lldb_private::CompilerDeclContext *decl_ctx) {
2125 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
2126 // Invalid namespace decl which means we aren't matching only things
2127 // in this symbol file, so return true to indicate it matches this
2128 // symbol file.
2129 return true;
2130 }
2131
2132 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2133 TypeSystem *type_system = GetTypeSystemForLanguage(
2134 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2135 if (decl_ctx_type_system == type_system)
2136 return true; // The type systems match, return true
2137
2138 // The namespace AST was valid, and it does not match...
2139 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2140
2141 if (log)
2142 GetObjectFile()->GetModule()->LogMessage(
2143 log, "Valid namespace does not match symbol file");
2144
2145 return false;
2146}
2147
2148uint32_t SymbolFileDWARF::FindGlobalVariables(
2149 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2150 bool append, uint32_t max_matches, VariableList &variables) {
2151 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2152
2153 if (log)
2154 GetObjectFile()->GetModule()->LogMessage(
2155 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2156 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2157 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2158 max_matches);
2159
2160 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2161 return 0;
2162
2163 DWARFDebugInfo *info = DebugInfo();
2164 if (info == NULL)
2165 return 0;
2166
2167 // If we aren't appending the results to this list, then clear the list
2168 if (!append)
2169 variables.Clear();
2170
2171 // Remember how many variables are in the list before we search in case
2172 // we are appending the results to a variable list.
2173 const uint32_t original_size = variables.GetSize();
2174
2175 DIEArray die_offsets;
2176
2177 if (m_using_apple_tables) {
2178 if (m_apple_names_ap.get()) {
2179 const char *name_cstr = name.GetCString();
2180 llvm::StringRef basename;
2181 llvm::StringRef context;
2182
2183 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2184 basename))
2185 basename = name_cstr;
2186
2187 m_apple_names_ap->FindByName(basename.data(), die_offsets);
2188 }
2189 } else {
2190 // Index the DWARF if we haven't already
2191 if (!m_indexed)
2192 Index();
2193
2194 m_global_index.Find(name, die_offsets);
2195 }
2196
2197 const size_t num_die_matches = die_offsets.size();
2198 if (num_die_matches) {
2199 SymbolContext sc;
2200 sc.module_sp = m_obj_file->GetModule();
2201 assert(sc.module_sp);
2202
2203 bool done = false;
2204 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2205 const DIERef &die_ref = die_offsets[i];
2206 DWARFDIE die = GetDIE(die_ref);
2207
2208 if (die) {
2209 switch (die.Tag()) {
2210 default:
2211 case DW_TAG_subprogram:
2212 case DW_TAG_inlined_subroutine:
2213 case DW_TAG_try_block:
2214 case DW_TAG_catch_block:
2215 break;
2216
2217 case DW_TAG_variable: {
2218 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2219
2220 if (parent_decl_ctx) {
2221 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2222 if (dwarf_ast) {
2223 CompilerDeclContext actual_parent_decl_ctx =
2224 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2225 if (!actual_parent_decl_ctx ||
2226 actual_parent_decl_ctx != *parent_decl_ctx)
2227 continue;
2228 }
2229 }
2230
2231 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2232 &variables);
2233
2234 if (variables.GetSize() - original_size >= max_matches)
2235 done = true;
2236 } break;
2237 }
2238 } else {
2239 if (m_using_apple_tables) {
2240 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2241 "the DWARF debug information has been modified (.apple_names "
2242 "accelerator table had bad die 0x%8.8x for '%s')\n",
2243 die_ref.die_offset, name.GetCString());
2244 }
2245 }
2246 }
2247 }
2248
2249 // Return the number of variable that were appended to the list
2250 const uint32_t num_matches = variables.GetSize() - original_size;
2251 if (log && num_matches > 0) {
2252 GetObjectFile()->GetModule()->LogMessage(
2253 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2254 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2255 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2256 max_matches, num_matches);
2257 }
2258 return num_matches;
2259}
2260
2261uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2262 bool append, uint32_t max_matches,
2263 VariableList &variables) {
2264 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2265
2266 if (log) {
2267 GetObjectFile()->GetModule()->LogMessage(
2268 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2269 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002270 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 }
2272
2273 DWARFDebugInfo *info = DebugInfo();
2274 if (info == NULL)
2275 return 0;
2276
2277 // If we aren't appending the results to this list, then clear the list
2278 if (!append)
2279 variables.Clear();
2280
2281 // Remember how many variables are in the list before we search in case
2282 // we are appending the results to a variable list.
2283 const uint32_t original_size = variables.GetSize();
2284
2285 DIEArray die_offsets;
2286
2287 if (m_using_apple_tables) {
2288 if (m_apple_names_ap.get()) {
2289 DWARFMappedHash::DIEInfoArray hash_data_array;
2290 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2291 hash_data_array))
2292 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2293 }
2294 } else {
2295 // Index the DWARF if we haven't already
2296 if (!m_indexed)
2297 Index();
2298
2299 m_global_index.Find(regex, die_offsets);
2300 }
2301
2302 SymbolContext sc;
2303 sc.module_sp = m_obj_file->GetModule();
2304 assert(sc.module_sp);
2305
2306 const size_t num_matches = die_offsets.size();
2307 if (num_matches) {
2308 for (size_t i = 0; i < num_matches; ++i) {
2309 const DIERef &die_ref = die_offsets[i];
2310 DWARFDIE die = GetDIE(die_ref);
2311
2312 if (die) {
2313 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2314
2315 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2316
2317 if (variables.GetSize() - original_size >= max_matches)
2318 break;
2319 } else {
2320 if (m_using_apple_tables) {
2321 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2322 "the DWARF debug information has been modified (.apple_names "
2323 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002324 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002325 }
2326 }
2327 }
2328 }
2329
2330 // Return the number of variable that were appended to the list
2331 return variables.GetSize() - original_size;
2332}
2333
2334bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2335 bool include_inlines,
2336 SymbolContextList &sc_list) {
2337 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2338 return ResolveFunction(die, include_inlines, sc_list);
2339}
2340
2341bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2342 bool include_inlines,
2343 SymbolContextList &sc_list) {
2344 SymbolContext sc;
2345
2346 if (!orig_die)
2347 return false;
2348
2349 // If we were passed a die that is not a function, just return false...
2350 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2351 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2352 return false;
2353
2354 DWARFDIE die = orig_die;
2355 DWARFDIE inlined_die;
2356 if (die.Tag() == DW_TAG_inlined_subroutine) {
2357 inlined_die = die;
2358
2359 while (1) {
2360 die = die.GetParent();
2361
2362 if (die) {
2363 if (die.Tag() == DW_TAG_subprogram)
2364 break;
2365 } else
2366 break;
2367 }
2368 }
2369 assert(die && die.Tag() == DW_TAG_subprogram);
2370 if (GetFunction(die, sc)) {
2371 Address addr;
2372 // Parse all blocks if needed
2373 if (inlined_die) {
2374 Block &function_block = sc.function->GetBlock(true);
2375 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2376 if (sc.block == NULL)
2377 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2378 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2379 addr.Clear();
2380 } else {
2381 sc.block = NULL;
2382 addr = sc.function->GetAddressRange().GetBaseAddress();
2383 }
2384
2385 if (addr.IsValid()) {
2386 sc_list.Append(sc);
2387 return true;
2388 }
2389 }
2390
2391 return false;
2392}
2393
2394void SymbolFileDWARF::FindFunctions(const ConstString &name,
2395 const NameToDIE &name_to_die,
2396 bool include_inlines,
2397 SymbolContextList &sc_list) {
2398 DIEArray die_offsets;
2399 if (name_to_die.Find(name, die_offsets)) {
2400 ParseFunctions(die_offsets, include_inlines, sc_list);
2401 }
2402}
2403
2404void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2405 const NameToDIE &name_to_die,
2406 bool include_inlines,
2407 SymbolContextList &sc_list) {
2408 DIEArray die_offsets;
2409 if (name_to_die.Find(regex, die_offsets)) {
2410 ParseFunctions(die_offsets, include_inlines, sc_list);
2411 }
2412}
2413
2414void SymbolFileDWARF::FindFunctions(
2415 const RegularExpression &regex,
2416 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2417 SymbolContextList &sc_list) {
2418 DIEArray die_offsets;
2419 DWARFMappedHash::DIEInfoArray hash_data_array;
2420 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2421 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2422 ParseFunctions(die_offsets, include_inlines, sc_list);
2423 }
2424}
2425
2426void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2427 bool include_inlines,
2428 SymbolContextList &sc_list) {
2429 const size_t num_matches = die_offsets.size();
2430 if (num_matches) {
2431 for (size_t i = 0; i < num_matches; ++i)
2432 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2433 }
2434}
2435
2436bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2437 const DWARFDIE &die) {
2438 // If we have no parent decl context to match this DIE matches, and if the
2439 // parent
2440 // decl context isn't valid, we aren't trying to look for any particular decl
2441 // context so any die matches.
2442 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2443 return true;
2444
2445 if (die) {
2446 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2447 if (dwarf_ast) {
2448 CompilerDeclContext actual_decl_ctx =
2449 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2450 if (actual_decl_ctx)
2451 return actual_decl_ctx == *decl_ctx;
2452 }
2453 }
2454 return false;
2455}
2456
2457uint32_t
2458SymbolFileDWARF::FindFunctions(const ConstString &name,
2459 const CompilerDeclContext *parent_decl_ctx,
2460 uint32_t name_type_mask, bool include_inlines,
2461 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002462 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2463 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 name.AsCString());
2465
2466 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002467 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2469
2470 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2471
2472 if (log) {
2473 GetObjectFile()->GetModule()->LogMessage(
2474 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2475 "name_type_mask=0x%x, append=%u, sc_list)",
2476 name.GetCString(), name_type_mask, append);
2477 }
2478
2479 // If we aren't appending the results to this list, then clear the list
2480 if (!append)
2481 sc_list.Clear();
2482
2483 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2484 return 0;
2485
2486 // If name is empty then we won't find anything.
2487 if (name.IsEmpty())
2488 return 0;
2489
2490 // Remember how many sc_list are in the list before we search in case
2491 // we are appending the results to a variable list.
2492
2493 const char *name_cstr = name.GetCString();
2494
2495 const uint32_t original_size = sc_list.GetSize();
2496
2497 DWARFDebugInfo *info = DebugInfo();
2498 if (info == NULL)
2499 return 0;
2500
2501 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2502 if (m_using_apple_tables) {
2503 if (m_apple_names_ap.get()) {
2504
2505 DIEArray die_offsets;
2506
2507 uint32_t num_matches = 0;
2508
2509 if (name_type_mask & eFunctionNameTypeFull) {
2510 // If they asked for the full name, match what they typed. At some
2511 // point we may
2512 // want to canonicalize this (strip double spaces, etc. For now, we
2513 // just add all the
2514 // dies that we find by exact match.
2515 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2516 for (uint32_t i = 0; i < num_matches; i++) {
2517 const DIERef &die_ref = die_offsets[i];
2518 DWARFDIE die = info->GetDIE(die_ref);
2519 if (die) {
2520 if (!DIEInDeclContext(parent_decl_ctx, die))
2521 continue; // The containing decl contexts don't match
2522
2523 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2524 if (ResolveFunction(die, include_inlines, sc_list))
2525 resolved_dies.insert(die.GetDIE());
2526 }
2527 } else {
2528 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2529 "the DWARF debug information has been modified (.apple_names "
2530 "accelerator table had bad die 0x%8.8x for '%s')",
2531 die_ref.die_offset, name_cstr);
2532 }
2533 }
2534 }
2535
2536 if (name_type_mask & eFunctionNameTypeSelector) {
2537 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2538 return 0; // no selectors in namespaces
2539
2540 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2541 // Now make sure these are actually ObjC methods. In this case we can
2542 // simply look up the name,
2543 // and if it is an ObjC method name, we're good.
2544
2545 for (uint32_t i = 0; i < num_matches; i++) {
2546 const DIERef &die_ref = die_offsets[i];
2547 DWARFDIE die = info->GetDIE(die_ref);
2548 if (die) {
2549 const char *die_name = die.GetName();
2550 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2551 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2552 if (ResolveFunction(die, include_inlines, sc_list))
2553 resolved_dies.insert(die.GetDIE());
2554 }
2555 }
2556 } else {
2557 GetObjectFile()->GetModule()->ReportError(
2558 "the DWARF debug information has been modified (.apple_names "
2559 "accelerator table had bad die 0x%8.8x for '%s')",
2560 die_ref.die_offset, name_cstr);
2561 }
2562 }
2563 die_offsets.clear();
2564 }
2565
2566 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2567 name_type_mask & eFunctionNameTypeBase) {
2568 // The apple_names table stores just the "base name" of C++ methods in
2569 // the table. So we have to
2570 // extract the base name, look that up, and if there is any other
2571 // information in the name we were
2572 // passed in we have to post-filter based on that.
2573
2574 // FIXME: Arrange the logic above so that we don't calculate the base
2575 // name twice:
2576 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2577
2578 for (uint32_t i = 0; i < num_matches; i++) {
2579 const DIERef &die_ref = die_offsets[i];
2580 DWARFDIE die = info->GetDIE(die_ref);
2581 if (die) {
2582 if (!DIEInDeclContext(parent_decl_ctx, die))
2583 continue; // The containing decl contexts don't match
2584
2585 // If we get to here, the die is good, and we should add it:
2586 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2587 ResolveFunction(die, include_inlines, sc_list)) {
2588 bool keep_die = true;
2589 if ((name_type_mask &
2590 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2591 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
2592 // We are looking for either basenames or methods, so we need to
2593 // trim out the ones we won't want by looking at the type
2594 SymbolContext sc;
2595 if (sc_list.GetLastContext(sc)) {
2596 if (sc.block) {
2597 // We have an inlined function
2598 } else if (sc.function) {
2599 Type *type = sc.function->GetType();
2600
2601 if (type) {
2602 CompilerDeclContext decl_ctx =
2603 GetDeclContextContainingUID(type->GetID());
2604 if (decl_ctx.IsStructUnionOrClass()) {
2605 if (name_type_mask & eFunctionNameTypeBase) {
2606 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2607 keep_die = false;
2608 }
2609 } else {
2610 if (name_type_mask & eFunctionNameTypeMethod) {
2611 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2612 keep_die = false;
2613 }
2614 }
2615 } else {
2616 GetObjectFile()->GetModule()->ReportWarning(
2617 "function at die offset 0x%8.8x had no function type",
2618 die_ref.die_offset);
2619 }
2620 }
2621 }
2622 }
2623 if (keep_die)
2624 resolved_dies.insert(die.GetDIE());
2625 }
2626 } else {
2627 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2628 "the DWARF debug information has been modified (.apple_names "
2629 "accelerator table had bad die 0x%8.8x for '%s')",
2630 die_ref.die_offset, name_cstr);
2631 }
2632 }
2633 die_offsets.clear();
2634 }
2635 }
2636 } else {
2637
2638 // Index the DWARF if we haven't already
2639 if (!m_indexed)
2640 Index();
2641
2642 if (name_type_mask & eFunctionNameTypeFull) {
2643 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2644
2645 // FIXME Temporary workaround for global/anonymous namespace
2646 // functions debugging FreeBSD and Linux binaries.
2647 // If we didn't find any functions in the global namespace try
2648 // looking in the basename index but ignore any returned
2649 // functions that have a namespace but keep functions which
2650 // have an anonymous namespace
2651 // TODO: The arch in the object file isn't correct for MSVC
2652 // binaries on windows, we should find a way to make it
2653 // correct and handle those symbols as well.
2654 if (sc_list.GetSize() == original_size) {
2655 ArchSpec arch;
2656 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002657 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002658 SymbolContextList temp_sc_list;
2659 FindFunctions(name, m_function_basename_index, include_inlines,
2660 temp_sc_list);
2661 SymbolContext sc;
2662 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2663 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2664 ConstString mangled_name =
2665 sc.GetFunctionName(Mangled::ePreferMangled);
2666 ConstString demangled_name =
2667 sc.GetFunctionName(Mangled::ePreferDemangled);
2668 // Mangled names on Linux and FreeBSD are of the form:
2669 // _ZN18function_namespace13function_nameEv.
2670 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2671 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2672 21)) {
2673 sc_list.Append(sc);
2674 }
2675 }
2676 }
2677 }
2678 }
2679 }
2680 DIEArray die_offsets;
2681 if (name_type_mask & eFunctionNameTypeBase) {
2682 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2683 for (uint32_t i = 0; i < num_base; i++) {
2684 DWARFDIE die = info->GetDIE(die_offsets[i]);
2685 if (die) {
2686 if (!DIEInDeclContext(parent_decl_ctx, die))
2687 continue; // The containing decl contexts don't match
2688
2689 // If we get to here, the die is good, and we should add it:
2690 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2691 if (ResolveFunction(die, include_inlines, sc_list))
2692 resolved_dies.insert(die.GetDIE());
2693 }
2694 }
2695 }
2696 die_offsets.clear();
2697 }
2698
2699 if (name_type_mask & eFunctionNameTypeMethod) {
2700 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2701 return 0; // no methods in namespaces
2702
2703 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2704 {
2705 for (uint32_t i = 0; i < num_base; i++) {
2706 DWARFDIE die = info->GetDIE(die_offsets[i]);
2707 if (die) {
2708 // If we get to here, the die is good, and we should add it:
2709 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2710 if (ResolveFunction(die, include_inlines, sc_list))
2711 resolved_dies.insert(die.GetDIE());
2712 }
2713 }
2714 }
2715 }
2716 die_offsets.clear();
2717 }
2718
2719 if ((name_type_mask & eFunctionNameTypeSelector) &&
2720 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2721 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2722 }
2723 }
2724
2725 // Return the number of variable that were appended to the list
2726 const uint32_t num_matches = sc_list.GetSize() - original_size;
2727
2728 if (log && num_matches > 0) {
2729 GetObjectFile()->GetModule()->LogMessage(
2730 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2731 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2732 "%u",
2733 name.GetCString(), name_type_mask, include_inlines, append,
2734 num_matches);
2735 }
2736 return num_matches;
2737}
2738
2739uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2740 bool include_inlines, bool append,
2741 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002742 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2743 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002744 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002745
2746 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2747
2748 if (log) {
2749 GetObjectFile()->GetModule()->LogMessage(
2750 log,
2751 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002752 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002753 }
2754
2755 // If we aren't appending the results to this list, then clear the list
2756 if (!append)
2757 sc_list.Clear();
2758
2759 // Remember how many sc_list are in the list before we search in case
2760 // we are appending the results to a variable list.
2761 uint32_t original_size = sc_list.GetSize();
2762
2763 if (m_using_apple_tables) {
2764 if (m_apple_names_ap.get())
2765 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2766 } else {
2767 // Index the DWARF if we haven't already
2768 if (!m_indexed)
2769 Index();
2770
2771 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2772
2773 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2774 }
2775
2776 // Return the number of variable that were appended to the list
2777 return sc_list.GetSize() - original_size;
2778}
2779
2780void SymbolFileDWARF::GetMangledNamesForFunction(
2781 const std::string &scope_qualified_name,
2782 std::vector<ConstString> &mangled_names) {
2783 DWARFDebugInfo *info = DebugInfo();
2784 uint32_t num_comp_units = 0;
2785 if (info)
2786 num_comp_units = info->GetNumCompileUnits();
2787
2788 for (uint32_t i = 0; i < num_comp_units; i++) {
2789 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2790 if (cu == nullptr)
2791 continue;
2792
2793 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2794 if (dwo)
2795 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2796 }
2797
2798 NameToOffsetMap::iterator iter =
2799 m_function_scope_qualified_name_map.find(scope_qualified_name);
2800 if (iter == m_function_scope_qualified_name_map.end())
2801 return;
2802
2803 DIERefSetSP set_sp = (*iter).second;
2804 std::set<DIERef>::iterator set_iter;
2805 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2806 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2807 mangled_names.push_back(ConstString(die.GetMangledName()));
2808 }
2809}
2810
2811uint32_t SymbolFileDWARF::FindTypes(
2812 const SymbolContext &sc, const ConstString &name,
2813 const CompilerDeclContext *parent_decl_ctx, bool append,
2814 uint32_t max_matches,
2815 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2816 TypeMap &types) {
2817 // If we aren't appending the results to this list, then clear the list
2818 if (!append)
2819 types.Clear();
2820
2821 // Make sure we haven't already searched this SymbolFile before...
2822 if (searched_symbol_files.count(this))
2823 return 0;
2824 else
2825 searched_symbol_files.insert(this);
2826
2827 DWARFDebugInfo *info = DebugInfo();
2828 if (info == NULL)
2829 return 0;
2830
2831 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2832
2833 if (log) {
2834 if (parent_decl_ctx)
2835 GetObjectFile()->GetModule()->LogMessage(
2836 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2837 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2838 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2839 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2840 else
2841 GetObjectFile()->GetModule()->LogMessage(
2842 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2843 "NULL, append=%u, max_matches=%u, type_list)",
2844 name.GetCString(), append, max_matches);
2845 }
2846
2847 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2848 return 0;
2849
2850 DIEArray die_offsets;
2851
2852 if (m_using_apple_tables) {
2853 if (m_apple_types_ap.get()) {
2854 const char *name_cstr = name.GetCString();
2855 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2856 }
2857 } else {
2858 if (!m_indexed)
2859 Index();
2860
2861 m_type_index.Find(name, die_offsets);
2862 }
2863
2864 const size_t num_die_matches = die_offsets.size();
2865
2866 if (num_die_matches) {
2867 const uint32_t initial_types_size = types.GetSize();
2868 for (size_t i = 0; i < num_die_matches; ++i) {
2869 const DIERef &die_ref = die_offsets[i];
2870 DWARFDIE die = GetDIE(die_ref);
2871
2872 if (die) {
2873 if (!DIEInDeclContext(parent_decl_ctx, die))
2874 continue; // The containing decl contexts don't match
2875
2876 Type *matching_type = ResolveType(die, true, true);
2877 if (matching_type) {
2878 // We found a type pointer, now find the shared pointer form our type
2879 // list
2880 types.InsertUnique(matching_type->shared_from_this());
2881 if (types.GetSize() >= max_matches)
2882 break;
2883 }
2884 } else {
2885 if (m_using_apple_tables) {
2886 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2887 "the DWARF debug information has been modified (.apple_types "
2888 "accelerator table had bad die 0x%8.8x for '%s')\n",
2889 die_ref.die_offset, name.GetCString());
2890 }
2891 }
2892 }
2893 const uint32_t num_matches = types.GetSize() - initial_types_size;
2894 if (log && num_matches) {
2895 if (parent_decl_ctx) {
2896 GetObjectFile()->GetModule()->LogMessage(
2897 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2898 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2899 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2900 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2901 num_matches);
2902 } else {
2903 GetObjectFile()->GetModule()->LogMessage(
2904 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2905 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2906 name.GetCString(), append, max_matches, num_matches);
2907 }
2908 }
2909 return num_matches;
2910 } else {
2911 UpdateExternalModuleListIfNeeded();
2912
2913 for (const auto &pair : m_external_type_modules) {
2914 ModuleSP external_module_sp = pair.second;
2915 if (external_module_sp) {
2916 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2917 if (sym_vendor) {
2918 const uint32_t num_external_matches =
2919 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2920 max_matches, searched_symbol_files, types);
2921 if (num_external_matches)
2922 return num_external_matches;
2923 }
2924 }
2925 }
2926 }
2927
2928 return 0;
2929}
2930
2931size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2932 bool append, TypeMap &types) {
2933 if (!append)
2934 types.Clear();
2935
2936 if (context.empty())
2937 return 0;
2938
2939 DIEArray die_offsets;
2940
2941 ConstString name = context.back().name;
2942
2943 if (!name)
2944 return 0;
2945
2946 if (m_using_apple_tables) {
2947 if (m_apple_types_ap.get()) {
2948 const char *name_cstr = name.GetCString();
2949 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2950 }
2951 } else {
2952 if (!m_indexed)
2953 Index();
2954
2955 m_type_index.Find(name, die_offsets);
2956 }
2957
2958 const size_t num_die_matches = die_offsets.size();
2959
2960 if (num_die_matches) {
2961 size_t num_matches = 0;
2962 for (size_t i = 0; i < num_die_matches; ++i) {
2963 const DIERef &die_ref = die_offsets[i];
2964 DWARFDIE die = GetDIE(die_ref);
2965
2966 if (die) {
2967 std::vector<CompilerContext> die_context;
2968 die.GetDWOContext(die_context);
2969 if (die_context != context)
2970 continue;
2971
2972 Type *matching_type = ResolveType(die, true, true);
2973 if (matching_type) {
2974 // We found a type pointer, now find the shared pointer form our type
2975 // list
2976 types.InsertUnique(matching_type->shared_from_this());
2977 ++num_matches;
2978 }
2979 } else {
2980 if (m_using_apple_tables) {
2981 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2982 "the DWARF debug information has been modified (.apple_types "
2983 "accelerator table had bad die 0x%8.8x for '%s')\n",
2984 die_ref.die_offset, name.GetCString());
2985 }
2986 }
2987 }
2988 return num_matches;
2989 }
2990 return 0;
2991}
2992
2993CompilerDeclContext
2994SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2995 const CompilerDeclContext *parent_decl_ctx) {
2996 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2997
2998 if (log) {
2999 GetObjectFile()->GetModule()->LogMessage(
3000 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3001 name.GetCString());
3002 }
3003
3004 CompilerDeclContext namespace_decl_ctx;
3005
3006 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
3007 return namespace_decl_ctx;
3008
3009 DWARFDebugInfo *info = DebugInfo();
3010 if (info) {
3011 DIEArray die_offsets;
3012
3013 // Index if we already haven't to make sure the compile units
3014 // get indexed and make their global DIE index list
3015 if (m_using_apple_tables) {
3016 if (m_apple_namespaces_ap.get()) {
3017 const char *name_cstr = name.GetCString();
3018 m_apple_namespaces_ap->FindByName(name_cstr, die_offsets);
3019 }
3020 } else {
3021 if (!m_indexed)
3022 Index();
3023
3024 m_namespace_index.Find(name, die_offsets);
3025 }
3026
3027 const size_t num_matches = die_offsets.size();
3028 if (num_matches) {
3029 for (size_t i = 0; i < num_matches; ++i) {
3030 const DIERef &die_ref = die_offsets[i];
3031 DWARFDIE die = GetDIE(die_ref);
3032
3033 if (die) {
3034 if (!DIEInDeclContext(parent_decl_ctx, die))
3035 continue; // The containing decl contexts don't match
3036
3037 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3038 if (dwarf_ast) {
3039 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
3040 if (namespace_decl_ctx)
3041 break;
3042 }
3043 } else {
3044 if (m_using_apple_tables) {
3045 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3046 "the DWARF debug information has been modified "
3047 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
3048 "'%s')\n",
3049 die_ref.die_offset, name.GetCString());
3050 }
3051 }
3052 }
3053 }
3054 }
3055 if (log && namespace_decl_ctx) {
3056 GetObjectFile()->GetModule()->LogMessage(
3057 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
3058 "CompilerDeclContext(%p/%p) \"%s\"",
3059 name.GetCString(),
3060 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
3061 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
3062 namespace_decl_ctx.GetName().AsCString("<NULL>"));
3063 }
3064
3065 return namespace_decl_ctx;
3066}
3067
3068TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3069 bool resolve_function_context) {
3070 TypeSP type_sp;
3071 if (die) {
3072 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3073 if (type_ptr == NULL) {
3074 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3075 assert(lldb_cu);
3076 SymbolContext sc(lldb_cu);
3077 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3078 while (parent_die != nullptr) {
3079 if (parent_die->Tag() == DW_TAG_subprogram)
3080 break;
3081 parent_die = parent_die->GetParent();
3082 }
3083 SymbolContext sc_backup = sc;
3084 if (resolve_function_context && parent_die != nullptr &&
3085 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3086 sc = sc_backup;
3087
3088 type_sp = ParseType(sc, die, NULL);
3089 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3090 // Grab the existing type from the master types lists
3091 type_sp = type_ptr->shared_from_this();
3092 }
3093 }
3094 return type_sp;
3095}
3096
3097DWARFDIE
3098SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3099 if (orig_die) {
3100 DWARFDIE die = orig_die;
3101
3102 while (die) {
3103 // If this is the original DIE that we are searching for a declaration
3104 // for, then don't look in the cache as we don't want our own decl
3105 // context to be our decl context...
3106 if (orig_die != die) {
3107 switch (die.Tag()) {
3108 case DW_TAG_compile_unit:
3109 case DW_TAG_namespace:
3110 case DW_TAG_structure_type:
3111 case DW_TAG_union_type:
3112 case DW_TAG_class_type:
3113 case DW_TAG_lexical_block:
3114 case DW_TAG_subprogram:
3115 return die;
Sean Callananb4945782017-04-24 22:11:10 +00003116 case DW_TAG_inlined_subroutine: {
3117 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3118 if (abs_die) {
3119 return abs_die;
3120 }
3121 break;
3122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003123 default:
3124 break;
3125 }
3126 }
3127
3128 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3129 if (spec_die) {
3130 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3131 if (decl_ctx_die)
3132 return decl_ctx_die;
3133 }
3134
3135 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3136 if (abs_die) {
3137 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3138 if (decl_ctx_die)
3139 return decl_ctx_die;
3140 }
3141
3142 die = die.GetParent();
3143 }
3144 }
3145 return DWARFDIE();
3146}
3147
3148Symbol *
3149SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3150 Symbol *objc_class_symbol = NULL;
3151 if (m_obj_file) {
3152 Symtab *symtab = m_obj_file->GetSymtab();
3153 if (symtab) {
3154 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3155 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3156 Symtab::eVisibilityAny);
3157 }
3158 }
3159 return objc_class_symbol;
3160}
3161
3162// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
3163// they don't
3164// then we can end up looking through all class types for a complete type and
3165// never find
3166// the full definition. We need to know if this attribute is supported, so we
3167// determine
3168// this here and cache th result. We also need to worry about the debug map
3169// DWARF file
3170// if we are doing darwin DWARF in .o file debugging.
3171bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
3172 DWARFCompileUnit *cu) {
3173 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3174 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3175 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3176 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3177 else {
3178 DWARFDebugInfo *debug_info = DebugInfo();
3179 const uint32_t num_compile_units = GetNumCompileUnits();
3180 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
3181 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3182 if (dwarf_cu != cu &&
3183 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3184 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3185 break;
3186 }
3187 }
3188 }
3189 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3190 GetDebugMapSymfile())
3191 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3192 }
3193 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003194}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003195
3196// This function can be used when a DIE is found that is a forward declaration
3197// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003198TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3199 const DWARFDIE &die, const ConstString &type_name,
3200 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003201
Kate Stoneb9c1b512016-09-06 20:57:50 +00003202 TypeSP type_sp;
3203
3204 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3205 return type_sp;
3206
3207 DIEArray die_offsets;
3208
3209 if (m_using_apple_tables) {
3210 if (m_apple_types_ap.get()) {
3211 const char *name_cstr = type_name.GetCString();
3212 m_apple_types_ap->FindCompleteObjCClassByName(name_cstr, die_offsets,
3213 must_be_implementation);
3214 }
3215 } else {
3216 if (!m_indexed)
3217 Index();
3218
3219 m_type_index.Find(type_name, die_offsets);
3220 }
3221
3222 const size_t num_matches = die_offsets.size();
3223
3224 if (num_matches) {
3225 for (size_t i = 0; i < num_matches; ++i) {
3226 const DIERef &die_ref = die_offsets[i];
3227 DWARFDIE type_die = GetDIE(die_ref);
3228
3229 if (type_die) {
3230 bool try_resolving_type = false;
3231
3232 // Don't try and resolve the DIE we are looking for with the DIE itself!
3233 if (type_die != die) {
3234 switch (type_die.Tag()) {
3235 case DW_TAG_class_type:
3236 case DW_TAG_structure_type:
3237 try_resolving_type = true;
3238 break;
3239 default:
3240 break;
3241 }
3242 }
3243
3244 if (try_resolving_type) {
3245 if (must_be_implementation &&
3246 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3247 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3248 DW_AT_APPLE_objc_complete_type, 0);
3249
3250 if (try_resolving_type) {
3251 Type *resolved_type = ResolveType(type_die, false, true);
3252 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3253 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3254 " (cu 0x%8.8" PRIx64 ")\n",
3255 die.GetID(),
3256 m_obj_file->GetFileSpec().GetFilename().AsCString(
3257 "<Unknown>"),
3258 type_die.GetID(), type_cu->GetID());
3259
3260 if (die)
3261 GetDIEToType()[die.GetDIE()] = resolved_type;
3262 type_sp = resolved_type->shared_from_this();
3263 break;
3264 }
3265 }
3266 }
3267 } else {
3268 if (m_using_apple_tables) {
3269 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3270 "the DWARF debug information has been modified (.apple_types "
3271 "accelerator table had bad die 0x%8.8x for '%s')\n",
3272 die_ref.die_offset, type_name.GetCString());
3273 }
3274 }
3275 }
3276 }
3277 return type_sp;
3278}
Greg Claytona8022fa2012-04-24 21:22:41 +00003279
Greg Clayton80c26302012-02-05 06:12:47 +00003280//----------------------------------------------------------------------
3281// This function helps to ensure that the declaration contexts match for
Kate Stoneb9c1b512016-09-06 20:57:50 +00003282// two different DIEs. Often times debug information will refer to a
Greg Clayton80c26302012-02-05 06:12:47 +00003283// forward declaration of a type (the equivalent of "struct my_struct;".
3284// There will often be a declaration of that type elsewhere that has the
3285// full definition. When we go looking for the full type "my_struct", we
3286// will find one or more matches in the accelerator tables and we will
Kate Stoneb9c1b512016-09-06 20:57:50 +00003287// then need to make sure the type was in the same declaration context
Greg Clayton80c26302012-02-05 06:12:47 +00003288// as the original DIE. This function can efficiently compare two DIEs
3289// and will return true when the declaration context matches, and false
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290// when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003291//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003292bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3293 const DWARFDIE &die2) {
3294 if (die1 == die2)
3295 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003296
Kate Stoneb9c1b512016-09-06 20:57:50 +00003297 DWARFDIECollection decl_ctx_1;
3298 DWARFDIECollection decl_ctx_2;
3299 // The declaration DIE stack is a stack of the declaration context
3300 // DIEs all the way back to the compile unit. If a type "T" is
3301 // declared inside a class "B", and class "B" is declared inside
3302 // a class "A" and class "A" is in a namespace "lldb", and the
3303 // namespace is in a compile unit, there will be a stack of DIEs:
3304 //
3305 // [0] DW_TAG_class_type for "B"
3306 // [1] DW_TAG_class_type for "A"
3307 // [2] DW_TAG_namespace for "lldb"
3308 // [3] DW_TAG_compile_unit for the source file.
3309 //
3310 // We grab both contexts and make sure that everything matches
3311 // all the way back to the compiler unit.
3312
3313 // First lets grab the decl contexts for both DIEs
3314 die1.GetDeclContextDIEs(decl_ctx_1);
3315 die2.GetDeclContextDIEs(decl_ctx_2);
3316 // Make sure the context arrays have the same size, otherwise
3317 // we are done
3318 const size_t count1 = decl_ctx_1.Size();
3319 const size_t count2 = decl_ctx_2.Size();
3320 if (count1 != count2)
3321 return false;
3322
3323 // Make sure the DW_TAG values match all the way back up the
3324 // compile unit. If they don't, then we are done.
3325 DWARFDIE decl_ctx_die1;
3326 DWARFDIE decl_ctx_die2;
3327 size_t i;
3328 for (i = 0; i < count1; i++) {
3329 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3330 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3331 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3332 return false;
3333 }
Greg Clayton890ff562012-02-02 05:48:16 +00003334#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003335
Kate Stoneb9c1b512016-09-06 20:57:50 +00003336 // Make sure the top item in the decl context die array is always
3337 // DW_TAG_compile_unit. If it isn't then something went wrong in
3338 // the DWARFDIE::GetDeclContextDIEs() function...
3339 assert(decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003340
Greg Clayton890ff562012-02-02 05:48:16 +00003341#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00003342 // Always skip the compile unit when comparing by only iterating up to
3343 // "count - 1". Here we compare the names as we go.
3344 for (i = 0; i < count1 - 1; i++) {
3345 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3346 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3347 const char *name1 = decl_ctx_die1.GetName();
3348 const char *name2 = decl_ctx_die2.GetName();
3349 // If the string was from a DW_FORM_strp, then the pointer will often
3350 // be the same!
3351 if (name1 == name2)
3352 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003353
Kate Stoneb9c1b512016-09-06 20:57:50 +00003354 // Name pointers are not equal, so only compare the strings
3355 // if both are not NULL.
3356 if (name1 && name2) {
3357 // If the strings don't compare, we are done...
3358 if (strcmp(name1, name2) != 0)
3359 return false;
3360 } else {
3361 // One name was NULL while the other wasn't
3362 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003363 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003364 }
3365 // We made it through all of the checks and the declaration contexts
3366 // are equal.
3367 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003368}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003369
Kate Stoneb9c1b512016-09-06 20:57:50 +00003370TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3371 const DWARFDeclContext &dwarf_decl_ctx) {
3372 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003373
Kate Stoneb9c1b512016-09-06 20:57:50 +00003374 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3375 if (dwarf_decl_ctx_count > 0) {
3376 const ConstString type_name(dwarf_decl_ctx[0].name);
3377 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003378
Kate Stoneb9c1b512016-09-06 20:57:50 +00003379 if (type_name) {
3380 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3381 DWARF_LOG_LOOKUPS));
3382 if (log) {
3383 GetObjectFile()->GetModule()->LogMessage(
3384 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3385 "s, qualified-name='%s')",
3386 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3387 dwarf_decl_ctx.GetQualifiedName());
3388 }
3389
3390 DIEArray die_offsets;
3391
3392 if (m_using_apple_tables) {
3393 if (m_apple_types_ap.get()) {
3394 const bool has_tag =
3395 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3396 DWARFMappedHash::eAtomTypeTag);
3397 const bool has_qualified_name_hash =
3398 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3399 DWARFMappedHash::eAtomTypeQualNameHash);
3400 if (has_tag && has_qualified_name_hash) {
3401 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3402 const uint32_t qualified_name_hash =
3403 MappedHash::HashStringUsingDJB(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003404 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003405 GetObjectFile()->GetModule()->LogMessage(
3406 log, "FindByNameAndTagAndQualifiedNameHash()");
3407 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
3408 type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3409 } else if (has_tag) {
3410 if (log)
3411 GetObjectFile()->GetModule()->LogMessage(log,
3412 "FindByNameAndTag()");
3413 m_apple_types_ap->FindByNameAndTag(type_name.GetCString(), tag,
3414 die_offsets);
3415 } else {
3416 m_apple_types_ap->FindByName(type_name.GetCString(), die_offsets);
3417 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003419 } else {
3420 if (!m_indexed)
3421 Index();
3422
3423 m_type_index.Find(type_name, die_offsets);
3424 }
3425
3426 const size_t num_matches = die_offsets.size();
3427
3428 // Get the type system that we are looking to find a type for. We will use
3429 // this
3430 // to ensure any matches we find are in a language that this type system
3431 // supports
3432 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3433 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3434 ? nullptr
3435 : GetTypeSystemForLanguage(language);
3436
3437 if (num_matches) {
3438 for (size_t i = 0; i < num_matches; ++i) {
3439 const DIERef &die_ref = die_offsets[i];
3440 DWARFDIE type_die = GetDIE(die_ref);
3441
3442 if (type_die) {
3443 // Make sure type_die's langauge matches the type system we are
3444 // looking for.
3445 // We don't want to find a "Foo" type from Java if we are looking
3446 // for a "Foo"
3447 // type for C, C++, ObjC, or ObjC++.
3448 if (type_system &&
3449 !type_system->SupportsLanguage(type_die.GetLanguage()))
3450 continue;
3451 bool try_resolving_type = false;
3452
3453 // Don't try and resolve the DIE we are looking for with the DIE
3454 // itself!
3455 const dw_tag_t type_tag = type_die.Tag();
3456 // Make sure the tags match
3457 if (type_tag == tag) {
3458 // The tags match, lets try resolving this type
3459 try_resolving_type = true;
3460 } else {
3461 // The tags don't match, but we need to watch our for a
3462 // forward declaration for a struct and ("struct foo")
3463 // ends up being a class ("class foo { ... };") or
3464 // vice versa.
3465 switch (type_tag) {
3466 case DW_TAG_class_type:
3467 // We had a "class foo", see if we ended up with a "struct foo {
3468 // ... };"
3469 try_resolving_type = (tag == DW_TAG_structure_type);
3470 break;
3471 case DW_TAG_structure_type:
3472 // We had a "struct foo", see if we ended up with a "class foo {
3473 // ... };"
3474 try_resolving_type = (tag == DW_TAG_class_type);
3475 break;
3476 default:
3477 // Tags don't match, don't event try to resolve
3478 // using this type whose name matches....
3479 break;
3480 }
3481 }
3482
3483 if (try_resolving_type) {
3484 DWARFDeclContext type_dwarf_decl_ctx;
3485 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3486
3487 if (log) {
3488 GetObjectFile()->GetModule()->LogMessage(
3489 log, "SymbolFileDWARF::"
3490 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3491 "qualified-name='%s') trying die=0x%8.8x (%s)",
3492 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3493 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3494 type_dwarf_decl_ctx.GetQualifiedName());
3495 }
3496
3497 // Make sure the decl contexts match all the way up
3498 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3499 Type *resolved_type = ResolveType(type_die, false);
3500 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3501 type_sp = resolved_type->shared_from_this();
3502 break;
3503 }
3504 }
3505 } else {
3506 if (log) {
3507 std::string qualified_name;
3508 type_die.GetQualifiedName(qualified_name);
3509 GetObjectFile()->GetModule()->LogMessage(
3510 log, "SymbolFileDWARF::"
3511 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3512 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3513 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3514 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3515 qualified_name.c_str());
3516 }
3517 }
3518 } else {
3519 if (m_using_apple_tables) {
3520 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3521 "the DWARF debug information has been modified (.apple_types "
3522 "accelerator table had bad die 0x%8.8x for '%s')\n",
3523 die_ref.die_offset, type_name.GetCString());
3524 }
3525 }
3526 }
3527 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003529 }
3530 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003531}
3532
Kate Stoneb9c1b512016-09-06 20:57:50 +00003533TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3534 bool *type_is_new_ptr) {
3535 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003536
Kate Stoneb9c1b512016-09-06 20:57:50 +00003537 if (die) {
3538 TypeSystem *type_system =
3539 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003540
Kate Stoneb9c1b512016-09-06 20:57:50 +00003541 if (type_system) {
3542 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3543 if (dwarf_ast) {
3544 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3545 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3546 if (type_sp) {
3547 TypeList *type_list = GetTypeList();
3548 if (type_list)
3549 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003550
Kate Stoneb9c1b512016-09-06 20:57:50 +00003551 if (die.Tag() == DW_TAG_subprogram) {
3552 DIERef die_ref = die.GetDIERef();
3553 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3554 .GetScopeQualifiedName()
3555 .AsCString(""));
3556 if (scope_qualified_name.size()) {
3557 NameToOffsetMap::iterator iter =
3558 m_function_scope_qualified_name_map.find(
3559 scope_qualified_name);
3560 if (iter != m_function_scope_qualified_name_map.end())
3561 (*iter).second->insert(die_ref);
3562 else {
3563 DIERefSetSP new_set(new std::set<DIERef>);
3564 new_set->insert(die_ref);
3565 m_function_scope_qualified_name_map.emplace(
3566 std::make_pair(scope_qualified_name, new_set));
3567 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003571 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003573 }
3574
3575 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003576}
3577
Kate Stoneb9c1b512016-09-06 20:57:50 +00003578size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3579 const DWARFDIE &orig_die,
3580 bool parse_siblings, bool parse_children) {
3581 size_t types_added = 0;
3582 DWARFDIE die = orig_die;
3583 while (die) {
3584 bool type_is_new = false;
3585 if (ParseType(sc, die, &type_is_new).get()) {
3586 if (type_is_new)
3587 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003589
3590 if (parse_children && die.HasChildren()) {
3591 if (die.Tag() == DW_TAG_subprogram) {
3592 SymbolContext child_sc(sc);
3593 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3594 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3595 } else
3596 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3597 }
3598
3599 if (parse_siblings)
3600 die = die.GetSibling();
3601 else
3602 die.Clear();
3603 }
3604 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003605}
3606
Kate Stoneb9c1b512016-09-06 20:57:50 +00003607size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3608 assert(sc.comp_unit && sc.function);
3609 size_t functions_added = 0;
3610 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3611 if (dwarf_cu) {
3612 const dw_offset_t function_die_offset = sc.function->GetID();
3613 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3614 if (function_die) {
3615 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3616 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003619
Kate Stoneb9c1b512016-09-06 20:57:50 +00003620 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003621}
3622
Kate Stoneb9c1b512016-09-06 20:57:50 +00003623size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3624 // At least a compile unit must be valid
3625 assert(sc.comp_unit);
3626 size_t types_added = 0;
3627 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3628 if (dwarf_cu) {
3629 if (sc.function) {
3630 dw_offset_t function_die_offset = sc.function->GetID();
3631 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3632 if (func_die && func_die.HasChildren()) {
3633 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3634 }
3635 } else {
3636 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3637 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3638 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3639 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003641 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003642
Kate Stoneb9c1b512016-09-06 20:57:50 +00003643 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003644}
3645
Kate Stoneb9c1b512016-09-06 20:57:50 +00003646size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3647 if (sc.comp_unit != NULL) {
3648 DWARFDebugInfo *info = DebugInfo();
3649 if (info == NULL)
3650 return 0;
3651
3652 if (sc.function) {
3653 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3654
3655 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3656 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3657 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3658 const size_t num_variables = ParseVariables(
3659 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3660
3661 // Let all blocks know they have parse all their variables
3662 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3663 return num_variables;
3664 }
3665 } else if (sc.comp_unit) {
3666 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3667
3668 if (dwarf_cu == NULL)
3669 return 0;
3670
3671 uint32_t vars_added = 0;
3672 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3673
3674 if (variables.get() == NULL) {
3675 variables.reset(new VariableList());
3676 sc.comp_unit->SetVariableList(variables);
3677
3678 DIEArray die_offsets;
3679 if (m_using_apple_tables) {
3680 if (m_apple_names_ap.get()) {
3681 DWARFMappedHash::DIEInfoArray hash_data_array;
3682 if (m_apple_names_ap->AppendAllDIEsInRange(
3683 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3684 hash_data_array)) {
3685 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003687 }
3688 } else {
3689 // Index if we already haven't to make sure the compile units
3690 // get indexed and make their global DIE index list
3691 if (!m_indexed)
3692 Index();
3693
3694 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3695 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003696 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003697
Kate Stoneb9c1b512016-09-06 20:57:50 +00003698 const size_t num_matches = die_offsets.size();
3699 if (num_matches) {
3700 for (size_t i = 0; i < num_matches; ++i) {
3701 const DIERef &die_ref = die_offsets[i];
3702 DWARFDIE die = GetDIE(die_ref);
3703 if (die) {
3704 VariableSP var_sp(
3705 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3706 if (var_sp) {
3707 variables->AddVariableIfUnique(var_sp);
3708 ++vars_added;
3709 }
3710 } else {
3711 if (m_using_apple_tables) {
3712 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3713 "the DWARF debug information has been modified "
3714 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3715 die_ref.die_offset);
3716 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003717 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003718 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720 }
3721 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003723 }
3724 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003725}
3726
Kate Stoneb9c1b512016-09-06 20:57:50 +00003727VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3728 const DWARFDIE &die,
3729 const lldb::addr_t func_low_pc) {
3730 if (die.GetDWARF() != this)
3731 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003732
Kate Stoneb9c1b512016-09-06 20:57:50 +00003733 VariableSP var_sp;
3734 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736
Kate Stoneb9c1b512016-09-06 20:57:50 +00003737 var_sp = GetDIEToVariable()[die.GetDIE()];
3738 if (var_sp)
3739 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003740
Kate Stoneb9c1b512016-09-06 20:57:50 +00003741 const dw_tag_t tag = die.Tag();
3742 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003743
Kate Stoneb9c1b512016-09-06 20:57:50 +00003744 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3745 (tag == DW_TAG_formal_parameter && sc.function)) {
3746 DWARFAttributes attributes;
3747 const size_t num_attributes = die.GetAttributes(attributes);
3748 DWARFDIE spec_die;
3749 if (num_attributes > 0) {
3750 const char *name = NULL;
3751 const char *mangled = NULL;
3752 Declaration decl;
3753 uint32_t i;
3754 DWARFFormValue type_die_form;
3755 DWARFExpression location(die.GetCU());
3756 bool is_external = false;
3757 bool is_artificial = false;
3758 bool location_is_const_value_data = false;
3759 bool has_explicit_location = false;
3760 DWARFFormValue const_value;
3761 Variable::RangeList scope_ranges;
3762 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003763
Kate Stoneb9c1b512016-09-06 20:57:50 +00003764 for (i = 0; i < num_attributes; ++i) {
3765 dw_attr_t attr = attributes.AttributeAtIndex(i);
3766 DWARFFormValue form_value;
3767
3768 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3769 switch (attr) {
3770 case DW_AT_decl_file:
3771 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3772 form_value.Unsigned()));
3773 break;
3774 case DW_AT_decl_line:
3775 decl.SetLine(form_value.Unsigned());
3776 break;
3777 case DW_AT_decl_column:
3778 decl.SetColumn(form_value.Unsigned());
3779 break;
3780 case DW_AT_name:
3781 name = form_value.AsCString();
3782 break;
3783 case DW_AT_linkage_name:
3784 case DW_AT_MIPS_linkage_name:
3785 mangled = form_value.AsCString();
3786 break;
3787 case DW_AT_type:
3788 type_die_form = form_value;
3789 break;
3790 case DW_AT_external:
3791 is_external = form_value.Boolean();
3792 break;
3793 case DW_AT_const_value:
3794 // If we have already found a DW_AT_location attribute, ignore this
3795 // attribute.
3796 if (!has_explicit_location) {
3797 location_is_const_value_data = true;
3798 // The constant value will be either a block, a data value or a
3799 // string.
3800 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3801 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3802 // Retrieve the value as a block expression.
3803 uint32_t block_offset =
3804 form_value.BlockData() - debug_info_data.GetDataStart();
3805 uint32_t block_length = form_value.Unsigned();
3806 location.CopyOpcodeData(module, debug_info_data, block_offset,
3807 block_length);
3808 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3809 // Retrieve the value as a data expression.
3810 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3811 DWARFFormValue::GetFixedFormSizesForAddressSize(
3812 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3813 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3814 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3815 uint32_t data_length =
3816 fixed_form_sizes.GetSize(form_value.Form());
3817 if (data_length == 0) {
3818 const uint8_t *data_pointer = form_value.BlockData();
3819 if (data_pointer) {
3820 form_value.Unsigned();
3821 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3822 // we need to get the byte size of the type later after we
3823 // create the variable
3824 const_value = form_value;
3825 }
3826 } else
3827 location.CopyOpcodeData(module, debug_info_data, data_offset,
3828 data_length);
3829 } else {
3830 // Retrieve the value as a string expression.
3831 if (form_value.Form() == DW_FORM_strp) {
3832 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3833 DWARFFormValue::GetFixedFormSizesForAddressSize(
3834 attributes.CompileUnitAtIndex(i)
3835 ->GetAddressByteSize(),
3836 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3837 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3838 uint32_t data_length =
3839 fixed_form_sizes.GetSize(form_value.Form());
3840 location.CopyOpcodeData(module, debug_info_data, data_offset,
3841 data_length);
3842 } else {
3843 const char *str = form_value.AsCString();
3844 uint32_t string_offset =
3845 str - (const char *)debug_info_data.GetDataStart();
3846 uint32_t string_length = strlen(str) + 1;
3847 location.CopyOpcodeData(module, debug_info_data,
3848 string_offset, string_length);
3849 }
3850 }
3851 }
3852 break;
3853 case DW_AT_location: {
3854 location_is_const_value_data = false;
3855 has_explicit_location = true;
3856 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3857 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3858
3859 uint32_t block_offset =
3860 form_value.BlockData() - debug_info_data.GetDataStart();
3861 uint32_t block_length = form_value.Unsigned();
3862 location.CopyOpcodeData(module, get_debug_info_data(),
3863 block_offset, block_length);
3864 } else {
3865 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3866 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3867
3868 size_t loc_list_length = DWARFExpression::LocationListSize(
3869 die.GetCU(), debug_loc_data, debug_loc_offset);
3870 if (loc_list_length > 0) {
3871 location.CopyOpcodeData(module, debug_loc_data,
3872 debug_loc_offset, loc_list_length);
3873 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3874 location.SetLocationListSlide(
3875 func_low_pc -
3876 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3877 }
3878 }
3879 } break;
3880 case DW_AT_specification:
3881 spec_die = GetDIE(DIERef(form_value));
3882 break;
3883 case DW_AT_start_scope: {
3884 if (form_value.Form() == DW_FORM_sec_offset) {
3885 DWARFRangeList dwarf_scope_ranges;
3886 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003887 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3888 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003889 dwarf_scope_ranges);
3890
3891 // All DW_AT_start_scope are relative to the base address of the
3892 // compile unit. We add the compile unit base address to make
3893 // sure all the addresses are properly fixed up.
3894 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3895 i < count; ++i) {
3896 const DWARFRangeList::Entry &range =
3897 dwarf_scope_ranges.GetEntryRef(i);
3898 scope_ranges.Append(range.GetRangeBase() +
3899 die.GetCU()->GetBaseAddress(),
3900 range.GetByteSize());
3901 }
3902 } else {
3903 // TODO: Handle the case when DW_AT_start_scope have form
3904 // constant. The
3905 // dwarf spec is a bit ambiguous about what is the expected
3906 // behavior in
3907 // case the enclosing block have a non coninious address range and
3908 // the
3909 // DW_AT_start_scope entry have a form constant.
3910 GetObjectFile()->GetModule()->ReportWarning(
3911 "0x%8.8" PRIx64
3912 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3913 die.GetID(), form_value.Form());
3914 }
3915
3916 scope_ranges.Sort();
3917 scope_ranges.CombineConsecutiveRanges();
3918 } break;
3919 case DW_AT_artificial:
3920 is_artificial = form_value.Boolean();
3921 break;
3922 case DW_AT_accessibility:
3923 break; // accessibility =
3924 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3925 case DW_AT_declaration:
3926 case DW_AT_description:
3927 case DW_AT_endianity:
3928 case DW_AT_segment:
3929 case DW_AT_visibility:
3930 default:
3931 case DW_AT_abstract_origin:
3932 case DW_AT_sibling:
3933 break;
3934 }
3935 }
3936 }
3937
3938 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3939 const dw_tag_t parent_tag = die.GetParent().Tag();
3940 bool is_static_member =
3941 parent_tag == DW_TAG_compile_unit &&
3942 (parent_context_die.Tag() == DW_TAG_class_type ||
3943 parent_context_die.Tag() == DW_TAG_structure_type);
3944
3945 ValueType scope = eValueTypeInvalid;
3946
3947 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3948 SymbolContextScope *symbol_context_scope = NULL;
3949
Sam McCall30621392016-11-22 11:40:25 +00003950 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003951 if (!mangled) {
3952 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3953 // DW_AT_MIPS_linkage_name) to
3954 // generate fully qualified names of global variables with commands like
3955 // "frame var j".
3956 // For example, if j were an int variable holding a value 4 and declared
3957 // in a namespace
3958 // B which in turn is contained in a namespace A, the command "frame var
3959 // j" returns
3960 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we
3961 // should be able
3962 // to generate a fully qualified name from the declaration context.
3963 if (parent_tag == DW_TAG_compile_unit &&
3964 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3965 DWARFDeclContext decl_ctx;
3966
3967 die.GetDWARFDeclContext(decl_ctx);
3968 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3969 }
3970 }
3971
Kate Stoneb9c1b512016-09-06 20:57:50 +00003972 if (tag == DW_TAG_formal_parameter)
3973 scope = eValueTypeVariableArgument;
3974 else {
Sam McCall30621392016-11-22 11:40:25 +00003975 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3976 // or static variable, so we have to do a little digging:
3977 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3978 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3979 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3980 // Clang likes to combine small global variables into the same symbol
3981 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3982 // so we need to look through the whole expression.
3983 bool is_static_lifetime =
3984 has_explicit_mangled ||
3985 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003986 // Check if the location has a DW_OP_addr with any address value...
3987 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3988 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003989 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003990 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3991 if (op_error) {
3992 StreamString strm;
3993 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3994 NULL);
3995 GetObjectFile()->GetModule()->ReportError(
3996 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003997 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003998 }
Sam McCall30621392016-11-22 11:40:25 +00003999 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
4000 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004001 }
4002 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
4003
Sam McCall30621392016-11-22 11:40:25 +00004004 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004005 if (is_external)
4006 scope = eValueTypeVariableGlobal;
4007 else
4008 scope = eValueTypeVariableStatic;
4009
4010 if (debug_map_symfile) {
4011 // When leaving the DWARF in the .o files on darwin,
4012 // when we have a global variable that wasn't initialized,
4013 // the .o file might not have allocated a virtual
4014 // address for the global variable. In this case it will
4015 // have created a symbol for the global variable
4016 // that is undefined/data and external and the value will
4017 // be the byte size of the variable. When we do the
4018 // address map in SymbolFileDWARFDebugMap we rely on
4019 // having an address, we need to do some magic here
4020 // so we can get the correct address for our global
4021 // variable. The address for all of these entries
4022 // will be zero, and there will be an undefined symbol
4023 // in this object file, and the executable will have
4024 // a matching symbol with a good address. So here we
4025 // dig up the correct address and replace it in the
4026 // location for the variable, and set the variable's
4027 // symbol context scope to be that of the main executable
4028 // so the file address will resolve correctly.
4029 bool linked_oso_file_addr = false;
4030 if (is_external && location_DW_OP_addr == 0) {
4031 // we have a possible uninitialized extern global
4032 ConstString const_name(mangled ? mangled : name);
4033 ObjectFile *debug_map_objfile =
4034 debug_map_symfile->GetObjectFile();
4035 if (debug_map_objfile) {
4036 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
4037 if (debug_map_symtab) {
4038 Symbol *exe_symbol =
4039 debug_map_symtab->FindFirstSymbolWithNameAndType(
4040 const_name, eSymbolTypeData, Symtab::eDebugYes,
4041 Symtab::eVisibilityExtern);
4042 if (exe_symbol) {
4043 if (exe_symbol->ValueIsAddress()) {
4044 const addr_t exe_file_addr =
4045 exe_symbol->GetAddressRef().GetFileAddress();
4046 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4047 if (location.Update_DW_OP_addr(exe_file_addr)) {
4048 linked_oso_file_addr = true;
4049 symbol_context_scope = exe_symbol;
4050 }
4051 }
4052 }
4053 }
4054 }
4055 }
4056 }
4057
4058 if (!linked_oso_file_addr) {
4059 // The DW_OP_addr is not zero, but it contains a .o file address
4060 // which
4061 // needs to be linked up correctly.
4062 const lldb::addr_t exe_file_addr =
4063 debug_map_symfile->LinkOSOFileAddress(this,
4064 location_DW_OP_addr);
4065 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4066 // Update the file address for this variable
4067 location.Update_DW_OP_addr(exe_file_addr);
4068 } else {
4069 // Variable didn't make it into the final executable
4070 return var_sp;
4071 }
4072 }
4073 }
4074 } else {
4075 if (location_is_const_value_data)
4076 scope = eValueTypeVariableStatic;
4077 else {
4078 scope = eValueTypeVariableLocal;
4079 if (debug_map_symfile) {
4080 // We need to check for TLS addresses that we need to fixup
4081 if (location.ContainsThreadLocalStorage()) {
4082 location.LinkThreadLocalStorage(
4083 debug_map_symfile->GetObjectFile()->GetModule(),
4084 [this, debug_map_symfile](
4085 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4086 return debug_map_symfile->LinkOSOFileAddress(
4087 this, unlinked_file_addr);
4088 });
4089 scope = eValueTypeVariableThreadLocal;
4090 }
4091 }
4092 }
4093 }
4094 }
4095
4096 if (symbol_context_scope == NULL) {
4097 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004098 case DW_TAG_subprogram:
4099 case DW_TAG_inlined_subroutine:
4100 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004101 if (sc.function) {
4102 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4103 sc_parent_die.GetID());
4104 if (symbol_context_scope == NULL)
4105 symbol_context_scope = sc.function;
4106 }
4107 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004108
Kate Stoneb9c1b512016-09-06 20:57:50 +00004109 default:
4110 symbol_context_scope = sc.comp_unit;
4111 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004112 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004113 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004114
Kate Stoneb9c1b512016-09-06 20:57:50 +00004115 if (symbol_context_scope) {
4116 SymbolFileTypeSP type_sp(
4117 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4118
4119 if (const_value.Form() && type_sp && type_sp->GetType())
4120 location.CopyOpcodeData(const_value.Unsigned(),
4121 type_sp->GetType()->GetByteSize(),
4122 die.GetCU()->GetAddressByteSize());
4123
4124 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4125 symbol_context_scope, scope_ranges, &decl,
4126 location, is_external, is_artificial,
4127 is_static_member));
4128
4129 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4130 } else {
4131 // Not ready to parse this variable yet. It might be a global
4132 // or static variable that is in a function scope and the function
4133 // in the symbol context wasn't filled in yet
4134 return var_sp;
4135 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004137 // Cache var_sp even if NULL (the variable was just a specification or
4138 // was missing vital information to be able to be displayed in the debugger
4139 // (missing location due to optimization, etc)) so we don't re-parse
4140 // this DIE over and over later...
4141 GetDIEToVariable()[die.GetDIE()] = var_sp;
4142 if (spec_die)
4143 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4144 }
4145 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004146}
4147
Kate Stoneb9c1b512016-09-06 20:57:50 +00004148DWARFDIE
4149SymbolFileDWARF::FindBlockContainingSpecification(
4150 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4151 // Give the concrete function die specified by "func_die_offset", find the
4152 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4153 // to "spec_block_die_offset"
4154 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4155 spec_block_die_offset);
4156}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004157
Kate Stoneb9c1b512016-09-06 20:57:50 +00004158DWARFDIE
4159SymbolFileDWARF::FindBlockContainingSpecification(
4160 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4161 if (die) {
4162 switch (die.Tag()) {
4163 case DW_TAG_subprogram:
4164 case DW_TAG_inlined_subroutine:
4165 case DW_TAG_lexical_block: {
4166 if (die.GetAttributeValueAsReference(
4167 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4168 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004169
Kate Stoneb9c1b512016-09-06 20:57:50 +00004170 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4171 DW_INVALID_OFFSET) ==
4172 spec_block_die_offset)
4173 return die;
4174 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004176
4177 // Give the concrete function die specified by "func_die_offset", find the
4178 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4179 // to "spec_block_die_offset"
4180 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4181 child_die = child_die.GetSibling()) {
4182 DWARFDIE result_die =
4183 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4184 if (result_die)
4185 return result_die;
4186 }
4187 }
4188
4189 return DWARFDIE();
4190}
4191
4192size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4193 const DWARFDIE &orig_die,
4194 const lldb::addr_t func_low_pc,
4195 bool parse_siblings, bool parse_children,
4196 VariableList *cc_variable_list) {
4197 if (!orig_die)
4198 return 0;
4199
4200 VariableListSP variable_list_sp;
4201
4202 size_t vars_added = 0;
4203 DWARFDIE die = orig_die;
4204 while (die) {
4205 dw_tag_t tag = die.Tag();
4206
4207 // Check to see if we have already parsed this variable or constant?
4208 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4209 if (var_sp) {
4210 if (cc_variable_list)
4211 cc_variable_list->AddVariableIfUnique(var_sp);
4212 } else {
4213 // We haven't already parsed it, lets do that now.
4214 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4215 (tag == DW_TAG_formal_parameter && sc.function)) {
4216 if (variable_list_sp.get() == NULL) {
4217 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4218 dw_tag_t parent_tag = sc_parent_die.Tag();
4219 switch (parent_tag) {
4220 case DW_TAG_compile_unit:
4221 if (sc.comp_unit != NULL) {
4222 variable_list_sp = sc.comp_unit->GetVariableList(false);
4223 if (variable_list_sp.get() == NULL) {
4224 variable_list_sp.reset(new VariableList());
4225 sc.comp_unit->SetVariableList(variable_list_sp);
4226 }
4227 } else {
4228 GetObjectFile()->GetModule()->ReportError(
4229 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4230 "symbol context for 0x%8.8" PRIx64
4231 " %s.\n",
4232 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4233 orig_die.GetID(), orig_die.GetTagAsCString());
4234 }
4235 break;
4236
4237 case DW_TAG_subprogram:
4238 case DW_TAG_inlined_subroutine:
4239 case DW_TAG_lexical_block:
4240 if (sc.function != NULL) {
4241 // Check to see if we already have parsed the variables for the
4242 // given scope
4243
4244 Block *block = sc.function->GetBlock(true).FindBlockByID(
4245 sc_parent_die.GetID());
4246 if (block == NULL) {
4247 // This must be a specification or abstract origin with
4248 // a concrete block counterpart in the current function. We need
4249 // to find the concrete block so we can correctly add the
4250 // variable to it
4251 const DWARFDIE concrete_block_die =
4252 FindBlockContainingSpecification(
4253 DIERef(sc.function->GetID(), this),
4254 sc_parent_die.GetOffset());
4255 if (concrete_block_die)
4256 block = sc.function->GetBlock(true).FindBlockByID(
4257 concrete_block_die.GetID());
4258 }
4259
4260 if (block != NULL) {
4261 const bool can_create = false;
4262 variable_list_sp = block->GetBlockVariableList(can_create);
4263 if (variable_list_sp.get() == NULL) {
4264 variable_list_sp.reset(new VariableList());
4265 block->SetVariableList(variable_list_sp);
4266 }
4267 }
4268 }
4269 break;
4270
4271 default:
4272 GetObjectFile()->GetModule()->ReportError(
4273 "didn't find appropriate parent DIE for variable list for "
4274 "0x%8.8" PRIx64 " %s.\n",
4275 orig_die.GetID(), orig_die.GetTagAsCString());
4276 break;
4277 }
4278 }
4279
4280 if (variable_list_sp) {
4281 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4282 if (var_sp) {
4283 variable_list_sp->AddVariableIfUnique(var_sp);
4284 if (cc_variable_list)
4285 cc_variable_list->AddVariableIfUnique(var_sp);
4286 ++vars_added;
4287 }
4288 }
4289 }
4290 }
4291
4292 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4293
4294 if (!skip_children && parse_children && die.HasChildren()) {
4295 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4296 true, cc_variable_list);
4297 }
4298
4299 if (parse_siblings)
4300 die = die.GetSibling();
4301 else
4302 die.Clear();
4303 }
4304 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004305}
4306
4307//------------------------------------------------------------------
4308// PluginInterface protocol
4309//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004310ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4311
4312uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4313
4314void SymbolFileDWARF::DumpIndexes() {
4315 StreamFile s(stdout, false);
4316
4317 s.Printf(
4318 "DWARF index for (%s) '%s':",
4319 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4320 GetObjectFile()->GetFileSpec().GetPath().c_str());
4321 s.Printf("\nFunction basenames:\n");
4322 m_function_basename_index.Dump(&s);
4323 s.Printf("\nFunction fullnames:\n");
4324 m_function_fullname_index.Dump(&s);
4325 s.Printf("\nFunction methods:\n");
4326 m_function_method_index.Dump(&s);
4327 s.Printf("\nFunction selectors:\n");
4328 m_function_selector_index.Dump(&s);
4329 s.Printf("\nObjective C class selectors:\n");
4330 m_objc_class_selectors_index.Dump(&s);
4331 s.Printf("\nGlobals and statics:\n");
4332 m_global_index.Dump(&s);
4333 s.Printf("\nTypes:\n");
4334 m_type_index.Dump(&s);
4335 s.Printf("\nNamespaces:\n");
4336 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004337}
4338
Kate Stoneb9c1b512016-09-06 20:57:50 +00004339SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4340 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4341 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4342 if (module_sp) {
4343 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4344 if (sym_vendor)
4345 m_debug_map_symfile =
4346 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004347 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004348 }
4349 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004350}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004351
4352DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004353SymbolFileDWARF::GetLocationListFormat() const {
4354 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004355}
Tamas Berghammer963ce482017-08-25 13:56:14 +00004356
4357SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
4358 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00004359 ModuleSpec module_spec;
4360 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
4361 module_spec.GetSymbolFileSpec() =
4362 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
4363 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Tamas Berghammer963ce482017-08-25 13:56:14 +00004364 if (dwp_filespec.Exists()) {
4365 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
4366 dwp_filespec);
4367 }
4368 });
4369 return m_dwp_symfile.get();
4370}