xref: /linux/tools/perf/trace/beauty/arch_errno_names.sh (revision 55d0969c451159cff86949b38c39171cab962069)
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	arch="$1"
21
22	header="$toolsdir/arch/$arch/include/uapi/asm/errno.h"
23	if test -r "$header"; then
24		echo "$header"
25	else
26		echo "$toolsdir/include/uapi/asm-generic/errno.h"
27	fi
28}
29
30create_errno_lookup_func()
31{
32	arch=$(arch_string "$1")
33
34	printf "static const char *errno_to_name__%s(int err)\n{\n\tswitch (err) {\n" $arch
35
36	while read name nr; do
37		printf '\tcase %d: return "%s";\n' $nr $name
38	done
39
40	printf '\tdefault: return "(unknown)";\n\t}\n}\n'
41}
42
43process_arch()
44{
45	arch="$1"
46	asm_errno=$(asm_errno_file "$arch")
47
48	$gcc $CFLAGS $include_path -E -dM -x c $asm_errno \
49		|grep -hE '^#define[[:blank:]]+(E[^[:blank:]]+)[[:blank:]]+([[:digit:]]+).*' \
50		|awk '{ print $2","$3; }' \
51		|sort -t, -k2 -nu \
52		|IFS=, create_errno_lookup_func "$arch"
53}
54
55create_arch_errno_table_func()
56{
57	archlist="$1"
58	default="$2"
59
60	printf 'arch_syscalls__strerrno_t *arch_syscalls__strerrno_function(const char *arch)\n'
61	printf '{\n'
62	for arch in $archlist; do
63		arch_str=$(arch_string "$arch")
64		printf '\tif (!strcmp(arch, "%s"))\n' "$arch_str"
65		printf '\t\treturn errno_to_name__%s;\n' "$arch_str"
66	done
67	arch_str=$(arch_string "$default")
68	printf '\treturn errno_to_name__%s;\n' "$arch_str"
69	printf '}\n'
70}
71
72cat <<EoHEADER
73/* SPDX-License-Identifier: GPL-2.0 */
74
75#include <string.h>
76
77EoHEADER
78
79# Create list of architectures that have a specific errno.h.
80archlist=""
81for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
82	d=${f%/include/uapi/asm/errno.h}
83	arch="${d##*/}"
84	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
85done
86
87for arch in generic $archlist; do
88	process_arch "$arch"
89done
90create_arch_errno_table_func "$archlist" "generic"
91