diff --git a/kern/include/lib.h b/kern/include/lib.h index b010a7b..a25c079 100644 --- a/kern/include/lib.h +++ b/kern/include/lib.h @@ -197,4 +197,13 @@ void kprintf_bootstrap(void); void random_yielder(uint32_t); void random_spinner(uint32_t); +/* + * Testing variants of kprintf. tprintf is silent during automated testing. + * sprintf prefixes the kernel secret to kprintf messages during automated + * testing. + */ + +int tkprintf(const char *format, ...) __PF(1,2); +int skprintf(const char *format, ...) __PF(1,2); + #endif /* _LIB_H_ */ diff --git a/kern/lib/kprintf.c b/kern/lib/kprintf.c index 3adc7a3..397355e 100644 --- a/kern/lib/kprintf.c +++ b/kern/lib/kprintf.c @@ -39,6 +39,7 @@ #include #include // for vfs_sync() #include // for ltrace_stop() +#include /* Flags word for DEBUG() macro. */ @@ -90,13 +91,13 @@ console_send(void *junk, const char *data, size_t len) } /* - * Printf to the console. + * kprintf and tprintf helper function. */ +inline int -kprintf(const char *fmt, ...) +vkprintf(const char *fmt, va_list ap) { int chars; - va_list ap; bool dolock; dolock = kprintf_lock != NULL @@ -111,9 +112,7 @@ kprintf(const char *fmt, ...) spinlock_acquire(&kprintf_spinlock); } - va_start(ap, fmt); chars = __vprintf(console_send, NULL, fmt, ap); - va_end(ap); if (dolock) { lock_release(kprintf_lock); @@ -125,6 +124,62 @@ kprintf(const char *fmt, ...) return chars; } +/* + * Printf to the console. + */ +int +kprintf(const char *fmt, ...) +{ + int chars; + va_list ap; + + va_start(ap, fmt); + chars = vkprintf(fmt, ap); + va_end(ap); + + return chars; +} + +/* + * kprintf variant that is quiet during automated testing + */ +int +tkprintf(const char *fmt, ...) +{ + int chars; + va_list ap; + + if (KERNEL_SECRET != 0) { + return 0; + } + + va_start(ap, fmt); + chars = vkprintf(fmt, ap); + va_end(ap); + + return chars; +} + +/* + * kprintf variant that prints the automated secret + */ +int +skprintf(const char *fmt, ...) +{ + int chars; + va_list ap; + + if (KERNEL_SECRET != 0) { + kprintf("SECRET=%llu ", KERNEL_SECRET); + } + + va_start(ap, fmt); + chars = vkprintf(fmt, ap); + va_end(ap); + + return chars; +} + /* * panic() is for fatal errors. It prints the printf arguments it's * passed and then halts the system.