diff options
author | Masatake YAMATO <yamato@redhat.com> | 2021-09-09 22:43:36 +0900 |
---|---|---|
committer | Dmitry V. Levin <ldv@strace.io> | 2021-11-14 08:00:00 +0000 |
commit | cb1650559273daa41fef08ab91da35860d1cf26f (patch) | |
tree | 07a2114bb9721d065bf505cd7dc8caf9ea879c4b | |
parent | 8cebeebcb587d557f1a870c4c544d308fb675362 (diff) | |
download | strace-cb1650559273daa41fef08ab91da35860d1cf26f.tar.gz |
Extend -Y option to decode PIDs in arguments and return values
This change extends -Y/--decode-pids=comm option. In addition to the
information printed before this change, command names would be printed
also for PIDs appeared in syscall arguments and return values.
For example:
getppid() = 3781395<strace>
getpid() = 3781398<a.out>
pidfd_open(1<systemd>, 0) = 3<pid:1<systemd>>
NOTE: The code printing the pid in printclockname() is not changed in
this commit. CLOCKID_TO_FD macro used in printclockname() was removed
from the kernel, so printclockname() may change drastically in the
near future.
* src/defs.h (maybe_printpid_comm): New function declaration.
* src/strace.c (maybe_printpid_comm): New function printing
command name for the given PID.
* src/pidns.c (printpid_translation): Call `maybe_printpid_comm'.
* tests/strace--decode-pids-comm.c (do_default_action): Update expected
output.
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Co-authored-by: Dmitry V. Levin <ldv@strace.io>
-rw-r--r-- | src/defs.h | 2 | ||||
-rw-r--r-- | src/pidns.c | 18 | ||||
-rw-r--r-- | src/strace.c | 13 | ||||
-rw-r--r-- | tests/strace--decode-pids-comm.c | 14 |
4 files changed, 35 insertions, 12 deletions
diff --git a/src/defs.h b/src/defs.h index 49636b967..936609ae2 100644 --- a/src/defs.h +++ b/src/defs.h @@ -1492,6 +1492,8 @@ extern void kvm_vcpu_info_free(struct tcb *); # endif extern void maybe_load_task_comm(struct tcb *tcp); +/* Print the contents of /proc/$pid/comm. */ +extern void maybe_printpid_comm(int pid); static inline int printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len) diff --git a/src/pidns.c b/src/pidns.c index eda87aad9..9b8504f7c 100644 --- a/src/pidns.c +++ b/src/pidns.c @@ -527,12 +527,18 @@ get_proc_pid(int pid) static void printpid_translation(struct tcb *tcp, int pid, enum pid_type type) { - if (!(pid_decoding & PID_DECODING_NS_TRANSLATION)) - return; - - int strace_pid = translate_pid(tcp, pid, type, NULL); - if (strace_pid && strace_pid != pid) - tprintf_comment("%d in strace's PID NS", strace_pid); + if ((pid_decoding & (PID_DECODING_COMM|PID_DECODING_NS_TRANSLATION))) { + int strace_pid = translate_pid(tcp, pid, type, NULL); + if (strace_pid) { + if ((pid_decoding & PID_DECODING_COMM) && + (type == PT_TID || type == PT_TGID)) + maybe_printpid_comm(strace_pid); + if ((pid_decoding & PID_DECODING_NS_TRANSLATION) && + strace_pid != pid) + tprintf_comment("%d in strace's PID NS", + strace_pid); + } + } } void diff --git a/src/strace.c b/src/strace.c index 950644040..38165a1a3 100644 --- a/src/strace.c +++ b/src/strace.c @@ -948,6 +948,19 @@ load_pid_comm(int pid, char *buf, size_t buf_size) } 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) { if (!(pid_decoding & PID_DECODING_COMM)) diff --git a/tests/strace--decode-pids-comm.c b/tests/strace--decode-pids-comm.c index eca990c1c..d5767853f 100644 --- a/tests/strace--decode-pids-comm.c +++ b/tests/strace--decode-pids-comm.c @@ -39,11 +39,13 @@ do_default_action(void) char comm[sizeof(NEW_NAME)]; if (prctl(PR_GET_NAME, comm)) perror_msg_and_skip("PR_GET_NAME"); + char ocomm[sizeof(comm)]; + strcpy(ocomm, comm); pid_t pid = getpid(); pid_t ppid = getppid(); - printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid); + printf("%-5d<%s> getppid() = %d<%s>\n", pid, comm, ppid, "strace"); fflush(stdout); pid_t child = fork(); @@ -52,18 +54,18 @@ do_default_action(void) else if (child == 0) { pid = getpid(); ppid = getppid(); - printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid); + printf("%-5d<%s> getppid() = %d<%s>\n", pid, comm, ppid, ocomm); strcpy(comm, NEW_NAME); prctl(PR_SET_NAME, comm); prctl(PR_GET_NAME, comm); ppid = getppid(); - printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid); + printf("%-5d<%s> getppid() = %d<%s>\n", pid, comm, ppid, ocomm); long rc = syscall(__NR_tgkill, ppid, ppid, SIGCONT); - printf("%-5d<%s> tgkill(%d, %d, SIGCONT) = %s\n", - pid, comm, ppid, ppid, sprintrc(rc)); + printf("%-5d<%s> tgkill(%d<%s>, %d<%s>, SIGCONT) = %s\n", + pid, comm, ppid, ocomm, ppid, ocomm, sprintrc(rc)); fflush(stdout); char *args[] = { (char *) "unused", (char *) "execve", NULL }; @@ -79,7 +81,7 @@ do_default_action(void) printf("%-5d<exe> +++ exited with 0 +++\n", child); ppid = getppid(); - printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid); + printf("%-5d<%s> getppid() = %d<%s>\n", pid, comm, ppid, "strace"); printf("%-5d<%s> +++ exited with 0 +++\n", pid, comm); return WEXITSTATUS(status); } |