2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1986, 1988, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
63 * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com)
64 * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
66 * 17-July-97 Umesh Vaishampayan (umeshv@apple.com)
67 * Eliminated multiple definition of constty which is defined
68 * in bsd/dev/XXX/cons.c
70 * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com
71 * Fixed tharshing format in many functions. Cleanup.
73 * 17-Jun-1995 Mac Gillon (mgillon) at NeXT
75 * New version based on 4.4 and NS3.3
78 #include <sys/param.h>
79 #include <sys/systm.h>
82 #include <sys/reboot.h>
83 #include <sys/msgbuf.h>
85 #include <sys/ioctl.h>
88 #include <sys/tprintf.h>
89 #include <sys/syslog.h>
91 #include <sys/malloc.h>
93 #include <kern/parallel.h>
94 #include <sys/subr_prf.h>
96 #include <kern/cpu_number.h> /* for cpu_number() */
97 #include <machine/spl.h>
98 #include <libkern/libkern.h>
100 struct snprintf_arg
{
107 * In case console is off,
108 * panicstr contains argument to last
111 extern const char *panicstr
;
113 extern cnputc(); /* standard console putc */
114 int (*v_putc
)() = cnputc
; /* routine to putc on virtual console */
116 extern struct tty cons
; /* standard console tty */
117 extern struct tty
*constty
; /* pointer to console "window" tty */
120 * Record cpu that panic'd and lock around panic data
123 static void puts(const char *s
, int flags
, struct tty
*ttyp
);
124 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
);
126 /* MP printf stuff */
127 decl_simple_lock_data(,printf_lock
)
129 boolean_t new_printf_cpu_number
; /* do we need to output who we are */
132 extern void logwakeup();
133 extern void halt_cpu();
138 snprintf_func(int ch
, void *arg
);
143 * Uprintf prints to the controlling terminal for the current process.
144 * It may block if the tty queue is overfull. No message is printed if
145 * the queue does not clear in a reasonable time.
148 uprintf(const char *fmt
, ...)
150 register struct proc
*p
= current_proc();
153 unix_master(); /* sessions, sigh */
154 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
156 prf(fmt
, ap
, TOTTY
, (struct tty
*)p
->p_session
->s_ttyvp
);
164 register struct proc
*p
;
166 unix_master(); /* sessions, sigh */
167 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
168 SESSHOLD(p
->p_session
);
170 return ((tpr_t
) p
->p_session
);
173 return ((tpr_t
) NULL
);
180 unix_master(); /* sessions, sigh */
182 SESSRELE((struct session
*) sess
);
187 * tprintf prints on the controlling terminal associated
188 * with the given session.
191 tprintf(tpr_t tpr
, const char *fmt
, ...)
193 register struct session
*sess
= (struct session
*)tpr
;
194 struct tty
*tp
= NULL
;
199 unix_master(); /* sessions, sigh */
200 if (sess
&& sess
->s_ttyvp
&& ttycheckoutq(sess
->s_ttyp
, 0)) {
206 prf(fmt
, ap
, TOTTY
, tp
);
214 * Ttyprintf displays a message on a tty; it should be used only by
215 * the tty driver, or anything that knows the underlying tty will not
216 * be revoke(2)'d away. Other callers should use tprintf.
219 ttyprintf(struct tty
*tp
, const char *fmt
, ...)
225 prf(fmt
, ap
, TOTTY
, tp
);
238 putchar('<', TOLOG
, (struct tty
*)0);
239 printn((u_long
)level
, 10, TOLOG
, (struct tty
*)0, 0, 0);
240 putchar('>', TOLOG
, (struct tty
*)0);
244 addlog(const char *fmt
, ...)
246 register s
= splhigh();
250 prf(fmt
, ap
, TOLOG
, (struct tty
*)0);
253 prf(fmt
, ap
, TOCONS
, (struct tty
*)0);
257 void _printf(int flags
, struct tty
*ttyp
, const char *format
, ...)
261 va_start(ap
, format
);
262 prf(format
, ap
, flags
, ttyp
);
266 int prf(const char *fmt
, va_list ap
, int flags
, struct tty
*ttyp
)
268 register int b
, c
, i
;
271 int zf
= 0, fld_size
;
274 int cpun
= cpu_number();
277 simple_lock(&printf_lock
);
281 if (cpun
!= master_cpu
)
282 new_printf_cpu_number
= TRUE
;
284 if (new_printf_cpu_number
) {
285 putchar('{', flags
, ttyp
);
286 printn((u_long
)cpun
, 10, flags
, ttyp
, 0, 0);
287 putchar('}', flags
, ttyp
);
289 #endif /* NCPUS > 1 */
291 while ((c
= *fmt
++) != '%') {
295 simple_unlock(&printf_lock
);
301 putchar(c
, flags
, ttyp
);
310 for (;c
<= '9' && c
>= '0'; c
= *fmt
++)
311 fld_size
= fld_size
* 10 + c
- '0';
313 /* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */
322 case 'u': /* what a joke */
328 printn(va_arg(ap
, unsigned), b
, flags
, ttyp
, zf
, fld_size
);
331 b
= va_arg(ap
, unsigned);
332 #if BYTE_ORDER == LITTLE_ENDIAN
333 for (i
= 24; i
>= 0; i
-= 8)
334 if (c
= (b
>> i
) & 0x7f)
335 putchar(c
, flags
, ttyp
);
337 #if BYTE_ORDER == BIG_ENDIAN
338 if ((c
= (b
& 0x7f)))
339 putchar(c
, flags
, ttyp
);
343 b
= va_arg(ap
, unsigned);
344 s
= va_arg(ap
, char *);
345 printn((u_long
)b
, *s
++, flags
, ttyp
, 0, 0);
353 putchar(',', flags
, ttyp
);
355 for (; (c
= *s
) > 32 ; s
++)
356 putchar(c
, flags
, ttyp
);
357 printn( (u_long
)( (b
>> (j
-1)) &
359 8, flags
, ttyp
, 0, 0);
360 } else if (b
& (1 << (i
-1))) {
361 putchar(any
? ',' : '<', flags
, ttyp
);
363 for (; (c
= *s
) > 32; s
++)
364 putchar(c
, flags
, ttyp
);
369 putchar('>', flags
, ttyp
);
374 s
= va_arg(ap
, char *);
377 while (fld_size
-- > 0)
378 putchar((c
= *s
++)? c
: '_', flags
, ttyp
);
381 putchar(c
, flags
, ttyp
);
385 putchar(c
, flags
, ttyp
);
390 putchar('%', flags
, ttyp
);
393 b
= va_arg(ap
, unsigned);
394 #if BYTE_ORDER == LITTLE_ENDIAN
395 for (i
= 24; i
>= 0; i
-= 8)
396 if (c
= (b
>> i
) & 0x7f)
397 putchar(c
, flags
, ttyp
);
399 #if BYTE_ORDER == BIG_ENDIAN
400 if ((c
= (b
& 0x7f)))
401 putchar(c
, flags
, ttyp
);
406 b
= va_arg(ap
, unsigned);
407 s
= va_arg(ap
, char *);
409 puts("0x", flags
, ttyp
);
410 printn((u_long
)b
, 16, flags
, ttyp
, 0, 0);
414 register struct reg_desc
*rd
;
415 register struct reg_values
*rv
;
418 putchar('<', flags
, ttyp
);
419 for (rd
= (struct reg_desc
*)s
; rd
->rd_mask
; rd
++) {
420 field
= b
& rd
->rd_mask
;
421 field
= (rd
->rd_shift
> 0)
422 ? field
<< rd
->rd_shift
423 : field
>> -rd
->rd_shift
;
425 (rd
->rd_format
|| rd
->rd_values
426 || (rd
->rd_name
&& field
)
429 putchar(',', flags
, ttyp
);
431 if (rd
->rd_format
|| rd
->rd_values
433 puts(rd
->rd_name
, flags
, ttyp
);
436 if (rd
->rd_format
|| rd
->rd_values
) {
437 putchar('=', flags
, ttyp
);
442 _printf(flags
, ttyp
, rd
->rd_format
,
446 putchar(':', flags
, ttyp
);
450 for (rv
= rd
->rd_values
;
453 if (field
== rv
->rv_value
) {
454 puts(rv
->rv_name
, flags
,
459 if (rv
->rv_name
== NULL
)
460 puts("???", flags
, ttyp
);
463 putchar('>', flags
, ttyp
);
470 register struct reg_values
*rv
;
472 b
= va_arg(ap
, unsigned);
473 s
= va_arg(ap
,char *);
474 for (rv
= (struct reg_values
*)s
; rv
->rv_name
; rv
++) {
475 if (b
== rv
->rv_value
) {
476 puts(rv
->rv_name
, flags
, ttyp
);
480 if (rv
->rv_name
== NULL
)
481 puts("???", flags
, ttyp
);
482 if (c
== 'N' || rv
->rv_name
== NULL
) {
483 putchar(':', flags
, ttyp
);
484 printn((u_long
)b
, 10, flags
, ttyp
, 0, 0);
492 static void puts(const char *s
, int flags
, struct tty
*ttyp
)
497 putchar(c
, flags
, ttyp
);
501 * Printn prints a number n in base b.
502 * We don't use recursion to avoid deep kernel stacks.
504 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
)
509 if (b
== 10 && (int)n
< 0) {
510 putchar('-', flags
, ttyp
);
511 n
= (unsigned)(-(int)n
);
515 *cp
++ = "0123456789abcdef"[n%b
];
519 for (fld_size
-= cp
- prbuf
; fld_size
> 0; fld_size
--)
521 putchar('0', flags
, ttyp
);
523 putchar(' ', flags
, ttyp
);
526 putchar(*--cp
, flags
, ttyp
);
533 * Warn that a system table is full.
535 void tablefull(const char *tab
)
537 log(LOG_ERR
, "%s: table is full\n", tab
);
541 * Print a character on console or users terminal.
542 * If destination is console then the last MSGBUFS characters
543 * are saved in msgbuf for inspection later.
547 putchar(c
, flags
, tp
)
551 register struct msgbuf
*mbp
;
552 char **sp
= (char**) tp
;
556 if ((flags
& TOCONS
) && tp
== NULL
&& constty
) {
560 if ((flags
& TOTTY
) && tp
&& tputchar(c
, tp
) < 0 &&
561 (flags
& TOCONS
) && tp
== constty
)
563 if ((flags
& TOLOG
) && c
!= '\0' && c
!= '\r' && c
!= 0177)
565 if ((flags
& TOCONS
) && constty
== 0 && c
!= '\0')
577 * Scaled down version of vsprintf(3).
580 vsprintf(char *buf
, const char *cfmt
, va_list ap
)
584 retval
= kvprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
590 * Scaled down version of snprintf(3).
593 snprintf(char *str
, size_t size
, const char *format
, ...)
598 va_start(ap
, format
);
599 retval
= vsnprintf(str
, size
, format
, ap
);
605 * Scaled down version of vsnprintf(3).
608 vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
610 struct snprintf_arg info
;
615 retval
= kvprintf(format
, snprintf_func
, &info
, 10, ap
);
616 if (info
.remain
>= 1)
622 snprintf_func(int ch
, void *arg
)
624 struct snprintf_arg
*const info
= arg
;
626 if (info
->remain
>= 2) {
633 * Put a number (base <= 16) in a buffer in reverse order; return an
634 * optional length and a pointer to the NULL terminated (preceded?)
638 ksprintn(ul
, base
, lenp
)
640 register int base
, *lenp
;
641 { /* A long in base 8, plus NULL. */
642 static char buf
[sizeof(long) * NBBY
/ 3 + 2];
647 *++p
= hex2ascii(ul
% base
);
648 } while (ul
/= base
);
655 * Scaled down version of printf(3).
657 * Two additional formats:
659 * The format %b is supported to decode error registers.
662 * printf("reg=%b\n", regval, "<base><arg>*");
664 * where <base> is the output base expressed as a control character, e.g.
665 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
666 * the first of which gives the bit number to be inspected (origin 1), and
667 * the next characters (up to a control character, i.e. a character <= 32),
668 * give the name of the register. Thus:
670 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
672 * would produce output:
674 * reg=3<BITTWO,BITONE>
676 * XXX: %D -- Hexdump, takes pointer and separator string:
677 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
678 * ("%*D", len, ptr, " " -> XX XX XX XX ...
681 kvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
683 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
688 int base
, lflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
699 fmt
= "(fmt null)\n";
701 if (radix
< 2 || radix
> 36)
707 while ((ch
= (u_char
)*fmt
++) != '%') {
712 lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
713 sign
= 0; dot
= 0; dwidth
= 0;
714 reswitch
: switch (ch
= (u_char
)*fmt
++) {
732 width
= va_arg(ap
, int);
738 dwidth
= va_arg(ap
, int);
746 case '1': case '2': case '3': case '4':
747 case '5': case '6': case '7': case '8': case '9':
748 for (n
= 0;; ++fmt
) {
749 n
= n
* 10 + ch
- '0';
751 if (ch
< '0' || ch
> '9')
760 ul
= va_arg(ap
, int);
761 p
= va_arg(ap
, char *);
762 for (q
= ksprintn(ul
, *p
++, NULL
); *q
;)
770 if (ul
& (1 << (n
- 1))) {
771 PCHAR(tmp
? ',' : '<');
772 for (; (n
= *p
) > ' '; ++p
)
776 for (; *p
> ' '; ++p
)
783 PCHAR(va_arg(ap
, int));
786 up
= va_arg(ap
, u_char
*);
787 p
= va_arg(ap
, char *);
791 PCHAR(hex2ascii(*up
>> 4));
792 PCHAR(hex2ascii(*up
& 0x0f));
800 ul
= lflag
? va_arg(ap
, long) : va_arg(ap
, int);
808 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
812 ul
= (uintptr_t)va_arg(ap
, void *);
814 sharpflag
= (width
== 0);
818 ul
= lflag
? va_arg(ap
, u_long
) :
819 sign
? (u_long
)va_arg(ap
, int) : va_arg(ap
, u_int
);
823 p
= va_arg(ap
, char *);
829 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
834 if (!ladjust
&& width
> 0)
839 if (ladjust
&& width
> 0)
844 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
848 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
852 ul
= lflag
? va_arg(ap
, u_long
) :
853 sign
? (u_long
)va_arg(ap
, int) : va_arg(ap
, u_int
);
857 number
: if (sign
&& (long)ul
< 0L) {
861 p
= ksprintn(ul
, base
, &tmp
);
862 if (sharpflag
&& ul
!= 0) {
871 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
876 if (sharpflag
&& ul
!= 0) {
879 } else if (base
== 16) {
888 if (ladjust
&& width
&& (width
-= tmp
) > 0)