blob: 28e8be2334be436ab4ddeb6c06d0551349a2f130 [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
10#include "lldb/Core/Module.h"
11#include "lldb/Core/Log.h"
12#include "lldb/Core/ModuleList.h"
13#include "lldb/Core/RegularExpression.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000014#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000016#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/lldb-private-log.h"
18#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Symbol/SymbolContext.h"
20#include "lldb/Symbol/SymbolVendor.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
Greg Clayton65a03992011-08-09 00:01:09 +000025// Shared pointers to modules track module lifetimes in
26// targets and in the global module, but this collection
27// will track all module objects that are still alive
28typedef std::vector<Module *> ModuleCollection;
29
30static ModuleCollection &
31GetModuleCollection()
32{
Jim Ingham549f7372011-10-31 23:47:10 +000033 // This module collection needs to live past any module, so we could either make it a
34 // shared pointer in each module or just leak is. Since it is only an empty vector by
35 // the time all the modules have gone away, we just leak it for now. If we decide this
36 // is a big problem we can introduce a Finalize method that will tear everything down in
37 // a predictable order.
38
39 static ModuleCollection *g_module_collection = NULL;
40 if (g_module_collection == NULL)
41 g_module_collection = new ModuleCollection();
42
43 return *g_module_collection;
Greg Clayton65a03992011-08-09 00:01:09 +000044}
45
Greg Claytonb26e6be2012-01-27 18:08:35 +000046Mutex *
Greg Clayton65a03992011-08-09 00:01:09 +000047Module::GetAllocationModuleCollectionMutex()
48{
Greg Claytonb26e6be2012-01-27 18:08:35 +000049 // NOTE: The mutex below must be leaked since the global module list in
50 // the ModuleList class will get torn at some point, and we can't know
51 // if it will tear itself down before the "g_module_collection_mutex" below
52 // will. So we leak a Mutex object below to safeguard against that
53
54 static Mutex *g_module_collection_mutex = NULL;
55 if (g_module_collection_mutex == NULL)
56 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
57 return g_module_collection_mutex;
Greg Clayton65a03992011-08-09 00:01:09 +000058}
59
60size_t
61Module::GetNumberAllocatedModules ()
62{
63 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
64 return GetModuleCollection().size();
65}
66
67Module *
68Module::GetAllocatedModuleAtIndex (size_t idx)
69{
70 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
71 ModuleCollection &modules = GetModuleCollection();
72 if (idx < modules.size())
73 return modules[idx];
74 return NULL;
75}
Greg Clayton29ad7b92012-01-27 18:45:39 +000076#if 0
Greg Clayton65a03992011-08-09 00:01:09 +000077
Greg Clayton29ad7b92012-01-27 18:45:39 +000078// These functions help us to determine if modules are still loaded, yet don't require that
79// you have a command interpreter and can easily be called from an external debugger.
80namespace lldb {
Greg Clayton65a03992011-08-09 00:01:09 +000081
Greg Clayton29ad7b92012-01-27 18:45:39 +000082 void
83 ClearModuleInfo (void)
84 {
85 ModuleList::RemoveOrphanSharedModules();
86 }
87
88 void
89 DumpModuleInfo (void)
90 {
91 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
92 ModuleCollection &modules = GetModuleCollection();
93 const size_t count = modules.size();
94 printf ("%s: %zu modules:\n", __PRETTY_FUNCTION__, count);
95 for (size_t i=0; i<count; ++i)
96 {
97
98 StreamString strm;
99 Module *module = modules[i];
100 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
101 module->GetDescription(&strm, eDescriptionLevelFull);
102 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
103 module,
104 in_shared_module_list,
105 (uint32_t)module->use_count(),
106 strm.GetString().c_str());
107 }
108 }
109}
110
111#endif
Greg Clayton65a03992011-08-09 00:01:09 +0000112
113
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
115 m_mutex (Mutex::eMutexTypeRecursive),
116 m_mod_time (file_spec.GetModificationTime()),
117 m_arch (arch),
118 m_uuid (),
119 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +0000120 m_platform_file(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +0000122 m_object_offset (object_offset),
Greg Clayton762f7132011-09-18 18:59:15 +0000123 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +0000124 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000125 m_ast (),
Greg Claytone83e7312010-09-07 23:40:05 +0000126 m_did_load_objfile (false),
127 m_did_load_symbol_vendor (false),
128 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000129 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +0000130 m_is_dynamic_loader_module (false),
131 m_was_modified (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132{
Greg Clayton65a03992011-08-09 00:01:09 +0000133 // Scope for locker below...
134 {
135 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
136 GetModuleCollection().push_back(this);
137 }
138
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 if (object_name)
140 m_object_name = *object_name;
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000141 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 if (log)
143 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
144 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000145 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 m_file.GetDirectory().AsCString(""),
147 m_file.GetFilename().AsCString(""),
148 m_object_name.IsEmpty() ? "" : "(",
149 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
150 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
153Module::~Module()
154{
Greg Clayton65a03992011-08-09 00:01:09 +0000155 // Scope for locker below...
156 {
157 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
158 ModuleCollection &modules = GetModuleCollection();
159 ModuleCollection::iterator end = modules.end();
160 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
161 if (pos != end)
162 modules.erase(pos);
163 }
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000164 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 if (log)
166 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
167 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000168 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 m_file.GetDirectory().AsCString(""),
170 m_file.GetFilename().AsCString(""),
171 m_object_name.IsEmpty() ? "" : "(",
172 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
173 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000174 // Release any auto pointers before we start tearing down our member
175 // variables since the object file and symbol files might need to make
176 // function calls back into this module object. The ordering is important
177 // here because symbol files can require the module object file. So we tear
178 // down the symbol file first, then the object file.
179 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000180 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181}
182
183
Greg Clayton60830262011-02-04 18:53:10 +0000184const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185Module::GetUUID()
186{
187 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000188 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
190 ObjectFile * obj_file = GetObjectFile ();
191
192 if (obj_file != NULL)
193 {
194 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000195 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 }
197 }
198 return m_uuid;
199}
200
Greg Clayton6beaaa62011-01-17 03:46:26 +0000201ClangASTContext &
202Module::GetClangASTContext ()
203{
204 Mutex::Locker locker (m_mutex);
205 if (m_did_init_ast == false)
206 {
207 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000208 ArchSpec object_arch;
209 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000210 {
211 m_did_init_ast = true;
Greg Clayton514487e2011-02-15 21:59:32 +0000212 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000213 }
214 }
215 return m_ast;
216}
217
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218void
219Module::ParseAllDebugSymbols()
220{
221 Mutex::Locker locker (m_mutex);
222 uint32_t num_comp_units = GetNumCompileUnits();
223 if (num_comp_units == 0)
224 return;
225
Greg Claytona2eee182011-09-17 07:23:18 +0000226 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000227 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 uint32_t cu_idx;
229 SymbolVendor *symbols = GetSymbolVendor ();
230
231 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
232 {
233 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
234 if (sc.comp_unit)
235 {
236 sc.function = NULL;
237 symbols->ParseVariablesForContext(sc);
238
239 symbols->ParseCompileUnitFunctions(sc);
240
241 uint32_t func_idx;
242 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
243 {
244 symbols->ParseFunctionBlocks(sc);
245
246 // Parse the variables for this function and all its blocks
247 symbols->ParseVariablesForContext(sc);
248 }
249
250
251 // Parse all types for this compile unit
252 sc.function = NULL;
253 symbols->ParseTypes(sc);
254 }
255 }
256}
257
258void
259Module::CalculateSymbolContext(SymbolContext* sc)
260{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000261 sc->module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000264Module *
265Module::CalculateSymbolContextModule ()
266{
267 return this;
268}
269
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270void
271Module::DumpSymbolContext(Stream *s)
272{
Jason Molendafd54b362011-09-20 21:44:10 +0000273 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274}
275
276uint32_t
277Module::GetNumCompileUnits()
278{
279 Mutex::Locker locker (m_mutex);
280 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
281 SymbolVendor *symbols = GetSymbolVendor ();
282 if (symbols)
283 return symbols->GetNumCompileUnits();
284 return 0;
285}
286
287CompUnitSP
288Module::GetCompileUnitAtIndex (uint32_t index)
289{
290 Mutex::Locker locker (m_mutex);
291 uint32_t num_comp_units = GetNumCompileUnits ();
292 CompUnitSP cu_sp;
293
294 if (index < num_comp_units)
295 {
296 SymbolVendor *symbols = GetSymbolVendor ();
297 if (symbols)
298 cu_sp = symbols->GetCompileUnitAtIndex(index);
299 }
300 return cu_sp;
301}
302
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303bool
304Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
305{
306 Mutex::Locker locker (m_mutex);
307 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
308 ObjectFile* ofile = GetObjectFile();
309 if (ofile)
310 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
311 return false;
312}
313
314uint32_t
315Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
316{
317 Mutex::Locker locker (m_mutex);
318 uint32_t resolved_flags = 0;
319
320 // Clear the result symbol context in case we don't find anything
321 sc.Clear();
322
323 // Get the section from the section/offset address.
324 const Section *section = so_addr.GetSection();
325
326 // Make sure the section matches this module before we try and match anything
327 if (section && section->GetModule() == this)
328 {
329 // If the section offset based address resolved itself, then this
330 // is the right module.
Greg Claytone1cd1be2012-01-29 20:56:30 +0000331 sc.module_sp = shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 resolved_flags |= eSymbolContextModule;
333
334 // Resolve the compile unit, function, block, line table or line
335 // entry if requested.
336 if (resolve_scope & eSymbolContextCompUnit ||
337 resolve_scope & eSymbolContextFunction ||
338 resolve_scope & eSymbolContextBlock ||
339 resolve_scope & eSymbolContextLineEntry )
340 {
341 SymbolVendor *symbols = GetSymbolVendor ();
342 if (symbols)
343 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
344 }
345
Jim Ingham680e1772010-08-31 23:51:36 +0000346 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
347 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
349 ObjectFile* ofile = GetObjectFile();
350 if (ofile)
351 {
352 Symtab *symtab = ofile->GetSymtab();
353 if (symtab)
354 {
355 if (so_addr.IsSectionOffset())
356 {
357 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
358 if (sc.symbol)
359 resolved_flags |= eSymbolContextSymbol;
360 }
361 }
362 }
363 }
364 }
365 return resolved_flags;
366}
367
368uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000369Module::ResolveSymbolContextForFilePath
370(
371 const char *file_path,
372 uint32_t line,
373 bool check_inlines,
374 uint32_t resolve_scope,
375 SymbolContextList& sc_list
376)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377{
Greg Clayton274060b2010-10-20 20:54:39 +0000378 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
380}
381
382uint32_t
383Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
384{
385 Mutex::Locker locker (m_mutex);
386 Timer scoped_timer(__PRETTY_FUNCTION__,
387 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
388 file_spec.GetDirectory().AsCString(""),
389 file_spec.GetDirectory() ? "/" : "",
390 file_spec.GetFilename().AsCString(""),
391 line,
392 check_inlines ? "yes" : "no",
393 resolve_scope);
394
395 const uint32_t initial_count = sc_list.GetSize();
396
397 SymbolVendor *symbols = GetSymbolVendor ();
398 if (symbols)
399 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
400
401 return sc_list.GetSize() - initial_count;
402}
403
404
405uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000406Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407{
408 SymbolVendor *symbols = GetSymbolVendor ();
409 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000410 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 return 0;
412}
413uint32_t
414Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
415{
416 SymbolVendor *symbols = GetSymbolVendor ();
417 if (symbols)
418 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
419 return 0;
420}
421
422uint32_t
Greg Clayton644247c2011-07-07 01:59:51 +0000423Module::FindCompileUnits (const FileSpec &path,
424 bool append,
425 SymbolContextList &sc_list)
426{
427 if (!append)
428 sc_list.Clear();
429
430 const uint32_t start_size = sc_list.GetSize();
431 const uint32_t num_compile_units = GetNumCompileUnits();
432 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000433 sc.module_sp = shared_from_this();
Greg Clayton644247c2011-07-07 01:59:51 +0000434 const bool compare_directory = path.GetDirectory();
435 for (uint32_t i=0; i<num_compile_units; ++i)
436 {
437 sc.comp_unit = GetCompileUnitAtIndex(i).get();
438 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
439 sc_list.Append(sc);
440 }
441 return sc_list.GetSize() - start_size;
442}
443
444uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000445Module::FindFunctions (const ConstString &name,
446 const ClangNamespaceDecl *namespace_decl,
Greg Clayton931180e2011-01-27 06:44:37 +0000447 uint32_t name_type_mask,
448 bool include_symbols,
449 bool append,
450 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451{
Greg Clayton931180e2011-01-27 06:44:37 +0000452 if (!append)
453 sc_list.Clear();
454
455 const uint32_t start_size = sc_list.GetSize();
456
457 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 SymbolVendor *symbols = GetSymbolVendor ();
459 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000460 symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000461
462 // Now check our symbol table for symbols that are code symbols if requested
463 if (include_symbols)
464 {
465 ObjectFile *objfile = GetObjectFile();
466 if (objfile)
467 {
468 Symtab *symtab = objfile->GetSymtab();
469 if (symtab)
470 {
471 std::vector<uint32_t> symbol_indexes;
472 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
473 const uint32_t num_matches = symbol_indexes.size();
474 if (num_matches)
475 {
Greg Clayton357132e2011-03-26 19:14:58 +0000476 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000477 SymbolContext sc(this);
478 for (uint32_t i=0; i<num_matches; i++)
479 {
480 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000481 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000482 }
483 }
484 }
485 }
486 }
487 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
490uint32_t
Greg Clayton931180e2011-01-27 06:44:37 +0000491Module::FindFunctions (const RegularExpression& regex,
492 bool include_symbols,
493 bool append,
494 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495{
Greg Clayton931180e2011-01-27 06:44:37 +0000496 if (!append)
497 sc_list.Clear();
498
499 const uint32_t start_size = sc_list.GetSize();
500
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 SymbolVendor *symbols = GetSymbolVendor ();
502 if (symbols)
Jim Ingham832332d2011-05-18 05:02:10 +0000503 symbols->FindFunctions(regex, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000504 // Now check our symbol table for symbols that are code symbols if requested
505 if (include_symbols)
506 {
507 ObjectFile *objfile = GetObjectFile();
508 if (objfile)
509 {
510 Symtab *symtab = objfile->GetSymtab();
511 if (symtab)
512 {
513 std::vector<uint32_t> symbol_indexes;
514 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
515 const uint32_t num_matches = symbol_indexes.size();
516 if (num_matches)
517 {
Greg Clayton357132e2011-03-26 19:14:58 +0000518 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000519 SymbolContext sc(this);
520 for (uint32_t i=0; i<num_matches; i++)
521 {
522 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000523 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000524 }
525 }
526 }
527 }
528 }
529 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
Greg Clayton3504eee2010-08-03 01:26:16 +0000532uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000533Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
Greg Clayton3504eee2010-08-03 01:26:16 +0000534{
535 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
536 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
537 {
538 SymbolVendor *symbols = GetSymbolVendor ();
539 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000540 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000541 }
542 return 0;
543}
544
Enrico Granata6f3533f2011-07-29 19:53:35 +0000545// depending on implementation details, type lookup might fail because of
546// embedded spurious namespace:: prefixes. this call strips them, paying
547// attention to the fact that a type might have namespace'd type names as
548// arguments to templates, and those must not be stripped off
549static const char*
550StripTypeName(const char* name_cstr)
551{
Johnny Chenc6770762011-12-14 01:43:31 +0000552 // Protect against null c string.
553 if (!name_cstr)
554 return name_cstr;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000555 const char* skip_namespace = strstr(name_cstr, "::");
556 const char* template_arg_char = strchr(name_cstr, '<');
557 while (skip_namespace != NULL)
558 {
559 if (template_arg_char != NULL &&
560 skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
561 break;
562 name_cstr = skip_namespace+2;
563 skip_namespace = strstr(name_cstr, "::");
564 }
565 return name_cstr;
566}
567
568uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000569Module::FindTypes (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000570{
Sean Callanan213fdb82011-10-13 01:49:10 +0000571 uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000572
573 if (retval == 0)
574 {
Jim Ingham50b3d502012-01-12 22:35:29 +0000575 const char *orig_name = name.GetCString();
576 const char *stripped = StripTypeName(orig_name);
577 // Only do this lookup if StripTypeName has stripped the name:
578 if (stripped != orig_name)
579 return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
580 else
581 return 0;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000582 }
583 else
584 return retval;
585
586}
587
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588//uint32_t
589//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
590//{
591// Timer scoped_timer(__PRETTY_FUNCTION__);
592// SymbolVendor *symbols = GetSymbolVendor ();
593// if (symbols)
594// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
595// return 0;
596//
597//}
598
599SymbolVendor*
600Module::GetSymbolVendor (bool can_create)
601{
602 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000603 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 {
605 ObjectFile *obj_file = GetObjectFile ();
606 if (obj_file != NULL)
607 {
608 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
609 m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
Greg Claytone83e7312010-09-07 23:40:05 +0000610 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 }
612 }
613 return m_symfile_ap.get();
614}
615
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616void
617Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
618{
619 // Container objects whose paths do not specify a file directly can call
620 // this function to correct the file and object names.
621 m_file = file;
622 m_mod_time = file.GetModificationTime();
623 m_object_name = object_name;
624}
625
626const ArchSpec&
627Module::GetArchitecture () const
628{
629 return m_arch;
630}
631
632void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000633Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000634{
635 Mutex::Locker locker (m_mutex);
636
Greg Claytonc982b3d2011-11-28 01:45:00 +0000637 if (level >= eDescriptionLevelFull)
638 {
639 if (m_arch.IsValid())
640 s->Printf("(%s) ", m_arch.GetArchitectureName());
641 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000642
Greg Claytonc982b3d2011-11-28 01:45:00 +0000643 if (level == eDescriptionLevelBrief)
644 {
645 const char *filename = m_file.GetFilename().GetCString();
646 if (filename)
647 s->PutCString (filename);
648 }
649 else
650 {
651 char path[PATH_MAX];
652 if (m_file.GetPath(path, sizeof(path)))
653 s->PutCString(path);
654 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000655
656 const char *object_name = m_object_name.GetCString();
657 if (object_name)
658 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000659}
660
661void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000662Module::ReportError (const char *format, ...)
663{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000664 if (format && format[0])
665 {
666 StreamString strm;
667 strm.PutCString("error: ");
668 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000669 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000670 va_list args;
671 va_start (args, format);
672 strm.PrintfVarArg(format, args);
673 va_end (args);
674
675 const int format_len = strlen(format);
676 if (format_len > 0)
677 {
678 const char last_char = format[format_len-1];
679 if (last_char != '\n' || last_char != '\r')
680 strm.EOL();
681 }
682 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
683
684 }
685}
686
687void
688Module::ReportErrorIfModifyDetected (const char *format, ...)
689{
690 if (!GetModified(true) && GetModified(false))
691 {
692 if (format)
693 {
694 StreamString strm;
695 strm.PutCString("error: the object file ");
696 GetDescription(&strm, lldb::eDescriptionLevelFull);
697 strm.PutCString (" has been modified\n");
698
699 va_list args;
700 va_start (args, format);
701 strm.PrintfVarArg(format, args);
702 va_end (args);
703
704 const int format_len = strlen(format);
705 if (format_len > 0)
706 {
707 const char last_char = format[format_len-1];
708 if (last_char != '\n' || last_char != '\r')
709 strm.EOL();
710 }
711 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
712 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
713 }
714 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000715}
716
717void
718Module::ReportWarning (const char *format, ...)
719{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000720 if (format && format[0])
721 {
722 StreamString strm;
723 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +0000724 GetDescription(&strm, lldb::eDescriptionLevelFull);
725 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000726
727 va_list args;
728 va_start (args, format);
729 strm.PrintfVarArg(format, args);
730 va_end (args);
731
732 const int format_len = strlen(format);
733 if (format_len > 0)
734 {
735 const char last_char = format[format_len-1];
736 if (last_char != '\n' || last_char != '\r')
737 strm.EOL();
738 }
739 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
740 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000741}
742
743void
744Module::LogMessage (Log *log, const char *format, ...)
745{
746 if (log)
747 {
748 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +0000749 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +0000750 log_message.PutCString (": ");
751 va_list args;
752 va_start (args, format);
753 log_message.PrintfVarArg (format, args);
754 va_end (args);
755 log->PutCString(log_message.GetString().c_str());
756 }
757}
758
Greg Claytone38a5ed2012-01-05 03:57:59 +0000759bool
760Module::GetModified (bool use_cached_only)
761{
762 if (m_was_modified == false && use_cached_only == false)
763 {
764 TimeValue curr_mod_time (m_file.GetModificationTime());
765 m_was_modified = curr_mod_time != m_mod_time;
766 }
767 return m_was_modified;
768}
769
770bool
771Module::SetModified (bool b)
772{
773 const bool prev_value = m_was_modified;
774 m_was_modified = b;
775 return prev_value;
776}
777
778
Greg Claytonc982b3d2011-11-28 01:45:00 +0000779void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780Module::Dump(Stream *s)
781{
782 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +0000783 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 s->Indent();
785 s->Printf("Module %s/%s%s%s%s\n",
786 m_file.GetDirectory().AsCString(),
787 m_file.GetFilename().AsCString(),
788 m_object_name ? "(" : "",
789 m_object_name ? m_object_name.GetCString() : "",
790 m_object_name ? ")" : "");
791
792 s->IndentMore();
793 ObjectFile *objfile = GetObjectFile ();
794
795 if (objfile)
796 objfile->Dump(s);
797
798 SymbolVendor *symbols = GetSymbolVendor ();
799
800 if (symbols)
801 symbols->Dump(s);
802
803 s->IndentLess();
804}
805
806
807TypeList*
808Module::GetTypeList ()
809{
810 SymbolVendor *symbols = GetSymbolVendor ();
811 if (symbols)
812 return &symbols->GetTypeList();
813 return NULL;
814}
815
816const ConstString &
817Module::GetObjectName() const
818{
819 return m_object_name;
820}
821
822ObjectFile *
823Module::GetObjectFile()
824{
825 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000826 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 {
Greg Claytone83e7312010-09-07 23:40:05 +0000828 m_did_load_objfile = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 Timer scoped_timer(__PRETTY_FUNCTION__,
830 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton44435ed2012-01-12 05:25:17 +0000831 DataBufferSP file_data_sp;
832 m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp);
Greg Clayton593577a2011-09-21 03:57:31 +0000833 if (m_objfile_sp)
834 {
835 // Once we get the object file, update our module with the object file's
836 // architecture since it might differ in vendor/os if some parts were
837 // unknown.
838 m_objfile_sp->GetArchitecture (m_arch);
839 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840 }
Greg Clayton762f7132011-09-18 18:59:15 +0000841 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842}
843
844
845const Symbol *
846Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
847{
848 Timer scoped_timer(__PRETTY_FUNCTION__,
849 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
850 name.AsCString(),
851 symbol_type);
852 ObjectFile *objfile = GetObjectFile();
853 if (objfile)
854 {
855 Symtab *symtab = objfile->GetSymtab();
856 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000857 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 }
859 return NULL;
860}
861void
862Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
863{
864 // No need to protect this call using m_mutex all other method calls are
865 // already thread safe.
866
867 size_t num_indices = symbol_indexes.size();
868 if (num_indices > 0)
869 {
870 SymbolContext sc;
871 CalculateSymbolContext (&sc);
872 for (size_t i = 0; i < num_indices; i++)
873 {
874 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
875 if (sc.symbol)
876 sc_list.Append (sc);
877 }
878 }
879}
880
881size_t
Sean Callananb96ff332011-10-13 16:49:47 +0000882Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883{
884 // No need to protect this call using m_mutex all other method calls are
885 // already thread safe.
886
887
888 Timer scoped_timer(__PRETTY_FUNCTION__,
889 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
890 name.AsCString(),
891 symbol_type);
892 const size_t initial_size = sc_list.GetSize();
893 ObjectFile *objfile = GetObjectFile ();
894 if (objfile)
895 {
896 Symtab *symtab = objfile->GetSymtab();
897 if (symtab)
898 {
899 std::vector<uint32_t> symbol_indexes;
900 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
901 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
902 }
903 }
904 return sc_list.GetSize() - initial_size;
905}
906
907size_t
908Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
909{
910 // No need to protect this call using m_mutex all other method calls are
911 // already thread safe.
912
913 Timer scoped_timer(__PRETTY_FUNCTION__,
914 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
915 regex.GetText(),
916 symbol_type);
917 const size_t initial_size = sc_list.GetSize();
918 ObjectFile *objfile = GetObjectFile ();
919 if (objfile)
920 {
921 Symtab *symtab = objfile->GetSymtab();
922 if (symtab)
923 {
924 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000925 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
927 }
928 }
929 return sc_list.GetSize() - initial_size;
930}
931
932const TimeValue &
933Module::GetModificationTime () const
934{
935 return m_mod_time;
936}
Jim Ingham5aee1622010-08-09 23:31:02 +0000937
938bool
939Module::IsExecutable ()
940{
941 if (GetObjectFile() == NULL)
942 return false;
943 else
944 return GetObjectFile()->IsExecutable();
945}
946
Jim Inghamb53cb272011-08-03 01:03:17 +0000947bool
948Module::IsLoadedInTarget (Target *target)
949{
950 ObjectFile *obj_file = GetObjectFile();
951 if (obj_file)
952 {
953 SectionList *sections = obj_file->GetSectionList();
954 if (sections != NULL)
955 {
956 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +0000957 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
958 {
959 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
960 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
961 {
962 return true;
963 }
964 }
965 }
966 }
967 return false;
968}
Jim Ingham5aee1622010-08-09 23:31:02 +0000969bool
970Module::SetArchitecture (const ArchSpec &new_arch)
971{
Greg Clayton64195a22011-02-23 00:35:02 +0000972 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +0000973 {
974 m_arch = new_arch;
975 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000976 }
977 return m_arch == new_arch;
Jim Ingham5aee1622010-08-09 23:31:02 +0000978}
979