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