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