/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: dinfo.c 1.27 90/03/26 $"

/*
 * dinfo.c
 *	Procedures for various debug info purposes.
 */

/* $Log:	dinfo.c,v $
 */

#ifdef	DEBUG

#include "sys/types.h"
#include "sys/devsw.h"
#include "sys/signal.h"
#include "sys/param.h"
#include "sys/debug.h"
#include "sys/mutex.h"
#include "sys/systm.h"
#include "sys/user.h"
#include "sys/map.h"
#include "sys/vm.h"
#include "sys/timer.h"
#include "sys/proc.h"
#include "sys/buf.h"
#include "sys/conf.h"
#include "sys/vnode.h"
#include "sys/ufsinode.h"
#include "sys/snode.h"
#include "sys/file.h"
#include "sys/engine.h"
#include "sys/slic.h"
#include "sys/slicreg.h"
#include "sys/var.h"

#include "sys/pte.h"
#include "sys/plocal.h"
#include "sys/mmu.h"
#include "sys/gate.h"
#include "sys/intctl.h"
#include "sys/psl.h"
#include "config.h"

struct	proc	*D_proc;		/* selected proc[] entry */

/*
 * procdump()
 *	Dump some useful stuff on a process.
 */

procdump(p)
	register struct proc *p;
{
	register spl_t	s;
#define	PF(sep,f)	printf("%c f=%x", sep, p->f)

	printf("<%d>:", p-proc);
	PF('\t',p_stat);
	PF(',',p_flag);
	PF(',',p_ul2pt);
	s = p_lock(&p->p_state, SPLHI);
	if (p->p_flag & SLOAD) {
		printf(", rshand=%x, rscurr=%x", p->p_rshand, p->p_rscurr);
	}
	v_lock(&p->p_state, s);
	printf("\n");

	PF('\t',p_pid);
	PF(',',p_ppid);
	PF(',',p_dsize);
	PF(',',p_ssize);
	PF(',',p_rssize);
	printf("\n");

	PF('\t',p_cursig);
	PF(',',p_sig);
	PF(',',p_sigmask);
	PF(',',p_sigignore);
	PF(',',p_sigcatch);
	printf("\n");
}

/*
 * debugit()
 *	Called with argument character from input junk.
 */

#define	BUMPPARAM(var,min,max,incr) \
		{ extern int var; \
		  if ((var += (incr)) > (max)) var = min; \
		  printf("var=%d\n", var); }

#define	BUMPDEBUG(var,nstate)	BUMPPARAM(var,0,nstate-1,1)

#define	DUMPSTAT(var)	{ extern int var; printf("var=%d\n", var); }

static	char	*help[] = {
	"a:	toggle abtdebug",
#ifdef NETWORK
	"A:	toggle arp debug",
#endif /* NETWORK */
	"B:	dump buffer stats",
#ifdef SM_CHARACTER
	"C:	toggle smdebug",
#endif
	"d:	toggle specfs debug",
	"D:	toggle getdents debug",
#ifdef NETWORK
	"e:	toggle ether input debug",
	"E:	toggle ether output debug",
#endif /* NETWORK */
	"f:	print freemem",
	"F:	memory, uptmap stats",
#ifdef	GATESTAT
	"g,G:	gate usage statistics",
#endif	/* GATESTAT  */
#ifdef	PERFSTAT
	"i:	inode, file-table, c-list usage stats",
#endif	/* PERFSTAT  */
	"I:	toggle putprocid (processor # at start of line)",
	"j:	up select pid",
	"J:	down select pid",
	"k:	kill 9 selected pid",
	"K:	kill 6 selected pid",
	"l:	print load-average",
#ifdef MULTIBUS
	"m,M:	toggle mbad_debug",
#endif
	"p:	info on selected proc[]",
	"P:	info on all existing proc[]'s",
	"q,Q:	select stream module id",
	"r,R:	toglle selected stream id debug level",
	"s:	toggle syscalltrace",
	"S:	toggle swapdebug",
	"t:	toggle traptrace",
	"T:	stack-trace selected pid",
#ifdef	PERFSTAT
	"v:	some VM usage stats",
	"V:	some VM kluster stats",
#endif	/* PERFSTAT  */
	"y:	dump 'vmmeter' structure",
	"Y:	dump 'mem_stats' structure",
	"Z:	dump 'procstate' structure",
	"x:	toggle sig_debug",
	"X:	toggle ptrace_debug",
	"?:	what you're reading now",
	"7:	toggle tracing of emulator",
};
#define	NARRAY(array)	(sizeof(array)/sizeof(array[0]))

debugit(c)
	char	c;
{
	register struct proc *p;
	register int	i;
	int	got_mutex;
	extern int freemem;
	extern sema_t dc_mutex;			/* console output mutex */

#ifdef lint
	debug_ref();
#endif /*lint*/
	got_mutex = cp_sema(&dc_mutex);		/* at least try to mutex! */

	switch(c) {
	case 'a':				/* toggle abtdebug */
		BUMPDEBUG(abtdebug, 2);
		break;
#ifdef NETWORK
	case 'A':				/* ARP debugging */
		BUMPDEBUG(if_ether_debug, 3);
		break;
#endif /* NETWORK */
	case 'B':				/* dump buffer stats */
		DUMPSTAT(albshrink);
		DUMPSTAT(albgrow);
#ifdef	PERFSTAT
		{
			extern int numswfree, minswfree;
			printf("Nswbuf: %d, cur: %d, min: %d\n",
					v.v_pbuf, numswfree, minswfree);
		}
#endif	/* PERFSTAT  */
		break;
#ifdef SM_CHARACTER
	case 'C':
		BUMPDEBUG(smdebug, 4);
		break;
#endif
	case 'd':
		BUMPDEBUG(spec_debug_level, 10);
		break;
	case 'D':
		BUMPDEBUG(getdents_lvl, 2);
		break;
#ifdef NETWORK
	case 'e':				/* ether input debug */
		BUMPDEBUG(se_ibug, 3);
		break;
	case 'E':				/* ether output debug */
		BUMPDEBUG(se_obug, 3);
		break;
#endif /* NETWORK */
	case 'f':				/* report free-memory */
		DUMPSTAT(freemem);
		DUMPSTAT(dirtymem);
		break;
	case 'F':				/* more memory stats */
		DUMPSTAT(freemem);
		DUMPSTAT(dirtymem);
		printf("Ave free=%d, free30=%d, dirty=%d, dirty30=%d\n",
			avefree, avefree30, avedirty, avedirty30);
		DUMPSTAT(deficit);
#ifdef	PERFSTAT
		DUMPSTAT(min_freemem);
		DUMPSTAT(max_dirtymem);
		uptmap_stat();
#endif	/* PERFSTAT  */
		break;
#ifdef	GATESTAT
	case 'g':				/* dump gate statistics */
	case 'G':
		dumpgates();
		break;
#endif	/* GATESTAT  */
#ifdef	PERFSTAT
	case 'i':				/* stats on file/inode usage */
#ifdef notnow
		  extern int nclist, cfreecount, cfreelow;

			printf("	Max	Cur	Min\n");
			printf("Inode:	%d\n", v.v_inode);
			printf("File:	%d\n", v.v_file);
			printf("C-list:	%d	%d	%d\n",
					(nclist-1)*CBSIZE, cfreecount, cfreelow);
#endif /* notnow */
		}
		break;
#endif	/* PERFSTAT  */
	case 'I':				/* toggle putprocid */
		BUMPDEBUG(putprocid, 2);
		break;
	case 'j':				/* up selected proc */
		++D_proc;
		if (D_proc < &proc[1] || D_proc >= v.ve_proc)
			D_proc = &proc[1];
		procdump(D_proc);
		break;
	case 'J':				/* down selected proc */
		--D_proc;
		if (D_proc < &proc[1] || D_proc >= v.ve_proc)
			D_proc = v.ve_proc-1;
		procdump(D_proc);
		break;
	case 'k':				/* hard kill selected process */
		{				/* if want mult signals, make */
						/* this a procedure: D_kill */
			/*
			 * sort of in-line kill1(0, 9, D_proc->p_pid).
			 * Done this way to preserve "spl" (we are at intr
			 * level here if SCSISQMON) and avoid u. ref's.
			 */
			register spl_t s;
			register struct proc *kp;

			s = spl6();
			if (D_proc && (kp = pfind(D_proc->p_pid))) {
				lpsignal(kp, 9);
				v_lock(&kp->p_state, s);
			} else {
				splx(s);
				printf("Process not found.\n");
			}
		}
		break;
	case 'K':				/* abort selected process */
		{				/* if want mult signals, make */
						/* this a procedure: D_kill */
			/*
			 * sort of in-line kill1(0, 6, D_proc->p_pid).
			 * Done this way to preserve "spl" (we are at intr
			 * level here if SCSISQMON) and avoid u. ref's.
			 */
			register spl_t s;
			register struct proc *kp;

			s = spl6();
			if (D_proc && (kp = pfind(D_proc->p_pid))) {
				lpsignal(kp, 6);	/* SIGABRT, core dump */
				v_lock(&kp->p_state, s);
			} else {
				splx(s);
				printf("Process not found.\n");
			}
		}
		break;
	case 'l':				/* print load-average */
		printf("load: %d, %d, %d\n", avenrun[0], avenrun[1], avenrun[2]);
		break;
#ifdef MULTIBUS
	case 'm':				/* toggle mbad_debug */
	case 'M':
		BUMPDEBUG(mbad_debug, 2);
		break;
#endif
	case 'p':				/* dump selected proc[] */
		if (D_proc == 0)
			D_proc = &proc[1];
		procdump(D_proc);
		break;
	case 'P':				/* dump about everybody */
		printf("proc=%x\n", proc);
		for(p = proc; p < v.ve_proc; p++) {
			if (p->p_stat != NULL)
				procdump(p);
		}
		break;
	case 'Q':
		str_nextmod(1);
		str_prmod();
		break;
	case 'q':
		str_nextmod(0);
		str_prmod();
		break;
	case 'R':
		str_bumpdebug();
		break;
	case 'r':
		str_decdebug();
		break;
	case 's':				/* toggle sys-call tracing */
		BUMPDEBUG(syscalltrace, 2);
		break;
	case 'S':				/* toggle swapdebug */
		BUMPDEBUG(swapdebug, 3);
		break;
	case 't':				/* toggle trap tracing */
		BUMPDEBUG(traptrace, 3);
		break;
	case 'T':				/* stack-trace D_proc */
		p = D_proc;
		if ((p->p_flag & SLOAD) && (p->p_stat == SSLEEP || p->p_stat == SSTOP))
			dbg_stacktrace(p->p_uarea);
		else
			printf("can't stack-trace process\n");
		break;
#ifdef	PERFSTAT
	case 'v':				/* dump some VM stats */
		printf("pteasy=%d, ptexpand=%d\n",
				swptstat.pteasy, swptstat.ptexpand);
		{
			extern	int	cnt_RS_sfail, cnt_RS_grow, cnt_RS_rfail,
					cnt_RS_shrink, cnt_PFF_call;
			printf("RS calls=%d, grow=%d, shrink=%d, range-fail=%d, space-fail=%d\n",
				cnt_PFF_call, cnt_RS_grow,
				cnt_RS_shrink, cnt_RS_rfail, cnt_RS_sfail);
		}
		{
			extern long min_forkmap;
			printf("forkmap=%d, min_forkmap=%d, maxforkRS=%d\n",
					forkmap, min_forkmap, maxforkRS);
		}
		printf("dmmin=%d, dmmax=%d, maxdmap=%d\n", dmmin, dmmax, maxdmap);
		DUMPSTAT(set_sfswap);
		swapmap_stat();
		break;
	case 'V':				/* dump VM kluster stats */
		{	extern int klocnt[], klicnt[];
			printf("%d pgout_klust, %d pgin_klust\n",
					klocnt[0], klicnt[0]);
			printf("Size	klo	kli\n");
			for (i = 1; i <= KLMAX; i++) {
				printf("%d	%d	%d\n",
					i, klocnt[i], klicnt[i]);
			}
		}
		break;
#endif	/* PERFSTAT  */
	case 'y':				/* dump 'vmmeter' structure */
		dump_vmmeter(&l.cnt);
		break;
	case 'Y':				/* dump 'mem_stats' structure */
		dump_mem_stats(&mem_stats);
		break;
	case 'Z':				/* dump 'procstate' */
		dump_procstate(&procstate);
		break;
	case 'x':				/* toggle signal tracing */
		BUMPDEBUG(sig_debug, 3);
		break;
	case 'X':				/* toggle ptrace tracing */
		BUMPDEBUG(ptrace_debug, 3);
		break;
	case '?':
		for(i = 0; i < NARRAY(help); i++)
			printf("%s\n", help[i]);
		break;
	case '7':				/* toggle emuladebug */
		BUMPDEBUG(emuladebug, 3);
		break;
	default:
		printf("Use '?' for help.\n");
		break;
	}

	if (got_mutex)
		v_sema(&dc_mutex);
}

#ifdef	GATESTAT
/*
 * dumpgates()
 *	Dump about gate collisions.  See ../machine/gate.c
 *
 * This junk taken from ../test/tgate.c; must track changes to gate
 * DS's to be useful!
 */

struct	gate	{			/* stolen from gate.c */
	short		g_procid;	/* processor currently holding */
	unsigned long	g_tries;	/* P() count */
	unsigned long	g_collide;	/* collision count */
};
extern	struct gate	gate[];

static	unsigned long collide[NUMGATES];
static	unsigned long tries[NUMGATES];

/*
 * a = what % of b, with round up, and scaled using int's
 */
#define	PCTOF(a,b)	((100*(a)+((b)/2))/(b))

dumpgates()
{
	register struct gate *g;
	register int i;
	int	cval, cdelta;
	int	tval, tdelta;
	int	cdeltsum, ctotsum;
	int	tdeltsum, ttotsum;

	ttotsum = tdeltsum = 0;
	ctotsum = cdeltsum = 0;
	printf("\t\tTotals\t\t\tDeltas\n");
	printf("Gate\tTries\tColl's\t%%Coll\tTries\tColl's\t%%Coll's\n");
	for(i = 0, g = gate; g < &gate[NUMGATES]; g++, i++) {
		tval = g->g_tries;
		cval = g->g_collide;
		tdelta = tval - tries[i];
		cdelta = cval - collide[i];
		if (tdelta > 0) {
			printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n", i,
					tval,   cval,   PCTOF(cval,tval),
					tdelta, cdelta, PCTOF(cdelta,tdelta)
				);
			collide[i] = cval;
			tries[i] = tval;
			tdeltsum += tdelta;
			cdeltsum += cdelta;
			ttotsum += tval;
			ctotsum += cval;
		}
	}
	if (tdeltsum > 0) {
		printf("Totals:\t%d\t%d\t%d\t%d\t%d\t%d\n",
				ttotsum, ctotsum, PCTOF(ctotsum,ttotsum),
				tdeltsum, cdeltsum, PCTOF(cdeltsum,tdeltsum)
			);
	} else
		printf("No change\n");
}
#endif	/* GATESTAT  */

#define	PRINTVM(f)	printf("f\t%d\n", vm->f)
dump_vmmeter(vm)
	register struct vmmeter *vm;
{
	PRINTVM(v_swtch);
	PRINTVM(v_trap);
	PRINTVM(v_syscall);
	PRINTVM(v_intr);
	PRINTVM(v_swpin);
	PRINTVM(v_swpout);
	PRINTVM(v_pswpin);
	PRINTVM(v_pswpout);
	PRINTVM(v_pgin_file);
	PRINTVM(v_pgin_swap);
	PRINTVM(v_pgout_file);
	PRINTVM(v_pgout_swap);
	PRINTVM(v_pgpgin_file);
	PRINTVM(v_pgpgin_swap);
	PRINTVM(v_pgpgout_file);
	PRINTVM(v_pgpgout_swap);
	PRINTVM(v_intrans);
	PRINTVM(v_pgfrec);
	PRINTVM(v_pgdrec);
	PRINTVM(v_zfod);
	PRINTVM(v_nffod	);
	PRINTVM(v_nzfod	);
	PRINTVM(v_realloc);
	PRINTVM(v_redofod);
	PRINTVM(v_faults);
	PRINTVM(v_dfree);
	PRINTVM(v_pffincr);
	PRINTVM(v_pffdecr);
	PRINTVM(v_tlbflush);
	PRINTVM(v_time[V_CPU_IDLE]);
	PRINTVM(v_time[V_CPU_USER]);
	PRINTVM(v_time[V_CPU_KERNEL]);
	PRINTVM(v_rcvint);
	PRINTVM(v_xmtint);
	PRINTVM(v_mdmint);
	PRINTVM(v_rawch);
	PRINTVM(v_canch);
	PRINTVM(v_outch);
	PRINTVM(v_cntfork);
	PRINTVM(v_cntvfork);
	PRINTVM(v_sizfork);
	PRINTVM(v_sizvfork);
	PRINTVM(v_cntexec);
	PRINTVM(v_lookup);
	PRINTVM(v_iget);
	PRINTVM(v_dirblk);
	PRINTVM(v_symlink);
	PRINTVM(v_nc_hits);
	PRINTVM(v_nc_misses);
	PRINTVM(v_nc_long_look);
	PRINTVM(v_nc_long_enter);
	PRINTVM(v_nc_lru_empty);
	PRINTVM(v_nc_purges);
	PRINTVM(v_nc_badname);
	PRINTVM(v_msg);
	PRINTVM(v_sema);
	PRINTVM(v_sysread);
	PRINTVM(v_syswrite);
	PRINTVM(v_readch);
	PRINTVM(v_writech);
	PRINTVM(v_lread);
	PRINTVM(v_lwrite);
	PRINTVM(v_delayedwrite);
	PRINTVM(v_bread);
	PRINTVM(v_bwrite);
	PRINTVM(v_rdahead);
	PRINTVM(v_rainbc);
	PRINTVM(v_rawasted);
	PRINTVM(v_bhitra);
	PRINTVM(v_bhitru);
	PRINTVM(v_bnotavail);
	PRINTVM(v_phread);
	PRINTVM(v_phwrite);
	PRINTVM(v_phreadblks);
	PRINTVM(v_phwriteblks);
}

#define	PRINTM(f)	printf("f\t%d\n", ms->f)
dump_mem_stats(ms)
	register struct mem_stats *ms;
{
	PRINTM(m_freemem[0]);
	PRINTM(m_freemem[1]);
	PRINTM(m_dirtymem[0]);
	PRINTM(m_dirtymem[1]);
	PRINTM(m_totalvm);
}

#define	PRINTP(f)	printf("f\t%d\n", ps->f)
dump_procstate(ps)
	register struct procstate *ps;
{
	PRINTP(ps_swoutq);
	PRINTP(ps_swinq);
	PRINTP(ps_pagewait);
	PRINTP(ps_filewait);
	PRINTP(ps_physwait);
	PRINTP(ps_cpuwait);
	PRINTP(ps_wait[0]);
	PRINTP(ps_wait[1]);
	PRINTP(ps_wait[2]);
	PRINTP(ps_wait[3]);
	PRINTP(ps_onproc);
	PRINTP(ps_runq);
	PRINTP(ps_fastwait);
	PRINTP(ps_sleep);
	PRINTP(ps_swap);
	PRINTP(ps_rqsum);
	PRINTP(ps_rqocc);
	PRINTP(ps_swinqsum);
	PRINTP(ps_swinqocc);
	PRINTP(ps_swoutqsum);
	PRINTP(ps_swoutqocc);
}

/*
 * dbg_stacktrace()
 *	Print process stack-trace given pointer to it's Uarea.
 *
 * Currently, u_ap -> saved ebp,pc (see resume).
 */

dbg_stacktrace(ua)
	struct	user	*ua;
{
	dbg_printstacktrace(ua, (int *) ua->u_sp);
}

/*
 * dbg_printstacktrace() is cloned from printstacktrace()
 */

#define	TOP_KSTK	((int *)(VA_UAREA - 1))
#define	BOT_KSTK	((int *)VA_UBLOCK)
#define	POPECX		0x59			/* pops a single argument */
#define	ADDLESP		0xc483			/* addl $xxx, %esp */

#define	RELOC(t,addr,ua)	(t) ((int)(ua) + ((int)(addr) - (int)&u))

dbg_printstacktrace(ua, bp)
	struct user *ua;
	int	*bp;			/* current frame pointer (unrelocated) */
{
	register int *rbp = RELOC(int*,bp,ua);	/* relocated frame pointer */
	int	*newbp = (int *) rbp[0];	/* next frame pointer */
	int	*oldpc = (int *) rbp[1];	/* pc this frame returns to */
	int	*oldap = &rbp[2];		/* point to first argument */
	int	 nargs;				/* number of arguments passed */
	extern	int etext;

	if ((oldpc < &etext) && ((*oldpc & 0xffff) == ADDLESP))
		nargs = ((*oldpc>>16) & 0xff) / 4;
	else if ((oldpc < &etext) && ((*oldpc & 0xff) == POPECX))
		nargs = 1;
	else
		nargs = 5;	/* default for OS */

	printf("@ 0x%x call(", oldpc);
	while (nargs) {
		printf("0x%x", *oldap++);
		if (--nargs)
			printf(", ");
	}
	printf(")\n");

	if ((newbp < TOP_KSTK) && (newbp > bp) && (newbp > BOT_KSTK))
		dbg_printstacktrace(ua, newbp);
}

dump_fpu(fpu)
	register struct fpusave *fpu;
{
	register int i;

	printf("fpu_control =	0x%x\n", fpu->fpu_control);
	printf("fpu_status =	0x%x\n", fpu->fpu_status);
	printf("fpu_tag =	0x%x\n", fpu->fpu_tag);
	printf("fpu_ip =	0x%x\n", fpu->fpu_ip);
	printf("fpu_cs =	0x%x\n", fpu->fpu_cs);
	printf("fpu_data_offset= 0x%x\n", fpu->fpu_data_offset);
	for (i = 0; i < 8; i++) {
		printf("fpu_stack[%d] = 0x%x 0x%x 0x%x 0x%x 0x%x\n", i,
			fpu->fpu_stack[i][0],
			fpu->fpu_stack[i][1],
			fpu->fpu_stack[i][2],
			fpu->fpu_stack[i][3],
			fpu->fpu_stack[i][4]
		);
	}
}

/*
 * Dump page-tables (for by-hand check).
 */

dumpPT(l1pt)
	struct pte *l1pt;
{
	register int i, j;
	register struct pte *pte;

	for (i = 0; i < KL1PT_PAGES*NPTEPG; i++) {
		if (*(int*)(&l1pt[i]) != PG_INVAL) {
			printf("l1pt[%d] = 0x%x\n", i, *(int*)(&l1pt[i]));
			pte = (struct pte *)PTETOPHYS(l1pt[i]);
			for (j = 0; j < NPTEPG; j++) {
				if (*(int*)(&pte[j]) != PG_INVAL) {
					printf("\tl2pt[%d][%d] = 0x%x\n",
						i, j, *(int*)(&pte[j]));
				}
			}
		}
	}
}

/*
 * Dump IDT and GDT for hand-check.
 */

dumpDT()
{
	register int i;

	printf("GDT @ 0x%x\n", l.eng->e_local->pp_local[0] + ((int)l.gdt - (int)&l));
	for (i = 0; i < GDT_SIZE; i++)
		printf("\tgdt[%d] = 0x%x 0x%x\n", i,
			*(int*)(&l.gdt[i]), *((int*)(&l.gdt[i])+1) );

	printf("IDT @ 0x%x\n", l.eng->e_local->pp_local[0] + ((int)l.idt - (int)&l));
	for (i = 0; i < IDT_SIZE; i++)
		printf("\tidt[%d] = 0x%x 0x%x\n", i,
			*(int*)(&l.idt[i]), *((int*)(&l.idt[i])+1) );

	printf("LDT @ 0x%x\n", l.eng->e_local->pp_local[0] + ((int)l.ldt - (int)&l));
	for (i = 0; i < LDT_SIZE; i++)
		printf("\tldt[%d] = 0x%x 0x%x\n", i,
			*(int*)(&l.ldt[i]), *((int*)(&l.ldt[i])+1) );

	printf("KERNEL_CS = 0x%x\n", KERNEL_CS);
	printf("KERNEL_DS = 0x%x\n", KERNEL_DS);
	printf("USER_CS = 0x%x\n", USER_CS);
	printf("USER_DS = 0x%x\n", USER_DS);
}

checkspl0(i)
{
	register spl_t s = spl0();
	
	if ((s & 0xff) != SPL0)
		printf("!SPL0 %d, at 0x%x\n", i, s & 0xff);
	asm("pushfl");
	asm("popl %edi");
	if ((s & FLAGS_IF) == 0)
		printf("!STI %d\n", i);
}

checkrunQ(i)
{
	if (CP_GATE(G_RUNQ))
		V_GATE(G_RUNQ);
	else
		printf("runQ held! %d\n", i);
}

dumpUPT(p)
	struct proc *p;
{
	register int i;
	register struct pte *pte;

	printf("<%d,%s>: dsize=%d, ssize=%d\n",
		p-proc, p->p_uarea->u_comm, p->p_dsize, p->p_ssize);

	pte = dvtopte(p, 0);
	printf("text+data ptes...\n");
	for (i = 0; i < p->p_dsize; i++, pte++)
		printf("\t[%d] @ 0x%x = 0x%x\n", i, pte, *(int*)pte);

	printf("stack ptes...\n");
	i = p->p_ssize-1;
	pte = sptopte(p, i);
	for (; i >= 0; i--, pte++)
		printf("\t[%d] @ 0x%x = 0x%x\n", i, pte, *(int*)pte);

	printf("Rset: rssize=%d, rshand=%d, rscurr=%d\n",
		p->p_rssize, p->p_rshand, p->p_rscurr);
}

static	char	hex[] = "0123456789abcdef";

dump_bytes(cp, len)
	register char	*cp;
	register int	len;
{
	register int	cnt;

	for (cnt = 0; cnt < len; cnt++, cp++) {
		if ((cnt % 20) == 0)
			printf("\n\t");
		printf(" %c%c", hex[((int)(*cp) >> 4) & 0xf], hex[(*cp) & 0xf]);
	}
	printf("\n");
}

#ifdef lint
debug_ref()
{
	dumpPT((struct pte *)0);
	dumpDT();
	checkspl0(0);
	checkrunQ(0);
	dump_bytes((char *)0, 0);
}
#endif /*lint*/
#endif	/* DEBUG  */
