[PATCH v3 2/7] Use printpid in decoders

Ákos Uzonyi uzonyi.akos at gmail.com
Fri Jul 24 23:12:38 UTC 2020


* getpid.c: New file.
* Makefile.am (libstrace_a_SOURCES): Add getpid.c.
* linux/dummy.h (sys_getpid, sys_getppid, sys_gettid,
sys_setpgid, sys_setpgrp): Remove.
* util.c (printfd_pid_tracee_ns): Implement using translate_pid.
* defs.h (printnum_pid): New function definition.
* util.c: (printnum_pid): New function.
* print_fields.h (PRINT_FIELD_TID): New macro.
(PRINT_FIELD_TGID): Likewise.
(PRINT_FIELD_PGID): Likewise.
(PRINT_FIELD_SID): Likewise.
* affinity.c: Print PIDs with printpid.
* block.c: Likewise.
* bpf.c: Likewise.
* capability.c: Likewise.
* clone.c: Likewise.
* fcntl.c: Likewise.
* get_robust_list.c: Likewise.
* ioprio.c: Likewise.
* kcmp.c: Likewise.
* msghdr.c: Likewise.
* net.c: Likewise.
* netlink.c: Likewise.
* numa.c: Likewise.
* pidfd_open.c: Likewise.
* printsiginfo.c: Likewise.
* process.c: Likewise.
* process_vm.c: Likewise.
* resource.c: Likewise.
* sched.c: Likewise.
* signal.c: Likewise.
* sockaddr.c: Likewise.
* wait.c: Likewise.
* kcmp.c (SYS_FUNC(kcmp)): Fix KCMP_FILE pid arguments.
* tests/kcmp.c (printpidfd): Print path if VERBOSE_FD.
(main): Use our real pid if real fds are used.
---
 affinity.c        |  6 ++++--
 block.c           |  2 +-
 bpf.c             |  2 +-
 capability.c      |  4 +++-
 clone.c           | 14 +++++++-------
 defs.h            |  3 +++
 fcntl.c           | 37 +++++++++++++++++++++++++++++++------
 get_robust_list.c |  3 ++-
 getpid.c          | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 ioprio.c          | 26 ++++++++++++++++++++++----
 ipc_shmctl.c      |  5 +++--
 kcmp.c            |  5 ++++-
 linux/dummy.h     |  8 +-------
 msghdr.c          |  2 +-
 net.c             |  2 +-
 netlink.c         |  5 +++--
 numa.c            |  6 ++++--
 pidfd_open.c      |  2 +-
 print_fields.h    | 24 ++++++++++++++++++++++++
 process.c         |  3 ++-
 process_vm.c      |  6 ++++--
 resource.c        | 27 ++++++++++++++++++++++++---
 sched.c           | 25 ++++++++++++++++---------
 signal.c          | 25 ++++++++++++++++++++-----
 tests/kcmp.c      | 25 ++++++++++++++++++++++---
 util.c            | 20 ++++++++++++++------
 wait.c            | 35 +++++++++++++++++++++++++++++------
 27 files changed, 293 insertions(+), 75 deletions(-)
 create mode 100644 getpid.c

diff --git a/affinity.c b/affinity.c
index eb3158b7..f9893adb 100644
--- a/affinity.c
+++ b/affinity.c
@@ -82,7 +82,8 @@ SYS_FUNC(sched_setaffinity)
 	const int pid = tcp->u_arg[0];
 	const unsigned int len = tcp->u_arg[1];
 
-	tprintf("%d, %u, ", pid, len);
+	printpid(tcp, pid, PT_TGID);
+	tprintf(", %u, ", len);
 	print_affinitylist(tcp, tcp->u_arg[2], len);
 
 	return RVAL_DECODED;
@@ -94,7 +95,8 @@ SYS_FUNC(sched_getaffinity)
 	const unsigned int len = tcp->u_arg[1];
 
 	if (entering(tcp)) {
-		tprintf("%d, %u, ", pid, len);
+		printpid(tcp, pid, PT_TGID);
+		tprintf(", %u, ", len);
 	} else {
 		print_affinitylist(tcp, tcp->u_arg[2], tcp->u_rval);
 	}
diff --git a/block.c b/block.c
index a2f41551..231fa56b 100644
--- a/block.c
+++ b/block.c
@@ -179,7 +179,7 @@ MPERS_PRINTER_DECL(int, block_ioctl, struct tcb *const tcp,
 			PRINT_FIELD_U(", ", buts, buf_nr);
 			PRINT_FIELD_U(", ", buts, start_lba);
 			PRINT_FIELD_U(", ", buts, end_lba);
-			PRINT_FIELD_D(", ", buts, pid);
+			PRINT_FIELD_TGID(", ", buts, pid, tcp);
 			return 0;
 		} else {
 			struct_blk_user_trace_setup buts;
diff --git a/bpf.c b/bpf.c
index b3ab9791..8aa02ad0 100644
--- a/bpf.c
+++ b/bpf.c
@@ -927,7 +927,7 @@ BEGIN_BPF_CMD_DECODER(BPF_TASK_FD_QUERY)
 	if (entering(tcp)) {
 		set_tcb_priv_ulong(tcp, attr.buf_len);
 
-		PRINT_FIELD_D("{task_fd_query={", attr, pid);
+		PRINT_FIELD_TGID("{task_fd_query={", attr, pid, tcp);
 		PRINT_FIELD_FD(", ", attr, fd, tcp);
 		PRINT_FIELD_U(", ", attr, flags);
 		PRINT_FIELD_U(", ", attr, buf_len);
diff --git a/capability.c b/capability.c
index 0e763ee1..f5a447fd 100644
--- a/capability.c
+++ b/capability.c
@@ -70,7 +70,9 @@ print_cap_header(struct tcb *const tcp, const kernel_ulong_t addr,
 	tprints("{version=");
 	printxval(cap_version, h->version,
 		  "_LINUX_CAPABILITY_VERSION_???");
-	tprintf(", pid=%d}", h->pid);
+	tprintf(", pid=");
+	printpid(tcp, h->pid, PT_TGID);
+	tprintf("}");
 }
 
 static void
diff --git a/clone.c b/clone.c
index 487c5c23..45f24065 100644
--- a/clone.c
+++ b/clone.c
@@ -114,14 +114,14 @@ SYS_FUNC(clone)
 		 */
 		if ((flags & (CLONE_PARENT_SETTID|CLONE_PIDFD|CLONE_CHILD_SETTID
 			      |CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0)
-			return RVAL_DECODED;
+			return RVAL_DECODED | RVAL_TID;
 	} else {
 		if (flags & (CLONE_PARENT_SETTID|CLONE_PIDFD)) {
 			kernel_ulong_t addr = tcp->u_arg[ARG_PTID];
 
 			tprints(", parent_tid=");
 			if (flags & CLONE_PARENT_SETTID)
-				printnum_int(tcp, addr, "%u");
+				printnum_pid(tcp, addr, PT_TID);
 			else
 				printnum_fd(tcp, addr);
 		}
@@ -134,7 +134,7 @@ SYS_FUNC(clone)
 			printaddr(tcp->u_arg[ARG_CTID]);
 		}
 	}
-	return 0;
+	return RVAL_TID;
 }
 
 
@@ -229,7 +229,7 @@ SYS_FUNC(clone3)
 
 		if ((arg.flags & (CLONE_PIDFD | CLONE_PARENT_SETTID)) ||
 		    (size > fetch_size))
-			return 0;
+			return RVAL_TID;
 
 		goto out;
 	}
@@ -256,7 +256,7 @@ SYS_FUNC(clone3)
 
 	if (arg.flags & CLONE_PARENT_SETTID) {
 		tprintf("%sparent_tid=", pfx);
-		printnum_int(tcp, arg.parent_tid, "%d"); /* TID */
+		printnum_pid(tcp, arg.parent_tid, PT_TID);
 		pfx = ", ";
 	}
 
@@ -279,7 +279,7 @@ SYS_FUNC(clone3)
 out:
 	tprintf(", %" PRI_klu, size);
 
-	return RVAL_DECODED;
+	return RVAL_DECODED | RVAL_TID;
 }
 
 
@@ -300,5 +300,5 @@ SYS_FUNC(unshare)
 
 SYS_FUNC(fork)
 {
-	return RVAL_DECODED;
+	return RVAL_DECODED | RVAL_TGID;
 }
diff --git a/defs.h b/defs.h
index 4df21700..ffe59e17 100644
--- a/defs.h
+++ b/defs.h
@@ -1532,6 +1532,9 @@ DECL_PRINTNUM_ADDR(int64);
 extern bool
 printnum_fd(struct tcb *, kernel_ulong_t addr);
 
+extern bool
+printnum_pid(struct tcb *const tcp, const kernel_ulong_t addr, enum pid_type type);
+
 static inline bool
 printnum_slong(struct tcb *tcp, kernel_ulong_t addr)
 {
diff --git a/fcntl.c b/fcntl.c
index e84549e9..bfa92714 100644
--- a/fcntl.c
+++ b/fcntl.c
@@ -21,14 +21,14 @@
 #include "xlat/notifyflags.h"
 
 static void
-print_struct_flock64(const struct_kernel_flock64 *fl, const int getlk)
+print_struct_flock64(struct tcb *const tcp, const struct_kernel_flock64 *fl, const int getlk)
 {
 	PRINT_FIELD_XVAL("{", *fl, l_type, lockfcmds, "F_???");
 	PRINT_FIELD_XVAL(", ", *fl, l_whence, whence_codes, "SEEK_???");
 	PRINT_FIELD_D(", ", *fl, l_start);
 	PRINT_FIELD_D(", ", *fl, l_len);
 	if (getlk)
-		PRINT_FIELD_D(", ", *fl, l_pid);
+		PRINT_FIELD_TGID(", ", *fl, l_pid, tcp);
 	tprints("}");
 }
 
@@ -38,7 +38,7 @@ printflock64(struct tcb *const tcp, const kernel_ulong_t addr, const int getlk)
 	struct_kernel_flock64 fl;
 
 	if (fetch_struct_flock64(tcp, addr, &fl))
-		print_struct_flock64(&fl, getlk);
+		print_struct_flock64(tcp, &fl, getlk);
 }
 
 static void
@@ -47,7 +47,7 @@ printflock(struct tcb *const tcp, const kernel_ulong_t addr, const int getlk)
 	struct_kernel_flock64 fl;
 
 	if (fetch_struct_flock(tcp, addr, &fl))
-		print_struct_flock64(&fl, getlk);
+		print_struct_flock64(tcp, &fl, getlk);
 }
 
 static void
@@ -59,8 +59,23 @@ print_f_owner_ex(struct tcb *const tcp, const kernel_ulong_t addr)
 		return;
 
 	PRINT_FIELD_XVAL("{", owner, type, f_owner_types, "F_OWNER_???");
-	PRINT_FIELD_D(", ", owner, pid);
-	tprints("}");
+
+	enum pid_type pid_type = PT_NONE;
+	switch (owner.type)
+	{
+	case F_OWNER_TID:
+		pid_type = PT_TID;
+		break;
+	case F_OWNER_PID:
+		pid_type = PT_TGID;
+		break;
+	case F_OWNER_PGRP:
+		pid_type = PT_PGID;
+		break;
+	}
+	tprintf(", pid=");
+	printpid(tcp, owner.pid, pid_type);
+	tprintf("}");
 }
 
 static int
@@ -74,6 +89,14 @@ print_fcntl(struct tcb *tcp)
 		printflags(fdflags, tcp->u_arg[2], "FD_???");
 		break;
 	case F_SETOWN:
+		tprintf(", ");
+		const int pid = tcp->u_arg[2];
+		tprintf("%d", pid);
+		if (pid < 0)
+			printpid_translation(tcp, -pid, PT_PGID);
+		else
+			printpid_translation(tcp,  pid, PT_TGID);
+		break;
 	case F_SETPIPE_SZ:
 		tprintf(", %" PRI_kld, tcp->u_arg[2]);
 		break;
@@ -116,6 +139,8 @@ print_fcntl(struct tcb *tcp)
 		printsignal(tcp->u_arg[2]);
 		break;
 	case F_GETOWN:
+		return RVAL_DECODED |
+		       ((int) tcp->u_rval < 0 ? RVAL_PGID : RVAL_TGID);
 	case F_GETPIPE_SZ:
 		break;
 	case F_GETFD:
diff --git a/get_robust_list.c b/get_robust_list.c
index b5aebaff..8c1b377c 100644
--- a/get_robust_list.c
+++ b/get_robust_list.c
@@ -10,7 +10,8 @@
 SYS_FUNC(get_robust_list)
 {
 	if (entering(tcp)) {
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TID);
+		tprintf(", ");
 	} else {
 		printnum_ptr(tcp, tcp->u_arg[1]);
 		tprints(", ");
diff --git a/getpid.c b/getpid.c
new file mode 100644
index 00000000..5e9a1a29
--- /dev/null
+++ b/getpid.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2020 Ákos Uzonyi <uzonyi.akos at gmail.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "defs.h"
+
+SYS_FUNC(getpid)
+{
+	return RVAL_DECODED | RVAL_TGID;
+}
+
+SYS_FUNC(gettid)
+{
+	return RVAL_DECODED | RVAL_TID;
+}
+
+SYS_FUNC(getpgrp)
+{
+	return RVAL_DECODED | RVAL_PGID;
+}
+
+SYS_FUNC(getpgid)
+{
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+
+	return RVAL_DECODED | RVAL_PGID;
+}
+
+SYS_FUNC(getsid)
+{
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+
+	return RVAL_DECODED | RVAL_SID;
+}
+
+SYS_FUNC(setpgid)
+{
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+	tprints(", ");
+	printpid(tcp, tcp->u_arg[1], PT_PGID);
+
+	return RVAL_DECODED;
+}
diff --git a/ioprio.c b/ioprio.c
index 873c8ce5..f41bb6b9 100644
--- a/ioprio.c
+++ b/ioprio.c
@@ -49,13 +49,30 @@ print_ioprio(unsigned int ioprio)
 		? tprints_comment : tprints)(str);
 }
 
+static void
+ioprio_print_who(struct tcb *tcp, int which, int who)
+{
+	switch (which)
+	{
+	case IOPRIO_WHO_PROCESS:
+		printpid(tcp, who, PT_TGID);
+		break;
+	case IOPRIO_WHO_PGRP:
+		printpid(tcp, who, PT_PGID);
+		break;
+	default:
+		tprintf("%d", who);
+		break;
+	}
+}
+
 SYS_FUNC(ioprio_get)
 {
 	if (entering(tcp)) {
 		/* int which */
 		printxval(ioprio_who, tcp->u_arg[0], "IOPRIO_WHO_???");
-		/* int who */
-		tprintf(", %d", (int) tcp->u_arg[1]);
+		tprintf(", ");
+		ioprio_print_who(tcp, (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
 		return 0;
 	} else {
 		if (syserror(tcp))
@@ -72,8 +89,9 @@ SYS_FUNC(ioprio_set)
 {
 	/* int which */
 	printxval(ioprio_who, tcp->u_arg[0], "IOPRIO_WHO_???");
-	/* int who */
-	tprintf(", %d, ", (int) tcp->u_arg[1]);
+	tprintf(", ");
+	ioprio_print_who(tcp, (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
+	tprintf(", ");
 	/* int ioprio */
 	if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
 		tprintf("%d", (int) tcp->u_arg[2]);
diff --git a/ipc_shmctl.c b/ipc_shmctl.c
index 8c9e1e7a..71b41651 100644
--- a/ipc_shmctl.c
+++ b/ipc_shmctl.c
@@ -12,6 +12,7 @@
  */
 
 #include "defs.h"
+#include "print_fields.h"
 
 #include DEF_MPERS_TYPE(shmid_ds_t)
 
@@ -53,8 +54,8 @@ print_shmid_ds(struct tcb *const tcp, const kernel_ulong_t addr, int cmd)
 		PRINT_FIELD_UID(", ", shmid_ds.shm_perm, cgid);
 		tprints("}");
 		tprintf(", shm_segsz=%u", (unsigned) shmid_ds.shm_segsz);
-		PRINT_FIELD_D(", ", shmid_ds, shm_cpid);
-		PRINT_FIELD_D(", ", shmid_ds, shm_lpid);
+		PRINT_FIELD_TGID(", ", shmid_ds, shm_cpid, tcp);
+		PRINT_FIELD_TGID(", ", shmid_ds, shm_lpid, tcp);
 		tprintf(", shm_nattch=%u", (unsigned) shmid_ds.shm_nattch);
 		tprintf(", shm_atime=%u", (unsigned) shmid_ds.shm_atime);
 		tprintf(", shm_dtime=%u", (unsigned) shmid_ds.shm_dtime);
diff --git a/kcmp.c b/kcmp.c
index 62115555..fa70c51c 100644
--- a/kcmp.c
+++ b/kcmp.c
@@ -30,7 +30,10 @@ SYS_FUNC(kcmp)
 	kernel_ulong_t idx1 = tcp->u_arg[3];
 	kernel_ulong_t idx2 = tcp->u_arg[4];
 
-	tprintf("%d, %d, ", pid1, pid2);
+	printpid(tcp, pid1, PT_TGID);
+	tprintf(", ");
+	printpid(tcp, pid2, PT_TGID);
+	tprintf(", ");
 	printxval(kcmp_types, type, "KCMP_???");
 
 	switch (type) {
diff --git a/linux/dummy.h b/linux/dummy.h
index 2f859a60..ca0d2f0c 100644
--- a/linux/dummy.h
+++ b/linux/dummy.h
@@ -53,6 +53,7 @@
 # define sys_getgid		sys_getuid
 # define sys_getgid16		sys_getuid16
 # define sys_getpeername		sys_getsockname
+# define sys_getppid		sys_getpid
 # define sys_getresgid		sys_getresuid
 # define sys_getresgid16		sys_getresuid16
 # define sys_lstat		sys_stat
@@ -87,10 +88,6 @@
 # define sys_vfork		sys_fork
 
 /* printargs does the right thing */
-# define sys_getpgrp		printargs
-# define sys_getpid		printargs
-# define sys_getppid		printargs
-# define sys_gettid		printargs
 # define sys_idle		printargs
 # define sys_munlockall		printargs
 # define sys_pause		printargs
@@ -108,10 +105,7 @@
 
 /* printargs_d does the right thing */
 # define sys_exit		printargs_d
-# define sys_getpgid		printargs_d
-# define sys_getsid		printargs_d
 # define sys_nice		printargs_d
-# define sys_setpgid		printargs_d
 # define sys_setpgrp		printargs_d
 # define sys_timer_delete	printargs_d
 # define sys_timer_getoverrun	printargs_d
diff --git a/msghdr.c b/msghdr.c
index 1ab4c6e2..adfdcd86 100644
--- a/msghdr.c
+++ b/msghdr.c
@@ -69,7 +69,7 @@ print_scm_creds(struct tcb *tcp, const void *cmsg_data,
 {
 	const struct ucred *uc = cmsg_data;
 
-	PRINT_FIELD_D("{", *uc, pid);
+	PRINT_FIELD_TGID("{", *uc, pid, tcp);
 	PRINT_FIELD_UID(", ", *uc, uid);
 	PRINT_FIELD_UID(", ", *uc, gid);
 	tprints("}");
diff --git a/net.c b/net.c
index 9ea34b22..6e899253 100644
--- a/net.c
+++ b/net.c
@@ -601,7 +601,7 @@ print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
 	if (umoven_or_printaddr(tcp, addr, len, &uc))
 		return;
 
-	PRINT_FIELD_LEN("{", uc, pid, len, PRINT_FIELD_D);
+	PRINT_FIELD_LEN("{", uc, pid, len, PRINT_FIELD_TGID, tcp);
 	PRINT_FIELD_LEN(", ", uc, uid, len, PRINT_FIELD_UID);
 	PRINT_FIELD_LEN(", ", uc, gid, len, PRINT_FIELD_UID);
 	tprints("}");
diff --git a/netlink.c b/netlink.c
index 88d864a9..aeffdd9e 100644
--- a/netlink.c
+++ b/netlink.c
@@ -446,8 +446,9 @@ print_nlmsghdr(struct tcb *tcp,
 	decode_nlmsg_flags(nlmsghdr->nlmsg_flags,
 			   nlmsghdr->nlmsg_type, family);
 
-	tprintf(", seq=%u, pid=%d}", nlmsghdr->nlmsg_seq,
-		nlmsghdr->nlmsg_pid);
+	tprintf(", seq=%u, pid=", nlmsghdr->nlmsg_seq);
+	printpid(tcp, nlmsghdr->nlmsg_pid, PT_TGID);
+	tprintf("}");
 }
 
 static bool
diff --git a/numa.c b/numa.c
index cc7a7cc6..37b95397 100644
--- a/numa.c
+++ b/numa.c
@@ -44,7 +44,8 @@ print_nodemask(struct tcb *const tcp, const kernel_ulong_t addr,
 
 SYS_FUNC(migrate_pages)
 {
-	tprintf("%d, %" PRI_klu ", ", (int) tcp->u_arg[0], tcp->u_arg[1]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
 	print_nodemask(tcp, tcp->u_arg[2], tcp->u_arg[1]);
 	tprints(", ");
 	print_nodemask(tcp, tcp->u_arg[3], tcp->u_arg[1]);
@@ -170,7 +171,8 @@ SYS_FUNC(move_pages)
 	kernel_ulong_t buf;
 
 	if (entering(tcp)) {
-		tprintf("%d, %" PRI_klu ", ", (int) tcp->u_arg[0], npages);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprintf(", %" PRI_klu ", ", npages);
 		print_array(tcp, tcp->u_arg[2], npages, &buf, current_wordsize,
 			    tfetch_mem, print_addr, 0);
 		tprints(", ");
diff --git a/pidfd_open.c b/pidfd_open.c
index bbc7f617..10b5003d 100644
--- a/pidfd_open.c
+++ b/pidfd_open.c
@@ -10,7 +10,7 @@
 SYS_FUNC(pidfd_open)
 {
 	/* pid_t pid */
-	tprintf("%d", (int) tcp->u_arg[0]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
 
 	/* unsigned int flags */
 	tprintf(", %#x", (unsigned int) tcp->u_arg[1]);
diff --git a/print_fields.h b/print_fields.h
index 89499459..04d971a9 100644
--- a/print_fields.h
+++ b/print_fields.h
@@ -263,6 +263,30 @@
 		printfd((tcp_), (where_).field_);			\
 	} while (0)
 
+# define PRINT_FIELD_TID(prefix_, where_, field_, tcp_)			\
+	do {								\
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
+		printpid((tcp_), (where_).field_, PT_TID);			\
+	} while (0)
+
+# define PRINT_FIELD_TGID(prefix_, where_, field_, tcp_)			\
+	do {								\
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
+		printpid((tcp_), (where_).field_, PT_TGID);			\
+	} while (0)
+
+# define PRINT_FIELD_PGID(prefix_, where_, field_, tcp_)			\
+	do {								\
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
+		printpid((tcp_), (where_).field_, PT_PGID);			\
+	} while (0)
+
+# define PRINT_FIELD_SID(prefix_, where_, field_, tcp_)			\
+	do {								\
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
+		printpid((tcp_), (where_).field_, PT_SID);			\
+	} while (0)
+
 # define PRINT_FIELD_STRN(prefix_, where_, field_, len_, tcp_)		\
 	do {								\
 		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
diff --git a/process.c b/process.c
index a98c304e..3cce5735 100644
--- a/process.c
+++ b/process.c
@@ -92,7 +92,8 @@ SYS_FUNC(ptrace)
 		}
 
 		/* pid */
-		tprintf(", %d", pid);
+		tprintf(", ");
+		printpid(tcp, pid, PT_TGID);
 
 		/* addr */
 		switch (request) {
diff --git a/process_vm.c b/process_vm.c
index abee1e68..2d8855d8 100644
--- a/process_vm.c
+++ b/process_vm.c
@@ -13,7 +13,8 @@ SYS_FUNC(process_vm_readv)
 {
 	if (entering(tcp)) {
 		/* arg 1: pid */
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprintf(", ");
 	} else {
 		kernel_ulong_t local_iovcnt = tcp->u_arg[2];
 		kernel_ulong_t remote_iovcnt = tcp->u_arg[4];
@@ -42,7 +43,8 @@ SYS_FUNC(process_vm_writev)
 	kernel_ulong_t flags = tcp->u_arg[5];
 
 	/* arg 1: pid */
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprintf(", ");
 	/* arg 2: local iov */
 	tprint_iov(tcp, local_iovcnt, tcp->u_arg[1], IOV_DECODE_STR);
 	/* arg 3: local iovcnt */
diff --git a/resource.c b/resource.c
index 53192ee9..c0ae76d8 100644
--- a/resource.c
+++ b/resource.c
@@ -142,7 +142,8 @@ SYS_FUNC(setrlimit)
 SYS_FUNC(prlimit64)
 {
 	if (entering(tcp)) {
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprintf(", ");
 		printxval(resources, tcp->u_arg[1], "RLIMIT_???");
 		tprints(", ");
 		print_rlimit64(tcp, tcp->u_arg[2]);
@@ -179,10 +180,28 @@ SYS_FUNC(osf_getrusage)
 
 #include "xlat/priorities.h"
 
+static void
+priority_print_who(struct tcb *tcp, int which, int who)
+{
+	switch (which)
+	{
+	case PRIO_PROCESS:
+		printpid(tcp, who, PT_TGID);
+		break;
+	case PRIO_PGRP:
+		printpid(tcp, who, PT_PGID);
+		break;
+	default:
+		tprintf("%d", who);
+		break;
+	}
+}
+
 SYS_FUNC(getpriority)
 {
 	printxval(priorities, tcp->u_arg[0], "PRIO_???");
-	tprintf(", %d", (int) tcp->u_arg[1]);
+	tprintf(", ");
+	priority_print_who(tcp, tcp->u_arg[0], tcp->u_arg[1]);
 
 	return RVAL_DECODED;
 }
@@ -190,7 +209,9 @@ SYS_FUNC(getpriority)
 SYS_FUNC(setpriority)
 {
 	printxval(priorities, tcp->u_arg[0], "PRIO_???");
-	tprintf(", %d, %d", (int) tcp->u_arg[1], (int) tcp->u_arg[2]);
+	tprintf(", ");
+	priority_print_who(tcp, tcp->u_arg[0], tcp->u_arg[1]);
+	tprintf(", %d", (int) tcp->u_arg[2]);
 
 	return RVAL_DECODED;
 }
diff --git a/sched.c b/sched.c
index 788ef39b..7ae5abe2 100644
--- a/sched.c
+++ b/sched.c
@@ -21,7 +21,7 @@
 SYS_FUNC(sched_getscheduler)
 {
 	if (entering(tcp)) {
-		tprintf("%d", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
 	} else if (!syserror(tcp)) {
 		tcp->auxstr = xlookup(schedulers, (kernel_ulong_t) tcp->u_rval);
 		return RVAL_STR;
@@ -31,7 +31,8 @@ SYS_FUNC(sched_getscheduler)
 
 SYS_FUNC(sched_setscheduler)
 {
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprints(", ");
 	printxval(schedulers, tcp->u_arg[1], "SCHED_???");
 	tprints(", ");
 	printnum_int(tcp, tcp->u_arg[2], "%d");
@@ -41,16 +42,19 @@ SYS_FUNC(sched_setscheduler)
 
 SYS_FUNC(sched_getparam)
 {
-	if (entering(tcp))
-		tprintf("%d, ", (int) tcp->u_arg[0]);
-	else
+	if (entering(tcp)) {
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprints(", ");
+	} else {
 		printnum_int(tcp, tcp->u_arg[1], "%d");
+	}
 	return 0;
 }
 
 SYS_FUNC(sched_setparam)
 {
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprints(", ");
 	printnum_int(tcp, tcp->u_arg[1], "%d");
 
 	return RVAL_DECODED;
@@ -68,7 +72,8 @@ do_sched_rr_get_interval(struct tcb *const tcp,
 			 const print_obj_by_addr_fn print_ts)
 {
 	if (entering(tcp)) {
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprints(", ");
 	} else {
 		if (syserror(tcp))
 			printaddr(tcp->u_arg[1]);
@@ -160,7 +165,8 @@ end:
 SYS_FUNC(sched_setattr)
 {
 	if (entering(tcp)) {
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprints(", ");
 		print_sched_attr(tcp, tcp->u_arg[1], 0);
 	} else {
 		struct sched_attr attr;
@@ -179,7 +185,8 @@ SYS_FUNC(sched_setattr)
 SYS_FUNC(sched_getattr)
 {
 	if (entering(tcp)) {
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+		tprints(", ");
 	} else {
 		const unsigned int size = tcp->u_arg[2];
 
diff --git a/signal.c b/signal.c
index 3cb54bb3..27e0b9e8 100644
--- a/signal.c
+++ b/signal.c
@@ -439,7 +439,14 @@ SYS_FUNC(sigprocmask)
 SYS_FUNC(kill)
 {
 	/* pid */
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	int pid = (int) tcp->u_arg[0];
+	tprintf("%d", pid);
+	if (pid > 0)
+		printpid_translation(tcp, pid, PT_TGID);
+	else if (pid < -1)
+		printpid_translation(tcp, -pid, PT_PGID);
+
+	tprintf(", ");
 	/* signal */
 	printsignal(tcp->u_arg[1]);
 
@@ -448,8 +455,12 @@ SYS_FUNC(kill)
 
 SYS_FUNC(tgkill)
 {
-	/* tgid, tid */
-	tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
+	/* tgid */
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprintf(", ");
+	/* tid */
+	printpid(tcp, (int) tcp->u_arg[1], PT_TID);
+	tprintf(", ");
 	/* signal */
 	printsignal(tcp->u_arg[2]);
 
@@ -615,7 +626,8 @@ print_sigqueueinfo(struct tcb *const tcp, const int sig,
 
 SYS_FUNC(rt_sigqueueinfo)
 {
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprintf(", ");
 	print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 
 	return RVAL_DECODED;
@@ -623,7 +635,10 @@ SYS_FUNC(rt_sigqueueinfo)
 
 SYS_FUNC(rt_tgsigqueueinfo)
 {
-	tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
+	printpid(tcp, (int) tcp->u_arg[0], PT_TGID);
+	tprintf(", ");
+	printpid(tcp, (int) tcp->u_arg[1], PT_TID);
+	tprintf(", ");
 	print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
 
 	return RVAL_DECODED;
diff --git a/tests/kcmp.c b/tests/kcmp.c
index a46da0ed..dc5ba9a4 100644
--- a/tests/kcmp.c
+++ b/tests/kcmp.c
@@ -64,7 +64,26 @@ static const char zero_path[] = "/dev/zero";
 static void
 printpidfd(const char *prefix, pid_t pid, unsigned fd)
 {
-	printf("%s%d", prefix, fd);
+	const char *path = NULL;
+
+# if VERBOSE_FD
+	if (pid == getpid()) {
+		switch (fd)
+		{
+		case NULL_FD:
+			path = null_path;
+			break;
+		case ZERO_FD:
+			path = zero_path;
+			break;
+		}
+	}
+# endif
+
+	if (path)
+		printf("%s%d<%s>", prefix, fd, path);
+	else
+		printf("%s%d", prefix, fd);
 }
 
 /*
@@ -179,7 +198,7 @@ main(void)
 	/* KCMP_FILE is the only type which has additional args */
 	do_kcmp(3141592653U, 2718281828U, ARG_STR(KCMP_FILE), bogus_idx1,
 		bogus_idx2);
-	do_kcmp(-1, -1, ARG_STR(KCMP_FILE), NULL_FD, ZERO_FD);
+	do_kcmp(getpid(), getpid(), ARG_STR(KCMP_FILE), NULL_FD, ZERO_FD);
 
 	/* Types without additional args */
 	do_kcmp(-1, -1, ARG_STR(KCMP_VM), bogus_idx1, bogus_idx2);
@@ -198,7 +217,7 @@ main(void)
 	for (i = 0; i < ARRAY_SIZE(slot_data); i++) {
 		memcpy(slot, slot_data + i, sizeof(*slot));
 
-		do_kcmp(getpid(), getppid(), ARG_STR(KCMP_EPOLL_TFD), NULL_FD,
+		do_kcmp(getpid(), -1, ARG_STR(KCMP_EPOLL_TFD), NULL_FD,
 			(uintptr_t) slot, 1);
 	}
 
diff --git a/util.c b/util.c
index 286c6903..094e5818 100644
--- a/util.c
+++ b/util.c
@@ -392,6 +392,18 @@ printnum_fd(struct tcb *const tcp, const kernel_ulong_t addr)
 	return true;
 }
 
+bool
+printnum_pid(struct tcb *const tcp, const kernel_ulong_t addr, enum pid_type type)
+{
+	int pid;
+	if (umove_or_printaddr(tcp, addr, &pid))
+		return false;
+	tprints("[");
+	printpid(tcp, pid, type);
+	tprints("]");
+	return true;
+}
+
 /**
  * Prints time to a (static internal) buffer and returns pointer to it.
  * Returns NULL if the provided time specification is not correct.
@@ -641,12 +653,8 @@ printed:
 void
 printfd_pid_tracee_ns(struct tcb *tcp, pid_t pid, int fd)
 {
-	/*
-	 * TODO: We want to have the same formatting as printfd here,
-	 *       but we should figure out first which process in strace's
-	 *       PID NS is referred to by pid in tracee's PID NS.
-	 */
-	tprintf("%d", fd);
+	int strace_pid = translate_pid(tcp, pid, PT_TGID, NULL);
+	printfd_pid(tcp, strace_pid, fd);
 }
 
 /*
diff --git a/wait.c b/wait.c
index 2b9f9807..708b0b48 100644
--- a/wait.c
+++ b/wait.c
@@ -86,8 +86,13 @@ printwaitn(struct tcb *const tcp,
 		 * for example, wait4(4294967295, ...) instead of -1
 		 * in strace. We have to use int here, not long.
 		 */
-		int pid = tcp->u_arg[0];
-		tprintf("%d, ", pid);
+		int pid = (int) tcp->u_arg[0];
+		tprintf("%d", pid);
+		if (pid > 0)
+			printpid_translation(tcp, pid, PT_TGID);
+		else if (pid < -1)
+			printpid_translation(tcp, -pid, PT_PGID);
+		tprintf(", ");
 	} else {
 		int status;
 
@@ -108,7 +113,7 @@ printwaitn(struct tcb *const tcp,
 				printaddr(tcp->u_arg[3]);
 		}
 	}
-	return 0;
+	return RVAL_TGID;
 }
 
 SYS_FUNC(waitpid)
@@ -134,10 +139,28 @@ SYS_FUNC(osf_wait4)
 
 SYS_FUNC(waitid)
 {
+	unsigned int idtype = (unsigned int) tcp->u_arg[0];
+	int id = (int) tcp->u_arg[1];
+
 	if (entering(tcp)) {
-		printxval(waitid_types, tcp->u_arg[0], "P_???");
-		int pid = tcp->u_arg[1];
-		tprintf(", %d, ", pid);
+		printxval(waitid_types, idtype, "P_???");
+		tprintf(", ");
+		switch (idtype)
+		{
+		case P_PID:
+			printpid(tcp, id, PT_TGID);
+			break;
+		case P_PIDFD:
+			printfd(tcp, id);
+			break;
+		case P_PGID:
+			printpid(tcp, id, PT_PGID);
+			break;
+		default:
+			tprintf("%d", id);
+			break;
+		}
+		tprintf(", ");
 	} else {
 		/* siginfo */
 		printsiginfo_at(tcp, tcp->u_arg[2]);
-- 
2.27.0



More information about the Strace-devel mailing list