hexdump.c revision 14768
19Sjkh/*-
29Sjkh * Copyright (c) 1986, 1988, 1991, 1993
39Sjkh *	The Regents of the University of California.  All rights reserved.
49Sjkh * (c) UNIX System Laboratories, Inc.
550472Speter * All or some portions of this file are derived from material licensed
69Sjkh * to the University of California by American Telephone and Telegraph
79Sjkh * Co. or Unix System Laboratories, Inc. and are reproduced herein with
89Sjkh * the permission of UNIX System Laboratories, Inc.
99Sjkh *
109Sjkh * Redistribution and use in source and binary forms, with or without
119Sjkh * modification, are permitted provided that the following conditions
129Sjkh * are met:
139Sjkh * 1. Redistributions of source code must retain the above copyright
149Sjkh *    notice, this list of conditions and the following disclaimer.
159Sjkh * 2. Redistributions in binary form must reproduce the above copyright
169Sjkh *    notice, this list of conditions and the following disclaimer in the
1711891Speter *    documentation and/or other materials provided with the distribution.
1811891Speter * 3. All advertising materials mentioning features or use of this software
1911891Speter *    must display the following acknowledgement:
2011891Speter *	This product includes software developed by the University of
2111891Speter *	California, Berkeley and its contributors.
229Sjkh * 4. Neither the name of the University nor the names of its contributors
239Sjkh *    may be used to endorse or promote products derived from this software
249Sjkh *    without specific prior written permission.
259Sjkh *
269Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
279Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
289Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
299Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
309Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
319Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
329Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
339Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
349Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
359Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
369Sjkh * SUCH DAMAGE.
379Sjkh *
389Sjkh *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
3911891Speter * $Id: subr_prf.c,v 1.30 1996/02/28 21:42:15 gpalmer Exp $
4011891Speter */
419Sjkh
429Sjkh#include "opt_ddb.h"
439Sjkh
449Sjkh#include <sys/param.h>
459Sjkh#include <sys/systm.h>
469Sjkh#include <sys/reboot.h>
479Sjkh#include <sys/msgbuf.h>
489Sjkh#include <sys/proc.h>
499Sjkh#include <sys/vnode.h>
509Sjkh#include <sys/tty.h>
519Sjkh#include <sys/tprintf.h>
529Sjkh#include <sys/syslog.h>
539Sjkh#include <sys/malloc.h>
549Sjkh#include <sys/kernel.h>
559Sjkh#include <sys/sysctl.h>
569Sjkh#include <machine/cons.h>
579Sjkh
589Sjkh/*
599Sjkh * Note that stdarg.h and the ANSI style va_start macro is used for both
609Sjkh * ANSI and traditional C compilers.
619Sjkh */
629Sjkh#include <machine/stdarg.h>
639Sjkh
649Sjkh#if defined(DDB) || defined (KGDB)
6511891Speter#ifdef DDB_UNATTENDED
669Sjkh	static int debugger_on_panic = 0;
679Sjkh#else
689Sjkh	static int debugger_on_panic = 1;
699Sjkh#endif
709Sjkh
719SjkhSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
7211891Speter	&debugger_on_panic, 0, "");
7311891Speter#endif
749Sjkh
759Sjkh#define TOCONS	0x01
769Sjkh#define TOTTY	0x02
779Sjkh#define TOLOG	0x04
789Sjkh
799Sjkhstruct	tty *constty;			/* pointer to console "window" tty */
809Sjkh
819Sjkhstatic void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
829Sjkh
839Sjkhstatic void  logpri __P((int level));
849Sjkhstatic void  msglogchar(int c, void *dummyarg);
859Sjkhstruct putchar_arg {int flags; struct tty *tty; };
869Sjkhstatic void  putchar __P((int ch, void *arg));
879Sjkhstatic char *ksprintn __P((u_long num, int base, int *len));
889Sjkh
899Sjkhstatic int consintr = 1;		/* Ok to handle console interrupts? */
909Sjkh
919Sjkh/*
929Sjkh * Variable panicstr contains argument to first call to panic; used as flag
939Sjkh * to indicate that the kernel has already called panic.
949Sjkh */
959Sjkhconst char *panicstr;
9611891Speter
9711891Speter/*
9811891Speter * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
9911891Speter * and then reboots.  If we are called twice, then we avoid trying to sync
10011891Speter * the disks as this often leads to recursive panics.
10111891Speter */
10211891Speter#ifdef __GNUC__
10311891Speter__dead			/* panic() does not return */
10411891Speter#endif
10511891Spetervoid
1069Sjkhpanic(const char *fmt, ...)
1079Sjkh{
1089Sjkh	int bootopt;
10911891Speter	va_list ap;
11011891Speter
11111891Speter	bootopt = RB_AUTOBOOT | RB_DUMP;
1129Sjkh	if (panicstr)
1139Sjkh		bootopt |= RB_NOSYNC;
1149Sjkh	else
1159Sjkh		panicstr = fmt;
1169Sjkh
1179Sjkh	printf("panic: ");
1189Sjkh	va_start(ap, fmt);
1199Sjkh	vprintf(fmt, ap);
1209Sjkh	va_end(ap);
1219Sjkh	printf("\n");
1229Sjkh
1239Sjkh	if (debugger_on_panic) {
1249Sjkh#ifdef KGDB
1259Sjkh		kgdb_panic();
12611891Speter#endif
12711891Speter#ifdef DDB
12811891Speter		Debugger ("panic");
12911891Speter#endif
13011891Speter	}
13111891Speter	boot(bootopt);
13211891Speter}
13311891Speter
1349Sjkh/*
1359Sjkh * Warn that a system table is full.
1369Sjkh */
1379Sjkhvoid
1389Sjkhtablefull(tab)
1399Sjkh	const char *tab;
1409Sjkh{
1419Sjkh
1429Sjkh	log(LOG_ERR, "%s: table is full\n", tab);
1439Sjkh}
1449Sjkh
1459Sjkh/*
1469Sjkh * Uprintf prints to the controlling terminal for the current process.
1479Sjkh * It may block if the tty queue is overfull.  No message is printed if
1489Sjkh * the queue does not clear in a reasonable time.
1499Sjkh */
1509Sjkhvoid
1519Sjkhuprintf(const char *fmt, ...)
1529Sjkh{
1539Sjkh	struct proc *p = curproc;
1549Sjkh	va_list ap;
1559Sjkh	struct putchar_arg pca;
1569Sjkh
1579Sjkh	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
1589Sjkh		va_start(ap, fmt);
1599Sjkh		pca.tty = p->p_session->s_ttyp;
1609Sjkh		pca.flags = TOTTY;
1619Sjkh		kvprintf(fmt, putchar, &pca, 10, ap);
1629Sjkh		va_end(ap);
1639Sjkh	}
1649Sjkh}
1659Sjkh
1669Sjkhtpr_t
1679Sjkhtprintf_open(p)
1689Sjkh	register struct proc *p;
1699Sjkh{
1709Sjkh
1719Sjkh	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
1729Sjkh		SESSHOLD(p->p_session);
1739Sjkh		return ((tpr_t) p->p_session);
1749Sjkh	}
17511891Speter	return ((tpr_t) NULL);
1769Sjkh}
17711891Speter
1789Sjkhvoid
1799Sjkhtprintf_close(sess)
1809Sjkh	tpr_t sess;
1819Sjkh{
1829Sjkh
1839Sjkh	if (sess)
18411891Speter		SESSRELE((struct session *) sess);
1859Sjkh}
18611891Speter
1879Sjkh/*
18811891Speter * tprintf prints on the controlling terminal associated
1899Sjkh * with the given session.
1909Sjkh */
1919Sjkhvoid
1929Sjkhtprintf(tpr_t tpr, const char *fmt, ...)
1939Sjkh{
1949Sjkh	register struct session *sess = (struct session *)tpr;
1959Sjkh	struct tty *tp = NULL;
1969Sjkh	int flags = TOLOG;
1979Sjkh	va_list ap;
1989Sjkh	struct putchar_arg pca;
1999Sjkh
2009Sjkh	logpri(LOG_INFO);
2019Sjkh	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
2029Sjkh		flags |= TOTTY;
2039Sjkh		tp = sess->s_ttyp;
204	}
205	va_start(ap, fmt);
206	pca.tty = tp;
207	pca.flags = flags;
208	kvprintf(fmt, putchar, &pca, 10, ap);
209	va_end(ap);
210	logwakeup();
211}
212
213/*
214 * Ttyprintf displays a message on a tty; it should be used only by
215 * the tty driver, or anything that knows the underlying tty will not
216 * be revoke(2)'d away.  Other callers should use tprintf.
217 */
218void
219ttyprintf(struct tty *tp, const char *fmt, ...)
220{
221	va_list ap;
222	struct putchar_arg pca;
223	va_start(ap, fmt);
224	pca.tty = tp;
225	pca.flags = TOTTY;
226	kvprintf(fmt, putchar, &pca, 10, ap);
227	va_end(ap);
228}
229
230extern	int log_open;
231
232/*
233 * Log writes to the log buffer, and guarantees not to sleep (so can be
234 * called by interrupt routines).  If there is no process reading the
235 * log yet, it writes to the console also.
236 */
237void
238log(int level, const char *fmt, ...)
239{
240	register int s;
241	va_list ap;
242
243	s = splhigh();
244	logpri(level);
245	va_start(ap, fmt);
246
247	kvprintf(fmt, msglogchar, NULL, 10, ap);
248	va_end(ap);
249
250	splx(s);
251	if (!log_open) {
252		struct putchar_arg pca;
253		va_start(ap, fmt);
254		pca.tty = NULL;
255		pca.flags = TOCONS;
256		kvprintf(fmt, putchar, &pca, 10, ap);
257		va_end(ap);
258	}
259	logwakeup();
260}
261
262static void
263logpri(level)
264	int level;
265{
266	register char *p;
267
268	msglogchar('<', NULL);
269	for (p = ksprintn((u_long)level, 10, NULL); *p;)
270		msglogchar(*p--, NULL);
271	msglogchar('>', NULL);
272}
273
274void
275addlog(const char *fmt, ...)
276{
277	register int s;
278	va_list ap;
279
280	s = splhigh();
281	va_start(ap, fmt);
282	kvprintf(fmt, msglogchar, NULL, 10, ap);
283	splx(s);
284	va_end(ap);
285	if (!log_open) {
286		struct putchar_arg pca;
287		va_start(ap, fmt);
288		pca.tty = NULL;
289		pca.flags = TOCONS;
290		kvprintf(fmt, putchar, &pca, 10, ap);
291		va_end(ap);
292	}
293	logwakeup();
294}
295
296int
297printf(const char *fmt, ...)
298{
299	va_list ap;
300	register int savintr;
301	struct putchar_arg pca;
302	int retval;
303
304	savintr = consintr;		/* disable interrupts */
305	consintr = 0;
306	va_start(ap, fmt);
307	pca.tty = NULL;
308	pca.flags = TOCONS | TOLOG;
309	retval = kvprintf(fmt, putchar, &pca, 10, ap);
310	va_end(ap);
311	if (!panicstr)
312		logwakeup();
313	consintr = savintr;		/* reenable interrupts */
314	return retval;
315}
316
317void
318vprintf(const char *fmt, va_list ap)
319{
320	register int savintr;
321	struct putchar_arg pca;
322
323	savintr = consintr;		/* disable interrupts */
324	consintr = 0;
325	pca.tty = NULL;
326	pca.flags = TOCONS | TOLOG;
327	kvprintf(fmt, putchar, &pca, 10, ap);
328	if (!panicstr)
329		logwakeup();
330	consintr = savintr;		/* reenable interrupts */
331}
332
333/*
334 * Print a character on console or users terminal.  If destination is
335 * the console then the last MSGBUFS characters are saved in msgbuf for
336 * inspection later.
337 */
338static void
339putchar(int c, void *arg)
340{
341	struct putchar_arg *ap = (struct putchar_arg*) arg;
342	int flags = ap->flags;
343	struct tty *tp = ap->tty;
344	if (panicstr)
345		constty = NULL;
346	if ((flags & TOCONS) && tp == NULL && constty) {
347		tp = constty;
348		flags |= TOTTY;
349	}
350	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
351	    (flags & TOCONS) && tp == constty)
352		constty = NULL;
353	if ((flags & TOLOG))
354		msglogchar(c, NULL);
355	if ((flags & TOCONS) && constty == NULL && c != '\0')
356		(*v_putc)(c);
357}
358
359/*
360 * Scaled down version of sprintf(3).
361 */
362int
363sprintf(char *buf, const char *cfmt, ...)
364{
365	int retval;
366	va_list ap;
367
368	va_start(ap, cfmt);
369	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
370	buf[retval] = '\0';
371	va_end(ap);
372	return retval;
373}
374
375/*
376 * Put a number (base <= 16) in a buffer in reverse order; return an
377 * optional length and a pointer to the NULL terminated (preceded?)
378 * buffer.
379 */
380static char *
381ksprintn(ul, base, lenp)
382	register u_long ul;
383	register int base, *lenp;
384{					/* A long in base 8, plus NULL. */
385	static char buf[sizeof(long) * NBBY / 3 + 2];
386	register char *p;
387
388	p = buf;
389	do {
390		*++p = hex2ascii(ul % base);
391	} while (ul /= base);
392	if (lenp)
393		*lenp = p - buf;
394	return (p);
395}
396
397/*
398 * Scaled down version of printf(3).
399 *
400 * Two additional formats:
401 *
402 * The format %b is supported to decode error registers.
403 * Its usage is:
404 *
405 *	printf("reg=%b\n", regval, "<base><arg>*");
406 *
407 * where <base> is the output base expressed as a control character, e.g.
408 * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
409 * the first of which gives the bit number to be inspected (origin 1), and
410 * the next characters (up to a control character, i.e. a character <= 32),
411 * give the name of the register.  Thus:
412 *
413 *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
414 *
415 * would produce output:
416 *
417 *	reg=3<BITTWO,BITONE>
418 *
419 * XXX:  %D  -- Hexdump, takes pointer and separator string:
420 *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
421 *		("%*D", len, ptr, " " -> XX XX XX XX ...
422 */
423int
424kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
425{
426#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
427	char *p, *q, *d;
428	u_char *up;
429	int ch, n;
430	u_long ul;
431	int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
432	int dwidth;
433	char padc;
434	int retval = 0;
435
436	if (!func)
437		d = (char *) arg;
438	else
439		d = NULL;
440
441	if (fmt == NULL)
442		fmt = "(fmt null)\n";
443
444	if (radix < 2 || radix > 36)
445		radix = 10;
446
447	for (;;) {
448		padc = ' ';
449		width = 0;
450		while ((ch = *(u_char *)fmt++) != '%') {
451			if (ch == '\0')
452				return retval;
453			PCHAR(ch);
454		}
455		lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
456		sign = 0; dot = 0; dwidth = 0;
457reswitch:	switch (ch = *(u_char *)fmt++) {
458		case '.':
459			dot = 1;
460			goto reswitch;
461		case '#':
462			sharpflag = 1;
463			goto reswitch;
464		case '+':
465			sign = 1;
466			goto reswitch;
467		case '-':
468			ladjust = 1;
469			goto reswitch;
470		case '%':
471			PCHAR(ch);
472			break;
473		case '*':
474			if (!dot) {
475				width = va_arg(ap, int);
476				if (width < 0) {
477					ladjust = !ladjust;
478					width = -width;
479				}
480			} else {
481				dwidth = va_arg(ap, int);
482			}
483			goto reswitch;
484		case '0':
485			if (!dot) {
486				padc = '0';
487				goto reswitch;
488			}
489		case '1': case '2': case '3': case '4':
490		case '5': case '6': case '7': case '8': case '9':
491				for (n = 0;; ++fmt) {
492					n = n * 10 + ch - '0';
493					ch = *fmt;
494					if (ch < '0' || ch > '9')
495						break;
496				}
497			if (dot)
498				dwidth = n;
499			else
500				width = n;
501			goto reswitch;
502		case 'b':
503			ul = va_arg(ap, int);
504			p = va_arg(ap, char *);
505			for (q = ksprintn(ul, *p++, NULL); *q;)
506				PCHAR(*q--);
507
508			if (!ul)
509				break;
510
511			for (tmp = 0; *p;) {
512				n = *p++;
513				if (ul & (1 << (n - 1))) {
514					PCHAR(tmp ? ',' : '<');
515					for (; (n = *p) > ' '; ++p)
516						PCHAR(n);
517					tmp = 1;
518				} else
519					for (; *p > ' '; ++p)
520						continue;
521			}
522			if (tmp)
523				PCHAR('>');
524			break;
525		case 'c':
526			PCHAR(va_arg(ap, int));
527			break;
528		case 'D':
529			up = va_arg(ap, u_char *);
530			p = va_arg(ap, char *);
531			if (!width)
532				width = 16;
533			while(width--) {
534				PCHAR(hex2ascii(*up >> 4));
535				PCHAR(hex2ascii(*up & 0x0f));
536				up++;
537				if (width)
538					for (q=p;*q;q++)
539						PCHAR(*q);
540			}
541			break;
542		case 'd':
543			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
544			sign = 1;
545			base = 10;
546			goto number;
547		case 'l':
548			lflag = 1;
549			goto reswitch;
550		case 'n':
551			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
552			base = radix;
553			goto number;
554		case 'o':
555			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
556			base = 8;
557			goto number;
558		case 'p':
559			ul = (u_long)va_arg(ap, void *);
560			base = 16;
561			PCHAR('0');
562			PCHAR('x');
563			goto number;
564		case 's':
565			p = va_arg(ap, char *);
566			if (p == NULL)
567				p = "(null)";
568			if (!dot)
569				n = strlen (p);
570			else
571				for (n = 0; n < dwidth && p[n]; n++)
572					continue;
573
574			width -= n;
575
576			if (!ladjust && width > 0)
577				while (width--)
578					PCHAR(padc);
579			while (n--)
580				PCHAR(*p++);
581			if (ladjust && width > 0)
582				while (width--)
583					PCHAR(padc);
584			break;
585		case 'u':
586			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
587			base = 10;
588			goto number;
589		case 'x':
590			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
591			base = 16;
592number:			if (sign && (long)ul < 0L) {
593				neg = 1;
594				ul = -(long)ul;
595			}
596			p = ksprintn(ul, base, &tmp);
597			if (sharpflag && ul != 0) {
598				if (base == 8)
599					tmp++;
600				else if (base == 16)
601					tmp += 2;
602			}
603			if (neg)
604				tmp++;
605
606			if (!ladjust && width && (width -= tmp) > 0)
607				while (width--)
608					PCHAR(padc);
609			if (neg)
610				PCHAR('-');
611			if (sharpflag && ul != 0) {
612				if (base == 8) {
613					PCHAR('0');
614				} else if (base == 16) {
615					PCHAR('0');
616					PCHAR('x');
617				}
618			}
619
620			while (*p)
621				PCHAR(*p--);
622
623			if (ladjust && width && (width -= tmp) > 0)
624				while (width--)
625					PCHAR(padc);
626
627			break;
628		default:
629			PCHAR('%');
630			if (lflag)
631				PCHAR('l');
632			PCHAR(ch);
633			break;
634		}
635	}
636#undef PCHAR
637}
638
639/*
640 * Put character in log buffer.
641 */
642static void
643msglogchar(int c, void *dummyarg)
644{
645	struct msgbuf *mbp;
646
647	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
648		mbp = msgbufp;
649		if (mbp->msg_magic != MSG_MAGIC ||
650		    mbp->msg_bufx >= MSG_BSIZE ||
651		    mbp->msg_bufr >= MSG_BSIZE) {
652			bzero(mbp, sizeof(struct msgbuf));
653			mbp->msg_magic = MSG_MAGIC;
654		}
655		mbp->msg_bufc[mbp->msg_bufx++] = c;
656		if (mbp->msg_bufx >= MSG_BSIZE)
657			mbp->msg_bufx = 0;
658		/* If the buffer is full, keep the most recent data. */
659		if (mbp->msg_bufr == mbp->msg_bufx) {
660			if (++mbp->msg_bufr >= MSG_BSIZE)
661				mbp->msg_bufr = 0;
662		}
663	}
664}
665