blob: 173d48308c6f9aab61d6d433066cb7f6d0f56618 [file] [log] [blame]
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001//===-- SystemInitializerCommon.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turnere6e2bb32015-03-31 21:03:22 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Initialization/SystemInitializerCommon.h"
10
Tamas Berghammerd8c338d2015-04-15 09:47:02 +000011#include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +000012#include "Plugins/Instruction/MIPS/EmulateInstructionMIPS.h"
Mohit K. Bhakkadcdc22a82015-05-07 05:56:27 +000013#include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000014#include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
15#include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000016#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
Jonas Devlieghere46376962018-10-31 21:49:27 +000017#include "lldb/Host/FileSystem.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Host/Host.h"
19#include "lldb/Host/HostInfo.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "lldb/Utility/Log.h"
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000021#include "lldb/Utility/Reproducer.h"
Pavel Labath38d06322017-06-29 14:32:17 +000022#include "lldb/Utility/Timer.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000023
Kamil Rytarowskia87101d2017-03-21 17:26:55 +000024#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Zachary Turnere6e2bb32015-03-31 21:03:22 +000025#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
26#endif
27
28#if defined(_MSC_VER)
Adrian McCarthy18a9135d2015-10-28 18:21:45 +000029#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000030#include "lldb/Host/windows/windows.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000031#endif
32
33#include "llvm/Support/TargetSelect.h"
34
35#include <string>
36
37using namespace lldb_private;
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000038using namespace lldb_private::repro;
Zachary Turnere6e2bb32015-03-31 21:03:22 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SystemInitializerCommon::SystemInitializerCommon() {}
Zachary Turnere6e2bb32015-03-31 21:03:22 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042SystemInitializerCommon::~SystemInitializerCommon() {}
Zachary Turnere6e2bb32015-03-31 21:03:22 +000043
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000044llvm::Error
45SystemInitializerCommon::Initialize(const InitializerOptions &options) {
Zachary Turnere6e2bb32015-03-31 21:03:22 +000046#if defined(_MSC_VER)
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
48 if (disable_crash_dialog_var &&
49 llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
50 // This will prevent Windows from displaying a dialog box requiring user
51 // interaction when
52 // LLDB crashes. This is mostly useful when automating LLDB, for example
53 // via the test
54 // suite, so that a crash in LLDB does not prevent completion of the test
55 // suite.
56 ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
57 SEM_NOGPFAULTERRORBOX);
Zachary Turnere6e2bb32015-03-31 21:03:22 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
60 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
61 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
62 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
63 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
64 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
65 }
Zachary Turnere6e2bb32015-03-31 21:03:22 +000066#endif
67
Jonas Devlieghere46575172019-01-29 20:36:38 +000068 // Initialize the reproducer.
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000069 ReproducerMode mode = ReproducerMode::Off;
70 if (options.reproducer_capture)
71 mode = ReproducerMode::Capture;
72 if (options.reproducer_replay)
73 mode = ReproducerMode::Replay;
74
75 if (auto e = Reproducer::Initialize(mode, FileSpec(options.reproducer_path)))
76 return e;
77
Jonas Devlieghere46575172019-01-29 20:36:38 +000078 // Initialize the file system.
79 auto &r = repro::Reproducer::Instance();
80 if (repro::Loader *loader = r.GetLoader()) {
81 FileSpec vfs_mapping = loader->GetFile<FileInfo>();
82 if (vfs_mapping) {
83 if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
84 return e;
85 } else {
86 FileSystem::Initialize();
87 }
88 } else if (repro::Generator *g = r.GetGenerator()) {
89 repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
90 FileSystem::Initialize(fp.GetFileCollector());
91 } else {
92 FileSystem::Initialize();
93 }
94
Pavel Labathd8133092017-10-23 19:41:17 +000095 Log::Initialize();
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 HostInfo::Initialize();
Pavel Labathf9d16472017-05-15 13:02:37 +000097 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
98 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
Zachary Turnere6e2bb32015-03-31 21:03:22 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 process_gdb_remote::ProcessGDBRemoteLog::Initialize();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 // Initialize plug-ins
103 ObjectContainerBSDArchive::Initialize();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 EmulateInstructionARM::Initialize();
106 EmulateInstructionMIPS::Initialize();
107 EmulateInstructionMIPS64::Initialize();
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 //----------------------------------------------------------------------
110 // Apple/Darwin hosted plugins
111 //----------------------------------------------------------------------
112 ObjectContainerUniversalMachO::Initialize();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000113
Kamil Rytarowskia87101d2017-03-21 17:26:55 +0000114#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Pavel Labathc29f7ff2017-02-23 10:33:16 +0000115 ProcessPOSIXLog::Initialize();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000116#endif
Adrian McCarthy42b33802015-04-10 16:18:08 +0000117#if defined(_MSC_VER)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 ProcessWindowsLog::Initialize();
Adrian McCarthy42b33802015-04-10 16:18:08 +0000119#endif
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000120
121 return llvm::Error::success();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void SystemInitializerCommon::Terminate() {
Pavel Labathf9d16472017-05-15 13:02:37 +0000125 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
126 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 ObjectContainerBSDArchive::Terminate();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 EmulateInstructionARM::Terminate();
130 EmulateInstructionMIPS::Terminate();
131 EmulateInstructionMIPS64::Terminate();
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 ObjectContainerUniversalMachO::Terminate();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000134
Hafiz Abid Qadeer23a4df22015-09-04 16:34:19 +0000135#if defined(_MSC_VER)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 ProcessWindowsLog::Terminate();
Zachary Turner610e5292015-05-07 21:39:33 +0000137#endif
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 HostInfo::Terminate();
Pavel Labath775588c2017-03-15 09:06:58 +0000140 Log::DisableAllLogChannels();
Jonas Devlieghere46376962018-10-31 21:49:27 +0000141 FileSystem::Terminate();
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000142 Reproducer::Terminate();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000143}