]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path lib/sysfs: add debug
authorKarel Zak <kzak@redhat.com>
Tue, 15 May 2018 11:43:29 +0000 (13:43 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:07:46 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
include/sysfs.h
lib/path.c
lib/sysfs.c
sys-utils/losetup.c

index 8b000e63a140c99d82414210c38427414245d2a9..8b7d568e7f0d45346dca915c75629d16d534b24e 100644 (file)
@@ -25,6 +25,8 @@ struct path_cxt *ul_new_path(const char *dir);
 void ul_unref_path(struct path_cxt *pc);
 void ul_ref_path(struct path_cxt *pc);
 
+void ul_path_init_debug(void);
+
 int ul_path_set_prefix(struct path_cxt *pc, const char *prefix);
 const char *ul_path_get_prefix(struct path_cxt *pc);
 
index aa02674ad59150c5e2272a51d9d8e671cd2b2a9e..ea87d6a125cde2dd3b099a2c689b0bb441a1c3fb 100644 (file)
@@ -64,6 +64,8 @@ struct sysfs_blkdev {
                        hctl_error : 1 ;
 };
 
+void ul_sysfs_init_debug(void);
+
 struct path_cxt *ul_new_sysfs_path(dev_t devno, struct path_cxt *parent, const char *prefix);
 int sysfs_blkdev_init_path(struct path_cxt *pc, dev_t devno, struct path_cxt *parent);
 int sysfs_blkdev_set_parent(struct path_cxt *pc, struct path_cxt *parent);
index d6cfe1453d516dc054876389aba80569bf24a62f..998dce5bb4b7229a594d3a126d3f03e1ed72b26a 100644 (file)
 #include "fileutils.h"
 #include "all-io.h"
 #include "path.h"
+#include "debug.h"
+
+/*
+ * Debug stuff (based on include/debug.h)
+ */
+static UL_DEBUG_DEFINE_MASK(ulpath);
+UL_DEBUG_DEFINE_MASKNAMES(ulpath) = UL_DEBUG_EMPTY_MASKNAMES;
+
+#define ULPATH_DEBUG_INIT      (1 << 1)
+#define ULPATH_DEBUG_CXT       (1 << 2)
+
+#define DBG(m, x)       __UL_DBG(ulpath, ULPATH_DEBUG_, m, x)
+#define ON_DBG(m, x)    __UL_DBG_CALL(ulpath, ULPATH_DEBUG_, m, x)
+
+#define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(ulpath)
+#include "debugobj.h"
+
+void ul_path_init_debug(void)
+{
+       if (ulpath_debug_mask)
+               return;
+       __UL_INIT_DEBUG_FROM_ENV(ulpath, ULPATH_DEBUG_, 0, ULPATH_DEBUG);
+}
 
 struct path_cxt *ul_new_path(const char *dir)
 {
@@ -26,6 +49,8 @@ struct path_cxt *ul_new_path(const char *dir)
        if (!pc)
                return NULL;
 
+       DBG(CXT, ul_debugobj(pc, "alloc"));
+
        pc->refcount = 1;
        pc->dir_fd = -1;
 
@@ -54,6 +79,7 @@ void ul_unref_path(struct path_cxt *pc)
        pc->refcount--;
 
        if (pc->refcount <= 0) {
+               DBG(CXT, ul_debugobj(pc, "dealloc"));
                if (pc->dialect)
                        pc->free_dialect(pc);
                if (pc->dir_fd >= 0)
@@ -78,6 +104,7 @@ int ul_path_set_prefix(struct path_cxt *pc, const char *prefix)
 
        free(pc->prefix);
        pc->prefix = p;
+       DBG(CXT, ul_debugobj(pc, "new prefix: '%s'", p));
        return 0;
 }
 
@@ -103,6 +130,7 @@ int ul_path_set_dir(struct path_cxt *pc, const char *dir)
 
        free(pc->dir_path);
        pc->dir_path = p;
+       DBG(CXT, ul_debugobj(pc, "new dir: '%s'", p));
        return 0;
 }
 
@@ -115,6 +143,7 @@ int ul_path_set_dialect(struct path_cxt *pc, void *data, void free_data(struct p
 {
        pc->dialect = data;
        pc->free_dialect = free_data;
+       DBG(CXT, ul_debugobj(pc, "new dialect"));
        return 0;
 }
 
@@ -157,6 +186,8 @@ int ul_path_get_dirfd(struct path_cxt *pc)
                const char *path = get_absdir(pc);
                if (!path)
                        return -errno;
+
+               DBG(CXT, ul_debugobj(pc, "opening dir: '%s'", path));
                pc->dir_fd = open(path, O_RDONLY|O_CLOEXEC);
        }
 
@@ -223,6 +254,7 @@ int ul_path_open(struct path_cxt *pc, int flags, const char *path)
            && pc->redirect_on_enoent(pc, path, &dir) == 0)
                fd = openat(dir, path, flags);
 
+       DBG(CXT, ul_debugobj(pc, "opening '%s'", path));
        return fd;
 }
 
@@ -327,8 +359,10 @@ DIR *ul_path_opendir(struct path_cxt *pc, const char *path)
 
        if (path)
                fd = ul_path_open(pc, O_RDONLY|O_CLOEXEC, path);
-       else if (pc->dir_path)
+       else if (pc->dir_path) {
+               DBG(CXT, ul_debugobj(pc, "duplicate dir path"));
                fd = dup_fd_cloexec(ul_path_get_dirfd(pc), STDERR_FILENO + 1);
+       }
 
        if (fd < 0)
                return NULL;
@@ -895,6 +929,8 @@ int main(int argc, char *argv[])
                errx(EXIT_FAILURE, "<dir> not defined");
        dir = argv[optind++];
 
+       ul_path_init_debug();
+
        pc = ul_new_path(dir);
        if (!pc)
                err(EXIT_FAILURE, "failed to initialize path context");
index b6f20ee1971a8022eac38e65d8de8a2bbfe9f6ef..3fe77144934d1f0a8afa27eb7031cd6f6dfa10ee 100644 (file)
 #include "sysfs.h"
 #include "fileutils.h"
 #include "all-io.h"
+#include "debug.h"
 
 static void sysfs_blkdev_deinit_path(struct path_cxt *pc);
 static int  sysfs_blkdev_enoent_redirect(struct path_cxt *pc, const char *path, int *dirfd);
 static dev_t __sysfs_devname_to_devno(const char *prefix, const char *name, const char *parent);
 
+/*
+ * Debug stuff (based on include/debug.h)
+ */
+static UL_DEBUG_DEFINE_MASK(ulsysfs);
+UL_DEBUG_DEFINE_MASKNAMES(ulsysfs) = UL_DEBUG_EMPTY_MASKNAMES;
+
+#define ULSYSFS_DEBUG_INIT     (1 << 1)
+#define ULSYSFS_DEBUG_CXT      (1 << 2)
+
+#define DBG(m, x)       __UL_DBG(ulsysfs, ULSYSFS_DEBUG_, m, x)
+#define ON_DBG(m, x)    __UL_DBG_CALL(ulsysfs, ULSYSFS_DEBUG_, m, x)
+
+#define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(ulsysfs)
+#include "debugobj.h"
+
+void ul_sysfs_init_debug(void)
+{
+       if (ulsysfs_debug_mask)
+               return;
+       __UL_INIT_DEBUG_FROM_ENV(ulsysfs, ULSYSFS_DEBUG_, 0, ULSYSFS_DEBUG);
+}
+
 struct path_cxt *ul_new_sysfs_path(dev_t devno, struct path_cxt *parent, const char *prefix)
 {
        struct path_cxt *pc = ul_new_path(NULL);
@@ -34,6 +57,7 @@ struct path_cxt *ul_new_sysfs_path(dev_t devno, struct path_cxt *parent, const c
                return NULL;
        }
 
+       DBG(CXT, ul_debugobj(pc, "alloc"));
        return pc;
 }
 
@@ -65,12 +89,15 @@ int sysfs_blkdev_init_path(struct path_cxt *pc, dev_t devno, struct path_cxt *pa
        if (!blk)
                return -ENOMEM;
 
+       DBG(CXT, ul_debugobj(pc, "init for sysfs"));
+
        blk->devno = devno;
        ul_path_set_dialect(pc, blk, sysfs_blkdev_deinit_path);
 
        sysfs_blkdev_set_parent(pc, parent);
 
        ul_path_set_enoent_redirect(pc, sysfs_blkdev_enoent_redirect);
+
        return 0;
 }
 
@@ -80,6 +107,9 @@ static void sysfs_blkdev_deinit_path(struct path_cxt *pc)
 
        if (!pc)
                return;
+
+       DBG(CXT, ul_debugobj(pc, "deinit"));
+
        blk = ul_path_get_dialect(pc);
        if (!blk)
                return;
@@ -106,6 +136,7 @@ int sysfs_blkdev_set_parent(struct path_cxt *pc, struct path_cxt *parent)
        } else
                blk->parent = NULL;
 
+       DBG(CXT, ul_debugobj(pc, "new parent"));
        return 0;
 }
 
@@ -122,8 +153,10 @@ static int sysfs_blkdev_enoent_redirect(struct path_cxt *pc, const char *path, i
 
        if (blk && blk->parent && strncmp(path, "queue/", 6) == 0) {
                *dirfd = ul_path_get_dirfd(blk->parent);
-               if (*dirfd >= 0)
+               if (*dirfd >= 0) {
+                       DBG(CXT, ul_debugobj(pc, "%s redirected to parent", path));
                        return 0;
+               }
        }
        return 1;       /* no redirect */
 }
@@ -261,6 +294,7 @@ dev_t sysfs_blkdev_partno_to_devno(struct path_cxt *pc, int partno)
        }
 
        closedir(dir);
+       DBG(CXT, ul_debugobj(pc, "partno (%d) -> devno (%d)", (int) partno, (int) devno));
        return devno;
 }
 
@@ -970,6 +1004,8 @@ int main(int argc, char *argv[])
        if (argc != 2)
                errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]);
 
+       ul_sysfs_init_debug();
+
        devname = argv[1];
        devno = sysfs_devname_to_devno(devname);
 
index 9eecf06a4cace3e1c568e98fa2cbee7cd3f311a0..7525d8449fbda0b9263ec062d96b7ac4a1901a2b 100644 (file)
@@ -733,6 +733,9 @@ int main(int argc, char **argv)
                }
        }
 
+       ul_path_init_debug();
+       ul_sysfs_init_debug();
+
        /* default is --list --all */
        if (argc == 1) {
                act = A_SHOW;