]> The Tcpdump Group git mirrors - tcpdump/commitdiff
instrument functions: Use an environment variable instead of config files
authorFrancois-Xavier Le Bail <[email protected]>
Sun, 12 Mar 2023 17:57:56 +0000 (18:57 +0100)
committerFrancois-Xavier Le Bail <[email protected]>
Mon, 13 Mar 2023 09:24:43 +0000 (10:24 +0100)
If the environment variable INSTRUMENT is
- unset or set to an empty string, print nothing, like with no
  instrumentation
- set to "all" or "a", print all the functions names
- set to "global" or "g", print only the global functions names

The configuration with --enable-instrument-functions remains.

Note that before this change, the default was to print all functions.
Now it is to print nothing. So by default 'make check' runs without errors.

This allows to run:
$ INSTRUMENT=a ./tcpdump ...
$ INSTRUMENT=g ./tcpdump ...
$ INSTRUMENT= ./tcpdump ...
or
$ export INSTRUMENT=global
$ ./tcpdump ...

This also allows to run the statically compiled binary on another host
after copying it.

It is no longer necessary to modify the configuration with:
$ make instrument_all
$ make instrument_global
$ make instrument_off
(Targets removed.)

Update .gitignore, CONTRIBUTING.md and Makefile.in accordingly.

Moreover:
Reduce the scope of a variable.
Rename a variable.
Remove '\n' in the perror() call.
Remove 2 spaces in function calls (style).

[skip ci]

.gitignore
CONTRIBUTING.md
Makefile.in
instrument-functions.c

index 71b90611448756d77c138928d2b058462e12c981..8de8e77021d4a02e2ba02c682e9b58c9fd72fb29 100644 (file)
@@ -42,5 +42,3 @@ netdissect.dir/
 tcpdump.dir/
 tcpdump.sln
 .vs/
-/instrument_functions_global.devel
-/instrument_functions_off.devel
index 01e9440164c2cf0e61e3b5e80687a17d49ee27bb..77f180e40584b6bbcadfbc6eb391664493fba761 100644 (file)
@@ -123,7 +123,7 @@ and ask!
   You can configure and build tcpdump with the instrumentation of functions:
   ```
   $ ./configure --enable-instrument-functions
-  $ make -s
+  $ make -s clean all
   ```
 
   This generates instrumentation calls for entry and exit to functions.
@@ -137,19 +137,22 @@ and ask!
   In some cases, with Clang 11, the file number is unknown (printed '??')
   or the line number is unknown (printed '?'). In this case, use GCC.
 
-  To print nothing, like with no instrumentation:
-  ```
-  $ make instrument_off
-  ```
+  If the environment variable INSTRUMENT is
+  - unset or set to an empty string, print nothing, like with no
+    instrumentation
+  - set to "all" or "a", print all the functions names
+  - set to "global" or "g", print only the global functions names
 
-  To configure the printing of only the global functions names:
+  This allows to run:
   ```
-  $ make instrument_global
+  $ INSTRUMENT=a ./tcpdump ...
+  $ INSTRUMENT=g ./tcpdump ...
+  $ INSTRUMENT= ./tcpdump ...
   ```
-
-  To go back to print all the functions names:
+  or
   ```
-  $ make instrument_all
+  $ export INSTRUMENT=global
+  $ ./tcpdump ...
   ```
 
   The library libbfd is used, therefore the binutils-dev package is required.
index 8d5b2534e31a645f0d6a9252973fe586360546c2..1b3c3a57477ce0c575b07cf17f3e28e7f1f51a50 100644 (file)
@@ -389,18 +389,6 @@ $(PROG): $(OBJ) @V_PCAPDEP@ $(LIBNETDISSECT)
        @rm -f $@
        $(CC) $(FULL_CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(LIBNETDISSECT) $(LIBS)
 
-instrument_all: $(PROG)
-       @rm -f instrument_functions_global.devel
-       @rm -f instrument_functions_off.devel
-
-instrument_global: $(PROG)
-       @touch instrument_functions_global.devel
-       @rm -f instrument_functions_off.devel
-
-instrument_off: $(PROG)
-       @touch instrument_functions_off.devel
-       @rm -f instrument_functions_global.devel
-
 $(LIBNETDISSECT): $(LIBNETDISSECT_OBJ)
        @rm -f $@
        $(AR) cr $@ $(LIBNETDISSECT_OBJ)
index 323497729ac62761147cc96caaff7991e7fc23d1..350a1682a0326f9a058e2f7369ff5320dcc01ce1 100644 (file)
@@ -14,8 +14,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <unistd.h>
 #include <bfd.h>
 
  * If entering in a function it prints also the calling function name with
  * file name and line number.
  *
- * To configure the printing of only the global functions names:
- * $ make instrument_global
- *
- * To go back to print all the functions names:
- * $ make instrument_all
- *
- * To print nothing, like with no instrumentation:
- * $ make instrument_off
+ * If the environment variable INSTRUMENT is
+ * unset or set to an empty string, print nothing, like with no instrumentation
+ * set to "all" or "a", print all the functions names
+ * set to "global" or "g", print only the global functions names
  */
 
 #define ND_NO_INSTRUMENT __attribute__((no_instrument_function))
@@ -73,12 +67,6 @@ __cyg_profile_func_exit(void *this_fn, void *call_site)
        print_debug(this_fn, call_site, EXIT);
 }
 
-/* If this file exists, print only the global functions */
-#define ND_FILE_FLAG_GLOBAL "instrument_functions_global.devel"
-
-/* If this file exists, print nothing, like with no instrumentation */
-#define ND_FILE_FLAG_OFF "instrument_functions_off.devel"
-
 static void print_debug(void *this_fn, void *call_site, action_type action)
 {
        static bfd* abfd;
@@ -86,32 +74,45 @@ static void print_debug(void *this_fn, void *call_site, action_type action)
        static long symcount;
        static asection *text;
        static bfd_vma vma;
+       static char *instrument_type;
+       static int instrument_set;
        static int instrument_off;
-       static int print_only_global;
-       symbol_info syminfo;
-       struct stat statbuf;
+       static int instrument_global;
        int i;
 
-       if (!instrument_off) {
-               /* one-time test */
-               if (!stat(ND_FILE_FLAG_OFF, &statbuf)) {
+       if (!instrument_set) {
+               /* Get the configuration environment variable INSTRUMENT value if any */
+               instrument_type = getenv("INSTRUMENT");
+               /* unset or set to an empty string ? */
+               if (instrument_type == NULL ||
+                       !strncmp(instrument_type, "", sizeof(""))) {
                        instrument_off = 1;
-                       return;
+               } else {
+                       /* set to "global" or "g" ? */
+                       if (!strncmp(instrument_type, "global", sizeof("global")) ||
+                               !strncmp(instrument_type, "g", sizeof("g")))
+                               instrument_global = 1;
+                       else if (strncmp(instrument_type, "all", sizeof("all")) &&
+                                        strncmp(instrument_type, "a", sizeof("a"))) {
+                               fprintf(stderr, "INSTRUMENT can be only \"\", \"all\", \"a\", "
+                                               "\"global\" or \"g\".\n");
+                               exit(1);
+                       }
                }
-       } else
-               return;
+               instrument_set = 1;
+       }
+
+       if (instrument_off)
+                       return;
 
        /* If no errors, this block should be executed one time */
        if (!abfd) {
                char pgm_name[1024];
                long symsize;
 
-               if (!stat(ND_FILE_FLAG_GLOBAL, &statbuf))
-                       print_only_global = 1;
-
                ssize_t ret = readlink("/proc/self/exe", pgm_name, sizeof(pgm_name));
                if (ret == -1) {
-                       perror("failed to find executable\n");
+                       perror("failed to find executable");
                        return;
                }
                if (ret == sizeof(pgm_name)) {
@@ -142,8 +143,8 @@ static void print_debug(void *this_fn, void *call_site, action_type action)
                symtab = (asymbol **)malloc(symsize);
                symcount = bfd_canonicalize_symtab(abfd, symtab);
                if (symcount < 0) {
-                       free (symtab);
-                       bfd_perror ("bfd_canonicalize_symtab");
+                       free(symtab);
+                       bfd_perror("bfd_canonicalize_symtab");
                        return;
                }
 
@@ -154,7 +155,8 @@ static void print_debug(void *this_fn, void *call_site, action_type action)
                vma = text->vma;
        }
 
-       if (print_only_global) {
+       if (instrument_global) {
+               symbol_info syminfo;
                int found;
 
                i = 0;