]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_prf.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / kern / subr_prf.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*-
24 * Copyright (c) 1986, 1988, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
61 */
62/* HISTORY
63 * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com)
64 * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
65 *
66 * 17-July-97 Umesh Vaishampayan (umeshv@apple.com)
67 * Eliminated multiple definition of constty which is defined
68 * in bsd/dev/XXX/cons.c
69 *
70 * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com
71 * Fixed tharshing format in many functions. Cleanup.
72 *
73 * 17-Jun-1995 Mac Gillon (mgillon) at NeXT
74 * Purged old history
75 * New version based on 4.4 and NS3.3
76 */
77
78#include <sys/param.h>
79#include <sys/systm.h>
80#include <sys/buf.h>
81#include <sys/conf.h>
82#include <sys/reboot.h>
83#include <sys/msgbuf.h>
84#include <sys/proc.h>
85#include <sys/ioctl.h>
86#include <sys/tty.h>
87#include <sys/file.h>
88#include <sys/tprintf.h>
89#include <sys/syslog.h>
90#include <stdarg.h>
91#include <sys/malloc.h>
92#include <sys/lock.h>
1c79356b
A
93#include <sys/subr_prf.h>
94
95#include <kern/cpu_number.h> /* for cpu_number() */
96#include <machine/spl.h>
97#include <libkern/libkern.h>
98
99struct snprintf_arg {
100 char *str;
101 size_t remain;
102};
103
104
105/*
106 * In case console is off,
107 * panicstr contains argument to last
108 * call to panic.
109 */
110extern const char *panicstr;
111
112extern cnputc(); /* standard console putc */
113int (*v_putc)() = cnputc; /* routine to putc on virtual console */
114
115extern struct tty cons; /* standard console tty */
116extern struct tty *constty; /* pointer to console "window" tty */
9bccf70c
A
117extern int __doprnt(const char *fmt,
118 va_list *argp,
119 void (*putc)(int, void *arg),
120 void *arg,
121 int radix);
1c79356b
A
122
123/*
124 * Record cpu that panic'd and lock around panic data
125 */
126
127static void puts(const char *s, int flags, struct tty *ttyp);
128static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size);
129
130/* MP printf stuff */
131decl_simple_lock_data(,printf_lock)
132#if NCPUS > 1
133boolean_t new_printf_cpu_number; /* do we need to output who we are */
134#endif
135
136extern void logwakeup();
137extern void halt_cpu();
138extern boot();
9bccf70c 139
1c79356b
A
140
141static void
142snprintf_func(int ch, void *arg);
143
9bccf70c
A
144struct putchar_args {
145 int flags;
146 struct tty *tty;
147};
148static void putchar(int c, void *arg);
1c79356b
A
149
150
151/*
152 * Uprintf prints to the controlling terminal for the current process.
153 * It may block if the tty queue is overfull. No message is printed if
154 * the queue does not clear in a reasonable time.
155 */
156void
157uprintf(const char *fmt, ...)
158{
159 register struct proc *p = current_proc();
9bccf70c 160 struct putchar_args pca;
1c79356b 161 va_list ap;
9bccf70c
A
162
163 pca.flags = TOTTY;
164 pca.tty = (struct tty *)p->p_session->s_ttyp;
1c79356b 165
1c79356b
A
166 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
167 va_start(ap, fmt);
9bccf70c 168 __doprnt(fmt, &ap, putchar, &pca, 10);
1c79356b
A
169 va_end(ap);
170 }
1c79356b
A
171}
172
173tpr_t
174tprintf_open(p)
175 register struct proc *p;
176{
1c79356b
A
177 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
178 SESSHOLD(p->p_session);
1c79356b
A
179 return ((tpr_t) p->p_session);
180 }
1c79356b
A
181 return ((tpr_t) NULL);
182}
183
184void
185tprintf_close(sess)
186 tpr_t sess;
187{
1c79356b
A
188 if (sess)
189 SESSRELE((struct session *) sess);
1c79356b
A
190}
191
192/*
193 * tprintf prints on the controlling terminal associated
194 * with the given session.
195 */
196void
197tprintf(tpr_t tpr, const char *fmt, ...)
198{
199 register struct session *sess = (struct session *)tpr;
200 struct tty *tp = NULL;
201 int flags = TOLOG;
202 va_list ap;
9bccf70c 203 struct putchar_args pca;
1c79356b
A
204
205 logpri(LOG_INFO);
1c79356b
A
206 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
207 flags |= TOTTY;
208 tp = sess->s_ttyp;
209 }
55e303ae
A
210
211 pca.flags = flags;
212 pca.tty = tp;
213 va_start(ap, fmt);
214 __doprnt(fmt, &ap, putchar, &pca, 10);
215 va_end(ap);
216
1c79356b
A
217 logwakeup();
218}
219
220/*
221 * Ttyprintf displays a message on a tty; it should be used only by
222 * the tty driver, or anything that knows the underlying tty will not
223 * be revoke(2)'d away. Other callers should use tprintf.
224 */
225void
226ttyprintf(struct tty *tp, const char *fmt, ...)
227{
228 va_list ap;
229
230 if (tp != NULL) {
9bccf70c
A
231 struct putchar_args pca;
232 pca.flags = TOTTY;
233 pca.tty = tp;
234
1c79356b 235 va_start(ap, fmt);
9bccf70c 236 __doprnt(fmt, &ap, putchar, &pca, 10);
1c79356b
A
237 va_end(ap);
238 }
239}
240
241extern int log_open;
242
243
244void
245logpri(level)
246 int level;
247{
9bccf70c
A
248 struct putchar_args pca;
249 pca.flags = TOLOG;
250 pca.tty = NULL;
251
252 putchar('<', &pca);
1c79356b 253 printn((u_long)level, 10, TOLOG, (struct tty *)0, 0, 0);
9bccf70c 254 putchar('>', &pca);
1c79356b
A
255}
256
257void
258addlog(const char *fmt, ...)
259{
260 register s = splhigh();
261 va_list ap;
9bccf70c
A
262 struct putchar_args pca;
263
264 pca.flags = TOLOG;
265 pca.tty = NULL;
1c79356b
A
266
267 va_start(ap, fmt);
9bccf70c
A
268 __doprnt(fmt, &ap, putchar, &pca, 10);
269
1c79356b 270 splx(s);
9bccf70c
A
271 if (!log_open) {
272 pca.flags = TOCONS;
273 __doprnt(fmt, &ap, putchar, &pca, 10);
274 }
1c79356b
A
275 va_end(ap);
276 logwakeup();
277}
278void _printf(int flags, struct tty *ttyp, const char *format, ...)
279{
280 va_list ap;
9bccf70c
A
281 struct putchar_args pca;
282
283 pca.flags = flags;
284 pca.tty = ttyp;
1c79356b
A
285
286 va_start(ap, format);
9bccf70c 287 __doprnt(format, &ap, putchar, &pca, 10);
1c79356b
A
288 va_end(ap);
289}
290
291int prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
292{
9bccf70c 293 struct putchar_args pca;
1c79356b 294
9bccf70c
A
295 pca.flags = flags;
296 pca.tty = ttyp;
297
298#if NCPUS > 1
299 int cpun = cpu_number();
300
301 if(ttyp == 0) {
302 simple_lock(&printf_lock);
1c79356b
A
303 } else
304 TTY_LOCK(ttyp);
9bccf70c 305
1c79356b 306 if (cpun != master_cpu)
9bccf70c 307 new_printf_cpu_number = TRUE;
1c79356b
A
308
309 if (new_printf_cpu_number) {
310 putchar('{', flags, ttyp);
311 printn((u_long)cpun, 10, flags, ttyp, 0, 0);
312 putchar('}', flags, ttyp);
313 }
9bccf70c
A
314#endif /* NCPUS > 1 */
315
316 __doprnt(fmt, &ap, putchar, &pca, 10);
1c79356b 317
9bccf70c
A
318#if NCPUS > 1
319 if(ttyp == 0) {
320 simple_unlock(&printf_lock);
321 } else
322 TTY_UNLOCK(ttyp);
323#endif
324
325 return 0;
1c79356b
A
326}
327
328static void puts(const char *s, int flags, struct tty *ttyp)
329{
330 register char c;
9bccf70c
A
331 struct putchar_args pca;
332
333 pca.flags = flags;
334 pca.tty = ttyp;
1c79356b
A
335
336 while ((c = *s++))
9bccf70c 337 putchar(c, &pca);
1c79356b
A
338}
339
340/*
341 * Printn prints a number n in base b.
342 * We don't use recursion to avoid deep kernel stacks.
343 */
344static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size)
345{
346 char prbuf[11];
347 register char *cp;
9bccf70c
A
348 struct putchar_args pca;
349
350 pca.flags = flags;
351 pca.tty = ttyp;
1c79356b
A
352
353 if (b == 10 && (int)n < 0) {
9bccf70c 354 putchar('-', &pca);
1c79356b
A
355 n = (unsigned)(-(int)n);
356 }
357 cp = prbuf;
358 do {
359 *cp++ = "0123456789abcdef"[n%b];
360 n /= b;
361 } while (n);
362 if (fld_size) {
363 for (fld_size -= cp - prbuf; fld_size > 0; fld_size--)
364 if (zf)
9bccf70c 365 putchar('0', &pca);
1c79356b 366 else
9bccf70c 367 putchar(' ', &pca);
1c79356b
A
368 }
369 do
9bccf70c 370 putchar(*--cp, &pca);
1c79356b
A
371 while (cp > prbuf);
372}
373
374
375
376/*
377 * Warn that a system table is full.
378 */
379void tablefull(const char *tab)
380{
381 log(LOG_ERR, "%s: table is full\n", tab);
382}
383
384/*
385 * Print a character on console or users terminal.
386 * If destination is console then the last MSGBUFS characters
387 * are saved in msgbuf for inspection later.
388 */
389/*ARGSUSED*/
9bccf70c
A
390void
391putchar(int c, void *arg)
1c79356b 392{
9bccf70c 393 struct putchar_args *pca = arg;
1c79356b 394 register struct msgbuf *mbp;
9bccf70c 395 char **sp = (char**) pca->tty;
1c79356b
A
396
397 if (panicstr)
398 constty = 0;
9bccf70c
A
399 if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
400 pca->tty = constty;
401 pca->flags |= TOTTY;
1c79356b 402 }
9bccf70c
A
403 if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
404 (pca->flags & TOCONS) && pca->tty == constty)
1c79356b 405 constty = 0;
9bccf70c 406 if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177)
1c79356b 407 log_putc(c);
9bccf70c 408 if ((pca->flags & TOCONS) && constty == 0 && c != '\0')
1c79356b 409 (*v_putc)(c);
9bccf70c 410 if (pca->flags & TOSTR) {
1c79356b
A
411 **sp = c;
412 (*sp)++;
413 }
1c79356b
A
414}
415
416
417
418/*
419 * Scaled down version of vsprintf(3).
420 */
421int
422vsprintf(char *buf, const char *cfmt, va_list ap)
423{
424 int retval;
9bccf70c 425 struct snprintf_arg info;
1c79356b 426
9bccf70c
A
427 info.str = buf;
428 info.remain = 999999;
429
430 retval = __doprnt(cfmt, &ap, snprintf_func, &info, 10);
431 if (info.remain >= 1) {
432 *info.str++ = '\0';
433 }
434 return 0;
1c79356b
A
435}
436
437/*
438 * Scaled down version of snprintf(3).
439 */
440int
441snprintf(char *str, size_t size, const char *format, ...)
442{
443 int retval;
444 va_list ap;
445
446 va_start(ap, format);
447 retval = vsnprintf(str, size, format, ap);
448 va_end(ap);
449 return(retval);
450}
451
452/*
453 * Scaled down version of vsnprintf(3).
454 */
455int
456vsnprintf(char *str, size_t size, const char *format, va_list ap)
457{
458 struct snprintf_arg info;
459 int retval;
460
461 info.str = str;
462 info.remain = size;
9bccf70c 463 retval = __doprnt(format, &ap, snprintf_func, &info, 10);
1c79356b
A
464 if (info.remain >= 1)
465 *info.str++ = '\0';
466 return retval;
467}
468
469static void
470snprintf_func(int ch, void *arg)
471{
472 struct snprintf_arg *const info = arg;
473
474 if (info->remain >= 2) {
475 *info->str++ = ch;
476 info->remain--;
477 }
478}
479
1c79356b
A
480int
481kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
482{
9bccf70c
A
483 __doprnt(fmt, &ap, func, arg, radix);
484 return 0;
1c79356b 485}
9bccf70c 486