blob: a6ffbd2b76aa7f5394edb54744452b5ba0333628 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Richard Mittonf86248d2013-09-12 02:20:34 +000012#include "lldb/Core/AddressResolverFileLine.h"
Enrico Granata17598482012-11-08 02:22:02 +000013#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Greg Claytonc9660542012-02-05 02:38:54 +000015#include "lldb/Core/DataBuffer.h"
16#include "lldb/Core/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/ModuleList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Section.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000022#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000024#include "lldb/Host/Host.h"
Enrico Granata17598482012-11-08 02:22:02 +000025#include "lldb/Host/Symbols.h"
26#include "lldb/Interpreter/CommandInterpreter.h"
27#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/lldb-private-log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000029#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Symbol/SymbolContext.h"
32#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000033#include "lldb/Target/CPPLanguageRuntime.h"
34#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytonc9660542012-02-05 02:38:54 +000035#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000036#include "lldb/Target/SectionLoadList.h"
Greg Claytonc9660542012-02-05 02:38:54 +000037#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000038#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Greg Clayton23f8c952014-03-24 23:10:19 +000040#include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042using namespace lldb;
43using namespace lldb_private;
44
Greg Clayton65a03992011-08-09 00:01:09 +000045// Shared pointers to modules track module lifetimes in
46// targets and in the global module, but this collection
47// will track all module objects that are still alive
48typedef std::vector<Module *> ModuleCollection;
49
50static ModuleCollection &
51GetModuleCollection()
52{
Jim Ingham549f7372011-10-31 23:47:10 +000053 // This module collection needs to live past any module, so we could either make it a
54 // shared pointer in each module or just leak is. Since it is only an empty vector by
55 // the time all the modules have gone away, we just leak it for now. If we decide this
56 // is a big problem we can introduce a Finalize method that will tear everything down in
57 // a predictable order.
58
59 static ModuleCollection *g_module_collection = NULL;
60 if (g_module_collection == NULL)
61 g_module_collection = new ModuleCollection();
62
63 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000064}
65
Greg Claytonb26e6be2012-01-27 18:08:35 +000066Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000067Module::GetAllocationModuleCollectionMutex()
68{
Greg Claytonb26e6be2012-01-27 18:08:35 +000069 // NOTE: The mutex below must be leaked since the global module list in
70 // the ModuleList class will get torn at some point, and we can't know
71 // if it will tear itself down before the "g_module_collection_mutex" below
72 // will. So we leak a Mutex object below to safeguard against that
73
74 static Mutex *g_module_collection_mutex = NULL;
75 if (g_module_collection_mutex == NULL)
76 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
77 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000078}
79
80size_t
81Module::GetNumberAllocatedModules ()
82{
83 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
84 return GetModuleCollection().size();
85}
86
87Module *
88Module::GetAllocatedModuleAtIndex (size_t idx)
89{
90 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
91 ModuleCollection &modules = GetModuleCollection();
92 if (idx < modules.size())
93 return modules[idx];
94 return NULL;
95}
Greg Clayton29ad7b92012-01-27 18:45:39 +000096#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000097
Greg Clayton29ad7b92012-01-27 18:45:39 +000098// These functions help us to determine if modules are still loaded, yet don't require that
99// you have a command interpreter and can easily be called from an external debugger.
100namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +0000101
Greg Clayton29ad7b92012-01-27 18:45:39 +0000102 void
103 ClearModuleInfo (void)
104 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000105 const bool mandatory = true;
106 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000107 }
108
109 void
110 DumpModuleInfo (void)
111 {
112 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
113 ModuleCollection &modules = GetModuleCollection();
114 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000115 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000116 for (size_t i=0; i<count; ++i)
117 {
118
119 StreamString strm;
120 Module *module = modules[i];
121 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
122 module->GetDescription(&strm, eDescriptionLevelFull);
123 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
124 module,
125 in_shared_module_list,
126 (uint32_t)module->use_count(),
127 strm.GetString().c_str());
128 }
129 }
130}
131
132#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000133
Greg Claytonb9a01b32012-02-26 05:51:37 +0000134Module::Module (const ModuleSpec &module_spec) :
135 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton34f11592014-03-04 21:20:23 +0000136 m_mod_time (),
137 m_arch (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000138 m_uuid (),
Greg Clayton34f11592014-03-04 21:20:23 +0000139 m_file (),
140 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000141 m_remote_install_file(),
Greg Clayton34f11592014-03-04 21:20:23 +0000142 m_symfile_spec (),
143 m_object_name (),
144 m_object_offset (),
145 m_object_mod_time (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000146 m_objfile_sp (),
147 m_symfile_ap (),
148 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000149 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000150 m_sections_ap(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000151 m_did_load_objfile (false),
152 m_did_load_symbol_vendor (false),
153 m_did_parse_uuid (false),
154 m_did_init_ast (false),
155 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000156 m_file_has_changed (false),
157 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000158{
159 // Scope for locker below...
160 {
161 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
162 GetModuleCollection().push_back(this);
163 }
164
Greg Clayton5160ce52013-03-27 23:08:40 +0000165 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000166 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000167 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Greg Claytonb9a01b32012-02-26 05:51:37 +0000168 this,
Greg Clayton34f11592014-03-04 21:20:23 +0000169 module_spec.GetArchitecture().GetArchitectureName(),
170 module_spec.GetFileSpec().GetPath().c_str(),
171 module_spec.GetObjectName().IsEmpty() ? "" : "(",
172 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
173 module_spec.GetObjectName().IsEmpty() ? "" : ")");
174
175 // First extract all module specifications from the file using the local
176 // file path. If there are no specifications, then don't fill anything in
177 ModuleSpecList modules_specs;
178 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
179 return;
180
181 // Now make sure that one of the module specifications matches what we just
182 // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
183 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
184 // UUID YYY and we don't want those to match. If they don't match, just don't
185 // fill any ivars in so we don't accidentally grab the wrong file later since
186 // they don't match...
187 ModuleSpec matching_module_spec;
188 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
189 return;
190 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
191 if (module_spec.GetArchitecture().IsValid())
192 m_arch = module_spec.GetArchitecture();
193 else
194 m_arch = matching_module_spec.GetArchitecture();
195 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
196 m_file = module_spec.GetFileSpec();
197 m_platform_file = module_spec.GetPlatformFileSpec();
198 m_symfile_spec = module_spec.GetSymbolFileSpec();
199 m_object_name = module_spec.GetObjectName();
200 m_object_offset = module_spec.GetObjectOffset();
201 m_object_mod_time = module_spec.GetObjectModificationTime();
202
Greg Claytonb9a01b32012-02-26 05:51:37 +0000203}
204
Greg Claytone72dfb32012-02-24 01:59:29 +0000205Module::Module(const FileSpec& file_spec,
206 const ArchSpec& arch,
207 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000208 off_t object_offset,
209 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 m_mutex (Mutex::eMutexTypeRecursive),
211 m_mod_time (file_spec.GetModificationTime()),
212 m_arch (arch),
213 m_uuid (),
214 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000215 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000216 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000217 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000219 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000220 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000221 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000222 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000223 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000224 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000225 m_sections_ap(),
Greg Claytone83e7312010-09-07 23:40:05 +0000226 m_did_load_objfile (false),
227 m_did_load_symbol_vendor (false),
228 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000229 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000230 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000231 m_file_has_changed (false),
232 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233{
Greg Clayton65a03992011-08-09 00:01:09 +0000234 // Scope for locker below...
235 {
236 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
237 GetModuleCollection().push_back(this);
238 }
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 if (object_name)
241 m_object_name = *object_name;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000242
243 if (object_mod_time_ptr)
244 m_object_mod_time = *object_mod_time_ptr;
245
Greg Clayton5160ce52013-03-27 23:08:40 +0000246 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000248 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000250 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000251 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 m_object_name.IsEmpty() ? "" : "(",
253 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
254 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
Greg Clayton23f8c952014-03-24 23:10:19 +0000257Module::Module () :
258 m_mutex (Mutex::eMutexTypeRecursive),
259 m_mod_time (),
260 m_arch (),
261 m_uuid (),
262 m_file (),
263 m_platform_file(),
264 m_remote_install_file (),
265 m_symfile_spec (),
266 m_object_name (),
267 m_object_offset (0),
268 m_object_mod_time (),
269 m_objfile_sp (),
270 m_symfile_ap (),
271 m_ast (),
272 m_source_mappings (),
273 m_sections_ap(),
274 m_did_load_objfile (false),
275 m_did_load_symbol_vendor (false),
276 m_did_parse_uuid (false),
277 m_did_init_ast (false),
278 m_is_dynamic_loader_module (false),
279 m_file_has_changed (false),
280 m_first_file_changed_log (false)
281{
282 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
283 GetModuleCollection().push_back(this);
284}
285
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286Module::~Module()
287{
Greg Clayton217b28b2013-05-22 20:13:22 +0000288 // Lock our module down while we tear everything down to make sure
289 // we don't get any access to the module while it is being destroyed
290 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000291 // Scope for locker below...
292 {
293 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
294 ModuleCollection &modules = GetModuleCollection();
295 ModuleCollection::iterator end = modules.end();
296 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000297 assert (pos != end);
298 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000299 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000300 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000302 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000304 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000305 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 m_object_name.IsEmpty() ? "" : "(",
307 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
308 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000309 // Release any auto pointers before we start tearing down our member
310 // variables since the object file and symbol files might need to make
311 // function calls back into this module object. The ordering is important
312 // here because symbol files can require the module object file. So we tear
313 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000314 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000315 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000316 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317}
318
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000319ObjectFile *
Andrew MacPherson17220c12014-03-05 10:12:43 +0000320Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error, size_t size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000321{
322 if (m_objfile_sp)
323 {
324 error.SetErrorString ("object file already exists");
325 }
326 else
327 {
328 Mutex::Locker locker (m_mutex);
329 if (process_sp)
330 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000331 m_did_load_objfile = true;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000332 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000333 Error readmem_error;
334 const size_t bytes_read = process_sp->ReadMemory (header_addr,
335 data_ap->GetBytes(),
336 data_ap->GetByteSize(),
337 readmem_error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000338 if (bytes_read == size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000339 {
340 DataBufferSP data_sp(data_ap.release());
341 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
342 if (m_objfile_sp)
343 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000344 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000345 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000346 m_object_name.SetCString (s.GetData());
347
348 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000349 // architecture since it might differ in vendor/os if some parts were
350 // unknown.
351 m_objfile_sp->GetArchitecture (m_arch);
352 }
353 else
354 {
355 error.SetErrorString ("unable to find suitable object file plug-in");
356 }
357 }
358 else
359 {
360 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
361 }
362 }
363 else
364 {
365 error.SetErrorString ("invalid process");
366 }
367 }
368 return m_objfile_sp.get();
369}
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371
Greg Clayton60830262011-02-04 18:53:10 +0000372const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373Module::GetUUID()
374{
375 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000376 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 {
378 ObjectFile * obj_file = GetObjectFile ();
379
380 if (obj_file != NULL)
381 {
382 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000383 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
385 }
386 return m_uuid;
387}
388
Greg Clayton6beaaa62011-01-17 03:46:26 +0000389ClangASTContext &
390Module::GetClangASTContext ()
391{
392 Mutex::Locker locker (m_mutex);
393 if (m_did_init_ast == false)
394 {
395 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000396 ArchSpec object_arch;
397 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000398 {
399 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000400
401 // LLVM wants this to be set to iOS or MacOSX; if we're working on
402 // a bare-boards type image, change the triple for llvm's benefit.
403 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
404 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
405 {
406 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
Jason Molendaa3329782014-03-29 18:54:20 +0000407 object_arch.GetTriple().getArch() == llvm::Triple::arm64 ||
Jason Molenda981d4df2012-10-16 20:45:49 +0000408 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
409 {
410 object_arch.GetTriple().setOS(llvm::Triple::IOS);
411 }
412 else
413 {
414 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
415 }
416 }
Greg Clayton514487e2011-02-15 21:59:32 +0000417 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000418 }
419 }
420 return m_ast;
421}
422
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423void
424Module::ParseAllDebugSymbols()
425{
426 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000427 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 if (num_comp_units == 0)
429 return;
430
Greg Claytona2eee182011-09-17 07:23:18 +0000431 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000432 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 SymbolVendor *symbols = GetSymbolVendor ();
434
Greg Claytonc7bece562013-01-25 18:06:21 +0000435 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 {
437 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
438 if (sc.comp_unit)
439 {
440 sc.function = NULL;
441 symbols->ParseVariablesForContext(sc);
442
443 symbols->ParseCompileUnitFunctions(sc);
444
Greg Claytonc7bece562013-01-25 18:06:21 +0000445 for (size_t func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 {
447 symbols->ParseFunctionBlocks(sc);
448
449 // Parse the variables for this function and all its blocks
450 symbols->ParseVariablesForContext(sc);
451 }
452
453
454 // Parse all types for this compile unit
455 sc.function = NULL;
456 symbols->ParseTypes(sc);
457 }
458 }
459}
460
461void
462Module::CalculateSymbolContext(SymbolContext* sc)
463{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000464 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465}
466
Greg Claytone72dfb32012-02-24 01:59:29 +0000467ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000468Module::CalculateSymbolContextModule ()
469{
Greg Claytone72dfb32012-02-24 01:59:29 +0000470 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000471}
472
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473void
474Module::DumpSymbolContext(Stream *s)
475{
Jason Molendafd54b362011-09-20 21:44:10 +0000476 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
Greg Claytonc7bece562013-01-25 18:06:21 +0000479size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480Module::GetNumCompileUnits()
481{
482 Mutex::Locker locker (m_mutex);
483 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
484 SymbolVendor *symbols = GetSymbolVendor ();
485 if (symbols)
486 return symbols->GetNumCompileUnits();
487 return 0;
488}
489
490CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000491Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492{
493 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000494 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495 CompUnitSP cu_sp;
496
497 if (index < num_comp_units)
498 {
499 SymbolVendor *symbols = GetSymbolVendor ();
500 if (symbols)
501 cu_sp = symbols->GetCompileUnitAtIndex(index);
502 }
503 return cu_sp;
504}
505
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506bool
507Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
508{
509 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000510 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000511 SectionList *section_list = GetSectionList();
512 if (section_list)
513 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 return false;
515}
516
517uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000518Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
519 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520{
521 Mutex::Locker locker (m_mutex);
522 uint32_t resolved_flags = 0;
523
Greg Clayton72310352013-02-23 04:12:47 +0000524 // Clear the result symbol context in case we don't find anything, but don't clear the target
525 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526
527 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000528 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529
530 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000531 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 {
533 // If the section offset based address resolved itself, then this
534 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000535 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 resolved_flags |= eSymbolContextModule;
537
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000538 SymbolVendor* sym_vendor = GetSymbolVendor();
539 if (!sym_vendor)
540 return resolved_flags;
541
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 // Resolve the compile unit, function, block, line table or line
543 // entry if requested.
544 if (resolve_scope & eSymbolContextCompUnit ||
545 resolve_scope & eSymbolContextFunction ||
546 resolve_scope & eSymbolContextBlock ||
547 resolve_scope & eSymbolContextLineEntry )
548 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000549 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 }
551
Jim Ingham680e1772010-08-31 23:51:36 +0000552 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
553 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000555 Symtab *symtab = sym_vendor->GetSymtab();
556 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000558 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000559 if (!sc.symbol &&
560 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
561 {
562 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
563 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
564 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
565 }
566
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000567 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000568 {
569 if (sc.symbol->IsSynthetic())
570 {
571 // We have a synthetic symbol so lets check if the object file
572 // from the symbol file in the symbol vendor is different than
573 // the object file for the module, and if so search its symbol
574 // table to see if we can come up with a better symbol. For example
575 // dSYM files on MacOSX have an unstripped symbol table inside of
576 // them.
577 ObjectFile *symtab_objfile = symtab->GetObjectFile();
578 if (symtab_objfile && symtab_objfile->IsStripped())
579 {
580 SymbolFile *symfile = sym_vendor->GetSymbolFile();
581 if (symfile)
582 {
583 ObjectFile *symfile_objfile = symfile->GetObjectFile();
584 if (symfile_objfile != symtab_objfile)
585 {
586 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
587 if (symfile_symtab)
588 {
589 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
590 if (symbol && !symbol->IsSynthetic())
591 {
592 sc.symbol = symbol;
593 }
594 }
595 }
596 }
597 }
598 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000599 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000600 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000601 }
602 }
603
604 // For function symbols, so_addr may be off by one. This is a convention consistent
605 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
606 // symbol lookup for disassembly and unwind.
607 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000608 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000609 {
610 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000611 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000612
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000613 bool do_resolve_tail_call_address = false; // prevent recursion
614 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
615 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000616 if (flags & eSymbolContextSymbol)
617 {
618 AddressRange addr_range;
619 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000621 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000623 // If the requested address is one past the address range of a function (i.e. a tail call),
624 // or the decremented address is the start of a function (i.e. some forms of trampoline),
625 // indicate that the symbol has been resolved.
626 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
627 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
628 {
629 resolved_flags |= flags;
630 }
631 }
632 else
633 {
634 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 }
636 }
637 }
638 }
639 }
640 return resolved_flags;
641}
642
643uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000644Module::ResolveSymbolContextForFilePath
645(
646 const char *file_path,
647 uint32_t line,
648 bool check_inlines,
649 uint32_t resolve_scope,
650 SymbolContextList& sc_list
651)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652{
Greg Clayton274060b2010-10-20 20:54:39 +0000653 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
655}
656
657uint32_t
658Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
659{
660 Mutex::Locker locker (m_mutex);
661 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000662 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
663 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 line,
665 check_inlines ? "yes" : "no",
666 resolve_scope);
667
668 const uint32_t initial_count = sc_list.GetSize();
669
670 SymbolVendor *symbols = GetSymbolVendor ();
671 if (symbols)
672 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
673
674 return sc_list.GetSize() - initial_count;
675}
676
677
Greg Claytonc7bece562013-01-25 18:06:21 +0000678size_t
679Module::FindGlobalVariables (const ConstString &name,
680 const ClangNamespaceDecl *namespace_decl,
681 bool append,
682 size_t max_matches,
683 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684{
685 SymbolVendor *symbols = GetSymbolVendor ();
686 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000687 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 return 0;
689}
Greg Claytonc7bece562013-01-25 18:06:21 +0000690
691size_t
692Module::FindGlobalVariables (const RegularExpression& regex,
693 bool append,
694 size_t max_matches,
695 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696{
697 SymbolVendor *symbols = GetSymbolVendor ();
698 if (symbols)
699 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
700 return 0;
701}
702
Greg Claytonc7bece562013-01-25 18:06:21 +0000703size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000704Module::FindCompileUnits (const FileSpec &path,
705 bool append,
706 SymbolContextList &sc_list)
707{
708 if (!append)
709 sc_list.Clear();
710
Greg Claytonc7bece562013-01-25 18:06:21 +0000711 const size_t start_size = sc_list.GetSize();
712 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000713 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000714 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000715 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000716 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000717 {
718 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000719 if (sc.comp_unit)
720 {
721 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
722 sc_list.Append(sc);
723 }
Greg Clayton644247c2011-07-07 01:59:51 +0000724 }
725 return sc_list.GetSize() - start_size;
726}
727
Greg Claytonc7bece562013-01-25 18:06:21 +0000728size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000729Module::FindFunctions (const ConstString &name,
730 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000731 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000732 bool include_symbols,
733 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000734 bool append,
735 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736{
Greg Clayton931180e2011-01-27 06:44:37 +0000737 if (!append)
738 sc_list.Clear();
739
Greg Clayton43fe2172013-04-03 02:00:15 +0000740 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000741
742 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000744
745 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000746 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000747 ConstString lookup_name;
748 uint32_t lookup_name_type_mask = 0;
749 bool match_name_after_lookup = false;
750 Module::PrepareForFunctionNameLookup (name,
751 name_type_mask,
752 lookup_name,
753 lookup_name_type_mask,
754 match_name_after_lookup);
755
756 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000757 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000758 symbols->FindFunctions(lookup_name,
759 namespace_decl,
760 lookup_name_type_mask,
761 include_inlines,
762 append,
763 sc_list);
764
Michael Sartaina7499c92013-07-01 19:45:50 +0000765 // Now check our symbol table for symbols that are code symbols if requested
766 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000767 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000768 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000769 if (symtab)
770 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
771 }
772 }
773
774 if (match_name_after_lookup)
775 {
776 SymbolContext sc;
777 size_t i = old_size;
778 while (i<sc_list.GetSize())
779 {
780 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000781 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000782 const char *func_name = sc.GetFunctionName().GetCString();
783 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000784 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000785 // Remove the current context
786 sc_list.RemoveContextAtIndex(i);
787 // Don't increment i and continue in the loop
788 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000789 }
790 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000791 ++i;
792 }
793 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000794 }
795 else
796 {
797 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000798 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000799 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
800
Michael Sartaina7499c92013-07-01 19:45:50 +0000801 // Now check our symbol table for symbols that are code symbols if requested
802 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000803 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000804 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000805 if (symtab)
806 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000807 }
808 }
809 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000810
811 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812}
813
Greg Claytonc7bece562013-01-25 18:06:21 +0000814size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000815Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000816 bool include_symbols,
817 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000818 bool append,
819 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820{
Greg Clayton931180e2011-01-27 06:44:37 +0000821 if (!append)
822 sc_list.Clear();
823
Greg Claytonc7bece562013-01-25 18:06:21 +0000824 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000825
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 SymbolVendor *symbols = GetSymbolVendor ();
827 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000828 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000829 symbols->FindFunctions(regex, include_inlines, append, sc_list);
830
831 // Now check our symbol table for symbols that are code symbols if requested
832 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000833 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000834 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000835 if (symtab)
836 {
837 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000838 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000839 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000840 if (num_matches)
841 {
842 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000843 const size_t end_functions_added_index = sc_list.GetSize();
844 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
845 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000846 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000847 // No functions were added, just symbols, so we can just append them
848 for (size_t i=0; i<num_matches; ++i)
849 {
850 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
851 SymbolType sym_type = sc.symbol->GetType();
852 if (sc.symbol && (sym_type == eSymbolTypeCode ||
853 sym_type == eSymbolTypeResolver))
854 sc_list.Append(sc);
855 }
856 }
857 else
858 {
859 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
860 FileAddrToIndexMap file_addr_to_index;
861 for (size_t i=start_size; i<end_functions_added_index; ++i)
862 {
863 const SymbolContext &sc = sc_list[i];
864 if (sc.block)
865 continue;
866 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
867 }
868
869 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
870 // Functions were added so we need to merge symbols into any
871 // existing function symbol contexts
872 for (size_t i=start_size; i<num_matches; ++i)
873 {
874 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
875 SymbolType sym_type = sc.symbol->GetType();
876 if (sc.symbol && (sym_type == eSymbolTypeCode ||
877 sym_type == eSymbolTypeResolver))
878 {
879 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
880 if (pos == end)
881 sc_list.Append(sc);
882 else
883 sc_list[pos->second].symbol = sc.symbol;
884 }
885 }
Greg Clayton931180e2011-01-27 06:44:37 +0000886 }
887 }
888 }
889 }
890 }
891 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892}
893
Richard Mittonf86248d2013-09-12 02:20:34 +0000894void
895Module::FindAddressesForLine (const lldb::TargetSP target_sp,
896 const FileSpec &file, uint32_t line,
897 Function *function,
898 std::vector<Address> &output_local, std::vector<Address> &output_extern)
899{
900 SearchFilterByModule filter(target_sp, m_file);
901 AddressResolverFileLine resolver(file, line, true);
902 resolver.ResolveAddress (filter);
903
904 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
905 {
906 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
907 Function *f = addr.CalculateSymbolContextFunction();
908 if (f && f == function)
909 output_local.push_back (addr);
910 else
911 output_extern.push_back (addr);
912 }
913}
914
Greg Claytonc7bece562013-01-25 18:06:21 +0000915size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000916Module::FindTypes_Impl (const SymbolContext& sc,
917 const ConstString &name,
918 const ClangNamespaceDecl *namespace_decl,
919 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000920 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000921 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000922{
923 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
924 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
925 {
926 SymbolVendor *symbols = GetSymbolVendor ();
927 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000928 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000929 }
930 return 0;
931}
932
Greg Claytonc7bece562013-01-25 18:06:21 +0000933size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000934Module::FindTypesInNamespace (const SymbolContext& sc,
935 const ConstString &type_name,
936 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000937 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000938 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000939{
Greg Clayton84db9102012-03-26 23:03:23 +0000940 const bool append = true;
941 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000942}
943
Greg Claytonb43165b2012-12-05 21:24:42 +0000944lldb::TypeSP
945Module::FindFirstType (const SymbolContext& sc,
946 const ConstString &name,
947 bool exact_match)
948{
949 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000950 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000951 if (num_matches)
952 return type_list.GetTypeAtIndex(0);
953 return TypeSP();
954}
955
956
Greg Claytonc7bece562013-01-25 18:06:21 +0000957size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000958Module::FindTypes (const SymbolContext& sc,
959 const ConstString &name,
960 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000961 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000962 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000963{
Greg Claytonc7bece562013-01-25 18:06:21 +0000964 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000965 const char *type_name_cstr = name.GetCString();
966 std::string type_scope;
967 std::string type_basename;
968 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000969 TypeClass type_class = eTypeClassAny;
970 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000971 {
Greg Clayton84db9102012-03-26 23:03:23 +0000972 // Check if "name" starts with "::" which means the qualified type starts
973 // from the root namespace and implies and exact match. The typenames we
974 // get back from clang do not start with "::" so we need to strip this off
975 // in order to get the qualfied names to match
976
977 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
978 {
979 type_scope.erase(0,2);
980 exact_match = true;
981 }
982 ConstString type_basename_const_str (type_basename.c_str());
983 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
984 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000985 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000986 num_matches = types.GetSize();
987 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000988 }
989 else
Greg Clayton84db9102012-03-26 23:03:23 +0000990 {
991 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000992 if (type_class != eTypeClassAny)
993 {
994 // The "type_name_cstr" will have been modified if we have a valid type class
995 // prefix (like "struct", "class", "union", "typedef" etc).
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +0000996 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
Greg Clayton7bc31332012-10-22 16:19:56 +0000997 types.RemoveMismatchedTypes (type_class);
998 num_matches = types.GetSize();
999 }
1000 else
1001 {
1002 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
1003 }
Greg Clayton84db9102012-03-26 23:03:23 +00001004 }
1005
1006 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001007
1008}
1009
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +00001011Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012{
1013 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001014 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 {
1016 ObjectFile *obj_file = GetObjectFile ();
1017 if (obj_file != NULL)
1018 {
1019 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +00001020 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +00001021 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 }
1023 }
1024 return m_symfile_ap.get();
1025}
1026
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027void
1028Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
1029{
1030 // Container objects whose paths do not specify a file directly can call
1031 // this function to correct the file and object names.
1032 m_file = file;
1033 m_mod_time = file.GetModificationTime();
1034 m_object_name = object_name;
1035}
1036
1037const ArchSpec&
1038Module::GetArchitecture () const
1039{
1040 return m_arch;
1041}
1042
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001043std::string
1044Module::GetSpecificationDescription () const
1045{
1046 std::string spec(GetFileSpec().GetPath());
1047 if (m_object_name)
1048 {
1049 spec += '(';
1050 spec += m_object_name.GetCString();
1051 spec += ')';
1052 }
1053 return spec;
1054}
1055
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001057Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001058{
1059 Mutex::Locker locker (m_mutex);
1060
Greg Claytonc982b3d2011-11-28 01:45:00 +00001061 if (level >= eDescriptionLevelFull)
1062 {
1063 if (m_arch.IsValid())
1064 s->Printf("(%s) ", m_arch.GetArchitectureName());
1065 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001066
Greg Claytonc982b3d2011-11-28 01:45:00 +00001067 if (level == eDescriptionLevelBrief)
1068 {
1069 const char *filename = m_file.GetFilename().GetCString();
1070 if (filename)
1071 s->PutCString (filename);
1072 }
1073 else
1074 {
1075 char path[PATH_MAX];
1076 if (m_file.GetPath(path, sizeof(path)))
1077 s->PutCString(path);
1078 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001079
1080 const char *object_name = m_object_name.GetCString();
1081 if (object_name)
1082 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001083}
1084
1085void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001086Module::ReportError (const char *format, ...)
1087{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001088 if (format && format[0])
1089 {
1090 StreamString strm;
1091 strm.PutCString("error: ");
1092 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001093 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001094 va_list args;
1095 va_start (args, format);
1096 strm.PrintfVarArg(format, args);
1097 va_end (args);
1098
1099 const int format_len = strlen(format);
1100 if (format_len > 0)
1101 {
1102 const char last_char = format[format_len-1];
1103 if (last_char != '\n' || last_char != '\r')
1104 strm.EOL();
1105 }
1106 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1107
1108 }
1109}
1110
Greg Clayton1d609092012-07-12 22:51:12 +00001111bool
1112Module::FileHasChanged () const
1113{
1114 if (m_file_has_changed == false)
1115 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1116 return m_file_has_changed;
1117}
1118
Greg Claytone38a5ed2012-01-05 03:57:59 +00001119void
1120Module::ReportErrorIfModifyDetected (const char *format, ...)
1121{
Greg Clayton1d609092012-07-12 22:51:12 +00001122 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001123 {
Greg Clayton1d609092012-07-12 22:51:12 +00001124 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001125 {
Greg Clayton1d609092012-07-12 22:51:12 +00001126 m_first_file_changed_log = true;
1127 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001128 {
Greg Clayton1d609092012-07-12 22:51:12 +00001129 StreamString strm;
1130 strm.PutCString("error: the object file ");
1131 GetDescription(&strm, lldb::eDescriptionLevelFull);
1132 strm.PutCString (" has been modified\n");
1133
1134 va_list args;
1135 va_start (args, format);
1136 strm.PrintfVarArg(format, args);
1137 va_end (args);
1138
1139 const int format_len = strlen(format);
1140 if (format_len > 0)
1141 {
1142 const char last_char = format[format_len-1];
1143 if (last_char != '\n' || last_char != '\r')
1144 strm.EOL();
1145 }
1146 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1147 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001148 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001149 }
1150 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001151}
1152
1153void
1154Module::ReportWarning (const char *format, ...)
1155{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001156 if (format && format[0])
1157 {
1158 StreamString strm;
1159 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001160 GetDescription(&strm, lldb::eDescriptionLevelFull);
1161 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001162
1163 va_list args;
1164 va_start (args, format);
1165 strm.PrintfVarArg(format, args);
1166 va_end (args);
1167
1168 const int format_len = strlen(format);
1169 if (format_len > 0)
1170 {
1171 const char last_char = format[format_len-1];
1172 if (last_char != '\n' || last_char != '\r')
1173 strm.EOL();
1174 }
1175 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1176 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001177}
1178
1179void
1180Module::LogMessage (Log *log, const char *format, ...)
1181{
1182 if (log)
1183 {
1184 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001185 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001186 log_message.PutCString (": ");
1187 va_list args;
1188 va_start (args, format);
1189 log_message.PrintfVarArg (format, args);
1190 va_end (args);
1191 log->PutCString(log_message.GetString().c_str());
1192 }
1193}
1194
Greg Claytond61c0fc2012-04-23 22:55:20 +00001195void
1196Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1197{
1198 if (log)
1199 {
1200 StreamString log_message;
1201 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1202 log_message.PutCString (": ");
1203 va_list args;
1204 va_start (args, format);
1205 log_message.PrintfVarArg (format, args);
1206 va_end (args);
1207 if (log->GetVerbose())
1208 Host::Backtrace (log_message, 1024);
1209 log->PutCString(log_message.GetString().c_str());
1210 }
1211}
1212
Greg Claytonc982b3d2011-11-28 01:45:00 +00001213void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214Module::Dump(Stream *s)
1215{
1216 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001217 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001219 s->Printf("Module %s%s%s%s\n",
1220 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 m_object_name ? "(" : "",
1222 m_object_name ? m_object_name.GetCString() : "",
1223 m_object_name ? ")" : "");
1224
1225 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228 if (objfile)
1229 objfile->Dump(s);
1230
1231 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232 if (symbols)
1233 symbols->Dump(s);
1234
1235 s->IndentLess();
1236}
1237
1238
1239TypeList*
1240Module::GetTypeList ()
1241{
1242 SymbolVendor *symbols = GetSymbolVendor ();
1243 if (symbols)
1244 return &symbols->GetTypeList();
1245 return NULL;
1246}
1247
1248const ConstString &
1249Module::GetObjectName() const
1250{
1251 return m_object_name;
1252}
1253
1254ObjectFile *
1255Module::GetObjectFile()
1256{
1257 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001258 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260 Timer scoped_timer(__PRETTY_FUNCTION__,
1261 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001262 DataBufferSP data_sp;
1263 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001264 const lldb::offset_t file_size = m_file.GetByteSize();
1265 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001266 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001267 m_did_load_objfile = true;
1268 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1269 &m_file,
1270 m_object_offset,
1271 file_size - m_object_offset,
1272 data_sp,
1273 data_offset);
1274 if (m_objfile_sp)
1275 {
1276 // Once we get the object file, update our module with the object file's
1277 // architecture since it might differ in vendor/os if some parts were
1278 // unknown.
1279 m_objfile_sp->GetArchitecture (m_arch);
1280 }
Greg Clayton593577a2011-09-21 03:57:31 +00001281 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 }
Greg Clayton762f7132011-09-18 18:59:15 +00001283 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284}
1285
Michael Sartaina7499c92013-07-01 19:45:50 +00001286SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001287Module::GetSectionList()
1288{
1289 // Populate m_unified_sections_ap with sections from objfile.
1290 if (m_sections_ap.get() == NULL)
1291 {
1292 ObjectFile *obj_file = GetObjectFile();
1293 if (obj_file)
1294 obj_file->CreateSections(*GetUnifiedSectionList());
1295 }
1296 return m_sections_ap.get();
1297}
1298
1299SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001300Module::GetUnifiedSectionList()
1301{
Greg Clayton3046e662013-07-10 01:23:25 +00001302 // Populate m_unified_sections_ap with sections from objfile.
1303 if (m_sections_ap.get() == NULL)
1304 m_sections_ap.reset(new SectionList());
1305 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001306}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307
1308const Symbol *
1309Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1310{
1311 Timer scoped_timer(__PRETTY_FUNCTION__,
1312 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1313 name.AsCString(),
1314 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001315 SymbolVendor* sym_vendor = GetSymbolVendor();
1316 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001318 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001320 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 }
1322 return NULL;
1323}
1324void
1325Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1326{
1327 // No need to protect this call using m_mutex all other method calls are
1328 // already thread safe.
1329
1330 size_t num_indices = symbol_indexes.size();
1331 if (num_indices > 0)
1332 {
1333 SymbolContext sc;
1334 CalculateSymbolContext (&sc);
1335 for (size_t i = 0; i < num_indices; i++)
1336 {
1337 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1338 if (sc.symbol)
1339 sc_list.Append (sc);
1340 }
1341 }
1342}
1343
1344size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001345Module::FindFunctionSymbols (const ConstString &name,
1346 uint32_t name_type_mask,
1347 SymbolContextList& sc_list)
1348{
1349 Timer scoped_timer(__PRETTY_FUNCTION__,
1350 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1351 name.AsCString(),
1352 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001353 SymbolVendor* sym_vendor = GetSymbolVendor();
1354 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001355 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001356 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001357 if (symtab)
1358 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1359 }
1360 return 0;
1361}
1362
1363size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001364Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365{
1366 // No need to protect this call using m_mutex all other method calls are
1367 // already thread safe.
1368
1369
1370 Timer scoped_timer(__PRETTY_FUNCTION__,
1371 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1372 name.AsCString(),
1373 symbol_type);
1374 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001375 SymbolVendor* sym_vendor = GetSymbolVendor();
1376 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001378 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 if (symtab)
1380 {
1381 std::vector<uint32_t> symbol_indexes;
1382 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1383 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1384 }
1385 }
1386 return sc_list.GetSize() - initial_size;
1387}
1388
1389size_t
1390Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1391{
1392 // No need to protect this call using m_mutex all other method calls are
1393 // already thread safe.
1394
1395 Timer scoped_timer(__PRETTY_FUNCTION__,
1396 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1397 regex.GetText(),
1398 symbol_type);
1399 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001400 SymbolVendor* sym_vendor = GetSymbolVendor();
1401 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001403 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 if (symtab)
1405 {
1406 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001407 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1409 }
1410 }
1411 return sc_list.GetSize() - initial_size;
1412}
1413
Greg Claytone01e07b2013-04-18 18:10:51 +00001414void
1415Module::SetSymbolFileFileSpec (const FileSpec &file)
1416{
Michael Sartaina7499c92013-07-01 19:45:50 +00001417 // Remove any sections in the unified section list that come from the current symbol vendor.
1418 if (m_symfile_ap)
1419 {
Greg Clayton3046e662013-07-10 01:23:25 +00001420 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001421 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1422 if (section_list && symbol_file)
1423 {
1424 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001425 // Make sure we have an object file and that the symbol vendor's objfile isn't
1426 // the same as the module's objfile before we remove any sections for it...
1427 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001428 {
1429 size_t num_sections = section_list->GetNumSections (0);
1430 for (size_t idx = num_sections; idx > 0; --idx)
1431 {
1432 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1433 if (section_sp->GetObjectFile() == obj_file)
1434 {
Greg Clayton3046e662013-07-10 01:23:25 +00001435 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001436 }
1437 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001438 }
1439 }
1440 }
1441
Greg Claytone01e07b2013-04-18 18:10:51 +00001442 m_symfile_spec = file;
1443 m_symfile_ap.reset();
1444 m_did_load_symbol_vendor = false;
1445}
1446
Jim Ingham5aee1622010-08-09 23:31:02 +00001447bool
1448Module::IsExecutable ()
1449{
1450 if (GetObjectFile() == NULL)
1451 return false;
1452 else
1453 return GetObjectFile()->IsExecutable();
1454}
1455
Jim Inghamb53cb272011-08-03 01:03:17 +00001456bool
1457Module::IsLoadedInTarget (Target *target)
1458{
1459 ObjectFile *obj_file = GetObjectFile();
1460 if (obj_file)
1461 {
Greg Clayton3046e662013-07-10 01:23:25 +00001462 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001463 if (sections != NULL)
1464 {
1465 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001466 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1467 {
1468 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1469 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1470 {
1471 return true;
1472 }
1473 }
1474 }
1475 }
1476 return false;
1477}
Enrico Granata17598482012-11-08 02:22:02 +00001478
1479bool
Enrico Granata97303392013-05-21 00:00:30 +00001480Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001481{
1482 if (!target)
1483 {
1484 error.SetErrorString("invalid destination Target");
1485 return false;
1486 }
1487
Enrico Granata397ddd52013-05-21 20:13:34 +00001488 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001489
Greg Clayton91c0e742013-01-11 23:44:27 +00001490 Debugger &debugger = target->GetDebugger();
1491 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1492 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001493 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001494
1495 PlatformSP platform_sp(target->GetPlatform());
1496
1497 if (!platform_sp)
1498 {
1499 error.SetErrorString("invalid Platform");
1500 return false;
1501 }
Enrico Granata17598482012-11-08 02:22:02 +00001502
Greg Claytonb9d88902013-03-23 00:50:58 +00001503 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1504 *this);
1505
1506
1507 const uint32_t num_specs = file_specs.GetSize();
1508 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001509 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001510 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1511 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001512 {
1513 for (uint32_t i=0; i<num_specs; ++i)
1514 {
1515 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1516 if (scripting_fspec && scripting_fspec.Exists())
1517 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001518 if (shoud_load == eLoadScriptFromSymFileFalse)
1519 return false;
1520 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001521 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001522 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001523 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1524 "this debug session:\n\n command script import \"%s\"\n\n"
1525 "To run all discovered debug scripts in this session:\n\n"
1526 " settings set target.load-script-from-symbol-file true\n",
1527 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1528 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001529 return false;
1530 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001531 StreamString scripting_stream;
1532 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001533 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001534 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001535 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1536 can_reload,
1537 init_lldb_globals,
1538 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001539 if (!did_load)
1540 return false;
1541 }
1542 }
1543 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001544 else
1545 {
1546 error.SetErrorString("invalid ScriptInterpreter");
1547 return false;
1548 }
Enrico Granata17598482012-11-08 02:22:02 +00001549 }
1550 }
1551 return true;
1552}
1553
1554bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001555Module::SetArchitecture (const ArchSpec &new_arch)
1556{
Greg Clayton64195a22011-02-23 00:35:02 +00001557 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001558 {
1559 m_arch = new_arch;
1560 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001561 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001562 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001563}
1564
Greg Claytonc9660542012-02-05 02:38:54 +00001565bool
Greg Clayton751caf62014-02-07 22:54:47 +00001566Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001567{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001568 ObjectFile *object_file = GetObjectFile();
1569 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001570 {
Greg Clayton751caf62014-02-07 22:54:47 +00001571 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001572 return true;
1573 }
1574 else
1575 {
1576 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001577 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001578 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001579}
1580
Greg Claytonb9a01b32012-02-26 05:51:37 +00001581
1582bool
1583Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1584{
1585 const UUID &uuid = module_ref.GetUUID();
1586
1587 if (uuid.IsValid())
1588 {
1589 // If the UUID matches, then nothing more needs to match...
1590 if (uuid == GetUUID())
1591 return true;
1592 else
1593 return false;
1594 }
1595
1596 const FileSpec &file_spec = module_ref.GetFileSpec();
1597 if (file_spec)
1598 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001599 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001600 return false;
1601 }
1602
1603 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1604 if (platform_file_spec)
1605 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001606 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001607 return false;
1608 }
1609
1610 const ArchSpec &arch = module_ref.GetArchitecture();
1611 if (arch.IsValid())
1612 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001613 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001614 return false;
1615 }
1616
1617 const ConstString &object_name = module_ref.GetObjectName();
1618 if (object_name)
1619 {
1620 if (object_name != GetObjectName())
1621 return false;
1622 }
1623 return true;
1624}
1625
Greg Claytond804d282012-03-15 21:01:31 +00001626bool
1627Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1628{
1629 Mutex::Locker locker (m_mutex);
1630 return m_source_mappings.FindFile (orig_spec, new_spec);
1631}
1632
Greg Claytonf9be6932012-03-19 22:22:41 +00001633bool
1634Module::RemapSourceFile (const char *path, std::string &new_path) const
1635{
1636 Mutex::Locker locker (m_mutex);
1637 return m_source_mappings.RemapPath(path, new_path);
1638}
1639
Enrico Granata3467d802012-09-04 18:47:54 +00001640uint32_t
1641Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1642{
1643 ObjectFile *obj_file = GetObjectFile();
1644 if (obj_file)
1645 return obj_file->GetVersion (versions, num_versions);
1646
1647 if (versions && num_versions)
1648 {
1649 for (uint32_t i=0; i<num_versions; ++i)
Enrico Granataafcbdb12014-03-25 20:53:33 +00001650 versions[i] = LLDB_INVALID_MODULE_VERSION;
Enrico Granata3467d802012-09-04 18:47:54 +00001651 }
1652 return 0;
1653}
Greg Clayton43fe2172013-04-03 02:00:15 +00001654
1655void
1656Module::PrepareForFunctionNameLookup (const ConstString &name,
1657 uint32_t name_type_mask,
1658 ConstString &lookup_name,
1659 uint32_t &lookup_name_type_mask,
1660 bool &match_name_after_lookup)
1661{
1662 const char *name_cstr = name.GetCString();
1663 lookup_name_type_mask = eFunctionNameTypeNone;
1664 match_name_after_lookup = false;
1665 const char *base_name_start = NULL;
1666 const char *base_name_end = NULL;
1667
1668 if (name_type_mask & eFunctionNameTypeAuto)
1669 {
1670 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1671 lookup_name_type_mask = eFunctionNameTypeFull;
1672 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1673 lookup_name_type_mask = eFunctionNameTypeFull;
1674 else
1675 {
1676 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1677 lookup_name_type_mask |= eFunctionNameTypeSelector;
1678
Greg Clayton6ecb2322013-05-18 00:11:21 +00001679 CPPLanguageRuntime::MethodName cpp_method (name);
1680 llvm::StringRef basename (cpp_method.GetBasename());
1681 if (basename.empty())
1682 {
1683 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1684 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1685 }
1686 else
1687 {
1688 base_name_start = basename.data();
1689 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001690 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001691 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001692 }
1693 }
1694 else
1695 {
1696 lookup_name_type_mask = name_type_mask;
1697 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1698 {
1699 // If they've asked for a CPP method or function name and it can't be that, we don't
1700 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001701 CPPLanguageRuntime::MethodName cpp_method (name);
1702 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001703 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001704 llvm::StringRef basename (cpp_method.GetBasename());
1705 base_name_start = basename.data();
1706 base_name_end = base_name_start + basename.size();
1707
1708 if (!cpp_method.GetQualifiers().empty())
1709 {
1710 // There is a "const" or other qualifer following the end of the fucntion parens,
1711 // this can't be a eFunctionNameTypeBase
1712 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1713 if (lookup_name_type_mask == eFunctionNameTypeNone)
1714 return;
1715 }
1716 }
1717 else
1718 {
1719 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1720 {
1721 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1722 if (lookup_name_type_mask == eFunctionNameTypeNone)
1723 return;
1724 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001725 }
1726 }
1727
1728 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1729 {
1730 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1731 {
1732 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1733 if (lookup_name_type_mask == eFunctionNameTypeNone)
1734 return;
1735 }
1736 }
1737 }
1738
1739 if (base_name_start &&
1740 base_name_end &&
1741 base_name_start != name_cstr &&
1742 base_name_start < base_name_end)
1743 {
1744 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1745 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1746 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1747 // to true
1748 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1749 match_name_after_lookup = true;
1750 }
1751 else
1752 {
1753 // The name is already correct, just use the exact name as supplied, and we won't need
1754 // to check if any matches contain "name"
1755 lookup_name = name;
1756 match_name_after_lookup = false;
1757 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001758}
Greg Clayton23f8c952014-03-24 23:10:19 +00001759
1760ModuleSP
1761Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
1762{
1763 if (delegate_sp)
1764 {
1765 // Must create a module and place it into a shared pointer before
1766 // we can create an object file since it has a std::weak_ptr back
1767 // to the module, so we need to control the creation carefully in
1768 // this static function
1769 ModuleSP module_sp(new Module());
1770 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
1771 if (module_sp->m_objfile_sp)
1772 {
1773 // Once we get the object file, update our module with the object file's
1774 // architecture since it might differ in vendor/os if some parts were
1775 // unknown.
1776 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
1777 }
1778 return module_sp;
1779 }
1780 return ModuleSP();
1781}
1782