From 712f56fe3369c59d32b9000830b4ed7b25ed24b5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 30 May 2020 17:34:29 +0200 Subject: Add support for tracing fork children MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- util.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..76a8a36 --- /dev/null +++ b/util.c @@ -0,0 +1,56 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +static FILE *trace_fp; +static char last_char = '\n'; +static pid_t last_pid = 0; + + +void +set_trace_output(FILE *fp) +{ + trace_fp = fp; +} + + +void +tprintf(struct process *proc, const char *fmt, ...) +{ + va_list ap; + if (fmt[0] == '\n' && fmt[1]) { + 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); + va_start(ap, fmt); + vfprintf(trace_fp, fmt, ap); + last_pid = proc->pid; + last_char = strchr(fmt, '\0')[-1]; + va_end(ap); +} + + +_Noreturn void +eprintf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s%s: ", last_char == '\n' ? "" : "\n", argv0); + vfprintf(stderr, fmt, ap); + switch (strchr(fmt, '\0')[-1]) { + case ':': + fprintf(stderr, " %s\n", strerror(errno)); + break; + case '\n': + break; + default: + fprintf(stderr, "\n"); + break; + } + va_end(ap); + exit(1); +} -- cgit v1.2.3-70-g09d2