2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1986, 1988, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
66 * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com)
67 * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
69 * 17-July-97 Umesh Vaishampayan (umeshv@apple.com)
70 * Eliminated multiple definition of constty which is defined
71 * in bsd/dev/XXX/cons.c
73 * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com
74 * Fixed tharshing format in many functions. Cleanup.
76 * 17-Jun-1995 Mac Gillon (mgillon) at NeXT
78 * New version based on 4.4 and NS3.3
81 #include <sys/param.h>
82 #include <sys/systm.h>
85 #include <sys/reboot.h>
86 #include <sys/msgbuf.h>
88 #include <sys/ioctl.h>
91 #include <sys/tprintf.h>
92 #include <sys/syslog.h>
94 #include <sys/malloc.h>
96 #include <sys/subr_prf.h>
98 #include <kern/cpu_number.h> /* for cpu_number() */
99 #include <machine/spl.h>
100 #include <libkern/libkern.h>
102 struct snprintf_arg
{
109 * In case console is off,
110 * panicstr contains argument to last
113 extern const char *panicstr
;
115 extern cnputc(); /* standard console putc */
116 int (*v_putc
)() = cnputc
; /* routine to putc on virtual console */
118 extern struct tty cons
; /* standard console tty */
119 extern struct tty
*constty
; /* pointer to console "window" tty */
120 extern int __doprnt(const char *fmt
,
122 void (*putc
)(int, void *arg
),
127 * Record cpu that panic'd and lock around panic data
130 static void puts(const char *s
, int flags
, struct tty
*ttyp
);
131 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
);
133 /* MP printf stuff */
134 decl_simple_lock_data(,printf_lock
)
136 boolean_t new_printf_cpu_number
; /* do we need to output who we are */
139 extern void logwakeup();
140 extern void halt_cpu();
145 snprintf_func(int ch
, void *arg
);
147 struct putchar_args
{
151 static void putchar(int c
, void *arg
);
155 * Uprintf prints to the controlling terminal for the current process.
156 * It may block if the tty queue is overfull. No message is printed if
157 * the queue does not clear in a reasonable time.
160 uprintf(const char *fmt
, ...)
162 register struct proc
*p
= current_proc();
163 struct putchar_args pca
;
167 pca
.tty
= (struct tty
*)p
->p_session
->s_ttyp
;
169 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
171 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
178 register struct proc
*p
;
180 if (p
->p_flag
& P_CONTROLT
&& p
->p_session
->s_ttyvp
) {
181 SESSHOLD(p
->p_session
);
182 return ((tpr_t
) p
->p_session
);
184 return ((tpr_t
) NULL
);
192 SESSRELE((struct session
*) sess
);
196 * tprintf prints on the controlling terminal associated
197 * with the given session.
200 tprintf(tpr_t tpr
, const char *fmt
, ...)
202 register struct session
*sess
= (struct session
*)tpr
;
203 struct tty
*tp
= NULL
;
206 struct putchar_args pca
;
209 if (sess
&& sess
->s_ttyvp
&& ttycheckoutq(sess
->s_ttyp
, 0)) {
218 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
225 * Ttyprintf displays a message on a tty; it should be used only by
226 * the tty driver, or anything that knows the underlying tty will not
227 * be revoke(2)'d away. Other callers should use tprintf.
230 ttyprintf(struct tty
*tp
, const char *fmt
, ...)
235 struct putchar_args pca
;
240 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
252 struct putchar_args pca
;
257 printn((u_long
)level
, 10, TOLOG
, (struct tty
*)0, 0, 0);
262 addlog(const char *fmt
, ...)
264 register s
= splhigh();
266 struct putchar_args pca
;
272 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
277 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
282 void _printf(int flags
, struct tty
*ttyp
, const char *format
, ...)
285 struct putchar_args pca
;
290 va_start(ap
, format
);
291 __doprnt(format
, &ap
, putchar
, &pca
, 10);
295 int prf(const char *fmt
, va_list ap
, int flags
, struct tty
*ttyp
)
297 struct putchar_args pca
;
303 int cpun
= cpu_number();
306 simple_lock(&printf_lock
);
310 if (cpun
!= master_cpu
)
311 new_printf_cpu_number
= TRUE
;
313 if (new_printf_cpu_number
) {
314 putchar('{', flags
, ttyp
);
315 printn((u_long
)cpun
, 10, flags
, ttyp
, 0, 0);
316 putchar('}', flags
, ttyp
);
318 #endif /* NCPUS > 1 */
320 __doprnt(fmt
, &ap
, putchar
, &pca
, 10);
324 simple_unlock(&printf_lock
);
332 static void puts(const char *s
, int flags
, struct tty
*ttyp
)
335 struct putchar_args pca
;
345 * Printn prints a number n in base b.
346 * We don't use recursion to avoid deep kernel stacks.
348 static void printn(u_long n
, int b
, int flags
, struct tty
*ttyp
, int zf
, int fld_size
)
352 struct putchar_args pca
;
357 if (b
== 10 && (int)n
< 0) {
359 n
= (unsigned)(-(int)n
);
363 *cp
++ = "0123456789abcdef"[n%b
];
367 for (fld_size
-= cp
- prbuf
; fld_size
> 0; fld_size
--)
374 putchar(*--cp
, &pca
);
381 * Warn that a system table is full.
383 void tablefull(const char *tab
)
385 log(LOG_ERR
, "%s: table is full\n", tab
);
389 * Print a character on console or users terminal.
390 * If destination is console then the last MSGBUFS characters
391 * are saved in msgbuf for inspection later.
395 putchar(int c
, void *arg
)
397 struct putchar_args
*pca
= arg
;
398 register struct msgbuf
*mbp
;
399 char **sp
= (char**) pca
->tty
;
403 if ((pca
->flags
& TOCONS
) && pca
->tty
== NULL
&& constty
) {
407 if ((pca
->flags
& TOTTY
) && pca
->tty
&& tputchar(c
, pca
->tty
) < 0 &&
408 (pca
->flags
& TOCONS
) && pca
->tty
== constty
)
410 if ((pca
->flags
& TOLOG
) && c
!= '\0' && c
!= '\r' && c
!= 0177)
412 if ((pca
->flags
& TOCONS
) && constty
== 0 && c
!= '\0')
414 if (pca
->flags
& TOSTR
) {
423 * Scaled down version of vsprintf(3).
426 vsprintf(char *buf
, const char *cfmt
, va_list ap
)
429 struct snprintf_arg info
;
432 info
.remain
= 999999;
434 retval
= __doprnt(cfmt
, &ap
, snprintf_func
, &info
, 10);
435 if (info
.remain
>= 1) {
442 * Scaled down version of snprintf(3).
445 snprintf(char *str
, size_t size
, const char *format
, ...)
450 va_start(ap
, format
);
451 retval
= vsnprintf(str
, size
, format
, ap
);
457 * Scaled down version of vsnprintf(3).
460 vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
462 struct snprintf_arg info
;
467 retval
= __doprnt(format
, &ap
, snprintf_func
, &info
, 10);
468 if (info
.remain
>= 1)
474 snprintf_func(int ch
, void *arg
)
476 struct snprintf_arg
*const info
= arg
;
478 if (info
->remain
>= 2) {
485 kvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
487 __doprnt(fmt
, &ap
, func
, arg
, radix
);