blob: 3c1dfbfb3c64ad7ae257509bf4e38e4f5f9d15e9 [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
Enrico Granata17598482012-11-08 02:22:02 +000012#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Module.h"
Greg Claytonc9660542012-02-05 02:38:54 +000014#include "lldb/Core/DataBuffer.h"
15#include "lldb/Core/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/ModuleList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000021#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000023#include "lldb/Host/Host.h"
Enrico Granata17598482012-11-08 02:22:02 +000024#include "lldb/Host/Symbols.h"
25#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/lldb-private-log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000028#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Symbol/SymbolContext.h"
31#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000032#include "lldb/Target/CPPLanguageRuntime.h"
33#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytonc9660542012-02-05 02:38:54 +000034#include "lldb/Target/Process.h"
35#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Greg Clayton65a03992011-08-09 00:01:09 +000040// Shared pointers to modules track module lifetimes in
41// targets and in the global module, but this collection
42// will track all module objects that are still alive
43typedef std::vector<Module *> ModuleCollection;
44
45static ModuleCollection &
46GetModuleCollection()
47{
Jim Ingham549f7372011-10-31 23:47:10 +000048 // This module collection needs to live past any module, so we could either make it a
49 // shared pointer in each module or just leak is. Since it is only an empty vector by
50 // the time all the modules have gone away, we just leak it for now. If we decide this
51 // is a big problem we can introduce a Finalize method that will tear everything down in
52 // a predictable order.
53
54 static ModuleCollection *g_module_collection = NULL;
55 if (g_module_collection == NULL)
56 g_module_collection = new ModuleCollection();
57
58 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000059}
60
Greg Claytonb26e6be2012-01-27 18:08:35 +000061Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000062Module::GetAllocationModuleCollectionMutex()
63{
Greg Claytonb26e6be2012-01-27 18:08:35 +000064 // NOTE: The mutex below must be leaked since the global module list in
65 // the ModuleList class will get torn at some point, and we can't know
66 // if it will tear itself down before the "g_module_collection_mutex" below
67 // will. So we leak a Mutex object below to safeguard against that
68
69 static Mutex *g_module_collection_mutex = NULL;
70 if (g_module_collection_mutex == NULL)
71 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
72 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000073}
74
75size_t
76Module::GetNumberAllocatedModules ()
77{
78 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
79 return GetModuleCollection().size();
80}
81
82Module *
83Module::GetAllocatedModuleAtIndex (size_t idx)
84{
85 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
86 ModuleCollection &modules = GetModuleCollection();
87 if (idx < modules.size())
88 return modules[idx];
89 return NULL;
90}
Greg Clayton29ad7b92012-01-27 18:45:39 +000091#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000092
Greg Clayton29ad7b92012-01-27 18:45:39 +000093// These functions help us to determine if modules are still loaded, yet don't require that
94// you have a command interpreter and can easily be called from an external debugger.
95namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +000096
Greg Clayton29ad7b92012-01-27 18:45:39 +000097 void
98 ClearModuleInfo (void)
99 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000100 const bool mandatory = true;
101 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000102 }
103
104 void
105 DumpModuleInfo (void)
106 {
107 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
108 ModuleCollection &modules = GetModuleCollection();
109 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000110 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000111 for (size_t i=0; i<count; ++i)
112 {
113
114 StreamString strm;
115 Module *module = modules[i];
116 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
117 module->GetDescription(&strm, eDescriptionLevelFull);
118 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
119 module,
120 in_shared_module_list,
121 (uint32_t)module->use_count(),
122 strm.GetString().c_str());
123 }
124 }
125}
126
127#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000128
Greg Claytonb9a01b32012-02-26 05:51:37 +0000129Module::Module (const ModuleSpec &module_spec) :
130 m_mutex (Mutex::eMutexTypeRecursive),
131 m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
132 m_arch (module_spec.GetArchitecture()),
133 m_uuid (),
134 m_file (module_spec.GetFileSpec()),
135 m_platform_file(module_spec.GetPlatformFileSpec()),
136 m_symfile_spec (module_spec.GetSymbolFileSpec()),
137 m_object_name (module_spec.GetObjectName()),
138 m_object_offset (module_spec.GetObjectOffset()),
139 m_objfile_sp (),
140 m_symfile_ap (),
141 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000142 m_source_mappings (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000143 m_did_load_objfile (false),
144 m_did_load_symbol_vendor (false),
145 m_did_parse_uuid (false),
146 m_did_init_ast (false),
147 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000148 m_file_has_changed (false),
149 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000150{
151 // Scope for locker below...
152 {
153 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
154 GetModuleCollection().push_back(this);
155 }
156
Greg Clayton5160ce52013-03-27 23:08:40 +0000157 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000158 if (log)
159 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
160 this,
161 m_arch.GetArchitectureName(),
162 m_file.GetDirectory().AsCString(""),
163 m_file.GetFilename().AsCString(""),
164 m_object_name.IsEmpty() ? "" : "(",
165 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
166 m_object_name.IsEmpty() ? "" : ")");
167}
168
Greg Claytone72dfb32012-02-24 01:59:29 +0000169Module::Module(const FileSpec& file_spec,
170 const ArchSpec& arch,
171 const ConstString *object_name,
172 off_t object_offset) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 m_mutex (Mutex::eMutexTypeRecursive),
174 m_mod_time (file_spec.GetModificationTime()),
175 m_arch (arch),
176 m_uuid (),
177 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000178 m_platform_file(),
Greg Claytone72dfb32012-02-24 01:59:29 +0000179 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000181 m_object_offset (object_offset),
Greg Clayton762f7132011-09-18 18:59:15 +0000182 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000183 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000184 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000185 m_source_mappings (),
Greg Claytone83e7312010-09-07 23:40:05 +0000186 m_did_load_objfile (false),
187 m_did_load_symbol_vendor (false),
188 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000189 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000190 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000191 m_file_has_changed (false),
192 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193{
Greg Clayton65a03992011-08-09 00:01:09 +0000194 // Scope for locker below...
195 {
196 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
197 GetModuleCollection().push_back(this);
198 }
199
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 if (object_name)
201 m_object_name = *object_name;
Greg Clayton5160ce52013-03-27 23:08:40 +0000202 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 if (log)
204 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
205 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000206 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 m_file.GetDirectory().AsCString(""),
208 m_file.GetFilename().AsCString(""),
209 m_object_name.IsEmpty() ? "" : "(",
210 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
211 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212}
213
214Module::~Module()
215{
Greg Clayton65a03992011-08-09 00:01:09 +0000216 // Scope for locker below...
217 {
218 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
219 ModuleCollection &modules = GetModuleCollection();
220 ModuleCollection::iterator end = modules.end();
221 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000222 assert (pos != end);
223 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000224 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000225 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 if (log)
227 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
228 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000229 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230 m_file.GetDirectory().AsCString(""),
231 m_file.GetFilename().AsCString(""),
232 m_object_name.IsEmpty() ? "" : "(",
233 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
234 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000235 // Release any auto pointers before we start tearing down our member
236 // variables since the object file and symbol files might need to make
237 // function calls back into this module object. The ordering is important
238 // here because symbol files can require the module object file. So we tear
239 // down the symbol file first, then the object file.
240 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000241 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000244ObjectFile *
245Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
246{
247 if (m_objfile_sp)
248 {
249 error.SetErrorString ("object file already exists");
250 }
251 else
252 {
253 Mutex::Locker locker (m_mutex);
254 if (process_sp)
255 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000256 m_did_load_objfile = true;
Greg Claytone01e07b2013-04-18 18:10:51 +0000257 STD_UNIQUE_PTR(DataBufferHeap) data_ap (new DataBufferHeap (512, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000258 Error readmem_error;
259 const size_t bytes_read = process_sp->ReadMemory (header_addr,
260 data_ap->GetBytes(),
261 data_ap->GetByteSize(),
262 readmem_error);
263 if (bytes_read == 512)
264 {
265 DataBufferSP data_sp(data_ap.release());
266 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
267 if (m_objfile_sp)
268 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000269 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000270 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000271 m_object_name.SetCString (s.GetData());
272
273 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000274 // architecture since it might differ in vendor/os if some parts were
275 // unknown.
276 m_objfile_sp->GetArchitecture (m_arch);
277 }
278 else
279 {
280 error.SetErrorString ("unable to find suitable object file plug-in");
281 }
282 }
283 else
284 {
285 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
286 }
287 }
288 else
289 {
290 error.SetErrorString ("invalid process");
291 }
292 }
293 return m_objfile_sp.get();
294}
295
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296
Greg Clayton60830262011-02-04 18:53:10 +0000297const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298Module::GetUUID()
299{
300 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000301 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 {
303 ObjectFile * obj_file = GetObjectFile ();
304
305 if (obj_file != NULL)
306 {
307 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000308 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 }
310 }
311 return m_uuid;
312}
313
Greg Clayton6beaaa62011-01-17 03:46:26 +0000314ClangASTContext &
315Module::GetClangASTContext ()
316{
317 Mutex::Locker locker (m_mutex);
318 if (m_did_init_ast == false)
319 {
320 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000321 ArchSpec object_arch;
322 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000323 {
324 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000325
326 // LLVM wants this to be set to iOS or MacOSX; if we're working on
327 // a bare-boards type image, change the triple for llvm's benefit.
328 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
329 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
330 {
331 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
332 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
333 {
334 object_arch.GetTriple().setOS(llvm::Triple::IOS);
335 }
336 else
337 {
338 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
339 }
340 }
Greg Clayton514487e2011-02-15 21:59:32 +0000341 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000342 }
343 }
344 return m_ast;
345}
346
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347void
348Module::ParseAllDebugSymbols()
349{
350 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000351 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 if (num_comp_units == 0)
353 return;
354
Greg Claytona2eee182011-09-17 07:23:18 +0000355 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000356 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 SymbolVendor *symbols = GetSymbolVendor ();
358
Greg Claytonc7bece562013-01-25 18:06:21 +0000359 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 {
361 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
362 if (sc.comp_unit)
363 {
364 sc.function = NULL;
365 symbols->ParseVariablesForContext(sc);
366
367 symbols->ParseCompileUnitFunctions(sc);
368
Greg Claytonc7bece562013-01-25 18:06:21 +0000369 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 +0000370 {
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 Claytone1cd1be2012-01-29 20:56:30 +0000388 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389}
390
Greg Claytone72dfb32012-02-24 01:59:29 +0000391ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000392Module::CalculateSymbolContextModule ()
393{
Greg Claytone72dfb32012-02-24 01:59:29 +0000394 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000395}
396
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397void
398Module::DumpSymbolContext(Stream *s)
399{
Jason Molendafd54b362011-09-20 21:44:10 +0000400 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
Greg Claytonc7bece562013-01-25 18:06:21 +0000403size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404Module::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
Greg Claytonc7bece562013-01-25 18:06:21 +0000415Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416{
417 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000418 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 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 Lattner30fdc8d2010-06-08 16:52:24 +0000430bool
431Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
432{
433 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000434 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Chris Lattner30fdc8d2010-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
Greg Clayton72310352013-02-23 04:12:47 +0000447 // Clear the result symbol context in case we don't find anything, but don't clear the target
448 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449
450 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000451 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
453 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000454 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 {
456 // If the section offset based address resolved itself, then this
457 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000458 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-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 Ingham680e1772010-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 Lattner30fdc8d2010-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 Clayton274060b2010-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 Lattner30fdc8d2010-06-08 16:52:24 +0000504{
Greg Clayton274060b2010-10-20 20:54:39 +0000505 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-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
Greg Claytonc7bece562013-01-25 18:06:21 +0000532size_t
533Module::FindGlobalVariables (const ConstString &name,
534 const ClangNamespaceDecl *namespace_decl,
535 bool append,
536 size_t max_matches,
537 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538{
539 SymbolVendor *symbols = GetSymbolVendor ();
540 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000541 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 return 0;
543}
Greg Claytonc7bece562013-01-25 18:06:21 +0000544
545size_t
546Module::FindGlobalVariables (const RegularExpression& regex,
547 bool append,
548 size_t max_matches,
549 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550{
551 SymbolVendor *symbols = GetSymbolVendor ();
552 if (symbols)
553 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
554 return 0;
555}
556
Greg Claytonc7bece562013-01-25 18:06:21 +0000557size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000558Module::FindCompileUnits (const FileSpec &path,
559 bool append,
560 SymbolContextList &sc_list)
561{
562 if (!append)
563 sc_list.Clear();
564
Greg Claytonc7bece562013-01-25 18:06:21 +0000565 const size_t start_size = sc_list.GetSize();
566 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000567 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000568 sc.module_sp = shared_from_this();
Greg Clayton644247c2011-07-07 01:59:51 +0000569 const bool compare_directory = path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000570 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000571 {
572 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000573 if (sc.comp_unit)
574 {
575 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
576 sc_list.Append(sc);
577 }
Greg Clayton644247c2011-07-07 01:59:51 +0000578 }
579 return sc_list.GetSize() - start_size;
580}
581
Greg Claytonc7bece562013-01-25 18:06:21 +0000582size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000583Module::FindFunctions (const ConstString &name,
584 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000585 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000586 bool include_symbols,
587 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000588 bool append,
589 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590{
Greg Clayton931180e2011-01-27 06:44:37 +0000591 if (!append)
592 sc_list.Clear();
593
Greg Clayton43fe2172013-04-03 02:00:15 +0000594 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000595
596 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000598
599 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000600 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000601 ConstString lookup_name;
602 uint32_t lookup_name_type_mask = 0;
603 bool match_name_after_lookup = false;
604 Module::PrepareForFunctionNameLookup (name,
605 name_type_mask,
606 lookup_name,
607 lookup_name_type_mask,
608 match_name_after_lookup);
609
610 if (symbols)
611 symbols->FindFunctions(lookup_name,
612 namespace_decl,
613 lookup_name_type_mask,
614 include_inlines,
615 append,
616 sc_list);
617
618 // Now check our symbol table for symbols that are code symbols if requested
619 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000620 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000621 ObjectFile *objfile = GetObjectFile();
622 if (objfile)
Greg Clayton931180e2011-01-27 06:44:37 +0000623 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000624 Symtab *symtab = objfile->GetSymtab();
625 if (symtab)
626 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
627 }
628 }
629
630 if (match_name_after_lookup)
631 {
632 SymbolContext sc;
633 size_t i = old_size;
634 while (i<sc_list.GetSize())
635 {
636 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000637 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000638 const char *func_name = sc.GetFunctionName().GetCString();
639 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000640 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000641 // Remove the current context
642 sc_list.RemoveContextAtIndex(i);
643 // Don't increment i and continue in the loop
644 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000645 }
646 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000647 ++i;
648 }
649 }
650
651 }
652 else
653 {
654 if (symbols)
655 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
656
657 // Now check our symbol table for symbols that are code symbols if requested
658 if (include_symbols)
659 {
660 ObjectFile *objfile = GetObjectFile();
661 if (objfile)
662 {
663 Symtab *symtab = objfile->GetSymtab();
664 if (symtab)
665 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000666 }
667 }
668 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000669
670 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671}
672
Greg Claytonc7bece562013-01-25 18:06:21 +0000673size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000674Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000675 bool include_symbols,
676 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000677 bool append,
678 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679{
Greg Clayton931180e2011-01-27 06:44:37 +0000680 if (!append)
681 sc_list.Clear();
682
Greg Claytonc7bece562013-01-25 18:06:21 +0000683 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000684
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 SymbolVendor *symbols = GetSymbolVendor ();
686 if (symbols)
Sean Callanan9df05fb2012-02-10 22:52:19 +0000687 symbols->FindFunctions(regex, include_inlines, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000688 // Now check our symbol table for symbols that are code symbols if requested
689 if (include_symbols)
690 {
691 ObjectFile *objfile = GetObjectFile();
692 if (objfile)
693 {
694 Symtab *symtab = objfile->GetSymtab();
695 if (symtab)
696 {
697 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000698 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000699 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000700 if (num_matches)
701 {
Greg Clayton357132e2011-03-26 19:14:58 +0000702 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000703 SymbolContext sc(this);
Greg Claytonc7bece562013-01-25 18:06:21 +0000704 for (size_t i=0; i<num_matches; i++)
Greg Clayton931180e2011-01-27 06:44:37 +0000705 {
706 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Matt Kopec00049b82013-02-27 20:13:38 +0000707 SymbolType sym_type = sc.symbol->GetType();
708 if (sc.symbol && (sym_type == eSymbolTypeCode ||
709 sym_type == eSymbolTypeResolver))
710 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000711 }
712 }
713 }
714 }
715 }
716 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717}
718
Greg Claytonc7bece562013-01-25 18:06:21 +0000719size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000720Module::FindTypes_Impl (const SymbolContext& sc,
721 const ConstString &name,
722 const ClangNamespaceDecl *namespace_decl,
723 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000724 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000725 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000726{
727 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
728 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
729 {
730 SymbolVendor *symbols = GetSymbolVendor ();
731 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000732 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000733 }
734 return 0;
735}
736
Greg Claytonc7bece562013-01-25 18:06:21 +0000737size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000738Module::FindTypesInNamespace (const SymbolContext& sc,
739 const ConstString &type_name,
740 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000741 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000742 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000743{
Greg Clayton84db9102012-03-26 23:03:23 +0000744 const bool append = true;
745 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000746}
747
Greg Claytonb43165b2012-12-05 21:24:42 +0000748lldb::TypeSP
749Module::FindFirstType (const SymbolContext& sc,
750 const ConstString &name,
751 bool exact_match)
752{
753 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000754 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000755 if (num_matches)
756 return type_list.GetTypeAtIndex(0);
757 return TypeSP();
758}
759
760
Greg Claytonc7bece562013-01-25 18:06:21 +0000761size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000762Module::FindTypes (const SymbolContext& sc,
763 const ConstString &name,
764 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000765 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000766 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000767{
Greg Claytonc7bece562013-01-25 18:06:21 +0000768 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000769 const char *type_name_cstr = name.GetCString();
770 std::string type_scope;
771 std::string type_basename;
772 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000773 TypeClass type_class = eTypeClassAny;
774 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000775 {
Greg Clayton84db9102012-03-26 23:03:23 +0000776 // Check if "name" starts with "::" which means the qualified type starts
777 // from the root namespace and implies and exact match. The typenames we
778 // get back from clang do not start with "::" so we need to strip this off
779 // in order to get the qualfied names to match
780
781 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
782 {
783 type_scope.erase(0,2);
784 exact_match = true;
785 }
786 ConstString type_basename_const_str (type_basename.c_str());
787 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
788 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000789 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000790 num_matches = types.GetSize();
791 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000792 }
793 else
Greg Clayton84db9102012-03-26 23:03:23 +0000794 {
795 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000796 if (type_class != eTypeClassAny)
797 {
798 // The "type_name_cstr" will have been modified if we have a valid type class
799 // prefix (like "struct", "class", "union", "typedef" etc).
800 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
801 types.RemoveMismatchedTypes (type_class);
802 num_matches = types.GetSize();
803 }
804 else
805 {
806 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
807 }
Greg Clayton84db9102012-03-26 23:03:23 +0000808 }
809
810 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000811
812}
813
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000815Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816{
817 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000818 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 {
820 ObjectFile *obj_file = GetObjectFile ();
821 if (obj_file != NULL)
822 {
823 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +0000824 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +0000825 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
827 }
828 return m_symfile_ap.get();
829}
830
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831void
832Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
833{
834 // Container objects whose paths do not specify a file directly can call
835 // this function to correct the file and object names.
836 m_file = file;
837 m_mod_time = file.GetModificationTime();
838 m_object_name = object_name;
839}
840
841const ArchSpec&
842Module::GetArchitecture () const
843{
844 return m_arch;
845}
846
847void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000848Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000849{
850 Mutex::Locker locker (m_mutex);
851
Greg Claytonc982b3d2011-11-28 01:45:00 +0000852 if (level >= eDescriptionLevelFull)
853 {
854 if (m_arch.IsValid())
855 s->Printf("(%s) ", m_arch.GetArchitectureName());
856 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000857
Greg Claytonc982b3d2011-11-28 01:45:00 +0000858 if (level == eDescriptionLevelBrief)
859 {
860 const char *filename = m_file.GetFilename().GetCString();
861 if (filename)
862 s->PutCString (filename);
863 }
864 else
865 {
866 char path[PATH_MAX];
867 if (m_file.GetPath(path, sizeof(path)))
868 s->PutCString(path);
869 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000870
871 const char *object_name = m_object_name.GetCString();
872 if (object_name)
873 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000874}
875
876void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000877Module::ReportError (const char *format, ...)
878{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000879 if (format && format[0])
880 {
881 StreamString strm;
882 strm.PutCString("error: ");
883 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000884 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000885 va_list args;
886 va_start (args, format);
887 strm.PrintfVarArg(format, args);
888 va_end (args);
889
890 const int format_len = strlen(format);
891 if (format_len > 0)
892 {
893 const char last_char = format[format_len-1];
894 if (last_char != '\n' || last_char != '\r')
895 strm.EOL();
896 }
897 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
898
899 }
900}
901
Greg Clayton1d609092012-07-12 22:51:12 +0000902bool
903Module::FileHasChanged () const
904{
905 if (m_file_has_changed == false)
906 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
907 return m_file_has_changed;
908}
909
Greg Claytone38a5ed2012-01-05 03:57:59 +0000910void
911Module::ReportErrorIfModifyDetected (const char *format, ...)
912{
Greg Clayton1d609092012-07-12 22:51:12 +0000913 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000914 {
Greg Clayton1d609092012-07-12 22:51:12 +0000915 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +0000916 {
Greg Clayton1d609092012-07-12 22:51:12 +0000917 m_first_file_changed_log = true;
918 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000919 {
Greg Clayton1d609092012-07-12 22:51:12 +0000920 StreamString strm;
921 strm.PutCString("error: the object file ");
922 GetDescription(&strm, lldb::eDescriptionLevelFull);
923 strm.PutCString (" has been modified\n");
924
925 va_list args;
926 va_start (args, format);
927 strm.PrintfVarArg(format, args);
928 va_end (args);
929
930 const int format_len = strlen(format);
931 if (format_len > 0)
932 {
933 const char last_char = format[format_len-1];
934 if (last_char != '\n' || last_char != '\r')
935 strm.EOL();
936 }
937 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
938 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +0000939 }
Greg Claytone38a5ed2012-01-05 03:57:59 +0000940 }
941 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000942}
943
944void
945Module::ReportWarning (const char *format, ...)
946{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000947 if (format && format[0])
948 {
949 StreamString strm;
950 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +0000951 GetDescription(&strm, lldb::eDescriptionLevelFull);
952 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000953
954 va_list args;
955 va_start (args, format);
956 strm.PrintfVarArg(format, args);
957 va_end (args);
958
959 const int format_len = strlen(format);
960 if (format_len > 0)
961 {
962 const char last_char = format[format_len-1];
963 if (last_char != '\n' || last_char != '\r')
964 strm.EOL();
965 }
966 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
967 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000968}
969
970void
971Module::LogMessage (Log *log, const char *format, ...)
972{
973 if (log)
974 {
975 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +0000976 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +0000977 log_message.PutCString (": ");
978 va_list args;
979 va_start (args, format);
980 log_message.PrintfVarArg (format, args);
981 va_end (args);
982 log->PutCString(log_message.GetString().c_str());
983 }
984}
985
Greg Claytond61c0fc2012-04-23 22:55:20 +0000986void
987Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
988{
989 if (log)
990 {
991 StreamString log_message;
992 GetDescription(&log_message, lldb::eDescriptionLevelFull);
993 log_message.PutCString (": ");
994 va_list args;
995 va_start (args, format);
996 log_message.PrintfVarArg (format, args);
997 va_end (args);
998 if (log->GetVerbose())
999 Host::Backtrace (log_message, 1024);
1000 log->PutCString(log_message.GetString().c_str());
1001 }
1002}
1003
Greg Claytonc982b3d2011-11-28 01:45:00 +00001004void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005Module::Dump(Stream *s)
1006{
1007 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001008 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 s->Indent();
1010 s->Printf("Module %s/%s%s%s%s\n",
1011 m_file.GetDirectory().AsCString(),
1012 m_file.GetFilename().AsCString(),
1013 m_object_name ? "(" : "",
1014 m_object_name ? m_object_name.GetCString() : "",
1015 m_object_name ? ")" : "");
1016
1017 s->IndentMore();
1018 ObjectFile *objfile = GetObjectFile ();
1019
1020 if (objfile)
1021 objfile->Dump(s);
1022
1023 SymbolVendor *symbols = GetSymbolVendor ();
1024
1025 if (symbols)
1026 symbols->Dump(s);
1027
1028 s->IndentLess();
1029}
1030
1031
1032TypeList*
1033Module::GetTypeList ()
1034{
1035 SymbolVendor *symbols = GetSymbolVendor ();
1036 if (symbols)
1037 return &symbols->GetTypeList();
1038 return NULL;
1039}
1040
1041const ConstString &
1042Module::GetObjectName() const
1043{
1044 return m_object_name;
1045}
1046
1047ObjectFile *
1048Module::GetObjectFile()
1049{
1050 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001051 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 {
Greg Claytone83e7312010-09-07 23:40:05 +00001053 m_did_load_objfile = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 Timer scoped_timer(__PRETTY_FUNCTION__,
1055 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001056 DataBufferSP data_sp;
1057 lldb::offset_t data_offset = 0;
1058 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001059 &m_file,
1060 m_object_offset,
1061 m_file.GetByteSize(),
Greg Clayton5ce9c562013-02-06 17:22:03 +00001062 data_sp,
1063 data_offset);
Greg Clayton593577a2011-09-21 03:57:31 +00001064 if (m_objfile_sp)
1065 {
1066 // Once we get the object file, update our module with the object file's
1067 // architecture since it might differ in vendor/os if some parts were
1068 // unknown.
1069 m_objfile_sp->GetArchitecture (m_arch);
1070 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071 }
Greg Clayton762f7132011-09-18 18:59:15 +00001072 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073}
1074
1075
1076const Symbol *
1077Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1078{
1079 Timer scoped_timer(__PRETTY_FUNCTION__,
1080 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1081 name.AsCString(),
1082 symbol_type);
1083 ObjectFile *objfile = GetObjectFile();
1084 if (objfile)
1085 {
1086 Symtab *symtab = objfile->GetSymtab();
1087 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001088 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
1090 return NULL;
1091}
1092void
1093Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, 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 size_t num_indices = symbol_indexes.size();
1099 if (num_indices > 0)
1100 {
1101 SymbolContext sc;
1102 CalculateSymbolContext (&sc);
1103 for (size_t i = 0; i < num_indices; i++)
1104 {
1105 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1106 if (sc.symbol)
1107 sc_list.Append (sc);
1108 }
1109 }
1110}
1111
1112size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001113Module::FindFunctionSymbols (const ConstString &name,
1114 uint32_t name_type_mask,
1115 SymbolContextList& sc_list)
1116{
1117 Timer scoped_timer(__PRETTY_FUNCTION__,
1118 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1119 name.AsCString(),
1120 name_type_mask);
1121 ObjectFile *objfile = GetObjectFile ();
1122 if (objfile)
1123 {
1124 Symtab *symtab = objfile->GetSymtab();
1125 if (symtab)
1126 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1127 }
1128 return 0;
1129}
1130
1131size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001132Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133{
1134 // No need to protect this call using m_mutex all other method calls are
1135 // already thread safe.
1136
1137
1138 Timer scoped_timer(__PRETTY_FUNCTION__,
1139 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1140 name.AsCString(),
1141 symbol_type);
1142 const size_t initial_size = sc_list.GetSize();
1143 ObjectFile *objfile = GetObjectFile ();
1144 if (objfile)
1145 {
1146 Symtab *symtab = objfile->GetSymtab();
1147 if (symtab)
1148 {
1149 std::vector<uint32_t> symbol_indexes;
1150 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1151 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1152 }
1153 }
1154 return sc_list.GetSize() - initial_size;
1155}
1156
1157size_t
1158Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1159{
1160 // No need to protect this call using m_mutex all other method calls are
1161 // already thread safe.
1162
1163 Timer scoped_timer(__PRETTY_FUNCTION__,
1164 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1165 regex.GetText(),
1166 symbol_type);
1167 const size_t initial_size = sc_list.GetSize();
1168 ObjectFile *objfile = GetObjectFile ();
1169 if (objfile)
1170 {
1171 Symtab *symtab = objfile->GetSymtab();
1172 if (symtab)
1173 {
1174 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001175 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1177 }
1178 }
1179 return sc_list.GetSize() - initial_size;
1180}
1181
1182const TimeValue &
1183Module::GetModificationTime () const
1184{
1185 return m_mod_time;
1186}
Jim Ingham5aee1622010-08-09 23:31:02 +00001187
Greg Claytone01e07b2013-04-18 18:10:51 +00001188void
1189Module::SetSymbolFileFileSpec (const FileSpec &file)
1190{
1191 m_symfile_spec = file;
1192 m_symfile_ap.reset();
1193 m_did_load_symbol_vendor = false;
1194}
1195
1196
Jim Ingham5aee1622010-08-09 23:31:02 +00001197bool
1198Module::IsExecutable ()
1199{
1200 if (GetObjectFile() == NULL)
1201 return false;
1202 else
1203 return GetObjectFile()->IsExecutable();
1204}
1205
Jim Inghamb53cb272011-08-03 01:03:17 +00001206bool
1207Module::IsLoadedInTarget (Target *target)
1208{
1209 ObjectFile *obj_file = GetObjectFile();
1210 if (obj_file)
1211 {
1212 SectionList *sections = obj_file->GetSectionList();
1213 if (sections != NULL)
1214 {
1215 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001216 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1217 {
1218 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1219 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1220 {
1221 return true;
1222 }
1223 }
1224 }
1225 }
1226 return false;
1227}
Enrico Granata17598482012-11-08 02:22:02 +00001228
1229bool
1230Module::LoadScriptingResourceInTarget (Target *target, Error& error)
1231{
1232 if (!target)
1233 {
1234 error.SetErrorString("invalid destination Target");
1235 return false;
1236 }
1237
Greg Clayton91c0e742013-01-11 23:44:27 +00001238 Debugger &debugger = target->GetDebugger();
1239 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1240 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001241 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001242
1243 PlatformSP platform_sp(target->GetPlatform());
1244
1245 if (!platform_sp)
1246 {
1247 error.SetErrorString("invalid Platform");
1248 return false;
1249 }
Enrico Granata17598482012-11-08 02:22:02 +00001250
Greg Claytonb9d88902013-03-23 00:50:58 +00001251 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1252 *this);
1253
1254
1255 const uint32_t num_specs = file_specs.GetSize();
1256 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001257 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001258 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1259 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001260 {
1261 for (uint32_t i=0; i<num_specs; ++i)
1262 {
1263 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1264 if (scripting_fspec && scripting_fspec.Exists())
1265 {
1266
1267 StreamString scripting_stream;
1268 scripting_fspec.Dump(&scripting_stream);
1269 const bool can_reload = false;
1270 const bool init_lldb_globals = false;
1271 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), can_reload, init_lldb_globals, error);
1272 if (!did_load)
1273 return false;
1274 }
1275 }
1276 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001277 else
1278 {
1279 error.SetErrorString("invalid ScriptInterpreter");
1280 return false;
1281 }
Enrico Granata17598482012-11-08 02:22:02 +00001282 }
1283 }
1284 return true;
1285}
1286
1287bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001288Module::SetArchitecture (const ArchSpec &new_arch)
1289{
Greg Clayton64195a22011-02-23 00:35:02 +00001290 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001291 {
1292 m_arch = new_arch;
1293 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001294 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001295 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001296}
1297
Greg Claytonc9660542012-02-05 02:38:54 +00001298bool
1299Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
1300{
Greg Clayton741f3f92012-03-27 21:10:07 +00001301 size_t num_loaded_sections = 0;
1302 ObjectFile *objfile = GetObjectFile();
1303 if (objfile)
Greg Claytonc9660542012-02-05 02:38:54 +00001304 {
Greg Clayton741f3f92012-03-27 21:10:07 +00001305 SectionList *section_list = objfile->GetSectionList ();
Greg Claytonc9660542012-02-05 02:38:54 +00001306 if (section_list)
1307 {
1308 const size_t num_sections = section_list->GetSize();
1309 size_t sect_idx = 0;
1310 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1311 {
1312 // Iterate through the object file sections to find the
1313 // first section that starts of file offset zero and that
1314 // has bytes in the file...
Greg Clayton7820bd12012-07-07 01:24:12 +00001315 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton741f3f92012-03-27 21:10:07 +00001316 // Only load non-thread specific sections when given a slide
Greg Clayton7820bd12012-07-07 01:24:12 +00001317 if (section_sp && !section_sp->IsThreadSpecific())
Greg Claytonc9660542012-02-05 02:38:54 +00001318 {
Greg Clayton7820bd12012-07-07 01:24:12 +00001319 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset))
Greg Clayton741f3f92012-03-27 21:10:07 +00001320 ++num_loaded_sections;
Greg Claytonc9660542012-02-05 02:38:54 +00001321 }
1322 }
Greg Claytonc9660542012-02-05 02:38:54 +00001323 }
1324 }
Greg Clayton741f3f92012-03-27 21:10:07 +00001325 changed = num_loaded_sections > 0;
1326 return num_loaded_sections > 0;
Greg Claytonc9660542012-02-05 02:38:54 +00001327}
1328
Greg Claytonb9a01b32012-02-26 05:51:37 +00001329
1330bool
1331Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1332{
1333 const UUID &uuid = module_ref.GetUUID();
1334
1335 if (uuid.IsValid())
1336 {
1337 // If the UUID matches, then nothing more needs to match...
1338 if (uuid == GetUUID())
1339 return true;
1340 else
1341 return false;
1342 }
1343
1344 const FileSpec &file_spec = module_ref.GetFileSpec();
1345 if (file_spec)
1346 {
1347 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory()))
1348 return false;
1349 }
1350
1351 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1352 if (platform_file_spec)
1353 {
Greg Clayton548e9a32012-10-02 06:04:17 +00001354 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001355 return false;
1356 }
1357
1358 const ArchSpec &arch = module_ref.GetArchitecture();
1359 if (arch.IsValid())
1360 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001361 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001362 return false;
1363 }
1364
1365 const ConstString &object_name = module_ref.GetObjectName();
1366 if (object_name)
1367 {
1368 if (object_name != GetObjectName())
1369 return false;
1370 }
1371 return true;
1372}
1373
Greg Claytond804d282012-03-15 21:01:31 +00001374bool
1375Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1376{
1377 Mutex::Locker locker (m_mutex);
1378 return m_source_mappings.FindFile (orig_spec, new_spec);
1379}
1380
Greg Claytonf9be6932012-03-19 22:22:41 +00001381bool
1382Module::RemapSourceFile (const char *path, std::string &new_path) const
1383{
1384 Mutex::Locker locker (m_mutex);
1385 return m_source_mappings.RemapPath(path, new_path);
1386}
1387
Enrico Granata3467d802012-09-04 18:47:54 +00001388uint32_t
1389Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1390{
1391 ObjectFile *obj_file = GetObjectFile();
1392 if (obj_file)
1393 return obj_file->GetVersion (versions, num_versions);
1394
1395 if (versions && num_versions)
1396 {
1397 for (uint32_t i=0; i<num_versions; ++i)
1398 versions[i] = UINT32_MAX;
1399 }
1400 return 0;
1401}
Greg Clayton43fe2172013-04-03 02:00:15 +00001402
1403void
1404Module::PrepareForFunctionNameLookup (const ConstString &name,
1405 uint32_t name_type_mask,
1406 ConstString &lookup_name,
1407 uint32_t &lookup_name_type_mask,
1408 bool &match_name_after_lookup)
1409{
1410 const char *name_cstr = name.GetCString();
1411 lookup_name_type_mask = eFunctionNameTypeNone;
1412 match_name_after_lookup = false;
1413 const char *base_name_start = NULL;
1414 const char *base_name_end = NULL;
1415
1416 if (name_type_mask & eFunctionNameTypeAuto)
1417 {
1418 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1419 lookup_name_type_mask = eFunctionNameTypeFull;
1420 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1421 lookup_name_type_mask = eFunctionNameTypeFull;
1422 else
1423 {
1424 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1425 lookup_name_type_mask |= eFunctionNameTypeSelector;
1426
1427 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
1428 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1429 }
1430 }
1431 else
1432 {
1433 lookup_name_type_mask = name_type_mask;
1434 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1435 {
1436 // If they've asked for a CPP method or function name and it can't be that, we don't
1437 // even need to search for CPP methods or names.
1438 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
1439 {
1440 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1441 if (lookup_name_type_mask == eFunctionNameTypeNone)
1442 return;
1443 }
1444 }
1445
1446 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1447 {
1448 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1449 {
1450 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1451 if (lookup_name_type_mask == eFunctionNameTypeNone)
1452 return;
1453 }
1454 }
1455 }
1456
1457 if (base_name_start &&
1458 base_name_end &&
1459 base_name_start != name_cstr &&
1460 base_name_start < base_name_end)
1461 {
1462 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1463 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1464 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1465 // to true
1466 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1467 match_name_after_lookup = true;
1468 }
1469 else
1470 {
1471 // The name is already correct, just use the exact name as supplied, and we won't need
1472 // to check if any matches contain "name"
1473 lookup_name = name;
1474 match_name_after_lookup = false;
1475 }
1476}