From 783dc9ce1c729ce171a4a460703bdd413bce6364 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 30 May 2020 22:58:04 +0200 Subject: Do not print PID numbers if only configured to trace one thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- common.h | 2 +- sctrace.c | 5 +++-- util.c | 14 +++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/common.h b/common.h index c4de909..996b9ef 100644 --- a/common.h +++ b/common.h @@ -112,6 +112,6 @@ struct process *add_process(pid_t pid, unsigned long int trace_options); void remove_process(struct process *proc); /* util.c */ -void set_trace_output(FILE *fp); +void setup_trace_output(FILE *fp, int multiprocess); void tprintf(struct process *proc, const char *fmt, ...); _Noreturn void eprintf(const char *fmt, ...); diff --git a/sctrace.c b/sctrace.c index 0ab561c..f027a77 100644 --- a/sctrace.c +++ b/sctrace.c @@ -99,7 +99,7 @@ main(int argc, char **argv) char *outfile = NULL; FILE *outfp = stderr; const char *num = NULL; - int status, exit_value = 0, trace_event, with_argv0 = 0; + int status, exit_value = 0, trace_event, with_argv0 = 0, multiprocess = 0; unsigned long int trace_options = PTRACE_O_EXITKILL | PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC; struct process *proc, *proc2; unsigned long int event; @@ -118,6 +118,7 @@ main(int argc, char **argv) case 'f': trace_options |= PTRACE_O_TRACEFORK; trace_options |= PTRACE_O_TRACEVFORK; + multiprocess = 1; break; default: usage(); @@ -184,7 +185,7 @@ main(int argc, char **argv) } have_outfp: - set_trace_output(outfp); + setup_trace_output(outfp, multiprocess); for (;;) { pid = wait(&status); diff --git a/util.c b/util.c index 76a8a36..7ee9c51 100644 --- a/util.c +++ b/util.c @@ -5,11 +5,13 @@ static FILE *trace_fp; static char last_char = '\n'; static pid_t last_pid = 0; +static int multiproctrace; void -set_trace_output(FILE *fp) +setup_trace_output(FILE *fp, int multiprocess) { + multiproctrace = multiprocess; trace_fp = fp; } @@ -22,10 +24,12 @@ tprintf(struct process *proc, const char *fmt, ...) last_pid = 0; fmt = &fmt[1]; } - if (last_char == '\n') - fprintf(trace_fp, "[%ju] ", (uintmax_t)proc->pid); - else if (proc->pid != last_pid) - fprintf(trace_fp, "\n[%ju] ", (uintmax_t)proc->pid); + if (multiproctrace) { + if (last_char == '\n') + fprintf(trace_fp, "[%ju] ", (uintmax_t)proc->pid); + else if (proc->pid != last_pid) + fprintf(trace_fp, "\n[%ju] ", (uintmax_t)proc->pid); + } va_start(ap, fmt); vfprintf(trace_fp, fmt, ap); last_pid = proc->pid; -- cgit v1.2.3-70-g09d2