Warn and return on fd overflow fdt
[lttng-modules.git] / tools / syscalls / lttng-get-syscall-inout.sh
CommitLineData
b7150b71 1#!/bin/bash
b7cdc182 2# SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
b75d00c4
MD
3
4# example usage:
a16d5130 5# lttng-get-syscall-inout.sh arm-64 select 5 1
b75d00c4 6
9497697f 7ARCH_NAME=$1
b75d00c4 8SYSCALL_NAME=$2
6ae73da4
MJ
9NB_ARGS=$3
10ARG_NR=$4
2879dbbc 11TMPFILE=$(mktemp)
9497697f 12GENERIC_INOUT_DESCRIPTION_FILE="$(dirname "$0")/table-syscall-inout.txt"
2879dbbc
MJ
13
14# Delete temp file on exit
15trap 'rm -f "$TMPFILE"' EXIT
16
a16d5130
MJ
17if [ "${ARCH_NAME}" = "" ]; then
18 echo "Error: Please specify the arch name as first argument" >&2
b75d00c4
MD
19 exit 1
20fi
21
b03cf820 22if [ "${SYSCALL_NAME}" = "" ]; then
a16d5130
MJ
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
b75d00c4
MD
29 exit 1
30fi
31
b03cf820 32if [[ "${ARG_NR}" = "" || ${ARG_NR} == 0 ]]; then
a16d5130 33 echo "Error: Please specify an argument number larger than 0 as fourth argument" >&2
b75d00c4
MD
34 exit 1
35fi
36
9497697f
JG
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
6ae73da4
MJ
46 local nb_args=$3
47 local output_file=$4
9497697f 48 local description_files=("$(dirname "$0")/table-syscall-inout-${arch_name}-override.txt" "$GENERIC_INOUT_DESCRIPTION_FILE")
b03cf820 49 local match_count
9497697f 50
b03cf820 51 for file in "${description_files[@]}"; do
9497697f
JG
52 if [ ! -f "$file" ]; then
53 continue
54 fi
55
56 # Look for the syscall's in/out description
b03cf820 57 grep "^syscall ${syscall_name} " "${file}" > "${output_file}" || true
9497697f
JG
58
59 # Error out if we got more than one syscall
b03cf820 60 match_count=$(wc -l < "${output_file}")
9497697f
JG
61 if [ "${match_count}" -gt 1 ]; then
62 # Fatal error; invalid description file
b03cf820 63 echo "Error: more than one system call match for ${SYSCALL_NAME}" >&2
9497697f
JG
64 exit 1
65 elif [ "${match_count}" -eq 1 ]; then
6ae73da4
MJ
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
9497697f
JG
70 # Description found
71 return 0
72 fi
73 done
74
75 return 1
76}
77
2879dbbc
MJ
78# Abort on error and undefined variable
79set -eu
80
b75d00c4 81
2879dbbc 82# Default to sc_inout for unknown syscalls
6ae73da4 83if ! write_inout_description "$ARCH_NAME" "$SYSCALL_NAME" "$NB_ARGS" "$TMPFILE"; then
2879dbbc 84 echo "Warning: no match for syscall '${SYSCALL_NAME}', set to 'inout'" >&2
b75d00c4
MD
85 # no match, default to inout
86 echo "sc_inout"
87 exit 0
88fi
89
2879dbbc
MJ
90# Get the number of argument
91SC_ARGS=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) .*/\2/g' "${TMPFILE}")
b75d00c4 92
2879dbbc 93if [ "${ARG_NR}" -gt "${SC_ARGS}" ]; then
b03cf820 94 echo "Error: argument number (${ARG_NR}) for ${SYSCALL_NAME} is larger than number of syscall arguments (${SC_ARGS})" >&2
b75d00c4
MD
95 exit 1
96fi
97
2879dbbc
MJ
98if [ "${ARG_NR}" == 1 ]; then
99 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: (\([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
100fi
101
2879dbbc
MJ
102if [ "${ARG_NR}" == 2 ]; then
103 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
104fi
105
2879dbbc
MJ
106if [ "${ARG_NR}" == 3 ]; then
107 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
108fi
109
2879dbbc
MJ
110if [ "${ARG_NR}" == 4 ]; then
111 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
112fi
113
2879dbbc
MJ
114if [ "${ARG_NR}" == 5 ]; then
115 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
116fi
117
2879dbbc
MJ
118if [ "${ARG_NR}" == 6 ]; then
119 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
120fi
121
b75d00c4 122
2879dbbc 123if [ "${SC_ARG_TYPE}" = "r" ]; then
b75d00c4
MD
124 echo "sc_in"
125fi
2879dbbc 126if [ "${SC_ARG_TYPE}" = "w" ]; then
b75d00c4
MD
127 echo "sc_out"
128fi
2879dbbc 129if [ "${SC_ARG_TYPE}" = "rw" ]; then
b75d00c4
MD
130 echo "sc_inout"
131fi
132
2879dbbc 133# EOF
This page took 0.089118 seconds and 5 git commands to generate.