1 //===-- lldb.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/lldb-python.h" 11 12 #include "lldb/lldb-private.h" 13 #include "lldb/lldb-private-log.h" 14 #include "lldb/Core/ArchSpec.h" 15 #include "lldb/Core/Debugger.h" 16 #include "lldb/Core/Log.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/RegularExpression.h" 19 #include "lldb/Core/Timer.h" 20 #include "lldb/Host/Host.h" 21 #include "lldb/Host/Mutex.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Target/Thread.h" 24 25 #include "llvm/ADT/StringRef.h" 26 27 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h" 28 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h" 29 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h" 30 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h" 31 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h" 32 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h" 33 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" 34 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h" 35 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h" 36 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h" 37 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h" 38 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h" 39 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h" 40 #include "Plugins/Platform/POSIX/PlatformPOSIX.h" 41 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h" 42 #ifndef LLDB_DISABLE_PYTHON 43 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h" 44 #endif 45 #if defined (__APPLE__) 46 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" 47 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" 48 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h" 49 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h" 50 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h" 51 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" 52 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h" 53 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h" 54 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" 55 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h" 56 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h" 57 #endif 58 59 #if defined(__linux__) || defined(__FreeBSD__) 60 #include "Plugins/Process/elf-core/ProcessElfCore.h" 61 #endif 62 63 #if defined (__linux__) 64 #include "Plugins/Process/Linux/ProcessLinux.h" 65 #endif 66 67 #if defined (__FreeBSD__) 68 #include "Plugins/Process/POSIX/ProcessPOSIX.h" 69 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h" 70 #endif 71 72 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h" 73 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h" 74 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" 75 76 using namespace lldb; 77 using namespace lldb_private; 78 79 void 80 lldb_private::Initialize () 81 { 82 // Make sure we inialize only once 83 static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive); 84 static bool g_inited = false; 85 86 Mutex::Locker locker(g_inited_mutex); 87 if (!g_inited) 88 { 89 g_inited = true; 90 Log::Initialize(); 91 Timer::Initialize (); 92 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 93 94 ABISysV_x86_64::Initialize(); 95 DisassemblerLLVMC::Initialize(); 96 ObjectContainerBSDArchive::Initialize(); 97 ObjectFileELF::Initialize(); 98 SymbolVendorELF::Initialize(); 99 SymbolFileDWARF::Initialize(); 100 SymbolFileSymtab::Initialize(); 101 UnwindAssemblyInstEmulation::Initialize(); 102 UnwindAssembly_x86::Initialize(); 103 EmulateInstructionARM::Initialize (); 104 DynamicLoaderPOSIXDYLD::Initialize (); 105 PlatformFreeBSD::Initialize(); 106 SymbolFileDWARFDebugMap::Initialize(); 107 ItaniumABILanguageRuntime::Initialize(); 108 #ifndef LLDB_DISABLE_PYTHON 109 OperatingSystemPython::Initialize(); 110 #endif 111 112 #if defined (__APPLE__) 113 //---------------------------------------------------------------------- 114 // Apple/Darwin hosted plugins 115 //---------------------------------------------------------------------- 116 DynamicLoaderMacOSXDYLD::Initialize(); 117 DynamicLoaderDarwinKernel::Initialize(); 118 AppleObjCRuntimeV2::Initialize(); 119 AppleObjCRuntimeV1::Initialize(); 120 ObjectContainerUniversalMachO::Initialize(); 121 ObjectFileMachO::Initialize(); 122 ProcessKDP::Initialize(); 123 PlatformDarwinKernel::Initialize(); 124 PlatformRemoteiOS::Initialize(); 125 PlatformMacOSX::Initialize(); 126 PlatformiOSSimulator::Initialize(); 127 #endif 128 #if defined (__linux__) 129 //---------------------------------------------------------------------- 130 // Linux hosted plugins 131 //---------------------------------------------------------------------- 132 ProcessLinux::Initialize(); 133 #endif 134 #if defined (__FreeBSD__) 135 ProcessFreeBSD::Initialize(); 136 #endif 137 138 #if defined(__linux__) || defined(__FreeBSD__) 139 ProcessElfCore::Initialize(); 140 #endif 141 //---------------------------------------------------------------------- 142 // Platform agnostic plugins 143 //---------------------------------------------------------------------- 144 PlatformRemoteGDBServer::Initialize (); 145 ProcessGDBRemote::Initialize(); 146 DynamicLoaderStatic::Initialize(); 147 148 // Scan for any system or user LLDB plug-ins 149 PluginManager::Initialize(); 150 151 // The process settings need to know about installed plug-ins, so the Settings must be initialized 152 // AFTER PluginManager::Initialize is called. 153 154 Debugger::SettingsInitialize(); 155 } 156 } 157 158 void 159 lldb_private::WillTerminate() 160 { 161 Host::WillTerminate(); 162 } 163 164 void 165 lldb_private::Terminate () 166 { 167 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 168 169 // Terminate and unload and loaded system or user LLDB plug-ins 170 PluginManager::Terminate(); 171 ABISysV_x86_64::Terminate(); 172 DisassemblerLLVMC::Terminate(); 173 ObjectContainerBSDArchive::Terminate(); 174 ObjectFileELF::Terminate(); 175 SymbolVendorELF::Terminate(); 176 SymbolFileDWARF::Terminate(); 177 SymbolFileSymtab::Terminate(); 178 UnwindAssembly_x86::Terminate(); 179 UnwindAssemblyInstEmulation::Terminate(); 180 EmulateInstructionARM::Terminate (); 181 DynamicLoaderPOSIXDYLD::Terminate (); 182 PlatformFreeBSD::Terminate(); 183 SymbolFileDWARFDebugMap::Terminate(); 184 ItaniumABILanguageRuntime::Terminate(); 185 #ifndef LLDB_DISABLE_PYTHON 186 OperatingSystemPython::Terminate(); 187 #endif 188 189 #if defined (__APPLE__) 190 DynamicLoaderMacOSXDYLD::Terminate(); 191 DynamicLoaderDarwinKernel::Terminate(); 192 AppleObjCRuntimeV2::Terminate(); 193 AppleObjCRuntimeV1::Terminate(); 194 ObjectContainerUniversalMachO::Terminate(); 195 ObjectFileMachO::Terminate(); 196 ProcessKDP::Terminate(); 197 PlatformMacOSX::Terminate(); 198 PlatformDarwinKernel::Terminate(); 199 PlatformRemoteiOS::Terminate(); 200 PlatformiOSSimulator::Terminate(); 201 #endif 202 203 Debugger::SettingsTerminate (); 204 205 #if defined (__linux__) 206 ProcessLinux::Terminate(); 207 #endif 208 209 #if defined (__FreeBSD__) 210 ProcessFreeBSD::Terminate(); 211 #endif 212 213 #if defined(__linux__) || defined(__FreeBSD__) 214 ProcessElfCore::Terminate(); 215 #endif 216 ProcessGDBRemote::Terminate(); 217 DynamicLoaderStatic::Terminate(); 218 219 Log::Terminate(); 220 } 221 222 #if defined (__APPLE__) 223 extern "C" const unsigned char liblldb_coreVersionString[]; 224 #else 225 226 #include "clang/Basic/Version.h" 227 228 static const char * 229 GetLLDBRevision() 230 { 231 #ifdef LLDB_REVISION 232 return LLDB_REVISION; 233 #else 234 return NULL; 235 #endif 236 } 237 238 static const char * 239 GetLLDBRepository() 240 { 241 #ifdef LLDB_REPOSITORY 242 return LLDB_REPOSITORY; 243 #else 244 return NULL; 245 #endif 246 } 247 248 #endif 249 250 const char * 251 lldb_private::GetVersion () 252 { 253 #if defined (__APPLE__) 254 static char g_version_string[32]; 255 if (g_version_string[0] == '\0') 256 { 257 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:"); 258 259 if (version_string) 260 version_string += sizeof("PROJECT:") - 1; 261 else 262 version_string = "unknown"; 263 264 const char *newline_loc = strchr(version_string, '\n'); 265 266 size_t version_len = sizeof(g_version_string); 267 268 if (newline_loc && (newline_loc - version_string < version_len)) 269 version_len = newline_loc - version_string; 270 271 ::strncpy(g_version_string, version_string, version_len); 272 } 273 274 return g_version_string; 275 #else 276 // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool. 277 static std::string g_version_str; 278 if (g_version_str.empty()) 279 { 280 g_version_str += "lldb version "; 281 g_version_str += CLANG_VERSION_STRING; 282 const char * lldb_repo = GetLLDBRepository(); 283 if (lldb_repo) 284 { 285 g_version_str += " ("; 286 g_version_str += lldb_repo; 287 } 288 289 const char *lldb_rev = GetLLDBRevision(); 290 if (lldb_rev) 291 { 292 g_version_str += " revision "; 293 g_version_str += lldb_rev; 294 } 295 std::string clang_rev (clang::getClangRevision()); 296 if (clang_rev.length() > 0) 297 { 298 g_version_str += " clang revision "; 299 g_version_str += clang_rev; 300 } 301 std::string llvm_rev (clang::getLLVMRevision()); 302 if (llvm_rev.length() > 0) 303 { 304 g_version_str += " llvm revision "; 305 g_version_str += llvm_rev; 306 } 307 308 if (lldb_repo) 309 g_version_str += ")"; 310 } 311 return g_version_str.c_str(); 312 #endif 313 } 314 315 const char * 316 lldb_private::GetVoteAsCString (Vote vote) 317 { 318 switch (vote) 319 { 320 case eVoteNo: return "no"; 321 case eVoteNoOpinion: return "no opinion"; 322 case eVoteYes: return "yes"; 323 } 324 return "invalid"; 325 } 326 327 328 const char * 329 lldb_private::GetSectionTypeAsCString (SectionType sect_type) 330 { 331 switch (sect_type) 332 { 333 case eSectionTypeInvalid: return "invalid"; 334 case eSectionTypeCode: return "code"; 335 case eSectionTypeContainer: return "container"; 336 case eSectionTypeData: return "data"; 337 case eSectionTypeDataCString: return "data-cstr"; 338 case eSectionTypeDataCStringPointers: return "data-cstr-ptr"; 339 case eSectionTypeDataSymbolAddress: return "data-symbol-addr"; 340 case eSectionTypeData4: return "data-4-byte"; 341 case eSectionTypeData8: return "data-8-byte"; 342 case eSectionTypeData16: return "data-16-byte"; 343 case eSectionTypeDataPointers: return "data-ptrs"; 344 case eSectionTypeDebug: return "debug"; 345 case eSectionTypeZeroFill: return "zero-fill"; 346 case eSectionTypeDataObjCMessageRefs: return "objc-message-refs"; 347 case eSectionTypeDataObjCCFStrings: return "objc-cfstrings"; 348 case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev"; 349 case eSectionTypeDWARFDebugAranges: return "dwarf-aranges"; 350 case eSectionTypeDWARFDebugFrame: return "dwarf-frame"; 351 case eSectionTypeDWARFDebugInfo: return "dwarf-info"; 352 case eSectionTypeDWARFDebugLine: return "dwarf-line"; 353 case eSectionTypeDWARFDebugLoc: return "dwarf-loc"; 354 case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo"; 355 case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames"; 356 case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes"; 357 case eSectionTypeDWARFDebugRanges: return "dwarf-ranges"; 358 case eSectionTypeDWARFDebugStr: return "dwarf-str"; 359 case eSectionTypeELFSymbolTable: return "elf-symbol-table"; 360 case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols"; 361 case eSectionTypeELFRelocationEntries: return "elf-relocation-entries"; 362 case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info"; 363 case eSectionTypeDWARFAppleNames: return "apple-names"; 364 case eSectionTypeDWARFAppleTypes: return "apple-types"; 365 case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces"; 366 case eSectionTypeDWARFAppleObjC: return "apple-objc"; 367 case eSectionTypeEHFrame: return "eh-frame"; 368 case eSectionTypeOther: return "regular"; 369 } 370 return "unknown"; 371 372 } 373 374 bool 375 lldb_private::NameMatches (const char *name, 376 NameMatchType match_type, 377 const char *match) 378 { 379 if (match_type == eNameMatchIgnore) 380 return true; 381 382 if (name == match) 383 return true; 384 385 if (name && match) 386 { 387 llvm::StringRef name_sref(name); 388 llvm::StringRef match_sref(match); 389 switch (match_type) 390 { 391 case eNameMatchIgnore: // This case cannot occur: tested before 392 return true; 393 case eNameMatchEquals: return name_sref == match_sref; 394 case eNameMatchContains: return name_sref.find (match_sref) != llvm::StringRef::npos; 395 case eNameMatchStartsWith: return name_sref.startswith (match_sref); 396 case eNameMatchEndsWith: return name_sref.endswith (match_sref); 397 case eNameMatchRegularExpression: 398 { 399 RegularExpression regex (match); 400 return regex.Execute (name); 401 } 402 break; 403 } 404 } 405 return false; 406 } 407