Fix syscall generator scripts
[lttng-modules.git] / tools / syscalls / lttng-syscalls-generate-headers.sh
1 #!/bin/bash
2
3 # Generate system call probe description macros from syscall metadata dump file.
4 # The resulting header will be written in the headers subdirectory, in a file name
5 # based on the name of the input file.
6 #
7 # example usage:
8 #
9 # lttng-syscalls-generate-headers.sh <type> <input_dir> <arch_name> <bitness> <output_dir>
10 # lttng-syscalls-generate-headers.sh integers 3.0.4 x86 64 ../../include/instrumentation/syscalls/
11 # lttng-syscalls-generate-headers.sh pointers 3.0.4 x86 64 ../../include/instrumentation/syscalls/
12
13 CLASS=$1
14 VERSIONDIR=$2
15 ARCH=$3
16 BITNESS=$4
17 OUTPUTDIR=$5
18 ARCH_NAME="$ARCH-$BITNESS"
19 INPUTFILE="$ARCH_NAME-syscalls"
20
21 if [ "$VERSIONDIR" = "" ]; then
22 echo "Error: Please specify input directory as second argument" >&2
23 exit 1
24 fi
25
26 if [ "$INPUTFILE" = "" ]; then
27 echo "Error: Please specify input file as third argument" >&2
28 exit 1
29 fi
30
31 if [ "$BITNESS" != "32" ] && [ "$BITNESS" != "64" ]; then
32 echo "Error: Please specify bitness as fourth argument (\"32\" or \"64\")" >&2
33 exit 1
34 fi
35
36 if [ "$ARCH_NAME" = "" ]; then
37 echo "Error: Please specify the architecture name as fourth argument" >&2
38 exit 1
39 fi
40
41 if [ "$OUTPUTDIR" = "" ]; then
42 echo "Error: Please specify output directory as fifth argument" >&2
43 exit 1
44 fi
45
46 # Abort on error and undefined variable
47 set -eu
48
49 INPUT=${VERSIONDIR}/${INPUTFILE}
50 HEADER="${OUTPUTDIR}/${INPUTFILE}_${CLASS}.h"
51
52 # Create temp files
53 SRCFILE=$(mktemp)
54 TMPFILE=$(mktemp)
55
56 # Delete temp files on exit
57 trap 'rm -f "${SRCFILE}" "${TMPFILE}"' EXIT
58
59 cp "${INPUT}" "${SRCFILE}"
60
61 ## Cleanup the input file
62 # Remove the dmesg timestamp if present
63 perl -pi -e 's/^\[.*\] //g' "${SRCFILE}"
64 # Remove the 'sys_' prefix from syscall names
65 perl -pi -e 's/^syscall sys_([^ ]*)/syscall $1/g' "${SRCFILE}"
66 # Remove the user attribute from arguments
67 sed -i 's/ __attribute__((user))//g' "${SRCFILE}"
68
69 #Filter
70
71 if [ "$CLASS" = integers ]; then
72 #select integers and no-args.
73 CLASSCAP=INTEGERS
74 grep -v "\\*\|cap_user_header_t" "${SRCFILE}" > "${TMPFILE}"
75 mv "${TMPFILE}" "${SRCFILE}"
76 elif [ "$CLASS" = pointers ]; then
77 #select system calls using pointers.
78 CLASSCAP=POINTERS
79 grep "\\*\|cap_#user_header_t" "${SRCFILE}" > "${TMPFILE}"
80 mv "${TMPFILE}" "${SRCFILE}"
81 else
82 echo "Error: Please specify \"integers\" or \"pointers\" as first argument" >&2
83 exit 1
84 fi
85
86
87 echo "/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) */
88 /* SPDX-FileCopyrightText: $(date +%Y) EfficiOS Inc. */
89
90 /* THIS FILE IS AUTO-GENERATED. DO NOT EDIT */
91
92 /* Generated from ${INPUTFILE} ${VERSIONDIR} */
93
94 #ifndef CREATE_SYSCALL_TABLE
95
96 #if !defined(_TRACE_SYSCALLS_${CLASSCAP}_H) || defined(TRACE_HEADER_MULTI_READ)
97 #define _TRACE_SYSCALLS_${CLASSCAP}_H
98
99 #include <lttng/tracepoint-event.h>
100 #include <linux/syscalls.h>
101 #include \"${INPUTFILE}_${CLASS}_override.h\"
102 #include \"syscalls_${CLASS}_override.h\"
103 " > "${HEADER}"
104
105 if [ "$CLASS" = integers ]; then
106
107 NRARGS=0
108
109 # shellcheck disable=SC2129
110 printf \
111 '#ifdef SC_ENTER
112 SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscalls_noargs,
113 TP_FIELDS()
114 )
115 ' >> "${HEADER}"
116
117 # shellcheck disable=SC2026
118 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | \
119 perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
120 'types: \(([^)]*)\) '\
121 'args: \(([^)]*)\)/'\
122 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
123 'SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscalls_noargs, $1)\n'\
124 '#endif/g' >> "${HEADER}"
125
126 printf '#else /* #ifdef SC_ENTER */\n' >> "${HEADER}"
127
128 # shellcheck disable=SC2026
129 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | \
130 perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
131 'types: \(([^)]*)\) '\
132 'args: \(([^)]*)\)/'\
133 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
134 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
135 ' TP_PROTO(sc_exit(long ret)),\n'\
136 ' TP_ARGS(sc_exit(ret)),\n'\
137 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)))\n'\
138 ')\n'\
139 '#endif/g' >> "${HEADER}"
140
141 printf '#endif /* else #ifdef SC_ENTER */\n' >> "${HEADER}"
142
143 fi
144
145
146 # types: 4
147 # args 5
148
149 NRARGS=1
150 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
151 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
152 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
153
154 echo Syscall: "${SC_NAME}" "${ARG1}"
155
156 # shellcheck disable=SC2026
157 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
158 'types: \(([^)]*)\) '\
159 'args: \(([^)]*)\)/'\
160 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
161 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
162 ' TP_PROTO(sc_exit(long ret,) $4 $5),\n'\
163 ' TP_ARGS(sc_exit(ret,) $5),\n'\
164 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $5, $5)))\n'\
165 ')\n'\
166 '#endif/g' >> "${HEADER}"
167 done
168
169 # types: 4 5
170 # args 6 7
171
172 NRARGS=2
173 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
174 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
175 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
176 ARG2=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 2)
177
178 echo Syscall: "${SC_NAME}" "${ARG1}" "${ARG2}"
179
180 # shellcheck disable=SC2026
181 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
182 'types: \(([^,]*), ([^)]*)\) '\
183 'args: \(([^,]*), ([^)]*)\)/'\
184 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
185 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
186 ' TP_PROTO(sc_exit(long ret,) $4 $6, $5 $7),\n'\
187 ' TP_ARGS(sc_exit(ret,) $6, $7),\n'\
188 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $6, $6)) '"${ARG2}"'(ctf_integer($5, $7, $7)))\n'\
189 ')\n'\
190 '#endif/g' >> "${HEADER}"
191 done
192
193 # types: 4 5 6
194 # args 7 8 9
195
196 NRARGS=3
197 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
198 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
199 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
200 ARG2=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 2)
201 ARG3=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 3)
202
203 echo Syscall: "${SC_NAME}" "${ARG1}" "${ARG2}" "${ARG3}"
204
205 # shellcheck disable=SC2026
206 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
207 'types: \(([^,]*), ([^,]*), ([^)]*)\) '\
208 'args: \(([^,]*), ([^,]*), ([^)]*)\)/'\
209 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
210 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
211 ' TP_PROTO(sc_exit(long ret,) $4 $7, $5 $8, $6 $9),\n'\
212 ' TP_ARGS(sc_exit(ret,) $7, $8, $9),\n'\
213 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $7, $7)) '"${ARG2}"'(ctf_integer($5, $8, $8)) '"${ARG3}"'(ctf_integer($6, $9, $9)))\n'\
214 ')\n'\
215 '#endif/g' >> "${HEADER}"
216 done
217
218
219 # types: 4 5 6 7
220 # args 8 9 10 11
221
222 NRARGS=4
223 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
224 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
225 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
226 ARG2=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 2)
227 ARG3=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 3)
228 ARG4=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 4)
229
230 echo Syscall: "${SC_NAME}" "${ARG1}" "${ARG2}" "${ARG3}" "${ARG4}"
231
232 # shellcheck disable=SC2026
233 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
234 'types: \(([^,]*), ([^,]*), ([^,]*), ([^)]*)\) '\
235 'args: \(([^,]*), ([^,]*), ([^,]*), ([^)]*)\)/'\
236 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
237 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
238 ' TP_PROTO(sc_exit(long ret,) $4 $8, $5 $9, $6 $10, $7 $11),\n'\
239 ' TP_ARGS(sc_exit(ret,) $8, $9, $10, $11),\n'\
240 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $8, $8)) '"${ARG2}"'(ctf_integer($5, $9, $9)) '"${ARG3}"'(ctf_integer($6, $10, $10)) '"${ARG4}"'(ctf_integer($7, $11, $11)))\n'\
241 ')\n'\
242 '#endif/g' >> "${HEADER}"
243 done
244
245 # types: 4 5 6 7 8
246 # args 9 10 11 12 13
247
248 NRARGS=5
249 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
250 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
251 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
252 ARG2=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 2)
253 ARG3=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 3)
254 ARG4=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 4)
255 ARG5=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 5)
256
257 echo Syscall: "${SC_NAME}" "${ARG1}" "${ARG2}" "${ARG3}" "${ARG4}" "${ARG5}"
258
259 # shellcheck disable=SC2026
260 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
261 'types: \(([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^)]*)\) '\
262 'args: \(([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^)]*)\)/'\
263 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
264 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
265 ' TP_PROTO(sc_exit(long ret,) $4 $9, $5 $10, $6 $11, $7 $12, $8 $13),\n'\
266 ' TP_ARGS(sc_exit(ret,) $9, $10, $11, $12, $13),\n'\
267 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $9, $9)) '"${ARG2}"'(ctf_integer($5, $10, $10)) '"${ARG3}"'(ctf_integer($6, $11, $11)) '"${ARG4}"'(ctf_integer($7, $12, $12)) '"${ARG5}"'(ctf_integer($8, $13, $13)))\n'\
268 ')\n'\
269 '#endif/g' >> "${HEADER}"
270 done
271
272
273 # types: 4 5 6 7 8 9
274 # args 10 11 12 13 14 15
275
276 NRARGS=6
277 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | while read -r LINE; do
278 SC_NAME=$(echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) .*/$1/g')
279 ARG1=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 1)
280 ARG2=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 2)
281 ARG3=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 3)
282 ARG4=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 4)
283 ARG5=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 5)
284 ARG6=$(./lttng-get-syscall-inout.sh "${ARCH_NAME}" "${SC_NAME}" "$NRARGS" 6)
285
286 echo Syscall: "${SC_NAME}" "${ARG1}" "${ARG2}" "${ARG3}" "${ARG4}" "${ARG5}" "${ARG6}"
287
288 # shellcheck disable=SC2026
289 echo "${LINE}" | perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) '\
290 'types: \(([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^\)]*)\) '\
291 'args: \(([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^,]*), ([^\)]*)\)/'\
292 '#ifndef OVERRIDE_'"${BITNESS}"'_$1\n'\
293 'SC_LTTNG_TRACEPOINT_EVENT($1,\n'\
294 ' TP_PROTO(sc_exit(long ret,) $4 $10, $5 $11, $6 $12, $7 $13, $8 $14, $9 $15),\n'\
295 ' TP_ARGS(sc_exit(ret,) $10, $11, $12, $13, $14, $15),\n'\
296 ' TP_FIELDS(sc_exit(ctf_integer(long, ret, ret)) '"${ARG1}"'(ctf_integer($4, $10, $10)) '"${ARG2}"'(ctf_integer($5, $11, $11)) '"${ARG3}"'(ctf_integer($6, $12, $12)) '"${ARG4}"'(ctf_integer($7, $13, $13)) '"${ARG5}"'(ctf_integer($8, $14, $14)) '"${ARG6}"'(ctf_integer($9, $15, $15)))\n'\
297 ')\n'\
298 '#endif/g' >> "${HEADER}"
299 done
300
301 # Macro for tracing syscall table
302
303 echo \
304 "
305 #endif /* _TRACE_SYSCALLS_${CLASSCAP}_H */
306
307 /* This part must be outside protection */
308 #include <lttng/define_trace.h>
309
310 #else /* CREATE_SYSCALL_TABLE */
311
312 #include \"${INPUTFILE}_${CLASS}_override.h\"
313 #include \"syscalls_${CLASS}_override.h\"
314 " >> "${HEADER}"
315
316 NRARGS=0
317
318 if [ "$CLASS" = integers ]; then
319 #noargs
320
321 # shellcheck disable=SC2129
322 printf '#ifdef SC_ENTER\n' >> "${HEADER}"
323
324 # shellcheck disable=SC2026
325 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | \
326 perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) .*$/'\
327 '#ifndef OVERRIDE_TABLE_'"${BITNESS}"'_$1\n'\
328 'TRACE_SYSCALL_TABLE\(syscalls_noargs, $1, $2, $3\)\n'\
329 '#endif/g' >> "${HEADER}"
330
331 printf '#else /* #ifdef SC_ENTER */\n' >> "${HEADER}"
332
333 # shellcheck disable=SC2026
334 grep "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | \
335 perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) .*$/'\
336 '#ifndef OVERRIDE_TABLE_'"${BITNESS}"'_$1\n'\
337 'TRACE_SYSCALL_TABLE($1, $1, $2, $3)\n'\
338 '#endif/g' >> "${HEADER}"
339
340 printf '#endif /* else #ifdef SC_ENTER */\n' >> "${HEADER}"
341 fi
342
343 #others.
344 # shellcheck disable=SC2026
345 grep -v "^syscall [^ ]* nr [^ ]* nbargs ${NRARGS} " "${SRCFILE}" | \
346 perl -p -e 's/^syscall ([^ ]*) nr ([^ ]*) nbargs ([^ ]*) .*$/'\
347 '#ifndef OVERRIDE_TABLE_'"${BITNESS}"'_$1\n'\
348 'TRACE_SYSCALL_TABLE($1, $1, $2, $3)\n'\
349 '#endif/g' >> "${HEADER}"
350
351 printf '\n#endif /* CREATE_SYSCALL_TABLE */\n' >> "${HEADER}"
352
353 #fields names: ...char * type with *name* or *file* or *path* or *root*
354 # or *put_old* or *type*
355 perl -pi -e 's/ctf_integer\(([^,)]*char \*), ([^\)]*)(name|file|path|root|put_old|type)([^\)]*)\)/ctf_user_string($2$3$4)/g' \
356 "${HEADER}"
357
358 #prettify addresses heuristics.
359 #field names with addr or ptr
360 perl -pi -e 's/ctf_integer\(([^,)]*), ([^,)]*addr|[^,)]*ptr)([^),]*)\)/ctf_integer_hex($1, $2$3, $2$3)/g' \
361 "${HEADER}"
362
363 #field types ending with '*'
364 perl -pi -e 's/ctf_integer\(([^,)]*\*), ([^),]*)\)/ctf_integer_hex($1, $2, $2)/g' "${HEADER}"
365
366 # EOF
This page took 0.039256 seconds and 4 git commands to generate.