1 //===-- SystemInitializerCommon.cpp -----------------------------*- C++ -*-===//
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/HostInfo.h"
15 #include "lldb/Host/Socket.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Reproducer.h"
18 #include "lldb/Utility/Timer.h"
19 
20 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
21 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
22 #endif
23 
24 #if defined(_WIN32)
25 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
26 #include "lldb/Host/windows/windows.h"
27 #endif
28 
29 #include "llvm/Support/TargetSelect.h"
30 
31 #include <string>
32 
33 using namespace lldb_private;
34 using namespace lldb_private::repro;
35 
36 SystemInitializerCommon::SystemInitializerCommon() {}
37 
38 SystemInitializerCommon::~SystemInitializerCommon() {}
39 
40 llvm::Error SystemInitializerCommon::Initialize() {
41 #if defined(_WIN32)
42   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
43   if (disable_crash_dialog_var &&
44       llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
45     // This will prevent Windows from displaying a dialog box requiring user
46     // interaction when
47     // LLDB crashes.  This is mostly useful when automating LLDB, for example
48     // via the test
49     // suite, so that a crash in LLDB does not prevent completion of the test
50     // suite.
51     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
52                    SEM_NOGPFAULTERRORBOX);
53 
54     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
55     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
56     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
57     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
58     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
59     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
60   }
61 #endif
62 
63   // If the reproducer wasn't initialized before, we can safely assume it's
64   // off.
65   if (!Reproducer::Initialized()) {
66     if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
67       return e;
68   }
69 
70   // Initialize the file system.
71   auto &r = repro::Reproducer::Instance();
72   if (repro::Loader *loader = r.GetLoader()) {
73     FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
74     if (vfs_mapping) {
75       if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
76         return e;
77     } else {
78       FileSystem::Initialize();
79     }
80   } else if (repro::Generator *g = r.GetGenerator()) {
81     repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
82     FileSystem::Initialize(fp.GetFileCollector());
83   } else {
84     FileSystem::Initialize();
85   }
86 
87   Log::Initialize();
88   HostInfo::Initialize();
89 
90   llvm::Error error = Socket::Initialize();
91   if (error)
92     return error;
93 
94   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
95   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
96 
97   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
98 
99 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
100   ProcessPOSIXLog::Initialize();
101 #endif
102 #if defined(_WIN32)
103   ProcessWindowsLog::Initialize();
104 #endif
105 
106   return llvm::Error::success();
107 }
108 
109 void SystemInitializerCommon::Terminate() {
110   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
111   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
112 
113 #if defined(_WIN32)
114   ProcessWindowsLog::Terminate();
115 #endif
116 
117   Socket::Terminate();
118   HostInfo::Terminate();
119   Log::DisableAllLogChannels();
120   FileSystem::Terminate();
121   Reproducer::Terminate();
122 }
123