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