blob: 3504172b4e3a447c4f10125271586ca7e2806308 [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
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Robert Flackeb83fab2015-05-15 18:59:59 +000029#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000031#include "lldb/Core/ModuleList.h"
32#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/PluginManager.h"
34#include "lldb/Core/RegularExpression.h"
35#include "lldb/Core/Scalar.h"
36#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000037#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000038#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/Timer.h"
40#include "lldb/Core/Value.h"
41
Sean Callananf0c5aeb2015-04-20 16:31:29 +000042#include "lldb/Expression/ClangModulesDeclVendor.h"
43
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000044#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000045#include "lldb/Host/Host.h"
46
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000047#include "lldb/Interpreter/OptionValueFileSpecList.h"
48#include "lldb/Interpreter/OptionValueProperties.h"
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/Block.h"
51#include "lldb/Symbol/CompileUnit.h"
52#include "lldb/Symbol/LineTable.h"
53#include "lldb/Symbol/ObjectFile.h"
54#include "lldb/Symbol/SymbolVendor.h"
55#include "lldb/Symbol/VariableList.h"
56
Jim Inghamaa816b82015-09-02 01:59:14 +000057#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
58#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000059
Jim Ingham0e0984e2015-09-02 01:06:46 +000060#include "lldb/Target/Language.h"
61
Greg Clayton261ac3f2015-08-28 01:01:03 +000062#include "DWARFASTParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFCompileUnit.h"
64#include "DWARFDebugAbbrev.h"
65#include "DWARFDebugAranges.h"
66#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "DWARFDebugLine.h"
68#include "DWARFDebugPubnames.h"
69#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000070#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071#include "DWARFDIECollection.h"
72#include "DWARFFormValue.h"
73#include "DWARFLocationList.h"
74#include "LogChannelDWARF.h"
Tamas Berghammereb882fc2015-09-09 10:20:48 +000075#include "SymbolFileDWARFDwo.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000076#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
78#include <map>
79
Matthew Gardinere81df3b2014-08-26 06:57:23 +000080#include <ctype.h>
81#include <string.h>
82
Greg Clayton62742b12010-11-11 01:09:45 +000083//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000084
85#ifdef ENABLE_DEBUG_PRINTF
86#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000087#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000088#else
89#define DEBUG_PRINTF(fmt, ...)
90#endif
91
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092using namespace lldb;
93using namespace lldb_private;
94
Greg Clayton219cf312012-03-30 00:51:13 +000095//static inline bool
96//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
97//{
98// switch (tag)
99// {
100// default:
101// break;
102// case DW_TAG_subprogram:
103// case DW_TAG_inlined_subroutine:
104// case DW_TAG_class_type:
105// case DW_TAG_structure_type:
106// case DW_TAG_union_type:
107// return true;
108// }
109// return false;
110//}
111//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000112
113namespace {
114
115 PropertyDefinition
116 g_properties[] =
117 {
118 { "comp-dir-symlink-paths" , OptionValue::eTypeFileSpecList, true, 0 , nullptr, nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic links will be resolved at DWARF parse time." },
119 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
120 };
121
122 enum
123 {
124 ePropertySymLinkPaths
125 };
126
127
128 class PluginProperties : public Properties
129 {
130 public:
131 static ConstString
132 GetSettingName()
133 {
134 return SymbolFileDWARF::GetPluginNameStatic();
135 }
136
137 PluginProperties()
138 {
139 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
140 m_collection_sp->Initialize(g_properties);
141 }
142
143 FileSpecList&
144 GetSymLinkPaths()
145 {
146 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, true, ePropertySymLinkPaths);
147 assert(option_value);
148 return option_value->GetCurrentValue();
149 }
150
151 };
152
153 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
154
155 static const SymbolFileDWARFPropertiesSP&
156 GetGlobalPluginProperties()
157 {
158 static const auto g_settings_sp(std::make_shared<PluginProperties>());
159 return g_settings_sp;
160 }
161
162} // anonymous namespace end
163
164
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000165static const char*
166removeHostnameFromPathname(const char* path_from_dwarf)
167{
168 if (!path_from_dwarf || !path_from_dwarf[0])
169 {
170 return path_from_dwarf;
171 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000172
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000173 const char *colon_pos = strchr(path_from_dwarf, ':');
Enrico Granata99e5e222015-07-27 21:27:02 +0000174 if (nullptr == colon_pos)
175 {
176 return path_from_dwarf;
177 }
178
179 const char *slash_pos = strchr(path_from_dwarf, '/');
180 if (slash_pos && (slash_pos < colon_pos))
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000181 {
182 return path_from_dwarf;
183 }
184
185 // check whether we have a windows path, and so the first character
186 // is a drive-letter not a hostname.
187 if (
188 colon_pos == path_from_dwarf + 1 &&
189 isalpha(*path_from_dwarf) &&
190 strlen(path_from_dwarf) > 2 &&
191 '\\' == path_from_dwarf[2])
192 {
193 return path_from_dwarf;
194 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000195
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000196 return colon_pos + 1;
197}
198
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000199static const char*
200resolveCompDir(const char* path_from_dwarf)
201{
202 if (!path_from_dwarf)
203 return nullptr;
204
205 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
206 // Remove the host part if present.
207 const char* local_path = removeHostnameFromPathname(path_from_dwarf);
208 if (!local_path)
209 return nullptr;
210
211 bool is_symlink = false;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000212 FileSpec local_path_spec(local_path, false);
213 const auto& file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
214 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
215 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i), local_path_spec, true);
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000216
217 if (!is_symlink)
218 return local_path;
219
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000220 if (!local_path_spec.IsSymbolicLink())
221 return local_path;
222
223 FileSpec resolved_local_path_spec;
224 const auto error = FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
225 if (error.Success())
226 return resolved_local_path_spec.GetCString();
227
228 return nullptr;
229}
230
231
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232void
233SymbolFileDWARF::Initialize()
234{
235 LogChannelDWARF::Initialize();
236 PluginManager::RegisterPlugin (GetPluginNameStatic(),
237 GetPluginDescriptionStatic(),
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000238 CreateInstance,
239 DebuggerInitialize);
240}
241
242void
243SymbolFileDWARF::DebuggerInitialize(Debugger &debugger)
244{
245 if (!PluginManager::GetSettingForSymbolFilePlugin(debugger, PluginProperties::GetSettingName()))
246 {
247 const bool is_global_setting = true;
248 PluginManager::CreateSettingForSymbolFilePlugin(debugger,
249 GetGlobalPluginProperties()->GetValueProperties(),
250 ConstString ("Properties for the dwarf symbol-file plug-in."),
251 is_global_setting);
252 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253}
254
255void
256SymbolFileDWARF::Terminate()
257{
258 PluginManager::UnregisterPlugin (CreateInstance);
259 LogChannelDWARF::Initialize();
260}
261
262
Greg Clayton57abc5d2013-05-10 21:47:16 +0000263lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264SymbolFileDWARF::GetPluginNameStatic()
265{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000266 static ConstString g_name("dwarf");
267 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
270const char *
271SymbolFileDWARF::GetPluginDescriptionStatic()
272{
273 return "DWARF and DWARF3 debug symbol file reader.";
274}
275
276
277SymbolFile*
278SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
279{
280 return new SymbolFileDWARF(obj_file);
281}
282
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000283TypeList *
284SymbolFileDWARF::GetTypeList ()
285{
Greg Clayton1f746072012-08-29 21:13:06 +0000286 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000287 return m_debug_map_symfile->GetTypeList();
288 return m_obj_file->GetModule()->GetTypeList();
289
290}
Greg Claytonf02500c2013-06-18 22:51:05 +0000291void
Greg Clayton6071e6f2015-08-26 22:57:51 +0000292SymbolFileDWARF::GetTypes (const DWARFDIE &die,
Greg Claytonf02500c2013-06-18 22:51:05 +0000293 dw_offset_t min_die_offset,
294 dw_offset_t max_die_offset,
295 uint32_t type_mask,
296 TypeSet &type_set)
297{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000298 if (die)
Greg Claytonf02500c2013-06-18 22:51:05 +0000299 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000300 const dw_offset_t die_offset = die.GetOffset();
301
302 if (die_offset >= max_die_offset)
303 return;
304
305 if (die_offset >= min_die_offset)
Greg Claytonf02500c2013-06-18 22:51:05 +0000306 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000307 const dw_tag_t tag = die.Tag();
Greg Claytonf02500c2013-06-18 22:51:05 +0000308
Greg Clayton6071e6f2015-08-26 22:57:51 +0000309 bool add_type = false;
310
311 switch (tag)
Greg Claytonf02500c2013-06-18 22:51:05 +0000312 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000313 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
314 case DW_TAG_unspecified_type:
315 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
316 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
317 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
318 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
319 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
320 case DW_TAG_subroutine_type:
321 case DW_TAG_subprogram:
322 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
323 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
324 case DW_TAG_rvalue_reference_type:
325 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
326 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
327 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
328 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000329
Greg Clayton6071e6f2015-08-26 22:57:51 +0000330 if (add_type)
331 {
332 const bool assert_not_being_parsed = true;
333 Type *type = ResolveTypeUID (die, assert_not_being_parsed);
334 if (type)
Greg Claytonf02500c2013-06-18 22:51:05 +0000335 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000336 if (type_set.find(type) == type_set.end())
337 type_set.insert(type);
Greg Claytonf02500c2013-06-18 22:51:05 +0000338 }
339 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000340 }
341
342 for (DWARFDIE child_die = die.GetFirstChild();
343 child_die.IsValid();
344 child_die = child_die.GetSibling())
345 {
346 GetTypes (child_die, min_die_offset, max_die_offset, type_mask, type_set);
Greg Claytonf02500c2013-06-18 22:51:05 +0000347 }
348 }
349}
350
351size_t
352SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
353 uint32_t type_mask,
354 TypeList &type_list)
355
356{
357 TypeSet type_set;
358
359 CompileUnit *comp_unit = NULL;
360 DWARFCompileUnit* dwarf_cu = NULL;
361 if (sc_scope)
362 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
363
364 if (comp_unit)
365 {
366 dwarf_cu = GetDWARFCompileUnit(comp_unit);
367 if (dwarf_cu == 0)
368 return 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +0000369 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000370 dwarf_cu->GetOffset(),
371 dwarf_cu->GetNextCompileUnitOffset(),
372 type_mask,
373 type_set);
374 }
375 else
376 {
377 DWARFDebugInfo* info = DebugInfo();
378 if (info)
379 {
380 const size_t num_cus = info->GetNumCompileUnits();
381 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
382 {
383 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
384 if (dwarf_cu)
385 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000386 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000387 0,
388 UINT32_MAX,
389 type_mask,
390 type_set);
391 }
392 }
393 }
394 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000395
Greg Claytona1e5dc82015-08-11 22:53:00 +0000396 std::set<CompilerType> clang_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000397 size_t num_types_added = 0;
398 for (Type *type : type_set)
399 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000400 CompilerType clang_type = type->GetForwardCompilerType ();
Greg Clayton0fc4f312013-06-20 01:23:18 +0000401 if (clang_type_set.find(clang_type) == clang_type_set.end())
402 {
403 clang_type_set.insert(clang_type);
404 type_list.Insert (type->shared_from_this());
405 ++num_types_added;
406 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000407 }
408 return num_types_added;
409}
410
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000411
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412//----------------------------------------------------------------------
413// Gets the first parent that is a lexical block, function or inlined
414// subroutine, or compile unit.
415//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000416DWARFDIE
417SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000419 DWARFDIE die;
420 for (die = child_die.GetParent(); die; die = die.GetParent())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000422 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423
424 switch (tag)
425 {
426 case DW_TAG_compile_unit:
427 case DW_TAG_subprogram:
428 case DW_TAG_inlined_subroutine:
429 case DW_TAG_lexical_block:
430 return die;
431 }
432 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000433 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434}
435
436
Greg Clayton450e3f32010-10-12 02:24:53 +0000437SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
438 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000439 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton1f746072012-08-29 21:13:06 +0000440 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000441 m_debug_map_symfile (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000443 m_data_debug_abbrev (),
444 m_data_debug_aranges (),
445 m_data_debug_frame (),
446 m_data_debug_info (),
447 m_data_debug_line (),
448 m_data_debug_loc (),
449 m_data_debug_ranges (),
450 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000451 m_data_apple_names (),
452 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000453 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 m_info(),
456 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000457 m_apple_names_ap (),
458 m_apple_types_ap (),
459 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000460 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000461 m_function_basename_index(),
462 m_function_fullname_index(),
463 m_function_method_index(),
464 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000465 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000466 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000467 m_type_index(),
468 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000469 m_indexed (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000470 m_using_apple_tables (false),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000471 m_fetched_external_modules (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000472 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000473 m_ranges(),
474 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475{
476}
477
478SymbolFileDWARF::~SymbolFileDWARF()
479{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000480}
481
482static const ConstString &
483GetDWARFMachOSegmentName ()
484{
485 static ConstString g_dwarf_section_name ("__DWARF");
486 return g_dwarf_section_name;
487}
488
Greg Claytone576ab22011-02-15 00:19:15 +0000489UniqueDWARFASTTypeMap &
490SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
491{
Greg Clayton1f746072012-08-29 21:13:06 +0000492 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000493 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
494 return m_unique_ast_type_map;
495}
496
Greg Clayton8b4edba2015-08-14 20:02:05 +0000497ClangASTContext &
Greg Clayton6beaaa62011-01-17 03:46:26 +0000498SymbolFileDWARF::GetClangASTContext ()
499{
Greg Clayton1f746072012-08-29 21:13:06 +0000500 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000501 return m_debug_map_symfile->GetClangASTContext ();
Greg Clayton6dc8d582015-08-18 22:32:36 +0000502 else
503 return m_obj_file->GetModule()->GetClangASTContext();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000504}
505
Greg Clayton8b4edba2015-08-14 20:02:05 +0000506TypeSystem *
507SymbolFileDWARF::GetTypeSystemForLanguage (LanguageType language)
508{
509 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
510 if (debug_map_symfile)
511 return debug_map_symfile->GetTypeSystemForLanguage (language);
512 else
Greg Clayton6dc8d582015-08-18 22:32:36 +0000513 return m_obj_file->GetModule()->GetTypeSystemForLanguage (language);
Greg Clayton8b4edba2015-08-14 20:02:05 +0000514}
515
Greg Clayton6beaaa62011-01-17 03:46:26 +0000516void
517SymbolFileDWARF::InitializeObject()
518{
Greg Claytone72dfb32012-02-24 01:59:29 +0000519 ModuleSP module_sp (m_obj_file->GetModule());
520 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000521 {
Greg Clayton3046e662013-07-10 01:23:25 +0000522 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000523
524 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
525
526 // Memory map the DWARF mach-o segment so we have everything mmap'ed
527 // to keep our heap memory usage down.
528 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000529 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000530 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000531 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000532 if (m_data_apple_names.GetByteSize() > 0)
533 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000534 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
535 if (m_apple_names_ap->IsValid())
536 m_using_apple_tables = true;
537 else
Greg Clayton7f995132011-10-04 22:41:51 +0000538 m_apple_names_ap.reset();
539 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000540 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000541 if (m_data_apple_types.GetByteSize() > 0)
542 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000543 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
544 if (m_apple_types_ap->IsValid())
545 m_using_apple_tables = true;
546 else
Greg Clayton7f995132011-10-04 22:41:51 +0000547 m_apple_types_ap.reset();
548 }
549
550 get_apple_namespaces_data();
551 if (m_data_apple_namespaces.GetByteSize() > 0)
552 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000553 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
554 if (m_apple_namespaces_ap->IsValid())
555 m_using_apple_tables = true;
556 else
Greg Clayton7f995132011-10-04 22:41:51 +0000557 m_apple_namespaces_ap.reset();
558 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000559
Greg Clayton5009f9d2011-10-27 17:55:14 +0000560 get_apple_objc_data();
561 if (m_data_apple_objc.GetByteSize() > 0)
562 {
563 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
564 if (m_apple_objc_ap->IsValid())
565 m_using_apple_tables = true;
566 else
567 m_apple_objc_ap.reset();
568 }
Greg Clayton6dc8d582015-08-18 22:32:36 +0000569
570 // Set the symbol file to this file if we don't have a debug map symbol
571 // file as our main symbol file. This allows the clang ASTContext to complete
572 // types using this symbol file when it needs to complete classes and structures.
573 if (GetDebugMapSymfile () == nullptr)
574 GetClangASTContext().SetSymbolFile(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575}
576
577bool
578SymbolFileDWARF::SupportedVersion(uint16_t version)
579{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000580 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
583uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000584SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585{
586 uint32_t abilities = 0;
587 if (m_obj_file != NULL)
588 {
589 const Section* section = NULL;
590 const SectionList *section_list = m_obj_file->GetSectionList();
591 if (section_list == NULL)
592 return 0;
593
594 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 uint64_t debug_info_file_size = 0;
596 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597
Greg Clayton6beaaa62011-01-17 03:46:26 +0000598 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599
600 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000601 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602
Greg Clayton4ceb9982010-07-21 22:54:26 +0000603 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 if (section != NULL)
605 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000606 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607
Greg Clayton4ceb9982010-07-21 22:54:26 +0000608 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000610 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 else
612 m_flags.Set (flagsGotDebugAbbrevData);
613
Greg Clayton4ceb9982010-07-21 22:54:26 +0000614 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000615 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 m_flags.Set (flagsGotDebugArangesData);
617
Greg Clayton4ceb9982010-07-21 22:54:26 +0000618 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000619 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 m_flags.Set (flagsGotDebugFrameData);
621
Greg Clayton4ceb9982010-07-21 22:54:26 +0000622 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000624 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 else
626 m_flags.Set (flagsGotDebugLineData);
627
Greg Clayton4ceb9982010-07-21 22:54:26 +0000628 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000629 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 m_flags.Set (flagsGotDebugLocData);
631
Greg Clayton4ceb9982010-07-21 22:54:26 +0000632 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000633 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 m_flags.Set (flagsGotDebugMacInfoData);
635
Greg Clayton4ceb9982010-07-21 22:54:26 +0000636 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000637 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 m_flags.Set (flagsGotDebugPubNamesData);
639
Greg Clayton4ceb9982010-07-21 22:54:26 +0000640 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000641 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 m_flags.Set (flagsGotDebugPubTypesData);
643
Greg Clayton4ceb9982010-07-21 22:54:26 +0000644 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000645 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 m_flags.Set (flagsGotDebugRangesData);
647
Greg Clayton4ceb9982010-07-21 22:54:26 +0000648 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000649 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 m_flags.Set (flagsGotDebugStrData);
651 }
Greg Clayton6c596612012-05-18 21:47:20 +0000652 else
653 {
654 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
655 if (symfile_dir_cstr)
656 {
657 if (strcasestr(symfile_dir_cstr, ".dsym"))
658 {
659 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
660 {
661 // We have a dSYM file that didn't have a any debug info.
662 // If the string table has a size of 1, then it was made from
663 // an executable with no debug info, or from an executable that
664 // was stripped.
665 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
666 if (section && section->GetFileSize() == 1)
667 {
668 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
669 }
670 }
671 }
672 }
673 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674
675 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
676 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
677
678 if (debug_line_file_size > 0)
679 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 }
681 return abilities;
682}
683
Ed Masteeeae7212013-10-24 20:43:47 +0000684const DWARFDataExtractor&
685SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686{
687 if (m_flags.IsClear (got_flag))
688 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000689 ModuleSP module_sp (m_obj_file->GetModule());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690 m_flags.Set (got_flag);
Greg Clayton3046e662013-07-10 01:23:25 +0000691 const SectionList *section_list = module_sp->GetSectionList();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 if (section_list)
693 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000694 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
695 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 {
697 // See if we memory mapped the DWARF segment?
698 if (m_dwarf_data.GetByteSize())
699 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000700 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
702 else
703 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000704 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 data.Clear();
706 }
707 }
708 }
709 }
710 return data;
711}
712
Ed Masteeeae7212013-10-24 20:43:47 +0000713const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714SymbolFileDWARF::get_debug_abbrev_data()
715{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000716 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717}
718
Ed Masteeeae7212013-10-24 20:43:47 +0000719const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000720SymbolFileDWARF::get_debug_addr_data()
721{
722 return GetCachedSectionData (flagsGotDebugAddrData, eSectionTypeDWARFDebugAddr, m_data_debug_addr);
723}
724
725const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000726SymbolFileDWARF::get_debug_aranges_data()
727{
728 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
729}
730
Ed Masteeeae7212013-10-24 20:43:47 +0000731const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732SymbolFileDWARF::get_debug_frame_data()
733{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000734 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735}
736
Ed Masteeeae7212013-10-24 20:43:47 +0000737const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738SymbolFileDWARF::get_debug_info_data()
739{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000740 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741}
742
Ed Masteeeae7212013-10-24 20:43:47 +0000743const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744SymbolFileDWARF::get_debug_line_data()
745{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000746 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747}
748
Ed Masteeeae7212013-10-24 20:43:47 +0000749const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750SymbolFileDWARF::get_debug_loc_data()
751{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000752 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753}
754
Ed Masteeeae7212013-10-24 20:43:47 +0000755const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756SymbolFileDWARF::get_debug_ranges_data()
757{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000758 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
Ed Masteeeae7212013-10-24 20:43:47 +0000761const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762SymbolFileDWARF::get_debug_str_data()
763{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000764 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765}
766
Ed Masteeeae7212013-10-24 20:43:47 +0000767const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000768SymbolFileDWARF::get_debug_str_offsets_data()
769{
770 return GetCachedSectionData (flagsGotDebugStrOffsetsData, eSectionTypeDWARFDebugStrOffsets, m_data_debug_str_offsets);
771}
772
773const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000774SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000775{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000776 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000777}
778
Ed Masteeeae7212013-10-24 20:43:47 +0000779const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000780SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000781{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000782 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000783}
784
Ed Masteeeae7212013-10-24 20:43:47 +0000785const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000786SymbolFileDWARF::get_apple_namespaces_data()
787{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000788 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
789}
790
Ed Masteeeae7212013-10-24 20:43:47 +0000791const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000792SymbolFileDWARF::get_apple_objc_data()
793{
794 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000795}
796
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797
798DWARFDebugAbbrev*
799SymbolFileDWARF::DebugAbbrev()
800{
801 if (m_abbr.get() == NULL)
802 {
Ed Masteeeae7212013-10-24 20:43:47 +0000803 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 if (debug_abbrev_data.GetByteSize() > 0)
805 {
806 m_abbr.reset(new DWARFDebugAbbrev());
807 if (m_abbr.get())
808 m_abbr->Parse(debug_abbrev_data);
809 }
810 }
811 return m_abbr.get();
812}
813
814const DWARFDebugAbbrev*
815SymbolFileDWARF::DebugAbbrev() const
816{
817 return m_abbr.get();
818}
819
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
821DWARFDebugInfo*
822SymbolFileDWARF::DebugInfo()
823{
824 if (m_info.get() == NULL)
825 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000826 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
827 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 if (get_debug_info_data().GetByteSize() > 0)
829 {
830 m_info.reset(new DWARFDebugInfo());
831 if (m_info.get())
832 {
833 m_info->SetDwarfData(this);
834 }
835 }
836 }
837 return m_info.get();
838}
839
840const DWARFDebugInfo*
841SymbolFileDWARF::DebugInfo() const
842{
843 return m_info.get();
844}
845
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000847SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848{
Greg Claytonea4a5bb2015-09-04 22:29:46 +0000849 if (!comp_unit)
850 return nullptr;
851
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000853 if (info)
854 {
855 if (GetDebugMapSymfile ())
856 {
857 // The debug map symbol file made the compile units for this DWARF
858 // file which is .o file with DWARF in it, and we should have
859 // only 1 compile unit which is at offset zero in the DWARF.
860 // TODO: modify to support LTO .o files where each .o file might
861 // have multiple DW_TAG_compile_unit tags.
Greg Clayton68c00bd2015-02-05 02:10:29 +0000862
Greg Clayton6071e6f2015-08-26 22:57:51 +0000863 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0);
Greg Clayton68c00bd2015-02-05 02:10:29 +0000864 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
865 dwarf_cu->SetUserData(comp_unit);
866 return dwarf_cu;
Greg Clayton1f746072012-08-29 21:13:06 +0000867 }
868 else
869 {
870 // Just a normal DWARF file whose user ID for the compile unit is
871 // the DWARF offset itself
Greg Clayton68c00bd2015-02-05 02:10:29 +0000872
Greg Clayton6071e6f2015-08-26 22:57:51 +0000873 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
Greg Clayton68c00bd2015-02-05 02:10:29 +0000874 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
875 dwarf_cu->SetUserData(comp_unit);
876 return dwarf_cu;
877
Greg Clayton1f746072012-08-29 21:13:06 +0000878 }
879 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880 return NULL;
881}
882
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883
884DWARFDebugRanges*
885SymbolFileDWARF::DebugRanges()
886{
887 if (m_ranges.get() == NULL)
888 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000889 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
890 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891 if (get_debug_ranges_data().GetByteSize() > 0)
892 {
893 m_ranges.reset(new DWARFDebugRanges());
894 if (m_ranges.get())
895 m_ranges->Extract(this);
896 }
897 }
898 return m_ranges.get();
899}
900
901const DWARFDebugRanges*
902SymbolFileDWARF::DebugRanges() const
903{
904 return m_ranges.get();
905}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906
Greg Clayton53eb1c22012-04-02 22:59:12 +0000907lldb::CompUnitSP
908SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000910 CompUnitSP cu_sp;
911 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000913 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
914 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000916 // We already parsed this compile unit, had out a shared pointer to it
917 cu_sp = comp_unit->shared_from_this();
918 }
919 else
920 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000921 if (dwarf_cu->GetSymbolFileDWARF() != this)
922 {
923 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu, cu_idx);
924 }
925 else if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 {
Greg Clayton1f746072012-08-29 21:13:06 +0000927 // Let the debug map create the compile unit
928 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
929 dwarf_cu->SetUserData(cu_sp.get());
930 }
931 else
932 {
933 ModuleSP module_sp (m_obj_file->GetModule());
934 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000935 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000936 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
Greg Clayton1f746072012-08-29 21:13:06 +0000937 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000938 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000939 FileSpec cu_file_spec{cu_die.GetName(), false};
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000940 if (cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000941 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000942 // If we have a full path to the compile unit, we don't need to resolve
943 // the file. This can be expensive e.g. when the source files are NFS mounted.
Chaoren Lin372e9062015-06-09 17:54:27 +0000944 if (cu_file_spec.IsRelative())
Greg Clayton53eb1c22012-04-02 22:59:12 +0000945 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000946 const char *cu_comp_dir{cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000947 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
Greg Clayton1f746072012-08-29 21:13:06 +0000948 }
949
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000950 std::string remapped_file;
951 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file))
952 cu_file_spec.SetFile(remapped_file, false);
David Srbeckyd515e942015-07-08 14:00:04 +0000953 }
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000954
Greg Clayton5ce1a842015-08-27 18:09:44 +0000955 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000956
Jason Molendac7099582015-07-31 05:47:00 +0000957 bool is_optimized = dwarf_cu->GetIsOptimized ();
David Srbeckyd515e942015-07-08 14:00:04 +0000958 cu_sp.reset(new CompileUnit (module_sp,
959 dwarf_cu,
960 cu_file_spec,
Greg Clayton6071e6f2015-08-26 22:57:51 +0000961 dwarf_cu->GetID(),
Jason Molenda6ab659a2015-07-29 00:42:47 +0000962 cu_language,
963 is_optimized));
David Srbeckyd515e942015-07-08 14:00:04 +0000964 if (cu_sp)
965 {
966 // If we just created a compile unit with an invalid file spec, try and get the
967 // first entry in the supports files from the line table as that should be the
968 // compile unit.
969 if (!cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000970 {
David Srbeckyd515e942015-07-08 14:00:04 +0000971 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
972 if (cu_file_spec)
973 {
974 (FileSpec &)(*cu_sp) = cu_file_spec;
975 // Also fix the invalid file spec which was copied from the compile unit.
976 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
977 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000978 }
David Srbeckyd515e942015-07-08 14:00:04 +0000979
980 dwarf_cu->SetUserData(cu_sp.get());
981
982 // Figure out the compile unit index if we weren't given one
983 if (cu_idx == UINT32_MAX)
984 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
985
986 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000987 }
988 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 }
990 }
991 }
992 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000993 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994}
995
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996uint32_t
997SymbolFileDWARF::GetNumCompileUnits()
998{
999 DWARFDebugInfo* info = DebugInfo();
1000 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002 return 0;
1003}
1004
1005CompUnitSP
1006SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
1007{
Greg Clayton53eb1c22012-04-02 22:59:12 +00001008 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 DWARFDebugInfo* info = DebugInfo();
1010 if (info)
1011 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001012 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
1013 if (dwarf_cu)
1014 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001016 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017}
1018
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019Function *
Greg Clayton6071e6f2015-08-26 22:57:51 +00001020SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, const DWARFDIE &die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001022 if (die.IsValid())
1023 {
1024 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025
Greg Clayton6071e6f2015-08-26 22:57:51 +00001026 if (type_system)
1027 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001028 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1029 if (dwarf_ast)
1030 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
Greg Clayton6071e6f2015-08-26 22:57:51 +00001031 }
1032 }
1033 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034}
1035
Greg Clayton9422dd62013-03-04 21:46:16 +00001036bool
1037SymbolFileDWARF::FixupAddress (Address &addr)
1038{
1039 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1040 if (debug_map_symfile)
1041 {
1042 return debug_map_symfile->LinkOSOAddress(addr);
1043 }
1044 // This is a normal DWARF file, no address fixups need to happen
1045 return true;
1046}
Greg Clayton1f746072012-08-29 21:13:06 +00001047lldb::LanguageType
1048SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1049{
1050 assert (sc.comp_unit);
1051 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1052 if (dwarf_cu)
Greg Clayton5ce1a842015-08-27 18:09:44 +00001053 return dwarf_cu->GetLanguageType();
1054 else
1055 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +00001056}
1057
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058size_t
1059SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1060{
1061 assert (sc.comp_unit);
1062 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001063 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 if (dwarf_cu)
1065 {
1066 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001067 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001069 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001071 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
1072 if (sc.comp_unit->FindFunctionByUID (die.GetID()).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001074 if (ParseCompileUnitFunction(sc, die))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 ++functions_added;
1076 }
1077 }
1078 //FixupTypes();
1079 }
1080 return functions_added;
1081}
1082
1083bool
1084SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1085{
1086 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001087 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001088 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001090 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091
Greg Claytonda2455b2012-11-01 17:28:37 +00001092 if (cu_die)
1093 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001094 const char * cu_comp_dir = resolveCompDir(cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001095
Greg Clayton5ce1a842015-08-27 18:09:44 +00001096 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097
Greg Claytonda2455b2012-11-01 17:28:37 +00001098 // All file indexes in DWARF are one based and a file of index zero is
1099 // supposed to be the compile unit itself.
1100 support_files.Append (*sc.comp_unit);
1101
1102 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 }
1105 return false;
1106}
1107
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001108bool
1109SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1110{
1111 assert (sc.comp_unit);
1112 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1113 if (dwarf_cu)
1114 {
1115 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1116 {
1117 UpdateExternalModuleListIfNeeded();
1118 for (const std::pair<uint64_t, const ClangModuleInfo> &external_type_module : m_external_type_modules)
1119 {
1120 imported_modules.push_back(external_type_module.second.m_name);
1121 }
1122 }
1123 }
1124 return false;
1125}
1126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127struct ParseDWARFLineTableCallbackInfo
1128{
1129 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001130 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131};
1132
1133//----------------------------------------------------------------------
1134// ParseStatementTableCallback
1135//----------------------------------------------------------------------
1136static void
1137ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1138{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1140 {
1141 // Just started parsing the line table
1142 }
1143 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1144 {
1145 // Done parsing line table, nothing to do for the cleanup
1146 }
1147 else
1148 {
1149 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001150 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151
Greg Clayton9422dd62013-03-04 21:46:16 +00001152 // If this is our first time here, we need to create a
1153 // sequence container.
1154 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001156 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1157 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001159 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1160 state.address,
1161 state.line,
1162 state.column,
1163 state.file,
1164 state.is_stmt,
1165 state.basic_block,
1166 state.prologue_end,
1167 state.epilogue_begin,
1168 state.end_sequence);
1169 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001171 // First, put the current sequence into the line table.
1172 line_table->InsertSequence(info->sequence_ap.get());
1173 // Then, empty it to prepare for the next sequence.
1174 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 }
1177}
1178
1179bool
1180SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1181{
1182 assert (sc.comp_unit);
1183 if (sc.comp_unit->GetLineTable() != NULL)
1184 return true;
1185
Greg Clayton1f746072012-08-29 21:13:06 +00001186 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 if (dwarf_cu)
1188 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001189 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001190 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001192 const dw_offset_t cu_line_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET);
Greg Clayton129d12c2011-11-28 03:29:03 +00001193 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001195 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001196 if (line_table_ap.get())
1197 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001198 ParseDWARFLineTableCallbackInfo info;
1199 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001200 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001201 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001202 if (m_debug_map_symfile)
1203 {
1204 // We have an object file that has a line table with addresses
1205 // that are not linked. We need to link the line table and convert
1206 // the addresses that are relative to the .o file into addresses
1207 // for the main executable.
1208 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1209 }
1210 else
1211 {
1212 sc.comp_unit->SetLineTable(line_table_ap.release());
1213 return true;
1214 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001215 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 }
1217 }
1218 }
1219 return false;
1220}
1221
1222size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00001223SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext& sc,
1224 Block *parent_block,
1225 const DWARFDIE &orig_die,
1226 addr_t subprogram_low_pc,
1227 uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228{
1229 size_t blocks_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001230 DWARFDIE die = orig_die;
1231 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001233 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234
1235 switch (tag)
1236 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001238 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 case DW_TAG_lexical_block:
1240 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001241 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001242 if (tag == DW_TAG_subprogram)
1243 {
1244 // Skip any DW_TAG_subprogram DIEs that are inside
1245 // of a normal or inlined functions. These will be
1246 // parsed on their own as separate entities.
1247
1248 if (depth > 0)
1249 break;
1250
1251 block = parent_block;
1252 }
1253 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001254 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001255 BlockSP block_sp(new Block (die.GetID()));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001256 parent_block->AddChild(block_sp);
1257 block = block_sp.get();
1258 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001259 DWARFRangeList ranges;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001260 const char *name = NULL;
1261 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263 int decl_file = 0;
1264 int decl_line = 0;
1265 int decl_column = 0;
1266 int call_file = 0;
1267 int call_line = 0;
1268 int call_column = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001269 if (die.GetDIENamesAndRanges (name,
1270 mangled_name,
1271 ranges,
1272 decl_file, decl_line, decl_column,
1273 call_file, call_line, call_column, nullptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 {
1275 if (tag == DW_TAG_subprogram)
1276 {
1277 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001278 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001280 else if (tag == DW_TAG_inlined_subroutine)
1281 {
1282 // We get called here for inlined subroutines in two ways.
1283 // The first time is when we are making the Function object
1284 // for this inlined concrete instance. Since we're creating a top level block at
1285 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1286 // adjust the containing address.
1287 // The second time is when we are parsing the blocks inside the function that contains
1288 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1289 // function the offset will be for that function.
1290 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1291 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001292 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001293 }
1294 }
Greg Clayton103f3092015-01-15 03:04:37 +00001295
1296 const size_t num_ranges = ranges.GetSize();
1297 for (size_t i = 0; i<num_ranges; ++i)
1298 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001299 const DWARFRangeList::Entry &range = ranges.GetEntryRef (i);
Greg Clayton103f3092015-01-15 03:04:37 +00001300 const addr_t range_base = range.GetRangeBase();
1301 if (range_base >= subprogram_low_pc)
1302 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1303 else
1304 {
1305 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64 ") which has a base that is less than the function's low PC 0x%" PRIx64 ". Please file a bug and attach the file at the start of this error message",
1306 block->GetID(),
1307 range_base,
1308 range.GetRangeEnd(),
1309 subprogram_low_pc);
1310 }
1311 }
1312 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313
1314 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1315 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001316 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001318 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1319 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320
Greg Clayton7b0992d2013-04-18 22:45:39 +00001321 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001323 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1324 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001326 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 }
1328
1329 ++blocks_added;
1330
Greg Clayton6071e6f2015-08-26 22:57:51 +00001331 if (die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001333 blocks_added += ParseFunctionBlocks (sc,
1334 block,
Greg Clayton6071e6f2015-08-26 22:57:51 +00001335 die.GetFirstChild(),
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001336 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001337 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 }
1339 }
1340 }
1341 break;
1342 default:
1343 break;
1344 }
1345
Greg Claytondd7feaf2011-08-12 17:54:33 +00001346 // Only parse siblings of the block if we are not at depth zero. A depth
1347 // of zero indicates we are currently parsing the top level
1348 // DW_TAG_subprogram DIE
1349
1350 if (depth == 0)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001351 die.Clear();
Greg Claytondd7feaf2011-08-12 17:54:33 +00001352 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001353 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
1355 return blocks_added;
1356}
1357
Greg Claytonf0705c82011-10-22 03:33:13 +00001358bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001359SymbolFileDWARF::ClassOrStructIsVirtual (const DWARFDIE &parent_die)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001360{
1361 if (parent_die)
1362 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001363 for (DWARFDIE die = parent_die.GetFirstChild(); die; die = die.GetSibling())
Greg Claytonc4ffd662013-03-08 01:37:30 +00001364 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001365 dw_tag_t tag = die.Tag();
Greg Claytonc4ffd662013-03-08 01:37:30 +00001366 bool check_virtuality = false;
1367 switch (tag)
1368 {
1369 case DW_TAG_inheritance:
1370 case DW_TAG_subprogram:
1371 check_virtuality = true;
1372 break;
1373 default:
1374 break;
1375 }
1376 if (check_virtuality)
1377 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001378 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001379 return true;
1380 }
1381 }
1382 }
1383 return false;
1384}
1385
Greg Clayton99558cc42015-08-24 23:46:31 +00001386CompilerDeclContext
1387SymbolFileDWARF::GetDeclContextForUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001389 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00001391 DWARFDebugInfo* debug_info = DebugInfo();
1392 if (debug_info)
1393 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001394 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001395 if (die)
1396 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001397 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1398 if (dwarf_ast)
1399 return dwarf_ast->GetDeclContextForUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001400 }
1401 }
Sean Callanan72e49402011-08-05 23:43:37 +00001402 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001403 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001404}
1405
Greg Clayton99558cc42015-08-24 23:46:31 +00001406CompilerDeclContext
1407SymbolFileDWARF::GetDeclContextContainingUID (lldb::user_id_t type_uid)
Sean Callanan72e49402011-08-05 23:43:37 +00001408{
Greg Clayton81c22f62011-10-19 18:09:39 +00001409 if (UserIDMatches(type_uid))
Greg Clayton8b4edba2015-08-14 20:02:05 +00001410 {
1411 DWARFDebugInfo* debug_info = DebugInfo();
1412 if (debug_info)
1413 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001414 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001415 if (die)
1416 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001417 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1418 if (dwarf_ast)
1419 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001420 }
1421 }
1422 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001423 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001424}
1425
Greg Clayton99558cc42015-08-24 23:46:31 +00001426
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001428SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429{
Greg Clayton81c22f62011-10-19 18:09:39 +00001430 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001432 DWARFDebugInfo* debug_info = DebugInfo();
1433 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001434 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001435 DWARFDIE type_die = debug_info->GetDIE (DIERef(type_uid));
Greg Clayton6071e6f2015-08-26 22:57:51 +00001436 if (type_die)
1437 {
1438 const bool assert_not_being_parsed = true;
1439 return ResolveTypeUID (type_die, assert_not_being_parsed);
1440 }
Greg Claytonca512b32011-01-14 04:54:56 +00001441 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 }
1443 return NULL;
1444}
1445
Greg Claytoncab36a32011-12-08 05:16:30 +00001446Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001447SymbolFileDWARF::ResolveTypeUID (const DWARFDIE &die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001448{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001449 if (die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001450 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001451 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00001452 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001453 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001454 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001455 die.GetOffset(),
1456 die.GetTagAsCString(),
1457 die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001458
Greg Claytoncab36a32011-12-08 05:16:30 +00001459 // We might be coming in in the middle of a type tree (a class
1460 // withing a class, an enum within a class), so parse any needed
1461 // parent DIEs before we get to this one...
Greg Clayton6071e6f2015-08-26 22:57:51 +00001462 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE (die);
1463 if (decl_ctx_die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001464 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001465 if (log)
Greg Claytoncab36a32011-12-08 05:16:30 +00001466 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001467 switch (decl_ctx_die.Tag())
1468 {
1469 case DW_TAG_structure_type:
1470 case DW_TAG_union_type:
1471 case DW_TAG_class_type:
1472 {
1473 // Get the type, which could be a forward declaration
1474 if (log)
1475 GetObjectFile()->GetModule()->LogMessage (log,
1476 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1477 die.GetOffset(),
1478 die.GetTagAsCString(),
1479 die.GetName(),
1480 decl_ctx_die.GetOffset());
1481 }
1482 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001483
Greg Clayton6071e6f2015-08-26 22:57:51 +00001484 default:
1485 break;
1486 }
1487 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001488 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001489 return ResolveType (die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001490 }
1491 return NULL;
1492}
1493
Greg Clayton6beaaa62011-01-17 03:46:26 +00001494// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1495// SymbolFileDWARF objects to detect if this DWARF file is the one that
1496// can resolve a clang_type.
1497bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001498SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &clang_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001499{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001500 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001501 return GetForwardDeclClangTypeToDie().count (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001502}
1503
1504
Greg Clayton57ee3062013-07-11 22:46:58 +00001505bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00001506SymbolFileDWARF::CompleteType (CompilerType &clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001507{
1508 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Claytona1e5dc82015-08-11 22:53:00 +00001509 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001510 auto die_it = GetForwardDeclClangTypeToDie().find (clang_type_no_qualifiers.GetOpaqueQualType());
1511 if (die_it == GetForwardDeclClangTypeToDie().end())
Greg Clayton73b472d2010-10-27 03:32:59 +00001512 {
1513 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00001514 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00001515 }
1516 // Once we start resolving this type, remove it from the forward declaration
1517 // map in case anyone child members or other types require this type to get resolved.
1518 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1519 // are done.
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001520 GetForwardDeclClangTypeToDie().erase (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001521
Greg Clayton450e3f32010-10-12 02:24:53 +00001522 DWARFDebugInfo* debug_info = DebugInfo();
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001523
1524 DWARFDIE dwarf_die = debug_info->GetDIE(die_it->getSecond());
1525 Type *type = GetDIEToType().lookup (dwarf_die.GetDIE());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001526
Greg Clayton5160ce52013-03-27 23:08:40 +00001527 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001528 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001529 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00001530 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001531 dwarf_die.GetID(),
1532 dwarf_die.GetTagAsCString(),
Greg Claytond61c0fc2012-04-23 22:55:20 +00001533 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001534 assert (clang_type);
Greg Clayton261ac3f2015-08-28 01:01:03 +00001535 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1536 if (dwarf_ast)
1537 return dwarf_ast->CompleteTypeFromDWARF (dwarf_die, type, clang_type);
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001538 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001539}
1540
Greg Clayton8b4edba2015-08-14 20:02:05 +00001541Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001542SymbolFileDWARF::ResolveType (const DWARFDIE &die, bool assert_not_being_parsed)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001543{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001544 if (die)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001545 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001546 Type *type = GetDIEToType().lookup (die.GetDIE());
Greg Claytoncab36a32011-12-08 05:16:30 +00001547
Greg Claytonc685f8e2010-09-15 04:15:46 +00001548 if (type == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001549 type = GetTypeForDIE (die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001550
Greg Clayton24739922010-10-13 03:15:28 +00001551 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001552 {
1553 if (type != DIE_IS_BEING_PARSED)
1554 return type;
1555
1556 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001557 die.GetOffset(),
1558 die.GetTagAsCString(),
1559 die.GetName());
Jim Inghamc3549282012-01-11 02:21:12 +00001560
1561 }
1562 else
1563 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001564 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001565 return nullptr;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001566}
1567
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001569SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570{
1571 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001572 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573 {
1574 // The symbol vendor doesn't know about this compile unit, we
1575 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00001576 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001578 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579}
1580
Greg Clayton8b4edba2015-08-14 20:02:05 +00001581size_t
1582SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets)
1583{
1584 method_die_offsets.clear();
1585 if (m_using_apple_tables)
1586 {
1587 if (m_apple_objc_ap.get())
1588 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1589 }
1590 else
1591 {
1592 if (!m_indexed)
1593 Index ();
1594
1595 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1596 }
1597 return method_die_offsets.size();
1598}
1599
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001601SymbolFileDWARF::GetFunction (const DWARFDIE &die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602{
Greg Clayton72310352013-02-23 04:12:47 +00001603 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604
Greg Clayton6071e6f2015-08-26 22:57:51 +00001605 if (die)
1606 {
1607 // Check if the symbol vendor already knows about this compile unit?
1608 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1609
1610 sc.function = sc.comp_unit->FindFunctionByUID (die.GetID()).get();
1611 if (sc.function == NULL)
1612 sc.function = ParseCompileUnitFunction(sc, die);
1613
1614 if (sc.function)
1615 {
1616 sc.module_sp = sc.function->CalculateSymbolContextModule();
1617 return true;
1618 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00001619 }
1620
1621 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001622}
1623
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001624void
1625SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
1626{
1627 if (m_fetched_external_modules)
1628 return;
1629 m_fetched_external_modules = true;
1630
1631 DWARFDebugInfo * debug_info = DebugInfo();
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001632
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001633 const uint32_t num_compile_units = GetNumCompileUnits();
1634 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1635 {
1636 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1637
Greg Clayton5ce1a842015-08-27 18:09:44 +00001638 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1639 if (die && die.HasChildren() == false)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001640 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001641 const uint64_t name_strp = die.GetAttributeValueAsUnsigned (DW_AT_name, UINT64_MAX);
1642 const uint64_t dwo_path_strp = die.GetAttributeValueAsUnsigned (DW_AT_GNU_dwo_name, UINT64_MAX);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001643
1644 if (name_strp != UINT64_MAX)
1645 {
1646 if (m_external_type_modules.find(dwo_path_strp) == m_external_type_modules.end())
1647 {
1648 const char *name = get_debug_str_data().PeekCStr(name_strp);
1649 const char *dwo_path = get_debug_str_data().PeekCStr(dwo_path_strp);
1650 if (name || dwo_path)
1651 {
1652 ModuleSP module_sp;
1653 if (dwo_path)
1654 {
1655 ModuleSpec dwo_module_spec;
1656 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1657 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
1658 //printf ("Loading dwo = '%s'\n", dwo_path);
1659 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
1660 }
1661
1662 if (dwo_path_strp != LLDB_INVALID_UID)
1663 {
1664 m_external_type_modules[dwo_path_strp] = ClangModuleInfo { ConstString(name), module_sp };
1665 }
1666 else
1667 {
1668 // This hack should be removed promptly once clang emits both.
1669 m_external_type_modules[name_strp] = ClangModuleInfo { ConstString(name), module_sp };
1670 }
1671 }
1672 }
1673 }
1674 }
1675 }
1676}
Greg Clayton2501e5e2015-01-15 02:59:20 +00001677
1678SymbolFileDWARF::GlobalVariableMap &
1679SymbolFileDWARF::GetGlobalAranges()
1680{
1681 if (!m_global_aranges_ap)
1682 {
1683 m_global_aranges_ap.reset (new GlobalVariableMap());
1684
1685 ModuleSP module_sp = GetObjectFile()->GetModule();
1686 if (module_sp)
1687 {
1688 const size_t num_cus = module_sp->GetNumCompileUnits();
1689 for (size_t i = 0; i < num_cus; ++i)
1690 {
1691 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1692 if (cu_sp)
1693 {
1694 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1695 if (globals_sp)
1696 {
1697 const size_t num_globals = globals_sp->GetSize();
1698 for (size_t g = 0; g < num_globals; ++g)
1699 {
1700 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1701 if (var_sp && !var_sp->GetLocationIsConstantValueData())
1702 {
1703 const DWARFExpression &location = var_sp->LocationExpression();
1704 Value location_result;
1705 Error error;
1706 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
1707 {
1708 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
1709 {
1710 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
1711 lldb::addr_t byte_size = 1;
1712 if (var_sp->GetType())
1713 byte_size = var_sp->GetType()->GetByteSize();
1714 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
1715 }
1716 }
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723 m_global_aranges_ap->Sort();
1724 }
1725 return *m_global_aranges_ap;
1726}
1727
1728
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729uint32_t
1730SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1731{
1732 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00001733 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001734 static_cast<void*>(so_addr.GetSection().get()),
1735 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00001737 if (resolve_scope & ( eSymbolContextCompUnit |
1738 eSymbolContextFunction |
1739 eSymbolContextBlock |
1740 eSymbolContextLineEntry |
1741 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742 {
1743 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1744
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001746 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001748 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00001749 if (cu_offset == DW_INVALID_OFFSET)
1750 {
1751 // Global variables are not in the compile unit address ranges. The only way to
1752 // currently find global variables is to iterate over the .debug_pubnames or the
1753 // __apple_names table and find all items in there that point to DW_TAG_variable
1754 // DIEs and then find the address that matches.
1755 if (resolve_scope & eSymbolContextVariable)
1756 {
1757 GlobalVariableMap &map = GetGlobalAranges();
1758 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
1759 if (entry && entry->data)
1760 {
1761 Variable *variable = entry->data;
1762 SymbolContextScope *scc = variable->GetSymbolContextScope();
1763 if (scc)
1764 {
1765 scc->CalculateSymbolContext(&sc);
1766 sc.variable = variable;
1767 }
1768 return sc.GetResolvedMask();
1769 }
1770 }
1771 }
1772 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001774 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001775 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001776 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001777 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001778 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001779 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001781 resolved |= eSymbolContextCompUnit;
1782
Greg Clayton6ab80132012-12-12 17:30:52 +00001783 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001784 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1785 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001786 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1787 DWARFDIE block_die;
1788 if (function_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001790 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001791 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001792 sc.function = ParseCompileUnitFunction(sc, function_die);
1793
1794 if (sc.function && (resolve_scope & eSymbolContextBlock))
1795 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001796 }
1797 else
1798 {
1799 // We might have had a compile unit that had discontiguous
1800 // address ranges where the gaps are symbols that don't have
1801 // any debug info. Discontiguous compile unit address ranges
1802 // should only happen when there aren't other functions from
1803 // other compile units in these gaps. This helps keep the size
1804 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00001805 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001806 }
1807
1808 if (sc.function != NULL)
1809 {
1810 resolved |= eSymbolContextFunction;
1811
1812 if (resolve_scope & eSymbolContextBlock)
1813 {
1814 Block& block = sc.function->GetBlock (true);
1815
Greg Clayton6071e6f2015-08-26 22:57:51 +00001816 if (block_die)
1817 sc.block = block.FindBlockByID (block_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001818 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001819 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001820 if (sc.block)
1821 resolved |= eSymbolContextBlock;
1822 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001823 }
1824 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001825
1826 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
1827 {
1828 LineTable *line_table = sc.comp_unit->GetLineTable();
1829 if (line_table != NULL)
1830 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001831 // And address that makes it into this function should be in terms
1832 // of this debug file if there is no debug map, or it will be an
1833 // address in the .o file which needs to be fixed up to be in terms
1834 // of the debug map executable. Either way, calling FixupAddress()
1835 // will work for us.
1836 Address exe_so_addr (so_addr);
1837 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00001838 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001839 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00001840 {
1841 resolved |= eSymbolContextLineEntry;
1842 }
1843 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001844 }
1845 }
1846
1847 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
1848 {
1849 // We might have had a compile unit that had discontiguous
1850 // address ranges where the gaps are symbols that don't have
1851 // any debug info. Discontiguous compile unit address ranges
1852 // should only happen when there aren't other functions from
1853 // other compile units in these gaps. This helps keep the size
1854 // of the aranges down.
1855 sc.comp_unit = NULL;
1856 resolved &= ~eSymbolContextCompUnit;
1857 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001859 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001860 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001861 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
1862 cu_offset,
1863 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864 }
1865 }
1866 }
1867 }
1868 }
1869 return resolved;
1870}
1871
1872
1873
1874uint32_t
1875SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1876{
1877 const uint32_t prev_size = sc_list.GetSize();
1878 if (resolve_scope & eSymbolContextCompUnit)
1879 {
1880 DWARFDebugInfo* debug_info = DebugInfo();
1881 if (debug_info)
1882 {
1883 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00001884 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885
Greg Clayton53eb1c22012-04-02 22:59:12 +00001886 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001888 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00001889 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00001890 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891 if (check_inlines || file_spec_matches_cu_file_spec)
1892 {
1893 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00001894 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001895 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001897 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898
Greg Clayton526a4ae2012-05-16 22:09:01 +00001899 // If we are looking for inline functions only and we don't
1900 // find it in the support files, we are done.
1901 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001902 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001903 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1904 if (file_idx == UINT32_MAX)
1905 continue;
1906 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001907
Greg Clayton526a4ae2012-05-16 22:09:01 +00001908 if (line != 0)
1909 {
1910 LineTable *line_table = sc.comp_unit->GetLineTable();
1911
1912 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001914 // We will have already looked up the file index if
1915 // we are searching for inline entries.
1916 if (!check_inlines)
1917 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918
Greg Clayton526a4ae2012-05-16 22:09:01 +00001919 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001921 uint32_t found_line;
1922 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1923 found_line = sc.line_entry.line;
1924
1925 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001927 sc.function = NULL;
1928 sc.block = NULL;
1929 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001931 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1932 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001934 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1935 DWARFDIE block_die;
1936 if (function_die)
Greg Clayton526a4ae2012-05-16 22:09:01 +00001937 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001938 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001939 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001940 sc.function = ParseCompileUnitFunction(sc, function_die);
1941
1942 if (sc.function && (resolve_scope & eSymbolContextBlock))
1943 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001944 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945
Greg Clayton526a4ae2012-05-16 22:09:01 +00001946 if (sc.function != NULL)
1947 {
1948 Block& block = sc.function->GetBlock (true);
1949
Greg Clayton6071e6f2015-08-26 22:57:51 +00001950 if (block_die)
1951 sc.block = block.FindBlockByID (block_die.GetID());
1952 else if (function_die)
1953 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955 }
1956 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001957
Greg Clayton526a4ae2012-05-16 22:09:01 +00001958 sc_list.Append(sc);
1959 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1960 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961 }
1962 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001963 else if (file_spec_matches_cu_file_spec && !check_inlines)
1964 {
1965 // only append the context if we aren't looking for inline call sites
1966 // by file and line and if the file spec matches that of the compile unit
1967 sc_list.Append(sc);
1968 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 }
1970 else if (file_spec_matches_cu_file_spec && !check_inlines)
1971 {
1972 // only append the context if we aren't looking for inline call sites
1973 // by file and line and if the file spec matches that of the compile unit
1974 sc_list.Append(sc);
1975 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976
Greg Clayton526a4ae2012-05-16 22:09:01 +00001977 if (!check_inlines)
1978 break;
1979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980 }
1981 }
1982 }
1983 }
1984 return sc_list.GetSize() - prev_size;
1985}
1986
1987void
1988SymbolFileDWARF::Index ()
1989{
1990 if (m_indexed)
1991 return;
1992 m_indexed = true;
1993 Timer scoped_timer (__PRETTY_FUNCTION__,
1994 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00001995 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996
1997 DWARFDebugInfo* debug_info = DebugInfo();
1998 if (debug_info)
1999 {
2000 uint32_t cu_idx = 0;
2001 const uint32_t num_compile_units = GetNumCompileUnits();
2002 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2003 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002004 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005
Greg Clayton53eb1c22012-04-02 22:59:12 +00002006 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002008 dwarf_cu->Index (m_function_basename_index,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002009 m_function_fullname_index,
2010 m_function_method_index,
2011 m_function_selector_index,
2012 m_objc_class_selectors_index,
2013 m_global_index,
2014 m_type_index,
2015 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002016
2017 // Keep memory down by clearing DIEs if this generate function
2018 // caused them to be parsed
2019 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002020 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002021 }
2022
Greg Claytond4a2b372011-09-12 23:21:58 +00002023 m_function_basename_index.Finalize();
2024 m_function_fullname_index.Finalize();
2025 m_function_method_index.Finalize();
2026 m_function_selector_index.Finalize();
2027 m_objc_class_selectors_index.Finalize();
2028 m_global_index.Finalize();
2029 m_type_index.Finalize();
2030 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002031
Greg Clayton24739922010-10-13 03:15:28 +00002032#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002033 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002034 s.Printf ("DWARF index for '%s':",
2035 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002036 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2037 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2038 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2039 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2040 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2041 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002042 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00002043 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002044#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045 }
2046}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002047
2048bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002049SymbolFileDWARF::DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx)
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002050{
Greg Clayton99558cc42015-08-24 23:46:31 +00002051 if (decl_ctx == nullptr || !decl_ctx->IsValid())
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002052 {
2053 // Invalid namespace decl which means we aren't matching only things
2054 // in this symbol file, so return true to indicate it matches this
2055 // symbol file.
2056 return true;
2057 }
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002058
Greg Clayton99558cc42015-08-24 23:46:31 +00002059 if ((TypeSystem *)&GetClangASTContext() == decl_ctx->GetTypeSystem())
2060 return true; // The type systems match, return true
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002061
2062 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00002063 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00002064
2065 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002066 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002067
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002068 return false;
2069}
2070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002072SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073{
Greg Clayton5160ce52013-03-27 23:08:40 +00002074 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002075
2076 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002077 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002078 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00002079 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002080 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002081 append, max_matches);
2082
Greg Clayton99558cc42015-08-24 23:46:31 +00002083 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002084 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002085
Greg Claytonc685f8e2010-09-15 04:15:46 +00002086 DWARFDebugInfo* info = DebugInfo();
2087 if (info == NULL)
2088 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089
2090 // If we aren't appending the results to this list, then clear the list
2091 if (!append)
2092 variables.Clear();
2093
2094 // Remember how many variables are in the list before we search in case
2095 // we are appending the results to a variable list.
2096 const uint32_t original_size = variables.GetSize();
2097
Greg Claytond4a2b372011-09-12 23:21:58 +00002098 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002099
Greg Clayton97fbc342011-10-20 22:30:33 +00002100 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002101 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002102 if (m_apple_names_ap.get())
2103 {
2104 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00002105 llvm::StringRef basename;
2106 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002107
Jim Inghamaa816b82015-09-02 01:59:14 +00002108 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, basename))
Jim Inghamfa39bb42014-10-25 00:33:55 +00002109 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002110
Jim Inghamfa39bb42014-10-25 00:33:55 +00002111 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00002112 }
Greg Clayton7f995132011-10-04 22:41:51 +00002113 }
2114 else
2115 {
2116 // Index the DWARF if we haven't already
2117 if (!m_indexed)
2118 Index ();
2119
2120 m_global_index.Find (name, die_offsets);
2121 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002122
Greg Clayton437a1352012-04-09 22:43:43 +00002123 const size_t num_die_matches = die_offsets.size();
2124 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125 {
Greg Clayton7f995132011-10-04 22:41:51 +00002126 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002127 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002128 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002129
Greg Claytond4a2b372011-09-12 23:21:58 +00002130 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002131 bool done = false;
2132 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002133 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002134 const DIERef& die_ref = die_offsets[i];
2135 DWARFDIE die = debug_info->GetDIE (die_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002136
Greg Clayton95d87902011-11-11 03:16:25 +00002137 if (die)
2138 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002139 switch (die.Tag())
Greg Clayton437a1352012-04-09 22:43:43 +00002140 {
2141 default:
2142 case DW_TAG_subprogram:
2143 case DW_TAG_inlined_subroutine:
2144 case DW_TAG_try_block:
2145 case DW_TAG_catch_block:
2146 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002147
Greg Clayton437a1352012-04-09 22:43:43 +00002148 case DW_TAG_variable:
2149 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002150 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002151
Greg Clayton99558cc42015-08-24 23:46:31 +00002152 if (parent_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002153 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002154 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2155 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002156 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002157 CompilerDeclContext actual_parent_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002158 if (!actual_parent_decl_ctx || actual_parent_decl_ctx != *parent_decl_ctx)
2159 continue;
2160 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00002161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162
Greg Clayton6071e6f2015-08-26 22:57:51 +00002163 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002164
Greg Clayton437a1352012-04-09 22:43:43 +00002165 if (variables.GetSize() - original_size >= max_matches)
2166 done = true;
2167 }
2168 break;
2169 }
Greg Clayton95d87902011-11-11 03:16:25 +00002170 }
2171 else
2172 {
2173 if (m_using_apple_tables)
2174 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002175 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002176 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002177 }
2178 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002179 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180 }
2181
2182 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002183 const uint32_t num_matches = variables.GetSize() - original_size;
2184 if (log && num_matches > 0)
2185 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002186 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002187 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002188 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002189 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002190 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002191 num_matches);
2192 }
2193 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194}
2195
2196uint32_t
2197SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2198{
Greg Clayton5160ce52013-03-27 23:08:40 +00002199 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002200
Greg Clayton21f2a492011-10-06 00:09:08 +00002201 if (log)
2202 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002203 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002204 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002205 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002206 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002207 }
2208
Greg Claytonc685f8e2010-09-15 04:15:46 +00002209 DWARFDebugInfo* info = DebugInfo();
2210 if (info == NULL)
2211 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212
2213 // If we aren't appending the results to this list, then clear the list
2214 if (!append)
2215 variables.Clear();
2216
2217 // Remember how many variables are in the list before we search in case
2218 // we are appending the results to a variable list.
2219 const uint32_t original_size = variables.GetSize();
2220
Greg Clayton7f995132011-10-04 22:41:51 +00002221 DIEArray die_offsets;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002222
Greg Clayton97fbc342011-10-20 22:30:33 +00002223 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002224 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002225 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002226 {
2227 DWARFMappedHash::DIEInfoArray hash_data_array;
2228 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2229 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2230 }
Greg Clayton7f995132011-10-04 22:41:51 +00002231 }
2232 else
2233 {
2234 // Index the DWARF if we haven't already
2235 if (!m_indexed)
2236 Index ();
2237
2238 m_global_index.Find (regex, die_offsets);
2239 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002240
Greg Claytonc685f8e2010-09-15 04:15:46 +00002241 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002242 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002243 assert (sc.module_sp);
2244
Greg Clayton7f995132011-10-04 22:41:51 +00002245 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002246 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002248 DWARFDebugInfo* debug_info = DebugInfo();
2249 for (size_t i=0; i<num_matches; ++i)
2250 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002251 const DIERef& die_ref = die_offsets[i];
2252 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00002253
2254 if (die)
2255 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002256 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257
Greg Clayton6071e6f2015-08-26 22:57:51 +00002258 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002259
Greg Clayton95d87902011-11-11 03:16:25 +00002260 if (variables.GetSize() - original_size >= max_matches)
2261 break;
2262 }
2263 else
2264 {
2265 if (m_using_apple_tables)
2266 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002267 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002268 die_ref.die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002269 }
2270 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002271 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002272 }
2273
2274 // Return the number of variable that were appended to the list
2275 return variables.GetSize() - original_size;
2276}
2277
Greg Claytonaa044962011-10-13 00:59:38 +00002278
Jim Ingham4cda6e02011-10-07 22:23:45 +00002279bool
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002280SymbolFileDWARF::ResolveFunction (const DIERef& die_ref,
Pavel Labatha73d6572015-03-13 10:22:00 +00002281 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00002282 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002283{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002284 DWARFDIE die = DebugInfo()->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002285 return ResolveFunction (die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00002286}
2287
2288
2289bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00002290SymbolFileDWARF::ResolveFunction (const DWARFDIE &orig_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002291 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00002292 SymbolContextList& sc_list)
2293{
Greg Clayton9e315582011-09-02 04:03:59 +00002294 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002295
Greg Clayton6071e6f2015-08-26 22:57:51 +00002296 if (!orig_die)
Greg Claytonaa044962011-10-13 00:59:38 +00002297 return false;
2298
Jim Ingham4cda6e02011-10-07 22:23:45 +00002299 // If we were passed a die that is not a function, just return false...
Greg Clayton6071e6f2015-08-26 22:57:51 +00002300 if (!(orig_die.Tag() == DW_TAG_subprogram || (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002301 return false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002302
2303 DWARFDIE die = orig_die;
2304 DWARFDIE inlined_die;
2305 if (die.Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002306 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002307 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002308
Greg Clayton6071e6f2015-08-26 22:57:51 +00002309 while (1)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002310 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002311 die = die.GetParent();
2312
2313 if (die)
2314 {
2315 if (die.Tag() == DW_TAG_subprogram)
2316 break;
2317 }
2318 else
Jim Ingham4cda6e02011-10-07 22:23:45 +00002319 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002320 }
2321 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002322 assert (die && die.Tag() == DW_TAG_subprogram);
2323 if (GetFunction (die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002324 {
2325 Address addr;
2326 // Parse all blocks if needed
2327 if (inlined_die)
2328 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002329 Block &function_block = sc.function->GetBlock (true);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002330 sc.block = function_block.FindBlockByID (inlined_die.GetID());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002331 if (sc.block == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002332 sc.block = function_block.FindBlockByID (inlined_die.GetOffset());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002333 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002334 addr.Clear();
2335 }
2336 else
2337 {
2338 sc.block = NULL;
2339 addr = sc.function->GetAddressRange().GetBaseAddress();
2340 }
2341
2342 if (addr.IsValid())
2343 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002344 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002345 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002346 }
2347 }
2348
Greg Claytonaa044962011-10-13 00:59:38 +00002349 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002350}
2351
Greg Clayton7f995132011-10-04 22:41:51 +00002352void
2353SymbolFileDWARF::FindFunctions (const ConstString &name,
2354 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002355 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002356 SymbolContextList& sc_list)
2357{
Greg Claytond4a2b372011-09-12 23:21:58 +00002358 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002359 if (name_to_die.Find (name, die_offsets))
2360 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002361 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002362 }
2363}
2364
2365
2366void
2367SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2368 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002369 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002370 SymbolContextList& sc_list)
2371{
2372 DIEArray die_offsets;
2373 if (name_to_die.Find (regex, die_offsets))
2374 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002375 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002376 }
2377}
2378
2379
2380void
2381SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2382 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00002383 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002384 SymbolContextList& sc_list)
2385{
2386 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002387 DWARFMappedHash::DIEInfoArray hash_data_array;
2388 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002389 {
Greg Claytond1767f02011-12-08 02:13:16 +00002390 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00002391 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002392 }
2393}
2394
2395void
2396SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00002397 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002398 SymbolContextList& sc_list)
2399{
2400 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002401 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002402 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002403 for (size_t i=0; i<num_matches; ++i)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002404 ResolveFunction (die_offsets[i], include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002405 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002406}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002407
Jim Ingham4cda6e02011-10-07 22:23:45 +00002408bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002409SymbolFileDWARF::DIEInDeclContext (const CompilerDeclContext *decl_ctx,
Greg Clayton6071e6f2015-08-26 22:57:51 +00002410 const DWARFDIE &die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002411{
2412 // If we have no parent decl context to match this DIE matches, and if the parent
2413 // decl context isn't valid, we aren't trying to look for any particular decl
2414 // context so any die matches.
2415 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2416 return true;
2417
Greg Clayton6071e6f2015-08-26 22:57:51 +00002418 if (die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002419 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002420 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2421 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002422 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002423 CompilerDeclContext actual_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002424 if (actual_decl_ctx)
2425 return actual_decl_ctx == *decl_ctx;
2426 }
2427 }
2428 return false;
2429}
2430
Greg Clayton0c5cd902010-06-28 21:30:43 +00002431uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002432SymbolFileDWARF::FindFunctions (const ConstString &name,
2433 const CompilerDeclContext *parent_decl_ctx,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002434 uint32_t name_type_mask,
2435 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002436 bool append,
2437 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002438{
2439 Timer scoped_timer (__PRETTY_FUNCTION__,
2440 "SymbolFileDWARF::FindFunctions (name = '%s')",
2441 name.AsCString());
2442
Greg Clayton43fe2172013-04-03 02:00:15 +00002443 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
2444 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
2445
Greg Clayton5160ce52013-03-27 23:08:40 +00002446 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002447
2448 if (log)
2449 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002450 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002451 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2452 name.GetCString(),
2453 name_type_mask,
2454 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002455 }
2456
Greg Clayton0c5cd902010-06-28 21:30:43 +00002457 // If we aren't appending the results to this list, then clear the list
2458 if (!append)
2459 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002460
Greg Clayton99558cc42015-08-24 23:46:31 +00002461 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002462 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002463
2464 // If name is empty then we won't find anything.
2465 if (name.IsEmpty())
2466 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002467
2468 // Remember how many sc_list are in the list before we search in case
2469 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002470
Jim Ingham4cda6e02011-10-07 22:23:45 +00002471 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00002472
2473 const uint32_t original_size = sc_list.GetSize();
2474
Jim Ingham4cda6e02011-10-07 22:23:45 +00002475 DWARFDebugInfo* info = DebugInfo();
2476 if (info == NULL)
2477 return 0;
2478
Greg Clayton43fe2172013-04-03 02:00:15 +00002479 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00002480 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002481 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002482 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002483 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002484
2485 DIEArray die_offsets;
2486
2487 uint32_t num_matches = 0;
2488
Greg Clayton43fe2172013-04-03 02:00:15 +00002489 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002490 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002491 // If they asked for the full name, match what they typed. At some point we may
2492 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2493 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002494 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002495 for (uint32_t i = 0; i < num_matches; i++)
2496 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002497 const DIERef& die_ref = die_offsets[i];
2498 DWARFDIE die = info->GetDIE (die_ref);
Greg Claytonaa044962011-10-13 00:59:38 +00002499 if (die)
2500 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002501 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002502 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002503
Greg Clayton6071e6f2015-08-26 22:57:51 +00002504 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002505 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002506 if (ResolveFunction (die, include_inlines, sc_list))
2507 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002508 }
Greg Claytonaa044962011-10-13 00:59:38 +00002509 }
Greg Clayton95d87902011-11-11 03:16:25 +00002510 else
2511 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002512 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002513 die_ref.die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002514 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002515 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002516 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002517
2518 if (name_type_mask & eFunctionNameTypeSelector)
2519 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002520 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00002521 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00002522
Greg Clayton43fe2172013-04-03 02:00:15 +00002523 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2524 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2525 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00002526
Greg Clayton43fe2172013-04-03 02:00:15 +00002527 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00002528 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002529 const DIERef& die_ref = die_offsets[i];
2530 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002531 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00002532 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002533 const char *die_name = die.GetName();
Jim Inghamaa816b82015-09-02 01:59:14 +00002534 if (ObjCLanguage::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00002535 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002536 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002537 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002538 if (ResolveFunction (die, include_inlines, sc_list))
2539 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002540 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002541 }
2542 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002543 else
2544 {
2545 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002546 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002547 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002548 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002549 die_offsets.clear();
2550 }
2551
Greg Clayton99558cc42015-08-24 23:46:31 +00002552 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) || name_type_mask & eFunctionNameTypeBase)
Greg Clayton43fe2172013-04-03 02:00:15 +00002553 {
2554 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2555 // extract the base name, look that up, and if there is any other information in the name we were
2556 // passed in we have to post-filter based on that.
2557
2558 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2559 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2560
2561 for (uint32_t i = 0; i < num_matches; i++)
2562 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002563 const DIERef& die_ref = die_offsets[i];
2564 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002565 if (die)
2566 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002567 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002568 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002569
Greg Clayton43fe2172013-04-03 02:00:15 +00002570
2571 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002572 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() && ResolveFunction (die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002573 {
2574 bool keep_die = true;
2575 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
2576 {
2577 // We are looking for either basenames or methods, so we need to
2578 // trim out the ones we won't want by looking at the type
2579 SymbolContext sc;
2580 if (sc_list.GetLastContext(sc))
2581 {
2582 if (sc.block)
2583 {
2584 // We have an inlined function
2585 }
2586 else if (sc.function)
2587 {
2588 Type *type = sc.function->GetType();
2589
Sean Callananc370a8a2013-09-18 22:59:55 +00002590 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00002591 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002592 CompilerDeclContext decl_ctx = GetDeclContextContainingUID (type->GetID());
2593 if (decl_ctx.IsStructUnionOrClass())
Greg Clayton43fe2172013-04-03 02:00:15 +00002594 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002595 if (name_type_mask & eFunctionNameTypeBase)
2596 {
2597 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2598 keep_die = false;
2599 }
2600 }
2601 else
2602 {
2603 if (name_type_mask & eFunctionNameTypeMethod)
2604 {
2605 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2606 keep_die = false;
2607 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002608 }
2609 }
2610 else
2611 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002612 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002613 die_ref.die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002614 }
2615 }
2616 }
2617 }
2618 if (keep_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002619 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002620 }
2621 }
2622 else
2623 {
2624 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002625 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002626 }
2627 }
2628 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002629 }
2630 }
Greg Clayton7f995132011-10-04 22:41:51 +00002631 }
2632 else
2633 {
2634
2635 // Index the DWARF if we haven't already
2636 if (!m_indexed)
2637 Index ();
2638
Greg Clayton7f995132011-10-04 22:41:51 +00002639 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00002640 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002641 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002642
Ed Mastefc7baa02013-09-09 18:00:45 +00002643 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00002644 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00002645 // If we didn't find any functions in the global namespace try
2646 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00002647 // functions that have a namespace but keep functions which
2648 // have an anonymous namespace
2649 // TODO: The arch in the object file isn't correct for MSVC
2650 // binaries on windows, we should find a way to make it
2651 // correct and handle those symbols as well.
Matt Kopecd6089962013-05-10 17:53:48 +00002652 if (sc_list.GetSize() == 0)
2653 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002654 ArchSpec arch;
Greg Clayton99558cc42015-08-24 23:46:31 +00002655 if (!parent_decl_ctx &&
Robert Flackeb83fab2015-05-15 18:59:59 +00002656 GetObjectFile()->GetArchitecture(arch) &&
2657 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
2658 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00002659 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002660 SymbolContextList temp_sc_list;
2661 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00002662 SymbolContext sc;
2663 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
2664 {
2665 if (temp_sc_list.GetContextAtIndex(i, sc))
2666 {
Matt Kopeca189d492013-05-10 22:55:24 +00002667 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
2668 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00002669 // Mangled names on Linux and FreeBSD are of the form:
2670 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00002671 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2672 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00002673 {
2674 sc_list.Append(sc);
2675 }
2676 }
2677 }
2678 }
2679 }
Matt Kopecd6089962013-05-10 17:53:48 +00002680 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002681 DIEArray die_offsets;
Greg Clayton43fe2172013-04-03 02:00:15 +00002682 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002683 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002684 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002685 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002686 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002687 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002688 if (die)
2689 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002690 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002691 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002692
Greg Claytonaa044962011-10-13 00:59:38 +00002693 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002694 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002695 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002696 if (ResolveFunction (die, include_inlines, sc_list))
2697 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002698 }
Greg Claytonaa044962011-10-13 00:59:38 +00002699 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002700 }
2701 die_offsets.clear();
2702 }
2703
Greg Clayton43fe2172013-04-03 02:00:15 +00002704 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002705 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002706 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Sean Callanan213fdb82011-10-13 01:49:10 +00002707 return 0; // no methods in namespaces
2708
Greg Clayton43fe2172013-04-03 02:00:15 +00002709 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002710 {
Greg Claytonaa044962011-10-13 00:59:38 +00002711 for (uint32_t i = 0; i < num_base; i++)
2712 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002713 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002714 if (die)
2715 {
Greg Claytonaa044962011-10-13 00:59:38 +00002716 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002717 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002718 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002719 if (ResolveFunction (die, include_inlines, sc_list))
2720 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002721 }
Greg Claytonaa044962011-10-13 00:59:38 +00002722 }
2723 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002724 }
2725 die_offsets.clear();
2726 }
Greg Clayton7f995132011-10-04 22:41:51 +00002727
Greg Clayton99558cc42015-08-24 23:46:31 +00002728 if ((name_type_mask & eFunctionNameTypeSelector) && (!parent_decl_ctx || !parent_decl_ctx->IsValid()))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002729 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002730 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002731 }
2732
Greg Clayton4d01ace2011-09-29 16:58:15 +00002733 }
2734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002736 const uint32_t num_matches = sc_list.GetSize() - original_size;
2737
2738 if (log && num_matches > 0)
2739 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002740 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00002741 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002742 name.GetCString(),
2743 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00002744 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00002745 append,
2746 num_matches);
2747 }
2748 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002749}
2750
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002751uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00002752SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002753{
2754 Timer scoped_timer (__PRETTY_FUNCTION__,
2755 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2756 regex.GetText());
2757
Greg Clayton5160ce52013-03-27 23:08:40 +00002758 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002759
2760 if (log)
2761 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002762 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002763 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2764 regex.GetText(),
2765 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002766 }
2767
2768
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002769 // If we aren't appending the results to this list, then clear the list
2770 if (!append)
2771 sc_list.Clear();
2772
2773 // Remember how many sc_list are in the list before we search in case
2774 // we are appending the results to a variable list.
2775 uint32_t original_size = sc_list.GetSize();
2776
Greg Clayton97fbc342011-10-20 22:30:33 +00002777 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002778 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002779 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00002780 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002781 }
2782 else
2783 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002784 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002785 if (!m_indexed)
2786 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002787
Pavel Labatha73d6572015-03-13 10:22:00 +00002788 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002789
Pavel Labatha73d6572015-03-13 10:22:00 +00002790 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002791 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002792
2793 // Return the number of variable that were appended to the list
2794 return sc_list.GetSize() - original_size;
2795}
Jim Ingham318c9f22011-08-26 19:44:13 +00002796
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002797uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00002798SymbolFileDWARF::FindTypes (const SymbolContext& sc,
2799 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00002800 const CompilerDeclContext *parent_decl_ctx,
Greg Claytond1767f02011-12-08 02:13:16 +00002801 bool append,
2802 uint32_t max_matches,
2803 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002804{
Greg Claytonc685f8e2010-09-15 04:15:46 +00002805 DWARFDebugInfo* info = DebugInfo();
2806 if (info == NULL)
2807 return 0;
2808
Greg Clayton5160ce52013-03-27 23:08:40 +00002809 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002810
Greg Clayton21f2a492011-10-06 00:09:08 +00002811 if (log)
2812 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002813 if (parent_decl_ctx)
Greg Clayton5160ce52013-03-27 23:08:40 +00002814 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002815 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list)",
Greg Clayton437a1352012-04-09 22:43:43 +00002816 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002817 static_cast<const void*>(parent_decl_ctx),
2818 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002819 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00002820 else
Greg Clayton5160ce52013-03-27 23:08:40 +00002821 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002822 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002823 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00002824 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002825 }
2826
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002827 // If we aren't appending the results to this list, then clear the list
2828 if (!append)
2829 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002830
Greg Clayton99558cc42015-08-24 23:46:31 +00002831 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002832 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002833
Greg Claytond4a2b372011-09-12 23:21:58 +00002834 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002835
Greg Clayton97fbc342011-10-20 22:30:33 +00002836 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002837 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002838 if (m_apple_types_ap.get())
2839 {
2840 const char *name_cstr = name.GetCString();
2841 m_apple_types_ap->FindByName (name_cstr, die_offsets);
2842 }
Greg Clayton7f995132011-10-04 22:41:51 +00002843 }
2844 else
2845 {
2846 if (!m_indexed)
2847 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002848
Greg Clayton7f995132011-10-04 22:41:51 +00002849 m_type_index.Find (name, die_offsets);
2850 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002851
Greg Clayton437a1352012-04-09 22:43:43 +00002852 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00002853
Greg Clayton437a1352012-04-09 22:43:43 +00002854 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002855 {
Greg Clayton7f995132011-10-04 22:41:51 +00002856 const uint32_t initial_types_size = types.GetSize();
Greg Claytond4a2b372011-09-12 23:21:58 +00002857 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002858 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002859 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002860 const DIERef& die_ref = die_offsets[i];
2861 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Claytond4a2b372011-09-12 23:21:58 +00002862
Greg Clayton95d87902011-11-11 03:16:25 +00002863 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00002864 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002865 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002866 continue; // The containing decl contexts don't match
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002867
Greg Clayton6071e6f2015-08-26 22:57:51 +00002868 Type *matching_type = ResolveType (die);
Greg Clayton95d87902011-11-11 03:16:25 +00002869 if (matching_type)
2870 {
2871 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00002872 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00002873 if (types.GetSize() >= max_matches)
2874 break;
2875 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00002876 }
Greg Clayton95d87902011-11-11 03:16:25 +00002877 else
2878 {
2879 if (m_using_apple_tables)
2880 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002881 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002882 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002883 }
2884 }
2885
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002886 }
Greg Clayton437a1352012-04-09 22:43:43 +00002887 const uint32_t num_matches = types.GetSize() - initial_types_size;
2888 if (log && num_matches)
2889 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002890 if (parent_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00002891 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002892 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002893 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002894 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002895 static_cast<const void*>(parent_decl_ctx),
2896 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002897 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002898 num_matches);
2899 }
2900 else
2901 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002902 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002903 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002904 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002905 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002906 num_matches);
2907 }
2908 }
2909 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002910 }
Greg Clayton7f995132011-10-04 22:41:51 +00002911 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002912}
2913
2914
Greg Clayton99558cc42015-08-24 23:46:31 +00002915CompilerDeclContext
Greg Clayton96d7d742010-11-10 23:42:09 +00002916SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00002917 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00002918 const CompilerDeclContext *parent_decl_ctx)
Greg Clayton96d7d742010-11-10 23:42:09 +00002919{
Greg Clayton5160ce52013-03-27 23:08:40 +00002920 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002921
2922 if (log)
2923 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002924 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002925 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2926 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00002927 }
2928
Greg Clayton99558cc42015-08-24 23:46:31 +00002929 CompilerDeclContext namespace_decl_ctx;
2930
2931 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2932 return namespace_decl_ctx;
2933
2934
Greg Clayton96d7d742010-11-10 23:42:09 +00002935 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00002936 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00002937 {
Greg Clayton7f995132011-10-04 22:41:51 +00002938 DIEArray die_offsets;
2939
Greg Clayton526e5af2010-11-13 03:52:47 +00002940 // Index if we already haven't to make sure the compile units
2941 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00002942 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002943 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002944 if (m_apple_namespaces_ap.get())
2945 {
2946 const char *name_cstr = name.GetCString();
2947 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
2948 }
Greg Clayton7f995132011-10-04 22:41:51 +00002949 }
2950 else
2951 {
2952 if (!m_indexed)
2953 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00002954
Greg Clayton7f995132011-10-04 22:41:51 +00002955 m_namespace_index.Find (name, die_offsets);
2956 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002957
Greg Clayton7f995132011-10-04 22:41:51 +00002958 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002959 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00002960 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002961 DWARFDebugInfo* debug_info = DebugInfo();
2962 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00002963 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002964 const DIERef& die_ref = die_offsets[i];
2965 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002966
Greg Clayton95d87902011-11-11 03:16:25 +00002967 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00002968 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002969 if (!DIEInDeclContext (parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002970 continue; // The containing decl contexts don't match
Greg Clayton95d87902011-11-11 03:16:25 +00002971
Greg Clayton261ac3f2015-08-28 01:01:03 +00002972 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2973 if (dwarf_ast)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002974 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002975 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002976 if (namespace_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002977 break;
Greg Clayton95d87902011-11-11 03:16:25 +00002978 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002979 }
Greg Clayton95d87902011-11-11 03:16:25 +00002980 else
2981 {
2982 if (m_using_apple_tables)
2983 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002984 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002985 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002986 }
2987 }
2988
Greg Clayton526e5af2010-11-13 03:52:47 +00002989 }
2990 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002991 }
Greg Clayton99558cc42015-08-24 23:46:31 +00002992 if (log && namespace_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00002993 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002994 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002995 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => CompilerDeclContext(%p/%p) \"%s\"",
Greg Clayton437a1352012-04-09 22:43:43 +00002996 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002997 static_cast<const void*>(namespace_decl_ctx.GetTypeSystem()),
2998 static_cast<const void*>(namespace_decl_ctx.GetOpaqueDeclContext()),
2999 namespace_decl_ctx.GetName().AsCString("<NULL>"));
Greg Clayton437a1352012-04-09 22:43:43 +00003000 }
3001
Greg Clayton99558cc42015-08-24 23:46:31 +00003002 return namespace_decl_ctx;
Greg Clayton96d7d742010-11-10 23:42:09 +00003003}
3004
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003005TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003006SymbolFileDWARF::GetTypeForDIE (const DWARFDIE &die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003007{
3008 TypeSP type_sp;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003009 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003010 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003011 Type *type_ptr = GetDIEToType().lookup (die.GetDIE());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003012 if (type_ptr == NULL)
3013 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003014 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
Greg Claytonca512b32011-01-14 04:54:56 +00003015 assert (lldb_cu);
3016 SymbolContext sc(lldb_cu);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003017 type_sp = ParseType(sc, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 }
3019 else if (type_ptr != DIE_IS_BEING_PARSED)
3020 {
3021 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003022 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003023 }
3024
3025 }
3026 return type_sp;
3027}
3028
Greg Clayton2bc22f82011-09-30 03:20:47 +00003029
Greg Clayton6071e6f2015-08-26 22:57:51 +00003030DWARFDIE
3031SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFDIE &orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003032{
Greg Clayton6071e6f2015-08-26 22:57:51 +00003033 if (orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003034 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003035 DWARFDIE die = orig_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003036
Greg Clayton6071e6f2015-08-26 22:57:51 +00003037 while (die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003038 {
3039 // If this is the original DIE that we are searching for a declaration
3040 // for, then don't look in the cache as we don't want our own decl
3041 // context to be our decl context...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003042 if (orig_die != die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003043 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003044 switch (die.Tag())
Greg Clayton2bc22f82011-09-30 03:20:47 +00003045 {
3046 case DW_TAG_compile_unit:
3047 case DW_TAG_namespace:
3048 case DW_TAG_structure_type:
3049 case DW_TAG_union_type:
3050 case DW_TAG_class_type:
3051 return die;
3052
3053 default:
3054 break;
3055 }
3056 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003057
3058 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3059 if (spec_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003060 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003061 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3062 if (decl_ctx_die)
3063 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003064 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003065
3066 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3067 if (abs_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003068 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003069 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3070 if (decl_ctx_die)
3071 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003072 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003073
3074 die = die.GetParent();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003075 }
3076 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003077 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003078}
3079
3080
Greg Clayton901c5ca2011-12-03 04:40:03 +00003081Symbol *
3082SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3083{
3084 Symbol *objc_class_symbol = NULL;
3085 if (m_obj_file)
3086 {
Greg Clayton3046e662013-07-10 01:23:25 +00003087 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003088 if (symtab)
3089 {
3090 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3091 eSymbolTypeObjCClass,
3092 Symtab::eDebugNo,
3093 Symtab::eVisibilityAny);
3094 }
3095 }
3096 return objc_class_symbol;
3097}
3098
Greg Claytonc7f03b62012-01-12 04:33:28 +00003099// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3100// then we can end up looking through all class types for a complete type and never find
3101// the full definition. We need to know if this attribute is supported, so we determine
3102// this here and cache th result. We also need to worry about the debug map DWARF file
3103// if we are doing darwin DWARF in .o file debugging.
3104bool
3105SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3106{
3107 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3108 {
3109 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3110 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3111 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3112 else
3113 {
3114 DWARFDebugInfo* debug_info = DebugInfo();
3115 const uint32_t num_compile_units = GetNumCompileUnits();
3116 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3117 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003118 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3119 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003120 {
3121 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3122 break;
3123 }
3124 }
3125 }
Greg Clayton1f746072012-08-29 21:13:06 +00003126 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003127 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3128 }
3129 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3130}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003131
3132// This function can be used when a DIE is found that is a forward declaration
3133// DIE and we want to try and find a type that has the complete definition.
3134TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003135SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
Greg Claytonc7f03b62012-01-12 04:33:28 +00003136 const ConstString &type_name,
3137 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003138{
3139
3140 TypeSP type_sp;
3141
Greg Claytonc7f03b62012-01-12 04:33:28 +00003142 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003143 return type_sp;
3144
3145 DIEArray die_offsets;
3146
3147 if (m_using_apple_tables)
3148 {
3149 if (m_apple_types_ap.get())
3150 {
3151 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003152 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003153 }
3154 }
3155 else
3156 {
3157 if (!m_indexed)
3158 Index ();
3159
3160 m_type_index.Find (type_name, die_offsets);
3161 }
3162
Greg Clayton901c5ca2011-12-03 04:40:03 +00003163 const size_t num_matches = die_offsets.size();
3164
Greg Clayton901c5ca2011-12-03 04:40:03 +00003165 if (num_matches)
3166 {
3167 DWARFDebugInfo* debug_info = DebugInfo();
3168 for (size_t i=0; i<num_matches; ++i)
3169 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003170 const DIERef& die_ref = die_offsets[i];
3171 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003172
3173 if (type_die)
3174 {
3175 bool try_resolving_type = false;
3176
3177 // Don't try and resolve the DIE we are looking for with the DIE itself!
3178 if (type_die != die)
3179 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003180 switch (type_die.Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003181 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003182 case DW_TAG_class_type:
3183 case DW_TAG_structure_type:
3184 try_resolving_type = true;
3185 break;
3186 default:
3187 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003188 }
3189 }
3190
3191 if (try_resolving_type)
3192 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003193 if (must_be_implementation && type_die.Supports_DW_AT_APPLE_objc_complete_type())
3194 try_resolving_type = type_die.GetAttributeValueAsUnsigned (DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003195
3196 if (try_resolving_type)
3197 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003198 Type *resolved_type = ResolveType (type_die, false);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003199 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3200 {
Ed Mastea0191d12013-10-17 20:42:56 +00003201 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton6071e6f2015-08-26 22:57:51 +00003202 die.GetID(),
Jim Ingham4af59612014-12-19 19:20:44 +00003203 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003204 type_die.GetID(),
3205 type_cu->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003206
Greg Claytonc7f03b62012-01-12 04:33:28 +00003207 if (die)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003208 GetDIEToType()[die.GetDIE()] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00003209 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003210 break;
3211 }
3212 }
3213 }
3214 }
3215 else
3216 {
3217 if (m_using_apple_tables)
3218 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003219 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003220 die_ref.die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003221 }
3222 }
3223
3224 }
3225 }
3226 return type_sp;
3227}
3228
Greg Claytona8022fa2012-04-24 21:22:41 +00003229
Greg Clayton80c26302012-02-05 06:12:47 +00003230//----------------------------------------------------------------------
3231// This function helps to ensure that the declaration contexts match for
3232// two different DIEs. Often times debug information will refer to a
3233// forward declaration of a type (the equivalent of "struct my_struct;".
3234// There will often be a declaration of that type elsewhere that has the
3235// full definition. When we go looking for the full type "my_struct", we
3236// will find one or more matches in the accelerator tables and we will
3237// then need to make sure the type was in the same declaration context
3238// as the original DIE. This function can efficiently compare two DIEs
3239// and will return true when the declaration context matches, and false
3240// when they don't.
3241//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00003242bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00003243SymbolFileDWARF::DIEDeclContextsMatch (const DWARFDIE &die1,
3244 const DWARFDIE &die2)
Greg Clayton890ff562012-02-02 05:48:16 +00003245{
Greg Claytona8022fa2012-04-24 21:22:41 +00003246 if (die1 == die2)
3247 return true;
3248
Greg Clayton890ff562012-02-02 05:48:16 +00003249 DWARFDIECollection decl_ctx_1;
3250 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00003251 //The declaration DIE stack is a stack of the declaration context
3252 // DIEs all the way back to the compile unit. If a type "T" is
3253 // declared inside a class "B", and class "B" is declared inside
3254 // a class "A" and class "A" is in a namespace "lldb", and the
3255 // namespace is in a compile unit, there will be a stack of DIEs:
3256 //
3257 // [0] DW_TAG_class_type for "B"
3258 // [1] DW_TAG_class_type for "A"
3259 // [2] DW_TAG_namespace for "lldb"
3260 // [3] DW_TAG_compile_unit for the source file.
3261 //
3262 // We grab both contexts and make sure that everything matches
3263 // all the way back to the compiler unit.
3264
3265 // First lets grab the decl contexts for both DIEs
Greg Clayton6071e6f2015-08-26 22:57:51 +00003266 die1.GetDeclContextDIEs (decl_ctx_1);
3267 die2.GetDeclContextDIEs (decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00003268 // Make sure the context arrays have the same size, otherwise
3269 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00003270 const size_t count1 = decl_ctx_1.Size();
3271 const size_t count2 = decl_ctx_2.Size();
3272 if (count1 != count2)
3273 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00003274
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003275 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00003276 // compile unit. If they don't, then we are done.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003277 DWARFDIE decl_ctx_die1;
3278 DWARFDIE decl_ctx_die2;
Greg Clayton890ff562012-02-02 05:48:16 +00003279 size_t i;
3280 for (i=0; i<count1; i++)
3281 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003282 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3283 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3284 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
Greg Clayton890ff562012-02-02 05:48:16 +00003285 return false;
3286 }
Greg Clayton890ff562012-02-02 05:48:16 +00003287#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003288
3289 // Make sure the top item in the decl context die array is always
3290 // DW_TAG_compile_unit. If it isn't then something went wrong in
Greg Clayton5ce1a842015-08-27 18:09:44 +00003291 // the DWARFDIE::GetDeclContextDIEs() function...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003292 assert (decl_ctx_1.GetDIEAtIndex (count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003293
Greg Clayton890ff562012-02-02 05:48:16 +00003294#endif
3295 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00003296 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00003297 for (i=0; i<count1 - 1; i++)
3298 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003299 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3300 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3301 const char *name1 = decl_ctx_die1.GetName();
3302 const char *name2 = decl_ctx_die2.GetName();
Greg Clayton890ff562012-02-02 05:48:16 +00003303 // If the string was from a DW_FORM_strp, then the pointer will often
3304 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00003305 if (name1 == name2)
3306 continue;
3307
3308 // Name pointers are not equal, so only compare the strings
3309 // if both are not NULL.
3310 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00003311 {
Greg Clayton5569e642012-02-06 01:44:54 +00003312 // If the strings don't compare, we are done...
3313 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00003314 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00003315 }
3316 else
3317 {
3318 // One name was NULL while the other wasn't
3319 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003320 }
3321 }
Greg Clayton80c26302012-02-05 06:12:47 +00003322 // We made it through all of the checks and the declaration contexts
3323 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00003324 return true;
3325}
Greg Clayton220a0072011-12-09 08:48:30 +00003326
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003327
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00003329SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
3330{
3331 TypeSP type_sp;
3332
3333 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3334 if (dwarf_decl_ctx_count > 0)
3335 {
3336 const ConstString type_name(dwarf_decl_ctx[0].name);
3337 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
3338
3339 if (type_name)
3340 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003341 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00003342 if (log)
3343 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003344 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003345 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
3346 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3347 dwarf_decl_ctx.GetQualifiedName());
3348 }
3349
3350 DIEArray die_offsets;
3351
3352 if (m_using_apple_tables)
3353 {
3354 if (m_apple_types_ap.get())
3355 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003356 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
3357 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
3358 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00003359 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003360 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3361 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
3362 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003363 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003364 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3365 }
3366 else if (has_tag)
3367 {
3368 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003369 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00003370 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
3371 }
3372 else
3373 {
3374 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3375 }
3376 }
3377 }
3378 else
3379 {
3380 if (!m_indexed)
3381 Index ();
3382
3383 m_type_index.Find (type_name, die_offsets);
3384 }
3385
3386 const size_t num_matches = die_offsets.size();
3387
3388
Greg Claytona8022fa2012-04-24 21:22:41 +00003389 if (num_matches)
3390 {
3391 DWARFDebugInfo* debug_info = DebugInfo();
3392 for (size_t i=0; i<num_matches; ++i)
3393 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003394 const DIERef& die_ref = die_offsets[i];
3395 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Claytona8022fa2012-04-24 21:22:41 +00003396
3397 if (type_die)
3398 {
3399 bool try_resolving_type = false;
3400
3401 // Don't try and resolve the DIE we are looking for with the DIE itself!
Greg Clayton6071e6f2015-08-26 22:57:51 +00003402 const dw_tag_t type_tag = type_die.Tag();
Greg Claytona8022fa2012-04-24 21:22:41 +00003403 // Make sure the tags match
3404 if (type_tag == tag)
3405 {
3406 // The tags match, lets try resolving this type
3407 try_resolving_type = true;
3408 }
3409 else
3410 {
3411 // The tags don't match, but we need to watch our for a
3412 // forward declaration for a struct and ("struct foo")
3413 // ends up being a class ("class foo { ... };") or
3414 // vice versa.
3415 switch (type_tag)
3416 {
3417 case DW_TAG_class_type:
3418 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3419 try_resolving_type = (tag == DW_TAG_structure_type);
3420 break;
3421 case DW_TAG_structure_type:
3422 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3423 try_resolving_type = (tag == DW_TAG_class_type);
3424 break;
3425 default:
3426 // Tags don't match, don't event try to resolve
3427 // using this type whose name matches....
3428 break;
3429 }
3430 }
3431
3432 if (try_resolving_type)
3433 {
3434 DWARFDeclContext type_dwarf_decl_ctx;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003435 type_die.GetDWARFDeclContext (type_dwarf_decl_ctx);
Greg Claytona8022fa2012-04-24 21:22:41 +00003436
3437 if (log)
3438 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003439 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003440 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
3441 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3442 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003443 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003444 type_dwarf_decl_ctx.GetQualifiedName());
3445 }
3446
3447 // Make sure the decl contexts match all the way up
3448 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
3449 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003450 Type *resolved_type = ResolveType (type_die, false);
Greg Claytona8022fa2012-04-24 21:22:41 +00003451 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3452 {
3453 type_sp = resolved_type->shared_from_this();
3454 break;
3455 }
3456 }
3457 }
3458 else
3459 {
3460 if (log)
3461 {
3462 std::string qualified_name;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003463 type_die.GetQualifiedName(qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00003464 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003465 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
3466 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3467 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003468 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003469 qualified_name.c_str());
3470 }
3471 }
3472 }
3473 else
3474 {
3475 if (m_using_apple_tables)
3476 {
3477 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003478 die_ref.die_offset, type_name.GetCString());
Greg Claytona8022fa2012-04-24 21:22:41 +00003479 }
3480 }
3481
3482 }
3483 }
3484 }
3485 }
3486 return type_sp;
3487}
3488
Greg Claytona8022fa2012-04-24 21:22:41 +00003489TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003490SymbolFileDWARF::ParseType (const SymbolContext& sc, const DWARFDIE &die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003491{
Greg Clayton196e8cd2015-08-17 20:31:46 +00003492 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003493
Greg Clayton6071e6f2015-08-26 22:57:51 +00003494 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003495 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003496 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
3497
3498 if (type_system)
Greg Clayton196e8cd2015-08-17 20:31:46 +00003499 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003500 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3501 if (dwarf_ast)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003502 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003503 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3504 type_sp = dwarf_ast->ParseTypeFromDWARF (sc, die, log, type_is_new_ptr);
3505 if (type_sp)
3506 {
3507 TypeList* type_list = GetTypeList();
3508 if (type_list)
3509 type_list->Insert(type_sp);
3510 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003511 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003512 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003513 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003514
3515 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003516}
3517
3518size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003519SymbolFileDWARF::ParseTypes
3520(
3521 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003522 const DWARFDIE &orig_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003523 bool parse_siblings,
3524 bool parse_children
3525)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003526{
3527 size_t types_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003528 DWARFDIE die = orig_die;
3529 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003530 {
3531 bool type_is_new = false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003532 if (ParseType(sc, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003533 {
3534 if (type_is_new)
3535 ++types_added;
3536 }
3537
Greg Clayton6071e6f2015-08-26 22:57:51 +00003538 if (parse_children && die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003539 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003540 if (die.Tag() == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003541 {
3542 SymbolContext child_sc(sc);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003543 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3544 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003545 }
3546 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003547 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003548 }
3549
3550 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003551 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003552 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003553 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003554 }
3555 return types_added;
3556}
3557
3558
3559size_t
3560SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3561{
3562 assert(sc.comp_unit && sc.function);
3563 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003564 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003565 if (dwarf_cu)
3566 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003567 const dw_offset_t function_die_offset = sc.function->GetID();
3568 DWARFDIE function_die = dwarf_cu->GetDIE (function_die_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003569 if (function_die)
3570 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003571 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003572 }
3573 }
3574
3575 return functions_added;
3576}
3577
3578
3579size_t
3580SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3581{
3582 // At least a compile unit must be valid
3583 assert(sc.comp_unit);
3584 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003585 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003586 if (dwarf_cu)
3587 {
3588 if (sc.function)
3589 {
3590 dw_offset_t function_die_offset = sc.function->GetID();
Greg Clayton6071e6f2015-08-26 22:57:51 +00003591 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3592 if (func_die && func_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003593 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003594 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003595 }
3596 }
3597 else
3598 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003599 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3600 if (dwarf_cu_die && dwarf_cu_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003601 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003602 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003603 }
3604 }
3605 }
3606
3607 return types_added;
3608}
3609
3610size_t
3611SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3612{
3613 if (sc.comp_unit != NULL)
3614 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003615 DWARFDebugInfo* info = DebugInfo();
3616 if (info == NULL)
3617 return 0;
3618
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003619 if (sc.function)
3620 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003621 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID()));
Greg Clayton9422dd62013-03-04 21:46:16 +00003622
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003623 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress (DW_AT_low_pc, LLDB_INVALID_ADDRESS);
Greg Claytonc7bece562013-01-25 18:06:21 +00003624 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003625 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003626 const size_t num_variables = ParseVariables(sc, function_die.GetFirstChild(), func_lo_pc, true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00003627
Greg Claytone38a5ed2012-01-05 03:57:59 +00003628 // Let all blocks know they have parse all their variables
3629 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3630 return num_variables;
3631 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003632 }
3633 else if (sc.comp_unit)
3634 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003635 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Greg Clayton9422dd62013-03-04 21:46:16 +00003636
3637 if (dwarf_cu == NULL)
3638 return 0;
3639
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640 uint32_t vars_added = 0;
3641 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3642
3643 if (variables.get() == NULL)
3644 {
3645 variables.reset(new VariableList());
3646 sc.comp_unit->SetVariableList(variables);
3647
Greg Claytond4a2b372011-09-12 23:21:58 +00003648 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00003649 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003650 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003651 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003652 {
3653 DWARFMappedHash::DIEInfoArray hash_data_array;
3654 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
3655 dwarf_cu->GetNextCompileUnitOffset(),
3656 hash_data_array))
3657 {
3658 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3659 }
3660 }
Greg Clayton7f995132011-10-04 22:41:51 +00003661 }
3662 else
3663 {
3664 // Index if we already haven't to make sure the compile units
3665 // get indexed and make their global DIE index list
3666 if (!m_indexed)
3667 Index ();
3668
3669 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
Greg Clayton7f995132011-10-04 22:41:51 +00003670 die_offsets);
3671 }
3672
3673 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003674 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003675 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003676 DWARFDebugInfo* debug_info = DebugInfo();
3677 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003678 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003679 const DIERef& die_ref = die_offsets[i];
3680 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00003681 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003682 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003683 VariableSP var_sp (ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
Greg Clayton95d87902011-11-11 03:16:25 +00003684 if (var_sp)
3685 {
3686 variables->AddVariableIfUnique (var_sp);
3687 ++vars_added;
3688 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003689 }
Greg Clayton95d87902011-11-11 03:16:25 +00003690 else
3691 {
3692 if (m_using_apple_tables)
3693 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003694 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_ref.die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00003695 }
3696 }
3697
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003698 }
3699 }
3700 }
3701 return vars_added;
3702 }
3703 }
3704 return 0;
3705}
3706
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003707VariableSP
3708SymbolFileDWARF::ParseVariableDIE
3709(
3710 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003711 const DWARFDIE &die,
Greg Clayton016a95e2010-09-14 02:20:48 +00003712 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003713)
3714{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003715 if (die.GetDWARF() != this)
3716 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
3717
Greg Clayton6071e6f2015-08-26 22:57:51 +00003718 VariableSP var_sp;
3719 if (!die)
3720 return var_sp;
3721
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003722 var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton83c5cd92010-11-14 22:13:40 +00003723 if (var_sp)
3724 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003725
Greg Clayton6071e6f2015-08-26 22:57:51 +00003726 const dw_tag_t tag = die.Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00003727 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003728
3729 if ((tag == DW_TAG_variable) ||
3730 (tag == DW_TAG_constant) ||
3731 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003732 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003733 DWARFAttributes attributes;
3734 const size_t num_attributes = die.GetAttributes(attributes);
Greg Clayton7f995132011-10-04 22:41:51 +00003735 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736 {
Greg Clayton7f995132011-10-04 22:41:51 +00003737 const char *name = NULL;
3738 const char *mangled = NULL;
3739 Declaration decl;
3740 uint32_t i;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003741 DWARFFormValue type_die_form;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003742 DWARFExpression location(die.GetCU());
Greg Clayton7f995132011-10-04 22:41:51 +00003743 bool is_external = false;
3744 bool is_artificial = false;
3745 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003746 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00003747 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00003748 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00003749
3750 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003751 {
Greg Clayton7f995132011-10-04 22:41:51 +00003752 dw_attr_t attr = attributes.AttributeAtIndex(i);
3753 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00003754
Greg Clayton6071e6f2015-08-26 22:57:51 +00003755 if (attributes.ExtractFormValueAtIndex(i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003756 {
Greg Clayton7f995132011-10-04 22:41:51 +00003757 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003758 {
Greg Clayton7f995132011-10-04 22:41:51 +00003759 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3760 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3761 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003762 case DW_AT_name: name = form_value.AsCString(); break;
Greg Clayton71415542012-12-08 00:24:40 +00003763 case DW_AT_linkage_name:
Greg Clayton6071e6f2015-08-26 22:57:51 +00003764 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003765 case DW_AT_type: type_die_form = form_value; break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00003766 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00003767 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003768 // If we have already found a DW_AT_location attribute, ignore this attribute.
3769 if (!has_explicit_location)
3770 {
3771 location_is_const_value_data = true;
3772 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00003773 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003774 if (DWARFFormValue::IsBlockForm(form_value.Form()))
3775 {
3776 // Retrieve the value as a block expression.
3777 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3778 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00003779 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003780 }
3781 else if (DWARFFormValue::IsDataForm(form_value.Form()))
3782 {
3783 // Retrieve the value as a data expression.
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003784 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3785 DWARFFormValue::GetFixedFormSizesForAddressSize (
3786 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3787 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003788 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003789 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Enrico Granata4ec130d2014-08-11 19:16:35 +00003790 if (data_length == 0)
3791 {
3792 const uint8_t *data_pointer = form_value.BlockData();
3793 if (data_pointer)
3794 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00003795 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00003796 }
3797 else if (DWARFFormValue::IsDataForm(form_value.Form()))
3798 {
3799 // we need to get the byte size of the type later after we create the variable
3800 const_value = form_value;
3801 }
3802 }
3803 else
3804 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003805 }
3806 else
3807 {
3808 // Retrieve the value as a string expression.
3809 if (form_value.Form() == DW_FORM_strp)
3810 {
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003811 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3812 DWARFFormValue::GetFixedFormSizesForAddressSize (
3813 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3814 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003815 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003816 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Richard Mitton0a558352013-10-17 21:14:00 +00003817 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003818 }
3819 else
3820 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003821 const char *str = form_value.AsCString();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003822 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
3823 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00003824 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003825 }
3826 }
3827 }
3828 break;
Greg Clayton7f995132011-10-04 22:41:51 +00003829 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003830 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003831 location_is_const_value_data = false;
3832 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00003833 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003834 {
Ed Masteeeae7212013-10-24 20:43:47 +00003835 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00003836
3837 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3838 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00003839 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00003840 }
3841 else
3842 {
Ed Masteeeae7212013-10-24 20:43:47 +00003843 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00003844 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3845
3846 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
3847 if (loc_list_length > 0)
3848 {
Richard Mitton0a558352013-10-17 21:14:00 +00003849 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00003850 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00003851 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00003852 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003853 }
3854 }
Greg Clayton7f995132011-10-04 22:41:51 +00003855 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003856
Greg Clayton1c8ef472013-04-05 23:27:21 +00003857 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00003858 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00003859 case DW_AT_declaration:
3860 case DW_AT_description:
3861 case DW_AT_endianity:
3862 case DW_AT_segment:
3863 case DW_AT_start_scope:
3864 case DW_AT_visibility:
3865 default:
3866 case DW_AT_abstract_origin:
3867 case DW_AT_sibling:
3868 case DW_AT_specification:
3869 break;
3870 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003871 }
3872 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003873
Greg Clayton6071e6f2015-08-26 22:57:51 +00003874 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3875 const dw_tag_t parent_tag = die.GetParent().Tag();
3876 bool is_static_member = parent_tag == DW_TAG_compile_unit && (parent_context_die.Tag() == DW_TAG_class_type || parent_context_die.Tag() == DW_TAG_structure_type);
Paul Herman10bc1a42015-08-18 22:46:57 +00003877
Greg Clayton9e9f2192013-05-17 00:55:28 +00003878 ValueType scope = eValueTypeInvalid;
3879
Greg Clayton6071e6f2015-08-26 22:57:51 +00003880 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Greg Clayton9e9f2192013-05-17 00:55:28 +00003881 SymbolContextScope * symbol_context_scope = NULL;
3882
Siva Chandra0783ab92015-03-24 18:32:27 +00003883 if (!mangled)
3884 {
3885 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
3886 // generate fully qualified names of global variables with commands like "frame var j".
3887 // For example, if j were an int variable holding a value 4 and declared in a namespace
3888 // B which in turn is contained in a namespace A, the command "frame var j" returns
3889 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
3890 // to generate a fully qualified name from the declaration context.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003891 if (parent_tag == DW_TAG_compile_unit &&
Jim Ingham0e0984e2015-09-02 01:06:46 +00003892 Language::LanguageIsCPlusPlus(die.GetLanguage()))
Siva Chandra0783ab92015-03-24 18:32:27 +00003893 {
3894 DWARFDeclContext decl_ctx;
3895
Greg Clayton6071e6f2015-08-26 22:57:51 +00003896 die.GetDWARFDeclContext(decl_ctx);
Siva Chandra0783ab92015-03-24 18:32:27 +00003897 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3898 }
3899 }
3900
Greg Clayton9e9f2192013-05-17 00:55:28 +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 by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003903 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00003904 // a DW_OP_addr opcode _somewhere_ in the definition. I say
3905 // somewhere because clang likes to combine small global variables
3906 // into the same symbol and have locations like:
3907 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3908 // So if we don't have a DW_TAG_formal_parameter, we can look at
3909 // the location to see if it contains a DW_OP_addr opcode, and
3910 // then we can correctly classify our variables.
3911 if (tag == DW_TAG_formal_parameter)
3912 scope = eValueTypeVariableArgument;
3913 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003914 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003915 bool op_error = false;
3916 // Check if the location has a DW_OP_addr with any address value...
3917 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3918 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003919 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003920 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
3921 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00003922 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003923 StreamString strm;
3924 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003925 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die.GetOffset(), die.GetTagAsCString(), strm.GetString().c_str());
Greg Clayton96c09682012-01-04 22:56:43 +00003926 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00003927 }
Greg Claytond1767f02011-12-08 02:13:16 +00003928
Greg Clayton9e9f2192013-05-17 00:55:28 +00003929 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3930 {
3931 if (is_external)
3932 scope = eValueTypeVariableGlobal;
3933 else
3934 scope = eValueTypeVariableStatic;
3935
3936
3937 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
3938
3939 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003940 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003941 // When leaving the DWARF in the .o files on darwin,
3942 // when we have a global variable that wasn't initialized,
3943 // the .o file might not have allocated a virtual
3944 // address for the global variable. In this case it will
3945 // have created a symbol for the global variable
3946 // that is undefined/data and external and the value will
3947 // be the byte size of the variable. When we do the
3948 // address map in SymbolFileDWARFDebugMap we rely on
3949 // having an address, we need to do some magic here
3950 // so we can get the correct address for our global
3951 // variable. The address for all of these entries
3952 // will be zero, and there will be an undefined symbol
3953 // in this object file, and the executable will have
3954 // a matching symbol with a good address. So here we
3955 // dig up the correct address and replace it in the
3956 // location for the variable, and set the variable's
3957 // symbol context scope to be that of the main executable
3958 // so the file address will resolve correctly.
3959 bool linked_oso_file_addr = false;
3960 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00003961 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003962 // we have a possible uninitialized extern global
3963 ConstString const_name(mangled ? mangled : name);
3964 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
3965 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003966 {
Greg Clayton3046e662013-07-10 01:23:25 +00003967 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00003968 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003969 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003970 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
3971 eSymbolTypeData,
3972 Symtab::eDebugYes,
3973 Symtab::eVisibilityExtern);
3974 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003975 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003976 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003977 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00003978 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00003979 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003980 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003981 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003982 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003983 linked_oso_file_addr = true;
3984 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003985 }
3986 }
3987 }
3988 }
3989 }
3990 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00003991 }
Greg Clayton9422dd62013-03-04 21:46:16 +00003992
Greg Clayton9e9f2192013-05-17 00:55:28 +00003993 if (!linked_oso_file_addr)
3994 {
3995 // The DW_OP_addr is not zero, but it contains a .o file address which
3996 // needs to be linked up correctly.
3997 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
3998 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00003999 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004000 // Update the file address for this variable
4001 location.Update_DW_OP_addr (exe_file_addr);
4002 }
4003 else
4004 {
4005 // Variable didn't make it into the final executable
4006 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00004007 }
Greg Claytond1767f02011-12-08 02:13:16 +00004008 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004009 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00004010 }
4011 else
4012 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004013 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004014 }
Greg Clayton7f995132011-10-04 22:41:51 +00004015 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004016
4017 if (symbol_context_scope == NULL)
4018 {
4019 switch (parent_tag)
4020 {
4021 case DW_TAG_subprogram:
4022 case DW_TAG_inlined_subroutine:
4023 case DW_TAG_lexical_block:
4024 if (sc.function)
4025 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004026 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Clayton9e9f2192013-05-17 00:55:28 +00004027 if (symbol_context_scope == NULL)
4028 symbol_context_scope = sc.function;
4029 }
4030 break;
4031
4032 default:
4033 symbol_context_scope = sc.comp_unit;
4034 break;
4035 }
4036 }
4037
4038 if (symbol_context_scope)
4039 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004040 SymbolFileTypeSP type_sp(new SymbolFileType(*this, DIERef(type_die_form).GetUID()));
Enrico Granata4ec130d2014-08-11 19:16:35 +00004041
4042 if (const_value.Form() && type_sp && type_sp->GetType())
Greg Clayton6071e6f2015-08-26 22:57:51 +00004043 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), die.GetCU()->GetAddressByteSize());
Enrico Granata4ec130d2014-08-11 19:16:35 +00004044
Greg Clayton6071e6f2015-08-26 22:57:51 +00004045 var_sp.reset (new Variable (die.GetID(),
Greg Clayton9e9f2192013-05-17 00:55:28 +00004046 name,
4047 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00004048 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00004049 scope,
4050 symbol_context_scope,
4051 &decl,
4052 location,
4053 is_external,
Paul Herman10bc1a42015-08-18 22:46:57 +00004054 is_artificial,
4055 is_static_member));
Greg Clayton9e9f2192013-05-17 00:55:28 +00004056
4057 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4058 }
4059 else
4060 {
4061 // Not ready to parse this variable yet. It might be a global
4062 // or static variable that is in a function scope and the function
4063 // in the symbol context wasn't filled in yet
4064 return var_sp;
4065 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004066 }
Greg Clayton7f995132011-10-04 22:41:51 +00004067 // Cache var_sp even if NULL (the variable was just a specification or
4068 // was missing vital information to be able to be displayed in the debugger
4069 // (missing location due to optimization, etc)) so we don't re-parse
4070 // this DIE over and over later...
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004071 GetDIEToVariable()[die.GetDIE()] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004072 }
4073 return var_sp;
4074}
4075
Greg Claytonc662ec82011-06-17 22:10:16 +00004076
Greg Clayton6071e6f2015-08-26 22:57:51 +00004077DWARFDIE
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004078SymbolFileDWARF::FindBlockContainingSpecification (const DIERef& func_die_ref,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004079 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004080{
4081 // Give the concrete function die specified by "func_die_offset", find the
4082 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4083 // to "spec_block_die_offset"
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004084 return FindBlockContainingSpecification (DebugInfo()->GetDIE (func_die_ref), spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004085}
4086
4087
Greg Clayton6071e6f2015-08-26 22:57:51 +00004088DWARFDIE
4089SymbolFileDWARF::FindBlockContainingSpecification(const DWARFDIE &die,
4090 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004091{
4092 if (die)
4093 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004094 switch (die.Tag())
Greg Claytonc662ec82011-06-17 22:10:16 +00004095 {
4096 case DW_TAG_subprogram:
4097 case DW_TAG_inlined_subroutine:
4098 case DW_TAG_lexical_block:
4099 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004100 if (die.GetAttributeValueAsReference (DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004101 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004102
Greg Clayton6071e6f2015-08-26 22:57:51 +00004103 if (die.GetAttributeValueAsReference (DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004104 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004105 }
4106 break;
4107 }
4108
4109 // Give the concrete function die specified by "func_die_offset", find the
4110 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4111 // to "spec_block_die_offset"
Greg Clayton6071e6f2015-08-26 22:57:51 +00004112 for (DWARFDIE child_die = die.GetFirstChild(); child_die; child_die = child_die.GetSibling())
Greg Claytonc662ec82011-06-17 22:10:16 +00004113 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004114 DWARFDIE result_die = FindBlockContainingSpecification (child_die, spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004115 if (result_die)
4116 return result_die;
4117 }
4118 }
4119
Greg Clayton6071e6f2015-08-26 22:57:51 +00004120 return DWARFDIE();
Greg Claytonc662ec82011-06-17 22:10:16 +00004121}
4122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004123size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00004124SymbolFileDWARF::ParseVariables (const SymbolContext& sc,
4125 const DWARFDIE &orig_die,
4126 const lldb::addr_t func_low_pc,
4127 bool parse_siblings,
4128 bool parse_children,
4129 VariableList* cc_variable_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004130{
Greg Clayton6071e6f2015-08-26 22:57:51 +00004131 if (!orig_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004132 return 0;
4133
Greg Claytonc662ec82011-06-17 22:10:16 +00004134 VariableListSP variable_list_sp;
4135
4136 size_t vars_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004137 DWARFDIE die = orig_die;
4138 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004139 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004140 dw_tag_t tag = die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004141
4142 // Check to see if we have already parsed this variable or constant?
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004143 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton6071e6f2015-08-26 22:57:51 +00004144 if (var_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004145 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004146 if (cc_variable_list)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004147 cc_variable_list->AddVariableIfUnique (var_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148 }
4149 else
4150 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004151 // We haven't already parsed it, lets do that now.
4152 if ((tag == DW_TAG_variable) ||
4153 (tag == DW_TAG_constant) ||
4154 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004155 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004156 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004157 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004158 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4159 dw_tag_t parent_tag = sc_parent_die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004160 switch (parent_tag)
4161 {
4162 case DW_TAG_compile_unit:
4163 if (sc.comp_unit != NULL)
4164 {
4165 variable_list_sp = sc.comp_unit->GetVariableList(false);
4166 if (variable_list_sp.get() == NULL)
4167 {
4168 variable_list_sp.reset(new VariableList());
4169 sc.comp_unit->SetVariableList(variable_list_sp);
4170 }
4171 }
4172 else
4173 {
Daniel Malead01b2952012-11-29 21:49:15 +00004174 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n",
Greg Clayton6071e6f2015-08-26 22:57:51 +00004175 sc_parent_die.GetID(),
4176 sc_parent_die.GetTagAsCString(),
4177 orig_die.GetID(),
4178 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004179 }
4180 break;
4181
4182 case DW_TAG_subprogram:
4183 case DW_TAG_inlined_subroutine:
4184 case DW_TAG_lexical_block:
4185 if (sc.function != NULL)
4186 {
4187 // Check to see if we already have parsed the variables for the given scope
4188
Greg Clayton6071e6f2015-08-26 22:57:51 +00004189 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004190 if (block == NULL)
4191 {
4192 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004193 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00004194 // to find the concrete block so we can correctly add the
4195 // variable to it
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004196 const DWARFDIE concrete_block_die = FindBlockContainingSpecification (DIERef(sc.function->GetID()),
Greg Clayton6071e6f2015-08-26 22:57:51 +00004197 sc_parent_die.GetOffset());
Greg Claytonc662ec82011-06-17 22:10:16 +00004198 if (concrete_block_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004199 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004200 }
4201
4202 if (block != NULL)
4203 {
4204 const bool can_create = false;
4205 variable_list_sp = block->GetBlockVariableList (can_create);
4206 if (variable_list_sp.get() == NULL)
4207 {
4208 variable_list_sp.reset(new VariableList());
4209 block->SetVariableList(variable_list_sp);
4210 }
4211 }
4212 }
4213 break;
4214
4215 default:
Daniel Malead01b2952012-11-29 21:49:15 +00004216 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n",
Greg Clayton6071e6f2015-08-26 22:57:51 +00004217 orig_die.GetID(),
4218 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004219 break;
4220 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004221 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004222
4223 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004224 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004225 VariableSP var_sp (ParseVariableDIE(sc, die, func_low_pc));
Greg Clayton73bf5db2011-06-17 01:22:15 +00004226 if (var_sp)
4227 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004228 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004229 if (cc_variable_list)
4230 cc_variable_list->AddVariableIfUnique (var_sp);
4231 ++vars_added;
4232 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004233 }
4234 }
4235 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004236
4237 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4238
Greg Clayton6071e6f2015-08-26 22:57:51 +00004239 if (!skip_children && parse_children && die.HasChildren())
Greg Claytonc662ec82011-06-17 22:10:16 +00004240 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004241 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, true, cc_variable_list);
Greg Claytonc662ec82011-06-17 22:10:16 +00004242 }
4243
4244 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004245 die = die.GetSibling();
Greg Claytonc662ec82011-06-17 22:10:16 +00004246 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00004247 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004248 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004249 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004250}
4251
4252//------------------------------------------------------------------
4253// PluginInterface protocol
4254//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004255ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004256SymbolFileDWARF::GetPluginName()
4257{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004258 return GetPluginNameStatic();
4259}
4260
4261uint32_t
4262SymbolFileDWARF::GetPluginVersion()
4263{
4264 return 1;
4265}
4266
4267void
Sean Callanancc427fa2011-07-30 02:42:06 +00004268SymbolFileDWARF::DumpIndexes ()
4269{
4270 StreamFile s(stdout, false);
4271
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004272 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00004273 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004274 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00004275 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
4276 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
4277 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
4278 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
4279 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
4280 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
4281 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00004282 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00004283}
4284
Greg Claytoncaab74e2012-01-28 00:48:57 +00004285
Greg Clayton1f746072012-08-29 21:13:06 +00004286SymbolFileDWARFDebugMap *
4287SymbolFileDWARF::GetDebugMapSymfile ()
4288{
4289 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
4290 {
4291 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
4292 if (module_sp)
4293 {
4294 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4295 if (sym_vendor)
4296 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
4297 }
4298 }
4299 return m_debug_map_symfile;
4300}