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