From b70d845dad672fb35f786fc8a314ffaf48283305 Mon Sep 17 00:00:00 2001 From: Érico Nogueira Date: Wed, 9 Jun 2021 16:56:14 -0300 Subject: extrace: add option to suppress runtime errors. Errors like "process vanished ..." can fill the screen when attempting to run a certain process under extrace, and redirecting all of stderr to /dev/null will hide that process's output. Therefore, we need an option in extrace to hide them. --- extrace.1 | 4 +++- extrace.c | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/extrace.1 b/extrace.1 index 7cec42d..3a36882 100644 --- a/extrace.1 +++ b/extrace.1 @@ -6,7 +6,7 @@ .Nd trace exec() calls system-wide .Sh SYNOPSIS .Nm -.Op Fl deflqtu +.Op Fl deflqQtu .Op Fl o Ar file .Op Fl p Ar pid | cmd\ ... .Sh DESCRIPTION @@ -33,6 +33,8 @@ is shown. Suppress printing of .Xr exec 3 arguments. +.It Fl Q +Suppress printing of runtime errors. .It Fl t Also display process exit status and duration. .It Fl u diff --git a/extrace.c b/extrace.c index 5e01efc..7f11191 100644 --- a/extrace.c +++ b/extrace.c @@ -3,7 +3,7 @@ * Requires CONFIG_CONNECTOR=y and CONFIG_PROC_EVENTS=y. * Requires root or "setcap cap_net_admin+ep extrace". * - * Usage: extrace [-deflqu] [-o FILE] [-p PID|CMD...] + * Usage: extrace [-deflqQu] [-o FILE] [-p PID|CMD...] * default: show all exec(), globally * -p PID only show exec() descendant of PID * CMD... run CMD... and only show exec() descendant of it @@ -13,6 +13,7 @@ * -f flat output: no indentation * -l print full path of argv[0] * -q don't print exec() arguments + * -Q don't print error messages * -u print user of process * * Copyright (C) 2014-2019 Leah Neukirchen @@ -66,6 +67,7 @@ #include #include #include +#include #include #include #include @@ -94,6 +96,7 @@ int flat = 0; int run = 0; int full_path = 0; int show_args = 1; +int show_errors = 1; int show_cwd = 0; int show_env = 0; int show_exit = 0; @@ -111,6 +114,16 @@ struct { char cmdline[CMDLINE_DB_MAX]; } pid_db[PID_DB_SIZE]; +static void +print_runtime_error(const char* fmt, ...) { + va_list ap; + if (show_errors) { + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + } +} + static int open_proc_dir(pid_t pid) { char name[48]; @@ -303,7 +316,7 @@ handle_msg(struct cn_msg *cn_hdr) int i = 0; int proc_dir_fd = open_proc_dir(pid); if (proc_dir_fd < 0) { - fprintf(stderr, + print_runtime_error( "extrace: process vanished before notification: pid %d\n", pid); return; @@ -330,11 +343,11 @@ handle_msg(struct cn_msg *cn_hdr) d = pid_depth(pid); if (d < 0) { if (*cmdline) { - fprintf(stderr, + print_runtime_error( "extrace: process vanished before we found its parent: pid %d: %s\n", pid, cmdline); } else { - fprintf(stderr, + print_runtime_error( "extrace: process vanished without a name: pid %d\n", pid); } @@ -347,7 +360,7 @@ handle_msg(struct cn_msg *cn_hdr) if (pid_db[i].pid == 0) break; if (i == PID_DB_SIZE - 1) - fprintf(stderr, "extrace: warning: pid_db of " + print_runtime_error("extrace: warning: pid_db of " "size %d overflowed\n", PID_DB_SIZE); pid_db[i].pid = pid; @@ -482,7 +495,7 @@ main(int argc, char *argv[]) output = stdout; - while ((opt = getopt(argc, argv, "+deflo:p:qtwu")) != -1) + while ((opt = getopt(argc, argv, "+deflo:p:qQtwu")) != -1) switch (opt) { case 'd': show_cwd = 1; break; case 'e': show_env = 1; break; @@ -490,6 +503,7 @@ main(int argc, char *argv[]) case 'l': full_path = 1; break; case 'p': parent = parse_pid(optarg); break; case 'q': show_args = 0; break; + case 'Q': show_errors = 0; break; case 't': show_exit = 1; break; case 'o': output = fopen(optarg, "w"); @@ -505,7 +519,7 @@ main(int argc, char *argv[]) if (parent != 1 && optind != argc) { usage: - fprintf(stderr, "Usage: extrace [-deflqt] [-o FILE] [-p PID|CMD...]\n"); + fprintf(stderr, "Usage: extrace [-deflqQt] [-o FILE] [-p PID|CMD...]\n"); exit(1); } @@ -593,7 +607,7 @@ usage: if (last_seq[cproc->cpu] && cmsg->seq != last_seq[cproc->cpu] + 1) - fprintf(stderr, + print_runtime_error( "extrace: out of order message on cpu %d\n", cproc->cpu); last_seq[cproc->cpu] = cmsg->seq; -- cgit 1.4.1