The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_prf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 18aeeebc0f10f70ae0f843cf92593123


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.