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