xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp (revision 54397f9ac128568838f2ac7bfc8e1f94b3eb264d)
1b2518971SLang Hames //===------ DebuggerSupport.cpp - Utils for enabling debugger support -----===//
2b2518971SLang Hames //
3b2518971SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b2518971SLang Hames // See https://llvm.org/LICENSE.txt for license information.
5b2518971SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b2518971SLang Hames //
7b2518971SLang Hames //===----------------------------------------------------------------------===//
8b2518971SLang Hames 
9b2518971SLang Hames #include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
10b2518971SLang Hames #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
11b2518971SLang Hames #include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
12b2518971SLang Hames #include "llvm/ExecutionEngine/Orc/LLJIT.h"
13b2518971SLang Hames 
14b2518971SLang Hames #define DEBUG_TYPE "orc"
15b2518971SLang Hames 
16b2518971SLang Hames using namespace llvm;
17b2518971SLang Hames using namespace llvm::orc;
18b2518971SLang Hames 
19b2518971SLang Hames namespace llvm::orc {
20b2518971SLang Hames 
enableDebuggerSupport(LLJIT & J)21b2518971SLang Hames Error enableDebuggerSupport(LLJIT &J) {
22b2518971SLang Hames   auto *ObjLinkingLayer = dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
23b2518971SLang Hames   if (!ObjLinkingLayer)
24b2518971SLang Hames     return make_error<StringError>("Cannot enable LLJIT debugger support: "
25b2518971SLang Hames                                    "Debugger support requires JITLink",
26b2518971SLang Hames                                    inconvertibleErrorCode());
27b2518971SLang Hames   auto ProcessSymsJD = J.getProcessSymbolsJITDylib();
28b2518971SLang Hames   if (!ProcessSymsJD)
29b2518971SLang Hames     return make_error<StringError>("Cannot enable LLJIT debugger support: "
30b2518971SLang Hames                                    "Process symbols are not available",
31b2518971SLang Hames                                    inconvertibleErrorCode());
32b2518971SLang Hames 
33b2518971SLang Hames   auto &ES = J.getExecutionSession();
34b2518971SLang Hames   const auto &TT = J.getTargetTriple();
35b2518971SLang Hames 
36b2518971SLang Hames   switch (TT.getObjectFormat()) {
37b2518971SLang Hames   case Triple::ELF: {
38b2518971SLang Hames     auto Registrar = createJITLoaderGDBRegistrar(ES);
39b2518971SLang Hames     if (!Registrar)
40b2518971SLang Hames       return Registrar.takeError();
41b2518971SLang Hames     ObjLinkingLayer->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
42*54397f9aSStefan Gränitz         ES, std::move(*Registrar), false, true));
43b2518971SLang Hames     return Error::success();
44b2518971SLang Hames   }
45b2518971SLang Hames   case Triple::MachO: {
46b2518971SLang Hames     auto DS = GDBJITDebugInfoRegistrationPlugin::Create(ES, *ProcessSymsJD, TT);
47b2518971SLang Hames     if (!DS)
48b2518971SLang Hames       return DS.takeError();
49b2518971SLang Hames     ObjLinkingLayer->addPlugin(std::move(*DS));
50b2518971SLang Hames     return Error::success();
51b2518971SLang Hames   }
52b2518971SLang Hames   default:
53b2518971SLang Hames     return make_error<StringError>(
54b2518971SLang Hames         "Cannot enable LLJIT debugger support: " +
55b2518971SLang Hames             Triple::getObjectFormatTypeName(TT.getObjectFormat()) +
56b2518971SLang Hames             " is not supported",
57b2518971SLang Hames         inconvertibleErrorCode());
58b2518971SLang Hames   }
59b2518971SLang Hames }
60b2518971SLang Hames 
61b2518971SLang Hames } // namespace llvm::orc
62