blob: b23836433c7a2ed71480a3f29aeffa5999246712 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
Sean Callanancc427fa2011-07-30 02:42:06 +000013#include "llvm/Support/Casting.h"
14
Robert Flackeb83fab2015-05-15 18:59:59 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000017#include "lldb/Core/ModuleList.h"
18#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/RegularExpression.h"
21#include "lldb/Core/Scalar.h"
22#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000023#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000024#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/Value.h"
27
Sean Callanan4dbb2712015-09-25 20:35:58 +000028#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000029
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000030#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000031#include "lldb/Host/Host.h"
32
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000033#include "lldb/Interpreter/OptionValueFileSpecList.h"
34#include "lldb/Interpreter/OptionValueProperties.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/Block.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000037#include "lldb/Symbol/ClangASTContext.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000038#include "lldb/Symbol/CompilerDecl.h"
39#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Symbol/CompileUnit.h"
41#include "lldb/Symbol/LineTable.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000042#include "lldb/Symbol/DebugMacros.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/ObjectFile.h"
44#include "lldb/Symbol/SymbolVendor.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000045#include "lldb/Symbol/TypeSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/VariableList.h"
Ravitheja Addepally40697302015-10-08 09:45:41 +000047#include "lldb/Symbol/TypeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Jim Inghamaa816b82015-09-02 01:59:14 +000049#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
50#include "Plugins/Language/ObjC/ObjCLanguage.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Jim Ingham0e0984e2015-09-02 01:06:46 +000052#include "lldb/Target/Language.h"
53
Tamas Berghammer2ff88702015-10-23 10:34:49 +000054#include "lldb/Utility/TaskPool.h"
55
Greg Clayton261ac3f2015-08-28 01:01:03 +000056#include "DWARFASTParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "DWARFCompileUnit.h"
58#include "DWARFDebugAbbrev.h"
59#include "DWARFDebugAranges.h"
60#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDebugLine.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000062#include "DWARFDebugMacro.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063#include "DWARFDebugPubnames.h"
64#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000065#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "DWARFDIECollection.h"
67#include "DWARFFormValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068#include "LogChannelDWARF.h"
Tamas Berghammereb882fc2015-09-09 10:20:48 +000069#include "SymbolFileDWARFDwo.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000070#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
72#include <map>
73
Matthew Gardinere81df3b2014-08-26 06:57:23 +000074#include <ctype.h>
75#include <string.h>
76
Greg Clayton62742b12010-11-11 01:09:45 +000077//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000078
79#ifdef ENABLE_DEBUG_PRINTF
80#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000081#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000082#else
83#define DEBUG_PRINTF(fmt, ...)
84#endif
85
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086using namespace lldb;
87using namespace lldb_private;
88
Greg Clayton219cf312012-03-30 00:51:13 +000089//static inline bool
90//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
91//{
92// switch (tag)
93// {
94// default:
95// break;
96// case DW_TAG_subprogram:
97// case DW_TAG_inlined_subroutine:
98// case DW_TAG_class_type:
99// case DW_TAG_structure_type:
100// case DW_TAG_union_type:
101// return true;
102// }
103// return false;
104//}
105//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000106
107namespace {
108
109 PropertyDefinition
110 g_properties[] =
111 {
112 { "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." },
113 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
114 };
115
116 enum
117 {
118 ePropertySymLinkPaths
119 };
120
121
122 class PluginProperties : public Properties
123 {
124 public:
125 static ConstString
126 GetSettingName()
127 {
128 return SymbolFileDWARF::GetPluginNameStatic();
129 }
130
131 PluginProperties()
132 {
133 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
134 m_collection_sp->Initialize(g_properties);
135 }
136
137 FileSpecList&
138 GetSymLinkPaths()
139 {
140 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, true, ePropertySymLinkPaths);
141 assert(option_value);
142 return option_value->GetCurrentValue();
143 }
144
145 };
146
147 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
148
149 static const SymbolFileDWARFPropertiesSP&
150 GetGlobalPluginProperties()
151 {
152 static const auto g_settings_sp(std::make_shared<PluginProperties>());
153 return g_settings_sp;
154 }
155
156} // anonymous namespace end
157
158
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000159static const char*
160removeHostnameFromPathname(const char* path_from_dwarf)
161{
162 if (!path_from_dwarf || !path_from_dwarf[0])
163 {
164 return path_from_dwarf;
165 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000166
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000167 const char *colon_pos = strchr(path_from_dwarf, ':');
Enrico Granata99e5e222015-07-27 21:27:02 +0000168 if (nullptr == colon_pos)
169 {
170 return path_from_dwarf;
171 }
172
173 const char *slash_pos = strchr(path_from_dwarf, '/');
174 if (slash_pos && (slash_pos < colon_pos))
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000175 {
176 return path_from_dwarf;
177 }
178
179 // check whether we have a windows path, and so the first character
180 // is a drive-letter not a hostname.
181 if (
182 colon_pos == path_from_dwarf + 1 &&
183 isalpha(*path_from_dwarf) &&
184 strlen(path_from_dwarf) > 2 &&
185 '\\' == path_from_dwarf[2])
186 {
187 return path_from_dwarf;
188 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000189
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000190 return colon_pos + 1;
191}
192
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000193static const char*
194resolveCompDir(const char* path_from_dwarf)
195{
196 if (!path_from_dwarf)
197 return nullptr;
198
199 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
200 // Remove the host part if present.
201 const char* local_path = removeHostnameFromPathname(path_from_dwarf);
202 if (!local_path)
203 return nullptr;
204
205 bool is_symlink = false;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000206 FileSpec local_path_spec(local_path, false);
207 const auto& file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
208 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
209 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i), local_path_spec, true);
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000210
211 if (!is_symlink)
212 return local_path;
213
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000214 if (!local_path_spec.IsSymbolicLink())
215 return local_path;
216
217 FileSpec resolved_local_path_spec;
218 const auto error = FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
219 if (error.Success())
220 return resolved_local_path_spec.GetCString();
221
222 return nullptr;
223}
224
225
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226void
227SymbolFileDWARF::Initialize()
228{
229 LogChannelDWARF::Initialize();
230 PluginManager::RegisterPlugin (GetPluginNameStatic(),
231 GetPluginDescriptionStatic(),
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000232 CreateInstance,
233 DebuggerInitialize);
234}
235
236void
237SymbolFileDWARF::DebuggerInitialize(Debugger &debugger)
238{
239 if (!PluginManager::GetSettingForSymbolFilePlugin(debugger, PluginProperties::GetSettingName()))
240 {
241 const bool is_global_setting = true;
242 PluginManager::CreateSettingForSymbolFilePlugin(debugger,
243 GetGlobalPluginProperties()->GetValueProperties(),
244 ConstString ("Properties for the dwarf symbol-file plug-in."),
245 is_global_setting);
246 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
249void
250SymbolFileDWARF::Terminate()
251{
252 PluginManager::UnregisterPlugin (CreateInstance);
253 LogChannelDWARF::Initialize();
254}
255
256
Greg Clayton57abc5d2013-05-10 21:47:16 +0000257lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258SymbolFileDWARF::GetPluginNameStatic()
259{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000260 static ConstString g_name("dwarf");
261 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
264const char *
265SymbolFileDWARF::GetPluginDescriptionStatic()
266{
267 return "DWARF and DWARF3 debug symbol file reader.";
268}
269
270
271SymbolFile*
272SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
273{
274 return new SymbolFileDWARF(obj_file);
275}
276
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000277TypeList *
278SymbolFileDWARF::GetTypeList ()
279{
Greg Clayton1f746072012-08-29 21:13:06 +0000280 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000281 return m_debug_map_symfile->GetTypeList();
282 return m_obj_file->GetModule()->GetTypeList();
283
284}
Greg Claytonf02500c2013-06-18 22:51:05 +0000285void
Greg Clayton6071e6f2015-08-26 22:57:51 +0000286SymbolFileDWARF::GetTypes (const DWARFDIE &die,
Greg Claytonf02500c2013-06-18 22:51:05 +0000287 dw_offset_t min_die_offset,
288 dw_offset_t max_die_offset,
289 uint32_t type_mask,
290 TypeSet &type_set)
291{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000292 if (die)
Greg Claytonf02500c2013-06-18 22:51:05 +0000293 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000294 const dw_offset_t die_offset = die.GetOffset();
295
296 if (die_offset >= max_die_offset)
297 return;
298
299 if (die_offset >= min_die_offset)
Greg Claytonf02500c2013-06-18 22:51:05 +0000300 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000301 const dw_tag_t tag = die.Tag();
Greg Claytonf02500c2013-06-18 22:51:05 +0000302
Greg Clayton6071e6f2015-08-26 22:57:51 +0000303 bool add_type = false;
304
305 switch (tag)
Greg Claytonf02500c2013-06-18 22:51:05 +0000306 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000307 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
308 case DW_TAG_unspecified_type:
309 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
310 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
311 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
312 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
313 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
314 case DW_TAG_subroutine_type:
315 case DW_TAG_subprogram:
316 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
317 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
318 case DW_TAG_rvalue_reference_type:
319 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
320 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
321 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
322 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000323
Greg Clayton6071e6f2015-08-26 22:57:51 +0000324 if (add_type)
325 {
326 const bool assert_not_being_parsed = true;
327 Type *type = ResolveTypeUID (die, assert_not_being_parsed);
328 if (type)
Greg Claytonf02500c2013-06-18 22:51:05 +0000329 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000330 if (type_set.find(type) == type_set.end())
331 type_set.insert(type);
Greg Claytonf02500c2013-06-18 22:51:05 +0000332 }
333 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000334 }
335
336 for (DWARFDIE child_die = die.GetFirstChild();
337 child_die.IsValid();
338 child_die = child_die.GetSibling())
339 {
340 GetTypes (child_die, min_die_offset, max_die_offset, type_mask, type_set);
Greg Claytonf02500c2013-06-18 22:51:05 +0000341 }
342 }
343}
344
345size_t
346SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
347 uint32_t type_mask,
348 TypeList &type_list)
349
350{
351 TypeSet type_set;
352
353 CompileUnit *comp_unit = NULL;
354 DWARFCompileUnit* dwarf_cu = NULL;
355 if (sc_scope)
356 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
357
358 if (comp_unit)
359 {
360 dwarf_cu = GetDWARFCompileUnit(comp_unit);
361 if (dwarf_cu == 0)
362 return 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +0000363 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000364 dwarf_cu->GetOffset(),
365 dwarf_cu->GetNextCompileUnitOffset(),
366 type_mask,
367 type_set);
368 }
369 else
370 {
371 DWARFDebugInfo* info = DebugInfo();
372 if (info)
373 {
374 const size_t num_cus = info->GetNumCompileUnits();
375 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
376 {
377 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
378 if (dwarf_cu)
379 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000380 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000381 0,
382 UINT32_MAX,
383 type_mask,
384 type_set);
385 }
386 }
387 }
388 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000389
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000390 std::set<CompilerType> compiler_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000391 size_t num_types_added = 0;
392 for (Type *type : type_set)
393 {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000394 CompilerType compiler_type = type->GetForwardCompilerType ();
395 if (compiler_type_set.find(compiler_type) == compiler_type_set.end())
Greg Clayton0fc4f312013-06-20 01:23:18 +0000396 {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000397 compiler_type_set.insert(compiler_type);
Greg Clayton0fc4f312013-06-20 01:23:18 +0000398 type_list.Insert (type->shared_from_this());
399 ++num_types_added;
400 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000401 }
402 return num_types_added;
403}
404
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000405
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406//----------------------------------------------------------------------
407// Gets the first parent that is a lexical block, function or inlined
408// subroutine, or compile unit.
409//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000410DWARFDIE
411SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000413 DWARFDIE die;
414 for (die = child_die.GetParent(); die; die = die.GetParent())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000416 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418 switch (tag)
419 {
420 case DW_TAG_compile_unit:
421 case DW_TAG_subprogram:
422 case DW_TAG_inlined_subroutine:
423 case DW_TAG_lexical_block:
424 return die;
425 }
426 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000427 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428}
429
430
Greg Clayton450e3f32010-10-12 02:24:53 +0000431SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
432 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000433 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 +0000434 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000435 m_debug_map_symfile (NULL),
Greg Claytond4a2b372011-09-12 23:21:58 +0000436 m_data_debug_abbrev (),
437 m_data_debug_aranges (),
438 m_data_debug_frame (),
439 m_data_debug_info (),
440 m_data_debug_line (),
Siva Chandrad8335e92015-12-16 00:22:08 +0000441 m_data_debug_macro (),
Greg Claytond4a2b372011-09-12 23:21:58 +0000442 m_data_debug_loc (),
443 m_data_debug_ranges (),
444 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000445 m_data_apple_names (),
446 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000447 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 m_info(),
450 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000451 m_apple_names_ap (),
452 m_apple_types_ap (),
453 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000454 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000455 m_function_basename_index(),
456 m_function_fullname_index(),
457 m_function_method_index(),
458 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000459 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000460 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000461 m_type_index(),
462 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000463 m_indexed (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000464 m_using_apple_tables (false),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000465 m_fetched_external_modules (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000466 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000467 m_ranges(),
468 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469{
470}
471
472SymbolFileDWARF::~SymbolFileDWARF()
473{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000474}
475
476static const ConstString &
477GetDWARFMachOSegmentName ()
478{
479 static ConstString g_dwarf_section_name ("__DWARF");
480 return g_dwarf_section_name;
481}
482
Greg Claytone576ab22011-02-15 00:19:15 +0000483UniqueDWARFASTTypeMap &
484SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
485{
Greg Clayton1f746072012-08-29 21:13:06 +0000486 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000487 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
488 return m_unique_ast_type_map;
489}
490
Greg Clayton8b4edba2015-08-14 20:02:05 +0000491TypeSystem *
492SymbolFileDWARF::GetTypeSystemForLanguage (LanguageType language)
493{
494 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000495 TypeSystem *type_system;
Greg Clayton8b4edba2015-08-14 20:02:05 +0000496 if (debug_map_symfile)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000497 {
498 type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
499 }
Greg Clayton8b4edba2015-08-14 20:02:05 +0000500 else
Ryan Brown57bee1e2015-09-14 22:45:11 +0000501 {
502 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
503 if (type_system)
504 type_system->SetSymbolFile(this);
505 }
506 return type_system;
Greg Clayton8b4edba2015-08-14 20:02:05 +0000507}
508
Greg Clayton6beaaa62011-01-17 03:46:26 +0000509void
510SymbolFileDWARF::InitializeObject()
511{
Greg Claytone72dfb32012-02-24 01:59:29 +0000512 ModuleSP module_sp (m_obj_file->GetModule());
513 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000514 {
Greg Clayton3046e662013-07-10 01:23:25 +0000515 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000516 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
517
518 // Memory map the DWARF mach-o segment so we have everything mmap'ed
519 // to keep our heap memory usage down.
520 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000521 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000522 }
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000523
Greg Clayton4d01ace2011-09-29 16:58:15 +0000524 get_apple_names_data();
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000525 if (m_data_apple_names.m_data.GetByteSize() > 0)
Greg Clayton7f995132011-10-04 22:41:51 +0000526 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000527 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names.m_data,
528 get_debug_str_data(),
529 ".apple_names"));
Greg Clayton97fbc342011-10-20 22:30:33 +0000530 if (m_apple_names_ap->IsValid())
531 m_using_apple_tables = true;
532 else
Greg Clayton7f995132011-10-04 22:41:51 +0000533 m_apple_names_ap.reset();
534 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000535 get_apple_types_data();
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000536 if (m_data_apple_types.m_data.GetByteSize() > 0)
Greg Clayton7f995132011-10-04 22:41:51 +0000537 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000538 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types.m_data,
539 get_debug_str_data(),
540 ".apple_types"));
Greg Clayton97fbc342011-10-20 22:30:33 +0000541 if (m_apple_types_ap->IsValid())
542 m_using_apple_tables = true;
543 else
Greg Clayton7f995132011-10-04 22:41:51 +0000544 m_apple_types_ap.reset();
545 }
546
547 get_apple_namespaces_data();
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000548 if (m_data_apple_namespaces.m_data.GetByteSize() > 0)
Greg Clayton7f995132011-10-04 22:41:51 +0000549 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000550 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces.m_data,
551 get_debug_str_data(),
552 ".apple_namespaces"));
Greg Clayton97fbc342011-10-20 22:30:33 +0000553 if (m_apple_namespaces_ap->IsValid())
554 m_using_apple_tables = true;
555 else
Greg Clayton7f995132011-10-04 22:41:51 +0000556 m_apple_namespaces_ap.reset();
557 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000558
Greg Clayton5009f9d2011-10-27 17:55:14 +0000559 get_apple_objc_data();
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000560 if (m_data_apple_objc.m_data.GetByteSize() > 0)
Greg Clayton5009f9d2011-10-27 17:55:14 +0000561 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000562 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc.m_data,
563 get_debug_str_data(),
564 ".apple_objc"));
Greg Clayton5009f9d2011-10-27 17:55:14 +0000565 if (m_apple_objc_ap->IsValid())
566 m_using_apple_tables = true;
567 else
568 m_apple_objc_ap.reset();
569 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
572bool
573SymbolFileDWARF::SupportedVersion(uint16_t version)
574{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000575 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576}
577
578uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000579SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580{
581 uint32_t abilities = 0;
582 if (m_obj_file != NULL)
583 {
584 const Section* section = NULL;
585 const SectionList *section_list = m_obj_file->GetSectionList();
586 if (section_list == NULL)
587 return 0;
588
589 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 uint64_t debug_info_file_size = 0;
591 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592
Greg Clayton6beaaa62011-01-17 03:46:26 +0000593 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594
595 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000596 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597
Greg Clayton4ceb9982010-07-21 22:54:26 +0000598 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 if (section != NULL)
600 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000601 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602
Greg Clayton4ceb9982010-07-21 22:54:26 +0000603 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000605 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606
Greg Clayton4ceb9982010-07-21 22:54:26 +0000607 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000609 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 }
Greg Clayton6c596612012-05-18 21:47:20 +0000611 else
612 {
613 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
614 if (symfile_dir_cstr)
615 {
616 if (strcasestr(symfile_dir_cstr, ".dsym"))
617 {
618 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
619 {
620 // We have a dSYM file that didn't have a any debug info.
621 // If the string table has a size of 1, then it was made from
622 // an executable with no debug info, or from an executable that
623 // was stripped.
624 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
625 if (section && section->GetFileSize() == 1)
626 {
627 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
628 }
629 }
630 }
631 }
632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
634 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
635 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
636
637 if (debug_line_file_size > 0)
638 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 }
640 return abilities;
641}
642
Ed Masteeeae7212013-10-24 20:43:47 +0000643const DWARFDataExtractor&
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000644SymbolFileDWARF::GetCachedSectionData (lldb::SectionType sect_type, DWARFDataSegment& data_segment)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000646 std::call_once(data_segment.m_flag,
647 &SymbolFileDWARF::LoadSectionData,
648 this,
649 sect_type,
650 std::ref(data_segment.m_data));
651 return data_segment.m_data;
652}
653
654void
655SymbolFileDWARF::LoadSectionData (lldb::SectionType sect_type, DWARFDataExtractor& data)
656{
657 ModuleSP module_sp (m_obj_file->GetModule());
658 const SectionList *section_list = module_sp->GetSectionList();
659 if (section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000661 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
662 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000664 // See if we memory mapped the DWARF segment?
665 if (m_dwarf_data.GetByteSize())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 {
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000667 data.SetData(m_dwarf_data, section_sp->GetOffset(), section_sp->GetFileSize());
668 }
669 else
670 {
671 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
672 data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 }
674 }
675 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676}
677
Ed Masteeeae7212013-10-24 20:43:47 +0000678const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679SymbolFileDWARF::get_debug_abbrev_data()
680{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000681 return GetCachedSectionData (eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682}
683
Ed Masteeeae7212013-10-24 20:43:47 +0000684const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000685SymbolFileDWARF::get_debug_addr_data()
686{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000687 return GetCachedSectionData (eSectionTypeDWARFDebugAddr, m_data_debug_addr);
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000688}
689
690const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000691SymbolFileDWARF::get_debug_aranges_data()
692{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000693 return GetCachedSectionData (eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
Greg Claytond4a2b372011-09-12 23:21:58 +0000694}
695
Ed Masteeeae7212013-10-24 20:43:47 +0000696const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697SymbolFileDWARF::get_debug_frame_data()
698{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000699 return GetCachedSectionData (eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700}
701
Ed Masteeeae7212013-10-24 20:43:47 +0000702const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703SymbolFileDWARF::get_debug_info_data()
704{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000705 return GetCachedSectionData (eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706}
707
Ed Masteeeae7212013-10-24 20:43:47 +0000708const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709SymbolFileDWARF::get_debug_line_data()
710{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000711 return GetCachedSectionData (eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712}
713
Ed Masteeeae7212013-10-24 20:43:47 +0000714const DWARFDataExtractor&
Siva Chandrad8335e92015-12-16 00:22:08 +0000715SymbolFileDWARF::get_debug_macro_data()
716{
717 return GetCachedSectionData (eSectionTypeDWARFDebugMacro, m_data_debug_macro);
718}
719
720const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721SymbolFileDWARF::get_debug_loc_data()
722{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000723 return GetCachedSectionData (eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724}
725
Ed Masteeeae7212013-10-24 20:43:47 +0000726const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727SymbolFileDWARF::get_debug_ranges_data()
728{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000729 return GetCachedSectionData (eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Ed Masteeeae7212013-10-24 20:43:47 +0000732const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733SymbolFileDWARF::get_debug_str_data()
734{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000735 return GetCachedSectionData (eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736}
737
Ed Masteeeae7212013-10-24 20:43:47 +0000738const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000739SymbolFileDWARF::get_debug_str_offsets_data()
740{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000741 return GetCachedSectionData (eSectionTypeDWARFDebugStrOffsets, m_data_debug_str_offsets);
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000742}
743
744const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000745SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000746{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000747 return GetCachedSectionData (eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000748}
749
Ed Masteeeae7212013-10-24 20:43:47 +0000750const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000751SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000752{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000753 return GetCachedSectionData (eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000754}
755
Ed Masteeeae7212013-10-24 20:43:47 +0000756const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000757SymbolFileDWARF::get_apple_namespaces_data()
758{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000759 return GetCachedSectionData (eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000760}
761
Ed Masteeeae7212013-10-24 20:43:47 +0000762const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000763SymbolFileDWARF::get_apple_objc_data()
764{
Tamas Berghammer90b4dce2015-10-22 11:14:37 +0000765 return GetCachedSectionData (eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000766}
767
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768
769DWARFDebugAbbrev*
770SymbolFileDWARF::DebugAbbrev()
771{
772 if (m_abbr.get() == NULL)
773 {
Ed Masteeeae7212013-10-24 20:43:47 +0000774 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 if (debug_abbrev_data.GetByteSize() > 0)
776 {
777 m_abbr.reset(new DWARFDebugAbbrev());
778 if (m_abbr.get())
779 m_abbr->Parse(debug_abbrev_data);
780 }
781 }
782 return m_abbr.get();
783}
784
785const DWARFDebugAbbrev*
786SymbolFileDWARF::DebugAbbrev() const
787{
788 return m_abbr.get();
789}
790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791
792DWARFDebugInfo*
793SymbolFileDWARF::DebugInfo()
794{
795 if (m_info.get() == NULL)
796 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
798 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 if (get_debug_info_data().GetByteSize() > 0)
800 {
801 m_info.reset(new DWARFDebugInfo());
802 if (m_info.get())
803 {
804 m_info->SetDwarfData(this);
805 }
806 }
807 }
808 return m_info.get();
809}
810
811const DWARFDebugInfo*
812SymbolFileDWARF::DebugInfo() const
813{
814 return m_info.get();
815}
816
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000818SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819{
Greg Claytonea4a5bb2015-09-04 22:29:46 +0000820 if (!comp_unit)
821 return nullptr;
822
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000824 if (info)
825 {
826 if (GetDebugMapSymfile ())
827 {
828 // The debug map symbol file made the compile units for this DWARF
829 // file which is .o file with DWARF in it, and we should have
830 // only 1 compile unit which is at offset zero in the DWARF.
831 // TODO: modify to support LTO .o files where each .o file might
832 // have multiple DW_TAG_compile_unit tags.
Greg Clayton68c00bd2015-02-05 02:10:29 +0000833
Greg Clayton6071e6f2015-08-26 22:57:51 +0000834 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0);
Greg Clayton68c00bd2015-02-05 02:10:29 +0000835 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
836 dwarf_cu->SetUserData(comp_unit);
837 return dwarf_cu;
Greg Clayton1f746072012-08-29 21:13:06 +0000838 }
839 else
840 {
841 // Just a normal DWARF file whose user ID for the compile unit is
842 // the DWARF offset itself
Greg Clayton68c00bd2015-02-05 02:10:29 +0000843
Greg Clayton6071e6f2015-08-26 22:57:51 +0000844 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
Greg Clayton68c00bd2015-02-05 02:10:29 +0000845 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
846 dwarf_cu->SetUserData(comp_unit);
847 return dwarf_cu;
848
Greg Clayton1f746072012-08-29 21:13:06 +0000849 }
850 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 return NULL;
852}
853
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854
855DWARFDebugRanges*
856SymbolFileDWARF::DebugRanges()
857{
858 if (m_ranges.get() == NULL)
859 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000860 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
861 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 if (get_debug_ranges_data().GetByteSize() > 0)
863 {
864 m_ranges.reset(new DWARFDebugRanges());
865 if (m_ranges.get())
866 m_ranges->Extract(this);
867 }
868 }
869 return m_ranges.get();
870}
871
872const DWARFDebugRanges*
873SymbolFileDWARF::DebugRanges() const
874{
875 return m_ranges.get();
876}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877
Greg Clayton53eb1c22012-04-02 22:59:12 +0000878lldb::CompUnitSP
879SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000881 CompUnitSP cu_sp;
882 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000884 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
885 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000887 // We already parsed this compile unit, had out a shared pointer to it
888 cu_sp = comp_unit->shared_from_this();
889 }
890 else
891 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000892 if (dwarf_cu->GetSymbolFileDWARF() != this)
893 {
894 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu, cu_idx);
895 }
896 else if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 {
Greg Clayton1f746072012-08-29 21:13:06 +0000898 // Let the debug map create the compile unit
899 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
900 dwarf_cu->SetUserData(cu_sp.get());
901 }
902 else
903 {
904 ModuleSP module_sp (m_obj_file->GetModule());
905 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000906 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000907 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
Greg Clayton1f746072012-08-29 21:13:06 +0000908 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000909 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000910 FileSpec cu_file_spec{cu_die.GetName(), false};
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000911 if (cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000912 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000913 // If we have a full path to the compile unit, we don't need to resolve
914 // the file. This can be expensive e.g. when the source files are NFS mounted.
Chaoren Lin372e9062015-06-09 17:54:27 +0000915 if (cu_file_spec.IsRelative())
Greg Clayton53eb1c22012-04-02 22:59:12 +0000916 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000917 const char *cu_comp_dir{cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000918 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
Greg Clayton1f746072012-08-29 21:13:06 +0000919 }
920
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000921 std::string remapped_file;
922 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file))
923 cu_file_spec.SetFile(remapped_file, false);
David Srbeckyd515e942015-07-08 14:00:04 +0000924 }
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000925
Greg Clayton5ce1a842015-08-27 18:09:44 +0000926 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000927
Jason Molendac7099582015-07-31 05:47:00 +0000928 bool is_optimized = dwarf_cu->GetIsOptimized ();
David Srbeckyd515e942015-07-08 14:00:04 +0000929 cu_sp.reset(new CompileUnit (module_sp,
930 dwarf_cu,
931 cu_file_spec,
Greg Clayton6071e6f2015-08-26 22:57:51 +0000932 dwarf_cu->GetID(),
Jason Molenda6ab659a2015-07-29 00:42:47 +0000933 cu_language,
934 is_optimized));
David Srbeckyd515e942015-07-08 14:00:04 +0000935 if (cu_sp)
936 {
937 // If we just created a compile unit with an invalid file spec, try and get the
938 // first entry in the supports files from the line table as that should be the
939 // compile unit.
940 if (!cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000941 {
David Srbeckyd515e942015-07-08 14:00:04 +0000942 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
943 if (cu_file_spec)
944 {
945 (FileSpec &)(*cu_sp) = cu_file_spec;
946 // Also fix the invalid file spec which was copied from the compile unit.
947 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
948 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000949 }
David Srbeckyd515e942015-07-08 14:00:04 +0000950
951 dwarf_cu->SetUserData(cu_sp.get());
952
953 // Figure out the compile unit index if we weren't given one
954 if (cu_idx == UINT32_MAX)
955 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
956
957 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000958 }
959 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960 }
961 }
962 }
963 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000964 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965}
966
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967uint32_t
968SymbolFileDWARF::GetNumCompileUnits()
969{
970 DWARFDebugInfo* info = DebugInfo();
971 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 return 0;
974}
975
976CompUnitSP
977SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
978{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000979 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 DWARFDebugInfo* info = DebugInfo();
981 if (info)
982 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000983 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
984 if (dwarf_cu)
985 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000987 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988}
989
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990Function *
Greg Clayton6071e6f2015-08-26 22:57:51 +0000991SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, const DWARFDIE &die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000993 if (die.IsValid())
994 {
995 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996
Greg Clayton6071e6f2015-08-26 22:57:51 +0000997 if (type_system)
998 {
Greg Clayton261ac3f2015-08-28 01:01:03 +0000999 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1000 if (dwarf_ast)
1001 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
Greg Clayton6071e6f2015-08-26 22:57:51 +00001002 }
1003 }
1004 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005}
1006
Greg Clayton9422dd62013-03-04 21:46:16 +00001007bool
1008SymbolFileDWARF::FixupAddress (Address &addr)
1009{
1010 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1011 if (debug_map_symfile)
1012 {
1013 return debug_map_symfile->LinkOSOAddress(addr);
1014 }
1015 // This is a normal DWARF file, no address fixups need to happen
1016 return true;
1017}
Greg Clayton1f746072012-08-29 21:13:06 +00001018lldb::LanguageType
1019SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1020{
1021 assert (sc.comp_unit);
1022 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1023 if (dwarf_cu)
Greg Clayton5ce1a842015-08-27 18:09:44 +00001024 return dwarf_cu->GetLanguageType();
1025 else
1026 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +00001027}
1028
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029size_t
1030SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1031{
1032 assert (sc.comp_unit);
1033 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001034 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035 if (dwarf_cu)
1036 {
1037 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001038 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001040 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001042 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
1043 if (sc.comp_unit->FindFunctionByUID (die.GetID()).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001045 if (ParseCompileUnitFunction(sc, die))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 ++functions_added;
1047 }
1048 }
1049 //FixupTypes();
1050 }
1051 return functions_added;
1052}
1053
1054bool
1055SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1056{
1057 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001058 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001059 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001061 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062
Greg Claytonda2455b2012-11-01 17:28:37 +00001063 if (cu_die)
1064 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001065 const char * cu_comp_dir = resolveCompDir(cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001066
Greg Clayton5ce1a842015-08-27 18:09:44 +00001067 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET);
Tamas Berghammer1966ac32016-01-11 14:56:05 +00001068 if (stmt_list != DW_INVALID_OFFSET)
1069 {
1070 // All file indexes in DWARF are one based and a file of index zero is
1071 // supposed to be the compile unit itself.
1072 support_files.Append (*sc.comp_unit);
1073 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(),
1074 get_debug_line_data(),
1075 cu_comp_dir,
1076 stmt_list,
1077 support_files);
1078 }
Greg Claytonda2455b2012-11-01 17:28:37 +00001079 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 }
1081 return false;
1082}
1083
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001084bool
1085SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1086{
1087 assert (sc.comp_unit);
1088 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1089 if (dwarf_cu)
1090 {
1091 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1092 {
1093 UpdateExternalModuleListIfNeeded();
Sean Callananb0300a42016-01-14 21:46:09 +00001094
1095 if (sc.comp_unit)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001096 {
Sean Callananb0300a42016-01-14 21:46:09 +00001097 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1098
1099 if (die)
1100 {
1101 for (DWARFDIE child_die = die.GetFirstChild();
1102 child_die;
1103 child_die = child_die.GetSibling())
1104 {
1105 if (child_die.Tag() == DW_TAG_imported_declaration)
1106 {
1107 if (DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import))
1108 {
1109 if (module_die.Tag() == DW_TAG_module)
1110 {
1111 if (const char *name = module_die.GetAttributeValueAsString(DW_AT_name, nullptr))
1112 {
1113 ConstString const_name(name);
1114 imported_modules.push_back(const_name);
1115 }
1116 }
1117 }
1118 }
1119 }
1120 }
1121 }
1122 else
1123 {
1124 for (const auto &pair : m_external_type_modules)
1125 {
1126 imported_modules.push_back(pair.first);
1127 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001128 }
1129 }
1130 }
1131 return false;
1132}
1133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134struct ParseDWARFLineTableCallbackInfo
1135{
1136 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001137 std::unique_ptr<LineSequence> sequence_ap;
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001138 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139};
1140
1141//----------------------------------------------------------------------
1142// ParseStatementTableCallback
1143//----------------------------------------------------------------------
1144static void
1145ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1146{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1148 {
1149 // Just started parsing the line table
1150 }
1151 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1152 {
1153 // Done parsing line table, nothing to do for the cleanup
1154 }
1155 else
1156 {
1157 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001158 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159
Greg Clayton9422dd62013-03-04 21:46:16 +00001160 // If this is our first time here, we need to create a
1161 // sequence container.
1162 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001164 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1165 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001167 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001168 state.address & info->addr_mask,
Greg Clayton9422dd62013-03-04 21:46:16 +00001169 state.line,
1170 state.column,
1171 state.file,
1172 state.is_stmt,
1173 state.basic_block,
1174 state.prologue_end,
1175 state.epilogue_begin,
1176 state.end_sequence);
1177 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001179 // First, put the current sequence into the line table.
1180 line_table->InsertSequence(info->sequence_ap.get());
1181 // Then, empty it to prepare for the next sequence.
1182 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 }
1185}
1186
1187bool
1188SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1189{
1190 assert (sc.comp_unit);
1191 if (sc.comp_unit->GetLineTable() != NULL)
1192 return true;
1193
Greg Clayton1f746072012-08-29 21:13:06 +00001194 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 if (dwarf_cu)
1196 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001197 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001198 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001200 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 +00001201 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001203 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001204 if (line_table_ap.get())
1205 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001206 ParseDWARFLineTableCallbackInfo info;
1207 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001208
1209 /*
1210 * MIPS:
1211 * The SymbolContext may not have a valid target, thus we may not be able
1212 * to call Address::GetOpcodeLoadAddress() which would clear the bit #0
1213 * for MIPS. Use ArchSpec to clear the bit #0.
1214 */
1215 ArchSpec arch;
1216 GetObjectFile()->GetArchitecture(arch);
1217 switch (arch.GetMachine())
1218 {
1219 case llvm::Triple::mips:
1220 case llvm::Triple::mipsel:
1221 case llvm::Triple::mips64:
1222 case llvm::Triple::mips64el:
1223 info.addr_mask = ~((lldb::addr_t)1);
1224 break;
1225 default:
1226 info.addr_mask = ~((lldb::addr_t)0);
1227 break;
1228 }
1229
Greg Claytonc7bece562013-01-25 18:06:21 +00001230 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001231 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001232 if (m_debug_map_symfile)
1233 {
1234 // We have an object file that has a line table with addresses
1235 // that are not linked. We need to link the line table and convert
1236 // the addresses that are relative to the .o file into addresses
1237 // for the main executable.
1238 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1239 }
1240 else
1241 {
1242 sc.comp_unit->SetLineTable(line_table_ap.release());
1243 return true;
1244 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001245 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246 }
1247 }
1248 }
1249 return false;
1250}
1251
Siva Chandrad8335e92015-12-16 00:22:08 +00001252lldb_private::DebugMacrosSP
1253SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset)
1254{
1255 auto iter = m_debug_macros_map.find(*offset);
1256 if (iter != m_debug_macros_map.end())
1257 return iter->second;
1258
1259 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1260 if (debug_macro_data.GetByteSize() == 0)
1261 return DebugMacrosSP();
1262
1263 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1264 m_debug_macros_map[*offset] = debug_macros_sp;
1265
1266 const DWARFDebugMacroHeader &header = DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1267 DWARFDebugMacroEntry::ReadMacroEntries(
1268 debug_macro_data, get_debug_str_data(), header.OffsetIs64Bit(), offset, this, debug_macros_sp);
1269
1270 return debug_macros_sp;
1271}
1272
1273bool
1274SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext& sc)
1275{
1276 assert (sc.comp_unit);
1277
1278 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1279 if (dwarf_cu == nullptr)
1280 return false;
1281
1282 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1283 if (!dwarf_cu_die)
1284 return false;
1285
1286 lldb::offset_t sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1287 if (sect_offset == DW_INVALID_OFFSET)
1288 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros, DW_INVALID_OFFSET);
1289 if (sect_offset == DW_INVALID_OFFSET)
1290 return false;
1291
1292 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1293
1294 return true;
1295}
1296
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00001298SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext& sc,
1299 Block *parent_block,
1300 const DWARFDIE &orig_die,
1301 addr_t subprogram_low_pc,
1302 uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303{
1304 size_t blocks_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001305 DWARFDIE die = orig_die;
1306 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001308 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001309
1310 switch (tag)
1311 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001313 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314 case DW_TAG_lexical_block:
1315 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001316 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001317 if (tag == DW_TAG_subprogram)
1318 {
1319 // Skip any DW_TAG_subprogram DIEs that are inside
1320 // of a normal or inlined functions. These will be
1321 // parsed on their own as separate entities.
1322
1323 if (depth > 0)
1324 break;
1325
1326 block = parent_block;
1327 }
1328 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001329 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001330 BlockSP block_sp(new Block (die.GetID()));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001331 parent_block->AddChild(block_sp);
1332 block = block_sp.get();
1333 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001334 DWARFRangeList ranges;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001335 const char *name = NULL;
1336 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 int decl_file = 0;
1339 int decl_line = 0;
1340 int decl_column = 0;
1341 int call_file = 0;
1342 int call_line = 0;
1343 int call_column = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001344 if (die.GetDIENamesAndRanges (name,
1345 mangled_name,
1346 ranges,
1347 decl_file, decl_line, decl_column,
1348 call_file, call_line, call_column, nullptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 {
1350 if (tag == DW_TAG_subprogram)
1351 {
1352 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001353 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001355 else if (tag == DW_TAG_inlined_subroutine)
1356 {
1357 // We get called here for inlined subroutines in two ways.
1358 // The first time is when we are making the Function object
1359 // for this inlined concrete instance. Since we're creating a top level block at
1360 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1361 // adjust the containing address.
1362 // The second time is when we are parsing the blocks inside the function that contains
1363 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1364 // function the offset will be for that function.
1365 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1366 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001367 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001368 }
1369 }
Greg Clayton103f3092015-01-15 03:04:37 +00001370
1371 const size_t num_ranges = ranges.GetSize();
1372 for (size_t i = 0; i<num_ranges; ++i)
1373 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001374 const DWARFRangeList::Entry &range = ranges.GetEntryRef (i);
Greg Clayton103f3092015-01-15 03:04:37 +00001375 const addr_t range_base = range.GetRangeBase();
1376 if (range_base >= subprogram_low_pc)
1377 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1378 else
1379 {
1380 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",
1381 block->GetID(),
1382 range_base,
1383 range.GetRangeEnd(),
1384 subprogram_low_pc);
1385 }
1386 }
1387 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388
1389 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1390 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001391 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001393 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1394 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395
Greg Clayton7b0992d2013-04-18 22:45:39 +00001396 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001398 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1399 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001401 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 }
1403
1404 ++blocks_added;
1405
Greg Clayton6071e6f2015-08-26 22:57:51 +00001406 if (die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001408 blocks_added += ParseFunctionBlocks (sc,
1409 block,
Greg Clayton6071e6f2015-08-26 22:57:51 +00001410 die.GetFirstChild(),
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001411 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001412 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 }
1414 }
1415 }
1416 break;
1417 default:
1418 break;
1419 }
1420
Greg Claytondd7feaf2011-08-12 17:54:33 +00001421 // Only parse siblings of the block if we are not at depth zero. A depth
1422 // of zero indicates we are currently parsing the top level
1423 // DW_TAG_subprogram DIE
1424
1425 if (depth == 0)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001426 die.Clear();
Greg Claytondd7feaf2011-08-12 17:54:33 +00001427 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001428 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 }
1430 return blocks_added;
1431}
1432
Greg Claytonf0705c82011-10-22 03:33:13 +00001433bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001434SymbolFileDWARF::ClassOrStructIsVirtual (const DWARFDIE &parent_die)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001435{
1436 if (parent_die)
1437 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001438 for (DWARFDIE die = parent_die.GetFirstChild(); die; die = die.GetSibling())
Greg Claytonc4ffd662013-03-08 01:37:30 +00001439 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001440 dw_tag_t tag = die.Tag();
Greg Claytonc4ffd662013-03-08 01:37:30 +00001441 bool check_virtuality = false;
1442 switch (tag)
1443 {
1444 case DW_TAG_inheritance:
1445 case DW_TAG_subprogram:
1446 check_virtuality = true;
1447 break;
1448 default:
1449 break;
1450 }
1451 if (check_virtuality)
1452 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001453 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001454 return true;
1455 }
1456 }
1457 }
1458 return false;
1459}
1460
Paul Hermanea188fc2015-09-16 18:48:30 +00001461void
1462SymbolFileDWARF::ParseDeclsForContext (CompilerDeclContext decl_ctx)
1463{
1464 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1465 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1466 std::vector<DWARFDIE> decl_ctx_die_list = ast_parser->GetDIEForDeclContext(decl_ctx);
1467
1468 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1469 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl; decl = decl.GetSibling())
1470 ast_parser->GetDeclForUIDFromDWARF(decl);
1471}
1472
Paul Hermand628cbb2015-09-15 23:44:17 +00001473CompilerDecl
1474SymbolFileDWARF::GetDeclForUID (lldb::user_id_t type_uid)
1475{
1476 if (UserIDMatches(type_uid))
1477 {
1478 DWARFDebugInfo* debug_info = DebugInfo();
1479 if (debug_info)
1480 {
1481 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
1482 if (die)
1483 {
1484 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1485 if (dwarf_ast)
1486 return dwarf_ast->GetDeclForUIDFromDWARF(die);
1487 }
1488 }
1489 }
1490 return CompilerDecl();
1491}
1492
Greg Clayton99558cc42015-08-24 23:46:31 +00001493CompilerDeclContext
1494SymbolFileDWARF::GetDeclContextForUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001496 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00001498 DWARFDebugInfo* debug_info = DebugInfo();
1499 if (debug_info)
1500 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001501 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001502 if (die)
1503 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001504 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1505 if (dwarf_ast)
1506 return dwarf_ast->GetDeclContextForUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001507 }
1508 }
Sean Callanan72e49402011-08-05 23:43:37 +00001509 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001510 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001511}
1512
Greg Clayton99558cc42015-08-24 23:46:31 +00001513CompilerDeclContext
1514SymbolFileDWARF::GetDeclContextContainingUID (lldb::user_id_t type_uid)
Sean Callanan72e49402011-08-05 23:43:37 +00001515{
Greg Clayton81c22f62011-10-19 18:09:39 +00001516 if (UserIDMatches(type_uid))
Greg Clayton8b4edba2015-08-14 20:02:05 +00001517 {
1518 DWARFDebugInfo* debug_info = DebugInfo();
1519 if (debug_info)
1520 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001521 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001522 if (die)
1523 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001524 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1525 if (dwarf_ast)
1526 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001527 }
1528 }
1529 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001530 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531}
1532
Greg Clayton99558cc42015-08-24 23:46:31 +00001533
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001535SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536{
Greg Clayton81c22f62011-10-19 18:09:39 +00001537 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001539 DWARFDebugInfo* debug_info = DebugInfo();
1540 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001541 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001542 DWARFDIE type_die = debug_info->GetDIE (DIERef(type_uid));
Greg Clayton6071e6f2015-08-26 22:57:51 +00001543 if (type_die)
1544 {
1545 const bool assert_not_being_parsed = true;
1546 return ResolveTypeUID (type_die, assert_not_being_parsed);
1547 }
Greg Claytonca512b32011-01-14 04:54:56 +00001548 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549 }
1550 return NULL;
1551}
1552
Greg Claytoncab36a32011-12-08 05:16:30 +00001553Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001554SymbolFileDWARF::ResolveTypeUID (const DWARFDIE &die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001555{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001556 if (die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001557 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001558 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00001559 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001560 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001561 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001562 die.GetOffset(),
1563 die.GetTagAsCString(),
1564 die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001565
Greg Claytoncab36a32011-12-08 05:16:30 +00001566 // We might be coming in in the middle of a type tree (a class
1567 // withing a class, an enum within a class), so parse any needed
1568 // parent DIEs before we get to this one...
Greg Clayton6071e6f2015-08-26 22:57:51 +00001569 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE (die);
1570 if (decl_ctx_die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001571 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001572 if (log)
Greg Claytoncab36a32011-12-08 05:16:30 +00001573 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001574 switch (decl_ctx_die.Tag())
1575 {
1576 case DW_TAG_structure_type:
1577 case DW_TAG_union_type:
1578 case DW_TAG_class_type:
1579 {
1580 // Get the type, which could be a forward declaration
1581 if (log)
1582 GetObjectFile()->GetModule()->LogMessage (log,
1583 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1584 die.GetOffset(),
1585 die.GetTagAsCString(),
1586 die.GetName(),
1587 decl_ctx_die.GetOffset());
1588 }
1589 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001590
Greg Clayton6071e6f2015-08-26 22:57:51 +00001591 default:
1592 break;
1593 }
1594 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001595 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001596 return ResolveType (die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001597 }
1598 return NULL;
1599}
1600
Greg Clayton6beaaa62011-01-17 03:46:26 +00001601// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1602// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001603// can resolve a compiler_type.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001604bool
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001605SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &compiler_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001606{
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001607 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00001608 if (GetForwardDeclClangTypeToDie().count (compiler_type_no_qualifiers.GetOpaqueQualType()))
1609 {
1610 return true;
1611 }
1612 TypeSystem *type_system = compiler_type.GetTypeSystem();
1613 if (type_system)
1614 {
1615 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1616 if (dwarf_ast)
1617 return dwarf_ast->CanCompleteType(compiler_type);
1618 }
1619 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001620}
1621
1622
Greg Clayton57ee3062013-07-11 22:46:58 +00001623bool
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001624SymbolFileDWARF::CompleteType (CompilerType &compiler_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001625{
Greg Claytonfb85e622016-02-09 22:36:24 +00001626 lldb_private::Mutex::Locker locker(GetObjectFile()->GetModule()->GetMutex());
1627
Greg Claytone6b36cd2015-12-08 01:02:08 +00001628 TypeSystem *type_system = compiler_type.GetTypeSystem();
1629 if (type_system)
1630 {
1631 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1632 if (dwarf_ast && dwarf_ast->CanCompleteType(compiler_type))
1633 return dwarf_ast->CompleteType(compiler_type);
1634 }
1635
Greg Clayton1be10fc2010-09-29 01:12:09 +00001636 // We have a struct/union/class/enum that needs to be fully resolved.
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001637 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type);
1638 auto die_it = GetForwardDeclClangTypeToDie().find (compiler_type_no_qualifiers.GetOpaqueQualType());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001639 if (die_it == GetForwardDeclClangTypeToDie().end())
Greg Clayton73b472d2010-10-27 03:32:59 +00001640 {
1641 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00001642 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00001643 }
Tamas Berghammerf8fd9b52015-09-14 15:44:29 +00001644
1645 DWARFDebugInfo* debug_info = DebugInfo();
1646 DWARFDIE dwarf_die = debug_info->GetDIE(die_it->getSecond());
1647
Tamas Berghammer69d0b332015-10-09 12:43:08 +00001648 assert(UserIDMatches(die_it->getSecond().GetUID()) && "CompleteType called on the wrong SymbolFile");
1649
Greg Clayton73b472d2010-10-27 03:32:59 +00001650 // Once we start resolving this type, remove it from the forward declaration
1651 // map in case anyone child members or other types require this type to get resolved.
1652 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1653 // are done.
Tamas Berghammerf8fd9b52015-09-14 15:44:29 +00001654 GetForwardDeclClangTypeToDie().erase (die_it);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001655
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001656 Type *type = GetDIEToType().lookup (dwarf_die.GetDIE());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001657
Greg Clayton5160ce52013-03-27 23:08:40 +00001658 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001659 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001660 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00001661 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001662 dwarf_die.GetID(),
1663 dwarf_die.GetTagAsCString(),
Greg Claytond61c0fc2012-04-23 22:55:20 +00001664 type->GetName().AsCString());
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001665 assert (compiler_type);
Greg Clayton261ac3f2015-08-28 01:01:03 +00001666 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1667 if (dwarf_ast)
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001668 return dwarf_ast->CompleteTypeFromDWARF (dwarf_die, type, compiler_type);
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001669 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001670}
1671
Greg Clayton8b4edba2015-08-14 20:02:05 +00001672Type*
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00001673SymbolFileDWARF::ResolveType (const DWARFDIE &die, bool assert_not_being_parsed, bool resolve_function_context)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001674{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001675 if (die)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001676 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001677 Type *type = GetDIEToType().lookup (die.GetDIE());
Greg Claytoncab36a32011-12-08 05:16:30 +00001678
Greg Claytonc685f8e2010-09-15 04:15:46 +00001679 if (type == NULL)
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00001680 type = GetTypeForDIE (die, resolve_function_context).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001681
Greg Clayton24739922010-10-13 03:15:28 +00001682 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001683 {
1684 if (type != DIE_IS_BEING_PARSED)
1685 return type;
1686
1687 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001688 die.GetOffset(),
1689 die.GetTagAsCString(),
1690 die.GetName());
Jim Inghamc3549282012-01-11 02:21:12 +00001691
1692 }
1693 else
1694 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001695 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001696 return nullptr;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001697}
1698
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001700SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701{
1702 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001703 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 {
1705 // The symbol vendor doesn't know about this compile unit, we
1706 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00001707 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001709 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710}
1711
Greg Clayton8b4edba2015-08-14 20:02:05 +00001712size_t
1713SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets)
1714{
1715 method_die_offsets.clear();
1716 if (m_using_apple_tables)
1717 {
1718 if (m_apple_objc_ap.get())
1719 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1720 }
1721 else
1722 {
1723 if (!m_indexed)
1724 Index ();
1725
1726 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1727 }
1728 return method_die_offsets.size();
1729}
1730
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001731bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001732SymbolFileDWARF::GetFunction (const DWARFDIE &die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733{
Greg Clayton72310352013-02-23 04:12:47 +00001734 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735
Greg Clayton6071e6f2015-08-26 22:57:51 +00001736 if (die)
1737 {
1738 // Check if the symbol vendor already knows about this compile unit?
1739 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1740
1741 sc.function = sc.comp_unit->FindFunctionByUID (die.GetID()).get();
1742 if (sc.function == NULL)
1743 sc.function = ParseCompileUnitFunction(sc, die);
1744
1745 if (sc.function)
1746 {
1747 sc.module_sp = sc.function->CalculateSymbolContextModule();
1748 return true;
1749 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00001750 }
1751
1752 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753}
1754
Greg Claytone6b36cd2015-12-08 01:02:08 +00001755lldb::ModuleSP
1756SymbolFileDWARF::GetDWOModule (ConstString name)
1757{
1758 UpdateExternalModuleListIfNeeded();
1759 const auto &pos = m_external_type_modules.find(name);
1760 if (pos != m_external_type_modules.end())
1761 return pos->second;
1762 else
1763 return lldb::ModuleSP();
1764}
1765
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001766void
1767SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
1768{
1769 if (m_fetched_external_modules)
1770 return;
1771 m_fetched_external_modules = true;
1772
1773 DWARFDebugInfo * debug_info = DebugInfo();
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001774
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001775 const uint32_t num_compile_units = GetNumCompileUnits();
1776 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1777 {
1778 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1779
Greg Clayton5ce1a842015-08-27 18:09:44 +00001780 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1781 if (die && die.HasChildren() == false)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001782 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001783 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1784
1785 if (name)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001786 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001787 ConstString const_name(name);
1788 if (m_external_type_modules.find(const_name) == m_external_type_modules.end())
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001789 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001790 ModuleSP module_sp;
1791 const char *dwo_path = die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1792 if (dwo_path)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001793 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001794 ModuleSpec dwo_module_spec;
1795 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1796 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
1797 //printf ("Loading dwo = '%s'\n", dwo_path);
1798 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001799 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00001800 m_external_type_modules[const_name] = module_sp;
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001801 }
1802 }
1803 }
1804 }
1805}
Greg Clayton2501e5e2015-01-15 02:59:20 +00001806
1807SymbolFileDWARF::GlobalVariableMap &
1808SymbolFileDWARF::GetGlobalAranges()
1809{
1810 if (!m_global_aranges_ap)
1811 {
1812 m_global_aranges_ap.reset (new GlobalVariableMap());
1813
1814 ModuleSP module_sp = GetObjectFile()->GetModule();
1815 if (module_sp)
1816 {
1817 const size_t num_cus = module_sp->GetNumCompileUnits();
1818 for (size_t i = 0; i < num_cus; ++i)
1819 {
1820 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1821 if (cu_sp)
1822 {
1823 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1824 if (globals_sp)
1825 {
1826 const size_t num_globals = globals_sp->GetSize();
1827 for (size_t g = 0; g < num_globals; ++g)
1828 {
1829 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1830 if (var_sp && !var_sp->GetLocationIsConstantValueData())
1831 {
1832 const DWARFExpression &location = var_sp->LocationExpression();
1833 Value location_result;
1834 Error error;
1835 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
1836 {
1837 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
1838 {
1839 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
1840 lldb::addr_t byte_size = 1;
1841 if (var_sp->GetType())
1842 byte_size = var_sp->GetType()->GetByteSize();
1843 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
1844 }
1845 }
1846 }
1847 }
1848 }
1849 }
1850 }
1851 }
1852 m_global_aranges_ap->Sort();
1853 }
1854 return *m_global_aranges_ap;
1855}
1856
1857
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858uint32_t
1859SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1860{
1861 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00001862 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001863 static_cast<void*>(so_addr.GetSection().get()),
1864 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00001866 if (resolve_scope & ( eSymbolContextCompUnit |
1867 eSymbolContextFunction |
1868 eSymbolContextBlock |
1869 eSymbolContextLineEntry |
1870 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871 {
1872 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1873
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001874 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001875 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001876 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001877 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00001878 if (cu_offset == DW_INVALID_OFFSET)
1879 {
1880 // Global variables are not in the compile unit address ranges. The only way to
1881 // currently find global variables is to iterate over the .debug_pubnames or the
1882 // __apple_names table and find all items in there that point to DW_TAG_variable
1883 // DIEs and then find the address that matches.
1884 if (resolve_scope & eSymbolContextVariable)
1885 {
1886 GlobalVariableMap &map = GetGlobalAranges();
1887 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
1888 if (entry && entry->data)
1889 {
1890 Variable *variable = entry->data;
1891 SymbolContextScope *scc = variable->GetSymbolContextScope();
1892 if (scc)
1893 {
1894 scc->CalculateSymbolContext(&sc);
1895 sc.variable = variable;
1896 }
1897 return sc.GetResolvedMask();
1898 }
1899 }
1900 }
1901 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001902 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001903 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001904 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001905 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001907 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001908 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001909 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001910 resolved |= eSymbolContextCompUnit;
1911
Greg Clayton6ab80132012-12-12 17:30:52 +00001912 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001913 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1914 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001915 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1916 DWARFDIE block_die;
1917 if (function_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001919 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001920 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001921 sc.function = ParseCompileUnitFunction(sc, function_die);
1922
1923 if (sc.function && (resolve_scope & eSymbolContextBlock))
1924 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001925 }
1926 else
1927 {
1928 // We might have had a compile unit that had discontiguous
1929 // address ranges where the gaps are symbols that don't have
1930 // any debug info. Discontiguous compile unit address ranges
1931 // should only happen when there aren't other functions from
1932 // other compile units in these gaps. This helps keep the size
1933 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00001934 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001935 }
1936
1937 if (sc.function != NULL)
1938 {
1939 resolved |= eSymbolContextFunction;
1940
1941 if (resolve_scope & eSymbolContextBlock)
1942 {
1943 Block& block = sc.function->GetBlock (true);
1944
Greg Clayton6071e6f2015-08-26 22:57:51 +00001945 if (block_die)
1946 sc.block = block.FindBlockByID (block_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001947 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001948 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001949 if (sc.block)
1950 resolved |= eSymbolContextBlock;
1951 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 }
1953 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001954
1955 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
1956 {
1957 LineTable *line_table = sc.comp_unit->GetLineTable();
1958 if (line_table != NULL)
1959 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001960 // And address that makes it into this function should be in terms
1961 // of this debug file if there is no debug map, or it will be an
1962 // address in the .o file which needs to be fixed up to be in terms
1963 // of the debug map executable. Either way, calling FixupAddress()
1964 // will work for us.
1965 Address exe_so_addr (so_addr);
1966 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00001967 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001968 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00001969 {
1970 resolved |= eSymbolContextLineEntry;
1971 }
1972 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001973 }
1974 }
1975
1976 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
1977 {
1978 // We might have had a compile unit that had discontiguous
1979 // address ranges where the gaps are symbols that don't have
1980 // any debug info. Discontiguous compile unit address ranges
1981 // should only happen when there aren't other functions from
1982 // other compile units in these gaps. This helps keep the size
1983 // of the aranges down.
1984 sc.comp_unit = NULL;
1985 resolved &= ~eSymbolContextCompUnit;
1986 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001988 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001990 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
1991 cu_offset,
1992 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993 }
1994 }
1995 }
1996 }
1997 }
1998 return resolved;
1999}
2000
2001
2002
2003uint32_t
2004SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2005{
2006 const uint32_t prev_size = sc_list.GetSize();
2007 if (resolve_scope & eSymbolContextCompUnit)
2008 {
2009 DWARFDebugInfo* debug_info = DebugInfo();
2010 if (debug_info)
2011 {
2012 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002013 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014
Greg Clayton53eb1c22012-04-02 22:59:12 +00002015 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002016 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002017 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00002018 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00002019 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 +00002020 if (check_inlines || file_spec_matches_cu_file_spec)
2021 {
2022 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002023 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002024 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002026 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002027
Greg Clayton526a4ae2012-05-16 22:09:01 +00002028 // If we are looking for inline functions only and we don't
2029 // find it in the support files, we are done.
2030 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002031 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002032 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2033 if (file_idx == UINT32_MAX)
2034 continue;
2035 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036
Greg Clayton526a4ae2012-05-16 22:09:01 +00002037 if (line != 0)
2038 {
2039 LineTable *line_table = sc.comp_unit->GetLineTable();
2040
2041 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002043 // We will have already looked up the file index if
2044 // we are searching for inline entries.
2045 if (!check_inlines)
2046 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047
Greg Clayton526a4ae2012-05-16 22:09:01 +00002048 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002050 uint32_t found_line;
2051 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2052 found_line = sc.line_entry.line;
2053
2054 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002056 sc.function = NULL;
2057 sc.block = NULL;
2058 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002059 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002060 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2061 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002062 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002063 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
2064 DWARFDIE block_die;
2065 if (function_die)
Greg Clayton526a4ae2012-05-16 22:09:01 +00002066 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002067 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00002068 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002069 sc.function = ParseCompileUnitFunction(sc, function_die);
2070
2071 if (sc.function && (resolve_scope & eSymbolContextBlock))
2072 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002073 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002074
Greg Clayton526a4ae2012-05-16 22:09:01 +00002075 if (sc.function != NULL)
2076 {
2077 Block& block = sc.function->GetBlock (true);
2078
Greg Clayton6071e6f2015-08-26 22:57:51 +00002079 if (block_die)
2080 sc.block = block.FindBlockByID (block_die.GetID());
2081 else if (function_die)
2082 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00002083 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084 }
2085 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002086
Greg Clayton526a4ae2012-05-16 22:09:01 +00002087 sc_list.Append(sc);
2088 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2089 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002090 }
2091 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002092 else if (file_spec_matches_cu_file_spec && !check_inlines)
2093 {
2094 // only append the context if we aren't looking for inline call sites
2095 // by file and line and if the file spec matches that of the compile unit
2096 sc_list.Append(sc);
2097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002098 }
2099 else if (file_spec_matches_cu_file_spec && !check_inlines)
2100 {
2101 // only append the context if we aren't looking for inline call sites
2102 // by file and line and if the file spec matches that of the compile unit
2103 sc_list.Append(sc);
2104 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105
Greg Clayton526a4ae2012-05-16 22:09:01 +00002106 if (!check_inlines)
2107 break;
2108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002109 }
2110 }
2111 }
2112 }
2113 return sc_list.GetSize() - prev_size;
2114}
2115
2116void
2117SymbolFileDWARF::Index ()
2118{
2119 if (m_indexed)
2120 return;
2121 m_indexed = true;
2122 Timer scoped_timer (__PRETTY_FUNCTION__,
2123 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00002124 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125
2126 DWARFDebugInfo* debug_info = DebugInfo();
2127 if (debug_info)
2128 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129 const uint32_t num_compile_units = GetNumCompileUnits();
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002130 std::vector<NameToDIE> function_basename_index(num_compile_units);
2131 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2132 std::vector<NameToDIE> function_method_index(num_compile_units);
2133 std::vector<NameToDIE> function_selector_index(num_compile_units);
2134 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2135 std::vector<NameToDIE> global_index(num_compile_units);
2136 std::vector<NameToDIE> type_index(num_compile_units);
2137 std::vector<NameToDIE> namespace_index(num_compile_units);
2138
2139 auto parser_fn = [this,
2140 debug_info,
2141 &function_basename_index,
2142 &function_fullname_index,
2143 &function_method_index,
2144 &function_selector_index,
2145 &objc_class_selectors_index,
2146 &global_index,
2147 &type_index,
2148 &namespace_index](uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002149 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002150 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002151 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded(false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002153 dwarf_cu->Index(function_basename_index[cu_idx],
2154 function_fullname_index[cu_idx],
2155 function_method_index[cu_idx],
2156 function_selector_index[cu_idx],
2157 objc_class_selectors_index[cu_idx],
2158 global_index[cu_idx],
2159 type_index[cu_idx],
2160 namespace_index[cu_idx]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162 // Keep memory down by clearing DIEs if this generate function
2163 // caused them to be parsed
2164 if (clear_dies)
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002165 dwarf_cu->ClearDIEs(true);
2166
2167 return cu_idx;
2168 };
2169
2170 TaskRunner<uint32_t> task_runner;
2171 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2172 task_runner.AddTask(parser_fn, cu_idx);
2173
2174 while (true)
2175 {
2176 std::future<uint32_t> f = task_runner.WaitForNextCompletedTask();
2177 if (!f.valid())
2178 break;
2179 uint32_t cu_idx = f.get();
2180
2181 m_function_basename_index.Append(function_basename_index[cu_idx]);
2182 m_function_fullname_index.Append(function_fullname_index[cu_idx]);
2183 m_function_method_index.Append(function_method_index[cu_idx]);
2184 m_function_selector_index.Append(function_selector_index[cu_idx]);
2185 m_objc_class_selectors_index.Append(objc_class_selectors_index[cu_idx]);
2186 m_global_index.Append(global_index[cu_idx]);
2187 m_type_index.Append(type_index[cu_idx]);
2188 m_namespace_index.Append(namespace_index[cu_idx]);
Tamas Berghammerda4e8ed2015-10-20 15:43:40 +00002189 }
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002190
2191 TaskPool::RunTasks(
2192 [&]() { m_function_basename_index.Finalize(); },
2193 [&]() { m_function_fullname_index.Finalize(); },
2194 [&]() { m_function_method_index.Finalize(); },
2195 [&]() { m_function_selector_index.Finalize(); },
2196 [&]() { m_objc_class_selectors_index.Finalize(); },
2197 [&]() { m_global_index.Finalize(); },
2198 [&]() { m_type_index.Finalize(); },
2199 [&]() { m_namespace_index.Finalize(); });
Greg Claytonc685f8e2010-09-15 04:15:46 +00002200
Greg Clayton24739922010-10-13 03:15:28 +00002201#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002202 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002203 s.Printf ("DWARF index for '%s':",
2204 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002205 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2206 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2207 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2208 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2209 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2210 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002211 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00002212 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002213#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214 }
2215}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002216
2217bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002218SymbolFileDWARF::DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx)
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002219{
Greg Clayton99558cc42015-08-24 23:46:31 +00002220 if (decl_ctx == nullptr || !decl_ctx->IsValid())
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002221 {
2222 // Invalid namespace decl which means we aren't matching only things
2223 // in this symbol file, so return true to indicate it matches this
2224 // symbol file.
2225 return true;
2226 }
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002227
Greg Clayton56939cb2015-09-17 22:23:34 +00002228 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2229 TypeSystem *type_system = GetTypeSystemForLanguage(decl_ctx_type_system->GetMinimumLanguage(nullptr));
2230 if (decl_ctx_type_system == type_system)
Greg Clayton99558cc42015-08-24 23:46:31 +00002231 return true; // The type systems match, return true
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002232
2233 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00002234 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00002235
2236 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002237 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002238
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002239 return false;
2240}
2241
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002243SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002244{
Greg Clayton5160ce52013-03-27 23:08:40 +00002245 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002246
2247 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002248 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002249 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00002250 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002251 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002252 append, max_matches);
2253
Greg Clayton99558cc42015-08-24 23:46:31 +00002254 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002255 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002256
Greg Claytonc685f8e2010-09-15 04:15:46 +00002257 DWARFDebugInfo* info = DebugInfo();
2258 if (info == NULL)
2259 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260
2261 // If we aren't appending the results to this list, then clear the list
2262 if (!append)
2263 variables.Clear();
2264
2265 // Remember how many variables are in the list before we search in case
2266 // we are appending the results to a variable list.
2267 const uint32_t original_size = variables.GetSize();
2268
Greg Claytond4a2b372011-09-12 23:21:58 +00002269 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002270
Greg Clayton97fbc342011-10-20 22:30:33 +00002271 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002272 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002273 if (m_apple_names_ap.get())
2274 {
2275 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00002276 llvm::StringRef basename;
2277 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002278
Jim Inghamaa816b82015-09-02 01:59:14 +00002279 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, basename))
Jim Inghamfa39bb42014-10-25 00:33:55 +00002280 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002281
Jim Inghamfa39bb42014-10-25 00:33:55 +00002282 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00002283 }
Greg Clayton7f995132011-10-04 22:41:51 +00002284 }
2285 else
2286 {
2287 // Index the DWARF if we haven't already
2288 if (!m_indexed)
2289 Index ();
2290
2291 m_global_index.Find (name, die_offsets);
2292 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002293
Greg Clayton437a1352012-04-09 22:43:43 +00002294 const size_t num_die_matches = die_offsets.size();
2295 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002296 {
Greg Clayton7f995132011-10-04 22:41:51 +00002297 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002298 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002299 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002300
Greg Claytond4a2b372011-09-12 23:21:58 +00002301 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002302 bool done = false;
2303 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002304 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002305 const DIERef& die_ref = die_offsets[i];
2306 DWARFDIE die = debug_info->GetDIE (die_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002307
Greg Clayton95d87902011-11-11 03:16:25 +00002308 if (die)
2309 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002310 switch (die.Tag())
Greg Clayton437a1352012-04-09 22:43:43 +00002311 {
2312 default:
2313 case DW_TAG_subprogram:
2314 case DW_TAG_inlined_subroutine:
2315 case DW_TAG_try_block:
2316 case DW_TAG_catch_block:
2317 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002318
Greg Clayton437a1352012-04-09 22:43:43 +00002319 case DW_TAG_variable:
2320 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002321 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002322
Greg Clayton99558cc42015-08-24 23:46:31 +00002323 if (parent_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002324 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002325 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2326 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002327 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002328 CompilerDeclContext actual_parent_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002329 if (!actual_parent_decl_ctx || actual_parent_decl_ctx != *parent_decl_ctx)
2330 continue;
2331 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00002332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333
Greg Clayton6071e6f2015-08-26 22:57:51 +00002334 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002335
Greg Clayton437a1352012-04-09 22:43:43 +00002336 if (variables.GetSize() - original_size >= max_matches)
2337 done = true;
2338 }
2339 break;
2340 }
Greg Clayton95d87902011-11-11 03:16:25 +00002341 }
2342 else
2343 {
2344 if (m_using_apple_tables)
2345 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002346 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 +00002347 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002348 }
2349 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002350 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002351 }
2352
2353 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002354 const uint32_t num_matches = variables.GetSize() - original_size;
2355 if (log && num_matches > 0)
2356 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002357 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002358 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002359 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002360 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002361 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002362 num_matches);
2363 }
2364 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002365}
2366
2367uint32_t
2368SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2369{
Greg Clayton5160ce52013-03-27 23:08:40 +00002370 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002371
Greg Clayton21f2a492011-10-06 00:09:08 +00002372 if (log)
2373 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002374 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002375 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002376 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002377 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002378 }
2379
Greg Claytonc685f8e2010-09-15 04:15:46 +00002380 DWARFDebugInfo* info = DebugInfo();
2381 if (info == NULL)
2382 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383
2384 // If we aren't appending the results to this list, then clear the list
2385 if (!append)
2386 variables.Clear();
2387
2388 // Remember how many variables are in the list before we search in case
2389 // we are appending the results to a variable list.
2390 const uint32_t original_size = variables.GetSize();
2391
Greg Clayton7f995132011-10-04 22:41:51 +00002392 DIEArray die_offsets;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002393
Greg Clayton97fbc342011-10-20 22:30:33 +00002394 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002395 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002396 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002397 {
2398 DWARFMappedHash::DIEInfoArray hash_data_array;
2399 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2400 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2401 }
Greg Clayton7f995132011-10-04 22:41:51 +00002402 }
2403 else
2404 {
2405 // Index the DWARF if we haven't already
2406 if (!m_indexed)
2407 Index ();
2408
2409 m_global_index.Find (regex, die_offsets);
2410 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002411
Greg Claytonc685f8e2010-09-15 04:15:46 +00002412 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002413 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002414 assert (sc.module_sp);
2415
Greg Clayton7f995132011-10-04 22:41:51 +00002416 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002417 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002418 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002419 DWARFDebugInfo* debug_info = DebugInfo();
2420 for (size_t i=0; i<num_matches; ++i)
2421 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002422 const DIERef& die_ref = die_offsets[i];
2423 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00002424
2425 if (die)
2426 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002427 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002428
Greg Clayton6071e6f2015-08-26 22:57:51 +00002429 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002430
Greg Clayton95d87902011-11-11 03:16:25 +00002431 if (variables.GetSize() - original_size >= max_matches)
2432 break;
2433 }
2434 else
2435 {
2436 if (m_using_apple_tables)
2437 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002438 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 +00002439 die_ref.die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002440 }
2441 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002442 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002443 }
2444
2445 // Return the number of variable that were appended to the list
2446 return variables.GetSize() - original_size;
2447}
2448
Greg Claytonaa044962011-10-13 00:59:38 +00002449
Jim Ingham4cda6e02011-10-07 22:23:45 +00002450bool
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002451SymbolFileDWARF::ResolveFunction (const DIERef& die_ref,
Pavel Labatha73d6572015-03-13 10:22:00 +00002452 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00002453 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002454{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002455 DWARFDIE die = DebugInfo()->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002456 return ResolveFunction (die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00002457}
2458
2459
2460bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00002461SymbolFileDWARF::ResolveFunction (const DWARFDIE &orig_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002462 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00002463 SymbolContextList& sc_list)
2464{
Greg Clayton9e315582011-09-02 04:03:59 +00002465 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002466
Greg Clayton6071e6f2015-08-26 22:57:51 +00002467 if (!orig_die)
Greg Claytonaa044962011-10-13 00:59:38 +00002468 return false;
2469
Jim Ingham4cda6e02011-10-07 22:23:45 +00002470 // If we were passed a die that is not a function, just return false...
Greg Clayton6071e6f2015-08-26 22:57:51 +00002471 if (!(orig_die.Tag() == DW_TAG_subprogram || (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002472 return false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002473
2474 DWARFDIE die = orig_die;
2475 DWARFDIE inlined_die;
2476 if (die.Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002477 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002478 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002479
Greg Clayton6071e6f2015-08-26 22:57:51 +00002480 while (1)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002481 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002482 die = die.GetParent();
2483
2484 if (die)
2485 {
2486 if (die.Tag() == DW_TAG_subprogram)
2487 break;
2488 }
2489 else
Jim Ingham4cda6e02011-10-07 22:23:45 +00002490 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002491 }
2492 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002493 assert (die && die.Tag() == DW_TAG_subprogram);
2494 if (GetFunction (die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002495 {
2496 Address addr;
2497 // Parse all blocks if needed
2498 if (inlined_die)
2499 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002500 Block &function_block = sc.function->GetBlock (true);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002501 sc.block = function_block.FindBlockByID (inlined_die.GetID());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002502 if (sc.block == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002503 sc.block = function_block.FindBlockByID (inlined_die.GetOffset());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002504 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002505 addr.Clear();
2506 }
2507 else
2508 {
2509 sc.block = NULL;
2510 addr = sc.function->GetAddressRange().GetBaseAddress();
2511 }
2512
2513 if (addr.IsValid())
2514 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002515 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002516 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002517 }
2518 }
2519
Greg Claytonaa044962011-10-13 00:59:38 +00002520 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002521}
2522
Greg Clayton7f995132011-10-04 22:41:51 +00002523void
2524SymbolFileDWARF::FindFunctions (const ConstString &name,
2525 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002526 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002527 SymbolContextList& sc_list)
2528{
Greg Claytond4a2b372011-09-12 23:21:58 +00002529 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002530 if (name_to_die.Find (name, die_offsets))
2531 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002532 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002533 }
2534}
2535
2536
2537void
2538SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2539 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002540 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002541 SymbolContextList& sc_list)
2542{
2543 DIEArray die_offsets;
2544 if (name_to_die.Find (regex, die_offsets))
2545 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002546 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002547 }
2548}
2549
2550
2551void
2552SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2553 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00002554 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002555 SymbolContextList& sc_list)
2556{
2557 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002558 DWARFMappedHash::DIEInfoArray hash_data_array;
2559 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002560 {
Greg Claytond1767f02011-12-08 02:13:16 +00002561 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00002562 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002563 }
2564}
2565
2566void
2567SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00002568 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002569 SymbolContextList& sc_list)
2570{
2571 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002572 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002573 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002574 for (size_t i=0; i<num_matches; ++i)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002575 ResolveFunction (die_offsets[i], include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002576 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002577}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002578
Jim Ingham4cda6e02011-10-07 22:23:45 +00002579bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002580SymbolFileDWARF::DIEInDeclContext (const CompilerDeclContext *decl_ctx,
Greg Clayton6071e6f2015-08-26 22:57:51 +00002581 const DWARFDIE &die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002582{
2583 // If we have no parent decl context to match this DIE matches, and if the parent
2584 // decl context isn't valid, we aren't trying to look for any particular decl
2585 // context so any die matches.
2586 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2587 return true;
2588
Greg Clayton6071e6f2015-08-26 22:57:51 +00002589 if (die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002590 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002591 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2592 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002593 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002594 CompilerDeclContext actual_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002595 if (actual_decl_ctx)
2596 return actual_decl_ctx == *decl_ctx;
2597 }
2598 }
2599 return false;
2600}
2601
Greg Clayton0c5cd902010-06-28 21:30:43 +00002602uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002603SymbolFileDWARF::FindFunctions (const ConstString &name,
2604 const CompilerDeclContext *parent_decl_ctx,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002605 uint32_t name_type_mask,
2606 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002607 bool append,
2608 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002609{
2610 Timer scoped_timer (__PRETTY_FUNCTION__,
2611 "SymbolFileDWARF::FindFunctions (name = '%s')",
2612 name.AsCString());
2613
Greg Clayton43fe2172013-04-03 02:00:15 +00002614 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
2615 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
2616
Greg Clayton5160ce52013-03-27 23:08:40 +00002617 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002618
2619 if (log)
2620 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002621 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002622 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2623 name.GetCString(),
2624 name_type_mask,
2625 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002626 }
2627
Greg Clayton0c5cd902010-06-28 21:30:43 +00002628 // If we aren't appending the results to this list, then clear the list
2629 if (!append)
2630 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002631
Greg Clayton99558cc42015-08-24 23:46:31 +00002632 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002633 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002634
2635 // If name is empty then we won't find anything.
2636 if (name.IsEmpty())
2637 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002638
2639 // Remember how many sc_list are in the list before we search in case
2640 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002641
Jim Ingham4cda6e02011-10-07 22:23:45 +00002642 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00002643
2644 const uint32_t original_size = sc_list.GetSize();
2645
Jim Ingham4cda6e02011-10-07 22:23:45 +00002646 DWARFDebugInfo* info = DebugInfo();
2647 if (info == NULL)
2648 return 0;
2649
Greg Clayton43fe2172013-04-03 02:00:15 +00002650 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00002651 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002652 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002653 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002654 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002655
2656 DIEArray die_offsets;
2657
2658 uint32_t num_matches = 0;
2659
Greg Clayton43fe2172013-04-03 02:00:15 +00002660 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002661 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002662 // If they asked for the full name, match what they typed. At some point we may
2663 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2664 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002665 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002666 for (uint32_t i = 0; i < num_matches; i++)
2667 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002668 const DIERef& die_ref = die_offsets[i];
2669 DWARFDIE die = info->GetDIE (die_ref);
Greg Claytonaa044962011-10-13 00:59:38 +00002670 if (die)
2671 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002672 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002673 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002674
Greg Clayton6071e6f2015-08-26 22:57:51 +00002675 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002676 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002677 if (ResolveFunction (die, include_inlines, sc_list))
2678 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002679 }
Greg Claytonaa044962011-10-13 00:59:38 +00002680 }
Greg Clayton95d87902011-11-11 03:16:25 +00002681 else
2682 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002683 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 +00002684 die_ref.die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002685 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002686 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002687 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002688
2689 if (name_type_mask & eFunctionNameTypeSelector)
2690 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002691 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00002692 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00002693
Greg Clayton43fe2172013-04-03 02:00:15 +00002694 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2695 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2696 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00002697
Greg Clayton43fe2172013-04-03 02:00:15 +00002698 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00002699 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002700 const DIERef& die_ref = die_offsets[i];
2701 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002702 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00002703 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002704 const char *die_name = die.GetName();
Jim Inghamaa816b82015-09-02 01:59:14 +00002705 if (ObjCLanguage::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00002706 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002707 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002708 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002709 if (ResolveFunction (die, include_inlines, sc_list))
2710 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002711 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002712 }
2713 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002714 else
2715 {
2716 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 +00002717 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002718 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002719 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002720 die_offsets.clear();
2721 }
2722
Greg Clayton99558cc42015-08-24 23:46:31 +00002723 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) || name_type_mask & eFunctionNameTypeBase)
Greg Clayton43fe2172013-04-03 02:00:15 +00002724 {
2725 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2726 // extract the base name, look that up, and if there is any other information in the name we were
2727 // passed in we have to post-filter based on that.
2728
2729 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2730 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2731
2732 for (uint32_t i = 0; i < num_matches; i++)
2733 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002734 const DIERef& die_ref = die_offsets[i];
2735 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002736 if (die)
2737 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002738 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002739 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002740
Greg Clayton43fe2172013-04-03 02:00:15 +00002741
2742 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002743 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() && ResolveFunction (die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002744 {
2745 bool keep_die = true;
2746 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
2747 {
2748 // We are looking for either basenames or methods, so we need to
2749 // trim out the ones we won't want by looking at the type
2750 SymbolContext sc;
2751 if (sc_list.GetLastContext(sc))
2752 {
2753 if (sc.block)
2754 {
2755 // We have an inlined function
2756 }
2757 else if (sc.function)
2758 {
2759 Type *type = sc.function->GetType();
2760
Sean Callananc370a8a2013-09-18 22:59:55 +00002761 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00002762 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002763 CompilerDeclContext decl_ctx = GetDeclContextContainingUID (type->GetID());
2764 if (decl_ctx.IsStructUnionOrClass())
Greg Clayton43fe2172013-04-03 02:00:15 +00002765 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002766 if (name_type_mask & eFunctionNameTypeBase)
2767 {
2768 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2769 keep_die = false;
2770 }
2771 }
2772 else
2773 {
2774 if (name_type_mask & eFunctionNameTypeMethod)
2775 {
2776 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2777 keep_die = false;
2778 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002779 }
2780 }
2781 else
2782 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002783 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002784 die_ref.die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002785 }
2786 }
2787 }
2788 }
2789 if (keep_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002790 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002791 }
2792 }
2793 else
2794 {
2795 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 +00002796 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002797 }
2798 }
2799 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002800 }
2801 }
Greg Clayton7f995132011-10-04 22:41:51 +00002802 }
2803 else
2804 {
2805
2806 // Index the DWARF if we haven't already
2807 if (!m_indexed)
2808 Index ();
2809
Greg Clayton7f995132011-10-04 22:41:51 +00002810 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00002811 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002812 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002813
Ed Mastefc7baa02013-09-09 18:00:45 +00002814 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00002815 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00002816 // If we didn't find any functions in the global namespace try
2817 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00002818 // functions that have a namespace but keep functions which
2819 // have an anonymous namespace
2820 // TODO: The arch in the object file isn't correct for MSVC
2821 // binaries on windows, we should find a way to make it
2822 // correct and handle those symbols as well.
Aidan Doddsc78e8992015-11-10 14:10:57 +00002823 if (sc_list.GetSize() == original_size)
Matt Kopecd6089962013-05-10 17:53:48 +00002824 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002825 ArchSpec arch;
Greg Clayton99558cc42015-08-24 23:46:31 +00002826 if (!parent_decl_ctx &&
Robert Flackeb83fab2015-05-15 18:59:59 +00002827 GetObjectFile()->GetArchitecture(arch) &&
2828 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
2829 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00002830 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002831 SymbolContextList temp_sc_list;
2832 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00002833 SymbolContext sc;
2834 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
2835 {
2836 if (temp_sc_list.GetContextAtIndex(i, sc))
2837 {
Matt Kopeca189d492013-05-10 22:55:24 +00002838 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
2839 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00002840 // Mangled names on Linux and FreeBSD are of the form:
2841 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00002842 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2843 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00002844 {
2845 sc_list.Append(sc);
2846 }
2847 }
2848 }
2849 }
2850 }
Matt Kopecd6089962013-05-10 17:53:48 +00002851 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002852 DIEArray die_offsets;
Greg Clayton43fe2172013-04-03 02:00:15 +00002853 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002854 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002855 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002856 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002857 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002858 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002859 if (die)
2860 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002861 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002862 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002863
Greg Claytonaa044962011-10-13 00:59:38 +00002864 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002865 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002866 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002867 if (ResolveFunction (die, include_inlines, sc_list))
2868 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002869 }
Greg Claytonaa044962011-10-13 00:59:38 +00002870 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002871 }
2872 die_offsets.clear();
2873 }
2874
Greg Clayton43fe2172013-04-03 02:00:15 +00002875 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002876 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002877 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Sean Callanan213fdb82011-10-13 01:49:10 +00002878 return 0; // no methods in namespaces
2879
Greg Clayton43fe2172013-04-03 02:00:15 +00002880 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002881 {
Greg Claytonaa044962011-10-13 00:59:38 +00002882 for (uint32_t i = 0; i < num_base; i++)
2883 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002884 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002885 if (die)
2886 {
Greg Claytonaa044962011-10-13 00:59:38 +00002887 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002888 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002889 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002890 if (ResolveFunction (die, include_inlines, sc_list))
2891 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002892 }
Greg Claytonaa044962011-10-13 00:59:38 +00002893 }
2894 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002895 }
2896 die_offsets.clear();
2897 }
Greg Clayton7f995132011-10-04 22:41:51 +00002898
Greg Clayton99558cc42015-08-24 23:46:31 +00002899 if ((name_type_mask & eFunctionNameTypeSelector) && (!parent_decl_ctx || !parent_decl_ctx->IsValid()))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002900 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002901 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002902 }
2903
Greg Clayton4d01ace2011-09-29 16:58:15 +00002904 }
2905
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002906 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002907 const uint32_t num_matches = sc_list.GetSize() - original_size;
2908
2909 if (log && num_matches > 0)
2910 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002911 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00002912 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002913 name.GetCString(),
2914 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00002915 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00002916 append,
2917 num_matches);
2918 }
2919 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002920}
2921
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002922uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00002923SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002924{
2925 Timer scoped_timer (__PRETTY_FUNCTION__,
2926 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2927 regex.GetText());
2928
Greg Clayton5160ce52013-03-27 23:08:40 +00002929 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002930
2931 if (log)
2932 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002933 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002934 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2935 regex.GetText(),
2936 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002937 }
2938
2939
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002940 // If we aren't appending the results to this list, then clear the list
2941 if (!append)
2942 sc_list.Clear();
2943
2944 // Remember how many sc_list are in the list before we search in case
2945 // we are appending the results to a variable list.
2946 uint32_t original_size = sc_list.GetSize();
2947
Greg Clayton97fbc342011-10-20 22:30:33 +00002948 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002949 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002950 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00002951 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002952 }
2953 else
2954 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002955 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002956 if (!m_indexed)
2957 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002958
Pavel Labatha73d6572015-03-13 10:22:00 +00002959 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002960
Pavel Labatha73d6572015-03-13 10:22:00 +00002961 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002962 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002963
2964 // Return the number of variable that were appended to the list
2965 return sc_list.GetSize() - original_size;
2966}
Jim Ingham318c9f22011-08-26 19:44:13 +00002967
Siva Chandra9293fc42016-01-07 23:32:34 +00002968void
2969SymbolFileDWARF::GetMangledNamesForFunction (const std::string &scope_qualified_name,
2970 std::vector<ConstString> &mangled_names)
2971{
2972 DWARFDebugInfo* info = DebugInfo();
2973 uint32_t num_comp_units = 0;
2974 if (info)
2975 num_comp_units = info->GetNumCompileUnits();
2976
2977 for (uint32_t i = 0; i < num_comp_units; i++)
2978 {
2979 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2980 if (cu == nullptr)
2981 continue;
2982
2983 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2984 if (dwo)
2985 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2986 }
2987
2988 NameToOffsetMap::iterator iter = m_function_scope_qualified_name_map.find(scope_qualified_name);
2989 if (iter == m_function_scope_qualified_name_map.end())
2990 return;
2991
2992 DIERefSetSP set_sp = (*iter).second;
2993 std::set<DIERef>::iterator set_iter;
2994 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++)
2995 {
2996 DWARFDIE die = DebugInfo()->GetDIE (*set_iter);
2997 mangled_names.push_back(ConstString(die.GetMangledName()));
2998 }
2999}
3000
3001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003002uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003003SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3004 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00003005 const CompilerDeclContext *parent_decl_ctx,
Greg Claytond1767f02011-12-08 02:13:16 +00003006 bool append,
3007 uint32_t max_matches,
Ravitheja Addepally40697302015-10-08 09:45:41 +00003008 TypeMap& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003009{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003010 DWARFDebugInfo* info = DebugInfo();
3011 if (info == NULL)
3012 return 0;
3013
Greg Clayton5160ce52013-03-27 23:08:40 +00003014 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003015
Greg Clayton21f2a492011-10-06 00:09:08 +00003016 if (log)
3017 {
Greg Clayton99558cc42015-08-24 23:46:31 +00003018 if (parent_decl_ctx)
Greg Clayton5160ce52013-03-27 23:08:40 +00003019 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003020 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list)",
Greg Clayton437a1352012-04-09 22:43:43 +00003021 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003022 static_cast<const void*>(parent_decl_ctx),
3023 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003024 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00003025 else
Greg Clayton5160ce52013-03-27 23:08:40 +00003026 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003027 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003028 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00003029 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003030 }
3031
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032 // If we aren't appending the results to this list, then clear the list
3033 if (!append)
3034 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003035
Greg Clayton99558cc42015-08-24 23:46:31 +00003036 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00003037 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003038
Greg Claytond4a2b372011-09-12 23:21:58 +00003039 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003040
Greg Clayton97fbc342011-10-20 22:30:33 +00003041 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003042 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003043 if (m_apple_types_ap.get())
3044 {
3045 const char *name_cstr = name.GetCString();
3046 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3047 }
Greg Clayton7f995132011-10-04 22:41:51 +00003048 }
3049 else
3050 {
3051 if (!m_indexed)
3052 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003053
Greg Clayton7f995132011-10-04 22:41:51 +00003054 m_type_index.Find (name, die_offsets);
3055 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003056
Greg Clayton437a1352012-04-09 22:43:43 +00003057 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003058
Greg Clayton437a1352012-04-09 22:43:43 +00003059 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060 {
Greg Clayton7f995132011-10-04 22:41:51 +00003061 const uint32_t initial_types_size = types.GetSize();
Greg Claytond4a2b372011-09-12 23:21:58 +00003062 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003063 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003064 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003065 const DIERef& die_ref = die_offsets[i];
3066 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Claytond4a2b372011-09-12 23:21:58 +00003067
Greg Clayton95d87902011-11-11 03:16:25 +00003068 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003069 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003070 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00003071 continue; // The containing decl contexts don't match
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003072
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003073 Type *matching_type = ResolveType (die, true, true);
Greg Clayton95d87902011-11-11 03:16:25 +00003074 if (matching_type)
3075 {
3076 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003077 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003078 if (types.GetSize() >= max_matches)
3079 break;
3080 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003081 }
Greg Clayton95d87902011-11-11 03:16:25 +00003082 else
3083 {
3084 if (m_using_apple_tables)
3085 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003086 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 +00003087 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003088 }
3089 }
3090
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003091 }
Greg Clayton437a1352012-04-09 22:43:43 +00003092 const uint32_t num_matches = types.GetSize() - initial_types_size;
3093 if (log && num_matches)
3094 {
Greg Clayton99558cc42015-08-24 23:46:31 +00003095 if (parent_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00003096 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003097 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003098 "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 +00003099 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003100 static_cast<const void*>(parent_decl_ctx),
3101 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003102 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003103 num_matches);
3104 }
3105 else
3106 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003107 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003108 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00003109 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003110 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003111 num_matches);
3112 }
3113 }
3114 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003115 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00003116 else
3117 {
3118 UpdateExternalModuleListIfNeeded();
3119
3120 for (const auto &pair : m_external_type_modules)
3121 {
3122 ModuleSP external_module_sp = pair.second;
3123 if (external_module_sp)
3124 {
3125 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
3126 if (sym_vendor)
3127 {
3128 const uint32_t num_external_matches = sym_vendor->FindTypes (sc,
3129 name,
3130 parent_decl_ctx,
3131 append,
3132 max_matches,
3133 types);
3134 if (num_external_matches)
3135 return num_external_matches;
3136 }
3137 }
3138 }
3139 }
3140
3141 return 0;
3142}
3143
3144
3145size_t
3146SymbolFileDWARF::FindTypes (const std::vector<CompilerContext> &context,
3147 bool append,
3148 TypeMap& types)
3149{
3150 if (!append)
3151 types.Clear();
3152
3153 if (context.empty())
3154 return 0;
3155
3156 DIEArray die_offsets;
3157
3158 ConstString name = context.back().name;
3159
Sean Callanan9b0cfe22016-01-14 18:59:49 +00003160 if (!name)
3161 return 0;
3162
Greg Claytone6b36cd2015-12-08 01:02:08 +00003163 if (m_using_apple_tables)
3164 {
3165 if (m_apple_types_ap.get())
3166 {
3167 const char *name_cstr = name.GetCString();
3168 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3169 }
3170 }
3171 else
3172 {
3173 if (!m_indexed)
3174 Index ();
3175
3176 m_type_index.Find (name, die_offsets);
3177 }
3178
3179 const size_t num_die_matches = die_offsets.size();
3180
3181 if (num_die_matches)
3182 {
3183 size_t num_matches = 0;
3184 DWARFDebugInfo* debug_info = DebugInfo();
3185 for (size_t i=0; i<num_die_matches; ++i)
3186 {
3187 const DIERef& die_ref = die_offsets[i];
3188 DWARFDIE die = debug_info->GetDIE (die_ref);
3189
3190 if (die)
3191 {
3192 std::vector<CompilerContext> die_context;
3193 die.GetDWOContext(die_context);
3194 if (die_context != context)
3195 continue;
3196
3197 Type *matching_type = ResolveType (die, true, true);
3198 if (matching_type)
3199 {
3200 // We found a type pointer, now find the shared pointer form our type list
3201 types.InsertUnique (matching_type->shared_from_this());
3202 ++num_matches;
3203 }
3204 }
3205 else
3206 {
3207 if (m_using_apple_tables)
3208 {
3209 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3210 die_ref.die_offset, name.GetCString());
3211 }
3212 }
3213
3214 }
3215 return num_matches;
3216 }
Greg Clayton7f995132011-10-04 22:41:51 +00003217 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003218}
3219
3220
Greg Clayton99558cc42015-08-24 23:46:31 +00003221CompilerDeclContext
Greg Clayton96d7d742010-11-10 23:42:09 +00003222SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003223 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00003224 const CompilerDeclContext *parent_decl_ctx)
Greg Clayton96d7d742010-11-10 23:42:09 +00003225{
Greg Clayton5160ce52013-03-27 23:08:40 +00003226 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003227
3228 if (log)
3229 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003230 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003231 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3232 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003233 }
3234
Greg Clayton99558cc42015-08-24 23:46:31 +00003235 CompilerDeclContext namespace_decl_ctx;
3236
3237 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
3238 return namespace_decl_ctx;
3239
3240
Greg Clayton96d7d742010-11-10 23:42:09 +00003241 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003242 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003243 {
Greg Clayton7f995132011-10-04 22:41:51 +00003244 DIEArray die_offsets;
3245
Greg Clayton526e5af2010-11-13 03:52:47 +00003246 // Index if we already haven't to make sure the compile units
3247 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003248 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003249 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003250 if (m_apple_namespaces_ap.get())
3251 {
3252 const char *name_cstr = name.GetCString();
3253 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3254 }
Greg Clayton7f995132011-10-04 22:41:51 +00003255 }
3256 else
3257 {
3258 if (!m_indexed)
3259 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003260
Greg Clayton7f995132011-10-04 22:41:51 +00003261 m_namespace_index.Find (name, die_offsets);
3262 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003263
Greg Clayton7f995132011-10-04 22:41:51 +00003264 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003265 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003266 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003267 DWARFDebugInfo* debug_info = DebugInfo();
3268 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003269 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003270 const DIERef& die_ref = die_offsets[i];
3271 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003272
Greg Clayton95d87902011-11-11 03:16:25 +00003273 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003274 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003275 if (!DIEInDeclContext (parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00003276 continue; // The containing decl contexts don't match
Greg Clayton95d87902011-11-11 03:16:25 +00003277
Greg Clayton261ac3f2015-08-28 01:01:03 +00003278 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3279 if (dwarf_ast)
Greg Clayton8b4edba2015-08-14 20:02:05 +00003280 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003281 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00003282 if (namespace_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00003283 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003284 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003285 }
Greg Clayton95d87902011-11-11 03:16:25 +00003286 else
3287 {
3288 if (m_using_apple_tables)
3289 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003290 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 +00003291 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003292 }
3293 }
3294
Greg Clayton526e5af2010-11-13 03:52:47 +00003295 }
3296 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003297 }
Greg Clayton99558cc42015-08-24 23:46:31 +00003298 if (log && namespace_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00003299 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003300 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003301 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => CompilerDeclContext(%p/%p) \"%s\"",
Greg Clayton437a1352012-04-09 22:43:43 +00003302 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003303 static_cast<const void*>(namespace_decl_ctx.GetTypeSystem()),
3304 static_cast<const void*>(namespace_decl_ctx.GetOpaqueDeclContext()),
3305 namespace_decl_ctx.GetName().AsCString("<NULL>"));
Greg Clayton437a1352012-04-09 22:43:43 +00003306 }
3307
Greg Clayton99558cc42015-08-24 23:46:31 +00003308 return namespace_decl_ctx;
Greg Clayton96d7d742010-11-10 23:42:09 +00003309}
3310
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003311TypeSP
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003312SymbolFileDWARF::GetTypeForDIE (const DWARFDIE &die, bool resolve_function_context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003313{
3314 TypeSP type_sp;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003315 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003316 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003317 Type *type_ptr = GetDIEToType().lookup (die.GetDIE());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003318 if (type_ptr == NULL)
3319 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003320 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
Greg Claytonca512b32011-01-14 04:54:56 +00003321 assert (lldb_cu);
3322 SymbolContext sc(lldb_cu);
Ravitheja Addepally40697302015-10-08 09:45:41 +00003323 const DWARFDebugInfoEntry* parent_die = die.GetParent().GetDIE();
3324 while (parent_die != nullptr)
3325 {
3326 if (parent_die->Tag() == DW_TAG_subprogram)
3327 break;
3328 parent_die = parent_die->GetParent();
3329 }
3330 SymbolContext sc_backup = sc;
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003331 if (resolve_function_context && parent_die != nullptr && !GetFunction(DWARFDIE(die.GetCU(),parent_die), sc))
Ravitheja Addepally40697302015-10-08 09:45:41 +00003332 sc = sc_backup;
3333
Greg Clayton6071e6f2015-08-26 22:57:51 +00003334 type_sp = ParseType(sc, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003335 }
3336 else if (type_ptr != DIE_IS_BEING_PARSED)
3337 {
3338 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003339 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003340 }
3341
3342 }
3343 return type_sp;
3344}
3345
Greg Clayton2bc22f82011-09-30 03:20:47 +00003346
Greg Clayton6071e6f2015-08-26 22:57:51 +00003347DWARFDIE
3348SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFDIE &orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003349{
Greg Clayton6071e6f2015-08-26 22:57:51 +00003350 if (orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003351 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003352 DWARFDIE die = orig_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003353
Greg Clayton6071e6f2015-08-26 22:57:51 +00003354 while (die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003355 {
3356 // If this is the original DIE that we are searching for a declaration
3357 // for, then don't look in the cache as we don't want our own decl
3358 // context to be our decl context...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003359 if (orig_die != die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003360 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003361 switch (die.Tag())
Greg Clayton2bc22f82011-09-30 03:20:47 +00003362 {
3363 case DW_TAG_compile_unit:
3364 case DW_TAG_namespace:
3365 case DW_TAG_structure_type:
3366 case DW_TAG_union_type:
3367 case DW_TAG_class_type:
Paul Hermand628cbb2015-09-15 23:44:17 +00003368 case DW_TAG_lexical_block:
3369 case DW_TAG_subprogram:
Greg Clayton2bc22f82011-09-30 03:20:47 +00003370 return die;
3371
3372 default:
3373 break;
3374 }
3375 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003376
3377 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3378 if (spec_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003379 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003380 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3381 if (decl_ctx_die)
3382 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003383 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003384
3385 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3386 if (abs_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003387 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003388 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3389 if (decl_ctx_die)
3390 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003391 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003392
3393 die = die.GetParent();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003394 }
3395 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003396 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003397}
3398
3399
Greg Clayton901c5ca2011-12-03 04:40:03 +00003400Symbol *
3401SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3402{
3403 Symbol *objc_class_symbol = NULL;
3404 if (m_obj_file)
3405 {
Greg Clayton3046e662013-07-10 01:23:25 +00003406 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003407 if (symtab)
3408 {
3409 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3410 eSymbolTypeObjCClass,
3411 Symtab::eDebugNo,
3412 Symtab::eVisibilityAny);
3413 }
3414 }
3415 return objc_class_symbol;
3416}
3417
Greg Claytonc7f03b62012-01-12 04:33:28 +00003418// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3419// then we can end up looking through all class types for a complete type and never find
3420// the full definition. We need to know if this attribute is supported, so we determine
3421// this here and cache th result. We also need to worry about the debug map DWARF file
3422// if we are doing darwin DWARF in .o file debugging.
3423bool
3424SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3425{
3426 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3427 {
3428 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3429 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3430 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3431 else
3432 {
3433 DWARFDebugInfo* debug_info = DebugInfo();
3434 const uint32_t num_compile_units = GetNumCompileUnits();
3435 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3436 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003437 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3438 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003439 {
3440 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3441 break;
3442 }
3443 }
3444 }
Greg Clayton1f746072012-08-29 21:13:06 +00003445 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003446 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3447 }
3448 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3449}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003450
3451// This function can be used when a DIE is found that is a forward declaration
3452// DIE and we want to try and find a type that has the complete definition.
3453TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003454SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
Greg Claytonc7f03b62012-01-12 04:33:28 +00003455 const ConstString &type_name,
3456 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003457{
3458
3459 TypeSP type_sp;
3460
Greg Claytonc7f03b62012-01-12 04:33:28 +00003461 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003462 return type_sp;
3463
3464 DIEArray die_offsets;
3465
3466 if (m_using_apple_tables)
3467 {
3468 if (m_apple_types_ap.get())
3469 {
3470 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003471 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003472 }
3473 }
3474 else
3475 {
3476 if (!m_indexed)
3477 Index ();
3478
3479 m_type_index.Find (type_name, die_offsets);
3480 }
3481
Greg Clayton901c5ca2011-12-03 04:40:03 +00003482 const size_t num_matches = die_offsets.size();
3483
Greg Clayton901c5ca2011-12-03 04:40:03 +00003484 if (num_matches)
3485 {
3486 DWARFDebugInfo* debug_info = DebugInfo();
3487 for (size_t i=0; i<num_matches; ++i)
3488 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003489 const DIERef& die_ref = die_offsets[i];
3490 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003491
3492 if (type_die)
3493 {
3494 bool try_resolving_type = false;
3495
3496 // Don't try and resolve the DIE we are looking for with the DIE itself!
3497 if (type_die != die)
3498 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003499 switch (type_die.Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003500 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003501 case DW_TAG_class_type:
3502 case DW_TAG_structure_type:
3503 try_resolving_type = true;
3504 break;
3505 default:
3506 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003507 }
3508 }
3509
3510 if (try_resolving_type)
3511 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003512 if (must_be_implementation && type_die.Supports_DW_AT_APPLE_objc_complete_type())
3513 try_resolving_type = type_die.GetAttributeValueAsUnsigned (DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003514
3515 if (try_resolving_type)
3516 {
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003517 Type *resolved_type = ResolveType (type_die, false, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003518 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3519 {
Ed Mastea0191d12013-10-17 20:42:56 +00003520 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 +00003521 die.GetID(),
Jim Ingham4af59612014-12-19 19:20:44 +00003522 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003523 type_die.GetID(),
3524 type_cu->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003525
Greg Claytonc7f03b62012-01-12 04:33:28 +00003526 if (die)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003527 GetDIEToType()[die.GetDIE()] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00003528 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003529 break;
3530 }
3531 }
3532 }
3533 }
3534 else
3535 {
3536 if (m_using_apple_tables)
3537 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003538 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 +00003539 die_ref.die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003540 }
3541 }
3542
3543 }
3544 }
3545 return type_sp;
3546}
3547
Greg Claytona8022fa2012-04-24 21:22:41 +00003548
Greg Clayton80c26302012-02-05 06:12:47 +00003549//----------------------------------------------------------------------
3550// This function helps to ensure that the declaration contexts match for
3551// two different DIEs. Often times debug information will refer to a
3552// forward declaration of a type (the equivalent of "struct my_struct;".
3553// There will often be a declaration of that type elsewhere that has the
3554// full definition. When we go looking for the full type "my_struct", we
3555// will find one or more matches in the accelerator tables and we will
3556// then need to make sure the type was in the same declaration context
3557// as the original DIE. This function can efficiently compare two DIEs
3558// and will return true when the declaration context matches, and false
3559// when they don't.
3560//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00003561bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00003562SymbolFileDWARF::DIEDeclContextsMatch (const DWARFDIE &die1,
3563 const DWARFDIE &die2)
Greg Clayton890ff562012-02-02 05:48:16 +00003564{
Greg Claytona8022fa2012-04-24 21:22:41 +00003565 if (die1 == die2)
3566 return true;
3567
Greg Clayton890ff562012-02-02 05:48:16 +00003568 DWARFDIECollection decl_ctx_1;
3569 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00003570 //The declaration DIE stack is a stack of the declaration context
3571 // DIEs all the way back to the compile unit. If a type "T" is
3572 // declared inside a class "B", and class "B" is declared inside
3573 // a class "A" and class "A" is in a namespace "lldb", and the
3574 // namespace is in a compile unit, there will be a stack of DIEs:
3575 //
3576 // [0] DW_TAG_class_type for "B"
3577 // [1] DW_TAG_class_type for "A"
3578 // [2] DW_TAG_namespace for "lldb"
3579 // [3] DW_TAG_compile_unit for the source file.
3580 //
3581 // We grab both contexts and make sure that everything matches
3582 // all the way back to the compiler unit.
3583
3584 // First lets grab the decl contexts for both DIEs
Greg Clayton6071e6f2015-08-26 22:57:51 +00003585 die1.GetDeclContextDIEs (decl_ctx_1);
3586 die2.GetDeclContextDIEs (decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00003587 // Make sure the context arrays have the same size, otherwise
3588 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00003589 const size_t count1 = decl_ctx_1.Size();
3590 const size_t count2 = decl_ctx_2.Size();
3591 if (count1 != count2)
3592 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00003593
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003594 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00003595 // compile unit. If they don't, then we are done.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003596 DWARFDIE decl_ctx_die1;
3597 DWARFDIE decl_ctx_die2;
Greg Clayton890ff562012-02-02 05:48:16 +00003598 size_t i;
3599 for (i=0; i<count1; i++)
3600 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003601 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3602 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3603 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
Greg Clayton890ff562012-02-02 05:48:16 +00003604 return false;
3605 }
Greg Clayton890ff562012-02-02 05:48:16 +00003606#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003607
3608 // Make sure the top item in the decl context die array is always
3609 // DW_TAG_compile_unit. If it isn't then something went wrong in
Greg Clayton5ce1a842015-08-27 18:09:44 +00003610 // the DWARFDIE::GetDeclContextDIEs() function...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003611 assert (decl_ctx_1.GetDIEAtIndex (count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003612
Greg Clayton890ff562012-02-02 05:48:16 +00003613#endif
3614 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00003615 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00003616 for (i=0; i<count1 - 1; i++)
3617 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003618 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3619 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3620 const char *name1 = decl_ctx_die1.GetName();
3621 const char *name2 = decl_ctx_die2.GetName();
Greg Clayton890ff562012-02-02 05:48:16 +00003622 // If the string was from a DW_FORM_strp, then the pointer will often
3623 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00003624 if (name1 == name2)
3625 continue;
3626
3627 // Name pointers are not equal, so only compare the strings
3628 // if both are not NULL.
3629 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00003630 {
Greg Clayton5569e642012-02-06 01:44:54 +00003631 // If the strings don't compare, we are done...
3632 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00003633 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00003634 }
3635 else
3636 {
3637 // One name was NULL while the other wasn't
3638 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003639 }
3640 }
Greg Clayton80c26302012-02-05 06:12:47 +00003641 // We made it through all of the checks and the declaration contexts
3642 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00003643 return true;
3644}
Greg Clayton220a0072011-12-09 08:48:30 +00003645
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003646
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003647TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00003648SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
3649{
3650 TypeSP type_sp;
3651
3652 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3653 if (dwarf_decl_ctx_count > 0)
3654 {
3655 const ConstString type_name(dwarf_decl_ctx[0].name);
3656 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
3657
3658 if (type_name)
3659 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003660 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00003661 if (log)
3662 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003663 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003664 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
3665 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3666 dwarf_decl_ctx.GetQualifiedName());
3667 }
3668
3669 DIEArray die_offsets;
3670
3671 if (m_using_apple_tables)
3672 {
3673 if (m_apple_types_ap.get())
3674 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003675 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
3676 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
3677 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00003678 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003679 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3680 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
3681 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003682 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003683 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3684 }
3685 else if (has_tag)
3686 {
3687 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003688 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00003689 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
3690 }
3691 else
3692 {
3693 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3694 }
3695 }
3696 }
3697 else
3698 {
3699 if (!m_indexed)
3700 Index ();
3701
3702 m_type_index.Find (type_name, die_offsets);
3703 }
3704
3705 const size_t num_matches = die_offsets.size();
3706
3707
Greg Claytona8022fa2012-04-24 21:22:41 +00003708 if (num_matches)
3709 {
3710 DWARFDebugInfo* debug_info = DebugInfo();
3711 for (size_t i=0; i<num_matches; ++i)
3712 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003713 const DIERef& die_ref = die_offsets[i];
3714 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Claytona8022fa2012-04-24 21:22:41 +00003715
3716 if (type_die)
3717 {
3718 bool try_resolving_type = false;
3719
3720 // Don't try and resolve the DIE we are looking for with the DIE itself!
Greg Clayton6071e6f2015-08-26 22:57:51 +00003721 const dw_tag_t type_tag = type_die.Tag();
Greg Claytona8022fa2012-04-24 21:22:41 +00003722 // Make sure the tags match
3723 if (type_tag == tag)
3724 {
3725 // The tags match, lets try resolving this type
3726 try_resolving_type = true;
3727 }
3728 else
3729 {
3730 // The tags don't match, but we need to watch our for a
3731 // forward declaration for a struct and ("struct foo")
3732 // ends up being a class ("class foo { ... };") or
3733 // vice versa.
3734 switch (type_tag)
3735 {
3736 case DW_TAG_class_type:
3737 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3738 try_resolving_type = (tag == DW_TAG_structure_type);
3739 break;
3740 case DW_TAG_structure_type:
3741 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3742 try_resolving_type = (tag == DW_TAG_class_type);
3743 break;
3744 default:
3745 // Tags don't match, don't event try to resolve
3746 // using this type whose name matches....
3747 break;
3748 }
3749 }
3750
3751 if (try_resolving_type)
3752 {
3753 DWARFDeclContext type_dwarf_decl_ctx;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003754 type_die.GetDWARFDeclContext (type_dwarf_decl_ctx);
Greg Claytona8022fa2012-04-24 21:22:41 +00003755
3756 if (log)
3757 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003758 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003759 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
3760 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3761 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003762 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003763 type_dwarf_decl_ctx.GetQualifiedName());
3764 }
3765
3766 // Make sure the decl contexts match all the way up
3767 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
3768 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003769 Type *resolved_type = ResolveType (type_die, false);
Greg Claytona8022fa2012-04-24 21:22:41 +00003770 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3771 {
3772 type_sp = resolved_type->shared_from_this();
3773 break;
3774 }
3775 }
3776 }
3777 else
3778 {
3779 if (log)
3780 {
3781 std::string qualified_name;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003782 type_die.GetQualifiedName(qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00003783 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003784 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
3785 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3786 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003787 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003788 qualified_name.c_str());
3789 }
3790 }
3791 }
3792 else
3793 {
3794 if (m_using_apple_tables)
3795 {
3796 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 +00003797 die_ref.die_offset, type_name.GetCString());
Greg Claytona8022fa2012-04-24 21:22:41 +00003798 }
3799 }
3800
3801 }
3802 }
3803 }
3804 }
3805 return type_sp;
3806}
3807
Greg Claytona8022fa2012-04-24 21:22:41 +00003808TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003809SymbolFileDWARF::ParseType (const SymbolContext& sc, const DWARFDIE &die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003810{
Greg Clayton196e8cd2015-08-17 20:31:46 +00003811 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003812
Greg Clayton6071e6f2015-08-26 22:57:51 +00003813 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003814 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003815 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
3816
3817 if (type_system)
Greg Clayton196e8cd2015-08-17 20:31:46 +00003818 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003819 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3820 if (dwarf_ast)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003821 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003822 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3823 type_sp = dwarf_ast->ParseTypeFromDWARF (sc, die, log, type_is_new_ptr);
3824 if (type_sp)
3825 {
3826 TypeList* type_list = GetTypeList();
3827 if (type_list)
3828 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003829
3830 if (die.Tag() == DW_TAG_subprogram)
3831 {
3832 DIERef die_ref = die.GetDIERef();
3833 std::string scope_qualified_name(GetDeclContextForUID(die.GetID()).GetScopeQualifiedName().AsCString(""));
3834 if (scope_qualified_name.size())
3835 {
3836 NameToOffsetMap::iterator iter = m_function_scope_qualified_name_map.find(scope_qualified_name);
3837 if (iter != m_function_scope_qualified_name_map.end())
3838 (*iter).second->insert(die_ref);
3839 else
3840 {
3841 DIERefSetSP new_set(new std::set<DIERef>);
3842 new_set->insert(die_ref);
3843 m_function_scope_qualified_name_map.emplace(std::make_pair(scope_qualified_name, new_set));
3844 }
3845 }
3846 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00003847 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003848 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003849 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003851
3852 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003853}
3854
3855size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003856SymbolFileDWARF::ParseTypes
3857(
3858 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003859 const DWARFDIE &orig_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003860 bool parse_siblings,
3861 bool parse_children
3862)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003863{
3864 size_t types_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003865 DWARFDIE die = orig_die;
3866 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 {
3868 bool type_is_new = false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003869 if (ParseType(sc, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003870 {
3871 if (type_is_new)
3872 ++types_added;
3873 }
3874
Greg Clayton6071e6f2015-08-26 22:57:51 +00003875 if (parse_children && die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003876 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003877 if (die.Tag() == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878 {
3879 SymbolContext child_sc(sc);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003880 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3881 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003882 }
3883 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003884 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003885 }
3886
3887 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003888 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003889 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003890 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003891 }
3892 return types_added;
3893}
3894
3895
3896size_t
3897SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3898{
3899 assert(sc.comp_unit && sc.function);
3900 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003901 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003902 if (dwarf_cu)
3903 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003904 const dw_offset_t function_die_offset = sc.function->GetID();
3905 DWARFDIE function_die = dwarf_cu->GetDIE (function_die_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003906 if (function_die)
3907 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003908 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003909 }
3910 }
3911
3912 return functions_added;
3913}
3914
3915
3916size_t
3917SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3918{
3919 // At least a compile unit must be valid
3920 assert(sc.comp_unit);
3921 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003922 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003923 if (dwarf_cu)
3924 {
3925 if (sc.function)
3926 {
3927 dw_offset_t function_die_offset = sc.function->GetID();
Greg Clayton6071e6f2015-08-26 22:57:51 +00003928 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3929 if (func_die && func_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003930 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003931 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003932 }
3933 }
3934 else
3935 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003936 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3937 if (dwarf_cu_die && dwarf_cu_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003938 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003939 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003940 }
3941 }
3942 }
3943
3944 return types_added;
3945}
3946
3947size_t
3948SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3949{
3950 if (sc.comp_unit != NULL)
3951 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003952 DWARFDebugInfo* info = DebugInfo();
3953 if (info == NULL)
3954 return 0;
3955
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003956 if (sc.function)
3957 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003958 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID()));
Greg Clayton9422dd62013-03-04 21:46:16 +00003959
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003960 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress (DW_AT_low_pc, LLDB_INVALID_ADDRESS);
Greg Claytonc7bece562013-01-25 18:06:21 +00003961 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003962 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003963 const size_t num_variables = ParseVariables(sc, function_die.GetFirstChild(), func_lo_pc, true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00003964
Greg Claytone38a5ed2012-01-05 03:57:59 +00003965 // Let all blocks know they have parse all their variables
3966 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3967 return num_variables;
3968 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003969 }
3970 else if (sc.comp_unit)
3971 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003972 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Greg Clayton9422dd62013-03-04 21:46:16 +00003973
3974 if (dwarf_cu == NULL)
3975 return 0;
3976
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003977 uint32_t vars_added = 0;
3978 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3979
3980 if (variables.get() == NULL)
3981 {
3982 variables.reset(new VariableList());
3983 sc.comp_unit->SetVariableList(variables);
3984
Greg Claytond4a2b372011-09-12 23:21:58 +00003985 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00003986 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003987 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003988 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003989 {
3990 DWARFMappedHash::DIEInfoArray hash_data_array;
3991 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
3992 dwarf_cu->GetNextCompileUnitOffset(),
3993 hash_data_array))
3994 {
3995 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3996 }
3997 }
Greg Clayton7f995132011-10-04 22:41:51 +00003998 }
3999 else
4000 {
4001 // Index if we already haven't to make sure the compile units
4002 // get indexed and make their global DIE index list
4003 if (!m_indexed)
4004 Index ();
4005
4006 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
Greg Clayton7f995132011-10-04 22:41:51 +00004007 die_offsets);
4008 }
4009
4010 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004011 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004012 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004013 DWARFDebugInfo* debug_info = DebugInfo();
4014 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004015 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004016 const DIERef& die_ref = die_offsets[i];
4017 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00004018 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00004019 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004020 VariableSP var_sp (ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
Greg Clayton95d87902011-11-11 03:16:25 +00004021 if (var_sp)
4022 {
4023 variables->AddVariableIfUnique (var_sp);
4024 ++vars_added;
4025 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004026 }
Greg Clayton95d87902011-11-11 03:16:25 +00004027 else
4028 {
4029 if (m_using_apple_tables)
4030 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004031 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 +00004032 }
4033 }
4034
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004035 }
4036 }
4037 }
4038 return vars_added;
4039 }
4040 }
4041 return 0;
4042}
4043
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004044VariableSP
4045SymbolFileDWARF::ParseVariableDIE
4046(
4047 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004048 const DWARFDIE &die,
Greg Clayton016a95e2010-09-14 02:20:48 +00004049 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050)
4051{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004052 if (die.GetDWARF() != this)
4053 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
4054
Greg Clayton6071e6f2015-08-26 22:57:51 +00004055 VariableSP var_sp;
4056 if (!die)
4057 return var_sp;
4058
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004059 var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton83c5cd92010-11-14 22:13:40 +00004060 if (var_sp)
4061 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062
Greg Clayton6071e6f2015-08-26 22:57:51 +00004063 const dw_tag_t tag = die.Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00004064 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00004065
4066 if ((tag == DW_TAG_variable) ||
4067 (tag == DW_TAG_constant) ||
4068 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004069 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004070 DWARFAttributes attributes;
4071 const size_t num_attributes = die.GetAttributes(attributes);
Paul Hermand628cbb2015-09-15 23:44:17 +00004072 DWARFDIE spec_die;
Greg Clayton7f995132011-10-04 22:41:51 +00004073 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004074 {
Greg Clayton7f995132011-10-04 22:41:51 +00004075 const char *name = NULL;
4076 const char *mangled = NULL;
4077 Declaration decl;
4078 uint32_t i;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004079 DWARFFormValue type_die_form;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004080 DWARFExpression location(die.GetCU());
Greg Clayton7f995132011-10-04 22:41:51 +00004081 bool is_external = false;
4082 bool is_artificial = false;
4083 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004084 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00004085 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00004086 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00004087
4088 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004089 {
Greg Clayton7f995132011-10-04 22:41:51 +00004090 dw_attr_t attr = attributes.AttributeAtIndex(i);
4091 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00004092
Greg Clayton6071e6f2015-08-26 22:57:51 +00004093 if (attributes.ExtractFormValueAtIndex(i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004094 {
Greg Clayton7f995132011-10-04 22:41:51 +00004095 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096 {
Greg Clayton7f995132011-10-04 22:41:51 +00004097 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4098 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4099 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004100 case DW_AT_name: name = form_value.AsCString(); break;
Greg Clayton71415542012-12-08 00:24:40 +00004101 case DW_AT_linkage_name:
Greg Clayton6071e6f2015-08-26 22:57:51 +00004102 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004103 case DW_AT_type: type_die_form = form_value; break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004104 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004105 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004106 // If we have already found a DW_AT_location attribute, ignore this attribute.
4107 if (!has_explicit_location)
4108 {
4109 location_is_const_value_data = true;
4110 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00004111 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004112 if (DWARFFormValue::IsBlockForm(form_value.Form()))
4113 {
4114 // Retrieve the value as a block expression.
4115 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4116 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004117 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004118 }
4119 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4120 {
4121 // Retrieve the value as a data expression.
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004122 DWARFFormValue::FixedFormSizes fixed_form_sizes =
4123 DWARFFormValue::GetFixedFormSizesForAddressSize (
4124 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
4125 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004126 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004127 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Enrico Granata4ec130d2014-08-11 19:16:35 +00004128 if (data_length == 0)
4129 {
4130 const uint8_t *data_pointer = form_value.BlockData();
4131 if (data_pointer)
4132 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00004133 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00004134 }
4135 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4136 {
4137 // we need to get the byte size of the type later after we create the variable
4138 const_value = form_value;
4139 }
4140 }
4141 else
4142 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004143 }
4144 else
4145 {
4146 // Retrieve the value as a string expression.
4147 if (form_value.Form() == DW_FORM_strp)
4148 {
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004149 DWARFFormValue::FixedFormSizes fixed_form_sizes =
4150 DWARFFormValue::GetFixedFormSizesForAddressSize (
4151 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
4152 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004153 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004154 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Richard Mitton0a558352013-10-17 21:14:00 +00004155 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004156 }
4157 else
4158 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004159 const char *str = form_value.AsCString();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004160 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
4161 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00004162 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004163 }
4164 }
4165 }
4166 break;
Greg Clayton7f995132011-10-04 22:41:51 +00004167 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004168 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004169 location_is_const_value_data = false;
4170 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00004171 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004172 {
Ed Masteeeae7212013-10-24 20:43:47 +00004173 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004174
4175 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4176 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004177 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004178 }
4179 else
4180 {
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004181 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004182 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4183
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004184 size_t loc_list_length = DWARFExpression::LocationListSize(die.GetCU(), debug_loc_data, debug_loc_offset);
Greg Clayton7f995132011-10-04 22:41:51 +00004185 if (loc_list_length > 0)
4186 {
Richard Mitton0a558352013-10-17 21:14:00 +00004187 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004188 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00004189 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00004190 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004191 }
4192 }
Greg Clayton7f995132011-10-04 22:41:51 +00004193 break;
Paul Hermand628cbb2015-09-15 23:44:17 +00004194 case DW_AT_specification:
4195 {
4196 DWARFDebugInfo* debug_info = DebugInfo();
4197 if (debug_info)
4198 spec_die = debug_info->GetDIE(DIERef(form_value));
4199 break;
4200 }
Greg Clayton1c8ef472013-04-05 23:27:21 +00004201 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00004202 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004203 case DW_AT_declaration:
4204 case DW_AT_description:
4205 case DW_AT_endianity:
4206 case DW_AT_segment:
4207 case DW_AT_start_scope:
4208 case DW_AT_visibility:
4209 default:
4210 case DW_AT_abstract_origin:
4211 case DW_AT_sibling:
Greg Clayton7f995132011-10-04 22:41:51 +00004212 break;
4213 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004214 }
4215 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004216
Greg Clayton6071e6f2015-08-26 22:57:51 +00004217 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
4218 const dw_tag_t parent_tag = die.GetParent().Tag();
4219 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 +00004220
Greg Clayton9e9f2192013-05-17 00:55:28 +00004221 ValueType scope = eValueTypeInvalid;
4222
Greg Clayton6071e6f2015-08-26 22:57:51 +00004223 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Greg Clayton9e9f2192013-05-17 00:55:28 +00004224 SymbolContextScope * symbol_context_scope = NULL;
4225
Siva Chandra0783ab92015-03-24 18:32:27 +00004226 if (!mangled)
4227 {
4228 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
4229 // generate fully qualified names of global variables with commands like "frame var j".
4230 // For example, if j were an int variable holding a value 4 and declared in a namespace
4231 // B which in turn is contained in a namespace A, the command "frame var j" returns
4232 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
4233 // to generate a fully qualified name from the declaration context.
Greg Clayton6071e6f2015-08-26 22:57:51 +00004234 if (parent_tag == DW_TAG_compile_unit &&
Jim Ingham0e0984e2015-09-02 01:06:46 +00004235 Language::LanguageIsCPlusPlus(die.GetLanguage()))
Siva Chandra0783ab92015-03-24 18:32:27 +00004236 {
4237 DWARFDeclContext decl_ctx;
4238
Greg Clayton6071e6f2015-08-26 22:57:51 +00004239 die.GetDWARFDeclContext(decl_ctx);
Siva Chandra0783ab92015-03-24 18:32:27 +00004240 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
4241 }
4242 }
4243
Greg Clayton9e9f2192013-05-17 00:55:28 +00004244 // DWARF doesn't specify if a DW_TAG_variable is a local, global
4245 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004246 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00004247 // a DW_OP_addr opcode _somewhere_ in the definition. I say
4248 // somewhere because clang likes to combine small global variables
4249 // into the same symbol and have locations like:
4250 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
4251 // So if we don't have a DW_TAG_formal_parameter, we can look at
4252 // the location to see if it contains a DW_OP_addr opcode, and
4253 // then we can correctly classify our variables.
4254 if (tag == DW_TAG_formal_parameter)
4255 scope = eValueTypeVariableArgument;
4256 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004257 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004258 bool op_error = false;
4259 // Check if the location has a DW_OP_addr with any address value...
4260 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
4261 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004262 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004263 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
4264 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00004265 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004266 StreamString strm;
4267 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Clayton6071e6f2015-08-26 22:57:51 +00004268 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 +00004269 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004270 }
Greg Claytond1767f02011-12-08 02:13:16 +00004271
Greg Clayton9e9f2192013-05-17 00:55:28 +00004272 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
4273 {
4274 if (is_external)
4275 scope = eValueTypeVariableGlobal;
4276 else
4277 scope = eValueTypeVariableStatic;
4278
4279
4280 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
4281
4282 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004283 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004284 // When leaving the DWARF in the .o files on darwin,
4285 // when we have a global variable that wasn't initialized,
4286 // the .o file might not have allocated a virtual
4287 // address for the global variable. In this case it will
4288 // have created a symbol for the global variable
4289 // that is undefined/data and external and the value will
4290 // be the byte size of the variable. When we do the
4291 // address map in SymbolFileDWARFDebugMap we rely on
4292 // having an address, we need to do some magic here
4293 // so we can get the correct address for our global
4294 // variable. The address for all of these entries
4295 // will be zero, and there will be an undefined symbol
4296 // in this object file, and the executable will have
4297 // a matching symbol with a good address. So here we
4298 // dig up the correct address and replace it in the
4299 // location for the variable, and set the variable's
4300 // symbol context scope to be that of the main executable
4301 // so the file address will resolve correctly.
4302 bool linked_oso_file_addr = false;
4303 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00004304 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004305 // we have a possible uninitialized extern global
4306 ConstString const_name(mangled ? mangled : name);
4307 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
4308 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004309 {
Greg Clayton3046e662013-07-10 01:23:25 +00004310 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004311 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004312 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004313 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
4314 eSymbolTypeData,
4315 Symtab::eDebugYes,
4316 Symtab::eVisibilityExtern);
4317 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004318 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004319 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004320 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004321 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004322 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004323 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004324 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004325 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004326 linked_oso_file_addr = true;
4327 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004328 }
4329 }
4330 }
4331 }
4332 }
4333 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004334 }
Greg Clayton9422dd62013-03-04 21:46:16 +00004335
Greg Clayton9e9f2192013-05-17 00:55:28 +00004336 if (!linked_oso_file_addr)
4337 {
4338 // The DW_OP_addr is not zero, but it contains a .o file address which
4339 // needs to be linked up correctly.
4340 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
4341 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00004342 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004343 // Update the file address for this variable
4344 location.Update_DW_OP_addr (exe_file_addr);
4345 }
4346 else
4347 {
4348 // Variable didn't make it into the final executable
4349 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00004350 }
Greg Claytond1767f02011-12-08 02:13:16 +00004351 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004352 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00004353 }
4354 else
4355 {
Ewan Crawford37395ad2015-12-17 11:59:47 +00004356 if (location_is_const_value_data)
4357 scope = eValueTypeVariableStatic;
4358 else
4359 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004360 }
Greg Clayton7f995132011-10-04 22:41:51 +00004361 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004362
4363 if (symbol_context_scope == NULL)
4364 {
4365 switch (parent_tag)
4366 {
4367 case DW_TAG_subprogram:
4368 case DW_TAG_inlined_subroutine:
4369 case DW_TAG_lexical_block:
4370 if (sc.function)
4371 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004372 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Clayton9e9f2192013-05-17 00:55:28 +00004373 if (symbol_context_scope == NULL)
4374 symbol_context_scope = sc.function;
4375 }
4376 break;
4377
4378 default:
4379 symbol_context_scope = sc.comp_unit;
4380 break;
4381 }
4382 }
4383
4384 if (symbol_context_scope)
4385 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004386 SymbolFileTypeSP type_sp(new SymbolFileType(*this, DIERef(type_die_form).GetUID()));
Enrico Granata4ec130d2014-08-11 19:16:35 +00004387
4388 if (const_value.Form() && type_sp && type_sp->GetType())
Greg Clayton6071e6f2015-08-26 22:57:51 +00004389 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), die.GetCU()->GetAddressByteSize());
Enrico Granata4ec130d2014-08-11 19:16:35 +00004390
Greg Clayton6071e6f2015-08-26 22:57:51 +00004391 var_sp.reset (new Variable (die.GetID(),
Greg Clayton9e9f2192013-05-17 00:55:28 +00004392 name,
4393 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00004394 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00004395 scope,
4396 symbol_context_scope,
4397 &decl,
4398 location,
4399 is_external,
Paul Herman10bc1a42015-08-18 22:46:57 +00004400 is_artificial,
4401 is_static_member));
Greg Clayton9e9f2192013-05-17 00:55:28 +00004402
4403 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4404 }
4405 else
4406 {
4407 // Not ready to parse this variable yet. It might be a global
4408 // or static variable that is in a function scope and the function
4409 // in the symbol context wasn't filled in yet
4410 return var_sp;
4411 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004412 }
Greg Clayton7f995132011-10-04 22:41:51 +00004413 // Cache var_sp even if NULL (the variable was just a specification or
4414 // was missing vital information to be able to be displayed in the debugger
4415 // (missing location due to optimization, etc)) so we don't re-parse
4416 // this DIE over and over later...
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004417 GetDIEToVariable()[die.GetDIE()] = var_sp;
Paul Hermand628cbb2015-09-15 23:44:17 +00004418 if (spec_die)
4419 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004420 }
4421 return var_sp;
4422}
4423
Greg Claytonc662ec82011-06-17 22:10:16 +00004424
Greg Clayton6071e6f2015-08-26 22:57:51 +00004425DWARFDIE
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004426SymbolFileDWARF::FindBlockContainingSpecification (const DIERef& func_die_ref,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004427 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004428{
4429 // Give the concrete function die specified by "func_die_offset", find the
4430 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4431 // to "spec_block_die_offset"
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004432 return FindBlockContainingSpecification (DebugInfo()->GetDIE (func_die_ref), spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004433}
4434
4435
Greg Clayton6071e6f2015-08-26 22:57:51 +00004436DWARFDIE
4437SymbolFileDWARF::FindBlockContainingSpecification(const DWARFDIE &die,
4438 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004439{
4440 if (die)
4441 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004442 switch (die.Tag())
Greg Claytonc662ec82011-06-17 22:10:16 +00004443 {
4444 case DW_TAG_subprogram:
4445 case DW_TAG_inlined_subroutine:
4446 case DW_TAG_lexical_block:
4447 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004448 if (die.GetAttributeValueAsReference (DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004449 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004450
Greg Clayton6071e6f2015-08-26 22:57:51 +00004451 if (die.GetAttributeValueAsReference (DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004452 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004453 }
4454 break;
4455 }
4456
4457 // Give the concrete function die specified by "func_die_offset", find the
4458 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4459 // to "spec_block_die_offset"
Greg Clayton6071e6f2015-08-26 22:57:51 +00004460 for (DWARFDIE child_die = die.GetFirstChild(); child_die; child_die = child_die.GetSibling())
Greg Claytonc662ec82011-06-17 22:10:16 +00004461 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004462 DWARFDIE result_die = FindBlockContainingSpecification (child_die, spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004463 if (result_die)
4464 return result_die;
4465 }
4466 }
4467
Greg Clayton6071e6f2015-08-26 22:57:51 +00004468 return DWARFDIE();
Greg Claytonc662ec82011-06-17 22:10:16 +00004469}
4470
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004471size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00004472SymbolFileDWARF::ParseVariables (const SymbolContext& sc,
4473 const DWARFDIE &orig_die,
4474 const lldb::addr_t func_low_pc,
4475 bool parse_siblings,
4476 bool parse_children,
4477 VariableList* cc_variable_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004478{
Greg Clayton6071e6f2015-08-26 22:57:51 +00004479 if (!orig_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004480 return 0;
4481
Greg Claytonc662ec82011-06-17 22:10:16 +00004482 VariableListSP variable_list_sp;
4483
4484 size_t vars_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004485 DWARFDIE die = orig_die;
4486 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004487 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004488 dw_tag_t tag = die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004489
4490 // Check to see if we have already parsed this variable or constant?
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004491 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton6071e6f2015-08-26 22:57:51 +00004492 if (var_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004493 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004494 if (cc_variable_list)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004495 cc_variable_list->AddVariableIfUnique (var_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004496 }
4497 else
4498 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004499 // We haven't already parsed it, lets do that now.
4500 if ((tag == DW_TAG_variable) ||
4501 (tag == DW_TAG_constant) ||
4502 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004503 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004504 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004505 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004506 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4507 dw_tag_t parent_tag = sc_parent_die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004508 switch (parent_tag)
4509 {
4510 case DW_TAG_compile_unit:
4511 if (sc.comp_unit != NULL)
4512 {
4513 variable_list_sp = sc.comp_unit->GetVariableList(false);
4514 if (variable_list_sp.get() == NULL)
4515 {
4516 variable_list_sp.reset(new VariableList());
4517 sc.comp_unit->SetVariableList(variable_list_sp);
4518 }
4519 }
4520 else
4521 {
Daniel Malead01b2952012-11-29 21:49:15 +00004522 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 +00004523 sc_parent_die.GetID(),
4524 sc_parent_die.GetTagAsCString(),
4525 orig_die.GetID(),
4526 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004527 }
4528 break;
4529
4530 case DW_TAG_subprogram:
4531 case DW_TAG_inlined_subroutine:
4532 case DW_TAG_lexical_block:
4533 if (sc.function != NULL)
4534 {
4535 // Check to see if we already have parsed the variables for the given scope
4536
Greg Clayton6071e6f2015-08-26 22:57:51 +00004537 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004538 if (block == NULL)
4539 {
4540 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004541 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00004542 // to find the concrete block so we can correctly add the
4543 // variable to it
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004544 const DWARFDIE concrete_block_die = FindBlockContainingSpecification (DIERef(sc.function->GetID()),
Greg Clayton6071e6f2015-08-26 22:57:51 +00004545 sc_parent_die.GetOffset());
Greg Claytonc662ec82011-06-17 22:10:16 +00004546 if (concrete_block_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004547 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004548 }
4549
4550 if (block != NULL)
4551 {
4552 const bool can_create = false;
4553 variable_list_sp = block->GetBlockVariableList (can_create);
4554 if (variable_list_sp.get() == NULL)
4555 {
4556 variable_list_sp.reset(new VariableList());
4557 block->SetVariableList(variable_list_sp);
4558 }
4559 }
4560 }
4561 break;
4562
4563 default:
Daniel Malead01b2952012-11-29 21:49:15 +00004564 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 +00004565 orig_die.GetID(),
4566 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004567 break;
4568 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004569 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004570
4571 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004572 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004573 VariableSP var_sp (ParseVariableDIE(sc, die, func_low_pc));
Greg Clayton73bf5db2011-06-17 01:22:15 +00004574 if (var_sp)
4575 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004576 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004577 if (cc_variable_list)
4578 cc_variable_list->AddVariableIfUnique (var_sp);
4579 ++vars_added;
4580 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004581 }
4582 }
4583 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004584
4585 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4586
Greg Clayton6071e6f2015-08-26 22:57:51 +00004587 if (!skip_children && parse_children && die.HasChildren())
Greg Claytonc662ec82011-06-17 22:10:16 +00004588 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004589 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, true, cc_variable_list);
Greg Claytonc662ec82011-06-17 22:10:16 +00004590 }
4591
4592 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004593 die = die.GetSibling();
Greg Claytonc662ec82011-06-17 22:10:16 +00004594 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00004595 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004596 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004597 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004598}
4599
4600//------------------------------------------------------------------
4601// PluginInterface protocol
4602//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004603ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004604SymbolFileDWARF::GetPluginName()
4605{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004606 return GetPluginNameStatic();
4607}
4608
4609uint32_t
4610SymbolFileDWARF::GetPluginVersion()
4611{
4612 return 1;
4613}
4614
4615void
Sean Callanancc427fa2011-07-30 02:42:06 +00004616SymbolFileDWARF::DumpIndexes ()
4617{
4618 StreamFile s(stdout, false);
4619
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004620 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00004621 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004622 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00004623 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
4624 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
4625 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
4626 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
4627 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
4628 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
4629 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00004630 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00004631}
4632
Greg Claytoncaab74e2012-01-28 00:48:57 +00004633
Greg Clayton1f746072012-08-29 21:13:06 +00004634SymbolFileDWARFDebugMap *
4635SymbolFileDWARF::GetDebugMapSymfile ()
4636{
4637 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
4638 {
4639 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
4640 if (module_sp)
4641 {
4642 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4643 if (sym_vendor)
4644 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
4645 }
4646 }
4647 return m_debug_map_symfile;
4648}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004649
4650DWARFExpression::LocationListFormat
4651SymbolFileDWARF::GetLocationListFormat() const
4652{
4653 return DWARFExpression::RegularLocationList;
4654}