lldb.cpp revision 269024
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/Interpreter/ScriptInterpreterPython.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
25
26#include "llvm/ADT/StringRef.h"
27
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"
45#endif
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"
59#endif
60
61#if defined(__linux__) || defined(__FreeBSD__)
62#include "Plugins/Process/elf-core/ProcessElfCore.h"
63#endif
64
65#if defined (__linux__)
66#include "Plugins/Process/Linux/ProcessLinux.h"
67#endif
68
69#if defined (__FreeBSD__)
70#include "Plugins/Process/POSIX/ProcessPOSIX.h"
71#include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
72#endif
73
74#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
75#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
76#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
77
78using namespace lldb;
79using namespace lldb_private;
80
81void
82lldb_private::Initialize ()
83{
84    // Make sure we inialize only once
85    static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
86    static bool g_inited = false;
87
88    Mutex::Locker locker(g_inited_mutex);
89    if (!g_inited)
90    {
91        g_inited = true;
92        Log::Initialize();
93        Timer::Initialize ();
94        Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
95
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();
113#endif
114
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();
131#endif
132#if defined (__linux__)
133        //----------------------------------------------------------------------
134        // Linux hosted plugins
135        //----------------------------------------------------------------------
136        ProcessLinux::Initialize();
137#endif
138#if defined (__FreeBSD__)
139        ProcessFreeBSD::Initialize();
140#endif
141
142#if defined(__linux__) || defined(__FreeBSD__)
143        ProcessElfCore::Initialize();
144#endif
145        //----------------------------------------------------------------------
146        // Platform agnostic plugins
147        //----------------------------------------------------------------------
148        PlatformRemoteGDBServer::Initialize ();
149        ProcessGDBRemote::Initialize();
150        DynamicLoaderStatic::Initialize();
151
152        // Scan for any system or user LLDB plug-ins
153        PluginManager::Initialize();
154
155        // The process settings need to know about installed plug-ins, so the Settings must be initialized
156        // AFTER PluginManager::Initialize is called.
157
158        Debugger::SettingsInitialize();
159    }
160}
161
162void
163lldb_private::WillTerminate()
164{
165    Host::WillTerminate();
166}
167
168void
169lldb_private::Terminate ()
170{
171    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
172
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();
191#endif
192
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();
206#endif
207
208    Debugger::SettingsTerminate ();
209
210#if defined (__linux__)
211    ProcessLinux::Terminate();
212#endif
213
214#if defined (__FreeBSD__)
215    ProcessFreeBSD::Terminate();
216#endif
217
218#if defined(__linux__) || defined(__FreeBSD__)
219    ProcessElfCore::Terminate();
220#endif
221    ProcessGDBRemote::Terminate();
222    DynamicLoaderStatic::Terminate();
223
224    Log::Terminate();
225}
226
227#if defined (__APPLE__)
228extern "C" const unsigned char liblldb_coreVersionString[];
229#else
230
231#include "clang/Basic/Version.h"
232
233static const char *
234GetLLDBRevision()
235{
236#ifdef LLDB_REVISION
237    return LLDB_REVISION;
238#else
239    return NULL;
240#endif
241}
242
243static const char *
244GetLLDBRepository()
245{
246#ifdef LLDB_REPOSITORY
247    return LLDB_REPOSITORY;
248#else
249    return NULL;
250#endif
251}
252
253#endif
254
255const char *
256lldb_private::GetVersion ()
257{
258#if defined (__APPLE__)
259    static char g_version_string[32];
260    if (g_version_string[0] == '\0')
261    {
262        const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
263
264        if (version_string)
265            version_string += sizeof("PROJECT:") - 1;
266        else
267            version_string = "unknown";
268
269        const char *newline_loc = strchr(version_string, '\n');
270
271        size_t version_len = sizeof(g_version_string);
272
273        if (newline_loc && (newline_loc - version_string < version_len))
274            version_len = newline_loc - version_string;
275
276        ::strncpy(g_version_string, version_string, version_len);
277    }
278
279    return g_version_string;
280#else
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())
284    {
285        g_version_str += "lldb version ";
286        g_version_str += CLANG_VERSION_STRING;
287        const char * lldb_repo = GetLLDBRepository();
288        if (lldb_repo)
289        {
290            g_version_str += " (";
291            g_version_str += lldb_repo;
292        }
293
294        const char *lldb_rev = GetLLDBRevision();
295        if (lldb_rev)
296        {
297            g_version_str += " revision ";
298            g_version_str += lldb_rev;
299        }
300        std::string clang_rev (clang::getClangRevision());
301        if (clang_rev.length() > 0)
302        {
303            g_version_str += " clang revision ";
304            g_version_str += clang_rev;
305        }
306        std::string llvm_rev (clang::getLLVMRevision());
307        if (llvm_rev.length() > 0)
308        {
309            g_version_str += " llvm revision ";
310            g_version_str += llvm_rev;
311        }
312
313        if (lldb_repo)
314            g_version_str += ")";
315    }
316    return g_version_str.c_str();
317#endif
318}
319
320const char *
321lldb_private::GetVoteAsCString (Vote vote)
322{
323    switch (vote)
324    {
325    case eVoteNo:           return "no";
326    case eVoteNoOpinion:    return "no opinion";
327    case eVoteYes:          return "yes";
328    }
329    return "invalid";
330}
331
332
333const char *
334lldb_private::GetSectionTypeAsCString (SectionType sect_type)
335{
336    switch (sect_type)
337    {
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";
374    }
375    return "unknown";
376
377}
378
379bool
380lldb_private::NameMatches (const char *name,
381                           NameMatchType match_type,
382                           const char *match)
383{
384    if (match_type == eNameMatchIgnore)
385        return true;
386
387    if (name == match)
388        return true;
389
390    if (name && match)
391    {
392        llvm::StringRef name_sref(name);
393        llvm::StringRef match_sref(match);
394        switch (match_type)
395        {
396        case eNameMatchIgnore: // This case cannot occur: tested before
397            return true;
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:
403            {
404                RegularExpression regex (match);
405                return regex.Execute (name);
406            }
407            break;
408        }
409    }
410    return false;
411}
412