Skip to content

Commit

Permalink
add -l and -q
Browse files Browse the repository at this point in the history
  • Loading branch information
leahneukirchen committed Mar 13, 2014
1 parent 89a7232 commit 3ba76d2
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions extrace.c
Expand Up @@ -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 <chneukirchen@gmail.com>
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit 3ba76d2

Please sign in to comment.