hexdump.c revision 14300
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.29 1996/01/29 03:18:05 gibbs 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 printf("panic: "); 110 va_start(ap, fmt); 111 vprintf(fmt, ap); 112 va_end(ap); 113 printf("\n"); 114 115#ifdef KGDB 116 kgdb_panic(); 117#endif 118#ifdef KADB 119 if (boothowto & RB_KDB) 120 kdbpanic(); 121#endif 122#ifdef DDB 123#ifndef DDB_UNATTENDED 124 Debugger ("panic"); 125#endif 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 270void 271addlog(const char *fmt, ...) 272{ 273 register int s; 274 va_list ap; 275 276 s = splhigh(); 277 va_start(ap, fmt); 278 kvprintf(fmt, msglogchar, NULL, 10, ap); 279 splx(s); 280 va_end(ap); 281 if (!log_open) { 282 struct putchar_arg pca; 283 va_start(ap, fmt); 284 pca.tty = NULL; 285 pca.flags = TOCONS; 286 kvprintf(fmt, putchar, &pca, 10, ap); 287 va_end(ap); 288 } 289 logwakeup(); 290} 291 292int 293printf(const char *fmt, ...) 294{ 295 va_list ap; 296 register int savintr; 297 struct putchar_arg pca; 298 int retval; 299 300 savintr = consintr; /* disable interrupts */ 301 consintr = 0; 302 va_start(ap, fmt); 303 pca.tty = NULL; 304 pca.flags = TOCONS | TOLOG; 305 retval = kvprintf(fmt, putchar, &pca, 10, ap); 306 va_end(ap); 307 if (!panicstr) 308 logwakeup(); 309 consintr = savintr; /* reenable interrupts */ 310 return retval; 311} 312 313void 314vprintf(const char *fmt, va_list ap) 315{ 316 register int savintr; 317 struct putchar_arg pca; 318 319 savintr = consintr; /* disable interrupts */ 320 consintr = 0; 321 pca.tty = NULL; 322 pca.flags = TOCONS | TOLOG; 323 kvprintf(fmt, putchar, &pca, 10, ap); 324 if (!panicstr) 325 logwakeup(); 326 consintr = savintr; /* reenable interrupts */ 327} 328 329/* 330 * Print a character on console or users terminal. If destination is 331 * the console then the last MSGBUFS characters are saved in msgbuf for 332 * inspection later. 333 */ 334static void 335putchar(int c, void *arg) 336{ 337 struct putchar_arg *ap = (struct putchar_arg*) arg; 338 int flags = ap->flags; 339 struct tty *tp = ap->tty; 340 if (panicstr) 341 constty = NULL; 342 if ((flags & TOCONS) && tp == NULL && constty) { 343 tp = constty; 344 flags |= TOTTY; 345 } 346 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 347 (flags & TOCONS) && tp == constty) 348 constty = NULL; 349 if ((flags & TOLOG)) 350 msglogchar(c, NULL); 351 if ((flags & TOCONS) && constty == NULL && c != '\0') 352 (*v_putc)(c); 353} 354 355/* 356 * Scaled down version of sprintf(3). 357 */ 358int 359sprintf(char *buf, const char *cfmt, ...) 360{ 361 int retval; 362 va_list ap; 363 364 va_start(ap, cfmt); 365 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 366 buf[retval] = '\0'; 367 va_end(ap); 368 return retval; 369} 370 371/* 372 * Put a number (base <= 16) in a buffer in reverse order; return an 373 * optional length and a pointer to the NULL terminated (preceded?) 374 * buffer. 375 */ 376static char * 377ksprintn(ul, base, lenp) 378 register u_long ul; 379 register int base, *lenp; 380{ /* A long in base 8, plus NULL. */ 381 static char buf[sizeof(long) * NBBY / 3 + 2]; 382 register char *p; 383 384 p = buf; 385 do { 386 *++p = hex2ascii(ul % base); 387 } while (ul /= base); 388 if (lenp) 389 *lenp = p - buf; 390 return (p); 391} 392 393/* 394 * Scaled down version of printf(3). 395 * 396 * Two additional formats: 397 * 398 * The format %b is supported to decode error registers. 399 * Its usage is: 400 * 401 * printf("reg=%b\n", regval, "<base><arg>*"); 402 * 403 * where <base> is the output base expressed as a control character, e.g. 404 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 405 * the first of which gives the bit number to be inspected (origin 1), and 406 * the next characters (up to a control character, i.e. a character <= 32), 407 * give the name of the register. Thus: 408 * 409 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 410 * 411 * would produce output: 412 * 413 * reg=3<BITTWO,BITONE> 414 * 415 * XXX: %D -- Hexdump, takes pointer and separator string: 416 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 417 * ("%*D", len, ptr, " " -> XX XX XX XX ... 418 */ 419int 420kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 421{ 422#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 423 char *p, *q, *d; 424 u_char *up; 425 int ch, n; 426 u_long ul; 427 int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 428 int dwidth; 429 char padc; 430 int retval = 0; 431 432 if (!func) 433 d = (char *) arg; 434 else 435 d = NULL; 436 437 if (fmt == NULL) 438 fmt = "(fmt null)\n"; 439 440 if (radix < 2 || radix > 36) 441 radix = 10; 442 443 for (;;) { 444 padc = ' '; 445 width = 0; 446 while ((ch = *(u_char *)fmt++) != '%') { 447 if (ch == '\0') 448 return retval; 449 PCHAR(ch); 450 } 451 lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 452 sign = 0; dot = 0; dwidth = 0; 453reswitch: switch (ch = *(u_char *)fmt++) { 454 case '.': 455 dot = 1; 456 goto reswitch; 457 case '#': 458 sharpflag = 1; 459 goto reswitch; 460 case '+': 461 sign = 1; 462 goto reswitch; 463 case '-': 464 ladjust = 1; 465 goto reswitch; 466 case '%': 467 PCHAR(ch); 468 break; 469 case '*': 470 if (!dot) { 471 width = va_arg(ap, int); 472 if (width < 0) { 473 ladjust = !ladjust; 474 width = -width; 475 } 476 } else { 477 dwidth = va_arg(ap, int); 478 } 479 goto reswitch; 480 case '0': 481 if (!dot) { 482 padc = '0'; 483 goto reswitch; 484 } 485 case '1': case '2': case '3': case '4': 486 case '5': case '6': case '7': case '8': case '9': 487 for (n = 0;; ++fmt) { 488 n = n * 10 + ch - '0'; 489 ch = *fmt; 490 if (ch < '0' || ch > '9') 491 break; 492 } 493 if (dot) 494 dwidth = n; 495 else 496 width = n; 497 goto reswitch; 498 case 'b': 499 ul = va_arg(ap, int); 500 p = va_arg(ap, char *); 501 for (q = ksprintn(ul, *p++, NULL); *q;) 502 PCHAR(*q--); 503 504 if (!ul) 505 break; 506 507 for (tmp = 0; *p;) { 508 n = *p++; 509 if (ul & (1 << (n - 1))) { 510 PCHAR(tmp ? ',' : '<'); 511 for (; (n = *p) > ' '; ++p) 512 PCHAR(n); 513 tmp = 1; 514 } else 515 for (; *p > ' '; ++p) 516 continue; 517 } 518 if (tmp) 519 PCHAR('>'); 520 break; 521 case 'c': 522 PCHAR(va_arg(ap, int)); 523 break; 524 case 'D': 525 up = va_arg(ap, u_char *); 526 p = va_arg(ap, char *); 527 if (!width) 528 width = 16; 529 while(width--) { 530 PCHAR(hex2ascii(*up >> 4)); 531 PCHAR(hex2ascii(*up & 0x0f)); 532 up++; 533 if (width) 534 for (q=p;*q;q++) 535 PCHAR(*q); 536 } 537 break; 538 case 'd': 539 ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 540 sign = 1; 541 base = 10; 542 goto number; 543 case 'l': 544 lflag = 1; 545 goto reswitch; 546 case 'n': 547 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 548 base = radix; 549 goto number; 550 case 'o': 551 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 552 base = 8; 553 goto number; 554 case 'p': 555 ul = (u_long)va_arg(ap, void *); 556 base = 16; 557 PCHAR('0'); 558 PCHAR('x'); 559 goto number; 560 case 's': 561 p = va_arg(ap, char *); 562 if (p == NULL) 563 p = "(null)"; 564 if (!dot) 565 n = strlen (p); 566 else 567 for (n = 0; n < dwidth && p[n]; n++) 568 continue; 569 570 width -= n; 571 572 if (!ladjust && width > 0) 573 while (width--) 574 PCHAR(padc); 575 while (n--) 576 PCHAR(*p++); 577 if (ladjust && width > 0) 578 while (width--) 579 PCHAR(padc); 580 break; 581 case 'u': 582 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 583 base = 10; 584 goto number; 585 case 'x': 586 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 587 base = 16; 588number: if (sign && (long)ul < 0L) { 589 neg = 1; 590 ul = -(long)ul; 591 } 592 p = ksprintn(ul, base, &tmp); 593 if (sharpflag && ul != 0) { 594 if (base == 8) 595 tmp++; 596 else if (base == 16) 597 tmp += 2; 598 } 599 if (neg) 600 tmp++; 601 602 if (!ladjust && width && (width -= tmp) > 0) 603 while (width--) 604 PCHAR(padc); 605 if (neg) 606 PCHAR('-'); 607 if (sharpflag && ul != 0) { 608 if (base == 8) { 609 PCHAR('0'); 610 } else if (base == 16) { 611 PCHAR('0'); 612 PCHAR('x'); 613 } 614 } 615 616 while (*p) 617 PCHAR(*p--); 618 619 if (ladjust && width && (width -= tmp) > 0) 620 while (width--) 621 PCHAR(padc); 622 623 break; 624 default: 625 PCHAR('%'); 626 if (lflag) 627 PCHAR('l'); 628 PCHAR(ch); 629 break; 630 } 631 } 632#undef PCHAR 633} 634 635/* 636 * Put character in log buffer. 637 */ 638static void 639msglogchar(int c, void *dummyarg) 640{ 641 struct msgbuf *mbp; 642 643 if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 644 mbp = msgbufp; 645 if (mbp->msg_magic != MSG_MAGIC || 646 mbp->msg_bufx >= MSG_BSIZE || 647 mbp->msg_bufr >= MSG_BSIZE) { 648 bzero(mbp, sizeof(struct msgbuf)); 649 mbp->msg_magic = MSG_MAGIC; 650 } 651 mbp->msg_bufc[mbp->msg_bufx++] = c; 652 if (mbp->msg_bufx >= MSG_BSIZE) 653 mbp->msg_bufx = 0; 654 /* If the buffer is full, keep the most recent data. */ 655 if (mbp->msg_bufr == mbp->msg_bufx) { 656 if (++mbp->msg_bufr >= MSG_BSIZE) 657 mbp->msg_bufr = 0; 658 } 659 } 660} 661