1 //===- JITLoaderGDB.h - Register objects via GDB JIT interface -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
11 #include "llvm/ExecutionEngine/JITSymbol.h"
12 #include "llvm/Support/BinaryStreamReader.h"
13 #include "llvm/Support/ManagedStatic.h"
19 #define DEBUG_TYPE "orc"
21 // First version as landed in August 2009
22 static constexpr uint32_t JitDescriptorVersion
= 1;
24 // Keep in sync with gdb/gdb/jit.h
33 struct jit_code_entry
{
34 struct jit_code_entry
*next_entry
;
35 struct jit_code_entry
*prev_entry
;
36 const char *symfile_addr
;
37 uint64_t symfile_size
;
40 struct jit_descriptor
{
42 // This should be jit_actions_t, but we want to be specific about the
45 struct jit_code_entry
*relevant_entry
;
46 struct jit_code_entry
*first_entry
;
49 // We put information about the JITed function in this global, which the
50 // debugger reads. Make sure to specify the version statically, because the
51 // debugger checks the version before we can set it during runtime.
52 struct jit_descriptor __jit_debug_descriptor
= {JitDescriptorVersion
, 0,
55 // Debuggers that implement the GDB JIT interface put a special breakpoint in
57 LLVM_ATTRIBUTE_NOINLINE
void __jit_debug_register_code() {
58 // The noinline and the asm prevent calls to this function from being
60 #if !defined(_MSC_VER)
61 asm volatile("" ::: "memory");
68 // Serialize rendezvous with the debugger as well as access to shared data.
69 ManagedStatic
<std::mutex
> JITDebugLock
;
71 // Register debug object, return error message or null for success.
72 static void registerJITLoaderGDBImpl(JITTargetAddress Addr
, uint64_t Size
) {
73 jit_code_entry
*E
= new jit_code_entry
;
74 E
->symfile_addr
= jitTargetAddressToPointer
<const char *>(Addr
);
75 E
->symfile_size
= Size
;
76 E
->prev_entry
= nullptr;
78 std::lock_guard
<std::mutex
> Lock(*JITDebugLock
);
80 // Insert this entry at the head of the list.
81 jit_code_entry
*NextEntry
= __jit_debug_descriptor
.first_entry
;
82 E
->next_entry
= NextEntry
;
84 NextEntry
->prev_entry
= E
;
87 __jit_debug_descriptor
.first_entry
= E
;
88 __jit_debug_descriptor
.relevant_entry
= E
;
90 // Run into the rendezvous breakpoint.
91 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
92 __jit_debug_register_code();
95 extern "C" orc::shared::detail::CWrapperFunctionResult
96 llvm_orc_registerJITLoaderGDBWrapper(const char *Data
, uint64_t Size
) {
97 using namespace orc::shared
;
98 return WrapperFunction
<void(SPSExecutorAddress
, uint64_t)>::handle(
99 Data
, Size
, registerJITLoaderGDBImpl
)