1 //===----- TPCDebugObjectRegistrar.cpp - TPC-based debug registration -----===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ExecutionEngine/Orc/TPCDebugObjectRegistrar.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Core.h"
12 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
13 #include "llvm/Support/BinaryStreamWriter.h"
14 
15 namespace llvm {
16 namespace orc {
17 
18 // Counterpart for readDebugObjectInfo() in TargetProcess/JITLoaderGDB.cpp
19 static std::vector<uint8_t>
writeDebugObjectInfo(sys::MemoryBlock TargetMemBlock)20 writeDebugObjectInfo(sys::MemoryBlock TargetMemBlock) {
21   auto DebugObjAddr = pointerToJITTargetAddress(TargetMemBlock.base());
22   uint64_t DebugObjSize = TargetMemBlock.allocatedSize();
23 
24   std::vector<uint8_t> ArgBuffer;
25   ArgBuffer.resize(sizeof(decltype(DebugObjAddr)) +
26                    sizeof(decltype(DebugObjSize)));
27 
28   BinaryStreamWriter ArgWriter(ArgBuffer, support::endianness::big);
29   cantFail(ArgWriter.writeInteger(DebugObjAddr));
30   cantFail(ArgWriter.writeInteger(DebugObjSize));
31 
32   return ArgBuffer;
33 }
34 
35 Expected<std::unique_ptr<TPCDebugObjectRegistrar>>
createJITLoaderGDBRegistrar(TargetProcessControl & TPC)36 createJITLoaderGDBRegistrar(TargetProcessControl &TPC) {
37   auto ProcessHandle = TPC.loadDylib(nullptr);
38   if (!ProcessHandle)
39     return ProcessHandle.takeError();
40 
41   SymbolStringPtr RegisterFn =
42       TPC.getTargetTriple().isOSBinFormatMachO()
43           ? TPC.intern("_llvm_orc_registerJITLoaderGDBWrapper")
44           : TPC.intern("llvm_orc_registerJITLoaderGDBWrapper");
45 
46   SymbolLookupSet RegistrationSymbols;
47   RegistrationSymbols.add(RegisterFn);
48 
49   auto Result = TPC.lookupSymbols({{*ProcessHandle, RegistrationSymbols}});
50   if (!Result)
51     return Result.takeError();
52 
53   assert(Result->size() == 1 && "Unexpected number of dylibs in result");
54   assert((*Result)[0].size() == 1 &&
55          "Unexpected number of addresses in result");
56 
57   return std::make_unique<TPCDebugObjectRegistrar>(TPC, (*Result)[0][0],
58                                                    &writeDebugObjectInfo);
59 }
60 
61 } // namespace orc
62 } // namespace llvm
63