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