180814287SRaphael Isemann //===-- SystemInitializerFull.cpp -----------------------------------------===//
2e6e2bb38SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6e2bb38SZachary Turner //
7e6e2bb38SZachary Turner //===----------------------------------------------------------------------===//
8e6e2bb38SZachary Turner 
9d17cd902SAlex Langford #include "SystemInitializerFull.h"
102c1f46dcSZachary Turner #include "lldb/API/SBCommandInterpreter.h"
11e6e2bb38SZachary Turner #include "lldb/Core/Debugger.h"
12fbb4d1e4SJonas Devlieghere #include "lldb/Core/PluginManager.h"
13fbb4d1e4SJonas Devlieghere #include "lldb/Host/Config.h"
14e6e2bb38SZachary Turner #include "lldb/Host/Host.h"
15e6e2bb38SZachary Turner #include "lldb/Initialization/SystemInitializerCommon.h"
162c1f46dcSZachary Turner #include "lldb/Interpreter/CommandInterpreter.h"
178a203bb2SWalter Erquinigo #include "lldb/Target/ProcessTrace.h"
18b31d7879SJonas Devlieghere #include "lldb/Utility/Reproducer.h"
1938d0632eSPavel Labath #include "lldb/Utility/Timer.h"
20e6e2bb38SZachary Turner #include "llvm/Support/TargetSelect.h"
21e6e2bb38SZachary Turner 
225a36558cSSaleem Abdulrasool #pragma clang diagnostic push
235a36558cSSaleem Abdulrasool #pragma clang diagnostic ignored "-Wglobal-constructors"
245a36558cSSaleem Abdulrasool #include "llvm/ExecutionEngine/MCJIT.h"
255a36558cSSaleem Abdulrasool #pragma clang diagnostic pop
265a36558cSSaleem Abdulrasool 
27e6e2bb38SZachary Turner #include <string>
28e6e2bb38SZachary Turner 
292d146aa2SJonas Devlieghere #define LLDB_PLUGIN(p) LLDB_PLUGIN_DECLARE(p)
302d146aa2SJonas Devlieghere #include "Plugins/Plugins.def"
313e70a919SJonas Devlieghere 
32e6e2bb38SZachary Turner using namespace lldb_private;
33e6e2bb38SZachary Turner 
34866b7a65SJonas Devlieghere SystemInitializerFull::SystemInitializerFull() = default;
35866b7a65SJonas Devlieghere SystemInitializerFull::~SystemInitializerFull() = default;
36e6e2bb38SZachary Turner 
37936c6242SJonas Devlieghere llvm::Error SystemInitializerFull::Initialize() {
38b31d7879SJonas Devlieghere   llvm::Error error = SystemInitializerCommon::Initialize();
39b31d7879SJonas Devlieghere   if (error) {
40b31d7879SJonas Devlieghere     // During active replay, the ::Initialize call is replayed like any other
41b31d7879SJonas Devlieghere     // SB API call and the return value is ignored. Since we can't intercept
42b31d7879SJonas Devlieghere     // this, we terminate here before the uninitialized debugger inevitably
43b31d7879SJonas Devlieghere     // crashes.
44b31d7879SJonas Devlieghere     if (repro::Reproducer::Instance().IsReplaying())
45*8da14fb7SJonas Devlieghere       llvm::report_fatal_error(std::move(error));
46b31d7879SJonas Devlieghere     return error;
47b31d7879SJonas Devlieghere   }
48fa3fa5b9SPavel Labath 
49e6e2bb38SZachary Turner   // Initialize LLVM and Clang
50e6e2bb38SZachary Turner   llvm::InitializeAllTargets();
51e6e2bb38SZachary Turner   llvm::InitializeAllAsmPrinters();
52e6e2bb38SZachary Turner   llvm::InitializeAllTargetMCs();
53e6e2bb38SZachary Turner   llvm::InitializeAllDisassemblers();
54e6e2bb38SZachary Turner 
5580c3ea4eSJonas Devlieghere #define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
5680c3ea4eSJonas Devlieghere #include "Plugins/Plugins.def"
5750c9cd95SJonas Devlieghere 
588a203bb2SWalter Erquinigo   // Initialize plug-ins in core LLDB
598a203bb2SWalter Erquinigo   ProcessTrace::Initialize();
608a203bb2SWalter Erquinigo 
6150c9cd95SJonas Devlieghere   // Scan for any system or user LLDB plug-ins
62e6e2bb38SZachary Turner   PluginManager::Initialize();
63e6e2bb38SZachary Turner 
6405097246SAdrian Prantl   // The process settings need to know about installed plug-ins, so the
6580c3ea4eSJonas Devlieghere   // Settings must be initialized AFTER PluginManager::Initialize is called.
66e6e2bb38SZachary Turner   Debugger::SettingsInitialize();
6715eacd74SJonas Devlieghere 
6815eacd74SJonas Devlieghere   return llvm::Error::success();
69e6e2bb38SZachary Turner }
70e6e2bb38SZachary Turner 
71b9c1b51eSKate Stone void SystemInitializerFull::Terminate() {
72f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
73f9d16476SPavel Labath   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
74e6e2bb38SZachary Turner 
75e6e2bb38SZachary Turner   Debugger::SettingsTerminate();
76e6e2bb38SZachary Turner 
778a203bb2SWalter Erquinigo   // Terminate plug-ins in core LLDB
788a203bb2SWalter Erquinigo   ProcessTrace::Terminate();
798a203bb2SWalter Erquinigo 
8050c9cd95SJonas Devlieghere   // Terminate and unload and loaded system or user LLDB plug-ins
81e6e2bb38SZachary Turner   PluginManager::Terminate();
8256939cb3SGreg Clayton 
8380c3ea4eSJonas Devlieghere #define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
8480c3ea4eSJonas Devlieghere #include "Plugins/Plugins.def"
85fbb4d1e4SJonas Devlieghere 
86e6e2bb38SZachary Turner   // Now shutdown the common parts, in reverse order.
87e6e2bb38SZachary Turner   SystemInitializerCommon::Terminate();
88e6e2bb38SZachary Turner }
89