2020-01-24 08:23:27 +01:00
|
|
|
//===-- SystemInitializerCommon.cpp ---------------------------------------===//
|
2015-03-31 21:03:22 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-03-31 21:03:22 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "lldb/Initialization/SystemInitializerCommon.h"
|
|
|
|
|
|
|
|
|
|
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
|
2018-10-31 21:49:27 +00:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2015-03-31 21:03:22 +00:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
|
#include "lldb/Host/HostInfo.h"
|
2019-04-10 04:57:18 +00:00
|
|
|
#include "lldb/Host/Socket.h"
|
2017-03-03 20:56:28 +00:00
|
|
|
#include "lldb/Utility/Log.h"
|
2020-08-22 00:36:32 -07:00
|
|
|
#include "lldb/Utility/ReproducerProvider.h"
|
2017-06-29 14:32:17 +00:00
|
|
|
#include "lldb/Utility/Timer.h"
|
2019-06-13 04:35:22 +00:00
|
|
|
#include "lldb/lldb-private.h"
|
2015-03-31 21:03:22 +00:00
|
|
|
|
2017-03-21 17:26:55 +00:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
2015-03-31 21:03:22 +00:00
|
|
|
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-02 19:25:18 +00:00
|
|
|
#if defined(_WIN32)
|
2015-10-28 18:21:45 +00:00
|
|
|
#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
|
2016-03-02 22:05:52 +00:00
|
|
|
#include "lldb/Host/windows/windows.h"
|
2019-08-06 18:20:43 +00:00
|
|
|
#include <crtdbg.h>
|
2015-03-31 21:03:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
2018-12-03 17:28:29 +00:00
|
|
|
using namespace lldb_private::repro;
|
2015-03-31 21:03:22 +00:00
|
|
|
|
|
|
|
|
SystemInitializerCommon::SystemInitializerCommon() {}
|
|
|
|
|
|
|
|
|
|
SystemInitializerCommon::~SystemInitializerCommon() {}
|
|
|
|
|
|
2020-08-20 15:07:27 -07:00
|
|
|
/// Initialize the FileSystem based on the current reproducer mode.
|
|
|
|
|
static llvm::Error InitializeFileSystem() {
|
|
|
|
|
auto &r = repro::Reproducer::Instance();
|
|
|
|
|
if (repro::Loader *loader = r.GetLoader()) {
|
|
|
|
|
FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
|
|
|
|
|
if (vfs_mapping) {
|
|
|
|
|
if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
|
|
|
|
|
return e;
|
|
|
|
|
} else {
|
|
|
|
|
FileSystem::Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 16:19:17 -07:00
|
|
|
// Set the current working directory form the reproducer.
|
|
|
|
|
llvm::Expected<std::string> working_dir =
|
|
|
|
|
repro::GetDirectoryFrom<WorkingDirectoryProvider>(loader);
|
|
|
|
|
if (!working_dir)
|
|
|
|
|
return working_dir.takeError();
|
2020-08-20 15:07:27 -07:00
|
|
|
if (std::error_code ec = FileSystem::Instance()
|
|
|
|
|
.GetVirtualFileSystem()
|
2020-08-20 16:19:17 -07:00
|
|
|
->setCurrentWorkingDirectory(*working_dir)) {
|
2020-08-20 15:07:27 -07:00
|
|
|
return llvm::errorCodeToError(ec);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 16:19:17 -07:00
|
|
|
// Set the home directory from the reproducer.
|
|
|
|
|
llvm::Expected<std::string> home_dir =
|
|
|
|
|
repro::GetDirectoryFrom<HomeDirectoryProvider>(loader);
|
|
|
|
|
if (!home_dir)
|
|
|
|
|
return home_dir.takeError();
|
|
|
|
|
FileSystem::Instance().SetHomeDirectory(*home_dir);
|
|
|
|
|
|
2020-08-20 15:07:27 -07:00
|
|
|
return llvm::Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (repro::Generator *g = r.GetGenerator()) {
|
|
|
|
|
repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
|
|
|
|
|
vp.SetVersion(lldb_private::GetVersion());
|
|
|
|
|
|
|
|
|
|
repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
|
|
|
|
|
FileSystem::Initialize(fp.GetFileCollector());
|
|
|
|
|
|
|
|
|
|
repro::WorkingDirectoryProvider &wp =
|
|
|
|
|
g->GetOrCreate<repro::WorkingDirectoryProvider>();
|
2020-08-20 16:03:45 -07:00
|
|
|
fp.RecordInterestingDirectory(wp.GetDirectory());
|
2020-08-20 15:07:27 -07:00
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileSystem::Initialize();
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 22:26:16 +00:00
|
|
|
llvm::Error SystemInitializerCommon::Initialize() {
|
2019-05-02 19:25:18 +00:00
|
|
|
#if defined(_WIN32)
|
2015-03-31 21:03:22 +00:00
|
|
|
const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
|
|
|
|
|
if (disable_crash_dialog_var &&
|
|
|
|
|
llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
|
|
|
|
|
// This will prevent Windows from displaying a dialog box requiring user
|
|
|
|
|
// interaction when
|
|
|
|
|
// LLDB crashes. This is mostly useful when automating LLDB, for example
|
|
|
|
|
// via the test
|
|
|
|
|
// suite, so that a crash in LLDB does not prevent completion of the test
|
|
|
|
|
// suite.
|
|
|
|
|
::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
|
|
|
|
|
SEM_NOGPFAULTERRORBOX);
|
|
|
|
|
|
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
|
|
|
|
|
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
|
|
|
|
|
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
|
|
|
|
|
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
|
|
|
|
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
|
|
|
|
|
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-02-21 22:26:16 +00:00
|
|
|
// If the reproducer wasn't initialized before, we can safely assume it's
|
|
|
|
|
// off.
|
|
|
|
|
if (!Reproducer::Initialized()) {
|
|
|
|
|
if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
|
|
|
|
|
return e;
|
|
|
|
|
}
|
2018-12-03 17:28:29 +00:00
|
|
|
|
2020-08-20 15:07:27 -07:00
|
|
|
if (auto e = InitializeFileSystem())
|
|
|
|
|
return e;
|
2019-01-29 20:36:38 +00:00
|
|
|
|
2017-10-23 19:41:17 +00:00
|
|
|
Log::Initialize();
|
2015-03-31 21:03:22 +00:00
|
|
|
HostInfo::Initialize();
|
2019-04-10 04:57:18 +00:00
|
|
|
|
|
|
|
|
llvm::Error error = Socket::Initialize();
|
|
|
|
|
if (error)
|
|
|
|
|
return error;
|
|
|
|
|
|
2017-05-15 13:02:37 +00:00
|
|
|
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
|
|
|
|
|
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
|
2015-03-31 21:03:22 +00:00
|
|
|
|
|
|
|
|
process_gdb_remote::ProcessGDBRemoteLog::Initialize();
|
|
|
|
|
|
2017-03-21 17:26:55 +00:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
2017-02-23 10:33:16 +00:00
|
|
|
ProcessPOSIXLog::Initialize();
|
2015-03-31 21:03:22 +00:00
|
|
|
#endif
|
2019-05-02 19:25:18 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-10 16:18:08 +00:00
|
|
|
ProcessWindowsLog::Initialize();
|
|
|
|
|
#endif
|
2018-12-03 17:28:29 +00:00
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
2015-03-31 21:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SystemInitializerCommon::Terminate() {
|
2017-05-15 13:02:37 +00:00
|
|
|
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
|
|
|
|
|
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
|
2015-03-31 21:03:22 +00:00
|
|
|
|
2019-05-02 19:25:18 +00:00
|
|
|
#if defined(_WIN32)
|
2015-05-07 21:39:33 +00:00
|
|
|
ProcessWindowsLog::Terminate();
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-04-10 04:57:18 +00:00
|
|
|
Socket::Terminate();
|
2016-02-19 19:20:44 +00:00
|
|
|
HostInfo::Terminate();
|
2017-03-15 09:06:58 +00:00
|
|
|
Log::DisableAllLogChannels();
|
2018-10-31 21:49:27 +00:00
|
|
|
FileSystem::Terminate();
|
2018-12-03 17:28:29 +00:00
|
|
|
Reproducer::Terminate();
|
2015-03-31 21:03:22 +00:00
|
|
|
}
|