]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_prf.c
xnu-3789.51.2.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@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 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
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>
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
100#include <kern/cpu_number.h> /* for cpu_number() */
101#include <machine/spl.h>
102#include <libkern/libkern.h>
39037602 103#include <os/log_private.h>
1c79356b 104
2d21ac55
A
105/* for vaddlog(): the following are implemented in osfmk/kern/printf.c */
106extern void bsd_log_lock(void);
107extern void bsd_log_unlock(void);
108
109/* Keep this around only because it's exported */
110void _printf(int, struct tty *, const char *, ...);
111
1c79356b
A
112struct snprintf_arg {
113 char *str;
114 size_t remain;
115};
116
117
118/*
119 * In case console is off,
120 * panicstr contains argument to last
121 * call to panic.
122 */
123extern const char *panicstr;
124
2d21ac55
A
125extern void cnputc(char); /* standard console putc */
126void (*v_putc)(char) = cnputc; /* routine to putc on virtual console */
1c79356b
A
127
128extern struct tty cons; /* standard console tty */
129extern struct tty *constty; /* pointer to console "window" tty */
9bccf70c 130extern int __doprnt(const char *fmt,
b0d623f7 131 va_list argp,
2d21ac55 132 void (*)(int, void *),
9bccf70c 133 void *arg,
3e170ce0
A
134 int radix,
135 int is_log);
1c79356b
A
136
137/*
138 * Record cpu that panic'd and lock around panic data
139 */
b0d623f7 140static void printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size);
1c79356b 141
2d21ac55
A
142extern void logwakeup(void);
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;
151};
152static void putchar(int c, void *arg);
1c79356b
A
153
154
155/*
156 * Uprintf prints to the controlling terminal for the current process.
157 * It may block if the tty queue is overfull. No message is printed if
158 * the queue does not clear in a reasonable time.
159 */
160void
161uprintf(const char *fmt, ...)
162{
2d21ac55 163 struct proc *p = current_proc();
9bccf70c 164 struct putchar_args pca;
1c79356b 165 va_list ap;
b0d623f7 166 struct session *sessp;
9bccf70c 167
2d21ac55
A
168 sessp = proc_session(p);
169
b0d623f7
A
170 if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
171 pca.flags = TOTTY;
172 pca.tty = SESSION_TP(sessp);
173 if (pca.tty != NULL)
174 tty_lock(pca.tty);
1c79356b 175 va_start(ap, fmt);
3e170ce0 176 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
1c79356b 177 va_end(ap);
b0d623f7
A
178 if (pca.tty != NULL)
179 tty_unlock(pca.tty);
1c79356b 180 }
b0d623f7
A
181 if (sessp != SESSION_NULL)
182 session_rele(sessp);
1c79356b
A
183}
184
185tpr_t
2d21ac55 186tprintf_open(struct proc *p)
1c79356b 187{
2d21ac55
A
188 struct session * sessp;
189
190 sessp = proc_session(p);
191
192 if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
193 return ((tpr_t)sessp);
1c79356b 194 }
2d21ac55
A
195 if (sessp != SESSION_NULL)
196 session_rele(sessp);
197
1c79356b
A
198 return ((tpr_t) NULL);
199}
200
201void
2d21ac55 202tprintf_close(tpr_t sessp)
1c79356b 203{
2d21ac55
A
204 if (sessp)
205 session_rele((struct session *) sessp);
1c79356b
A
206}
207
208/*
209 * tprintf prints on the controlling terminal associated
210 * with the given session.
b0d623f7
A
211 *
212 * NOTE: No one else should call this function!!!
1c79356b
A
213 */
214void
215tprintf(tpr_t tpr, const char *fmt, ...)
216{
2d21ac55 217 struct session *sess = (struct session *)tpr;
39037602 218 struct tty *tp;
1c79356b 219 va_list ap;
9bccf70c 220 struct putchar_args pca;
1c79356b
A
221
222 logpri(LOG_INFO);
2d21ac55 223
b0d623f7
A
224 if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
225 /* ttycheckoutq(), tputchar() require a locked tp */
226 tty_lock(tp);
227 if(ttycheckoutq(tp, 0)) {
39037602 228 pca.flags = TOTTY;
b0d623f7 229 /* going to the tty; leave locked */
39037602
A
230 pca.tty = tp;
231 va_start(ap, fmt);
232 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
233 va_end(ap);
b0d623f7 234 }
39037602 235 tty_unlock(tp);
1c79356b 236 }
39037602
A
237
238 pca.flags = TOLOG;
239 pca.tty = TTY_NULL;
55e303ae 240 va_start(ap, fmt);
39037602 241 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
55e303ae
A
242 va_end(ap);
243
1c79356b 244 logwakeup();
39037602
A
245
246 va_start(ap, fmt);
247 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
248 va_end(ap);
1c79356b
A
249}
250
251/*
252 * Ttyprintf displays a message on a tty; it should be used only by
253 * the tty driver, or anything that knows the underlying tty will not
254 * be revoke(2)'d away. Other callers should use tprintf.
b0d623f7
A
255 *
256 * Locks: It is assumed that the tty_lock() is held over the call
257 * to this function. Ensuring this is the responsibility
258 * of the caller.
1c79356b
A
259 */
260void
261ttyprintf(struct tty *tp, const char *fmt, ...)
262{
263 va_list ap;
264
265 if (tp != NULL) {
9bccf70c
A
266 struct putchar_args pca;
267 pca.flags = TOTTY;
268 pca.tty = tp;
269
1c79356b 270 va_start(ap, fmt);
3e170ce0 271 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
1c79356b
A
272 va_end(ap);
273 }
274}
275
b0d623f7 276
1c79356b
A
277extern int log_open;
278
279
280void
2d21ac55 281logpri(int level)
1c79356b 282{
9bccf70c
A
283 struct putchar_args pca;
284 pca.flags = TOLOG;
285 pca.tty = NULL;
286
287 putchar('<', &pca);
b0d623f7 288 printn((uint32_t)level, 10, TOLOG, (struct tty *)0, 0, 0);
9bccf70c 289 putchar('>', &pca);
1c79356b
A
290}
291
2d21ac55
A
292static void
293_logtime(const char *fmt, ...)
1c79356b 294{
1c79356b 295 va_list ap;
2d21ac55
A
296 va_start(ap, fmt);
297 vaddlog(fmt, ap);
298 va_end(ap);
299}
300
301void
302logtime(time_t secs)
303{
304 _logtime(" 0 [Time %ld] [Message ", secs);
305}
306
307int
308vaddlog(const char *fmt, va_list ap)
309{
9bccf70c
A
310 struct putchar_args pca;
311
2d21ac55 312 pca.flags = TOLOGLOCKED;
9bccf70c 313 pca.tty = NULL;
1c79356b 314
9bccf70c 315 if (!log_open) {
2d21ac55 316 pca.flags |= TOCONS;
9bccf70c 317 }
2d21ac55
A
318
319 bsd_log_lock();
3e170ce0 320 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
2d21ac55
A
321 bsd_log_unlock();
322
1c79356b 323 logwakeup();
2d21ac55 324 return 0;
1c79356b 325}
2d21ac55
A
326
327void
328_printf(int flags, struct tty *ttyp, const char *format, ...)
1c79356b
A
329{
330 va_list ap;
9bccf70c
A
331 struct putchar_args pca;
332
333 pca.flags = flags;
334 pca.tty = ttyp;
b0d623f7
A
335
336 if (ttyp != NULL) {
337 tty_lock(ttyp);
1c79356b 338
b0d623f7 339 va_start(ap, format);
3e170ce0 340 __doprnt(format, ap, putchar, &pca, 10, TRUE);
b0d623f7
A
341 va_end(ap);
342
343 tty_unlock(ttyp);
344 }
1c79356b
A
345}
346
b0d623f7
A
347int
348prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
1c79356b 349{
9bccf70c 350 struct putchar_args pca;
1c79356b 351
9bccf70c
A
352 pca.flags = flags;
353 pca.tty = ttyp;
354
3e170ce0 355 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
9bccf70c 356
9bccf70c 357 return 0;
1c79356b
A
358}
359
1c79356b
A
360/*
361 * Printn prints a number n in base b.
362 * We don't use recursion to avoid deep kernel stacks.
363 */
b0d623f7
A
364static void
365printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size)
1c79356b
A
366{
367 char prbuf[11];
2d21ac55 368 char *cp;
9bccf70c
A
369 struct putchar_args pca;
370
371 pca.flags = flags;
372 pca.tty = ttyp;
1c79356b
A
373
374 if (b == 10 && (int)n < 0) {
9bccf70c 375 putchar('-', &pca);
1c79356b
A
376 n = (unsigned)(-(int)n);
377 }
378 cp = prbuf;
379 do {
380 *cp++ = "0123456789abcdef"[n%b];
381 n /= b;
382 } while (n);
383 if (fld_size) {
384 for (fld_size -= cp - prbuf; fld_size > 0; fld_size--)
385 if (zf)
9bccf70c 386 putchar('0', &pca);
1c79356b 387 else
9bccf70c 388 putchar(' ', &pca);
1c79356b
A
389 }
390 do
9bccf70c 391 putchar(*--cp, &pca);
1c79356b
A
392 while (cp > prbuf);
393}
394
395
396
397/*
398 * Warn that a system table is full.
399 */
400void tablefull(const char *tab)
401{
402 log(LOG_ERR, "%s: table is full\n", tab);
403}
404
405/*
406 * Print a character on console or users terminal.
407 * If destination is console then the last MSGBUFS characters
408 * are saved in msgbuf for inspection later.
b0d623f7
A
409 *
410 * Locks: If TOTTY is set, we assume that the tty lock is held
411 * over the call to this function.
1c79356b
A
412 */
413/*ARGSUSED*/
9bccf70c
A
414void
415putchar(int c, void *arg)
1c79356b 416{
9bccf70c 417 struct putchar_args *pca = arg;
9bccf70c 418 char **sp = (char**) pca->tty;
1c79356b
A
419
420 if (panicstr)
421 constty = 0;
9bccf70c
A
422 if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
423 pca->tty = constty;
424 pca->flags |= TOTTY;
1c79356b 425 }
9bccf70c
A
426 if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
427 (pca->flags & TOCONS) && pca->tty == constty)
1c79356b 428 constty = 0;
9bccf70c 429 if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177)
1c79356b 430 log_putc(c);
2d21ac55
A
431 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177)
432 log_putc_locked(c);
9bccf70c 433 if ((pca->flags & TOCONS) && constty == 0 && c != '\0')
1c79356b 434 (*v_putc)(c);
9bccf70c 435 if (pca->flags & TOSTR) {
1c79356b
A
436 **sp = c;
437 (*sp)++;
438 }
1c79356b
A
439}
440
2d21ac55 441int
39037602 442vprintf_log_locked(const char *fmt, va_list ap)
2d21ac55
A
443{
444 struct putchar_args pca;
1c79356b 445
39037602 446 pca.flags = TOLOGLOCKED;
2d21ac55 447 pca.tty = NULL;
3e170ce0 448 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
2d21ac55
A
449 return 0;
450}
1c79356b 451
316670eb 452
1c79356b
A
453/*
454 * Scaled down version of vsprintf(3).
2d21ac55
A
455 *
456 * Deprecation Warning:
457 * vsprintf() is being deprecated. Please use vsnprintf() instead.
1c79356b
A
458 */
459int
460vsprintf(char *buf, const char *cfmt, va_list ap)
461{
462 int retval;
9bccf70c 463 struct snprintf_arg info;
1c79356b 464
9bccf70c
A
465 info.str = buf;
466 info.remain = 999999;
467
3e170ce0 468 retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
9bccf70c
A
469 if (info.remain >= 1) {
470 *info.str++ = '\0';
471 }
472 return 0;
1c79356b
A
473}
474
475/*
476 * Scaled down version of snprintf(3).
477 */
478int
479snprintf(char *str, size_t size, const char *format, ...)
480{
481 int retval;
482 va_list ap;
483
484 va_start(ap, format);
485 retval = vsnprintf(str, size, format, ap);
486 va_end(ap);
487 return(retval);
488}
489
490/*
491 * Scaled down version of vsnprintf(3).
492 */
493int
494vsnprintf(char *str, size_t size, const char *format, va_list ap)
495{
496 struct snprintf_arg info;
497 int retval;
498
499 info.str = str;
500 info.remain = size;
3e170ce0 501 retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
1c79356b
A
502 if (info.remain >= 1)
503 *info.str++ = '\0';
504 return retval;
505}
506
507static void
508snprintf_func(int ch, void *arg)
509{
510 struct snprintf_arg *const info = arg;
511
512 if (info->remain >= 2) {
513 *info->str++ = ch;
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}
9bccf70c 524