Fix syscall generator scripts
[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:
644d6e9c 5# lttng-get-syscall-inout.sh table-syscall-inout.txt select 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
b03cf820 17if [ "${GENERIC_INOUT_DESCRIPTION_FILE}" = "" ]; then
2879dbbc 18 echo "Error: Please specify input file name as first argument" >&2
b75d00c4
MD
19 exit 1
20fi
21
b03cf820 22if [ "${SYSCALL_NAME}" = "" ]; then
2879dbbc 23 echo "Error: Please specify system call name as second argument" >&2
b75d00c4
MD
24 exit 1
25fi
26
b03cf820 27if [[ "${ARG_NR}" = "" || ${ARG_NR} == 0 ]]; then
2879dbbc 28 echo "Error: Please specify argument number larger than 0 as third argument" >&2
b75d00c4
MD
29 exit 1
30fi
31
9497697f
JG
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.
37function write_inout_description ()
38{
39 local arch_name=$1
40 local syscall_name=$2
6ae73da4
MJ
41 local nb_args=$3
42 local output_file=$4
9497697f 43 local description_files=("$(dirname "$0")/table-syscall-inout-${arch_name}-override.txt" "$GENERIC_INOUT_DESCRIPTION_FILE")
b03cf820 44 local match_count
9497697f 45
b03cf820 46 for file in "${description_files[@]}"; do
9497697f
JG
47 if [ ! -f "$file" ]; then
48 continue
49 fi
50
51 # Look for the syscall's in/out description
b03cf820 52 grep "^syscall ${syscall_name} " "${file}" > "${output_file}" || true
9497697f
JG
53
54 # Error out if we got more than one syscall
b03cf820 55 match_count=$(wc -l < "${output_file}")
9497697f
JG
56 if [ "${match_count}" -gt 1 ]; then
57 # Fatal error; invalid description file
b03cf820 58 echo "Error: more than one system call match for ${SYSCALL_NAME}" >&2
9497697f
JG
59 exit 1
60 elif [ "${match_count}" -eq 1 ]; then
6ae73da4
MJ
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
9497697f
JG
65 # Description found
66 return 0
67 fi
68 done
69
70 return 1
71}
72
2879dbbc
MJ
73# Abort on error and undefined variable
74set -eu
75
b75d00c4 76
2879dbbc 77# Default to sc_inout for unknown syscalls
6ae73da4 78if ! write_inout_description "$ARCH_NAME" "$SYSCALL_NAME" "$NB_ARGS" "$TMPFILE"; then
2879dbbc 79 echo "Warning: no match for syscall '${SYSCALL_NAME}', set to 'inout'" >&2
b75d00c4
MD
80 # no match, default to inout
81 echo "sc_inout"
82 exit 0
83fi
84
2879dbbc
MJ
85# Get the number of argument
86SC_ARGS=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) .*/\2/g' "${TMPFILE}")
b75d00c4 87
2879dbbc 88if [ "${ARG_NR}" -gt "${SC_ARGS}" ]; then
b03cf820 89 echo "Error: argument number (${ARG_NR}) for ${SYSCALL_NAME} is larger than number of syscall arguments (${SC_ARGS})" >&2
b75d00c4
MD
90 exit 1
91fi
92
2879dbbc
MJ
93if [ "${ARG_NR}" == 1 ]; then
94 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: (\([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
95fi
96
2879dbbc
MJ
97if [ "${ARG_NR}" == 2 ]; then
98 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
99fi
100
2879dbbc
MJ
101if [ "${ARG_NR}" == 3 ]; then
102 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
103fi
104
2879dbbc
MJ
105if [ "${ARG_NR}" == 4 ]; then
106 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
107fi
108
2879dbbc
MJ
109if [ "${ARG_NR}" == 5 ]; then
110 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
111fi
112
2879dbbc
MJ
113if [ "${ARG_NR}" == 6 ]; then
114 SC_ARG_TYPE=$(sed 's/^syscall \([^ ]*\) nbargs \([^ ]*\) rw: ([^,]*, [^,]*, [^,]*, [^,]*, [^,]*, \([^,)]*\).*/\3/g' "${TMPFILE}")
b75d00c4
MD
115fi
116
b75d00c4 117
2879dbbc 118if [ "${SC_ARG_TYPE}" = "r" ]; then
b75d00c4
MD
119 echo "sc_in"
120fi
2879dbbc 121if [ "${SC_ARG_TYPE}" = "w" ]; then
b75d00c4
MD
122 echo "sc_out"
123fi
2879dbbc 124if [ "${SC_ARG_TYPE}" = "rw" ]; then
b75d00c4
MD
125 echo "sc_inout"
126fi
127
2879dbbc 128# EOF
This page took 0.054517 seconds and 4 git commands to generate.