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