tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

KASSERTMSG fix



Hi list,

KASSERT(9) states the following:

  The panic message will display the style of assertion (debugging vs.
  diagnostic), the expression that failed and the filename, and line
  number the failure happened on.  The KASSERTMSG() macro
  appends additional message to the panic(9) format string.

However, the way KASSERTMSG is defined in libkern.h cannot do this, as
only the msg part is passed to panic(9) therefore missing to display the
filename, line, and expression that triggered the assertion.

The attached patch takes care of this; it does not really "append" the
msg to the panic string (so it won't be available via panicstr like
before), but now KASSERTMSG() will print the additional info.

Of course, an alternative is to fix the man page.

Opinions?

-- 
Jean-Yves Migeon
jeanyves.migeon%free.fr@localhost
Index: sys/lib/libkern/kern_assert.c
===================================================================
RCS file: /cvsroot/src/sys/lib/libkern/kern_assert.c,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 kern_assert.c
--- sys/lib/libkern/kern_assert.c       19 Jan 2010 22:28:30 -0000      1.1
+++ sys/lib/libkern/kern_assert.c       7 Sep 2011 14:28:32 -0000
@@ -38,6 +38,9 @@
 #include <lib/libkern/libkern.h>
 #endif
 
+static const char fmt[] = \
+       "kernel %sassertion \"%s\" failed: file \"%s\", line %d"; 
+
 void
 kern_assert(const char *t, const char *f, int l, const char *e)
 {
@@ -47,6 +50,12 @@ kern_assert(const char *t, const char *f
                return;
 #endif
 
-       panic("kernel %sassertion \"%s\" failed: file \"%s\", line %d",
-           t, e, f, l);
+       panic(fmt, t, e, f, l);
+}
+
+void
+kern_assert_print(const char *t, const char *f, int l, const char *e)
+{
+       printf(fmt, t, e, f, l);
+       printf("\n");
 }
Index: sys/lib/libkern/libkern.h
===================================================================
RCS file: /cvsroot/src/sys/lib/libkern/libkern.h,v
retrieving revision 1.99
diff -u -p -u -p -r1.99 libkern.h
--- sys/lib/libkern/libkern.h   1 Sep 2011 22:35:17 -0000       1.99
+++ sys/lib/libkern/libkern.h   7 Sep 2011 14:28:32 -0000
@@ -206,9 +206,10 @@ tolower(int ch)
 #else /* DIAGNOSTIC */
 #define _DIAGASSERT(a) assert(a)
 #define        KASSERTMSG(e, msg) do {         \
-       if (__predict_false(!(e)))      \
+       if (__predict_false(!(e))) {    \
+               kern_assert_print("diagnostic ", __FILE__, __LINE__, #e); \
                panic msg;              \
-       } while (/*CONSTCOND*/ 0)
+       } } while (/*CONSTCOND*/ 0)
 #ifdef __STDC__
 #define        KASSERT(e)      (__predict_true((e)) ? (void)0 :                
    \
                            kern_assert("diagnostic ", __FILE__, __LINE__, #e))
@@ -228,9 +229,10 @@ tolower(int ch)
 #endif /* lint */
 #else
 #define        KDASSERTMSG(e, msg) do {        \
-       if (__predict_false(!(e)))      \
+       if (__predict_false(!(e))) {    \
+               kern_assert_print("debugging ", __FILE__, __LINE__, #e); \
                panic msg;              \
-       } while (/*CONSTCOND*/ 0)
+       } } while (/*CONSTCOND*/ 0)
 #ifdef __STDC__
 #define        KDASSERT(e)     (__predict_true((e)) ? (void)0 :                
    \
                            kern_assert("debugging ", __FILE__, __LINE__, #e))
@@ -308,6 +310,7 @@ int  ffs(int);
 #endif
 
 void    kern_assert(const char *, const char *, int, const char *);
+void    kern_assert_print(const char *, const char *, int, const char *);
 unsigned int
        bcdtobin(unsigned int);
 unsigned int


Home | Main Index | Thread Index | Old Index