From dak@wanadoo.fr  Fri Sep 27 11:35:18 2002
Return-Path: <dak@wanadoo.fr>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 7895237B401
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 27 Sep 2002 11:35:18 -0700 (PDT)
Received: from nebula.wanadoo.fr (ca-sqy-5-26.abo.wanadoo.fr [80.8.58.26])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D2BF543E42
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 27 Sep 2002 11:35:16 -0700 (PDT)
	(envelope-from dak@wanadoo.fr)
Received: from nebula.wanadoo.fr (localhost [127.0.0.1])
	by nebula.wanadoo.fr (8.12.5/8.12.5) with ESMTP id g8RIareQ091645
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 27 Sep 2002 20:36:53 +0200 (CEST)
	(envelope-from dak@nebula.wanadoo.fr)
Received: (from dak@localhost)
	by nebula.wanadoo.fr (8.12.6/8.12.6/Submit) id g8RIaqVo091644;
	Fri, 27 Sep 2002 20:36:52 +0200 (CEST)
Message-Id: <200209271836.g8RIaqVo091644@nebula.wanadoo.fr>
Date: Fri, 27 Sep 2002 20:36:52 +0200 (CEST)
From: Aurlien Nephtali <aurelien.nephtali@wanadoo.fr>
Reply-To: Aurlien Nephtali <aurelien.nephtali@wanadoo.fr>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: New option to 'dmesg' which allow to display or not old boot messages
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         43434
>Category:       bin
>Synopsis:       [patch] new option to dmesg(8) which allows to display or not old boot messages
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Sep 27 11:40:01 PDT 2002
>Closed-Date:    
>Last-Modified:  Sat Jun 16 00:00:00 GMT 2007
>Originator:     dak
>Release:        FreeBSD 4.7-RC i386
>Organization:
none
>Environment:
System: FreeBSD nebula.wanadoo.fr 4.7-RC FreeBSD 4.7-RC #168: Fri Sep 27 07:44:50 CEST 2002 dak@nebula.wanadoo.fr:/usr/src/sys/compile/NEBULA i386


	
>Description:
	Some times it could be usefull to display only the last boot message (ie: when grep'ing).
	The attached patch modifies sys/kern/subr_prf.c and dmesg.c (and the also the manual).
	It adds a tag (---<<<BOOT>>>---) at the begining of each boot message (the tag
	macro is stored in sys/sys/msgbuf.h and could also be stored in /usr/include/msgbuf.h)
	dmesg.c is modified and has a new option: -o, which when it is specified, displays the full
	content of the message buffer, otherwise only the last message is displayed).
	This patch has been tested (by me :p) without any troubles on -CURRENT.
	
>How-To-Repeat:
	
>Fix:

--- current.patch begins here ---
--- sbin/dmesg/dmesg.c	Sun Aug 18 19:57:07 2002
+++ sbin/dmesg/dmesg.c	Sun Sep 15 20:12:42 2002
@@ -66,12 +66,35 @@
 	{ NULL },
 };
 
+int getlastbootpos(char *buf, int len);
 void usage(void) __dead2;
 
 #define	KREAD(addr, var) \
 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
 
 int
+getlastbootpos(char *buf, int len)
+{
+	int i;
+	char *ptr, *save;
+
+	/* Skip NULL bytes */
+	for(i = 0; (buf[i] == 0x0) && (i < len); i++);
+
+	if (strlen(buf) == 0)
+		return(-1);
+
+	while((ptr = (char *) strstr((buf +i), BOOT_TAG)) != NULL) {
+		save = (char *) ptr;
+		i += strlen(BOOT_TAG); /* Force strstr() to catch
+					* the next tag
+					*/
+	}
+
+	return((int) ((int) (save + (strlen(BOOT_TAG))) - (int) buf));
+}
+
+int
 main(int argc, char *argv[])
 {
 	int ch, newl, skip;
@@ -84,10 +107,12 @@
 	int pri;
 	size_t buflen;
 	int bufpos;
+	int old = 0;
+	int lastboot = 0;
 
 	(void) setlocale(LC_CTYPE, "");
 	memf = nlistf = NULL;
-	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
+	while ((ch = getopt(argc, argv, "aM:N:o")) != -1)
 		switch(ch) {
 		case 'a':
 			all++;
@@ -98,6 +123,9 @@
 		case 'N':
 			nlistf = optarg;
 			break;
+		case 'o':
+			old = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -151,11 +179,19 @@
 	 * we effectively start at the oldest data.
 	 */
 	p = bp + bufpos;
+	if (!old) {
+		lastboot = getlastbootpos(p, buflen);
+		if (lastboot != -1)
+			p = bp + lastboot;
+	}
 	ep = (bufpos == 0 ? bp + buflen : p);
 	newl = skip = 0;
 	do {
 		if (p == bp + buflen)
 			p = bp;
+		/* Skipping boot tag */
+		if (!memcmp(p, BOOT_TAG, strlen(BOOT_TAG)))
+			p += strlen(BOOT_TAG);
 		ch = *p;
 		/* Skip "\n<.*>" syslog sequences. */
 		if (skip) {
@@ -193,6 +229,6 @@
 void
 usage(void)
 {
-	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
+	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system] [-o]\n");
 	exit(1);
 }
--- sbin/dmesg/dmesg.8	Sat Aug 31 21:15:54 2002
+++ sbin/dmesg/dmesg.8	Sat Aug 31 20:57:27 2002
@@ -43,6 +43,7 @@
 .Op Fl a
 .Op Fl M Ar core
 .Op Fl N Ar system
+.Op Fl o
 .Sh DESCRIPTION
 The
 .Nm
@@ -73,6 +74,8 @@
 .It Fl N
 Extract the name list from the specified system instead of the default,
 which is the kernel image the system has booted from.
+.It Fl o
+Display all boot messages, not only the last.
 .El
 .Sh FILES
 .Bl -tag -width ".Pa /var/run/dmesg.boot" -compact
--- sys/sys/msgbuf.h	Sat Aug 31 21:07:05 2002
+++ sys/sys/msgbuf.h	Sat Aug 31 21:08:40 2002
@@ -46,6 +46,10 @@
 	char * 		msg_ptr;		/* pointer to buffer */
 };
 
+#define	BOOT_TAG  "---<<BOOT>>---"	/* used by dmesg util to know where a
+					 * boot message starts.
+					 */
+
 #ifdef _KERNEL
 extern int	msgbuftrigger;
 extern struct	msgbuf *msgbufp;
--- sys/kern/subr_prf.c	Sat Aug 31 21:00:35 2002
+++ sys/kern/subr_prf.c	Sat Aug 31 21:01:01 2002
@@ -818,6 +818,9 @@
 		msgbufp->msg_size = (char *)msgbufp - cp;
 	}
 	msgbufp->msg_ptr = cp;
+	memcpy((void *) (msgbufp->msg_ptr + msgbufp->msg_bufx), BOOT_TAG,
+            strlen(BOOT_TAG));
+	msgbufp->msg_bufx += strlen(BOOT_TAG);
 	if (msgbufmapped && oldp != msgbufp)
 		msgbufcopy(oldp);
 	msgbufmapped = 1;
--- current.patch ends here ---


>Release-Note:
>Audit-Trail:

From: Brooks Davis <brooks@one-eyed-alien.net>
To: =?iso-8859-1?Q?Aur=E9lien_Nephtali?= <aurelien.nephtali@wanadoo.fr>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not old boot messages
Date: Fri, 27 Sep 2002 11:57:28 -0700

 On Fri, Sep 27, 2002 at 08:36:52PM +0200, Aurlien Nephtali wrote:
 > 
 >       Some times it could be usefull to display only the last boot
 >       message (ie: when grep'ing).  The attached patch modifies
 >       sys/kern/subr_prf.c and dmesg.c (and the also the manual).  It
 >       adds a tag (---<<<BOOT>>>---) at the begining of each boot
 >       message (the tag macro is stored in sys/sys/msgbuf.h and could
 >       also be stored in /usr/include/msgbuf.h) dmesg.c is modified
 >       and has a new option: -o, which when it is specified, displays
 >       the full content of the message buffer, otherwise only the last
 >       message is displayed).  This patch has been tested (by me :p)
 >       without any troubles on -CURRENT.
 
 This was my propsed solution to this annoyance so I agree with it. :)  I
 haven't had time to test and fully review this change so I asked
 Aurlien to put it in a PR in hopes that someone else will have to time
 to commit it or something similar.
 
 -- Brooks

From: Ruslan Ermilov <ru@FreeBSD.org>
To: Brooks Davis <brooks@one-eyed-alien.net>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not old boot messages
Date: Mon, 30 Sep 2002 09:53:43 +0300

 On Fri, Sep 27, 2002 at 12:00:20PM -0700, Brooks Davis wrote:
 > The following reply was made to PR bin/43434; it has been noted by GNATS.
 > 
 > From: Brooks Davis <brooks@one-eyed-alien.net>
 > To: =?iso-8859-1?Q?Aur=E9lien_Nephtali?= <aurelien.nephtali@wanadoo.fr>
 > Cc: FreeBSD-gnats-submit@FreeBSD.ORG
 > Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not old boot messages
 > Date: Fri, 27 Sep 2002 11:57:28 -0700
 > 
 >  On Fri, Sep 27, 2002 at 08:36:52PM +0200, Aurlien Nephtali wrote:
 >  > 
 >  >       Some times it could be usefull to display only the last boot
 >  >       message (ie: when grep'ing).  The attached patch modifies
 >  >       sys/kern/subr_prf.c and dmesg.c (and the also the manual).  It
 >  >       adds a tag (---<<<BOOT>>>---) at the begining of each boot
 >  >       message (the tag macro is stored in sys/sys/msgbuf.h and could
 >  >       also be stored in /usr/include/msgbuf.h) dmesg.c is modified
 >  >       and has a new option: -o, which when it is specified, displays
 >  >       the full content of the message buffer, otherwise only the last
 >  >       message is displayed).  This patch has been tested (by me :p)
 >  >       without any troubles on -CURRENT.
 >  
 >  This was my propsed solution to this annoyance so I agree with it. :)  I
 >  haven't had time to test and fully review this change so I asked
 >  Aurlien to put it in a PR in hopes that someone else will have to time
 >  to commit it or something similar.
 >  
 What's wrong with ``dmesg | tail -1''?
 
 
 Cheers,
 -- 
 Ruslan Ermilov		Sysadmin and DBA,
 ru@sunbay.com		Sunbay Software AG,
 ru@FreeBSD.org		FreeBSD committer,
 +380.652.512.251	Simferopol, Ukraine
 
 http://www.FreeBSD.org	The Power To Serve
 http://www.oracle.com	Enabling The Information Age

From: Brooks Davis <brooks@one-eyed-alien.net>
To: Ruslan Ermilov <ru@FreeBSD.org>
Cc: Brooks Davis <brooks@one-eyed-alien.net>,
	bug-followup@FreeBSD.org
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not old boot messages
Date: Mon, 30 Sep 2002 10:04:07 -0700

 On Mon, Sep 30, 2002 at 09:53:43AM +0300, Ruslan Ermilov wrote:
 > On Fri, Sep 27, 2002 at 12:00:20PM -0700, Brooks Davis wrote:
 > >  On Fri, Sep 27, 2002 at 08:36:52PM +0200, AurIlien Nephtali wrote:
 > >  >=20
 > >  >       Some times it could be usefull to display only the last boot
 > >  >       message (ie: when grep'ing).  The attached patch modifies
 > >  >       sys/kern/subr_prf.c and dmesg.c (and the also the manual).  It
 > >  >       adds a tag (---<<<BOOT>>>---) at the begining of each boot
 > >  >       message (the tag macro is stored in sys/sys/msgbuf.h and could
 > >  >       also be stored in /usr/include/msgbuf.h) dmesg.c is modified
 > >  >       and has a new option: -o, which when it is specified, displays
 > >  >       the full content of the message buffer, otherwise only the last
 > >  >       message is displayed).  This patch has been tested (by me :p)
 > >  >       without any troubles on -CURRENT.
 > > =20
 > >  This was my propsed solution to this annoyance so I agree with it. :) =
  I
 > >  haven't had time to test and fully review this change so I asked
 > >  AurIlien to put it in a PR in hopes that someone else will have to time
 > >  to commit it or something similar.
 > > =20
 > What's wrong with ``dmesg | tail -1''?
 
 It does nothing like what this patch does?  That just shows the last
 line of dmesg, this cause dmesg to only show messages from this boot
 unless you specificly ask for saved messages (assuming they exist).
 
 -- Brooks
 
 --=20
 Any statement of the form "X is the one, true Y" is FALSE.
 PGP fingerprint 655D 519C 26A7 82E7 2529  9BF0 5D8E 8BE9 F238 1AD4

From: Ruslan Ermilov <ru@FreeBSD.org>
To: Brooks Davis <brooks@one-eyed-alien.net>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not old boot messages
Date: Tue, 1 Oct 2002 10:36:13 +0300

 --R3G7APHDIzY6R/pk
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Mon, Sep 30, 2002 at 10:04:07AM -0700, Brooks Davis wrote:
 > On Mon, Sep 30, 2002 at 09:53:43AM +0300, Ruslan Ermilov wrote:
 > > On Fri, Sep 27, 2002 at 12:00:20PM -0700, Brooks Davis wrote:
 > > >  On Fri, Sep 27, 2002 at 08:36:52PM +0200, AurIlien Nephtali wrote:
 > > >  >=20
 > > >  >       Some times it could be usefull to display only the last boot
 > > >  >       message (ie: when grep'ing).  The attached patch modifies
 > > >  >       sys/kern/subr_prf.c and dmesg.c (and the also the manual).  =
 It
 > > >  >       adds a tag (---<<<BOOT>>>---) at the begining of each boot
 > > >  >       message (the tag macro is stored in sys/sys/msgbuf.h and cou=
 ld
 > > >  >       also be stored in /usr/include/msgbuf.h) dmesg.c is modified
 > > >  >       and has a new option: -o, which when it is specified, displa=
 ys
 > > >  >       the full content of the message buffer, otherwise only the l=
 ast
 > > >  >       message is displayed).  This patch has been tested (by me :p)
 > > >  >       without any troubles on -CURRENT.
 > > > =20
 > > >  This was my propsed solution to this annoyance so I agree with it. :=
 )  I
 > > >  haven't had time to test and fully review this change so I asked
 > > >  AurIlien to put it in a PR in hopes that someone else will have to t=
 ime
 > > >  to commit it or something similar.
 > > > =20
 > > What's wrong with ``dmesg | tail -1''?
 >=20
 > It does nothing like what this patch does?  That just shows the last
 > line of dmesg, this cause dmesg to only show messages from this boot
 > unless you specificly ask for saved messages (assuming they exist).
 >=20
 Then this would be indeed a useful option.  The "last boot message"
 did not sound to me like the "messages from the last boot".  :-)
 
 
 Cheers,
 --=20
 Ruslan Ermilov		Sysadmin and DBA,
 ru@sunbay.com		Sunbay Software AG,
 ru@FreeBSD.org		FreeBSD committer,
 +380.652.512.251	Simferopol, Ukraine
 
 http://www.FreeBSD.org	The Power To Serve
 http://www.oracle.com	Enabling The Information Age
 
 --R3G7APHDIzY6R/pk
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.0.7 (FreeBSD)
 
 iD8DBQE9mVBtUkv4P6juNwoRArFfAJsEX3Ysp69IM/lKgVvFBnfMbekDtQCfd4j/
 PwMMRNPd+bM9WyPoKgJDxEo=
 =HoMI
 -----END PGP SIGNATURE-----
 
 --R3G7APHDIzY6R/pk--

From: Bruce Evans <bde@zeta.org.au>
To: Brooks Davis <brooks@one-eyed-alien.net>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not
 old boot messages
Date: Wed, 2 Oct 2002 03:10:58 +1000 (EST)

 On Mon, 30 Sep 2002, Brooks Davis wrote:
 >  On Mon, Sep 30, 2002 at 09:53:43AM +0300, Ruslan Ermilov wrote:
 >  > On Fri, Sep 27, 2002 at 12:00:20PM -0700, Brooks Davis wrote:
 >  > >  On Fri, Sep 27, 2002 at 08:36:52PM +0200, AurIlien Nephtali wrote:
 >  > >  >=20
 >  > >  >       Some times it could be usefull to display only the last boot
 >  > >  >       message (ie: when grep'ing).  The attached patch modifies
 >  > >  >       sys/kern/subr_prf.c and dmesg.c (and the also the manual).  It
 >  > >  >       adds a tag (---<<<BOOT>>>---) at the begining of each boot
 >  > >  >       message (the tag macro is stored in sys/sys/msgbuf.h and could
 >  > >  >       also be stored in /usr/include/msgbuf.h) dmesg.c is modified
 >  > >  >       and has a new option: -o, which when it is specified, displays
 >  > >  >       the full content of the message buffer, otherwise only the last
 >  > >  >       message is displayed).  This patch has been tested (by me :p)
 >  > >  >       without any troubles on -CURRENT.
 >  > > =20
 >  > >  This was my propsed solution to this annoyance so I agree with it. :) =
 >   I
 >  > >  haven't had time to test and fully review this change so I asked
 >  > >  AurIlien to put it in a PR in hopes that someone else will have to time
 >  > >  to commit it or something similar.
 >  > > =20
 >  > What's wrong with ``dmesg | tail -1''?
 >
 >  It does nothing like what this patch does?  That just shows the last
 >  line of dmesg, this cause dmesg to only show messages from this boot
 >  unless you specificly ask for saved messages (assuming they exist).
 
 What's wrong with ``grep [-C] <magic> /var/log/messages'' then? :-)
 (except it doesn't handled rotated log files or races with newsyslog ...).
 It handles rotation a bit better than dmesg -- my message buffer gets
 rotated by debugging cruft and doesn't keep boot messages for long.
 Some magic would be required to find the last message without tags.
 Printing only the very last message doesn't seem very useful anyway.
 Lastness depends on an arbitrary descision about where the boot ends.
 
 Bruce
 

From: Bruce Evans <bde@zeta.org.au>
To: Brooks Davis <brooks@one-eyed-alien.net>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: bin/43434: New option to 'dmesg' which allow to display or not
 old boot messages
Date: Wed, 2 Oct 2002 06:26:15 +1000 (EST)

 On Tue, 1 Oct 2002, Brooks Davis wrote:
 
 > On Tue, Oct 01, 2002 at 10:10:06AM -0700, Bruce Evans wrote:
 > >
 > >  What's wrong with ``grep [-C] <magic> /var/log/messages'' then? :-)
 > >  (except it doesn't handled rotated log files or races with newsyslog ...).
 > >
 >
 > 99+% of the time when a user types dmesg what they really want to see is:
 >
 > Copyright (c) 1992-2002 The FreeBSD Project.
 > Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
 >         The Regents of the University of California. All rights
 > reserved.
 > FreeBSD 5.0-CURRENT #2: Thu Sep 26 14:33:02 PDT 2002
 >     root@minya.sea.one-eyed-alien.net:/usr/obj/usr/src/sys/GENERIC
 > Preloaded elf kernel "/boot/kernel/kernel" at 0xc06ca000.
 > Preloaded elf module "/boot/kernel/snd_maestro3.ko" at 0xc06ca0a8.
 > ...
 >
 > For the _current_ boot.  They couldn't care less about previous boots
 > unless they are looking for a message to diagnose a crash.  Currently if
 > you don't have extra debugging stuff on and you don't generate a lot of
 > errors, you typicaly find boot messages repeated several times, especialy
 > in -stable.  Thus you have to carefully scroll though the whole list to
 > find the current boot.  This change makes dmesg do what the user wants,
 > but allows access to the previous messages in the unlikely event they
 > are needed.
 
 I see.  The description in the PR is confusing -- it says that the last
 boot message is wanted, but actually the messages from the current boot
 are wanted.
 
 I think grep or an editor search is enough to find the start of the
 buffer.  This can be automated, preferably not by hacking on dmesg,
 but I wouldn't mind adding a tag that is easy to search for and likely
 to be unique for each boot.
 
 The implementation has a buffer overrun in subr_prf.c (when msg_bufx is
 near the end of the buffer at init time) and some style bugs.  Any tag
 should be written using an ordinary printf.
 
 Bruce
 
>Unformatted:
