xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_interface_internal.h (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 //===-- xray_interface_internal.h -------------------------------*- 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 // This file is a part of XRay, a dynamic runtime instrumentation system.
10 //
11 // Implementation of the API functions. See also include/xray/xray_interface.h.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef XRAY_INTERFACE_INTERNAL_H
15 #define XRAY_INTERFACE_INTERNAL_H
16 
17 #include "sanitizer_common/sanitizer_platform.h"
18 #include "xray/xray_interface.h"
19 #include <cstddef>
20 #include <cstdint>
21 
22 extern "C" {
23 
24 struct XRaySledEntry {
25 #if SANITIZER_WORDSIZE == 64
26   uint64_t Address;
27   uint64_t Function;
28   unsigned char Kind;
29   unsigned char AlwaysInstrument;
30   unsigned char Version;
31   unsigned char Padding[13]; // Need 32 bytes
32   uint64_t function() const {
33     // The target address is relative to the location of the Function variable.
34     return reinterpret_cast<uint64_t>(&Function) + Function;
35   }
36   uint64_t address() const {
37     // The target address is relative to the location of the Address variable.
38     return reinterpret_cast<uint64_t>(&Address) + Address;
39   }
40 #elif SANITIZER_WORDSIZE == 32
41   uint32_t Address;
42   uint32_t Function;
43   unsigned char Kind;
44   unsigned char AlwaysInstrument;
45   unsigned char Version;
46   unsigned char Padding[5]; // Need 16 bytes
47   uint32_t function() const {
48     // The target address is relative to the location of the Function variable.
49     return reinterpret_cast<uint32_t>(&Function) + Function;
50   }
51   uint32_t address() const {
52     // The target address is relative to the location of the Address variable.
53     return reinterpret_cast<uint32_t>(&Address) + Address;
54   }
55 #else
56 #error "Unsupported word size."
57 #endif
58 };
59 
60 struct XRayFunctionSledIndex {
61   const XRaySledEntry *Begin;
62   size_t Size;
63   // For an entry in the xray_fn_idx section, the address is relative to the
64   // location of the Begin variable.
65   const XRaySledEntry *fromPCRelative() const {
66     return reinterpret_cast<const XRaySledEntry *>(uintptr_t(&Begin) +
67                                                    uintptr_t(Begin));
68   }
69 };
70 }
71 
72 namespace __xray {
73 
74 struct XRaySledMap {
75   const XRaySledEntry *Sleds;
76   size_t Entries;
77   const XRayFunctionSledIndex *SledsIndex;
78   size_t Functions;
79 };
80 
81 bool patchFunctionEntry(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
82                         void (*Trampoline)());
83 bool patchFunctionExit(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
84 bool patchFunctionTailExit(bool Enable, uint32_t FuncId,
85                            const XRaySledEntry &Sled);
86 bool patchCustomEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
87 bool patchTypedEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
88 
89 } // namespace __xray
90 
91 extern "C" {
92 // The following functions have to be defined in assembler, on a per-platform
93 // basis. See xray_trampoline_*.S files for implementations.
94 extern void __xray_FunctionEntry();
95 extern void __xray_FunctionExit();
96 extern void __xray_FunctionTailExit();
97 extern void __xray_ArgLoggerEntry();
98 extern void __xray_CustomEvent();
99 extern void __xray_TypedEvent();
100 }
101 
102 #endif
103