Skip to contentSkip to conversation

fdisk: add debug support

PATCH Applied Davidlohr Bueso 2 participants 3 messages May 20, 2012 – May 23, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 6/6 fdisk: add debug support

From: Davidlohr Bueso <[email protected]>

Based on libmnt, this patch adds basic debugging support for fdisk. Currently only CONTEXT is debugged, yet keeps exact functionality as libmnt/libblkid.

---
 fdisk/fdisk.c |    2 ++
 fdisk/fdisk.h |   27 +++++++++++++++++++++++++++
 fdisk/utils.c |   33 ++++++++++++++++++++++++++++++++-
 3 files changed, 61 insertions(+), 1 deletions(-)
fdisk/fdisk.c +2 -0
index 400e682..9b91fa0 100644
@@ -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"));
fdisk/fdisk.h +27 -0
index c43517c..2471541 100644
@@ -32,6 +32,33 @@
 #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 */
fdisk/utils.c +32 -1
index ad83d7f..9f071a6 100644
@@ -23,6 +23,35 @@
 #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:
  *
@@ -37,9 +66,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)
 			goto ret;
+		DBG(CONTEXT, printf("opened %s as read-only\n", fname));
+	}
 
 	cxt = calloc(1, sizeof(*cxt));
 	if (!cxt)
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 1 line · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

Rebase as this patch no longer applies with the changes from 5/6. Thanks.

From: Davidlohr Bueso <[email protected]>
Date: Mon, 21 May 2012 22:30:14 +0200
Subject: [PATCH] fdisk: add debug support

Based on libmnt, this patch adds basic debugging support for fdisk. Currently only CONTEXT is debugged, yet keeps exact functionality as libmnt/libblkid.

---
 fdisk/fdisk.c |    2 ++
 fdisk/fdisk.h |   27 +++++++++++++++++++++++++++
 fdisk/utils.c |   33 ++++++++++++++++++++++++++++++++-
 3 files changed, 61 insertions(+), 1 deletions(-)
fdisk/fdisk.c +2 -0
index 71c009c..1da4ffe 100644
@@ -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"));
fdisk/fdisk.h +27 -0
index c43517c..2471541 100644
@@ -32,6 +32,33 @@
 #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 */
fdisk/utils.c +32 -1
index 5138c04..f97aa4f 100644
@@ -24,6 +24,35 @@
 #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)
Signature · 1 line
1.7.4.1
maintainer Applied
#2 lore raw
Quoted 4 lines · Davidlohr Bueso
 fdisk/fdisk.c |    2 ++
 fdisk/fdisk.h |   27 +++++++++++++++++++++++++++
 fdisk/utils.c |   33 ++++++++++++++++++++++++++++++++-
 3 files changed, 61 insertions(+), 1 deletions(-)

Applied, thanks.

Quoted 4 lines
fdisk/utils.c +4 -0
+void fdisk_init_debug(int mask)
+{
+	if (fdisk_debug_mask & FDISK_DEBUG_INIT)
+		return;

fdisk/utils.c line 52

Quoted 10 lines
fdisk/utils.c +10 -0
+	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);

this is bug (in libmount too), the message should be printed to stderr as all others debug messages.

I have added dbgprint() inline function to standardize the output, so

fdisk/utils.c struct fdisk_context *fdisk_new_context_from_filename(const char *fname) · line 73

Quoted 1 line
fdisk/utils.c +1 -0
+		DBG(CONTEXT, printf("opened %s as read-only\n", fname));

should be

        DBG(CONTEXT, dbgprint("opened %s as read-only", fname));

All the changes are already pushed to the repository.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com