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