blob: 8c2fc3d3aa42504250000dc1488c99182f4e9d63 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
Sean Callanancc427fa2011-07-30 02:42:06 +000013#include "llvm/Support/Casting.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include "llvm/Support/Threading.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000015
Robert Flackeb83fab2015-05-15 18:59:59 +000016#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000018#include "lldb/Core/ModuleList.h"
19#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Scalar.h"
22#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000023#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/Timer.h"
25#include "lldb/Core/Value.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/RegularExpression.h"
27#include "lldb/Utility/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Sean Callanan4dbb2712015-09-25 20:35:58 +000029#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000030
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000031#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000032#include "lldb/Host/Host.h"
33
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000034#include "lldb/Interpreter/OptionValueFileSpecList.h"
35#include "lldb/Interpreter/OptionValueProperties.h"
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000038#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000039#include "lldb/Symbol/ClangUtil.h"
40#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000041#include "lldb/Symbol/CompilerDecl.h"
42#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000043#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000044#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000047#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000048#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Symbol/VariableList.h"
50
Jim Inghamaa816b82015-09-02 01:59:14 +000051#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
52#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000053
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
55
Tamas Berghammer2ff88702015-10-23 10:34:49 +000056#include "lldb/Utility/TaskPool.h"
57
Greg Clayton261ac3f2015-08-28 01:01:03 +000058#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000059#include "DWARFASTParserClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "DWARFCompileUnit.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000061#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "DWARFDebugAbbrev.h"
63#include "DWARFDebugAranges.h"
64#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000066#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "DWARFDebugPubnames.h"
68#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000069#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070#include "DWARFFormValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000072#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000073#include "SymbolFileDWARFDwo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Zachary Turner7d86ee52017-03-08 17:56:08 +000075#include "llvm/Support/FileSystem.h"
76
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077#include <map>
78
Matthew Gardinere81df3b2014-08-26 06:57:23 +000079#include <ctype.h>
80#include <string.h>
81
Greg Clayton62742b12010-11-11 01:09:45 +000082//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000083
84#ifdef ENABLE_DEBUG_PRINTF
85#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000086#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000087#else
88#define DEBUG_PRINTF(fmt, ...)
89#endif
90
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091using namespace lldb;
92using namespace lldb_private;
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094// static inline bool
95// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000096//{
97// switch (tag)
98// {
99// default:
100// break;
101// case DW_TAG_subprogram:
102// case DW_TAG_inlined_subroutine:
103// case DW_TAG_class_type:
104// case DW_TAG_structure_type:
105// case DW_TAG_union_type:
106// return true;
107// }
108// return false;
109//}
110//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000111
112namespace {
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114PropertyDefinition g_properties[] = {
115 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
116 nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic "
117 "links will be resolved at DWARF parse time."},
118 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120enum { ePropertySymLinkPaths };
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122class PluginProperties : public Properties {
123public:
124 static ConstString GetSettingName() {
125 return SymbolFileDWARF::GetPluginNameStatic();
126 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 PluginProperties() {
129 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
130 m_collection_sp->Initialize(g_properties);
131 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 FileSpecList &GetSymLinkPaths() {
134 OptionValueFileSpecList *option_value =
135 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
136 nullptr, true, ePropertySymLinkPaths);
137 assert(option_value);
138 return option_value->GetCurrentValue();
139 }
140};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
145 static const auto g_settings_sp(std::make_shared<PluginProperties>());
146 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
152 if (!path_from_dwarf || !path_from_dwarf[0]) {
153 return path_from_dwarf;
154 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 const char *colon_pos = strchr(path_from_dwarf, ':');
157 if (nullptr == colon_pos) {
158 return path_from_dwarf;
159 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 const char *slash_pos = strchr(path_from_dwarf, '/');
162 if (slash_pos && (slash_pos < colon_pos)) {
163 return path_from_dwarf;
164 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 // check whether we have a windows path, and so the first character
167 // is a drive-letter not a hostname.
168 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
169 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
170 return path_from_dwarf;
171 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 return colon_pos + 1;
174}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176static const char *resolveCompDir(const char *path_from_dwarf) {
177 if (!path_from_dwarf)
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000178 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179
180 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
181 // Remove the host part if present.
182 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
183 if (!local_path)
184 return nullptr;
185
186 bool is_symlink = false;
187 FileSpec local_path_spec(local_path, false);
188 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
189 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
190 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
191 local_path_spec, true);
192
193 if (!is_symlink)
194 return local_path;
195
Zachary Turner7d86ee52017-03-08 17:56:08 +0000196 namespace fs = llvm::sys::fs;
197 if (fs::get_file_type(local_path_spec.GetPath(), false) !=
198 fs::file_type::symlink_file)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 return local_path;
200
201 FileSpec resolved_local_path_spec;
202 const auto error =
203 FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
204 if (error.Success())
205 return resolved_local_path_spec.GetCString();
206
207 return nullptr;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000208}
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210void SymbolFileDWARF::Initialize() {
211 LogChannelDWARF::Initialize();
212 PluginManager::RegisterPlugin(GetPluginNameStatic(),
213 GetPluginDescriptionStatic(), CreateInstance,
214 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
218 if (!PluginManager::GetSettingForSymbolFilePlugin(
219 debugger, PluginProperties::GetSettingName())) {
220 const bool is_global_setting = true;
221 PluginManager::CreateSettingForSymbolFilePlugin(
222 debugger, GetGlobalPluginProperties()->GetValueProperties(),
223 ConstString("Properties for the dwarf symbol-file plug-in."),
224 is_global_setting);
225 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226}
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228void SymbolFileDWARF::Terminate() {
229 PluginManager::UnregisterPlugin(CreateInstance);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000230 LogChannelDWARF::Terminate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
234 static ConstString g_name("dwarf");
235 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
239 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
243 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246TypeList *SymbolFileDWARF::GetTypeList() {
247 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
248 if (debug_map_symfile)
249 return debug_map_symfile->GetTypeList();
250 else
251 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000252}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
254 dw_offset_t max_die_offset, uint32_t type_mask,
255 TypeSet &type_set) {
256 if (die) {
257 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 if (die_offset >= max_die_offset)
260 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 if (die_offset >= min_die_offset) {
263 const dw_tag_t tag = die.Tag();
264
265 bool add_type = false;
266
267 switch (tag) {
268 case DW_TAG_array_type:
269 add_type = (type_mask & eTypeClassArray) != 0;
270 break;
271 case DW_TAG_unspecified_type:
272 case DW_TAG_base_type:
273 add_type = (type_mask & eTypeClassBuiltin) != 0;
274 break;
275 case DW_TAG_class_type:
276 add_type = (type_mask & eTypeClassClass) != 0;
277 break;
278 case DW_TAG_structure_type:
279 add_type = (type_mask & eTypeClassStruct) != 0;
280 break;
281 case DW_TAG_union_type:
282 add_type = (type_mask & eTypeClassUnion) != 0;
283 break;
284 case DW_TAG_enumeration_type:
285 add_type = (type_mask & eTypeClassEnumeration) != 0;
286 break;
287 case DW_TAG_subroutine_type:
288 case DW_TAG_subprogram:
289 case DW_TAG_inlined_subroutine:
290 add_type = (type_mask & eTypeClassFunction) != 0;
291 break;
292 case DW_TAG_pointer_type:
293 add_type = (type_mask & eTypeClassPointer) != 0;
294 break;
295 case DW_TAG_rvalue_reference_type:
296 case DW_TAG_reference_type:
297 add_type = (type_mask & eTypeClassReference) != 0;
298 break;
299 case DW_TAG_typedef:
300 add_type = (type_mask & eTypeClassTypedef) != 0;
301 break;
302 case DW_TAG_ptr_to_member_type:
303 add_type = (type_mask & eTypeClassMemberPointer) != 0;
304 break;
305 }
306
307 if (add_type) {
308 const bool assert_not_being_parsed = true;
309 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
310 if (type) {
311 if (type_set.find(type) == type_set.end())
312 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316
317 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
318 child_die = child_die.GetSibling()) {
319 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
320 }
321 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000322}
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
325 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000326
327{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 CompileUnit *comp_unit = NULL;
331 DWARFCompileUnit *dwarf_cu = NULL;
332 if (sc_scope)
333 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 if (comp_unit) {
336 dwarf_cu = GetDWARFCompileUnit(comp_unit);
337 if (dwarf_cu == 0)
338 return 0;
339 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
340 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
341 } else {
342 DWARFDebugInfo *info = DebugInfo();
343 if (info) {
344 const size_t num_cus = info->GetNumCompileUnits();
345 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
346 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
347 if (dwarf_cu) {
348 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000349 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000351 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 }
353
354 std::set<CompilerType> compiler_type_set;
355 size_t num_types_added = 0;
356 for (Type *type : type_set) {
357 CompilerType compiler_type = type->GetForwardCompilerType();
358 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
359 compiler_type_set.insert(compiler_type);
360 type_list.Insert(type->shared_from_this());
361 ++num_types_added;
362 }
363 }
364 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000365}
366
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367//----------------------------------------------------------------------
368// Gets the first parent that is a lexical block, function or inlined
369// subroutine, or compile unit.
370//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000371DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
373 DWARFDIE die;
374 for (die = child_die.GetParent(); die; die = die.GetParent()) {
375 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 switch (tag) {
378 case DW_TAG_compile_unit:
379 case DW_TAG_subprogram:
380 case DW_TAG_inlined_subroutine:
381 case DW_TAG_lexical_block:
382 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 }
385 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386}
387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
389 : SymbolFile(objfile), UserID(0), // Used by SymbolFileDWARFDebugMap to when
390 // this class parses .o files to contain
391 // the .o file index/ID
392 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
393 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
394 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
395 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
396 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
397 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
398 m_apple_objc_ap(), m_function_basename_index(),
399 m_function_fullname_index(), m_function_method_index(),
400 m_function_selector_index(), m_objc_class_selectors_index(),
401 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
402 m_using_apple_tables(false), m_fetched_external_modules(false),
403 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
404 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406SymbolFileDWARF::~SymbolFileDWARF() {}
407
408static const ConstString &GetDWARFMachOSegmentName() {
409 static ConstString g_dwarf_section_name("__DWARF");
410 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
414 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
415 if (debug_map_symfile)
416 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
417 else
418 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000419}
420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
422 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
423 TypeSystem *type_system;
424 if (debug_map_symfile) {
425 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
426 } else {
427 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
428 if (type_system)
429 type_system->SetSymbolFile(this);
430 }
431 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000432}
433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434void SymbolFileDWARF::InitializeObject() {
435 ModuleSP module_sp(m_obj_file->GetModule());
436 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000437 const SectionList *section_list = module_sp->GetSectionList();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 const Section *section =
439 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
440
441 // Memory map the DWARF mach-o segment so we have everything mmap'ed
442 // to keep our heap memory usage down.
443 if (section)
444 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
445 }
446
447 get_apple_names_data();
448 if (m_data_apple_names.m_data.GetByteSize() > 0) {
449 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
450 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000451 if (m_apple_names_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000452 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000453 else
454 m_apple_names_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 }
456 get_apple_types_data();
457 if (m_data_apple_types.m_data.GetByteSize() > 0) {
458 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
459 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000460 if (m_apple_types_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000461 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000462 else
463 m_apple_types_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 }
465
466 get_apple_namespaces_data();
467 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
468 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
469 m_data_apple_namespaces.m_data, get_debug_str_data(),
470 ".apple_namespaces"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000471 if (m_apple_namespaces_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000472 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000473 else
474 m_apple_namespaces_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 }
476
477 get_apple_objc_data();
478 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
479 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
480 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
Jason Molenda19cf6892017-03-10 19:31:54 +0000481 if (m_apple_objc_ap->IsValid())
Jason Molendabc3a47f2017-03-10 06:38:19 +0000482 m_using_apple_tables = true;
Jason Molenda19cf6892017-03-10 19:31:54 +0000483 else
484 m_apple_objc_ap.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 }
486}
487
488bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
489 return version == 2 || version == 3 || version == 4;
490}
491
492uint32_t SymbolFileDWARF::CalculateAbilities() {
493 uint32_t abilities = 0;
494 if (m_obj_file != NULL) {
495 const Section *section = NULL;
496 const SectionList *section_list = m_obj_file->GetSectionList();
497 if (section_list == NULL)
498 return 0;
499
500 uint64_t debug_abbrev_file_size = 0;
501 uint64_t debug_info_file_size = 0;
502 uint64_t debug_line_file_size = 0;
503
504 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
505
506 if (section)
507 section_list = &section->GetChildren();
508
509 section =
510 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
511 if (section != NULL) {
512 debug_info_file_size = section->GetFileSize();
513
514 section =
515 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
516 .get();
517 if (section)
518 debug_abbrev_file_size = section->GetFileSize();
519
520 section =
521 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
522 .get();
523 if (section)
524 debug_line_file_size = section->GetFileSize();
525 } else {
526 const char *symfile_dir_cstr =
527 m_obj_file->GetFileSpec().GetDirectory().GetCString();
528 if (symfile_dir_cstr) {
529 if (strcasestr(symfile_dir_cstr, ".dsym")) {
530 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
531 // We have a dSYM file that didn't have a any debug info.
532 // If the string table has a size of 1, then it was made from
533 // an executable with no debug info, or from an executable that
534 // was stripped.
535 section =
536 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
537 .get();
538 if (section && section->GetFileSize() == 1) {
539 m_obj_file->GetModule()->ReportWarning(
540 "empty dSYM file detected, dSYM was created with an "
541 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547
548 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
549 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
550 LocalVariables | VariableTypes;
551
552 if (debug_line_file_size > 0)
553 abilities |= LineTables;
554 }
555 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556}
557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558const DWARFDataExtractor &
559SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
560 DWARFDataSegment &data_segment) {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000561 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
562 this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
563 });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
568 DWARFDataExtractor &data) {
569 ModuleSP module_sp(m_obj_file->GetModule());
570 const SectionList *section_list = module_sp->GetSectionList();
571 if (section_list) {
572 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
573 if (section_sp) {
574 // See if we memory mapped the DWARF segment?
575 if (m_dwarf_data.GetByteSize()) {
576 data.SetData(m_dwarf_data, section_sp->GetOffset(),
577 section_sp->GetFileSize());
578 } else {
579 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
580 data.Clear();
581 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
587 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
588 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589}
590
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
592 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
593}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
596 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
597 m_data_debug_aranges);
598}
599
600const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
601 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
602}
603
604const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
605 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
606}
607
608const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
609 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
610}
611
612const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
613 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
614}
615
616const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
617 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
618}
619
620const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
621 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
622 m_data_debug_ranges);
623}
624
625const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
626 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
627}
628
629const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
630 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
631 m_data_debug_str_offsets);
632}
633
634const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
635 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
636}
637
638const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
639 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
640}
641
642const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
643 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
644 m_data_apple_namespaces);
645}
646
647const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
648 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
649}
650
651DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
652 if (m_abbr.get() == NULL) {
653 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
654 if (debug_abbrev_data.GetByteSize() > 0) {
655 m_abbr.reset(new DWARFDebugAbbrev());
656 if (m_abbr.get())
657 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 }
660 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661}
662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
664 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
668 if (m_info.get() == NULL) {
669 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p",
670 LLVM_PRETTY_FUNCTION, static_cast<void *>(this));
671 if (get_debug_info_data().GetByteSize() > 0) {
672 m_info.reset(new DWARFDebugInfo());
673 if (m_info.get()) {
674 m_info->SetDwarfData(this);
675 }
Greg Clayton1f746072012-08-29 21:13:06 +0000676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 }
678 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679}
680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
682 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683}
684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685DWARFCompileUnit *
686SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
687 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000688 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689
690 DWARFDebugInfo *info = DebugInfo();
691 if (info) {
692 // Just a normal DWARF file whose user ID for the compile unit is
693 // the DWARF offset itself
694
695 DWARFCompileUnit *dwarf_cu =
696 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
697 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
698 dwarf_cu->SetUserData(comp_unit);
699 return dwarf_cu;
700 }
701 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702}
703
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
705 if (m_ranges.get() == NULL) {
706 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p",
707 LLVM_PRETTY_FUNCTION, static_cast<void *>(this));
708 if (get_debug_ranges_data().GetByteSize() > 0) {
709 m_ranges.reset(new DWARFDebugRanges());
710 if (m_ranges.get())
711 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000712 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 }
714 return m_ranges.get();
715}
716
717const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
718 return m_ranges.get();
719}
720
721lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit *dwarf_cu,
722 uint32_t cu_idx) {
723 CompUnitSP cu_sp;
724 if (dwarf_cu) {
725 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
726 if (comp_unit) {
727 // We already parsed this compile unit, had out a shared pointer to it
728 cu_sp = comp_unit->shared_from_this();
729 } else {
730 if (dwarf_cu->GetSymbolFileDWARF() != this) {
731 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
732 cu_idx);
733 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
734 // Let the debug map create the compile unit
735 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
736 dwarf_cu->SetUserData(cu_sp.get());
737 } else {
738 ModuleSP module_sp(m_obj_file->GetModule());
739 if (module_sp) {
740 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
741 if (cu_die) {
742 FileSpec cu_file_spec{cu_die.GetName(), false};
743 if (cu_file_spec) {
744 // If we have a full path to the compile unit, we don't need to
745 // resolve
746 // the file. This can be expensive e.g. when the source files are
747 // NFS mounted.
748 if (cu_file_spec.IsRelative()) {
749 const char *cu_comp_dir{
750 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
751 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
752 }
753
754 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000755 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 remapped_file))
757 cu_file_spec.SetFile(remapped_file, false);
758 }
759
760 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(
761 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
762
763 bool is_optimized = dwarf_cu->GetIsOptimized();
764 cu_sp.reset(new CompileUnit(
765 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
766 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
767 if (cu_sp) {
768 // If we just created a compile unit with an invalid file spec,
769 // try and get the
770 // first entry in the supports files from the line table as that
771 // should be the
772 // compile unit.
773 if (!cu_file_spec) {
774 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
775 if (cu_file_spec) {
776 (FileSpec &)(*cu_sp) = cu_file_spec;
777 // Also fix the invalid file spec which was copied from the
778 // compile unit.
779 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
780 }
781 }
782
783 dwarf_cu->SetUserData(cu_sp.get());
784
785 // Figure out the compile unit index if we weren't given one
786 if (cu_idx == UINT32_MAX)
787 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
788
789 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
790 cu_idx, cu_sp);
791 }
792 }
793 }
794 }
795 }
796 }
797 return cu_sp;
798}
799
800uint32_t SymbolFileDWARF::GetNumCompileUnits() {
801 DWARFDebugInfo *info = DebugInfo();
802 if (info)
803 return info->GetNumCompileUnits();
804 return 0;
805}
806
807CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
808 CompUnitSP cu_sp;
809 DWARFDebugInfo *info = DebugInfo();
810 if (info) {
811 DWARFCompileUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
812 if (dwarf_cu)
813 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
814 }
815 return cu_sp;
816}
817
818Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
819 const DWARFDIE &die) {
820 if (die.IsValid()) {
821 TypeSystem *type_system =
822 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
823
824 if (type_system) {
825 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
826 if (dwarf_ast)
827 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
828 }
829 }
830 return nullptr;
831}
832
833bool SymbolFileDWARF::FixupAddress(Address &addr) {
834 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
835 if (debug_map_symfile) {
836 return debug_map_symfile->LinkOSOAddress(addr);
837 }
838 // This is a normal DWARF file, no address fixups need to happen
839 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000840}
Greg Clayton1f746072012-08-29 21:13:06 +0000841lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
843 assert(sc.comp_unit);
844 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
845 if (dwarf_cu)
846 return dwarf_cu->GetLanguageType();
847 else
848 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000849}
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
852 assert(sc.comp_unit);
853 size_t functions_added = 0;
854 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
855 if (dwarf_cu) {
856 DWARFDIECollection function_dies;
857 const size_t num_functions =
858 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
859 size_t func_idx;
860 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
861 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
862 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
863 if (ParseCompileUnitFunction(sc, die))
864 ++functions_added;
865 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 // FixupTypes();
868 }
869 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870}
871
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
873 const SymbolContext &sc, FileSpecList &support_files) {
874 assert(sc.comp_unit);
875 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
876 if (dwarf_cu) {
877 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 if (cu_die) {
880 const char *cu_comp_dir = resolveCompDir(
881 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
882 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
883 DW_AT_stmt_list, DW_INVALID_OFFSET);
884 if (stmt_list != DW_INVALID_OFFSET) {
885 // All file indexes in DWARF are one based and a file of index zero is
886 // supposed to be the compile unit itself.
887 support_files.Append(*sc.comp_unit);
888 return DWARFDebugLine::ParseSupportFiles(
889 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
890 stmt_list, support_files);
891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 }
894 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
898 const lldb_private::SymbolContext &sc) {
899 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
900 if (dwarf_cu)
901 return dwarf_cu->GetIsOptimized();
902 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000903}
904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905bool SymbolFileDWARF::ParseImportedModules(
906 const lldb_private::SymbolContext &sc,
907 std::vector<lldb_private::ConstString> &imported_modules) {
908 assert(sc.comp_unit);
909 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
910 if (dwarf_cu) {
911 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
912 sc.comp_unit->GetLanguage())) {
913 UpdateExternalModuleListIfNeeded();
914
915 if (sc.comp_unit) {
916 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
917
918 if (die) {
919 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
920 child_die = child_die.GetSibling()) {
921 if (child_die.Tag() == DW_TAG_imported_declaration) {
922 if (DWARFDIE module_die =
923 child_die.GetReferencedDIE(DW_AT_import)) {
924 if (module_die.Tag() == DW_TAG_module) {
925 if (const char *name = module_die.GetAttributeValueAsString(
926 DW_AT_name, nullptr)) {
927 ConstString const_name(name);
928 imported_modules.push_back(const_name);
929 }
Sean Callananb0300a42016-01-14 21:46:09 +0000930 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 }
Sean Callananb0300a42016-01-14 21:46:09 +0000932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 } else {
936 for (const auto &pair : m_external_type_modules) {
937 imported_modules.push_back(pair.first);
938 }
939 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 }
942 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000943}
944
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945struct ParseDWARFLineTableCallbackInfo {
946 LineTable *line_table;
947 std::unique_ptr<LineSequence> sequence_ap;
948 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949};
950
951//----------------------------------------------------------------------
952// ParseStatementTableCallback
953//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954static void ParseDWARFLineTableCallback(dw_offset_t offset,
955 const DWARFDebugLine::State &state,
956 void *userData) {
957 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
958 // Just started parsing the line table
959 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
960 // Done parsing line table, nothing to do for the cleanup
961 } else {
962 ParseDWARFLineTableCallbackInfo *info =
963 (ParseDWARFLineTableCallbackInfo *)userData;
964 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 // If this is our first time here, we need to create a
967 // sequence container.
968 if (!info->sequence_ap.get()) {
969 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
970 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 line_table->AppendLineEntryToSequence(
973 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
974 state.column, state.file, state.is_stmt, state.basic_block,
975 state.prologue_end, state.epilogue_begin, state.end_sequence);
976 if (state.end_sequence) {
977 // First, put the current sequence into the line table.
978 line_table->InsertSequence(info->sequence_ap.get());
979 // Then, empty it to prepare for the next sequence.
980 info->sequence_ap->Clear();
981 }
982 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983}
984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
986 assert(sc.comp_unit);
987 if (sc.comp_unit->GetLineTable() != NULL)
988 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
991 if (dwarf_cu) {
992 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
993 if (dwarf_cu_die) {
994 const dw_offset_t cu_line_offset =
995 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
996 DW_INVALID_OFFSET);
997 if (cu_line_offset != DW_INVALID_OFFSET) {
998 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
999 if (line_table_ap.get()) {
1000 ParseDWARFLineTableCallbackInfo info;
1001 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001002
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 /*
1004 * MIPS:
1005 * The SymbolContext may not have a valid target, thus we may not be
1006 * able
1007 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1008 * #0
1009 * for MIPS. Use ArchSpec to clear the bit #0.
1010 */
1011 ArchSpec arch;
1012 GetObjectFile()->GetArchitecture(arch);
1013 switch (arch.GetMachine()) {
1014 case llvm::Triple::mips:
1015 case llvm::Triple::mipsel:
1016 case llvm::Triple::mips64:
1017 case llvm::Triple::mips64el:
1018 info.addr_mask = ~((lldb::addr_t)1);
1019 break;
1020 default:
1021 info.addr_mask = ~((lldb::addr_t)0);
1022 break;
1023 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 lldb::offset_t offset = cu_line_offset;
1026 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1027 ParseDWARFLineTableCallback,
1028 &info);
1029 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1030 if (debug_map_symfile) {
1031 // We have an object file that has a line table with addresses
1032 // that are not linked. We need to link the line table and convert
1033 // the addresses that are relative to the .o file into addresses
1034 // for the main executable.
1035 sc.comp_unit->SetLineTable(
1036 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1037 } else {
1038 sc.comp_unit->SetLineTable(line_table_ap.release());
1039 return true;
1040 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 }
1045 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046}
1047
Siva Chandrad8335e92015-12-16 00:22:08 +00001048lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1050 auto iter = m_debug_macros_map.find(*offset);
1051 if (iter != m_debug_macros_map.end())
1052 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1055 if (debug_macro_data.GetByteSize() == 0)
1056 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1059 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 const DWARFDebugMacroHeader &header =
1062 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1063 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1064 header.OffsetIs64Bit(), offset, this,
1065 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001068}
1069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1071 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1074 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001075 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076
1077 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1078 if (!dwarf_cu_die)
1079 return false;
1080
1081 lldb::offset_t sect_offset =
1082 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1083 if (sect_offset == DW_INVALID_OFFSET)
1084 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1085 DW_INVALID_OFFSET);
1086 if (sect_offset == DW_INVALID_OFFSET)
1087 return false;
1088
1089 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1090
1091 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001092}
1093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1095 Block *parent_block,
1096 const DWARFDIE &orig_die,
1097 addr_t subprogram_low_pc,
1098 uint32_t depth) {
1099 size_t blocks_added = 0;
1100 DWARFDIE die = orig_die;
1101 while (die) {
1102 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001103
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 switch (tag) {
1105 case DW_TAG_inlined_subroutine:
1106 case DW_TAG_subprogram:
1107 case DW_TAG_lexical_block: {
1108 Block *block = NULL;
1109 if (tag == DW_TAG_subprogram) {
1110 // Skip any DW_TAG_subprogram DIEs that are inside
1111 // of a normal or inlined functions. These will be
1112 // parsed on their own as separate entities.
1113
1114 if (depth > 0)
1115 break;
1116
1117 block = parent_block;
1118 } else {
1119 BlockSP block_sp(new Block(die.GetID()));
1120 parent_block->AddChild(block_sp);
1121 block = block_sp.get();
1122 }
1123 DWARFRangeList ranges;
1124 const char *name = NULL;
1125 const char *mangled_name = NULL;
1126
1127 int decl_file = 0;
1128 int decl_line = 0;
1129 int decl_column = 0;
1130 int call_file = 0;
1131 int call_line = 0;
1132 int call_column = 0;
1133 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1134 decl_line, decl_column, call_file, call_line,
1135 call_column, nullptr)) {
1136 if (tag == DW_TAG_subprogram) {
1137 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1138 subprogram_low_pc = ranges.GetMinRangeBase(0);
1139 } else if (tag == DW_TAG_inlined_subroutine) {
1140 // We get called here for inlined subroutines in two ways.
1141 // The first time is when we are making the Function object
1142 // for this inlined concrete instance. Since we're creating a top
1143 // level block at
1144 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1145 // need to
1146 // adjust the containing address.
1147 // The second time is when we are parsing the blocks inside the
1148 // function that contains
1149 // the inlined concrete instance. Since these will be blocks inside
1150 // the containing "real"
1151 // function the offset will be for that function.
1152 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1153 subprogram_low_pc = ranges.GetMinRangeBase(0);
1154 }
1155 }
1156
1157 const size_t num_ranges = ranges.GetSize();
1158 for (size_t i = 0; i < num_ranges; ++i) {
1159 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1160 const addr_t range_base = range.GetRangeBase();
1161 if (range_base >= subprogram_low_pc)
1162 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1163 range.GetByteSize()));
1164 else {
1165 GetObjectFile()->GetModule()->ReportError(
1166 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1167 ") which has a base that is less than the function's low PC "
1168 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1169 "start of this error message",
1170 block->GetID(), range_base, range.GetRangeEnd(),
1171 subprogram_low_pc);
1172 }
1173 }
1174 block->FinalizeRanges();
1175
1176 if (tag != DW_TAG_subprogram &&
1177 (name != NULL || mangled_name != NULL)) {
1178 std::unique_ptr<Declaration> decl_ap;
1179 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1180 decl_ap.reset(new Declaration(
1181 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1182 decl_line, decl_column));
1183
1184 std::unique_ptr<Declaration> call_ap;
1185 if (call_file != 0 || call_line != 0 || call_column != 0)
1186 call_ap.reset(new Declaration(
1187 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1188 call_line, call_column));
1189
1190 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1191 call_ap.get());
1192 }
1193
1194 ++blocks_added;
1195
1196 if (die.HasChildren()) {
1197 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1198 subprogram_low_pc, depth + 1);
1199 }
1200 }
1201 } break;
1202 default:
1203 break;
1204 }
1205
1206 // Only parse siblings of the block if we are not at depth zero. A depth
1207 // of zero indicates we are currently parsing the top level
1208 // DW_TAG_subprogram DIE
1209
1210 if (depth == 0)
1211 die.Clear();
1212 else
1213 die = die.GetSibling();
1214 }
1215 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001216}
1217
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1219 if (parent_die) {
1220 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1221 die = die.GetSibling()) {
1222 dw_tag_t tag = die.Tag();
1223 bool check_virtuality = false;
1224 switch (tag) {
1225 case DW_TAG_inheritance:
1226 case DW_TAG_subprogram:
1227 check_virtuality = true;
1228 break;
1229 default:
1230 break;
1231 }
1232 if (check_virtuality) {
1233 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1234 return true;
1235 }
1236 }
1237 }
1238 return false;
1239}
1240
1241void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1242 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1243 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1244 std::vector<DWARFDIE> decl_ctx_die_list =
1245 ast_parser->GetDIEForDeclContext(decl_ctx);
1246
1247 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1248 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1249 decl = decl.GetSibling())
1250 ast_parser->GetDeclForUIDFromDWARF(decl);
1251}
1252
1253SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1254 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1255 // we must make sure we use the correct DWARF file when resolving things.
1256 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1257 // SymbolFileDWARF classes, one for each .o file. We can often end up
1258 // with references to other DWARF objects and we must be ready to receive
1259 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1260 // instance.
1261 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1262 if (debug_map)
1263 return debug_map->GetSymbolFileByOSOIndex(
1264 debug_map->GetOSOIndexFromUserID(uid));
1265 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001266}
1267
1268DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1270 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1271 // we must make sure we use the correct DWARF file when resolving things.
1272 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1273 // SymbolFileDWARF classes, one for each .o file. We can often end up
1274 // with references to other DWARF objects and we must be ready to receive
1275 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1276 // instance.
1277 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1278 if (dwarf)
1279 return dwarf->GetDIE(DIERef(uid, dwarf));
1280 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001281}
1282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1284 // Anytime we have a lldb::user_id_t, we must get the DIE by
1285 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1286 // the SymbolFileDWARF::GetDIEFromUID() for details.
1287 DWARFDIE die = GetDIEFromUID(type_uid);
1288 if (die)
1289 return die.GetDecl();
1290 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001291}
1292
Greg Clayton99558cc42015-08-24 23:46:31 +00001293CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1295 // Anytime we have a lldb::user_id_t, we must get the DIE by
1296 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1297 // the SymbolFileDWARF::GetDIEFromUID() for details.
1298 DWARFDIE die = GetDIEFromUID(type_uid);
1299 if (die)
1300 return die.GetDeclContext();
1301 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001302}
1303
Greg Clayton99558cc42015-08-24 23:46:31 +00001304CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1306 // Anytime we have a lldb::user_id_t, we must get the DIE by
1307 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1308 // the SymbolFileDWARF::GetDIEFromUID() for details.
1309 DWARFDIE die = GetDIEFromUID(type_uid);
1310 if (die)
1311 return die.GetContainingDeclContext();
1312 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313}
1314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1316 // Anytime we have a lldb::user_id_t, we must get the DIE by
1317 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1318 // the SymbolFileDWARF::GetDIEFromUID() for details.
1319 DWARFDIE type_die = GetDIEFromUID(type_uid);
1320 if (type_die)
1321 return type_die.ResolveType();
1322 else
1323 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324}
1325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1327 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001328}
1329
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1331 bool assert_not_being_parsed) {
1332 if (die) {
1333 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1334 if (log)
1335 GetObjectFile()->GetModule()->LogMessage(
1336 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1337 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 // We might be coming in in the middle of a type tree (a class
1340 // within a class, an enum within a class), so parse any needed
1341 // parent DIEs before we get to this one...
1342 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1343 if (decl_ctx_die) {
1344 if (log) {
1345 switch (decl_ctx_die.Tag()) {
1346 case DW_TAG_structure_type:
1347 case DW_TAG_union_type:
1348 case DW_TAG_class_type: {
1349 // Get the type, which could be a forward declaration
1350 if (log)
1351 GetObjectFile()->GetModule()->LogMessage(
1352 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1353 "resolve parent forward type for 0x%8.8x",
1354 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1355 decl_ctx_die.GetOffset());
1356 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 default:
1359 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001360 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001362 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 return ResolveType(die);
1364 }
1365 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001366}
1367
Greg Clayton6beaaa62011-01-17 03:46:26 +00001368// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1369// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001370// can resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371bool SymbolFileDWARF::HasForwardDeclForClangType(
1372 const CompilerType &compiler_type) {
1373 CompilerType compiler_type_no_qualifiers =
1374 ClangUtil::RemoveFastQualifiers(compiler_type);
1375 if (GetForwardDeclClangTypeToDie().count(
1376 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1377 return true;
1378 }
1379 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001380
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 ClangASTContext *clang_type_system =
1382 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1383 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001384 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 DWARFASTParserClang *ast_parser =
1386 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1387 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001388}
1389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1391 std::lock_guard<std::recursive_mutex> guard(
1392 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001393
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 ClangASTContext *clang_type_system =
1395 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1396 if (clang_type_system) {
1397 DWARFASTParserClang *ast_parser =
1398 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1399 if (ast_parser &&
1400 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1401 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1402 }
Jim Inghamc3549282012-01-11 02:21:12 +00001403
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 // We have a struct/union/class/enum that needs to be fully resolved.
1405 CompilerType compiler_type_no_qualifiers =
1406 ClangUtil::RemoveFastQualifiers(compiler_type);
1407 auto die_it = GetForwardDeclClangTypeToDie().find(
1408 compiler_type_no_qualifiers.GetOpaqueQualType());
1409 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1410 // We have already resolved this type...
1411 return true;
1412 }
1413
1414 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1415 if (dwarf_die) {
1416 // Once we start resolving this type, remove it from the forward declaration
1417 // map in case anyone child members or other types require this type to get
1418 // resolved.
1419 // The type will get resolved when all of the calls to
1420 // SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1421 // are done.
1422 GetForwardDeclClangTypeToDie().erase(die_it);
1423
1424 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1425
1426 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1427 DWARF_LOG_TYPE_COMPLETION));
1428 if (log)
1429 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1430 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1431 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1432 type->GetName().AsCString());
1433 assert(compiler_type);
1434 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1435 if (dwarf_ast)
1436 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1437 }
1438 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001439}
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1442 bool assert_not_being_parsed,
1443 bool resolve_function_context) {
1444 if (die) {
1445 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1446
1447 if (assert_not_being_parsed) {
1448 if (type != DIE_IS_BEING_PARSED)
1449 return type;
1450
1451 GetObjectFile()->GetModule()->ReportError(
1452 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1453 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1454
1455 } else
1456 return type;
1457 }
1458 return nullptr;
1459}
1460
1461CompileUnit *
1462SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit *dwarf_cu,
1463 uint32_t cu_idx) {
1464 // Check if the symbol vendor already knows about this compile unit?
1465 if (dwarf_cu->GetUserData() == NULL) {
1466 // The symbol vendor doesn't know about this compile unit, we
1467 // need to parse and add it to the symbol vendor object.
1468 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1469 }
1470 return (CompileUnit *)dwarf_cu->GetUserData();
1471}
1472
1473size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1474 DIEArray &method_die_offsets) {
1475 method_die_offsets.clear();
1476 if (m_using_apple_tables) {
1477 if (m_apple_objc_ap.get())
1478 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1479 } else {
1480 if (!m_indexed)
1481 Index();
1482
1483 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1484 }
1485 return method_die_offsets.size();
1486}
1487
1488bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1489 sc.Clear(false);
1490
1491 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1494
1495 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1496 if (sc.function == NULL)
1497 sc.function = ParseCompileUnitFunction(sc, die);
1498
1499 if (sc.function) {
1500 sc.module_sp = sc.function->CalculateSymbolContextModule();
1501 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 }
1504
1505 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506}
1507
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1509 UpdateExternalModuleListIfNeeded();
1510 const auto &pos = m_external_type_modules.find(name);
1511 if (pos != m_external_type_modules.end())
1512 return pos->second;
1513 else
1514 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001515}
1516
Greg Clayton2f869fe2016-03-30 20:14:35 +00001517DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1519 DWARFDebugInfo *debug_info = DebugInfo();
1520 if (debug_info)
1521 return debug_info->GetDIE(die_ref);
1522 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001523 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001524}
1525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526std::unique_ptr<SymbolFileDWARFDwo>
1527SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1528 DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1529 // If we are using a dSYM file, we never want the standard DWO files since
1530 // the -gmodule support uses the same DWO machanism to specify full debug
1531 // info files for modules.
1532 if (GetDebugMapSymfile())
1533 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001534
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 const char *dwo_name = cu_die.GetAttributeValueAsString(
1536 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1537 if (!dwo_name)
1538 return nullptr;
1539
1540 FileSpec dwo_file(dwo_name, true);
1541 if (dwo_file.IsRelative()) {
1542 const char *comp_dir = cu_die.GetAttributeValueAsString(
1543 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1544 if (!comp_dir)
1545 return nullptr;
1546
1547 dwo_file.SetFile(comp_dir, true);
1548 dwo_file.AppendPathComponent(dwo_name);
1549 }
1550
1551 if (!dwo_file.Exists())
1552 return nullptr;
1553
1554 const lldb::offset_t file_offset = 0;
1555 DataBufferSP dwo_file_data_sp;
1556 lldb::offset_t dwo_file_data_offset = 0;
1557 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1558 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1559 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1560 if (dwo_obj_file == nullptr)
1561 return nullptr;
1562
1563 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001564}
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1567 if (m_fetched_external_modules)
1568 return;
1569 m_fetched_external_modules = true;
1570
1571 DWARFDebugInfo *debug_info = DebugInfo();
1572
1573 const uint32_t num_compile_units = GetNumCompileUnits();
1574 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1575 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1576
1577 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1578 if (die && die.HasChildren() == false) {
1579 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1580
1581 if (name) {
1582 ConstString const_name(name);
1583 if (m_external_type_modules.find(const_name) ==
1584 m_external_type_modules.end()) {
1585 ModuleSP module_sp;
1586 const char *dwo_path =
1587 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1588 if (dwo_path) {
1589 ModuleSpec dwo_module_spec;
1590 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1591 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1592 const char *comp_dir =
1593 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1594 if (comp_dir) {
1595 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1596 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1597 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001598 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 dwo_module_spec.GetArchitecture() =
1600 m_obj_file->GetModule()->GetArchitecture();
1601 // printf ("Loading dwo = '%s'\n", dwo_path);
1602 Error error = ModuleList::GetSharedModule(
1603 dwo_module_spec, module_sp, NULL, NULL, NULL);
1604 if (!module_sp) {
1605 GetObjectFile()->GetModule()->ReportWarning(
1606 "0x%8.8x: unable to locate module needed for external types: "
1607 "%s\nerror: %s\nDebugging will be degraded due to missing "
1608 "types. Rebuilding your project will regenerate the needed "
1609 "module files.",
1610 die.GetOffset(),
1611 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1612 error.AsCString("unknown error"));
1613 }
1614 }
1615 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001618 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 }
1620}
1621
1622SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1623 if (!m_global_aranges_ap) {
1624 m_global_aranges_ap.reset(new GlobalVariableMap());
1625
1626 ModuleSP module_sp = GetObjectFile()->GetModule();
1627 if (module_sp) {
1628 const size_t num_cus = module_sp->GetNumCompileUnits();
1629 for (size_t i = 0; i < num_cus; ++i) {
1630 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1631 if (cu_sp) {
1632 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1633 if (globals_sp) {
1634 const size_t num_globals = globals_sp->GetSize();
1635 for (size_t g = 0; g < num_globals; ++g) {
1636 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1637 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1638 const DWARFExpression &location = var_sp->LocationExpression();
1639 Value location_result;
1640 Error error;
1641 if (location.Evaluate(nullptr, nullptr, nullptr,
1642 LLDB_INVALID_ADDRESS, nullptr, nullptr,
1643 location_result, &error)) {
1644 if (location_result.GetValueType() ==
1645 Value::eValueTypeFileAddress) {
1646 lldb::addr_t file_addr =
1647 location_result.GetScalar().ULongLong();
1648 lldb::addr_t byte_size = 1;
1649 if (var_sp->GetType())
1650 byte_size = var_sp->GetType()->GetByteSize();
1651 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1652 file_addr, byte_size, var_sp.get()));
1653 }
1654 }
1655 }
1656 }
1657 }
1658 }
1659 }
1660 }
1661 m_global_aranges_ap->Sort();
1662 }
1663 return *m_global_aranges_ap;
1664}
1665
1666uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1667 uint32_t resolve_scope,
1668 SymbolContext &sc) {
1669 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "SymbolFileDWARF::"
1670 "ResolveSymbolContext (so_addr = { "
1671 "section = %p, offset = 0x%" PRIx64
1672 " }, resolve_scope = 0x%8.8x)",
1673 static_cast<void *>(so_addr.GetSection().get()),
1674 so_addr.GetOffset(), resolve_scope);
1675 uint32_t resolved = 0;
1676 if (resolve_scope &
1677 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1678 eSymbolContextLineEntry | eSymbolContextVariable)) {
1679 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1680
1681 DWARFDebugInfo *debug_info = DebugInfo();
1682 if (debug_info) {
1683 const dw_offset_t cu_offset =
1684 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1685 if (cu_offset == DW_INVALID_OFFSET) {
1686 // Global variables are not in the compile unit address ranges. The only
1687 // way to
1688 // currently find global variables is to iterate over the
1689 // .debug_pubnames or the
1690 // __apple_names table and find all items in there that point to
1691 // DW_TAG_variable
1692 // DIEs and then find the address that matches.
1693 if (resolve_scope & eSymbolContextVariable) {
1694 GlobalVariableMap &map = GetGlobalAranges();
1695 const GlobalVariableMap::Entry *entry =
1696 map.FindEntryThatContains(file_vm_addr);
1697 if (entry && entry->data) {
1698 Variable *variable = entry->data;
1699 SymbolContextScope *scc = variable->GetSymbolContextScope();
1700 if (scc) {
1701 scc->CalculateSymbolContext(&sc);
1702 sc.variable = variable;
1703 }
1704 return sc.GetResolvedMask();
1705 }
1706 }
1707 } else {
1708 uint32_t cu_idx = DW_INVALID_INDEX;
1709 DWARFCompileUnit *dwarf_cu =
1710 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1711 if (dwarf_cu) {
1712 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1713 if (sc.comp_unit) {
1714 resolved |= eSymbolContextCompUnit;
1715
1716 bool force_check_line_table = false;
1717 if (resolve_scope &
1718 (eSymbolContextFunction | eSymbolContextBlock)) {
1719 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1720 DWARFDIE block_die;
1721 if (function_die) {
1722 sc.function =
1723 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1724 if (sc.function == NULL)
1725 sc.function = ParseCompileUnitFunction(sc, function_die);
1726
1727 if (sc.function && (resolve_scope & eSymbolContextBlock))
1728 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1729 } else {
1730 // We might have had a compile unit that had discontiguous
1731 // address ranges where the gaps are symbols that don't have
1732 // any debug info. Discontiguous compile unit address ranges
1733 // should only happen when there aren't other functions from
1734 // other compile units in these gaps. This helps keep the size
1735 // of the aranges down.
1736 force_check_line_table = true;
1737 }
1738
1739 if (sc.function != NULL) {
1740 resolved |= eSymbolContextFunction;
1741
1742 if (resolve_scope & eSymbolContextBlock) {
1743 Block &block = sc.function->GetBlock(true);
1744
1745 if (block_die)
1746 sc.block = block.FindBlockByID(block_die.GetID());
1747 else
1748 sc.block = block.FindBlockByID(function_die.GetID());
1749 if (sc.block)
1750 resolved |= eSymbolContextBlock;
1751 }
1752 }
1753 }
1754
1755 if ((resolve_scope & eSymbolContextLineEntry) ||
1756 force_check_line_table) {
1757 LineTable *line_table = sc.comp_unit->GetLineTable();
1758 if (line_table != NULL) {
1759 // And address that makes it into this function should be in
1760 // terms
1761 // of this debug file if there is no debug map, or it will be an
1762 // address in the .o file which needs to be fixed up to be in
1763 // terms
1764 // of the debug map executable. Either way, calling
1765 // FixupAddress()
1766 // will work for us.
1767 Address exe_so_addr(so_addr);
1768 if (FixupAddress(exe_so_addr)) {
1769 if (line_table->FindLineEntryByAddress(exe_so_addr,
1770 sc.line_entry)) {
1771 resolved |= eSymbolContextLineEntry;
1772 }
1773 }
1774 }
1775 }
1776
1777 if (force_check_line_table &&
1778 !(resolved & eSymbolContextLineEntry)) {
1779 // We might have had a compile unit that had discontiguous
1780 // address ranges where the gaps are symbols that don't have
1781 // any debug info. Discontiguous compile unit address ranges
1782 // should only happen when there aren't other functions from
1783 // other compile units in these gaps. This helps keep the size
1784 // of the aranges down.
1785 sc.comp_unit = NULL;
1786 resolved &= ~eSymbolContextCompUnit;
1787 }
1788 } else {
1789 GetObjectFile()->GetModule()->ReportWarning(
1790 "0x%8.8x: compile unit %u failed to create a valid "
1791 "lldb_private::CompileUnit class.",
1792 cu_offset, cu_idx);
1793 }
1794 }
1795 }
1796 }
1797 }
1798 return resolved;
1799}
1800
1801uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1802 uint32_t line,
1803 bool check_inlines,
1804 uint32_t resolve_scope,
1805 SymbolContextList &sc_list) {
1806 const uint32_t prev_size = sc_list.GetSize();
1807 if (resolve_scope & eSymbolContextCompUnit) {
1808 DWARFDebugInfo *debug_info = DebugInfo();
1809 if (debug_info) {
1810 uint32_t cu_idx;
1811 DWARFCompileUnit *dwarf_cu = NULL;
1812
1813 for (cu_idx = 0;
1814 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1815 ++cu_idx) {
1816 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1817 const bool full_match = (bool)file_spec.GetDirectory();
1818 bool file_spec_matches_cu_file_spec =
1819 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1820 if (check_inlines || file_spec_matches_cu_file_spec) {
1821 SymbolContext sc(m_obj_file->GetModule());
1822 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1823 if (sc.comp_unit) {
1824 uint32_t file_idx = UINT32_MAX;
1825
1826 // If we are looking for inline functions only and we don't
1827 // find it in the support files, we are done.
1828 if (check_inlines) {
1829 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1830 1, file_spec, true);
1831 if (file_idx == UINT32_MAX)
1832 continue;
1833 }
1834
1835 if (line != 0) {
1836 LineTable *line_table = sc.comp_unit->GetLineTable();
1837
1838 if (line_table != NULL && line != 0) {
1839 // We will have already looked up the file index if
1840 // we are searching for inline entries.
1841 if (!check_inlines)
1842 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1843 1, file_spec, true);
1844
1845 if (file_idx != UINT32_MAX) {
1846 uint32_t found_line;
1847 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1848 0, file_idx, line, false, &sc.line_entry);
1849 found_line = sc.line_entry.line;
1850
1851 while (line_idx != UINT32_MAX) {
1852 sc.function = NULL;
1853 sc.block = NULL;
1854 if (resolve_scope &
1855 (eSymbolContextFunction | eSymbolContextBlock)) {
1856 const lldb::addr_t file_vm_addr =
1857 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1858 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1859 DWARFDIE function_die =
1860 dwarf_cu->LookupAddress(file_vm_addr);
1861 DWARFDIE block_die;
1862 if (function_die) {
1863 sc.function =
1864 sc.comp_unit
1865 ->FindFunctionByUID(function_die.GetID())
1866 .get();
1867 if (sc.function == NULL)
1868 sc.function =
1869 ParseCompileUnitFunction(sc, function_die);
1870
1871 if (sc.function &&
1872 (resolve_scope & eSymbolContextBlock))
1873 block_die =
1874 function_die.LookupDeepestBlock(file_vm_addr);
1875 }
1876
1877 if (sc.function != NULL) {
1878 Block &block = sc.function->GetBlock(true);
1879
1880 if (block_die)
1881 sc.block = block.FindBlockByID(block_die.GetID());
1882 else if (function_die)
1883 sc.block =
1884 block.FindBlockByID(function_die.GetID());
1885 }
1886 }
1887 }
1888
1889 sc_list.Append(sc);
1890 line_idx = line_table->FindLineEntryIndexByFileIndex(
1891 line_idx + 1, file_idx, found_line, true,
1892 &sc.line_entry);
1893 }
1894 }
1895 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1896 // only append the context if we aren't looking for inline call
1897 // sites
1898 // by file and line and if the file spec matches that of the
1899 // compile unit
1900 sc_list.Append(sc);
1901 }
1902 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1903 // only append the context if we aren't looking for inline call
1904 // sites
1905 // by file and line and if the file spec matches that of the
1906 // compile unit
1907 sc_list.Append(sc);
1908 }
1909
1910 if (!check_inlines)
1911 break;
1912 }
1913 }
1914 }
1915 }
1916 }
1917 return sc_list.GetSize() - prev_size;
1918}
1919
Jim Ingham7fca8c02017-04-28 00:51:06 +00001920void SymbolFileDWARF::PreloadSymbols() {
1921 std::lock_guard<std::recursive_mutex> guard(
1922 GetObjectFile()->GetModule()->GetMutex());
1923 Index();
1924}
1925
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926void SymbolFileDWARF::Index() {
1927 if (m_indexed)
1928 return;
1929 m_indexed = true;
1930 Timer scoped_timer(
1931 LLVM_PRETTY_FUNCTION, "SymbolFileDWARF::Index (%s)",
1932 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
1933
1934 DWARFDebugInfo *debug_info = DebugInfo();
1935 if (debug_info) {
1936 const uint32_t num_compile_units = GetNumCompileUnits();
1937 if (num_compile_units == 0)
1938 return;
1939
1940 std::vector<NameToDIE> function_basename_index(num_compile_units);
1941 std::vector<NameToDIE> function_fullname_index(num_compile_units);
1942 std::vector<NameToDIE> function_method_index(num_compile_units);
1943 std::vector<NameToDIE> function_selector_index(num_compile_units);
1944 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
1945 std::vector<NameToDIE> global_index(num_compile_units);
1946 std::vector<NameToDIE> type_index(num_compile_units);
1947 std::vector<NameToDIE> namespace_index(num_compile_units);
1948
1949 std::vector<bool> clear_cu_dies(num_compile_units, false);
Zachary Turner3bc714b2017-03-02 00:05:25 +00001950 auto parser_fn = [debug_info, &function_basename_index,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001951 &function_fullname_index, &function_method_index,
1952 &function_selector_index, &objc_class_selectors_index,
1953 &global_index, &type_index,
1954 &namespace_index](uint32_t cu_idx) {
1955 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1956 if (dwarf_cu) {
1957 dwarf_cu->Index(
1958 function_basename_index[cu_idx], function_fullname_index[cu_idx],
1959 function_method_index[cu_idx], function_selector_index[cu_idx],
1960 objc_class_selectors_index[cu_idx], global_index[cu_idx],
1961 type_index[cu_idx], namespace_index[cu_idx]);
1962 }
1963 return cu_idx;
1964 };
1965
Zachary Turner3bc714b2017-03-02 00:05:25 +00001966 auto extract_fn = [debug_info](uint32_t cu_idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001967 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1968 if (dwarf_cu) {
1969 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the
1970 // DIEs for a compile unit have already been parsed.
1971 return std::make_pair(cu_idx, dwarf_cu->ExtractDIEsIfNeeded(false) > 1);
1972 }
1973 return std::make_pair(cu_idx, false);
1974 };
1975
1976 // Create a task runner that extracts dies for each DWARF compile unit in a
1977 // separate thread
1978 TaskRunner<std::pair<uint32_t, bool>> task_runner_extract;
1979 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1980 task_runner_extract.AddTask(extract_fn, cu_idx);
1981
1982 //----------------------------------------------------------------------
1983 // First figure out which compile units didn't have their DIEs already
1984 // parsed and remember this. If no DIEs were parsed prior to this index
1985 // function call, we are going to want to clear the CU dies after we
1986 // are done indexing to make sure we don't pull in all DWARF dies, but
1987 // we need to wait until all compile units have been indexed in case
1988 // a DIE in one compile unit refers to another and the indexes accesses
1989 // those DIEs.
1990 //----------------------------------------------------------------------
1991 while (true) {
1992 auto f = task_runner_extract.WaitForNextCompletedTask();
1993 if (!f.valid())
1994 break;
1995 unsigned cu_idx;
1996 bool clear;
1997 std::tie(cu_idx, clear) = f.get();
1998 clear_cu_dies[cu_idx] = clear;
1999 }
2000
2001 // Now create a task runner that can index each DWARF compile unit in a
2002 // separate
2003 // thread so we can index quickly.
2004
2005 TaskRunner<uint32_t> task_runner;
2006 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2007 task_runner.AddTask(parser_fn, cu_idx);
2008
2009 while (true) {
2010 std::future<uint32_t> f = task_runner.WaitForNextCompletedTask();
2011 if (!f.valid())
2012 break;
2013 uint32_t cu_idx = f.get();
2014
2015 m_function_basename_index.Append(function_basename_index[cu_idx]);
2016 m_function_fullname_index.Append(function_fullname_index[cu_idx]);
2017 m_function_method_index.Append(function_method_index[cu_idx]);
2018 m_function_selector_index.Append(function_selector_index[cu_idx]);
2019 m_objc_class_selectors_index.Append(objc_class_selectors_index[cu_idx]);
2020 m_global_index.Append(global_index[cu_idx]);
2021 m_type_index.Append(type_index[cu_idx]);
2022 m_namespace_index.Append(namespace_index[cu_idx]);
2023 }
2024
2025 TaskPool::RunTasks([&]() { m_function_basename_index.Finalize(); },
2026 [&]() { m_function_fullname_index.Finalize(); },
2027 [&]() { m_function_method_index.Finalize(); },
2028 [&]() { m_function_selector_index.Finalize(); },
2029 [&]() { m_objc_class_selectors_index.Finalize(); },
2030 [&]() { m_global_index.Finalize(); },
2031 [&]() { m_type_index.Finalize(); },
2032 [&]() { m_namespace_index.Finalize(); });
2033
2034 //----------------------------------------------------------------------
2035 // Keep memory down by clearing DIEs for any compile units if indexing
2036 // caused us to load the compile unit's DIEs.
2037 //----------------------------------------------------------------------
2038 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2039 if (clear_cu_dies[cu_idx])
2040 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2041 }
2042
2043#if defined(ENABLE_DEBUG_PRINTF)
2044 StreamFile s(stdout, false);
2045 s.Printf("DWARF index for '%s':",
2046 GetObjectFile()->GetFileSpec().GetPath().c_str());
2047 s.Printf("\nFunction basenames:\n");
2048 m_function_basename_index.Dump(&s);
2049 s.Printf("\nFunction fullnames:\n");
2050 m_function_fullname_index.Dump(&s);
2051 s.Printf("\nFunction methods:\n");
2052 m_function_method_index.Dump(&s);
2053 s.Printf("\nFunction selectors:\n");
2054 m_function_selector_index.Dump(&s);
2055 s.Printf("\nObjective C class selectors:\n");
2056 m_objc_class_selectors_index.Dump(&s);
2057 s.Printf("\nGlobals and statics:\n");
2058 m_global_index.Dump(&s);
2059 s.Printf("\nTypes:\n");
2060 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002061 s.Printf("\nNamespaces:\n");
2062 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002063#endif
2064 }
2065}
2066
2067bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2068 const lldb_private::CompilerDeclContext *decl_ctx) {
2069 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
2070 // Invalid namespace decl which means we aren't matching only things
2071 // in this symbol file, so return true to indicate it matches this
2072 // symbol file.
2073 return true;
2074 }
2075
2076 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2077 TypeSystem *type_system = GetTypeSystemForLanguage(
2078 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2079 if (decl_ctx_type_system == type_system)
2080 return true; // The type systems match, return true
2081
2082 // The namespace AST was valid, and it does not match...
2083 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2084
2085 if (log)
2086 GetObjectFile()->GetModule()->LogMessage(
2087 log, "Valid namespace does not match symbol file");
2088
2089 return false;
2090}
2091
2092uint32_t SymbolFileDWARF::FindGlobalVariables(
2093 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2094 bool append, uint32_t max_matches, VariableList &variables) {
2095 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2096
2097 if (log)
2098 GetObjectFile()->GetModule()->LogMessage(
2099 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2100 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2101 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2102 max_matches);
2103
2104 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2105 return 0;
2106
2107 DWARFDebugInfo *info = DebugInfo();
2108 if (info == NULL)
2109 return 0;
2110
2111 // If we aren't appending the results to this list, then clear the list
2112 if (!append)
2113 variables.Clear();
2114
2115 // Remember how many variables are in the list before we search in case
2116 // we are appending the results to a variable list.
2117 const uint32_t original_size = variables.GetSize();
2118
2119 DIEArray die_offsets;
2120
2121 if (m_using_apple_tables) {
2122 if (m_apple_names_ap.get()) {
2123 const char *name_cstr = name.GetCString();
2124 llvm::StringRef basename;
2125 llvm::StringRef context;
2126
2127 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2128 basename))
2129 basename = name_cstr;
2130
2131 m_apple_names_ap->FindByName(basename.data(), die_offsets);
2132 }
2133 } else {
2134 // Index the DWARF if we haven't already
2135 if (!m_indexed)
2136 Index();
2137
2138 m_global_index.Find(name, die_offsets);
2139 }
2140
2141 const size_t num_die_matches = die_offsets.size();
2142 if (num_die_matches) {
2143 SymbolContext sc;
2144 sc.module_sp = m_obj_file->GetModule();
2145 assert(sc.module_sp);
2146
2147 bool done = false;
2148 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2149 const DIERef &die_ref = die_offsets[i];
2150 DWARFDIE die = GetDIE(die_ref);
2151
2152 if (die) {
2153 switch (die.Tag()) {
2154 default:
2155 case DW_TAG_subprogram:
2156 case DW_TAG_inlined_subroutine:
2157 case DW_TAG_try_block:
2158 case DW_TAG_catch_block:
2159 break;
2160
2161 case DW_TAG_variable: {
2162 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2163
2164 if (parent_decl_ctx) {
2165 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2166 if (dwarf_ast) {
2167 CompilerDeclContext actual_parent_decl_ctx =
2168 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2169 if (!actual_parent_decl_ctx ||
2170 actual_parent_decl_ctx != *parent_decl_ctx)
2171 continue;
2172 }
2173 }
2174
2175 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2176 &variables);
2177
2178 if (variables.GetSize() - original_size >= max_matches)
2179 done = true;
2180 } break;
2181 }
2182 } else {
2183 if (m_using_apple_tables) {
2184 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2185 "the DWARF debug information has been modified (.apple_names "
2186 "accelerator table had bad die 0x%8.8x for '%s')\n",
2187 die_ref.die_offset, name.GetCString());
2188 }
2189 }
2190 }
2191 }
2192
2193 // Return the number of variable that were appended to the list
2194 const uint32_t num_matches = variables.GetSize() - original_size;
2195 if (log && num_matches > 0) {
2196 GetObjectFile()->GetModule()->LogMessage(
2197 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2198 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2199 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2200 max_matches, num_matches);
2201 }
2202 return num_matches;
2203}
2204
2205uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2206 bool append, uint32_t max_matches,
2207 VariableList &variables) {
2208 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2209
2210 if (log) {
2211 GetObjectFile()->GetModule()->LogMessage(
2212 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2213 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002214 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 }
2216
2217 DWARFDebugInfo *info = DebugInfo();
2218 if (info == NULL)
2219 return 0;
2220
2221 // If we aren't appending the results to this list, then clear the list
2222 if (!append)
2223 variables.Clear();
2224
2225 // Remember how many variables are in the list before we search in case
2226 // we are appending the results to a variable list.
2227 const uint32_t original_size = variables.GetSize();
2228
2229 DIEArray die_offsets;
2230
2231 if (m_using_apple_tables) {
2232 if (m_apple_names_ap.get()) {
2233 DWARFMappedHash::DIEInfoArray hash_data_array;
2234 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2235 hash_data_array))
2236 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2237 }
2238 } else {
2239 // Index the DWARF if we haven't already
2240 if (!m_indexed)
2241 Index();
2242
2243 m_global_index.Find(regex, die_offsets);
2244 }
2245
2246 SymbolContext sc;
2247 sc.module_sp = m_obj_file->GetModule();
2248 assert(sc.module_sp);
2249
2250 const size_t num_matches = die_offsets.size();
2251 if (num_matches) {
2252 for (size_t i = 0; i < num_matches; ++i) {
2253 const DIERef &die_ref = die_offsets[i];
2254 DWARFDIE die = GetDIE(die_ref);
2255
2256 if (die) {
2257 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2258
2259 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2260
2261 if (variables.GetSize() - original_size >= max_matches)
2262 break;
2263 } else {
2264 if (m_using_apple_tables) {
2265 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2266 "the DWARF debug information has been modified (.apple_names "
2267 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002268 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269 }
2270 }
2271 }
2272 }
2273
2274 // Return the number of variable that were appended to the list
2275 return variables.GetSize() - original_size;
2276}
2277
2278bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2279 bool include_inlines,
2280 SymbolContextList &sc_list) {
2281 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2282 return ResolveFunction(die, include_inlines, sc_list);
2283}
2284
2285bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2286 bool include_inlines,
2287 SymbolContextList &sc_list) {
2288 SymbolContext sc;
2289
2290 if (!orig_die)
2291 return false;
2292
2293 // If we were passed a die that is not a function, just return false...
2294 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2295 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2296 return false;
2297
2298 DWARFDIE die = orig_die;
2299 DWARFDIE inlined_die;
2300 if (die.Tag() == DW_TAG_inlined_subroutine) {
2301 inlined_die = die;
2302
2303 while (1) {
2304 die = die.GetParent();
2305
2306 if (die) {
2307 if (die.Tag() == DW_TAG_subprogram)
2308 break;
2309 } else
2310 break;
2311 }
2312 }
2313 assert(die && die.Tag() == DW_TAG_subprogram);
2314 if (GetFunction(die, sc)) {
2315 Address addr;
2316 // Parse all blocks if needed
2317 if (inlined_die) {
2318 Block &function_block = sc.function->GetBlock(true);
2319 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2320 if (sc.block == NULL)
2321 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2322 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2323 addr.Clear();
2324 } else {
2325 sc.block = NULL;
2326 addr = sc.function->GetAddressRange().GetBaseAddress();
2327 }
2328
2329 if (addr.IsValid()) {
2330 sc_list.Append(sc);
2331 return true;
2332 }
2333 }
2334
2335 return false;
2336}
2337
2338void SymbolFileDWARF::FindFunctions(const ConstString &name,
2339 const NameToDIE &name_to_die,
2340 bool include_inlines,
2341 SymbolContextList &sc_list) {
2342 DIEArray die_offsets;
2343 if (name_to_die.Find(name, die_offsets)) {
2344 ParseFunctions(die_offsets, include_inlines, sc_list);
2345 }
2346}
2347
2348void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2349 const NameToDIE &name_to_die,
2350 bool include_inlines,
2351 SymbolContextList &sc_list) {
2352 DIEArray die_offsets;
2353 if (name_to_die.Find(regex, die_offsets)) {
2354 ParseFunctions(die_offsets, include_inlines, sc_list);
2355 }
2356}
2357
2358void SymbolFileDWARF::FindFunctions(
2359 const RegularExpression &regex,
2360 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2361 SymbolContextList &sc_list) {
2362 DIEArray die_offsets;
2363 DWARFMappedHash::DIEInfoArray hash_data_array;
2364 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2365 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2366 ParseFunctions(die_offsets, include_inlines, sc_list);
2367 }
2368}
2369
2370void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2371 bool include_inlines,
2372 SymbolContextList &sc_list) {
2373 const size_t num_matches = die_offsets.size();
2374 if (num_matches) {
2375 for (size_t i = 0; i < num_matches; ++i)
2376 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2377 }
2378}
2379
2380bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2381 const DWARFDIE &die) {
2382 // If we have no parent decl context to match this DIE matches, and if the
2383 // parent
2384 // decl context isn't valid, we aren't trying to look for any particular decl
2385 // context so any die matches.
2386 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2387 return true;
2388
2389 if (die) {
2390 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2391 if (dwarf_ast) {
2392 CompilerDeclContext actual_decl_ctx =
2393 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2394 if (actual_decl_ctx)
2395 return actual_decl_ctx == *decl_ctx;
2396 }
2397 }
2398 return false;
2399}
2400
2401uint32_t
2402SymbolFileDWARF::FindFunctions(const ConstString &name,
2403 const CompilerDeclContext *parent_decl_ctx,
2404 uint32_t name_type_mask, bool include_inlines,
2405 bool append, SymbolContextList &sc_list) {
2406 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
2407 "SymbolFileDWARF::FindFunctions (name = '%s')",
2408 name.AsCString());
2409
2410 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002411 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002412 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2413
2414 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2415
2416 if (log) {
2417 GetObjectFile()->GetModule()->LogMessage(
2418 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2419 "name_type_mask=0x%x, append=%u, sc_list)",
2420 name.GetCString(), name_type_mask, append);
2421 }
2422
2423 // If we aren't appending the results to this list, then clear the list
2424 if (!append)
2425 sc_list.Clear();
2426
2427 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2428 return 0;
2429
2430 // If name is empty then we won't find anything.
2431 if (name.IsEmpty())
2432 return 0;
2433
2434 // Remember how many sc_list are in the list before we search in case
2435 // we are appending the results to a variable list.
2436
2437 const char *name_cstr = name.GetCString();
2438
2439 const uint32_t original_size = sc_list.GetSize();
2440
2441 DWARFDebugInfo *info = DebugInfo();
2442 if (info == NULL)
2443 return 0;
2444
2445 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2446 if (m_using_apple_tables) {
2447 if (m_apple_names_ap.get()) {
2448
2449 DIEArray die_offsets;
2450
2451 uint32_t num_matches = 0;
2452
2453 if (name_type_mask & eFunctionNameTypeFull) {
2454 // If they asked for the full name, match what they typed. At some
2455 // point we may
2456 // want to canonicalize this (strip double spaces, etc. For now, we
2457 // just add all the
2458 // dies that we find by exact match.
2459 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2460 for (uint32_t i = 0; i < num_matches; i++) {
2461 const DIERef &die_ref = die_offsets[i];
2462 DWARFDIE die = info->GetDIE(die_ref);
2463 if (die) {
2464 if (!DIEInDeclContext(parent_decl_ctx, die))
2465 continue; // The containing decl contexts don't match
2466
2467 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2468 if (ResolveFunction(die, include_inlines, sc_list))
2469 resolved_dies.insert(die.GetDIE());
2470 }
2471 } else {
2472 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2473 "the DWARF debug information has been modified (.apple_names "
2474 "accelerator table had bad die 0x%8.8x for '%s')",
2475 die_ref.die_offset, name_cstr);
2476 }
2477 }
2478 }
2479
2480 if (name_type_mask & eFunctionNameTypeSelector) {
2481 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2482 return 0; // no selectors in namespaces
2483
2484 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2485 // Now make sure these are actually ObjC methods. In this case we can
2486 // simply look up the name,
2487 // and if it is an ObjC method name, we're good.
2488
2489 for (uint32_t i = 0; i < num_matches; i++) {
2490 const DIERef &die_ref = die_offsets[i];
2491 DWARFDIE die = info->GetDIE(die_ref);
2492 if (die) {
2493 const char *die_name = die.GetName();
2494 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2495 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2496 if (ResolveFunction(die, include_inlines, sc_list))
2497 resolved_dies.insert(die.GetDIE());
2498 }
2499 }
2500 } else {
2501 GetObjectFile()->GetModule()->ReportError(
2502 "the DWARF debug information has been modified (.apple_names "
2503 "accelerator table had bad die 0x%8.8x for '%s')",
2504 die_ref.die_offset, name_cstr);
2505 }
2506 }
2507 die_offsets.clear();
2508 }
2509
2510 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2511 name_type_mask & eFunctionNameTypeBase) {
2512 // The apple_names table stores just the "base name" of C++ methods in
2513 // the table. So we have to
2514 // extract the base name, look that up, and if there is any other
2515 // information in the name we were
2516 // passed in we have to post-filter based on that.
2517
2518 // FIXME: Arrange the logic above so that we don't calculate the base
2519 // name twice:
2520 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2521
2522 for (uint32_t i = 0; i < num_matches; i++) {
2523 const DIERef &die_ref = die_offsets[i];
2524 DWARFDIE die = info->GetDIE(die_ref);
2525 if (die) {
2526 if (!DIEInDeclContext(parent_decl_ctx, die))
2527 continue; // The containing decl contexts don't match
2528
2529 // If we get to here, the die is good, and we should add it:
2530 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2531 ResolveFunction(die, include_inlines, sc_list)) {
2532 bool keep_die = true;
2533 if ((name_type_mask &
2534 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2535 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
2536 // We are looking for either basenames or methods, so we need to
2537 // trim out the ones we won't want by looking at the type
2538 SymbolContext sc;
2539 if (sc_list.GetLastContext(sc)) {
2540 if (sc.block) {
2541 // We have an inlined function
2542 } else if (sc.function) {
2543 Type *type = sc.function->GetType();
2544
2545 if (type) {
2546 CompilerDeclContext decl_ctx =
2547 GetDeclContextContainingUID(type->GetID());
2548 if (decl_ctx.IsStructUnionOrClass()) {
2549 if (name_type_mask & eFunctionNameTypeBase) {
2550 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2551 keep_die = false;
2552 }
2553 } else {
2554 if (name_type_mask & eFunctionNameTypeMethod) {
2555 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2556 keep_die = false;
2557 }
2558 }
2559 } else {
2560 GetObjectFile()->GetModule()->ReportWarning(
2561 "function at die offset 0x%8.8x had no function type",
2562 die_ref.die_offset);
2563 }
2564 }
2565 }
2566 }
2567 if (keep_die)
2568 resolved_dies.insert(die.GetDIE());
2569 }
2570 } else {
2571 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2572 "the DWARF debug information has been modified (.apple_names "
2573 "accelerator table had bad die 0x%8.8x for '%s')",
2574 die_ref.die_offset, name_cstr);
2575 }
2576 }
2577 die_offsets.clear();
2578 }
2579 }
2580 } else {
2581
2582 // Index the DWARF if we haven't already
2583 if (!m_indexed)
2584 Index();
2585
2586 if (name_type_mask & eFunctionNameTypeFull) {
2587 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2588
2589 // FIXME Temporary workaround for global/anonymous namespace
2590 // functions debugging FreeBSD and Linux binaries.
2591 // If we didn't find any functions in the global namespace try
2592 // looking in the basename index but ignore any returned
2593 // functions that have a namespace but keep functions which
2594 // have an anonymous namespace
2595 // TODO: The arch in the object file isn't correct for MSVC
2596 // binaries on windows, we should find a way to make it
2597 // correct and handle those symbols as well.
2598 if (sc_list.GetSize() == original_size) {
2599 ArchSpec arch;
2600 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002601 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002602 SymbolContextList temp_sc_list;
2603 FindFunctions(name, m_function_basename_index, include_inlines,
2604 temp_sc_list);
2605 SymbolContext sc;
2606 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2607 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2608 ConstString mangled_name =
2609 sc.GetFunctionName(Mangled::ePreferMangled);
2610 ConstString demangled_name =
2611 sc.GetFunctionName(Mangled::ePreferDemangled);
2612 // Mangled names on Linux and FreeBSD are of the form:
2613 // _ZN18function_namespace13function_nameEv.
2614 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2615 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2616 21)) {
2617 sc_list.Append(sc);
2618 }
2619 }
2620 }
2621 }
2622 }
2623 }
2624 DIEArray die_offsets;
2625 if (name_type_mask & eFunctionNameTypeBase) {
2626 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2627 for (uint32_t i = 0; i < num_base; i++) {
2628 DWARFDIE die = info->GetDIE(die_offsets[i]);
2629 if (die) {
2630 if (!DIEInDeclContext(parent_decl_ctx, die))
2631 continue; // The containing decl contexts don't match
2632
2633 // If we get to here, the die is good, and we should add it:
2634 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2635 if (ResolveFunction(die, include_inlines, sc_list))
2636 resolved_dies.insert(die.GetDIE());
2637 }
2638 }
2639 }
2640 die_offsets.clear();
2641 }
2642
2643 if (name_type_mask & eFunctionNameTypeMethod) {
2644 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2645 return 0; // no methods in namespaces
2646
2647 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2648 {
2649 for (uint32_t i = 0; i < num_base; i++) {
2650 DWARFDIE die = info->GetDIE(die_offsets[i]);
2651 if (die) {
2652 // If we get to here, the die is good, and we should add it:
2653 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2654 if (ResolveFunction(die, include_inlines, sc_list))
2655 resolved_dies.insert(die.GetDIE());
2656 }
2657 }
2658 }
2659 }
2660 die_offsets.clear();
2661 }
2662
2663 if ((name_type_mask & eFunctionNameTypeSelector) &&
2664 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2665 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2666 }
2667 }
2668
2669 // Return the number of variable that were appended to the list
2670 const uint32_t num_matches = sc_list.GetSize() - original_size;
2671
2672 if (log && num_matches > 0) {
2673 GetObjectFile()->GetModule()->LogMessage(
2674 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2675 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2676 "%u",
2677 name.GetCString(), name_type_mask, include_inlines, append,
2678 num_matches);
2679 }
2680 return num_matches;
2681}
2682
2683uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2684 bool include_inlines, bool append,
2685 SymbolContextList &sc_list) {
2686 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
2687 "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002688 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002689
2690 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2691
2692 if (log) {
2693 GetObjectFile()->GetModule()->LogMessage(
2694 log,
2695 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002696 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002697 }
2698
2699 // If we aren't appending the results to this list, then clear the list
2700 if (!append)
2701 sc_list.Clear();
2702
2703 // Remember how many sc_list are in the list before we search in case
2704 // we are appending the results to a variable list.
2705 uint32_t original_size = sc_list.GetSize();
2706
2707 if (m_using_apple_tables) {
2708 if (m_apple_names_ap.get())
2709 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2710 } else {
2711 // Index the DWARF if we haven't already
2712 if (!m_indexed)
2713 Index();
2714
2715 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2716
2717 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2718 }
2719
2720 // Return the number of variable that were appended to the list
2721 return sc_list.GetSize() - original_size;
2722}
2723
2724void SymbolFileDWARF::GetMangledNamesForFunction(
2725 const std::string &scope_qualified_name,
2726 std::vector<ConstString> &mangled_names) {
2727 DWARFDebugInfo *info = DebugInfo();
2728 uint32_t num_comp_units = 0;
2729 if (info)
2730 num_comp_units = info->GetNumCompileUnits();
2731
2732 for (uint32_t i = 0; i < num_comp_units; i++) {
2733 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2734 if (cu == nullptr)
2735 continue;
2736
2737 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2738 if (dwo)
2739 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2740 }
2741
2742 NameToOffsetMap::iterator iter =
2743 m_function_scope_qualified_name_map.find(scope_qualified_name);
2744 if (iter == m_function_scope_qualified_name_map.end())
2745 return;
2746
2747 DIERefSetSP set_sp = (*iter).second;
2748 std::set<DIERef>::iterator set_iter;
2749 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2750 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2751 mangled_names.push_back(ConstString(die.GetMangledName()));
2752 }
2753}
2754
2755uint32_t SymbolFileDWARF::FindTypes(
2756 const SymbolContext &sc, const ConstString &name,
2757 const CompilerDeclContext *parent_decl_ctx, bool append,
2758 uint32_t max_matches,
2759 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2760 TypeMap &types) {
2761 // If we aren't appending the results to this list, then clear the list
2762 if (!append)
2763 types.Clear();
2764
2765 // Make sure we haven't already searched this SymbolFile before...
2766 if (searched_symbol_files.count(this))
2767 return 0;
2768 else
2769 searched_symbol_files.insert(this);
2770
2771 DWARFDebugInfo *info = DebugInfo();
2772 if (info == NULL)
2773 return 0;
2774
2775 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2776
2777 if (log) {
2778 if (parent_decl_ctx)
2779 GetObjectFile()->GetModule()->LogMessage(
2780 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2781 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2782 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2783 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2784 else
2785 GetObjectFile()->GetModule()->LogMessage(
2786 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2787 "NULL, append=%u, max_matches=%u, type_list)",
2788 name.GetCString(), append, max_matches);
2789 }
2790
2791 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2792 return 0;
2793
2794 DIEArray die_offsets;
2795
2796 if (m_using_apple_tables) {
2797 if (m_apple_types_ap.get()) {
2798 const char *name_cstr = name.GetCString();
2799 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2800 }
2801 } else {
2802 if (!m_indexed)
2803 Index();
2804
2805 m_type_index.Find(name, die_offsets);
2806 }
2807
2808 const size_t num_die_matches = die_offsets.size();
2809
2810 if (num_die_matches) {
2811 const uint32_t initial_types_size = types.GetSize();
2812 for (size_t i = 0; i < num_die_matches; ++i) {
2813 const DIERef &die_ref = die_offsets[i];
2814 DWARFDIE die = GetDIE(die_ref);
2815
2816 if (die) {
2817 if (!DIEInDeclContext(parent_decl_ctx, die))
2818 continue; // The containing decl contexts don't match
2819
2820 Type *matching_type = ResolveType(die, true, true);
2821 if (matching_type) {
2822 // We found a type pointer, now find the shared pointer form our type
2823 // list
2824 types.InsertUnique(matching_type->shared_from_this());
2825 if (types.GetSize() >= max_matches)
2826 break;
2827 }
2828 } else {
2829 if (m_using_apple_tables) {
2830 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2831 "the DWARF debug information has been modified (.apple_types "
2832 "accelerator table had bad die 0x%8.8x for '%s')\n",
2833 die_ref.die_offset, name.GetCString());
2834 }
2835 }
2836 }
2837 const uint32_t num_matches = types.GetSize() - initial_types_size;
2838 if (log && num_matches) {
2839 if (parent_decl_ctx) {
2840 GetObjectFile()->GetModule()->LogMessage(
2841 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2842 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2843 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2844 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2845 num_matches);
2846 } else {
2847 GetObjectFile()->GetModule()->LogMessage(
2848 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2849 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2850 name.GetCString(), append, max_matches, num_matches);
2851 }
2852 }
2853 return num_matches;
2854 } else {
2855 UpdateExternalModuleListIfNeeded();
2856
2857 for (const auto &pair : m_external_type_modules) {
2858 ModuleSP external_module_sp = pair.second;
2859 if (external_module_sp) {
2860 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2861 if (sym_vendor) {
2862 const uint32_t num_external_matches =
2863 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2864 max_matches, searched_symbol_files, types);
2865 if (num_external_matches)
2866 return num_external_matches;
2867 }
2868 }
2869 }
2870 }
2871
2872 return 0;
2873}
2874
2875size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2876 bool append, TypeMap &types) {
2877 if (!append)
2878 types.Clear();
2879
2880 if (context.empty())
2881 return 0;
2882
2883 DIEArray die_offsets;
2884
2885 ConstString name = context.back().name;
2886
2887 if (!name)
2888 return 0;
2889
2890 if (m_using_apple_tables) {
2891 if (m_apple_types_ap.get()) {
2892 const char *name_cstr = name.GetCString();
2893 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2894 }
2895 } else {
2896 if (!m_indexed)
2897 Index();
2898
2899 m_type_index.Find(name, die_offsets);
2900 }
2901
2902 const size_t num_die_matches = die_offsets.size();
2903
2904 if (num_die_matches) {
2905 size_t num_matches = 0;
2906 for (size_t i = 0; i < num_die_matches; ++i) {
2907 const DIERef &die_ref = die_offsets[i];
2908 DWARFDIE die = GetDIE(die_ref);
2909
2910 if (die) {
2911 std::vector<CompilerContext> die_context;
2912 die.GetDWOContext(die_context);
2913 if (die_context != context)
2914 continue;
2915
2916 Type *matching_type = ResolveType(die, true, true);
2917 if (matching_type) {
2918 // We found a type pointer, now find the shared pointer form our type
2919 // list
2920 types.InsertUnique(matching_type->shared_from_this());
2921 ++num_matches;
2922 }
2923 } else {
2924 if (m_using_apple_tables) {
2925 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2926 "the DWARF debug information has been modified (.apple_types "
2927 "accelerator table had bad die 0x%8.8x for '%s')\n",
2928 die_ref.die_offset, name.GetCString());
2929 }
2930 }
2931 }
2932 return num_matches;
2933 }
2934 return 0;
2935}
2936
2937CompilerDeclContext
2938SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2939 const CompilerDeclContext *parent_decl_ctx) {
2940 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2941
2942 if (log) {
2943 GetObjectFile()->GetModule()->LogMessage(
2944 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2945 name.GetCString());
2946 }
2947
2948 CompilerDeclContext namespace_decl_ctx;
2949
2950 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2951 return namespace_decl_ctx;
2952
2953 DWARFDebugInfo *info = DebugInfo();
2954 if (info) {
2955 DIEArray die_offsets;
2956
2957 // Index if we already haven't to make sure the compile units
2958 // get indexed and make their global DIE index list
2959 if (m_using_apple_tables) {
2960 if (m_apple_namespaces_ap.get()) {
2961 const char *name_cstr = name.GetCString();
2962 m_apple_namespaces_ap->FindByName(name_cstr, die_offsets);
2963 }
2964 } else {
2965 if (!m_indexed)
2966 Index();
2967
2968 m_namespace_index.Find(name, die_offsets);
2969 }
2970
2971 const size_t num_matches = die_offsets.size();
2972 if (num_matches) {
2973 for (size_t i = 0; i < num_matches; ++i) {
2974 const DIERef &die_ref = die_offsets[i];
2975 DWARFDIE die = GetDIE(die_ref);
2976
2977 if (die) {
2978 if (!DIEInDeclContext(parent_decl_ctx, die))
2979 continue; // The containing decl contexts don't match
2980
2981 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2982 if (dwarf_ast) {
2983 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2984 if (namespace_decl_ctx)
2985 break;
2986 }
2987 } else {
2988 if (m_using_apple_tables) {
2989 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2990 "the DWARF debug information has been modified "
2991 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
2992 "'%s')\n",
2993 die_ref.die_offset, name.GetCString());
2994 }
2995 }
2996 }
2997 }
2998 }
2999 if (log && namespace_decl_ctx) {
3000 GetObjectFile()->GetModule()->LogMessage(
3001 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
3002 "CompilerDeclContext(%p/%p) \"%s\"",
3003 name.GetCString(),
3004 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
3005 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
3006 namespace_decl_ctx.GetName().AsCString("<NULL>"));
3007 }
3008
3009 return namespace_decl_ctx;
3010}
3011
3012TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3013 bool resolve_function_context) {
3014 TypeSP type_sp;
3015 if (die) {
3016 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3017 if (type_ptr == NULL) {
3018 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3019 assert(lldb_cu);
3020 SymbolContext sc(lldb_cu);
3021 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3022 while (parent_die != nullptr) {
3023 if (parent_die->Tag() == DW_TAG_subprogram)
3024 break;
3025 parent_die = parent_die->GetParent();
3026 }
3027 SymbolContext sc_backup = sc;
3028 if (resolve_function_context && parent_die != nullptr &&
3029 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3030 sc = sc_backup;
3031
3032 type_sp = ParseType(sc, die, NULL);
3033 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3034 // Grab the existing type from the master types lists
3035 type_sp = type_ptr->shared_from_this();
3036 }
3037 }
3038 return type_sp;
3039}
3040
3041DWARFDIE
3042SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3043 if (orig_die) {
3044 DWARFDIE die = orig_die;
3045
3046 while (die) {
3047 // If this is the original DIE that we are searching for a declaration
3048 // for, then don't look in the cache as we don't want our own decl
3049 // context to be our decl context...
3050 if (orig_die != die) {
3051 switch (die.Tag()) {
3052 case DW_TAG_compile_unit:
3053 case DW_TAG_namespace:
3054 case DW_TAG_structure_type:
3055 case DW_TAG_union_type:
3056 case DW_TAG_class_type:
3057 case DW_TAG_lexical_block:
3058 case DW_TAG_subprogram:
3059 return die;
Sean Callananb4945782017-04-24 22:11:10 +00003060 case DW_TAG_inlined_subroutine: {
3061 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3062 if (abs_die) {
3063 return abs_die;
3064 }
3065 break;
3066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003067 default:
3068 break;
3069 }
3070 }
3071
3072 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3073 if (spec_die) {
3074 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3075 if (decl_ctx_die)
3076 return decl_ctx_die;
3077 }
3078
3079 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3080 if (abs_die) {
3081 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3082 if (decl_ctx_die)
3083 return decl_ctx_die;
3084 }
3085
3086 die = die.GetParent();
3087 }
3088 }
3089 return DWARFDIE();
3090}
3091
3092Symbol *
3093SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3094 Symbol *objc_class_symbol = NULL;
3095 if (m_obj_file) {
3096 Symtab *symtab = m_obj_file->GetSymtab();
3097 if (symtab) {
3098 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3099 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3100 Symtab::eVisibilityAny);
3101 }
3102 }
3103 return objc_class_symbol;
3104}
3105
3106// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
3107// they don't
3108// then we can end up looking through all class types for a complete type and
3109// never find
3110// the full definition. We need to know if this attribute is supported, so we
3111// determine
3112// this here and cache th result. We also need to worry about the debug map
3113// DWARF file
3114// if we are doing darwin DWARF in .o file debugging.
3115bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
3116 DWARFCompileUnit *cu) {
3117 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3118 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3119 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3120 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3121 else {
3122 DWARFDebugInfo *debug_info = DebugInfo();
3123 const uint32_t num_compile_units = GetNumCompileUnits();
3124 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
3125 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3126 if (dwarf_cu != cu &&
3127 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3128 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3129 break;
3130 }
3131 }
3132 }
3133 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3134 GetDebugMapSymfile())
3135 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3136 }
3137 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003138}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003139
3140// This function can be used when a DIE is found that is a forward declaration
3141// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3143 const DWARFDIE &die, const ConstString &type_name,
3144 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003145
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 TypeSP type_sp;
3147
3148 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3149 return type_sp;
3150
3151 DIEArray die_offsets;
3152
3153 if (m_using_apple_tables) {
3154 if (m_apple_types_ap.get()) {
3155 const char *name_cstr = type_name.GetCString();
3156 m_apple_types_ap->FindCompleteObjCClassByName(name_cstr, die_offsets,
3157 must_be_implementation);
3158 }
3159 } else {
3160 if (!m_indexed)
3161 Index();
3162
3163 m_type_index.Find(type_name, die_offsets);
3164 }
3165
3166 const size_t num_matches = die_offsets.size();
3167
3168 if (num_matches) {
3169 for (size_t i = 0; i < num_matches; ++i) {
3170 const DIERef &die_ref = die_offsets[i];
3171 DWARFDIE type_die = GetDIE(die_ref);
3172
3173 if (type_die) {
3174 bool try_resolving_type = false;
3175
3176 // Don't try and resolve the DIE we are looking for with the DIE itself!
3177 if (type_die != die) {
3178 switch (type_die.Tag()) {
3179 case DW_TAG_class_type:
3180 case DW_TAG_structure_type:
3181 try_resolving_type = true;
3182 break;
3183 default:
3184 break;
3185 }
3186 }
3187
3188 if (try_resolving_type) {
3189 if (must_be_implementation &&
3190 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3191 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3192 DW_AT_APPLE_objc_complete_type, 0);
3193
3194 if (try_resolving_type) {
3195 Type *resolved_type = ResolveType(type_die, false, true);
3196 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3197 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3198 " (cu 0x%8.8" PRIx64 ")\n",
3199 die.GetID(),
3200 m_obj_file->GetFileSpec().GetFilename().AsCString(
3201 "<Unknown>"),
3202 type_die.GetID(), type_cu->GetID());
3203
3204 if (die)
3205 GetDIEToType()[die.GetDIE()] = resolved_type;
3206 type_sp = resolved_type->shared_from_this();
3207 break;
3208 }
3209 }
3210 }
3211 } else {
3212 if (m_using_apple_tables) {
3213 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3214 "the DWARF debug information has been modified (.apple_types "
3215 "accelerator table had bad die 0x%8.8x for '%s')\n",
3216 die_ref.die_offset, type_name.GetCString());
3217 }
3218 }
3219 }
3220 }
3221 return type_sp;
3222}
Greg Claytona8022fa2012-04-24 21:22:41 +00003223
Greg Clayton80c26302012-02-05 06:12:47 +00003224//----------------------------------------------------------------------
3225// This function helps to ensure that the declaration contexts match for
Kate Stoneb9c1b512016-09-06 20:57:50 +00003226// two different DIEs. Often times debug information will refer to a
Greg Clayton80c26302012-02-05 06:12:47 +00003227// forward declaration of a type (the equivalent of "struct my_struct;".
3228// There will often be a declaration of that type elsewhere that has the
3229// full definition. When we go looking for the full type "my_struct", we
3230// will find one or more matches in the accelerator tables and we will
Kate Stoneb9c1b512016-09-06 20:57:50 +00003231// then need to make sure the type was in the same declaration context
Greg Clayton80c26302012-02-05 06:12:47 +00003232// as the original DIE. This function can efficiently compare two DIEs
3233// and will return true when the declaration context matches, and false
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234// when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003235//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003236bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3237 const DWARFDIE &die2) {
3238 if (die1 == die2)
3239 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003240
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241 DWARFDIECollection decl_ctx_1;
3242 DWARFDIECollection decl_ctx_2;
3243 // The declaration DIE stack is a stack of the declaration context
3244 // DIEs all the way back to the compile unit. If a type "T" is
3245 // declared inside a class "B", and class "B" is declared inside
3246 // a class "A" and class "A" is in a namespace "lldb", and the
3247 // namespace is in a compile unit, there will be a stack of DIEs:
3248 //
3249 // [0] DW_TAG_class_type for "B"
3250 // [1] DW_TAG_class_type for "A"
3251 // [2] DW_TAG_namespace for "lldb"
3252 // [3] DW_TAG_compile_unit for the source file.
3253 //
3254 // We grab both contexts and make sure that everything matches
3255 // all the way back to the compiler unit.
3256
3257 // First lets grab the decl contexts for both DIEs
3258 die1.GetDeclContextDIEs(decl_ctx_1);
3259 die2.GetDeclContextDIEs(decl_ctx_2);
3260 // Make sure the context arrays have the same size, otherwise
3261 // we are done
3262 const size_t count1 = decl_ctx_1.Size();
3263 const size_t count2 = decl_ctx_2.Size();
3264 if (count1 != count2)
3265 return false;
3266
3267 // Make sure the DW_TAG values match all the way back up the
3268 // compile unit. If they don't, then we are done.
3269 DWARFDIE decl_ctx_die1;
3270 DWARFDIE decl_ctx_die2;
3271 size_t i;
3272 for (i = 0; i < count1; i++) {
3273 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3274 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3275 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3276 return false;
3277 }
Greg Clayton890ff562012-02-02 05:48:16 +00003278#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003279
Kate Stoneb9c1b512016-09-06 20:57:50 +00003280 // Make sure the top item in the decl context die array is always
3281 // DW_TAG_compile_unit. If it isn't then something went wrong in
3282 // the DWARFDIE::GetDeclContextDIEs() function...
3283 assert(decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003284
Greg Clayton890ff562012-02-02 05:48:16 +00003285#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286 // Always skip the compile unit when comparing by only iterating up to
3287 // "count - 1". Here we compare the names as we go.
3288 for (i = 0; i < count1 - 1; i++) {
3289 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3290 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3291 const char *name1 = decl_ctx_die1.GetName();
3292 const char *name2 = decl_ctx_die2.GetName();
3293 // If the string was from a DW_FORM_strp, then the pointer will often
3294 // be the same!
3295 if (name1 == name2)
3296 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003297
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 // Name pointers are not equal, so only compare the strings
3299 // if both are not NULL.
3300 if (name1 && name2) {
3301 // If the strings don't compare, we are done...
3302 if (strcmp(name1, name2) != 0)
3303 return false;
3304 } else {
3305 // One name was NULL while the other wasn't
3306 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003307 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003308 }
3309 // We made it through all of the checks and the declaration contexts
3310 // are equal.
3311 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003312}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003313
Kate Stoneb9c1b512016-09-06 20:57:50 +00003314TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3315 const DWARFDeclContext &dwarf_decl_ctx) {
3316 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003317
Kate Stoneb9c1b512016-09-06 20:57:50 +00003318 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3319 if (dwarf_decl_ctx_count > 0) {
3320 const ConstString type_name(dwarf_decl_ctx[0].name);
3321 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003322
Kate Stoneb9c1b512016-09-06 20:57:50 +00003323 if (type_name) {
3324 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3325 DWARF_LOG_LOOKUPS));
3326 if (log) {
3327 GetObjectFile()->GetModule()->LogMessage(
3328 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3329 "s, qualified-name='%s')",
3330 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3331 dwarf_decl_ctx.GetQualifiedName());
3332 }
3333
3334 DIEArray die_offsets;
3335
3336 if (m_using_apple_tables) {
3337 if (m_apple_types_ap.get()) {
3338 const bool has_tag =
3339 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3340 DWARFMappedHash::eAtomTypeTag);
3341 const bool has_qualified_name_hash =
3342 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3343 DWARFMappedHash::eAtomTypeQualNameHash);
3344 if (has_tag && has_qualified_name_hash) {
3345 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3346 const uint32_t qualified_name_hash =
3347 MappedHash::HashStringUsingDJB(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003348 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003349 GetObjectFile()->GetModule()->LogMessage(
3350 log, "FindByNameAndTagAndQualifiedNameHash()");
3351 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
3352 type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3353 } else if (has_tag) {
3354 if (log)
3355 GetObjectFile()->GetModule()->LogMessage(log,
3356 "FindByNameAndTag()");
3357 m_apple_types_ap->FindByNameAndTag(type_name.GetCString(), tag,
3358 die_offsets);
3359 } else {
3360 m_apple_types_ap->FindByName(type_name.GetCString(), die_offsets);
3361 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003362 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003363 } else {
3364 if (!m_indexed)
3365 Index();
3366
3367 m_type_index.Find(type_name, die_offsets);
3368 }
3369
3370 const size_t num_matches = die_offsets.size();
3371
3372 // Get the type system that we are looking to find a type for. We will use
3373 // this
3374 // to ensure any matches we find are in a language that this type system
3375 // supports
3376 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3377 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3378 ? nullptr
3379 : GetTypeSystemForLanguage(language);
3380
3381 if (num_matches) {
3382 for (size_t i = 0; i < num_matches; ++i) {
3383 const DIERef &die_ref = die_offsets[i];
3384 DWARFDIE type_die = GetDIE(die_ref);
3385
3386 if (type_die) {
3387 // Make sure type_die's langauge matches the type system we are
3388 // looking for.
3389 // We don't want to find a "Foo" type from Java if we are looking
3390 // for a "Foo"
3391 // type for C, C++, ObjC, or ObjC++.
3392 if (type_system &&
3393 !type_system->SupportsLanguage(type_die.GetLanguage()))
3394 continue;
3395 bool try_resolving_type = false;
3396
3397 // Don't try and resolve the DIE we are looking for with the DIE
3398 // itself!
3399 const dw_tag_t type_tag = type_die.Tag();
3400 // Make sure the tags match
3401 if (type_tag == tag) {
3402 // The tags match, lets try resolving this type
3403 try_resolving_type = true;
3404 } else {
3405 // The tags don't match, but we need to watch our for a
3406 // forward declaration for a struct and ("struct foo")
3407 // ends up being a class ("class foo { ... };") or
3408 // vice versa.
3409 switch (type_tag) {
3410 case DW_TAG_class_type:
3411 // We had a "class foo", see if we ended up with a "struct foo {
3412 // ... };"
3413 try_resolving_type = (tag == DW_TAG_structure_type);
3414 break;
3415 case DW_TAG_structure_type:
3416 // We had a "struct foo", see if we ended up with a "class foo {
3417 // ... };"
3418 try_resolving_type = (tag == DW_TAG_class_type);
3419 break;
3420 default:
3421 // Tags don't match, don't event try to resolve
3422 // using this type whose name matches....
3423 break;
3424 }
3425 }
3426
3427 if (try_resolving_type) {
3428 DWARFDeclContext type_dwarf_decl_ctx;
3429 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3430
3431 if (log) {
3432 GetObjectFile()->GetModule()->LogMessage(
3433 log, "SymbolFileDWARF::"
3434 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3435 "qualified-name='%s') trying die=0x%8.8x (%s)",
3436 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3437 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3438 type_dwarf_decl_ctx.GetQualifiedName());
3439 }
3440
3441 // Make sure the decl contexts match all the way up
3442 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3443 Type *resolved_type = ResolveType(type_die, false);
3444 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3445 type_sp = resolved_type->shared_from_this();
3446 break;
3447 }
3448 }
3449 } else {
3450 if (log) {
3451 std::string qualified_name;
3452 type_die.GetQualifiedName(qualified_name);
3453 GetObjectFile()->GetModule()->LogMessage(
3454 log, "SymbolFileDWARF::"
3455 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3456 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3457 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3458 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3459 qualified_name.c_str());
3460 }
3461 }
3462 } else {
3463 if (m_using_apple_tables) {
3464 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3465 "the DWARF debug information has been modified (.apple_types "
3466 "accelerator table had bad die 0x%8.8x for '%s')\n",
3467 die_ref.die_offset, type_name.GetCString());
3468 }
3469 }
3470 }
3471 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003472 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003473 }
3474 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003475}
3476
Kate Stoneb9c1b512016-09-06 20:57:50 +00003477TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3478 bool *type_is_new_ptr) {
3479 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003480
Kate Stoneb9c1b512016-09-06 20:57:50 +00003481 if (die) {
3482 TypeSystem *type_system =
3483 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003484
Kate Stoneb9c1b512016-09-06 20:57:50 +00003485 if (type_system) {
3486 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3487 if (dwarf_ast) {
3488 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3489 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3490 if (type_sp) {
3491 TypeList *type_list = GetTypeList();
3492 if (type_list)
3493 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003494
Kate Stoneb9c1b512016-09-06 20:57:50 +00003495 if (die.Tag() == DW_TAG_subprogram) {
3496 DIERef die_ref = die.GetDIERef();
3497 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3498 .GetScopeQualifiedName()
3499 .AsCString(""));
3500 if (scope_qualified_name.size()) {
3501 NameToOffsetMap::iterator iter =
3502 m_function_scope_qualified_name_map.find(
3503 scope_qualified_name);
3504 if (iter != m_function_scope_qualified_name_map.end())
3505 (*iter).second->insert(die_ref);
3506 else {
3507 DIERefSetSP new_set(new std::set<DIERef>);
3508 new_set->insert(die_ref);
3509 m_function_scope_qualified_name_map.emplace(
3510 std::make_pair(scope_qualified_name, new_set));
3511 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003512 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003513 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003515 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003517 }
3518
3519 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003520}
3521
Kate Stoneb9c1b512016-09-06 20:57:50 +00003522size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3523 const DWARFDIE &orig_die,
3524 bool parse_siblings, bool parse_children) {
3525 size_t types_added = 0;
3526 DWARFDIE die = orig_die;
3527 while (die) {
3528 bool type_is_new = false;
3529 if (ParseType(sc, die, &type_is_new).get()) {
3530 if (type_is_new)
3531 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003533
3534 if (parse_children && die.HasChildren()) {
3535 if (die.Tag() == DW_TAG_subprogram) {
3536 SymbolContext child_sc(sc);
3537 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3538 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3539 } else
3540 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3541 }
3542
3543 if (parse_siblings)
3544 die = die.GetSibling();
3545 else
3546 die.Clear();
3547 }
3548 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003549}
3550
Kate Stoneb9c1b512016-09-06 20:57:50 +00003551size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3552 assert(sc.comp_unit && sc.function);
3553 size_t functions_added = 0;
3554 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3555 if (dwarf_cu) {
3556 const dw_offset_t function_die_offset = sc.function->GetID();
3557 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3558 if (function_die) {
3559 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3560 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003561 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003563
Kate Stoneb9c1b512016-09-06 20:57:50 +00003564 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003565}
3566
Kate Stoneb9c1b512016-09-06 20:57:50 +00003567size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3568 // At least a compile unit must be valid
3569 assert(sc.comp_unit);
3570 size_t types_added = 0;
3571 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3572 if (dwarf_cu) {
3573 if (sc.function) {
3574 dw_offset_t function_die_offset = sc.function->GetID();
3575 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3576 if (func_die && func_die.HasChildren()) {
3577 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3578 }
3579 } else {
3580 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3581 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3582 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3583 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003585 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003586
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003588}
3589
Kate Stoneb9c1b512016-09-06 20:57:50 +00003590size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3591 if (sc.comp_unit != NULL) {
3592 DWARFDebugInfo *info = DebugInfo();
3593 if (info == NULL)
3594 return 0;
3595
3596 if (sc.function) {
3597 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3598
3599 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3600 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3601 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3602 const size_t num_variables = ParseVariables(
3603 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3604
3605 // Let all blocks know they have parse all their variables
3606 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3607 return num_variables;
3608 }
3609 } else if (sc.comp_unit) {
3610 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3611
3612 if (dwarf_cu == NULL)
3613 return 0;
3614
3615 uint32_t vars_added = 0;
3616 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3617
3618 if (variables.get() == NULL) {
3619 variables.reset(new VariableList());
3620 sc.comp_unit->SetVariableList(variables);
3621
3622 DIEArray die_offsets;
3623 if (m_using_apple_tables) {
3624 if (m_apple_names_ap.get()) {
3625 DWARFMappedHash::DIEInfoArray hash_data_array;
3626 if (m_apple_names_ap->AppendAllDIEsInRange(
3627 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3628 hash_data_array)) {
3629 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003630 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003631 }
3632 } else {
3633 // Index if we already haven't to make sure the compile units
3634 // get indexed and make their global DIE index list
3635 if (!m_indexed)
3636 Index();
3637
3638 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3639 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003641
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642 const size_t num_matches = die_offsets.size();
3643 if (num_matches) {
3644 for (size_t i = 0; i < num_matches; ++i) {
3645 const DIERef &die_ref = die_offsets[i];
3646 DWARFDIE die = GetDIE(die_ref);
3647 if (die) {
3648 VariableSP var_sp(
3649 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3650 if (var_sp) {
3651 variables->AddVariableIfUnique(var_sp);
3652 ++vars_added;
3653 }
3654 } else {
3655 if (m_using_apple_tables) {
3656 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3657 "the DWARF debug information has been modified "
3658 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3659 die_ref.die_offset);
3660 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003663 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003664 }
3665 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003666 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003667 }
3668 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003669}
3670
Kate Stoneb9c1b512016-09-06 20:57:50 +00003671VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3672 const DWARFDIE &die,
3673 const lldb::addr_t func_low_pc) {
3674 if (die.GetDWARF() != this)
3675 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003676
Kate Stoneb9c1b512016-09-06 20:57:50 +00003677 VariableSP var_sp;
3678 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003679 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003680
Kate Stoneb9c1b512016-09-06 20:57:50 +00003681 var_sp = GetDIEToVariable()[die.GetDIE()];
3682 if (var_sp)
3683 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003684
Kate Stoneb9c1b512016-09-06 20:57:50 +00003685 const dw_tag_t tag = die.Tag();
3686 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003687
Kate Stoneb9c1b512016-09-06 20:57:50 +00003688 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3689 (tag == DW_TAG_formal_parameter && sc.function)) {
3690 DWARFAttributes attributes;
3691 const size_t num_attributes = die.GetAttributes(attributes);
3692 DWARFDIE spec_die;
3693 if (num_attributes > 0) {
3694 const char *name = NULL;
3695 const char *mangled = NULL;
3696 Declaration decl;
3697 uint32_t i;
3698 DWARFFormValue type_die_form;
3699 DWARFExpression location(die.GetCU());
3700 bool is_external = false;
3701 bool is_artificial = false;
3702 bool location_is_const_value_data = false;
3703 bool has_explicit_location = false;
3704 DWARFFormValue const_value;
3705 Variable::RangeList scope_ranges;
3706 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003707
Kate Stoneb9c1b512016-09-06 20:57:50 +00003708 for (i = 0; i < num_attributes; ++i) {
3709 dw_attr_t attr = attributes.AttributeAtIndex(i);
3710 DWARFFormValue form_value;
3711
3712 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3713 switch (attr) {
3714 case DW_AT_decl_file:
3715 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3716 form_value.Unsigned()));
3717 break;
3718 case DW_AT_decl_line:
3719 decl.SetLine(form_value.Unsigned());
3720 break;
3721 case DW_AT_decl_column:
3722 decl.SetColumn(form_value.Unsigned());
3723 break;
3724 case DW_AT_name:
3725 name = form_value.AsCString();
3726 break;
3727 case DW_AT_linkage_name:
3728 case DW_AT_MIPS_linkage_name:
3729 mangled = form_value.AsCString();
3730 break;
3731 case DW_AT_type:
3732 type_die_form = form_value;
3733 break;
3734 case DW_AT_external:
3735 is_external = form_value.Boolean();
3736 break;
3737 case DW_AT_const_value:
3738 // If we have already found a DW_AT_location attribute, ignore this
3739 // attribute.
3740 if (!has_explicit_location) {
3741 location_is_const_value_data = true;
3742 // The constant value will be either a block, a data value or a
3743 // string.
3744 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3745 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3746 // Retrieve the value as a block expression.
3747 uint32_t block_offset =
3748 form_value.BlockData() - debug_info_data.GetDataStart();
3749 uint32_t block_length = form_value.Unsigned();
3750 location.CopyOpcodeData(module, debug_info_data, block_offset,
3751 block_length);
3752 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3753 // Retrieve the value as a data expression.
3754 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3755 DWARFFormValue::GetFixedFormSizesForAddressSize(
3756 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3757 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3758 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3759 uint32_t data_length =
3760 fixed_form_sizes.GetSize(form_value.Form());
3761 if (data_length == 0) {
3762 const uint8_t *data_pointer = form_value.BlockData();
3763 if (data_pointer) {
3764 form_value.Unsigned();
3765 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3766 // we need to get the byte size of the type later after we
3767 // create the variable
3768 const_value = form_value;
3769 }
3770 } else
3771 location.CopyOpcodeData(module, debug_info_data, data_offset,
3772 data_length);
3773 } else {
3774 // Retrieve the value as a string expression.
3775 if (form_value.Form() == DW_FORM_strp) {
3776 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3777 DWARFFormValue::GetFixedFormSizesForAddressSize(
3778 attributes.CompileUnitAtIndex(i)
3779 ->GetAddressByteSize(),
3780 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3781 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3782 uint32_t data_length =
3783 fixed_form_sizes.GetSize(form_value.Form());
3784 location.CopyOpcodeData(module, debug_info_data, data_offset,
3785 data_length);
3786 } else {
3787 const char *str = form_value.AsCString();
3788 uint32_t string_offset =
3789 str - (const char *)debug_info_data.GetDataStart();
3790 uint32_t string_length = strlen(str) + 1;
3791 location.CopyOpcodeData(module, debug_info_data,
3792 string_offset, string_length);
3793 }
3794 }
3795 }
3796 break;
3797 case DW_AT_location: {
3798 location_is_const_value_data = false;
3799 has_explicit_location = true;
3800 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3801 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3802
3803 uint32_t block_offset =
3804 form_value.BlockData() - debug_info_data.GetDataStart();
3805 uint32_t block_length = form_value.Unsigned();
3806 location.CopyOpcodeData(module, get_debug_info_data(),
3807 block_offset, block_length);
3808 } else {
3809 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3810 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3811
3812 size_t loc_list_length = DWARFExpression::LocationListSize(
3813 die.GetCU(), debug_loc_data, debug_loc_offset);
3814 if (loc_list_length > 0) {
3815 location.CopyOpcodeData(module, debug_loc_data,
3816 debug_loc_offset, loc_list_length);
3817 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3818 location.SetLocationListSlide(
3819 func_low_pc -
3820 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3821 }
3822 }
3823 } break;
3824 case DW_AT_specification:
3825 spec_die = GetDIE(DIERef(form_value));
3826 break;
3827 case DW_AT_start_scope: {
3828 if (form_value.Form() == DW_FORM_sec_offset) {
3829 DWARFRangeList dwarf_scope_ranges;
3830 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003831 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3832 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003833 dwarf_scope_ranges);
3834
3835 // All DW_AT_start_scope are relative to the base address of the
3836 // compile unit. We add the compile unit base address to make
3837 // sure all the addresses are properly fixed up.
3838 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3839 i < count; ++i) {
3840 const DWARFRangeList::Entry &range =
3841 dwarf_scope_ranges.GetEntryRef(i);
3842 scope_ranges.Append(range.GetRangeBase() +
3843 die.GetCU()->GetBaseAddress(),
3844 range.GetByteSize());
3845 }
3846 } else {
3847 // TODO: Handle the case when DW_AT_start_scope have form
3848 // constant. The
3849 // dwarf spec is a bit ambiguous about what is the expected
3850 // behavior in
3851 // case the enclosing block have a non coninious address range and
3852 // the
3853 // DW_AT_start_scope entry have a form constant.
3854 GetObjectFile()->GetModule()->ReportWarning(
3855 "0x%8.8" PRIx64
3856 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3857 die.GetID(), form_value.Form());
3858 }
3859
3860 scope_ranges.Sort();
3861 scope_ranges.CombineConsecutiveRanges();
3862 } break;
3863 case DW_AT_artificial:
3864 is_artificial = form_value.Boolean();
3865 break;
3866 case DW_AT_accessibility:
3867 break; // accessibility =
3868 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3869 case DW_AT_declaration:
3870 case DW_AT_description:
3871 case DW_AT_endianity:
3872 case DW_AT_segment:
3873 case DW_AT_visibility:
3874 default:
3875 case DW_AT_abstract_origin:
3876 case DW_AT_sibling:
3877 break;
3878 }
3879 }
3880 }
3881
3882 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3883 const dw_tag_t parent_tag = die.GetParent().Tag();
3884 bool is_static_member =
3885 parent_tag == DW_TAG_compile_unit &&
3886 (parent_context_die.Tag() == DW_TAG_class_type ||
3887 parent_context_die.Tag() == DW_TAG_structure_type);
3888
3889 ValueType scope = eValueTypeInvalid;
3890
3891 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3892 SymbolContextScope *symbol_context_scope = NULL;
3893
Sam McCall30621392016-11-22 11:40:25 +00003894 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003895 if (!mangled) {
3896 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3897 // DW_AT_MIPS_linkage_name) to
3898 // generate fully qualified names of global variables with commands like
3899 // "frame var j".
3900 // For example, if j were an int variable holding a value 4 and declared
3901 // in a namespace
3902 // B which in turn is contained in a namespace A, the command "frame var
3903 // j" returns
3904 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we
3905 // should be able
3906 // to generate a fully qualified name from the declaration context.
3907 if (parent_tag == DW_TAG_compile_unit &&
3908 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3909 DWARFDeclContext decl_ctx;
3910
3911 die.GetDWARFDeclContext(decl_ctx);
3912 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3913 }
3914 }
3915
Kate Stoneb9c1b512016-09-06 20:57:50 +00003916 if (tag == DW_TAG_formal_parameter)
3917 scope = eValueTypeVariableArgument;
3918 else {
Sam McCall30621392016-11-22 11:40:25 +00003919 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3920 // or static variable, so we have to do a little digging:
3921 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3922 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3923 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3924 // Clang likes to combine small global variables into the same symbol
3925 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3926 // so we need to look through the whole expression.
3927 bool is_static_lifetime =
3928 has_explicit_mangled ||
3929 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003930 // Check if the location has a DW_OP_addr with any address value...
3931 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3932 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003933 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003934 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3935 if (op_error) {
3936 StreamString strm;
3937 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3938 NULL);
3939 GetObjectFile()->GetModule()->ReportError(
3940 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003941 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003942 }
Sam McCall30621392016-11-22 11:40:25 +00003943 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3944 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003945 }
3946 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3947
Sam McCall30621392016-11-22 11:40:25 +00003948 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003949 if (is_external)
3950 scope = eValueTypeVariableGlobal;
3951 else
3952 scope = eValueTypeVariableStatic;
3953
3954 if (debug_map_symfile) {
3955 // When leaving the DWARF in the .o files on darwin,
3956 // when we have a global variable that wasn't initialized,
3957 // the .o file might not have allocated a virtual
3958 // address for the global variable. In this case it will
3959 // have created a symbol for the global variable
3960 // that is undefined/data and external and the value will
3961 // be the byte size of the variable. When we do the
3962 // address map in SymbolFileDWARFDebugMap we rely on
3963 // having an address, we need to do some magic here
3964 // so we can get the correct address for our global
3965 // variable. The address for all of these entries
3966 // will be zero, and there will be an undefined symbol
3967 // in this object file, and the executable will have
3968 // a matching symbol with a good address. So here we
3969 // dig up the correct address and replace it in the
3970 // location for the variable, and set the variable's
3971 // symbol context scope to be that of the main executable
3972 // so the file address will resolve correctly.
3973 bool linked_oso_file_addr = false;
3974 if (is_external && location_DW_OP_addr == 0) {
3975 // we have a possible uninitialized extern global
3976 ConstString const_name(mangled ? mangled : name);
3977 ObjectFile *debug_map_objfile =
3978 debug_map_symfile->GetObjectFile();
3979 if (debug_map_objfile) {
3980 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3981 if (debug_map_symtab) {
3982 Symbol *exe_symbol =
3983 debug_map_symtab->FindFirstSymbolWithNameAndType(
3984 const_name, eSymbolTypeData, Symtab::eDebugYes,
3985 Symtab::eVisibilityExtern);
3986 if (exe_symbol) {
3987 if (exe_symbol->ValueIsAddress()) {
3988 const addr_t exe_file_addr =
3989 exe_symbol->GetAddressRef().GetFileAddress();
3990 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3991 if (location.Update_DW_OP_addr(exe_file_addr)) {
3992 linked_oso_file_addr = true;
3993 symbol_context_scope = exe_symbol;
3994 }
3995 }
3996 }
3997 }
3998 }
3999 }
4000 }
4001
4002 if (!linked_oso_file_addr) {
4003 // The DW_OP_addr is not zero, but it contains a .o file address
4004 // which
4005 // needs to be linked up correctly.
4006 const lldb::addr_t exe_file_addr =
4007 debug_map_symfile->LinkOSOFileAddress(this,
4008 location_DW_OP_addr);
4009 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
4010 // Update the file address for this variable
4011 location.Update_DW_OP_addr(exe_file_addr);
4012 } else {
4013 // Variable didn't make it into the final executable
4014 return var_sp;
4015 }
4016 }
4017 }
4018 } else {
4019 if (location_is_const_value_data)
4020 scope = eValueTypeVariableStatic;
4021 else {
4022 scope = eValueTypeVariableLocal;
4023 if (debug_map_symfile) {
4024 // We need to check for TLS addresses that we need to fixup
4025 if (location.ContainsThreadLocalStorage()) {
4026 location.LinkThreadLocalStorage(
4027 debug_map_symfile->GetObjectFile()->GetModule(),
4028 [this, debug_map_symfile](
4029 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4030 return debug_map_symfile->LinkOSOFileAddress(
4031 this, unlinked_file_addr);
4032 });
4033 scope = eValueTypeVariableThreadLocal;
4034 }
4035 }
4036 }
4037 }
4038 }
4039
4040 if (symbol_context_scope == NULL) {
4041 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004042 case DW_TAG_subprogram:
4043 case DW_TAG_inlined_subroutine:
4044 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004045 if (sc.function) {
4046 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4047 sc_parent_die.GetID());
4048 if (symbol_context_scope == NULL)
4049 symbol_context_scope = sc.function;
4050 }
4051 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004052
Kate Stoneb9c1b512016-09-06 20:57:50 +00004053 default:
4054 symbol_context_scope = sc.comp_unit;
4055 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004056 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004057 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004058
Kate Stoneb9c1b512016-09-06 20:57:50 +00004059 if (symbol_context_scope) {
4060 SymbolFileTypeSP type_sp(
4061 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4062
4063 if (const_value.Form() && type_sp && type_sp->GetType())
4064 location.CopyOpcodeData(const_value.Unsigned(),
4065 type_sp->GetType()->GetByteSize(),
4066 die.GetCU()->GetAddressByteSize());
4067
4068 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4069 symbol_context_scope, scope_ranges, &decl,
4070 location, is_external, is_artificial,
4071 is_static_member));
4072
4073 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4074 } else {
4075 // Not ready to parse this variable yet. It might be a global
4076 // or static variable that is in a function scope and the function
4077 // in the symbol context wasn't filled in yet
4078 return var_sp;
4079 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004081 // Cache var_sp even if NULL (the variable was just a specification or
4082 // was missing vital information to be able to be displayed in the debugger
4083 // (missing location due to optimization, etc)) so we don't re-parse
4084 // this DIE over and over later...
4085 GetDIEToVariable()[die.GetDIE()] = var_sp;
4086 if (spec_die)
4087 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4088 }
4089 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004090}
4091
Kate Stoneb9c1b512016-09-06 20:57:50 +00004092DWARFDIE
4093SymbolFileDWARF::FindBlockContainingSpecification(
4094 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4095 // Give the concrete function die specified by "func_die_offset", find the
4096 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4097 // to "spec_block_die_offset"
4098 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4099 spec_block_die_offset);
4100}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004101
Kate Stoneb9c1b512016-09-06 20:57:50 +00004102DWARFDIE
4103SymbolFileDWARF::FindBlockContainingSpecification(
4104 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4105 if (die) {
4106 switch (die.Tag()) {
4107 case DW_TAG_subprogram:
4108 case DW_TAG_inlined_subroutine:
4109 case DW_TAG_lexical_block: {
4110 if (die.GetAttributeValueAsReference(
4111 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4112 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004113
Kate Stoneb9c1b512016-09-06 20:57:50 +00004114 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4115 DW_INVALID_OFFSET) ==
4116 spec_block_die_offset)
4117 return die;
4118 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004120
4121 // Give the concrete function die specified by "func_die_offset", find the
4122 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4123 // to "spec_block_die_offset"
4124 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4125 child_die = child_die.GetSibling()) {
4126 DWARFDIE result_die =
4127 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4128 if (result_die)
4129 return result_die;
4130 }
4131 }
4132
4133 return DWARFDIE();
4134}
4135
4136size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4137 const DWARFDIE &orig_die,
4138 const lldb::addr_t func_low_pc,
4139 bool parse_siblings, bool parse_children,
4140 VariableList *cc_variable_list) {
4141 if (!orig_die)
4142 return 0;
4143
4144 VariableListSP variable_list_sp;
4145
4146 size_t vars_added = 0;
4147 DWARFDIE die = orig_die;
4148 while (die) {
4149 dw_tag_t tag = die.Tag();
4150
4151 // Check to see if we have already parsed this variable or constant?
4152 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4153 if (var_sp) {
4154 if (cc_variable_list)
4155 cc_variable_list->AddVariableIfUnique(var_sp);
4156 } else {
4157 // We haven't already parsed it, lets do that now.
4158 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4159 (tag == DW_TAG_formal_parameter && sc.function)) {
4160 if (variable_list_sp.get() == NULL) {
4161 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4162 dw_tag_t parent_tag = sc_parent_die.Tag();
4163 switch (parent_tag) {
4164 case DW_TAG_compile_unit:
4165 if (sc.comp_unit != NULL) {
4166 variable_list_sp = sc.comp_unit->GetVariableList(false);
4167 if (variable_list_sp.get() == NULL) {
4168 variable_list_sp.reset(new VariableList());
4169 sc.comp_unit->SetVariableList(variable_list_sp);
4170 }
4171 } else {
4172 GetObjectFile()->GetModule()->ReportError(
4173 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4174 "symbol context for 0x%8.8" PRIx64
4175 " %s.\n",
4176 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4177 orig_die.GetID(), orig_die.GetTagAsCString());
4178 }
4179 break;
4180
4181 case DW_TAG_subprogram:
4182 case DW_TAG_inlined_subroutine:
4183 case DW_TAG_lexical_block:
4184 if (sc.function != NULL) {
4185 // Check to see if we already have parsed the variables for the
4186 // given scope
4187
4188 Block *block = sc.function->GetBlock(true).FindBlockByID(
4189 sc_parent_die.GetID());
4190 if (block == NULL) {
4191 // This must be a specification or abstract origin with
4192 // a concrete block counterpart in the current function. We need
4193 // to find the concrete block so we can correctly add the
4194 // variable to it
4195 const DWARFDIE concrete_block_die =
4196 FindBlockContainingSpecification(
4197 DIERef(sc.function->GetID(), this),
4198 sc_parent_die.GetOffset());
4199 if (concrete_block_die)
4200 block = sc.function->GetBlock(true).FindBlockByID(
4201 concrete_block_die.GetID());
4202 }
4203
4204 if (block != NULL) {
4205 const bool can_create = false;
4206 variable_list_sp = block->GetBlockVariableList(can_create);
4207 if (variable_list_sp.get() == NULL) {
4208 variable_list_sp.reset(new VariableList());
4209 block->SetVariableList(variable_list_sp);
4210 }
4211 }
4212 }
4213 break;
4214
4215 default:
4216 GetObjectFile()->GetModule()->ReportError(
4217 "didn't find appropriate parent DIE for variable list for "
4218 "0x%8.8" PRIx64 " %s.\n",
4219 orig_die.GetID(), orig_die.GetTagAsCString());
4220 break;
4221 }
4222 }
4223
4224 if (variable_list_sp) {
4225 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4226 if (var_sp) {
4227 variable_list_sp->AddVariableIfUnique(var_sp);
4228 if (cc_variable_list)
4229 cc_variable_list->AddVariableIfUnique(var_sp);
4230 ++vars_added;
4231 }
4232 }
4233 }
4234 }
4235
4236 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4237
4238 if (!skip_children && parse_children && die.HasChildren()) {
4239 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4240 true, cc_variable_list);
4241 }
4242
4243 if (parse_siblings)
4244 die = die.GetSibling();
4245 else
4246 die.Clear();
4247 }
4248 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004249}
4250
4251//------------------------------------------------------------------
4252// PluginInterface protocol
4253//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004254ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4255
4256uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4257
4258void SymbolFileDWARF::DumpIndexes() {
4259 StreamFile s(stdout, false);
4260
4261 s.Printf(
4262 "DWARF index for (%s) '%s':",
4263 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4264 GetObjectFile()->GetFileSpec().GetPath().c_str());
4265 s.Printf("\nFunction basenames:\n");
4266 m_function_basename_index.Dump(&s);
4267 s.Printf("\nFunction fullnames:\n");
4268 m_function_fullname_index.Dump(&s);
4269 s.Printf("\nFunction methods:\n");
4270 m_function_method_index.Dump(&s);
4271 s.Printf("\nFunction selectors:\n");
4272 m_function_selector_index.Dump(&s);
4273 s.Printf("\nObjective C class selectors:\n");
4274 m_objc_class_selectors_index.Dump(&s);
4275 s.Printf("\nGlobals and statics:\n");
4276 m_global_index.Dump(&s);
4277 s.Printf("\nTypes:\n");
4278 m_type_index.Dump(&s);
4279 s.Printf("\nNamespaces:\n");
4280 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004281}
4282
Kate Stoneb9c1b512016-09-06 20:57:50 +00004283SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4284 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4285 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4286 if (module_sp) {
4287 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4288 if (sym_vendor)
4289 m_debug_map_symfile =
4290 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004291 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004292 }
4293 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004294}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004295
4296DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004297SymbolFileDWARF::GetLocationListFormat() const {
4298 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004299}