]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_prf.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / subr_prf.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
0a7de745
A
77 * Fixed tharshing format in many functions. Cleanup.
78 *
1c79356b
A
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>
1c79356b
A
86#include <sys/conf.h>
87#include <sys/reboot.h>
88#include <sys/msgbuf.h>
91447636 89#include <sys/proc_internal.h>
1c79356b
A
90#include <sys/ioctl.h>
91#include <sys/tty.h>
91447636 92#include <sys/file_internal.h>
1c79356b
A
93#include <sys/tprintf.h>
94#include <sys/syslog.h>
95#include <stdarg.h>
96#include <sys/malloc.h>
97#include <sys/lock.h>
1c79356b
A
98#include <sys/subr_prf.h>
99
0a7de745 100#include <kern/cpu_number.h> /* for cpu_number() */
1c79356b 101#include <libkern/libkern.h>
39037602 102#include <os/log_private.h>
1c79356b 103
2d21ac55 104/* for vaddlog(): the following are implemented in osfmk/kern/printf.c */
f427ee49 105extern bool bsd_log_lock(bool);
2d21ac55
A
106extern void bsd_log_unlock(void);
107
f427ee49
A
108uint32_t vaddlog_msgcount = 0;
109uint32_t vaddlog_msgcount_dropped = 0;
110
2d21ac55
A
111/* Keep this around only because it's exported */
112void _printf(int, struct tty *, const char *, ...);
113
1c79356b
A
114struct snprintf_arg {
115 char *str;
116 size_t remain;
117};
118
119
120/*
121 * In case console is off,
5ba3f43e 122 * debugger_panic_str contains argument to last
1c79356b
A
123 * call to panic.
124 */
0a7de745 125extern const char *debugger_panic_str;
1c79356b 126
0a7de745 127extern void cnputc(char); /* standard console putc */
1c79356b 128
0a7de745
A
129extern struct tty cons; /* standard console tty */
130extern struct tty *constty; /* pointer to console "window" tty */
9bccf70c 131extern int __doprnt(const char *fmt,
0a7de745
A
132 va_list argp,
133 void (*)(int, void *),
134 void *arg,
135 int radix,
136 int is_log);
1c79356b
A
137
138/*
139 * Record cpu that panic'd and lock around panic data
140 */
1c79356b 141
0a7de745
A
142extern void logwakeup(struct msgbuf *);
143extern void halt_cpu(void);
1c79356b
A
144
145static void
146snprintf_func(int ch, void *arg);
147
9bccf70c
A
148struct putchar_args {
149 int flags;
150 struct tty *tty;
f427ee49 151 bool last_char_was_cr;
9bccf70c
A
152};
153static void putchar(int c, void *arg);
1c79356b
A
154
155
156/*
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.
160 */
161void
162uprintf(const char *fmt, ...)
163{
2d21ac55 164 struct proc *p = current_proc();
9bccf70c 165 struct putchar_args pca;
1c79356b 166 va_list ap;
b0d623f7 167 struct session *sessp;
0a7de745 168
2d21ac55
A
169 sessp = proc_session(p);
170
b0d623f7
A
171 if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
172 pca.flags = TOTTY;
173 pca.tty = SESSION_TP(sessp);
0a7de745 174 if (pca.tty != NULL) {
b0d623f7 175 tty_lock(pca.tty);
0a7de745 176 }
1c79356b 177 va_start(ap, fmt);
3e170ce0 178 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
1c79356b 179 va_end(ap);
0a7de745
A
180 if (pca.tty != NULL) {
181 tty_unlock(pca.tty);
182 }
1c79356b 183 }
0a7de745 184 if (sessp != SESSION_NULL) {
b0d623f7 185 session_rele(sessp);
0a7de745 186 }
1c79356b
A
187}
188
189tpr_t
2d21ac55 190tprintf_open(struct proc *p)
1c79356b 191{
2d21ac55
A
192 struct session * sessp;
193
194 sessp = proc_session(p);
195
196 if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
0a7de745 197 return (tpr_t)sessp;
1c79356b 198 }
0a7de745 199 if (sessp != SESSION_NULL) {
2d21ac55 200 session_rele(sessp);
0a7de745 201 }
2d21ac55 202
0a7de745 203 return (tpr_t) NULL;
1c79356b
A
204}
205
206void
2d21ac55 207tprintf_close(tpr_t sessp)
1c79356b 208{
0a7de745 209 if (sessp) {
2d21ac55 210 session_rele((struct session *) sessp);
0a7de745 211 }
1c79356b
A
212}
213
214/*
215 * tprintf prints on the controlling terminal associated
216 * with the given session.
b0d623f7
A
217 *
218 * NOTE: No one else should call this function!!!
1c79356b
A
219 */
220void
221tprintf(tpr_t tpr, const char *fmt, ...)
222{
2d21ac55 223 struct session *sess = (struct session *)tpr;
39037602 224 struct tty *tp;
1c79356b 225 va_list ap;
9bccf70c 226 struct putchar_args pca;
1c79356b 227
b0d623f7
A
228 if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
229 /* ttycheckoutq(), tputchar() require a locked tp */
230 tty_lock(tp);
0a7de745 231 if (ttycheckoutq(tp, 0)) {
39037602 232 pca.flags = TOTTY;
b0d623f7 233 /* going to the tty; leave locked */
39037602
A
234 pca.tty = tp;
235 va_start(ap, fmt);
236 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
237 va_end(ap);
b0d623f7 238 }
39037602 239 tty_unlock(tp);
1c79356b 240 }
39037602 241
39037602
A
242 va_start(ap, fmt);
243 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
244 va_end(ap);
1c79356b
A
245}
246
247/*
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.
b0d623f7
A
251 *
252 * Locks: It is assumed that the tty_lock() is held over the call
253 * to this function. Ensuring this is the responsibility
254 * of the caller.
1c79356b
A
255 */
256void
257ttyprintf(struct tty *tp, const char *fmt, ...)
258{
259 va_list ap;
260
261 if (tp != NULL) {
9bccf70c
A
262 struct putchar_args pca;
263 pca.flags = TOTTY;
264 pca.tty = tp;
0a7de745 265
1c79356b 266 va_start(ap, fmt);
3e170ce0 267 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
1c79356b
A
268 va_end(ap);
269 }
270}
271
1c79356b 272void
cc8bc92a 273logtime(time_t secs)
1c79356b 274{
f427ee49 275 printf("Time 0x%lx Message ", secs);
1c79356b
A
276}
277
2d21ac55 278static void
cc8bc92a 279putchar_asl(int c, void *arg)
1c79356b 280{
cc8bc92a 281 struct putchar_args *pca = arg;
2d21ac55 282
0a7de745 283 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
f427ee49 284 log_putc_locked(aslbufp, (char)c);
0a7de745 285 }
cc8bc92a 286 putchar(c, arg);
2d21ac55
A
287}
288
cc8bc92a
A
289/*
290 * Vestigial support for kern_asl_msg() via /dev/klog
291 */
2d21ac55
A
292int
293vaddlog(const char *fmt, va_list ap)
294{
f427ee49
A
295 if (!bsd_log_lock(oslog_is_safe())) {
296 os_atomic_inc(&vaddlog_msgcount_dropped, relaxed);
297 return 1;
298 }
299
cc8bc92a
A
300 struct putchar_args pca = {
301 .flags = TOLOGLOCKED,
302 .tty = NULL,
303 };
2d21ac55 304
cc8bc92a 305 __doprnt(fmt, ap, putchar_asl, &pca, 10, TRUE);
2d21ac55 306 bsd_log_unlock();
cc8bc92a
A
307 logwakeup(NULL);
308
f427ee49 309 os_atomic_inc(&vaddlog_msgcount, relaxed);
0a7de745 310 return 0;
1c79356b 311}
2d21ac55
A
312
313void
314_printf(int flags, struct tty *ttyp, const char *format, ...)
1c79356b
A
315{
316 va_list ap;
9bccf70c
A
317 struct putchar_args pca;
318
319 pca.flags = flags;
320 pca.tty = ttyp;
b0d623f7
A
321
322 if (ttyp != NULL) {
323 tty_lock(ttyp);
0a7de745 324
b0d623f7 325 va_start(ap, format);
3e170ce0 326 __doprnt(format, ap, putchar, &pca, 10, TRUE);
b0d623f7
A
327 va_end(ap);
328
329 tty_unlock(ttyp);
330 }
1c79356b
A
331}
332
b0d623f7
A
333int
334prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
1c79356b 335{
9bccf70c 336 struct putchar_args pca;
1c79356b 337
9bccf70c
A
338 pca.flags = flags;
339 pca.tty = ttyp;
340
3e170ce0 341 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
9bccf70c 342
9bccf70c 343 return 0;
1c79356b
A
344}
345
1c79356b
A
346/*
347 * Warn that a system table is full.
348 */
0a7de745
A
349void
350tablefull(const char *tab)
1c79356b
A
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.
b0d623f7
A
359 *
360 * Locks: If TOTTY is set, we assume that the tty lock is held
361 * over the call to this function.
1c79356b
A
362 */
363/*ARGSUSED*/
9bccf70c
A
364void
365putchar(int c, void *arg)
1c79356b 366{
9bccf70c 367 struct putchar_args *pca = arg;
9bccf70c 368 char **sp = (char**) pca->tty;
1c79356b 369
0a7de745 370 if (debugger_panic_str) {
1c79356b 371 constty = 0;
0a7de745 372 }
9bccf70c
A
373 if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
374 pca->tty = constty;
375 pca->flags |= TOTTY;
1c79356b 376 }
9bccf70c 377 if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
0a7de745 378 (pca->flags & TOCONS) && pca->tty == constty) {
1c79356b 379 constty = 0;
0a7de745
A
380 }
381 if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) {
f427ee49 382 log_putc((char)c);
0a7de745
A
383 }
384 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
f427ee49 385 log_putc_locked(msgbufp, (char)c);
0a7de745
A
386 }
387 if ((pca->flags & TOCONS) && constty == 0 && c != '\0') {
f427ee49 388 cnputc((char)c);
0a7de745 389 }
9bccf70c 390 if (pca->flags & TOSTR) {
f427ee49 391 **sp = (char)c;
1c79356b
A
392 (*sp)++;
393 }
f427ee49
A
394
395 pca->last_char_was_cr = ('\n' == c);
1c79356b
A
396}
397
f427ee49
A
398bool
399printf_log_locked(bool addcr, const char *fmt, ...)
400{
401 bool retval;
402 va_list args;
403
404 va_start(args, fmt);
405 retval = vprintf_log_locked(fmt, args, addcr);
406 va_end(args);
407
408 return retval;
409}
410
411bool
cb323159 412vprintf_log_locked(const char *fmt, va_list ap, bool addcr)
2d21ac55
A
413{
414 struct putchar_args pca;
1c79356b 415
39037602 416 pca.flags = TOLOGLOCKED;
2d21ac55 417 pca.tty = NULL;
f427ee49 418 pca.last_char_was_cr = false;
3e170ce0 419 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
cb323159
A
420 if (addcr) {
421 putchar('\n', &pca);
422 }
f427ee49 423 return pca.last_char_was_cr;
2d21ac55 424}
1c79356b 425
f427ee49 426#if CONFIG_VSPRINTF
1c79356b
A
427/*
428 * Scaled down version of vsprintf(3).
2d21ac55
A
429 *
430 * Deprecation Warning:
0a7de745 431 * vsprintf() is being deprecated. Please use vsnprintf() instead.
1c79356b
A
432 */
433int
434vsprintf(char *buf, const char *cfmt, va_list ap)
435{
436 int retval;
9bccf70c 437 struct snprintf_arg info;
1c79356b 438
9bccf70c
A
439 info.str = buf;
440 info.remain = 999999;
441
3e170ce0 442 retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
9bccf70c
A
443 if (info.remain >= 1) {
444 *info.str++ = '\0';
445 }
446 return 0;
1c79356b 447}
f427ee49 448#endif /* CONFIG_VSPRINTF */
1c79356b
A
449
450/*
451 * Scaled down version of snprintf(3).
452 */
453int
454snprintf(char *str, size_t size, const char *format, ...)
455{
456 int retval;
457 va_list ap;
458
459 va_start(ap, format);
460 retval = vsnprintf(str, size, format, ap);
461 va_end(ap);
0a7de745 462 return retval;
1c79356b
A
463}
464
465/*
466 * Scaled down version of vsnprintf(3).
467 */
468int
469vsnprintf(char *str, size_t size, const char *format, va_list ap)
470{
471 struct snprintf_arg info;
472 int retval;
473
474 info.str = str;
475 info.remain = size;
3e170ce0 476 retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
0a7de745 477 if (info.remain >= 1) {
1c79356b 478 *info.str++ = '\0';
0a7de745 479 }
1c79356b
A
480 return retval;
481}
482
4ba76501
A
483int
484vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
485{
486 ssize_t ssize = size;
487 int i;
488
489 i = vsnprintf(buf, size, fmt, args);
490
f427ee49 491 return (i >= ssize) ? (int)(ssize - 1) : i;
4ba76501
A
492}
493
494int
495scnprintf(char *buf, size_t size, const char *fmt, ...)
496{
497 va_list args;
498 int i;
499
500 va_start(args, fmt);
501 i = vscnprintf(buf, size, fmt, args);
502 va_end(args);
503
504 return i;
505}
506
1c79356b
A
507static void
508snprintf_func(int ch, void *arg)
509{
510 struct snprintf_arg *const info = arg;
511
512 if (info->remain >= 2) {
f427ee49 513 *info->str++ = (char)ch;
1c79356b
A
514 info->remain--;
515 }
516}
517
1c79356b
A
518int
519kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
520{
3e170ce0 521 __doprnt(fmt, ap, func, arg, radix, TRUE);
9bccf70c 522 return 0;
1c79356b 523}