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