blob: 3e5a8855024603b3c5032b890c3bc2b4f88d944a [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "DWARFCompileUnit.h"
61#include "DWARFDebugAbbrev.h"
62#include "DWARFDebugAranges.h"
63#include "DWARFDebugInfo.h"
64#include "DWARFDebugInfoEntry.h"
65#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
289SymbolFileDWARF::GetTypes (DWARFCompileUnit* cu,
290 const DWARFDebugInfoEntry *die,
291 dw_offset_t min_die_offset,
292 dw_offset_t max_die_offset,
293 uint32_t type_mask,
294 TypeSet &type_set)
295{
296 if (cu)
297 {
298 if (die)
299 {
300 const dw_offset_t die_offset = die->GetOffset();
301
302 if (die_offset >= max_die_offset)
303 return;
304
305 if (die_offset >= min_die_offset)
306 {
307 const dw_tag_t tag = die->Tag();
308
309 bool add_type = false;
310
311 switch (tag)
312 {
313 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
314 case DW_TAG_unspecified_type:
315 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
316 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
317 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
318 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
319 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
320 case DW_TAG_subroutine_type:
321 case DW_TAG_subprogram:
322 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
323 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
324 case DW_TAG_rvalue_reference_type:
325 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
326 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
327 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
328 }
329
330 if (add_type)
331 {
332 const bool assert_not_being_parsed = true;
333 Type *type = ResolveTypeUID (cu, die, assert_not_being_parsed);
334 if (type)
335 {
336 if (type_set.find(type) == type_set.end())
337 type_set.insert(type);
338 }
339 }
340 }
341
342 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
343 child_die != NULL;
344 child_die = child_die->GetSibling())
345 {
346 GetTypes (cu, child_die, min_die_offset, max_die_offset, type_mask, type_set);
347 }
348 }
349 }
350}
351
352size_t
353SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
354 uint32_t type_mask,
355 TypeList &type_list)
356
357{
358 TypeSet type_set;
359
360 CompileUnit *comp_unit = NULL;
361 DWARFCompileUnit* dwarf_cu = NULL;
362 if (sc_scope)
363 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
364
365 if (comp_unit)
366 {
367 dwarf_cu = GetDWARFCompileUnit(comp_unit);
368 if (dwarf_cu == 0)
369 return 0;
370 GetTypes (dwarf_cu,
371 dwarf_cu->DIE(),
372 dwarf_cu->GetOffset(),
373 dwarf_cu->GetNextCompileUnitOffset(),
374 type_mask,
375 type_set);
376 }
377 else
378 {
379 DWARFDebugInfo* info = DebugInfo();
380 if (info)
381 {
382 const size_t num_cus = info->GetNumCompileUnits();
383 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
384 {
385 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
386 if (dwarf_cu)
387 {
388 GetTypes (dwarf_cu,
389 dwarf_cu->DIE(),
390 0,
391 UINT32_MAX,
392 type_mask,
393 type_set);
394 }
395 }
396 }
397 }
398// if (m_using_apple_tables)
399// {
400// DWARFMappedHash::MemoryTable *apple_types = m_apple_types_ap.get();
401// if (apple_types)
402// {
403// apple_types->ForEach([this, &type_set, apple_types, type_mask](const DWARFMappedHash::DIEInfoArray &die_info_array) -> bool {
404//
405// for (auto die_info: die_info_array)
406// {
407// bool add_type = TagMatchesTypeMask (type_mask, 0);
408// if (!add_type)
409// {
410// dw_tag_t tag = die_info.tag;
411// if (tag == 0)
412// {
413// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_info.offset, NULL);
414// tag = die->Tag();
415// }
416// add_type = TagMatchesTypeMask (type_mask, tag);
417// }
418// if (add_type)
419// {
420// Type *type = ResolveTypeUID(die_info.offset);
421//
422// if (type_set.find(type) == type_set.end())
423// type_set.insert(type);
424// }
425// }
426// return true; // Keep iterating
427// });
428// }
429// }
430// else
431// {
432// if (!m_indexed)
433// Index ();
434//
435// m_type_index.ForEach([this, &type_set, type_mask](const char *name, uint32_t die_offset) -> bool {
436//
437// bool add_type = TagMatchesTypeMask (type_mask, 0);
438//
439// if (!add_type)
440// {
441// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_offset, NULL);
442// if (die)
443// {
444// const dw_tag_t tag = die->Tag();
445// add_type = TagMatchesTypeMask (type_mask, tag);
446// }
447// }
448//
449// if (add_type)
450// {
451// Type *type = ResolveTypeUID(die_offset);
452//
453// if (type_set.find(type) == type_set.end())
454// type_set.insert(type);
455// }
456// return true; // Keep iterating
457// });
458// }
459
Greg Claytona1e5dc82015-08-11 22:53:00 +0000460 std::set<CompilerType> clang_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000461 size_t num_types_added = 0;
462 for (Type *type : type_set)
463 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000464 CompilerType clang_type = type->GetClangForwardType();
Greg Clayton0fc4f312013-06-20 01:23:18 +0000465 if (clang_type_set.find(clang_type) == clang_type_set.end())
466 {
467 clang_type_set.insert(clang_type);
468 type_list.Insert (type->shared_from_this());
469 ++num_types_added;
470 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000471 }
472 return num_types_added;
473}
474
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000475
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476//----------------------------------------------------------------------
477// Gets the first parent that is a lexical block, function or inlined
478// subroutine, or compile unit.
479//----------------------------------------------------------------------
Greg Clayton8b4edba2015-08-14 20:02:05 +0000480const DWARFDebugInfoEntry *
481SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482{
483 const DWARFDebugInfoEntry *die;
484 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
485 {
486 dw_tag_t tag = die->Tag();
487
488 switch (tag)
489 {
490 case DW_TAG_compile_unit:
491 case DW_TAG_subprogram:
492 case DW_TAG_inlined_subroutine:
493 case DW_TAG_lexical_block:
494 return die;
495 }
496 }
497 return NULL;
498}
499
500
Greg Clayton450e3f32010-10-12 02:24:53 +0000501SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
502 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000503 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 +0000504 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000505 m_debug_map_symfile (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000507 m_data_debug_abbrev (),
508 m_data_debug_aranges (),
509 m_data_debug_frame (),
510 m_data_debug_info (),
511 m_data_debug_line (),
512 m_data_debug_loc (),
513 m_data_debug_ranges (),
514 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000515 m_data_apple_names (),
516 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000517 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 m_info(),
520 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000521 m_apple_names_ap (),
522 m_apple_types_ap (),
523 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000524 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000525 m_function_basename_index(),
526 m_function_fullname_index(),
527 m_function_method_index(),
528 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000529 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000530 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000531 m_type_index(),
532 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000533 m_indexed (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000534 m_using_apple_tables (false),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000535 m_fetched_external_modules (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000536 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000537 m_ranges(),
538 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539{
540}
541
542SymbolFileDWARF::~SymbolFileDWARF()
543{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000544}
545
546static const ConstString &
547GetDWARFMachOSegmentName ()
548{
549 static ConstString g_dwarf_section_name ("__DWARF");
550 return g_dwarf_section_name;
551}
552
Greg Claytone576ab22011-02-15 00:19:15 +0000553UniqueDWARFASTTypeMap &
554SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
555{
Greg Clayton1f746072012-08-29 21:13:06 +0000556 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000557 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
558 return m_unique_ast_type_map;
559}
560
Greg Clayton8b4edba2015-08-14 20:02:05 +0000561ClangASTContext &
Greg Clayton6beaaa62011-01-17 03:46:26 +0000562SymbolFileDWARF::GetClangASTContext ()
563{
Greg Clayton1f746072012-08-29 21:13:06 +0000564 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000565 return m_debug_map_symfile->GetClangASTContext ();
Greg Clayton6dc8d582015-08-18 22:32:36 +0000566 else
567 return m_obj_file->GetModule()->GetClangASTContext();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000568}
569
Greg Clayton8b4edba2015-08-14 20:02:05 +0000570TypeSystem *
571SymbolFileDWARF::GetTypeSystemForLanguage (LanguageType language)
572{
573 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
574 if (debug_map_symfile)
575 return debug_map_symfile->GetTypeSystemForLanguage (language);
576 else
Greg Clayton6dc8d582015-08-18 22:32:36 +0000577 return m_obj_file->GetModule()->GetTypeSystemForLanguage (language);
Greg Clayton8b4edba2015-08-14 20:02:05 +0000578}
579
Greg Clayton6beaaa62011-01-17 03:46:26 +0000580void
581SymbolFileDWARF::InitializeObject()
582{
Greg Claytone72dfb32012-02-24 01:59:29 +0000583 ModuleSP module_sp (m_obj_file->GetModule());
584 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000585 {
Greg Clayton3046e662013-07-10 01:23:25 +0000586 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000587
588 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
589
590 // Memory map the DWARF mach-o segment so we have everything mmap'ed
591 // to keep our heap memory usage down.
592 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000593 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000594 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000595 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000596 if (m_data_apple_names.GetByteSize() > 0)
597 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000598 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
599 if (m_apple_names_ap->IsValid())
600 m_using_apple_tables = true;
601 else
Greg Clayton7f995132011-10-04 22:41:51 +0000602 m_apple_names_ap.reset();
603 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000604 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000605 if (m_data_apple_types.GetByteSize() > 0)
606 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000607 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
608 if (m_apple_types_ap->IsValid())
609 m_using_apple_tables = true;
610 else
Greg Clayton7f995132011-10-04 22:41:51 +0000611 m_apple_types_ap.reset();
612 }
613
614 get_apple_namespaces_data();
615 if (m_data_apple_namespaces.GetByteSize() > 0)
616 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000617 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
618 if (m_apple_namespaces_ap->IsValid())
619 m_using_apple_tables = true;
620 else
Greg Clayton7f995132011-10-04 22:41:51 +0000621 m_apple_namespaces_ap.reset();
622 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000623
Greg Clayton5009f9d2011-10-27 17:55:14 +0000624 get_apple_objc_data();
625 if (m_data_apple_objc.GetByteSize() > 0)
626 {
627 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
628 if (m_apple_objc_ap->IsValid())
629 m_using_apple_tables = true;
630 else
631 m_apple_objc_ap.reset();
632 }
Greg Clayton6dc8d582015-08-18 22:32:36 +0000633
634 // Set the symbol file to this file if we don't have a debug map symbol
635 // file as our main symbol file. This allows the clang ASTContext to complete
636 // types using this symbol file when it needs to complete classes and structures.
637 if (GetDebugMapSymfile () == nullptr)
638 GetClangASTContext().SetSymbolFile(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639}
640
641bool
642SymbolFileDWARF::SupportedVersion(uint16_t version)
643{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000644 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
647uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000648SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649{
650 uint32_t abilities = 0;
651 if (m_obj_file != NULL)
652 {
653 const Section* section = NULL;
654 const SectionList *section_list = m_obj_file->GetSectionList();
655 if (section_list == NULL)
656 return 0;
657
658 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 uint64_t debug_info_file_size = 0;
660 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661
Greg Clayton6beaaa62011-01-17 03:46:26 +0000662 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663
664 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000665 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666
Greg Clayton4ceb9982010-07-21 22:54:26 +0000667 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 if (section != NULL)
669 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000670 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671
Greg Clayton4ceb9982010-07-21 22:54:26 +0000672 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000674 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 else
676 m_flags.Set (flagsGotDebugAbbrevData);
677
Greg Clayton4ceb9982010-07-21 22:54:26 +0000678 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000679 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 m_flags.Set (flagsGotDebugArangesData);
681
Greg Clayton4ceb9982010-07-21 22:54:26 +0000682 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000683 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 m_flags.Set (flagsGotDebugFrameData);
685
Greg Clayton4ceb9982010-07-21 22:54:26 +0000686 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000688 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 else
690 m_flags.Set (flagsGotDebugLineData);
691
Greg Clayton4ceb9982010-07-21 22:54:26 +0000692 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000693 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 m_flags.Set (flagsGotDebugLocData);
695
Greg Clayton4ceb9982010-07-21 22:54:26 +0000696 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000697 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 m_flags.Set (flagsGotDebugMacInfoData);
699
Greg Clayton4ceb9982010-07-21 22:54:26 +0000700 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000701 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 m_flags.Set (flagsGotDebugPubNamesData);
703
Greg Clayton4ceb9982010-07-21 22:54:26 +0000704 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000705 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 m_flags.Set (flagsGotDebugPubTypesData);
707
Greg Clayton4ceb9982010-07-21 22:54:26 +0000708 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000709 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 m_flags.Set (flagsGotDebugRangesData);
711
Greg Clayton4ceb9982010-07-21 22:54:26 +0000712 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000713 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 m_flags.Set (flagsGotDebugStrData);
715 }
Greg Clayton6c596612012-05-18 21:47:20 +0000716 else
717 {
718 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
719 if (symfile_dir_cstr)
720 {
721 if (strcasestr(symfile_dir_cstr, ".dsym"))
722 {
723 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
724 {
725 // We have a dSYM file that didn't have a any debug info.
726 // If the string table has a size of 1, then it was made from
727 // an executable with no debug info, or from an executable that
728 // was stripped.
729 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
730 if (section && section->GetFileSize() == 1)
731 {
732 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
733 }
734 }
735 }
736 }
737 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738
739 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
740 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
741
742 if (debug_line_file_size > 0)
743 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 }
745 return abilities;
746}
747
Ed Masteeeae7212013-10-24 20:43:47 +0000748const DWARFDataExtractor&
749SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750{
751 if (m_flags.IsClear (got_flag))
752 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000753 ModuleSP module_sp (m_obj_file->GetModule());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 m_flags.Set (got_flag);
Greg Clayton3046e662013-07-10 01:23:25 +0000755 const SectionList *section_list = module_sp->GetSectionList();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 if (section_list)
757 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000758 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
759 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 {
761 // See if we memory mapped the DWARF segment?
762 if (m_dwarf_data.GetByteSize())
763 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000764 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 }
766 else
767 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000768 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 data.Clear();
770 }
771 }
772 }
773 }
774 return data;
775}
776
Ed Masteeeae7212013-10-24 20:43:47 +0000777const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778SymbolFileDWARF::get_debug_abbrev_data()
779{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000780 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781}
782
Ed Masteeeae7212013-10-24 20:43:47 +0000783const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000784SymbolFileDWARF::get_debug_aranges_data()
785{
786 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
787}
788
Ed Masteeeae7212013-10-24 20:43:47 +0000789const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790SymbolFileDWARF::get_debug_frame_data()
791{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000792 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793}
794
Ed Masteeeae7212013-10-24 20:43:47 +0000795const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796SymbolFileDWARF::get_debug_info_data()
797{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000798 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799}
800
Ed Masteeeae7212013-10-24 20:43:47 +0000801const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802SymbolFileDWARF::get_debug_line_data()
803{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000804 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805}
806
Ed Masteeeae7212013-10-24 20:43:47 +0000807const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808SymbolFileDWARF::get_debug_loc_data()
809{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000810 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811}
812
Ed Masteeeae7212013-10-24 20:43:47 +0000813const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814SymbolFileDWARF::get_debug_ranges_data()
815{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000816 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817}
818
Ed Masteeeae7212013-10-24 20:43:47 +0000819const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820SymbolFileDWARF::get_debug_str_data()
821{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000822 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823}
824
Ed Masteeeae7212013-10-24 20:43:47 +0000825const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000826SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000827{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000828 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000829}
830
Ed Masteeeae7212013-10-24 20:43:47 +0000831const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000832SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000833{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000834 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000835}
836
Ed Masteeeae7212013-10-24 20:43:47 +0000837const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000838SymbolFileDWARF::get_apple_namespaces_data()
839{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000840 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
841}
842
Ed Masteeeae7212013-10-24 20:43:47 +0000843const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000844SymbolFileDWARF::get_apple_objc_data()
845{
846 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000847}
848
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849
850DWARFDebugAbbrev*
851SymbolFileDWARF::DebugAbbrev()
852{
853 if (m_abbr.get() == NULL)
854 {
Ed Masteeeae7212013-10-24 20:43:47 +0000855 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 if (debug_abbrev_data.GetByteSize() > 0)
857 {
858 m_abbr.reset(new DWARFDebugAbbrev());
859 if (m_abbr.get())
860 m_abbr->Parse(debug_abbrev_data);
861 }
862 }
863 return m_abbr.get();
864}
865
866const DWARFDebugAbbrev*
867SymbolFileDWARF::DebugAbbrev() const
868{
869 return m_abbr.get();
870}
871
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872
873DWARFDebugInfo*
874SymbolFileDWARF::DebugInfo()
875{
876 if (m_info.get() == NULL)
877 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000878 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
879 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880 if (get_debug_info_data().GetByteSize() > 0)
881 {
882 m_info.reset(new DWARFDebugInfo());
883 if (m_info.get())
884 {
885 m_info->SetDwarfData(this);
886 }
887 }
888 }
889 return m_info.get();
890}
891
892const DWARFDebugInfo*
893SymbolFileDWARF::DebugInfo() const
894{
895 return m_info.get();
896}
897
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000899SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900{
901 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000902 if (info)
903 {
904 if (GetDebugMapSymfile ())
905 {
906 // The debug map symbol file made the compile units for this DWARF
907 // file which is .o file with DWARF in it, and we should have
908 // only 1 compile unit which is at offset zero in the DWARF.
909 // TODO: modify to support LTO .o files where each .o file might
910 // have multiple DW_TAG_compile_unit tags.
Greg Clayton68c00bd2015-02-05 02:10:29 +0000911
912 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0).get();
913 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
914 dwarf_cu->SetUserData(comp_unit);
915 return dwarf_cu;
Greg Clayton1f746072012-08-29 21:13:06 +0000916 }
917 else
918 {
919 // Just a normal DWARF file whose user ID for the compile unit is
920 // the DWARF offset itself
Greg Clayton68c00bd2015-02-05 02:10:29 +0000921
922 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
923 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
924 dwarf_cu->SetUserData(comp_unit);
925 return dwarf_cu;
926
Greg Clayton1f746072012-08-29 21:13:06 +0000927 }
928 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 return NULL;
930}
931
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932
933DWARFDebugRanges*
934SymbolFileDWARF::DebugRanges()
935{
936 if (m_ranges.get() == NULL)
937 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000938 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
939 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 if (get_debug_ranges_data().GetByteSize() > 0)
941 {
942 m_ranges.reset(new DWARFDebugRanges());
943 if (m_ranges.get())
944 m_ranges->Extract(this);
945 }
946 }
947 return m_ranges.get();
948}
949
950const DWARFDebugRanges*
951SymbolFileDWARF::DebugRanges() const
952{
953 return m_ranges.get();
954}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955
Greg Clayton53eb1c22012-04-02 22:59:12 +0000956lldb::CompUnitSP
957SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000959 CompUnitSP cu_sp;
960 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000962 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
963 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000965 // We already parsed this compile unit, had out a shared pointer to it
966 cu_sp = comp_unit->shared_from_this();
967 }
968 else
969 {
Greg Clayton1f746072012-08-29 21:13:06 +0000970 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 {
Greg Clayton1f746072012-08-29 21:13:06 +0000972 // Let the debug map create the compile unit
973 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
974 dwarf_cu->SetUserData(cu_sp.get());
975 }
976 else
977 {
978 ModuleSP module_sp (m_obj_file->GetModule());
979 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000980 {
Greg Clayton1f746072012-08-29 21:13:06 +0000981 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
982 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000983 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000984 FileSpec cu_file_spec{cu_die->GetName(this, dwarf_cu), false};
985 if (cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +0000986 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000987 // If we have a full path to the compile unit, we don't need to resolve
988 // the file. This can be expensive e.g. when the source files are NFS mounted.
Chaoren Lin372e9062015-06-09 17:54:27 +0000989 if (cu_file_spec.IsRelative())
Greg Clayton53eb1c22012-04-02 22:59:12 +0000990 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000991 const char *cu_comp_dir{cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, nullptr)};
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000992 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
Greg Clayton1f746072012-08-29 21:13:06 +0000993 }
994
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000995 std::string remapped_file;
996 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file))
997 cu_file_spec.SetFile(remapped_file, false);
David Srbeckyd515e942015-07-08 14:00:04 +0000998 }
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000999
David Srbeckyd515e942015-07-08 14:00:04 +00001000 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001001
Jason Molendac7099582015-07-31 05:47:00 +00001002 bool is_optimized = dwarf_cu->GetIsOptimized ();
David Srbeckyd515e942015-07-08 14:00:04 +00001003 cu_sp.reset(new CompileUnit (module_sp,
1004 dwarf_cu,
1005 cu_file_spec,
1006 MakeUserID(dwarf_cu->GetOffset()),
Jason Molenda6ab659a2015-07-29 00:42:47 +00001007 cu_language,
1008 is_optimized));
David Srbeckyd515e942015-07-08 14:00:04 +00001009 if (cu_sp)
1010 {
1011 // If we just created a compile unit with an invalid file spec, try and get the
1012 // first entry in the supports files from the line table as that should be the
1013 // compile unit.
1014 if (!cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +00001015 {
David Srbeckyd515e942015-07-08 14:00:04 +00001016 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
1017 if (cu_file_spec)
1018 {
1019 (FileSpec &)(*cu_sp) = cu_file_spec;
1020 // Also fix the invalid file spec which was copied from the compile unit.
1021 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
1022 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001023 }
David Srbeckyd515e942015-07-08 14:00:04 +00001024
1025 dwarf_cu->SetUserData(cu_sp.get());
1026
1027 // Figure out the compile unit index if we weren't given one
1028 if (cu_idx == UINT32_MAX)
1029 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
1030
1031 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001032 }
1033 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 }
1035 }
1036 }
1037 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001038 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039}
1040
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041uint32_t
1042SymbolFileDWARF::GetNumCompileUnits()
1043{
1044 DWARFDebugInfo* info = DebugInfo();
1045 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047 return 0;
1048}
1049
1050CompUnitSP
1051SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
1052{
Greg Clayton53eb1c22012-04-02 22:59:12 +00001053 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 DWARFDebugInfo* info = DebugInfo();
1055 if (info)
1056 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001057 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
1058 if (dwarf_cu)
1059 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001061 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062}
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064Function *
Greg Clayton0fffff52010-09-24 05:15:53 +00001065SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001067 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
Greg Clayton8b4edba2015-08-14 20:02:05 +00001069 if (type_system)
1070 return type_system->ParseFunctionFromDWARF(sc, this, dwarf_cu, die);
1071 else
1072 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073}
1074
Greg Clayton9422dd62013-03-04 21:46:16 +00001075bool
1076SymbolFileDWARF::FixupAddress (Address &addr)
1077{
1078 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1079 if (debug_map_symfile)
1080 {
1081 return debug_map_symfile->LinkOSOAddress(addr);
1082 }
1083 // This is a normal DWARF file, no address fixups need to happen
1084 return true;
1085}
Greg Clayton1f746072012-08-29 21:13:06 +00001086lldb::LanguageType
1087SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1088{
1089 assert (sc.comp_unit);
1090 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1091 if (dwarf_cu)
1092 {
1093 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +00001094 if (die)
Dawn Perchikd0e87eb2015-06-17 22:30:24 +00001095 return DWARFCompileUnit::LanguageTypeFromDWARF(die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0));
Greg Clayton1f746072012-08-29 21:13:06 +00001096 }
1097 return eLanguageTypeUnknown;
1098}
1099
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100size_t
1101SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1102{
1103 assert (sc.comp_unit);
1104 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001105 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 if (dwarf_cu)
1107 {
1108 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001109 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001111 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 {
1113 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +00001114 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 {
1116 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
1117 ++functions_added;
1118 }
1119 }
1120 //FixupTypes();
1121 }
1122 return functions_added;
1123}
1124
1125bool
1126SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1127{
1128 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001129 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001130 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 {
Greg Claytonda2455b2012-11-01 17:28:37 +00001132 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
Greg Claytonda2455b2012-11-01 17:28:37 +00001134 if (cu_die)
1135 {
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +00001136 const char * cu_comp_dir = resolveCompDir(cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, nullptr));
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001137
Greg Claytonda2455b2012-11-01 17:28:37 +00001138 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139
Greg Claytonda2455b2012-11-01 17:28:37 +00001140 // All file indexes in DWARF are one based and a file of index zero is
1141 // supposed to be the compile unit itself.
1142 support_files.Append (*sc.comp_unit);
1143
1144 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 }
1147 return false;
1148}
1149
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001150bool
1151SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1152{
1153 assert (sc.comp_unit);
1154 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1155 if (dwarf_cu)
1156 {
1157 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1158 {
1159 UpdateExternalModuleListIfNeeded();
1160 for (const std::pair<uint64_t, const ClangModuleInfo> &external_type_module : m_external_type_modules)
1161 {
1162 imported_modules.push_back(external_type_module.second.m_name);
1163 }
1164 }
1165 }
1166 return false;
1167}
1168
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169struct ParseDWARFLineTableCallbackInfo
1170{
1171 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001172 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173};
1174
1175//----------------------------------------------------------------------
1176// ParseStatementTableCallback
1177//----------------------------------------------------------------------
1178static void
1179ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1180{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1182 {
1183 // Just started parsing the line table
1184 }
1185 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1186 {
1187 // Done parsing line table, nothing to do for the cleanup
1188 }
1189 else
1190 {
1191 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001192 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193
Greg Clayton9422dd62013-03-04 21:46:16 +00001194 // If this is our first time here, we need to create a
1195 // sequence container.
1196 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001198 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1199 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001201 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1202 state.address,
1203 state.line,
1204 state.column,
1205 state.file,
1206 state.is_stmt,
1207 state.basic_block,
1208 state.prologue_end,
1209 state.epilogue_begin,
1210 state.end_sequence);
1211 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001213 // First, put the current sequence into the line table.
1214 line_table->InsertSequence(info->sequence_ap.get());
1215 // Then, empty it to prepare for the next sequence.
1216 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 }
1219}
1220
1221bool
1222SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1223{
1224 assert (sc.comp_unit);
1225 if (sc.comp_unit->GetLineTable() != NULL)
1226 return true;
1227
Greg Clayton1f746072012-08-29 21:13:06 +00001228 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229 if (dwarf_cu)
1230 {
1231 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001232 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001234 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1235 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001237 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001238 if (line_table_ap.get())
1239 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001240 ParseDWARFLineTableCallbackInfo info;
1241 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001242 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001243 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001244 if (m_debug_map_symfile)
1245 {
1246 // We have an object file that has a line table with addresses
1247 // that are not linked. We need to link the line table and convert
1248 // the addresses that are relative to the .o file into addresses
1249 // for the main executable.
1250 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1251 }
1252 else
1253 {
1254 sc.comp_unit->SetLineTable(line_table_ap.release());
1255 return true;
1256 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001257 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
1259 }
1260 }
1261 return false;
1262}
1263
1264size_t
1265SymbolFileDWARF::ParseFunctionBlocks
1266(
1267 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001268 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001269 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 const DWARFDebugInfoEntry *die,
1271 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001272 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273)
1274{
1275 size_t blocks_added = 0;
1276 while (die != NULL)
1277 {
1278 dw_tag_t tag = die->Tag();
1279
1280 switch (tag)
1281 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001283 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284 case DW_TAG_lexical_block:
1285 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001286 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001287 if (tag == DW_TAG_subprogram)
1288 {
1289 // Skip any DW_TAG_subprogram DIEs that are inside
1290 // of a normal or inlined functions. These will be
1291 // parsed on their own as separate entities.
1292
1293 if (depth > 0)
1294 break;
1295
1296 block = parent_block;
1297 }
1298 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001299 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001300 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001301 parent_block->AddChild(block_sp);
1302 block = block_sp.get();
1303 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001304 DWARFDebugRanges::RangeList ranges;
1305 const char *name = NULL;
1306 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308 int decl_file = 0;
1309 int decl_line = 0;
1310 int decl_column = 0;
1311 int call_file = 0;
1312 int call_line = 0;
1313 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001314 if (die->GetDIENamesAndRanges (this,
1315 dwarf_cu,
1316 name,
1317 mangled_name,
1318 ranges,
1319 decl_file, decl_line, decl_column,
1320 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 {
1322 if (tag == DW_TAG_subprogram)
1323 {
1324 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001325 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001327 else if (tag == DW_TAG_inlined_subroutine)
1328 {
1329 // We get called here for inlined subroutines in two ways.
1330 // The first time is when we are making the Function object
1331 // for this inlined concrete instance. Since we're creating a top level block at
1332 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1333 // adjust the containing address.
1334 // The second time is when we are parsing the blocks inside the function that contains
1335 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1336 // function the offset will be for that function.
1337 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1338 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001339 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001340 }
1341 }
Greg Clayton103f3092015-01-15 03:04:37 +00001342
1343 const size_t num_ranges = ranges.GetSize();
1344 for (size_t i = 0; i<num_ranges; ++i)
1345 {
1346 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
1347 const addr_t range_base = range.GetRangeBase();
1348 if (range_base >= subprogram_low_pc)
1349 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1350 else
1351 {
1352 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",
1353 block->GetID(),
1354 range_base,
1355 range.GetRangeEnd(),
1356 subprogram_low_pc);
1357 }
1358 }
1359 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360
1361 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1362 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001363 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001365 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1366 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367
Greg Clayton7b0992d2013-04-18 22:45:39 +00001368 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001370 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1371 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001373 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 }
1375
1376 ++blocks_added;
1377
Greg Claytondd7feaf2011-08-12 17:54:33 +00001378 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001380 blocks_added += ParseFunctionBlocks (sc,
1381 block,
1382 dwarf_cu,
1383 die->GetFirstChild(),
1384 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001385 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386 }
1387 }
1388 }
1389 break;
1390 default:
1391 break;
1392 }
1393
Greg Claytondd7feaf2011-08-12 17:54:33 +00001394 // Only parse siblings of the block if we are not at depth zero. A depth
1395 // of zero indicates we are currently parsing the top level
1396 // DW_TAG_subprogram DIE
1397
1398 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001400 else
1401 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 }
1403 return blocks_added;
1404}
1405
Greg Claytonf0705c82011-10-22 03:33:13 +00001406bool
Greg Claytonc4ffd662013-03-08 01:37:30 +00001407SymbolFileDWARF::ClassOrStructIsVirtual (DWARFCompileUnit* dwarf_cu,
1408 const DWARFDebugInfoEntry *parent_die)
1409{
1410 if (parent_die)
1411 {
1412 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1413 {
1414 dw_tag_t tag = die->Tag();
1415 bool check_virtuality = false;
1416 switch (tag)
1417 {
1418 case DW_TAG_inheritance:
1419 case DW_TAG_subprogram:
1420 check_virtuality = true;
1421 break;
1422 default:
1423 break;
1424 }
1425 if (check_virtuality)
1426 {
1427 if (die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_virtuality, 0) != 0)
1428 return true;
1429 }
1430 }
1431 }
1432 return false;
1433}
1434
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001436SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437{
Greg Clayton8b4edba2015-08-14 20:02:05 +00001438 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00001440 DWARFDebugInfo* debug_info = DebugInfo();
1441 if (debug_info)
1442 {
1443 DWARFCompileUnitSP cu_sp;
1444 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1445 if (die)
1446 {
1447 TypeSystem *type_system = GetTypeSystemForLanguage(cu_sp->GetLanguageType());
1448 if (type_system)
1449 return type_system->GetClangDeclContextContainingTypeUID(this, type_uid);
1450 }
1451 }
Sean Callanan72e49402011-08-05 23:43:37 +00001452 }
1453 return NULL;
1454}
1455
1456clang::DeclContext*
1457SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1458{
Greg Clayton81c22f62011-10-19 18:09:39 +00001459 if (UserIDMatches(type_uid))
Greg Clayton8b4edba2015-08-14 20:02:05 +00001460 {
1461 DWARFDebugInfo* debug_info = DebugInfo();
1462 if (debug_info)
1463 {
1464 DWARFCompileUnitSP cu_sp;
1465 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1466 if (die)
1467 {
1468 TypeSystem *type_system = GetTypeSystemForLanguage(cu_sp->GetLanguageType());
1469 if (type_system)
1470 return type_system->GetClangDeclContextForTypeUID(this, sc, type_uid);
1471 }
1472 }
1473 }
Greg Clayton81c22f62011-10-19 18:09:39 +00001474 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475}
1476
1477Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001478SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479{
Greg Clayton81c22f62011-10-19 18:09:39 +00001480 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001482 DWARFDebugInfo* debug_info = DebugInfo();
1483 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001484 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001485 DWARFCompileUnitSP cu_sp;
1486 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001487 const bool assert_not_being_parsed = true;
1488 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001489 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 }
1491 return NULL;
1492}
1493
Greg Claytoncab36a32011-12-08 05:16:30 +00001494Type*
1495SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001496{
Greg Claytoncab36a32011-12-08 05:16:30 +00001497 if (die != NULL)
1498 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001499 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00001500 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001501 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001502 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1503 die->GetOffset(),
1504 DW_TAG_value_to_name(die->Tag()),
1505 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001506
Greg Claytoncab36a32011-12-08 05:16:30 +00001507 // We might be coming in in the middle of a type tree (a class
1508 // withing a class, an enum within a class), so parse any needed
1509 // parent DIEs before we get to this one...
1510 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1511 switch (decl_ctx_die->Tag())
1512 {
1513 case DW_TAG_structure_type:
1514 case DW_TAG_union_type:
1515 case DW_TAG_class_type:
1516 {
1517 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001518 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001519 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00001520 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1521 die->GetOffset(),
1522 DW_TAG_value_to_name(die->Tag()),
1523 die->GetName(this, cu),
1524 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00001525//
1526// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
1527// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
1528// {
1529// if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001530// GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton219cf312012-03-30 00:51:13 +00001531// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1532// die->GetOffset(),
1533// DW_TAG_value_to_name(die->Tag()),
1534// die->GetName(this, cu),
1535// decl_ctx_die->GetOffset());
1536// // Ask the type to complete itself if it already hasn't since if we
1537// // want a function (method or static) from a class, the class must
1538// // create itself and add it's own methods and class functions.
1539// if (parent_type)
1540// parent_type->GetClangFullType();
1541// }
Greg Claytoncab36a32011-12-08 05:16:30 +00001542 }
1543 break;
1544
1545 default:
1546 break;
1547 }
1548 return ResolveType (cu, die);
1549 }
1550 return NULL;
1551}
1552
Greg Clayton6beaaa62011-01-17 03:46:26 +00001553// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1554// SymbolFileDWARF objects to detect if this DWARF file is the one that
1555// can resolve a clang_type.
1556bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001557SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &clang_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001558{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001559 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001560 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001561 return die != NULL;
1562}
1563
1564
Greg Clayton57ee3062013-07-11 22:46:58 +00001565bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00001566SymbolFileDWARF::CompleteType (CompilerType &clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001567{
1568 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Claytona1e5dc82015-08-11 22:53:00 +00001569 CompilerType clang_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001570 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001571 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001572 {
1573 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00001574 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00001575 }
1576 // Once we start resolving this type, remove it from the forward declaration
1577 // map in case anyone child members or other types require this type to get resolved.
1578 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1579 // are done.
Greg Clayton57ee3062013-07-11 22:46:58 +00001580 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001581
Greg Clayton450e3f32010-10-12 02:24:53 +00001582 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton53eb1c22012-04-02 22:59:12 +00001583 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001584 Type *type = m_die_to_type.lookup (die);
1585
Greg Clayton5160ce52013-03-27 23:08:40 +00001586 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001587 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001588 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00001589 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Claytond61c0fc2012-04-23 22:55:20 +00001590 MakeUserID(die->GetOffset()),
Greg Clayton8b4edba2015-08-14 20:02:05 +00001591 DW_TAG_value_to_name(die->Tag()),
Greg Claytond61c0fc2012-04-23 22:55:20 +00001592 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001593 assert (clang_type);
1594 DWARFDebugInfoEntry::Attributes attributes;
1595
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001596
Greg Clayton8b4edba2015-08-14 20:02:05 +00001597 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001598
Greg Clayton8b4edba2015-08-14 20:02:05 +00001599 if (type_system)
Greg Clayton6dc8d582015-08-18 22:32:36 +00001600 return type_system->CompleteTypeFromDWARF (this, dwarf_cu, die, type, clang_type);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001601
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001602 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001603}
1604
Greg Clayton8b4edba2015-08-14 20:02:05 +00001605void
1606SymbolFileDWARF::ClearDIEBeingParsed (const DWARFDebugInfoEntry* type_die)
1607{
1608 if (type_die)
1609 {
1610 if (m_die_to_type.lookup (type_die) == DIE_IS_BEING_PARSED)
1611 m_die_to_type[type_die] = nullptr;
1612 }
1613}
1614Type*
1615SymbolFileDWARF::GetCachedTypeForDIE (const DWARFDebugInfoEntry* type_die) const
1616{
1617 if (type_die)
1618 {
1619 Type *type = m_die_to_type.lookup (type_die);
1620 if (type != DIE_IS_BEING_PARSED)
1621 return type;
1622 }
1623 return nullptr;
1624}
1625
Greg Claytonc685f8e2010-09-15 04:15:46 +00001626Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001627SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001628{
1629 if (type_die != NULL)
1630 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001631 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001632
Greg Claytonc685f8e2010-09-15 04:15:46 +00001633 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00001634 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001635
Greg Clayton24739922010-10-13 03:15:28 +00001636 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001637 {
1638 if (type != DIE_IS_BEING_PARSED)
1639 return type;
1640
1641 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00001642 type_die->GetOffset(),
1643 DW_TAG_value_to_name(type_die->Tag()),
1644 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00001645
1646 }
1647 else
1648 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001649 }
1650 return NULL;
1651}
1652
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00001654SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655{
1656 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001657 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658 {
1659 // The symbol vendor doesn't know about this compile unit, we
1660 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00001661 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001663 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664}
1665
Greg Clayton8b4edba2015-08-14 20:02:05 +00001666size_t
1667SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets)
1668{
1669 method_die_offsets.clear();
1670 if (m_using_apple_tables)
1671 {
1672 if (m_apple_objc_ap.get())
1673 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
1674 }
1675 else
1676 {
1677 if (!m_indexed)
1678 Index ();
1679
1680 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1681 }
1682 return method_die_offsets.size();
1683}
1684
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00001686SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687{
Greg Clayton72310352013-02-23 04:12:47 +00001688 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00001690 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691
Greg Clayton81c22f62011-10-19 18:09:39 +00001692 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00001694 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00001695
1696 if (sc.function)
1697 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001698 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00001699 return true;
1700 }
1701
1702 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703}
1704
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001705void
1706SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
1707{
1708 if (m_fetched_external_modules)
1709 return;
1710 m_fetched_external_modules = true;
1711
1712 DWARFDebugInfo * debug_info = DebugInfo();
1713 debug_info->GetNumCompileUnits();
1714
1715 const uint32_t num_compile_units = GetNumCompileUnits();
1716 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1717 {
1718 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1719
1720 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
1721 if (die && die->HasChildren() == false)
1722 {
1723 const uint64_t name_strp = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_name, UINT64_MAX);
1724 const uint64_t dwo_path_strp = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_GNU_dwo_name, UINT64_MAX);
1725
1726 if (name_strp != UINT64_MAX)
1727 {
1728 if (m_external_type_modules.find(dwo_path_strp) == m_external_type_modules.end())
1729 {
1730 const char *name = get_debug_str_data().PeekCStr(name_strp);
1731 const char *dwo_path = get_debug_str_data().PeekCStr(dwo_path_strp);
1732 if (name || dwo_path)
1733 {
1734 ModuleSP module_sp;
1735 if (dwo_path)
1736 {
1737 ModuleSpec dwo_module_spec;
1738 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
1739 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
1740 //printf ("Loading dwo = '%s'\n", dwo_path);
1741 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
1742 }
1743
1744 if (dwo_path_strp != LLDB_INVALID_UID)
1745 {
1746 m_external_type_modules[dwo_path_strp] = ClangModuleInfo { ConstString(name), module_sp };
1747 }
1748 else
1749 {
1750 // This hack should be removed promptly once clang emits both.
1751 m_external_type_modules[name_strp] = ClangModuleInfo { ConstString(name), module_sp };
1752 }
1753 }
1754 }
1755 }
1756 }
1757 }
1758}
Greg Clayton2501e5e2015-01-15 02:59:20 +00001759
1760SymbolFileDWARF::GlobalVariableMap &
1761SymbolFileDWARF::GetGlobalAranges()
1762{
1763 if (!m_global_aranges_ap)
1764 {
1765 m_global_aranges_ap.reset (new GlobalVariableMap());
1766
1767 ModuleSP module_sp = GetObjectFile()->GetModule();
1768 if (module_sp)
1769 {
1770 const size_t num_cus = module_sp->GetNumCompileUnits();
1771 for (size_t i = 0; i < num_cus; ++i)
1772 {
1773 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1774 if (cu_sp)
1775 {
1776 VariableListSP globals_sp = cu_sp->GetVariableList(true);
1777 if (globals_sp)
1778 {
1779 const size_t num_globals = globals_sp->GetSize();
1780 for (size_t g = 0; g < num_globals; ++g)
1781 {
1782 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1783 if (var_sp && !var_sp->GetLocationIsConstantValueData())
1784 {
1785 const DWARFExpression &location = var_sp->LocationExpression();
1786 Value location_result;
1787 Error error;
1788 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
1789 {
1790 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
1791 {
1792 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
1793 lldb::addr_t byte_size = 1;
1794 if (var_sp->GetType())
1795 byte_size = var_sp->GetType()->GetByteSize();
1796 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
1797 }
1798 }
1799 }
1800 }
1801 }
1802 }
1803 }
1804 }
1805 m_global_aranges_ap->Sort();
1806 }
1807 return *m_global_aranges_ap;
1808}
1809
1810
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811uint32_t
1812SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1813{
1814 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00001815 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001816 static_cast<void*>(so_addr.GetSection().get()),
1817 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00001819 if (resolve_scope & ( eSymbolContextCompUnit |
1820 eSymbolContextFunction |
1821 eSymbolContextBlock |
1822 eSymbolContextLineEntry |
1823 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824 {
1825 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1826
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001828 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001829 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001830 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00001831 if (cu_offset == DW_INVALID_OFFSET)
1832 {
1833 // Global variables are not in the compile unit address ranges. The only way to
1834 // currently find global variables is to iterate over the .debug_pubnames or the
1835 // __apple_names table and find all items in there that point to DW_TAG_variable
1836 // DIEs and then find the address that matches.
1837 if (resolve_scope & eSymbolContextVariable)
1838 {
1839 GlobalVariableMap &map = GetGlobalAranges();
1840 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
1841 if (entry && entry->data)
1842 {
1843 Variable *variable = entry->data;
1844 SymbolContextScope *scc = variable->GetSymbolContextScope();
1845 if (scc)
1846 {
1847 scc->CalculateSymbolContext(&sc);
1848 sc.variable = variable;
1849 }
1850 return sc.GetResolvedMask();
1851 }
1852 }
1853 }
1854 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001856 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00001857 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1858 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001860 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001861 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001863 resolved |= eSymbolContextCompUnit;
1864
Greg Clayton6ab80132012-12-12 17:30:52 +00001865 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001866 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1867 {
1868 DWARFDebugInfoEntry *function_die = NULL;
1869 DWARFDebugInfoEntry *block_die = NULL;
1870 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001872 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
1873 }
1874 else
1875 {
1876 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
1877 }
1878
1879 if (function_die != NULL)
1880 {
1881 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
1882 if (sc.function == NULL)
1883 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
1884 }
1885 else
1886 {
1887 // We might have had a compile unit that had discontiguous
1888 // address ranges where the gaps are symbols that don't have
1889 // any debug info. Discontiguous compile unit address ranges
1890 // should only happen when there aren't other functions from
1891 // other compile units in these gaps. This helps keep the size
1892 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00001893 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00001894 }
1895
1896 if (sc.function != NULL)
1897 {
1898 resolved |= eSymbolContextFunction;
1899
1900 if (resolve_scope & eSymbolContextBlock)
1901 {
1902 Block& block = sc.function->GetBlock (true);
1903
1904 if (block_die != NULL)
1905 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
1906 else
1907 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
1908 if (sc.block)
1909 resolved |= eSymbolContextBlock;
1910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001911 }
1912 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001913
1914 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
1915 {
1916 LineTable *line_table = sc.comp_unit->GetLineTable();
1917 if (line_table != NULL)
1918 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001919 // And address that makes it into this function should be in terms
1920 // of this debug file if there is no debug map, or it will be an
1921 // address in the .o file which needs to be fixed up to be in terms
1922 // of the debug map executable. Either way, calling FixupAddress()
1923 // will work for us.
1924 Address exe_so_addr (so_addr);
1925 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00001926 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001927 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00001928 {
1929 resolved |= eSymbolContextLineEntry;
1930 }
1931 }
Greg Clayton6ab80132012-12-12 17:30:52 +00001932 }
1933 }
1934
1935 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
1936 {
1937 // We might have had a compile unit that had discontiguous
1938 // address ranges where the gaps are symbols that don't have
1939 // any debug info. Discontiguous compile unit address ranges
1940 // should only happen when there aren't other functions from
1941 // other compile units in these gaps. This helps keep the size
1942 // of the aranges down.
1943 sc.comp_unit = NULL;
1944 resolved &= ~eSymbolContextCompUnit;
1945 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00001947 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001949 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
1950 cu_offset,
1951 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 }
1953 }
1954 }
1955 }
1956 }
1957 return resolved;
1958}
1959
1960
1961
1962uint32_t
1963SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1964{
1965 const uint32_t prev_size = sc_list.GetSize();
1966 if (resolve_scope & eSymbolContextCompUnit)
1967 {
1968 DWARFDebugInfo* debug_info = DebugInfo();
1969 if (debug_info)
1970 {
1971 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00001972 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973
Greg Clayton53eb1c22012-04-02 22:59:12 +00001974 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001976 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00001977 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00001978 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 +00001979 if (check_inlines || file_spec_matches_cu_file_spec)
1980 {
1981 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00001982 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00001983 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001985 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986
Greg Clayton526a4ae2012-05-16 22:09:01 +00001987 // If we are looking for inline functions only and we don't
1988 // find it in the support files, we are done.
1989 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00001991 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1992 if (file_idx == UINT32_MAX)
1993 continue;
1994 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001995
Greg Clayton526a4ae2012-05-16 22:09:01 +00001996 if (line != 0)
1997 {
1998 LineTable *line_table = sc.comp_unit->GetLineTable();
1999
2000 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002002 // We will have already looked up the file index if
2003 // we are searching for inline entries.
2004 if (!check_inlines)
2005 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006
Greg Clayton526a4ae2012-05-16 22:09:01 +00002007 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002009 uint32_t found_line;
2010 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2011 found_line = sc.line_entry.line;
2012
2013 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002015 sc.function = NULL;
2016 sc.block = NULL;
2017 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002018 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002019 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2020 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002021 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002022 DWARFDebugInfoEntry *function_die = NULL;
2023 DWARFDebugInfoEntry *block_die = NULL;
2024 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025
Greg Clayton526a4ae2012-05-16 22:09:01 +00002026 if (function_die != NULL)
2027 {
2028 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2029 if (sc.function == NULL)
2030 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2031 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032
Greg Clayton526a4ae2012-05-16 22:09:01 +00002033 if (sc.function != NULL)
2034 {
2035 Block& block = sc.function->GetBlock (true);
2036
2037 if (block_die != NULL)
2038 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Jason Molenda60db6e42014-10-16 01:40:16 +00002039 else if (function_die != NULL)
Greg Clayton526a4ae2012-05-16 22:09:01 +00002040 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2041 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 }
2043 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002044
Greg Clayton526a4ae2012-05-16 22:09:01 +00002045 sc_list.Append(sc);
2046 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2047 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048 }
2049 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002050 else if (file_spec_matches_cu_file_spec && !check_inlines)
2051 {
2052 // only append the context if we aren't looking for inline call sites
2053 // by file and line and if the file spec matches that of the compile unit
2054 sc_list.Append(sc);
2055 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056 }
2057 else if (file_spec_matches_cu_file_spec && !check_inlines)
2058 {
2059 // only append the context if we aren't looking for inline call sites
2060 // by file and line and if the file spec matches that of the compile unit
2061 sc_list.Append(sc);
2062 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002063
Greg Clayton526a4ae2012-05-16 22:09:01 +00002064 if (!check_inlines)
2065 break;
2066 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067 }
2068 }
2069 }
2070 }
2071 return sc_list.GetSize() - prev_size;
2072}
2073
2074void
2075SymbolFileDWARF::Index ()
2076{
2077 if (m_indexed)
2078 return;
2079 m_indexed = true;
2080 Timer scoped_timer (__PRETTY_FUNCTION__,
2081 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00002082 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002083
2084 DWARFDebugInfo* debug_info = DebugInfo();
2085 if (debug_info)
2086 {
2087 uint32_t cu_idx = 0;
2088 const uint32_t num_compile_units = GetNumCompileUnits();
2089 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2090 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002091 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092
Greg Clayton53eb1c22012-04-02 22:59:12 +00002093 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094
Greg Clayton53eb1c22012-04-02 22:59:12 +00002095 dwarf_cu->Index (cu_idx,
2096 m_function_basename_index,
2097 m_function_fullname_index,
2098 m_function_method_index,
2099 m_function_selector_index,
2100 m_objc_class_selectors_index,
2101 m_global_index,
2102 m_type_index,
2103 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104
2105 // Keep memory down by clearing DIEs if this generate function
2106 // caused them to be parsed
2107 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002108 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002109 }
2110
Greg Claytond4a2b372011-09-12 23:21:58 +00002111 m_function_basename_index.Finalize();
2112 m_function_fullname_index.Finalize();
2113 m_function_method_index.Finalize();
2114 m_function_selector_index.Finalize();
2115 m_objc_class_selectors_index.Finalize();
2116 m_global_index.Finalize();
2117 m_type_index.Finalize();
2118 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002119
Greg Clayton24739922010-10-13 03:15:28 +00002120#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002121 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002122 s.Printf ("DWARF index for '%s':",
2123 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002124 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2125 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2126 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2127 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2128 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2129 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002130 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00002131 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002132#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002133 }
2134}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002135
2136bool
2137SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2138{
2139 if (namespace_decl == NULL)
2140 {
2141 // Invalid namespace decl which means we aren't matching only things
2142 // in this symbol file, so return true to indicate it matches this
2143 // symbol file.
2144 return true;
2145 }
2146
2147 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2148
2149 if (namespace_ast == NULL)
2150 return true; // No AST in the "namespace_decl", return true since it
2151 // could then match any symbol file, including this one
2152
2153 if (namespace_ast == GetClangASTContext().getASTContext())
2154 return true; // The ASTs match, return true
2155
2156 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00002157 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00002158
2159 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002160 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002161
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002162 return false;
2163}
2164
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002165uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002166SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002167{
Greg Clayton5160ce52013-03-27 23:08:40 +00002168 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002169
2170 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002171 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002172 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2173 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002174 static_cast<const void*>(namespace_decl),
2175 append, max_matches);
2176
Sean Callanan213fdb82011-10-13 01:49:10 +00002177 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00002178 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002179
Greg Claytonc685f8e2010-09-15 04:15:46 +00002180 DWARFDebugInfo* info = DebugInfo();
2181 if (info == NULL)
2182 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183
2184 // If we aren't appending the results to this list, then clear the list
2185 if (!append)
2186 variables.Clear();
2187
2188 // Remember how many variables are in the list before we search in case
2189 // we are appending the results to a variable list.
2190 const uint32_t original_size = variables.GetSize();
2191
Greg Claytond4a2b372011-09-12 23:21:58 +00002192 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002193
Greg Clayton97fbc342011-10-20 22:30:33 +00002194 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002195 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002196 if (m_apple_names_ap.get())
2197 {
2198 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00002199 llvm::StringRef basename;
2200 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002201
Jim Inghamfa39bb42014-10-25 00:33:55 +00002202 if (!CPPLanguageRuntime::ExtractContextAndIdentifier(name_cstr, context, basename))
2203 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002204
Jim Inghamfa39bb42014-10-25 00:33:55 +00002205 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00002206 }
Greg Clayton7f995132011-10-04 22:41:51 +00002207 }
2208 else
2209 {
2210 // Index the DWARF if we haven't already
2211 if (!m_indexed)
2212 Index ();
2213
2214 m_global_index.Find (name, die_offsets);
2215 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002216
Greg Clayton437a1352012-04-09 22:43:43 +00002217 const size_t num_die_matches = die_offsets.size();
2218 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002219 {
Greg Clayton7f995132011-10-04 22:41:51 +00002220 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002221 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002222 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002223
Greg Claytond4a2b372011-09-12 23:21:58 +00002224 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002225 DWARFCompileUnit* dwarf_cu = NULL;
2226 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002227 bool done = false;
2228 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002229 {
2230 const dw_offset_t die_offset = die_offsets[i];
2231 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002232
Greg Clayton95d87902011-11-11 03:16:25 +00002233 if (die)
2234 {
Greg Clayton437a1352012-04-09 22:43:43 +00002235 switch (die->Tag())
2236 {
2237 default:
2238 case DW_TAG_subprogram:
2239 case DW_TAG_inlined_subroutine:
2240 case DW_TAG_try_block:
2241 case DW_TAG_catch_block:
2242 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002243
Greg Clayton437a1352012-04-09 22:43:43 +00002244 case DW_TAG_variable:
2245 {
2246 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002247
Greg Clayton8b4edba2015-08-14 20:02:05 +00002248 if (namespace_decl)
2249 {
2250 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
2251
2252 if (!type_system->DIEIsInNamespace (namespace_decl, this, dwarf_cu, die))
2253 continue;
2254 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255
Greg Clayton437a1352012-04-09 22:43:43 +00002256 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257
Greg Clayton437a1352012-04-09 22:43:43 +00002258 if (variables.GetSize() - original_size >= max_matches)
2259 done = true;
2260 }
2261 break;
2262 }
Greg Clayton95d87902011-11-11 03:16:25 +00002263 }
2264 else
2265 {
2266 if (m_using_apple_tables)
2267 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002268 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2269 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002270 }
2271 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002272 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002273 }
2274
2275 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002276 const uint32_t num_matches = variables.GetSize() - original_size;
2277 if (log && num_matches > 0)
2278 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002279 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00002280 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002281 name.GetCString(),
2282 static_cast<const void*>(namespace_decl),
2283 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00002284 num_matches);
2285 }
2286 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287}
2288
2289uint32_t
2290SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2291{
Greg Clayton5160ce52013-03-27 23:08:40 +00002292 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002293
Greg Clayton21f2a492011-10-06 00:09:08 +00002294 if (log)
2295 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002296 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002297 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002298 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002299 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002300 }
2301
Greg Claytonc685f8e2010-09-15 04:15:46 +00002302 DWARFDebugInfo* info = DebugInfo();
2303 if (info == NULL)
2304 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305
2306 // If we aren't appending the results to this list, then clear the list
2307 if (!append)
2308 variables.Clear();
2309
2310 // Remember how many variables are in the list before we search in case
2311 // we are appending the results to a variable list.
2312 const uint32_t original_size = variables.GetSize();
2313
Greg Clayton7f995132011-10-04 22:41:51 +00002314 DIEArray die_offsets;
2315
Greg Clayton97fbc342011-10-20 22:30:33 +00002316 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002317 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002318 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002319 {
2320 DWARFMappedHash::DIEInfoArray hash_data_array;
2321 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2322 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2323 }
Greg Clayton7f995132011-10-04 22:41:51 +00002324 }
2325 else
2326 {
2327 // Index the DWARF if we haven't already
2328 if (!m_indexed)
2329 Index ();
2330
2331 m_global_index.Find (regex, die_offsets);
2332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333
Greg Claytonc685f8e2010-09-15 04:15:46 +00002334 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002335 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002336 assert (sc.module_sp);
2337
Greg Claytond4a2b372011-09-12 23:21:58 +00002338 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002339 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002340 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002341 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002342 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002343 DWARFDebugInfo* debug_info = DebugInfo();
2344 for (size_t i=0; i<num_matches; ++i)
2345 {
2346 const dw_offset_t die_offset = die_offsets[i];
2347 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002348
2349 if (die)
2350 {
2351 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002352
Greg Clayton95d87902011-11-11 03:16:25 +00002353 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002354
Greg Clayton95d87902011-11-11 03:16:25 +00002355 if (variables.GetSize() - original_size >= max_matches)
2356 break;
2357 }
2358 else
2359 {
2360 if (m_using_apple_tables)
2361 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002362 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2363 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002364 }
2365 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002366 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002367 }
2368
2369 // Return the number of variable that were appended to the list
2370 return variables.GetSize() - original_size;
2371}
2372
Greg Claytonaa044962011-10-13 00:59:38 +00002373
Jim Ingham4cda6e02011-10-07 22:23:45 +00002374bool
2375SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2376 DWARFCompileUnit *&dwarf_cu,
Pavel Labatha73d6572015-03-13 10:22:00 +00002377 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00002378 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002379{
Greg Claytonaa044962011-10-13 00:59:38 +00002380 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Pavel Labatha73d6572015-03-13 10:22:00 +00002381 return ResolveFunction (dwarf_cu, die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00002382}
2383
2384
2385bool
2386SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2387 const DWARFDebugInfoEntry *die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002388 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00002389 SymbolContextList& sc_list)
2390{
Greg Clayton9e315582011-09-02 04:03:59 +00002391 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002392
2393 if (die == NULL)
2394 return false;
2395
Jim Ingham4cda6e02011-10-07 22:23:45 +00002396 // If we were passed a die that is not a function, just return false...
Pavel Labatha73d6572015-03-13 10:22:00 +00002397 if (! (die->Tag() == DW_TAG_subprogram || (include_inlines && die->Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002398 return false;
2399
2400 const DWARFDebugInfoEntry* inlined_die = NULL;
2401 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002402 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002403 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002404
Jim Ingham4cda6e02011-10-07 22:23:45 +00002405 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002406 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002407 if (die->Tag() == DW_TAG_subprogram)
2408 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002409 }
2410 }
Jason Molenda60db6e42014-10-16 01:40:16 +00002411 assert (die && die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002412 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002413 {
2414 Address addr;
2415 // Parse all blocks if needed
2416 if (inlined_die)
2417 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00002418 Block &function_block = sc.function->GetBlock (true);
2419 sc.block = function_block.FindBlockByID (MakeUserID(inlined_die->GetOffset()));
2420 if (sc.block == NULL)
2421 sc.block = function_block.FindBlockByID (inlined_die->GetOffset());
2422 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002423 addr.Clear();
2424 }
2425 else
2426 {
2427 sc.block = NULL;
2428 addr = sc.function->GetAddressRange().GetBaseAddress();
2429 }
2430
2431 if (addr.IsValid())
2432 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002433 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002434 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002435 }
2436 }
2437
Greg Claytonaa044962011-10-13 00:59:38 +00002438 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002439}
2440
Greg Clayton7f995132011-10-04 22:41:51 +00002441void
2442SymbolFileDWARF::FindFunctions (const ConstString &name,
2443 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002444 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002445 SymbolContextList& sc_list)
2446{
Greg Claytond4a2b372011-09-12 23:21:58 +00002447 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002448 if (name_to_die.Find (name, die_offsets))
2449 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002450 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002451 }
2452}
2453
2454
2455void
2456SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2457 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00002458 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002459 SymbolContextList& sc_list)
2460{
2461 DIEArray die_offsets;
2462 if (name_to_die.Find (regex, die_offsets))
2463 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002464 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002465 }
2466}
2467
2468
2469void
2470SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2471 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00002472 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002473 SymbolContextList& sc_list)
2474{
2475 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002476 DWARFMappedHash::DIEInfoArray hash_data_array;
2477 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002478 {
Greg Claytond1767f02011-12-08 02:13:16 +00002479 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00002480 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002481 }
2482}
2483
2484void
2485SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00002486 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00002487 SymbolContextList& sc_list)
2488{
2489 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002490 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002491 {
Greg Clayton7f995132011-10-04 22:41:51 +00002492 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002493 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002494 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002495 const dw_offset_t die_offset = die_offsets[i];
Pavel Labatha73d6572015-03-13 10:22:00 +00002496 ResolveFunction (die_offset, dwarf_cu, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002497 }
2498 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002499}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002500
Jim Ingham4cda6e02011-10-07 22:23:45 +00002501bool
2502SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2503 const DWARFCompileUnit *dwarf_cu,
2504 uint32_t name_type_mask,
2505 const char *partial_name,
2506 const char *base_name_start,
2507 const char *base_name_end)
2508{
Greg Claytonfbea0f62012-11-15 18:05:43 +00002509 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes:
2510 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002511 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00002512 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (dwarf_cu, die);
2513
2514 if (decl_ctx_die == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00002515 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00002516
2517 const dw_tag_t decl_ctx_tag = decl_ctx_die->Tag();
2518
2519 bool is_cxx_method = (decl_ctx_tag == DW_TAG_structure_type) || (decl_ctx_tag == DW_TAG_class_type);
Greg Claytonf0705c82011-10-22 03:33:13 +00002520
Greg Claytonfbea0f62012-11-15 18:05:43 +00002521 if (name_type_mask == eFunctionNameTypeMethod)
2522 {
2523 if (is_cxx_method == false)
2524 return false;
2525 }
2526
2527 if (name_type_mask == eFunctionNameTypeBase)
2528 {
2529 if (is_cxx_method == true)
2530 return false;
2531 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002532 }
2533
2534 // Now we need to check whether the name we got back for this type matches the extra specifications
2535 // that were in the name we're looking up:
2536 if (base_name_start != partial_name || *base_name_end != '\0')
2537 {
2538 // First see if the stuff to the left matches the full name. To do that let's see if
2539 // we can pull out the mips linkage name attribute:
2540
2541 Mangled best_name;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002542 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonfbea0f62012-11-15 18:05:43 +00002543 DWARFFormValue form_value;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002544 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2545 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
Greg Clayton71415542012-12-08 00:24:40 +00002546 if (idx == UINT32_MAX)
2547 idx = attributes.FindAttributeIndex(DW_AT_linkage_name);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002548 if (idx != UINT32_MAX)
2549 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002550 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2551 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00002552 const char *mangled_name = form_value.AsCString(&get_debug_str_data());
2553 if (mangled_name)
2554 best_name.SetValue (ConstString(mangled_name), true);
2555 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002556 }
Greg Claytonfbea0f62012-11-15 18:05:43 +00002557
2558 if (!best_name)
2559 {
2560 idx = attributes.FindAttributeIndex(DW_AT_name);
2561 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value))
2562 {
2563 const char *name = form_value.AsCString(&get_debug_str_data());
2564 best_name.SetValue (ConstString(name), false);
2565 }
2566 }
2567
Greg Claytonddaf6a72015-07-08 22:32:23 +00002568 const LanguageType cu_language = const_cast<DWARFCompileUnit *>(dwarf_cu)->GetLanguageType();
2569 if (best_name.GetDemangledName(cu_language))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002570 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00002571 const char *demangled = best_name.GetDemangledName(cu_language).GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002572 if (demangled)
2573 {
2574 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00002575 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
2576 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002577 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00002578 else
2579 {
2580 // Sort out the case where our name is something like "Process::Destroy" and the match is
2581 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
2582 // namespace boundaries...
2583
2584 if (partial_name[0] == ':' && partial_name[1] == ':')
2585 {
2586 // The partial name was already on a namespace boundary so all matches are good.
2587 return true;
2588 }
2589 else if (partial_in_demangled == demangled)
2590 {
2591 // They both start the same, so this is an good match.
2592 return true;
2593 }
2594 else
2595 {
2596 if (partial_in_demangled - demangled == 1)
2597 {
2598 // Only one character difference, can't be a namespace boundary...
2599 return false;
2600 }
2601 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
2602 {
2603 // We are on a namespace boundary, so this is also good.
2604 return true;
2605 }
2606 else
2607 return false;
2608 }
2609 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002610 }
2611 }
2612 }
2613
2614 return true;
2615}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002616
Greg Clayton0c5cd902010-06-28 21:30:43 +00002617uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002618SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002619 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002620 uint32_t name_type_mask,
2621 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002622 bool append,
2623 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002624{
2625 Timer scoped_timer (__PRETTY_FUNCTION__,
2626 "SymbolFileDWARF::FindFunctions (name = '%s')",
2627 name.AsCString());
2628
Greg Clayton43fe2172013-04-03 02:00:15 +00002629 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
2630 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
2631
Greg Clayton5160ce52013-03-27 23:08:40 +00002632 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002633
2634 if (log)
2635 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002636 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002637 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2638 name.GetCString(),
2639 name_type_mask,
2640 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002641 }
2642
Greg Clayton0c5cd902010-06-28 21:30:43 +00002643 // If we aren't appending the results to this list, then clear the list
2644 if (!append)
2645 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002646
2647 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00002648 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002649
2650 // If name is empty then we won't find anything.
2651 if (name.IsEmpty())
2652 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002653
2654 // Remember how many sc_list are in the list before we search in case
2655 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002656
Jim Ingham4cda6e02011-10-07 22:23:45 +00002657 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00002658
2659 const uint32_t original_size = sc_list.GetSize();
2660
Jim Ingham4cda6e02011-10-07 22:23:45 +00002661 DWARFDebugInfo* info = DebugInfo();
2662 if (info == NULL)
2663 return 0;
2664
Greg Claytonaa044962011-10-13 00:59:38 +00002665 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton43fe2172013-04-03 02:00:15 +00002666 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00002667 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002668 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002669 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002670 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002671
2672 DIEArray die_offsets;
2673
2674 uint32_t num_matches = 0;
2675
Greg Clayton43fe2172013-04-03 02:00:15 +00002676 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002677 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002678 // If they asked for the full name, match what they typed. At some point we may
2679 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2680 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002681 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002682 for (uint32_t i = 0; i < num_matches; i++)
2683 {
Greg Clayton95d87902011-11-11 03:16:25 +00002684 const dw_offset_t die_offset = die_offsets[i];
2685 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00002686 if (die)
2687 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00002688 if (namespace_decl)
2689 {
2690 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
2691
2692 if (!type_system->DIEIsInNamespace (namespace_decl, this, dwarf_cu, die))
2693 continue;
2694 }
2695
Greg Clayton43fe2172013-04-03 02:00:15 +00002696 if (resolved_dies.find(die) == resolved_dies.end())
2697 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002698 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002699 resolved_dies.insert(die);
2700 }
Greg Claytonaa044962011-10-13 00:59:38 +00002701 }
Greg Clayton95d87902011-11-11 03:16:25 +00002702 else
2703 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002704 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2705 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002706 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002707 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002708 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002709
2710 if (name_type_mask & eFunctionNameTypeSelector)
2711 {
2712 if (namespace_decl && *namespace_decl)
2713 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00002714
Greg Clayton43fe2172013-04-03 02:00:15 +00002715 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2716 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2717 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00002718
Greg Clayton43fe2172013-04-03 02:00:15 +00002719 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00002720 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002721 const dw_offset_t die_offset = die_offsets[i];
2722 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2723 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00002724 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002725 const char *die_name = die->GetName(this, dwarf_cu);
2726 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00002727 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002728 if (resolved_dies.find(die) == resolved_dies.end())
2729 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002730 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002731 resolved_dies.insert(die);
2732 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002733 }
2734 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002735 else
2736 {
2737 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2738 die_offset, name_cstr);
2739 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002740 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002741 die_offsets.clear();
2742 }
2743
2744 if (((name_type_mask & eFunctionNameTypeMethod) && !namespace_decl) || name_type_mask & eFunctionNameTypeBase)
2745 {
2746 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2747 // extract the base name, look that up, and if there is any other information in the name we were
2748 // passed in we have to post-filter based on that.
2749
2750 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2751 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2752
2753 for (uint32_t i = 0; i < num_matches; i++)
2754 {
2755 const dw_offset_t die_offset = die_offsets[i];
2756 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2757 if (die)
2758 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00002759 if (namespace_decl)
2760 {
2761 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
2762
2763 if (!type_system->DIEIsInNamespace (namespace_decl, this, dwarf_cu, die))
2764 continue;
2765 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002766
2767 // If we get to here, the die is good, and we should add it:
2768 if (resolved_dies.find(die) == resolved_dies.end())
Pavel Labatha73d6572015-03-13 10:22:00 +00002769 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002770 {
2771 bool keep_die = true;
2772 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
2773 {
2774 // We are looking for either basenames or methods, so we need to
2775 // trim out the ones we won't want by looking at the type
2776 SymbolContext sc;
2777 if (sc_list.GetLastContext(sc))
2778 {
2779 if (sc.block)
2780 {
2781 // We have an inlined function
2782 }
2783 else if (sc.function)
2784 {
2785 Type *type = sc.function->GetType();
2786
Sean Callananc370a8a2013-09-18 22:59:55 +00002787 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00002788 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002789 clang::DeclContext* decl_ctx = GetClangDeclContextContainingTypeUID (type->GetID());
2790 if (decl_ctx->isRecord())
Greg Clayton43fe2172013-04-03 02:00:15 +00002791 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002792 if (name_type_mask & eFunctionNameTypeBase)
2793 {
2794 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2795 keep_die = false;
2796 }
2797 }
2798 else
2799 {
2800 if (name_type_mask & eFunctionNameTypeMethod)
2801 {
2802 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
2803 keep_die = false;
2804 }
Greg Clayton43fe2172013-04-03 02:00:15 +00002805 }
2806 }
2807 else
2808 {
Sean Callananc370a8a2013-09-18 22:59:55 +00002809 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
2810 die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00002811 }
2812 }
2813 }
2814 }
2815 if (keep_die)
2816 resolved_dies.insert(die);
2817 }
2818 }
2819 else
2820 {
2821 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2822 die_offset, name_cstr);
2823 }
2824 }
2825 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002826 }
2827 }
Greg Clayton7f995132011-10-04 22:41:51 +00002828 }
2829 else
2830 {
2831
2832 // Index the DWARF if we haven't already
2833 if (!m_indexed)
2834 Index ();
2835
Greg Clayton7f995132011-10-04 22:41:51 +00002836 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00002837 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002838 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002839
Ed Mastefc7baa02013-09-09 18:00:45 +00002840 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00002841 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00002842 // If we didn't find any functions in the global namespace try
2843 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00002844 // functions that have a namespace but keep functions which
2845 // have an anonymous namespace
2846 // TODO: The arch in the object file isn't correct for MSVC
2847 // binaries on windows, we should find a way to make it
2848 // correct and handle those symbols as well.
Matt Kopecd6089962013-05-10 17:53:48 +00002849 if (sc_list.GetSize() == 0)
2850 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002851 ArchSpec arch;
2852 if (!namespace_decl &&
2853 GetObjectFile()->GetArchitecture(arch) &&
2854 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
2855 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00002856 {
Robert Flackeb83fab2015-05-15 18:59:59 +00002857 SymbolContextList temp_sc_list;
2858 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00002859 SymbolContext sc;
2860 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
2861 {
2862 if (temp_sc_list.GetContextAtIndex(i, sc))
2863 {
Matt Kopeca189d492013-05-10 22:55:24 +00002864 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
2865 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00002866 // Mangled names on Linux and FreeBSD are of the form:
2867 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00002868 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
2869 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00002870 {
2871 sc_list.Append(sc);
2872 }
2873 }
2874 }
2875 }
2876 }
Matt Kopecd6089962013-05-10 17:53:48 +00002877 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002878 DIEArray die_offsets;
2879 DWARFCompileUnit *dwarf_cu = NULL;
2880
Greg Clayton43fe2172013-04-03 02:00:15 +00002881 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002882 {
Greg Clayton43fe2172013-04-03 02:00:15 +00002883 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002884 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002885 {
Greg Claytonaa044962011-10-13 00:59:38 +00002886 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2887 if (die)
2888 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00002889 if (namespace_decl)
2890 {
2891 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
2892
2893 if (!type_system->DIEIsInNamespace (namespace_decl, this, dwarf_cu, die))
2894 continue;
2895 }
2896
Greg Claytonaa044962011-10-13 00:59:38 +00002897 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00002898 if (resolved_dies.find(die) == resolved_dies.end())
2899 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002900 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002901 resolved_dies.insert(die);
2902 }
Greg Claytonaa044962011-10-13 00:59:38 +00002903 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002904 }
2905 die_offsets.clear();
2906 }
2907
Greg Clayton43fe2172013-04-03 02:00:15 +00002908 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002909 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002910 if (namespace_decl && *namespace_decl)
2911 return 0; // no methods in namespaces
2912
Greg Clayton43fe2172013-04-03 02:00:15 +00002913 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002914 {
Greg Claytonaa044962011-10-13 00:59:38 +00002915 for (uint32_t i = 0; i < num_base; i++)
2916 {
2917 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2918 if (die)
2919 {
Greg Claytonaa044962011-10-13 00:59:38 +00002920 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00002921 if (resolved_dies.find(die) == resolved_dies.end())
2922 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002923 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00002924 resolved_dies.insert(die);
2925 }
Greg Claytonaa044962011-10-13 00:59:38 +00002926 }
2927 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002928 }
2929 die_offsets.clear();
2930 }
Greg Clayton7f995132011-10-04 22:41:51 +00002931
Greg Clayton43fe2172013-04-03 02:00:15 +00002932 if ((name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002933 {
Pavel Labatha73d6572015-03-13 10:22:00 +00002934 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002935 }
2936
Greg Clayton4d01ace2011-09-29 16:58:15 +00002937 }
2938
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002939 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002940 const uint32_t num_matches = sc_list.GetSize() - original_size;
2941
2942 if (log && num_matches > 0)
2943 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002944 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00002945 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00002946 name.GetCString(),
2947 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00002948 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00002949 append,
2950 num_matches);
2951 }
2952 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002953}
2954
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002955uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00002956SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002957{
2958 Timer scoped_timer (__PRETTY_FUNCTION__,
2959 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2960 regex.GetText());
2961
Greg Clayton5160ce52013-03-27 23:08:40 +00002962 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00002963
2964 if (log)
2965 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002966 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002967 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2968 regex.GetText(),
2969 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002970 }
2971
2972
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002973 // If we aren't appending the results to this list, then clear the list
2974 if (!append)
2975 sc_list.Clear();
2976
2977 // Remember how many sc_list are in the list before we search in case
2978 // we are appending the results to a variable list.
2979 uint32_t original_size = sc_list.GetSize();
2980
Greg Clayton97fbc342011-10-20 22:30:33 +00002981 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002982 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002983 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00002984 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002985 }
2986 else
2987 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002988 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002989 if (!m_indexed)
2990 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002991
Pavel Labatha73d6572015-03-13 10:22:00 +00002992 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002993
Pavel Labatha73d6572015-03-13 10:22:00 +00002994 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002996
2997 // Return the number of variable that were appended to the list
2998 return sc_list.GetSize() - original_size;
2999}
Jim Ingham318c9f22011-08-26 19:44:13 +00003000
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003002SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3003 const ConstString &name,
3004 const lldb_private::ClangNamespaceDecl *namespace_decl,
3005 bool append,
3006 uint32_t max_matches,
3007 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003008{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003009 DWARFDebugInfo* info = DebugInfo();
3010 if (info == NULL)
3011 return 0;
3012
Greg Clayton5160ce52013-03-27 23:08:40 +00003013 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003014
Greg Clayton21f2a492011-10-06 00:09:08 +00003015 if (log)
3016 {
Greg Clayton437a1352012-04-09 22:43:43 +00003017 if (namespace_decl)
Greg Clayton5160ce52013-03-27 23:08:40 +00003018 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003019 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3020 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003021 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00003022 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003023 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00003024 else
Greg Clayton5160ce52013-03-27 23:08:40 +00003025 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003026 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003027 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00003028 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003029 }
3030
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003031 // If we aren't appending the results to this list, then clear the list
3032 if (!append)
3033 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003034
Sean Callanan213fdb82011-10-13 01:49:10 +00003035 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003036 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037
Greg Claytond4a2b372011-09-12 23:21:58 +00003038 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003039
Greg Clayton97fbc342011-10-20 22:30:33 +00003040 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003041 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003042 if (m_apple_types_ap.get())
3043 {
3044 const char *name_cstr = name.GetCString();
3045 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3046 }
Greg Clayton7f995132011-10-04 22:41:51 +00003047 }
3048 else
3049 {
3050 if (!m_indexed)
3051 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003052
Greg Clayton7f995132011-10-04 22:41:51 +00003053 m_type_index.Find (name, die_offsets);
3054 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003055
Greg Clayton437a1352012-04-09 22:43:43 +00003056 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003057
Greg Clayton437a1352012-04-09 22:43:43 +00003058 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003059 {
Greg Clayton7f995132011-10-04 22:41:51 +00003060 const uint32_t initial_types_size = types.GetSize();
3061 DWARFCompileUnit* dwarf_cu = NULL;
3062 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003063 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003064 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003065 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003066 const dw_offset_t die_offset = die_offsets[i];
3067 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3068
Greg Clayton95d87902011-11-11 03:16:25 +00003069 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003070 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003071 if (namespace_decl)
3072 {
3073 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
3074
3075 if (!type_system->DIEIsInNamespace (namespace_decl, this, dwarf_cu, die))
3076 continue;
3077 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003078
Greg Clayton95d87902011-11-11 03:16:25 +00003079 Type *matching_type = ResolveType (dwarf_cu, die);
3080 if (matching_type)
3081 {
3082 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003083 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003084 if (types.GetSize() >= max_matches)
3085 break;
3086 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003087 }
Greg Clayton95d87902011-11-11 03:16:25 +00003088 else
3089 {
3090 if (m_using_apple_tables)
3091 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003092 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3093 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003094 }
3095 }
3096
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003097 }
Greg Clayton437a1352012-04-09 22:43:43 +00003098 const uint32_t num_matches = types.GetSize() - initial_types_size;
3099 if (log && num_matches)
3100 {
3101 if (namespace_decl)
3102 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003103 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003104 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3105 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003106 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00003107 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003108 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003109 num_matches);
3110 }
3111 else
3112 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003113 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003114 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3115 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003116 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003117 num_matches);
3118 }
3119 }
3120 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003121 }
Greg Clayton7f995132011-10-04 22:41:51 +00003122 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003123}
3124
3125
Greg Clayton526e5af2010-11-13 03:52:47 +00003126ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003127SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003128 const ConstString &name,
3129 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003130{
Greg Clayton5160ce52013-03-27 23:08:40 +00003131 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003132
3133 if (log)
3134 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003135 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003136 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3137 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003138 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003139
3140 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003141 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003142
Greg Clayton526e5af2010-11-13 03:52:47 +00003143 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003144 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003145 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003146 {
Greg Clayton7f995132011-10-04 22:41:51 +00003147 DIEArray die_offsets;
3148
Greg Clayton526e5af2010-11-13 03:52:47 +00003149 // Index if we already haven't to make sure the compile units
3150 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003151 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003152 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003153 if (m_apple_namespaces_ap.get())
3154 {
3155 const char *name_cstr = name.GetCString();
3156 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3157 }
Greg Clayton7f995132011-10-04 22:41:51 +00003158 }
3159 else
3160 {
3161 if (!m_indexed)
3162 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003163
Greg Clayton7f995132011-10-04 22:41:51 +00003164 m_namespace_index.Find (name, die_offsets);
3165 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003166
3167 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003168 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003169 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003170 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003171 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003172 DWARFDebugInfo* debug_info = DebugInfo();
3173 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003174 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003175 const dw_offset_t die_offset = die_offsets[i];
3176 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003177
Greg Clayton95d87902011-11-11 03:16:25 +00003178 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003179 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003180 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
Greg Clayton95d87902011-11-11 03:16:25 +00003181
Greg Clayton8b4edba2015-08-14 20:02:05 +00003182 if (parent_namespace_decl)
Greg Clayton95d87902011-11-11 03:16:25 +00003183 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003184 if (type_system && !type_system->DIEIsInNamespace (parent_namespace_decl, this, dwarf_cu, die))
3185 continue;
3186 }
3187
3188 if (type_system)
3189 {
3190 clang::NamespaceDecl *clang_namespace_decl = type_system->ResolveNamespaceDIE (this, dwarf_cu, die);
3191 if (clang_namespace_decl)
3192 {
3193 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3194 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
3195 break;
3196 }
Greg Clayton95d87902011-11-11 03:16:25 +00003197 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003198 }
Greg Clayton95d87902011-11-11 03:16:25 +00003199 else
3200 {
3201 if (m_using_apple_tables)
3202 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003203 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3204 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003205 }
3206 }
3207
Greg Clayton526e5af2010-11-13 03:52:47 +00003208 }
3209 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003210 }
Greg Clayton437a1352012-04-09 22:43:43 +00003211 if (log && namespace_decl.GetNamespaceDecl())
3212 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003213 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003214 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3215 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003216 static_cast<const void*>(namespace_decl.GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00003217 namespace_decl.GetQualifiedName().c_str());
3218 }
3219
Greg Clayton526e5af2010-11-13 03:52:47 +00003220 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003221}
3222
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003223uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003224SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003225{
3226 // Remember how many sc_list are in the list before we search in case
3227 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003228 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003229
3230 const uint32_t num_die_offsets = die_offsets.size();
3231 // Parse all of the types we found from the pubtypes matches
3232 uint32_t i;
3233 uint32_t num_matches = 0;
3234 for (i = 0; i < num_die_offsets; ++i)
3235 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003236 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3237 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003238 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003239 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003240 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003241 ++num_matches;
3242 if (num_matches >= max_matches)
3243 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003244 }
3245 }
3246
3247 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003248 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003249}
3250
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003251
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003252TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00003253SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003254{
3255 TypeSP type_sp;
3256 if (die != NULL)
3257 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003258 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003259 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003260 if (type_ptr == NULL)
3261 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003262 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00003263 assert (lldb_cu);
3264 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00003265 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003266 }
3267 else if (type_ptr != DIE_IS_BEING_PARSED)
3268 {
3269 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003270 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003271 }
3272
3273 }
3274 return type_sp;
3275}
3276
Greg Clayton2bc22f82011-09-30 03:20:47 +00003277
3278const DWARFDebugInfoEntry *
Greg Clayton8b4edba2015-08-14 20:02:05 +00003279SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003280{
3281 if (cu && die)
3282 {
3283 const DWARFDebugInfoEntry * const decl_die = die;
3284
3285 while (die != NULL)
3286 {
3287 // If this is the original DIE that we are searching for a declaration
3288 // for, then don't look in the cache as we don't want our own decl
3289 // context to be our decl context...
3290 if (decl_die != die)
3291 {
3292 switch (die->Tag())
3293 {
3294 case DW_TAG_compile_unit:
3295 case DW_TAG_namespace:
3296 case DW_TAG_structure_type:
3297 case DW_TAG_union_type:
3298 case DW_TAG_class_type:
3299 return die;
3300
3301 default:
3302 break;
3303 }
3304 }
3305
3306 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3307 if (die_offset != DW_INVALID_OFFSET)
3308 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003309 DWARFCompileUnit *spec_cu = const_cast<DWARFCompileUnit *>(cu);
Greg Clayton2bc22f82011-09-30 03:20:47 +00003310 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3311 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3312 if (spec_die_decl_ctx_die)
3313 return spec_die_decl_ctx_die;
3314 }
3315
3316 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3317 if (die_offset != DW_INVALID_OFFSET)
3318 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003319 DWARFCompileUnit *abs_cu = const_cast<DWARFCompileUnit *>(cu);
Greg Clayton2bc22f82011-09-30 03:20:47 +00003320 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3321 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3322 if (abs_die_decl_ctx_die)
3323 return abs_die_decl_ctx_die;
3324 }
3325
3326 die = die->GetParent();
3327 }
3328 }
3329 return NULL;
3330}
3331
3332
Greg Clayton901c5ca2011-12-03 04:40:03 +00003333Symbol *
3334SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3335{
3336 Symbol *objc_class_symbol = NULL;
3337 if (m_obj_file)
3338 {
Greg Clayton3046e662013-07-10 01:23:25 +00003339 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003340 if (symtab)
3341 {
3342 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3343 eSymbolTypeObjCClass,
3344 Symtab::eDebugNo,
3345 Symtab::eVisibilityAny);
3346 }
3347 }
3348 return objc_class_symbol;
3349}
3350
Greg Claytonc7f03b62012-01-12 04:33:28 +00003351// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3352// then we can end up looking through all class types for a complete type and never find
3353// the full definition. We need to know if this attribute is supported, so we determine
3354// this here and cache th result. We also need to worry about the debug map DWARF file
3355// if we are doing darwin DWARF in .o file debugging.
3356bool
3357SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3358{
3359 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3360 {
3361 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3362 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3363 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3364 else
3365 {
3366 DWARFDebugInfo* debug_info = DebugInfo();
3367 const uint32_t num_compile_units = GetNumCompileUnits();
3368 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3369 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003370 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3371 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003372 {
3373 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3374 break;
3375 }
3376 }
3377 }
Greg Clayton1f746072012-08-29 21:13:06 +00003378 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00003379 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3380 }
3381 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3382}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003383
3384// This function can be used when a DIE is found that is a forward declaration
3385// DIE and we want to try and find a type that has the complete definition.
3386TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00003387SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
3388 const ConstString &type_name,
3389 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003390{
3391
3392 TypeSP type_sp;
3393
Greg Claytonc7f03b62012-01-12 04:33:28 +00003394 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003395 return type_sp;
3396
3397 DIEArray die_offsets;
3398
3399 if (m_using_apple_tables)
3400 {
3401 if (m_apple_types_ap.get())
3402 {
3403 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003404 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003405 }
3406 }
3407 else
3408 {
3409 if (!m_indexed)
3410 Index ();
3411
3412 m_type_index.Find (type_name, die_offsets);
3413 }
3414
Greg Clayton901c5ca2011-12-03 04:40:03 +00003415 const size_t num_matches = die_offsets.size();
3416
Greg Clayton901c5ca2011-12-03 04:40:03 +00003417 DWARFCompileUnit* type_cu = NULL;
3418 const DWARFDebugInfoEntry* type_die = NULL;
3419 if (num_matches)
3420 {
3421 DWARFDebugInfo* debug_info = DebugInfo();
3422 for (size_t i=0; i<num_matches; ++i)
3423 {
3424 const dw_offset_t die_offset = die_offsets[i];
3425 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3426
3427 if (type_die)
3428 {
3429 bool try_resolving_type = false;
3430
3431 // Don't try and resolve the DIE we are looking for with the DIE itself!
3432 if (type_die != die)
3433 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003434 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003435 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003436 case DW_TAG_class_type:
3437 case DW_TAG_structure_type:
3438 try_resolving_type = true;
3439 break;
3440 default:
3441 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003442 }
3443 }
3444
3445 if (try_resolving_type)
3446 {
Ed Maste4c24b122013-10-17 20:13:14 +00003447 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
3448 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003449
3450 if (try_resolving_type)
3451 {
3452 Type *resolved_type = ResolveType (type_cu, type_die, false);
3453 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3454 {
Ed Mastea0191d12013-10-17 20:42:56 +00003455 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton901c5ca2011-12-03 04:40:03 +00003456 MakeUserID(die->GetOffset()),
Jim Ingham4af59612014-12-19 19:20:44 +00003457 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton901c5ca2011-12-03 04:40:03 +00003458 MakeUserID(type_die->GetOffset()),
3459 MakeUserID(type_cu->GetOffset()));
3460
Greg Claytonc7f03b62012-01-12 04:33:28 +00003461 if (die)
3462 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00003463 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00003464 break;
3465 }
3466 }
3467 }
3468 }
3469 else
3470 {
3471 if (m_using_apple_tables)
3472 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003473 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3474 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003475 }
3476 }
3477
3478 }
3479 }
3480 return type_sp;
3481}
3482
Greg Claytona8022fa2012-04-24 21:22:41 +00003483
Greg Clayton80c26302012-02-05 06:12:47 +00003484//----------------------------------------------------------------------
3485// This function helps to ensure that the declaration contexts match for
3486// two different DIEs. Often times debug information will refer to a
3487// forward declaration of a type (the equivalent of "struct my_struct;".
3488// There will often be a declaration of that type elsewhere that has the
3489// full definition. When we go looking for the full type "my_struct", we
3490// will find one or more matches in the accelerator tables and we will
3491// then need to make sure the type was in the same declaration context
3492// as the original DIE. This function can efficiently compare two DIEs
3493// and will return true when the declaration context matches, and false
3494// when they don't.
3495//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00003496bool
3497SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
3498 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
3499{
Greg Claytona8022fa2012-04-24 21:22:41 +00003500 if (die1 == die2)
3501 return true;
3502
3503#if defined (LLDB_CONFIGURATION_DEBUG)
3504 // You can't and shouldn't call this function with a compile unit from
3505 // two different SymbolFileDWARF instances.
3506 assert (DebugInfo()->ContainsCompileUnit (cu1));
3507 assert (DebugInfo()->ContainsCompileUnit (cu2));
3508#endif
3509
Greg Clayton890ff562012-02-02 05:48:16 +00003510 DWARFDIECollection decl_ctx_1;
3511 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00003512 //The declaration DIE stack is a stack of the declaration context
3513 // DIEs all the way back to the compile unit. If a type "T" is
3514 // declared inside a class "B", and class "B" is declared inside
3515 // a class "A" and class "A" is in a namespace "lldb", and the
3516 // namespace is in a compile unit, there will be a stack of DIEs:
3517 //
3518 // [0] DW_TAG_class_type for "B"
3519 // [1] DW_TAG_class_type for "A"
3520 // [2] DW_TAG_namespace for "lldb"
3521 // [3] DW_TAG_compile_unit for the source file.
3522 //
3523 // We grab both contexts and make sure that everything matches
3524 // all the way back to the compiler unit.
3525
3526 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00003527 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00003528 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00003529 // Make sure the context arrays have the same size, otherwise
3530 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00003531 const size_t count1 = decl_ctx_1.Size();
3532 const size_t count2 = decl_ctx_2.Size();
3533 if (count1 != count2)
3534 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00003535
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003536 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00003537 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00003538 const DWARFDebugInfoEntry *decl_ctx_die1;
3539 const DWARFDebugInfoEntry *decl_ctx_die2;
3540 size_t i;
3541 for (i=0; i<count1; i++)
3542 {
3543 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
3544 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
3545 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
3546 return false;
3547 }
Greg Clayton890ff562012-02-02 05:48:16 +00003548#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00003549
3550 // Make sure the top item in the decl context die array is always
3551 // DW_TAG_compile_unit. If it isn't then something went wrong in
3552 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00003553 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00003554
Greg Clayton890ff562012-02-02 05:48:16 +00003555#endif
3556 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00003557 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00003558 for (i=0; i<count1 - 1; i++)
3559 {
3560 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
3561 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
3562 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00003563 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00003564 // If the string was from a DW_FORM_strp, then the pointer will often
3565 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00003566 if (name1 == name2)
3567 continue;
3568
3569 // Name pointers are not equal, so only compare the strings
3570 // if both are not NULL.
3571 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00003572 {
Greg Clayton5569e642012-02-06 01:44:54 +00003573 // If the strings don't compare, we are done...
3574 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00003575 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00003576 }
3577 else
3578 {
3579 // One name was NULL while the other wasn't
3580 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00003581 }
3582 }
Greg Clayton80c26302012-02-05 06:12:47 +00003583 // We made it through all of the checks and the declaration contexts
3584 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00003585 return true;
3586}
Greg Clayton220a0072011-12-09 08:48:30 +00003587
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003588
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003589TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00003590SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
3591{
3592 TypeSP type_sp;
3593
3594 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
3595 if (dwarf_decl_ctx_count > 0)
3596 {
3597 const ConstString type_name(dwarf_decl_ctx[0].name);
3598 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
3599
3600 if (type_name)
3601 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003602 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00003603 if (log)
3604 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003605 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003606 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
3607 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3608 dwarf_decl_ctx.GetQualifiedName());
3609 }
3610
3611 DIEArray die_offsets;
3612
3613 if (m_using_apple_tables)
3614 {
3615 if (m_apple_types_ap.get())
3616 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003617 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
3618 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
3619 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00003620 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003621 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
3622 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
3623 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003624 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00003625 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
3626 }
3627 else if (has_tag)
3628 {
3629 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003630 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00003631 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
3632 }
3633 else
3634 {
3635 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3636 }
3637 }
3638 }
3639 else
3640 {
3641 if (!m_indexed)
3642 Index ();
3643
3644 m_type_index.Find (type_name, die_offsets);
3645 }
3646
3647 const size_t num_matches = die_offsets.size();
3648
3649
3650 DWARFCompileUnit* type_cu = NULL;
3651 const DWARFDebugInfoEntry* type_die = NULL;
3652 if (num_matches)
3653 {
3654 DWARFDebugInfo* debug_info = DebugInfo();
3655 for (size_t i=0; i<num_matches; ++i)
3656 {
3657 const dw_offset_t die_offset = die_offsets[i];
3658 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3659
3660 if (type_die)
3661 {
3662 bool try_resolving_type = false;
3663
3664 // Don't try and resolve the DIE we are looking for with the DIE itself!
3665 const dw_tag_t type_tag = type_die->Tag();
3666 // Make sure the tags match
3667 if (type_tag == tag)
3668 {
3669 // The tags match, lets try resolving this type
3670 try_resolving_type = true;
3671 }
3672 else
3673 {
3674 // The tags don't match, but we need to watch our for a
3675 // forward declaration for a struct and ("struct foo")
3676 // ends up being a class ("class foo { ... };") or
3677 // vice versa.
3678 switch (type_tag)
3679 {
3680 case DW_TAG_class_type:
3681 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3682 try_resolving_type = (tag == DW_TAG_structure_type);
3683 break;
3684 case DW_TAG_structure_type:
3685 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3686 try_resolving_type = (tag == DW_TAG_class_type);
3687 break;
3688 default:
3689 // Tags don't match, don't event try to resolve
3690 // using this type whose name matches....
3691 break;
3692 }
3693 }
3694
3695 if (try_resolving_type)
3696 {
3697 DWARFDeclContext type_dwarf_decl_ctx;
3698 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
3699
3700 if (log)
3701 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003702 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003703 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
3704 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3705 dwarf_decl_ctx.GetQualifiedName(),
3706 type_die->GetOffset(),
3707 type_dwarf_decl_ctx.GetQualifiedName());
3708 }
3709
3710 // Make sure the decl contexts match all the way up
3711 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
3712 {
3713 Type *resolved_type = ResolveType (type_cu, type_die, false);
3714 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3715 {
3716 type_sp = resolved_type->shared_from_this();
3717 break;
3718 }
3719 }
3720 }
3721 else
3722 {
3723 if (log)
3724 {
3725 std::string qualified_name;
3726 type_die->GetQualifiedName(this, type_cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00003727 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00003728 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
3729 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
3730 dwarf_decl_ctx.GetQualifiedName(),
3731 type_die->GetOffset(),
3732 qualified_name.c_str());
3733 }
3734 }
3735 }
3736 else
3737 {
3738 if (m_using_apple_tables)
3739 {
3740 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3741 die_offset, type_name.GetCString());
3742 }
3743 }
3744
3745 }
3746 }
3747 }
3748 }
3749 return type_sp;
3750}
3751
Greg Claytona8022fa2012-04-24 21:22:41 +00003752TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00003753SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003754{
Greg Clayton196e8cd2015-08-17 20:31:46 +00003755 TypeSP type_sp;
3756
Greg Clayton8b4edba2015-08-14 20:02:05 +00003757 TypeSystem *type_system = GetTypeSystemForLanguage(dwarf_cu->GetLanguageType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003758
Greg Clayton8b4edba2015-08-14 20:02:05 +00003759 if (type_system)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003760 {
Greg Clayton8b4edba2015-08-14 20:02:05 +00003761 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
Greg Clayton196e8cd2015-08-17 20:31:46 +00003762 type_sp = type_system->ParseTypeFromDWARF (sc, this, dwarf_cu, die, log, type_is_new_ptr);
3763 if (type_sp)
3764 {
3765 TypeList* type_list = GetTypeList();
3766 if (type_list)
3767 type_list->Insert(type_sp);
3768 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003769 }
Greg Clayton196e8cd2015-08-17 20:31:46 +00003770
3771 return type_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003772}
3773
3774size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003775SymbolFileDWARF::ParseTypes
3776(
3777 const SymbolContext& sc,
3778 DWARFCompileUnit* dwarf_cu,
3779 const DWARFDebugInfoEntry *die,
3780 bool parse_siblings,
3781 bool parse_children
3782)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003783{
3784 size_t types_added = 0;
3785 while (die != NULL)
3786 {
3787 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00003788 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003789 {
3790 if (type_is_new)
3791 ++types_added;
3792 }
3793
3794 if (parse_children && die->HasChildren())
3795 {
3796 if (die->Tag() == DW_TAG_subprogram)
3797 {
3798 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00003799 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003800 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
3801 }
3802 else
3803 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
3804 }
3805
3806 if (parse_siblings)
3807 die = die->GetSibling();
3808 else
3809 die = NULL;
3810 }
3811 return types_added;
3812}
3813
3814
3815size_t
3816SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3817{
3818 assert(sc.comp_unit && sc.function);
3819 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003820 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821 if (dwarf_cu)
3822 {
3823 dw_offset_t function_die_offset = sc.function->GetID();
3824 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
3825 if (function_die)
3826 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00003827 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003828 }
3829 }
3830
3831 return functions_added;
3832}
3833
3834
3835size_t
3836SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3837{
3838 // At least a compile unit must be valid
3839 assert(sc.comp_unit);
3840 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00003841 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003842 if (dwarf_cu)
3843 {
3844 if (sc.function)
3845 {
3846 dw_offset_t function_die_offset = sc.function->GetID();
3847 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
3848 if (func_die && func_die->HasChildren())
3849 {
3850 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
3851 }
3852 }
3853 else
3854 {
3855 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
3856 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
3857 {
3858 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
3859 }
3860 }
3861 }
3862
3863 return types_added;
3864}
3865
3866size_t
3867SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3868{
3869 if (sc.comp_unit != NULL)
3870 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003871 DWARFDebugInfo* info = DebugInfo();
3872 if (info == NULL)
3873 return 0;
3874
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003875 if (sc.function)
3876 {
Greg Clayton9422dd62013-03-04 21:46:16 +00003877 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get();
3878
3879 if (dwarf_cu == NULL)
3880 return 0;
3881
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003882 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00003883
Greg Claytonc7bece562013-01-25 18:06:21 +00003884 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3885 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003886 {
3887 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00003888
Greg Claytone38a5ed2012-01-05 03:57:59 +00003889 // Let all blocks know they have parse all their variables
3890 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3891 return num_variables;
3892 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003893 }
3894 else if (sc.comp_unit)
3895 {
Greg Clayton9422dd62013-03-04 21:46:16 +00003896 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get();
3897
3898 if (dwarf_cu == NULL)
3899 return 0;
3900
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003901 uint32_t vars_added = 0;
3902 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3903
3904 if (variables.get() == NULL)
3905 {
3906 variables.reset(new VariableList());
3907 sc.comp_unit->SetVariableList(variables);
3908
Greg Claytond4a2b372011-09-12 23:21:58 +00003909 DWARFCompileUnit* match_dwarf_cu = NULL;
3910 const DWARFDebugInfoEntry* die = NULL;
3911 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00003912 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003913 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003914 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003915 {
3916 DWARFMappedHash::DIEInfoArray hash_data_array;
3917 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
3918 dwarf_cu->GetNextCompileUnitOffset(),
3919 hash_data_array))
3920 {
3921 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3922 }
3923 }
Greg Clayton7f995132011-10-04 22:41:51 +00003924 }
3925 else
3926 {
3927 // Index if we already haven't to make sure the compile units
3928 // get indexed and make their global DIE index list
3929 if (!m_indexed)
3930 Index ();
3931
3932 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
3933 dwarf_cu->GetNextCompileUnitOffset(),
3934 die_offsets);
3935 }
3936
3937 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003938 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003940 DWARFDebugInfo* debug_info = DebugInfo();
3941 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003942 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003943 const dw_offset_t die_offset = die_offsets[i];
3944 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00003945 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003946 {
Greg Clayton95d87902011-11-11 03:16:25 +00003947 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
3948 if (var_sp)
3949 {
3950 variables->AddVariableIfUnique (var_sp);
3951 ++vars_added;
3952 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003953 }
Greg Clayton95d87902011-11-11 03:16:25 +00003954 else
3955 {
3956 if (m_using_apple_tables)
3957 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003958 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 +00003959 }
3960 }
3961
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003962 }
3963 }
3964 }
3965 return vars_added;
3966 }
3967 }
3968 return 0;
3969}
3970
3971
3972VariableSP
3973SymbolFileDWARF::ParseVariableDIE
3974(
3975 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003976 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00003977 const DWARFDebugInfoEntry *die,
3978 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003979)
3980{
Greg Clayton83c5cd92010-11-14 22:13:40 +00003981 VariableSP var_sp (m_die_to_variable_sp[die]);
3982 if (var_sp)
3983 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003984
3985 const dw_tag_t tag = die->Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00003986 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003987
3988 if ((tag == DW_TAG_variable) ||
3989 (tag == DW_TAG_constant) ||
3990 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003991 {
Greg Clayton7f995132011-10-04 22:41:51 +00003992 DWARFDebugInfoEntry::Attributes attributes;
3993 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3994 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003995 {
Greg Clayton7f995132011-10-04 22:41:51 +00003996 const char *name = NULL;
3997 const char *mangled = NULL;
3998 Declaration decl;
3999 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00004000 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00004001 DWARFExpression location;
4002 bool is_external = false;
4003 bool is_artificial = false;
4004 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004005 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00004006 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00004007 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00004008
4009 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004010 {
Greg Clayton7f995132011-10-04 22:41:51 +00004011 dw_attr_t attr = attributes.AttributeAtIndex(i);
4012 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00004013
Greg Clayton7f995132011-10-04 22:41:51 +00004014 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004015 {
Greg Clayton7f995132011-10-04 22:41:51 +00004016 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004017 {
Greg Clayton7f995132011-10-04 22:41:51 +00004018 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4019 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4020 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4021 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton71415542012-12-08 00:24:40 +00004022 case DW_AT_linkage_name:
Greg Clayton7f995132011-10-04 22:41:51 +00004023 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00004024 case DW_AT_type: type_uid = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004025 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004026 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004027 // If we have already found a DW_AT_location attribute, ignore this attribute.
4028 if (!has_explicit_location)
4029 {
4030 location_is_const_value_data = true;
4031 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00004032 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004033 if (DWARFFormValue::IsBlockForm(form_value.Form()))
4034 {
4035 // Retrieve the value as a block expression.
4036 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4037 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004038 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004039 }
4040 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4041 {
4042 // Retrieve the value as a data expression.
Greg Clayton54166af2014-11-22 01:58:59 +00004043 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004044 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
4045 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Enrico Granata4ec130d2014-08-11 19:16:35 +00004046 if (data_length == 0)
4047 {
4048 const uint8_t *data_pointer = form_value.BlockData();
4049 if (data_pointer)
4050 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00004051 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00004052 }
4053 else if (DWARFFormValue::IsDataForm(form_value.Form()))
4054 {
4055 // we need to get the byte size of the type later after we create the variable
4056 const_value = form_value;
4057 }
4058 }
4059 else
4060 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004061 }
4062 else
4063 {
4064 // Retrieve the value as a string expression.
4065 if (form_value.Form() == DW_FORM_strp)
4066 {
Greg Clayton54166af2014-11-22 01:58:59 +00004067 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004068 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
4069 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Richard Mitton0a558352013-10-17 21:14:00 +00004070 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004071 }
4072 else
4073 {
4074 const char *str = form_value.AsCString(&debug_info_data);
4075 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
4076 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00004077 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004078 }
4079 }
4080 }
4081 break;
Greg Clayton7f995132011-10-04 22:41:51 +00004082 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004083 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00004084 location_is_const_value_data = false;
4085 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00004086 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004087 {
Ed Masteeeae7212013-10-24 20:43:47 +00004088 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004089
4090 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4091 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00004092 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004093 }
4094 else
4095 {
Ed Masteeeae7212013-10-24 20:43:47 +00004096 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00004097 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4098
4099 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
4100 if (loc_list_length > 0)
4101 {
Richard Mitton0a558352013-10-17 21:14:00 +00004102 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00004103 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00004104 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00004105 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004106 }
4107 }
Greg Clayton7f995132011-10-04 22:41:51 +00004108 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004109
Greg Clayton1c8ef472013-04-05 23:27:21 +00004110 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00004111 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00004112 case DW_AT_declaration:
4113 case DW_AT_description:
4114 case DW_AT_endianity:
4115 case DW_AT_segment:
4116 case DW_AT_start_scope:
4117 case DW_AT_visibility:
4118 default:
4119 case DW_AT_abstract_origin:
4120 case DW_AT_sibling:
4121 case DW_AT_specification:
4122 break;
4123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004124 }
4125 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004126
Greg Clayton9e9f2192013-05-17 00:55:28 +00004127 ValueType scope = eValueTypeInvalid;
4128
4129 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4130 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4131 SymbolContextScope * symbol_context_scope = NULL;
4132
Siva Chandra0783ab92015-03-24 18:32:27 +00004133 if (!mangled)
4134 {
4135 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
4136 // generate fully qualified names of global variables with commands like "frame var j".
4137 // For example, if j were an int variable holding a value 4 and declared in a namespace
4138 // B which in turn is contained in a namespace A, the command "frame var j" returns
4139 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
4140 // to generate a fully qualified name from the declaration context.
4141 if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
4142 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()))
4143 {
4144 DWARFDeclContext decl_ctx;
4145
4146 die->GetDWARFDeclContext(this, dwarf_cu, decl_ctx);
4147 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
4148 }
4149 }
4150
Greg Clayton9e9f2192013-05-17 00:55:28 +00004151 // DWARF doesn't specify if a DW_TAG_variable is a local, global
4152 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004153 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00004154 // a DW_OP_addr opcode _somewhere_ in the definition. I say
4155 // somewhere because clang likes to combine small global variables
4156 // into the same symbol and have locations like:
4157 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
4158 // So if we don't have a DW_TAG_formal_parameter, we can look at
4159 // the location to see if it contains a DW_OP_addr opcode, and
4160 // then we can correctly classify our variables.
4161 if (tag == DW_TAG_formal_parameter)
4162 scope = eValueTypeVariableArgument;
4163 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004165 bool op_error = false;
4166 // Check if the location has a DW_OP_addr with any address value...
4167 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
4168 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004169 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004170 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
4171 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00004172 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004173 StreamString strm;
4174 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
4175 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), strm.GetString().c_str());
Greg Clayton96c09682012-01-04 22:56:43 +00004176 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004177 }
Greg Claytond1767f02011-12-08 02:13:16 +00004178
Greg Clayton9e9f2192013-05-17 00:55:28 +00004179 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
4180 {
4181 if (is_external)
4182 scope = eValueTypeVariableGlobal;
4183 else
4184 scope = eValueTypeVariableStatic;
4185
4186
4187 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
4188
4189 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004190 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004191 // When leaving the DWARF in the .o files on darwin,
4192 // when we have a global variable that wasn't initialized,
4193 // the .o file might not have allocated a virtual
4194 // address for the global variable. In this case it will
4195 // have created a symbol for the global variable
4196 // that is undefined/data and external and the value will
4197 // be the byte size of the variable. When we do the
4198 // address map in SymbolFileDWARFDebugMap we rely on
4199 // having an address, we need to do some magic here
4200 // so we can get the correct address for our global
4201 // variable. The address for all of these entries
4202 // will be zero, and there will be an undefined symbol
4203 // in this object file, and the executable will have
4204 // a matching symbol with a good address. So here we
4205 // dig up the correct address and replace it in the
4206 // location for the variable, and set the variable's
4207 // symbol context scope to be that of the main executable
4208 // so the file address will resolve correctly.
4209 bool linked_oso_file_addr = false;
4210 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00004211 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004212 // we have a possible uninitialized extern global
4213 ConstString const_name(mangled ? mangled : name);
4214 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
4215 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004216 {
Greg Clayton3046e662013-07-10 01:23:25 +00004217 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004218 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004219 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004220 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
4221 eSymbolTypeData,
4222 Symtab::eDebugYes,
4223 Symtab::eVisibilityExtern);
4224 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004225 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004226 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004227 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004228 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00004229 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004230 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004231 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004232 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004233 linked_oso_file_addr = true;
4234 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004235 }
4236 }
4237 }
4238 }
4239 }
4240 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004241 }
Greg Clayton9422dd62013-03-04 21:46:16 +00004242
Greg Clayton9e9f2192013-05-17 00:55:28 +00004243 if (!linked_oso_file_addr)
4244 {
4245 // The DW_OP_addr is not zero, but it contains a .o file address which
4246 // needs to be linked up correctly.
4247 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
4248 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00004249 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004250 // Update the file address for this variable
4251 location.Update_DW_OP_addr (exe_file_addr);
4252 }
4253 else
4254 {
4255 // Variable didn't make it into the final executable
4256 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00004257 }
Greg Claytond1767f02011-12-08 02:13:16 +00004258 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00004259 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00004260 }
4261 else
4262 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00004263 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004264 }
Greg Clayton7f995132011-10-04 22:41:51 +00004265 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00004266
4267 if (symbol_context_scope == NULL)
4268 {
4269 switch (parent_tag)
4270 {
4271 case DW_TAG_subprogram:
4272 case DW_TAG_inlined_subroutine:
4273 case DW_TAG_lexical_block:
4274 if (sc.function)
4275 {
4276 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
4277 if (symbol_context_scope == NULL)
4278 symbol_context_scope = sc.function;
4279 }
4280 break;
4281
4282 default:
4283 symbol_context_scope = sc.comp_unit;
4284 break;
4285 }
4286 }
4287
4288 if (symbol_context_scope)
4289 {
Enrico Granata4ec130d2014-08-11 19:16:35 +00004290 SymbolFileTypeSP type_sp(new SymbolFileType(*this, type_uid));
4291
4292 if (const_value.Form() && type_sp && type_sp->GetType())
4293 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), dwarf_cu->GetAddressByteSize());
4294
Greg Clayton9e9f2192013-05-17 00:55:28 +00004295 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
4296 name,
4297 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00004298 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00004299 scope,
4300 symbol_context_scope,
4301 &decl,
4302 location,
4303 is_external,
4304 is_artificial));
4305
4306 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4307 }
4308 else
4309 {
4310 // Not ready to parse this variable yet. It might be a global
4311 // or static variable that is in a function scope and the function
4312 // in the symbol context wasn't filled in yet
4313 return var_sp;
4314 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004315 }
Greg Clayton7f995132011-10-04 22:41:51 +00004316 // Cache var_sp even if NULL (the variable was just a specification or
4317 // was missing vital information to be able to be displayed in the debugger
4318 // (missing location due to optimization, etc)) so we don't re-parse
4319 // this DIE over and over later...
4320 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004321 }
4322 return var_sp;
4323}
4324
Greg Claytonc662ec82011-06-17 22:10:16 +00004325
4326const DWARFDebugInfoEntry *
4327SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
4328 dw_offset_t spec_block_die_offset,
4329 DWARFCompileUnit **result_die_cu_handle)
4330{
4331 // Give the concrete function die specified by "func_die_offset", find the
4332 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4333 // to "spec_block_die_offset"
4334 DWARFDebugInfo* info = DebugInfo();
4335
4336 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
4337 if (die)
4338 {
4339 assert (*result_die_cu_handle);
4340 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
4341 }
4342 return NULL;
4343}
4344
4345
4346const DWARFDebugInfoEntry *
4347SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
4348 const DWARFDebugInfoEntry *die,
4349 dw_offset_t spec_block_die_offset,
4350 DWARFCompileUnit **result_die_cu_handle)
4351{
4352 if (die)
4353 {
4354 switch (die->Tag())
4355 {
4356 case DW_TAG_subprogram:
4357 case DW_TAG_inlined_subroutine:
4358 case DW_TAG_lexical_block:
4359 {
4360 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4361 {
4362 *result_die_cu_handle = dwarf_cu;
4363 return die;
4364 }
4365
4366 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
4367 {
4368 *result_die_cu_handle = dwarf_cu;
4369 return die;
4370 }
4371 }
4372 break;
4373 }
4374
4375 // Give the concrete function die specified by "func_die_offset", find the
4376 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4377 // to "spec_block_die_offset"
4378 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
4379 {
4380 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
4381 child_die,
4382 spec_block_die_offset,
4383 result_die_cu_handle);
4384 if (result_die)
4385 return result_die;
4386 }
4387 }
4388
4389 *result_die_cu_handle = NULL;
4390 return NULL;
4391}
4392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004393size_t
4394SymbolFileDWARF::ParseVariables
4395(
4396 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004397 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00004398 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004399 const DWARFDebugInfoEntry *orig_die,
4400 bool parse_siblings,
4401 bool parse_children,
4402 VariableList* cc_variable_list
4403)
4404{
4405 if (orig_die == NULL)
4406 return 0;
4407
Greg Claytonc662ec82011-06-17 22:10:16 +00004408 VariableListSP variable_list_sp;
4409
4410 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004411 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004412 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004413 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004414 dw_tag_t tag = die->Tag();
4415
4416 // Check to see if we have already parsed this variable or constant?
4417 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004418 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004419 if (cc_variable_list)
4420 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004421 }
4422 else
4423 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004424 // We haven't already parsed it, lets do that now.
4425 if ((tag == DW_TAG_variable) ||
4426 (tag == DW_TAG_constant) ||
4427 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004428 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004429 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004430 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004431 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
4432 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4433 switch (parent_tag)
4434 {
4435 case DW_TAG_compile_unit:
4436 if (sc.comp_unit != NULL)
4437 {
4438 variable_list_sp = sc.comp_unit->GetVariableList(false);
4439 if (variable_list_sp.get() == NULL)
4440 {
4441 variable_list_sp.reset(new VariableList());
4442 sc.comp_unit->SetVariableList(variable_list_sp);
4443 }
4444 }
4445 else
4446 {
Daniel Malead01b2952012-11-29 21:49:15 +00004447 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 Claytone38a5ed2012-01-05 03:57:59 +00004448 MakeUserID(sc_parent_die->GetOffset()),
4449 DW_TAG_value_to_name (parent_tag),
4450 MakeUserID(orig_die->GetOffset()),
4451 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00004452 }
4453 break;
4454
4455 case DW_TAG_subprogram:
4456 case DW_TAG_inlined_subroutine:
4457 case DW_TAG_lexical_block:
4458 if (sc.function != NULL)
4459 {
4460 // Check to see if we already have parsed the variables for the given scope
4461
Greg Clayton81c22f62011-10-19 18:09:39 +00004462 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00004463 if (block == NULL)
4464 {
4465 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004466 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00004467 // to find the concrete block so we can correctly add the
4468 // variable to it
4469 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
4470 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
4471 sc_parent_die->GetOffset(),
4472 &concrete_block_die_cu);
4473 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00004474 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00004475 }
4476
4477 if (block != NULL)
4478 {
4479 const bool can_create = false;
4480 variable_list_sp = block->GetBlockVariableList (can_create);
4481 if (variable_list_sp.get() == NULL)
4482 {
4483 variable_list_sp.reset(new VariableList());
4484 block->SetVariableList(variable_list_sp);
4485 }
4486 }
4487 }
4488 break;
4489
4490 default:
Daniel Malead01b2952012-11-29 21:49:15 +00004491 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00004492 MakeUserID(orig_die->GetOffset()),
4493 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00004494 break;
4495 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004496 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004497
4498 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004499 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00004500 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
4501 if (var_sp)
4502 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004503 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004504 if (cc_variable_list)
4505 cc_variable_list->AddVariableIfUnique (var_sp);
4506 ++vars_added;
4507 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004508 }
4509 }
4510 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004511
4512 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4513
4514 if (!skip_children && parse_children && die->HasChildren())
4515 {
4516 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
4517 }
4518
4519 if (parse_siblings)
4520 die = die->GetSibling();
4521 else
4522 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004523 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004524 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004525}
4526
4527//------------------------------------------------------------------
4528// PluginInterface protocol
4529//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004530ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004531SymbolFileDWARF::GetPluginName()
4532{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004533 return GetPluginNameStatic();
4534}
4535
4536uint32_t
4537SymbolFileDWARF::GetPluginVersion()
4538{
4539 return 1;
4540}
4541
4542void
Sean Callanancc427fa2011-07-30 02:42:06 +00004543SymbolFileDWARF::DumpIndexes ()
4544{
4545 StreamFile s(stdout, false);
4546
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004547 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00004548 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004549 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00004550 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
4551 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
4552 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
4553 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
4554 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
4555 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
4556 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00004557 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00004558}
4559
Greg Claytoncaab74e2012-01-28 00:48:57 +00004560
Greg Clayton1f746072012-08-29 21:13:06 +00004561SymbolFileDWARFDebugMap *
4562SymbolFileDWARF::GetDebugMapSymfile ()
4563{
4564 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
4565 {
4566 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
4567 if (module_sp)
4568 {
4569 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
4570 if (sym_vendor)
4571 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
4572 }
4573 }
4574 return m_debug_map_symfile;
4575}
4576
Greg Claytoncaab74e2012-01-28 00:48:57 +00004577