blob: d5758c09b1e26bb5ab74ece76bd6f55baa9a1052 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Richard Mittonf86248d2013-09-12 02:20:34 +000012#include "lldb/Core/AddressResolverFileLine.h"
Enrico Granata17598482012-11-08 02:22:02 +000013#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Greg Claytonc9660542012-02-05 02:38:54 +000015#include "lldb/Core/DataBuffer.h"
16#include "lldb/Core/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/ModuleList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Section.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000022#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000024#include "lldb/Host/Host.h"
Enrico Granata17598482012-11-08 02:22:02 +000025#include "lldb/Host/Symbols.h"
26#include "lldb/Interpreter/CommandInterpreter.h"
27#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/lldb-private-log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000029#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Symbol/SymbolContext.h"
32#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000033#include "lldb/Target/CPPLanguageRuntime.h"
34#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytonc9660542012-02-05 02:38:54 +000035#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000036#include "lldb/Target/SectionLoadList.h"
Greg Claytonc9660542012-02-05 02:38:54 +000037#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000038#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Greg Clayton65a03992011-08-09 00:01:09 +000043// Shared pointers to modules track module lifetimes in
44// targets and in the global module, but this collection
45// will track all module objects that are still alive
46typedef std::vector<Module *> ModuleCollection;
47
48static ModuleCollection &
49GetModuleCollection()
50{
Jim Ingham549f7372011-10-31 23:47:10 +000051 // This module collection needs to live past any module, so we could either make it a
52 // shared pointer in each module or just leak is. Since it is only an empty vector by
53 // the time all the modules have gone away, we just leak it for now. If we decide this
54 // is a big problem we can introduce a Finalize method that will tear everything down in
55 // a predictable order.
56
57 static ModuleCollection *g_module_collection = NULL;
58 if (g_module_collection == NULL)
59 g_module_collection = new ModuleCollection();
60
61 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000062}
63
Greg Claytonb26e6be2012-01-27 18:08:35 +000064Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000065Module::GetAllocationModuleCollectionMutex()
66{
Greg Claytonb26e6be2012-01-27 18:08:35 +000067 // NOTE: The mutex below must be leaked since the global module list in
68 // the ModuleList class will get torn at some point, and we can't know
69 // if it will tear itself down before the "g_module_collection_mutex" below
70 // will. So we leak a Mutex object below to safeguard against that
71
72 static Mutex *g_module_collection_mutex = NULL;
73 if (g_module_collection_mutex == NULL)
74 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
75 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000076}
77
78size_t
79Module::GetNumberAllocatedModules ()
80{
81 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
82 return GetModuleCollection().size();
83}
84
85Module *
86Module::GetAllocatedModuleAtIndex (size_t idx)
87{
88 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
89 ModuleCollection &modules = GetModuleCollection();
90 if (idx < modules.size())
91 return modules[idx];
92 return NULL;
93}
Greg Clayton29ad7b92012-01-27 18:45:39 +000094#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000095
Greg Clayton29ad7b92012-01-27 18:45:39 +000096// These functions help us to determine if modules are still loaded, yet don't require that
97// you have a command interpreter and can easily be called from an external debugger.
98namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +000099
Greg Clayton29ad7b92012-01-27 18:45:39 +0000100 void
101 ClearModuleInfo (void)
102 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000103 const bool mandatory = true;
104 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000105 }
106
107 void
108 DumpModuleInfo (void)
109 {
110 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
111 ModuleCollection &modules = GetModuleCollection();
112 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000113 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000114 for (size_t i=0; i<count; ++i)
115 {
116
117 StreamString strm;
118 Module *module = modules[i];
119 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
120 module->GetDescription(&strm, eDescriptionLevelFull);
121 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
122 module,
123 in_shared_module_list,
124 (uint32_t)module->use_count(),
125 strm.GetString().c_str());
126 }
127 }
128}
129
130#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000131
Greg Claytonb9a01b32012-02-26 05:51:37 +0000132Module::Module (const ModuleSpec &module_spec) :
133 m_mutex (Mutex::eMutexTypeRecursive),
134 m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
135 m_arch (module_spec.GetArchitecture()),
136 m_uuid (),
137 m_file (module_spec.GetFileSpec()),
138 m_platform_file(module_spec.GetPlatformFileSpec()),
Greg Claytonfbb76342013-11-20 21:07:01 +0000139 m_remote_install_file(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000140 m_symfile_spec (module_spec.GetSymbolFileSpec()),
141 m_object_name (module_spec.GetObjectName()),
142 m_object_offset (module_spec.GetObjectOffset()),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000143 m_object_mod_time (module_spec.GetObjectModificationTime()),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000144 m_objfile_sp (),
145 m_symfile_ap (),
146 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000147 m_source_mappings (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000148 m_did_load_objfile (false),
149 m_did_load_symbol_vendor (false),
150 m_did_parse_uuid (false),
151 m_did_init_ast (false),
152 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000153 m_file_has_changed (false),
154 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000155{
156 // Scope for locker below...
157 {
158 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
159 GetModuleCollection().push_back(this);
160 }
161
Greg Clayton5160ce52013-03-27 23:08:40 +0000162 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000163 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000164 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Greg Claytonb9a01b32012-02-26 05:51:37 +0000165 this,
166 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000167 m_file.GetPath().c_str(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000168 m_object_name.IsEmpty() ? "" : "(",
169 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
170 m_object_name.IsEmpty() ? "" : ")");
171}
172
Greg Claytone72dfb32012-02-24 01:59:29 +0000173Module::Module(const FileSpec& file_spec,
174 const ArchSpec& arch,
175 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000176 off_t object_offset,
177 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 m_mutex (Mutex::eMutexTypeRecursive),
179 m_mod_time (file_spec.GetModificationTime()),
180 m_arch (arch),
181 m_uuid (),
182 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000183 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000184 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000185 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000187 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000188 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000189 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000190 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000191 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000192 m_source_mappings (),
Greg Claytone83e7312010-09-07 23:40:05 +0000193 m_did_load_objfile (false),
194 m_did_load_symbol_vendor (false),
195 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000196 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000197 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000198 m_file_has_changed (false),
199 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200{
Greg Clayton65a03992011-08-09 00:01:09 +0000201 // Scope for locker below...
202 {
203 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
204 GetModuleCollection().push_back(this);
205 }
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 if (object_name)
208 m_object_name = *object_name;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000209
210 if (object_mod_time_ptr)
211 m_object_mod_time = *object_mod_time_ptr;
212
Greg Clayton5160ce52013-03-27 23:08:40 +0000213 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000215 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000217 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000218 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219 m_object_name.IsEmpty() ? "" : "(",
220 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
221 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224Module::~Module()
225{
Greg Clayton217b28b2013-05-22 20:13:22 +0000226 // Lock our module down while we tear everything down to make sure
227 // we don't get any access to the module while it is being destroyed
228 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000229 // Scope for locker below...
230 {
231 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
232 ModuleCollection &modules = GetModuleCollection();
233 ModuleCollection::iterator end = modules.end();
234 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000235 assert (pos != end);
236 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000237 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000238 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000240 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000242 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000243 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 m_object_name.IsEmpty() ? "" : "(",
245 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
246 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000247 // Release any auto pointers before we start tearing down our member
248 // variables since the object file and symbol files might need to make
249 // function calls back into this module object. The ordering is important
250 // here because symbol files can require the module object file. So we tear
251 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000252 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000253 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000254 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000257ObjectFile *
258Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
259{
260 if (m_objfile_sp)
261 {
262 error.SetErrorString ("object file already exists");
263 }
264 else
265 {
266 Mutex::Locker locker (m_mutex);
267 if (process_sp)
268 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000269 m_did_load_objfile = true;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000270 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000271 Error readmem_error;
272 const size_t bytes_read = process_sp->ReadMemory (header_addr,
273 data_ap->GetBytes(),
274 data_ap->GetByteSize(),
275 readmem_error);
276 if (bytes_read == 512)
277 {
278 DataBufferSP data_sp(data_ap.release());
279 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
280 if (m_objfile_sp)
281 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000282 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000283 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000284 m_object_name.SetCString (s.GetData());
285
286 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000287 // architecture since it might differ in vendor/os if some parts were
288 // unknown.
289 m_objfile_sp->GetArchitecture (m_arch);
290 }
291 else
292 {
293 error.SetErrorString ("unable to find suitable object file plug-in");
294 }
295 }
296 else
297 {
298 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
299 }
300 }
301 else
302 {
303 error.SetErrorString ("invalid process");
304 }
305 }
306 return m_objfile_sp.get();
307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
Greg Clayton60830262011-02-04 18:53:10 +0000310const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311Module::GetUUID()
312{
313 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000314 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 {
316 ObjectFile * obj_file = GetObjectFile ();
317
318 if (obj_file != NULL)
319 {
320 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000321 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 }
323 }
324 return m_uuid;
325}
326
Greg Clayton6beaaa62011-01-17 03:46:26 +0000327ClangASTContext &
328Module::GetClangASTContext ()
329{
330 Mutex::Locker locker (m_mutex);
331 if (m_did_init_ast == false)
332 {
333 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000334 ArchSpec object_arch;
335 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000336 {
337 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000338
339 // LLVM wants this to be set to iOS or MacOSX; if we're working on
340 // a bare-boards type image, change the triple for llvm's benefit.
341 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
342 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
343 {
344 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
345 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
346 {
347 object_arch.GetTriple().setOS(llvm::Triple::IOS);
348 }
349 else
350 {
351 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
352 }
353 }
Greg Clayton514487e2011-02-15 21:59:32 +0000354 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000355 }
356 }
357 return m_ast;
358}
359
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360void
361Module::ParseAllDebugSymbols()
362{
363 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000364 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 if (num_comp_units == 0)
366 return;
367
Greg Claytona2eee182011-09-17 07:23:18 +0000368 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000369 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 SymbolVendor *symbols = GetSymbolVendor ();
371
Greg Claytonc7bece562013-01-25 18:06:21 +0000372 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 {
374 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
375 if (sc.comp_unit)
376 {
377 sc.function = NULL;
378 symbols->ParseVariablesForContext(sc);
379
380 symbols->ParseCompileUnitFunctions(sc);
381
Greg Claytonc7bece562013-01-25 18:06:21 +0000382 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 +0000383 {
384 symbols->ParseFunctionBlocks(sc);
385
386 // Parse the variables for this function and all its blocks
387 symbols->ParseVariablesForContext(sc);
388 }
389
390
391 // Parse all types for this compile unit
392 sc.function = NULL;
393 symbols->ParseTypes(sc);
394 }
395 }
396}
397
398void
399Module::CalculateSymbolContext(SymbolContext* sc)
400{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000401 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402}
403
Greg Claytone72dfb32012-02-24 01:59:29 +0000404ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000405Module::CalculateSymbolContextModule ()
406{
Greg Claytone72dfb32012-02-24 01:59:29 +0000407 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000408}
409
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410void
411Module::DumpSymbolContext(Stream *s)
412{
Jason Molendafd54b362011-09-20 21:44:10 +0000413 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Greg Claytonc7bece562013-01-25 18:06:21 +0000416size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417Module::GetNumCompileUnits()
418{
419 Mutex::Locker locker (m_mutex);
420 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
421 SymbolVendor *symbols = GetSymbolVendor ();
422 if (symbols)
423 return symbols->GetNumCompileUnits();
424 return 0;
425}
426
427CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000428Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429{
430 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000431 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 CompUnitSP cu_sp;
433
434 if (index < num_comp_units)
435 {
436 SymbolVendor *symbols = GetSymbolVendor ();
437 if (symbols)
438 cu_sp = symbols->GetCompileUnitAtIndex(index);
439 }
440 return cu_sp;
441}
442
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443bool
444Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
445{
446 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000447 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000448 SectionList *section_list = GetSectionList();
449 if (section_list)
450 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 return false;
452}
453
454uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000455Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
456 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457{
458 Mutex::Locker locker (m_mutex);
459 uint32_t resolved_flags = 0;
460
Greg Clayton72310352013-02-23 04:12:47 +0000461 // Clear the result symbol context in case we don't find anything, but don't clear the target
462 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
464 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000465 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
467 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000468 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 {
470 // If the section offset based address resolved itself, then this
471 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000472 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 resolved_flags |= eSymbolContextModule;
474
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000475 SymbolVendor* sym_vendor = GetSymbolVendor();
476 if (!sym_vendor)
477 return resolved_flags;
478
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 // Resolve the compile unit, function, block, line table or line
480 // entry if requested.
481 if (resolve_scope & eSymbolContextCompUnit ||
482 resolve_scope & eSymbolContextFunction ||
483 resolve_scope & eSymbolContextBlock ||
484 resolve_scope & eSymbolContextLineEntry )
485 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000486 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
488
Jim Ingham680e1772010-08-31 23:51:36 +0000489 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
490 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000492 Symtab *symtab = sym_vendor->GetSymtab();
493 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000495 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000496 if (!sc.symbol &&
497 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
498 {
499 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
500 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
501 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
502 }
503
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000504 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000505 {
506 if (sc.symbol->IsSynthetic())
507 {
508 // We have a synthetic symbol so lets check if the object file
509 // from the symbol file in the symbol vendor is different than
510 // the object file for the module, and if so search its symbol
511 // table to see if we can come up with a better symbol. For example
512 // dSYM files on MacOSX have an unstripped symbol table inside of
513 // them.
514 ObjectFile *symtab_objfile = symtab->GetObjectFile();
515 if (symtab_objfile && symtab_objfile->IsStripped())
516 {
517 SymbolFile *symfile = sym_vendor->GetSymbolFile();
518 if (symfile)
519 {
520 ObjectFile *symfile_objfile = symfile->GetObjectFile();
521 if (symfile_objfile != symtab_objfile)
522 {
523 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
524 if (symfile_symtab)
525 {
526 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
527 if (symbol && !symbol->IsSynthetic())
528 {
529 sc.symbol = symbol;
530 }
531 }
532 }
533 }
534 }
535 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000536 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000537 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000538 }
539 }
540
541 // For function symbols, so_addr may be off by one. This is a convention consistent
542 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
543 // symbol lookup for disassembly and unwind.
544 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000545 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000546 {
547 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000548 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000549
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000550 bool do_resolve_tail_call_address = false; // prevent recursion
551 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
552 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000553 if (flags & eSymbolContextSymbol)
554 {
555 AddressRange addr_range;
556 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000558 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000560 // If the requested address is one past the address range of a function (i.e. a tail call),
561 // or the decremented address is the start of a function (i.e. some forms of trampoline),
562 // indicate that the symbol has been resolved.
563 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
564 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
565 {
566 resolved_flags |= flags;
567 }
568 }
569 else
570 {
571 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 }
573 }
574 }
575 }
576 }
577 return resolved_flags;
578}
579
580uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000581Module::ResolveSymbolContextForFilePath
582(
583 const char *file_path,
584 uint32_t line,
585 bool check_inlines,
586 uint32_t resolve_scope,
587 SymbolContextList& sc_list
588)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589{
Greg Clayton274060b2010-10-20 20:54:39 +0000590 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
592}
593
594uint32_t
595Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
596{
597 Mutex::Locker locker (m_mutex);
598 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000599 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
600 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 line,
602 check_inlines ? "yes" : "no",
603 resolve_scope);
604
605 const uint32_t initial_count = sc_list.GetSize();
606
607 SymbolVendor *symbols = GetSymbolVendor ();
608 if (symbols)
609 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
610
611 return sc_list.GetSize() - initial_count;
612}
613
614
Greg Claytonc7bece562013-01-25 18:06:21 +0000615size_t
616Module::FindGlobalVariables (const ConstString &name,
617 const ClangNamespaceDecl *namespace_decl,
618 bool append,
619 size_t max_matches,
620 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621{
622 SymbolVendor *symbols = GetSymbolVendor ();
623 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000624 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 return 0;
626}
Greg Claytonc7bece562013-01-25 18:06:21 +0000627
628size_t
629Module::FindGlobalVariables (const RegularExpression& regex,
630 bool append,
631 size_t max_matches,
632 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633{
634 SymbolVendor *symbols = GetSymbolVendor ();
635 if (symbols)
636 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
637 return 0;
638}
639
Greg Claytonc7bece562013-01-25 18:06:21 +0000640size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000641Module::FindCompileUnits (const FileSpec &path,
642 bool append,
643 SymbolContextList &sc_list)
644{
645 if (!append)
646 sc_list.Clear();
647
Greg Claytonc7bece562013-01-25 18:06:21 +0000648 const size_t start_size = sc_list.GetSize();
649 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000650 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000651 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000652 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000653 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000654 {
655 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000656 if (sc.comp_unit)
657 {
658 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
659 sc_list.Append(sc);
660 }
Greg Clayton644247c2011-07-07 01:59:51 +0000661 }
662 return sc_list.GetSize() - start_size;
663}
664
Greg Claytonc7bece562013-01-25 18:06:21 +0000665size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000666Module::FindFunctions (const ConstString &name,
667 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000668 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000669 bool include_symbols,
670 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000671 bool append,
672 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673{
Greg Clayton931180e2011-01-27 06:44:37 +0000674 if (!append)
675 sc_list.Clear();
676
Greg Clayton43fe2172013-04-03 02:00:15 +0000677 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000678
679 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000681
682 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000683 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000684 ConstString lookup_name;
685 uint32_t lookup_name_type_mask = 0;
686 bool match_name_after_lookup = false;
687 Module::PrepareForFunctionNameLookup (name,
688 name_type_mask,
689 lookup_name,
690 lookup_name_type_mask,
691 match_name_after_lookup);
692
693 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000694 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000695 symbols->FindFunctions(lookup_name,
696 namespace_decl,
697 lookup_name_type_mask,
698 include_inlines,
699 append,
700 sc_list);
701
Michael Sartaina7499c92013-07-01 19:45:50 +0000702 // Now check our symbol table for symbols that are code symbols if requested
703 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000704 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000705 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000706 if (symtab)
707 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
708 }
709 }
710
711 if (match_name_after_lookup)
712 {
713 SymbolContext sc;
714 size_t i = old_size;
715 while (i<sc_list.GetSize())
716 {
717 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000718 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000719 const char *func_name = sc.GetFunctionName().GetCString();
720 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000721 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000722 // Remove the current context
723 sc_list.RemoveContextAtIndex(i);
724 // Don't increment i and continue in the loop
725 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000726 }
727 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000728 ++i;
729 }
730 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000731 }
732 else
733 {
734 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000735 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000736 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
737
Michael Sartaina7499c92013-07-01 19:45:50 +0000738 // Now check our symbol table for symbols that are code symbols if requested
739 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000740 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000741 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000742 if (symtab)
743 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000744 }
745 }
746 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000747
748 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749}
750
Greg Claytonc7bece562013-01-25 18:06:21 +0000751size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000752Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000753 bool include_symbols,
754 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000755 bool append,
756 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757{
Greg Clayton931180e2011-01-27 06:44:37 +0000758 if (!append)
759 sc_list.Clear();
760
Greg Claytonc7bece562013-01-25 18:06:21 +0000761 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000762
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 SymbolVendor *symbols = GetSymbolVendor ();
764 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000765 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000766 symbols->FindFunctions(regex, include_inlines, append, sc_list);
767
768 // Now check our symbol table for symbols that are code symbols if requested
769 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000770 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000771 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000772 if (symtab)
773 {
774 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000775 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000776 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000777 if (num_matches)
778 {
779 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000780 const size_t end_functions_added_index = sc_list.GetSize();
781 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
782 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000783 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000784 // No functions were added, just symbols, so we can just append them
785 for (size_t i=0; i<num_matches; ++i)
786 {
787 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
788 SymbolType sym_type = sc.symbol->GetType();
789 if (sc.symbol && (sym_type == eSymbolTypeCode ||
790 sym_type == eSymbolTypeResolver))
791 sc_list.Append(sc);
792 }
793 }
794 else
795 {
796 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
797 FileAddrToIndexMap file_addr_to_index;
798 for (size_t i=start_size; i<end_functions_added_index; ++i)
799 {
800 const SymbolContext &sc = sc_list[i];
801 if (sc.block)
802 continue;
803 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
804 }
805
806 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
807 // Functions were added so we need to merge symbols into any
808 // existing function symbol contexts
809 for (size_t i=start_size; i<num_matches; ++i)
810 {
811 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
812 SymbolType sym_type = sc.symbol->GetType();
813 if (sc.symbol && (sym_type == eSymbolTypeCode ||
814 sym_type == eSymbolTypeResolver))
815 {
816 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
817 if (pos == end)
818 sc_list.Append(sc);
819 else
820 sc_list[pos->second].symbol = sc.symbol;
821 }
822 }
Greg Clayton931180e2011-01-27 06:44:37 +0000823 }
824 }
825 }
826 }
827 }
828 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829}
830
Richard Mittonf86248d2013-09-12 02:20:34 +0000831void
832Module::FindAddressesForLine (const lldb::TargetSP target_sp,
833 const FileSpec &file, uint32_t line,
834 Function *function,
835 std::vector<Address> &output_local, std::vector<Address> &output_extern)
836{
837 SearchFilterByModule filter(target_sp, m_file);
838 AddressResolverFileLine resolver(file, line, true);
839 resolver.ResolveAddress (filter);
840
841 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
842 {
843 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
844 Function *f = addr.CalculateSymbolContextFunction();
845 if (f && f == function)
846 output_local.push_back (addr);
847 else
848 output_extern.push_back (addr);
849 }
850}
851
Greg Claytonc7bece562013-01-25 18:06:21 +0000852size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000853Module::FindTypes_Impl (const SymbolContext& sc,
854 const ConstString &name,
855 const ClangNamespaceDecl *namespace_decl,
856 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000857 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000858 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000859{
860 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
861 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
862 {
863 SymbolVendor *symbols = GetSymbolVendor ();
864 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000865 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000866 }
867 return 0;
868}
869
Greg Claytonc7bece562013-01-25 18:06:21 +0000870size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000871Module::FindTypesInNamespace (const SymbolContext& sc,
872 const ConstString &type_name,
873 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000874 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000875 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000876{
Greg Clayton84db9102012-03-26 23:03:23 +0000877 const bool append = true;
878 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000879}
880
Greg Claytonb43165b2012-12-05 21:24:42 +0000881lldb::TypeSP
882Module::FindFirstType (const SymbolContext& sc,
883 const ConstString &name,
884 bool exact_match)
885{
886 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000887 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000888 if (num_matches)
889 return type_list.GetTypeAtIndex(0);
890 return TypeSP();
891}
892
893
Greg Claytonc7bece562013-01-25 18:06:21 +0000894size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000895Module::FindTypes (const SymbolContext& sc,
896 const ConstString &name,
897 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000898 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000899 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000900{
Greg Claytonc7bece562013-01-25 18:06:21 +0000901 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000902 const char *type_name_cstr = name.GetCString();
903 std::string type_scope;
904 std::string type_basename;
905 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000906 TypeClass type_class = eTypeClassAny;
907 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000908 {
Greg Clayton84db9102012-03-26 23:03:23 +0000909 // Check if "name" starts with "::" which means the qualified type starts
910 // from the root namespace and implies and exact match. The typenames we
911 // get back from clang do not start with "::" so we need to strip this off
912 // in order to get the qualfied names to match
913
914 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
915 {
916 type_scope.erase(0,2);
917 exact_match = true;
918 }
919 ConstString type_basename_const_str (type_basename.c_str());
920 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
921 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000922 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000923 num_matches = types.GetSize();
924 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000925 }
926 else
Greg Clayton84db9102012-03-26 23:03:23 +0000927 {
928 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000929 if (type_class != eTypeClassAny)
930 {
931 // The "type_name_cstr" will have been modified if we have a valid type class
932 // prefix (like "struct", "class", "union", "typedef" etc).
933 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
934 types.RemoveMismatchedTypes (type_class);
935 num_matches = types.GetSize();
936 }
937 else
938 {
939 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
940 }
Greg Clayton84db9102012-03-26 23:03:23 +0000941 }
942
943 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000944
945}
946
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000948Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949{
950 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000951 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 {
953 ObjectFile *obj_file = GetObjectFile ();
954 if (obj_file != NULL)
955 {
956 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +0000957 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +0000958 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 }
960 }
961 return m_symfile_ap.get();
962}
963
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964void
965Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
966{
967 // Container objects whose paths do not specify a file directly can call
968 // this function to correct the file and object names.
969 m_file = file;
970 m_mod_time = file.GetModificationTime();
971 m_object_name = object_name;
972}
973
974const ArchSpec&
975Module::GetArchitecture () const
976{
977 return m_arch;
978}
979
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000980std::string
981Module::GetSpecificationDescription () const
982{
983 std::string spec(GetFileSpec().GetPath());
984 if (m_object_name)
985 {
986 spec += '(';
987 spec += m_object_name.GetCString();
988 spec += ')';
989 }
990 return spec;
991}
992
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000994Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000995{
996 Mutex::Locker locker (m_mutex);
997
Greg Claytonc982b3d2011-11-28 01:45:00 +0000998 if (level >= eDescriptionLevelFull)
999 {
1000 if (m_arch.IsValid())
1001 s->Printf("(%s) ", m_arch.GetArchitectureName());
1002 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001003
Greg Claytonc982b3d2011-11-28 01:45:00 +00001004 if (level == eDescriptionLevelBrief)
1005 {
1006 const char *filename = m_file.GetFilename().GetCString();
1007 if (filename)
1008 s->PutCString (filename);
1009 }
1010 else
1011 {
1012 char path[PATH_MAX];
1013 if (m_file.GetPath(path, sizeof(path)))
1014 s->PutCString(path);
1015 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001016
1017 const char *object_name = m_object_name.GetCString();
1018 if (object_name)
1019 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001020}
1021
1022void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001023Module::ReportError (const char *format, ...)
1024{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001025 if (format && format[0])
1026 {
1027 StreamString strm;
1028 strm.PutCString("error: ");
1029 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001030 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001031 va_list args;
1032 va_start (args, format);
1033 strm.PrintfVarArg(format, args);
1034 va_end (args);
1035
1036 const int format_len = strlen(format);
1037 if (format_len > 0)
1038 {
1039 const char last_char = format[format_len-1];
1040 if (last_char != '\n' || last_char != '\r')
1041 strm.EOL();
1042 }
1043 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1044
1045 }
1046}
1047
Greg Clayton1d609092012-07-12 22:51:12 +00001048bool
1049Module::FileHasChanged () const
1050{
1051 if (m_file_has_changed == false)
1052 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1053 return m_file_has_changed;
1054}
1055
Greg Claytone38a5ed2012-01-05 03:57:59 +00001056void
1057Module::ReportErrorIfModifyDetected (const char *format, ...)
1058{
Greg Clayton1d609092012-07-12 22:51:12 +00001059 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001060 {
Greg Clayton1d609092012-07-12 22:51:12 +00001061 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001062 {
Greg Clayton1d609092012-07-12 22:51:12 +00001063 m_first_file_changed_log = true;
1064 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001065 {
Greg Clayton1d609092012-07-12 22:51:12 +00001066 StreamString strm;
1067 strm.PutCString("error: the object file ");
1068 GetDescription(&strm, lldb::eDescriptionLevelFull);
1069 strm.PutCString (" has been modified\n");
1070
1071 va_list args;
1072 va_start (args, format);
1073 strm.PrintfVarArg(format, args);
1074 va_end (args);
1075
1076 const int format_len = strlen(format);
1077 if (format_len > 0)
1078 {
1079 const char last_char = format[format_len-1];
1080 if (last_char != '\n' || last_char != '\r')
1081 strm.EOL();
1082 }
1083 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1084 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001085 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001086 }
1087 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001088}
1089
1090void
1091Module::ReportWarning (const char *format, ...)
1092{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001093 if (format && format[0])
1094 {
1095 StreamString strm;
1096 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001097 GetDescription(&strm, lldb::eDescriptionLevelFull);
1098 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001099
1100 va_list args;
1101 va_start (args, format);
1102 strm.PrintfVarArg(format, args);
1103 va_end (args);
1104
1105 const int format_len = strlen(format);
1106 if (format_len > 0)
1107 {
1108 const char last_char = format[format_len-1];
1109 if (last_char != '\n' || last_char != '\r')
1110 strm.EOL();
1111 }
1112 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1113 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001114}
1115
1116void
1117Module::LogMessage (Log *log, const char *format, ...)
1118{
1119 if (log)
1120 {
1121 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001122 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001123 log_message.PutCString (": ");
1124 va_list args;
1125 va_start (args, format);
1126 log_message.PrintfVarArg (format, args);
1127 va_end (args);
1128 log->PutCString(log_message.GetString().c_str());
1129 }
1130}
1131
Greg Claytond61c0fc2012-04-23 22:55:20 +00001132void
1133Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1134{
1135 if (log)
1136 {
1137 StreamString log_message;
1138 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1139 log_message.PutCString (": ");
1140 va_list args;
1141 va_start (args, format);
1142 log_message.PrintfVarArg (format, args);
1143 va_end (args);
1144 if (log->GetVerbose())
1145 Host::Backtrace (log_message, 1024);
1146 log->PutCString(log_message.GetString().c_str());
1147 }
1148}
1149
Greg Claytonc982b3d2011-11-28 01:45:00 +00001150void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151Module::Dump(Stream *s)
1152{
1153 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001154 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001156 s->Printf("Module %s%s%s%s\n",
1157 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 m_object_name ? "(" : "",
1159 m_object_name ? m_object_name.GetCString() : "",
1160 m_object_name ? ")" : "");
1161
1162 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001163
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 if (objfile)
1166 objfile->Dump(s);
1167
1168 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 if (symbols)
1170 symbols->Dump(s);
1171
1172 s->IndentLess();
1173}
1174
1175
1176TypeList*
1177Module::GetTypeList ()
1178{
1179 SymbolVendor *symbols = GetSymbolVendor ();
1180 if (symbols)
1181 return &symbols->GetTypeList();
1182 return NULL;
1183}
1184
1185const ConstString &
1186Module::GetObjectName() const
1187{
1188 return m_object_name;
1189}
1190
1191ObjectFile *
1192Module::GetObjectFile()
1193{
1194 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001195 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 Timer scoped_timer(__PRETTY_FUNCTION__,
1198 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001199 DataBufferSP data_sp;
1200 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001201 const lldb::offset_t file_size = m_file.GetByteSize();
1202 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001203 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001204 m_did_load_objfile = true;
1205 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1206 &m_file,
1207 m_object_offset,
1208 file_size - m_object_offset,
1209 data_sp,
1210 data_offset);
1211 if (m_objfile_sp)
1212 {
1213 // Once we get the object file, update our module with the object file's
1214 // architecture since it might differ in vendor/os if some parts were
1215 // unknown.
1216 m_objfile_sp->GetArchitecture (m_arch);
1217 }
Greg Clayton593577a2011-09-21 03:57:31 +00001218 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219 }
Greg Clayton762f7132011-09-18 18:59:15 +00001220 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221}
1222
Michael Sartaina7499c92013-07-01 19:45:50 +00001223SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001224Module::GetSectionList()
1225{
1226 // Populate m_unified_sections_ap with sections from objfile.
1227 if (m_sections_ap.get() == NULL)
1228 {
1229 ObjectFile *obj_file = GetObjectFile();
1230 if (obj_file)
1231 obj_file->CreateSections(*GetUnifiedSectionList());
1232 }
1233 return m_sections_ap.get();
1234}
1235
1236SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001237Module::GetUnifiedSectionList()
1238{
Greg Clayton3046e662013-07-10 01:23:25 +00001239 // Populate m_unified_sections_ap with sections from objfile.
1240 if (m_sections_ap.get() == NULL)
1241 m_sections_ap.reset(new SectionList());
1242 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001243}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244
1245const Symbol *
1246Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1247{
1248 Timer scoped_timer(__PRETTY_FUNCTION__,
1249 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1250 name.AsCString(),
1251 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001252 SymbolVendor* sym_vendor = GetSymbolVendor();
1253 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001255 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001257 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
1259 return NULL;
1260}
1261void
1262Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1263{
1264 // No need to protect this call using m_mutex all other method calls are
1265 // already thread safe.
1266
1267 size_t num_indices = symbol_indexes.size();
1268 if (num_indices > 0)
1269 {
1270 SymbolContext sc;
1271 CalculateSymbolContext (&sc);
1272 for (size_t i = 0; i < num_indices; i++)
1273 {
1274 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1275 if (sc.symbol)
1276 sc_list.Append (sc);
1277 }
1278 }
1279}
1280
1281size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001282Module::FindFunctionSymbols (const ConstString &name,
1283 uint32_t name_type_mask,
1284 SymbolContextList& sc_list)
1285{
1286 Timer scoped_timer(__PRETTY_FUNCTION__,
1287 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1288 name.AsCString(),
1289 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001290 SymbolVendor* sym_vendor = GetSymbolVendor();
1291 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001292 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001293 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001294 if (symtab)
1295 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1296 }
1297 return 0;
1298}
1299
1300size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001301Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302{
1303 // No need to protect this call using m_mutex all other method calls are
1304 // already thread safe.
1305
1306
1307 Timer scoped_timer(__PRETTY_FUNCTION__,
1308 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1309 name.AsCString(),
1310 symbol_type);
1311 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001312 SymbolVendor* sym_vendor = GetSymbolVendor();
1313 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001315 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 if (symtab)
1317 {
1318 std::vector<uint32_t> symbol_indexes;
1319 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1320 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1321 }
1322 }
1323 return sc_list.GetSize() - initial_size;
1324}
1325
1326size_t
1327Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1328{
1329 // No need to protect this call using m_mutex all other method calls are
1330 // already thread safe.
1331
1332 Timer scoped_timer(__PRETTY_FUNCTION__,
1333 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1334 regex.GetText(),
1335 symbol_type);
1336 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001337 SymbolVendor* sym_vendor = GetSymbolVendor();
1338 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001340 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 if (symtab)
1342 {
1343 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001344 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1346 }
1347 }
1348 return sc_list.GetSize() - initial_size;
1349}
1350
Greg Claytone01e07b2013-04-18 18:10:51 +00001351void
1352Module::SetSymbolFileFileSpec (const FileSpec &file)
1353{
Michael Sartaina7499c92013-07-01 19:45:50 +00001354 // Remove any sections in the unified section list that come from the current symbol vendor.
1355 if (m_symfile_ap)
1356 {
Greg Clayton3046e662013-07-10 01:23:25 +00001357 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001358 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1359 if (section_list && symbol_file)
1360 {
1361 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001362 // Make sure we have an object file and that the symbol vendor's objfile isn't
1363 // the same as the module's objfile before we remove any sections for it...
1364 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001365 {
1366 size_t num_sections = section_list->GetNumSections (0);
1367 for (size_t idx = num_sections; idx > 0; --idx)
1368 {
1369 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1370 if (section_sp->GetObjectFile() == obj_file)
1371 {
Greg Clayton3046e662013-07-10 01:23:25 +00001372 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001373 }
1374 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001375 }
1376 }
1377 }
1378
Greg Claytone01e07b2013-04-18 18:10:51 +00001379 m_symfile_spec = file;
1380 m_symfile_ap.reset();
1381 m_did_load_symbol_vendor = false;
1382}
1383
Jim Ingham5aee1622010-08-09 23:31:02 +00001384bool
1385Module::IsExecutable ()
1386{
1387 if (GetObjectFile() == NULL)
1388 return false;
1389 else
1390 return GetObjectFile()->IsExecutable();
1391}
1392
Jim Inghamb53cb272011-08-03 01:03:17 +00001393bool
1394Module::IsLoadedInTarget (Target *target)
1395{
1396 ObjectFile *obj_file = GetObjectFile();
1397 if (obj_file)
1398 {
Greg Clayton3046e662013-07-10 01:23:25 +00001399 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001400 if (sections != NULL)
1401 {
1402 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001403 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1404 {
1405 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1406 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1407 {
1408 return true;
1409 }
1410 }
1411 }
1412 }
1413 return false;
1414}
Enrico Granata17598482012-11-08 02:22:02 +00001415
1416bool
Enrico Granata97303392013-05-21 00:00:30 +00001417Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001418{
1419 if (!target)
1420 {
1421 error.SetErrorString("invalid destination Target");
1422 return false;
1423 }
1424
Enrico Granata397ddd52013-05-21 20:13:34 +00001425 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001426
Greg Clayton91c0e742013-01-11 23:44:27 +00001427 Debugger &debugger = target->GetDebugger();
1428 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1429 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001430 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001431
1432 PlatformSP platform_sp(target->GetPlatform());
1433
1434 if (!platform_sp)
1435 {
1436 error.SetErrorString("invalid Platform");
1437 return false;
1438 }
Enrico Granata17598482012-11-08 02:22:02 +00001439
Greg Claytonb9d88902013-03-23 00:50:58 +00001440 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1441 *this);
1442
1443
1444 const uint32_t num_specs = file_specs.GetSize();
1445 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001446 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001447 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1448 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001449 {
1450 for (uint32_t i=0; i<num_specs; ++i)
1451 {
1452 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1453 if (scripting_fspec && scripting_fspec.Exists())
1454 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001455 if (shoud_load == eLoadScriptFromSymFileFalse)
1456 return false;
1457 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001458 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001459 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001460 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1461 "this debug session:\n\n command script import \"%s\"\n\n"
1462 "To run all discovered debug scripts in this session:\n\n"
1463 " settings set target.load-script-from-symbol-file true\n",
1464 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1465 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001466 return false;
1467 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001468 StreamString scripting_stream;
1469 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001470 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001471 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001472 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1473 can_reload,
1474 init_lldb_globals,
1475 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001476 if (!did_load)
1477 return false;
1478 }
1479 }
1480 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001481 else
1482 {
1483 error.SetErrorString("invalid ScriptInterpreter");
1484 return false;
1485 }
Enrico Granata17598482012-11-08 02:22:02 +00001486 }
1487 }
1488 return true;
1489}
1490
1491bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001492Module::SetArchitecture (const ArchSpec &new_arch)
1493{
Greg Clayton64195a22011-02-23 00:35:02 +00001494 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001495 {
1496 m_arch = new_arch;
1497 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001498 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001499 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001500}
1501
Greg Claytonc9660542012-02-05 02:38:54 +00001502bool
Greg Clayton751caf62014-02-07 22:54:47 +00001503Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001504{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001505 ObjectFile *object_file = GetObjectFile();
1506 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001507 {
Greg Clayton751caf62014-02-07 22:54:47 +00001508 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001509 return true;
1510 }
1511 else
1512 {
1513 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001514 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001515 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001516}
1517
Greg Claytonb9a01b32012-02-26 05:51:37 +00001518
1519bool
1520Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1521{
1522 const UUID &uuid = module_ref.GetUUID();
1523
1524 if (uuid.IsValid())
1525 {
1526 // If the UUID matches, then nothing more needs to match...
1527 if (uuid == GetUUID())
1528 return true;
1529 else
1530 return false;
1531 }
1532
1533 const FileSpec &file_spec = module_ref.GetFileSpec();
1534 if (file_spec)
1535 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001536 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001537 return false;
1538 }
1539
1540 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1541 if (platform_file_spec)
1542 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001543 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001544 return false;
1545 }
1546
1547 const ArchSpec &arch = module_ref.GetArchitecture();
1548 if (arch.IsValid())
1549 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001550 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001551 return false;
1552 }
1553
1554 const ConstString &object_name = module_ref.GetObjectName();
1555 if (object_name)
1556 {
1557 if (object_name != GetObjectName())
1558 return false;
1559 }
1560 return true;
1561}
1562
Greg Claytond804d282012-03-15 21:01:31 +00001563bool
1564Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1565{
1566 Mutex::Locker locker (m_mutex);
1567 return m_source_mappings.FindFile (orig_spec, new_spec);
1568}
1569
Greg Claytonf9be6932012-03-19 22:22:41 +00001570bool
1571Module::RemapSourceFile (const char *path, std::string &new_path) const
1572{
1573 Mutex::Locker locker (m_mutex);
1574 return m_source_mappings.RemapPath(path, new_path);
1575}
1576
Enrico Granata3467d802012-09-04 18:47:54 +00001577uint32_t
1578Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1579{
1580 ObjectFile *obj_file = GetObjectFile();
1581 if (obj_file)
1582 return obj_file->GetVersion (versions, num_versions);
1583
1584 if (versions && num_versions)
1585 {
1586 for (uint32_t i=0; i<num_versions; ++i)
1587 versions[i] = UINT32_MAX;
1588 }
1589 return 0;
1590}
Greg Clayton43fe2172013-04-03 02:00:15 +00001591
1592void
1593Module::PrepareForFunctionNameLookup (const ConstString &name,
1594 uint32_t name_type_mask,
1595 ConstString &lookup_name,
1596 uint32_t &lookup_name_type_mask,
1597 bool &match_name_after_lookup)
1598{
1599 const char *name_cstr = name.GetCString();
1600 lookup_name_type_mask = eFunctionNameTypeNone;
1601 match_name_after_lookup = false;
1602 const char *base_name_start = NULL;
1603 const char *base_name_end = NULL;
1604
1605 if (name_type_mask & eFunctionNameTypeAuto)
1606 {
1607 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1608 lookup_name_type_mask = eFunctionNameTypeFull;
1609 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1610 lookup_name_type_mask = eFunctionNameTypeFull;
1611 else
1612 {
1613 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1614 lookup_name_type_mask |= eFunctionNameTypeSelector;
1615
Greg Clayton6ecb2322013-05-18 00:11:21 +00001616 CPPLanguageRuntime::MethodName cpp_method (name);
1617 llvm::StringRef basename (cpp_method.GetBasename());
1618 if (basename.empty())
1619 {
1620 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1621 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1622 }
1623 else
1624 {
1625 base_name_start = basename.data();
1626 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001627 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001628 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001629 }
1630 }
1631 else
1632 {
1633 lookup_name_type_mask = name_type_mask;
1634 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1635 {
1636 // If they've asked for a CPP method or function name and it can't be that, we don't
1637 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001638 CPPLanguageRuntime::MethodName cpp_method (name);
1639 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001640 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001641 llvm::StringRef basename (cpp_method.GetBasename());
1642 base_name_start = basename.data();
1643 base_name_end = base_name_start + basename.size();
1644
1645 if (!cpp_method.GetQualifiers().empty())
1646 {
1647 // There is a "const" or other qualifer following the end of the fucntion parens,
1648 // this can't be a eFunctionNameTypeBase
1649 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1650 if (lookup_name_type_mask == eFunctionNameTypeNone)
1651 return;
1652 }
1653 }
1654 else
1655 {
1656 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1657 {
1658 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1659 if (lookup_name_type_mask == eFunctionNameTypeNone)
1660 return;
1661 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001662 }
1663 }
1664
1665 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1666 {
1667 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1668 {
1669 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1670 if (lookup_name_type_mask == eFunctionNameTypeNone)
1671 return;
1672 }
1673 }
1674 }
1675
1676 if (base_name_start &&
1677 base_name_end &&
1678 base_name_start != name_cstr &&
1679 base_name_start < base_name_end)
1680 {
1681 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1682 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1683 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1684 // to true
1685 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1686 match_name_after_lookup = true;
1687 }
1688 else
1689 {
1690 // The name is already correct, just use the exact name as supplied, and we won't need
1691 // to check if any matches contain "name"
1692 lookup_name = name;
1693 match_name_after_lookup = false;
1694 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001695}