1 | //===-- SymbolFileDWARFDebugMap.cpp ---------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "SymbolFileDWARFDebugMap.h" |
10 | #include "DWARFCompileUnit.h" |
11 | #include "DWARFDebugAranges.h" |
12 | #include "DWARFDebugInfo.h" |
13 | |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/ModuleList.h" |
16 | #include "lldb/Core/PluginManager.h" |
17 | #include "lldb/Core/Progress.h" |
18 | #include "lldb/Core/Section.h" |
19 | #include "lldb/Host/FileSystem.h" |
20 | #include "lldb/Utility/RangeMap.h" |
21 | #include "lldb/Utility/RegularExpression.h" |
22 | #include "lldb/Utility/StreamString.h" |
23 | #include "lldb/Utility/StructuredData.h" |
24 | #include "lldb/Utility/Timer.h" |
25 | |
26 | //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT |
27 | |
28 | #include "lldb/Symbol/CompileUnit.h" |
29 | #include "lldb/Symbol/LineTable.h" |
30 | #include "lldb/Symbol/ObjectFile.h" |
31 | #include "lldb/Symbol/SymbolVendor.h" |
32 | #include "lldb/Symbol/TypeMap.h" |
33 | #include "lldb/Symbol/VariableList.h" |
34 | #include "llvm/ADT/STLExtras.h" |
35 | #include "llvm/ADT/StringRef.h" |
36 | #include "llvm/Support/ScopedPrinter.h" |
37 | |
38 | #include "lldb/Target/StackFrame.h" |
39 | |
40 | #include "LogChannelDWARF.h" |
41 | #include "SymbolFileDWARF.h" |
42 | #include "lldb/lldb-private-enumerations.h" |
43 | |
44 | #include <memory> |
45 | #include <optional> |
46 | |
47 | using namespace lldb; |
48 | using namespace lldb_private; |
49 | using namespace lldb_private::plugin::dwarf; |
50 | |
51 | char SymbolFileDWARFDebugMap::ID; |
52 | |
53 | // Subclass lldb_private::Module so we can intercept the |
54 | // "Module::GetObjectFile()" (so we can fixup the object file sections) and |
55 | // also for "Module::GetSymbolFile()" (so we can fixup the symbol file id. |
56 | |
57 | const SymbolFileDWARFDebugMap::FileRangeMap & |
58 | SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap( |
59 | SymbolFileDWARFDebugMap *exe_symfile) { |
60 | if (file_range_map_valid) |
61 | return file_range_map; |
62 | |
63 | file_range_map_valid = true; |
64 | |
65 | Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(comp_unit_info: this); |
66 | if (!oso_module) |
67 | return file_range_map; |
68 | |
69 | ObjectFile *oso_objfile = oso_module->GetObjectFile(); |
70 | if (!oso_objfile) |
71 | return file_range_map; |
72 | |
73 | Log *log = GetLog(mask: DWARFLog::DebugMap); |
74 | LLDB_LOGF( |
75 | log, |
76 | "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')" , |
77 | static_cast<void *>(this), |
78 | oso_module->GetSpecificationDescription().c_str()); |
79 | |
80 | std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos; |
81 | if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) { |
82 | for (auto comp_unit_info : cu_infos) { |
83 | Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab(); |
84 | ModuleSP oso_module_sp(oso_objfile->GetModule()); |
85 | Symtab *oso_symtab = oso_objfile->GetSymtab(); |
86 | |
87 | /// const uint32_t fun_resolve_flags = SymbolContext::Module | |
88 | /// eSymbolContextCompUnit | eSymbolContextFunction; |
89 | // SectionList *oso_sections = oso_objfile->Sections(); |
90 | // Now we need to make sections that map from zero based object file |
91 | // addresses to where things ended up in the main executable. |
92 | |
93 | assert(comp_unit_info->first_symbol_index != UINT32_MAX); |
94 | // End index is one past the last valid symbol index |
95 | const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1; |
96 | for (uint32_t idx = comp_unit_info->first_symbol_index + |
97 | 2; // Skip the N_SO and N_OSO |
98 | idx < oso_end_idx; ++idx) { |
99 | Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx); |
100 | if (exe_symbol) { |
101 | if (!exe_symbol->IsDebug()) |
102 | continue; |
103 | |
104 | switch (exe_symbol->GetType()) { |
105 | default: |
106 | break; |
107 | |
108 | case eSymbolTypeCode: { |
109 | // For each N_FUN, or function that we run into in the debug map we |
110 | // make a new section that we add to the sections found in the .o |
111 | // file. This new section has the file address set to what the |
112 | // addresses are in the .o file, and the load address is adjusted |
113 | // to match where it ended up in the final executable! We do this |
114 | // before we parse any dwarf info so that when it goes get parsed |
115 | // all section/offset addresses that get registered will resolve |
116 | // correctly to the new addresses in the main executable. |
117 | |
118 | // First we find the original symbol in the .o file's symbol table |
119 | Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType( |
120 | name: exe_symbol->GetMangled().GetName(preference: Mangled::ePreferMangled), |
121 | symbol_type: eSymbolTypeCode, symbol_debug_type: Symtab::eDebugNo, symbol_visibility: Symtab::eVisibilityAny); |
122 | if (oso_fun_symbol) { |
123 | // Add the inverse OSO file address to debug map entry mapping |
124 | exe_symfile->AddOSOFileRange( |
125 | cu_info: this, exe_file_addr: exe_symbol->GetAddressRef().GetFileAddress(), |
126 | exe_byte_size: exe_symbol->GetByteSize(), |
127 | oso_file_addr: oso_fun_symbol->GetAddressRef().GetFileAddress(), |
128 | oso_byte_size: oso_fun_symbol->GetByteSize()); |
129 | } |
130 | } break; |
131 | |
132 | case eSymbolTypeData: { |
133 | // For each N_GSYM we remap the address for the global by making a |
134 | // new section that we add to the sections found in the .o file. |
135 | // This new section has the file address set to what the addresses |
136 | // are in the .o file, and the load address is adjusted to match |
137 | // where it ended up in the final executable! We do this before we |
138 | // parse any dwarf info so that when it goes get parsed all |
139 | // section/offset addresses that get registered will resolve |
140 | // correctly to the new addresses in the main executable. We |
141 | // initially set the section size to be 1 byte, but will need to |
142 | // fix up these addresses further after all globals have been |
143 | // parsed to span the gaps, or we can find the global variable |
144 | // sizes from the DWARF info as we are parsing. |
145 | |
146 | // Next we find the non-stab entry that corresponds to the N_GSYM |
147 | // in the .o file |
148 | Symbol *oso_gsym_symbol = |
149 | oso_symtab->FindFirstSymbolWithNameAndType( |
150 | name: exe_symbol->GetMangled().GetName(preference: Mangled::ePreferMangled), |
151 | symbol_type: eSymbolTypeData, symbol_debug_type: Symtab::eDebugNo, symbol_visibility: Symtab::eVisibilityAny); |
152 | if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() && |
153 | oso_gsym_symbol->ValueIsAddress()) { |
154 | // Add the inverse OSO file address to debug map entry mapping |
155 | exe_symfile->AddOSOFileRange( |
156 | cu_info: this, exe_file_addr: exe_symbol->GetAddressRef().GetFileAddress(), |
157 | exe_byte_size: exe_symbol->GetByteSize(), |
158 | oso_file_addr: oso_gsym_symbol->GetAddressRef().GetFileAddress(), |
159 | oso_byte_size: oso_gsym_symbol->GetByteSize()); |
160 | } |
161 | } break; |
162 | } |
163 | } |
164 | } |
165 | |
166 | exe_symfile->FinalizeOSOFileRanges(cu_info: this); |
167 | // We don't need the symbols anymore for the .o files |
168 | oso_objfile->ClearSymtab(); |
169 | } |
170 | } |
171 | return file_range_map; |
172 | } |
173 | |
174 | namespace lldb_private::plugin { |
175 | namespace dwarf { |
176 | class DebugMapModule : public Module { |
177 | public: |
178 | DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx, |
179 | const FileSpec &file_spec, const ArchSpec &arch, |
180 | ConstString object_name, off_t object_offset, |
181 | const llvm::sys::TimePoint<> object_mod_time) |
182 | : Module(file_spec, arch, object_name, object_offset, object_mod_time), |
183 | m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {} |
184 | |
185 | ~DebugMapModule() override = default; |
186 | |
187 | SymbolFile * |
188 | GetSymbolFile(bool can_create = true, |
189 | lldb_private::Stream *feedback_strm = nullptr) override { |
190 | // Scope for locker |
191 | if (m_symfile_up.get() || !can_create) |
192 | return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr; |
193 | |
194 | ModuleSP exe_module_sp(m_exe_module_wp.lock()); |
195 | if (exe_module_sp) { |
196 | // Now get the object file outside of a locking scope |
197 | ObjectFile *oso_objfile = GetObjectFile(); |
198 | if (oso_objfile) { |
199 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
200 | if (SymbolFile *symfile = |
201 | Module::GetSymbolFile(can_create, feedback_strm)) { |
202 | // Set a pointer to this class to set our OSO DWARF file know that |
203 | // the DWARF is being used along with a debug map and that it will |
204 | // have the remapped sections that we do below. |
205 | SymbolFileDWARF *oso_symfile = |
206 | SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(sym_file: symfile); |
207 | |
208 | if (!oso_symfile) |
209 | return nullptr; |
210 | |
211 | ObjectFile *exe_objfile = exe_module_sp->GetObjectFile(); |
212 | SymbolFile *exe_symfile = exe_module_sp->GetSymbolFile(); |
213 | |
214 | if (exe_objfile && exe_symfile) { |
215 | oso_symfile->SetDebugMapModule(exe_module_sp); |
216 | // Set the ID of the symbol file DWARF to the index of the OSO |
217 | // shifted left by 32 bits to provide a unique prefix for any |
218 | // UserID's that get created in the symbol file. |
219 | oso_symfile->SetFileIndex((uint64_t)m_cu_idx); |
220 | } |
221 | return symfile; |
222 | } |
223 | } |
224 | } |
225 | return nullptr; |
226 | } |
227 | |
228 | protected: |
229 | ModuleWP m_exe_module_wp; |
230 | const uint32_t m_cu_idx; |
231 | }; |
232 | } // namespace dwarf |
233 | } // namespace lldb_private::plugin |
234 | |
235 | void SymbolFileDWARFDebugMap::Initialize() { |
236 | PluginManager::RegisterPlugin(name: GetPluginNameStatic(), |
237 | description: GetPluginDescriptionStatic(), create_callback: CreateInstance); |
238 | } |
239 | |
240 | void SymbolFileDWARFDebugMap::Terminate() { |
241 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
242 | } |
243 | |
244 | llvm::StringRef SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() { |
245 | return "DWARF and DWARF3 debug symbol file reader (debug map)." ; |
246 | } |
247 | |
248 | SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) { |
249 | return new SymbolFileDWARFDebugMap(std::move(objfile_sp)); |
250 | } |
251 | |
252 | SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp) |
253 | : SymbolFileCommon(std::move(objfile_sp)), m_flags(), |
254 | m_compile_unit_infos(), m_func_indexes(), m_glob_indexes() {} |
255 | |
256 | SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() = default; |
257 | |
258 | void SymbolFileDWARFDebugMap::InitializeObject() {} |
259 | |
260 | void SymbolFileDWARFDebugMap::InitOSO() { |
261 | if (m_flags.test(position: kHaveInitializedOSOs)) |
262 | return; |
263 | |
264 | m_flags.set(position: kHaveInitializedOSOs); |
265 | |
266 | // If the object file has been stripped, there is no sense in looking further |
267 | // as all of the debug symbols for the debug map will not be available |
268 | if (m_objfile_sp->IsStripped()) |
269 | return; |
270 | |
271 | // Also make sure the file type is some sort of executable. Core files, debug |
272 | // info files (dSYM), object files (.o files), and stub libraries all can |
273 | switch (m_objfile_sp->GetType()) { |
274 | case ObjectFile::eTypeInvalid: |
275 | case ObjectFile::eTypeCoreFile: |
276 | case ObjectFile::eTypeDebugInfo: |
277 | case ObjectFile::eTypeObjectFile: |
278 | case ObjectFile::eTypeStubLibrary: |
279 | case ObjectFile::eTypeUnknown: |
280 | case ObjectFile::eTypeJIT: |
281 | return; |
282 | |
283 | case ObjectFile::eTypeExecutable: |
284 | case ObjectFile::eTypeDynamicLinker: |
285 | case ObjectFile::eTypeSharedLibrary: |
286 | break; |
287 | } |
288 | |
289 | // In order to get the abilities of this plug-in, we look at the list of |
290 | // N_OSO entries (object files) from the symbol table and make sure that |
291 | // these files exist and also contain valid DWARF. If we get any of that then |
292 | // we return the abilities of the first N_OSO's DWARF. |
293 | |
294 | Symtab *symtab = m_objfile_sp->GetSymtab(); |
295 | if (!symtab) |
296 | return; |
297 | |
298 | Log *log = GetLog(mask: DWARFLog::DebugMap); |
299 | |
300 | std::vector<uint32_t> oso_indexes; |
301 | // When a mach-o symbol is encoded, the n_type field is encoded in bits |
302 | // 23:16, and the n_desc field is encoded in bits 15:0. |
303 | // |
304 | // To find all N_OSO entries that are part of the DWARF + debug map we find |
305 | // only object file symbols with the flags value as follows: bits 23:16 == |
306 | // 0x66 (N_OSO) bits 15: 0 == 0x0001 (specifies this is a debug map object |
307 | // file) |
308 | const uint32_t k_oso_symbol_flags_value = 0x660001u; |
309 | |
310 | const uint32_t oso_index_count = |
311 | symtab->AppendSymbolIndexesWithTypeAndFlagsValue( |
312 | symbol_type: eSymbolTypeObjectFile, flags_value: k_oso_symbol_flags_value, indexes&: oso_indexes); |
313 | |
314 | if (oso_index_count == 0) |
315 | return; |
316 | |
317 | symtab->AppendSymbolIndexesWithType(symbol_type: eSymbolTypeCode, symbol_debug_type: Symtab::eDebugYes, |
318 | symbol_visibility: Symtab::eVisibilityAny, matches&: m_func_indexes); |
319 | symtab->AppendSymbolIndexesWithType(symbol_type: eSymbolTypeData, symbol_debug_type: Symtab::eDebugYes, |
320 | symbol_visibility: Symtab::eVisibilityAny, matches&: m_glob_indexes); |
321 | |
322 | symtab->SortSymbolIndexesByValue(indexes&: m_func_indexes, remove_duplicates: true); |
323 | symtab->SortSymbolIndexesByValue(indexes&: m_glob_indexes, remove_duplicates: true); |
324 | |
325 | for (uint32_t sym_idx : |
326 | llvm::concat<uint32_t>(Ranges&: m_func_indexes, Ranges&: m_glob_indexes)) { |
327 | const Symbol *symbol = symtab->SymbolAtIndex(idx: sym_idx); |
328 | lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress(); |
329 | lldb::addr_t byte_size = symbol->GetByteSize(); |
330 | DebugMap::Entry debug_map_entry(file_addr, byte_size, |
331 | OSOEntry(sym_idx, LLDB_INVALID_ADDRESS)); |
332 | m_debug_map.Append(entry: debug_map_entry); |
333 | } |
334 | m_debug_map.Sort(); |
335 | |
336 | m_compile_unit_infos.resize(new_size: oso_index_count); |
337 | |
338 | for (uint32_t i = 0; i < oso_index_count; ++i) { |
339 | const uint32_t so_idx = oso_indexes[i] - 1; |
340 | const uint32_t oso_idx = oso_indexes[i]; |
341 | const Symbol *so_symbol = symtab->SymbolAtIndex(idx: so_idx); |
342 | const Symbol *oso_symbol = symtab->SymbolAtIndex(idx: oso_idx); |
343 | if (so_symbol && oso_symbol && |
344 | so_symbol->GetType() == eSymbolTypeSourceFile && |
345 | oso_symbol->GetType() == eSymbolTypeObjectFile) { |
346 | m_compile_unit_infos[i].so_file.SetFile(path: so_symbol->GetName().AsCString(), |
347 | style: FileSpec::Style::native); |
348 | m_compile_unit_infos[i].oso_path = oso_symbol->GetName(); |
349 | m_compile_unit_infos[i].oso_mod_time = |
350 | llvm::sys::toTimePoint(T: oso_symbol->GetIntegerValue(fail_value: 0)); |
351 | uint32_t sibling_idx = so_symbol->GetSiblingIndex(); |
352 | // The sibling index can't be less that or equal to the current index |
353 | // "i" |
354 | if (sibling_idx <= i || sibling_idx == UINT32_MAX) { |
355 | m_objfile_sp->GetModule()->ReportError( |
356 | format: "N_SO in symbol with UID {0} has invalid sibling in debug " |
357 | "map, " |
358 | "please file a bug and attach the binary listed in this error" , |
359 | args: so_symbol->GetID()); |
360 | } else { |
361 | const Symbol *last_symbol = symtab->SymbolAtIndex(idx: sibling_idx - 1); |
362 | m_compile_unit_infos[i].first_symbol_index = so_idx; |
363 | m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1; |
364 | m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID(); |
365 | m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID(); |
366 | |
367 | LLDB_LOGF(log, "Initialized OSO 0x%8.8x: file=%s" , i, |
368 | oso_symbol->GetName().GetCString()); |
369 | } |
370 | } else { |
371 | if (oso_symbol == nullptr) |
372 | m_objfile_sp->GetModule()->ReportError( |
373 | format: "N_OSO symbol[{0}] can't be found, please file a bug and " |
374 | "attach " |
375 | "the binary listed in this error" , |
376 | args: oso_idx); |
377 | else if (so_symbol == nullptr) |
378 | m_objfile_sp->GetModule()->ReportError( |
379 | format: "N_SO not found for N_OSO symbol[{0}], please file a bug and " |
380 | "attach the binary listed in this error" , |
381 | args: oso_idx); |
382 | else if (so_symbol->GetType() != eSymbolTypeSourceFile) |
383 | m_objfile_sp->GetModule()->ReportError( |
384 | format: "N_SO has incorrect symbol type ({0}) for N_OSO " |
385 | "symbol[{1}], " |
386 | "please file a bug and attach the binary listed in this error" , |
387 | args: so_symbol->GetType(), args: oso_idx); |
388 | else if (oso_symbol->GetType() != eSymbolTypeSourceFile) |
389 | m_objfile_sp->GetModule()->ReportError( |
390 | format: "N_OSO has incorrect symbol type ({0}) for N_OSO " |
391 | "symbol[{1}], " |
392 | "please file a bug and attach the binary listed in this error" , |
393 | args: oso_symbol->GetType(), args: oso_idx); |
394 | } |
395 | } |
396 | } |
397 | |
398 | Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) { |
399 | const uint32_t cu_count = GetNumCompileUnits(); |
400 | if (oso_idx < cu_count) |
401 | return GetModuleByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[oso_idx]); |
402 | return nullptr; |
403 | } |
404 | |
405 | Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo( |
406 | CompileUnitInfo *comp_unit_info) { |
407 | if (!comp_unit_info->oso_sp) { |
408 | auto pos = m_oso_map.find( |
409 | x: {comp_unit_info->oso_path, comp_unit_info->oso_mod_time}); |
410 | if (pos != m_oso_map.end()) { |
411 | comp_unit_info->oso_sp = pos->second; |
412 | } else { |
413 | ObjectFile *obj_file = GetObjectFile(); |
414 | comp_unit_info->oso_sp = std::make_shared<OSOInfo>(); |
415 | m_oso_map[{comp_unit_info->oso_path, comp_unit_info->oso_mod_time}] = |
416 | comp_unit_info->oso_sp; |
417 | const char *oso_path = comp_unit_info->oso_path.GetCString(); |
418 | FileSpec oso_file(oso_path); |
419 | ConstString oso_object; |
420 | if (FileSystem::Instance().Exists(file_spec: oso_file)) { |
421 | // The modification time returned by the FS can have a higher precision |
422 | // than the one from the CU. |
423 | auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>( |
424 | t: FileSystem::Instance().GetModificationTime(file_spec: oso_file)); |
425 | // A timestamp of 0 means that the linker was in deterministic mode. In |
426 | // that case, we should skip the check against the filesystem last |
427 | // modification timestamp, since it will never match. |
428 | if (comp_unit_info->oso_mod_time != llvm::sys::TimePoint<>() && |
429 | oso_mod_time != comp_unit_info->oso_mod_time) { |
430 | comp_unit_info->oso_load_error = Status::FromErrorStringWithFormat( |
431 | format: "debug map object file \"%s\" changed (actual: 0x%8.8x, debug " |
432 | "map: 0x%8.8x) since this executable was linked, debug info " |
433 | "will not be loaded" , |
434 | oso_file.GetPath().c_str(), |
435 | (uint32_t)llvm::sys::toTimeT(TP: oso_mod_time), |
436 | (uint32_t)llvm::sys::toTimeT(TP: comp_unit_info->oso_mod_time)); |
437 | obj_file->GetModule()->ReportError( |
438 | format: "{0}" , args: comp_unit_info->oso_load_error.AsCString()); |
439 | return nullptr; |
440 | } |
441 | |
442 | } else { |
443 | const bool must_exist = true; |
444 | |
445 | if (!ObjectFile::SplitArchivePathWithObject(path_with_object: oso_path, archive_file&: oso_file, |
446 | archive_object&: oso_object, must_exist)) { |
447 | comp_unit_info->oso_load_error = Status::FromErrorStringWithFormat( |
448 | format: "debug map object file \"%s\" containing debug info does not " |
449 | "exist, debug info will not be loaded" , |
450 | comp_unit_info->oso_path.GetCString()); |
451 | return nullptr; |
452 | } |
453 | } |
454 | // Always create a new module for .o files. Why? Because we use the debug |
455 | // map, to add new sections to each .o file and even though a .o file |
456 | // might not have changed, the sections that get added to the .o file can |
457 | // change. |
458 | ArchSpec oso_arch; |
459 | // Only adopt the architecture from the module (not the vendor or OS) |
460 | // since .o files for "i386-apple-ios" will historically show up as "i386 |
461 | // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or |
462 | // LC_VERSION_MIN_IPHONEOS load command... |
463 | oso_arch.SetTriple(m_objfile_sp->GetModule() |
464 | ->GetArchitecture() |
465 | .GetTriple() |
466 | .getArchName() |
467 | .str() |
468 | .c_str()); |
469 | comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>( |
470 | args: obj_file->GetModule(), args: GetCompUnitInfoIndex(comp_unit_info), args&: oso_file, |
471 | args&: oso_arch, args&: oso_object, args: 0, |
472 | args: oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>()); |
473 | |
474 | if (oso_object && !comp_unit_info->oso_sp->module_sp->GetObjectFile() && |
475 | FileSystem::Instance().Exists(file_spec: oso_file)) { |
476 | // If we are loading a .o file from a .a file the "oso_object" will |
477 | // have a valid value name and if the .a file exists, either the .o |
478 | // file didn't exist in the .a file or the mod time didn't match. |
479 | comp_unit_info->oso_load_error = Status::FromErrorStringWithFormat( |
480 | format: "\"%s\" object from the \"%s\" archive: " |
481 | "either the .o file doesn't exist in the archive or the " |
482 | "modification time (0x%8.8x) of the .o file doesn't match" , |
483 | oso_object.AsCString(), oso_file.GetPath().c_str(), |
484 | (uint32_t)llvm::sys::toTimeT(TP: comp_unit_info->oso_mod_time)); |
485 | } |
486 | } |
487 | } |
488 | if (comp_unit_info->oso_sp) |
489 | return comp_unit_info->oso_sp->module_sp.get(); |
490 | return nullptr; |
491 | } |
492 | |
493 | bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx, |
494 | FileSpec &file_spec) { |
495 | if (oso_idx < m_compile_unit_infos.size()) { |
496 | if (m_compile_unit_infos[oso_idx].so_file) { |
497 | file_spec = m_compile_unit_infos[oso_idx].so_file; |
498 | return true; |
499 | } |
500 | } |
501 | return false; |
502 | } |
503 | |
504 | ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) { |
505 | Module *oso_module = GetModuleByOSOIndex(oso_idx); |
506 | if (oso_module) |
507 | return oso_module->GetObjectFile(); |
508 | return nullptr; |
509 | } |
510 | |
511 | SymbolFileDWARF * |
512 | SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) { |
513 | return GetSymbolFile(comp_unit: *sc.comp_unit); |
514 | } |
515 | |
516 | SymbolFileDWARF * |
517 | SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) { |
518 | CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit); |
519 | if (comp_unit_info) |
520 | return GetSymbolFileByCompUnitInfo(comp_unit_info); |
521 | return nullptr; |
522 | } |
523 | |
524 | ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo( |
525 | CompileUnitInfo *comp_unit_info) { |
526 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); |
527 | if (oso_module) |
528 | return oso_module->GetObjectFile(); |
529 | return nullptr; |
530 | } |
531 | |
532 | uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex( |
533 | const CompileUnitInfo *comp_unit_info) { |
534 | if (!m_compile_unit_infos.empty()) { |
535 | const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front(); |
536 | const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back(); |
537 | if (first_comp_unit_info <= comp_unit_info && |
538 | comp_unit_info <= last_comp_unit_info) |
539 | return comp_unit_info - first_comp_unit_info; |
540 | } |
541 | return UINT32_MAX; |
542 | } |
543 | |
544 | SymbolFileDWARF * |
545 | SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) { |
546 | unsigned size = m_compile_unit_infos.size(); |
547 | if (oso_idx < size) |
548 | return GetSymbolFileByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[oso_idx]); |
549 | return nullptr; |
550 | } |
551 | |
552 | SymbolFileDWARF * |
553 | SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) { |
554 | if (sym_file && |
555 | sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic()) |
556 | return static_cast<SymbolFileDWARF *>(sym_file); |
557 | return nullptr; |
558 | } |
559 | |
560 | SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo( |
561 | CompileUnitInfo *comp_unit_info) { |
562 | if (Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info)) |
563 | return GetSymbolFileAsSymbolFileDWARF(sym_file: oso_module->GetSymbolFile()); |
564 | return nullptr; |
565 | } |
566 | |
567 | uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() { |
568 | // In order to get the abilities of this plug-in, we look at the list of |
569 | // N_OSO entries (object files) from the symbol table and make sure that |
570 | // these files exist and also contain valid DWARF. If we get any of that then |
571 | // we return the abilities of the first N_OSO's DWARF. |
572 | |
573 | const uint32_t oso_index_count = GetNumCompileUnits(); |
574 | if (oso_index_count > 0) { |
575 | InitOSO(); |
576 | if (!m_compile_unit_infos.empty()) { |
577 | return SymbolFile::CompileUnits | SymbolFile::Functions | |
578 | SymbolFile::Blocks | SymbolFile::GlobalVariables | |
579 | SymbolFile::LocalVariables | SymbolFile::VariableTypes | |
580 | SymbolFile::LineTables; |
581 | } |
582 | } |
583 | return 0; |
584 | } |
585 | |
586 | uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() { |
587 | InitOSO(); |
588 | return m_compile_unit_infos.size(); |
589 | } |
590 | |
591 | CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) { |
592 | CompUnitSP comp_unit_sp; |
593 | const uint32_t cu_count = GetNumCompileUnits(); |
594 | |
595 | if (cu_idx < cu_count) { |
596 | auto &cu_info = m_compile_unit_infos[cu_idx]; |
597 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info: &cu_info); |
598 | if (oso_module) { |
599 | FileSpec so_file_spec; |
600 | if (GetFileSpecForSO(oso_idx: cu_idx, file_spec&: so_file_spec)) { |
601 | // User zero as the ID to match the compile unit at offset zero in each |
602 | // .o file. |
603 | lldb::user_id_t cu_id = 0; |
604 | cu_info.compile_units_sps.push_back(Elt: std::make_shared<CompileUnit>( |
605 | args: m_objfile_sp->GetModule(), args: nullptr, |
606 | args: std::make_shared<SupportFile>(args&: so_file_spec), args&: cu_id, |
607 | args: eLanguageTypeUnknown, args: eLazyBoolCalculate)); |
608 | cu_info.id_to_index_map.insert(KV: {0, 0}); |
609 | SetCompileUnitAtIndex(idx: cu_idx, cu_sp: cu_info.compile_units_sps[0]); |
610 | // If there's a symbol file also register all the extra compile units. |
611 | if (SymbolFileDWARF *oso_symfile = |
612 | GetSymbolFileByCompUnitInfo(comp_unit_info: &cu_info)) { |
613 | auto num_dwarf_units = oso_symfile->DebugInfo().GetNumUnits(); |
614 | for (size_t i = 0; i < num_dwarf_units; ++i) { |
615 | auto *dwarf_unit = oso_symfile->DebugInfo().GetUnitAtIndex(idx: i); |
616 | if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(Val: dwarf_unit)) { |
617 | // The "main" one was already registered. |
618 | if (dwarf_cu->GetID() == 0) |
619 | continue; |
620 | cu_info.compile_units_sps.push_back(Elt: std::make_shared<CompileUnit>( |
621 | args: m_objfile_sp->GetModule(), args: nullptr, |
622 | args: std::make_shared<SupportFile>(args&: so_file_spec), |
623 | args: dwarf_cu->GetID(), args: eLanguageTypeUnknown, args: eLazyBoolCalculate)); |
624 | cu_info.id_to_index_map.insert( |
625 | KV: {dwarf_cu->GetID(), cu_info.compile_units_sps.size() - 1}); |
626 | } |
627 | } |
628 | } |
629 | } |
630 | } |
631 | if (!cu_info.compile_units_sps.empty()) |
632 | comp_unit_sp = cu_info.compile_units_sps[0]; |
633 | } |
634 | |
635 | return comp_unit_sp; |
636 | } |
637 | |
638 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
639 | SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) { |
640 | return GetCompUnitInfo(comp_unit: *sc.comp_unit); |
641 | } |
642 | |
643 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
644 | SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) { |
645 | const uint32_t cu_count = GetNumCompileUnits(); |
646 | for (uint32_t i = 0; i < cu_count; ++i) { |
647 | auto &id_to_index_map = m_compile_unit_infos[i].id_to_index_map; |
648 | |
649 | auto it = id_to_index_map.find(Val: comp_unit.GetID()); |
650 | if (it != id_to_index_map.end() && |
651 | &comp_unit == |
652 | m_compile_unit_infos[i].compile_units_sps[it->getSecond()].get()) |
653 | return &m_compile_unit_infos[i]; |
654 | } |
655 | return nullptr; |
656 | } |
657 | |
658 | size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule( |
659 | const lldb_private::Module *module, |
660 | std::vector<CompileUnitInfo *> &cu_infos) { |
661 | const uint32_t cu_count = GetNumCompileUnits(); |
662 | for (uint32_t i = 0; i < cu_count; ++i) { |
663 | if (module == GetModuleByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[i])) |
664 | cu_infos.push_back(x: &m_compile_unit_infos[i]); |
665 | } |
666 | return cu_infos.size(); |
667 | } |
668 | |
669 | lldb::LanguageType |
670 | SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) { |
671 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
672 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
673 | if (oso_dwarf) |
674 | return oso_dwarf->ParseLanguage(comp_unit); |
675 | return eLanguageTypeUnknown; |
676 | } |
677 | |
678 | XcodeSDK SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) { |
679 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
680 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
681 | if (oso_dwarf) |
682 | return oso_dwarf->ParseXcodeSDK(comp_unit); |
683 | return {}; |
684 | } |
685 | |
686 | llvm::SmallSet<lldb::LanguageType, 4> |
687 | SymbolFileDWARFDebugMap::ParseAllLanguages( |
688 | lldb_private::CompileUnit &comp_unit) { |
689 | llvm::SmallSet<lldb::LanguageType, 4> langs; |
690 | auto *info = GetCompUnitInfo(comp_unit); |
691 | for (auto &comp_unit : info->compile_units_sps) { |
692 | langs.insert(V: comp_unit->GetLanguage()); |
693 | } |
694 | return langs; |
695 | } |
696 | |
697 | size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) { |
698 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
699 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
700 | if (oso_dwarf) |
701 | return oso_dwarf->ParseFunctions(comp_unit); |
702 | return 0; |
703 | } |
704 | |
705 | bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) { |
706 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
707 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
708 | if (oso_dwarf) |
709 | return oso_dwarf->ParseLineTable(comp_unit); |
710 | return false; |
711 | } |
712 | |
713 | bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) { |
714 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
715 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
716 | if (oso_dwarf) |
717 | return oso_dwarf->ParseDebugMacros(comp_unit); |
718 | return false; |
719 | } |
720 | |
721 | void SymbolFileDWARFDebugMap::ForEachSymbolFile( |
722 | std::string description, |
723 | std::function<IterationAction(SymbolFileDWARF &)> closure) { |
724 | const size_t num_oso_idxs = m_compile_unit_infos.size(); |
725 | Progress progress(std::move(description), "" , num_oso_idxs, |
726 | /*debugger=*/nullptr, |
727 | Progress::kDefaultHighFrequencyReportTime); |
728 | for (uint32_t oso_idx = 0; oso_idx < num_oso_idxs; ++oso_idx) { |
729 | if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) { |
730 | progress.Increment(amount: oso_idx, updated_detail: oso_dwarf->GetObjectName()); |
731 | if (closure(*oso_dwarf) == IterationAction::Stop) |
732 | return; |
733 | } |
734 | } |
735 | } |
736 | |
737 | bool SymbolFileDWARFDebugMap::ForEachExternalModule( |
738 | CompileUnit &comp_unit, |
739 | llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files, |
740 | llvm::function_ref<bool(Module &)> f) { |
741 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
742 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
743 | if (oso_dwarf) |
744 | return oso_dwarf->ForEachExternalModule(comp_unit, visited_symbol_files, f); |
745 | return false; |
746 | } |
747 | |
748 | bool SymbolFileDWARFDebugMap::ParseSupportFiles( |
749 | CompileUnit &comp_unit, SupportFileList &support_files) { |
750 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
751 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
752 | if (oso_dwarf) |
753 | return oso_dwarf->ParseSupportFiles(comp_unit, support_files); |
754 | return false; |
755 | } |
756 | |
757 | bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) { |
758 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
759 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
760 | if (oso_dwarf) |
761 | return oso_dwarf->ParseIsOptimized(comp_unit); |
762 | return false; |
763 | } |
764 | |
765 | bool SymbolFileDWARFDebugMap::ParseImportedModules( |
766 | const SymbolContext &sc, std::vector<SourceModule> &imported_modules) { |
767 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
768 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); |
769 | if (oso_dwarf) |
770 | return oso_dwarf->ParseImportedModules(sc, imported_modules); |
771 | return false; |
772 | } |
773 | |
774 | size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) { |
775 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
776 | CompileUnit *comp_unit = func.GetCompileUnit(); |
777 | if (!comp_unit) |
778 | return 0; |
779 | |
780 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit: *comp_unit); |
781 | if (oso_dwarf) |
782 | return oso_dwarf->ParseBlocksRecursive(func); |
783 | return 0; |
784 | } |
785 | |
786 | size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) { |
787 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
788 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit); |
789 | if (oso_dwarf) |
790 | return oso_dwarf->ParseTypes(comp_unit); |
791 | return 0; |
792 | } |
793 | |
794 | size_t |
795 | SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) { |
796 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
797 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); |
798 | if (oso_dwarf) |
799 | return oso_dwarf->ParseVariablesForContext(sc); |
800 | return 0; |
801 | } |
802 | |
803 | Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) { |
804 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
805 | const uint64_t oso_idx = GetOSOIndexFromUserID(uid: type_uid); |
806 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); |
807 | if (oso_dwarf) |
808 | return oso_dwarf->ResolveTypeUID(type_uid); |
809 | return nullptr; |
810 | } |
811 | |
812 | std::optional<SymbolFile::ArrayInfo> |
813 | SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID( |
814 | lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { |
815 | const uint64_t oso_idx = GetOSOIndexFromUserID(uid: type_uid); |
816 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); |
817 | if (oso_dwarf) |
818 | return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx); |
819 | return std::nullopt; |
820 | } |
821 | |
822 | bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) { |
823 | bool success = false; |
824 | if (compiler_type) { |
825 | ForEachSymbolFile(description: "Completing type" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
826 | if (oso_dwarf.HasForwardDeclForCompilerType(compiler_type)) { |
827 | oso_dwarf.CompleteType(compiler_type); |
828 | success = true; |
829 | return IterationAction::Stop; |
830 | } |
831 | return IterationAction::Continue; |
832 | }); |
833 | } |
834 | return success; |
835 | } |
836 | |
837 | uint32_t |
838 | SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr, |
839 | SymbolContextItem resolve_scope, |
840 | SymbolContext &sc) { |
841 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
842 | uint32_t resolved_flags = 0; |
843 | Symtab *symtab = m_objfile_sp->GetSymtab(); |
844 | if (symtab) { |
845 | const addr_t exe_file_addr = exe_so_addr.GetFileAddress(); |
846 | |
847 | const DebugMap::Entry *debug_map_entry = |
848 | m_debug_map.FindEntryThatContains(addr: exe_file_addr); |
849 | if (debug_map_entry) { |
850 | |
851 | sc.symbol = |
852 | symtab->SymbolAtIndex(idx: debug_map_entry->data.GetExeSymbolIndex()); |
853 | |
854 | if (sc.symbol != nullptr) { |
855 | resolved_flags |= eSymbolContextSymbol; |
856 | |
857 | uint32_t oso_idx = 0; |
858 | CompileUnitInfo *comp_unit_info = |
859 | GetCompileUnitInfoForSymbolWithID(symbol_id: sc.symbol->GetID(), oso_idx_ptr: &oso_idx); |
860 | if (comp_unit_info) { |
861 | comp_unit_info->GetFileRangeMap(exe_symfile: this); |
862 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); |
863 | if (oso_module) { |
864 | lldb::addr_t oso_file_addr = |
865 | exe_file_addr - debug_map_entry->GetRangeBase() + |
866 | debug_map_entry->data.GetOSOFileAddress(); |
867 | Address oso_so_addr; |
868 | if (oso_module->ResolveFileAddress(vm_addr: oso_file_addr, so_addr&: oso_so_addr)) { |
869 | if (SymbolFile *sym_file = oso_module->GetSymbolFile()) { |
870 | resolved_flags |= sym_file->ResolveSymbolContext( |
871 | so_addr: oso_so_addr, resolve_scope, sc); |
872 | } else { |
873 | ObjectFile *obj_file = GetObjectFile(); |
874 | LLDB_LOG(GetLog(DWARFLog::DebugMap), |
875 | "Failed to get symfile for OSO: {0} in module: {1}" , |
876 | oso_module->GetFileSpec(), |
877 | obj_file ? obj_file->GetFileSpec() |
878 | : FileSpec("unknown" )); |
879 | } |
880 | } |
881 | } |
882 | } |
883 | } |
884 | } |
885 | } |
886 | return resolved_flags; |
887 | } |
888 | |
889 | uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext( |
890 | const SourceLocationSpec &src_location_spec, |
891 | SymbolContextItem resolve_scope, SymbolContextList &sc_list) { |
892 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
893 | const uint32_t initial = sc_list.GetSize(); |
894 | const uint32_t cu_count = GetNumCompileUnits(); |
895 | |
896 | for (uint32_t i = 0; i < cu_count; ++i) { |
897 | // If we are checking for inlines, then we need to look through all compile |
898 | // units no matter if "file_spec" matches. |
899 | bool resolve = src_location_spec.GetCheckInlines(); |
900 | |
901 | if (!resolve) { |
902 | FileSpec so_file_spec; |
903 | if (GetFileSpecForSO(oso_idx: i, file_spec&: so_file_spec)) |
904 | resolve = |
905 | FileSpec::Match(pattern: src_location_spec.GetFileSpec(), file: so_file_spec); |
906 | } |
907 | if (resolve) { |
908 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx: i); |
909 | if (oso_dwarf) |
910 | oso_dwarf->ResolveSymbolContext(src_location_spec, resolve_scope, |
911 | sc_list); |
912 | } |
913 | } |
914 | return sc_list.GetSize() - initial; |
915 | } |
916 | |
917 | void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables( |
918 | ConstString name, const CompilerDeclContext &parent_decl_ctx, |
919 | const std::vector<uint32_t> |
920 | &indexes, // Indexes into the symbol table that match "name" |
921 | uint32_t max_matches, VariableList &variables) { |
922 | const size_t match_count = indexes.size(); |
923 | for (size_t i = 0; i < match_count; ++i) { |
924 | uint32_t oso_idx; |
925 | CompileUnitInfo *comp_unit_info = |
926 | GetCompileUnitInfoForSymbolWithIndex(symbol_idx: indexes[i], oso_idx_ptr: &oso_idx); |
927 | if (comp_unit_info) { |
928 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); |
929 | if (oso_dwarf) { |
930 | oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches, |
931 | variables); |
932 | if (variables.GetSize() > max_matches) |
933 | break; |
934 | } |
935 | } |
936 | } |
937 | } |
938 | |
939 | void SymbolFileDWARFDebugMap::FindGlobalVariables( |
940 | ConstString name, const CompilerDeclContext &parent_decl_ctx, |
941 | uint32_t max_matches, VariableList &variables) { |
942 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
943 | uint32_t total_matches = 0; |
944 | |
945 | ForEachSymbolFile( |
946 | description: "Looking up global variables" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
947 | const uint32_t old_size = variables.GetSize(); |
948 | oso_dwarf.FindGlobalVariables(name, parent_decl_ctx, max_matches, |
949 | variables); |
950 | const uint32_t oso_matches = variables.GetSize() - old_size; |
951 | if (oso_matches > 0) { |
952 | total_matches += oso_matches; |
953 | |
954 | // If we are getting all matches, keep going. |
955 | if (max_matches == UINT32_MAX) |
956 | return IterationAction::Continue; |
957 | |
958 | // If we have found enough matches, lets get out |
959 | if (max_matches >= total_matches) |
960 | return IterationAction::Stop; |
961 | |
962 | // Update the max matches for any subsequent calls to find globals in |
963 | // any other object files with DWARF |
964 | max_matches -= oso_matches; |
965 | } |
966 | |
967 | return IterationAction::Continue; |
968 | }); |
969 | } |
970 | |
971 | void SymbolFileDWARFDebugMap::FindGlobalVariables( |
972 | const RegularExpression ®ex, uint32_t max_matches, |
973 | VariableList &variables) { |
974 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
975 | uint32_t total_matches = 0; |
976 | ForEachSymbolFile( |
977 | description: "Looking up global variables" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
978 | const uint32_t old_size = variables.GetSize(); |
979 | oso_dwarf.FindGlobalVariables(regex, max_matches, variables); |
980 | |
981 | const uint32_t oso_matches = variables.GetSize() - old_size; |
982 | if (oso_matches > 0) { |
983 | total_matches += oso_matches; |
984 | |
985 | // If we are getting all matches, keep going. |
986 | if (max_matches == UINT32_MAX) |
987 | return IterationAction::Continue; |
988 | |
989 | // If we have found enough matches, lets get out |
990 | if (max_matches >= total_matches) |
991 | return IterationAction::Stop; |
992 | |
993 | // Update the max matches for any subsequent calls to find globals in |
994 | // any other object files with DWARF |
995 | max_matches -= oso_matches; |
996 | } |
997 | |
998 | return IterationAction::Continue; |
999 | }); |
1000 | } |
1001 | |
1002 | int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex( |
1003 | uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { |
1004 | const uint32_t symbol_idx = *symbol_idx_ptr; |
1005 | |
1006 | if (symbol_idx < comp_unit_info->first_symbol_index) |
1007 | return -1; |
1008 | |
1009 | if (symbol_idx <= comp_unit_info->last_symbol_index) |
1010 | return 0; |
1011 | |
1012 | return 1; |
1013 | } |
1014 | |
1015 | int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID( |
1016 | user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { |
1017 | const user_id_t symbol_id = *symbol_idx_ptr; |
1018 | |
1019 | if (symbol_id < comp_unit_info->first_symbol_id) |
1020 | return -1; |
1021 | |
1022 | if (symbol_id <= comp_unit_info->last_symbol_id) |
1023 | return 0; |
1024 | |
1025 | return 1; |
1026 | } |
1027 | |
1028 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
1029 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex( |
1030 | uint32_t symbol_idx, uint32_t *oso_idx_ptr) { |
1031 | const uint32_t oso_index_count = m_compile_unit_infos.size(); |
1032 | CompileUnitInfo *comp_unit_info = nullptr; |
1033 | if (oso_index_count) { |
1034 | comp_unit_info = (CompileUnitInfo *)bsearch( |
1035 | key: &symbol_idx, base: &m_compile_unit_infos[0], nmemb: m_compile_unit_infos.size(), |
1036 | size: sizeof(CompileUnitInfo), |
1037 | compar: (ComparisonFunction)SymbolContainsSymbolWithIndex); |
1038 | } |
1039 | |
1040 | if (oso_idx_ptr) { |
1041 | if (comp_unit_info != nullptr) |
1042 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; |
1043 | else |
1044 | *oso_idx_ptr = UINT32_MAX; |
1045 | } |
1046 | return comp_unit_info; |
1047 | } |
1048 | |
1049 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
1050 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID( |
1051 | user_id_t symbol_id, uint32_t *oso_idx_ptr) { |
1052 | const uint32_t oso_index_count = m_compile_unit_infos.size(); |
1053 | CompileUnitInfo *comp_unit_info = nullptr; |
1054 | if (oso_index_count) { |
1055 | comp_unit_info = (CompileUnitInfo *)::bsearch( |
1056 | key: &symbol_id, base: &m_compile_unit_infos[0], nmemb: m_compile_unit_infos.size(), |
1057 | size: sizeof(CompileUnitInfo), |
1058 | compar: (ComparisonFunction)SymbolContainsSymbolWithID); |
1059 | } |
1060 | |
1061 | if (oso_idx_ptr) { |
1062 | if (comp_unit_info != nullptr) |
1063 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; |
1064 | else |
1065 | *oso_idx_ptr = UINT32_MAX; |
1066 | } |
1067 | return comp_unit_info; |
1068 | } |
1069 | |
1070 | static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp, |
1071 | SymbolContextList &sc_list, |
1072 | uint32_t start_idx) { |
1073 | // We found functions in .o files. Not all functions in the .o files will |
1074 | // have made it into the final output file. The ones that did make it into |
1075 | // the final output file will have a section whose module matches the module |
1076 | // from the ObjectFile for this SymbolFile. When the modules don't match, |
1077 | // then we have something that was in a .o file, but doesn't map to anything |
1078 | // in the final executable. |
1079 | uint32_t i = start_idx; |
1080 | while (i < sc_list.GetSize()) { |
1081 | SymbolContext sc; |
1082 | sc_list.GetContextAtIndex(idx: i, sc); |
1083 | if (sc.function) { |
1084 | const SectionSP section_sp = sc.function->GetAddress().GetSection(); |
1085 | if (section_sp->GetModule() != module_sp) { |
1086 | sc_list.RemoveContextAtIndex(idx: i); |
1087 | continue; |
1088 | } |
1089 | } |
1090 | ++i; |
1091 | } |
1092 | } |
1093 | |
1094 | void SymbolFileDWARFDebugMap::FindFunctions( |
1095 | const Module::LookupInfo &lookup_info, |
1096 | const CompilerDeclContext &parent_decl_ctx, bool include_inlines, |
1097 | SymbolContextList &sc_list) { |
1098 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1099 | LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (name = %s)" , |
1100 | lookup_info.GetLookupName().GetCString()); |
1101 | |
1102 | ForEachSymbolFile(description: "Looking up functions" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1103 | uint32_t sc_idx = sc_list.GetSize(); |
1104 | oso_dwarf.FindFunctions(lookup_info, parent_decl_ctx, include_inlines, |
1105 | sc_list); |
1106 | if (!sc_list.IsEmpty()) { |
1107 | RemoveFunctionsWithModuleNotEqualTo(module_sp: m_objfile_sp->GetModule(), sc_list, |
1108 | start_idx: sc_idx); |
1109 | } |
1110 | return IterationAction::Continue; |
1111 | }); |
1112 | } |
1113 | |
1114 | void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex, |
1115 | bool include_inlines, |
1116 | SymbolContextList &sc_list) { |
1117 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1118 | LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')" , |
1119 | regex.GetText().str().c_str()); |
1120 | |
1121 | ForEachSymbolFile(description: "Looking up functions" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1122 | uint32_t sc_idx = sc_list.GetSize(); |
1123 | |
1124 | oso_dwarf.FindFunctions(regex, include_inlines, sc_list); |
1125 | if (!sc_list.IsEmpty()) { |
1126 | RemoveFunctionsWithModuleNotEqualTo(module_sp: m_objfile_sp->GetModule(), sc_list, |
1127 | start_idx: sc_idx); |
1128 | } |
1129 | return IterationAction::Continue; |
1130 | }); |
1131 | } |
1132 | |
1133 | void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope, |
1134 | lldb::TypeClass type_mask, |
1135 | TypeList &type_list) { |
1136 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1137 | LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)" , |
1138 | type_mask); |
1139 | |
1140 | SymbolFileDWARF *oso_dwarf = nullptr; |
1141 | if (sc_scope) { |
1142 | SymbolContext sc; |
1143 | sc_scope->CalculateSymbolContext(sc: &sc); |
1144 | |
1145 | CompileUnitInfo *cu_info = GetCompUnitInfo(sc); |
1146 | if (cu_info) { |
1147 | oso_dwarf = GetSymbolFileByCompUnitInfo(comp_unit_info: cu_info); |
1148 | if (oso_dwarf) |
1149 | oso_dwarf->GetTypes(sc_scope, type_mask, type_list); |
1150 | } |
1151 | } else { |
1152 | ForEachSymbolFile(description: "Looking up types" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1153 | oso_dwarf.GetTypes(sc_scope, type_mask, type_list); |
1154 | return IterationAction::Continue; |
1155 | }); |
1156 | } |
1157 | } |
1158 | |
1159 | std::vector<std::unique_ptr<lldb_private::CallEdge>> |
1160 | SymbolFileDWARFDebugMap::ParseCallEdgesInFunction( |
1161 | lldb_private::UserID func_id) { |
1162 | uint32_t oso_idx = GetOSOIndexFromUserID(uid: func_id.GetID()); |
1163 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); |
1164 | if (oso_dwarf) |
1165 | return oso_dwarf->ParseCallEdgesInFunction(func_id); |
1166 | return {}; |
1167 | } |
1168 | |
1169 | DWARFDIE SymbolFileDWARFDebugMap::FindDefinitionDIE(const DWARFDIE &die) { |
1170 | DWARFDIE result; |
1171 | ForEachSymbolFile( |
1172 | description: "Looking up type definition" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1173 | result = oso_dwarf.FindDefinitionDIE(die); |
1174 | return result ? IterationAction::Stop : IterationAction::Continue; |
1175 | }); |
1176 | return result; |
1177 | } |
1178 | |
1179 | TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE( |
1180 | const DWARFDIE &die, ConstString type_name, bool must_be_implementation) { |
1181 | // If we have a debug map, we will have an Objective-C symbol whose name is |
1182 | // the type name and whose type is eSymbolTypeObjCClass. If we can find that |
1183 | // symbol and find its containing parent, we can locate the .o file that will |
1184 | // contain the implementation definition since it will be scoped inside the |
1185 | // N_SO and we can then locate the SymbolFileDWARF that corresponds to that |
1186 | // N_SO. |
1187 | SymbolFileDWARF *oso_dwarf = nullptr; |
1188 | TypeSP type_sp; |
1189 | ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile(); |
1190 | if (module_objfile) { |
1191 | Symtab *symtab = module_objfile->GetSymtab(); |
1192 | if (symtab) { |
1193 | Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType( |
1194 | name: type_name, symbol_type: eSymbolTypeObjCClass, symbol_debug_type: Symtab::eDebugAny, |
1195 | symbol_visibility: Symtab::eVisibilityAny); |
1196 | if (objc_class_symbol) { |
1197 | // Get the N_SO symbol that contains the objective C class symbol as |
1198 | // this should be the .o file that contains the real definition... |
1199 | const Symbol *source_file_symbol = symtab->GetParent(symbol: objc_class_symbol); |
1200 | |
1201 | if (source_file_symbol && |
1202 | source_file_symbol->GetType() == eSymbolTypeSourceFile) { |
1203 | const uint32_t source_file_symbol_idx = |
1204 | symtab->GetIndexForSymbol(symbol: source_file_symbol); |
1205 | if (source_file_symbol_idx != UINT32_MAX) { |
1206 | CompileUnitInfo *compile_unit_info = |
1207 | GetCompileUnitInfoForSymbolWithIndex(symbol_idx: source_file_symbol_idx, |
1208 | oso_idx_ptr: nullptr); |
1209 | if (compile_unit_info) { |
1210 | oso_dwarf = GetSymbolFileByCompUnitInfo(comp_unit_info: compile_unit_info); |
1211 | if (oso_dwarf) { |
1212 | TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE( |
1213 | die, type_name, must_be_implementation)); |
1214 | if (type_sp) { |
1215 | return type_sp; |
1216 | } |
1217 | } |
1218 | } |
1219 | } |
1220 | } |
1221 | } |
1222 | } |
1223 | } |
1224 | |
1225 | // Only search all .o files for the definition if we don't need the |
1226 | // implementation because otherwise, with a valid debug map we should have |
1227 | // the ObjC class symbol and the code above should have found it. |
1228 | if (!must_be_implementation) { |
1229 | TypeSP type_sp; |
1230 | |
1231 | ForEachSymbolFile( |
1232 | description: "Looking up Objective-C definition" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1233 | type_sp = oso_dwarf.FindCompleteObjCDefinitionTypeForDIE( |
1234 | die, type_name, must_be_implementation); |
1235 | return type_sp ? IterationAction::Stop : IterationAction::Continue; |
1236 | }); |
1237 | |
1238 | return type_sp; |
1239 | } |
1240 | return TypeSP(); |
1241 | } |
1242 | |
1243 | void SymbolFileDWARFDebugMap::FindTypes(const TypeQuery &query, |
1244 | TypeResults &results) { |
1245 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1246 | ForEachSymbolFile(description: "Looking up type" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1247 | oso_dwarf.FindTypes(match: query, results); |
1248 | return results.Done(query) ? IterationAction::Stop |
1249 | : IterationAction::Continue; |
1250 | }); |
1251 | } |
1252 | |
1253 | CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace( |
1254 | lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx, |
1255 | bool only_root_namespaces) { |
1256 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1257 | CompilerDeclContext matching_namespace; |
1258 | |
1259 | ForEachSymbolFile(description: "Looking up namespace" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1260 | matching_namespace = |
1261 | oso_dwarf.FindNamespace(name, parent_decl_ctx, only_root_namespaces); |
1262 | |
1263 | return matching_namespace ? IterationAction::Stop |
1264 | : IterationAction::Continue; |
1265 | }); |
1266 | |
1267 | return matching_namespace; |
1268 | } |
1269 | |
1270 | void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s, llvm::StringRef filter) { |
1271 | ForEachSymbolFile(description: "Dumping clang AST" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1272 | oso_dwarf.DumpClangAST(s, filter); |
1273 | // The underlying assumption is that DumpClangAST(...) will obtain the |
1274 | // AST from the underlying TypeSystem and therefore we only need to do |
1275 | // this once and can stop after the first iteration hence we return true. |
1276 | return IterationAction::Stop; |
1277 | }); |
1278 | } |
1279 | |
1280 | bool SymbolFileDWARFDebugMap::GetSeparateDebugInfo( |
1281 | lldb_private::StructuredData::Dictionary &d, bool errors_only) { |
1282 | StructuredData::Array separate_debug_info_files; |
1283 | const uint32_t cu_count = GetNumCompileUnits(); |
1284 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { |
1285 | const auto &info = m_compile_unit_infos[cu_idx]; |
1286 | StructuredData::DictionarySP oso_data = |
1287 | std::make_shared<StructuredData::Dictionary>(); |
1288 | oso_data->AddStringItem(key: "so_file" , value: info.so_file.GetPath()); |
1289 | oso_data->AddStringItem(key: "oso_path" , value: info.oso_path); |
1290 | oso_data->AddIntegerItem(key: "oso_mod_time" , |
1291 | value: (uint32_t)llvm::sys::toTimeT(TP: info.oso_mod_time)); |
1292 | |
1293 | bool loaded_successfully = false; |
1294 | if (GetModuleByOSOIndex(oso_idx: cu_idx)) { |
1295 | // If we have a valid pointer to the module, we successfully |
1296 | // loaded the oso if there are no load errors. |
1297 | if (!info.oso_load_error.Fail()) { |
1298 | loaded_successfully = true; |
1299 | } |
1300 | } |
1301 | if (!loaded_successfully) { |
1302 | oso_data->AddStringItem(key: "error" , value: info.oso_load_error.AsCString()); |
1303 | } |
1304 | oso_data->AddBooleanItem(key: "loaded" , value: loaded_successfully); |
1305 | if (!errors_only || oso_data->HasKey(key: "error" )) |
1306 | separate_debug_info_files.AddItem(item: oso_data); |
1307 | } |
1308 | |
1309 | d.AddStringItem(key: "type" , value: "oso" ); |
1310 | d.AddStringItem(key: "symfile" , value: GetMainObjectFile()->GetFileSpec().GetPath()); |
1311 | d.AddItem(key: "separate-debug-info-files" , |
1312 | value_sp: std::make_shared<StructuredData::Array>( |
1313 | args: std::move(separate_debug_info_files))); |
1314 | return true; |
1315 | } |
1316 | |
1317 | lldb::CompUnitSP |
1318 | SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf, |
1319 | DWARFCompileUnit &dwarf_cu) { |
1320 | if (oso_dwarf) { |
1321 | const uint32_t cu_count = GetNumCompileUnits(); |
1322 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { |
1323 | SymbolFileDWARF *oso_symfile = |
1324 | GetSymbolFileByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[cu_idx]); |
1325 | if (oso_symfile == oso_dwarf) { |
1326 | if (m_compile_unit_infos[cu_idx].compile_units_sps.empty()) |
1327 | ParseCompileUnitAtIndex(cu_idx); |
1328 | |
1329 | auto &id_to_index_map = m_compile_unit_infos[cu_idx].id_to_index_map; |
1330 | auto it = id_to_index_map.find(Val: dwarf_cu.GetID()); |
1331 | if (it != id_to_index_map.end()) |
1332 | return m_compile_unit_infos[cu_idx] |
1333 | .compile_units_sps[it->getSecond()]; |
1334 | } |
1335 | } |
1336 | } |
1337 | llvm_unreachable("this shouldn't happen" ); |
1338 | } |
1339 | |
1340 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
1341 | SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) { |
1342 | if (oso_dwarf) { |
1343 | const uint32_t cu_count = GetNumCompileUnits(); |
1344 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { |
1345 | SymbolFileDWARF *oso_symfile = |
1346 | GetSymbolFileByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[cu_idx]); |
1347 | if (oso_symfile == oso_dwarf) { |
1348 | return &m_compile_unit_infos[cu_idx]; |
1349 | } |
1350 | } |
1351 | } |
1352 | return nullptr; |
1353 | } |
1354 | |
1355 | void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf, |
1356 | const CompUnitSP &cu_sp) { |
1357 | if (oso_dwarf) { |
1358 | const uint32_t cu_count = GetNumCompileUnits(); |
1359 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { |
1360 | SymbolFileDWARF *oso_symfile = |
1361 | GetSymbolFileByCompUnitInfo(comp_unit_info: &m_compile_unit_infos[cu_idx]); |
1362 | if (oso_symfile == oso_dwarf) { |
1363 | if (!m_compile_unit_infos[cu_idx].compile_units_sps.empty()) { |
1364 | assert(m_compile_unit_infos[cu_idx].compile_units_sps[0].get() == |
1365 | cu_sp.get()); |
1366 | } else { |
1367 | assert(cu_sp->GetID() == 0 && |
1368 | "Setting first compile unit but with id different than 0!" ); |
1369 | auto &compile_units_sps = |
1370 | m_compile_unit_infos[cu_idx].compile_units_sps; |
1371 | compile_units_sps.push_back(Elt: cu_sp); |
1372 | m_compile_unit_infos[cu_idx].id_to_index_map.insert( |
1373 | KV: {cu_sp->GetID(), compile_units_sps.size() - 1}); |
1374 | |
1375 | SetCompileUnitAtIndex(idx: cu_idx, cu_sp); |
1376 | } |
1377 | } |
1378 | } |
1379 | } |
1380 | } |
1381 | |
1382 | CompilerDeclContext |
1383 | SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) { |
1384 | const uint64_t oso_idx = GetOSOIndexFromUserID(uid: type_uid); |
1385 | if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) |
1386 | return oso_dwarf->GetDeclContextForUID(uid: type_uid); |
1387 | return {}; |
1388 | } |
1389 | |
1390 | CompilerDeclContext |
1391 | SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) { |
1392 | const uint64_t oso_idx = GetOSOIndexFromUserID(uid: type_uid); |
1393 | if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) |
1394 | return oso_dwarf->GetDeclContextContainingUID(uid: type_uid); |
1395 | return {}; |
1396 | } |
1397 | |
1398 | std::vector<CompilerContext> |
1399 | SymbolFileDWARFDebugMap::GetCompilerContextForUID(lldb::user_id_t type_uid) { |
1400 | const uint64_t oso_idx = GetOSOIndexFromUserID(uid: type_uid); |
1401 | if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) |
1402 | return oso_dwarf->GetCompilerContextForUID(uid: type_uid); |
1403 | return {}; |
1404 | } |
1405 | |
1406 | void SymbolFileDWARFDebugMap::ParseDeclsForContext( |
1407 | lldb_private::CompilerDeclContext decl_ctx) { |
1408 | ForEachSymbolFile(description: "Parsing declarations" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1409 | oso_dwarf.ParseDeclsForContext(decl_ctx); |
1410 | return IterationAction::Continue; |
1411 | }); |
1412 | } |
1413 | |
1414 | bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info, |
1415 | lldb::addr_t exe_file_addr, |
1416 | lldb::addr_t exe_byte_size, |
1417 | lldb::addr_t oso_file_addr, |
1418 | lldb::addr_t oso_byte_size) { |
1419 | const uint32_t debug_map_idx = |
1420 | m_debug_map.FindEntryIndexThatContains(addr: exe_file_addr); |
1421 | if (debug_map_idx != UINT32_MAX) { |
1422 | DebugMap::Entry *debug_map_entry = |
1423 | m_debug_map.FindEntryThatContains(addr: exe_file_addr); |
1424 | debug_map_entry->data.SetOSOFileAddress(oso_file_addr); |
1425 | addr_t range_size = std::min<addr_t>(a: exe_byte_size, b: oso_byte_size); |
1426 | if (range_size == 0) { |
1427 | range_size = std::max<addr_t>(a: exe_byte_size, b: oso_byte_size); |
1428 | if (range_size == 0) |
1429 | range_size = 1; |
1430 | } |
1431 | cu_info->file_range_map.Append( |
1432 | entry: FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr)); |
1433 | return true; |
1434 | } |
1435 | return false; |
1436 | } |
1437 | |
1438 | void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) { |
1439 | cu_info->file_range_map.Sort(); |
1440 | #if defined(DEBUG_OSO_DMAP) |
1441 | const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this); |
1442 | const size_t n = oso_file_range_map.GetSize(); |
1443 | printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n" , |
1444 | cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str()); |
1445 | for (size_t i = 0; i < n; ++i) { |
1446 | const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i); |
1447 | printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 |
1448 | ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n" , |
1449 | entry.GetRangeBase(), entry.GetRangeEnd(), entry.data, |
1450 | entry.data + entry.GetByteSize()); |
1451 | } |
1452 | #endif |
1453 | } |
1454 | |
1455 | lldb::addr_t |
1456 | SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile, |
1457 | lldb::addr_t oso_file_addr) { |
1458 | CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf: oso_symfile); |
1459 | if (cu_info) { |
1460 | const FileRangeMap::Entry *oso_range_entry = |
1461 | cu_info->GetFileRangeMap(exe_symfile: this).FindEntryThatContains(addr: oso_file_addr); |
1462 | if (oso_range_entry) { |
1463 | const DebugMap::Entry *debug_map_entry = |
1464 | m_debug_map.FindEntryThatContains(addr: oso_range_entry->data); |
1465 | if (debug_map_entry) { |
1466 | const lldb::addr_t offset = |
1467 | oso_file_addr - oso_range_entry->GetRangeBase(); |
1468 | const lldb::addr_t exe_file_addr = |
1469 | debug_map_entry->GetRangeBase() + offset; |
1470 | return exe_file_addr; |
1471 | } |
1472 | } |
1473 | } |
1474 | return LLDB_INVALID_ADDRESS; |
1475 | } |
1476 | |
1477 | bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) { |
1478 | // Make sure this address hasn't been fixed already |
1479 | Module *exe_module = GetObjectFile()->GetModule().get(); |
1480 | Module *addr_module = addr.GetModule().get(); |
1481 | if (addr_module == exe_module) |
1482 | return true; // Address is already in terms of the main executable module |
1483 | |
1484 | CompileUnitInfo *cu_info = GetCompileUnitInfo( |
1485 | oso_dwarf: GetSymbolFileAsSymbolFileDWARF(sym_file: addr_module->GetSymbolFile())); |
1486 | if (cu_info) { |
1487 | const lldb::addr_t oso_file_addr = addr.GetFileAddress(); |
1488 | const FileRangeMap::Entry *oso_range_entry = |
1489 | cu_info->GetFileRangeMap(exe_symfile: this).FindEntryThatContains(addr: oso_file_addr); |
1490 | if (oso_range_entry) { |
1491 | const DebugMap::Entry *debug_map_entry = |
1492 | m_debug_map.FindEntryThatContains(addr: oso_range_entry->data); |
1493 | if (debug_map_entry) { |
1494 | const lldb::addr_t offset = |
1495 | oso_file_addr - oso_range_entry->GetRangeBase(); |
1496 | const lldb::addr_t exe_file_addr = |
1497 | debug_map_entry->GetRangeBase() + offset; |
1498 | return exe_module->ResolveFileAddress(vm_addr: exe_file_addr, so_addr&: addr); |
1499 | } |
1500 | } |
1501 | } |
1502 | return true; |
1503 | } |
1504 | |
1505 | LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf, |
1506 | LineTable *line_table) { |
1507 | CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf); |
1508 | if (cu_info) |
1509 | return line_table->LinkLineTable(file_range_map: cu_info->GetFileRangeMap(exe_symfile: this)); |
1510 | return nullptr; |
1511 | } |
1512 | |
1513 | size_t |
1514 | SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data, |
1515 | DWARFDebugAranges *debug_aranges) { |
1516 | size_t num_line_entries_added = 0; |
1517 | if (debug_aranges && dwarf2Data) { |
1518 | CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(oso_dwarf: dwarf2Data); |
1519 | if (compile_unit_info) { |
1520 | const FileRangeMap &file_range_map = |
1521 | compile_unit_info->GetFileRangeMap(exe_symfile: this); |
1522 | for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) { |
1523 | const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(i: idx); |
1524 | if (entry) { |
1525 | debug_aranges->AppendRange(cu_offset: *dwarf2Data->GetFileIndex(), |
1526 | low_pc: entry->GetRangeBase(), |
1527 | high_pc: entry->GetRangeEnd()); |
1528 | num_line_entries_added++; |
1529 | } |
1530 | } |
1531 | } |
1532 | } |
1533 | return num_line_entries_added; |
1534 | } |
1535 | |
1536 | ModuleList SymbolFileDWARFDebugMap::GetDebugInfoModules() { |
1537 | ModuleList oso_modules; |
1538 | ForEachSymbolFile(description: "Parsing modules" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1539 | ObjectFile *oso_objfile = oso_dwarf.GetObjectFile(); |
1540 | if (oso_objfile) { |
1541 | ModuleSP module_sp = oso_objfile->GetModule(); |
1542 | if (module_sp) |
1543 | oso_modules.Append(module_sp); |
1544 | } |
1545 | return IterationAction::Continue; |
1546 | }); |
1547 | return oso_modules; |
1548 | } |
1549 | |
1550 | Status SymbolFileDWARFDebugMap::CalculateFrameVariableError(StackFrame &frame) { |
1551 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1552 | |
1553 | // We need to make sure that our PC value from the frame matches the module |
1554 | // for this object file since we will lookup the PC file address in the debug |
1555 | // map below. |
1556 | Address pc_addr = frame.GetFrameCodeAddress(); |
1557 | if (pc_addr.GetModule() == m_objfile_sp->GetModule()) { |
1558 | Symtab *symtab = m_objfile_sp->GetSymtab(); |
1559 | if (symtab) { |
1560 | const DebugMap::Entry *debug_map_entry = |
1561 | m_debug_map.FindEntryThatContains(addr: pc_addr.GetFileAddress()); |
1562 | if (debug_map_entry) { |
1563 | Symbol *symbol = |
1564 | symtab->SymbolAtIndex(idx: debug_map_entry->data.GetExeSymbolIndex()); |
1565 | if (symbol) { |
1566 | uint32_t oso_idx = 0; |
1567 | CompileUnitInfo *comp_unit_info = |
1568 | GetCompileUnitInfoForSymbolWithID(symbol_id: symbol->GetID(), oso_idx_ptr: &oso_idx); |
1569 | if (comp_unit_info) { |
1570 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); |
1571 | if (oso_module) { |
1572 | // Check the .o file's DWARF in case it has an error to display. |
1573 | SymbolFile *oso_sym_file = oso_module->GetSymbolFile(); |
1574 | if (oso_sym_file) |
1575 | return oso_sym_file->GetFrameVariableError(frame); |
1576 | } |
1577 | // If we don't have a valid OSO module here, then something went |
1578 | // wrong as we have a symbol for the address in the debug map, but |
1579 | // we weren't able to open the .o file. Display an appropriate |
1580 | // error |
1581 | if (comp_unit_info->oso_load_error.Fail()) |
1582 | return comp_unit_info->oso_load_error.Clone(); |
1583 | else |
1584 | return Status::FromErrorStringWithFormat( |
1585 | format: "unable to load debug map object file \"%s\" " |
1586 | "exist, debug info will not be loaded" , |
1587 | comp_unit_info->oso_path.GetCString()); |
1588 | } |
1589 | } |
1590 | } |
1591 | } |
1592 | } |
1593 | return Status(); |
1594 | } |
1595 | |
1596 | void SymbolFileDWARFDebugMap::GetCompileOptions( |
1597 | std::unordered_map<lldb::CompUnitSP, lldb_private::Args> &args) { |
1598 | |
1599 | ForEachSymbolFile(description: "Parsing compile options" , closure: [&](SymbolFileDWARF &oso_dwarf) { |
1600 | oso_dwarf.GetCompileOptions(args); |
1601 | return IterationAction::Continue; |
1602 | }); |
1603 | } |
1604 | |