1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1986, 1988, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 403ae59505SPoul-Henning Kamp #include "opt_ddb.h" 413ae59505SPoul-Henning Kamp 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44f591779bSSeigo Tanimura #include <sys/lock.h> 4582ebaee7SMarcel Moolenaar #include <sys/kdb.h> 46f591779bSSeigo Tanimura #include <sys/mutex.h> 47f591779bSSeigo Tanimura #include <sys/sx.h> 48e796e00dSPoul-Henning Kamp #include <sys/kernel.h> 49df8bae1dSRodney W. Grimes #include <sys/msgbuf.h> 50a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/proc.h> 522bb95458SMaxime Henrion #include <sys/stddef.h> 5358a24f79SPoul-Henning Kamp #include <sys/sysctl.h> 54df8bae1dSRodney W. Grimes #include <sys/tty.h> 55df8bae1dSRodney W. Grimes #include <sys/syslog.h> 56ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 57e2a09b26SPoul-Henning Kamp #include <sys/uio.h> 58df8bae1dSRodney W. Grimes 593ae59505SPoul-Henning Kamp #ifdef DDB 603ae59505SPoul-Henning Kamp #include <ddb/ddb.h> 613ae59505SPoul-Henning Kamp #endif 623ae59505SPoul-Henning Kamp 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Note that stdarg.h and the ANSI style va_start macro is used for both 65df8bae1dSRodney W. Grimes * ANSI and traditional C compilers. 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes #include <machine/stdarg.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #define TOCONS 0x01 70df8bae1dSRodney W. Grimes #define TOTTY 0x02 71df8bae1dSRodney W. Grimes #define TOLOG 0x04 72df8bae1dSRodney W. Grimes 7382941964SPeter Wemm /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ 743b1f7e7dSDag-Erling Smørgrav #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) 75df8bae1dSRodney W. Grimes 768245f3f5SArchie Cobbs struct putchar_arg { 778245f3f5SArchie Cobbs int flags; 78e2a09b26SPoul-Henning Kamp int pri; 798245f3f5SArchie Cobbs struct tty *tty; 808245f3f5SArchie Cobbs }; 818245f3f5SArchie Cobbs 828245f3f5SArchie Cobbs struct snprintf_arg { 838245f3f5SArchie Cobbs char *str; 848245f3f5SArchie Cobbs size_t remain; 858245f3f5SArchie Cobbs }; 868245f3f5SArchie Cobbs 87e2a09b26SPoul-Henning Kamp extern int log_open; 88e2a09b26SPoul-Henning Kamp 89e2a09b26SPoul-Henning Kamp static void msglogchar(int c, int pri); 904d77a549SAlfred Perlstein static void putchar(int ch, void *arg); 913b1f7e7dSDag-Erling Smørgrav static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len); 924d77a549SAlfred Perlstein static void snprintf_func(int ch, void *arg); 93df8bae1dSRodney W. Grimes 9487b6de2bSPoul-Henning Kamp static int consintr = 1; /* Ok to handle console interrupts? */ 95e796e00dSPoul-Henning Kamp static int msgbufmapped; /* Set when safe to use msgbuf */ 96e2a09b26SPoul-Henning Kamp int msgbuftrigger; 97df8bae1dSRodney W. Grimes 98dbe620d3SDavid Malone static int log_console_output = 1; 99af338beaSDavid Malone TUNABLE_INT("kern.log_console_output", &log_console_output); 100dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, 1018e1b7974SBrian Feldman &log_console_output, 0, "Duplicate console output to the syslog."); 1028e1b7974SBrian Feldman 1038e1b7974SBrian Feldman static int always_console_output = 0; 1048e1b7974SBrian Feldman TUNABLE_INT("kern.always_console_output", &always_console_output); 1058e1b7974SBrian Feldman SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW, 1068e1b7974SBrian Feldman &always_console_output, 0, "Always output to console despite TIOCCONS."); 107dbe620d3SDavid Malone 108df8bae1dSRodney W. Grimes /* 109df8bae1dSRodney W. Grimes * Warn that a system table is full. 110df8bae1dSRodney W. Grimes */ 111df8bae1dSRodney W. Grimes void 112e2a09b26SPoul-Henning Kamp tablefull(const char *tab) 113df8bae1dSRodney W. Grimes { 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes log(LOG_ERR, "%s: table is full\n", tab); 116df8bae1dSRodney W. Grimes } 117df8bae1dSRodney W. Grimes 118df8bae1dSRodney W. Grimes /* 119df8bae1dSRodney W. Grimes * Uprintf prints to the controlling terminal for the current process. 120df8bae1dSRodney W. Grimes * It may block if the tty queue is overfull. No message is printed if 121df8bae1dSRodney W. Grimes * the queue does not clear in a reasonable time. 122df8bae1dSRodney W. Grimes */ 123f1550d9dSDoug Rabson int 124df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...) 125df8bae1dSRodney W. Grimes { 126b40ce416SJulian Elischer struct thread *td = curthread; 127b40ce416SJulian Elischer struct proc *p = td->td_proc; 128df8bae1dSRodney W. Grimes va_list ap; 129791d77e0SPoul-Henning Kamp struct putchar_arg pca; 130f591779bSSeigo Tanimura int retval; 131df8bae1dSRodney W. Grimes 132f591779bSSeigo Tanimura if (td == NULL || td == PCPU_GET(idlethread)) 133f591779bSSeigo Tanimura return (0); 134f591779bSSeigo Tanimura 135f591779bSSeigo Tanimura p = td->td_proc; 136f591779bSSeigo Tanimura PROC_LOCK(p); 137f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) == 0) { 138f591779bSSeigo Tanimura PROC_UNLOCK(p); 139f591779bSSeigo Tanimura return (0); 140f591779bSSeigo Tanimura } 141f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 142791d77e0SPoul-Henning Kamp pca.tty = p->p_session->s_ttyp; 143f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 144f591779bSSeigo Tanimura PROC_UNLOCK(p); 145f591779bSSeigo Tanimura if (pca.tty == NULL) 146f591779bSSeigo Tanimura return (0); 147791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 148f591779bSSeigo Tanimura va_start(ap, fmt); 149f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 150df8bae1dSRodney W. Grimes va_end(ap); 151f591779bSSeigo Tanimura 15258a24f79SPoul-Henning Kamp return (retval); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes 155df8bae1dSRodney W. Grimes /* 156df8bae1dSRodney W. Grimes * tprintf prints on the controlling terminal associated 157a52585d7SPoul-Henning Kamp * with the given session, possibly to the log as well. 158df8bae1dSRodney W. Grimes */ 159a52585d7SPoul-Henning Kamp void 160a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...) 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes struct tty *tp = NULL; 163b5a2bad1SJohn Baldwin int flags = 0; 164df8bae1dSRodney W. Grimes va_list ap; 165791d77e0SPoul-Henning Kamp struct putchar_arg pca; 166b5a2bad1SJohn Baldwin struct session *sess = NULL; 167df8bae1dSRodney W. Grimes 168e2a09b26SPoul-Henning Kamp if (pri != -1) 169a52585d7SPoul-Henning Kamp flags |= TOLOG; 170f591779bSSeigo Tanimura if (p != NULL) { 171f591779bSSeigo Tanimura PROC_LOCK(p); 172f591779bSSeigo Tanimura if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 173b5a2bad1SJohn Baldwin sess = p->p_session; 174b5a2bad1SJohn Baldwin SESS_LOCK(sess); 175f591779bSSeigo Tanimura PROC_UNLOCK(p); 176b5a2bad1SJohn Baldwin SESSHOLD(sess); 177b5a2bad1SJohn Baldwin tp = sess->s_ttyp; 178b5a2bad1SJohn Baldwin SESS_UNLOCK(sess); 179f591779bSSeigo Tanimura if (ttycheckoutq(tp, 0)) 180f591779bSSeigo Tanimura flags |= TOTTY; 181f591779bSSeigo Tanimura else 182f591779bSSeigo Tanimura tp = NULL; 183f591779bSSeigo Tanimura } else 184f591779bSSeigo Tanimura PROC_UNLOCK(p); 185a52585d7SPoul-Henning Kamp } 186e2a09b26SPoul-Henning Kamp pca.pri = pri; 187791d77e0SPoul-Henning Kamp pca.tty = tp; 188791d77e0SPoul-Henning Kamp pca.flags = flags; 189a52585d7SPoul-Henning Kamp va_start(ap, fmt); 19074f1af01SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 191df8bae1dSRodney W. Grimes va_end(ap); 192572b4402SPoul-Henning Kamp if (sess != NULL) 193b5a2bad1SJohn Baldwin SESSRELE(sess); 194e2a09b26SPoul-Henning Kamp msgbuftrigger = 1; 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes 197df8bae1dSRodney W. Grimes /* 198df8bae1dSRodney W. Grimes * Ttyprintf displays a message on a tty; it should be used only by 199df8bae1dSRodney W. Grimes * the tty driver, or anything that knows the underlying tty will not 200df8bae1dSRodney W. Grimes * be revoke(2)'d away. Other callers should use tprintf. 201df8bae1dSRodney W. Grimes */ 202f1550d9dSDoug Rabson int 203df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...) 204df8bae1dSRodney W. Grimes { 205df8bae1dSRodney W. Grimes va_list ap; 206791d77e0SPoul-Henning Kamp struct putchar_arg pca; 207f1550d9dSDoug Rabson int retval; 208f1550d9dSDoug Rabson 209df8bae1dSRodney W. Grimes va_start(ap, fmt); 210791d77e0SPoul-Henning Kamp pca.tty = tp; 211791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 212f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 213df8bae1dSRodney W. Grimes va_end(ap); 21458a24f79SPoul-Henning Kamp return (retval); 215df8bae1dSRodney W. Grimes } 216df8bae1dSRodney W. Grimes 217df8bae1dSRodney W. Grimes /* 218df8bae1dSRodney W. Grimes * Log writes to the log buffer, and guarantees not to sleep (so can be 219df8bae1dSRodney W. Grimes * called by interrupt routines). If there is no process reading the 220df8bae1dSRodney W. Grimes * log yet, it writes to the console also. 221df8bae1dSRodney W. Grimes */ 2224d88c459SPoul-Henning Kamp void 223df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...) 224df8bae1dSRodney W. Grimes { 225df8bae1dSRodney W. Grimes va_list ap; 226791d77e0SPoul-Henning Kamp struct putchar_arg pca; 227e2a09b26SPoul-Henning Kamp 228791d77e0SPoul-Henning Kamp pca.tty = NULL; 229e2a09b26SPoul-Henning Kamp pca.pri = level; 230e2a09b26SPoul-Henning Kamp pca.flags = log_open ? TOLOG : TOCONS; 231e2a09b26SPoul-Henning Kamp 232e2a09b26SPoul-Henning Kamp va_start(ap, fmt); 23374f1af01SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 234df8bae1dSRodney W. Grimes va_end(ap); 235e2a09b26SPoul-Henning Kamp 236b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 237df8bae1dSRodney W. Grimes } 238df8bae1dSRodney W. Grimes 239e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128 240e2a09b26SPoul-Henning Kamp 241e2a09b26SPoul-Henning Kamp void 242e2a09b26SPoul-Henning Kamp log_console(struct uio *uio) 243df8bae1dSRodney W. Grimes { 244552afd9cSPoul-Henning Kamp int c, i, error, nl; 245e2a09b26SPoul-Henning Kamp char *consbuffer; 246e2a09b26SPoul-Henning Kamp int pri; 247df8bae1dSRodney W. Grimes 248dbe620d3SDavid Malone if (!log_console_output) 249dbe620d3SDavid Malone return; 250dbe620d3SDavid Malone 251e2a09b26SPoul-Henning Kamp pri = LOG_INFO | LOG_CONSOLE; 252552afd9cSPoul-Henning Kamp uio = cloneuio(uio); 253552afd9cSPoul-Henning Kamp consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK); 254e2a09b26SPoul-Henning Kamp 255e2a09b26SPoul-Henning Kamp nl = 0; 256e2a09b26SPoul-Henning Kamp while (uio->uio_resid > 0) { 257e2a09b26SPoul-Henning Kamp c = imin(uio->uio_resid, CONSCHUNK); 258e2a09b26SPoul-Henning Kamp error = uiomove(consbuffer, c, uio); 259e2a09b26SPoul-Henning Kamp if (error != 0) 2602f9752e9SPoul-Henning Kamp break; 261e2a09b26SPoul-Henning Kamp for (i = 0; i < c; i++) { 262e2a09b26SPoul-Henning Kamp msglogchar(consbuffer[i], pri); 263e2a09b26SPoul-Henning Kamp if (consbuffer[i] == '\n') 264e2a09b26SPoul-Henning Kamp nl = 1; 265e2a09b26SPoul-Henning Kamp else 266e2a09b26SPoul-Henning Kamp nl = 0; 267e2a09b26SPoul-Henning Kamp } 268e2a09b26SPoul-Henning Kamp } 269e2a09b26SPoul-Henning Kamp if (!nl) 270e2a09b26SPoul-Henning Kamp msglogchar('\n', pri); 271b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 272552afd9cSPoul-Henning Kamp free(uio, M_IOV); 273552afd9cSPoul-Henning Kamp free(consbuffer, M_TEMP); 274e2a09b26SPoul-Henning Kamp return; 275df8bae1dSRodney W. Grimes } 276df8bae1dSRodney W. Grimes 2776ddbf1e2SGary Palmer int 278df8bae1dSRodney W. Grimes printf(const char *fmt, ...) 279df8bae1dSRodney W. Grimes { 280df8bae1dSRodney W. Grimes va_list ap; 2814d88c459SPoul-Henning Kamp int savintr; 282791d77e0SPoul-Henning Kamp struct putchar_arg pca; 28365ed8cbdSJustin T. Gibbs int retval; 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes savintr = consintr; /* disable interrupts */ 286df8bae1dSRodney W. Grimes consintr = 0; 287df8bae1dSRodney W. Grimes va_start(ap, fmt); 288791d77e0SPoul-Henning Kamp pca.tty = NULL; 289791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 290e2a09b26SPoul-Henning Kamp pca.pri = -1; 29165ed8cbdSJustin T. Gibbs retval = kvprintf(fmt, putchar, &pca, 10, ap); 292df8bae1dSRodney W. Grimes va_end(ap); 293df8bae1dSRodney W. Grimes if (!panicstr) 294b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 295df8bae1dSRodney W. Grimes consintr = savintr; /* reenable interrupts */ 29658a24f79SPoul-Henning Kamp return (retval); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 299f1550d9dSDoug Rabson int 300791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap) 301791d77e0SPoul-Henning Kamp { 3024d88c459SPoul-Henning Kamp int savintr; 303791d77e0SPoul-Henning Kamp struct putchar_arg pca; 304f1550d9dSDoug Rabson int retval; 305791d77e0SPoul-Henning Kamp 306791d77e0SPoul-Henning Kamp savintr = consintr; /* disable interrupts */ 307791d77e0SPoul-Henning Kamp consintr = 0; 308791d77e0SPoul-Henning Kamp pca.tty = NULL; 309791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 310e2a09b26SPoul-Henning Kamp pca.pri = -1; 311f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 312791d77e0SPoul-Henning Kamp if (!panicstr) 313b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 314791d77e0SPoul-Henning Kamp consintr = savintr; /* reenable interrupts */ 31558a24f79SPoul-Henning Kamp return (retval); 316791d77e0SPoul-Henning Kamp } 317791d77e0SPoul-Henning Kamp 318791d77e0SPoul-Henning Kamp /* 319791d77e0SPoul-Henning Kamp * Print a character on console or users terminal. If destination is 320e796e00dSPoul-Henning Kamp * the console then the last bunch of characters are saved in msgbuf for 321791d77e0SPoul-Henning Kamp * inspection later. 322791d77e0SPoul-Henning Kamp */ 323791d77e0SPoul-Henning Kamp static void 324791d77e0SPoul-Henning Kamp putchar(int c, void *arg) 325791d77e0SPoul-Henning Kamp { 326791d77e0SPoul-Henning Kamp struct putchar_arg *ap = (struct putchar_arg*) arg; 327791d77e0SPoul-Henning Kamp struct tty *tp = ap->tty; 328d29bf12fSIan Dowse int consdirect, flags = ap->flags; 329d29bf12fSIan Dowse 330d29bf12fSIan Dowse consdirect = ((flags & TOCONS) && constty == NULL); 331adef9265SIan Dowse /* Don't use the tty code after a panic or while in ddb. */ 332791d77e0SPoul-Henning Kamp if (panicstr) 333d29bf12fSIan Dowse consdirect = 1; 33482ebaee7SMarcel Moolenaar if (kdb_active) 335adef9265SIan Dowse consdirect = 1; 336d29bf12fSIan Dowse if (consdirect) { 337d29bf12fSIan Dowse if (c != '\0') 338d29bf12fSIan Dowse cnputc(c); 339d29bf12fSIan Dowse } else { 340d29bf12fSIan Dowse if ((flags & TOTTY) && tp != NULL) 341d29bf12fSIan Dowse tputchar(c, tp); 3428e1b7974SBrian Feldman if (flags & TOCONS) { 3438e1b7974SBrian Feldman if (constty != NULL) 344d29bf12fSIan Dowse msgbuf_addchar(&consmsgbuf, c); 3458e1b7974SBrian Feldman if (always_console_output && c != '\0') 3468e1b7974SBrian Feldman cnputc(c); 3478e1b7974SBrian Feldman } 348791d77e0SPoul-Henning Kamp } 349791d77e0SPoul-Henning Kamp if ((flags & TOLOG)) 350e2a09b26SPoul-Henning Kamp msglogchar(c, ap->pri); 351791d77e0SPoul-Henning Kamp } 352791d77e0SPoul-Henning Kamp 353791d77e0SPoul-Henning Kamp /* 354791d77e0SPoul-Henning Kamp * Scaled down version of sprintf(3). 355791d77e0SPoul-Henning Kamp */ 356791d77e0SPoul-Henning Kamp int 357791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...) 358791d77e0SPoul-Henning Kamp { 359791d77e0SPoul-Henning Kamp int retval; 360791d77e0SPoul-Henning Kamp va_list ap; 361791d77e0SPoul-Henning Kamp 362791d77e0SPoul-Henning Kamp va_start(ap, cfmt); 363791d77e0SPoul-Henning Kamp retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 364fe96d47dSPoul-Henning Kamp buf[retval] = '\0'; 365791d77e0SPoul-Henning Kamp va_end(ap); 36658a24f79SPoul-Henning Kamp return (retval); 367791d77e0SPoul-Henning Kamp } 368791d77e0SPoul-Henning Kamp 369791d77e0SPoul-Henning Kamp /* 37099237364SAndrey A. Chernov * Scaled down version of vsprintf(3). 37199237364SAndrey A. Chernov */ 37299237364SAndrey A. Chernov int 37399237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap) 37499237364SAndrey A. Chernov { 37599237364SAndrey A. Chernov int retval; 37699237364SAndrey A. Chernov 37799237364SAndrey A. Chernov retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 37899237364SAndrey A. Chernov buf[retval] = '\0'; 37958a24f79SPoul-Henning Kamp return (retval); 38099237364SAndrey A. Chernov } 38199237364SAndrey A. Chernov 38299237364SAndrey A. Chernov /* 3838245f3f5SArchie Cobbs * Scaled down version of snprintf(3). 3848245f3f5SArchie Cobbs */ 3858245f3f5SArchie Cobbs int 3868245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...) 3878245f3f5SArchie Cobbs { 3888245f3f5SArchie Cobbs int retval; 3898245f3f5SArchie Cobbs va_list ap; 3908245f3f5SArchie Cobbs 3918245f3f5SArchie Cobbs va_start(ap, format); 3928245f3f5SArchie Cobbs retval = vsnprintf(str, size, format, ap); 3938245f3f5SArchie Cobbs va_end(ap); 3948245f3f5SArchie Cobbs return(retval); 3958245f3f5SArchie Cobbs } 3968245f3f5SArchie Cobbs 3978245f3f5SArchie Cobbs /* 3988245f3f5SArchie Cobbs * Scaled down version of vsnprintf(3). 3998245f3f5SArchie Cobbs */ 4008245f3f5SArchie Cobbs int 4018245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap) 4028245f3f5SArchie Cobbs { 4038245f3f5SArchie Cobbs struct snprintf_arg info; 4048245f3f5SArchie Cobbs int retval; 4058245f3f5SArchie Cobbs 4068245f3f5SArchie Cobbs info.str = str; 4078245f3f5SArchie Cobbs info.remain = size; 4088245f3f5SArchie Cobbs retval = kvprintf(format, snprintf_func, &info, 10, ap); 4098245f3f5SArchie Cobbs if (info.remain >= 1) 4108245f3f5SArchie Cobbs *info.str++ = '\0'; 41158a24f79SPoul-Henning Kamp return (retval); 4128245f3f5SArchie Cobbs } 4138245f3f5SArchie Cobbs 4148751a8c7SPoul-Henning Kamp /* 4158751a8c7SPoul-Henning Kamp * Kernel version which takes radix argument vsnprintf(3). 4168751a8c7SPoul-Henning Kamp */ 4178751a8c7SPoul-Henning Kamp int 4188751a8c7SPoul-Henning Kamp vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap) 4198751a8c7SPoul-Henning Kamp { 4208751a8c7SPoul-Henning Kamp struct snprintf_arg info; 4218751a8c7SPoul-Henning Kamp int retval; 4228751a8c7SPoul-Henning Kamp 4238751a8c7SPoul-Henning Kamp info.str = str; 4248751a8c7SPoul-Henning Kamp info.remain = size; 4258751a8c7SPoul-Henning Kamp retval = kvprintf(format, snprintf_func, &info, radix, ap); 4268751a8c7SPoul-Henning Kamp if (info.remain >= 1) 4278751a8c7SPoul-Henning Kamp *info.str++ = '\0'; 4288751a8c7SPoul-Henning Kamp return (retval); 4298751a8c7SPoul-Henning Kamp } 4308751a8c7SPoul-Henning Kamp 4318245f3f5SArchie Cobbs static void 4328245f3f5SArchie Cobbs snprintf_func(int ch, void *arg) 4338245f3f5SArchie Cobbs { 4348245f3f5SArchie Cobbs struct snprintf_arg *const info = arg; 4358245f3f5SArchie Cobbs 4368245f3f5SArchie Cobbs if (info->remain >= 2) { 4378245f3f5SArchie Cobbs *info->str++ = ch; 4388245f3f5SArchie Cobbs info->remain--; 4398245f3f5SArchie Cobbs } 4408245f3f5SArchie Cobbs } 4418245f3f5SArchie Cobbs 4428245f3f5SArchie Cobbs /* 44382941964SPeter Wemm * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 44405292ba2SArchie Cobbs * order; return an optional length and a pointer to the last character 44505292ba2SArchie Cobbs * written in the buffer (i.e., the first character of the string). 44605292ba2SArchie Cobbs * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 447791d77e0SPoul-Henning Kamp */ 448791d77e0SPoul-Henning Kamp static char * 449f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp) 45005292ba2SArchie Cobbs { 4514d88c459SPoul-Henning Kamp char *p; 452791d77e0SPoul-Henning Kamp 45305292ba2SArchie Cobbs p = nbuf; 454ad4f8dbdSArchie Cobbs *p = '\0'; 455791d77e0SPoul-Henning Kamp do { 4563b1f7e7dSDag-Erling Smørgrav *++p = hex2ascii(num % base); 4573b1f7e7dSDag-Erling Smørgrav } while (num /= base); 4587d921a01SPeter Wemm if (lenp) 4597d921a01SPeter Wemm *lenp = p - nbuf; 4607d921a01SPeter Wemm return (p); 4617d921a01SPeter Wemm } 462791d77e0SPoul-Henning Kamp 463df8bae1dSRodney W. Grimes /* 464df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 465df8bae1dSRodney W. Grimes * 466df8bae1dSRodney W. Grimes * Two additional formats: 467df8bae1dSRodney W. Grimes * 468df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 469df8bae1dSRodney W. Grimes * Its usage is: 470df8bae1dSRodney W. Grimes * 471df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 472df8bae1dSRodney W. Grimes * 473df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 474df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 475df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 476df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 477df8bae1dSRodney W. Grimes * give the name of the register. Thus: 478df8bae1dSRodney W. Grimes * 4794e31a37bSGary Palmer * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 480df8bae1dSRodney W. Grimes * 481df8bae1dSRodney W. Grimes * would produce output: 482df8bae1dSRodney W. Grimes * 483df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 484df8bae1dSRodney W. Grimes * 485120f0783SPoul-Henning Kamp * XXX: %D -- Hexdump, takes pointer and separator string: 486120f0783SPoul-Henning Kamp * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 487120f0783SPoul-Henning Kamp * ("%*D", len, ptr, " " -> XX XX XX XX ... 488df8bae1dSRodney W. Grimes */ 489791d77e0SPoul-Henning Kamp int 490791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 491df8bae1dSRodney W. Grimes { 492791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 493ad4f8dbdSArchie Cobbs char nbuf[MAXNBUF]; 4943b1f7e7dSDag-Erling Smørgrav char *d; 4953b1f7e7dSDag-Erling Smørgrav const char *p, *percent, *q; 496120f0783SPoul-Henning Kamp u_char *up; 497791d77e0SPoul-Henning Kamp int ch, n; 4983b1f7e7dSDag-Erling Smørgrav uintmax_t num; 4997d921a01SPeter Wemm int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 50032869e71SNate Lawson int cflag, hflag, jflag, tflag, zflag; 5014f20e4b1SPoul-Henning Kamp int dwidth; 502df8bae1dSRodney W. Grimes char padc; 503791d77e0SPoul-Henning Kamp int retval = 0; 504791d77e0SPoul-Henning Kamp 5053b1f7e7dSDag-Erling Smørgrav num = 0; 506fe96d47dSPoul-Henning Kamp if (!func) 507791d77e0SPoul-Henning Kamp d = (char *) arg; 508791d77e0SPoul-Henning Kamp else 509fe96d47dSPoul-Henning Kamp d = NULL; 5108f5067baSDavid Greenman 5118f5067baSDavid Greenman if (fmt == NULL) 5122336b9d7SBruce Evans fmt = "(fmt null)\n"; 513b4b2f81eSPoul-Henning Kamp 514120f0783SPoul-Henning Kamp if (radix < 2 || radix > 36) 515b4b2f81eSPoul-Henning Kamp radix = 10; 516b4b2f81eSPoul-Henning Kamp 517df8bae1dSRodney W. Grimes for (;;) { 518df8bae1dSRodney W. Grimes padc = ' '; 519df8bae1dSRodney W. Grimes width = 0; 520e0c95ed9SBruce Evans while ((ch = (u_char)*fmt++) != '%') { 521df8bae1dSRodney W. Grimes if (ch == '\0') 52258a24f79SPoul-Henning Kamp return (retval); 523791d77e0SPoul-Henning Kamp PCHAR(ch); 524df8bae1dSRodney W. Grimes } 5253b1f7e7dSDag-Erling Smørgrav percent = fmt - 1; 5267d921a01SPeter Wemm qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 5274f20e4b1SPoul-Henning Kamp sign = 0; dot = 0; dwidth = 0; 52832869e71SNate Lawson cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; 529e0c95ed9SBruce Evans reswitch: switch (ch = (u_char)*fmt++) { 5304830092aSPoul-Henning Kamp case '.': 5314830092aSPoul-Henning Kamp dot = 1; 5324830092aSPoul-Henning Kamp goto reswitch; 533791d77e0SPoul-Henning Kamp case '#': 534791d77e0SPoul-Henning Kamp sharpflag = 1; 535791d77e0SPoul-Henning Kamp goto reswitch; 536791d77e0SPoul-Henning Kamp case '+': 537791d77e0SPoul-Henning Kamp sign = 1; 538791d77e0SPoul-Henning Kamp goto reswitch; 539791d77e0SPoul-Henning Kamp case '-': 540791d77e0SPoul-Henning Kamp ladjust = 1; 541791d77e0SPoul-Henning Kamp goto reswitch; 542791d77e0SPoul-Henning Kamp case '%': 543791d77e0SPoul-Henning Kamp PCHAR(ch); 544791d77e0SPoul-Henning Kamp break; 545791d77e0SPoul-Henning Kamp case '*': 546ed71c342SPoul-Henning Kamp if (!dot) { 547791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 548791d77e0SPoul-Henning Kamp if (width < 0) { 549791d77e0SPoul-Henning Kamp ladjust = !ladjust; 550791d77e0SPoul-Henning Kamp width = -width; 551791d77e0SPoul-Henning Kamp } 552ed71c342SPoul-Henning Kamp } else { 553ed71c342SPoul-Henning Kamp dwidth = va_arg(ap, int); 554ed71c342SPoul-Henning Kamp } 555791d77e0SPoul-Henning Kamp goto reswitch; 556df8bae1dSRodney W. Grimes case '0': 5574f20e4b1SPoul-Henning Kamp if (!dot) { 558df8bae1dSRodney W. Grimes padc = '0'; 559df8bae1dSRodney W. Grimes goto reswitch; 5604f20e4b1SPoul-Henning Kamp } 561df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 562df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 5634f20e4b1SPoul-Henning Kamp for (n = 0;; ++fmt) { 5644f20e4b1SPoul-Henning Kamp n = n * 10 + ch - '0'; 565df8bae1dSRodney W. Grimes ch = *fmt; 566df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 567df8bae1dSRodney W. Grimes break; 568df8bae1dSRodney W. Grimes } 5694f20e4b1SPoul-Henning Kamp if (dot) 5704f20e4b1SPoul-Henning Kamp dwidth = n; 5714f20e4b1SPoul-Henning Kamp else 5724f20e4b1SPoul-Henning Kamp width = n; 573df8bae1dSRodney W. Grimes goto reswitch; 574df8bae1dSRodney W. Grimes case 'b': 575377a66bcSJohn Baldwin num = (u_int)va_arg(ap, int); 576df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 5773b1f7e7dSDag-Erling Smørgrav for (q = ksprintn(nbuf, num, *p++, NULL); *q;) 578791d77e0SPoul-Henning Kamp PCHAR(*q--); 579df8bae1dSRodney W. Grimes 5803b1f7e7dSDag-Erling Smørgrav if (num == 0) 581df8bae1dSRodney W. Grimes break; 582df8bae1dSRodney W. Grimes 583797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 584797f2d22SPoul-Henning Kamp n = *p++; 5853b1f7e7dSDag-Erling Smørgrav if (num & (1 << (n - 1))) { 586791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 587df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 588791d77e0SPoul-Henning Kamp PCHAR(n); 589df8bae1dSRodney W. Grimes tmp = 1; 590df8bae1dSRodney W. Grimes } else 591df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 592df8bae1dSRodney W. Grimes continue; 593df8bae1dSRodney W. Grimes } 594df8bae1dSRodney W. Grimes if (tmp) 595791d77e0SPoul-Henning Kamp PCHAR('>'); 596df8bae1dSRodney W. Grimes break; 597df8bae1dSRodney W. Grimes case 'c': 598791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 599df8bae1dSRodney W. Grimes break; 600120f0783SPoul-Henning Kamp case 'D': 601120f0783SPoul-Henning Kamp up = va_arg(ap, u_char *); 602120f0783SPoul-Henning Kamp p = va_arg(ap, char *); 603120f0783SPoul-Henning Kamp if (!width) 604120f0783SPoul-Henning Kamp width = 16; 605120f0783SPoul-Henning Kamp while(width--) { 606120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up >> 4)); 607120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up & 0x0f)); 608120f0783SPoul-Henning Kamp up++; 609120f0783SPoul-Henning Kamp if (width) 610120f0783SPoul-Henning Kamp for (q=p;*q;q++) 611120f0783SPoul-Henning Kamp PCHAR(*q); 612120f0783SPoul-Henning Kamp } 613120f0783SPoul-Henning Kamp break; 614df8bae1dSRodney W. Grimes case 'd': 615e0b74464SWarner Losh case 'i': 616df8bae1dSRodney W. Grimes base = 10; 6173b1f7e7dSDag-Erling Smørgrav sign = 1; 6183b1f7e7dSDag-Erling Smørgrav goto handle_sign; 61932869e71SNate Lawson case 'h': 62032869e71SNate Lawson if (hflag) { 62132869e71SNate Lawson hflag = 0; 62232869e71SNate Lawson cflag = 1; 62332869e71SNate Lawson } else 62432869e71SNate Lawson hflag = 1; 62532869e71SNate Lawson goto reswitch; 6263b1f7e7dSDag-Erling Smørgrav case 'j': 6273b1f7e7dSDag-Erling Smørgrav jflag = 1; 6283b1f7e7dSDag-Erling Smørgrav goto reswitch; 629791d77e0SPoul-Henning Kamp case 'l': 630301ca4ffSBrian Feldman if (lflag) { 631301ca4ffSBrian Feldman lflag = 0; 632301ca4ffSBrian Feldman qflag = 1; 633301ca4ffSBrian Feldman } else 634791d77e0SPoul-Henning Kamp lflag = 1; 635791d77e0SPoul-Henning Kamp goto reswitch; 6363b1f7e7dSDag-Erling Smørgrav case 'n': 6373b1f7e7dSDag-Erling Smørgrav if (jflag) 6383b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, intmax_t *)) = retval; 6393b1f7e7dSDag-Erling Smørgrav else if (qflag) 6403b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, quad_t *)) = retval; 6417d921a01SPeter Wemm else if (lflag) 6423b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, long *)) = retval; 6434578a2e6SMaxime Henrion else if (zflag) 6444578a2e6SMaxime Henrion *(va_arg(ap, size_t *)) = retval; 64532869e71SNate Lawson else if (hflag) 64632869e71SNate Lawson *(va_arg(ap, short *)) = retval; 64732869e71SNate Lawson else if (cflag) 64832869e71SNate Lawson *(va_arg(ap, char *)) = retval; 6497d921a01SPeter Wemm else 6503b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, int *)) = retval; 6513b1f7e7dSDag-Erling Smørgrav break; 6523b1f7e7dSDag-Erling Smørgrav case 'o': 653df8bae1dSRodney W. Grimes base = 8; 6543b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 65512d17f65SPoul-Henning Kamp case 'p': 65612d17f65SPoul-Henning Kamp base = 16; 657c41141b0SBruce Evans sharpflag = (width == 0); 6583b1f7e7dSDag-Erling Smørgrav sign = 0; 6593b1f7e7dSDag-Erling Smørgrav num = (uintptr_t)va_arg(ap, void *); 6603b1f7e7dSDag-Erling Smørgrav goto number; 6617d921a01SPeter Wemm case 'q': 6627d921a01SPeter Wemm qflag = 1; 6637d921a01SPeter Wemm goto reswitch; 664e0c38587SBruce Evans case 'r': 665e0c38587SBruce Evans base = radix; 6663b1f7e7dSDag-Erling Smørgrav if (sign) 6673b1f7e7dSDag-Erling Smørgrav goto handle_sign; 6683b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 669791d77e0SPoul-Henning Kamp case 's': 670791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 671791d77e0SPoul-Henning Kamp if (p == NULL) 672791d77e0SPoul-Henning Kamp p = "(null)"; 6734830092aSPoul-Henning Kamp if (!dot) 6744830092aSPoul-Henning Kamp n = strlen (p); 6754830092aSPoul-Henning Kamp else 6764f20e4b1SPoul-Henning Kamp for (n = 0; n < dwidth && p[n]; n++) 6774830092aSPoul-Henning Kamp continue; 6784f20e4b1SPoul-Henning Kamp 6794830092aSPoul-Henning Kamp width -= n; 6804f20e4b1SPoul-Henning Kamp 681791d77e0SPoul-Henning Kamp if (!ladjust && width > 0) 682791d77e0SPoul-Henning Kamp while (width--) 683791d77e0SPoul-Henning Kamp PCHAR(padc); 6844830092aSPoul-Henning Kamp while (n--) 685791d77e0SPoul-Henning Kamp PCHAR(*p++); 686791d77e0SPoul-Henning Kamp if (ladjust && width > 0) 687791d77e0SPoul-Henning Kamp while (width--) 688791d77e0SPoul-Henning Kamp PCHAR(padc); 689791d77e0SPoul-Henning Kamp break; 6902bb95458SMaxime Henrion case 't': 6912bb95458SMaxime Henrion tflag = 1; 6922bb95458SMaxime Henrion goto reswitch; 693f53dbe97SBruce Evans case 'u': 694f53dbe97SBruce Evans base = 10; 6953b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 696df8bae1dSRodney W. Grimes case 'x': 697aa998012SMike Smith case 'X': 698df8bae1dSRodney W. Grimes base = 16; 6993b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 7004578a2e6SMaxime Henrion case 'y': 701e0c38587SBruce Evans base = 16; 70285594430SJohn Baldwin sign = 1; 7033b1f7e7dSDag-Erling Smørgrav goto handle_sign; 7044578a2e6SMaxime Henrion case 'z': 7054578a2e6SMaxime Henrion zflag = 1; 7064578a2e6SMaxime Henrion goto reswitch; 7073b1f7e7dSDag-Erling Smørgrav handle_nosign: 7083b1f7e7dSDag-Erling Smørgrav sign = 0; 7093b1f7e7dSDag-Erling Smørgrav if (jflag) 7103b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, uintmax_t); 7113b1f7e7dSDag-Erling Smørgrav else if (qflag) 7123b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_quad_t); 7132bb95458SMaxime Henrion else if (tflag) 7142bb95458SMaxime Henrion num = va_arg(ap, ptrdiff_t); 7153b1f7e7dSDag-Erling Smørgrav else if (lflag) 7163b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_long); 7174578a2e6SMaxime Henrion else if (zflag) 7184578a2e6SMaxime Henrion num = va_arg(ap, size_t); 71932869e71SNate Lawson else if (hflag) 72032869e71SNate Lawson num = (u_short)va_arg(ap, int); 72132869e71SNate Lawson else if (cflag) 72232869e71SNate Lawson num = (u_char)va_arg(ap, int); 7233b1f7e7dSDag-Erling Smørgrav else 7243b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_int); 725e0c38587SBruce Evans goto number; 7263b1f7e7dSDag-Erling Smørgrav handle_sign: 7273b1f7e7dSDag-Erling Smørgrav if (jflag) 7283b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, intmax_t); 7293b1f7e7dSDag-Erling Smørgrav else if (qflag) 7303b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, quad_t); 7312bb95458SMaxime Henrion else if (tflag) 7322bb95458SMaxime Henrion num = va_arg(ap, ptrdiff_t); 7333b1f7e7dSDag-Erling Smørgrav else if (lflag) 7343b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, long); 7354578a2e6SMaxime Henrion else if (zflag) 7364578a2e6SMaxime Henrion num = va_arg(ap, size_t); 73732869e71SNate Lawson else if (hflag) 73832869e71SNate Lawson num = (short)va_arg(ap, int); 73932869e71SNate Lawson else if (cflag) 74032869e71SNate Lawson num = (char)va_arg(ap, int); 7413b1f7e7dSDag-Erling Smørgrav else 7423b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, int); 7437d921a01SPeter Wemm number: 7443b1f7e7dSDag-Erling Smørgrav if (sign && (intmax_t)num < 0) { 7457d921a01SPeter Wemm neg = 1; 7463b1f7e7dSDag-Erling Smørgrav num = -(intmax_t)num; 7477d921a01SPeter Wemm } 7483b1f7e7dSDag-Erling Smørgrav p = ksprintn(nbuf, num, base, &tmp); 7493b1f7e7dSDag-Erling Smørgrav if (sharpflag && num != 0) { 750791d77e0SPoul-Henning Kamp if (base == 8) 751791d77e0SPoul-Henning Kamp tmp++; 752791d77e0SPoul-Henning Kamp else if (base == 16) 753791d77e0SPoul-Henning Kamp tmp += 2; 754791d77e0SPoul-Henning Kamp } 755791d77e0SPoul-Henning Kamp if (neg) 756791d77e0SPoul-Henning Kamp tmp++; 757791d77e0SPoul-Henning Kamp 758791d77e0SPoul-Henning Kamp if (!ladjust && width && (width -= tmp) > 0) 759df8bae1dSRodney W. Grimes while (width--) 760791d77e0SPoul-Henning Kamp PCHAR(padc); 761791d77e0SPoul-Henning Kamp if (neg) 762791d77e0SPoul-Henning Kamp PCHAR('-'); 7633b1f7e7dSDag-Erling Smørgrav if (sharpflag && num != 0) { 764791d77e0SPoul-Henning Kamp if (base == 8) { 765791d77e0SPoul-Henning Kamp PCHAR('0'); 766791d77e0SPoul-Henning Kamp } else if (base == 16) { 767791d77e0SPoul-Henning Kamp PCHAR('0'); 768791d77e0SPoul-Henning Kamp PCHAR('x'); 769791d77e0SPoul-Henning Kamp } 770791d77e0SPoul-Henning Kamp } 771791d77e0SPoul-Henning Kamp 772797f2d22SPoul-Henning Kamp while (*p) 773791d77e0SPoul-Henning Kamp PCHAR(*p--); 774791d77e0SPoul-Henning Kamp 775791d77e0SPoul-Henning Kamp if (ladjust && width && (width -= tmp) > 0) 776791d77e0SPoul-Henning Kamp while (width--) 777791d77e0SPoul-Henning Kamp PCHAR(padc); 778791d77e0SPoul-Henning Kamp 779df8bae1dSRodney W. Grimes break; 780df8bae1dSRodney W. Grimes default: 7813b1f7e7dSDag-Erling Smørgrav while (percent < fmt) 7823b1f7e7dSDag-Erling Smørgrav PCHAR(*percent++); 783791d77e0SPoul-Henning Kamp break; 784df8bae1dSRodney W. Grimes } 785df8bae1dSRodney W. Grimes } 786791d77e0SPoul-Henning Kamp #undef PCHAR 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes /* 790e2a09b26SPoul-Henning Kamp * Put character in log buffer with a particular priority. 791df8bae1dSRodney W. Grimes */ 792df8bae1dSRodney W. Grimes static void 793e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri) 794e2a09b26SPoul-Henning Kamp { 795e2a09b26SPoul-Henning Kamp static int lastpri = -1; 796e2a09b26SPoul-Henning Kamp static int dangling; 797e2a09b26SPoul-Henning Kamp char nbuf[MAXNBUF]; 798e2a09b26SPoul-Henning Kamp char *p; 799e2a09b26SPoul-Henning Kamp 800e2a09b26SPoul-Henning Kamp if (!msgbufmapped) 801e2a09b26SPoul-Henning Kamp return; 802e2a09b26SPoul-Henning Kamp if (c == '\0' || c == '\r') 803e2a09b26SPoul-Henning Kamp return; 804e2a09b26SPoul-Henning Kamp if (pri != -1 && pri != lastpri) { 805e2a09b26SPoul-Henning Kamp if (dangling) { 8064784a469SIan Dowse msgbuf_addchar(msgbufp, '\n'); 807e2a09b26SPoul-Henning Kamp dangling = 0; 808e2a09b26SPoul-Henning Kamp } 8094784a469SIan Dowse msgbuf_addchar(msgbufp, '<'); 8103b1f7e7dSDag-Erling Smørgrav for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;) 8114784a469SIan Dowse msgbuf_addchar(msgbufp, *p--); 8124784a469SIan Dowse msgbuf_addchar(msgbufp, '>'); 813e2a09b26SPoul-Henning Kamp lastpri = pri; 814e2a09b26SPoul-Henning Kamp } 8154784a469SIan Dowse msgbuf_addchar(msgbufp, c); 816e2a09b26SPoul-Henning Kamp if (c == '\n') { 817e2a09b26SPoul-Henning Kamp dangling = 0; 818e2a09b26SPoul-Henning Kamp lastpri = -1; 819e2a09b26SPoul-Henning Kamp } else { 820e2a09b26SPoul-Henning Kamp dangling = 1; 821e2a09b26SPoul-Henning Kamp } 822e2a09b26SPoul-Henning Kamp } 823e2a09b26SPoul-Henning Kamp 824e796e00dSPoul-Henning Kamp void 82501ee4395SThomas Moestl msgbufinit(void *ptr, int size) 826e796e00dSPoul-Henning Kamp { 827e796e00dSPoul-Henning Kamp char *cp; 828eb9d435aSJonathan Lemon static struct msgbuf *oldp = NULL; 829e796e00dSPoul-Henning Kamp 830cca8f980SIan Dowse size -= sizeof(*msgbufp); 831e796e00dSPoul-Henning Kamp cp = (char *)ptr; 832cca8f980SIan Dowse msgbufp = (struct msgbuf *)(cp + size); 8334784a469SIan Dowse msgbuf_reinit(msgbufp, cp, size); 834eb9d435aSJonathan Lemon if (msgbufmapped && oldp != msgbufp) 8354784a469SIan Dowse msgbuf_copy(oldp, msgbufp); 836e796e00dSPoul-Henning Kamp msgbufmapped = 1; 837eb9d435aSJonathan Lemon oldp = msgbufp; 838e796e00dSPoul-Henning Kamp } 839e796e00dSPoul-Henning Kamp 840d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd); 8416f3933faSRobert Watson 8426f3933faSRobert Watson static int unprivileged_read_msgbuf = 1; 843d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf, 8446f3933faSRobert Watson CTLFLAG_RW, &unprivileged_read_msgbuf, 0, 8456f3933faSRobert Watson "Unprivileged processes may read the kernel message buffer"); 8466f3933faSRobert Watson 847948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */ 848948d3d94SThomas Moestl static int 849948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 850948d3d94SThomas Moestl { 8514784a469SIan Dowse char buf[128]; 8524784a469SIan Dowse u_int seq; 8534784a469SIan Dowse int error, len; 854948d3d94SThomas Moestl 8556f3933faSRobert Watson if (!unprivileged_read_msgbuf) { 85644731cabSJohn Baldwin error = suser(req->td); 8576f3933faSRobert Watson if (error) 8586f3933faSRobert Watson return (error); 8596f3933faSRobert Watson } 8606f3933faSRobert Watson 8614784a469SIan Dowse /* Read the whole buffer, one chunk at a time. */ 8624784a469SIan Dowse msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 8634784a469SIan Dowse while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 8644784a469SIan Dowse error = sysctl_handle_opaque(oidp, buf, len, req); 865948d3d94SThomas Moestl if (error) 866948d3d94SThomas Moestl return (error); 867948d3d94SThomas Moestl } 8684784a469SIan Dowse return (0); 869948d3d94SThomas Moestl } 870948d3d94SThomas Moestl 871948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 872948d3d94SThomas Moestl 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 873948d3d94SThomas Moestl 8744784a469SIan Dowse static int msgbuf_clearflag; 875948d3d94SThomas Moestl 876948d3d94SThomas Moestl static int 877948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 878948d3d94SThomas Moestl { 879948d3d94SThomas Moestl int error; 880948d3d94SThomas Moestl error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 881948d3d94SThomas Moestl if (!error && req->newptr) { 8824784a469SIan Dowse msgbuf_clear(msgbufp); 8834784a469SIan Dowse msgbuf_clearflag = 0; 884948d3d94SThomas Moestl } 885948d3d94SThomas Moestl return (error); 886948d3d94SThomas Moestl } 887948d3d94SThomas Moestl 888948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 8894784a469SIan Dowse CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0, 890948d3d94SThomas Moestl sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); 891948d3d94SThomas Moestl 892e796e00dSPoul-Henning Kamp #ifdef DDB 893e796e00dSPoul-Henning Kamp 894e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 895e796e00dSPoul-Henning Kamp { 896e796e00dSPoul-Henning Kamp int i, j; 897e796e00dSPoul-Henning Kamp 898e796e00dSPoul-Henning Kamp if (!msgbufmapped) { 899e796e00dSPoul-Henning Kamp db_printf("msgbuf not mapped yet\n"); 900e796e00dSPoul-Henning Kamp return; 901e796e00dSPoul-Henning Kamp } 902e796e00dSPoul-Henning Kamp db_printf("msgbufp = %p\n", msgbufp); 9034784a469SIan Dowse db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n", 9044784a469SIan Dowse msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq, 9054784a469SIan Dowse msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum); 906e796e00dSPoul-Henning Kamp for (i = 0; i < msgbufp->msg_size; i++) { 9074784a469SIan Dowse j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq); 908e796e00dSPoul-Henning Kamp db_printf("%c", msgbufp->msg_ptr[j]); 909e796e00dSPoul-Henning Kamp } 910e796e00dSPoul-Henning Kamp db_printf("\n"); 911e796e00dSPoul-Henning Kamp } 912e796e00dSPoul-Henning Kamp 913e796e00dSPoul-Henning Kamp #endif /* DDB */ 91477411499SScott Long 91577411499SScott Long void 91630a1695bSPoul-Henning Kamp hexdump(const void *ptr, int length, const char *hdr, int flags) 91777411499SScott Long { 91877411499SScott Long int i, j, k; 91977411499SScott Long int cols; 92030a1695bSPoul-Henning Kamp const unsigned char *cp; 92177411499SScott Long char delim; 92277411499SScott Long 92377411499SScott Long if ((flags & HD_DELIM_MASK) != 0) 92477411499SScott Long delim = (flags & HD_DELIM_MASK) >> 8; 92577411499SScott Long else 92677411499SScott Long delim = ' '; 92777411499SScott Long 92877411499SScott Long if ((flags & HD_COLUMN_MASK) != 0) 92977411499SScott Long cols = flags & HD_COLUMN_MASK; 93077411499SScott Long else 93177411499SScott Long cols = 16; 93277411499SScott Long 93377411499SScott Long cp = ptr; 93477411499SScott Long for (i = 0; i < length; i+= cols) { 93577411499SScott Long if (hdr != NULL) 93677411499SScott Long printf("%s", hdr); 93777411499SScott Long 93877411499SScott Long if ((flags & HD_OMIT_COUNT) == 0) 93977411499SScott Long printf("%04x ", i); 94077411499SScott Long 94177411499SScott Long if ((flags & HD_OMIT_HEX) == 0) { 94277411499SScott Long for (j = 0; j < cols; j++) { 94377411499SScott Long k = i + j; 94477411499SScott Long if (k < length) 94577411499SScott Long printf("%c%02x", delim, cp[k]); 94677411499SScott Long else 94777411499SScott Long printf(" "); 94877411499SScott Long } 94977411499SScott Long } 95077411499SScott Long 95177411499SScott Long if ((flags & HD_OMIT_CHARS) == 0) { 95277411499SScott Long printf(" |"); 95377411499SScott Long for (j = 0; j < cols; j++) { 95477411499SScott Long k = i + j; 95577411499SScott Long if (k >= length) 95677411499SScott Long printf(" "); 95777411499SScott Long else if (cp[k] >= ' ' && cp[k] <= '~') 95877411499SScott Long printf("%c", cp[k]); 95977411499SScott Long else 96077411499SScott Long printf("."); 96177411499SScott Long } 96277411499SScott Long printf("|\n"); 96377411499SScott Long } 96477411499SScott Long } 96577411499SScott Long } 96677411499SScott Long 967