xref: /openbsd-src/gnu/llvm/compiler-rt/lib/xray/xray_interface_internal.h (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- xray_interface_internal.h -------------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of XRay, a dynamic runtime instrumentation system.
103cab2bb3Spatrick //
113cab2bb3Spatrick // Implementation of the API functions. See also include/xray/xray_interface.h.
123cab2bb3Spatrick //
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick #ifndef XRAY_INTERFACE_INTERNAL_H
153cab2bb3Spatrick #define XRAY_INTERFACE_INTERNAL_H
163cab2bb3Spatrick 
173cab2bb3Spatrick #include "sanitizer_common/sanitizer_platform.h"
183cab2bb3Spatrick #include "xray/xray_interface.h"
193cab2bb3Spatrick #include <cstddef>
203cab2bb3Spatrick #include <cstdint>
213cab2bb3Spatrick 
223cab2bb3Spatrick extern "C" {
233cab2bb3Spatrick 
243cab2bb3Spatrick struct XRaySledEntry {
253cab2bb3Spatrick #if SANITIZER_WORDSIZE == 64
263cab2bb3Spatrick   uint64_t Address;
273cab2bb3Spatrick   uint64_t Function;
283cab2bb3Spatrick   unsigned char Kind;
293cab2bb3Spatrick   unsigned char AlwaysInstrument;
303cab2bb3Spatrick   unsigned char Version;
313cab2bb3Spatrick   unsigned char Padding[13]; // Need 32 bytes
functionXRaySledEntry32*1f9cb04fSpatrick   uint64_t function() const {
33*1f9cb04fSpatrick     // The target address is relative to the location of the Function variable.
34*1f9cb04fSpatrick     return reinterpret_cast<uint64_t>(&Function) + Function;
35*1f9cb04fSpatrick   }
addressXRaySledEntry36*1f9cb04fSpatrick   uint64_t address() const {
37*1f9cb04fSpatrick     // The target address is relative to the location of the Address variable.
38*1f9cb04fSpatrick     return reinterpret_cast<uint64_t>(&Address) + Address;
39*1f9cb04fSpatrick   }
403cab2bb3Spatrick #elif SANITIZER_WORDSIZE == 32
413cab2bb3Spatrick   uint32_t Address;
423cab2bb3Spatrick   uint32_t Function;
433cab2bb3Spatrick   unsigned char Kind;
443cab2bb3Spatrick   unsigned char AlwaysInstrument;
453cab2bb3Spatrick   unsigned char Version;
463cab2bb3Spatrick   unsigned char Padding[5]; // Need 16 bytes
47*1f9cb04fSpatrick   uint32_t function() const {
48*1f9cb04fSpatrick     // The target address is relative to the location of the Function variable.
49*1f9cb04fSpatrick     return reinterpret_cast<uint32_t>(&Function) + Function;
50*1f9cb04fSpatrick   }
51*1f9cb04fSpatrick   uint32_t address() const {
52*1f9cb04fSpatrick     // The target address is relative to the location of the Address variable.
53*1f9cb04fSpatrick     return reinterpret_cast<uint32_t>(&Address) + Address;
54*1f9cb04fSpatrick   }
553cab2bb3Spatrick #else
563cab2bb3Spatrick #error "Unsupported word size."
573cab2bb3Spatrick #endif
583cab2bb3Spatrick };
593cab2bb3Spatrick 
603cab2bb3Spatrick struct XRayFunctionSledIndex {
613cab2bb3Spatrick   const XRaySledEntry *Begin;
623cab2bb3Spatrick   const XRaySledEntry *End;
633cab2bb3Spatrick };
643cab2bb3Spatrick }
653cab2bb3Spatrick 
663cab2bb3Spatrick namespace __xray {
673cab2bb3Spatrick 
683cab2bb3Spatrick struct XRaySledMap {
693cab2bb3Spatrick   const XRaySledEntry *Sleds;
703cab2bb3Spatrick   size_t Entries;
713cab2bb3Spatrick   const XRayFunctionSledIndex *SledsIndex;
723cab2bb3Spatrick   size_t Functions;
733cab2bb3Spatrick };
743cab2bb3Spatrick 
753cab2bb3Spatrick bool patchFunctionEntry(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
763cab2bb3Spatrick                         void (*Trampoline)());
773cab2bb3Spatrick bool patchFunctionExit(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
783cab2bb3Spatrick bool patchFunctionTailExit(bool Enable, uint32_t FuncId,
793cab2bb3Spatrick                            const XRaySledEntry &Sled);
803cab2bb3Spatrick bool patchCustomEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
813cab2bb3Spatrick bool patchTypedEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
823cab2bb3Spatrick 
833cab2bb3Spatrick } // namespace __xray
843cab2bb3Spatrick 
853cab2bb3Spatrick extern "C" {
863cab2bb3Spatrick // The following functions have to be defined in assembler, on a per-platform
873cab2bb3Spatrick // basis. See xray_trampoline_*.S files for implementations.
883cab2bb3Spatrick extern void __xray_FunctionEntry();
893cab2bb3Spatrick extern void __xray_FunctionExit();
903cab2bb3Spatrick extern void __xray_FunctionTailExit();
913cab2bb3Spatrick extern void __xray_ArgLoggerEntry();
923cab2bb3Spatrick extern void __xray_CustomEvent();
933cab2bb3Spatrick extern void __xray_TypedEvent();
943cab2bb3Spatrick }
953cab2bb3Spatrick 
963cab2bb3Spatrick #endif
97