[BACK]Return to subr_prf.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/subr_prf.c, Revision 1.179

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

CVSweb <webmaster@jp.NetBSD.org>