From bcf445fd688692fd4cbfde03cda60be4281ac28b Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 15 May 2018 13:43:29 +0200 Subject: lib/path lib/sysfs: add debug Signed-off-by: Karel Zak --- lib/path.c | 38 +++++++++++++++++++++++++++++++++++++- lib/sysfs.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/path.c b/lib/path.c index d6cfe1453..998dce5bb 100644 --- a/lib/path.c +++ b/lib/path.c @@ -18,6 +18,29 @@ #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, " not defined"); dir = argv[optind++]; + ul_path_init_debug(); + pc = ul_new_path(dir); if (!pc) err(EXIT_FAILURE, "failed to initialize path context"); diff --git a/lib/sysfs.c b/lib/sysfs.c index b6f20ee19..3fe771449 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -15,11 +15,34 @@ #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 ", argv[0]); + ul_sysfs_init_debug(); + devname = argv[1]; devno = sysfs_devname_to_devno(devname); -- cgit v1.2.3-55-g7522