[PATCH v3 2/7] Introduce --decode-pid=pidns as an alias for --pidns-translation

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


In preparation for the forthcoming -Y,--decode-pids=comm option.

Decoding pids has some aspects. --decode-pids is for covering them.

* src/defs.h (pid_decoding): Declare bit flags as replacement for
pidns_translation.
(pidns_translation): Remove.
(enum pid_decoding_flags): New enums specifying the flags.
(qualify_decode_pid): New function prototype.
* src/strace.c (pid_decoding): The bit flag definition.
(usage): Add help messages for --decode-pid=pidns.
(init) <enum>: Add GETOPT_QUAL_DECODE_PID.
<longopts> Add --decode-pids as a long option.
<case GETOPT_PIDNS_TRANSLATION>: call qualify_decode_pid
to update pid_decoding indirectly.
<case GETOPT_QUAL_DECODE_PID>: New condition.
* src/filter_qualify.c (qualify_decode_pid): New function.
* src/pidns (printpid_translation): Refer pid_decoding as
the replacement for pidns_translation.

Making --decode-pid option a super set of --pidns-translation option
is suggested by Eugene Syromyatnikov <evgsyr at gmail.com>.
Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 doc/strace.1.in      |  5 +++++
 src/defs.h           |  6 +++++-
 src/filter_qualify.c |  9 +++++++++
 src/pidns.c          |  2 +-
 src/strace.c         | 11 +++++++++--
 5 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/doc/strace.1.in b/doc/strace.1.in
index 36079c947..21495dba3 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
+.BR \-\-decode\-pids = pidns
+Same as
+.B "\-\-pidns\-translation"
+option.
+.TP
 .B \-\-pidns\-translation
 If strace and tracee are in different PID namespaces, print PIDs in
 strace's namespace, too.
diff --git a/src/defs.h b/src/defs.h
index 96f742460..fdc89579c 100644
--- a/src/defs.h
+++ b/src/defs.h
@@ -505,7 +505,10 @@ extern int Tflag_scale;
 extern int Tflag_width;
 extern bool iflag;
 extern bool count_wallclock;
-extern unsigned int pidns_translation;
+extern unsigned int pid_decoding;
+enum pid_decoding_flags {
+	PID_DECODING_NS_TRANSLATION = 1 << 0,
+};
 /* are we filtering traces based on paths? */
 extern struct path_set {
 	const char **paths_selected;
@@ -1286,6 +1289,7 @@ extern void qualify_signals(const char *);
 extern void qualify_status(const char *);
 extern void qualify_quiet(const char *);
 extern void qualify_decode_fd(const char *);
+extern void qualify_decode_pid(const char *);
 extern void qualify_read(const char *);
 extern void qualify_write(const char *);
 extern void qualify_fault(const char *);
diff --git a/src/filter_qualify.c b/src/filter_qualify.c
index ff66507fd..2d0dcc6f4 100644
--- a/src/filter_qualify.c
+++ b/src/filter_qualify.c
@@ -467,6 +467,15 @@ qualify_decode_fd(const char *const str)
 		       "decode-fds");
 }
 
+void
+qualify_decode_pid(const char *const str)
+{
+	if (strcmp(str, "pidns") == 0)
+		pid_decoding |= PID_DECODING_NS_TRANSLATION;
+	else
+		error_msg_and_die("invalid --decode-pids = argument: '%s'", str);
+}
+
 void
 qualify_trace(const char *const str)
 {
diff --git a/src/pidns.c b/src/pidns.c
index 9c7b7ce1a..eda87aad9 100644
--- a/src/pidns.c
+++ b/src/pidns.c
@@ -527,7 +527,7 @@ get_proc_pid(int pid)
 static void
 printpid_translation(struct tcb *tcp, int pid, enum pid_type type)
 {
-	if (!pidns_translation)
+	if (!(pid_decoding & PID_DECODING_NS_TRANSLATION))
 		return;
 
 	int strace_pid = translate_pid(tcp, pid, type, NULL);
diff --git a/src/strace.c b/src/strace.c
index 6c336c9d9..00a99f3c6 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -135,7 +135,7 @@ static unsigned int daemonized_tracer;
 static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
 #define use_seize (post_attach_sigstop == 0)
 
-unsigned int pidns_translation;
+unsigned int pid_decoding;
 
 static bool detach_on_execve;
 
@@ -414,6 +414,8 @@ Output format:\n\
   -yy, --decode-fds=all\n\
                  print all available information associated with file\n\
                  descriptors in addition to paths\n\
+  --decode-pids=pidns\n\
+                 print PIDs in strace's namespace, too\n\
 "
 #ifdef ENABLE_SECONTEXT
 "\
@@ -2085,6 +2087,7 @@ init(int argc, char *argv[])
 		GETOPT_QUAL_KVM,
 		GETOPT_QUAL_QUIET,
 		GETOPT_QUAL_DECODE_FD,
+		GETOPT_QUAL_DECODE_PID,
 	};
 	static const struct option longopts[] = {
 		{ "columns",		required_argument, 0, 'a' },
@@ -2146,6 +2149,7 @@ init(int argc, char *argv[])
 		{ "silent",	optional_argument, 0, GETOPT_QUAL_QUIET },
 		{ "silence",	optional_argument, 0, GETOPT_QUAL_QUIET },
 		{ "decode-fds",	optional_argument, 0, GETOPT_QUAL_DECODE_FD },
+		{ "decode-pids",optional_argument, 0, GETOPT_QUAL_DECODE_PID },
 
 		{ 0, 0, 0, 0 }
 	};
@@ -2343,7 +2347,7 @@ init(int argc, char *argv[])
 			yflag_short++;
 			break;
 		case GETOPT_PIDNS_TRANSLATION:
-			pidns_translation++;
+			qualify_decode_pid("pidns");
 			break;
 		case 'z':
 			clear_number_set_array(status_set, 1);
@@ -2408,6 +2412,9 @@ init(int argc, char *argv[])
 		case GETOPT_QUAL_DECODE_FD:
 			qualify_decode_fd(optarg ?: yflag_qual);
 			break;
+		case GETOPT_QUAL_DECODE_PID:
+			qualify_decode_pid(optarg);
+			break;
 		default:
 			error_msg_and_help(NULL);
 			break;
-- 
2.31.1



More information about the Strace-devel mailing list