I'd like to add some regress tests for the btrace(8) parser.
To do that, it would help to have a dry-run mode where it just parses the
file and exits.

The way I've implemented it here, it exits immediately after parsing, so it
won't open /dev/dt and try to find the probes the program uses.  This means
it doesn't require privileges and can be run on kernels without dt(4)
compiled in, but it can't catch typos in probe names.

ok?

Index: btrace.8
===================================================================
RCS file: /cvs/src/usr.sbin/btrace/btrace.8,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 btrace.8
--- btrace.8    11 Sep 2020 08:16:15 -0000      1.2
+++ btrace.8    19 Mar 2021 11:22:41 -0000
@@ -41,6 +41,9 @@ in
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl d
+Dry run.
+Parse the program and then exit.
 .It Fl e Ar program
 Execute
 .Ar program .
Index: btrace.c
===================================================================
RCS file: /cvs/src/usr.sbin/btrace/btrace.c,v
retrieving revision 1.26
diff -u -p -u -p -r1.26 btrace.c
--- btrace.c    7 Dec 2020 18:28:09 -0000       1.26
+++ btrace.c    19 Mar 2021 11:22:41 -0000
@@ -123,7 +123,7 @@ main(int argc, char *argv[])
        int fd = -1, ch, error = 0;
        const char *filename = NULL, *btscript = NULL;
        const char *errstr;
-       int showprobes = 0, tracepid = -1;
+       int showprobes = 0, tracepid = -1, dryrun = 0;
 
        setlocale(LC_ALL, "");
 
@@ -132,8 +132,11 @@ main(int argc, char *argv[])
                err(1, "pledge");
 #endif
 
-       while ((ch = getopt(argc, argv, "e:lp:v")) != -1) {
+       while ((ch = getopt(argc, argv, "de:lp:v")) != -1) {
                switch (ch) {
+               case 'd':
+                       dryrun = 1;
+                       break;
                case 'e':
                        btscript = optarg;
                        break;
@@ -177,6 +180,9 @@ main(int argc, char *argv[])
                        return error;
        }
 
+       if (dryrun)
+               return error;
+
        if (showprobes || g_nprobes > 0) {
                fd = open(__PATH_DEVDT, O_RDONLY);
                if (fd == -1)
@@ -200,7 +206,7 @@ main(int argc, char *argv[])
 __dead void
 usage(void)
 {
-       fprintf(stderr, "usage: %s [-lv] [-p pid] [-e program|file]\n",
+       fprintf(stderr, "usage: %s [-dlv] [-p pid] [-e program|file]\n",
            getprogname());
        exit(1);
 }

Reply via email to