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 <sys/subr_prf.h>
95 #include <kern/cpu_number.h> /* for cpu_number() */
96 #include <machine/spl.h>
97 #include <libkern/libkern.h>
106 * In case console is off,
107 * panicstr contains argument to last
110 extern const char *panicstr
;
112 extern cnputc(); /* standard console putc */
113 int (*v_putc
)() = cnputc
; /* routine to putc on virtual console */
115 extern struct tty cons
; /* standard console tty */
116 extern struct tty
*constty
; /* pointer to console "window" tty */
117 extern int __doprnt(const char *fmt
,
119 void (*putc
)(int, void *arg
),
124 * Record cpu that panic'd and lock around panic data
127 static void puts(const char *s
, int flags
, struct tty
*ttyp
);
128 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
);
130 /* MP printf stuff */
131 decl_simple_lock_data(,printf_lock
)
133 boolean_t new_printf_cpu_number
; /* do we need to output who we are */
136 extern void logwakeup();
137 extern void halt_cpu();
142 snprintf_func(int ch
, void *arg
);
144 struct putchar_args
{
148 static void putchar(int c
, void *arg
);
152 * Uprintf prints to the controlling terminal for the current process.
153 * It may block if the tty queue is overfull. No message is printed if
154 * the queue does not clear in a reasonable time.
157 uprintf(const char *fmt
, ...)
159 register struct proc
*p
= current_proc();
160 struct putchar_args pca
;
164 pca
.tty
= (struct tty
*)p
->p_session
->s_ttyp
;
166 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
168 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
175 register struct proc
*p
;
177 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
178 SESSHOLD(p
->p_session
);
179 return ((tpr_t
) p
->p_session
);
181 return ((tpr_t
) NULL
);
189 SESSRELE((struct session
*) sess
);
193 * tprintf prints on the controlling terminal associated
194 * with the given session.
197 tprintf(tpr_t tpr
, const char *fmt
, ...)
199 register struct session
*sess
= (struct session
*)tpr
;
200 struct tty
*tp
= NULL
;
203 struct putchar_args pca
;
206 if (sess
&& sess
->s_ttyvp
&& ttycheckoutq(sess
->s_ttyp
, 0)) {
215 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
222 * Ttyprintf displays a message on a tty; it should be used only by
223 * the tty driver, or anything that knows the underlying tty will not
224 * be revoke(2)'d away. Other callers should use tprintf.
227 ttyprintf(struct tty
*tp
, const char *fmt
, ...)
232 struct putchar_args pca
;
237 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
249 struct putchar_args pca
;
254 printn((u_long
)level
, 10, TOLOG
, (struct tty
*)0, 0, 0);
259 addlog(const char *fmt
, ...)
261 register s
= splhigh();
263 struct putchar_args pca
;
269 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
274 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
279 void _printf(int flags
, struct tty
*ttyp
, const char *format
, ...)
282 struct putchar_args pca
;
287 va_start(ap
, format
);
288 __doprnt(format
, &ap
, putchar
, &pca
, 10);
292 int prf(const char *fmt
, va_list ap
, int flags
, struct tty
*ttyp
)
294 struct putchar_args pca
;
300 int cpun
= cpu_number();
303 simple_lock(&printf_lock
);
307 if (cpun
!= master_cpu
)
308 new_printf_cpu_number
= TRUE
;
310 if (new_printf_cpu_number
) {
311 putchar('{', flags
, ttyp
);
312 printn((u_long
)cpun
, 10, flags
, ttyp
, 0, 0);
313 putchar('}', flags
, ttyp
);
315 #endif /* NCPUS > 1 */
317 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
321 simple_unlock(&printf_lock
);
329 static void puts(const char *s
, int flags
, struct tty
*ttyp
)
332 struct putchar_args pca
;
342 * Printn prints a number n in base b.
343 * We don't use recursion to avoid deep kernel stacks.
345 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
)
349 struct putchar_args pca
;
354 if (b
== 10 && (int)n
< 0) {
356 n
= (unsigned)(-(int)n
);
360 *cp
++ = "0123456789abcdef"[n%b
];
364 for (fld_size
-= cp
- prbuf
; fld_size
> 0; fld_size
--)
371 putchar(*--cp
, &pca
);
378 * Warn that a system table is full.
380 void tablefull(const char *tab
)
382 log(LOG_ERR
, "%s: table is full\n", tab
);
386 * Print a character on console or users terminal.
387 * If destination is console then the last MSGBUFS characters
388 * are saved in msgbuf for inspection later.
392 putchar(int c
, void *arg
)
394 struct putchar_args
*pca
= arg
;
395 register struct msgbuf
*mbp
;
396 char **sp
= (char**) pca
->tty
;
400 if ((pca
->flags
& TOCONS
) && pca
->tty
== NULL
&& constty
) {
404 if ((pca
->flags
& TOTTY
) && pca
->tty
&& tputchar(c
, pca
->tty
) < 0 &&
405 (pca
->flags
& TOCONS
) && pca
->tty
== constty
)
407 if ((pca
->flags
& TOLOG
) && c
!= '\0' && c
!= '\r' && c
!= 0177)
409 if ((pca
->flags
& TOCONS
) && constty
== 0 && c
!= '\0')
411 if (pca
->flags
& TOSTR
) {
420 * Scaled down version of vsprintf(3).
423 vsprintf(char *buf
, const char *cfmt
, va_list ap
)
426 struct snprintf_arg info
;
429 info
.remain
= 999999;
431 retval
= __doprnt(cfmt
, &ap
, snprintf_func
, &info
, 10);
432 if (info
.remain
>= 1) {
439 * Scaled down version of snprintf(3).
442 snprintf(char *str
, size_t size
, const char *format
, ...)
447 va_start(ap
, format
);
448 retval
= vsnprintf(str
, size
, format
, ap
);
454 * Scaled down version of vsnprintf(3).
457 vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
459 struct snprintf_arg info
;
464 retval
= __doprnt(format
, &ap
, snprintf_func
, &info
, 10);
465 if (info
.remain
>= 1)
471 snprintf_func(int ch
, void *arg
)
473 struct snprintf_arg
*const info
= arg
;
475 if (info
->remain
>= 2) {
482 kvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
484 __doprnt(fmt
, &ap
, func
, arg
, radix
);