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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_ddb.h" 41 #include "opt_printf.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/lock.h> 46 #include <sys/kdb.h> 47 #include <sys/mutex.h> 48 #include <sys/sx.h> 49 #include <sys/kernel.h> 50 #include <sys/msgbuf.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 #include <sys/proc.h> 54 #include <sys/stddef.h> 55 #include <sys/sysctl.h> 56 #include <sys/tty.h> 57 #include <sys/syslog.h> 58 #include <sys/cons.h> 59 #include <sys/uio.h> 60 #include <sys/ctype.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 79 struct putchar_arg { 80 int flags; 81 int pri; 82 struct tty *tty; 83 char *p_bufr; 84 size_t n_bufr; 85 char *p_next; 86 size_t remain; 87 }; 88 89 struct snprintf_arg { 90 char *str; 91 size_t remain; 92 }; 93 94 extern int log_open; 95 96 static void msglogchar(int c, int pri); 97 static void msglogstr(char *str, int pri, int filter_cr); 98 static void putchar(int ch, void *arg); 99 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper); 100 static void snprintf_func(int ch, void *arg); 101 102 static int msgbufmapped; /* Set when safe to use msgbuf */ 103 int msgbuftrigger; 104 105 static int log_console_output = 1; 106 TUNABLE_INT("kern.log_console_output", &log_console_output); 107 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, 108 &log_console_output, 0, "Duplicate console output to the syslog."); 109 110 /* 111 * See the comment in log_console() below for more explanation of this. 112 */ 113 static int log_console_add_linefeed = 0; 114 TUNABLE_INT("kern.log_console_add_linefeed", &log_console_add_linefeed); 115 SYSCTL_INT(_kern, OID_AUTO, log_console_add_linefeed, CTLFLAG_RW, 116 &log_console_add_linefeed, 0, "log_console() adds extra newlines."); 117 118 static int always_console_output = 0; 119 TUNABLE_INT("kern.always_console_output", &always_console_output); 120 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW, 121 &always_console_output, 0, "Always output to console despite TIOCCONS."); 122 123 /* 124 * Warn that a system table is full. 125 */ 126 void 127 tablefull(const char *tab) 128 { 129 130 log(LOG_ERR, "%s: table is full\n", tab); 131 } 132 133 /* 134 * Uprintf prints to the controlling terminal for the current process. 135 */ 136 int 137 uprintf(const char *fmt, ...) 138 { 139 va_list ap; 140 struct putchar_arg pca; 141 struct proc *p; 142 struct thread *td; 143 int retval; 144 145 td = curthread; 146 if (TD_IS_IDLETHREAD(td)) 147 return (0); 148 149 sx_slock(&proctree_lock); 150 p = td->td_proc; 151 PROC_LOCK(p); 152 if ((p->p_flag & P_CONTROLT) == 0) { 153 PROC_UNLOCK(p); 154 retval = 0; 155 goto out; 156 } 157 SESS_LOCK(p->p_session); 158 pca.tty = p->p_session->s_ttyp; 159 SESS_UNLOCK(p->p_session); 160 PROC_UNLOCK(p); 161 if (pca.tty == NULL) { 162 retval = 0; 163 goto out; 164 } 165 pca.flags = TOTTY; 166 pca.p_bufr = NULL; 167 va_start(ap, fmt); 168 tty_lock(pca.tty); 169 retval = kvprintf(fmt, putchar, &pca, 10, ap); 170 tty_unlock(pca.tty); 171 va_end(ap); 172 out: 173 sx_sunlock(&proctree_lock); 174 return (retval); 175 } 176 177 /* 178 * tprintf prints on the controlling terminal associated with the given 179 * session, possibly to the log as well. 180 */ 181 void 182 tprintf(struct proc *p, int pri, const char *fmt, ...) 183 { 184 struct tty *tp = NULL; 185 int flags = 0; 186 va_list ap; 187 struct putchar_arg pca; 188 struct session *sess = NULL; 189 190 sx_slock(&proctree_lock); 191 if (pri != -1) 192 flags |= TOLOG; 193 if (p != NULL) { 194 PROC_LOCK(p); 195 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 196 sess = p->p_session; 197 sess_hold(sess); 198 PROC_UNLOCK(p); 199 tp = sess->s_ttyp; 200 if (tp != NULL && tty_checkoutq(tp)) 201 flags |= TOTTY; 202 else 203 tp = NULL; 204 } else 205 PROC_UNLOCK(p); 206 } 207 pca.pri = pri; 208 pca.tty = tp; 209 pca.flags = flags; 210 pca.p_bufr = NULL; 211 va_start(ap, fmt); 212 if (pca.tty != NULL) 213 tty_lock(pca.tty); 214 kvprintf(fmt, putchar, &pca, 10, ap); 215 if (pca.tty != NULL) 216 tty_unlock(pca.tty); 217 va_end(ap); 218 if (sess != NULL) 219 sess_release(sess); 220 msgbuftrigger = 1; 221 sx_sunlock(&proctree_lock); 222 } 223 224 /* 225 * Ttyprintf displays a message on a tty; it should be used only by 226 * the tty driver, or anything that knows the underlying tty will not 227 * be revoke(2)'d away. Other callers should use tprintf. 228 */ 229 int 230 ttyprintf(struct tty *tp, const char *fmt, ...) 231 { 232 va_list ap; 233 struct putchar_arg pca; 234 int retval; 235 236 va_start(ap, fmt); 237 pca.tty = tp; 238 pca.flags = TOTTY; 239 pca.p_bufr = NULL; 240 retval = kvprintf(fmt, putchar, &pca, 10, ap); 241 va_end(ap); 242 return (retval); 243 } 244 245 /* 246 * Log writes to the log buffer, and guarantees not to sleep (so can be 247 * called by interrupt routines). If there is no process reading the 248 * log yet, it writes to the console also. 249 */ 250 void 251 log(int level, const char *fmt, ...) 252 { 253 va_list ap; 254 struct putchar_arg pca; 255 #ifdef PRINTF_BUFR_SIZE 256 char bufr[PRINTF_BUFR_SIZE]; 257 #endif 258 259 pca.tty = NULL; 260 pca.pri = level; 261 pca.flags = log_open ? TOLOG : TOCONS; 262 #ifdef PRINTF_BUFR_SIZE 263 pca.p_bufr = bufr; 264 pca.p_next = pca.p_bufr; 265 pca.n_bufr = sizeof(bufr); 266 pca.remain = sizeof(bufr); 267 *pca.p_next = '\0'; 268 #else 269 pca.p_bufr = NULL; 270 #endif 271 272 va_start(ap, fmt); 273 kvprintf(fmt, putchar, &pca, 10, ap); 274 va_end(ap); 275 276 #ifdef PRINTF_BUFR_SIZE 277 /* Write any buffered console/log output: */ 278 if (*pca.p_bufr != '\0') { 279 if (pca.flags & TOLOG) 280 msglogstr(pca.p_bufr, level, /*filter_cr*/1); 281 282 if (pca.flags & TOCONS) 283 cnputs(pca.p_bufr); 284 } 285 #endif 286 msgbuftrigger = 1; 287 } 288 289 #define CONSCHUNK 128 290 291 void 292 log_console(struct uio *uio) 293 { 294 int c, error, nl; 295 char *consbuffer; 296 int pri; 297 298 if (!log_console_output) 299 return; 300 301 pri = LOG_INFO | LOG_CONSOLE; 302 uio = cloneuio(uio); 303 consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK); 304 305 nl = 0; 306 while (uio->uio_resid > 0) { 307 c = imin(uio->uio_resid, CONSCHUNK - 1); 308 error = uiomove(consbuffer, c, uio); 309 if (error != 0) 310 break; 311 /* Make sure we're NUL-terminated */ 312 consbuffer[c] = '\0'; 313 if (consbuffer[c - 1] == '\n') 314 nl = 1; 315 else 316 nl = 0; 317 msglogstr(consbuffer, pri, /*filter_cr*/ 1); 318 } 319 /* 320 * The previous behavior in log_console() is preserved when 321 * log_console_add_linefeed is non-zero. For that behavior, if an 322 * individual console write came in that was not terminated with a 323 * line feed, it would add a line feed. 324 * 325 * This results in different data in the message buffer than 326 * appears on the system console (which doesn't add extra line feed 327 * characters). 328 * 329 * A number of programs and rc scripts write a line feed, or a period 330 * and a line feed when they have completed their operation. On 331 * the console, this looks seamless, but when displayed with 332 * 'dmesg -a', you wind up with output that looks like this: 333 * 334 * Updating motd: 335 * . 336 * 337 * On the console, it looks like this: 338 * Updating motd:. 339 * 340 * We could add logic to detect that situation, or just not insert 341 * the extra newlines. Set the kern.log_console_add_linefeed 342 * sysctl/tunable variable to get the old behavior. 343 */ 344 if (!nl && log_console_add_linefeed) { 345 consbuffer[0] = '\n'; 346 consbuffer[1] = '\0'; 347 msglogstr(consbuffer, pri, /*filter_cr*/ 1); 348 } 349 msgbuftrigger = 1; 350 free(uio, M_IOV); 351 free(consbuffer, M_TEMP); 352 return; 353 } 354 355 int 356 printf(const char *fmt, ...) 357 { 358 va_list ap; 359 int retval; 360 361 va_start(ap, fmt); 362 retval = vprintf(fmt, ap); 363 va_end(ap); 364 365 return (retval); 366 } 367 368 int 369 vprintf(const char *fmt, va_list ap) 370 { 371 struct putchar_arg pca; 372 int retval; 373 #ifdef PRINTF_BUFR_SIZE 374 char bufr[PRINTF_BUFR_SIZE]; 375 #endif 376 377 pca.tty = NULL; 378 pca.flags = TOCONS | TOLOG; 379 pca.pri = -1; 380 #ifdef PRINTF_BUFR_SIZE 381 pca.p_bufr = bufr; 382 pca.p_next = pca.p_bufr; 383 pca.n_bufr = sizeof(bufr); 384 pca.remain = sizeof(bufr); 385 *pca.p_next = '\0'; 386 #else 387 /* Don't buffer console output. */ 388 pca.p_bufr = NULL; 389 #endif 390 391 retval = kvprintf(fmt, putchar, &pca, 10, ap); 392 393 #ifdef PRINTF_BUFR_SIZE 394 /* Write any buffered console/log output: */ 395 if (*pca.p_bufr != '\0') { 396 cnputs(pca.p_bufr); 397 msglogstr(pca.p_bufr, pca.pri, /*filter_cr*/ 1); 398 } 399 #endif 400 401 if (!panicstr) 402 msgbuftrigger = 1; 403 404 return (retval); 405 } 406 407 static void 408 putbuf(int c, struct putchar_arg *ap) 409 { 410 /* Check if no console output buffer was provided. */ 411 if (ap->p_bufr == NULL) { 412 /* Output direct to the console. */ 413 if (ap->flags & TOCONS) 414 cnputc(c); 415 416 if (ap->flags & TOLOG) 417 msglogchar(c, ap->pri); 418 } else { 419 /* Buffer the character: */ 420 *ap->p_next++ = c; 421 ap->remain--; 422 423 /* Always leave the buffer zero terminated. */ 424 *ap->p_next = '\0'; 425 426 /* Check if the buffer needs to be flushed. */ 427 if (ap->remain == 2 || c == '\n') { 428 429 if (ap->flags & TOLOG) 430 msglogstr(ap->p_bufr, ap->pri, /*filter_cr*/1); 431 432 if (ap->flags & TOCONS) { 433 if ((panicstr == NULL) && (constty != NULL)) 434 msgbuf_addstr(&consmsgbuf, -1, 435 ap->p_bufr, /*filter_cr*/ 0); 436 437 if ((constty == NULL) ||(always_console_output)) 438 cnputs(ap->p_bufr); 439 } 440 441 ap->p_next = ap->p_bufr; 442 ap->remain = ap->n_bufr; 443 *ap->p_next = '\0'; 444 } 445 446 /* 447 * Since we fill the buffer up one character at a time, 448 * this should not happen. We should always catch it when 449 * ap->remain == 2 (if not sooner due to a newline), flush 450 * the buffer and move on. One way this could happen is 451 * if someone sets PRINTF_BUFR_SIZE to 1 or something 452 * similarly silly. 453 */ 454 KASSERT(ap->remain > 2, ("Bad buffer logic, remain = %zd", 455 ap->remain)); 456 } 457 } 458 459 /* 460 * Print a character on console or users terminal. If destination is 461 * the console then the last bunch of characters are saved in msgbuf for 462 * inspection later. 463 */ 464 static void 465 putchar(int c, void *arg) 466 { 467 struct putchar_arg *ap = (struct putchar_arg*) arg; 468 struct tty *tp = ap->tty; 469 int flags = ap->flags; 470 471 /* Don't use the tty code after a panic or while in ddb. */ 472 if (kdb_active) { 473 if (c != '\0') 474 cnputc(c); 475 return; 476 } 477 478 if ((flags & TOTTY) && tp != NULL && panicstr == NULL) 479 tty_putchar(tp, c); 480 481 if ((flags & (TOCONS | TOLOG)) && c != '\0') 482 putbuf(c, ap); 483 } 484 485 /* 486 * Scaled down version of sprintf(3). 487 */ 488 int 489 sprintf(char *buf, const char *cfmt, ...) 490 { 491 int retval; 492 va_list ap; 493 494 va_start(ap, cfmt); 495 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 496 buf[retval] = '\0'; 497 va_end(ap); 498 return (retval); 499 } 500 501 /* 502 * Scaled down version of vsprintf(3). 503 */ 504 int 505 vsprintf(char *buf, const char *cfmt, va_list ap) 506 { 507 int retval; 508 509 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 510 buf[retval] = '\0'; 511 return (retval); 512 } 513 514 /* 515 * Scaled down version of snprintf(3). 516 */ 517 int 518 snprintf(char *str, size_t size, const char *format, ...) 519 { 520 int retval; 521 va_list ap; 522 523 va_start(ap, format); 524 retval = vsnprintf(str, size, format, ap); 525 va_end(ap); 526 return(retval); 527 } 528 529 /* 530 * Scaled down version of vsnprintf(3). 531 */ 532 int 533 vsnprintf(char *str, size_t size, const char *format, va_list ap) 534 { 535 struct snprintf_arg info; 536 int retval; 537 538 info.str = str; 539 info.remain = size; 540 retval = kvprintf(format, snprintf_func, &info, 10, ap); 541 if (info.remain >= 1) 542 *info.str++ = '\0'; 543 return (retval); 544 } 545 546 /* 547 * Kernel version which takes radix argument vsnprintf(3). 548 */ 549 int 550 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap) 551 { 552 struct snprintf_arg info; 553 int retval; 554 555 info.str = str; 556 info.remain = size; 557 retval = kvprintf(format, snprintf_func, &info, radix, ap); 558 if (info.remain >= 1) 559 *info.str++ = '\0'; 560 return (retval); 561 } 562 563 static void 564 snprintf_func(int ch, void *arg) 565 { 566 struct snprintf_arg *const info = arg; 567 568 if (info->remain >= 2) { 569 *info->str++ = ch; 570 info->remain--; 571 } 572 } 573 574 /* 575 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 576 * order; return an optional length and a pointer to the last character 577 * written in the buffer (i.e., the first character of the string). 578 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 579 */ 580 static char * 581 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) 582 { 583 char *p, c; 584 585 p = nbuf; 586 *p = '\0'; 587 do { 588 c = hex2ascii(num % base); 589 *++p = upper ? toupper(c) : c; 590 } while (num /= base); 591 if (lenp) 592 *lenp = p - nbuf; 593 return (p); 594 } 595 596 /* 597 * Scaled down version of printf(3). 598 * 599 * Two additional formats: 600 * 601 * The format %b is supported to decode error registers. 602 * Its usage is: 603 * 604 * printf("reg=%b\n", regval, "<base><arg>*"); 605 * 606 * where <base> is the output base expressed as a control character, e.g. 607 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 608 * the first of which gives the bit number to be inspected (origin 1), and 609 * the next characters (up to a control character, i.e. a character <= 32), 610 * give the name of the register. Thus: 611 * 612 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 613 * 614 * would produce output: 615 * 616 * reg=3<BITTWO,BITONE> 617 * 618 * XXX: %D -- Hexdump, takes pointer and separator string: 619 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 620 * ("%*D", len, ptr, " " -> XX XX XX XX ... 621 */ 622 int 623 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 624 { 625 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 626 char nbuf[MAXNBUF]; 627 char *d; 628 const char *p, *percent, *q; 629 u_char *up; 630 int ch, n; 631 uintmax_t num; 632 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 633 int cflag, hflag, jflag, tflag, zflag; 634 int dwidth, upper; 635 char padc; 636 int stop = 0, retval = 0; 637 638 num = 0; 639 if (!func) 640 d = (char *) arg; 641 else 642 d = NULL; 643 644 if (fmt == NULL) 645 fmt = "(fmt null)\n"; 646 647 if (radix < 2 || radix > 36) 648 radix = 10; 649 650 for (;;) { 651 padc = ' '; 652 width = 0; 653 while ((ch = (u_char)*fmt++) != '%' || stop) { 654 if (ch == '\0') 655 return (retval); 656 PCHAR(ch); 657 } 658 percent = fmt - 1; 659 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 660 sign = 0; dot = 0; dwidth = 0; upper = 0; 661 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; 662 reswitch: switch (ch = (u_char)*fmt++) { 663 case '.': 664 dot = 1; 665 goto reswitch; 666 case '#': 667 sharpflag = 1; 668 goto reswitch; 669 case '+': 670 sign = 1; 671 goto reswitch; 672 case '-': 673 ladjust = 1; 674 goto reswitch; 675 case '%': 676 PCHAR(ch); 677 break; 678 case '*': 679 if (!dot) { 680 width = va_arg(ap, int); 681 if (width < 0) { 682 ladjust = !ladjust; 683 width = -width; 684 } 685 } else { 686 dwidth = va_arg(ap, int); 687 } 688 goto reswitch; 689 case '0': 690 if (!dot) { 691 padc = '0'; 692 goto reswitch; 693 } 694 case '1': case '2': case '3': case '4': 695 case '5': case '6': case '7': case '8': case '9': 696 for (n = 0;; ++fmt) { 697 n = n * 10 + ch - '0'; 698 ch = *fmt; 699 if (ch < '0' || ch > '9') 700 break; 701 } 702 if (dot) 703 dwidth = n; 704 else 705 width = n; 706 goto reswitch; 707 case 'b': 708 num = (u_int)va_arg(ap, int); 709 p = va_arg(ap, char *); 710 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) 711 PCHAR(*q--); 712 713 if (num == 0) 714 break; 715 716 for (tmp = 0; *p;) { 717 n = *p++; 718 if (num & (1 << (n - 1))) { 719 PCHAR(tmp ? ',' : '<'); 720 for (; (n = *p) > ' '; ++p) 721 PCHAR(n); 722 tmp = 1; 723 } else 724 for (; *p > ' '; ++p) 725 continue; 726 } 727 if (tmp) 728 PCHAR('>'); 729 break; 730 case 'c': 731 PCHAR(va_arg(ap, int)); 732 break; 733 case 'D': 734 up = va_arg(ap, u_char *); 735 p = va_arg(ap, char *); 736 if (!width) 737 width = 16; 738 while(width--) { 739 PCHAR(hex2ascii(*up >> 4)); 740 PCHAR(hex2ascii(*up & 0x0f)); 741 up++; 742 if (width) 743 for (q=p;*q;q++) 744 PCHAR(*q); 745 } 746 break; 747 case 'd': 748 case 'i': 749 base = 10; 750 sign = 1; 751 goto handle_sign; 752 case 'h': 753 if (hflag) { 754 hflag = 0; 755 cflag = 1; 756 } else 757 hflag = 1; 758 goto reswitch; 759 case 'j': 760 jflag = 1; 761 goto reswitch; 762 case 'l': 763 if (lflag) { 764 lflag = 0; 765 qflag = 1; 766 } else 767 lflag = 1; 768 goto reswitch; 769 case 'n': 770 if (jflag) 771 *(va_arg(ap, intmax_t *)) = retval; 772 else if (qflag) 773 *(va_arg(ap, quad_t *)) = retval; 774 else if (lflag) 775 *(va_arg(ap, long *)) = retval; 776 else if (zflag) 777 *(va_arg(ap, size_t *)) = retval; 778 else if (hflag) 779 *(va_arg(ap, short *)) = retval; 780 else if (cflag) 781 *(va_arg(ap, char *)) = retval; 782 else 783 *(va_arg(ap, int *)) = retval; 784 break; 785 case 'o': 786 base = 8; 787 goto handle_nosign; 788 case 'p': 789 base = 16; 790 sharpflag = (width == 0); 791 sign = 0; 792 num = (uintptr_t)va_arg(ap, void *); 793 goto number; 794 case 'q': 795 qflag = 1; 796 goto reswitch; 797 case 'r': 798 base = radix; 799 if (sign) 800 goto handle_sign; 801 goto handle_nosign; 802 case 's': 803 p = va_arg(ap, char *); 804 if (p == NULL) 805 p = "(null)"; 806 if (!dot) 807 n = strlen (p); 808 else 809 for (n = 0; n < dwidth && p[n]; n++) 810 continue; 811 812 width -= n; 813 814 if (!ladjust && width > 0) 815 while (width--) 816 PCHAR(padc); 817 while (n--) 818 PCHAR(*p++); 819 if (ladjust && width > 0) 820 while (width--) 821 PCHAR(padc); 822 break; 823 case 't': 824 tflag = 1; 825 goto reswitch; 826 case 'u': 827 base = 10; 828 goto handle_nosign; 829 case 'X': 830 upper = 1; 831 case 'x': 832 base = 16; 833 goto handle_nosign; 834 case 'y': 835 base = 16; 836 sign = 1; 837 goto handle_sign; 838 case 'z': 839 zflag = 1; 840 goto reswitch; 841 handle_nosign: 842 sign = 0; 843 if (jflag) 844 num = va_arg(ap, uintmax_t); 845 else if (qflag) 846 num = va_arg(ap, u_quad_t); 847 else if (tflag) 848 num = va_arg(ap, ptrdiff_t); 849 else if (lflag) 850 num = va_arg(ap, u_long); 851 else if (zflag) 852 num = va_arg(ap, size_t); 853 else if (hflag) 854 num = (u_short)va_arg(ap, int); 855 else if (cflag) 856 num = (u_char)va_arg(ap, int); 857 else 858 num = va_arg(ap, u_int); 859 goto number; 860 handle_sign: 861 if (jflag) 862 num = va_arg(ap, intmax_t); 863 else if (qflag) 864 num = va_arg(ap, quad_t); 865 else if (tflag) 866 num = va_arg(ap, ptrdiff_t); 867 else if (lflag) 868 num = va_arg(ap, long); 869 else if (zflag) 870 num = va_arg(ap, ssize_t); 871 else if (hflag) 872 num = (short)va_arg(ap, int); 873 else if (cflag) 874 num = (char)va_arg(ap, int); 875 else 876 num = va_arg(ap, int); 877 number: 878 if (sign && (intmax_t)num < 0) { 879 neg = 1; 880 num = -(intmax_t)num; 881 } 882 p = ksprintn(nbuf, num, base, &n, upper); 883 tmp = 0; 884 if (sharpflag && num != 0) { 885 if (base == 8) 886 tmp++; 887 else if (base == 16) 888 tmp += 2; 889 } 890 if (neg) 891 tmp++; 892 893 if (!ladjust && padc == '0') 894 dwidth = width - tmp; 895 width -= tmp + imax(dwidth, n); 896 dwidth -= n; 897 if (!ladjust) 898 while (width-- > 0) 899 PCHAR(' '); 900 if (neg) 901 PCHAR('-'); 902 if (sharpflag && num != 0) { 903 if (base == 8) { 904 PCHAR('0'); 905 } else if (base == 16) { 906 PCHAR('0'); 907 PCHAR('x'); 908 } 909 } 910 while (dwidth-- > 0) 911 PCHAR('0'); 912 913 while (*p) 914 PCHAR(*p--); 915 916 if (ladjust) 917 while (width-- > 0) 918 PCHAR(' '); 919 920 break; 921 default: 922 while (percent < fmt) 923 PCHAR(*percent++); 924 /* 925 * Since we ignore an formatting argument it is no 926 * longer safe to obey the remaining formatting 927 * arguments as the arguments will no longer match 928 * the format specs. 929 */ 930 stop = 1; 931 break; 932 } 933 } 934 #undef PCHAR 935 } 936 937 /* 938 * Put character in log buffer with a particular priority. 939 */ 940 static void 941 msglogchar(int c, int pri) 942 { 943 static int lastpri = -1; 944 static int dangling; 945 char nbuf[MAXNBUF]; 946 char *p; 947 948 if (!msgbufmapped) 949 return; 950 if (c == '\0' || c == '\r') 951 return; 952 if (pri != -1 && pri != lastpri) { 953 if (dangling) { 954 msgbuf_addchar(msgbufp, '\n'); 955 dangling = 0; 956 } 957 msgbuf_addchar(msgbufp, '<'); 958 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;) 959 msgbuf_addchar(msgbufp, *p--); 960 msgbuf_addchar(msgbufp, '>'); 961 lastpri = pri; 962 } 963 msgbuf_addchar(msgbufp, c); 964 if (c == '\n') { 965 dangling = 0; 966 lastpri = -1; 967 } else { 968 dangling = 1; 969 } 970 } 971 972 static void 973 msglogstr(char *str, int pri, int filter_cr) 974 { 975 if (!msgbufmapped) 976 return; 977 978 msgbuf_addstr(msgbufp, pri, str, filter_cr); 979 } 980 981 void 982 msgbufinit(void *ptr, int size) 983 { 984 char *cp; 985 static struct msgbuf *oldp = NULL; 986 987 size -= sizeof(*msgbufp); 988 cp = (char *)ptr; 989 msgbufp = (struct msgbuf *)(cp + size); 990 msgbuf_reinit(msgbufp, cp, size); 991 if (msgbufmapped && oldp != msgbufp) 992 msgbuf_copy(oldp, msgbufp); 993 msgbufmapped = 1; 994 oldp = msgbufp; 995 } 996 997 static int unprivileged_read_msgbuf = 1; 998 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf, 999 CTLFLAG_RW, &unprivileged_read_msgbuf, 0, 1000 "Unprivileged processes may read the kernel message buffer"); 1001 1002 /* Sysctls for accessing/clearing the msgbuf */ 1003 static int 1004 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 1005 { 1006 char buf[128]; 1007 u_int seq; 1008 int error, len; 1009 1010 if (!unprivileged_read_msgbuf) { 1011 error = priv_check(req->td, PRIV_MSGBUF); 1012 if (error) 1013 return (error); 1014 } 1015 1016 /* Read the whole buffer, one chunk at a time. */ 1017 mtx_lock(&msgbuf_lock); 1018 msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 1019 for (;;) { 1020 len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq); 1021 mtx_unlock(&msgbuf_lock); 1022 if (len == 0) 1023 return (0); 1024 1025 error = sysctl_handle_opaque(oidp, buf, len, req); 1026 if (error) 1027 return (error); 1028 1029 mtx_lock(&msgbuf_lock); 1030 } 1031 } 1032 1033 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, 1034 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1035 NULL, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 1036 1037 static int msgbuf_clearflag; 1038 1039 static int 1040 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 1041 { 1042 int error; 1043 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 1044 if (!error && req->newptr) { 1045 mtx_lock(&msgbuf_lock); 1046 msgbuf_clear(msgbufp); 1047 mtx_unlock(&msgbuf_lock); 1048 msgbuf_clearflag = 0; 1049 } 1050 return (error); 1051 } 1052 1053 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 1054 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, 1055 &msgbuf_clearflag, 0, sysctl_kern_msgbuf_clear, "I", 1056 "Clear kernel message buffer"); 1057 1058 #ifdef DDB 1059 1060 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 1061 { 1062 int i, j; 1063 1064 if (!msgbufmapped) { 1065 db_printf("msgbuf not mapped yet\n"); 1066 return; 1067 } 1068 db_printf("msgbufp = %p\n", msgbufp); 1069 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n", 1070 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq, 1071 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum); 1072 for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) { 1073 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq); 1074 db_printf("%c", msgbufp->msg_ptr[j]); 1075 } 1076 db_printf("\n"); 1077 } 1078 1079 #endif /* DDB */ 1080 1081 void 1082 hexdump(const void *ptr, int length, const char *hdr, int flags) 1083 { 1084 int i, j, k; 1085 int cols; 1086 const unsigned char *cp; 1087 char delim; 1088 1089 if ((flags & HD_DELIM_MASK) != 0) 1090 delim = (flags & HD_DELIM_MASK) >> 8; 1091 else 1092 delim = ' '; 1093 1094 if ((flags & HD_COLUMN_MASK) != 0) 1095 cols = flags & HD_COLUMN_MASK; 1096 else 1097 cols = 16; 1098 1099 cp = ptr; 1100 for (i = 0; i < length; i+= cols) { 1101 if (hdr != NULL) 1102 printf("%s", hdr); 1103 1104 if ((flags & HD_OMIT_COUNT) == 0) 1105 printf("%04x ", i); 1106 1107 if ((flags & HD_OMIT_HEX) == 0) { 1108 for (j = 0; j < cols; j++) { 1109 k = i + j; 1110 if (k < length) 1111 printf("%c%02x", delim, cp[k]); 1112 else 1113 printf(" "); 1114 } 1115 } 1116 1117 if ((flags & HD_OMIT_CHARS) == 0) { 1118 printf(" |"); 1119 for (j = 0; j < cols; j++) { 1120 k = i + j; 1121 if (k >= length) 1122 printf(" "); 1123 else if (cp[k] >= ' ' && cp[k] <= '~') 1124 printf("%c", cp[k]); 1125 else 1126 printf("."); 1127 } 1128 printf("|"); 1129 } 1130 printf("\n"); 1131 } 1132 } 1133 1134