xref: /linux/tools/perf/trace/beauty/arch_errno_names.sh (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Generate C file mapping errno codes to errno names.
5#
6# Copyright IBM Corp. 2018
7# Author(s):  Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8
9gcc="$1"
10toolsdir="$2"
11include_path="-I$toolsdir/include/uapi"
12
13arch_string()
14{
15	echo "$1" |sed -e 'y/- /__/' |tr '[[:upper:]]' '[[:lower:]]'
16}
17
18asm_errno_file()
19{
20	local arch="$1"
21	local header
22
23	header="$toolsdir/arch/$arch/include/uapi/asm/errno.h"
24	if test -r "$header"; then
25		echo "$header"
26	else
27		echo "$toolsdir/include/uapi/asm-generic/errno.h"
28	fi
29}
30
31create_errno_lookup_func()
32{
33	local arch=$(arch_string "$1")
34	local nr name
35
36	cat <<EoFuncBegin
37static const char *errno_to_name__$arch(int err)
38{
39	switch (err) {
40EoFuncBegin
41
42	while read name nr; do
43		printf '\tcase %d: return "%s";\n' $nr $name
44	done
45
46	cat <<EoFuncEnd
47	default:
48		return "(unknown)";
49	}
50}
51
52EoFuncEnd
53}
54
55process_arch()
56{
57	local arch="$1"
58	local asm_errno=$(asm_errno_file "$arch")
59
60	$gcc $CFLAGS $include_path -E -dM -x c $asm_errno \
61		|grep -hE '^#define[[:blank:]]+(E[^[:blank:]]+)[[:blank:]]+([[:digit:]]+).*' \
62		|awk '{ print $2","$3; }' \
63		|sort -t, -k2 -nu \
64		|IFS=, create_errno_lookup_func "$arch"
65}
66
67create_arch_errno_table_func()
68{
69	local archlist="$1"
70	local default="$2"
71	local arch
72
73	printf 'const char *arch_syscalls__strerrno(const char *arch, int err)\n'
74	printf '{\n'
75	for arch in $archlist; do
76		printf '\tif (!strcmp(arch, "%s"))\n' $(arch_string "$arch")
77		printf '\t\treturn errno_to_name__%s(err);\n' $(arch_string "$arch")
78	done
79	printf '\treturn errno_to_name__%s(err);\n' $(arch_string "$default")
80	printf '}\n'
81}
82
83cat <<EoHEADER
84/* SPDX-License-Identifier: GPL-2.0 */
85
86#include <string.h>
87
88EoHEADER
89
90# Create list of architectures that have a specific errno.h.
91archlist=""
92for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
93	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
94done
95
96for arch in generic $archlist; do
97	process_arch "$arch"
98done
99create_arch_errno_table_func "$archlist" "generic"
100