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/MacOSX-arm64/ABIMacOSX_arm64.h"
31 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
32 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
33 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
34 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
35 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
36 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
37 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
38 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
39 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
40 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
41 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
42 #include "Plugins/Platform/Linux/PlatformLinux.h"
43 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
44 #include "Plugins/Platform/Windows/PlatformWindows.h"
45 #include "Plugins/Platform/Kalimba/PlatformKalimba.h"
46 #include "Plugins/Process/elf-core/ProcessElfCore.h"
47 #include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
48 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
49 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
50 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
51 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
52 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
53 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
55 #ifndef LLDB_DISABLE_PYTHON
56 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
58 #if defined (__APPLE__)
59 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
60 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
61 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
62 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
63 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
64 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
65 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
66 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
67 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
68 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
69 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
70 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
74 #if defined (__linux__)
75 #include "Plugins/Process/Linux/ProcessLinux.h"
79 #include "Plugins/Process/Windows/ProcessWindows.h"
82 #if defined (__FreeBSD__)
83 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
84 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
87 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
88 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
89 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.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 HostInfo::Initialize();
113 Timer::Initialize ();
114 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
116 // Initialize LLVM and Clang
117 llvm::InitializeAllTargets();
118 llvm::InitializeAllAsmPrinters();
119 llvm::InitializeAllTargetMCs();
120 llvm::InitializeAllDisassemblers();
121 llvm::install_fatal_error_handler(fatal_error_handler, 0);
123 // Initialize plug-ins
124 ABIMacOSX_arm64::Initialize();
125 ABISysV_x86_64::Initialize();
126 DisassemblerLLVMC::Initialize();
127 ObjectContainerBSDArchive::Initialize();
128 ObjectFileELF::Initialize();
129 SymbolVendorELF::Initialize();
130 SymbolFileDWARF::Initialize();
131 SymbolFileSymtab::Initialize();
132 UnwindAssemblyInstEmulation::Initialize();
133 UnwindAssembly_x86::Initialize();
134 EmulateInstructionARM::Initialize ();
135 EmulateInstructionARM64::Initialize ();
136 DynamicLoaderPOSIXDYLD::Initialize ();
137 PlatformFreeBSD::Initialize();
138 PlatformKalimba::Initialize();
139 SymbolFileDWARFDebugMap::Initialize();
140 ItaniumABILanguageRuntime::Initialize();
141 #ifndef LLDB_DISABLE_PYTHON
142 ScriptInterpreterPython::InitializePrivate();
143 OperatingSystemPython::Initialize();
145 JITLoaderGDB::Initialize();
146 ProcessElfCore::Initialize();
148 #if defined (__APPLE__)
149 //----------------------------------------------------------------------
150 // Apple/Darwin hosted plugins
151 //----------------------------------------------------------------------
152 DynamicLoaderMacOSXDYLD::Initialize();
153 DynamicLoaderDarwinKernel::Initialize();
154 AppleObjCRuntimeV2::Initialize();
155 AppleObjCRuntimeV1::Initialize();
156 ObjectContainerUniversalMachO::Initialize();
157 ObjectFileMachO::Initialize();
158 ProcessKDP::Initialize();
159 PlatformDarwinKernel::Initialize();
160 PlatformRemoteiOS::Initialize();
161 PlatformMacOSX::Initialize();
162 PlatformiOSSimulator::Initialize();
163 SystemRuntimeMacOSX::Initialize();
165 #if defined (__linux__)
166 //----------------------------------------------------------------------
167 // Linux hosted plugins
168 //----------------------------------------------------------------------
169 ProcessLinux::Initialize();
172 ProcessWindows::Initialize();
174 #if defined (__FreeBSD__)
175 ProcessFreeBSD::Initialize();
178 //----------------------------------------------------------------------
179 // Platform agnostic plugins
180 //----------------------------------------------------------------------
181 PlatformRemoteGDBServer::Initialize ();
182 ProcessGDBRemote::Initialize();
183 DynamicLoaderStatic::Initialize();
185 // Scan for any system or user LLDB plug-ins
186 PluginManager::Initialize();
188 // The process settings need to know about installed plug-ins, so the Settings must be initialized
189 // AFTER PluginManager::Initialize is called.
191 Debugger::SettingsInitialize();
196 lldb_private::WillTerminate()
198 Host::WillTerminate();
202 lldb_private::Terminate ()
204 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
206 // Terminate and unload and loaded system or user LLDB plug-ins
207 PluginManager::Terminate();
208 ABIMacOSX_arm64::Terminate();
209 ABISysV_x86_64::Terminate();
210 DisassemblerLLVMC::Terminate();
211 ObjectContainerBSDArchive::Terminate();
212 ObjectFileELF::Terminate();
213 SymbolVendorELF::Terminate();
214 SymbolFileDWARF::Terminate();
215 SymbolFileSymtab::Terminate();
216 UnwindAssembly_x86::Terminate();
217 UnwindAssemblyInstEmulation::Terminate();
218 EmulateInstructionARM::Terminate ();
219 EmulateInstructionARM64::Terminate ();
220 DynamicLoaderPOSIXDYLD::Terminate ();
221 PlatformFreeBSD::Terminate();
222 PlatformKalimba::Terminate();
223 SymbolFileDWARFDebugMap::Terminate();
224 ItaniumABILanguageRuntime::Terminate();
225 #ifndef LLDB_DISABLE_PYTHON
226 OperatingSystemPython::Terminate();
228 JITLoaderGDB::Terminate();
229 ProcessElfCore::Terminate();
231 #if defined (__APPLE__)
232 DynamicLoaderMacOSXDYLD::Terminate();
233 DynamicLoaderDarwinKernel::Terminate();
234 AppleObjCRuntimeV2::Terminate();
235 AppleObjCRuntimeV1::Terminate();
236 ObjectContainerUniversalMachO::Terminate();
237 ObjectFileMachO::Terminate();
238 ProcessKDP::Terminate();
239 PlatformMacOSX::Terminate();
240 PlatformDarwinKernel::Terminate();
241 PlatformRemoteiOS::Terminate();
242 PlatformiOSSimulator::Terminate();
243 SystemRuntimeMacOSX::Terminate();
246 Debugger::SettingsTerminate ();
248 #if defined (__linux__)
249 ProcessLinux::Terminate();
252 #if defined (__FreeBSD__)
253 ProcessFreeBSD::Terminate();
256 ProcessGDBRemote::Terminate();
257 DynamicLoaderStatic::Terminate();
262 #if defined (__APPLE__)
263 extern "C" const unsigned char liblldb_coreVersionString[];
266 #include "clang/Basic/Version.h"
272 return LLDB_REVISION;
281 #ifdef LLDB_REPOSITORY
282 return LLDB_REPOSITORY;
291 lldb_private::GetVersion ()
293 #if defined (__APPLE__)
294 static char g_version_string[32];
295 if (g_version_string[0] == '\0')
297 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
300 version_string += sizeof("PROJECT:") - 1;
302 version_string = "unknown";
304 const char *newline_loc = strchr(version_string, '\n');
306 size_t version_len = sizeof(g_version_string);
309 (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
310 version_len = newline_loc - version_string;
312 ::strncpy(g_version_string, version_string, version_len);
315 return g_version_string;
317 // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
318 static std::string g_version_str;
319 if (g_version_str.empty())
321 g_version_str += "lldb version ";
322 g_version_str += CLANG_VERSION_STRING;
323 const char * lldb_repo = GetLLDBRepository();
326 g_version_str += " (";
327 g_version_str += lldb_repo;
330 const char *lldb_rev = GetLLDBRevision();
333 g_version_str += " revision ";
334 g_version_str += lldb_rev;
336 std::string clang_rev (clang::getClangRevision());
337 if (clang_rev.length() > 0)
339 g_version_str += " clang revision ";
340 g_version_str += clang_rev;
342 std::string llvm_rev (clang::getLLVMRevision());
343 if (llvm_rev.length() > 0)
345 g_version_str += " llvm revision ";
346 g_version_str += llvm_rev;
350 g_version_str += ")";
352 return g_version_str.c_str();
357 lldb_private::GetVoteAsCString (Vote vote)
361 case eVoteNo: return "no";
362 case eVoteNoOpinion: return "no opinion";
363 case eVoteYes: return "yes";
370 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
374 case eSectionTypeInvalid: return "invalid";
375 case eSectionTypeCode: return "code";
376 case eSectionTypeContainer: return "container";
377 case eSectionTypeData: return "data";
378 case eSectionTypeDataCString: return "data-cstr";
379 case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
380 case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
381 case eSectionTypeData4: return "data-4-byte";
382 case eSectionTypeData8: return "data-8-byte";
383 case eSectionTypeData16: return "data-16-byte";
384 case eSectionTypeDataPointers: return "data-ptrs";
385 case eSectionTypeDebug: return "debug";
386 case eSectionTypeZeroFill: return "zero-fill";
387 case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
388 case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
389 case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
390 case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
391 case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
392 case eSectionTypeDWARFDebugInfo: return "dwarf-info";
393 case eSectionTypeDWARFDebugLine: return "dwarf-line";
394 case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
395 case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
396 case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
397 case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
398 case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
399 case eSectionTypeDWARFDebugStr: return "dwarf-str";
400 case eSectionTypeELFSymbolTable: return "elf-symbol-table";
401 case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
402 case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
403 case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
404 case eSectionTypeDWARFAppleNames: return "apple-names";
405 case eSectionTypeDWARFAppleTypes: return "apple-types";
406 case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
407 case eSectionTypeDWARFAppleObjC: return "apple-objc";
408 case eSectionTypeEHFrame: return "eh-frame";
409 case eSectionTypeOther: return "regular";
416 lldb_private::NameMatches (const char *name,
417 NameMatchType match_type,
420 if (match_type == eNameMatchIgnore)
428 llvm::StringRef name_sref(name);
429 llvm::StringRef match_sref(match);
432 case eNameMatchIgnore: // This case cannot occur: tested before
434 case eNameMatchEquals: return name_sref == match_sref;
435 case eNameMatchContains: return name_sref.find (match_sref) != llvm::StringRef::npos;
436 case eNameMatchStartsWith: return name_sref.startswith (match_sref);
437 case eNameMatchEndsWith: return name_sref.endswith (match_sref);
438 case eNameMatchRegularExpression:
440 RegularExpression regex (match);
441 return regex.Execute (name);