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" |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 11 | #include "lldb/Core/DataBuffer.h" |
| 12 | #include "lldb/Core/DataBufferHeap.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Log.h" |
| 14 | #include "lldb/Core/ModuleList.h" |
| 15 | #include "lldb/Core/RegularExpression.h" |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 16 | #include "lldb/Core/StreamString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Timer.h" |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 18 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #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 Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 29 | // 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 |
| 32 | typedef std::vector<Module *> ModuleCollection; |
| 33 | |
| 34 | static ModuleCollection & |
| 35 | GetModuleCollection() |
| 36 | { |
Jim Ingham | 549f737 | 2011-10-31 23:47:10 +0000 | [diff] [blame] | 37 | // 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 Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Greg Clayton | b26e6be | 2012-01-27 18:08:35 +0000 | [diff] [blame] | 50 | Mutex * |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 51 | Module::GetAllocationModuleCollectionMutex() |
| 52 | { |
Greg Clayton | b26e6be | 2012-01-27 18:08:35 +0000 | [diff] [blame] | 53 | // 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 Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | size_t |
| 65 | Module::GetNumberAllocatedModules () |
| 66 | { |
| 67 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 68 | return GetModuleCollection().size(); |
| 69 | } |
| 70 | |
| 71 | Module * |
| 72 | Module::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 Clayton | 29ad7b9 | 2012-01-27 18:45:39 +0000 | [diff] [blame] | 80 | #if 0 |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 81 | |
Greg Clayton | 29ad7b9 | 2012-01-27 18:45:39 +0000 | [diff] [blame] | 82 | // 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. |
| 84 | namespace lldb { |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 85 | |
Greg Clayton | 29ad7b9 | 2012-01-27 18:45:39 +0000 | [diff] [blame] | 86 | 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 Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 116 | |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 117 | Module::Module (const ModuleSpec &module_spec) : |
| 118 | m_mutex (Mutex::eMutexTypeRecursive), |
| 119 | m_mod_time (module_spec.GetFileSpec().GetModificationTime()), |
| 120 | m_arch (module_spec.GetArchitecture()), |
| 121 | m_uuid (), |
| 122 | m_file (module_spec.GetFileSpec()), |
| 123 | m_platform_file(module_spec.GetPlatformFileSpec()), |
| 124 | m_symfile_spec (module_spec.GetSymbolFileSpec()), |
| 125 | m_object_name (module_spec.GetObjectName()), |
| 126 | m_object_offset (module_spec.GetObjectOffset()), |
| 127 | m_objfile_sp (), |
| 128 | m_symfile_ap (), |
| 129 | m_ast (), |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 130 | m_source_mappings (), |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 131 | m_did_load_objfile (false), |
| 132 | m_did_load_symbol_vendor (false), |
| 133 | m_did_parse_uuid (false), |
| 134 | m_did_init_ast (false), |
| 135 | m_is_dynamic_loader_module (false), |
| 136 | m_was_modified (false) |
| 137 | { |
| 138 | // Scope for locker below... |
| 139 | { |
| 140 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 141 | GetModuleCollection().push_back(this); |
| 142 | } |
| 143 | |
| 144 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
| 145 | if (log) |
| 146 | log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')", |
| 147 | this, |
| 148 | m_arch.GetArchitectureName(), |
| 149 | m_file.GetDirectory().AsCString(""), |
| 150 | m_file.GetFilename().AsCString(""), |
| 151 | m_object_name.IsEmpty() ? "" : "(", |
| 152 | m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), |
| 153 | m_object_name.IsEmpty() ? "" : ")"); |
| 154 | } |
| 155 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 156 | Module::Module(const FileSpec& file_spec, |
| 157 | const ArchSpec& arch, |
| 158 | const ConstString *object_name, |
| 159 | off_t object_offset) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | m_mutex (Mutex::eMutexTypeRecursive), |
| 161 | m_mod_time (file_spec.GetModificationTime()), |
| 162 | m_arch (arch), |
| 163 | m_uuid (), |
| 164 | m_file (file_spec), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 165 | m_platform_file(), |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 166 | m_symfile_spec (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | m_object_name (), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 168 | m_object_offset (object_offset), |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 169 | m_objfile_sp (), |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 170 | m_symfile_ap (), |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 171 | m_ast (), |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 172 | m_source_mappings (), |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 173 | m_did_load_objfile (false), |
| 174 | m_did_load_symbol_vendor (false), |
| 175 | m_did_parse_uuid (false), |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 176 | m_did_init_ast (false), |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 177 | m_is_dynamic_loader_module (false), |
| 178 | m_was_modified (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 180 | // Scope for locker below... |
| 181 | { |
| 182 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 183 | GetModuleCollection().push_back(this); |
| 184 | } |
| 185 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | if (object_name) |
| 187 | m_object_name = *object_name; |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 188 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | if (log) |
| 190 | log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')", |
| 191 | this, |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 192 | m_arch.GetArchitectureName(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | m_file.GetDirectory().AsCString(""), |
| 194 | m_file.GetFilename().AsCString(""), |
| 195 | m_object_name.IsEmpty() ? "" : "(", |
| 196 | m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), |
| 197 | m_object_name.IsEmpty() ? "" : ")"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | Module::~Module() |
| 201 | { |
Greg Clayton | 65a0399 | 2011-08-09 00:01:09 +0000 | [diff] [blame] | 202 | // Scope for locker below... |
| 203 | { |
| 204 | Mutex::Locker locker (GetAllocationModuleCollectionMutex()); |
| 205 | ModuleCollection &modules = GetModuleCollection(); |
| 206 | ModuleCollection::iterator end = modules.end(); |
| 207 | ModuleCollection::iterator pos = std::find(modules.begin(), end, this); |
| 208 | if (pos != end) |
| 209 | modules.erase(pos); |
| 210 | } |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 211 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | if (log) |
| 213 | log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')", |
| 214 | this, |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 215 | m_arch.GetArchitectureName(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | m_file.GetDirectory().AsCString(""), |
| 217 | m_file.GetFilename().AsCString(""), |
| 218 | m_object_name.IsEmpty() ? "" : "(", |
| 219 | m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), |
| 220 | m_object_name.IsEmpty() ? "" : ")"); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 221 | // Release any auto pointers before we start tearing down our member |
| 222 | // variables since the object file and symbol files might need to make |
| 223 | // function calls back into this module object. The ordering is important |
| 224 | // here because symbol files can require the module object file. So we tear |
| 225 | // down the symbol file first, then the object file. |
| 226 | m_symfile_ap.reset(); |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 227 | m_objfile_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 230 | ObjectFile * |
| 231 | Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error) |
| 232 | { |
| 233 | if (m_objfile_sp) |
| 234 | { |
| 235 | error.SetErrorString ("object file already exists"); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | Mutex::Locker locker (m_mutex); |
| 240 | if (process_sp) |
| 241 | { |
| 242 | StreamString s; |
| 243 | if (m_file.GetFilename()) |
| 244 | s << m_file.GetFilename(); |
| 245 | s.Printf("[0x%16.16llx]", header_addr); |
| 246 | m_file.GetFilename().SetCString (s.GetData()); |
| 247 | m_did_load_objfile = true; |
| 248 | std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0)); |
| 249 | Error readmem_error; |
| 250 | const size_t bytes_read = process_sp->ReadMemory (header_addr, |
| 251 | data_ap->GetBytes(), |
| 252 | data_ap->GetByteSize(), |
| 253 | readmem_error); |
| 254 | if (bytes_read == 512) |
| 255 | { |
| 256 | DataBufferSP data_sp(data_ap.release()); |
| 257 | m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp); |
| 258 | if (m_objfile_sp) |
| 259 | { |
| 260 | // Once we get the object file, update our module with the object file's |
| 261 | // architecture since it might differ in vendor/os if some parts were |
| 262 | // unknown. |
| 263 | m_objfile_sp->GetArchitecture (m_arch); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | error.SetErrorString ("unable to find suitable object file plug-in"); |
| 268 | } |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString()); |
| 273 | } |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | error.SetErrorString ("invalid process"); |
| 278 | } |
| 279 | } |
| 280 | return m_objfile_sp.get(); |
| 281 | } |
| 282 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 284 | const lldb_private::UUID& |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | Module::GetUUID() |
| 286 | { |
| 287 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 288 | if (m_did_parse_uuid == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | { |
| 290 | ObjectFile * obj_file = GetObjectFile (); |
| 291 | |
| 292 | if (obj_file != NULL) |
| 293 | { |
| 294 | obj_file->GetUUID(&m_uuid); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 295 | m_did_parse_uuid = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | return m_uuid; |
| 299 | } |
| 300 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 301 | ClangASTContext & |
| 302 | Module::GetClangASTContext () |
| 303 | { |
| 304 | Mutex::Locker locker (m_mutex); |
| 305 | if (m_did_init_ast == false) |
| 306 | { |
| 307 | ObjectFile * objfile = GetObjectFile(); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 308 | ArchSpec object_arch; |
| 309 | if (objfile && objfile->GetArchitecture(object_arch)) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 310 | { |
| 311 | m_did_init_ast = true; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 312 | m_ast.SetArchitecture (object_arch); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | return m_ast; |
| 316 | } |
| 317 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | void |
| 319 | Module::ParseAllDebugSymbols() |
| 320 | { |
| 321 | Mutex::Locker locker (m_mutex); |
| 322 | uint32_t num_comp_units = GetNumCompileUnits(); |
| 323 | if (num_comp_units == 0) |
| 324 | return; |
| 325 | |
Greg Clayton | a2eee18 | 2011-09-17 07:23:18 +0000 | [diff] [blame] | 326 | SymbolContext sc; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 327 | sc.module_sp = shared_from_this(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | uint32_t cu_idx; |
| 329 | SymbolVendor *symbols = GetSymbolVendor (); |
| 330 | |
| 331 | for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++) |
| 332 | { |
| 333 | sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); |
| 334 | if (sc.comp_unit) |
| 335 | { |
| 336 | sc.function = NULL; |
| 337 | symbols->ParseVariablesForContext(sc); |
| 338 | |
| 339 | symbols->ParseCompileUnitFunctions(sc); |
| 340 | |
| 341 | uint32_t func_idx; |
| 342 | for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx) |
| 343 | { |
| 344 | symbols->ParseFunctionBlocks(sc); |
| 345 | |
| 346 | // Parse the variables for this function and all its blocks |
| 347 | symbols->ParseVariablesForContext(sc); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | // Parse all types for this compile unit |
| 352 | sc.function = NULL; |
| 353 | symbols->ParseTypes(sc); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | Module::CalculateSymbolContext(SymbolContext* sc) |
| 360 | { |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 361 | sc->module_sp = shared_from_this(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 364 | ModuleSP |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 365 | Module::CalculateSymbolContextModule () |
| 366 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 367 | return shared_from_this(); |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | void |
| 371 | Module::DumpSymbolContext(Stream *s) |
| 372 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 373 | s->Printf(", Module{%p}", this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | uint32_t |
| 377 | Module::GetNumCompileUnits() |
| 378 | { |
| 379 | Mutex::Locker locker (m_mutex); |
| 380 | Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this); |
| 381 | SymbolVendor *symbols = GetSymbolVendor (); |
| 382 | if (symbols) |
| 383 | return symbols->GetNumCompileUnits(); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | CompUnitSP |
| 388 | Module::GetCompileUnitAtIndex (uint32_t index) |
| 389 | { |
| 390 | Mutex::Locker locker (m_mutex); |
| 391 | uint32_t num_comp_units = GetNumCompileUnits (); |
| 392 | CompUnitSP cu_sp; |
| 393 | |
| 394 | if (index < num_comp_units) |
| 395 | { |
| 396 | SymbolVendor *symbols = GetSymbolVendor (); |
| 397 | if (symbols) |
| 398 | cu_sp = symbols->GetCompileUnitAtIndex(index); |
| 399 | } |
| 400 | return cu_sp; |
| 401 | } |
| 402 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 403 | bool |
| 404 | Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) |
| 405 | { |
| 406 | Mutex::Locker locker (m_mutex); |
| 407 | Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr); |
| 408 | ObjectFile* ofile = GetObjectFile(); |
| 409 | if (ofile) |
| 410 | return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList()); |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | uint32_t |
| 415 | Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) |
| 416 | { |
| 417 | Mutex::Locker locker (m_mutex); |
| 418 | uint32_t resolved_flags = 0; |
| 419 | |
| 420 | // Clear the result symbol context in case we don't find anything |
| 421 | sc.Clear(); |
| 422 | |
| 423 | // Get the section from the section/offset address. |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 424 | SectionSP section_sp (so_addr.GetSection()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | |
| 426 | // Make sure the section matches this module before we try and match anything |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 427 | if (section_sp && section_sp->GetModule().get() == this) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | { |
| 429 | // If the section offset based address resolved itself, then this |
| 430 | // is the right module. |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 431 | sc.module_sp = shared_from_this(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | resolved_flags |= eSymbolContextModule; |
| 433 | |
| 434 | // Resolve the compile unit, function, block, line table or line |
| 435 | // entry if requested. |
| 436 | if (resolve_scope & eSymbolContextCompUnit || |
| 437 | resolve_scope & eSymbolContextFunction || |
| 438 | resolve_scope & eSymbolContextBlock || |
| 439 | resolve_scope & eSymbolContextLineEntry ) |
| 440 | { |
| 441 | SymbolVendor *symbols = GetSymbolVendor (); |
| 442 | if (symbols) |
| 443 | resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc); |
| 444 | } |
| 445 | |
Jim Ingham | 680e177 | 2010-08-31 23:51:36 +0000 | [diff] [blame] | 446 | // Resolve the symbol if requested, but don't re-look it up if we've already found it. |
| 447 | if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 448 | { |
| 449 | ObjectFile* ofile = GetObjectFile(); |
| 450 | if (ofile) |
| 451 | { |
| 452 | Symtab *symtab = ofile->GetSymtab(); |
| 453 | if (symtab) |
| 454 | { |
| 455 | if (so_addr.IsSectionOffset()) |
| 456 | { |
| 457 | sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress()); |
| 458 | if (sc.symbol) |
| 459 | resolved_flags |= eSymbolContextSymbol; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return resolved_flags; |
| 466 | } |
| 467 | |
| 468 | uint32_t |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 469 | Module::ResolveSymbolContextForFilePath |
| 470 | ( |
| 471 | const char *file_path, |
| 472 | uint32_t line, |
| 473 | bool check_inlines, |
| 474 | uint32_t resolve_scope, |
| 475 | SymbolContextList& sc_list |
| 476 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 478 | FileSpec file_spec(file_path, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); |
| 480 | } |
| 481 | |
| 482 | uint32_t |
| 483 | Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) |
| 484 | { |
| 485 | Mutex::Locker locker (m_mutex); |
| 486 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 487 | "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)", |
| 488 | file_spec.GetDirectory().AsCString(""), |
| 489 | file_spec.GetDirectory() ? "/" : "", |
| 490 | file_spec.GetFilename().AsCString(""), |
| 491 | line, |
| 492 | check_inlines ? "yes" : "no", |
| 493 | resolve_scope); |
| 494 | |
| 495 | const uint32_t initial_count = sc_list.GetSize(); |
| 496 | |
| 497 | SymbolVendor *symbols = GetSymbolVendor (); |
| 498 | if (symbols) |
| 499 | symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list); |
| 500 | |
| 501 | return sc_list.GetSize() - initial_count; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | uint32_t |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 506 | 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] | 507 | { |
| 508 | SymbolVendor *symbols = GetSymbolVendor (); |
| 509 | if (symbols) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 510 | return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | uint32_t |
| 514 | Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) |
| 515 | { |
| 516 | SymbolVendor *symbols = GetSymbolVendor (); |
| 517 | if (symbols) |
| 518 | return symbols->FindGlobalVariables(regex, append, max_matches, variables); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | uint32_t |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 523 | Module::FindCompileUnits (const FileSpec &path, |
| 524 | bool append, |
| 525 | SymbolContextList &sc_list) |
| 526 | { |
| 527 | if (!append) |
| 528 | sc_list.Clear(); |
| 529 | |
| 530 | const uint32_t start_size = sc_list.GetSize(); |
| 531 | const uint32_t num_compile_units = GetNumCompileUnits(); |
| 532 | SymbolContext sc; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 533 | sc.module_sp = shared_from_this(); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 534 | const bool compare_directory = path.GetDirectory(); |
| 535 | for (uint32_t i=0; i<num_compile_units; ++i) |
| 536 | { |
| 537 | sc.comp_unit = GetCompileUnitAtIndex(i).get(); |
| 538 | if (FileSpec::Equal (*sc.comp_unit, path, compare_directory)) |
| 539 | sc_list.Append(sc); |
| 540 | } |
| 541 | return sc_list.GetSize() - start_size; |
| 542 | } |
| 543 | |
| 544 | uint32_t |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 545 | Module::FindFunctions (const ConstString &name, |
| 546 | const ClangNamespaceDecl *namespace_decl, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 547 | uint32_t name_type_mask, |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 548 | bool include_symbols, |
| 549 | bool include_inlines, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 550 | bool append, |
| 551 | SymbolContextList& sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 553 | if (!append) |
| 554 | sc_list.Clear(); |
| 555 | |
| 556 | const uint32_t start_size = sc_list.GetSize(); |
| 557 | |
| 558 | // Find all the functions (not symbols, but debug information functions... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | SymbolVendor *symbols = GetSymbolVendor (); |
| 560 | if (symbols) |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 561 | symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 562 | |
| 563 | // 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->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
| 574 | const uint32_t num_matches = symbol_indexes.size(); |
| 575 | if (num_matches) |
| 576 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 577 | const bool merge_symbol_into_function = true; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 578 | SymbolContext sc(this); |
| 579 | for (uint32_t i=0; i<num_matches; i++) |
| 580 | { |
| 581 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 582 | sc_list.AppendIfUnique (sc, merge_symbol_into_function); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | return sc_list.GetSize() - start_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | uint32_t |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 592 | Module::FindFunctions (const RegularExpression& regex, |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 593 | bool include_symbols, |
| 594 | bool include_inlines, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 595 | bool append, |
| 596 | SymbolContextList& sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 598 | if (!append) |
| 599 | sc_list.Clear(); |
| 600 | |
| 601 | const uint32_t start_size = sc_list.GetSize(); |
| 602 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | SymbolVendor *symbols = GetSymbolVendor (); |
| 604 | if (symbols) |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 605 | symbols->FindFunctions(regex, include_inlines, append, sc_list); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 606 | // Now check our symbol table for symbols that are code symbols if requested |
| 607 | if (include_symbols) |
| 608 | { |
| 609 | ObjectFile *objfile = GetObjectFile(); |
| 610 | if (objfile) |
| 611 | { |
| 612 | Symtab *symtab = objfile->GetSymtab(); |
| 613 | if (symtab) |
| 614 | { |
| 615 | std::vector<uint32_t> symbol_indexes; |
| 616 | symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
| 617 | const uint32_t num_matches = symbol_indexes.size(); |
| 618 | if (num_matches) |
| 619 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 620 | const bool merge_symbol_into_function = true; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 621 | SymbolContext sc(this); |
| 622 | for (uint32_t i=0; i<num_matches; i++) |
| 623 | { |
| 624 | sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 625 | sc_list.AppendIfUnique (sc, merge_symbol_into_function); |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | return sc_list.GetSize() - start_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 634 | uint32_t |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 635 | Module::FindTypes_Impl (const SymbolContext& sc, |
| 636 | const ConstString &name, |
| 637 | const ClangNamespaceDecl *namespace_decl, |
| 638 | bool append, |
| 639 | uint32_t max_matches, |
| 640 | TypeList& types) |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 641 | { |
| 642 | Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 643 | if (sc.module_sp.get() == NULL || sc.module_sp.get() == this) |
| 644 | { |
| 645 | SymbolVendor *symbols = GetSymbolVendor (); |
| 646 | if (symbols) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame] | 647 | return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types); |
Greg Clayton | 3504eee | 2010-08-03 01:26:16 +0000 | [diff] [blame] | 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 652 | uint32_t |
| 653 | Module::FindTypesInNamespace (const SymbolContext& sc, |
| 654 | const ConstString &type_name, |
| 655 | const ClangNamespaceDecl *namespace_decl, |
| 656 | uint32_t max_matches, |
| 657 | TypeList& type_list) |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 658 | { |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 659 | const bool append = true; |
| 660 | return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list); |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | uint32_t |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 664 | Module::FindTypes (const SymbolContext& sc, |
| 665 | const ConstString &name, |
| 666 | bool exact_match, |
| 667 | uint32_t max_matches, |
| 668 | TypeList& types) |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 669 | { |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 670 | uint32_t num_matches = 0; |
| 671 | const char *type_name_cstr = name.GetCString(); |
| 672 | std::string type_scope; |
| 673 | std::string type_basename; |
| 674 | const bool append = true; |
| 675 | if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename)) |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 676 | { |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 677 | // Check if "name" starts with "::" which means the qualified type starts |
| 678 | // from the root namespace and implies and exact match. The typenames we |
| 679 | // get back from clang do not start with "::" so we need to strip this off |
| 680 | // in order to get the qualfied names to match |
| 681 | |
| 682 | if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':') |
| 683 | { |
| 684 | type_scope.erase(0,2); |
| 685 | exact_match = true; |
| 686 | } |
| 687 | ConstString type_basename_const_str (type_basename.c_str()); |
| 688 | if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types)) |
| 689 | { |
| 690 | types.RemoveMismatchedTypes (type_scope, type_basename, exact_match); |
| 691 | num_matches = types.GetSize(); |
| 692 | } |
Jim Ingham | 50b3d50 | 2012-01-12 22:35:29 +0000 | [diff] [blame] | 693 | else |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 694 | { |
| 695 | types.Clear(); |
| 696 | } |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 697 | } |
| 698 | else |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 699 | { |
| 700 | // The type is not in a namespace/class scope, just search for it by basename |
| 701 | num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types); |
| 702 | } |
| 703 | |
| 704 | return num_matches; |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 705 | |
| 706 | } |
| 707 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | //uint32_t |
| 709 | //Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types) |
| 710 | //{ |
| 711 | // Timer scoped_timer(__PRETTY_FUNCTION__); |
| 712 | // SymbolVendor *symbols = GetSymbolVendor (); |
| 713 | // if (symbols) |
| 714 | // return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types); |
| 715 | // return 0; |
| 716 | // |
| 717 | //} |
| 718 | |
| 719 | SymbolVendor* |
| 720 | Module::GetSymbolVendor (bool can_create) |
| 721 | { |
| 722 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 723 | if (m_did_load_symbol_vendor == false && can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | { |
| 725 | ObjectFile *obj_file = GetObjectFile (); |
| 726 | if (obj_file != NULL) |
| 727 | { |
| 728 | Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 729 | m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this())); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 730 | m_did_load_symbol_vendor = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | return m_symfile_ap.get(); |
| 734 | } |
| 735 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | void |
| 737 | Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) |
| 738 | { |
| 739 | // Container objects whose paths do not specify a file directly can call |
| 740 | // this function to correct the file and object names. |
| 741 | m_file = file; |
| 742 | m_mod_time = file.GetModificationTime(); |
| 743 | m_object_name = object_name; |
| 744 | } |
| 745 | |
| 746 | const ArchSpec& |
| 747 | Module::GetArchitecture () const |
| 748 | { |
| 749 | return m_arch; |
| 750 | } |
| 751 | |
| 752 | void |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 753 | Module::GetDescription (Stream *s, lldb::DescriptionLevel level) |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 754 | { |
| 755 | Mutex::Locker locker (m_mutex); |
| 756 | |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 757 | if (level >= eDescriptionLevelFull) |
| 758 | { |
| 759 | if (m_arch.IsValid()) |
| 760 | s->Printf("(%s) ", m_arch.GetArchitectureName()); |
| 761 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 762 | |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 763 | if (level == eDescriptionLevelBrief) |
| 764 | { |
| 765 | const char *filename = m_file.GetFilename().GetCString(); |
| 766 | if (filename) |
| 767 | s->PutCString (filename); |
| 768 | } |
| 769 | else |
| 770 | { |
| 771 | char path[PATH_MAX]; |
| 772 | if (m_file.GetPath(path, sizeof(path))) |
| 773 | s->PutCString(path); |
| 774 | } |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 775 | |
| 776 | const char *object_name = m_object_name.GetCString(); |
| 777 | if (object_name) |
| 778 | s->Printf("(%s)", object_name); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 782 | Module::ReportError (const char *format, ...) |
| 783 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 784 | if (format && format[0]) |
| 785 | { |
| 786 | StreamString strm; |
| 787 | strm.PutCString("error: "); |
| 788 | GetDescription(&strm, lldb::eDescriptionLevelBrief); |
Greg Clayton | 8b35334 | 2012-01-11 01:59:18 +0000 | [diff] [blame] | 789 | strm.PutChar (' '); |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 790 | va_list args; |
| 791 | va_start (args, format); |
| 792 | strm.PrintfVarArg(format, args); |
| 793 | va_end (args); |
| 794 | |
| 795 | const int format_len = strlen(format); |
| 796 | if (format_len > 0) |
| 797 | { |
| 798 | const char last_char = format[format_len-1]; |
| 799 | if (last_char != '\n' || last_char != '\r') |
| 800 | strm.EOL(); |
| 801 | } |
| 802 | Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); |
| 803 | |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | void |
| 808 | Module::ReportErrorIfModifyDetected (const char *format, ...) |
| 809 | { |
| 810 | if (!GetModified(true) && GetModified(false)) |
| 811 | { |
| 812 | if (format) |
| 813 | { |
| 814 | StreamString strm; |
| 815 | strm.PutCString("error: the object file "); |
| 816 | GetDescription(&strm, lldb::eDescriptionLevelFull); |
| 817 | strm.PutCString (" has been modified\n"); |
| 818 | |
| 819 | va_list args; |
| 820 | va_start (args, format); |
| 821 | strm.PrintfVarArg(format, args); |
| 822 | va_end (args); |
| 823 | |
| 824 | const int format_len = strlen(format); |
| 825 | if (format_len > 0) |
| 826 | { |
| 827 | const char last_char = format[format_len-1]; |
| 828 | if (last_char != '\n' || last_char != '\r') |
| 829 | strm.EOL(); |
| 830 | } |
| 831 | strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); |
| 832 | Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); |
| 833 | } |
| 834 | } |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void |
| 838 | Module::ReportWarning (const char *format, ...) |
| 839 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 840 | if (format && format[0]) |
| 841 | { |
| 842 | StreamString strm; |
| 843 | strm.PutCString("warning: "); |
Greg Clayton | 8b35334 | 2012-01-11 01:59:18 +0000 | [diff] [blame] | 844 | GetDescription(&strm, lldb::eDescriptionLevelFull); |
| 845 | strm.PutChar (' '); |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 846 | |
| 847 | va_list args; |
| 848 | va_start (args, format); |
| 849 | strm.PrintfVarArg(format, args); |
| 850 | va_end (args); |
| 851 | |
| 852 | const int format_len = strlen(format); |
| 853 | if (format_len > 0) |
| 854 | { |
| 855 | const char last_char = format[format_len-1]; |
| 856 | if (last_char != '\n' || last_char != '\r') |
| 857 | strm.EOL(); |
| 858 | } |
| 859 | Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); |
| 860 | } |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | void |
| 864 | Module::LogMessage (Log *log, const char *format, ...) |
| 865 | { |
| 866 | if (log) |
| 867 | { |
| 868 | StreamString log_message; |
Greg Clayton | 8b35334 | 2012-01-11 01:59:18 +0000 | [diff] [blame] | 869 | GetDescription(&log_message, lldb::eDescriptionLevelFull); |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 870 | log_message.PutCString (": "); |
| 871 | va_list args; |
| 872 | va_start (args, format); |
| 873 | log_message.PrintfVarArg (format, args); |
| 874 | va_end (args); |
| 875 | log->PutCString(log_message.GetString().c_str()); |
| 876 | } |
| 877 | } |
| 878 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 879 | bool |
| 880 | Module::GetModified (bool use_cached_only) |
| 881 | { |
| 882 | if (m_was_modified == false && use_cached_only == false) |
| 883 | { |
| 884 | TimeValue curr_mod_time (m_file.GetModificationTime()); |
| 885 | m_was_modified = curr_mod_time != m_mod_time; |
| 886 | } |
| 887 | return m_was_modified; |
| 888 | } |
| 889 | |
| 890 | bool |
| 891 | Module::SetModified (bool b) |
| 892 | { |
| 893 | const bool prev_value = m_was_modified; |
| 894 | m_was_modified = b; |
| 895 | return prev_value; |
| 896 | } |
| 897 | |
| 898 | |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 899 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 900 | Module::Dump(Stream *s) |
| 901 | { |
| 902 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 903 | //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 904 | s->Indent(); |
| 905 | s->Printf("Module %s/%s%s%s%s\n", |
| 906 | m_file.GetDirectory().AsCString(), |
| 907 | m_file.GetFilename().AsCString(), |
| 908 | m_object_name ? "(" : "", |
| 909 | m_object_name ? m_object_name.GetCString() : "", |
| 910 | m_object_name ? ")" : ""); |
| 911 | |
| 912 | s->IndentMore(); |
| 913 | ObjectFile *objfile = GetObjectFile (); |
| 914 | |
| 915 | if (objfile) |
| 916 | objfile->Dump(s); |
| 917 | |
| 918 | SymbolVendor *symbols = GetSymbolVendor (); |
| 919 | |
| 920 | if (symbols) |
| 921 | symbols->Dump(s); |
| 922 | |
| 923 | s->IndentLess(); |
| 924 | } |
| 925 | |
| 926 | |
| 927 | TypeList* |
| 928 | Module::GetTypeList () |
| 929 | { |
| 930 | SymbolVendor *symbols = GetSymbolVendor (); |
| 931 | if (symbols) |
| 932 | return &symbols->GetTypeList(); |
| 933 | return NULL; |
| 934 | } |
| 935 | |
| 936 | const ConstString & |
| 937 | Module::GetObjectName() const |
| 938 | { |
| 939 | return m_object_name; |
| 940 | } |
| 941 | |
| 942 | ObjectFile * |
| 943 | Module::GetObjectFile() |
| 944 | { |
| 945 | Mutex::Locker locker (m_mutex); |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 946 | if (m_did_load_objfile == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 947 | { |
Greg Clayton | e83e731 | 2010-09-07 23:40:05 +0000 | [diff] [blame] | 948 | m_did_load_objfile = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 949 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 950 | "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 951 | DataBufferSP file_data_sp; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 952 | m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(), |
| 953 | &m_file, |
| 954 | m_object_offset, |
| 955 | m_file.GetByteSize(), |
| 956 | file_data_sp); |
Greg Clayton | 593577a | 2011-09-21 03:57:31 +0000 | [diff] [blame] | 957 | if (m_objfile_sp) |
| 958 | { |
| 959 | // Once we get the object file, update our module with the object file's |
| 960 | // architecture since it might differ in vendor/os if some parts were |
| 961 | // unknown. |
| 962 | m_objfile_sp->GetArchitecture (m_arch); |
| 963 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 964 | } |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 965 | return m_objfile_sp.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | |
| 969 | const Symbol * |
| 970 | Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) |
| 971 | { |
| 972 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 973 | "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", |
| 974 | name.AsCString(), |
| 975 | symbol_type); |
| 976 | ObjectFile *objfile = GetObjectFile(); |
| 977 | if (objfile) |
| 978 | { |
| 979 | Symtab *symtab = objfile->GetSymtab(); |
| 980 | if (symtab) |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 981 | return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 982 | } |
| 983 | return NULL; |
| 984 | } |
| 985 | void |
| 986 | Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) |
| 987 | { |
| 988 | // No need to protect this call using m_mutex all other method calls are |
| 989 | // already thread safe. |
| 990 | |
| 991 | size_t num_indices = symbol_indexes.size(); |
| 992 | if (num_indices > 0) |
| 993 | { |
| 994 | SymbolContext sc; |
| 995 | CalculateSymbolContext (&sc); |
| 996 | for (size_t i = 0; i < num_indices; i++) |
| 997 | { |
| 998 | sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); |
| 999 | if (sc.symbol) |
| 1000 | sc_list.Append (sc); |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | size_t |
Sean Callanan | b96ff33 | 2011-10-13 16:49:47 +0000 | [diff] [blame] | 1006 | Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | { |
| 1008 | // No need to protect this call using m_mutex all other method calls are |
| 1009 | // already thread safe. |
| 1010 | |
| 1011 | |
| 1012 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 1013 | "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", |
| 1014 | name.AsCString(), |
| 1015 | symbol_type); |
| 1016 | const size_t initial_size = sc_list.GetSize(); |
| 1017 | ObjectFile *objfile = GetObjectFile (); |
| 1018 | if (objfile) |
| 1019 | { |
| 1020 | Symtab *symtab = objfile->GetSymtab(); |
| 1021 | if (symtab) |
| 1022 | { |
| 1023 | std::vector<uint32_t> symbol_indexes; |
| 1024 | symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); |
| 1025 | SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); |
| 1026 | } |
| 1027 | } |
| 1028 | return sc_list.GetSize() - initial_size; |
| 1029 | } |
| 1030 | |
| 1031 | size_t |
| 1032 | Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) |
| 1033 | { |
| 1034 | // No need to protect this call using m_mutex all other method calls are |
| 1035 | // already thread safe. |
| 1036 | |
| 1037 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 1038 | "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", |
| 1039 | regex.GetText(), |
| 1040 | symbol_type); |
| 1041 | const size_t initial_size = sc_list.GetSize(); |
| 1042 | ObjectFile *objfile = GetObjectFile (); |
| 1043 | if (objfile) |
| 1044 | { |
| 1045 | Symtab *symtab = objfile->GetSymtab(); |
| 1046 | if (symtab) |
| 1047 | { |
| 1048 | std::vector<uint32_t> symbol_indexes; |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1049 | symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1050 | SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); |
| 1051 | } |
| 1052 | } |
| 1053 | return sc_list.GetSize() - initial_size; |
| 1054 | } |
| 1055 | |
| 1056 | const TimeValue & |
| 1057 | Module::GetModificationTime () const |
| 1058 | { |
| 1059 | return m_mod_time; |
| 1060 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1061 | |
| 1062 | bool |
| 1063 | Module::IsExecutable () |
| 1064 | { |
| 1065 | if (GetObjectFile() == NULL) |
| 1066 | return false; |
| 1067 | else |
| 1068 | return GetObjectFile()->IsExecutable(); |
| 1069 | } |
| 1070 | |
Jim Ingham | b53cb27 | 2011-08-03 01:03:17 +0000 | [diff] [blame] | 1071 | bool |
| 1072 | Module::IsLoadedInTarget (Target *target) |
| 1073 | { |
| 1074 | ObjectFile *obj_file = GetObjectFile(); |
| 1075 | if (obj_file) |
| 1076 | { |
| 1077 | SectionList *sections = obj_file->GetSectionList(); |
| 1078 | if (sections != NULL) |
| 1079 | { |
| 1080 | size_t num_sections = sections->GetSize(); |
Jim Ingham | b53cb27 | 2011-08-03 01:03:17 +0000 | [diff] [blame] | 1081 | for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) |
| 1082 | { |
| 1083 | SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); |
| 1084 | if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) |
| 1085 | { |
| 1086 | return true; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | return false; |
| 1092 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1093 | bool |
| 1094 | Module::SetArchitecture (const ArchSpec &new_arch) |
| 1095 | { |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1096 | if (!m_arch.IsValid()) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1097 | { |
| 1098 | m_arch = new_arch; |
| 1099 | return true; |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1100 | } |
| 1101 | return m_arch == new_arch; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1104 | bool |
| 1105 | Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed) |
| 1106 | { |
| 1107 | changed = false; |
| 1108 | ObjectFile *image_object_file = GetObjectFile(); |
| 1109 | if (image_object_file) |
| 1110 | { |
| 1111 | SectionList *section_list = image_object_file->GetSectionList (); |
| 1112 | if (section_list) |
| 1113 | { |
| 1114 | const size_t num_sections = section_list->GetSize(); |
| 1115 | size_t sect_idx = 0; |
| 1116 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) |
| 1117 | { |
| 1118 | // Iterate through the object file sections to find the |
| 1119 | // first section that starts of file offset zero and that |
| 1120 | // has bytes in the file... |
| 1121 | Section *section = section_list->GetSectionAtIndex (sect_idx).get(); |
| 1122 | if (section) |
| 1123 | { |
| 1124 | if (target.GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + offset)) |
| 1125 | changed = true; |
| 1126 | } |
| 1127 | } |
| 1128 | return sect_idx > 0; |
| 1129 | } |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 1134 | |
| 1135 | bool |
| 1136 | Module::MatchesModuleSpec (const ModuleSpec &module_ref) |
| 1137 | { |
| 1138 | const UUID &uuid = module_ref.GetUUID(); |
| 1139 | |
| 1140 | if (uuid.IsValid()) |
| 1141 | { |
| 1142 | // If the UUID matches, then nothing more needs to match... |
| 1143 | if (uuid == GetUUID()) |
| 1144 | return true; |
| 1145 | else |
| 1146 | return false; |
| 1147 | } |
| 1148 | |
| 1149 | const FileSpec &file_spec = module_ref.GetFileSpec(); |
| 1150 | if (file_spec) |
| 1151 | { |
| 1152 | if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory())) |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); |
| 1157 | if (platform_file_spec) |
| 1158 | { |
| 1159 | if (!FileSpec::Equal (platform_file_spec, m_platform_file, platform_file_spec.GetDirectory())) |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | const ArchSpec &arch = module_ref.GetArchitecture(); |
| 1164 | if (arch.IsValid()) |
| 1165 | { |
| 1166 | if (m_arch != arch) |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | const ConstString &object_name = module_ref.GetObjectName(); |
| 1171 | if (object_name) |
| 1172 | { |
| 1173 | if (object_name != GetObjectName()) |
| 1174 | return false; |
| 1175 | } |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 1179 | bool |
| 1180 | Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const |
| 1181 | { |
| 1182 | Mutex::Locker locker (m_mutex); |
| 1183 | return m_source_mappings.FindFile (orig_spec, new_spec); |
| 1184 | } |
| 1185 | |
Greg Clayton | f9be693 | 2012-03-19 22:22:41 +0000 | [diff] [blame] | 1186 | bool |
| 1187 | Module::RemapSourceFile (const char *path, std::string &new_path) const |
| 1188 | { |
| 1189 | Mutex::Locker locker (m_mutex); |
| 1190 | return m_source_mappings.RemapPath(path, new_path); |
| 1191 | } |
| 1192 | |