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