From a972b4a4eb1466195f36d2e762d5d43d5e152e6c Mon Sep 17 00:00:00 2001 From: uebayasi Date: Sun, 13 Jul 2014 23:49:40 +0000 Subject: [PATCH] KASSERTMSG(9): New kernel assertion with message KASSERT() is annoying as it only prints the expression as a string. If you (developers) want to know a little more information, you have to do: #ifdef DIAGNOSTIC if (bad) panic(...); #endif KASSERTMSG() replaces it into a single line: KASSERTMSG(!bad, ...); Taken from NetBSD. (There is a concern that KASSERT() messages are too long; consume more memory, and not friendly for small monitors. This have to be considered & revisited later.) "Like" from henning@ Man page review & advices from jmc@ and schwarze@ --- share/man/man9/Makefile | 3 ++- share/man/man9/kern.9 | 29 +++++++++++++++++++++++++++-- sys/kern/kern_physio.c | 18 +++++------------- sys/kern/subr_prf.c | 5 ++--- sys/lib/libkern/libkern.h | 18 +++++++++++++++++- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index 95453c67680..c69a8933ce9 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.216 2014/07/13 09:37:22 mpi Exp $ +# $OpenBSD: Makefile,v 1.217 2014/07/13 23:49:40 uebayasi Exp $ # $NetBSD: Makefile,v 1.4 1996/01/09 03:23:01 thorpej Exp $ # Makefile for section 9 (kernel function and variable) manual pages. @@ -219,6 +219,7 @@ MLINKS+=iic.9 iic_acquire_bus.9 iic.9 iic_release_bus.9 iic.9 iic_exec.9 \ MLINKS+=kern.9 imax.9 kern.9 imin.9 kern.9 lmax.9 kern.9 lmin.9 \ kern.9 max.9 kern.9 min.9 kern.9 ulmax.9 kern.9 ulmin.9 kern.9 abs.9 \ kern.9 assert.9 kern.9 KASSERT.9 kern.9 KDASSERT.9 \ + kern.9 KASSERTMSG.9 kern.9 KDASSERTMSG.9 \ kern.9 skpc.9 kern.9 scanc.9 kern.9 bcmp.9 \ kern.9 memchr.9 kern.9 memcmp.9 kern.9 ffs.9 kern.9 fls.9 kern.9 flsl.9 \ kern.9 strlen.9 kern.9 strncpy.9 kern.9 strlcpy.9 kern.9 strlcat.9 \ diff --git a/share/man/man9/kern.9 b/share/man/man9/kern.9 index 315572de2d5..8d98eff1b73 100644 --- a/share/man/man9/kern.9 +++ b/share/man/man9/kern.9 @@ -1,4 +1,4 @@ -.\" $OpenBSD: kern.9,v 1.18 2013/07/11 01:24:12 jsg Exp $ +.\" $OpenBSD: kern.9,v 1.19 2014/07/13 23:49:40 uebayasi Exp $ .\" .\" Copyright (c) 2002, 2003 CubeSoft Communications, Inc. .\" @@ -24,7 +24,7 @@ .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: July 11 2013 $ +.Dd $Mdocdate: July 13 2014 $ .Dt KERN 9 .Os .Sh NAME @@ -93,6 +93,10 @@ function computes the absolute value of integer .Fn KASSERT "CONDITION" .Ft "void" .Fn KDASSERT "CONDITION" +.Ft "void" +.Fn KASSERTMSG "CONDITION" "fmt" "..." +.Ft "void" +.Fn KDASSERTMSG "CONDITION" "fmt" "..." .nr nS 0 .Pp These macros cause kernel @@ -101,13 +105,29 @@ if the given condition evaluates to false. .Fn assert tests are always compiled in. .Fn KASSERT +and +.Fn KASSERTMSG tests are only included if the kernel has .Dv DIAGNOSTIC enabled. .Fn KDASSERT +and +.Fn KDASSERTMSG tests are only included if the kernel has .Dv DEBUG enabled. +The +.Fn KASSERTMSG +and +.Fn KDASSERTMSG +macros append +to the +.Xr panic 9 +format string the message specified by +.Fa format +and its subsequent arguments, similar to +.Xr printf 9 +functions. .Pp .nr nS 1 .Fn CTASSERT "CONDITION" @@ -251,3 +271,8 @@ The and .Fn scanc functions are based on vax instructions of the same name. +The +.Fn KASSERTMSG +and +.Fn KDASSERTMSG +macros are taken from NetBSD. diff --git a/sys/kern/kern_physio.c b/sys/kern/kern_physio.c index 60d44331d28..bf588f26ba9 100644 --- a/sys/kern/kern_physio.c +++ b/sys/kern/kern_physio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_physio.c,v 1.39 2011/07/18 02:49:20 matthew Exp $ */ +/* $OpenBSD: kern_physio.c,v 1.40 2014/07/13 23:49:40 uebayasi Exp $ */ /* $NetBSD: kern_physio.c,v 1.28 1997/05/19 10:43:28 pk Exp $ */ /*- @@ -128,12 +128,8 @@ physio(void (*strategy)(struct buf *), dev_t dev, int flags, */ (*minphys)(bp); todo = bp->b_bcount; -#ifdef DIAGNOSTIC - if (todo < 0) - panic("todo < 0; minphys broken"); - if (todo > MAXPHYS) - panic("todo > MAXPHYS; minphys broken"); -#endif + KASSERTMSG(todo >= 0, "minphys broken"); + KASSERTMSG(todo <= MAXPHYS, "minphys broken"); /* * [lock the part of the user address space involved @@ -194,12 +190,8 @@ physio(void (*strategy)(struct buf *), dev_t dev, int flags, * of data to transfer] */ done = bp->b_bcount - bp->b_resid; -#ifdef DIAGNOSTIC - if (done < 0) - panic("done < 0; strategy broken"); - if (done > todo) - panic("done > todo; strategy broken"); -#endif + KASSERTMSG(done >= 0, "strategy broken"); + KASSERTMSG(done <= todo, "strategy broken"); iovp->iov_len -= done; iovp->iov_base = (caddr_t)iovp->iov_base + done; uio->uio_offset += done; diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index ed134d1fe3a..899fcb529d5 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr_prf.c,v 1.82 2014/07/11 14:36:44 uebayasi Exp $ */ +/* $OpenBSD: subr_prf.c,v 1.83 2014/07/13 23:49:40 uebayasi Exp $ */ /* $NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $ */ /*- @@ -156,8 +156,7 @@ void __assert(const char *t, const char *f, int l, const char *e) { - panic("kernel %sassertion \"%s\" failed: file \"%s\", line %d", - t, e, f, l); + panic(__KASSERTSTR, t, e, f, l); } /* diff --git a/sys/lib/libkern/libkern.h b/sys/lib/libkern/libkern.h index 4e53f92fbc2..d743920ea0b 100644 --- a/sys/lib/libkern/libkern.h +++ b/sys/lib/libkern/libkern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libkern.h,v 1.33 2014/06/10 04:16:57 deraadt Exp $ */ +/* $OpenBSD: libkern.h,v 1.34 2014/07/13 23:49:40 uebayasi Exp $ */ /* $NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $ */ /*- @@ -114,25 +114,41 @@ abs(int j) #endif #endif +#define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d" + #ifndef DIAGNOSTIC +#define KASSERTMSG(e, msg, ...) ((void)0) #define KASSERT(e) ((void)0) #else #ifdef __STDC__ +#define KASSERTMSG(e, msg, ...) ((e) ? (void)0 : \ + panic(__KASSERTSTR " " msg, "diagnostic ", #e, \ + __FILE__, __LINE__, ## __VA_ARGS__)) #define KASSERT(e) ((e) ? (void)0 : \ __assert("diagnostic ", __FILE__, __LINE__, #e)) #else +#define KASSERTMSG(e, msg, ...) ((e) ? (void)0 : \ + panic(__KASSERTSTR " " msg, "diagnostic ", "e", \ + __FILE__, __LINE__, ## __VA_ARGS__)) #define KASSERT(e) ((e) ? (void)0 : \ __assert("diagnostic ", __FILE__, __LINE__, "e")) #endif #endif #ifndef DEBUG +#define KDASSERTMSG(e, msg, ...) ((void)0) #define KDASSERT(e) ((void)0) #else #ifdef __STDC__ +#define KDASSERTMSG(e, msg, ...) ((e) ? (void)0 : \ + panic(__KASSERTSTR " " msg, "debugging ", #e, \ + __FILE__, __LINE__, ## __VA_ARGS__)) #define KDASSERT(e) ((e) ? (void)0 : \ __assert("debugging ", __FILE__, __LINE__, #e)) #else +#define KDASSERTMSG(e, msg, ...) ((e) ? (void)0 : \ + panic(__KASSERTSTR " " msg, "debugging ", "e", \ + __FILE__, __LINE__, ## __VA_ARGS__)) #define KDASSERT(e) ((e) ? (void)0 : \ __assert("debugging ", __FILE__, __LINE__, "e")) #endif -- 2.20.1