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/Mutex.h"
22 #include "lldb/Interpreter/ScriptInterpreterPython.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
26 #include "llvm/ADT/StringRef.h"
28 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
29 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
30 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
31 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
32 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
33 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
34 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
35 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
36 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
37 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
38 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
39 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
40 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
41 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
42 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
43 #ifndef LLDB_DISABLE_PYTHON
44 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
46 #if defined (__APPLE__)
47 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
48 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
49 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
50 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
51 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
52 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
53 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
54 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
55 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
56 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
57 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
58 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
61 #if defined(__linux__) || defined(__FreeBSD__)
62 #include "Plugins/Process/elf-core/ProcessElfCore.h"
65 #if defined (__linux__)
66 #include "Plugins/Process/Linux/ProcessLinux.h"
69 #if defined (__FreeBSD__)
70 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
71 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
74 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
75 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
76 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
79 using namespace lldb_private;
82 lldb_private::Initialize ()
84 // Make sure we inialize only once
85 static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
86 static bool g_inited = false;
88 Mutex::Locker locker(g_inited_mutex);
94 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
96 ABISysV_x86_64::Initialize();
97 DisassemblerLLVMC::Initialize();
98 ObjectContainerBSDArchive::Initialize();
99 ObjectFileELF::Initialize();
100 SymbolVendorELF::Initialize();
101 SymbolFileDWARF::Initialize();
102 SymbolFileSymtab::Initialize();
103 UnwindAssemblyInstEmulation::Initialize();
104 UnwindAssembly_x86::Initialize();
105 EmulateInstructionARM::Initialize ();
106 DynamicLoaderPOSIXDYLD::Initialize ();
107 PlatformFreeBSD::Initialize();
108 SymbolFileDWARFDebugMap::Initialize();
109 ItaniumABILanguageRuntime::Initialize();
110 #ifndef LLDB_DISABLE_PYTHON
111 ScriptInterpreterPython::InitializePrivate();
112 OperatingSystemPython::Initialize();
115 #if defined (__APPLE__)
116 //----------------------------------------------------------------------
117 // Apple/Darwin hosted plugins
118 //----------------------------------------------------------------------
119 DynamicLoaderMacOSXDYLD::Initialize();
120 DynamicLoaderDarwinKernel::Initialize();
121 AppleObjCRuntimeV2::Initialize();
122 AppleObjCRuntimeV1::Initialize();
123 ObjectContainerUniversalMachO::Initialize();
124 ObjectFileMachO::Initialize();
125 ProcessKDP::Initialize();
126 PlatformDarwinKernel::Initialize();
127 PlatformRemoteiOS::Initialize();
128 PlatformMacOSX::Initialize();
129 PlatformiOSSimulator::Initialize();
130 SystemRuntimeMacOSX::Initialize();
132 #if defined (__linux__)
133 //----------------------------------------------------------------------
134 // Linux hosted plugins
135 //----------------------------------------------------------------------
136 ProcessLinux::Initialize();
138 #if defined (__FreeBSD__)
139 ProcessFreeBSD::Initialize();
142 #if defined(__linux__) || defined(__FreeBSD__)
143 ProcessElfCore::Initialize();
145 //----------------------------------------------------------------------
146 // Platform agnostic plugins
147 //----------------------------------------------------------------------
148 PlatformRemoteGDBServer::Initialize ();
149 ProcessGDBRemote::Initialize();
150 DynamicLoaderStatic::Initialize();
152 // Scan for any system or user LLDB plug-ins
153 PluginManager::Initialize();
155 // The process settings need to know about installed plug-ins, so the Settings must be initialized
156 // AFTER PluginManager::Initialize is called.
158 Debugger::SettingsInitialize();
163 lldb_private::WillTerminate()
165 Host::WillTerminate();
169 lldb_private::Terminate ()
171 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
173 // Terminate and unload and loaded system or user LLDB plug-ins
174 PluginManager::Terminate();
175 ABISysV_x86_64::Terminate();
176 DisassemblerLLVMC::Terminate();
177 ObjectContainerBSDArchive::Terminate();
178 ObjectFileELF::Terminate();
179 SymbolVendorELF::Terminate();
180 SymbolFileDWARF::Terminate();
181 SymbolFileSymtab::Terminate();
182 UnwindAssembly_x86::Terminate();
183 UnwindAssemblyInstEmulation::Terminate();
184 EmulateInstructionARM::Terminate ();
185 DynamicLoaderPOSIXDYLD::Terminate ();
186 PlatformFreeBSD::Terminate();
187 SymbolFileDWARFDebugMap::Terminate();
188 ItaniumABILanguageRuntime::Terminate();
189 #ifndef LLDB_DISABLE_PYTHON
190 OperatingSystemPython::Terminate();
193 #if defined (__APPLE__)
194 DynamicLoaderMacOSXDYLD::Terminate();
195 DynamicLoaderDarwinKernel::Terminate();
196 AppleObjCRuntimeV2::Terminate();
197 AppleObjCRuntimeV1::Terminate();
198 ObjectContainerUniversalMachO::Terminate();
199 ObjectFileMachO::Terminate();
200 ProcessKDP::Terminate();
201 PlatformMacOSX::Terminate();
202 PlatformDarwinKernel::Terminate();
203 PlatformRemoteiOS::Terminate();
204 PlatformiOSSimulator::Terminate();
205 SystemRuntimeMacOSX::Terminate();
208 Debugger::SettingsTerminate ();
210 #if defined (__linux__)
211 ProcessLinux::Terminate();
214 #if defined (__FreeBSD__)
215 ProcessFreeBSD::Terminate();
218 #if defined(__linux__) || defined(__FreeBSD__)
219 ProcessElfCore::Terminate();
221 ProcessGDBRemote::Terminate();
222 DynamicLoaderStatic::Terminate();
227 #if defined (__APPLE__)
228 extern "C" const unsigned char liblldb_coreVersionString[];
231 #include "clang/Basic/Version.h"
237 return LLDB_REVISION;
246 #ifdef LLDB_REPOSITORY
247 return LLDB_REPOSITORY;
256 lldb_private::GetVersion ()
258 #if defined (__APPLE__)
259 static char g_version_string[32];
260 if (g_version_string[0] == '\0')
262 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
265 version_string += sizeof("PROJECT:") - 1;
267 version_string = "unknown";
269 const char *newline_loc = strchr(version_string, '\n');
271 size_t version_len = sizeof(g_version_string);
273 if (newline_loc && (newline_loc - version_string < version_len))
274 version_len = newline_loc - version_string;
276 ::strncpy(g_version_string, version_string, version_len);
279 return g_version_string;
281 // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
282 static std::string g_version_str;
283 if (g_version_str.empty())
285 g_version_str += "lldb version ";
286 g_version_str += CLANG_VERSION_STRING;
287 const char * lldb_repo = GetLLDBRepository();
290 g_version_str += " (";
291 g_version_str += lldb_repo;
294 const char *lldb_rev = GetLLDBRevision();
297 g_version_str += " revision ";
298 g_version_str += lldb_rev;
300 std::string clang_rev (clang::getClangRevision());
301 if (clang_rev.length() > 0)
303 g_version_str += " clang revision ";
304 g_version_str += clang_rev;
306 std::string llvm_rev (clang::getLLVMRevision());
307 if (llvm_rev.length() > 0)
309 g_version_str += " llvm revision ";
310 g_version_str += llvm_rev;
314 g_version_str += ")";
316 return g_version_str.c_str();
321 lldb_private::GetVoteAsCString (Vote vote)
325 case eVoteNo: return "no";
326 case eVoteNoOpinion: return "no opinion";
327 case eVoteYes: return "yes";
334 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
338 case eSectionTypeInvalid: return "invalid";
339 case eSectionTypeCode: return "code";
340 case eSectionTypeContainer: return "container";
341 case eSectionTypeData: return "data";
342 case eSectionTypeDataCString: return "data-cstr";
343 case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
344 case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
345 case eSectionTypeData4: return "data-4-byte";
346 case eSectionTypeData8: return "data-8-byte";
347 case eSectionTypeData16: return "data-16-byte";
348 case eSectionTypeDataPointers: return "data-ptrs";
349 case eSectionTypeDebug: return "debug";
350 case eSectionTypeZeroFill: return "zero-fill";
351 case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
352 case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
353 case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
354 case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
355 case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
356 case eSectionTypeDWARFDebugInfo: return "dwarf-info";
357 case eSectionTypeDWARFDebugLine: return "dwarf-line";
358 case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
359 case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
360 case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
361 case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
362 case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
363 case eSectionTypeDWARFDebugStr: return "dwarf-str";
364 case eSectionTypeELFSymbolTable: return "elf-symbol-table";
365 case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
366 case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
367 case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
368 case eSectionTypeDWARFAppleNames: return "apple-names";
369 case eSectionTypeDWARFAppleTypes: return "apple-types";
370 case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
371 case eSectionTypeDWARFAppleObjC: return "apple-objc";
372 case eSectionTypeEHFrame: return "eh-frame";
373 case eSectionTypeOther: return "regular";
380 lldb_private::NameMatches (const char *name,
381 NameMatchType match_type,
384 if (match_type == eNameMatchIgnore)
392 llvm::StringRef name_sref(name);
393 llvm::StringRef match_sref(match);
396 case eNameMatchIgnore: // This case cannot occur: tested before
398 case eNameMatchEquals: return name_sref == match_sref;
399 case eNameMatchContains: return name_sref.find (match_sref) != llvm::StringRef::npos;
400 case eNameMatchStartsWith: return name_sref.startswith (match_sref);
401 case eNameMatchEndsWith: return name_sref.endswith (match_sref);
402 case eNameMatchRegularExpression:
404 RegularExpression regex (match);
405 return regex.Execute (name);