cleanup: add correct error messages to lttng-get-syscall-inout.sh
[lttng-modules.git] / tools / syscalls / lttng-get-syscall-inout.sh
... / ...
CommitLineData
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
7ARCH_NAME=$1
8SYSCALL_NAME=$2
9NB_ARGS=$3
10ARG_NR=$4
11TMPFILE=$(mktemp)
12GENERIC_INOUT_DESCRIPTION_FILE="$(dirname "$0")/table-syscall-inout.txt"
13
14# Delete temp file on exit
15trap 'rm -f "$TMPFILE"' EXIT
16
17if [ "${ARCH_NAME}" = "" ]; then
18 echo "Error: Please specify the arch name as first argument" >&2
19 exit 1
20fi
21
22if [ "${SYSCALL_NAME}" = "" ]; then
23 echo "Error: Please specify the system call name as second argument" >&2
24 exit 1
25fi
26
27if [[ "${NB_ARGS}" = "" ]]; then
28 echo "Error: Please specify a number of arguments as third argument" >&2
29 exit 1
30fi
31
32if [[ "${ARG_NR}" = "" || ${ARG_NR} == 0 ]]; then
33 echo "Error: Please specify an argument number larger than 0 as fourth argument" >&2
34 exit 1
35fi
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.
42function 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
79set -eu
80
81
82# Default to sc_inout for unknown syscalls
83if ! 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
88fi
89
90# Get the number of argument
91SC_ARGS=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) .*/\2/g' "${TMPFILE}")
92
93if [ "${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
96fi
97
98if [ "${ARG_NR}" == 1 ]; then
99 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: (\([^,)]*\).*/\3/g' "${TMPFILE}")
100fi
101
102if [ "${ARG_NR}" == 2 ]; then
103 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
104fi
105
106if [ "${ARG_NR}" == 3 ]; then
107 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
108fi
109
110if [ "${ARG_NR}" == 4 ]; then
111 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
112fi
113
114if [ "${ARG_NR}" == 5 ]; then
115 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
116fi
117
118if [ "${ARG_NR}" == 6 ]; then
119 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
120fi
121
122
123if [ "${SC_ARG_TYPE}" = "r" ]; then
124 echo "sc_in"
125fi
126if [ "${SC_ARG_TYPE}" = "w" ]; then
127 echo "sc_out"
128fi
129if [ "${SC_ARG_TYPE}" = "rw" ]; then
130 echo "sc_inout"
131fi
132
133# EOF
This page took 0.0264 seconds and 4 git commands to generate.