Add --debug=parse for option parsing debug

Adds log.h and debug.h to split the logging and debug bits out of
fio, so that the parser can use them.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/README b/README
index cf48c01..ef614a2 100644
--- a/README
+++ b/README
@@ -100,6 +100,7 @@
 	verify		Dump info related to IO verification
 	all		Enable all debug options
 	random		Dump info related to random offset generation
+	parse		Dump info related to option matching and parsing
 	? or help	Show available debug options.
 
 You can specify as many as you want, eg --debug=file,mem will enable
diff --git a/debug.h b/debug.h
new file mode 100644
index 0000000..b11a901
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,42 @@
+#ifndef FIO_DEBUG_H
+#define FIO_DEBUG_H
+
+#include <assert.h>
+#include "log.h"
+
+enum {
+	FD_PROCESS	= 0,
+	FD_FILE,
+	FD_IO,
+	FD_MEM,
+	FD_BLKTRACE,
+	FD_VERIFY,
+	FD_RANDOM,
+	FD_PARSE,
+	FD_DEBUG_MAX,
+};
+
+#ifdef FIO_INC_DEBUG
+struct debug_level {
+	const char *name;
+	unsigned long shift;
+};
+extern struct debug_level debug_levels[];
+
+extern unsigned long fio_debug;
+
+#define dprint(type, str, args...)				\
+	do {							\
+		assert(type < FD_DEBUG_MAX);			\
+		if ((((1 << type)) & fio_debug) == 0)		\
+			break;					\
+		log_info("%-8s ", debug_levels[(type)].name);	\
+		log_info(str, ##args);				\
+	} while (0)
+
+#else
+
+#define dprint(type, str, args...)
+#endif
+
+#endif
diff --git a/fio.h b/fio.h
index 028ef4c..0c5b648 100644
--- a/fio.h
+++ b/fio.h
@@ -22,6 +22,8 @@
 #include "arch/arch.h"
 #include "os/os.h"
 #include "mutex.h"
+#include "log.h"
+#include "debug.h"
 
 #ifdef FIO_HAVE_GUASI
 #include <guasi.h>
@@ -659,8 +661,6 @@
 extern int shm_id;
 extern int groupid;
 extern int terse_output;
-extern FILE *f_out;
-extern FILE *f_err;
 extern int temp_stall_ts;
 extern unsigned long long mlock_size;
 extern unsigned long page_mask, page_size;
@@ -903,20 +903,6 @@
 extern int load_blktrace(struct thread_data *, const char *);
 #endif
 
-/*
- * If logging output to a file, stderr should go to both stderr and f_err
- */
-#define log_err(args...)	do {		\
-	fprintf(f_err, ##args);			\
-	if (f_err != stderr)			\
-		fprintf(stderr, ##args);	\
-	} while (0)
-
-#define log_info(args...)	fprintf(f_out, ##args)
-
-FILE *get_f_out(void);
-FILE *get_f_err(void);
-
 struct ioengine_ops {
 	struct list_head list;
 	char name[16];
@@ -972,35 +958,7 @@
 	td->verror[0] = '\0';
 }
 
-enum {
-	FD_PROCESS	= 0,
-	FD_FILE,
-	FD_IO,
-	FD_MEM,
-	FD_BLKTRACE,
-	FD_VERIFY,
-	FD_RANDOM,
-	FD_DEBUG_MAX,
-};
-
 #ifdef FIO_INC_DEBUG
-struct debug_level {
-	const char *name;
-	unsigned long shift;
-};
-extern struct debug_level debug_levels[];
-
-extern unsigned long fio_debug;
-
-#define dprint(type, str, args...)				\
-	do {							\
-		assert(type < FD_DEBUG_MAX);			\
-		if ((((1 << type)) & fio_debug) == 0)		\
-			break;					\
-		log_info("%-8s ", debug_levels[(type)].name);	\
-		log_info(str, ##args);				\
-	} while (0)
-
 static inline void dprint_io_u(struct io_u *io_u, const char *p)
 {
 	struct fio_file *f = io_u->file;
@@ -1015,7 +973,6 @@
 	}
 }
 #else
-#define dprint(type, str, args...)
 #define dprint_io_u(io_u, p)
 #endif
 
diff --git a/init.c b/init.c
index 4f57ba3..6599729 100644
--- a/init.c
+++ b/init.c
@@ -812,6 +812,7 @@
 	{ .name = "blktrace",	.shift = FD_BLKTRACE },
 	{ .name = "verify",	.shift = FD_VERIFY },
 	{ .name = "random",	.shift = FD_RANDOM },
+	{ .name = "parse",	.shift = FD_PARSE },
 	{ },
 };
 
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..12c9a55
--- /dev/null
+++ b/log.h
@@ -0,0 +1,21 @@
+#ifndef FIO_LOG_H
+#define FIO_LOG_H
+
+extern FILE *f_out;
+extern FILE *f_err;
+
+/*
+ * If logging output to a file, stderr should go to both stderr and f_err
+ */
+#define log_err(args...)	do {		\
+	fprintf(f_err, ##args);			\
+	if (f_err != stderr)			\
+		fprintf(stderr, ##args);	\
+	} while (0)
+
+#define log_info(args...)	fprintf(f_out, ##args)
+
+FILE *get_f_out(void);
+FILE *get_f_err(void);
+
+#endif
diff --git a/parse.c b/parse.c
index 90874f1..3c20312 100644
--- a/parse.c
+++ b/parse.c
@@ -10,6 +10,7 @@
 #include <limits.h>
 
 #include "parse.h"
+#include "debug.h"
 
 static int vp_cmp(const void *p1, const void *p2)
 {
@@ -229,6 +230,9 @@
 	char **cp;
 	int ret = 0, is_time = 0;
 
+	dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
+							o->type, ptr);
+
 	if (!ptr && o->type != FIO_OPT_STR_SET) {
 		fprintf(stderr, "Option %s requires an argument\n", o->name);
 		return 1;
@@ -414,6 +418,8 @@
 	const char *ptr2 = NULL;
 	int r1, r2;
 
+	dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
+
 	/*
 	 * See if we have a second set of parameters, hidden after a comma.
 	 * Do this before parsing the first round, to check if we should
@@ -661,6 +667,8 @@
 {
 	struct fio_option *o;
 
+	dprint(FD_PARSE, "filling default options\n");
+
 	for (o = &options[0]; o->name; o++)
 		if (o->def)
 			handle_option(o, o->def, data);
@@ -674,6 +682,8 @@
 {
 	struct fio_option *o;
 
+	dprint(FD_PARSE, "init options\n");
+
 	for (o = &options[0]; o->name; o++) {
 		if (o->type == FIO_OPT_BOOL) {
 			o->minval = 0;