xref: /freebsd-src/contrib/llvm-project/lldb/source/Initialization/SystemInitializerCommon.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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/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/Reproducer.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 #endif
29 
30 #include "llvm/Support/TargetSelect.h"
31 
32 #include <string>
33 
34 using namespace lldb_private;
35 using namespace lldb_private::repro;
36 
37 SystemInitializerCommon::SystemInitializerCommon() {}
38 
39 SystemInitializerCommon::~SystemInitializerCommon() {}
40 
41 llvm::Error SystemInitializerCommon::Initialize() {
42 #if defined(_WIN32)
43   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
44   if (disable_crash_dialog_var &&
45       llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
46     // This will prevent Windows from displaying a dialog box requiring user
47     // interaction when
48     // LLDB crashes.  This is mostly useful when automating LLDB, for example
49     // via the test
50     // suite, so that a crash in LLDB does not prevent completion of the test
51     // suite.
52     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
53                    SEM_NOGPFAULTERRORBOX);
54 
55     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
56     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
57     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
58     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
59     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
60     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
61   }
62 #endif
63 
64   // If the reproducer wasn't initialized before, we can safely assume it's
65   // off.
66   if (!Reproducer::Initialized()) {
67     if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
68       return e;
69   }
70 
71   auto &r = repro::Reproducer::Instance();
72   if (repro::Loader *loader = r.GetLoader()) {
73     FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
74     if (vfs_mapping) {
75       if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
76         return e;
77     } else {
78       FileSystem::Initialize();
79     }
80   } else if (repro::Generator *g = r.GetGenerator()) {
81     repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
82     vp.SetVersion(lldb_private::GetVersion());
83     repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
84     FileSystem::Initialize(fp.GetFileCollector());
85   } else {
86     FileSystem::Initialize();
87   }
88 
89   Log::Initialize();
90   HostInfo::Initialize();
91 
92   llvm::Error error = Socket::Initialize();
93   if (error)
94     return error;
95 
96   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
97   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
98 
99   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
100 
101 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
102   ProcessPOSIXLog::Initialize();
103 #endif
104 #if defined(_WIN32)
105   ProcessWindowsLog::Initialize();
106 #endif
107 
108   return llvm::Error::success();
109 }
110 
111 void SystemInitializerCommon::Terminate() {
112   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
113   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
114 
115 #if defined(_WIN32)
116   ProcessWindowsLog::Terminate();
117 #endif
118 
119   Socket::Terminate();
120   HostInfo::Terminate();
121   Log::DisableAllLogChannels();
122   FileSystem::Terminate();
123   Reproducer::Terminate();
124 }
125