blob: a29456f5b5a5fde31fc244862f8921eca77d286e [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"
Greg Clayton56939cb2015-09-17 22:23:34 +000018#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000021#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000023#include "lldb/Host/Host.h"
Enrico Granata17598482012-11-08 02:22:02 +000024#include "lldb/Host/Symbols.h"
25#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/ScriptInterpreter.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"
Greg Clayton56939cb2015-09-17 22:23:34 +000030#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000032#include "lldb/Symbol/TypeSystem.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000033#include "lldb/Target/Language.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"
Jim Inghamaa816b82015-09-02 01:59:14 +000037#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
38#include "Plugins/Language/ObjC/ObjCLanguage.h"
Ravitheja Addepally40697302015-10-08 09:45:41 +000039#include "lldb/Symbol/TypeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
Greg Clayton23f8c952014-03-24 23:10:19 +000041#include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
42
Zachary Turnera893d302015-03-06 20:45:43 +000043#include "llvm/Support/raw_os_ostream.h"
44#include "llvm/Support/Signals.h"
45
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046using namespace lldb;
47using namespace lldb_private;
48
Greg Clayton65a03992011-08-09 00:01:09 +000049// Shared pointers to modules track module lifetimes in
50// targets and in the global module, but this collection
51// will track all module objects that are still alive
52typedef std::vector<Module *> ModuleCollection;
53
54static ModuleCollection &
55GetModuleCollection()
56{
Jim Ingham549f7372011-10-31 23:47:10 +000057 // This module collection needs to live past any module, so we could either make it a
58 // shared pointer in each module or just leak is. Since it is only an empty vector by
59 // the time all the modules have gone away, we just leak it for now. If we decide this
60 // is a big problem we can introduce a Finalize method that will tear everything down in
61 // a predictable order.
62
63 static ModuleCollection *g_module_collection = NULL;
64 if (g_module_collection == NULL)
65 g_module_collection = new ModuleCollection();
66
67 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000068}
69
Greg Claytonb26e6be2012-01-27 18:08:35 +000070Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000071Module::GetAllocationModuleCollectionMutex()
72{
Greg Claytonb26e6be2012-01-27 18:08:35 +000073 // NOTE: The mutex below must be leaked since the global module list in
74 // the ModuleList class will get torn at some point, and we can't know
75 // if it will tear itself down before the "g_module_collection_mutex" below
76 // will. So we leak a Mutex object below to safeguard against that
77
78 static Mutex *g_module_collection_mutex = NULL;
79 if (g_module_collection_mutex == NULL)
80 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
81 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000082}
83
84size_t
85Module::GetNumberAllocatedModules ()
86{
87 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
88 return GetModuleCollection().size();
89}
90
91Module *
92Module::GetAllocatedModuleAtIndex (size_t idx)
93{
94 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
95 ModuleCollection &modules = GetModuleCollection();
96 if (idx < modules.size())
97 return modules[idx];
98 return NULL;
99}
Greg Clayton29ad7b92012-01-27 18:45:39 +0000100#if 0
Greg Clayton65a03992011-08-09 00:01:09 +0000101
Greg Clayton29ad7b92012-01-27 18:45:39 +0000102// These functions help us to determine if modules are still loaded, yet don't require that
103// you have a command interpreter and can easily be called from an external debugger.
104namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +0000105
Greg Clayton29ad7b92012-01-27 18:45:39 +0000106 void
107 ClearModuleInfo (void)
108 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000109 const bool mandatory = true;
110 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000111 }
112
113 void
114 DumpModuleInfo (void)
115 {
116 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
117 ModuleCollection &modules = GetModuleCollection();
118 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000119 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000120 for (size_t i=0; i<count; ++i)
121 {
122
123 StreamString strm;
124 Module *module = modules[i];
125 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
126 module->GetDescription(&strm, eDescriptionLevelFull);
127 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
128 module,
129 in_shared_module_list,
130 (uint32_t)module->use_count(),
131 strm.GetString().c_str());
132 }
133 }
134}
135
136#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000137
Greg Claytonb9a01b32012-02-26 05:51:37 +0000138Module::Module (const ModuleSpec &module_spec) :
139 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton34f11592014-03-04 21:20:23 +0000140 m_mod_time (),
141 m_arch (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000142 m_uuid (),
Greg Clayton34f11592014-03-04 21:20:23 +0000143 m_file (),
144 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000145 m_remote_install_file(),
Greg Clayton34f11592014-03-04 21:20:23 +0000146 m_symfile_spec (),
147 m_object_name (),
148 m_object_offset (),
149 m_object_mod_time (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000150 m_objfile_sp (),
151 m_symfile_ap (),
Greg Clayton56939cb2015-09-17 22:23:34 +0000152 m_type_system_map(),
Greg Claytond804d282012-03-15 21:01:31 +0000153 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000154 m_sections_ap(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000155 m_did_load_objfile (false),
156 m_did_load_symbol_vendor (false),
157 m_did_parse_uuid (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000158 m_file_has_changed (false),
159 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000160{
161 // Scope for locker below...
162 {
163 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
164 GetModuleCollection().push_back(this);
165 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000166
Greg Clayton5160ce52013-03-27 23:08:40 +0000167 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000168 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000169 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170 static_cast<void*>(this),
Greg Clayton34f11592014-03-04 21:20:23 +0000171 module_spec.GetArchitecture().GetArchitectureName(),
172 module_spec.GetFileSpec().GetPath().c_str(),
173 module_spec.GetObjectName().IsEmpty() ? "" : "(",
174 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
175 module_spec.GetObjectName().IsEmpty() ? "" : ")");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000176
Greg Clayton34f11592014-03-04 21:20:23 +0000177 // First extract all module specifications from the file using the local
178 // file path. If there are no specifications, then don't fill anything in
179 ModuleSpecList modules_specs;
180 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
181 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000182
Greg Clayton34f11592014-03-04 21:20:23 +0000183 // Now make sure that one of the module specifications matches what we just
184 // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
185 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
186 // UUID YYY and we don't want those to match. If they don't match, just don't
187 // fill any ivars in so we don't accidentally grab the wrong file later since
188 // they don't match...
189 ModuleSpec matching_module_spec;
190 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
191 return;
Greg Clayton7ab7f892014-05-29 21:33:45 +0000192
193 if (module_spec.GetFileSpec())
194 m_mod_time = module_spec.GetFileSpec().GetModificationTime();
195 else if (matching_module_spec.GetFileSpec())
196 m_mod_time = matching_module_spec.GetFileSpec().GetModificationTime();
197
198 // Copy the architecture from the actual spec if we got one back, else use the one that was specified
199 if (matching_module_spec.GetArchitecture().IsValid())
Greg Clayton34f11592014-03-04 21:20:23 +0000200 m_arch = matching_module_spec.GetArchitecture();
Greg Clayton7ab7f892014-05-29 21:33:45 +0000201 else if (module_spec.GetArchitecture().IsValid())
202 m_arch = module_spec.GetArchitecture();
203
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000204 // Copy the file spec over and use the specified one (if there was one) so we
Greg Clayton7ab7f892014-05-29 21:33:45 +0000205 // don't use a path that might have gotten resolved a path in 'matching_module_spec'
206 if (module_spec.GetFileSpec())
207 m_file = module_spec.GetFileSpec();
208 else if (matching_module_spec.GetFileSpec())
209 m_file = matching_module_spec.GetFileSpec();
210
211 // Copy the platform file spec over
212 if (module_spec.GetPlatformFileSpec())
213 m_platform_file = module_spec.GetPlatformFileSpec();
214 else if (matching_module_spec.GetPlatformFileSpec())
215 m_platform_file = matching_module_spec.GetPlatformFileSpec();
216
217 // Copy the symbol file spec over
218 if (module_spec.GetSymbolFileSpec())
219 m_symfile_spec = module_spec.GetSymbolFileSpec();
220 else if (matching_module_spec.GetSymbolFileSpec())
221 m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
222
223 // Copy the object name over
224 if (matching_module_spec.GetObjectName())
225 m_object_name = matching_module_spec.GetObjectName();
226 else
227 m_object_name = module_spec.GetObjectName();
228
229 // Always trust the object offset (file offset) and object modification
230 // time (for mod time in a BSD static archive) of from the matching
231 // module specification
Greg Clayton36d7c892014-05-29 17:52:46 +0000232 m_object_offset = matching_module_spec.GetObjectOffset();
233 m_object_mod_time = matching_module_spec.GetObjectModificationTime();
Greg Clayton34f11592014-03-04 21:20:23 +0000234
Greg Claytonb9a01b32012-02-26 05:51:37 +0000235}
236
Greg Claytone72dfb32012-02-24 01:59:29 +0000237Module::Module(const FileSpec& file_spec,
238 const ArchSpec& arch,
239 const ConstString *object_name,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000240 lldb::offset_t object_offset,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000241 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 m_mutex (Mutex::eMutexTypeRecursive),
243 m_mod_time (file_spec.GetModificationTime()),
244 m_arch (arch),
245 m_uuid (),
246 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000247 m_platform_file(),
Greg Claytonfbb76342013-11-20 21:07:01 +0000248 m_remote_install_file (),
Greg Claytone72dfb32012-02-24 01:59:29 +0000249 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000251 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000252 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000253 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000254 m_symfile_ap (),
Greg Clayton56939cb2015-09-17 22:23:34 +0000255 m_type_system_map(),
Greg Claytond804d282012-03-15 21:01:31 +0000256 m_source_mappings (),
Greg Clayton23f8c952014-03-24 23:10:19 +0000257 m_sections_ap(),
Greg Claytone83e7312010-09-07 23:40:05 +0000258 m_did_load_objfile (false),
259 m_did_load_symbol_vendor (false),
260 m_did_parse_uuid (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 (),
Greg Clayton56939cb2015-09-17 22:23:34 +0000300 m_type_system_map(),
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),
Greg Clayton23f8c952014-03-24 23:10:19 +0000306 m_file_has_changed (false),
307 m_first_file_changed_log (false)
308{
309 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
310 GetModuleCollection().push_back(this);
311}
312
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313Module::~Module()
314{
Greg Clayton217b28b2013-05-22 20:13:22 +0000315 // Lock our module down while we tear everything down to make sure
316 // we don't get any access to the module while it is being destroyed
317 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000318 // Scope for locker below...
319 {
320 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
321 ModuleCollection &modules = GetModuleCollection();
322 ModuleCollection::iterator end = modules.end();
323 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000324 assert (pos != end);
325 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000326 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000327 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000329 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000330 static_cast<void*>(this),
Greg Clayton64195a22011-02-23 00:35:02 +0000331 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000332 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 m_object_name.IsEmpty() ? "" : "(",
334 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
335 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000336 // Release any auto pointers before we start tearing down our member
337 // variables since the object file and symbol files might need to make
338 // function calls back into this module object. The ordering is important
339 // here because symbol files can require the module object file. So we tear
340 // down the symbol file first, then the object file.
Greg Clayton3046e662013-07-10 01:23:25 +0000341 m_sections_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000342 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000343 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000346ObjectFile *
Andrew MacPherson17220c12014-03-05 10:12:43 +0000347Module::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 +0000348{
349 if (m_objfile_sp)
350 {
351 error.SetErrorString ("object file already exists");
352 }
353 else
354 {
355 Mutex::Locker locker (m_mutex);
356 if (process_sp)
357 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000358 m_did_load_objfile = true;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000359 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000360 Error readmem_error;
361 const size_t bytes_read = process_sp->ReadMemory (header_addr,
362 data_ap->GetBytes(),
363 data_ap->GetByteSize(),
364 readmem_error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000365 if (bytes_read == size_to_read)
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000366 {
367 DataBufferSP data_sp(data_ap.release());
368 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
369 if (m_objfile_sp)
370 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000371 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000372 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000373 m_object_name.SetCString (s.GetData());
374
375 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000376 // architecture since it might differ in vendor/os if some parts were
377 // unknown.
378 m_objfile_sp->GetArchitecture (m_arch);
379 }
380 else
381 {
382 error.SetErrorString ("unable to find suitable object file plug-in");
383 }
384 }
385 else
386 {
387 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
388 }
389 }
390 else
391 {
392 error.SetErrorString ("invalid process");
393 }
394 }
395 return m_objfile_sp.get();
396}
397
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398
Greg Clayton60830262011-02-04 18:53:10 +0000399const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400Module::GetUUID()
401{
Greg Clayton88c05f52015-07-24 23:38:01 +0000402 if (m_did_parse_uuid.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000404 Mutex::Locker locker (m_mutex);
405 if (m_did_parse_uuid.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 {
Greg Clayton88c05f52015-07-24 23:38:01 +0000407 ObjectFile * obj_file = GetObjectFile ();
408
409 if (obj_file != NULL)
410 {
411 obj_file->GetUUID(&m_uuid);
412 m_did_parse_uuid = true;
413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 }
415 }
416 return m_uuid;
417}
418
Greg Clayton8b4edba2015-08-14 20:02:05 +0000419TypeSystem *
420Module::GetTypeSystemForLanguage (LanguageType language)
421{
Greg Clayton5beec212015-10-08 21:04:34 +0000422 return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000423}
424
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425void
426Module::ParseAllDebugSymbols()
427{
428 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000429 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 if (num_comp_units == 0)
431 return;
432
Greg Claytona2eee182011-09-17 07:23:18 +0000433 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000434 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 SymbolVendor *symbols = GetSymbolVendor ();
436
Greg Claytonc7bece562013-01-25 18:06:21 +0000437 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 {
439 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
440 if (sc.comp_unit)
441 {
442 sc.function = NULL;
443 symbols->ParseVariablesForContext(sc);
444
445 symbols->ParseCompileUnitFunctions(sc);
446
Greg Claytonc7bece562013-01-25 18:06:21 +0000447 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 +0000448 {
449 symbols->ParseFunctionBlocks(sc);
450
451 // Parse the variables for this function and all its blocks
452 symbols->ParseVariablesForContext(sc);
453 }
454
455
456 // Parse all types for this compile unit
457 sc.function = NULL;
458 symbols->ParseTypes(sc);
459 }
460 }
461}
462
463void
464Module::CalculateSymbolContext(SymbolContext* sc)
465{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000466 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467}
468
Greg Claytone72dfb32012-02-24 01:59:29 +0000469ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000470Module::CalculateSymbolContextModule ()
471{
Greg Claytone72dfb32012-02-24 01:59:29 +0000472 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000473}
474
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475void
476Module::DumpSymbolContext(Stream *s)
477{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000478 s->Printf(", Module{%p}", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Greg Claytonc7bece562013-01-25 18:06:21 +0000481size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482Module::GetNumCompileUnits()
483{
484 Mutex::Locker locker (m_mutex);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485 Timer scoped_timer(__PRETTY_FUNCTION__,
486 "Module::GetNumCompileUnits (module = %p)",
487 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 SymbolVendor *symbols = GetSymbolVendor ();
489 if (symbols)
490 return symbols->GetNumCompileUnits();
491 return 0;
492}
493
494CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000495Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496{
497 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000498 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 CompUnitSP cu_sp;
500
501 if (index < num_comp_units)
502 {
503 SymbolVendor *symbols = GetSymbolVendor ();
504 if (symbols)
505 cu_sp = symbols->GetCompileUnitAtIndex(index);
506 }
507 return cu_sp;
508}
509
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510bool
511Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
512{
513 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000514 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000515 SectionList *section_list = GetSectionList();
516 if (section_list)
517 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 return false;
519}
520
521uint32_t
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000522Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
523 bool resolve_tail_call_address)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524{
525 Mutex::Locker locker (m_mutex);
526 uint32_t resolved_flags = 0;
527
Greg Clayton72310352013-02-23 04:12:47 +0000528 // Clear the result symbol context in case we don't find anything, but don't clear the target
529 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530
531 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000532 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
534 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000535 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 {
537 // If the section offset based address resolved itself, then this
538 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000539 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 resolved_flags |= eSymbolContextModule;
541
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000542 SymbolVendor* sym_vendor = GetSymbolVendor();
543 if (!sym_vendor)
544 return resolved_flags;
545
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 // Resolve the compile unit, function, block, line table or line
547 // entry if requested.
548 if (resolve_scope & eSymbolContextCompUnit ||
549 resolve_scope & eSymbolContextFunction ||
550 resolve_scope & eSymbolContextBlock ||
551 resolve_scope & eSymbolContextLineEntry )
552 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000553 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 }
555
Jim Ingham680e1772010-08-31 23:51:36 +0000556 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
557 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000559 Symtab *symtab = sym_vendor->GetSymtab();
560 if (symtab && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 {
Mohit K. Bhakkad0d9dd7d2016-01-23 10:36:06 +0000562 Symbol *matching_symbol = nullptr;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000563
564 symtab->ForEachSymbolContainingFileAddress(so_addr.GetFileAddress(),
565 [&matching_symbol](Symbol *symbol) -> bool {
566 if (symbol->GetType() != eSymbolTypeInvalid)
567 {
568 matching_symbol = symbol;
569 return false; // Stop iterating
570 }
571 return true; // Keep iterating
572 });
Mohit K. Bhakkad0d9dd7d2016-01-23 10:36:06 +0000573 sc.symbol = matching_symbol;
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000574 if (!sc.symbol &&
575 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
576 {
577 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
578 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
579 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
580 }
581
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000582 if (sc.symbol)
Greg Clayton93e28612013-10-11 22:03:48 +0000583 {
584 if (sc.symbol->IsSynthetic())
585 {
586 // We have a synthetic symbol so lets check if the object file
587 // from the symbol file in the symbol vendor is different than
588 // the object file for the module, and if so search its symbol
589 // table to see if we can come up with a better symbol. For example
590 // dSYM files on MacOSX have an unstripped symbol table inside of
591 // them.
592 ObjectFile *symtab_objfile = symtab->GetObjectFile();
593 if (symtab_objfile && symtab_objfile->IsStripped())
594 {
595 SymbolFile *symfile = sym_vendor->GetSymbolFile();
596 if (symfile)
597 {
598 ObjectFile *symfile_objfile = symfile->GetObjectFile();
599 if (symfile_objfile != symtab_objfile)
600 {
601 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
602 if (symfile_symtab)
603 {
604 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
605 if (symbol && !symbol->IsSynthetic())
606 {
607 sc.symbol = symbol;
608 }
609 }
610 }
611 }
612 }
613 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000614 resolved_flags |= eSymbolContextSymbol;
Greg Clayton93e28612013-10-11 22:03:48 +0000615 }
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000616 }
617 }
618
619 // For function symbols, so_addr may be off by one. This is a convention consistent
620 // with FDE row indices in eh_frame sections, but requires extra logic here to permit
621 // symbol lookup for disassembly and unwind.
622 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000623 resolve_tail_call_address && so_addr.IsSectionOffset())
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000624 {
625 Address previous_addr = so_addr;
Greg Claytonedfaae32013-09-18 20:03:31 +0000626 previous_addr.Slide(-1);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000627
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000628 bool do_resolve_tail_call_address = false; // prevent recursion
629 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
630 do_resolve_tail_call_address);
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000631 if (flags & eSymbolContextSymbol)
632 {
633 AddressRange addr_range;
634 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000636 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 {
Ashok Thirumurthi38807142013-09-16 22:00:17 +0000638 // If the requested address is one past the address range of a function (i.e. a tail call),
639 // or the decremented address is the start of a function (i.e. some forms of trampoline),
640 // indicate that the symbol has been resolved.
641 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
642 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
643 {
644 resolved_flags |= flags;
645 }
646 }
647 else
648 {
649 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 }
651 }
652 }
653 }
654 }
655 return resolved_flags;
656}
657
658uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000659Module::ResolveSymbolContextForFilePath
660(
661 const char *file_path,
662 uint32_t line,
663 bool check_inlines,
664 uint32_t resolve_scope,
665 SymbolContextList& sc_list
666)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667{
Greg Clayton274060b2010-10-20 20:54:39 +0000668 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
670}
671
672uint32_t
673Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
674{
675 Mutex::Locker locker (m_mutex);
676 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000677 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
678 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 line,
680 check_inlines ? "yes" : "no",
681 resolve_scope);
682
683 const uint32_t initial_count = sc_list.GetSize();
684
685 SymbolVendor *symbols = GetSymbolVendor ();
686 if (symbols)
687 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
688
689 return sc_list.GetSize() - initial_count;
690}
691
692
Greg Claytonc7bece562013-01-25 18:06:21 +0000693size_t
694Module::FindGlobalVariables (const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000695 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000696 bool append,
697 size_t max_matches,
698 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699{
700 SymbolVendor *symbols = GetSymbolVendor ();
701 if (symbols)
Greg Clayton99558cc42015-08-24 23:46:31 +0000702 return symbols->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 return 0;
704}
Greg Claytonc7bece562013-01-25 18:06:21 +0000705
706size_t
707Module::FindGlobalVariables (const RegularExpression& regex,
708 bool append,
709 size_t max_matches,
710 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
712 SymbolVendor *symbols = GetSymbolVendor ();
713 if (symbols)
714 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
715 return 0;
716}
717
Greg Claytonc7bece562013-01-25 18:06:21 +0000718size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000719Module::FindCompileUnits (const FileSpec &path,
720 bool append,
721 SymbolContextList &sc_list)
722{
723 if (!append)
724 sc_list.Clear();
725
Greg Claytonc7bece562013-01-25 18:06:21 +0000726 const size_t start_size = sc_list.GetSize();
727 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000728 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000729 sc.module_sp = shared_from_this();
Sean Callananddd7a2a2013-10-03 22:27:29 +0000730 const bool compare_directory = (bool)path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000731 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000732 {
733 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000734 if (sc.comp_unit)
735 {
736 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
737 sc_list.Append(sc);
738 }
Greg Clayton644247c2011-07-07 01:59:51 +0000739 }
740 return sc_list.GetSize() - start_size;
741}
742
Greg Claytonc7bece562013-01-25 18:06:21 +0000743size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000744Module::FindFunctions (const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000745 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000746 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000747 bool include_symbols,
748 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000749 bool append,
750 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751{
Greg Clayton931180e2011-01-27 06:44:37 +0000752 if (!append)
753 sc_list.Clear();
754
Greg Clayton43fe2172013-04-03 02:00:15 +0000755 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000756
757 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000759
760 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000761 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000762 ConstString lookup_name;
763 uint32_t lookup_name_type_mask = 0;
764 bool match_name_after_lookup = false;
765 Module::PrepareForFunctionNameLookup (name,
766 name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000767 eLanguageTypeUnknown, // TODO: add support
Greg Clayton43fe2172013-04-03 02:00:15 +0000768 lookup_name,
769 lookup_name_type_mask,
770 match_name_after_lookup);
771
772 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000773 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000774 symbols->FindFunctions(lookup_name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000775 parent_decl_ctx,
Greg Clayton43fe2172013-04-03 02:00:15 +0000776 lookup_name_type_mask,
777 include_inlines,
778 append,
779 sc_list);
780
Michael Sartaina7499c92013-07-01 19:45:50 +0000781 // Now check our symbol table for symbols that are code symbols if requested
782 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000783 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000784 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000785 if (symtab)
786 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
787 }
788 }
789
790 if (match_name_after_lookup)
791 {
792 SymbolContext sc;
793 size_t i = old_size;
794 while (i<sc_list.GetSize())
795 {
796 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000797 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000798 const char *func_name = sc.GetFunctionName().GetCString();
799 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000800 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000801 // Remove the current context
802 sc_list.RemoveContextAtIndex(i);
803 // Don't increment i and continue in the loop
804 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000805 }
806 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000807 ++i;
808 }
809 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000810 }
811 else
812 {
813 if (symbols)
Michael Sartaina7499c92013-07-01 19:45:50 +0000814 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000815 symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list);
Greg Clayton43fe2172013-04-03 02:00:15 +0000816
Michael Sartaina7499c92013-07-01 19:45:50 +0000817 // Now check our symbol table for symbols that are code symbols if requested
818 if (include_symbols)
Greg Clayton43fe2172013-04-03 02:00:15 +0000819 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000820 Symtab *symtab = symbols->GetSymtab();
Greg Clayton43fe2172013-04-03 02:00:15 +0000821 if (symtab)
822 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000823 }
824 }
825 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000826
827 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828}
829
Greg Claytonc7bece562013-01-25 18:06:21 +0000830size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000831Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000832 bool include_symbols,
833 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000834 bool append,
835 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836{
Greg Clayton931180e2011-01-27 06:44:37 +0000837 if (!append)
838 sc_list.Clear();
839
Greg Claytonc7bece562013-01-25 18:06:21 +0000840 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000841
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 SymbolVendor *symbols = GetSymbolVendor ();
843 if (symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000844 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000845 symbols->FindFunctions(regex, include_inlines, append, sc_list);
846
847 // Now check our symbol table for symbols that are code symbols if requested
848 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000849 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000850 Symtab *symtab = symbols->GetSymtab();
Greg Clayton931180e2011-01-27 06:44:37 +0000851 if (symtab)
852 {
853 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000854 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000855 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000856 if (num_matches)
857 {
858 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000859 const size_t end_functions_added_index = sc_list.GetSize();
860 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
861 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000862 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000863 // No functions were added, just symbols, so we can just append them
864 for (size_t i=0; i<num_matches; ++i)
865 {
866 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
867 SymbolType sym_type = sc.symbol->GetType();
868 if (sc.symbol && (sym_type == eSymbolTypeCode ||
869 sym_type == eSymbolTypeResolver))
870 sc_list.Append(sc);
871 }
872 }
873 else
874 {
875 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
876 FileAddrToIndexMap file_addr_to_index;
877 for (size_t i=start_size; i<end_functions_added_index; ++i)
878 {
879 const SymbolContext &sc = sc_list[i];
880 if (sc.block)
881 continue;
882 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
883 }
884
885 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
886 // Functions were added so we need to merge symbols into any
887 // existing function symbol contexts
888 for (size_t i=start_size; i<num_matches; ++i)
889 {
890 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
891 SymbolType sym_type = sc.symbol->GetType();
Greg Clayton358cf1e2015-06-25 21:46:34 +0000892 if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver))
Greg Claytond8cf1a12013-06-12 00:46:38 +0000893 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000894 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress());
Greg Claytond8cf1a12013-06-12 00:46:38 +0000895 if (pos == end)
896 sc_list.Append(sc);
897 else
898 sc_list[pos->second].symbol = sc.symbol;
899 }
900 }
Greg Clayton931180e2011-01-27 06:44:37 +0000901 }
902 }
903 }
904 }
905 }
906 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907}
908
Richard Mittonf86248d2013-09-12 02:20:34 +0000909void
910Module::FindAddressesForLine (const lldb::TargetSP target_sp,
911 const FileSpec &file, uint32_t line,
912 Function *function,
913 std::vector<Address> &output_local, std::vector<Address> &output_extern)
914{
915 SearchFilterByModule filter(target_sp, m_file);
916 AddressResolverFileLine resolver(file, line, true);
917 resolver.ResolveAddress (filter);
918
919 for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
920 {
921 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
922 Function *f = addr.CalculateSymbolContextFunction();
923 if (f && f == function)
924 output_local.push_back (addr);
925 else
926 output_extern.push_back (addr);
927 }
928}
929
Greg Claytonc7bece562013-01-25 18:06:21 +0000930size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000931Module::FindTypes_Impl (const SymbolContext& sc,
932 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000933 const CompilerDeclContext *parent_decl_ctx,
Greg Clayton84db9102012-03-26 23:03:23 +0000934 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000935 size_t max_matches,
Ravitheja Addepally40697302015-10-08 09:45:41 +0000936 TypeMap& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000937{
938 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
939 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
940 {
941 SymbolVendor *symbols = GetSymbolVendor ();
942 if (symbols)
Greg Clayton99558cc42015-08-24 23:46:31 +0000943 return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000944 }
945 return 0;
946}
947
Greg Claytonc7bece562013-01-25 18:06:21 +0000948size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000949Module::FindTypesInNamespace (const SymbolContext& sc,
950 const ConstString &type_name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000951 const CompilerDeclContext *parent_decl_ctx,
Greg Claytonc7bece562013-01-25 18:06:21 +0000952 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000953 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000954{
Greg Clayton84db9102012-03-26 23:03:23 +0000955 const bool append = true;
Ravitheja Addepally40697302015-10-08 09:45:41 +0000956 TypeMap types_map;
957 size_t num_types = FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, types_map);
958 if (num_types > 0)
959 sc.SortTypeList(types_map, type_list);
960 return num_types;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000961}
962
Greg Claytonb43165b2012-12-05 21:24:42 +0000963lldb::TypeSP
964Module::FindFirstType (const SymbolContext& sc,
965 const ConstString &name,
966 bool exact_match)
967{
968 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000969 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000970 if (num_matches)
971 return type_list.GetTypeAtIndex(0);
972 return TypeSP();
973}
974
975
Greg Claytonc7bece562013-01-25 18:06:21 +0000976size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000977Module::FindTypes (const SymbolContext& sc,
978 const ConstString &name,
979 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000980 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000981 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000982{
Greg Claytonc7bece562013-01-25 18:06:21 +0000983 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000984 const char *type_name_cstr = name.GetCString();
985 std::string type_scope;
986 std::string type_basename;
987 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000988 TypeClass type_class = eTypeClassAny;
Ravitheja Addepally40697302015-10-08 09:45:41 +0000989 TypeMap typesmap;
Greg Clayton7bc31332012-10-22 16:19:56 +0000990 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000991 {
Greg Clayton84db9102012-03-26 23:03:23 +0000992 // Check if "name" starts with "::" which means the qualified type starts
993 // from the root namespace and implies and exact match. The typenames we
994 // get back from clang do not start with "::" so we need to strip this off
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000995 // in order to get the qualified names to match
Greg Clayton84db9102012-03-26 23:03:23 +0000996
997 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
998 {
999 type_scope.erase(0,2);
1000 exact_match = true;
1001 }
1002 ConstString type_basename_const_str (type_basename.c_str());
Ravitheja Addepally40697302015-10-08 09:45:41 +00001003 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, typesmap))
Greg Clayton84db9102012-03-26 23:03:23 +00001004 {
Ravitheja Addepally40697302015-10-08 09:45:41 +00001005 typesmap.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
1006 num_matches = typesmap.GetSize();
Greg Clayton84db9102012-03-26 23:03:23 +00001007 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001008 }
1009 else
Greg Clayton84db9102012-03-26 23:03:23 +00001010 {
1011 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +00001012 if (type_class != eTypeClassAny)
1013 {
1014 // The "type_name_cstr" will have been modified if we have a valid type class
1015 // prefix (like "struct", "class", "union", "typedef" etc).
Ravitheja Addepally40697302015-10-08 09:45:41 +00001016 FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, typesmap);
1017 typesmap.RemoveMismatchedTypes (type_class);
1018 num_matches = typesmap.GetSize();
Greg Clayton7bc31332012-10-22 16:19:56 +00001019 }
1020 else
1021 {
Ravitheja Addepally40697302015-10-08 09:45:41 +00001022 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, typesmap);
Greg Clayton7bc31332012-10-22 16:19:56 +00001023 }
Greg Clayton84db9102012-03-26 23:03:23 +00001024 }
Ravitheja Addepally40697302015-10-08 09:45:41 +00001025 if (num_matches > 0)
1026 sc.SortTypeList(typesmap, types);
Greg Clayton84db9102012-03-26 23:03:23 +00001027 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001028}
1029
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +00001031Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032{
Greg Clayton88c05f52015-07-24 23:38:01 +00001033 if (m_did_load_symbol_vendor.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001035 Mutex::Locker locker (m_mutex);
1036 if (m_did_load_symbol_vendor.load() == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001038 ObjectFile *obj_file = GetObjectFile ();
1039 if (obj_file != NULL)
1040 {
1041 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1042 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1043 m_did_load_symbol_vendor = true;
1044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045 }
1046 }
1047 return m_symfile_ap.get();
1048}
1049
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050void
1051Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
1052{
1053 // Container objects whose paths do not specify a file directly can call
1054 // this function to correct the file and object names.
1055 m_file = file;
1056 m_mod_time = file.GetModificationTime();
1057 m_object_name = object_name;
1058}
1059
1060const ArchSpec&
1061Module::GetArchitecture () const
1062{
1063 return m_arch;
1064}
1065
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001066std::string
1067Module::GetSpecificationDescription () const
1068{
1069 std::string spec(GetFileSpec().GetPath());
1070 if (m_object_name)
1071 {
1072 spec += '(';
1073 spec += m_object_name.GetCString();
1074 spec += ')';
1075 }
1076 return spec;
1077}
1078
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001080Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +00001081{
1082 Mutex::Locker locker (m_mutex);
1083
Greg Claytonc982b3d2011-11-28 01:45:00 +00001084 if (level >= eDescriptionLevelFull)
1085 {
1086 if (m_arch.IsValid())
1087 s->Printf("(%s) ", m_arch.GetArchitectureName());
1088 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001089
Greg Claytonc982b3d2011-11-28 01:45:00 +00001090 if (level == eDescriptionLevelBrief)
1091 {
1092 const char *filename = m_file.GetFilename().GetCString();
1093 if (filename)
1094 s->PutCString (filename);
1095 }
1096 else
1097 {
1098 char path[PATH_MAX];
1099 if (m_file.GetPath(path, sizeof(path)))
1100 s->PutCString(path);
1101 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001102
1103 const char *object_name = m_object_name.GetCString();
1104 if (object_name)
1105 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001106}
1107
1108void
Greg Claytonc982b3d2011-11-28 01:45:00 +00001109Module::ReportError (const char *format, ...)
1110{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001111 if (format && format[0])
1112 {
1113 StreamString strm;
1114 strm.PutCString("error: ");
1115 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +00001116 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001117 va_list args;
1118 va_start (args, format);
1119 strm.PrintfVarArg(format, args);
1120 va_end (args);
1121
1122 const int format_len = strlen(format);
1123 if (format_len > 0)
1124 {
1125 const char last_char = format[format_len-1];
1126 if (last_char != '\n' || last_char != '\r')
1127 strm.EOL();
1128 }
1129 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1130
1131 }
1132}
1133
Greg Clayton1d609092012-07-12 22:51:12 +00001134bool
1135Module::FileHasChanged () const
1136{
1137 if (m_file_has_changed == false)
1138 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1139 return m_file_has_changed;
1140}
1141
Greg Claytone38a5ed2012-01-05 03:57:59 +00001142void
1143Module::ReportErrorIfModifyDetected (const char *format, ...)
1144{
Greg Clayton1d609092012-07-12 22:51:12 +00001145 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001146 {
Greg Clayton1d609092012-07-12 22:51:12 +00001147 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +00001148 {
Greg Clayton1d609092012-07-12 22:51:12 +00001149 m_first_file_changed_log = true;
1150 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001151 {
Greg Clayton1d609092012-07-12 22:51:12 +00001152 StreamString strm;
1153 strm.PutCString("error: the object file ");
1154 GetDescription(&strm, lldb::eDescriptionLevelFull);
1155 strm.PutCString (" has been modified\n");
1156
1157 va_list args;
1158 va_start (args, format);
1159 strm.PrintfVarArg(format, args);
1160 va_end (args);
1161
1162 const int format_len = strlen(format);
1163 if (format_len > 0)
1164 {
1165 const char last_char = format[format_len-1];
1166 if (last_char != '\n' || last_char != '\r')
1167 strm.EOL();
1168 }
1169 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1170 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +00001171 }
Greg Claytone38a5ed2012-01-05 03:57:59 +00001172 }
1173 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001174}
1175
1176void
1177Module::ReportWarning (const char *format, ...)
1178{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001179 if (format && format[0])
1180 {
1181 StreamString strm;
1182 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001183 GetDescription(&strm, lldb::eDescriptionLevelFull);
1184 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001185
1186 va_list args;
1187 va_start (args, format);
1188 strm.PrintfVarArg(format, args);
1189 va_end (args);
1190
1191 const int format_len = strlen(format);
1192 if (format_len > 0)
1193 {
1194 const char last_char = format[format_len-1];
1195 if (last_char != '\n' || last_char != '\r')
1196 strm.EOL();
1197 }
1198 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1199 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001200}
1201
1202void
1203Module::LogMessage (Log *log, const char *format, ...)
1204{
1205 if (log)
1206 {
1207 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001208 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001209 log_message.PutCString (": ");
1210 va_list args;
1211 va_start (args, format);
1212 log_message.PrintfVarArg (format, args);
1213 va_end (args);
1214 log->PutCString(log_message.GetString().c_str());
1215 }
1216}
1217
Greg Claytond61c0fc2012-04-23 22:55:20 +00001218void
1219Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1220{
1221 if (log)
1222 {
1223 StreamString log_message;
1224 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1225 log_message.PutCString (": ");
1226 va_list args;
1227 va_start (args, format);
1228 log_message.PrintfVarArg (format, args);
1229 va_end (args);
1230 if (log->GetVerbose())
Zachary Turnera893d302015-03-06 20:45:43 +00001231 {
1232 std::string back_trace;
1233 llvm::raw_string_ostream stream(back_trace);
1234 llvm::sys::PrintStackTrace(stream);
1235 log_message.PutCString(back_trace.c_str());
1236 }
Greg Claytond61c0fc2012-04-23 22:55:20 +00001237 log->PutCString(log_message.GetString().c_str());
1238 }
1239}
1240
Greg Claytonc982b3d2011-11-28 01:45:00 +00001241void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242Module::Dump(Stream *s)
1243{
1244 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001245 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001247 s->Printf("Module %s%s%s%s\n",
1248 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 m_object_name ? "(" : "",
1250 m_object_name ? m_object_name.GetCString() : "",
1251 m_object_name ? ")" : "");
1252
1253 s->IndentMore();
Michael Sartaina7499c92013-07-01 19:45:50 +00001254
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 ObjectFile *objfile = GetObjectFile ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 if (objfile)
1257 objfile->Dump(s);
1258
1259 SymbolVendor *symbols = GetSymbolVendor ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260 if (symbols)
1261 symbols->Dump(s);
1262
1263 s->IndentLess();
1264}
1265
1266
1267TypeList*
1268Module::GetTypeList ()
1269{
1270 SymbolVendor *symbols = GetSymbolVendor ();
1271 if (symbols)
1272 return &symbols->GetTypeList();
1273 return NULL;
1274}
1275
1276const ConstString &
1277Module::GetObjectName() const
1278{
1279 return m_object_name;
1280}
1281
1282ObjectFile *
1283Module::GetObjectFile()
1284{
Greg Clayton88c05f52015-07-24 23:38:01 +00001285 if (m_did_load_objfile.load() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001287 Mutex::Locker locker (m_mutex);
1288 if (m_did_load_objfile.load() == false)
Greg Clayton593577a2011-09-21 03:57:31 +00001289 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001290 Timer scoped_timer(__PRETTY_FUNCTION__,
1291 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
1292 DataBufferSP data_sp;
1293 lldb::offset_t data_offset = 0;
1294 const lldb::offset_t file_size = m_file.GetByteSize();
1295 if (file_size > m_object_offset)
Greg Clayton2540a8a2013-07-12 22:07:46 +00001296 {
Greg Clayton88c05f52015-07-24 23:38:01 +00001297 m_did_load_objfile = true;
1298 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1299 &m_file,
1300 m_object_offset,
1301 file_size - m_object_offset,
1302 data_sp,
1303 data_offset);
1304 if (m_objfile_sp)
1305 {
1306 // Once we get the object file, update our module with the object file's
1307 // architecture since it might differ in vendor/os if some parts were
1308 // unknown. But since the matching arch might already be more specific
1309 // than the generic COFF architecture, only merge in those values that
1310 // overwrite unspecified unknown values.
1311 ArchSpec new_arch;
1312 m_objfile_sp->GetArchitecture(new_arch);
1313 m_arch.MergeFrom(new_arch);
1314 }
1315 else
1316 {
1317 ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str());
1318 }
Todd Fiala0ee56ce2014-09-05 14:48:49 +00001319 }
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{
Greg Clayton902716722015-03-31 21:01:48 +00001467 if (!file.Exists())
1468 return;
Michael Sartaina7499c92013-07-01 19:45:50 +00001469 if (m_symfile_ap)
1470 {
Greg Clayton902716722015-03-31 21:01:48 +00001471 // Remove any sections in the unified section list that come from the current symbol vendor.
Greg Clayton3046e662013-07-10 01:23:25 +00001472 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001473 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1474 if (section_list && symbol_file)
1475 {
1476 ObjectFile *obj_file = symbol_file->GetObjectFile();
Greg Clayton540fbbf2013-08-13 16:46:35 +00001477 // Make sure we have an object file and that the symbol vendor's objfile isn't
1478 // the same as the module's objfile before we remove any sections for it...
Greg Clayton902716722015-03-31 21:01:48 +00001479 if (obj_file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001480 {
Greg Clayton902716722015-03-31 21:01:48 +00001481 // Check to make sure we aren't trying to specify the file we already have
1482 if (obj_file->GetFileSpec() == file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001483 {
Greg Clayton902716722015-03-31 21:01:48 +00001484 // We are being told to add the exact same file that we already have
1485 // we don't have to do anything.
1486 return;
1487 }
Tamas Berghammerd00438e2015-07-30 12:38:18 +00001488
1489 // Cleare the current symtab as we are going to replace it with a new one
1490 obj_file->ClearSymtab();
Greg Clayton902716722015-03-31 21:01:48 +00001491
1492 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead
1493 // of a full path to the symbol file within the bundle
1494 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this
1495
1496 if (file.IsDirectory())
1497 {
1498 std::string new_path(file.GetPath());
1499 std::string old_path(obj_file->GetFileSpec().GetPath());
1500 if (old_path.find(new_path) == 0)
Michael Sartaina7499c92013-07-01 19:45:50 +00001501 {
Greg Clayton902716722015-03-31 21:01:48 +00001502 // We specified the same bundle as the symbol file that we already have
1503 return;
1504 }
1505 }
1506
1507 if (obj_file != m_objfile_sp.get())
1508 {
1509 size_t num_sections = section_list->GetNumSections (0);
1510 for (size_t idx = num_sections; idx > 0; --idx)
1511 {
1512 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1513 if (section_sp->GetObjectFile() == obj_file)
1514 {
1515 section_list->DeleteSection (idx - 1);
1516 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001517 }
1518 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001519 }
1520 }
Greg Clayton902716722015-03-31 21:01:48 +00001521 // Keep all old symbol files around in case there are any lingering type references in
1522 // any SBValue objects that might have been handed out.
1523 m_old_symfiles.push_back(std::move(m_symfile_ap));
Michael Sartaina7499c92013-07-01 19:45:50 +00001524 }
Greg Claytone01e07b2013-04-18 18:10:51 +00001525 m_symfile_spec = file;
1526 m_symfile_ap.reset();
1527 m_did_load_symbol_vendor = false;
1528}
1529
Jim Ingham5aee1622010-08-09 23:31:02 +00001530bool
1531Module::IsExecutable ()
1532{
1533 if (GetObjectFile() == NULL)
1534 return false;
1535 else
1536 return GetObjectFile()->IsExecutable();
1537}
1538
Jim Inghamb53cb272011-08-03 01:03:17 +00001539bool
1540Module::IsLoadedInTarget (Target *target)
1541{
1542 ObjectFile *obj_file = GetObjectFile();
1543 if (obj_file)
1544 {
Greg Clayton3046e662013-07-10 01:23:25 +00001545 SectionList *sections = GetSectionList();
Jim Inghamb53cb272011-08-03 01:03:17 +00001546 if (sections != NULL)
1547 {
1548 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001549 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1550 {
1551 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1552 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1553 {
1554 return true;
1555 }
1556 }
1557 }
1558 }
1559 return false;
1560}
Enrico Granata17598482012-11-08 02:22:02 +00001561
1562bool
Enrico Granata97303392013-05-21 00:00:30 +00001563Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001564{
1565 if (!target)
1566 {
1567 error.SetErrorString("invalid destination Target");
1568 return false;
1569 }
1570
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001571 LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001572
Greg Clayton994740f2014-08-18 21:08:44 +00001573 if (should_load == eLoadScriptFromSymFileFalse)
1574 return false;
1575
Greg Clayton91c0e742013-01-11 23:44:27 +00001576 Debugger &debugger = target->GetDebugger();
1577 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1578 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001579 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001580
1581 PlatformSP platform_sp(target->GetPlatform());
1582
1583 if (!platform_sp)
1584 {
1585 error.SetErrorString("invalid Platform");
1586 return false;
1587 }
Enrico Granata17598482012-11-08 02:22:02 +00001588
Greg Claytonb9d88902013-03-23 00:50:58 +00001589 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
Enrico Granatafe7295d2014-08-16 00:32:58 +00001590 *this,
1591 feedback_stream);
Greg Claytonb9d88902013-03-23 00:50:58 +00001592
1593
1594 const uint32_t num_specs = file_specs.GetSize();
1595 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001596 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001597 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1598 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001599 {
1600 for (uint32_t i=0; i<num_specs; ++i)
1601 {
1602 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1603 if (scripting_fspec && scripting_fspec.Exists())
1604 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001605 if (should_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001606 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001607 if (feedback_stream)
Jim Inghamd516deb2013-07-01 18:49:43 +00001608 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1609 "this debug session:\n\n command script import \"%s\"\n\n"
1610 "To run all discovered debug scripts in this session:\n\n"
1611 " settings set target.load-script-from-symbol-file true\n",
1612 GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1613 scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001614 return false;
1615 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001616 StreamString scripting_stream;
1617 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001618 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001619 const bool init_lldb_globals = false;
Jim Inghamd516deb2013-07-01 18:49:43 +00001620 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1621 can_reload,
1622 init_lldb_globals,
1623 error);
Greg Clayton91c0e742013-01-11 23:44:27 +00001624 if (!did_load)
1625 return false;
1626 }
1627 }
1628 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001629 else
1630 {
1631 error.SetErrorString("invalid ScriptInterpreter");
1632 return false;
1633 }
Enrico Granata17598482012-11-08 02:22:02 +00001634 }
1635 }
1636 return true;
1637}
1638
1639bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001640Module::SetArchitecture (const ArchSpec &new_arch)
1641{
Greg Clayton64195a22011-02-23 00:35:02 +00001642 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001643 {
1644 m_arch = new_arch;
1645 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001646 }
Chaoren Linb6cd5fe2015-02-26 22:15:16 +00001647 return m_arch.IsCompatibleMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001648}
1649
Greg Claytonc9660542012-02-05 02:38:54 +00001650bool
Greg Clayton751caf62014-02-07 22:54:47 +00001651Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
Greg Claytonc9660542012-02-05 02:38:54 +00001652{
Steve Pucci9e02dac2014-02-06 19:02:19 +00001653 ObjectFile *object_file = GetObjectFile();
1654 if (object_file)
Greg Claytonc9660542012-02-05 02:38:54 +00001655 {
Greg Clayton751caf62014-02-07 22:54:47 +00001656 changed = object_file->SetLoadAddress(target, value, value_is_offset);
Greg Clayton7524e092014-02-06 20:10:16 +00001657 return true;
1658 }
1659 else
1660 {
1661 changed = false;
Greg Claytonc9660542012-02-05 02:38:54 +00001662 }
Steve Pucci9e02dac2014-02-06 19:02:19 +00001663 return false;
Greg Claytonc9660542012-02-05 02:38:54 +00001664}
1665
Greg Claytonb9a01b32012-02-26 05:51:37 +00001666
1667bool
1668Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1669{
1670 const UUID &uuid = module_ref.GetUUID();
1671
1672 if (uuid.IsValid())
1673 {
1674 // If the UUID matches, then nothing more needs to match...
1675 if (uuid == GetUUID())
1676 return true;
1677 else
1678 return false;
1679 }
1680
1681 const FileSpec &file_spec = module_ref.GetFileSpec();
1682 if (file_spec)
1683 {
Tamas Berghammer980662e2015-09-04 12:42:41 +00001684 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1685 !FileSpec::Equal (file_spec, m_platform_file, (bool)file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001686 return false;
1687 }
1688
1689 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1690 if (platform_file_spec)
1691 {
Sean Callananddd7a2a2013-10-03 22:27:29 +00001692 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001693 return false;
1694 }
1695
1696 const ArchSpec &arch = module_ref.GetArchitecture();
1697 if (arch.IsValid())
1698 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001699 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001700 return false;
1701 }
1702
1703 const ConstString &object_name = module_ref.GetObjectName();
1704 if (object_name)
1705 {
1706 if (object_name != GetObjectName())
1707 return false;
1708 }
1709 return true;
1710}
1711
Greg Claytond804d282012-03-15 21:01:31 +00001712bool
1713Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1714{
1715 Mutex::Locker locker (m_mutex);
1716 return m_source_mappings.FindFile (orig_spec, new_spec);
1717}
1718
Greg Claytonf9be6932012-03-19 22:22:41 +00001719bool
1720Module::RemapSourceFile (const char *path, std::string &new_path) const
1721{
1722 Mutex::Locker locker (m_mutex);
1723 return m_source_mappings.RemapPath(path, new_path);
1724}
1725
Enrico Granata3467d802012-09-04 18:47:54 +00001726uint32_t
1727Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1728{
1729 ObjectFile *obj_file = GetObjectFile();
1730 if (obj_file)
1731 return obj_file->GetVersion (versions, num_versions);
1732
1733 if (versions && num_versions)
1734 {
1735 for (uint32_t i=0; i<num_versions; ++i)
Enrico Granataafcbdb12014-03-25 20:53:33 +00001736 versions[i] = LLDB_INVALID_MODULE_VERSION;
Enrico Granata3467d802012-09-04 18:47:54 +00001737 }
1738 return 0;
1739}
Greg Clayton43fe2172013-04-03 02:00:15 +00001740
1741void
1742Module::PrepareForFunctionNameLookup (const ConstString &name,
1743 uint32_t name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001744 LanguageType language,
Greg Clayton43fe2172013-04-03 02:00:15 +00001745 ConstString &lookup_name,
1746 uint32_t &lookup_name_type_mask,
1747 bool &match_name_after_lookup)
1748{
1749 const char *name_cstr = name.GetCString();
1750 lookup_name_type_mask = eFunctionNameTypeNone;
1751 match_name_after_lookup = false;
Jim Inghamfa39bb42014-10-25 00:33:55 +00001752
1753 llvm::StringRef basename;
1754 llvm::StringRef context;
Greg Clayton43fe2172013-04-03 02:00:15 +00001755
1756 if (name_type_mask & eFunctionNameTypeAuto)
1757 {
Jim Inghamaa816b82015-09-02 01:59:14 +00001758 if (CPlusPlusLanguage::IsCPPMangledName (name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001759 lookup_name_type_mask = eFunctionNameTypeFull;
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001760 else if ((language == eLanguageTypeUnknown ||
Jim Ingham0e0984e2015-09-02 01:06:46 +00001761 Language::LanguageIsObjC(language)) &&
Jim Inghamaa816b82015-09-02 01:59:14 +00001762 ObjCLanguage::IsPossibleObjCMethodName (name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001763 lookup_name_type_mask = eFunctionNameTypeFull;
Jim Ingham0e0984e2015-09-02 01:06:46 +00001764 else if (Language::LanguageIsC(language))
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001765 {
1766 lookup_name_type_mask = eFunctionNameTypeFull;
1767 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001768 else
1769 {
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001770 if ((language == eLanguageTypeUnknown ||
Jim Ingham0e0984e2015-09-02 01:06:46 +00001771 Language::LanguageIsObjC(language)) &&
Jim Inghamaa816b82015-09-02 01:59:14 +00001772 ObjCLanguage::IsPossibleObjCSelector(name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001773 lookup_name_type_mask |= eFunctionNameTypeSelector;
1774
Jim Inghamaa816b82015-09-02 01:59:14 +00001775 CPlusPlusLanguage::MethodName cpp_method (name);
Jim Inghamfa39bb42014-10-25 00:33:55 +00001776 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001777 if (basename.empty())
1778 {
Jim Inghamaa816b82015-09-02 01:59:14 +00001779 if (CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename))
Greg Clayton6ecb2322013-05-18 00:11:21 +00001780 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Claytonc3795402014-10-22 21:47:13 +00001781 else
Greg Clayton65b0e762015-01-28 01:08:39 +00001782 lookup_name_type_mask |= eFunctionNameTypeFull;
Greg Clayton6ecb2322013-05-18 00:11:21 +00001783 }
1784 else
1785 {
Greg Clayton43fe2172013-04-03 02:00:15 +00001786 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001787 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001788 }
1789 }
1790 else
1791 {
1792 lookup_name_type_mask = name_type_mask;
1793 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1794 {
1795 // If they've asked for a CPP method or function name and it can't be that, we don't
1796 // even need to search for CPP methods or names.
Jim Inghamaa816b82015-09-02 01:59:14 +00001797 CPlusPlusLanguage::MethodName cpp_method (name);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001798 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001799 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001800 basename = cpp_method.GetBasename();
Greg Clayton6ecb2322013-05-18 00:11:21 +00001801
1802 if (!cpp_method.GetQualifiers().empty())
1803 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001804 // There is a "const" or other qualifier following the end of the function parens,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001805 // this can't be a eFunctionNameTypeBase
1806 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1807 if (lookup_name_type_mask == eFunctionNameTypeNone)
1808 return;
1809 }
1810 }
1811 else
1812 {
Jim Inghamfa39bb42014-10-25 00:33:55 +00001813 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can.
1814 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later.
Jim Inghamaa816b82015-09-02 01:59:14 +00001815 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001816 }
1817 }
1818
1819 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1820 {
Jim Inghamaa816b82015-09-02 01:59:14 +00001821 if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr))
Greg Clayton43fe2172013-04-03 02:00:15 +00001822 {
1823 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1824 if (lookup_name_type_mask == eFunctionNameTypeNone)
1825 return;
1826 }
1827 }
1828 }
1829
Jim Inghamfa39bb42014-10-25 00:33:55 +00001830 if (!basename.empty())
Greg Clayton43fe2172013-04-03 02:00:15 +00001831 {
1832 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1833 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1834 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1835 // to true
Jim Inghamfa39bb42014-10-25 00:33:55 +00001836 lookup_name.SetString(basename);
Greg Clayton43fe2172013-04-03 02:00:15 +00001837 match_name_after_lookup = true;
1838 }
1839 else
1840 {
1841 // The name is already correct, just use the exact name as supplied, and we won't need
1842 // to check if any matches contain "name"
1843 lookup_name = name;
1844 match_name_after_lookup = false;
1845 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001846}
Greg Clayton23f8c952014-03-24 23:10:19 +00001847
1848ModuleSP
1849Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
1850{
1851 if (delegate_sp)
1852 {
1853 // Must create a module and place it into a shared pointer before
1854 // we can create an object file since it has a std::weak_ptr back
1855 // to the module, so we need to control the creation carefully in
1856 // this static function
1857 ModuleSP module_sp(new Module());
1858 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
1859 if (module_sp->m_objfile_sp)
1860 {
1861 // Once we get the object file, update our module with the object file's
1862 // architecture since it might differ in vendor/os if some parts were
1863 // unknown.
1864 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
1865 }
1866 return module_sp;
1867 }
1868 return ModuleSP();
1869}
1870
Greg Clayton08928f32015-02-05 02:01:34 +00001871bool
1872Module::GetIsDynamicLinkEditor()
1873{
1874 ObjectFile * obj_file = GetObjectFile ();
1875
1876 if (obj_file)
1877 return obj_file->GetIsDynamicLinkEditor();
1878
1879 return false;
1880}