Browse Source

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 <ldv@altlinux.org>
Eugene Syromyatnikov 1 year ago
parent
commit
d058acc5d1
11 changed files with 52 additions and 47 deletions
  1. 8
    1
      defs.h
  2. 1
    3
      keyctl.c
  3. 1
    1
      net.c
  4. 2
    6
      netlink.c
  5. 1
    14
      numa.c
  6. 14
    0
      print_fields.h
  7. 4
    8
      printsiginfo.c
  8. 1
    5
      ptrace_syscall_info.c
  9. 16
    5
      syscall.c
  10. 1
    1
      tests/pidfd_send_signal.c
  11. 3
    3
      tests/ptrace.c

+ 8
- 1
defs.h View File

@@ -479,7 +479,14 @@ extern kernel_long_t scno_by_name(const char *s, unsigned p,
479 479
  * @return     Shuffled or raw syscall number, respectively.
480 480
  */
481 481
 extern kernel_ulong_t shuffle_scno(kernel_ulong_t scno);
482
-extern const char *err_name(unsigned long err);
482
+/**
483
+ * Print error name in accordance with current xlat style setting.
484
+ *
485
+ * @param err     Error value.
486
+ * @param negated If set to true, negative values of the err parameter indicate
487
+ *                error condition, otherwise positive.
488
+ */
489
+extern void print_err(int64_t err, bool negated);
483 490
 
484 491
 extern bool is_erestart(struct tcb *);
485 492
 extern void temporarily_clear_syserror(struct tcb *);

+ 1
- 3
keyctl.c View File

@@ -166,11 +166,9 @@ static void
166 166
 keyctl_reject_key(struct tcb *tcp, key_serial_t id1, unsigned timeout,
167 167
 		  unsigned error, key_serial_t id2)
168 168
 {
169
-	const char *err_str = err_name(error);
170
-
171 169
 	print_keyring_serial_number(id1);
172 170
 	tprintf(", %u, ", timeout);
173
-	print_xlat_ex(error, err_str, XLAT_STYLE_FMT_U);
171
+	print_err(error, false);
174 172
 	tprints(", ");
175 173
 	print_keyring_serial_number(id2);
176 174
 }

+ 1
- 1
net.c View File

@@ -665,7 +665,7 @@ print_get_error(struct tcb *const tcp, const kernel_ulong_t addr,
665 665
 		return;
666 666
 
667 667
 	tprints("[");
668
-	print_xlat_ex(err, err_name(err), XLAT_STYLE_FMT_U);
668
+	print_err(err, false);
669 669
 	tprints("]");
670 670
 }
671 671
 

+ 2
- 6
netlink.c View File

@@ -13,6 +13,7 @@
13 13
 #include <linux/audit.h>
14 14
 #include <linux/rtnetlink.h>
15 15
 #include <linux/xfrm.h>
16
+#include "print_fields.h"
16 17
 #include "xlat/netlink_ack_flags.h"
17 18
 #include "xlat/netlink_delete_flags.h"
18 19
 #include "xlat/netlink_flags.h"
@@ -501,12 +502,7 @@ decode_nlmsgerr(struct tcb *const tcp,
501 502
 	if (umove_or_printaddr(tcp, addr, &err.error))
502 503
 		return;
503 504
 
504
-	tprints("{error=");
505
-	if (err.error < 0 && (unsigned) -err.error < nerrnos) {
506
-		tprintf("-%s", errnoent[-err.error]);
507
-	} else {
508
-		tprintf("%d", err.error);
509
-	}
505
+	PRINT_FIELD_ERR_D("{", err, error);
510 506
 
511 507
 	addr += offsetof(struct nlmsgerr, msg);
512 508
 	len -= offsetof(struct nlmsgerr, msg);

+ 1
- 14
numa.c View File

@@ -158,21 +158,8 @@ static bool
158 158
 print_status(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
159 159
 {
160 160
 	const int status = *(int *) elem_buf;
161
-	bool is_errno = (status < 0) && ((unsigned) -status < nerrnos);
162 161
 
163
-	if (!is_errno || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
164
-		tprintf("%d", status);
165
-
166
-	if (!is_errno || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
167
-		return true;
168
-
169
-	if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
170
-		tprints(" /* ");
171
-
172
-	tprintf("-%s", errnoent[-status]);
173
-
174
-	if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
175
-		tprints(" */");
162
+	print_err(status, true);
176 163
 
177 164
 	return true;
178 165
 }

+ 14
- 0
print_fields.h View File

@@ -79,6 +79,20 @@
79 79
 			      (xlat_), NULL);				\
80 80
 	} while (0)
81 81
 
82
+#define PRINT_FIELD_ERR_D(prefix_, where_, field_)			\
83
+	do {								\
84
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
85
+		print_err(sign_extend_unsigned_to_ll((where_).field_),	\
86
+			  true);					\
87
+	} while (0)
88
+
89
+#define PRINT_FIELD_ERR_U(prefix_, where_, field_)			\
90
+	do {								\
91
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
92
+		print_err(zero_extend_signed_to_ull((where_).field_),	\
93
+			  false);					\
94
+	} while (0)
95
+
82 96
 /*
83 97
  * Generic "ID" printing. ID is considered unsigned except for the special value
84 98
  * of -1.

+ 4
- 8
printsiginfo.c View File

@@ -24,6 +24,8 @@
24 24
 
25 25
 #include "nr_prefix.c"
26 26
 
27
+#include "print_fields.h"
28
+
27 29
 #ifndef IN_MPERS
28 30
 # include "printsiginfo.h"
29 31
 #endif
@@ -117,14 +119,8 @@ print_si_code(int si_signo, unsigned int si_code)
117 119
 static void
118 120
 print_si_info(const siginfo_t *sip)
119 121
 {
120
-	if (sip->si_errno) {
121
-		tprints(", si_errno=");
122
-		if ((unsigned) sip->si_errno < nerrnos
123
-		    && errnoent[sip->si_errno])
124
-			tprints(errnoent[sip->si_errno]);
125
-		else
126
-			tprintf("%d", sip->si_errno);
127
-	}
122
+	if (sip->si_errno)
123
+		PRINT_FIELD_ERR_U(", ", *sip, si_errno);
128 124
 
129 125
 	if (SI_FROMUSER(sip)) {
130 126
 		switch (sip->si_code) {

+ 1
- 5
ptrace_syscall_info.c View File

@@ -326,11 +326,7 @@ entry_printed:
326 326
 			tprints(", exit={");
327 327
 			if (fetch_size >= expected_exit_size
328 328
 			    && info.exit.is_error) {
329
-				uint64_t err = -info.exit.rval;
330
-
331
-				tprints("rval=-");
332
-				print_xlat_ex(err, err_name(err),
333
-					      XLAT_STYLE_FMT_U);
329
+				PRINT_FIELD_ERR_D("", info.exit, rval);
334 330
 			} else {
335 331
 				PRINT_FIELD_D("", info.exit, rval);
336 332
 			}

+ 16
- 5
syscall.c View File

@@ -417,13 +417,24 @@ dumpio(struct tcb *tcp)
417 417
 	}
418 418
 }
419 419
 
420
-const char *
421
-err_name(unsigned long err)
420
+static const char *
421
+err_name(uint64_t err)
422
+{
423
+	return err < nerrnos ? errnoent[err] : NULL;
424
+}
425
+
426
+void
427
+print_err(int64_t err, bool negated)
422 428
 {
423
-	if ((err < nerrnos) && errnoent[err])
424
-		return errnoent[err];
429
+	const char *str = err_name(negated ? -err : err);
425 430
 
426
-	return NULL;
431
+	if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
432
+		tprintf(negated ? "%" PRId64 : "%" PRIu64, err);
433
+	if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
434
+		return;
435
+	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
436
+		? tprintf : tprintf_comment)("%s%s",
437
+					     negated ? "-" : "", str);
427 438
 }
428 439
 
429 440
 static void

+ 1
- 1
tests/pidfd_send_signal.c View File

@@ -54,7 +54,7 @@ main(void)
54 54
 
55 55
 	sys_pidfd_send_signal(fd, SIGUSR2, si, -1);
56 56
 	printf("pidfd_send_signal(%d, SIGUSR2, {si_signo=SIGUSR1"
57
-	       ", si_code=SI_QUEUE, si_errno=%d, si_pid=%u, si_uid=%u"
57
+	       ", si_code=SI_QUEUE, si_errno=%u, si_pid=%u, si_uid=%u"
58 58
 	       ", si_value={int=%d, ptr=%p}}, %#x) = %s\n",
59 59
 	       fd, si->si_errno, si->si_pid, si->si_uid, si->si_int, si->si_ptr,
60 60
 	       -1U, errstr);

+ 3
- 3
tests/ptrace.c View File

@@ -307,7 +307,7 @@ main(void)
307 307
 
308 308
 	do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
309 309
 	printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGBUS"
310
-	       ", si_code=BUS_ADRALN, si_errno=%d, si_addr=%p}) = %s\n",
310
+	       ", si_code=BUS_ADRALN, si_errno=%u, si_addr=%p}) = %s\n",
311 311
 	       (unsigned) pid, bad_request, sip->si_errno, sip->si_addr,
312 312
 	       errstr);
313 313
 
@@ -321,7 +321,7 @@ main(void)
321 321
 
322 322
 	do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
323 323
 	printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGPROF"
324
-	       ", si_code=%#x, si_errno=%d, si_pid=0, si_uid=3}) = %s\n",
324
+	       ", si_code=%#x, si_errno=%u, si_pid=0, si_uid=3}) = %s\n",
325 325
 	       (unsigned) pid, bad_request, sip->si_code, sip->si_errno,
326 326
 	       errstr);
327 327
 
@@ -349,7 +349,7 @@ main(void)
349 349
 
350 350
 	do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
351 351
 	printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGSYS"
352
-	       ", si_code=SYS_SECCOMP, si_errno=%d, si_call_addr=NULL"
352
+	       ", si_code=SYS_SECCOMP, si_errno=%u, si_call_addr=NULL"
353 353
 	       ", si_syscall=__NR_read, si_arch=%#x /* AUDIT_ARCH_??? */})"
354 354
 	       " = %s\n",
355 355
 	       (unsigned) pid, bad_request, sip->si_errno, sip->si_arch,

Loading…
Cancel
Save