]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: add debug support
authorDavidlohr Bueso <dave@gnu.org>
Mon, 21 May 2012 20:32:25 +0000 (22:32 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 23 May 2012 08:53:30 +0000 (10:53 +0200)
Based on libmnt, this patch adds basic debugging support for fdisk. Currently
only CONTEXT is debugged, yet keeps exact functionality as libmnt/libblkid.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
fdisk/fdisk.c
fdisk/fdisk.h
fdisk/utils.c

index 7f94523c0463c8ca5782667100ff99e7b6f11e94..aac41a1ed4fe0f3b6c214e8be1d39f2fe2af90a3 100644 (file)
@@ -2168,6 +2168,8 @@ int main(int argc, char **argv)
                }
        }
 
+       fdisk_init_debug(0);
+
        if (user_set_sector_size && argc-optind != 1)
                printf(_("Warning: the -b (set sector size) option should"
                         " be used with one specified device\n"));
index c43517cfc03d512eb5a901e7231fe9764c81f5fc..2471541b9da0f68f8079d04c58654cdda4971e23 100644 (file)
 #define cround(n)      (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
 #define scround(x)     (((x)+units_per_sector-1)/units_per_sector)
 
+/* fdisk debugging flags/options */
+#define FDISK_DEBUG_INIT       (1 << 1)
+#define FDISK_DEBUG_CONTEXT    (1 << 2)
+#define FDISK_DEBUG_ALL                0xFFFF
+
+# define ON_DBG(m, x)  do { \
+                               if ((FDISK_DEBUG_ ## m) & fdisk_debug_mask) { \
+                                       x; \
+                               }          \
+                       } while (0)
+
+# define DBG(m, x)     do { \
+                               if ((FDISK_DEBUG_ ## m) & fdisk_debug_mask) { \
+                                       fprintf(stderr, "%d: fdisk: %8s: ", getpid(), # m); \
+                                       x;                              \
+                               } \
+                       } while (0)
+
+# define DBG_FLUSH     do { \
+                               if (fdisk_debug_mask && \
+                                   fdisk_debug_mask != FDISK_DEBUG_INIT) \
+                                       fflush(stderr);                 \
+                       } while(0)
+
+extern int fdisk_debug_mask;
+extern void fdisk_init_debug(int mask);
+
 struct partition {
        unsigned char boot_ind;         /* 0x80 - active */
        unsigned char head;             /* starting head */
index 5138c043aee2139892a9020b6925ff3021b37434..f97aa4fe45f6f2e7c3f6ca7eb767a8e6d8e65fcb 100644 (file)
 #include "common.h"
 #include "fdisk.h"
 
+int fdisk_debug_mask;
+
+/**
+ * fdisk_init_debug:
+ * @mask: debug mask (0xffff to enable full debuging)
+ *
+ * If the @mask is not specified then this function reads
+ * FDISK_DEBUG environment variable to get the mask.
+ *
+ * Already initialized debugging stuff cannot be changed. It does not
+ * have effect to call this function twice.
+ */
+void fdisk_init_debug(int mask)
+{
+       if (fdisk_debug_mask & FDISK_DEBUG_INIT)
+               return;
+       if (!mask) {
+               char *str = getenv("FDISK_DEBUG");
+               if (str)
+                       fdisk_debug_mask = strtoul(str, 0, 0);
+       } else
+               fdisk_debug_mask = mask;
+
+       if (fdisk_debug_mask)
+               printf("fdisk: debug mask set to 0x%04x.\n",
+                      fdisk_debug_mask);
+       fdisk_debug_mask |= FDISK_DEBUG_INIT;
+}
+
 /**
  * fdisk_new_context:
  *
@@ -38,9 +67,11 @@ struct fdisk_context *fdisk_new_context_from_filename(const char *fname)
         * Attempt to open the device with r-w permissions
         * by default, otherwise try read-only.
         */
-       if ((fd = open(fname, O_RDWR)) < 0)
+       if ((fd = open(fname, O_RDWR)) < 0) {
                if ((fd = open(fname, O_RDONLY)) < 0)
                        return NULL;
+               DBG(CONTEXT, printf("opened %s as read-only\n", fname));
+       }
 
        cxt = calloc(1, sizeof(*cxt));
        if (!cxt)