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