]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CONTRIB: debug: add the possibility to decode the value as certain types only
authorWilly Tarreau <w@1wt.eu>
Thu, 6 Feb 2020 07:33:08 +0000 (08:33 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 6 Feb 2020 07:36:36 +0000 (08:36 +0100)
It's often confusing to have a whole dump on the screen while only
checking for a set of task or stream flags, and appending "|grep ^chn"
isn't very convenient to repeat the opeation. Instead let's add the
ability to filter the output as certain types only by prepending their
name(s) before the value.

contrib/debug/flags.c

index c5315f4c2a4402e6e6eeae4c2f2b9844154b4dfb..e164c9345aab793e7a3ba157e054bb7d6b5abb3e 100644 (file)
@@ -8,6 +8,21 @@
 #include <types/stream_interface.h>
 #include <types/task.h>
 
+// 1 bit per flag, no hole permitted here
+#define SHOW_AS_ANA   0x00000001
+#define SHOW_AS_CHN   0x00000002
+#define SHOW_AS_CONN  0x00000004
+#define SHOW_AS_CS    0x00000008
+#define SHOW_AS_SI    0x00000010
+#define SHOW_AS_SIET  0x00000020
+#define SHOW_AS_STRM  0x00000040
+#define SHOW_AS_TASK  0x00000080
+#define SHOW_AS_TXN   0x00000100
+
+// command line names, must be in exact same order as the SHOW_AS_* flags above
+// so that show_as_words[i] matches flag 1U<<i.
+const char *show_as_words[] = { "ana", "chn", "conn", "cs", "si", "siet", "strm", "task", "txn", };
+
 #define SHOW_FLAG(f,n)                                 \
        do {                                            \
                if (!((f) & (n))) break;                \
                printf(#n"%s", (f) ? " | " : "");       \
        } while (0)
 
+unsigned int get_show_as(const char *word)
+{
+       int w = 0;
+
+       while (1) {
+               if (w == sizeof(show_as_words) / sizeof(*show_as_words))
+                       return 0;
+               if (strcmp(word, show_as_words[w]) == 0)
+                       return 1U << w;
+               w++;
+       }
+}
+
 void show_chn_ana(unsigned int f)
 {
        printf("chn->ana    = ");
@@ -365,26 +393,49 @@ void show_strm_flags(unsigned int f)
        putchar('\n');
 }
 
+void usage_exit(const char *name)
+{
+       fprintf(stderr, "Usage: %s [ana|chn|conn|cs|si|sierr|strm|task|txn]* [0x]value\n", name);
+       exit(1);
+}
+
 int main(int argc, char **argv)
 {
        unsigned int flags;
-
-       if (argc < 2) {
-               fprintf(stderr, "Usage: %s 0x<flags|state>\n", argv[0]);
-               exit(1);
+       unsigned int show_as = 0;
+       unsigned int f;
+       const char *name = argv[0];
+       char *err;
+
+       while (argc > 0) {
+               argv++; argc--;
+               if (argc < 1)
+                       usage_exit(name);
+
+               flags = strtoul(argv[0], &err, 0);
+               if (*argv[0] && !*err)
+                       break;
+
+               f = get_show_as(argv[0]);
+               if (!f) {
+                       fprintf(stderr, "Unknown argument: <%s>\n", argv[0]);
+                       usage_exit(name);
+               }
+               show_as |= f;
        }
 
-       flags = strtoul(argv[1], NULL, 0);
-
-       show_task_state(flags);
-       show_txn_flags(flags);
-       show_strm_flags(flags);
-       show_si_et(flags);
-       show_si_flags(flags);
-       show_cs_flags(flags);
-       show_conn_flags(flags);
-       show_chn_flags(flags);
-       show_chn_ana(flags);
+       if (!show_as)
+               show_as = ~0U;
+
+       if (show_as & SHOW_AS_ANA)   show_chn_ana(flags);
+       if (show_as & SHOW_AS_CHN)   show_chn_flags(flags);
+       if (show_as & SHOW_AS_CONN)  show_conn_flags(flags);
+       if (show_as & SHOW_AS_CS)    show_cs_flags(flags);
+       if (show_as & SHOW_AS_SI)    show_si_flags(flags);
+       if (show_as & SHOW_AS_SIET)  show_si_et(flags);
+       if (show_as & SHOW_AS_STRM)  show_strm_flags(flags);
+       if (show_as & SHOW_AS_TASK)  show_task_state(flags);
+       if (show_as & SHOW_AS_TXN)   show_txn_flags(flags);
 
        return 0;
 }