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 } else if (repro::Generator *g = r.GetGenerator()) { 82 repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>(); 83 vp.SetVersion(lldb_private::GetVersion()); 84 repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>(); 85 FileSystem::Initialize(fp.GetFileCollector()); 86 } else { 87 FileSystem::Initialize(); 88 } 89 90 Log::Initialize(); 91 HostInfo::Initialize(); 92 93 llvm::Error error = Socket::Initialize(); 94 if (error) 95 return error; 96 97 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 98 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 99 100 process_gdb_remote::ProcessGDBRemoteLog::Initialize(); 101 102 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) 103 ProcessPOSIXLog::Initialize(); 104 #endif 105 #if defined(_WIN32) 106 ProcessWindowsLog::Initialize(); 107 #endif 108 109 return llvm::Error::success(); 110 } 111 112 void SystemInitializerCommon::Terminate() { 113 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 114 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 115 116 #if defined(_WIN32) 117 ProcessWindowsLog::Terminate(); 118 #endif 119 120 Socket::Terminate(); 121 HostInfo::Terminate(); 122 Log::DisableAllLogChannels(); 123 FileSystem::Terminate(); 124 Reproducer::Terminate(); 125 } 126