blob: 205d2d9fb22d903bb7e0323ae786e85bfd3963d0 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Enrico Granata17598482012-11-08 02:22:02 +000012#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Module.h"
Greg Claytonc9660542012-02-05 02:38:54 +000014#include "lldb/Core/DataBuffer.h"
15#include "lldb/Core/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/ModuleList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/lldb-private-log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000028#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Symbol/SymbolContext.h"
31#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton43fe2172013-04-03 02:00:15 +000032#include "lldb/Target/CPPLanguageRuntime.h"
33#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytonc9660542012-02-05 02:38:54 +000034#include "lldb/Target/Process.h"
35#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Greg Clayton65a03992011-08-09 00:01:09 +000040// Shared pointers to modules track module lifetimes in
41// targets and in the global module, but this collection
42// will track all module objects that are still alive
43typedef std::vector<Module *> ModuleCollection;
44
45static ModuleCollection &
46GetModuleCollection()
47{
Jim Ingham549f7372011-10-31 23:47:10 +000048 // This module collection needs to live past any module, so we could either make it a
49 // shared pointer in each module or just leak is. Since it is only an empty vector by
50 // the time all the modules have gone away, we just leak it for now. If we decide this
51 // is a big problem we can introduce a Finalize method that will tear everything down in
52 // a predictable order.
53
54 static ModuleCollection *g_module_collection = NULL;
55 if (g_module_collection == NULL)
56 g_module_collection = new ModuleCollection();
57
58 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000059}
60
Greg Claytonb26e6be2012-01-27 18:08:35 +000061Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000062Module::GetAllocationModuleCollectionMutex()
63{
Greg Claytonb26e6be2012-01-27 18:08:35 +000064 // NOTE: The mutex below must be leaked since the global module list in
65 // the ModuleList class will get torn at some point, and we can't know
66 // if it will tear itself down before the "g_module_collection_mutex" below
67 // will. So we leak a Mutex object below to safeguard against that
68
69 static Mutex *g_module_collection_mutex = NULL;
70 if (g_module_collection_mutex == NULL)
71 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
72 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000073}
74
75size_t
76Module::GetNumberAllocatedModules ()
77{
78 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
79 return GetModuleCollection().size();
80}
81
82Module *
83Module::GetAllocatedModuleAtIndex (size_t idx)
84{
85 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
86 ModuleCollection &modules = GetModuleCollection();
87 if (idx < modules.size())
88 return modules[idx];
89 return NULL;
90}
Greg Clayton29ad7b92012-01-27 18:45:39 +000091#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000092
Greg Clayton29ad7b92012-01-27 18:45:39 +000093// These functions help us to determine if modules are still loaded, yet don't require that
94// you have a command interpreter and can easily be called from an external debugger.
95namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +000096
Greg Clayton29ad7b92012-01-27 18:45:39 +000097 void
98 ClearModuleInfo (void)
99 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000100 const bool mandatory = true;
101 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000102 }
103
104 void
105 DumpModuleInfo (void)
106 {
107 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
108 ModuleCollection &modules = GetModuleCollection();
109 const size_t count = modules.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000110 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
Greg Clayton29ad7b92012-01-27 18:45:39 +0000111 for (size_t i=0; i<count; ++i)
112 {
113
114 StreamString strm;
115 Module *module = modules[i];
116 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
117 module->GetDescription(&strm, eDescriptionLevelFull);
118 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
119 module,
120 in_shared_module_list,
121 (uint32_t)module->use_count(),
122 strm.GetString().c_str());
123 }
124 }
125}
126
127#endif
Greg Clayton3a18e312012-10-08 22:41:53 +0000128
Greg Claytonb9a01b32012-02-26 05:51:37 +0000129Module::Module (const ModuleSpec &module_spec) :
130 m_mutex (Mutex::eMutexTypeRecursive),
131 m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
132 m_arch (module_spec.GetArchitecture()),
133 m_uuid (),
134 m_file (module_spec.GetFileSpec()),
135 m_platform_file(module_spec.GetPlatformFileSpec()),
136 m_symfile_spec (module_spec.GetSymbolFileSpec()),
137 m_object_name (module_spec.GetObjectName()),
138 m_object_offset (module_spec.GetObjectOffset()),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000139 m_object_mod_time (module_spec.GetObjectModificationTime()),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000140 m_objfile_sp (),
141 m_symfile_ap (),
142 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000143 m_source_mappings (),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000144 m_did_load_objfile (false),
145 m_did_load_symbol_vendor (false),
146 m_did_parse_uuid (false),
147 m_did_init_ast (false),
148 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000149 m_file_has_changed (false),
150 m_first_file_changed_log (false)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000151{
152 // Scope for locker below...
153 {
154 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
155 GetModuleCollection().push_back(this);
156 }
157
Greg Clayton5160ce52013-03-27 23:08:40 +0000158 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Greg Claytonb9a01b32012-02-26 05:51:37 +0000159 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000160 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Greg Claytonb9a01b32012-02-26 05:51:37 +0000161 this,
162 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000163 m_file.GetPath().c_str(),
Greg Claytonb9a01b32012-02-26 05:51:37 +0000164 m_object_name.IsEmpty() ? "" : "(",
165 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
166 m_object_name.IsEmpty() ? "" : ")");
167}
168
Greg Claytone72dfb32012-02-24 01:59:29 +0000169Module::Module(const FileSpec& file_spec,
170 const ArchSpec& arch,
171 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000172 off_t object_offset,
173 const TimeValue *object_mod_time_ptr) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 m_mutex (Mutex::eMutexTypeRecursive),
175 m_mod_time (file_spec.GetModificationTime()),
176 m_arch (arch),
177 m_uuid (),
178 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000179 m_platform_file(),
Greg Claytone72dfb32012-02-24 01:59:29 +0000180 m_symfile_spec (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000182 m_object_offset (object_offset),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000183 m_object_mod_time (),
Greg Clayton762f7132011-09-18 18:59:15 +0000184 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000185 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000186 m_ast (),
Greg Claytond804d282012-03-15 21:01:31 +0000187 m_source_mappings (),
Greg Claytone83e7312010-09-07 23:40:05 +0000188 m_did_load_objfile (false),
189 m_did_load_symbol_vendor (false),
190 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000191 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000192 m_is_dynamic_loader_module (false),
Greg Clayton1d609092012-07-12 22:51:12 +0000193 m_file_has_changed (false),
194 m_first_file_changed_log (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195{
Greg Clayton65a03992011-08-09 00:01:09 +0000196 // Scope for locker below...
197 {
198 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
199 GetModuleCollection().push_back(this);
200 }
201
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 if (object_name)
203 m_object_name = *object_name;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000204
205 if (object_mod_time_ptr)
206 m_object_mod_time = *object_mod_time_ptr;
207
Greg Clayton5160ce52013-03-27 23:08:40 +0000208 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000210 log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000212 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000213 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214 m_object_name.IsEmpty() ? "" : "(",
215 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
216 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
219Module::~Module()
220{
Greg Clayton217b28b2013-05-22 20:13:22 +0000221 // Lock our module down while we tear everything down to make sure
222 // we don't get any access to the module while it is being destroyed
223 Mutex::Locker locker (m_mutex);
Greg Clayton65a03992011-08-09 00:01:09 +0000224 // Scope for locker below...
225 {
226 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
227 ModuleCollection &modules = GetModuleCollection();
228 ModuleCollection::iterator end = modules.end();
229 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
Greg Clayton3a18e312012-10-08 22:41:53 +0000230 assert (pos != end);
231 modules.erase(pos);
Greg Clayton65a03992011-08-09 00:01:09 +0000232 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000233 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000235 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000237 m_arch.GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000238 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 m_object_name.IsEmpty() ? "" : "(",
240 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
241 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000242 // Release any auto pointers before we start tearing down our member
243 // variables since the object file and symbol files might need to make
244 // function calls back into this module object. The ordering is important
245 // here because symbol files can require the module object file. So we tear
246 // down the symbol file first, then the object file.
247 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000248 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249}
250
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000251ObjectFile *
252Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
253{
254 if (m_objfile_sp)
255 {
256 error.SetErrorString ("object file already exists");
257 }
258 else
259 {
260 Mutex::Locker locker (m_mutex);
261 if (process_sp)
262 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000263 m_did_load_objfile = true;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000264 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000265 Error readmem_error;
266 const size_t bytes_read = process_sp->ReadMemory (header_addr,
267 data_ap->GetBytes(),
268 data_ap->GetByteSize(),
269 readmem_error);
270 if (bytes_read == 512)
271 {
272 DataBufferSP data_sp(data_ap.release());
273 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
274 if (m_objfile_sp)
275 {
Greg Clayton3e10cf32012-04-20 19:50:20 +0000276 StreamString s;
Daniel Malead01b2952012-11-29 21:49:15 +0000277 s.Printf("0x%16.16" PRIx64, header_addr);
Greg Clayton3e10cf32012-04-20 19:50:20 +0000278 m_object_name.SetCString (s.GetData());
279
280 // Once we get the object file, update our module with the object file's
Greg Claytonc7f09cc2012-02-24 21:55:59 +0000281 // architecture since it might differ in vendor/os if some parts were
282 // unknown.
283 m_objfile_sp->GetArchitecture (m_arch);
284 }
285 else
286 {
287 error.SetErrorString ("unable to find suitable object file plug-in");
288 }
289 }
290 else
291 {
292 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
293 }
294 }
295 else
296 {
297 error.SetErrorString ("invalid process");
298 }
299 }
300 return m_objfile_sp.get();
301}
302
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303
Greg Clayton60830262011-02-04 18:53:10 +0000304const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305Module::GetUUID()
306{
307 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000308 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 {
310 ObjectFile * obj_file = GetObjectFile ();
311
312 if (obj_file != NULL)
313 {
314 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000315 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 }
317 }
318 return m_uuid;
319}
320
Greg Clayton6beaaa62011-01-17 03:46:26 +0000321ClangASTContext &
322Module::GetClangASTContext ()
323{
324 Mutex::Locker locker (m_mutex);
325 if (m_did_init_ast == false)
326 {
327 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000328 ArchSpec object_arch;
329 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000330 {
331 m_did_init_ast = true;
Jason Molenda981d4df2012-10-16 20:45:49 +0000332
333 // LLVM wants this to be set to iOS or MacOSX; if we're working on
334 // a bare-boards type image, change the triple for llvm's benefit.
335 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
336 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
337 {
338 if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
339 object_arch.GetTriple().getArch() == llvm::Triple::thumb)
340 {
341 object_arch.GetTriple().setOS(llvm::Triple::IOS);
342 }
343 else
344 {
345 object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
346 }
347 }
Greg Clayton514487e2011-02-15 21:59:32 +0000348 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000349 }
350 }
351 return m_ast;
352}
353
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354void
355Module::ParseAllDebugSymbols()
356{
357 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000358 size_t num_comp_units = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 if (num_comp_units == 0)
360 return;
361
Greg Claytona2eee182011-09-17 07:23:18 +0000362 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000363 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 SymbolVendor *symbols = GetSymbolVendor ();
365
Greg Claytonc7bece562013-01-25 18:06:21 +0000366 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 {
368 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
369 if (sc.comp_unit)
370 {
371 sc.function = NULL;
372 symbols->ParseVariablesForContext(sc);
373
374 symbols->ParseCompileUnitFunctions(sc);
375
Greg Claytonc7bece562013-01-25 18:06:21 +0000376 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 +0000377 {
378 symbols->ParseFunctionBlocks(sc);
379
380 // Parse the variables for this function and all its blocks
381 symbols->ParseVariablesForContext(sc);
382 }
383
384
385 // Parse all types for this compile unit
386 sc.function = NULL;
387 symbols->ParseTypes(sc);
388 }
389 }
390}
391
392void
393Module::CalculateSymbolContext(SymbolContext* sc)
394{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000395 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
Greg Claytone72dfb32012-02-24 01:59:29 +0000398ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000399Module::CalculateSymbolContextModule ()
400{
Greg Claytone72dfb32012-02-24 01:59:29 +0000401 return shared_from_this();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000402}
403
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404void
405Module::DumpSymbolContext(Stream *s)
406{
Jason Molendafd54b362011-09-20 21:44:10 +0000407 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408}
409
Greg Claytonc7bece562013-01-25 18:06:21 +0000410size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411Module::GetNumCompileUnits()
412{
413 Mutex::Locker locker (m_mutex);
414 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
415 SymbolVendor *symbols = GetSymbolVendor ();
416 if (symbols)
417 return symbols->GetNumCompileUnits();
418 return 0;
419}
420
421CompUnitSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000422Module::GetCompileUnitAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423{
424 Mutex::Locker locker (m_mutex);
Greg Claytonc7bece562013-01-25 18:06:21 +0000425 size_t num_comp_units = GetNumCompileUnits ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 CompUnitSP cu_sp;
427
428 if (index < num_comp_units)
429 {
430 SymbolVendor *symbols = GetSymbolVendor ();
431 if (symbols)
432 cu_sp = symbols->GetCompileUnitAtIndex(index);
433 }
434 return cu_sp;
435}
436
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437bool
438Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
439{
440 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000441 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 ObjectFile* ofile = GetObjectFile();
443 if (ofile)
444 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
445 return false;
446}
447
448uint32_t
449Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
450{
451 Mutex::Locker locker (m_mutex);
452 uint32_t resolved_flags = 0;
453
Greg Clayton72310352013-02-23 04:12:47 +0000454 // Clear the result symbol context in case we don't find anything, but don't clear the target
455 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456
457 // Get the section from the section/offset address.
Greg Claytone72dfb32012-02-24 01:59:29 +0000458 SectionSP section_sp (so_addr.GetSection());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459
460 // Make sure the section matches this module before we try and match anything
Greg Claytone72dfb32012-02-24 01:59:29 +0000461 if (section_sp && section_sp->GetModule().get() == this)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 {
463 // If the section offset based address resolved itself, then this
464 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000465 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 resolved_flags |= eSymbolContextModule;
467
468 // Resolve the compile unit, function, block, line table or line
469 // entry if requested.
470 if (resolve_scope & eSymbolContextCompUnit ||
471 resolve_scope & eSymbolContextFunction ||
472 resolve_scope & eSymbolContextBlock ||
473 resolve_scope & eSymbolContextLineEntry )
474 {
475 SymbolVendor *symbols = GetSymbolVendor ();
476 if (symbols)
477 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
478 }
479
Jim Ingham680e1772010-08-31 23:51:36 +0000480 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
481 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 {
483 ObjectFile* ofile = GetObjectFile();
484 if (ofile)
485 {
486 Symtab *symtab = ofile->GetSymtab();
487 if (symtab)
488 {
489 if (so_addr.IsSectionOffset())
490 {
491 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
492 if (sc.symbol)
493 resolved_flags |= eSymbolContextSymbol;
494 }
495 }
496 }
497 }
498 }
499 return resolved_flags;
500}
501
502uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000503Module::ResolveSymbolContextForFilePath
504(
505 const char *file_path,
506 uint32_t line,
507 bool check_inlines,
508 uint32_t resolve_scope,
509 SymbolContextList& sc_list
510)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511{
Greg Clayton274060b2010-10-20 20:54:39 +0000512 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
514}
515
516uint32_t
517Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
518{
519 Mutex::Locker locker (m_mutex);
520 Timer scoped_timer(__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000521 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
522 file_spec.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 line,
524 check_inlines ? "yes" : "no",
525 resolve_scope);
526
527 const uint32_t initial_count = sc_list.GetSize();
528
529 SymbolVendor *symbols = GetSymbolVendor ();
530 if (symbols)
531 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
532
533 return sc_list.GetSize() - initial_count;
534}
535
536
Greg Claytonc7bece562013-01-25 18:06:21 +0000537size_t
538Module::FindGlobalVariables (const ConstString &name,
539 const ClangNamespaceDecl *namespace_decl,
540 bool append,
541 size_t max_matches,
542 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543{
544 SymbolVendor *symbols = GetSymbolVendor ();
545 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000546 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 return 0;
548}
Greg Claytonc7bece562013-01-25 18:06:21 +0000549
550size_t
551Module::FindGlobalVariables (const RegularExpression& regex,
552 bool append,
553 size_t max_matches,
554 VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555{
556 SymbolVendor *symbols = GetSymbolVendor ();
557 if (symbols)
558 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
559 return 0;
560}
561
Greg Claytonc7bece562013-01-25 18:06:21 +0000562size_t
Greg Clayton644247c2011-07-07 01:59:51 +0000563Module::FindCompileUnits (const FileSpec &path,
564 bool append,
565 SymbolContextList &sc_list)
566{
567 if (!append)
568 sc_list.Clear();
569
Greg Claytonc7bece562013-01-25 18:06:21 +0000570 const size_t start_size = sc_list.GetSize();
571 const size_t num_compile_units = GetNumCompileUnits();
Greg Clayton644247c2011-07-07 01:59:51 +0000572 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000573 sc.module_sp = shared_from_this();
Greg Clayton644247c2011-07-07 01:59:51 +0000574 const bool compare_directory = path.GetDirectory();
Greg Claytonc7bece562013-01-25 18:06:21 +0000575 for (size_t i=0; i<num_compile_units; ++i)
Greg Clayton644247c2011-07-07 01:59:51 +0000576 {
577 sc.comp_unit = GetCompileUnitAtIndex(i).get();
Greg Clayton2dafd8e2012-04-23 22:00:21 +0000578 if (sc.comp_unit)
579 {
580 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
581 sc_list.Append(sc);
582 }
Greg Clayton644247c2011-07-07 01:59:51 +0000583 }
584 return sc_list.GetSize() - start_size;
585}
586
Greg Claytonc7bece562013-01-25 18:06:21 +0000587size_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000588Module::FindFunctions (const ConstString &name,
589 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000590 uint32_t name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000591 bool include_symbols,
592 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000593 bool append,
594 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595{
Greg Clayton931180e2011-01-27 06:44:37 +0000596 if (!append)
597 sc_list.Clear();
598
Greg Clayton43fe2172013-04-03 02:00:15 +0000599 const size_t old_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000600
601 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 SymbolVendor *symbols = GetSymbolVendor ();
Greg Clayton43fe2172013-04-03 02:00:15 +0000603
604 if (name_type_mask & eFunctionNameTypeAuto)
Greg Clayton931180e2011-01-27 06:44:37 +0000605 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000606 ConstString lookup_name;
607 uint32_t lookup_name_type_mask = 0;
608 bool match_name_after_lookup = false;
609 Module::PrepareForFunctionNameLookup (name,
610 name_type_mask,
611 lookup_name,
612 lookup_name_type_mask,
613 match_name_after_lookup);
614
615 if (symbols)
616 symbols->FindFunctions(lookup_name,
617 namespace_decl,
618 lookup_name_type_mask,
619 include_inlines,
620 append,
621 sc_list);
622
623 // Now check our symbol table for symbols that are code symbols if requested
624 if (include_symbols)
Greg Clayton931180e2011-01-27 06:44:37 +0000625 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000626 ObjectFile *objfile = GetObjectFile();
627 if (objfile)
Greg Clayton931180e2011-01-27 06:44:37 +0000628 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000629 Symtab *symtab = objfile->GetSymtab();
630 if (symtab)
631 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
632 }
633 }
634
635 if (match_name_after_lookup)
636 {
637 SymbolContext sc;
638 size_t i = old_size;
639 while (i<sc_list.GetSize())
640 {
641 if (sc_list.GetContextAtIndex(i, sc))
Greg Clayton931180e2011-01-27 06:44:37 +0000642 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000643 const char *func_name = sc.GetFunctionName().GetCString();
644 if (func_name && strstr (func_name, name.GetCString()) == NULL)
Greg Clayton931180e2011-01-27 06:44:37 +0000645 {
Greg Clayton43fe2172013-04-03 02:00:15 +0000646 // Remove the current context
647 sc_list.RemoveContextAtIndex(i);
648 // Don't increment i and continue in the loop
649 continue;
Greg Clayton931180e2011-01-27 06:44:37 +0000650 }
651 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000652 ++i;
653 }
654 }
655
656 }
657 else
658 {
659 if (symbols)
660 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
661
662 // Now check our symbol table for symbols that are code symbols if requested
663 if (include_symbols)
664 {
665 ObjectFile *objfile = GetObjectFile();
666 if (objfile)
667 {
668 Symtab *symtab = objfile->GetSymtab();
669 if (symtab)
670 symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000671 }
672 }
673 }
Greg Clayton43fe2172013-04-03 02:00:15 +0000674
675 return sc_list.GetSize() - old_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676}
677
Greg Claytonc7bece562013-01-25 18:06:21 +0000678size_t
Greg Clayton931180e2011-01-27 06:44:37 +0000679Module::FindFunctions (const RegularExpression& regex,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000680 bool include_symbols,
681 bool include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000682 bool append,
683 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684{
Greg Clayton931180e2011-01-27 06:44:37 +0000685 if (!append)
686 sc_list.Clear();
687
Greg Claytonc7bece562013-01-25 18:06:21 +0000688 const size_t start_size = sc_list.GetSize();
Greg Clayton931180e2011-01-27 06:44:37 +0000689
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690 SymbolVendor *symbols = GetSymbolVendor ();
691 if (symbols)
Sean Callanan9df05fb2012-02-10 22:52:19 +0000692 symbols->FindFunctions(regex, include_inlines, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000693 // Now check our symbol table for symbols that are code symbols if requested
694 if (include_symbols)
695 {
696 ObjectFile *objfile = GetObjectFile();
697 if (objfile)
698 {
699 Symtab *symtab = objfile->GetSymtab();
700 if (symtab)
701 {
702 std::vector<uint32_t> symbol_indexes;
Matt Kopec00049b82013-02-27 20:13:38 +0000703 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Greg Claytonc7bece562013-01-25 18:06:21 +0000704 const size_t num_matches = symbol_indexes.size();
Greg Clayton931180e2011-01-27 06:44:37 +0000705 if (num_matches)
706 {
707 SymbolContext sc(this);
Greg Claytond8cf1a12013-06-12 00:46:38 +0000708 const size_t end_functions_added_index = sc_list.GetSize();
709 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
710 if (num_functions_added_to_sc_list == 0)
Greg Clayton931180e2011-01-27 06:44:37 +0000711 {
Greg Claytond8cf1a12013-06-12 00:46:38 +0000712 // No functions were added, just symbols, so we can just append them
713 for (size_t i=0; i<num_matches; ++i)
714 {
715 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
716 SymbolType sym_type = sc.symbol->GetType();
717 if (sc.symbol && (sym_type == eSymbolTypeCode ||
718 sym_type == eSymbolTypeResolver))
719 sc_list.Append(sc);
720 }
721 }
722 else
723 {
724 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
725 FileAddrToIndexMap file_addr_to_index;
726 for (size_t i=start_size; i<end_functions_added_index; ++i)
727 {
728 const SymbolContext &sc = sc_list[i];
729 if (sc.block)
730 continue;
731 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
732 }
733
734 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
735 // Functions were added so we need to merge symbols into any
736 // existing function symbol contexts
737 for (size_t i=start_size; i<num_matches; ++i)
738 {
739 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
740 SymbolType sym_type = sc.symbol->GetType();
741 if (sc.symbol && (sym_type == eSymbolTypeCode ||
742 sym_type == eSymbolTypeResolver))
743 {
744 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
745 if (pos == end)
746 sc_list.Append(sc);
747 else
748 sc_list[pos->second].symbol = sc.symbol;
749 }
750 }
Greg Clayton931180e2011-01-27 06:44:37 +0000751 }
752 }
753 }
754 }
755 }
756 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757}
758
Greg Claytonc7bece562013-01-25 18:06:21 +0000759size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000760Module::FindTypes_Impl (const SymbolContext& sc,
761 const ConstString &name,
762 const ClangNamespaceDecl *namespace_decl,
763 bool append,
Greg Claytonc7bece562013-01-25 18:06:21 +0000764 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000765 TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000766{
767 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
768 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
769 {
770 SymbolVendor *symbols = GetSymbolVendor ();
771 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000772 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000773 }
774 return 0;
775}
776
Greg Claytonc7bece562013-01-25 18:06:21 +0000777size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000778Module::FindTypesInNamespace (const SymbolContext& sc,
779 const ConstString &type_name,
780 const ClangNamespaceDecl *namespace_decl,
Greg Claytonc7bece562013-01-25 18:06:21 +0000781 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000782 TypeList& type_list)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000783{
Greg Clayton84db9102012-03-26 23:03:23 +0000784 const bool append = true;
785 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000786}
787
Greg Claytonb43165b2012-12-05 21:24:42 +0000788lldb::TypeSP
789Module::FindFirstType (const SymbolContext& sc,
790 const ConstString &name,
791 bool exact_match)
792{
793 TypeList type_list;
Greg Claytonc7bece562013-01-25 18:06:21 +0000794 const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
Greg Claytonb43165b2012-12-05 21:24:42 +0000795 if (num_matches)
796 return type_list.GetTypeAtIndex(0);
797 return TypeSP();
798}
799
800
Greg Claytonc7bece562013-01-25 18:06:21 +0000801size_t
Greg Clayton84db9102012-03-26 23:03:23 +0000802Module::FindTypes (const SymbolContext& sc,
803 const ConstString &name,
804 bool exact_match,
Greg Claytonc7bece562013-01-25 18:06:21 +0000805 size_t max_matches,
Greg Clayton84db9102012-03-26 23:03:23 +0000806 TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000807{
Greg Claytonc7bece562013-01-25 18:06:21 +0000808 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +0000809 const char *type_name_cstr = name.GetCString();
810 std::string type_scope;
811 std::string type_basename;
812 const bool append = true;
Greg Clayton7bc31332012-10-22 16:19:56 +0000813 TypeClass type_class = eTypeClassAny;
814 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
Enrico Granata6f3533f2011-07-29 19:53:35 +0000815 {
Greg Clayton84db9102012-03-26 23:03:23 +0000816 // Check if "name" starts with "::" which means the qualified type starts
817 // from the root namespace and implies and exact match. The typenames we
818 // get back from clang do not start with "::" so we need to strip this off
819 // in order to get the qualfied names to match
820
821 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
822 {
823 type_scope.erase(0,2);
824 exact_match = true;
825 }
826 ConstString type_basename_const_str (type_basename.c_str());
827 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
828 {
Greg Clayton7bc31332012-10-22 16:19:56 +0000829 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
Greg Clayton84db9102012-03-26 23:03:23 +0000830 num_matches = types.GetSize();
831 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000832 }
833 else
Greg Clayton84db9102012-03-26 23:03:23 +0000834 {
835 // The type is not in a namespace/class scope, just search for it by basename
Greg Clayton7bc31332012-10-22 16:19:56 +0000836 if (type_class != eTypeClassAny)
837 {
838 // The "type_name_cstr" will have been modified if we have a valid type class
839 // prefix (like "struct", "class", "union", "typedef" etc).
840 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
841 types.RemoveMismatchedTypes (type_class);
842 num_matches = types.GetSize();
843 }
844 else
845 {
846 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
847 }
Greg Clayton84db9102012-03-26 23:03:23 +0000848 }
849
850 return num_matches;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000851
852}
853
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000855Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856{
857 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000858 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 {
860 ObjectFile *obj_file = GetObjectFile ();
861 if (obj_file != NULL)
862 {
863 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Greg Clayton136dff82012-12-14 02:15:00 +0000864 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
Greg Claytone83e7312010-09-07 23:40:05 +0000865 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 }
867 }
868 return m_symfile_ap.get();
869}
870
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871void
872Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
873{
874 // Container objects whose paths do not specify a file directly can call
875 // this function to correct the file and object names.
876 m_file = file;
877 m_mod_time = file.GetModificationTime();
878 m_object_name = object_name;
879}
880
881const ArchSpec&
882Module::GetArchitecture () const
883{
884 return m_arch;
885}
886
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000887std::string
888Module::GetSpecificationDescription () const
889{
890 std::string spec(GetFileSpec().GetPath());
891 if (m_object_name)
892 {
893 spec += '(';
894 spec += m_object_name.GetCString();
895 spec += ')';
896 }
897 return spec;
898}
899
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000901Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000902{
903 Mutex::Locker locker (m_mutex);
904
Greg Claytonc982b3d2011-11-28 01:45:00 +0000905 if (level >= eDescriptionLevelFull)
906 {
907 if (m_arch.IsValid())
908 s->Printf("(%s) ", m_arch.GetArchitectureName());
909 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000910
Greg Claytonc982b3d2011-11-28 01:45:00 +0000911 if (level == eDescriptionLevelBrief)
912 {
913 const char *filename = m_file.GetFilename().GetCString();
914 if (filename)
915 s->PutCString (filename);
916 }
917 else
918 {
919 char path[PATH_MAX];
920 if (m_file.GetPath(path, sizeof(path)))
921 s->PutCString(path);
922 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000923
924 const char *object_name = m_object_name.GetCString();
925 if (object_name)
926 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000927}
928
929void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000930Module::ReportError (const char *format, ...)
931{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000932 if (format && format[0])
933 {
934 StreamString strm;
935 strm.PutCString("error: ");
936 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000937 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000938 va_list args;
939 va_start (args, format);
940 strm.PrintfVarArg(format, args);
941 va_end (args);
942
943 const int format_len = strlen(format);
944 if (format_len > 0)
945 {
946 const char last_char = format[format_len-1];
947 if (last_char != '\n' || last_char != '\r')
948 strm.EOL();
949 }
950 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
951
952 }
953}
954
Greg Clayton1d609092012-07-12 22:51:12 +0000955bool
956Module::FileHasChanged () const
957{
958 if (m_file_has_changed == false)
959 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
960 return m_file_has_changed;
961}
962
Greg Claytone38a5ed2012-01-05 03:57:59 +0000963void
964Module::ReportErrorIfModifyDetected (const char *format, ...)
965{
Greg Clayton1d609092012-07-12 22:51:12 +0000966 if (m_first_file_changed_log == false)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000967 {
Greg Clayton1d609092012-07-12 22:51:12 +0000968 if (FileHasChanged ())
Greg Claytone38a5ed2012-01-05 03:57:59 +0000969 {
Greg Clayton1d609092012-07-12 22:51:12 +0000970 m_first_file_changed_log = true;
971 if (format)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000972 {
Greg Clayton1d609092012-07-12 22:51:12 +0000973 StreamString strm;
974 strm.PutCString("error: the object file ");
975 GetDescription(&strm, lldb::eDescriptionLevelFull);
976 strm.PutCString (" has been modified\n");
977
978 va_list args;
979 va_start (args, format);
980 strm.PrintfVarArg(format, args);
981 va_end (args);
982
983 const int format_len = strlen(format);
984 if (format_len > 0)
985 {
986 const char last_char = format[format_len-1];
987 if (last_char != '\n' || last_char != '\r')
988 strm.EOL();
989 }
990 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
991 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
Greg Claytone38a5ed2012-01-05 03:57:59 +0000992 }
Greg Claytone38a5ed2012-01-05 03:57:59 +0000993 }
994 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000995}
996
997void
998Module::ReportWarning (const char *format, ...)
999{
Greg Claytone38a5ed2012-01-05 03:57:59 +00001000 if (format && format[0])
1001 {
1002 StreamString strm;
1003 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +00001004 GetDescription(&strm, lldb::eDescriptionLevelFull);
1005 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +00001006
1007 va_list args;
1008 va_start (args, format);
1009 strm.PrintfVarArg(format, args);
1010 va_end (args);
1011
1012 const int format_len = strlen(format);
1013 if (format_len > 0)
1014 {
1015 const char last_char = format[format_len-1];
1016 if (last_char != '\n' || last_char != '\r')
1017 strm.EOL();
1018 }
1019 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1020 }
Greg Claytonc982b3d2011-11-28 01:45:00 +00001021}
1022
1023void
1024Module::LogMessage (Log *log, const char *format, ...)
1025{
1026 if (log)
1027 {
1028 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +00001029 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +00001030 log_message.PutCString (": ");
1031 va_list args;
1032 va_start (args, format);
1033 log_message.PrintfVarArg (format, args);
1034 va_end (args);
1035 log->PutCString(log_message.GetString().c_str());
1036 }
1037}
1038
Greg Claytond61c0fc2012-04-23 22:55:20 +00001039void
1040Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1041{
1042 if (log)
1043 {
1044 StreamString log_message;
1045 GetDescription(&log_message, lldb::eDescriptionLevelFull);
1046 log_message.PutCString (": ");
1047 va_list args;
1048 va_start (args, format);
1049 log_message.PrintfVarArg (format, args);
1050 va_end (args);
1051 if (log->GetVerbose())
1052 Host::Backtrace (log_message, 1024);
1053 log->PutCString(log_message.GetString().c_str());
1054 }
1055}
1056
Greg Claytonc982b3d2011-11-28 01:45:00 +00001057void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058Module::Dump(Stream *s)
1059{
1060 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +00001061 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 s->Indent();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001063 s->Printf("Module %s%s%s%s\n",
1064 m_file.GetPath().c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 m_object_name ? "(" : "",
1066 m_object_name ? m_object_name.GetCString() : "",
1067 m_object_name ? ")" : "");
1068
1069 s->IndentMore();
1070 ObjectFile *objfile = GetObjectFile ();
1071
1072 if (objfile)
1073 objfile->Dump(s);
1074
1075 SymbolVendor *symbols = GetSymbolVendor ();
1076
1077 if (symbols)
1078 symbols->Dump(s);
1079
1080 s->IndentLess();
1081}
1082
1083
1084TypeList*
1085Module::GetTypeList ()
1086{
1087 SymbolVendor *symbols = GetSymbolVendor ();
1088 if (symbols)
1089 return &symbols->GetTypeList();
1090 return NULL;
1091}
1092
1093const ConstString &
1094Module::GetObjectName() const
1095{
1096 return m_object_name;
1097}
1098
1099ObjectFile *
1100Module::GetObjectFile()
1101{
1102 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +00001103 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 {
Greg Claytone83e7312010-09-07 23:40:05 +00001105 m_did_load_objfile = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 Timer scoped_timer(__PRETTY_FUNCTION__,
1107 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton5ce9c562013-02-06 17:22:03 +00001108 DataBufferSP data_sp;
1109 lldb::offset_t data_offset = 0;
1110 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001111 &m_file,
1112 m_object_offset,
1113 m_file.GetByteSize(),
Greg Clayton5ce9c562013-02-06 17:22:03 +00001114 data_sp,
1115 data_offset);
Greg Clayton593577a2011-09-21 03:57:31 +00001116 if (m_objfile_sp)
1117 {
1118 // Once we get the object file, update our module with the object file's
1119 // architecture since it might differ in vendor/os if some parts were
1120 // unknown.
1121 m_objfile_sp->GetArchitecture (m_arch);
1122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 }
Greg Clayton762f7132011-09-18 18:59:15 +00001124 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125}
1126
1127
1128const Symbol *
1129Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1130{
1131 Timer scoped_timer(__PRETTY_FUNCTION__,
1132 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1133 name.AsCString(),
1134 symbol_type);
1135 ObjectFile *objfile = GetObjectFile();
1136 if (objfile)
1137 {
1138 Symtab *symtab = objfile->GetSymtab();
1139 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001140 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 }
1142 return NULL;
1143}
1144void
1145Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1146{
1147 // No need to protect this call using m_mutex all other method calls are
1148 // already thread safe.
1149
1150 size_t num_indices = symbol_indexes.size();
1151 if (num_indices > 0)
1152 {
1153 SymbolContext sc;
1154 CalculateSymbolContext (&sc);
1155 for (size_t i = 0; i < num_indices; i++)
1156 {
1157 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1158 if (sc.symbol)
1159 sc_list.Append (sc);
1160 }
1161 }
1162}
1163
1164size_t
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001165Module::FindFunctionSymbols (const ConstString &name,
1166 uint32_t name_type_mask,
1167 SymbolContextList& sc_list)
1168{
1169 Timer scoped_timer(__PRETTY_FUNCTION__,
1170 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1171 name.AsCString(),
1172 name_type_mask);
1173 ObjectFile *objfile = GetObjectFile ();
1174 if (objfile)
1175 {
1176 Symtab *symtab = objfile->GetSymtab();
1177 if (symtab)
1178 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1179 }
1180 return 0;
1181}
1182
1183size_t
Sean Callananb96ff332011-10-13 16:49:47 +00001184Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185{
1186 // No need to protect this call using m_mutex all other method calls are
1187 // already thread safe.
1188
1189
1190 Timer scoped_timer(__PRETTY_FUNCTION__,
1191 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1192 name.AsCString(),
1193 symbol_type);
1194 const size_t initial_size = sc_list.GetSize();
1195 ObjectFile *objfile = GetObjectFile ();
1196 if (objfile)
1197 {
1198 Symtab *symtab = objfile->GetSymtab();
1199 if (symtab)
1200 {
1201 std::vector<uint32_t> symbol_indexes;
1202 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1203 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1204 }
1205 }
1206 return sc_list.GetSize() - initial_size;
1207}
1208
1209size_t
1210Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1211{
1212 // No need to protect this call using m_mutex all other method calls are
1213 // already thread safe.
1214
1215 Timer scoped_timer(__PRETTY_FUNCTION__,
1216 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1217 regex.GetText(),
1218 symbol_type);
1219 const size_t initial_size = sc_list.GetSize();
1220 ObjectFile *objfile = GetObjectFile ();
1221 if (objfile)
1222 {
1223 Symtab *symtab = objfile->GetSymtab();
1224 if (symtab)
1225 {
1226 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001227 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1229 }
1230 }
1231 return sc_list.GetSize() - initial_size;
1232}
1233
Greg Claytone01e07b2013-04-18 18:10:51 +00001234void
1235Module::SetSymbolFileFileSpec (const FileSpec &file)
1236{
1237 m_symfile_spec = file;
1238 m_symfile_ap.reset();
1239 m_did_load_symbol_vendor = false;
1240}
1241
1242
Jim Ingham5aee1622010-08-09 23:31:02 +00001243bool
1244Module::IsExecutable ()
1245{
1246 if (GetObjectFile() == NULL)
1247 return false;
1248 else
1249 return GetObjectFile()->IsExecutable();
1250}
1251
Jim Inghamb53cb272011-08-03 01:03:17 +00001252bool
1253Module::IsLoadedInTarget (Target *target)
1254{
1255 ObjectFile *obj_file = GetObjectFile();
1256 if (obj_file)
1257 {
1258 SectionList *sections = obj_file->GetSectionList();
1259 if (sections != NULL)
1260 {
1261 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +00001262 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1263 {
1264 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1265 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1266 {
1267 return true;
1268 }
1269 }
1270 }
1271 }
1272 return false;
1273}
Enrico Granata17598482012-11-08 02:22:02 +00001274
1275bool
Enrico Granata97303392013-05-21 00:00:30 +00001276Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +00001277{
1278 if (!target)
1279 {
1280 error.SetErrorString("invalid destination Target");
1281 return false;
1282 }
1283
Enrico Granata397ddd52013-05-21 20:13:34 +00001284 LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001285
Greg Clayton91c0e742013-01-11 23:44:27 +00001286 Debugger &debugger = target->GetDebugger();
1287 const ScriptLanguage script_language = debugger.GetScriptLanguage();
1288 if (script_language != eScriptLanguageNone)
Enrico Granata17598482012-11-08 02:22:02 +00001289 {
Greg Clayton91c0e742013-01-11 23:44:27 +00001290
1291 PlatformSP platform_sp(target->GetPlatform());
1292
1293 if (!platform_sp)
1294 {
1295 error.SetErrorString("invalid Platform");
1296 return false;
1297 }
Enrico Granata17598482012-11-08 02:22:02 +00001298
Greg Claytonb9d88902013-03-23 00:50:58 +00001299 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1300 *this);
1301
1302
1303 const uint32_t num_specs = file_specs.GetSize();
1304 if (num_specs)
Enrico Granata17598482012-11-08 02:22:02 +00001305 {
Greg Claytonb9d88902013-03-23 00:50:58 +00001306 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1307 if (script_interpreter)
Greg Clayton91c0e742013-01-11 23:44:27 +00001308 {
1309 for (uint32_t i=0; i<num_specs; ++i)
1310 {
1311 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1312 if (scripting_fspec && scripting_fspec.Exists())
1313 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001314 if (shoud_load == eLoadScriptFromSymFileFalse)
1315 return false;
1316 if (shoud_load == eLoadScriptFromSymFileWarn)
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001317 {
Enrico Granata397ddd52013-05-21 20:13:34 +00001318 if (feedback_stream)
Enrico Granata4e284ca2013-05-21 22:34:17 +00001319 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in this debug session:\n\n command script import \"%s\"\n\nTo run all discovered debug scripts in this session:\n\n settings set target.load-script-from-symbol-file true\n"
Enrico Granata397ddd52013-05-21 20:13:34 +00001320 ,GetFileSpec().GetFileNameStrippingExtension().GetCString(),scripting_fspec.GetPath().c_str());
Enrico Granata2ea43cd2013-05-13 17:03:52 +00001321 return false;
1322 }
Greg Clayton91c0e742013-01-11 23:44:27 +00001323 StreamString scripting_stream;
1324 scripting_fspec.Dump(&scripting_stream);
Enrico Granatae0c70f12013-05-31 01:03:09 +00001325 const bool can_reload = true;
Greg Clayton91c0e742013-01-11 23:44:27 +00001326 const bool init_lldb_globals = false;
1327 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), can_reload, init_lldb_globals, error);
1328 if (!did_load)
1329 return false;
1330 }
1331 }
1332 }
Greg Claytonb9d88902013-03-23 00:50:58 +00001333 else
1334 {
1335 error.SetErrorString("invalid ScriptInterpreter");
1336 return false;
1337 }
Enrico Granata17598482012-11-08 02:22:02 +00001338 }
1339 }
1340 return true;
1341}
1342
1343bool
Jim Ingham5aee1622010-08-09 23:31:02 +00001344Module::SetArchitecture (const ArchSpec &new_arch)
1345{
Greg Clayton64195a22011-02-23 00:35:02 +00001346 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001347 {
1348 m_arch = new_arch;
1349 return true;
Greg Clayton64195a22011-02-23 00:35:02 +00001350 }
Sean Callananbf4b7be2012-12-13 22:07:14 +00001351 return m_arch.IsExactMatch(new_arch);
Jim Ingham5aee1622010-08-09 23:31:02 +00001352}
1353
Greg Claytonc9660542012-02-05 02:38:54 +00001354bool
1355Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
1356{
Greg Clayton741f3f92012-03-27 21:10:07 +00001357 size_t num_loaded_sections = 0;
1358 ObjectFile *objfile = GetObjectFile();
1359 if (objfile)
Greg Claytonc9660542012-02-05 02:38:54 +00001360 {
Greg Clayton741f3f92012-03-27 21:10:07 +00001361 SectionList *section_list = objfile->GetSectionList ();
Greg Claytonc9660542012-02-05 02:38:54 +00001362 if (section_list)
1363 {
1364 const size_t num_sections = section_list->GetSize();
1365 size_t sect_idx = 0;
1366 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1367 {
1368 // Iterate through the object file sections to find the
1369 // first section that starts of file offset zero and that
1370 // has bytes in the file...
Greg Clayton7820bd12012-07-07 01:24:12 +00001371 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton741f3f92012-03-27 21:10:07 +00001372 // Only load non-thread specific sections when given a slide
Greg Clayton7820bd12012-07-07 01:24:12 +00001373 if (section_sp && !section_sp->IsThreadSpecific())
Greg Claytonc9660542012-02-05 02:38:54 +00001374 {
Greg Clayton7820bd12012-07-07 01:24:12 +00001375 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset))
Greg Clayton741f3f92012-03-27 21:10:07 +00001376 ++num_loaded_sections;
Greg Claytonc9660542012-02-05 02:38:54 +00001377 }
1378 }
Greg Claytonc9660542012-02-05 02:38:54 +00001379 }
1380 }
Greg Clayton741f3f92012-03-27 21:10:07 +00001381 changed = num_loaded_sections > 0;
1382 return num_loaded_sections > 0;
Greg Claytonc9660542012-02-05 02:38:54 +00001383}
1384
Greg Claytonb9a01b32012-02-26 05:51:37 +00001385
1386bool
1387Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1388{
1389 const UUID &uuid = module_ref.GetUUID();
1390
1391 if (uuid.IsValid())
1392 {
1393 // If the UUID matches, then nothing more needs to match...
1394 if (uuid == GetUUID())
1395 return true;
1396 else
1397 return false;
1398 }
1399
1400 const FileSpec &file_spec = module_ref.GetFileSpec();
1401 if (file_spec)
1402 {
1403 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory()))
1404 return false;
1405 }
1406
1407 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1408 if (platform_file_spec)
1409 {
Greg Clayton548e9a32012-10-02 06:04:17 +00001410 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory()))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001411 return false;
1412 }
1413
1414 const ArchSpec &arch = module_ref.GetArchitecture();
1415 if (arch.IsValid())
1416 {
Sean Callananbf4b7be2012-12-13 22:07:14 +00001417 if (!m_arch.IsCompatibleMatch(arch))
Greg Claytonb9a01b32012-02-26 05:51:37 +00001418 return false;
1419 }
1420
1421 const ConstString &object_name = module_ref.GetObjectName();
1422 if (object_name)
1423 {
1424 if (object_name != GetObjectName())
1425 return false;
1426 }
1427 return true;
1428}
1429
Greg Claytond804d282012-03-15 21:01:31 +00001430bool
1431Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1432{
1433 Mutex::Locker locker (m_mutex);
1434 return m_source_mappings.FindFile (orig_spec, new_spec);
1435}
1436
Greg Claytonf9be6932012-03-19 22:22:41 +00001437bool
1438Module::RemapSourceFile (const char *path, std::string &new_path) const
1439{
1440 Mutex::Locker locker (m_mutex);
1441 return m_source_mappings.RemapPath(path, new_path);
1442}
1443
Enrico Granata3467d802012-09-04 18:47:54 +00001444uint32_t
1445Module::GetVersion (uint32_t *versions, uint32_t num_versions)
1446{
1447 ObjectFile *obj_file = GetObjectFile();
1448 if (obj_file)
1449 return obj_file->GetVersion (versions, num_versions);
1450
1451 if (versions && num_versions)
1452 {
1453 for (uint32_t i=0; i<num_versions; ++i)
1454 versions[i] = UINT32_MAX;
1455 }
1456 return 0;
1457}
Greg Clayton43fe2172013-04-03 02:00:15 +00001458
1459void
1460Module::PrepareForFunctionNameLookup (const ConstString &name,
1461 uint32_t name_type_mask,
1462 ConstString &lookup_name,
1463 uint32_t &lookup_name_type_mask,
1464 bool &match_name_after_lookup)
1465{
1466 const char *name_cstr = name.GetCString();
1467 lookup_name_type_mask = eFunctionNameTypeNone;
1468 match_name_after_lookup = false;
1469 const char *base_name_start = NULL;
1470 const char *base_name_end = NULL;
1471
1472 if (name_type_mask & eFunctionNameTypeAuto)
1473 {
1474 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1475 lookup_name_type_mask = eFunctionNameTypeFull;
1476 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1477 lookup_name_type_mask = eFunctionNameTypeFull;
1478 else
1479 {
1480 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1481 lookup_name_type_mask |= eFunctionNameTypeSelector;
1482
Greg Clayton6ecb2322013-05-18 00:11:21 +00001483 CPPLanguageRuntime::MethodName cpp_method (name);
1484 llvm::StringRef basename (cpp_method.GetBasename());
1485 if (basename.empty())
1486 {
1487 if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1488 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1489 }
1490 else
1491 {
1492 base_name_start = basename.data();
1493 base_name_end = base_name_start + basename.size();
Greg Clayton43fe2172013-04-03 02:00:15 +00001494 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
Greg Clayton6ecb2322013-05-18 00:11:21 +00001495 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001496 }
1497 }
1498 else
1499 {
1500 lookup_name_type_mask = name_type_mask;
1501 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1502 {
1503 // If they've asked for a CPP method or function name and it can't be that, we don't
1504 // even need to search for CPP methods or names.
Greg Clayton6ecb2322013-05-18 00:11:21 +00001505 CPPLanguageRuntime::MethodName cpp_method (name);
1506 if (cpp_method.IsValid())
Greg Clayton43fe2172013-04-03 02:00:15 +00001507 {
Greg Clayton6ecb2322013-05-18 00:11:21 +00001508 llvm::StringRef basename (cpp_method.GetBasename());
1509 base_name_start = basename.data();
1510 base_name_end = base_name_start + basename.size();
1511
1512 if (!cpp_method.GetQualifiers().empty())
1513 {
1514 // There is a "const" or other qualifer following the end of the fucntion parens,
1515 // this can't be a eFunctionNameTypeBase
1516 lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1517 if (lookup_name_type_mask == eFunctionNameTypeNone)
1518 return;
1519 }
1520 }
1521 else
1522 {
1523 if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1524 {
1525 lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1526 if (lookup_name_type_mask == eFunctionNameTypeNone)
1527 return;
1528 }
Greg Clayton43fe2172013-04-03 02:00:15 +00001529 }
1530 }
1531
1532 if (lookup_name_type_mask & eFunctionNameTypeSelector)
1533 {
1534 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1535 {
1536 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1537 if (lookup_name_type_mask == eFunctionNameTypeNone)
1538 return;
1539 }
1540 }
1541 }
1542
1543 if (base_name_start &&
1544 base_name_end &&
1545 base_name_start != name_cstr &&
1546 base_name_start < base_name_end)
1547 {
1548 // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1549 // lookup on the basename "count" and then make sure any matching results contain "a::count"
1550 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1551 // to true
1552 lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1553 match_name_after_lookup = true;
1554 }
1555 else
1556 {
1557 // The name is already correct, just use the exact name as supplied, and we won't need
1558 // to check if any matches contain "name"
1559 lookup_name = name;
1560 match_name_after_lookup = false;
1561 }
1562}