lldb.cpp revision 888a7334344778d1a4edbd58b5852ae4d53ffed9
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/Disassembler/llvm/DisassemblerLLVM.h"
26#include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
27#include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
28#include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
29#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
30#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
31#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
32#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
33#include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
34#include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
35#include "Plugins/ArchDefaultUnwindPlan/x86/ArchDefaultUnwindPlan-x86.h"
36#include "Plugins/ArchVolatileRegs/x86/ArchVolatileRegs-x86.h"
37
38#if defined (__APPLE__)
39#include "Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h"
40#include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
41#include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
42#include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
43#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
44#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
45#include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
46#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
47#include "Plugins/Process/MacOSX-User/source/ProcessMacOSX.h"
48#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
49#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
50#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
51#endif
52
53#if defined (__linux__)
54#include "Plugins/DynamicLoader/Linux-DYLD/DynamicLoaderLinuxDYLD.h"
55#include "Plugins/Platform/Linux/PlatformLinux.h"
56#include "Plugins/Process/Linux/ProcessLinux.h"
57#endif
58
59#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
60#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
61
62using namespace lldb;
63using namespace lldb_private;
64
65
66void
67lldb_private::Initialize ()
68{
69    // Make sure we inialize only once
70    static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
71    static bool g_inited = false;
72
73    Mutex::Locker locker(g_inited_mutex);
74    if (!g_inited)
75    {
76        g_inited = true;
77        Log::Initialize();
78        Timer::Initialize ();
79        Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
80
81        DisassemblerLLVM::Initialize();
82        ObjectContainerBSDArchive::Initialize();
83        ObjectFileELF::Initialize();
84        SymbolFileDWARF::Initialize();
85        SymbolFileSymtab::Initialize();
86        UnwindAssemblyInstEmulation::Initialize();
87        UnwindAssembly_x86::Initialize();
88        ArchDefaultUnwindPlan_x86_64::Initialize();
89        ArchDefaultUnwindPlan_i386::Initialize();
90        ArchVolatileRegs_x86::Initialize();
91        EmulateInstructionARM::Initialize ();
92
93#if defined (__APPLE__)
94        //----------------------------------------------------------------------
95        // Apple/Darwin hosted plugins
96        //----------------------------------------------------------------------
97        ABIMacOSX_i386::Initialize();
98        ABISysV_x86_64::Initialize();
99        DynamicLoaderMacOSXDYLD::Initialize();
100        SymbolFileDWARFDebugMap::Initialize();
101        ItaniumABILanguageRuntime::Initialize();
102        AppleObjCRuntimeV2::Initialize();
103        AppleObjCRuntimeV1::Initialize();
104        ObjectContainerUniversalMachO::Initialize();
105        ObjectFileMachO::Initialize();
106        ProcessGDBRemote::Initialize();
107        //ProcessMacOSX::Initialize();
108        SymbolVendorMacOSX::Initialize();
109        PlatformMacOSX::Initialize();
110        PlatformRemoteiOS::Initialize();
111#endif
112#if defined (__linux__)
113        //----------------------------------------------------------------------
114        // Linux hosted plugins
115        //----------------------------------------------------------------------
116        PlatformLinux::Initialize();
117        ProcessLinux::Initialize();
118        DynamicLoaderLinuxDYLD::Initialize();
119#endif
120        //----------------------------------------------------------------------
121        // Platform agnostic plugins
122        //----------------------------------------------------------------------
123        PlatformRemoteGDBServer::Initialize ();
124        DynamicLoaderStatic::Initialize();
125
126        // Scan for any system or user LLDB plug-ins
127        PluginManager::Initialize();
128
129        // The process settings need to know about installed plug-ins, so the Settings must be initialized
130        // AFTER PluginManager::Initialize is called.
131
132        Debugger::SettingsInitialize();
133    }
134}
135
136void
137lldb_private::WillTerminate()
138{
139    Host::WillTerminate();
140}
141
142void
143lldb_private::Terminate ()
144{
145    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
146
147    // Terminate and unload and loaded system or user LLDB plug-ins
148    PluginManager::Terminate();
149
150    DisassemblerLLVM::Terminate();
151    ObjectContainerBSDArchive::Terminate();
152    ObjectFileELF::Terminate();
153    SymbolFileDWARF::Terminate();
154    SymbolFileSymtab::Terminate();
155    UnwindAssembly_x86::Terminate();
156    UnwindAssemblyInstEmulation::Terminate();
157    ArchDefaultUnwindPlan_i386::Terminate();
158    ArchDefaultUnwindPlan_x86_64::Terminate();
159    ArchVolatileRegs_x86::Terminate();
160    EmulateInstructionARM::Terminate ();
161
162#if defined (__APPLE__)
163    DynamicLoaderMacOSXDYLD::Terminate();
164    SymbolFileDWARFDebugMap::Terminate();
165    ItaniumABILanguageRuntime::Terminate();
166    AppleObjCRuntimeV2::Terminate();
167    AppleObjCRuntimeV1::Terminate();
168    ObjectContainerUniversalMachO::Terminate();
169    ObjectFileMachO::Terminate();
170    ProcessGDBRemote::Terminate();
171    //ProcessMacOSX::Terminate();
172    SymbolVendorMacOSX::Terminate();
173    PlatformMacOSX::Terminate();
174    PlatformRemoteiOS::Terminate();
175#endif
176
177    Debugger::SettingsTerminate ();
178
179#if defined (__linux__)
180    PlatformLinux::Terminate();
181    ProcessLinux::Terminate();
182    DynamicLoaderLinuxDYLD::Terminate();
183#endif
184
185    DynamicLoaderStatic::Terminate();
186
187    Log::Terminate();
188}
189
190extern "C" const double liblldb_coreVersionNumber;
191const char *
192lldb_private::GetVersion ()
193{
194    static char g_version_string[32];
195    if (g_version_string[0] == '\0')
196        ::snprintf (g_version_string, sizeof(g_version_string), "LLDB-%g", liblldb_coreVersionNumber);
197
198    return g_version_string;
199}
200
201const char *
202lldb_private::GetVoteAsCString (Vote vote)
203{
204    switch (vote)
205    {
206    case eVoteNo:           return "no";
207    case eVoteNoOpinion:    return "no opinion";
208    case eVoteYes:          return "yes";
209    default:
210        break;
211    }
212    return "invalid";
213}
214
215
216const char *
217lldb_private::GetSectionTypeAsCString (SectionType sect_type)
218{
219    switch (sect_type)
220    {
221    case eSectionTypeInvalid: return "invalid";
222    case eSectionTypeCode: return "code";
223    case eSectionTypeContainer: return "container";
224    case eSectionTypeData: return "data";
225    case eSectionTypeDataCString: return "data-cstr";
226    case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
227    case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
228    case eSectionTypeData4: return "data-4-byte";
229    case eSectionTypeData8: return "data-8-byte";
230    case eSectionTypeData16: return "data-16-byte";
231    case eSectionTypeDataPointers: return "data-ptrs";
232    case eSectionTypeDebug: return "debug";
233    case eSectionTypeZeroFill: return "zero-fill";
234    case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
235    case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
236    case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
237    case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
238    case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
239    case eSectionTypeDWARFDebugInfo: return "dwarf-info";
240    case eSectionTypeDWARFDebugLine: return "dwarf-line";
241    case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
242    case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
243    case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
244    case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
245    case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
246    case eSectionTypeDWARFDebugStr: return "dwarf-str";
247    case eSectionTypeEHFrame: return "eh-frame";
248    case eSectionTypeOther: return "regular";
249    }
250    return "unknown";
251
252}
253
254bool
255lldb_private::NameMatches (const char *name,
256                           NameMatchType match_type,
257                           const char *match)
258{
259    if (match_type == eNameMatchIgnore)
260        return true;
261
262    if (name == match)
263        return true;
264
265    if (name && match)
266    {
267        llvm::StringRef name_sref(name);
268        llvm::StringRef match_sref(match);
269        switch (match_type)
270        {
271        case eNameMatchIgnore:
272            return true;
273        case eNameMatchEquals:      return name_sref == match_sref;
274        case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
275        case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
276        case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
277        case eNameMatchRegularExpression:
278            {
279                RegularExpression regex (match);
280                return regex.Execute (name);
281            }
282            break;
283        default:
284            assert (!"unhandled NameMatchType in lldb_private::NameMatches()");
285            break;
286        }
287    }
288    return false;
289}
290