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