blob: e6f2daa76e7e41872d940a0242b58d9ecba55254 [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 ||
407 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
408 {
409 object_arch.GetTriple().setOS(llvm::Triple::IOS);
410 }
411 else
412 {
413 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
414 }
415 }
Greg Clayton514487e2011-02-15 21:59:32 +0000416 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000417 }
418 }
419 return m_ast;
420}
421
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422void
423Module::ParseAllDebugSymbols()
424{
425 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000426 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 if (num_comp_units == 0)
428 return;
429
Greg Claytona2eee182011-09-17 07:23:18 +0000430 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000431 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 SymbolVendor *symbols = GetSymbolVendor ();
433
Greg Claytonc7bece562013-01-25 18:06:21 +0000434 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 {
436 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
437 if (sc.comp_unit)
438 {
439 sc.function = NULL;
440 symbols->ParseVariablesForContext(sc);
441
442 symbols->ParseCompileUnitFunctions(sc);
443
Greg Claytonc7bece562013-01-25 18:06:21 +0000444 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 +0000445 {
446 symbols->ParseFunctionBlocks(sc);
447
448 // Parse the variables for this function and all its blocks
449 symbols->ParseVariablesForContext(sc);
450 }
451
452
453 // Parse all types for this compile unit
454 sc.function = NULL;
455 symbols->ParseTypes(sc);
456 }
457 }
458}
459
460void
461Module::CalculateSymbolContext(SymbolContext* sc)
462{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000463 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
Greg Claytone72dfb32012-02-24 01:59:29 +0000466ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000467Module::CalculateSymbolContextModule ()
468{
Greg Claytone72dfb32012-02-24 01:59:29 +0000469 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000470}
471
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472void
473Module::DumpSymbolContext(Stream *s)
474{
Jason Molendafd54b362011-09-20 21:44:10 +0000475 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476}
477
Greg Claytonc7bece562013-01-25 18:06:21 +0000478size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479Module::GetNumCompileUnits()
480{
481 Mutex::Locker locker (m_mutex);
482 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
483 SymbolVendor *symbols = GetSymbolVendor ();
484 if (symbols)
485 return symbols->GetNumCompileUnits();
486 return 0;
487}
488
489CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000490Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491{
492 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000493 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 CompUnitSP cu_sp;
495
496 if (index < num_comp_units)
497 {
498 SymbolVendor *symbols = GetSymbolVendor ();
499 if (symbols)
500 cu_sp = symbols->GetCompileUnitAtIndex(index);
501 }
502 return cu_sp;
503}
504
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505bool
506Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
507{
508 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000509 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000510 SectionList *section_list = GetSectionList();
511 if (section_list)
512 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 return false;
514}
515
516uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000517Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
518 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519{
520 Mutex::Locker locker (m_mutex);
521 uint32_t resolved_flags = 0;
522
Greg Clayton72310352013-02-23 04:12:47 +0000523 // Clear the result symbol context in case we don't find anything, but don't clear the target
524 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525
526 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000527 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
529 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000530 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 {
532 // If the section offset based address resolved itself, then this
533 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000534 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 resolved_flags |= eSymbolContextModule;
536
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000537 SymbolVendor* sym_vendor = GetSymbolVendor();
538 if (!sym_vendor)
539 return resolved_flags;
540
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 // Resolve the compile unit, function, block, line table or line
542 // entry if requested.
543 if (resolve_scope & eSymbolContextCompUnit ||
544 resolve_scope & eSymbolContextFunction ||
545 resolve_scope & eSymbolContextBlock ||
546 resolve_scope & eSymbolContextLineEntry )
547 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000548 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 }
550
Jim Ingham680e1772010-08-31 23:51:36 +0000551 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
552 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000554 Symtab *symtab = sym_vendor->GetSymtab();
555 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000557 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000558 if (!sc.symbol &&
559 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
560 {
561 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
562 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
563 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
564 }
565
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000566 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000567 {
568 if (sc.symbol->IsSynthetic())
569 {
570 // We have a synthetic symbol so lets check if the object file
571 // from the symbol file in the symbol vendor is different than
572 // the object file for the module, and if so search its symbol
573 // table to see if we can come up with a better symbol. For example
574 // dSYM files on MacOSX have an unstripped symbol table inside of
575 // them.
576 ObjectFile *symtab_objfile = symtab->GetObjectFile();
577 if (symtab_objfile && symtab_objfile->IsStripped())
578 {
579 SymbolFile *symfile = sym_vendor->GetSymbolFile();
580 if (symfile)
581 {
582 ObjectFile *symfile_objfile = symfile->GetObjectFile();
583 if (symfile_objfile != symtab_objfile)
584 {
585 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
586 if (symfile_symtab)
587 {
588 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
589 if (symbol && !symbol->IsSynthetic())
590 {
591 sc.symbol = symbol;
592 }
593 }
594 }
595 }
596 }
597 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000598 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000599 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000600 }
601 }
602
603 // For function symbols, so_addr may be off by one. This is a convention consistent
604 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
605 // symbol lookup for disassembly and unwind.
606 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000607 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000608 {
609 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000610 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000611
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000612 bool do_resolve_tail_call_address = false; // prevent recursion
613 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
614 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000615 if (flags & eSymbolContextSymbol)
616 {
617 AddressRange addr_range;
618 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000620 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000622 // If the requested address is one past the address range of a function (i.e. a tail call),
623 // or the decremented address is the start of a function (i.e. some forms of trampoline),
624 // indicate that the symbol has been resolved.
625 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
626 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
627 {
628 resolved_flags |= flags;
629 }
630 }
631 else
632 {
633 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 }
635 }
636 }
637 }
638 }
639 return resolved_flags;
640}
641
642uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000643Module::ResolveSymbolContextForFilePath
644(
645 const char *file_path,
646 uint32_t line,
647 bool check_inlines,
648 uint32_t resolve_scope,
649 SymbolContextList& sc_list
650)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651{
Greg Clayton274060b2010-10-20 20:54:39 +0000652 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
654}
655
656uint32_t
657Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
658{
659 Mutex::Locker locker (m_mutex);
660 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000661 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
662 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 line,
664 check_inlines ? "yes" : "no",
665 resolve_scope);
666
667 const uint32_t initial_count = sc_list.GetSize();
668
669 SymbolVendor *symbols = GetSymbolVendor ();
670 if (symbols)
671 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
672
673 return sc_list.GetSize() - initial_count;
674}
675
676
Greg Claytonc7bece562013-01-25 18:06:21 +0000677size_t
678Module::FindGlobalVariables (const ConstString &name,
679 const ClangNamespaceDecl *namespace_decl,
680 bool append,
681 size_t max_matches,
682 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683{
684 SymbolVendor *symbols = GetSymbolVendor ();
685 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000686 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 return 0;
688}
Greg Claytonc7bece562013-01-25 18:06:21 +0000689
690size_t
691Module::FindGlobalVariables (const RegularExpression& regex,
692 bool append,
693 size_t max_matches,
694 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695{
696 SymbolVendor *symbols = GetSymbolVendor ();
697 if (symbols)
698 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
699 return 0;
700}
701
Greg Claytonc7bece562013-01-25 18:06:21 +0000702size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000703Module::FindCompileUnits (const FileSpec &path,
704 bool append,
705 SymbolContextList &sc_list)
706{
707 if (!append)
708 sc_list.Clear();
709
Greg Claytonc7bece562013-01-25 18:06:21 +0000710 const size_t start_size = sc_list.GetSize();
711 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000712 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000713 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000714 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000715 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000716 {
717 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000718 if (sc.comp_unit)
719 {
720 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
721 sc_list.Append(sc);
722 }
Greg Clayton644247c2011-07-07 01:59:51 +0000723 }
724 return sc_list.GetSize() - start_size;
725}
726
Greg Claytonc7bece562013-01-25 18:06:21 +0000727size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000728Module::FindFunctions (const ConstString &name,
729 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000730 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000731 bool include_symbols,
732 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000733 bool append,
734 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735{
Greg Clayton931180e2011-01-27 06:44:37 +0000736 if (!append)
737 sc_list.Clear();
738
Greg Clayton43fe2172013-04-03 02:00:15 +0000739 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000740
741 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000743
744 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000745 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000746 ConstString lookup_name;
747 uint32_t lookup_name_type_mask = 0;
748 bool match_name_after_lookup = false;
749 Module::PrepareForFunctionNameLookup (name,
750 name_type_mask,
751 lookup_name,
752 lookup_name_type_mask,
753 match_name_after_lookup);
754
755 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000756 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000757 symbols->FindFunctions(lookup_name,
758 namespace_decl,
759 lookup_name_type_mask,
760 include_inlines,
761 append,
762 sc_list);
763
Michael Sartaina7499c92013-07-01 19:45:50 +0000764 // Now check our symbol table for symbols that are code symbols if requested
765 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000766 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000767 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000768 if (symtab)
769 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
770 }
771 }
772
773 if (match_name_after_lookup)
774 {
775 SymbolContext sc;
776 size_t i = old_size;
777 while (i<sc_list.GetSize())
778 {
779 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000780 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000781 const char *func_name = sc.GetFunctionName().GetCString();
782 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000783 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000784 // Remove the current context
785 sc_list.RemoveContextAtIndex(i);
786 // Don't increment i and continue in the loop
787 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000788 }
789 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000790 ++i;
791 }
792 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000793 }
794 else
795 {
796 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000797 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000798 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
799
Michael Sartaina7499c92013-07-01 19:45:50 +0000800 // Now check our symbol table for symbols that are code symbols if requested
801 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000802 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000803 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000804 if (symtab)
805 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000806 }
807 }
808 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000809
810 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811}
812
Greg Claytonc7bece562013-01-25 18:06:21 +0000813size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000814Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000815 bool include_symbols,
816 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000817 bool append,
818 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819{
Greg Clayton931180e2011-01-27 06:44:37 +0000820 if (!append)
821 sc_list.Clear();
822
Greg Claytonc7bece562013-01-25 18:06:21 +0000823 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000824
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 SymbolVendor *symbols = GetSymbolVendor ();
826 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000827 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000828 symbols->FindFunctions(regex, include_inlines, append, sc_list);
829
830 // Now check our symbol table for symbols that are code symbols if requested
831 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000832 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000833 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000834 if (symtab)
835 {
836 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000837 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000838 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000839 if (num_matches)
840 {
841 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000842 const size_t end_functions_added_index = sc_list.GetSize();
843 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
844 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000845 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000846 // No functions were added, just symbols, so we can just append them
847 for (size_t i=0; i<num_matches; ++i)
848 {
849 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
850 SymbolType sym_type = sc.symbol->GetType();
851 if (sc.symbol && (sym_type == eSymbolTypeCode ||
852 sym_type == eSymbolTypeResolver))
853 sc_list.Append(sc);
854 }
855 }
856 else
857 {
858 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
859 FileAddrToIndexMap file_addr_to_index;
860 for (size_t i=start_size; i<end_functions_added_index; ++i)
861 {
862 const SymbolContext &sc = sc_list[i];
863 if (sc.block)
864 continue;
865 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
866 }
867
868 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
869 // Functions were added so we need to merge symbols into any
870 // existing function symbol contexts
871 for (size_t i=start_size; i<num_matches; ++i)
872 {
873 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
874 SymbolType sym_type = sc.symbol->GetType();
875 if (sc.symbol && (sym_type == eSymbolTypeCode ||
876 sym_type == eSymbolTypeResolver))
877 {
878 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
879 if (pos == end)
880 sc_list.Append(sc);
881 else
882 sc_list[pos->second].symbol = sc.symbol;
883 }
884 }
Greg Clayton931180e2011-01-27 06:44:37 +0000885 }
886 }
887 }
888 }
889 }
890 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891}
892
Richard Mittonf86248d2013-09-12 02:20:34 +0000893void
894Module::FindAddressesForLine (const lldb::TargetSP target_sp,
895 const FileSpec &file, uint32_t line,
896 Function *function,
897 std::vector<Address> &output_local, std::vector<Address> &output_extern)
898{
899 SearchFilterByModule filter(target_sp, m_file);
900 AddressResolverFileLine resolver(file, line, true);
901 resolver.ResolveAddress (filter);
902
903 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
904 {
905 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
906 Function *f = addr.CalculateSymbolContextFunction();
907 if (f && f == function)
908 output_local.push_back (addr);
909 else
910 output_extern.push_back (addr);
911 }
912}
913
Greg Claytonc7bece562013-01-25 18:06:21 +0000914size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000915Module::FindTypes_Impl (const SymbolContext& sc,
916 const ConstString &name,
917 const ClangNamespaceDecl *namespace_decl,
918 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000919 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000920 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000921{
922 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
923 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
924 {
925 SymbolVendor *symbols = GetSymbolVendor ();
926 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000927 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000928 }
929 return 0;
930}
931
Greg Claytonc7bece562013-01-25 18:06:21 +0000932size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000933Module::FindTypesInNamespace (const SymbolContext& sc,
934 const ConstString &type_name,
935 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000936 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000937 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000938{
Greg Clayton84db9102012-03-26 23:03:23 +0000939 const bool append = true;
940 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000941}
942
Greg Claytonb43165b2012-12-05 21:24:42 +0000943lldb::TypeSP
944Module::FindFirstType (const SymbolContext& sc,
945 const ConstString &name,
946 bool exact_match)
947{
948 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000949 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000950 if (num_matches)
951 return type_list.GetTypeAtIndex(0);
952 return TypeSP();
953}
954
955
Greg Claytonc7bece562013-01-25 18:06:21 +0000956size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000957Module::FindTypes (const SymbolContext& sc,
958 const ConstString &name,
959 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000960 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000961 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000962{
Greg Claytonc7bece562013-01-25 18:06:21 +0000963 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000964 const char *type_name_cstr = name.GetCString();
965 std::string type_scope;
966 std::string type_basename;
967 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000968 TypeClass type_class = eTypeClassAny;
969 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000970 {
Greg Clayton84db9102012-03-26 23:03:23 +0000971 // Check if "name" starts with "::" which means the qualified type starts
972 // from the root namespace and implies and exact match. The typenames we
973 // get back from clang do not start with "::" so we need to strip this off
974 // in order to get the qualfied names to match
975
976 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
977 {
978 type_scope.erase(0,2);
979 exact_match = true;
980 }
981 ConstString type_basename_const_str (type_basename.c_str());
982 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
983 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000984 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000985 num_matches = types.GetSize();
986 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000987 }
988 else
Greg Clayton84db9102012-03-26 23:03:23 +0000989 {
990 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000991 if (type_class != eTypeClassAny)
992 {
993 // The "type_name_cstr" will have been modified if we have a valid type class
994 // prefix (like "struct", "class", "union", "typedef" etc).
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +0000995 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
Greg Clayton7bc31332012-10-22 16:19:56 +0000996 types.RemoveMismatchedTypes (type_class);
997 num_matches = types.GetSize();
998 }
999 else
1000 {
1001 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
1002 }
Greg Clayton84db9102012-03-26 23:03:23 +00001003 }
1004
1005 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001006
1007}
1008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +00001010Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011{
1012 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001013 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014 {
1015 ObjectFile *obj_file = GetObjectFile ();
1016 if (obj_file != NULL)
1017 {
1018 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +00001019 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +00001020 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 }
1022 }
1023 return m_symfile_ap.get();
1024}
1025
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026void
1027Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
1028{
1029 // Container objects whose paths do not specify a file directly can call
1030 // this function to correct the file and object names.
1031 m_file = file;
1032 m_mod_time = file.GetModificationTime();
1033 m_object_name = object_name;
1034}
1035
1036const ArchSpec&
1037Module::GetArchitecture () const
1038{
1039 return m_arch;
1040}
1041
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001042std::string
1043Module::GetSpecificationDescription () const
1044{
1045 std::string spec(GetFileSpec().GetPath());
1046 if (m_object_name)
1047 {
1048 spec += '(';
1049 spec += m_object_name.GetCString();
1050 spec += ')';
1051 }
1052 return spec;
1053}
1054
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001056Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001057{
1058 Mutex::Locker locker (m_mutex);
1059
Greg Claytonc982b3d2011-11-28 01:45:00 +00001060 if (level >= eDescriptionLevelFull)
1061 {
1062 if (m_arch.IsValid())
1063 s->Printf("(%s) ", m_arch.GetArchitectureName());
1064 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001065
Greg Claytonc982b3d2011-11-28 01:45:00 +00001066 if (level == eDescriptionLevelBrief)
1067 {
1068 const char *filename = m_file.GetFilename().GetCString();
1069 if (filename)
1070 s->PutCString (filename);
1071 }
1072 else
1073 {
1074 char path[PATH_MAX];
1075 if (m_file.GetPath(path, sizeof(path)))
1076 s->PutCString(path);
1077 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001078
1079 const char *object_name = m_object_name.GetCString();
1080 if (object_name)
1081 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001082}
1083
1084void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001085Module::ReportError (const char *format, ...)
1086{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001087 if (format && format[0])
1088 {
1089 StreamString strm;
1090 strm.PutCString("error: ");
1091 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001092 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001093 va_list args;
1094 va_start (args, format);
1095 strm.PrintfVarArg(format, args);
1096 va_end (args);
1097
1098 const int format_len = strlen(format);
1099 if (format_len > 0)
1100 {
1101 const char last_char = format[format_len-1];
1102 if (last_char != '\n' || last_char != '\r')
1103 strm.EOL();
1104 }
1105 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1106
1107 }
1108}
1109
Greg Clayton1d609092012-07-12 22:51:12 +00001110bool
1111Module::FileHasChanged () const
1112{
1113 if (m_file_has_changed == false)
1114 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1115 return m_file_has_changed;
1116}
1117
Greg Claytone38a5ed2012-01-05 03:57:59 +00001118void
1119Module::ReportErrorIfModifyDetected (const char *format, ...)
1120{
Greg Clayton1d609092012-07-12 22:51:12 +00001121 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001122 {
Greg Clayton1d609092012-07-12 22:51:12 +00001123 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001124 {
Greg Clayton1d609092012-07-12 22:51:12 +00001125 m_first_file_changed_log = true;
1126 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001127 {
Greg Clayton1d609092012-07-12 22:51:12 +00001128 StreamString strm;
1129 strm.PutCString("error: the object file ");
1130 GetDescription(&strm, lldb::eDescriptionLevelFull);
1131 strm.PutCString (" has been modified\n");
1132
1133 va_list args;
1134 va_start (args, format);
1135 strm.PrintfVarArg(format, args);
1136 va_end (args);
1137
1138 const int format_len = strlen(format);
1139 if (format_len > 0)
1140 {
1141 const char last_char = format[format_len-1];
1142 if (last_char != '\n' || last_char != '\r')
1143 strm.EOL();
1144 }
1145 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1146 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001147 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001148 }
1149 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001150}
1151
1152void
1153Module::ReportWarning (const char *format, ...)
1154{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001155 if (format && format[0])
1156 {
1157 StreamString strm;
1158 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001159 GetDescription(&strm, lldb::eDescriptionLevelFull);
1160 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001161
1162 va_list args;
1163 va_start (args, format);
1164 strm.PrintfVarArg(format, args);
1165 va_end (args);
1166
1167 const int format_len = strlen(format);
1168 if (format_len > 0)
1169 {
1170 const char last_char = format[format_len-1];
1171 if (last_char != '\n' || last_char != '\r')
1172 strm.EOL();
1173 }
1174 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1175 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001176}
1177
1178void
1179Module::LogMessage (Log *log, const char *format, ...)
1180{
1181 if (log)
1182 {
1183 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001184 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001185 log_message.PutCString (": ");
1186 va_list args;
1187 va_start (args, format);
1188 log_message.PrintfVarArg (format, args);
1189 va_end (args);
1190 log->PutCString(log_message.GetString().c_str());
1191 }
1192}
1193
Greg Claytond61c0fc2012-04-23 22:55:20 +00001194void
1195Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1196{
1197 if (log)
1198 {
1199 StreamString log_message;
1200 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1201 log_message.PutCString (": ");
1202 va_list args;
1203 va_start (args, format);
1204 log_message.PrintfVarArg (format, args);
1205 va_end (args);
1206 if (log->GetVerbose())
1207 Host::Backtrace (log_message, 1024);
1208 log->PutCString(log_message.GetString().c_str());
1209 }
1210}
1211
Greg Claytonc982b3d2011-11-28 01:45:00 +00001212void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213Module::Dump(Stream *s)
1214{
1215 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001216 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001218 s->Printf("Module %s%s%s%s\n",
1219 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 m_object_name ? "(" : "",
1221 m_object_name ? m_object_name.GetCString() : "",
1222 m_object_name ? ")" : "");
1223
1224 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 if (objfile)
1228 objfile->Dump(s);
1229
1230 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 if (symbols)
1232 symbols->Dump(s);
1233
1234 s->IndentLess();
1235}
1236
1237
1238TypeList*
1239Module::GetTypeList ()
1240{
1241 SymbolVendor *symbols = GetSymbolVendor ();
1242 if (symbols)
1243 return &symbols->GetTypeList();
1244 return NULL;
1245}
1246
1247const ConstString &
1248Module::GetObjectName() const
1249{
1250 return m_object_name;
1251}
1252
1253ObjectFile *
1254Module::GetObjectFile()
1255{
1256 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001257 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 Timer scoped_timer(__PRETTY_FUNCTION__,
1260 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001261 DataBufferSP data_sp;
1262 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001263 const lldb::offset_t file_size = m_file.GetByteSize();
1264 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001265 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001266 m_did_load_objfile = true;
1267 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1268 &m_file,
1269 m_object_offset,
1270 file_size - m_object_offset,
1271 data_sp,
1272 data_offset);
1273 if (m_objfile_sp)
1274 {
1275 // Once we get the object file, update our module with the object file's
1276 // architecture since it might differ in vendor/os if some parts were
1277 // unknown.
1278 m_objfile_sp->GetArchitecture (m_arch);
1279 }
Greg Clayton593577a2011-09-21 03:57:31 +00001280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281 }
Greg Clayton762f7132011-09-18 18:59:15 +00001282 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283}
1284
Michael Sartaina7499c92013-07-01 19:45:50 +00001285SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001286Module::GetSectionList()
1287{
1288 // Populate m_unified_sections_ap with sections from objfile.
1289 if (m_sections_ap.get() == NULL)
1290 {
1291 ObjectFile *obj_file = GetObjectFile();
1292 if (obj_file)
1293 obj_file->CreateSections(*GetUnifiedSectionList());
1294 }
1295 return m_sections_ap.get();
1296}
1297
1298SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001299Module::GetUnifiedSectionList()
1300{
Greg Clayton3046e662013-07-10 01:23:25 +00001301 // Populate m_unified_sections_ap with sections from objfile.
1302 if (m_sections_ap.get() == NULL)
1303 m_sections_ap.reset(new SectionList());
1304 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001305}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306
1307const Symbol *
1308Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1309{
1310 Timer scoped_timer(__PRETTY_FUNCTION__,
1311 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1312 name.AsCString(),
1313 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001314 SymbolVendor* sym_vendor = GetSymbolVendor();
1315 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001317 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001319 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 }
1321 return NULL;
1322}
1323void
1324Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1325{
1326 // No need to protect this call using m_mutex all other method calls are
1327 // already thread safe.
1328
1329 size_t num_indices = symbol_indexes.size();
1330 if (num_indices > 0)
1331 {
1332 SymbolContext sc;
1333 CalculateSymbolContext (&sc);
1334 for (size_t i = 0; i < num_indices; i++)
1335 {
1336 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1337 if (sc.symbol)
1338 sc_list.Append (sc);
1339 }
1340 }
1341}
1342
1343size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001344Module::FindFunctionSymbols (const ConstString &name,
1345 uint32_t name_type_mask,
1346 SymbolContextList& sc_list)
1347{
1348 Timer scoped_timer(__PRETTY_FUNCTION__,
1349 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1350 name.AsCString(),
1351 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001352 SymbolVendor* sym_vendor = GetSymbolVendor();
1353 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001354 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001355 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001356 if (symtab)
1357 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1358 }
1359 return 0;
1360}
1361
1362size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001363Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364{
1365 // No need to protect this call using m_mutex all other method calls are
1366 // already thread safe.
1367
1368
1369 Timer scoped_timer(__PRETTY_FUNCTION__,
1370 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1371 name.AsCString(),
1372 symbol_type);
1373 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001374 SymbolVendor* sym_vendor = GetSymbolVendor();
1375 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001376 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001377 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 if (symtab)
1379 {
1380 std::vector<uint32_t> symbol_indexes;
1381 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1382 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1383 }
1384 }
1385 return sc_list.GetSize() - initial_size;
1386}
1387
1388size_t
1389Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1390{
1391 // No need to protect this call using m_mutex all other method calls are
1392 // already thread safe.
1393
1394 Timer scoped_timer(__PRETTY_FUNCTION__,
1395 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1396 regex.GetText(),
1397 symbol_type);
1398 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001399 SymbolVendor* sym_vendor = GetSymbolVendor();
1400 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001402 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 if (symtab)
1404 {
1405 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001406 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1408 }
1409 }
1410 return sc_list.GetSize() - initial_size;
1411}
1412
Greg Claytone01e07b2013-04-18 18:10:51 +00001413void
1414Module::SetSymbolFileFileSpec (const FileSpec &file)
1415{
Michael Sartaina7499c92013-07-01 19:45:50 +00001416 // Remove any sections in the unified section list that come from the current symbol vendor.
1417 if (m_symfile_ap)
1418 {
Greg Clayton3046e662013-07-10 01:23:25 +00001419 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001420 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1421 if (section_list && symbol_file)
1422 {
1423 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001424 // Make sure we have an object file and that the symbol vendor's objfile isn't
1425 // the same as the module's objfile before we remove any sections for it...
1426 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001427 {
1428 size_t num_sections = section_list->GetNumSections (0);
1429 for (size_t idx = num_sections; idx > 0; --idx)
1430 {
1431 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1432 if (section_sp->GetObjectFile() == obj_file)
1433 {
Greg Clayton3046e662013-07-10 01:23:25 +00001434 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001435 }
1436 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001437 }
1438 }
1439 }
1440
Greg Claytone01e07b2013-04-18 18:10:51 +00001441 m_symfile_spec = file;
1442 m_symfile_ap.reset();
1443 m_did_load_symbol_vendor = false;
1444}
1445
Jim Ingham5aee1622010-08-09 23:31:02 +00001446bool
1447Module::IsExecutable ()
1448{
1449 if (GetObjectFile() == NULL)
1450 return false;
1451 else
1452 return GetObjectFile()->IsExecutable();
1453}
1454
Jim Inghamb53cb272011-08-03 01:03:17 +00001455bool
1456Module::IsLoadedInTarget (Target *target)
1457{
1458 ObjectFile *obj_file = GetObjectFile();
1459 if (obj_file)
1460 {
Greg Clayton3046e662013-07-10 01:23:25 +00001461 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001462 if (sections != NULL)
1463 {
1464 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001465 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1466 {
1467 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1468 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1469 {
1470 return true;
1471 }
1472 }
1473 }
1474 }
1475 return false;
1476}
Enrico Granata17598482012-11-08 02:22:02 +00001477
1478bool
Enrico Granata97303392013-05-21 00:00:30 +00001479Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001480{
1481 if (!target)
1482 {
1483 error.SetErrorString("invalid destination Target");
1484 return false;
1485 }
1486
Enrico Granata397ddd52013-05-21 20:13:34 +00001487 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001488
Greg Clayton91c0e742013-01-11 23:44:27 +00001489 Debugger &debugger = target->GetDebugger();
1490 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1491 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001492 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001493
1494 PlatformSP platform_sp(target->GetPlatform());
1495
1496 if (!platform_sp)
1497 {
1498 error.SetErrorString("invalid Platform");
1499 return false;
1500 }
Enrico Granata17598482012-11-08 02:22:02 +00001501
Greg Claytonb9d88902013-03-23 00:50:58 +00001502 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1503 *this);
1504
1505
1506 const uint32_t num_specs = file_specs.GetSize();
1507 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001508 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001509 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1510 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001511 {
1512 for (uint32_t i=0; i<num_specs; ++i)
1513 {
1514 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1515 if (scripting_fspec && scripting_fspec.Exists())
1516 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001517 if (shoud_load == eLoadScriptFromSymFileFalse)
1518 return false;
1519 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001520 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001521 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001522 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1523 "this debug session:\n\n command script import \"%s\"\n\n"
1524 "To run all discovered debug scripts in this session:\n\n"
1525 " settings set target.load-script-from-symbol-file true\n",
1526 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1527 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001528 return false;
1529 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001530 StreamString scripting_stream;
1531 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001532 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001533 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001534 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1535 can_reload,
1536 init_lldb_globals,
1537 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001538 if (!did_load)
1539 return false;
1540 }
1541 }
1542 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001543 else
1544 {
1545 error.SetErrorString("invalid ScriptInterpreter");
1546 return false;
1547 }
Enrico Granata17598482012-11-08 02:22:02 +00001548 }
1549 }
1550 return true;
1551}
1552
1553bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001554Module::SetArchitecture (const ArchSpec &new_arch)
1555{
Greg Clayton64195a22011-02-23 00:35:02 +00001556 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001557 {
1558 m_arch = new_arch;
1559 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001560 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001561 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001562}
1563
Greg Claytonc9660542012-02-05 02:38:54 +00001564bool
Greg Clayton751caf62014-02-07 22:54:47 +00001565Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001566{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001567 ObjectFile *object_file = GetObjectFile();
1568 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001569 {
Greg Clayton751caf62014-02-07 22:54:47 +00001570 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001571 return true;
1572 }
1573 else
1574 {
1575 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001576 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001577 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001578}
1579
Greg Claytonb9a01b32012-02-26 05:51:37 +00001580
1581bool
1582Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1583{
1584 const UUID &uuid = module_ref.GetUUID();
1585
1586 if (uuid.IsValid())
1587 {
1588 // If the UUID matches, then nothing more needs to match...
1589 if (uuid == GetUUID())
1590 return true;
1591 else
1592 return false;
1593 }
1594
1595 const FileSpec &file_spec = module_ref.GetFileSpec();
1596 if (file_spec)
1597 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001598 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001599 return false;
1600 }
1601
1602 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1603 if (platform_file_spec)
1604 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001605 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001606 return false;
1607 }
1608
1609 const ArchSpec &arch = module_ref.GetArchitecture();
1610 if (arch.IsValid())
1611 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001612 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001613 return false;
1614 }
1615
1616 const ConstString &object_name = module_ref.GetObjectName();
1617 if (object_name)
1618 {
1619 if (object_name != GetObjectName())
1620 return false;
1621 }
1622 return true;
1623}
1624
Greg Claytond804d282012-03-15 21:01:31 +00001625bool
1626Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1627{
1628 Mutex::Locker locker (m_mutex);
1629 return m_source_mappings.FindFile (orig_spec, new_spec);
1630}
1631
Greg Claytonf9be6932012-03-19 22:22:41 +00001632bool
1633Module::RemapSourceFile (const char *path, std::string &new_path) const
1634{
1635 Mutex::Locker locker (m_mutex);
1636 return m_source_mappings.RemapPath(path, new_path);
1637}
1638
Enrico Granata3467d802012-09-04 18:47:54 +00001639uint32_t
1640Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1641{
1642 ObjectFile *obj_file = GetObjectFile();
1643 if (obj_file)
1644 return obj_file->GetVersion (versions, num_versions);
1645
1646 if (versions && num_versions)
1647 {
1648 for (uint32_t i=0; i<num_versions; ++i)
1649 versions[i] = UINT32_MAX;
1650 }
1651 return 0;
1652}
Greg Clayton43fe2172013-04-03 02:00:15 +00001653
1654void
1655Module::PrepareForFunctionNameLookup (const ConstString &name,
1656 uint32_t name_type_mask,
1657 ConstString &lookup_name,
1658 uint32_t &lookup_name_type_mask,
1659 bool &match_name_after_lookup)
1660{
1661 const char *name_cstr = name.GetCString();
1662 lookup_name_type_mask = eFunctionNameTypeNone;
1663 match_name_after_lookup = false;
1664 const char *base_name_start = NULL;
1665 const char *base_name_end = NULL;
1666
1667 if (name_type_mask & eFunctionNameTypeAuto)
1668 {
1669 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1670 lookup_name_type_mask = eFunctionNameTypeFull;
1671 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1672 lookup_name_type_mask = eFunctionNameTypeFull;
1673 else
1674 {
1675 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1676 lookup_name_type_mask |= eFunctionNameTypeSelector;
1677
Greg Clayton6ecb2322013-05-18 00:11:21 +00001678 CPPLanguageRuntime::MethodName cpp_method (name);
1679 llvm::StringRef basename (cpp_method.GetBasename());
1680 if (basename.empty())
1681 {
1682 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1683 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1684 }
1685 else
1686 {
1687 base_name_start = basename.data();
1688 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001689 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001690 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001691 }
1692 }
1693 else
1694 {
1695 lookup_name_type_mask = name_type_mask;
1696 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1697 {
1698 // If they've asked for a CPP method or function name and it can't be that, we don't
1699 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001700 CPPLanguageRuntime::MethodName cpp_method (name);
1701 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001702 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001703 llvm::StringRef basename (cpp_method.GetBasename());
1704 base_name_start = basename.data();
1705 base_name_end = base_name_start + basename.size();
1706
1707 if (!cpp_method.GetQualifiers().empty())
1708 {
1709 // There is a "const" or other qualifer following the end of the fucntion parens,
1710 // this can't be a eFunctionNameTypeBase
1711 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1712 if (lookup_name_type_mask == eFunctionNameTypeNone)
1713 return;
1714 }
1715 }
1716 else
1717 {
1718 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1719 {
1720 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1721 if (lookup_name_type_mask == eFunctionNameTypeNone)
1722 return;
1723 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001724 }
1725 }
1726
1727 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1728 {
1729 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1730 {
1731 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1732 if (lookup_name_type_mask == eFunctionNameTypeNone)
1733 return;
1734 }
1735 }
1736 }
1737
1738 if (base_name_start &&
1739 base_name_end &&
1740 base_name_start != name_cstr &&
1741 base_name_start < base_name_end)
1742 {
1743 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1744 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1745 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1746 // to true
1747 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1748 match_name_after_lookup = true;
1749 }
1750 else
1751 {
1752 // The name is already correct, just use the exact name as supplied, and we won't need
1753 // to check if any matches contain "name"
1754 lookup_name = name;
1755 match_name_after_lookup = false;
1756 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001757}
Greg Clayton23f8c952014-03-24 23:10:19 +00001758
1759ModuleSP
1760Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
1761{
1762 if (delegate_sp)
1763 {
1764 // Must create a module and place it into a shared pointer before
1765 // we can create an object file since it has a std::weak_ptr back
1766 // to the module, so we need to control the creation carefully in
1767 // this static function
1768 ModuleSP module_sp(new Module());
1769 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
1770 if (module_sp->m_objfile_sp)
1771 {
1772 // Once we get the object file, update our module with the object file's
1773 // architecture since it might differ in vendor/os if some parts were
1774 // unknown.
1775 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
1776 }
1777 return module_sp;
1778 }
1779 return ModuleSP();
1780}
1781