From e5909c170bde83b730e6d056e9081199d9f7d69d Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 12 Feb 2018 17:33:14 +0000 Subject: [PATCH] Add a set of convenience routines for RTC drivers to use for debug output, and a debug.clock_show_io sysctl to control debugging output. --- sys/kern/subr_rtc.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ sys/sys/clock.h | 12 ++++++++++ 2 files changed, 70 insertions(+) diff --git a/sys/kern/subr_rtc.c b/sys/kern/subr_rtc.c index 34a30879f47c..6440fe66f25f 100644 --- a/sys/kern/subr_rtc.c +++ b/sys/kern/subr_rtc.c @@ -76,6 +76,10 @@ __FBSDID("$FreeBSD$"); #include "clock_if.h" +static int show_io; +SYSCTL_INT(_debug, OID_AUTO, clock_show_io, CTLFLAG_RWTUN, &show_io, 0, + "Enable debug printing of RTC clock I/O; 1=reads, 2=writes, 3=both."); + /* XXX: should be kern. now, it's no longer machdep. */ static int disable_rtc_set; SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, @@ -144,6 +148,60 @@ settime_task_func(void *arg, int pending) CLOCK_SETTIME(rtc->clockdev, &ts); } +static void +clock_dbgprint_hdr(device_t dev, int rw) +{ + struct timespec now; + + getnanotime(&now); + device_printf(dev, "%s at ", (rw & CLOCK_DBG_READ) ? "read " : "write"); + clock_print_ts(&now, 9); + printf(": "); +} + +void +clock_dbgprint_bcd(device_t dev, int rw, const struct bcd_clocktime *bct) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_bcd(bct, 9); + printf("\n"); + } +} + +void +clock_dbgprint_ct(device_t dev, int rw, const struct clocktime *ct) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_ct(ct, 9); + printf("\n"); + } +} + +void +clock_dbgprint_err(device_t dev, int rw, int err) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + printf("error = %d\n", err); + } +} + +void +clock_dbgprint_ts(device_t dev, int rw, const struct timespec *ts) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_ts(ts, 9); + printf("\n"); + } +} + void clock_register_flags(device_t clockdev, long resolution, int flags) { diff --git a/sys/sys/clock.h b/sys/sys/clock.h index a5e89746d488..bfbd6efd2ad3 100644 --- a/sys/sys/clock.h +++ b/sys/sys/clock.h @@ -191,6 +191,18 @@ void clock_print_bcd(const struct bcd_clocktime *bct, int nsdig); void clock_print_ct(const struct clocktime *ct, int nsdig); void clock_print_ts(const struct timespec *ts, int nsdig); +/* + * Debugging helpers for RTC clock drivers. Print a [bcd_]clocktime or + * timespec, only if rtc clock debugging has been enabled. The rw argument is + * one of CLOCK_DBG_READ or CLOCK_DBG_WRITE. + */ +#define CLOCK_DBG_READ 0x01 +#define CLOCK_DBG_WRITE 0x02 +void clock_dbgprint_bcd(device_t dev, int rw, const struct bcd_clocktime *bct); +void clock_dbgprint_ct(device_t dev, int rw, const struct clocktime *ct); +void clock_dbgprint_err(device_t dev, int rw, int err); +void clock_dbgprint_ts(device_t dev, int rw, const struct timespec *ts); + #endif /* _KERNEL */ #endif /* !_SYS_CLOCK_H_ */