hexdump.c revision 49047
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.57 1999/07/14 17:37:53 peter Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/msgbuf.h>
46#include <sys/malloc.h>
47#include <sys/proc.h>
48#include <sys/tty.h>
49#include <sys/tprintf.h>
50#include <sys/syslog.h>
51#include <machine/cons.h>
52
53/*
54 * Note that stdarg.h and the ANSI style va_start macro is used for both
55 * ANSI and traditional C compilers.
56 */
57#include <machine/stdarg.h>
58
59#define TOCONS	0x01
60#define TOTTY	0x02
61#define TOLOG	0x04
62
63/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
64#define MAXNBUF	(sizeof(quad_t) * NBBY + 1)
65
66struct putchar_arg {
67	int	flags;
68	struct	tty *tty;
69};
70
71struct snprintf_arg {
72	char	*str;
73	size_t	remain;
74};
75
76struct	tty *constty;			/* pointer to console "window" tty */
77
78static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
79static void  logpri __P((int level));
80static void  msglogchar(int c, void *dummyarg);
81static void  putchar __P((int ch, void *arg));
82static char *ksprintn __P((char *nbuf, u_long num, int base, int *len));
83static char *ksprintqn __P((char *nbuf, u_quad_t num, int base, int *len));
84static void  snprintf_func __P((int ch, void *arg));
85
86static int consintr = 1;		/* Ok to handle console interrupts? */
87static int msgbufmapped;		/* Set when safe to use msgbuf */
88
89/*
90 * Warn that a system table is full.
91 */
92void
93tablefull(tab)
94	const char *tab;
95{
96
97	log(LOG_ERR, "%s: table is full\n", tab);
98}
99
100/*
101 * Uprintf prints to the controlling terminal for the current process.
102 * It may block if the tty queue is overfull.  No message is printed if
103 * the queue does not clear in a reasonable time.
104 */
105int
106uprintf(const char *fmt, ...)
107{
108	struct proc *p = curproc;
109	va_list ap;
110	struct putchar_arg pca;
111	int retval = 0;
112
113	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
114		va_start(ap, fmt);
115		pca.tty = p->p_session->s_ttyp;
116		pca.flags = TOTTY;
117		retval = kvprintf(fmt, putchar, &pca, 10, ap);
118		va_end(ap);
119	}
120	return retval;
121}
122
123tpr_t
124tprintf_open(p)
125	register struct proc *p;
126{
127
128	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
129		SESSHOLD(p->p_session);
130		return ((tpr_t) p->p_session);
131	}
132	return ((tpr_t) NULL);
133}
134
135void
136tprintf_close(sess)
137	tpr_t sess;
138{
139
140	if (sess)
141		SESSRELE((struct session *) sess);
142}
143
144/*
145 * tprintf prints on the controlling terminal associated
146 * with the given session.
147 */
148int
149tprintf(tpr_t tpr, const char *fmt, ...)
150{
151	register struct session *sess = (struct session *)tpr;
152	struct tty *tp = NULL;
153	int flags = TOLOG;
154	va_list ap;
155	struct putchar_arg pca;
156	int retval;
157
158	logpri(LOG_INFO);
159	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
160		flags |= TOTTY;
161		tp = sess->s_ttyp;
162	}
163	va_start(ap, fmt);
164	pca.tty = tp;
165	pca.flags = flags;
166	retval = kvprintf(fmt, putchar, &pca, 10, ap);
167	va_end(ap);
168	logwakeup();
169	return retval;
170}
171
172/*
173 * Ttyprintf displays a message on a tty; it should be used only by
174 * the tty driver, or anything that knows the underlying tty will not
175 * be revoke(2)'d away.  Other callers should use tprintf.
176 */
177int
178ttyprintf(struct tty *tp, const char *fmt, ...)
179{
180	va_list ap;
181	struct putchar_arg pca;
182	int retval;
183
184	va_start(ap, fmt);
185	pca.tty = tp;
186	pca.flags = TOTTY;
187	retval = kvprintf(fmt, putchar, &pca, 10, ap);
188	va_end(ap);
189	return retval;
190}
191
192extern	int log_open;
193
194/*
195 * Log writes to the log buffer, and guarantees not to sleep (so can be
196 * called by interrupt routines).  If there is no process reading the
197 * log yet, it writes to the console also.
198 */
199int
200log(int level, const char *fmt, ...)
201{
202	register int s;
203	va_list ap;
204	int retval;
205
206	s = splhigh();
207	logpri(level);
208	va_start(ap, fmt);
209
210	retval = kvprintf(fmt, msglogchar, NULL, 10, ap);
211	va_end(ap);
212
213	splx(s);
214	if (!log_open) {
215		struct putchar_arg pca;
216		va_start(ap, fmt);
217		pca.tty = NULL;
218		pca.flags = TOCONS;
219		retval += kvprintf(fmt, putchar, &pca, 10, ap);
220		va_end(ap);
221	}
222	logwakeup();
223	return retval;
224}
225
226static void
227logpri(level)
228	int level;
229{
230	char nbuf[MAXNBUF];
231	register char *p;
232
233	msglogchar('<', NULL);
234	for (p = ksprintn(nbuf, (u_long)level, 10, NULL); *p;)
235		msglogchar(*p--, NULL);
236	msglogchar('>', NULL);
237}
238
239int
240addlog(const char *fmt, ...)
241{
242	register int s;
243	va_list ap;
244	int retval;
245
246	s = splhigh();
247	va_start(ap, fmt);
248	retval = kvprintf(fmt, msglogchar, NULL, 10, ap);
249	splx(s);
250	va_end(ap);
251	if (!log_open) {
252		struct putchar_arg pca;
253		va_start(ap, fmt);
254		pca.tty = NULL;
255		pca.flags = TOCONS;
256		retval += kvprintf(fmt, putchar, &pca, 10, ap);
257		va_end(ap);
258	}
259	logwakeup();
260	return (retval);
261}
262
263int
264printf(const char *fmt, ...)
265{
266	va_list ap;
267	register int savintr;
268	struct putchar_arg pca;
269	int retval;
270
271	savintr = consintr;		/* disable interrupts */
272	consintr = 0;
273	va_start(ap, fmt);
274	pca.tty = NULL;
275	pca.flags = TOCONS | TOLOG;
276	retval = kvprintf(fmt, putchar, &pca, 10, ap);
277	va_end(ap);
278	if (!panicstr)
279		logwakeup();
280	consintr = savintr;		/* reenable interrupts */
281	return retval;
282}
283
284int
285vprintf(const char *fmt, va_list ap)
286{
287	register int savintr;
288	struct putchar_arg pca;
289	int retval;
290
291	savintr = consintr;		/* disable interrupts */
292	consintr = 0;
293	pca.tty = NULL;
294	pca.flags = TOCONS | TOLOG;
295	retval = kvprintf(fmt, putchar, &pca, 10, ap);
296	if (!panicstr)
297		logwakeup();
298	consintr = savintr;		/* reenable interrupts */
299	return retval;
300}
301
302/*
303 * Print a character on console or users terminal.  If destination is
304 * the console then the last bunch of characters are saved in msgbuf for
305 * inspection later.
306 */
307static void
308putchar(int c, void *arg)
309{
310	struct putchar_arg *ap = (struct putchar_arg*) arg;
311	int flags = ap->flags;
312	struct tty *tp = ap->tty;
313	if (panicstr)
314		constty = NULL;
315	if ((flags & TOCONS) && tp == NULL && constty) {
316		tp = constty;
317		flags |= TOTTY;
318	}
319	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
320	    (flags & TOCONS) && tp == constty)
321		constty = NULL;
322	if ((flags & TOLOG))
323		msglogchar(c, NULL);
324	if ((flags & TOCONS) && constty == NULL && c != '\0')
325		(*v_putc)(c);
326}
327
328/*
329 * Scaled down version of sprintf(3).
330 */
331int
332sprintf(char *buf, const char *cfmt, ...)
333{
334	int retval;
335	va_list ap;
336
337	va_start(ap, cfmt);
338	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
339	buf[retval] = '\0';
340	va_end(ap);
341	return retval;
342}
343
344/*
345 * Scaled down version of vsprintf(3).
346 */
347int
348vsprintf(char *buf, const char *cfmt, va_list ap)
349{
350	int retval;
351
352	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
353	buf[retval] = '\0';
354	return retval;
355}
356
357/*
358 * Scaled down version of snprintf(3).
359 */
360int
361snprintf(char *str, size_t size, const char *format, ...)
362{
363	int retval;
364	va_list ap;
365
366	va_start(ap, format);
367	retval = vsnprintf(str, size, format, ap);
368	va_end(ap);
369	return(retval);
370}
371
372/*
373 * Scaled down version of vsnprintf(3).
374 */
375int
376vsnprintf(char *str, size_t size, const char *format, va_list ap)
377{
378	struct snprintf_arg info;
379	int retval;
380
381	info.str = str;
382	info.remain = size;
383	retval = kvprintf(format, snprintf_func, &info, 10, ap);
384	if (info.remain >= 1)
385		*info.str++ = '\0';
386	return retval;
387}
388
389static void
390snprintf_func(int ch, void *arg)
391{
392	struct snprintf_arg *const info = arg;
393
394	if (info->remain >= 2) {
395		*info->str++ = ch;
396		info->remain--;
397	}
398}
399
400/*
401 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
402 * order; return an optional length and a pointer to the last character
403 * written in the buffer (i.e., the first character of the string).
404 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
405 */
406static char *
407ksprintn(nbuf, ul, base, lenp)
408	char *nbuf;
409	register u_long ul;
410	register int base, *lenp;
411{
412	register char *p;
413
414	p = nbuf;
415	*p = '\0';
416	do {
417		*++p = hex2ascii(ul % base);
418	} while (ul /= base);
419	if (lenp)
420		*lenp = p - nbuf;
421	return (p);
422}
423/* ksprintn, but for a quad_t. */
424static char *
425ksprintqn(nbuf, uq, base, lenp)
426	char *nbuf;
427	register u_quad_t uq;
428	register int base, *lenp;
429{
430	register char *p;
431
432	p = nbuf;
433	*p = '\0';
434	do {
435		*++p = hex2ascii(uq % base);
436	} while (uq /= base);
437	if (lenp)
438		*lenp = p - nbuf;
439	return (p);
440}
441
442/*
443 * Scaled down version of printf(3).
444 *
445 * Two additional formats:
446 *
447 * The format %b is supported to decode error registers.
448 * Its usage is:
449 *
450 *	printf("reg=%b\n", regval, "<base><arg>*");
451 *
452 * where <base> is the output base expressed as a control character, e.g.
453 * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
454 * the first of which gives the bit number to be inspected (origin 1), and
455 * the next characters (up to a control character, i.e. a character <= 32),
456 * give the name of the register.  Thus:
457 *
458 *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
459 *
460 * would produce output:
461 *
462 *	reg=3<BITTWO,BITONE>
463 *
464 * XXX:  %D  -- Hexdump, takes pointer and separator string:
465 *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
466 *		("%*D", len, ptr, " " -> XX XX XX XX ...
467 */
468int
469kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
470{
471#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
472	char nbuf[MAXNBUF];
473	char *p, *q, *d;
474	u_char *up;
475	int ch, n;
476	u_long ul;
477	u_quad_t uq;
478	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
479	int dwidth;
480	char padc;
481	int retval = 0;
482
483	ul = 0;
484	uq = 0;
485	if (!func)
486		d = (char *) arg;
487	else
488		d = NULL;
489
490	if (fmt == NULL)
491		fmt = "(fmt null)\n";
492
493	if (radix < 2 || radix > 36)
494		radix = 10;
495
496	for (;;) {
497		padc = ' ';
498		width = 0;
499		while ((ch = (u_char)*fmt++) != '%') {
500			if (ch == '\0')
501				return retval;
502			PCHAR(ch);
503		}
504		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
505		sign = 0; dot = 0; dwidth = 0;
506reswitch:	switch (ch = (u_char)*fmt++) {
507		case '.':
508			dot = 1;
509			goto reswitch;
510		case '#':
511			sharpflag = 1;
512			goto reswitch;
513		case '+':
514			sign = 1;
515			goto reswitch;
516		case '-':
517			ladjust = 1;
518			goto reswitch;
519		case '%':
520			PCHAR(ch);
521			break;
522		case '*':
523			if (!dot) {
524				width = va_arg(ap, int);
525				if (width < 0) {
526					ladjust = !ladjust;
527					width = -width;
528				}
529			} else {
530				dwidth = va_arg(ap, int);
531			}
532			goto reswitch;
533		case '0':
534			if (!dot) {
535				padc = '0';
536				goto reswitch;
537			}
538		case '1': case '2': case '3': case '4':
539		case '5': case '6': case '7': case '8': case '9':
540				for (n = 0;; ++fmt) {
541					n = n * 10 + ch - '0';
542					ch = *fmt;
543					if (ch < '0' || ch > '9')
544						break;
545				}
546			if (dot)
547				dwidth = n;
548			else
549				width = n;
550			goto reswitch;
551		case 'b':
552			ul = va_arg(ap, int);
553			p = va_arg(ap, char *);
554			for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
555				PCHAR(*q--);
556
557			if (!ul)
558				break;
559
560			for (tmp = 0; *p;) {
561				n = *p++;
562				if (ul & (1 << (n - 1))) {
563					PCHAR(tmp ? ',' : '<');
564					for (; (n = *p) > ' '; ++p)
565						PCHAR(n);
566					tmp = 1;
567				} else
568					for (; *p > ' '; ++p)
569						continue;
570			}
571			if (tmp)
572				PCHAR('>');
573			break;
574		case 'c':
575			PCHAR(va_arg(ap, int));
576			break;
577		case 'D':
578			up = va_arg(ap, u_char *);
579			p = va_arg(ap, char *);
580			if (!width)
581				width = 16;
582			while(width--) {
583				PCHAR(hex2ascii(*up >> 4));
584				PCHAR(hex2ascii(*up & 0x0f));
585				up++;
586				if (width)
587					for (q=p;*q;q++)
588						PCHAR(*q);
589			}
590			break;
591		case 'd':
592			if (qflag)
593				uq = va_arg(ap, quad_t);
594			else if (lflag)
595				ul = va_arg(ap, long);
596			else
597				ul = va_arg(ap, int);
598			sign = 1;
599			base = 10;
600			goto number;
601		case 'l':
602			lflag = 1;
603			goto reswitch;
604		case 'o':
605			if (qflag)
606				uq = va_arg(ap, u_quad_t);
607			else if (lflag)
608				ul = va_arg(ap, u_long);
609			else
610				ul = va_arg(ap, u_int);
611			base = 8;
612			goto nosign;
613		case 'p':
614			ul = (uintptr_t)va_arg(ap, void *);
615			base = 16;
616			sharpflag = (width == 0);
617			goto nosign;
618		case 'q':
619			qflag = 1;
620			goto reswitch;
621		case 'n':
622		case 'r':
623			if (qflag)
624				uq = va_arg(ap, u_quad_t);
625			else if (lflag)
626				ul = va_arg(ap, u_long);
627			else
628				ul = sign ?
629				    (u_long)va_arg(ap, int) : va_arg(ap, u_int);
630			base = radix;
631			goto number;
632		case 's':
633			p = va_arg(ap, char *);
634			if (p == NULL)
635				p = "(null)";
636			if (!dot)
637				n = strlen (p);
638			else
639				for (n = 0; n < dwidth && p[n]; n++)
640					continue;
641
642			width -= n;
643
644			if (!ladjust && width > 0)
645				while (width--)
646					PCHAR(padc);
647			while (n--)
648				PCHAR(*p++);
649			if (ladjust && width > 0)
650				while (width--)
651					PCHAR(padc);
652			break;
653		case 'u':
654			if (qflag)
655				uq = va_arg(ap, u_quad_t);
656			else if (lflag)
657				ul = va_arg(ap, u_long);
658			else
659				ul = va_arg(ap, u_int);
660			base = 10;
661			goto nosign;
662		case 'x':
663			if (qflag)
664				uq = va_arg(ap, u_quad_t);
665			else if (lflag)
666				ul = va_arg(ap, u_long);
667			else
668				ul = va_arg(ap, u_int);
669			base = 16;
670			goto nosign;
671		case 'z':
672			if (qflag)
673				uq = va_arg(ap, u_quad_t);
674			else if (lflag)
675				ul = va_arg(ap, u_long);
676			else
677				ul = sign ?
678				    (u_long)va_arg(ap, int) : va_arg(ap, u_int);
679			base = 16;
680			goto number;
681nosign:			sign = 0;
682number:
683			if (qflag) {
684				if (sign && (quad_t)uq < 0) {
685					neg = 1;
686					uq = -(quad_t)uq;
687				}
688				p = ksprintqn(nbuf, uq, base, &tmp);
689			} else {
690				if (sign && (long)ul < 0) {
691					neg = 1;
692					ul = -(long)ul;
693				}
694				p = ksprintn(nbuf, ul, base, &tmp);
695			}
696			if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
697				if (base == 8)
698					tmp++;
699				else if (base == 16)
700					tmp += 2;
701			}
702			if (neg)
703				tmp++;
704
705			if (!ladjust && width && (width -= tmp) > 0)
706				while (width--)
707					PCHAR(padc);
708			if (neg)
709				PCHAR('-');
710			if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
711				if (base == 8) {
712					PCHAR('0');
713				} else if (base == 16) {
714					PCHAR('0');
715					PCHAR('x');
716				}
717			}
718
719			while (*p)
720				PCHAR(*p--);
721
722			if (ladjust && width && (width -= tmp) > 0)
723				while (width--)
724					PCHAR(padc);
725
726			break;
727		default:
728			PCHAR('%');
729			if (lflag)
730				PCHAR('l');
731			PCHAR(ch);
732			break;
733		}
734	}
735#undef PCHAR
736}
737
738/*
739 * Put character in log buffer.
740 */
741static void
742msglogchar(int c, void *dummyarg)
743{
744	struct msgbuf *mbp;
745
746	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
747		mbp = msgbufp;
748		mbp->msg_ptr[mbp->msg_bufx++] = c;
749		if (mbp->msg_bufx >= mbp->msg_size)
750			mbp->msg_bufx = 0;
751		/* If the buffer is full, keep the most recent data. */
752		if (mbp->msg_bufr == mbp->msg_bufx) {
753			if (++mbp->msg_bufr >= mbp->msg_size)
754				mbp->msg_bufr = 0;
755		}
756	}
757}
758
759static void
760msgbufcopy(struct msgbuf *oldp)
761{
762	int pos;
763
764	pos = oldp->msg_bufr;
765	while (pos != oldp->msg_bufx) {
766		msglogchar(oldp->msg_ptr[pos], NULL);
767		if (++pos >= oldp->msg_size)
768			pos = 0;
769	}
770}
771
772void
773msgbufinit(void *ptr, size_t size)
774{
775	char *cp;
776	static struct msgbuf *oldp = NULL;
777
778	cp = (char *)ptr;
779	msgbufp = (struct msgbuf *) (cp + size - sizeof(*msgbufp));
780	if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_ptr != cp) {
781		bzero(cp, size);
782		msgbufp->msg_magic = MSG_MAGIC;
783		msgbufp->msg_size = (char *)msgbufp - cp;
784		msgbufp->msg_ptr = cp;
785	}
786	if (msgbufmapped && oldp != msgbufp)
787		msgbufcopy(oldp);
788	msgbufmapped = 1;
789	oldp = msgbufp;
790}
791
792#include "opt_ddb.h"
793#ifdef DDB
794#include <ddb/ddb.h>
795
796DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
797{
798	int i, j;
799
800	if (!msgbufmapped) {
801		db_printf("msgbuf not mapped yet\n");
802		return;
803	}
804	db_printf("msgbufp = %p\n", msgbufp);
805	db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
806	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
807	    msgbufp->msg_bufx, msgbufp->msg_ptr);
808	for (i = 0; i < msgbufp->msg_size; i++) {
809		j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
810		db_printf("%c", msgbufp->msg_ptr[j]);
811	}
812	db_printf("\n");
813}
814
815#endif /* DDB */
816