blob: ffc9f7f2dc9ce27247b14afe9337e56b1d8c2119 [file] [log] [blame]
Chris Lattner24943d22010-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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Enrico Granata146d9522012-11-08 02:22:02 +000012#include "lldb/Core/Error.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Module.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000014#include "lldb/Core/DataBuffer.h"
15#include "lldb/Core/DataBufferHeap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/ModuleList.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Clayton75d8c252011-11-28 01:45:00 +000021#include "lldb/Core/StreamString.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/Timer.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000023#include "lldb/Host/Host.h"
Enrico Granata146d9522012-11-08 02:22:02 +000024#include "lldb/Host/Symbols.h"
25#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/lldb-private-log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000028#include "lldb/Symbol/CompileUnit.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Symbol/SymbolContext.h"
31#include "lldb/Symbol/SymbolVendor.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000032#include "lldb/Target/Process.h"
33#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034
35using namespace lldb;
36using namespace lldb_private;
37
Greg Clayton899025f2011-08-09 00:01:09 +000038// Shared pointers to modules track module lifetimes in
39// targets and in the global module, but this collection
40// will track all module objects that are still alive
41typedef std::vector<Module *> ModuleCollection;
42
43static ModuleCollection &
44GetModuleCollection()
45{
Jim Inghamb67c2a22011-10-31 23:47:10 +000046 // This module collection needs to live past any module, so we could either make it a
47 // shared pointer in each module or just leak is. Since it is only an empty vector by
48 // the time all the modules have gone away, we just leak it for now. If we decide this
49 // is a big problem we can introduce a Finalize method that will tear everything down in
50 // a predictable order.
51
52 static ModuleCollection *g_module_collection = NULL;
53 if (g_module_collection == NULL)
54 g_module_collection = new ModuleCollection();
55
56 return *g_module_collection;
Greg Clayton899025f2011-08-09 00:01:09 +000057}
58
Greg Claytonc149c8b2012-01-27 18:08:35 +000059Mutex *
Greg Clayton899025f2011-08-09 00:01:09 +000060Module::GetAllocationModuleCollectionMutex()
61{
Greg Claytonc149c8b2012-01-27 18:08:35 +000062 // NOTE: The mutex below must be leaked since the global module list in
63 // the ModuleList class will get torn at some point, and we can't know
64 // if it will tear itself down before the "g_module_collection_mutex" below
65 // will. So we leak a Mutex object below to safeguard against that
66
67 static Mutex *g_module_collection_mutex = NULL;
68 if (g_module_collection_mutex == NULL)
69 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
70 return g_module_collection_mutex;
Greg Clayton899025f2011-08-09 00:01:09 +000071}
72
73size_t
74Module::GetNumberAllocatedModules ()
75{
76 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
77 return GetModuleCollection().size();
78}
79
80Module *
81Module::GetAllocatedModuleAtIndex (size_t idx)
82{
83 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
84 ModuleCollection &modules = GetModuleCollection();
85 if (idx < modules.size())
86 return modules[idx];
87 return NULL;
88}
Greg Clayton08205a62012-01-27 18:45:39 +000089#if 0
Greg Clayton899025f2011-08-09 00:01:09 +000090
Greg Clayton08205a62012-01-27 18:45:39 +000091// These functions help us to determine if modules are still loaded, yet don't require that
92// you have a command interpreter and can easily be called from an external debugger.
93namespace lldb {
Greg Clayton899025f2011-08-09 00:01:09 +000094
Greg Clayton08205a62012-01-27 18:45:39 +000095 void
96 ClearModuleInfo (void)
97 {
Greg Clayton860b9ea2012-04-09 20:22:01 +000098 const bool mandatory = true;
99 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton08205a62012-01-27 18:45:39 +0000100 }
101
102 void
103 DumpModuleInfo (void)
104 {
105 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
106 ModuleCollection &modules = GetModuleCollection();
107 const size_t count = modules.size();
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000108 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton08205a62012-01-27 18:45:39 +0000109 for (size_t i=0; i<count; ++i)
110 {
111
112 StreamString strm;
113 Module *module = modules[i];
114 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
115 module->GetDescription(&strm, eDescriptionLevelFull);
116 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
117 module,
118 in_shared_module_list,
119 (uint32_t)module->use_count(),
120 strm.GetString().c_str());
121 }
122 }
123}
124
125#endif
Greg Claytonf737d372012-10-08 22:41:53 +0000126
Greg Clayton444fe992012-02-26 05:51:37 +0000127Module::Module (const ModuleSpec &module_spec) :
128 m_mutex (Mutex::eMutexTypeRecursive),
129 m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
130 m_arch (module_spec.GetArchitecture()),
131 m_uuid (),
132 m_file (module_spec.GetFileSpec()),
133 m_platform_file(module_spec.GetPlatformFileSpec()),
134 m_symfile_spec (module_spec.GetSymbolFileSpec()),
135 m_object_name (module_spec.GetObjectName()),
136 m_object_offset (module_spec.GetObjectOffset()),
137 m_objfile_sp (),
138 m_symfile_ap (),
139 m_ast (),
Greg Clayton964deba2012-03-15 21:01:31 +0000140 m_source_mappings (),
Greg Clayton444fe992012-02-26 05:51:37 +0000141 m_did_load_objfile (false),
142 m_did_load_symbol_vendor (false),
143 m_did_parse_uuid (false),
144 m_did_init_ast (false),
145 m_is_dynamic_loader_module (false),
Greg Claytoncbacba12012-07-12 22:51:12 +0000146 m_file_has_changed (false),
147 m_first_file_changed_log (false)
Greg Clayton444fe992012-02-26 05:51:37 +0000148{
149 // Scope for locker below...
150 {
151 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
152 GetModuleCollection().push_back(this);
153 }
154
Greg Claytonf737d372012-10-08 22:41:53 +0000155 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Clayton444fe992012-02-26 05:51:37 +0000156 if (log)
157 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
158 this,
159 m_arch.GetArchitectureName(),
160 m_file.GetDirectory().AsCString(""),
161 m_file.GetFilename().AsCString(""),
162 m_object_name.IsEmpty() ? "" : "(",
163 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
164 m_object_name.IsEmpty() ? "" : ")");
165}
166
Greg Clayton3508c382012-02-24 01:59:29 +0000167Module::Module(const FileSpec& file_spec,
168 const ArchSpec& arch,
169 const ConstString *object_name,
170 off_t object_offset) :
Chris Lattner24943d22010-06-08 16:52:24 +0000171 m_mutex (Mutex::eMutexTypeRecursive),
172 m_mod_time (file_spec.GetModificationTime()),
173 m_arch (arch),
174 m_uuid (),
175 m_file (file_spec),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000176 m_platform_file(),
Greg Clayton3508c382012-02-24 01:59:29 +0000177 m_symfile_spec (),
Chris Lattner24943d22010-06-08 16:52:24 +0000178 m_object_name (),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000179 m_object_offset (object_offset),
Greg Claytone40b6422011-09-18 18:59:15 +0000180 m_objfile_sp (),
Greg Clayton236c1c72010-09-07 23:40:05 +0000181 m_symfile_ap (),
Greg Claytonb01000f2011-01-17 03:46:26 +0000182 m_ast (),
Greg Clayton964deba2012-03-15 21:01:31 +0000183 m_source_mappings (),
Greg Clayton236c1c72010-09-07 23:40:05 +0000184 m_did_load_objfile (false),
185 m_did_load_symbol_vendor (false),
186 m_did_parse_uuid (false),
Greg Claytonb01000f2011-01-17 03:46:26 +0000187 m_did_init_ast (false),
Greg Claytondf6dc882012-01-05 03:57:59 +0000188 m_is_dynamic_loader_module (false),
Greg Claytoncbacba12012-07-12 22:51:12 +0000189 m_file_has_changed (false),
190 m_first_file_changed_log (false)
Chris Lattner24943d22010-06-08 16:52:24 +0000191{
Greg Clayton899025f2011-08-09 00:01:09 +0000192 // Scope for locker below...
193 {
194 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
195 GetModuleCollection().push_back(this);
196 }
197
Chris Lattner24943d22010-06-08 16:52:24 +0000198 if (object_name)
199 m_object_name = *object_name;
Greg Claytonf737d372012-10-08 22:41:53 +0000200 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner24943d22010-06-08 16:52:24 +0000201 if (log)
202 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
203 this,
Greg Clayton940b1032011-02-23 00:35:02 +0000204 m_arch.GetArchitectureName(),
Chris Lattner24943d22010-06-08 16:52:24 +0000205 m_file.GetDirectory().AsCString(""),
206 m_file.GetFilename().AsCString(""),
207 m_object_name.IsEmpty() ? "" : "(",
208 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
209 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner24943d22010-06-08 16:52:24 +0000210}
211
212Module::~Module()
213{
Greg Clayton899025f2011-08-09 00:01:09 +0000214 // Scope for locker below...
215 {
216 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
217 ModuleCollection &modules = GetModuleCollection();
218 ModuleCollection::iterator end = modules.end();
219 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Claytonf737d372012-10-08 22:41:53 +0000220 assert (pos != end);
221 modules.erase(pos);
Greg Clayton899025f2011-08-09 00:01:09 +0000222 }
Greg Claytonf737d372012-10-08 22:41:53 +0000223 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner24943d22010-06-08 16:52:24 +0000224 if (log)
225 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
226 this,
Greg Clayton940b1032011-02-23 00:35:02 +0000227 m_arch.GetArchitectureName(),
Chris Lattner24943d22010-06-08 16:52:24 +0000228 m_file.GetDirectory().AsCString(""),
229 m_file.GetFilename().AsCString(""),
230 m_object_name.IsEmpty() ? "" : "(",
231 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
232 m_object_name.IsEmpty() ? "" : ")");
Greg Claytonb01000f2011-01-17 03:46:26 +0000233 // Release any auto pointers before we start tearing down our member
234 // variables since the object file and symbol files might need to make
235 // function calls back into this module object. The ordering is important
236 // here because symbol files can require the module object file. So we tear
237 // down the symbol file first, then the object file.
238 m_symfile_ap.reset();
Greg Claytone40b6422011-09-18 18:59:15 +0000239 m_objfile_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000240}
241
Greg Clayton6c5438b2012-02-24 21:55:59 +0000242ObjectFile *
243Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
244{
245 if (m_objfile_sp)
246 {
247 error.SetErrorString ("object file already exists");
248 }
249 else
250 {
251 Mutex::Locker locker (m_mutex);
252 if (process_sp)
253 {
Greg Clayton6c5438b2012-02-24 21:55:59 +0000254 m_did_load_objfile = true;
255 std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
256 Error readmem_error;
257 const size_t bytes_read = process_sp->ReadMemory (header_addr,
258 data_ap->GetBytes(),
259 data_ap->GetByteSize(),
260 readmem_error);
261 if (bytes_read == 512)
262 {
263 DataBufferSP data_sp(data_ap.release());
264 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
265 if (m_objfile_sp)
266 {
Greg Clayton808e92a2012-04-20 19:50:20 +0000267 StreamString s;
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000268 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton808e92a2012-04-20 19:50:20 +0000269 m_object_name.SetCString (s.GetData());
270
271 // Once we get the object file, update our module with the object file's
Greg Clayton6c5438b2012-02-24 21:55:59 +0000272 // architecture since it might differ in vendor/os if some parts were
273 // unknown.
274 m_objfile_sp->GetArchitecture (m_arch);
275 }
276 else
277 {
278 error.SetErrorString ("unable to find suitable object file plug-in");
279 }
280 }
281 else
282 {
283 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
284 }
285 }
286 else
287 {
288 error.SetErrorString ("invalid process");
289 }
290 }
291 return m_objfile_sp.get();
292}
293
Chris Lattner24943d22010-06-08 16:52:24 +0000294
Greg Clayton0467c782011-02-04 18:53:10 +0000295const lldb_private::UUID&
Chris Lattner24943d22010-06-08 16:52:24 +0000296Module::GetUUID()
297{
298 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +0000299 if (m_did_parse_uuid == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000300 {
301 ObjectFile * obj_file = GetObjectFile ();
302
303 if (obj_file != NULL)
304 {
305 obj_file->GetUUID(&m_uuid);
Greg Clayton236c1c72010-09-07 23:40:05 +0000306 m_did_parse_uuid = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000307 }
308 }
309 return m_uuid;
310}
311
Greg Claytonb01000f2011-01-17 03:46:26 +0000312ClangASTContext &
313Module::GetClangASTContext ()
314{
315 Mutex::Locker locker (m_mutex);
316 if (m_did_init_ast == false)
317 {
318 ObjectFile * objfile = GetObjectFile();
Greg Clayton395fc332011-02-15 21:59:32 +0000319 ArchSpec object_arch;
320 if (objfile && objfile->GetArchitecture(object_arch))
Greg Claytonb01000f2011-01-17 03:46:26 +0000321 {
322 m_did_init_ast = true;
Jason Molenda1b97a652012-10-16 20:45:49 +0000323
324 // LLVM wants this to be set to iOS or MacOSX; if we're working on
325 // a bare-boards type image, change the triple for llvm's benefit.
326 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
327 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
328 {
329 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
330 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
331 {
332 object_arch.GetTriple().setOS(llvm::Triple::IOS);
333 }
334 else
335 {
336 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
337 }
338 }
Greg Clayton395fc332011-02-15 21:59:32 +0000339 m_ast.SetArchitecture (object_arch);
Greg Claytonb01000f2011-01-17 03:46:26 +0000340 }
341 }
342 return m_ast;
343}
344
Chris Lattner24943d22010-06-08 16:52:24 +0000345void
346Module::ParseAllDebugSymbols()
347{
348 Mutex::Locker locker (m_mutex);
349 uint32_t num_comp_units = GetNumCompileUnits();
350 if (num_comp_units == 0)
351 return;
352
Greg Clayton02e210c2011-09-17 07:23:18 +0000353 SymbolContext sc;
Greg Clayton13d24fb2012-01-29 20:56:30 +0000354 sc.module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000355 uint32_t cu_idx;
356 SymbolVendor *symbols = GetSymbolVendor ();
357
358 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
359 {
360 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
361 if (sc.comp_unit)
362 {
363 sc.function = NULL;
364 symbols->ParseVariablesForContext(sc);
365
366 symbols->ParseCompileUnitFunctions(sc);
367
368 uint32_t func_idx;
369 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
370 {
371 symbols->ParseFunctionBlocks(sc);
372
373 // Parse the variables for this function and all its blocks
374 symbols->ParseVariablesForContext(sc);
375 }
376
377
378 // Parse all types for this compile unit
379 sc.function = NULL;
380 symbols->ParseTypes(sc);
381 }
382 }
383}
384
385void
386Module::CalculateSymbolContext(SymbolContext* sc)
387{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000388 sc->module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000389}
390
Greg Clayton3508c382012-02-24 01:59:29 +0000391ModuleSP
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000392Module::CalculateSymbolContextModule ()
393{
Greg Clayton3508c382012-02-24 01:59:29 +0000394 return shared_from_this();
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000395}
396
Chris Lattner24943d22010-06-08 16:52:24 +0000397void
398Module::DumpSymbolContext(Stream *s)
399{
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000400 s->Printf(", Module{%p}", this);
Chris Lattner24943d22010-06-08 16:52:24 +0000401}
402
403uint32_t
404Module::GetNumCompileUnits()
405{
406 Mutex::Locker locker (m_mutex);
407 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
408 SymbolVendor *symbols = GetSymbolVendor ();
409 if (symbols)
410 return symbols->GetNumCompileUnits();
411 return 0;
412}
413
414CompUnitSP
415Module::GetCompileUnitAtIndex (uint32_t index)
416{
417 Mutex::Locker locker (m_mutex);
418 uint32_t num_comp_units = GetNumCompileUnits ();
419 CompUnitSP cu_sp;
420
421 if (index < num_comp_units)
422 {
423 SymbolVendor *symbols = GetSymbolVendor ();
424 if (symbols)
425 cu_sp = symbols->GetCompileUnitAtIndex(index);
426 }
427 return cu_sp;
428}
429
Chris Lattner24943d22010-06-08 16:52:24 +0000430bool
431Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
432{
433 Mutex::Locker locker (m_mutex);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000434 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000435 ObjectFile* ofile = GetObjectFile();
436 if (ofile)
437 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
438 return false;
439}
440
441uint32_t
442Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
443{
444 Mutex::Locker locker (m_mutex);
445 uint32_t resolved_flags = 0;
446
447 // Clear the result symbol context in case we don't find anything
448 sc.Clear();
449
450 // Get the section from the section/offset address.
Greg Clayton3508c382012-02-24 01:59:29 +0000451 SectionSP section_sp (so_addr.GetSection());
Chris Lattner24943d22010-06-08 16:52:24 +0000452
453 // Make sure the section matches this module before we try and match anything
Greg Clayton3508c382012-02-24 01:59:29 +0000454 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner24943d22010-06-08 16:52:24 +0000455 {
456 // If the section offset based address resolved itself, then this
457 // is the right module.
Greg Clayton13d24fb2012-01-29 20:56:30 +0000458 sc.module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000459 resolved_flags |= eSymbolContextModule;
460
461 // Resolve the compile unit, function, block, line table or line
462 // entry if requested.
463 if (resolve_scope & eSymbolContextCompUnit ||
464 resolve_scope & eSymbolContextFunction ||
465 resolve_scope & eSymbolContextBlock ||
466 resolve_scope & eSymbolContextLineEntry )
467 {
468 SymbolVendor *symbols = GetSymbolVendor ();
469 if (symbols)
470 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
471 }
472
Jim Inghambddb7892010-08-31 23:51:36 +0000473 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
474 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner24943d22010-06-08 16:52:24 +0000475 {
476 ObjectFile* ofile = GetObjectFile();
477 if (ofile)
478 {
479 Symtab *symtab = ofile->GetSymtab();
480 if (symtab)
481 {
482 if (so_addr.IsSectionOffset())
483 {
484 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
485 if (sc.symbol)
486 resolved_flags |= eSymbolContextSymbol;
487 }
488 }
489 }
490 }
491 }
492 return resolved_flags;
493}
494
495uint32_t
Greg Clayton537a7a82010-10-20 20:54:39 +0000496Module::ResolveSymbolContextForFilePath
497(
498 const char *file_path,
499 uint32_t line,
500 bool check_inlines,
501 uint32_t resolve_scope,
502 SymbolContextList& sc_list
503)
Chris Lattner24943d22010-06-08 16:52:24 +0000504{
Greg Clayton537a7a82010-10-20 20:54:39 +0000505 FileSpec file_spec(file_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000506 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
507}
508
509uint32_t
510Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
511{
512 Mutex::Locker locker (m_mutex);
513 Timer scoped_timer(__PRETTY_FUNCTION__,
514 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
515 file_spec.GetDirectory().AsCString(""),
516 file_spec.GetDirectory() ? "/" : "",
517 file_spec.GetFilename().AsCString(""),
518 line,
519 check_inlines ? "yes" : "no",
520 resolve_scope);
521
522 const uint32_t initial_count = sc_list.GetSize();
523
524 SymbolVendor *symbols = GetSymbolVendor ();
525 if (symbols)
526 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
527
528 return sc_list.GetSize() - initial_count;
529}
530
531
532uint32_t
Sean Callanan3e80cd92011-10-12 02:08:07 +0000533Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner24943d22010-06-08 16:52:24 +0000534{
535 SymbolVendor *symbols = GetSymbolVendor ();
536 if (symbols)
Sean Callanan0fcec132011-10-13 01:49:10 +0000537 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner24943d22010-06-08 16:52:24 +0000538 return 0;
539}
540uint32_t
541Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
542{
543 SymbolVendor *symbols = GetSymbolVendor ();
544 if (symbols)
545 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
546 return 0;
547}
548
549uint32_t
Greg Clayton801417e2011-07-07 01:59:51 +0000550Module::FindCompileUnits (const FileSpec &path,
551 bool append,
552 SymbolContextList &sc_list)
553{
554 if (!append)
555 sc_list.Clear();
556
557 const uint32_t start_size = sc_list.GetSize();
558 const uint32_t num_compile_units = GetNumCompileUnits();
559 SymbolContext sc;
Greg Clayton13d24fb2012-01-29 20:56:30 +0000560 sc.module_sp = shared_from_this();
Greg Clayton801417e2011-07-07 01:59:51 +0000561 const bool compare_directory = path.GetDirectory();
562 for (uint32_t i=0; i<num_compile_units; ++i)
563 {
564 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Claytonc7057302012-04-23 22:00:21 +0000565 if (sc.comp_unit)
566 {
567 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
568 sc_list.Append(sc);
569 }
Greg Clayton801417e2011-07-07 01:59:51 +0000570 }
571 return sc_list.GetSize() - start_size;
572}
573
574uint32_t
Sean Callanan3e80cd92011-10-12 02:08:07 +0000575Module::FindFunctions (const ConstString &name,
576 const ClangNamespaceDecl *namespace_decl,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000577 uint32_t name_type_mask,
Sean Callanan302d78c2012-02-10 22:52:19 +0000578 bool include_symbols,
579 bool include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000580 bool append,
581 SymbolContextList& sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000582{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000583 if (!append)
584 sc_list.Clear();
585
586 const uint32_t start_size = sc_list.GetSize();
587
588 // Find all the functions (not symbols, but debug information functions...
Chris Lattner24943d22010-06-08 16:52:24 +0000589 SymbolVendor *symbols = GetSymbolVendor ();
590 if (symbols)
Sean Callanan302d78c2012-02-10 22:52:19 +0000591 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000592
593 // Now check our symbol table for symbols that are code symbols if requested
594 if (include_symbols)
595 {
596 ObjectFile *objfile = GetObjectFile();
597 if (objfile)
598 {
599 Symtab *symtab = objfile->GetSymtab();
600 if (symtab)
601 {
602 std::vector<uint32_t> symbol_indexes;
603 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
604 const uint32_t num_matches = symbol_indexes.size();
605 if (num_matches)
606 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000607 const bool merge_symbol_into_function = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000608 SymbolContext sc(this);
609 for (uint32_t i=0; i<num_matches; i++)
610 {
611 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton889fbd02011-03-26 19:14:58 +0000612 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000613 }
614 }
615 }
616 }
617 }
618 return sc_list.GetSize() - start_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000619}
620
621uint32_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000622Module::FindFunctions (const RegularExpression& regex,
Sean Callanan302d78c2012-02-10 22:52:19 +0000623 bool include_symbols,
624 bool include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000625 bool append,
626 SymbolContextList& sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000627{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000628 if (!append)
629 sc_list.Clear();
630
631 const uint32_t start_size = sc_list.GetSize();
632
Chris Lattner24943d22010-06-08 16:52:24 +0000633 SymbolVendor *symbols = GetSymbolVendor ();
634 if (symbols)
Sean Callanan302d78c2012-02-10 22:52:19 +0000635 symbols->FindFunctions(regex, include_inlines, append, sc_list);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000636 // Now check our symbol table for symbols that are code symbols if requested
637 if (include_symbols)
638 {
639 ObjectFile *objfile = GetObjectFile();
640 if (objfile)
641 {
642 Symtab *symtab = objfile->GetSymtab();
643 if (symtab)
644 {
645 std::vector<uint32_t> symbol_indexes;
646 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
647 const uint32_t num_matches = symbol_indexes.size();
648 if (num_matches)
649 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000650 const bool merge_symbol_into_function = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000651 SymbolContext sc(this);
652 for (uint32_t i=0; i<num_matches; i++)
653 {
654 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton889fbd02011-03-26 19:14:58 +0000655 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000656 }
657 }
658 }
659 }
660 }
661 return sc_list.GetSize() - start_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000662}
663
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000664uint32_t
Greg Claytondc0a38c2012-03-26 23:03:23 +0000665Module::FindTypes_Impl (const SymbolContext& sc,
666 const ConstString &name,
667 const ClangNamespaceDecl *namespace_decl,
668 bool append,
669 uint32_t max_matches,
670 TypeList& types)
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000671{
672 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
673 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
674 {
675 SymbolVendor *symbols = GetSymbolVendor ();
676 if (symbols)
Sean Callanan0fcec132011-10-13 01:49:10 +0000677 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000678 }
679 return 0;
680}
681
Greg Claytondc0a38c2012-03-26 23:03:23 +0000682uint32_t
683Module::FindTypesInNamespace (const SymbolContext& sc,
684 const ConstString &type_name,
685 const ClangNamespaceDecl *namespace_decl,
686 uint32_t max_matches,
687 TypeList& type_list)
Enrico Granata979e20d2011-07-29 19:53:35 +0000688{
Greg Claytondc0a38c2012-03-26 23:03:23 +0000689 const bool append = true;
690 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata979e20d2011-07-29 19:53:35 +0000691}
692
Greg Clayton0b93a752012-12-05 21:24:42 +0000693lldb::TypeSP
694Module::FindFirstType (const SymbolContext& sc,
695 const ConstString &name,
696 bool exact_match)
697{
698 TypeList type_list;
699 const uint32_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
700 if (num_matches)
701 return type_list.GetTypeAtIndex(0);
702 return TypeSP();
703}
704
705
Enrico Granata979e20d2011-07-29 19:53:35 +0000706uint32_t
Greg Claytondc0a38c2012-03-26 23:03:23 +0000707Module::FindTypes (const SymbolContext& sc,
708 const ConstString &name,
709 bool exact_match,
710 uint32_t max_matches,
711 TypeList& types)
Enrico Granata979e20d2011-07-29 19:53:35 +0000712{
Greg Claytondc0a38c2012-03-26 23:03:23 +0000713 uint32_t num_matches = 0;
714 const char *type_name_cstr = name.GetCString();
715 std::string type_scope;
716 std::string type_basename;
717 const bool append = true;
Greg Claytonb9bd4ee2012-10-22 16:19:56 +0000718 TypeClass type_class = eTypeClassAny;
719 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata979e20d2011-07-29 19:53:35 +0000720 {
Greg Claytondc0a38c2012-03-26 23:03:23 +0000721 // Check if "name" starts with "::" which means the qualified type starts
722 // from the root namespace and implies and exact match. The typenames we
723 // get back from clang do not start with "::" so we need to strip this off
724 // in order to get the qualfied names to match
725
726 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
727 {
728 type_scope.erase(0,2);
729 exact_match = true;
730 }
731 ConstString type_basename_const_str (type_basename.c_str());
732 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
733 {
Greg Claytonb9bd4ee2012-10-22 16:19:56 +0000734 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Claytondc0a38c2012-03-26 23:03:23 +0000735 num_matches = types.GetSize();
736 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000737 }
738 else
Greg Claytondc0a38c2012-03-26 23:03:23 +0000739 {
740 // The type is not in a namespace/class scope, just search for it by basename
Greg Claytonb9bd4ee2012-10-22 16:19:56 +0000741 if (type_class != eTypeClassAny)
742 {
743 // The "type_name_cstr" will have been modified if we have a valid type class
744 // prefix (like "struct", "class", "union", "typedef" etc).
745 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
746 types.RemoveMismatchedTypes (type_class);
747 num_matches = types.GetSize();
748 }
749 else
750 {
751 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
752 }
Greg Claytondc0a38c2012-03-26 23:03:23 +0000753 }
754
755 return num_matches;
Enrico Granata979e20d2011-07-29 19:53:35 +0000756
757}
758
Chris Lattner24943d22010-06-08 16:52:24 +0000759//uint32_t
760//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
761//{
762// Timer scoped_timer(__PRETTY_FUNCTION__);
763// SymbolVendor *symbols = GetSymbolVendor ();
764// if (symbols)
765// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
766// return 0;
767//
768//}
769
770SymbolVendor*
771Module::GetSymbolVendor (bool can_create)
772{
773 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +0000774 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000775 {
776 ObjectFile *obj_file = GetObjectFile ();
777 if (obj_file != NULL)
778 {
779 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton3508c382012-02-24 01:59:29 +0000780 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this()));
Greg Clayton236c1c72010-09-07 23:40:05 +0000781 m_did_load_symbol_vendor = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000782 }
783 }
784 return m_symfile_ap.get();
785}
786
Chris Lattner24943d22010-06-08 16:52:24 +0000787void
788Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
789{
790 // Container objects whose paths do not specify a file directly can call
791 // this function to correct the file and object names.
792 m_file = file;
793 m_mod_time = file.GetModificationTime();
794 m_object_name = object_name;
795}
796
797const ArchSpec&
798Module::GetArchitecture () const
799{
800 return m_arch;
801}
802
803void
Greg Clayton75d8c252011-11-28 01:45:00 +0000804Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Tice7826c882010-10-26 03:11:13 +0000805{
806 Mutex::Locker locker (m_mutex);
807
Greg Clayton75d8c252011-11-28 01:45:00 +0000808 if (level >= eDescriptionLevelFull)
809 {
810 if (m_arch.IsValid())
811 s->Printf("(%s) ", m_arch.GetArchitectureName());
812 }
Caroline Tice7826c882010-10-26 03:11:13 +0000813
Greg Clayton75d8c252011-11-28 01:45:00 +0000814 if (level == eDescriptionLevelBrief)
815 {
816 const char *filename = m_file.GetFilename().GetCString();
817 if (filename)
818 s->PutCString (filename);
819 }
820 else
821 {
822 char path[PATH_MAX];
823 if (m_file.GetPath(path, sizeof(path)))
824 s->PutCString(path);
825 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000826
827 const char *object_name = m_object_name.GetCString();
828 if (object_name)
829 s->Printf("(%s)", object_name);
Caroline Tice7826c882010-10-26 03:11:13 +0000830}
831
832void
Greg Clayton75d8c252011-11-28 01:45:00 +0000833Module::ReportError (const char *format, ...)
834{
Greg Claytondf6dc882012-01-05 03:57:59 +0000835 if (format && format[0])
836 {
837 StreamString strm;
838 strm.PutCString("error: ");
839 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton71299132012-01-11 01:59:18 +0000840 strm.PutChar (' ');
Greg Claytondf6dc882012-01-05 03:57:59 +0000841 va_list args;
842 va_start (args, format);
843 strm.PrintfVarArg(format, args);
844 va_end (args);
845
846 const int format_len = strlen(format);
847 if (format_len > 0)
848 {
849 const char last_char = format[format_len-1];
850 if (last_char != '\n' || last_char != '\r')
851 strm.EOL();
852 }
853 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
854
855 }
856}
857
Greg Claytoncbacba12012-07-12 22:51:12 +0000858bool
859Module::FileHasChanged () const
860{
861 if (m_file_has_changed == false)
862 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
863 return m_file_has_changed;
864}
865
Greg Claytondf6dc882012-01-05 03:57:59 +0000866void
867Module::ReportErrorIfModifyDetected (const char *format, ...)
868{
Greg Claytoncbacba12012-07-12 22:51:12 +0000869 if (m_first_file_changed_log == false)
Greg Claytondf6dc882012-01-05 03:57:59 +0000870 {
Greg Claytoncbacba12012-07-12 22:51:12 +0000871 if (FileHasChanged ())
Greg Claytondf6dc882012-01-05 03:57:59 +0000872 {
Greg Claytoncbacba12012-07-12 22:51:12 +0000873 m_first_file_changed_log = true;
874 if (format)
Greg Claytondf6dc882012-01-05 03:57:59 +0000875 {
Greg Claytoncbacba12012-07-12 22:51:12 +0000876 StreamString strm;
877 strm.PutCString("error: the object file ");
878 GetDescription(&strm, lldb::eDescriptionLevelFull);
879 strm.PutCString (" has been modified\n");
880
881 va_list args;
882 va_start (args, format);
883 strm.PrintfVarArg(format, args);
884 va_end (args);
885
886 const int format_len = strlen(format);
887 if (format_len > 0)
888 {
889 const char last_char = format[format_len-1];
890 if (last_char != '\n' || last_char != '\r')
891 strm.EOL();
892 }
893 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
894 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytondf6dc882012-01-05 03:57:59 +0000895 }
Greg Claytondf6dc882012-01-05 03:57:59 +0000896 }
897 }
Greg Clayton75d8c252011-11-28 01:45:00 +0000898}
899
900void
901Module::ReportWarning (const char *format, ...)
902{
Greg Claytondf6dc882012-01-05 03:57:59 +0000903 if (format && format[0])
904 {
905 StreamString strm;
906 strm.PutCString("warning: ");
Greg Clayton71299132012-01-11 01:59:18 +0000907 GetDescription(&strm, lldb::eDescriptionLevelFull);
908 strm.PutChar (' ');
Greg Claytondf6dc882012-01-05 03:57:59 +0000909
910 va_list args;
911 va_start (args, format);
912 strm.PrintfVarArg(format, args);
913 va_end (args);
914
915 const int format_len = strlen(format);
916 if (format_len > 0)
917 {
918 const char last_char = format[format_len-1];
919 if (last_char != '\n' || last_char != '\r')
920 strm.EOL();
921 }
922 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
923 }
Greg Clayton75d8c252011-11-28 01:45:00 +0000924}
925
926void
927Module::LogMessage (Log *log, const char *format, ...)
928{
929 if (log)
930 {
931 StreamString log_message;
Greg Clayton71299132012-01-11 01:59:18 +0000932 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Clayton75d8c252011-11-28 01:45:00 +0000933 log_message.PutCString (": ");
934 va_list args;
935 va_start (args, format);
936 log_message.PrintfVarArg (format, args);
937 va_end (args);
938 log->PutCString(log_message.GetString().c_str());
939 }
940}
941
Greg Clayton73a35712012-04-23 22:55:20 +0000942void
943Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
944{
945 if (log)
946 {
947 StreamString log_message;
948 GetDescription(&log_message, lldb::eDescriptionLevelFull);
949 log_message.PutCString (": ");
950 va_list args;
951 va_start (args, format);
952 log_message.PrintfVarArg (format, args);
953 va_end (args);
954 if (log->GetVerbose())
955 Host::Backtrace (log_message, 1024);
956 log->PutCString(log_message.GetString().c_str());
957 }
958}
959
Greg Clayton75d8c252011-11-28 01:45:00 +0000960void
Chris Lattner24943d22010-06-08 16:52:24 +0000961Module::Dump(Stream *s)
962{
963 Mutex::Locker locker (m_mutex);
Greg Clayton3fed8b92010-10-08 00:21:05 +0000964 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner24943d22010-06-08 16:52:24 +0000965 s->Indent();
966 s->Printf("Module %s/%s%s%s%s\n",
967 m_file.GetDirectory().AsCString(),
968 m_file.GetFilename().AsCString(),
969 m_object_name ? "(" : "",
970 m_object_name ? m_object_name.GetCString() : "",
971 m_object_name ? ")" : "");
972
973 s->IndentMore();
974 ObjectFile *objfile = GetObjectFile ();
975
976 if (objfile)
977 objfile->Dump(s);
978
979 SymbolVendor *symbols = GetSymbolVendor ();
980
981 if (symbols)
982 symbols->Dump(s);
983
984 s->IndentLess();
985}
986
987
988TypeList*
989Module::GetTypeList ()
990{
991 SymbolVendor *symbols = GetSymbolVendor ();
992 if (symbols)
993 return &symbols->GetTypeList();
994 return NULL;
995}
996
997const ConstString &
998Module::GetObjectName() const
999{
1000 return m_object_name;
1001}
1002
1003ObjectFile *
1004Module::GetObjectFile()
1005{
1006 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +00001007 if (m_did_load_objfile == false)
Chris Lattner24943d22010-06-08 16:52:24 +00001008 {
Greg Clayton236c1c72010-09-07 23:40:05 +00001009 m_did_load_objfile = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001010 Timer scoped_timer(__PRETTY_FUNCTION__,
1011 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Claytondb2dc2b2012-01-12 05:25:17 +00001012 DataBufferSP file_data_sp;
Greg Clayton3508c382012-02-24 01:59:29 +00001013 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1014 &m_file,
1015 m_object_offset,
1016 m_file.GetByteSize(),
1017 file_data_sp);
Greg Clayton6a64bbf2011-09-21 03:57:31 +00001018 if (m_objfile_sp)
1019 {
1020 // Once we get the object file, update our module with the object file's
1021 // architecture since it might differ in vendor/os if some parts were
1022 // unknown.
1023 m_objfile_sp->GetArchitecture (m_arch);
1024 }
Chris Lattner24943d22010-06-08 16:52:24 +00001025 }
Greg Claytone40b6422011-09-18 18:59:15 +00001026 return m_objfile_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001027}
1028
1029
1030const Symbol *
1031Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1032{
1033 Timer scoped_timer(__PRETTY_FUNCTION__,
1034 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1035 name.AsCString(),
1036 symbol_type);
1037 ObjectFile *objfile = GetObjectFile();
1038 if (objfile)
1039 {
1040 Symtab *symtab = objfile->GetSymtab();
1041 if (symtab)
Greg Clayton7c36fa02010-09-11 03:13:28 +00001042 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner24943d22010-06-08 16:52:24 +00001043 }
1044 return NULL;
1045}
1046void
1047Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1048{
1049 // No need to protect this call using m_mutex all other method calls are
1050 // already thread safe.
1051
1052 size_t num_indices = symbol_indexes.size();
1053 if (num_indices > 0)
1054 {
1055 SymbolContext sc;
1056 CalculateSymbolContext (&sc);
1057 for (size_t i = 0; i < num_indices; i++)
1058 {
1059 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1060 if (sc.symbol)
1061 sc_list.Append (sc);
1062 }
1063 }
1064}
1065
1066size_t
Sean Callananaa4a5532011-10-13 16:49:47 +00001067Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +00001068{
1069 // No need to protect this call using m_mutex all other method calls are
1070 // already thread safe.
1071
1072
1073 Timer scoped_timer(__PRETTY_FUNCTION__,
1074 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1075 name.AsCString(),
1076 symbol_type);
1077 const size_t initial_size = sc_list.GetSize();
1078 ObjectFile *objfile = GetObjectFile ();
1079 if (objfile)
1080 {
1081 Symtab *symtab = objfile->GetSymtab();
1082 if (symtab)
1083 {
1084 std::vector<uint32_t> symbol_indexes;
1085 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1086 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1087 }
1088 }
1089 return sc_list.GetSize() - initial_size;
1090}
1091
1092size_t
1093Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1094{
1095 // No need to protect this call using m_mutex all other method calls are
1096 // already thread safe.
1097
1098 Timer scoped_timer(__PRETTY_FUNCTION__,
1099 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1100 regex.GetText(),
1101 symbol_type);
1102 const size_t initial_size = sc_list.GetSize();
1103 ObjectFile *objfile = GetObjectFile ();
1104 if (objfile)
1105 {
1106 Symtab *symtab = objfile->GetSymtab();
1107 if (symtab)
1108 {
1109 std::vector<uint32_t> symbol_indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001110 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner24943d22010-06-08 16:52:24 +00001111 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1112 }
1113 }
1114 return sc_list.GetSize() - initial_size;
1115}
1116
1117const TimeValue &
1118Module::GetModificationTime () const
1119{
1120 return m_mod_time;
1121}
Jim Ingham7508e732010-08-09 23:31:02 +00001122
1123bool
1124Module::IsExecutable ()
1125{
1126 if (GetObjectFile() == NULL)
1127 return false;
1128 else
1129 return GetObjectFile()->IsExecutable();
1130}
1131
Jim Ingham51b11e02011-08-03 01:03:17 +00001132bool
1133Module::IsLoadedInTarget (Target *target)
1134{
1135 ObjectFile *obj_file = GetObjectFile();
1136 if (obj_file)
1137 {
1138 SectionList *sections = obj_file->GetSectionList();
1139 if (sections != NULL)
1140 {
1141 size_t num_sections = sections->GetSize();
Jim Ingham51b11e02011-08-03 01:03:17 +00001142 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1143 {
1144 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1145 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1146 {
1147 return true;
1148 }
1149 }
1150 }
1151 }
1152 return false;
1153}
Enrico Granata146d9522012-11-08 02:22:02 +00001154
1155bool
1156Module::LoadScriptingResourceInTarget (Target *target, Error& error)
1157{
1158 if (!target)
1159 {
1160 error.SetErrorString("invalid destination Target");
1161 return false;
1162 }
1163
1164 PlatformSP platform_sp(target->GetPlatform());
1165
1166 if (!platform_sp)
1167 {
1168 error.SetErrorString("invalid Platform");
1169 return false;
1170 }
1171
1172 ModuleSpec module_spec(GetFileSpec());
1173 FileSpec scripting_fspec = platform_sp->LocateExecutableScriptingResource(module_spec);
1174 Debugger &debugger(target->GetDebugger());
1175 if (scripting_fspec && scripting_fspec.Exists())
1176 {
1177 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1178 if (script_interpreter)
1179 {
1180 StreamString scripting_stream;
1181 scripting_fspec.Dump(&scripting_stream);
Jim Ingham14e71ec2012-12-07 17:43:38 +00001182 const bool can_reload = false;
1183 const bool init_lldb_globals = false;
1184 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), can_reload, init_lldb_globals, error);
Enrico Granata146d9522012-11-08 02:22:02 +00001185 if (!did_load)
1186 return false;
1187 }
1188 else
1189 {
1190 error.SetErrorString("invalid ScriptInterpreter");
1191 return false;
1192 }
1193 }
1194 return true;
1195}
1196
1197bool
Jim Ingham7508e732010-08-09 23:31:02 +00001198Module::SetArchitecture (const ArchSpec &new_arch)
1199{
Greg Clayton940b1032011-02-23 00:35:02 +00001200 if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001201 {
1202 m_arch = new_arch;
1203 return true;
Greg Clayton940b1032011-02-23 00:35:02 +00001204 }
1205 return m_arch == new_arch;
Jim Ingham7508e732010-08-09 23:31:02 +00001206}
1207
Greg Claytonb5a8f142012-02-05 02:38:54 +00001208bool
1209Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
1210{
Greg Clayton9ab696e2012-03-27 21:10:07 +00001211 size_t num_loaded_sections = 0;
1212 ObjectFile *objfile = GetObjectFile();
1213 if (objfile)
Greg Claytonb5a8f142012-02-05 02:38:54 +00001214 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00001215 SectionList *section_list = objfile->GetSectionList ();
Greg Claytonb5a8f142012-02-05 02:38:54 +00001216 if (section_list)
1217 {
1218 const size_t num_sections = section_list->GetSize();
1219 size_t sect_idx = 0;
1220 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1221 {
1222 // Iterate through the object file sections to find the
1223 // first section that starts of file offset zero and that
1224 // has bytes in the file...
Greg Clayton545762f2012-07-07 01:24:12 +00001225 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton9ab696e2012-03-27 21:10:07 +00001226 // Only load non-thread specific sections when given a slide
Greg Clayton545762f2012-07-07 01:24:12 +00001227 if (section_sp && !section_sp->IsThreadSpecific())
Greg Claytonb5a8f142012-02-05 02:38:54 +00001228 {
Greg Clayton545762f2012-07-07 01:24:12 +00001229 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset))
Greg Clayton9ab696e2012-03-27 21:10:07 +00001230 ++num_loaded_sections;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001231 }
1232 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00001233 }
1234 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00001235 changed = num_loaded_sections > 0;
1236 return num_loaded_sections > 0;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001237}
1238
Greg Clayton444fe992012-02-26 05:51:37 +00001239
1240bool
1241Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1242{
1243 const UUID &uuid = module_ref.GetUUID();
1244
1245 if (uuid.IsValid())
1246 {
1247 // If the UUID matches, then nothing more needs to match...
1248 if (uuid == GetUUID())
1249 return true;
1250 else
1251 return false;
1252 }
1253
1254 const FileSpec &file_spec = module_ref.GetFileSpec();
1255 if (file_spec)
1256 {
1257 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory()))
1258 return false;
1259 }
1260
1261 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1262 if (platform_file_spec)
1263 {
Greg Clayton58aa7152012-10-02 06:04:17 +00001264 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory()))
Greg Clayton444fe992012-02-26 05:51:37 +00001265 return false;
1266 }
1267
1268 const ArchSpec &arch = module_ref.GetArchitecture();
1269 if (arch.IsValid())
1270 {
1271 if (m_arch != arch)
1272 return false;
1273 }
1274
1275 const ConstString &object_name = module_ref.GetObjectName();
1276 if (object_name)
1277 {
1278 if (object_name != GetObjectName())
1279 return false;
1280 }
1281 return true;
1282}
1283
Greg Clayton964deba2012-03-15 21:01:31 +00001284bool
1285Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1286{
1287 Mutex::Locker locker (m_mutex);
1288 return m_source_mappings.FindFile (orig_spec, new_spec);
1289}
1290
Greg Clayton2418fdd2012-03-19 22:22:41 +00001291bool
1292Module::RemapSourceFile (const char *path, std::string &new_path) const
1293{
1294 Mutex::Locker locker (m_mutex);
1295 return m_source_mappings.RemapPath(path, new_path);
1296}
1297
Enrico Granataae2ae942012-09-04 18:47:54 +00001298uint32_t
1299Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1300{
1301 ObjectFile *obj_file = GetObjectFile();
1302 if (obj_file)
1303 return obj_file->GetVersion (versions, num_versions);
1304
1305 if (versions && num_versions)
1306 {
1307 for (uint32_t i=0; i<num_versions; ++i)
1308 versions[i] = UINT32_MAX;
1309 }
1310 return 0;
1311}