Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 14 | #include "lldb/Core/Timer.h" |
| 15 | #include "lldb/lldb-private-log.h" |
| 16 | #include "lldb/Symbol/ObjectFile.h" |
| 17 | #include "lldb/Symbol/SymbolContext.h" |
| 18 | #include "lldb/Symbol/SymbolVendor.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 23 | // Shared pointers to modules track module lifetimes in |
| 24 | // targets and in the global module, but this collection |
| 25 | // will track all module objects that are still alive |
| 26 | typedef std::vector<Module *> ModuleCollection; |
| 27 | |
| 28 | static ModuleCollection & |
| 29 | GetModuleCollection() |
| 30 | { |
| 31 | static ModuleCollection g_module_collection; |
| 32 | return g_module_collection; |
| 33 | } |
| 34 | |
| 35 | Mutex & |
| 36 | Module::GetAllocationModuleCollectionMutex() |
| 37 | { |
| 38 | static Mutex g_module_collection_mutex(Mutex::eMutexTypeRecursive); |
| 39 | return g_module_collection_mutex; |
| 40 | } |
| 41 | |
| 42 | size_t |
| 43 | Module::GetNumberAllocatedModules () |
| 44 | { |
| 45 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 46 | return GetModuleCollection().size(); |
| 47 | } |
| 48 | |
| 49 | Module * |
| 50 | Module::GetAllocatedModuleAtIndex (size_t idx) |
| 51 | { |
| 52 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 53 | ModuleCollection &modules = GetModuleCollection(); |
| 54 | if (idx < modules.size()) |
| 55 | return modules[idx]; |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) : |
| 63 | m_mutex (Mutex::eMutexTypeRecursive), |
| 64 | m_mod_time (file_spec.GetModificationTime()), |
| 65 | m_arch (arch), |
| 66 | m_uuid (), |
| 67 | m_file (file_spec), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 68 | m_platform_file(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | m_object_name (), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 70 | m_object_offset (object_offset), |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 71 | m_objfile_sp (), |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 72 | m_symfile_ap (), |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 73 | m_ast (), |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 74 | m_did_load_objfile (false), |
| 75 | m_did_load_symbol_vendor (false), |
| 76 | m_did_parse_uuid (false), |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 77 | m_did_init_ast (false), |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 78 | m_is_dynamic_loader_module (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | { |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 80 | // Scope for locker below... |
| 81 | { |
| 82 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 83 | GetModuleCollection().push_back(this); |
| 84 | } |
| 85 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | if (object_name) |
| 87 | m_object_name = *object_name; |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 88 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | if (log) |
| 90 | log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')", |
| 91 | this, |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 92 | m_arch.GetArchitectureName(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | m_file.GetDirectory().AsCString(""), |
| 94 | m_file.GetFilename().AsCString(""), |
| 95 | m_object_name.IsEmpty() ? "" : "(", |
| 96 | m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), |
| 97 | m_object_name.IsEmpty() ? "" : ")"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Module::~Module() |
| 101 | { |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 102 | // Scope for locker below... |
| 103 | { |
| 104 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 105 | ModuleCollection &modules = GetModuleCollection(); |
| 106 | ModuleCollection::iterator end = modules.end(); |
| 107 | ModuleCollection::iterator pos = std::find(modules.begin(), end, this); |
| 108 | if (pos != end) |
| 109 | modules.erase(pos); |
| 110 | } |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 111 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | if (log) |
| 113 | log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')", |
| 114 | this, |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 115 | m_arch.GetArchitectureName(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | m_file.GetDirectory().AsCString(""), |
| 117 | m_file.GetFilename().AsCString(""), |
| 118 | m_object_name.IsEmpty() ? "" : "(", |
| 119 | m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), |
| 120 | m_object_name.IsEmpty() ? "" : ")"); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 121 | // Release any auto pointers before we start tearing down our member |
| 122 | // variables since the object file and symbol files might need to make |
| 123 | // function calls back into this module object. The ordering is important |
| 124 | // here because symbol files can require the module object file. So we tear |
| 125 | // down the symbol file first, then the object file. |
| 126 | m_symfile_ap.reset(); |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 127 | m_objfile_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 131 | const lldb_private::UUID& |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | Module::GetUUID() |
| 133 | { |
| 134 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 135 | if (m_did_parse_uuid == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | { |
| 137 | ObjectFile * obj_file = GetObjectFile (); |
| 138 | |
| 139 | if (obj_file != NULL) |
| 140 | { |
| 141 | obj_file->GetUUID(&m_uuid); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 142 | m_did_parse_uuid = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return m_uuid; |
| 146 | } |
| 147 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 148 | ClangASTContext & |
| 149 | Module::GetClangASTContext () |
| 150 | { |
| 151 | Mutex::Locker locker (m_mutex); |
| 152 | if (m_did_init_ast == false) |
| 153 | { |
| 154 | ObjectFile * objfile = GetObjectFile(); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 155 | ArchSpec object_arch; |
| 156 | if (objfile && objfile->GetArchitecture(object_arch)) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 157 | { |
| 158 | m_did_init_ast = true; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 159 | m_ast.SetArchitecture (object_arch); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | return m_ast; |
| 163 | } |
| 164 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | void |
| 166 | Module::ParseAllDebugSymbols() |
| 167 | { |
| 168 | Mutex::Locker locker (m_mutex); |
| 169 | uint32_t num_comp_units = GetNumCompileUnits(); |
| 170 | if (num_comp_units == 0) |
| 171 | return; |
| 172 | |
Greg Clayton | a2eee18 | 2011-09-17 07:23:18 +0000 | [diff] [blame] | 173 | SymbolContext sc; |
| 174 | sc.module_sp = this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | uint32_t cu_idx; |
| 176 | SymbolVendor *symbols = GetSymbolVendor (); |
| 177 | |
| 178 | for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++) |
| 179 | { |
| 180 | sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); |
| 181 | if (sc.comp_unit) |
| 182 | { |
| 183 | sc.function = NULL; |
| 184 | symbols->ParseVariablesForContext(sc); |
| 185 | |
| 186 | symbols->ParseCompileUnitFunctions(sc); |
| 187 | |
| 188 | uint32_t func_idx; |
| 189 | for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx) |
| 190 | { |
| 191 | symbols->ParseFunctionBlocks(sc); |
| 192 | |
| 193 | // Parse the variables for this function and all its blocks |
| 194 | symbols->ParseVariablesForContext(sc); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | // Parse all types for this compile unit |
| 199 | sc.function = NULL; |
| 200 | symbols->ParseTypes(sc); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | Module::CalculateSymbolContext(SymbolContext* sc) |
| 207 | { |
Greg Clayton | a2eee18 | 2011-09-17 07:23:18 +0000 | [diff] [blame] | 208 | sc->module_sp = this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 211 | Module * |
| 212 | Module::CalculateSymbolContextModule () |
| 213 | { |
| 214 | return this; |
| 215 | } |
| 216 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | void |
| 218 | Module::DumpSymbolContext(Stream *s) |
| 219 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 220 | s->Printf(", Module{%p}", this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | uint32_t |
| 224 | Module::GetNumCompileUnits() |
| 225 | { |
| 226 | Mutex::Locker locker (m_mutex); |
| 227 | Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this); |
| 228 | SymbolVendor *symbols = GetSymbolVendor (); |
| 229 | if (symbols) |
| 230 | return symbols->GetNumCompileUnits(); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | CompUnitSP |
| 235 | Module::GetCompileUnitAtIndex (uint32_t index) |
| 236 | { |
| 237 | Mutex::Locker locker (m_mutex); |
| 238 | uint32_t num_comp_units = GetNumCompileUnits (); |
| 239 | CompUnitSP cu_sp; |
| 240 | |
| 241 | if (index < num_comp_units) |
| 242 | { |
| 243 | SymbolVendor *symbols = GetSymbolVendor (); |
| 244 | if (symbols) |
| 245 | cu_sp = symbols->GetCompileUnitAtIndex(index); |
| 246 | } |
| 247 | return cu_sp; |
| 248 | } |
| 249 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | bool |
| 251 | Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) |
| 252 | { |
| 253 | Mutex::Locker locker (m_mutex); |
| 254 | Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr); |
| 255 | ObjectFile* ofile = GetObjectFile(); |
| 256 | if (ofile) |
| 257 | return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList()); |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | uint32_t |
| 262 | Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) |
| 263 | { |
| 264 | Mutex::Locker locker (m_mutex); |
| 265 | uint32_t resolved_flags = 0; |
| 266 | |
| 267 | // Clear the result symbol context in case we don't find anything |
| 268 | sc.Clear(); |
| 269 | |
| 270 | // Get the section from the section/offset address. |
| 271 | const Section *section = so_addr.GetSection(); |
| 272 | |
| 273 | // Make sure the section matches this module before we try and match anything |
| 274 | if (section && section->GetModule() == this) |
| 275 | { |
| 276 | // If the section offset based address resolved itself, then this |
| 277 | // is the right module. |
Greg Clayton | a2eee18 | 2011-09-17 07:23:18 +0000 | [diff] [blame] | 278 | sc.module_sp = this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | resolved_flags |= eSymbolContextModule; |
| 280 | |
| 281 | // Resolve the compile unit, function, block, line table or line |
| 282 | // entry if requested. |
| 283 | if (resolve_scope & eSymbolContextCompUnit || |
| 284 | resolve_scope & eSymbolContextFunction || |
| 285 | resolve_scope & eSymbolContextBlock || |
| 286 | resolve_scope & eSymbolContextLineEntry ) |
| 287 | { |
| 288 | SymbolVendor *symbols = GetSymbolVendor (); |
| 289 | if (symbols) |
| 290 | resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc); |
| 291 | } |
| 292 | |
Jim Ingham | 680e177 | 2010-08-31 23:51:36 +0000 | [diff] [blame] | 293 | // Resolve the symbol if requested, but don't re-look it up if we've already found it. |
| 294 | if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | { |
| 296 | ObjectFile* ofile = GetObjectFile(); |
| 297 | if (ofile) |
| 298 | { |
| 299 | Symtab *symtab = ofile->GetSymtab(); |
| 300 | if (symtab) |
| 301 | { |
| 302 | if (so_addr.IsSectionOffset()) |
| 303 | { |
| 304 | sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress()); |
| 305 | if (sc.symbol) |
| 306 | resolved_flags |= eSymbolContextSymbol; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | return resolved_flags; |
| 313 | } |
| 314 | |
| 315 | uint32_t |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 316 | Module::ResolveSymbolContextForFilePath |
| 317 | ( |
| 318 | const char *file_path, |
| 319 | uint32_t line, |
| 320 | bool check_inlines, |
| 321 | uint32_t resolve_scope, |
| 322 | SymbolContextList& sc_list |
| 323 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 325 | FileSpec file_spec(file_path, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); |
| 327 | } |
| 328 | |
| 329 | uint32_t |
| 330 | Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) |
| 331 | { |
| 332 | Mutex::Locker locker (m_mutex); |
| 333 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 334 | "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)", |
| 335 | file_spec.GetDirectory().AsCString(""), |
| 336 | file_spec.GetDirectory() ? "/" : "", |
| 337 | file_spec.GetFilename().AsCString(""), |
| 338 | line, |
| 339 | check_inlines ? "yes" : "no", |
| 340 | resolve_scope); |
| 341 | |
| 342 | const uint32_t initial_count = sc_list.GetSize(); |
| 343 | |
| 344 | SymbolVendor *symbols = GetSymbolVendor (); |
| 345 | if (symbols) |
| 346 | symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list); |
| 347 | |
| 348 | return sc_list.GetSize() - initial_count; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | uint32_t |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 353 | Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 354 | { |
| 355 | SymbolVendor *symbols = GetSymbolVendor (); |
| 356 | if (symbols) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 357 | return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | uint32_t |
| 361 | Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) |
| 362 | { |
| 363 | SymbolVendor *symbols = GetSymbolVendor (); |
| 364 | if (symbols) |
| 365 | return symbols->FindGlobalVariables(regex, append, max_matches, variables); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | uint32_t |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 370 | Module::FindCompileUnits (const FileSpec &path, |
| 371 | bool append, |
| 372 | SymbolContextList &sc_list) |
| 373 | { |
| 374 | if (!append) |
| 375 | sc_list.Clear(); |
| 376 | |
| 377 | const uint32_t start_size = sc_list.GetSize(); |
| 378 | const uint32_t num_compile_units = GetNumCompileUnits(); |
| 379 | SymbolContext sc; |
Greg Clayton | a2eee18 | 2011-09-17 07:23:18 +0000 | [diff] [blame] | 380 | sc.module_sp = this; |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 381 | const bool compare_directory = path.GetDirectory(); |
| 382 | for (uint32_t i=0; i<num_compile_units; ++i) |
| 383 | { |
| 384 | sc.comp_unit = GetCompileUnitAtIndex(i).get(); |
| 385 | if (FileSpec::Equal (*sc.comp_unit, path, compare_directory)) |
| 386 | sc_list.Append(sc); |
| 387 | } |
| 388 | return sc_list.GetSize() - start_size; |
| 389 | } |
| 390 | |
| 391 | uint32_t |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 392 | Module::FindFunctions (const ConstString &name, |
| 393 | const ClangNamespaceDecl *namespace_decl, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 394 | uint32_t name_type_mask, |
| 395 | bool include_symbols, |
| 396 | bool append, |
| 397 | SymbolContextList& sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 399 | if (!append) |
| 400 | sc_list.Clear(); |
| 401 | |
| 402 | const uint32_t start_size = sc_list.GetSize(); |
| 403 | |
| 404 | // Find all the functions (not symbols, but debug information functions... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 405 | SymbolVendor *symbols = GetSymbolVendor (); |
| 406 | if (symbols) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 407 | symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 408 | |
| 409 | // Now check our symbol table for symbols that are code symbols if requested |
| 410 | if (include_symbols) |
| 411 | { |
| 412 | ObjectFile *objfile = GetObjectFile(); |
| 413 | if (objfile) |
| 414 | { |
| 415 | Symtab *symtab = objfile->GetSymtab(); |
| 416 | if (symtab) |
| 417 | { |
| 418 | std::vector<uint32_t> symbol_indexes; |
| 419 | symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
| 420 | const uint32_t num_matches = symbol_indexes.size(); |
| 421 | if (num_matches) |
| 422 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 423 | const bool merge_symbol_into_function = true; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 424 | SymbolContext sc(this); |
| 425 | for (uint32_t i=0; i<num_matches; i++) |
| 426 | { |
| 427 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 428 | sc_list.AppendIfUnique (sc, merge_symbol_into_function); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return sc_list.GetSize() - start_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | uint32_t |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 438 | Module::FindFunctions (const RegularExpression& regex, |
| 439 | bool include_symbols, |
| 440 | bool append, |
| 441 | SymbolContextList& sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 443 | if (!append) |
| 444 | sc_list.Clear(); |
| 445 | |
| 446 | const uint32_t start_size = sc_list.GetSize(); |
| 447 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 448 | SymbolVendor *symbols = GetSymbolVendor (); |
| 449 | if (symbols) |
Jim Ingham | 832332d | 2011-05-18 05:02:10 +0000 | [diff] [blame] | 450 | symbols->FindFunctions(regex, append, sc_list); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 451 | // Now check our symbol table for symbols that are code symbols if requested |
| 452 | if (include_symbols) |
| 453 | { |
| 454 | ObjectFile *objfile = GetObjectFile(); |
| 455 | if (objfile) |
| 456 | { |
| 457 | Symtab *symtab = objfile->GetSymtab(); |
| 458 | if (symtab) |
| 459 | { |
| 460 | std::vector<uint32_t> symbol_indexes; |
| 461 | symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
| 462 | const uint32_t num_matches = symbol_indexes.size(); |
| 463 | if (num_matches) |
| 464 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 465 | const bool merge_symbol_into_function = true; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 466 | SymbolContext sc(this); |
| 467 | for (uint32_t i=0; i<num_matches; i++) |
| 468 | { |
| 469 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 470 | sc_list.AppendIfUnique (sc, merge_symbol_into_function); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | return sc_list.GetSize() - start_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 479 | uint32_t |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 480 | Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types) |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 481 | { |
| 482 | Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 483 | if (sc.module_sp.get() == NULL || sc.module_sp.get() == this) |
| 484 | { |
| 485 | SymbolVendor *symbols = GetSymbolVendor (); |
| 486 | if (symbols) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 487 | return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types); |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 492 | // depending on implementation details, type lookup might fail because of |
| 493 | // embedded spurious namespace:: prefixes. this call strips them, paying |
| 494 | // attention to the fact that a type might have namespace'd type names as |
| 495 | // arguments to templates, and those must not be stripped off |
| 496 | static const char* |
| 497 | StripTypeName(const char* name_cstr) |
| 498 | { |
| 499 | const char* skip_namespace = strstr(name_cstr, "::"); |
| 500 | const char* template_arg_char = strchr(name_cstr, '<'); |
| 501 | while (skip_namespace != NULL) |
| 502 | { |
| 503 | if (template_arg_char != NULL && |
| 504 | skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go |
| 505 | break; |
| 506 | name_cstr = skip_namespace+2; |
| 507 | skip_namespace = strstr(name_cstr, "::"); |
| 508 | } |
| 509 | return name_cstr; |
| 510 | } |
| 511 | |
| 512 | uint32_t |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 513 | Module::FindTypes (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types) |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 514 | { |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 515 | uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types); |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 516 | |
| 517 | if (retval == 0) |
| 518 | { |
| 519 | const char *stripped = StripTypeName(name.GetCString()); |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 520 | return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types); |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 521 | } |
| 522 | else |
| 523 | return retval; |
| 524 | |
| 525 | } |
| 526 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | //uint32_t |
| 528 | //Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types) |
| 529 | //{ |
| 530 | // Timer scoped_timer(__PRETTY_FUNCTION__); |
| 531 | // SymbolVendor *symbols = GetSymbolVendor (); |
| 532 | // if (symbols) |
| 533 | // return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types); |
| 534 | // return 0; |
| 535 | // |
| 536 | //} |
| 537 | |
| 538 | SymbolVendor* |
| 539 | Module::GetSymbolVendor (bool can_create) |
| 540 | { |
| 541 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 542 | if (m_did_load_symbol_vendor == false && can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | { |
| 544 | ObjectFile *obj_file = GetObjectFile (); |
| 545 | if (obj_file != NULL) |
| 546 | { |
| 547 | Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 548 | m_symfile_ap.reset(SymbolVendor::FindPlugin(this)); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 549 | m_did_load_symbol_vendor = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | return m_symfile_ap.get(); |
| 553 | } |
| 554 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | void |
| 556 | Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) |
| 557 | { |
| 558 | // Container objects whose paths do not specify a file directly can call |
| 559 | // this function to correct the file and object names. |
| 560 | m_file = file; |
| 561 | m_mod_time = file.GetModificationTime(); |
| 562 | m_object_name = object_name; |
| 563 | } |
| 564 | |
| 565 | const ArchSpec& |
| 566 | Module::GetArchitecture () const |
| 567 | { |
| 568 | return m_arch; |
| 569 | } |
| 570 | |
| 571 | void |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 572 | Module::GetDescription (Stream *s) |
| 573 | { |
| 574 | Mutex::Locker locker (m_mutex); |
| 575 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 576 | if (m_arch.IsValid()) |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 577 | s->Printf("(%s) ", m_arch.GetArchitectureName()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 578 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 579 | char path[PATH_MAX]; |
| 580 | if (m_file.GetPath(path, sizeof(path))) |
| 581 | s->PutCString(path); |
| 582 | |
| 583 | const char *object_name = m_object_name.GetCString(); |
| 584 | if (object_name) |
| 585 | s->Printf("(%s)", object_name); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 589 | Module::Dump(Stream *s) |
| 590 | { |
| 591 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 592 | //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 593 | s->Indent(); |
| 594 | s->Printf("Module %s/%s%s%s%s\n", |
| 595 | m_file.GetDirectory().AsCString(), |
| 596 | m_file.GetFilename().AsCString(), |
| 597 | m_object_name ? "(" : "", |
| 598 | m_object_name ? m_object_name.GetCString() : "", |
| 599 | m_object_name ? ")" : ""); |
| 600 | |
| 601 | s->IndentMore(); |
| 602 | ObjectFile *objfile = GetObjectFile (); |
| 603 | |
| 604 | if (objfile) |
| 605 | objfile->Dump(s); |
| 606 | |
| 607 | SymbolVendor *symbols = GetSymbolVendor (); |
| 608 | |
| 609 | if (symbols) |
| 610 | symbols->Dump(s); |
| 611 | |
| 612 | s->IndentLess(); |
| 613 | } |
| 614 | |
| 615 | |
| 616 | TypeList* |
| 617 | Module::GetTypeList () |
| 618 | { |
| 619 | SymbolVendor *symbols = GetSymbolVendor (); |
| 620 | if (symbols) |
| 621 | return &symbols->GetTypeList(); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | const ConstString & |
| 626 | Module::GetObjectName() const |
| 627 | { |
| 628 | return m_object_name; |
| 629 | } |
| 630 | |
| 631 | ObjectFile * |
| 632 | Module::GetObjectFile() |
| 633 | { |
| 634 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 635 | if (m_did_load_objfile == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 636 | { |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 637 | m_did_load_objfile = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 638 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 639 | "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 640 | m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize()); |
Greg Clayton | 593577a | 2011-09-21 03:57:31 +0000 | [diff] [blame] | 641 | if (m_objfile_sp) |
| 642 | { |
| 643 | // Once we get the object file, update our module with the object file's |
| 644 | // architecture since it might differ in vendor/os if some parts were |
| 645 | // unknown. |
| 646 | m_objfile_sp->GetArchitecture (m_arch); |
| 647 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | } |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 649 | return m_objfile_sp.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | const Symbol * |
| 654 | Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) |
| 655 | { |
| 656 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 657 | "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", |
| 658 | name.AsCString(), |
| 659 | symbol_type); |
| 660 | ObjectFile *objfile = GetObjectFile(); |
| 661 | if (objfile) |
| 662 | { |
| 663 | Symtab *symtab = objfile->GetSymtab(); |
| 664 | if (symtab) |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 665 | return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 666 | } |
| 667 | return NULL; |
| 668 | } |
| 669 | void |
| 670 | Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) |
| 671 | { |
| 672 | // No need to protect this call using m_mutex all other method calls are |
| 673 | // already thread safe. |
| 674 | |
| 675 | size_t num_indices = symbol_indexes.size(); |
| 676 | if (num_indices > 0) |
| 677 | { |
| 678 | SymbolContext sc; |
| 679 | CalculateSymbolContext (&sc); |
| 680 | for (size_t i = 0; i < num_indices; i++) |
| 681 | { |
| 682 | sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); |
| 683 | if (sc.symbol) |
| 684 | sc_list.Append (sc); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | size_t |
Sean Callanan | b96ff33 | 2011-10-13 16:49:47 +0000 | [diff] [blame] | 690 | Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 691 | { |
| 692 | // No need to protect this call using m_mutex all other method calls are |
| 693 | // already thread safe. |
| 694 | |
| 695 | |
| 696 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 697 | "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", |
| 698 | name.AsCString(), |
| 699 | symbol_type); |
| 700 | const size_t initial_size = sc_list.GetSize(); |
| 701 | ObjectFile *objfile = GetObjectFile (); |
| 702 | if (objfile) |
| 703 | { |
| 704 | Symtab *symtab = objfile->GetSymtab(); |
| 705 | if (symtab) |
| 706 | { |
| 707 | std::vector<uint32_t> symbol_indexes; |
| 708 | symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); |
| 709 | SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); |
| 710 | } |
| 711 | } |
| 712 | return sc_list.GetSize() - initial_size; |
| 713 | } |
| 714 | |
| 715 | size_t |
| 716 | Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) |
| 717 | { |
| 718 | // No need to protect this call using m_mutex all other method calls are |
| 719 | // already thread safe. |
| 720 | |
| 721 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 722 | "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", |
| 723 | regex.GetText(), |
| 724 | symbol_type); |
| 725 | const size_t initial_size = sc_list.GetSize(); |
| 726 | ObjectFile *objfile = GetObjectFile (); |
| 727 | if (objfile) |
| 728 | { |
| 729 | Symtab *symtab = objfile->GetSymtab(); |
| 730 | if (symtab) |
| 731 | { |
| 732 | std::vector<uint32_t> symbol_indexes; |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 733 | symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 734 | SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); |
| 735 | } |
| 736 | } |
| 737 | return sc_list.GetSize() - initial_size; |
| 738 | } |
| 739 | |
| 740 | const TimeValue & |
| 741 | Module::GetModificationTime () const |
| 742 | { |
| 743 | return m_mod_time; |
| 744 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 745 | |
| 746 | bool |
| 747 | Module::IsExecutable () |
| 748 | { |
| 749 | if (GetObjectFile() == NULL) |
| 750 | return false; |
| 751 | else |
| 752 | return GetObjectFile()->IsExecutable(); |
| 753 | } |
| 754 | |
Jim Ingham | b53cb27 | 2011-08-03 01:03:17 +0000 | [diff] [blame] | 755 | bool |
| 756 | Module::IsLoadedInTarget (Target *target) |
| 757 | { |
| 758 | ObjectFile *obj_file = GetObjectFile(); |
| 759 | if (obj_file) |
| 760 | { |
| 761 | SectionList *sections = obj_file->GetSectionList(); |
| 762 | if (sections != NULL) |
| 763 | { |
| 764 | size_t num_sections = sections->GetSize(); |
Jim Ingham | b53cb27 | 2011-08-03 01:03:17 +0000 | [diff] [blame] | 765 | for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) |
| 766 | { |
| 767 | SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); |
| 768 | if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) |
| 769 | { |
| 770 | return true; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | return false; |
| 776 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 777 | bool |
| 778 | Module::SetArchitecture (const ArchSpec &new_arch) |
| 779 | { |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 780 | if (!m_arch.IsValid()) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 781 | { |
| 782 | m_arch = new_arch; |
| 783 | return true; |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 784 | } |
| 785 | return m_arch == new_arch; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 786 | } |
| 787 | |