]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_prf.c
ddf8e5db4aed91e230804f171bef57984f4d68ef
[apple/xnu.git] / bsd / kern / subr_prf.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*-
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.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
67 */
68 /* HISTORY
69 * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com)
70 * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
71 *
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
75 *
76 * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com
77 * Fixed tharshing format in many functions. Cleanup.
78 *
79 * 17-Jun-1995 Mac Gillon (mgillon) at NeXT
80 * Purged old history
81 * New version based on 4.4 and NS3.3
82 */
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/conf.h>
87 #include <sys/reboot.h>
88 #include <sys/msgbuf.h>
89 #include <sys/proc_internal.h>
90 #include <sys/ioctl.h>
91 #include <sys/tty.h>
92 #include <sys/file_internal.h>
93 #include <sys/tprintf.h>
94 #include <sys/syslog.h>
95 #include <stdarg.h>
96 #include <sys/malloc.h>
97 #include <sys/lock.h>
98 #include <sys/subr_prf.h>
99
100 #include <kern/cpu_number.h> /* for cpu_number() */
101 #include <libkern/libkern.h>
102 #include <os/log_private.h>
103
104 /* for vaddlog(): the following are implemented in osfmk/kern/printf.c */
105 extern void bsd_log_lock(void);
106 extern void bsd_log_unlock(void);
107
108 /* Keep this around only because it's exported */
109 void _printf(int, struct tty *, const char *, ...);
110
111 struct snprintf_arg {
112 char *str;
113 size_t remain;
114 };
115
116
117 /*
118 * In case console is off,
119 * debugger_panic_str contains argument to last
120 * call to panic.
121 */
122 extern const char *debugger_panic_str;
123
124 extern void cnputc(char); /* standard console putc */
125 void (*v_putc)(char) = cnputc; /* routine to putc on virtual console */
126
127 extern struct tty cons; /* standard console tty */
128 extern struct tty *constty; /* pointer to console "window" tty */
129 extern int __doprnt(const char *fmt,
130 va_list argp,
131 void (*)(int, void *),
132 void *arg,
133 int radix,
134 int is_log);
135
136 /*
137 * Record cpu that panic'd and lock around panic data
138 */
139
140 extern void logwakeup(struct msgbuf *);
141 extern void halt_cpu(void);
142
143 static void
144 snprintf_func(int ch, void *arg);
145
146 struct putchar_args {
147 int flags;
148 struct tty *tty;
149 };
150 static void putchar(int c, void *arg);
151
152
153 /*
154 * Uprintf prints to the controlling terminal for the current process.
155 * It may block if the tty queue is overfull. No message is printed if
156 * the queue does not clear in a reasonable time.
157 */
158 void
159 uprintf(const char *fmt, ...)
160 {
161 struct proc *p = current_proc();
162 struct putchar_args pca;
163 va_list ap;
164 struct session *sessp;
165
166 sessp = proc_session(p);
167
168 if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
169 pca.flags = TOTTY;
170 pca.tty = SESSION_TP(sessp);
171 if (pca.tty != NULL) {
172 tty_lock(pca.tty);
173 }
174 va_start(ap, fmt);
175 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
176 va_end(ap);
177 if (pca.tty != NULL) {
178 tty_unlock(pca.tty);
179 }
180 }
181 if (sessp != SESSION_NULL) {
182 session_rele(sessp);
183 }
184 }
185
186 tpr_t
187 tprintf_open(struct proc *p)
188 {
189 struct session * sessp;
190
191 sessp = proc_session(p);
192
193 if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
194 return (tpr_t)sessp;
195 }
196 if (sessp != SESSION_NULL) {
197 session_rele(sessp);
198 }
199
200 return (tpr_t) NULL;
201 }
202
203 void
204 tprintf_close(tpr_t sessp)
205 {
206 if (sessp) {
207 session_rele((struct session *) sessp);
208 }
209 }
210
211 /*
212 * tprintf prints on the controlling terminal associated
213 * with the given session.
214 *
215 * NOTE: No one else should call this function!!!
216 */
217 void
218 tprintf(tpr_t tpr, const char *fmt, ...)
219 {
220 struct session *sess = (struct session *)tpr;
221 struct tty *tp;
222 va_list ap;
223 struct putchar_args pca;
224
225 if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
226 /* ttycheckoutq(), tputchar() require a locked tp */
227 tty_lock(tp);
228 if (ttycheckoutq(tp, 0)) {
229 pca.flags = TOTTY;
230 /* going to the tty; leave locked */
231 pca.tty = tp;
232 va_start(ap, fmt);
233 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
234 va_end(ap);
235 }
236 tty_unlock(tp);
237 }
238
239 pca.flags = TOLOG;
240 pca.tty = TTY_NULL;
241 va_start(ap, fmt);
242 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
243 va_end(ap);
244
245 logwakeup(msgbufp);
246
247 va_start(ap, fmt);
248 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
249 va_end(ap);
250 }
251
252 /*
253 * Ttyprintf displays a message on a tty; it should be used only by
254 * the tty driver, or anything that knows the underlying tty will not
255 * be revoke(2)'d away. Other callers should use tprintf.
256 *
257 * Locks: It is assumed that the tty_lock() is held over the call
258 * to this function. Ensuring this is the responsibility
259 * of the caller.
260 */
261 void
262 ttyprintf(struct tty *tp, const char *fmt, ...)
263 {
264 va_list ap;
265
266 if (tp != NULL) {
267 struct putchar_args pca;
268 pca.flags = TOTTY;
269 pca.tty = tp;
270
271 va_start(ap, fmt);
272 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
273 va_end(ap);
274 }
275 }
276
277 void
278 logtime(time_t secs)
279 {
280 printf("Time %ld Message ", secs);
281 }
282
283 static void
284 putchar_asl(int c, void *arg)
285 {
286 struct putchar_args *pca = arg;
287
288 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
289 log_putc_locked(aslbufp, c);
290 }
291 putchar(c, arg);
292 }
293
294 /*
295 * Vestigial support for kern_asl_msg() via /dev/klog
296 */
297 int
298 vaddlog(const char *fmt, va_list ap)
299 {
300 struct putchar_args pca = {
301 .flags = TOLOGLOCKED,
302 .tty = NULL,
303 };
304
305 bsd_log_lock();
306 __doprnt(fmt, ap, putchar_asl, &pca, 10, TRUE);
307 bsd_log_unlock();
308 logwakeup(NULL);
309
310 return 0;
311 }
312
313 void
314 _printf(int flags, struct tty *ttyp, const char *format, ...)
315 {
316 va_list ap;
317 struct putchar_args pca;
318
319 pca.flags = flags;
320 pca.tty = ttyp;
321
322 if (ttyp != NULL) {
323 tty_lock(ttyp);
324
325 va_start(ap, format);
326 __doprnt(format, ap, putchar, &pca, 10, TRUE);
327 va_end(ap);
328
329 tty_unlock(ttyp);
330 }
331 }
332
333 int
334 prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
335 {
336 struct putchar_args pca;
337
338 pca.flags = flags;
339 pca.tty = ttyp;
340
341 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
342
343 return 0;
344 }
345
346 /*
347 * Warn that a system table is full.
348 */
349 void
350 tablefull(const char *tab)
351 {
352 log(LOG_ERR, "%s: table is full\n", tab);
353 }
354
355 /*
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.
359 *
360 * Locks: If TOTTY is set, we assume that the tty lock is held
361 * over the call to this function.
362 */
363 /*ARGSUSED*/
364 void
365 putchar(int c, void *arg)
366 {
367 struct putchar_args *pca = arg;
368 char **sp = (char**) pca->tty;
369
370 if (debugger_panic_str) {
371 constty = 0;
372 }
373 if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
374 pca->tty = constty;
375 pca->flags |= TOTTY;
376 }
377 if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
378 (pca->flags & TOCONS) && pca->tty == constty) {
379 constty = 0;
380 }
381 if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) {
382 log_putc(c);
383 }
384 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
385 log_putc_locked(msgbufp, c);
386 }
387 if ((pca->flags & TOCONS) && constty == 0 && c != '\0') {
388 (*v_putc)(c);
389 }
390 if (pca->flags & TOSTR) {
391 **sp = c;
392 (*sp)++;
393 }
394 }
395
396 int
397 vprintf_log_locked(const char *fmt, va_list ap, bool addcr)
398 {
399 struct putchar_args pca;
400
401 pca.flags = TOLOGLOCKED;
402 pca.tty = NULL;
403 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
404 if (addcr) {
405 putchar('\n', &pca);
406 }
407 return 0;
408 }
409
410 #if !CONFIG_EMBEDDED
411
412 /*
413 * Scaled down version of vsprintf(3).
414 *
415 * Deprecation Warning:
416 * vsprintf() is being deprecated. Please use vsnprintf() instead.
417 */
418 int
419 vsprintf(char *buf, const char *cfmt, va_list ap)
420 {
421 int retval;
422 struct snprintf_arg info;
423
424 info.str = buf;
425 info.remain = 999999;
426
427 retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
428 if (info.remain >= 1) {
429 *info.str++ = '\0';
430 }
431 return 0;
432 }
433 #endif /* !CONFIG_EMBEDDED */
434
435 /*
436 * Scaled down version of snprintf(3).
437 */
438 int
439 snprintf(char *str, size_t size, const char *format, ...)
440 {
441 int retval;
442 va_list ap;
443
444 va_start(ap, format);
445 retval = vsnprintf(str, size, format, ap);
446 va_end(ap);
447 return retval;
448 }
449
450 /*
451 * Scaled down version of vsnprintf(3).
452 */
453 int
454 vsnprintf(char *str, size_t size, const char *format, va_list ap)
455 {
456 struct snprintf_arg info;
457 int retval;
458
459 info.str = str;
460 info.remain = size;
461 retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
462 if (info.remain >= 1) {
463 *info.str++ = '\0';
464 }
465 return retval;
466 }
467
468 int
469 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
470 {
471 ssize_t ssize = size;
472 int i;
473
474 i = vsnprintf(buf, size, fmt, args);
475
476 return (i >= ssize) ? (ssize - 1) : i;
477 }
478
479 int
480 scnprintf(char *buf, size_t size, const char *fmt, ...)
481 {
482 va_list args;
483 int i;
484
485 va_start(args, fmt);
486 i = vscnprintf(buf, size, fmt, args);
487 va_end(args);
488
489 return i;
490 }
491
492 static void
493 snprintf_func(int ch, void *arg)
494 {
495 struct snprintf_arg *const info = arg;
496
497 if (info->remain >= 2) {
498 *info->str++ = ch;
499 info->remain--;
500 }
501 }
502
503 int
504 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
505 {
506 __doprnt(fmt, ap, func, arg, radix, TRUE);
507 return 0;
508 }