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"
122487db1fSLang Hames #include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
13662c5544SLang Hames #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
14662c5544SLang Hames #include "llvm/Support/BinaryStreamWriter.h"
15662c5544SLang Hames 
16662c5544SLang Hames namespace llvm {
17662c5544SLang Hames namespace orc {
18662c5544SLang Hames 
19662c5544SLang Hames Expected<std::unique_ptr<EPCDebugObjectRegistrar>>
createJITLoaderGDBRegistrar(ExecutionSession & ES)202487db1fSLang Hames createJITLoaderGDBRegistrar(ExecutionSession &ES) {
212487db1fSLang Hames   auto &EPC = ES.getExecutorProcessControl();
22662c5544SLang Hames   auto ProcessHandle = EPC.loadDylib(nullptr);
23662c5544SLang Hames   if (!ProcessHandle)
24662c5544SLang Hames     return ProcessHandle.takeError();
25662c5544SLang Hames 
26662c5544SLang Hames   SymbolStringPtr RegisterFn =
27662c5544SLang Hames       EPC.getTargetTriple().isOSBinFormatMachO()
28662c5544SLang Hames           ? EPC.intern("_llvm_orc_registerJITLoaderGDBWrapper")
29662c5544SLang Hames           : EPC.intern("llvm_orc_registerJITLoaderGDBWrapper");
30662c5544SLang Hames 
31662c5544SLang Hames   SymbolLookupSet RegistrationSymbols;
32662c5544SLang Hames   RegistrationSymbols.add(RegisterFn);
33662c5544SLang Hames 
34662c5544SLang Hames   auto Result = EPC.lookupSymbols({{*ProcessHandle, RegistrationSymbols}});
35662c5544SLang Hames   if (!Result)
36662c5544SLang Hames     return Result.takeError();
37662c5544SLang Hames 
38662c5544SLang Hames   assert(Result->size() == 1 && "Unexpected number of dylibs in result");
39662c5544SLang Hames   assert((*Result)[0].size() == 1 &&
40662c5544SLang Hames          "Unexpected number of addresses in result");
41662c5544SLang Hames 
4221a06254SLang Hames   return std::make_unique<EPCDebugObjectRegistrar>(
4321a06254SLang Hames       ES, ExecutorAddr((*Result)[0][0]));
442487db1fSLang Hames }
452487db1fSLang Hames 
registerDebugObject(ExecutorAddrRange TargetMem)46962a2479SLang Hames Error EPCDebugObjectRegistrar::registerDebugObject(
47962a2479SLang Hames     ExecutorAddrRange TargetMem) {
48*ec6d6210SPavel Labath   return ES.callSPSWrapper<void(shared::SPSExecutorAddrRange)>(RegisterFn,
49*ec6d6210SPavel Labath                                                                TargetMem);
50662c5544SLang Hames }
51662c5544SLang Hames 
52662c5544SLang Hames } // namespace orc
53662c5544SLang Hames } // namespace llvm
54