From nobody@FreeBSD.ORG  Wed Aug 23 12:41:19 2000
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id 0F9CD37B43E; Wed, 23 Aug 2000 12:41:19 -0700 (PDT)
Message-Id: <20000823194119.0F9CD37B43E@hub.freebsd.org>
Date: Wed, 23 Aug 2000 12:41:19 -0700 (PDT)
From: imp@village.org
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@FreeBSD.org
Subject: netstat -m doesn't use -N or -M arguments, nor warn about it
X-Send-Pr-Version: www-1.0

>Number:         20808
>Category:       misc
>Synopsis:       netstat -m doesn't use -N or -M arguments, nor warn about it
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    ru
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 23 12:50:01 PDT 2000
>Closed-Date:    Thu Jun 14 08:39:54 PDT 2001
>Last-Modified:  Thu Jun 14 08:48:27 PDT 2001
>Originator:     Warner Losh
>Release:        FreeBSD -current
>Organization:
FreeBSD
>Environment:
>Description:
The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
allow for specification of a core and kernel file to use rather than the 
current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
doesn't fallback to getting the information from core file.  Nor
does it warn about it :-(
>How-To-Repeat:
Run netstat -m -N xxxx -M yyyy on a core file.  Run it many times.
Watch the numbers change.
>Fix:
Sadly, all I have is the following:, which I'll commit, but it doesn't
fix the actual bug.  Note, I did a bad thing and cut/paste tabs
so they are likely gone.  But this is for reference only.

Index: main.c
===================================================================
RCS file: /base/FreeBSD-CVS/src/usr.bin/netstat/main.c,v
retrieving revision 1.23.2.5
diff -u -r1.23.2.5 main.c
--- main.c      2000/03/20 18:34:54     1.23.2.5
+++ main.c      2000/08/23 18:58:36
@@ -346,6 +346,8 @@
                setgid(getgid());
 
        if (mflag) {
+               if (nlistf != NULL || memf != NULL)
+                       err("Cannot obtain mbuf stats from a core file");
                mbpr();
                exit(0);
        }


>Release-Note:
>Audit-Trail:

From: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
To: imp@village.org
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it
Date: Wed, 23 Aug 2000 16:11:48 -0400 (EDT)

 <<On Wed, 23 Aug 2000 12:41:19 -0700 (PDT), imp@village.org said:
 
 
 > The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
 > allow for specification of a core and kernel file to use rather than the 
 > current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
 > doesn't fallback to getting the information from core file.
 
 This is intentional.  Perhaps the documentation should reflect that
 `-N' and `-M' are deprecated.
 
 -GAWollman
 
 --
 Garrett A. Wollman   | O Siem / We are all family / O Siem / We're all the same
 wollman@lcs.mit.edu  | O Siem / The fires of freedom 
 Opinions not those of| Dance in the burning flame
 MIT, LCS, CRS, or NSA|                     - Susan Aglukark and Chad Irschick
 

From: Warner Losh <imp@village.org>
To: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it 
Date: Wed, 23 Aug 2000 14:41:52 -0600

 In message <200008232011.QAA20610@khavrinen.lcs.mit.edu> Garrett Wollman writes:
 : <<On Wed, 23 Aug 2000 12:41:19 -0700 (PDT), imp@village.org said:
 : 
 : 
 : > The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
 : > allow for specification of a core and kernel file to use rather than the 
 : > current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
 : > doesn't fallback to getting the information from core file.
 : 
 : This is intentional.  Perhaps the documentation should reflect that
 : `-N' and `-M' are deprecated.
 
 How do I get the mbuf stats from a core dump then?
 
 Warner
 

From: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
To: Warner Losh <imp@village.org>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it 
Date: Wed, 23 Aug 2000 17:15:43 -0400 (EDT)

 <<On Wed, 23 Aug 2000 14:41:52 -0600, Warner Losh <imp@village.org> said:
 
 > How do I get the mbuf stats from a core dump then?
 
 (gdb) p mbstat
 
 -GAWollman
 
 

From: Warner Losh <imp@village.org>
To: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it 
Date: Wed, 23 Aug 2000 15:48:46 -0600

 In message <200008232115.RAA21032@khavrinen.lcs.mit.edu> Garrett
 Wollman writes:
 : <<On Wed, 23 Aug 2000 14:41:52 -0600, Warner Losh <imp@village.org> said:
 : 
 : > How do I get the mbuf stats from a core dump then?
 : 
 : (gdb) p mbstat
 
 gdb -k kernel.1 vmcore.1
 (kgdb) p mbstat
 $1 = 6368
 (kgdb)
 
 Hmmm, that seems a little thin to me.  Do I need symbols or some kind
 of cast?
 
 Warner
 

From: Bruce Evans <bde@zeta.org.au>
To: imp@village.org
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn
 about it
Date: Thu, 24 Aug 2000 16:57:41 +1000 (EST)

 On Wed, 23 Aug 2000 imp@village.org wrote:
 
 > >Description:
 > The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
 > allow for specification of a core and kernel file to use rather than the 
 > current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
 > doesn't fallback to getting the information from core file.  Nor
 > does it warn about it :-(
 
 Support for "-M core -N system" is mostly dysfunctional.  Support for
 dead kernels was mostly in libkvm and was lost when we downgraded to
 using only sysctl to read kernel info.  Support for netstat's -m on dead
 kernels was lost in the corresponding downgrade in netstat.
 
 Bruce
 
 

From: Warner Losh <imp@village.org>
To: Bruce Evans <bde@zeta.org.au>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it 
Date: Thu, 24 Aug 2000 00:59:21 -0600

 In message <Pine.BSF.4.21.0008241647140.2882-100000@besplex.bde.org> Bruce Evans writes:
 : On Wed, 23 Aug 2000 imp@village.org wrote:
 : 
 : > >Description:
 : > The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
 : > allow for specification of a core and kernel file to use rather than the 
 : > current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
 : > doesn't fallback to getting the information from core file.  Nor
 : > does it warn about it :-(
 : 
 : Support for "-M core -N system" is mostly dysfunctional.  Support for
 : dead kernels was mostly in libkvm and was lost when we downgraded to
 : using only sysctl to read kernel info.  Support for netstat's -m on dead
 : kernels was lost in the corresponding downgrade in netstat.
 
 So is it OK to commit the "Hey stupid don't do that" fix?
 
 Warner
 
Responsible-Changed-From-To: freebsd-bugs->ps 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Thu Aug 24 01:50:03 PDT 2000 
Responsible-Changed-Why:  
Paul's tackling this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20808 

From: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
To: Warner Losh <imp@village.org>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it 
Date: Thu, 24 Aug 2000 11:12:50 -0400 (EDT)

 <<On Wed, 23 Aug 2000 15:48:46 -0600, Warner Losh <imp@village.org> said:
 
 > Hmmm, that seems a little thin to me.  Do I need symbols or some kind
 > of cast?
 
 If you have debugging symbols, it should print out the whole
 structure.
 
 -GAWollman
 
 --
 Garrett A. Wollman   | O Siem / We are all family / O Siem / We're all the same
 wollman@lcs.mit.edu  | O Siem / The fires of freedom 
 Opinions not those of| Dance in the burning flame
 MIT, LCS, CRS, or NSA|                     - Susan Aglukark and Chad Irschick
 

From: Ruslan Ermilov <ru@FreeBSD.org>
To: Bruce Evans <bde@FreeBSD.org>, Warner Losh <imp@FreeBSD.org>,
	Garrett Wollman <wollman@FreeBSD.org>
Cc: bug-followup@FreeBSD.org
Subject: Re: misc/20808: netstat -m doesn't use -N or -M arguments, nor warn about it
Date: Tue, 12 Jun 2001 18:38:13 +0300

 --azLHFNyN32YCQGCU
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 On Thu, Aug 24, 2000 at 04:57:41PM +1000, Bruce Evans wrote:
 >  On Wed, 23 Aug 2000 imp@village.org wrote:
 >  
 >  > >Description:
 >  > The netstat -m option reports the number of mbuf in use.  Netstat -N and -M
 >  > allow for specification of a core and kernel file to use rather than the 
 >  > current running system.  The mbuf stat code in mbuf.h (specifically mbpr)
 >  > doesn't fallback to getting the information from core file.  Nor
 >  > does it warn about it :-(
 >  
 >  Support for "-M core -N system" is mostly dysfunctional.  Support for
 >  dead kernels was mostly in libkvm and was lost when we downgraded to
 >  using only sysctl to read kernel info.  Support for netstat's -m on dead
 >  kernels was lost in the corresponding downgrade in netstat.
 >  
 Here's the patch against RELENG_4 sources that (sorta) restores the -N -M
 support for -m.  Please review.
 
 
 Cheers,
 -- 
 Ruslan Ermilov		Oracle Developer/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
 
 --azLHFNyN32YCQGCU
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename=p
 
 Index: main.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/netstat/main.c,v
 retrieving revision 1.34.2.4
 diff -u -p -r1.34.2.4 main.c
 --- main.c	2001/03/22 13:48:42	1.34.2.4
 +++ main.c	2001/06/12 15:28:42
 @@ -145,6 +145,14 @@ static struct nlist nl[] = {
  	{ "_mif6table" },
  #define N_PFKEYSTAT	37
  	{ "_pfkeystat" },
 +#define N_MBSTAT	38
 +	{ "_mbstat" },
 +#define N_MBTYPES	39
 +	{ "_mbtypes" },
 +#define N_NMBCLUSTERS	40
 +	{ "_nmbclusters" },
 +#define N_NMBUFS	41
 +	{ "_nmbufs" },
  	{ "" },
  };
  
 @@ -465,7 +473,12 @@ main(argc, argv)
  		setgid(getgid());
  
  	if (mflag) {
 -		mbpr();
 +		if (memf != NULL) {
 +			kread(0, 0, 0);
 +			mbpr(nl[N_MBSTAT].n_value, nl[N_MBTYPES].n_value,
 +			    nl[N_NMBCLUSTERS].n_value, nl[N_NMBUFS].n_value);
 +		} else
 +			mbpr(0, 0, 0, 0);
  		exit(0);
  	}
  	if (pflag) {
 Index: mbuf.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/netstat/mbuf.c,v
 retrieving revision 1.17.2.2
 diff -u -p -r1.17.2.2 mbuf.c
 --- mbuf.c	2000/10/24 22:53:26	1.17.2.2
 +++ mbuf.c	2001/06/12 15:28:42
 @@ -53,8 +53,6 @@ static const char rcsid[] =
  #define	YES	1
  typedef int bool;
  
 -struct	mbstat mbstat;
 -
  static struct mbtypenames {
  	int	mt_type;
  	char	*mt_name;
 @@ -96,10 +94,12 @@ static struct mbtypenames {
   * Print mbuf statistics.
   */
  void
 -mbpr()
 +mbpr(mbaddr, mbtaddr, nmbcaddr, nmbufaddr)
 +	u_long mbaddr, mbtaddr, nmbcaddr, nmbufaddr;
  {
  	u_long totmem, totpossible, totmbufs;
  	register int i;
 +	struct mbstat mbstat;
  	struct mbtypenames *mp;
  	int name[3], nmbclusters, nmbufs, nmbtypes;
  	size_t nmbclen, nmbuflen, mbstatlen, mbtypeslen;
 @@ -108,16 +108,12 @@ mbpr()
  
  	mbtypes = NULL;
  	seen = NULL;
 -
 -	name[0] = CTL_KERN;
 -	name[1] = KERN_IPC;
 -	name[2] = KIPC_MBSTAT;
 -	mbstatlen = sizeof mbstat;
 -	if (sysctl(name, 3, &mbstat, &mbstatlen, 0, 0) < 0) {
 -		warn("sysctl: retrieving mbstat");
 -		goto err;
 -	}
  
 +	/*
 +	 * XXX
 +	 * We can't kread() mbtypeslen from a core image so we'll
 +	 * bogusly assume it's the same as in the running kernel.
 +	 */
  	if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) {
  		warn("sysctl: retrieving mbtypes length");
  		goto err;
 @@ -126,29 +122,50 @@ mbpr()
  		warn("malloc: %lu bytes for mbtypes", (u_long)mbtypeslen);
  		goto err;
  	}
 -	if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL,
 -	    0) < 0) {
 -		warn("sysctl: retrieving mbtypes");
 -		goto err;
 -	}
  
  	nmbtypes = mbtypeslen / sizeof(*mbtypes);
  	if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) {
  		warn("calloc");
  		goto err;
  	}
 -		
 -	name[2] = KIPC_NMBCLUSTERS;
 -	nmbclen = sizeof(int);
 -	if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) {
 -		warn("sysctl: retrieving nmbclusters");
 -		goto err;
 -	}
  
 -	nmbuflen = sizeof(int);
 -	if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) {
 -		warn("sysctl: retrieving nmbufs");
 -		goto err;
 +	if (mbaddr) {
 +		if (kread(mbaddr, (char *)&mbstat, sizeof mbstat))
 +			goto err;
 +		if (kread(mbtaddr, (char *)mbtypes, mbtypeslen))
 +			goto err;
 +		if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int)))
 +			goto err;
 +		if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int)))
 +			goto err;
 +	} else {
 +		name[0] = CTL_KERN;
 +		name[1] = KERN_IPC;
 +		name[2] = KIPC_MBSTAT;
 +		mbstatlen = sizeof mbstat;
 +		if (sysctl(name, 3, &mbstat, &mbstatlen, 0, 0) < 0) {
 +			warn("sysctl: retrieving mbstat");
 +			goto err;
 +		}
 +
 +		if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL,
 +		    0) < 0) {
 +			warn("sysctl: retrieving mbtypes");
 +			goto err;
 +		}
 +		
 +		name[2] = KIPC_NMBCLUSTERS;
 +		nmbclen = sizeof(int);
 +		if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) {
 +			warn("sysctl: retrieving nmbclusters");
 +			goto err;
 +		}
 +
 +		nmbuflen = sizeof(int);
 +		if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) {
 +			warn("sysctl: retrieving nmbufs");
 +			goto err;
 +		}
  	}
  
  #undef MSIZE
 Index: netstat.h
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/netstat/netstat.h,v
 retrieving revision 1.16.2.2
 diff -u -p -r1.16.2.2 netstat.h
 --- netstat.h	2001/03/22 13:48:44	1.16.2.2
 +++ netstat.h	2001/06/12 15:28:42
 @@ -94,7 +94,7 @@ void	pfkey_stats __P((u_long, char *));
  
  void	bdg_stats __P((u_long, char *));
  
 -void	mbpr __P((void));
 +void	mbpr __P((u_long, u_long, u_long, u_long));
  
  void	hostpr __P((u_long, u_long));
  void	impstats __P((u_long, u_long));
 
 --azLHFNyN32YCQGCU--
State-Changed-From-To: open->closed 
State-Changed-By: ru 
State-Changed-When: Thu Jun 14 08:39:54 PDT 2001 
State-Changed-Why:  
I have restored the -N -M support for -m. 


Responsible-Changed-From-To: ps->ru 
Responsible-Changed-By: ru 
Responsible-Changed-When: Thu Jun 14 08:39:54 PDT 2001 
Responsible-Changed-Why:  
Increment counter. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=20808 
>Unformatted:
