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/Instruction/ARM/EmulateInstructionARM.h"
12 #include "Plugins/Instruction/MIPS/EmulateInstructionMIPS.h"
13 #include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
14 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
15 #include "lldb/Host/FileSystem.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Host/HostInfo.h"
18 #include "lldb/Host/Socket.h"
19 #include "lldb/Utility/Log.h"
20 #include "lldb/Utility/Reproducer.h"
21 #include "lldb/Utility/Timer.h"
22 
23 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
24 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
25 #endif
26 
27 #if defined(_WIN32)
28 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
29 #include "lldb/Host/windows/windows.h"
30 #endif
31 
32 #include "llvm/Support/TargetSelect.h"
33 
34 #include <string>
35 
36 using namespace lldb_private;
37 using namespace lldb_private::repro;
38 
39 SystemInitializerCommon::SystemInitializerCommon() {}
40 
41 SystemInitializerCommon::~SystemInitializerCommon() {}
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_lower("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   // If the reproducer wasn't initialized before, we can safely assume it's
67   // off.
68   if (!Reproducer::Initialized()) {
69     if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
70       return e;
71   }
72 
73   // Initialize the file system.
74   auto &r = repro::Reproducer::Instance();
75   if (repro::Loader *loader = r.GetLoader()) {
76     FileSpec vfs_mapping = loader->GetFile<FileInfo>();
77     if (vfs_mapping) {
78       if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
79         return e;
80     } else {
81       FileSystem::Initialize();
82     }
83   } else if (repro::Generator *g = r.GetGenerator()) {
84     repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
85     FileSystem::Initialize(fp.GetFileCollector());
86   } else {
87     FileSystem::Initialize();
88   }
89 
90   Log::Initialize();
91   HostInfo::Initialize();
92 
93   llvm::Error error = Socket::Initialize();
94   if (error)
95     return error;
96 
97   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
98   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
99 
100   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
101 
102   EmulateInstructionARM::Initialize();
103   EmulateInstructionMIPS::Initialize();
104   EmulateInstructionMIPS64::Initialize();
105 
106 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
107   ProcessPOSIXLog::Initialize();
108 #endif
109 #if defined(_WIN32)
110   ProcessWindowsLog::Initialize();
111 #endif
112 
113   return llvm::Error::success();
114 }
115 
116 void SystemInitializerCommon::Terminate() {
117   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
118   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
119 
120   EmulateInstructionARM::Terminate();
121   EmulateInstructionMIPS::Terminate();
122   EmulateInstructionMIPS64::Terminate();
123 
124 #if defined(_WIN32)
125   ProcessWindowsLog::Terminate();
126 #endif
127 
128   Socket::Terminate();
129   HostInfo::Terminate();
130   Log::DisableAllLogChannels();
131   FileSystem::Terminate();
132   Reproducer::Terminate();
133 }
134