xref: /freebsd/contrib/llvm-project/lldb/source/Core/Module.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- Module.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 "lldb/Core/Module.h"
10 
11 #include "lldb/Core/AddressRange.h"
12 #include "lldb/Core/AddressResolverFileLine.h"
13 #include "lldb/Core/DataFileCache.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Mangled.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/SearchFilter.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Host/FileSystem.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/ScriptInterpreter.h"
24 #include "lldb/Symbol/CompileUnit.h"
25 #include "lldb/Symbol/Function.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Symbol/Symbol.h"
28 #include "lldb/Symbol/SymbolContext.h"
29 #include "lldb/Symbol/SymbolFile.h"
30 #include "lldb/Symbol/SymbolLocator.h"
31 #include "lldb/Symbol/SymbolVendor.h"
32 #include "lldb/Symbol/Symtab.h"
33 #include "lldb/Symbol/Type.h"
34 #include "lldb/Symbol/TypeList.h"
35 #include "lldb/Symbol/TypeMap.h"
36 #include "lldb/Symbol/TypeSystem.h"
37 #include "lldb/Target/Language.h"
38 #include "lldb/Target/Process.h"
39 #include "lldb/Target/Target.h"
40 #include "lldb/Utility/DataBufferHeap.h"
41 #include "lldb/Utility/FileSpecList.h"
42 #include "lldb/Utility/LLDBAssert.h"
43 #include "lldb/Utility/LLDBLog.h"
44 #include "lldb/Utility/Log.h"
45 #include "lldb/Utility/RegularExpression.h"
46 #include "lldb/Utility/Status.h"
47 #include "lldb/Utility/Stream.h"
48 #include "lldb/Utility/StreamString.h"
49 #include "lldb/Utility/Timer.h"
50 
51 #if defined(_WIN32)
52 #include "lldb/Host/windows/PosixApi.h"
53 #endif
54 
55 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
56 #include "Plugins/Language/ObjC/ObjCLanguage.h"
57 
58 #include "llvm/ADT/STLExtras.h"
59 #include "llvm/Support/Compiler.h"
60 #include "llvm/Support/DJB.h"
61 #include "llvm/Support/FileSystem.h"
62 #include "llvm/Support/FormatVariadic.h"
63 #include "llvm/Support/JSON.h"
64 #include "llvm/Support/Signals.h"
65 #include "llvm/Support/raw_ostream.h"
66 
67 #include <cassert>
68 #include <cinttypes>
69 #include <cstdarg>
70 #include <cstdint>
71 #include <cstring>
72 #include <map>
73 #include <optional>
74 #include <type_traits>
75 #include <utility>
76 
77 namespace lldb_private {
78 class CompilerDeclContext;
79 }
80 namespace lldb_private {
81 class VariableList;
82 }
83 
84 using namespace lldb;
85 using namespace lldb_private;
86 
87 // Shared pointers to modules track module lifetimes in targets and in the
88 // global module, but this collection will track all module objects that are
89 // still alive
90 typedef std::vector<Module *> ModuleCollection;
91 
GetModuleCollection()92 static ModuleCollection &GetModuleCollection() {
93   // This module collection needs to live past any module, so we could either
94   // make it a shared pointer in each module or just leak is.  Since it is only
95   // an empty vector by the time all the modules have gone away, we just leak
96   // it for now.  If we decide this is a big problem we can introduce a
97   // Finalize method that will tear everything down in a predictable order.
98 
99   static ModuleCollection *g_module_collection = nullptr;
100   if (g_module_collection == nullptr)
101     g_module_collection = new ModuleCollection();
102 
103   return *g_module_collection;
104 }
105 
GetAllocationModuleCollectionMutex()106 std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() {
107   // NOTE: The mutex below must be leaked since the global module list in
108   // the ModuleList class will get torn at some point, and we can't know if it
109   // will tear itself down before the "g_module_collection_mutex" below will.
110   // So we leak a Mutex object below to safeguard against that
111 
112   static std::recursive_mutex *g_module_collection_mutex = nullptr;
113   if (g_module_collection_mutex == nullptr)
114     g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
115   return *g_module_collection_mutex;
116 }
117 
GetNumberAllocatedModules()118 size_t Module::GetNumberAllocatedModules() {
119   std::lock_guard<std::recursive_mutex> guard(
120       GetAllocationModuleCollectionMutex());
121   return GetModuleCollection().size();
122 }
123 
GetAllocatedModuleAtIndex(size_t idx)124 Module *Module::GetAllocatedModuleAtIndex(size_t idx) {
125   std::lock_guard<std::recursive_mutex> guard(
126       GetAllocationModuleCollectionMutex());
127   ModuleCollection &modules = GetModuleCollection();
128   if (idx < modules.size())
129     return modules[idx];
130   return nullptr;
131 }
132 
Module(const ModuleSpec & module_spec)133 Module::Module(const ModuleSpec &module_spec)
134     : m_file_has_changed(false), m_first_file_changed_log(false) {
135   // Scope for locker below...
136   {
137     std::lock_guard<std::recursive_mutex> guard(
138         GetAllocationModuleCollectionMutex());
139     GetModuleCollection().push_back(this);
140   }
141 
142   Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
143   if (log != nullptr)
144     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
145               static_cast<void *>(this),
146               module_spec.GetArchitecture().GetArchitectureName(),
147               module_spec.GetFileSpec().GetPath().c_str(),
148               module_spec.GetObjectName().IsEmpty() ? "" : "(",
149               module_spec.GetObjectName().AsCString(""),
150               module_spec.GetObjectName().IsEmpty() ? "" : ")");
151 
152   auto data_sp = module_spec.GetData();
153   lldb::offset_t file_size = 0;
154   if (data_sp)
155     file_size = data_sp->GetByteSize();
156 
157   // First extract all module specifications from the file using the local file
158   // path. If there are no specifications, then don't fill anything in
159   ModuleSpecList modules_specs;
160   if (ObjectFile::GetModuleSpecifications(
161           module_spec.GetFileSpec(), 0, file_size, modules_specs, data_sp) == 0)
162     return;
163 
164   // Now make sure that one of the module specifications matches what we just
165   // extract. We might have a module specification that specifies a file
166   // "/usr/lib/dyld" with UUID XXX, but we might have a local version of
167   // "/usr/lib/dyld" that has
168   // UUID YYY and we don't want those to match. If they don't match, just don't
169   // fill any ivars in so we don't accidentally grab the wrong file later since
170   // they don't match...
171   ModuleSpec matching_module_spec;
172   if (!modules_specs.FindMatchingModuleSpec(module_spec,
173                                             matching_module_spec)) {
174     if (log) {
175       LLDB_LOGF(log, "Found local object file but the specs didn't match");
176     }
177     return;
178   }
179 
180   // Set m_data_sp if it was initially provided in the ModuleSpec. Note that
181   // we cannot use the data_sp variable here, because it will have been
182   // modified by GetModuleSpecifications().
183   if (auto module_spec_data_sp = module_spec.GetData()) {
184     m_data_sp = module_spec_data_sp;
185     m_mod_time = {};
186   } else {
187     if (module_spec.GetFileSpec())
188       m_mod_time =
189           FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec());
190     else if (matching_module_spec.GetFileSpec())
191       m_mod_time = FileSystem::Instance().GetModificationTime(
192           matching_module_spec.GetFileSpec());
193   }
194 
195   // Copy the architecture from the actual spec if we got one back, else use
196   // the one that was specified
197   if (matching_module_spec.GetArchitecture().IsValid())
198     m_arch = matching_module_spec.GetArchitecture();
199   else if (module_spec.GetArchitecture().IsValid())
200     m_arch = module_spec.GetArchitecture();
201 
202   // Copy the file spec over and use the specified one (if there was one) so we
203   // don't use a path that might have gotten resolved a path in
204   // 'matching_module_spec'
205   if (module_spec.GetFileSpec())
206     m_file = module_spec.GetFileSpec();
207   else if (matching_module_spec.GetFileSpec())
208     m_file = matching_module_spec.GetFileSpec();
209 
210   // Copy the platform file spec over
211   if (module_spec.GetPlatformFileSpec())
212     m_platform_file = module_spec.GetPlatformFileSpec();
213   else if (matching_module_spec.GetPlatformFileSpec())
214     m_platform_file = matching_module_spec.GetPlatformFileSpec();
215 
216   // Copy the symbol file spec over
217   if (module_spec.GetSymbolFileSpec())
218     m_symfile_spec = module_spec.GetSymbolFileSpec();
219   else if (matching_module_spec.GetSymbolFileSpec())
220     m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
221 
222   // Copy the object name over
223   if (matching_module_spec.GetObjectName())
224     m_object_name = matching_module_spec.GetObjectName();
225   else
226     m_object_name = module_spec.GetObjectName();
227 
228   // Always trust the object offset (file offset) and object modification time
229   // (for mod time in a BSD static archive) of from the matching module
230   // specification
231   m_object_offset = matching_module_spec.GetObjectOffset();
232   m_object_mod_time = matching_module_spec.GetObjectModificationTime();
233 }
234 
Module(const FileSpec & file_spec,const ArchSpec & arch,ConstString object_name,lldb::offset_t object_offset,const llvm::sys::TimePoint<> & object_mod_time)235 Module::Module(const FileSpec &file_spec, const ArchSpec &arch,
236                ConstString object_name, lldb::offset_t object_offset,
237                const llvm::sys::TimePoint<> &object_mod_time)
238     : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
239       m_arch(arch), m_file(file_spec), m_object_name(object_name),
240       m_object_offset(object_offset), m_object_mod_time(object_mod_time),
241       m_file_has_changed(false), m_first_file_changed_log(false) {
242   // Scope for locker below...
243   {
244     std::lock_guard<std::recursive_mutex> guard(
245         GetAllocationModuleCollectionMutex());
246     GetModuleCollection().push_back(this);
247   }
248 
249   Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
250   if (log != nullptr)
251     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
252               static_cast<void *>(this), m_arch.GetArchitectureName(),
253               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
254               m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
255 }
256 
Module()257 Module::Module() : m_file_has_changed(false), m_first_file_changed_log(false) {
258   std::lock_guard<std::recursive_mutex> guard(
259       GetAllocationModuleCollectionMutex());
260   GetModuleCollection().push_back(this);
261 }
262 
~Module()263 Module::~Module() {
264   // Lock our module down while we tear everything down to make sure we don't
265   // get any access to the module while it is being destroyed
266   std::lock_guard<std::recursive_mutex> guard(m_mutex);
267   // Scope for locker below...
268   {
269     std::lock_guard<std::recursive_mutex> guard(
270         GetAllocationModuleCollectionMutex());
271     ModuleCollection &modules = GetModuleCollection();
272     ModuleCollection::iterator end = modules.end();
273     ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
274     assert(pos != end);
275     modules.erase(pos);
276   }
277   Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
278   if (log != nullptr)
279     LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')",
280               static_cast<void *>(this), m_arch.GetArchitectureName(),
281               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
282               m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
283   // Release any auto pointers before we start tearing down our member
284   // variables since the object file and symbol files might need to make
285   // function calls back into this module object. The ordering is important
286   // here because symbol files can require the module object file. So we tear
287   // down the symbol file first, then the object file.
288   m_sections_up.reset();
289   m_symfile_up.reset();
290   m_objfile_sp.reset();
291 }
292 
GetMemoryObjectFile(const lldb::ProcessSP & process_sp,lldb::addr_t header_addr,Status & error,size_t size_to_read)293 ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
294                                         lldb::addr_t header_addr, Status &error,
295                                         size_t size_to_read) {
296   if (m_objfile_sp) {
297     error.SetErrorString("object file already exists");
298   } else {
299     std::lock_guard<std::recursive_mutex> guard(m_mutex);
300     if (process_sp) {
301       m_did_load_objfile = true;
302       std::shared_ptr<DataBufferHeap> data_sp =
303           std::make_shared<DataBufferHeap>(size_to_read, 0);
304       Status readmem_error;
305       const size_t bytes_read =
306           process_sp->ReadMemory(header_addr, data_sp->GetBytes(),
307                                  data_sp->GetByteSize(), readmem_error);
308       if (bytes_read < size_to_read)
309         data_sp->SetByteSize(bytes_read);
310       if (data_sp->GetByteSize() > 0) {
311         m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
312                                               header_addr, data_sp);
313         if (m_objfile_sp) {
314           StreamString s;
315           s.Printf("0x%16.16" PRIx64, header_addr);
316           m_object_name.SetString(s.GetString());
317 
318           // Once we get the object file, update our module with the object
319           // file's architecture since it might differ in vendor/os if some
320           // parts were unknown.
321           m_arch = m_objfile_sp->GetArchitecture();
322 
323           // Augment the arch with the target's information in case
324           // we are unable to extract the os/environment from memory.
325           m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
326         } else {
327           error.SetErrorString("unable to find suitable object file plug-in");
328         }
329       } else {
330         error.SetErrorStringWithFormat("unable to read header from memory: %s",
331                                        readmem_error.AsCString());
332       }
333     } else {
334       error.SetErrorString("invalid process");
335     }
336   }
337   return m_objfile_sp.get();
338 }
339 
GetUUID()340 const lldb_private::UUID &Module::GetUUID() {
341   if (!m_did_set_uuid.load()) {
342     std::lock_guard<std::recursive_mutex> guard(m_mutex);
343     if (!m_did_set_uuid.load()) {
344       ObjectFile *obj_file = GetObjectFile();
345 
346       if (obj_file != nullptr) {
347         m_uuid = obj_file->GetUUID();
348         m_did_set_uuid = true;
349       }
350     }
351   }
352   return m_uuid;
353 }
354 
SetUUID(const lldb_private::UUID & uuid)355 void Module::SetUUID(const lldb_private::UUID &uuid) {
356   std::lock_guard<std::recursive_mutex> guard(m_mutex);
357   if (!m_did_set_uuid) {
358     m_uuid = uuid;
359     m_did_set_uuid = true;
360   } else {
361     lldbassert(0 && "Attempting to overwrite the existing module UUID");
362   }
363 }
364 
365 llvm::Expected<TypeSystemSP>
GetTypeSystemForLanguage(LanguageType language)366 Module::GetTypeSystemForLanguage(LanguageType language) {
367   return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
368 }
369 
ForEachTypeSystem(llvm::function_ref<bool (lldb::TypeSystemSP)> callback)370 void Module::ForEachTypeSystem(
371     llvm::function_ref<bool(lldb::TypeSystemSP)> callback) {
372   m_type_system_map.ForEach(callback);
373 }
374 
ParseAllDebugSymbols()375 void Module::ParseAllDebugSymbols() {
376   std::lock_guard<std::recursive_mutex> guard(m_mutex);
377   size_t num_comp_units = GetNumCompileUnits();
378   if (num_comp_units == 0)
379     return;
380 
381   SymbolFile *symbols = GetSymbolFile();
382 
383   for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) {
384     SymbolContext sc;
385     sc.module_sp = shared_from_this();
386     sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
387     if (!sc.comp_unit)
388       continue;
389 
390     symbols->ParseVariablesForContext(sc);
391 
392     symbols->ParseFunctions(*sc.comp_unit);
393 
394     sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
395       symbols->ParseBlocksRecursive(*f);
396 
397       // Parse the variables for this function and all its blocks
398       sc.function = f.get();
399       symbols->ParseVariablesForContext(sc);
400       return false;
401     });
402 
403     // Parse all types for this compile unit
404     symbols->ParseTypes(*sc.comp_unit);
405   }
406 }
407 
CalculateSymbolContext(SymbolContext * sc)408 void Module::CalculateSymbolContext(SymbolContext *sc) {
409   sc->module_sp = shared_from_this();
410 }
411 
CalculateSymbolContextModule()412 ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
413 
DumpSymbolContext(Stream * s)414 void Module::DumpSymbolContext(Stream *s) {
415   s->Printf(", Module{%p}", static_cast<void *>(this));
416 }
417 
GetNumCompileUnits()418 size_t Module::GetNumCompileUnits() {
419   std::lock_guard<std::recursive_mutex> guard(m_mutex);
420   if (SymbolFile *symbols = GetSymbolFile())
421     return symbols->GetNumCompileUnits();
422   return 0;
423 }
424 
GetCompileUnitAtIndex(size_t index)425 CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
426   std::lock_guard<std::recursive_mutex> guard(m_mutex);
427   size_t num_comp_units = GetNumCompileUnits();
428   CompUnitSP cu_sp;
429 
430   if (index < num_comp_units) {
431     if (SymbolFile *symbols = GetSymbolFile())
432       cu_sp = symbols->GetCompileUnitAtIndex(index);
433   }
434   return cu_sp;
435 }
436 
ResolveFileAddress(lldb::addr_t vm_addr,Address & so_addr)437 bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
438   std::lock_guard<std::recursive_mutex> guard(m_mutex);
439   SectionList *section_list = GetSectionList();
440   if (section_list)
441     return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
442   return false;
443 }
444 
ResolveSymbolContextForAddress(const Address & so_addr,lldb::SymbolContextItem resolve_scope,SymbolContext & sc,bool resolve_tail_call_address)445 uint32_t Module::ResolveSymbolContextForAddress(
446     const Address &so_addr, lldb::SymbolContextItem resolve_scope,
447     SymbolContext &sc, bool resolve_tail_call_address) {
448   std::lock_guard<std::recursive_mutex> guard(m_mutex);
449   uint32_t resolved_flags = 0;
450 
451   // Clear the result symbol context in case we don't find anything, but don't
452   // clear the target
453   sc.Clear(false);
454 
455   // Get the section from the section/offset address.
456   SectionSP section_sp(so_addr.GetSection());
457 
458   // Make sure the section matches this module before we try and match anything
459   if (section_sp && section_sp->GetModule().get() == this) {
460     // If the section offset based address resolved itself, then this is the
461     // right module.
462     sc.module_sp = shared_from_this();
463     resolved_flags |= eSymbolContextModule;
464 
465     SymbolFile *symfile = GetSymbolFile();
466     if (!symfile)
467       return resolved_flags;
468 
469     // Resolve the compile unit, function, block, line table or line entry if
470     // requested.
471     if (resolve_scope & eSymbolContextCompUnit ||
472         resolve_scope & eSymbolContextFunction ||
473         resolve_scope & eSymbolContextBlock ||
474         resolve_scope & eSymbolContextLineEntry ||
475         resolve_scope & eSymbolContextVariable) {
476       symfile->SetLoadDebugInfoEnabled();
477       resolved_flags |=
478           symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
479     }
480 
481     // Resolve the symbol if requested, but don't re-look it up if we've
482     // already found it.
483     if (resolve_scope & eSymbolContextSymbol &&
484         !(resolved_flags & eSymbolContextSymbol)) {
485       Symtab *symtab = symfile->GetSymtab();
486       if (symtab && so_addr.IsSectionOffset()) {
487         Symbol *matching_symbol = nullptr;
488 
489         symtab->ForEachSymbolContainingFileAddress(
490             so_addr.GetFileAddress(),
491             [&matching_symbol](Symbol *symbol) -> bool {
492               if (symbol->GetType() != eSymbolTypeInvalid) {
493                 matching_symbol = symbol;
494                 return false; // Stop iterating
495               }
496               return true; // Keep iterating
497             });
498         sc.symbol = matching_symbol;
499         if (!sc.symbol && resolve_scope & eSymbolContextFunction &&
500             !(resolved_flags & eSymbolContextFunction)) {
501           bool verify_unique = false; // No need to check again since
502                                       // ResolveSymbolContext failed to find a
503                                       // symbol at this address.
504           if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
505             sc.symbol =
506                 obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
507         }
508 
509         if (sc.symbol) {
510           if (sc.symbol->IsSynthetic()) {
511             // We have a synthetic symbol so lets check if the object file from
512             // the symbol file in the symbol vendor is different than the
513             // object file for the module, and if so search its symbol table to
514             // see if we can come up with a better symbol. For example dSYM
515             // files on MacOSX have an unstripped symbol table inside of them.
516             ObjectFile *symtab_objfile = symtab->GetObjectFile();
517             if (symtab_objfile && symtab_objfile->IsStripped()) {
518               ObjectFile *symfile_objfile = symfile->GetObjectFile();
519               if (symfile_objfile != symtab_objfile) {
520                 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
521                 if (symfile_symtab) {
522                   Symbol *symbol =
523                       symfile_symtab->FindSymbolContainingFileAddress(
524                           so_addr.GetFileAddress());
525                   if (symbol && !symbol->IsSynthetic()) {
526                     sc.symbol = symbol;
527                   }
528                 }
529               }
530             }
531           }
532           resolved_flags |= eSymbolContextSymbol;
533         }
534       }
535     }
536 
537     // For function symbols, so_addr may be off by one.  This is a convention
538     // consistent with FDE row indices in eh_frame sections, but requires extra
539     // logic here to permit symbol lookup for disassembly and unwind.
540     if (resolve_scope & eSymbolContextSymbol &&
541         !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address &&
542         so_addr.IsSectionOffset()) {
543       Address previous_addr = so_addr;
544       previous_addr.Slide(-1);
545 
546       bool do_resolve_tail_call_address = false; // prevent recursion
547       const uint32_t flags = ResolveSymbolContextForAddress(
548           previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
549       if (flags & eSymbolContextSymbol) {
550         AddressRange addr_range;
551         if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
552                                false, addr_range)) {
553           if (addr_range.GetBaseAddress().GetSection() ==
554               so_addr.GetSection()) {
555             // If the requested address is one past the address range of a
556             // function (i.e. a tail call), or the decremented address is the
557             // start of a function (i.e. some forms of trampoline), indicate
558             // that the symbol has been resolved.
559             if (so_addr.GetOffset() ==
560                     addr_range.GetBaseAddress().GetOffset() ||
561                 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() +
562                                            addr_range.GetByteSize()) {
563               resolved_flags |= flags;
564             }
565           } else {
566             sc.symbol =
567                 nullptr; // Don't trust the symbol if the sections didn't match.
568           }
569         }
570       }
571     }
572   }
573   return resolved_flags;
574 }
575 
ResolveSymbolContextForFilePath(const char * file_path,uint32_t line,bool check_inlines,lldb::SymbolContextItem resolve_scope,SymbolContextList & sc_list)576 uint32_t Module::ResolveSymbolContextForFilePath(
577     const char *file_path, uint32_t line, bool check_inlines,
578     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
579   FileSpec file_spec(file_path);
580   return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
581                                           resolve_scope, sc_list);
582 }
583 
ResolveSymbolContextsForFileSpec(const FileSpec & file_spec,uint32_t line,bool check_inlines,lldb::SymbolContextItem resolve_scope,SymbolContextList & sc_list)584 uint32_t Module::ResolveSymbolContextsForFileSpec(
585     const FileSpec &file_spec, uint32_t line, bool check_inlines,
586     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
587   std::lock_guard<std::recursive_mutex> guard(m_mutex);
588   LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, "
589                      "check_inlines = %s, resolve_scope = 0x%8.8x)",
590                      file_spec.GetPath().c_str(), line,
591                      check_inlines ? "yes" : "no", resolve_scope);
592 
593   const uint32_t initial_count = sc_list.GetSize();
594 
595   if (SymbolFile *symbols = GetSymbolFile()) {
596     // TODO: Handle SourceLocationSpec column information
597     SourceLocationSpec location_spec(file_spec, line, /*column=*/std::nullopt,
598                                      check_inlines, /*exact_match=*/false);
599 
600     symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list);
601   }
602 
603   return sc_list.GetSize() - initial_count;
604 }
605 
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,size_t max_matches,VariableList & variables)606 void Module::FindGlobalVariables(ConstString name,
607                                  const CompilerDeclContext &parent_decl_ctx,
608                                  size_t max_matches, VariableList &variables) {
609   if (SymbolFile *symbols = GetSymbolFile())
610     symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables);
611 }
612 
FindGlobalVariables(const RegularExpression & regex,size_t max_matches,VariableList & variables)613 void Module::FindGlobalVariables(const RegularExpression &regex,
614                                  size_t max_matches, VariableList &variables) {
615   SymbolFile *symbols = GetSymbolFile();
616   if (symbols)
617     symbols->FindGlobalVariables(regex, max_matches, variables);
618 }
619 
FindCompileUnits(const FileSpec & path,SymbolContextList & sc_list)620 void Module::FindCompileUnits(const FileSpec &path,
621                               SymbolContextList &sc_list) {
622   const size_t num_compile_units = GetNumCompileUnits();
623   SymbolContext sc;
624   sc.module_sp = shared_from_this();
625   for (size_t i = 0; i < num_compile_units; ++i) {
626     sc.comp_unit = GetCompileUnitAtIndex(i).get();
627     if (sc.comp_unit) {
628       if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile()))
629         sc_list.Append(sc);
630     }
631   }
632 }
633 
LookupInfo(ConstString name,FunctionNameType name_type_mask,LanguageType language)634 Module::LookupInfo::LookupInfo(ConstString name,
635                                FunctionNameType name_type_mask,
636                                LanguageType language)
637     : m_name(name), m_lookup_name(), m_language(language) {
638   const char *name_cstr = name.GetCString();
639   llvm::StringRef basename;
640   llvm::StringRef context;
641 
642   if (name_type_mask & eFunctionNameTypeAuto) {
643     if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
644       m_name_type_mask = eFunctionNameTypeFull;
645     else if ((language == eLanguageTypeUnknown ||
646               Language::LanguageIsObjC(language)) &&
647              ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
648       m_name_type_mask = eFunctionNameTypeFull;
649     else if (Language::LanguageIsC(language)) {
650       m_name_type_mask = eFunctionNameTypeFull;
651     } else {
652       if ((language == eLanguageTypeUnknown ||
653            Language::LanguageIsObjC(language)) &&
654           ObjCLanguage::IsPossibleObjCSelector(name_cstr))
655         m_name_type_mask |= eFunctionNameTypeSelector;
656 
657       CPlusPlusLanguage::MethodName cpp_method(name);
658       basename = cpp_method.GetBasename();
659       if (basename.empty()) {
660         if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
661                                                            basename))
662           m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
663         else
664           m_name_type_mask |= eFunctionNameTypeFull;
665       } else {
666         m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
667       }
668     }
669   } else {
670     m_name_type_mask = name_type_mask;
671     if (name_type_mask & eFunctionNameTypeMethod ||
672         name_type_mask & eFunctionNameTypeBase) {
673       // If they've asked for a CPP method or function name and it can't be
674       // that, we don't even need to search for CPP methods or names.
675       CPlusPlusLanguage::MethodName cpp_method(name);
676       if (cpp_method.IsValid()) {
677         basename = cpp_method.GetBasename();
678 
679         if (!cpp_method.GetQualifiers().empty()) {
680           // There is a "const" or other qualifier following the end of the
681           // function parens, this can't be a eFunctionNameTypeBase
682           m_name_type_mask &= ~(eFunctionNameTypeBase);
683           if (m_name_type_mask == eFunctionNameTypeNone)
684             return;
685         }
686       } else {
687         // If the CPP method parser didn't manage to chop this up, try to fill
688         // in the base name if we can. If a::b::c is passed in, we need to just
689         // look up "c", and then we'll filter the result later.
690         CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
691                                                        basename);
692       }
693     }
694 
695     if (name_type_mask & eFunctionNameTypeSelector) {
696       if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
697         m_name_type_mask &= ~(eFunctionNameTypeSelector);
698         if (m_name_type_mask == eFunctionNameTypeNone)
699           return;
700       }
701     }
702 
703     // Still try and get a basename in case someone specifies a name type mask
704     // of eFunctionNameTypeFull and a name like "A::func"
705     if (basename.empty()) {
706       if (name_type_mask & eFunctionNameTypeFull &&
707           !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
708         CPlusPlusLanguage::MethodName cpp_method(name);
709         basename = cpp_method.GetBasename();
710         if (basename.empty())
711           CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
712                                                          basename);
713       }
714     }
715   }
716 
717   if (!basename.empty()) {
718     // The name supplied was a partial C++ path like "a::count". In this case
719     // we want to do a lookup on the basename "count" and then make sure any
720     // matching results contain "a::count" so that it would match "b::a::count"
721     // and "a::count". This is why we set "match_name_after_lookup" to true
722     m_lookup_name.SetString(basename);
723     m_match_name_after_lookup = true;
724   } else {
725     // The name is already correct, just use the exact name as supplied, and we
726     // won't need to check if any matches contain "name"
727     m_lookup_name = name;
728     m_match_name_after_lookup = false;
729   }
730 }
731 
NameMatchesLookupInfo(ConstString function_name,LanguageType language_type) const732 bool Module::LookupInfo::NameMatchesLookupInfo(
733     ConstString function_name, LanguageType language_type) const {
734   // We always keep unnamed symbols
735   if (!function_name)
736     return true;
737 
738   // If we match exactly, we can return early
739   if (m_name == function_name)
740     return true;
741 
742   // If function_name is mangled, we'll need to demangle it.
743   // In the pathologial case where the function name "looks" mangled but is
744   // actually demangled (e.g. a method named _Zonk), this operation should be
745   // relatively inexpensive since no demangling is actually occuring. See
746   // Mangled::SetValue for more context.
747   const bool function_name_may_be_mangled =
748       Mangled::GetManglingScheme(function_name) != Mangled::eManglingSchemeNone;
749   ConstString demangled_function_name = function_name;
750   if (function_name_may_be_mangled) {
751     Mangled mangled_function_name(function_name);
752     demangled_function_name = mangled_function_name.GetDemangledName();
753   }
754 
755   // If the symbol has a language, then let the language make the match.
756   // Otherwise just check that the demangled function name contains the
757   // demangled user-provided name.
758   if (Language *language = Language::FindPlugin(language_type))
759     return language->DemangledNameContainsPath(m_name, demangled_function_name);
760 
761   llvm::StringRef function_name_ref = demangled_function_name;
762   return function_name_ref.contains(m_name);
763 }
764 
Prune(SymbolContextList & sc_list,size_t start_idx) const765 void Module::LookupInfo::Prune(SymbolContextList &sc_list,
766                                size_t start_idx) const {
767   if (m_match_name_after_lookup && m_name) {
768     SymbolContext sc;
769     size_t i = start_idx;
770     while (i < sc_list.GetSize()) {
771       if (!sc_list.GetContextAtIndex(i, sc))
772         break;
773 
774       bool keep_it =
775           NameMatchesLookupInfo(sc.GetFunctionName(), sc.GetLanguage());
776       if (keep_it)
777         ++i;
778       else
779         sc_list.RemoveContextAtIndex(i);
780     }
781   }
782 
783   // If we have only full name matches we might have tried to set breakpoint on
784   // "func" and specified eFunctionNameTypeFull, but we might have found
785   // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
786   // "func()" and "func" should end up matching.
787   if (m_name_type_mask == eFunctionNameTypeFull) {
788     SymbolContext sc;
789     size_t i = start_idx;
790     while (i < sc_list.GetSize()) {
791       if (!sc_list.GetContextAtIndex(i, sc))
792         break;
793       // Make sure the mangled and demangled names don't match before we try to
794       // pull anything out
795       ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
796       ConstString full_name(sc.GetFunctionName());
797       if (mangled_name != m_name && full_name != m_name) {
798         CPlusPlusLanguage::MethodName cpp_method(full_name);
799         if (cpp_method.IsValid()) {
800           if (cpp_method.GetContext().empty()) {
801             if (cpp_method.GetBasename().compare(m_name) != 0) {
802               sc_list.RemoveContextAtIndex(i);
803               continue;
804             }
805           } else {
806             std::string qualified_name;
807             llvm::StringRef anon_prefix("(anonymous namespace)");
808             if (cpp_method.GetContext() == anon_prefix)
809               qualified_name = cpp_method.GetBasename().str();
810             else
811               qualified_name = cpp_method.GetScopeQualifiedName();
812             if (qualified_name != m_name.GetCString()) {
813               sc_list.RemoveContextAtIndex(i);
814               continue;
815             }
816           }
817         }
818       }
819       ++i;
820     }
821   }
822 }
823 
FindFunctions(const Module::LookupInfo & lookup_info,const CompilerDeclContext & parent_decl_ctx,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)824 void Module::FindFunctions(const Module::LookupInfo &lookup_info,
825                            const CompilerDeclContext &parent_decl_ctx,
826                            const ModuleFunctionSearchOptions &options,
827                            SymbolContextList &sc_list) {
828   // Find all the functions (not symbols, but debug information functions...
829   if (SymbolFile *symbols = GetSymbolFile()) {
830     symbols->FindFunctions(lookup_info, parent_decl_ctx,
831                            options.include_inlines, sc_list);
832     // Now check our symbol table for symbols that are code symbols if
833     // requested
834     if (options.include_symbols) {
835       if (Symtab *symtab = symbols->GetSymtab()) {
836         symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
837                                     lookup_info.GetNameTypeMask(), sc_list);
838       }
839     }
840   }
841 }
842 
FindFunctions(ConstString name,const CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)843 void Module::FindFunctions(ConstString name,
844                            const CompilerDeclContext &parent_decl_ctx,
845                            FunctionNameType name_type_mask,
846                            const ModuleFunctionSearchOptions &options,
847                            SymbolContextList &sc_list) {
848   const size_t old_size = sc_list.GetSize();
849   LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
850   FindFunctions(lookup_info, parent_decl_ctx, options, sc_list);
851   if (name_type_mask & eFunctionNameTypeAuto) {
852     const size_t new_size = sc_list.GetSize();
853     if (old_size < new_size)
854       lookup_info.Prune(sc_list, old_size);
855   }
856 }
857 
FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,FunctionNameType name_type_mask,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)858 void Module::FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,
859                            FunctionNameType name_type_mask,
860                            const ModuleFunctionSearchOptions &options,
861                            SymbolContextList &sc_list) {
862   if (compiler_ctx.empty() ||
863       compiler_ctx.back().kind != CompilerContextKind::Function)
864     return;
865   ConstString name = compiler_ctx.back().name;
866   SymbolContextList unfiltered;
867   FindFunctions(name, CompilerDeclContext(), name_type_mask, options,
868                 unfiltered);
869   // Filter by context.
870   for (auto &sc : unfiltered)
871     if (sc.function && compiler_ctx.equals(sc.function->GetCompilerContext()))
872       sc_list.Append(sc);
873 }
874 
FindFunctions(const RegularExpression & regex,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)875 void Module::FindFunctions(const RegularExpression &regex,
876                            const ModuleFunctionSearchOptions &options,
877                            SymbolContextList &sc_list) {
878   const size_t start_size = sc_list.GetSize();
879 
880   if (SymbolFile *symbols = GetSymbolFile()) {
881     symbols->FindFunctions(regex, options.include_inlines, sc_list);
882 
883     // Now check our symbol table for symbols that are code symbols if
884     // requested
885     if (options.include_symbols) {
886       Symtab *symtab = symbols->GetSymtab();
887       if (symtab) {
888         std::vector<uint32_t> symbol_indexes;
889         symtab->AppendSymbolIndexesMatchingRegExAndType(
890             regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
891             symbol_indexes);
892         const size_t num_matches = symbol_indexes.size();
893         if (num_matches) {
894           SymbolContext sc(this);
895           const size_t end_functions_added_index = sc_list.GetSize();
896           size_t num_functions_added_to_sc_list =
897               end_functions_added_index - start_size;
898           if (num_functions_added_to_sc_list == 0) {
899             // No functions were added, just symbols, so we can just append
900             // them
901             for (size_t i = 0; i < num_matches; ++i) {
902               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
903               SymbolType sym_type = sc.symbol->GetType();
904               if (sc.symbol && (sym_type == eSymbolTypeCode ||
905                                 sym_type == eSymbolTypeResolver))
906                 sc_list.Append(sc);
907             }
908           } else {
909             typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
910             FileAddrToIndexMap file_addr_to_index;
911             for (size_t i = start_size; i < end_functions_added_index; ++i) {
912               const SymbolContext &sc = sc_list[i];
913               if (sc.block)
914                 continue;
915               file_addr_to_index[sc.function->GetAddressRange()
916                                      .GetBaseAddress()
917                                      .GetFileAddress()] = i;
918             }
919 
920             FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
921             // Functions were added so we need to merge symbols into any
922             // existing function symbol contexts
923             for (size_t i = start_size; i < num_matches; ++i) {
924               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
925               SymbolType sym_type = sc.symbol->GetType();
926               if (sc.symbol && sc.symbol->ValueIsAddress() &&
927                   (sym_type == eSymbolTypeCode ||
928                    sym_type == eSymbolTypeResolver)) {
929                 FileAddrToIndexMap::const_iterator pos =
930                     file_addr_to_index.find(
931                         sc.symbol->GetAddressRef().GetFileAddress());
932                 if (pos == end)
933                   sc_list.Append(sc);
934                 else
935                   sc_list[pos->second].symbol = sc.symbol;
936               }
937             }
938           }
939         }
940       }
941     }
942   }
943 }
944 
FindAddressesForLine(const lldb::TargetSP target_sp,const FileSpec & file,uint32_t line,Function * function,std::vector<Address> & output_local,std::vector<Address> & output_extern)945 void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
946                                   const FileSpec &file, uint32_t line,
947                                   Function *function,
948                                   std::vector<Address> &output_local,
949                                   std::vector<Address> &output_extern) {
950   SearchFilterByModule filter(target_sp, m_file);
951 
952   // TODO: Handle SourceLocationSpec column information
953   SourceLocationSpec location_spec(file, line, /*column=*/std::nullopt,
954                                    /*check_inlines=*/true,
955                                    /*exact_match=*/false);
956   AddressResolverFileLine resolver(location_spec);
957   resolver.ResolveAddress(filter);
958 
959   for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
960     Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
961     Function *f = addr.CalculateSymbolContextFunction();
962     if (f && f == function)
963       output_local.push_back(addr);
964     else
965       output_extern.push_back(addr);
966   }
967 }
968 
FindTypes(const TypeQuery & query,TypeResults & results)969 void Module::FindTypes(const TypeQuery &query, TypeResults &results) {
970   if (SymbolFile *symbols = GetSymbolFile())
971     symbols->FindTypes(query, results);
972 }
973 
974 static Debugger::DebuggerList
DebuggersOwningModuleRequestingInterruption(Module & module)975 DebuggersOwningModuleRequestingInterruption(Module &module) {
976   Debugger::DebuggerList requestors =
977       Debugger::DebuggersRequestingInterruption();
978   Debugger::DebuggerList interruptors;
979   if (requestors.empty())
980     return interruptors;
981 
982   for (auto debugger_sp : requestors) {
983     if (!debugger_sp->InterruptRequested())
984       continue;
985     if (debugger_sp->GetTargetList()
986         .AnyTargetContainsModule(module))
987       interruptors.push_back(debugger_sp);
988   }
989   return interruptors;
990 }
991 
GetSymbolFile(bool can_create,Stream * feedback_strm)992 SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
993   if (!m_did_load_symfile.load()) {
994     std::lock_guard<std::recursive_mutex> guard(m_mutex);
995     if (!m_did_load_symfile.load() && can_create) {
996       Debugger::DebuggerList interruptors =
997           DebuggersOwningModuleRequestingInterruption(*this);
998       if (!interruptors.empty()) {
999         for (auto debugger_sp : interruptors) {
1000           REPORT_INTERRUPTION(*(debugger_sp.get()),
1001                               "Interrupted fetching symbols for module {0}",
1002                               this->GetFileSpec());
1003         }
1004         return nullptr;
1005       }
1006       ObjectFile *obj_file = GetObjectFile();
1007       if (obj_file != nullptr) {
1008         LLDB_SCOPED_TIMER();
1009         m_symfile_up.reset(
1010             SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1011         m_did_load_symfile = true;
1012         if (m_unwind_table)
1013           m_unwind_table->Update();
1014       }
1015     }
1016   }
1017   return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
1018 }
1019 
GetSymtab()1020 Symtab *Module::GetSymtab() {
1021   if (SymbolFile *symbols = GetSymbolFile())
1022     return symbols->GetSymtab();
1023   return nullptr;
1024 }
1025 
SetFileSpecAndObjectName(const FileSpec & file,ConstString object_name)1026 void Module::SetFileSpecAndObjectName(const FileSpec &file,
1027                                       ConstString object_name) {
1028   // Container objects whose paths do not specify a file directly can call this
1029   // function to correct the file and object names.
1030   m_file = file;
1031   m_mod_time = FileSystem::Instance().GetModificationTime(file);
1032   m_object_name = object_name;
1033 }
1034 
GetArchitecture() const1035 const ArchSpec &Module::GetArchitecture() const { return m_arch; }
1036 
GetSpecificationDescription() const1037 std::string Module::GetSpecificationDescription() const {
1038   std::string spec(GetFileSpec().GetPath());
1039   if (m_object_name) {
1040     spec += '(';
1041     spec += m_object_name.GetCString();
1042     spec += ')';
1043   }
1044   return spec;
1045 }
1046 
GetDescription(llvm::raw_ostream & s,lldb::DescriptionLevel level)1047 void Module::GetDescription(llvm::raw_ostream &s,
1048                             lldb::DescriptionLevel level) {
1049   if (level >= eDescriptionLevelFull) {
1050     if (m_arch.IsValid())
1051       s << llvm::formatv("({0}) ", m_arch.GetArchitectureName());
1052   }
1053 
1054   if (level == eDescriptionLevelBrief) {
1055     const char *filename = m_file.GetFilename().GetCString();
1056     if (filename)
1057       s << filename;
1058   } else {
1059     char path[PATH_MAX];
1060     if (m_file.GetPath(path, sizeof(path)))
1061       s << path;
1062   }
1063 
1064   const char *object_name = m_object_name.GetCString();
1065   if (object_name)
1066     s << llvm::formatv("({0})", object_name);
1067 }
1068 
FileHasChanged() const1069 bool Module::FileHasChanged() const {
1070   // We have provided the DataBuffer for this module to avoid accessing the
1071   // filesystem. We never want to reload those files.
1072   if (m_data_sp)
1073     return false;
1074   if (!m_file_has_changed)
1075     m_file_has_changed =
1076         (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
1077   return m_file_has_changed;
1078 }
1079 
ReportWarningOptimization(std::optional<lldb::user_id_t> debugger_id)1080 void Module::ReportWarningOptimization(
1081     std::optional<lldb::user_id_t> debugger_id) {
1082   ConstString file_name = GetFileSpec().GetFilename();
1083   if (file_name.IsEmpty())
1084     return;
1085 
1086   StreamString ss;
1087   ss << file_name
1088      << " was compiled with optimization - stepping may behave "
1089         "oddly; variables may not be available.";
1090   Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1091                           &m_optimization_warning);
1092 }
1093 
ReportWarningUnsupportedLanguage(LanguageType language,std::optional<lldb::user_id_t> debugger_id)1094 void Module::ReportWarningUnsupportedLanguage(
1095     LanguageType language, std::optional<lldb::user_id_t> debugger_id) {
1096   StreamString ss;
1097   ss << "This version of LLDB has no plugin for the language \""
1098      << Language::GetNameForLanguageType(language)
1099      << "\". "
1100         "Inspection of frame variables will be limited.";
1101   Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1102                           &m_language_warning);
1103 }
1104 
ReportErrorIfModifyDetected(const llvm::formatv_object_base & payload)1105 void Module::ReportErrorIfModifyDetected(
1106     const llvm::formatv_object_base &payload) {
1107   if (!m_first_file_changed_log) {
1108     if (FileHasChanged()) {
1109       m_first_file_changed_log = true;
1110       StreamString strm;
1111       strm.PutCString("the object file ");
1112       GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1113       strm.PutCString(" has been modified\n");
1114       strm.PutCString(payload.str());
1115       strm.PutCString("The debug session should be aborted as the original "
1116                       "debug information has been overwritten.");
1117       Debugger::ReportError(std::string(strm.GetString()));
1118     }
1119   }
1120 }
1121 
ReportError(const llvm::formatv_object_base & payload)1122 void Module::ReportError(const llvm::formatv_object_base &payload) {
1123   StreamString strm;
1124   GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief);
1125   strm.PutChar(' ');
1126   strm.PutCString(payload.str());
1127   Debugger::ReportError(strm.GetString().str());
1128 }
1129 
ReportWarning(const llvm::formatv_object_base & payload)1130 void Module::ReportWarning(const llvm::formatv_object_base &payload) {
1131   StreamString strm;
1132   GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1133   strm.PutChar(' ');
1134   strm.PutCString(payload.str());
1135   Debugger::ReportWarning(std::string(strm.GetString()));
1136 }
1137 
LogMessage(Log * log,const llvm::formatv_object_base & payload)1138 void Module::LogMessage(Log *log, const llvm::formatv_object_base &payload) {
1139   StreamString log_message;
1140   GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1141   log_message.PutCString(": ");
1142   log_message.PutCString(payload.str());
1143   log->PutCString(log_message.GetData());
1144 }
1145 
LogMessageVerboseBacktrace(Log * log,const llvm::formatv_object_base & payload)1146 void Module::LogMessageVerboseBacktrace(
1147     Log *log, const llvm::formatv_object_base &payload) {
1148   StreamString log_message;
1149   GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1150   log_message.PutCString(": ");
1151   log_message.PutCString(payload.str());
1152   if (log->GetVerbose()) {
1153     std::string back_trace;
1154     llvm::raw_string_ostream stream(back_trace);
1155     llvm::sys::PrintStackTrace(stream);
1156     log_message.PutCString(back_trace);
1157   }
1158   log->PutCString(log_message.GetData());
1159 }
1160 
Dump(Stream * s)1161 void Module::Dump(Stream *s) {
1162   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1163   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1164   s->Indent();
1165   s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
1166             m_object_name ? "(" : "",
1167             m_object_name ? m_object_name.GetCString() : "",
1168             m_object_name ? ")" : "");
1169 
1170   s->IndentMore();
1171 
1172   ObjectFile *objfile = GetObjectFile();
1173   if (objfile)
1174     objfile->Dump(s);
1175 
1176   if (SymbolFile *symbols = GetSymbolFile())
1177     symbols->Dump(*s);
1178 
1179   s->IndentLess();
1180 }
1181 
GetObjectName() const1182 ConstString Module::GetObjectName() const { return m_object_name; }
1183 
GetObjectFile()1184 ObjectFile *Module::GetObjectFile() {
1185   if (!m_did_load_objfile.load()) {
1186     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1187     if (!m_did_load_objfile.load()) {
1188       LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s",
1189                          GetFileSpec().GetFilename().AsCString(""));
1190       lldb::offset_t data_offset = 0;
1191       lldb::offset_t file_size = 0;
1192 
1193       if (m_data_sp)
1194         file_size = m_data_sp->GetByteSize();
1195       else if (m_file)
1196         file_size = FileSystem::Instance().GetByteSize(m_file);
1197 
1198       if (file_size > m_object_offset) {
1199         m_did_load_objfile = true;
1200         // FindPlugin will modify its data_sp argument. Do not let it
1201         // modify our m_data_sp member.
1202         auto data_sp = m_data_sp;
1203         m_objfile_sp = ObjectFile::FindPlugin(
1204             shared_from_this(), &m_file, m_object_offset,
1205             file_size - m_object_offset, data_sp, data_offset);
1206         if (m_objfile_sp) {
1207           // Once we get the object file, update our module with the object
1208           // file's architecture since it might differ in vendor/os if some
1209           // parts were unknown.  But since the matching arch might already be
1210           // more specific than the generic COFF architecture, only merge in
1211           // those values that overwrite unspecified unknown values.
1212           m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1213         } else {
1214           ReportError("failed to load objfile for {0}\nDebugging will be "
1215                       "degraded for this module.",
1216                       GetFileSpec().GetPath().c_str());
1217         }
1218       }
1219     }
1220   }
1221   return m_objfile_sp.get();
1222 }
1223 
GetSectionList()1224 SectionList *Module::GetSectionList() {
1225   // Populate m_sections_up with sections from objfile.
1226   if (!m_sections_up) {
1227     ObjectFile *obj_file = GetObjectFile();
1228     if (obj_file != nullptr)
1229       obj_file->CreateSections(*GetUnifiedSectionList());
1230   }
1231   return m_sections_up.get();
1232 }
1233 
SectionFileAddressesChanged()1234 void Module::SectionFileAddressesChanged() {
1235   ObjectFile *obj_file = GetObjectFile();
1236   if (obj_file)
1237     obj_file->SectionFileAddressesChanged();
1238   if (SymbolFile *symbols = GetSymbolFile())
1239     symbols->SectionFileAddressesChanged();
1240 }
1241 
GetUnwindTable()1242 UnwindTable &Module::GetUnwindTable() {
1243   if (!m_unwind_table) {
1244     if (!m_symfile_spec)
1245       SymbolLocator::DownloadSymbolFileAsync(GetUUID());
1246     m_unwind_table.emplace(*this);
1247   }
1248   return *m_unwind_table;
1249 }
1250 
GetUnifiedSectionList()1251 SectionList *Module::GetUnifiedSectionList() {
1252   if (!m_sections_up)
1253     m_sections_up = std::make_unique<SectionList>();
1254   return m_sections_up.get();
1255 }
1256 
FindFirstSymbolWithNameAndType(ConstString name,SymbolType symbol_type)1257 const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1258                                                      SymbolType symbol_type) {
1259   LLDB_SCOPED_TIMERF(
1260       "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1261       name.AsCString(), symbol_type);
1262   if (Symtab *symtab = GetSymtab())
1263     return symtab->FindFirstSymbolWithNameAndType(
1264         name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1265   return nullptr;
1266 }
SymbolIndicesToSymbolContextList(Symtab * symtab,std::vector<uint32_t> & symbol_indexes,SymbolContextList & sc_list)1267 void Module::SymbolIndicesToSymbolContextList(
1268     Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1269     SymbolContextList &sc_list) {
1270   // No need to protect this call using m_mutex all other method calls are
1271   // already thread safe.
1272 
1273   size_t num_indices = symbol_indexes.size();
1274   if (num_indices > 0) {
1275     SymbolContext sc;
1276     CalculateSymbolContext(&sc);
1277     for (size_t i = 0; i < num_indices; i++) {
1278       sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
1279       if (sc.symbol)
1280         sc_list.Append(sc);
1281     }
1282   }
1283 }
1284 
FindFunctionSymbols(ConstString name,uint32_t name_type_mask,SymbolContextList & sc_list)1285 void Module::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1286                                  SymbolContextList &sc_list) {
1287   LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1288                      name.AsCString(), name_type_mask);
1289   if (Symtab *symtab = GetSymtab())
1290     symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1291 }
1292 
FindSymbolsWithNameAndType(ConstString name,SymbolType symbol_type,SymbolContextList & sc_list)1293 void Module::FindSymbolsWithNameAndType(ConstString name,
1294                                         SymbolType symbol_type,
1295                                         SymbolContextList &sc_list) {
1296   // No need to protect this call using m_mutex all other method calls are
1297   // already thread safe.
1298   if (Symtab *symtab = GetSymtab()) {
1299     std::vector<uint32_t> symbol_indexes;
1300     symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
1301     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1302   }
1303 }
1304 
FindSymbolsMatchingRegExAndType(const RegularExpression & regex,SymbolType symbol_type,SymbolContextList & sc_list,Mangled::NamePreference mangling_preference)1305 void Module::FindSymbolsMatchingRegExAndType(
1306     const RegularExpression &regex, SymbolType symbol_type,
1307     SymbolContextList &sc_list, Mangled::NamePreference mangling_preference) {
1308   // No need to protect this call using m_mutex all other method calls are
1309   // already thread safe.
1310   LLDB_SCOPED_TIMERF(
1311       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1312       regex.GetText().str().c_str(), symbol_type);
1313   if (Symtab *symtab = GetSymtab()) {
1314     std::vector<uint32_t> symbol_indexes;
1315     symtab->FindAllSymbolsMatchingRexExAndType(
1316         regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1317         symbol_indexes, mangling_preference);
1318     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1319   }
1320 }
1321 
PreloadSymbols()1322 void Module::PreloadSymbols() {
1323   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1324   SymbolFile *sym_file = GetSymbolFile();
1325   if (!sym_file)
1326     return;
1327 
1328   // Load the object file symbol table and any symbols from the SymbolFile that
1329   // get appended using SymbolFile::AddSymbols(...).
1330   if (Symtab *symtab = sym_file->GetSymtab())
1331     symtab->PreloadSymbols();
1332 
1333   // Now let the symbol file preload its data and the symbol table will be
1334   // available without needing to take the module lock.
1335   sym_file->PreloadSymbols();
1336 }
1337 
SetSymbolFileFileSpec(const FileSpec & file)1338 void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1339   if (!FileSystem::Instance().Exists(file))
1340     return;
1341   if (m_symfile_up) {
1342     // Remove any sections in the unified section list that come from the
1343     // current symbol vendor.
1344     SectionList *section_list = GetSectionList();
1345     SymbolFile *symbol_file = GetSymbolFile();
1346     if (section_list && symbol_file) {
1347       ObjectFile *obj_file = symbol_file->GetObjectFile();
1348       // Make sure we have an object file and that the symbol vendor's objfile
1349       // isn't the same as the module's objfile before we remove any sections
1350       // for it...
1351       if (obj_file) {
1352         // Check to make sure we aren't trying to specify the file we already
1353         // have
1354         if (obj_file->GetFileSpec() == file) {
1355           // We are being told to add the exact same file that we already have
1356           // we don't have to do anything.
1357           return;
1358         }
1359 
1360         // Cleare the current symtab as we are going to replace it with a new
1361         // one
1362         obj_file->ClearSymtab();
1363 
1364         // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
1365         // instead of a full path to the symbol file within the bundle
1366         // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1367         // check this
1368         if (FileSystem::Instance().IsDirectory(file)) {
1369           std::string new_path(file.GetPath());
1370           std::string old_path(obj_file->GetFileSpec().GetPath());
1371           if (llvm::StringRef(old_path).starts_with(new_path)) {
1372             // We specified the same bundle as the symbol file that we already
1373             // have
1374             return;
1375           }
1376         }
1377 
1378         if (obj_file != m_objfile_sp.get()) {
1379           size_t num_sections = section_list->GetNumSections(0);
1380           for (size_t idx = num_sections; idx > 0; --idx) {
1381             lldb::SectionSP section_sp(
1382                 section_list->GetSectionAtIndex(idx - 1));
1383             if (section_sp->GetObjectFile() == obj_file) {
1384               section_list->DeleteSection(idx - 1);
1385             }
1386           }
1387         }
1388       }
1389     }
1390     // Keep all old symbol files around in case there are any lingering type
1391     // references in any SBValue objects that might have been handed out.
1392     m_old_symfiles.push_back(std::move(m_symfile_up));
1393   }
1394   m_symfile_spec = file;
1395   m_symfile_up.reset();
1396   m_did_load_symfile = false;
1397 }
1398 
IsExecutable()1399 bool Module::IsExecutable() {
1400   if (GetObjectFile() == nullptr)
1401     return false;
1402   else
1403     return GetObjectFile()->IsExecutable();
1404 }
1405 
IsLoadedInTarget(Target * target)1406 bool Module::IsLoadedInTarget(Target *target) {
1407   ObjectFile *obj_file = GetObjectFile();
1408   if (obj_file) {
1409     SectionList *sections = GetSectionList();
1410     if (sections != nullptr) {
1411       size_t num_sections = sections->GetSize();
1412       for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1413         SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1414         if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1415           return true;
1416         }
1417       }
1418     }
1419   }
1420   return false;
1421 }
1422 
LoadScriptingResourceInTarget(Target * target,Status & error,Stream & feedback_stream)1423 bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1424                                            Stream &feedback_stream) {
1425   if (!target) {
1426     error.SetErrorString("invalid destination Target");
1427     return false;
1428   }
1429 
1430   LoadScriptFromSymFile should_load =
1431       target->TargetProperties::GetLoadScriptFromSymbolFile();
1432 
1433   if (should_load == eLoadScriptFromSymFileFalse)
1434     return false;
1435 
1436   Debugger &debugger = target->GetDebugger();
1437   const ScriptLanguage script_language = debugger.GetScriptLanguage();
1438   if (script_language != eScriptLanguageNone) {
1439 
1440     PlatformSP platform_sp(target->GetPlatform());
1441 
1442     if (!platform_sp) {
1443       error.SetErrorString("invalid Platform");
1444       return false;
1445     }
1446 
1447     FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1448         target, *this, feedback_stream);
1449 
1450     const uint32_t num_specs = file_specs.GetSize();
1451     if (num_specs) {
1452       ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1453       if (script_interpreter) {
1454         for (uint32_t i = 0; i < num_specs; ++i) {
1455           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1456           if (scripting_fspec &&
1457               FileSystem::Instance().Exists(scripting_fspec)) {
1458             if (should_load == eLoadScriptFromSymFileWarn) {
1459               feedback_stream.Printf(
1460                   "warning: '%s' contains a debug script. To run this script "
1461                   "in "
1462                   "this debug session:\n\n    command script import "
1463                   "\"%s\"\n\n"
1464                   "To run all discovered debug scripts in this session:\n\n"
1465                   "    settings set target.load-script-from-symbol-file "
1466                   "true\n",
1467                   GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1468                   scripting_fspec.GetPath().c_str());
1469               return false;
1470             }
1471             StreamString scripting_stream;
1472             scripting_fspec.Dump(scripting_stream.AsRawOstream());
1473             LoadScriptOptions options;
1474             bool did_load = script_interpreter->LoadScriptingModule(
1475                 scripting_stream.GetData(), options, error);
1476             if (!did_load)
1477               return false;
1478           }
1479         }
1480       } else {
1481         error.SetErrorString("invalid ScriptInterpreter");
1482         return false;
1483       }
1484     }
1485   }
1486   return true;
1487 }
1488 
SetArchitecture(const ArchSpec & new_arch)1489 bool Module::SetArchitecture(const ArchSpec &new_arch) {
1490   if (!m_arch.IsValid()) {
1491     m_arch = new_arch;
1492     return true;
1493   }
1494   return m_arch.IsCompatibleMatch(new_arch);
1495 }
1496 
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset,bool & changed)1497 bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1498                             bool value_is_offset, bool &changed) {
1499   ObjectFile *object_file = GetObjectFile();
1500   if (object_file != nullptr) {
1501     changed = object_file->SetLoadAddress(target, value, value_is_offset);
1502     return true;
1503   } else {
1504     changed = false;
1505   }
1506   return false;
1507 }
1508 
MatchesModuleSpec(const ModuleSpec & module_ref)1509 bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1510   const UUID &uuid = module_ref.GetUUID();
1511 
1512   if (uuid.IsValid()) {
1513     // If the UUID matches, then nothing more needs to match...
1514     return (uuid == GetUUID());
1515   }
1516 
1517   const FileSpec &file_spec = module_ref.GetFileSpec();
1518   if (!FileSpec::Match(file_spec, m_file) &&
1519       !FileSpec::Match(file_spec, m_platform_file))
1520     return false;
1521 
1522   const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1523   if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec()))
1524     return false;
1525 
1526   const ArchSpec &arch = module_ref.GetArchitecture();
1527   if (arch.IsValid()) {
1528     if (!m_arch.IsCompatibleMatch(arch))
1529       return false;
1530   }
1531 
1532   ConstString object_name = module_ref.GetObjectName();
1533   if (object_name) {
1534     if (object_name != GetObjectName())
1535       return false;
1536   }
1537   return true;
1538 }
1539 
FindSourceFile(const FileSpec & orig_spec,FileSpec & new_spec) const1540 bool Module::FindSourceFile(const FileSpec &orig_spec,
1541                             FileSpec &new_spec) const {
1542   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1543   if (auto remapped = m_source_mappings.FindFile(orig_spec)) {
1544     new_spec = *remapped;
1545     return true;
1546   }
1547   return false;
1548 }
1549 
RemapSourceFile(llvm::StringRef path) const1550 std::optional<std::string> Module::RemapSourceFile(llvm::StringRef path) const {
1551   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1552   if (auto remapped = m_source_mappings.RemapPath(path))
1553     return remapped->GetPath();
1554   return {};
1555 }
1556 
RegisterXcodeSDK(llvm::StringRef sdk_name,llvm::StringRef sysroot)1557 void Module::RegisterXcodeSDK(llvm::StringRef sdk_name,
1558                               llvm::StringRef sysroot) {
1559   auto sdk_path_or_err =
1560       HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk_name.str()});
1561 
1562   if (!sdk_path_or_err) {
1563     Debugger::ReportError("Error while searching for Xcode SDK: " +
1564                           toString(sdk_path_or_err.takeError()));
1565     return;
1566   }
1567 
1568   auto sdk_path = *sdk_path_or_err;
1569   if (sdk_path.empty())
1570     return;
1571   // If the SDK changed for a previously registered source path, update it.
1572   // This could happend with -fdebug-prefix-map, otherwise it's unlikely.
1573   if (!m_source_mappings.Replace(sysroot, sdk_path, true))
1574     // In the general case, however, append it to the list.
1575     m_source_mappings.Append(sysroot, sdk_path, false);
1576 }
1577 
MergeArchitecture(const ArchSpec & arch_spec)1578 bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
1579   if (!arch_spec.IsValid())
1580     return false;
1581   LLDB_LOGF(GetLog(LLDBLog::Object | LLDBLog::Modules),
1582             "module has arch %s, merging/replacing with arch %s",
1583             m_arch.GetTriple().getTriple().c_str(),
1584             arch_spec.GetTriple().getTriple().c_str());
1585   if (!m_arch.IsCompatibleMatch(arch_spec)) {
1586     // The new architecture is different, we just need to replace it.
1587     return SetArchitecture(arch_spec);
1588   }
1589 
1590   // Merge bits from arch_spec into "merged_arch" and set our architecture.
1591   ArchSpec merged_arch(m_arch);
1592   merged_arch.MergeFrom(arch_spec);
1593   // SetArchitecture() is a no-op if m_arch is already valid.
1594   m_arch = ArchSpec();
1595   return SetArchitecture(merged_arch);
1596 }
1597 
GetVersion()1598 llvm::VersionTuple Module::GetVersion() {
1599   if (ObjectFile *obj_file = GetObjectFile())
1600     return obj_file->GetVersion();
1601   return llvm::VersionTuple();
1602 }
1603 
GetIsDynamicLinkEditor()1604 bool Module::GetIsDynamicLinkEditor() {
1605   ObjectFile *obj_file = GetObjectFile();
1606 
1607   if (obj_file)
1608     return obj_file->GetIsDynamicLinkEditor();
1609 
1610   return false;
1611 }
1612 
Hash()1613 uint32_t Module::Hash() {
1614   std::string identifier;
1615   llvm::raw_string_ostream id_strm(identifier);
1616   id_strm << m_arch.GetTriple().str() << '-' << m_file.GetPath();
1617   if (m_object_name)
1618     id_strm << '(' << m_object_name << ')';
1619   if (m_object_offset > 0)
1620     id_strm << m_object_offset;
1621   const auto mtime = llvm::sys::toTimeT(m_object_mod_time);
1622   if (mtime > 0)
1623     id_strm << mtime;
1624   return llvm::djbHash(id_strm.str());
1625 }
1626 
GetCacheKey()1627 std::string Module::GetCacheKey() {
1628   std::string key;
1629   llvm::raw_string_ostream strm(key);
1630   strm << m_arch.GetTriple().str() << '-' << m_file.GetFilename();
1631   if (m_object_name)
1632     strm << '(' << m_object_name << ')';
1633   strm << '-' << llvm::format_hex(Hash(), 10);
1634   return strm.str();
1635 }
1636 
GetIndexCache()1637 DataFileCache *Module::GetIndexCache() {
1638   if (!ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache())
1639     return nullptr;
1640   // NOTE: intentional leak so we don't crash if global destructor chain gets
1641   // called as other threads still use the result of this function
1642   static DataFileCache *g_data_file_cache =
1643       new DataFileCache(ModuleList::GetGlobalModuleListProperties()
1644                             .GetLLDBIndexCachePath()
1645                             .GetPath());
1646   return g_data_file_cache;
1647 }
1648