about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2014-03-13 20:05:34 +0100
committerChristian Neukirchen <chneukirchen@gmail.com>2014-03-13 20:05:34 +0100
commit3ba76d2eadd444c4df5aa51441650e660121f2a5 (patch)
treea107aab7ce08dabca629a22c7e8d726e592fbf1c
parent89a7232d436a150ede517eaf09f0134309cc0cb3 (diff)
downloadextrace-3ba76d2eadd444c4df5aa51441650e660121f2a5.tar.gz
extrace-3ba76d2eadd444c4df5aa51441650e660121f2a5.tar.xz
extrace-3ba76d2eadd444c4df5aa51441650e660121f2a5.zip
add -l and -q
-rw-r--r--extrace.c40
1 files 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 <chneukirchen@gmail.com>
  *
@@ -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);
   }