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/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"
49 #ifndef LLDB_DISABLE_PYTHON
50 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
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"
68 #if defined (__linux__)
69 #include "Plugins/Process/Linux/ProcessLinux.h"
73 #include "Plugins/Process/Windows/ProcessWindows.h"
76 #if defined (__FreeBSD__)
77 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
78 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
81 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
82 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
83 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
86 using namespace lldb_private;
88 static void fatal_error_handler(void *user_data, const std::string& reason,
89 bool gen_crash_diag) {
90 Host::SetCrashDescription(reason.c_str());
95 lldb_private::Initialize ()
97 // Make sure we inialize only once
98 static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
99 static bool g_inited = false;
101 Mutex::Locker locker(g_inited_mutex);
106 HostInfo::Initialize();
107 Timer::Initialize ();
108 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
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);
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();
137 JITLoaderGDB::Initialize();
138 ProcessElfCore::Initialize();
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();
157 #if defined (__linux__)
158 //----------------------------------------------------------------------
159 // Linux hosted plugins
160 //----------------------------------------------------------------------
161 ProcessLinux::Initialize();
164 ProcessWindows::Initialize();
166 #if defined (__FreeBSD__)
167 ProcessFreeBSD::Initialize();
170 //----------------------------------------------------------------------
171 // Platform agnostic plugins
172 //----------------------------------------------------------------------
173 PlatformRemoteGDBServer::Initialize ();
174 ProcessGDBRemote::Initialize();
175 DynamicLoaderStatic::Initialize();
177 // Scan for any system or user LLDB plug-ins
178 PluginManager::Initialize();
180 // The process settings need to know about installed plug-ins, so the Settings must be initialized
181 // AFTER PluginManager::Initialize is called.
183 Debugger::SettingsInitialize();
188 lldb_private::WillTerminate()
190 Host::WillTerminate();
194 lldb_private::Terminate ()
196 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
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();
218 JITLoaderGDB::Terminate();
219 ProcessElfCore::Terminate();
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();
236 Debugger::SettingsTerminate ();
238 #if defined (__linux__)
239 ProcessLinux::Terminate();
242 #if defined (__FreeBSD__)
243 ProcessFreeBSD::Terminate();
246 ProcessGDBRemote::Terminate();
247 DynamicLoaderStatic::Terminate();
252 #if defined (__APPLE__)
253 extern "C" const unsigned char liblldb_coreVersionString[];
256 #include "clang/Basic/Version.h"
262 return LLDB_REVISION;
271 #ifdef LLDB_REPOSITORY
272 return LLDB_REPOSITORY;
281 lldb_private::GetVersion ()
283 #if defined (__APPLE__)
284 static char g_version_string[32];
285 if (g_version_string[0] == '\0')
287 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
290 version_string += sizeof("PROJECT:") - 1;
292 version_string = "unknown";
294 const char *newline_loc = strchr(version_string, '\n');
296 size_t version_len = sizeof(g_version_string);
299 (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
300 version_len = newline_loc - version_string;
302 ::strncpy(g_version_string, version_string, version_len);
305 return g_version_string;
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())
311 g_version_str += "lldb version ";
312 g_version_str += CLANG_VERSION_STRING;
313 const char * lldb_repo = GetLLDBRepository();
316 g_version_str += " (";
317 g_version_str += lldb_repo;
320 const char *lldb_rev = GetLLDBRevision();
323 g_version_str += " revision ";
324 g_version_str += lldb_rev;
326 std::string clang_rev (clang::getClangRevision());
327 if (clang_rev.length() > 0)
329 g_version_str += " clang revision ";
330 g_version_str += clang_rev;
332 std::string llvm_rev (clang::getLLVMRevision());
333 if (llvm_rev.length() > 0)
335 g_version_str += " llvm revision ";
336 g_version_str += llvm_rev;
340 g_version_str += ")";
342 return g_version_str.c_str();
347 lldb_private::GetVoteAsCString (Vote vote)
351 case eVoteNo: return "no";
352 case eVoteNoOpinion: return "no opinion";
353 case eVoteYes: return "yes";
360 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
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";
406 lldb_private::NameMatches (const char *name,
407 NameMatchType match_type,
410 if (match_type == eNameMatchIgnore)
418 llvm::StringRef name_sref(name);
419 llvm::StringRef match_sref(match);
422 case eNameMatchIgnore: // This case cannot occur: tested before
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:
430 RegularExpression regex (match);
431 return regex.Execute (name);