blob: 859ec866ac0322164691073bb329525246186835 [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"
36#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000037#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton65a03992011-08-09 00:01:09 +000042// Shared pointers to modules track module lifetimes in
43// targets and in the global module, but this collection
44// will track all module objects that are still alive
45typedef std::vector<Module *> ModuleCollection;
46
47static ModuleCollection &
48GetModuleCollection()
49{
Jim Ingham549f7372011-10-31 23:47:10 +000050 // This module collection needs to live past any module, so we could either make it a
51 // shared pointer in each module or just leak is. Since it is only an empty vector by
52 // the time all the modules have gone away, we just leak it for now. If we decide this
53 // is a big problem we can introduce a Finalize method that will tear everything down in
54 // a predictable order.
55
56 static ModuleCollection *g_module_collection = NULL;
57 if (g_module_collection == NULL)
58 g_module_collection = new ModuleCollection();
59
60 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000061}
62
Greg Claytonb26e6be2012-01-27 18:08:35 +000063Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000064Module::GetAllocationModuleCollectionMutex()
65{
Greg Claytonb26e6be2012-01-27 18:08:35 +000066 // NOTE: The mutex below must be leaked since the global module list in
67 // the ModuleList class will get torn at some point, and we can't know
68 // if it will tear itself down before the "g_module_collection_mutex" below
69 // will. So we leak a Mutex object below to safeguard against that
70
71 static Mutex *g_module_collection_mutex = NULL;
72 if (g_module_collection_mutex == NULL)
73 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
74 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000075}
76
77size_t
78Module::GetNumberAllocatedModules ()
79{
80 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
81 return GetModuleCollection().size();
82}
83
84Module *
85Module::GetAllocatedModuleAtIndex (size_t idx)
86{
87 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
88 ModuleCollection &modules = GetModuleCollection();
89 if (idx < modules.size())
90 return modules[idx];
91 return NULL;
92}
Greg Clayton29ad7b92012-01-27 18:45:39 +000093#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000094
Greg Clayton29ad7b92012-01-27 18:45:39 +000095// These functions help us to determine if modules are still loaded, yet don't require that
96// you have a command interpreter and can easily be called from an external debugger.
97namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +000098
Greg Clayton29ad7b92012-01-27 18:45:39 +000099 void
100 ClearModuleInfo (void)
101 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000102 const bool mandatory = true;
103 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000104 }
105
106 void
107 DumpModuleInfo (void)
108 {
109 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
110 ModuleCollection &modules = GetModuleCollection();
111 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000112 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000113 for (size_t i=0; i<count; ++i)
114 {
115
116 StreamString strm;
117 Module *module = modules[i];
118 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
119 module->GetDescription(&strm, eDescriptionLevelFull);
120 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
121 module,
122 in_shared_module_list,
123 (uint32_t)module->use_count(),
124 strm.GetString().c_str());
125 }
126 }
127}
128
129#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000130
Greg Claytonb9a01b32012-02-26 05:51:37 +0000131Module::Module (const ModuleSpec &module_spec) :
132 m_mutex (Mutex::eMutexTypeRecursive),
133 m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
134 m_arch (module_spec.GetArchitecture()),
135 m_uuid (),
136 m_file (module_spec.GetFileSpec()),
137 m_platform_file(module_spec.GetPlatformFileSpec()),
138 m_symfile_spec (module_spec.GetSymbolFileSpec()),
139 m_object_name (module_spec.GetObjectName()),
140 m_object_offset (module_spec.GetObjectOffset()),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000141 m_object_mod_time (module_spec.GetObjectModificationTime()),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000142 m_objfile_sp (),
143 m_symfile_ap (),
144 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000145 m_source_mappings (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000146 m_did_load_objfile (false),
147 m_did_load_symbol_vendor (false),
148 m_did_parse_uuid (false),
149 m_did_init_ast (false),
150 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000151 m_file_has_changed (false),
152 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000153{
154 // Scope for locker below...
155 {
156 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
157 GetModuleCollection().push_back(this);
158 }
159
Greg Clayton5160ce52013-03-27 23:08:40 +0000160 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000161 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000162 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Greg Claytonb9a01b32012-02-26 05:51:37 +0000163 this,
164 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000165 m_file.GetPath().c_str(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000166 m_object_name.IsEmpty() ? "" : "(",
167 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
168 m_object_name.IsEmpty() ? "" : ")");
169}
170
Greg Claytone72dfb32012-02-24 01:59:29 +0000171Module::Module(const FileSpec& file_spec,
172 const ArchSpec& arch,
173 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000174 off_t object_offset,
175 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 m_mutex (Mutex::eMutexTypeRecursive),
177 m_mod_time (file_spec.GetModificationTime()),
178 m_arch (arch),
179 m_uuid (),
180 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000181 m_platform_file(),
Greg Claytone72dfb32012-02-24 01:59:29 +0000182 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000184 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000185 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000186 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000187 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000188 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000189 m_source_mappings (),
Greg Claytone83e7312010-09-07 23:40:05 +0000190 m_did_load_objfile (false),
191 m_did_load_symbol_vendor (false),
192 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000193 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000194 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000195 m_file_has_changed (false),
196 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197{
Greg Clayton65a03992011-08-09 00:01:09 +0000198 // Scope for locker below...
199 {
200 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
201 GetModuleCollection().push_back(this);
202 }
203
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 if (object_name)
205 m_object_name = *object_name;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000206
207 if (object_mod_time_ptr)
208 m_object_mod_time = *object_mod_time_ptr;
209
Greg Clayton5160ce52013-03-27 23:08:40 +0000210 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000212 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000214 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000215 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 m_object_name.IsEmpty() ? "" : "(",
217 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
218 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
221Module::~Module()
222{
Greg Clayton217b28b2013-05-22 20:13:22 +0000223 // Lock our module down while we tear everything down to make sure
224 // we don't get any access to the module while it is being destroyed
225 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000226 // Scope for locker below...
227 {
228 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
229 ModuleCollection &modules = GetModuleCollection();
230 ModuleCollection::iterator end = modules.end();
231 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000232 assert (pos != end);
233 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000234 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000235 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000237 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000239 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000240 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 m_object_name.IsEmpty() ? "" : "(",
242 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
243 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000244 // Release any auto pointers before we start tearing down our member
245 // variables since the object file and symbol files might need to make
246 // function calls back into this module object. The ordering is important
247 // here because symbol files can require the module object file. So we tear
248 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000249 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000250 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000251 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252}
253
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000254ObjectFile *
255Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
256{
257 if (m_objfile_sp)
258 {
259 error.SetErrorString ("object file already exists");
260 }
261 else
262 {
263 Mutex::Locker locker (m_mutex);
264 if (process_sp)
265 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000266 m_did_load_objfile = true;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000267 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000268 Error readmem_error;
269 const size_t bytes_read = process_sp->ReadMemory (header_addr,
270 data_ap->GetBytes(),
271 data_ap->GetByteSize(),
272 readmem_error);
273 if (bytes_read == 512)
274 {
275 DataBufferSP data_sp(data_ap.release());
276 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
277 if (m_objfile_sp)
278 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000279 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000280 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000281 m_object_name.SetCString (s.GetData());
282
283 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000284 // architecture since it might differ in vendor/os if some parts were
285 // unknown.
286 m_objfile_sp->GetArchitecture (m_arch);
287 }
288 else
289 {
290 error.SetErrorString ("unable to find suitable object file plug-in");
291 }
292 }
293 else
294 {
295 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
296 }
297 }
298 else
299 {
300 error.SetErrorString ("invalid process");
301 }
302 }
303 return m_objfile_sp.get();
304}
305
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Greg Clayton60830262011-02-04 18:53:10 +0000307const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308Module::GetUUID()
309{
310 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000311 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 {
313 ObjectFile * obj_file = GetObjectFile ();
314
315 if (obj_file != NULL)
316 {
317 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000318 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 }
320 }
321 return m_uuid;
322}
323
Greg Clayton6beaaa62011-01-17 03:46:26 +0000324ClangASTContext &
325Module::GetClangASTContext ()
326{
327 Mutex::Locker locker (m_mutex);
328 if (m_did_init_ast == false)
329 {
330 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000331 ArchSpec object_arch;
332 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000333 {
334 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000335
336 // LLVM wants this to be set to iOS or MacOSX; if we're working on
337 // a bare-boards type image, change the triple for llvm's benefit.
338 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
339 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
340 {
341 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
342 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
343 {
344 object_arch.GetTriple().setOS(llvm::Triple::IOS);
345 }
346 else
347 {
348 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
349 }
350 }
Greg Clayton514487e2011-02-15 21:59:32 +0000351 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000352 }
353 }
354 return m_ast;
355}
356
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357void
358Module::ParseAllDebugSymbols()
359{
360 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000361 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 if (num_comp_units == 0)
363 return;
364
Greg Claytona2eee182011-09-17 07:23:18 +0000365 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000366 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 SymbolVendor *symbols = GetSymbolVendor ();
368
Greg Claytonc7bece562013-01-25 18:06:21 +0000369 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 {
371 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
372 if (sc.comp_unit)
373 {
374 sc.function = NULL;
375 symbols->ParseVariablesForContext(sc);
376
377 symbols->ParseCompileUnitFunctions(sc);
378
Greg Claytonc7bece562013-01-25 18:06:21 +0000379 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 +0000380 {
381 symbols->ParseFunctionBlocks(sc);
382
383 // Parse the variables for this function and all its blocks
384 symbols->ParseVariablesForContext(sc);
385 }
386
387
388 // Parse all types for this compile unit
389 sc.function = NULL;
390 symbols->ParseTypes(sc);
391 }
392 }
393}
394
395void
396Module::CalculateSymbolContext(SymbolContext* sc)
397{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000398 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
Greg Claytone72dfb32012-02-24 01:59:29 +0000401ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000402Module::CalculateSymbolContextModule ()
403{
Greg Claytone72dfb32012-02-24 01:59:29 +0000404 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000405}
406
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407void
408Module::DumpSymbolContext(Stream *s)
409{
Jason Molendafd54b362011-09-20 21:44:10 +0000410 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
Greg Claytonc7bece562013-01-25 18:06:21 +0000413size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414Module::GetNumCompileUnits()
415{
416 Mutex::Locker locker (m_mutex);
417 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
418 SymbolVendor *symbols = GetSymbolVendor ();
419 if (symbols)
420 return symbols->GetNumCompileUnits();
421 return 0;
422}
423
424CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000425Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426{
427 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000428 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 CompUnitSP cu_sp;
430
431 if (index < num_comp_units)
432 {
433 SymbolVendor *symbols = GetSymbolVendor ();
434 if (symbols)
435 cu_sp = symbols->GetCompileUnitAtIndex(index);
436 }
437 return cu_sp;
438}
439
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440bool
441Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
442{
443 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000444 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000445 SectionList *section_list = GetSectionList();
446 if (section_list)
447 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 return false;
449}
450
451uint32_t
452Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
453{
454 Mutex::Locker locker (m_mutex);
455 uint32_t resolved_flags = 0;
456
Greg Clayton72310352013-02-23 04:12:47 +0000457 // Clear the result symbol context in case we don't find anything, but don't clear the target
458 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459
460 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000461 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462
463 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000464 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 {
466 // If the section offset based address resolved itself, then this
467 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000468 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 resolved_flags |= eSymbolContextModule;
470
471 // Resolve the compile unit, function, block, line table or line
472 // entry if requested.
473 if (resolve_scope & eSymbolContextCompUnit ||
474 resolve_scope & eSymbolContextFunction ||
475 resolve_scope & eSymbolContextBlock ||
476 resolve_scope & eSymbolContextLineEntry )
477 {
478 SymbolVendor *symbols = GetSymbolVendor ();
479 if (symbols)
480 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
481 }
482
Jim Ingham680e1772010-08-31 23:51:36 +0000483 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
484 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000486 SymbolVendor* sym_vendor = GetSymbolVendor();
487 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000489 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 if (symtab)
491 {
492 if (so_addr.IsSectionOffset())
493 {
494 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
495 if (sc.symbol)
496 resolved_flags |= eSymbolContextSymbol;
497 }
498 }
499 }
500 }
501 }
502 return resolved_flags;
503}
504
505uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000506Module::ResolveSymbolContextForFilePath
507(
508 const char *file_path,
509 uint32_t line,
510 bool check_inlines,
511 uint32_t resolve_scope,
512 SymbolContextList& sc_list
513)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514{
Greg Clayton274060b2010-10-20 20:54:39 +0000515 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
517}
518
519uint32_t
520Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
521{
522 Mutex::Locker locker (m_mutex);
523 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000524 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
525 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 line,
527 check_inlines ? "yes" : "no",
528 resolve_scope);
529
530 const uint32_t initial_count = sc_list.GetSize();
531
532 SymbolVendor *symbols = GetSymbolVendor ();
533 if (symbols)
534 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
535
536 return sc_list.GetSize() - initial_count;
537}
538
539
Greg Claytonc7bece562013-01-25 18:06:21 +0000540size_t
541Module::FindGlobalVariables (const ConstString &name,
542 const ClangNamespaceDecl *namespace_decl,
543 bool append,
544 size_t max_matches,
545 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546{
547 SymbolVendor *symbols = GetSymbolVendor ();
548 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000549 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 return 0;
551}
Greg Claytonc7bece562013-01-25 18:06:21 +0000552
553size_t
554Module::FindGlobalVariables (const RegularExpression& regex,
555 bool append,
556 size_t max_matches,
557 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558{
559 SymbolVendor *symbols = GetSymbolVendor ();
560 if (symbols)
561 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
562 return 0;
563}
564
Greg Claytonc7bece562013-01-25 18:06:21 +0000565size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000566Module::FindCompileUnits (const FileSpec &path,
567 bool append,
568 SymbolContextList &sc_list)
569{
570 if (!append)
571 sc_list.Clear();
572
Greg Claytonc7bece562013-01-25 18:06:21 +0000573 const size_t start_size = sc_list.GetSize();
574 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000575 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000576 sc.module_sp = shared_from_this();
Greg Clayton644247c2011-07-07 01:59:51 +0000577 const bool compare_directory = path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000578 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000579 {
580 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000581 if (sc.comp_unit)
582 {
583 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
584 sc_list.Append(sc);
585 }
Greg Clayton644247c2011-07-07 01:59:51 +0000586 }
587 return sc_list.GetSize() - start_size;
588}
589
Greg Claytonc7bece562013-01-25 18:06:21 +0000590size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000591Module::FindFunctions (const ConstString &name,
592 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000593 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000594 bool include_symbols,
595 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000596 bool append,
597 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598{
Greg Clayton931180e2011-01-27 06:44:37 +0000599 if (!append)
600 sc_list.Clear();
601
Greg Clayton43fe2172013-04-03 02:00:15 +0000602 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000603
604 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000606
607 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000608 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000609 ConstString lookup_name;
610 uint32_t lookup_name_type_mask = 0;
611 bool match_name_after_lookup = false;
612 Module::PrepareForFunctionNameLookup (name,
613 name_type_mask,
614 lookup_name,
615 lookup_name_type_mask,
616 match_name_after_lookup);
617
618 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000619 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000620 symbols->FindFunctions(lookup_name,
621 namespace_decl,
622 lookup_name_type_mask,
623 include_inlines,
624 append,
625 sc_list);
626
Michael Sartaina7499c92013-07-01 19:45:50 +0000627 // Now check our symbol table for symbols that are code symbols if requested
628 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000629 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000630 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000631 if (symtab)
632 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
633 }
634 }
635
636 if (match_name_after_lookup)
637 {
638 SymbolContext sc;
639 size_t i = old_size;
640 while (i<sc_list.GetSize())
641 {
642 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000643 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000644 const char *func_name = sc.GetFunctionName().GetCString();
645 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000646 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000647 // Remove the current context
648 sc_list.RemoveContextAtIndex(i);
649 // Don't increment i and continue in the loop
650 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000651 }
652 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000653 ++i;
654 }
655 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000656 }
657 else
658 {
659 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000660 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000661 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
662
Michael Sartaina7499c92013-07-01 19:45:50 +0000663 // Now check our symbol table for symbols that are code symbols if requested
664 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000665 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000666 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000667 if (symtab)
668 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000669 }
670 }
671 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000672
673 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674}
675
Greg Claytonc7bece562013-01-25 18:06:21 +0000676size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000677Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000678 bool include_symbols,
679 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000680 bool append,
681 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682{
Greg Clayton931180e2011-01-27 06:44:37 +0000683 if (!append)
684 sc_list.Clear();
685
Greg Claytonc7bece562013-01-25 18:06:21 +0000686 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000687
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 SymbolVendor *symbols = GetSymbolVendor ();
689 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000690 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000691 symbols->FindFunctions(regex, include_inlines, append, sc_list);
692
693 // Now check our symbol table for symbols that are code symbols if requested
694 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000695 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000696 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000697 if (symtab)
698 {
699 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000700 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000701 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000702 if (num_matches)
703 {
704 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000705 const size_t end_functions_added_index = sc_list.GetSize();
706 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
707 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000708 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000709 // No functions were added, just symbols, so we can just append them
710 for (size_t i=0; i<num_matches; ++i)
711 {
712 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
713 SymbolType sym_type = sc.symbol->GetType();
714 if (sc.symbol && (sym_type == eSymbolTypeCode ||
715 sym_type == eSymbolTypeResolver))
716 sc_list.Append(sc);
717 }
718 }
719 else
720 {
721 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
722 FileAddrToIndexMap file_addr_to_index;
723 for (size_t i=start_size; i<end_functions_added_index; ++i)
724 {
725 const SymbolContext &sc = sc_list[i];
726 if (sc.block)
727 continue;
728 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
729 }
730
731 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
732 // Functions were added so we need to merge symbols into any
733 // existing function symbol contexts
734 for (size_t i=start_size; i<num_matches; ++i)
735 {
736 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
737 SymbolType sym_type = sc.symbol->GetType();
738 if (sc.symbol && (sym_type == eSymbolTypeCode ||
739 sym_type == eSymbolTypeResolver))
740 {
741 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
742 if (pos == end)
743 sc_list.Append(sc);
744 else
745 sc_list[pos->second].symbol = sc.symbol;
746 }
747 }
Greg Clayton931180e2011-01-27 06:44:37 +0000748 }
749 }
750 }
751 }
752 }
753 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754}
755
Richard Mittonf86248d2013-09-12 02:20:34 +0000756void
757Module::FindAddressesForLine (const lldb::TargetSP target_sp,
758 const FileSpec &file, uint32_t line,
759 Function *function,
760 std::vector<Address> &output_local, std::vector<Address> &output_extern)
761{
762 SearchFilterByModule filter(target_sp, m_file);
763 AddressResolverFileLine resolver(file, line, true);
764 resolver.ResolveAddress (filter);
765
766 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
767 {
768 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
769 Function *f = addr.CalculateSymbolContextFunction();
770 if (f && f == function)
771 output_local.push_back (addr);
772 else
773 output_extern.push_back (addr);
774 }
775}
776
Greg Claytonc7bece562013-01-25 18:06:21 +0000777size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000778Module::FindTypes_Impl (const SymbolContext& sc,
779 const ConstString &name,
780 const ClangNamespaceDecl *namespace_decl,
781 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000782 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000783 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000784{
785 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
786 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
787 {
788 SymbolVendor *symbols = GetSymbolVendor ();
789 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000790 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000791 }
792 return 0;
793}
794
Greg Claytonc7bece562013-01-25 18:06:21 +0000795size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000796Module::FindTypesInNamespace (const SymbolContext& sc,
797 const ConstString &type_name,
798 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000799 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000800 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000801{
Greg Clayton84db9102012-03-26 23:03:23 +0000802 const bool append = true;
803 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000804}
805
Greg Claytonb43165b2012-12-05 21:24:42 +0000806lldb::TypeSP
807Module::FindFirstType (const SymbolContext& sc,
808 const ConstString &name,
809 bool exact_match)
810{
811 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000812 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000813 if (num_matches)
814 return type_list.GetTypeAtIndex(0);
815 return TypeSP();
816}
817
818
Greg Claytonc7bece562013-01-25 18:06:21 +0000819size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000820Module::FindTypes (const SymbolContext& sc,
821 const ConstString &name,
822 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000823 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000824 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000825{
Greg Claytonc7bece562013-01-25 18:06:21 +0000826 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000827 const char *type_name_cstr = name.GetCString();
828 std::string type_scope;
829 std::string type_basename;
830 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000831 TypeClass type_class = eTypeClassAny;
832 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000833 {
Greg Clayton84db9102012-03-26 23:03:23 +0000834 // Check if "name" starts with "::" which means the qualified type starts
835 // from the root namespace and implies and exact match. The typenames we
836 // get back from clang do not start with "::" so we need to strip this off
837 // in order to get the qualfied names to match
838
839 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
840 {
841 type_scope.erase(0,2);
842 exact_match = true;
843 }
844 ConstString type_basename_const_str (type_basename.c_str());
845 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
846 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000847 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000848 num_matches = types.GetSize();
849 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000850 }
851 else
Greg Clayton84db9102012-03-26 23:03:23 +0000852 {
853 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000854 if (type_class != eTypeClassAny)
855 {
856 // The "type_name_cstr" will have been modified if we have a valid type class
857 // prefix (like "struct", "class", "union", "typedef" etc).
858 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
859 types.RemoveMismatchedTypes (type_class);
860 num_matches = types.GetSize();
861 }
862 else
863 {
864 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
865 }
Greg Clayton84db9102012-03-26 23:03:23 +0000866 }
867
868 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000869
870}
871
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000873Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874{
875 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000876 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877 {
878 ObjectFile *obj_file = GetObjectFile ();
879 if (obj_file != NULL)
880 {
881 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +0000882 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +0000883 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 }
885 }
886 return m_symfile_ap.get();
887}
888
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889void
890Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
891{
892 // Container objects whose paths do not specify a file directly can call
893 // this function to correct the file and object names.
894 m_file = file;
895 m_mod_time = file.GetModificationTime();
896 m_object_name = object_name;
897}
898
899const ArchSpec&
900Module::GetArchitecture () const
901{
902 return m_arch;
903}
904
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000905std::string
906Module::GetSpecificationDescription () const
907{
908 std::string spec(GetFileSpec().GetPath());
909 if (m_object_name)
910 {
911 spec += '(';
912 spec += m_object_name.GetCString();
913 spec += ')';
914 }
915 return spec;
916}
917
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000919Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000920{
921 Mutex::Locker locker (m_mutex);
922
Greg Claytonc982b3d2011-11-28 01:45:00 +0000923 if (level >= eDescriptionLevelFull)
924 {
925 if (m_arch.IsValid())
926 s->Printf("(%s) ", m_arch.GetArchitectureName());
927 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000928
Greg Claytonc982b3d2011-11-28 01:45:00 +0000929 if (level == eDescriptionLevelBrief)
930 {
931 const char *filename = m_file.GetFilename().GetCString();
932 if (filename)
933 s->PutCString (filename);
934 }
935 else
936 {
937 char path[PATH_MAX];
938 if (m_file.GetPath(path, sizeof(path)))
939 s->PutCString(path);
940 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000941
942 const char *object_name = m_object_name.GetCString();
943 if (object_name)
944 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000945}
946
947void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000948Module::ReportError (const char *format, ...)
949{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000950 if (format && format[0])
951 {
952 StreamString strm;
953 strm.PutCString("error: ");
954 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000955 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000956 va_list args;
957 va_start (args, format);
958 strm.PrintfVarArg(format, args);
959 va_end (args);
960
961 const int format_len = strlen(format);
962 if (format_len > 0)
963 {
964 const char last_char = format[format_len-1];
965 if (last_char != '\n' || last_char != '\r')
966 strm.EOL();
967 }
968 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
969
970 }
971}
972
Greg Clayton1d609092012-07-12 22:51:12 +0000973bool
974Module::FileHasChanged () const
975{
976 if (m_file_has_changed == false)
977 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
978 return m_file_has_changed;
979}
980
Greg Claytone38a5ed2012-01-05 03:57:59 +0000981void
982Module::ReportErrorIfModifyDetected (const char *format, ...)
983{
Greg Clayton1d609092012-07-12 22:51:12 +0000984 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000985 {
Greg Clayton1d609092012-07-12 22:51:12 +0000986 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +0000987 {
Greg Clayton1d609092012-07-12 22:51:12 +0000988 m_first_file_changed_log = true;
989 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000990 {
Greg Clayton1d609092012-07-12 22:51:12 +0000991 StreamString strm;
992 strm.PutCString("error: the object file ");
993 GetDescription(&strm, lldb::eDescriptionLevelFull);
994 strm.PutCString (" has been modified\n");
995
996 va_list args;
997 va_start (args, format);
998 strm.PrintfVarArg(format, args);
999 va_end (args);
1000
1001 const int format_len = strlen(format);
1002 if (format_len > 0)
1003 {
1004 const char last_char = format[format_len-1];
1005 if (last_char != '\n' || last_char != '\r')
1006 strm.EOL();
1007 }
1008 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1009 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001010 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001011 }
1012 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001013}
1014
1015void
1016Module::ReportWarning (const char *format, ...)
1017{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001018 if (format && format[0])
1019 {
1020 StreamString strm;
1021 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001022 GetDescription(&strm, lldb::eDescriptionLevelFull);
1023 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001024
1025 va_list args;
1026 va_start (args, format);
1027 strm.PrintfVarArg(format, args);
1028 va_end (args);
1029
1030 const int format_len = strlen(format);
1031 if (format_len > 0)
1032 {
1033 const char last_char = format[format_len-1];
1034 if (last_char != '\n' || last_char != '\r')
1035 strm.EOL();
1036 }
1037 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1038 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001039}
1040
1041void
1042Module::LogMessage (Log *log, const char *format, ...)
1043{
1044 if (log)
1045 {
1046 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001047 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001048 log_message.PutCString (": ");
1049 va_list args;
1050 va_start (args, format);
1051 log_message.PrintfVarArg (format, args);
1052 va_end (args);
1053 log->PutCString(log_message.GetString().c_str());
1054 }
1055}
1056
Greg Claytond61c0fc2012-04-23 22:55:20 +00001057void
1058Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1059{
1060 if (log)
1061 {
1062 StreamString log_message;
1063 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1064 log_message.PutCString (": ");
1065 va_list args;
1066 va_start (args, format);
1067 log_message.PrintfVarArg (format, args);
1068 va_end (args);
1069 if (log->GetVerbose())
1070 Host::Backtrace (log_message, 1024);
1071 log->PutCString(log_message.GetString().c_str());
1072 }
1073}
1074
Greg Claytonc982b3d2011-11-28 01:45:00 +00001075void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076Module::Dump(Stream *s)
1077{
1078 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001079 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001081 s->Printf("Module %s%s%s%s\n",
1082 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 m_object_name ? "(" : "",
1084 m_object_name ? m_object_name.GetCString() : "",
1085 m_object_name ? ")" : "");
1086
1087 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 if (objfile)
1091 objfile->Dump(s);
1092
1093 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 if (symbols)
1095 symbols->Dump(s);
1096
1097 s->IndentLess();
1098}
1099
1100
1101TypeList*
1102Module::GetTypeList ()
1103{
1104 SymbolVendor *symbols = GetSymbolVendor ();
1105 if (symbols)
1106 return &symbols->GetTypeList();
1107 return NULL;
1108}
1109
1110const ConstString &
1111Module::GetObjectName() const
1112{
1113 return m_object_name;
1114}
1115
1116ObjectFile *
1117Module::GetObjectFile()
1118{
1119 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001120 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 Timer scoped_timer(__PRETTY_FUNCTION__,
1123 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001124 DataBufferSP data_sp;
1125 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001126 const lldb::offset_t file_size = m_file.GetByteSize();
1127 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001128 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001129 m_did_load_objfile = true;
1130 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1131 &m_file,
1132 m_object_offset,
1133 file_size - m_object_offset,
1134 data_sp,
1135 data_offset);
1136 if (m_objfile_sp)
1137 {
1138 // Once we get the object file, update our module with the object file's
1139 // architecture since it might differ in vendor/os if some parts were
1140 // unknown.
1141 m_objfile_sp->GetArchitecture (m_arch);
1142 }
Greg Clayton593577a2011-09-21 03:57:31 +00001143 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 }
Greg Clayton762f7132011-09-18 18:59:15 +00001145 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146}
1147
Michael Sartaina7499c92013-07-01 19:45:50 +00001148SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001149Module::GetSectionList()
1150{
1151 // Populate m_unified_sections_ap with sections from objfile.
1152 if (m_sections_ap.get() == NULL)
1153 {
1154 ObjectFile *obj_file = GetObjectFile();
1155 if (obj_file)
1156 obj_file->CreateSections(*GetUnifiedSectionList());
1157 }
1158 return m_sections_ap.get();
1159}
1160
1161SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001162Module::GetUnifiedSectionList()
1163{
Greg Clayton3046e662013-07-10 01:23:25 +00001164 // Populate m_unified_sections_ap with sections from objfile.
1165 if (m_sections_ap.get() == NULL)
1166 m_sections_ap.reset(new SectionList());
1167 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001168}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169
1170const Symbol *
1171Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1172{
1173 Timer scoped_timer(__PRETTY_FUNCTION__,
1174 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1175 name.AsCString(),
1176 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001177 SymbolVendor* sym_vendor = GetSymbolVendor();
1178 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001180 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001182 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
1184 return NULL;
1185}
1186void
1187Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1188{
1189 // No need to protect this call using m_mutex all other method calls are
1190 // already thread safe.
1191
1192 size_t num_indices = symbol_indexes.size();
1193 if (num_indices > 0)
1194 {
1195 SymbolContext sc;
1196 CalculateSymbolContext (&sc);
1197 for (size_t i = 0; i < num_indices; i++)
1198 {
1199 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1200 if (sc.symbol)
1201 sc_list.Append (sc);
1202 }
1203 }
1204}
1205
1206size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001207Module::FindFunctionSymbols (const ConstString &name,
1208 uint32_t name_type_mask,
1209 SymbolContextList& sc_list)
1210{
1211 Timer scoped_timer(__PRETTY_FUNCTION__,
1212 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1213 name.AsCString(),
1214 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001215 SymbolVendor* sym_vendor = GetSymbolVendor();
1216 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001217 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001218 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001219 if (symtab)
1220 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1221 }
1222 return 0;
1223}
1224
1225size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001226Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227{
1228 // No need to protect this call using m_mutex all other method calls are
1229 // already thread safe.
1230
1231
1232 Timer scoped_timer(__PRETTY_FUNCTION__,
1233 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1234 name.AsCString(),
1235 symbol_type);
1236 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001237 SymbolVendor* sym_vendor = GetSymbolVendor();
1238 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001240 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 if (symtab)
1242 {
1243 std::vector<uint32_t> symbol_indexes;
1244 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1245 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1246 }
1247 }
1248 return sc_list.GetSize() - initial_size;
1249}
1250
1251size_t
1252Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1253{
1254 // No need to protect this call using m_mutex all other method calls are
1255 // already thread safe.
1256
1257 Timer scoped_timer(__PRETTY_FUNCTION__,
1258 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1259 regex.GetText(),
1260 symbol_type);
1261 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001262 SymbolVendor* sym_vendor = GetSymbolVendor();
1263 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001265 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 if (symtab)
1267 {
1268 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001269 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1271 }
1272 }
1273 return sc_list.GetSize() - initial_size;
1274}
1275
Greg Claytone01e07b2013-04-18 18:10:51 +00001276void
1277Module::SetSymbolFileFileSpec (const FileSpec &file)
1278{
Michael Sartaina7499c92013-07-01 19:45:50 +00001279 // Remove any sections in the unified section list that come from the current symbol vendor.
1280 if (m_symfile_ap)
1281 {
Greg Clayton3046e662013-07-10 01:23:25 +00001282 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001283 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1284 if (section_list && symbol_file)
1285 {
1286 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001287 // Make sure we have an object file and that the symbol vendor's objfile isn't
1288 // the same as the module's objfile before we remove any sections for it...
1289 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001290 {
1291 size_t num_sections = section_list->GetNumSections (0);
1292 for (size_t idx = num_sections; idx > 0; --idx)
1293 {
1294 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1295 if (section_sp->GetObjectFile() == obj_file)
1296 {
Greg Clayton3046e662013-07-10 01:23:25 +00001297 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001298 }
1299 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001300 }
1301 }
1302 }
1303
Greg Claytone01e07b2013-04-18 18:10:51 +00001304 m_symfile_spec = file;
1305 m_symfile_ap.reset();
1306 m_did_load_symbol_vendor = false;
1307}
1308
Jim Ingham5aee1622010-08-09 23:31:02 +00001309bool
1310Module::IsExecutable ()
1311{
1312 if (GetObjectFile() == NULL)
1313 return false;
1314 else
1315 return GetObjectFile()->IsExecutable();
1316}
1317
Jim Inghamb53cb272011-08-03 01:03:17 +00001318bool
1319Module::IsLoadedInTarget (Target *target)
1320{
1321 ObjectFile *obj_file = GetObjectFile();
1322 if (obj_file)
1323 {
Greg Clayton3046e662013-07-10 01:23:25 +00001324 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001325 if (sections != NULL)
1326 {
1327 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001328 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1329 {
1330 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1331 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1332 {
1333 return true;
1334 }
1335 }
1336 }
1337 }
1338 return false;
1339}
Enrico Granata17598482012-11-08 02:22:02 +00001340
1341bool
Enrico Granata97303392013-05-21 00:00:30 +00001342Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001343{
1344 if (!target)
1345 {
1346 error.SetErrorString("invalid destination Target");
1347 return false;
1348 }
1349
Enrico Granata397ddd52013-05-21 20:13:34 +00001350 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001351
Greg Clayton91c0e742013-01-11 23:44:27 +00001352 Debugger &debugger = target->GetDebugger();
1353 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1354 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001355 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001356
1357 PlatformSP platform_sp(target->GetPlatform());
1358
1359 if (!platform_sp)
1360 {
1361 error.SetErrorString("invalid Platform");
1362 return false;
1363 }
Enrico Granata17598482012-11-08 02:22:02 +00001364
Greg Claytonb9d88902013-03-23 00:50:58 +00001365 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1366 *this);
1367
1368
1369 const uint32_t num_specs = file_specs.GetSize();
1370 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001371 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001372 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1373 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001374 {
1375 for (uint32_t i=0; i<num_specs; ++i)
1376 {
1377 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1378 if (scripting_fspec && scripting_fspec.Exists())
1379 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001380 if (shoud_load == eLoadScriptFromSymFileFalse)
1381 return false;
1382 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001383 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001384 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001385 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1386 "this debug session:\n\n command script import \"%s\"\n\n"
1387 "To run all discovered debug scripts in this session:\n\n"
1388 " settings set target.load-script-from-symbol-file true\n",
1389 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1390 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001391 return false;
1392 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001393 StreamString scripting_stream;
1394 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001395 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001396 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001397 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1398 can_reload,
1399 init_lldb_globals,
1400 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001401 if (!did_load)
1402 return false;
1403 }
1404 }
1405 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001406 else
1407 {
1408 error.SetErrorString("invalid ScriptInterpreter");
1409 return false;
1410 }
Enrico Granata17598482012-11-08 02:22:02 +00001411 }
1412 }
1413 return true;
1414}
1415
1416bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001417Module::SetArchitecture (const ArchSpec &new_arch)
1418{
Greg Clayton64195a22011-02-23 00:35:02 +00001419 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001420 {
1421 m_arch = new_arch;
1422 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001423 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001424 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001425}
1426
Greg Claytonc9660542012-02-05 02:38:54 +00001427bool
1428Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
1429{
Greg Clayton741f3f92012-03-27 21:10:07 +00001430 size_t num_loaded_sections = 0;
Greg Clayton3046e662013-07-10 01:23:25 +00001431 SectionList *section_list = GetSectionList ();
1432 if (section_list)
Greg Claytonc9660542012-02-05 02:38:54 +00001433 {
Greg Clayton3046e662013-07-10 01:23:25 +00001434 const size_t num_sections = section_list->GetSize();
1435 size_t sect_idx = 0;
1436 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
Greg Claytonc9660542012-02-05 02:38:54 +00001437 {
Greg Clayton3046e662013-07-10 01:23:25 +00001438 // Iterate through the object file sections to find the
1439 // first section that starts of file offset zero and that
1440 // has bytes in the file...
1441 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
1442 // Only load non-thread specific sections when given a slide
1443 if (section_sp && !section_sp->IsThreadSpecific())
Greg Claytonc9660542012-02-05 02:38:54 +00001444 {
Greg Clayton3046e662013-07-10 01:23:25 +00001445 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset))
1446 ++num_loaded_sections;
Greg Claytonc9660542012-02-05 02:38:54 +00001447 }
Greg Claytonc9660542012-02-05 02:38:54 +00001448 }
1449 }
Greg Clayton741f3f92012-03-27 21:10:07 +00001450 changed = num_loaded_sections > 0;
1451 return num_loaded_sections > 0;
Greg Claytonc9660542012-02-05 02:38:54 +00001452}
1453
Greg Claytonb9a01b32012-02-26 05:51:37 +00001454
1455bool
1456Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1457{
1458 const UUID &uuid = module_ref.GetUUID();
1459
1460 if (uuid.IsValid())
1461 {
1462 // If the UUID matches, then nothing more needs to match...
1463 if (uuid == GetUUID())
1464 return true;
1465 else
1466 return false;
1467 }
1468
1469 const FileSpec &file_spec = module_ref.GetFileSpec();
1470 if (file_spec)
1471 {
1472 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory()))
1473 return false;
1474 }
1475
1476 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1477 if (platform_file_spec)
1478 {
Greg Clayton548e9a32012-10-02 06:04:17 +00001479 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001480 return false;
1481 }
1482
1483 const ArchSpec &arch = module_ref.GetArchitecture();
1484 if (arch.IsValid())
1485 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001486 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001487 return false;
1488 }
1489
1490 const ConstString &object_name = module_ref.GetObjectName();
1491 if (object_name)
1492 {
1493 if (object_name != GetObjectName())
1494 return false;
1495 }
1496 return true;
1497}
1498
Greg Claytond804d282012-03-15 21:01:31 +00001499bool
1500Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1501{
1502 Mutex::Locker locker (m_mutex);
1503 return m_source_mappings.FindFile (orig_spec, new_spec);
1504}
1505
Greg Claytonf9be6932012-03-19 22:22:41 +00001506bool
1507Module::RemapSourceFile (const char *path, std::string &new_path) const
1508{
1509 Mutex::Locker locker (m_mutex);
1510 return m_source_mappings.RemapPath(path, new_path);
1511}
1512
Enrico Granata3467d802012-09-04 18:47:54 +00001513uint32_t
1514Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1515{
1516 ObjectFile *obj_file = GetObjectFile();
1517 if (obj_file)
1518 return obj_file->GetVersion (versions, num_versions);
1519
1520 if (versions && num_versions)
1521 {
1522 for (uint32_t i=0; i<num_versions; ++i)
1523 versions[i] = UINT32_MAX;
1524 }
1525 return 0;
1526}
Greg Clayton43fe2172013-04-03 02:00:15 +00001527
1528void
1529Module::PrepareForFunctionNameLookup (const ConstString &name,
1530 uint32_t name_type_mask,
1531 ConstString &lookup_name,
1532 uint32_t &lookup_name_type_mask,
1533 bool &match_name_after_lookup)
1534{
1535 const char *name_cstr = name.GetCString();
1536 lookup_name_type_mask = eFunctionNameTypeNone;
1537 match_name_after_lookup = false;
1538 const char *base_name_start = NULL;
1539 const char *base_name_end = NULL;
1540
1541 if (name_type_mask & eFunctionNameTypeAuto)
1542 {
1543 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1544 lookup_name_type_mask = eFunctionNameTypeFull;
1545 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1546 lookup_name_type_mask = eFunctionNameTypeFull;
1547 else
1548 {
1549 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1550 lookup_name_type_mask |= eFunctionNameTypeSelector;
1551
Greg Clayton6ecb2322013-05-18 00:11:21 +00001552 CPPLanguageRuntime::MethodName cpp_method (name);
1553 llvm::StringRef basename (cpp_method.GetBasename());
1554 if (basename.empty())
1555 {
1556 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1557 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1558 }
1559 else
1560 {
1561 base_name_start = basename.data();
1562 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001563 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001564 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001565 }
1566 }
1567 else
1568 {
1569 lookup_name_type_mask = name_type_mask;
1570 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1571 {
1572 // If they've asked for a CPP method or function name and it can't be that, we don't
1573 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001574 CPPLanguageRuntime::MethodName cpp_method (name);
1575 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001576 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001577 llvm::StringRef basename (cpp_method.GetBasename());
1578 base_name_start = basename.data();
1579 base_name_end = base_name_start + basename.size();
1580
1581 if (!cpp_method.GetQualifiers().empty())
1582 {
1583 // There is a "const" or other qualifer following the end of the fucntion parens,
1584 // this can't be a eFunctionNameTypeBase
1585 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1586 if (lookup_name_type_mask == eFunctionNameTypeNone)
1587 return;
1588 }
1589 }
1590 else
1591 {
1592 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1593 {
1594 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1595 if (lookup_name_type_mask == eFunctionNameTypeNone)
1596 return;
1597 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001598 }
1599 }
1600
1601 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1602 {
1603 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1604 {
1605 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1606 if (lookup_name_type_mask == eFunctionNameTypeNone)
1607 return;
1608 }
1609 }
1610 }
1611
1612 if (base_name_start &&
1613 base_name_end &&
1614 base_name_start != name_cstr &&
1615 base_name_start < base_name_end)
1616 {
1617 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1618 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1619 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1620 // to true
1621 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1622 match_name_after_lookup = true;
1623 }
1624 else
1625 {
1626 // The name is already correct, just use the exact name as supplied, and we won't need
1627 // to check if any matches contain "name"
1628 lookup_name = name;
1629 match_name_after_lookup = false;
1630 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001631}