blob: c4d62842f09585d254e066c50d8c3956942a3fa7 [file] [log] [blame]
Chris Lattner24943d22010-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"
Greg Claytonb5a8f142012-02-05 02:38:54 +000011#include "lldb/Core/DataBuffer.h"
12#include "lldb/Core/DataBufferHeap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/ModuleList.h"
15#include "lldb/Core/RegularExpression.h"
Greg Clayton75d8c252011-11-28 01:45:00 +000016#include "lldb/Core/StreamString.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Timer.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000018#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/lldb-private-log.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Symbol/SymbolContext.h"
22#include "lldb/Symbol/SymbolVendor.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
Greg Clayton899025f2011-08-09 00:01:09 +000029// Shared pointers to modules track module lifetimes in
30// targets and in the global module, but this collection
31// will track all module objects that are still alive
32typedef std::vector<Module *> ModuleCollection;
33
34static ModuleCollection &
35GetModuleCollection()
36{
Jim Inghamb67c2a22011-10-31 23:47:10 +000037 // This module collection needs to live past any module, so we could either make it a
38 // shared pointer in each module or just leak is. Since it is only an empty vector by
39 // the time all the modules have gone away, we just leak it for now. If we decide this
40 // is a big problem we can introduce a Finalize method that will tear everything down in
41 // a predictable order.
42
43 static ModuleCollection *g_module_collection = NULL;
44 if (g_module_collection == NULL)
45 g_module_collection = new ModuleCollection();
46
47 return *g_module_collection;
Greg Clayton899025f2011-08-09 00:01:09 +000048}
49
Greg Claytonc149c8b2012-01-27 18:08:35 +000050Mutex *
Greg Clayton899025f2011-08-09 00:01:09 +000051Module::GetAllocationModuleCollectionMutex()
52{
Greg Claytonc149c8b2012-01-27 18:08:35 +000053 // NOTE: The mutex below must be leaked since the global module list in
54 // the ModuleList class will get torn at some point, and we can't know
55 // if it will tear itself down before the "g_module_collection_mutex" below
56 // will. So we leak a Mutex object below to safeguard against that
57
58 static Mutex *g_module_collection_mutex = NULL;
59 if (g_module_collection_mutex == NULL)
60 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
61 return g_module_collection_mutex;
Greg Clayton899025f2011-08-09 00:01:09 +000062}
63
64size_t
65Module::GetNumberAllocatedModules ()
66{
67 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
68 return GetModuleCollection().size();
69}
70
71Module *
72Module::GetAllocatedModuleAtIndex (size_t idx)
73{
74 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
75 ModuleCollection &modules = GetModuleCollection();
76 if (idx < modules.size())
77 return modules[idx];
78 return NULL;
79}
Greg Clayton08205a62012-01-27 18:45:39 +000080#if 0
Greg Clayton899025f2011-08-09 00:01:09 +000081
Greg Clayton08205a62012-01-27 18:45:39 +000082// These functions help us to determine if modules are still loaded, yet don't require that
83// you have a command interpreter and can easily be called from an external debugger.
84namespace lldb {
Greg Clayton899025f2011-08-09 00:01:09 +000085
Greg Clayton08205a62012-01-27 18:45:39 +000086 void
87 ClearModuleInfo (void)
88 {
89 ModuleList::RemoveOrphanSharedModules();
90 }
91
92 void
93 DumpModuleInfo (void)
94 {
95 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
96 ModuleCollection &modules = GetModuleCollection();
97 const size_t count = modules.size();
98 printf ("%s: %zu modules:\n", __PRETTY_FUNCTION__, count);
99 for (size_t i=0; i<count; ++i)
100 {
101
102 StreamString strm;
103 Module *module = modules[i];
104 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
105 module->GetDescription(&strm, eDescriptionLevelFull);
106 printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
107 module,
108 in_shared_module_list,
109 (uint32_t)module->use_count(),
110 strm.GetString().c_str());
111 }
112 }
113}
114
115#endif
Greg Clayton899025f2011-08-09 00:01:09 +0000116
117
Greg Claytonb5a8f142012-02-05 02:38:54 +0000118Module::Module(const FileSpec& file_spec, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) :
119 m_mutex (Mutex::eMutexTypeRecursive),
120 m_mod_time (),
121 m_arch (),
122 m_uuid (),
123 m_file (file_spec),
124 m_platform_file(),
125 m_object_name (),
126 m_object_offset (),
127 m_objfile_sp (),
128 m_symfile_ap (),
129 m_ast (),
130 m_did_load_objfile (false),
131 m_did_load_symbol_vendor (false),
132 m_did_parse_uuid (false),
133 m_did_init_ast (false),
134 m_is_dynamic_loader_module (false),
135 m_was_modified (false)
136{
137 // Scope for locker below...
138 {
139 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
140 GetModuleCollection().push_back(this);
141 }
142 StreamString s;
143 if (m_file.GetFilename())
144 s << m_file.GetFilename();
145 s.Printf("[0x%16.16llx]", header_addr);
146 m_file.GetFilename().SetCString (s.GetData());
147 Mutex::Locker locker (m_mutex);
148 DataBufferSP data_sp;
149 if (process_sp)
150 {
151 m_did_load_objfile = true;
152 std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
153 Error error;
154 const size_t bytes_read = process_sp->ReadMemory (header_addr,
155 data_ap->GetBytes(),
156 data_ap->GetByteSize(),
157 error);
158 if (bytes_read == 512)
159 {
160 data_sp.reset (data_ap.release());
161 m_objfile_sp = ObjectFile::FindPlugin(this, process_sp, header_addr, data_sp);
162 if (m_objfile_sp)
163 {
164 // Once we get the object file, update our module with the object file's
165 // architecture since it might differ in vendor/os if some parts were
166 // unknown.
167 m_objfile_sp->GetArchitecture (m_arch);
168 }
169 }
170 }
171}
172
Chris Lattner24943d22010-06-08 16:52:24 +0000173Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
174 m_mutex (Mutex::eMutexTypeRecursive),
175 m_mod_time (file_spec.GetModificationTime()),
176 m_arch (arch),
177 m_uuid (),
178 m_file (file_spec),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000179 m_platform_file(),
Chris Lattner24943d22010-06-08 16:52:24 +0000180 m_object_name (),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000181 m_object_offset (object_offset),
Greg Claytone40b6422011-09-18 18:59:15 +0000182 m_objfile_sp (),
Greg Clayton236c1c72010-09-07 23:40:05 +0000183 m_symfile_ap (),
Greg Claytonb01000f2011-01-17 03:46:26 +0000184 m_ast (),
Greg Clayton236c1c72010-09-07 23:40:05 +0000185 m_did_load_objfile (false),
186 m_did_load_symbol_vendor (false),
187 m_did_parse_uuid (false),
Greg Claytonb01000f2011-01-17 03:46:26 +0000188 m_did_init_ast (false),
Greg Claytondf6dc882012-01-05 03:57:59 +0000189 m_is_dynamic_loader_module (false),
190 m_was_modified (false)
Chris Lattner24943d22010-06-08 16:52:24 +0000191{
Greg Clayton899025f2011-08-09 00:01:09 +0000192 // Scope for locker below...
193 {
194 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
195 GetModuleCollection().push_back(this);
196 }
197
Chris Lattner24943d22010-06-08 16:52:24 +0000198 if (object_name)
199 m_object_name = *object_name;
Greg Claytone005f2c2010-11-06 01:53:30 +0000200 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000201 if (log)
202 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
203 this,
Greg Clayton940b1032011-02-23 00:35:02 +0000204 m_arch.GetArchitectureName(),
Chris Lattner24943d22010-06-08 16:52:24 +0000205 m_file.GetDirectory().AsCString(""),
206 m_file.GetFilename().AsCString(""),
207 m_object_name.IsEmpty() ? "" : "(",
208 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
209 m_object_name.IsEmpty() ? "" : ")");
Chris Lattner24943d22010-06-08 16:52:24 +0000210}
211
212Module::~Module()
213{
Greg Clayton899025f2011-08-09 00:01:09 +0000214 // Scope for locker below...
215 {
216 Mutex::Locker locker (GetAllocationModuleCollectionMutex());
217 ModuleCollection &modules = GetModuleCollection();
218 ModuleCollection::iterator end = modules.end();
219 ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
220 if (pos != end)
221 modules.erase(pos);
222 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000223 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000224 if (log)
225 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
226 this,
Greg Clayton940b1032011-02-23 00:35:02 +0000227 m_arch.GetArchitectureName(),
Chris Lattner24943d22010-06-08 16:52:24 +0000228 m_file.GetDirectory().AsCString(""),
229 m_file.GetFilename().AsCString(""),
230 m_object_name.IsEmpty() ? "" : "(",
231 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
232 m_object_name.IsEmpty() ? "" : ")");
Greg Claytonb01000f2011-01-17 03:46:26 +0000233 // Release any auto pointers before we start tearing down our member
234 // variables since the object file and symbol files might need to make
235 // function calls back into this module object. The ordering is important
236 // here because symbol files can require the module object file. So we tear
237 // down the symbol file first, then the object file.
238 m_symfile_ap.reset();
Greg Claytone40b6422011-09-18 18:59:15 +0000239 m_objfile_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000240}
241
242
Greg Clayton0467c782011-02-04 18:53:10 +0000243const lldb_private::UUID&
Chris Lattner24943d22010-06-08 16:52:24 +0000244Module::GetUUID()
245{
246 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +0000247 if (m_did_parse_uuid == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000248 {
249 ObjectFile * obj_file = GetObjectFile ();
250
251 if (obj_file != NULL)
252 {
253 obj_file->GetUUID(&m_uuid);
Greg Clayton236c1c72010-09-07 23:40:05 +0000254 m_did_parse_uuid = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000255 }
256 }
257 return m_uuid;
258}
259
Greg Claytonb01000f2011-01-17 03:46:26 +0000260ClangASTContext &
261Module::GetClangASTContext ()
262{
263 Mutex::Locker locker (m_mutex);
264 if (m_did_init_ast == false)
265 {
266 ObjectFile * objfile = GetObjectFile();
Greg Clayton395fc332011-02-15 21:59:32 +0000267 ArchSpec object_arch;
268 if (objfile && objfile->GetArchitecture(object_arch))
Greg Claytonb01000f2011-01-17 03:46:26 +0000269 {
270 m_did_init_ast = true;
Greg Clayton395fc332011-02-15 21:59:32 +0000271 m_ast.SetArchitecture (object_arch);
Greg Claytonb01000f2011-01-17 03:46:26 +0000272 }
273 }
274 return m_ast;
275}
276
Chris Lattner24943d22010-06-08 16:52:24 +0000277void
278Module::ParseAllDebugSymbols()
279{
280 Mutex::Locker locker (m_mutex);
281 uint32_t num_comp_units = GetNumCompileUnits();
282 if (num_comp_units == 0)
283 return;
284
Greg Clayton02e210c2011-09-17 07:23:18 +0000285 SymbolContext sc;
Greg Clayton13d24fb2012-01-29 20:56:30 +0000286 sc.module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000287 uint32_t cu_idx;
288 SymbolVendor *symbols = GetSymbolVendor ();
289
290 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
291 {
292 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
293 if (sc.comp_unit)
294 {
295 sc.function = NULL;
296 symbols->ParseVariablesForContext(sc);
297
298 symbols->ParseCompileUnitFunctions(sc);
299
300 uint32_t func_idx;
301 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
302 {
303 symbols->ParseFunctionBlocks(sc);
304
305 // Parse the variables for this function and all its blocks
306 symbols->ParseVariablesForContext(sc);
307 }
308
309
310 // Parse all types for this compile unit
311 sc.function = NULL;
312 symbols->ParseTypes(sc);
313 }
314 }
315}
316
317void
318Module::CalculateSymbolContext(SymbolContext* sc)
319{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000320 sc->module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000321}
322
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000323Module *
324Module::CalculateSymbolContextModule ()
325{
326 return this;
327}
328
Chris Lattner24943d22010-06-08 16:52:24 +0000329void
330Module::DumpSymbolContext(Stream *s)
331{
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000332 s->Printf(", Module{%p}", this);
Chris Lattner24943d22010-06-08 16:52:24 +0000333}
334
335uint32_t
336Module::GetNumCompileUnits()
337{
338 Mutex::Locker locker (m_mutex);
339 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
340 SymbolVendor *symbols = GetSymbolVendor ();
341 if (symbols)
342 return symbols->GetNumCompileUnits();
343 return 0;
344}
345
346CompUnitSP
347Module::GetCompileUnitAtIndex (uint32_t index)
348{
349 Mutex::Locker locker (m_mutex);
350 uint32_t num_comp_units = GetNumCompileUnits ();
351 CompUnitSP cu_sp;
352
353 if (index < num_comp_units)
354 {
355 SymbolVendor *symbols = GetSymbolVendor ();
356 if (symbols)
357 cu_sp = symbols->GetCompileUnitAtIndex(index);
358 }
359 return cu_sp;
360}
361
Chris Lattner24943d22010-06-08 16:52:24 +0000362bool
363Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
364{
365 Mutex::Locker locker (m_mutex);
366 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
367 ObjectFile* ofile = GetObjectFile();
368 if (ofile)
369 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
370 return false;
371}
372
373uint32_t
374Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
375{
376 Mutex::Locker locker (m_mutex);
377 uint32_t resolved_flags = 0;
378
379 // Clear the result symbol context in case we don't find anything
380 sc.Clear();
381
382 // Get the section from the section/offset address.
383 const Section *section = so_addr.GetSection();
384
385 // Make sure the section matches this module before we try and match anything
386 if (section && section->GetModule() == this)
387 {
388 // If the section offset based address resolved itself, then this
389 // is the right module.
Greg Clayton13d24fb2012-01-29 20:56:30 +0000390 sc.module_sp = shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000391 resolved_flags |= eSymbolContextModule;
392
393 // Resolve the compile unit, function, block, line table or line
394 // entry if requested.
395 if (resolve_scope & eSymbolContextCompUnit ||
396 resolve_scope & eSymbolContextFunction ||
397 resolve_scope & eSymbolContextBlock ||
398 resolve_scope & eSymbolContextLineEntry )
399 {
400 SymbolVendor *symbols = GetSymbolVendor ();
401 if (symbols)
402 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
403 }
404
Jim Inghambddb7892010-08-31 23:51:36 +0000405 // Resolve the symbol if requested, but don't re-look it up if we've already found it.
406 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
Chris Lattner24943d22010-06-08 16:52:24 +0000407 {
408 ObjectFile* ofile = GetObjectFile();
409 if (ofile)
410 {
411 Symtab *symtab = ofile->GetSymtab();
412 if (symtab)
413 {
414 if (so_addr.IsSectionOffset())
415 {
416 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
417 if (sc.symbol)
418 resolved_flags |= eSymbolContextSymbol;
419 }
420 }
421 }
422 }
423 }
424 return resolved_flags;
425}
426
427uint32_t
Greg Clayton537a7a82010-10-20 20:54:39 +0000428Module::ResolveSymbolContextForFilePath
429(
430 const char *file_path,
431 uint32_t line,
432 bool check_inlines,
433 uint32_t resolve_scope,
434 SymbolContextList& sc_list
435)
Chris Lattner24943d22010-06-08 16:52:24 +0000436{
Greg Clayton537a7a82010-10-20 20:54:39 +0000437 FileSpec file_spec(file_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000438 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
439}
440
441uint32_t
442Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
443{
444 Mutex::Locker locker (m_mutex);
445 Timer scoped_timer(__PRETTY_FUNCTION__,
446 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
447 file_spec.GetDirectory().AsCString(""),
448 file_spec.GetDirectory() ? "/" : "",
449 file_spec.GetFilename().AsCString(""),
450 line,
451 check_inlines ? "yes" : "no",
452 resolve_scope);
453
454 const uint32_t initial_count = sc_list.GetSize();
455
456 SymbolVendor *symbols = GetSymbolVendor ();
457 if (symbols)
458 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
459
460 return sc_list.GetSize() - initial_count;
461}
462
463
464uint32_t
Sean Callanan3e80cd92011-10-12 02:08:07 +0000465Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner24943d22010-06-08 16:52:24 +0000466{
467 SymbolVendor *symbols = GetSymbolVendor ();
468 if (symbols)
Sean Callanan0fcec132011-10-13 01:49:10 +0000469 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
Chris Lattner24943d22010-06-08 16:52:24 +0000470 return 0;
471}
472uint32_t
473Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
474{
475 SymbolVendor *symbols = GetSymbolVendor ();
476 if (symbols)
477 return symbols->FindGlobalVariables(regex, append, max_matches, variables);
478 return 0;
479}
480
481uint32_t
Greg Clayton801417e2011-07-07 01:59:51 +0000482Module::FindCompileUnits (const FileSpec &path,
483 bool append,
484 SymbolContextList &sc_list)
485{
486 if (!append)
487 sc_list.Clear();
488
489 const uint32_t start_size = sc_list.GetSize();
490 const uint32_t num_compile_units = GetNumCompileUnits();
491 SymbolContext sc;
Greg Clayton13d24fb2012-01-29 20:56:30 +0000492 sc.module_sp = shared_from_this();
Greg Clayton801417e2011-07-07 01:59:51 +0000493 const bool compare_directory = path.GetDirectory();
494 for (uint32_t i=0; i<num_compile_units; ++i)
495 {
496 sc.comp_unit = GetCompileUnitAtIndex(i).get();
497 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
498 sc_list.Append(sc);
499 }
500 return sc_list.GetSize() - start_size;
501}
502
503uint32_t
Sean Callanan3e80cd92011-10-12 02:08:07 +0000504Module::FindFunctions (const ConstString &name,
505 const ClangNamespaceDecl *namespace_decl,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000506 uint32_t name_type_mask,
507 bool include_symbols,
508 bool append,
509 SymbolContextList& sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000510{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000511 if (!append)
512 sc_list.Clear();
513
514 const uint32_t start_size = sc_list.GetSize();
515
516 // Find all the functions (not symbols, but debug information functions...
Chris Lattner24943d22010-06-08 16:52:24 +0000517 SymbolVendor *symbols = GetSymbolVendor ();
518 if (symbols)
Sean Callanan0fcec132011-10-13 01:49:10 +0000519 symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000520
521 // Now check our symbol table for symbols that are code symbols if requested
522 if (include_symbols)
523 {
524 ObjectFile *objfile = GetObjectFile();
525 if (objfile)
526 {
527 Symtab *symtab = objfile->GetSymtab();
528 if (symtab)
529 {
530 std::vector<uint32_t> symbol_indexes;
531 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
532 const uint32_t num_matches = symbol_indexes.size();
533 if (num_matches)
534 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000535 const bool merge_symbol_into_function = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000536 SymbolContext sc(this);
537 for (uint32_t i=0; i<num_matches; i++)
538 {
539 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton889fbd02011-03-26 19:14:58 +0000540 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000541 }
542 }
543 }
544 }
545 }
546 return sc_list.GetSize() - start_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000547}
548
549uint32_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000550Module::FindFunctions (const RegularExpression& regex,
551 bool include_symbols,
552 bool append,
553 SymbolContextList& sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000554{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000555 if (!append)
556 sc_list.Clear();
557
558 const uint32_t start_size = sc_list.GetSize();
559
Chris Lattner24943d22010-06-08 16:52:24 +0000560 SymbolVendor *symbols = GetSymbolVendor ();
561 if (symbols)
Jim Ingham9935a862011-05-18 05:02:10 +0000562 symbols->FindFunctions(regex, append, sc_list);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000563 // Now check our symbol table for symbols that are code symbols if requested
564 if (include_symbols)
565 {
566 ObjectFile *objfile = GetObjectFile();
567 if (objfile)
568 {
569 Symtab *symtab = objfile->GetSymtab();
570 if (symtab)
571 {
572 std::vector<uint32_t> symbol_indexes;
573 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
574 const uint32_t num_matches = symbol_indexes.size();
575 if (num_matches)
576 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000577 const bool merge_symbol_into_function = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000578 SymbolContext sc(this);
579 for (uint32_t i=0; i<num_matches; i++)
580 {
581 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
Greg Clayton889fbd02011-03-26 19:14:58 +0000582 sc_list.AppendIfUnique (sc, merge_symbol_into_function);
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000583 }
584 }
585 }
586 }
587 }
588 return sc_list.GetSize() - start_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000589}
590
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000591uint32_t
Sean Callanan0fcec132011-10-13 01:49:10 +0000592Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000593{
594 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
595 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
596 {
597 SymbolVendor *symbols = GetSymbolVendor ();
598 if (symbols)
Sean Callanan0fcec132011-10-13 01:49:10 +0000599 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000600 }
601 return 0;
602}
603
Enrico Granata979e20d2011-07-29 19:53:35 +0000604// depending on implementation details, type lookup might fail because of
605// embedded spurious namespace:: prefixes. this call strips them, paying
606// attention to the fact that a type might have namespace'd type names as
607// arguments to templates, and those must not be stripped off
608static const char*
609StripTypeName(const char* name_cstr)
610{
Johnny Chencd186e52011-12-14 01:43:31 +0000611 // Protect against null c string.
612 if (!name_cstr)
613 return name_cstr;
Enrico Granata979e20d2011-07-29 19:53:35 +0000614 const char* skip_namespace = strstr(name_cstr, "::");
615 const char* template_arg_char = strchr(name_cstr, '<');
616 while (skip_namespace != NULL)
617 {
618 if (template_arg_char != NULL &&
619 skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
620 break;
621 name_cstr = skip_namespace+2;
622 skip_namespace = strstr(name_cstr, "::");
623 }
624 return name_cstr;
625}
626
627uint32_t
Sean Callanan3e80cd92011-10-12 02:08:07 +0000628Module::FindTypes (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
Enrico Granata979e20d2011-07-29 19:53:35 +0000629{
Sean Callanan0fcec132011-10-13 01:49:10 +0000630 uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
Enrico Granata979e20d2011-07-29 19:53:35 +0000631
632 if (retval == 0)
633 {
Jim Inghama334c9d2012-01-12 22:35:29 +0000634 const char *orig_name = name.GetCString();
635 const char *stripped = StripTypeName(orig_name);
636 // Only do this lookup if StripTypeName has stripped the name:
637 if (stripped != orig_name)
638 return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
639 else
640 return 0;
Enrico Granata979e20d2011-07-29 19:53:35 +0000641 }
642 else
643 return retval;
644
645}
646
Chris Lattner24943d22010-06-08 16:52:24 +0000647//uint32_t
648//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
649//{
650// Timer scoped_timer(__PRETTY_FUNCTION__);
651// SymbolVendor *symbols = GetSymbolVendor ();
652// if (symbols)
653// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
654// return 0;
655//
656//}
657
658SymbolVendor*
659Module::GetSymbolVendor (bool can_create)
660{
661 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +0000662 if (m_did_load_symbol_vendor == false && can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000663 {
664 ObjectFile *obj_file = GetObjectFile ();
665 if (obj_file != NULL)
666 {
667 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
668 m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
Greg Clayton236c1c72010-09-07 23:40:05 +0000669 m_did_load_symbol_vendor = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000670 }
671 }
672 return m_symfile_ap.get();
673}
674
Chris Lattner24943d22010-06-08 16:52:24 +0000675void
676Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
677{
678 // Container objects whose paths do not specify a file directly can call
679 // this function to correct the file and object names.
680 m_file = file;
681 m_mod_time = file.GetModificationTime();
682 m_object_name = object_name;
683}
684
685const ArchSpec&
686Module::GetArchitecture () const
687{
688 return m_arch;
689}
690
691void
Greg Clayton75d8c252011-11-28 01:45:00 +0000692Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
Caroline Tice7826c882010-10-26 03:11:13 +0000693{
694 Mutex::Locker locker (m_mutex);
695
Greg Clayton75d8c252011-11-28 01:45:00 +0000696 if (level >= eDescriptionLevelFull)
697 {
698 if (m_arch.IsValid())
699 s->Printf("(%s) ", m_arch.GetArchitectureName());
700 }
Caroline Tice7826c882010-10-26 03:11:13 +0000701
Greg Clayton75d8c252011-11-28 01:45:00 +0000702 if (level == eDescriptionLevelBrief)
703 {
704 const char *filename = m_file.GetFilename().GetCString();
705 if (filename)
706 s->PutCString (filename);
707 }
708 else
709 {
710 char path[PATH_MAX];
711 if (m_file.GetPath(path, sizeof(path)))
712 s->PutCString(path);
713 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000714
715 const char *object_name = m_object_name.GetCString();
716 if (object_name)
717 s->Printf("(%s)", object_name);
Caroline Tice7826c882010-10-26 03:11:13 +0000718}
719
720void
Greg Clayton75d8c252011-11-28 01:45:00 +0000721Module::ReportError (const char *format, ...)
722{
Greg Claytondf6dc882012-01-05 03:57:59 +0000723 if (format && format[0])
724 {
725 StreamString strm;
726 strm.PutCString("error: ");
727 GetDescription(&strm, lldb::eDescriptionLevelBrief);
Greg Clayton71299132012-01-11 01:59:18 +0000728 strm.PutChar (' ');
Greg Claytondf6dc882012-01-05 03:57:59 +0000729 va_list args;
730 va_start (args, format);
731 strm.PrintfVarArg(format, args);
732 va_end (args);
733
734 const int format_len = strlen(format);
735 if (format_len > 0)
736 {
737 const char last_char = format[format_len-1];
738 if (last_char != '\n' || last_char != '\r')
739 strm.EOL();
740 }
741 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
742
743 }
744}
745
746void
747Module::ReportErrorIfModifyDetected (const char *format, ...)
748{
749 if (!GetModified(true) && GetModified(false))
750 {
751 if (format)
752 {
753 StreamString strm;
754 strm.PutCString("error: the object file ");
755 GetDescription(&strm, lldb::eDescriptionLevelFull);
756 strm.PutCString (" has been modified\n");
757
758 va_list args;
759 va_start (args, format);
760 strm.PrintfVarArg(format, args);
761 va_end (args);
762
763 const int format_len = strlen(format);
764 if (format_len > 0)
765 {
766 const char last_char = format[format_len-1];
767 if (last_char != '\n' || last_char != '\r')
768 strm.EOL();
769 }
770 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
771 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
772 }
773 }
Greg Clayton75d8c252011-11-28 01:45:00 +0000774}
775
776void
777Module::ReportWarning (const char *format, ...)
778{
Greg Claytondf6dc882012-01-05 03:57:59 +0000779 if (format && format[0])
780 {
781 StreamString strm;
782 strm.PutCString("warning: ");
Greg Clayton71299132012-01-11 01:59:18 +0000783 GetDescription(&strm, lldb::eDescriptionLevelFull);
784 strm.PutChar (' ');
Greg Claytondf6dc882012-01-05 03:57:59 +0000785
786 va_list args;
787 va_start (args, format);
788 strm.PrintfVarArg(format, args);
789 va_end (args);
790
791 const int format_len = strlen(format);
792 if (format_len > 0)
793 {
794 const char last_char = format[format_len-1];
795 if (last_char != '\n' || last_char != '\r')
796 strm.EOL();
797 }
798 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
799 }
Greg Clayton75d8c252011-11-28 01:45:00 +0000800}
801
802void
803Module::LogMessage (Log *log, const char *format, ...)
804{
805 if (log)
806 {
807 StreamString log_message;
Greg Clayton71299132012-01-11 01:59:18 +0000808 GetDescription(&log_message, lldb::eDescriptionLevelFull);
Greg Clayton75d8c252011-11-28 01:45:00 +0000809 log_message.PutCString (": ");
810 va_list args;
811 va_start (args, format);
812 log_message.PrintfVarArg (format, args);
813 va_end (args);
814 log->PutCString(log_message.GetString().c_str());
815 }
816}
817
Greg Claytondf6dc882012-01-05 03:57:59 +0000818bool
819Module::GetModified (bool use_cached_only)
820{
821 if (m_was_modified == false && use_cached_only == false)
822 {
823 TimeValue curr_mod_time (m_file.GetModificationTime());
824 m_was_modified = curr_mod_time != m_mod_time;
825 }
826 return m_was_modified;
827}
828
829bool
830Module::SetModified (bool b)
831{
832 const bool prev_value = m_was_modified;
833 m_was_modified = b;
834 return prev_value;
835}
836
837
Greg Clayton75d8c252011-11-28 01:45:00 +0000838void
Chris Lattner24943d22010-06-08 16:52:24 +0000839Module::Dump(Stream *s)
840{
841 Mutex::Locker locker (m_mutex);
Greg Clayton3fed8b92010-10-08 00:21:05 +0000842 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner24943d22010-06-08 16:52:24 +0000843 s->Indent();
844 s->Printf("Module %s/%s%s%s%s\n",
845 m_file.GetDirectory().AsCString(),
846 m_file.GetFilename().AsCString(),
847 m_object_name ? "(" : "",
848 m_object_name ? m_object_name.GetCString() : "",
849 m_object_name ? ")" : "");
850
851 s->IndentMore();
852 ObjectFile *objfile = GetObjectFile ();
853
854 if (objfile)
855 objfile->Dump(s);
856
857 SymbolVendor *symbols = GetSymbolVendor ();
858
859 if (symbols)
860 symbols->Dump(s);
861
862 s->IndentLess();
863}
864
865
866TypeList*
867Module::GetTypeList ()
868{
869 SymbolVendor *symbols = GetSymbolVendor ();
870 if (symbols)
871 return &symbols->GetTypeList();
872 return NULL;
873}
874
875const ConstString &
876Module::GetObjectName() const
877{
878 return m_object_name;
879}
880
881ObjectFile *
882Module::GetObjectFile()
883{
884 Mutex::Locker locker (m_mutex);
Greg Clayton236c1c72010-09-07 23:40:05 +0000885 if (m_did_load_objfile == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000886 {
Greg Clayton236c1c72010-09-07 23:40:05 +0000887 m_did_load_objfile = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000888 Timer scoped_timer(__PRETTY_FUNCTION__,
889 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000890 DataBufferSP file_data_sp;
891 m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp);
Greg Clayton6a64bbf2011-09-21 03:57:31 +0000892 if (m_objfile_sp)
893 {
894 // Once we get the object file, update our module with the object file's
895 // architecture since it might differ in vendor/os if some parts were
896 // unknown.
897 m_objfile_sp->GetArchitecture (m_arch);
898 }
Chris Lattner24943d22010-06-08 16:52:24 +0000899 }
Greg Claytone40b6422011-09-18 18:59:15 +0000900 return m_objfile_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000901}
902
903
904const Symbol *
905Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
906{
907 Timer scoped_timer(__PRETTY_FUNCTION__,
908 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
909 name.AsCString(),
910 symbol_type);
911 ObjectFile *objfile = GetObjectFile();
912 if (objfile)
913 {
914 Symtab *symtab = objfile->GetSymtab();
915 if (symtab)
Greg Clayton7c36fa02010-09-11 03:13:28 +0000916 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
Chris Lattner24943d22010-06-08 16:52:24 +0000917 }
918 return NULL;
919}
920void
921Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
922{
923 // No need to protect this call using m_mutex all other method calls are
924 // already thread safe.
925
926 size_t num_indices = symbol_indexes.size();
927 if (num_indices > 0)
928 {
929 SymbolContext sc;
930 CalculateSymbolContext (&sc);
931 for (size_t i = 0; i < num_indices; i++)
932 {
933 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
934 if (sc.symbol)
935 sc_list.Append (sc);
936 }
937 }
938}
939
940size_t
Sean Callananaa4a5532011-10-13 16:49:47 +0000941Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000942{
943 // No need to protect this call using m_mutex all other method calls are
944 // already thread safe.
945
946
947 Timer scoped_timer(__PRETTY_FUNCTION__,
948 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
949 name.AsCString(),
950 symbol_type);
951 const size_t initial_size = sc_list.GetSize();
952 ObjectFile *objfile = GetObjectFile ();
953 if (objfile)
954 {
955 Symtab *symtab = objfile->GetSymtab();
956 if (symtab)
957 {
958 std::vector<uint32_t> symbol_indexes;
959 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
960 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
961 }
962 }
963 return sc_list.GetSize() - initial_size;
964}
965
966size_t
967Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
968{
969 // No need to protect this call using m_mutex all other method calls are
970 // already thread safe.
971
972 Timer scoped_timer(__PRETTY_FUNCTION__,
973 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
974 regex.GetText(),
975 symbol_type);
976 const size_t initial_size = sc_list.GetSize();
977 ObjectFile *objfile = GetObjectFile ();
978 if (objfile)
979 {
980 Symtab *symtab = objfile->GetSymtab();
981 if (symtab)
982 {
983 std::vector<uint32_t> symbol_indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000984 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
Chris Lattner24943d22010-06-08 16:52:24 +0000985 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
986 }
987 }
988 return sc_list.GetSize() - initial_size;
989}
990
991const TimeValue &
992Module::GetModificationTime () const
993{
994 return m_mod_time;
995}
Jim Ingham7508e732010-08-09 23:31:02 +0000996
997bool
998Module::IsExecutable ()
999{
1000 if (GetObjectFile() == NULL)
1001 return false;
1002 else
1003 return GetObjectFile()->IsExecutable();
1004}
1005
Jim Ingham51b11e02011-08-03 01:03:17 +00001006bool
1007Module::IsLoadedInTarget (Target *target)
1008{
1009 ObjectFile *obj_file = GetObjectFile();
1010 if (obj_file)
1011 {
1012 SectionList *sections = obj_file->GetSectionList();
1013 if (sections != NULL)
1014 {
1015 size_t num_sections = sections->GetSize();
Jim Ingham51b11e02011-08-03 01:03:17 +00001016 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1017 {
1018 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1019 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1020 {
1021 return true;
1022 }
1023 }
1024 }
1025 }
1026 return false;
1027}
Jim Ingham7508e732010-08-09 23:31:02 +00001028bool
1029Module::SetArchitecture (const ArchSpec &new_arch)
1030{
Greg Clayton940b1032011-02-23 00:35:02 +00001031 if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001032 {
1033 m_arch = new_arch;
1034 return true;
Greg Clayton940b1032011-02-23 00:35:02 +00001035 }
1036 return m_arch == new_arch;
Jim Ingham7508e732010-08-09 23:31:02 +00001037}
1038
Greg Claytonb5a8f142012-02-05 02:38:54 +00001039bool
1040Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
1041{
1042 changed = false;
1043 ObjectFile *image_object_file = GetObjectFile();
1044 if (image_object_file)
1045 {
1046 SectionList *section_list = image_object_file->GetSectionList ();
1047 if (section_list)
1048 {
1049 const size_t num_sections = section_list->GetSize();
1050 size_t sect_idx = 0;
1051 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1052 {
1053 // Iterate through the object file sections to find the
1054 // first section that starts of file offset zero and that
1055 // has bytes in the file...
1056 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1057 if (section)
1058 {
1059 if (target.GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + offset))
1060 changed = true;
1061 }
1062 }
1063 return sect_idx > 0;
1064 }
1065 }
1066 return false;
1067}
1068