[RFC PATCH v9 2/8] Use printpid in decoders

Ákos Uzonyi uzonyi.akos at gmail.com
Sun Aug 16 22:18:45 UTC 2020


* getpid.c: New file.
* Makefile.am (libstrace_a_SOURCES): Add it.
* 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.
(printfd_pid_tracee_ns): Update documentation
* 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.
---
 Makefile.am       |  1 +
 affinity.c        |  6 ++++--
 block.c           |  2 +-
 bpf.c             |  2 +-
 capability.c      |  4 +++-
 clone.c           | 14 +++++++-------
 defs.h            |  5 ++++-
 fcntl.c           | 24 ++++++++++++++++++++++--
 get_robust_list.c |  3 ++-
 getpid.c          | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 ioprio.c          | 26 ++++++++++++++++++++++----
 ipc_shmctl.c      |  4 ++--
 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 ++++++++++++++++++++++++
 printsiginfo.c    |  2 +-
 process.c         |  3 ++-
 process_vm.c      |  6 ++++--
 resource.c        | 27 ++++++++++++++++++++++++---
 sched.c           | 25 ++++++++++++++++---------
 signal.c          | 21 +++++++++++++++------
 sockaddr.c        |  2 +-
 tests/kcmp.c      | 25 ++++++++++++++++++++++---
 util.c            | 22 +++++++++++++++-------
 wait.c            | 36 ++++++++++++++++++++++++------------
 30 files changed, 278 insertions(+), 82 deletions(-)
 create mode 100644 getpid.c

diff --git a/Makefile.am b/Makefile.am
index 73c22035..15fcd9a6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -135,6 +135,7 @@ libstrace_a_SOURCES =	\
 	getcpu.c	\
 	getcwd.c	\
 	getpagesize.c \
+	getpid.c	\
 	getrandom.c	\
 	hdio.c		\
 	hostname.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 b527ef43..783a4b5a 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 af830053..0ec33ba4 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..812fb435 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);
+	tprints(", pid=");
+	printpid(tcp, h->pid, PT_TGID);
+	tprints("}");
 }
 
 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 511a92a2..328c1940 100644
--- a/defs.h
+++ b/defs.h
@@ -1100,7 +1100,7 @@ printfd(struct tcb *tcp, int fd)
 
 /**
  * Print file descriptor fd owned by process with ID pid (from the PID NS
- * of the tracee the descriptor tcp).  This is a stub.
+ * of the tracee).
  */
 extern void printfd_pid_tracee_ns(struct tcb *tcp, pid_t pid, int fd);
 
@@ -1561,6 +1561,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 6fcd5dac..5c630a06 100644
--- a/fcntl.c
+++ b/fcntl.c
@@ -28,7 +28,7 @@ print_struct_flock64(struct tcb *const tcp, const struct_kernel_flock64 *fl, con
 	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("}");
 }
 
@@ -59,7 +59,22 @@ 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);
+
+	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;
+	}
+	tprints(", pid=");
+	printpid(tcp, owner.pid, pid_type);
 	tprints("}");
 }
 
@@ -74,6 +89,9 @@ print_fcntl(struct tcb *tcp)
 		printflags(fdflags, tcp->u_arg[2], "FD_???");
 		break;
 	case F_SETOWN:
+		tprints(", ");
+		printpid_tgid_pgid(tcp, tcp->u_arg[2]);
+		break;
 	case F_SETPIPE_SZ:
 		tprintf(", %" PRI_kld, tcp->u_arg[2]);
 		break;
@@ -116,6 +134,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..81aba2dd 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, tcp->u_arg[0], PT_TID);
+		tprints(", ");
 	} 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..149ef3fd 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]);
+		tprints(", ");
+		ioprio_print_who(tcp, tcp->u_arg[0], 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]);
+	tprints(", ");
+	ioprio_print_who(tcp, tcp->u_arg[0], tcp->u_arg[1]);
+	tprints(", ");
 	/* 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..1f99f02a 100644
--- a/ipc_shmctl.c
+++ b/ipc_shmctl.c
@@ -53,8 +53,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..a5e1cdce 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);
+	tprints(", ");
+	printpid(tcp, pid2, PT_TGID);
+	tprints(", ");
 	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 ef6dc24b..170b8e51 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 fa4638e7..2099f0ac 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..cb29033a 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);
+	tprints("}");
 }
 
 static bool
diff --git a/numa.c b/numa.c
index cc7a7cc6..f7a58266 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, 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, 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..cf05a110 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, 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 f9714115..33d4a458 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/printsiginfo.c b/printsiginfo.c
index 8ed1e7ba..cb4cde4a 100644
--- a/printsiginfo.c
+++ b/printsiginfo.c
@@ -58,7 +58,7 @@
 static void
 printsigsource(struct tcb *tcp, const siginfo_t *sip)
 {
-	PRINT_FIELD_D(", ", *sip, si_pid);
+	PRINT_FIELD_TGID(", ", *sip, si_pid, tcp);
 	PRINT_FIELD_UID(", ", *sip, si_uid);
 }
 
diff --git a/process.c b/process.c
index a98c304e..d6ff255d 100644
--- a/process.c
+++ b/process.c
@@ -92,7 +92,8 @@ SYS_FUNC(ptrace)
 		}
 
 		/* pid */
-		tprintf(", %d", pid);
+		tprints(", ");
+		printpid(tcp, pid, PT_TGID);
 
 		/* addr */
 		switch (request) {
diff --git a/process_vm.c b/process_vm.c
index abee1e68..81d9b0f4 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, tcp->u_arg[0], PT_TGID);
+		tprints(", ");
 	} 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, tcp->u_arg[0], PT_TGID);
+	tprints(", ");
 	/* 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..070f4740 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, tcp->u_arg[0], PT_TGID);
+		tprints(", ");
 		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]);
+	tprints(", ");
+	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]);
+	tprints(", ");
+	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..ff427e48 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, 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, 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, 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, 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, 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, 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, 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 d5d59105..49b4ebd5 100644
--- a/signal.c
+++ b/signal.c
@@ -439,7 +439,8 @@ SYS_FUNC(sigprocmask)
 SYS_FUNC(kill)
 {
 	/* pid */
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid_tgid_pgid(tcp, tcp->u_arg[0]);
+	tprints(", ");
 	/* signal */
 	printsignal(tcp->u_arg[1]);
 
@@ -448,7 +449,7 @@ SYS_FUNC(kill)
 
 SYS_FUNC(tkill)
 {
-	tprintf("%d", (int) tcp->u_arg[0]);
+	printpid(tcp, tcp->u_arg[0], PT_TID);
 	tprints(", ");
 	printsignal(tcp->u_arg[1]);
 
@@ -457,8 +458,12 @@ SYS_FUNC(tkill)
 
 SYS_FUNC(tgkill)
 {
-	/* tgid, tid */
-	tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
+	/* tgid */
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+	tprints(", ");
+	/* tid */
+	printpid(tcp, tcp->u_arg[1], PT_TID);
+	tprints(", ");
 	/* signal */
 	printsignal(tcp->u_arg[2]);
 
@@ -624,7 +629,8 @@ print_sigqueueinfo(struct tcb *const tcp, const int sig,
 
 SYS_FUNC(rt_sigqueueinfo)
 {
-	tprintf("%d, ", (int) tcp->u_arg[0]);
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+	tprints(", ");
 	print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 
 	return RVAL_DECODED;
@@ -632,7 +638,10 @@ SYS_FUNC(rt_sigqueueinfo)
 
 SYS_FUNC(rt_tgsigqueueinfo)
 {
-	tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
+	printpid(tcp, tcp->u_arg[0], PT_TGID);
+	tprints(", ");
+	printpid(tcp, tcp->u_arg[1], PT_TID);
+	tprints(", ");
 	print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
 
 	return RVAL_DECODED;
diff --git a/sockaddr.c b/sockaddr.c
index b0046331..fb8887c2 100644
--- a/sockaddr.c
+++ b/sockaddr.c
@@ -416,7 +416,7 @@ print_sockaddr_data_nl(struct tcb *tcp, const void *const buf, const int addrlen
 {
 	const struct sockaddr_nl *const sa_nl = buf;
 
-	PRINT_FIELD_D("", *sa_nl, nl_pid);
+	PRINT_FIELD_TGID("", *sa_nl, nl_pid, tcp);
 	PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
 }
 
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..2568021e 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.
@@ -616,7 +628,7 @@ void
 printfd_pid(struct tcb *tcp, pid_t pid, int fd)
 {
 	char path[PATH_MAX + 1];
-	if (!number_set_array_is_empty(decode_fd_set, 0)
+	if (pid > 0 && !number_set_array_is_empty(decode_fd_set, 0)
 	    && getfdpath_pid(pid, fd, path, sizeof(path)) >= 0) {
 		tprintf("%d<", (int) fd);
 		if (is_number_in_set(DECODE_FD_SOCKET, decode_fd_set) &&
@@ -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..177427c8 100644
--- a/wait.c
+++ b/wait.c
@@ -80,14 +80,8 @@ printwaitn(struct tcb *const tcp,
 	   void (*const print_rusage)(struct tcb *, kernel_ulong_t))
 {
 	if (entering(tcp)) {
-		/* On Linux, kernel-side pid_t is typedef'ed to int
-		 * on all arches. Also, glibc-2.8 truncates wait3 and wait4
-		 * pid argument to int on 64bit arches, producing,
-		 * 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);
+		printpid_tgid_pgid(tcp, tcp->u_arg[0]);
+		tprintf(", ");
 	} else {
 		int status;
 
@@ -108,7 +102,7 @@ printwaitn(struct tcb *const tcp,
 				printaddr(tcp->u_arg[3]);
 		}
 	}
-	return 0;
+	return RVAL_TGID;
 }
 
 SYS_FUNC(waitpid)
@@ -134,10 +128,28 @@ SYS_FUNC(osf_wait4)
 
 SYS_FUNC(waitid)
 {
+	unsigned int idtype = (unsigned int) tcp->u_arg[0];
+	int id = 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_???");
+		tprints(", ");
+		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;
+		}
+		tprints(", ");
 	} else {
 		/* siginfo */
 		printsiginfo_at(tcp, tcp->u_arg[2]);
-- 
2.28.0



More information about the Strace-devel mailing list