hexdump.c revision 14771
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.31 1996/03/23 11:31:16 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 <sys/kernel.h> 55#include <sys/sysctl.h> 56#include <machine/cons.h> 57 58/* 59 * Note that stdarg.h and the ANSI style va_start macro is used for both 60 * ANSI and traditional C compilers. 61 */ 62#include <machine/stdarg.h> 63 64#if defined(DDB) || defined (KGDB) 65#ifdef DDB_UNATTENDED 66 static int debugger_on_panic = 0; 67#else 68 static int debugger_on_panic = 1; 69#endif 70 71SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 72 &debugger_on_panic, 0, ""); 73#endif 74 75#define TOCONS 0x01 76#define TOTTY 0x02 77#define TOLOG 0x04 78 79struct tty *constty; /* pointer to console "window" tty */ 80 81static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */ 82 83static void logpri __P((int level)); 84static void msglogchar(int c, void *dummyarg); 85struct putchar_arg {int flags; struct tty *tty; }; 86static void putchar __P((int ch, void *arg)); 87static char *ksprintn __P((u_long num, int base, int *len)); 88 89static int consintr = 1; /* Ok to handle console interrupts? */ 90 91/* 92 * Variable panicstr contains argument to first call to panic; used as flag 93 * to indicate that the kernel has already called panic. 94 */ 95const char *panicstr; 96 97/* 98 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 99 * and then reboots. If we are called twice, then we avoid trying to sync 100 * the disks as this often leads to recursive panics. 101 */ 102#ifdef __GNUC__ 103__dead /* panic() does not return */ 104#endif 105void 106panic(const char *fmt, ...) 107{ 108 int bootopt; 109 va_list ap; 110 111 bootopt = RB_AUTOBOOT | RB_DUMP; 112 if (panicstr) 113 bootopt |= RB_NOSYNC; 114 else 115 panicstr = fmt; 116 117 printf("panic: "); 118 va_start(ap, fmt); 119 vprintf(fmt, ap); 120 va_end(ap); 121 printf("\n"); 122 123#ifdef KGDB 124 if (debugger_on_panic) { 125 kgdb_panic(); 126#endif 127#ifdef DDB 128 Debugger ("panic"); 129#endif 130 } 131 boot(bootopt); 132} 133 134/* 135 * Warn that a system table is full. 136 */ 137void 138tablefull(tab) 139 const char *tab; 140{ 141 142 log(LOG_ERR, "%s: table is full\n", tab); 143} 144 145/* 146 * Uprintf prints to the controlling terminal for the current process. 147 * It may block if the tty queue is overfull. No message is printed if 148 * the queue does not clear in a reasonable time. 149 */ 150void 151uprintf(const char *fmt, ...) 152{ 153 struct proc *p = curproc; 154 va_list ap; 155 struct putchar_arg pca; 156 157 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 158 va_start(ap, fmt); 159 pca.tty = p->p_session->s_ttyp; 160 pca.flags = TOTTY; 161 kvprintf(fmt, putchar, &pca, 10, ap); 162 va_end(ap); 163 } 164} 165 166tpr_t 167tprintf_open(p) 168 register struct proc *p; 169{ 170 171 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 172 SESSHOLD(p->p_session); 173 return ((tpr_t) p->p_session); 174 } 175 return ((tpr_t) NULL); 176} 177 178void 179tprintf_close(sess) 180 tpr_t sess; 181{ 182 183 if (sess) 184 SESSRELE((struct session *) sess); 185} 186 187/* 188 * tprintf prints on the controlling terminal associated 189 * with the given session. 190 */ 191void 192tprintf(tpr_t tpr, const char *fmt, ...) 193{ 194 register struct session *sess = (struct session *)tpr; 195 struct tty *tp = NULL; 196 int flags = TOLOG; 197 va_list ap; 198 struct putchar_arg pca; 199 200 logpri(LOG_INFO); 201 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 202 flags |= TOTTY; 203 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