$NetBSD: patch-al,v 1.4 2004/03/19 22:55:30 kristerw Exp $

--- sysdeps/freebsd/prockernel.c.orig	2003-10-19 18:55:32.000000000 +0200
+++ sysdeps/freebsd/prockernel.c	2004-03-19 23:39:38.000000000 +0100
@@ -34,12 +34,9 @@
 #if (!defined __OpenBSD__) && (!defined __bsdi__)
 #include <sys/user.h>
 #endif
-#if !defined(__bsdi__) && !(defined(__FreeBSD__) && defined(__alpha__))
+#if !defined(__bsdi__) && !(defined(__FreeBSD__) && defined(__alpha__)) && !defined(__NetBSD__)
 #include <machine/pcb.h>
 #endif
-#if defined(__FreeBSD__) && !defined(__alpha__)
-#include <machine/tss.h>
-#endif
 
 #include <unistd.h>
 #include <fcntl.h>
@@ -48,6 +45,23 @@
 #include <osreldate.h>
 #endif
 
+#ifdef __NetBSD__
+#include <machine/vmparam.h>
+#include <machine/pmap.h>
+#ifdef __arm32__
+#define	KERNBASE	KERNEL_BASE
+#endif
+#endif
+
+#ifdef __NetBSD__
+#include <machine/vmparam.h>
+#include <machine/pmap.h>
+#ifdef __arm32__
+#define	KERNBASE	KERNEL_BASE
+#endif
+#endif
+
+
 static const unsigned long _glibtop_sysdeps_proc_kernel_pstats =
 (1L << GLIBTOP_PROC_KERNEL_MIN_FLT) +
 (1L << GLIBTOP_PROC_KERNEL_MAJ_FLT) +
@@ -78,9 +92,11 @@
 			   pid_t pid)
 {
 	struct kinfo_proc *pinfo;
+#ifndef __FreeBSD__
 	struct user *u_addr = (struct user *)USRSTACK;
 	struct pstats pstats;
 	struct pcb pcb;
+#endif
 	int count;
 
 	char filename [BUFSIZ];
@@ -101,17 +117,36 @@
 	if ((pinfo == NULL) || (count != 1))
 		glibtop_error_io_r (server, "kvm_getprocs (%d)", pid);
 
-	buf->nwchan = (unsigned long) pinfo [0].kp_proc.p_wchan &~ KERNBASE;
+#if defined(__FreeBSD__) && (__FreeBSD_version >= 500013)
+
+#define	XXX_P_WCHAN	ki_wchan
+#define	XXX_P_WMESG	ki_wmesg
+#define	XXX_E_WMESG	ki_wmesg
+
+#else
+
+#define	XXX_P_WCHAN	kp_proc.p_wchan
+#define	XXX_P_WMESG	kp_proc.p_wmesg
+#define	XXX_E_WMESG	kp_eproc.e_wmesg
+
+#endif
+
+#if !defined(__NetBSD__) || !defined(SACTIVE)
+	buf->nwchan = (unsigned long) pinfo [0].XXX_P_WCHAN &~ KERNBASE;
+
 	buf->flags |= (1L << GLIBTOP_PROC_KERNEL_NWCHAN);
 
-	if (pinfo [0].kp_proc.p_wchan && pinfo [0].kp_proc.p_wmesg) {
-		strncpy (buf->wchan, pinfo [0].kp_eproc.e_wmesg,
+	if (pinfo [0].XXX_P_WCHAN && pinfo [0].XXX_P_WMESG) {
+		strncpy (buf->wchan, pinfo [0].XXX_E_WMESG,
 			 sizeof (buf->wchan) - 1);
 		buf->wchan [sizeof (buf->wchan) - 1] = 0;
 		buf->flags |= (1L << GLIBTOP_PROC_KERNEL_WCHAN);
 	} else {
 		buf->wchan [0] = 0;
 	}
+#endif
+
+#ifndef __FreeBSD__
 
 	/* Taken from `saveuser ()' in `/usr/src/bin/ps/ps.c'. */
 
@@ -129,8 +164,17 @@
 
 	glibtop_suid_enter (server);
 
+#if !defined(__NetBSD__) || !defined(SACTIVE)
+#ifdef __NetBSD__
+	/* On NetBSD, there is no kvm_uread(), and kvm_read() always reads
+	 * from kernel memory.  */
+
+	if (kvm_read (server->machine.kd,
+#else
+
 	if ((pinfo [0].kp_proc.p_flag & P_INMEM) &&
 	    kvm_uread (server->machine.kd, &(pinfo [0]).kp_proc,
+#endif
 		       (unsigned long) &u_addr->u_stats,
 		       (char *) &pstats, sizeof (pstats)) == sizeof (pstats))
 		{
@@ -149,8 +193,12 @@
 			buf->flags |= _glibtop_sysdeps_proc_kernel_pstats;
 		}
 
+#ifdef __NetBSD__
+	if (kvm_read (server->machine.kd,
+#else
 	if ((pinfo [0].kp_proc.p_flag & P_INMEM) &&
 	    kvm_uread (server->machine.kd, &(pinfo [0]).kp_proc,
+#endif
 		       (unsigned long) &u_addr->u_pcb,
 		       (char *) &pcb, sizeof (pcb)) == sizeof (pcb))
 		{
@@ -167,18 +215,55 @@
 			/*xxx FreeBSD/Alpha? */
 #endif
 #else
+#ifdef __i386__
 			buf->kstk_esp = (u_int64_t) pcb.pcb_tss.tss_esp0;
 #ifdef __bsdi__
 			buf->kstk_eip = (u_int64_t) pcb.pcb_tss.tss_eip;
 #else
 			buf->kstk_eip = (u_int64_t) pcb.pcb_tss.__tss_eip;
 #endif
-
+#else
+#if defined(__NetBSD__)
+#if defined(__m68k__)
+			buf->kstk_esp = (u_int64_t) pcb.pcb_usp;
+			buf->kstk_eip = (u_int64_t) 0;
+#elif defined(__x86_64__)
+			buf->kstk_esp = (u_int64_t) pcb.pcb_usersp;
+			buf->kstk_eip = (u_int64_t) 0;
+#elif (defined(__arm32__) || defined(__powerpc__))
+			buf->kstk_esp = (u_int64_t) pcb.pcb_sp;
+			buf->kstk_eip = (u_int64_t) 0;
+#elif defined(__mipsel__)
+			buf->kstk_esp = (u_int64_t) pcb.pcb_context[8];
+			buf->kstk_eip = (u_int64_t) 0;
+#elif defined(__sparc__)
+			buf->kstk_esp = (u_int64_t) pcb.pcb_sp;
+			buf->kstk_eip = (u_int64_t) pcb.pcb_pc;
+#elif defined(__alpha__)
+			buf->kstk_esp = (u_int64_t) pcb.pcb_context[9];
+			buf->kstk_eip = (u_int64_t) pcb.pcb_context[8];
+#else
+			/* provide some defaults for other platforms */
+			buf->kstk_esp = (u_int64_t) 0;
+			buf->kstk_eip = (u_int64_t) 0;
+#endif /* ${MACHINE_ARCH} */
+#endif /* __NetBSD__ */
 			buf->flags |= _glibtop_sysdeps_proc_kernel_pcb;
 #endif
+#endif
 		}
-
+#endif
 	/* Taken from `wchan ()' in `/usr/src/bin/ps/print.c'. */
 
 	glibtop_suid_leave (server);
+
+#else	
+	/* XXX: the code here was, quite frankly, junk, and almost
+	 * certainly wrong - remove it all, leave these fields
+	 * unpopulated, and give up until such time as the right
+	 * code is produced for both FreeBSD 4.x and 5.x
+	 */
+	return;
+#endif /* __FreeBSD__ */
+
 }
