blob: ea16376aa340ad12cff40889e5a982ef20156289 [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
46Mutex &
47Module::GetAllocationModuleCollectionMutex()
48{
49 static Mutex g_module_collection_mutex(Mutex::eMutexTypeRecursive);
50 return g_module_collection_mutex;
51}
52
53size_t
54Module::GetNumberAllocatedModules ()
55{
56 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
57 return GetModuleCollection().size();
58}
59
60Module *
61Module::GetAllocatedModuleAtIndex (size_t idx)
62{
63 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
64 ModuleCollection &modules = GetModuleCollection();
65 if (idx < modules.size())
66 return modules[idx];
67 return NULL;
68}
69
70
71
72
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
74 m_mutex (Mutex::eMutexTypeRecursive),
75 m_mod_time (file_spec.GetModificationTime()),
76 m_arch (arch),
77 m_uuid (),
78 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +000079 m_platform_file(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +000081 m_object_offset (object_offset),
Greg Clayton762f7132011-09-18 18:59:15 +000082 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +000083 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +000084 m_ast (),
Greg Claytone83e7312010-09-07 23:40:05 +000085 m_did_load_objfile (false),
86 m_did_load_symbol_vendor (false),
87 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +000088 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +000089 m_is_dynamic_loader_module (false),
90 m_was_modified (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091{
Greg Clayton65a03992011-08-09 00:01:09 +000092 // Scope for locker below...
93 {
94 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
95 GetModuleCollection().push_back(this);
96 }
97
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 if (object_name)
99 m_object_name = *object_name;
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000100 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 if (log)
102 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
103 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000104 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 m_file.GetDirectory().AsCString(""),
106 m_file.GetFilename().AsCString(""),
107 m_object_name.IsEmpty() ? "" : "(",
108 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
109 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
112Module::~Module()
113{
Greg Clayton65a03992011-08-09 00:01:09 +0000114 // Scope for locker below...
115 {
116 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
117 ModuleCollection &modules = GetModuleCollection();
118 ModuleCollection::iterator end = modules.end();
119 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
120 if (pos != end)
121 modules.erase(pos);
122 }
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000123 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 if (log)
125 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
126 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000127 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 m_file.GetDirectory().AsCString(""),
129 m_file.GetFilename().AsCString(""),
130 m_object_name.IsEmpty() ? "" : "(",
131 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
132 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000133 // Release any auto pointers before we start tearing down our member
134 // variables since the object file and symbol files might need to make
135 // function calls back into this module object. The ordering is important
136 // here because symbol files can require the module object file. So we tear
137 // down the symbol file first, then the object file.
138 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000139 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
141
142
Greg Clayton60830262011-02-04 18:53:10 +0000143const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144Module::GetUUID()
145{
146 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000147 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 {
149 ObjectFile * obj_file = GetObjectFile ();
150
151 if (obj_file != NULL)
152 {
153 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000154 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 }
156 }
157 return m_uuid;
158}
159
Greg Clayton6beaaa62011-01-17 03:46:26 +0000160ClangASTContext &
161Module::GetClangASTContext ()
162{
163 Mutex::Locker locker (m_mutex);
164 if (m_did_init_ast == false)
165 {
166 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000167 ArchSpec object_arch;
168 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000169 {
170 m_did_init_ast = true;
Greg Clayton514487e2011-02-15 21:59:32 +0000171 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000172 }
173 }
174 return m_ast;
175}
176
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177void
178Module::ParseAllDebugSymbols()
179{
180 Mutex::Locker locker (m_mutex);
181 uint32_t num_comp_units = GetNumCompileUnits();
182 if (num_comp_units == 0)
183 return;
184
Greg Claytona2eee182011-09-17 07:23:18 +0000185 SymbolContext sc;
186 sc.module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 uint32_t cu_idx;
188 SymbolVendor *symbols = GetSymbolVendor ();
189
190 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
191 {
192 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
193 if (sc.comp_unit)
194 {
195 sc.function = NULL;
196 symbols->ParseVariablesForContext(sc);
197
198 symbols->ParseCompileUnitFunctions(sc);
199
200 uint32_t func_idx;
201 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
202 {
203 symbols->ParseFunctionBlocks(sc);
204
205 // Parse the variables for this function and all its blocks
206 symbols->ParseVariablesForContext(sc);
207 }
208
209
210 // Parse all types for this compile unit
211 sc.function = NULL;
212 symbols->ParseTypes(sc);
213 }
214 }
215}
216
217void
218Module::CalculateSymbolContext(SymbolContext* sc)
219{
Greg Claytona2eee182011-09-17 07:23:18 +0000220 sc->module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221}
222
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000223Module *
224Module::CalculateSymbolContextModule ()
225{
226 return this;
227}
228
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229void
230Module::DumpSymbolContext(Stream *s)
231{
Jason Molendafd54b362011-09-20 21:44:10 +0000232 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
235uint32_t
236Module::GetNumCompileUnits()
237{
238 Mutex::Locker locker (m_mutex);
239 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
240 SymbolVendor *symbols = GetSymbolVendor ();
241 if (symbols)
242 return symbols->GetNumCompileUnits();
243 return 0;
244}
245
246CompUnitSP
247Module::GetCompileUnitAtIndex (uint32_t index)
248{
249 Mutex::Locker locker (m_mutex);
250 uint32_t num_comp_units = GetNumCompileUnits ();
251 CompUnitSP cu_sp;
252
253 if (index < num_comp_units)
254 {
255 SymbolVendor *symbols = GetSymbolVendor ();
256 if (symbols)
257 cu_sp = symbols->GetCompileUnitAtIndex(index);
258 }
259 return cu_sp;
260}
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262bool
263Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
264{
265 Mutex::Locker locker (m_mutex);
266 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
267 ObjectFile* ofile = GetObjectFile();
268 if (ofile)
269 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
270 return false;
271}
272
273uint32_t
274Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
275{
276 Mutex::Locker locker (m_mutex);
277 uint32_t resolved_flags = 0;
278
279 // Clear the result symbol context in case we don't find anything
280 sc.Clear();
281
282 // Get the section from the section/offset address.
283 const Section *section = so_addr.GetSection();
284
285 // Make sure the section matches this module before we try and match anything
286 if (section && section->GetModule() == this)
287 {
288 // If the section offset based address resolved itself, then this
289 // is the right module.
Greg Claytona2eee182011-09-17 07:23:18 +0000290 sc.module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 resolved_flags |= eSymbolContextModule;
292
293 // Resolve the compile unit, function, block, line table or line
294 // entry if requested.
295 if (resolve_scope & eSymbolContextCompUnit ||
296 resolve_scope & eSymbolContextFunction ||
297 resolve_scope & eSymbolContextBlock ||
298 resolve_scope & eSymbolContextLineEntry )
299 {
300 SymbolVendor *symbols = GetSymbolVendor ();
301 if (symbols)
302 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
303 }
304
Jim Ingham680e1772010-08-31 23:51:36 +0000305 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
306 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 {
308 ObjectFile* ofile = GetObjectFile();
309 if (ofile)
310 {
311 Symtab *symtab = ofile->GetSymtab();
312 if (symtab)
313 {
314 if (so_addr.IsSectionOffset())
315 {
316 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
317 if (sc.symbol)
318 resolved_flags |= eSymbolContextSymbol;
319 }
320 }
321 }
322 }
323 }
324 return resolved_flags;
325}
326
327uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000328Module::ResolveSymbolContextForFilePath
329(
330 const char *file_path,
331 uint32_t line,
332 bool check_inlines,
333 uint32_t resolve_scope,
334 SymbolContextList& sc_list
335)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336{
Greg Clayton274060b2010-10-20 20:54:39 +0000337 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
339}
340
341uint32_t
342Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
343{
344 Mutex::Locker locker (m_mutex);
345 Timer scoped_timer(__PRETTY_FUNCTION__,
346 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
347 file_spec.GetDirectory().AsCString(""),
348 file_spec.GetDirectory() ? "/" : "",
349 file_spec.GetFilename().AsCString(""),
350 line,
351 check_inlines ? "yes" : "no",
352 resolve_scope);
353
354 const uint32_t initial_count = sc_list.GetSize();
355
356 SymbolVendor *symbols = GetSymbolVendor ();
357 if (symbols)
358 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
359
360 return sc_list.GetSize() - initial_count;
361}
362
363
364uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000365Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366{
367 SymbolVendor *symbols = GetSymbolVendor ();
368 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000369 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 return 0;
371}
372uint32_t
373Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
374{
375 SymbolVendor *symbols = GetSymbolVendor ();
376 if (symbols)
377 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
378 return 0;
379}
380
381uint32_t
Greg Clayton644247c2011-07-07 01:59:51 +0000382Module::FindCompileUnits (const FileSpec &path,
383 bool append,
384 SymbolContextList &sc_list)
385{
386 if (!append)
387 sc_list.Clear();
388
389 const uint32_t start_size = sc_list.GetSize();
390 const uint32_t num_compile_units = GetNumCompileUnits();
391 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000392 sc.module_sp = this;
Greg Clayton644247c2011-07-07 01:59:51 +0000393 const bool compare_directory = path.GetDirectory();
394 for (uint32_t i=0; i<num_compile_units; ++i)
395 {
396 sc.comp_unit = GetCompileUnitAtIndex(i).get();
397 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
398 sc_list.Append(sc);
399 }
400 return sc_list.GetSize() - start_size;
401}
402
403uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000404Module::FindFunctions (const ConstString &name,
405 const ClangNamespaceDecl *namespace_decl,
Greg Clayton931180e2011-01-27 06:44:37 +0000406 uint32_t name_type_mask,
407 bool include_symbols,
408 bool append,
409 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410{
Greg Clayton931180e2011-01-27 06:44:37 +0000411 if (!append)
412 sc_list.Clear();
413
414 const uint32_t start_size = sc_list.GetSize();
415
416 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 SymbolVendor *symbols = GetSymbolVendor ();
418 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000419 symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000420
421 // Now check our symbol table for symbols that are code symbols if requested
422 if (include_symbols)
423 {
424 ObjectFile *objfile = GetObjectFile();
425 if (objfile)
426 {
427 Symtab *symtab = objfile->GetSymtab();
428 if (symtab)
429 {
430 std::vector<uint32_t> symbol_indexes;
431 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
432 const uint32_t num_matches = symbol_indexes.size();
433 if (num_matches)
434 {
Greg Clayton357132e2011-03-26 19:14:58 +0000435 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000436 SymbolContext sc(this);
437 for (uint32_t i=0; i<num_matches; i++)
438 {
439 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000440 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000441 }
442 }
443 }
444 }
445 }
446 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
449uint32_t
Greg Clayton931180e2011-01-27 06:44:37 +0000450Module::FindFunctions (const RegularExpression& regex,
451 bool include_symbols,
452 bool append,
453 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454{
Greg Clayton931180e2011-01-27 06:44:37 +0000455 if (!append)
456 sc_list.Clear();
457
458 const uint32_t start_size = sc_list.GetSize();
459
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 SymbolVendor *symbols = GetSymbolVendor ();
461 if (symbols)
Jim Ingham832332d2011-05-18 05:02:10 +0000462 symbols->FindFunctions(regex, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000463 // Now check our symbol table for symbols that are code symbols if requested
464 if (include_symbols)
465 {
466 ObjectFile *objfile = GetObjectFile();
467 if (objfile)
468 {
469 Symtab *symtab = objfile->GetSymtab();
470 if (symtab)
471 {
472 std::vector<uint32_t> symbol_indexes;
473 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
474 const uint32_t num_matches = symbol_indexes.size();
475 if (num_matches)
476 {
Greg Clayton357132e2011-03-26 19:14:58 +0000477 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000478 SymbolContext sc(this);
479 for (uint32_t i=0; i<num_matches; i++)
480 {
481 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000482 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000483 }
484 }
485 }
486 }
487 }
488 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
Greg Clayton3504eee2010-08-03 01:26:16 +0000491uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000492Module::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 +0000493{
494 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
495 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
496 {
497 SymbolVendor *symbols = GetSymbolVendor ();
498 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000499 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000500 }
501 return 0;
502}
503
Enrico Granata6f3533f2011-07-29 19:53:35 +0000504// depending on implementation details, type lookup might fail because of
505// embedded spurious namespace:: prefixes. this call strips them, paying
506// attention to the fact that a type might have namespace'd type names as
507// arguments to templates, and those must not be stripped off
508static const char*
509StripTypeName(const char* name_cstr)
510{
Johnny Chenc6770762011-12-14 01:43:31 +0000511 // Protect against null c string.
512 if (!name_cstr)
513 return name_cstr;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000514 const char* skip_namespace = strstr(name_cstr, "::");
515 const char* template_arg_char = strchr(name_cstr, '<');
516 while (skip_namespace != NULL)
517 {
518 if (template_arg_char != NULL &&
519 skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
520 break;
521 name_cstr = skip_namespace+2;
522 skip_namespace = strstr(name_cstr, "::");
523 }
524 return name_cstr;
525}
526
527uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000528Module::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 +0000529{
Sean Callanan213fdb82011-10-13 01:49:10 +0000530 uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000531
532 if (retval == 0)
533 {
Jim Ingham50b3d502012-01-12 22:35:29 +0000534 const char *orig_name = name.GetCString();
535 const char *stripped = StripTypeName(orig_name);
536 // Only do this lookup if StripTypeName has stripped the name:
537 if (stripped != orig_name)
538 return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
539 else
540 return 0;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000541 }
542 else
543 return retval;
544
545}
546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547//uint32_t
548//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
549//{
550// Timer scoped_timer(__PRETTY_FUNCTION__);
551// SymbolVendor *symbols = GetSymbolVendor ();
552// if (symbols)
553// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
554// return 0;
555//
556//}
557
558SymbolVendor*
559Module::GetSymbolVendor (bool can_create)
560{
561 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000562 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 {
564 ObjectFile *obj_file = GetObjectFile ();
565 if (obj_file != NULL)
566 {
567 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
568 m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
Greg Claytone83e7312010-09-07 23:40:05 +0000569 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 }
571 }
572 return m_symfile_ap.get();
573}
574
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575void
576Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
577{
578 // Container objects whose paths do not specify a file directly can call
579 // this function to correct the file and object names.
580 m_file = file;
581 m_mod_time = file.GetModificationTime();
582 m_object_name = object_name;
583}
584
585const ArchSpec&
586Module::GetArchitecture () const
587{
588 return m_arch;
589}
590
591void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000592Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000593{
594 Mutex::Locker locker (m_mutex);
595
Greg Claytonc982b3d2011-11-28 01:45:00 +0000596 if (level >= eDescriptionLevelFull)
597 {
598 if (m_arch.IsValid())
599 s->Printf("(%s) ", m_arch.GetArchitectureName());
600 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000601
Greg Claytonc982b3d2011-11-28 01:45:00 +0000602 if (level == eDescriptionLevelBrief)
603 {
604 const char *filename = m_file.GetFilename().GetCString();
605 if (filename)
606 s->PutCString (filename);
607 }
608 else
609 {
610 char path[PATH_MAX];
611 if (m_file.GetPath(path, sizeof(path)))
612 s->PutCString(path);
613 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000614
615 const char *object_name = m_object_name.GetCString();
616 if (object_name)
617 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000618}
619
620void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000621Module::ReportError (const char *format, ...)
622{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000623 if (format && format[0])
624 {
625 StreamString strm;
626 strm.PutCString("error: ");
627 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000628 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000629 va_list args;
630 va_start (args, format);
631 strm.PrintfVarArg(format, args);
632 va_end (args);
633
634 const int format_len = strlen(format);
635 if (format_len > 0)
636 {
637 const char last_char = format[format_len-1];
638 if (last_char != '\n' || last_char != '\r')
639 strm.EOL();
640 }
641 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
642
643 }
644}
645
646void
647Module::ReportErrorIfModifyDetected (const char *format, ...)
648{
649 if (!GetModified(true) && GetModified(false))
650 {
651 if (format)
652 {
653 StreamString strm;
654 strm.PutCString("error: the object file ");
655 GetDescription(&strm, lldb::eDescriptionLevelFull);
656 strm.PutCString (" has been modified\n");
657
658 va_list args;
659 va_start (args, format);
660 strm.PrintfVarArg(format, args);
661 va_end (args);
662
663 const int format_len = strlen(format);
664 if (format_len > 0)
665 {
666 const char last_char = format[format_len-1];
667 if (last_char != '\n' || last_char != '\r')
668 strm.EOL();
669 }
670 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
671 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
672 }
673 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000674}
675
676void
677Module::ReportWarning (const char *format, ...)
678{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000679 if (format && format[0])
680 {
681 StreamString strm;
682 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +0000683 GetDescription(&strm, lldb::eDescriptionLevelFull);
684 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000685
686 va_list args;
687 va_start (args, format);
688 strm.PrintfVarArg(format, args);
689 va_end (args);
690
691 const int format_len = strlen(format);
692 if (format_len > 0)
693 {
694 const char last_char = format[format_len-1];
695 if (last_char != '\n' || last_char != '\r')
696 strm.EOL();
697 }
698 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
699 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000700}
701
702void
703Module::LogMessage (Log *log, const char *format, ...)
704{
705 if (log)
706 {
707 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +0000708 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +0000709 log_message.PutCString (": ");
710 va_list args;
711 va_start (args, format);
712 log_message.PrintfVarArg (format, args);
713 va_end (args);
714 log->PutCString(log_message.GetString().c_str());
715 }
716}
717
Greg Claytone38a5ed2012-01-05 03:57:59 +0000718bool
719Module::GetModified (bool use_cached_only)
720{
721 if (m_was_modified == false && use_cached_only == false)
722 {
723 TimeValue curr_mod_time (m_file.GetModificationTime());
724 m_was_modified = curr_mod_time != m_mod_time;
725 }
726 return m_was_modified;
727}
728
729bool
730Module::SetModified (bool b)
731{
732 const bool prev_value = m_was_modified;
733 m_was_modified = b;
734 return prev_value;
735}
736
737
Greg Claytonc982b3d2011-11-28 01:45:00 +0000738void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739Module::Dump(Stream *s)
740{
741 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +0000742 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 s->Indent();
744 s->Printf("Module %s/%s%s%s%s\n",
745 m_file.GetDirectory().AsCString(),
746 m_file.GetFilename().AsCString(),
747 m_object_name ? "(" : "",
748 m_object_name ? m_object_name.GetCString() : "",
749 m_object_name ? ")" : "");
750
751 s->IndentMore();
752 ObjectFile *objfile = GetObjectFile ();
753
754 if (objfile)
755 objfile->Dump(s);
756
757 SymbolVendor *symbols = GetSymbolVendor ();
758
759 if (symbols)
760 symbols->Dump(s);
761
762 s->IndentLess();
763}
764
765
766TypeList*
767Module::GetTypeList ()
768{
769 SymbolVendor *symbols = GetSymbolVendor ();
770 if (symbols)
771 return &symbols->GetTypeList();
772 return NULL;
773}
774
775const ConstString &
776Module::GetObjectName() const
777{
778 return m_object_name;
779}
780
781ObjectFile *
782Module::GetObjectFile()
783{
784 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000785 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 {
Greg Claytone83e7312010-09-07 23:40:05 +0000787 m_did_load_objfile = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 Timer scoped_timer(__PRETTY_FUNCTION__,
789 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton44435ed2012-01-12 05:25:17 +0000790 DataBufferSP file_data_sp;
791 m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp);
Greg Clayton593577a2011-09-21 03:57:31 +0000792 if (m_objfile_sp)
793 {
794 // Once we get the object file, update our module with the object file's
795 // architecture since it might differ in vendor/os if some parts were
796 // unknown.
797 m_objfile_sp->GetArchitecture (m_arch);
798 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 }
Greg Clayton762f7132011-09-18 18:59:15 +0000800 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801}
802
803
804const Symbol *
805Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
806{
807 Timer scoped_timer(__PRETTY_FUNCTION__,
808 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
809 name.AsCString(),
810 symbol_type);
811 ObjectFile *objfile = GetObjectFile();
812 if (objfile)
813 {
814 Symtab *symtab = objfile->GetSymtab();
815 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000816 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 }
818 return NULL;
819}
820void
821Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
822{
823 // No need to protect this call using m_mutex all other method calls are
824 // already thread safe.
825
826 size_t num_indices = symbol_indexes.size();
827 if (num_indices > 0)
828 {
829 SymbolContext sc;
830 CalculateSymbolContext (&sc);
831 for (size_t i = 0; i < num_indices; i++)
832 {
833 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
834 if (sc.symbol)
835 sc_list.Append (sc);
836 }
837 }
838}
839
840size_t
Sean Callananb96ff332011-10-13 16:49:47 +0000841Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842{
843 // No need to protect this call using m_mutex all other method calls are
844 // already thread safe.
845
846
847 Timer scoped_timer(__PRETTY_FUNCTION__,
848 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
849 name.AsCString(),
850 symbol_type);
851 const size_t initial_size = sc_list.GetSize();
852 ObjectFile *objfile = GetObjectFile ();
853 if (objfile)
854 {
855 Symtab *symtab = objfile->GetSymtab();
856 if (symtab)
857 {
858 std::vector<uint32_t> symbol_indexes;
859 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
860 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
861 }
862 }
863 return sc_list.GetSize() - initial_size;
864}
865
866size_t
867Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
868{
869 // No need to protect this call using m_mutex all other method calls are
870 // already thread safe.
871
872 Timer scoped_timer(__PRETTY_FUNCTION__,
873 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
874 regex.GetText(),
875 symbol_type);
876 const size_t initial_size = sc_list.GetSize();
877 ObjectFile *objfile = GetObjectFile ();
878 if (objfile)
879 {
880 Symtab *symtab = objfile->GetSymtab();
881 if (symtab)
882 {
883 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000884 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
886 }
887 }
888 return sc_list.GetSize() - initial_size;
889}
890
891const TimeValue &
892Module::GetModificationTime () const
893{
894 return m_mod_time;
895}
Jim Ingham5aee1622010-08-09 23:31:02 +0000896
897bool
898Module::IsExecutable ()
899{
900 if (GetObjectFile() == NULL)
901 return false;
902 else
903 return GetObjectFile()->IsExecutable();
904}
905
Jim Inghamb53cb272011-08-03 01:03:17 +0000906bool
907Module::IsLoadedInTarget (Target *target)
908{
909 ObjectFile *obj_file = GetObjectFile();
910 if (obj_file)
911 {
912 SectionList *sections = obj_file->GetSectionList();
913 if (sections != NULL)
914 {
915 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +0000916 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
917 {
918 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
919 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
920 {
921 return true;
922 }
923 }
924 }
925 }
926 return false;
927}
Jim Ingham5aee1622010-08-09 23:31:02 +0000928bool
929Module::SetArchitecture (const ArchSpec &new_arch)
930{
Greg Clayton64195a22011-02-23 00:35:02 +0000931 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +0000932 {
933 m_arch = new_arch;
934 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000935 }
936 return m_arch == new_arch;
Jim Ingham5aee1622010-08-09 23:31:02 +0000937}
938