]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/llvm/tools/lldb/source/lldb.cpp
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / llvm / tools / lldb / source / lldb.cpp
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/Target/Target.h"
23 #include "lldb/Target/Thread.h"
24
25 #include "llvm/ADT/StringRef.h"
26
27 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
28 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
29 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
30 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.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/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
39 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
40 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
41 #ifndef LLDB_DISABLE_PYTHON
42 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
43 #endif
44 #if defined (__APPLE__)
45 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
46 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
47 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
48 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
49 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
50 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
51 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
52 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
53 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
54 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
55 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
56 #endif
57
58 #if defined(__linux__) or defined(__FreeBSD__)
59 #include "Plugins/Process/elf-core/ProcessElfCore.h"
60 #endif
61
62 #if defined (__linux__)
63 #include "Plugins/Process/Linux/ProcessLinux.h"
64 #endif
65
66 #if defined (__FreeBSD__)
67 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
68 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
69 #endif
70
71 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
72 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
73 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
74
75 using namespace lldb;
76 using namespace lldb_private;
77
78 void
79 lldb_private::Initialize ()
80 {
81     // Make sure we inialize only once
82     static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
83     static bool g_inited = false;
84
85     Mutex::Locker locker(g_inited_mutex);
86     if (!g_inited)
87     {
88         g_inited = true;
89         Log::Initialize();
90         Timer::Initialize ();
91         Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
92         
93         ABISysV_x86_64::Initialize();
94         DisassemblerLLVMC::Initialize();
95         ObjectContainerBSDArchive::Initialize();
96         ObjectFileELF::Initialize();
97         SymbolVendorELF::Initialize();
98         SymbolFileDWARF::Initialize();
99         SymbolFileSymtab::Initialize();
100         UnwindAssemblyInstEmulation::Initialize();
101         UnwindAssembly_x86::Initialize();
102         EmulateInstructionARM::Initialize ();
103         DynamicLoaderPOSIXDYLD::Initialize ();
104         PlatformFreeBSD::Initialize();
105         SymbolFileDWARFDebugMap::Initialize();
106         ItaniumABILanguageRuntime::Initialize();
107 #ifndef LLDB_DISABLE_PYTHON
108         OperatingSystemPython::Initialize();
109 #endif
110
111 #if defined (__APPLE__)
112         //----------------------------------------------------------------------
113         // Apple/Darwin hosted plugins
114         //----------------------------------------------------------------------
115         DynamicLoaderMacOSXDYLD::Initialize();
116         DynamicLoaderDarwinKernel::Initialize();
117         AppleObjCRuntimeV2::Initialize();
118         AppleObjCRuntimeV1::Initialize();
119         ObjectContainerUniversalMachO::Initialize();
120         ObjectFileMachO::Initialize();
121         ProcessKDP::Initialize();
122         PlatformDarwinKernel::Initialize();
123         PlatformRemoteiOS::Initialize();
124         PlatformMacOSX::Initialize();
125         PlatformiOSSimulator::Initialize();
126 #endif
127 #if defined (__linux__)
128         //----------------------------------------------------------------------
129         // Linux hosted plugins
130         //----------------------------------------------------------------------
131         ProcessLinux::Initialize();
132 #endif
133 #if defined (__FreeBSD__)
134         ProcessFreeBSD::Initialize();
135 #endif
136
137 #if defined(__linux__) or defined(__FreeBSD__)
138         ProcessElfCore::Initialize();
139 #endif
140         //----------------------------------------------------------------------
141         // Platform agnostic plugins
142         //----------------------------------------------------------------------
143         PlatformRemoteGDBServer::Initialize ();
144         ProcessGDBRemote::Initialize();
145         DynamicLoaderStatic::Initialize();
146
147         // Scan for any system or user LLDB plug-ins
148         PluginManager::Initialize();
149
150         // The process settings need to know about installed plug-ins, so the Settings must be initialized
151         // AFTER PluginManager::Initialize is called.
152         
153         Debugger::SettingsInitialize();
154     }
155 }
156
157 void
158 lldb_private::WillTerminate()
159 {
160     Host::WillTerminate();
161 }
162
163 void
164 lldb_private::Terminate ()
165 {
166     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
167     
168     // Terminate and unload and loaded system or user LLDB plug-ins
169     PluginManager::Terminate();
170
171     ABISysV_x86_64::Terminate();
172     DisassemblerLLVMC::Terminate();
173     ObjectContainerBSDArchive::Terminate();
174     ObjectFileELF::Terminate();
175     SymbolVendorELF::Terminate();
176     SymbolFileDWARF::Terminate();
177     SymbolFileSymtab::Terminate();
178     UnwindAssembly_x86::Terminate();
179     UnwindAssemblyInstEmulation::Terminate();
180     EmulateInstructionARM::Terminate ();
181     DynamicLoaderPOSIXDYLD::Terminate ();
182     PlatformFreeBSD::Terminate();
183     SymbolFileDWARFDebugMap::Terminate();
184     ItaniumABILanguageRuntime::Terminate();
185 #ifndef LLDB_DISABLE_PYTHON
186     OperatingSystemPython::Terminate();
187 #endif
188
189 #if defined (__APPLE__)
190     DynamicLoaderMacOSXDYLD::Terminate();
191     DynamicLoaderDarwinKernel::Terminate();
192     AppleObjCRuntimeV2::Terminate();
193     AppleObjCRuntimeV1::Terminate();
194     ObjectContainerUniversalMachO::Terminate();
195     ObjectFileMachO::Terminate();
196     ProcessKDP::Terminate();
197     PlatformMacOSX::Terminate();
198     PlatformDarwinKernel::Terminate();
199     PlatformRemoteiOS::Terminate();
200     PlatformiOSSimulator::Terminate();
201 #endif
202
203     Debugger::SettingsTerminate ();
204
205 #if defined (__linux__)
206     ProcessLinux::Terminate();
207 #endif
208
209 #if defined (__FreeBSD__)
210     ProcessFreeBSD::Terminate();
211 #endif
212
213 #if defined(__linux__) or defined(__FreeBSD__)
214     ProcessElfCore::Terminate();
215 #endif
216     ProcessGDBRemote::Terminate();
217     DynamicLoaderStatic::Terminate();
218
219     Log::Terminate();
220 }
221
222 #if defined (__APPLE__)
223 extern "C" const unsigned char liblldb_coreVersionString[];
224 #else
225
226 #include "clang/Basic/Version.h"
227
228 static const char *
229 GetLLDBRevision()
230 {
231 #ifdef LLDB_REVISION
232     return LLDB_REVISION;
233 #else
234     return NULL;
235 #endif
236 }
237
238 static const char *
239 GetLLDBRepository()
240 {
241 #ifdef LLDB_REPOSITORY
242     return LLDB_REPOSITORY;
243 #else
244     return NULL;
245 #endif
246 }
247
248 #endif
249
250 const char *
251 lldb_private::GetVersion ()
252 {
253 #if defined (__APPLE__)
254     static char g_version_string[32];
255     if (g_version_string[0] == '\0')
256     {
257         const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
258         
259         if (version_string)
260             version_string += sizeof("PROJECT:") - 1;
261         else
262             version_string = "unknown";
263         
264         const char *newline_loc = strchr(version_string, '\n');
265         
266         size_t version_len = sizeof(g_version_string);
267         
268         if (newline_loc && (newline_loc - version_string < version_len))
269             version_len = newline_loc - version_string;
270         
271         ::strncpy(g_version_string, version_string, version_len);
272     }
273
274     return g_version_string;
275 #else
276     // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
277     static std::string g_version_str;
278     if (g_version_str.empty())
279     {
280         g_version_str += "lldb version ";
281         g_version_str += CLANG_VERSION_STRING;
282         const char * lldb_repo = GetLLDBRepository();
283         if (lldb_repo)
284         {
285             g_version_str += " (";
286             g_version_str += lldb_repo;
287         }
288
289         const char *lldb_rev = GetLLDBRevision();
290         if (lldb_rev)
291         {
292             g_version_str += " revision ";
293             g_version_str += lldb_rev;
294         }
295         std::string clang_rev (clang::getClangRevision());
296         if (clang_rev.length() > 0)
297         {
298             g_version_str += " clang revision ";
299             g_version_str += clang_rev;
300         }
301         std::string llvm_rev (clang::getLLVMRevision());
302         if (llvm_rev.length() > 0)
303         {
304             g_version_str += " llvm revision ";
305             g_version_str += llvm_rev;
306         }
307
308         if (lldb_repo)
309             g_version_str += ")";
310     }
311     return g_version_str.c_str();
312 #endif
313 }
314
315 const char *
316 lldb_private::GetVoteAsCString (Vote vote)
317 {
318     switch (vote)
319     {
320     case eVoteNo:           return "no";
321     case eVoteNoOpinion:    return "no opinion";
322     case eVoteYes:          return "yes";
323     }
324     return "invalid";
325 }
326
327
328 const char *
329 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
330 {
331     switch (sect_type)
332     {
333     case eSectionTypeInvalid: return "invalid";
334     case eSectionTypeCode: return "code";
335     case eSectionTypeContainer: return "container";
336     case eSectionTypeData: return "data";
337     case eSectionTypeDataCString: return "data-cstr";
338     case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
339     case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
340     case eSectionTypeData4: return "data-4-byte";
341     case eSectionTypeData8: return "data-8-byte";
342     case eSectionTypeData16: return "data-16-byte";
343     case eSectionTypeDataPointers: return "data-ptrs";
344     case eSectionTypeDebug: return "debug";
345     case eSectionTypeZeroFill: return "zero-fill";
346     case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
347     case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
348     case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
349     case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
350     case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
351     case eSectionTypeDWARFDebugInfo: return "dwarf-info";
352     case eSectionTypeDWARFDebugLine: return "dwarf-line";
353     case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
354     case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
355     case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
356     case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
357     case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
358     case eSectionTypeDWARFDebugStr: return "dwarf-str";
359     case eSectionTypeELFSymbolTable: return "elf-symbol-table";
360     case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
361     case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
362     case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
363     case eSectionTypeDWARFAppleNames: return "apple-names";
364     case eSectionTypeDWARFAppleTypes: return "apple-types";
365     case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
366     case eSectionTypeDWARFAppleObjC: return "apple-objc";
367     case eSectionTypeEHFrame: return "eh-frame";
368     case eSectionTypeOther: return "regular";
369     }
370     return "unknown";
371
372 }
373
374 bool
375 lldb_private::NameMatches (const char *name, 
376                            NameMatchType match_type, 
377                            const char *match)
378 {
379     if (match_type == eNameMatchIgnore)
380         return true;
381
382     if (name == match)
383         return true;
384
385     if (name && match)
386     {
387         llvm::StringRef name_sref(name);
388         llvm::StringRef match_sref(match);
389         switch (match_type)
390         {
391         case eNameMatchIgnore:
392             return true;
393         case eNameMatchEquals:      return name_sref == match_sref;
394         case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
395         case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
396         case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
397         case eNameMatchRegularExpression:
398             {
399                 RegularExpression regex (match);
400                 return regex.Execute (name);
401             }
402             break;
403         }
404     }
405     return false;
406 }