From nobody@FreeBSD.org  Mon May  7 06:31:56 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 4056C106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  7 May 2012 06:31:56 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 1259E8FC08
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  7 May 2012 06:31:56 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q476VtPx065753
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 7 May 2012 06:31:55 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id q476VtWV065752;
	Mon, 7 May 2012 06:31:55 GMT
	(envelope-from nobody)
Message-Id: <201205070631.q476VtWV065752@red.freebsd.org>
Date: Mon, 7 May 2012 06:31:55 GMT
From: David Marker <dmarker@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: libkvm doesn't initialize vnet
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         167671
>Category:       kern
>Synopsis:       [libkvm] [patch] libkvm doesn't initialize vnet
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 07 06:40:08 UTC 2012
>Closed-Date:    Tue Sep 11 06:00:27 UTC 2012
>Last-Modified:  Tue Sep 11 06:10:02 UTC 2012
>Originator:     David Marker
>Release:        9.0-stable
>Organization:
>Environment:
FreeBSD familiar 9.0-STABLE FreeBSD 9.0-STABLE #0 r235098: Sun May  6 19:31:38 MDT 2012     root@familiar:/usr/obj/usr/src/sys/FAMILIAR  amd64
>Description:
Adding "options VIMAGE" to kernel configuration causes symbol lookups to fail in libkvm.

For example `netstat -rn` will complain "netstat: no namelist"
>How-To-Repeat:
Add "options VIMAGE" to your kernel config, rebuild and install. Now `netstat -rn` will complain.
>Fix:
/usr/src/lib/libkvm/kvm_vnet.c is looking for "dumptid" (among other symbols) when calling _kvm_nlist() in _kvm_vnet_selectpid(). The other symbols are there, but "dumptid" is not and it causes _kvm_nlist() to return an error.

Further down the code, comments suggest the "dumptid" symbol is only supposed to be there if libkvm is working on a crashdump.

Removing "dumptid" from the nlist fixes the issue and `netstat -rn` works as normal again.

I'm attaching a patch that works, but I confess I do not understand how "dumptid" is supposed to work so it would be best to consider it just a starting point.

Patch attached with submission follows:

--- kvm_vnet.c	2012-05-06 23:27:25.701672209 -0600
+++ kvm_vnet.c	2012-05-06 23:49:27.186721426 -0600
@@ -75,12 +75,16 @@
 		{ .n_name = "vnet_head" },
 #define	NLIST_ALLPROC		3
 		{ .n_name = "allproc" },
-#define	NLIST_DUMPTID		4
-		{ .n_name = "dumptid" },
-#define	NLIST_PROC0		5
+#define NLIST_PROC0		4
 		{ .n_name = "proc0" },
 		{ .n_name = NULL },
 	};
+	struct nlist dnl[] = {
+#define	NLIST_DUMPTID		5
+		{ .n_name = "dumptid" },
+		{ .n_name = NULL },
+	};
+
 	uintptr_t procp, credp;
 #define	VMCORE_VNET_OF_PROC0
 #ifndef VMCORE_VNET_OF_PROC0
@@ -108,12 +112,15 @@
 		return (-1);
 	}
 
+	/* dumptid may not be there */
+	(void) _kvm_nlist(kd, dnl, 0);
+
 	/*
 	 * Auto-detect if this is a crashdump by reading dumptid.
 	 */
 	dumptid = 0;
-	if (nl[NLIST_DUMPTID].n_value) {
-		if (kvm_read(kd, nl[NLIST_DUMPTID].n_value, &dumptid,
+	if (dnl[NLIST_DUMPTID].n_value) {
+		if (kvm_read(kd, dnl[NLIST_DUMPTID].n_value, &dumptid,
 		    sizeof(dumptid)) != sizeof(dumptid)) {
 			_kvm_err(kd, kd->program, "%s: dumptid", __func__);
 			return (-1);


>Release-Note:
>Audit-Trail:

From: David Marker <dmarker@gmail.com>
To: bug-followup@FreeBSD.org, dmarker@gmail.com
Cc:  
Subject: Re: kern/167671: [libkvm] [patch] libkvm doesn&#39;t initialize
 vnet
Date: Tue, 4 Sep 2012 08:36:34 -0600

 After further investigation, the problem is that clang(1) optimized the symbol dumptid out of kern_shutdown.c. 
 To reproduce you need "options VIMAGE" and you need to be building with clang(1) not gcc(1).
 
 I can verify this with a simple program to look for the "dumptid" symbol in the same way _kvm_nlist() does:
 
 --------------------------------------------------------------------------------
 #include <errno.h>
 #include <stdio.h>
 #include <sys/param.h>
 #include <sys/linker.h>
 int
 main()
 {
 int rc;
 char *symname = "dumptid";
 struct kld_sym_lookup lookup = {
 .version = sizeof(struct kld_sym_lookup),
 .symname = symname,
 .symvalue = 0,
 .symsize = 0
 };
 if (0 == (rc = kldsym(0, KLDSYM_LOOKUP, &lookup))) {
 (void) fprintf(stdout, "kldsym(...) -> %d\n", rc);
 (void) fprintf(stdout, "found symbol \"%s\"\n", symname);
 } else {
 (void) fprintf(stdout, "kldsym(...) -> %d, errno = %d\n", rc, errno);
 perror(NULL);
 }
 return (0);
 }
 
 --------------------------------------------------------------------------------
 
 The symbol is missing. So the fix is much simpler, make dumptid volatile so clang(1) doesn't feel free to optimize it away.
 After making dumptid volatile the test program above finds the symbol and the original patch isn't needed.
 
 
 $ svn diff
 Index: sys/kern/kern_shutdown.c
 ===================================================================
 --- sys/kern/kern_shutdown.c (revision 240074)
 +++ sys/kern/kern_shutdown.c (working copy)
 @@ -148,7 +148,7 @@
 
 /* Context information for dump-debuggers. */
 static struct pcb dumppcb; /* Registers. */
 -static lwpid_t dumptid; /* Thread ID. */
 +static volatile lwpid_t dumptid; /* Thread ID. */
 
 static void poweroff_wait(void *, int);
 static void shutdown_halt(void *junk, int howto);
 
 
 
 
 

From: Andriy Gapon <avg@FreeBSD.org>
To: bug-followup@FreeBSD.org, dmarker@gmail.com
Cc:  
Subject: Re: kern/167671: [libkvm] [patch] libkvm doesn&#39;t initialize vnet
Date: Fri, 07 Sep 2012 12:36:57 +0300

 Thank you for the great investigation!
 Could you please try simply dropping static instead of adding volatile?
 
 -- 
 Andriy Gapon

From: David Marker <dmarker@gmail.com>
To: bug-followup@FreeBSD.org, dmarker@gmail.com
Cc:  
Subject: Re: kern/167671: [libkvm] [patch] libkvm doesn&#39;t initialize
 vnet
Date: Fri, 7 Sep 2012 15:49:30 -0600

 I can confirm that Andriy Gapon's suggestion to just removing static also fixes kernels built with `options VIMAGE` and clang(1).
 
 -Dave  
 
 
 root@familiar:/usr/src # svn diff sys/kern/kern_shutdown.c
 Index: sys/kern/kern_shutdown.c
 ===================================================================
 --- sys/kern/kern_shutdown.c (revision 240204)
 +++ sys/kern/kern_shutdown.c (working copy)
 @@ -148,7 +148,7 @@
 
 /* Context information for dump-debuggers. */
 static struct pcb dumppcb; /* Registers. */
 -static lwpid_t dumptid; /* Thread ID. */
 +lwpid_t dumptid; /* Thread ID. */
 
 static void poweroff_wait(void *, int);
 static void shutdown_halt(void *junk, int howto);
 
 
 
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/167671: commit references a PR
Date: Tue, 11 Sep 2012 05:58:43 +0000 (UTC)

 Author: avg
 Date: Tue Sep 11 05:58:32 2012
 New Revision: 240337
 URL: http://svn.freebsd.org/changeset/base/240337
 
 Log:
   MFC r235777: Make dumptid non-static. It is used by libkvm.
   
   PR:		kern/167671
   MFC slacker:	harti
 
 Modified:
   stable/9/sys/kern/kern_shutdown.c
 Directory Properties:
   stable/9/sys/   (props changed)
 
 Modified: stable/9/sys/kern/kern_shutdown.c
 ==============================================================================
 --- stable/9/sys/kern/kern_shutdown.c	Tue Sep 11 05:04:59 2012	(r240336)
 +++ stable/9/sys/kern/kern_shutdown.c	Tue Sep 11 05:58:32 2012	(r240337)
 @@ -148,7 +148,7 @@ static struct dumperinfo dumper;	/* our 
  
  /* Context information for dump-debuggers. */
  static struct pcb dumppcb;		/* Registers. */
 -static lwpid_t dumptid;			/* Thread ID. */
 +lwpid_t dumptid;			/* Thread ID. */
  
  static void poweroff_wait(void *, int);
  static void shutdown_halt(void *junk, int howto);
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: avg 
State-Changed-When: Tue Sep 11 06:00:23 UTC 2012 
State-Changed-Why:  
fixed in all supported branches 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/167671: commit references a PR
Date: Tue, 11 Sep 2012 05:59:32 +0000 (UTC)

 Author: avg
 Date: Tue Sep 11 05:59:19 2012
 New Revision: 240338
 URL: http://svn.freebsd.org/changeset/base/240338
 
 Log:
   MFC r235777: Make dumptid non-static. It is used by libkvm.
   
   PR:		kern/167671
   MFC slacker:	harti
 
 Modified:
   stable/8/sys/kern/kern_shutdown.c
 Directory Properties:
   stable/8/sys/   (props changed)
   stable/8/sys/kern/   (props changed)
 
 Modified: stable/8/sys/kern/kern_shutdown.c
 ==============================================================================
 --- stable/8/sys/kern/kern_shutdown.c	Tue Sep 11 05:58:32 2012	(r240337)
 +++ stable/8/sys/kern/kern_shutdown.c	Tue Sep 11 05:59:19 2012	(r240338)
 @@ -146,7 +146,7 @@ static struct dumperinfo dumper;	/* our 
  
  /* Context information for dump-debuggers. */
  static struct pcb dumppcb;		/* Registers. */
 -static lwpid_t dumptid;			/* Thread ID. */
 +lwpid_t dumptid;			/* Thread ID. */
  
  static void boot(int) __dead2;
  static void poweroff_wait(void *, int);
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
