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