blob: c132889a7ba67ba01dbbe5611c86680e4622e9b5 [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)
395 : SymbolFile(objfile), UserID(0), // Used by SymbolFileDWARFDebugMap to when
396 // this class parses .o files to contain
397 // the .o file index/ID
398 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
399 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
400 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
401 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
402 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
403 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
404 m_apple_objc_ap(), m_function_basename_index(),
405 m_function_fullname_index(), m_function_method_index(),
406 m_function_selector_index(), m_objc_class_selectors_index(),
407 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
408 m_using_apple_tables(false), m_fetched_external_modules(false),
409 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
410 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412SymbolFileDWARF::~SymbolFileDWARF() {}
413
414static const ConstString &GetDWARFMachOSegmentName() {
415 static ConstString g_dwarf_section_name("__DWARF");
416 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417}
418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
420 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
421 if (debug_map_symfile)
422 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
423 else
424 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000425}
426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
428 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
429 TypeSystem *type_system;
430 if (debug_map_symfile) {
431 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
432 } else {
433 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
434 if (type_system)
435 type_system->SetSymbolFile(this);
436 }
437 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000438}
439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440void SymbolFileDWARF::InitializeObject() {
441 ModuleSP module_sp(m_obj_file->GetModule());
442 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000443 const SectionList *section_list = module_sp->GetSectionList();
Ed Masted13f6912017-10-02 14:35:07 +0000444 Section *section =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (section)
Pavel Labathf1208e72017-12-14 14:56:45 +0000448 m_obj_file->ReadSectionData(section, m_dwarf_data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 }
450
451 get_apple_names_data();
452 if (m_data_apple_names.m_data.GetByteSize() > 0) {
453 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
454 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000455 if (m_apple_names_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000456 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000457 else
458 m_apple_names_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 }
460 get_apple_types_data();
461 if (m_data_apple_types.m_data.GetByteSize() > 0) {
462 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
463 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000464 if (m_apple_types_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000465 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000466 else
467 m_apple_types_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 }
469
470 get_apple_namespaces_data();
471 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
472 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
473 m_data_apple_namespaces.m_data, get_debug_str_data(),
474 ".apple_namespaces"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000475 if (m_apple_namespaces_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000476 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000477 else
478 m_apple_namespaces_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 }
480
481 get_apple_objc_data();
482 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
483 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
484 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000485 if (m_apple_objc_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000486 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000487 else
488 m_apple_objc_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 }
490}
491
492bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
493 return version == 2 || version == 3 || version == 4;
494}
495
496uint32_t SymbolFileDWARF::CalculateAbilities() {
497 uint32_t abilities = 0;
498 if (m_obj_file != NULL) {
499 const Section *section = NULL;
500 const SectionList *section_list = m_obj_file->GetSectionList();
501 if (section_list == NULL)
502 return 0;
503
Greg Claytonea5df102017-07-24 18:40:33 +0000504 // On non Apple platforms we might have .debug_types debug info that
505 // is created by using "-fdebug-types-section". LLDB currently will try
506 // to load this debug info, but it causes crashes during debugging when
507 // types are missing since it doesn't know how to parse the info in
508 // the .debug_types type units. This causes all complex debug info
509 // types to be unresolved. Because this causes LLDB to crash and since
510 // it really doesn't provide a solid debuggiung experience, we should
511 // disable trying to debug this kind of DWARF until support gets
512 // added or deprecated.
513 if (section_list->FindSectionByName(ConstString(".debug_types"))) {
514 m_obj_file->GetModule()->ReportWarning(
515 "lldb doesn’t support .debug_types debug info");
516 return 0;
517 }
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 uint64_t debug_abbrev_file_size = 0;
520 uint64_t debug_info_file_size = 0;
521 uint64_t debug_line_file_size = 0;
522
523 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
524
525 if (section)
526 section_list = &section->GetChildren();
527
528 section =
529 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
530 if (section != NULL) {
531 debug_info_file_size = section->GetFileSize();
532
533 section =
534 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
535 .get();
536 if (section)
537 debug_abbrev_file_size = section->GetFileSize();
538
Jan Kratochvilb14f1da2017-07-31 17:02:52 +0000539 DWARFDebugAbbrev *abbrev = DebugAbbrev();
540 if (abbrev) {
541 std::set<dw_form_t> invalid_forms;
542 abbrev->GetUnsupportedForms(invalid_forms);
543 if (!invalid_forms.empty()) {
544 StreamString error;
545 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
546 for (auto form : invalid_forms)
547 error.Printf(" %#x", form);
548 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
549 return 0;
550 }
551 }
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 section =
554 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
555 .get();
556 if (section)
557 debug_line_file_size = section->GetFileSize();
558 } else {
559 const char *symfile_dir_cstr =
560 m_obj_file->GetFileSpec().GetDirectory().GetCString();
561 if (symfile_dir_cstr) {
562 if (strcasestr(symfile_dir_cstr, ".dsym")) {
563 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
564 // We have a dSYM file that didn't have a any debug info.
565 // If the string table has a size of 1, then it was made from
566 // an executable with no debug info, or from an executable that
567 // was stripped.
568 section =
569 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
570 .get();
571 if (section && section->GetFileSize() == 1) {
572 m_obj_file->GetModule()->ReportWarning(
573 "empty dSYM file detected, dSYM was created with an "
574 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000575 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580
581 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
582 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
583 LocalVariables | VariableTypes;
584
585 if (debug_line_file_size > 0)
586 abilities |= LineTables;
587 }
588 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589}
590
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591const DWARFDataExtractor &
592SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
593 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000594 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
595 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
596 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}
599
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
601 DWARFDataExtractor &data) {
602 ModuleSP module_sp(m_obj_file->GetModule());
603 const SectionList *section_list = module_sp->GetSectionList();
604 if (section_list) {
605 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
606 if (section_sp) {
607 // See if we memory mapped the DWARF segment?
608 if (m_dwarf_data.GetByteSize()) {
609 data.SetData(m_dwarf_data, section_sp->GetOffset(),
610 section_sp->GetFileSize());
611 } else {
612 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
613 data.Clear();
614 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617}
618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
620 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
621 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622}
623
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
625 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
626}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
629 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
630 m_data_debug_aranges);
631}
632
633const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
634 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
635}
636
637const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
638 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
639}
640
641const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
642 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
643}
644
645const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
646 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
647}
648
649const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
650 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
651}
652
653const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
654 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
655 m_data_debug_ranges);
656}
657
658const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
659 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
660}
661
662const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
663 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
664 m_data_debug_str_offsets);
665}
666
667const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
668 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
669}
670
671const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
672 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
673}
674
675const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
676 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
677 m_data_apple_namespaces);
678}
679
680const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
681 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
682}
683
684DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
685 if (m_abbr.get() == NULL) {
686 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
687 if (debug_abbrev_data.GetByteSize() > 0) {
688 m_abbr.reset(new DWARFDebugAbbrev());
689 if (m_abbr.get())
690 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 }
693 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694}
695
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
697 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698}
699
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
701 if (m_info.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000702 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
703 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
704 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (get_debug_info_data().GetByteSize() > 0) {
706 m_info.reset(new DWARFDebugInfo());
707 if (m_info.get()) {
708 m_info->SetDwarfData(this);
709 }
Greg Clayton1f746072012-08-29 21:13:06 +0000710 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 }
712 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713}
714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
716 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717}
718
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719DWARFCompileUnit *
720SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
721 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000722 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723
724 DWARFDebugInfo *info = DebugInfo();
725 if (info) {
726 // Just a normal DWARF file whose user ID for the compile unit is
727 // the DWARF offset itself
728
729 DWARFCompileUnit *dwarf_cu =
730 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
731 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
732 dwarf_cu->SetUserData(comp_unit);
733 return dwarf_cu;
734 }
735 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736}
737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
739 if (m_ranges.get() == NULL) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000740 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
741 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
742 static_cast<void *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 if (get_debug_ranges_data().GetByteSize() > 0) {
744 m_ranges.reset(new DWARFDebugRanges());
745 if (m_ranges.get())
746 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000747 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 }
749 return m_ranges.get();
750}
751
752const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
753 return m_ranges.get();
754}
755
756lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit *dwarf_cu,
757 uint32_t cu_idx) {
758 CompUnitSP cu_sp;
759 if (dwarf_cu) {
760 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
761 if (comp_unit) {
762 // We already parsed this compile unit, had out a shared pointer to it
763 cu_sp = comp_unit->shared_from_this();
764 } else {
765 if (dwarf_cu->GetSymbolFileDWARF() != this) {
766 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
767 cu_idx);
768 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
769 // Let the debug map create the compile unit
770 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
771 dwarf_cu->SetUserData(cu_sp.get());
772 } else {
773 ModuleSP module_sp(m_obj_file->GetModule());
774 if (module_sp) {
775 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
776 if (cu_die) {
777 FileSpec cu_file_spec{cu_die.GetName(), false};
778 if (cu_file_spec) {
779 // If we have a full path to the compile unit, we don't need to
780 // resolve
781 // the file. This can be expensive e.g. when the source files are
782 // NFS mounted.
783 if (cu_file_spec.IsRelative()) {
784 const char *cu_comp_dir{
785 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
786 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
787 }
788
789 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000790 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 remapped_file))
792 cu_file_spec.SetFile(remapped_file, false);
793 }
794
795 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(
796 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
797
798 bool is_optimized = dwarf_cu->GetIsOptimized();
799 cu_sp.reset(new CompileUnit(
800 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
801 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
802 if (cu_sp) {
803 // If we just created a compile unit with an invalid file spec,
804 // try and get the
805 // first entry in the supports files from the line table as that
806 // should be the
807 // compile unit.
808 if (!cu_file_spec) {
809 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
810 if (cu_file_spec) {
811 (FileSpec &)(*cu_sp) = cu_file_spec;
812 // Also fix the invalid file spec which was copied from the
813 // compile unit.
814 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
815 }
816 }
817
818 dwarf_cu->SetUserData(cu_sp.get());
819
820 // Figure out the compile unit index if we weren't given one
821 if (cu_idx == UINT32_MAX)
822 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
823
824 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
825 cu_idx, cu_sp);
826 }
827 }
828 }
829 }
830 }
831 }
832 return cu_sp;
833}
834
835uint32_t SymbolFileDWARF::GetNumCompileUnits() {
836 DWARFDebugInfo *info = DebugInfo();
837 if (info)
838 return info->GetNumCompileUnits();
839 return 0;
840}
841
842CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
843 CompUnitSP cu_sp;
844 DWARFDebugInfo *info = DebugInfo();
845 if (info) {
846 DWARFCompileUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
847 if (dwarf_cu)
848 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
849 }
850 return cu_sp;
851}
852
853Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
854 const DWARFDIE &die) {
855 if (die.IsValid()) {
856 TypeSystem *type_system =
857 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
858
859 if (type_system) {
860 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
861 if (dwarf_ast)
862 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
863 }
864 }
865 return nullptr;
866}
867
868bool SymbolFileDWARF::FixupAddress(Address &addr) {
869 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
870 if (debug_map_symfile) {
871 return debug_map_symfile->LinkOSOAddress(addr);
872 }
873 // This is a normal DWARF file, no address fixups need to happen
874 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000875}
Greg Clayton1f746072012-08-29 21:13:06 +0000876lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
878 assert(sc.comp_unit);
879 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
880 if (dwarf_cu)
881 return dwarf_cu->GetLanguageType();
882 else
883 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000884}
885
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
887 assert(sc.comp_unit);
888 size_t functions_added = 0;
889 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
890 if (dwarf_cu) {
891 DWARFDIECollection function_dies;
892 const size_t num_functions =
893 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
894 size_t func_idx;
895 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
896 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
897 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
898 if (ParseCompileUnitFunction(sc, die))
899 ++functions_added;
900 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 // FixupTypes();
903 }
904 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905}
906
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
908 const SymbolContext &sc, FileSpecList &support_files) {
909 assert(sc.comp_unit);
910 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
911 if (dwarf_cu) {
912 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 if (cu_die) {
915 const char *cu_comp_dir = resolveCompDir(
916 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
917 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
918 DW_AT_stmt_list, DW_INVALID_OFFSET);
919 if (stmt_list != DW_INVALID_OFFSET) {
920 // All file indexes in DWARF are one based and a file of index zero is
921 // supposed to be the compile unit itself.
922 support_files.Append(*sc.comp_unit);
923 return DWARFDebugLine::ParseSupportFiles(
924 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
925 stmt_list, support_files);
926 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 }
929 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930}
931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
933 const lldb_private::SymbolContext &sc) {
934 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
935 if (dwarf_cu)
936 return dwarf_cu->GetIsOptimized();
937 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000938}
939
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940bool SymbolFileDWARF::ParseImportedModules(
941 const lldb_private::SymbolContext &sc,
942 std::vector<lldb_private::ConstString> &imported_modules) {
943 assert(sc.comp_unit);
944 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
945 if (dwarf_cu) {
946 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
947 sc.comp_unit->GetLanguage())) {
948 UpdateExternalModuleListIfNeeded();
949
950 if (sc.comp_unit) {
951 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
952
953 if (die) {
954 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
955 child_die = child_die.GetSibling()) {
956 if (child_die.Tag() == DW_TAG_imported_declaration) {
957 if (DWARFDIE module_die =
958 child_die.GetReferencedDIE(DW_AT_import)) {
959 if (module_die.Tag() == DW_TAG_module) {
960 if (const char *name = module_die.GetAttributeValueAsString(
961 DW_AT_name, nullptr)) {
962 ConstString const_name(name);
963 imported_modules.push_back(const_name);
964 }
Sean Callananb0300a42016-01-14 21:46:09 +0000965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 }
Sean Callananb0300a42016-01-14 21:46:09 +0000967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000969 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 } else {
971 for (const auto &pair : m_external_type_modules) {
972 imported_modules.push_back(pair.first);
973 }
974 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 }
977 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000978}
979
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980struct ParseDWARFLineTableCallbackInfo {
981 LineTable *line_table;
982 std::unique_ptr<LineSequence> sequence_ap;
983 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984};
985
986//----------------------------------------------------------------------
987// ParseStatementTableCallback
988//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989static void ParseDWARFLineTableCallback(dw_offset_t offset,
990 const DWARFDebugLine::State &state,
991 void *userData) {
992 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
993 // Just started parsing the line table
994 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
995 // Done parsing line table, nothing to do for the cleanup
996 } else {
997 ParseDWARFLineTableCallbackInfo *info =
998 (ParseDWARFLineTableCallbackInfo *)userData;
999 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 // If this is our first time here, we need to create a
1002 // sequence container.
1003 if (!info->sequence_ap.get()) {
1004 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1005 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 line_table->AppendLineEntryToSequence(
1008 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
1009 state.column, state.file, state.is_stmt, state.basic_block,
1010 state.prologue_end, state.epilogue_begin, state.end_sequence);
1011 if (state.end_sequence) {
1012 // First, put the current sequence into the line table.
1013 line_table->InsertSequence(info->sequence_ap.get());
1014 // Then, empty it to prepare for the next sequence.
1015 info->sequence_ap->Clear();
1016 }
1017 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018}
1019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
1021 assert(sc.comp_unit);
1022 if (sc.comp_unit->GetLineTable() != NULL)
1023 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1026 if (dwarf_cu) {
1027 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1028 if (dwarf_cu_die) {
1029 const dw_offset_t cu_line_offset =
1030 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
1031 DW_INVALID_OFFSET);
1032 if (cu_line_offset != DW_INVALID_OFFSET) {
1033 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1034 if (line_table_ap.get()) {
1035 ParseDWARFLineTableCallbackInfo info;
1036 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001037
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 /*
1039 * MIPS:
1040 * The SymbolContext may not have a valid target, thus we may not be
1041 * able
1042 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1043 * #0
1044 * for MIPS. Use ArchSpec to clear the bit #0.
1045 */
1046 ArchSpec arch;
1047 GetObjectFile()->GetArchitecture(arch);
1048 switch (arch.GetMachine()) {
1049 case llvm::Triple::mips:
1050 case llvm::Triple::mipsel:
1051 case llvm::Triple::mips64:
1052 case llvm::Triple::mips64el:
1053 info.addr_mask = ~((lldb::addr_t)1);
1054 break;
1055 default:
1056 info.addr_mask = ~((lldb::addr_t)0);
1057 break;
1058 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 lldb::offset_t offset = cu_line_offset;
1061 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1062 ParseDWARFLineTableCallback,
1063 &info);
1064 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1065 if (debug_map_symfile) {
1066 // We have an object file that has a line table with addresses
1067 // that are not linked. We need to link the line table and convert
1068 // the addresses that are relative to the .o file into addresses
1069 // for the main executable.
1070 sc.comp_unit->SetLineTable(
1071 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1072 } else {
1073 sc.comp_unit->SetLineTable(line_table_ap.release());
1074 return true;
1075 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 }
1080 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081}
1082
Siva Chandrad8335e92015-12-16 00:22:08 +00001083lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1085 auto iter = m_debug_macros_map.find(*offset);
1086 if (iter != m_debug_macros_map.end())
1087 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1090 if (debug_macro_data.GetByteSize() == 0)
1091 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1094 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001095
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 const DWARFDebugMacroHeader &header =
1097 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1098 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1099 header.OffsetIs64Bit(), offset, this,
1100 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001103}
1104
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1106 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1109 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001110 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111
1112 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1113 if (!dwarf_cu_die)
1114 return false;
1115
1116 lldb::offset_t sect_offset =
1117 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1118 if (sect_offset == DW_INVALID_OFFSET)
1119 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1120 DW_INVALID_OFFSET);
1121 if (sect_offset == DW_INVALID_OFFSET)
1122 return false;
1123
1124 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1125
1126 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001127}
1128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1130 Block *parent_block,
1131 const DWARFDIE &orig_die,
1132 addr_t subprogram_low_pc,
1133 uint32_t depth) {
1134 size_t blocks_added = 0;
1135 DWARFDIE die = orig_die;
1136 while (die) {
1137 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001138
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 switch (tag) {
1140 case DW_TAG_inlined_subroutine:
1141 case DW_TAG_subprogram:
1142 case DW_TAG_lexical_block: {
1143 Block *block = NULL;
1144 if (tag == DW_TAG_subprogram) {
1145 // Skip any DW_TAG_subprogram DIEs that are inside
1146 // of a normal or inlined functions. These will be
1147 // parsed on their own as separate entities.
1148
1149 if (depth > 0)
1150 break;
1151
1152 block = parent_block;
1153 } else {
1154 BlockSP block_sp(new Block(die.GetID()));
1155 parent_block->AddChild(block_sp);
1156 block = block_sp.get();
1157 }
1158 DWARFRangeList ranges;
1159 const char *name = NULL;
1160 const char *mangled_name = NULL;
1161
1162 int decl_file = 0;
1163 int decl_line = 0;
1164 int decl_column = 0;
1165 int call_file = 0;
1166 int call_line = 0;
1167 int call_column = 0;
1168 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1169 decl_line, decl_column, call_file, call_line,
1170 call_column, nullptr)) {
1171 if (tag == DW_TAG_subprogram) {
1172 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1173 subprogram_low_pc = ranges.GetMinRangeBase(0);
1174 } else if (tag == DW_TAG_inlined_subroutine) {
1175 // We get called here for inlined subroutines in two ways.
1176 // The first time is when we are making the Function object
1177 // for this inlined concrete instance. Since we're creating a top
1178 // level block at
1179 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1180 // need to
1181 // adjust the containing address.
1182 // The second time is when we are parsing the blocks inside the
1183 // function that contains
1184 // the inlined concrete instance. Since these will be blocks inside
1185 // the containing "real"
1186 // function the offset will be for that function.
1187 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1188 subprogram_low_pc = ranges.GetMinRangeBase(0);
1189 }
1190 }
1191
1192 const size_t num_ranges = ranges.GetSize();
1193 for (size_t i = 0; i < num_ranges; ++i) {
1194 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1195 const addr_t range_base = range.GetRangeBase();
1196 if (range_base >= subprogram_low_pc)
1197 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1198 range.GetByteSize()));
1199 else {
1200 GetObjectFile()->GetModule()->ReportError(
1201 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1202 ") which has a base that is less than the function's low PC "
1203 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1204 "start of this error message",
1205 block->GetID(), range_base, range.GetRangeEnd(),
1206 subprogram_low_pc);
1207 }
1208 }
1209 block->FinalizeRanges();
1210
1211 if (tag != DW_TAG_subprogram &&
1212 (name != NULL || mangled_name != NULL)) {
1213 std::unique_ptr<Declaration> decl_ap;
1214 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1215 decl_ap.reset(new Declaration(
1216 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1217 decl_line, decl_column));
1218
1219 std::unique_ptr<Declaration> call_ap;
1220 if (call_file != 0 || call_line != 0 || call_column != 0)
1221 call_ap.reset(new Declaration(
1222 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1223 call_line, call_column));
1224
1225 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1226 call_ap.get());
1227 }
1228
1229 ++blocks_added;
1230
1231 if (die.HasChildren()) {
1232 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1233 subprogram_low_pc, depth + 1);
1234 }
1235 }
1236 } break;
1237 default:
1238 break;
1239 }
1240
1241 // Only parse siblings of the block if we are not at depth zero. A depth
1242 // of zero indicates we are currently parsing the top level
1243 // DW_TAG_subprogram DIE
1244
1245 if (depth == 0)
1246 die.Clear();
1247 else
1248 die = die.GetSibling();
1249 }
1250 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001251}
1252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1254 if (parent_die) {
1255 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1256 die = die.GetSibling()) {
1257 dw_tag_t tag = die.Tag();
1258 bool check_virtuality = false;
1259 switch (tag) {
1260 case DW_TAG_inheritance:
1261 case DW_TAG_subprogram:
1262 check_virtuality = true;
1263 break;
1264 default:
1265 break;
1266 }
1267 if (check_virtuality) {
1268 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1269 return true;
1270 }
1271 }
1272 }
1273 return false;
1274}
1275
1276void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1277 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1278 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1279 std::vector<DWARFDIE> decl_ctx_die_list =
1280 ast_parser->GetDIEForDeclContext(decl_ctx);
1281
1282 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1283 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1284 decl = decl.GetSibling())
1285 ast_parser->GetDeclForUIDFromDWARF(decl);
1286}
1287
1288SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1289 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1290 // we must make sure we use the correct DWARF file when resolving things.
1291 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1292 // SymbolFileDWARF classes, one for each .o file. We can often end up
1293 // with references to other DWARF objects and we must be ready to receive
1294 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1295 // instance.
1296 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1297 if (debug_map)
1298 return debug_map->GetSymbolFileByOSOIndex(
1299 debug_map->GetOSOIndexFromUserID(uid));
1300 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001301}
1302
1303DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1305 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1306 // we must make sure we use the correct DWARF file when resolving things.
1307 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1308 // SymbolFileDWARF classes, one for each .o file. We can often end up
1309 // with references to other DWARF objects and we must be ready to receive
1310 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1311 // instance.
1312 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1313 if (dwarf)
1314 return dwarf->GetDIE(DIERef(uid, dwarf));
1315 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001316}
1317
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1319 // Anytime we have a lldb::user_id_t, we must get the DIE by
1320 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1321 // the SymbolFileDWARF::GetDIEFromUID() for details.
1322 DWARFDIE die = GetDIEFromUID(type_uid);
1323 if (die)
1324 return die.GetDecl();
1325 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001326}
1327
Greg Clayton99558cc42015-08-24 23:46:31 +00001328CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1330 // Anytime we have a lldb::user_id_t, we must get the DIE by
1331 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1332 // the SymbolFileDWARF::GetDIEFromUID() for details.
1333 DWARFDIE die = GetDIEFromUID(type_uid);
1334 if (die)
1335 return die.GetDeclContext();
1336 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001337}
1338
Greg Clayton99558cc42015-08-24 23:46:31 +00001339CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1341 // Anytime we have a lldb::user_id_t, we must get the DIE by
1342 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1343 // the SymbolFileDWARF::GetDIEFromUID() for details.
1344 DWARFDIE die = GetDIEFromUID(type_uid);
1345 if (die)
1346 return die.GetContainingDeclContext();
1347 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348}
1349
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1351 // Anytime we have a lldb::user_id_t, we must get the DIE by
1352 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1353 // the SymbolFileDWARF::GetDIEFromUID() for details.
1354 DWARFDIE type_die = GetDIEFromUID(type_uid);
1355 if (type_die)
1356 return type_die.ResolveType();
1357 else
1358 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359}
1360
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1362 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001363}
1364
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1366 bool assert_not_being_parsed) {
1367 if (die) {
1368 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1369 if (log)
1370 GetObjectFile()->GetModule()->LogMessage(
1371 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1372 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 // We might be coming in in the middle of a type tree (a class
1375 // within a class, an enum within a class), so parse any needed
1376 // parent DIEs before we get to this one...
1377 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1378 if (decl_ctx_die) {
1379 if (log) {
1380 switch (decl_ctx_die.Tag()) {
1381 case DW_TAG_structure_type:
1382 case DW_TAG_union_type:
1383 case DW_TAG_class_type: {
1384 // Get the type, which could be a forward declaration
1385 if (log)
1386 GetObjectFile()->GetModule()->LogMessage(
1387 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1388 "resolve parent forward type for 0x%8.8x",
1389 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1390 decl_ctx_die.GetOffset());
1391 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 default:
1394 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001397 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 return ResolveType(die);
1399 }
1400 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001401}
1402
Greg Clayton6beaaa62011-01-17 03:46:26 +00001403// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1404// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001405// can resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406bool SymbolFileDWARF::HasForwardDeclForClangType(
1407 const CompilerType &compiler_type) {
1408 CompilerType compiler_type_no_qualifiers =
1409 ClangUtil::RemoveFastQualifiers(compiler_type);
1410 if (GetForwardDeclClangTypeToDie().count(
1411 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1412 return true;
1413 }
1414 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 ClangASTContext *clang_type_system =
1417 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1418 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001419 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 DWARFASTParserClang *ast_parser =
1421 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1422 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001423}
1424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1426 std::lock_guard<std::recursive_mutex> guard(
1427 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 ClangASTContext *clang_type_system =
1430 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1431 if (clang_type_system) {
1432 DWARFASTParserClang *ast_parser =
1433 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1434 if (ast_parser &&
1435 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1436 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1437 }
Jim Inghamc3549282012-01-11 02:21:12 +00001438
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 // We have a struct/union/class/enum that needs to be fully resolved.
1440 CompilerType compiler_type_no_qualifiers =
1441 ClangUtil::RemoveFastQualifiers(compiler_type);
1442 auto die_it = GetForwardDeclClangTypeToDie().find(
1443 compiler_type_no_qualifiers.GetOpaqueQualType());
1444 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1445 // We have already resolved this type...
1446 return true;
1447 }
1448
1449 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1450 if (dwarf_die) {
1451 // Once we start resolving this type, remove it from the forward declaration
1452 // map in case anyone child members or other types require this type to get
1453 // resolved.
1454 // The type will get resolved when all of the calls to
1455 // SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1456 // are done.
1457 GetForwardDeclClangTypeToDie().erase(die_it);
1458
1459 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1460
1461 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1462 DWARF_LOG_TYPE_COMPLETION));
1463 if (log)
1464 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1465 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1466 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1467 type->GetName().AsCString());
1468 assert(compiler_type);
1469 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1470 if (dwarf_ast)
1471 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1472 }
1473 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001474}
1475
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1477 bool assert_not_being_parsed,
1478 bool resolve_function_context) {
1479 if (die) {
1480 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1481
1482 if (assert_not_being_parsed) {
1483 if (type != DIE_IS_BEING_PARSED)
1484 return type;
1485
1486 GetObjectFile()->GetModule()->ReportError(
1487 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1488 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1489
1490 } else
1491 return type;
1492 }
1493 return nullptr;
1494}
1495
1496CompileUnit *
1497SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit *dwarf_cu,
1498 uint32_t cu_idx) {
1499 // Check if the symbol vendor already knows about this compile unit?
1500 if (dwarf_cu->GetUserData() == NULL) {
1501 // The symbol vendor doesn't know about this compile unit, we
1502 // need to parse and add it to the symbol vendor object.
1503 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1504 }
1505 return (CompileUnit *)dwarf_cu->GetUserData();
1506}
1507
1508size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1509 DIEArray &method_die_offsets) {
1510 method_die_offsets.clear();
1511 if (m_using_apple_tables) {
1512 if (m_apple_objc_ap.get())
1513 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1514 } else {
1515 if (!m_indexed)
1516 Index();
1517
1518 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1519 }
1520 return method_die_offsets.size();
1521}
1522
1523bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1524 sc.Clear(false);
1525
1526 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1529
1530 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1531 if (sc.function == NULL)
1532 sc.function = ParseCompileUnitFunction(sc, die);
1533
1534 if (sc.function) {
1535 sc.module_sp = sc.function->CalculateSymbolContextModule();
1536 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538 }
1539
1540 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541}
1542
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1544 UpdateExternalModuleListIfNeeded();
1545 const auto &pos = m_external_type_modules.find(name);
1546 if (pos != m_external_type_modules.end())
1547 return pos->second;
1548 else
1549 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001550}
1551
Greg Clayton2f869fe2016-03-30 20:14:35 +00001552DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1554 DWARFDebugInfo *debug_info = DebugInfo();
1555 if (debug_info)
1556 return debug_info->GetDIE(die_ref);
1557 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001558 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001559}
1560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561std::unique_ptr<SymbolFileDWARFDwo>
1562SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1563 DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1564 // If we are using a dSYM file, we never want the standard DWO files since
Adrian Prantldce4a9a2018-01-04 16:42:05 +00001565 // the -gmodules support uses the same DWO machanism to specify full debug
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 // info files for modules.
1567 if (GetDebugMapSymfile())
1568 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001569
Kate Stoneb9c1b512016-09-06 20:57:50 +00001570 const char *dwo_name = cu_die.GetAttributeValueAsString(
1571 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1572 if (!dwo_name)
1573 return nullptr;
1574
Tamas Berghammer963ce482017-08-25 13:56:14 +00001575 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile();
1576 if (dwp_symfile) {
1577 uint64_t dwo_id = cu_die.GetAttributeValueAsUnsigned(this, &dwarf_cu,
1578 DW_AT_GNU_dwo_id, 0);
1579 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile =
1580 dwp_symfile->GetSymbolFileForDwoId(&dwarf_cu, dwo_id);
1581 if (dwo_symfile)
1582 return dwo_symfile;
1583 }
1584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 FileSpec dwo_file(dwo_name, true);
1586 if (dwo_file.IsRelative()) {
1587 const char *comp_dir = cu_die.GetAttributeValueAsString(
1588 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1589 if (!comp_dir)
1590 return nullptr;
1591
1592 dwo_file.SetFile(comp_dir, true);
1593 dwo_file.AppendPathComponent(dwo_name);
1594 }
1595
1596 if (!dwo_file.Exists())
1597 return nullptr;
1598
1599 const lldb::offset_t file_offset = 0;
1600 DataBufferSP dwo_file_data_sp;
1601 lldb::offset_t dwo_file_data_offset = 0;
1602 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1603 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1604 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1605 if (dwo_obj_file == nullptr)
1606 return nullptr;
1607
1608 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001609}
1610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1612 if (m_fetched_external_modules)
1613 return;
1614 m_fetched_external_modules = true;
1615
1616 DWARFDebugInfo *debug_info = DebugInfo();
1617
1618 const uint32_t num_compile_units = GetNumCompileUnits();
1619 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1620 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1621
1622 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1623 if (die && die.HasChildren() == false) {
1624 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1625
1626 if (name) {
1627 ConstString const_name(name);
1628 if (m_external_type_modules.find(const_name) ==
1629 m_external_type_modules.end()) {
1630 ModuleSP module_sp;
1631 const char *dwo_path =
1632 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1633 if (dwo_path) {
1634 ModuleSpec dwo_module_spec;
1635 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1636 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1637 const char *comp_dir =
1638 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1639 if (comp_dir) {
1640 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1641 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1642 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001643 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 dwo_module_spec.GetArchitecture() =
1645 m_obj_file->GetModule()->GetArchitecture();
Alexander Shaposhnikovff7b03f2017-09-12 22:14:36 +00001646
1647 // When LLDB loads "external" modules it looks at the
1648 // presence of DW_AT_GNU_dwo_name.
1649 // However, when the already created module
1650 // (corresponding to .dwo itself) is being processed,
1651 // it will see the presence of DW_AT_GNU_dwo_name
1652 // (which contains the name of dwo file) and
1653 // will try to call ModuleList::GetSharedModule again.
1654 // In some cases (i.e. for empty files) Clang 4.0
1655 // generates a *.dwo file which has DW_AT_GNU_dwo_name,
1656 // but no DW_AT_comp_dir. In this case the method
1657 // ModuleList::GetSharedModule will fail and
1658 // the warning will be printed. However, as one can notice
1659 // in this case we don't actually need to try to load the already
1660 // loaded module (corresponding to .dwo) so we simply skip it.
1661 if (m_obj_file->GetFileSpec()
1662 .GetFileNameExtension()
1663 .GetStringRef() == "dwo" &&
1664 llvm::StringRef(m_obj_file->GetFileSpec().GetPath())
1665 .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1666 continue;
1667 }
1668
Zachary Turner97206d52017-05-12 04:51:55 +00001669 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 dwo_module_spec, module_sp, NULL, NULL, NULL);
1671 if (!module_sp) {
1672 GetObjectFile()->GetModule()->ReportWarning(
1673 "0x%8.8x: unable to locate module needed for external types: "
1674 "%s\nerror: %s\nDebugging will be degraded due to missing "
1675 "types. Rebuilding your project will regenerate the needed "
1676 "module files.",
1677 die.GetOffset(),
1678 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1679 error.AsCString("unknown error"));
1680 }
1681 }
1682 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001683 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 }
1687}
1688
1689SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1690 if (!m_global_aranges_ap) {
1691 m_global_aranges_ap.reset(new GlobalVariableMap());
1692
1693 ModuleSP module_sp = GetObjectFile()->GetModule();
1694 if (module_sp) {
1695 const size_t num_cus = module_sp->GetNumCompileUnits();
1696 for (size_t i = 0; i < num_cus; ++i) {
1697 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1698 if (cu_sp) {
1699 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1700 if (globals_sp) {
1701 const size_t num_globals = globals_sp->GetSize();
1702 for (size_t g = 0; g < num_globals; ++g) {
1703 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1704 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1705 const DWARFExpression &location = var_sp->LocationExpression();
1706 Value location_result;
Zachary Turner97206d52017-05-12 04:51:55 +00001707 Status error;
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001708 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1709 nullptr, location_result, &error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 if (location_result.GetValueType() ==
1711 Value::eValueTypeFileAddress) {
1712 lldb::addr_t file_addr =
1713 location_result.GetScalar().ULongLong();
1714 lldb::addr_t byte_size = 1;
1715 if (var_sp->GetType())
1716 byte_size = var_sp->GetType()->GetByteSize();
1717 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1718 file_addr, byte_size, var_sp.get()));
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 }
1727 m_global_aranges_ap->Sort();
1728 }
1729 return *m_global_aranges_ap;
1730}
1731
1732uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1733 uint32_t resolve_scope,
1734 SymbolContext &sc) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001735 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1736 Timer scoped_timer(func_cat,
1737 "SymbolFileDWARF::"
1738 "ResolveSymbolContext (so_addr = { "
1739 "section = %p, offset = 0x%" PRIx64
1740 " }, resolve_scope = 0x%8.8x)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 static_cast<void *>(so_addr.GetSection().get()),
1742 so_addr.GetOffset(), resolve_scope);
1743 uint32_t resolved = 0;
1744 if (resolve_scope &
1745 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1746 eSymbolContextLineEntry | eSymbolContextVariable)) {
1747 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1748
1749 DWARFDebugInfo *debug_info = DebugInfo();
1750 if (debug_info) {
1751 const dw_offset_t cu_offset =
1752 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1753 if (cu_offset == DW_INVALID_OFFSET) {
1754 // Global variables are not in the compile unit address ranges. The only
1755 // way to
1756 // currently find global variables is to iterate over the
1757 // .debug_pubnames or the
1758 // __apple_names table and find all items in there that point to
1759 // DW_TAG_variable
1760 // DIEs and then find the address that matches.
1761 if (resolve_scope & eSymbolContextVariable) {
1762 GlobalVariableMap &map = GetGlobalAranges();
1763 const GlobalVariableMap::Entry *entry =
1764 map.FindEntryThatContains(file_vm_addr);
1765 if (entry && entry->data) {
1766 Variable *variable = entry->data;
1767 SymbolContextScope *scc = variable->GetSymbolContextScope();
1768 if (scc) {
1769 scc->CalculateSymbolContext(&sc);
1770 sc.variable = variable;
1771 }
1772 return sc.GetResolvedMask();
1773 }
1774 }
1775 } else {
1776 uint32_t cu_idx = DW_INVALID_INDEX;
1777 DWARFCompileUnit *dwarf_cu =
1778 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1779 if (dwarf_cu) {
1780 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1781 if (sc.comp_unit) {
1782 resolved |= eSymbolContextCompUnit;
1783
1784 bool force_check_line_table = false;
1785 if (resolve_scope &
1786 (eSymbolContextFunction | eSymbolContextBlock)) {
1787 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1788 DWARFDIE block_die;
1789 if (function_die) {
1790 sc.function =
1791 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1792 if (sc.function == NULL)
1793 sc.function = ParseCompileUnitFunction(sc, function_die);
1794
1795 if (sc.function && (resolve_scope & eSymbolContextBlock))
1796 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1797 } else {
1798 // We might have had a compile unit that had discontiguous
1799 // address ranges where the gaps are symbols that don't have
1800 // any debug info. Discontiguous compile unit address ranges
1801 // should only happen when there aren't other functions from
1802 // other compile units in these gaps. This helps keep the size
1803 // of the aranges down.
1804 force_check_line_table = true;
1805 }
1806
1807 if (sc.function != NULL) {
1808 resolved |= eSymbolContextFunction;
1809
1810 if (resolve_scope & eSymbolContextBlock) {
1811 Block &block = sc.function->GetBlock(true);
1812
1813 if (block_die)
1814 sc.block = block.FindBlockByID(block_die.GetID());
1815 else
1816 sc.block = block.FindBlockByID(function_die.GetID());
1817 if (sc.block)
1818 resolved |= eSymbolContextBlock;
1819 }
1820 }
1821 }
1822
1823 if ((resolve_scope & eSymbolContextLineEntry) ||
1824 force_check_line_table) {
1825 LineTable *line_table = sc.comp_unit->GetLineTable();
1826 if (line_table != NULL) {
1827 // And address that makes it into this function should be in
1828 // terms
1829 // of this debug file if there is no debug map, or it will be an
1830 // address in the .o file which needs to be fixed up to be in
1831 // terms
1832 // of the debug map executable. Either way, calling
1833 // FixupAddress()
1834 // will work for us.
1835 Address exe_so_addr(so_addr);
1836 if (FixupAddress(exe_so_addr)) {
1837 if (line_table->FindLineEntryByAddress(exe_so_addr,
1838 sc.line_entry)) {
1839 resolved |= eSymbolContextLineEntry;
1840 }
1841 }
1842 }
1843 }
1844
1845 if (force_check_line_table &&
1846 !(resolved & eSymbolContextLineEntry)) {
1847 // We might have had a compile unit that had discontiguous
1848 // address ranges where the gaps are symbols that don't have
1849 // any debug info. Discontiguous compile unit address ranges
1850 // should only happen when there aren't other functions from
1851 // other compile units in these gaps. This helps keep the size
1852 // of the aranges down.
1853 sc.comp_unit = NULL;
1854 resolved &= ~eSymbolContextCompUnit;
1855 }
1856 } else {
1857 GetObjectFile()->GetModule()->ReportWarning(
1858 "0x%8.8x: compile unit %u failed to create a valid "
1859 "lldb_private::CompileUnit class.",
1860 cu_offset, cu_idx);
1861 }
1862 }
1863 }
1864 }
1865 }
1866 return resolved;
1867}
1868
1869uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1870 uint32_t line,
1871 bool check_inlines,
1872 uint32_t resolve_scope,
1873 SymbolContextList &sc_list) {
1874 const uint32_t prev_size = sc_list.GetSize();
1875 if (resolve_scope & eSymbolContextCompUnit) {
1876 DWARFDebugInfo *debug_info = DebugInfo();
1877 if (debug_info) {
1878 uint32_t cu_idx;
1879 DWARFCompileUnit *dwarf_cu = NULL;
1880
1881 for (cu_idx = 0;
1882 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1883 ++cu_idx) {
1884 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1885 const bool full_match = (bool)file_spec.GetDirectory();
1886 bool file_spec_matches_cu_file_spec =
1887 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1888 if (check_inlines || file_spec_matches_cu_file_spec) {
1889 SymbolContext sc(m_obj_file->GetModule());
1890 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1891 if (sc.comp_unit) {
1892 uint32_t file_idx = UINT32_MAX;
1893
1894 // If we are looking for inline functions only and we don't
1895 // find it in the support files, we are done.
1896 if (check_inlines) {
1897 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1898 1, file_spec, true);
1899 if (file_idx == UINT32_MAX)
1900 continue;
1901 }
1902
1903 if (line != 0) {
1904 LineTable *line_table = sc.comp_unit->GetLineTable();
1905
1906 if (line_table != NULL && line != 0) {
1907 // We will have already looked up the file index if
1908 // we are searching for inline entries.
1909 if (!check_inlines)
1910 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1911 1, file_spec, true);
1912
1913 if (file_idx != UINT32_MAX) {
1914 uint32_t found_line;
1915 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1916 0, file_idx, line, false, &sc.line_entry);
1917 found_line = sc.line_entry.line;
1918
1919 while (line_idx != UINT32_MAX) {
1920 sc.function = NULL;
1921 sc.block = NULL;
1922 if (resolve_scope &
1923 (eSymbolContextFunction | eSymbolContextBlock)) {
1924 const lldb::addr_t file_vm_addr =
1925 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1926 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1927 DWARFDIE function_die =
1928 dwarf_cu->LookupAddress(file_vm_addr);
1929 DWARFDIE block_die;
1930 if (function_die) {
1931 sc.function =
1932 sc.comp_unit
1933 ->FindFunctionByUID(function_die.GetID())
1934 .get();
1935 if (sc.function == NULL)
1936 sc.function =
1937 ParseCompileUnitFunction(sc, function_die);
1938
1939 if (sc.function &&
1940 (resolve_scope & eSymbolContextBlock))
1941 block_die =
1942 function_die.LookupDeepestBlock(file_vm_addr);
1943 }
1944
1945 if (sc.function != NULL) {
1946 Block &block = sc.function->GetBlock(true);
1947
1948 if (block_die)
1949 sc.block = block.FindBlockByID(block_die.GetID());
1950 else if (function_die)
1951 sc.block =
1952 block.FindBlockByID(function_die.GetID());
1953 }
1954 }
1955 }
1956
1957 sc_list.Append(sc);
1958 line_idx = line_table->FindLineEntryIndexByFileIndex(
1959 line_idx + 1, file_idx, found_line, true,
1960 &sc.line_entry);
1961 }
1962 }
1963 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1964 // only append the context if we aren't looking for inline call
1965 // sites
1966 // by file and line and if the file spec matches that of the
1967 // compile unit
1968 sc_list.Append(sc);
1969 }
1970 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1971 // only append the context if we aren't looking for inline call
1972 // sites
1973 // by file and line and if the file spec matches that of the
1974 // compile unit
1975 sc_list.Append(sc);
1976 }
1977
1978 if (!check_inlines)
1979 break;
1980 }
1981 }
1982 }
1983 }
1984 }
1985 return sc_list.GetSize() - prev_size;
1986}
1987
Jim Ingham7fca8c02017-04-28 00:51:06 +00001988void SymbolFileDWARF::PreloadSymbols() {
1989 std::lock_guard<std::recursive_mutex> guard(
1990 GetObjectFile()->GetModule()->GetMutex());
1991 Index();
1992}
1993
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994void SymbolFileDWARF::Index() {
1995 if (m_indexed)
1996 return;
1997 m_indexed = true;
Pavel Labathf9d16472017-05-15 13:02:37 +00001998 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001999 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +00002000 func_cat, "SymbolFileDWARF::Index (%s)",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002001 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
2002
2003 DWARFDebugInfo *debug_info = DebugInfo();
2004 if (debug_info) {
2005 const uint32_t num_compile_units = GetNumCompileUnits();
2006 if (num_compile_units == 0)
2007 return;
2008
2009 std::vector<NameToDIE> function_basename_index(num_compile_units);
2010 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2011 std::vector<NameToDIE> function_method_index(num_compile_units);
2012 std::vector<NameToDIE> function_selector_index(num_compile_units);
2013 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2014 std::vector<NameToDIE> global_index(num_compile_units);
2015 std::vector<NameToDIE> type_index(num_compile_units);
2016 std::vector<NameToDIE> namespace_index(num_compile_units);
2017
Pavel Labath5c913e92017-05-05 11:16:59 +00002018 // std::vector<bool> might be implemented using bit test-and-set, so use
2019 // uint8_t instead.
2020 std::vector<uint8_t> clear_cu_dies(num_compile_units, false);
Zachary Turner3bc714b2017-03-02 00:05:25 +00002021 auto parser_fn = [debug_info, &function_basename_index,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022 &function_fullname_index, &function_method_index,
2023 &function_selector_index, &objc_class_selectors_index,
2024 &global_index, &type_index,
Pavel Labath92f66772017-06-07 16:28:08 +00002025 &namespace_index](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002026 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2027 if (dwarf_cu) {
2028 dwarf_cu->Index(
2029 function_basename_index[cu_idx], function_fullname_index[cu_idx],
2030 function_method_index[cu_idx], function_selector_index[cu_idx],
2031 objc_class_selectors_index[cu_idx], global_index[cu_idx],
2032 type_index[cu_idx], namespace_index[cu_idx]);
2033 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002034 };
2035
Pavel Labath92f66772017-06-07 16:28:08 +00002036 auto extract_fn = [debug_info, &clear_cu_dies](size_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002037 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
2038 if (dwarf_cu) {
2039 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the
2040 // DIEs for a compile unit have already been parsed.
Pavel Labath5c913e92017-05-05 11:16:59 +00002041 if (dwarf_cu->ExtractDIEsIfNeeded(false) > 1)
2042 clear_cu_dies[cu_idx] = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044 };
2045
2046 // Create a task runner that extracts dies for each DWARF compile unit in a
2047 // separate thread
Kate Stoneb9c1b512016-09-06 20:57:50 +00002048 //----------------------------------------------------------------------
2049 // First figure out which compile units didn't have their DIEs already
2050 // parsed and remember this. If no DIEs were parsed prior to this index
2051 // function call, we are going to want to clear the CU dies after we
2052 // are done indexing to make sure we don't pull in all DWARF dies, but
2053 // we need to wait until all compile units have been indexed in case
2054 // a DIE in one compile unit refers to another and the indexes accesses
2055 // those DIEs.
2056 //----------------------------------------------------------------------
Pavel Labath5c913e92017-05-05 11:16:59 +00002057 TaskMapOverInt(0, num_compile_units, extract_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002058
2059 // Now create a task runner that can index each DWARF compile unit in a
2060 // separate
2061 // thread so we can index quickly.
2062
Pavel Labath5c913e92017-05-05 11:16:59 +00002063 TaskMapOverInt(0, num_compile_units, parser_fn);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002064
Pavel Labath5c913e92017-05-05 11:16:59 +00002065 auto finalize_fn = [](NameToDIE &index, std::vector<NameToDIE> &srcs) {
2066 for (auto &src : srcs)
2067 index.Append(src);
2068 index.Finalize();
2069 };
Kate Stoneb9c1b512016-09-06 20:57:50 +00002070
Pavel Labath5c913e92017-05-05 11:16:59 +00002071 TaskPool::RunTasks(
2072 [&]() {
2073 finalize_fn(m_function_basename_index, function_basename_index);
2074 },
2075 [&]() {
2076 finalize_fn(m_function_fullname_index, function_fullname_index);
2077 },
2078 [&]() { finalize_fn(m_function_method_index, function_method_index); },
2079 [&]() {
2080 finalize_fn(m_function_selector_index, function_selector_index);
2081 },
2082 [&]() {
2083 finalize_fn(m_objc_class_selectors_index, objc_class_selectors_index);
2084 },
2085 [&]() { finalize_fn(m_global_index, global_index); },
2086 [&]() { finalize_fn(m_type_index, type_index); },
2087 [&]() { finalize_fn(m_namespace_index, namespace_index); });
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088
2089 //----------------------------------------------------------------------
2090 // Keep memory down by clearing DIEs for any compile units if indexing
2091 // caused us to load the compile unit's DIEs.
2092 //----------------------------------------------------------------------
2093 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2094 if (clear_cu_dies[cu_idx])
2095 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2096 }
2097
2098#if defined(ENABLE_DEBUG_PRINTF)
2099 StreamFile s(stdout, false);
2100 s.Printf("DWARF index for '%s':",
2101 GetObjectFile()->GetFileSpec().GetPath().c_str());
2102 s.Printf("\nFunction basenames:\n");
2103 m_function_basename_index.Dump(&s);
2104 s.Printf("\nFunction fullnames:\n");
2105 m_function_fullname_index.Dump(&s);
2106 s.Printf("\nFunction methods:\n");
2107 m_function_method_index.Dump(&s);
2108 s.Printf("\nFunction selectors:\n");
2109 m_function_selector_index.Dump(&s);
2110 s.Printf("\nObjective C class selectors:\n");
2111 m_objc_class_selectors_index.Dump(&s);
2112 s.Printf("\nGlobals and statics:\n");
2113 m_global_index.Dump(&s);
2114 s.Printf("\nTypes:\n");
2115 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002116 s.Printf("\nNamespaces:\n");
2117 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002118#endif
2119 }
2120}
2121
2122bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2123 const lldb_private::CompilerDeclContext *decl_ctx) {
2124 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
2125 // Invalid namespace decl which means we aren't matching only things
2126 // in this symbol file, so return true to indicate it matches this
2127 // symbol file.
2128 return true;
2129 }
2130
2131 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2132 TypeSystem *type_system = GetTypeSystemForLanguage(
2133 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2134 if (decl_ctx_type_system == type_system)
2135 return true; // The type systems match, return true
2136
2137 // The namespace AST was valid, and it does not match...
2138 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2139
2140 if (log)
2141 GetObjectFile()->GetModule()->LogMessage(
2142 log, "Valid namespace does not match symbol file");
2143
2144 return false;
2145}
2146
2147uint32_t SymbolFileDWARF::FindGlobalVariables(
2148 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2149 bool append, uint32_t max_matches, VariableList &variables) {
2150 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2151
2152 if (log)
2153 GetObjectFile()->GetModule()->LogMessage(
2154 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2155 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2156 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2157 max_matches);
2158
2159 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2160 return 0;
2161
2162 DWARFDebugInfo *info = DebugInfo();
2163 if (info == NULL)
2164 return 0;
2165
2166 // If we aren't appending the results to this list, then clear the list
2167 if (!append)
2168 variables.Clear();
2169
2170 // Remember how many variables are in the list before we search in case
2171 // we are appending the results to a variable list.
2172 const uint32_t original_size = variables.GetSize();
2173
2174 DIEArray die_offsets;
2175
2176 if (m_using_apple_tables) {
2177 if (m_apple_names_ap.get()) {
2178 const char *name_cstr = name.GetCString();
2179 llvm::StringRef basename;
2180 llvm::StringRef context;
2181
2182 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2183 basename))
2184 basename = name_cstr;
2185
2186 m_apple_names_ap->FindByName(basename.data(), die_offsets);
2187 }
2188 } else {
2189 // Index the DWARF if we haven't already
2190 if (!m_indexed)
2191 Index();
2192
2193 m_global_index.Find(name, die_offsets);
2194 }
2195
2196 const size_t num_die_matches = die_offsets.size();
2197 if (num_die_matches) {
2198 SymbolContext sc;
2199 sc.module_sp = m_obj_file->GetModule();
2200 assert(sc.module_sp);
2201
2202 bool done = false;
2203 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2204 const DIERef &die_ref = die_offsets[i];
2205 DWARFDIE die = GetDIE(die_ref);
2206
2207 if (die) {
2208 switch (die.Tag()) {
2209 default:
2210 case DW_TAG_subprogram:
2211 case DW_TAG_inlined_subroutine:
2212 case DW_TAG_try_block:
2213 case DW_TAG_catch_block:
2214 break;
2215
2216 case DW_TAG_variable: {
2217 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2218
2219 if (parent_decl_ctx) {
2220 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2221 if (dwarf_ast) {
2222 CompilerDeclContext actual_parent_decl_ctx =
2223 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2224 if (!actual_parent_decl_ctx ||
2225 actual_parent_decl_ctx != *parent_decl_ctx)
2226 continue;
2227 }
2228 }
2229
2230 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2231 &variables);
2232
2233 if (variables.GetSize() - original_size >= max_matches)
2234 done = true;
2235 } break;
2236 }
2237 } else {
2238 if (m_using_apple_tables) {
2239 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2240 "the DWARF debug information has been modified (.apple_names "
2241 "accelerator table had bad die 0x%8.8x for '%s')\n",
2242 die_ref.die_offset, name.GetCString());
2243 }
2244 }
2245 }
2246 }
2247
2248 // Return the number of variable that were appended to the list
2249 const uint32_t num_matches = variables.GetSize() - original_size;
2250 if (log && num_matches > 0) {
2251 GetObjectFile()->GetModule()->LogMessage(
2252 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2253 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2254 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2255 max_matches, num_matches);
2256 }
2257 return num_matches;
2258}
2259
2260uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2261 bool append, uint32_t max_matches,
2262 VariableList &variables) {
2263 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2264
2265 if (log) {
2266 GetObjectFile()->GetModule()->LogMessage(
2267 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2268 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002269 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270 }
2271
2272 DWARFDebugInfo *info = DebugInfo();
2273 if (info == NULL)
2274 return 0;
2275
2276 // If we aren't appending the results to this list, then clear the list
2277 if (!append)
2278 variables.Clear();
2279
2280 // Remember how many variables are in the list before we search in case
2281 // we are appending the results to a variable list.
2282 const uint32_t original_size = variables.GetSize();
2283
2284 DIEArray die_offsets;
2285
2286 if (m_using_apple_tables) {
2287 if (m_apple_names_ap.get()) {
2288 DWARFMappedHash::DIEInfoArray hash_data_array;
2289 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2290 hash_data_array))
2291 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2292 }
2293 } else {
2294 // Index the DWARF if we haven't already
2295 if (!m_indexed)
2296 Index();
2297
2298 m_global_index.Find(regex, die_offsets);
2299 }
2300
2301 SymbolContext sc;
2302 sc.module_sp = m_obj_file->GetModule();
2303 assert(sc.module_sp);
2304
2305 const size_t num_matches = die_offsets.size();
2306 if (num_matches) {
2307 for (size_t i = 0; i < num_matches; ++i) {
2308 const DIERef &die_ref = die_offsets[i];
2309 DWARFDIE die = GetDIE(die_ref);
2310
2311 if (die) {
2312 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2313
2314 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2315
2316 if (variables.GetSize() - original_size >= max_matches)
2317 break;
2318 } else {
2319 if (m_using_apple_tables) {
2320 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2321 "the DWARF debug information has been modified (.apple_names "
2322 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002323 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 }
2325 }
2326 }
2327 }
2328
2329 // Return the number of variable that were appended to the list
2330 return variables.GetSize() - original_size;
2331}
2332
2333bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2334 bool include_inlines,
2335 SymbolContextList &sc_list) {
2336 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2337 return ResolveFunction(die, include_inlines, sc_list);
2338}
2339
2340bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2341 bool include_inlines,
2342 SymbolContextList &sc_list) {
2343 SymbolContext sc;
2344
2345 if (!orig_die)
2346 return false;
2347
2348 // If we were passed a die that is not a function, just return false...
2349 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2350 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2351 return false;
2352
2353 DWARFDIE die = orig_die;
2354 DWARFDIE inlined_die;
2355 if (die.Tag() == DW_TAG_inlined_subroutine) {
2356 inlined_die = die;
2357
2358 while (1) {
2359 die = die.GetParent();
2360
2361 if (die) {
2362 if (die.Tag() == DW_TAG_subprogram)
2363 break;
2364 } else
2365 break;
2366 }
2367 }
2368 assert(die && die.Tag() == DW_TAG_subprogram);
2369 if (GetFunction(die, sc)) {
2370 Address addr;
2371 // Parse all blocks if needed
2372 if (inlined_die) {
2373 Block &function_block = sc.function->GetBlock(true);
2374 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2375 if (sc.block == NULL)
2376 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2377 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2378 addr.Clear();
2379 } else {
2380 sc.block = NULL;
2381 addr = sc.function->GetAddressRange().GetBaseAddress();
2382 }
2383
2384 if (addr.IsValid()) {
2385 sc_list.Append(sc);
2386 return true;
2387 }
2388 }
2389
2390 return false;
2391}
2392
2393void SymbolFileDWARF::FindFunctions(const ConstString &name,
2394 const NameToDIE &name_to_die,
2395 bool include_inlines,
2396 SymbolContextList &sc_list) {
2397 DIEArray die_offsets;
2398 if (name_to_die.Find(name, die_offsets)) {
2399 ParseFunctions(die_offsets, include_inlines, sc_list);
2400 }
2401}
2402
2403void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2404 const NameToDIE &name_to_die,
2405 bool include_inlines,
2406 SymbolContextList &sc_list) {
2407 DIEArray die_offsets;
2408 if (name_to_die.Find(regex, die_offsets)) {
2409 ParseFunctions(die_offsets, include_inlines, sc_list);
2410 }
2411}
2412
2413void SymbolFileDWARF::FindFunctions(
2414 const RegularExpression &regex,
2415 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2416 SymbolContextList &sc_list) {
2417 DIEArray die_offsets;
2418 DWARFMappedHash::DIEInfoArray hash_data_array;
2419 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2420 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2421 ParseFunctions(die_offsets, include_inlines, sc_list);
2422 }
2423}
2424
2425void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2426 bool include_inlines,
2427 SymbolContextList &sc_list) {
2428 const size_t num_matches = die_offsets.size();
2429 if (num_matches) {
2430 for (size_t i = 0; i < num_matches; ++i)
2431 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2432 }
2433}
2434
2435bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2436 const DWARFDIE &die) {
2437 // If we have no parent decl context to match this DIE matches, and if the
2438 // parent
2439 // decl context isn't valid, we aren't trying to look for any particular decl
2440 // context so any die matches.
2441 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2442 return true;
2443
2444 if (die) {
2445 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2446 if (dwarf_ast) {
2447 CompilerDeclContext actual_decl_ctx =
2448 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2449 if (actual_decl_ctx)
2450 return actual_decl_ctx == *decl_ctx;
2451 }
2452 }
2453 return false;
2454}
2455
2456uint32_t
2457SymbolFileDWARF::FindFunctions(const ConstString &name,
2458 const CompilerDeclContext *parent_decl_ctx,
2459 uint32_t name_type_mask, bool include_inlines,
2460 bool append, SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002461 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2462 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002463 name.AsCString());
2464
2465 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002466 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2468
2469 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2470
2471 if (log) {
2472 GetObjectFile()->GetModule()->LogMessage(
2473 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2474 "name_type_mask=0x%x, append=%u, sc_list)",
2475 name.GetCString(), name_type_mask, append);
2476 }
2477
2478 // If we aren't appending the results to this list, then clear the list
2479 if (!append)
2480 sc_list.Clear();
2481
2482 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2483 return 0;
2484
2485 // If name is empty then we won't find anything.
2486 if (name.IsEmpty())
2487 return 0;
2488
2489 // Remember how many sc_list are in the list before we search in case
2490 // we are appending the results to a variable list.
2491
2492 const char *name_cstr = name.GetCString();
2493
2494 const uint32_t original_size = sc_list.GetSize();
2495
2496 DWARFDebugInfo *info = DebugInfo();
2497 if (info == NULL)
2498 return 0;
2499
2500 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2501 if (m_using_apple_tables) {
2502 if (m_apple_names_ap.get()) {
2503
2504 DIEArray die_offsets;
2505
2506 uint32_t num_matches = 0;
2507
2508 if (name_type_mask & eFunctionNameTypeFull) {
2509 // If they asked for the full name, match what they typed. At some
2510 // point we may
2511 // want to canonicalize this (strip double spaces, etc. For now, we
2512 // just add all the
2513 // dies that we find by exact match.
2514 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2515 for (uint32_t i = 0; i < num_matches; i++) {
2516 const DIERef &die_ref = die_offsets[i];
2517 DWARFDIE die = info->GetDIE(die_ref);
2518 if (die) {
2519 if (!DIEInDeclContext(parent_decl_ctx, die))
2520 continue; // The containing decl contexts don't match
2521
2522 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2523 if (ResolveFunction(die, include_inlines, sc_list))
2524 resolved_dies.insert(die.GetDIE());
2525 }
2526 } else {
2527 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2528 "the DWARF debug information has been modified (.apple_names "
2529 "accelerator table had bad die 0x%8.8x for '%s')",
2530 die_ref.die_offset, name_cstr);
2531 }
2532 }
2533 }
2534
2535 if (name_type_mask & eFunctionNameTypeSelector) {
2536 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2537 return 0; // no selectors in namespaces
2538
2539 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2540 // Now make sure these are actually ObjC methods. In this case we can
2541 // simply look up the name,
2542 // and if it is an ObjC method name, we're good.
2543
2544 for (uint32_t i = 0; i < num_matches; i++) {
2545 const DIERef &die_ref = die_offsets[i];
2546 DWARFDIE die = info->GetDIE(die_ref);
2547 if (die) {
2548 const char *die_name = die.GetName();
2549 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2550 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2551 if (ResolveFunction(die, include_inlines, sc_list))
2552 resolved_dies.insert(die.GetDIE());
2553 }
2554 }
2555 } else {
2556 GetObjectFile()->GetModule()->ReportError(
2557 "the DWARF debug information has been modified (.apple_names "
2558 "accelerator table had bad die 0x%8.8x for '%s')",
2559 die_ref.die_offset, name_cstr);
2560 }
2561 }
2562 die_offsets.clear();
2563 }
2564
2565 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2566 name_type_mask & eFunctionNameTypeBase) {
2567 // The apple_names table stores just the "base name" of C++ methods in
2568 // the table. So we have to
2569 // extract the base name, look that up, and if there is any other
2570 // information in the name we were
2571 // passed in we have to post-filter based on that.
2572
2573 // FIXME: Arrange the logic above so that we don't calculate the base
2574 // name twice:
2575 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2576
2577 for (uint32_t i = 0; i < num_matches; i++) {
2578 const DIERef &die_ref = die_offsets[i];
2579 DWARFDIE die = info->GetDIE(die_ref);
2580 if (die) {
2581 if (!DIEInDeclContext(parent_decl_ctx, die))
2582 continue; // The containing decl contexts don't match
2583
2584 // If we get to here, the die is good, and we should add it:
2585 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2586 ResolveFunction(die, include_inlines, sc_list)) {
2587 bool keep_die = true;
2588 if ((name_type_mask &
2589 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2590 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
2591 // We are looking for either basenames or methods, so we need to
2592 // trim out the ones we won't want by looking at the type
2593 SymbolContext sc;
2594 if (sc_list.GetLastContext(sc)) {
2595 if (sc.block) {
2596 // We have an inlined function
2597 } else if (sc.function) {
2598 Type *type = sc.function->GetType();
2599
2600 if (type) {
2601 CompilerDeclContext decl_ctx =
2602 GetDeclContextContainingUID(type->GetID());
2603 if (decl_ctx.IsStructUnionOrClass()) {
2604 if (name_type_mask & eFunctionNameTypeBase) {
2605 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2606 keep_die = false;
2607 }
2608 } else {
2609 if (name_type_mask & eFunctionNameTypeMethod) {
2610 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2611 keep_die = false;
2612 }
2613 }
2614 } else {
2615 GetObjectFile()->GetModule()->ReportWarning(
2616 "function at die offset 0x%8.8x had no function type",
2617 die_ref.die_offset);
2618 }
2619 }
2620 }
2621 }
2622 if (keep_die)
2623 resolved_dies.insert(die.GetDIE());
2624 }
2625 } else {
2626 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2627 "the DWARF debug information has been modified (.apple_names "
2628 "accelerator table had bad die 0x%8.8x for '%s')",
2629 die_ref.die_offset, name_cstr);
2630 }
2631 }
2632 die_offsets.clear();
2633 }
2634 }
2635 } else {
2636
2637 // Index the DWARF if we haven't already
2638 if (!m_indexed)
2639 Index();
2640
2641 if (name_type_mask & eFunctionNameTypeFull) {
2642 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2643
2644 // FIXME Temporary workaround for global/anonymous namespace
2645 // functions debugging FreeBSD and Linux binaries.
2646 // If we didn't find any functions in the global namespace try
2647 // looking in the basename index but ignore any returned
2648 // functions that have a namespace but keep functions which
2649 // have an anonymous namespace
2650 // TODO: The arch in the object file isn't correct for MSVC
2651 // binaries on windows, we should find a way to make it
2652 // correct and handle those symbols as well.
2653 if (sc_list.GetSize() == original_size) {
2654 ArchSpec arch;
2655 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002656 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002657 SymbolContextList temp_sc_list;
2658 FindFunctions(name, m_function_basename_index, include_inlines,
2659 temp_sc_list);
2660 SymbolContext sc;
2661 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2662 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2663 ConstString mangled_name =
2664 sc.GetFunctionName(Mangled::ePreferMangled);
2665 ConstString demangled_name =
2666 sc.GetFunctionName(Mangled::ePreferDemangled);
2667 // Mangled names on Linux and FreeBSD are of the form:
2668 // _ZN18function_namespace13function_nameEv.
2669 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2670 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2671 21)) {
2672 sc_list.Append(sc);
2673 }
2674 }
2675 }
2676 }
2677 }
2678 }
2679 DIEArray die_offsets;
2680 if (name_type_mask & eFunctionNameTypeBase) {
2681 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2682 for (uint32_t i = 0; i < num_base; i++) {
2683 DWARFDIE die = info->GetDIE(die_offsets[i]);
2684 if (die) {
2685 if (!DIEInDeclContext(parent_decl_ctx, die))
2686 continue; // The containing decl contexts don't match
2687
2688 // If we get to here, the die is good, and we should add it:
2689 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2690 if (ResolveFunction(die, include_inlines, sc_list))
2691 resolved_dies.insert(die.GetDIE());
2692 }
2693 }
2694 }
2695 die_offsets.clear();
2696 }
2697
2698 if (name_type_mask & eFunctionNameTypeMethod) {
2699 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2700 return 0; // no methods in namespaces
2701
2702 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2703 {
2704 for (uint32_t i = 0; i < num_base; i++) {
2705 DWARFDIE die = info->GetDIE(die_offsets[i]);
2706 if (die) {
2707 // If we get to here, the die is good, and we should add it:
2708 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2709 if (ResolveFunction(die, include_inlines, sc_list))
2710 resolved_dies.insert(die.GetDIE());
2711 }
2712 }
2713 }
2714 }
2715 die_offsets.clear();
2716 }
2717
2718 if ((name_type_mask & eFunctionNameTypeSelector) &&
2719 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2720 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2721 }
2722 }
2723
2724 // Return the number of variable that were appended to the list
2725 const uint32_t num_matches = sc_list.GetSize() - original_size;
2726
2727 if (log && num_matches > 0) {
2728 GetObjectFile()->GetModule()->LogMessage(
2729 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2730 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2731 "%u",
2732 name.GetCString(), name_type_mask, include_inlines, append,
2733 num_matches);
2734 }
2735 return num_matches;
2736}
2737
2738uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2739 bool include_inlines, bool append,
2740 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00002741 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2742 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002743 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002744
2745 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2746
2747 if (log) {
2748 GetObjectFile()->GetModule()->LogMessage(
2749 log,
2750 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002751 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002752 }
2753
2754 // If we aren't appending the results to this list, then clear the list
2755 if (!append)
2756 sc_list.Clear();
2757
2758 // Remember how many sc_list are in the list before we search in case
2759 // we are appending the results to a variable list.
2760 uint32_t original_size = sc_list.GetSize();
2761
2762 if (m_using_apple_tables) {
2763 if (m_apple_names_ap.get())
2764 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2765 } else {
2766 // Index the DWARF if we haven't already
2767 if (!m_indexed)
2768 Index();
2769
2770 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2771
2772 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2773 }
2774
2775 // Return the number of variable that were appended to the list
2776 return sc_list.GetSize() - original_size;
2777}
2778
2779void SymbolFileDWARF::GetMangledNamesForFunction(
2780 const std::string &scope_qualified_name,
2781 std::vector<ConstString> &mangled_names) {
2782 DWARFDebugInfo *info = DebugInfo();
2783 uint32_t num_comp_units = 0;
2784 if (info)
2785 num_comp_units = info->GetNumCompileUnits();
2786
2787 for (uint32_t i = 0; i < num_comp_units; i++) {
2788 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2789 if (cu == nullptr)
2790 continue;
2791
2792 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2793 if (dwo)
2794 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2795 }
2796
2797 NameToOffsetMap::iterator iter =
2798 m_function_scope_qualified_name_map.find(scope_qualified_name);
2799 if (iter == m_function_scope_qualified_name_map.end())
2800 return;
2801
2802 DIERefSetSP set_sp = (*iter).second;
2803 std::set<DIERef>::iterator set_iter;
2804 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2805 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2806 mangled_names.push_back(ConstString(die.GetMangledName()));
2807 }
2808}
2809
2810uint32_t SymbolFileDWARF::FindTypes(
2811 const SymbolContext &sc, const ConstString &name,
2812 const CompilerDeclContext *parent_decl_ctx, bool append,
2813 uint32_t max_matches,
2814 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2815 TypeMap &types) {
2816 // If we aren't appending the results to this list, then clear the list
2817 if (!append)
2818 types.Clear();
2819
2820 // Make sure we haven't already searched this SymbolFile before...
2821 if (searched_symbol_files.count(this))
2822 return 0;
2823 else
2824 searched_symbol_files.insert(this);
2825
2826 DWARFDebugInfo *info = DebugInfo();
2827 if (info == NULL)
2828 return 0;
2829
2830 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2831
2832 if (log) {
2833 if (parent_decl_ctx)
2834 GetObjectFile()->GetModule()->LogMessage(
2835 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2836 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2837 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2838 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2839 else
2840 GetObjectFile()->GetModule()->LogMessage(
2841 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2842 "NULL, append=%u, max_matches=%u, type_list)",
2843 name.GetCString(), append, max_matches);
2844 }
2845
2846 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2847 return 0;
2848
2849 DIEArray die_offsets;
2850
2851 if (m_using_apple_tables) {
2852 if (m_apple_types_ap.get()) {
2853 const char *name_cstr = name.GetCString();
2854 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2855 }
2856 } else {
2857 if (!m_indexed)
2858 Index();
2859
2860 m_type_index.Find(name, die_offsets);
2861 }
2862
2863 const size_t num_die_matches = die_offsets.size();
2864
2865 if (num_die_matches) {
2866 const uint32_t initial_types_size = types.GetSize();
2867 for (size_t i = 0; i < num_die_matches; ++i) {
2868 const DIERef &die_ref = die_offsets[i];
2869 DWARFDIE die = GetDIE(die_ref);
2870
2871 if (die) {
2872 if (!DIEInDeclContext(parent_decl_ctx, die))
2873 continue; // The containing decl contexts don't match
2874
2875 Type *matching_type = ResolveType(die, true, true);
2876 if (matching_type) {
2877 // We found a type pointer, now find the shared pointer form our type
2878 // list
2879 types.InsertUnique(matching_type->shared_from_this());
2880 if (types.GetSize() >= max_matches)
2881 break;
2882 }
2883 } else {
2884 if (m_using_apple_tables) {
2885 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2886 "the DWARF debug information has been modified (.apple_types "
2887 "accelerator table had bad die 0x%8.8x for '%s')\n",
2888 die_ref.die_offset, name.GetCString());
2889 }
2890 }
2891 }
2892 const uint32_t num_matches = types.GetSize() - initial_types_size;
2893 if (log && num_matches) {
2894 if (parent_decl_ctx) {
2895 GetObjectFile()->GetModule()->LogMessage(
2896 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2897 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2898 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2899 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2900 num_matches);
2901 } else {
2902 GetObjectFile()->GetModule()->LogMessage(
2903 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2904 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2905 name.GetCString(), append, max_matches, num_matches);
2906 }
2907 }
2908 return num_matches;
2909 } else {
2910 UpdateExternalModuleListIfNeeded();
2911
2912 for (const auto &pair : m_external_type_modules) {
2913 ModuleSP external_module_sp = pair.second;
2914 if (external_module_sp) {
2915 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2916 if (sym_vendor) {
2917 const uint32_t num_external_matches =
2918 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2919 max_matches, searched_symbol_files, types);
2920 if (num_external_matches)
2921 return num_external_matches;
2922 }
2923 }
2924 }
2925 }
2926
2927 return 0;
2928}
2929
2930size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2931 bool append, TypeMap &types) {
2932 if (!append)
2933 types.Clear();
2934
2935 if (context.empty())
2936 return 0;
2937
2938 DIEArray die_offsets;
2939
2940 ConstString name = context.back().name;
2941
2942 if (!name)
2943 return 0;
2944
2945 if (m_using_apple_tables) {
2946 if (m_apple_types_ap.get()) {
2947 const char *name_cstr = name.GetCString();
2948 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2949 }
2950 } else {
2951 if (!m_indexed)
2952 Index();
2953
2954 m_type_index.Find(name, die_offsets);
2955 }
2956
2957 const size_t num_die_matches = die_offsets.size();
2958
2959 if (num_die_matches) {
2960 size_t num_matches = 0;
2961 for (size_t i = 0; i < num_die_matches; ++i) {
2962 const DIERef &die_ref = die_offsets[i];
2963 DWARFDIE die = GetDIE(die_ref);
2964
2965 if (die) {
2966 std::vector<CompilerContext> die_context;
2967 die.GetDWOContext(die_context);
2968 if (die_context != context)
2969 continue;
2970
2971 Type *matching_type = ResolveType(die, true, true);
2972 if (matching_type) {
2973 // We found a type pointer, now find the shared pointer form our type
2974 // list
2975 types.InsertUnique(matching_type->shared_from_this());
2976 ++num_matches;
2977 }
2978 } else {
2979 if (m_using_apple_tables) {
2980 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2981 "the DWARF debug information has been modified (.apple_types "
2982 "accelerator table had bad die 0x%8.8x for '%s')\n",
2983 die_ref.die_offset, name.GetCString());
2984 }
2985 }
2986 }
2987 return num_matches;
2988 }
2989 return 0;
2990}
2991
2992CompilerDeclContext
2993SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2994 const CompilerDeclContext *parent_decl_ctx) {
2995 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2996
2997 if (log) {
2998 GetObjectFile()->GetModule()->LogMessage(
2999 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3000 name.GetCString());
3001 }
3002
3003 CompilerDeclContext namespace_decl_ctx;
3004
3005 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
3006 return namespace_decl_ctx;
3007
3008 DWARFDebugInfo *info = DebugInfo();
3009 if (info) {
3010 DIEArray die_offsets;
3011
3012 // Index if we already haven't to make sure the compile units
3013 // get indexed and make their global DIE index list
3014 if (m_using_apple_tables) {
3015 if (m_apple_namespaces_ap.get()) {
3016 const char *name_cstr = name.GetCString();
3017 m_apple_namespaces_ap->FindByName(name_cstr, die_offsets);
3018 }
3019 } else {
3020 if (!m_indexed)
3021 Index();
3022
3023 m_namespace_index.Find(name, die_offsets);
3024 }
3025
3026 const size_t num_matches = die_offsets.size();
3027 if (num_matches) {
3028 for (size_t i = 0; i < num_matches; ++i) {
3029 const DIERef &die_ref = die_offsets[i];
3030 DWARFDIE die = GetDIE(die_ref);
3031
3032 if (die) {
3033 if (!DIEInDeclContext(parent_decl_ctx, die))
3034 continue; // The containing decl contexts don't match
3035
3036 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3037 if (dwarf_ast) {
3038 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
3039 if (namespace_decl_ctx)
3040 break;
3041 }
3042 } else {
3043 if (m_using_apple_tables) {
3044 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3045 "the DWARF debug information has been modified "
3046 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
3047 "'%s')\n",
3048 die_ref.die_offset, name.GetCString());
3049 }
3050 }
3051 }
3052 }
3053 }
3054 if (log && namespace_decl_ctx) {
3055 GetObjectFile()->GetModule()->LogMessage(
3056 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
3057 "CompilerDeclContext(%p/%p) \"%s\"",
3058 name.GetCString(),
3059 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
3060 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
3061 namespace_decl_ctx.GetName().AsCString("<NULL>"));
3062 }
3063
3064 return namespace_decl_ctx;
3065}
3066
3067TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3068 bool resolve_function_context) {
3069 TypeSP type_sp;
3070 if (die) {
3071 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3072 if (type_ptr == NULL) {
3073 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3074 assert(lldb_cu);
3075 SymbolContext sc(lldb_cu);
3076 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3077 while (parent_die != nullptr) {
3078 if (parent_die->Tag() == DW_TAG_subprogram)
3079 break;
3080 parent_die = parent_die->GetParent();
3081 }
3082 SymbolContext sc_backup = sc;
3083 if (resolve_function_context && parent_die != nullptr &&
3084 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3085 sc = sc_backup;
3086
3087 type_sp = ParseType(sc, die, NULL);
3088 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3089 // Grab the existing type from the master types lists
3090 type_sp = type_ptr->shared_from_this();
3091 }
3092 }
3093 return type_sp;
3094}
3095
3096DWARFDIE
3097SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3098 if (orig_die) {
3099 DWARFDIE die = orig_die;
3100
3101 while (die) {
3102 // If this is the original DIE that we are searching for a declaration
3103 // for, then don't look in the cache as we don't want our own decl
3104 // context to be our decl context...
3105 if (orig_die != die) {
3106 switch (die.Tag()) {
3107 case DW_TAG_compile_unit:
3108 case DW_TAG_namespace:
3109 case DW_TAG_structure_type:
3110 case DW_TAG_union_type:
3111 case DW_TAG_class_type:
3112 case DW_TAG_lexical_block:
3113 case DW_TAG_subprogram:
3114 return die;
Sean Callananb4945782017-04-24 22:11:10 +00003115 case DW_TAG_inlined_subroutine: {
3116 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3117 if (abs_die) {
3118 return abs_die;
3119 }
3120 break;
3121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003122 default:
3123 break;
3124 }
3125 }
3126
3127 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3128 if (spec_die) {
3129 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3130 if (decl_ctx_die)
3131 return decl_ctx_die;
3132 }
3133
3134 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3135 if (abs_die) {
3136 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3137 if (decl_ctx_die)
3138 return decl_ctx_die;
3139 }
3140
3141 die = die.GetParent();
3142 }
3143 }
3144 return DWARFDIE();
3145}
3146
3147Symbol *
3148SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3149 Symbol *objc_class_symbol = NULL;
3150 if (m_obj_file) {
3151 Symtab *symtab = m_obj_file->GetSymtab();
3152 if (symtab) {
3153 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3154 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3155 Symtab::eVisibilityAny);
3156 }
3157 }
3158 return objc_class_symbol;
3159}
3160
3161// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
3162// they don't
3163// then we can end up looking through all class types for a complete type and
3164// never find
3165// the full definition. We need to know if this attribute is supported, so we
3166// determine
3167// this here and cache th result. We also need to worry about the debug map
3168// DWARF file
3169// if we are doing darwin DWARF in .o file debugging.
3170bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
3171 DWARFCompileUnit *cu) {
3172 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3173 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3174 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3175 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3176 else {
3177 DWARFDebugInfo *debug_info = DebugInfo();
3178 const uint32_t num_compile_units = GetNumCompileUnits();
3179 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
3180 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3181 if (dwarf_cu != cu &&
3182 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3183 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3184 break;
3185 }
3186 }
3187 }
3188 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3189 GetDebugMapSymfile())
3190 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3191 }
3192 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003193}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003194
3195// This function can be used when a DIE is found that is a forward declaration
3196// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3198 const DWARFDIE &die, const ConstString &type_name,
3199 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003200
Kate Stoneb9c1b512016-09-06 20:57:50 +00003201 TypeSP type_sp;
3202
3203 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3204 return type_sp;
3205
3206 DIEArray die_offsets;
3207
3208 if (m_using_apple_tables) {
3209 if (m_apple_types_ap.get()) {
3210 const char *name_cstr = type_name.GetCString();
3211 m_apple_types_ap->FindCompleteObjCClassByName(name_cstr, die_offsets,
3212 must_be_implementation);
3213 }
3214 } else {
3215 if (!m_indexed)
3216 Index();
3217
3218 m_type_index.Find(type_name, die_offsets);
3219 }
3220
3221 const size_t num_matches = die_offsets.size();
3222
3223 if (num_matches) {
3224 for (size_t i = 0; i < num_matches; ++i) {
3225 const DIERef &die_ref = die_offsets[i];
3226 DWARFDIE type_die = GetDIE(die_ref);
3227
3228 if (type_die) {
3229 bool try_resolving_type = false;
3230
3231 // Don't try and resolve the DIE we are looking for with the DIE itself!
3232 if (type_die != die) {
3233 switch (type_die.Tag()) {
3234 case DW_TAG_class_type:
3235 case DW_TAG_structure_type:
3236 try_resolving_type = true;
3237 break;
3238 default:
3239 break;
3240 }
3241 }
3242
3243 if (try_resolving_type) {
3244 if (must_be_implementation &&
3245 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3246 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3247 DW_AT_APPLE_objc_complete_type, 0);
3248
3249 if (try_resolving_type) {
3250 Type *resolved_type = ResolveType(type_die, false, true);
3251 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3252 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3253 " (cu 0x%8.8" PRIx64 ")\n",
3254 die.GetID(),
3255 m_obj_file->GetFileSpec().GetFilename().AsCString(
3256 "<Unknown>"),
3257 type_die.GetID(), type_cu->GetID());
3258
3259 if (die)
3260 GetDIEToType()[die.GetDIE()] = resolved_type;
3261 type_sp = resolved_type->shared_from_this();
3262 break;
3263 }
3264 }
3265 }
3266 } else {
3267 if (m_using_apple_tables) {
3268 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3269 "the DWARF debug information has been modified (.apple_types "
3270 "accelerator table had bad die 0x%8.8x for '%s')\n",
3271 die_ref.die_offset, type_name.GetCString());
3272 }
3273 }
3274 }
3275 }
3276 return type_sp;
3277}
Greg Claytona8022fa2012-04-24 21:22:41 +00003278
Greg Clayton80c26302012-02-05 06:12:47 +00003279//----------------------------------------------------------------------
3280// This function helps to ensure that the declaration contexts match for
Kate Stoneb9c1b512016-09-06 20:57:50 +00003281// two different DIEs. Often times debug information will refer to a
Greg Clayton80c26302012-02-05 06:12:47 +00003282// forward declaration of a type (the equivalent of "struct my_struct;".
3283// There will often be a declaration of that type elsewhere that has the
3284// full definition. When we go looking for the full type "my_struct", we
3285// will find one or more matches in the accelerator tables and we will
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286// then need to make sure the type was in the same declaration context
Greg Clayton80c26302012-02-05 06:12:47 +00003287// as the original DIE. This function can efficiently compare two DIEs
3288// and will return true when the declaration context matches, and false
Kate Stoneb9c1b512016-09-06 20:57:50 +00003289// when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003290//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003291bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3292 const DWARFDIE &die2) {
3293 if (die1 == die2)
3294 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003295
Kate Stoneb9c1b512016-09-06 20:57:50 +00003296 DWARFDIECollection decl_ctx_1;
3297 DWARFDIECollection decl_ctx_2;
3298 // The declaration DIE stack is a stack of the declaration context
3299 // DIEs all the way back to the compile unit. If a type "T" is
3300 // declared inside a class "B", and class "B" is declared inside
3301 // a class "A" and class "A" is in a namespace "lldb", and the
3302 // namespace is in a compile unit, there will be a stack of DIEs:
3303 //
3304 // [0] DW_TAG_class_type for "B"
3305 // [1] DW_TAG_class_type for "A"
3306 // [2] DW_TAG_namespace for "lldb"
3307 // [3] DW_TAG_compile_unit for the source file.
3308 //
3309 // We grab both contexts and make sure that everything matches
3310 // all the way back to the compiler unit.
3311
3312 // First lets grab the decl contexts for both DIEs
3313 die1.GetDeclContextDIEs(decl_ctx_1);
3314 die2.GetDeclContextDIEs(decl_ctx_2);
3315 // Make sure the context arrays have the same size, otherwise
3316 // we are done
3317 const size_t count1 = decl_ctx_1.Size();
3318 const size_t count2 = decl_ctx_2.Size();
3319 if (count1 != count2)
3320 return false;
3321
3322 // Make sure the DW_TAG values match all the way back up the
3323 // compile unit. If they don't, then we are done.
3324 DWARFDIE decl_ctx_die1;
3325 DWARFDIE decl_ctx_die2;
3326 size_t i;
3327 for (i = 0; i < count1; i++) {
3328 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3329 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3330 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3331 return false;
3332 }
Greg Clayton890ff562012-02-02 05:48:16 +00003333#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003334
Kate Stoneb9c1b512016-09-06 20:57:50 +00003335 // Make sure the top item in the decl context die array is always
3336 // DW_TAG_compile_unit. If it isn't then something went wrong in
3337 // the DWARFDIE::GetDeclContextDIEs() function...
3338 assert(decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003339
Greg Clayton890ff562012-02-02 05:48:16 +00003340#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00003341 // Always skip the compile unit when comparing by only iterating up to
3342 // "count - 1". Here we compare the names as we go.
3343 for (i = 0; i < count1 - 1; i++) {
3344 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3345 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3346 const char *name1 = decl_ctx_die1.GetName();
3347 const char *name2 = decl_ctx_die2.GetName();
3348 // If the string was from a DW_FORM_strp, then the pointer will often
3349 // be the same!
3350 if (name1 == name2)
3351 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003352
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353 // Name pointers are not equal, so only compare the strings
3354 // if both are not NULL.
3355 if (name1 && name2) {
3356 // If the strings don't compare, we are done...
3357 if (strcmp(name1, name2) != 0)
3358 return false;
3359 } else {
3360 // One name was NULL while the other wasn't
3361 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003362 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003363 }
3364 // We made it through all of the checks and the declaration contexts
3365 // are equal.
3366 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003367}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003368
Kate Stoneb9c1b512016-09-06 20:57:50 +00003369TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3370 const DWARFDeclContext &dwarf_decl_ctx) {
3371 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003372
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3374 if (dwarf_decl_ctx_count > 0) {
3375 const ConstString type_name(dwarf_decl_ctx[0].name);
3376 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003377
Kate Stoneb9c1b512016-09-06 20:57:50 +00003378 if (type_name) {
3379 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3380 DWARF_LOG_LOOKUPS));
3381 if (log) {
3382 GetObjectFile()->GetModule()->LogMessage(
3383 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3384 "s, qualified-name='%s')",
3385 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3386 dwarf_decl_ctx.GetQualifiedName());
3387 }
3388
3389 DIEArray die_offsets;
3390
3391 if (m_using_apple_tables) {
3392 if (m_apple_types_ap.get()) {
3393 const bool has_tag =
3394 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3395 DWARFMappedHash::eAtomTypeTag);
3396 const bool has_qualified_name_hash =
3397 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3398 DWARFMappedHash::eAtomTypeQualNameHash);
3399 if (has_tag && has_qualified_name_hash) {
3400 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3401 const uint32_t qualified_name_hash =
3402 MappedHash::HashStringUsingDJB(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003403 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003404 GetObjectFile()->GetModule()->LogMessage(
3405 log, "FindByNameAndTagAndQualifiedNameHash()");
3406 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
3407 type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3408 } else if (has_tag) {
3409 if (log)
3410 GetObjectFile()->GetModule()->LogMessage(log,
3411 "FindByNameAndTag()");
3412 m_apple_types_ap->FindByNameAndTag(type_name.GetCString(), tag,
3413 die_offsets);
3414 } else {
3415 m_apple_types_ap->FindByName(type_name.GetCString(), die_offsets);
3416 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003418 } else {
3419 if (!m_indexed)
3420 Index();
3421
3422 m_type_index.Find(type_name, die_offsets);
3423 }
3424
3425 const size_t num_matches = die_offsets.size();
3426
3427 // Get the type system that we are looking to find a type for. We will use
3428 // this
3429 // to ensure any matches we find are in a language that this type system
3430 // supports
3431 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3432 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3433 ? nullptr
3434 : GetTypeSystemForLanguage(language);
3435
3436 if (num_matches) {
3437 for (size_t i = 0; i < num_matches; ++i) {
3438 const DIERef &die_ref = die_offsets[i];
3439 DWARFDIE type_die = GetDIE(die_ref);
3440
3441 if (type_die) {
3442 // Make sure type_die's langauge matches the type system we are
3443 // looking for.
3444 // We don't want to find a "Foo" type from Java if we are looking
3445 // for a "Foo"
3446 // type for C, C++, ObjC, or ObjC++.
3447 if (type_system &&
3448 !type_system->SupportsLanguage(type_die.GetLanguage()))
3449 continue;
3450 bool try_resolving_type = false;
3451
3452 // Don't try and resolve the DIE we are looking for with the DIE
3453 // itself!
3454 const dw_tag_t type_tag = type_die.Tag();
3455 // Make sure the tags match
3456 if (type_tag == tag) {
3457 // The tags match, lets try resolving this type
3458 try_resolving_type = true;
3459 } else {
3460 // The tags don't match, but we need to watch our for a
3461 // forward declaration for a struct and ("struct foo")
3462 // ends up being a class ("class foo { ... };") or
3463 // vice versa.
3464 switch (type_tag) {
3465 case DW_TAG_class_type:
3466 // We had a "class foo", see if we ended up with a "struct foo {
3467 // ... };"
3468 try_resolving_type = (tag == DW_TAG_structure_type);
3469 break;
3470 case DW_TAG_structure_type:
3471 // We had a "struct foo", see if we ended up with a "class foo {
3472 // ... };"
3473 try_resolving_type = (tag == DW_TAG_class_type);
3474 break;
3475 default:
3476 // Tags don't match, don't event try to resolve
3477 // using this type whose name matches....
3478 break;
3479 }
3480 }
3481
3482 if (try_resolving_type) {
3483 DWARFDeclContext type_dwarf_decl_ctx;
3484 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3485
3486 if (log) {
3487 GetObjectFile()->GetModule()->LogMessage(
3488 log, "SymbolFileDWARF::"
3489 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3490 "qualified-name='%s') trying die=0x%8.8x (%s)",
3491 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3492 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3493 type_dwarf_decl_ctx.GetQualifiedName());
3494 }
3495
3496 // Make sure the decl contexts match all the way up
3497 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3498 Type *resolved_type = ResolveType(type_die, false);
3499 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3500 type_sp = resolved_type->shared_from_this();
3501 break;
3502 }
3503 }
3504 } else {
3505 if (log) {
3506 std::string qualified_name;
3507 type_die.GetQualifiedName(qualified_name);
3508 GetObjectFile()->GetModule()->LogMessage(
3509 log, "SymbolFileDWARF::"
3510 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3511 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3512 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3513 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3514 qualified_name.c_str());
3515 }
3516 }
3517 } else {
3518 if (m_using_apple_tables) {
3519 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3520 "the DWARF debug information has been modified (.apple_types "
3521 "accelerator table had bad die 0x%8.8x for '%s')\n",
3522 die_ref.die_offset, type_name.GetCString());
3523 }
3524 }
3525 }
3526 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003528 }
3529 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003530}
3531
Kate Stoneb9c1b512016-09-06 20:57:50 +00003532TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3533 bool *type_is_new_ptr) {
3534 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003535
Kate Stoneb9c1b512016-09-06 20:57:50 +00003536 if (die) {
3537 TypeSystem *type_system =
3538 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003539
Kate Stoneb9c1b512016-09-06 20:57:50 +00003540 if (type_system) {
3541 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3542 if (dwarf_ast) {
3543 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3544 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3545 if (type_sp) {
3546 TypeList *type_list = GetTypeList();
3547 if (type_list)
3548 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003549
Kate Stoneb9c1b512016-09-06 20:57:50 +00003550 if (die.Tag() == DW_TAG_subprogram) {
3551 DIERef die_ref = die.GetDIERef();
3552 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3553 .GetScopeQualifiedName()
3554 .AsCString(""));
3555 if (scope_qualified_name.size()) {
3556 NameToOffsetMap::iterator iter =
3557 m_function_scope_qualified_name_map.find(
3558 scope_qualified_name);
3559 if (iter != m_function_scope_qualified_name_map.end())
3560 (*iter).second->insert(die_ref);
3561 else {
3562 DIERefSetSP new_set(new std::set<DIERef>);
3563 new_set->insert(die_ref);
3564 m_function_scope_qualified_name_map.emplace(
3565 std::make_pair(scope_qualified_name, new_set));
3566 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003568 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003569 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003570 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003571 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003572 }
3573
3574 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003575}
3576
Kate Stoneb9c1b512016-09-06 20:57:50 +00003577size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3578 const DWARFDIE &orig_die,
3579 bool parse_siblings, bool parse_children) {
3580 size_t types_added = 0;
3581 DWARFDIE die = orig_die;
3582 while (die) {
3583 bool type_is_new = false;
3584 if (ParseType(sc, die, &type_is_new).get()) {
3585 if (type_is_new)
3586 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003588
3589 if (parse_children && die.HasChildren()) {
3590 if (die.Tag() == DW_TAG_subprogram) {
3591 SymbolContext child_sc(sc);
3592 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3593 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3594 } else
3595 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3596 }
3597
3598 if (parse_siblings)
3599 die = die.GetSibling();
3600 else
3601 die.Clear();
3602 }
3603 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003604}
3605
Kate Stoneb9c1b512016-09-06 20:57:50 +00003606size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3607 assert(sc.comp_unit && sc.function);
3608 size_t functions_added = 0;
3609 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3610 if (dwarf_cu) {
3611 const dw_offset_t function_die_offset = sc.function->GetID();
3612 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3613 if (function_die) {
3614 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3615 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003618
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003620}
3621
Kate Stoneb9c1b512016-09-06 20:57:50 +00003622size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3623 // At least a compile unit must be valid
3624 assert(sc.comp_unit);
3625 size_t types_added = 0;
3626 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3627 if (dwarf_cu) {
3628 if (sc.function) {
3629 dw_offset_t function_die_offset = sc.function->GetID();
3630 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3631 if (func_die && func_die.HasChildren()) {
3632 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3633 }
3634 } else {
3635 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3636 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3637 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003640 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003641
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003643}
3644
Kate Stoneb9c1b512016-09-06 20:57:50 +00003645size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3646 if (sc.comp_unit != NULL) {
3647 DWARFDebugInfo *info = DebugInfo();
3648 if (info == NULL)
3649 return 0;
3650
3651 if (sc.function) {
3652 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3653
3654 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3655 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3656 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3657 const size_t num_variables = ParseVariables(
3658 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3659
3660 // Let all blocks know they have parse all their variables
3661 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3662 return num_variables;
3663 }
3664 } else if (sc.comp_unit) {
3665 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3666
3667 if (dwarf_cu == NULL)
3668 return 0;
3669
3670 uint32_t vars_added = 0;
3671 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3672
3673 if (variables.get() == NULL) {
3674 variables.reset(new VariableList());
3675 sc.comp_unit->SetVariableList(variables);
3676
3677 DIEArray die_offsets;
3678 if (m_using_apple_tables) {
3679 if (m_apple_names_ap.get()) {
3680 DWARFMappedHash::DIEInfoArray hash_data_array;
3681 if (m_apple_names_ap->AppendAllDIEsInRange(
3682 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3683 hash_data_array)) {
3684 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003686 }
3687 } else {
3688 // Index if we already haven't to make sure the compile units
3689 // get indexed and make their global DIE index list
3690 if (!m_indexed)
3691 Index();
3692
3693 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3694 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003695 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003696
Kate Stoneb9c1b512016-09-06 20:57:50 +00003697 const size_t num_matches = die_offsets.size();
3698 if (num_matches) {
3699 for (size_t i = 0; i < num_matches; ++i) {
3700 const DIERef &die_ref = die_offsets[i];
3701 DWARFDIE die = GetDIE(die_ref);
3702 if (die) {
3703 VariableSP var_sp(
3704 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3705 if (var_sp) {
3706 variables->AddVariableIfUnique(var_sp);
3707 ++vars_added;
3708 }
3709 } else {
3710 if (m_using_apple_tables) {
3711 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3712 "the DWARF debug information has been modified "
3713 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3714 die_ref.die_offset);
3715 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003716 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003718 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003719 }
3720 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003721 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003722 }
3723 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003724}
3725
Kate Stoneb9c1b512016-09-06 20:57:50 +00003726VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3727 const DWARFDIE &die,
3728 const lldb::addr_t func_low_pc) {
3729 if (die.GetDWARF() != this)
3730 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003731
Kate Stoneb9c1b512016-09-06 20:57:50 +00003732 VariableSP var_sp;
3733 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003734 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735
Kate Stoneb9c1b512016-09-06 20:57:50 +00003736 var_sp = GetDIEToVariable()[die.GetDIE()];
3737 if (var_sp)
3738 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003739
Kate Stoneb9c1b512016-09-06 20:57:50 +00003740 const dw_tag_t tag = die.Tag();
3741 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003742
Kate Stoneb9c1b512016-09-06 20:57:50 +00003743 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3744 (tag == DW_TAG_formal_parameter && sc.function)) {
3745 DWARFAttributes attributes;
3746 const size_t num_attributes = die.GetAttributes(attributes);
3747 DWARFDIE spec_die;
3748 if (num_attributes > 0) {
3749 const char *name = NULL;
3750 const char *mangled = NULL;
3751 Declaration decl;
3752 uint32_t i;
3753 DWARFFormValue type_die_form;
3754 DWARFExpression location(die.GetCU());
3755 bool is_external = false;
3756 bool is_artificial = false;
3757 bool location_is_const_value_data = false;
3758 bool has_explicit_location = false;
3759 DWARFFormValue const_value;
3760 Variable::RangeList scope_ranges;
3761 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003762
Kate Stoneb9c1b512016-09-06 20:57:50 +00003763 for (i = 0; i < num_attributes; ++i) {
3764 dw_attr_t attr = attributes.AttributeAtIndex(i);
3765 DWARFFormValue form_value;
3766
3767 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3768 switch (attr) {
3769 case DW_AT_decl_file:
3770 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3771 form_value.Unsigned()));
3772 break;
3773 case DW_AT_decl_line:
3774 decl.SetLine(form_value.Unsigned());
3775 break;
3776 case DW_AT_decl_column:
3777 decl.SetColumn(form_value.Unsigned());
3778 break;
3779 case DW_AT_name:
3780 name = form_value.AsCString();
3781 break;
3782 case DW_AT_linkage_name:
3783 case DW_AT_MIPS_linkage_name:
3784 mangled = form_value.AsCString();
3785 break;
3786 case DW_AT_type:
3787 type_die_form = form_value;
3788 break;
3789 case DW_AT_external:
3790 is_external = form_value.Boolean();
3791 break;
3792 case DW_AT_const_value:
3793 // If we have already found a DW_AT_location attribute, ignore this
3794 // attribute.
3795 if (!has_explicit_location) {
3796 location_is_const_value_data = true;
3797 // The constant value will be either a block, a data value or a
3798 // string.
3799 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3800 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3801 // Retrieve the value as a block expression.
3802 uint32_t block_offset =
3803 form_value.BlockData() - debug_info_data.GetDataStart();
3804 uint32_t block_length = form_value.Unsigned();
3805 location.CopyOpcodeData(module, debug_info_data, block_offset,
3806 block_length);
3807 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3808 // Retrieve the value as a data expression.
3809 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3810 DWARFFormValue::GetFixedFormSizesForAddressSize(
3811 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3812 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3813 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3814 uint32_t data_length =
3815 fixed_form_sizes.GetSize(form_value.Form());
3816 if (data_length == 0) {
3817 const uint8_t *data_pointer = form_value.BlockData();
3818 if (data_pointer) {
3819 form_value.Unsigned();
3820 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3821 // we need to get the byte size of the type later after we
3822 // create the variable
3823 const_value = form_value;
3824 }
3825 } else
3826 location.CopyOpcodeData(module, debug_info_data, data_offset,
3827 data_length);
3828 } else {
3829 // Retrieve the value as a string expression.
3830 if (form_value.Form() == DW_FORM_strp) {
3831 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3832 DWARFFormValue::GetFixedFormSizesForAddressSize(
3833 attributes.CompileUnitAtIndex(i)
3834 ->GetAddressByteSize(),
3835 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3836 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3837 uint32_t data_length =
3838 fixed_form_sizes.GetSize(form_value.Form());
3839 location.CopyOpcodeData(module, debug_info_data, data_offset,
3840 data_length);
3841 } else {
3842 const char *str = form_value.AsCString();
3843 uint32_t string_offset =
3844 str - (const char *)debug_info_data.GetDataStart();
3845 uint32_t string_length = strlen(str) + 1;
3846 location.CopyOpcodeData(module, debug_info_data,
3847 string_offset, string_length);
3848 }
3849 }
3850 }
3851 break;
3852 case DW_AT_location: {
3853 location_is_const_value_data = false;
3854 has_explicit_location = true;
3855 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3856 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3857
3858 uint32_t block_offset =
3859 form_value.BlockData() - debug_info_data.GetDataStart();
3860 uint32_t block_length = form_value.Unsigned();
3861 location.CopyOpcodeData(module, get_debug_info_data(),
3862 block_offset, block_length);
3863 } else {
3864 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3865 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3866
3867 size_t loc_list_length = DWARFExpression::LocationListSize(
3868 die.GetCU(), debug_loc_data, debug_loc_offset);
3869 if (loc_list_length > 0) {
3870 location.CopyOpcodeData(module, debug_loc_data,
3871 debug_loc_offset, loc_list_length);
3872 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3873 location.SetLocationListSlide(
3874 func_low_pc -
3875 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3876 }
3877 }
3878 } break;
3879 case DW_AT_specification:
3880 spec_die = GetDIE(DIERef(form_value));
3881 break;
3882 case DW_AT_start_scope: {
3883 if (form_value.Form() == DW_FORM_sec_offset) {
3884 DWARFRangeList dwarf_scope_ranges;
3885 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003886 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3887 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003888 dwarf_scope_ranges);
3889
3890 // All DW_AT_start_scope are relative to the base address of the
3891 // compile unit. We add the compile unit base address to make
3892 // sure all the addresses are properly fixed up.
3893 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3894 i < count; ++i) {
3895 const DWARFRangeList::Entry &range =
3896 dwarf_scope_ranges.GetEntryRef(i);
3897 scope_ranges.Append(range.GetRangeBase() +
3898 die.GetCU()->GetBaseAddress(),
3899 range.GetByteSize());
3900 }
3901 } else {
3902 // TODO: Handle the case when DW_AT_start_scope have form
3903 // constant. The
3904 // dwarf spec is a bit ambiguous about what is the expected
3905 // behavior in
3906 // case the enclosing block have a non coninious address range and
3907 // the
3908 // DW_AT_start_scope entry have a form constant.
3909 GetObjectFile()->GetModule()->ReportWarning(
3910 "0x%8.8" PRIx64
3911 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3912 die.GetID(), form_value.Form());
3913 }
3914
3915 scope_ranges.Sort();
3916 scope_ranges.CombineConsecutiveRanges();
3917 } break;
3918 case DW_AT_artificial:
3919 is_artificial = form_value.Boolean();
3920 break;
3921 case DW_AT_accessibility:
3922 break; // accessibility =
3923 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3924 case DW_AT_declaration:
3925 case DW_AT_description:
3926 case DW_AT_endianity:
3927 case DW_AT_segment:
3928 case DW_AT_visibility:
3929 default:
3930 case DW_AT_abstract_origin:
3931 case DW_AT_sibling:
3932 break;
3933 }
3934 }
3935 }
3936
3937 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3938 const dw_tag_t parent_tag = die.GetParent().Tag();
3939 bool is_static_member =
3940 parent_tag == DW_TAG_compile_unit &&
3941 (parent_context_die.Tag() == DW_TAG_class_type ||
3942 parent_context_die.Tag() == DW_TAG_structure_type);
3943
3944 ValueType scope = eValueTypeInvalid;
3945
3946 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3947 SymbolContextScope *symbol_context_scope = NULL;
3948
Sam McCall30621392016-11-22 11:40:25 +00003949 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003950 if (!mangled) {
3951 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3952 // DW_AT_MIPS_linkage_name) to
3953 // generate fully qualified names of global variables with commands like
3954 // "frame var j".
3955 // For example, if j were an int variable holding a value 4 and declared
3956 // in a namespace
3957 // B which in turn is contained in a namespace A, the command "frame var
3958 // j" returns
3959 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we
3960 // should be able
3961 // to generate a fully qualified name from the declaration context.
3962 if (parent_tag == DW_TAG_compile_unit &&
3963 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3964 DWARFDeclContext decl_ctx;
3965
3966 die.GetDWARFDeclContext(decl_ctx);
3967 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3968 }
3969 }
3970
Kate Stoneb9c1b512016-09-06 20:57:50 +00003971 if (tag == DW_TAG_formal_parameter)
3972 scope = eValueTypeVariableArgument;
3973 else {
Sam McCall30621392016-11-22 11:40:25 +00003974 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3975 // or static variable, so we have to do a little digging:
3976 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3977 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3978 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3979 // Clang likes to combine small global variables into the same symbol
3980 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3981 // so we need to look through the whole expression.
3982 bool is_static_lifetime =
3983 has_explicit_mangled ||
3984 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003985 // Check if the location has a DW_OP_addr with any address value...
3986 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3987 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003988 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003989 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3990 if (op_error) {
3991 StreamString strm;
3992 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3993 NULL);
3994 GetObjectFile()->GetModule()->ReportError(
3995 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003996 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003997 }
Sam McCall30621392016-11-22 11:40:25 +00003998 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3999 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004000 }
4001 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
4002
Sam McCall30621392016-11-22 11:40:25 +00004003 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004004 if (is_external)
4005 scope = eValueTypeVariableGlobal;
4006 else
4007 scope = eValueTypeVariableStatic;
4008
4009 if (debug_map_symfile) {
4010 // When leaving the DWARF in the .o files on darwin,
4011 // when we have a global variable that wasn't initialized,
4012 // the .o file might not have allocated a virtual
4013 // address for the global variable. In this case it will
4014 // have created a symbol for the global variable
4015 // that is undefined/data and external and the value will
4016 // be the byte size of the variable. When we do the
4017 // address map in SymbolFileDWARFDebugMap we rely on
4018 // having an address, we need to do some magic here
4019 // so we can get the correct address for our global
4020 // variable. The address for all of these entries
4021 // will be zero, and there will be an undefined symbol
4022 // in this object file, and the executable will have
4023 // a matching symbol with a good address. So here we
4024 // dig up the correct address and replace it in the
4025 // location for the variable, and set the variable's
4026 // symbol context scope to be that of the main executable
4027 // so the file address will resolve correctly.
4028 bool linked_oso_file_addr = false;
4029 if (is_external && location_DW_OP_addr == 0) {
4030 // we have a possible uninitialized extern global
4031 ConstString const_name(mangled ? mangled : name);
4032 ObjectFile *debug_map_objfile =
4033 debug_map_symfile->GetObjectFile();
4034 if (debug_map_objfile) {
4035 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
4036 if (debug_map_symtab) {
4037 Symbol *exe_symbol =
4038 debug_map_symtab->FindFirstSymbolWithNameAndType(
4039 const_name, eSymbolTypeData, Symtab::eDebugYes,
4040 Symtab::eVisibilityExtern);
4041 if (exe_symbol) {
4042 if (exe_symbol->ValueIsAddress()) {
4043 const addr_t exe_file_addr =
4044 exe_symbol->GetAddressRef().GetFileAddress();
4045 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4046 if (location.Update_DW_OP_addr(exe_file_addr)) {
4047 linked_oso_file_addr = true;
4048 symbol_context_scope = exe_symbol;
4049 }
4050 }
4051 }
4052 }
4053 }
4054 }
4055 }
4056
4057 if (!linked_oso_file_addr) {
4058 // The DW_OP_addr is not zero, but it contains a .o file address
4059 // which
4060 // needs to be linked up correctly.
4061 const lldb::addr_t exe_file_addr =
4062 debug_map_symfile->LinkOSOFileAddress(this,
4063 location_DW_OP_addr);
4064 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4065 // Update the file address for this variable
4066 location.Update_DW_OP_addr(exe_file_addr);
4067 } else {
4068 // Variable didn't make it into the final executable
4069 return var_sp;
4070 }
4071 }
4072 }
4073 } else {
4074 if (location_is_const_value_data)
4075 scope = eValueTypeVariableStatic;
4076 else {
4077 scope = eValueTypeVariableLocal;
4078 if (debug_map_symfile) {
4079 // We need to check for TLS addresses that we need to fixup
4080 if (location.ContainsThreadLocalStorage()) {
4081 location.LinkThreadLocalStorage(
4082 debug_map_symfile->GetObjectFile()->GetModule(),
4083 [this, debug_map_symfile](
4084 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4085 return debug_map_symfile->LinkOSOFileAddress(
4086 this, unlinked_file_addr);
4087 });
4088 scope = eValueTypeVariableThreadLocal;
4089 }
4090 }
4091 }
4092 }
4093 }
4094
4095 if (symbol_context_scope == NULL) {
4096 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004097 case DW_TAG_subprogram:
4098 case DW_TAG_inlined_subroutine:
4099 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004100 if (sc.function) {
4101 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4102 sc_parent_die.GetID());
4103 if (symbol_context_scope == NULL)
4104 symbol_context_scope = sc.function;
4105 }
4106 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004107
Kate Stoneb9c1b512016-09-06 20:57:50 +00004108 default:
4109 symbol_context_scope = sc.comp_unit;
4110 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004112 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004113
Kate Stoneb9c1b512016-09-06 20:57:50 +00004114 if (symbol_context_scope) {
4115 SymbolFileTypeSP type_sp(
4116 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4117
4118 if (const_value.Form() && type_sp && type_sp->GetType())
4119 location.CopyOpcodeData(const_value.Unsigned(),
4120 type_sp->GetType()->GetByteSize(),
4121 die.GetCU()->GetAddressByteSize());
4122
4123 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4124 symbol_context_scope, scope_ranges, &decl,
4125 location, is_external, is_artificial,
4126 is_static_member));
4127
4128 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4129 } else {
4130 // Not ready to parse this variable yet. It might be a global
4131 // or static variable that is in a function scope and the function
4132 // in the symbol context wasn't filled in yet
4133 return var_sp;
4134 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004135 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004136 // Cache var_sp even if NULL (the variable was just a specification or
4137 // was missing vital information to be able to be displayed in the debugger
4138 // (missing location due to optimization, etc)) so we don't re-parse
4139 // this DIE over and over later...
4140 GetDIEToVariable()[die.GetDIE()] = var_sp;
4141 if (spec_die)
4142 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4143 }
4144 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004145}
4146
Kate Stoneb9c1b512016-09-06 20:57:50 +00004147DWARFDIE
4148SymbolFileDWARF::FindBlockContainingSpecification(
4149 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4150 // Give the concrete function die specified by "func_die_offset", find the
4151 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4152 // to "spec_block_die_offset"
4153 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4154 spec_block_die_offset);
4155}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004156
Kate Stoneb9c1b512016-09-06 20:57:50 +00004157DWARFDIE
4158SymbolFileDWARF::FindBlockContainingSpecification(
4159 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4160 if (die) {
4161 switch (die.Tag()) {
4162 case DW_TAG_subprogram:
4163 case DW_TAG_inlined_subroutine:
4164 case DW_TAG_lexical_block: {
4165 if (die.GetAttributeValueAsReference(
4166 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4167 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004168
Kate Stoneb9c1b512016-09-06 20:57:50 +00004169 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4170 DW_INVALID_OFFSET) ==
4171 spec_block_die_offset)
4172 return die;
4173 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004174 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004175
4176 // Give the concrete function die specified by "func_die_offset", find the
4177 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4178 // to "spec_block_die_offset"
4179 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4180 child_die = child_die.GetSibling()) {
4181 DWARFDIE result_die =
4182 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4183 if (result_die)
4184 return result_die;
4185 }
4186 }
4187
4188 return DWARFDIE();
4189}
4190
4191size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4192 const DWARFDIE &orig_die,
4193 const lldb::addr_t func_low_pc,
4194 bool parse_siblings, bool parse_children,
4195 VariableList *cc_variable_list) {
4196 if (!orig_die)
4197 return 0;
4198
4199 VariableListSP variable_list_sp;
4200
4201 size_t vars_added = 0;
4202 DWARFDIE die = orig_die;
4203 while (die) {
4204 dw_tag_t tag = die.Tag();
4205
4206 // Check to see if we have already parsed this variable or constant?
4207 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4208 if (var_sp) {
4209 if (cc_variable_list)
4210 cc_variable_list->AddVariableIfUnique(var_sp);
4211 } else {
4212 // We haven't already parsed it, lets do that now.
4213 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4214 (tag == DW_TAG_formal_parameter && sc.function)) {
4215 if (variable_list_sp.get() == NULL) {
4216 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4217 dw_tag_t parent_tag = sc_parent_die.Tag();
4218 switch (parent_tag) {
4219 case DW_TAG_compile_unit:
4220 if (sc.comp_unit != NULL) {
4221 variable_list_sp = sc.comp_unit->GetVariableList(false);
4222 if (variable_list_sp.get() == NULL) {
4223 variable_list_sp.reset(new VariableList());
4224 sc.comp_unit->SetVariableList(variable_list_sp);
4225 }
4226 } else {
4227 GetObjectFile()->GetModule()->ReportError(
4228 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4229 "symbol context for 0x%8.8" PRIx64
4230 " %s.\n",
4231 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4232 orig_die.GetID(), orig_die.GetTagAsCString());
4233 }
4234 break;
4235
4236 case DW_TAG_subprogram:
4237 case DW_TAG_inlined_subroutine:
4238 case DW_TAG_lexical_block:
4239 if (sc.function != NULL) {
4240 // Check to see if we already have parsed the variables for the
4241 // given scope
4242
4243 Block *block = sc.function->GetBlock(true).FindBlockByID(
4244 sc_parent_die.GetID());
4245 if (block == NULL) {
4246 // This must be a specification or abstract origin with
4247 // a concrete block counterpart in the current function. We need
4248 // to find the concrete block so we can correctly add the
4249 // variable to it
4250 const DWARFDIE concrete_block_die =
4251 FindBlockContainingSpecification(
4252 DIERef(sc.function->GetID(), this),
4253 sc_parent_die.GetOffset());
4254 if (concrete_block_die)
4255 block = sc.function->GetBlock(true).FindBlockByID(
4256 concrete_block_die.GetID());
4257 }
4258
4259 if (block != NULL) {
4260 const bool can_create = false;
4261 variable_list_sp = block->GetBlockVariableList(can_create);
4262 if (variable_list_sp.get() == NULL) {
4263 variable_list_sp.reset(new VariableList());
4264 block->SetVariableList(variable_list_sp);
4265 }
4266 }
4267 }
4268 break;
4269
4270 default:
4271 GetObjectFile()->GetModule()->ReportError(
4272 "didn't find appropriate parent DIE for variable list for "
4273 "0x%8.8" PRIx64 " %s.\n",
4274 orig_die.GetID(), orig_die.GetTagAsCString());
4275 break;
4276 }
4277 }
4278
4279 if (variable_list_sp) {
4280 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4281 if (var_sp) {
4282 variable_list_sp->AddVariableIfUnique(var_sp);
4283 if (cc_variable_list)
4284 cc_variable_list->AddVariableIfUnique(var_sp);
4285 ++vars_added;
4286 }
4287 }
4288 }
4289 }
4290
4291 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4292
4293 if (!skip_children && parse_children && die.HasChildren()) {
4294 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4295 true, cc_variable_list);
4296 }
4297
4298 if (parse_siblings)
4299 die = die.GetSibling();
4300 else
4301 die.Clear();
4302 }
4303 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004304}
4305
4306//------------------------------------------------------------------
4307// PluginInterface protocol
4308//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004309ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4310
4311uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4312
4313void SymbolFileDWARF::DumpIndexes() {
4314 StreamFile s(stdout, false);
4315
4316 s.Printf(
4317 "DWARF index for (%s) '%s':",
4318 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4319 GetObjectFile()->GetFileSpec().GetPath().c_str());
4320 s.Printf("\nFunction basenames:\n");
4321 m_function_basename_index.Dump(&s);
4322 s.Printf("\nFunction fullnames:\n");
4323 m_function_fullname_index.Dump(&s);
4324 s.Printf("\nFunction methods:\n");
4325 m_function_method_index.Dump(&s);
4326 s.Printf("\nFunction selectors:\n");
4327 m_function_selector_index.Dump(&s);
4328 s.Printf("\nObjective C class selectors:\n");
4329 m_objc_class_selectors_index.Dump(&s);
4330 s.Printf("\nGlobals and statics:\n");
4331 m_global_index.Dump(&s);
4332 s.Printf("\nTypes:\n");
4333 m_type_index.Dump(&s);
4334 s.Printf("\nNamespaces:\n");
4335 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004336}
4337
Kate Stoneb9c1b512016-09-06 20:57:50 +00004338SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4339 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4340 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4341 if (module_sp) {
4342 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4343 if (sym_vendor)
4344 m_debug_map_symfile =
4345 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004347 }
4348 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004349}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004350
4351DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004352SymbolFileDWARF::GetLocationListFormat() const {
4353 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004354}
Tamas Berghammer963ce482017-08-25 13:56:14 +00004355
4356SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() {
4357 llvm::call_once(m_dwp_symfile_once_flag, [this]() {
Alexander Shaposhnikov64b4bcf2017-10-10 23:28:34 +00004358 ModuleSpec module_spec;
4359 module_spec.GetFileSpec() = m_obj_file->GetFileSpec();
4360 module_spec.GetSymbolFileSpec() =
4361 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp", false);
4362 FileSpec dwp_filespec = Symbols::LocateExecutableSymbolFile(module_spec);
Tamas Berghammer963ce482017-08-25 13:56:14 +00004363 if (dwp_filespec.Exists()) {
4364 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(),
4365 dwp_filespec);
4366 }
4367 });
4368 return m_dwp_symfile.get();
4369}