]> granicus.if.org Git - strace/commitdiff
Use dynamically-sized selected[] array for -P PATH
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 5 Mar 2013 14:46:34 +0000 (15:46 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 5 Mar 2013 14:46:34 +0000 (15:46 +0100)
While at it, added a small optimization of not remembering
the path twice if it happens to be the same.

   text    data     bss     dec     hex filename
 245111     680   10860  256651   3ea8b strace_old
 245075     680    9804  255559   3e647 strace

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
defs.h
pathtrace.c
strace.1
strace.c

diff --git a/defs.h b/defs.h
index 6bfa916a72bedcce63e7a968eb22cfcec7d154ec..1d607c709de9554c14f62b95b414d5c10d64fc8a 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -601,7 +601,7 @@ extern int clearbpt(struct tcb *);
 
 extern const char *signame(int);
 extern int is_restart_error(struct tcb *);
-extern int pathtrace_select(const char *);
+extern void pathtrace_select(const char *);
 extern int pathtrace_match(struct tcb *);
 extern const char *getfdpath(struct tcb *, int);
 
index 83d79d3763ed1f12fdad8c31360194d2fb95cee7..19110348b68875029a2f087b93e2a4a62bd503b0 100644 (file)
@@ -37,8 +37,8 @@
 
 #include "syscall.h"
 
-#define MAXSELECTED  256       /* max number of "selected" paths */
-static const char *selected[MAXSELECTED];      /* paths selected for tracing */
+static const char **selected = NULL; /* paths selected for tracing */
+static unsigned num_selected = 0;
 
 /*
  * Return true if specified path matches one that we're tracing.
@@ -46,11 +46,9 @@ static const char *selected[MAXSELECTED];    /* paths selected for tracing */
 static int
 pathmatch(const char *path)
 {
-       unsigned int i;
+       unsigned i;
 
-       for (i = 0; i < ARRAY_SIZE(selected); ++i) {
-               if (selected[i] == NULL)
-                       return 0;
+       for (i = 0; i < num_selected; ++i) {
                if (strcmp(path, selected[i]) == 0)
                        return 1;
        }
@@ -84,21 +82,19 @@ fdmatch(struct tcb *tcp, int fd)
  * Add a path to the set we're tracing.
  * Secifying NULL will delete all paths.
  */
-static int
+static void
 storepath(const char *path)
 {
-       unsigned int i;
+       unsigned i;
 
-       for (i = 0; i < ARRAY_SIZE(selected); ++i) {
-               if (!selected[i]) {
-                       selected[i] = path;
-                       return 0;
-               }
-       }
+       if (pathmatch(path))
+               return; /* already in table */
 
-       fprintf(stderr, "Max trace paths exceeded, only using first %u\n",
-               (unsigned int) ARRAY_SIZE(selected));
-       return -1;
+       i = num_selected++;
+       selected = realloc(selected, num_selected * sizeof(selected[0]));
+       if (!selected)
+               die_out_of_memory();
+       selected[i] = path;
 }
 
 /*
@@ -126,28 +122,27 @@ getfdpath(struct tcb *tcp, int fd)
  * Add a path to the set we're tracing.  Also add the canonicalized
  * version of the path.  Secifying NULL will delete all paths.
  */
-int
+void
 pathtrace_select(const char *path)
 {
        char *rpath;
 
-       if (storepath(path))
-               return -1;
+       storepath(path);
 
        rpath = realpath(path, NULL);
 
        if (rpath == NULL)
-               return 0;
+               return;
 
        /* if realpath and specified path are same, we're done */
        if (strcmp(path, rpath) == 0) {
                free(rpath);
-               return 0;
+               return;
        }
 
        fprintf(stderr, "Requested path '%s' resolved into '%s'\n",
                path, rpath);
-       return storepath(rpath);
+       storepath(rpath);
 }
 
 /*
@@ -159,7 +154,7 @@ pathtrace_match(struct tcb *tcp)
 {
        const struct_sysent *s;
 
-       if (selected[0] == NULL)
+       if (!selected)
                return 1;
 
        s = tcp->s_ent;
index 63b604c84ce648b84e5d6a575eb1a7adaa6aa20c..00ca03c489d1ac1bbf9e68e40577bfa98bbfcd61 100644 (file)
--- a/strace.1
+++ b/strace.1
@@ -517,7 +517,7 @@ Trace only system calls accessing
 .I path.
 Multiple
 .B \-P
-options can be used to specify up to 256 paths.
+options can be used to specify several paths.
 .TP
 .BI "\-s " strsize
 Specify the maximum string size to print (the default is 32).  Note
index 319dff2fd33c22c911dc3fca42e2a7ebf1585347..9ec354dab65580c005865cbd518b0ea8b72b2fb9 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -1649,9 +1649,7 @@ init(int argc, char *argv[])
                        break;
                case 'P':
                        tracing_paths = 1;
-                       if (pathtrace_select(optarg)) {
-                               error_msg_and_die("Failed to select path '%s'", optarg);
-                       }
+                       pathtrace_select(optarg);
                        break;
                case 's':
                        i = string_to_uint(optarg);