Fix syscall generator scripts
[lttng-modules.git] / tools / syscalls / lttng-get-syscall-inout.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3
4 # example usage:
5 # lttng-get-syscall-inout.sh table-syscall-inout.txt select 1
6
7 ARCH_NAME=$1
8 SYSCALL_NAME=$2
9 NB_ARGS=$3
10 ARG_NR=$4
11 TMPFILE=$(mktemp)
12 GENERIC_INOUT_DESCRIPTION_FILE="$(dirname "$0")/table-syscall-inout.txt"
13
14 # Delete temp file on exit
15 trap 'rm -f "$TMPFILE"' EXIT
16
17 if [ "${GENERIC_INOUT_DESCRIPTION_FILE}" = "" ]; then
18 echo "Error: Please specify input file name as first argument" >&2
19 exit 1
20 fi
21
22 if [ "${SYSCALL_NAME}" = "" ]; then
23 echo "Error: Please specify system call name as second argument" >&2
24 exit 1
25 fi
26
27 if [[ "${ARG_NR}" = "" || ${ARG_NR} == 0 ]]; then
28 echo "Error: Please specify argument number larger than 0 as third argument" >&2
29 exit 1
30 fi
31
32 # Search for the in/out description of a syscall. This function attempts to find
33 # a matching description in the per-architecture description override file (if it exists)
34 # and falls back to the generic description file otherwise.
35 #
36 # Returns 0 if a description was found and written to the output file, 1 otherwise.
37 function write_inout_description ()
38 {
39 local arch_name=$1
40 local syscall_name=$2
41 local nb_args=$3
42 local output_file=$4
43 local description_files=("$(dirname "$0")/table-syscall-inout-${arch_name}-override.txt" "$GENERIC_INOUT_DESCRIPTION_FILE")
44 local match_count
45
46 for file in "${description_files[@]}"; do
47 if [ ! -f "$file" ]; then
48 continue
49 fi
50
51 # Look for the syscall's in/out description
52 grep "^syscall ${syscall_name} " "${file}" > "${output_file}" || true
53
54 # Error out if we got more than one syscall
55 match_count=$(wc -l < "${output_file}")
56 if [ "${match_count}" -gt 1 ]; then
57 # Fatal error; invalid description file
58 echo "Error: more than one system call match for ${SYSCALL_NAME}" >&2
59 exit 1
60 elif [ "${match_count}" -eq 1 ]; then
61 if ! grep -q "^syscall ${syscall_name} nbargs ${nb_args}" "${output_file}"; then
62 echo "Error: number of arguments doesn't match for ${SYSCALL_NAME}" >&2
63 exit 1
64 fi
65 # Description found
66 return 0
67 fi
68 done
69
70 return 1
71 }
72
73 # Abort on error and undefined variable
74 set -eu
75
76
77 # Default to sc_inout for unknown syscalls
78 if ! write_inout_description "$ARCH_NAME" "$SYSCALL_NAME" "$NB_ARGS" "$TMPFILE"; then
79 echo "Warning: no match for syscall '${SYSCALL_NAME}', set to 'inout'" >&2
80 # no match, default to inout
81 echo "sc_inout"
82 exit 0
83 fi
84
85 # Get the number of argument
86 SC_ARGS=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) .*/\2/g' "${TMPFILE}")
87
88 if [ "${ARG_NR}" -gt "${SC_ARGS}" ]; then
89 echo "Error: argument number (${ARG_NR}) for ${SYSCALL_NAME} is larger than number of syscall arguments (${SC_ARGS})" >&2
90 exit 1
91 fi
92
93 if [ "${ARG_NR}" == 1 ]; then
94 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: (\([^,)]*\).*/\3/g' "${TMPFILE}")
95 fi
96
97 if [ "${ARG_NR}" == 2 ]; then
98 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
99 fi
100
101 if [ "${ARG_NR}" == 3 ]; then
102 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
103 fi
104
105 if [ "${ARG_NR}" == 4 ]; then
106 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
107 fi
108
109 if [ "${ARG_NR}" == 5 ]; then
110 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
111 fi
112
113 if [ "${ARG_NR}" == 6 ]; then
114 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
115 fi
116
117
118 if [ "${SC_ARG_TYPE}" = "r" ]; then
119 echo "sc_in"
120 fi
121 if [ "${SC_ARG_TYPE}" = "w" ]; then
122 echo "sc_out"
123 fi
124 if [ "${SC_ARG_TYPE}" = "rw" ]; then
125 echo "sc_inout"
126 fi
127
128 # EOF
This page took 0.045436 seconds and 4 git commands to generate.