blob: 1672eed8fb965604382e545cbda0716864a85315 [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
Greg Clayton23f8c952014-03-24 23:10:19 +000040#include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042using namespace lldb;
43using namespace lldb_private;
44
Greg Clayton65a03992011-08-09 00:01:09 +000045// Shared pointers to modules track module lifetimes in
46// targets and in the global module, but this collection
47// will track all module objects that are still alive
48typedef std::vector<Module *> ModuleCollection;
49
50static ModuleCollection &
51GetModuleCollection()
52{
Jim Ingham549f7372011-10-31 23:47:10 +000053 // This module collection needs to live past any module, so we could either make it a
54 // shared pointer in each module or just leak is. Since it is only an empty vector by
55 // the time all the modules have gone away, we just leak it for now. If we decide this
56 // is a big problem we can introduce a Finalize method that will tear everything down in
57 // a predictable order.
58
59 static ModuleCollection *g_module_collection = NULL;
60 if (g_module_collection == NULL)
61 g_module_collection = new ModuleCollection();
62
63 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000064}
65
Greg Claytonb26e6be2012-01-27 18:08:35 +000066Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000067Module::GetAllocationModuleCollectionMutex()
68{
Greg Claytonb26e6be2012-01-27 18:08:35 +000069 // NOTE: The mutex below must be leaked since the global module list in
70 // the ModuleList class will get torn at some point, and we can't know
71 // if it will tear itself down before the "g_module_collection_mutex" below
72 // will. So we leak a Mutex object below to safeguard against that
73
74 static Mutex *g_module_collection_mutex = NULL;
75 if (g_module_collection_mutex == NULL)
76 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
77 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000078}
79
80size_t
81Module::GetNumberAllocatedModules ()
82{
83 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
84 return GetModuleCollection().size();
85}
86
87Module *
88Module::GetAllocatedModuleAtIndex (size_t idx)
89{
90 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
91 ModuleCollection &modules = GetModuleCollection();
92 if (idx < modules.size())
93 return modules[idx];
94 return NULL;
95}
Greg Clayton29ad7b92012-01-27 18:45:39 +000096#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000097
Greg Clayton29ad7b92012-01-27 18:45:39 +000098// These functions help us to determine if modules are still loaded, yet don't require that
99// you have a command interpreter and can easily be called from an external debugger.
100namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +0000101
Greg Clayton29ad7b92012-01-27 18:45:39 +0000102 void
103 ClearModuleInfo (void)
104 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000105 const bool mandatory = true;
106 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000107 }
108
109 void
110 DumpModuleInfo (void)
111 {
112 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
113 ModuleCollection &modules = GetModuleCollection();
114 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000115 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000116 for (size_t i=0; i<count; ++i)
117 {
118
119 StreamString strm;
120 Module *module = modules[i];
121 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
122 module->GetDescription(&strm, eDescriptionLevelFull);
123 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
124 module,
125 in_shared_module_list,
126 (uint32_t)module->use_count(),
127 strm.GetString().c_str());
128 }
129 }
130}
131
132#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000133
Greg Claytonb9a01b32012-02-26 05:51:37 +0000134Module::Module (const ModuleSpec &module_spec) :
135 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton34f11592014-03-04 21:20:23 +0000136 m_mod_time (),
137 m_arch (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000138 m_uuid (),
Greg Clayton34f11592014-03-04 21:20:23 +0000139 m_file (),
140 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000141 m_remote_install_file(),
Greg Clayton34f11592014-03-04 21:20:23 +0000142 m_symfile_spec (),
143 m_object_name (),
144 m_object_offset (),
145 m_object_mod_time (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000146 m_objfile_sp (),
147 m_symfile_ap (),
148 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000149 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000150 m_sections_ap(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000151 m_did_load_objfile (false),
152 m_did_load_symbol_vendor (false),
153 m_did_parse_uuid (false),
154 m_did_init_ast (false),
155 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000156 m_file_has_changed (false),
157 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000158{
159 // Scope for locker below...
160 {
161 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
162 GetModuleCollection().push_back(this);
163 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000164
Greg Clayton5160ce52013-03-27 23:08:40 +0000165 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000166 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000167 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000168 static_cast<void*>(this),
Greg Clayton34f11592014-03-04 21:20:23 +0000169 module_spec.GetArchitecture().GetArchitectureName(),
170 module_spec.GetFileSpec().GetPath().c_str(),
171 module_spec.GetObjectName().IsEmpty() ? "" : "(",
172 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
173 module_spec.GetObjectName().IsEmpty() ? "" : ")");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000174
Greg Clayton34f11592014-03-04 21:20:23 +0000175 // First extract all module specifications from the file using the local
176 // file path. If there are no specifications, then don't fill anything in
177 ModuleSpecList modules_specs;
178 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
179 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000180
Greg Clayton34f11592014-03-04 21:20:23 +0000181 // Now make sure that one of the module specifications matches what we just
182 // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
183 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
184 // UUID YYY and we don't want those to match. If they don't match, just don't
185 // fill any ivars in so we don't accidentally grab the wrong file later since
186 // they don't match...
187 ModuleSpec matching_module_spec;
188 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
189 return;
Greg Clayton7ab7f892014-05-29 21:33:45 +0000190
191 if (module_spec.GetFileSpec())
192 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
193 else if (matching_module_spec.GetFileSpec())
194 m_mod_time = matching_module_spec.GetFileSpec().GetModificationTime();
195
196 // Copy the architecture from the actual spec if we got one back, else use the one that was specified
197 if (matching_module_spec.GetArchitecture().IsValid())
Greg Clayton34f11592014-03-04 21:20:23 +0000198 m_arch = matching_module_spec.GetArchitecture();
Greg Clayton7ab7f892014-05-29 21:33:45 +0000199 else if (module_spec.GetArchitecture().IsValid())
200 m_arch = module_spec.GetArchitecture();
201
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000202 // Copy the file spec over and use the specified one (if there was one) so we
Greg Clayton7ab7f892014-05-29 21:33:45 +0000203 // don't use a path that might have gotten resolved a path in 'matching_module_spec'
204 if (module_spec.GetFileSpec())
205 m_file = module_spec.GetFileSpec();
206 else if (matching_module_spec.GetFileSpec())
207 m_file = matching_module_spec.GetFileSpec();
208
209 // Copy the platform file spec over
210 if (module_spec.GetPlatformFileSpec())
211 m_platform_file = module_spec.GetPlatformFileSpec();
212 else if (matching_module_spec.GetPlatformFileSpec())
213 m_platform_file = matching_module_spec.GetPlatformFileSpec();
214
215 // Copy the symbol file spec over
216 if (module_spec.GetSymbolFileSpec())
217 m_symfile_spec = module_spec.GetSymbolFileSpec();
218 else if (matching_module_spec.GetSymbolFileSpec())
219 m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
220
221 // Copy the object name over
222 if (matching_module_spec.GetObjectName())
223 m_object_name = matching_module_spec.GetObjectName();
224 else
225 m_object_name = module_spec.GetObjectName();
226
227 // Always trust the object offset (file offset) and object modification
228 // time (for mod time in a BSD static archive) of from the matching
229 // module specification
Greg Clayton36d7c892014-05-29 17:52:46 +0000230 m_object_offset = matching_module_spec.GetObjectOffset();
231 m_object_mod_time = matching_module_spec.GetObjectModificationTime();
Greg Clayton34f11592014-03-04 21:20:23 +0000232
Greg Claytonb9a01b32012-02-26 05:51:37 +0000233}
234
Greg Claytone72dfb32012-02-24 01:59:29 +0000235Module::Module(const FileSpec& file_spec,
236 const ArchSpec& arch,
237 const ConstString *object_name,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000238 lldb::offset_t object_offset,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000239 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 m_mutex (Mutex::eMutexTypeRecursive),
241 m_mod_time (file_spec.GetModificationTime()),
242 m_arch (arch),
243 m_uuid (),
244 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000245 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000246 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000247 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000249 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000250 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000251 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000252 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000253 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000254 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000255 m_sections_ap(),
Greg Claytone83e7312010-09-07 23:40:05 +0000256 m_did_load_objfile (false),
257 m_did_load_symbol_vendor (false),
258 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000259 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000260 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000261 m_file_has_changed (false),
262 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263{
Greg Clayton65a03992011-08-09 00:01:09 +0000264 // Scope for locker below...
265 {
266 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
267 GetModuleCollection().push_back(this);
268 }
269
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 if (object_name)
271 m_object_name = *object_name;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000272
Greg Clayton57abc5d2013-05-10 21:47:16 +0000273 if (object_mod_time_ptr)
274 m_object_mod_time = *object_mod_time_ptr;
275
Greg Clayton5160ce52013-03-27 23:08:40 +0000276 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000278 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279 static_cast<void*>(this), m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000280 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_object_name.IsEmpty() ? "" : "(",
282 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
283 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284}
285
Greg Clayton23f8c952014-03-24 23:10:19 +0000286Module::Module () :
287 m_mutex (Mutex::eMutexTypeRecursive),
288 m_mod_time (),
289 m_arch (),
290 m_uuid (),
291 m_file (),
292 m_platform_file(),
293 m_remote_install_file (),
294 m_symfile_spec (),
295 m_object_name (),
296 m_object_offset (0),
297 m_object_mod_time (),
298 m_objfile_sp (),
299 m_symfile_ap (),
300 m_ast (),
301 m_source_mappings (),
302 m_sections_ap(),
303 m_did_load_objfile (false),
304 m_did_load_symbol_vendor (false),
305 m_did_parse_uuid (false),
306 m_did_init_ast (false),
307 m_is_dynamic_loader_module (false),
308 m_file_has_changed (false),
309 m_first_file_changed_log (false)
310{
311 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
312 GetModuleCollection().push_back(this);
313}
314
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315Module::~Module()
316{
Greg Clayton217b28b2013-05-22 20:13:22 +0000317 // Lock our module down while we tear everything down to make sure
318 // we don't get any access to the module while it is being destroyed
319 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000320 // Scope for locker below...
321 {
322 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
323 ModuleCollection &modules = GetModuleCollection();
324 ModuleCollection::iterator end = modules.end();
325 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000326 assert (pos != end);
327 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000328 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000329 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000331 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000332 static_cast<void*>(this),
Greg Clayton64195a22011-02-23 00:35:02 +0000333 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000334 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 m_object_name.IsEmpty() ? "" : "(",
336 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
337 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000338 // Release any auto pointers before we start tearing down our member
339 // variables since the object file and symbol files might need to make
340 // function calls back into this module object. The ordering is important
341 // here because symbol files can require the module object file. So we tear
342 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000343 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000345 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000348ObjectFile *
Andrew MacPherson17220c12014-03-05 10:12:43 +0000349Module::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 +0000350{
351 if (m_objfile_sp)
352 {
353 error.SetErrorString ("object file already exists");
354 }
355 else
356 {
357 Mutex::Locker locker (m_mutex);
358 if (process_sp)
359 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000360 m_did_load_objfile = true;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000361 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000362 Error readmem_error;
363 const size_t bytes_read = process_sp->ReadMemory (header_addr,
364 data_ap->GetBytes(),
365 data_ap->GetByteSize(),
366 readmem_error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000367 if (bytes_read == size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000368 {
369 DataBufferSP data_sp(data_ap.release());
370 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
371 if (m_objfile_sp)
372 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000373 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000374 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000375 m_object_name.SetCString (s.GetData());
376
377 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000378 // architecture since it might differ in vendor/os if some parts were
379 // unknown.
380 m_objfile_sp->GetArchitecture (m_arch);
381 }
382 else
383 {
384 error.SetErrorString ("unable to find suitable object file plug-in");
385 }
386 }
387 else
388 {
389 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
390 }
391 }
392 else
393 {
394 error.SetErrorString ("invalid process");
395 }
396 }
397 return m_objfile_sp.get();
398}
399
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400
Greg Clayton60830262011-02-04 18:53:10 +0000401const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402Module::GetUUID()
403{
404 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000405 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 {
407 ObjectFile * obj_file = GetObjectFile ();
408
409 if (obj_file != NULL)
410 {
411 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000412 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 }
414 }
415 return m_uuid;
416}
417
Greg Clayton6beaaa62011-01-17 03:46:26 +0000418ClangASTContext &
419Module::GetClangASTContext ()
420{
421 Mutex::Locker locker (m_mutex);
422 if (m_did_init_ast == false)
423 {
424 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000425 ArchSpec object_arch;
426 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000427 {
428 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000429
430 // LLVM wants this to be set to iOS or MacOSX; if we're working on
431 // a bare-boards type image, change the triple for llvm's benefit.
432 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
433 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
434 {
435 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
Todd Fialad8eaa172014-07-23 14:37:35 +0000436 object_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
Jason Molenda981d4df2012-10-16 20:45:49 +0000437 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
438 {
439 object_arch.GetTriple().setOS(llvm::Triple::IOS);
440 }
441 else
442 {
443 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
444 }
445 }
Greg Clayton514487e2011-02-15 21:59:32 +0000446 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000447 }
448 }
449 return m_ast;
450}
451
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452void
453Module::ParseAllDebugSymbols()
454{
455 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000456 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 if (num_comp_units == 0)
458 return;
459
Greg Claytona2eee182011-09-17 07:23:18 +0000460 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000461 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 SymbolVendor *symbols = GetSymbolVendor ();
463
Greg Claytonc7bece562013-01-25 18:06:21 +0000464 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 {
466 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
467 if (sc.comp_unit)
468 {
469 sc.function = NULL;
470 symbols->ParseVariablesForContext(sc);
471
472 symbols->ParseCompileUnitFunctions(sc);
473
Greg Claytonc7bece562013-01-25 18:06:21 +0000474 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 +0000475 {
476 symbols->ParseFunctionBlocks(sc);
477
478 // Parse the variables for this function and all its blocks
479 symbols->ParseVariablesForContext(sc);
480 }
481
482
483 // Parse all types for this compile unit
484 sc.function = NULL;
485 symbols->ParseTypes(sc);
486 }
487 }
488}
489
490void
491Module::CalculateSymbolContext(SymbolContext* sc)
492{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000493 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494}
495
Greg Claytone72dfb32012-02-24 01:59:29 +0000496ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000497Module::CalculateSymbolContextModule ()
498{
Greg Claytone72dfb32012-02-24 01:59:29 +0000499 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000500}
501
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502void
503Module::DumpSymbolContext(Stream *s)
504{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000505 s->Printf(", Module{%p}", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506}
507
Greg Claytonc7bece562013-01-25 18:06:21 +0000508size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509Module::GetNumCompileUnits()
510{
511 Mutex::Locker locker (m_mutex);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000512 Timer scoped_timer(__PRETTY_FUNCTION__,
513 "Module::GetNumCompileUnits (module = %p)",
514 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 SymbolVendor *symbols = GetSymbolVendor ();
516 if (symbols)
517 return symbols->GetNumCompileUnits();
518 return 0;
519}
520
521CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000522Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523{
524 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000525 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 CompUnitSP cu_sp;
527
528 if (index < num_comp_units)
529 {
530 SymbolVendor *symbols = GetSymbolVendor ();
531 if (symbols)
532 cu_sp = symbols->GetCompileUnitAtIndex(index);
533 }
534 return cu_sp;
535}
536
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537bool
538Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
539{
540 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000541 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000542 SectionList *section_list = GetSectionList();
543 if (section_list)
544 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 return false;
546}
547
548uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000549Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
550 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551{
552 Mutex::Locker locker (m_mutex);
553 uint32_t resolved_flags = 0;
554
Greg Clayton72310352013-02-23 04:12:47 +0000555 // Clear the result symbol context in case we don't find anything, but don't clear the target
556 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557
558 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000559 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560
561 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000562 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 {
564 // If the section offset based address resolved itself, then this
565 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000566 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 resolved_flags |= eSymbolContextModule;
568
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000569 SymbolVendor* sym_vendor = GetSymbolVendor();
570 if (!sym_vendor)
571 return resolved_flags;
572
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 // Resolve the compile unit, function, block, line table or line
574 // entry if requested.
575 if (resolve_scope & eSymbolContextCompUnit ||
576 resolve_scope & eSymbolContextFunction ||
577 resolve_scope & eSymbolContextBlock ||
578 resolve_scope & eSymbolContextLineEntry )
579 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000580 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 }
582
Jim Ingham680e1772010-08-31 23:51:36 +0000583 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
584 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000586 Symtab *symtab = sym_vendor->GetSymtab();
587 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000589 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000590 if (!sc.symbol &&
591 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
592 {
593 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
594 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
595 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
596 }
597
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000598 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000599 {
600 if (sc.symbol->IsSynthetic())
601 {
602 // We have a synthetic symbol so lets check if the object file
603 // from the symbol file in the symbol vendor is different than
604 // the object file for the module, and if so search its symbol
605 // table to see if we can come up with a better symbol. For example
606 // dSYM files on MacOSX have an unstripped symbol table inside of
607 // them.
608 ObjectFile *symtab_objfile = symtab->GetObjectFile();
609 if (symtab_objfile && symtab_objfile->IsStripped())
610 {
611 SymbolFile *symfile = sym_vendor->GetSymbolFile();
612 if (symfile)
613 {
614 ObjectFile *symfile_objfile = symfile->GetObjectFile();
615 if (symfile_objfile != symtab_objfile)
616 {
617 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
618 if (symfile_symtab)
619 {
620 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
621 if (symbol && !symbol->IsSynthetic())
622 {
623 sc.symbol = symbol;
624 }
625 }
626 }
627 }
628 }
629 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000630 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000631 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000632 }
633 }
634
635 // For function symbols, so_addr may be off by one. This is a convention consistent
636 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
637 // symbol lookup for disassembly and unwind.
638 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000639 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000640 {
641 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000642 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000643
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000644 bool do_resolve_tail_call_address = false; // prevent recursion
645 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
646 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000647 if (flags & eSymbolContextSymbol)
648 {
649 AddressRange addr_range;
650 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000652 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000654 // If the requested address is one past the address range of a function (i.e. a tail call),
655 // or the decremented address is the start of a function (i.e. some forms of trampoline),
656 // indicate that the symbol has been resolved.
657 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
658 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
659 {
660 resolved_flags |= flags;
661 }
662 }
663 else
664 {
665 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 }
667 }
668 }
669 }
670 }
671 return resolved_flags;
672}
673
674uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000675Module::ResolveSymbolContextForFilePath
676(
677 const char *file_path,
678 uint32_t line,
679 bool check_inlines,
680 uint32_t resolve_scope,
681 SymbolContextList& sc_list
682)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683{
Greg Clayton274060b2010-10-20 20:54:39 +0000684 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
686}
687
688uint32_t
689Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
690{
691 Mutex::Locker locker (m_mutex);
692 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000693 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
694 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 line,
696 check_inlines ? "yes" : "no",
697 resolve_scope);
698
699 const uint32_t initial_count = sc_list.GetSize();
700
701 SymbolVendor *symbols = GetSymbolVendor ();
702 if (symbols)
703 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
704
705 return sc_list.GetSize() - initial_count;
706}
707
708
Greg Claytonc7bece562013-01-25 18:06:21 +0000709size_t
710Module::FindGlobalVariables (const ConstString &name,
711 const ClangNamespaceDecl *namespace_decl,
712 bool append,
713 size_t max_matches,
714 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715{
716 SymbolVendor *symbols = GetSymbolVendor ();
717 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000718 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 return 0;
720}
Greg Claytonc7bece562013-01-25 18:06:21 +0000721
722size_t
723Module::FindGlobalVariables (const RegularExpression& regex,
724 bool append,
725 size_t max_matches,
726 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727{
728 SymbolVendor *symbols = GetSymbolVendor ();
729 if (symbols)
730 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
731 return 0;
732}
733
Greg Claytonc7bece562013-01-25 18:06:21 +0000734size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000735Module::FindCompileUnits (const FileSpec &path,
736 bool append,
737 SymbolContextList &sc_list)
738{
739 if (!append)
740 sc_list.Clear();
741
Greg Claytonc7bece562013-01-25 18:06:21 +0000742 const size_t start_size = sc_list.GetSize();
743 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000744 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000745 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000746 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000747 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000748 {
749 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000750 if (sc.comp_unit)
751 {
752 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
753 sc_list.Append(sc);
754 }
Greg Clayton644247c2011-07-07 01:59:51 +0000755 }
756 return sc_list.GetSize() - start_size;
757}
758
Greg Claytonc7bece562013-01-25 18:06:21 +0000759size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000760Module::FindFunctions (const ConstString &name,
761 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000762 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000763 bool include_symbols,
764 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000765 bool append,
766 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767{
Greg Clayton931180e2011-01-27 06:44:37 +0000768 if (!append)
769 sc_list.Clear();
770
Greg Clayton43fe2172013-04-03 02:00:15 +0000771 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000772
773 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000775
776 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000777 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000778 ConstString lookup_name;
779 uint32_t lookup_name_type_mask = 0;
780 bool match_name_after_lookup = false;
781 Module::PrepareForFunctionNameLookup (name,
782 name_type_mask,
783 lookup_name,
784 lookup_name_type_mask,
785 match_name_after_lookup);
786
787 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000788 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000789 symbols->FindFunctions(lookup_name,
790 namespace_decl,
791 lookup_name_type_mask,
792 include_inlines,
793 append,
794 sc_list);
795
Michael Sartaina7499c92013-07-01 19:45:50 +0000796 // Now check our symbol table for symbols that are code symbols if requested
797 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000798 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000799 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000800 if (symtab)
801 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
802 }
803 }
804
805 if (match_name_after_lookup)
806 {
807 SymbolContext sc;
808 size_t i = old_size;
809 while (i<sc_list.GetSize())
810 {
811 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000812 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000813 const char *func_name = sc.GetFunctionName().GetCString();
814 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000815 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000816 // Remove the current context
817 sc_list.RemoveContextAtIndex(i);
818 // Don't increment i and continue in the loop
819 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000820 }
821 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000822 ++i;
823 }
824 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000825 }
826 else
827 {
828 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000829 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000830 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
831
Michael Sartaina7499c92013-07-01 19:45:50 +0000832 // Now check our symbol table for symbols that are code symbols if requested
833 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000834 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000835 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000836 if (symtab)
837 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000838 }
839 }
840 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000841
842 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843}
844
Greg Claytonc7bece562013-01-25 18:06:21 +0000845size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000846Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000847 bool include_symbols,
848 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000849 bool append,
850 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851{
Greg Clayton931180e2011-01-27 06:44:37 +0000852 if (!append)
853 sc_list.Clear();
854
Greg Claytonc7bece562013-01-25 18:06:21 +0000855 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000856
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 SymbolVendor *symbols = GetSymbolVendor ();
858 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000859 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000860 symbols->FindFunctions(regex, include_inlines, append, sc_list);
861
862 // Now check our symbol table for symbols that are code symbols if requested
863 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000864 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000865 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000866 if (symtab)
867 {
868 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000869 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000870 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000871 if (num_matches)
872 {
873 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000874 const size_t end_functions_added_index = sc_list.GetSize();
875 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
876 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000877 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000878 // No functions were added, just symbols, so we can just append them
879 for (size_t i=0; i<num_matches; ++i)
880 {
881 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
882 SymbolType sym_type = sc.symbol->GetType();
883 if (sc.symbol && (sym_type == eSymbolTypeCode ||
884 sym_type == eSymbolTypeResolver))
885 sc_list.Append(sc);
886 }
887 }
888 else
889 {
890 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
891 FileAddrToIndexMap file_addr_to_index;
892 for (size_t i=start_size; i<end_functions_added_index; ++i)
893 {
894 const SymbolContext &sc = sc_list[i];
895 if (sc.block)
896 continue;
897 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
898 }
899
900 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
901 // Functions were added so we need to merge symbols into any
902 // existing function symbol contexts
903 for (size_t i=start_size; i<num_matches; ++i)
904 {
905 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
906 SymbolType sym_type = sc.symbol->GetType();
907 if (sc.symbol && (sym_type == eSymbolTypeCode ||
908 sym_type == eSymbolTypeResolver))
909 {
910 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
911 if (pos == end)
912 sc_list.Append(sc);
913 else
914 sc_list[pos->second].symbol = sc.symbol;
915 }
916 }
Greg Clayton931180e2011-01-27 06:44:37 +0000917 }
918 }
919 }
920 }
921 }
922 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923}
924
Richard Mittonf86248d2013-09-12 02:20:34 +0000925void
926Module::FindAddressesForLine (const lldb::TargetSP target_sp,
927 const FileSpec &file, uint32_t line,
928 Function *function,
929 std::vector<Address> &output_local, std::vector<Address> &output_extern)
930{
931 SearchFilterByModule filter(target_sp, m_file);
932 AddressResolverFileLine resolver(file, line, true);
933 resolver.ResolveAddress (filter);
934
935 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
936 {
937 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
938 Function *f = addr.CalculateSymbolContextFunction();
939 if (f && f == function)
940 output_local.push_back (addr);
941 else
942 output_extern.push_back (addr);
943 }
944}
945
Greg Claytonc7bece562013-01-25 18:06:21 +0000946size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000947Module::FindTypes_Impl (const SymbolContext& sc,
948 const ConstString &name,
949 const ClangNamespaceDecl *namespace_decl,
950 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000951 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000952 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000953{
954 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
955 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
956 {
957 SymbolVendor *symbols = GetSymbolVendor ();
958 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000959 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000960 }
961 return 0;
962}
963
Greg Claytonc7bece562013-01-25 18:06:21 +0000964size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000965Module::FindTypesInNamespace (const SymbolContext& sc,
966 const ConstString &type_name,
967 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000968 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000969 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000970{
Greg Clayton84db9102012-03-26 23:03:23 +0000971 const bool append = true;
972 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000973}
974
Greg Claytonb43165b2012-12-05 21:24:42 +0000975lldb::TypeSP
976Module::FindFirstType (const SymbolContext& sc,
977 const ConstString &name,
978 bool exact_match)
979{
980 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000981 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000982 if (num_matches)
983 return type_list.GetTypeAtIndex(0);
984 return TypeSP();
985}
986
987
Greg Claytonc7bece562013-01-25 18:06:21 +0000988size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000989Module::FindTypes (const SymbolContext& sc,
990 const ConstString &name,
991 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000992 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000993 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000994{
Greg Claytonc7bece562013-01-25 18:06:21 +0000995 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000996 const char *type_name_cstr = name.GetCString();
997 std::string type_scope;
998 std::string type_basename;
999 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +00001000 TypeClass type_class = eTypeClassAny;
1001 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +00001002 {
Greg Clayton84db9102012-03-26 23:03:23 +00001003 // Check if "name" starts with "::" which means the qualified type starts
1004 // from the root namespace and implies and exact match. The typenames we
1005 // get back from clang do not start with "::" so we need to strip this off
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001006 // in order to get the qualified names to match
Greg Clayton84db9102012-03-26 23:03:23 +00001007
1008 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
1009 {
1010 type_scope.erase(0,2);
1011 exact_match = true;
1012 }
1013 ConstString type_basename_const_str (type_basename.c_str());
1014 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
1015 {
Greg Clayton7bc31332012-10-22 16:19:56 +00001016 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +00001017 num_matches = types.GetSize();
1018 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001019 }
1020 else
Greg Clayton84db9102012-03-26 23:03:23 +00001021 {
1022 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +00001023 if (type_class != eTypeClassAny)
1024 {
1025 // The "type_name_cstr" will have been modified if we have a valid type class
1026 // prefix (like "struct", "class", "union", "typedef" etc).
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001027 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
Greg Clayton7bc31332012-10-22 16:19:56 +00001028 types.RemoveMismatchedTypes (type_class);
1029 num_matches = types.GetSize();
1030 }
1031 else
1032 {
1033 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
1034 }
Greg Clayton84db9102012-03-26 23:03:23 +00001035 }
1036
1037 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001038
1039}
1040
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +00001042Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043{
1044 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001045 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 {
1047 ObjectFile *obj_file = GetObjectFile ();
1048 if (obj_file != NULL)
1049 {
1050 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +00001051 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +00001052 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053 }
1054 }
1055 return m_symfile_ap.get();
1056}
1057
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058void
1059Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
1060{
1061 // Container objects whose paths do not specify a file directly can call
1062 // this function to correct the file and object names.
1063 m_file = file;
1064 m_mod_time = file.GetModificationTime();
1065 m_object_name = object_name;
1066}
1067
1068const ArchSpec&
1069Module::GetArchitecture () const
1070{
1071 return m_arch;
1072}
1073
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001074std::string
1075Module::GetSpecificationDescription () const
1076{
1077 std::string spec(GetFileSpec().GetPath());
1078 if (m_object_name)
1079 {
1080 spec += '(';
1081 spec += m_object_name.GetCString();
1082 spec += ')';
1083 }
1084 return spec;
1085}
1086
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001088Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001089{
1090 Mutex::Locker locker (m_mutex);
1091
Greg Claytonc982b3d2011-11-28 01:45:00 +00001092 if (level >= eDescriptionLevelFull)
1093 {
1094 if (m_arch.IsValid())
1095 s->Printf("(%s) ", m_arch.GetArchitectureName());
1096 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001097
Greg Claytonc982b3d2011-11-28 01:45:00 +00001098 if (level == eDescriptionLevelBrief)
1099 {
1100 const char *filename = m_file.GetFilename().GetCString();
1101 if (filename)
1102 s->PutCString (filename);
1103 }
1104 else
1105 {
1106 char path[PATH_MAX];
1107 if (m_file.GetPath(path, sizeof(path)))
1108 s->PutCString(path);
1109 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001110
1111 const char *object_name = m_object_name.GetCString();
1112 if (object_name)
1113 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001114}
1115
1116void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001117Module::ReportError (const char *format, ...)
1118{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001119 if (format && format[0])
1120 {
1121 StreamString strm;
1122 strm.PutCString("error: ");
1123 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001124 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001125 va_list args;
1126 va_start (args, format);
1127 strm.PrintfVarArg(format, args);
1128 va_end (args);
1129
1130 const int format_len = strlen(format);
1131 if (format_len > 0)
1132 {
1133 const char last_char = format[format_len-1];
1134 if (last_char != '\n' || last_char != '\r')
1135 strm.EOL();
1136 }
1137 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1138
1139 }
1140}
1141
Greg Clayton1d609092012-07-12 22:51:12 +00001142bool
1143Module::FileHasChanged () const
1144{
1145 if (m_file_has_changed == false)
1146 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1147 return m_file_has_changed;
1148}
1149
Greg Claytone38a5ed2012-01-05 03:57:59 +00001150void
1151Module::ReportErrorIfModifyDetected (const char *format, ...)
1152{
Greg Clayton1d609092012-07-12 22:51:12 +00001153 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001154 {
Greg Clayton1d609092012-07-12 22:51:12 +00001155 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001156 {
Greg Clayton1d609092012-07-12 22:51:12 +00001157 m_first_file_changed_log = true;
1158 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001159 {
Greg Clayton1d609092012-07-12 22:51:12 +00001160 StreamString strm;
1161 strm.PutCString("error: the object file ");
1162 GetDescription(&strm, lldb::eDescriptionLevelFull);
1163 strm.PutCString (" has been modified\n");
1164
1165 va_list args;
1166 va_start (args, format);
1167 strm.PrintfVarArg(format, args);
1168 va_end (args);
1169
1170 const int format_len = strlen(format);
1171 if (format_len > 0)
1172 {
1173 const char last_char = format[format_len-1];
1174 if (last_char != '\n' || last_char != '\r')
1175 strm.EOL();
1176 }
1177 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1178 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001179 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001180 }
1181 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001182}
1183
1184void
1185Module::ReportWarning (const char *format, ...)
1186{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001187 if (format && format[0])
1188 {
1189 StreamString strm;
1190 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001191 GetDescription(&strm, lldb::eDescriptionLevelFull);
1192 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001193
1194 va_list args;
1195 va_start (args, format);
1196 strm.PrintfVarArg(format, args);
1197 va_end (args);
1198
1199 const int format_len = strlen(format);
1200 if (format_len > 0)
1201 {
1202 const char last_char = format[format_len-1];
1203 if (last_char != '\n' || last_char != '\r')
1204 strm.EOL();
1205 }
1206 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1207 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001208}
1209
1210void
1211Module::LogMessage (Log *log, const char *format, ...)
1212{
1213 if (log)
1214 {
1215 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001216 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001217 log_message.PutCString (": ");
1218 va_list args;
1219 va_start (args, format);
1220 log_message.PrintfVarArg (format, args);
1221 va_end (args);
1222 log->PutCString(log_message.GetString().c_str());
1223 }
1224}
1225
Greg Claytond61c0fc2012-04-23 22:55:20 +00001226void
1227Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1228{
1229 if (log)
1230 {
1231 StreamString log_message;
1232 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1233 log_message.PutCString (": ");
1234 va_list args;
1235 va_start (args, format);
1236 log_message.PrintfVarArg (format, args);
1237 va_end (args);
1238 if (log->GetVerbose())
1239 Host::Backtrace (log_message, 1024);
1240 log->PutCString(log_message.GetString().c_str());
1241 }
1242}
1243
Greg Claytonc982b3d2011-11-28 01:45:00 +00001244void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245Module::Dump(Stream *s)
1246{
1247 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001248 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001250 s->Printf("Module %s%s%s%s\n",
1251 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 m_object_name ? "(" : "",
1253 m_object_name ? m_object_name.GetCString() : "",
1254 m_object_name ? ")" : "");
1255
1256 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 if (objfile)
1260 objfile->Dump(s);
1261
1262 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263 if (symbols)
1264 symbols->Dump(s);
1265
1266 s->IndentLess();
1267}
1268
1269
1270TypeList*
1271Module::GetTypeList ()
1272{
1273 SymbolVendor *symbols = GetSymbolVendor ();
1274 if (symbols)
1275 return &symbols->GetTypeList();
1276 return NULL;
1277}
1278
1279const ConstString &
1280Module::GetObjectName() const
1281{
1282 return m_object_name;
1283}
1284
1285ObjectFile *
1286Module::GetObjectFile()
1287{
1288 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001289 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 Timer scoped_timer(__PRETTY_FUNCTION__,
1292 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001293 DataBufferSP data_sp;
1294 lldb::offset_t data_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +00001295 const lldb::offset_t file_size = m_file.GetByteSize();
1296 if (file_size > m_object_offset)
Greg Clayton593577a2011-09-21 03:57:31 +00001297 {
Greg Clayton2540a8a2013-07-12 22:07:46 +00001298 m_did_load_objfile = true;
1299 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1300 &m_file,
1301 m_object_offset,
1302 file_size - m_object_offset,
1303 data_sp,
1304 data_offset);
1305 if (m_objfile_sp)
1306 {
Zachary Turner5e6f4522015-01-22 18:59:05 +00001307 // Once we get the object file, update our module with the object file's
Greg Clayton2540a8a2013-07-12 22:07:46 +00001308 // architecture since it might differ in vendor/os if some parts were
Zachary Turner5e6f4522015-01-22 18:59:05 +00001309 // unknown. But since the matching arch might already be more specific
1310 // than the generic COFF architecture, only merge in those values that
1311 // overwrite unspecified unknown values.
1312 ArchSpec new_arch;
1313 m_objfile_sp->GetArchitecture(new_arch);
1314 m_arch.MergeFrom(new_arch);
Greg Clayton2540a8a2013-07-12 22:07:46 +00001315 }
Todd Fiala0ee56ce2014-09-05 14:48:49 +00001316 else
1317 {
1318 ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str());
1319 }
Greg Clayton593577a2011-09-21 03:57:31 +00001320 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 }
Greg Clayton762f7132011-09-18 18:59:15 +00001322 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323}
1324
Michael Sartaina7499c92013-07-01 19:45:50 +00001325SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001326Module::GetSectionList()
1327{
1328 // Populate m_unified_sections_ap with sections from objfile.
1329 if (m_sections_ap.get() == NULL)
1330 {
1331 ObjectFile *obj_file = GetObjectFile();
1332 if (obj_file)
1333 obj_file->CreateSections(*GetUnifiedSectionList());
1334 }
1335 return m_sections_ap.get();
1336}
1337
Jason Molenda05a09c62014-08-22 02:46:46 +00001338void
1339Module::SectionFileAddressesChanged ()
1340{
1341 ObjectFile *obj_file = GetObjectFile ();
1342 if (obj_file)
1343 obj_file->SectionFileAddressesChanged ();
1344 SymbolVendor* sym_vendor = GetSymbolVendor();
1345 if (sym_vendor)
1346 sym_vendor->SectionFileAddressesChanged ();
1347}
1348
Greg Clayton3046e662013-07-10 01:23:25 +00001349SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001350Module::GetUnifiedSectionList()
1351{
Greg Clayton3046e662013-07-10 01:23:25 +00001352 // Populate m_unified_sections_ap with sections from objfile.
1353 if (m_sections_ap.get() == NULL)
1354 m_sections_ap.reset(new SectionList());
1355 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001356}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357
1358const Symbol *
1359Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1360{
1361 Timer scoped_timer(__PRETTY_FUNCTION__,
1362 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1363 name.AsCString(),
1364 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001365 SymbolVendor* sym_vendor = GetSymbolVendor();
1366 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001368 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001370 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371 }
1372 return NULL;
1373}
1374void
1375Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1376{
1377 // No need to protect this call using m_mutex all other method calls are
1378 // already thread safe.
1379
1380 size_t num_indices = symbol_indexes.size();
1381 if (num_indices > 0)
1382 {
1383 SymbolContext sc;
1384 CalculateSymbolContext (&sc);
1385 for (size_t i = 0; i < num_indices; i++)
1386 {
1387 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1388 if (sc.symbol)
1389 sc_list.Append (sc);
1390 }
1391 }
1392}
1393
1394size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001395Module::FindFunctionSymbols (const ConstString &name,
1396 uint32_t name_type_mask,
1397 SymbolContextList& sc_list)
1398{
1399 Timer scoped_timer(__PRETTY_FUNCTION__,
1400 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1401 name.AsCString(),
1402 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001403 SymbolVendor* sym_vendor = GetSymbolVendor();
1404 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001405 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001406 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001407 if (symtab)
1408 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1409 }
1410 return 0;
1411}
1412
1413size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001414Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001415{
1416 // No need to protect this call using m_mutex all other method calls are
1417 // already thread safe.
1418
1419
1420 Timer scoped_timer(__PRETTY_FUNCTION__,
1421 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1422 name.AsCString(),
1423 symbol_type);
1424 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001425 SymbolVendor* sym_vendor = GetSymbolVendor();
1426 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001428 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 if (symtab)
1430 {
1431 std::vector<uint32_t> symbol_indexes;
1432 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1433 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1434 }
1435 }
1436 return sc_list.GetSize() - initial_size;
1437}
1438
1439size_t
1440Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1441{
1442 // No need to protect this call using m_mutex all other method calls are
1443 // already thread safe.
1444
1445 Timer scoped_timer(__PRETTY_FUNCTION__,
1446 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1447 regex.GetText(),
1448 symbol_type);
1449 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001450 SymbolVendor* sym_vendor = GetSymbolVendor();
1451 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001453 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454 if (symtab)
1455 {
1456 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001457 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1459 }
1460 }
1461 return sc_list.GetSize() - initial_size;
1462}
1463
Greg Claytone01e07b2013-04-18 18:10:51 +00001464void
1465Module::SetSymbolFileFileSpec (const FileSpec &file)
1466{
Michael Sartaina7499c92013-07-01 19:45:50 +00001467 // Remove any sections in the unified section list that come from the current symbol vendor.
1468 if (m_symfile_ap)
1469 {
Greg Clayton3046e662013-07-10 01:23:25 +00001470 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001471 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1472 if (section_list && symbol_file)
1473 {
1474 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001475 // Make sure we have an object file and that the symbol vendor's objfile isn't
1476 // the same as the module's objfile before we remove any sections for it...
1477 if (obj_file && obj_file != m_objfile_sp.get())
Michael Sartaina7499c92013-07-01 19:45:50 +00001478 {
1479 size_t num_sections = section_list->GetNumSections (0);
1480 for (size_t idx = num_sections; idx > 0; --idx)
1481 {
1482 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1483 if (section_sp->GetObjectFile() == obj_file)
1484 {
Greg Clayton3046e662013-07-10 01:23:25 +00001485 section_list->DeleteSection (idx - 1);
Michael Sartaina7499c92013-07-01 19:45:50 +00001486 }
1487 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001488 }
1489 }
1490 }
1491
Greg Claytone01e07b2013-04-18 18:10:51 +00001492 m_symfile_spec = file;
1493 m_symfile_ap.reset();
1494 m_did_load_symbol_vendor = false;
1495}
1496
Jim Ingham5aee1622010-08-09 23:31:02 +00001497bool
1498Module::IsExecutable ()
1499{
1500 if (GetObjectFile() == NULL)
1501 return false;
1502 else
1503 return GetObjectFile()->IsExecutable();
1504}
1505
Jim Inghamb53cb272011-08-03 01:03:17 +00001506bool
1507Module::IsLoadedInTarget (Target *target)
1508{
1509 ObjectFile *obj_file = GetObjectFile();
1510 if (obj_file)
1511 {
Greg Clayton3046e662013-07-10 01:23:25 +00001512 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001513 if (sections != NULL)
1514 {
1515 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001516 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1517 {
1518 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1519 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1520 {
1521 return true;
1522 }
1523 }
1524 }
1525 }
1526 return false;
1527}
Enrico Granata17598482012-11-08 02:22:02 +00001528
1529bool
Enrico Granata97303392013-05-21 00:00:30 +00001530Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001531{
1532 if (!target)
1533 {
1534 error.SetErrorString("invalid destination Target");
1535 return false;
1536 }
1537
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001538 LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001539
Greg Clayton994740f2014-08-18 21:08:44 +00001540 if (should_load == eLoadScriptFromSymFileFalse)
1541 return false;
1542
Greg Clayton91c0e742013-01-11 23:44:27 +00001543 Debugger &debugger = target->GetDebugger();
1544 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1545 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001546 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001547
1548 PlatformSP platform_sp(target->GetPlatform());
1549
1550 if (!platform_sp)
1551 {
1552 error.SetErrorString("invalid Platform");
1553 return false;
1554 }
Enrico Granata17598482012-11-08 02:22:02 +00001555
Greg Claytonb9d88902013-03-23 00:50:58 +00001556 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
Enrico Granatafe7295d2014-08-16 00:32:58 +00001557 *this,
1558 feedback_stream);
Greg Claytonb9d88902013-03-23 00:50:58 +00001559
1560
1561 const uint32_t num_specs = file_specs.GetSize();
1562 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001563 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001564 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1565 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001566 {
1567 for (uint32_t i=0; i<num_specs; ++i)
1568 {
1569 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1570 if (scripting_fspec && scripting_fspec.Exists())
1571 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001572 if (should_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001573 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001574 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001575 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1576 "this debug session:\n\n command script import \"%s\"\n\n"
1577 "To run all discovered debug scripts in this session:\n\n"
1578 " settings set target.load-script-from-symbol-file true\n",
1579 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1580 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001581 return false;
1582 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001583 StreamString scripting_stream;
1584 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001585 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001586 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001587 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1588 can_reload,
1589 init_lldb_globals,
1590 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001591 if (!did_load)
1592 return false;
1593 }
1594 }
1595 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001596 else
1597 {
1598 error.SetErrorString("invalid ScriptInterpreter");
1599 return false;
1600 }
Enrico Granata17598482012-11-08 02:22:02 +00001601 }
1602 }
1603 return true;
1604}
1605
1606bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001607Module::SetArchitecture (const ArchSpec &new_arch)
1608{
Greg Clayton64195a22011-02-23 00:35:02 +00001609 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001610 {
1611 m_arch = new_arch;
1612 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001613 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001614 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001615}
1616
Greg Claytonc9660542012-02-05 02:38:54 +00001617bool
Greg Clayton751caf62014-02-07 22:54:47 +00001618Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001619{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001620 ObjectFile *object_file = GetObjectFile();
1621 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001622 {
Greg Clayton751caf62014-02-07 22:54:47 +00001623 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001624 return true;
1625 }
1626 else
1627 {
1628 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001629 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001630 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001631}
1632
Greg Claytonb9a01b32012-02-26 05:51:37 +00001633
1634bool
1635Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1636{
1637 const UUID &uuid = module_ref.GetUUID();
1638
1639 if (uuid.IsValid())
1640 {
1641 // If the UUID matches, then nothing more needs to match...
1642 if (uuid == GetUUID())
1643 return true;
1644 else
1645 return false;
1646 }
1647
1648 const FileSpec &file_spec = module_ref.GetFileSpec();
1649 if (file_spec)
1650 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001651 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001652 return false;
1653 }
1654
1655 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1656 if (platform_file_spec)
1657 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001658 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001659 return false;
1660 }
1661
1662 const ArchSpec &arch = module_ref.GetArchitecture();
1663 if (arch.IsValid())
1664 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001665 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001666 return false;
1667 }
1668
1669 const ConstString &object_name = module_ref.GetObjectName();
1670 if (object_name)
1671 {
1672 if (object_name != GetObjectName())
1673 return false;
1674 }
1675 return true;
1676}
1677
Greg Claytond804d282012-03-15 21:01:31 +00001678bool
1679Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1680{
1681 Mutex::Locker locker (m_mutex);
1682 return m_source_mappings.FindFile (orig_spec, new_spec);
1683}
1684
Greg Claytonf9be6932012-03-19 22:22:41 +00001685bool
1686Module::RemapSourceFile (const char *path, std::string &new_path) const
1687{
1688 Mutex::Locker locker (m_mutex);
1689 return m_source_mappings.RemapPath(path, new_path);
1690}
1691
Enrico Granata3467d802012-09-04 18:47:54 +00001692uint32_t
1693Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1694{
1695 ObjectFile *obj_file = GetObjectFile();
1696 if (obj_file)
1697 return obj_file->GetVersion (versions, num_versions);
1698
1699 if (versions && num_versions)
1700 {
1701 for (uint32_t i=0; i<num_versions; ++i)
Enrico Granataafcbdb12014-03-25 20:53:33 +00001702 versions[i] = LLDB_INVALID_MODULE_VERSION;
Enrico Granata3467d802012-09-04 18:47:54 +00001703 }
1704 return 0;
1705}
Greg Clayton43fe2172013-04-03 02:00:15 +00001706
1707void
1708Module::PrepareForFunctionNameLookup (const ConstString &name,
1709 uint32_t name_type_mask,
1710 ConstString &lookup_name,
1711 uint32_t &lookup_name_type_mask,
1712 bool &match_name_after_lookup)
1713{
1714 const char *name_cstr = name.GetCString();
1715 lookup_name_type_mask = eFunctionNameTypeNone;
1716 match_name_after_lookup = false;
Jim Inghamfa39bb42014-10-25 00:33:55 +00001717
1718 llvm::StringRef basename;
1719 llvm::StringRef context;
Greg Clayton43fe2172013-04-03 02:00:15 +00001720
1721 if (name_type_mask & eFunctionNameTypeAuto)
1722 {
1723 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1724 lookup_name_type_mask = eFunctionNameTypeFull;
1725 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1726 lookup_name_type_mask = eFunctionNameTypeFull;
1727 else
1728 {
1729 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1730 lookup_name_type_mask |= eFunctionNameTypeSelector;
1731
Greg Clayton6ecb2322013-05-18 00:11:21 +00001732 CPPLanguageRuntime::MethodName cpp_method (name);
Jim Inghamfa39bb42014-10-25 00:33:55 +00001733 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001734 if (basename.empty())
1735 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001736 if (CPPLanguageRuntime::ExtractContextAndIdentifier (name_cstr, context, basename))
Greg Clayton6ecb2322013-05-18 00:11:21 +00001737 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Claytonc3795402014-10-22 21:47:13 +00001738 else
Greg Clayton65b0e762015-01-28 01:08:39 +00001739 lookup_name_type_mask |= eFunctionNameTypeFull;
Greg Clayton6ecb2322013-05-18 00:11:21 +00001740 }
1741 else
1742 {
Greg Clayton43fe2172013-04-03 02:00:15 +00001743 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001744 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001745 }
1746 }
1747 else
1748 {
1749 lookup_name_type_mask = name_type_mask;
1750 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1751 {
1752 // If they've asked for a CPP method or function name and it can't be that, we don't
1753 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001754 CPPLanguageRuntime::MethodName cpp_method (name);
1755 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001756 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001757 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001758
1759 if (!cpp_method.GetQualifiers().empty())
1760 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001761 // There is a "const" or other qualifier following the end of the function parens,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001762 // this can't be a eFunctionNameTypeBase
1763 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1764 if (lookup_name_type_mask == eFunctionNameTypeNone)
1765 return;
1766 }
1767 }
1768 else
1769 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001770 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can.
1771 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later.
1772 CPPLanguageRuntime::ExtractContextAndIdentifier (name_cstr, context, basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001773 }
1774 }
1775
1776 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1777 {
1778 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1779 {
1780 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1781 if (lookup_name_type_mask == eFunctionNameTypeNone)
1782 return;
1783 }
1784 }
1785 }
1786
Jim Inghamfa39bb42014-10-25 00:33:55 +00001787 if (!basename.empty())
Greg Clayton43fe2172013-04-03 02:00:15 +00001788 {
1789 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1790 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1791 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1792 // to true
Jim Inghamfa39bb42014-10-25 00:33:55 +00001793 lookup_name.SetString(basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001794 match_name_after_lookup = true;
1795 }
1796 else
1797 {
1798 // The name is already correct, just use the exact name as supplied, and we won't need
1799 // to check if any matches contain "name"
1800 lookup_name = name;
1801 match_name_after_lookup = false;
1802 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001803}
Greg Clayton23f8c952014-03-24 23:10:19 +00001804
1805ModuleSP
1806Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
1807{
1808 if (delegate_sp)
1809 {
1810 // Must create a module and place it into a shared pointer before
1811 // we can create an object file since it has a std::weak_ptr back
1812 // to the module, so we need to control the creation carefully in
1813 // this static function
1814 ModuleSP module_sp(new Module());
1815 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
1816 if (module_sp->m_objfile_sp)
1817 {
1818 // Once we get the object file, update our module with the object file's
1819 // architecture since it might differ in vendor/os if some parts were
1820 // unknown.
1821 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
1822 }
1823 return module_sp;
1824 }
1825 return ModuleSP();
1826}
1827