From: Eugene Syromyatnikov Date: Thu, 27 Sep 2018 05:35:32 +0000 (+0200) Subject: Replace direct usage of err_name/errnoent with print_err X-Git-Tag: v5.3~53 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d058acc5d12f7132d07a06e86be8bcdf7c5a0739;p=strace Replace direct usage of err_name/errnoent with print_err Introduce print_err function that prints error number respecting current xlat verbosity settings, and switch err_name/errnoent callers to use this new function instead. * defs.h (err_name): Remove. (print_err): New declaration. * print_fields.h (PRINT_FIELD_ERR_D, PRINT_FIELD_ERR_U): New macros. * syscall.c (err_name): Add static qualifier, change argument type to uint64_t. (print_err): New function. * keyctl.c (keyctl_reject_key): Use print_err for printing error argument. * net.c (print_get_error): Use print_err for printing err. * numa.c (print_status): Use print_err for printing errno. * netlink.c: Include "print_fields.h". (decode_nlmsgerr): Use PRINT_FIELD_ERR_D for printing errno field. * printsiginfo.c: Include "print_fields.h". (print_si_info): Use PRINT_FIELD_ERR_U for printing si_errno field. * ptrace_syscall_info.c (print_ptrace_syscall_info): Use PRINT_FIELD_ERR_D for printing info.exit.rval. * tests/pidfd_send_signal.c (main): Update expected output. * tests/ptrace.c (main): Likewise. Co-Authored-by: Dmitry V. Levin --- diff --git a/defs.h b/defs.h index 003cc228..3cccdac1 100644 --- a/defs.h +++ b/defs.h @@ -479,7 +479,14 @@ extern kernel_long_t scno_by_name(const char *s, unsigned p, * @return Shuffled or raw syscall number, respectively. */ extern kernel_ulong_t shuffle_scno(kernel_ulong_t scno); -extern const char *err_name(unsigned long err); +/** + * Print error name in accordance with current xlat style setting. + * + * @param err Error value. + * @param negated If set to true, negative values of the err parameter indicate + * error condition, otherwise positive. + */ +extern void print_err(int64_t err, bool negated); extern bool is_erestart(struct tcb *); extern void temporarily_clear_syserror(struct tcb *); diff --git a/keyctl.c b/keyctl.c index e880410c..586fdc7c 100644 --- a/keyctl.c +++ b/keyctl.c @@ -166,11 +166,9 @@ static void keyctl_reject_key(struct tcb *tcp, key_serial_t id1, unsigned timeout, unsigned error, key_serial_t id2) { - const char *err_str = err_name(error); - print_keyring_serial_number(id1); tprintf(", %u, ", timeout); - print_xlat_ex(error, err_str, XLAT_STYLE_FMT_U); + print_err(error, false); tprints(", "); print_keyring_serial_number(id2); } diff --git a/net.c b/net.c index b4b3ac40..a58fa921 100644 --- a/net.c +++ b/net.c @@ -665,7 +665,7 @@ print_get_error(struct tcb *const tcp, const kernel_ulong_t addr, return; tprints("["); - print_xlat_ex(err, err_name(err), XLAT_STYLE_FMT_U); + print_err(err, false); tprints("]"); } diff --git a/netlink.c b/netlink.c index 0a5016f6..ad10b7ab 100644 --- a/netlink.c +++ b/netlink.c @@ -13,6 +13,7 @@ #include #include #include +#include "print_fields.h" #include "xlat/netlink_ack_flags.h" #include "xlat/netlink_delete_flags.h" #include "xlat/netlink_flags.h" @@ -501,12 +502,7 @@ decode_nlmsgerr(struct tcb *const tcp, if (umove_or_printaddr(tcp, addr, &err.error)) return; - tprints("{error="); - if (err.error < 0 && (unsigned) -err.error < nerrnos) { - tprintf("-%s", errnoent[-err.error]); - } else { - tprintf("%d", err.error); - } + PRINT_FIELD_ERR_D("{", err, error); addr += offsetof(struct nlmsgerr, msg); len -= offsetof(struct nlmsgerr, msg); diff --git a/numa.c b/numa.c index 4b39b1cd..cc7a7cc6 100644 --- a/numa.c +++ b/numa.c @@ -158,21 +158,8 @@ static bool print_status(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data) { const int status = *(int *) elem_buf; - bool is_errno = (status < 0) && ((unsigned) -status < nerrnos); - if (!is_errno || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) - tprintf("%d", status); - - if (!is_errno || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) - return true; - - if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE) - tprints(" /* "); - - tprintf("-%s", errnoent[-status]); - - if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE) - tprints(" */"); + print_err(status, true); return true; } diff --git a/print_fields.h b/print_fields.h index 677b5f6c..eda16f67 100644 --- a/print_fields.h +++ b/print_fields.h @@ -79,6 +79,20 @@ (xlat_), NULL); \ } while (0) +#define PRINT_FIELD_ERR_D(prefix_, where_, field_) \ + do { \ + STRACE_PRINTF("%s%s=", (prefix_), #field_); \ + print_err(sign_extend_unsigned_to_ll((where_).field_), \ + true); \ + } while (0) + +#define PRINT_FIELD_ERR_U(prefix_, where_, field_) \ + do { \ + STRACE_PRINTF("%s%s=", (prefix_), #field_); \ + print_err(zero_extend_signed_to_ull((where_).field_), \ + false); \ + } while (0) + /* * Generic "ID" printing. ID is considered unsigned except for the special value * of -1. diff --git a/printsiginfo.c b/printsiginfo.c index c1d601fd..24f5dd7c 100644 --- a/printsiginfo.c +++ b/printsiginfo.c @@ -24,6 +24,8 @@ #include "nr_prefix.c" +#include "print_fields.h" + #ifndef IN_MPERS # include "printsiginfo.h" #endif @@ -117,14 +119,8 @@ print_si_code(int si_signo, unsigned int si_code) static void print_si_info(const siginfo_t *sip) { - if (sip->si_errno) { - tprints(", si_errno="); - if ((unsigned) sip->si_errno < nerrnos - && errnoent[sip->si_errno]) - tprints(errnoent[sip->si_errno]); - else - tprintf("%d", sip->si_errno); - } + if (sip->si_errno) + PRINT_FIELD_ERR_U(", ", *sip, si_errno); if (SI_FROMUSER(sip)) { switch (sip->si_code) { diff --git a/ptrace_syscall_info.c b/ptrace_syscall_info.c index bfd10814..6a0ac33d 100644 --- a/ptrace_syscall_info.c +++ b/ptrace_syscall_info.c @@ -326,11 +326,7 @@ entry_printed: tprints(", exit={"); if (fetch_size >= expected_exit_size && info.exit.is_error) { - uint64_t err = -info.exit.rval; - - tprints("rval=-"); - print_xlat_ex(err, err_name(err), - XLAT_STYLE_FMT_U); + PRINT_FIELD_ERR_D("", info.exit, rval); } else { PRINT_FIELD_D("", info.exit, rval); } diff --git a/syscall.c b/syscall.c index 16c603bc..fadd3b55 100644 --- a/syscall.c +++ b/syscall.c @@ -417,13 +417,24 @@ dumpio(struct tcb *tcp) } } -const char * -err_name(unsigned long err) +static const char * +err_name(uint64_t err) +{ + return err < nerrnos ? errnoent[err] : NULL; +} + +void +print_err(int64_t err, bool negated) { - if ((err < nerrnos) && errnoent[err]) - return errnoent[err]; + const char *str = err_name(negated ? -err : err); - return NULL; + if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) + tprintf(negated ? "%" PRId64 : "%" PRIu64, err); + if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) + return; + (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV + ? tprintf : tprintf_comment)("%s%s", + negated ? "-" : "", str); } static void diff --git a/tests/pidfd_send_signal.c b/tests/pidfd_send_signal.c index 5cbc6df6..4b736815 100644 --- a/tests/pidfd_send_signal.c +++ b/tests/pidfd_send_signal.c @@ -54,7 +54,7 @@ main(void) sys_pidfd_send_signal(fd, SIGUSR2, si, -1); printf("pidfd_send_signal(%d, SIGUSR2, {si_signo=SIGUSR1" - ", si_code=SI_QUEUE, si_errno=%d, si_pid=%u, si_uid=%u" + ", si_code=SI_QUEUE, si_errno=%u, si_pid=%u, si_uid=%u" ", si_value={int=%d, ptr=%p}}, %#x) = %s\n", fd, si->si_errno, si->si_pid, si->si_uid, si->si_int, si->si_ptr, -1U, errstr); diff --git a/tests/ptrace.c b/tests/ptrace.c index 59b83095..c2988fc8 100644 --- a/tests/ptrace.c +++ b/tests/ptrace.c @@ -307,7 +307,7 @@ main(void) do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip); printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGBUS" - ", si_code=BUS_ADRALN, si_errno=%d, si_addr=%p}) = %s\n", + ", si_code=BUS_ADRALN, si_errno=%u, si_addr=%p}) = %s\n", (unsigned) pid, bad_request, sip->si_errno, sip->si_addr, errstr); @@ -321,7 +321,7 @@ main(void) do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip); printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGPROF" - ", si_code=%#x, si_errno=%d, si_pid=0, si_uid=3}) = %s\n", + ", si_code=%#x, si_errno=%u, si_pid=0, si_uid=3}) = %s\n", (unsigned) pid, bad_request, sip->si_code, sip->si_errno, errstr); @@ -349,7 +349,7 @@ main(void) do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip); printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGSYS" - ", si_code=SYS_SECCOMP, si_errno=%d, si_call_addr=NULL" + ", si_code=SYS_SECCOMP, si_errno=%u, si_call_addr=NULL" ", si_syscall=__NR_read, si_arch=%#x /* AUDIT_ARCH_??? */})" " = %s\n", (unsigned) pid, bad_request, sip->si_errno, sip->si_arch,