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   ReproducerMode mode = ReproducerMode::Off;
69   if (options.reproducer_capture)
70     mode = ReproducerMode::Capture;
71   if (options.reproducer_replay)
72     mode = ReproducerMode::Replay;
73 
74   if (auto e = Reproducer::Initialize(mode, FileSpec(options.reproducer_path)))
75     return e;
76 
77   FileSystem::Initialize();
78   Log::Initialize();
79   HostInfo::Initialize();
80   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
81   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
82 
83   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
84 
85   // Initialize plug-ins
86   ObjectContainerBSDArchive::Initialize();
87 
88   EmulateInstructionARM::Initialize();
89   EmulateInstructionMIPS::Initialize();
90   EmulateInstructionMIPS64::Initialize();
91 
92   //----------------------------------------------------------------------
93   // Apple/Darwin hosted plugins
94   //----------------------------------------------------------------------
95   ObjectContainerUniversalMachO::Initialize();
96 
97 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
98   ProcessPOSIXLog::Initialize();
99 #endif
100 #if defined(_MSC_VER)
101   ProcessWindowsLog::Initialize();
102 #endif
103 
104   return llvm::Error::success();
105 }
106 
107 void SystemInitializerCommon::Terminate() {
108   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
109   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
110   ObjectContainerBSDArchive::Terminate();
111 
112   EmulateInstructionARM::Terminate();
113   EmulateInstructionMIPS::Terminate();
114   EmulateInstructionMIPS64::Terminate();
115 
116   ObjectContainerUniversalMachO::Terminate();
117 
118 #if defined(_MSC_VER)
119   ProcessWindowsLog::Terminate();
120 #endif
121 
122   HostInfo::Terminate();
123   Log::DisableAllLogChannels();
124   FileSystem::Terminate();
125   Reproducer::Terminate();
126 }
127