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     defined(__OpenBSD__)
23 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
24 #endif
25 
26 #if defined(_WIN32)
27 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
28 #include "lldb/Host/windows/windows.h"
29 #include <crtdbg.h>
30 #endif
31 
32 #include "llvm/Support/TargetSelect.h"
33 
34 #include <string>
35 
36 using namespace lldb_private;
37 
SystemInitializerCommon(HostInfo::SharedLibraryDirectoryHelper * helper)38 SystemInitializerCommon::SystemInitializerCommon(
39     HostInfo::SharedLibraryDirectoryHelper *helper)
40     : m_shlib_dir_helper(helper) {}
41 
42 SystemInitializerCommon::~SystemInitializerCommon() = default;
43 
Initialize()44 llvm::Error SystemInitializerCommon::Initialize() {
45 #if defined(_WIN32)
46   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
47   if (disable_crash_dialog_var &&
48       llvm::StringRef(disable_crash_dialog_var).equals_insensitive("true")) {
49     // This will prevent Windows from displaying a dialog box requiring user
50     // interaction when
51     // LLDB crashes.  This is mostly useful when automating LLDB, for example
52     // via the test
53     // suite, so that a crash in LLDB does not prevent completion of the test
54     // suite.
55     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
56                    SEM_NOGPFAULTERRORBOX);
57 
58     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
59     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
60     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
61     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
62     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
63     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
64   }
65 #endif
66 
67   InitializeLldbChannel();
68 
69   Diagnostics::Initialize();
70   FileSystem::Initialize();
71   HostInfo::Initialize(m_shlib_dir_helper);
72 
73   llvm::Error error = Socket::Initialize();
74   if (error)
75     return error;
76 
77   LLDB_SCOPED_TIMER();
78 
79   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
80 
81 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) ||       \
82     defined(__OpenBSD__)
83   ProcessPOSIXLog::Initialize();
84 #endif
85 #if defined(_WIN32)
86   ProcessWindowsLog::Initialize();
87 #endif
88 
89   return llvm::Error::success();
90 }
91 
Terminate()92 void SystemInitializerCommon::Terminate() {
93   LLDB_SCOPED_TIMER();
94 
95 #if defined(_WIN32)
96   ProcessWindowsLog::Terminate();
97 #endif
98 
99   Socket::Terminate();
100   HostInfo::Terminate();
101   Log::DisableAllLogChannels();
102   FileSystem::Terminate();
103   Diagnostics::Terminate();
104 }
105