]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/lldb.cpp
Merge bmake-20150418
[FreeBSD/FreeBSD.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/HostInfo.h"
22 #include "lldb/Host/Mutex.h"
23 #include "lldb/Interpreter/ScriptInterpreterPython.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/TargetSelect.h"
29
30 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
31 #include "Plugins/ABI/SysV-ppc/ABISysV_ppc.h"
32 #include "Plugins/ABI/SysV-ppc64/ABISysV_ppc64.h"
33 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
34 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
35 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
36 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
37 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
38 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
39 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
40 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
41 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
42 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
43 #include "Plugins/Process/elf-core/ProcessElfCore.h"
44 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
45 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
46 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
47 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
48 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
49 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
50
51 #ifndef LLDB_DISABLE_PYTHON
52 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
53 #endif
54 #if defined (__APPLE__)
55 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
56 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
57 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
58 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
59 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
60 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
61 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
62 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
63 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
64 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
65 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
66 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
67 #endif
68
69
70 #if defined (__linux__)
71 #include "Plugins/Process/Linux/ProcessLinux.h"
72 #endif
73
74 #if defined (_WIN32)
75 #include "lldb/Host/windows/windows.h"
76 #include "Plugins/Process/Windows/DynamicLoaderWindows.h"
77 #include "Plugins/Process/Windows/ProcessWindows.h"
78 #endif
79
80 #if defined (__FreeBSD__)
81 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
82 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
83 #endif
84
85 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
86 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
87 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
88 #include "Plugins/MemoryHistory/asan/MemoryHistoryASan.h"
89 #include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h"
90
91 using namespace lldb;
92 using namespace lldb_private;
93
94 static void fatal_error_handler(void *user_data, const std::string& reason,
95                                 bool gen_crash_diag) {
96     Host::SetCrashDescription(reason.c_str());
97     ::abort();
98 }
99
100 void
101 lldb_private::Initialize ()
102 {
103     // Make sure we inialize only once
104     static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
105     static bool g_inited = false;
106
107     Mutex::Locker locker(g_inited_mutex);
108     if (!g_inited)
109     {
110         g_inited = true;
111
112 #if defined(_MSC_VER)
113         const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
114         if (disable_crash_dialog_var && llvm::StringRef(disable_crash_dialog_var).equals_lower("true"))
115         {
116             // This will prevent Windows from displaying a dialog box requiring user interaction when
117             // LLDB crashes.  This is mostly useful when automating LLDB, for example via the test
118             // suite, so that a crash in LLDB does not prevent completion of the test suite.
119             ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
120
121             _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
122             _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
123             _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
124             _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
125             _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
126             _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
127         }
128 #endif
129
130         Log::Initialize();
131         HostInfo::Initialize();
132         Timer::Initialize ();
133         Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
134
135         // Initialize LLVM and Clang
136         llvm::InitializeAllTargets();
137         llvm::InitializeAllAsmPrinters();
138         llvm::InitializeAllTargetMCs();
139         llvm::InitializeAllDisassemblers();
140         llvm::install_fatal_error_handler(fatal_error_handler, 0);
141
142         // Initialize plug-ins
143         ABISysV_x86_64::Initialize();
144         ABISysV_ppc::Initialize();
145         ABISysV_ppc64::Initialize();
146         DisassemblerLLVMC::Initialize();
147         ObjectContainerBSDArchive::Initialize();
148         ObjectFileELF::Initialize();
149         SymbolVendorELF::Initialize();
150         SymbolFileDWARF::Initialize();
151         SymbolFileSymtab::Initialize();
152         UnwindAssemblyInstEmulation::Initialize();
153         UnwindAssembly_x86::Initialize();
154         EmulateInstructionARM::Initialize ();
155         EmulateInstructionARM64::Initialize ();
156         DynamicLoaderPOSIXDYLD::Initialize ();
157         PlatformFreeBSD::Initialize();
158         SymbolFileDWARFDebugMap::Initialize();
159         ItaniumABILanguageRuntime::Initialize();
160 #ifndef LLDB_DISABLE_PYTHON
161         ScriptInterpreterPython::InitializePrivate();
162         OperatingSystemPython::Initialize();
163 #endif
164         JITLoaderGDB::Initialize();
165         ProcessElfCore::Initialize();
166         MemoryHistoryASan::Initialize();
167         AddressSanitizerRuntime::Initialize();
168         
169 #if defined (__APPLE__)
170         //----------------------------------------------------------------------
171         // Apple/Darwin hosted plugins
172         //----------------------------------------------------------------------
173         DynamicLoaderMacOSXDYLD::Initialize();
174         DynamicLoaderDarwinKernel::Initialize();
175         AppleObjCRuntimeV2::Initialize();
176         AppleObjCRuntimeV1::Initialize();
177         ObjectContainerUniversalMachO::Initialize();
178         ObjectFileMachO::Initialize();
179         ProcessKDP::Initialize();
180         PlatformDarwinKernel::Initialize();
181         PlatformRemoteiOS::Initialize();
182         PlatformMacOSX::Initialize();
183         PlatformiOSSimulator::Initialize();
184         SystemRuntimeMacOSX::Initialize();
185 #endif
186 #if defined (__linux__)
187         //----------------------------------------------------------------------
188         // Linux hosted plugins
189         //----------------------------------------------------------------------
190         ProcessLinux::Initialize();
191 #endif
192 #if defined(_WIN32)
193         DynamicLoaderWindows::Initialize();
194         ProcessWindows::Initialize();
195 #endif
196 #if defined (__FreeBSD__)
197         ProcessFreeBSD::Initialize();
198 #endif
199
200         //----------------------------------------------------------------------
201         // Platform agnostic plugins
202         //----------------------------------------------------------------------
203         PlatformRemoteGDBServer::Initialize ();
204         ProcessGDBRemote::Initialize();
205         DynamicLoaderStatic::Initialize();
206
207         // Scan for any system or user LLDB plug-ins
208         PluginManager::Initialize();
209
210         // The process settings need to know about installed plug-ins, so the Settings must be initialized
211         // AFTER PluginManager::Initialize is called.
212         
213         Debugger::SettingsInitialize();
214     }
215 }
216
217 void
218 lldb_private::WillTerminate()
219 {
220     Host::WillTerminate();
221 }
222
223 void
224 lldb_private::Terminate ()
225 {
226     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
227     
228     // Terminate and unload and loaded system or user LLDB plug-ins
229     PluginManager::Terminate();
230     ABISysV_x86_64::Terminate();
231     ABISysV_ppc::Terminate();
232     ABISysV_ppc64::Terminate();
233     DisassemblerLLVMC::Terminate();
234     ObjectContainerBSDArchive::Terminate();
235     ObjectFileELF::Terminate();
236     SymbolVendorELF::Terminate();
237     SymbolFileDWARF::Terminate();
238     SymbolFileSymtab::Terminate();
239     UnwindAssembly_x86::Terminate();
240     UnwindAssemblyInstEmulation::Terminate();
241     EmulateInstructionARM::Terminate ();
242     EmulateInstructionARM64::Terminate ();
243     DynamicLoaderPOSIXDYLD::Terminate ();
244     PlatformFreeBSD::Terminate();
245     SymbolFileDWARFDebugMap::Terminate();
246     ItaniumABILanguageRuntime::Terminate();
247 #ifndef LLDB_DISABLE_PYTHON
248     OperatingSystemPython::Terminate();
249 #endif
250     JITLoaderGDB::Terminate();
251     ProcessElfCore::Terminate();
252     MemoryHistoryASan::Terminate();
253     AddressSanitizerRuntime::Terminate();
254     
255 #if defined (__APPLE__)
256     DynamicLoaderMacOSXDYLD::Terminate();
257     DynamicLoaderDarwinKernel::Terminate();
258     AppleObjCRuntimeV2::Terminate();
259     AppleObjCRuntimeV1::Terminate();
260     ObjectContainerUniversalMachO::Terminate();
261     ObjectFileMachO::Terminate();
262     ProcessKDP::Terminate();
263     PlatformMacOSX::Terminate();
264     PlatformDarwinKernel::Terminate();
265     PlatformRemoteiOS::Terminate();
266     PlatformiOSSimulator::Terminate();
267     SystemRuntimeMacOSX::Terminate();
268 #endif
269
270     Debugger::SettingsTerminate ();
271
272 #if defined (_WIN32)
273     DynamicLoaderWindows::Terminate();
274 #endif
275
276 #if defined (__linux__)
277     ProcessLinux::Terminate();
278 #endif
279
280 #if defined (__FreeBSD__)
281     ProcessFreeBSD::Terminate();
282 #endif
283
284     ProcessGDBRemote::Terminate();
285     DynamicLoaderStatic::Terminate();
286
287     Log::Terminate();
288 }
289
290 #if defined (__APPLE__)
291 extern "C" const unsigned char liblldb_coreVersionString[];
292 #else
293
294 #include "clang/Basic/Version.h"
295
296 static const char *
297 GetLLDBRevision()
298 {
299 #ifdef LLDB_REVISION
300     return LLDB_REVISION;
301 #else
302     return NULL;
303 #endif
304 }
305
306 static const char *
307 GetLLDBRepository()
308 {
309 #ifdef LLDB_REPOSITORY
310     return LLDB_REPOSITORY;
311 #else
312     return NULL;
313 #endif
314 }
315
316 #endif
317
318 const char *
319 lldb_private::GetVersion ()
320 {
321 #if defined (__APPLE__)
322     static char g_version_string[32];
323     if (g_version_string[0] == '\0')
324     {
325         const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
326         
327         if (version_string)
328             version_string += sizeof("PROJECT:") - 1;
329         else
330             version_string = "unknown";
331         
332         const char *newline_loc = strchr(version_string, '\n');
333         
334         size_t version_len = sizeof(g_version_string);
335         
336         if (newline_loc &&
337             (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
338             version_len = newline_loc - version_string;
339         
340         ::strncpy(g_version_string, version_string, version_len);
341     }
342
343     return g_version_string;
344 #else
345     // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
346     static std::string g_version_str;
347     if (g_version_str.empty())
348     {
349         g_version_str += "lldb version ";
350         g_version_str += CLANG_VERSION_STRING;
351         const char * lldb_repo = GetLLDBRepository();
352         if (lldb_repo)
353         {
354             g_version_str += " (";
355             g_version_str += lldb_repo;
356         }
357
358         const char *lldb_rev = GetLLDBRevision();
359         if (lldb_rev)
360         {
361             g_version_str += " revision ";
362             g_version_str += lldb_rev;
363         }
364         std::string clang_rev (clang::getClangRevision());
365         if (clang_rev.length() > 0)
366         {
367             g_version_str += " clang revision ";
368             g_version_str += clang_rev;
369         }
370         std::string llvm_rev (clang::getLLVMRevision());
371         if (llvm_rev.length() > 0)
372         {
373             g_version_str += " llvm revision ";
374             g_version_str += llvm_rev;
375         }
376
377         if (lldb_repo)
378             g_version_str += ")";
379     }
380     return g_version_str.c_str();
381 #endif
382 }
383
384 const char *
385 lldb_private::GetVoteAsCString (Vote vote)
386 {
387     switch (vote)
388     {
389     case eVoteNo:           return "no";
390     case eVoteNoOpinion:    return "no opinion";
391     case eVoteYes:          return "yes";
392     }
393     return "invalid";
394 }
395
396
397 const char *
398 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
399 {
400     switch (sect_type)
401     {
402     case eSectionTypeInvalid: return "invalid";
403     case eSectionTypeCode: return "code";
404     case eSectionTypeContainer: return "container";
405     case eSectionTypeData: return "data";
406     case eSectionTypeDataCString: return "data-cstr";
407     case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
408     case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
409     case eSectionTypeData4: return "data-4-byte";
410     case eSectionTypeData8: return "data-8-byte";
411     case eSectionTypeData16: return "data-16-byte";
412     case eSectionTypeDataPointers: return "data-ptrs";
413     case eSectionTypeDebug: return "debug";
414     case eSectionTypeZeroFill: return "zero-fill";
415     case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
416     case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
417     case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
418     case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
419     case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
420     case eSectionTypeDWARFDebugInfo: return "dwarf-info";
421     case eSectionTypeDWARFDebugLine: return "dwarf-line";
422     case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
423     case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
424     case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
425     case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
426     case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
427     case eSectionTypeDWARFDebugStr: return "dwarf-str";
428     case eSectionTypeELFSymbolTable: return "elf-symbol-table";
429     case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
430     case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
431     case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
432     case eSectionTypeDWARFAppleNames: return "apple-names";
433     case eSectionTypeDWARFAppleTypes: return "apple-types";
434     case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
435     case eSectionTypeDWARFAppleObjC: return "apple-objc";
436     case eSectionTypeEHFrame: return "eh-frame";
437     case eSectionTypeCompactUnwind: return "compact-unwind";
438     case eSectionTypeOther: return "regular";
439     }
440     return "unknown";
441
442 }
443
444 bool
445 lldb_private::NameMatches (const char *name, 
446                            NameMatchType match_type, 
447                            const char *match)
448 {
449     if (match_type == eNameMatchIgnore)
450         return true;
451
452     if (name == match)
453         return true;
454
455     if (name && match)
456     {
457         llvm::StringRef name_sref(name);
458         llvm::StringRef match_sref(match);
459         switch (match_type)
460         {
461         case eNameMatchIgnore: // This case cannot occur: tested before
462             return true;
463         case eNameMatchEquals:      return name_sref == match_sref;
464         case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
465         case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
466         case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
467         case eNameMatchRegularExpression:
468             {
469                 RegularExpression regex (match);
470                 return regex.Execute (name);
471             }
472             break;
473         }
474     }
475     return false;
476 }