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