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/Utility/Log.h"
21 #include "lldb/Utility/Reproducer.h"
22 #include "lldb/Utility/Timer.h"
23 
24 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
25 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
26 #endif
27 
28 #if defined(_MSC_VER)
29 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
30 #include "lldb/Host/windows/windows.h"
31 #endif
32 
33 #include "llvm/Support/TargetSelect.h"
34 
35 #include <string>
36 
37 using namespace lldb_private;
38 using namespace lldb_private::repro;
39 
40 SystemInitializerCommon::SystemInitializerCommon() {}
41 
42 SystemInitializerCommon::~SystemInitializerCommon() {}
43 
44 llvm::Error
45 SystemInitializerCommon::Initialize(const InitializerOptions &options) {
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   // Initialize the reproducer.
69   ReproducerMode mode = ReproducerMode::Off;
70   if (options.reproducer_capture)
71     mode = ReproducerMode::Capture;
72   if (options.reproducer_replay)
73     mode = ReproducerMode::Replay;
74 
75   if (auto e = Reproducer::Initialize(mode, FileSpec(options.reproducer_path)))
76     return e;
77 
78   // Initialize the file system.
79   auto &r = repro::Reproducer::Instance();
80   if (repro::Loader *loader = r.GetLoader()) {
81     FileSpec vfs_mapping = loader->GetFile<FileInfo>();
82     if (vfs_mapping) {
83       if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
84         return e;
85     } else {
86       FileSystem::Initialize();
87     }
88   } else if (repro::Generator *g = r.GetGenerator()) {
89     repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
90     FileSystem::Initialize(fp.GetFileCollector());
91   } else {
92     FileSystem::Initialize();
93   }
94 
95   Log::Initialize();
96   HostInfo::Initialize();
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   // Initialize plug-ins
103   ObjectContainerBSDArchive::Initialize();
104 
105   EmulateInstructionARM::Initialize();
106   EmulateInstructionMIPS::Initialize();
107   EmulateInstructionMIPS64::Initialize();
108 
109   //----------------------------------------------------------------------
110   // Apple/Darwin hosted plugins
111   //----------------------------------------------------------------------
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   HostInfo::Terminate();
140   Log::DisableAllLogChannels();
141   FileSystem::Terminate();
142   Reproducer::Terminate();
143 }
144