blob: 62b25c50015ad990dadf5e622b2035c548cd71d8 [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}
76
77
78
79
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
81 m_mutex (Mutex::eMutexTypeRecursive),
82 m_mod_time (file_spec.GetModificationTime()),
83 m_arch (arch),
84 m_uuid (),
85 m_file (file_spec),
Greg Clayton32e0a752011-03-30 18:16:51 +000086 m_platform_file(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_object_name (),
Greg Clayton8b82f082011-04-12 05:54:46 +000088 m_object_offset (object_offset),
Greg Clayton762f7132011-09-18 18:59:15 +000089 m_objfile_sp (),
Greg Claytone83e7312010-09-07 23:40:05 +000090 m_symfile_ap (),
Greg Clayton6beaaa62011-01-17 03:46:26 +000091 m_ast (),
Greg Claytone83e7312010-09-07 23:40:05 +000092 m_did_load_objfile (false),
93 m_did_load_symbol_vendor (false),
94 m_did_parse_uuid (false),
Greg Clayton6beaaa62011-01-17 03:46:26 +000095 m_did_init_ast (false),
Greg Claytone38a5ed2012-01-05 03:57:59 +000096 m_is_dynamic_loader_module (false),
97 m_was_modified (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098{
Greg Clayton65a03992011-08-09 00:01:09 +000099 // Scope for locker below...
100 {
101 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
102 GetModuleCollection().push_back(this);
103 }
104
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 if (object_name)
106 m_object_name = *object_name;
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000107 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 if (log)
109 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
110 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000111 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 m_file.GetDirectory().AsCString(""),
113 m_file.GetFilename().AsCString(""),
114 m_object_name.IsEmpty() ? "" : "(",
115 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
116 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117}
118
119Module::~Module()
120{
Greg Clayton65a03992011-08-09 00:01:09 +0000121 // Scope for locker below...
122 {
123 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
124 ModuleCollection &modules = GetModuleCollection();
125 ModuleCollection::iterator end = modules.end();
126 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
127 if (pos != end)
128 modules.erase(pos);
129 }
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000130 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 if (log)
132 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
133 this,
Greg Clayton64195a22011-02-23 00:35:02 +0000134 m_arch.GetArchitectureName(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 m_file.GetDirectory().AsCString(""),
136 m_file.GetFilename().AsCString(""),
137 m_object_name.IsEmpty() ? "" : "(",
138 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
139 m_object_name.IsEmpty() ? "" : ")");
Greg Clayton6beaaa62011-01-17 03:46:26 +0000140 // Release any auto pointers before we start tearing down our member
141 // variables since the object file and symbol files might need to make
142 // function calls back into this module object. The ordering is important
143 // here because symbol files can require the module object file. So we tear
144 // down the symbol file first, then the object file.
145 m_symfile_ap.reset();
Greg Clayton762f7132011-09-18 18:59:15 +0000146 m_objfile_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
149
Greg Clayton60830262011-02-04 18:53:10 +0000150const lldb_private::UUID&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151Module::GetUUID()
152{
153 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000154 if (m_did_parse_uuid == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 {
156 ObjectFile * obj_file = GetObjectFile ();
157
158 if (obj_file != NULL)
159 {
160 obj_file->GetUUID(&m_uuid);
Greg Claytone83e7312010-09-07 23:40:05 +0000161 m_did_parse_uuid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 }
163 }
164 return m_uuid;
165}
166
Greg Clayton6beaaa62011-01-17 03:46:26 +0000167ClangASTContext &
168Module::GetClangASTContext ()
169{
170 Mutex::Locker locker (m_mutex);
171 if (m_did_init_ast == false)
172 {
173 ObjectFile * objfile = GetObjectFile();
Greg Clayton514487e2011-02-15 21:59:32 +0000174 ArchSpec object_arch;
175 if (objfile && objfile->GetArchitecture(object_arch))
Greg Clayton6beaaa62011-01-17 03:46:26 +0000176 {
177 m_did_init_ast = true;
Greg Clayton514487e2011-02-15 21:59:32 +0000178 m_ast.SetArchitecture (object_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000179 }
180 }
181 return m_ast;
182}
183
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184void
185Module::ParseAllDebugSymbols()
186{
187 Mutex::Locker locker (m_mutex);
188 uint32_t num_comp_units = GetNumCompileUnits();
189 if (num_comp_units == 0)
190 return;
191
Greg Claytona2eee182011-09-17 07:23:18 +0000192 SymbolContext sc;
193 sc.module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 uint32_t cu_idx;
195 SymbolVendor *symbols = GetSymbolVendor ();
196
197 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
198 {
199 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
200 if (sc.comp_unit)
201 {
202 sc.function = NULL;
203 symbols->ParseVariablesForContext(sc);
204
205 symbols->ParseCompileUnitFunctions(sc);
206
207 uint32_t func_idx;
208 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
209 {
210 symbols->ParseFunctionBlocks(sc);
211
212 // Parse the variables for this function and all its blocks
213 symbols->ParseVariablesForContext(sc);
214 }
215
216
217 // Parse all types for this compile unit
218 sc.function = NULL;
219 symbols->ParseTypes(sc);
220 }
221 }
222}
223
224void
225Module::CalculateSymbolContext(SymbolContext* sc)
226{
Greg Claytona2eee182011-09-17 07:23:18 +0000227 sc->module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000230Module *
231Module::CalculateSymbolContextModule ()
232{
233 return this;
234}
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236void
237Module::DumpSymbolContext(Stream *s)
238{
Jason Molendafd54b362011-09-20 21:44:10 +0000239 s->Printf(", Module{%p}", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
242uint32_t
243Module::GetNumCompileUnits()
244{
245 Mutex::Locker locker (m_mutex);
246 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
247 SymbolVendor *symbols = GetSymbolVendor ();
248 if (symbols)
249 return symbols->GetNumCompileUnits();
250 return 0;
251}
252
253CompUnitSP
254Module::GetCompileUnitAtIndex (uint32_t index)
255{
256 Mutex::Locker locker (m_mutex);
257 uint32_t num_comp_units = GetNumCompileUnits ();
258 CompUnitSP cu_sp;
259
260 if (index < num_comp_units)
261 {
262 SymbolVendor *symbols = GetSymbolVendor ();
263 if (symbols)
264 cu_sp = symbols->GetCompileUnitAtIndex(index);
265 }
266 return cu_sp;
267}
268
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269bool
270Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
271{
272 Mutex::Locker locker (m_mutex);
273 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
274 ObjectFile* ofile = GetObjectFile();
275 if (ofile)
276 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
277 return false;
278}
279
280uint32_t
281Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
282{
283 Mutex::Locker locker (m_mutex);
284 uint32_t resolved_flags = 0;
285
286 // Clear the result symbol context in case we don't find anything
287 sc.Clear();
288
289 // Get the section from the section/offset address.
290 const Section *section = so_addr.GetSection();
291
292 // Make sure the section matches this module before we try and match anything
293 if (section && section->GetModule() == this)
294 {
295 // If the section offset based address resolved itself, then this
296 // is the right module.
Greg Claytona2eee182011-09-17 07:23:18 +0000297 sc.module_sp = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 resolved_flags |= eSymbolContextModule;
299
300 // Resolve the compile unit, function, block, line table or line
301 // entry if requested.
302 if (resolve_scope & eSymbolContextCompUnit ||
303 resolve_scope & eSymbolContextFunction ||
304 resolve_scope & eSymbolContextBlock ||
305 resolve_scope & eSymbolContextLineEntry )
306 {
307 SymbolVendor *symbols = GetSymbolVendor ();
308 if (symbols)
309 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
310 }
311
Jim Ingham680e1772010-08-31 23:51:36 +0000312 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
313 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 {
315 ObjectFile* ofile = GetObjectFile();
316 if (ofile)
317 {
318 Symtab *symtab = ofile->GetSymtab();
319 if (symtab)
320 {
321 if (so_addr.IsSectionOffset())
322 {
323 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
324 if (sc.symbol)
325 resolved_flags |= eSymbolContextSymbol;
326 }
327 }
328 }
329 }
330 }
331 return resolved_flags;
332}
333
334uint32_t
Greg Clayton274060b2010-10-20 20:54:39 +0000335Module::ResolveSymbolContextForFilePath
336(
337 const char *file_path,
338 uint32_t line,
339 bool check_inlines,
340 uint32_t resolve_scope,
341 SymbolContextList& sc_list
342)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343{
Greg Clayton274060b2010-10-20 20:54:39 +0000344 FileSpec file_spec(file_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
346}
347
348uint32_t
349Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
350{
351 Mutex::Locker locker (m_mutex);
352 Timer scoped_timer(__PRETTY_FUNCTION__,
353 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
354 file_spec.GetDirectory().AsCString(""),
355 file_spec.GetDirectory() ? "/" : "",
356 file_spec.GetFilename().AsCString(""),
357 line,
358 check_inlines ? "yes" : "no",
359 resolve_scope);
360
361 const uint32_t initial_count = sc_list.GetSize();
362
363 SymbolVendor *symbols = GetSymbolVendor ();
364 if (symbols)
365 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
366
367 return sc_list.GetSize() - initial_count;
368}
369
370
371uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000372Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373{
374 SymbolVendor *symbols = GetSymbolVendor ();
375 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000376 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 return 0;
378}
379uint32_t
380Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
381{
382 SymbolVendor *symbols = GetSymbolVendor ();
383 if (symbols)
384 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
385 return 0;
386}
387
388uint32_t
Greg Clayton644247c2011-07-07 01:59:51 +0000389Module::FindCompileUnits (const FileSpec &path,
390 bool append,
391 SymbolContextList &sc_list)
392{
393 if (!append)
394 sc_list.Clear();
395
396 const uint32_t start_size = sc_list.GetSize();
397 const uint32_t num_compile_units = GetNumCompileUnits();
398 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000399 sc.module_sp = this;
Greg Clayton644247c2011-07-07 01:59:51 +0000400 const bool compare_directory = path.GetDirectory();
401 for (uint32_t i=0; i<num_compile_units; ++i)
402 {
403 sc.comp_unit = GetCompileUnitAtIndex(i).get();
404 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
405 sc_list.Append(sc);
406 }
407 return sc_list.GetSize() - start_size;
408}
409
410uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000411Module::FindFunctions (const ConstString &name,
412 const ClangNamespaceDecl *namespace_decl,
Greg Clayton931180e2011-01-27 06:44:37 +0000413 uint32_t name_type_mask,
414 bool include_symbols,
415 bool append,
416 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417{
Greg Clayton931180e2011-01-27 06:44:37 +0000418 if (!append)
419 sc_list.Clear();
420
421 const uint32_t start_size = sc_list.GetSize();
422
423 // Find all the functions (not symbols, but debug information functions...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 SymbolVendor *symbols = GetSymbolVendor ();
425 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000426 symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000427
428 // Now check our symbol table for symbols that are code symbols if requested
429 if (include_symbols)
430 {
431 ObjectFile *objfile = GetObjectFile();
432 if (objfile)
433 {
434 Symtab *symtab = objfile->GetSymtab();
435 if (symtab)
436 {
437 std::vector<uint32_t> symbol_indexes;
438 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
439 const uint32_t num_matches = symbol_indexes.size();
440 if (num_matches)
441 {
Greg Clayton357132e2011-03-26 19:14:58 +0000442 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000443 SymbolContext sc(this);
444 for (uint32_t i=0; i<num_matches; i++)
445 {
446 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000447 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000448 }
449 }
450 }
451 }
452 }
453 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
456uint32_t
Greg Clayton931180e2011-01-27 06:44:37 +0000457Module::FindFunctions (const RegularExpression& regex,
458 bool include_symbols,
459 bool append,
460 SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461{
Greg Clayton931180e2011-01-27 06:44:37 +0000462 if (!append)
463 sc_list.Clear();
464
465 const uint32_t start_size = sc_list.GetSize();
466
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 SymbolVendor *symbols = GetSymbolVendor ();
468 if (symbols)
Jim Ingham832332d2011-05-18 05:02:10 +0000469 symbols->FindFunctions(regex, append, sc_list);
Greg Clayton931180e2011-01-27 06:44:37 +0000470 // Now check our symbol table for symbols that are code symbols if requested
471 if (include_symbols)
472 {
473 ObjectFile *objfile = GetObjectFile();
474 if (objfile)
475 {
476 Symtab *symtab = objfile->GetSymtab();
477 if (symtab)
478 {
479 std::vector<uint32_t> symbol_indexes;
480 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
481 const uint32_t num_matches = symbol_indexes.size();
482 if (num_matches)
483 {
Greg Clayton357132e2011-03-26 19:14:58 +0000484 const bool merge_symbol_into_function = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000485 SymbolContext sc(this);
486 for (uint32_t i=0; i<num_matches; i++)
487 {
488 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton357132e2011-03-26 19:14:58 +0000489 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton931180e2011-01-27 06:44:37 +0000490 }
491 }
492 }
493 }
494 }
495 return sc_list.GetSize() - start_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496}
497
Greg Clayton3504eee2010-08-03 01:26:16 +0000498uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000499Module::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 +0000500{
501 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
502 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
503 {
504 SymbolVendor *symbols = GetSymbolVendor ();
505 if (symbols)
Sean Callanan213fdb82011-10-13 01:49:10 +0000506 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton3504eee2010-08-03 01:26:16 +0000507 }
508 return 0;
509}
510
Enrico Granata6f3533f2011-07-29 19:53:35 +0000511// depending on implementation details, type lookup might fail because of
512// embedded spurious namespace:: prefixes. this call strips them, paying
513// attention to the fact that a type might have namespace'd type names as
514// arguments to templates, and those must not be stripped off
515static const char*
516StripTypeName(const char* name_cstr)
517{
Johnny Chenc6770762011-12-14 01:43:31 +0000518 // Protect against null c string.
519 if (!name_cstr)
520 return name_cstr;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000521 const char* skip_namespace = strstr(name_cstr, "::");
522 const char* template_arg_char = strchr(name_cstr, '<');
523 while (skip_namespace != NULL)
524 {
525 if (template_arg_char != NULL &&
526 skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
527 break;
528 name_cstr = skip_namespace+2;
529 skip_namespace = strstr(name_cstr, "::");
530 }
531 return name_cstr;
532}
533
534uint32_t
Sean Callananb6d70eb2011-10-12 02:08:07 +0000535Module::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 +0000536{
Sean Callanan213fdb82011-10-13 01:49:10 +0000537 uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
Enrico Granata6f3533f2011-07-29 19:53:35 +0000538
539 if (retval == 0)
540 {
Jim Ingham50b3d502012-01-12 22:35:29 +0000541 const char *orig_name = name.GetCString();
542 const char *stripped = StripTypeName(orig_name);
543 // Only do this lookup if StripTypeName has stripped the name:
544 if (stripped != orig_name)
545 return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
546 else
547 return 0;
Enrico Granata6f3533f2011-07-29 19:53:35 +0000548 }
549 else
550 return retval;
551
552}
553
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554//uint32_t
555//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
556//{
557// Timer scoped_timer(__PRETTY_FUNCTION__);
558// SymbolVendor *symbols = GetSymbolVendor ();
559// if (symbols)
560// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
561// return 0;
562//
563//}
564
565SymbolVendor*
566Module::GetSymbolVendor (bool can_create)
567{
568 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000569 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 {
571 ObjectFile *obj_file = GetObjectFile ();
572 if (obj_file != NULL)
573 {
574 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
575 m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
Greg Claytone83e7312010-09-07 23:40:05 +0000576 m_did_load_symbol_vendor = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 }
578 }
579 return m_symfile_ap.get();
580}
581
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582void
583Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
584{
585 // Container objects whose paths do not specify a file directly can call
586 // this function to correct the file and object names.
587 m_file = file;
588 m_mod_time = file.GetModificationTime();
589 m_object_name = object_name;
590}
591
592const ArchSpec&
593Module::GetArchitecture () const
594{
595 return m_arch;
596}
597
598void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000599Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000600{
601 Mutex::Locker locker (m_mutex);
602
Greg Claytonc982b3d2011-11-28 01:45:00 +0000603 if (level >= eDescriptionLevelFull)
604 {
605 if (m_arch.IsValid())
606 s->Printf("(%s) ", m_arch.GetArchitectureName());
607 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000608
Greg Claytonc982b3d2011-11-28 01:45:00 +0000609 if (level == eDescriptionLevelBrief)
610 {
611 const char *filename = m_file.GetFilename().GetCString();
612 if (filename)
613 s->PutCString (filename);
614 }
615 else
616 {
617 char path[PATH_MAX];
618 if (m_file.GetPath(path, sizeof(path)))
619 s->PutCString(path);
620 }
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000621
622 const char *object_name = m_object_name.GetCString();
623 if (object_name)
624 s->Printf("(%s)", object_name);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000625}
626
627void
Greg Claytonc982b3d2011-11-28 01:45:00 +0000628Module::ReportError (const char *format, ...)
629{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000630 if (format && format[0])
631 {
632 StreamString strm;
633 strm.PutCString("error: ");
634 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton8b353342012-01-11 01:59:18 +0000635 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000636 va_list args;
637 va_start (args, format);
638 strm.PrintfVarArg(format, args);
639 va_end (args);
640
641 const int format_len = strlen(format);
642 if (format_len > 0)
643 {
644 const char last_char = format[format_len-1];
645 if (last_char != '\n' || last_char != '\r')
646 strm.EOL();
647 }
648 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
649
650 }
651}
652
653void
654Module::ReportErrorIfModifyDetected (const char *format, ...)
655{
656 if (!GetModified(true) && GetModified(false))
657 {
658 if (format)
659 {
660 StreamString strm;
661 strm.PutCString("error: the object file ");
662 GetDescription(&strm, lldb::eDescriptionLevelFull);
663 strm.PutCString (" has been modified\n");
664
665 va_list args;
666 va_start (args, format);
667 strm.PrintfVarArg(format, args);
668 va_end (args);
669
670 const int format_len = strlen(format);
671 if (format_len > 0)
672 {
673 const char last_char = format[format_len-1];
674 if (last_char != '\n' || last_char != '\r')
675 strm.EOL();
676 }
677 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
678 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
679 }
680 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000681}
682
683void
684Module::ReportWarning (const char *format, ...)
685{
Greg Claytone38a5ed2012-01-05 03:57:59 +0000686 if (format && format[0])
687 {
688 StreamString strm;
689 strm.PutCString("warning: ");
Greg Clayton8b353342012-01-11 01:59:18 +0000690 GetDescription(&strm, lldb::eDescriptionLevelFull);
691 strm.PutChar (' ');
Greg Claytone38a5ed2012-01-05 03:57:59 +0000692
693 va_list args;
694 va_start (args, format);
695 strm.PrintfVarArg(format, args);
696 va_end (args);
697
698 const int format_len = strlen(format);
699 if (format_len > 0)
700 {
701 const char last_char = format[format_len-1];
702 if (last_char != '\n' || last_char != '\r')
703 strm.EOL();
704 }
705 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
706 }
Greg Claytonc982b3d2011-11-28 01:45:00 +0000707}
708
709void
710Module::LogMessage (Log *log, const char *format, ...)
711{
712 if (log)
713 {
714 StreamString log_message;
Greg Clayton8b353342012-01-11 01:59:18 +0000715 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Claytonc982b3d2011-11-28 01:45:00 +0000716 log_message.PutCString (": ");
717 va_list args;
718 va_start (args, format);
719 log_message.PrintfVarArg (format, args);
720 va_end (args);
721 log->PutCString(log_message.GetString().c_str());
722 }
723}
724
Greg Claytone38a5ed2012-01-05 03:57:59 +0000725bool
726Module::GetModified (bool use_cached_only)
727{
728 if (m_was_modified == false && use_cached_only == false)
729 {
730 TimeValue curr_mod_time (m_file.GetModificationTime());
731 m_was_modified = curr_mod_time != m_mod_time;
732 }
733 return m_was_modified;
734}
735
736bool
737Module::SetModified (bool b)
738{
739 const bool prev_value = m_was_modified;
740 m_was_modified = b;
741 return prev_value;
742}
743
744
Greg Claytonc982b3d2011-11-28 01:45:00 +0000745void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746Module::Dump(Stream *s)
747{
748 Mutex::Locker locker (m_mutex);
Greg Clayton89411422010-10-08 00:21:05 +0000749 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 s->Indent();
751 s->Printf("Module %s/%s%s%s%s\n",
752 m_file.GetDirectory().AsCString(),
753 m_file.GetFilename().AsCString(),
754 m_object_name ? "(" : "",
755 m_object_name ? m_object_name.GetCString() : "",
756 m_object_name ? ")" : "");
757
758 s->IndentMore();
759 ObjectFile *objfile = GetObjectFile ();
760
761 if (objfile)
762 objfile->Dump(s);
763
764 SymbolVendor *symbols = GetSymbolVendor ();
765
766 if (symbols)
767 symbols->Dump(s);
768
769 s->IndentLess();
770}
771
772
773TypeList*
774Module::GetTypeList ()
775{
776 SymbolVendor *symbols = GetSymbolVendor ();
777 if (symbols)
778 return &symbols->GetTypeList();
779 return NULL;
780}
781
782const ConstString &
783Module::GetObjectName() const
784{
785 return m_object_name;
786}
787
788ObjectFile *
789Module::GetObjectFile()
790{
791 Mutex::Locker locker (m_mutex);
Greg Claytone83e7312010-09-07 23:40:05 +0000792 if (m_did_load_objfile == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 {
Greg Claytone83e7312010-09-07 23:40:05 +0000794 m_did_load_objfile = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 Timer scoped_timer(__PRETTY_FUNCTION__,
796 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Clayton44435ed2012-01-12 05:25:17 +0000797 DataBufferSP file_data_sp;
798 m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp);
Greg Clayton593577a2011-09-21 03:57:31 +0000799 if (m_objfile_sp)
800 {
801 // Once we get the object file, update our module with the object file's
802 // architecture since it might differ in vendor/os if some parts were
803 // unknown.
804 m_objfile_sp->GetArchitecture (m_arch);
805 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 }
Greg Clayton762f7132011-09-18 18:59:15 +0000807 return m_objfile_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808}
809
810
811const Symbol *
812Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
813{
814 Timer scoped_timer(__PRETTY_FUNCTION__,
815 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
816 name.AsCString(),
817 symbol_type);
818 ObjectFile *objfile = GetObjectFile();
819 if (objfile)
820 {
821 Symtab *symtab = objfile->GetSymtab();
822 if (symtab)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000823 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824 }
825 return NULL;
826}
827void
828Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
829{
830 // No need to protect this call using m_mutex all other method calls are
831 // already thread safe.
832
833 size_t num_indices = symbol_indexes.size();
834 if (num_indices > 0)
835 {
836 SymbolContext sc;
837 CalculateSymbolContext (&sc);
838 for (size_t i = 0; i < num_indices; i++)
839 {
840 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
841 if (sc.symbol)
842 sc_list.Append (sc);
843 }
844 }
845}
846
847size_t
Sean Callananb96ff332011-10-13 16:49:47 +0000848Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849{
850 // No need to protect this call using m_mutex all other method calls are
851 // already thread safe.
852
853
854 Timer scoped_timer(__PRETTY_FUNCTION__,
855 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
856 name.AsCString(),
857 symbol_type);
858 const size_t initial_size = sc_list.GetSize();
859 ObjectFile *objfile = GetObjectFile ();
860 if (objfile)
861 {
862 Symtab *symtab = objfile->GetSymtab();
863 if (symtab)
864 {
865 std::vector<uint32_t> symbol_indexes;
866 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
867 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
868 }
869 }
870 return sc_list.GetSize() - initial_size;
871}
872
873size_t
874Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
875{
876 // No need to protect this call using m_mutex all other method calls are
877 // already thread safe.
878
879 Timer scoped_timer(__PRETTY_FUNCTION__,
880 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
881 regex.GetText(),
882 symbol_type);
883 const size_t initial_size = sc_list.GetSize();
884 ObjectFile *objfile = GetObjectFile ();
885 if (objfile)
886 {
887 Symtab *symtab = objfile->GetSymtab();
888 if (symtab)
889 {
890 std::vector<uint32_t> symbol_indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000891 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
893 }
894 }
895 return sc_list.GetSize() - initial_size;
896}
897
898const TimeValue &
899Module::GetModificationTime () const
900{
901 return m_mod_time;
902}
Jim Ingham5aee1622010-08-09 23:31:02 +0000903
904bool
905Module::IsExecutable ()
906{
907 if (GetObjectFile() == NULL)
908 return false;
909 else
910 return GetObjectFile()->IsExecutable();
911}
912
Jim Inghamb53cb272011-08-03 01:03:17 +0000913bool
914Module::IsLoadedInTarget (Target *target)
915{
916 ObjectFile *obj_file = GetObjectFile();
917 if (obj_file)
918 {
919 SectionList *sections = obj_file->GetSectionList();
920 if (sections != NULL)
921 {
922 size_t num_sections = sections->GetSize();
Jim Inghamb53cb272011-08-03 01:03:17 +0000923 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
924 {
925 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
926 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
927 {
928 return true;
929 }
930 }
931 }
932 }
933 return false;
934}
Jim Ingham5aee1622010-08-09 23:31:02 +0000935bool
936Module::SetArchitecture (const ArchSpec &new_arch)
937{
Greg Clayton64195a22011-02-23 00:35:02 +0000938 if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +0000939 {
940 m_arch = new_arch;
941 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000942 }
943 return m_arch == new_arch;
Jim Ingham5aee1622010-08-09 23:31:02 +0000944}
945