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/Target/Target.h"
23 #include "lldb/Target/Thread.h"
25 #include "llvm/ADT/StringRef.h"
27 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
28 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
29 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
30 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
31 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
32 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
33 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
34 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
35 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
36 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
37 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
38 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
39 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
40 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
41 #ifndef LLDB_DISABLE_PYTHON
42 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
44 #if defined (__APPLE__)
45 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
46 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
47 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
48 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
49 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
50 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
51 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
52 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
53 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
54 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
55 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
58 #if defined(__linux__) or defined(__FreeBSD__)
59 #include "Plugins/Process/elf-core/ProcessElfCore.h"
62 #if defined (__linux__)
63 #include "Plugins/Process/Linux/ProcessLinux.h"
66 #if defined (__FreeBSD__)
67 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
68 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
71 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
72 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
73 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
76 using namespace lldb_private;
79 lldb_private::Initialize ()
81 // Make sure we inialize only once
82 static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
83 static bool g_inited = false;
85 Mutex::Locker locker(g_inited_mutex);
91 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
93 ABISysV_x86_64::Initialize();
94 DisassemblerLLVMC::Initialize();
95 ObjectContainerBSDArchive::Initialize();
96 ObjectFileELF::Initialize();
97 SymbolVendorELF::Initialize();
98 SymbolFileDWARF::Initialize();
99 SymbolFileSymtab::Initialize();
100 UnwindAssemblyInstEmulation::Initialize();
101 UnwindAssembly_x86::Initialize();
102 EmulateInstructionARM::Initialize ();
103 DynamicLoaderPOSIXDYLD::Initialize ();
104 PlatformFreeBSD::Initialize();
105 SymbolFileDWARFDebugMap::Initialize();
106 ItaniumABILanguageRuntime::Initialize();
107 #ifndef LLDB_DISABLE_PYTHON
108 OperatingSystemPython::Initialize();
111 #if defined (__APPLE__)
112 //----------------------------------------------------------------------
113 // Apple/Darwin hosted plugins
114 //----------------------------------------------------------------------
115 DynamicLoaderMacOSXDYLD::Initialize();
116 DynamicLoaderDarwinKernel::Initialize();
117 AppleObjCRuntimeV2::Initialize();
118 AppleObjCRuntimeV1::Initialize();
119 ObjectContainerUniversalMachO::Initialize();
120 ObjectFileMachO::Initialize();
121 ProcessKDP::Initialize();
122 PlatformDarwinKernel::Initialize();
123 PlatformRemoteiOS::Initialize();
124 PlatformMacOSX::Initialize();
125 PlatformiOSSimulator::Initialize();
127 #if defined (__linux__)
128 //----------------------------------------------------------------------
129 // Linux hosted plugins
130 //----------------------------------------------------------------------
131 ProcessLinux::Initialize();
133 #if defined (__FreeBSD__)
134 ProcessFreeBSD::Initialize();
137 #if defined(__linux__) or defined(__FreeBSD__)
138 ProcessElfCore::Initialize();
140 //----------------------------------------------------------------------
141 // Platform agnostic plugins
142 //----------------------------------------------------------------------
143 PlatformRemoteGDBServer::Initialize ();
144 ProcessGDBRemote::Initialize();
145 DynamicLoaderStatic::Initialize();
147 // Scan for any system or user LLDB plug-ins
148 PluginManager::Initialize();
150 // The process settings need to know about installed plug-ins, so the Settings must be initialized
151 // AFTER PluginManager::Initialize is called.
153 Debugger::SettingsInitialize();
158 lldb_private::WillTerminate()
160 Host::WillTerminate();
164 lldb_private::Terminate ()
166 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
168 // Terminate and unload and loaded system or user LLDB plug-ins
169 PluginManager::Terminate();
171 ABISysV_x86_64::Terminate();
172 DisassemblerLLVMC::Terminate();
173 ObjectContainerBSDArchive::Terminate();
174 ObjectFileELF::Terminate();
175 SymbolVendorELF::Terminate();
176 SymbolFileDWARF::Terminate();
177 SymbolFileSymtab::Terminate();
178 UnwindAssembly_x86::Terminate();
179 UnwindAssemblyInstEmulation::Terminate();
180 EmulateInstructionARM::Terminate ();
181 DynamicLoaderPOSIXDYLD::Terminate ();
182 PlatformFreeBSD::Terminate();
183 SymbolFileDWARFDebugMap::Terminate();
184 ItaniumABILanguageRuntime::Terminate();
185 #ifndef LLDB_DISABLE_PYTHON
186 OperatingSystemPython::Terminate();
189 #if defined (__APPLE__)
190 DynamicLoaderMacOSXDYLD::Terminate();
191 DynamicLoaderDarwinKernel::Terminate();
192 AppleObjCRuntimeV2::Terminate();
193 AppleObjCRuntimeV1::Terminate();
194 ObjectContainerUniversalMachO::Terminate();
195 ObjectFileMachO::Terminate();
196 ProcessKDP::Terminate();
197 PlatformMacOSX::Terminate();
198 PlatformDarwinKernel::Terminate();
199 PlatformRemoteiOS::Terminate();
200 PlatformiOSSimulator::Terminate();
203 Debugger::SettingsTerminate ();
205 #if defined (__linux__)
206 ProcessLinux::Terminate();
209 #if defined (__FreeBSD__)
210 ProcessFreeBSD::Terminate();
213 #if defined(__linux__) or defined(__FreeBSD__)
214 ProcessElfCore::Terminate();
216 ProcessGDBRemote::Terminate();
217 DynamicLoaderStatic::Terminate();
222 #if defined (__APPLE__)
223 extern "C" const unsigned char liblldb_coreVersionString[];
226 #include "clang/Basic/Version.h"
232 return LLDB_REVISION;
241 #ifdef LLDB_REPOSITORY
242 return LLDB_REPOSITORY;
251 lldb_private::GetVersion ()
253 #if defined (__APPLE__)
254 static char g_version_string[32];
255 if (g_version_string[0] == '\0')
257 const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
260 version_string += sizeof("PROJECT:") - 1;
262 version_string = "unknown";
264 const char *newline_loc = strchr(version_string, '\n');
266 size_t version_len = sizeof(g_version_string);
268 if (newline_loc && (newline_loc - version_string < version_len))
269 version_len = newline_loc - version_string;
271 ::strncpy(g_version_string, version_string, version_len);
274 return g_version_string;
276 // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
277 static std::string g_version_str;
278 if (g_version_str.empty())
280 g_version_str += "lldb version ";
281 g_version_str += CLANG_VERSION_STRING;
282 const char * lldb_repo = GetLLDBRepository();
285 g_version_str += " (";
286 g_version_str += lldb_repo;
289 const char *lldb_rev = GetLLDBRevision();
292 g_version_str += " revision ";
293 g_version_str += lldb_rev;
295 std::string clang_rev (clang::getClangRevision());
296 if (clang_rev.length() > 0)
298 g_version_str += " clang revision ";
299 g_version_str += clang_rev;
301 std::string llvm_rev (clang::getLLVMRevision());
302 if (llvm_rev.length() > 0)
304 g_version_str += " llvm revision ";
305 g_version_str += llvm_rev;
309 g_version_str += ")";
311 return g_version_str.c_str();
316 lldb_private::GetVoteAsCString (Vote vote)
320 case eVoteNo: return "no";
321 case eVoteNoOpinion: return "no opinion";
322 case eVoteYes: return "yes";
329 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
333 case eSectionTypeInvalid: return "invalid";
334 case eSectionTypeCode: return "code";
335 case eSectionTypeContainer: return "container";
336 case eSectionTypeData: return "data";
337 case eSectionTypeDataCString: return "data-cstr";
338 case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
339 case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
340 case eSectionTypeData4: return "data-4-byte";
341 case eSectionTypeData8: return "data-8-byte";
342 case eSectionTypeData16: return "data-16-byte";
343 case eSectionTypeDataPointers: return "data-ptrs";
344 case eSectionTypeDebug: return "debug";
345 case eSectionTypeZeroFill: return "zero-fill";
346 case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
347 case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
348 case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
349 case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
350 case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
351 case eSectionTypeDWARFDebugInfo: return "dwarf-info";
352 case eSectionTypeDWARFDebugLine: return "dwarf-line";
353 case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
354 case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
355 case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
356 case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
357 case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
358 case eSectionTypeDWARFDebugStr: return "dwarf-str";
359 case eSectionTypeELFSymbolTable: return "elf-symbol-table";
360 case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
361 case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
362 case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
363 case eSectionTypeDWARFAppleNames: return "apple-names";
364 case eSectionTypeDWARFAppleTypes: return "apple-types";
365 case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
366 case eSectionTypeDWARFAppleObjC: return "apple-objc";
367 case eSectionTypeEHFrame: return "eh-frame";
368 case eSectionTypeOther: return "regular";
375 lldb_private::NameMatches (const char *name,
376 NameMatchType match_type,
379 if (match_type == eNameMatchIgnore)
387 llvm::StringRef name_sref(name);
388 llvm::StringRef match_sref(match);
391 case eNameMatchIgnore:
393 case eNameMatchEquals: return name_sref == match_sref;
394 case eNameMatchContains: return name_sref.find (match_sref) != llvm::StringRef::npos;
395 case eNameMatchStartsWith: return name_sref.startswith (match_sref);
396 case eNameMatchEndsWith: return name_sref.endswith (match_sref);
397 case eNameMatchRegularExpression:
399 RegularExpression regex (match);
400 return regex.Execute (name);