]> granicus.if.org Git - strace/commitdiff
Replace direct usage of err_name/errnoent with print_err
authorEugene Syromyatnikov <evgsyr@gmail.com>
Thu, 27 Sep 2018 05:35:32 +0000 (07:35 +0200)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 14 Aug 2019 11:22:32 +0000 (11:22 +0000)
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 <ldv@altlinux.org>
defs.h
keyctl.c
net.c
netlink.c
numa.c
print_fields.h
printsiginfo.c
ptrace_syscall_info.c
syscall.c
tests/pidfd_send_signal.c
tests/ptrace.c

diff --git a/defs.h b/defs.h
index 003cc2283f8cf7d454a69e336920bd17c41a62f7..3cccdac188e709e9ed9be0e7ec79b0f5b5e2a9af 100644 (file)
--- 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 *);
index e880410c1adcaebafbd39f7510d6348a972c26f5..586fdc7c22568e6d995357bd9d331a30d0f5fbe7 100644 (file)
--- 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 b4b3ac40112a881d1b41793d117dea8096a37cb6..a58fa921e68f02a8e637542caf1812f8d09b0116 100644 (file)
--- 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("]");
 }
 
index 0a5016f6051debcc2cb2c6e1b19e3c20f9c0efa6..ad10b7ab5a9147e49df1e9a31424515d4b94defe 100644 (file)
--- a/netlink.c
+++ b/netlink.c
@@ -13,6 +13,7 @@
 #include <linux/audit.h>
 #include <linux/rtnetlink.h>
 #include <linux/xfrm.h>
+#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 4b39b1cd8b843d5b9bd13b2a2d3b14c9891fb37c..cc7a7cc66a8ce3ae3762f96256b3c6d3b971a852 100644 (file)
--- 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;
 }
index 677b5f6c220859621226dc21bc1835cd7e85f6dd..eda16f67cc67629fcb4904f24310e7e1afecb8fe 100644 (file)
                              (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.
index c1d601fd6ad5f7cfa9ff88b3768ab5638aa91a61..24f5dd7cd9071a54d8a6938f3d0afac61bef9065 100644 (file)
@@ -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) {
index bfd10814ed0b3b97d282638663c70fc1115e1c62..6a0ac33da49f69753db6537cb246c2a8134b56e8 100644 (file)
@@ -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);
                        }
index 16c603bc382c004d7a5610ad4efe00dd3fddab9e..fadd3b55855001eb82daa81505c529303fee93ba 100644 (file)
--- 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
index 5cbc6df659f369ddcb6c430bd03da25fcf7f62ee..4b736815327cc3647c087c146de57fa3c1b0692e 100644 (file)
@@ -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);
index 59b830954201212d6422020891157033c6d9b602..c2988fc851d477d3c6503e04a25f4e41e1f7f504 100644 (file)
@@ -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,