Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.cpp
35323 views
//===- JITLoaderGDB.h - Register objects via GDB JIT interface -*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"910#include "llvm/ExecutionEngine/JITSymbol.h"11#include "llvm/Support/BinaryStreamReader.h"12#include "llvm/Support/FormatVariadic.h"1314#include <cstdint>15#include <mutex>16#include <utility>1718#define DEBUG_TYPE "orc"1920// First version as landed in August 200921static constexpr uint32_t JitDescriptorVersion = 1;2223extern "C" {2425// We put information about the JITed function in this global, which the26// debugger reads. Make sure to specify the version statically, because the27// debugger checks the version before we can set it during runtime.28LLVM_ATTRIBUTE_VISIBILITY_DEFAULT29struct jit_descriptor __jit_debug_descriptor = {JitDescriptorVersion, 0,30nullptr, nullptr};3132// Debuggers that implement the GDB JIT interface put a special breakpoint in33// this function.34LLVM_ATTRIBUTE_VISIBILITY_DEFAULT35LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {36// The noinline and the asm prevent calls to this function from being37// optimized out.38#if !defined(_MSC_VER)39asm volatile("" ::: "memory");40#endif41}42}4344using namespace llvm;45using namespace llvm::orc;4647// Register debug object, return error message or null for success.48static void appendJITDebugDescriptor(const char *ObjAddr, size_t Size) {49LLVM_DEBUG({50dbgs() << "Adding debug object to GDB JIT interface "51<< formatv("([{0:x16} -- {1:x16}])",52reinterpret_cast<uintptr_t>(ObjAddr),53reinterpret_cast<uintptr_t>(ObjAddr + Size))54<< "\n";55});5657jit_code_entry *E = new jit_code_entry;58E->symfile_addr = ObjAddr;59E->symfile_size = Size;60E->prev_entry = nullptr;6162// Serialize rendezvous with the debugger as well as access to shared data.63static std::mutex JITDebugLock;64std::lock_guard<std::mutex> Lock(JITDebugLock);6566// Insert this entry at the head of the list.67jit_code_entry *NextEntry = __jit_debug_descriptor.first_entry;68E->next_entry = NextEntry;69if (NextEntry) {70NextEntry->prev_entry = E;71}7273__jit_debug_descriptor.first_entry = E;74__jit_debug_descriptor.relevant_entry = E;75__jit_debug_descriptor.action_flag = JIT_REGISTER_FN;76}7778extern "C" orc::shared::CWrapperFunctionResult79llvm_orc_registerJITLoaderGDBAllocAction(const char *Data, size_t Size) {80using namespace orc::shared;81return WrapperFunction<SPSError(SPSExecutorAddrRange, bool)>::handle(82Data, Size,83[](ExecutorAddrRange R, bool AutoRegisterCode) {84appendJITDebugDescriptor(R.Start.toPtr<const char *>(),85R.size());86// Run into the rendezvous breakpoint.87if (AutoRegisterCode)88__jit_debug_register_code();89return Error::success();90})91.release();92}9394extern "C" orc::shared::CWrapperFunctionResult95llvm_orc_registerJITLoaderGDBWrapper(const char *Data, uint64_t Size) {96using namespace orc::shared;97return WrapperFunction<SPSError(SPSExecutorAddrRange, bool)>::handle(98Data, Size,99[](ExecutorAddrRange R, bool AutoRegisterCode) {100appendJITDebugDescriptor(R.Start.toPtr<const char *>(),101R.size());102// Run into the rendezvous breakpoint.103if (AutoRegisterCode)104__jit_debug_register_code();105return Error::success();106})107.release();108}109110111