]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_prf.c
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1986, 1988, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
69 * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com)
70 * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
72 * 17-July-97 Umesh Vaishampayan (umeshv@apple.com)
73 * Eliminated multiple definition of constty which is defined
74 * in bsd/dev/XXX/cons.c
76 * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com
77 * Fixed tharshing format in many functions. Cleanup.
79 * 17-Jun-1995 Mac Gillon (mgillon) at NeXT
81 * New version based on 4.4 and NS3.3
84 #include <sys/param.h>
85 #include <sys/systm.h>
87 #include <sys/reboot.h>
88 #include <sys/msgbuf.h>
89 #include <sys/proc_internal.h>
90 #include <sys/ioctl.h>
92 #include <sys/file_internal.h>
93 #include <sys/tprintf.h>
94 #include <sys/syslog.h>
96 #include <sys/malloc.h>
98 #include <sys/subr_prf.h>
100 #include <kern/cpu_number.h> /* for cpu_number() */
101 #include <libkern/libkern.h>
102 #include <os/log_private.h>
104 /* for vaddlog(): the following are implemented in osfmk/kern/printf.c */
105 extern bool bsd_log_lock(bool);
106 extern void bsd_log_unlock(void);
108 uint32_t vaddlog_msgcount
= 0;
109 uint32_t vaddlog_msgcount_dropped
= 0;
111 /* Keep this around only because it's exported */
112 void _printf(int, struct tty
*, const char *, ...);
114 struct snprintf_arg
{
121 * In case console is off,
122 * debugger_panic_str contains argument to last
125 extern const char *debugger_panic_str
;
127 extern void cnputc(char); /* standard console putc */
129 extern struct tty cons
; /* standard console tty */
130 extern struct tty
*constty
; /* pointer to console "window" tty */
131 extern int __doprnt(const char *fmt
,
133 void (*)(int, void *),
139 * Record cpu that panic'd and lock around panic data
142 extern void logwakeup(struct msgbuf
*);
143 extern void halt_cpu(void);
146 snprintf_func(int ch
, void *arg
);
148 struct putchar_args
{
151 bool last_char_was_cr
;
153 static void putchar(int c
, void *arg
);
157 * Uprintf prints to the controlling terminal for the current process.
158 * It may block if the tty queue is overfull. No message is printed if
159 * the queue does not clear in a reasonable time.
162 uprintf(const char *fmt
, ...)
164 struct proc
*p
= current_proc();
165 struct putchar_args pca
;
167 struct session
*sessp
;
169 sessp
= proc_session(p
);
171 if (p
->p_flag
& P_CONTROLT
&& sessp
!= SESSION_NULL
&& sessp
->s_ttyvp
) {
173 pca
.tty
= SESSION_TP(sessp
);
174 if (pca
.tty
!= NULL
) {
178 __doprnt(fmt
, ap
, putchar
, &pca
, 10, FALSE
);
180 if (pca
.tty
!= NULL
) {
184 if (sessp
!= SESSION_NULL
) {
190 tprintf_open(struct proc
*p
)
192 struct session
* sessp
;
194 sessp
= proc_session(p
);
196 if (p
->p_flag
& P_CONTROLT
&& sessp
->s_ttyvp
) {
199 if (sessp
!= SESSION_NULL
) {
207 tprintf_close(tpr_t sessp
)
210 session_rele((struct session
*) sessp
);
215 * tprintf prints on the controlling terminal associated
216 * with the given session.
218 * NOTE: No one else should call this function!!!
221 tprintf(tpr_t tpr
, const char *fmt
, ...)
223 struct session
*sess
= (struct session
*)tpr
;
226 struct putchar_args pca
;
228 if (sess
&& (tp
= SESSION_TP(sess
)) != TTY_NULL
) {
229 /* ttycheckoutq(), tputchar() require a locked tp */
231 if (ttycheckoutq(tp
, 0)) {
233 /* going to the tty; leave locked */
236 __doprnt(fmt
, ap
, putchar
, &pca
, 10, FALSE
);
243 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, fmt
, ap
, __builtin_return_address(0));
248 * Ttyprintf displays a message on a tty; it should be used only by
249 * the tty driver, or anything that knows the underlying tty will not
250 * be revoke(2)'d away. Other callers should use tprintf.
252 * Locks: It is assumed that the tty_lock() is held over the call
253 * to this function. Ensuring this is the responsibility
257 ttyprintf(struct tty
*tp
, const char *fmt
, ...)
262 struct putchar_args pca
;
267 __doprnt(fmt
, ap
, putchar
, &pca
, 10, TRUE
);
275 printf("Time 0x%lx Message ", secs
);
279 putchar_asl(int c
, void *arg
)
281 struct putchar_args
*pca
= arg
;
283 if ((pca
->flags
& TOLOGLOCKED
) && c
!= '\0' && c
!= '\r' && c
!= 0177) {
284 log_putc_locked(aslbufp
, (char)c
);
290 * Vestigial support for kern_asl_msg() via /dev/klog
293 vaddlog(const char *fmt
, va_list ap
)
295 if (!bsd_log_lock(oslog_is_safe())) {
296 os_atomic_inc(&vaddlog_msgcount_dropped
, relaxed
);
300 struct putchar_args pca
= {
301 .flags
= TOLOGLOCKED
,
305 __doprnt(fmt
, ap
, putchar_asl
, &pca
, 10, TRUE
);
309 os_atomic_inc(&vaddlog_msgcount
, relaxed
);
314 _printf(int flags
, struct tty
*ttyp
, const char *format
, ...)
317 struct putchar_args pca
;
325 va_start(ap
, format
);
326 __doprnt(format
, ap
, putchar
, &pca
, 10, TRUE
);
334 prf(const char *fmt
, va_list ap
, int flags
, struct tty
*ttyp
)
336 struct putchar_args pca
;
341 __doprnt(fmt
, ap
, putchar
, &pca
, 10, TRUE
);
347 * Warn that a system table is full.
350 tablefull(const char *tab
)
352 log(LOG_ERR
, "%s: table is full\n", tab
);
356 * Print a character on console or users terminal.
357 * If destination is console then the last MSGBUFS characters
358 * are saved in msgbuf for inspection later.
360 * Locks: If TOTTY is set, we assume that the tty lock is held
361 * over the call to this function.
365 putchar(int c
, void *arg
)
367 struct putchar_args
*pca
= arg
;
368 char **sp
= (char**) pca
->tty
;
370 if (debugger_panic_str
) {
373 if ((pca
->flags
& TOCONS
) && pca
->tty
== NULL
&& constty
) {
377 if ((pca
->flags
& TOTTY
) && pca
->tty
&& tputchar(c
, pca
->tty
) < 0 &&
378 (pca
->flags
& TOCONS
) && pca
->tty
== constty
) {
381 if ((pca
->flags
& TOLOG
) && c
!= '\0' && c
!= '\r' && c
!= 0177) {
384 if ((pca
->flags
& TOLOGLOCKED
) && c
!= '\0' && c
!= '\r' && c
!= 0177) {
385 log_putc_locked(msgbufp
, (char)c
);
387 if ((pca
->flags
& TOCONS
) && constty
== 0 && c
!= '\0') {
390 if (pca
->flags
& TOSTR
) {
395 pca
->last_char_was_cr
= ('\n' == c
);
399 printf_log_locked(bool addcr
, const char *fmt
, ...)
405 retval
= vprintf_log_locked(fmt
, args
, addcr
);
412 vprintf_log_locked(const char *fmt
, va_list ap
, bool addcr
)
414 struct putchar_args pca
;
416 pca
.flags
= TOLOGLOCKED
;
418 pca
.last_char_was_cr
= false;
419 __doprnt(fmt
, ap
, putchar
, &pca
, 10, TRUE
);
423 return pca
.last_char_was_cr
;
428 * Scaled down version of vsprintf(3).
430 * Deprecation Warning:
431 * vsprintf() is being deprecated. Please use vsnprintf() instead.
434 vsprintf(char *buf
, const char *cfmt
, va_list ap
)
437 struct snprintf_arg info
;
440 info
.remain
= 999999;
442 retval
= __doprnt(cfmt
, ap
, snprintf_func
, &info
, 10, FALSE
);
443 if (info
.remain
>= 1) {
448 #endif /* CONFIG_VSPRINTF */
451 * Scaled down version of snprintf(3).
454 snprintf(char *str
, size_t size
, const char *format
, ...)
459 va_start(ap
, format
);
460 retval
= vsnprintf(str
, size
, format
, ap
);
466 * Scaled down version of vsnprintf(3).
469 vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
471 struct snprintf_arg info
;
476 retval
= __doprnt(format
, ap
, snprintf_func
, &info
, 10, FALSE
);
477 if (info
.remain
>= 1) {
484 vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
486 ssize_t ssize
= size
;
489 i
= vsnprintf(buf
, size
, fmt
, args
);
491 return (i
>= ssize
) ? (int)(ssize
- 1) : i
;
495 scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
501 i
= vscnprintf(buf
, size
, fmt
, args
);
508 snprintf_func(int ch
, void *arg
)
510 struct snprintf_arg
*const info
= arg
;
512 if (info
->remain
>= 2) {
513 *info
->str
++ = (char)ch
;
519 kvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
521 __doprnt(fmt
, ap
, func
, arg
, radix
, TRUE
);