[PATCH v3 6/7] Extend -Y option to decode PIDS in arguments and retvals

Masatake YAMATO yamato at redhat.com
Thu Sep 9 13:43:36 UTC 2021


This change extends -Y,--decode-pids=comm option.  In addition to
what printed -Y option, strace with this change prints command
names for PIDS appeared in arguments and retvals.

An example:

  getppid()                  = 3781395<strace>
  getpid()                   = 3781398<a.out>
  pidfd_open(1<systemd>, 0)  = 3<pid:1<systemd>>

* src/defs.h (maybe_printpid_comm): New function declaration.
* src/strace.c (maybe_printpid_comm): New function printing
command name for given PID.
(usage): Update the help message for the option.
* src/pidns.c (printpid_translation): call `maybe_printpid_comm'.
* src/filter_qualify.c (qualify_decode_pid): Handle "all" specified in
--decode-pids=all.
* doc/strace.1.in (.SS Output format): updated the description
for the option.

NOTE: The code printing a pid in printclockname() is not changed in
this commit. CLOCKID_TO_FD macro used in printclockname() was remove
from the kernel. So printclockname() may change drastically in the
near future.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 doc/strace.1.in |  4 ++--
 src/defs.h      |  2 ++
 src/pidns.c     |  2 ++
 src/strace.c    | 15 ++++++++++++++-
 4 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/doc/strace.1.in b/doc/strace.1.in
index b15f1178d..259c7bfae 100644
--- a/doc/strace.1.in
+++ b/doc/strace.1.in
@@ -1090,8 +1090,8 @@ option.
 .B \-Y
 .TQ
 .BR \-\-decode\-pids = comm
-Print command names for PIDs that appear as the prefixes of trace lines.
-Specifying comm implies enabling pidns.
+Print command names for PIDs that appear in trace lines
+(prefix, arguments, and retvals.)
 .TP
 .B \-\-pidns\-translation
 If strace and tracee are in different PID namespaces, print PIDs in
diff --git a/src/defs.h b/src/defs.h
index 71a40f88e..4092d2053 100644
--- a/src/defs.h
+++ b/src/defs.h
@@ -1938,4 +1938,6 @@ print_big_u64_addr(const uint64_t addr)
 # endif
 
 void maybe_load_task_comm(struct tcb *tcp);
+/* print the content of /proc/$pid/comm. */
+void maybe_printpid_comm(int pid);
 #endif /* !STRACE_DEFS_H */
diff --git a/src/pidns.c b/src/pidns.c
index eda87aad9..821d03424 100644
--- a/src/pidns.c
+++ b/src/pidns.c
@@ -531,6 +531,8 @@ printpid_translation(struct tcb *tcp, int pid, enum pid_type type)
 		return;
 
 	int strace_pid = translate_pid(tcp, pid, type, NULL);
+	if (strace_pid && (type == PT_TID || type == PT_TGID))
+		maybe_printpid_comm(strace_pid);
 	if (strace_pid && strace_pid != pid)
 		tprintf_comment("%d in strace's PID NS", strace_pid);
 }
diff --git a/src/strace.c b/src/strace.c
index 76ff43d22..fd19dc2e8 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -418,7 +418,7 @@ Output format:\n\
                  print PIDs in strace's namespace, too\n\
   -Y, --decode-pids=comm\n\
                  print command names associated with the pids appeared\n\
-                 as prefixes of trace lines\n\
+                 in trace lines\n\
 "
 #ifdef ENABLE_SECONTEXT
 "\
@@ -941,6 +941,19 @@ load_pid_comm(int pid, char *buf, size_t buf_size)
 	fclose(fp);
 }
 
+void
+maybe_printpid_comm(int pid)
+{
+	if (!(pid_decoding & PID_DECODING_COMM))
+		return;
+
+	char buf[PROC_COMM_LEN];
+	load_pid_comm(pid, buf, sizeof(buf));
+	tprints("<");
+	tprints(buf);
+	tprints(">");
+}
+
 void
 maybe_load_task_comm(struct tcb *tcp)
 {
-- 
2.31.1



More information about the Strace-devel mailing list