blob: 90c9aad7e208dad0acee25c34f28a730ef5dffe5 [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
Richard Mittonf86248d2013-09-12 02:20:34 +000010#include "lldb/Core/AddressResolverFileLine.h"
Enrico Granata17598482012-11-08 02:22:02 +000011#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
Greg Claytonc9660542012-02-05 02:38:54 +000013#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/ModuleList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000017#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Section.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000020#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000022#include "lldb/Host/Host.h"
Enrico Granata17598482012-11-08 02:22:02 +000023#include "lldb/Host/Symbols.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/ScriptInterpreter.h"
Zachary Turner88c6b622015-03-03 18:34:26 +000026#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Symbol/SymbolContext.h"
30#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000031#include "lldb/Target/CPPLanguageRuntime.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000032#include "lldb/Target/Language.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000033#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytonc9660542012-02-05 02:38:54 +000034#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000035#include "lldb/Target/SectionLoadList.h"
Greg Claytonc9660542012-02-05 02:38:54 +000036#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000037#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
Greg Clayton23f8c952014-03-24 23:10:19 +000039#include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
40
Zachary Turnera893d302015-03-06 20:45:43 +000041#include "llvm/Support/raw_os_ostream.h"
42#include "llvm/Support/Signals.h"
43
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton65a03992011-08-09 00:01:09 +000047// Shared pointers to modules track module lifetimes in
48// targets and in the global module, but this collection
49// will track all module objects that are still alive
50typedef std::vector<Module *> ModuleCollection;
51
52static ModuleCollection &
53GetModuleCollection()
54{
Jim Ingham549f7372011-10-31 23:47:10 +000055 // This module collection needs to live past any module, so we could either make it a
56 // shared pointer in each module or just leak is. Since it is only an empty vector by
57 // the time all the modules have gone away, we just leak it for now. If we decide this
58 // is a big problem we can introduce a Finalize method that will tear everything down in
59 // a predictable order.
60
61 static ModuleCollection *g_module_collection = NULL;
62 if (g_module_collection == NULL)
63 g_module_collection = new ModuleCollection();
64
65 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000066}
67
Greg Claytonb26e6be2012-01-27 18:08:35 +000068Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000069Module::GetAllocationModuleCollectionMutex()
70{
Greg Claytonb26e6be2012-01-27 18:08:35 +000071 // NOTE: The mutex below must be leaked since the global module list in
72 // the ModuleList class will get torn at some point, and we can't know
73 // if it will tear itself down before the "g_module_collection_mutex" below
74 // will. So we leak a Mutex object below to safeguard against that
75
76 static Mutex *g_module_collection_mutex = NULL;
77 if (g_module_collection_mutex == NULL)
78 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
79 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000080}
81
82size_t
83Module::GetNumberAllocatedModules ()
84{
85 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
86 return GetModuleCollection().size();
87}
88
89Module *
90Module::GetAllocatedModuleAtIndex (size_t idx)
91{
92 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
93 ModuleCollection &modules = GetModuleCollection();
94 if (idx < modules.size())
95 return modules[idx];
96 return NULL;
97}
Greg Clayton29ad7b92012-01-27 18:45:39 +000098#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000099
Greg Clayton29ad7b92012-01-27 18:45:39 +0000100// These functions help us to determine if modules are still loaded, yet don't require that
101// you have a command interpreter and can easily be called from an external debugger.
102namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +0000103
Greg Clayton29ad7b92012-01-27 18:45:39 +0000104 void
105 ClearModuleInfo (void)
106 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000107 const bool mandatory = true;
108 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000109 }
110
111 void
112 DumpModuleInfo (void)
113 {
114 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
115 ModuleCollection &modules = GetModuleCollection();
116 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000117 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000118 for (size_t i=0; i<count; ++i)
119 {
120
121 StreamString strm;
122 Module *module = modules[i];
123 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
124 module->GetDescription(&strm, eDescriptionLevelFull);
125 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
126 module,
127 in_shared_module_list,
128 (uint32_t)module->use_count(),
129 strm.GetString().c_str());
130 }
131 }
132}
133
134#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000135
Greg Claytonb9a01b32012-02-26 05:51:37 +0000136Module::Module (const ModuleSpec &module_spec) :
137 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton34f11592014-03-04 21:20:23 +0000138 m_mod_time (),
139 m_arch (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000140 m_uuid (),
Greg Clayton34f11592014-03-04 21:20:23 +0000141 m_file (),
142 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000143 m_remote_install_file(),
Greg Clayton34f11592014-03-04 21:20:23 +0000144 m_symfile_spec (),
145 m_object_name (),
146 m_object_offset (),
147 m_object_mod_time (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000148 m_objfile_sp (),
149 m_symfile_ap (),
Zachary Turner88c6b622015-03-03 18:34:26 +0000150 m_ast (new ClangASTContext),
Greg Claytond804d282012-03-15 21:01:31 +0000151 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000152 m_sections_ap(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000153 m_did_load_objfile (false),
154 m_did_load_symbol_vendor (false),
155 m_did_parse_uuid (false),
156 m_did_init_ast (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000157 m_file_has_changed (false),
158 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000159{
160 // Scope for locker below...
161 {
162 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
163 GetModuleCollection().push_back(this);
164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000165
Greg Clayton5160ce52013-03-27 23:08:40 +0000166 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000167 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000168 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000169 static_cast<void*>(this),
Greg Clayton34f11592014-03-04 21:20:23 +0000170 module_spec.GetArchitecture().GetArchitectureName(),
171 module_spec.GetFileSpec().GetPath().c_str(),
172 module_spec.GetObjectName().IsEmpty() ? "" : "(",
173 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
174 module_spec.GetObjectName().IsEmpty() ? "" : ")");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000175
Greg Clayton34f11592014-03-04 21:20:23 +0000176 // First extract all module specifications from the file using the local
177 // file path. If there are no specifications, then don't fill anything in
178 ModuleSpecList modules_specs;
179 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
180 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000181
Greg Clayton34f11592014-03-04 21:20:23 +0000182 // Now make sure that one of the module specifications matches what we just
183 // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
184 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
185 // UUID YYY and we don't want those to match. If they don't match, just don't
186 // fill any ivars in so we don't accidentally grab the wrong file later since
187 // they don't match...
188 ModuleSpec matching_module_spec;
189 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
190 return;
Greg Clayton7ab7f892014-05-29 21:33:45 +0000191
192 if (module_spec.GetFileSpec())
193 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
194 else if (matching_module_spec.GetFileSpec())
195 m_mod_time = matching_module_spec.GetFileSpec().GetModificationTime();
196
197 // Copy the architecture from the actual spec if we got one back, else use the one that was specified
198 if (matching_module_spec.GetArchitecture().IsValid())
Greg Clayton34f11592014-03-04 21:20:23 +0000199 m_arch = matching_module_spec.GetArchitecture();
Greg Clayton7ab7f892014-05-29 21:33:45 +0000200 else if (module_spec.GetArchitecture().IsValid())
201 m_arch = module_spec.GetArchitecture();
202
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000203 // Copy the file spec over and use the specified one (if there was one) so we
Greg Clayton7ab7f892014-05-29 21:33:45 +0000204 // don't use a path that might have gotten resolved a path in 'matching_module_spec'
205 if (module_spec.GetFileSpec())
206 m_file = module_spec.GetFileSpec();
207 else if (matching_module_spec.GetFileSpec())
208 m_file = matching_module_spec.GetFileSpec();
209
210 // Copy the platform file spec over
211 if (module_spec.GetPlatformFileSpec())
212 m_platform_file = module_spec.GetPlatformFileSpec();
213 else if (matching_module_spec.GetPlatformFileSpec())
214 m_platform_file = matching_module_spec.GetPlatformFileSpec();
215
216 // Copy the symbol file spec over
217 if (module_spec.GetSymbolFileSpec())
218 m_symfile_spec = module_spec.GetSymbolFileSpec();
219 else if (matching_module_spec.GetSymbolFileSpec())
220 m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
221
222 // Copy the object name over
223 if (matching_module_spec.GetObjectName())
224 m_object_name = matching_module_spec.GetObjectName();
225 else
226 m_object_name = module_spec.GetObjectName();
227
228 // Always trust the object offset (file offset) and object modification
229 // time (for mod time in a BSD static archive) of from the matching
230 // module specification
Greg Clayton36d7c892014-05-29 17:52:46 +0000231 m_object_offset = matching_module_spec.GetObjectOffset();
232 m_object_mod_time = matching_module_spec.GetObjectModificationTime();
Greg Clayton34f11592014-03-04 21:20:23 +0000233
Greg Claytonb9a01b32012-02-26 05:51:37 +0000234}
235
Greg Claytone72dfb32012-02-24 01:59:29 +0000236Module::Module(const FileSpec& file_spec,
237 const ArchSpec& arch,
238 const ConstString *object_name,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000239 lldb::offset_t object_offset,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000240 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 m_mutex (Mutex::eMutexTypeRecursive),
242 m_mod_time (file_spec.GetModificationTime()),
243 m_arch (arch),
244 m_uuid (),
245 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000246 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000247 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000248 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000250 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000251 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000252 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000253 m_symfile_ap (),
Zachary Turner88c6b622015-03-03 18:34:26 +0000254 m_ast (new ClangASTContext),
Greg Claytond804d282012-03-15 21:01:31 +0000255 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000256 m_sections_ap(),
Greg Claytone83e7312010-09-07 23:40:05 +0000257 m_did_load_objfile (false),
258 m_did_load_symbol_vendor (false),
259 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000260 m_did_init_ast (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 (),
Zachary Turner88c6b622015-03-03 18:34:26 +0000300 m_ast (new ClangASTContext),
Greg Clayton23f8c952014-03-24 23:10:19 +0000301 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),
Greg Clayton23f8c952014-03-24 23:10:19 +0000307 m_file_has_changed (false),
308 m_first_file_changed_log (false)
309{
310 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
311 GetModuleCollection().push_back(this);
312}
313
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314Module::~Module()
315{
Greg Clayton217b28b2013-05-22 20:13:22 +0000316 // Lock our module down while we tear everything down to make sure
317 // we don't get any access to the module while it is being destroyed
318 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000319 // Scope for locker below...
320 {
321 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
322 ModuleCollection &modules = GetModuleCollection();
323 ModuleCollection::iterator end = modules.end();
324 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000325 assert (pos != end);
326 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000327 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000328 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000330 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000331 static_cast<void*>(this),
Greg Clayton64195a22011-02-23 00:35:02 +0000332 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000333 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 m_object_name.IsEmpty() ? "" : "(",
335 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
336 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000337 // Release any auto pointers before we start tearing down our member
338 // variables since the object file and symbol files might need to make
339 // function calls back into this module object. The ordering is important
340 // here because symbol files can require the module object file. So we tear
341 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000342 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000343 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000344 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345}
346
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000347ObjectFile *
Andrew MacPherson17220c12014-03-05 10:12:43 +0000348Module::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 +0000349{
350 if (m_objfile_sp)
351 {
352 error.SetErrorString ("object file already exists");
353 }
354 else
355 {
356 Mutex::Locker locker (m_mutex);
357 if (process_sp)
358 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000359 m_did_load_objfile = true;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000360 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000361 Error readmem_error;
362 const size_t bytes_read = process_sp->ReadMemory (header_addr,
363 data_ap->GetBytes(),
364 data_ap->GetByteSize(),
365 readmem_error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000366 if (bytes_read == size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000367 {
368 DataBufferSP data_sp(data_ap.release());
369 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
370 if (m_objfile_sp)
371 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000372 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000373 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000374 m_object_name.SetCString (s.GetData());
375
376 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000377 // architecture since it might differ in vendor/os if some parts were
378 // unknown.
379 m_objfile_sp->GetArchitecture (m_arch);
380 }
381 else
382 {
383 error.SetErrorString ("unable to find suitable object file plug-in");
384 }
385 }
386 else
387 {
388 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
389 }
390 }
391 else
392 {
393 error.SetErrorString ("invalid process");
394 }
395 }
396 return m_objfile_sp.get();
397}
398
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399
Greg Clayton60830262011-02-04 18:53:10 +0000400const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401Module::GetUUID()
402{
Greg Clayton88c05f52015-07-24 23:38:01 +0000403 if (m_did_parse_uuid.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000405 Mutex::Locker locker (m_mutex);
406 if (m_did_parse_uuid.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000408 ObjectFile * obj_file = GetObjectFile ();
409
410 if (obj_file != NULL)
411 {
412 obj_file->GetUUID(&m_uuid);
413 m_did_parse_uuid = true;
414 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 }
416 }
417 return m_uuid;
418}
419
Greg Clayton8b4edba2015-08-14 20:02:05 +0000420TypeSystem *
421Module::GetTypeSystemForLanguage (LanguageType language)
422{
423 if (language != eLanguageTypeSwift)
424 {
425 // For now assume all languages except swift use the ClangASTContext for types
426 return &GetClangASTContext();
427 }
428 return nullptr;
429}
430
Greg Clayton6beaaa62011-01-17 03:46:26 +0000431ClangASTContext &
432Module::GetClangASTContext ()
433{
Greg Clayton88c05f52015-07-24 23:38:01 +0000434 if (m_did_init_ast.load() == false)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000435 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000436 Mutex::Locker locker (m_mutex);
437 if (m_did_init_ast.load() == false)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000438 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000439 ObjectFile * objfile = GetObjectFile();
440 ArchSpec object_arch;
441 if (objfile && objfile->GetArchitecture(object_arch))
Jason Molenda981d4df2012-10-16 20:45:49 +0000442 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000443 m_did_init_ast = true;
444
445 // LLVM wants this to be set to iOS or MacOSX; if we're working on
446 // a bare-boards type image, change the triple for llvm's benefit.
447 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
448 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
Jason Molenda981d4df2012-10-16 20:45:49 +0000449 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000450 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
451 object_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
452 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
453 {
454 object_arch.GetTriple().setOS(llvm::Triple::IOS);
455 }
456 else
457 {
458 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
459 }
Jason Molenda981d4df2012-10-16 20:45:49 +0000460 }
Greg Clayton88c05f52015-07-24 23:38:01 +0000461 m_ast->SetArchitecture (object_arch);
Jason Molenda981d4df2012-10-16 20:45:49 +0000462 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000463 }
464 }
Zachary Turner88c6b622015-03-03 18:34:26 +0000465 return *m_ast;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000466}
467
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468void
469Module::ParseAllDebugSymbols()
470{
471 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000472 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 if (num_comp_units == 0)
474 return;
475
Greg Claytona2eee182011-09-17 07:23:18 +0000476 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000477 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 SymbolVendor *symbols = GetSymbolVendor ();
479
Greg Claytonc7bece562013-01-25 18:06:21 +0000480 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 {
482 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
483 if (sc.comp_unit)
484 {
485 sc.function = NULL;
486 symbols->ParseVariablesForContext(sc);
487
488 symbols->ParseCompileUnitFunctions(sc);
489
Greg Claytonc7bece562013-01-25 18:06:21 +0000490 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 +0000491 {
492 symbols->ParseFunctionBlocks(sc);
493
494 // Parse the variables for this function and all its blocks
495 symbols->ParseVariablesForContext(sc);
496 }
497
498
499 // Parse all types for this compile unit
500 sc.function = NULL;
501 symbols->ParseTypes(sc);
502 }
503 }
504}
505
506void
507Module::CalculateSymbolContext(SymbolContext* sc)
508{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000509 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
Greg Claytone72dfb32012-02-24 01:59:29 +0000512ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000513Module::CalculateSymbolContextModule ()
514{
Greg Claytone72dfb32012-02-24 01:59:29 +0000515 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000516}
517
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518void
519Module::DumpSymbolContext(Stream *s)
520{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000521 s->Printf(", Module{%p}", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522}
523
Greg Claytonc7bece562013-01-25 18:06:21 +0000524size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525Module::GetNumCompileUnits()
526{
527 Mutex::Locker locker (m_mutex);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528 Timer scoped_timer(__PRETTY_FUNCTION__,
529 "Module::GetNumCompileUnits (module = %p)",
530 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 SymbolVendor *symbols = GetSymbolVendor ();
532 if (symbols)
533 return symbols->GetNumCompileUnits();
534 return 0;
535}
536
537CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000538Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539{
540 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000541 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 CompUnitSP cu_sp;
543
544 if (index < num_comp_units)
545 {
546 SymbolVendor *symbols = GetSymbolVendor ();
547 if (symbols)
548 cu_sp = symbols->GetCompileUnitAtIndex(index);
549 }
550 return cu_sp;
551}
552
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553bool
554Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
555{
556 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000557 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000558 SectionList *section_list = GetSectionList();
559 if (section_list)
560 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 return false;
562}
563
564uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000565Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
566 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567{
568 Mutex::Locker locker (m_mutex);
569 uint32_t resolved_flags = 0;
570
Greg Clayton72310352013-02-23 04:12:47 +0000571 // Clear the result symbol context in case we don't find anything, but don't clear the target
572 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573
574 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000575 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576
577 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000578 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 {
580 // If the section offset based address resolved itself, then this
581 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000582 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 resolved_flags |= eSymbolContextModule;
584
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000585 SymbolVendor* sym_vendor = GetSymbolVendor();
586 if (!sym_vendor)
587 return resolved_flags;
588
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589 // Resolve the compile unit, function, block, line table or line
590 // entry if requested.
591 if (resolve_scope & eSymbolContextCompUnit ||
592 resolve_scope & eSymbolContextFunction ||
593 resolve_scope & eSymbolContextBlock ||
594 resolve_scope & eSymbolContextLineEntry )
595 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000596 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 }
598
Jim Ingham680e1772010-08-31 23:51:36 +0000599 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
600 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000602 Symtab *symtab = sym_vendor->GetSymtab();
603 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000605 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000606 if (!sc.symbol &&
607 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
608 {
609 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
610 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
611 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
612 }
613
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000614 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000615 {
616 if (sc.symbol->IsSynthetic())
617 {
618 // We have a synthetic symbol so lets check if the object file
619 // from the symbol file in the symbol vendor is different than
620 // the object file for the module, and if so search its symbol
621 // table to see if we can come up with a better symbol. For example
622 // dSYM files on MacOSX have an unstripped symbol table inside of
623 // them.
624 ObjectFile *symtab_objfile = symtab->GetObjectFile();
625 if (symtab_objfile && symtab_objfile->IsStripped())
626 {
627 SymbolFile *symfile = sym_vendor->GetSymbolFile();
628 if (symfile)
629 {
630 ObjectFile *symfile_objfile = symfile->GetObjectFile();
631 if (symfile_objfile != symtab_objfile)
632 {
633 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
634 if (symfile_symtab)
635 {
636 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
637 if (symbol && !symbol->IsSynthetic())
638 {
639 sc.symbol = symbol;
640 }
641 }
642 }
643 }
644 }
645 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000646 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000647 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000648 }
649 }
650
651 // For function symbols, so_addr may be off by one. This is a convention consistent
652 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
653 // symbol lookup for disassembly and unwind.
654 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000655 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000656 {
657 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000658 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000659
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000660 bool do_resolve_tail_call_address = false; // prevent recursion
661 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
662 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000663 if (flags & eSymbolContextSymbol)
664 {
665 AddressRange addr_range;
666 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000668 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000670 // If the requested address is one past the address range of a function (i.e. a tail call),
671 // or the decremented address is the start of a function (i.e. some forms of trampoline),
672 // indicate that the symbol has been resolved.
673 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
674 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
675 {
676 resolved_flags |= flags;
677 }
678 }
679 else
680 {
681 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 }
683 }
684 }
685 }
686 }
687 return resolved_flags;
688}
689
690uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000691Module::ResolveSymbolContextForFilePath
692(
693 const char *file_path,
694 uint32_t line,
695 bool check_inlines,
696 uint32_t resolve_scope,
697 SymbolContextList& sc_list
698)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699{
Greg Clayton274060b2010-10-20 20:54:39 +0000700 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
702}
703
704uint32_t
705Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
706{
707 Mutex::Locker locker (m_mutex);
708 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000709 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
710 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 line,
712 check_inlines ? "yes" : "no",
713 resolve_scope);
714
715 const uint32_t initial_count = sc_list.GetSize();
716
717 SymbolVendor *symbols = GetSymbolVendor ();
718 if (symbols)
719 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
720
721 return sc_list.GetSize() - initial_count;
722}
723
724
Greg Claytonc7bece562013-01-25 18:06:21 +0000725size_t
726Module::FindGlobalVariables (const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000727 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000728 bool append,
729 size_t max_matches,
730 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731{
732 SymbolVendor *symbols = GetSymbolVendor ();
733 if (symbols)
Greg Clayton99558cc42015-08-24 23:46:31 +0000734 return symbols->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 return 0;
736}
Greg Claytonc7bece562013-01-25 18:06:21 +0000737
738size_t
739Module::FindGlobalVariables (const RegularExpression& regex,
740 bool append,
741 size_t max_matches,
742 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743{
744 SymbolVendor *symbols = GetSymbolVendor ();
745 if (symbols)
746 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
747 return 0;
748}
749
Greg Claytonc7bece562013-01-25 18:06:21 +0000750size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000751Module::FindCompileUnits (const FileSpec &path,
752 bool append,
753 SymbolContextList &sc_list)
754{
755 if (!append)
756 sc_list.Clear();
757
Greg Claytonc7bece562013-01-25 18:06:21 +0000758 const size_t start_size = sc_list.GetSize();
759 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000760 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000761 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000762 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000763 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000764 {
765 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000766 if (sc.comp_unit)
767 {
768 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
769 sc_list.Append(sc);
770 }
Greg Clayton644247c2011-07-07 01:59:51 +0000771 }
772 return sc_list.GetSize() - start_size;
773}
774
Greg Claytonc7bece562013-01-25 18:06:21 +0000775size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000776Module::FindFunctions (const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000777 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000778 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000779 bool include_symbols,
780 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000781 bool append,
782 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783{
Greg Clayton931180e2011-01-27 06:44:37 +0000784 if (!append)
785 sc_list.Clear();
786
Greg Clayton43fe2172013-04-03 02:00:15 +0000787 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000788
789 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000791
792 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000793 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000794 ConstString lookup_name;
795 uint32_t lookup_name_type_mask = 0;
796 bool match_name_after_lookup = false;
797 Module::PrepareForFunctionNameLookup (name,
798 name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000799 eLanguageTypeUnknown, // TODO: add support
Greg Clayton43fe2172013-04-03 02:00:15 +0000800 lookup_name,
801 lookup_name_type_mask,
802 match_name_after_lookup);
803
804 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000805 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000806 symbols->FindFunctions(lookup_name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000807 parent_decl_ctx,
Greg Clayton43fe2172013-04-03 02:00:15 +0000808 lookup_name_type_mask,
809 include_inlines,
810 append,
811 sc_list);
812
Michael Sartaina7499c92013-07-01 19:45:50 +0000813 // Now check our symbol table for symbols that are code symbols if requested
814 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000815 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000816 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000817 if (symtab)
818 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
819 }
820 }
821
822 if (match_name_after_lookup)
823 {
824 SymbolContext sc;
825 size_t i = old_size;
826 while (i<sc_list.GetSize())
827 {
828 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000829 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000830 const char *func_name = sc.GetFunctionName().GetCString();
831 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000832 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000833 // Remove the current context
834 sc_list.RemoveContextAtIndex(i);
835 // Don't increment i and continue in the loop
836 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000837 }
838 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000839 ++i;
840 }
841 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000842 }
843 else
844 {
845 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000846 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000847 symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list);
Greg Clayton43fe2172013-04-03 02:00:15 +0000848
Michael Sartaina7499c92013-07-01 19:45:50 +0000849 // Now check our symbol table for symbols that are code symbols if requested
850 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000851 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000852 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000853 if (symtab)
854 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000855 }
856 }
857 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000858
859 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860}
861
Greg Claytonc7bece562013-01-25 18:06:21 +0000862size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000863Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000864 bool include_symbols,
865 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000866 bool append,
867 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868{
Greg Clayton931180e2011-01-27 06:44:37 +0000869 if (!append)
870 sc_list.Clear();
871
Greg Claytonc7bece562013-01-25 18:06:21 +0000872 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000873
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 SymbolVendor *symbols = GetSymbolVendor ();
875 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000876 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000877 symbols->FindFunctions(regex, include_inlines, append, sc_list);
878
879 // Now check our symbol table for symbols that are code symbols if requested
880 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000881 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000882 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000883 if (symtab)
884 {
885 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000886 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000887 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000888 if (num_matches)
889 {
890 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000891 const size_t end_functions_added_index = sc_list.GetSize();
892 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
893 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000894 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000895 // No functions were added, just symbols, so we can just append them
896 for (size_t i=0; i<num_matches; ++i)
897 {
898 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
899 SymbolType sym_type = sc.symbol->GetType();
900 if (sc.symbol && (sym_type == eSymbolTypeCode ||
901 sym_type == eSymbolTypeResolver))
902 sc_list.Append(sc);
903 }
904 }
905 else
906 {
907 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
908 FileAddrToIndexMap file_addr_to_index;
909 for (size_t i=start_size; i<end_functions_added_index; ++i)
910 {
911 const SymbolContext &sc = sc_list[i];
912 if (sc.block)
913 continue;
914 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
915 }
916
917 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
918 // Functions were added so we need to merge symbols into any
919 // existing function symbol contexts
920 for (size_t i=start_size; i<num_matches; ++i)
921 {
922 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
923 SymbolType sym_type = sc.symbol->GetType();
Greg Clayton358cf1e2015-06-25 21:46:34 +0000924 if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver))
Greg Claytond8cf1a12013-06-12 00:46:38 +0000925 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000926 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress());
Greg Claytond8cf1a12013-06-12 00:46:38 +0000927 if (pos == end)
928 sc_list.Append(sc);
929 else
930 sc_list[pos->second].symbol = sc.symbol;
931 }
932 }
Greg Clayton931180e2011-01-27 06:44:37 +0000933 }
934 }
935 }
936 }
937 }
938 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939}
940
Richard Mittonf86248d2013-09-12 02:20:34 +0000941void
942Module::FindAddressesForLine (const lldb::TargetSP target_sp,
943 const FileSpec &file, uint32_t line,
944 Function *function,
945 std::vector<Address> &output_local, std::vector<Address> &output_extern)
946{
947 SearchFilterByModule filter(target_sp, m_file);
948 AddressResolverFileLine resolver(file, line, true);
949 resolver.ResolveAddress (filter);
950
951 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
952 {
953 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
954 Function *f = addr.CalculateSymbolContextFunction();
955 if (f && f == function)
956 output_local.push_back (addr);
957 else
958 output_extern.push_back (addr);
959 }
960}
961
Greg Claytonc7bece562013-01-25 18:06:21 +0000962size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000963Module::FindTypes_Impl (const SymbolContext& sc,
964 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000965 const CompilerDeclContext *parent_decl_ctx,
Greg Clayton84db9102012-03-26 23:03:23 +0000966 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000967 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000968 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000969{
970 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
971 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
972 {
973 SymbolVendor *symbols = GetSymbolVendor ();
974 if (symbols)
Greg Clayton99558cc42015-08-24 23:46:31 +0000975 return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000976 }
977 return 0;
978}
979
Greg Claytonc7bece562013-01-25 18:06:21 +0000980size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000981Module::FindTypesInNamespace (const SymbolContext& sc,
982 const ConstString &type_name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000983 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000984 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000985 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000986{
Greg Clayton84db9102012-03-26 23:03:23 +0000987 const bool append = true;
Greg Clayton99558cc42015-08-24 23:46:31 +0000988 return FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000989}
990
Greg Claytonb43165b2012-12-05 21:24:42 +0000991lldb::TypeSP
992Module::FindFirstType (const SymbolContext& sc,
993 const ConstString &name,
994 bool exact_match)
995{
996 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000997 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000998 if (num_matches)
999 return type_list.GetTypeAtIndex(0);
1000 return TypeSP();
1001}
1002
1003
Greg Claytonc7bece562013-01-25 18:06:21 +00001004size_t
Greg Clayton84db9102012-03-26 23:03:23 +00001005Module::FindTypes (const SymbolContext& sc,
1006 const ConstString &name,
1007 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +00001008 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +00001009 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001010{
Greg Claytonc7bece562013-01-25 18:06:21 +00001011 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001012 const char *type_name_cstr = name.GetCString();
1013 std::string type_scope;
1014 std::string type_basename;
1015 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +00001016 TypeClass type_class = eTypeClassAny;
1017 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +00001018 {
Greg Clayton84db9102012-03-26 23:03:23 +00001019 // Check if "name" starts with "::" which means the qualified type starts
1020 // from the root namespace and implies and exact match. The typenames we
1021 // get back from clang do not start with "::" so we need to strip this off
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001022 // in order to get the qualified names to match
Greg Clayton84db9102012-03-26 23:03:23 +00001023
1024 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
1025 {
1026 type_scope.erase(0,2);
1027 exact_match = true;
1028 }
1029 ConstString type_basename_const_str (type_basename.c_str());
1030 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
1031 {
Greg Clayton7bc31332012-10-22 16:19:56 +00001032 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +00001033 num_matches = types.GetSize();
1034 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001035 }
1036 else
Greg Clayton84db9102012-03-26 23:03:23 +00001037 {
1038 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +00001039 if (type_class != eTypeClassAny)
1040 {
1041 // The "type_name_cstr" will have been modified if we have a valid type class
1042 // prefix (like "struct", "class", "union", "typedef" etc).
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001043 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
Greg Clayton7bc31332012-10-22 16:19:56 +00001044 types.RemoveMismatchedTypes (type_class);
1045 num_matches = types.GetSize();
1046 }
1047 else
1048 {
1049 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
1050 }
Greg Clayton84db9102012-03-26 23:03:23 +00001051 }
1052
1053 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001054
1055}
1056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +00001058Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059{
Greg Clayton88c05f52015-07-24 23:38:01 +00001060 if (m_did_load_symbol_vendor.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001062 Mutex::Locker locker (m_mutex);
1063 if (m_did_load_symbol_vendor.load() == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001065 ObjectFile *obj_file = GetObjectFile ();
1066 if (obj_file != NULL)
1067 {
1068 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1069 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1070 m_did_load_symbol_vendor = true;
1071 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 }
1073 }
1074 return m_symfile_ap.get();
1075}
1076
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077void
1078Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
1079{
1080 // Container objects whose paths do not specify a file directly can call
1081 // this function to correct the file and object names.
1082 m_file = file;
1083 m_mod_time = file.GetModificationTime();
1084 m_object_name = object_name;
1085}
1086
1087const ArchSpec&
1088Module::GetArchitecture () const
1089{
1090 return m_arch;
1091}
1092
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001093std::string
1094Module::GetSpecificationDescription () const
1095{
1096 std::string spec(GetFileSpec().GetPath());
1097 if (m_object_name)
1098 {
1099 spec += '(';
1100 spec += m_object_name.GetCString();
1101 spec += ')';
1102 }
1103 return spec;
1104}
1105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001107Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001108{
1109 Mutex::Locker locker (m_mutex);
1110
Greg Claytonc982b3d2011-11-28 01:45:00 +00001111 if (level >= eDescriptionLevelFull)
1112 {
1113 if (m_arch.IsValid())
1114 s->Printf("(%s) ", m_arch.GetArchitectureName());
1115 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001116
Greg Claytonc982b3d2011-11-28 01:45:00 +00001117 if (level == eDescriptionLevelBrief)
1118 {
1119 const char *filename = m_file.GetFilename().GetCString();
1120 if (filename)
1121 s->PutCString (filename);
1122 }
1123 else
1124 {
1125 char path[PATH_MAX];
1126 if (m_file.GetPath(path, sizeof(path)))
1127 s->PutCString(path);
1128 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001129
1130 const char *object_name = m_object_name.GetCString();
1131 if (object_name)
1132 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001133}
1134
1135void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001136Module::ReportError (const char *format, ...)
1137{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001138 if (format && format[0])
1139 {
1140 StreamString strm;
1141 strm.PutCString("error: ");
1142 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001143 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001144 va_list args;
1145 va_start (args, format);
1146 strm.PrintfVarArg(format, args);
1147 va_end (args);
1148
1149 const int format_len = strlen(format);
1150 if (format_len > 0)
1151 {
1152 const char last_char = format[format_len-1];
1153 if (last_char != '\n' || last_char != '\r')
1154 strm.EOL();
1155 }
1156 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1157
1158 }
1159}
1160
Greg Clayton1d609092012-07-12 22:51:12 +00001161bool
1162Module::FileHasChanged () const
1163{
1164 if (m_file_has_changed == false)
1165 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1166 return m_file_has_changed;
1167}
1168
Greg Claytone38a5ed2012-01-05 03:57:59 +00001169void
1170Module::ReportErrorIfModifyDetected (const char *format, ...)
1171{
Greg Clayton1d609092012-07-12 22:51:12 +00001172 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001173 {
Greg Clayton1d609092012-07-12 22:51:12 +00001174 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001175 {
Greg Clayton1d609092012-07-12 22:51:12 +00001176 m_first_file_changed_log = true;
1177 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001178 {
Greg Clayton1d609092012-07-12 22:51:12 +00001179 StreamString strm;
1180 strm.PutCString("error: the object file ");
1181 GetDescription(&strm, lldb::eDescriptionLevelFull);
1182 strm.PutCString (" has been modified\n");
1183
1184 va_list args;
1185 va_start (args, format);
1186 strm.PrintfVarArg(format, args);
1187 va_end (args);
1188
1189 const int format_len = strlen(format);
1190 if (format_len > 0)
1191 {
1192 const char last_char = format[format_len-1];
1193 if (last_char != '\n' || last_char != '\r')
1194 strm.EOL();
1195 }
1196 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1197 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001198 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001199 }
1200 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001201}
1202
1203void
1204Module::ReportWarning (const char *format, ...)
1205{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001206 if (format && format[0])
1207 {
1208 StreamString strm;
1209 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001210 GetDescription(&strm, lldb::eDescriptionLevelFull);
1211 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001212
1213 va_list args;
1214 va_start (args, format);
1215 strm.PrintfVarArg(format, args);
1216 va_end (args);
1217
1218 const int format_len = strlen(format);
1219 if (format_len > 0)
1220 {
1221 const char last_char = format[format_len-1];
1222 if (last_char != '\n' || last_char != '\r')
1223 strm.EOL();
1224 }
1225 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1226 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001227}
1228
1229void
1230Module::LogMessage (Log *log, const char *format, ...)
1231{
1232 if (log)
1233 {
1234 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001235 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001236 log_message.PutCString (": ");
1237 va_list args;
1238 va_start (args, format);
1239 log_message.PrintfVarArg (format, args);
1240 va_end (args);
1241 log->PutCString(log_message.GetString().c_str());
1242 }
1243}
1244
Greg Claytond61c0fc2012-04-23 22:55:20 +00001245void
1246Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1247{
1248 if (log)
1249 {
1250 StreamString log_message;
1251 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1252 log_message.PutCString (": ");
1253 va_list args;
1254 va_start (args, format);
1255 log_message.PrintfVarArg (format, args);
1256 va_end (args);
1257 if (log->GetVerbose())
Zachary Turnera893d302015-03-06 20:45:43 +00001258 {
1259 std::string back_trace;
1260 llvm::raw_string_ostream stream(back_trace);
1261 llvm::sys::PrintStackTrace(stream);
1262 log_message.PutCString(back_trace.c_str());
1263 }
Greg Claytond61c0fc2012-04-23 22:55:20 +00001264 log->PutCString(log_message.GetString().c_str());
1265 }
1266}
1267
Greg Claytonc982b3d2011-11-28 01:45:00 +00001268void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269Module::Dump(Stream *s)
1270{
1271 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001272 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001274 s->Printf("Module %s%s%s%s\n",
1275 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276 m_object_name ? "(" : "",
1277 m_object_name ? m_object_name.GetCString() : "",
1278 m_object_name ? ")" : "");
1279
1280 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 if (objfile)
1284 objfile->Dump(s);
1285
1286 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 if (symbols)
1288 symbols->Dump(s);
1289
1290 s->IndentLess();
1291}
1292
1293
1294TypeList*
1295Module::GetTypeList ()
1296{
1297 SymbolVendor *symbols = GetSymbolVendor ();
1298 if (symbols)
1299 return &symbols->GetTypeList();
1300 return NULL;
1301}
1302
1303const ConstString &
1304Module::GetObjectName() const
1305{
1306 return m_object_name;
1307}
1308
1309ObjectFile *
1310Module::GetObjectFile()
1311{
Greg Clayton88c05f52015-07-24 23:38:01 +00001312 if (m_did_load_objfile.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001314 Mutex::Locker locker (m_mutex);
1315 if (m_did_load_objfile.load() == false)
Greg Clayton593577a2011-09-21 03:57:31 +00001316 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001317 Timer scoped_timer(__PRETTY_FUNCTION__,
1318 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
1319 DataBufferSP data_sp;
1320 lldb::offset_t data_offset = 0;
1321 const lldb::offset_t file_size = m_file.GetByteSize();
1322 if (file_size > m_object_offset)
Greg Clayton2540a8a2013-07-12 22:07:46 +00001323 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001324 m_did_load_objfile = true;
1325 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1326 &m_file,
1327 m_object_offset,
1328 file_size - m_object_offset,
1329 data_sp,
1330 data_offset);
1331 if (m_objfile_sp)
1332 {
1333 // Once we get the object file, update our module with the object file's
1334 // architecture since it might differ in vendor/os if some parts were
1335 // unknown. But since the matching arch might already be more specific
1336 // than the generic COFF architecture, only merge in those values that
1337 // overwrite unspecified unknown values.
1338 ArchSpec new_arch;
1339 m_objfile_sp->GetArchitecture(new_arch);
1340 m_arch.MergeFrom(new_arch);
1341 }
1342 else
1343 {
1344 ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str());
1345 }
Todd Fiala0ee56ce2014-09-05 14:48:49 +00001346 }
Greg Clayton593577a2011-09-21 03:57:31 +00001347 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 }
Greg Clayton762f7132011-09-18 18:59:15 +00001349 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350}
1351
Michael Sartaina7499c92013-07-01 19:45:50 +00001352SectionList *
Greg Clayton3046e662013-07-10 01:23:25 +00001353Module::GetSectionList()
1354{
1355 // Populate m_unified_sections_ap with sections from objfile.
1356 if (m_sections_ap.get() == NULL)
1357 {
1358 ObjectFile *obj_file = GetObjectFile();
1359 if (obj_file)
1360 obj_file->CreateSections(*GetUnifiedSectionList());
1361 }
1362 return m_sections_ap.get();
1363}
1364
Jason Molenda05a09c62014-08-22 02:46:46 +00001365void
1366Module::SectionFileAddressesChanged ()
1367{
1368 ObjectFile *obj_file = GetObjectFile ();
1369 if (obj_file)
1370 obj_file->SectionFileAddressesChanged ();
1371 SymbolVendor* sym_vendor = GetSymbolVendor();
1372 if (sym_vendor)
1373 sym_vendor->SectionFileAddressesChanged ();
1374}
1375
Greg Clayton3046e662013-07-10 01:23:25 +00001376SectionList *
Michael Sartaina7499c92013-07-01 19:45:50 +00001377Module::GetUnifiedSectionList()
1378{
Greg Clayton3046e662013-07-10 01:23:25 +00001379 // Populate m_unified_sections_ap with sections from objfile.
1380 if (m_sections_ap.get() == NULL)
1381 m_sections_ap.reset(new SectionList());
1382 return m_sections_ap.get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001383}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384
1385const Symbol *
1386Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1387{
1388 Timer scoped_timer(__PRETTY_FUNCTION__,
1389 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1390 name.AsCString(),
1391 symbol_type);
Michael Sartaina7499c92013-07-01 19:45:50 +00001392 SymbolVendor* sym_vendor = GetSymbolVendor();
1393 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001395 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001397 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398 }
1399 return NULL;
1400}
1401void
1402Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1403{
1404 // No need to protect this call using m_mutex all other method calls are
1405 // already thread safe.
1406
1407 size_t num_indices = symbol_indexes.size();
1408 if (num_indices > 0)
1409 {
1410 SymbolContext sc;
1411 CalculateSymbolContext (&sc);
1412 for (size_t i = 0; i < num_indices; i++)
1413 {
1414 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1415 if (sc.symbol)
1416 sc_list.Append (sc);
1417 }
1418 }
1419}
1420
1421size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001422Module::FindFunctionSymbols (const ConstString &name,
1423 uint32_t name_type_mask,
1424 SymbolContextList& sc_list)
1425{
1426 Timer scoped_timer(__PRETTY_FUNCTION__,
1427 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1428 name.AsCString(),
1429 name_type_mask);
Michael Sartaina7499c92013-07-01 19:45:50 +00001430 SymbolVendor* sym_vendor = GetSymbolVendor();
1431 if (sym_vendor)
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001432 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001433 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001434 if (symtab)
1435 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1436 }
1437 return 0;
1438}
1439
1440size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001441Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442{
1443 // No need to protect this call using m_mutex all other method calls are
1444 // already thread safe.
1445
1446
1447 Timer scoped_timer(__PRETTY_FUNCTION__,
1448 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1449 name.AsCString(),
1450 symbol_type);
1451 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001452 SymbolVendor* sym_vendor = GetSymbolVendor();
1453 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001455 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 if (symtab)
1457 {
1458 std::vector<uint32_t> symbol_indexes;
1459 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1460 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1461 }
1462 }
1463 return sc_list.GetSize() - initial_size;
1464}
1465
1466size_t
1467Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1468{
1469 // No need to protect this call using m_mutex all other method calls are
1470 // already thread safe.
1471
1472 Timer scoped_timer(__PRETTY_FUNCTION__,
1473 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1474 regex.GetText(),
1475 symbol_type);
1476 const size_t initial_size = sc_list.GetSize();
Michael Sartaina7499c92013-07-01 19:45:50 +00001477 SymbolVendor* sym_vendor = GetSymbolVendor();
1478 if (sym_vendor)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001480 Symtab *symtab = sym_vendor->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 if (symtab)
1482 {
1483 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001484 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1486 }
1487 }
1488 return sc_list.GetSize() - initial_size;
1489}
1490
Greg Claytone01e07b2013-04-18 18:10:51 +00001491void
1492Module::SetSymbolFileFileSpec (const FileSpec &file)
1493{
Greg Clayton902716722015-03-31 21:01:48 +00001494 if (!file.Exists())
1495 return;
Michael Sartaina7499c92013-07-01 19:45:50 +00001496 if (m_symfile_ap)
1497 {
Greg Clayton902716722015-03-31 21:01:48 +00001498 // Remove any sections in the unified section list that come from the current symbol vendor.
Greg Clayton3046e662013-07-10 01:23:25 +00001499 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001500 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1501 if (section_list && symbol_file)
1502 {
1503 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001504 // Make sure we have an object file and that the symbol vendor's objfile isn't
1505 // the same as the module's objfile before we remove any sections for it...
Greg Clayton902716722015-03-31 21:01:48 +00001506 if (obj_file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001507 {
Greg Clayton902716722015-03-31 21:01:48 +00001508 // Check to make sure we aren't trying to specify the file we already have
1509 if (obj_file->GetFileSpec() == file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001510 {
Greg Clayton902716722015-03-31 21:01:48 +00001511 // We are being told to add the exact same file that we already have
1512 // we don't have to do anything.
1513 return;
1514 }
Tamas Berghammerd00438e2015-07-30 12:38:18 +00001515
1516 // Cleare the current symtab as we are going to replace it with a new one
1517 obj_file->ClearSymtab();
Greg Clayton902716722015-03-31 21:01:48 +00001518
1519 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead
1520 // of a full path to the symbol file within the bundle
1521 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this
1522
1523 if (file.IsDirectory())
1524 {
1525 std::string new_path(file.GetPath());
1526 std::string old_path(obj_file->GetFileSpec().GetPath());
1527 if (old_path.find(new_path) == 0)
Michael Sartaina7499c92013-07-01 19:45:50 +00001528 {
Greg Clayton902716722015-03-31 21:01:48 +00001529 // We specified the same bundle as the symbol file that we already have
1530 return;
1531 }
1532 }
1533
1534 if (obj_file != m_objfile_sp.get())
1535 {
1536 size_t num_sections = section_list->GetNumSections (0);
1537 for (size_t idx = num_sections; idx > 0; --idx)
1538 {
1539 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1540 if (section_sp->GetObjectFile() == obj_file)
1541 {
1542 section_list->DeleteSection (idx - 1);
1543 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001544 }
1545 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001546 }
1547 }
Greg Clayton902716722015-03-31 21:01:48 +00001548 // Keep all old symbol files around in case there are any lingering type references in
1549 // any SBValue objects that might have been handed out.
1550 m_old_symfiles.push_back(std::move(m_symfile_ap));
Michael Sartaina7499c92013-07-01 19:45:50 +00001551 }
Greg Claytone01e07b2013-04-18 18:10:51 +00001552 m_symfile_spec = file;
1553 m_symfile_ap.reset();
1554 m_did_load_symbol_vendor = false;
1555}
1556
Jim Ingham5aee1622010-08-09 23:31:02 +00001557bool
1558Module::IsExecutable ()
1559{
1560 if (GetObjectFile() == NULL)
1561 return false;
1562 else
1563 return GetObjectFile()->IsExecutable();
1564}
1565
Jim Inghamb53cb272011-08-03 01:03:17 +00001566bool
1567Module::IsLoadedInTarget (Target *target)
1568{
1569 ObjectFile *obj_file = GetObjectFile();
1570 if (obj_file)
1571 {
Greg Clayton3046e662013-07-10 01:23:25 +00001572 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001573 if (sections != NULL)
1574 {
1575 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001576 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1577 {
1578 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1579 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1580 {
1581 return true;
1582 }
1583 }
1584 }
1585 }
1586 return false;
1587}
Enrico Granata17598482012-11-08 02:22:02 +00001588
1589bool
Enrico Granata97303392013-05-21 00:00:30 +00001590Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001591{
1592 if (!target)
1593 {
1594 error.SetErrorString("invalid destination Target");
1595 return false;
1596 }
1597
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001598 LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001599
Greg Clayton994740f2014-08-18 21:08:44 +00001600 if (should_load == eLoadScriptFromSymFileFalse)
1601 return false;
1602
Greg Clayton91c0e742013-01-11 23:44:27 +00001603 Debugger &debugger = target->GetDebugger();
1604 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1605 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001606 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001607
1608 PlatformSP platform_sp(target->GetPlatform());
1609
1610 if (!platform_sp)
1611 {
1612 error.SetErrorString("invalid Platform");
1613 return false;
1614 }
Enrico Granata17598482012-11-08 02:22:02 +00001615
Greg Claytonb9d88902013-03-23 00:50:58 +00001616 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
Enrico Granatafe7295d2014-08-16 00:32:58 +00001617 *this,
1618 feedback_stream);
Greg Claytonb9d88902013-03-23 00:50:58 +00001619
1620
1621 const uint32_t num_specs = file_specs.GetSize();
1622 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001623 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001624 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1625 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001626 {
1627 for (uint32_t i=0; i<num_specs; ++i)
1628 {
1629 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1630 if (scripting_fspec && scripting_fspec.Exists())
1631 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001632 if (should_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001633 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001634 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001635 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1636 "this debug session:\n\n command script import \"%s\"\n\n"
1637 "To run all discovered debug scripts in this session:\n\n"
1638 " settings set target.load-script-from-symbol-file true\n",
1639 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1640 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001641 return false;
1642 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001643 StreamString scripting_stream;
1644 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001645 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001646 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001647 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1648 can_reload,
1649 init_lldb_globals,
1650 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001651 if (!did_load)
1652 return false;
1653 }
1654 }
1655 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001656 else
1657 {
1658 error.SetErrorString("invalid ScriptInterpreter");
1659 return false;
1660 }
Enrico Granata17598482012-11-08 02:22:02 +00001661 }
1662 }
1663 return true;
1664}
1665
1666bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001667Module::SetArchitecture (const ArchSpec &new_arch)
1668{
Greg Clayton64195a22011-02-23 00:35:02 +00001669 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001670 {
1671 m_arch = new_arch;
1672 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001673 }
Chaoren Linb6cd5fe2015-02-26 22:15:16 +00001674 return m_arch.IsCompatibleMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001675}
1676
Greg Claytonc9660542012-02-05 02:38:54 +00001677bool
Greg Clayton751caf62014-02-07 22:54:47 +00001678Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001679{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001680 ObjectFile *object_file = GetObjectFile();
1681 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001682 {
Greg Clayton751caf62014-02-07 22:54:47 +00001683 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001684 return true;
1685 }
1686 else
1687 {
1688 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001689 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001690 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001691}
1692
Greg Claytonb9a01b32012-02-26 05:51:37 +00001693
1694bool
1695Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1696{
1697 const UUID &uuid = module_ref.GetUUID();
1698
1699 if (uuid.IsValid())
1700 {
1701 // If the UUID matches, then nothing more needs to match...
1702 if (uuid == GetUUID())
1703 return true;
1704 else
1705 return false;
1706 }
1707
1708 const FileSpec &file_spec = module_ref.GetFileSpec();
1709 if (file_spec)
1710 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001711 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001712 return false;
1713 }
1714
1715 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1716 if (platform_file_spec)
1717 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001718 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001719 return false;
1720 }
1721
1722 const ArchSpec &arch = module_ref.GetArchitecture();
1723 if (arch.IsValid())
1724 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001725 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001726 return false;
1727 }
1728
1729 const ConstString &object_name = module_ref.GetObjectName();
1730 if (object_name)
1731 {
1732 if (object_name != GetObjectName())
1733 return false;
1734 }
1735 return true;
1736}
1737
Greg Claytond804d282012-03-15 21:01:31 +00001738bool
1739Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1740{
1741 Mutex::Locker locker (m_mutex);
1742 return m_source_mappings.FindFile (orig_spec, new_spec);
1743}
1744
Greg Claytonf9be6932012-03-19 22:22:41 +00001745bool
1746Module::RemapSourceFile (const char *path, std::string &new_path) const
1747{
1748 Mutex::Locker locker (m_mutex);
1749 return m_source_mappings.RemapPath(path, new_path);
1750}
1751
Enrico Granata3467d802012-09-04 18:47:54 +00001752uint32_t
1753Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1754{
1755 ObjectFile *obj_file = GetObjectFile();
1756 if (obj_file)
1757 return obj_file->GetVersion (versions, num_versions);
1758
1759 if (versions && num_versions)
1760 {
1761 for (uint32_t i=0; i<num_versions; ++i)
Enrico Granataafcbdb12014-03-25 20:53:33 +00001762 versions[i] = LLDB_INVALID_MODULE_VERSION;
Enrico Granata3467d802012-09-04 18:47:54 +00001763 }
1764 return 0;
1765}
Greg Clayton43fe2172013-04-03 02:00:15 +00001766
1767void
1768Module::PrepareForFunctionNameLookup (const ConstString &name,
1769 uint32_t name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001770 LanguageType language,
Greg Clayton43fe2172013-04-03 02:00:15 +00001771 ConstString &lookup_name,
1772 uint32_t &lookup_name_type_mask,
1773 bool &match_name_after_lookup)
1774{
1775 const char *name_cstr = name.GetCString();
1776 lookup_name_type_mask = eFunctionNameTypeNone;
1777 match_name_after_lookup = false;
Jim Inghamfa39bb42014-10-25 00:33:55 +00001778
1779 llvm::StringRef basename;
1780 llvm::StringRef context;
Greg Clayton43fe2172013-04-03 02:00:15 +00001781
1782 if (name_type_mask & eFunctionNameTypeAuto)
1783 {
1784 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1785 lookup_name_type_mask = eFunctionNameTypeFull;
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001786 else if ((language == eLanguageTypeUnknown ||
Jim Ingham0e0984e2015-09-02 01:06:46 +00001787 Language::LanguageIsObjC(language)) &&
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001788 ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001789 lookup_name_type_mask = eFunctionNameTypeFull;
Jim Ingham0e0984e2015-09-02 01:06:46 +00001790 else if (Language::LanguageIsC(language))
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001791 {
1792 lookup_name_type_mask = eFunctionNameTypeFull;
1793 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001794 else
1795 {
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001796 if ((language == eLanguageTypeUnknown ||
Jim Ingham0e0984e2015-09-02 01:06:46 +00001797 Language::LanguageIsObjC(language)) &&
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001798 ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001799 lookup_name_type_mask |= eFunctionNameTypeSelector;
1800
Greg Clayton6ecb2322013-05-18 00:11:21 +00001801 CPPLanguageRuntime::MethodName cpp_method (name);
Jim Inghamfa39bb42014-10-25 00:33:55 +00001802 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001803 if (basename.empty())
1804 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001805 if (CPPLanguageRuntime::ExtractContextAndIdentifier (name_cstr, context, basename))
Greg Clayton6ecb2322013-05-18 00:11:21 +00001806 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Claytonc3795402014-10-22 21:47:13 +00001807 else
Greg Clayton65b0e762015-01-28 01:08:39 +00001808 lookup_name_type_mask |= eFunctionNameTypeFull;
Greg Clayton6ecb2322013-05-18 00:11:21 +00001809 }
1810 else
1811 {
Greg Clayton43fe2172013-04-03 02:00:15 +00001812 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001813 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001814 }
1815 }
1816 else
1817 {
1818 lookup_name_type_mask = name_type_mask;
1819 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1820 {
1821 // If they've asked for a CPP method or function name and it can't be that, we don't
1822 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001823 CPPLanguageRuntime::MethodName cpp_method (name);
1824 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001825 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001826 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001827
1828 if (!cpp_method.GetQualifiers().empty())
1829 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001830 // There is a "const" or other qualifier following the end of the function parens,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001831 // this can't be a eFunctionNameTypeBase
1832 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1833 if (lookup_name_type_mask == eFunctionNameTypeNone)
1834 return;
1835 }
1836 }
1837 else
1838 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001839 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can.
1840 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later.
1841 CPPLanguageRuntime::ExtractContextAndIdentifier (name_cstr, context, basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001842 }
1843 }
1844
1845 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1846 {
1847 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1848 {
1849 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1850 if (lookup_name_type_mask == eFunctionNameTypeNone)
1851 return;
1852 }
1853 }
1854 }
1855
Jim Inghamfa39bb42014-10-25 00:33:55 +00001856 if (!basename.empty())
Greg Clayton43fe2172013-04-03 02:00:15 +00001857 {
1858 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1859 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1860 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1861 // to true
Jim Inghamfa39bb42014-10-25 00:33:55 +00001862 lookup_name.SetString(basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001863 match_name_after_lookup = true;
1864 }
1865 else
1866 {
1867 // The name is already correct, just use the exact name as supplied, and we won't need
1868 // to check if any matches contain "name"
1869 lookup_name = name;
1870 match_name_after_lookup = false;
1871 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001872}
Greg Clayton23f8c952014-03-24 23:10:19 +00001873
1874ModuleSP
1875Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
1876{
1877 if (delegate_sp)
1878 {
1879 // Must create a module and place it into a shared pointer before
1880 // we can create an object file since it has a std::weak_ptr back
1881 // to the module, so we need to control the creation carefully in
1882 // this static function
1883 ModuleSP module_sp(new Module());
1884 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
1885 if (module_sp->m_objfile_sp)
1886 {
1887 // Once we get the object file, update our module with the object file's
1888 // architecture since it might differ in vendor/os if some parts were
1889 // unknown.
1890 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
1891 }
1892 return module_sp;
1893 }
1894 return ModuleSP();
1895}
1896
Greg Clayton08928f32015-02-05 02:01:34 +00001897bool
1898Module::GetIsDynamicLinkEditor()
1899{
1900 ObjectFile * obj_file = GetObjectFile ();
1901
1902 if (obj_file)
1903 return obj_file->GetIsDynamicLinkEditor();
1904
1905 return false;
1906}