xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/EPCDebugObjectRegistrar.cpp (revision 1f4d91ecb8529678a3d3919d7523743bd21942ca)
1662c5544SLang Hames //===----- EPCDebugObjectRegistrar.cpp - EPC-based debug registration -----===//
2662c5544SLang Hames //
3662c5544SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4662c5544SLang Hames // See https://llvm.org/LICENSE.txt for license information.
5662c5544SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6662c5544SLang Hames //
7662c5544SLang Hames //===----------------------------------------------------------------------===//
8662c5544SLang Hames 
9662c5544SLang Hames #include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
10662c5544SLang Hames 
11662c5544SLang Hames #include "llvm/ExecutionEngine/Orc/Core.h"
12662c5544SLang Hames 
13662c5544SLang Hames namespace llvm {
14662c5544SLang Hames namespace orc {
15662c5544SLang Hames 
1679e3e65bSFangrui Song Expected<std::unique_ptr<EPCDebugObjectRegistrar>> createJITLoaderGDBRegistrar(
1779e3e65bSFangrui Song     ExecutionSession &ES,
1879e3e65bSFangrui Song     std::optional<ExecutorAddr> RegistrationFunctionDylib) {
192487db1fSLang Hames   auto &EPC = ES.getExecutorProcessControl();
20b3c9ced9SLang Hames 
21b3c9ced9SLang Hames   if (!RegistrationFunctionDylib) {
22*db21bd4fSLang Hames     if (auto D = EPC.getDylibMgr().loadDylib(nullptr))
23b3c9ced9SLang Hames       RegistrationFunctionDylib = *D;
24b3c9ced9SLang Hames     else
25b3c9ced9SLang Hames       return D.takeError();
26b3c9ced9SLang Hames   }
27662c5544SLang Hames 
28662c5544SLang Hames   SymbolStringPtr RegisterFn =
29662c5544SLang Hames       EPC.getTargetTriple().isOSBinFormatMachO()
30662c5544SLang Hames           ? EPC.intern("_llvm_orc_registerJITLoaderGDBWrapper")
31662c5544SLang Hames           : EPC.intern("llvm_orc_registerJITLoaderGDBWrapper");
32662c5544SLang Hames 
33662c5544SLang Hames   SymbolLookupSet RegistrationSymbols;
34662c5544SLang Hames   RegistrationSymbols.add(RegisterFn);
35662c5544SLang Hames 
36*db21bd4fSLang Hames   auto Result = EPC.getDylibMgr().lookupSymbols(
37*db21bd4fSLang Hames       {{*RegistrationFunctionDylib, RegistrationSymbols}});
38662c5544SLang Hames   if (!Result)
39662c5544SLang Hames     return Result.takeError();
40662c5544SLang Hames 
41662c5544SLang Hames   assert(Result->size() == 1 && "Unexpected number of dylibs in result");
42662c5544SLang Hames   assert((*Result)[0].size() == 1 &&
43662c5544SLang Hames          "Unexpected number of addresses in result");
44662c5544SLang Hames 
4540b4ac27SBen Langmuir   ExecutorAddr RegisterAddr = (*Result)[0][0].getAddress();
4640b4ac27SBen Langmuir   return std::make_unique<EPCDebugObjectRegistrar>(ES, RegisterAddr);
472487db1fSLang Hames }
482487db1fSLang Hames 
494c7f53b9SStefan Gränitz Error EPCDebugObjectRegistrar::registerDebugObject(ExecutorAddrRange TargetMem,
504c7f53b9SStefan Gränitz                                                    bool AutoRegisterCode) {
514c7f53b9SStefan Gränitz   return ES.callSPSWrapper<void(shared::SPSExecutorAddrRange, bool)>(
524c7f53b9SStefan Gränitz       RegisterFn, TargetMem, AutoRegisterCode);
53662c5544SLang Hames }
54662c5544SLang Hames 
55662c5544SLang Hames } // namespace orc
56662c5544SLang Hames } // namespace llvm
57