1 //===-- SystemInitializerCommon.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Initialization/SystemInitializerCommon.h"
10 
11 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/Host.h"
14 #include "lldb/Host/Socket.h"
15 #include "lldb/Target/Statistics.h"
16 #include "lldb/Utility/Diagnostics.h"
17 #include "lldb/Utility/LLDBLog.h"
18 #include "lldb/Utility/Timer.h"
19 #include "lldb/Version/Version.h"
20 
21 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
22 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
23 #endif
24 
25 #if defined(_WIN32)
26 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
27 #include "lldb/Host/windows/windows.h"
28 #include <crtdbg.h>
29 #endif
30 
31 #include "llvm/Support/TargetSelect.h"
32 
33 #include <string>
34 
35 using namespace lldb_private;
36 
37 SystemInitializerCommon::SystemInitializerCommon(
38     HostInfo::SharedLibraryDirectoryHelper *helper)
39     : m_shlib_dir_helper(helper) {}
40 
41 SystemInitializerCommon::~SystemInitializerCommon() = default;
42 
43 llvm::Error SystemInitializerCommon::Initialize() {
44 #if defined(_WIN32)
45   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
46   if (disable_crash_dialog_var &&
47       llvm::StringRef(disable_crash_dialog_var).equals_insensitive("true")) {
48     // This will prevent Windows from displaying a dialog box requiring user
49     // interaction when
50     // LLDB crashes.  This is mostly useful when automating LLDB, for example
51     // via the test
52     // suite, so that a crash in LLDB does not prevent completion of the test
53     // suite.
54     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
55                    SEM_NOGPFAULTERRORBOX);
56 
57     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
58     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
59     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
60     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
61     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
62     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
63   }
64 #endif
65 
66   InitializeLldbChannel();
67 
68   Diagnostics::Initialize();
69   FileSystem::Initialize();
70   HostInfo::Initialize(m_shlib_dir_helper);
71 
72   llvm::Error error = Socket::Initialize();
73   if (error)
74     return error;
75 
76   LLDB_SCOPED_TIMER();
77 
78   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
79 
80 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
81   ProcessPOSIXLog::Initialize();
82 #endif
83 #if defined(_WIN32)
84   ProcessWindowsLog::Initialize();
85 #endif
86 
87   return llvm::Error::success();
88 }
89 
90 void SystemInitializerCommon::Terminate() {
91   LLDB_SCOPED_TIMER();
92 
93 #if defined(_WIN32)
94   ProcessWindowsLog::Terminate();
95 #endif
96 
97   Socket::Terminate();
98   HostInfo::Terminate();
99   Log::DisableAllLogChannels();
100   FileSystem::Terminate();
101   Diagnostics::Terminate();
102 }
103