From: Arnaud Lacombe <lacom...@gmail.com>

Hi folks,

There is many case recently when I really wished timestamp were present in the
post-mortem msgbuf. Such situation could be when userland application segfault
potentially triggering a panic/crash, or have information about the time-wise
location of a given message (kernel or userland).

Attached patch is available in the git repository at:
  git://github.com/lacombar/freebsd.git master/topic/msgbuf-timestamp

Arnaud Lacombe (3):
      msgbuf(4): convert `msg_needsnl' to a bit flag
      msgbuf(4): add logic to prepend timestamp on new line
      msgbuf(4): add a sysctl to toggle timestamp prepend

 sys/kern/subr_msgbuf.c |   53 ++++++++++++++++++++++++++++++++++++++++-------
 sys/sys/msgbuf.h       |    4 ++-
 2 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/sys/kern/subr_msgbuf.c b/sys/kern/subr_msgbuf.c
index cd9c551..b2f0e1a 100644
--- a/sys/kern/subr_msgbuf.c
+++ b/sys/kern/subr_msgbuf.c
@@ -34,6 +34,7 @@
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/msgbuf.h>
+#include <sys/sysctl.h>
 
 /*
  * Maximum number conversion buffer length: uintmax_t in base 2, plus <>
@@ -47,6 +48,13 @@
 static u_int msgbuf_cksum(struct msgbuf *mbp);
 
 /*
+ *
+ */
+static int msgbuf_show_timestamp = 1;
+SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW,
+    &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf");
+
+/*
  * Initialize a message buffer of the specified size at the specified
  * location. This also zeros the buffer area.
  */
@@ -60,7 +68,7 @@ msgbuf_init(struct msgbuf *mbp, void *ptr, int size)
        msgbuf_clear(mbp);
        mbp->msg_magic = MSG_MAGIC;
        mbp->msg_lastpri = -1;
-       mbp->msg_needsnl = 0;
+       mbp->msg_flags = 0;
        bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
        mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
 }
@@ -95,7 +103,7 @@ msgbuf_reinit(struct msgbuf *mbp, void *ptr, int size)
 
        mbp->msg_lastpri = -1;
        /* Assume that the old message buffer didn't end in a newline. */
-       mbp->msg_needsnl = 1;
+       mbp->msg_flags |= MSGBUF_NEEDNL;
        bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
        mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
 }
@@ -134,7 +142,7 @@ msgbuf_getcount(struct msgbuf *mbp)
  * The caller should hold the message buffer spinlock.
  */
 static inline void
-msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c)
+__msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c)
 {
        u_int pos;
 
@@ -149,6 +157,34 @@ msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c)
        *seq = MSGBUF_SEQNORM(mbp, *seq + 1);
 }
 
+static inline void
+msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c)
+{
+
+       if (msgbuf_show_timestamp && mbp->msg_flags & MSGBUF_NEXT_NEW_LINE) {
+               char buf[32], *bufp;
+               struct timespec ts;
+               int err;
+
+               buf[0] = '\0';
+               getnanouptime(&ts);
+               err = snprintf(buf, sizeof buf, "[%d.%ld] ", ts.tv_sec, 
ts.tv_nsec / 1000);
+
+               bufp = buf;
+               while (*bufp != '\0') {
+                       __msgbuf_do_addchar(mbp, seq, *bufp);
+                       bufp++;
+               }
+
+               mbp->msg_flags &= ~MSGBUF_NEXT_NEW_LINE;
+       }
+
+       __msgbuf_do_addchar(mbp, seq, c);
+
+       if (c == '\n')
+               mbp->msg_flags |= MSGBUF_NEXT_NEW_LINE;
+}
+
 /*
  * Append a character to a message buffer.
  */
@@ -207,10 +243,10 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int 
filter_cr)
         * did not end with a newline.  If that is the case, we need to
         * insert a newline before this string.
         */
-       if (mbp->msg_lastpri != pri && mbp->msg_needsnl != 0) {
+       if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
 
                msgbuf_do_addchar(mbp, &seq, '\n');
-               mbp->msg_needsnl = 0;
+               mbp->msg_flags &= ~MSGBUF_NEEDNL;
        }
 
        for (i = 0; i < len; i++) {
@@ -219,7 +255,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int 
filter_cr)
                 * (and therefore prefix_len != 0), then we need a priority
                 * prefix for this line.
                 */
-               if (mbp->msg_needsnl == 0 && prefix_len != 0) {
+               if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
                        int j;
 
                        for (j = 0; j < prefix_len; j++)
@@ -242,9 +278,9 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int 
filter_cr)
                 * we need to insert a new prefix or insert a newline later.
                 */
                if (str[i] == '\n')
-                       mbp->msg_needsnl = 0;
+                       mbp->msg_flags &= ~MSGBUF_NEEDNL;
                else
-                       mbp->msg_needsnl = 1;
+                       mbp->msg_flags |= MSGBUF_NEEDNL;
 
                msgbuf_do_addchar(mbp, &seq, str[i]);
        }
@@ -395,3 +431,4 @@ msgbuf_copy(struct msgbuf *src, struct msgbuf *dst)
        while ((c = msgbuf_getchar(src)) >= 0)
                msgbuf_addchar(dst, c);
 }
+
diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h
index 67f80a5..639ed72 100644
--- a/sys/sys/msgbuf.h
+++ b/sys/sys/msgbuf.h
@@ -46,7 +46,9 @@ struct msgbuf {
        u_int      msg_cksum;           /* checksum of contents */
        u_int      msg_seqmod;          /* range for sequence numbers */
        int        msg_lastpri;         /* saved priority value */
-       int        msg_needsnl;         /* set when newline needed */
+       uint32_t   msg_flags;
+#define MSGBUF_NEEDNL          0x01    /* set when newline needed */
+#define MSGBUF_NEXT_NEW_LINE   0x02
        struct mtx msg_lock;            /* mutex to protect the buffer */
 };
 
_______________________________________________
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"

Reply via email to