blob: aabb09daf3b3723c78f861a986d0e1912024b904 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Robert Flackeb83fab2015-05-15 18:59:59 +000029#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000031#include "lldb/Core/ModuleList.h"
32#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/PluginManager.h"
34#include "lldb/Core/RegularExpression.h"
35#include "lldb/Core/Scalar.h"
36#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000037#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000038#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/Timer.h"
40#include "lldb/Core/Value.h"
41
Sean Callananf0c5aeb2015-04-20 16:31:29 +000042#include "lldb/Expression/ClangModulesDeclVendor.h"
43
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000044#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000045#include "lldb/Host/Host.h"
46
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000047#include "lldb/Interpreter/OptionValueFileSpecList.h"
48#include "lldb/Interpreter/OptionValueProperties.h"
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/Block.h"
51#include "lldb/Symbol/CompileUnit.h"
52#include "lldb/Symbol/LineTable.h"
53#include "lldb/Symbol/ObjectFile.h"
54#include "lldb/Symbol/SymbolVendor.h"
55#include "lldb/Symbol/VariableList.h"
56
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000057#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000058#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000059
Greg Clayton261ac3f2015-08-28 01:01:03 +000060#include "DWARFASTParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFCompileUnit.h"
62#include "DWARFDebugAbbrev.h"
63#include "DWARFDebugAranges.h"
64#include "DWARFDebugInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "DWARFDebugLine.h"
66#include "DWARFDebugPubnames.h"
67#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000068#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "DWARFDIECollection.h"
70#include "DWARFFormValue.h"
71#include "DWARFLocationList.h"
72#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000073#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
75#include <map>
76
Matthew Gardinere81df3b2014-08-26 06:57:23 +000077#include <ctype.h>
78#include <string.h>
79
Greg Clayton62742b12010-11-11 01:09:45 +000080//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000081
82#ifdef ENABLE_DEBUG_PRINTF
83#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000084#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000085#else
86#define DEBUG_PRINTF(fmt, ...)
87#endif
88
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089using namespace lldb;
90using namespace lldb_private;
91
Greg Clayton219cf312012-03-30 00:51:13 +000092//static inline bool
93//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
94//{
95// switch (tag)
96// {
97// default:
98// break;
99// case DW_TAG_subprogram:
100// case DW_TAG_inlined_subroutine:
101// case DW_TAG_class_type:
102// case DW_TAG_structure_type:
103// case DW_TAG_union_type:
104// return true;
105// }
106// return false;
107//}
108//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000109
110namespace {
111
112 PropertyDefinition
113 g_properties[] =
114 {
115 { "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." },
116 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
117 };
118
119 enum
120 {
121 ePropertySymLinkPaths
122 };
123
124
125 class PluginProperties : public Properties
126 {
127 public:
128 static ConstString
129 GetSettingName()
130 {
131 return SymbolFileDWARF::GetPluginNameStatic();
132 }
133
134 PluginProperties()
135 {
136 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
137 m_collection_sp->Initialize(g_properties);
138 }
139
140 FileSpecList&
141 GetSymLinkPaths()
142 {
143 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, true, ePropertySymLinkPaths);
144 assert(option_value);
145 return option_value->GetCurrentValue();
146 }
147
148 };
149
150 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
151
152 static const SymbolFileDWARFPropertiesSP&
153 GetGlobalPluginProperties()
154 {
155 static const auto g_settings_sp(std::make_shared<PluginProperties>());
156 return g_settings_sp;
157 }
158
159} // anonymous namespace end
160
161
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000162static const char*
163removeHostnameFromPathname(const char* path_from_dwarf)
164{
165 if (!path_from_dwarf || !path_from_dwarf[0])
166 {
167 return path_from_dwarf;
168 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000169
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000170 const char *colon_pos = strchr(path_from_dwarf, ':');
Enrico Granata99e5e222015-07-27 21:27:02 +0000171 if (nullptr == colon_pos)
172 {
173 return path_from_dwarf;
174 }
175
176 const char *slash_pos = strchr(path_from_dwarf, '/');
177 if (slash_pos && (slash_pos < colon_pos))
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000178 {
179 return path_from_dwarf;
180 }
181
182 // check whether we have a windows path, and so the first character
183 // is a drive-letter not a hostname.
184 if (
185 colon_pos == path_from_dwarf + 1 &&
186 isalpha(*path_from_dwarf) &&
187 strlen(path_from_dwarf) > 2 &&
188 '\\' == path_from_dwarf[2])
189 {
190 return path_from_dwarf;
191 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000192
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000193 return colon_pos + 1;
194}
195
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000196static const char*
197resolveCompDir(const char* path_from_dwarf)
198{
199 if (!path_from_dwarf)
200 return nullptr;
201
202 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
203 // Remove the host part if present.
204 const char* local_path = removeHostnameFromPathname(path_from_dwarf);
205 if (!local_path)
206 return nullptr;
207
208 bool is_symlink = false;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000209 FileSpec local_path_spec(local_path, false);
210 const auto& file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
211 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
212 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i), local_path_spec, true);
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000213
214 if (!is_symlink)
215 return local_path;
216
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000217 if (!local_path_spec.IsSymbolicLink())
218 return local_path;
219
220 FileSpec resolved_local_path_spec;
221 const auto error = FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
222 if (error.Success())
223 return resolved_local_path_spec.GetCString();
224
225 return nullptr;
226}
227
228
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229void
230SymbolFileDWARF::Initialize()
231{
232 LogChannelDWARF::Initialize();
233 PluginManager::RegisterPlugin (GetPluginNameStatic(),
234 GetPluginDescriptionStatic(),
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000235 CreateInstance,
236 DebuggerInitialize);
237}
238
239void
240SymbolFileDWARF::DebuggerInitialize(Debugger &debugger)
241{
242 if (!PluginManager::GetSettingForSymbolFilePlugin(debugger, PluginProperties::GetSettingName()))
243 {
244 const bool is_global_setting = true;
245 PluginManager::CreateSettingForSymbolFilePlugin(debugger,
246 GetGlobalPluginProperties()->GetValueProperties(),
247 ConstString ("Properties for the dwarf symbol-file plug-in."),
248 is_global_setting);
249 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
252void
253SymbolFileDWARF::Terminate()
254{
255 PluginManager::UnregisterPlugin (CreateInstance);
256 LogChannelDWARF::Initialize();
257}
258
259
Greg Clayton57abc5d2013-05-10 21:47:16 +0000260lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261SymbolFileDWARF::GetPluginNameStatic()
262{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000263 static ConstString g_name("dwarf");
264 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265}
266
267const char *
268SymbolFileDWARF::GetPluginDescriptionStatic()
269{
270 return "DWARF and DWARF3 debug symbol file reader.";
271}
272
273
274SymbolFile*
275SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
276{
277 return new SymbolFileDWARF(obj_file);
278}
279
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000280TypeList *
281SymbolFileDWARF::GetTypeList ()
282{
Greg Clayton1f746072012-08-29 21:13:06 +0000283 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000284 return m_debug_map_symfile->GetTypeList();
285 return m_obj_file->GetModule()->GetTypeList();
286
287}
Greg Claytonf02500c2013-06-18 22:51:05 +0000288void
Greg Clayton6071e6f2015-08-26 22:57:51 +0000289SymbolFileDWARF::GetTypes (const DWARFDIE &die,
Greg Claytonf02500c2013-06-18 22:51:05 +0000290 dw_offset_t min_die_offset,
291 dw_offset_t max_die_offset,
292 uint32_t type_mask,
293 TypeSet &type_set)
294{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000295 if (die)
Greg Claytonf02500c2013-06-18 22:51:05 +0000296 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000297 const dw_offset_t die_offset = die.GetOffset();
298
299 if (die_offset >= max_die_offset)
300 return;
301
302 if (die_offset >= min_die_offset)
Greg Claytonf02500c2013-06-18 22:51:05 +0000303 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000304 const dw_tag_t tag = die.Tag();
Greg Claytonf02500c2013-06-18 22:51:05 +0000305
Greg Clayton6071e6f2015-08-26 22:57:51 +0000306 bool add_type = false;
307
308 switch (tag)
Greg Claytonf02500c2013-06-18 22:51:05 +0000309 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000310 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
311 case DW_TAG_unspecified_type:
312 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
313 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
314 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
315 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
316 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
317 case DW_TAG_subroutine_type:
318 case DW_TAG_subprogram:
319 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
320 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
321 case DW_TAG_rvalue_reference_type:
322 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
323 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
324 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
325 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000326
Greg Clayton6071e6f2015-08-26 22:57:51 +0000327 if (add_type)
328 {
329 const bool assert_not_being_parsed = true;
330 Type *type = ResolveTypeUID (die, assert_not_being_parsed);
331 if (type)
Greg Claytonf02500c2013-06-18 22:51:05 +0000332 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000333 if (type_set.find(type) == type_set.end())
334 type_set.insert(type);
Greg Claytonf02500c2013-06-18 22:51:05 +0000335 }
336 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000337 }
338
339 for (DWARFDIE child_die = die.GetFirstChild();
340 child_die.IsValid();
341 child_die = child_die.GetSibling())
342 {
343 GetTypes (child_die, min_die_offset, max_die_offset, type_mask, type_set);
Greg Claytonf02500c2013-06-18 22:51:05 +0000344 }
345 }
346}
347
348size_t
349SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
350 uint32_t type_mask,
351 TypeList &type_list)
352
353{
354 TypeSet type_set;
355
356 CompileUnit *comp_unit = NULL;
357 DWARFCompileUnit* dwarf_cu = NULL;
358 if (sc_scope)
359 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
360
361 if (comp_unit)
362 {
363 dwarf_cu = GetDWARFCompileUnit(comp_unit);
364 if (dwarf_cu == 0)
365 return 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +0000366 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000367 dwarf_cu->GetOffset(),
368 dwarf_cu->GetNextCompileUnitOffset(),
369 type_mask,
370 type_set);
371 }
372 else
373 {
374 DWARFDebugInfo* info = DebugInfo();
375 if (info)
376 {
377 const size_t num_cus = info->GetNumCompileUnits();
378 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
379 {
380 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
381 if (dwarf_cu)
382 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000383 GetTypes (dwarf_cu->DIE(),
Greg Claytonf02500c2013-06-18 22:51:05 +0000384 0,
385 UINT32_MAX,
386 type_mask,
387 type_set);
388 }
389 }
390 }
391 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000392
Greg Claytona1e5dc82015-08-11 22:53:00 +0000393 std::set<CompilerType> clang_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000394 size_t num_types_added = 0;
395 for (Type *type : type_set)
396 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000397 CompilerType clang_type = type->GetForwardCompilerType ();
Greg Clayton0fc4f312013-06-20 01:23:18 +0000398 if (clang_type_set.find(clang_type) == clang_type_set.end())
399 {
400 clang_type_set.insert(clang_type);
401 type_list.Insert (type->shared_from_this());
402 ++num_types_added;
403 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000404 }
405 return num_types_added;
406}
407
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409//----------------------------------------------------------------------
410// Gets the first parent that is a lexical block, function or inlined
411// subroutine, or compile unit.
412//----------------------------------------------------------------------
Greg Clayton6071e6f2015-08-26 22:57:51 +0000413DWARFDIE
414SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415{
Greg Clayton6071e6f2015-08-26 22:57:51 +0000416 DWARFDIE die;
417 for (die = child_die.GetParent(); die; die = die.GetParent())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 {
Greg Clayton6071e6f2015-08-26 22:57:51 +0000419 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
421 switch (tag)
422 {
423 case DW_TAG_compile_unit:
424 case DW_TAG_subprogram:
425 case DW_TAG_inlined_subroutine:
426 case DW_TAG_lexical_block:
427 return die;
428 }
429 }
Greg Clayton6071e6f2015-08-26 22:57:51 +0000430 return DWARFDIE();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
433
Greg Clayton450e3f32010-10-12 02:24:53 +0000434SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
435 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000436 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 +0000437 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000438 m_debug_map_symfile (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000440 m_data_debug_abbrev (),
441 m_data_debug_aranges (),
442 m_data_debug_frame (),
443 m_data_debug_info (),
444 m_data_debug_line (),
445 m_data_debug_loc (),
446 m_data_debug_ranges (),
447 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000448 m_data_apple_names (),
449 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000450 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 m_info(),
453 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000454 m_apple_names_ap (),
455 m_apple_types_ap (),
456 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000457 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000458 m_function_basename_index(),
459 m_function_fullname_index(),
460 m_function_method_index(),
461 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000462 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000463 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000464 m_type_index(),
465 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000466 m_indexed (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000467 m_using_apple_tables (false),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000468 m_fetched_external_modules (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000469 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000470 m_ranges(),
471 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472{
473}
474
475SymbolFileDWARF::~SymbolFileDWARF()
476{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000477}
478
479static const ConstString &
480GetDWARFMachOSegmentName ()
481{
482 static ConstString g_dwarf_section_name ("__DWARF");
483 return g_dwarf_section_name;
484}
485
Greg Claytone576ab22011-02-15 00:19:15 +0000486UniqueDWARFASTTypeMap &
487SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
488{
Greg Clayton1f746072012-08-29 21:13:06 +0000489 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000490 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
491 return m_unique_ast_type_map;
492}
493
Greg Clayton8b4edba2015-08-14 20:02:05 +0000494ClangASTContext &
Greg Clayton6beaaa62011-01-17 03:46:26 +0000495SymbolFileDWARF::GetClangASTContext ()
496{
Greg Clayton1f746072012-08-29 21:13:06 +0000497 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000498 return m_debug_map_symfile->GetClangASTContext ();
Greg Clayton6dc8d582015-08-18 22:32:36 +0000499 else
500 return m_obj_file->GetModule()->GetClangASTContext();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000501}
502
Greg Clayton8b4edba2015-08-14 20:02:05 +0000503TypeSystem *
504SymbolFileDWARF::GetTypeSystemForLanguage (LanguageType language)
505{
506 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
507 if (debug_map_symfile)
508 return debug_map_symfile->GetTypeSystemForLanguage (language);
509 else
Greg Clayton6dc8d582015-08-18 22:32:36 +0000510 return m_obj_file->GetModule()->GetTypeSystemForLanguage (language);
Greg Clayton8b4edba2015-08-14 20:02:05 +0000511}
512
Greg Clayton6beaaa62011-01-17 03:46:26 +0000513void
514SymbolFileDWARF::InitializeObject()
515{
Greg Claytone72dfb32012-02-24 01:59:29 +0000516 ModuleSP module_sp (m_obj_file->GetModule());
517 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000518 {
Greg Clayton3046e662013-07-10 01:23:25 +0000519 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000520
521 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
522
523 // Memory map the DWARF mach-o segment so we have everything mmap'ed
524 // to keep our heap memory usage down.
525 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000526 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000527 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000528 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000529 if (m_data_apple_names.GetByteSize() > 0)
530 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000531 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
532 if (m_apple_names_ap->IsValid())
533 m_using_apple_tables = true;
534 else
Greg Clayton7f995132011-10-04 22:41:51 +0000535 m_apple_names_ap.reset();
536 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000537 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000538 if (m_data_apple_types.GetByteSize() > 0)
539 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000540 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
541 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();
548 if (m_data_apple_namespaces.GetByteSize() > 0)
549 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000550 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
551 if (m_apple_namespaces_ap->IsValid())
552 m_using_apple_tables = true;
553 else
Greg Clayton7f995132011-10-04 22:41:51 +0000554 m_apple_namespaces_ap.reset();
555 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000556
Greg Clayton5009f9d2011-10-27 17:55:14 +0000557 get_apple_objc_data();
558 if (m_data_apple_objc.GetByteSize() > 0)
559 {
560 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
561 if (m_apple_objc_ap->IsValid())
562 m_using_apple_tables = true;
563 else
564 m_apple_objc_ap.reset();
565 }
Greg Clayton6dc8d582015-08-18 22:32:36 +0000566
567 // Set the symbol file to this file if we don't have a debug map symbol
568 // file as our main symbol file. This allows the clang ASTContext to complete
569 // types using this symbol file when it needs to complete classes and structures.
570 if (GetDebugMapSymfile () == nullptr)
571 GetClangASTContext().SetSymbolFile(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572}
573
574bool
575SymbolFileDWARF::SupportedVersion(uint16_t version)
576{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000577 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578}
579
580uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000581SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582{
583 uint32_t abilities = 0;
584 if (m_obj_file != NULL)
585 {
586 const Section* section = NULL;
587 const SectionList *section_list = m_obj_file->GetSectionList();
588 if (section_list == NULL)
589 return 0;
590
591 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 uint64_t debug_info_file_size = 0;
593 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594
Greg Clayton6beaaa62011-01-17 03:46:26 +0000595 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
597 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000598 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599
Greg Clayton4ceb9982010-07-21 22:54:26 +0000600 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 if (section != NULL)
602 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000603 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604
Greg Clayton4ceb9982010-07-21 22:54:26 +0000605 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000607 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 else
609 m_flags.Set (flagsGotDebugAbbrevData);
610
Greg Clayton4ceb9982010-07-21 22:54:26 +0000611 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000612 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 m_flags.Set (flagsGotDebugArangesData);
614
Greg Clayton4ceb9982010-07-21 22:54:26 +0000615 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000616 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 m_flags.Set (flagsGotDebugFrameData);
618
Greg Clayton4ceb9982010-07-21 22:54:26 +0000619 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000621 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 else
623 m_flags.Set (flagsGotDebugLineData);
624
Greg Clayton4ceb9982010-07-21 22:54:26 +0000625 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000626 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 m_flags.Set (flagsGotDebugLocData);
628
Greg Clayton4ceb9982010-07-21 22:54:26 +0000629 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000630 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 m_flags.Set (flagsGotDebugMacInfoData);
632
Greg Clayton4ceb9982010-07-21 22:54:26 +0000633 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000634 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 m_flags.Set (flagsGotDebugPubNamesData);
636
Greg Clayton4ceb9982010-07-21 22:54:26 +0000637 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000638 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 m_flags.Set (flagsGotDebugPubTypesData);
640
Greg Clayton4ceb9982010-07-21 22:54:26 +0000641 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000642 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 m_flags.Set (flagsGotDebugRangesData);
644
Greg Clayton4ceb9982010-07-21 22:54:26 +0000645 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000646 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 m_flags.Set (flagsGotDebugStrData);
648 }
Greg Clayton6c596612012-05-18 21:47:20 +0000649 else
650 {
651 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
652 if (symfile_dir_cstr)
653 {
654 if (strcasestr(symfile_dir_cstr, ".dsym"))
655 {
656 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
657 {
658 // We have a dSYM file that didn't have a any debug info.
659 // If the string table has a size of 1, then it was made from
660 // an executable with no debug info, or from an executable that
661 // was stripped.
662 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
663 if (section && section->GetFileSize() == 1)
664 {
665 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
666 }
667 }
668 }
669 }
670 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671
672 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
673 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
674
675 if (debug_line_file_size > 0)
676 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 }
678 return abilities;
679}
680
Ed Masteeeae7212013-10-24 20:43:47 +0000681const DWARFDataExtractor&
682SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683{
684 if (m_flags.IsClear (got_flag))
685 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000686 ModuleSP module_sp (m_obj_file->GetModule());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 m_flags.Set (got_flag);
Greg Clayton3046e662013-07-10 01:23:25 +0000688 const SectionList *section_list = module_sp->GetSectionList();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 if (section_list)
690 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000691 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
692 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 {
694 // See if we memory mapped the DWARF segment?
695 if (m_dwarf_data.GetByteSize())
696 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000697 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 }
699 else
700 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000701 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 data.Clear();
703 }
704 }
705 }
706 }
707 return data;
708}
709
Ed Masteeeae7212013-10-24 20:43:47 +0000710const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711SymbolFileDWARF::get_debug_abbrev_data()
712{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000713 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714}
715
Ed Masteeeae7212013-10-24 20:43:47 +0000716const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000717SymbolFileDWARF::get_debug_addr_data()
718{
719 return GetCachedSectionData (flagsGotDebugAddrData, eSectionTypeDWARFDebugAddr, m_data_debug_addr);
720}
721
722const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000723SymbolFileDWARF::get_debug_aranges_data()
724{
725 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
726}
727
Ed Masteeeae7212013-10-24 20:43:47 +0000728const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729SymbolFileDWARF::get_debug_frame_data()
730{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000731 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732}
733
Ed Masteeeae7212013-10-24 20:43:47 +0000734const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735SymbolFileDWARF::get_debug_info_data()
736{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000737 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738}
739
Ed Masteeeae7212013-10-24 20:43:47 +0000740const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741SymbolFileDWARF::get_debug_line_data()
742{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000743 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744}
745
Ed Masteeeae7212013-10-24 20:43:47 +0000746const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747SymbolFileDWARF::get_debug_loc_data()
748{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000749 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750}
751
Ed Masteeeae7212013-10-24 20:43:47 +0000752const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753SymbolFileDWARF::get_debug_ranges_data()
754{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000755 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756}
757
Ed Masteeeae7212013-10-24 20:43:47 +0000758const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759SymbolFileDWARF::get_debug_str_data()
760{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000761 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Ed Masteeeae7212013-10-24 20:43:47 +0000764const DWARFDataExtractor&
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000765SymbolFileDWARF::get_debug_str_offsets_data()
766{
767 return GetCachedSectionData (flagsGotDebugStrOffsetsData, eSectionTypeDWARFDebugStrOffsets, m_data_debug_str_offsets);
768}
769
770const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000771SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000772{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000773 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000774}
775
Ed Masteeeae7212013-10-24 20:43:47 +0000776const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000777SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000778{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000779 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000780}
781
Ed Masteeeae7212013-10-24 20:43:47 +0000782const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000783SymbolFileDWARF::get_apple_namespaces_data()
784{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000785 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
786}
787
Ed Masteeeae7212013-10-24 20:43:47 +0000788const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000789SymbolFileDWARF::get_apple_objc_data()
790{
791 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000792}
793
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794
795DWARFDebugAbbrev*
796SymbolFileDWARF::DebugAbbrev()
797{
798 if (m_abbr.get() == NULL)
799 {
Ed Masteeeae7212013-10-24 20:43:47 +0000800 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 if (debug_abbrev_data.GetByteSize() > 0)
802 {
803 m_abbr.reset(new DWARFDebugAbbrev());
804 if (m_abbr.get())
805 m_abbr->Parse(debug_abbrev_data);
806 }
807 }
808 return m_abbr.get();
809}
810
811const DWARFDebugAbbrev*
812SymbolFileDWARF::DebugAbbrev() const
813{
814 return m_abbr.get();
815}
816
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817
818DWARFDebugInfo*
819SymbolFileDWARF::DebugInfo()
820{
821 if (m_info.get() == NULL)
822 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000823 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
824 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 if (get_debug_info_data().GetByteSize() > 0)
826 {
827 m_info.reset(new DWARFDebugInfo());
828 if (m_info.get())
829 {
830 m_info->SetDwarfData(this);
831 }
832 }
833 }
834 return m_info.get();
835}
836
837const DWARFDebugInfo*
838SymbolFileDWARF::DebugInfo() const
839{
840 return m_info.get();
841}
842
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000844SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845{
846 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000847 if (info)
848 {
849 if (GetDebugMapSymfile ())
850 {
851 // The debug map symbol file made the compile units for this DWARF
852 // file which is .o file with DWARF in it, and we should have
853 // only 1 compile unit which is at offset zero in the DWARF.
854 // TODO: modify to support LTO .o files where each .o file might
855 // have multiple DW_TAG_compile_unit tags.
Greg Clayton68c00bd2015-02-05 02:10:29 +0000856
Greg Clayton6071e6f2015-08-26 22:57:51 +0000857 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0);
Greg Clayton68c00bd2015-02-05 02:10:29 +0000858 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
859 dwarf_cu->SetUserData(comp_unit);
860 return dwarf_cu;
Greg Clayton1f746072012-08-29 21:13:06 +0000861 }
862 else
863 {
864 // Just a normal DWARF file whose user ID for the compile unit is
865 // the DWARF offset itself
Greg Clayton68c00bd2015-02-05 02:10:29 +0000866
Greg Clayton6071e6f2015-08-26 22:57:51 +0000867 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
Greg Clayton68c00bd2015-02-05 02:10:29 +0000868 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
869 dwarf_cu->SetUserData(comp_unit);
870 return dwarf_cu;
871
Greg Clayton1f746072012-08-29 21:13:06 +0000872 }
873 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 return NULL;
875}
876
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877
878DWARFDebugRanges*
879SymbolFileDWARF::DebugRanges()
880{
881 if (m_ranges.get() == NULL)
882 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000883 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
884 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 if (get_debug_ranges_data().GetByteSize() > 0)
886 {
887 m_ranges.reset(new DWARFDebugRanges());
888 if (m_ranges.get())
889 m_ranges->Extract(this);
890 }
891 }
892 return m_ranges.get();
893}
894
895const DWARFDebugRanges*
896SymbolFileDWARF::DebugRanges() const
897{
898 return m_ranges.get();
899}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900
Greg Clayton53eb1c22012-04-02 22:59:12 +0000901lldb::CompUnitSP
902SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000904 CompUnitSP cu_sp;
905 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000907 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
908 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000910 // We already parsed this compile unit, had out a shared pointer to it
911 cu_sp = comp_unit->shared_from_this();
912 }
913 else
914 {
Greg Clayton1f746072012-08-29 21:13:06 +0000915 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 {
Greg Clayton1f746072012-08-29 21:13:06 +0000917 // Let the debug map create the compile unit
918 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
919 dwarf_cu->SetUserData(cu_sp.get());
920 }
921 else
922 {
923 ModuleSP module_sp (m_obj_file->GetModule());
924 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000925 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000926 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
Greg Clayton1f746072012-08-29 21:13:06 +0000927 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000928 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000929 FileSpec cu_file_spec{cu_die.GetName(), false};
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000930 if (cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000931 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000932 // If we have a full path to the compile unit, we don't need to resolve
933 // the file. This can be expensive e.g. when the source files are NFS mounted.
Chaoren Lin372e9062015-06-09 17:54:27 +0000934 if (cu_file_spec.IsRelative())
Greg Clayton53eb1c22012-04-02 22:59:12 +0000935 {
Greg Clayton5ce1a842015-08-27 18:09:44 +0000936 const char *cu_comp_dir{cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)};
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000937 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
Greg Clayton1f746072012-08-29 21:13:06 +0000938 }
939
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000940 std::string remapped_file;
941 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file))
942 cu_file_spec.SetFile(remapped_file, false);
David Srbeckyd515e942015-07-08 14:00:04 +0000943 }
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000944
Greg Clayton5ce1a842015-08-27 18:09:44 +0000945 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000946
Jason Molendac7099582015-07-31 05:47:00 +0000947 bool is_optimized = dwarf_cu->GetIsOptimized ();
David Srbeckyd515e942015-07-08 14:00:04 +0000948 cu_sp.reset(new CompileUnit (module_sp,
949 dwarf_cu,
950 cu_file_spec,
Greg Clayton6071e6f2015-08-26 22:57:51 +0000951 dwarf_cu->GetID(),
Jason Molenda6ab659a2015-07-29 00:42:47 +0000952 cu_language,
953 is_optimized));
David Srbeckyd515e942015-07-08 14:00:04 +0000954 if (cu_sp)
955 {
956 // If we just created a compile unit with an invalid file spec, try and get the
957 // first entry in the supports files from the line table as that should be the
958 // compile unit.
959 if (!cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000960 {
David Srbeckyd515e942015-07-08 14:00:04 +0000961 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
962 if (cu_file_spec)
963 {
964 (FileSpec &)(*cu_sp) = cu_file_spec;
965 // Also fix the invalid file spec which was copied from the compile unit.
966 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
967 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000968 }
David Srbeckyd515e942015-07-08 14:00:04 +0000969
970 dwarf_cu->SetUserData(cu_sp.get());
971
972 // Figure out the compile unit index if we weren't given one
973 if (cu_idx == UINT32_MAX)
974 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
975
976 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000977 }
978 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 }
980 }
981 }
982 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000983 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984}
985
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986uint32_t
987SymbolFileDWARF::GetNumCompileUnits()
988{
989 DWARFDebugInfo* info = DebugInfo();
990 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 return 0;
993}
994
995CompUnitSP
996SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
997{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000998 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999 DWARFDebugInfo* info = DebugInfo();
1000 if (info)
1001 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001002 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
1003 if (dwarf_cu)
1004 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001006 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007}
1008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009Function *
Greg Clayton6071e6f2015-08-26 22:57:51 +00001010SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, const DWARFDIE &die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001012 if (die.IsValid())
1013 {
1014 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015
Greg Clayton6071e6f2015-08-26 22:57:51 +00001016 if (type_system)
1017 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001018 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
1019 if (dwarf_ast)
1020 return dwarf_ast->ParseFunctionFromDWARF(sc, die);
Greg Clayton6071e6f2015-08-26 22:57:51 +00001021 }
1022 }
1023 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024}
1025
Greg Clayton9422dd62013-03-04 21:46:16 +00001026bool
1027SymbolFileDWARF::FixupAddress (Address &addr)
1028{
1029 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1030 if (debug_map_symfile)
1031 {
1032 return debug_map_symfile->LinkOSOAddress(addr);
1033 }
1034 // This is a normal DWARF file, no address fixups need to happen
1035 return true;
1036}
Greg Clayton1f746072012-08-29 21:13:06 +00001037lldb::LanguageType
1038SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1039{
1040 assert (sc.comp_unit);
1041 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1042 if (dwarf_cu)
Greg Clayton5ce1a842015-08-27 18:09:44 +00001043 return dwarf_cu->GetLanguageType();
1044 else
1045 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +00001046}
1047
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001048size_t
1049SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1050{
1051 assert (sc.comp_unit);
1052 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001053 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 if (dwarf_cu)
1055 {
1056 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001057 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001059 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001061 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx);
1062 if (sc.comp_unit->FindFunctionByUID (die.GetID()).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001064 if (ParseCompileUnitFunction(sc, die))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 ++functions_added;
1066 }
1067 }
1068 //FixupTypes();
1069 }
1070 return functions_added;
1071}
1072
1073bool
1074SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1075{
1076 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001077 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001078 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001080 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
Greg Claytonda2455b2012-11-01 17:28:37 +00001082 if (cu_die)
1083 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001084 const char * cu_comp_dir = resolveCompDir(cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr));
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001085
Greg Clayton5ce1a842015-08-27 18:09:44 +00001086 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087
Greg Claytonda2455b2012-11-01 17:28:37 +00001088 // All file indexes in DWARF are one based and a file of index zero is
1089 // supposed to be the compile unit itself.
1090 support_files.Append (*sc.comp_unit);
1091
1092 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1093 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 }
1095 return false;
1096}
1097
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001098bool
1099SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1100{
1101 assert (sc.comp_unit);
1102 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1103 if (dwarf_cu)
1104 {
1105 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1106 {
1107 UpdateExternalModuleListIfNeeded();
1108 for (const std::pair<uint64_t, const ClangModuleInfo> &external_type_module : m_external_type_modules)
1109 {
1110 imported_modules.push_back(external_type_module.second.m_name);
1111 }
1112 }
1113 }
1114 return false;
1115}
1116
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117struct ParseDWARFLineTableCallbackInfo
1118{
1119 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001120 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121};
1122
1123//----------------------------------------------------------------------
1124// ParseStatementTableCallback
1125//----------------------------------------------------------------------
1126static void
1127ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1128{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1130 {
1131 // Just started parsing the line table
1132 }
1133 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1134 {
1135 // Done parsing line table, nothing to do for the cleanup
1136 }
1137 else
1138 {
1139 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001140 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141
Greg Clayton9422dd62013-03-04 21:46:16 +00001142 // If this is our first time here, we need to create a
1143 // sequence container.
1144 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001146 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1147 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001149 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1150 state.address,
1151 state.line,
1152 state.column,
1153 state.file,
1154 state.is_stmt,
1155 state.basic_block,
1156 state.prologue_end,
1157 state.epilogue_begin,
1158 state.end_sequence);
1159 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001161 // First, put the current sequence into the line table.
1162 line_table->InsertSequence(info->sequence_ap.get());
1163 // Then, empty it to prepare for the next sequence.
1164 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 }
1167}
1168
1169bool
1170SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1171{
1172 assert (sc.comp_unit);
1173 if (sc.comp_unit->GetLineTable() != NULL)
1174 return true;
1175
Greg Clayton1f746072012-08-29 21:13:06 +00001176 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 if (dwarf_cu)
1178 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001179 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001180 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001182 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 +00001183 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001185 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001186 if (line_table_ap.get())
1187 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001188 ParseDWARFLineTableCallbackInfo info;
1189 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001190 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001191 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001192 if (m_debug_map_symfile)
1193 {
1194 // We have an object file that has a line table with addresses
1195 // that are not linked. We need to link the line table and convert
1196 // the addresses that are relative to the .o file into addresses
1197 // for the main executable.
1198 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1199 }
1200 else
1201 {
1202 sc.comp_unit->SetLineTable(line_table_ap.release());
1203 return true;
1204 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001205 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 }
1207 }
1208 }
1209 return false;
1210}
1211
1212size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00001213SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext& sc,
1214 Block *parent_block,
1215 const DWARFDIE &orig_die,
1216 addr_t subprogram_low_pc,
1217 uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218{
1219 size_t blocks_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001220 DWARFDIE die = orig_die;
1221 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001223 dw_tag_t tag = die.Tag();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224
1225 switch (tag)
1226 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001228 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229 case DW_TAG_lexical_block:
1230 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001231 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001232 if (tag == DW_TAG_subprogram)
1233 {
1234 // Skip any DW_TAG_subprogram DIEs that are inside
1235 // of a normal or inlined functions. These will be
1236 // parsed on their own as separate entities.
1237
1238 if (depth > 0)
1239 break;
1240
1241 block = parent_block;
1242 }
1243 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001244 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001245 BlockSP block_sp(new Block (die.GetID()));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001246 parent_block->AddChild(block_sp);
1247 block = block_sp.get();
1248 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001249 DWARFRangeList ranges;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001250 const char *name = NULL;
1251 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 int decl_file = 0;
1254 int decl_line = 0;
1255 int decl_column = 0;
1256 int call_file = 0;
1257 int call_line = 0;
1258 int call_column = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001259 if (die.GetDIENamesAndRanges (name,
1260 mangled_name,
1261 ranges,
1262 decl_file, decl_line, decl_column,
1263 call_file, call_line, call_column, nullptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 {
1265 if (tag == DW_TAG_subprogram)
1266 {
1267 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001268 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001270 else if (tag == DW_TAG_inlined_subroutine)
1271 {
1272 // We get called here for inlined subroutines in two ways.
1273 // The first time is when we are making the Function object
1274 // for this inlined concrete instance. Since we're creating a top level block at
1275 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1276 // adjust the containing address.
1277 // The second time is when we are parsing the blocks inside the function that contains
1278 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1279 // function the offset will be for that function.
1280 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1281 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001282 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001283 }
1284 }
Greg Clayton103f3092015-01-15 03:04:37 +00001285
1286 const size_t num_ranges = ranges.GetSize();
1287 for (size_t i = 0; i<num_ranges; ++i)
1288 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001289 const DWARFRangeList::Entry &range = ranges.GetEntryRef (i);
Greg Clayton103f3092015-01-15 03:04:37 +00001290 const addr_t range_base = range.GetRangeBase();
1291 if (range_base >= subprogram_low_pc)
1292 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1293 else
1294 {
1295 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",
1296 block->GetID(),
1297 range_base,
1298 range.GetRangeEnd(),
1299 subprogram_low_pc);
1300 }
1301 }
1302 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303
1304 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1305 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001306 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001308 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1309 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310
Greg Clayton7b0992d2013-04-18 22:45:39 +00001311 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001313 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1314 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001316 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 }
1318
1319 ++blocks_added;
1320
Greg Clayton6071e6f2015-08-26 22:57:51 +00001321 if (die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001323 blocks_added += ParseFunctionBlocks (sc,
1324 block,
Greg Clayton6071e6f2015-08-26 22:57:51 +00001325 die.GetFirstChild(),
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001326 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001327 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 }
1329 }
1330 }
1331 break;
1332 default:
1333 break;
1334 }
1335
Greg Claytondd7feaf2011-08-12 17:54:33 +00001336 // Only parse siblings of the block if we are not at depth zero. A depth
1337 // of zero indicates we are currently parsing the top level
1338 // DW_TAG_subprogram DIE
1339
1340 if (depth == 0)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001341 die.Clear();
Greg Claytondd7feaf2011-08-12 17:54:33 +00001342 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001343 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344 }
1345 return blocks_added;
1346}
1347
Greg Claytonf0705c82011-10-22 03:33:13 +00001348bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001349SymbolFileDWARF::ClassOrStructIsVirtual (const DWARFDIE &parent_die)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001350{
1351 if (parent_die)
1352 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001353 for (DWARFDIE die = parent_die.GetFirstChild(); die; die = die.GetSibling())
Greg Claytonc4ffd662013-03-08 01:37:30 +00001354 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001355 dw_tag_t tag = die.Tag();
Greg Claytonc4ffd662013-03-08 01:37:30 +00001356 bool check_virtuality = false;
1357 switch (tag)
1358 {
1359 case DW_TAG_inheritance:
1360 case DW_TAG_subprogram:
1361 check_virtuality = true;
1362 break;
1363 default:
1364 break;
1365 }
1366 if (check_virtuality)
1367 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001368 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
Greg Claytonc4ffd662013-03-08 01:37:30 +00001369 return true;
1370 }
1371 }
1372 }
1373 return false;
1374}
1375
Greg Clayton99558cc42015-08-24 23:46:31 +00001376CompilerDeclContext
1377SymbolFileDWARF::GetDeclContextForUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001379 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00001381 DWARFDebugInfo* debug_info = DebugInfo();
1382 if (debug_info)
1383 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001384 DWARFDIE die = debug_info->GetDIE(type_uid);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001385 if (die)
1386 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001387 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1388 if (dwarf_ast)
1389 return dwarf_ast->GetDeclContextForUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001390 }
1391 }
Sean Callanan72e49402011-08-05 23:43:37 +00001392 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001393 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +00001394}
1395
Greg Clayton99558cc42015-08-24 23:46:31 +00001396CompilerDeclContext
1397SymbolFileDWARF::GetDeclContextContainingUID (lldb::user_id_t type_uid)
Sean Callanan72e49402011-08-05 23:43:37 +00001398{
Greg Clayton81c22f62011-10-19 18:09:39 +00001399 if (UserIDMatches(type_uid))
Greg Clayton8b4edba2015-08-14 20:02:05 +00001400 {
1401 DWARFDebugInfo* debug_info = DebugInfo();
1402 if (debug_info)
1403 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001404 DWARFDIE die = debug_info->GetDIE(type_uid);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001405 if (die)
1406 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001407 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
1408 if (dwarf_ast)
1409 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
Greg Clayton8b4edba2015-08-14 20:02:05 +00001410 }
1411 }
1412 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001413 return CompilerDeclContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414}
1415
Greg Clayton99558cc42015-08-24 23:46:31 +00001416
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001418SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419{
Greg Clayton81c22f62011-10-19 18:09:39 +00001420 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001422 DWARFDebugInfo* debug_info = DebugInfo();
1423 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001424 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001425 DWARFDIE type_die = debug_info->GetDIE (type_uid);
1426 if (type_die)
1427 {
1428 const bool assert_not_being_parsed = true;
1429 return ResolveTypeUID (type_die, assert_not_being_parsed);
1430 }
Greg Claytonca512b32011-01-14 04:54:56 +00001431 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 }
1433 return NULL;
1434}
1435
Greg Claytoncab36a32011-12-08 05:16:30 +00001436Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001437SymbolFileDWARF::ResolveTypeUID (const DWARFDIE &die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001438{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001439 if (die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001440 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001441 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00001442 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001443 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001444 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001445 die.GetOffset(),
1446 die.GetTagAsCString(),
1447 die.GetName());
Greg Clayton3bffb082011-12-10 02:15:28 +00001448
Greg Claytoncab36a32011-12-08 05:16:30 +00001449 // We might be coming in in the middle of a type tree (a class
1450 // withing a class, an enum within a class), so parse any needed
1451 // parent DIEs before we get to this one...
Greg Clayton6071e6f2015-08-26 22:57:51 +00001452 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE (die);
1453 if (decl_ctx_die)
Greg Claytoncab36a32011-12-08 05:16:30 +00001454 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001455 if (log)
Greg Claytoncab36a32011-12-08 05:16:30 +00001456 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001457 switch (decl_ctx_die.Tag())
1458 {
1459 case DW_TAG_structure_type:
1460 case DW_TAG_union_type:
1461 case DW_TAG_class_type:
1462 {
1463 // Get the type, which could be a forward declaration
1464 if (log)
1465 GetObjectFile()->GetModule()->LogMessage (log,
1466 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1467 die.GetOffset(),
1468 die.GetTagAsCString(),
1469 die.GetName(),
1470 decl_ctx_die.GetOffset());
1471 }
1472 break;
Greg Claytoncab36a32011-12-08 05:16:30 +00001473
Greg Clayton6071e6f2015-08-26 22:57:51 +00001474 default:
1475 break;
1476 }
1477 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001478 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001479 return ResolveType (die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001480 }
1481 return NULL;
1482}
1483
Greg Clayton6beaaa62011-01-17 03:46:26 +00001484// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1485// SymbolFileDWARF objects to detect if this DWARF file is the one that
1486// can resolve a clang_type.
1487bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001488SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &clang_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001489{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001490 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Greg Clayton5ce1a842015-08-27 18:09:44 +00001491 return m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType()) != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001492}
1493
1494
Greg Clayton57ee3062013-07-11 22:46:58 +00001495bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00001496SymbolFileDWARF::CompleteType (CompilerType &clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001497{
1498 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Claytona1e5dc82015-08-11 22:53:00 +00001499 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001500 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001501 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001502 {
1503 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00001504 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00001505 }
1506 // Once we start resolving this type, remove it from the forward declaration
1507 // map in case anyone child members or other types require this type to get resolved.
1508 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1509 // are done.
Greg Clayton57ee3062013-07-11 22:46:58 +00001510 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001511
Greg Clayton450e3f32010-10-12 02:24:53 +00001512 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton6071e6f2015-08-26 22:57:51 +00001513 DWARFDIE dwarf_die (debug_info->GetCompileUnitContainingDIE (die->GetOffset()), die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001514 Type *type = m_die_to_type.lookup (die);
1515
Greg Clayton5160ce52013-03-27 23:08:40 +00001516 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001517 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001518 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00001519 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001520 dwarf_die.GetID(),
1521 dwarf_die.GetTagAsCString(),
Greg Claytond61c0fc2012-04-23 22:55:20 +00001522 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001523 assert (clang_type);
Greg Clayton6071e6f2015-08-26 22:57:51 +00001524 DWARFAttributes attributes;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001525
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001526
Greg Clayton261ac3f2015-08-28 01:01:03 +00001527 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser();
1528 if (dwarf_ast)
1529 return dwarf_ast->CompleteTypeFromDWARF (dwarf_die, type, clang_type);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001530
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001531 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001532}
1533
Greg Clayton8b4edba2015-08-14 20:02:05 +00001534Type*
Greg Clayton6071e6f2015-08-26 22:57:51 +00001535SymbolFileDWARF::ResolveType (const DWARFDIE &die, bool assert_not_being_parsed)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001536{
Greg Clayton6071e6f2015-08-26 22:57:51 +00001537 if (die)
Greg Clayton8b4edba2015-08-14 20:02:05 +00001538 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001539 Type *type = m_die_to_type.lookup (die.GetDIE());
Greg Claytoncab36a32011-12-08 05:16:30 +00001540
Greg Claytonc685f8e2010-09-15 04:15:46 +00001541 if (type == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001542 type = GetTypeForDIE (die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001543
Greg Clayton24739922010-10-13 03:15:28 +00001544 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001545 {
1546 if (type != DIE_IS_BEING_PARSED)
1547 return type;
1548
1549 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton6071e6f2015-08-26 22:57:51 +00001550 die.GetOffset(),
1551 die.GetTagAsCString(),
1552 die.GetName());
Jim Inghamc3549282012-01-11 02:21:12 +00001553
1554 }
1555 else
1556 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001557 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00001558 return nullptr;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001559}
1560
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001562SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563{
1564 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001565 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 {
1567 // The symbol vendor doesn't know about this compile unit, we
1568 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00001569 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001571 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001572}
1573
Greg Clayton8b4edba2015-08-14 20:02:05 +00001574size_t
1575SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets)
1576{
1577 method_die_offsets.clear();
1578 if (m_using_apple_tables)
1579 {
1580 if (m_apple_objc_ap.get())
1581 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1582 }
1583 else
1584 {
1585 if (!m_indexed)
1586 Index ();
1587
1588 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1589 }
1590 return method_die_offsets.size();
1591}
1592
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001593bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00001594SymbolFileDWARF::GetFunction (const DWARFDIE &die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595{
Greg Clayton72310352013-02-23 04:12:47 +00001596 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001597
Greg Clayton6071e6f2015-08-26 22:57:51 +00001598 if (die)
1599 {
1600 // Check if the symbol vendor already knows about this compile unit?
1601 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
1602
1603 sc.function = sc.comp_unit->FindFunctionByUID (die.GetID()).get();
1604 if (sc.function == NULL)
1605 sc.function = ParseCompileUnitFunction(sc, die);
1606
1607 if (sc.function)
1608 {
1609 sc.module_sp = sc.function->CalculateSymbolContextModule();
1610 return true;
1611 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00001612 }
1613
1614 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001615}
1616
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001617void
1618SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
1619{
1620 if (m_fetched_external_modules)
1621 return;
1622 m_fetched_external_modules = true;
1623
1624 DWARFDebugInfo * debug_info = DebugInfo();
1625 debug_info->GetNumCompileUnits();
1626
1627 const uint32_t num_compile_units = GetNumCompileUnits();
1628 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1629 {
1630 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1631
Greg Clayton5ce1a842015-08-27 18:09:44 +00001632 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly();
1633 if (die && die.HasChildren() == false)
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001634 {
Greg Clayton5ce1a842015-08-27 18:09:44 +00001635 const uint64_t name_strp = die.GetAttributeValueAsUnsigned (DW_AT_name, UINT64_MAX);
1636 const uint64_t dwo_path_strp = die.GetAttributeValueAsUnsigned (DW_AT_GNU_dwo_name, UINT64_MAX);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001637
1638 if (name_strp != UINT64_MAX)
1639 {
1640 if (m_external_type_modules.find(dwo_path_strp) == m_external_type_modules.end())
1641 {
1642 const char *name = get_debug_str_data().PeekCStr(name_strp);
1643 const char *dwo_path = get_debug_str_data().PeekCStr(dwo_path_strp);
1644 if (name || dwo_path)
1645 {
1646 ModuleSP module_sp;
1647 if (dwo_path)
1648 {
1649 ModuleSpec dwo_module_spec;
1650 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1651 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
1652 //printf ("Loading dwo = '%s'\n", dwo_path);
1653 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
1654 }
1655
1656 if (dwo_path_strp != LLDB_INVALID_UID)
1657 {
1658 m_external_type_modules[dwo_path_strp] = ClangModuleInfo { ConstString(name), module_sp };
1659 }
1660 else
1661 {
1662 // This hack should be removed promptly once clang emits both.
1663 m_external_type_modules[name_strp] = ClangModuleInfo { ConstString(name), module_sp };
1664 }
1665 }
1666 }
1667 }
1668 }
1669 }
1670}
Greg Clayton2501e5e2015-01-15 02:59:20 +00001671
1672SymbolFileDWARF::GlobalVariableMap &
1673SymbolFileDWARF::GetGlobalAranges()
1674{
1675 if (!m_global_aranges_ap)
1676 {
1677 m_global_aranges_ap.reset (new GlobalVariableMap());
1678
1679 ModuleSP module_sp = GetObjectFile()->GetModule();
1680 if (module_sp)
1681 {
1682 const size_t num_cus = module_sp->GetNumCompileUnits();
1683 for (size_t i = 0; i < num_cus; ++i)
1684 {
1685 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1686 if (cu_sp)
1687 {
1688 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1689 if (globals_sp)
1690 {
1691 const size_t num_globals = globals_sp->GetSize();
1692 for (size_t g = 0; g < num_globals; ++g)
1693 {
1694 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1695 if (var_sp && !var_sp->GetLocationIsConstantValueData())
1696 {
1697 const DWARFExpression &location = var_sp->LocationExpression();
1698 Value location_result;
1699 Error error;
1700 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
1701 {
1702 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
1703 {
1704 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
1705 lldb::addr_t byte_size = 1;
1706 if (var_sp->GetType())
1707 byte_size = var_sp->GetType()->GetByteSize();
1708 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
1709 }
1710 }
1711 }
1712 }
1713 }
1714 }
1715 }
1716 }
1717 m_global_aranges_ap->Sort();
1718 }
1719 return *m_global_aranges_ap;
1720}
1721
1722
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723uint32_t
1724SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1725{
1726 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00001727 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001728 static_cast<void*>(so_addr.GetSection().get()),
1729 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00001731 if (resolve_scope & ( eSymbolContextCompUnit |
1732 eSymbolContextFunction |
1733 eSymbolContextBlock |
1734 eSymbolContextLineEntry |
1735 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 {
1737 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1738
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001740 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001742 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00001743 if (cu_offset == DW_INVALID_OFFSET)
1744 {
1745 // Global variables are not in the compile unit address ranges. The only way to
1746 // currently find global variables is to iterate over the .debug_pubnames or the
1747 // __apple_names table and find all items in there that point to DW_TAG_variable
1748 // DIEs and then find the address that matches.
1749 if (resolve_scope & eSymbolContextVariable)
1750 {
1751 GlobalVariableMap &map = GetGlobalAranges();
1752 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
1753 if (entry && entry->data)
1754 {
1755 Variable *variable = entry->data;
1756 SymbolContextScope *scc = variable->GetSymbolContextScope();
1757 if (scc)
1758 {
1759 scc->CalculateSymbolContext(&sc);
1760 sc.variable = variable;
1761 }
1762 return sc.GetResolvedMask();
1763 }
1764 }
1765 }
1766 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001767 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001768 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton6071e6f2015-08-26 22:57:51 +00001769 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001770 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001772 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001773 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001775 resolved |= eSymbolContextCompUnit;
1776
Greg Clayton6ab80132012-12-12 17:30:52 +00001777 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001778 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1779 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001780 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1781 DWARFDIE block_die;
1782 if (function_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001783 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001784 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001785 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001786 sc.function = ParseCompileUnitFunction(sc, function_die);
1787
1788 if (sc.function && (resolve_scope & eSymbolContextBlock))
1789 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001790 }
1791 else
1792 {
1793 // We might have had a compile unit that had discontiguous
1794 // address ranges where the gaps are symbols that don't have
1795 // any debug info. Discontiguous compile unit address ranges
1796 // should only happen when there aren't other functions from
1797 // other compile units in these gaps. This helps keep the size
1798 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00001799 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001800 }
1801
1802 if (sc.function != NULL)
1803 {
1804 resolved |= eSymbolContextFunction;
1805
1806 if (resolve_scope & eSymbolContextBlock)
1807 {
1808 Block& block = sc.function->GetBlock (true);
1809
Greg Clayton6071e6f2015-08-26 22:57:51 +00001810 if (block_die)
1811 sc.block = block.FindBlockByID (block_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001812 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00001813 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001814 if (sc.block)
1815 resolved |= eSymbolContextBlock;
1816 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 }
1818 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001819
1820 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
1821 {
1822 LineTable *line_table = sc.comp_unit->GetLineTable();
1823 if (line_table != NULL)
1824 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001825 // And address that makes it into this function should be in terms
1826 // of this debug file if there is no debug map, or it will be an
1827 // address in the .o file which needs to be fixed up to be in terms
1828 // of the debug map executable. Either way, calling FixupAddress()
1829 // will work for us.
1830 Address exe_so_addr (so_addr);
1831 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00001832 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001833 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00001834 {
1835 resolved |= eSymbolContextLineEntry;
1836 }
1837 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001838 }
1839 }
1840
1841 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
1842 {
1843 // We might have had a compile unit that had discontiguous
1844 // address ranges where the gaps are symbols that don't have
1845 // any debug info. Discontiguous compile unit address ranges
1846 // should only happen when there aren't other functions from
1847 // other compile units in these gaps. This helps keep the size
1848 // of the aranges down.
1849 sc.comp_unit = NULL;
1850 resolved &= ~eSymbolContextCompUnit;
1851 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001852 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001853 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001855 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
1856 cu_offset,
1857 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858 }
1859 }
1860 }
1861 }
1862 }
1863 return resolved;
1864}
1865
1866
1867
1868uint32_t
1869SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1870{
1871 const uint32_t prev_size = sc_list.GetSize();
1872 if (resolve_scope & eSymbolContextCompUnit)
1873 {
1874 DWARFDebugInfo* debug_info = DebugInfo();
1875 if (debug_info)
1876 {
1877 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00001878 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001879
Greg Clayton53eb1c22012-04-02 22:59:12 +00001880 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001881 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001882 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00001883 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00001884 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 +00001885 if (check_inlines || file_spec_matches_cu_file_spec)
1886 {
1887 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00001888 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001889 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001890 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001891 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892
Greg Clayton526a4ae2012-05-16 22:09:01 +00001893 // If we are looking for inline functions only and we don't
1894 // find it in the support files, we are done.
1895 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001897 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1898 if (file_idx == UINT32_MAX)
1899 continue;
1900 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901
Greg Clayton526a4ae2012-05-16 22:09:01 +00001902 if (line != 0)
1903 {
1904 LineTable *line_table = sc.comp_unit->GetLineTable();
1905
1906 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001907 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001908 // We will have already looked up the file index if
1909 // we are searching for inline entries.
1910 if (!check_inlines)
1911 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912
Greg Clayton526a4ae2012-05-16 22:09:01 +00001913 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001915 uint32_t found_line;
1916 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1917 found_line = sc.line_entry.line;
1918
1919 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001921 sc.function = NULL;
1922 sc.block = NULL;
1923 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001925 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1926 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001927 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001928 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr);
1929 DWARFDIE block_die;
1930 if (function_die)
Greg Clayton526a4ae2012-05-16 22:09:01 +00001931 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00001932 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get();
Greg Clayton526a4ae2012-05-16 22:09:01 +00001933 if (sc.function == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00001934 sc.function = ParseCompileUnitFunction(sc, function_die);
1935
1936 if (sc.function && (resolve_scope & eSymbolContextBlock))
1937 block_die = function_die.LookupDeepestBlock(file_vm_addr);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001938 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939
Greg Clayton526a4ae2012-05-16 22:09:01 +00001940 if (sc.function != NULL)
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());
1946 else if (function_die)
1947 sc.block = block.FindBlockByID (function_die.GetID());
Greg Clayton526a4ae2012-05-16 22:09:01 +00001948 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949 }
1950 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951
Greg Clayton526a4ae2012-05-16 22:09:01 +00001952 sc_list.Append(sc);
1953 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955 }
1956 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001957 else if (file_spec_matches_cu_file_spec && !check_inlines)
1958 {
1959 // only append the context if we aren't looking for inline call sites
1960 // by file and line and if the file spec matches that of the compile unit
1961 sc_list.Append(sc);
1962 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963 }
1964 else if (file_spec_matches_cu_file_spec && !check_inlines)
1965 {
1966 // only append the context if we aren't looking for inline call sites
1967 // by file and line and if the file spec matches that of the compile unit
1968 sc_list.Append(sc);
1969 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001970
Greg Clayton526a4ae2012-05-16 22:09:01 +00001971 if (!check_inlines)
1972 break;
1973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974 }
1975 }
1976 }
1977 }
1978 return sc_list.GetSize() - prev_size;
1979}
1980
1981void
1982SymbolFileDWARF::Index ()
1983{
1984 if (m_indexed)
1985 return;
1986 m_indexed = true;
1987 Timer scoped_timer (__PRETTY_FUNCTION__,
1988 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00001989 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990
1991 DWARFDebugInfo* debug_info = DebugInfo();
1992 if (debug_info)
1993 {
1994 uint32_t cu_idx = 0;
1995 const uint32_t num_compile_units = GetNumCompileUnits();
1996 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1997 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001998 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999
Greg Clayton53eb1c22012-04-02 22:59:12 +00002000 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001
Greg Clayton53eb1c22012-04-02 22:59:12 +00002002 dwarf_cu->Index (cu_idx,
2003 m_function_basename_index,
2004 m_function_fullname_index,
2005 m_function_method_index,
2006 m_function_selector_index,
2007 m_objc_class_selectors_index,
2008 m_global_index,
2009 m_type_index,
2010 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002011
2012 // Keep memory down by clearing DIEs if this generate function
2013 // caused them to be parsed
2014 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002015 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002016 }
2017
Greg Claytond4a2b372011-09-12 23:21:58 +00002018 m_function_basename_index.Finalize();
2019 m_function_fullname_index.Finalize();
2020 m_function_method_index.Finalize();
2021 m_function_selector_index.Finalize();
2022 m_objc_class_selectors_index.Finalize();
2023 m_global_index.Finalize();
2024 m_type_index.Finalize();
2025 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002026
Greg Clayton24739922010-10-13 03:15:28 +00002027#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002028 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002029 s.Printf ("DWARF index for '%s':",
2030 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002031 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2032 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2033 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2034 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2035 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2036 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002037 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00002038 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002039#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002040 }
2041}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002042
2043bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002044SymbolFileDWARF::DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx)
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002045{
Greg Clayton99558cc42015-08-24 23:46:31 +00002046 if (decl_ctx == nullptr || !decl_ctx->IsValid())
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002047 {
2048 // Invalid namespace decl which means we aren't matching only things
2049 // in this symbol file, so return true to indicate it matches this
2050 // symbol file.
2051 return true;
2052 }
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002053
Greg Clayton99558cc42015-08-24 23:46:31 +00002054 if ((TypeSystem *)&GetClangASTContext() == decl_ctx->GetTypeSystem())
2055 return true; // The type systems match, return true
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002056
2057 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00002058 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00002059
2060 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002061 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002062
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002063 return false;
2064}
2065
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002066uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002067SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002068{
Greg Clayton5160ce52013-03-27 23:08:40 +00002069 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002070
2071 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002072 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002073 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00002074 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002075 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002076 append, max_matches);
2077
Greg Clayton99558cc42015-08-24 23:46:31 +00002078 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002079 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002080
Greg Claytonc685f8e2010-09-15 04:15:46 +00002081 DWARFDebugInfo* info = DebugInfo();
2082 if (info == NULL)
2083 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084
2085 // If we aren't appending the results to this list, then clear the list
2086 if (!append)
2087 variables.Clear();
2088
2089 // Remember how many variables are in the list before we search in case
2090 // we are appending the results to a variable list.
2091 const uint32_t original_size = variables.GetSize();
2092
Greg Claytond4a2b372011-09-12 23:21:58 +00002093 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002094
Greg Clayton97fbc342011-10-20 22:30:33 +00002095 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002096 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002097 if (m_apple_names_ap.get())
2098 {
2099 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00002100 llvm::StringRef basename;
2101 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002102
Jim Inghamfa39bb42014-10-25 00:33:55 +00002103 if (!CPPLanguageRuntime::ExtractContextAndIdentifier(name_cstr, context, basename))
2104 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002105
Jim Inghamfa39bb42014-10-25 00:33:55 +00002106 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00002107 }
Greg Clayton7f995132011-10-04 22:41:51 +00002108 }
2109 else
2110 {
2111 // Index the DWARF if we haven't already
2112 if (!m_indexed)
2113 Index ();
2114
2115 m_global_index.Find (name, die_offsets);
2116 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002117
Greg Clayton437a1352012-04-09 22:43:43 +00002118 const size_t num_die_matches = die_offsets.size();
2119 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120 {
Greg Clayton7f995132011-10-04 22:41:51 +00002121 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002122 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002123 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002124
Greg Claytond4a2b372011-09-12 23:21:58 +00002125 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002126 bool done = false;
2127 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002128 {
2129 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002130 DWARFDIE die = debug_info->GetDIE (die_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131
Greg Clayton95d87902011-11-11 03:16:25 +00002132 if (die)
2133 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002134 switch (die.Tag())
Greg Clayton437a1352012-04-09 22:43:43 +00002135 {
2136 default:
2137 case DW_TAG_subprogram:
2138 case DW_TAG_inlined_subroutine:
2139 case DW_TAG_try_block:
2140 case DW_TAG_catch_block:
2141 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002142
Greg Clayton437a1352012-04-09 22:43:43 +00002143 case DW_TAG_variable:
2144 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002145 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002146
Greg Clayton99558cc42015-08-24 23:46:31 +00002147 if (parent_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002148 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002149 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2150 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002151 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002152 CompilerDeclContext actual_parent_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002153 if (!actual_parent_decl_ctx || actual_parent_decl_ctx != *parent_decl_ctx)
2154 continue;
2155 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00002156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157
Greg Clayton6071e6f2015-08-26 22:57:51 +00002158 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002159
Greg Clayton437a1352012-04-09 22:43:43 +00002160 if (variables.GetSize() - original_size >= max_matches)
2161 done = true;
2162 }
2163 break;
2164 }
Greg Clayton95d87902011-11-11 03:16:25 +00002165 }
2166 else
2167 {
2168 if (m_using_apple_tables)
2169 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002170 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2171 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002172 }
2173 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002174 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002175 }
2176
2177 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002178 const uint32_t num_matches = variables.GetSize() - original_size;
2179 if (log && num_matches > 0)
2180 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002181 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002182 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002183 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002184 static_cast<const void*>(parent_decl_ctx),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002185 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002186 num_matches);
2187 }
2188 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002189}
2190
2191uint32_t
2192SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2193{
Greg Clayton5160ce52013-03-27 23:08:40 +00002194 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002195
Greg Clayton21f2a492011-10-06 00:09:08 +00002196 if (log)
2197 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002198 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002199 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002200 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002201 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002202 }
2203
Greg Claytonc685f8e2010-09-15 04:15:46 +00002204 DWARFDebugInfo* info = DebugInfo();
2205 if (info == NULL)
2206 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207
2208 // If we aren't appending the results to this list, then clear the list
2209 if (!append)
2210 variables.Clear();
2211
2212 // Remember how many variables are in the list before we search in case
2213 // we are appending the results to a variable list.
2214 const uint32_t original_size = variables.GetSize();
2215
Greg Clayton7f995132011-10-04 22:41:51 +00002216 DIEArray die_offsets;
2217
Greg Clayton97fbc342011-10-20 22:30:33 +00002218 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002219 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002220 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002221 {
2222 DWARFMappedHash::DIEInfoArray hash_data_array;
2223 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2224 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2225 }
Greg Clayton7f995132011-10-04 22:41:51 +00002226 }
2227 else
2228 {
2229 // Index the DWARF if we haven't already
2230 if (!m_indexed)
2231 Index ();
2232
2233 m_global_index.Find (regex, die_offsets);
2234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235
Greg Claytonc685f8e2010-09-15 04:15:46 +00002236 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002237 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002238 assert (sc.module_sp);
2239
Greg Clayton7f995132011-10-04 22:41:51 +00002240 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002241 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002243 DWARFDebugInfo* debug_info = DebugInfo();
2244 for (size_t i=0; i<num_matches; ++i)
2245 {
2246 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002247 DWARFDIE die = debug_info->GetDIE (die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00002248
2249 if (die)
2250 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002251 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252
Greg Clayton6071e6f2015-08-26 22:57:51 +00002253 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254
Greg Clayton95d87902011-11-11 03:16:25 +00002255 if (variables.GetSize() - original_size >= max_matches)
2256 break;
2257 }
2258 else
2259 {
2260 if (m_using_apple_tables)
2261 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002262 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2263 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002264 }
2265 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002266 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002267 }
2268
2269 // Return the number of variable that were appended to the list
2270 return variables.GetSize() - original_size;
2271}
2272
Greg Claytonaa044962011-10-13 00:59:38 +00002273
Jim Ingham4cda6e02011-10-07 22:23:45 +00002274bool
2275SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
Pavel Labatha73d6572015-03-13 10:22:00 +00002276 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00002277 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002278{
Greg Clayton6071e6f2015-08-26 22:57:51 +00002279 DWARFDIE die = DebugInfo()->GetDIE (die_offset);
2280 return ResolveFunction (die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00002281}
2282
2283
2284bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00002285SymbolFileDWARF::ResolveFunction (const DWARFDIE &orig_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002286 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00002287 SymbolContextList& sc_list)
2288{
Greg Clayton9e315582011-09-02 04:03:59 +00002289 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002290
Greg Clayton6071e6f2015-08-26 22:57:51 +00002291 if (!orig_die)
Greg Claytonaa044962011-10-13 00:59:38 +00002292 return false;
2293
Jim Ingham4cda6e02011-10-07 22:23:45 +00002294 // If we were passed a die that is not a function, just return false...
Greg Clayton6071e6f2015-08-26 22:57:51 +00002295 if (!(orig_die.Tag() == DW_TAG_subprogram || (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002296 return false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002297
2298 DWARFDIE die = orig_die;
2299 DWARFDIE inlined_die;
2300 if (die.Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002301 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002302 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002303
Greg Clayton6071e6f2015-08-26 22:57:51 +00002304 while (1)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002305 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002306 die = die.GetParent();
2307
2308 if (die)
2309 {
2310 if (die.Tag() == DW_TAG_subprogram)
2311 break;
2312 }
2313 else
Jim Ingham4cda6e02011-10-07 22:23:45 +00002314 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002315 }
2316 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00002317 assert (die && die.Tag() == DW_TAG_subprogram);
2318 if (GetFunction (die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002319 {
2320 Address addr;
2321 // Parse all blocks if needed
2322 if (inlined_die)
2323 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002324 Block &function_block = sc.function->GetBlock (true);
Greg Clayton6071e6f2015-08-26 22:57:51 +00002325 sc.block = function_block.FindBlockByID (inlined_die.GetID());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002326 if (sc.block == NULL)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002327 sc.block = function_block.FindBlockByID (inlined_die.GetOffset());
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002328 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002329 addr.Clear();
2330 }
2331 else
2332 {
2333 sc.block = NULL;
2334 addr = sc.function->GetAddressRange().GetBaseAddress();
2335 }
2336
2337 if (addr.IsValid())
2338 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002339 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002340 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002341 }
2342 }
2343
Greg Claytonaa044962011-10-13 00:59:38 +00002344 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002345}
2346
Greg Clayton7f995132011-10-04 22:41:51 +00002347void
2348SymbolFileDWARF::FindFunctions (const ConstString &name,
2349 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002350 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002351 SymbolContextList& sc_list)
2352{
Greg Claytond4a2b372011-09-12 23:21:58 +00002353 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002354 if (name_to_die.Find (name, die_offsets))
2355 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002356 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002357 }
2358}
2359
2360
2361void
2362SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2363 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002364 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002365 SymbolContextList& sc_list)
2366{
2367 DIEArray die_offsets;
2368 if (name_to_die.Find (regex, die_offsets))
2369 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002370 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002371 }
2372}
2373
2374
2375void
2376SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2377 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00002378 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002379 SymbolContextList& sc_list)
2380{
2381 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002382 DWARFMappedHash::DIEInfoArray hash_data_array;
2383 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002384 {
Greg Claytond1767f02011-12-08 02:13:16 +00002385 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00002386 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002387 }
2388}
2389
2390void
2391SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00002392 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002393 SymbolContextList& sc_list)
2394{
2395 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002396 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002397 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002398 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002399 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002400 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002401 ResolveFunction (die_offset, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002402 }
2403 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002404}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002405
Jim Ingham4cda6e02011-10-07 22:23:45 +00002406bool
Greg Clayton99558cc42015-08-24 23:46:31 +00002407SymbolFileDWARF::DIEInDeclContext (const CompilerDeclContext *decl_ctx,
Greg Clayton6071e6f2015-08-26 22:57:51 +00002408 const DWARFDIE &die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002409{
2410 // If we have no parent decl context to match this DIE matches, and if the parent
2411 // decl context isn't valid, we aren't trying to look for any particular decl
2412 // context so any die matches.
2413 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2414 return true;
2415
Greg Clayton6071e6f2015-08-26 22:57:51 +00002416 if (die)
Greg Clayton99558cc42015-08-24 23:46:31 +00002417 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002418 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2419 if (dwarf_ast)
Greg Clayton99558cc42015-08-24 23:46:31 +00002420 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002421 CompilerDeclContext actual_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002422 if (actual_decl_ctx)
2423 return actual_decl_ctx == *decl_ctx;
2424 }
2425 }
2426 return false;
2427}
2428
Greg Clayton0c5cd902010-06-28 21:30:43 +00002429uint32_t
Greg Clayton99558cc42015-08-24 23:46:31 +00002430SymbolFileDWARF::FindFunctions (const ConstString &name,
2431 const CompilerDeclContext *parent_decl_ctx,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002432 uint32_t name_type_mask,
2433 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002434 bool append,
2435 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002436{
2437 Timer scoped_timer (__PRETTY_FUNCTION__,
2438 "SymbolFileDWARF::FindFunctions (name = '%s')",
2439 name.AsCString());
2440
Greg Clayton43fe2172013-04-03 02:00:15 +00002441 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
2442 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
2443
Greg Clayton5160ce52013-03-27 23:08:40 +00002444 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002445
2446 if (log)
2447 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002448 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002449 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2450 name.GetCString(),
2451 name_type_mask,
2452 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002453 }
2454
Greg Clayton0c5cd902010-06-28 21:30:43 +00002455 // If we aren't appending the results to this list, then clear the list
2456 if (!append)
2457 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002458
Greg Clayton99558cc42015-08-24 23:46:31 +00002459 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002460 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002461
2462 // If name is empty then we won't find anything.
2463 if (name.IsEmpty())
2464 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002465
2466 // Remember how many sc_list are in the list before we search in case
2467 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002468
Jim Ingham4cda6e02011-10-07 22:23:45 +00002469 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00002470
2471 const uint32_t original_size = sc_list.GetSize();
2472
Jim Ingham4cda6e02011-10-07 22:23:45 +00002473 DWARFDebugInfo* info = DebugInfo();
2474 if (info == NULL)
2475 return 0;
2476
Greg Clayton43fe2172013-04-03 02:00:15 +00002477 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00002478 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002479 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002480 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002481 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002482
2483 DIEArray die_offsets;
2484
2485 uint32_t num_matches = 0;
2486
Greg Clayton43fe2172013-04-03 02:00:15 +00002487 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002488 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002489 // If they asked for the full name, match what they typed. At some point we may
2490 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2491 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002492 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002493 for (uint32_t i = 0; i < num_matches; i++)
2494 {
Greg Clayton95d87902011-11-11 03:16:25 +00002495 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002496 DWARFDIE die = info->GetDIE (die_offset);
Greg Claytonaa044962011-10-13 00:59:38 +00002497 if (die)
2498 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002499 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002500 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002501
Greg Clayton6071e6f2015-08-26 22:57:51 +00002502 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002503 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002504 if (ResolveFunction (die, include_inlines, sc_list))
2505 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002506 }
Greg Claytonaa044962011-10-13 00:59:38 +00002507 }
Greg Clayton95d87902011-11-11 03:16:25 +00002508 else
2509 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002510 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2511 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002512 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002513 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002514 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002515
2516 if (name_type_mask & eFunctionNameTypeSelector)
2517 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002518 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00002519 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00002520
Greg Clayton43fe2172013-04-03 02:00:15 +00002521 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2522 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2523 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00002524
Greg Clayton43fe2172013-04-03 02:00:15 +00002525 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00002526 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002527 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002528 DWARFDIE die = info->GetDIE (die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002529 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00002530 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002531 const char *die_name = die.GetName();
Greg Clayton43fe2172013-04-03 02:00:15 +00002532 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00002533 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002534 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002535 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002536 if (ResolveFunction (die, include_inlines, sc_list))
2537 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002538 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002539 }
2540 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002541 else
2542 {
2543 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2544 die_offset, name_cstr);
2545 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002546 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002547 die_offsets.clear();
2548 }
2549
Greg Clayton99558cc42015-08-24 23:46:31 +00002550 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) || name_type_mask & eFunctionNameTypeBase)
Greg Clayton43fe2172013-04-03 02:00:15 +00002551 {
2552 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2553 // extract the base name, look that up, and if there is any other information in the name we were
2554 // passed in we have to post-filter based on that.
2555
2556 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2557 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2558
2559 for (uint32_t i = 0; i < num_matches; i++)
2560 {
2561 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002562 DWARFDIE die = info->GetDIE (die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002563 if (die)
2564 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002565 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002566 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002567
Greg Clayton43fe2172013-04-03 02:00:15 +00002568
2569 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002570 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() && ResolveFunction (die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002571 {
2572 bool keep_die = true;
2573 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
2574 {
2575 // We are looking for either basenames or methods, so we need to
2576 // trim out the ones we won't want by looking at the type
2577 SymbolContext sc;
2578 if (sc_list.GetLastContext(sc))
2579 {
2580 if (sc.block)
2581 {
2582 // We have an inlined function
2583 }
2584 else if (sc.function)
2585 {
2586 Type *type = sc.function->GetType();
2587
Sean Callananc370a8a2013-09-18 22:59:55 +00002588 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00002589 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002590 CompilerDeclContext decl_ctx = GetDeclContextContainingUID (type->GetID());
2591 if (decl_ctx.IsStructUnionOrClass())
Greg Clayton43fe2172013-04-03 02:00:15 +00002592 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002593 if (name_type_mask & eFunctionNameTypeBase)
2594 {
2595 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2596 keep_die = false;
2597 }
2598 }
2599 else
2600 {
2601 if (name_type_mask & eFunctionNameTypeMethod)
2602 {
2603 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2604 keep_die = false;
2605 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002606 }
2607 }
2608 else
2609 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002610 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
2611 die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002612 }
2613 }
2614 }
2615 }
2616 if (keep_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00002617 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002618 }
2619 }
2620 else
2621 {
2622 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2623 die_offset, name_cstr);
2624 }
2625 }
2626 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002627 }
2628 }
Greg Clayton7f995132011-10-04 22:41:51 +00002629 }
2630 else
2631 {
2632
2633 // Index the DWARF if we haven't already
2634 if (!m_indexed)
2635 Index ();
2636
Greg Clayton7f995132011-10-04 22:41:51 +00002637 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00002638 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002639 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002640
Ed Mastefc7baa02013-09-09 18:00:45 +00002641 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00002642 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00002643 // If we didn't find any functions in the global namespace try
2644 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00002645 // functions that have a namespace but keep functions which
2646 // have an anonymous namespace
2647 // TODO: The arch in the object file isn't correct for MSVC
2648 // binaries on windows, we should find a way to make it
2649 // correct and handle those symbols as well.
Matt Kopecd6089962013-05-10 17:53:48 +00002650 if (sc_list.GetSize() == 0)
2651 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002652 ArchSpec arch;
Greg Clayton99558cc42015-08-24 23:46:31 +00002653 if (!parent_decl_ctx &&
Robert Flackeb83fab2015-05-15 18:59:59 +00002654 GetObjectFile()->GetArchitecture(arch) &&
2655 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
2656 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00002657 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002658 SymbolContextList temp_sc_list;
2659 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00002660 SymbolContext sc;
2661 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
2662 {
2663 if (temp_sc_list.GetContextAtIndex(i, sc))
2664 {
Matt Kopeca189d492013-05-10 22:55:24 +00002665 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
2666 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00002667 // Mangled names on Linux and FreeBSD are of the form:
2668 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00002669 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2670 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00002671 {
2672 sc_list.Append(sc);
2673 }
2674 }
2675 }
2676 }
2677 }
Matt Kopecd6089962013-05-10 17:53:48 +00002678 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002679 DIEArray die_offsets;
Greg Clayton43fe2172013-04-03 02:00:15 +00002680 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002681 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002682 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002683 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002684 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002685 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002686 if (die)
2687 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002688 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002689 continue; // The containing decl contexts don't match
Greg Clayton8b4edba2015-08-14 20:02:05 +00002690
Greg Claytonaa044962011-10-13 00:59:38 +00002691 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002692 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002693 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002694 if (ResolveFunction (die, include_inlines, sc_list))
2695 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002696 }
Greg Claytonaa044962011-10-13 00:59:38 +00002697 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002698 }
2699 die_offsets.clear();
2700 }
2701
Greg Clayton43fe2172013-04-03 02:00:15 +00002702 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002703 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002704 if (parent_decl_ctx && parent_decl_ctx->IsValid())
Sean Callanan213fdb82011-10-13 01:49:10 +00002705 return 0; // no methods in namespaces
2706
Greg Clayton43fe2172013-04-03 02:00:15 +00002707 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002708 {
Greg Claytonaa044962011-10-13 00:59:38 +00002709 for (uint32_t i = 0; i < num_base; i++)
2710 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002711 DWARFDIE die = info->GetDIE (die_offsets[i]);
Greg Claytonaa044962011-10-13 00:59:38 +00002712 if (die)
2713 {
Greg Claytonaa044962011-10-13 00:59:38 +00002714 // If we get to here, the die is good, and we should add it:
Greg Clayton6071e6f2015-08-26 22:57:51 +00002715 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end())
Greg Clayton43fe2172013-04-03 02:00:15 +00002716 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002717 if (ResolveFunction (die, include_inlines, sc_list))
2718 resolved_dies.insert(die.GetDIE());
Greg Clayton43fe2172013-04-03 02:00:15 +00002719 }
Greg Claytonaa044962011-10-13 00:59:38 +00002720 }
2721 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002722 }
2723 die_offsets.clear();
2724 }
Greg Clayton7f995132011-10-04 22:41:51 +00002725
Greg Clayton99558cc42015-08-24 23:46:31 +00002726 if ((name_type_mask & eFunctionNameTypeSelector) && (!parent_decl_ctx || !parent_decl_ctx->IsValid()))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002727 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002728 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002729 }
2730
Greg Clayton4d01ace2011-09-29 16:58:15 +00002731 }
2732
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002733 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002734 const uint32_t num_matches = sc_list.GetSize() - original_size;
2735
2736 if (log && num_matches > 0)
2737 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002738 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00002739 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002740 name.GetCString(),
2741 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00002742 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00002743 append,
2744 num_matches);
2745 }
2746 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002747}
2748
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002749uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00002750SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002751{
2752 Timer scoped_timer (__PRETTY_FUNCTION__,
2753 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2754 regex.GetText());
2755
Greg Clayton5160ce52013-03-27 23:08:40 +00002756 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002757
2758 if (log)
2759 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002760 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002761 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2762 regex.GetText(),
2763 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002764 }
2765
2766
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002767 // If we aren't appending the results to this list, then clear the list
2768 if (!append)
2769 sc_list.Clear();
2770
2771 // Remember how many sc_list are in the list before we search in case
2772 // we are appending the results to a variable list.
2773 uint32_t original_size = sc_list.GetSize();
2774
Greg Clayton97fbc342011-10-20 22:30:33 +00002775 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002776 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002777 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00002778 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002779 }
2780 else
2781 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002782 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002783 if (!m_indexed)
2784 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002785
Pavel Labatha73d6572015-03-13 10:22:00 +00002786 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002787
Pavel Labatha73d6572015-03-13 10:22:00 +00002788 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002789 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002790
2791 // Return the number of variable that were appended to the list
2792 return sc_list.GetSize() - original_size;
2793}
Jim Ingham318c9f22011-08-26 19:44:13 +00002794
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002795uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00002796SymbolFileDWARF::FindTypes (const SymbolContext& sc,
2797 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00002798 const CompilerDeclContext *parent_decl_ctx,
Greg Claytond1767f02011-12-08 02:13:16 +00002799 bool append,
2800 uint32_t max_matches,
2801 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002802{
Greg Claytonc685f8e2010-09-15 04:15:46 +00002803 DWARFDebugInfo* info = DebugInfo();
2804 if (info == NULL)
2805 return 0;
2806
Greg Clayton5160ce52013-03-27 23:08:40 +00002807 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002808
Greg Clayton21f2a492011-10-06 00:09:08 +00002809 if (log)
2810 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002811 if (parent_decl_ctx)
Greg Clayton5160ce52013-03-27 23:08:40 +00002812 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002813 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list)",
Greg Clayton437a1352012-04-09 22:43:43 +00002814 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002815 static_cast<const void*>(parent_decl_ctx),
2816 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002817 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00002818 else
Greg Clayton5160ce52013-03-27 23:08:40 +00002819 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002820 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002821 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00002822 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002823 }
2824
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002825 // If we aren't appending the results to this list, then clear the list
2826 if (!append)
2827 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002828
Greg Clayton99558cc42015-08-24 23:46:31 +00002829 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
Ed Maste4c24b122013-10-17 20:13:14 +00002830 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002831
Greg Claytond4a2b372011-09-12 23:21:58 +00002832 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002833
Greg Clayton97fbc342011-10-20 22:30:33 +00002834 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002835 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002836 if (m_apple_types_ap.get())
2837 {
2838 const char *name_cstr = name.GetCString();
2839 m_apple_types_ap->FindByName (name_cstr, die_offsets);
2840 }
Greg Clayton7f995132011-10-04 22:41:51 +00002841 }
2842 else
2843 {
2844 if (!m_indexed)
2845 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002846
Greg Clayton7f995132011-10-04 22:41:51 +00002847 m_type_index.Find (name, die_offsets);
2848 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002849
Greg Clayton437a1352012-04-09 22:43:43 +00002850 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00002851
Greg Clayton437a1352012-04-09 22:43:43 +00002852 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002853 {
Greg Clayton7f995132011-10-04 22:41:51 +00002854 const uint32_t initial_types_size = types.GetSize();
Greg Claytond4a2b372011-09-12 23:21:58 +00002855 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00002856 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002857 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002858 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002859 DWARFDIE die = debug_info->GetDIE (die_offset);
Greg Claytond4a2b372011-09-12 23:21:58 +00002860
Greg Clayton95d87902011-11-11 03:16:25 +00002861 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00002862 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002863 if (!DIEInDeclContext(parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002864 continue; // The containing decl contexts don't match
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002865
Greg Clayton6071e6f2015-08-26 22:57:51 +00002866 Type *matching_type = ResolveType (die);
Greg Clayton95d87902011-11-11 03:16:25 +00002867 if (matching_type)
2868 {
2869 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00002870 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00002871 if (types.GetSize() >= max_matches)
2872 break;
2873 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00002874 }
Greg Clayton95d87902011-11-11 03:16:25 +00002875 else
2876 {
2877 if (m_using_apple_tables)
2878 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002879 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
2880 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002881 }
2882 }
2883
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002884 }
Greg Clayton437a1352012-04-09 22:43:43 +00002885 const uint32_t num_matches = types.GetSize() - initial_types_size;
2886 if (log && num_matches)
2887 {
Greg Clayton99558cc42015-08-24 23:46:31 +00002888 if (parent_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00002889 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002890 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002891 "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 +00002892 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002893 static_cast<const void*>(parent_decl_ctx),
2894 parent_decl_ctx->GetName().AsCString("<NULL>"),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002895 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002896 num_matches);
2897 }
2898 else
2899 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002900 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002901 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002902 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002903 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002904 num_matches);
2905 }
2906 }
2907 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002908 }
Greg Clayton7f995132011-10-04 22:41:51 +00002909 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002910}
2911
2912
Greg Clayton99558cc42015-08-24 23:46:31 +00002913CompilerDeclContext
Greg Clayton96d7d742010-11-10 23:42:09 +00002914SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00002915 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +00002916 const CompilerDeclContext *parent_decl_ctx)
Greg Clayton96d7d742010-11-10 23:42:09 +00002917{
Greg Clayton5160ce52013-03-27 23:08:40 +00002918 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002919
2920 if (log)
2921 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002922 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002923 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2924 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00002925 }
2926
Greg Clayton99558cc42015-08-24 23:46:31 +00002927 CompilerDeclContext namespace_decl_ctx;
2928
2929 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2930 return namespace_decl_ctx;
2931
2932
Greg Clayton96d7d742010-11-10 23:42:09 +00002933 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00002934 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00002935 {
Greg Clayton7f995132011-10-04 22:41:51 +00002936 DIEArray die_offsets;
2937
Greg Clayton526e5af2010-11-13 03:52:47 +00002938 // Index if we already haven't to make sure the compile units
2939 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00002940 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002941 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002942 if (m_apple_namespaces_ap.get())
2943 {
2944 const char *name_cstr = name.GetCString();
2945 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
2946 }
Greg Clayton7f995132011-10-04 22:41:51 +00002947 }
2948 else
2949 {
2950 if (!m_indexed)
2951 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00002952
Greg Clayton7f995132011-10-04 22:41:51 +00002953 m_namespace_index.Find (name, die_offsets);
2954 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002955
Greg Clayton7f995132011-10-04 22:41:51 +00002956 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002957 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00002958 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002959 DWARFDebugInfo* debug_info = DebugInfo();
2960 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00002961 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002962 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00002963 DWARFDIE die = debug_info->GetDIE (die_offset);
2964
Greg Clayton95d87902011-11-11 03:16:25 +00002965 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00002966 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00002967 if (!DIEInDeclContext (parent_decl_ctx, die))
Greg Clayton99558cc42015-08-24 23:46:31 +00002968 continue; // The containing decl contexts don't match
Greg Clayton95d87902011-11-11 03:16:25 +00002969
Greg Clayton261ac3f2015-08-28 01:01:03 +00002970 DWARFASTParser *dwarf_ast = die.GetDWARFParser();
2971 if (dwarf_ast)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002972 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00002973 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF (die);
Greg Clayton99558cc42015-08-24 23:46:31 +00002974 if (namespace_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00002975 break;
Greg Clayton95d87902011-11-11 03:16:25 +00002976 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002977 }
Greg Clayton95d87902011-11-11 03:16:25 +00002978 else
2979 {
2980 if (m_using_apple_tables)
2981 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002982 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
2983 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002984 }
2985 }
2986
Greg Clayton526e5af2010-11-13 03:52:47 +00002987 }
2988 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002989 }
Greg Clayton99558cc42015-08-24 23:46:31 +00002990 if (log && namespace_decl_ctx)
Greg Clayton437a1352012-04-09 22:43:43 +00002991 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002992 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton99558cc42015-08-24 23:46:31 +00002993 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => CompilerDeclContext(%p/%p) \"%s\"",
Greg Clayton437a1352012-04-09 22:43:43 +00002994 name.GetCString(),
Greg Clayton99558cc42015-08-24 23:46:31 +00002995 static_cast<const void*>(namespace_decl_ctx.GetTypeSystem()),
2996 static_cast<const void*>(namespace_decl_ctx.GetOpaqueDeclContext()),
2997 namespace_decl_ctx.GetName().AsCString("<NULL>"));
Greg Clayton437a1352012-04-09 22:43:43 +00002998 }
2999
Greg Clayton99558cc42015-08-24 23:46:31 +00003000 return namespace_decl_ctx;
Greg Clayton96d7d742010-11-10 23:42:09 +00003001}
3002
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003003uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003004SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003005{
3006 // Remember how many sc_list are in the list before we search in case
3007 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003008 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003009
3010 const uint32_t num_die_offsets = die_offsets.size();
3011 // Parse all of the types we found from the pubtypes matches
3012 uint32_t i;
3013 uint32_t num_matches = 0;
3014 for (i = 0; i < num_die_offsets; ++i)
3015 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003016 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3017 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003019 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003020 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003021 ++num_matches;
3022 if (num_matches >= max_matches)
3023 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003024 }
3025 }
3026
3027 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003028 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003029}
3030
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003031
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003033SymbolFileDWARF::GetTypeForDIE (const DWARFDIE &die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003034{
3035 TypeSP type_sp;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003036 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003038 Type *type_ptr = m_die_to_type.lookup (die.GetDIE());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039 if (type_ptr == NULL)
3040 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003041 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU());
Greg Claytonca512b32011-01-14 04:54:56 +00003042 assert (lldb_cu);
3043 SymbolContext sc(lldb_cu);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003044 type_sp = ParseType(sc, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045 }
3046 else if (type_ptr != DIE_IS_BEING_PARSED)
3047 {
3048 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003049 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003050 }
3051
3052 }
3053 return type_sp;
3054}
3055
Greg Clayton2bc22f82011-09-30 03:20:47 +00003056
Greg Clayton6071e6f2015-08-26 22:57:51 +00003057DWARFDIE
3058SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFDIE &orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003059{
Greg Clayton6071e6f2015-08-26 22:57:51 +00003060 if (orig_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003061 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003062 DWARFDIE die = orig_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003063
Greg Clayton6071e6f2015-08-26 22:57:51 +00003064 while (die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003065 {
3066 // If this is the original DIE that we are searching for a declaration
3067 // for, then don't look in the cache as we don't want our own decl
3068 // context to be our decl context...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003069 if (orig_die != die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003070 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003071 switch (die.Tag())
Greg Clayton2bc22f82011-09-30 03:20:47 +00003072 {
3073 case DW_TAG_compile_unit:
3074 case DW_TAG_namespace:
3075 case DW_TAG_structure_type:
3076 case DW_TAG_union_type:
3077 case DW_TAG_class_type:
3078 return die;
3079
3080 default:
3081 break;
3082 }
3083 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003084
3085 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
3086 if (spec_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003087 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003088 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
3089 if (decl_ctx_die)
3090 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003091 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003092
3093 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
3094 if (abs_die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003095 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003096 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
3097 if (decl_ctx_die)
3098 return decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003099 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003100
3101 die = die.GetParent();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003102 }
3103 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003104 return DWARFDIE();
Greg Clayton2bc22f82011-09-30 03:20:47 +00003105}
3106
3107
Greg Clayton901c5ca2011-12-03 04:40:03 +00003108Symbol *
3109SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3110{
3111 Symbol *objc_class_symbol = NULL;
3112 if (m_obj_file)
3113 {
Greg Clayton3046e662013-07-10 01:23:25 +00003114 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003115 if (symtab)
3116 {
3117 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3118 eSymbolTypeObjCClass,
3119 Symtab::eDebugNo,
3120 Symtab::eVisibilityAny);
3121 }
3122 }
3123 return objc_class_symbol;
3124}
3125
Greg Claytonc7f03b62012-01-12 04:33:28 +00003126// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3127// then we can end up looking through all class types for a complete type and never find
3128// the full definition. We need to know if this attribute is supported, so we determine
3129// this here and cache th result. We also need to worry about the debug map DWARF file
3130// if we are doing darwin DWARF in .o file debugging.
3131bool
3132SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3133{
3134 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3135 {
3136 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3137 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3138 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3139 else
3140 {
3141 DWARFDebugInfo* debug_info = DebugInfo();
3142 const uint32_t num_compile_units = GetNumCompileUnits();
3143 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3144 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003145 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3146 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003147 {
3148 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3149 break;
3150 }
3151 }
3152 }
Greg Clayton1f746072012-08-29 21:13:06 +00003153 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003154 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3155 }
3156 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3157}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003158
3159// This function can be used when a DIE is found that is a forward declaration
3160// DIE and we want to try and find a type that has the complete definition.
3161TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003162SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
Greg Claytonc7f03b62012-01-12 04:33:28 +00003163 const ConstString &type_name,
3164 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003165{
3166
3167 TypeSP type_sp;
3168
Greg Claytonc7f03b62012-01-12 04:33:28 +00003169 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003170 return type_sp;
3171
3172 DIEArray die_offsets;
3173
3174 if (m_using_apple_tables)
3175 {
3176 if (m_apple_types_ap.get())
3177 {
3178 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003179 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003180 }
3181 }
3182 else
3183 {
3184 if (!m_indexed)
3185 Index ();
3186
3187 m_type_index.Find (type_name, die_offsets);
3188 }
3189
Greg Clayton901c5ca2011-12-03 04:40:03 +00003190 const size_t num_matches = die_offsets.size();
3191
Greg Clayton901c5ca2011-12-03 04:40:03 +00003192 if (num_matches)
3193 {
3194 DWARFDebugInfo* debug_info = DebugInfo();
3195 for (size_t i=0; i<num_matches; ++i)
3196 {
3197 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00003198 DWARFDIE type_die = debug_info->GetDIE (die_offset);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003199
3200 if (type_die)
3201 {
3202 bool try_resolving_type = false;
3203
3204 // Don't try and resolve the DIE we are looking for with the DIE itself!
3205 if (type_die != die)
3206 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003207 switch (type_die.Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003208 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003209 case DW_TAG_class_type:
3210 case DW_TAG_structure_type:
3211 try_resolving_type = true;
3212 break;
3213 default:
3214 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003215 }
3216 }
3217
3218 if (try_resolving_type)
3219 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003220 if (must_be_implementation && type_die.Supports_DW_AT_APPLE_objc_complete_type())
3221 try_resolving_type = type_die.GetAttributeValueAsUnsigned (DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003222
3223 if (try_resolving_type)
3224 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003225 Type *resolved_type = ResolveType (type_die, false);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003226 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3227 {
Ed Mastea0191d12013-10-17 20:42:56 +00003228 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 +00003229 die.GetID(),
Jim Ingham4af59612014-12-19 19:20:44 +00003230 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003231 type_die.GetID(),
3232 type_cu->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003233
Greg Claytonc7f03b62012-01-12 04:33:28 +00003234 if (die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003235 m_die_to_type[die.GetDIE()] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00003236 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003237 break;
3238 }
3239 }
3240 }
3241 }
3242 else
3243 {
3244 if (m_using_apple_tables)
3245 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003246 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3247 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003248 }
3249 }
3250
3251 }
3252 }
3253 return type_sp;
3254}
3255
Greg Claytona8022fa2012-04-24 21:22:41 +00003256
Greg Clayton80c26302012-02-05 06:12:47 +00003257//----------------------------------------------------------------------
3258// This function helps to ensure that the declaration contexts match for
3259// two different DIEs. Often times debug information will refer to a
3260// forward declaration of a type (the equivalent of "struct my_struct;".
3261// There will often be a declaration of that type elsewhere that has the
3262// full definition. When we go looking for the full type "my_struct", we
3263// will find one or more matches in the accelerator tables and we will
3264// then need to make sure the type was in the same declaration context
3265// as the original DIE. This function can efficiently compare two DIEs
3266// and will return true when the declaration context matches, and false
3267// when they don't.
3268//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00003269bool
Greg Clayton6071e6f2015-08-26 22:57:51 +00003270SymbolFileDWARF::DIEDeclContextsMatch (const DWARFDIE &die1,
3271 const DWARFDIE &die2)
Greg Clayton890ff562012-02-02 05:48:16 +00003272{
Greg Claytona8022fa2012-04-24 21:22:41 +00003273 if (die1 == die2)
3274 return true;
3275
Greg Clayton890ff562012-02-02 05:48:16 +00003276 DWARFDIECollection decl_ctx_1;
3277 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00003278 //The declaration DIE stack is a stack of the declaration context
3279 // DIEs all the way back to the compile unit. If a type "T" is
3280 // declared inside a class "B", and class "B" is declared inside
3281 // a class "A" and class "A" is in a namespace "lldb", and the
3282 // namespace is in a compile unit, there will be a stack of DIEs:
3283 //
3284 // [0] DW_TAG_class_type for "B"
3285 // [1] DW_TAG_class_type for "A"
3286 // [2] DW_TAG_namespace for "lldb"
3287 // [3] DW_TAG_compile_unit for the source file.
3288 //
3289 // We grab both contexts and make sure that everything matches
3290 // all the way back to the compiler unit.
3291
3292 // First lets grab the decl contexts for both DIEs
Greg Clayton6071e6f2015-08-26 22:57:51 +00003293 die1.GetDeclContextDIEs (decl_ctx_1);
3294 die2.GetDeclContextDIEs (decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00003295 // Make sure the context arrays have the same size, otherwise
3296 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00003297 const size_t count1 = decl_ctx_1.Size();
3298 const size_t count2 = decl_ctx_2.Size();
3299 if (count1 != count2)
3300 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00003301
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003302 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00003303 // compile unit. If they don't, then we are done.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003304 DWARFDIE decl_ctx_die1;
3305 DWARFDIE decl_ctx_die2;
Greg Clayton890ff562012-02-02 05:48:16 +00003306 size_t i;
3307 for (i=0; i<count1; i++)
3308 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003309 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3310 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3311 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
Greg Clayton890ff562012-02-02 05:48:16 +00003312 return false;
3313 }
Greg Clayton890ff562012-02-02 05:48:16 +00003314#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003315
3316 // Make sure the top item in the decl context die array is always
3317 // DW_TAG_compile_unit. If it isn't then something went wrong in
Greg Clayton5ce1a842015-08-27 18:09:44 +00003318 // the DWARFDIE::GetDeclContextDIEs() function...
Greg Clayton6071e6f2015-08-26 22:57:51 +00003319 assert (decl_ctx_1.GetDIEAtIndex (count1 - 1).Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003320
Greg Clayton890ff562012-02-02 05:48:16 +00003321#endif
3322 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00003323 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00003324 for (i=0; i<count1 - 1; i++)
3325 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003326 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i);
3327 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i);
3328 const char *name1 = decl_ctx_die1.GetName();
3329 const char *name2 = decl_ctx_die2.GetName();
Greg Clayton890ff562012-02-02 05:48:16 +00003330 // If the string was from a DW_FORM_strp, then the pointer will often
3331 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00003332 if (name1 == name2)
3333 continue;
3334
3335 // Name pointers are not equal, so only compare the strings
3336 // if both are not NULL.
3337 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00003338 {
Greg Clayton5569e642012-02-06 01:44:54 +00003339 // If the strings don't compare, we are done...
3340 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00003341 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00003342 }
3343 else
3344 {
3345 // One name was NULL while the other wasn't
3346 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003347 }
3348 }
Greg Clayton80c26302012-02-05 06:12:47 +00003349 // We made it through all of the checks and the declaration contexts
3350 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00003351 return true;
3352}
Greg Clayton220a0072011-12-09 08:48:30 +00003353
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003354
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003355TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00003356SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
3357{
3358 TypeSP type_sp;
3359
3360 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3361 if (dwarf_decl_ctx_count > 0)
3362 {
3363 const ConstString type_name(dwarf_decl_ctx[0].name);
3364 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
3365
3366 if (type_name)
3367 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003368 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00003369 if (log)
3370 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003371 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003372 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
3373 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3374 dwarf_decl_ctx.GetQualifiedName());
3375 }
3376
3377 DIEArray die_offsets;
3378
3379 if (m_using_apple_tables)
3380 {
3381 if (m_apple_types_ap.get())
3382 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003383 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
3384 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
3385 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00003386 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003387 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3388 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
3389 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003390 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003391 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3392 }
3393 else if (has_tag)
3394 {
3395 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003396 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00003397 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
3398 }
3399 else
3400 {
3401 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3402 }
3403 }
3404 }
3405 else
3406 {
3407 if (!m_indexed)
3408 Index ();
3409
3410 m_type_index.Find (type_name, die_offsets);
3411 }
3412
3413 const size_t num_matches = die_offsets.size();
3414
3415
Greg Claytona8022fa2012-04-24 21:22:41 +00003416 if (num_matches)
3417 {
3418 DWARFDebugInfo* debug_info = DebugInfo();
3419 for (size_t i=0; i<num_matches; ++i)
3420 {
3421 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00003422 DWARFDIE type_die = debug_info->GetDIE (die_offset);
Greg Claytona8022fa2012-04-24 21:22:41 +00003423
3424 if (type_die)
3425 {
3426 bool try_resolving_type = false;
3427
3428 // Don't try and resolve the DIE we are looking for with the DIE itself!
Greg Clayton6071e6f2015-08-26 22:57:51 +00003429 const dw_tag_t type_tag = type_die.Tag();
Greg Claytona8022fa2012-04-24 21:22:41 +00003430 // Make sure the tags match
3431 if (type_tag == tag)
3432 {
3433 // The tags match, lets try resolving this type
3434 try_resolving_type = true;
3435 }
3436 else
3437 {
3438 // The tags don't match, but we need to watch our for a
3439 // forward declaration for a struct and ("struct foo")
3440 // ends up being a class ("class foo { ... };") or
3441 // vice versa.
3442 switch (type_tag)
3443 {
3444 case DW_TAG_class_type:
3445 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3446 try_resolving_type = (tag == DW_TAG_structure_type);
3447 break;
3448 case DW_TAG_structure_type:
3449 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3450 try_resolving_type = (tag == DW_TAG_class_type);
3451 break;
3452 default:
3453 // Tags don't match, don't event try to resolve
3454 // using this type whose name matches....
3455 break;
3456 }
3457 }
3458
3459 if (try_resolving_type)
3460 {
3461 DWARFDeclContext type_dwarf_decl_ctx;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003462 type_die.GetDWARFDeclContext (type_dwarf_decl_ctx);
Greg Claytona8022fa2012-04-24 21:22:41 +00003463
3464 if (log)
3465 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003466 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003467 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
3468 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3469 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003470 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003471 type_dwarf_decl_ctx.GetQualifiedName());
3472 }
3473
3474 // Make sure the decl contexts match all the way up
3475 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
3476 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003477 Type *resolved_type = ResolveType (type_die, false);
Greg Claytona8022fa2012-04-24 21:22:41 +00003478 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3479 {
3480 type_sp = resolved_type->shared_from_this();
3481 break;
3482 }
3483 }
3484 }
3485 else
3486 {
3487 if (log)
3488 {
3489 std::string qualified_name;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003490 type_die.GetQualifiedName(qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00003491 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003492 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
3493 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3494 dwarf_decl_ctx.GetQualifiedName(),
Greg Clayton6071e6f2015-08-26 22:57:51 +00003495 type_die.GetOffset(),
Greg Claytona8022fa2012-04-24 21:22:41 +00003496 qualified_name.c_str());
3497 }
3498 }
3499 }
3500 else
3501 {
3502 if (m_using_apple_tables)
3503 {
3504 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3505 die_offset, type_name.GetCString());
3506 }
3507 }
3508
3509 }
3510 }
3511 }
3512 }
3513 return type_sp;
3514}
3515
Greg Claytona8022fa2012-04-24 21:22:41 +00003516TypeSP
Greg Clayton6071e6f2015-08-26 22:57:51 +00003517SymbolFileDWARF::ParseType (const SymbolContext& sc, const DWARFDIE &die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003518{
Greg Clayton196e8cd2015-08-17 20:31:46 +00003519 TypeSP type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003520
Greg Clayton6071e6f2015-08-26 22:57:51 +00003521 if (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003522 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003523 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
3524
3525 if (type_system)
Greg Clayton196e8cd2015-08-17 20:31:46 +00003526 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003527 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
3528 if (dwarf_ast)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003529 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00003530 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3531 type_sp = dwarf_ast->ParseTypeFromDWARF (sc, die, log, type_is_new_ptr);
3532 if (type_sp)
3533 {
3534 TypeList* type_list = GetTypeList();
3535 if (type_list)
3536 type_list->Insert(type_sp);
3537 }
Greg Clayton6071e6f2015-08-26 22:57:51 +00003538 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003539 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003540 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003541
3542 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003543}
3544
3545size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003546SymbolFileDWARF::ParseTypes
3547(
3548 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003549 const DWARFDIE &orig_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003550 bool parse_siblings,
3551 bool parse_children
3552)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003553{
3554 size_t types_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003555 DWARFDIE die = orig_die;
3556 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003557 {
3558 bool type_is_new = false;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003559 if (ParseType(sc, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003560 {
3561 if (type_is_new)
3562 ++types_added;
3563 }
3564
Greg Clayton6071e6f2015-08-26 22:57:51 +00003565 if (parse_children && die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003566 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003567 if (die.Tag() == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003568 {
3569 SymbolContext child_sc(sc);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003570 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
3571 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003572 }
3573 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003574 types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003575 }
3576
3577 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00003578 die = die.GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003579 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00003580 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003581 }
3582 return types_added;
3583}
3584
3585
3586size_t
3587SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3588{
3589 assert(sc.comp_unit && sc.function);
3590 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003591 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592 if (dwarf_cu)
3593 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003594 const dw_offset_t function_die_offset = sc.function->GetID();
3595 DWARFDIE function_die = dwarf_cu->GetDIE (function_die_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003596 if (function_die)
3597 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003598 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003599 }
3600 }
3601
3602 return functions_added;
3603}
3604
3605
3606size_t
3607SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3608{
3609 // At least a compile unit must be valid
3610 assert(sc.comp_unit);
3611 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003612 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003613 if (dwarf_cu)
3614 {
3615 if (sc.function)
3616 {
3617 dw_offset_t function_die_offset = sc.function->GetID();
Greg Clayton6071e6f2015-08-26 22:57:51 +00003618 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset);
3619 if (func_die && func_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003620 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003621 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003622 }
3623 }
3624 else
3625 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003626 DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3627 if (dwarf_cu_die && dwarf_cu_die.HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003628 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003629 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003630 }
3631 }
3632 }
3633
3634 return types_added;
3635}
3636
3637size_t
3638SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3639{
3640 if (sc.comp_unit != NULL)
3641 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003642 DWARFDebugInfo* info = DebugInfo();
3643 if (info == NULL)
3644 return 0;
3645
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003646 if (sc.function)
3647 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003648 DWARFDIE function_die = info->GetDIE(sc.function->GetID());
Greg Clayton9422dd62013-03-04 21:46:16 +00003649
Greg Clayton6071e6f2015-08-26 22:57:51 +00003650 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsUnsigned (DW_AT_low_pc, LLDB_INVALID_ADDRESS);
Greg Claytonc7bece562013-01-25 18:06:21 +00003651 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003652 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003653 const size_t num_variables = ParseVariables(sc, function_die.GetFirstChild(), func_lo_pc, true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00003654
Greg Claytone38a5ed2012-01-05 03:57:59 +00003655 // Let all blocks know they have parse all their variables
3656 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3657 return num_variables;
3658 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003659 }
3660 else if (sc.comp_unit)
3661 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003662 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID());
Greg Clayton9422dd62013-03-04 21:46:16 +00003663
3664 if (dwarf_cu == NULL)
3665 return 0;
3666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003667 uint32_t vars_added = 0;
3668 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3669
3670 if (variables.get() == NULL)
3671 {
3672 variables.reset(new VariableList());
3673 sc.comp_unit->SetVariableList(variables);
3674
Greg Claytond4a2b372011-09-12 23:21:58 +00003675 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00003676 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003677 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003678 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003679 {
3680 DWARFMappedHash::DIEInfoArray hash_data_array;
3681 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
3682 dwarf_cu->GetNextCompileUnitOffset(),
3683 hash_data_array))
3684 {
3685 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3686 }
3687 }
Greg Clayton7f995132011-10-04 22:41:51 +00003688 }
3689 else
3690 {
3691 // Index if we already haven't to make sure the compile units
3692 // get indexed and make their global DIE index list
3693 if (!m_indexed)
3694 Index ();
3695
3696 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
3697 dwarf_cu->GetNextCompileUnitOffset(),
3698 die_offsets);
3699 }
3700
3701 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003702 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003703 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003704 DWARFDebugInfo* debug_info = DebugInfo();
3705 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003706 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003707 const dw_offset_t die_offset = die_offsets[i];
Greg Clayton6071e6f2015-08-26 22:57:51 +00003708 DWARFDIE die = debug_info->GetDIE (die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00003709 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003710 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003711 VariableSP var_sp (ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
Greg Clayton95d87902011-11-11 03:16:25 +00003712 if (var_sp)
3713 {
3714 variables->AddVariableIfUnique (var_sp);
3715 ++vars_added;
3716 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003717 }
Greg Clayton95d87902011-11-11 03:16:25 +00003718 else
3719 {
3720 if (m_using_apple_tables)
3721 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003722 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00003723 }
3724 }
3725
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726 }
3727 }
3728 }
3729 return vars_added;
3730 }
3731 }
3732 return 0;
3733}
3734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735VariableSP
3736SymbolFileDWARF::ParseVariableDIE
3737(
3738 const SymbolContext& sc,
Greg Clayton6071e6f2015-08-26 22:57:51 +00003739 const DWARFDIE &die,
Greg Clayton016a95e2010-09-14 02:20:48 +00003740 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003741)
3742{
Greg Clayton6071e6f2015-08-26 22:57:51 +00003743 VariableSP var_sp;
3744 if (!die)
3745 return var_sp;
3746
3747 var_sp = m_die_to_variable_sp[die.GetDIE()];
Greg Clayton83c5cd92010-11-14 22:13:40 +00003748 if (var_sp)
3749 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003750
Greg Clayton6071e6f2015-08-26 22:57:51 +00003751 const dw_tag_t tag = die.Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00003752 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003753
3754 if ((tag == DW_TAG_variable) ||
3755 (tag == DW_TAG_constant) ||
3756 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003757 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003758 DWARFAttributes attributes;
3759 const size_t num_attributes = die.GetAttributes(attributes);
Greg Clayton7f995132011-10-04 22:41:51 +00003760 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761 {
Greg Clayton7f995132011-10-04 22:41:51 +00003762 const char *name = NULL;
3763 const char *mangled = NULL;
3764 Declaration decl;
3765 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00003766 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003767 DWARFExpression location(die.GetCU());
Greg Clayton7f995132011-10-04 22:41:51 +00003768 bool is_external = false;
3769 bool is_artificial = false;
3770 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003771 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00003772 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00003773 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00003774
3775 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003776 {
Greg Clayton7f995132011-10-04 22:41:51 +00003777 dw_attr_t attr = attributes.AttributeAtIndex(i);
3778 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00003779
Greg Clayton6071e6f2015-08-26 22:57:51 +00003780 if (attributes.ExtractFormValueAtIndex(i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003781 {
Greg Clayton7f995132011-10-04 22:41:51 +00003782 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003783 {
Greg Clayton7f995132011-10-04 22:41:51 +00003784 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3785 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3786 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton6071e6f2015-08-26 22:57:51 +00003787 case DW_AT_name: name = form_value.AsCString(); break;
Greg Clayton71415542012-12-08 00:24:40 +00003788 case DW_AT_linkage_name:
Greg Clayton6071e6f2015-08-26 22:57:51 +00003789 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(); break;
Greg Clayton54166af2014-11-22 01:58:59 +00003790 case DW_AT_type: type_uid = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00003791 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00003792 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003793 // If we have already found a DW_AT_location attribute, ignore this attribute.
3794 if (!has_explicit_location)
3795 {
3796 location_is_const_value_data = true;
3797 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00003798 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003799 if (DWARFFormValue::IsBlockForm(form_value.Form()))
3800 {
3801 // Retrieve the value as a block expression.
3802 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3803 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00003804 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003805 }
3806 else if (DWARFFormValue::IsDataForm(form_value.Form()))
3807 {
3808 // Retrieve the value as a data expression.
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003809 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3810 DWARFFormValue::GetFixedFormSizesForAddressSize (
3811 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3812 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003813 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003814 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Enrico Granata4ec130d2014-08-11 19:16:35 +00003815 if (data_length == 0)
3816 {
3817 const uint8_t *data_pointer = form_value.BlockData();
3818 if (data_pointer)
3819 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00003820 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00003821 }
3822 else if (DWARFFormValue::IsDataForm(form_value.Form()))
3823 {
3824 // we need to get the byte size of the type later after we create the variable
3825 const_value = form_value;
3826 }
3827 }
3828 else
3829 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003830 }
3831 else
3832 {
3833 // Retrieve the value as a string expression.
3834 if (form_value.Form() == DW_FORM_strp)
3835 {
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003836 DWARFFormValue::FixedFormSizes fixed_form_sizes =
3837 DWARFFormValue::GetFixedFormSizesForAddressSize (
3838 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(),
3839 attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003840 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
Tamas Berghammerb7c64652015-08-25 11:45:46 +00003841 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form());
Richard Mitton0a558352013-10-17 21:14:00 +00003842 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003843 }
3844 else
3845 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00003846 const char *str = form_value.AsCString();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003847 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
3848 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00003849 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003850 }
3851 }
3852 }
3853 break;
Greg Clayton7f995132011-10-04 22:41:51 +00003854 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00003856 location_is_const_value_data = false;
3857 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00003858 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003859 {
Ed Masteeeae7212013-10-24 20:43:47 +00003860 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00003861
3862 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3863 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00003864 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00003865 }
3866 else
3867 {
Ed Masteeeae7212013-10-24 20:43:47 +00003868 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00003869 const dw_offset_t debug_loc_offset = form_value.Unsigned();
3870
3871 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
3872 if (loc_list_length > 0)
3873 {
Richard Mitton0a558352013-10-17 21:14:00 +00003874 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00003875 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00003876 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00003877 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878 }
3879 }
Greg Clayton7f995132011-10-04 22:41:51 +00003880 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003881
Greg Clayton1c8ef472013-04-05 23:27:21 +00003882 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00003883 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00003884 case DW_AT_declaration:
3885 case DW_AT_description:
3886 case DW_AT_endianity:
3887 case DW_AT_segment:
3888 case DW_AT_start_scope:
3889 case DW_AT_visibility:
3890 default:
3891 case DW_AT_abstract_origin:
3892 case DW_AT_sibling:
3893 case DW_AT_specification:
3894 break;
3895 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003896 }
3897 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003898
Greg Clayton6071e6f2015-08-26 22:57:51 +00003899 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3900 const dw_tag_t parent_tag = die.GetParent().Tag();
3901 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 +00003902
Greg Clayton9e9f2192013-05-17 00:55:28 +00003903 ValueType scope = eValueTypeInvalid;
3904
Greg Clayton6071e6f2015-08-26 22:57:51 +00003905 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
Greg Clayton9e9f2192013-05-17 00:55:28 +00003906 SymbolContextScope * symbol_context_scope = NULL;
3907
Siva Chandra0783ab92015-03-24 18:32:27 +00003908 if (!mangled)
3909 {
3910 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
3911 // generate fully qualified names of global variables with commands like "frame var j".
3912 // For example, if j were an int variable holding a value 4 and declared in a namespace
3913 // B which in turn is contained in a namespace A, the command "frame var j" returns
3914 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
3915 // to generate a fully qualified name from the declaration context.
Greg Clayton6071e6f2015-08-26 22:57:51 +00003916 if (parent_tag == DW_TAG_compile_unit &&
3917 LanguageRuntime::LanguageIsCPlusPlus(die.GetLanguage()))
Siva Chandra0783ab92015-03-24 18:32:27 +00003918 {
3919 DWARFDeclContext decl_ctx;
3920
Greg Clayton6071e6f2015-08-26 22:57:51 +00003921 die.GetDWARFDeclContext(decl_ctx);
Siva Chandra0783ab92015-03-24 18:32:27 +00003922 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
3923 }
3924 }
3925
Greg Clayton9e9f2192013-05-17 00:55:28 +00003926 // DWARF doesn't specify if a DW_TAG_variable is a local, global
3927 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003928 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00003929 // a DW_OP_addr opcode _somewhere_ in the definition. I say
3930 // somewhere because clang likes to combine small global variables
3931 // into the same symbol and have locations like:
3932 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3933 // So if we don't have a DW_TAG_formal_parameter, we can look at
3934 // the location to see if it contains a DW_OP_addr opcode, and
3935 // then we can correctly classify our variables.
3936 if (tag == DW_TAG_formal_parameter)
3937 scope = eValueTypeVariableArgument;
3938 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003940 bool op_error = false;
3941 // Check if the location has a DW_OP_addr with any address value...
3942 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3943 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003944 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003945 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
3946 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00003947 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003948 StreamString strm;
3949 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Clayton6071e6f2015-08-26 22:57:51 +00003950 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 +00003951 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00003952 }
Greg Claytond1767f02011-12-08 02:13:16 +00003953
Greg Clayton9e9f2192013-05-17 00:55:28 +00003954 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3955 {
3956 if (is_external)
3957 scope = eValueTypeVariableGlobal;
3958 else
3959 scope = eValueTypeVariableStatic;
3960
3961
3962 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
3963
3964 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003965 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003966 // When leaving the DWARF in the .o files on darwin,
3967 // when we have a global variable that wasn't initialized,
3968 // the .o file might not have allocated a virtual
3969 // address for the global variable. In this case it will
3970 // have created a symbol for the global variable
3971 // that is undefined/data and external and the value will
3972 // be the byte size of the variable. When we do the
3973 // address map in SymbolFileDWARFDebugMap we rely on
3974 // having an address, we need to do some magic here
3975 // so we can get the correct address for our global
3976 // variable. The address for all of these entries
3977 // will be zero, and there will be an undefined symbol
3978 // in this object file, and the executable will have
3979 // a matching symbol with a good address. So here we
3980 // dig up the correct address and replace it in the
3981 // location for the variable, and set the variable's
3982 // symbol context scope to be that of the main executable
3983 // so the file address will resolve correctly.
3984 bool linked_oso_file_addr = false;
3985 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00003986 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003987 // we have a possible uninitialized extern global
3988 ConstString const_name(mangled ? mangled : name);
3989 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
3990 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003991 {
Greg Clayton3046e662013-07-10 01:23:25 +00003992 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00003993 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00003994 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00003995 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
3996 eSymbolTypeData,
3997 Symtab::eDebugYes,
3998 Symtab::eVisibilityExtern);
3999 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004000 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004001 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004002 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004003 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004004 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004005 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004006 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004007 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004008 linked_oso_file_addr = true;
4009 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004010 }
4011 }
4012 }
4013 }
4014 }
4015 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004016 }
Greg Clayton9422dd62013-03-04 21:46:16 +00004017
Greg Clayton9e9f2192013-05-17 00:55:28 +00004018 if (!linked_oso_file_addr)
4019 {
4020 // The DW_OP_addr is not zero, but it contains a .o file address which
4021 // needs to be linked up correctly.
4022 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
4023 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00004024 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004025 // Update the file address for this variable
4026 location.Update_DW_OP_addr (exe_file_addr);
4027 }
4028 else
4029 {
4030 // Variable didn't make it into the final executable
4031 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00004032 }
Greg Claytond1767f02011-12-08 02:13:16 +00004033 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004034 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00004035 }
4036 else
4037 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004038 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004039 }
Greg Clayton7f995132011-10-04 22:41:51 +00004040 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004041
4042 if (symbol_context_scope == NULL)
4043 {
4044 switch (parent_tag)
4045 {
4046 case DW_TAG_subprogram:
4047 case DW_TAG_inlined_subroutine:
4048 case DW_TAG_lexical_block:
4049 if (sc.function)
4050 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004051 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Clayton9e9f2192013-05-17 00:55:28 +00004052 if (symbol_context_scope == NULL)
4053 symbol_context_scope = sc.function;
4054 }
4055 break;
4056
4057 default:
4058 symbol_context_scope = sc.comp_unit;
4059 break;
4060 }
4061 }
4062
4063 if (symbol_context_scope)
4064 {
Enrico Granata4ec130d2014-08-11 19:16:35 +00004065 SymbolFileTypeSP type_sp(new SymbolFileType(*this, type_uid));
4066
4067 if (const_value.Form() && type_sp && type_sp->GetType())
Greg Clayton6071e6f2015-08-26 22:57:51 +00004068 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), die.GetCU()->GetAddressByteSize());
Enrico Granata4ec130d2014-08-11 19:16:35 +00004069
Greg Clayton6071e6f2015-08-26 22:57:51 +00004070 var_sp.reset (new Variable (die.GetID(),
Greg Clayton9e9f2192013-05-17 00:55:28 +00004071 name,
4072 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00004073 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00004074 scope,
4075 symbol_context_scope,
4076 &decl,
4077 location,
4078 is_external,
Paul Herman10bc1a42015-08-18 22:46:57 +00004079 is_artificial,
4080 is_static_member));
Greg Clayton9e9f2192013-05-17 00:55:28 +00004081
4082 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4083 }
4084 else
4085 {
4086 // Not ready to parse this variable yet. It might be a global
4087 // or static variable that is in a function scope and the function
4088 // in the symbol context wasn't filled in yet
4089 return var_sp;
4090 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004091 }
Greg Clayton7f995132011-10-04 22:41:51 +00004092 // Cache var_sp even if NULL (the variable was just a specification or
4093 // was missing vital information to be able to be displayed in the debugger
4094 // (missing location due to optimization, etc)) so we don't re-parse
4095 // this DIE over and over later...
Greg Clayton6071e6f2015-08-26 22:57:51 +00004096 m_die_to_variable_sp[die.GetDIE()] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004097 }
4098 return var_sp;
4099}
4100
Greg Claytonc662ec82011-06-17 22:10:16 +00004101
Greg Clayton6071e6f2015-08-26 22:57:51 +00004102DWARFDIE
Greg Claytonc662ec82011-06-17 22:10:16 +00004103SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
Greg Clayton6071e6f2015-08-26 22:57:51 +00004104 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004105{
4106 // Give the concrete function die specified by "func_die_offset", find the
4107 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4108 // to "spec_block_die_offset"
Greg Clayton6071e6f2015-08-26 22:57:51 +00004109 return FindBlockContainingSpecification (DebugInfo()->GetDIE (func_die_offset), spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004110}
4111
4112
Greg Clayton6071e6f2015-08-26 22:57:51 +00004113DWARFDIE
4114SymbolFileDWARF::FindBlockContainingSpecification(const DWARFDIE &die,
4115 dw_offset_t spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004116{
4117 if (die)
4118 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004119 switch (die.Tag())
Greg Claytonc662ec82011-06-17 22:10:16 +00004120 {
4121 case DW_TAG_subprogram:
4122 case DW_TAG_inlined_subroutine:
4123 case DW_TAG_lexical_block:
4124 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004125 if (die.GetAttributeValueAsReference (DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004126 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004127
Greg Clayton6071e6f2015-08-26 22:57:51 +00004128 if (die.GetAttributeValueAsReference (DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
Greg Claytonc662ec82011-06-17 22:10:16 +00004129 return die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004130 }
4131 break;
4132 }
4133
4134 // Give the concrete function die specified by "func_die_offset", find the
4135 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4136 // to "spec_block_die_offset"
Greg Clayton6071e6f2015-08-26 22:57:51 +00004137 for (DWARFDIE child_die = die.GetFirstChild(); child_die; child_die = child_die.GetSibling())
Greg Claytonc662ec82011-06-17 22:10:16 +00004138 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004139 DWARFDIE result_die = FindBlockContainingSpecification (child_die, spec_block_die_offset);
Greg Claytonc662ec82011-06-17 22:10:16 +00004140 if (result_die)
4141 return result_die;
4142 }
4143 }
4144
Greg Clayton6071e6f2015-08-26 22:57:51 +00004145 return DWARFDIE();
Greg Claytonc662ec82011-06-17 22:10:16 +00004146}
4147
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148size_t
Greg Clayton6071e6f2015-08-26 22:57:51 +00004149SymbolFileDWARF::ParseVariables (const SymbolContext& sc,
4150 const DWARFDIE &orig_die,
4151 const lldb::addr_t func_low_pc,
4152 bool parse_siblings,
4153 bool parse_children,
4154 VariableList* cc_variable_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004155{
Greg Clayton6071e6f2015-08-26 22:57:51 +00004156 if (!orig_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004157 return 0;
4158
Greg Claytonc662ec82011-06-17 22:10:16 +00004159 VariableListSP variable_list_sp;
4160
4161 size_t vars_added = 0;
Greg Clayton6071e6f2015-08-26 22:57:51 +00004162 DWARFDIE die = orig_die;
4163 while (die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004165 dw_tag_t tag = die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004166
4167 // Check to see if we have already parsed this variable or constant?
Greg Clayton6071e6f2015-08-26 22:57:51 +00004168 VariableSP var_sp = m_die_to_variable_sp[die.GetDIE()];
4169 if (var_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004170 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004171 if (cc_variable_list)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004172 cc_variable_list->AddVariableIfUnique (var_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173 }
4174 else
4175 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004176 // We haven't already parsed it, lets do that now.
4177 if ((tag == DW_TAG_variable) ||
4178 (tag == DW_TAG_constant) ||
4179 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004180 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004181 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004182 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004183 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
4184 dw_tag_t parent_tag = sc_parent_die.Tag();
Greg Claytonc662ec82011-06-17 22:10:16 +00004185 switch (parent_tag)
4186 {
4187 case DW_TAG_compile_unit:
4188 if (sc.comp_unit != NULL)
4189 {
4190 variable_list_sp = sc.comp_unit->GetVariableList(false);
4191 if (variable_list_sp.get() == NULL)
4192 {
4193 variable_list_sp.reset(new VariableList());
4194 sc.comp_unit->SetVariableList(variable_list_sp);
4195 }
4196 }
4197 else
4198 {
Daniel Malead01b2952012-11-29 21:49:15 +00004199 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 +00004200 sc_parent_die.GetID(),
4201 sc_parent_die.GetTagAsCString(),
4202 orig_die.GetID(),
4203 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004204 }
4205 break;
4206
4207 case DW_TAG_subprogram:
4208 case DW_TAG_inlined_subroutine:
4209 case DW_TAG_lexical_block:
4210 if (sc.function != NULL)
4211 {
4212 // Check to see if we already have parsed the variables for the given scope
4213
Greg Clayton6071e6f2015-08-26 22:57:51 +00004214 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004215 if (block == NULL)
4216 {
4217 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004218 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00004219 // to find the concrete block so we can correctly add the
4220 // variable to it
Greg Clayton6071e6f2015-08-26 22:57:51 +00004221 const DWARFDIE concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
4222 sc_parent_die.GetOffset());
Greg Claytonc662ec82011-06-17 22:10:16 +00004223 if (concrete_block_die)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004224 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die.GetID());
Greg Claytonc662ec82011-06-17 22:10:16 +00004225 }
4226
4227 if (block != NULL)
4228 {
4229 const bool can_create = false;
4230 variable_list_sp = block->GetBlockVariableList (can_create);
4231 if (variable_list_sp.get() == NULL)
4232 {
4233 variable_list_sp.reset(new VariableList());
4234 block->SetVariableList(variable_list_sp);
4235 }
4236 }
4237 }
4238 break;
4239
4240 default:
Daniel Malead01b2952012-11-29 21:49:15 +00004241 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 +00004242 orig_die.GetID(),
4243 orig_die.GetTagAsCString());
Greg Claytonc662ec82011-06-17 22:10:16 +00004244 break;
4245 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004246 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004247
4248 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004249 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004250 VariableSP var_sp (ParseVariableDIE(sc, die, func_low_pc));
Greg Clayton73bf5db2011-06-17 01:22:15 +00004251 if (var_sp)
4252 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004253 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004254 if (cc_variable_list)
4255 cc_variable_list->AddVariableIfUnique (var_sp);
4256 ++vars_added;
4257 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004258 }
4259 }
4260 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004261
4262 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4263
Greg Clayton6071e6f2015-08-26 22:57:51 +00004264 if (!skip_children && parse_children && die.HasChildren())
Greg Claytonc662ec82011-06-17 22:10:16 +00004265 {
Greg Clayton6071e6f2015-08-26 22:57:51 +00004266 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, true, cc_variable_list);
Greg Claytonc662ec82011-06-17 22:10:16 +00004267 }
4268
4269 if (parse_siblings)
Greg Clayton6071e6f2015-08-26 22:57:51 +00004270 die = die.GetSibling();
Greg Claytonc662ec82011-06-17 22:10:16 +00004271 else
Greg Clayton6071e6f2015-08-26 22:57:51 +00004272 die.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004273 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004274 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004275}
4276
4277//------------------------------------------------------------------
4278// PluginInterface protocol
4279//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004280ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004281SymbolFileDWARF::GetPluginName()
4282{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004283 return GetPluginNameStatic();
4284}
4285
4286uint32_t
4287SymbolFileDWARF::GetPluginVersion()
4288{
4289 return 1;
4290}
4291
4292void
Sean Callanancc427fa2011-07-30 02:42:06 +00004293SymbolFileDWARF::DumpIndexes ()
4294{
4295 StreamFile s(stdout, false);
4296
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004297 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00004298 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004299 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00004300 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
4301 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
4302 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
4303 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
4304 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
4305 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
4306 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00004307 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00004308}
4309
Greg Claytoncaab74e2012-01-28 00:48:57 +00004310
Greg Clayton1f746072012-08-29 21:13:06 +00004311SymbolFileDWARFDebugMap *
4312SymbolFileDWARF::GetDebugMapSymfile ()
4313{
4314 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
4315 {
4316 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
4317 if (module_sp)
4318 {
4319 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4320 if (sym_vendor)
4321 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
4322 }
4323 }
4324 return m_debug_map_symfile;
4325}
4326
Greg Claytoncaab74e2012-01-28 00:48:57 +00004327