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