* Copyright (c) 1986, 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
* Note that stdarg.h and the ANSI style va_start macro is used for both
* ANSI and traditional C compilers.
#include <machine/stdarg.h>
#include <machine/kdbparam.h>
struct tty
*constty
; /* pointer to console "window" tty */
extern cnputc(); /* standard console putc */
int (*v_putc
)() = cnputc
; /* routine to putc on virtual console */
void logpri
__P((int level
));
static void putchar
__P((int ch
, int flags
, struct tty
*tp
));
static char *ksprintn
__P((u_long num
, int base
, int *len
));
void kprintf
__P((const char *fmt
, int flags
, struct tty
*tp
, va_list ap
));
int consintr
= 1; /* Ok to handle console interrupts? */
* Variable panicstr contains argument to first call to panic; used as flag
* to indicate that the kernel has already called panic.
* Panic is called on unresolvable fatal errors. It prints "panic: mesg",
* and then reboots. If we are called twice, then we avoid trying to sync
* the disks as this often leads to recursive panics.
volatile void boot(int flags
); /* boot() does not return */
volatile /* panic() does not return */
panic(const char *fmt
, ...)
bootopt
= RB_AUTOBOOT
| RB_DUMP
;
printf("panic: %r\n", fmt
, ap
);
* Warn that a system table is full.
log(LOG_ERR
, "%s: table is full\n", tab
);
* Uprintf prints to the controlling terminal for the current process.
* It may block if the tty queue is overfull. No message is printed if
* the queue does not clear in a reasonable time.
uprintf(const char *fmt
, ...)
register struct proc
*p
= curproc
;
if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
kprintf(fmt
, TOTTY
, p
->p_session
->s_ttyp
, ap
);
if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
return ((tpr_t
) p
->p_session
);
SESSRELE((struct session
*) sess
);
* tprintf prints on the controlling terminal associated
* with the given session.
tprintf(tpr_t tpr
, const char *fmt
, ...)
tprintf(tpr
, fmt
, va_alist
)
register struct session
*sess
= (struct session
*)tpr
;
if (sess
&& sess
->s_ttyvp
&& ttycheckoutq(sess
->s_ttyp
, 0)) {
kprintf(fmt
, flags
, tp
, ap
);
* Ttyprintf displays a message on a tty; it should be used only by
* the tty driver, or anything that knows the underlying tty will not
* be revoke(2)'d away. Other callers should use tprintf.
ttyprintf(struct tty
*tp
, const char *fmt
, ...)
ttyprintf(tp
, fmt
, va_alist
)
kprintf(fmt
, TOTTY
, tp
, ap
);
* Log writes to the log buffer, and guarantees not to sleep (so can be
* called by interrupt routines). If there is no process reading the
* log yet, it writes to the console also.
log(int level
, const char *fmt
, ...)
log(level
, fmt
, va_alist
)
kprintf(fmt
, TOLOG
, NULL
, ap
);
kprintf(fmt
, TOCONS
, NULL
, ap
);
putchar('<', TOLOG
, NULL
);
for (p
= ksprintn((u_long
)level
, 10, NULL
); ch
= *p
--;)
putchar(ch
, TOLOG
, NULL
);
putchar('>', TOLOG
, NULL
);
addlog(const char *fmt
, ...)
kprintf(fmt
, TOLOG
, NULL
, ap
);
kprintf(fmt
, TOCONS
, NULL
, ap
);
printf(const char *fmt
, ...)
savintr
= consintr
; /* disable interrupts */
kprintf(fmt
, TOCONS
| TOLOG
, NULL
, ap
);
consintr
= savintr
; /* reenable interrupts */
* Scaled down version of printf(3).
* Two additional formats:
* The format %b is supported to decode error registers.
* printf("reg=%b\n", regval, "<base><arg>*");
* where <base> is the output base expressed as a control character, e.g.
* \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
* the first of which gives the bit number to be inspected (origin 1), and
* the next characters (up to a control character, i.e. a character <= 32),
* give the name of the register. Thus:
* kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
* The format %r passes an additional format string and argument list
* recursively. Its usage is:
* printf("prefix: %r: suffix\n", fmt, ap);
* Space or zero padding and a field width are supported for the numeric
kprintf(fmt
, flags
, tp
, ap
)
register const char *fmt
;
int base
, lflag
, tmp
, width
;
while ((ch
= *(u_char
*)fmt
++) != '%') {
reswitch
: switch (ch
= *(u_char
*)fmt
++) {
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
for (width
= 0;; ++fmt
) {
width
= width
* 10 + ch
- '0';
if (ch
< '0' || ch
> '9')
for (q
= ksprintn(ul
, *p
++, NULL
); ch
= *q
--;)
for (tmp
= 0; n
= *p
++;) {
if (ul
& (1 << (n
- 1))) {
putchar(tmp
? ',' : '<', flags
, tp
);
for (; (n
= *p
) > ' '; ++p
)
putchar(va_arg(ap
, int), flags
, tp
);
kprintf(p
, flags
, tp
, va_arg(ap
, va_list));
ul
= lflag
? va_arg(ap
, long) : va_arg(ap
, int);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
number
: p
= ksprintn(ul
, base
, &tmp
);
if (width
&& (width
-= tmp
) > 0)
putchar(padc
, flags
, tp
);
* Print a character on console or users terminal. If destination is
* the console then the last MSGBUFS characters are saved in msgbuf for
register struct msgbuf
*mbp
;
if ((flags
& TOCONS
) && tp
== NULL
&& constty
) {
if ((flags
& TOTTY
) && tp
&& tputchar(c
, tp
) < 0 &&
(flags
& TOCONS
) && tp
== constty
)
c
!= '\0' && c
!= '\r' && c
!= 0177 && msgbufmapped
) {
if (mbp
->msg_magic
!= MSG_MAGIC
) {
bzero((caddr_t
)mbp
, sizeof(*mbp
));
mbp
->msg_magic
= MSG_MAGIC
;
mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
if (mbp
->msg_bufx
< 0 || mbp
->msg_bufx
>= MSG_BSIZE
)
if ((flags
& TOCONS
) && constty
== NULL
&& c
!= '\0')
* Scaled down version of sprintf(3).
sprintf(char *buf
, const char *cfmt
, ...)
sprintf(buf
, cfmt
, va_alist
)
register const char *fmt
= cfmt
;
while ((ch
= *(u_char
*)fmt
++) != '%')
if ((*bp
++ = ch
) == '\0')
reswitch
: switch (ch
= *(u_char
*)fmt
++) {
ul
= lflag
? va_arg(ap
, long) : va_arg(ap
, int);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
number
: for (p
= ksprintn(ul
, base
, NULL
); ch
= *p
--;)
* Put a number (base <= 16) in a buffer in reverse order; return an
* optional length and a pointer to the NULL terminated (preceded?)
register int base
, *lenp
;
{ /* A long in base 8, plus NULL. */
static char buf
[sizeof(long) * NBBY
/ 3 + 2];
*++p
= "0123456789abcdef"[ul
% base
];