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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 04e2a8db942aa24d282430dd7aff5b6e


[ 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.