1 //===-- SystemInitializerCommon.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/Initialization/SystemInitializerCommon.h"
11 
12 #include "lldb/Host/Host.h"
13 #include "lldb/Host/HostInfo.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/Symbol/GoASTContext.h"
17 #include "lldb/Symbol/ClangASTContext.h"
18 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
19 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
20 #include "Plugins/Instruction/MIPS/EmulateInstructionMIPS.h"
21 #include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
22 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
23 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
24 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
25 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
26 #include "Plugins/Platform/NetBSD/PlatformNetBSD.h"
27 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
28 
29 #if defined(__APPLE__)
30 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
31 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
32 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
33 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
34 #endif
35 
36 #if defined(__linux__)
37 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
38 #endif
39 
40 #if defined(_MSC_VER)
41 #include "lldb/Host/windows/windows.h"
42 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
43 #endif
44 
45 #include "llvm/Support/TargetSelect.h"
46 
47 #include <string>
48 
49 using namespace lldb_private;
50 
51 static void
52 fatal_error_handler(void *user_data, const std::string &reason, bool gen_crash_diag)
53 {
54     Host::SetCrashDescription(reason.c_str());
55     ::abort();
56 }
57 
58 SystemInitializerCommon::SystemInitializerCommon()
59 {
60 }
61 
62 SystemInitializerCommon::~SystemInitializerCommon()
63 {
64 }
65 
66 void
67 SystemInitializerCommon::Initialize()
68 {
69 #if defined(_MSC_VER)
70     const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
71     if (disable_crash_dialog_var && llvm::StringRef(disable_crash_dialog_var).equals_lower("true"))
72     {
73         // This will prevent Windows from displaying a dialog box requiring user interaction when
74         // LLDB crashes.  This is mostly useful when automating LLDB, for example via the test
75         // suite, so that a crash in LLDB does not prevent completion of the test suite.
76         ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
77 
78         _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
79         _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
80         _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
81         _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
82         _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
83         _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
84     }
85 #endif
86 
87     Log::Initialize();
88     HostInfo::Initialize();
89     Timer::Initialize();
90     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
91 
92     llvm::install_fatal_error_handler(fatal_error_handler, 0);
93 
94     process_gdb_remote::ProcessGDBRemoteLog::Initialize();
95 
96     // Initialize plug-ins
97     ClangASTContext::Initialize();
98     GoASTContext::Initialize();
99 
100     ObjectContainerBSDArchive::Initialize();
101     ObjectFileELF::Initialize();
102     DynamicLoaderPOSIXDYLD::Initialize();
103     platform_freebsd::PlatformFreeBSD::Initialize();
104 
105     EmulateInstructionARM::Initialize();
106     EmulateInstructionMIPS::Initialize();
107     EmulateInstructionMIPS64::Initialize();
108 
109     //----------------------------------------------------------------------
110     // Apple/Darwin hosted plugins
111     //----------------------------------------------------------------------
112 
113 #if defined(__APPLE__)
114     PlatformiOSSimulator::Initialize();
115     DynamicLoaderDarwinKernel::Initialize();
116     PlatformDarwinKernel::Initialize();
117     ObjectFileMachO::Initialize();
118 #endif
119 #if defined(__linux__)
120     static ConstString g_linux_log_name("linux");
121     ProcessPOSIXLog::Initialize(g_linux_log_name);
122 #endif
123 #if defined(_MSC_VER)
124     ProcessWindowsLog::Initialize();
125 #endif
126 #ifndef LLDB_DISABLE_PYTHON
127     OperatingSystemPython::Initialize();
128 #endif
129 }
130 
131 void
132 SystemInitializerCommon::Terminate()
133 {
134     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
135     ObjectContainerBSDArchive::Terminate();
136     ObjectFileELF::Terminate();
137     DynamicLoaderPOSIXDYLD::Terminate();
138     platform_freebsd::PlatformFreeBSD::Terminate();
139 
140     ClangASTContext::Terminate();
141     GoASTContext::Terminate();
142 
143     EmulateInstructionARM::Terminate();
144     EmulateInstructionMIPS::Terminate();
145     EmulateInstructionMIPS64::Terminate();
146 
147 #if defined(__APPLE__)
148     PlatformiOSSimulator::Terminate();
149     DynamicLoaderDarwinKernel::Terminate();
150     ObjectFileMachO::Terminate();
151     PlatformDarwinKernel::Terminate();
152 #endif
153 
154 #if defined(_MSC_VER)
155     ProcessWindowsLog::Terminate();
156 #endif
157 
158 #ifndef LLDB_DISABLE_PYTHON
159     OperatingSystemPython::Terminate();
160 #endif
161 
162     Log::Terminate();
163 }
164