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