blob: 588c3eacc11d32c344866db45684a4295669c507 [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),
Greg Clayton34f11592014-03-04 21:20:23 +0000134 m_mod_time (),
135 m_arch (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000136 m_uuid (),
Greg Clayton34f11592014-03-04 21:20:23 +0000137 m_file (),
138 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000139 m_remote_install_file(),
Greg Clayton34f11592014-03-04 21:20:23 +0000140 m_symfile_spec (),
141 m_object_name (),
142 m_object_offset (),
143 m_object_mod_time (),
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,
Greg Clayton34f11592014-03-04 21:20:23 +0000166 module_spec.GetArchitecture().GetArchitectureName(),
167 module_spec.GetFileSpec().GetPath().c_str(),
168 module_spec.GetObjectName().IsEmpty() ? "" : "(",
169 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
170 module_spec.GetObjectName().IsEmpty() ? "" : ")");
171
172 // First extract all module specifications from the file using the local
173 // file path. If there are no specifications, then don't fill anything in
174 ModuleSpecList modules_specs;
175 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
176 return;
177
178 // Now make sure that one of the module specifications matches what we just
179 // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
180 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
181 // UUID YYY and we don't want those to match. If they don't match, just don't
182 // fill any ivars in so we don't accidentally grab the wrong file later since
183 // they don't match...
184 ModuleSpec matching_module_spec;
185 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
186 return;
187 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
188 if (module_spec.GetArchitecture().IsValid())
189 m_arch = module_spec.GetArchitecture();
190 else
191 m_arch = matching_module_spec.GetArchitecture();
192 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
193 m_file = module_spec.GetFileSpec();
194 m_platform_file = module_spec.GetPlatformFileSpec();
195 m_symfile_spec = module_spec.GetSymbolFileSpec();
196 m_object_name = module_spec.GetObjectName();
197 m_object_offset = module_spec.GetObjectOffset();
198 m_object_mod_time = module_spec.GetObjectModificationTime();
199
Greg Claytonb9a01b32012-02-26 05:51:37 +0000200}
201
Greg Claytone72dfb32012-02-24 01:59:29 +0000202Module::Module(const FileSpec& file_spec,
203 const ArchSpec& arch,
204 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000205 off_t object_offset,
206 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 m_mutex (Mutex::eMutexTypeRecursive),
208 m_mod_time (file_spec.GetModificationTime()),
209 m_arch (arch),
210 m_uuid (),
211 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000212 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000213 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000214 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000216 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000217 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000218 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000219 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000220 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000221 m_source_mappings (),
Greg Claytone83e7312010-09-07 23:40:05 +0000222 m_did_load_objfile (false),
223 m_did_load_symbol_vendor (false),
224 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000225 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000226 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000227 m_file_has_changed (false),
228 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229{
Greg Clayton65a03992011-08-09 00:01:09 +0000230 // Scope for locker below...
231 {
232 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
233 GetModuleCollection().push_back(this);
234 }
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 if (object_name)
237 m_object_name = *object_name;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000238
239 if (object_mod_time_ptr)
240 m_object_mod_time = *object_mod_time_ptr;
241
Greg Clayton5160ce52013-03-27 23:08:40 +0000242 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000244 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000246 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000247 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 m_object_name.IsEmpty() ? "" : "(",
249 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
250 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
253Module::~Module()
254{
Greg Clayton217b28b2013-05-22 20:13:22 +0000255 // Lock our module down while we tear everything down to make sure
256 // we don't get any access to the module while it is being destroyed
257 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000258 // Scope for locker below...
259 {
260 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
261 ModuleCollection &modules = GetModuleCollection();
262 ModuleCollection::iterator end = modules.end();
263 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000264 assert (pos != end);
265 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000266 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000267 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000269 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000271 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000272 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 m_object_name.IsEmpty() ? "" : "(",
274 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
275 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000276 // Release any auto pointers before we start tearing down our member
277 // variables since the object file and symbol files might need to make
278 // function calls back into this module object. The ordering is important
279 // here because symbol files can require the module object file. So we tear
280 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000281 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000282 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000283 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284}
285
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000286ObjectFile *
Andrew MacPherson17220c12014-03-05 10:12:43 +0000287Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error, size_t size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000288{
289 if (m_objfile_sp)
290 {
291 error.SetErrorString ("object file already exists");
292 }
293 else
294 {
295 Mutex::Locker locker (m_mutex);
296 if (process_sp)
297 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000298 m_did_load_objfile = true;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000299 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000300 Error readmem_error;
301 const size_t bytes_read = process_sp->ReadMemory (header_addr,
302 data_ap->GetBytes(),
303 data_ap->GetByteSize(),
304 readmem_error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000305 if (bytes_read == size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000306 {
307 DataBufferSP data_sp(data_ap.release());
308 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
309 if (m_objfile_sp)
310 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000311 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000312 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000313 m_object_name.SetCString (s.GetData());
314
315 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000316 // architecture since it might differ in vendor/os if some parts were
317 // unknown.
318 m_objfile_sp->GetArchitecture (m_arch);
319 }
320 else
321 {
322 error.SetErrorString ("unable to find suitable object file plug-in");
323 }
324 }
325 else
326 {
327 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
328 }
329 }
330 else
331 {
332 error.SetErrorString ("invalid process");
333 }
334 }
335 return m_objfile_sp.get();
336}
337
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
Greg Clayton60830262011-02-04 18:53:10 +0000339const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340Module::GetUUID()
341{
342 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000343 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 {
345 ObjectFile * obj_file = GetObjectFile ();
346
347 if (obj_file != NULL)
348 {
349 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000350 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 }
352 }
353 return m_uuid;
354}
355
Greg Clayton6beaaa62011-01-17 03:46:26 +0000356ClangASTContext &
357Module::GetClangASTContext ()
358{
359 Mutex::Locker locker (m_mutex);
360 if (m_did_init_ast == false)
361 {
362 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000363 ArchSpec object_arch;
364 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000365 {
366 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000367
368 // LLVM wants this to be set to iOS or MacOSX; if we're working on
369 // a bare-boards type image, change the triple for llvm's benefit.
370 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
371 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
372 {
373 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
374 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
375 {
376 object_arch.GetTriple().setOS(llvm::Triple::IOS);
377 }
378 else
379 {
380 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
381 }
382 }
Greg Clayton514487e2011-02-15 21:59:32 +0000383 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000384 }
385 }
386 return m_ast;
387}
388
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389void
390Module::ParseAllDebugSymbols()
391{
392 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000393 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 if (num_comp_units == 0)
395 return;
396
Greg Claytona2eee182011-09-17 07:23:18 +0000397 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000398 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 SymbolVendor *symbols = GetSymbolVendor ();
400
Greg Claytonc7bece562013-01-25 18:06:21 +0000401 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
403 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
404 if (sc.comp_unit)
405 {
406 sc.function = NULL;
407 symbols->ParseVariablesForContext(sc);
408
409 symbols->ParseCompileUnitFunctions(sc);
410
Greg Claytonc7bece562013-01-25 18:06:21 +0000411 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 +0000412 {
413 symbols->ParseFunctionBlocks(sc);
414
415 // Parse the variables for this function and all its blocks
416 symbols->ParseVariablesForContext(sc);
417 }
418
419
420 // Parse all types for this compile unit
421 sc.function = NULL;
422 symbols->ParseTypes(sc);
423 }
424 }
425}
426
427void
428Module::CalculateSymbolContext(SymbolContext* sc)
429{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000430 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
Greg Claytone72dfb32012-02-24 01:59:29 +0000433ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000434Module::CalculateSymbolContextModule ()
435{
Greg Claytone72dfb32012-02-24 01:59:29 +0000436 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000437}
438
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439void
440Module::DumpSymbolContext(Stream *s)
441{
Jason Molendafd54b362011-09-20 21:44:10 +0000442 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443}
444
Greg Claytonc7bece562013-01-25 18:06:21 +0000445size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446Module::GetNumCompileUnits()
447{
448 Mutex::Locker locker (m_mutex);
449 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
450 SymbolVendor *symbols = GetSymbolVendor ();
451 if (symbols)
452 return symbols->GetNumCompileUnits();
453 return 0;
454}
455
456CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000457Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
459 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000460 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 CompUnitSP cu_sp;
462
463 if (index < num_comp_units)
464 {
465 SymbolVendor *symbols = GetSymbolVendor ();
466 if (symbols)
467 cu_sp = symbols->GetCompileUnitAtIndex(index);
468 }
469 return cu_sp;
470}
471
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472bool
473Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
474{
475 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000476 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000477 SectionList *section_list = GetSectionList();
478 if (section_list)
479 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 return false;
481}
482
483uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000484Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
485 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486{
487 Mutex::Locker locker (m_mutex);
488 uint32_t resolved_flags = 0;
489
Greg Clayton72310352013-02-23 04:12:47 +0000490 // Clear the result symbol context in case we don't find anything, but don't clear the target
491 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
493 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000494 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
496 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000497 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 {
499 // If the section offset based address resolved itself, then this
500 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000501 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 resolved_flags |= eSymbolContextModule;
503
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000504 SymbolVendor* sym_vendor = GetSymbolVendor();
505 if (!sym_vendor)
506 return resolved_flags;
507
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 // Resolve the compile unit, function, block, line table or line
509 // entry if requested.
510 if (resolve_scope & eSymbolContextCompUnit ||
511 resolve_scope & eSymbolContextFunction ||
512 resolve_scope & eSymbolContextBlock ||
513 resolve_scope & eSymbolContextLineEntry )
514 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000515 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 }
517
Jim Ingham680e1772010-08-31 23:51:36 +0000518 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
519 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000521 Symtab *symtab = sym_vendor->GetSymtab();
522 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000524 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000525 if (!sc.symbol &&
526 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
527 {
528 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
529 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
530 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
531 }
532
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000533 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000534 {
535 if (sc.symbol->IsSynthetic())
536 {
537 // We have a synthetic symbol so lets check if the object file
538 // from the symbol file in the symbol vendor is different than
539 // the object file for the module, and if so search its symbol
540 // table to see if we can come up with a better symbol. For example
541 // dSYM files on MacOSX have an unstripped symbol table inside of
542 // them.
543 ObjectFile *symtab_objfile = symtab->GetObjectFile();
544 if (symtab_objfile && symtab_objfile->IsStripped())
545 {
546 SymbolFile *symfile = sym_vendor->GetSymbolFile();
547 if (symfile)
548 {
549 ObjectFile *symfile_objfile = symfile->GetObjectFile();
550 if (symfile_objfile != symtab_objfile)
551 {
552 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
553 if (symfile_symtab)
554 {
555 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
556 if (symbol && !symbol->IsSynthetic())
557 {
558 sc.symbol = symbol;
559 }
560 }
561 }
562 }
563 }
564 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000565 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000566 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000567 }
568 }
569
570 // For function symbols, so_addr may be off by one. This is a convention consistent
571 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
572 // symbol lookup for disassembly and unwind.
573 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000574 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000575 {
576 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000577 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000578
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000579 bool do_resolve_tail_call_address = false; // prevent recursion
580 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
581 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000582 if (flags & eSymbolContextSymbol)
583 {
584 AddressRange addr_range;
585 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000587 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000589 // If the requested address is one past the address range of a function (i.e. a tail call),
590 // or the decremented address is the start of a function (i.e. some forms of trampoline),
591 // indicate that the symbol has been resolved.
592 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
593 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
594 {
595 resolved_flags |= flags;
596 }
597 }
598 else
599 {
600 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 }
602 }
603 }
604 }
605 }
606 return resolved_flags;
607}
608
609uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000610Module::ResolveSymbolContextForFilePath
611(
612 const char *file_path,
613 uint32_t line,
614 bool check_inlines,
615 uint32_t resolve_scope,
616 SymbolContextList& sc_list
617)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618{
Greg Clayton274060b2010-10-20 20:54:39 +0000619 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
621}
622
623uint32_t
624Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
625{
626 Mutex::Locker locker (m_mutex);
627 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000628 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
629 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 line,
631 check_inlines ? "yes" : "no",
632 resolve_scope);
633
634 const uint32_t initial_count = sc_list.GetSize();
635
636 SymbolVendor *symbols = GetSymbolVendor ();
637 if (symbols)
638 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
639
640 return sc_list.GetSize() - initial_count;
641}
642
643
Greg Claytonc7bece562013-01-25 18:06:21 +0000644size_t
645Module::FindGlobalVariables (const ConstString &name,
646 const ClangNamespaceDecl *namespace_decl,
647 bool append,
648 size_t max_matches,
649 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650{
651 SymbolVendor *symbols = GetSymbolVendor ();
652 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000653 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 return 0;
655}
Greg Claytonc7bece562013-01-25 18:06:21 +0000656
657size_t
658Module::FindGlobalVariables (const RegularExpression& regex,
659 bool append,
660 size_t max_matches,
661 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662{
663 SymbolVendor *symbols = GetSymbolVendor ();
664 if (symbols)
665 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
666 return 0;
667}
668
Greg Claytonc7bece562013-01-25 18:06:21 +0000669size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000670Module::FindCompileUnits (const FileSpec &path,
671 bool append,
672 SymbolContextList &sc_list)
673{
674 if (!append)
675 sc_list.Clear();
676
Greg Claytonc7bece562013-01-25 18:06:21 +0000677 const size_t start_size = sc_list.GetSize();
678 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000679 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000680 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000681 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000682 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000683 {
684 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000685 if (sc.comp_unit)
686 {
687 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
688 sc_list.Append(sc);
689 }
Greg Clayton644247c2011-07-07 01:59:51 +0000690 }
691 return sc_list.GetSize() - start_size;
692}
693
Greg Claytonc7bece562013-01-25 18:06:21 +0000694size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000695Module::FindFunctions (const ConstString &name,
696 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000697 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000698 bool include_symbols,
699 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000700 bool append,
701 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702{
Greg Clayton931180e2011-01-27 06:44:37 +0000703 if (!append)
704 sc_list.Clear();
705
Greg Clayton43fe2172013-04-03 02:00:15 +0000706 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000707
708 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000710
711 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000712 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000713 ConstString lookup_name;
714 uint32_t lookup_name_type_mask = 0;
715 bool match_name_after_lookup = false;
716 Module::PrepareForFunctionNameLookup (name,
717 name_type_mask,
718 lookup_name,
719 lookup_name_type_mask,
720 match_name_after_lookup);
721
722 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000723 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000724 symbols->FindFunctions(lookup_name,
725 namespace_decl,
726 lookup_name_type_mask,
727 include_inlines,
728 append,
729 sc_list);
730
Michael Sartaina7499c92013-07-01 19:45:50 +0000731 // Now check our symbol table for symbols that are code symbols if requested
732 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000733 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000734 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000735 if (symtab)
736 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
737 }
738 }
739
740 if (match_name_after_lookup)
741 {
742 SymbolContext sc;
743 size_t i = old_size;
744 while (i<sc_list.GetSize())
745 {
746 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000747 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000748 const char *func_name = sc.GetFunctionName().GetCString();
749 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000750 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000751 // Remove the current context
752 sc_list.RemoveContextAtIndex(i);
753 // Don't increment i and continue in the loop
754 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000755 }
756 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000757 ++i;
758 }
759 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000760 }
761 else
762 {
763 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000764 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000765 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
766
Michael Sartaina7499c92013-07-01 19:45:50 +0000767 // Now check our symbol table for symbols that are code symbols if requested
768 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000769 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000770 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000771 if (symtab)
772 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000773 }
774 }
775 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000776
777 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778}
779
Greg Claytonc7bece562013-01-25 18:06:21 +0000780size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000781Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000782 bool include_symbols,
783 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000784 bool append,
785 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786{
Greg Clayton931180e2011-01-27 06:44:37 +0000787 if (!append)
788 sc_list.Clear();
789
Greg Claytonc7bece562013-01-25 18:06:21 +0000790 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000791
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 SymbolVendor *symbols = GetSymbolVendor ();
793 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000794 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000795 symbols->FindFunctions(regex, include_inlines, append, sc_list);
796
797 // Now check our symbol table for symbols that are code symbols if requested
798 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000799 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000800 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000801 if (symtab)
802 {
803 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000804 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000805 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000806 if (num_matches)
807 {
808 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000809 const size_t end_functions_added_index = sc_list.GetSize();
810 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
811 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000812 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000813 // No functions were added, just symbols, so we can just append them
814 for (size_t i=0; i<num_matches; ++i)
815 {
816 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
817 SymbolType sym_type = sc.symbol->GetType();
818 if (sc.symbol && (sym_type == eSymbolTypeCode ||
819 sym_type == eSymbolTypeResolver))
820 sc_list.Append(sc);
821 }
822 }
823 else
824 {
825 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
826 FileAddrToIndexMap file_addr_to_index;
827 for (size_t i=start_size; i<end_functions_added_index; ++i)
828 {
829 const SymbolContext &sc = sc_list[i];
830 if (sc.block)
831 continue;
832 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
833 }
834
835 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
836 // Functions were added so we need to merge symbols into any
837 // existing function symbol contexts
838 for (size_t i=start_size; i<num_matches; ++i)
839 {
840 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
841 SymbolType sym_type = sc.symbol->GetType();
842 if (sc.symbol && (sym_type == eSymbolTypeCode ||
843 sym_type == eSymbolTypeResolver))
844 {
845 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
846 if (pos == end)
847 sc_list.Append(sc);
848 else
849 sc_list[pos->second].symbol = sc.symbol;
850 }
851 }
Greg Clayton931180e2011-01-27 06:44:37 +0000852 }
853 }
854 }
855 }
856 }
857 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858}
859
Richard Mittonf86248d2013-09-12 02:20:34 +0000860void
861Module::FindAddressesForLine (const lldb::TargetSP target_sp,
862 const FileSpec &file, uint32_t line,
863 Function *function,
864 std::vector<Address> &output_local, std::vector<Address> &output_extern)
865{
866 SearchFilterByModule filter(target_sp, m_file);
867 AddressResolverFileLine resolver(file, line, true);
868 resolver.ResolveAddress (filter);
869
870 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
871 {
872 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
873 Function *f = addr.CalculateSymbolContextFunction();
874 if (f && f == function)
875 output_local.push_back (addr);
876 else
877 output_extern.push_back (addr);
878 }
879}
880
Greg Claytonc7bece562013-01-25 18:06:21 +0000881size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000882Module::FindTypes_Impl (const SymbolContext& sc,
883 const ConstString &name,
884 const ClangNamespaceDecl *namespace_decl,
885 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000886 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000887 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000888{
889 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
890 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
891 {
892 SymbolVendor *symbols = GetSymbolVendor ();
893 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000894 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000895 }
896 return 0;
897}
898
Greg Claytonc7bece562013-01-25 18:06:21 +0000899size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000900Module::FindTypesInNamespace (const SymbolContext& sc,
901 const ConstString &type_name,
902 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000903 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000904 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000905{
Greg Clayton84db9102012-03-26 23:03:23 +0000906 const bool append = true;
907 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000908}
909
Greg Claytonb43165b2012-12-05 21:24:42 +0000910lldb::TypeSP
911Module::FindFirstType (const SymbolContext& sc,
912 const ConstString &name,
913 bool exact_match)
914{
915 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000916 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000917 if (num_matches)
918 return type_list.GetTypeAtIndex(0);
919 return TypeSP();
920}
921
922
Greg Claytonc7bece562013-01-25 18:06:21 +0000923size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000924Module::FindTypes (const SymbolContext& sc,
925 const ConstString &name,
926 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000927 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000928 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000929{
Greg Claytonc7bece562013-01-25 18:06:21 +0000930 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000931 const char *type_name_cstr = name.GetCString();
932 std::string type_scope;
933 std::string type_basename;
934 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000935 TypeClass type_class = eTypeClassAny;
936 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000937 {
Greg Clayton84db9102012-03-26 23:03:23 +0000938 // Check if "name" starts with "::" which means the qualified type starts
939 // from the root namespace and implies and exact match. The typenames we
940 // get back from clang do not start with "::" so we need to strip this off
941 // in order to get the qualfied names to match
942
943 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
944 {
945 type_scope.erase(0,2);
946 exact_match = true;
947 }
948 ConstString type_basename_const_str (type_basename.c_str());
949 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
950 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000951 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000952 num_matches = types.GetSize();
953 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000954 }
955 else
Greg Clayton84db9102012-03-26 23:03:23 +0000956 {
957 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000958 if (type_class != eTypeClassAny)
959 {
960 // The "type_name_cstr" will have been modified if we have a valid type class
961 // prefix (like "struct", "class", "union", "typedef" etc).
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +0000962 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
Greg Clayton7bc31332012-10-22 16:19:56 +0000963 types.RemoveMismatchedTypes (type_class);
964 num_matches = types.GetSize();
965 }
966 else
967 {
968 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
969 }
Greg Clayton84db9102012-03-26 23:03:23 +0000970 }
971
972 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000973
974}
975
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000977Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978{
979 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000980 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 {
982 ObjectFile *obj_file = GetObjectFile ();
983 if (obj_file != NULL)
984 {
985 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +0000986 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +0000987 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988 }
989 }
990 return m_symfile_ap.get();
991}
992
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993void
994Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
995{
996 // Container objects whose paths do not specify a file directly can call
997 // this function to correct the file and object names.
998 m_file = file;
999 m_mod_time = file.GetModificationTime();
1000 m_object_name = object_name;
1001}
1002
1003const ArchSpec&
1004Module::GetArchitecture () const
1005{
1006 return m_arch;
1007}
1008
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001009std::string
1010Module::GetSpecificationDescription () const
1011{
1012 std::string spec(GetFileSpec().GetPath());
1013 if (m_object_name)
1014 {
1015 spec += '(';
1016 spec += m_object_name.GetCString();
1017 spec += ')';
1018 }
1019 return spec;
1020}
1021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001023Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001024{
1025 Mutex::Locker locker (m_mutex);
1026
Greg Claytonc982b3d2011-11-28 01:45:00 +00001027 if (level >= eDescriptionLevelFull)
1028 {
1029 if (m_arch.IsValid())
1030 s->Printf("(%s) ", m_arch.GetArchitectureName());
1031 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001032
Greg Claytonc982b3d2011-11-28 01:45:00 +00001033 if (level == eDescriptionLevelBrief)
1034 {
1035 const char *filename = m_file.GetFilename().GetCString();
1036 if (filename)
1037 s->PutCString (filename);
1038 }
1039 else
1040 {
1041 char path[PATH_MAX];
1042 if (m_file.GetPath(path, sizeof(path)))
1043 s->PutCString(path);
1044 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001045
1046 const char *object_name = m_object_name.GetCString();
1047 if (object_name)
1048 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001049}
1050
1051void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001052Module::ReportError (const char *format, ...)
1053{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001054 if (format && format[0])
1055 {
1056 StreamString strm;
1057 strm.PutCString("error: ");
1058 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001059 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001060 va_list args;
1061 va_start (args, format);
1062 strm.PrintfVarArg(format, args);
1063 va_end (args);
1064
1065 const int format_len = strlen(format);
1066 if (format_len > 0)
1067 {
1068 const char last_char = format[format_len-1];
1069 if (last_char != '\n' || last_char != '\r')
1070 strm.EOL();
1071 }
1072 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1073
1074 }
1075}
1076
Greg Clayton1d609092012-07-12 22:51:12 +00001077bool
1078Module::FileHasChanged () const
1079{
1080 if (m_file_has_changed == false)
1081 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1082 return m_file_has_changed;
1083}
1084
Greg Claytone38a5ed2012-01-05 03:57:59 +00001085void
1086Module::ReportErrorIfModifyDetected (const char *format, ...)
1087{
Greg Clayton1d609092012-07-12 22:51:12 +00001088 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001089 {
Greg Clayton1d609092012-07-12 22:51:12 +00001090 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001091 {
Greg Clayton1d609092012-07-12 22:51:12 +00001092 m_first_file_changed_log = true;
1093 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001094 {
Greg Clayton1d609092012-07-12 22:51:12 +00001095 StreamString strm;
1096 strm.PutCString("error: the object file ");
1097 GetDescription(&strm, lldb::eDescriptionLevelFull);
1098 strm.PutCString (" has been modified\n");
1099
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 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1113 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001114 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001115 }
1116 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001117}
1118
1119void
1120Module::ReportWarning (const char *format, ...)
1121{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001122 if (format && format[0])
1123 {
1124 StreamString strm;
1125 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001126 GetDescription(&strm, lldb::eDescriptionLevelFull);
1127 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001128
1129 va_list args;
1130 va_start (args, format);
1131 strm.PrintfVarArg(format, args);
1132 va_end (args);
1133
1134 const int format_len = strlen(format);
1135 if (format_len > 0)
1136 {
1137 const char last_char = format[format_len-1];
1138 if (last_char != '\n' || last_char != '\r')
1139 strm.EOL();
1140 }
1141 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1142 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001143}
1144
1145void
1146Module::LogMessage (Log *log, const char *format, ...)
1147{
1148 if (log)
1149 {
1150 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001151 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001152 log_message.PutCString (": ");
1153 va_list args;
1154 va_start (args, format);
1155 log_message.PrintfVarArg (format, args);
1156 va_end (args);
1157 log->PutCString(log_message.GetString().c_str());
1158 }
1159}
1160
Greg Claytond61c0fc2012-04-23 22:55:20 +00001161void
1162Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1163{
1164 if (log)
1165 {
1166 StreamString log_message;
1167 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1168 log_message.PutCString (": ");
1169 va_list args;
1170 va_start (args, format);
1171 log_message.PrintfVarArg (format, args);
1172 va_end (args);
1173 if (log->GetVerbose())
1174 Host::Backtrace (log_message, 1024);
1175 log->PutCString(log_message.GetString().c_str());
1176 }
1177}
1178
Greg Claytonc982b3d2011-11-28 01:45:00 +00001179void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180Module::Dump(Stream *s)
1181{
1182 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001183 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001185 s->Printf("Module %s%s%s%s\n",
1186 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 m_object_name ? "(" : "",
1188 m_object_name ? m_object_name.GetCString() : "",
1189 m_object_name ? ")" : "");
1190
1191 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 if (objfile)
1195 objfile->Dump(s);
1196
1197 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 if (symbols)
1199 symbols->Dump(s);
1200
1201 s->IndentLess();
1202}
1203
1204
1205TypeList*
1206Module::GetTypeList ()
1207{
1208 SymbolVendor *symbols = GetSymbolVendor ();
1209 if (symbols)
1210 return &symbols->GetTypeList();
1211 return NULL;
1212}
1213
1214const ConstString &
1215Module::GetObjectName() const
1216{
1217 return m_object_name;
1218}
1219
1220ObjectFile *
1221Module::GetObjectFile()
1222{
1223 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001224 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 Timer scoped_timer(__PRETTY_FUNCTION__,
1227 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001228 DataBufferSP data_sp;
1229 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001230 const lldb::offset_t file_size = m_file.GetByteSize();
1231 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001232 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001233 m_did_load_objfile = true;
1234 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1235 &m_file,
1236 m_object_offset,
1237 file_size - m_object_offset,
1238 data_sp,
1239 data_offset);
1240 if (m_objfile_sp)
1241 {
1242 // Once we get the object file, update our module with the object file's
1243 // architecture since it might differ in vendor/os if some parts were
1244 // unknown.
1245 m_objfile_sp->GetArchitecture (m_arch);
1246 }
Greg Clayton593577a2011-09-21 03:57:31 +00001247 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 }
Greg Clayton762f7132011-09-18 18:59:15 +00001249 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250}
1251
Michael Sartaina7499c92013-07-01 19:45:50 +00001252SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001253Module::GetSectionList()
1254{
1255 // Populate m_unified_sections_ap with sections from objfile.
1256 if (m_sections_ap.get() == NULL)
1257 {
1258 ObjectFile *obj_file = GetObjectFile();
1259 if (obj_file)
1260 obj_file->CreateSections(*GetUnifiedSectionList());
1261 }
1262 return m_sections_ap.get();
1263}
1264
1265SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001266Module::GetUnifiedSectionList()
1267{
Greg Clayton3046e662013-07-10 01:23:25 +00001268 // Populate m_unified_sections_ap with sections from objfile.
1269 if (m_sections_ap.get() == NULL)
1270 m_sections_ap.reset(new SectionList());
1271 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001272}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273
1274const Symbol *
1275Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1276{
1277 Timer scoped_timer(__PRETTY_FUNCTION__,
1278 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1279 name.AsCString(),
1280 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001281 SymbolVendor* sym_vendor = GetSymbolVendor();
1282 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001284 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001286 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 }
1288 return NULL;
1289}
1290void
1291Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1292{
1293 // No need to protect this call using m_mutex all other method calls are
1294 // already thread safe.
1295
1296 size_t num_indices = symbol_indexes.size();
1297 if (num_indices > 0)
1298 {
1299 SymbolContext sc;
1300 CalculateSymbolContext (&sc);
1301 for (size_t i = 0; i < num_indices; i++)
1302 {
1303 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1304 if (sc.symbol)
1305 sc_list.Append (sc);
1306 }
1307 }
1308}
1309
1310size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001311Module::FindFunctionSymbols (const ConstString &name,
1312 uint32_t name_type_mask,
1313 SymbolContextList& sc_list)
1314{
1315 Timer scoped_timer(__PRETTY_FUNCTION__,
1316 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1317 name.AsCString(),
1318 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001319 SymbolVendor* sym_vendor = GetSymbolVendor();
1320 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001321 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001322 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001323 if (symtab)
1324 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1325 }
1326 return 0;
1327}
1328
1329size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001330Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331{
1332 // No need to protect this call using m_mutex all other method calls are
1333 // already thread safe.
1334
1335
1336 Timer scoped_timer(__PRETTY_FUNCTION__,
1337 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1338 name.AsCString(),
1339 symbol_type);
1340 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001341 SymbolVendor* sym_vendor = GetSymbolVendor();
1342 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001344 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 if (symtab)
1346 {
1347 std::vector<uint32_t> symbol_indexes;
1348 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1349 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1350 }
1351 }
1352 return sc_list.GetSize() - initial_size;
1353}
1354
1355size_t
1356Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1357{
1358 // No need to protect this call using m_mutex all other method calls are
1359 // already thread safe.
1360
1361 Timer scoped_timer(__PRETTY_FUNCTION__,
1362 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1363 regex.GetText(),
1364 symbol_type);
1365 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001366 SymbolVendor* sym_vendor = GetSymbolVendor();
1367 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001369 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 if (symtab)
1371 {
1372 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001373 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1375 }
1376 }
1377 return sc_list.GetSize() - initial_size;
1378}
1379
Greg Claytone01e07b2013-04-18 18:10:51 +00001380void
1381Module::SetSymbolFileFileSpec (const FileSpec &file)
1382{
Michael Sartaina7499c92013-07-01 19:45:50 +00001383 // Remove any sections in the unified section list that come from the current symbol vendor.
1384 if (m_symfile_ap)
1385 {
Greg Clayton3046e662013-07-10 01:23:25 +00001386 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001387 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1388 if (section_list && symbol_file)
1389 {
1390 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001391 // Make sure we have an object file and that the symbol vendor's objfile isn't
1392 // the same as the module's objfile before we remove any sections for it...
1393 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001394 {
1395 size_t num_sections = section_list->GetNumSections (0);
1396 for (size_t idx = num_sections; idx > 0; --idx)
1397 {
1398 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1399 if (section_sp->GetObjectFile() == obj_file)
1400 {
Greg Clayton3046e662013-07-10 01:23:25 +00001401 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001402 }
1403 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001404 }
1405 }
1406 }
1407
Greg Claytone01e07b2013-04-18 18:10:51 +00001408 m_symfile_spec = file;
1409 m_symfile_ap.reset();
1410 m_did_load_symbol_vendor = false;
1411}
1412
Jim Ingham5aee1622010-08-09 23:31:02 +00001413bool
1414Module::IsExecutable ()
1415{
1416 if (GetObjectFile() == NULL)
1417 return false;
1418 else
1419 return GetObjectFile()->IsExecutable();
1420}
1421
Jim Inghamb53cb272011-08-03 01:03:17 +00001422bool
1423Module::IsLoadedInTarget (Target *target)
1424{
1425 ObjectFile *obj_file = GetObjectFile();
1426 if (obj_file)
1427 {
Greg Clayton3046e662013-07-10 01:23:25 +00001428 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001429 if (sections != NULL)
1430 {
1431 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001432 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1433 {
1434 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1435 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1436 {
1437 return true;
1438 }
1439 }
1440 }
1441 }
1442 return false;
1443}
Enrico Granata17598482012-11-08 02:22:02 +00001444
1445bool
Enrico Granata97303392013-05-21 00:00:30 +00001446Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001447{
1448 if (!target)
1449 {
1450 error.SetErrorString("invalid destination Target");
1451 return false;
1452 }
1453
Enrico Granata397ddd52013-05-21 20:13:34 +00001454 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001455
Greg Clayton91c0e742013-01-11 23:44:27 +00001456 Debugger &debugger = target->GetDebugger();
1457 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1458 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001459 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001460
1461 PlatformSP platform_sp(target->GetPlatform());
1462
1463 if (!platform_sp)
1464 {
1465 error.SetErrorString("invalid Platform");
1466 return false;
1467 }
Enrico Granata17598482012-11-08 02:22:02 +00001468
Greg Claytonb9d88902013-03-23 00:50:58 +00001469 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1470 *this);
1471
1472
1473 const uint32_t num_specs = file_specs.GetSize();
1474 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001475 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001476 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1477 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001478 {
1479 for (uint32_t i=0; i<num_specs; ++i)
1480 {
1481 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1482 if (scripting_fspec && scripting_fspec.Exists())
1483 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001484 if (shoud_load == eLoadScriptFromSymFileFalse)
1485 return false;
1486 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001487 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001488 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001489 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1490 "this debug session:\n\n command script import \"%s\"\n\n"
1491 "To run all discovered debug scripts in this session:\n\n"
1492 " settings set target.load-script-from-symbol-file true\n",
1493 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1494 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001495 return false;
1496 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001497 StreamString scripting_stream;
1498 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001499 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001500 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001501 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1502 can_reload,
1503 init_lldb_globals,
1504 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001505 if (!did_load)
1506 return false;
1507 }
1508 }
1509 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001510 else
1511 {
1512 error.SetErrorString("invalid ScriptInterpreter");
1513 return false;
1514 }
Enrico Granata17598482012-11-08 02:22:02 +00001515 }
1516 }
1517 return true;
1518}
1519
1520bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001521Module::SetArchitecture (const ArchSpec &new_arch)
1522{
Greg Clayton64195a22011-02-23 00:35:02 +00001523 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001524 {
1525 m_arch = new_arch;
1526 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001527 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001528 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001529}
1530
Greg Claytonc9660542012-02-05 02:38:54 +00001531bool
Greg Clayton751caf62014-02-07 22:54:47 +00001532Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001533{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001534 ObjectFile *object_file = GetObjectFile();
1535 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001536 {
Greg Clayton751caf62014-02-07 22:54:47 +00001537 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001538 return true;
1539 }
1540 else
1541 {
1542 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001543 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001544 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001545}
1546
Greg Claytonb9a01b32012-02-26 05:51:37 +00001547
1548bool
1549Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1550{
1551 const UUID &uuid = module_ref.GetUUID();
1552
1553 if (uuid.IsValid())
1554 {
1555 // If the UUID matches, then nothing more needs to match...
1556 if (uuid == GetUUID())
1557 return true;
1558 else
1559 return false;
1560 }
1561
1562 const FileSpec &file_spec = module_ref.GetFileSpec();
1563 if (file_spec)
1564 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001565 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001566 return false;
1567 }
1568
1569 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1570 if (platform_file_spec)
1571 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001572 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001573 return false;
1574 }
1575
1576 const ArchSpec &arch = module_ref.GetArchitecture();
1577 if (arch.IsValid())
1578 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001579 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001580 return false;
1581 }
1582
1583 const ConstString &object_name = module_ref.GetObjectName();
1584 if (object_name)
1585 {
1586 if (object_name != GetObjectName())
1587 return false;
1588 }
1589 return true;
1590}
1591
Greg Claytond804d282012-03-15 21:01:31 +00001592bool
1593Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1594{
1595 Mutex::Locker locker (m_mutex);
1596 return m_source_mappings.FindFile (orig_spec, new_spec);
1597}
1598
Greg Claytonf9be6932012-03-19 22:22:41 +00001599bool
1600Module::RemapSourceFile (const char *path, std::string &new_path) const
1601{
1602 Mutex::Locker locker (m_mutex);
1603 return m_source_mappings.RemapPath(path, new_path);
1604}
1605
Enrico Granata3467d802012-09-04 18:47:54 +00001606uint32_t
1607Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1608{
1609 ObjectFile *obj_file = GetObjectFile();
1610 if (obj_file)
1611 return obj_file->GetVersion (versions, num_versions);
1612
1613 if (versions && num_versions)
1614 {
1615 for (uint32_t i=0; i<num_versions; ++i)
1616 versions[i] = UINT32_MAX;
1617 }
1618 return 0;
1619}
Greg Clayton43fe2172013-04-03 02:00:15 +00001620
1621void
1622Module::PrepareForFunctionNameLookup (const ConstString &name,
1623 uint32_t name_type_mask,
1624 ConstString &lookup_name,
1625 uint32_t &lookup_name_type_mask,
1626 bool &match_name_after_lookup)
1627{
1628 const char *name_cstr = name.GetCString();
1629 lookup_name_type_mask = eFunctionNameTypeNone;
1630 match_name_after_lookup = false;
1631 const char *base_name_start = NULL;
1632 const char *base_name_end = NULL;
1633
1634 if (name_type_mask & eFunctionNameTypeAuto)
1635 {
1636 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1637 lookup_name_type_mask = eFunctionNameTypeFull;
1638 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1639 lookup_name_type_mask = eFunctionNameTypeFull;
1640 else
1641 {
1642 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1643 lookup_name_type_mask |= eFunctionNameTypeSelector;
1644
Greg Clayton6ecb2322013-05-18 00:11:21 +00001645 CPPLanguageRuntime::MethodName cpp_method (name);
1646 llvm::StringRef basename (cpp_method.GetBasename());
1647 if (basename.empty())
1648 {
1649 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1650 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1651 }
1652 else
1653 {
1654 base_name_start = basename.data();
1655 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001656 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001657 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001658 }
1659 }
1660 else
1661 {
1662 lookup_name_type_mask = name_type_mask;
1663 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1664 {
1665 // If they've asked for a CPP method or function name and it can't be that, we don't
1666 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001667 CPPLanguageRuntime::MethodName cpp_method (name);
1668 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001669 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001670 llvm::StringRef basename (cpp_method.GetBasename());
1671 base_name_start = basename.data();
1672 base_name_end = base_name_start + basename.size();
1673
1674 if (!cpp_method.GetQualifiers().empty())
1675 {
1676 // There is a "const" or other qualifer following the end of the fucntion parens,
1677 // this can't be a eFunctionNameTypeBase
1678 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1679 if (lookup_name_type_mask == eFunctionNameTypeNone)
1680 return;
1681 }
1682 }
1683 else
1684 {
1685 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1686 {
1687 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1688 if (lookup_name_type_mask == eFunctionNameTypeNone)
1689 return;
1690 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001691 }
1692 }
1693
1694 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1695 {
1696 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1697 {
1698 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1699 if (lookup_name_type_mask == eFunctionNameTypeNone)
1700 return;
1701 }
1702 }
1703 }
1704
1705 if (base_name_start &&
1706 base_name_end &&
1707 base_name_start != name_cstr &&
1708 base_name_start < base_name_end)
1709 {
1710 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1711 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1712 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1713 // to true
1714 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1715 match_name_after_lookup = true;
1716 }
1717 else
1718 {
1719 // The name is already correct, just use the exact name as supplied, and we won't need
1720 // to check if any matches contain "name"
1721 lookup_name = name;
1722 match_name_after_lookup = false;
1723 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001724}