llvm-project/compiler-rt/lib/xray/xray_dso_init.cpp
Sebastian Kreutzer e738a5d8e3
Reapply " [XRay] Add support for instrumentation of DSOs on x86_64 (#90959)" (#113548)
This fixes remaining issues in my previous PR #90959.

Changes:
- Removed dependency on LLVM header in `xray_interface.cpp`
- Fixed XRay patching for some targets due to missing changes in
architecture-specific patching functions
- Addressed some remaining compiler warnings that I missed in the
previous patch
- Formatting

I have tested these changes on `x86_64` (natively), as well as
`ppc64le`, `aarch64` and `arm32` (cross-compiled and emulated using
qemu).

**Original description:**

This PR introduces shared library (DSO) support for XRay based on a
revised version of the implementation outlined in [this
RFC](https://discourse.llvm.org/t/rfc-upstreaming-dso-instrumentation-support-for-xray/73000).
The feature enables the patching and handling of events from DSOs,
supporting both libraries linked at startup or explicitly loaded, e.g.
via `dlopen`.
This patch adds the following:
- The `-fxray-shared` flag to enable the feature (turned off by default)
- A small runtime library that is linked into every instrumented DSO,
providing position-independent trampolines and code to register with the
main XRay runtime
- Changes to the XRay runtime to support management and patching of
multiple objects

These changes are fully backward compatible, i.e. running without
instrumented DSOs will produce identical traces (in terms of recorded
function IDs) to the previous implementation.

Due to my limited ability to test on other architectures, this feature
is only implemented and tested with x86_64. Extending support to other
architectures is fairly straightforward, requiring only a
position-independent implementation of the architecture-specific
trampoline implementation (see
`compiler-rt/lib/xray/xray_trampoline_x86_64.S` for reference).

This patch does not include any functionality to resolve function IDs
from DSOs for the provided logging/tracing modes. These modes still work
and will record calls from DSOs, but symbol resolution for these
functions in not available. Getting this to work properly requires
recording information about the loaded DSOs and should IMO be discussed
in a separate RFC, as there are mulitple feasible approaches.

---------

Co-authored-by: Sebastian Kreutzer <sebastian.kreutzer@tu-darmstadt.de>
2024-10-25 10:15:25 +02:00

63 lines
2.4 KiB
C++

//===-- xray_init.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a dynamic runtime instrumentation system.
//
// XRay initialisation logic for DSOs.
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_atomic.h"
#include "xray_defs.h"
#include "xray_flags.h"
#include "xray_interface_internal.h"
using namespace __sanitizer;
extern "C" {
extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak))
__attribute__((visibility("hidden")));
extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak))
__attribute__((visibility("hidden")));
extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak))
__attribute__((visibility("hidden")));
extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak))
__attribute__((visibility("hidden")));
#if SANITIZER_APPLE
// HACK: This is a temporary workaround to make XRay build on
// Darwin, but it will probably not work at runtime.
extern const XRaySledEntry __start_xray_instr_map[] = {};
extern const XRaySledEntry __stop_xray_instr_map[] = {};
extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
#endif
}
// Handler functions to call in the patched entry/exit sled.
extern atomic_uintptr_t XRayPatchedFunction;
extern atomic_uintptr_t XRayArgLogger;
extern atomic_uintptr_t XRayPatchedCustomEvent;
extern atomic_uintptr_t XRayPatchedTypedEvent;
static int __xray_object_id{-1};
// Note: .preinit_array initialization does not work for DSOs
__attribute__((constructor(0))) static void
__xray_init_dso() XRAY_NEVER_INSTRUMENT {
// Register sleds in main XRay runtime.
__xray_object_id =
__xray_register_dso(__start_xray_instr_map, __stop_xray_instr_map,
__start_xray_fn_idx, __stop_xray_fn_idx, {});
}
__attribute__((destructor(0))) static void
__xray_finalize_dso() XRAY_NEVER_INSTRUMENT {
// Inform the main runtime that this DSO is no longer used.
__xray_deregister_dso(__xray_object_id);
}