1 //===- JITLoaderGDB.h - Register objects via GDB JIT interface -*- C++ -*-===// 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/TargetProcess/JITLoaderGDB.h" 10 11 #include "llvm/ExecutionEngine/JITSymbol.h" 12 #include "llvm/Support/BinaryStreamReader.h" 13 #include "llvm/Support/FormatVariadic.h" 14 #include "llvm/Support/ManagedStatic.h" 15 16 #include <cstdint> 17 #include <mutex> 18 #include <utility> 19 20 #define DEBUG_TYPE "orc" 21 22 // First version as landed in August 2009 23 static constexpr uint32_t JitDescriptorVersion = 1; 24 25 // Keep in sync with gdb/gdb/jit.h 26 extern "C" { 27 28 typedef enum { 29 JIT_NOACTION = 0, 30 JIT_REGISTER_FN, 31 JIT_UNREGISTER_FN 32 } jit_actions_t; 33 34 struct jit_code_entry { 35 struct jit_code_entry *next_entry; 36 struct jit_code_entry *prev_entry; 37 const char *symfile_addr; 38 uint64_t symfile_size; 39 }; 40 41 struct jit_descriptor { 42 uint32_t version; 43 // This should be jit_actions_t, but we want to be specific about the 44 // bit-width. 45 uint32_t action_flag; 46 struct jit_code_entry *relevant_entry; 47 struct jit_code_entry *first_entry; 48 }; 49 50 // We put information about the JITed function in this global, which the 51 // debugger reads. Make sure to specify the version statically, because the 52 // debugger checks the version before we can set it during runtime. 53 struct jit_descriptor __jit_debug_descriptor = {JitDescriptorVersion, 0, 54 nullptr, nullptr}; 55 56 // Debuggers that implement the GDB JIT interface put a special breakpoint in 57 // this function. 58 LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() { 59 // The noinline and the asm prevent calls to this function from being 60 // optimized out. 61 #if !defined(_MSC_VER) 62 asm volatile("" ::: "memory"); 63 #endif 64 } 65 } 66 67 using namespace llvm; 68 using namespace llvm::orc; 69 70 // Serialize rendezvous with the debugger as well as access to shared data. 71 ManagedStatic<std::mutex> JITDebugLock; 72 73 // Register debug object, return error message or null for success. 74 static void registerJITLoaderGDBImpl(const char *ObjAddr, size_t Size) { 75 LLVM_DEBUG({ 76 dbgs() << "Registering debug object with GDB JIT interface " 77 << formatv("([{0:x16} -- {1:x16}])", 78 reinterpret_cast<uintptr_t>(ObjAddr), 79 reinterpret_cast<uintptr_t>(ObjAddr + Size)) 80 << "\n"; 81 }); 82 83 jit_code_entry *E = new jit_code_entry; 84 E->symfile_addr = ObjAddr; 85 E->symfile_size = Size; 86 E->prev_entry = nullptr; 87 88 std::lock_guard<std::mutex> Lock(*JITDebugLock); 89 90 // Insert this entry at the head of the list. 91 jit_code_entry *NextEntry = __jit_debug_descriptor.first_entry; 92 E->next_entry = NextEntry; 93 if (NextEntry) { 94 NextEntry->prev_entry = E; 95 } 96 97 __jit_debug_descriptor.first_entry = E; 98 __jit_debug_descriptor.relevant_entry = E; 99 100 // Run into the rendezvous breakpoint. 101 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; 102 __jit_debug_register_code(); 103 } 104 105 extern "C" orc::shared::CWrapperFunctionResult 106 llvm_orc_registerJITLoaderGDBAllocAction(const char *Data, size_t Size) { 107 using namespace orc::shared; 108 return WrapperFunction<SPSError(SPSExecutorAddrRange)>::handle( 109 Data, Size, 110 [](ExecutorAddrRange R) { 111 registerJITLoaderGDBImpl(R.Start.toPtr<const char *>(), 112 R.size()); 113 return Error::success(); 114 }) 115 .release(); 116 } 117 118 extern "C" orc::shared::CWrapperFunctionResult 119 llvm_orc_registerJITLoaderGDBWrapper(const char *Data, uint64_t Size) { 120 using namespace orc::shared; 121 return WrapperFunction<SPSError(SPSExecutorAddrRange)>::handle( 122 Data, Size, 123 [](ExecutorAddrRange R) { 124 registerJITLoaderGDBImpl(R.Start.toPtr<const char *>(), 125 R.size()); 126 return Error::success(); 127 }) 128 .release(); 129 } 130