lldb.cpp revision d815ded12b0c9fea2c8a38cc241d3609b3fe228a
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-private.h"
11#include "lldb/lldb-private-log.h"
12#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Log.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/RegularExpression.h"
17#include "lldb/Core/Timer.h"
18#include "lldb/Host/Host.h"
19#include "lldb/Host/Mutex.h"
20#include "lldb/Target/Target.h"
21#include "lldb/Target/Thread.h"
22
23#include "llvm/ADT/StringRef.h"
24
25#include "Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h"
26#include "Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h"
27#include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
28#include "Plugins/Disassembler/llvm/DisassemblerLLVM.h"
29#include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
30#include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
31#include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.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/ObjectFile/PECOFF/ObjectFilePECOFF.h"
40#include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
41#include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
42#include "Plugins/Platform/Linux/PlatformLinux.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/OperatingSystem/Darwin-Kernel/OperatingSystemDarwinKernel.h"
50#include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
51#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
52#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
53#include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
54#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
55#include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
56#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
57#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
58#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
59#include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
60#endif
61
62#include "Plugins/Process/mach-core/ProcessMachCore.h"
63
64#if defined (__linux__)
65#include "Plugins/Process/Linux/ProcessLinux.h"
66#endif
67
68#if defined (__FreeBSD__)
69#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
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/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        ABIMacOSX_i386::Initialize();
96        ABIMacOSX_arm::Initialize();
97        ABISysV_x86_64::Initialize();
98        DisassemblerLLVMC::Initialize();
99        DisassemblerLLVM::Initialize();
100        ObjectContainerBSDArchive::Initialize();
101        ObjectFileELF::Initialize();
102        SymbolFileDWARF::Initialize();
103        SymbolFileSymtab::Initialize();
104        UnwindAssemblyInstEmulation::Initialize();
105        UnwindAssembly_x86::Initialize();
106        EmulateInstructionARM::Initialize ();
107        ObjectFilePECOFF::Initialize ();
108        DynamicLoaderPOSIXDYLD::Initialize ();
109        PlatformFreeBSD::Initialize();
110        PlatformLinux::Initialize();
111#ifndef LLDB_DISABLE_PYTHON
112        OperatingSystemPython::Initialize();
113#endif
114
115#if defined (__APPLE__)
116        //----------------------------------------------------------------------
117        // Apple/Darwin hosted plugins
118        //----------------------------------------------------------------------
119        DynamicLoaderMacOSXDYLD::Initialize();
120        DynamicLoaderDarwinKernel::Initialize();
121        OperatingSystemDarwinKernel::Initialize();
122        SymbolFileDWARFDebugMap::Initialize();
123        ItaniumABILanguageRuntime::Initialize();
124        AppleObjCRuntimeV2::Initialize();
125        AppleObjCRuntimeV1::Initialize();
126        ObjectContainerUniversalMachO::Initialize();
127        ObjectFileMachO::Initialize();
128        ProcessGDBRemote::Initialize();
129        ProcessKDP::Initialize();
130        ProcessMachCore::Initialize();
131        SymbolVendorMacOSX::Initialize();
132        PlatformRemoteiOS::Initialize();
133        PlatformMacOSX::Initialize();
134        PlatformiOSSimulator::Initialize();
135#endif
136#if defined (__linux__)
137        //----------------------------------------------------------------------
138        // Linux hosted plugins
139        //----------------------------------------------------------------------
140        ProcessLinux::Initialize();
141#endif
142#if defined (__FreeBSD__)
143        ProcessFreeBSD::Initialize();
144        ProcessGDBRemote::Initialize();
145#endif
146        //----------------------------------------------------------------------
147        // Platform agnostic plugins
148        //----------------------------------------------------------------------
149        PlatformRemoteGDBServer::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
176    ABIMacOSX_i386::Terminate();
177    ABIMacOSX_arm::Terminate();
178    ABISysV_x86_64::Terminate();
179    DisassemblerLLVMC::Terminate();
180    DisassemblerLLVM::Terminate();
181    ObjectContainerBSDArchive::Terminate();
182    ObjectFileELF::Terminate();
183    SymbolFileDWARF::Terminate();
184    SymbolFileSymtab::Terminate();
185    UnwindAssembly_x86::Terminate();
186    UnwindAssemblyInstEmulation::Terminate();
187    EmulateInstructionARM::Terminate ();
188    ObjectFilePECOFF::Terminate ();
189    DynamicLoaderPOSIXDYLD::Terminate ();
190    PlatformFreeBSD::Terminate();
191    PlatformLinux::Terminate();
192#ifndef LLDB_DISABLE_PYTHON
193    OperatingSystemPython::Terminate();
194#endif
195
196#if defined (__APPLE__)
197    DynamicLoaderMacOSXDYLD::Terminate();
198    DynamicLoaderDarwinKernel::Terminate();
199    OperatingSystemDarwinKernel::Terminate();
200    SymbolFileDWARFDebugMap::Terminate();
201    ItaniumABILanguageRuntime::Terminate();
202    AppleObjCRuntimeV2::Terminate();
203    AppleObjCRuntimeV1::Terminate();
204    ObjectContainerUniversalMachO::Terminate();
205    ObjectFileMachO::Terminate();
206    ProcessMachCore::Terminate();
207    ProcessGDBRemote::Terminate();
208    ProcessKDP::Terminate();
209    SymbolVendorMacOSX::Terminate();
210    PlatformMacOSX::Terminate();
211    PlatformRemoteiOS::Terminate();
212    PlatformiOSSimulator::Terminate();
213#endif
214
215    Debugger::SettingsTerminate ();
216
217#if defined (__linux__)
218    ProcessLinux::Terminate();
219#endif
220
221#if defined (__FreeBSD__)
222    ProcessFreeBSD::Terminate();
223    ProcessGDBRemote::Terminate();
224#endif
225
226    DynamicLoaderStatic::Terminate();
227
228    Log::Terminate();
229}
230
231extern "C" const double liblldb_coreVersionNumber;
232const char *
233lldb_private::GetVersion ()
234{
235    static char g_version_string[32];
236    if (g_version_string[0] == '\0')
237        ::snprintf (g_version_string, sizeof(g_version_string), "LLDB-%g", liblldb_coreVersionNumber);
238
239    return g_version_string;
240}
241
242const char *
243lldb_private::GetVoteAsCString (Vote vote)
244{
245    switch (vote)
246    {
247    case eVoteNo:           return "no";
248    case eVoteNoOpinion:    return "no opinion";
249    case eVoteYes:          return "yes";
250    default:
251        break;
252    }
253    return "invalid";
254}
255
256
257const char *
258lldb_private::GetSectionTypeAsCString (SectionType sect_type)
259{
260    switch (sect_type)
261    {
262    case eSectionTypeInvalid: return "invalid";
263    case eSectionTypeCode: return "code";
264    case eSectionTypeContainer: return "container";
265    case eSectionTypeData: return "data";
266    case eSectionTypeDataCString: return "data-cstr";
267    case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
268    case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
269    case eSectionTypeData4: return "data-4-byte";
270    case eSectionTypeData8: return "data-8-byte";
271    case eSectionTypeData16: return "data-16-byte";
272    case eSectionTypeDataPointers: return "data-ptrs";
273    case eSectionTypeDebug: return "debug";
274    case eSectionTypeZeroFill: return "zero-fill";
275    case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
276    case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
277    case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
278    case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
279    case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
280    case eSectionTypeDWARFDebugInfo: return "dwarf-info";
281    case eSectionTypeDWARFDebugLine: return "dwarf-line";
282    case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
283    case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
284    case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
285    case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
286    case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
287    case eSectionTypeDWARFDebugStr: return "dwarf-str";
288    case eSectionTypeDWARFAppleNames: return "apple-names";
289    case eSectionTypeDWARFAppleTypes: return "apple-types";
290    case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
291    case eSectionTypeDWARFAppleObjC: return "apple-objc";
292    case eSectionTypeEHFrame: return "eh-frame";
293    case eSectionTypeOther: return "regular";
294    }
295    return "unknown";
296
297}
298
299bool
300lldb_private::NameMatches (const char *name,
301                           NameMatchType match_type,
302                           const char *match)
303{
304    if (match_type == eNameMatchIgnore)
305        return true;
306
307    if (name == match)
308        return true;
309
310    if (name && match)
311    {
312        llvm::StringRef name_sref(name);
313        llvm::StringRef match_sref(match);
314        switch (match_type)
315        {
316        case eNameMatchIgnore:
317            return true;
318        case eNameMatchEquals:      return name_sref == match_sref;
319        case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
320        case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
321        case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
322        case eNameMatchRegularExpression:
323            {
324                RegularExpression regex (match);
325                return regex.Execute (name);
326            }
327            break;
328        default:
329            assert (!"unhandled NameMatchType in lldb_private::NameMatches()");
330            break;
331        }
332    }
333    return false;
334}
335