/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Module.cpp
Line | Count | Source (jump to first uncovered line) |
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/LocateSymbolFile.h" |
27 | | #include "lldb/Symbol/ObjectFile.h" |
28 | | #include "lldb/Symbol/Symbol.h" |
29 | | #include "lldb/Symbol/SymbolContext.h" |
30 | | #include "lldb/Symbol/SymbolFile.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 | | |
92 | 238k | 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 | 238k | static ModuleCollection *g_module_collection = nullptr; |
100 | 238k | if (g_module_collection == nullptr) |
101 | 1.49k | g_module_collection = new ModuleCollection(); |
102 | | |
103 | 238k | return *g_module_collection; |
104 | 238k | } |
105 | | |
106 | 239k | 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 | 239k | static std::recursive_mutex *g_module_collection_mutex = nullptr; |
113 | 239k | if (g_module_collection_mutex == nullptr) |
114 | 1.49k | g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak |
115 | 239k | return *g_module_collection_mutex; |
116 | 239k | } |
117 | | |
118 | 3.25k | size_t Module::GetNumberAllocatedModules() { |
119 | 3.25k | std::lock_guard<std::recursive_mutex> guard( |
120 | 3.25k | GetAllocationModuleCollectionMutex()); |
121 | 3.25k | return GetModuleCollection().size(); |
122 | 3.25k | } |
123 | | |
124 | 4.67k | Module *Module::GetAllocatedModuleAtIndex(size_t idx) { |
125 | 4.67k | std::lock_guard<std::recursive_mutex> guard( |
126 | 4.67k | GetAllocationModuleCollectionMutex()); |
127 | 4.67k | ModuleCollection &modules = GetModuleCollection(); |
128 | 4.67k | if (idx < modules.size()) |
129 | 4.67k | return modules[idx]; |
130 | 0 | return nullptr; |
131 | 4.67k | } |
132 | | |
133 | | Module::Module(const ModuleSpec &module_spec) |
134 | 113k | : m_file_has_changed(false), m_first_file_changed_log(false) { |
135 | | // Scope for locker below... |
136 | 113k | { |
137 | 113k | std::lock_guard<std::recursive_mutex> guard( |
138 | 113k | GetAllocationModuleCollectionMutex()); |
139 | 113k | GetModuleCollection().push_back(this); |
140 | 113k | } |
141 | | |
142 | 113k | Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules)); |
143 | 113k | if (log != nullptr) |
144 | 0 | LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')", |
145 | 113k | static_cast<void *>(this), |
146 | 113k | module_spec.GetArchitecture().GetArchitectureName(), |
147 | 113k | module_spec.GetFileSpec().GetPath().c_str(), |
148 | 113k | module_spec.GetObjectName().IsEmpty() ? "" : "(", |
149 | 113k | module_spec.GetObjectName().AsCString(""), |
150 | 113k | module_spec.GetObjectName().IsEmpty() ? "" : ")"); |
151 | | |
152 | 113k | auto data_sp = module_spec.GetData(); |
153 | 113k | lldb::offset_t file_size = 0; |
154 | 113k | if (data_sp) |
155 | 102k | 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 | 113k | ModuleSpecList modules_specs; |
160 | 113k | if (ObjectFile::GetModuleSpecifications( |
161 | 113k | module_spec.GetFileSpec(), 0, file_size, modules_specs, data_sp) == 0) |
162 | 1.00k | 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 | 112k | ModuleSpec matching_module_spec; |
172 | 112k | if (!modules_specs.FindMatchingModuleSpec(module_spec, |
173 | 112k | matching_module_spec)) { |
174 | 32 | if (log) { |
175 | 0 | LLDB_LOGF(log, "Found local object file but the specs didn't match"); |
176 | 0 | } |
177 | 32 | return; |
178 | 32 | } |
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 | 112k | if (auto module_spec_data_sp = module_spec.GetData()) { |
184 | 102k | m_data_sp = module_spec_data_sp; |
185 | 102k | m_mod_time = {}; |
186 | 102k | } else { |
187 | 9.23k | if (module_spec.GetFileSpec()) |
188 | 9.23k | m_mod_time = |
189 | 9.23k | FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec()); |
190 | 0 | else if (matching_module_spec.GetFileSpec()) |
191 | 0 | m_mod_time = FileSystem::Instance().GetModificationTime( |
192 | 0 | matching_module_spec.GetFileSpec()); |
193 | 9.23k | } |
194 | | |
195 | | // Copy the architecture from the actual spec if we got one back, else use |
196 | | // the one that was specified |
197 | 112k | if (matching_module_spec.GetArchitecture().IsValid()) |
198 | 112k | m_arch = matching_module_spec.GetArchitecture(); |
199 | 0 | else if (module_spec.GetArchitecture().IsValid()) |
200 | 0 | 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 | 112k | if (module_spec.GetFileSpec()) |
206 | 111k | m_file = module_spec.GetFileSpec(); |
207 | 45 | else if (matching_module_spec.GetFileSpec()) |
208 | 0 | m_file = matching_module_spec.GetFileSpec(); |
209 | | |
210 | | // Copy the platform file spec over |
211 | 112k | if (module_spec.GetPlatformFileSpec()) |
212 | 28 | m_platform_file = module_spec.GetPlatformFileSpec(); |
213 | 111k | else if (matching_module_spec.GetPlatformFileSpec()) |
214 | 0 | m_platform_file = matching_module_spec.GetPlatformFileSpec(); |
215 | | |
216 | | // Copy the symbol file spec over |
217 | 112k | if (module_spec.GetSymbolFileSpec()) |
218 | 37 | m_symfile_spec = module_spec.GetSymbolFileSpec(); |
219 | 111k | else if (matching_module_spec.GetSymbolFileSpec()) |
220 | 0 | m_symfile_spec = matching_module_spec.GetSymbolFileSpec(); |
221 | | |
222 | | // Copy the object name over |
223 | 112k | if (matching_module_spec.GetObjectName()) |
224 | 0 | m_object_name = matching_module_spec.GetObjectName(); |
225 | 112k | else |
226 | 112k | 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 | 112k | m_object_offset = matching_module_spec.GetObjectOffset(); |
232 | 112k | m_object_mod_time = matching_module_spec.GetObjectModificationTime(); |
233 | 112k | } |
234 | | |
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 | 1.01k | m_file_has_changed(false), m_first_file_changed_log(false) { |
242 | | // Scope for locker below... |
243 | 1.01k | { |
244 | 1.01k | std::lock_guard<std::recursive_mutex> guard( |
245 | 1.01k | GetAllocationModuleCollectionMutex()); |
246 | 1.01k | GetModuleCollection().push_back(this); |
247 | 1.01k | } |
248 | | |
249 | 1.01k | Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules)); |
250 | 1.01k | if (log != nullptr) |
251 | 0 | LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')", |
252 | 1.01k | static_cast<void *>(this), m_arch.GetArchitectureName(), |
253 | 1.01k | m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(", |
254 | 1.01k | m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")"); |
255 | 1.01k | } |
256 | | |
257 | 4.32k | Module::Module() : m_file_has_changed(false), m_first_file_changed_log(false) { |
258 | 4.32k | std::lock_guard<std::recursive_mutex> guard( |
259 | 4.32k | GetAllocationModuleCollectionMutex()); |
260 | 4.32k | GetModuleCollection().push_back(this); |
261 | 4.32k | } |
262 | | |
263 | 112k | 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 | 112k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
267 | | // Scope for locker below... |
268 | 112k | { |
269 | 112k | std::lock_guard<std::recursive_mutex> guard( |
270 | 112k | GetAllocationModuleCollectionMutex()); |
271 | 112k | ModuleCollection &modules = GetModuleCollection(); |
272 | 112k | ModuleCollection::iterator end = modules.end(); |
273 | 112k | ModuleCollection::iterator pos = std::find(modules.begin(), end, this); |
274 | 112k | assert(pos != end); |
275 | 112k | modules.erase(pos); |
276 | 112k | } |
277 | 0 | Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules)); |
278 | 112k | if (log != nullptr) |
279 | 0 | LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')", |
280 | 112k | static_cast<void *>(this), m_arch.GetArchitectureName(), |
281 | 112k | m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(", |
282 | 112k | 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 | 112k | m_sections_up.reset(); |
289 | 112k | m_symfile_up.reset(); |
290 | 112k | m_objfile_sp.reset(); |
291 | 112k | } |
292 | | |
293 | | ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp, |
294 | | lldb::addr_t header_addr, Status &error, |
295 | 32 | size_t size_to_read) { |
296 | 32 | if (m_objfile_sp) { |
297 | 0 | error.SetErrorString("object file already exists"); |
298 | 32 | } else { |
299 | 32 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
300 | 32 | if (process_sp) { |
301 | 32 | m_did_load_objfile = true; |
302 | 32 | std::shared_ptr<DataBufferHeap> data_sp = |
303 | 32 | std::make_shared<DataBufferHeap>(size_to_read, 0); |
304 | 32 | Status readmem_error; |
305 | 32 | const size_t bytes_read = |
306 | 32 | process_sp->ReadMemory(header_addr, data_sp->GetBytes(), |
307 | 32 | data_sp->GetByteSize(), readmem_error); |
308 | 32 | if (bytes_read < size_to_read) |
309 | 4 | data_sp->SetByteSize(bytes_read); |
310 | 32 | if (data_sp->GetByteSize() > 0) { |
311 | 30 | m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, |
312 | 30 | header_addr, data_sp); |
313 | 30 | if (m_objfile_sp) { |
314 | 30 | StreamString s; |
315 | 30 | s.Printf("0x%16.16" PRIx64, header_addr); |
316 | 30 | 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 | 30 | 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 | 30 | m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture()); |
326 | 30 | } else { |
327 | 0 | error.SetErrorString("unable to find suitable object file plug-in"); |
328 | 0 | } |
329 | 30 | } else { |
330 | 2 | error.SetErrorStringWithFormat("unable to read header from memory: %s", |
331 | 2 | readmem_error.AsCString()); |
332 | 2 | } |
333 | 32 | } else { |
334 | 0 | error.SetErrorString("invalid process"); |
335 | 0 | } |
336 | 32 | } |
337 | 32 | return m_objfile_sp.get(); |
338 | 32 | } |
339 | | |
340 | 17.3M | const lldb_private::UUID &Module::GetUUID() { |
341 | 17.3M | if (!m_did_set_uuid.load()) { |
342 | 113k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
343 | 113k | if (!m_did_set_uuid.load()) { |
344 | 113k | ObjectFile *obj_file = GetObjectFile(); |
345 | | |
346 | 113k | if (obj_file != nullptr) { |
347 | 113k | m_uuid = obj_file->GetUUID(); |
348 | 113k | m_did_set_uuid = true; |
349 | 113k | } |
350 | 113k | } |
351 | 113k | } |
352 | 17.3M | return m_uuid; |
353 | 17.3M | } |
354 | | |
355 | 0 | void Module::SetUUID(const lldb_private::UUID &uuid) { |
356 | 0 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
357 | 0 | if (!m_did_set_uuid) { |
358 | 0 | m_uuid = uuid; |
359 | 0 | m_did_set_uuid = true; |
360 | 0 | } else { |
361 | 0 | lldbassert(0 && "Attempting to overwrite the existing module UUID"); |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | llvm::Expected<TypeSystemSP> |
366 | 540k | Module::GetTypeSystemForLanguage(LanguageType language) { |
367 | 540k | return m_type_system_map.GetTypeSystemForLanguage(language, this, true); |
368 | 540k | } |
369 | | |
370 | | void Module::ForEachTypeSystem( |
371 | 735 | llvm::function_ref<bool(lldb::TypeSystemSP)> callback) { |
372 | 735 | m_type_system_map.ForEach(callback); |
373 | 735 | } |
374 | | |
375 | 20 | void Module::ParseAllDebugSymbols() { |
376 | 20 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
377 | 20 | size_t num_comp_units = GetNumCompileUnits(); |
378 | 20 | if (num_comp_units == 0) |
379 | 6 | return; |
380 | | |
381 | 14 | SymbolFile *symbols = GetSymbolFile(); |
382 | | |
383 | 36 | for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++22 ) { |
384 | 22 | SymbolContext sc; |
385 | 22 | sc.module_sp = shared_from_this(); |
386 | 22 | sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); |
387 | 22 | if (!sc.comp_unit) |
388 | 0 | continue; |
389 | | |
390 | 22 | symbols->ParseVariablesForContext(sc); |
391 | | |
392 | 22 | symbols->ParseFunctions(*sc.comp_unit); |
393 | | |
394 | 22 | sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) { |
395 | 11 | symbols->ParseBlocksRecursive(*f); |
396 | | |
397 | | // Parse the variables for this function and all its blocks |
398 | 11 | sc.function = f.get(); |
399 | 11 | symbols->ParseVariablesForContext(sc); |
400 | 11 | return false; |
401 | 11 | }); |
402 | | |
403 | | // Parse all types for this compile unit |
404 | 22 | symbols->ParseTypes(*sc.comp_unit); |
405 | 22 | } |
406 | 14 | } |
407 | | |
408 | 744k | void Module::CalculateSymbolContext(SymbolContext *sc) { |
409 | 744k | sc->module_sp = shared_from_this(); |
410 | 744k | } |
411 | | |
412 | 12 | ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); } |
413 | | |
414 | 0 | void Module::DumpSymbolContext(Stream *s) { |
415 | 0 | s->Printf(", Module{%p}", static_cast<void *>(this)); |
416 | 0 | } |
417 | | |
418 | 214k | size_t Module::GetNumCompileUnits() { |
419 | 214k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
420 | 214k | if (SymbolFile *symbols = GetSymbolFile()) |
421 | 213k | return symbols->GetNumCompileUnits(); |
422 | 959 | return 0; |
423 | 214k | } |
424 | | |
425 | 8.31k | CompUnitSP Module::GetCompileUnitAtIndex(size_t index) { |
426 | 8.31k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
427 | 8.31k | size_t num_comp_units = GetNumCompileUnits(); |
428 | 8.31k | CompUnitSP cu_sp; |
429 | | |
430 | 8.31k | if (index < num_comp_units) { |
431 | 8.31k | if (SymbolFile *symbols = GetSymbolFile()) |
432 | 8.31k | cu_sp = symbols->GetCompileUnitAtIndex(index); |
433 | 8.31k | } |
434 | 8.31k | return cu_sp; |
435 | 8.31k | } |
436 | | |
437 | 10.1M | bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) { |
438 | 10.1M | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
439 | 10.1M | SectionList *section_list = GetSectionList(); |
440 | 10.1M | if (section_list) |
441 | 10.1M | return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list); |
442 | 13 | return false; |
443 | 10.1M | } |
444 | | |
445 | | uint32_t Module::ResolveSymbolContextForAddress( |
446 | | const Address &so_addr, lldb::SymbolContextItem resolve_scope, |
447 | 3.26M | SymbolContext &sc, bool resolve_tail_call_address) { |
448 | 3.26M | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
449 | 3.26M | 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 | 3.26M | sc.Clear(false); |
454 | | |
455 | | // Get the section from the section/offset address. |
456 | 3.26M | SectionSP section_sp(so_addr.GetSection()); |
457 | | |
458 | | // Make sure the section matches this module before we try and match anything |
459 | 3.26M | if (section_sp && section_sp->GetModule().get() == this3.26M ) { |
460 | | // If the section offset based address resolved itself, then this is the |
461 | | // right module. |
462 | 3.26M | sc.module_sp = shared_from_this(); |
463 | 3.26M | resolved_flags |= eSymbolContextModule; |
464 | | |
465 | 3.26M | SymbolFile *symfile = GetSymbolFile(); |
466 | 3.26M | if (!symfile) |
467 | 177 | return resolved_flags; |
468 | | |
469 | | // Resolve the compile unit, function, block, line table or line entry if |
470 | | // requested. |
471 | 3.26M | if (resolve_scope & eSymbolContextCompUnit || |
472 | 3.26M | resolve_scope & eSymbolContextFunction2.06M || |
473 | 3.26M | resolve_scope & eSymbolContextBlock201k || |
474 | 3.26M | resolve_scope & eSymbolContextLineEntry24.1k || |
475 | 3.26M | resolve_scope & eSymbolContextVariable16.0k ) { |
476 | 3.24M | symfile->SetLoadDebugInfoEnabled(); |
477 | 3.24M | resolved_flags |= |
478 | 3.24M | symfile->ResolveSymbolContext(so_addr, resolve_scope, sc); |
479 | 3.24M | } |
480 | | |
481 | | // Resolve the symbol if requested, but don't re-look it up if we've |
482 | | // already found it. |
483 | 3.26M | if (resolve_scope & eSymbolContextSymbol && |
484 | 3.26M | !(resolved_flags & eSymbolContextSymbol)2.87M ) { |
485 | 389k | Symtab *symtab = symfile->GetSymtab(); |
486 | 389k | if (symtab && so_addr.IsSectionOffset()) { |
487 | 389k | Symbol *matching_symbol = nullptr; |
488 | | |
489 | 389k | symtab->ForEachSymbolContainingFileAddress( |
490 | 389k | so_addr.GetFileAddress(), |
491 | 389k | [&matching_symbol](Symbol *symbol) -> bool { |
492 | 387k | if (symbol->GetType() != eSymbolTypeInvalid) { |
493 | 387k | matching_symbol = symbol; |
494 | 387k | return false; // Stop iterating |
495 | 387k | } |
496 | 107 | return true; // Keep iterating |
497 | 387k | }); |
498 | 389k | sc.symbol = matching_symbol; |
499 | 389k | if (!sc.symbol && resolve_scope & eSymbolContextFunction1.62k && |
500 | 389k | !(resolved_flags & eSymbolContextFunction)1.59k ) { |
501 | 1.57k | bool verify_unique = false; // No need to check again since |
502 | | // ResolveSymbolContext failed to find a |
503 | | // symbol at this address. |
504 | 1.57k | if (ObjectFile *obj_file = sc.module_sp->GetObjectFile()) |
505 | 1.57k | sc.symbol = |
506 | 1.57k | obj_file->ResolveSymbolForAddress(so_addr, verify_unique); |
507 | 1.57k | } |
508 | | |
509 | 389k | if (sc.symbol) { |
510 | 387k | 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 | 108 | ObjectFile *symtab_objfile = symtab->GetObjectFile(); |
517 | 108 | if (symtab_objfile && symtab_objfile->IsStripped()) { |
518 | 13 | ObjectFile *symfile_objfile = symfile->GetObjectFile(); |
519 | 13 | if (symfile_objfile != symtab_objfile) { |
520 | 11 | Symtab *symfile_symtab = symfile_objfile->GetSymtab(); |
521 | 11 | if (symfile_symtab) { |
522 | 11 | Symbol *symbol = |
523 | 11 | symfile_symtab->FindSymbolContainingFileAddress( |
524 | 11 | so_addr.GetFileAddress()); |
525 | 11 | if (symbol && !symbol->IsSynthetic()) { |
526 | 11 | sc.symbol = symbol; |
527 | 11 | } |
528 | 11 | } |
529 | 11 | } |
530 | 13 | } |
531 | 108 | } |
532 | 387k | resolved_flags |= eSymbolContextSymbol; |
533 | 387k | } |
534 | 389k | } |
535 | 389k | } |
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 | 3.26M | if (resolve_scope & eSymbolContextSymbol && |
541 | 3.26M | !(resolved_flags & eSymbolContextSymbol)2.87M && resolve_tail_call_address1.62k && |
542 | 3.26M | so_addr.IsSectionOffset()1 ) { |
543 | 1 | Address previous_addr = so_addr; |
544 | 1 | previous_addr.Slide(-1); |
545 | | |
546 | 1 | bool do_resolve_tail_call_address = false; // prevent recursion |
547 | 1 | const uint32_t flags = ResolveSymbolContextForAddress( |
548 | 1 | previous_addr, resolve_scope, sc, do_resolve_tail_call_address); |
549 | 1 | if (flags & eSymbolContextSymbol) { |
550 | 0 | AddressRange addr_range; |
551 | 0 | if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, |
552 | 0 | false, addr_range)) { |
553 | 0 | if (addr_range.GetBaseAddress().GetSection() == |
554 | 0 | 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 | 0 | if (so_addr.GetOffset() == |
560 | 0 | addr_range.GetBaseAddress().GetOffset() || |
561 | 0 | so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + |
562 | 0 | addr_range.GetByteSize()) { |
563 | 0 | resolved_flags |= flags; |
564 | 0 | } |
565 | 0 | } else { |
566 | 0 | sc.symbol = |
567 | 0 | nullptr; // Don't trust the symbol if the sections didn't match. |
568 | 0 | } |
569 | 0 | } |
570 | 0 | } |
571 | 1 | } |
572 | 3.26M | } |
573 | 3.26M | return resolved_flags; |
574 | 3.26M | } |
575 | | |
576 | | uint32_t Module::ResolveSymbolContextForFilePath( |
577 | | const char *file_path, uint32_t line, bool check_inlines, |
578 | 0 | lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { |
579 | 0 | FileSpec file_spec(file_path); |
580 | 0 | return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, |
581 | 0 | resolve_scope, sc_list); |
582 | 0 | } |
583 | | |
584 | | uint32_t Module::ResolveSymbolContextsForFileSpec( |
585 | | const FileSpec &file_spec, uint32_t line, bool check_inlines, |
586 | 227 | lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { |
587 | 227 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
588 | 227 | LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, " |
589 | 227 | "check_inlines = %s, resolve_scope = 0x%8.8x)", |
590 | 227 | file_spec.GetPath().c_str(), line, |
591 | 227 | check_inlines ? "yes" : "no", resolve_scope); |
592 | | |
593 | 227 | const uint32_t initial_count = sc_list.GetSize(); |
594 | | |
595 | 227 | if (SymbolFile *symbols = GetSymbolFile()) { |
596 | | // TODO: Handle SourceLocationSpec column information |
597 | 227 | SourceLocationSpec location_spec(file_spec, line, /*column=*/std::nullopt, |
598 | 227 | check_inlines, /*exact_match=*/false); |
599 | | |
600 | 227 | symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list); |
601 | 227 | } |
602 | | |
603 | 227 | return sc_list.GetSize() - initial_count; |
604 | 227 | } |
605 | | |
606 | | void Module::FindGlobalVariables(ConstString name, |
607 | | const CompilerDeclContext &parent_decl_ctx, |
608 | 122k | size_t max_matches, VariableList &variables) { |
609 | 122k | if (SymbolFile *symbols = GetSymbolFile()) |
610 | 122k | symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables); |
611 | 122k | } |
612 | | |
613 | | void Module::FindGlobalVariables(const RegularExpression ®ex, |
614 | 4 | size_t max_matches, VariableList &variables) { |
615 | 4 | SymbolFile *symbols = GetSymbolFile(); |
616 | 4 | if (symbols) |
617 | 4 | symbols->FindGlobalVariables(regex, max_matches, variables); |
618 | 4 | } |
619 | | |
620 | | void Module::FindCompileUnits(const FileSpec &path, |
621 | 756 | SymbolContextList &sc_list) { |
622 | 756 | const size_t num_compile_units = GetNumCompileUnits(); |
623 | 756 | SymbolContext sc; |
624 | 756 | sc.module_sp = shared_from_this(); |
625 | 786 | for (size_t i = 0; i < num_compile_units; ++i30 ) { |
626 | 30 | sc.comp_unit = GetCompileUnitAtIndex(i).get(); |
627 | 30 | if (sc.comp_unit) { |
628 | 30 | if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile())) |
629 | 18 | sc_list.Append(sc); |
630 | 30 | } |
631 | 30 | } |
632 | 756 | } |
633 | | |
634 | | Module::LookupInfo::LookupInfo(ConstString name, |
635 | | FunctionNameType name_type_mask, |
636 | | LanguageType language) |
637 | 613k | : m_name(name), m_lookup_name(), m_language(language) { |
638 | 613k | const char *name_cstr = name.GetCString(); |
639 | 613k | llvm::StringRef basename; |
640 | 613k | llvm::StringRef context; |
641 | | |
642 | 613k | if (name_type_mask & eFunctionNameTypeAuto) { |
643 | 18.3k | if (CPlusPlusLanguage::IsCPPMangledName(name_cstr)) |
644 | 117 | m_name_type_mask = eFunctionNameTypeFull; |
645 | 18.2k | else if ((language == eLanguageTypeUnknown || |
646 | 18.2k | Language::LanguageIsObjC(language)20 ) && |
647 | 18.2k | ObjCLanguage::IsPossibleObjCMethodName(name_cstr)18.2k ) |
648 | 13.5k | m_name_type_mask = eFunctionNameTypeFull; |
649 | 4.64k | else if (Language::LanguageIsC(language)) { |
650 | 8 | m_name_type_mask = eFunctionNameTypeFull; |
651 | 4.63k | } else { |
652 | 4.63k | if ((language == eLanguageTypeUnknown || |
653 | 4.63k | Language::LanguageIsObjC(language)12 ) && |
654 | 4.63k | ObjCLanguage::IsPossibleObjCSelector(name_cstr)4.62k ) |
655 | 3.49k | m_name_type_mask |= eFunctionNameTypeSelector; |
656 | | |
657 | 4.63k | CPlusPlusLanguage::MethodName cpp_method(name); |
658 | 4.63k | basename = cpp_method.GetBasename(); |
659 | 4.63k | if (basename.empty()) { |
660 | 4.60k | if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, |
661 | 4.60k | basename)) |
662 | 3.23k | m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); |
663 | 1.37k | else |
664 | 1.37k | m_name_type_mask |= eFunctionNameTypeFull; |
665 | 4.60k | } else { |
666 | 28 | m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); |
667 | 28 | } |
668 | 4.63k | } |
669 | 595k | } else { |
670 | 595k | m_name_type_mask = name_type_mask; |
671 | 595k | if (name_type_mask & eFunctionNameTypeMethod || |
672 | 595k | name_type_mask & eFunctionNameTypeBase595k ) { |
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 | 111k | CPlusPlusLanguage::MethodName cpp_method(name); |
676 | 111k | if (cpp_method.IsValid()) { |
677 | 0 | basename = cpp_method.GetBasename(); |
678 | |
|
679 | 0 | 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 | 0 | m_name_type_mask &= ~(eFunctionNameTypeBase); |
683 | 0 | if (m_name_type_mask == eFunctionNameTypeNone) |
684 | 0 | return; |
685 | 0 | } |
686 | 111k | } 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 | 111k | CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, |
691 | 111k | basename); |
692 | 111k | } |
693 | 111k | } |
694 | | |
695 | 595k | if (name_type_mask & eFunctionNameTypeSelector) { |
696 | 8.35k | if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) { |
697 | 0 | m_name_type_mask &= ~(eFunctionNameTypeSelector); |
698 | 0 | if (m_name_type_mask == eFunctionNameTypeNone) |
699 | 0 | return; |
700 | 0 | } |
701 | 8.35k | } |
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 | 595k | if (basename.empty()) { |
706 | 484k | if (name_type_mask & eFunctionNameTypeFull && |
707 | 484k | !CPlusPlusLanguage::IsCPPMangledName(name_cstr)475k ) { |
708 | 463k | CPlusPlusLanguage::MethodName cpp_method(name); |
709 | 463k | basename = cpp_method.GetBasename(); |
710 | 463k | if (basename.empty()) |
711 | 463k | CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, |
712 | 463k | basename); |
713 | 463k | } |
714 | 484k | } |
715 | 595k | } |
716 | | |
717 | 613k | 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 | 560k | m_lookup_name.SetString(basename); |
723 | 560k | m_match_name_after_lookup = true; |
724 | 560k | } 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 | 52.7k | m_lookup_name = name; |
728 | 52.7k | m_match_name_after_lookup = false; |
729 | 52.7k | } |
730 | 613k | } |
731 | | |
732 | | bool Module::LookupInfo::NameMatchesLookupInfo( |
733 | 23.9k | ConstString function_name, LanguageType language_type) const { |
734 | | // We always keep unnamed symbols |
735 | 23.9k | if (!function_name) |
736 | 3 | return true; |
737 | | |
738 | | // If we match exactly, we can return early |
739 | 23.9k | if (m_name == function_name) |
740 | 19.2k | 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 | 4.64k | const bool function_name_may_be_mangled = |
748 | 4.64k | Mangled::GetManglingScheme(function_name) != Mangled::eManglingSchemeNone; |
749 | 4.64k | ConstString demangled_function_name = function_name; |
750 | 4.64k | if (function_name_may_be_mangled) { |
751 | 873 | Mangled mangled_function_name(function_name); |
752 | 873 | demangled_function_name = mangled_function_name.GetDemangledName(); |
753 | 873 | } |
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 | 4.64k | if (Language *language = Language::FindPlugin(language_type)) |
759 | 3.76k | return language->DemangledNameContainsPath(m_name, demangled_function_name); |
760 | | |
761 | 881 | llvm::StringRef function_name_ref = demangled_function_name; |
762 | 881 | return function_name_ref.contains(m_name); |
763 | 4.64k | } |
764 | | |
765 | | void Module::LookupInfo::Prune(SymbolContextList &sc_list, |
766 | 26.2k | size_t start_idx) const { |
767 | 26.2k | if (m_match_name_after_lookup && m_name11.1k ) { |
768 | 11.1k | SymbolContext sc; |
769 | 11.1k | size_t i = start_idx; |
770 | 33.9k | while (i < sc_list.GetSize()) { |
771 | 22.7k | if (!sc_list.GetContextAtIndex(i, sc)) |
772 | 0 | break; |
773 | | |
774 | 22.7k | bool keep_it = |
775 | 22.7k | NameMatchesLookupInfo(sc.GetFunctionName(), sc.GetLanguage()); |
776 | 22.7k | if (keep_it) |
777 | 22.4k | ++i; |
778 | 374 | else |
779 | 374 | sc_list.RemoveContextAtIndex(i); |
780 | 22.7k | } |
781 | 11.1k | } |
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 | 26.2k | if (m_name_type_mask == eFunctionNameTypeFull) { |
788 | 16.6k | SymbolContext sc; |
789 | 16.6k | size_t i = start_idx; |
790 | 33.3k | while (i < sc_list.GetSize()) { |
791 | 16.6k | if (!sc_list.GetContextAtIndex(i, sc)) |
792 | 0 | break; |
793 | | // Make sure the mangled and demangled names don't match before we try to |
794 | | // pull anything out |
795 | 16.6k | ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled)); |
796 | 16.6k | ConstString full_name(sc.GetFunctionName()); |
797 | 16.6k | if (mangled_name != m_name && full_name != m_name1.94k ) { |
798 | 1.94k | CPlusPlusLanguage::MethodName cpp_method(full_name); |
799 | 1.94k | if (cpp_method.IsValid()) { |
800 | 1.93k | if (cpp_method.GetContext().empty()) { |
801 | 1.93k | if (cpp_method.GetBasename().compare(m_name) != 0) { |
802 | 0 | sc_list.RemoveContextAtIndex(i); |
803 | 0 | continue; |
804 | 0 | } |
805 | 1.93k | } else { |
806 | 0 | std::string qualified_name; |
807 | 0 | llvm::StringRef anon_prefix("(anonymous namespace)"); |
808 | 0 | if (cpp_method.GetContext() == anon_prefix) |
809 | 0 | qualified_name = cpp_method.GetBasename().str(); |
810 | 0 | else |
811 | 0 | qualified_name = cpp_method.GetScopeQualifiedName(); |
812 | 0 | if (qualified_name != m_name.GetCString()) { |
813 | 0 | sc_list.RemoveContextAtIndex(i); |
814 | 0 | continue; |
815 | 0 | } |
816 | 0 | } |
817 | 1.93k | } |
818 | 1.94k | } |
819 | 16.6k | ++i; |
820 | 16.6k | } |
821 | 16.6k | } |
822 | 26.2k | } |
823 | | |
824 | | void Module::FindFunctions(const Module::LookupInfo &lookup_info, |
825 | | const CompilerDeclContext &parent_decl_ctx, |
826 | | const ModuleFunctionSearchOptions &options, |
827 | 2.73M | SymbolContextList &sc_list) { |
828 | | // Find all the functions (not symbols, but debug information functions... |
829 | 2.73M | if (SymbolFile *symbols = GetSymbolFile()) { |
830 | 2.70M | symbols->FindFunctions(lookup_info, parent_decl_ctx, |
831 | 2.70M | options.include_inlines, sc_list); |
832 | | // Now check our symbol table for symbols that are code symbols if |
833 | | // requested |
834 | 2.70M | if (options.include_symbols) { |
835 | 2.67M | if (Symtab *symtab = symbols->GetSymtab()) { |
836 | 2.67M | symtab->FindFunctionSymbols(lookup_info.GetLookupName(), |
837 | 2.67M | lookup_info.GetNameTypeMask(), sc_list); |
838 | 2.67M | } |
839 | 2.67M | } |
840 | 2.70M | } |
841 | 2.73M | } |
842 | | |
843 | | void Module::FindFunctions(ConstString name, |
844 | | const CompilerDeclContext &parent_decl_ctx, |
845 | | FunctionNameType name_type_mask, |
846 | | const ModuleFunctionSearchOptions &options, |
847 | 585k | SymbolContextList &sc_list) { |
848 | 585k | const size_t old_size = sc_list.GetSize(); |
849 | 585k | LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); |
850 | 585k | FindFunctions(lookup_info, parent_decl_ctx, options, sc_list); |
851 | 585k | if (name_type_mask & eFunctionNameTypeAuto) { |
852 | 340 | const size_t new_size = sc_list.GetSize(); |
853 | 340 | if (old_size < new_size) |
854 | 54 | lookup_info.Prune(sc_list, old_size); |
855 | 340 | } |
856 | 585k | } |
857 | | |
858 | | void Module::FindFunctions(const RegularExpression ®ex, |
859 | | const ModuleFunctionSearchOptions &options, |
860 | 464 | SymbolContextList &sc_list) { |
861 | 464 | const size_t start_size = sc_list.GetSize(); |
862 | | |
863 | 464 | if (SymbolFile *symbols = GetSymbolFile()) { |
864 | 464 | symbols->FindFunctions(regex, options.include_inlines, sc_list); |
865 | | |
866 | | // Now check our symbol table for symbols that are code symbols if |
867 | | // requested |
868 | 464 | if (options.include_symbols) { |
869 | 216 | Symtab *symtab = symbols->GetSymtab(); |
870 | 216 | if (symtab) { |
871 | 216 | std::vector<uint32_t> symbol_indexes; |
872 | 216 | symtab->AppendSymbolIndexesMatchingRegExAndType( |
873 | 216 | regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, |
874 | 216 | symbol_indexes); |
875 | 216 | const size_t num_matches = symbol_indexes.size(); |
876 | 216 | if (num_matches) { |
877 | 16 | SymbolContext sc(this); |
878 | 16 | const size_t end_functions_added_index = sc_list.GetSize(); |
879 | 16 | size_t num_functions_added_to_sc_list = |
880 | 16 | end_functions_added_index - start_size; |
881 | 16 | if (num_functions_added_to_sc_list == 0) { |
882 | | // No functions were added, just symbols, so we can just append |
883 | | // them |
884 | 5 | for (size_t i = 0; i < num_matches; ++i3 ) { |
885 | 3 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
886 | 3 | SymbolType sym_type = sc.symbol->GetType(); |
887 | 3 | if (sc.symbol && (sym_type == eSymbolTypeCode || |
888 | 3 | sym_type == eSymbolTypeResolver1 )) |
889 | 2 | sc_list.Append(sc); |
890 | 3 | } |
891 | 14 | } else { |
892 | 14 | typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap; |
893 | 14 | FileAddrToIndexMap file_addr_to_index; |
894 | 41 | for (size_t i = start_size; i < end_functions_added_index; ++i27 ) { |
895 | 27 | const SymbolContext &sc = sc_list[i]; |
896 | 27 | if (sc.block) |
897 | 0 | continue; |
898 | 27 | file_addr_to_index[sc.function->GetAddressRange() |
899 | 27 | .GetBaseAddress() |
900 | 27 | .GetFileAddress()] = i; |
901 | 27 | } |
902 | | |
903 | 14 | FileAddrToIndexMap::const_iterator end = file_addr_to_index.end(); |
904 | | // Functions were added so we need to merge symbols into any |
905 | | // existing function symbol contexts |
906 | 42 | for (size_t i = start_size; i < num_matches; ++i28 ) { |
907 | 28 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
908 | 28 | SymbolType sym_type = sc.symbol->GetType(); |
909 | 28 | if (sc.symbol && sc.symbol->ValueIsAddress() && |
910 | 28 | (sym_type == eSymbolTypeCode || |
911 | 28 | sym_type == eSymbolTypeResolver1 )) { |
912 | 27 | FileAddrToIndexMap::const_iterator pos = |
913 | 27 | file_addr_to_index.find( |
914 | 27 | sc.symbol->GetAddressRef().GetFileAddress()); |
915 | 27 | if (pos == end) |
916 | 0 | sc_list.Append(sc); |
917 | 27 | else |
918 | 27 | sc_list[pos->second].symbol = sc.symbol; |
919 | 27 | } |
920 | 28 | } |
921 | 14 | } |
922 | 16 | } |
923 | 216 | } |
924 | 216 | } |
925 | 464 | } |
926 | 464 | } |
927 | | |
928 | | void Module::FindAddressesForLine(const lldb::TargetSP target_sp, |
929 | | const FileSpec &file, uint32_t line, |
930 | | Function *function, |
931 | | std::vector<Address> &output_local, |
932 | 904 | std::vector<Address> &output_extern) { |
933 | 904 | SearchFilterByModule filter(target_sp, m_file); |
934 | | |
935 | | // TODO: Handle SourceLocationSpec column information |
936 | 904 | SourceLocationSpec location_spec(file, line, /*column=*/std::nullopt, |
937 | 904 | /*check_inlines=*/true, |
938 | 904 | /*exact_match=*/false); |
939 | 904 | AddressResolverFileLine resolver(location_spec); |
940 | 904 | resolver.ResolveAddress(filter); |
941 | | |
942 | 978 | for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++74 ) { |
943 | 74 | Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress(); |
944 | 74 | Function *f = addr.CalculateSymbolContextFunction(); |
945 | 74 | if (f && f == function) |
946 | 48 | output_local.push_back(addr); |
947 | 26 | else |
948 | 26 | output_extern.push_back(addr); |
949 | 74 | } |
950 | 904 | } |
951 | | |
952 | | void Module::FindTypes_Impl( |
953 | | ConstString name, const CompilerDeclContext &parent_decl_ctx, |
954 | | size_t max_matches, |
955 | | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
956 | 418k | TypeMap &types) { |
957 | 418k | if (SymbolFile *symbols = GetSymbolFile()) |
958 | 417k | symbols->FindTypes(name, parent_decl_ctx, max_matches, |
959 | 417k | searched_symbol_files, types); |
960 | 418k | } |
961 | | |
962 | | void Module::FindTypesInNamespace(ConstString type_name, |
963 | | const CompilerDeclContext &parent_decl_ctx, |
964 | 455 | size_t max_matches, TypeList &type_list) { |
965 | 455 | TypeMap types_map; |
966 | 455 | llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; |
967 | 455 | FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files, |
968 | 455 | types_map); |
969 | 455 | if (types_map.GetSize()) { |
970 | 100 | SymbolContext sc; |
971 | 100 | sc.module_sp = shared_from_this(); |
972 | 100 | sc.SortTypeList(types_map, type_list); |
973 | 100 | } |
974 | 455 | } |
975 | | |
976 | | lldb::TypeSP Module::FindFirstType(const SymbolContext &sc, ConstString name, |
977 | 241 | bool exact_match) { |
978 | 241 | TypeList type_list; |
979 | 241 | llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; |
980 | 241 | FindTypes(name, exact_match, 1, searched_symbol_files, type_list); |
981 | 241 | if (type_list.GetSize()) |
982 | 31 | return type_list.GetTypeAtIndex(0); |
983 | 210 | return TypeSP(); |
984 | 241 | } |
985 | | |
986 | | void Module::FindTypes( |
987 | | ConstString name, bool exact_match, size_t max_matches, |
988 | | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
989 | 418k | TypeList &types) { |
990 | 418k | const char *type_name_cstr = name.GetCString(); |
991 | 418k | llvm::StringRef type_scope; |
992 | 418k | llvm::StringRef type_basename; |
993 | 418k | TypeClass type_class = eTypeClassAny; |
994 | 418k | TypeMap typesmap; |
995 | | |
996 | 418k | if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename, |
997 | 418k | type_class)) { |
998 | | // Check if "name" starts with "::" which means the qualified type starts |
999 | | // from the root namespace and implies and exact match. The typenames we |
1000 | | // get back from clang do not start with "::" so we need to strip this off |
1001 | | // in order to get the qualified names to match |
1002 | 1.09k | exact_match = type_scope.consume_front("::"); |
1003 | | |
1004 | 1.09k | ConstString type_basename_const_str(type_basename); |
1005 | 1.09k | FindTypes_Impl(type_basename_const_str, CompilerDeclContext(), max_matches, |
1006 | 1.09k | searched_symbol_files, typesmap); |
1007 | 1.09k | if (typesmap.GetSize()) |
1008 | 60 | typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class, |
1009 | 60 | exact_match); |
1010 | 416k | } else { |
1011 | | // The type is not in a namespace/class scope, just search for it by |
1012 | | // basename |
1013 | 416k | if (type_class != eTypeClassAny && !type_basename.empty()126 ) { |
1014 | | // The "type_name_cstr" will have been modified if we have a valid type |
1015 | | // class prefix (like "struct", "class", "union", "typedef" etc). |
1016 | 126 | FindTypes_Impl(ConstString(type_basename), CompilerDeclContext(), |
1017 | 126 | UINT_MAX, searched_symbol_files, typesmap); |
1018 | 126 | typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class, |
1019 | 126 | exact_match); |
1020 | 416k | } else { |
1021 | 416k | FindTypes_Impl(name, CompilerDeclContext(), UINT_MAX, |
1022 | 416k | searched_symbol_files, typesmap); |
1023 | 416k | if (exact_match) { |
1024 | 400k | typesmap.RemoveMismatchedTypes(type_scope, name, type_class, |
1025 | 400k | exact_match); |
1026 | 400k | } |
1027 | 416k | } |
1028 | 416k | } |
1029 | 418k | if (typesmap.GetSize()) { |
1030 | 909 | SymbolContext sc; |
1031 | 909 | sc.module_sp = shared_from_this(); |
1032 | 909 | sc.SortTypeList(typesmap, types); |
1033 | 909 | } |
1034 | 418k | } |
1035 | | |
1036 | | void Module::FindTypes( |
1037 | | llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, |
1038 | | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
1039 | 148 | TypeMap &types) { |
1040 | | // If a scoped timer is needed, place it in a SymbolFile::FindTypes override. |
1041 | | // A timer here is too high volume for some cases, for example when calling |
1042 | | // FindTypes on each object file. |
1043 | 148 | if (SymbolFile *symbols = GetSymbolFile()) |
1044 | 148 | symbols->FindTypes(pattern, languages, searched_symbol_files, types); |
1045 | 148 | } |
1046 | | |
1047 | | static Debugger::DebuggerList |
1048 | 116k | DebuggersOwningModuleRequestingInterruption(Module &module) { |
1049 | 116k | Debugger::DebuggerList requestors |
1050 | 116k | = Debugger::DebuggersRequestingInterruption(); |
1051 | 116k | Debugger::DebuggerList interruptors; |
1052 | 116k | if (requestors.empty()) |
1053 | 116k | return interruptors; |
1054 | | |
1055 | 0 | for (auto debugger_sp : requestors) { |
1056 | 0 | if (!debugger_sp->InterruptRequested()) |
1057 | 0 | continue; |
1058 | 0 | if (debugger_sp->GetTargetList() |
1059 | 0 | .AnyTargetContainsModule(module)) |
1060 | 0 | interruptors.push_back(debugger_sp); |
1061 | 0 | } |
1062 | 0 | return interruptors; |
1063 | 116k | } |
1064 | | |
1065 | 11.1M | SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) { |
1066 | 11.1M | if (!m_did_load_symfile.load()) { |
1067 | 116k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1068 | 116k | if (!m_did_load_symfile.load() && can_create116k ) { |
1069 | 116k | Debugger::DebuggerList interruptors |
1070 | 116k | = DebuggersOwningModuleRequestingInterruption(*this); |
1071 | 116k | if (!interruptors.empty()) { |
1072 | 0 | for (auto debugger_sp : interruptors) { |
1073 | 0 | REPORT_INTERRUPTION(*(debugger_sp.get()), |
1074 | 0 | "Interrupted fetching symbols for module {0}", |
1075 | 0 | this->GetFileSpec()); |
1076 | 0 | } |
1077 | 0 | return nullptr; |
1078 | 0 | } |
1079 | 116k | ObjectFile *obj_file = GetObjectFile(); |
1080 | 116k | if (obj_file != nullptr) { |
1081 | 116k | LLDB_SCOPED_TIMER(); |
1082 | 116k | m_symfile_up.reset( |
1083 | 116k | SymbolVendor::FindPlugin(shared_from_this(), feedback_strm)); |
1084 | 116k | m_did_load_symfile = true; |
1085 | 116k | } |
1086 | 116k | } |
1087 | 116k | } |
1088 | 11.1M | return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr0 ; |
1089 | 11.1M | } |
1090 | | |
1091 | 2.85M | Symtab *Module::GetSymtab() { |
1092 | 2.85M | if (SymbolFile *symbols = GetSymbolFile()) |
1093 | 2.82M | return symbols->GetSymtab(); |
1094 | 38.4k | return nullptr; |
1095 | 2.85M | } |
1096 | | |
1097 | | void Module::SetFileSpecAndObjectName(const FileSpec &file, |
1098 | 4.14k | ConstString object_name) { |
1099 | | // Container objects whose paths do not specify a file directly can call this |
1100 | | // function to correct the file and object names. |
1101 | 4.14k | m_file = file; |
1102 | 4.14k | m_mod_time = FileSystem::Instance().GetModificationTime(file); |
1103 | 4.14k | m_object_name = object_name; |
1104 | 4.14k | } |
1105 | | |
1106 | 599k | const ArchSpec &Module::GetArchitecture() const { return m_arch; } |
1107 | | |
1108 | 217 | std::string Module::GetSpecificationDescription() const { |
1109 | 217 | std::string spec(GetFileSpec().GetPath()); |
1110 | 217 | if (m_object_name) { |
1111 | 0 | spec += '('; |
1112 | 0 | spec += m_object_name.GetCString(); |
1113 | 0 | spec += ')'; |
1114 | 0 | } |
1115 | 217 | return spec; |
1116 | 217 | } |
1117 | | |
1118 | | void Module::GetDescription(llvm::raw_ostream &s, |
1119 | 11.2k | lldb::DescriptionLevel level) { |
1120 | 11.2k | if (level >= eDescriptionLevelFull) { |
1121 | 513 | if (m_arch.IsValid()) |
1122 | 513 | s << llvm::formatv("({0}) ", m_arch.GetArchitectureName()); |
1123 | 513 | } |
1124 | | |
1125 | 11.2k | if (level == eDescriptionLevelBrief) { |
1126 | 10.7k | const char *filename = m_file.GetFilename().GetCString(); |
1127 | 10.7k | if (filename) |
1128 | 10.7k | s << filename; |
1129 | 10.7k | } else { |
1130 | 513 | char path[PATH_MAX]; |
1131 | 513 | if (m_file.GetPath(path, sizeof(path))) |
1132 | 513 | s << path; |
1133 | 513 | } |
1134 | | |
1135 | 11.2k | const char *object_name = m_object_name.GetCString(); |
1136 | 11.2k | if (object_name) |
1137 | 69 | s << llvm::formatv("({0})", object_name); |
1138 | 11.2k | } |
1139 | | |
1140 | 204k | bool Module::FileHasChanged() const { |
1141 | | // We have provided the DataBuffer for this module to avoid accessing the |
1142 | | // filesystem. We never want to reload those files. |
1143 | 204k | if (m_data_sp) |
1144 | 188k | return false; |
1145 | 16.1k | if (!m_file_has_changed) |
1146 | 16.1k | m_file_has_changed = |
1147 | 16.1k | (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time); |
1148 | 16.1k | return m_file_has_changed; |
1149 | 204k | } |
1150 | | |
1151 | | void Module::ReportWarningOptimization( |
1152 | 635 | std::optional<lldb::user_id_t> debugger_id) { |
1153 | 635 | ConstString file_name = GetFileSpec().GetFilename(); |
1154 | 635 | if (file_name.IsEmpty()) |
1155 | 0 | return; |
1156 | | |
1157 | 635 | StreamString ss; |
1158 | 635 | ss << file_name |
1159 | 635 | << " was compiled with optimization - stepping may behave " |
1160 | 635 | "oddly; variables may not be available."; |
1161 | 635 | Debugger::ReportWarning(std::string(ss.GetString()), debugger_id, |
1162 | 635 | &m_optimization_warning); |
1163 | 635 | } |
1164 | | |
1165 | | void Module::ReportWarningUnsupportedLanguage( |
1166 | 3 | LanguageType language, std::optional<lldb::user_id_t> debugger_id) { |
1167 | 3 | StreamString ss; |
1168 | 3 | ss << "This version of LLDB has no plugin for the language \"" |
1169 | 3 | << Language::GetNameForLanguageType(language) |
1170 | 3 | << "\". " |
1171 | 3 | "Inspection of frame variables will be limited."; |
1172 | 3 | Debugger::ReportWarning(std::string(ss.GetString()), debugger_id, |
1173 | 3 | &m_language_warning); |
1174 | 3 | } |
1175 | | |
1176 | | void Module::ReportErrorIfModifyDetected( |
1177 | 0 | const llvm::formatv_object_base &payload) { |
1178 | 0 | if (!m_first_file_changed_log) { |
1179 | 0 | if (FileHasChanged()) { |
1180 | 0 | m_first_file_changed_log = true; |
1181 | 0 | StreamString strm; |
1182 | 0 | strm.PutCString("the object file "); |
1183 | 0 | GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); |
1184 | 0 | strm.PutCString(" has been modified\n"); |
1185 | 0 | strm.PutCString(payload.str()); |
1186 | 0 | strm.PutCString("The debug session should be aborted as the original " |
1187 | 0 | "debug information has been overwritten."); |
1188 | 0 | Debugger::ReportError(std::string(strm.GetString())); |
1189 | 0 | } |
1190 | 0 | } |
1191 | 0 | } |
1192 | | |
1193 | 22 | void Module::ReportError(const llvm::formatv_object_base &payload) { |
1194 | 22 | StreamString strm; |
1195 | 22 | GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief); |
1196 | 22 | strm.PutChar(' '); |
1197 | 22 | strm.PutCString(payload.str()); |
1198 | 22 | Debugger::ReportError(strm.GetString().str()); |
1199 | 22 | } |
1200 | | |
1201 | 12 | void Module::ReportWarning(const llvm::formatv_object_base &payload) { |
1202 | 12 | StreamString strm; |
1203 | 12 | GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); |
1204 | 12 | strm.PutChar(' '); |
1205 | 12 | strm.PutCString(payload.str()); |
1206 | 12 | Debugger::ReportWarning(std::string(strm.GetString())); |
1207 | 12 | } |
1208 | | |
1209 | 31 | void Module::LogMessage(Log *log, const llvm::formatv_object_base &payload) { |
1210 | 31 | StreamString log_message; |
1211 | 31 | GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); |
1212 | 31 | log_message.PutCString(": "); |
1213 | 31 | log_message.PutCString(payload.str()); |
1214 | 31 | log->PutCString(log_message.GetData()); |
1215 | 31 | } |
1216 | | |
1217 | | void Module::LogMessageVerboseBacktrace( |
1218 | 0 | Log *log, const llvm::formatv_object_base &payload) { |
1219 | 0 | StreamString log_message; |
1220 | 0 | GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); |
1221 | 0 | log_message.PutCString(": "); |
1222 | 0 | log_message.PutCString(payload.str()); |
1223 | 0 | if (log->GetVerbose()) { |
1224 | 0 | std::string back_trace; |
1225 | 0 | llvm::raw_string_ostream stream(back_trace); |
1226 | 0 | llvm::sys::PrintStackTrace(stream); |
1227 | 0 | log_message.PutCString(back_trace); |
1228 | 0 | } |
1229 | 0 | log->PutCString(log_message.GetData()); |
1230 | 0 | } |
1231 | | |
1232 | 98 | void Module::Dump(Stream *s) { |
1233 | 98 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1234 | | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
1235 | 98 | s->Indent(); |
1236 | 98 | s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(), |
1237 | 98 | m_object_name ? "("0 : "", |
1238 | 98 | m_object_name ? m_object_name.GetCString()0 : "", |
1239 | 98 | m_object_name ? ")"0 : ""); |
1240 | | |
1241 | 98 | s->IndentMore(); |
1242 | | |
1243 | 98 | ObjectFile *objfile = GetObjectFile(); |
1244 | 98 | if (objfile) |
1245 | 98 | objfile->Dump(s); |
1246 | | |
1247 | 98 | if (SymbolFile *symbols = GetSymbolFile()) |
1248 | 98 | symbols->Dump(*s); |
1249 | | |
1250 | 98 | s->IndentLess(); |
1251 | 98 | } |
1252 | | |
1253 | 13.9k | ConstString Module::GetObjectName() const { return m_object_name; } |
1254 | | |
1255 | 10.0M | ObjectFile *Module::GetObjectFile() { |
1256 | 10.0M | if (!m_did_load_objfile.load()) { |
1257 | 114k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1258 | 114k | if (!m_did_load_objfile.load()) { |
1259 | 114k | LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s", |
1260 | 114k | GetFileSpec().GetFilename().AsCString("")); |
1261 | 114k | lldb::offset_t data_offset = 0; |
1262 | 114k | lldb::offset_t file_size = 0; |
1263 | | |
1264 | 114k | if (m_data_sp) |
1265 | 102k | file_size = m_data_sp->GetByteSize(); |
1266 | 11.2k | else if (m_file) |
1267 | 10.2k | file_size = FileSystem::Instance().GetByteSize(m_file); |
1268 | | |
1269 | 114k | if (file_size > m_object_offset) { |
1270 | 112k | m_did_load_objfile = true; |
1271 | | // FindPlugin will modify its data_sp argument. Do not let it |
1272 | | // modify our m_data_sp member. |
1273 | 112k | auto data_sp = m_data_sp; |
1274 | 112k | m_objfile_sp = ObjectFile::FindPlugin( |
1275 | 112k | shared_from_this(), &m_file, m_object_offset, |
1276 | 112k | file_size - m_object_offset, data_sp, data_offset); |
1277 | 112k | if (m_objfile_sp) { |
1278 | | // Once we get the object file, update our module with the object |
1279 | | // file's architecture since it might differ in vendor/os if some |
1280 | | // parts were unknown. But since the matching arch might already be |
1281 | | // more specific than the generic COFF architecture, only merge in |
1282 | | // those values that overwrite unspecified unknown values. |
1283 | 112k | m_arch.MergeFrom(m_objfile_sp->GetArchitecture()); |
1284 | 112k | } else { |
1285 | 2 | ReportError("failed to load objfile for {0}\nDebugging will be " |
1286 | 2 | "degraded for this module.", |
1287 | 2 | GetFileSpec().GetPath().c_str()); |
1288 | 2 | } |
1289 | 112k | } |
1290 | 114k | } |
1291 | 114k | } |
1292 | 10.0M | return m_objfile_sp.get(); |
1293 | 10.0M | } |
1294 | | |
1295 | 10.5M | SectionList *Module::GetSectionList() { |
1296 | | // Populate m_sections_up with sections from objfile. |
1297 | 10.5M | if (!m_sections_up) { |
1298 | 110k | ObjectFile *obj_file = GetObjectFile(); |
1299 | 110k | if (obj_file != nullptr) |
1300 | 110k | obj_file->CreateSections(*GetUnifiedSectionList()); |
1301 | 110k | } |
1302 | 10.5M | return m_sections_up.get(); |
1303 | 10.5M | } |
1304 | | |
1305 | 0 | void Module::SectionFileAddressesChanged() { |
1306 | 0 | ObjectFile *obj_file = GetObjectFile(); |
1307 | 0 | if (obj_file) |
1308 | 0 | obj_file->SectionFileAddressesChanged(); |
1309 | 0 | if (SymbolFile *symbols = GetSymbolFile()) |
1310 | 0 | symbols->SectionFileAddressesChanged(); |
1311 | 0 | } |
1312 | | |
1313 | 243k | UnwindTable &Module::GetUnwindTable() { |
1314 | 243k | if (!m_unwind_table) { |
1315 | 6.01k | m_unwind_table.emplace(*this); |
1316 | 6.01k | if (!m_symfile_spec) |
1317 | 4.92k | Symbols::DownloadSymbolFileAsync(GetUUID()); |
1318 | 6.01k | } |
1319 | 243k | return *m_unwind_table; |
1320 | 243k | } |
1321 | | |
1322 | 117k | SectionList *Module::GetUnifiedSectionList() { |
1323 | 117k | if (!m_sections_up) |
1324 | 116k | m_sections_up = std::make_unique<SectionList>(); |
1325 | 117k | return m_sections_up.get(); |
1326 | 117k | } |
1327 | | |
1328 | | const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name, |
1329 | 113k | SymbolType symbol_type) { |
1330 | 113k | LLDB_SCOPED_TIMERF( |
1331 | 113k | "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", |
1332 | 113k | name.AsCString(), symbol_type); |
1333 | 113k | if (Symtab *symtab = GetSymtab()) |
1334 | 113k | return symtab->FindFirstSymbolWithNameAndType( |
1335 | 113k | name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); |
1336 | 140 | return nullptr; |
1337 | 113k | } |
1338 | | void Module::SymbolIndicesToSymbolContextList( |
1339 | | Symtab *symtab, std::vector<uint32_t> &symbol_indexes, |
1340 | 519k | SymbolContextList &sc_list) { |
1341 | | // No need to protect this call using m_mutex all other method calls are |
1342 | | // already thread safe. |
1343 | | |
1344 | 519k | size_t num_indices = symbol_indexes.size(); |
1345 | 519k | if (num_indices > 0) { |
1346 | 5.78k | SymbolContext sc; |
1347 | 5.78k | CalculateSymbolContext(&sc); |
1348 | 12.3k | for (size_t i = 0; i < num_indices; i++6.56k ) { |
1349 | 6.56k | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
1350 | 6.56k | if (sc.symbol) |
1351 | 6.56k | sc_list.Append(sc); |
1352 | 6.56k | } |
1353 | 5.78k | } |
1354 | 519k | } |
1355 | | |
1356 | | void Module::FindFunctionSymbols(ConstString name, uint32_t name_type_mask, |
1357 | 6.31k | SymbolContextList &sc_list) { |
1358 | 6.31k | LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)", |
1359 | 6.31k | name.AsCString(), name_type_mask); |
1360 | 6.31k | if (Symtab *symtab = GetSymtab()) |
1361 | 6.31k | symtab->FindFunctionSymbols(name, name_type_mask, sc_list); |
1362 | 6.31k | } |
1363 | | |
1364 | | void Module::FindSymbolsWithNameAndType(ConstString name, |
1365 | | SymbolType symbol_type, |
1366 | 522k | SymbolContextList &sc_list) { |
1367 | | // No need to protect this call using m_mutex all other method calls are |
1368 | | // already thread safe. |
1369 | 522k | if (Symtab *symtab = GetSymtab()) { |
1370 | 519k | std::vector<uint32_t> symbol_indexes; |
1371 | 519k | symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes); |
1372 | 519k | SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); |
1373 | 519k | } |
1374 | 522k | } |
1375 | | |
1376 | | void Module::FindSymbolsMatchingRegExAndType( |
1377 | | const RegularExpression ®ex, SymbolType symbol_type, |
1378 | 0 | SymbolContextList &sc_list, Mangled::NamePreference mangling_preference) { |
1379 | | // No need to protect this call using m_mutex all other method calls are |
1380 | | // already thread safe. |
1381 | 0 | LLDB_SCOPED_TIMERF( |
1382 | 0 | "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", |
1383 | 0 | regex.GetText().str().c_str(), symbol_type); |
1384 | 0 | if (Symtab *symtab = GetSymtab()) { |
1385 | 0 | std::vector<uint32_t> symbol_indexes; |
1386 | 0 | symtab->FindAllSymbolsMatchingRexExAndType( |
1387 | 0 | regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, |
1388 | 0 | symbol_indexes, mangling_preference); |
1389 | 0 | SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); |
1390 | 0 | } |
1391 | 0 | } |
1392 | | |
1393 | 215k | void Module::PreloadSymbols() { |
1394 | 215k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1395 | 215k | SymbolFile *sym_file = GetSymbolFile(); |
1396 | 215k | if (!sym_file) |
1397 | 1.08k | return; |
1398 | | |
1399 | | // Load the object file symbol table and any symbols from the SymbolFile that |
1400 | | // get appended using SymbolFile::AddSymbols(...). |
1401 | 214k | if (Symtab *symtab = sym_file->GetSymtab()) |
1402 | 214k | symtab->PreloadSymbols(); |
1403 | | |
1404 | | // Now let the symbol file preload its data and the symbol table will be |
1405 | | // available without needing to take the module lock. |
1406 | 214k | sym_file->PreloadSymbols(); |
1407 | 214k | } |
1408 | | |
1409 | 1.47k | void Module::SetSymbolFileFileSpec(const FileSpec &file) { |
1410 | 1.47k | if (!FileSystem::Instance().Exists(file)) |
1411 | 1 | return; |
1412 | 1.47k | if (m_symfile_up) { |
1413 | | // Remove any sections in the unified section list that come from the |
1414 | | // current symbol vendor. |
1415 | 23 | SectionList *section_list = GetSectionList(); |
1416 | 23 | SymbolFile *symbol_file = GetSymbolFile(); |
1417 | 23 | if (section_list && symbol_file) { |
1418 | 16 | ObjectFile *obj_file = symbol_file->GetObjectFile(); |
1419 | | // Make sure we have an object file and that the symbol vendor's objfile |
1420 | | // isn't the same as the module's objfile before we remove any sections |
1421 | | // for it... |
1422 | 16 | if (obj_file) { |
1423 | | // Check to make sure we aren't trying to specify the file we already |
1424 | | // have |
1425 | 16 | if (obj_file->GetFileSpec() == file) { |
1426 | | // We are being told to add the exact same file that we already have |
1427 | | // we don't have to do anything. |
1428 | 2 | return; |
1429 | 2 | } |
1430 | | |
1431 | | // Cleare the current symtab as we are going to replace it with a new |
1432 | | // one |
1433 | 14 | obj_file->ClearSymtab(); |
1434 | | |
1435 | | // Clear the unwind table too, as that may also be affected by the |
1436 | | // symbol file information. |
1437 | 14 | m_unwind_table.reset(); |
1438 | | |
1439 | | // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") |
1440 | | // instead of a full path to the symbol file within the bundle |
1441 | | // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to |
1442 | | // check this |
1443 | | |
1444 | 14 | if (FileSystem::Instance().IsDirectory(file)) { |
1445 | 0 | std::string new_path(file.GetPath()); |
1446 | 0 | std::string old_path(obj_file->GetFileSpec().GetPath()); |
1447 | 0 | if (llvm::StringRef(old_path).startswith(new_path)) { |
1448 | | // We specified the same bundle as the symbol file that we already |
1449 | | // have |
1450 | 0 | return; |
1451 | 0 | } |
1452 | 0 | } |
1453 | | |
1454 | 14 | if (obj_file != m_objfile_sp.get()) { |
1455 | 0 | size_t num_sections = section_list->GetNumSections(0); |
1456 | 0 | for (size_t idx = num_sections; idx > 0; --idx) { |
1457 | 0 | lldb::SectionSP section_sp( |
1458 | 0 | section_list->GetSectionAtIndex(idx - 1)); |
1459 | 0 | if (section_sp->GetObjectFile() == obj_file) { |
1460 | 0 | section_list->DeleteSection(idx - 1); |
1461 | 0 | } |
1462 | 0 | } |
1463 | 0 | } |
1464 | 14 | } |
1465 | 16 | } |
1466 | | // Keep all old symbol files around in case there are any lingering type |
1467 | | // references in any SBValue objects that might have been handed out. |
1468 | 21 | m_old_symfiles.push_back(std::move(m_symfile_up)); |
1469 | 21 | } |
1470 | 1.46k | m_symfile_spec = file; |
1471 | 1.46k | m_symfile_up.reset(); |
1472 | 1.46k | m_did_load_symfile = false; |
1473 | 1.46k | } |
1474 | | |
1475 | 419k | bool Module::IsExecutable() { |
1476 | 419k | if (GetObjectFile() == nullptr) |
1477 | 0 | return false; |
1478 | 419k | else |
1479 | 419k | return GetObjectFile()->IsExecutable(); |
1480 | 419k | } |
1481 | | |
1482 | 2.14k | bool Module::IsLoadedInTarget(Target *target) { |
1483 | 2.14k | ObjectFile *obj_file = GetObjectFile(); |
1484 | 2.14k | if (obj_file) { |
1485 | 2.14k | SectionList *sections = GetSectionList(); |
1486 | 2.14k | if (sections != nullptr) { |
1487 | 2.14k | size_t num_sections = sections->GetSize(); |
1488 | 3.10k | for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++966 ) { |
1489 | 2.88k | SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); |
1490 | 2.88k | if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) { |
1491 | 1.91k | return true; |
1492 | 1.91k | } |
1493 | 2.88k | } |
1494 | 2.14k | } |
1495 | 2.14k | } |
1496 | 224 | return false; |
1497 | 2.14k | } |
1498 | | |
1499 | | bool Module::LoadScriptingResourceInTarget(Target *target, Status &error, |
1500 | 221k | Stream &feedback_stream) { |
1501 | 221k | if (!target) { |
1502 | 0 | error.SetErrorString("invalid destination Target"); |
1503 | 0 | return false; |
1504 | 0 | } |
1505 | | |
1506 | 221k | LoadScriptFromSymFile should_load = |
1507 | 221k | target->TargetProperties::GetLoadScriptFromSymbolFile(); |
1508 | | |
1509 | 221k | if (should_load == eLoadScriptFromSymFileFalse) |
1510 | 1.00k | return false; |
1511 | | |
1512 | 220k | Debugger &debugger = target->GetDebugger(); |
1513 | 220k | const ScriptLanguage script_language = debugger.GetScriptLanguage(); |
1514 | 220k | if (script_language != eScriptLanguageNone220k ) { |
1515 | | |
1516 | 220k | PlatformSP platform_sp(target->GetPlatform()); |
1517 | | |
1518 | 220k | if (!platform_sp) { |
1519 | 0 | error.SetErrorString("invalid Platform"); |
1520 | 0 | return false; |
1521 | 0 | } |
1522 | | |
1523 | 220k | FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources( |
1524 | 220k | target, *this, feedback_stream); |
1525 | | |
1526 | 220k | const uint32_t num_specs = file_specs.GetSize(); |
1527 | 220k | if (num_specs) { |
1528 | 3 | ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter(); |
1529 | 3 | if (script_interpreter) { |
1530 | 6 | for (uint32_t i = 0; i < num_specs; ++i3 ) { |
1531 | 3 | FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i)); |
1532 | 3 | if (scripting_fspec && |
1533 | 3 | FileSystem::Instance().Exists(scripting_fspec)) { |
1534 | 3 | if (should_load == eLoadScriptFromSymFileWarn) { |
1535 | 0 | feedback_stream.Printf( |
1536 | 0 | "warning: '%s' contains a debug script. To run this script " |
1537 | 0 | "in " |
1538 | 0 | "this debug session:\n\n command script import " |
1539 | 0 | "\"%s\"\n\n" |
1540 | 0 | "To run all discovered debug scripts in this session:\n\n" |
1541 | 0 | " settings set target.load-script-from-symbol-file " |
1542 | 0 | "true\n", |
1543 | 0 | GetFileSpec().GetFileNameStrippingExtension().GetCString(), |
1544 | 0 | scripting_fspec.GetPath().c_str()); |
1545 | 0 | return false; |
1546 | 0 | } |
1547 | 3 | StreamString scripting_stream; |
1548 | 3 | scripting_fspec.Dump(scripting_stream.AsRawOstream()); |
1549 | 3 | LoadScriptOptions options; |
1550 | 3 | bool did_load = script_interpreter->LoadScriptingModule( |
1551 | 3 | scripting_stream.GetData(), options, error); |
1552 | 3 | if (!did_load) |
1553 | 0 | return false; |
1554 | 3 | } |
1555 | 3 | } |
1556 | 3 | } else { |
1557 | 0 | error.SetErrorString("invalid ScriptInterpreter"); |
1558 | 0 | return false; |
1559 | 0 | } |
1560 | 3 | } |
1561 | 220k | } |
1562 | 220k | return true; |
1563 | 220k | } |
1564 | | |
1565 | 114k | bool Module::SetArchitecture(const ArchSpec &new_arch) { |
1566 | 114k | if (!m_arch.IsValid()) { |
1567 | 108 | m_arch = new_arch; |
1568 | 108 | return true; |
1569 | 108 | } |
1570 | 114k | return m_arch.IsCompatibleMatch(new_arch); |
1571 | 114k | } |
1572 | | |
1573 | | bool Module::SetLoadAddress(Target &target, lldb::addr_t value, |
1574 | 4.41k | bool value_is_offset, bool &changed) { |
1575 | 4.41k | ObjectFile *object_file = GetObjectFile(); |
1576 | 4.41k | if (object_file != nullptr) { |
1577 | 4.41k | changed = object_file->SetLoadAddress(target, value, value_is_offset); |
1578 | 4.41k | return true; |
1579 | 4.41k | } else { |
1580 | 0 | changed = false; |
1581 | 0 | } |
1582 | 0 | return false; |
1583 | 4.41k | } |
1584 | | |
1585 | 24.2M | bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) { |
1586 | 24.2M | const UUID &uuid = module_ref.GetUUID(); |
1587 | | |
1588 | 24.2M | if (uuid.IsValid()) { |
1589 | | // If the UUID matches, then nothing more needs to match... |
1590 | 17.0M | return (uuid == GetUUID()); |
1591 | 17.0M | } |
1592 | | |
1593 | 7.13M | const FileSpec &file_spec = module_ref.GetFileSpec(); |
1594 | 7.13M | if (!FileSpec::Match(file_spec, m_file) && |
1595 | 7.13M | !FileSpec::Match(file_spec, m_platform_file)) |
1596 | 7.13M | return false; |
1597 | | |
1598 | 18.4E | const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); |
1599 | 18.4E | if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec())) |
1600 | 0 | return false; |
1601 | | |
1602 | 18.4E | const ArchSpec &arch = module_ref.GetArchitecture(); |
1603 | 18.4E | if (arch.IsValid()) { |
1604 | 792 | if (!m_arch.IsCompatibleMatch(arch)) |
1605 | 4 | return false; |
1606 | 792 | } |
1607 | | |
1608 | 18.4E | ConstString object_name = module_ref.GetObjectName(); |
1609 | 18.4E | if (object_name) { |
1610 | 0 | if (object_name != GetObjectName()) |
1611 | 0 | return false; |
1612 | 0 | } |
1613 | 18.4E | return true; |
1614 | 18.4E | } |
1615 | | |
1616 | | bool Module::FindSourceFile(const FileSpec &orig_spec, |
1617 | 40 | FileSpec &new_spec) const { |
1618 | 40 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1619 | 40 | if (auto remapped = m_source_mappings.FindFile(orig_spec)) { |
1620 | 0 | new_spec = *remapped; |
1621 | 0 | return true; |
1622 | 0 | } |
1623 | 40 | return false; |
1624 | 40 | } |
1625 | | |
1626 | 44.2k | std::optional<std::string> Module::RemapSourceFile(llvm::StringRef path) const { |
1627 | 44.2k | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1628 | 44.2k | if (auto remapped = m_source_mappings.RemapPath(path)) |
1629 | 94 | return remapped->GetPath(); |
1630 | 44.1k | return {}; |
1631 | 44.2k | } |
1632 | | |
1633 | | void Module::RegisterXcodeSDK(llvm::StringRef sdk_name, |
1634 | 1.58k | llvm::StringRef sysroot) { |
1635 | 1.58k | auto sdk_path_or_err = |
1636 | 1.58k | HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk_name.str()}); |
1637 | | |
1638 | 1.58k | if (!sdk_path_or_err) { |
1639 | 2 | Debugger::ReportError("Error while searching for Xcode SDK: " + |
1640 | 2 | toString(sdk_path_or_err.takeError())); |
1641 | 2 | return; |
1642 | 2 | } |
1643 | | |
1644 | 1.58k | auto sdk_path = *sdk_path_or_err; |
1645 | 1.58k | if (sdk_path.empty()) |
1646 | 4 | return; |
1647 | | // If the SDK changed for a previously registered source path, update it. |
1648 | | // This could happend with -fdebug-prefix-map, otherwise it's unlikely. |
1649 | 1.58k | if (!m_source_mappings.Replace(sysroot, sdk_path, true)) |
1650 | | // In the general case, however, append it to the list. |
1651 | 1.45k | m_source_mappings.Append(sysroot, sdk_path, false); |
1652 | 1.58k | } |
1653 | | |
1654 | 78 | bool Module::MergeArchitecture(const ArchSpec &arch_spec) { |
1655 | 78 | if (!arch_spec.IsValid()) |
1656 | 0 | return false; |
1657 | 78 | LLDB_LOGF(GetLog(LLDBLog::Object | LLDBLog::Modules), |
1658 | 78 | "module has arch %s, merging/replacing with arch %s", |
1659 | 78 | m_arch.GetTriple().getTriple().c_str(), |
1660 | 78 | arch_spec.GetTriple().getTriple().c_str()); |
1661 | 78 | if (!m_arch.IsCompatibleMatch(arch_spec)) { |
1662 | | // The new architecture is different, we just need to replace it. |
1663 | 0 | return SetArchitecture(arch_spec); |
1664 | 0 | } |
1665 | | |
1666 | | // Merge bits from arch_spec into "merged_arch" and set our architecture. |
1667 | 78 | ArchSpec merged_arch(m_arch); |
1668 | 78 | merged_arch.MergeFrom(arch_spec); |
1669 | | // SetArchitecture() is a no-op if m_arch is already valid. |
1670 | 78 | m_arch = ArchSpec(); |
1671 | 78 | return SetArchitecture(merged_arch); |
1672 | 78 | } |
1673 | | |
1674 | 90 | llvm::VersionTuple Module::GetVersion() { |
1675 | 90 | if (ObjectFile *obj_file = GetObjectFile()) |
1676 | 90 | return obj_file->GetVersion(); |
1677 | 0 | return llvm::VersionTuple(); |
1678 | 90 | } |
1679 | | |
1680 | 0 | bool Module::GetIsDynamicLinkEditor() { |
1681 | 0 | ObjectFile *obj_file = GetObjectFile(); |
1682 | |
|
1683 | 0 | if (obj_file) |
1684 | 0 | return obj_file->GetIsDynamicLinkEditor(); |
1685 | | |
1686 | 0 | return false; |
1687 | 0 | } |
1688 | | |
1689 | 18 | uint32_t Module::Hash() { |
1690 | 18 | std::string identifier; |
1691 | 18 | llvm::raw_string_ostream id_strm(identifier); |
1692 | 18 | id_strm << m_arch.GetTriple().str() << '-' << m_file.GetPath(); |
1693 | 18 | if (m_object_name) |
1694 | 3 | id_strm << '(' << m_object_name << ')'; |
1695 | 18 | if (m_object_offset > 0) |
1696 | 4 | id_strm << m_object_offset; |
1697 | 18 | const auto mtime = llvm::sys::toTimeT(m_object_mod_time); |
1698 | 18 | if (mtime > 0) |
1699 | 3 | id_strm << mtime; |
1700 | 18 | return llvm::djbHash(id_strm.str()); |
1701 | 18 | } |
1702 | | |
1703 | 18 | std::string Module::GetCacheKey() { |
1704 | 18 | std::string key; |
1705 | 18 | llvm::raw_string_ostream strm(key); |
1706 | 18 | strm << m_arch.GetTriple().str() << '-' << m_file.GetFilename(); |
1707 | 18 | if (m_object_name) |
1708 | 3 | strm << '(' << m_object_name << ')'; |
1709 | 18 | strm << '-' << llvm::format_hex(Hash(), 10); |
1710 | 18 | return strm.str(); |
1711 | 18 | } |
1712 | | |
1713 | 238k | DataFileCache *Module::GetIndexCache() { |
1714 | 238k | if (!ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache()) |
1715 | 238k | return nullptr; |
1716 | | // NOTE: intentional leak so we don't crash if global destructor chain gets |
1717 | | // called as other threads still use the result of this function |
1718 | 20 | static DataFileCache *g_data_file_cache = |
1719 | 20 | new DataFileCache(ModuleList::GetGlobalModuleListProperties() |
1720 | 20 | .GetLLDBIndexCachePath() |
1721 | 20 | .GetPath()); |
1722 | 20 | return g_data_file_cache; |
1723 | 238k | } |