hexdump.c revision 125985
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 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sys/kern/subr_prf.c 125985 2004-02-19 05:29:39Z njl $"); 43 44#include "opt_ddb.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/lock.h> 49#include <sys/mutex.h> 50#include <sys/sx.h> 51#include <sys/kernel.h> 52#include <sys/msgbuf.h> 53#include <sys/malloc.h> 54#include <sys/proc.h> 55#include <sys/stddef.h> 56#include <sys/sysctl.h> 57#include <sys/tty.h> 58#include <sys/syslog.h> 59#include <sys/cons.h> 60#include <sys/uio.h> 61 62#ifdef DDB 63#include <ddb/ddb.h> 64#endif 65 66/* 67 * Note that stdarg.h and the ANSI style va_start macro is used for both 68 * ANSI and traditional C compilers. 69 */ 70#include <machine/stdarg.h> 71 72#define TOCONS 0x01 73#define TOTTY 0x02 74#define TOLOG 0x04 75 76/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ 77#define MAXNBUF (sizeof(intmax_t) * NBBY + 1) 78 79struct putchar_arg { 80 int flags; 81 int pri; 82 struct tty *tty; 83}; 84 85struct snprintf_arg { 86 char *str; 87 size_t remain; 88}; 89 90extern int log_open; 91 92static void msglogchar(int c, int pri); 93static void putchar(int ch, void *arg); 94static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len); 95static void snprintf_func(int ch, void *arg); 96 97static int consintr = 1; /* Ok to handle console interrupts? */ 98static int msgbufmapped; /* Set when safe to use msgbuf */ 99int msgbuftrigger; 100 101static int log_console_output = 1; 102TUNABLE_INT("kern.log_console_output", &log_console_output); 103SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, 104 &log_console_output, 0, ""); 105 106/* 107 * Warn that a system table is full. 108 */ 109void 110tablefull(const char *tab) 111{ 112 113 log(LOG_ERR, "%s: table is full\n", tab); 114} 115 116/* 117 * Uprintf prints to the controlling terminal for the current process. 118 * It may block if the tty queue is overfull. No message is printed if 119 * the queue does not clear in a reasonable time. 120 */ 121int 122uprintf(const char *fmt, ...) 123{ 124 struct thread *td = curthread; 125 struct proc *p = td->td_proc; 126 va_list ap; 127 struct putchar_arg pca; 128 int retval; 129 130 if (td == NULL || td == PCPU_GET(idlethread)) 131 return (0); 132 133 p = td->td_proc; 134 PROC_LOCK(p); 135 if ((p->p_flag & P_CONTROLT) == 0) { 136 PROC_UNLOCK(p); 137 return (0); 138 } 139 SESS_LOCK(p->p_session); 140 pca.tty = p->p_session->s_ttyp; 141 SESS_UNLOCK(p->p_session); 142 PROC_UNLOCK(p); 143 if (pca.tty == NULL) 144 return (0); 145 pca.flags = TOTTY; 146 va_start(ap, fmt); 147 retval = kvprintf(fmt, putchar, &pca, 10, ap); 148 va_end(ap); 149 150 return (retval); 151} 152 153/* 154 * tprintf prints on the controlling terminal associated 155 * with the given session, possibly to the log as well. 156 */ 157void 158tprintf(struct proc *p, int pri, const char *fmt, ...) 159{ 160 struct tty *tp = NULL; 161 int flags = 0; 162 va_list ap; 163 struct putchar_arg pca; 164 struct session *sess = NULL; 165 166 if (pri != -1) 167 flags |= TOLOG; 168 if (p != NULL) { 169 PROC_LOCK(p); 170 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 171 sess = p->p_session; 172 SESS_LOCK(sess); 173 PROC_UNLOCK(p); 174 SESSHOLD(sess); 175 tp = sess->s_ttyp; 176 SESS_UNLOCK(sess); 177 if (ttycheckoutq(tp, 0)) 178 flags |= TOTTY; 179 else 180 tp = NULL; 181 } else 182 PROC_UNLOCK(p); 183 } 184 pca.pri = pri; 185 pca.tty = tp; 186 pca.flags = flags; 187 va_start(ap, fmt); 188 kvprintf(fmt, putchar, &pca, 10, ap); 189 va_end(ap); 190 if (sess != NULL) { 191 SESS_LOCK(sess); 192 SESSRELE(sess); 193 SESS_UNLOCK(sess); 194 } 195 msgbuftrigger = 1; 196} 197 198/* 199 * Ttyprintf displays a message on a tty; it should be used only by 200 * the tty driver, or anything that knows the underlying tty will not 201 * be revoke(2)'d away. Other callers should use tprintf. 202 */ 203int 204ttyprintf(struct tty *tp, const char *fmt, ...) 205{ 206 va_list ap; 207 struct putchar_arg pca; 208 int retval; 209 210 va_start(ap, fmt); 211 pca.tty = tp; 212 pca.flags = TOTTY; 213 retval = kvprintf(fmt, putchar, &pca, 10, ap); 214 va_end(ap); 215 return (retval); 216} 217 218/* 219 * Log writes to the log buffer, and guarantees not to sleep (so can be 220 * called by interrupt routines). If there is no process reading the 221 * log yet, it writes to the console also. 222 */ 223void 224log(int level, const char *fmt, ...) 225{ 226 va_list ap; 227 struct putchar_arg pca; 228 229 pca.tty = NULL; 230 pca.pri = level; 231 pca.flags = log_open ? TOLOG : TOCONS; 232 233 va_start(ap, fmt); 234 kvprintf(fmt, putchar, &pca, 10, ap); 235 va_end(ap); 236 237 msgbuftrigger = 1; 238} 239 240#define CONSCHUNK 128 241 242void 243log_console(struct uio *uio) 244{ 245 int c, i, error, iovlen, nl; 246 struct uio muio; 247 struct iovec *miov = NULL; 248 char *consbuffer; 249 int pri; 250 251 if (!log_console_output) 252 return; 253 254 pri = LOG_INFO | LOG_CONSOLE; 255 muio = *uio; 256 iovlen = uio->uio_iovcnt * sizeof (struct iovec); 257 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 258 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK); 259 bcopy(muio.uio_iov, miov, iovlen); 260 muio.uio_iov = miov; 261 uio = &muio; 262 263 nl = 0; 264 while (uio->uio_resid > 0) { 265 c = imin(uio->uio_resid, CONSCHUNK); 266 error = uiomove(consbuffer, c, uio); 267 if (error != 0) 268 break; 269 for (i = 0; i < c; i++) { 270 msglogchar(consbuffer[i], pri); 271 if (consbuffer[i] == '\n') 272 nl = 1; 273 else 274 nl = 0; 275 } 276 } 277 if (!nl) 278 msglogchar('\n', pri); 279 msgbuftrigger = 1; 280 FREE(miov, M_TEMP); 281 FREE(consbuffer, M_TEMP); 282 return; 283} 284 285int 286printf(const char *fmt, ...) 287{ 288 va_list ap; 289 int savintr; 290 struct putchar_arg pca; 291 int retval; 292 293 savintr = consintr; /* disable interrupts */ 294 consintr = 0; 295 va_start(ap, fmt); 296 pca.tty = NULL; 297 pca.flags = TOCONS | TOLOG; 298 pca.pri = -1; 299 retval = kvprintf(fmt, putchar, &pca, 10, ap); 300 va_end(ap); 301 if (!panicstr) 302 msgbuftrigger = 1; 303 consintr = savintr; /* reenable interrupts */ 304 return (retval); 305} 306 307int 308vprintf(const char *fmt, va_list ap) 309{ 310 int savintr; 311 struct putchar_arg pca; 312 int retval; 313 314 savintr = consintr; /* disable interrupts */ 315 consintr = 0; 316 pca.tty = NULL; 317 pca.flags = TOCONS | TOLOG; 318 pca.pri = -1; 319 retval = kvprintf(fmt, putchar, &pca, 10, ap); 320 if (!panicstr) 321 msgbuftrigger = 1; 322 consintr = savintr; /* reenable interrupts */ 323 return (retval); 324} 325 326/* 327 * Print a character on console or users terminal. If destination is 328 * the console then the last bunch of characters are saved in msgbuf for 329 * inspection later. 330 */ 331static void 332putchar(int c, void *arg) 333{ 334 struct putchar_arg *ap = (struct putchar_arg*) arg; 335 struct tty *tp = ap->tty; 336 int consdirect, flags = ap->flags; 337 338 consdirect = ((flags & TOCONS) && constty == NULL); 339 /* Don't use the tty code after a panic or while in ddb. */ 340 if (panicstr) 341 consdirect = 1; 342#ifdef DDB 343 if (db_active) 344 consdirect = 1; 345#endif 346 if (consdirect) { 347 if (c != '\0') 348 cnputc(c); 349 } else { 350 if ((flags & TOTTY) && tp != NULL) 351 tputchar(c, tp); 352 if ((flags & TOCONS) && constty != NULL) 353 msgbuf_addchar(&consmsgbuf, c); 354 } 355 if ((flags & TOLOG)) 356 msglogchar(c, ap->pri); 357} 358 359/* 360 * Scaled down version of sprintf(3). 361 */ 362int 363sprintf(char *buf, const char *cfmt, ...) 364{ 365 int retval; 366 va_list ap; 367 368 va_start(ap, cfmt); 369 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 370 buf[retval] = '\0'; 371 va_end(ap); 372 return (retval); 373} 374 375/* 376 * Scaled down version of vsprintf(3). 377 */ 378int 379vsprintf(char *buf, const char *cfmt, va_list ap) 380{ 381 int retval; 382 383 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 384 buf[retval] = '\0'; 385 return (retval); 386} 387 388/* 389 * Scaled down version of snprintf(3). 390 */ 391int 392snprintf(char *str, size_t size, const char *format, ...) 393{ 394 int retval; 395 va_list ap; 396 397 va_start(ap, format); 398 retval = vsnprintf(str, size, format, ap); 399 va_end(ap); 400 return(retval); 401} 402 403/* 404 * Scaled down version of vsnprintf(3). 405 */ 406int 407vsnprintf(char *str, size_t size, const char *format, va_list ap) 408{ 409 struct snprintf_arg info; 410 int retval; 411 412 info.str = str; 413 info.remain = size; 414 retval = kvprintf(format, snprintf_func, &info, 10, ap); 415 if (info.remain >= 1) 416 *info.str++ = '\0'; 417 return (retval); 418} 419 420/* 421 * Kernel version which takes radix argument vsnprintf(3). 422 */ 423int 424vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap) 425{ 426 struct snprintf_arg info; 427 int retval; 428 429 info.str = str; 430 info.remain = size; 431 retval = kvprintf(format, snprintf_func, &info, radix, ap); 432 if (info.remain >= 1) 433 *info.str++ = '\0'; 434 return (retval); 435} 436 437static void 438snprintf_func(int ch, void *arg) 439{ 440 struct snprintf_arg *const info = arg; 441 442 if (info->remain >= 2) { 443 *info->str++ = ch; 444 info->remain--; 445 } 446} 447 448/* 449 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 450 * order; return an optional length and a pointer to the last character 451 * written in the buffer (i.e., the first character of the string). 452 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 453 */ 454static char * 455ksprintn(char *nbuf, uintmax_t num, int base, int *lenp) 456{ 457 char *p; 458 459 p = nbuf; 460 *p = '\0'; 461 do { 462 *++p = hex2ascii(num % base); 463 } while (num /= base); 464 if (lenp) 465 *lenp = p - nbuf; 466 return (p); 467} 468 469/* 470 * Scaled down version of printf(3). 471 * 472 * Two additional formats: 473 * 474 * The format %b is supported to decode error registers. 475 * Its usage is: 476 * 477 * printf("reg=%b\n", regval, "<base><arg>*"); 478 * 479 * where <base> is the output base expressed as a control character, e.g. 480 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 481 * the first of which gives the bit number to be inspected (origin 1), and 482 * the next characters (up to a control character, i.e. a character <= 32), 483 * give the name of the register. Thus: 484 * 485 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 486 * 487 * would produce output: 488 * 489 * reg=3<BITTWO,BITONE> 490 * 491 * XXX: %D -- Hexdump, takes pointer and separator string: 492 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 493 * ("%*D", len, ptr, " " -> XX XX XX XX ... 494 */ 495int 496kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 497{ 498#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 499 char nbuf[MAXNBUF]; 500 char *d; 501 const char *p, *percent, *q; 502 u_char *up; 503 int ch, n; 504 uintmax_t num; 505 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 506 int cflag, hflag, jflag, tflag, zflag; 507 int dwidth; 508 char padc; 509 int retval = 0; 510 511 num = 0; 512 if (!func) 513 d = (char *) arg; 514 else 515 d = NULL; 516 517 if (fmt == NULL) 518 fmt = "(fmt null)\n"; 519 520 if (radix < 2 || radix > 36) 521 radix = 10; 522 523 for (;;) { 524 padc = ' '; 525 width = 0; 526 while ((ch = (u_char)*fmt++) != '%') { 527 if (ch == '\0') 528 return (retval); 529 PCHAR(ch); 530 } 531 percent = fmt - 1; 532 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 533 sign = 0; dot = 0; dwidth = 0; 534 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; 535reswitch: switch (ch = (u_char)*fmt++) { 536 case '.': 537 dot = 1; 538 goto reswitch; 539 case '#': 540 sharpflag = 1; 541 goto reswitch; 542 case '+': 543 sign = 1; 544 goto reswitch; 545 case '-': 546 ladjust = 1; 547 goto reswitch; 548 case '%': 549 PCHAR(ch); 550 break; 551 case '*': 552 if (!dot) { 553 width = va_arg(ap, int); 554 if (width < 0) { 555 ladjust = !ladjust; 556 width = -width; 557 } 558 } else { 559 dwidth = va_arg(ap, int); 560 } 561 goto reswitch; 562 case '0': 563 if (!dot) { 564 padc = '0'; 565 goto reswitch; 566 } 567 case '1': case '2': case '3': case '4': 568 case '5': case '6': case '7': case '8': case '9': 569 for (n = 0;; ++fmt) { 570 n = n * 10 + ch - '0'; 571 ch = *fmt; 572 if (ch < '0' || ch > '9') 573 break; 574 } 575 if (dot) 576 dwidth = n; 577 else 578 width = n; 579 goto reswitch; 580 case 'b': 581 num = (u_int)va_arg(ap, int); 582 p = va_arg(ap, char *); 583 for (q = ksprintn(nbuf, num, *p++, NULL); *q;) 584 PCHAR(*q--); 585 586 if (num == 0) 587 break; 588 589 for (tmp = 0; *p;) { 590 n = *p++; 591 if (num & (1 << (n - 1))) { 592 PCHAR(tmp ? ',' : '<'); 593 for (; (n = *p) > ' '; ++p) 594 PCHAR(n); 595 tmp = 1; 596 } else 597 for (; *p > ' '; ++p) 598 continue; 599 } 600 if (tmp) 601 PCHAR('>'); 602 break; 603 case 'c': 604 PCHAR(va_arg(ap, int)); 605 break; 606 case 'D': 607 up = va_arg(ap, u_char *); 608 p = va_arg(ap, char *); 609 if (!width) 610 width = 16; 611 while(width--) { 612 PCHAR(hex2ascii(*up >> 4)); 613 PCHAR(hex2ascii(*up & 0x0f)); 614 up++; 615 if (width) 616 for (q=p;*q;q++) 617 PCHAR(*q); 618 } 619 break; 620 case 'd': 621 case 'i': 622 base = 10; 623 sign = 1; 624 goto handle_sign; 625 case 'h': 626 if (hflag) { 627 hflag = 0; 628 cflag = 1; 629 } else 630 hflag = 1; 631 goto reswitch; 632 case 'j': 633 jflag = 1; 634 goto reswitch; 635 case 'l': 636 if (lflag) { 637 lflag = 0; 638 qflag = 1; 639 } else 640 lflag = 1; 641 goto reswitch; 642 case 'n': 643 if (jflag) 644 *(va_arg(ap, intmax_t *)) = retval; 645 else if (qflag) 646 *(va_arg(ap, quad_t *)) = retval; 647 else if (lflag) 648 *(va_arg(ap, long *)) = retval; 649 else if (zflag) 650 *(va_arg(ap, size_t *)) = retval; 651 else if (hflag) 652 *(va_arg(ap, short *)) = retval; 653 else if (cflag) 654 *(va_arg(ap, char *)) = retval; 655 else 656 *(va_arg(ap, int *)) = retval; 657 break; 658 case 'o': 659 base = 8; 660 goto handle_nosign; 661 case 'p': 662 base = 16; 663 sharpflag = (width == 0); 664 sign = 0; 665 num = (uintptr_t)va_arg(ap, void *); 666 goto number; 667 case 'q': 668 qflag = 1; 669 goto reswitch; 670 case 'r': 671 base = radix; 672 if (sign) 673 goto handle_sign; 674 goto handle_nosign; 675 case 's': 676 p = va_arg(ap, char *); 677 if (p == NULL) 678 p = "(null)"; 679 if (!dot) 680 n = strlen (p); 681 else 682 for (n = 0; n < dwidth && p[n]; n++) 683 continue; 684 685 width -= n; 686 687 if (!ladjust && width > 0) 688 while (width--) 689 PCHAR(padc); 690 while (n--) 691 PCHAR(*p++); 692 if (ladjust && width > 0) 693 while (width--) 694 PCHAR(padc); 695 break; 696 case 't': 697 tflag = 1; 698 goto reswitch; 699 case 'u': 700 base = 10; 701 goto handle_nosign; 702 case 'x': 703 case 'X': 704 base = 16; 705 goto handle_nosign; 706 case 'y': 707 base = 16; 708 sign = 1; 709 goto handle_sign; 710 case 'z': 711 zflag = 1; 712 goto reswitch; 713handle_nosign: 714 sign = 0; 715 if (jflag) 716 num = va_arg(ap, uintmax_t); 717 else if (qflag) 718 num = va_arg(ap, u_quad_t); 719 else if (tflag) 720 num = va_arg(ap, ptrdiff_t); 721 else if (lflag) 722 num = va_arg(ap, u_long); 723 else if (zflag) 724 num = va_arg(ap, size_t); 725 else if (hflag) 726 num = (u_short)va_arg(ap, int); 727 else if (cflag) 728 num = (u_char)va_arg(ap, int); 729 else 730 num = va_arg(ap, u_int); 731 goto number; 732handle_sign: 733 if (jflag) 734 num = va_arg(ap, intmax_t); 735 else if (qflag) 736 num = va_arg(ap, quad_t); 737 else if (tflag) 738 num = va_arg(ap, ptrdiff_t); 739 else if (lflag) 740 num = va_arg(ap, long); 741 else if (zflag) 742 num = va_arg(ap, size_t); 743 else if (hflag) 744 num = (short)va_arg(ap, int); 745 else if (cflag) 746 num = (char)va_arg(ap, int); 747 else 748 num = va_arg(ap, int); 749number: 750 if (sign && (intmax_t)num < 0) { 751 neg = 1; 752 num = -(intmax_t)num; 753 } 754 p = ksprintn(nbuf, num, base, &tmp); 755 if (sharpflag && num != 0) { 756 if (base == 8) 757 tmp++; 758 else if (base == 16) 759 tmp += 2; 760 } 761 if (neg) 762 tmp++; 763 764 if (!ladjust && width && (width -= tmp) > 0) 765 while (width--) 766 PCHAR(padc); 767 if (neg) 768 PCHAR('-'); 769 if (sharpflag && num != 0) { 770 if (base == 8) { 771 PCHAR('0'); 772 } else if (base == 16) { 773 PCHAR('0'); 774 PCHAR('x'); 775 } 776 } 777 778 while (*p) 779 PCHAR(*p--); 780 781 if (ladjust && width && (width -= tmp) > 0) 782 while (width--) 783 PCHAR(padc); 784 785 break; 786 default: 787 while (percent < fmt) 788 PCHAR(*percent++); 789 break; 790 } 791 } 792#undef PCHAR 793} 794 795/* 796 * Put character in log buffer with a particular priority. 797 */ 798static void 799msglogchar(int c, int pri) 800{ 801 static int lastpri = -1; 802 static int dangling; 803 char nbuf[MAXNBUF]; 804 char *p; 805 806 if (!msgbufmapped) 807 return; 808 if (c == '\0' || c == '\r') 809 return; 810 if (pri != -1 && pri != lastpri) { 811 if (dangling) { 812 msgbuf_addchar(msgbufp, '\n'); 813 dangling = 0; 814 } 815 msgbuf_addchar(msgbufp, '<'); 816 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;) 817 msgbuf_addchar(msgbufp, *p--); 818 msgbuf_addchar(msgbufp, '>'); 819 lastpri = pri; 820 } 821 msgbuf_addchar(msgbufp, c); 822 if (c == '\n') { 823 dangling = 0; 824 lastpri = -1; 825 } else { 826 dangling = 1; 827 } 828} 829 830void 831msgbufinit(void *ptr, int size) 832{ 833 char *cp; 834 static struct msgbuf *oldp = NULL; 835 836 size -= sizeof(*msgbufp); 837 cp = (char *)ptr; 838 msgbufp = (struct msgbuf *)(cp + size); 839 msgbuf_reinit(msgbufp, cp, size); 840 if (msgbufmapped && oldp != msgbufp) 841 msgbuf_copy(oldp, msgbufp); 842 msgbufmapped = 1; 843 oldp = msgbufp; 844} 845 846SYSCTL_DECL(_security_bsd); 847 848static int unprivileged_read_msgbuf = 1; 849SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf, 850 CTLFLAG_RW, &unprivileged_read_msgbuf, 0, 851 "Unprivileged processes may read the kernel message buffer"); 852 853/* Sysctls for accessing/clearing the msgbuf */ 854static int 855sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 856{ 857 char buf[128]; 858 u_int seq; 859 int error, len; 860 861 if (!unprivileged_read_msgbuf) { 862 error = suser(req->td); 863 if (error) 864 return (error); 865 } 866 867 /* Read the whole buffer, one chunk at a time. */ 868 msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 869 while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 870 error = sysctl_handle_opaque(oidp, buf, len, req); 871 if (error) 872 return (error); 873 } 874 return (0); 875} 876 877SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 878 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 879 880static int msgbuf_clearflag; 881 882static int 883sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 884{ 885 int error; 886 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 887 if (!error && req->newptr) { 888 msgbuf_clear(msgbufp); 889 msgbuf_clearflag = 0; 890 } 891 return (error); 892} 893 894SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 895 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0, 896 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); 897 898#ifdef DDB 899 900DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 901{ 902 int i, j; 903 904 if (!msgbufmapped) { 905 db_printf("msgbuf not mapped yet\n"); 906 return; 907 } 908 db_printf("msgbufp = %p\n", msgbufp); 909 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n", 910 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq, 911 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum); 912 for (i = 0; i < msgbufp->msg_size; i++) { 913 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq); 914 db_printf("%c", msgbufp->msg_ptr[j]); 915 } 916 db_printf("\n"); 917} 918 919#endif /* DDB */ 920 921void 922hexdump(void *ptr, int length, const char *hdr, int flags) 923{ 924 int i, j, k; 925 int cols; 926 unsigned char *cp; 927 char delim; 928 929 if ((flags & HD_DELIM_MASK) != 0) 930 delim = (flags & HD_DELIM_MASK) >> 8; 931 else 932 delim = ' '; 933 934 if ((flags & HD_COLUMN_MASK) != 0) 935 cols = flags & HD_COLUMN_MASK; 936 else 937 cols = 16; 938 939 cp = ptr; 940 for (i = 0; i < length; i+= cols) { 941 if (hdr != NULL) 942 printf("%s", hdr); 943 944 if ((flags & HD_OMIT_COUNT) == 0) 945 printf("%04x ", i); 946 947 if ((flags & HD_OMIT_HEX) == 0) { 948 for (j = 0; j < cols; j++) { 949 k = i + j; 950 if (k < length) 951 printf("%c%02x", delim, cp[k]); 952 else 953 printf(" "); 954 } 955 } 956 957 if ((flags & HD_OMIT_CHARS) == 0) { 958 printf(" |"); 959 for (j = 0; j < cols; j++) { 960 k = i + j; 961 if (k >= length) 962 printf(" "); 963 else if (cp[k] >= ' ' && cp[k] <= '~') 964 printf("%c", cp[k]); 965 else 966 printf("."); 967 } 968 printf("|\n"); 969 } 970 } 971} 972 973