1 //===-- SystemInitializerCommon.cpp ---------------------------------------===// 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/Socket.h" 15 #include "lldb/Utility/LLDBLog.h" 16 #include "lldb/Utility/ReproducerProvider.h" 17 #include "lldb/Utility/Timer.h" 18 #include "lldb/Version/Version.h" 19 20 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) 21 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 22 #endif 23 24 #if defined(_WIN32) 25 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" 26 #include "lldb/Host/windows/windows.h" 27 #include <crtdbg.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 HostInfo::SharedLibraryDirectoryHelper *helper) 39 : m_shlib_dir_helper(helper) {} 40 41 SystemInitializerCommon::~SystemInitializerCommon() = default; 42 43 /// Initialize the FileSystem based on the current reproducer mode. 44 static llvm::Error InitializeFileSystem() { 45 auto &r = repro::Reproducer::Instance(); 46 47 if (repro::Generator *g = r.GetGenerator()) { 48 repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>(); 49 vp.SetVersion(lldb_private::GetVersion()); 50 51 repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>(); 52 53 FileSystem::Initialize(llvm::FileCollector::createCollectorVFS( 54 llvm::vfs::getRealFileSystem(), fp.GetFileCollector())); 55 56 fp.RecordInterestingDirectory( 57 g->GetOrCreate<repro::WorkingDirectoryProvider>().GetDirectory()); 58 fp.RecordInterestingDirectory( 59 g->GetOrCreate<repro::HomeDirectoryProvider>().GetDirectory()); 60 61 return llvm::Error::success(); 62 } 63 64 FileSystem::Initialize(); 65 return llvm::Error::success(); 66 } 67 68 llvm::Error SystemInitializerCommon::Initialize() { 69 #if defined(_WIN32) 70 const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG"); 71 if (disable_crash_dialog_var && 72 llvm::StringRef(disable_crash_dialog_var).equals_insensitive("true")) { 73 // This will prevent Windows from displaying a dialog box requiring user 74 // interaction when 75 // LLDB crashes. This is mostly useful when automating LLDB, for example 76 // via the test 77 // suite, so that a crash in LLDB does not prevent completion of the test 78 // suite. 79 ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | 80 SEM_NOGPFAULTERRORBOX); 81 82 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 83 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 84 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 85 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); 86 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); 87 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); 88 } 89 #endif 90 91 // If the reproducer wasn't initialized before, we can safely assume it's 92 // off. 93 if (!Reproducer::Initialized()) { 94 if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None)) 95 return e; 96 } 97 98 if (auto e = InitializeFileSystem()) 99 return e; 100 101 InitializeLldbChannel(); 102 HostInfo::Initialize(m_shlib_dir_helper); 103 104 llvm::Error error = Socket::Initialize(); 105 if (error) 106 return error; 107 108 LLDB_SCOPED_TIMER(); 109 110 process_gdb_remote::ProcessGDBRemoteLog::Initialize(); 111 112 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) 113 ProcessPOSIXLog::Initialize(); 114 #endif 115 #if defined(_WIN32) 116 ProcessWindowsLog::Initialize(); 117 #endif 118 119 return llvm::Error::success(); 120 } 121 122 void SystemInitializerCommon::Terminate() { 123 LLDB_SCOPED_TIMER(); 124 125 #if defined(_WIN32) 126 ProcessWindowsLog::Terminate(); 127 #endif 128 129 Socket::Terminate(); 130 HostInfo::Terminate(); 131 Log::DisableAllLogChannels(); 132 FileSystem::Terminate(); 133 Reproducer::Terminate(); 134 } 135