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/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 #include <crtdbg.h> 29 #endif 30 31 #include "llvm/Support/TargetSelect.h" 32 33 #include <string> 34 35 using namespace lldb_private; 36 using namespace lldb_private::repro; 37 38 SystemInitializerCommon::SystemInitializerCommon() {} 39 40 SystemInitializerCommon::~SystemInitializerCommon() {} 41 42 /// Initialize the FileSystem based on the current reproducer mode. 43 static llvm::Error InitializeFileSystem() { 44 auto &r = repro::Reproducer::Instance(); 45 if (repro::Loader *loader = r.GetLoader()) { 46 FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); 47 if (vfs_mapping) { 48 if (llvm::Error e = FileSystem::Initialize(vfs_mapping)) 49 return e; 50 } else { 51 FileSystem::Initialize(); 52 } 53 54 llvm::Expected<std::string> cwd = 55 loader->LoadBuffer<WorkingDirectoryProvider>(); 56 if (!cwd) 57 return cwd.takeError(); 58 59 llvm::StringRef working_dir = llvm::StringRef(*cwd).rtrim(); 60 if (std::error_code ec = FileSystem::Instance() 61 .GetVirtualFileSystem() 62 ->setCurrentWorkingDirectory(working_dir)) { 63 return llvm::errorCodeToError(ec); 64 } 65 66 return llvm::Error::success(); 67 } 68 69 if (repro::Generator *g = r.GetGenerator()) { 70 repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>(); 71 vp.SetVersion(lldb_private::GetVersion()); 72 73 repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>(); 74 FileSystem::Initialize(fp.GetFileCollector()); 75 76 repro::WorkingDirectoryProvider &wp = 77 g->GetOrCreate<repro::WorkingDirectoryProvider>(); 78 fp.RecordInterestingDirectory(wp.GetDirectory()); 79 80 return llvm::Error::success(); 81 } 82 83 FileSystem::Initialize(); 84 return llvm::Error::success(); 85 } 86 87 llvm::Error SystemInitializerCommon::Initialize() { 88 #if defined(_WIN32) 89 const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG"); 90 if (disable_crash_dialog_var && 91 llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) { 92 // This will prevent Windows from displaying a dialog box requiring user 93 // interaction when 94 // LLDB crashes. This is mostly useful when automating LLDB, for example 95 // via the test 96 // suite, so that a crash in LLDB does not prevent completion of the test 97 // suite. 98 ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | 99 SEM_NOGPFAULTERRORBOX); 100 101 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 102 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 103 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 104 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); 105 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); 106 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); 107 } 108 #endif 109 110 // If the reproducer wasn't initialized before, we can safely assume it's 111 // off. 112 if (!Reproducer::Initialized()) { 113 if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None)) 114 return e; 115 } 116 117 if (auto e = InitializeFileSystem()) 118 return e; 119 120 Log::Initialize(); 121 HostInfo::Initialize(); 122 123 llvm::Error error = Socket::Initialize(); 124 if (error) 125 return error; 126 127 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 128 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 129 130 process_gdb_remote::ProcessGDBRemoteLog::Initialize(); 131 132 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) 133 ProcessPOSIXLog::Initialize(); 134 #endif 135 #if defined(_WIN32) 136 ProcessWindowsLog::Initialize(); 137 #endif 138 139 return llvm::Error::success(); 140 } 141 142 void SystemInitializerCommon::Terminate() { 143 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 144 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 145 146 #if defined(_WIN32) 147 ProcessWindowsLog::Terminate(); 148 #endif 149 150 Socket::Terminate(); 151 HostInfo::Terminate(); 152 Log::DisableAllLogChannels(); 153 FileSystem::Terminate(); 154 Reproducer::Terminate(); 155 } 156