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/HostInfo.h"
15 #include "lldb/Host/Socket.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/ReproducerProvider.h"
18 #include "lldb/Utility/Timer.h"
19 #include "lldb/lldb-private.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 using namespace lldb_private::repro;
37 
38 SystemInitializerCommon::SystemInitializerCommon() {}
39 
40 SystemInitializerCommon::~SystemInitializerCommon() {}
41 
42 /// Initialize the FileSystem based on the current reproducer mode.
43 static llvm::Error InitializeFileSystem() {
44   auto &r = repro::Reproducer::Instance();
45   if (repro::Loader *loader = r.GetLoader()) {
46     FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
47     if (vfs_mapping) {
48       if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
49         return e;
50     } else {
51       FileSystem::Initialize();
52     }
53 
54     // Set the current working directory form the reproducer.
55     llvm::Expected<std::string> working_dir =
56         repro::GetDirectoryFrom<WorkingDirectoryProvider>(loader);
57     if (!working_dir)
58       return working_dir.takeError();
59     if (std::error_code ec = FileSystem::Instance()
60                                  .GetVirtualFileSystem()
61                                  ->setCurrentWorkingDirectory(*working_dir)) {
62       return llvm::errorCodeToError(ec);
63     }
64 
65     // Set the home directory from the reproducer.
66     llvm::Expected<std::string> home_dir =
67         repro::GetDirectoryFrom<HomeDirectoryProvider>(loader);
68     if (!home_dir)
69       return home_dir.takeError();
70     FileSystem::Instance().SetHomeDirectory(*home_dir);
71 
72     return llvm::Error::success();
73   }
74 
75   if (repro::Generator *g = r.GetGenerator()) {
76     repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
77     vp.SetVersion(lldb_private::GetVersion());
78 
79     repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
80     FileSystem::Initialize(fp.GetFileCollector());
81 
82     repro::WorkingDirectoryProvider &wp =
83         g->GetOrCreate<repro::WorkingDirectoryProvider>();
84     fp.RecordInterestingDirectory(wp.GetDirectory());
85 
86     return llvm::Error::success();
87   }
88 
89   FileSystem::Initialize();
90   return llvm::Error::success();
91 }
92 
93 llvm::Error SystemInitializerCommon::Initialize() {
94 #if defined(_WIN32)
95   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
96   if (disable_crash_dialog_var &&
97       llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
98     // This will prevent Windows from displaying a dialog box requiring user
99     // interaction when
100     // LLDB crashes.  This is mostly useful when automating LLDB, for example
101     // via the test
102     // suite, so that a crash in LLDB does not prevent completion of the test
103     // suite.
104     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
105                    SEM_NOGPFAULTERRORBOX);
106 
107     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
108     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
109     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
110     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
111     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
112     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
113   }
114 #endif
115 
116   // If the reproducer wasn't initialized before, we can safely assume it's
117   // off.
118   if (!Reproducer::Initialized()) {
119     if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
120       return e;
121   }
122 
123   if (auto e = InitializeFileSystem())
124     return e;
125 
126   Log::Initialize();
127   HostInfo::Initialize();
128 
129   llvm::Error error = Socket::Initialize();
130   if (error)
131     return error;
132 
133   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
134   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
135 
136   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
137 
138 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
139   ProcessPOSIXLog::Initialize();
140 #endif
141 #if defined(_WIN32)
142   ProcessWindowsLog::Initialize();
143 #endif
144 
145   return llvm::Error::success();
146 }
147 
148 void SystemInitializerCommon::Terminate() {
149   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
150   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
151 
152 #if defined(_WIN32)
153   ProcessWindowsLog::Terminate();
154 #endif
155 
156   Socket::Terminate();
157   HostInfo::Terminate();
158   Log::DisableAllLogChannels();
159   FileSystem::Terminate();
160   Reproducer::Terminate();
161 }
162