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