hexdump.c revision 13480
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.22 1996/01/16 18:08:57 phk 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, dot;
430	int dwidth;
431	char padc;
432	int retval = 0;
433
434	if (func == NULL)
435		d = (char *) arg;
436	else
437		d = 0;
438
439	if (fmt == NULL)
440		fmt = "(fmt null)\n";
441	for (;;) {
442		padc = ' ';
443		width = 0;
444		while ((ch = *(u_char *)fmt++) != '%') {
445			if (ch == '\0')
446				return retval;
447			PCHAR(ch);
448		}
449		lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
450		sign = 0; dot = 0; dwidth = 0;
451reswitch:	switch (ch = *(u_char *)fmt++) {
452		case '.':
453			dot = 1;
454			goto reswitch;
455		case '#':
456			sharpflag = 1;
457			goto reswitch;
458		case '+':
459			sign = 1;
460			goto reswitch;
461		case '-':
462			ladjust = 1;
463			goto reswitch;
464		case '%':
465			PCHAR(ch);
466			break;
467		case '*':
468			width = va_arg(ap, int);
469			if (width < 0) {
470				ladjust = !ladjust;
471				width = -width;
472			}
473			goto reswitch;
474		case '0':
475			if (!dot) {
476				padc = '0';
477				goto reswitch;
478			}
479		case '1': case '2': case '3': case '4':
480		case '5': case '6': case '7': case '8': case '9':
481				for (n = 0;; ++fmt) {
482					n = n * 10 + ch - '0';
483					ch = *fmt;
484					if (ch < '0' || ch > '9')
485						break;
486				}
487			if (dot)
488				dwidth = n;
489			else
490				width = n;
491			goto reswitch;
492		case 'b':
493			ul = va_arg(ap, int);
494			p = va_arg(ap, char *);
495			for (q = ksprintn(ul, *p++, NULL); *q;)
496				PCHAR(*q--);
497
498			if (!ul)
499				break;
500
501			for (tmp = 0; *p;) {
502				n = *p++;
503				if (ul & (1 << (n - 1))) {
504					PCHAR(tmp ? ',' : '<');
505					for (; (n = *p) > ' '; ++p)
506						PCHAR(n);
507					tmp = 1;
508				} else
509					for (; *p > ' '; ++p)
510						continue;
511			}
512			if (tmp)
513				PCHAR('>');
514			break;
515		case 'c':
516			PCHAR(va_arg(ap, int));
517			break;
518		case 'd':
519			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
520			sign = 1;
521			base = 10;
522			goto number;
523		case 'l':
524			lflag = 1;
525			goto reswitch;
526		case 'n':
527			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
528			base = radix;
529			goto number;
530		case 'o':
531			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
532			base = 8;
533			goto number;
534		case 'p':
535			ul = (u_long)va_arg(ap, void *);
536			base = 16;
537			PCHAR('0');
538			PCHAR('x');
539			goto number;
540		case 'r':
541			p = va_arg(ap, char *);
542			n = kvprintf(p, func, arg, radix, ap);
543			if (!func)
544				d += n;
545			retval += n;
546			break;
547		case 's':
548			p = va_arg(ap, char *);
549			if (p == NULL)
550				p = "(null)";
551			if (!dot)
552				n = strlen (p);
553			else
554				for (n = 0; n < dwidth && p[n]; n++)
555					continue;
556
557			width -= n;
558
559			if (!ladjust && width > 0)
560				while (width--)
561					PCHAR(padc);
562			while (n--)
563				PCHAR(*p++);
564			if (ladjust && width > 0)
565				while (width--)
566					PCHAR(padc);
567			break;
568		case 'u':
569			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
570			base = 10;
571			goto number;
572		case 'x':
573			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
574			base = 16;
575number:			if (sign && (long)ul < 0L) {
576				neg = 1;
577				ul = -(long)ul;
578			}
579			p = ksprintn(ul, base, &tmp);
580			if (sharpflag && ul != 0) {
581				if (base == 8)
582					tmp++;
583				else if (base == 16)
584					tmp += 2;
585			}
586			if (neg)
587				tmp++;
588
589			if (!ladjust && width && (width -= tmp) > 0)
590				while (width--)
591					PCHAR(padc);
592			if (neg)
593				PCHAR('-');
594			if (sharpflag && ul != 0) {
595				if (base == 8) {
596					PCHAR('0');
597				} else if (base == 16) {
598					PCHAR('0');
599					PCHAR('x');
600				}
601			}
602
603			while (*p)
604				PCHAR(*p--);
605
606			if (ladjust && width && (width -= tmp) > 0)
607				while (width--)
608					PCHAR(padc);
609
610			break;
611		default:
612			PCHAR('%');
613			if (lflag)
614				PCHAR('l');
615			PCHAR(ch);
616			break;
617		}
618	}
619#undef PCHAR
620}
621
622/*
623 * Put character in log buffer.
624 */
625static void
626msglogchar(int c, void *dummyarg)
627{
628	struct msgbuf *mbp;
629
630	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
631		mbp = msgbufp;
632		if (mbp->msg_magic != MSG_MAGIC ||
633		    mbp->msg_bufx >= MSG_BSIZE ||
634		    mbp->msg_bufr >= MSG_BSIZE) {
635			bzero(mbp, sizeof(struct msgbuf));
636			mbp->msg_magic = MSG_MAGIC;
637		}
638		mbp->msg_bufc[mbp->msg_bufx++] = c;
639		if (mbp->msg_bufx >= MSG_BSIZE)
640			mbp->msg_bufx = 0;
641		/* If the buffer is full, keep the most recent data. */
642		if (mbp->msg_bufr == mbp->msg_bufx) {
643			if (++mbp->msg_bufr >= MSG_BSIZE)
644				mbp->msg_bufr = 0;
645		}
646	}
647}
648