From 3ba76d2eadd444c4df5aa51441650e660121f2a5 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Thu, 13 Mar 2014 20:05:34 +0100 Subject: add -l and -q --- extrace.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/extrace.c b/extrace.c index 76b99ca..446f31e 100644 --- a/extrace.c +++ b/extrace.c @@ -10,6 +10,8 @@ * -o FILE log to FILE instead of standard output (implies -w) * -w wide output: show full command line * -f flat output: no indentation + * -l print full path of argv[0] + * -q don't print exec() arguments * * Copyright (C) 2014 Christian Neukirchen * @@ -83,6 +85,8 @@ pid_t parent = 1; int width = 80; int flat = 0; int run = 0; +int full_path = 0; +int show_args = 1; FILE *output; sig_atomic_t quit = 0; @@ -133,8 +137,10 @@ handle_msg(struct cn_msg *cn_hdr) { char cmdline[CMDLINE_MAX], name[PATH_MAX]; char buf[CMDLINE_MAX]; + char exe[PATH_MAX]; + char *argvrest; - int r = 0, fd, i, d; + int r = 0, r2 = 0, fd, i, d; struct proc_event *ev = (struct proc_event *)cn_hdr->data; if (ev->what == PROC_EVENT_EXEC) { @@ -151,16 +157,32 @@ handle_msg(struct cn_msg *cn_hdr) r = read(fd, cmdline, sizeof cmdline); close(fd); + if (full_path) { + snprintf(name, sizeof name, "/proc/%d/exe", + ev->event_data.exec.process_pid); + r2 = readlink(name, exe, sizeof exe); + if (r2 > 0) + exe[r2] = 0; + argvrest = strchr(cmdline, 0); + } + /* convert nuls (argument seperators) and newlines to spaces */ - for (i = 0; r > 0 && i < r; i++) + for (i = 0; show_args && r > 0 && i < r-1; i++) if (cmdline[i] == 0 || cmdline[i] == '\n') cmdline[i] = ' '; } - snprintf(buf, min(sizeof buf, width+1), - "%*s%d %s", flat ? 0 : 2*d, "", - ev->event_data.exec.process_pid, - cmdline); + if (full_path && r2 > 0) + snprintf(buf, min(sizeof buf, width+1), + "%*s%d %s%s", flat ? 0 : 2*d, "", + ev->event_data.exec.process_pid, + exe, + argvrest); + else + snprintf(buf, min(sizeof buf, width+1), + "%*s%d %s", flat ? 0 : 2*d, "", + ev->event_data.exec.process_pid, + cmdline); fprintf(output, "%s\n", buf); fflush(output); } @@ -186,10 +208,12 @@ main(int argc, char *argv[]) output = stdout; - while ((opt = getopt(argc, argv, "+fo:p:w")) != -1) + while ((opt = getopt(argc, argv, "+flo:p:qw")) != -1) switch (opt) { case 'f': flat = 1; break; + case 'l': full_path = 1; break; case 'p': parent = atoi(optarg); break; + case 'q': show_args = 0; break; case 'o': output = fopen(optarg, "w"); if (!output) { @@ -203,7 +227,7 @@ main(int argc, char *argv[]) if (parent != 1 && optind != argc) { usage: - fprintf(stderr, "Usage: extrace [-f] [-w] [-o FILE] [-p PID|CMD...]\n"); + fprintf(stderr, "Usage: extrace [-flwq] [-o FILE] [-p PID|CMD...]\n"); exit(1); } -- cgit 1.4.1