| 1 | /* $NetBSD: subr_prf.c,v 1.181 2019/05/21 04:57:02 christos Exp $ */ |
| 2 | |
| 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 | * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95 |
| 37 | */ |
| 38 | |
| 39 | #include <sys/cdefs.h> |
| 40 | __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.181 2019/05/21 04:57:02 christos Exp $" ); |
| 41 | |
| 42 | #ifdef _KERNEL_OPT |
| 43 | #include "opt_ddb.h" |
| 44 | #include "opt_kgdb.h" |
| 45 | #include "opt_dump.h" |
| 46 | #include "opt_rnd_printf.h" |
| 47 | #endif |
| 48 | |
| 49 | #include <sys/param.h> |
| 50 | #include <sys/stdint.h> |
| 51 | #include <sys/systm.h> |
| 52 | #include <sys/buf.h> |
| 53 | #include <sys/device.h> |
| 54 | #include <sys/reboot.h> |
| 55 | #include <sys/msgbuf.h> |
| 56 | #include <sys/proc.h> |
| 57 | #include <sys/ioctl.h> |
| 58 | #include <sys/vnode.h> |
| 59 | #include <sys/file.h> |
| 60 | #include <sys/tty.h> |
| 61 | #include <sys/tprintf.h> |
| 62 | #include <sys/spldebug.h> |
| 63 | #include <sys/syslog.h> |
| 64 | #include <sys/kprintf.h> |
| 65 | #include <sys/atomic.h> |
| 66 | #include <sys/kernel.h> |
| 67 | #include <sys/cpu.h> |
| 68 | #include <sys/sha2.h> |
| 69 | #include <sys/rndsource.h> |
| 70 | #include <sys/kmem.h> |
| 71 | |
| 72 | #include <dev/cons.h> |
| 73 | |
| 74 | #include <net/if.h> |
| 75 | |
| 76 | static kmutex_t kprintf_mtx; |
| 77 | static bool kprintf_inited = false; |
| 78 | |
| 79 | #ifdef KGDB |
| 80 | #include <sys/kgdb.h> |
| 81 | #endif |
| 82 | |
| 83 | #ifdef DDB |
| 84 | #include <ddb/ddbvar.h> /* db_panic */ |
| 85 | #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ |
| 86 | #endif |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * defines |
| 91 | */ |
| 92 | #define KLOG_PRI 0x80000000 |
| 93 | |
| 94 | |
| 95 | /* |
| 96 | * local prototypes |
| 97 | */ |
| 98 | |
| 99 | static void putchar(int, int, struct tty *); |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * globals |
| 104 | */ |
| 105 | |
| 106 | extern struct tty *constty; /* pointer to console "window" tty */ |
| 107 | extern int log_open; /* subr_log: is /dev/klog open? */ |
| 108 | extern krndsource_t rnd_printf_source; |
| 109 | const char *panicstr; /* arg to first call to panic (used as a flag |
| 110 | to indicate that panic has already been called). */ |
| 111 | struct cpu_info *paniccpu; /* cpu that first paniced */ |
| 112 | long panicstart, panicend; /* position in the msgbuf of the start and |
| 113 | end of the formatted panicstr. */ |
| 114 | int doing_shutdown; /* set to indicate shutdown in progress */ |
| 115 | |
| 116 | #ifdef RND_PRINTF |
| 117 | static bool kprintf_inited_callout = false; |
| 118 | static SHA512_CTX kprnd_sha; |
| 119 | static uint8_t kprnd_accum[SHA512_DIGEST_LENGTH]; |
| 120 | static int kprnd_added; |
| 121 | |
| 122 | static struct callout kprnd_callout; |
| 123 | #endif |
| 124 | |
| 125 | #ifndef DUMP_ON_PANIC |
| 126 | #define DUMP_ON_PANIC 1 |
| 127 | #endif |
| 128 | int dumponpanic = DUMP_ON_PANIC; |
| 129 | |
| 130 | /* |
| 131 | * v_putc: routine to putc on virtual console |
| 132 | * |
| 133 | * the v_putc pointer can be used to redirect the console cnputc elsewhere |
| 134 | * [e.g. to a "virtual console"]. |
| 135 | */ |
| 136 | |
| 137 | void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */ |
| 138 | void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */ |
| 139 | |
| 140 | const char hexdigits[] = "0123456789abcdef" ; |
| 141 | const char HEXDIGITS[] = "0123456789ABCDEF" ; |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | * functions |
| 146 | */ |
| 147 | |
| 148 | #ifdef RND_PRINTF |
| 149 | static void kprintf_rnd_get(size_t bytes, void *priv) |
| 150 | { |
| 151 | if (kprnd_added) { |
| 152 | KASSERT(kprintf_inited); |
| 153 | if (mutex_tryenter(&kprintf_mtx)) { |
| 154 | SHA512_Final(kprnd_accum, &kprnd_sha); |
| 155 | rnd_add_data(&rnd_printf_source, |
| 156 | kprnd_accum, sizeof(kprnd_accum), 0); |
| 157 | kprnd_added = 0; |
| 158 | /* This, we must do, since we called _Final. */ |
| 159 | SHA512_Init(&kprnd_sha); |
| 160 | /* This is optional but seems useful. */ |
| 161 | SHA512_Update(&kprnd_sha, kprnd_accum, |
| 162 | sizeof(kprnd_accum)); |
| 163 | mutex_exit(&kprintf_mtx); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void kprintf_rnd_callout(void *arg) |
| 169 | { |
| 170 | kprintf_rnd_get(0, NULL); |
| 171 | callout_schedule(&kprnd_callout, hz); |
| 172 | } |
| 173 | |
| 174 | #endif |
| 175 | |
| 176 | /* |
| 177 | * Locking is inited fairly early in MI bootstrap. Before that |
| 178 | * prints are done unlocked. But that doesn't really matter, |
| 179 | * since nothing can preempt us before interrupts are enabled. |
| 180 | */ |
| 181 | void |
| 182 | kprintf_init(void) |
| 183 | { |
| 184 | |
| 185 | KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */ |
| 186 | #ifdef RND_PRINTF |
| 187 | SHA512_Init(&kprnd_sha); |
| 188 | #endif |
| 189 | mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH); |
| 190 | kprintf_inited = true; |
| 191 | } |
| 192 | |
| 193 | #ifdef RND_PRINTF |
| 194 | void |
| 195 | kprintf_init_callout(void) |
| 196 | { |
| 197 | KASSERT(!kprintf_inited_callout); |
| 198 | callout_init(&kprnd_callout, CALLOUT_MPSAFE); |
| 199 | callout_setfunc(&kprnd_callout, kprintf_rnd_callout, NULL); |
| 200 | callout_schedule(&kprnd_callout, hz); |
| 201 | kprintf_inited_callout = true; |
| 202 | } |
| 203 | #endif |
| 204 | |
| 205 | void |
| 206 | kprintf_lock(void) |
| 207 | { |
| 208 | |
| 209 | if (__predict_true(kprintf_inited)) |
| 210 | mutex_enter(&kprintf_mtx); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | kprintf_unlock(void) |
| 215 | { |
| 216 | |
| 217 | if (__predict_true(kprintf_inited)) { |
| 218 | /* assert kprintf wasn't somehow inited while we were in */ |
| 219 | KASSERT(mutex_owned(&kprintf_mtx)); |
| 220 | mutex_exit(&kprintf_mtx); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * twiddle: spin a little propellor on the console. |
| 226 | */ |
| 227 | |
| 228 | void |
| 229 | twiddle(void) |
| 230 | { |
| 231 | static const char twiddle_chars[] = "|/-\\" ; |
| 232 | static int pos; |
| 233 | |
| 234 | kprintf_lock(); |
| 235 | |
| 236 | putchar(twiddle_chars[pos++ & 3], TOCONS|NOTSTAMP, NULL); |
| 237 | putchar('\b', TOCONS|NOTSTAMP, NULL); |
| 238 | |
| 239 | kprintf_unlock(); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * panic: handle an unresolvable fatal error |
| 244 | * |
| 245 | * prints "panic: <message>" and reboots. if called twice (i.e. recursive |
| 246 | * call) we avoid trying to dump and just reboot (to avoid recursive panics). |
| 247 | */ |
| 248 | |
| 249 | void |
| 250 | panic(const char *fmt, ...) |
| 251 | { |
| 252 | va_list ap; |
| 253 | |
| 254 | va_start(ap, fmt); |
| 255 | vpanic(fmt, ap); |
| 256 | va_end(ap); |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | vpanic(const char *fmt, va_list ap) |
| 261 | { |
| 262 | CPU_INFO_ITERATOR cii; |
| 263 | struct cpu_info *ci, *oci; |
| 264 | int bootopt; |
| 265 | static char scratchstr[384]; /* stores panic message */ |
| 266 | |
| 267 | spldebug_stop(); |
| 268 | |
| 269 | if (lwp0.l_cpu && curlwp) { |
| 270 | /* |
| 271 | * Disable preemption. If already panicing on another CPU, sit |
| 272 | * here and spin until the system is rebooted. Allow the CPU that |
| 273 | * first paniced to panic again. |
| 274 | */ |
| 275 | kpreempt_disable(); |
| 276 | ci = curcpu(); |
| 277 | oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci); |
| 278 | if (oci != NULL && oci != ci) { |
| 279 | /* Give interrupts a chance to try and prevent deadlock. */ |
| 280 | for (;;) { |
| 281 | #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */ |
| 282 | DELAY(10); |
| 283 | #endif /* _RUMPKERNEL */ |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Convert the current thread to a bound thread and prevent all |
| 289 | * CPUs from scheduling unbound jobs. Do so without taking any |
| 290 | * locks. |
| 291 | */ |
| 292 | curlwp->l_pflag |= LP_BOUND; |
| 293 | for (CPU_INFO_FOREACH(cii, ci)) { |
| 294 | ci->ci_schedstate.spc_flags |= SPCF_OFFLINE; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | bootopt = RB_AUTOBOOT | RB_NOSYNC; |
| 299 | if (!doing_shutdown) { |
| 300 | if (dumponpanic) |
| 301 | bootopt |= RB_DUMP; |
| 302 | } else |
| 303 | printf("Skipping crash dump on recursive panic\n" ); |
| 304 | |
| 305 | doing_shutdown = 1; |
| 306 | |
| 307 | if (logenabled(msgbufp)) |
| 308 | panicstart = msgbufp->msg_bufx; |
| 309 | |
| 310 | printf("panic: " ); |
| 311 | if (panicstr == NULL) { |
| 312 | /* first time in panic - store fmt first for precaution */ |
| 313 | panicstr = fmt; |
| 314 | |
| 315 | vsnprintf(scratchstr, sizeof(scratchstr), fmt, ap); |
| 316 | printf("%s" , scratchstr); |
| 317 | panicstr = scratchstr; |
| 318 | } else { |
| 319 | vprintf(fmt, ap); |
| 320 | } |
| 321 | printf("\n" ); |
| 322 | |
| 323 | if (logenabled(msgbufp)) |
| 324 | panicend = msgbufp->msg_bufx; |
| 325 | |
| 326 | #ifdef KGDB |
| 327 | kgdb_panic(); |
| 328 | #endif |
| 329 | #ifdef KADB |
| 330 | if (boothowto & RB_KDB) |
| 331 | kdbpanic(); |
| 332 | #endif |
| 333 | #ifdef DDB |
| 334 | db_panic(); |
| 335 | #endif |
| 336 | cpu_reboot(bootopt, NULL); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * kernel logging functions: log, logpri, addlog |
| 341 | */ |
| 342 | |
| 343 | /* |
| 344 | * log: write to the log buffer |
| 345 | * |
| 346 | * => will not sleep [so safe to call from interrupt] |
| 347 | * => will log to console if /dev/klog isn't open |
| 348 | */ |
| 349 | |
| 350 | void |
| 351 | log(int level, const char *fmt, ...) |
| 352 | { |
| 353 | va_list ap; |
| 354 | |
| 355 | kprintf_lock(); |
| 356 | |
| 357 | klogpri(level); /* log the level first */ |
| 358 | va_start(ap, fmt); |
| 359 | kprintf(fmt, TOLOG, NULL, NULL, ap); |
| 360 | va_end(ap); |
| 361 | if (!log_open) { |
| 362 | va_start(ap, fmt); |
| 363 | kprintf(fmt, TOCONS, NULL, NULL, ap); |
| 364 | va_end(ap); |
| 365 | } |
| 366 | |
| 367 | kprintf_unlock(); |
| 368 | |
| 369 | logwakeup(); /* wake up anyone waiting for log msgs */ |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * vlog: write to the log buffer [already have va_list] |
| 374 | */ |
| 375 | |
| 376 | void |
| 377 | vlog(int level, const char *fmt, va_list ap) |
| 378 | { |
| 379 | va_list cap; |
| 380 | |
| 381 | va_copy(cap, ap); |
| 382 | kprintf_lock(); |
| 383 | |
| 384 | klogpri(level); /* log the level first */ |
| 385 | kprintf(fmt, TOLOG, NULL, NULL, ap); |
| 386 | if (!log_open) |
| 387 | kprintf(fmt, TOCONS, NULL, NULL, cap); |
| 388 | |
| 389 | kprintf_unlock(); |
| 390 | va_end(cap); |
| 391 | |
| 392 | logwakeup(); /* wake up anyone waiting for log msgs */ |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * logpri: log the priority level to the klog |
| 397 | */ |
| 398 | |
| 399 | void |
| 400 | logpri(int level) |
| 401 | { |
| 402 | |
| 403 | kprintf_lock(); |
| 404 | klogpri(level); |
| 405 | kprintf_unlock(); |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Note: we must be in the mutex here! |
| 410 | */ |
| 411 | void |
| 412 | klogpri(int level) |
| 413 | { |
| 414 | KASSERT((level & KLOG_PRI) == 0); |
| 415 | |
| 416 | putchar(level | KLOG_PRI, TOLOG, NULL); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * addlog: add info to previous log message |
| 421 | */ |
| 422 | |
| 423 | void |
| 424 | addlog(const char *fmt, ...) |
| 425 | { |
| 426 | va_list ap; |
| 427 | |
| 428 | kprintf_lock(); |
| 429 | |
| 430 | va_start(ap, fmt); |
| 431 | kprintf(fmt, TOLOG, NULL, NULL, ap); |
| 432 | va_end(ap); |
| 433 | if (!log_open) { |
| 434 | va_start(ap, fmt); |
| 435 | kprintf(fmt, TOCONS, NULL, NULL, ap); |
| 436 | va_end(ap); |
| 437 | } |
| 438 | |
| 439 | kprintf_unlock(); |
| 440 | |
| 441 | logwakeup(); |
| 442 | } |
| 443 | |
| 444 | static void |
| 445 | putone(int c, int flags, struct tty *tp) |
| 446 | { |
| 447 | if (panicstr) |
| 448 | constty = NULL; |
| 449 | |
| 450 | if ((flags & TOCONS) && tp == NULL && constty) { |
| 451 | tp = constty; |
| 452 | flags |= TOTTY; |
| 453 | } |
| 454 | if ((flags & TOTTY) && tp && |
| 455 | tputchar(c, flags, tp) < 0 && |
| 456 | (flags & TOCONS) && tp == constty) |
| 457 | constty = NULL; |
| 458 | if ((flags & TOLOG) && |
| 459 | c != '\0' && c != '\r' && c != 0177) |
| 460 | logputchar(c); |
| 461 | if ((flags & TOCONS) && constty == NULL && c != '\0') |
| 462 | (*v_putc)(c); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | putlogpri(int level) |
| 467 | { |
| 468 | char *p; |
| 469 | char snbuf[KPRINTF_BUFSIZE]; |
| 470 | |
| 471 | putone('<', TOLOG, NULL); |
| 472 | snprintf(snbuf, sizeof(snbuf), "%d" , level); |
| 473 | for (p = snbuf ; *p ; p++) |
| 474 | putone(*p, TOLOG, NULL); |
| 475 | putone('>', TOLOG, NULL); |
| 476 | } |
| 477 | |
| 478 | #ifndef KLOG_NOTIMESTAMP |
| 479 | static int needtstamp = 1; |
| 480 | int log_ts_prec = 7; |
| 481 | |
| 482 | static void |
| 483 | addtstamp(int flags, struct tty *tp) |
| 484 | { |
| 485 | char buf[64]; |
| 486 | struct timespec ts; |
| 487 | int n, prec; |
| 488 | long fsec; |
| 489 | |
| 490 | prec = log_ts_prec; |
| 491 | if (prec < 0) { |
| 492 | prec = 0; |
| 493 | log_ts_prec = prec; |
| 494 | } else if (prec > 9) { |
| 495 | prec = 9; |
| 496 | log_ts_prec = prec; |
| 497 | } |
| 498 | |
| 499 | getnanouptime(&ts); |
| 500 | |
| 501 | for (n = prec, fsec = ts.tv_nsec; n < 8; n++) |
| 502 | fsec /= 10; |
| 503 | if (n < 9) |
| 504 | fsec = (fsec / 10) + ((fsec % 10) >= 5); |
| 505 | |
| 506 | n = snprintf(buf, sizeof(buf), "[% 4jd.%.*ld] " , |
| 507 | (intmax_t)ts.tv_sec, prec, fsec); |
| 508 | |
| 509 | for (int i = 0; i < n; i++) |
| 510 | putone(buf[i], flags, tp); |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | /* |
| 515 | * putchar: print a single character on console or user terminal. |
| 516 | * |
| 517 | * => if console, then the last MSGBUFS chars are saved in msgbuf |
| 518 | * for inspection later (e.g. dmesg/syslog) |
| 519 | * => we must already be in the mutex! |
| 520 | */ |
| 521 | static void |
| 522 | putchar(int c, int flags, struct tty *tp) |
| 523 | { |
| 524 | if (c & KLOG_PRI) { |
| 525 | putlogpri(c & ~KLOG_PRI); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | #ifndef KLOG_NOTIMESTAMP |
| 530 | if (c != '\0' && c != '\n' && needtstamp && (flags & NOTSTAMP) == 0) { |
| 531 | addtstamp(flags, tp); |
| 532 | needtstamp = 0; |
| 533 | } |
| 534 | |
| 535 | if (c == '\n') |
| 536 | needtstamp = 1; |
| 537 | #endif |
| 538 | putone(c, flags, tp); |
| 539 | |
| 540 | #ifdef DDB |
| 541 | if (flags & TODDB) { |
| 542 | db_putchar(c); |
| 543 | return; |
| 544 | } |
| 545 | #endif |
| 546 | |
| 547 | #ifdef RND_PRINTF |
| 548 | if (__predict_true(kprintf_inited)) { |
| 549 | static uint8_t rbuf[SHA512_BLOCK_LENGTH]; |
| 550 | static int cursor; |
| 551 | |
| 552 | rbuf[cursor] = c; |
| 553 | if (cursor == sizeof(rbuf) - 1) { |
| 554 | SHA512_Update(&kprnd_sha, rbuf, sizeof(rbuf)); |
| 555 | kprnd_added++; |
| 556 | cursor = 0; |
| 557 | } else { |
| 558 | cursor++; |
| 559 | } |
| 560 | } |
| 561 | #endif |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * tablefull: warn that a system table is full |
| 566 | */ |
| 567 | |
| 568 | void |
| 569 | tablefull(const char *tab, const char *hint) |
| 570 | { |
| 571 | if (hint) |
| 572 | log(LOG_ERR, "%s: table is full - %s\n" , tab, hint); |
| 573 | else |
| 574 | log(LOG_ERR, "%s: table is full\n" , tab); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /* |
| 579 | * uprintf: print to the controlling tty of the current process |
| 580 | * |
| 581 | * => we may block if the tty queue is full |
| 582 | * => no message is printed if the queue doesn't clear in a reasonable |
| 583 | * time |
| 584 | */ |
| 585 | |
| 586 | void |
| 587 | uprintf(const char *fmt, ...) |
| 588 | { |
| 589 | struct proc *p = curproc; |
| 590 | va_list ap; |
| 591 | |
| 592 | /* mutex_enter(proc_lock); XXXSMP */ |
| 593 | |
| 594 | if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { |
| 595 | /* No mutex needed; going to process TTY. */ |
| 596 | va_start(ap, fmt); |
| 597 | kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); |
| 598 | va_end(ap); |
| 599 | } |
| 600 | |
| 601 | /* mutex_exit(proc_lock); XXXSMP */ |
| 602 | } |
| 603 | |
| 604 | void |
| 605 | uprintf_locked(const char *fmt, ...) |
| 606 | { |
| 607 | struct proc *p = curproc; |
| 608 | va_list ap; |
| 609 | |
| 610 | if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { |
| 611 | /* No mutex needed; going to process TTY. */ |
| 612 | va_start(ap, fmt); |
| 613 | kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); |
| 614 | va_end(ap); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * tprintf functions: used to send messages to a specific process |
| 620 | * |
| 621 | * usage: |
| 622 | * get a tpr_t handle on a process "p" by using "tprintf_open(p)" |
| 623 | * use the handle when calling "tprintf" |
| 624 | * when done, do a "tprintf_close" to drop the handle |
| 625 | */ |
| 626 | |
| 627 | /* |
| 628 | * tprintf_open: get a tprintf handle on a process "p" |
| 629 | * |
| 630 | * => returns NULL if process can't be printed to |
| 631 | */ |
| 632 | |
| 633 | tpr_t |
| 634 | tprintf_open(struct proc *p) |
| 635 | { |
| 636 | tpr_t cookie; |
| 637 | |
| 638 | cookie = NULL; |
| 639 | |
| 640 | mutex_enter(proc_lock); |
| 641 | if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { |
| 642 | proc_sesshold(p->p_session); |
| 643 | cookie = (tpr_t)p->p_session; |
| 644 | } |
| 645 | mutex_exit(proc_lock); |
| 646 | |
| 647 | return cookie; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * tprintf_close: dispose of a tprintf handle obtained with tprintf_open |
| 652 | */ |
| 653 | |
| 654 | void |
| 655 | tprintf_close(tpr_t sess) |
| 656 | { |
| 657 | |
| 658 | if (sess) { |
| 659 | mutex_enter(proc_lock); |
| 660 | /* Releases proc_lock. */ |
| 661 | proc_sessrele((struct session *)sess); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * tprintf: given tprintf handle to a process [obtained with tprintf_open], |
| 667 | * send a message to the controlling tty for that process. |
| 668 | * |
| 669 | * => also sends message to /dev/klog |
| 670 | */ |
| 671 | void |
| 672 | tprintf(tpr_t tpr, const char *fmt, ...) |
| 673 | { |
| 674 | struct session *sess = (struct session *)tpr; |
| 675 | struct tty *tp = NULL; |
| 676 | int flags = TOLOG; |
| 677 | va_list ap; |
| 678 | |
| 679 | /* mutex_enter(proc_lock); XXXSMP */ |
| 680 | if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { |
| 681 | flags |= TOTTY; |
| 682 | tp = sess->s_ttyp; |
| 683 | } |
| 684 | |
| 685 | kprintf_lock(); |
| 686 | |
| 687 | klogpri(LOG_INFO); |
| 688 | va_start(ap, fmt); |
| 689 | kprintf(fmt, flags, tp, NULL, ap); |
| 690 | va_end(ap); |
| 691 | |
| 692 | kprintf_unlock(); |
| 693 | /* mutex_exit(proc_lock); XXXSMP */ |
| 694 | |
| 695 | logwakeup(); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /* |
| 700 | * ttyprintf: send a message to a specific tty |
| 701 | * |
| 702 | * => should be used only by tty driver or anything that knows the |
| 703 | * underlying tty will not be revoked(2)'d away. [otherwise, |
| 704 | * use tprintf] |
| 705 | */ |
| 706 | void |
| 707 | ttyprintf(struct tty *tp, const char *fmt, ...) |
| 708 | { |
| 709 | va_list ap; |
| 710 | |
| 711 | /* No mutex needed; going to process TTY. */ |
| 712 | va_start(ap, fmt); |
| 713 | kprintf(fmt, TOTTY, tp, NULL, ap); |
| 714 | va_end(ap); |
| 715 | } |
| 716 | |
| 717 | #ifdef DDB |
| 718 | |
| 719 | /* |
| 720 | * db_printf: printf for DDB (via db_putchar) |
| 721 | */ |
| 722 | |
| 723 | void |
| 724 | db_printf(const char *fmt, ...) |
| 725 | { |
| 726 | va_list ap; |
| 727 | |
| 728 | /* No mutex needed; DDB pauses all processors. */ |
| 729 | va_start(ap, fmt); |
| 730 | kprintf(fmt, TODDB, NULL, NULL, ap); |
| 731 | va_end(ap); |
| 732 | |
| 733 | if (db_tee_msgbuf) { |
| 734 | va_start(ap, fmt); |
| 735 | kprintf(fmt, TOLOG, NULL, NULL, ap); |
| 736 | va_end(ap); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void |
| 741 | db_vprintf(const char *fmt, va_list ap) |
| 742 | { |
| 743 | va_list cap; |
| 744 | |
| 745 | va_copy(cap, ap); |
| 746 | /* No mutex needed; DDB pauses all processors. */ |
| 747 | kprintf(fmt, TODDB, NULL, NULL, ap); |
| 748 | if (db_tee_msgbuf) |
| 749 | kprintf(fmt, TOLOG, NULL, NULL, cap); |
| 750 | va_end(cap); |
| 751 | } |
| 752 | |
| 753 | #endif /* DDB */ |
| 754 | |
| 755 | static void |
| 756 | kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...) |
| 757 | { |
| 758 | va_list ap; |
| 759 | |
| 760 | va_start(ap, sbuf); |
| 761 | (void)kprintf(fmt, oflags, vp, sbuf, ap); |
| 762 | va_end(ap); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Device autoconfiguration printf routines. These change their |
| 767 | * behavior based on the AB_* flags in boothowto. If AB_SILENT |
| 768 | * is set, messages never go to the console (but they still always |
| 769 | * go to the log). AB_VERBOSE overrides AB_SILENT. |
| 770 | */ |
| 771 | |
| 772 | /* |
| 773 | * aprint_normal: Send to console unless AB_QUIET. Always goes |
| 774 | * to the log. |
| 775 | */ |
| 776 | static void |
| 777 | aprint_normal_internal(const char *prefix, const char *fmt, va_list ap) |
| 778 | { |
| 779 | int flags = TOLOG; |
| 780 | |
| 781 | if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || |
| 782 | (boothowto & AB_VERBOSE) != 0) |
| 783 | flags |= TOCONS; |
| 784 | |
| 785 | kprintf_lock(); |
| 786 | |
| 787 | if (prefix) |
| 788 | kprintf_internal("%s: " , flags, NULL, NULL, prefix); |
| 789 | kprintf(fmt, flags, NULL, NULL, ap); |
| 790 | |
| 791 | kprintf_unlock(); |
| 792 | |
| 793 | if (!panicstr) |
| 794 | logwakeup(); |
| 795 | } |
| 796 | |
| 797 | void |
| 798 | aprint_normal(const char *fmt, ...) |
| 799 | { |
| 800 | va_list ap; |
| 801 | |
| 802 | va_start(ap, fmt); |
| 803 | aprint_normal_internal(NULL, fmt, ap); |
| 804 | va_end(ap); |
| 805 | } |
| 806 | |
| 807 | void |
| 808 | aprint_normal_dev(device_t dv, const char *fmt, ...) |
| 809 | { |
| 810 | va_list ap; |
| 811 | |
| 812 | KASSERT(dv != NULL); |
| 813 | |
| 814 | va_start(ap, fmt); |
| 815 | aprint_normal_internal(device_xname(dv), fmt, ap); |
| 816 | va_end(ap); |
| 817 | } |
| 818 | |
| 819 | void |
| 820 | aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...) |
| 821 | { |
| 822 | va_list ap; |
| 823 | |
| 824 | KASSERT(ifp != NULL); |
| 825 | |
| 826 | va_start(ap, fmt); |
| 827 | aprint_normal_internal(ifp->if_xname, fmt, ap); |
| 828 | va_end(ap); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * aprint_error: Send to console unless AB_QUIET. Always goes |
| 833 | * to the log. Also counts the number of times called so other |
| 834 | * parts of the kernel can report the number of errors during a |
| 835 | * given phase of system startup. |
| 836 | */ |
| 837 | static int aprint_error_count; |
| 838 | |
| 839 | int |
| 840 | aprint_get_error_count(void) |
| 841 | { |
| 842 | int count; |
| 843 | |
| 844 | kprintf_lock(); |
| 845 | |
| 846 | count = aprint_error_count; |
| 847 | aprint_error_count = 0; |
| 848 | |
| 849 | kprintf_unlock(); |
| 850 | |
| 851 | return (count); |
| 852 | } |
| 853 | |
| 854 | static void |
| 855 | aprint_error_internal(const char *prefix, const char *fmt, va_list ap) |
| 856 | { |
| 857 | int flags = TOLOG; |
| 858 | |
| 859 | if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || |
| 860 | (boothowto & AB_VERBOSE) != 0) |
| 861 | flags |= TOCONS; |
| 862 | |
| 863 | kprintf_lock(); |
| 864 | |
| 865 | aprint_error_count++; |
| 866 | |
| 867 | if (prefix) |
| 868 | kprintf_internal("%s: " , flags, NULL, NULL, prefix); |
| 869 | kprintf_internal("autoconfiguration error: " , TOLOG, NULL, NULL); |
| 870 | kprintf(fmt, flags, NULL, NULL, ap); |
| 871 | |
| 872 | kprintf_unlock(); |
| 873 | |
| 874 | if (!panicstr) |
| 875 | logwakeup(); |
| 876 | } |
| 877 | |
| 878 | void |
| 879 | aprint_error(const char *fmt, ...) |
| 880 | { |
| 881 | va_list ap; |
| 882 | |
| 883 | va_start(ap, fmt); |
| 884 | aprint_error_internal(NULL, fmt, ap); |
| 885 | va_end(ap); |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | aprint_error_dev(device_t dv, const char *fmt, ...) |
| 890 | { |
| 891 | va_list ap; |
| 892 | |
| 893 | KASSERT(dv != NULL); |
| 894 | |
| 895 | va_start(ap, fmt); |
| 896 | aprint_error_internal(device_xname(dv), fmt, ap); |
| 897 | va_end(ap); |
| 898 | } |
| 899 | |
| 900 | void |
| 901 | aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...) |
| 902 | { |
| 903 | va_list ap; |
| 904 | |
| 905 | KASSERT(ifp != NULL); |
| 906 | |
| 907 | va_start(ap, fmt); |
| 908 | aprint_error_internal(ifp->if_xname, fmt, ap); |
| 909 | va_end(ap); |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * aprint_naive: Send to console only if AB_QUIET. Never goes |
| 914 | * to the log. |
| 915 | */ |
| 916 | static void |
| 917 | aprint_naive_internal(const char *prefix, const char *fmt, va_list ap) |
| 918 | { |
| 919 | if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET) |
| 920 | return; |
| 921 | |
| 922 | kprintf_lock(); |
| 923 | |
| 924 | if (prefix) |
| 925 | kprintf_internal("%s: " , TOCONS, NULL, NULL, prefix); |
| 926 | kprintf(fmt, TOCONS, NULL, NULL, ap); |
| 927 | |
| 928 | kprintf_unlock(); |
| 929 | } |
| 930 | |
| 931 | void |
| 932 | aprint_naive(const char *fmt, ...) |
| 933 | { |
| 934 | va_list ap; |
| 935 | |
| 936 | va_start(ap, fmt); |
| 937 | aprint_naive_internal(NULL, fmt, ap); |
| 938 | va_end(ap); |
| 939 | } |
| 940 | |
| 941 | void |
| 942 | aprint_naive_dev(device_t dv, const char *fmt, ...) |
| 943 | { |
| 944 | va_list ap; |
| 945 | |
| 946 | KASSERT(dv != NULL); |
| 947 | |
| 948 | va_start(ap, fmt); |
| 949 | aprint_naive_internal(device_xname(dv), fmt, ap); |
| 950 | va_end(ap); |
| 951 | } |
| 952 | |
| 953 | void |
| 954 | aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...) |
| 955 | { |
| 956 | va_list ap; |
| 957 | |
| 958 | KASSERT(ifp != NULL); |
| 959 | |
| 960 | va_start(ap, fmt); |
| 961 | aprint_naive_internal(ifp->if_xname, fmt, ap); |
| 962 | va_end(ap); |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * aprint_verbose: Send to console only if AB_VERBOSE. Always |
| 967 | * goes to the log. |
| 968 | */ |
| 969 | static void |
| 970 | aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap) |
| 971 | { |
| 972 | int flags = TOLOG; |
| 973 | |
| 974 | if (boothowto & AB_VERBOSE) |
| 975 | flags |= TOCONS; |
| 976 | |
| 977 | kprintf_lock(); |
| 978 | |
| 979 | if (prefix) |
| 980 | kprintf_internal("%s: " , flags, NULL, NULL, prefix); |
| 981 | kprintf(fmt, flags, NULL, NULL, ap); |
| 982 | |
| 983 | kprintf_unlock(); |
| 984 | |
| 985 | if (!panicstr) |
| 986 | logwakeup(); |
| 987 | } |
| 988 | |
| 989 | void |
| 990 | aprint_verbose(const char *fmt, ...) |
| 991 | { |
| 992 | va_list ap; |
| 993 | |
| 994 | va_start(ap, fmt); |
| 995 | aprint_verbose_internal(NULL, fmt, ap); |
| 996 | va_end(ap); |
| 997 | } |
| 998 | |
| 999 | void |
| 1000 | aprint_verbose_dev(device_t dv, const char *fmt, ...) |
| 1001 | { |
| 1002 | va_list ap; |
| 1003 | |
| 1004 | KASSERT(dv != NULL); |
| 1005 | |
| 1006 | va_start(ap, fmt); |
| 1007 | aprint_verbose_internal(device_xname(dv), fmt, ap); |
| 1008 | va_end(ap); |
| 1009 | } |
| 1010 | |
| 1011 | void |
| 1012 | aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...) |
| 1013 | { |
| 1014 | va_list ap; |
| 1015 | |
| 1016 | KASSERT(ifp != NULL); |
| 1017 | |
| 1018 | va_start(ap, fmt); |
| 1019 | aprint_verbose_internal(ifp->if_xname, fmt, ap); |
| 1020 | va_end(ap); |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * aprint_debug: Send to console and log only if AB_DEBUG. |
| 1025 | */ |
| 1026 | static void |
| 1027 | aprint_debug_internal(const char *prefix, const char *fmt, va_list ap) |
| 1028 | { |
| 1029 | if ((boothowto & AB_DEBUG) == 0) |
| 1030 | return; |
| 1031 | |
| 1032 | kprintf_lock(); |
| 1033 | |
| 1034 | if (prefix) |
| 1035 | kprintf_internal("%s: " , TOCONS | TOLOG, NULL, NULL, prefix); |
| 1036 | kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); |
| 1037 | |
| 1038 | kprintf_unlock(); |
| 1039 | } |
| 1040 | |
| 1041 | void |
| 1042 | aprint_debug(const char *fmt, ...) |
| 1043 | { |
| 1044 | va_list ap; |
| 1045 | |
| 1046 | va_start(ap, fmt); |
| 1047 | aprint_debug_internal(NULL, fmt, ap); |
| 1048 | va_end(ap); |
| 1049 | } |
| 1050 | |
| 1051 | void |
| 1052 | aprint_debug_dev(device_t dv, const char *fmt, ...) |
| 1053 | { |
| 1054 | va_list ap; |
| 1055 | |
| 1056 | KASSERT(dv != NULL); |
| 1057 | |
| 1058 | va_start(ap, fmt); |
| 1059 | aprint_debug_internal(device_xname(dv), fmt, ap); |
| 1060 | va_end(ap); |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...) |
| 1065 | { |
| 1066 | va_list ap; |
| 1067 | |
| 1068 | KASSERT(ifp != NULL); |
| 1069 | |
| 1070 | va_start(ap, fmt); |
| 1071 | aprint_debug_internal(ifp->if_xname, fmt, ap); |
| 1072 | va_end(ap); |
| 1073 | } |
| 1074 | |
| 1075 | void |
| 1076 | vprintf_flags(int flags, const char *fmt, va_list ap) |
| 1077 | { |
| 1078 | kprintf_lock(); |
| 1079 | kprintf(fmt, flags, NULL, NULL, ap); |
| 1080 | kprintf_unlock(); |
| 1081 | } |
| 1082 | |
| 1083 | void |
| 1084 | printf_flags(int flags, const char *fmt, ...) |
| 1085 | { |
| 1086 | va_list ap; |
| 1087 | |
| 1088 | va_start(ap, fmt); |
| 1089 | vprintf_flags(flags, fmt, ap); |
| 1090 | va_end(ap); |
| 1091 | } |
| 1092 | |
| 1093 | void |
| 1094 | printf_tolog(const char *fmt, ...) |
| 1095 | { |
| 1096 | va_list ap; |
| 1097 | |
| 1098 | va_start(ap, fmt); |
| 1099 | vprintf_flags(TOLOG, fmt, ap); |
| 1100 | va_end(ap); |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * printf_nolog: Like printf(), but does not send message to the log. |
| 1105 | */ |
| 1106 | |
| 1107 | void |
| 1108 | printf_nolog(const char *fmt, ...) |
| 1109 | { |
| 1110 | va_list ap; |
| 1111 | |
| 1112 | va_start(ap, fmt); |
| 1113 | vprintf_flags(TOCONS, fmt, ap); |
| 1114 | va_end(ap); |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf |
| 1119 | */ |
| 1120 | |
| 1121 | /* |
| 1122 | * printf: print a message to the console and the log |
| 1123 | */ |
| 1124 | void |
| 1125 | printf(const char *fmt, ...) |
| 1126 | { |
| 1127 | va_list ap; |
| 1128 | |
| 1129 | va_start(ap, fmt); |
| 1130 | vprintf_flags(TOCONS | TOLOG, fmt, ap); |
| 1131 | va_end(ap); |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * vprintf: print a message to the console and the log [already have |
| 1136 | * va_list] |
| 1137 | */ |
| 1138 | |
| 1139 | void |
| 1140 | vprintf(const char *fmt, va_list ap) |
| 1141 | { |
| 1142 | vprintf_flags(TOCONS | TOLOG, fmt, ap); |
| 1143 | |
| 1144 | if (!panicstr) |
| 1145 | logwakeup(); |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * snprintf: print a message to a buffer |
| 1150 | */ |
| 1151 | int |
| 1152 | snprintf(char *bf, size_t size, const char *fmt, ...) |
| 1153 | { |
| 1154 | int retval; |
| 1155 | va_list ap; |
| 1156 | |
| 1157 | va_start(ap, fmt); |
| 1158 | retval = vsnprintf(bf, size, fmt, ap); |
| 1159 | va_end(ap); |
| 1160 | |
| 1161 | return retval; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * vsnprintf: print a message to a buffer [already have va_list] |
| 1166 | */ |
| 1167 | int |
| 1168 | vsnprintf(char *bf, size_t size, const char *fmt, va_list ap) |
| 1169 | { |
| 1170 | int retval; |
| 1171 | char *p; |
| 1172 | |
| 1173 | p = bf + size; |
| 1174 | retval = kprintf(fmt, TOBUFONLY, &p, bf, ap); |
| 1175 | if (bf && size > 0) { |
| 1176 | /* nul terminate */ |
| 1177 | if (size <= (size_t)retval) |
| 1178 | bf[size - 1] = '\0'; |
| 1179 | else |
| 1180 | bf[retval] = '\0'; |
| 1181 | } |
| 1182 | return retval; |
| 1183 | } |
| 1184 | |
| 1185 | int |
| 1186 | vasprintf(char **bf, const char *fmt, va_list ap) |
| 1187 | { |
| 1188 | int retval; |
| 1189 | va_list cap; |
| 1190 | |
| 1191 | va_copy(cap, ap); |
| 1192 | retval = kprintf(fmt, TOBUFONLY, NULL, NULL, cap) + 1; |
| 1193 | va_end(cap); |
| 1194 | *bf = kmem_alloc(retval, KM_SLEEP); |
| 1195 | return vsnprintf(*bf, retval, fmt, ap); |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * kprintf: scaled down version of printf(3). |
| 1200 | * |
| 1201 | * this version based on vfprintf() from libc which was derived from |
| 1202 | * software contributed to Berkeley by Chris Torek. |
| 1203 | * |
| 1204 | * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS! |
| 1205 | */ |
| 1206 | |
| 1207 | /* |
| 1208 | * macros for converting digits to letters and vice versa |
| 1209 | */ |
| 1210 | #define to_digit(c) ((c) - '0') |
| 1211 | #define is_digit(c) ((unsigned)to_digit(c) <= 9) |
| 1212 | #define to_char(n) ((n) + '0') |
| 1213 | |
| 1214 | /* |
| 1215 | * flags used during conversion. |
| 1216 | */ |
| 1217 | #define ALT 0x001 /* alternate form */ |
| 1218 | #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ |
| 1219 | #define LADJUST 0x004 /* left adjustment */ |
| 1220 | #define LONGDBL 0x008 /* long double; unimplemented */ |
| 1221 | #define LONGINT 0x010 /* long integer */ |
| 1222 | #define QUADINT 0x020 /* quad integer */ |
| 1223 | #define SHORTINT 0x040 /* short integer */ |
| 1224 | #define MAXINT 0x080 /* intmax_t */ |
| 1225 | #define PTRINT 0x100 /* intptr_t */ |
| 1226 | #define SIZEINT 0x200 /* size_t */ |
| 1227 | #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */ |
| 1228 | #define FPT 0x800 /* Floating point number */ |
| 1229 | |
| 1230 | /* |
| 1231 | * To extend shorts properly, we need both signed and unsigned |
| 1232 | * argument extraction methods. |
| 1233 | */ |
| 1234 | #define SARG() \ |
| 1235 | (flags&MAXINT ? va_arg(ap, intmax_t) : \ |
| 1236 | flags&PTRINT ? va_arg(ap, intptr_t) : \ |
| 1237 | flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \ |
| 1238 | flags&QUADINT ? va_arg(ap, quad_t) : \ |
| 1239 | flags&LONGINT ? va_arg(ap, long) : \ |
| 1240 | flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ |
| 1241 | (long)va_arg(ap, int)) |
| 1242 | #define UARG() \ |
| 1243 | (flags&MAXINT ? va_arg(ap, uintmax_t) : \ |
| 1244 | flags&PTRINT ? va_arg(ap, uintptr_t) : \ |
| 1245 | flags&SIZEINT ? va_arg(ap, size_t) : \ |
| 1246 | flags&QUADINT ? va_arg(ap, u_quad_t) : \ |
| 1247 | flags&LONGINT ? va_arg(ap, u_long) : \ |
| 1248 | flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ |
| 1249 | (u_long)va_arg(ap, u_int)) |
| 1250 | |
| 1251 | #define KPRINTF_PUTCHAR(C) { \ |
| 1252 | if (oflags == TOBUFONLY) { \ |
| 1253 | if (sbuf && ((vp == NULL) || (sbuf < tailp))) \ |
| 1254 | *sbuf++ = (C); \ |
| 1255 | } else { \ |
| 1256 | putchar((C), oflags, vp); \ |
| 1257 | } \ |
| 1258 | } |
| 1259 | |
| 1260 | void |
| 1261 | device_printf(device_t dev, const char *fmt, ...) |
| 1262 | { |
| 1263 | va_list ap; |
| 1264 | |
| 1265 | va_start(ap, fmt); |
| 1266 | printf("%s: " , device_xname(dev)); |
| 1267 | vprintf(fmt, ap); |
| 1268 | va_end(ap); |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * Guts of kernel printf. Note, we already expect to be in a mutex! |
| 1274 | */ |
| 1275 | int |
| 1276 | kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) |
| 1277 | { |
| 1278 | const char *fmt; /* format string */ |
| 1279 | int ch; /* character from fmt */ |
| 1280 | int n; /* handy integer (short term usage) */ |
| 1281 | char *cp; /* handy char pointer (short term usage) */ |
| 1282 | int flags; /* flags as above */ |
| 1283 | int ret; /* return value accumulator */ |
| 1284 | int width; /* width from format (%8d), or 0 */ |
| 1285 | int prec; /* precision from format (%.3d), or -1 */ |
| 1286 | char sign; /* sign prefix (' ', '+', '-', or \0) */ |
| 1287 | |
| 1288 | u_quad_t _uquad; /* integer arguments %[diouxX] */ |
| 1289 | enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ |
| 1290 | int dprec; /* a copy of prec if [diouxX], 0 otherwise */ |
| 1291 | int realsz; /* field size expanded by dprec */ |
| 1292 | int size; /* size of converted field or string */ |
| 1293 | const char *xdigs; /* digits for [xX] conversion */ |
| 1294 | char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ |
| 1295 | char *tailp; /* tail pointer for snprintf */ |
| 1296 | |
| 1297 | if (oflags == TOBUFONLY && (vp != NULL)) |
| 1298 | tailp = *(char **)vp; |
| 1299 | else |
| 1300 | tailp = NULL; |
| 1301 | |
| 1302 | cp = NULL; /* XXX: shutup gcc */ |
| 1303 | size = 0; /* XXX: shutup gcc */ |
| 1304 | |
| 1305 | fmt = fmt0; |
| 1306 | ret = 0; |
| 1307 | |
| 1308 | xdigs = NULL; /* XXX: shut up gcc warning */ |
| 1309 | |
| 1310 | /* |
| 1311 | * Scan the format for conversions (`%' character). |
| 1312 | */ |
| 1313 | for (;;) { |
| 1314 | for (; *fmt != '%' && *fmt; fmt++) { |
| 1315 | ret++; |
| 1316 | KPRINTF_PUTCHAR(*fmt); |
| 1317 | } |
| 1318 | if (*fmt == 0) |
| 1319 | goto done; |
| 1320 | |
| 1321 | fmt++; /* skip over '%' */ |
| 1322 | |
| 1323 | flags = 0; |
| 1324 | dprec = 0; |
| 1325 | width = 0; |
| 1326 | prec = -1; |
| 1327 | sign = '\0'; |
| 1328 | |
| 1329 | rflag: ch = *fmt++; |
| 1330 | reswitch: switch (ch) { |
| 1331 | case ' ': |
| 1332 | /* |
| 1333 | * ``If the space and + flags both appear, the space |
| 1334 | * flag will be ignored.'' |
| 1335 | * -- ANSI X3J11 |
| 1336 | */ |
| 1337 | if (!sign) |
| 1338 | sign = ' '; |
| 1339 | goto rflag; |
| 1340 | case '#': |
| 1341 | flags |= ALT; |
| 1342 | goto rflag; |
| 1343 | case '*': |
| 1344 | /* |
| 1345 | * ``A negative field width argument is taken as a |
| 1346 | * - flag followed by a positive field width.'' |
| 1347 | * -- ANSI X3J11 |
| 1348 | * They don't exclude field widths read from args. |
| 1349 | */ |
| 1350 | if ((width = va_arg(ap, int)) >= 0) |
| 1351 | goto rflag; |
| 1352 | width = -width; |
| 1353 | /* FALLTHROUGH */ |
| 1354 | case '-': |
| 1355 | flags |= LADJUST; |
| 1356 | goto rflag; |
| 1357 | case '+': |
| 1358 | sign = '+'; |
| 1359 | goto rflag; |
| 1360 | case '.': |
| 1361 | if ((ch = *fmt++) == '*') { |
| 1362 | n = va_arg(ap, int); |
| 1363 | prec = n < 0 ? -1 : n; |
| 1364 | goto rflag; |
| 1365 | } |
| 1366 | n = 0; |
| 1367 | while (is_digit(ch)) { |
| 1368 | n = 10 * n + to_digit(ch); |
| 1369 | ch = *fmt++; |
| 1370 | } |
| 1371 | prec = n < 0 ? -1 : n; |
| 1372 | goto reswitch; |
| 1373 | case '0': |
| 1374 | /* |
| 1375 | * ``Note that 0 is taken as a flag, not as the |
| 1376 | * beginning of a field width.'' |
| 1377 | * -- ANSI X3J11 |
| 1378 | */ |
| 1379 | flags |= ZEROPAD; |
| 1380 | goto rflag; |
| 1381 | case '1': case '2': case '3': case '4': |
| 1382 | case '5': case '6': case '7': case '8': case '9': |
| 1383 | n = 0; |
| 1384 | do { |
| 1385 | n = 10 * n + to_digit(ch); |
| 1386 | ch = *fmt++; |
| 1387 | } while (is_digit(ch)); |
| 1388 | width = n; |
| 1389 | goto reswitch; |
| 1390 | case 'h': |
| 1391 | flags |= SHORTINT; |
| 1392 | goto rflag; |
| 1393 | case 'j': |
| 1394 | flags |= MAXINT; |
| 1395 | goto rflag; |
| 1396 | case 'l': |
| 1397 | if (*fmt == 'l') { |
| 1398 | fmt++; |
| 1399 | flags |= QUADINT; |
| 1400 | } else { |
| 1401 | flags |= LONGINT; |
| 1402 | } |
| 1403 | goto rflag; |
| 1404 | case 'q': |
| 1405 | flags |= QUADINT; |
| 1406 | goto rflag; |
| 1407 | case 't': |
| 1408 | flags |= PTRINT; |
| 1409 | goto rflag; |
| 1410 | case 'z': |
| 1411 | flags |= SIZEINT; |
| 1412 | goto rflag; |
| 1413 | case 'c': |
| 1414 | *(cp = bf) = va_arg(ap, int); |
| 1415 | size = 1; |
| 1416 | sign = '\0'; |
| 1417 | break; |
| 1418 | case 'D': |
| 1419 | flags |= LONGINT; |
| 1420 | /*FALLTHROUGH*/ |
| 1421 | case 'd': |
| 1422 | case 'i': |
| 1423 | _uquad = SARG(); |
| 1424 | if ((quad_t)_uquad < 0) { |
| 1425 | _uquad = -_uquad; |
| 1426 | sign = '-'; |
| 1427 | } |
| 1428 | base = DEC; |
| 1429 | goto number; |
| 1430 | case 'n': |
| 1431 | if (flags & MAXINT) |
| 1432 | *va_arg(ap, intmax_t *) = ret; |
| 1433 | else if (flags & PTRINT) |
| 1434 | *va_arg(ap, intptr_t *) = ret; |
| 1435 | else if (flags & SIZEINT) |
| 1436 | *va_arg(ap, ssize_t *) = ret; |
| 1437 | else if (flags & QUADINT) |
| 1438 | *va_arg(ap, quad_t *) = ret; |
| 1439 | else if (flags & LONGINT) |
| 1440 | *va_arg(ap, long *) = ret; |
| 1441 | else if (flags & SHORTINT) |
| 1442 | *va_arg(ap, short *) = ret; |
| 1443 | else |
| 1444 | *va_arg(ap, int *) = ret; |
| 1445 | continue; /* no output */ |
| 1446 | case 'O': |
| 1447 | flags |= LONGINT; |
| 1448 | /*FALLTHROUGH*/ |
| 1449 | case 'o': |
| 1450 | _uquad = UARG(); |
| 1451 | base = OCT; |
| 1452 | goto nosign; |
| 1453 | case 'p': |
| 1454 | /* |
| 1455 | * ``The argument shall be a pointer to void. The |
| 1456 | * value of the pointer is converted to a sequence |
| 1457 | * of printable characters, in an implementation- |
| 1458 | * defined manner.'' |
| 1459 | * -- ANSI X3J11 |
| 1460 | */ |
| 1461 | /* NOSTRICT */ |
| 1462 | _uquad = (u_long)va_arg(ap, void *); |
| 1463 | base = HEX; |
| 1464 | xdigs = hexdigits; |
| 1465 | flags |= HEXPREFIX; |
| 1466 | ch = 'x'; |
| 1467 | goto nosign; |
| 1468 | case 's': |
| 1469 | if ((cp = va_arg(ap, char *)) == NULL) |
| 1470 | /*XXXUNCONST*/ |
| 1471 | cp = __UNCONST("(null)" ); |
| 1472 | if (prec >= 0) { |
| 1473 | /* |
| 1474 | * can't use strlen; can only look for the |
| 1475 | * NUL in the first `prec' characters, and |
| 1476 | * strlen() will go further. |
| 1477 | */ |
| 1478 | char *p = memchr(cp, 0, prec); |
| 1479 | |
| 1480 | if (p != NULL) { |
| 1481 | size = p - cp; |
| 1482 | if (size > prec) |
| 1483 | size = prec; |
| 1484 | } else |
| 1485 | size = prec; |
| 1486 | } else |
| 1487 | size = strlen(cp); |
| 1488 | sign = '\0'; |
| 1489 | break; |
| 1490 | case 'U': |
| 1491 | flags |= LONGINT; |
| 1492 | /*FALLTHROUGH*/ |
| 1493 | case 'u': |
| 1494 | _uquad = UARG(); |
| 1495 | base = DEC; |
| 1496 | goto nosign; |
| 1497 | case 'X': |
| 1498 | xdigs = HEXDIGITS; |
| 1499 | goto hex; |
| 1500 | case 'x': |
| 1501 | xdigs = hexdigits; |
| 1502 | hex: _uquad = UARG(); |
| 1503 | base = HEX; |
| 1504 | /* leading 0x/X only if non-zero */ |
| 1505 | if (flags & ALT && _uquad != 0) |
| 1506 | flags |= HEXPREFIX; |
| 1507 | |
| 1508 | /* unsigned conversions */ |
| 1509 | nosign: sign = '\0'; |
| 1510 | /* |
| 1511 | * ``... diouXx conversions ... if a precision is |
| 1512 | * specified, the 0 flag will be ignored.'' |
| 1513 | * -- ANSI X3J11 |
| 1514 | */ |
| 1515 | number: if ((dprec = prec) >= 0) |
| 1516 | flags &= ~ZEROPAD; |
| 1517 | |
| 1518 | /* |
| 1519 | * ``The result of converting a zero value with an |
| 1520 | * explicit precision of zero is no characters.'' |
| 1521 | * -- ANSI X3J11 |
| 1522 | */ |
| 1523 | cp = bf + KPRINTF_BUFSIZE; |
| 1524 | if (_uquad != 0 || prec != 0) { |
| 1525 | /* |
| 1526 | * Unsigned mod is hard, and unsigned mod |
| 1527 | * by a constant is easier than that by |
| 1528 | * a variable; hence this switch. |
| 1529 | */ |
| 1530 | switch (base) { |
| 1531 | case OCT: |
| 1532 | do { |
| 1533 | *--cp = to_char(_uquad & 7); |
| 1534 | _uquad >>= 3; |
| 1535 | } while (_uquad); |
| 1536 | /* handle octal leading 0 */ |
| 1537 | if (flags & ALT && *cp != '0') |
| 1538 | *--cp = '0'; |
| 1539 | break; |
| 1540 | |
| 1541 | case DEC: |
| 1542 | /* many numbers are 1 digit */ |
| 1543 | while (_uquad >= 10) { |
| 1544 | *--cp = to_char(_uquad % 10); |
| 1545 | _uquad /= 10; |
| 1546 | } |
| 1547 | *--cp = to_char(_uquad); |
| 1548 | break; |
| 1549 | |
| 1550 | case HEX: |
| 1551 | do { |
| 1552 | *--cp = xdigs[_uquad & 15]; |
| 1553 | _uquad >>= 4; |
| 1554 | } while (_uquad); |
| 1555 | break; |
| 1556 | |
| 1557 | default: |
| 1558 | /*XXXUNCONST*/ |
| 1559 | cp = __UNCONST("bug in kprintf: bad base" ); |
| 1560 | size = strlen(cp); |
| 1561 | goto skipsize; |
| 1562 | } |
| 1563 | } |
| 1564 | size = bf + KPRINTF_BUFSIZE - cp; |
| 1565 | skipsize: |
| 1566 | break; |
| 1567 | default: /* "%?" prints ?, unless ? is NUL */ |
| 1568 | if (ch == '\0') |
| 1569 | goto done; |
| 1570 | /* pretend it was %c with argument ch */ |
| 1571 | cp = bf; |
| 1572 | *cp = ch; |
| 1573 | size = 1; |
| 1574 | sign = '\0'; |
| 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * All reasonable formats wind up here. At this point, `cp' |
| 1580 | * points to a string which (if not flags&LADJUST) should be |
| 1581 | * padded out to `width' places. If flags&ZEROPAD, it should |
| 1582 | * first be prefixed by any sign or other prefix; otherwise, |
| 1583 | * it should be blank padded before the prefix is emitted. |
| 1584 | * After any left-hand padding and prefixing, emit zeroes |
| 1585 | * required by a decimal [diouxX] precision, then print the |
| 1586 | * string proper, then emit zeroes required by any leftover |
| 1587 | * floating precision; finally, if LADJUST, pad with blanks. |
| 1588 | * |
| 1589 | * Compute actual size, so we know how much to pad. |
| 1590 | * size excludes decimal prec; realsz includes it. |
| 1591 | */ |
| 1592 | realsz = dprec > size ? dprec : size; |
| 1593 | if (sign) |
| 1594 | realsz++; |
| 1595 | else if (flags & HEXPREFIX) |
| 1596 | realsz+= 2; |
| 1597 | |
| 1598 | /* adjust ret */ |
| 1599 | ret += width > realsz ? width : realsz; |
| 1600 | |
| 1601 | /* right-adjusting blank padding */ |
| 1602 | if ((flags & (LADJUST|ZEROPAD)) == 0) { |
| 1603 | n = width - realsz; |
| 1604 | while (n-- > 0) |
| 1605 | KPRINTF_PUTCHAR(' '); |
| 1606 | } |
| 1607 | |
| 1608 | /* prefix */ |
| 1609 | if (sign) { |
| 1610 | KPRINTF_PUTCHAR(sign); |
| 1611 | } else if (flags & HEXPREFIX) { |
| 1612 | KPRINTF_PUTCHAR('0'); |
| 1613 | KPRINTF_PUTCHAR(ch); |
| 1614 | } |
| 1615 | |
| 1616 | /* right-adjusting zero padding */ |
| 1617 | if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { |
| 1618 | n = width - realsz; |
| 1619 | while (n-- > 0) |
| 1620 | KPRINTF_PUTCHAR('0'); |
| 1621 | } |
| 1622 | |
| 1623 | /* leading zeroes from decimal precision */ |
| 1624 | n = dprec - size; |
| 1625 | while (n-- > 0) |
| 1626 | KPRINTF_PUTCHAR('0'); |
| 1627 | |
| 1628 | /* the string or number proper */ |
| 1629 | for (; size--; cp++) |
| 1630 | KPRINTF_PUTCHAR(*cp); |
| 1631 | /* left-adjusting padding (always blank) */ |
| 1632 | if (flags & LADJUST) { |
| 1633 | n = width - realsz; |
| 1634 | while (n-- > 0) |
| 1635 | KPRINTF_PUTCHAR(' '); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | done: |
| 1640 | if ((oflags == TOBUFONLY) && (vp != NULL)) |
| 1641 | *(char **)vp = sbuf; |
| 1642 | (*v_flush)(); |
| 1643 | |
| 1644 | #ifdef RND_PRINTF |
| 1645 | if (!cold) { |
| 1646 | struct timespec ts; |
| 1647 | (void)nanotime(&ts); |
| 1648 | SHA512_Update(&kprnd_sha, (char *)&ts, sizeof(ts)); |
| 1649 | } |
| 1650 | #endif |
| 1651 | return ret; |
| 1652 | } |
| 1653 | |