1 //===-- lldb.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
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"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/TargetSelect.h"
30 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
31 #include "Plugins/ABI/SysV-ppc/ABISysV_ppc.h"
32 #include "Plugins/ABI/SysV-ppc64/ABISysV_ppc64.h"
33 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
34 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
35 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
36 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
37 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
38 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
39 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
40 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
41 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
42 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
43 #include "Plugins/Process/elf-core/ProcessElfCore.h"
44 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
45 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
46 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
47 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
48 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
49 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
51 #ifndef LLDB_DISABLE_PYTHON
52 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
54 #if defined (__APPLE__)
55 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
56 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
57 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
58 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
59 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
60 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
61 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
62 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
63 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
64 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
65 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
66 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
70 #if defined (__linux__)
71 #include "Plugins/Process/Linux/ProcessLinux.h"
75 #include "lldb/Host/windows/windows.h"
76 #include "Plugins/Process/Windows/DynamicLoaderWindows.h"
77 #include "Plugins/Process/Windows/ProcessWindows.h"
80 #if defined (__FreeBSD__)
81 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
82 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
85 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
86 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
87 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
88 #include "Plugins/MemoryHistory/asan/MemoryHistoryASan.h"
89 #include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h"
92 using namespace lldb_private;
94 static void fatal_error_handler(void *user_data, const std::string& reason,
95 bool gen_crash_diag) {
96 Host::SetCrashDescription(reason.c_str());
101 lldb_private::Initialize ()
103 // Make sure we inialize only once
104 static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
105 static bool g_inited = false;
107 Mutex::Locker locker(g_inited_mutex);
112 #if defined(_MSC_VER)
113 const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
114 if (disable_crash_dialog_var && llvm::StringRef(disable_crash_dialog_var).equals_lower("true"))
116 // This will prevent Windows from displaying a dialog box requiring user interaction when
117 // LLDB crashes. This is mostly useful when automating LLDB, for example via the test
118 // suite, so that a crash in LLDB does not prevent completion of the test suite.
119 ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
121 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
122 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
123 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
124 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
125 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
126 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
131 HostInfo::Initialize();
132 Timer::Initialize ();
133 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
135 // Initialize LLVM and Clang
136 llvm::InitializeAllTargets();
137 llvm::InitializeAllAsmPrinters();
138 llvm::InitializeAllTargetMCs();
139 llvm::InitializeAllDisassemblers();
140 llvm::install_fatal_error_handler(fatal_error_handler, 0);
142 // Initialize plug-ins
143 ABISysV_x86_64::Initialize();
144 ABISysV_ppc::Initialize();
145 ABISysV_ppc64::Initialize();
146 DisassemblerLLVMC::Initialize();
147 ObjectContainerBSDArchive::Initialize();
148 ObjectFileELF::Initialize();
149 SymbolVendorELF::Initialize();
150 SymbolFileDWARF::Initialize();
151 SymbolFileSymtab::Initialize();
152 UnwindAssemblyInstEmulation::Initialize();
153 UnwindAssembly_x86::Initialize();
154 EmulateInstructionARM::Initialize ();
155 EmulateInstructionARM64::Initialize ();
156 DynamicLoaderPOSIXDYLD::Initialize ();
157 PlatformFreeBSD::Initialize();
158 SymbolFileDWARFDebugMap::Initialize();
159 ItaniumABILanguageRuntime::Initialize();
160 #ifndef LLDB_DISABLE_PYTHON
161 ScriptInterpreterPython::InitializePrivate();
162 OperatingSystemPython::Initialize();
164 JITLoaderGDB::Initialize();
165 ProcessElfCore::Initialize();
166 MemoryHistoryASan::Initialize();
167 AddressSanitizerRuntime::Initialize();
169 #if defined (__APPLE__)
170 //----------------------------------------------------------------------
171 // Apple/Darwin hosted plugins
172 //----------------------------------------------------------------------
173 DynamicLoaderMacOSXDYLD::Initialize();
174 DynamicLoaderDarwinKernel::Initialize();
175 AppleObjCRuntimeV2::Initialize();
176 AppleObjCRuntimeV1::Initialize();
177 ObjectContainerUniversalMachO::Initialize();
178 ObjectFileMachO::Initialize();
179 ProcessKDP::Initialize();
180 PlatformDarwinKernel::Initialize();
181 PlatformRemoteiOS::Initialize();
182 PlatformMacOSX::Initialize();
183 PlatformiOSSimulator::Initialize();
184 SystemRuntimeMacOSX::Initialize();
186 #if defined (__linux__)
187 //----------------------------------------------------------------------
188 // Linux hosted plugins
189 //----------------------------------------------------------------------
190 ProcessLinux::Initialize();
193 DynamicLoaderWindows::Initialize();
194 ProcessWindows::Initialize();
196 #if defined (__FreeBSD__)
197 ProcessFreeBSD::Initialize();
200 //----------------------------------------------------------------------
201 // Platform agnostic plugins
202 //----------------------------------------------------------------------
203 PlatformRemoteGDBServer::Initialize ();
204 ProcessGDBRemote::Initialize();
205 DynamicLoaderStatic::Initialize();
207 // Scan for any system or user LLDB plug-ins
208 PluginManager::Initialize();
210 // The process settings need to know about installed plug-ins, so the Settings must be initialized
211 // AFTER PluginManager::Initialize is called.
213 Debugger::SettingsInitialize();
218 lldb_private::WillTerminate()
220 Host::WillTerminate();
224 lldb_private::Terminate ()
226 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
228 // Terminate and unload and loaded system or user LLDB plug-ins
229 PluginManager::Terminate();
230 ABISysV_x86_64::Terminate();
231 ABISysV_ppc::Terminate();
232 ABISysV_ppc64::Terminate();
233 DisassemblerLLVMC::Terminate();
234 ObjectContainerBSDArchive::Terminate();
235 ObjectFileELF::Terminate();
236 SymbolVendorELF::Terminate();
237 SymbolFileDWARF::Terminate();
238 SymbolFileSymtab::Terminate();
239 UnwindAssembly_x86::Terminate();
240 UnwindAssemblyInstEmulation::Terminate();
241 EmulateInstructionARM::Terminate ();
242 EmulateInstructionARM64::Terminate ();
243 DynamicLoaderPOSIXDYLD::Terminate ();
244 PlatformFreeBSD::Terminate();
245 SymbolFileDWARFDebugMap::Terminate();
246 ItaniumABILanguageRuntime::Terminate();
247 #ifndef LLDB_DISABLE_PYTHON
248 OperatingSystemPython::Terminate();
250 JITLoaderGDB::Terminate();
251 ProcessElfCore::Terminate();
252 MemoryHistoryASan::Terminate();
253 AddressSanitizerRuntime::Terminate();
255 #if defined (__APPLE__)
256 DynamicLoaderMacOSXDYLD::Terminate();
257 DynamicLoaderDarwinKernel::Terminate();
258 AppleObjCRuntimeV2::Terminate();
259 AppleObjCRuntimeV1::Terminate();
260 ObjectContainerUniversalMachO::Terminate();
261 ObjectFileMachO::Terminate();
262 ProcessKDP::Terminate();
263 PlatformMacOSX::Terminate();
264 PlatformDarwinKernel::Terminate();
265 PlatformRemoteiOS::Terminate();
266 PlatformiOSSimulator::Terminate();
267 SystemRuntimeMacOSX::Terminate();
270 Debugger::SettingsTerminate ();
273 DynamicLoaderWindows::Terminate();
276 #if defined (__linux__)
277 ProcessLinux::Terminate();
280 #if defined (__FreeBSD__)
281 ProcessFreeBSD::Terminate();
284 ProcessGDBRemote::Terminate();
285 DynamicLoaderStatic::Terminate();
290 #if defined (__APPLE__)
291 extern "C" const unsigned char liblldb_coreVersionString[];
294 #include "clang/Basic/Version.h"
300 return LLDB_REVISION;
309 #ifdef LLDB_REPOSITORY
310 return LLDB_REPOSITORY;
319 lldb_private::GetVersion ()
321 #if defined (__APPLE__)
322 static char g_version_string[32];
323 if (g_version_string[0] == '\0')
325 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
328 version_string += sizeof("PROJECT:") - 1;
330 version_string = "unknown";
332 const char *newline_loc = strchr(version_string, '\n');
334 size_t version_len = sizeof(g_version_string);
337 (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
338 version_len = newline_loc - version_string;
340 ::strncpy(g_version_string, version_string, version_len);
343 return g_version_string;
345 // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
346 static std::string g_version_str;
347 if (g_version_str.empty())
349 g_version_str += "lldb version ";
350 g_version_str += CLANG_VERSION_STRING;
351 const char * lldb_repo = GetLLDBRepository();
354 g_version_str += " (";
355 g_version_str += lldb_repo;
358 const char *lldb_rev = GetLLDBRevision();
361 g_version_str += " revision ";
362 g_version_str += lldb_rev;
364 std::string clang_rev (clang::getClangRevision());
365 if (clang_rev.length() > 0)
367 g_version_str += " clang revision ";
368 g_version_str += clang_rev;
370 std::string llvm_rev (clang::getLLVMRevision());
371 if (llvm_rev.length() > 0)
373 g_version_str += " llvm revision ";
374 g_version_str += llvm_rev;
378 g_version_str += ")";
380 return g_version_str.c_str();
385 lldb_private::GetVoteAsCString (Vote vote)
389 case eVoteNo: return "no";
390 case eVoteNoOpinion: return "no opinion";
391 case eVoteYes: return "yes";
398 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
402 case eSectionTypeInvalid: return "invalid";
403 case eSectionTypeCode: return "code";
404 case eSectionTypeContainer: return "container";
405 case eSectionTypeData: return "data";
406 case eSectionTypeDataCString: return "data-cstr";
407 case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
408 case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
409 case eSectionTypeData4: return "data-4-byte";
410 case eSectionTypeData8: return "data-8-byte";
411 case eSectionTypeData16: return "data-16-byte";
412 case eSectionTypeDataPointers: return "data-ptrs";
413 case eSectionTypeDebug: return "debug";
414 case eSectionTypeZeroFill: return "zero-fill";
415 case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
416 case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
417 case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
418 case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
419 case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
420 case eSectionTypeDWARFDebugInfo: return "dwarf-info";
421 case eSectionTypeDWARFDebugLine: return "dwarf-line";
422 case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
423 case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
424 case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
425 case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
426 case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
427 case eSectionTypeDWARFDebugStr: return "dwarf-str";
428 case eSectionTypeELFSymbolTable: return "elf-symbol-table";
429 case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
430 case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
431 case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
432 case eSectionTypeDWARFAppleNames: return "apple-names";
433 case eSectionTypeDWARFAppleTypes: return "apple-types";
434 case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
435 case eSectionTypeDWARFAppleObjC: return "apple-objc";
436 case eSectionTypeEHFrame: return "eh-frame";
437 case eSectionTypeCompactUnwind: return "compact-unwind";
438 case eSectionTypeOther: return "regular";
445 lldb_private::NameMatches (const char *name,
446 NameMatchType match_type,
449 if (match_type == eNameMatchIgnore)
457 llvm::StringRef name_sref(name);
458 llvm::StringRef match_sref(match);
461 case eNameMatchIgnore: // This case cannot occur: tested before
463 case eNameMatchEquals: return name_sref == match_sref;
464 case eNameMatchContains: return name_sref.find (match_sref) != llvm::StringRef::npos;
465 case eNameMatchStartsWith: return name_sref.startswith (match_sref);
466 case eNameMatchEndsWith: return name_sref.endswith (match_sref);
467 case eNameMatchRegularExpression:
469 RegularExpression regex (match);
470 return regex.Execute (name);