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     if (Version < 2)
34       return Function;
35     // The target address is relative to the location of the Function variable.
36     return reinterpret_cast<uint64_t>(&Function) + Function;
37   }
38   uint64_t address() const {
39     if (Version < 2)
40       return Address;
41     // The target address is relative to the location of the Address variable.
42     return reinterpret_cast<uint64_t>(&Address) + Address;
43   }
44 #elif SANITIZER_WORDSIZE == 32
45   uint32_t Address;
46   uint32_t Function;
47   unsigned char Kind;
48   unsigned char AlwaysInstrument;
49   unsigned char Version;
50   unsigned char Padding[5]; // Need 16 bytes
51   uint32_t function() const {
52     if (Version < 2)
53       return Function;
54     // The target address is relative to the location of the Function variable.
55     return reinterpret_cast<uint32_t>(&Function) + Function;
56   }
57   uint32_t address() const {
58     if (Version < 2)
59       return Address;
60     // The target address is relative to the location of the Address variable.
61     return reinterpret_cast<uint32_t>(&Address) + Address;
62   }
63 #else
64 #error "Unsupported word size."
65 #endif
66 };
67 
68 struct XRayFunctionSledIndex {
69   const XRaySledEntry *Begin;
70   const XRaySledEntry *End;
71 };
72 }
73 
74 namespace __xray {
75 
76 struct XRaySledMap {
77   const XRaySledEntry *Sleds;
78   size_t Entries;
79   const XRayFunctionSledIndex *SledsIndex;
80   size_t Functions;
81 };
82 
83 bool patchFunctionEntry(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
84                         void (*Trampoline)());
85 bool patchFunctionExit(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
86 bool patchFunctionTailExit(bool Enable, uint32_t FuncId,
87                            const XRaySledEntry &Sled);
88 bool patchCustomEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
89 bool patchTypedEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
90 
91 } // namespace __xray
92 
93 extern "C" {
94 // The following functions have to be defined in assembler, on a per-platform
95 // basis. See xray_trampoline_*.S files for implementations.
96 extern void __xray_FunctionEntry();
97 extern void __xray_FunctionExit();
98 extern void __xray_FunctionTailExit();
99 extern void __xray_ArgLoggerEntry();
100 extern void __xray_CustomEvent();
101 extern void __xray_TypedEvent();
102 }
103 
104 #endif
105