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