hexdump.c revision 13446
1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
39 * $Id: subr_prf.c,v 1.20 1996/01/04 21:12:15 wollman Exp $
40 */
41
42#include "opt_ddb.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/reboot.h>
47#include <sys/msgbuf.h>
48#include <sys/proc.h>
49#include <sys/vnode.h>
50#include <sys/tty.h>
51#include <sys/tprintf.h>
52#include <sys/syslog.h>
53#include <sys/malloc.h>
54#include <machine/cons.h>
55
56/*
57 * Note that stdarg.h and the ANSI style va_start macro is used for both
58 * ANSI and traditional C compilers.
59 */
60#include <machine/stdarg.h>
61
62#ifdef KADB
63#include <machine/kdbparam.h>
64#endif
65
66
67#define TOCONS	0x01
68#define TOTTY	0x02
69#define TOLOG	0x04
70
71struct	tty *constty;			/* pointer to console "window" tty */
72
73static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
74
75static void  logpri __P((int level));
76static void  msglogchar(int c, void *dummyarg);
77struct putchar_arg {int flags; struct tty *tty; };
78static void  putchar __P((int ch, void *arg));
79static char *ksprintn __P((u_long num, int base, int *len));
80
81static int consintr = 1;		/* Ok to handle console interrupts? */
82
83/*
84 * Variable panicstr contains argument to first call to panic; used as flag
85 * to indicate that the kernel has already called panic.
86 */
87const char *panicstr;
88
89/*
90 * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
91 * and then reboots.  If we are called twice, then we avoid trying to sync
92 * the disks as this often leads to recursive panics.
93 */
94#ifdef __GNUC__
95__dead			/* panic() does not return */
96#endif
97void
98panic(const char *fmt, ...)
99{
100	int bootopt;
101	va_list ap;
102
103	bootopt = RB_AUTOBOOT | RB_DUMP;
104	if (panicstr)
105		bootopt |= RB_NOSYNC;
106	else
107		panicstr = fmt;
108
109	va_start(ap, fmt);
110	printf("panic: %r\n", fmt, ap);
111	va_end(ap);
112
113#ifdef KGDB
114	kgdb_panic();
115#endif
116#ifdef KADB
117	if (boothowto & RB_KDB)
118		kdbpanic();
119#endif
120#ifdef DDB
121	Debugger ("panic");
122#endif
123	boot(bootopt);
124}
125
126/*
127 * Warn that a system table is full.
128 */
129void
130tablefull(tab)
131	const char *tab;
132{
133
134	log(LOG_ERR, "%s: table is full\n", tab);
135}
136
137/*
138 * Uprintf prints to the controlling terminal for the current process.
139 * It may block if the tty queue is overfull.  No message is printed if
140 * the queue does not clear in a reasonable time.
141 */
142void
143uprintf(const char *fmt, ...)
144{
145	struct proc *p = curproc;
146	va_list ap;
147	struct putchar_arg pca;
148
149	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
150		va_start(ap, fmt);
151		pca.tty = p->p_session->s_ttyp;
152		pca.flags = TOTTY;
153		kvprintf(fmt, putchar, &pca, 10, ap);
154		va_end(ap);
155	}
156}
157
158tpr_t
159tprintf_open(p)
160	register struct proc *p;
161{
162
163	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
164		SESSHOLD(p->p_session);
165		return ((tpr_t) p->p_session);
166	}
167	return ((tpr_t) NULL);
168}
169
170void
171tprintf_close(sess)
172	tpr_t sess;
173{
174
175	if (sess)
176		SESSRELE((struct session *) sess);
177}
178
179/*
180 * tprintf prints on the controlling terminal associated
181 * with the given session.
182 */
183void
184tprintf(tpr_t tpr, const char *fmt, ...)
185{
186	register struct session *sess = (struct session *)tpr;
187	struct tty *tp = NULL;
188	int flags = TOLOG;
189	va_list ap;
190	struct putchar_arg pca;
191
192	logpri(LOG_INFO);
193	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
194		flags |= TOTTY;
195		tp = sess->s_ttyp;
196	}
197	va_start(ap, fmt);
198	pca.tty = tp;
199	pca.flags = flags;
200	kvprintf(fmt, putchar, &pca, 10, ap);
201	va_end(ap);
202	logwakeup();
203}
204
205/*
206 * Ttyprintf displays a message on a tty; it should be used only by
207 * the tty driver, or anything that knows the underlying tty will not
208 * be revoke(2)'d away.  Other callers should use tprintf.
209 */
210void
211ttyprintf(struct tty *tp, const char *fmt, ...)
212{
213	va_list ap;
214	struct putchar_arg pca;
215	va_start(ap, fmt);
216	pca.tty = tp;
217	pca.flags = TOTTY;
218	kvprintf(fmt, putchar, &pca, 10, ap);
219	va_end(ap);
220}
221
222extern	int log_open;
223
224/*
225 * Log writes to the log buffer, and guarantees not to sleep (so can be
226 * called by interrupt routines).  If there is no process reading the
227 * log yet, it writes to the console also.
228 */
229void
230log(int level, const char *fmt, ...)
231{
232	register int s;
233	va_list ap;
234
235	s = splhigh();
236	logpri(level);
237	va_start(ap, fmt);
238
239	kvprintf(fmt, msglogchar, NULL, 10, ap);
240	va_end(ap);
241
242	splx(s);
243	if (!log_open) {
244		struct putchar_arg pca;
245		va_start(ap, fmt);
246		pca.tty = NULL;
247		pca.flags = TOCONS;
248		kvprintf(fmt, putchar, &pca, 10, ap);
249		va_end(ap);
250	}
251	logwakeup();
252}
253
254static void
255logpri(level)
256	int level;
257{
258	register char *p;
259
260	msglogchar('<', NULL);
261	for (p = ksprintn((u_long)level, 10, NULL); *p;)
262		msglogchar(*p--, NULL);
263	msglogchar('>', NULL);
264}
265
266void
267addlog(const char *fmt, ...)
268{
269	register int s;
270	va_list ap;
271
272	s = splhigh();
273	va_start(ap, fmt);
274	kvprintf(fmt, msglogchar, NULL, 10, ap);
275	splx(s);
276	va_end(ap);
277	if (!log_open) {
278		struct putchar_arg pca;
279		va_start(ap, fmt);
280		pca.tty = NULL;
281		pca.flags = TOCONS;
282		kvprintf(fmt, putchar, &pca, 10, ap);
283		va_end(ap);
284	}
285	logwakeup();
286}
287
288void
289printf(const char *fmt, ...)
290{
291	va_list ap;
292	register int savintr;
293	struct putchar_arg pca;
294
295	savintr = consintr;		/* disable interrupts */
296	consintr = 0;
297	va_start(ap, fmt);
298	pca.tty = NULL;
299	pca.flags = TOCONS | TOLOG;
300	kvprintf(fmt, putchar, &pca, 10, ap);
301	va_end(ap);
302	if (!panicstr)
303		logwakeup();
304	consintr = savintr;		/* reenable interrupts */
305}
306
307void
308vprintf(const char *fmt, va_list ap)
309{
310	register int savintr;
311	struct putchar_arg pca;
312
313	savintr = consintr;		/* disable interrupts */
314	consintr = 0;
315	pca.tty = NULL;
316	pca.flags = TOCONS | TOLOG;
317	kvprintf(fmt, putchar, &pca, 10, ap);
318	if (!panicstr)
319		logwakeup();
320	consintr = savintr;		/* reenable interrupts */
321}
322
323/*
324 * Print a character on console or users terminal.  If destination is
325 * the console then the last MSGBUFS characters are saved in msgbuf for
326 * inspection later.
327 */
328static void
329putchar(int c, void *arg)
330{
331	struct putchar_arg *ap = (struct putchar_arg*) arg;
332	int flags = ap->flags;
333	struct tty *tp = ap->tty;
334	if (panicstr)
335		constty = NULL;
336	if ((flags & TOCONS) && tp == NULL && constty) {
337		tp = constty;
338		flags |= TOTTY;
339	}
340	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
341	    (flags & TOCONS) && tp == constty)
342		constty = NULL;
343	if ((flags & TOLOG))
344		msglogchar(c, NULL);
345	if ((flags & TOCONS) && constty == NULL && c != '\0')
346		(*v_putc)(c);
347}
348
349/*
350 * Scaled down version of sprintf(3).
351 */
352int
353sprintf(char *buf, const char *cfmt, ...)
354{
355	int retval;
356	va_list ap;
357
358	va_start(ap, cfmt);
359	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
360	va_end(ap);
361	return retval;
362}
363
364/*
365 * Put a number (base <= 16) in a buffer in reverse order; return an
366 * optional length and a pointer to the NULL terminated (preceded?)
367 * buffer.
368 */
369static char *
370ksprintn(ul, base, lenp)
371	register u_long ul;
372	register int base, *lenp;
373{					/* A long in base 8, plus NULL. */
374	static char buf[sizeof(long) * NBBY / 3 + 2];
375	register char *p;
376
377	p = buf;
378	do {
379		*++p = hex2ascii(ul % base);
380	} while (ul /= base);
381	if (lenp)
382		*lenp = p - buf;
383	return (p);
384}
385
386/*
387 * Scaled down version of printf(3).
388 *
389 * Two additional formats:
390 *
391 * The format %b is supported to decode error registers.
392 * Its usage is:
393 *
394 *	printf("reg=%b\n", regval, "<base><arg>*");
395 *
396 * where <base> is the output base expressed as a control character, e.g.
397 * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
398 * the first of which gives the bit number to be inspected (origin 1), and
399 * the next characters (up to a control character, i.e. a character <= 32),
400 * give the name of the register.  Thus:
401 *
402 *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
403 *
404 * would produce output:
405 *
406 *	reg=3<BITTWO,BITONE>
407 *
408 * The format %r passes an additional format string and argument list
409 * recursively.  Its usage is:
410 *
411 * fn(char *fmt, ...)
412 * {
413 *	va_list ap;
414 *	va_start(ap, fmt);
415 *	printf("prefix: %r: suffix\n", fmt, ap);
416 *	va_end(ap);
417 * }
418 *
419 * Space or zero padding and a field width are supported for the numeric
420 * formats only.
421 */
422int
423kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
424{
425#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
426	char *p, *q, *d;
427	int ch, n;
428	u_long ul;
429	int base, lflag, tmp, width, ladjust, sharpflag, neg, sign;
430	char padc;
431	int retval = 0;
432
433	if (func == NULL)
434		d = (char *) arg;
435	else
436		d = 0;
437
438	if (fmt == NULL)
439		fmt = "(fmt null)\n";
440	for (;;) {
441		padc = ' ';
442		width = 0;
443		while ((ch = *(u_char *)fmt++) != '%') {
444			if (ch == '\0')
445				return retval;
446			PCHAR(ch);
447		}
448		lflag = 0;
449		ladjust = 0;
450		sharpflag = 0;
451		neg = 0;
452		sign = 0;
453reswitch:	switch (ch = *(u_char *)fmt++) {
454		case '#':
455			sharpflag = 1;
456			goto reswitch;
457		case '+':
458			sign = 1;
459			goto reswitch;
460		case '-':
461			ladjust = 1;
462			goto reswitch;
463		case '%':
464			PCHAR(ch);
465			break;
466		case '*':
467			width = va_arg(ap, int);
468			if (width < 0) {
469				ladjust = !ladjust;
470				width = -width;
471			}
472			goto reswitch;
473		case '0':
474			padc = '0';
475			goto reswitch;
476		case '1': case '2': case '3': case '4':
477		case '5': case '6': case '7': case '8': case '9':
478			for (width = 0;; ++fmt) {
479				width = width * 10 + ch - '0';
480				ch = *fmt;
481				if (ch < '0' || ch > '9')
482					break;
483			}
484			goto reswitch;
485		case 'b':
486			ul = va_arg(ap, int);
487			p = va_arg(ap, char *);
488			for (q = ksprintn(ul, *p++, NULL); *q;)
489				PCHAR(*q--);
490
491			if (!ul)
492				break;
493
494			for (tmp = 0; *p;) {
495				n = *p++;
496				if (ul & (1 << (n - 1))) {
497					PCHAR(tmp ? ',' : '<');
498					for (; (n = *p) > ' '; ++p)
499						PCHAR(n);
500					tmp = 1;
501				} else
502					for (; *p > ' '; ++p)
503						continue;
504			}
505			if (tmp)
506				PCHAR('>');
507			break;
508		case 'c':
509			PCHAR(va_arg(ap, int));
510			break;
511		case 'd':
512			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
513			sign = 1;
514			base = 10;
515			goto number;
516		case 'l':
517			lflag = 1;
518			goto reswitch;
519		case 'n':
520			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
521			base = radix;
522			goto number;
523		case 'o':
524			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
525			base = 8;
526			goto number;
527		case 'p':
528			ul = (u_long)va_arg(ap, void *);
529			base = 16;
530			PCHAR('0');
531			PCHAR('x');
532			goto number;
533		case 'r':
534			p = va_arg(ap, char *);
535			n = kvprintf(p, func, arg, radix, ap);
536			if (!func)
537				d += n;
538			retval += n;
539			break;
540		case 's':
541			p = va_arg(ap, char *);
542			if (p == NULL)
543				p = "(null)";
544			width -= strlen (p);
545			if (!ladjust && width > 0)
546				while (width--)
547					PCHAR(padc);
548			while (*p)
549				PCHAR(*p++);
550			if (ladjust && width > 0)
551				while (width--)
552					PCHAR(padc);
553			break;
554		case 'u':
555			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
556			base = 10;
557			goto number;
558		case 'x':
559			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
560			base = 16;
561number:			if (sign && (long)ul < 0L) {
562				neg = 1;
563				ul = -(long)ul;
564			}
565			p = ksprintn(ul, base, &tmp);
566			if (sharpflag && ul != 0) {
567				if (base == 8)
568					tmp++;
569				else if (base == 16)
570					tmp += 2;
571			}
572			if (neg)
573				tmp++;
574
575			if (!ladjust && width && (width -= tmp) > 0)
576				while (width--)
577					PCHAR(padc);
578			if (neg)
579				PCHAR('-');
580			if (sharpflag && ul != 0) {
581				if (base == 8) {
582					PCHAR('0');
583				} else if (base == 16) {
584					PCHAR('0');
585					PCHAR('x');
586				}
587			}
588
589			while (*p)
590				PCHAR(*p--);
591
592			if (ladjust && width && (width -= tmp) > 0)
593				while (width--)
594					PCHAR(padc);
595
596			break;
597		default:
598			PCHAR('%');
599			if (lflag)
600				PCHAR('l');
601			PCHAR(ch);
602			break;
603		}
604	}
605#undef PCHAR
606}
607
608/*
609 * Put character in log buffer.
610 */
611static void
612msglogchar(int c, void *dummyarg)
613{
614	struct msgbuf *mbp;
615
616	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
617		mbp = msgbufp;
618		if (mbp->msg_magic != MSG_MAGIC ||
619		    mbp->msg_bufx >= MSG_BSIZE ||
620		    mbp->msg_bufr >= MSG_BSIZE) {
621			bzero(mbp, sizeof(struct msgbuf));
622			mbp->msg_magic = MSG_MAGIC;
623		}
624		mbp->msg_bufc[mbp->msg_bufx++] = c;
625		if (mbp->msg_bufx >= MSG_BSIZE)
626			mbp->msg_bufx = 0;
627		/* If the buffer is full, keep the most recent data. */
628		if (mbp->msg_bufr == mbp->msg_bufx) {
629			if (++mbp->msg_bufr >= MSG_BSIZE)
630				mbp->msg_bufr = 0;
631		}
632	}
633}
634