From ae3c354e844698bdb4ed35a6845aa9dca1e9205f Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Fri, 4 Apr 2014 13:05:18 +0800 Subject: [PATCH 1/1] log: Allow runtime selection of 'debug' log level Currently, we need to compile with -DDEBUG to implement debug-level logging in the UIs and discover server. Since we may not be able to easily replace a system's petitboot binaries, this change introduces a -v|--verbose option to the discver server and ncurses UI, which enables debug at runtime. We also move some of the udev debug code out of an #ifdef DEBUG block. Since petitboot is generally started on boot, we also add a little infrastructure to pass -v to petitboot on certain system contitions: either petitboot.debug on the kernel command line, or a petitboot,debug? NVRAM property containing the value 'true'. Signed-off-by: Jeremy Kerr --- discover/pb-discover.c | 13 +++++++++++-- discover/udev.c | 23 +++++++++-------------- lib/log/log.c | 5 +++++ lib/log/log.h | 1 + ui/ncurses/generic-main.c | 12 ++++++++++-- utils/pb-console | 9 ++++++++- utils/pb-sysinfo | 6 ++++++ 7 files changed, 50 insertions(+), 19 deletions(-) diff --git a/discover/pb-discover.c b/discover/pb-discover.c index 713d99d..e895f47 100644 --- a/discover/pb-discover.c +++ b/discover/pb-discover.c @@ -29,7 +29,7 @@ static void print_usage(void) print_version(); printf( "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n" -" [-n, --dry-run] [-V, --version]\n"); +" [-n, --dry-run] [-v, --verbose] [-V, --version]\n"); } /** @@ -48,6 +48,7 @@ struct opts { const char *log_file; enum opt_value dry_run; enum opt_value show_version; + enum opt_value verbose; }; /** @@ -61,14 +62,16 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"log", required_argument, NULL, 'l'}, {"dry-run", no_argument, NULL, 'n'}, + {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, { NULL, 0, NULL, 0}, }; - static const char short_options[] = "ahl:nV"; + static const char short_options[] = "ahl:nvV"; static const struct opts default_values = { .no_autoboot = opt_no, .log_file = "/var/log/petitboot/pb-discover.log", .dry_run = opt_no, + .verbose = opt_no, }; *opts = default_values; @@ -93,6 +96,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) case 'n': opts->dry_run = opt_yes; break; + case 'v': + opts->verbose = opt_yes; + break; case 'V': opts->show_version = opt_yes; break; @@ -147,6 +153,9 @@ int main(int argc, char *argv[]) } pb_log_init(log); + if (opts.verbose) + pb_log_set_debug(true); + pb_log("--- pb-discover ---\n"); /* we look for closed sockets when we write, so ignore SIGPIPE */ diff --git a/discover/udev.c b/discover/udev.c index 7db36a7..9e1a5ce 100644 --- a/discover/udev.c +++ b/discover/udev.c @@ -263,23 +263,18 @@ static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev) static int udev_handle_dev_action(struct udev_device *dev, const char *action) { struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev)); + struct udev_list_entry *list; + const char *name; -#ifdef DEBUG - { - struct udev_list_entry *list; - const char *name; - - list = udev_device_get_properties_list_entry(dev); - name = udev_device_get_sysname(dev); + list = udev_device_get_properties_list_entry(dev); + name = udev_device_get_sysname(dev); - pb_debug("%s: action %s, device %s\n", __func__, action, name); - pb_debug("%s properties:\n", __func__); + pb_debug("udev: action %s, device %s\n", action, name); + pb_debug("udev: properties:\n"); - for (; list; list = udev_list_entry_get_next(list)) - pb_log("\t%-20s: %s\n", udev_list_entry_get_name(list), - udev_list_entry_get_value(list)); - } while (0); -#endif + for (; list; list = udev_list_entry_get_next(list)) + pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list), + udev_list_entry_get_value(list)); if (!strcmp(action, "add")) return udev_handle_dev_add(udev, dev); diff --git a/lib/log/log.c b/lib/log/log.c index a4f5c22..44543d0 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -41,6 +41,11 @@ void __pb_log_init(FILE *fp, bool _debug) debug = _debug; } +void pb_log_set_debug(bool _debug) +{ + debug = _debug; +} + FILE *pb_log_get_stream(void) { static FILE *null_stream; diff --git a/lib/log/log.h b/lib/log/log.h index e34de33..9454596 100644 --- a/lib/log/log.h +++ b/lib/log/log.h @@ -15,6 +15,7 @@ void __pb_log_init(FILE *stream, bool debug); #define pb_log_init(s) __pb_log_init(s, false) #endif +void pb_log_set_debug(bool debug); FILE *pb_log_get_stream(void); #endif /* _LOG_H */ diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c index ad4c0cf..67b0b61 100644 --- a/ui/ncurses/generic-main.c +++ b/ui/ncurses/generic-main.c @@ -49,7 +49,7 @@ static void print_usage(void) print_version(); printf( "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n" -" [-s, --start-daemon] [-V, --version]\n"); +" [-s, --start-daemon] [-v, --verbose] [-V, --version]\n"); } /** @@ -66,6 +66,7 @@ struct opts { enum opt_value show_help; const char *log_file; enum opt_value start_daemon; + enum opt_value verbose; enum opt_value show_version; }; @@ -79,10 +80,11 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"log", required_argument, NULL, 'l'}, {"start-daemon", no_argument, NULL, 's'}, + {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, { NULL, 0, NULL, 0}, }; - static const char short_options[] = "dhl:sV"; + static const char short_options[] = "dhl:svV"; static const struct opts default_values = { 0 }; *opts = default_values; @@ -104,6 +106,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) case 's': opts->start_daemon = opt_yes; break; + case 'v': + opts->verbose = opt_yes; + break; case 'V': opts->show_version = opt_yes; break; @@ -297,6 +302,9 @@ int main(int argc, char *argv[]) pb_log_init(log); + if (opts.verbose) + pb_log_set_debug(true); + pb_log("--- petitboot-nc ---\n"); sa.sa_handler = sig_handler; diff --git a/utils/pb-console b/utils/pb-console index d846ea7..9da899f 100644 --- a/utils/pb-console +++ b/utils/pb-console @@ -96,9 +96,16 @@ do fi done +verbose_opt= +sysinfo_bin=$(dirname $0)/pb-sysinfo +if [ -x $sysinfo_bin ] && $sysinfo_bin --debug-enabled +then + verbose_opt=--verbose +fi + while : do - $ui + $ui $verbose_opt reset echo "Exiting petitboot. Type 'exit' to return." $shell diff --git a/utils/pb-sysinfo b/utils/pb-sysinfo index bd2f0fe..001535a 100755 --- a/utils/pb-sysinfo +++ b/utils/pb-sysinfo @@ -7,4 +7,10 @@ case "$1" in '--id') tr -d '\0' < /proc/device-tree/system-id ;; +'--debug-enabled') + exec >/dev/null 2>&1 + nvram --print-config=petitboot,debug? | grep -q true && exit 0 + grep -Fq petitboot.debug /proc/cmdline && exit 0 + exit 1 + ;; esac -- 2.39.2