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