[PATCH 2/3] Implement -Y option for printing command names for PIDs

Masatake YAMATO yamato at redhat.com
Wed Aug 18 11:00:41 UTC 2021


From: <yamato at redhat.com>

From: Masatake YAMATO <yamato at redhat.com>

In some applications like qemu-kvm, threads change their
command names to indicate their roles in the application.
Users can access the command name via /proc/$pid/comm.
The command name can be a great hit to understand the application
behavior.

This change introduces -Y option that enables printing command names
where PIDs appear.

For example (without -Y option):

[pid 254329] ioctl(18, KVM_RUN <unfinished ...>
[pid 254332] ioctl(21, KVM_RUN <unfinished ...>
[pid 254331] ioctl(20, KVM_RUN <unfinished ...>
[pid 254330] ioctl(19, KVM_RUN <unfinished ...>
[pid 254309] poll([{fd=9, events=POLLOUT}], 1, 0) = 1 ...
[pid 254309] poll([{fd=9, events=POLLOUT}], 1, 0) = 1 ...

(with -Y option):

[pid 254329<CPU 0/KVM>] ioctl(18, KVM_RUN <unfinished ...>
[pid 254332<CPU 3/KVM>] ioctl(21, KVM_RUN <unfinished ...>
[pid 254331<CPU 2/KVM>] ioctl(20, KVM_RUN <unfinished ...>
[pid 254330<CPU 1/KVM>] ioctl(19, KVM_RUN <unfinished ...>
[pid 254309<qemu-system-x86>] poll([{fd=9, events=POLLOUT}], 1, 0) = 1 ...
[pid 254309<qemu-system-x86>] poll([{fd=9, events=POLLOUT}], 1, 0) = 1 ...

This change considers only PIDs in the prefixes of trace lines.
Printing command names for PIDs in arguments and return values is
future work.

* src/strace.c (usage): Add help message for the -Y option.
(printleader): Print the command name for the given tcb.
(init): Add -Y and --decode-pids options. Set `decode_pids_enabled'
when one of the options is given.
* doc/strace.1.in (.SS Output format): document the -Y option.
* NEWS: document the -Y option.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 NEWS            |  1 +
 doc/strace.1.in |  5 +++++
 src/strace.c    | 24 +++++++++++++++++++-----
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index 53ea3e3bf..04e929ca6 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ Noteworthy changes in release ?.?? (????-??-??)
   * Enhanced decoding of MDBA_ROUTER_PATTR_* mdb router port netlink attributes.
   * Updated lists of BPF_*, IORING_*, MADV_*, MOUNT_ATTR_*, SCTP_*,
     and UFFD_* constants.
+  * Implemented -Y,--decode-pids option for printing command names for PIDs
 
 * Bug fixes
   * Fixed build using bionic libc.
diff --git a/doc/strace.1.in b/doc/strace.1.in
index 003e9e58e..bf84109b6 100644
--- a/doc/strace.1.in
+++ b/doc/strace.1.in
@@ -1082,6 +1082,11 @@ protocol-specific information associated with socket file descriptors,
 block/character device number associated with device file descriptors,
 and PIDs associated with pidfd file descriptors.
 .TP
+.B \-Y
+.TQ
+.BR \-\-decode\-pids
+Print command names for PIDs that appear as the prefixes of trace lines.
+.TP
 .B \-\-pidns\-translation
 If strace and tracee are in different PID namespaces, print PIDs in
 strace's namespace, too.
diff --git a/src/strace.c b/src/strace.c
index fd854105a..9a8555162 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -417,6 +417,8 @@ Output format:\n\
   -yy, --decode-fds=all\n\
                  print all available information associated with file\n\
                  descriptors in addition to paths\n\
+  -Y, --decode-pids\n\
+                 print command names associated with the pids\n\
 "
 #ifdef ENABLE_SECONTEXT
 "\
@@ -799,10 +801,18 @@ printleader(struct tcb *tcp)
 	set_current_tcp(tcp);
 	current_tcp->curcol = 0;
 
-	if (print_pid_pfx)
-		tprintf("%-5d ", tcp->pid);
-	else if (nprocs > 1 && !outfname)
-		tprintf("[pid %5u] ", tcp->pid);
+	if (print_pid_pfx) {
+		if (decode_pids_enabled)
+			tprintf("%-5d<%s> ", tcp->pid, tcp->comm);
+		else
+			tprintf("%-5d ", tcp->pid);
+	}
+	else if (nprocs > 1 && !outfname) {
+		if (decode_pids_enabled)
+			tprintf("[pid %5u<%s>] ", tcp->pid, tcp->comm);
+		else
+			tprintf("[pid %5u] ", tcp->pid);
+	}
 
 #ifdef ENABLE_SECONTEXT
 	char *context;
@@ -2094,7 +2104,7 @@ init(int argc, char *argv[])
 	qualify_signals("all");
 
 	static const char optstring[] =
-		"+a:Ab:cCdDe:E:fFhiI:kno:O:p:P:qrs:S:tTu:U:vVwxX:yzZ";
+		"+a:Ab:cCdDe:E:fFhiI:kno:O:p:P:qrs:S:tTu:U:vVwxX:yYzZ";
 
 	enum {
 		GETOPT_SECCOMP = 0x100,
@@ -2158,6 +2168,7 @@ init(int argc, char *argv[])
 		{ "summary-wall-clock", no_argument,	   0, 'w' },
 		{ "strings-in-hex",	optional_argument, 0, GETOPT_HEX_STR },
 		{ "const-print-style",	required_argument, 0, 'X' },
+		{ "decode-pids",	optional_argument, 0, 'Y' },
 		{ "pidns-translation",	no_argument      , 0, GETOPT_PIDNS_TRANSLATION },
 		{ "successful-only",	no_argument,	   0, 'z' },
 		{ "failed-only",	no_argument,	   0, 'Z' },
@@ -2378,6 +2389,9 @@ init(int argc, char *argv[])
 		case 'y':
 			yflag_short++;
 			break;
+		case 'Y':
+			decode_pids_enabled = true;
+			break;
 		case GETOPT_PIDNS_TRANSLATION:
 			pidns_translation++;
 			break;
-- 
2.31.1



More information about the Strace-devel mailing list