[RFC PATCH v3 12/14] Use printpid in decoders

Ákos Uzonyi uzonyi.akos at gmail.com
Sat Jun 13 11:25:34 UTC 2020


* pidns.c (printpid_translation): New function.
(printpid): Rewritten using printpid_translation.
* getpid.c: New file.
* Makefile.am (libstrace_a_SOURCES): Add getpid.c.
* defs.h (printpid_translation): New function definition.
(printnum_pid): Likewise
* 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.
* util.c: Likewise.
* wait.c: Likewise.
---
 Makefile.am       |  1 +
 affinity.c        |  6 ++++--
 block.c           |  2 +-
 bpf.c             |  2 +-
 capability.c      |  4 +++-
 clone.c           | 14 +++++++-------
 defs.h            |  4 ++++
 fcntl.c           | 16 ++++++++++------
 get_robust_list.c |  3 ++-
 getpid.c          | 44 ++++++++++++++++++++++++++++++++++++++++++++
 ioprio.c          | 26 ++++++++++++++++++++++----
 kcmp.c            |  5 ++++-
 msghdr.c          |  2 +-
 net.c             |  2 +-
 netlink.c         |  5 +++--
 numa.c            |  6 ++++--
 pidfd_open.c      |  2 +-
 pidns.c           | 11 ++++++++---
 print_fields.h    | 24 ++++++++++++++++++++++++
 printsiginfo.c    |  5 +++++
 process.c         |  3 ++-
 process_vm.c      |  6 ++++--
 resource.c        | 27 ++++++++++++++++++++++++---
 sched.c           | 25 ++++++++++++++++---------
 signal.c          | 25 ++++++++++++++++++++-----
 sockaddr.c        |  5 +++++
 util.c            | 12 ++++++++++++
 wait.c            | 35 +++++++++++++++++++++++++++++------
 28 files changed, 262 insertions(+), 60 deletions(-)
 create mode 100644 getpid.c

diff --git a/Makefile.am b/Makefile.am
index aa9a8d78..64c3d7de 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -136,6 +136,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 893269d0..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_U(", ", 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 469f809a..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_U("{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 cc8dae04..91cfb3f6 100644
--- a/defs.h
+++ b/defs.h
@@ -1083,6 +1083,7 @@ printfd(struct tcb *tcp, int fd)
  */
 extern void printfd_pid_tracee_ns(struct tcb *tcp, pid_t pid, int fd);
 extern void printpid(struct tcb *tcp, int pid, enum pid_type type);
+extern void printpid_translation(struct tcb *tcp, int pid, enum pid_type type);
 extern void print_sockaddr(const void *sa, int len);
 extern bool
 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
@@ -1532,6 +1533,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 0b4221ce..cc0b00ec 100644
--- a/fcntl.c
+++ b/fcntl.c
@@ -20,7 +20,7 @@
 #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)
 {
 	tprints("{l_type=");
 	printxval(lockfcmds, (unsigned short) fl->l_type, "F_???");
@@ -28,8 +28,10 @@ print_struct_flock64(const struct_kernel_flock64 *fl, const int getlk)
 	printxval(whence_codes, (unsigned short) fl->l_whence, "SEEK_???");
 	tprintf(", l_start=%" PRId64 ", l_len=%" PRId64,
 		(int64_t) fl->l_start, (int64_t) fl->l_len);
-	if (getlk)
-		tprintf(", l_pid=%lu", (unsigned long) fl->l_pid);
+	if (getlk) {
+		tprintf(", l_pid=");
+		printpid(tcp, (unsigned long) fl->l_pid, PT_TGID);
+	}
 	tprints("}");
 }
 
@@ -39,7 +41,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
@@ -48,7 +50,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
@@ -61,7 +63,9 @@ print_f_owner_ex(struct tcb *const tcp, const kernel_ulong_t addr)
 
 	tprints("{type=");
 	printxval(f_owner_types, owner.type, "F_OWNER_???");
-	tprintf(", pid=%d}", owner.pid);
+	tprintf(", pid=");
+	printpid(tcp, owner.pid, PT_TGID);
+	tprintf("}");
 }
 
 static int
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..84c3ecf3
--- /dev/null
+++ b/getpid.c
@@ -0,0 +1,44 @@
+#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;
+}
+
+SYS_FUNC(setpgrp)
+{
+	return RVAL_DECODED | RVAL_PGID;
+}
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/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/msghdr.c b/msghdr.c
index ec6e77f2..930d72bc 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_U("{", *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 4b4ef448..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=%u}", 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/pidns.c b/pidns.c
index 4bdec595..1c696034 100644
--- a/pidns.c
+++ b/pidns.c
@@ -614,12 +614,10 @@ get_proc_pid(struct tcb *tcp)
 }
 
 void
-printpid(struct tcb *tcp, int pid, enum pid_type type)
+printpid_translation(struct tcb *tcp, int pid, enum pid_type type)
 {
 	int strace_pid;
 
-	tprintf("%d", pid);
-
 	if (perform_ns_resolution) {
 		strace_pid = translate_pid(tcp, pid, type, NULL);
 
@@ -627,3 +625,10 @@ printpid(struct tcb *tcp, int pid, enum pid_type type)
 			tprintf_comment("%d in strace's PID NS", strace_pid);
 	}
 }
+
+void
+printpid(struct tcb *tcp, int pid, enum pid_type type)
+{
+	tprintf("%d", pid);
+	printpid_translation(tcp, pid, type);
+}
diff --git a/print_fields.h b/print_fields.h
index f5f26b46..9af16cab 100644
--- a/print_fields.h
+++ b/print_fields.h
@@ -236,6 +236,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 133bc24d..c2f70534 100644
--- a/printsiginfo.c
+++ b/printsiginfo.c
@@ -58,6 +58,11 @@
 static void
 printsigsource(const siginfo_t *sip)
 {
+	/*
+	TODO
+	We don't have a tcb here to translate this PID
+	Should I add a tcb parameter to every function in the call hiearchy?
+	*/
 	tprintf(", si_pid=%u, si_uid=%u",
 		(unsigned int) sip->si_pid,
 		(unsigned int) sip->si_uid);
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/sockaddr.c b/sockaddr.c
index b6b9aa7d..1d0e54b8 100644
--- a/sockaddr.c
+++ b/sockaddr.c
@@ -411,6 +411,11 @@ print_sockaddr_data_nl(const void *const buf, const int addrlen)
 {
 	const struct sockaddr_nl *const sa_nl = buf;
 
+	/*
+	TODO
+	We don't have a tcb here to translate this PID
+	Should I add a tcb parameter to sockaddr_printer?
+	*/
 	PRINT_FIELD_D("", *sa_nl, nl_pid);
 	PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
 }
diff --git a/util.c b/util.c
index 6d599e98..28d7ef4f 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.
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