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