• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 // Default constraint for the probe arguments as operands.
20 #ifndef FOLLY_SDT_ARG_CONSTRAINT
21 #if defined(__powerpc64__) || defined(__powerpc__)
22 #define FOLLY_SDT_ARG_CONSTRAINT      "nZr"
23 #else
24 #define FOLLY_SDT_ARG_CONSTRAINT      "nor"
25 #endif
26 #endif
27 
28 // Instruction to emit for the probe.
29 #define FOLLY_SDT_NOP                 nop
30 
31 // Note section properties.
32 #define FOLLY_SDT_NOTE_NAME           "stapsdt"
33 #define FOLLY_SDT_NOTE_TYPE           3
34 
35 // Size of address depending on platform.
36 #ifdef __LP64__
37 #define FOLLY_SDT_ASM_ADDR            .8byte
38 #else
39 #define FOLLY_SDT_ASM_ADDR            .4byte
40 #endif
41 
42 // Assembler helper Macros.
43 #define FOLLY_SDT_S(x)                #x
44 #define FOLLY_SDT_ASM_1(x)            FOLLY_SDT_S(x) "\n"
45 #define FOLLY_SDT_ASM_2(a, b)         FOLLY_SDT_S(a) "," FOLLY_SDT_S(b) "\n"
46 #define FOLLY_SDT_ASM_3(a, b, c)      FOLLY_SDT_S(a) "," FOLLY_SDT_S(b) ","    \
47                                       FOLLY_SDT_S(c) "\n"
48 #define FOLLY_SDT_ASM_STRING(x)       FOLLY_SDT_ASM_1(.asciz FOLLY_SDT_S(x))
49 
50 // Helper to determine the size of an argument.
51 #define FOLLY_SDT_ISARRAY(x)  (__builtin_classify_type(x) == 14)
52 #define FOLLY_SDT_ARGSIZE(x)  (FOLLY_SDT_ISARRAY(x) ? sizeof(void*) : sizeof(x))
53 
54 // Format of each probe arguments as operand.
55 // Size of the arugment tagged with FOLLY_SDT_Sn, with "n" constraint.
56 // Value of the argument tagged with FOLLY_SDT_An, with configured constraint.
57 #define FOLLY_SDT_ARG(n, x)                                                    \
58   [FOLLY_SDT_S##n] "n"                ((size_t)FOLLY_SDT_ARGSIZE(x)),          \
59   [FOLLY_SDT_A##n] FOLLY_SDT_ARG_CONSTRAINT (x)
60 
61 // Templates to append arguments as operands.
62 #define FOLLY_SDT_OPERANDS_0()        [__sdt_dummy] "g" (0)
63 #define FOLLY_SDT_OPERANDS_1(_1)      FOLLY_SDT_ARG(1, _1)
64 #define FOLLY_SDT_OPERANDS_2(_1, _2)                                           \
65   FOLLY_SDT_OPERANDS_1(_1), FOLLY_SDT_ARG(2, _2)
66 #define FOLLY_SDT_OPERANDS_3(_1, _2, _3)                                       \
67   FOLLY_SDT_OPERANDS_2(_1, _2), FOLLY_SDT_ARG(3, _3)
68 #define FOLLY_SDT_OPERANDS_4(_1, _2, _3, _4)                                   \
69   FOLLY_SDT_OPERANDS_3(_1, _2, _3), FOLLY_SDT_ARG(4, _4)
70 #define FOLLY_SDT_OPERANDS_5(_1, _2, _3, _4, _5)                               \
71   FOLLY_SDT_OPERANDS_4(_1, _2, _3, _4), FOLLY_SDT_ARG(5, _5)
72 #define FOLLY_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6)                           \
73   FOLLY_SDT_OPERANDS_5(_1, _2, _3, _4, _5), FOLLY_SDT_ARG(6, _6)
74 #define FOLLY_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7)                       \
75   FOLLY_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), FOLLY_SDT_ARG(7, _7)
76 #define FOLLY_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8)                   \
77   FOLLY_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), FOLLY_SDT_ARG(8, _8)
78 
79 // Templates to reference the arguments from operands in note section.
80 #if defined(__powerpc64__ ) || defined(__powerpc__)
81 #define FOLLY_SDT_ARGTMPL(id)       %I[id]%[id]
82 #elif defined(__i386__)
83 #define FOLLY_SDT_ARGTMPL(id)       %w[id]
84 #else
85 #define FOLLY_SDT_ARGTMPL(id)       %[id]
86 #endif
87 #define FOLLY_SDT_ARGFMT(no)        %n[FOLLY_SDT_S##no]@FOLLY_SDT_ARGTMPL(FOLLY_SDT_A##no)
88 #define FOLLY_SDT_ARG_TEMPLATE_0    /*No arguments*/
89 #define FOLLY_SDT_ARG_TEMPLATE_1    FOLLY_SDT_ARGFMT(1)
90 #define FOLLY_SDT_ARG_TEMPLATE_2    FOLLY_SDT_ARG_TEMPLATE_1 FOLLY_SDT_ARGFMT(2)
91 #define FOLLY_SDT_ARG_TEMPLATE_3    FOLLY_SDT_ARG_TEMPLATE_2 FOLLY_SDT_ARGFMT(3)
92 #define FOLLY_SDT_ARG_TEMPLATE_4    FOLLY_SDT_ARG_TEMPLATE_3 FOLLY_SDT_ARGFMT(4)
93 #define FOLLY_SDT_ARG_TEMPLATE_5    FOLLY_SDT_ARG_TEMPLATE_4 FOLLY_SDT_ARGFMT(5)
94 #define FOLLY_SDT_ARG_TEMPLATE_6    FOLLY_SDT_ARG_TEMPLATE_5 FOLLY_SDT_ARGFMT(6)
95 #define FOLLY_SDT_ARG_TEMPLATE_7    FOLLY_SDT_ARG_TEMPLATE_6 FOLLY_SDT_ARGFMT(7)
96 #define FOLLY_SDT_ARG_TEMPLATE_8    FOLLY_SDT_ARG_TEMPLATE_7 FOLLY_SDT_ARGFMT(8)
97 
98 // Structure of note section for the probe.
99 #define FOLLY_SDT_NOTE_CONTENT(provider, name, arg_template)                   \
100   FOLLY_SDT_ASM_1(990: FOLLY_SDT_NOP)                                          \
101   FOLLY_SDT_ASM_3(     .pushsection .note.stapsdt,"","note")                   \
102   FOLLY_SDT_ASM_1(     .balign 4)                                              \
103   FOLLY_SDT_ASM_3(     .4byte 992f-991f, 994f-993f, FOLLY_SDT_NOTE_TYPE)       \
104   FOLLY_SDT_ASM_1(991: .asciz FOLLY_SDT_NOTE_NAME)                             \
105   FOLLY_SDT_ASM_1(992: .balign 4)                                              \
106   FOLLY_SDT_ASM_1(993: FOLLY_SDT_ASM_ADDR 990b)                                \
107   FOLLY_SDT_ASM_1(     FOLLY_SDT_ASM_ADDR 0) /*Reserved for Semaphore address*/\
108   FOLLY_SDT_ASM_1(     FOLLY_SDT_ASM_ADDR 0) /*Reserved for Semaphore name*/   \
109   FOLLY_SDT_ASM_STRING(provider)                                               \
110   FOLLY_SDT_ASM_STRING(name)                                                   \
111   FOLLY_SDT_ASM_STRING(arg_template)                                           \
112   FOLLY_SDT_ASM_1(994: .balign 4)                                              \
113   FOLLY_SDT_ASM_1(     .popsection)
114 
115 // Main probe Macro.
116 #define FOLLY_SDT_PROBE(provider, name, n, arglist)                            \
117     __asm__ __volatile__ (                                                     \
118       FOLLY_SDT_NOTE_CONTENT(provider, name, FOLLY_SDT_ARG_TEMPLATE_##n)       \
119       :: FOLLY_SDT_OPERANDS_##n arglist                                        \
120     )                                                                          \
121 
122 // Helper Macros to handle variadic arguments.
123 #define FOLLY_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
124 #define FOLLY_SDT_NARG(...)                                                    \
125   FOLLY_SDT_NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
126 #define FOLLY_SDT_PROBE_N(provider, name, N, ...)                              \
127   FOLLY_SDT_PROBE(provider, name, N, (__VA_ARGS__))
128