lldb.cpp revision 263367
1//===-- lldb.cpp ------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-python.h"
11
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"
24
25#include "llvm/ADT/StringRef.h"
26
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/Platform/POSIX/PlatformPOSIX.h"
41#include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
42#ifndef LLDB_DISABLE_PYTHON
43#include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
44#endif
45#if defined (__APPLE__)
46#include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
47#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
48#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
49#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
50#include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
51#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
52#include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
53#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
54#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
55#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
56#include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
57#include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
58#endif
59
60#if defined(__linux__) || defined(__FreeBSD__)
61#include "Plugins/Process/elf-core/ProcessElfCore.h"
62#endif
63
64#if defined (__linux__)
65#include "Plugins/Process/Linux/ProcessLinux.h"
66#endif
67
68#if defined (__FreeBSD__)
69#include "Plugins/Process/POSIX/ProcessPOSIX.h"
70#include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
71#endif
72
73#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
74#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
75#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
76
77using namespace lldb;
78using namespace lldb_private;
79
80void
81lldb_private::Initialize ()
82{
83    // Make sure we inialize only once
84    static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
85    static bool g_inited = false;
86
87    Mutex::Locker locker(g_inited_mutex);
88    if (!g_inited)
89    {
90        g_inited = true;
91        Log::Initialize();
92        Timer::Initialize ();
93        Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
94
95        ABISysV_x86_64::Initialize();
96        DisassemblerLLVMC::Initialize();
97        ObjectContainerBSDArchive::Initialize();
98        ObjectFileELF::Initialize();
99        SymbolVendorELF::Initialize();
100        SymbolFileDWARF::Initialize();
101        SymbolFileSymtab::Initialize();
102        UnwindAssemblyInstEmulation::Initialize();
103        UnwindAssembly_x86::Initialize();
104        EmulateInstructionARM::Initialize ();
105        DynamicLoaderPOSIXDYLD::Initialize ();
106        PlatformFreeBSD::Initialize();
107        SymbolFileDWARFDebugMap::Initialize();
108        ItaniumABILanguageRuntime::Initialize();
109#ifndef LLDB_DISABLE_PYTHON
110        OperatingSystemPython::Initialize();
111#endif
112
113#if defined (__APPLE__)
114        //----------------------------------------------------------------------
115        // Apple/Darwin hosted plugins
116        //----------------------------------------------------------------------
117        DynamicLoaderMacOSXDYLD::Initialize();
118        DynamicLoaderDarwinKernel::Initialize();
119        AppleObjCRuntimeV2::Initialize();
120        AppleObjCRuntimeV1::Initialize();
121        ObjectContainerUniversalMachO::Initialize();
122        ObjectFileMachO::Initialize();
123        ProcessKDP::Initialize();
124        PlatformDarwinKernel::Initialize();
125        PlatformRemoteiOS::Initialize();
126        PlatformMacOSX::Initialize();
127        PlatformiOSSimulator::Initialize();
128        SystemRuntimeMacOSX::Initialize();
129#endif
130#if defined (__linux__)
131        //----------------------------------------------------------------------
132        // Linux hosted plugins
133        //----------------------------------------------------------------------
134        ProcessLinux::Initialize();
135#endif
136#if defined (__FreeBSD__)
137        ProcessFreeBSD::Initialize();
138#endif
139
140#if defined(__linux__) || defined(__FreeBSD__)
141        ProcessElfCore::Initialize();
142#endif
143        //----------------------------------------------------------------------
144        // Platform agnostic plugins
145        //----------------------------------------------------------------------
146        PlatformRemoteGDBServer::Initialize ();
147        ProcessGDBRemote::Initialize();
148        DynamicLoaderStatic::Initialize();
149
150        // Scan for any system or user LLDB plug-ins
151        PluginManager::Initialize();
152
153        // The process settings need to know about installed plug-ins, so the Settings must be initialized
154        // AFTER PluginManager::Initialize is called.
155
156        Debugger::SettingsInitialize();
157    }
158}
159
160void
161lldb_private::WillTerminate()
162{
163    Host::WillTerminate();
164}
165
166void
167lldb_private::Terminate ()
168{
169    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
170
171    // Terminate and unload and loaded system or user LLDB plug-ins
172    PluginManager::Terminate();
173    ABISysV_x86_64::Terminate();
174    DisassemblerLLVMC::Terminate();
175    ObjectContainerBSDArchive::Terminate();
176    ObjectFileELF::Terminate();
177    SymbolVendorELF::Terminate();
178    SymbolFileDWARF::Terminate();
179    SymbolFileSymtab::Terminate();
180    UnwindAssembly_x86::Terminate();
181    UnwindAssemblyInstEmulation::Terminate();
182    EmulateInstructionARM::Terminate ();
183    DynamicLoaderPOSIXDYLD::Terminate ();
184    PlatformFreeBSD::Terminate();
185    SymbolFileDWARFDebugMap::Terminate();
186    ItaniumABILanguageRuntime::Terminate();
187#ifndef LLDB_DISABLE_PYTHON
188    OperatingSystemPython::Terminate();
189#endif
190
191#if defined (__APPLE__)
192    DynamicLoaderMacOSXDYLD::Terminate();
193    DynamicLoaderDarwinKernel::Terminate();
194    AppleObjCRuntimeV2::Terminate();
195    AppleObjCRuntimeV1::Terminate();
196    ObjectContainerUniversalMachO::Terminate();
197    ObjectFileMachO::Terminate();
198    ProcessKDP::Terminate();
199    PlatformMacOSX::Terminate();
200    PlatformDarwinKernel::Terminate();
201    PlatformRemoteiOS::Terminate();
202    PlatformiOSSimulator::Terminate();
203    SystemRuntimeMacOSX::Terminate();
204#endif
205
206    Debugger::SettingsTerminate ();
207
208#if defined (__linux__)
209    ProcessLinux::Terminate();
210#endif
211
212#if defined (__FreeBSD__)
213    ProcessFreeBSD::Terminate();
214#endif
215
216#if defined(__linux__) || defined(__FreeBSD__)
217    ProcessElfCore::Terminate();
218#endif
219    ProcessGDBRemote::Terminate();
220    DynamicLoaderStatic::Terminate();
221
222    Log::Terminate();
223}
224
225#if defined (__APPLE__)
226extern "C" const unsigned char liblldb_coreVersionString[];
227#else
228
229#include "clang/Basic/Version.h"
230
231static const char *
232GetLLDBRevision()
233{
234#ifdef LLDB_REVISION
235    return LLDB_REVISION;
236#else
237    return NULL;
238#endif
239}
240
241static const char *
242GetLLDBRepository()
243{
244#ifdef LLDB_REPOSITORY
245    return LLDB_REPOSITORY;
246#else
247    return NULL;
248#endif
249}
250
251#endif
252
253const char *
254lldb_private::GetVersion ()
255{
256#if defined (__APPLE__)
257    static char g_version_string[32];
258    if (g_version_string[0] == '\0')
259    {
260        const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
261
262        if (version_string)
263            version_string += sizeof("PROJECT:") - 1;
264        else
265            version_string = "unknown";
266
267        const char *newline_loc = strchr(version_string, '\n');
268
269        size_t version_len = sizeof(g_version_string);
270
271        if (newline_loc && (newline_loc - version_string < version_len))
272            version_len = newline_loc - version_string;
273
274        ::strncpy(g_version_string, version_string, version_len);
275    }
276
277    return g_version_string;
278#else
279    // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
280    static std::string g_version_str;
281    if (g_version_str.empty())
282    {
283        g_version_str += "lldb version ";
284        g_version_str += CLANG_VERSION_STRING;
285        const char * lldb_repo = GetLLDBRepository();
286        if (lldb_repo)
287        {
288            g_version_str += " (";
289            g_version_str += lldb_repo;
290        }
291
292        const char *lldb_rev = GetLLDBRevision();
293        if (lldb_rev)
294        {
295            g_version_str += " revision ";
296            g_version_str += lldb_rev;
297        }
298        std::string clang_rev (clang::getClangRevision());
299        if (clang_rev.length() > 0)
300        {
301            g_version_str += " clang revision ";
302            g_version_str += clang_rev;
303        }
304        std::string llvm_rev (clang::getLLVMRevision());
305        if (llvm_rev.length() > 0)
306        {
307            g_version_str += " llvm revision ";
308            g_version_str += llvm_rev;
309        }
310
311        if (lldb_repo)
312            g_version_str += ")";
313    }
314    return g_version_str.c_str();
315#endif
316}
317
318const char *
319lldb_private::GetVoteAsCString (Vote vote)
320{
321    switch (vote)
322    {
323    case eVoteNo:           return "no";
324    case eVoteNoOpinion:    return "no opinion";
325    case eVoteYes:          return "yes";
326    }
327    return "invalid";
328}
329
330
331const char *
332lldb_private::GetSectionTypeAsCString (SectionType sect_type)
333{
334    switch (sect_type)
335    {
336    case eSectionTypeInvalid: return "invalid";
337    case eSectionTypeCode: return "code";
338    case eSectionTypeContainer: return "container";
339    case eSectionTypeData: return "data";
340    case eSectionTypeDataCString: return "data-cstr";
341    case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
342    case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
343    case eSectionTypeData4: return "data-4-byte";
344    case eSectionTypeData8: return "data-8-byte";
345    case eSectionTypeData16: return "data-16-byte";
346    case eSectionTypeDataPointers: return "data-ptrs";
347    case eSectionTypeDebug: return "debug";
348    case eSectionTypeZeroFill: return "zero-fill";
349    case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
350    case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
351    case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
352    case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
353    case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
354    case eSectionTypeDWARFDebugInfo: return "dwarf-info";
355    case eSectionTypeDWARFDebugLine: return "dwarf-line";
356    case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
357    case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
358    case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
359    case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
360    case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
361    case eSectionTypeDWARFDebugStr: return "dwarf-str";
362    case eSectionTypeELFSymbolTable: return "elf-symbol-table";
363    case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
364    case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
365    case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
366    case eSectionTypeDWARFAppleNames: return "apple-names";
367    case eSectionTypeDWARFAppleTypes: return "apple-types";
368    case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
369    case eSectionTypeDWARFAppleObjC: return "apple-objc";
370    case eSectionTypeEHFrame: return "eh-frame";
371    case eSectionTypeOther: return "regular";
372    }
373    return "unknown";
374
375}
376
377bool
378lldb_private::NameMatches (const char *name,
379                           NameMatchType match_type,
380                           const char *match)
381{
382    if (match_type == eNameMatchIgnore)
383        return true;
384
385    if (name == match)
386        return true;
387
388    if (name && match)
389    {
390        llvm::StringRef name_sref(name);
391        llvm::StringRef match_sref(match);
392        switch (match_type)
393        {
394        case eNameMatchIgnore: // This case cannot occur: tested before
395            return true;
396        case eNameMatchEquals:      return name_sref == match_sref;
397        case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
398        case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
399        case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
400        case eNameMatchRegularExpression:
401            {
402                RegularExpression regex (match);
403                return regex.Execute (name);
404            }
405            break;
406        }
407    }
408    return false;
409}
410