]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libs/debug: accept human readable names for _DEBUG=
authorOndrej Oprala <ooprala@redhat.com>
Thu, 31 Jul 2014 11:23:07 +0000 (13:23 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 13 Aug 2014 10:33:47 +0000 (12:33 +0200)
For example
$ LIBMOUNT_DEBUG=tab,cache findmnt

to debug only TAB and CACHE subsystem.

Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/debug.h
libblkid/src/init.c
libfdisk/src/init.c
libmount/src/Makemodule.am
libmount/src/init.c
libsmartcols/src/init.c
tests/commands.sh

index 2eb9d4421ed3d42feda780f5b096fb18cdd0fa22..1497490b59d65331591a65b890339e2736a1e679 100644 (file)
@@ -8,6 +8,7 @@
 #define UTIL_LINUX_DEBUG_H
 
 #include <stdarg.h>
+#include <string.h>
 
 #define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask
 #define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m)
@@ -47,7 +48,7 @@
                else if (!mask) { \
                        char *str = getenv(# env); \
                        if (str) \
-                               lib ## _debug_mask = strtoul(str, 0, 0); \
+                               lib ## _debug_mask = parse_envmask(lib ## _masknames, str); \
                } else \
                        lib ## _debug_mask = mask; \
                lib ## _debug_mask |= pref ## INIT; \
@@ -57,6 +58,8 @@
                } \
        } while (0)
 
+struct dbg_mask { char *mname; int val; };
+
 static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
 ul_debug(const char *mesg, ...)
 {
@@ -80,4 +83,41 @@ ul_debugobj(void *handler, const char *mesg, ...)
        fputc('\n', stderr);
 }
 
+static inline int parse_envmask(const struct dbg_mask const flagnames[],
+                               const char *mask)
+{
+       int res;
+       char *ptr;
+
+       /* let's check for a numeric mask first */
+       res = strtoul(mask, &ptr, 0);
+
+       /* perhaps it's a comma-separated string? */
+       if (*ptr != '\0') {
+               char *msbuf, *ms, *name;
+               res = 0;
+
+               ms = msbuf = strdup(mask);
+               if (!ms)
+                       return res;
+
+               while ((name = strtok_r(ms, ",", &ptr))) {
+                       size_t i = 0;
+                       ms = ptr;
+
+                       while (flagnames[i].mname) {
+                               if (!strcmp(name, flagnames[i].mname)) {
+                                       res |= flagnames[i].val;
+                                       break;
+                               }
+                               ++i;
+                       }
+                       /* nothing else we can do by OR-ing the mask */
+                       if (res == 0xffff)
+                               break;
+               }
+               free(msbuf);
+       }
+       return res;
+}
 #endif /* UTIL_LINUX_DEBUG_H */
index 5b21ce331516117945000ca41ee726b53e0aa87a..61fe5d45e738818f0ca96dc1398b6ecc8b8e3c8f 100644 (file)
 
 UL_DEBUG_DEFINE_MASK(libblkid);
 
+static const struct dbg_mask libblkid_masknames [] = {
+       { "all", BLKID_DEBUG_ALL },
+       { "cache", BLKID_DEBUG_CACHE },
+       { "dump", BLKID_DEBUG_DUMP },
+       { "dev", BLKID_DEBUG_DEV },
+       { "devname", BLKID_DEBUG_DEVNAME },
+       { "devno", BLKID_DEBUG_DEVNO },
+       { "probe", BLKID_DEBUG_PROBE },
+       { "read", BLKID_DEBUG_READ },
+       { "resolve", BLKID_DEBUG_RESOLVE },
+       { "save", BLKID_DEBUG_SAVE },
+       { "tag", BLKID_DEBUG_TAG },
+       { "lowprobe", BLKID_DEBUG_LOWPROBE },
+       { "config", BLKID_DEBUG_CONFIG },
+       { "evaluate", BLKID_DEBUG_EVALUATE },
+       { "init", BLKID_DEBUG_INIT },
+       { NULL, 0 }
+};
+
 /**
  * blkid_init_debug:
  * @mask: debug mask (0xffff to enable full debuging)
index e30c8b97677311fe9c040199beb39d7a419580aa..87217cbd769ad7581c82fe350206aa729452e189 100644 (file)
@@ -3,6 +3,18 @@
 
 UL_DEBUG_DEFINE_MASK(libfdisk);
 
+static const struct dbg_mask libfdisk_masknames[] = {
+       { "all", FDISK_DEBUG_ALL },
+       { "init", FDISK_DEBUG_INIT },
+       { "cxt", FDISK_DEBUG_CXT },
+       { "label", FDISK_DEBUG_LABEL },
+       { "ask", FDISK_DEBUG_ASK},
+       { "frontend", FDISK_DEBUG_FRONTEND },
+       { "part", FDISK_DEBUG_PART },
+       { "parttype", FDISK_DEBUG_PARTTYPE },
+       { "tab", FDISK_DEBUG_TAB},
+       { NULL, 0 }
+};
 /**
  * fdisk_init_debug:
  * @mask: debug mask (0xffff to enable full debuging)
index 54ab51d3e6d62cc74ba66922019ddb04a9a8107b..d21179a9c062a452cb0840043d6cdad23d271e42 100644 (file)
@@ -63,7 +63,8 @@ check_PROGRAMS += \
        test_mount_tab_diff \
        test_mount_tab_update \
        test_mount_utils \
-       test_mount_version
+       test_mount_version \
+       test_mount_debug
 
 libmount_tests_cflags  = -DTEST_PROGRAM $(libmount_la_CFLAGS)
 libmount_tests_ldflags = libblkid.la -static
@@ -118,6 +119,11 @@ test_mount_version_CFLAGS = $(libmount_tests_cflags)
 test_mount_version_LDFLAGS = $(libmount_tests_ldflags)
 test_mount_version_LDADD = $(libmount_tests_ldadd)
 
+test_mount_debug_SOURCES = libmount/src/init.c
+test_mount_debug_CFLAGS = $(libmount_tests_cflags)
+test_mount_debug_LDFLAGS = $(libmount_tests_ldflags)
+test_mount_debug_LDADD = $(libmount_tests_ldadd)
+
 endif # BUILD_LIBMOUNT_TESTS
 
 
index b3d10df448c6951a29c95a2028aa775ed6d55383..e460788fb0af75fac1862b3f3bc2654f751c45bf 100644 (file)
 #include "mountP.h"
 
 UL_DEBUG_DEFINE_MASK(libmount);
-
+static const struct dbg_mask libmount_masknames [] = {
+       { "all", MNT_DEBUG_ALL },
+       { "init", MNT_DEBUG_INIT },
+       { "cache", MNT_DEBUG_CACHE },
+       { "options", MNT_DEBUG_OPTIONS },
+       { "locks", MNT_DEBUG_LOCKS },
+       { "tab", MNT_DEBUG_TAB },
+       { "fs", MNT_DEBUG_FS },
+       { "opts", MNT_DEBUG_OPTS },
+       { "update", MNT_DEBUG_UPDATE },
+       { "utils", MNT_DEBUG_UTILS },
+       { "cxt", MNT_DEBUG_CXT },
+       { "diff", MNT_DEBUG_DIFF },
+       { NULL, 0 }
+};
 /**
  * mnt_init_debug:
  * @mask: debug mask (0xffff to enable full debugging)
@@ -44,3 +58,31 @@ void mnt_init_debug(int mask)
                        DBG(INIT, ul_debug("    feature: %s", *p++));
        }
 }
+
+#ifdef TEST_PROGRAM
+
+#include <errno.h>
+#include <stdlib.h>
+int main(int argc, char *argv[])
+{
+       if (argc == 2) {
+               int mask;
+
+               errno = 0;
+               mask = strtoul(argv[1], 0, 0);
+
+               if (errno)
+                       return 1;
+
+               __UL_INIT_DEBUG(libmount, MNT_DEBUG_, mask, LIBMOUNT_DEBUG);
+       }
+       else if (argc == 1) {
+               __UL_INIT_DEBUG(libmount, MNT_DEBUG_, 0, LIBMOUNT_DEBUG);
+       }
+       else
+               return 1;
+
+       return 0;
+}
+#endif /* TEST_PROGRAM */
+
index 95b4610b77a468b8f90294ff349ce6c7ec840d47..9fec0356d54b19c9dd890ad23fe116e9bd9f8283 100644 (file)
 
 UL_DEBUG_DEFINE_MASK(libsmartcols);
 
+static const struct dbg_mask libsmartcols_masknames [] = {
+       { "all", SCOLS_DEBUG_ALL },
+       { "cell", SCOLS_DEBUG_CELL },
+       { "line", SCOLS_DEBUG_LINE },
+       { "tab", SCOLS_DEBUG_TAB },
+       { "col", SCOLS_DEBUG_COL },
+       { "buff", SCOLS_DEBUG_BUFF },
+       { NULL, 0 }
+};
 /**
  * scols_init_debug:
  * @mask: debug mask (0xffff to enable full debugging)
index f699ef1aba58b4dccc07fa7bfbb2d0708f6323c6..0a3062872b2ffe392f0367dbbdc1f6e9b28e8010 100644 (file)
@@ -13,6 +13,7 @@ TS_HELPER_LIBMOUNT_TABDIFF="$top_builddir/test_mount_tab_diff"
 TS_HELPER_LIBMOUNT_TAB="$top_builddir/test_mount_tab"
 TS_HELPER_LIBMOUNT_UPDATE="$top_builddir/test_mount_tab_update"
 TS_HELPER_LIBMOUNT_UTILS="$top_builddir/test_mount_utils"
+TS_HELPER_LIBMOUNT_DEBUG="$top_builddir/test_mount_debug"
 TS_HELPER_PYLIBMOUNT_CONTEXT="$top_srcdir/libmount/python/test_mount_context.py"
 TS_HELPER_PYLIBMOUNT_TAB="$top_srcdir/libmount/python/test_mount_tab.py"
 TS_HELPER_PYLIBMOUNT_UPDATE="$top_srcdir/libmount/python/test_mount_tab_update.py"