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