hexdump.c revision 15700
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.36 1996/05/08 04:28:51 gpalmer 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) 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#if defined(DDB) 124 if (debugger_on_panic) 125 Debugger ("panic"); 126#endif 127 boot(bootopt); 128} 129 130/* 131 * Warn that a system table is full. 132 */ 133void 134tablefull(tab) 135 const char *tab; 136{ 137 138 log(LOG_ERR, "%s: table is full\n", tab); 139} 140 141/* 142 * Uprintf prints to the controlling terminal for the current process. 143 * It may block if the tty queue is overfull. No message is printed if 144 * the queue does not clear in a reasonable time. 145 */ 146void 147uprintf(const char *fmt, ...) 148{ 149 struct proc *p = curproc; 150 va_list ap; 151 struct putchar_arg pca; 152 153 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 154 va_start(ap, fmt); 155 pca.tty = p->p_session->s_ttyp; 156 pca.flags = TOTTY; 157 kvprintf(fmt, putchar, &pca, 10, ap); 158 va_end(ap); 159 } 160} 161 162tpr_t 163tprintf_open(p) 164 register struct proc *p; 165{ 166 167 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 168 SESSHOLD(p->p_session); 169 return ((tpr_t) p->p_session); 170 } 171 return ((tpr_t) NULL); 172} 173 174void 175tprintf_close(sess) 176 tpr_t sess; 177{ 178 179 if (sess) 180 SESSRELE((struct session *) sess); 181} 182 183/* 184 * tprintf prints on the controlling terminal associated 185 * with the given session. 186 */ 187void 188tprintf(tpr_t tpr, const char *fmt, ...) 189{ 190 register struct session *sess = (struct session *)tpr; 191 struct tty *tp = NULL; 192 int flags = TOLOG; 193 va_list ap; 194 struct putchar_arg pca; 195 196 logpri(LOG_INFO); 197 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 198 flags |= TOTTY; 199 tp = sess->s_ttyp; 200 } 201 va_start(ap, fmt); 202 pca.tty = tp; 203 pca.flags = flags; 204 kvprintf(fmt, putchar, &pca, 10, ap); 205 va_end(ap); 206 logwakeup(); 207} 208 209/* 210 * Ttyprintf displays a message on a tty; it should be used only by 211 * the tty driver, or anything that knows the underlying tty will not 212 * be revoke(2)'d away. Other callers should use tprintf. 213 */ 214void 215ttyprintf(struct tty *tp, const char *fmt, ...) 216{ 217 va_list ap; 218 struct putchar_arg pca; 219 va_start(ap, fmt); 220 pca.tty = tp; 221 pca.flags = TOTTY; 222 kvprintf(fmt, putchar, &pca, 10, ap); 223 va_end(ap); 224} 225 226extern int log_open; 227 228/* 229 * Log writes to the log buffer, and guarantees not to sleep (so can be 230 * called by interrupt routines). If there is no process reading the 231 * log yet, it writes to the console also. 232 */ 233void 234log(int level, const char *fmt, ...) 235{ 236 register int s; 237 va_list ap; 238 239 s = splhigh(); 240 logpri(level); 241 va_start(ap, fmt); 242 243 kvprintf(fmt, msglogchar, NULL, 10, ap); 244 va_end(ap); 245 246 splx(s); 247 if (!log_open) { 248 struct putchar_arg pca; 249 va_start(ap, fmt); 250 pca.tty = NULL; 251 pca.flags = TOCONS; 252 kvprintf(fmt, putchar, &pca, 10, ap); 253 va_end(ap); 254 } 255 logwakeup(); 256} 257 258static void 259logpri(level) 260 int level; 261{ 262 register char *p; 263 264 msglogchar('<', NULL); 265 for (p = ksprintn((u_long)level, 10, NULL); *p;) 266 msglogchar(*p--, NULL); 267 msglogchar('>', NULL); 268} 269 270int 271addlog(const char *fmt, ...) 272{ 273 register int s; 274 va_list ap; 275 int retval; 276 277 s = splhigh(); 278 va_start(ap, fmt); 279 retval = kvprintf(fmt, msglogchar, NULL, 10, ap); 280 splx(s); 281 va_end(ap); 282 if (!log_open) { 283 struct putchar_arg pca; 284 va_start(ap, fmt); 285 pca.tty = NULL; 286 pca.flags = TOCONS; 287 kvprintf(fmt, putchar, &pca, 10, ap); 288 va_end(ap); 289 } 290 logwakeup(); 291 return (retval); 292} 293 294int 295printf(const char *fmt, ...) 296{ 297 va_list ap; 298 register int savintr; 299 struct putchar_arg pca; 300 int retval; 301 302 savintr = consintr; /* disable interrupts */ 303 consintr = 0; 304 va_start(ap, fmt); 305 pca.tty = NULL; 306 pca.flags = TOCONS | TOLOG; 307 retval = kvprintf(fmt, putchar, &pca, 10, ap); 308 va_end(ap); 309 if (!panicstr) 310 logwakeup(); 311 consintr = savintr; /* reenable interrupts */ 312 return retval; 313} 314 315void 316vprintf(const char *fmt, va_list ap) 317{ 318 register int savintr; 319 struct putchar_arg pca; 320 321 savintr = consintr; /* disable interrupts */ 322 consintr = 0; 323 pca.tty = NULL; 324 pca.flags = TOCONS | TOLOG; 325 kvprintf(fmt, putchar, &pca, 10, ap); 326 if (!panicstr) 327 logwakeup(); 328 consintr = savintr; /* reenable interrupts */ 329} 330 331/* 332 * Print a character on console or users terminal. If destination is 333 * the console then the last MSGBUFS characters are saved in msgbuf for 334 * inspection later. 335 */ 336static void 337putchar(int c, void *arg) 338{ 339 struct putchar_arg *ap = (struct putchar_arg*) arg; 340 int flags = ap->flags; 341 struct tty *tp = ap->tty; 342 if (panicstr) 343 constty = NULL; 344 if ((flags & TOCONS) && tp == NULL && constty) { 345 tp = constty; 346 flags |= TOTTY; 347 } 348 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 349 (flags & TOCONS) && tp == constty) 350 constty = NULL; 351 if ((flags & TOLOG)) 352 msglogchar(c, NULL); 353 if ((flags & TOCONS) && constty == NULL && c != '\0') 354 (*v_putc)(c); 355} 356 357/* 358 * Scaled down version of sprintf(3). 359 */ 360int 361sprintf(char *buf, const char *cfmt, ...) 362{ 363 int retval; 364 va_list ap; 365 366 va_start(ap, cfmt); 367 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 368 buf[retval] = '\0'; 369 va_end(ap); 370 return retval; 371} 372 373/* 374 * Put a number (base <= 16) in a buffer in reverse order; return an 375 * optional length and a pointer to the NULL terminated (preceded?) 376 * buffer. 377 */ 378static char * 379ksprintn(ul, base, lenp) 380 register u_long ul; 381 register int base, *lenp; 382{ /* A long in base 8, plus NULL. */ 383 static char buf[sizeof(long) * NBBY / 3 + 2]; 384 register char *p; 385 386 p = buf; 387 do { 388 *++p = hex2ascii(ul % base); 389 } while (ul /= base); 390 if (lenp) 391 *lenp = p - buf; 392 return (p); 393} 394 395/* 396 * Scaled down version of printf(3). 397 * 398 * Two additional formats: 399 * 400 * The format %b is supported to decode error registers. 401 * Its usage is: 402 * 403 * printf("reg=%b\n", regval, "<base><arg>*"); 404 * 405 * where <base> is the output base expressed as a control character, e.g. 406 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 407 * the first of which gives the bit number to be inspected (origin 1), and 408 * the next characters (up to a control character, i.e. a character <= 32), 409 * give the name of the register. Thus: 410 * 411 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 412 * 413 * would produce output: 414 * 415 * reg=3<BITTWO,BITONE> 416 * 417 * XXX: %D -- Hexdump, takes pointer and separator string: 418 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 419 * ("%*D", len, ptr, " " -> XX XX XX XX ... 420 */ 421int 422kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 423{ 424#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 425 char *p, *q, *d; 426 u_char *up; 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) 435 d = (char *) arg; 436 else 437 d = NULL; 438 439 if (fmt == NULL) 440 fmt = "(fmt null)\n"; 441 442 if (radix < 2 || radix > 36) 443 radix = 10; 444 445 for (;;) { 446 padc = ' '; 447 width = 0; 448 while ((ch = *(u_char *)fmt++) != '%') { 449 if (ch == '\0') 450 return retval; 451 PCHAR(ch); 452 } 453 lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 454 sign = 0; dot = 0; dwidth = 0; 455reswitch: switch (ch = *(u_char *)fmt++) { 456 case '.': 457 dot = 1; 458 goto reswitch; 459 case '#': 460 sharpflag = 1; 461 goto reswitch; 462 case '+': 463 sign = 1; 464 goto reswitch; 465 case '-': 466 ladjust = 1; 467 goto reswitch; 468 case '%': 469 PCHAR(ch); 470 break; 471 case '*': 472 if (!dot) { 473 width = va_arg(ap, int); 474 if (width < 0) { 475 ladjust = !ladjust; 476 width = -width; 477 } 478 } else { 479 dwidth = va_arg(ap, int); 480 } 481 goto reswitch; 482 case '0': 483 if (!dot) { 484 padc = '0'; 485 goto reswitch; 486 } 487 case '1': case '2': case '3': case '4': 488 case '5': case '6': case '7': case '8': case '9': 489 for (n = 0;; ++fmt) { 490 n = n * 10 + ch - '0'; 491 ch = *fmt; 492 if (ch < '0' || ch > '9') 493 break; 494 } 495 if (dot) 496 dwidth = n; 497 else 498 width = n; 499 goto reswitch; 500 case 'b': 501 ul = va_arg(ap, int); 502 p = va_arg(ap, char *); 503 for (q = ksprintn(ul, *p++, NULL); *q;) 504 PCHAR(*q--); 505 506 if (!ul) 507 break; 508 509 for (tmp = 0; *p;) { 510 n = *p++; 511 if (ul & (1 << (n - 1))) { 512 PCHAR(tmp ? ',' : '<'); 513 for (; (n = *p) > ' '; ++p) 514 PCHAR(n); 515 tmp = 1; 516 } else 517 for (; *p > ' '; ++p) 518 continue; 519 } 520 if (tmp) 521 PCHAR('>'); 522 break; 523 case 'c': 524 PCHAR(va_arg(ap, int)); 525 break; 526 case 'D': 527 up = va_arg(ap, u_char *); 528 p = va_arg(ap, char *); 529 if (!width) 530 width = 16; 531 while(width--) { 532 PCHAR(hex2ascii(*up >> 4)); 533 PCHAR(hex2ascii(*up & 0x0f)); 534 up++; 535 if (width) 536 for (q=p;*q;q++) 537 PCHAR(*q); 538 } 539 break; 540 case 'd': 541 ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 542 sign = 1; 543 base = 10; 544 goto number; 545 case 'l': 546 lflag = 1; 547 goto reswitch; 548 case 'n': 549 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 550 base = radix; 551 goto number; 552 case 'o': 553 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 554 base = 8; 555 goto number; 556 case 'p': 557 ul = (u_long)va_arg(ap, void *); 558 base = 16; 559 PCHAR('0'); 560 PCHAR('x'); 561 goto number; 562 case 's': 563 p = va_arg(ap, char *); 564 if (p == NULL) 565 p = "(null)"; 566 if (!dot) 567 n = strlen (p); 568 else 569 for (n = 0; n < dwidth && p[n]; n++) 570 continue; 571 572 width -= n; 573 574 if (!ladjust && width > 0) 575 while (width--) 576 PCHAR(padc); 577 while (n--) 578 PCHAR(*p++); 579 if (ladjust && width > 0) 580 while (width--) 581 PCHAR(padc); 582 break; 583 case 'u': 584 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 585 base = 10; 586 goto number; 587 case 'x': 588 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 589 base = 16; 590number: if (sign && (long)ul < 0L) { 591 neg = 1; 592 ul = -(long)ul; 593 } 594 p = ksprintn(ul, base, &tmp); 595 if (sharpflag && ul != 0) { 596 if (base == 8) 597 tmp++; 598 else if (base == 16) 599 tmp += 2; 600 } 601 if (neg) 602 tmp++; 603 604 if (!ladjust && width && (width -= tmp) > 0) 605 while (width--) 606 PCHAR(padc); 607 if (neg) 608 PCHAR('-'); 609 if (sharpflag && ul != 0) { 610 if (base == 8) { 611 PCHAR('0'); 612 } else if (base == 16) { 613 PCHAR('0'); 614 PCHAR('x'); 615 } 616 } 617 618 while (*p) 619 PCHAR(*p--); 620 621 if (ladjust && width && (width -= tmp) > 0) 622 while (width--) 623 PCHAR(padc); 624 625 break; 626 default: 627 PCHAR('%'); 628 if (lflag) 629 PCHAR('l'); 630 PCHAR(ch); 631 break; 632 } 633 } 634#undef PCHAR 635} 636 637/* 638 * Put character in log buffer. 639 */ 640static void 641msglogchar(int c, void *dummyarg) 642{ 643 struct msgbuf *mbp; 644 645 if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 646 mbp = msgbufp; 647 if (mbp->msg_magic != MSG_MAGIC || 648 mbp->msg_bufx >= MSG_BSIZE || 649 mbp->msg_bufr >= MSG_BSIZE) { 650 bzero(mbp, sizeof(struct msgbuf)); 651 mbp->msg_magic = MSG_MAGIC; 652 } 653 mbp->msg_bufc[mbp->msg_bufx++] = c; 654 if (mbp->msg_bufx >= MSG_BSIZE) 655 mbp->msg_bufx = 0; 656 /* If the buffer is full, keep the most recent data. */ 657 if (mbp->msg_bufr == mbp->msg_bufx) { 658 if (++mbp->msg_bufr >= MSG_BSIZE) 659 mbp->msg_bufr = 0; 660 } 661 } 662} 663