blob: 4446d95ef98b063668f1b404663c3db2ca790993 [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));
Greg Clayton5ce1a842015-08-27 18:09:44 +00001066 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET);
Tamas Berghammer1966ac32016-01-11 14:56:05 +00001067 if (stmt_list != DW_INVALID_OFFSET)
1068 {
1069 // All file indexes in DWARF are one based and a file of index zero is
1070 // supposed to be the compile unit itself.
1071 support_files.Append (*sc.comp_unit);
1072 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(),
1073 get_debug_line_data(),
1074 cu_comp_dir,
1075 stmt_list,
1076 support_files);
1077 }
Greg Claytonda2455b2012-11-01 17:28:37 +00001078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 }
1080 return false;
1081}
1082
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001083bool
1084SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1085{
1086 assert (sc.comp_unit);
1087 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1088 if (dwarf_cu)
1089 {
1090 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1091 {
1092 UpdateExternalModuleListIfNeeded();
Sean Callananb0300a42016-01-14 21:46:09 +00001093
1094 if (sc.comp_unit)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001095 {
Sean Callananb0300a42016-01-14 21:46:09 +00001096 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1097
1098 if (die)
1099 {
1100 for (DWARFDIE child_die = die.GetFirstChild();
1101 child_die;
1102 child_die = child_die.GetSibling())
1103 {
1104 if (child_die.Tag() == DW_TAG_imported_declaration)
1105 {
1106 if (DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import))
1107 {
1108 if (module_die.Tag() == DW_TAG_module)
1109 {
1110 if (const char *name = module_die.GetAttributeValueAsString(DW_AT_name, nullptr))
1111 {
1112 ConstString const_name(name);
1113 imported_modules.push_back(const_name);
1114 }
1115 }
1116 }
1117 }
1118 }
1119 }
1120 }
1121 else
1122 {
1123 for (const auto &pair : m_external_type_modules)
1124 {
1125 imported_modules.push_back(pair.first);
1126 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001127 }
1128 }
1129 }
1130 return false;
1131}
1132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133struct ParseDWARFLineTableCallbackInfo
1134{
1135 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001136 std::unique_ptr<LineSequence> sequence_ap;
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001137 lldb::addr_t addr_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138};
1139
1140//----------------------------------------------------------------------
1141// ParseStatementTableCallback
1142//----------------------------------------------------------------------
1143static void
1144ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1145{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1147 {
1148 // Just started parsing the line table
1149 }
1150 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1151 {
1152 // Done parsing line table, nothing to do for the cleanup
1153 }
1154 else
1155 {
1156 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001157 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158
Greg Clayton9422dd62013-03-04 21:46:16 +00001159 // If this is our first time here, we need to create a
1160 // sequence container.
1161 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001163 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1164 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001166 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001167 state.address & info->addr_mask,
Greg Clayton9422dd62013-03-04 21:46:16 +00001168 state.line,
1169 state.column,
1170 state.file,
1171 state.is_stmt,
1172 state.basic_block,
1173 state.prologue_end,
1174 state.epilogue_begin,
1175 state.end_sequence);
1176 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001178 // First, put the current sequence into the line table.
1179 line_table->InsertSequence(info->sequence_ap.get());
1180 // Then, empty it to prepare for the next sequence.
1181 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
1184}
1185
1186bool
1187SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1188{
1189 assert (sc.comp_unit);
1190 if (sc.comp_unit->GetLineTable() != NULL)
1191 return true;
1192
Greg Clayton1f746072012-08-29 21:13:06 +00001193 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 if (dwarf_cu)
1195 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001196 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001197 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001199 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 +00001200 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001202 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001203 if (line_table_ap.get())
1204 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001205 ParseDWARFLineTableCallbackInfo info;
1206 info.line_table = line_table_ap.get();
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00001207
1208 /*
1209 * MIPS:
1210 * The SymbolContext may not have a valid target, thus we may not be able
1211 * to call Address::GetOpcodeLoadAddress() which would clear the bit #0
1212 * for MIPS. Use ArchSpec to clear the bit #0.
1213 */
1214 ArchSpec arch;
1215 GetObjectFile()->GetArchitecture(arch);
1216 switch (arch.GetMachine())
1217 {
1218 case llvm::Triple::mips:
1219 case llvm::Triple::mipsel:
1220 case llvm::Triple::mips64:
1221 case llvm::Triple::mips64el:
1222 info.addr_mask = ~((lldb::addr_t)1);
1223 break;
1224 default:
1225 info.addr_mask = ~((lldb::addr_t)0);
1226 break;
1227 }
1228
Greg Claytonc7bece562013-01-25 18:06:21 +00001229 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001230 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001231 if (m_debug_map_symfile)
1232 {
1233 // We have an object file that has a line table with addresses
1234 // that are not linked. We need to link the line table and convert
1235 // the addresses that are relative to the .o file into addresses
1236 // for the main executable.
1237 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1238 }
1239 else
1240 {
1241 sc.comp_unit->SetLineTable(line_table_ap.release());
1242 return true;
1243 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001244 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245 }
1246 }
1247 }
1248 return false;
1249}
1250
Siva Chandrad8335e92015-12-16 00:22:08 +00001251lldb_private::DebugMacrosSP
1252SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset)
1253{
1254 auto iter = m_debug_macros_map.find(*offset);
1255 if (iter != m_debug_macros_map.end())
1256 return iter->second;
1257
1258 const DWARFDataExtractor &debug_macro_data = get_debug_macro_data();
1259 if (debug_macro_data.GetByteSize() == 0)
1260 return DebugMacrosSP();
1261
1262 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1263 m_debug_macros_map[*offset] = debug_macros_sp;
1264
1265 const DWARFDebugMacroHeader &header = DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1266 DWARFDebugMacroEntry::ReadMacroEntries(
1267 debug_macro_data, get_debug_str_data(), header.OffsetIs64Bit(), offset, this, debug_macros_sp);
1268
1269 return debug_macros_sp;
1270}
1271
1272bool
1273SymbolFileDWARF::ParseCompileUnitDebugMacros(const SymbolContext& sc)
1274{
1275 assert (sc.comp_unit);
1276
1277 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1278 if (dwarf_cu == nullptr)
1279 return false;
1280
1281 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
1282 if (!dwarf_cu_die)
1283 return false;
1284
1285 lldb::offset_t sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1286 if (sect_offset == DW_INVALID_OFFSET)
1287 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros, DW_INVALID_OFFSET);
1288 if (sect_offset == DW_INVALID_OFFSET)
1289 return false;
1290
1291 sc.comp_unit->SetDebugMacros(ParseDebugMacros(&sect_offset));
1292
1293 return true;
1294}
1295
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00001297SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext& sc,
1298 Block *parent_block,
1299 const DWARFDIE &orig_die,
1300 addr_t subprogram_low_pc,
1301 uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302{
1303 size_t blocks_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001304 DWARFDIE die = orig_die;
1305 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001307 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308
1309 switch (tag)
1310 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001312 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 case DW_TAG_lexical_block:
1314 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001315 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001316 if (tag == DW_TAG_subprogram)
1317 {
1318 // Skip any DW_TAG_subprogram DIEs that are inside
1319 // of a normal or inlined functions. These will be
1320 // parsed on their own as separate entities.
1321
1322 if (depth > 0)
1323 break;
1324
1325 block = parent_block;
1326 }
1327 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001328 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001329 BlockSP block_sp(new Block (die.GetID()));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001330 parent_block->AddChild(block_sp);
1331 block = block_sp.get();
1332 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001333 DWARFRangeList ranges;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001334 const char *name = NULL;
1335 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 int decl_file = 0;
1338 int decl_line = 0;
1339 int decl_column = 0;
1340 int call_file = 0;
1341 int call_line = 0;
1342 int call_column = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001343 if (die.GetDIENamesAndRanges (name,
1344 mangled_name,
1345 ranges,
1346 decl_file, decl_line, decl_column,
1347 call_file, call_line, call_column, nullptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 {
1349 if (tag == DW_TAG_subprogram)
1350 {
1351 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001352 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001354 else if (tag == DW_TAG_inlined_subroutine)
1355 {
1356 // We get called here for inlined subroutines in two ways.
1357 // The first time is when we are making the Function object
1358 // for this inlined concrete instance. Since we're creating a top level block at
1359 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1360 // adjust the containing address.
1361 // The second time is when we are parsing the blocks inside the function that contains
1362 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1363 // function the offset will be for that function.
1364 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1365 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001366 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001367 }
1368 }
Greg Clayton103f3092015-01-15 03:04:37 +00001369
1370 const size_t num_ranges = ranges.GetSize();
1371 for (size_t i = 0; i<num_ranges; ++i)
1372 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001373 const DWARFRangeList::Entry &range = ranges.GetEntryRef (i);
Greg Clayton103f3092015-01-15 03:04:37 +00001374 const addr_t range_base = range.GetRangeBase();
1375 if (range_base >= subprogram_low_pc)
1376 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1377 else
1378 {
1379 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",
1380 block->GetID(),
1381 range_base,
1382 range.GetRangeEnd(),
1383 subprogram_low_pc);
1384 }
1385 }
1386 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387
1388 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1389 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001390 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001392 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1393 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394
Greg Clayton7b0992d2013-04-18 22:45:39 +00001395 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001397 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1398 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001400 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 }
1402
1403 ++blocks_added;
1404
Greg Clayton6071e6f2015-08-26 22:57:51 +00001405 if (die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001407 blocks_added += ParseFunctionBlocks (sc,
1408 block,
Greg Clayton6071e6f2015-08-26 22:57:51 +00001409 die.GetFirstChild(),
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001410 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001411 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 }
1413 }
1414 }
1415 break;
1416 default:
1417 break;
1418 }
1419
Greg Claytondd7feaf2011-08-12 17:54:33 +00001420 // Only parse siblings of the block if we are not at depth zero. A depth
1421 // of zero indicates we are currently parsing the top level
1422 // DW_TAG_subprogram DIE
1423
1424 if (depth == 0)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001425 die.Clear();
Greg Claytondd7feaf2011-08-12 17:54:33 +00001426 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001427 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 }
1429 return blocks_added;
1430}
1431
Greg Claytonf0705c82011-10-22 03:33:13 +00001432bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001433SymbolFileDWARF::ClassOrStructIsVirtual (const DWARFDIE &parent_die)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001434{
1435 if (parent_die)
1436 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001437 for (DWARFDIE die = parent_die.GetFirstChild(); die; die = die.GetSibling())
Greg Claytonc4ffd662013-03-08 01:37:30 +00001438 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001439 dw_tag_t tag = die.Tag();
Greg Claytonc4ffd662013-03-08 01:37:30 +00001440 bool check_virtuality = false;
1441 switch (tag)
1442 {
1443 case DW_TAG_inheritance:
1444 case DW_TAG_subprogram:
1445 check_virtuality = true;
1446 break;
1447 default:
1448 break;
1449 }
1450 if (check_virtuality)
1451 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001452 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001453 return true;
1454 }
1455 }
1456 }
1457 return false;
1458}
1459
Paul Hermanea188fc2015-09-16 18:48:30 +00001460void
1461SymbolFileDWARF::ParseDeclsForContext (CompilerDeclContext decl_ctx)
1462{
1463 TypeSystem *type_system = decl_ctx.GetTypeSystem();
1464 DWARFASTParser *ast_parser = type_system->GetDWARFParser();
1465 std::vector<DWARFDIE> decl_ctx_die_list = ast_parser->GetDIEForDeclContext(decl_ctx);
1466
1467 for (DWARFDIE decl_ctx_die : decl_ctx_die_list)
1468 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl; decl = decl.GetSibling())
1469 ast_parser->GetDeclForUIDFromDWARF(decl);
1470}
1471
Paul Hermand628cbb2015-09-15 23:44:17 +00001472CompilerDecl
1473SymbolFileDWARF::GetDeclForUID (lldb::user_id_t type_uid)
1474{
1475 if (UserIDMatches(type_uid))
1476 {
1477 DWARFDebugInfo* debug_info = DebugInfo();
1478 if (debug_info)
1479 {
1480 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
1481 if (die)
1482 {
1483 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1484 if (dwarf_ast)
1485 return dwarf_ast->GetDeclForUIDFromDWARF(die);
1486 }
1487 }
1488 }
1489 return CompilerDecl();
1490}
1491
Greg Clayton99558cc42015-08-24 23:46:31 +00001492CompilerDeclContext
1493SymbolFileDWARF::GetDeclContextForUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001495 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00001497 DWARFDebugInfo* debug_info = DebugInfo();
1498 if (debug_info)
1499 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001500 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001501 if (die)
1502 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001503 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1504 if (dwarf_ast)
1505 return dwarf_ast->GetDeclContextForUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001506 }
1507 }
Sean Callanan72e49402011-08-05 23:43:37 +00001508 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001509 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001510}
1511
Greg Clayton99558cc42015-08-24 23:46:31 +00001512CompilerDeclContext
1513SymbolFileDWARF::GetDeclContextContainingUID (lldb::user_id_t type_uid)
Sean Callanan72e49402011-08-05 23:43:37 +00001514{
Greg Clayton81c22f62011-10-19 18:09:39 +00001515 if (UserIDMatches(type_uid))
Greg Clayton8b4edba2015-08-14 20:02:05 +00001516 {
1517 DWARFDebugInfo* debug_info = DebugInfo();
1518 if (debug_info)
1519 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001520 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid));
Greg Clayton8b4edba2015-08-14 20:02:05 +00001521 if (die)
1522 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001523 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1524 if (dwarf_ast)
1525 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001526 }
1527 }
1528 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001529 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001530}
1531
Greg Clayton99558cc42015-08-24 23:46:31 +00001532
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001534SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535{
Greg Clayton81c22f62011-10-19 18:09:39 +00001536 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001538 DWARFDebugInfo* debug_info = DebugInfo();
1539 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001540 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001541 DWARFDIE type_die = debug_info->GetDIE (DIERef(type_uid));
Greg Clayton6071e6f2015-08-26 22:57:51 +00001542 if (type_die)
1543 {
1544 const bool assert_not_being_parsed = true;
1545 return ResolveTypeUID (type_die, assert_not_being_parsed);
1546 }
Greg Claytonca512b32011-01-14 04:54:56 +00001547 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548 }
1549 return NULL;
1550}
1551
Greg Claytoncab36a32011-12-08 05:16:30 +00001552Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001553SymbolFileDWARF::ResolveTypeUID (const DWARFDIE &die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001554{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001555 if (die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001556 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001557 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00001558 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001559 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001560 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001561 die.GetOffset(),
1562 die.GetTagAsCString(),
1563 die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001564
Greg Claytoncab36a32011-12-08 05:16:30 +00001565 // We might be coming in in the middle of a type tree (a class
1566 // withing a class, an enum within a class), so parse any needed
1567 // parent DIEs before we get to this one...
Greg Clayton6071e6f2015-08-26 22:57:51 +00001568 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE (die);
1569 if (decl_ctx_die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001570 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001571 if (log)
Greg Claytoncab36a32011-12-08 05:16:30 +00001572 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001573 switch (decl_ctx_die.Tag())
1574 {
1575 case DW_TAG_structure_type:
1576 case DW_TAG_union_type:
1577 case DW_TAG_class_type:
1578 {
1579 // Get the type, which could be a forward declaration
1580 if (log)
1581 GetObjectFile()->GetModule()->LogMessage (log,
1582 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1583 die.GetOffset(),
1584 die.GetTagAsCString(),
1585 die.GetName(),
1586 decl_ctx_die.GetOffset());
1587 }
1588 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001589
Greg Clayton6071e6f2015-08-26 22:57:51 +00001590 default:
1591 break;
1592 }
1593 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001594 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001595 return ResolveType (die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001596 }
1597 return NULL;
1598}
1599
Greg Clayton6beaaa62011-01-17 03:46:26 +00001600// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1601// SymbolFileDWARF objects to detect if this DWARF file is the one that
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001602// can resolve a compiler_type.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001603bool
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001604SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &compiler_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001605{
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001606 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00001607 if (GetForwardDeclClangTypeToDie().count (compiler_type_no_qualifiers.GetOpaqueQualType()))
1608 {
1609 return true;
1610 }
1611 TypeSystem *type_system = compiler_type.GetTypeSystem();
1612 if (type_system)
1613 {
1614 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1615 if (dwarf_ast)
1616 return dwarf_ast->CanCompleteType(compiler_type);
1617 }
1618 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001619}
1620
1621
Greg Clayton57ee3062013-07-11 22:46:58 +00001622bool
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001623SymbolFileDWARF::CompleteType (CompilerType &compiler_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001624{
Greg Claytonfb85e622016-02-09 22:36:24 +00001625 lldb_private::Mutex::Locker locker(GetObjectFile()->GetModule()->GetMutex());
1626
Greg Claytone6b36cd2015-12-08 01:02:08 +00001627 TypeSystem *type_system = compiler_type.GetTypeSystem();
1628 if (type_system)
1629 {
1630 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1631 if (dwarf_ast && dwarf_ast->CanCompleteType(compiler_type))
1632 return dwarf_ast->CompleteType(compiler_type);
1633 }
1634
Greg Clayton1be10fc2010-09-29 01:12:09 +00001635 // We have a struct/union/class/enum that needs to be fully resolved.
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001636 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type);
1637 auto die_it = GetForwardDeclClangTypeToDie().find (compiler_type_no_qualifiers.GetOpaqueQualType());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001638 if (die_it == GetForwardDeclClangTypeToDie().end())
Greg Clayton73b472d2010-10-27 03:32:59 +00001639 {
1640 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00001641 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00001642 }
Tamas Berghammerf8fd9b52015-09-14 15:44:29 +00001643
1644 DWARFDebugInfo* debug_info = DebugInfo();
1645 DWARFDIE dwarf_die = debug_info->GetDIE(die_it->getSecond());
1646
Tamas Berghammer69d0b332015-10-09 12:43:08 +00001647 assert(UserIDMatches(die_it->getSecond().GetUID()) && "CompleteType called on the wrong SymbolFile");
1648
Greg Clayton73b472d2010-10-27 03:32:59 +00001649 // Once we start resolving this type, remove it from the forward declaration
1650 // map in case anyone child members or other types require this type to get resolved.
1651 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1652 // are done.
Tamas Berghammerf8fd9b52015-09-14 15:44:29 +00001653 GetForwardDeclClangTypeToDie().erase (die_it);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001654
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001655 Type *type = GetDIEToType().lookup (dwarf_die.GetDIE());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001656
Greg Clayton5160ce52013-03-27 23:08:40 +00001657 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001658 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001659 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00001660 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001661 dwarf_die.GetID(),
1662 dwarf_die.GetTagAsCString(),
Greg Claytond61c0fc2012-04-23 22:55:20 +00001663 type->GetName().AsCString());
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001664 assert (compiler_type);
Greg Clayton261ac3f2015-08-28 01:01:03 +00001665 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1666 if (dwarf_ast)
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001667 return dwarf_ast->CompleteTypeFromDWARF (dwarf_die, type, compiler_type);
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001668 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001669}
1670
Greg Clayton8b4edba2015-08-14 20:02:05 +00001671Type*
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00001672SymbolFileDWARF::ResolveType (const DWARFDIE &die, bool assert_not_being_parsed, bool resolve_function_context)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001673{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001674 if (die)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001675 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001676 Type *type = GetDIEToType().lookup (die.GetDIE());
Greg Claytoncab36a32011-12-08 05:16:30 +00001677
Greg Claytonc685f8e2010-09-15 04:15:46 +00001678 if (type == NULL)
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00001679 type = GetTypeForDIE (die, resolve_function_context).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001680
Greg Clayton24739922010-10-13 03:15:28 +00001681 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001682 {
1683 if (type != DIE_IS_BEING_PARSED)
1684 return type;
1685
1686 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001687 die.GetOffset(),
1688 die.GetTagAsCString(),
1689 die.GetName());
Jim Inghamc3549282012-01-11 02:21:12 +00001690
1691 }
1692 else
1693 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001694 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001695 return nullptr;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001696}
1697
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001699SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700{
1701 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001702 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 {
1704 // The symbol vendor doesn't know about this compile unit, we
1705 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00001706 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001708 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709}
1710
Greg Clayton8b4edba2015-08-14 20:02:05 +00001711size_t
1712SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets)
1713{
1714 method_die_offsets.clear();
1715 if (m_using_apple_tables)
1716 {
1717 if (m_apple_objc_ap.get())
1718 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1719 }
1720 else
1721 {
1722 if (!m_indexed)
1723 Index ();
1724
1725 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1726 }
1727 return method_die_offsets.size();
1728}
1729
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001731SymbolFileDWARF::GetFunction (const DWARFDIE &die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001732{
Greg Clayton72310352013-02-23 04:12:47 +00001733 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734
Greg Clayton6071e6f2015-08-26 22:57:51 +00001735 if (die)
1736 {
1737 // Check if the symbol vendor already knows about this compile unit?
1738 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1739
1740 sc.function = sc.comp_unit->FindFunctionByUID (die.GetID()).get();
1741 if (sc.function == NULL)
1742 sc.function = ParseCompileUnitFunction(sc, die);
1743
1744 if (sc.function)
1745 {
1746 sc.module_sp = sc.function->CalculateSymbolContextModule();
1747 return true;
1748 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00001749 }
1750
1751 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
Greg Claytone6b36cd2015-12-08 01:02:08 +00001754lldb::ModuleSP
1755SymbolFileDWARF::GetDWOModule (ConstString name)
1756{
1757 UpdateExternalModuleListIfNeeded();
1758 const auto &pos = m_external_type_modules.find(name);
1759 if (pos != m_external_type_modules.end())
1760 return pos->second;
1761 else
1762 return lldb::ModuleSP();
1763}
1764
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001765void
1766SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
1767{
1768 if (m_fetched_external_modules)
1769 return;
1770 m_fetched_external_modules = true;
1771
1772 DWARFDebugInfo * debug_info = DebugInfo();
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001773
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001774 const uint32_t num_compile_units = GetNumCompileUnits();
1775 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1776 {
1777 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1778
Greg Clayton5ce1a842015-08-27 18:09:44 +00001779 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1780 if (die && die.HasChildren() == false)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001781 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001782 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1783
1784 if (name)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001785 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001786 ConstString const_name(name);
1787 if (m_external_type_modules.find(const_name) == m_external_type_modules.end())
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001788 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001789 ModuleSP module_sp;
1790 const char *dwo_path = die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
1791 if (dwo_path)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001792 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00001793 ModuleSpec dwo_module_spec;
1794 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1795 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
1796 //printf ("Loading dwo = '%s'\n", dwo_path);
1797 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001798 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00001799 m_external_type_modules[const_name] = module_sp;
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001800 }
1801 }
1802 }
1803 }
1804}
Greg Clayton2501e5e2015-01-15 02:59:20 +00001805
1806SymbolFileDWARF::GlobalVariableMap &
1807SymbolFileDWARF::GetGlobalAranges()
1808{
1809 if (!m_global_aranges_ap)
1810 {
1811 m_global_aranges_ap.reset (new GlobalVariableMap());
1812
1813 ModuleSP module_sp = GetObjectFile()->GetModule();
1814 if (module_sp)
1815 {
1816 const size_t num_cus = module_sp->GetNumCompileUnits();
1817 for (size_t i = 0; i < num_cus; ++i)
1818 {
1819 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1820 if (cu_sp)
1821 {
1822 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1823 if (globals_sp)
1824 {
1825 const size_t num_globals = globals_sp->GetSize();
1826 for (size_t g = 0; g < num_globals; ++g)
1827 {
1828 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1829 if (var_sp && !var_sp->GetLocationIsConstantValueData())
1830 {
1831 const DWARFExpression &location = var_sp->LocationExpression();
1832 Value location_result;
1833 Error error;
Tamas Berghammer5b42c7a2016-02-26 14:21:10 +00001834 if (location.Evaluate(nullptr, nullptr, nullptr, LLDB_INVALID_ADDRESS, nullptr, nullptr, location_result, &error))
Greg Clayton2501e5e2015-01-15 02:59:20 +00001835 {
1836 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
1837 {
1838 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
1839 lldb::addr_t byte_size = 1;
1840 if (var_sp->GetType())
1841 byte_size = var_sp->GetType()->GetByteSize();
1842 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
1843 }
1844 }
1845 }
1846 }
1847 }
1848 }
1849 }
1850 }
1851 m_global_aranges_ap->Sort();
1852 }
1853 return *m_global_aranges_ap;
1854}
1855
1856
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001857uint32_t
1858SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1859{
1860 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00001861 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001862 static_cast<void*>(so_addr.GetSection().get()),
1863 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00001865 if (resolve_scope & ( eSymbolContextCompUnit |
1866 eSymbolContextFunction |
1867 eSymbolContextBlock |
1868 eSymbolContextLineEntry |
1869 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001870 {
1871 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1872
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001874 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001876 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00001877 if (cu_offset == DW_INVALID_OFFSET)
1878 {
1879 // Global variables are not in the compile unit address ranges. The only way to
1880 // currently find global variables is to iterate over the .debug_pubnames or the
1881 // __apple_names table and find all items in there that point to DW_TAG_variable
1882 // DIEs and then find the address that matches.
1883 if (resolve_scope & eSymbolContextVariable)
1884 {
1885 GlobalVariableMap &map = GetGlobalAranges();
1886 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
1887 if (entry && entry->data)
1888 {
1889 Variable *variable = entry->data;
1890 SymbolContextScope *scc = variable->GetSymbolContextScope();
1891 if (scc)
1892 {
1893 scc->CalculateSymbolContext(&sc);
1894 sc.variable = variable;
1895 }
1896 return sc.GetResolvedMask();
1897 }
1898 }
1899 }
1900 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001902 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001903 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001904 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001905 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001906 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001907 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001908 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001909 resolved |= eSymbolContextCompUnit;
1910
Greg Clayton6ab80132012-12-12 17:30:52 +00001911 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001912 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1913 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001914 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1915 DWARFDIE block_die;
1916 if (function_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001918 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001919 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001920 sc.function = ParseCompileUnitFunction(sc, function_die);
1921
1922 if (sc.function && (resolve_scope & eSymbolContextBlock))
1923 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001924 }
1925 else
1926 {
1927 // We might have had a compile unit that had discontiguous
1928 // address ranges where the gaps are symbols that don't have
1929 // any debug info. Discontiguous compile unit address ranges
1930 // should only happen when there aren't other functions from
1931 // other compile units in these gaps. This helps keep the size
1932 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00001933 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001934 }
1935
1936 if (sc.function != NULL)
1937 {
1938 resolved |= eSymbolContextFunction;
1939
1940 if (resolve_scope & eSymbolContextBlock)
1941 {
1942 Block& block = sc.function->GetBlock (true);
1943
Greg Clayton6071e6f2015-08-26 22:57:51 +00001944 if (block_die)
1945 sc.block = block.FindBlockByID (block_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001946 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001947 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001948 if (sc.block)
1949 resolved |= eSymbolContextBlock;
1950 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951 }
1952 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001953
1954 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
1955 {
1956 LineTable *line_table = sc.comp_unit->GetLineTable();
1957 if (line_table != NULL)
1958 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001959 // And address that makes it into this function should be in terms
1960 // of this debug file if there is no debug map, or it will be an
1961 // address in the .o file which needs to be fixed up to be in terms
1962 // of the debug map executable. Either way, calling FixupAddress()
1963 // will work for us.
1964 Address exe_so_addr (so_addr);
1965 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00001966 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001967 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00001968 {
1969 resolved |= eSymbolContextLineEntry;
1970 }
1971 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001972 }
1973 }
1974
1975 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
1976 {
1977 // We might have had a compile unit that had discontiguous
1978 // address ranges where the gaps are symbols that don't have
1979 // any debug info. Discontiguous compile unit address ranges
1980 // should only happen when there aren't other functions from
1981 // other compile units in these gaps. This helps keep the size
1982 // of the aranges down.
1983 sc.comp_unit = NULL;
1984 resolved &= ~eSymbolContextCompUnit;
1985 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001987 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001989 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
1990 cu_offset,
1991 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992 }
1993 }
1994 }
1995 }
1996 }
1997 return resolved;
1998}
1999
2000
2001
2002uint32_t
2003SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2004{
2005 const uint32_t prev_size = sc_list.GetSize();
2006 if (resolve_scope & eSymbolContextCompUnit)
2007 {
2008 DWARFDebugInfo* debug_info = DebugInfo();
2009 if (debug_info)
2010 {
2011 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002012 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002013
Greg Clayton53eb1c22012-04-02 22:59:12 +00002014 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002015 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002016 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00002017 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00002018 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 +00002019 if (check_inlines || file_spec_matches_cu_file_spec)
2020 {
2021 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002022 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002023 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002025 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026
Greg Clayton526a4ae2012-05-16 22:09:01 +00002027 // If we are looking for inline functions only and we don't
2028 // find it in the support files, we are done.
2029 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002031 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2032 if (file_idx == UINT32_MAX)
2033 continue;
2034 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035
Greg Clayton526a4ae2012-05-16 22:09:01 +00002036 if (line != 0)
2037 {
2038 LineTable *line_table = sc.comp_unit->GetLineTable();
2039
2040 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002042 // We will have already looked up the file index if
2043 // we are searching for inline entries.
2044 if (!check_inlines)
2045 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046
Greg Clayton526a4ae2012-05-16 22:09:01 +00002047 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002049 uint32_t found_line;
2050 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2051 found_line = sc.line_entry.line;
2052
2053 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002054 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002055 sc.function = NULL;
2056 sc.block = NULL;
2057 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002059 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2060 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002062 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
2063 DWARFDIE block_die;
2064 if (function_die)
Greg Clayton526a4ae2012-05-16 22:09:01 +00002065 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002066 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00002067 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002068 sc.function = ParseCompileUnitFunction(sc, function_die);
2069
2070 if (sc.function && (resolve_scope & eSymbolContextBlock))
2071 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073
Greg Clayton526a4ae2012-05-16 22:09:01 +00002074 if (sc.function != NULL)
2075 {
2076 Block& block = sc.function->GetBlock (true);
2077
Greg Clayton6071e6f2015-08-26 22:57:51 +00002078 if (block_die)
2079 sc.block = block.FindBlockByID (block_die.GetID());
2080 else if (function_die)
2081 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00002082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002083 }
2084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085
Greg Clayton526a4ae2012-05-16 22:09:01 +00002086 sc_list.Append(sc);
2087 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2088 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089 }
2090 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002091 else if (file_spec_matches_cu_file_spec && !check_inlines)
2092 {
2093 // only append the context if we aren't looking for inline call sites
2094 // by file and line and if the file spec matches that of the compile unit
2095 sc_list.Append(sc);
2096 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002097 }
2098 else if (file_spec_matches_cu_file_spec && !check_inlines)
2099 {
2100 // only append the context if we aren't looking for inline call sites
2101 // by file and line and if the file spec matches that of the compile unit
2102 sc_list.Append(sc);
2103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104
Greg Clayton526a4ae2012-05-16 22:09:01 +00002105 if (!check_inlines)
2106 break;
2107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002108 }
2109 }
2110 }
2111 }
2112 return sc_list.GetSize() - prev_size;
2113}
2114
2115void
2116SymbolFileDWARF::Index ()
2117{
2118 if (m_indexed)
2119 return;
2120 m_indexed = true;
2121 Timer scoped_timer (__PRETTY_FUNCTION__,
2122 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00002123 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124
2125 DWARFDebugInfo* debug_info = DebugInfo();
2126 if (debug_info)
2127 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128 const uint32_t num_compile_units = GetNumCompileUnits();
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002129 std::vector<NameToDIE> function_basename_index(num_compile_units);
2130 std::vector<NameToDIE> function_fullname_index(num_compile_units);
2131 std::vector<NameToDIE> function_method_index(num_compile_units);
2132 std::vector<NameToDIE> function_selector_index(num_compile_units);
2133 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units);
2134 std::vector<NameToDIE> global_index(num_compile_units);
2135 std::vector<NameToDIE> type_index(num_compile_units);
2136 std::vector<NameToDIE> namespace_index(num_compile_units);
2137
2138 auto parser_fn = [this,
2139 debug_info,
2140 &function_basename_index,
2141 &function_fullname_index,
2142 &function_method_index,
2143 &function_selector_index,
2144 &objc_class_selectors_index,
2145 &global_index,
2146 &type_index,
2147 &namespace_index](uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002148 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002149 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002150 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded(false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002151
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002152 dwarf_cu->Index(function_basename_index[cu_idx],
2153 function_fullname_index[cu_idx],
2154 function_method_index[cu_idx],
2155 function_selector_index[cu_idx],
2156 objc_class_selectors_index[cu_idx],
2157 global_index[cu_idx],
2158 type_index[cu_idx],
2159 namespace_index[cu_idx]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002160
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161 // Keep memory down by clearing DIEs if this generate function
2162 // caused them to be parsed
2163 if (clear_dies)
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002164 dwarf_cu->ClearDIEs(true);
2165
2166 return cu_idx;
2167 };
2168
2169 TaskRunner<uint32_t> task_runner;
2170 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2171 task_runner.AddTask(parser_fn, cu_idx);
2172
2173 while (true)
2174 {
2175 std::future<uint32_t> f = task_runner.WaitForNextCompletedTask();
2176 if (!f.valid())
2177 break;
2178 uint32_t cu_idx = f.get();
2179
2180 m_function_basename_index.Append(function_basename_index[cu_idx]);
2181 m_function_fullname_index.Append(function_fullname_index[cu_idx]);
2182 m_function_method_index.Append(function_method_index[cu_idx]);
2183 m_function_selector_index.Append(function_selector_index[cu_idx]);
2184 m_objc_class_selectors_index.Append(objc_class_selectors_index[cu_idx]);
2185 m_global_index.Append(global_index[cu_idx]);
2186 m_type_index.Append(type_index[cu_idx]);
2187 m_namespace_index.Append(namespace_index[cu_idx]);
Tamas Berghammerda4e8ed2015-10-20 15:43:40 +00002188 }
Tamas Berghammer2ff88702015-10-23 10:34:49 +00002189
2190 TaskPool::RunTasks(
2191 [&]() { m_function_basename_index.Finalize(); },
2192 [&]() { m_function_fullname_index.Finalize(); },
2193 [&]() { m_function_method_index.Finalize(); },
2194 [&]() { m_function_selector_index.Finalize(); },
2195 [&]() { m_objc_class_selectors_index.Finalize(); },
2196 [&]() { m_global_index.Finalize(); },
2197 [&]() { m_type_index.Finalize(); },
2198 [&]() { m_namespace_index.Finalize(); });
Greg Claytonc685f8e2010-09-15 04:15:46 +00002199
Greg Clayton24739922010-10-13 03:15:28 +00002200#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002201 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002202 s.Printf ("DWARF index for '%s':",
2203 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002204 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2205 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2206 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2207 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2208 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2209 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002210 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00002211 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002212#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002213 }
2214}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002215
2216bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002217SymbolFileDWARF::DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx)
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002218{
Greg Clayton99558cc42015-08-24 23:46:31 +00002219 if (decl_ctx == nullptr || !decl_ctx->IsValid())
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002220 {
2221 // Invalid namespace decl which means we aren't matching only things
2222 // in this symbol file, so return true to indicate it matches this
2223 // symbol file.
2224 return true;
2225 }
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002226
Greg Clayton56939cb2015-09-17 22:23:34 +00002227 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2228 TypeSystem *type_system = GetTypeSystemForLanguage(decl_ctx_type_system->GetMinimumLanguage(nullptr));
2229 if (decl_ctx_type_system == type_system)
Greg Clayton99558cc42015-08-24 23:46:31 +00002230 return true; // The type systems match, return true
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002231
2232 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00002233 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00002234
2235 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002236 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002237
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002238 return false;
2239}
2240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002242SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243{
Greg Clayton5160ce52013-03-27 23:08:40 +00002244 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002245
2246 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002247 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002248 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00002249 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002250 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002251 append, max_matches);
2252
Greg Clayton99558cc42015-08-24 23:46:31 +00002253 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002254 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002255
Greg Claytonc685f8e2010-09-15 04:15:46 +00002256 DWARFDebugInfo* info = DebugInfo();
2257 if (info == NULL)
2258 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002259
2260 // If we aren't appending the results to this list, then clear the list
2261 if (!append)
2262 variables.Clear();
2263
2264 // Remember how many variables are in the list before we search in case
2265 // we are appending the results to a variable list.
2266 const uint32_t original_size = variables.GetSize();
2267
Greg Claytond4a2b372011-09-12 23:21:58 +00002268 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002269
Greg Clayton97fbc342011-10-20 22:30:33 +00002270 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002271 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002272 if (m_apple_names_ap.get())
2273 {
2274 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00002275 llvm::StringRef basename;
2276 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002277
Jim Inghamaa816b82015-09-02 01:59:14 +00002278 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, basename))
Jim Inghamfa39bb42014-10-25 00:33:55 +00002279 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002280
Jim Inghamfa39bb42014-10-25 00:33:55 +00002281 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00002282 }
Greg Clayton7f995132011-10-04 22:41:51 +00002283 }
2284 else
2285 {
2286 // Index the DWARF if we haven't already
2287 if (!m_indexed)
2288 Index ();
2289
2290 m_global_index.Find (name, die_offsets);
2291 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002292
Greg Clayton437a1352012-04-09 22:43:43 +00002293 const size_t num_die_matches = die_offsets.size();
2294 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002295 {
Greg Clayton7f995132011-10-04 22:41:51 +00002296 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002297 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002298 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002299
Greg Claytond4a2b372011-09-12 23:21:58 +00002300 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002301 bool done = false;
2302 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002303 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002304 const DIERef& die_ref = die_offsets[i];
2305 DWARFDIE die = debug_info->GetDIE (die_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306
Greg Clayton95d87902011-11-11 03:16:25 +00002307 if (die)
2308 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002309 switch (die.Tag())
Greg Clayton437a1352012-04-09 22:43:43 +00002310 {
2311 default:
2312 case DW_TAG_subprogram:
2313 case DW_TAG_inlined_subroutine:
2314 case DW_TAG_try_block:
2315 case DW_TAG_catch_block:
2316 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002317
Greg Clayton437a1352012-04-09 22:43:43 +00002318 case DW_TAG_variable:
2319 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002320 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002321
Greg Clayton99558cc42015-08-24 23:46:31 +00002322 if (parent_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002323 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002324 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2325 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002326 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002327 CompilerDeclContext actual_parent_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002328 if (!actual_parent_decl_ctx || actual_parent_decl_ctx != *parent_decl_ctx)
2329 continue;
2330 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00002331 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002332
Greg Clayton6071e6f2015-08-26 22:57:51 +00002333 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002334
Greg Clayton437a1352012-04-09 22:43:43 +00002335 if (variables.GetSize() - original_size >= max_matches)
2336 done = true;
2337 }
2338 break;
2339 }
Greg Clayton95d87902011-11-11 03:16:25 +00002340 }
2341 else
2342 {
2343 if (m_using_apple_tables)
2344 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002345 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 +00002346 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002347 }
2348 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002349 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002350 }
2351
2352 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002353 const uint32_t num_matches = variables.GetSize() - original_size;
2354 if (log && num_matches > 0)
2355 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002356 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002357 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002358 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002359 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002360 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002361 num_matches);
2362 }
2363 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002364}
2365
2366uint32_t
2367SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2368{
Greg Clayton5160ce52013-03-27 23:08:40 +00002369 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002370
Greg Clayton21f2a492011-10-06 00:09:08 +00002371 if (log)
2372 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002373 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002374 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002375 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002376 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002377 }
2378
Greg Claytonc685f8e2010-09-15 04:15:46 +00002379 DWARFDebugInfo* info = DebugInfo();
2380 if (info == NULL)
2381 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002382
2383 // If we aren't appending the results to this list, then clear the list
2384 if (!append)
2385 variables.Clear();
2386
2387 // Remember how many variables are in the list before we search in case
2388 // we are appending the results to a variable list.
2389 const uint32_t original_size = variables.GetSize();
2390
Greg Clayton7f995132011-10-04 22:41:51 +00002391 DIEArray die_offsets;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002392
Greg Clayton97fbc342011-10-20 22:30:33 +00002393 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002394 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002395 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002396 {
2397 DWARFMappedHash::DIEInfoArray hash_data_array;
2398 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2399 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2400 }
Greg Clayton7f995132011-10-04 22:41:51 +00002401 }
2402 else
2403 {
2404 // Index the DWARF if we haven't already
2405 if (!m_indexed)
2406 Index ();
2407
2408 m_global_index.Find (regex, die_offsets);
2409 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002410
Greg Claytonc685f8e2010-09-15 04:15:46 +00002411 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002412 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002413 assert (sc.module_sp);
2414
Greg Clayton7f995132011-10-04 22:41:51 +00002415 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002416 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002418 DWARFDebugInfo* debug_info = DebugInfo();
2419 for (size_t i=0; i<num_matches; ++i)
2420 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002421 const DIERef& die_ref = die_offsets[i];
2422 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00002423
2424 if (die)
2425 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002426 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002427
Greg Clayton6071e6f2015-08-26 22:57:51 +00002428 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002429
Greg Clayton95d87902011-11-11 03:16:25 +00002430 if (variables.GetSize() - original_size >= max_matches)
2431 break;
2432 }
2433 else
2434 {
2435 if (m_using_apple_tables)
2436 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002437 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 +00002438 die_ref.die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002439 }
2440 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002441 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002442 }
2443
2444 // Return the number of variable that were appended to the list
2445 return variables.GetSize() - original_size;
2446}
2447
Greg Claytonaa044962011-10-13 00:59:38 +00002448
Jim Ingham4cda6e02011-10-07 22:23:45 +00002449bool
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002450SymbolFileDWARF::ResolveFunction (const DIERef& die_ref,
Pavel Labatha73d6572015-03-13 10:22:00 +00002451 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00002452 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002453{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002454 DWARFDIE die = DebugInfo()->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002455 return ResolveFunction (die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00002456}
2457
2458
2459bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00002460SymbolFileDWARF::ResolveFunction (const DWARFDIE &orig_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002461 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00002462 SymbolContextList& sc_list)
2463{
Greg Clayton9e315582011-09-02 04:03:59 +00002464 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002465
Greg Clayton6071e6f2015-08-26 22:57:51 +00002466 if (!orig_die)
Greg Claytonaa044962011-10-13 00:59:38 +00002467 return false;
2468
Jim Ingham4cda6e02011-10-07 22:23:45 +00002469 // If we were passed a die that is not a function, just return false...
Greg Clayton6071e6f2015-08-26 22:57:51 +00002470 if (!(orig_die.Tag() == DW_TAG_subprogram || (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002471 return false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002472
2473 DWARFDIE die = orig_die;
2474 DWARFDIE inlined_die;
2475 if (die.Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002476 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002477 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002478
Greg Clayton6071e6f2015-08-26 22:57:51 +00002479 while (1)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002480 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002481 die = die.GetParent();
2482
2483 if (die)
2484 {
2485 if (die.Tag() == DW_TAG_subprogram)
2486 break;
2487 }
2488 else
Jim Ingham4cda6e02011-10-07 22:23:45 +00002489 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002490 }
2491 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002492 assert (die && die.Tag() == DW_TAG_subprogram);
2493 if (GetFunction (die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002494 {
2495 Address addr;
2496 // Parse all blocks if needed
2497 if (inlined_die)
2498 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002499 Block &function_block = sc.function->GetBlock (true);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002500 sc.block = function_block.FindBlockByID (inlined_die.GetID());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002501 if (sc.block == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002502 sc.block = function_block.FindBlockByID (inlined_die.GetOffset());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002503 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002504 addr.Clear();
2505 }
2506 else
2507 {
2508 sc.block = NULL;
2509 addr = sc.function->GetAddressRange().GetBaseAddress();
2510 }
2511
2512 if (addr.IsValid())
2513 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002514 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002515 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002516 }
2517 }
2518
Greg Claytonaa044962011-10-13 00:59:38 +00002519 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002520}
2521
Greg Clayton7f995132011-10-04 22:41:51 +00002522void
2523SymbolFileDWARF::FindFunctions (const ConstString &name,
2524 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002525 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002526 SymbolContextList& sc_list)
2527{
Greg Claytond4a2b372011-09-12 23:21:58 +00002528 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002529 if (name_to_die.Find (name, die_offsets))
2530 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002531 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002532 }
2533}
2534
2535
2536void
2537SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2538 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002539 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002540 SymbolContextList& sc_list)
2541{
2542 DIEArray die_offsets;
2543 if (name_to_die.Find (regex, die_offsets))
2544 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002545 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002546 }
2547}
2548
2549
2550void
2551SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2552 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00002553 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002554 SymbolContextList& sc_list)
2555{
2556 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002557 DWARFMappedHash::DIEInfoArray hash_data_array;
2558 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002559 {
Greg Claytond1767f02011-12-08 02:13:16 +00002560 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00002561 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002562 }
2563}
2564
2565void
2566SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00002567 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002568 SymbolContextList& sc_list)
2569{
2570 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002571 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002572 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002573 for (size_t i=0; i<num_matches; ++i)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002574 ResolveFunction (die_offsets[i], include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002575 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002576}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002577
Jim Ingham4cda6e02011-10-07 22:23:45 +00002578bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002579SymbolFileDWARF::DIEInDeclContext (const CompilerDeclContext *decl_ctx,
Greg Clayton6071e6f2015-08-26 22:57:51 +00002580 const DWARFDIE &die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002581{
2582 // If we have no parent decl context to match this DIE matches, and if the parent
2583 // decl context isn't valid, we aren't trying to look for any particular decl
2584 // context so any die matches.
2585 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2586 return true;
2587
Greg Clayton6071e6f2015-08-26 22:57:51 +00002588 if (die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002589 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002590 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2591 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002592 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002593 CompilerDeclContext actual_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002594 if (actual_decl_ctx)
2595 return actual_decl_ctx == *decl_ctx;
2596 }
2597 }
2598 return false;
2599}
2600
Greg Clayton0c5cd902010-06-28 21:30:43 +00002601uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002602SymbolFileDWARF::FindFunctions (const ConstString &name,
2603 const CompilerDeclContext *parent_decl_ctx,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002604 uint32_t name_type_mask,
2605 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002606 bool append,
2607 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002608{
2609 Timer scoped_timer (__PRETTY_FUNCTION__,
2610 "SymbolFileDWARF::FindFunctions (name = '%s')",
2611 name.AsCString());
2612
Greg Clayton43fe2172013-04-03 02:00:15 +00002613 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
2614 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
2615
Greg Clayton5160ce52013-03-27 23:08:40 +00002616 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002617
2618 if (log)
2619 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002620 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002621 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2622 name.GetCString(),
2623 name_type_mask,
2624 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002625 }
2626
Greg Clayton0c5cd902010-06-28 21:30:43 +00002627 // If we aren't appending the results to this list, then clear the list
2628 if (!append)
2629 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002630
Greg Clayton99558cc42015-08-24 23:46:31 +00002631 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002632 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002633
2634 // If name is empty then we won't find anything.
2635 if (name.IsEmpty())
2636 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002637
2638 // Remember how many sc_list are in the list before we search in case
2639 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002640
Jim Ingham4cda6e02011-10-07 22:23:45 +00002641 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00002642
2643 const uint32_t original_size = sc_list.GetSize();
2644
Jim Ingham4cda6e02011-10-07 22:23:45 +00002645 DWARFDebugInfo* info = DebugInfo();
2646 if (info == NULL)
2647 return 0;
2648
Greg Clayton43fe2172013-04-03 02:00:15 +00002649 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00002650 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002651 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002652 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002653 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002654
2655 DIEArray die_offsets;
2656
2657 uint32_t num_matches = 0;
2658
Greg Clayton43fe2172013-04-03 02:00:15 +00002659 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002660 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002661 // If they asked for the full name, match what they typed. At some point we may
2662 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2663 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002664 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002665 for (uint32_t i = 0; i < num_matches; i++)
2666 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002667 const DIERef& die_ref = die_offsets[i];
2668 DWARFDIE die = info->GetDIE (die_ref);
Greg Claytonaa044962011-10-13 00:59:38 +00002669 if (die)
2670 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002671 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002672 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002673
Greg Clayton6071e6f2015-08-26 22:57:51 +00002674 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002675 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002676 if (ResolveFunction (die, include_inlines, sc_list))
2677 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002678 }
Greg Claytonaa044962011-10-13 00:59:38 +00002679 }
Greg Clayton95d87902011-11-11 03:16:25 +00002680 else
2681 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002682 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 +00002683 die_ref.die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002684 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002685 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002686 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002687
2688 if (name_type_mask & eFunctionNameTypeSelector)
2689 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002690 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00002691 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00002692
Greg Clayton43fe2172013-04-03 02:00:15 +00002693 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2694 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2695 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00002696
Greg Clayton43fe2172013-04-03 02:00:15 +00002697 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00002698 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002699 const DIERef& die_ref = die_offsets[i];
2700 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002701 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00002702 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002703 const char *die_name = die.GetName();
Jim Inghamaa816b82015-09-02 01:59:14 +00002704 if (ObjCLanguage::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00002705 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002706 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002707 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002708 if (ResolveFunction (die, include_inlines, sc_list))
2709 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002710 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002711 }
2712 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002713 else
2714 {
2715 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 +00002716 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002717 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002718 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002719 die_offsets.clear();
2720 }
2721
Greg Clayton99558cc42015-08-24 23:46:31 +00002722 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) || name_type_mask & eFunctionNameTypeBase)
Greg Clayton43fe2172013-04-03 02:00:15 +00002723 {
2724 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2725 // extract the base name, look that up, and if there is any other information in the name we were
2726 // passed in we have to post-filter based on that.
2727
2728 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2729 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2730
2731 for (uint32_t i = 0; i < num_matches; i++)
2732 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002733 const DIERef& die_ref = die_offsets[i];
2734 DWARFDIE die = info->GetDIE (die_ref);
Greg Clayton43fe2172013-04-03 02:00:15 +00002735 if (die)
2736 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002737 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002738 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002739
Greg Clayton43fe2172013-04-03 02:00:15 +00002740
2741 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002742 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() && ResolveFunction (die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002743 {
2744 bool keep_die = true;
2745 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
2746 {
2747 // We are looking for either basenames or methods, so we need to
2748 // trim out the ones we won't want by looking at the type
2749 SymbolContext sc;
2750 if (sc_list.GetLastContext(sc))
2751 {
2752 if (sc.block)
2753 {
2754 // We have an inlined function
2755 }
2756 else if (sc.function)
2757 {
2758 Type *type = sc.function->GetType();
2759
Sean Callananc370a8a2013-09-18 22:59:55 +00002760 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00002761 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002762 CompilerDeclContext decl_ctx = GetDeclContextContainingUID (type->GetID());
2763 if (decl_ctx.IsStructUnionOrClass())
Greg Clayton43fe2172013-04-03 02:00:15 +00002764 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002765 if (name_type_mask & eFunctionNameTypeBase)
2766 {
2767 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2768 keep_die = false;
2769 }
2770 }
2771 else
2772 {
2773 if (name_type_mask & eFunctionNameTypeMethod)
2774 {
2775 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2776 keep_die = false;
2777 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002778 }
2779 }
2780 else
2781 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002782 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002783 die_ref.die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002784 }
2785 }
2786 }
2787 }
2788 if (keep_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002789 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002790 }
2791 }
2792 else
2793 {
2794 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 +00002795 die_ref.die_offset, name_cstr);
Greg Clayton43fe2172013-04-03 02:00:15 +00002796 }
2797 }
2798 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002799 }
2800 }
Greg Clayton7f995132011-10-04 22:41:51 +00002801 }
2802 else
2803 {
2804
2805 // Index the DWARF if we haven't already
2806 if (!m_indexed)
2807 Index ();
2808
Greg Clayton7f995132011-10-04 22:41:51 +00002809 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00002810 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002811 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002812
Ed Mastefc7baa02013-09-09 18:00:45 +00002813 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00002814 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00002815 // If we didn't find any functions in the global namespace try
2816 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00002817 // functions that have a namespace but keep functions which
2818 // have an anonymous namespace
2819 // TODO: The arch in the object file isn't correct for MSVC
2820 // binaries on windows, we should find a way to make it
2821 // correct and handle those symbols as well.
Aidan Doddsc78e8992015-11-10 14:10:57 +00002822 if (sc_list.GetSize() == original_size)
Matt Kopecd6089962013-05-10 17:53:48 +00002823 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002824 ArchSpec arch;
Greg Clayton99558cc42015-08-24 23:46:31 +00002825 if (!parent_decl_ctx &&
Robert Flackeb83fab2015-05-15 18:59:59 +00002826 GetObjectFile()->GetArchitecture(arch) &&
2827 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
2828 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00002829 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002830 SymbolContextList temp_sc_list;
2831 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00002832 SymbolContext sc;
2833 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
2834 {
2835 if (temp_sc_list.GetContextAtIndex(i, sc))
2836 {
Matt Kopeca189d492013-05-10 22:55:24 +00002837 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
2838 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00002839 // Mangled names on Linux and FreeBSD are of the form:
2840 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00002841 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2842 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00002843 {
2844 sc_list.Append(sc);
2845 }
2846 }
2847 }
2848 }
2849 }
Matt Kopecd6089962013-05-10 17:53:48 +00002850 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002851 DIEArray die_offsets;
Greg Clayton43fe2172013-04-03 02:00:15 +00002852 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002853 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002854 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002855 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002856 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002857 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002858 if (die)
2859 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002860 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002861 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002862
Greg Claytonaa044962011-10-13 00:59:38 +00002863 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002864 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002865 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002866 if (ResolveFunction (die, include_inlines, sc_list))
2867 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002868 }
Greg Claytonaa044962011-10-13 00:59:38 +00002869 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002870 }
2871 die_offsets.clear();
2872 }
2873
Greg Clayton43fe2172013-04-03 02:00:15 +00002874 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002875 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002876 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Sean Callanan213fdb82011-10-13 01:49:10 +00002877 return 0; // no methods in namespaces
2878
Greg Clayton43fe2172013-04-03 02:00:15 +00002879 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002880 {
Greg Claytonaa044962011-10-13 00:59:38 +00002881 for (uint32_t i = 0; i < num_base; i++)
2882 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002883 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002884 if (die)
2885 {
Greg Claytonaa044962011-10-13 00:59:38 +00002886 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002887 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002888 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002889 if (ResolveFunction (die, include_inlines, sc_list))
2890 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002891 }
Greg Claytonaa044962011-10-13 00:59:38 +00002892 }
2893 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002894 }
2895 die_offsets.clear();
2896 }
Greg Clayton7f995132011-10-04 22:41:51 +00002897
Greg Clayton99558cc42015-08-24 23:46:31 +00002898 if ((name_type_mask & eFunctionNameTypeSelector) && (!parent_decl_ctx || !parent_decl_ctx->IsValid()))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002899 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002900 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002901 }
2902
Greg Clayton4d01ace2011-09-29 16:58:15 +00002903 }
2904
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002905 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002906 const uint32_t num_matches = sc_list.GetSize() - original_size;
2907
2908 if (log && num_matches > 0)
2909 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002910 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00002911 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002912 name.GetCString(),
2913 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00002914 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00002915 append,
2916 num_matches);
2917 }
2918 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002919}
2920
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002921uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00002922SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923{
2924 Timer scoped_timer (__PRETTY_FUNCTION__,
2925 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2926 regex.GetText());
2927
Greg Clayton5160ce52013-03-27 23:08:40 +00002928 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002929
2930 if (log)
2931 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002932 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002933 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2934 regex.GetText(),
2935 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002936 }
2937
2938
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002939 // If we aren't appending the results to this list, then clear the list
2940 if (!append)
2941 sc_list.Clear();
2942
2943 // Remember how many sc_list are in the list before we search in case
2944 // we are appending the results to a variable list.
2945 uint32_t original_size = sc_list.GetSize();
2946
Greg Clayton97fbc342011-10-20 22:30:33 +00002947 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002948 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002949 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00002950 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002951 }
2952 else
2953 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002954 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002955 if (!m_indexed)
2956 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002957
Pavel Labatha73d6572015-03-13 10:22:00 +00002958 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002959
Pavel Labatha73d6572015-03-13 10:22:00 +00002960 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002961 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002962
2963 // Return the number of variable that were appended to the list
2964 return sc_list.GetSize() - original_size;
2965}
Jim Ingham318c9f22011-08-26 19:44:13 +00002966
Siva Chandra9293fc42016-01-07 23:32:34 +00002967void
2968SymbolFileDWARF::GetMangledNamesForFunction (const std::string &scope_qualified_name,
2969 std::vector<ConstString> &mangled_names)
2970{
2971 DWARFDebugInfo* info = DebugInfo();
2972 uint32_t num_comp_units = 0;
2973 if (info)
2974 num_comp_units = info->GetNumCompileUnits();
2975
2976 for (uint32_t i = 0; i < num_comp_units; i++)
2977 {
2978 DWARFCompileUnit *cu = info->GetCompileUnitAtIndex(i);
2979 if (cu == nullptr)
2980 continue;
2981
2982 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2983 if (dwo)
2984 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2985 }
2986
2987 NameToOffsetMap::iterator iter = m_function_scope_qualified_name_map.find(scope_qualified_name);
2988 if (iter == m_function_scope_qualified_name_map.end())
2989 return;
2990
2991 DIERefSetSP set_sp = (*iter).second;
2992 std::set<DIERef>::iterator set_iter;
2993 for (set_iter = set_sp->begin(); set_iter != set_sp->end(); set_iter++)
2994 {
2995 DWARFDIE die = DebugInfo()->GetDIE (*set_iter);
2996 mangled_names.push_back(ConstString(die.GetMangledName()));
2997 }
2998}
2999
3000
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003002SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3003 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00003004 const CompilerDeclContext *parent_decl_ctx,
Greg Claytond1767f02011-12-08 02:13:16 +00003005 bool append,
Greg Claytonae088e52016-02-10 21:28:13 +00003006 uint32_t max_matches,
3007 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
Ravitheja Addepally40697302015-10-08 09:45:41 +00003008 TypeMap& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003009{
Greg Claytonae088e52016-02-10 21:28:13 +00003010 // If we aren't appending the results to this list, then clear the list
3011 if (!append)
3012 types.Clear();
3013
3014 // Make sure we haven't already searched this SymbolFile before...
3015 if (searched_symbol_files.count(this))
3016 return 0;
3017 else
3018 searched_symbol_files.insert(this);
3019
Greg Claytonc685f8e2010-09-15 04:15:46 +00003020 DWARFDebugInfo* info = DebugInfo();
3021 if (info == NULL)
3022 return 0;
3023
Greg Clayton5160ce52013-03-27 23:08:40 +00003024 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003025
Greg Clayton21f2a492011-10-06 00:09:08 +00003026 if (log)
3027 {
Greg Clayton99558cc42015-08-24 23:46:31 +00003028 if (parent_decl_ctx)
Greg Clayton5160ce52013-03-27 23:08:40 +00003029 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003030 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list)",
Greg Clayton437a1352012-04-09 22:43:43 +00003031 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003032 static_cast<const void*>(parent_decl_ctx),
3033 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003034 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00003035 else
Greg Clayton5160ce52013-03-27 23:08:40 +00003036 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003037 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003038 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00003039 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003040 }
3041
Greg Clayton99558cc42015-08-24 23:46:31 +00003042 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00003043 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044
Greg Claytond4a2b372011-09-12 23:21:58 +00003045 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003046
Greg Clayton97fbc342011-10-20 22:30:33 +00003047 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003048 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003049 if (m_apple_types_ap.get())
3050 {
3051 const char *name_cstr = name.GetCString();
3052 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3053 }
Greg Clayton7f995132011-10-04 22:41:51 +00003054 }
3055 else
3056 {
3057 if (!m_indexed)
3058 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003059
Greg Clayton7f995132011-10-04 22:41:51 +00003060 m_type_index.Find (name, die_offsets);
3061 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003062
Greg Clayton437a1352012-04-09 22:43:43 +00003063 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003064
Greg Clayton437a1352012-04-09 22:43:43 +00003065 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 {
Greg Clayton7f995132011-10-04 22:41:51 +00003067 const uint32_t initial_types_size = types.GetSize();
Greg Claytond4a2b372011-09-12 23:21:58 +00003068 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003069 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003070 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003071 const DIERef& die_ref = die_offsets[i];
3072 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Claytond4a2b372011-09-12 23:21:58 +00003073
Greg Clayton95d87902011-11-11 03:16:25 +00003074 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003075 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003076 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00003077 continue; // The containing decl contexts don't match
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003078
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003079 Type *matching_type = ResolveType (die, true, true);
Greg Clayton95d87902011-11-11 03:16:25 +00003080 if (matching_type)
3081 {
3082 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003083 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003084 if (types.GetSize() >= max_matches)
3085 break;
3086 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003087 }
Greg Clayton95d87902011-11-11 03:16:25 +00003088 else
3089 {
3090 if (m_using_apple_tables)
3091 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003092 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 +00003093 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003094 }
3095 }
3096
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003097 }
Greg Clayton437a1352012-04-09 22:43:43 +00003098 const uint32_t num_matches = types.GetSize() - initial_types_size;
3099 if (log && num_matches)
3100 {
Greg Clayton99558cc42015-08-24 23:46:31 +00003101 if (parent_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00003102 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003103 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003104 "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 +00003105 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003106 static_cast<const void*>(parent_decl_ctx),
3107 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003108 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003109 num_matches);
3110 }
3111 else
3112 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003113 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003114 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00003115 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003116 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003117 num_matches);
3118 }
3119 }
3120 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003121 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00003122 else
3123 {
3124 UpdateExternalModuleListIfNeeded();
3125
3126 for (const auto &pair : m_external_type_modules)
3127 {
3128 ModuleSP external_module_sp = pair.second;
3129 if (external_module_sp)
3130 {
3131 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor();
3132 if (sym_vendor)
3133 {
3134 const uint32_t num_external_matches = sym_vendor->FindTypes (sc,
3135 name,
3136 parent_decl_ctx,
3137 append,
3138 max_matches,
Greg Claytonae088e52016-02-10 21:28:13 +00003139 searched_symbol_files,
Greg Claytone6b36cd2015-12-08 01:02:08 +00003140 types);
3141 if (num_external_matches)
3142 return num_external_matches;
3143 }
3144 }
3145 }
3146 }
3147
3148 return 0;
3149}
3150
3151
3152size_t
3153SymbolFileDWARF::FindTypes (const std::vector<CompilerContext> &context,
3154 bool append,
3155 TypeMap& types)
3156{
3157 if (!append)
3158 types.Clear();
3159
3160 if (context.empty())
3161 return 0;
3162
3163 DIEArray die_offsets;
3164
3165 ConstString name = context.back().name;
3166
Sean Callanan9b0cfe22016-01-14 18:59:49 +00003167 if (!name)
3168 return 0;
3169
Greg Claytone6b36cd2015-12-08 01:02:08 +00003170 if (m_using_apple_tables)
3171 {
3172 if (m_apple_types_ap.get())
3173 {
3174 const char *name_cstr = name.GetCString();
3175 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3176 }
3177 }
3178 else
3179 {
3180 if (!m_indexed)
3181 Index ();
3182
3183 m_type_index.Find (name, die_offsets);
3184 }
3185
3186 const size_t num_die_matches = die_offsets.size();
3187
3188 if (num_die_matches)
3189 {
3190 size_t num_matches = 0;
3191 DWARFDebugInfo* debug_info = DebugInfo();
3192 for (size_t i=0; i<num_die_matches; ++i)
3193 {
3194 const DIERef& die_ref = die_offsets[i];
3195 DWARFDIE die = debug_info->GetDIE (die_ref);
3196
3197 if (die)
3198 {
3199 std::vector<CompilerContext> die_context;
3200 die.GetDWOContext(die_context);
3201 if (die_context != context)
3202 continue;
3203
3204 Type *matching_type = ResolveType (die, true, true);
3205 if (matching_type)
3206 {
3207 // We found a type pointer, now find the shared pointer form our type list
3208 types.InsertUnique (matching_type->shared_from_this());
3209 ++num_matches;
3210 }
3211 }
3212 else
3213 {
3214 if (m_using_apple_tables)
3215 {
3216 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3217 die_ref.die_offset, name.GetCString());
3218 }
3219 }
3220
3221 }
3222 return num_matches;
3223 }
Greg Clayton7f995132011-10-04 22:41:51 +00003224 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003225}
3226
3227
Greg Clayton99558cc42015-08-24 23:46:31 +00003228CompilerDeclContext
Greg Clayton96d7d742010-11-10 23:42:09 +00003229SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003230 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00003231 const CompilerDeclContext *parent_decl_ctx)
Greg Clayton96d7d742010-11-10 23:42:09 +00003232{
Greg Clayton5160ce52013-03-27 23:08:40 +00003233 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003234
3235 if (log)
3236 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003237 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003238 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3239 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003240 }
3241
Greg Clayton99558cc42015-08-24 23:46:31 +00003242 CompilerDeclContext namespace_decl_ctx;
3243
3244 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
3245 return namespace_decl_ctx;
3246
3247
Greg Clayton96d7d742010-11-10 23:42:09 +00003248 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003249 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003250 {
Greg Clayton7f995132011-10-04 22:41:51 +00003251 DIEArray die_offsets;
3252
Greg Clayton526e5af2010-11-13 03:52:47 +00003253 // Index if we already haven't to make sure the compile units
3254 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003255 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003256 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003257 if (m_apple_namespaces_ap.get())
3258 {
3259 const char *name_cstr = name.GetCString();
3260 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3261 }
Greg Clayton7f995132011-10-04 22:41:51 +00003262 }
3263 else
3264 {
3265 if (!m_indexed)
3266 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003267
Greg Clayton7f995132011-10-04 22:41:51 +00003268 m_namespace_index.Find (name, die_offsets);
3269 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003270
Greg Clayton7f995132011-10-04 22:41:51 +00003271 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003272 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003273 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003274 DWARFDebugInfo* debug_info = DebugInfo();
3275 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003276 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003277 const DIERef& die_ref = die_offsets[i];
3278 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003279
Greg Clayton95d87902011-11-11 03:16:25 +00003280 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003281 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003282 if (!DIEInDeclContext (parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00003283 continue; // The containing decl contexts don't match
Greg Clayton95d87902011-11-11 03:16:25 +00003284
Greg Clayton261ac3f2015-08-28 01:01:03 +00003285 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
3286 if (dwarf_ast)
Greg Clayton8b4edba2015-08-14 20:02:05 +00003287 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003288 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00003289 if (namespace_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00003290 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003291 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003292 }
Greg Clayton95d87902011-11-11 03:16:25 +00003293 else
3294 {
3295 if (m_using_apple_tables)
3296 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003297 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 +00003298 die_ref.die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003299 }
3300 }
3301
Greg Clayton526e5af2010-11-13 03:52:47 +00003302 }
3303 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003304 }
Greg Clayton99558cc42015-08-24 23:46:31 +00003305 if (log && namespace_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00003306 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003307 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00003308 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => CompilerDeclContext(%p/%p) \"%s\"",
Greg Clayton437a1352012-04-09 22:43:43 +00003309 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00003310 static_cast<const void*>(namespace_decl_ctx.GetTypeSystem()),
3311 static_cast<const void*>(namespace_decl_ctx.GetOpaqueDeclContext()),
3312 namespace_decl_ctx.GetName().AsCString("<NULL>"));
Greg Clayton437a1352012-04-09 22:43:43 +00003313 }
3314
Greg Clayton99558cc42015-08-24 23:46:31 +00003315 return namespace_decl_ctx;
Greg Clayton96d7d742010-11-10 23:42:09 +00003316}
3317
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003318TypeSP
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003319SymbolFileDWARF::GetTypeForDIE (const DWARFDIE &die, bool resolve_function_context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003320{
3321 TypeSP type_sp;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003322 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003323 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003324 Type *type_ptr = GetDIEToType().lookup (die.GetDIE());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003325 if (type_ptr == NULL)
3326 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003327 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
Greg Claytonca512b32011-01-14 04:54:56 +00003328 assert (lldb_cu);
3329 SymbolContext sc(lldb_cu);
Ravitheja Addepally40697302015-10-08 09:45:41 +00003330 const DWARFDebugInfoEntry* parent_die = die.GetParent().GetDIE();
3331 while (parent_die != nullptr)
3332 {
3333 if (parent_die->Tag() == DW_TAG_subprogram)
3334 break;
3335 parent_die = parent_die->GetParent();
3336 }
3337 SymbolContext sc_backup = sc;
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003338 if (resolve_function_context && parent_die != nullptr && !GetFunction(DWARFDIE(die.GetCU(),parent_die), sc))
Ravitheja Addepally40697302015-10-08 09:45:41 +00003339 sc = sc_backup;
3340
Greg Clayton6071e6f2015-08-26 22:57:51 +00003341 type_sp = ParseType(sc, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003342 }
3343 else if (type_ptr != DIE_IS_BEING_PARSED)
3344 {
3345 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003346 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003347 }
3348
3349 }
3350 return type_sp;
3351}
3352
Greg Clayton2bc22f82011-09-30 03:20:47 +00003353
Greg Clayton6071e6f2015-08-26 22:57:51 +00003354DWARFDIE
3355SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFDIE &orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003356{
Greg Clayton6071e6f2015-08-26 22:57:51 +00003357 if (orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003358 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003359 DWARFDIE die = orig_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003360
Greg Clayton6071e6f2015-08-26 22:57:51 +00003361 while (die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003362 {
3363 // If this is the original DIE that we are searching for a declaration
3364 // for, then don't look in the cache as we don't want our own decl
3365 // context to be our decl context...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003366 if (orig_die != die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003367 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003368 switch (die.Tag())
Greg Clayton2bc22f82011-09-30 03:20:47 +00003369 {
3370 case DW_TAG_compile_unit:
3371 case DW_TAG_namespace:
3372 case DW_TAG_structure_type:
3373 case DW_TAG_union_type:
3374 case DW_TAG_class_type:
Paul Hermand628cbb2015-09-15 23:44:17 +00003375 case DW_TAG_lexical_block:
3376 case DW_TAG_subprogram:
Greg Clayton2bc22f82011-09-30 03:20:47 +00003377 return die;
3378
3379 default:
3380 break;
3381 }
3382 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003383
3384 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3385 if (spec_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003386 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003387 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3388 if (decl_ctx_die)
3389 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003390 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003391
3392 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3393 if (abs_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003394 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003395 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3396 if (decl_ctx_die)
3397 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003398 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003399
3400 die = die.GetParent();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003401 }
3402 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003403 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003404}
3405
3406
Greg Clayton901c5ca2011-12-03 04:40:03 +00003407Symbol *
3408SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3409{
3410 Symbol *objc_class_symbol = NULL;
3411 if (m_obj_file)
3412 {
Greg Clayton3046e662013-07-10 01:23:25 +00003413 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003414 if (symtab)
3415 {
3416 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3417 eSymbolTypeObjCClass,
3418 Symtab::eDebugNo,
3419 Symtab::eVisibilityAny);
3420 }
3421 }
3422 return objc_class_symbol;
3423}
3424
Greg Claytonc7f03b62012-01-12 04:33:28 +00003425// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3426// then we can end up looking through all class types for a complete type and never find
3427// the full definition. We need to know if this attribute is supported, so we determine
3428// this here and cache th result. We also need to worry about the debug map DWARF file
3429// if we are doing darwin DWARF in .o file debugging.
3430bool
3431SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3432{
3433 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3434 {
3435 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3436 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3437 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3438 else
3439 {
3440 DWARFDebugInfo* debug_info = DebugInfo();
3441 const uint32_t num_compile_units = GetNumCompileUnits();
3442 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3443 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003444 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3445 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003446 {
3447 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3448 break;
3449 }
3450 }
3451 }
Greg Clayton1f746072012-08-29 21:13:06 +00003452 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003453 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3454 }
3455 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3456}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003457
3458// This function can be used when a DIE is found that is a forward declaration
3459// DIE and we want to try and find a type that has the complete definition.
3460TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003461SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
Greg Claytonc7f03b62012-01-12 04:33:28 +00003462 const ConstString &type_name,
3463 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003464{
3465
3466 TypeSP type_sp;
3467
Greg Claytonc7f03b62012-01-12 04:33:28 +00003468 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003469 return type_sp;
3470
3471 DIEArray die_offsets;
3472
3473 if (m_using_apple_tables)
3474 {
3475 if (m_apple_types_ap.get())
3476 {
3477 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003478 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003479 }
3480 }
3481 else
3482 {
3483 if (!m_indexed)
3484 Index ();
3485
3486 m_type_index.Find (type_name, die_offsets);
3487 }
3488
Greg Clayton901c5ca2011-12-03 04:40:03 +00003489 const size_t num_matches = die_offsets.size();
3490
Greg Clayton901c5ca2011-12-03 04:40:03 +00003491 if (num_matches)
3492 {
3493 DWARFDebugInfo* debug_info = DebugInfo();
3494 for (size_t i=0; i<num_matches; ++i)
3495 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003496 const DIERef& die_ref = die_offsets[i];
3497 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003498
3499 if (type_die)
3500 {
3501 bool try_resolving_type = false;
3502
3503 // Don't try and resolve the DIE we are looking for with the DIE itself!
3504 if (type_die != die)
3505 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003506 switch (type_die.Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003507 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003508 case DW_TAG_class_type:
3509 case DW_TAG_structure_type:
3510 try_resolving_type = true;
3511 break;
3512 default:
3513 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003514 }
3515 }
3516
3517 if (try_resolving_type)
3518 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003519 if (must_be_implementation && type_die.Supports_DW_AT_APPLE_objc_complete_type())
3520 try_resolving_type = type_die.GetAttributeValueAsUnsigned (DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003521
3522 if (try_resolving_type)
3523 {
Ravitheja Addepally46bcbaa2015-11-03 14:24:24 +00003524 Type *resolved_type = ResolveType (type_die, false, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003525 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3526 {
Ed Mastea0191d12013-10-17 20:42:56 +00003527 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 +00003528 die.GetID(),
Jim Ingham4af59612014-12-19 19:20:44 +00003529 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003530 type_die.GetID(),
3531 type_cu->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003532
Greg Claytonc7f03b62012-01-12 04:33:28 +00003533 if (die)
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003534 GetDIEToType()[die.GetDIE()] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00003535 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003536 break;
3537 }
3538 }
3539 }
3540 }
3541 else
3542 {
3543 if (m_using_apple_tables)
3544 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003545 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 +00003546 die_ref.die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003547 }
3548 }
3549
3550 }
3551 }
3552 return type_sp;
3553}
3554
Greg Claytona8022fa2012-04-24 21:22:41 +00003555
Greg Clayton80c26302012-02-05 06:12:47 +00003556//----------------------------------------------------------------------
3557// This function helps to ensure that the declaration contexts match for
3558// two different DIEs. Often times debug information will refer to a
3559// forward declaration of a type (the equivalent of "struct my_struct;".
3560// There will often be a declaration of that type elsewhere that has the
3561// full definition. When we go looking for the full type "my_struct", we
3562// will find one or more matches in the accelerator tables and we will
3563// then need to make sure the type was in the same declaration context
3564// as the original DIE. This function can efficiently compare two DIEs
3565// and will return true when the declaration context matches, and false
3566// when they don't.
3567//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00003568bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00003569SymbolFileDWARF::DIEDeclContextsMatch (const DWARFDIE &die1,
3570 const DWARFDIE &die2)
Greg Clayton890ff562012-02-02 05:48:16 +00003571{
Greg Claytona8022fa2012-04-24 21:22:41 +00003572 if (die1 == die2)
3573 return true;
3574
Greg Clayton890ff562012-02-02 05:48:16 +00003575 DWARFDIECollection decl_ctx_1;
3576 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00003577 //The declaration DIE stack is a stack of the declaration context
3578 // DIEs all the way back to the compile unit. If a type "T" is
3579 // declared inside a class "B", and class "B" is declared inside
3580 // a class "A" and class "A" is in a namespace "lldb", and the
3581 // namespace is in a compile unit, there will be a stack of DIEs:
3582 //
3583 // [0] DW_TAG_class_type for "B"
3584 // [1] DW_TAG_class_type for "A"
3585 // [2] DW_TAG_namespace for "lldb"
3586 // [3] DW_TAG_compile_unit for the source file.
3587 //
3588 // We grab both contexts and make sure that everything matches
3589 // all the way back to the compiler unit.
3590
3591 // First lets grab the decl contexts for both DIEs
Greg Clayton6071e6f2015-08-26 22:57:51 +00003592 die1.GetDeclContextDIEs (decl_ctx_1);
3593 die2.GetDeclContextDIEs (decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00003594 // Make sure the context arrays have the same size, otherwise
3595 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00003596 const size_t count1 = decl_ctx_1.Size();
3597 const size_t count2 = decl_ctx_2.Size();
3598 if (count1 != count2)
3599 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00003600
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003601 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00003602 // compile unit. If they don't, then we are done.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003603 DWARFDIE decl_ctx_die1;
3604 DWARFDIE decl_ctx_die2;
Greg Clayton890ff562012-02-02 05:48:16 +00003605 size_t i;
3606 for (i=0; i<count1; i++)
3607 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003608 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3609 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3610 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
Greg Clayton890ff562012-02-02 05:48:16 +00003611 return false;
3612 }
Greg Clayton890ff562012-02-02 05:48:16 +00003613#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003614
3615 // Make sure the top item in the decl context die array is always
3616 // DW_TAG_compile_unit. If it isn't then something went wrong in
Greg Clayton5ce1a842015-08-27 18:09:44 +00003617 // the DWARFDIE::GetDeclContextDIEs() function...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003618 assert (decl_ctx_1.GetDIEAtIndex (count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003619
Greg Clayton890ff562012-02-02 05:48:16 +00003620#endif
3621 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00003622 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00003623 for (i=0; i<count1 - 1; i++)
3624 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003625 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3626 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3627 const char *name1 = decl_ctx_die1.GetName();
3628 const char *name2 = decl_ctx_die2.GetName();
Greg Clayton890ff562012-02-02 05:48:16 +00003629 // If the string was from a DW_FORM_strp, then the pointer will often
3630 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00003631 if (name1 == name2)
3632 continue;
3633
3634 // Name pointers are not equal, so only compare the strings
3635 // if both are not NULL.
3636 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00003637 {
Greg Clayton5569e642012-02-06 01:44:54 +00003638 // If the strings don't compare, we are done...
3639 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00003640 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00003641 }
3642 else
3643 {
3644 // One name was NULL while the other wasn't
3645 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003646 }
3647 }
Greg Clayton80c26302012-02-05 06:12:47 +00003648 // We made it through all of the checks and the declaration contexts
3649 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00003650 return true;
3651}
Greg Clayton220a0072011-12-09 08:48:30 +00003652
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003653
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003654TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00003655SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
3656{
3657 TypeSP type_sp;
3658
3659 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3660 if (dwarf_decl_ctx_count > 0)
3661 {
3662 const ConstString type_name(dwarf_decl_ctx[0].name);
3663 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
3664
3665 if (type_name)
3666 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003667 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00003668 if (log)
3669 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003670 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003671 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
3672 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3673 dwarf_decl_ctx.GetQualifiedName());
3674 }
3675
3676 DIEArray die_offsets;
3677
3678 if (m_using_apple_tables)
3679 {
3680 if (m_apple_types_ap.get())
3681 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003682 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
3683 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
3684 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00003685 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003686 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3687 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
3688 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003689 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003690 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3691 }
3692 else if (has_tag)
3693 {
3694 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003695 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00003696 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
3697 }
3698 else
3699 {
3700 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3701 }
3702 }
3703 }
3704 else
3705 {
3706 if (!m_indexed)
3707 Index ();
3708
3709 m_type_index.Find (type_name, die_offsets);
3710 }
3711
3712 const size_t num_matches = die_offsets.size();
3713
3714
Greg Claytona8022fa2012-04-24 21:22:41 +00003715 if (num_matches)
3716 {
3717 DWARFDebugInfo* debug_info = DebugInfo();
3718 for (size_t i=0; i<num_matches; ++i)
3719 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003720 const DIERef& die_ref = die_offsets[i];
3721 DWARFDIE type_die = debug_info->GetDIE (die_ref);
Greg Claytona8022fa2012-04-24 21:22:41 +00003722
3723 if (type_die)
3724 {
3725 bool try_resolving_type = false;
3726
3727 // Don't try and resolve the DIE we are looking for with the DIE itself!
Greg Clayton6071e6f2015-08-26 22:57:51 +00003728 const dw_tag_t type_tag = type_die.Tag();
Greg Claytona8022fa2012-04-24 21:22:41 +00003729 // Make sure the tags match
3730 if (type_tag == tag)
3731 {
3732 // The tags match, lets try resolving this type
3733 try_resolving_type = true;
3734 }
3735 else
3736 {
3737 // The tags don't match, but we need to watch our for a
3738 // forward declaration for a struct and ("struct foo")
3739 // ends up being a class ("class foo { ... };") or
3740 // vice versa.
3741 switch (type_tag)
3742 {
3743 case DW_TAG_class_type:
3744 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3745 try_resolving_type = (tag == DW_TAG_structure_type);
3746 break;
3747 case DW_TAG_structure_type:
3748 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3749 try_resolving_type = (tag == DW_TAG_class_type);
3750 break;
3751 default:
3752 // Tags don't match, don't event try to resolve
3753 // using this type whose name matches....
3754 break;
3755 }
3756 }
3757
3758 if (try_resolving_type)
3759 {
3760 DWARFDeclContext type_dwarf_decl_ctx;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003761 type_die.GetDWARFDeclContext (type_dwarf_decl_ctx);
Greg Claytona8022fa2012-04-24 21:22:41 +00003762
3763 if (log)
3764 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003765 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003766 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
3767 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3768 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003769 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003770 type_dwarf_decl_ctx.GetQualifiedName());
3771 }
3772
3773 // Make sure the decl contexts match all the way up
3774 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
3775 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003776 Type *resolved_type = ResolveType (type_die, false);
Greg Claytona8022fa2012-04-24 21:22:41 +00003777 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3778 {
3779 type_sp = resolved_type->shared_from_this();
3780 break;
3781 }
3782 }
3783 }
3784 else
3785 {
3786 if (log)
3787 {
3788 std::string qualified_name;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003789 type_die.GetQualifiedName(qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00003790 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003791 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
3792 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3793 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003794 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003795 qualified_name.c_str());
3796 }
3797 }
3798 }
3799 else
3800 {
3801 if (m_using_apple_tables)
3802 {
3803 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 +00003804 die_ref.die_offset, type_name.GetCString());
Greg Claytona8022fa2012-04-24 21:22:41 +00003805 }
3806 }
3807
3808 }
3809 }
3810 }
3811 }
3812 return type_sp;
3813}
3814
Greg Claytona8022fa2012-04-24 21:22:41 +00003815TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003816SymbolFileDWARF::ParseType (const SymbolContext& sc, const DWARFDIE &die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003817{
Greg Clayton196e8cd2015-08-17 20:31:46 +00003818 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003819
Greg Clayton6071e6f2015-08-26 22:57:51 +00003820 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003822 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
3823
3824 if (type_system)
Greg Clayton196e8cd2015-08-17 20:31:46 +00003825 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003826 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3827 if (dwarf_ast)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003828 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003829 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3830 type_sp = dwarf_ast->ParseTypeFromDWARF (sc, die, log, type_is_new_ptr);
3831 if (type_sp)
3832 {
3833 TypeList* type_list = GetTypeList();
3834 if (type_list)
3835 type_list->Insert(type_sp);
Siva Chandra9293fc42016-01-07 23:32:34 +00003836
3837 if (die.Tag() == DW_TAG_subprogram)
3838 {
3839 DIERef die_ref = die.GetDIERef();
3840 std::string scope_qualified_name(GetDeclContextForUID(die.GetID()).GetScopeQualifiedName().AsCString(""));
3841 if (scope_qualified_name.size())
3842 {
3843 NameToOffsetMap::iterator iter = m_function_scope_qualified_name_map.find(scope_qualified_name);
3844 if (iter != m_function_scope_qualified_name_map.end())
3845 (*iter).second->insert(die_ref);
3846 else
3847 {
3848 DIERefSetSP new_set(new std::set<DIERef>);
3849 new_set->insert(die_ref);
3850 m_function_scope_qualified_name_map.emplace(std::make_pair(scope_qualified_name, new_set));
3851 }
3852 }
3853 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00003854 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003855 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003856 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003857 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003858
3859 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003860}
3861
3862size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003863SymbolFileDWARF::ParseTypes
3864(
3865 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003866 const DWARFDIE &orig_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003867 bool parse_siblings,
3868 bool parse_children
3869)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003870{
3871 size_t types_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003872 DWARFDIE die = orig_die;
3873 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003874 {
3875 bool type_is_new = false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003876 if (ParseType(sc, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003877 {
3878 if (type_is_new)
3879 ++types_added;
3880 }
3881
Greg Clayton6071e6f2015-08-26 22:57:51 +00003882 if (parse_children && die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003883 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003884 if (die.Tag() == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003885 {
3886 SymbolContext child_sc(sc);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003887 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3888 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003889 }
3890 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003891 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 }
3893
3894 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003895 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003896 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003897 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003898 }
3899 return types_added;
3900}
3901
3902
3903size_t
3904SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3905{
3906 assert(sc.comp_unit && sc.function);
3907 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003908 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003909 if (dwarf_cu)
3910 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003911 const dw_offset_t function_die_offset = sc.function->GetID();
3912 DWARFDIE function_die = dwarf_cu->GetDIE (function_die_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003913 if (function_die)
3914 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003915 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003916 }
3917 }
3918
3919 return functions_added;
3920}
3921
3922
3923size_t
3924SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3925{
3926 // At least a compile unit must be valid
3927 assert(sc.comp_unit);
3928 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003929 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003930 if (dwarf_cu)
3931 {
3932 if (sc.function)
3933 {
3934 dw_offset_t function_die_offset = sc.function->GetID();
Greg Clayton6071e6f2015-08-26 22:57:51 +00003935 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3936 if (func_die && func_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003937 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003938 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 }
3940 }
3941 else
3942 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003943 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3944 if (dwarf_cu_die && dwarf_cu_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003945 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003946 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 }
3948 }
3949 }
3950
3951 return types_added;
3952}
3953
3954size_t
3955SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3956{
3957 if (sc.comp_unit != NULL)
3958 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003959 DWARFDebugInfo* info = DebugInfo();
3960 if (info == NULL)
3961 return 0;
3962
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963 if (sc.function)
3964 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003965 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID()));
Greg Clayton9422dd62013-03-04 21:46:16 +00003966
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003967 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress (DW_AT_low_pc, LLDB_INVALID_ADDRESS);
Greg Claytonc7bece562013-01-25 18:06:21 +00003968 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003969 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003970 const size_t num_variables = ParseVariables(sc, function_die.GetFirstChild(), func_lo_pc, true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00003971
Greg Claytone38a5ed2012-01-05 03:57:59 +00003972 // Let all blocks know they have parse all their variables
3973 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3974 return num_variables;
3975 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003976 }
3977 else if (sc.comp_unit)
3978 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003979 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Greg Clayton9422dd62013-03-04 21:46:16 +00003980
3981 if (dwarf_cu == NULL)
3982 return 0;
3983
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003984 uint32_t vars_added = 0;
3985 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3986
3987 if (variables.get() == NULL)
3988 {
3989 variables.reset(new VariableList());
3990 sc.comp_unit->SetVariableList(variables);
3991
Greg Claytond4a2b372011-09-12 23:21:58 +00003992 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00003993 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003994 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003995 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003996 {
3997 DWARFMappedHash::DIEInfoArray hash_data_array;
3998 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
3999 dwarf_cu->GetNextCompileUnitOffset(),
4000 hash_data_array))
4001 {
4002 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
4003 }
4004 }
Greg Clayton7f995132011-10-04 22:41:51 +00004005 }
4006 else
4007 {
4008 // Index if we already haven't to make sure the compile units
4009 // get indexed and make their global DIE index list
4010 if (!m_indexed)
4011 Index ();
4012
4013 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
Greg Clayton7f995132011-10-04 22:41:51 +00004014 die_offsets);
4015 }
4016
4017 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004018 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004019 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004020 DWARFDebugInfo* debug_info = DebugInfo();
4021 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004022 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004023 const DIERef& die_ref = die_offsets[i];
4024 DWARFDIE die = debug_info->GetDIE (die_ref);
Greg Clayton95d87902011-11-11 03:16:25 +00004025 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00004026 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004027 VariableSP var_sp (ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
Greg Clayton95d87902011-11-11 03:16:25 +00004028 if (var_sp)
4029 {
4030 variables->AddVariableIfUnique (var_sp);
4031 ++vars_added;
4032 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004033 }
Greg Clayton95d87902011-11-11 03:16:25 +00004034 else
4035 {
4036 if (m_using_apple_tables)
4037 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004038 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 +00004039 }
4040 }
4041
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004042 }
4043 }
4044 }
4045 return vars_added;
4046 }
4047 }
4048 return 0;
4049}
4050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004051VariableSP
4052SymbolFileDWARF::ParseVariableDIE
4053(
4054 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004055 const DWARFDIE &die,
Greg Clayton016a95e2010-09-14 02:20:48 +00004056 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004057)
4058{
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004059 if (die.GetDWARF() != this)
4060 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
4061
Greg Clayton6071e6f2015-08-26 22:57:51 +00004062 VariableSP var_sp;
4063 if (!die)
4064 return var_sp;
4065
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004066 var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton83c5cd92010-11-14 22:13:40 +00004067 if (var_sp)
4068 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004069
Greg Clayton6071e6f2015-08-26 22:57:51 +00004070 const dw_tag_t tag = die.Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00004071 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00004072
4073 if ((tag == DW_TAG_variable) ||
4074 (tag == DW_TAG_constant) ||
4075 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004076 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004077 DWARFAttributes attributes;
4078 const size_t num_attributes = die.GetAttributes(attributes);
Paul Hermand628cbb2015-09-15 23:44:17 +00004079 DWARFDIE spec_die;
Greg Clayton7f995132011-10-04 22:41:51 +00004080 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004081 {
Greg Clayton7f995132011-10-04 22:41:51 +00004082 const char *name = NULL;
4083 const char *mangled = NULL;
4084 Declaration decl;
4085 uint32_t i;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004086 DWARFFormValue type_die_form;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004087 DWARFExpression location(die.GetCU());
Greg Clayton7f995132011-10-04 22:41:51 +00004088 bool is_external = false;
4089 bool is_artificial = false;
4090 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004091 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00004092 DWARFFormValue const_value;
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004093 Variable::RangeList scope_ranges;
Greg Clayton23f59502012-07-17 03:23:13 +00004094 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00004095
4096 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004097 {
Greg Clayton7f995132011-10-04 22:41:51 +00004098 dw_attr_t attr = attributes.AttributeAtIndex(i);
4099 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00004100
Greg Clayton6071e6f2015-08-26 22:57:51 +00004101 if (attributes.ExtractFormValueAtIndex(i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004102 {
Greg Clayton7f995132011-10-04 22:41:51 +00004103 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004104 {
Greg Clayton7f995132011-10-04 22:41:51 +00004105 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4106 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4107 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004108 case DW_AT_name: name = form_value.AsCString(); break;
Greg Clayton71415542012-12-08 00:24:40 +00004109 case DW_AT_linkage_name:
Greg Clayton6071e6f2015-08-26 22:57:51 +00004110 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004111 case DW_AT_type: type_die_form = form_value; break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004112 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004113 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004114 // If we have already found a DW_AT_location attribute, ignore this attribute.
4115 if (!has_explicit_location)
4116 {
4117 location_is_const_value_data = true;
4118 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00004119 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004120 if (DWARFFormValue::IsBlockForm(form_value.Form()))
4121 {
4122 // Retrieve the value as a block expression.
4123 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4124 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004125 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004126 }
4127 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4128 {
4129 // Retrieve the value as a data expression.
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004130 DWARFFormValue::FixedFormSizes fixed_form_sizes =
4131 DWARFFormValue::GetFixedFormSizesForAddressSize (
4132 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
4133 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004134 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004135 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Enrico Granata4ec130d2014-08-11 19:16:35 +00004136 if (data_length == 0)
4137 {
4138 const uint8_t *data_pointer = form_value.BlockData();
4139 if (data_pointer)
4140 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00004141 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00004142 }
4143 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4144 {
4145 // we need to get the byte size of the type later after we create the variable
4146 const_value = form_value;
4147 }
4148 }
4149 else
4150 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004151 }
4152 else
4153 {
4154 // Retrieve the value as a string expression.
4155 if (form_value.Form() == DW_FORM_strp)
4156 {
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004157 DWARFFormValue::FixedFormSizes fixed_form_sizes =
4158 DWARFFormValue::GetFixedFormSizesForAddressSize (
4159 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
4160 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004161 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00004162 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Richard Mitton0a558352013-10-17 21:14:00 +00004163 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004164 }
4165 else
4166 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004167 const char *str = form_value.AsCString();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004168 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
4169 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00004170 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004171 }
4172 }
4173 }
4174 break;
Greg Clayton7f995132011-10-04 22:41:51 +00004175 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004176 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004177 location_is_const_value_data = false;
4178 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00004179 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004180 {
Ed Masteeeae7212013-10-24 20:43:47 +00004181 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004182
4183 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4184 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004185 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004186 }
4187 else
4188 {
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004189 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004190 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4191
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004192 size_t loc_list_length = DWARFExpression::LocationListSize(die.GetCU(), debug_loc_data, debug_loc_offset);
Greg Clayton7f995132011-10-04 22:41:51 +00004193 if (loc_list_length > 0)
4194 {
Richard Mitton0a558352013-10-17 21:14:00 +00004195 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004196 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00004197 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00004198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004199 }
4200 }
Greg Clayton7f995132011-10-04 22:41:51 +00004201 break;
Paul Hermand628cbb2015-09-15 23:44:17 +00004202 case DW_AT_specification:
4203 {
4204 DWARFDebugInfo* debug_info = DebugInfo();
4205 if (debug_info)
4206 spec_die = debug_info->GetDIE(DIERef(form_value));
4207 break;
4208 }
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004209 case DW_AT_start_scope:
4210 {
4211 if (form_value.Form() == DW_FORM_sec_offset)
4212 {
4213 DWARFRangeList dwarf_scope_ranges;
4214 const DWARFDebugRanges* debug_ranges = DebugRanges();
4215 debug_ranges->FindRanges(form_value.Unsigned(), dwarf_scope_ranges);
4216
4217 // All DW_AT_start_scope are relative to the base address of the
4218 // compile unit. We add the compile unit base address to make
4219 // sure all the addresses are properly fixed up.
4220 for (size_t i = 0, count = dwarf_scope_ranges.GetSize(); i < count; ++i)
4221 {
4222 const DWARFRangeList::Entry& range = dwarf_scope_ranges.GetEntryRef(i);
4223 scope_ranges.Append(range.GetRangeBase() + die.GetCU()->GetBaseAddress(),
4224 range.GetByteSize());
4225 }
4226 }
4227 else
4228 {
4229 // TODO: Handle the case when DW_AT_start_scope have form constant. The
4230 // dwarf spec is a bit ambiguous about what is the expected behavior in
4231 // case the enclosing block have a non coninious address range and the
4232 // DW_AT_start_scope entry have a form constant.
4233 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_start_scope has unsupported form type (0x%x)\n",
4234 die.GetID(),
4235 form_value.Form());
4236 }
4237
4238 scope_ranges.Sort();
4239 scope_ranges.CombineConsecutiveRanges();
4240 }
Greg Clayton1c8ef472013-04-05 23:27:21 +00004241 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00004242 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004243 case DW_AT_declaration:
4244 case DW_AT_description:
4245 case DW_AT_endianity:
4246 case DW_AT_segment:
Greg Clayton7f995132011-10-04 22:41:51 +00004247 case DW_AT_visibility:
4248 default:
4249 case DW_AT_abstract_origin:
4250 case DW_AT_sibling:
Greg Clayton7f995132011-10-04 22:41:51 +00004251 break;
4252 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004253 }
4254 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004255
Greg Clayton6071e6f2015-08-26 22:57:51 +00004256 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
4257 const dw_tag_t parent_tag = die.GetParent().Tag();
4258 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 +00004259
Greg Clayton9e9f2192013-05-17 00:55:28 +00004260 ValueType scope = eValueTypeInvalid;
4261
Greg Clayton6071e6f2015-08-26 22:57:51 +00004262 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Greg Clayton9e9f2192013-05-17 00:55:28 +00004263 SymbolContextScope * symbol_context_scope = NULL;
4264
Siva Chandra0783ab92015-03-24 18:32:27 +00004265 if (!mangled)
4266 {
4267 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
4268 // generate fully qualified names of global variables with commands like "frame var j".
4269 // For example, if j were an int variable holding a value 4 and declared in a namespace
4270 // B which in turn is contained in a namespace A, the command "frame var j" returns
4271 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
4272 // to generate a fully qualified name from the declaration context.
Greg Clayton6071e6f2015-08-26 22:57:51 +00004273 if (parent_tag == DW_TAG_compile_unit &&
Jim Ingham0e0984e2015-09-02 01:06:46 +00004274 Language::LanguageIsCPlusPlus(die.GetLanguage()))
Siva Chandra0783ab92015-03-24 18:32:27 +00004275 {
4276 DWARFDeclContext decl_ctx;
4277
Greg Clayton6071e6f2015-08-26 22:57:51 +00004278 die.GetDWARFDeclContext(decl_ctx);
Siva Chandra0783ab92015-03-24 18:32:27 +00004279 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
4280 }
4281 }
4282
Greg Clayton9e9f2192013-05-17 00:55:28 +00004283 // DWARF doesn't specify if a DW_TAG_variable is a local, global
4284 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004285 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00004286 // a DW_OP_addr opcode _somewhere_ in the definition. I say
4287 // somewhere because clang likes to combine small global variables
4288 // into the same symbol and have locations like:
4289 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
4290 // So if we don't have a DW_TAG_formal_parameter, we can look at
4291 // the location to see if it contains a DW_OP_addr opcode, and
4292 // then we can correctly classify our variables.
4293 if (tag == DW_TAG_formal_parameter)
4294 scope = eValueTypeVariableArgument;
4295 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004296 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004297 bool op_error = false;
4298 // Check if the location has a DW_OP_addr with any address value...
4299 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
4300 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004301 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004302 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
4303 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00004304 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004305 StreamString strm;
4306 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Clayton6071e6f2015-08-26 22:57:51 +00004307 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 +00004308 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004309 }
Greg Claytond1767f02011-12-08 02:13:16 +00004310
Greg Clayton9e9f2192013-05-17 00:55:28 +00004311 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
4312 {
4313 if (is_external)
4314 scope = eValueTypeVariableGlobal;
4315 else
4316 scope = eValueTypeVariableStatic;
4317
4318
4319 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
4320
4321 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004322 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004323 // When leaving the DWARF in the .o files on darwin,
4324 // when we have a global variable that wasn't initialized,
4325 // the .o file might not have allocated a virtual
4326 // address for the global variable. In this case it will
4327 // have created a symbol for the global variable
4328 // that is undefined/data and external and the value will
4329 // be the byte size of the variable. When we do the
4330 // address map in SymbolFileDWARFDebugMap we rely on
4331 // having an address, we need to do some magic here
4332 // so we can get the correct address for our global
4333 // variable. The address for all of these entries
4334 // will be zero, and there will be an undefined symbol
4335 // in this object file, and the executable will have
4336 // a matching symbol with a good address. So here we
4337 // dig up the correct address and replace it in the
4338 // location for the variable, and set the variable's
4339 // symbol context scope to be that of the main executable
4340 // so the file address will resolve correctly.
4341 bool linked_oso_file_addr = false;
4342 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00004343 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004344 // we have a possible uninitialized extern global
4345 ConstString const_name(mangled ? mangled : name);
4346 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
4347 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004348 {
Greg Clayton3046e662013-07-10 01:23:25 +00004349 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004350 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004351 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004352 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
4353 eSymbolTypeData,
4354 Symtab::eDebugYes,
4355 Symtab::eVisibilityExtern);
4356 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004357 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004358 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004359 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004360 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004361 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004362 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004363 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004364 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004365 linked_oso_file_addr = true;
4366 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004367 }
4368 }
4369 }
4370 }
4371 }
4372 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004373 }
Greg Clayton9422dd62013-03-04 21:46:16 +00004374
Greg Clayton9e9f2192013-05-17 00:55:28 +00004375 if (!linked_oso_file_addr)
4376 {
4377 // The DW_OP_addr is not zero, but it contains a .o file address which
4378 // needs to be linked up correctly.
4379 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
4380 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00004381 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004382 // Update the file address for this variable
4383 location.Update_DW_OP_addr (exe_file_addr);
4384 }
4385 else
4386 {
4387 // Variable didn't make it into the final executable
4388 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00004389 }
Greg Claytond1767f02011-12-08 02:13:16 +00004390 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004391 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00004392 }
4393 else
4394 {
Ewan Crawford37395ad2015-12-17 11:59:47 +00004395 if (location_is_const_value_data)
4396 scope = eValueTypeVariableStatic;
4397 else
4398 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004399 }
Greg Clayton7f995132011-10-04 22:41:51 +00004400 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004401
4402 if (symbol_context_scope == NULL)
4403 {
4404 switch (parent_tag)
4405 {
4406 case DW_TAG_subprogram:
4407 case DW_TAG_inlined_subroutine:
4408 case DW_TAG_lexical_block:
4409 if (sc.function)
4410 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004411 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Clayton9e9f2192013-05-17 00:55:28 +00004412 if (symbol_context_scope == NULL)
4413 symbol_context_scope = sc.function;
4414 }
4415 break;
4416
4417 default:
4418 symbol_context_scope = sc.comp_unit;
4419 break;
4420 }
4421 }
4422
4423 if (symbol_context_scope)
4424 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004425 SymbolFileTypeSP type_sp(new SymbolFileType(*this, DIERef(type_die_form).GetUID()));
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004426
Enrico Granata4ec130d2014-08-11 19:16:35 +00004427 if (const_value.Form() && type_sp && type_sp->GetType())
Greg Clayton6071e6f2015-08-26 22:57:51 +00004428 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), die.GetCU()->GetAddressByteSize());
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004429
Greg Clayton6071e6f2015-08-26 22:57:51 +00004430 var_sp.reset (new Variable (die.GetID(),
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004431 name,
Greg Clayton9e9f2192013-05-17 00:55:28 +00004432 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00004433 type_sp,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +00004434 scope,
4435 symbol_context_scope,
4436 scope_ranges,
4437 &decl,
4438 location,
4439 is_external,
Paul Herman10bc1a42015-08-18 22:46:57 +00004440 is_artificial,
4441 is_static_member));
Greg Clayton9e9f2192013-05-17 00:55:28 +00004442
4443 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4444 }
4445 else
4446 {
4447 // Not ready to parse this variable yet. It might be a global
4448 // or static variable that is in a function scope and the function
4449 // in the symbol context wasn't filled in yet
4450 return var_sp;
4451 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004452 }
Greg Clayton7f995132011-10-04 22:41:51 +00004453 // Cache var_sp even if NULL (the variable was just a specification or
4454 // was missing vital information to be able to be displayed in the debugger
4455 // (missing location due to optimization, etc)) so we don't re-parse
4456 // this DIE over and over later...
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004457 GetDIEToVariable()[die.GetDIE()] = var_sp;
Paul Hermand628cbb2015-09-15 23:44:17 +00004458 if (spec_die)
4459 GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004460 }
4461 return var_sp;
4462}
4463
Greg Claytonc662ec82011-06-17 22:10:16 +00004464
Greg Clayton6071e6f2015-08-26 22:57:51 +00004465DWARFDIE
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004466SymbolFileDWARF::FindBlockContainingSpecification (const DIERef& func_die_ref,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004467 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004468{
4469 // Give the concrete function die specified by "func_die_offset", find the
4470 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4471 // to "spec_block_die_offset"
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004472 return FindBlockContainingSpecification (DebugInfo()->GetDIE (func_die_ref), spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004473}
4474
4475
Greg Clayton6071e6f2015-08-26 22:57:51 +00004476DWARFDIE
4477SymbolFileDWARF::FindBlockContainingSpecification(const DWARFDIE &die,
4478 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004479{
4480 if (die)
4481 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004482 switch (die.Tag())
Greg Claytonc662ec82011-06-17 22:10:16 +00004483 {
4484 case DW_TAG_subprogram:
4485 case DW_TAG_inlined_subroutine:
4486 case DW_TAG_lexical_block:
4487 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004488 if (die.GetAttributeValueAsReference (DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004489 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004490
Greg Clayton6071e6f2015-08-26 22:57:51 +00004491 if (die.GetAttributeValueAsReference (DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004492 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004493 }
4494 break;
4495 }
4496
4497 // Give the concrete function die specified by "func_die_offset", find the
4498 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4499 // to "spec_block_die_offset"
Greg Clayton6071e6f2015-08-26 22:57:51 +00004500 for (DWARFDIE child_die = die.GetFirstChild(); child_die; child_die = child_die.GetSibling())
Greg Claytonc662ec82011-06-17 22:10:16 +00004501 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004502 DWARFDIE result_die = FindBlockContainingSpecification (child_die, spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004503 if (result_die)
4504 return result_die;
4505 }
4506 }
4507
Greg Clayton6071e6f2015-08-26 22:57:51 +00004508 return DWARFDIE();
Greg Claytonc662ec82011-06-17 22:10:16 +00004509}
4510
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004511size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00004512SymbolFileDWARF::ParseVariables (const SymbolContext& sc,
4513 const DWARFDIE &orig_die,
4514 const lldb::addr_t func_low_pc,
4515 bool parse_siblings,
4516 bool parse_children,
4517 VariableList* cc_variable_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004518{
Greg Clayton6071e6f2015-08-26 22:57:51 +00004519 if (!orig_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004520 return 0;
4521
Greg Claytonc662ec82011-06-17 22:10:16 +00004522 VariableListSP variable_list_sp;
4523
4524 size_t vars_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004525 DWARFDIE die = orig_die;
4526 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004527 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004528 dw_tag_t tag = die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004529
4530 // Check to see if we have already parsed this variable or constant?
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004531 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
Greg Clayton6071e6f2015-08-26 22:57:51 +00004532 if (var_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004533 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004534 if (cc_variable_list)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004535 cc_variable_list->AddVariableIfUnique (var_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004536 }
4537 else
4538 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004539 // We haven't already parsed it, lets do that now.
4540 if ((tag == DW_TAG_variable) ||
4541 (tag == DW_TAG_constant) ||
4542 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004543 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004544 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004545 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004546 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4547 dw_tag_t parent_tag = sc_parent_die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004548 switch (parent_tag)
4549 {
4550 case DW_TAG_compile_unit:
4551 if (sc.comp_unit != NULL)
4552 {
4553 variable_list_sp = sc.comp_unit->GetVariableList(false);
4554 if (variable_list_sp.get() == NULL)
4555 {
4556 variable_list_sp.reset(new VariableList());
4557 sc.comp_unit->SetVariableList(variable_list_sp);
4558 }
4559 }
4560 else
4561 {
Daniel Malead01b2952012-11-29 21:49:15 +00004562 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 +00004563 sc_parent_die.GetID(),
4564 sc_parent_die.GetTagAsCString(),
4565 orig_die.GetID(),
4566 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004567 }
4568 break;
4569
4570 case DW_TAG_subprogram:
4571 case DW_TAG_inlined_subroutine:
4572 case DW_TAG_lexical_block:
4573 if (sc.function != NULL)
4574 {
4575 // Check to see if we already have parsed the variables for the given scope
4576
Greg Clayton6071e6f2015-08-26 22:57:51 +00004577 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004578 if (block == NULL)
4579 {
4580 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004581 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00004582 // to find the concrete block so we can correctly add the
4583 // variable to it
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004584 const DWARFDIE concrete_block_die = FindBlockContainingSpecification (DIERef(sc.function->GetID()),
Greg Clayton6071e6f2015-08-26 22:57:51 +00004585 sc_parent_die.GetOffset());
Greg Claytonc662ec82011-06-17 22:10:16 +00004586 if (concrete_block_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004587 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004588 }
4589
4590 if (block != NULL)
4591 {
4592 const bool can_create = false;
4593 variable_list_sp = block->GetBlockVariableList (can_create);
4594 if (variable_list_sp.get() == NULL)
4595 {
4596 variable_list_sp.reset(new VariableList());
4597 block->SetVariableList(variable_list_sp);
4598 }
4599 }
4600 }
4601 break;
4602
4603 default:
Daniel Malead01b2952012-11-29 21:49:15 +00004604 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 +00004605 orig_die.GetID(),
4606 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004607 break;
4608 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004609 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004610
4611 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004612 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004613 VariableSP var_sp (ParseVariableDIE(sc, die, func_low_pc));
Greg Clayton73bf5db2011-06-17 01:22:15 +00004614 if (var_sp)
4615 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004616 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004617 if (cc_variable_list)
4618 cc_variable_list->AddVariableIfUnique (var_sp);
4619 ++vars_added;
4620 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004621 }
4622 }
4623 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004624
4625 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4626
Greg Clayton6071e6f2015-08-26 22:57:51 +00004627 if (!skip_children && parse_children && die.HasChildren())
Greg Claytonc662ec82011-06-17 22:10:16 +00004628 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004629 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, true, cc_variable_list);
Greg Claytonc662ec82011-06-17 22:10:16 +00004630 }
4631
4632 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004633 die = die.GetSibling();
Greg Claytonc662ec82011-06-17 22:10:16 +00004634 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00004635 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004636 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004637 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004638}
4639
4640//------------------------------------------------------------------
4641// PluginInterface protocol
4642//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004643ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004644SymbolFileDWARF::GetPluginName()
4645{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004646 return GetPluginNameStatic();
4647}
4648
4649uint32_t
4650SymbolFileDWARF::GetPluginVersion()
4651{
4652 return 1;
4653}
4654
4655void
Sean Callanancc427fa2011-07-30 02:42:06 +00004656SymbolFileDWARF::DumpIndexes ()
4657{
4658 StreamFile s(stdout, false);
4659
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004660 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00004661 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004662 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00004663 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
4664 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
4665 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
4666 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
4667 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
4668 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
4669 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00004670 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00004671}
4672
Greg Claytoncaab74e2012-01-28 00:48:57 +00004673
Greg Clayton1f746072012-08-29 21:13:06 +00004674SymbolFileDWARFDebugMap *
4675SymbolFileDWARF::GetDebugMapSymfile ()
4676{
4677 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
4678 {
4679 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
4680 if (module_sp)
4681 {
4682 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4683 if (sym_vendor)
4684 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
4685 }
4686 }
4687 return m_debug_map_symfile;
4688}
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00004689
4690DWARFExpression::LocationListFormat
4691SymbolFileDWARF::GetLocationListFormat() const
4692{
4693 return DWARFExpression::RegularLocationList;
4694}