blob: 5c44ee03b02c38b4175a3f977b1a904984646b9e [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"
14
Robert Flackeb83fab2015-05-15 18:59:59 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000017#include "lldb/Core/ModuleList.h"
18#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/RegularExpression.h"
21#include "lldb/Core/Scalar.h"
22#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000023#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000024#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/Value.h"
27
Sean Callanan4dbb2712015-09-25 20:35:58 +000028#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000029
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000030#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000031#include "lldb/Host/Host.h"
32
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000033#include "lldb/Interpreter/OptionValueFileSpecList.h"
34#include "lldb/Interpreter/OptionValueProperties.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000037#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000038#include "lldb/Symbol/ClangUtil.h"
39#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000040#include "lldb/Symbol/CompilerDecl.h"
41#include "lldb/Symbol/CompilerDeclContext.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000042#include "lldb/Symbol/DebugMacros.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000043#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Symbol/ObjectFile.h"
45#include "lldb/Symbol/SymbolVendor.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000046#include "lldb/Symbol/TypeMap.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000047#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Symbol/VariableList.h"
49
Jim Inghamaa816b82015-09-02 01:59:14 +000050#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
51#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000052
Jim Ingham0e0984e2015-09-02 01:06:46 +000053#include "lldb/Target/Language.h"
54
Tamas Berghammer2ff88702015-10-23 10:34:49 +000055#include "lldb/Utility/TaskPool.h"
56
Greg Clayton261ac3f2015-08-28 01:01:03 +000057#include "DWARFASTParser.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000058#include "DWARFASTParserClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "DWARFCompileUnit.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000060#include "DWARFDIECollection.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDebugAbbrev.h"
62#include "DWARFDebugAranges.h"
63#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000065#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "DWARFDebugPubnames.h"
67#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000068#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "DWARFFormValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000071#include "SymbolFileDWARFDebugMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000072#include "SymbolFileDWARFDwo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073
74#include <map>
75
Matthew Gardinere81df3b2014-08-26 06:57:23 +000076#include <ctype.h>
77#include <string.h>
78
Greg Clayton62742b12010-11-11 01:09:45 +000079//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000080
81#ifdef ENABLE_DEBUG_PRINTF
82#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000083#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000084#else
85#define DEBUG_PRINTF(fmt, ...)
86#endif
87
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088using namespace lldb;
89using namespace lldb_private;
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091// static inline bool
92// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton219cf312012-03-30 00:51:13 +000093//{
94// switch (tag)
95// {
96// default:
97// break;
98// case DW_TAG_subprogram:
99// case DW_TAG_inlined_subroutine:
100// case DW_TAG_class_type:
101// case DW_TAG_structure_type:
102// case DW_TAG_union_type:
103// return true;
104// }
105// return false;
106//}
107//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000108
109namespace {
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111PropertyDefinition g_properties[] = {
112 {"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
113 nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic "
114 "links will be resolved at DWARF parse time."},
115 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117enum { ePropertySymLinkPaths };
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119class PluginProperties : public Properties {
120public:
121 static ConstString GetSettingName() {
122 return SymbolFileDWARF::GetPluginNameStatic();
123 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 PluginProperties() {
126 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
127 m_collection_sp->Initialize(g_properties);
128 }
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 FileSpecList &GetSymLinkPaths() {
131 OptionValueFileSpecList *option_value =
132 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
133 nullptr, true, ePropertySymLinkPaths);
134 assert(option_value);
135 return option_value->GetCurrentValue();
136 }
137};
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
142 static const auto g_settings_sp(std::make_shared<PluginProperties>());
143 return g_settings_sp;
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146} // anonymous namespace end
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148static const char *removeHostnameFromPathname(const char *path_from_dwarf) {
149 if (!path_from_dwarf || !path_from_dwarf[0]) {
150 return path_from_dwarf;
151 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 const char *colon_pos = strchr(path_from_dwarf, ':');
154 if (nullptr == colon_pos) {
155 return path_from_dwarf;
156 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 const char *slash_pos = strchr(path_from_dwarf, '/');
159 if (slash_pos && (slash_pos < colon_pos)) {
160 return path_from_dwarf;
161 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 // check whether we have a windows path, and so the first character
164 // is a drive-letter not a hostname.
165 if (colon_pos == path_from_dwarf + 1 && isalpha(*path_from_dwarf) &&
166 strlen(path_from_dwarf) > 2 && '\\' == path_from_dwarf[2]) {
167 return path_from_dwarf;
168 }
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 return colon_pos + 1;
171}
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173static const char *resolveCompDir(const char *path_from_dwarf) {
174 if (!path_from_dwarf)
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000175 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176
177 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
178 // Remove the host part if present.
179 const char *local_path = removeHostnameFromPathname(path_from_dwarf);
180 if (!local_path)
181 return nullptr;
182
183 bool is_symlink = false;
184 FileSpec local_path_spec(local_path, false);
185 const auto &file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
186 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
187 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i),
188 local_path_spec, true);
189
190 if (!is_symlink)
191 return local_path;
192
193 if (!local_path_spec.IsSymbolicLink())
194 return local_path;
195
196 FileSpec resolved_local_path_spec;
197 const auto error =
198 FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
199 if (error.Success())
200 return resolved_local_path_spec.GetCString();
201
202 return nullptr;
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205void SymbolFileDWARF::Initialize() {
206 LogChannelDWARF::Initialize();
207 PluginManager::RegisterPlugin(GetPluginNameStatic(),
208 GetPluginDescriptionStatic(), CreateInstance,
209 DebuggerInitialize);
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000210}
211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
213 if (!PluginManager::GetSettingForSymbolFilePlugin(
214 debugger, PluginProperties::GetSettingName())) {
215 const bool is_global_setting = true;
216 PluginManager::CreateSettingForSymbolFilePlugin(
217 debugger, GetGlobalPluginProperties()->GetValueProperties(),
218 ConstString("Properties for the dwarf symbol-file plug-in."),
219 is_global_setting);
220 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223void SymbolFileDWARF::Terminate() {
224 PluginManager::UnregisterPlugin(CreateInstance);
225 LogChannelDWARF::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226}
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
229 static ConstString g_name("dwarf");
230 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
234 return "DWARF and DWARF3 debug symbol file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
238 return new SymbolFileDWARF(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241TypeList *SymbolFileDWARF::GetTypeList() {
242 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
243 if (debug_map_symfile)
244 return debug_map_symfile->GetTypeList();
245 else
246 return m_obj_file->GetModule()->GetTypeList();
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000247}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
249 dw_offset_t max_die_offset, uint32_t type_mask,
250 TypeSet &type_set) {
251 if (die) {
252 const dw_offset_t die_offset = die.GetOffset();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 if (die_offset >= max_die_offset)
255 return;
Greg Claytonf02500c2013-06-18 22:51:05 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 if (die_offset >= min_die_offset) {
258 const dw_tag_t tag = die.Tag();
259
260 bool add_type = false;
261
262 switch (tag) {
263 case DW_TAG_array_type:
264 add_type = (type_mask & eTypeClassArray) != 0;
265 break;
266 case DW_TAG_unspecified_type:
267 case DW_TAG_base_type:
268 add_type = (type_mask & eTypeClassBuiltin) != 0;
269 break;
270 case DW_TAG_class_type:
271 add_type = (type_mask & eTypeClassClass) != 0;
272 break;
273 case DW_TAG_structure_type:
274 add_type = (type_mask & eTypeClassStruct) != 0;
275 break;
276 case DW_TAG_union_type:
277 add_type = (type_mask & eTypeClassUnion) != 0;
278 break;
279 case DW_TAG_enumeration_type:
280 add_type = (type_mask & eTypeClassEnumeration) != 0;
281 break;
282 case DW_TAG_subroutine_type:
283 case DW_TAG_subprogram:
284 case DW_TAG_inlined_subroutine:
285 add_type = (type_mask & eTypeClassFunction) != 0;
286 break;
287 case DW_TAG_pointer_type:
288 add_type = (type_mask & eTypeClassPointer) != 0;
289 break;
290 case DW_TAG_rvalue_reference_type:
291 case DW_TAG_reference_type:
292 add_type = (type_mask & eTypeClassReference) != 0;
293 break;
294 case DW_TAG_typedef:
295 add_type = (type_mask & eTypeClassTypedef) != 0;
296 break;
297 case DW_TAG_ptr_to_member_type:
298 add_type = (type_mask & eTypeClassMemberPointer) != 0;
299 break;
300 }
301
302 if (add_type) {
303 const bool assert_not_being_parsed = true;
304 Type *type = ResolveTypeUID(die, assert_not_being_parsed);
305 if (type) {
306 if (type_set.find(type) == type_set.end())
307 type_set.insert(type);
Greg Clayton6071e6f2015-08-26 22:57:51 +0000308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311
312 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
313 child_die = child_die.GetSibling()) {
314 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
315 }
316 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
320 uint32_t type_mask, TypeList &type_list)
Greg Claytonf02500c2013-06-18 22:51:05 +0000321
322{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 TypeSet type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 CompileUnit *comp_unit = NULL;
326 DWARFCompileUnit *dwarf_cu = NULL;
327 if (sc_scope)
328 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
Greg Clayton6071e6f2015-08-26 22:57:51 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 if (comp_unit) {
331 dwarf_cu = GetDWARFCompileUnit(comp_unit);
332 if (dwarf_cu == 0)
333 return 0;
334 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
335 dwarf_cu->GetNextCompileUnitOffset(), type_mask, type_set);
336 } else {
337 DWARFDebugInfo *info = DebugInfo();
338 if (info) {
339 const size_t num_cus = info->GetNumCompileUnits();
340 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
341 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
342 if (dwarf_cu) {
343 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 }
348
349 std::set<CompilerType> compiler_type_set;
350 size_t num_types_added = 0;
351 for (Type *type : type_set) {
352 CompilerType compiler_type = type->GetForwardCompilerType();
353 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
354 compiler_type_set.insert(compiler_type);
355 type_list.Insert(type->shared_from_this());
356 ++num_types_added;
357 }
358 }
359 return num_types_added;
Greg Claytonf02500c2013-06-18 22:51:05 +0000360}
361
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362//----------------------------------------------------------------------
363// Gets the first parent that is a lexical block, function or inlined
364// subroutine, or compile unit.
365//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000366DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
368 DWARFDIE die;
369 for (die = child_die.GetParent(); die; die = die.GetParent()) {
370 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 switch (tag) {
373 case DW_TAG_compile_unit:
374 case DW_TAG_subprogram:
375 case DW_TAG_inlined_subroutine:
376 case DW_TAG_lexical_block:
377 return die;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 }
380 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381}
382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
384 : SymbolFile(objfile), UserID(0), // Used by SymbolFileDWARFDebugMap to when
385 // this class parses .o files to contain
386 // the .o file index/ID
387 m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
388 m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
389 m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
390 m_data_debug_ranges(), m_data_debug_str(), m_data_apple_names(),
391 m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
392 m_line(), m_apple_names_ap(), m_apple_types_ap(), m_apple_namespaces_ap(),
393 m_apple_objc_ap(), m_function_basename_index(),
394 m_function_fullname_index(), m_function_method_index(),
395 m_function_selector_index(), m_objc_class_selectors_index(),
396 m_global_index(), m_type_index(), m_namespace_index(), m_indexed(false),
397 m_using_apple_tables(false), m_fetched_external_modules(false),
398 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
399 m_unique_ast_type_map() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401SymbolFileDWARF::~SymbolFileDWARF() {}
402
403static const ConstString &GetDWARFMachOSegmentName() {
404 static ConstString g_dwarf_section_name("__DWARF");
405 return g_dwarf_section_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406}
407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
409 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
410 if (debug_map_symfile)
411 return debug_map_symfile->GetUniqueDWARFASTTypeMap();
412 else
413 return m_unique_ast_type_map;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
417 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
418 TypeSystem *type_system;
419 if (debug_map_symfile) {
420 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
421 } else {
422 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
423 if (type_system)
424 type_system->SetSymbolFile(this);
425 }
426 return type_system;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000427}
428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429void SymbolFileDWARF::InitializeObject() {
430 ModuleSP module_sp(m_obj_file->GetModule());
431 if (module_sp) {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000432 const SectionList *section_list = module_sp->GetSectionList();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 const Section *section =
434 section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
435
436 // Memory map the DWARF mach-o segment so we have everything mmap'ed
437 // to keep our heap memory usage down.
438 if (section)
439 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
440 }
441
442 get_apple_names_data();
443 if (m_data_apple_names.m_data.GetByteSize() > 0) {
444 m_apple_names_ap.reset(new DWARFMappedHash::MemoryTable(
445 m_data_apple_names.m_data, get_debug_str_data(), ".apple_names"));
446 if (m_apple_names_ap->IsValid())
447 m_using_apple_tables = true;
448 else
449 m_apple_names_ap.reset();
450 }
451 get_apple_types_data();
452 if (m_data_apple_types.m_data.GetByteSize() > 0) {
453 m_apple_types_ap.reset(new DWARFMappedHash::MemoryTable(
454 m_data_apple_types.m_data, get_debug_str_data(), ".apple_types"));
455 if (m_apple_types_ap->IsValid())
456 m_using_apple_tables = true;
457 else
458 m_apple_types_ap.reset();
459 }
460
461 get_apple_namespaces_data();
462 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) {
463 m_apple_namespaces_ap.reset(new DWARFMappedHash::MemoryTable(
464 m_data_apple_namespaces.m_data, get_debug_str_data(),
465 ".apple_namespaces"));
466 if (m_apple_namespaces_ap->IsValid())
467 m_using_apple_tables = true;
468 else
469 m_apple_namespaces_ap.reset();
470 }
471
472 get_apple_objc_data();
473 if (m_data_apple_objc.m_data.GetByteSize() > 0) {
474 m_apple_objc_ap.reset(new DWARFMappedHash::MemoryTable(
475 m_data_apple_objc.m_data, get_debug_str_data(), ".apple_objc"));
476 if (m_apple_objc_ap->IsValid())
477 m_using_apple_tables = true;
478 else
479 m_apple_objc_ap.reset();
480 }
481}
482
483bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
484 return version == 2 || version == 3 || version == 4;
485}
486
487uint32_t SymbolFileDWARF::CalculateAbilities() {
488 uint32_t abilities = 0;
489 if (m_obj_file != NULL) {
490 const Section *section = NULL;
491 const SectionList *section_list = m_obj_file->GetSectionList();
492 if (section_list == NULL)
493 return 0;
494
495 uint64_t debug_abbrev_file_size = 0;
496 uint64_t debug_info_file_size = 0;
497 uint64_t debug_line_file_size = 0;
498
499 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
500
501 if (section)
502 section_list = &section->GetChildren();
503
504 section =
505 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
506 if (section != NULL) {
507 debug_info_file_size = section->GetFileSize();
508
509 section =
510 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
511 .get();
512 if (section)
513 debug_abbrev_file_size = section->GetFileSize();
514
515 section =
516 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
517 .get();
518 if (section)
519 debug_line_file_size = section->GetFileSize();
520 } else {
521 const char *symfile_dir_cstr =
522 m_obj_file->GetFileSpec().GetDirectory().GetCString();
523 if (symfile_dir_cstr) {
524 if (strcasestr(symfile_dir_cstr, ".dsym")) {
525 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) {
526 // We have a dSYM file that didn't have a any debug info.
527 // If the string table has a size of 1, then it was made from
528 // an executable with no debug info, or from an executable that
529 // was stripped.
530 section =
531 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
532 .get();
533 if (section && section->GetFileSize() == 1) {
534 m_obj_file->GetModule()->ReportWarning(
535 "empty dSYM file detected, dSYM was created with an "
536 "executable with no debug info.");
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000537 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542
543 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
544 abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
545 LocalVariables | VariableTypes;
546
547 if (debug_line_file_size > 0)
548 abilities |= LineTables;
549 }
550 return abilities;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553const DWARFDataExtractor &
554SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
555 DWARFDataSegment &data_segment) {
556 std::call_once(data_segment.m_flag, &SymbolFileDWARF::LoadSectionData, this,
557 sect_type, std::ref(data_segment.m_data));
558 return data_segment.m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559}
560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
562 DWARFDataExtractor &data) {
563 ModuleSP module_sp(m_obj_file->GetModule());
564 const SectionList *section_list = module_sp->GetSectionList();
565 if (section_list) {
566 SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
567 if (section_sp) {
568 // See if we memory mapped the DWARF segment?
569 if (m_dwarf_data.GetByteSize()) {
570 data.SetData(m_dwarf_data, section_sp->GetOffset(),
571 section_sp->GetFileSize());
572 } else {
573 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
574 data.Clear();
575 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578}
579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
581 return GetCachedSectionData(eSectionTypeDWARFDebugAbbrev,
582 m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585const DWARFDataExtractor &SymbolFileDWARF::get_debug_addr_data() {
586 return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
587}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
590 return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
591 m_data_debug_aranges);
592}
593
594const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
595 return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
596}
597
598const DWARFDataExtractor &SymbolFileDWARF::get_debug_info_data() {
599 return GetCachedSectionData(eSectionTypeDWARFDebugInfo, m_data_debug_info);
600}
601
602const DWARFDataExtractor &SymbolFileDWARF::get_debug_line_data() {
603 return GetCachedSectionData(eSectionTypeDWARFDebugLine, m_data_debug_line);
604}
605
606const DWARFDataExtractor &SymbolFileDWARF::get_debug_macro_data() {
607 return GetCachedSectionData(eSectionTypeDWARFDebugMacro, m_data_debug_macro);
608}
609
610const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
611 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
612}
613
614const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
615 return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
616 m_data_debug_ranges);
617}
618
619const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_data() {
620 return GetCachedSectionData(eSectionTypeDWARFDebugStr, m_data_debug_str);
621}
622
623const DWARFDataExtractor &SymbolFileDWARF::get_debug_str_offsets_data() {
624 return GetCachedSectionData(eSectionTypeDWARFDebugStrOffsets,
625 m_data_debug_str_offsets);
626}
627
628const DWARFDataExtractor &SymbolFileDWARF::get_apple_names_data() {
629 return GetCachedSectionData(eSectionTypeDWARFAppleNames, m_data_apple_names);
630}
631
632const DWARFDataExtractor &SymbolFileDWARF::get_apple_types_data() {
633 return GetCachedSectionData(eSectionTypeDWARFAppleTypes, m_data_apple_types);
634}
635
636const DWARFDataExtractor &SymbolFileDWARF::get_apple_namespaces_data() {
637 return GetCachedSectionData(eSectionTypeDWARFAppleNamespaces,
638 m_data_apple_namespaces);
639}
640
641const DWARFDataExtractor &SymbolFileDWARF::get_apple_objc_data() {
642 return GetCachedSectionData(eSectionTypeDWARFAppleObjC, m_data_apple_objc);
643}
644
645DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
646 if (m_abbr.get() == NULL) {
647 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
648 if (debug_abbrev_data.GetByteSize() > 0) {
649 m_abbr.reset(new DWARFDebugAbbrev());
650 if (m_abbr.get())
651 m_abbr->Parse(debug_abbrev_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 }
654 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655}
656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
658 return m_abbr.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
662 if (m_info.get() == NULL) {
663 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p",
664 LLVM_PRETTY_FUNCTION, static_cast<void *>(this));
665 if (get_debug_info_data().GetByteSize() > 0) {
666 m_info.reset(new DWARFDebugInfo());
667 if (m_info.get()) {
668 m_info->SetDwarfData(this);
669 }
Greg Clayton1f746072012-08-29 21:13:06 +0000670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 }
672 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673}
674
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
676 return m_info.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677}
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679DWARFCompileUnit *
680SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
681 if (!comp_unit)
Greg Clayton6071e6f2015-08-26 22:57:51 +0000682 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683
684 DWARFDebugInfo *info = DebugInfo();
685 if (info) {
686 // Just a normal DWARF file whose user ID for the compile unit is
687 // the DWARF offset itself
688
689 DWARFCompileUnit *dwarf_cu =
690 info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
691 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
692 dwarf_cu->SetUserData(comp_unit);
693 return dwarf_cu;
694 }
695 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698DWARFDebugRanges *SymbolFileDWARF::DebugRanges() {
699 if (m_ranges.get() == NULL) {
700 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p",
701 LLVM_PRETTY_FUNCTION, static_cast<void *>(this));
702 if (get_debug_ranges_data().GetByteSize() > 0) {
703 m_ranges.reset(new DWARFDebugRanges());
704 if (m_ranges.get())
705 m_ranges->Extract(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000706 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 }
708 return m_ranges.get();
709}
710
711const DWARFDebugRanges *SymbolFileDWARF::DebugRanges() const {
712 return m_ranges.get();
713}
714
715lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit *dwarf_cu,
716 uint32_t cu_idx) {
717 CompUnitSP cu_sp;
718 if (dwarf_cu) {
719 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu->GetUserData();
720 if (comp_unit) {
721 // We already parsed this compile unit, had out a shared pointer to it
722 cu_sp = comp_unit->shared_from_this();
723 } else {
724 if (dwarf_cu->GetSymbolFileDWARF() != this) {
725 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu,
726 cu_idx);
727 } else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
728 // Let the debug map create the compile unit
729 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
730 dwarf_cu->SetUserData(cu_sp.get());
731 } else {
732 ModuleSP module_sp(m_obj_file->GetModule());
733 if (module_sp) {
734 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
735 if (cu_die) {
736 FileSpec cu_file_spec{cu_die.GetName(), false};
737 if (cu_file_spec) {
738 // If we have a full path to the compile unit, we don't need to
739 // resolve
740 // the file. This can be expensive e.g. when the source files are
741 // NFS mounted.
742 if (cu_file_spec.IsRelative()) {
743 const char *cu_comp_dir{
744 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
745 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
746 }
747
748 std::string remapped_file;
Zachary Turnera498f0e2016-09-23 18:42:38 +0000749 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 remapped_file))
751 cu_file_spec.SetFile(remapped_file, false);
752 }
753
754 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(
755 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
756
757 bool is_optimized = dwarf_cu->GetIsOptimized();
758 cu_sp.reset(new CompileUnit(
759 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
760 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo));
761 if (cu_sp) {
762 // If we just created a compile unit with an invalid file spec,
763 // try and get the
764 // first entry in the supports files from the line table as that
765 // should be the
766 // compile unit.
767 if (!cu_file_spec) {
768 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
769 if (cu_file_spec) {
770 (FileSpec &)(*cu_sp) = cu_file_spec;
771 // Also fix the invalid file spec which was copied from the
772 // compile unit.
773 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
774 }
775 }
776
777 dwarf_cu->SetUserData(cu_sp.get());
778
779 // Figure out the compile unit index if we weren't given one
780 if (cu_idx == UINT32_MAX)
781 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
782
783 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
784 cu_idx, cu_sp);
785 }
786 }
787 }
788 }
789 }
790 }
791 return cu_sp;
792}
793
794uint32_t SymbolFileDWARF::GetNumCompileUnits() {
795 DWARFDebugInfo *info = DebugInfo();
796 if (info)
797 return info->GetNumCompileUnits();
798 return 0;
799}
800
801CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
802 CompUnitSP cu_sp;
803 DWARFDebugInfo *info = DebugInfo();
804 if (info) {
805 DWARFCompileUnit *dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
806 if (dwarf_cu)
807 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
808 }
809 return cu_sp;
810}
811
812Function *SymbolFileDWARF::ParseCompileUnitFunction(const SymbolContext &sc,
813 const DWARFDIE &die) {
814 if (die.IsValid()) {
815 TypeSystem *type_system =
816 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
817
818 if (type_system) {
819 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
820 if (dwarf_ast)
821 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
822 }
823 }
824 return nullptr;
825}
826
827bool SymbolFileDWARF::FixupAddress(Address &addr) {
828 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
829 if (debug_map_symfile) {
830 return debug_map_symfile->LinkOSOAddress(addr);
831 }
832 // This is a normal DWARF file, no address fixups need to happen
833 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +0000834}
Greg Clayton1f746072012-08-29 21:13:06 +0000835lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836SymbolFileDWARF::ParseCompileUnitLanguage(const SymbolContext &sc) {
837 assert(sc.comp_unit);
838 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
839 if (dwarf_cu)
840 return dwarf_cu->GetLanguageType();
841 else
842 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000843}
844
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845size_t SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) {
846 assert(sc.comp_unit);
847 size_t functions_added = 0;
848 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
849 if (dwarf_cu) {
850 DWARFDIECollection function_dies;
851 const size_t num_functions =
852 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
853 size_t func_idx;
854 for (func_idx = 0; func_idx < num_functions; ++func_idx) {
855 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
856 if (sc.comp_unit->FindFunctionByUID(die.GetID()).get() == NULL) {
857 if (ParseCompileUnitFunction(sc, die))
858 ++functions_added;
859 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 // FixupTypes();
862 }
863 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864}
865
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866bool SymbolFileDWARF::ParseCompileUnitSupportFiles(
867 const SymbolContext &sc, FileSpecList &support_files) {
868 assert(sc.comp_unit);
869 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
870 if (dwarf_cu) {
871 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 if (cu_die) {
874 const char *cu_comp_dir = resolveCompDir(
875 cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
876 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
877 DW_AT_stmt_list, DW_INVALID_OFFSET);
878 if (stmt_list != DW_INVALID_OFFSET) {
879 // All file indexes in DWARF are one based and a file of index zero is
880 // supposed to be the compile unit itself.
881 support_files.Append(*sc.comp_unit);
882 return DWARFDebugLine::ParseSupportFiles(
883 sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir,
884 stmt_list, support_files);
885 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887 }
888 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889}
890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891bool SymbolFileDWARF::ParseCompileUnitIsOptimized(
892 const lldb_private::SymbolContext &sc) {
893 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
894 if (dwarf_cu)
895 return dwarf_cu->GetIsOptimized();
896 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000897}
898
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899bool SymbolFileDWARF::ParseImportedModules(
900 const lldb_private::SymbolContext &sc,
901 std::vector<lldb_private::ConstString> &imported_modules) {
902 assert(sc.comp_unit);
903 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
904 if (dwarf_cu) {
905 if (ClangModulesDeclVendor::LanguageSupportsClangModules(
906 sc.comp_unit->GetLanguage())) {
907 UpdateExternalModuleListIfNeeded();
908
909 if (sc.comp_unit) {
910 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
911
912 if (die) {
913 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
914 child_die = child_die.GetSibling()) {
915 if (child_die.Tag() == DW_TAG_imported_declaration) {
916 if (DWARFDIE module_die =
917 child_die.GetReferencedDIE(DW_AT_import)) {
918 if (module_die.Tag() == DW_TAG_module) {
919 if (const char *name = module_die.GetAttributeValueAsString(
920 DW_AT_name, nullptr)) {
921 ConstString const_name(name);
922 imported_modules.push_back(const_name);
923 }
Sean Callananb0300a42016-01-14 21:46:09 +0000924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 }
Sean Callananb0300a42016-01-14 21:46:09 +0000926 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 } else {
930 for (const auto &pair : m_external_type_modules) {
931 imported_modules.push_back(pair.first);
932 }
933 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 }
936 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000937}
938
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939struct ParseDWARFLineTableCallbackInfo {
940 LineTable *line_table;
941 std::unique_ptr<LineSequence> sequence_ap;
942 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943};
944
945//----------------------------------------------------------------------
946// ParseStatementTableCallback
947//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948static void ParseDWARFLineTableCallback(dw_offset_t offset,
949 const DWARFDebugLine::State &state,
950 void *userData) {
951 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
952 // Just started parsing the line table
953 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
954 // Done parsing line table, nothing to do for the cleanup
955 } else {
956 ParseDWARFLineTableCallbackInfo *info =
957 (ParseDWARFLineTableCallbackInfo *)userData;
958 LineTable *line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 // If this is our first time here, we need to create a
961 // sequence container.
962 if (!info->sequence_ap.get()) {
963 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
964 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 line_table->AppendLineEntryToSequence(
967 info->sequence_ap.get(), state.address & info->addr_mask, state.line,
968 state.column, state.file, state.is_stmt, state.basic_block,
969 state.prologue_end, state.epilogue_begin, state.end_sequence);
970 if (state.end_sequence) {
971 // First, put the current sequence into the line table.
972 line_table->InsertSequence(info->sequence_ap.get());
973 // Then, empty it to prepare for the next sequence.
974 info->sequence_ap->Clear();
975 }
976 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977}
978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979bool SymbolFileDWARF::ParseCompileUnitLineTable(const SymbolContext &sc) {
980 assert(sc.comp_unit);
981 if (sc.comp_unit->GetLineTable() != NULL)
982 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
985 if (dwarf_cu) {
986 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
987 if (dwarf_cu_die) {
988 const dw_offset_t cu_line_offset =
989 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
990 DW_INVALID_OFFSET);
991 if (cu_line_offset != DW_INVALID_OFFSET) {
992 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
993 if (line_table_ap.get()) {
994 ParseDWARFLineTableCallbackInfo info;
995 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 /*
998 * MIPS:
999 * The SymbolContext may not have a valid target, thus we may not be
1000 * able
1001 * to call Address::GetOpcodeLoadAddress() which would clear the bit
1002 * #0
1003 * for MIPS. Use ArchSpec to clear the bit #0.
1004 */
1005 ArchSpec arch;
1006 GetObjectFile()->GetArchitecture(arch);
1007 switch (arch.GetMachine()) {
1008 case llvm::Triple::mips:
1009 case llvm::Triple::mipsel:
1010 case llvm::Triple::mips64:
1011 case llvm::Triple::mips64el:
1012 info.addr_mask = ~((lldb::addr_t)1);
1013 break;
1014 default:
1015 info.addr_mask = ~((lldb::addr_t)0);
1016 break;
1017 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 lldb::offset_t offset = cu_line_offset;
1020 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset,
1021 ParseDWARFLineTableCallback,
1022 &info);
1023 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
1024 if (debug_map_symfile) {
1025 // We have an object file that has a line table with addresses
1026 // that are not linked. We need to link the line table and convert
1027 // the addresses that are relative to the .o file into addresses
1028 // for the main executable.
1029 sc.comp_unit->SetLineTable(
1030 debug_map_symfile->LinkOSOLineTable(this, line_table_ap.get()));
1031 } else {
1032 sc.comp_unit->SetLineTable(line_table_ap.release());
1033 return true;
1034 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 }
1039 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040}
1041
Siva Chandrad8335e92015-12-16 00:22:08 +00001042lldb_private::DebugMacrosSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1044 auto iter = m_debug_macros_map.find(*offset);
1045 if (iter != m_debug_macros_map.end())
1046 return iter->second;
Siva Chandrad8335e92015-12-16 00:22:08 +00001047
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1049 if (debug_macro_data.GetByteSize() == 0)
1050 return DebugMacrosSP();
Siva Chandrad8335e92015-12-16 00:22:08 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1053 m_debug_macros_map[*offset] = debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 const DWARFDebugMacroHeader &header =
1056 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1057 DWARFDebugMacroEntry::ReadMacroEntries(debug_macro_data, get_debug_str_data(),
1058 header.OffsetIs64Bit(), offset, this,
1059 debug_macros_sp);
Siva Chandrad8335e92015-12-16 00:22:08 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 return debug_macros_sp;
Siva Chandrad8335e92015-12-16 00:22:08 +00001062}
1063
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064bool SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
1065 assert(sc.comp_unit);
Siva Chandrad8335e92015-12-16 00:22:08 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1068 if (dwarf_cu == nullptr)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001069 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070
1071 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1072 if (!dwarf_cu_die)
1073 return false;
1074
1075 lldb::offset_t sect_offset =
1076 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1077 if (sect_offset == DW_INVALID_OFFSET)
1078 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1079 DW_INVALID_OFFSET);
1080 if (sect_offset == DW_INVALID_OFFSET)
1081 return false;
1082
1083 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1084
1085 return true;
Greg Claytonc4ffd662013-03-08 01:37:30 +00001086}
1087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc,
1089 Block *parent_block,
1090 const DWARFDIE &orig_die,
1091 addr_t subprogram_low_pc,
1092 uint32_t depth) {
1093 size_t blocks_added = 0;
1094 DWARFDIE die = orig_die;
1095 while (die) {
1096 dw_tag_t tag = die.Tag();
Paul Hermanea188fc2015-09-16 18:48:30 +00001097
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 switch (tag) {
1099 case DW_TAG_inlined_subroutine:
1100 case DW_TAG_subprogram:
1101 case DW_TAG_lexical_block: {
1102 Block *block = NULL;
1103 if (tag == DW_TAG_subprogram) {
1104 // Skip any DW_TAG_subprogram DIEs that are inside
1105 // of a normal or inlined functions. These will be
1106 // parsed on their own as separate entities.
1107
1108 if (depth > 0)
1109 break;
1110
1111 block = parent_block;
1112 } else {
1113 BlockSP block_sp(new Block(die.GetID()));
1114 parent_block->AddChild(block_sp);
1115 block = block_sp.get();
1116 }
1117 DWARFRangeList ranges;
1118 const char *name = NULL;
1119 const char *mangled_name = NULL;
1120
1121 int decl_file = 0;
1122 int decl_line = 0;
1123 int decl_column = 0;
1124 int call_file = 0;
1125 int call_line = 0;
1126 int call_column = 0;
1127 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1128 decl_line, decl_column, call_file, call_line,
1129 call_column, nullptr)) {
1130 if (tag == DW_TAG_subprogram) {
1131 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1132 subprogram_low_pc = ranges.GetMinRangeBase(0);
1133 } else if (tag == DW_TAG_inlined_subroutine) {
1134 // We get called here for inlined subroutines in two ways.
1135 // The first time is when we are making the Function object
1136 // for this inlined concrete instance. Since we're creating a top
1137 // level block at
1138 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we
1139 // need to
1140 // adjust the containing address.
1141 // The second time is when we are parsing the blocks inside the
1142 // function that contains
1143 // the inlined concrete instance. Since these will be blocks inside
1144 // the containing "real"
1145 // function the offset will be for that function.
1146 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1147 subprogram_low_pc = ranges.GetMinRangeBase(0);
1148 }
1149 }
1150
1151 const size_t num_ranges = ranges.GetSize();
1152 for (size_t i = 0; i < num_ranges; ++i) {
1153 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1154 const addr_t range_base = range.GetRangeBase();
1155 if (range_base >= subprogram_low_pc)
1156 block->AddRange(Block::Range(range_base - subprogram_low_pc,
1157 range.GetByteSize()));
1158 else {
1159 GetObjectFile()->GetModule()->ReportError(
1160 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1161 ") which has a base that is less than the function's low PC "
1162 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1163 "start of this error message",
1164 block->GetID(), range_base, range.GetRangeEnd(),
1165 subprogram_low_pc);
1166 }
1167 }
1168 block->FinalizeRanges();
1169
1170 if (tag != DW_TAG_subprogram &&
1171 (name != NULL || mangled_name != NULL)) {
1172 std::unique_ptr<Declaration> decl_ap;
1173 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1174 decl_ap.reset(new Declaration(
1175 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1176 decl_line, decl_column));
1177
1178 std::unique_ptr<Declaration> call_ap;
1179 if (call_file != 0 || call_line != 0 || call_column != 0)
1180 call_ap.reset(new Declaration(
1181 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1182 call_line, call_column));
1183
1184 block->SetInlinedFunctionInfo(name, mangled_name, decl_ap.get(),
1185 call_ap.get());
1186 }
1187
1188 ++blocks_added;
1189
1190 if (die.HasChildren()) {
1191 blocks_added += ParseFunctionBlocks(sc, block, die.GetFirstChild(),
1192 subprogram_low_pc, depth + 1);
1193 }
1194 }
1195 } break;
1196 default:
1197 break;
1198 }
1199
1200 // Only parse siblings of the block if we are not at depth zero. A depth
1201 // of zero indicates we are currently parsing the top level
1202 // DW_TAG_subprogram DIE
1203
1204 if (depth == 0)
1205 die.Clear();
1206 else
1207 die = die.GetSibling();
1208 }
1209 return blocks_added;
Paul Hermanea188fc2015-09-16 18:48:30 +00001210}
1211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1213 if (parent_die) {
1214 for (DWARFDIE die = parent_die.GetFirstChild(); die;
1215 die = die.GetSibling()) {
1216 dw_tag_t tag = die.Tag();
1217 bool check_virtuality = false;
1218 switch (tag) {
1219 case DW_TAG_inheritance:
1220 case DW_TAG_subprogram:
1221 check_virtuality = true;
1222 break;
1223 default:
1224 break;
1225 }
1226 if (check_virtuality) {
1227 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1228 return true;
1229 }
1230 }
1231 }
1232 return false;
1233}
1234
1235void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1236 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1237 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1238 std::vector<DWARFDIE> decl_ctx_die_list =
1239 ast_parser->GetDIEForDeclContext(decl_ctx);
1240
1241 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1242 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl;
1243 decl = decl.GetSibling())
1244 ast_parser->GetDeclForUIDFromDWARF(decl);
1245}
1246
1247SymbolFileDWARF *SymbolFileDWARF::GetDWARFForUID(lldb::user_id_t uid) {
1248 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1249 // we must make sure we use the correct DWARF file when resolving things.
1250 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1251 // SymbolFileDWARF classes, one for each .o file. We can often end up
1252 // with references to other DWARF objects and we must be ready to receive
1253 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1254 // instance.
1255 SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile();
1256 if (debug_map)
1257 return debug_map->GetSymbolFileByOSOIndex(
1258 debug_map->GetOSOIndexFromUserID(uid));
1259 return this;
Greg Clayton07c8c442016-04-25 23:39:19 +00001260}
1261
1262DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263SymbolFileDWARF::GetDIEFromUID(lldb::user_id_t uid) {
1264 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API
1265 // we must make sure we use the correct DWARF file when resolving things.
1266 // On MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1267 // SymbolFileDWARF classes, one for each .o file. We can often end up
1268 // with references to other DWARF objects and we must be ready to receive
1269 // a "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1270 // instance.
1271 SymbolFileDWARF *dwarf = GetDWARFForUID(uid);
1272 if (dwarf)
1273 return dwarf->GetDIE(DIERef(uid, dwarf));
1274 return DWARFDIE();
Greg Clayton07c8c442016-04-25 23:39:19 +00001275}
1276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1278 // Anytime we have a lldb::user_id_t, we must get the DIE by
1279 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1280 // the SymbolFileDWARF::GetDIEFromUID() for details.
1281 DWARFDIE die = GetDIEFromUID(type_uid);
1282 if (die)
1283 return die.GetDecl();
1284 return CompilerDecl();
Paul Hermand628cbb2015-09-15 23:44:17 +00001285}
1286
Greg Clayton99558cc42015-08-24 23:46:31 +00001287CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1289 // Anytime we have a lldb::user_id_t, we must get the DIE by
1290 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1291 // the SymbolFileDWARF::GetDIEFromUID() for details.
1292 DWARFDIE die = GetDIEFromUID(type_uid);
1293 if (die)
1294 return die.GetDeclContext();
1295 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001296}
1297
Greg Clayton99558cc42015-08-24 23:46:31 +00001298CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1300 // Anytime we have a lldb::user_id_t, we must get the DIE by
1301 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1302 // the SymbolFileDWARF::GetDIEFromUID() for details.
1303 DWARFDIE die = GetDIEFromUID(type_uid);
1304 if (die)
1305 return die.GetContainingDeclContext();
1306 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307}
1308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1310 // Anytime we have a lldb::user_id_t, we must get the DIE by
1311 // calling SymbolFileDWARF::GetDIEFromUID(). See comments inside
1312 // the SymbolFileDWARF::GetDIEFromUID() for details.
1313 DWARFDIE type_die = GetDIEFromUID(type_uid);
1314 if (type_die)
1315 return type_die.ResolveType();
1316 else
1317 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318}
1319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1321 return ResolveType(GetDIE(die_ref), true);
Greg Clayton2f869fe2016-03-30 20:14:35 +00001322}
1323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1325 bool assert_not_being_parsed) {
1326 if (die) {
1327 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1328 if (log)
1329 GetObjectFile()->GetModule()->LogMessage(
1330 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1331 die.GetOffset(), die.GetTagAsCString(), die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001332
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 // We might be coming in in the middle of a type tree (a class
1334 // within a class, an enum within a class), so parse any needed
1335 // parent DIEs before we get to this one...
1336 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1337 if (decl_ctx_die) {
1338 if (log) {
1339 switch (decl_ctx_die.Tag()) {
1340 case DW_TAG_structure_type:
1341 case DW_TAG_union_type:
1342 case DW_TAG_class_type: {
1343 // Get the type, which could be a forward declaration
1344 if (log)
1345 GetObjectFile()->GetModule()->LogMessage(
1346 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1347 "resolve parent forward type for 0x%8.8x",
1348 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1349 decl_ctx_die.GetOffset());
1350 } break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001351
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 default:
1353 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001354 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001356 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 return ResolveType(die);
1358 }
1359 return NULL;
Greg Claytoncab36a32011-12-08 05:16:30 +00001360}
1361
Greg Clayton6beaaa62011-01-17 03:46:26 +00001362// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1363// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001364// can resolve a compiler_type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365bool SymbolFileDWARF::HasForwardDeclForClangType(
1366 const CompilerType &compiler_type) {
1367 CompilerType compiler_type_no_qualifiers =
1368 ClangUtil::RemoveFastQualifiers(compiler_type);
1369 if (GetForwardDeclClangTypeToDie().count(
1370 compiler_type_no_qualifiers.GetOpaqueQualType())) {
1371 return true;
1372 }
1373 TypeSystem *type_system = compiler_type.GetTypeSystem();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 ClangASTContext *clang_type_system =
1376 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1377 if (!clang_type_system)
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001378 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 DWARFASTParserClang *ast_parser =
1380 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1381 return ast_parser->GetClangASTImporter().CanImport(compiler_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001382}
1383
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1385 std::lock_guard<std::recursive_mutex> guard(
1386 GetObjectFile()->GetModule()->GetMutex());
Greg Claytoncab36a32011-12-08 05:16:30 +00001387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 ClangASTContext *clang_type_system =
1389 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
1390 if (clang_type_system) {
1391 DWARFASTParserClang *ast_parser =
1392 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1393 if (ast_parser &&
1394 ast_parser->GetClangASTImporter().CanImport(compiler_type))
1395 return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1396 }
Jim Inghamc3549282012-01-11 02:21:12 +00001397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 // We have a struct/union/class/enum that needs to be fully resolved.
1399 CompilerType compiler_type_no_qualifiers =
1400 ClangUtil::RemoveFastQualifiers(compiler_type);
1401 auto die_it = GetForwardDeclClangTypeToDie().find(
1402 compiler_type_no_qualifiers.GetOpaqueQualType());
1403 if (die_it == GetForwardDeclClangTypeToDie().end()) {
1404 // We have already resolved this type...
1405 return true;
1406 }
1407
1408 DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1409 if (dwarf_die) {
1410 // Once we start resolving this type, remove it from the forward declaration
1411 // map in case anyone child members or other types require this type to get
1412 // resolved.
1413 // The type will get resolved when all of the calls to
1414 // SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1415 // are done.
1416 GetForwardDeclClangTypeToDie().erase(die_it);
1417
1418 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1419
1420 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1421 DWARF_LOG_TYPE_COMPLETION));
1422 if (log)
1423 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1424 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1425 dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1426 type->GetName().AsCString());
1427 assert(compiler_type);
1428 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1429 if (dwarf_ast)
1430 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1431 }
1432 return false;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001433}
1434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1436 bool assert_not_being_parsed,
1437 bool resolve_function_context) {
1438 if (die) {
1439 Type *type = GetTypeForDIE(die, resolve_function_context).get();
1440
1441 if (assert_not_being_parsed) {
1442 if (type != DIE_IS_BEING_PARSED)
1443 return type;
1444
1445 GetObjectFile()->GetModule()->ReportError(
1446 "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1447 die.GetOffset(), die.GetTagAsCString(), die.GetName());
1448
1449 } else
1450 return type;
1451 }
1452 return nullptr;
1453}
1454
1455CompileUnit *
1456SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit *dwarf_cu,
1457 uint32_t cu_idx) {
1458 // Check if the symbol vendor already knows about this compile unit?
1459 if (dwarf_cu->GetUserData() == NULL) {
1460 // The symbol vendor doesn't know about this compile unit, we
1461 // need to parse and add it to the symbol vendor object.
1462 return ParseCompileUnit(dwarf_cu, cu_idx).get();
1463 }
1464 return (CompileUnit *)dwarf_cu->GetUserData();
1465}
1466
1467size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
1468 DIEArray &method_die_offsets) {
1469 method_die_offsets.clear();
1470 if (m_using_apple_tables) {
1471 if (m_apple_objc_ap.get())
1472 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1473 } else {
1474 if (!m_indexed)
1475 Index();
1476
1477 m_objc_class_selectors_index.Find(class_name, method_die_offsets);
1478 }
1479 return method_die_offsets.size();
1480}
1481
1482bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1483 sc.Clear(false);
1484
1485 if (die) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486 // Check if the symbol vendor already knows about this compile unit?
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1488
1489 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1490 if (sc.function == NULL)
1491 sc.function = ParseCompileUnitFunction(sc, die);
1492
1493 if (sc.function) {
1494 sc.module_sp = sc.function->CalculateSymbolContextModule();
1495 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497 }
1498
1499 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500}
1501
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) {
1503 UpdateExternalModuleListIfNeeded();
1504 const auto &pos = m_external_type_modules.find(name);
1505 if (pos != m_external_type_modules.end())
1506 return pos->second;
1507 else
1508 return lldb::ModuleSP();
Greg Claytone6b36cd2015-12-08 01:02:08 +00001509}
1510
Greg Clayton2f869fe2016-03-30 20:14:35 +00001511DWARFDIE
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1513 DWARFDebugInfo *debug_info = DebugInfo();
1514 if (debug_info)
1515 return debug_info->GetDIE(die_ref);
1516 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001517 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00001518}
1519
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520std::unique_ptr<SymbolFileDWARFDwo>
1521SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1522 DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die) {
1523 // If we are using a dSYM file, we never want the standard DWO files since
1524 // the -gmodule support uses the same DWO machanism to specify full debug
1525 // info files for modules.
1526 if (GetDebugMapSymfile())
1527 return nullptr;
Greg Clayton2bc22f82011-09-30 03:20:47 +00001528
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 const char *dwo_name = cu_die.GetAttributeValueAsString(
1530 this, &dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1531 if (!dwo_name)
1532 return nullptr;
1533
1534 FileSpec dwo_file(dwo_name, true);
1535 if (dwo_file.IsRelative()) {
1536 const char *comp_dir = cu_die.GetAttributeValueAsString(
1537 this, &dwarf_cu, DW_AT_comp_dir, nullptr);
1538 if (!comp_dir)
1539 return nullptr;
1540
1541 dwo_file.SetFile(comp_dir, true);
1542 dwo_file.AppendPathComponent(dwo_name);
1543 }
1544
1545 if (!dwo_file.Exists())
1546 return nullptr;
1547
1548 const lldb::offset_t file_offset = 0;
1549 DataBufferSP dwo_file_data_sp;
1550 lldb::offset_t dwo_file_data_offset = 0;
1551 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1552 GetObjectFile()->GetModule(), &dwo_file, file_offset,
1553 dwo_file.GetByteSize(), dwo_file_data_sp, dwo_file_data_offset);
1554 if (dwo_obj_file == nullptr)
1555 return nullptr;
1556
1557 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, &dwarf_cu);
Greg Clayton901c5ca2011-12-03 04:40:03 +00001558}
1559
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1561 if (m_fetched_external_modules)
1562 return;
1563 m_fetched_external_modules = true;
1564
1565 DWARFDebugInfo *debug_info = DebugInfo();
1566
1567 const uint32_t num_compile_units = GetNumCompileUnits();
1568 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1569 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1570
1571 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1572 if (die && die.HasChildren() == false) {
1573 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1574
1575 if (name) {
1576 ConstString const_name(name);
1577 if (m_external_type_modules.find(const_name) ==
1578 m_external_type_modules.end()) {
1579 ModuleSP module_sp;
1580 const char *dwo_path =
1581 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1582 if (dwo_path) {
1583 ModuleSpec dwo_module_spec;
1584 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1585 if (dwo_module_spec.GetFileSpec().IsRelative()) {
1586 const char *comp_dir =
1587 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1588 if (comp_dir) {
1589 dwo_module_spec.GetFileSpec().SetFile(comp_dir, true);
1590 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1591 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 dwo_module_spec.GetArchitecture() =
1594 m_obj_file->GetModule()->GetArchitecture();
1595 // printf ("Loading dwo = '%s'\n", dwo_path);
1596 Error error = ModuleList::GetSharedModule(
1597 dwo_module_spec, module_sp, NULL, NULL, NULL);
1598 if (!module_sp) {
1599 GetObjectFile()->GetModule()->ReportWarning(
1600 "0x%8.8x: unable to locate module needed for external types: "
1601 "%s\nerror: %s\nDebugging will be degraded due to missing "
1602 "types. Rebuilding your project will regenerate the needed "
1603 "module files.",
1604 die.GetOffset(),
1605 dwo_module_spec.GetFileSpec().GetPath().c_str(),
1606 error.AsCString("unknown error"));
1607 }
1608 }
1609 m_external_type_modules[const_name] = module_sp;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001610 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00001612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 }
1614}
1615
1616SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1617 if (!m_global_aranges_ap) {
1618 m_global_aranges_ap.reset(new GlobalVariableMap());
1619
1620 ModuleSP module_sp = GetObjectFile()->GetModule();
1621 if (module_sp) {
1622 const size_t num_cus = module_sp->GetNumCompileUnits();
1623 for (size_t i = 0; i < num_cus; ++i) {
1624 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1625 if (cu_sp) {
1626 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1627 if (globals_sp) {
1628 const size_t num_globals = globals_sp->GetSize();
1629 for (size_t g = 0; g < num_globals; ++g) {
1630 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1631 if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1632 const DWARFExpression &location = var_sp->LocationExpression();
1633 Value location_result;
1634 Error error;
1635 if (location.Evaluate(nullptr, nullptr, nullptr,
1636 LLDB_INVALID_ADDRESS, nullptr, nullptr,
1637 location_result, &error)) {
1638 if (location_result.GetValueType() ==
1639 Value::eValueTypeFileAddress) {
1640 lldb::addr_t file_addr =
1641 location_result.GetScalar().ULongLong();
1642 lldb::addr_t byte_size = 1;
1643 if (var_sp->GetType())
1644 byte_size = var_sp->GetType()->GetByteSize();
1645 m_global_aranges_ap->Append(GlobalVariableMap::Entry(
1646 file_addr, byte_size, var_sp.get()));
1647 }
1648 }
1649 }
1650 }
1651 }
1652 }
1653 }
1654 }
1655 m_global_aranges_ap->Sort();
1656 }
1657 return *m_global_aranges_ap;
1658}
1659
1660uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1661 uint32_t resolve_scope,
1662 SymbolContext &sc) {
1663 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "SymbolFileDWARF::"
1664 "ResolveSymbolContext (so_addr = { "
1665 "section = %p, offset = 0x%" PRIx64
1666 " }, resolve_scope = 0x%8.8x)",
1667 static_cast<void *>(so_addr.GetSection().get()),
1668 so_addr.GetOffset(), resolve_scope);
1669 uint32_t resolved = 0;
1670 if (resolve_scope &
1671 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1672 eSymbolContextLineEntry | eSymbolContextVariable)) {
1673 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1674
1675 DWARFDebugInfo *debug_info = DebugInfo();
1676 if (debug_info) {
1677 const dw_offset_t cu_offset =
1678 debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1679 if (cu_offset == DW_INVALID_OFFSET) {
1680 // Global variables are not in the compile unit address ranges. The only
1681 // way to
1682 // currently find global variables is to iterate over the
1683 // .debug_pubnames or the
1684 // __apple_names table and find all items in there that point to
1685 // DW_TAG_variable
1686 // DIEs and then find the address that matches.
1687 if (resolve_scope & eSymbolContextVariable) {
1688 GlobalVariableMap &map = GetGlobalAranges();
1689 const GlobalVariableMap::Entry *entry =
1690 map.FindEntryThatContains(file_vm_addr);
1691 if (entry && entry->data) {
1692 Variable *variable = entry->data;
1693 SymbolContextScope *scc = variable->GetSymbolContextScope();
1694 if (scc) {
1695 scc->CalculateSymbolContext(&sc);
1696 sc.variable = variable;
1697 }
1698 return sc.GetResolvedMask();
1699 }
1700 }
1701 } else {
1702 uint32_t cu_idx = DW_INVALID_INDEX;
1703 DWARFCompileUnit *dwarf_cu =
1704 debug_info->GetCompileUnit(cu_offset, &cu_idx);
1705 if (dwarf_cu) {
1706 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1707 if (sc.comp_unit) {
1708 resolved |= eSymbolContextCompUnit;
1709
1710 bool force_check_line_table = false;
1711 if (resolve_scope &
1712 (eSymbolContextFunction | eSymbolContextBlock)) {
1713 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1714 DWARFDIE block_die;
1715 if (function_die) {
1716 sc.function =
1717 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1718 if (sc.function == NULL)
1719 sc.function = ParseCompileUnitFunction(sc, function_die);
1720
1721 if (sc.function && (resolve_scope & eSymbolContextBlock))
1722 block_die = function_die.LookupDeepestBlock(file_vm_addr);
1723 } else {
1724 // We might have had a compile unit that had discontiguous
1725 // address ranges where the gaps are symbols that don't have
1726 // any debug info. Discontiguous compile unit address ranges
1727 // should only happen when there aren't other functions from
1728 // other compile units in these gaps. This helps keep the size
1729 // of the aranges down.
1730 force_check_line_table = true;
1731 }
1732
1733 if (sc.function != NULL) {
1734 resolved |= eSymbolContextFunction;
1735
1736 if (resolve_scope & eSymbolContextBlock) {
1737 Block &block = sc.function->GetBlock(true);
1738
1739 if (block_die)
1740 sc.block = block.FindBlockByID(block_die.GetID());
1741 else
1742 sc.block = block.FindBlockByID(function_die.GetID());
1743 if (sc.block)
1744 resolved |= eSymbolContextBlock;
1745 }
1746 }
1747 }
1748
1749 if ((resolve_scope & eSymbolContextLineEntry) ||
1750 force_check_line_table) {
1751 LineTable *line_table = sc.comp_unit->GetLineTable();
1752 if (line_table != NULL) {
1753 // And address that makes it into this function should be in
1754 // terms
1755 // of this debug file if there is no debug map, or it will be an
1756 // address in the .o file which needs to be fixed up to be in
1757 // terms
1758 // of the debug map executable. Either way, calling
1759 // FixupAddress()
1760 // will work for us.
1761 Address exe_so_addr(so_addr);
1762 if (FixupAddress(exe_so_addr)) {
1763 if (line_table->FindLineEntryByAddress(exe_so_addr,
1764 sc.line_entry)) {
1765 resolved |= eSymbolContextLineEntry;
1766 }
1767 }
1768 }
1769 }
1770
1771 if (force_check_line_table &&
1772 !(resolved & eSymbolContextLineEntry)) {
1773 // We might have had a compile unit that had discontiguous
1774 // address ranges where the gaps are symbols that don't have
1775 // any debug info. Discontiguous compile unit address ranges
1776 // should only happen when there aren't other functions from
1777 // other compile units in these gaps. This helps keep the size
1778 // of the aranges down.
1779 sc.comp_unit = NULL;
1780 resolved &= ~eSymbolContextCompUnit;
1781 }
1782 } else {
1783 GetObjectFile()->GetModule()->ReportWarning(
1784 "0x%8.8x: compile unit %u failed to create a valid "
1785 "lldb_private::CompileUnit class.",
1786 cu_offset, cu_idx);
1787 }
1788 }
1789 }
1790 }
1791 }
1792 return resolved;
1793}
1794
1795uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1796 uint32_t line,
1797 bool check_inlines,
1798 uint32_t resolve_scope,
1799 SymbolContextList &sc_list) {
1800 const uint32_t prev_size = sc_list.GetSize();
1801 if (resolve_scope & eSymbolContextCompUnit) {
1802 DWARFDebugInfo *debug_info = DebugInfo();
1803 if (debug_info) {
1804 uint32_t cu_idx;
1805 DWARFCompileUnit *dwarf_cu = NULL;
1806
1807 for (cu_idx = 0;
1808 (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL;
1809 ++cu_idx) {
1810 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1811 const bool full_match = (bool)file_spec.GetDirectory();
1812 bool file_spec_matches_cu_file_spec =
1813 dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
1814 if (check_inlines || file_spec_matches_cu_file_spec) {
1815 SymbolContext sc(m_obj_file->GetModule());
1816 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
1817 if (sc.comp_unit) {
1818 uint32_t file_idx = UINT32_MAX;
1819
1820 // If we are looking for inline functions only and we don't
1821 // find it in the support files, we are done.
1822 if (check_inlines) {
1823 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1824 1, file_spec, true);
1825 if (file_idx == UINT32_MAX)
1826 continue;
1827 }
1828
1829 if (line != 0) {
1830 LineTable *line_table = sc.comp_unit->GetLineTable();
1831
1832 if (line_table != NULL && line != 0) {
1833 // We will have already looked up the file index if
1834 // we are searching for inline entries.
1835 if (!check_inlines)
1836 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1837 1, file_spec, true);
1838
1839 if (file_idx != UINT32_MAX) {
1840 uint32_t found_line;
1841 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1842 0, file_idx, line, false, &sc.line_entry);
1843 found_line = sc.line_entry.line;
1844
1845 while (line_idx != UINT32_MAX) {
1846 sc.function = NULL;
1847 sc.block = NULL;
1848 if (resolve_scope &
1849 (eSymbolContextFunction | eSymbolContextBlock)) {
1850 const lldb::addr_t file_vm_addr =
1851 sc.line_entry.range.GetBaseAddress().GetFileAddress();
1852 if (file_vm_addr != LLDB_INVALID_ADDRESS) {
1853 DWARFDIE function_die =
1854 dwarf_cu->LookupAddress(file_vm_addr);
1855 DWARFDIE block_die;
1856 if (function_die) {
1857 sc.function =
1858 sc.comp_unit
1859 ->FindFunctionByUID(function_die.GetID())
1860 .get();
1861 if (sc.function == NULL)
1862 sc.function =
1863 ParseCompileUnitFunction(sc, function_die);
1864
1865 if (sc.function &&
1866 (resolve_scope & eSymbolContextBlock))
1867 block_die =
1868 function_die.LookupDeepestBlock(file_vm_addr);
1869 }
1870
1871 if (sc.function != NULL) {
1872 Block &block = sc.function->GetBlock(true);
1873
1874 if (block_die)
1875 sc.block = block.FindBlockByID(block_die.GetID());
1876 else if (function_die)
1877 sc.block =
1878 block.FindBlockByID(function_die.GetID());
1879 }
1880 }
1881 }
1882
1883 sc_list.Append(sc);
1884 line_idx = line_table->FindLineEntryIndexByFileIndex(
1885 line_idx + 1, file_idx, found_line, true,
1886 &sc.line_entry);
1887 }
1888 }
1889 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1890 // only append the context if we aren't looking for inline call
1891 // sites
1892 // by file and line and if the file spec matches that of the
1893 // compile unit
1894 sc_list.Append(sc);
1895 }
1896 } else if (file_spec_matches_cu_file_spec && !check_inlines) {
1897 // only append the context if we aren't looking for inline call
1898 // sites
1899 // by file and line and if the file spec matches that of the
1900 // compile unit
1901 sc_list.Append(sc);
1902 }
1903
1904 if (!check_inlines)
1905 break;
1906 }
1907 }
1908 }
1909 }
1910 }
1911 return sc_list.GetSize() - prev_size;
1912}
1913
1914void SymbolFileDWARF::Index() {
1915 if (m_indexed)
1916 return;
1917 m_indexed = true;
1918 Timer scoped_timer(
1919 LLVM_PRETTY_FUNCTION, "SymbolFileDWARF::Index (%s)",
1920 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
1921
1922 DWARFDebugInfo *debug_info = DebugInfo();
1923 if (debug_info) {
1924 const uint32_t num_compile_units = GetNumCompileUnits();
1925 if (num_compile_units == 0)
1926 return;
1927
1928 std::vector<NameToDIE> function_basename_index(num_compile_units);
1929 std::vector<NameToDIE> function_fullname_index(num_compile_units);
1930 std::vector<NameToDIE> function_method_index(num_compile_units);
1931 std::vector<NameToDIE> function_selector_index(num_compile_units);
1932 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
1933 std::vector<NameToDIE> global_index(num_compile_units);
1934 std::vector<NameToDIE> type_index(num_compile_units);
1935 std::vector<NameToDIE> namespace_index(num_compile_units);
1936
1937 std::vector<bool> clear_cu_dies(num_compile_units, false);
1938 auto parser_fn = [this, debug_info, &function_basename_index,
1939 &function_fullname_index, &function_method_index,
1940 &function_selector_index, &objc_class_selectors_index,
1941 &global_index, &type_index,
1942 &namespace_index](uint32_t cu_idx) {
1943 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1944 if (dwarf_cu) {
1945 dwarf_cu->Index(
1946 function_basename_index[cu_idx], function_fullname_index[cu_idx],
1947 function_method_index[cu_idx], function_selector_index[cu_idx],
1948 objc_class_selectors_index[cu_idx], global_index[cu_idx],
1949 type_index[cu_idx], namespace_index[cu_idx]);
1950 }
1951 return cu_idx;
1952 };
1953
1954 auto extract_fn = [this, debug_info, num_compile_units](uint32_t cu_idx) {
1955 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1956 if (dwarf_cu) {
1957 // dwarf_cu->ExtractDIEsIfNeeded(false) will return zero if the
1958 // DIEs for a compile unit have already been parsed.
1959 return std::make_pair(cu_idx, dwarf_cu->ExtractDIEsIfNeeded(false) > 1);
1960 }
1961 return std::make_pair(cu_idx, false);
1962 };
1963
1964 // Create a task runner that extracts dies for each DWARF compile unit in a
1965 // separate thread
1966 TaskRunner<std::pair<uint32_t, bool>> task_runner_extract;
1967 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1968 task_runner_extract.AddTask(extract_fn, cu_idx);
1969
1970 //----------------------------------------------------------------------
1971 // First figure out which compile units didn't have their DIEs already
1972 // parsed and remember this. If no DIEs were parsed prior to this index
1973 // function call, we are going to want to clear the CU dies after we
1974 // are done indexing to make sure we don't pull in all DWARF dies, but
1975 // we need to wait until all compile units have been indexed in case
1976 // a DIE in one compile unit refers to another and the indexes accesses
1977 // those DIEs.
1978 //----------------------------------------------------------------------
1979 while (true) {
1980 auto f = task_runner_extract.WaitForNextCompletedTask();
1981 if (!f.valid())
1982 break;
1983 unsigned cu_idx;
1984 bool clear;
1985 std::tie(cu_idx, clear) = f.get();
1986 clear_cu_dies[cu_idx] = clear;
1987 }
1988
1989 // Now create a task runner that can index each DWARF compile unit in a
1990 // separate
1991 // thread so we can index quickly.
1992
1993 TaskRunner<uint32_t> task_runner;
1994 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1995 task_runner.AddTask(parser_fn, cu_idx);
1996
1997 while (true) {
1998 std::future<uint32_t> f = task_runner.WaitForNextCompletedTask();
1999 if (!f.valid())
2000 break;
2001 uint32_t cu_idx = f.get();
2002
2003 m_function_basename_index.Append(function_basename_index[cu_idx]);
2004 m_function_fullname_index.Append(function_fullname_index[cu_idx]);
2005 m_function_method_index.Append(function_method_index[cu_idx]);
2006 m_function_selector_index.Append(function_selector_index[cu_idx]);
2007 m_objc_class_selectors_index.Append(objc_class_selectors_index[cu_idx]);
2008 m_global_index.Append(global_index[cu_idx]);
2009 m_type_index.Append(type_index[cu_idx]);
2010 m_namespace_index.Append(namespace_index[cu_idx]);
2011 }
2012
2013 TaskPool::RunTasks([&]() { m_function_basename_index.Finalize(); },
2014 [&]() { m_function_fullname_index.Finalize(); },
2015 [&]() { m_function_method_index.Finalize(); },
2016 [&]() { m_function_selector_index.Finalize(); },
2017 [&]() { m_objc_class_selectors_index.Finalize(); },
2018 [&]() { m_global_index.Finalize(); },
2019 [&]() { m_type_index.Finalize(); },
2020 [&]() { m_namespace_index.Finalize(); });
2021
2022 //----------------------------------------------------------------------
2023 // Keep memory down by clearing DIEs for any compile units if indexing
2024 // caused us to load the compile unit's DIEs.
2025 //----------------------------------------------------------------------
2026 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2027 if (clear_cu_dies[cu_idx])
2028 debug_info->GetCompileUnitAtIndex(cu_idx)->ClearDIEs(true);
2029 }
2030
2031#if defined(ENABLE_DEBUG_PRINTF)
2032 StreamFile s(stdout, false);
2033 s.Printf("DWARF index for '%s':",
2034 GetObjectFile()->GetFileSpec().GetPath().c_str());
2035 s.Printf("\nFunction basenames:\n");
2036 m_function_basename_index.Dump(&s);
2037 s.Printf("\nFunction fullnames:\n");
2038 m_function_fullname_index.Dump(&s);
2039 s.Printf("\nFunction methods:\n");
2040 m_function_method_index.Dump(&s);
2041 s.Printf("\nFunction selectors:\n");
2042 m_function_selector_index.Dump(&s);
2043 s.Printf("\nObjective C class selectors:\n");
2044 m_objc_class_selectors_index.Dump(&s);
2045 s.Printf("\nGlobals and statics:\n");
2046 m_global_index.Dump(&s);
2047 s.Printf("\nTypes:\n");
2048 m_type_index.Dump(&s);
Alexander Shaposhnikov3666e9c2016-11-29 00:40:13 +00002049 s.Printf("\nNamespaces:\n");
2050 m_namespace_index.Dump(&s);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051#endif
2052 }
2053}
2054
2055bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2056 const lldb_private::CompilerDeclContext *decl_ctx) {
2057 if (decl_ctx == nullptr || !decl_ctx->IsValid()) {
2058 // Invalid namespace decl which means we aren't matching only things
2059 // in this symbol file, so return true to indicate it matches this
2060 // symbol file.
2061 return true;
2062 }
2063
2064 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2065 TypeSystem *type_system = GetTypeSystemForLanguage(
2066 decl_ctx_type_system->GetMinimumLanguage(nullptr));
2067 if (decl_ctx_type_system == type_system)
2068 return true; // The type systems match, return true
2069
2070 // The namespace AST was valid, and it does not match...
2071 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2072
2073 if (log)
2074 GetObjectFile()->GetModule()->LogMessage(
2075 log, "Valid namespace does not match symbol file");
2076
2077 return false;
2078}
2079
2080uint32_t SymbolFileDWARF::FindGlobalVariables(
2081 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
2082 bool append, uint32_t max_matches, VariableList &variables) {
2083 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2084
2085 if (log)
2086 GetObjectFile()->GetModule()->LogMessage(
2087 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2088 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
2089 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2090 max_matches);
2091
2092 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2093 return 0;
2094
2095 DWARFDebugInfo *info = DebugInfo();
2096 if (info == NULL)
2097 return 0;
2098
2099 // If we aren't appending the results to this list, then clear the list
2100 if (!append)
2101 variables.Clear();
2102
2103 // Remember how many variables are in the list before we search in case
2104 // we are appending the results to a variable list.
2105 const uint32_t original_size = variables.GetSize();
2106
2107 DIEArray die_offsets;
2108
2109 if (m_using_apple_tables) {
2110 if (m_apple_names_ap.get()) {
2111 const char *name_cstr = name.GetCString();
2112 llvm::StringRef basename;
2113 llvm::StringRef context;
2114
2115 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
2116 basename))
2117 basename = name_cstr;
2118
2119 m_apple_names_ap->FindByName(basename.data(), die_offsets);
2120 }
2121 } else {
2122 // Index the DWARF if we haven't already
2123 if (!m_indexed)
2124 Index();
2125
2126 m_global_index.Find(name, die_offsets);
2127 }
2128
2129 const size_t num_die_matches = die_offsets.size();
2130 if (num_die_matches) {
2131 SymbolContext sc;
2132 sc.module_sp = m_obj_file->GetModule();
2133 assert(sc.module_sp);
2134
2135 bool done = false;
2136 for (size_t i = 0; i < num_die_matches && !done; ++i) {
2137 const DIERef &die_ref = die_offsets[i];
2138 DWARFDIE die = GetDIE(die_ref);
2139
2140 if (die) {
2141 switch (die.Tag()) {
2142 default:
2143 case DW_TAG_subprogram:
2144 case DW_TAG_inlined_subroutine:
2145 case DW_TAG_try_block:
2146 case DW_TAG_catch_block:
2147 break;
2148
2149 case DW_TAG_variable: {
2150 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2151
2152 if (parent_decl_ctx) {
2153 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2154 if (dwarf_ast) {
2155 CompilerDeclContext actual_parent_decl_ctx =
2156 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2157 if (!actual_parent_decl_ctx ||
2158 actual_parent_decl_ctx != *parent_decl_ctx)
2159 continue;
2160 }
2161 }
2162
2163 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false,
2164 &variables);
2165
2166 if (variables.GetSize() - original_size >= max_matches)
2167 done = true;
2168 } break;
2169 }
2170 } else {
2171 if (m_using_apple_tables) {
2172 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2173 "the DWARF debug information has been modified (.apple_names "
2174 "accelerator table had bad die 0x%8.8x for '%s')\n",
2175 die_ref.die_offset, name.GetCString());
2176 }
2177 }
2178 }
2179 }
2180
2181 // Return the number of variable that were appended to the list
2182 const uint32_t num_matches = variables.GetSize() - original_size;
2183 if (log && num_matches > 0) {
2184 GetObjectFile()->GetModule()->LogMessage(
2185 log, "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2186 "parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
2187 name.GetCString(), static_cast<const void *>(parent_decl_ctx), append,
2188 max_matches, num_matches);
2189 }
2190 return num_matches;
2191}
2192
2193uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2194 bool append, uint32_t max_matches,
2195 VariableList &variables) {
2196 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2197
2198 if (log) {
2199 GetObjectFile()->GetModule()->LogMessage(
2200 log, "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, "
2201 "max_matches=%u, variables)",
Zachary Turner95eae422016-09-21 16:01:28 +00002202 regex.GetText().str().c_str(), append, max_matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002203 }
2204
2205 DWARFDebugInfo *info = DebugInfo();
2206 if (info == NULL)
2207 return 0;
2208
2209 // If we aren't appending the results to this list, then clear the list
2210 if (!append)
2211 variables.Clear();
2212
2213 // Remember how many variables are in the list before we search in case
2214 // we are appending the results to a variable list.
2215 const uint32_t original_size = variables.GetSize();
2216
2217 DIEArray die_offsets;
2218
2219 if (m_using_apple_tables) {
2220 if (m_apple_names_ap.get()) {
2221 DWARFMappedHash::DIEInfoArray hash_data_array;
2222 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex(regex,
2223 hash_data_array))
2224 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2225 }
2226 } else {
2227 // Index the DWARF if we haven't already
2228 if (!m_indexed)
2229 Index();
2230
2231 m_global_index.Find(regex, die_offsets);
2232 }
2233
2234 SymbolContext sc;
2235 sc.module_sp = m_obj_file->GetModule();
2236 assert(sc.module_sp);
2237
2238 const size_t num_matches = die_offsets.size();
2239 if (num_matches) {
2240 for (size_t i = 0; i < num_matches; ++i) {
2241 const DIERef &die_ref = die_offsets[i];
2242 DWARFDIE die = GetDIE(die_ref);
2243
2244 if (die) {
2245 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
2246
2247 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2248
2249 if (variables.GetSize() - original_size >= max_matches)
2250 break;
2251 } else {
2252 if (m_using_apple_tables) {
2253 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2254 "the DWARF debug information has been modified (.apple_names "
2255 "accelerator table had bad die 0x%8.8x for regex '%s')\n",
Zachary Turner95eae422016-09-21 16:01:28 +00002256 die_ref.die_offset, regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 }
2258 }
2259 }
2260 }
2261
2262 // Return the number of variable that were appended to the list
2263 return variables.GetSize() - original_size;
2264}
2265
2266bool SymbolFileDWARF::ResolveFunction(const DIERef &die_ref,
2267 bool include_inlines,
2268 SymbolContextList &sc_list) {
2269 DWARFDIE die = DebugInfo()->GetDIE(die_ref);
2270 return ResolveFunction(die, include_inlines, sc_list);
2271}
2272
2273bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2274 bool include_inlines,
2275 SymbolContextList &sc_list) {
2276 SymbolContext sc;
2277
2278 if (!orig_die)
2279 return false;
2280
2281 // If we were passed a die that is not a function, just return false...
2282 if (!(orig_die.Tag() == DW_TAG_subprogram ||
2283 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2284 return false;
2285
2286 DWARFDIE die = orig_die;
2287 DWARFDIE inlined_die;
2288 if (die.Tag() == DW_TAG_inlined_subroutine) {
2289 inlined_die = die;
2290
2291 while (1) {
2292 die = die.GetParent();
2293
2294 if (die) {
2295 if (die.Tag() == DW_TAG_subprogram)
2296 break;
2297 } else
2298 break;
2299 }
2300 }
2301 assert(die && die.Tag() == DW_TAG_subprogram);
2302 if (GetFunction(die, sc)) {
2303 Address addr;
2304 // Parse all blocks if needed
2305 if (inlined_die) {
2306 Block &function_block = sc.function->GetBlock(true);
2307 sc.block = function_block.FindBlockByID(inlined_die.GetID());
2308 if (sc.block == NULL)
2309 sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2310 if (sc.block == NULL || sc.block->GetStartAddress(addr) == false)
2311 addr.Clear();
2312 } else {
2313 sc.block = NULL;
2314 addr = sc.function->GetAddressRange().GetBaseAddress();
2315 }
2316
2317 if (addr.IsValid()) {
2318 sc_list.Append(sc);
2319 return true;
2320 }
2321 }
2322
2323 return false;
2324}
2325
2326void SymbolFileDWARF::FindFunctions(const ConstString &name,
2327 const NameToDIE &name_to_die,
2328 bool include_inlines,
2329 SymbolContextList &sc_list) {
2330 DIEArray die_offsets;
2331 if (name_to_die.Find(name, die_offsets)) {
2332 ParseFunctions(die_offsets, include_inlines, sc_list);
2333 }
2334}
2335
2336void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2337 const NameToDIE &name_to_die,
2338 bool include_inlines,
2339 SymbolContextList &sc_list) {
2340 DIEArray die_offsets;
2341 if (name_to_die.Find(regex, die_offsets)) {
2342 ParseFunctions(die_offsets, include_inlines, sc_list);
2343 }
2344}
2345
2346void SymbolFileDWARF::FindFunctions(
2347 const RegularExpression &regex,
2348 const DWARFMappedHash::MemoryTable &memory_table, bool include_inlines,
2349 SymbolContextList &sc_list) {
2350 DIEArray die_offsets;
2351 DWARFMappedHash::DIEInfoArray hash_data_array;
2352 if (memory_table.AppendAllDIEsThatMatchingRegex(regex, hash_data_array)) {
2353 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
2354 ParseFunctions(die_offsets, include_inlines, sc_list);
2355 }
2356}
2357
2358void SymbolFileDWARF::ParseFunctions(const DIEArray &die_offsets,
2359 bool include_inlines,
2360 SymbolContextList &sc_list) {
2361 const size_t num_matches = die_offsets.size();
2362 if (num_matches) {
2363 for (size_t i = 0; i < num_matches; ++i)
2364 ResolveFunction(die_offsets[i], include_inlines, sc_list);
2365 }
2366}
2367
2368bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx,
2369 const DWARFDIE &die) {
2370 // If we have no parent decl context to match this DIE matches, and if the
2371 // parent
2372 // decl context isn't valid, we aren't trying to look for any particular decl
2373 // context so any die matches.
2374 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2375 return true;
2376
2377 if (die) {
2378 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2379 if (dwarf_ast) {
2380 CompilerDeclContext actual_decl_ctx =
2381 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2382 if (actual_decl_ctx)
2383 return actual_decl_ctx == *decl_ctx;
2384 }
2385 }
2386 return false;
2387}
2388
2389uint32_t
2390SymbolFileDWARF::FindFunctions(const ConstString &name,
2391 const CompilerDeclContext *parent_decl_ctx,
2392 uint32_t name_type_mask, bool include_inlines,
2393 bool append, SymbolContextList &sc_list) {
2394 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
2395 "SymbolFileDWARF::FindFunctions (name = '%s')",
2396 name.AsCString());
2397
2398 // eFunctionNameTypeAuto should be pre-resolved by a call to
Dawn Perchik9d9474ba2016-09-30 20:38:33 +00002399 // Module::LookupInfo::LookupInfo()
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2401
2402 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2403
2404 if (log) {
2405 GetObjectFile()->GetModule()->LogMessage(
2406 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2407 "name_type_mask=0x%x, append=%u, sc_list)",
2408 name.GetCString(), name_type_mask, append);
2409 }
2410
2411 // If we aren't appending the results to this list, then clear the list
2412 if (!append)
2413 sc_list.Clear();
2414
2415 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2416 return 0;
2417
2418 // If name is empty then we won't find anything.
2419 if (name.IsEmpty())
2420 return 0;
2421
2422 // Remember how many sc_list are in the list before we search in case
2423 // we are appending the results to a variable list.
2424
2425 const char *name_cstr = name.GetCString();
2426
2427 const uint32_t original_size = sc_list.GetSize();
2428
2429 DWARFDebugInfo *info = DebugInfo();
2430 if (info == NULL)
2431 return 0;
2432
2433 std::set<const DWARFDebugInfoEntry *> resolved_dies;
2434 if (m_using_apple_tables) {
2435 if (m_apple_names_ap.get()) {
2436
2437 DIEArray die_offsets;
2438
2439 uint32_t num_matches = 0;
2440
2441 if (name_type_mask & eFunctionNameTypeFull) {
2442 // If they asked for the full name, match what they typed. At some
2443 // point we may
2444 // want to canonicalize this (strip double spaces, etc. For now, we
2445 // just add all the
2446 // dies that we find by exact match.
2447 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2448 for (uint32_t i = 0; i < num_matches; i++) {
2449 const DIERef &die_ref = die_offsets[i];
2450 DWARFDIE die = info->GetDIE(die_ref);
2451 if (die) {
2452 if (!DIEInDeclContext(parent_decl_ctx, die))
2453 continue; // The containing decl contexts don't match
2454
2455 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2456 if (ResolveFunction(die, include_inlines, sc_list))
2457 resolved_dies.insert(die.GetDIE());
2458 }
2459 } else {
2460 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2461 "the DWARF debug information has been modified (.apple_names "
2462 "accelerator table had bad die 0x%8.8x for '%s')",
2463 die_ref.die_offset, name_cstr);
2464 }
2465 }
2466 }
2467
2468 if (name_type_mask & eFunctionNameTypeSelector) {
2469 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2470 return 0; // no selectors in namespaces
2471
2472 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2473 // Now make sure these are actually ObjC methods. In this case we can
2474 // simply look up the name,
2475 // and if it is an ObjC method name, we're good.
2476
2477 for (uint32_t i = 0; i < num_matches; i++) {
2478 const DIERef &die_ref = die_offsets[i];
2479 DWARFDIE die = info->GetDIE(die_ref);
2480 if (die) {
2481 const char *die_name = die.GetName();
2482 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) {
2483 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2484 if (ResolveFunction(die, include_inlines, sc_list))
2485 resolved_dies.insert(die.GetDIE());
2486 }
2487 }
2488 } else {
2489 GetObjectFile()->GetModule()->ReportError(
2490 "the DWARF debug information has been modified (.apple_names "
2491 "accelerator table had bad die 0x%8.8x for '%s')",
2492 die_ref.die_offset, name_cstr);
2493 }
2494 }
2495 die_offsets.clear();
2496 }
2497
2498 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) ||
2499 name_type_mask & eFunctionNameTypeBase) {
2500 // The apple_names table stores just the "base name" of C++ methods in
2501 // the table. So we have to
2502 // extract the base name, look that up, and if there is any other
2503 // information in the name we were
2504 // passed in we have to post-filter based on that.
2505
2506 // FIXME: Arrange the logic above so that we don't calculate the base
2507 // name twice:
2508 num_matches = m_apple_names_ap->FindByName(name_cstr, die_offsets);
2509
2510 for (uint32_t i = 0; i < num_matches; i++) {
2511 const DIERef &die_ref = die_offsets[i];
2512 DWARFDIE die = info->GetDIE(die_ref);
2513 if (die) {
2514 if (!DIEInDeclContext(parent_decl_ctx, die))
2515 continue; // The containing decl contexts don't match
2516
2517 // If we get to here, the die is good, and we should add it:
2518 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() &&
2519 ResolveFunction(die, include_inlines, sc_list)) {
2520 bool keep_die = true;
2521 if ((name_type_mask &
2522 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) !=
2523 (eFunctionNameTypeBase | eFunctionNameTypeMethod)) {
2524 // We are looking for either basenames or methods, so we need to
2525 // trim out the ones we won't want by looking at the type
2526 SymbolContext sc;
2527 if (sc_list.GetLastContext(sc)) {
2528 if (sc.block) {
2529 // We have an inlined function
2530 } else if (sc.function) {
2531 Type *type = sc.function->GetType();
2532
2533 if (type) {
2534 CompilerDeclContext decl_ctx =
2535 GetDeclContextContainingUID(type->GetID());
2536 if (decl_ctx.IsStructUnionOrClass()) {
2537 if (name_type_mask & eFunctionNameTypeBase) {
2538 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2539 keep_die = false;
2540 }
2541 } else {
2542 if (name_type_mask & eFunctionNameTypeMethod) {
2543 sc_list.RemoveContextAtIndex(sc_list.GetSize() - 1);
2544 keep_die = false;
2545 }
2546 }
2547 } else {
2548 GetObjectFile()->GetModule()->ReportWarning(
2549 "function at die offset 0x%8.8x had no function type",
2550 die_ref.die_offset);
2551 }
2552 }
2553 }
2554 }
2555 if (keep_die)
2556 resolved_dies.insert(die.GetDIE());
2557 }
2558 } else {
2559 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2560 "the DWARF debug information has been modified (.apple_names "
2561 "accelerator table had bad die 0x%8.8x for '%s')",
2562 die_ref.die_offset, name_cstr);
2563 }
2564 }
2565 die_offsets.clear();
2566 }
2567 }
2568 } else {
2569
2570 // Index the DWARF if we haven't already
2571 if (!m_indexed)
2572 Index();
2573
2574 if (name_type_mask & eFunctionNameTypeFull) {
2575 FindFunctions(name, m_function_fullname_index, include_inlines, sc_list);
2576
2577 // FIXME Temporary workaround for global/anonymous namespace
2578 // functions debugging FreeBSD and Linux binaries.
2579 // If we didn't find any functions in the global namespace try
2580 // looking in the basename index but ignore any returned
2581 // functions that have a namespace but keep functions which
2582 // have an anonymous namespace
2583 // TODO: The arch in the object file isn't correct for MSVC
2584 // binaries on windows, we should find a way to make it
2585 // correct and handle those symbols as well.
2586 if (sc_list.GetSize() == original_size) {
2587 ArchSpec arch;
2588 if (!parent_decl_ctx && GetObjectFile()->GetArchitecture(arch) &&
Alexander Shaposhnikova08ba422016-12-05 18:42:21 +00002589 arch.GetTriple().isOSBinFormatELF()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 SymbolContextList temp_sc_list;
2591 FindFunctions(name, m_function_basename_index, include_inlines,
2592 temp_sc_list);
2593 SymbolContext sc;
2594 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) {
2595 if (temp_sc_list.GetContextAtIndex(i, sc)) {
2596 ConstString mangled_name =
2597 sc.GetFunctionName(Mangled::ePreferMangled);
2598 ConstString demangled_name =
2599 sc.GetFunctionName(Mangled::ePreferDemangled);
2600 // Mangled names on Linux and FreeBSD are of the form:
2601 // _ZN18function_namespace13function_nameEv.
2602 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2603 !strncmp(demangled_name.GetCString(), "(anonymous namespace)",
2604 21)) {
2605 sc_list.Append(sc);
2606 }
2607 }
2608 }
2609 }
2610 }
2611 }
2612 DIEArray die_offsets;
2613 if (name_type_mask & eFunctionNameTypeBase) {
2614 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
2615 for (uint32_t i = 0; i < num_base; i++) {
2616 DWARFDIE die = info->GetDIE(die_offsets[i]);
2617 if (die) {
2618 if (!DIEInDeclContext(parent_decl_ctx, die))
2619 continue; // The containing decl contexts don't match
2620
2621 // If we get to here, the die is good, and we should add it:
2622 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2623 if (ResolveFunction(die, include_inlines, sc_list))
2624 resolved_dies.insert(die.GetDIE());
2625 }
2626 }
2627 }
2628 die_offsets.clear();
2629 }
2630
2631 if (name_type_mask & eFunctionNameTypeMethod) {
2632 if (parent_decl_ctx && parent_decl_ctx->IsValid())
2633 return 0; // no methods in namespaces
2634
2635 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
2636 {
2637 for (uint32_t i = 0; i < num_base; i++) {
2638 DWARFDIE die = info->GetDIE(die_offsets[i]);
2639 if (die) {
2640 // If we get to here, the die is good, and we should add it:
2641 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) {
2642 if (ResolveFunction(die, include_inlines, sc_list))
2643 resolved_dies.insert(die.GetDIE());
2644 }
2645 }
2646 }
2647 }
2648 die_offsets.clear();
2649 }
2650
2651 if ((name_type_mask & eFunctionNameTypeSelector) &&
2652 (!parent_decl_ctx || !parent_decl_ctx->IsValid())) {
2653 FindFunctions(name, m_function_selector_index, include_inlines, sc_list);
2654 }
2655 }
2656
2657 // Return the number of variable that were appended to the list
2658 const uint32_t num_matches = sc_list.GetSize() - original_size;
2659
2660 if (log && num_matches > 0) {
2661 GetObjectFile()->GetModule()->LogMessage(
2662 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2663 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => "
2664 "%u",
2665 name.GetCString(), name_type_mask, include_inlines, append,
2666 num_matches);
2667 }
2668 return num_matches;
2669}
2670
2671uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2672 bool include_inlines, bool append,
2673 SymbolContextList &sc_list) {
2674 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
2675 "SymbolFileDWARF::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00002676 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677
2678 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2679
2680 if (log) {
2681 GetObjectFile()->GetModule()->LogMessage(
2682 log,
2683 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
Zachary Turner95eae422016-09-21 16:01:28 +00002684 regex.GetText().str().c_str(), append);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002685 }
2686
2687 // If we aren't appending the results to this list, then clear the list
2688 if (!append)
2689 sc_list.Clear();
2690
2691 // Remember how many sc_list are in the list before we search in case
2692 // we are appending the results to a variable list.
2693 uint32_t original_size = sc_list.GetSize();
2694
2695 if (m_using_apple_tables) {
2696 if (m_apple_names_ap.get())
2697 FindFunctions(regex, *m_apple_names_ap, include_inlines, sc_list);
2698 } else {
2699 // Index the DWARF if we haven't already
2700 if (!m_indexed)
2701 Index();
2702
2703 FindFunctions(regex, m_function_basename_index, include_inlines, sc_list);
2704
2705 FindFunctions(regex, m_function_fullname_index, include_inlines, sc_list);
2706 }
2707
2708 // Return the number of variable that were appended to the list
2709 return sc_list.GetSize() - original_size;
2710}
2711
2712void SymbolFileDWARF::GetMangledNamesForFunction(
2713 const std::string &scope_qualified_name,
2714 std::vector<ConstString> &mangled_names) {
2715 DWARFDebugInfo *info = DebugInfo();
2716 uint32_t num_comp_units = 0;
2717 if (info)
2718 num_comp_units = info->GetNumCompileUnits();
2719
2720 for (uint32_t i = 0; i < num_comp_units; i++) {
2721 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2722 if (cu == nullptr)
2723 continue;
2724
2725 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2726 if (dwo)
2727 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2728 }
2729
2730 NameToOffsetMap::iterator iter =
2731 m_function_scope_qualified_name_map.find(scope_qualified_name);
2732 if (iter == m_function_scope_qualified_name_map.end())
2733 return;
2734
2735 DIERefSetSP set_sp = (*iter).second;
2736 std::set<DIERef>::iterator set_iter;
2737 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++) {
2738 DWARFDIE die = DebugInfo()->GetDIE(*set_iter);
2739 mangled_names.push_back(ConstString(die.GetMangledName()));
2740 }
2741}
2742
2743uint32_t SymbolFileDWARF::FindTypes(
2744 const SymbolContext &sc, const ConstString &name,
2745 const CompilerDeclContext *parent_decl_ctx, bool append,
2746 uint32_t max_matches,
2747 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2748 TypeMap &types) {
2749 // If we aren't appending the results to this list, then clear the list
2750 if (!append)
2751 types.Clear();
2752
2753 // Make sure we haven't already searched this SymbolFile before...
2754 if (searched_symbol_files.count(this))
2755 return 0;
2756 else
2757 searched_symbol_files.insert(this);
2758
2759 DWARFDebugInfo *info = DebugInfo();
2760 if (info == NULL)
2761 return 0;
2762
2763 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2764
2765 if (log) {
2766 if (parent_decl_ctx)
2767 GetObjectFile()->GetModule()->LogMessage(
2768 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2769 "%p (\"%s\"), append=%u, max_matches=%u, type_list)",
2770 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2771 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches);
2772 else
2773 GetObjectFile()->GetModule()->LogMessage(
2774 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2775 "NULL, append=%u, max_matches=%u, type_list)",
2776 name.GetCString(), append, max_matches);
2777 }
2778
2779 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2780 return 0;
2781
2782 DIEArray die_offsets;
2783
2784 if (m_using_apple_tables) {
2785 if (m_apple_types_ap.get()) {
2786 const char *name_cstr = name.GetCString();
2787 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2788 }
2789 } else {
2790 if (!m_indexed)
2791 Index();
2792
2793 m_type_index.Find(name, die_offsets);
2794 }
2795
2796 const size_t num_die_matches = die_offsets.size();
2797
2798 if (num_die_matches) {
2799 const uint32_t initial_types_size = types.GetSize();
2800 for (size_t i = 0; i < num_die_matches; ++i) {
2801 const DIERef &die_ref = die_offsets[i];
2802 DWARFDIE die = GetDIE(die_ref);
2803
2804 if (die) {
2805 if (!DIEInDeclContext(parent_decl_ctx, die))
2806 continue; // The containing decl contexts don't match
2807
2808 Type *matching_type = ResolveType(die, true, true);
2809 if (matching_type) {
2810 // We found a type pointer, now find the shared pointer form our type
2811 // list
2812 types.InsertUnique(matching_type->shared_from_this());
2813 if (types.GetSize() >= max_matches)
2814 break;
2815 }
2816 } else {
2817 if (m_using_apple_tables) {
2818 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2819 "the DWARF debug information has been modified (.apple_types "
2820 "accelerator table had bad die 0x%8.8x for '%s')\n",
2821 die_ref.die_offset, name.GetCString());
2822 }
2823 }
2824 }
2825 const uint32_t num_matches = types.GetSize() - initial_types_size;
2826 if (log && num_matches) {
2827 if (parent_decl_ctx) {
2828 GetObjectFile()->GetModule()->LogMessage(
2829 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2830 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
2831 name.GetCString(), static_cast<const void *>(parent_decl_ctx),
2832 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches,
2833 num_matches);
2834 } else {
2835 GetObjectFile()->GetModule()->LogMessage(
2836 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2837 "= NULL, append=%u, max_matches=%u, type_list) => %u",
2838 name.GetCString(), append, max_matches, num_matches);
2839 }
2840 }
2841 return num_matches;
2842 } else {
2843 UpdateExternalModuleListIfNeeded();
2844
2845 for (const auto &pair : m_external_type_modules) {
2846 ModuleSP external_module_sp = pair.second;
2847 if (external_module_sp) {
2848 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
2849 if (sym_vendor) {
2850 const uint32_t num_external_matches =
2851 sym_vendor->FindTypes(sc, name, parent_decl_ctx, append,
2852 max_matches, searched_symbol_files, types);
2853 if (num_external_matches)
2854 return num_external_matches;
2855 }
2856 }
2857 }
2858 }
2859
2860 return 0;
2861}
2862
2863size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context,
2864 bool append, TypeMap &types) {
2865 if (!append)
2866 types.Clear();
2867
2868 if (context.empty())
2869 return 0;
2870
2871 DIEArray die_offsets;
2872
2873 ConstString name = context.back().name;
2874
2875 if (!name)
2876 return 0;
2877
2878 if (m_using_apple_tables) {
2879 if (m_apple_types_ap.get()) {
2880 const char *name_cstr = name.GetCString();
2881 m_apple_types_ap->FindByName(name_cstr, die_offsets);
2882 }
2883 } else {
2884 if (!m_indexed)
2885 Index();
2886
2887 m_type_index.Find(name, die_offsets);
2888 }
2889
2890 const size_t num_die_matches = die_offsets.size();
2891
2892 if (num_die_matches) {
2893 size_t num_matches = 0;
2894 for (size_t i = 0; i < num_die_matches; ++i) {
2895 const DIERef &die_ref = die_offsets[i];
2896 DWARFDIE die = GetDIE(die_ref);
2897
2898 if (die) {
2899 std::vector<CompilerContext> die_context;
2900 die.GetDWOContext(die_context);
2901 if (die_context != context)
2902 continue;
2903
2904 Type *matching_type = ResolveType(die, true, true);
2905 if (matching_type) {
2906 // We found a type pointer, now find the shared pointer form our type
2907 // list
2908 types.InsertUnique(matching_type->shared_from_this());
2909 ++num_matches;
2910 }
2911 } else {
2912 if (m_using_apple_tables) {
2913 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2914 "the DWARF debug information has been modified (.apple_types "
2915 "accelerator table had bad die 0x%8.8x for '%s')\n",
2916 die_ref.die_offset, name.GetCString());
2917 }
2918 }
2919 }
2920 return num_matches;
2921 }
2922 return 0;
2923}
2924
2925CompilerDeclContext
2926SymbolFileDWARF::FindNamespace(const SymbolContext &sc, const ConstString &name,
2927 const CompilerDeclContext *parent_decl_ctx) {
2928 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2929
2930 if (log) {
2931 GetObjectFile()->GetModule()->LogMessage(
2932 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2933 name.GetCString());
2934 }
2935
2936 CompilerDeclContext namespace_decl_ctx;
2937
2938 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2939 return namespace_decl_ctx;
2940
2941 DWARFDebugInfo *info = DebugInfo();
2942 if (info) {
2943 DIEArray die_offsets;
2944
2945 // Index if we already haven't to make sure the compile units
2946 // get indexed and make their global DIE index list
2947 if (m_using_apple_tables) {
2948 if (m_apple_namespaces_ap.get()) {
2949 const char *name_cstr = name.GetCString();
2950 m_apple_namespaces_ap->FindByName(name_cstr, die_offsets);
2951 }
2952 } else {
2953 if (!m_indexed)
2954 Index();
2955
2956 m_namespace_index.Find(name, die_offsets);
2957 }
2958
2959 const size_t num_matches = die_offsets.size();
2960 if (num_matches) {
2961 for (size_t i = 0; i < num_matches; ++i) {
2962 const DIERef &die_ref = die_offsets[i];
2963 DWARFDIE die = GetDIE(die_ref);
2964
2965 if (die) {
2966 if (!DIEInDeclContext(parent_decl_ctx, die))
2967 continue; // The containing decl contexts don't match
2968
2969 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2970 if (dwarf_ast) {
2971 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2972 if (namespace_decl_ctx)
2973 break;
2974 }
2975 } else {
2976 if (m_using_apple_tables) {
2977 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
2978 "the DWARF debug information has been modified "
2979 "(.apple_namespaces accelerator table had bad die 0x%8.8x for "
2980 "'%s')\n",
2981 die_ref.die_offset, name.GetCString());
2982 }
2983 }
2984 }
2985 }
2986 }
2987 if (log && namespace_decl_ctx) {
2988 GetObjectFile()->GetModule()->LogMessage(
2989 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2990 "CompilerDeclContext(%p/%p) \"%s\"",
2991 name.GetCString(),
2992 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2993 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2994 namespace_decl_ctx.GetName().AsCString("<NULL>"));
2995 }
2996
2997 return namespace_decl_ctx;
2998}
2999
3000TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
3001 bool resolve_function_context) {
3002 TypeSP type_sp;
3003 if (die) {
3004 Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
3005 if (type_ptr == NULL) {
3006 CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
3007 assert(lldb_cu);
3008 SymbolContext sc(lldb_cu);
3009 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
3010 while (parent_die != nullptr) {
3011 if (parent_die->Tag() == DW_TAG_subprogram)
3012 break;
3013 parent_die = parent_die->GetParent();
3014 }
3015 SymbolContext sc_backup = sc;
3016 if (resolve_function_context && parent_die != nullptr &&
3017 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
3018 sc = sc_backup;
3019
3020 type_sp = ParseType(sc, die, NULL);
3021 } else if (type_ptr != DIE_IS_BEING_PARSED) {
3022 // Grab the existing type from the master types lists
3023 type_sp = type_ptr->shared_from_this();
3024 }
3025 }
3026 return type_sp;
3027}
3028
3029DWARFDIE
3030SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
3031 if (orig_die) {
3032 DWARFDIE die = orig_die;
3033
3034 while (die) {
3035 // If this is the original DIE that we are searching for a declaration
3036 // for, then don't look in the cache as we don't want our own decl
3037 // context to be our decl context...
3038 if (orig_die != die) {
3039 switch (die.Tag()) {
3040 case DW_TAG_compile_unit:
3041 case DW_TAG_namespace:
3042 case DW_TAG_structure_type:
3043 case DW_TAG_union_type:
3044 case DW_TAG_class_type:
3045 case DW_TAG_lexical_block:
3046 case DW_TAG_subprogram:
3047 return die;
3048
3049 default:
3050 break;
3051 }
3052 }
3053
3054 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3055 if (spec_die) {
3056 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3057 if (decl_ctx_die)
3058 return decl_ctx_die;
3059 }
3060
3061 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3062 if (abs_die) {
3063 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3064 if (decl_ctx_die)
3065 return decl_ctx_die;
3066 }
3067
3068 die = die.GetParent();
3069 }
3070 }
3071 return DWARFDIE();
3072}
3073
3074Symbol *
3075SymbolFileDWARF::GetObjCClassSymbol(const ConstString &objc_class_name) {
3076 Symbol *objc_class_symbol = NULL;
3077 if (m_obj_file) {
3078 Symtab *symtab = m_obj_file->GetSymtab();
3079 if (symtab) {
3080 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
3081 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
3082 Symtab::eVisibilityAny);
3083 }
3084 }
3085 return objc_class_symbol;
3086}
3087
3088// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
3089// they don't
3090// then we can end up looking through all class types for a complete type and
3091// never find
3092// the full definition. We need to know if this attribute is supported, so we
3093// determine
3094// this here and cache th result. We also need to worry about the debug map
3095// DWARF file
3096// if we are doing darwin DWARF in .o file debugging.
3097bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(
3098 DWARFCompileUnit *cu) {
3099 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
3100 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3101 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3102 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3103 else {
3104 DWARFDebugInfo *debug_info = DebugInfo();
3105 const uint32_t num_compile_units = GetNumCompileUnits();
3106 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
3107 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3108 if (dwarf_cu != cu &&
3109 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
3110 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3111 break;
3112 }
3113 }
3114 }
3115 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
3116 GetDebugMapSymfile())
3117 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
3118 }
3119 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Claytonc7f03b62012-01-12 04:33:28 +00003120}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003121
3122// This function can be used when a DIE is found that is a forward declaration
3123// DIE and we want to try and find a type that has the complete definition.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003124TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
3125 const DWARFDIE &die, const ConstString &type_name,
3126 bool must_be_implementation) {
Greg Clayton901c5ca2011-12-03 04:40:03 +00003127
Kate Stoneb9c1b512016-09-06 20:57:50 +00003128 TypeSP type_sp;
3129
3130 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
3131 return type_sp;
3132
3133 DIEArray die_offsets;
3134
3135 if (m_using_apple_tables) {
3136 if (m_apple_types_ap.get()) {
3137 const char *name_cstr = type_name.GetCString();
3138 m_apple_types_ap->FindCompleteObjCClassByName(name_cstr, die_offsets,
3139 must_be_implementation);
3140 }
3141 } else {
3142 if (!m_indexed)
3143 Index();
3144
3145 m_type_index.Find(type_name, die_offsets);
3146 }
3147
3148 const size_t num_matches = die_offsets.size();
3149
3150 if (num_matches) {
3151 for (size_t i = 0; i < num_matches; ++i) {
3152 const DIERef &die_ref = die_offsets[i];
3153 DWARFDIE type_die = GetDIE(die_ref);
3154
3155 if (type_die) {
3156 bool try_resolving_type = false;
3157
3158 // Don't try and resolve the DIE we are looking for with the DIE itself!
3159 if (type_die != die) {
3160 switch (type_die.Tag()) {
3161 case DW_TAG_class_type:
3162 case DW_TAG_structure_type:
3163 try_resolving_type = true;
3164 break;
3165 default:
3166 break;
3167 }
3168 }
3169
3170 if (try_resolving_type) {
3171 if (must_be_implementation &&
3172 type_die.Supports_DW_AT_APPLE_objc_complete_type())
3173 try_resolving_type = type_die.GetAttributeValueAsUnsigned(
3174 DW_AT_APPLE_objc_complete_type, 0);
3175
3176 if (try_resolving_type) {
3177 Type *resolved_type = ResolveType(type_die, false, true);
3178 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3179 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
3180 " (cu 0x%8.8" PRIx64 ")\n",
3181 die.GetID(),
3182 m_obj_file->GetFileSpec().GetFilename().AsCString(
3183 "<Unknown>"),
3184 type_die.GetID(), type_cu->GetID());
3185
3186 if (die)
3187 GetDIEToType()[die.GetDIE()] = resolved_type;
3188 type_sp = resolved_type->shared_from_this();
3189 break;
3190 }
3191 }
3192 }
3193 } else {
3194 if (m_using_apple_tables) {
3195 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3196 "the DWARF debug information has been modified (.apple_types "
3197 "accelerator table had bad die 0x%8.8x for '%s')\n",
3198 die_ref.die_offset, type_name.GetCString());
3199 }
3200 }
3201 }
3202 }
3203 return type_sp;
3204}
Greg Claytona8022fa2012-04-24 21:22:41 +00003205
Greg Clayton80c26302012-02-05 06:12:47 +00003206//----------------------------------------------------------------------
3207// This function helps to ensure that the declaration contexts match for
Kate Stoneb9c1b512016-09-06 20:57:50 +00003208// two different DIEs. Often times debug information will refer to a
Greg Clayton80c26302012-02-05 06:12:47 +00003209// forward declaration of a type (the equivalent of "struct my_struct;".
3210// There will often be a declaration of that type elsewhere that has the
3211// full definition. When we go looking for the full type "my_struct", we
3212// will find one or more matches in the accelerator tables and we will
Kate Stoneb9c1b512016-09-06 20:57:50 +00003213// then need to make sure the type was in the same declaration context
Greg Clayton80c26302012-02-05 06:12:47 +00003214// as the original DIE. This function can efficiently compare two DIEs
3215// and will return true when the declaration context matches, and false
Kate Stoneb9c1b512016-09-06 20:57:50 +00003216// when they don't.
Greg Clayton80c26302012-02-05 06:12:47 +00003217//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003218bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
3219 const DWARFDIE &die2) {
3220 if (die1 == die2)
3221 return true;
Greg Claytona8022fa2012-04-24 21:22:41 +00003222
Kate Stoneb9c1b512016-09-06 20:57:50 +00003223 DWARFDIECollection decl_ctx_1;
3224 DWARFDIECollection decl_ctx_2;
3225 // The declaration DIE stack is a stack of the declaration context
3226 // DIEs all the way back to the compile unit. If a type "T" is
3227 // declared inside a class "B", and class "B" is declared inside
3228 // a class "A" and class "A" is in a namespace "lldb", and the
3229 // namespace is in a compile unit, there will be a stack of DIEs:
3230 //
3231 // [0] DW_TAG_class_type for "B"
3232 // [1] DW_TAG_class_type for "A"
3233 // [2] DW_TAG_namespace for "lldb"
3234 // [3] DW_TAG_compile_unit for the source file.
3235 //
3236 // We grab both contexts and make sure that everything matches
3237 // all the way back to the compiler unit.
3238
3239 // First lets grab the decl contexts for both DIEs
3240 die1.GetDeclContextDIEs(decl_ctx_1);
3241 die2.GetDeclContextDIEs(decl_ctx_2);
3242 // Make sure the context arrays have the same size, otherwise
3243 // we are done
3244 const size_t count1 = decl_ctx_1.Size();
3245 const size_t count2 = decl_ctx_2.Size();
3246 if (count1 != count2)
3247 return false;
3248
3249 // Make sure the DW_TAG values match all the way back up the
3250 // compile unit. If they don't, then we are done.
3251 DWARFDIE decl_ctx_die1;
3252 DWARFDIE decl_ctx_die2;
3253 size_t i;
3254 for (i = 0; i < count1; i++) {
3255 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3256 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3257 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
3258 return false;
3259 }
Greg Clayton890ff562012-02-02 05:48:16 +00003260#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003261
Kate Stoneb9c1b512016-09-06 20:57:50 +00003262 // Make sure the top item in the decl context die array is always
3263 // DW_TAG_compile_unit. If it isn't then something went wrong in
3264 // the DWARFDIE::GetDeclContextDIEs() function...
3265 assert(decl_ctx_1.GetDIEAtIndex(count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003266
Greg Clayton890ff562012-02-02 05:48:16 +00003267#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268 // Always skip the compile unit when comparing by only iterating up to
3269 // "count - 1". Here we compare the names as we go.
3270 for (i = 0; i < count1 - 1; i++) {
3271 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex(i);
3272 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex(i);
3273 const char *name1 = decl_ctx_die1.GetName();
3274 const char *name2 = decl_ctx_die2.GetName();
3275 // If the string was from a DW_FORM_strp, then the pointer will often
3276 // be the same!
3277 if (name1 == name2)
3278 continue;
Greg Clayton5569e642012-02-06 01:44:54 +00003279
Kate Stoneb9c1b512016-09-06 20:57:50 +00003280 // Name pointers are not equal, so only compare the strings
3281 // if both are not NULL.
3282 if (name1 && name2) {
3283 // If the strings don't compare, we are done...
3284 if (strcmp(name1, name2) != 0)
3285 return false;
3286 } else {
3287 // One name was NULL while the other wasn't
3288 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003290 }
3291 // We made it through all of the checks and the declaration contexts
3292 // are equal.
3293 return true;
Greg Clayton890ff562012-02-02 05:48:16 +00003294}
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003295
Kate Stoneb9c1b512016-09-06 20:57:50 +00003296TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
3297 const DWARFDeclContext &dwarf_decl_ctx) {
3298 TypeSP type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003299
Kate Stoneb9c1b512016-09-06 20:57:50 +00003300 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3301 if (dwarf_decl_ctx_count > 0) {
3302 const ConstString type_name(dwarf_decl_ctx[0].name);
3303 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
Greg Claytona8022fa2012-04-24 21:22:41 +00003304
Kate Stoneb9c1b512016-09-06 20:57:50 +00003305 if (type_name) {
3306 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
3307 DWARF_LOG_LOOKUPS));
3308 if (log) {
3309 GetObjectFile()->GetModule()->LogMessage(
3310 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
3311 "s, qualified-name='%s')",
3312 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3313 dwarf_decl_ctx.GetQualifiedName());
3314 }
3315
3316 DIEArray die_offsets;
3317
3318 if (m_using_apple_tables) {
3319 if (m_apple_types_ap.get()) {
3320 const bool has_tag =
3321 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3322 DWARFMappedHash::eAtomTypeTag);
3323 const bool has_qualified_name_hash =
3324 m_apple_types_ap->GetHeader().header_data.ContainsAtom(
3325 DWARFMappedHash::eAtomTypeQualNameHash);
3326 if (has_tag && has_qualified_name_hash) {
3327 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3328 const uint32_t qualified_name_hash =
3329 MappedHash::HashStringUsingDJB(qualified_name);
Greg Claytona8022fa2012-04-24 21:22:41 +00003330 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003331 GetObjectFile()->GetModule()->LogMessage(
3332 log, "FindByNameAndTagAndQualifiedNameHash()");
3333 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash(
3334 type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3335 } else if (has_tag) {
3336 if (log)
3337 GetObjectFile()->GetModule()->LogMessage(log,
3338 "FindByNameAndTag()");
3339 m_apple_types_ap->FindByNameAndTag(type_name.GetCString(), tag,
3340 die_offsets);
3341 } else {
3342 m_apple_types_ap->FindByName(type_name.GetCString(), die_offsets);
3343 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003345 } else {
3346 if (!m_indexed)
3347 Index();
3348
3349 m_type_index.Find(type_name, die_offsets);
3350 }
3351
3352 const size_t num_matches = die_offsets.size();
3353
3354 // Get the type system that we are looking to find a type for. We will use
3355 // this
3356 // to ensure any matches we find are in a language that this type system
3357 // supports
3358 const LanguageType language = dwarf_decl_ctx.GetLanguage();
3359 TypeSystem *type_system = (language == eLanguageTypeUnknown)
3360 ? nullptr
3361 : GetTypeSystemForLanguage(language);
3362
3363 if (num_matches) {
3364 for (size_t i = 0; i < num_matches; ++i) {
3365 const DIERef &die_ref = die_offsets[i];
3366 DWARFDIE type_die = GetDIE(die_ref);
3367
3368 if (type_die) {
3369 // Make sure type_die's langauge matches the type system we are
3370 // looking for.
3371 // We don't want to find a "Foo" type from Java if we are looking
3372 // for a "Foo"
3373 // type for C, C++, ObjC, or ObjC++.
3374 if (type_system &&
3375 !type_system->SupportsLanguage(type_die.GetLanguage()))
3376 continue;
3377 bool try_resolving_type = false;
3378
3379 // Don't try and resolve the DIE we are looking for with the DIE
3380 // itself!
3381 const dw_tag_t type_tag = type_die.Tag();
3382 // Make sure the tags match
3383 if (type_tag == tag) {
3384 // The tags match, lets try resolving this type
3385 try_resolving_type = true;
3386 } else {
3387 // The tags don't match, but we need to watch our for a
3388 // forward declaration for a struct and ("struct foo")
3389 // ends up being a class ("class foo { ... };") or
3390 // vice versa.
3391 switch (type_tag) {
3392 case DW_TAG_class_type:
3393 // We had a "class foo", see if we ended up with a "struct foo {
3394 // ... };"
3395 try_resolving_type = (tag == DW_TAG_structure_type);
3396 break;
3397 case DW_TAG_structure_type:
3398 // We had a "struct foo", see if we ended up with a "class foo {
3399 // ... };"
3400 try_resolving_type = (tag == DW_TAG_class_type);
3401 break;
3402 default:
3403 // Tags don't match, don't event try to resolve
3404 // using this type whose name matches....
3405 break;
3406 }
3407 }
3408
3409 if (try_resolving_type) {
3410 DWARFDeclContext type_dwarf_decl_ctx;
3411 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx);
3412
3413 if (log) {
3414 GetObjectFile()->GetModule()->LogMessage(
3415 log, "SymbolFileDWARF::"
3416 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3417 "qualified-name='%s') trying die=0x%8.8x (%s)",
3418 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3419 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3420 type_dwarf_decl_ctx.GetQualifiedName());
3421 }
3422
3423 // Make sure the decl contexts match all the way up
3424 if (dwarf_decl_ctx == type_dwarf_decl_ctx) {
3425 Type *resolved_type = ResolveType(type_die, false);
3426 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) {
3427 type_sp = resolved_type->shared_from_this();
3428 break;
3429 }
3430 }
3431 } else {
3432 if (log) {
3433 std::string qualified_name;
3434 type_die.GetQualifiedName(qualified_name);
3435 GetObjectFile()->GetModule()->LogMessage(
3436 log, "SymbolFileDWARF::"
3437 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
3438 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
3439 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3440 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
3441 qualified_name.c_str());
3442 }
3443 }
3444 } else {
3445 if (m_using_apple_tables) {
3446 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3447 "the DWARF debug information has been modified (.apple_types "
3448 "accelerator table had bad die 0x%8.8x for '%s')\n",
3449 die_ref.die_offset, type_name.GetCString());
3450 }
3451 }
3452 }
3453 }
Greg Claytona8022fa2012-04-24 21:22:41 +00003454 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003455 }
3456 return type_sp;
Greg Claytona8022fa2012-04-24 21:22:41 +00003457}
3458
Kate Stoneb9c1b512016-09-06 20:57:50 +00003459TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
3460 bool *type_is_new_ptr) {
3461 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003462
Kate Stoneb9c1b512016-09-06 20:57:50 +00003463 if (die) {
3464 TypeSystem *type_system =
3465 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Greg Clayton6071e6f2015-08-26 22:57:51 +00003466
Kate Stoneb9c1b512016-09-06 20:57:50 +00003467 if (type_system) {
3468 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3469 if (dwarf_ast) {
3470 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3471 type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr);
3472 if (type_sp) {
3473 TypeList *type_list = GetTypeList();
3474 if (type_list)
3475 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003476
Kate Stoneb9c1b512016-09-06 20:57:50 +00003477 if (die.Tag() == DW_TAG_subprogram) {
3478 DIERef die_ref = die.GetDIERef();
3479 std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
3480 .GetScopeQualifiedName()
3481 .AsCString(""));
3482 if (scope_qualified_name.size()) {
3483 NameToOffsetMap::iterator iter =
3484 m_function_scope_qualified_name_map.find(
3485 scope_qualified_name);
3486 if (iter != m_function_scope_qualified_name_map.end())
3487 (*iter).second->insert(die_ref);
3488 else {
3489 DIERefSetSP new_set(new std::set<DIERef>);
3490 new_set->insert(die_ref);
3491 m_function_scope_qualified_name_map.emplace(
3492 std::make_pair(scope_qualified_name, new_set));
3493 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003494 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003495 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003497 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003499 }
3500
3501 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003502}
3503
Kate Stoneb9c1b512016-09-06 20:57:50 +00003504size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
3505 const DWARFDIE &orig_die,
3506 bool parse_siblings, bool parse_children) {
3507 size_t types_added = 0;
3508 DWARFDIE die = orig_die;
3509 while (die) {
3510 bool type_is_new = false;
3511 if (ParseType(sc, die, &type_is_new).get()) {
3512 if (type_is_new)
3513 ++types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003515
3516 if (parse_children && die.HasChildren()) {
3517 if (die.Tag() == DW_TAG_subprogram) {
3518 SymbolContext child_sc(sc);
3519 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3520 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
3521 } else
3522 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
3523 }
3524
3525 if (parse_siblings)
3526 die = die.GetSibling();
3527 else
3528 die.Clear();
3529 }
3530 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003531}
3532
Kate Stoneb9c1b512016-09-06 20:57:50 +00003533size_t SymbolFileDWARF::ParseFunctionBlocks(const SymbolContext &sc) {
3534 assert(sc.comp_unit && sc.function);
3535 size_t functions_added = 0;
3536 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3537 if (dwarf_cu) {
3538 const dw_offset_t function_die_offset = sc.function->GetID();
3539 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset);
3540 if (function_die) {
3541 ParseFunctionBlocks(sc, &sc.function->GetBlock(false), function_die,
3542 LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003543 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003544 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003545
Kate Stoneb9c1b512016-09-06 20:57:50 +00003546 return functions_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547}
3548
Kate Stoneb9c1b512016-09-06 20:57:50 +00003549size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc) {
3550 // At least a compile unit must be valid
3551 assert(sc.comp_unit);
3552 size_t types_added = 0;
3553 DWARFCompileUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
3554 if (dwarf_cu) {
3555 if (sc.function) {
3556 dw_offset_t function_die_offset = sc.function->GetID();
3557 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3558 if (func_die && func_die.HasChildren()) {
3559 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
3560 }
3561 } else {
3562 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3563 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3564 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3565 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003567 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003568
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569 return types_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003570}
3571
Kate Stoneb9c1b512016-09-06 20:57:50 +00003572size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3573 if (sc.comp_unit != NULL) {
3574 DWARFDebugInfo *info = DebugInfo();
3575 if (info == NULL)
3576 return 0;
3577
3578 if (sc.function) {
3579 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID(), this));
3580
3581 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3582 DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3583 if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3584 const size_t num_variables = ParseVariables(
3585 sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3586
3587 // Let all blocks know they have parse all their variables
3588 sc.function->GetBlock(false).SetDidParseVariables(true, true);
3589 return num_variables;
3590 }
3591 } else if (sc.comp_unit) {
3592 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
3593
3594 if (dwarf_cu == NULL)
3595 return 0;
3596
3597 uint32_t vars_added = 0;
3598 VariableListSP variables(sc.comp_unit->GetVariableList(false));
3599
3600 if (variables.get() == NULL) {
3601 variables.reset(new VariableList());
3602 sc.comp_unit->SetVariableList(variables);
3603
3604 DIEArray die_offsets;
3605 if (m_using_apple_tables) {
3606 if (m_apple_names_ap.get()) {
3607 DWARFMappedHash::DIEInfoArray hash_data_array;
3608 if (m_apple_names_ap->AppendAllDIEsInRange(
3609 dwarf_cu->GetOffset(), dwarf_cu->GetNextCompileUnitOffset(),
3610 hash_data_array)) {
3611 DWARFMappedHash::ExtractDIEArray(hash_data_array, die_offsets);
Greg Claytone38a5ed2012-01-05 03:57:59 +00003612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003613 }
3614 } else {
3615 // Index if we already haven't to make sure the compile units
3616 // get indexed and make their global DIE index list
3617 if (!m_indexed)
3618 Index();
3619
3620 m_global_index.FindAllEntriesForCompileUnit(dwarf_cu->GetOffset(),
3621 die_offsets);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003622 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003623
Kate Stoneb9c1b512016-09-06 20:57:50 +00003624 const size_t num_matches = die_offsets.size();
3625 if (num_matches) {
3626 for (size_t i = 0; i < num_matches; ++i) {
3627 const DIERef &die_ref = die_offsets[i];
3628 DWARFDIE die = GetDIE(die_ref);
3629 if (die) {
3630 VariableSP var_sp(
3631 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3632 if (var_sp) {
3633 variables->AddVariableIfUnique(var_sp);
3634 ++vars_added;
3635 }
3636 } else {
3637 if (m_using_apple_tables) {
3638 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
3639 "the DWARF debug information has been modified "
3640 "(.apple_names accelerator table had bad die 0x%8.8x)\n",
3641 die_ref.die_offset);
3642 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003643 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003646 }
3647 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003648 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003649 }
3650 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003651}
3652
Kate Stoneb9c1b512016-09-06 20:57:50 +00003653VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3654 const DWARFDIE &die,
3655 const lldb::addr_t func_low_pc) {
3656 if (die.GetDWARF() != this)
3657 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003658
Kate Stoneb9c1b512016-09-06 20:57:50 +00003659 VariableSP var_sp;
3660 if (!die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 return var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003662
Kate Stoneb9c1b512016-09-06 20:57:50 +00003663 var_sp = GetDIEToVariable()[die.GetDIE()];
3664 if (var_sp)
3665 return var_sp; // Already been parsed!
Greg Claytonc662ec82011-06-17 22:10:16 +00003666
Kate Stoneb9c1b512016-09-06 20:57:50 +00003667 const dw_tag_t tag = die.Tag();
3668 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytonc662ec82011-06-17 22:10:16 +00003669
Kate Stoneb9c1b512016-09-06 20:57:50 +00003670 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3671 (tag == DW_TAG_formal_parameter && sc.function)) {
3672 DWARFAttributes attributes;
3673 const size_t num_attributes = die.GetAttributes(attributes);
3674 DWARFDIE spec_die;
3675 if (num_attributes > 0) {
3676 const char *name = NULL;
3677 const char *mangled = NULL;
3678 Declaration decl;
3679 uint32_t i;
3680 DWARFFormValue type_die_form;
3681 DWARFExpression location(die.GetCU());
3682 bool is_external = false;
3683 bool is_artificial = false;
3684 bool location_is_const_value_data = false;
3685 bool has_explicit_location = false;
3686 DWARFFormValue const_value;
3687 Variable::RangeList scope_ranges;
3688 // AccessType accessibility = eAccessNone;
Greg Claytonc662ec82011-06-17 22:10:16 +00003689
Kate Stoneb9c1b512016-09-06 20:57:50 +00003690 for (i = 0; i < num_attributes; ++i) {
3691 dw_attr_t attr = attributes.AttributeAtIndex(i);
3692 DWARFFormValue form_value;
3693
3694 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3695 switch (attr) {
3696 case DW_AT_decl_file:
3697 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3698 form_value.Unsigned()));
3699 break;
3700 case DW_AT_decl_line:
3701 decl.SetLine(form_value.Unsigned());
3702 break;
3703 case DW_AT_decl_column:
3704 decl.SetColumn(form_value.Unsigned());
3705 break;
3706 case DW_AT_name:
3707 name = form_value.AsCString();
3708 break;
3709 case DW_AT_linkage_name:
3710 case DW_AT_MIPS_linkage_name:
3711 mangled = form_value.AsCString();
3712 break;
3713 case DW_AT_type:
3714 type_die_form = form_value;
3715 break;
3716 case DW_AT_external:
3717 is_external = form_value.Boolean();
3718 break;
3719 case DW_AT_const_value:
3720 // If we have already found a DW_AT_location attribute, ignore this
3721 // attribute.
3722 if (!has_explicit_location) {
3723 location_is_const_value_data = true;
3724 // The constant value will be either a block, a data value or a
3725 // string.
3726 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3727 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3728 // Retrieve the value as a block expression.
3729 uint32_t block_offset =
3730 form_value.BlockData() - debug_info_data.GetDataStart();
3731 uint32_t block_length = form_value.Unsigned();
3732 location.CopyOpcodeData(module, debug_info_data, block_offset,
3733 block_length);
3734 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3735 // Retrieve the value as a data expression.
3736 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3737 DWARFFormValue::GetFixedFormSizesForAddressSize(
3738 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3739 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3740 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3741 uint32_t data_length =
3742 fixed_form_sizes.GetSize(form_value.Form());
3743 if (data_length == 0) {
3744 const uint8_t *data_pointer = form_value.BlockData();
3745 if (data_pointer) {
3746 form_value.Unsigned();
3747 } else if (DWARFFormValue::IsDataForm(form_value.Form())) {
3748 // we need to get the byte size of the type later after we
3749 // create the variable
3750 const_value = form_value;
3751 }
3752 } else
3753 location.CopyOpcodeData(module, debug_info_data, data_offset,
3754 data_length);
3755 } else {
3756 // Retrieve the value as a string expression.
3757 if (form_value.Form() == DW_FORM_strp) {
3758 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3759 DWARFFormValue::GetFixedFormSizesForAddressSize(
3760 attributes.CompileUnitAtIndex(i)
3761 ->GetAddressByteSize(),
3762 attributes.CompileUnitAtIndex(i)->IsDWARF64());
3763 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
3764 uint32_t data_length =
3765 fixed_form_sizes.GetSize(form_value.Form());
3766 location.CopyOpcodeData(module, debug_info_data, data_offset,
3767 data_length);
3768 } else {
3769 const char *str = form_value.AsCString();
3770 uint32_t string_offset =
3771 str - (const char *)debug_info_data.GetDataStart();
3772 uint32_t string_length = strlen(str) + 1;
3773 location.CopyOpcodeData(module, debug_info_data,
3774 string_offset, string_length);
3775 }
3776 }
3777 }
3778 break;
3779 case DW_AT_location: {
3780 location_is_const_value_data = false;
3781 has_explicit_location = true;
3782 if (DWARFFormValue::IsBlockForm(form_value.Form())) {
3783 const DWARFDataExtractor &debug_info_data = get_debug_info_data();
3784
3785 uint32_t block_offset =
3786 form_value.BlockData() - debug_info_data.GetDataStart();
3787 uint32_t block_length = form_value.Unsigned();
3788 location.CopyOpcodeData(module, get_debug_info_data(),
3789 block_offset, block_length);
3790 } else {
3791 const DWARFDataExtractor &debug_loc_data = get_debug_loc_data();
3792 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3793
3794 size_t loc_list_length = DWARFExpression::LocationListSize(
3795 die.GetCU(), debug_loc_data, debug_loc_offset);
3796 if (loc_list_length > 0) {
3797 location.CopyOpcodeData(module, debug_loc_data,
3798 debug_loc_offset, loc_list_length);
3799 assert(func_low_pc != LLDB_INVALID_ADDRESS);
3800 location.SetLocationListSlide(
3801 func_low_pc -
3802 attributes.CompileUnitAtIndex(i)->GetBaseAddress());
3803 }
3804 }
3805 } break;
3806 case DW_AT_specification:
3807 spec_die = GetDIE(DIERef(form_value));
3808 break;
3809 case DW_AT_start_scope: {
3810 if (form_value.Form() == DW_FORM_sec_offset) {
3811 DWARFRangeList dwarf_scope_ranges;
3812 const DWARFDebugRanges *debug_ranges = DebugRanges();
Tamas Berghammer2540a552016-09-15 08:53:33 +00003813 debug_ranges->FindRanges(die.GetCU()->GetRangesBase(),
3814 form_value.Unsigned(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003815 dwarf_scope_ranges);
3816
3817 // All DW_AT_start_scope are relative to the base address of the
3818 // compile unit. We add the compile unit base address to make
3819 // sure all the addresses are properly fixed up.
3820 for (size_t i = 0, count = dwarf_scope_ranges.GetSize();
3821 i < count; ++i) {
3822 const DWARFRangeList::Entry &range =
3823 dwarf_scope_ranges.GetEntryRef(i);
3824 scope_ranges.Append(range.GetRangeBase() +
3825 die.GetCU()->GetBaseAddress(),
3826 range.GetByteSize());
3827 }
3828 } else {
3829 // TODO: Handle the case when DW_AT_start_scope have form
3830 // constant. The
3831 // dwarf spec is a bit ambiguous about what is the expected
3832 // behavior in
3833 // case the enclosing block have a non coninious address range and
3834 // the
3835 // DW_AT_start_scope entry have a form constant.
3836 GetObjectFile()->GetModule()->ReportWarning(
3837 "0x%8.8" PRIx64
3838 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
3839 die.GetID(), form_value.Form());
3840 }
3841
3842 scope_ranges.Sort();
3843 scope_ranges.CombineConsecutiveRanges();
3844 } break;
3845 case DW_AT_artificial:
3846 is_artificial = form_value.Boolean();
3847 break;
3848 case DW_AT_accessibility:
3849 break; // accessibility =
3850 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3851 case DW_AT_declaration:
3852 case DW_AT_description:
3853 case DW_AT_endianity:
3854 case DW_AT_segment:
3855 case DW_AT_visibility:
3856 default:
3857 case DW_AT_abstract_origin:
3858 case DW_AT_sibling:
3859 break;
3860 }
3861 }
3862 }
3863
3864 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3865 const dw_tag_t parent_tag = die.GetParent().Tag();
3866 bool is_static_member =
3867 parent_tag == DW_TAG_compile_unit &&
3868 (parent_context_die.Tag() == DW_TAG_class_type ||
3869 parent_context_die.Tag() == DW_TAG_structure_type);
3870
3871 ValueType scope = eValueTypeInvalid;
3872
3873 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3874 SymbolContextScope *symbol_context_scope = NULL;
3875
Sam McCall30621392016-11-22 11:40:25 +00003876 bool has_explicit_mangled = mangled != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003877 if (!mangled) {
3878 // LLDB relies on the mangled name (DW_TAG_linkage_name or
3879 // DW_AT_MIPS_linkage_name) to
3880 // generate fully qualified names of global variables with commands like
3881 // "frame var j".
3882 // For example, if j were an int variable holding a value 4 and declared
3883 // in a namespace
3884 // B which in turn is contained in a namespace A, the command "frame var
3885 // j" returns
3886 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we
3887 // should be able
3888 // to generate a fully qualified name from the declaration context.
3889 if (parent_tag == DW_TAG_compile_unit &&
3890 Language::LanguageIsCPlusPlus(die.GetLanguage())) {
3891 DWARFDeclContext decl_ctx;
3892
3893 die.GetDWARFDeclContext(decl_ctx);
3894 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3895 }
3896 }
3897
Kate Stoneb9c1b512016-09-06 20:57:50 +00003898 if (tag == DW_TAG_formal_parameter)
3899 scope = eValueTypeVariableArgument;
3900 else {
Sam McCall30621392016-11-22 11:40:25 +00003901 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3902 // or static variable, so we have to do a little digging:
3903 // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3904 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3905 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3906 // Clang likes to combine small global variables into the same symbol
3907 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3908 // so we need to look through the whole expression.
3909 bool is_static_lifetime =
3910 has_explicit_mangled ||
3911 (has_explicit_location && !location.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003912 // Check if the location has a DW_OP_addr with any address value...
3913 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3914 if (!location_is_const_value_data) {
Sam McCall30621392016-11-22 11:40:25 +00003915 bool op_error = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003916 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3917 if (op_error) {
3918 StreamString strm;
3919 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3920 NULL);
3921 GetObjectFile()->GetModule()->ReportError(
3922 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
Zachary Turnerc1564272016-11-16 21:15:24 +00003923 die.GetTagAsCString(), strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003924 }
Sam McCall30621392016-11-22 11:40:25 +00003925 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3926 is_static_lifetime = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003927 }
3928 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3929
Sam McCall30621392016-11-22 11:40:25 +00003930 if (is_static_lifetime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003931 if (is_external)
3932 scope = eValueTypeVariableGlobal;
3933 else
3934 scope = eValueTypeVariableStatic;
3935
3936 if (debug_map_symfile) {
3937 // When leaving the DWARF in the .o files on darwin,
3938 // when we have a global variable that wasn't initialized,
3939 // the .o file might not have allocated a virtual
3940 // address for the global variable. In this case it will
3941 // have created a symbol for the global variable
3942 // that is undefined/data and external and the value will
3943 // be the byte size of the variable. When we do the
3944 // address map in SymbolFileDWARFDebugMap we rely on
3945 // having an address, we need to do some magic here
3946 // so we can get the correct address for our global
3947 // variable. The address for all of these entries
3948 // will be zero, and there will be an undefined symbol
3949 // in this object file, and the executable will have
3950 // a matching symbol with a good address. So here we
3951 // dig up the correct address and replace it in the
3952 // location for the variable, and set the variable's
3953 // symbol context scope to be that of the main executable
3954 // so the file address will resolve correctly.
3955 bool linked_oso_file_addr = false;
3956 if (is_external && location_DW_OP_addr == 0) {
3957 // we have a possible uninitialized extern global
3958 ConstString const_name(mangled ? mangled : name);
3959 ObjectFile *debug_map_objfile =
3960 debug_map_symfile->GetObjectFile();
3961 if (debug_map_objfile) {
3962 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3963 if (debug_map_symtab) {
3964 Symbol *exe_symbol =
3965 debug_map_symtab->FindFirstSymbolWithNameAndType(
3966 const_name, eSymbolTypeData, Symtab::eDebugYes,
3967 Symtab::eVisibilityExtern);
3968 if (exe_symbol) {
3969 if (exe_symbol->ValueIsAddress()) {
3970 const addr_t exe_file_addr =
3971 exe_symbol->GetAddressRef().GetFileAddress();
3972 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3973 if (location.Update_DW_OP_addr(exe_file_addr)) {
3974 linked_oso_file_addr = true;
3975 symbol_context_scope = exe_symbol;
3976 }
3977 }
3978 }
3979 }
3980 }
3981 }
3982 }
3983
3984 if (!linked_oso_file_addr) {
3985 // The DW_OP_addr is not zero, but it contains a .o file address
3986 // which
3987 // needs to be linked up correctly.
3988 const lldb::addr_t exe_file_addr =
3989 debug_map_symfile->LinkOSOFileAddress(this,
3990 location_DW_OP_addr);
3991 if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3992 // Update the file address for this variable
3993 location.Update_DW_OP_addr(exe_file_addr);
3994 } else {
3995 // Variable didn't make it into the final executable
3996 return var_sp;
3997 }
3998 }
3999 }
4000 } else {
4001 if (location_is_const_value_data)
4002 scope = eValueTypeVariableStatic;
4003 else {
4004 scope = eValueTypeVariableLocal;
4005 if (debug_map_symfile) {
4006 // We need to check for TLS addresses that we need to fixup
4007 if (location.ContainsThreadLocalStorage()) {
4008 location.LinkThreadLocalStorage(
4009 debug_map_symfile->GetObjectFile()->GetModule(),
4010 [this, debug_map_symfile](
4011 lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
4012 return debug_map_symfile->LinkOSOFileAddress(
4013 this, unlinked_file_addr);
4014 });
4015 scope = eValueTypeVariableThreadLocal;
4016 }
4017 }
4018 }
4019 }
4020 }
4021
4022 if (symbol_context_scope == NULL) {
4023 switch (parent_tag) {
Greg Claytonc662ec82011-06-17 22:10:16 +00004024 case DW_TAG_subprogram:
4025 case DW_TAG_inlined_subroutine:
4026 case DW_TAG_lexical_block:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004027 if (sc.function) {
4028 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(
4029 sc_parent_die.GetID());
4030 if (symbol_context_scope == NULL)
4031 symbol_context_scope = sc.function;
4032 }
4033 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004034
Kate Stoneb9c1b512016-09-06 20:57:50 +00004035 default:
4036 symbol_context_scope = sc.comp_unit;
4037 break;
Greg Claytonc662ec82011-06-17 22:10:16 +00004038 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004039 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004040
Kate Stoneb9c1b512016-09-06 20:57:50 +00004041 if (symbol_context_scope) {
4042 SymbolFileTypeSP type_sp(
4043 new SymbolFileType(*this, DIERef(type_die_form).GetUID(this)));
4044
4045 if (const_value.Form() && type_sp && type_sp->GetType())
4046 location.CopyOpcodeData(const_value.Unsigned(),
4047 type_sp->GetType()->GetByteSize(),
4048 die.GetCU()->GetAddressByteSize());
4049
4050 var_sp.reset(new Variable(die.GetID(), name, mangled, type_sp, scope,
4051 symbol_context_scope, scope_ranges, &decl,
4052 location, is_external, is_artificial,
4053 is_static_member));
4054
4055 var_sp->SetLocationIsConstantValueData(location_is_const_value_data);
4056 } else {
4057 // Not ready to parse this variable yet. It might be a global
4058 // or static variable that is in a function scope and the function
4059 // in the symbol context wasn't filled in yet
4060 return var_sp;
4061 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004063 // Cache var_sp even if NULL (the variable was just a specification or
4064 // was missing vital information to be able to be displayed in the debugger
4065 // (missing location due to optimization, etc)) so we don't re-parse
4066 // this DIE over and over later...
4067 GetDIEToVariable()[die.GetDIE()] = var_sp;
4068 if (spec_die)
4069 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
4070 }
4071 return var_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +00004072}
4073
Kate Stoneb9c1b512016-09-06 20:57:50 +00004074DWARFDIE
4075SymbolFileDWARF::FindBlockContainingSpecification(
4076 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
4077 // Give the concrete function die specified by "func_die_offset", find the
4078 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4079 // to "spec_block_die_offset"
4080 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref),
4081 spec_block_die_offset);
4082}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004083
Kate Stoneb9c1b512016-09-06 20:57:50 +00004084DWARFDIE
4085SymbolFileDWARF::FindBlockContainingSpecification(
4086 const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
4087 if (die) {
4088 switch (die.Tag()) {
4089 case DW_TAG_subprogram:
4090 case DW_TAG_inlined_subroutine:
4091 case DW_TAG_lexical_block: {
4092 if (die.GetAttributeValueAsReference(
4093 DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4094 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004095
Kate Stoneb9c1b512016-09-06 20:57:50 +00004096 if (die.GetAttributeValueAsReference(DW_AT_abstract_origin,
4097 DW_INVALID_OFFSET) ==
4098 spec_block_die_offset)
4099 return die;
4100 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004102
4103 // Give the concrete function die specified by "func_die_offset", find the
4104 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4105 // to "spec_block_die_offset"
4106 for (DWARFDIE child_die = die.GetFirstChild(); child_die;
4107 child_die = child_die.GetSibling()) {
4108 DWARFDIE result_die =
4109 FindBlockContainingSpecification(child_die, spec_block_die_offset);
4110 if (result_die)
4111 return result_die;
4112 }
4113 }
4114
4115 return DWARFDIE();
4116}
4117
4118size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
4119 const DWARFDIE &orig_die,
4120 const lldb::addr_t func_low_pc,
4121 bool parse_siblings, bool parse_children,
4122 VariableList *cc_variable_list) {
4123 if (!orig_die)
4124 return 0;
4125
4126 VariableListSP variable_list_sp;
4127
4128 size_t vars_added = 0;
4129 DWARFDIE die = orig_die;
4130 while (die) {
4131 dw_tag_t tag = die.Tag();
4132
4133 // Check to see if we have already parsed this variable or constant?
4134 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
4135 if (var_sp) {
4136 if (cc_variable_list)
4137 cc_variable_list->AddVariableIfUnique(var_sp);
4138 } else {
4139 // We haven't already parsed it, lets do that now.
4140 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
4141 (tag == DW_TAG_formal_parameter && sc.function)) {
4142 if (variable_list_sp.get() == NULL) {
4143 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4144 dw_tag_t parent_tag = sc_parent_die.Tag();
4145 switch (parent_tag) {
4146 case DW_TAG_compile_unit:
4147 if (sc.comp_unit != NULL) {
4148 variable_list_sp = sc.comp_unit->GetVariableList(false);
4149 if (variable_list_sp.get() == NULL) {
4150 variable_list_sp.reset(new VariableList());
4151 sc.comp_unit->SetVariableList(variable_list_sp);
4152 }
4153 } else {
4154 GetObjectFile()->GetModule()->ReportError(
4155 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
4156 "symbol context for 0x%8.8" PRIx64
4157 " %s.\n",
4158 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
4159 orig_die.GetID(), orig_die.GetTagAsCString());
4160 }
4161 break;
4162
4163 case DW_TAG_subprogram:
4164 case DW_TAG_inlined_subroutine:
4165 case DW_TAG_lexical_block:
4166 if (sc.function != NULL) {
4167 // Check to see if we already have parsed the variables for the
4168 // given scope
4169
4170 Block *block = sc.function->GetBlock(true).FindBlockByID(
4171 sc_parent_die.GetID());
4172 if (block == NULL) {
4173 // This must be a specification or abstract origin with
4174 // a concrete block counterpart in the current function. We need
4175 // to find the concrete block so we can correctly add the
4176 // variable to it
4177 const DWARFDIE concrete_block_die =
4178 FindBlockContainingSpecification(
4179 DIERef(sc.function->GetID(), this),
4180 sc_parent_die.GetOffset());
4181 if (concrete_block_die)
4182 block = sc.function->GetBlock(true).FindBlockByID(
4183 concrete_block_die.GetID());
4184 }
4185
4186 if (block != NULL) {
4187 const bool can_create = false;
4188 variable_list_sp = block->GetBlockVariableList(can_create);
4189 if (variable_list_sp.get() == NULL) {
4190 variable_list_sp.reset(new VariableList());
4191 block->SetVariableList(variable_list_sp);
4192 }
4193 }
4194 }
4195 break;
4196
4197 default:
4198 GetObjectFile()->GetModule()->ReportError(
4199 "didn't find appropriate parent DIE for variable list for "
4200 "0x%8.8" PRIx64 " %s.\n",
4201 orig_die.GetID(), orig_die.GetTagAsCString());
4202 break;
4203 }
4204 }
4205
4206 if (variable_list_sp) {
4207 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
4208 if (var_sp) {
4209 variable_list_sp->AddVariableIfUnique(var_sp);
4210 if (cc_variable_list)
4211 cc_variable_list->AddVariableIfUnique(var_sp);
4212 ++vars_added;
4213 }
4214 }
4215 }
4216 }
4217
4218 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4219
4220 if (!skip_children && parse_children && die.HasChildren()) {
4221 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
4222 true, cc_variable_list);
4223 }
4224
4225 if (parse_siblings)
4226 die = die.GetSibling();
4227 else
4228 die.Clear();
4229 }
4230 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004231}
4232
4233//------------------------------------------------------------------
4234// PluginInterface protocol
4235//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00004236ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
4237
4238uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
4239
4240void SymbolFileDWARF::DumpIndexes() {
4241 StreamFile s(stdout, false);
4242
4243 s.Printf(
4244 "DWARF index for (%s) '%s':",
4245 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4246 GetObjectFile()->GetFileSpec().GetPath().c_str());
4247 s.Printf("\nFunction basenames:\n");
4248 m_function_basename_index.Dump(&s);
4249 s.Printf("\nFunction fullnames:\n");
4250 m_function_fullname_index.Dump(&s);
4251 s.Printf("\nFunction methods:\n");
4252 m_function_method_index.Dump(&s);
4253 s.Printf("\nFunction selectors:\n");
4254 m_function_selector_index.Dump(&s);
4255 s.Printf("\nObjective C class selectors:\n");
4256 m_objc_class_selectors_index.Dump(&s);
4257 s.Printf("\nGlobals and statics:\n");
4258 m_global_index.Dump(&s);
4259 s.Printf("\nTypes:\n");
4260 m_type_index.Dump(&s);
4261 s.Printf("\nNamespaces:\n");
4262 m_namespace_index.Dump(&s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004263}
4264
Kate Stoneb9c1b512016-09-06 20:57:50 +00004265SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
4266 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) {
4267 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
4268 if (module_sp) {
4269 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4270 if (sym_vendor)
4271 m_debug_map_symfile =
4272 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
Greg Clayton1f746072012-08-29 21:13:06 +00004273 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004274 }
4275 return m_debug_map_symfile;
Greg Clayton1f746072012-08-29 21:13:06 +00004276}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004277
4278DWARFExpression::LocationListFormat
Kate Stoneb9c1b512016-09-06 20:57:50 +00004279SymbolFileDWARF::GetLocationListFormat() const {
4280 return DWARFExpression::RegularLocationList;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004281}