subr_prf.c revision 1.76.2.8 1 1.76.2.8 nathanw /* $NetBSD: subr_prf.c,v 1.76.2.8 2002/06/24 22:10:58 nathanw Exp $ */
2 1.15 cgd
3 1.12 cgd /*-
4 1.12 cgd * Copyright (c) 1986, 1988, 1991, 1993
5 1.12 cgd * The Regents of the University of California. All rights reserved.
6 1.12 cgd * (c) UNIX System Laboratories, Inc.
7 1.12 cgd * All or some portions of this file are derived from material licensed
8 1.12 cgd * to the University of California by American Telephone and Telegraph
9 1.12 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.12 cgd * the permission of UNIX System Laboratories, Inc.
11 1.12 cgd *
12 1.12 cgd * Redistribution and use in source and binary forms, with or without
13 1.12 cgd * modification, are permitted provided that the following conditions
14 1.12 cgd * are met:
15 1.12 cgd * 1. Redistributions of source code must retain the above copyright
16 1.12 cgd * notice, this list of conditions and the following disclaimer.
17 1.12 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.12 cgd * notice, this list of conditions and the following disclaimer in the
19 1.12 cgd * documentation and/or other materials provided with the distribution.
20 1.12 cgd * 3. All advertising materials mentioning features or use of this software
21 1.12 cgd * must display the following acknowledgement:
22 1.12 cgd * This product includes software developed by the University of
23 1.12 cgd * California, Berkeley and its contributors.
24 1.12 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.12 cgd * may be used to endorse or promote products derived from this software
26 1.12 cgd * without specific prior written permission.
27 1.12 cgd *
28 1.12 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.12 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.12 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.12 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.12 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.12 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.12 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.12 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.12 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.12 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.12 cgd * SUCH DAMAGE.
39 1.12 cgd *
40 1.48 fvdl * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
41 1.12 cgd */
42 1.76.2.5 nathanw
43 1.76.2.5 nathanw #include <sys/cdefs.h>
44 1.76.2.8 nathanw __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.76.2.8 2002/06/24 22:10:58 nathanw Exp $");
45 1.54 thorpej
46 1.49 jonathan #include "opt_ddb.h"
47 1.67 ws #include "opt_ipkdb.h"
48 1.76.2.3 nathanw #include "opt_kgdb.h"
49 1.54 thorpej #include "opt_multiprocessor.h"
50 1.12 cgd
51 1.12 cgd #include <sys/param.h>
52 1.76.2.3 nathanw #include <sys/stdint.h>
53 1.12 cgd #include <sys/systm.h>
54 1.12 cgd #include <sys/buf.h>
55 1.12 cgd #include <sys/reboot.h>
56 1.12 cgd #include <sys/msgbuf.h>
57 1.76.2.1 nathanw #include <sys/lwp.h>
58 1.12 cgd #include <sys/proc.h>
59 1.12 cgd #include <sys/ioctl.h>
60 1.12 cgd #include <sys/vnode.h>
61 1.12 cgd #include <sys/file.h>
62 1.12 cgd #include <sys/tty.h>
63 1.12 cgd #include <sys/tprintf.h>
64 1.12 cgd #include <sys/syslog.h>
65 1.12 cgd #include <sys/malloc.h>
66 1.54 thorpej #include <sys/lock.h>
67 1.12 cgd
68 1.20 christos #include <dev/cons.h>
69 1.20 christos
70 1.49 jonathan #ifdef DDB
71 1.49 jonathan #include <ddb/ddbvar.h>
72 1.72 jhawk #include <machine/db_machdep.h>
73 1.72 jhawk #include <ddb/db_command.h>
74 1.72 jhawk #include <ddb/db_interface.h>
75 1.49 jonathan #endif
76 1.49 jonathan
77 1.67 ws #ifdef IPKDB
78 1.67 ws #include <ipkdb/ipkdb.h>
79 1.67 ws #endif
80 1.67 ws
81 1.54 thorpej #if defined(MULTIPROCESSOR)
82 1.63 thorpej struct simplelock kprintf_slock = SIMPLELOCK_INITIALIZER;
83 1.54 thorpej
84 1.63 thorpej /*
85 1.63 thorpej * Use cpu_simple_lock() and cpu_simple_unlock(). These are the actual
86 1.63 thorpej * atomic locking operations, and never attempt to print debugging
87 1.63 thorpej * information.
88 1.63 thorpej */
89 1.54 thorpej #define KPRINTF_MUTEX_ENTER(s) \
90 1.54 thorpej do { \
91 1.54 thorpej (s) = splhigh(); \
92 1.70 pk __cpu_simple_lock(&kprintf_slock.lock_data); \
93 1.54 thorpej } while (0)
94 1.54 thorpej
95 1.54 thorpej #define KPRINTF_MUTEX_EXIT(s) \
96 1.54 thorpej do { \
97 1.70 pk __cpu_simple_unlock(&kprintf_slock.lock_data); \
98 1.54 thorpej splx((s)); \
99 1.54 thorpej } while (0)
100 1.54 thorpej #else /* ! MULTIPROCESSOR */
101 1.54 thorpej #define KPRINTF_MUTEX_ENTER(s) (s) = splhigh()
102 1.54 thorpej #define KPRINTF_MUTEX_EXIT(s) splx((s))
103 1.55 thorpej #endif /* MULTIPROCESSOR */
104 1.49 jonathan
105 1.12 cgd /*
106 1.45 chuck * note that stdarg.h and the ansi style va_start macro is used for both
107 1.45 chuck * ansi and traditional c complers.
108 1.45 chuck * XXX: this requires that stdarg.h define: va_alist and va_dcl
109 1.12 cgd */
110 1.12 cgd #include <machine/stdarg.h>
111 1.12 cgd
112 1.28 ws
113 1.22 christos #ifdef KGDB
114 1.42 thorpej #include <sys/kgdb.h>
115 1.22 christos #include <machine/cpu.h>
116 1.12 cgd #endif
117 1.45 chuck #ifdef DDB
118 1.45 chuck #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
119 1.45 chuck #endif
120 1.45 chuck
121 1.45 chuck
122 1.45 chuck /*
123 1.45 chuck * defines
124 1.45 chuck */
125 1.45 chuck
126 1.45 chuck /* flags for kprintf */
127 1.45 chuck #define TOCONS 0x01 /* to the console */
128 1.45 chuck #define TOTTY 0x02 /* to the process' tty */
129 1.45 chuck #define TOLOG 0x04 /* to the kernel message buffer */
130 1.76 tv #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */
131 1.45 chuck #define TODDB 0x10 /* to ddb console */
132 1.45 chuck
133 1.45 chuck /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
134 1.45 chuck #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
135 1.45 chuck
136 1.45 chuck
137 1.45 chuck /*
138 1.45 chuck * local prototypes
139 1.45 chuck */
140 1.45 chuck
141 1.58 msaitoh static int kprintf __P((const char *, int, void *,
142 1.45 chuck char *, va_list));
143 1.45 chuck static void putchar __P((int, int, struct tty *));
144 1.54 thorpej static void klogpri __P((int));
145 1.20 christos
146 1.12 cgd
147 1.37 thorpej /*
148 1.45 chuck * globals
149 1.37 thorpej */
150 1.37 thorpej
151 1.76.2.2 nathanw extern struct tty *constty; /* pointer to console "window" tty */
152 1.45 chuck extern int log_open; /* subr_log: is /dev/klog open? */
153 1.45 chuck const char *panicstr; /* arg to first call to panic (used as a flag
154 1.45 chuck to indicate that panic has already been called). */
155 1.76.2.3 nathanw long panicstart, panicend; /* position in the msgbuf of the start and
156 1.76.2.3 nathanw end of the formatted panicstr. */
157 1.74 sommerfe int doing_shutdown; /* set to indicate shutdown in progress */
158 1.45 chuck
159 1.45 chuck /*
160 1.45 chuck * v_putc: routine to putc on virtual console
161 1.45 chuck *
162 1.45 chuck * the v_putc pointer can be used to redirect the console cnputc elsewhere
163 1.45 chuck * [e.g. to a "virtual console"].
164 1.45 chuck */
165 1.12 cgd
166 1.45 chuck void (*v_putc) __P((int)) = cnputc; /* start with cnputc (normal cons) */
167 1.12 cgd
168 1.12 cgd
169 1.45 chuck /*
170 1.45 chuck * functions
171 1.45 chuck */
172 1.12 cgd
173 1.12 cgd /*
174 1.45 chuck * tablefull: warn that a system table is full
175 1.12 cgd */
176 1.45 chuck
177 1.45 chuck void
178 1.75 jdolecek tablefull(tab, hint)
179 1.75 jdolecek const char *tab, *hint;
180 1.45 chuck {
181 1.75 jdolecek if (hint)
182 1.75 jdolecek log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
183 1.75 jdolecek else
184 1.75 jdolecek log(LOG_ERR, "%s: table is full\n", tab);
185 1.45 chuck }
186 1.12 cgd
187 1.12 cgd /*
188 1.45 chuck * panic: handle an unresolvable fatal error
189 1.45 chuck *
190 1.45 chuck * prints "panic: <message>" and reboots. if called twice (i.e. recursive
191 1.45 chuck * call) we avoid trying to sync the disk and just reboot (to avoid
192 1.45 chuck * recursive panics).
193 1.12 cgd */
194 1.45 chuck
195 1.14 cgd void
196 1.12 cgd #ifdef __STDC__
197 1.12 cgd panic(const char *fmt, ...)
198 1.12 cgd #else
199 1.14 cgd panic(fmt, va_alist)
200 1.12 cgd char *fmt;
201 1.20 christos va_dcl
202 1.12 cgd #endif
203 1.12 cgd {
204 1.14 cgd int bootopt;
205 1.12 cgd va_list ap;
206 1.12 cgd
207 1.14 cgd bootopt = RB_AUTOBOOT | RB_DUMP;
208 1.74 sommerfe if (doing_shutdown)
209 1.12 cgd bootopt |= RB_NOSYNC;
210 1.74 sommerfe if (!panicstr)
211 1.12 cgd panicstr = fmt;
212 1.74 sommerfe doing_shutdown = 1;
213 1.76.2.3 nathanw
214 1.76.2.4 nathanw if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
215 1.76.2.3 nathanw panicstart = msgbufp->msg_bufx;
216 1.74 sommerfe
217 1.12 cgd va_start(ap, fmt);
218 1.45 chuck printf("panic: ");
219 1.45 chuck vprintf(fmt, ap);
220 1.45 chuck printf("\n");
221 1.12 cgd va_end(ap);
222 1.12 cgd
223 1.76.2.4 nathanw if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
224 1.76.2.3 nathanw panicend = msgbufp->msg_bufx;
225 1.76.2.3 nathanw
226 1.67 ws #ifdef IPKDB
227 1.32 ws ipkdb_panic();
228 1.28 ws #endif
229 1.12 cgd #ifdef KGDB
230 1.12 cgd kgdb_panic();
231 1.12 cgd #endif
232 1.12 cgd #ifdef KADB
233 1.14 cgd if (boothowto & RB_KDB)
234 1.14 cgd kdbpanic();
235 1.12 cgd #endif
236 1.12 cgd #ifdef DDB
237 1.38 thorpej if (db_onpanic)
238 1.38 thorpej Debugger();
239 1.72 jhawk else {
240 1.73 jhawk static int intrace = 0;
241 1.73 jhawk
242 1.73 jhawk if (intrace==0) {
243 1.73 jhawk intrace=1;
244 1.73 jhawk printf("Begin traceback...\n");
245 1.73 jhawk db_stack_trace_print(
246 1.73 jhawk (db_expr_t)__builtin_frame_address(0),
247 1.73 jhawk TRUE, 65535, "", printf);
248 1.73 jhawk printf("End traceback...\n");
249 1.73 jhawk intrace=0;
250 1.73 jhawk } else
251 1.73 jhawk printf("Faulted in mid-traceback; aborting...");
252 1.72 jhawk }
253 1.12 cgd #endif
254 1.39 gwr cpu_reboot(bootopt, NULL);
255 1.12 cgd }
256 1.12 cgd
257 1.12 cgd /*
258 1.45 chuck * kernel logging functions: log, logpri, addlog
259 1.45 chuck */
260 1.45 chuck
261 1.45 chuck /*
262 1.45 chuck * log: write to the log buffer
263 1.45 chuck *
264 1.45 chuck * => will not sleep [so safe to call from interrupt]
265 1.45 chuck * => will log to console if /dev/klog isn't open
266 1.45 chuck */
267 1.45 chuck
268 1.45 chuck void
269 1.45 chuck #ifdef __STDC__
270 1.45 chuck log(int level, const char *fmt, ...)
271 1.45 chuck #else
272 1.45 chuck log(level, fmt, va_alist)
273 1.45 chuck int level;
274 1.45 chuck char *fmt;
275 1.45 chuck va_dcl
276 1.45 chuck #endif
277 1.45 chuck {
278 1.54 thorpej int s;
279 1.45 chuck va_list ap;
280 1.45 chuck
281 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
282 1.54 thorpej
283 1.54 thorpej klogpri(level); /* log the level first */
284 1.45 chuck va_start(ap, fmt);
285 1.45 chuck kprintf(fmt, TOLOG, NULL, NULL, ap);
286 1.45 chuck va_end(ap);
287 1.45 chuck if (!log_open) {
288 1.45 chuck va_start(ap, fmt);
289 1.45 chuck kprintf(fmt, TOCONS, NULL, NULL, ap);
290 1.45 chuck va_end(ap);
291 1.45 chuck }
292 1.64 thorpej
293 1.64 thorpej KPRINTF_MUTEX_EXIT(s);
294 1.64 thorpej
295 1.64 thorpej logwakeup(); /* wake up anyone waiting for log msgs */
296 1.64 thorpej }
297 1.64 thorpej
298 1.64 thorpej /*
299 1.64 thorpej * vlog: write to the log buffer [already have va_alist]
300 1.64 thorpej */
301 1.64 thorpej
302 1.64 thorpej void
303 1.64 thorpej vlog(level, fmt, ap)
304 1.64 thorpej int level;
305 1.64 thorpej const char *fmt;
306 1.64 thorpej va_list ap;
307 1.64 thorpej {
308 1.64 thorpej int s;
309 1.64 thorpej
310 1.64 thorpej KPRINTF_MUTEX_ENTER(s);
311 1.64 thorpej
312 1.64 thorpej klogpri(level); /* log the level first */
313 1.64 thorpej kprintf(fmt, TOLOG, NULL, NULL, ap);
314 1.64 thorpej if (!log_open)
315 1.64 thorpej kprintf(fmt, TOCONS, NULL, NULL, ap);
316 1.54 thorpej
317 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
318 1.54 thorpej
319 1.45 chuck logwakeup(); /* wake up anyone waiting for log msgs */
320 1.45 chuck }
321 1.45 chuck
322 1.45 chuck /*
323 1.45 chuck * logpri: log the priority level to the klog
324 1.45 chuck */
325 1.45 chuck
326 1.54 thorpej void
327 1.45 chuck logpri(level)
328 1.45 chuck int level;
329 1.45 chuck {
330 1.54 thorpej int s;
331 1.54 thorpej
332 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
333 1.54 thorpej klogpri(level);
334 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
335 1.54 thorpej }
336 1.54 thorpej
337 1.54 thorpej /*
338 1.54 thorpej * Note: we must be in the mutex here!
339 1.54 thorpej */
340 1.54 thorpej static void
341 1.54 thorpej klogpri(level)
342 1.54 thorpej int level;
343 1.54 thorpej {
344 1.45 chuck char *p;
345 1.45 chuck char snbuf[KPRINTF_BUFSIZE];
346 1.45 chuck
347 1.45 chuck putchar('<', TOLOG, NULL);
348 1.76 tv snprintf(snbuf, sizeof(snbuf), "%d", level);
349 1.45 chuck for (p = snbuf ; *p ; p++)
350 1.45 chuck putchar(*p, TOLOG, NULL);
351 1.45 chuck putchar('>', TOLOG, NULL);
352 1.45 chuck }
353 1.45 chuck
354 1.45 chuck /*
355 1.45 chuck * addlog: add info to previous log message
356 1.12 cgd */
357 1.45 chuck
358 1.12 cgd void
359 1.45 chuck #ifdef __STDC__
360 1.45 chuck addlog(const char *fmt, ...)
361 1.45 chuck #else
362 1.45 chuck addlog(fmt, va_alist)
363 1.45 chuck char *fmt;
364 1.45 chuck va_dcl
365 1.45 chuck #endif
366 1.45 chuck {
367 1.54 thorpej int s;
368 1.45 chuck va_list ap;
369 1.45 chuck
370 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
371 1.54 thorpej
372 1.45 chuck va_start(ap, fmt);
373 1.45 chuck kprintf(fmt, TOLOG, NULL, NULL, ap);
374 1.45 chuck va_end(ap);
375 1.45 chuck if (!log_open) {
376 1.45 chuck va_start(ap, fmt);
377 1.45 chuck kprintf(fmt, TOCONS, NULL, NULL, ap);
378 1.45 chuck va_end(ap);
379 1.45 chuck }
380 1.54 thorpej
381 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
382 1.54 thorpej
383 1.45 chuck logwakeup();
384 1.45 chuck }
385 1.45 chuck
386 1.45 chuck
387 1.45 chuck /*
388 1.45 chuck * putchar: print a single character on console or user terminal.
389 1.45 chuck *
390 1.45 chuck * => if console, then the last MSGBUFS chars are saved in msgbuf
391 1.45 chuck * for inspection later (e.g. dmesg/syslog)
392 1.54 thorpej * => we must already be in the mutex!
393 1.45 chuck */
394 1.45 chuck static void
395 1.45 chuck putchar(c, flags, tp)
396 1.69 augustss int c;
397 1.45 chuck int flags;
398 1.45 chuck struct tty *tp;
399 1.12 cgd {
400 1.69 augustss struct kern_msgbuf *mbp;
401 1.12 cgd
402 1.45 chuck if (panicstr)
403 1.45 chuck constty = NULL;
404 1.45 chuck if ((flags & TOCONS) && tp == NULL && constty) {
405 1.45 chuck tp = constty;
406 1.45 chuck flags |= TOTTY;
407 1.45 chuck }
408 1.45 chuck if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
409 1.45 chuck (flags & TOCONS) && tp == constty)
410 1.45 chuck constty = NULL;
411 1.45 chuck if ((flags & TOLOG) &&
412 1.45 chuck c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
413 1.45 chuck mbp = msgbufp;
414 1.45 chuck if (mbp->msg_magic != MSG_MAGIC) {
415 1.45 chuck /*
416 1.45 chuck * Arguably should panic or somehow notify the
417 1.45 chuck * user... but how? Panic may be too drastic,
418 1.45 chuck * and would obliterate the message being kicked
419 1.45 chuck * out (maybe a panic itself), and printf
420 1.45 chuck * would invoke us recursively. Silently punt
421 1.45 chuck * for now. If syslog is running, it should
422 1.45 chuck * notice.
423 1.45 chuck */
424 1.45 chuck msgbufenabled = 0;
425 1.45 chuck } else {
426 1.45 chuck mbp->msg_bufc[mbp->msg_bufx++] = c;
427 1.45 chuck if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
428 1.45 chuck mbp->msg_bufx = 0;
429 1.48 fvdl /* If the buffer is full, keep the most recent data. */
430 1.48 fvdl if (mbp->msg_bufr == mbp->msg_bufx) {
431 1.48 fvdl if (++mbp->msg_bufr >= mbp->msg_bufs)
432 1.48 fvdl mbp->msg_bufr = 0;
433 1.48 fvdl }
434 1.45 chuck }
435 1.45 chuck }
436 1.45 chuck if ((flags & TOCONS) && constty == NULL && c != '\0')
437 1.45 chuck (*v_putc)(c);
438 1.45 chuck #ifdef DDB
439 1.45 chuck if (flags & TODDB)
440 1.45 chuck db_putchar(c);
441 1.45 chuck #endif
442 1.12 cgd }
443 1.12 cgd
444 1.45 chuck
445 1.12 cgd /*
446 1.45 chuck * uprintf: print to the controlling tty of the current process
447 1.45 chuck *
448 1.45 chuck * => we may block if the tty queue is full
449 1.45 chuck * => no message is printed if the queue doesn't clear in a reasonable
450 1.45 chuck * time
451 1.12 cgd */
452 1.45 chuck
453 1.12 cgd void
454 1.12 cgd #ifdef __STDC__
455 1.12 cgd uprintf(const char *fmt, ...)
456 1.12 cgd #else
457 1.14 cgd uprintf(fmt, va_alist)
458 1.12 cgd char *fmt;
459 1.20 christos va_dcl
460 1.12 cgd #endif
461 1.12 cgd {
462 1.76.2.8 nathanw struct proc *p = curproc;
463 1.12 cgd va_list ap;
464 1.12 cgd
465 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
466 1.54 thorpej /* No mutex needed; going to process TTY. */
467 1.12 cgd va_start(ap, fmt);
468 1.45 chuck kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
469 1.12 cgd va_end(ap);
470 1.12 cgd }
471 1.12 cgd }
472 1.12 cgd
473 1.45 chuck /*
474 1.45 chuck * tprintf functions: used to send messages to a specific process
475 1.45 chuck *
476 1.45 chuck * usage:
477 1.45 chuck * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
478 1.45 chuck * use the handle when calling "tprintf"
479 1.45 chuck * when done, do a "tprintf_close" to drop the handle
480 1.45 chuck */
481 1.45 chuck
482 1.45 chuck /*
483 1.45 chuck * tprintf_open: get a tprintf handle on a process "p"
484 1.45 chuck *
485 1.45 chuck * => returns NULL if process can't be printed to
486 1.45 chuck */
487 1.45 chuck
488 1.12 cgd tpr_t
489 1.12 cgd tprintf_open(p)
490 1.69 augustss struct proc *p;
491 1.12 cgd {
492 1.12 cgd
493 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
494 1.12 cgd SESSHOLD(p->p_session);
495 1.12 cgd return ((tpr_t) p->p_session);
496 1.12 cgd }
497 1.12 cgd return ((tpr_t) NULL);
498 1.12 cgd }
499 1.12 cgd
500 1.45 chuck /*
501 1.45 chuck * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
502 1.45 chuck */
503 1.45 chuck
504 1.12 cgd void
505 1.12 cgd tprintf_close(sess)
506 1.12 cgd tpr_t sess;
507 1.12 cgd {
508 1.12 cgd
509 1.12 cgd if (sess)
510 1.12 cgd SESSRELE((struct session *) sess);
511 1.12 cgd }
512 1.12 cgd
513 1.12 cgd /*
514 1.45 chuck * tprintf: given tprintf handle to a process [obtained with tprintf_open],
515 1.45 chuck * send a message to the controlling tty for that process.
516 1.45 chuck *
517 1.45 chuck * => also sends message to /dev/klog
518 1.12 cgd */
519 1.12 cgd void
520 1.12 cgd #ifdef __STDC__
521 1.12 cgd tprintf(tpr_t tpr, const char *fmt, ...)
522 1.12 cgd #else
523 1.14 cgd tprintf(tpr, fmt, va_alist)
524 1.12 cgd tpr_t tpr;
525 1.12 cgd char *fmt;
526 1.20 christos va_dcl
527 1.12 cgd #endif
528 1.12 cgd {
529 1.69 augustss struct session *sess = (struct session *)tpr;
530 1.12 cgd struct tty *tp = NULL;
531 1.54 thorpej int s, flags = TOLOG;
532 1.12 cgd va_list ap;
533 1.12 cgd
534 1.12 cgd if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
535 1.12 cgd flags |= TOTTY;
536 1.12 cgd tp = sess->s_ttyp;
537 1.12 cgd }
538 1.54 thorpej
539 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
540 1.54 thorpej
541 1.54 thorpej klogpri(LOG_INFO);
542 1.12 cgd va_start(ap, fmt);
543 1.45 chuck kprintf(fmt, flags, tp, NULL, ap);
544 1.12 cgd va_end(ap);
545 1.54 thorpej
546 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
547 1.54 thorpej
548 1.12 cgd logwakeup();
549 1.12 cgd }
550 1.12 cgd
551 1.45 chuck
552 1.12 cgd /*
553 1.45 chuck * ttyprintf: send a message to a specific tty
554 1.45 chuck *
555 1.45 chuck * => should be used only by tty driver or anything that knows the
556 1.52 mrg * underlying tty will not be revoked(2)'d away. [otherwise,
557 1.52 mrg * use tprintf]
558 1.12 cgd */
559 1.12 cgd void
560 1.12 cgd #ifdef __STDC__
561 1.12 cgd ttyprintf(struct tty *tp, const char *fmt, ...)
562 1.12 cgd #else
563 1.14 cgd ttyprintf(tp, fmt, va_alist)
564 1.12 cgd struct tty *tp;
565 1.12 cgd char *fmt;
566 1.20 christos va_dcl
567 1.12 cgd #endif
568 1.12 cgd {
569 1.12 cgd va_list ap;
570 1.12 cgd
571 1.54 thorpej /* No mutex needed; going to process TTY. */
572 1.12 cgd va_start(ap, fmt);
573 1.45 chuck kprintf(fmt, TOTTY, tp, NULL, ap);
574 1.12 cgd va_end(ap);
575 1.12 cgd }
576 1.12 cgd
577 1.45 chuck #ifdef DDB
578 1.12 cgd
579 1.12 cgd /*
580 1.45 chuck * db_printf: printf for DDB (via db_putchar)
581 1.12 cgd */
582 1.45 chuck
583 1.12 cgd void
584 1.12 cgd #ifdef __STDC__
585 1.45 chuck db_printf(const char *fmt, ...)
586 1.12 cgd #else
587 1.45 chuck db_printf(fmt, va_alist)
588 1.12 cgd char *fmt;
589 1.20 christos va_dcl
590 1.12 cgd #endif
591 1.12 cgd {
592 1.12 cgd va_list ap;
593 1.12 cgd
594 1.54 thorpej /* No mutex needed; DDB pauses all processors. */
595 1.12 cgd va_start(ap, fmt);
596 1.45 chuck kprintf(fmt, TODDB, NULL, NULL, ap);
597 1.12 cgd va_end(ap);
598 1.76.2.7 nathanw }
599 1.76.2.7 nathanw
600 1.76.2.7 nathanw void
601 1.76.2.7 nathanw db_vprintf(fmt, ap)
602 1.76.2.7 nathanw const char *fmt;
603 1.76.2.7 nathanw va_list ap;
604 1.76.2.7 nathanw {
605 1.76.2.7 nathanw
606 1.76.2.7 nathanw /* No mutex needed; DDB pauses all processors. */
607 1.76.2.7 nathanw kprintf(fmt, TODDB, NULL, NULL, ap);
608 1.12 cgd }
609 1.12 cgd
610 1.45 chuck #endif /* DDB */
611 1.12 cgd
612 1.12 cgd
613 1.45 chuck /*
614 1.76 tv * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
615 1.45 chuck */
616 1.12 cgd
617 1.45 chuck /*
618 1.45 chuck * printf: print a message to the console and the log
619 1.45 chuck */
620 1.12 cgd void
621 1.12 cgd #ifdef __STDC__
622 1.30 christos printf(const char *fmt, ...)
623 1.12 cgd #else
624 1.30 christos printf(fmt, va_alist)
625 1.12 cgd char *fmt;
626 1.20 christos va_dcl
627 1.12 cgd #endif
628 1.12 cgd {
629 1.12 cgd va_list ap;
630 1.66 thorpej int s;
631 1.54 thorpej
632 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
633 1.12 cgd
634 1.12 cgd va_start(ap, fmt);
635 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
636 1.12 cgd va_end(ap);
637 1.54 thorpej
638 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
639 1.54 thorpej
640 1.12 cgd if (!panicstr)
641 1.12 cgd logwakeup();
642 1.12 cgd }
643 1.12 cgd
644 1.45 chuck /*
645 1.45 chuck * vprintf: print a message to the console and the log [already have
646 1.45 chuck * va_alist]
647 1.45 chuck */
648 1.45 chuck
649 1.40 thorpej void
650 1.40 thorpej vprintf(fmt, ap)
651 1.40 thorpej const char *fmt;
652 1.40 thorpej va_list ap;
653 1.40 thorpej {
654 1.66 thorpej int s;
655 1.54 thorpej
656 1.54 thorpej KPRINTF_MUTEX_ENTER(s);
657 1.40 thorpej
658 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
659 1.54 thorpej
660 1.54 thorpej KPRINTF_MUTEX_EXIT(s);
661 1.54 thorpej
662 1.40 thorpej if (!panicstr)
663 1.40 thorpej logwakeup();
664 1.40 thorpej }
665 1.40 thorpej
666 1.12 cgd /*
667 1.45 chuck * sprintf: print a message to a buffer
668 1.12 cgd */
669 1.45 chuck int
670 1.45 chuck #ifdef __STDC__
671 1.45 chuck sprintf(char *buf, const char *fmt, ...)
672 1.45 chuck #else
673 1.45 chuck sprintf(buf, fmt, va_alist)
674 1.45 chuck char *buf;
675 1.45 chuck const char *cfmt;
676 1.45 chuck va_dcl
677 1.45 chuck #endif
678 1.45 chuck {
679 1.45 chuck int retval;
680 1.12 cgd va_list ap;
681 1.12 cgd
682 1.45 chuck va_start(ap, fmt);
683 1.45 chuck retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
684 1.12 cgd va_end(ap);
685 1.45 chuck *(buf + retval) = 0; /* null terminate */
686 1.45 chuck return(retval);
687 1.55 thorpej }
688 1.55 thorpej
689 1.55 thorpej /*
690 1.55 thorpej * vsprintf: print a message to a buffer [already have va_alist]
691 1.55 thorpej */
692 1.55 thorpej
693 1.55 thorpej int
694 1.55 thorpej vsprintf(buf, fmt, ap)
695 1.55 thorpej char *buf;
696 1.55 thorpej const char *fmt;
697 1.55 thorpej va_list ap;
698 1.55 thorpej {
699 1.55 thorpej int retval;
700 1.55 thorpej
701 1.55 thorpej retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
702 1.55 thorpej *(buf + retval) = 0; /* null terminate */
703 1.55 thorpej return (retval);
704 1.12 cgd }
705 1.12 cgd
706 1.12 cgd /*
707 1.58 msaitoh * snprintf: print a message to a buffer
708 1.58 msaitoh */
709 1.58 msaitoh int
710 1.58 msaitoh #ifdef __STDC__
711 1.58 msaitoh snprintf(char *buf, size_t size, const char *fmt, ...)
712 1.58 msaitoh #else
713 1.58 msaitoh snprintf(buf, size, fmt, va_alist)
714 1.58 msaitoh char *buf;
715 1.58 msaitoh size_t size;
716 1.58 msaitoh const char *cfmt;
717 1.58 msaitoh va_dcl
718 1.58 msaitoh #endif
719 1.58 msaitoh {
720 1.58 msaitoh int retval;
721 1.58 msaitoh va_list ap;
722 1.58 msaitoh char *p;
723 1.58 msaitoh
724 1.58 msaitoh if (size < 1)
725 1.58 msaitoh return (-1);
726 1.58 msaitoh p = buf + size - 1;
727 1.58 msaitoh va_start(ap, fmt);
728 1.58 msaitoh retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
729 1.58 msaitoh va_end(ap);
730 1.58 msaitoh *(p) = 0; /* null terminate */
731 1.58 msaitoh return(retval);
732 1.58 msaitoh }
733 1.58 msaitoh
734 1.58 msaitoh /*
735 1.58 msaitoh * vsnprintf: print a message to a buffer [already have va_alist]
736 1.58 msaitoh */
737 1.58 msaitoh int
738 1.58 msaitoh vsnprintf(buf, size, fmt, ap)
739 1.58 msaitoh char *buf;
740 1.58 msaitoh size_t size;
741 1.58 msaitoh const char *fmt;
742 1.58 msaitoh va_list ap;
743 1.58 msaitoh {
744 1.58 msaitoh int retval;
745 1.58 msaitoh char *p;
746 1.58 msaitoh
747 1.58 msaitoh if (size < 1)
748 1.58 msaitoh return (-1);
749 1.58 msaitoh p = buf + size - 1;
750 1.58 msaitoh retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
751 1.58 msaitoh *(p) = 0; /* null terminate */
752 1.58 msaitoh return(retval);
753 1.58 msaitoh }
754 1.58 msaitoh
755 1.58 msaitoh /*
756 1.76 tv * bitmask_snprintf: print an interpreted bitmask to a buffer
757 1.45 chuck *
758 1.45 chuck * => returns pointer to the buffer
759 1.37 thorpej */
760 1.37 thorpej char *
761 1.51 pk bitmask_snprintf(val, p, buf, buflen)
762 1.51 pk u_quad_t val;
763 1.37 thorpej const char *p;
764 1.37 thorpej char *buf;
765 1.37 thorpej size_t buflen;
766 1.37 thorpej {
767 1.37 thorpej char *bp, *q;
768 1.37 thorpej size_t left;
769 1.51 pk char *sbase, snbuf[KPRINTF_BUFSIZE];
770 1.51 pk int base, bit, ch, len, sep;
771 1.51 pk u_quad_t field;
772 1.37 thorpej
773 1.37 thorpej bp = buf;
774 1.50 perry memset(buf, 0, buflen);
775 1.37 thorpej
776 1.37 thorpej /*
777 1.37 thorpej * Always leave room for the trailing NULL.
778 1.37 thorpej */
779 1.37 thorpej left = buflen - 1;
780 1.37 thorpej
781 1.37 thorpej /*
782 1.37 thorpej * Print the value into the buffer. Abort if there's not
783 1.37 thorpej * enough room.
784 1.37 thorpej */
785 1.45 chuck if (buflen < KPRINTF_BUFSIZE)
786 1.37 thorpej return (buf);
787 1.37 thorpej
788 1.51 pk ch = *p++;
789 1.51 pk base = ch != '\177' ? ch : *p++;
790 1.51 pk sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
791 1.51 pk if (sbase == 0)
792 1.51 pk return (buf); /* punt if not oct, dec, or hex */
793 1.45 chuck
794 1.76 tv snprintf(snbuf, sizeof(snbuf), sbase, val);
795 1.45 chuck for (q = snbuf ; *q ; q++) {
796 1.45 chuck *bp++ = *q;
797 1.37 thorpej left--;
798 1.37 thorpej }
799 1.37 thorpej
800 1.37 thorpej /*
801 1.65 sommerfe * If the value we printed was 0 and we're using the old-style format,
802 1.65 sommerfe * or if we don't have room for "<x>", we're done.
803 1.37 thorpej */
804 1.65 sommerfe if (((val == 0) && (ch != '\177')) || left < 3)
805 1.37 thorpej return (buf);
806 1.37 thorpej
807 1.76.2.6 nathanw #define PUTBYTE(b, c, l) do { \
808 1.37 thorpej *(b)++ = (c); \
809 1.37 thorpej if (--(l) == 0) \
810 1.76.2.6 nathanw goto out; \
811 1.76.2.6 nathanw } while (0)
812 1.51 pk #define PUTSTR(b, p, l) do { \
813 1.51 pk int c; \
814 1.51 pk while ((c = *(p)++) != 0) { \
815 1.51 pk *(b)++ = c; \
816 1.51 pk if (--(l) == 0) \
817 1.51 pk goto out; \
818 1.51 pk } \
819 1.51 pk } while (0)
820 1.37 thorpej
821 1.51 pk /*
822 1.76 tv * Chris Torek's new bitmask format is identified by a leading \177
823 1.51 pk */
824 1.51 pk sep = '<';
825 1.51 pk if (ch != '\177') {
826 1.76 tv /* old (standard) format. */
827 1.51 pk for (;(bit = *p++) != 0;) {
828 1.51 pk if (val & (1 << (bit - 1))) {
829 1.51 pk PUTBYTE(bp, sep, left);
830 1.51 pk for (; (ch = *p) > ' '; ++p) {
831 1.51 pk PUTBYTE(bp, ch, left);
832 1.37 thorpej }
833 1.51 pk sep = ',';
834 1.51 pk } else
835 1.51 pk for (; *p > ' '; ++p)
836 1.51 pk continue;
837 1.51 pk }
838 1.51 pk } else {
839 1.76 tv /* new quad-capable format; also does fields. */
840 1.51 pk field = val;
841 1.51 pk while ((ch = *p++) != '\0') {
842 1.51 pk bit = *p++; /* now 0-origin */
843 1.51 pk switch (ch) {
844 1.51 pk case 'b':
845 1.51 pk if (((u_int)(val >> bit) & 1) == 0)
846 1.51 pk goto skip;
847 1.51 pk PUTBYTE(bp, sep, left);
848 1.51 pk PUTSTR(bp, p, left);
849 1.51 pk sep = ',';
850 1.51 pk break;
851 1.51 pk case 'f':
852 1.51 pk case 'F':
853 1.51 pk len = *p++; /* field length */
854 1.51 pk field = (val >> bit) & ((1ULL << len) - 1);
855 1.51 pk if (ch == 'F') /* just extract */
856 1.51 pk break;
857 1.51 pk PUTBYTE(bp, sep, left);
858 1.51 pk sep = ',';
859 1.51 pk PUTSTR(bp, p, left);
860 1.51 pk PUTBYTE(bp, '=', left);
861 1.51 pk sprintf(snbuf, sbase, field);
862 1.51 pk q = snbuf; PUTSTR(bp, q, left);
863 1.51 pk break;
864 1.51 pk case '=':
865 1.51 pk case ':':
866 1.51 pk /*
867 1.51 pk * Here "bit" is actually a value instead,
868 1.51 pk * to be compared against the last field.
869 1.51 pk * This only works for values in [0..255],
870 1.51 pk * of course.
871 1.51 pk */
872 1.51 pk if ((int)field != bit)
873 1.51 pk goto skip;
874 1.51 pk if (ch == '=')
875 1.51 pk PUTBYTE(bp, '=', left);
876 1.51 pk PUTSTR(bp, p, left);
877 1.51 pk break;
878 1.51 pk default:
879 1.51 pk skip:
880 1.51 pk while (*p++ != '\0')
881 1.51 pk continue;
882 1.51 pk break;
883 1.51 pk }
884 1.51 pk }
885 1.37 thorpej }
886 1.51 pk if (sep != '<')
887 1.51 pk PUTBYTE(bp, '>', left);
888 1.51 pk
889 1.51 pk out:
890 1.51 pk return (buf);
891 1.37 thorpej
892 1.37 thorpej #undef PUTBYTE
893 1.51 pk #undef PUTSTR
894 1.45 chuck }
895 1.45 chuck
896 1.45 chuck /*
897 1.45 chuck * kprintf: scaled down version of printf(3).
898 1.45 chuck *
899 1.45 chuck * this version based on vfprintf() from libc which was derived from
900 1.45 chuck * software contributed to Berkeley by Chris Torek.
901 1.45 chuck *
902 1.76 tv * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
903 1.45 chuck */
904 1.45 chuck
905 1.45 chuck /*
906 1.45 chuck * macros for converting digits to letters and vice versa
907 1.45 chuck */
908 1.45 chuck #define to_digit(c) ((c) - '0')
909 1.45 chuck #define is_digit(c) ((unsigned)to_digit(c) <= 9)
910 1.45 chuck #define to_char(n) ((n) + '0')
911 1.45 chuck
912 1.45 chuck /*
913 1.45 chuck * flags used during conversion.
914 1.45 chuck */
915 1.45 chuck #define ALT 0x001 /* alternate form */
916 1.45 chuck #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
917 1.45 chuck #define LADJUST 0x004 /* left adjustment */
918 1.45 chuck #define LONGDBL 0x008 /* long double; unimplemented */
919 1.45 chuck #define LONGINT 0x010 /* long integer */
920 1.45 chuck #define QUADINT 0x020 /* quad integer */
921 1.45 chuck #define SHORTINT 0x040 /* short integer */
922 1.76.2.3 nathanw #define MAXINT 0x080 /* intmax_t */
923 1.76.2.3 nathanw #define PTRINT 0x100 /* intptr_t */
924 1.76.2.3 nathanw #define SIZEINT 0x200 /* size_t */
925 1.76.2.3 nathanw #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
926 1.76.2.3 nathanw #define FPT 0x800 /* Floating point number */
927 1.45 chuck
928 1.45 chuck /*
929 1.45 chuck * To extend shorts properly, we need both signed and unsigned
930 1.45 chuck * argument extraction methods.
931 1.45 chuck */
932 1.45 chuck #define SARG() \
933 1.76.2.3 nathanw (flags&MAXINT ? va_arg(ap, intmax_t) : \
934 1.76.2.3 nathanw flags&PTRINT ? va_arg(ap, intptr_t) : \
935 1.76.2.3 nathanw flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
936 1.76.2.3 nathanw flags&QUADINT ? va_arg(ap, quad_t) : \
937 1.45 chuck flags&LONGINT ? va_arg(ap, long) : \
938 1.45 chuck flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
939 1.45 chuck (long)va_arg(ap, int))
940 1.45 chuck #define UARG() \
941 1.76.2.3 nathanw (flags&MAXINT ? va_arg(ap, uintmax_t) : \
942 1.76.2.3 nathanw flags&PTRINT ? va_arg(ap, uintptr_t) : \
943 1.76.2.3 nathanw flags&SIZEINT ? va_arg(ap, size_t) : \
944 1.76.2.3 nathanw flags&QUADINT ? va_arg(ap, u_quad_t) : \
945 1.45 chuck flags&LONGINT ? va_arg(ap, u_long) : \
946 1.45 chuck flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
947 1.45 chuck (u_long)va_arg(ap, u_int))
948 1.45 chuck
949 1.60 explorer #define KPRINTF_PUTCHAR(C) { \
950 1.60 explorer if (oflags == TOBUFONLY) { \
951 1.60 explorer if ((vp != NULL) && (sbuf == tailp)) { \
952 1.60 explorer ret += 1; /* indicate error */ \
953 1.60 explorer goto overflow; \
954 1.60 explorer } \
955 1.60 explorer *sbuf++ = (C); \
956 1.60 explorer } else { \
957 1.60 explorer putchar((C), oflags, (struct tty *)vp); \
958 1.60 explorer } \
959 1.58 msaitoh }
960 1.45 chuck
961 1.54 thorpej /*
962 1.54 thorpej * Guts of kernel printf. Note, we already expect to be in a mutex!
963 1.54 thorpej */
964 1.54 thorpej static int
965 1.58 msaitoh kprintf(fmt0, oflags, vp, sbuf, ap)
966 1.45 chuck const char *fmt0;
967 1.45 chuck int oflags;
968 1.58 msaitoh void *vp;
969 1.45 chuck char *sbuf;
970 1.45 chuck va_list ap;
971 1.45 chuck {
972 1.45 chuck char *fmt; /* format string */
973 1.45 chuck int ch; /* character from fmt */
974 1.45 chuck int n; /* handy integer (short term usage) */
975 1.45 chuck char *cp; /* handy char pointer (short term usage) */
976 1.45 chuck int flags; /* flags as above */
977 1.45 chuck int ret; /* return value accumulator */
978 1.45 chuck int width; /* width from format (%8d), or 0 */
979 1.45 chuck int prec; /* precision from format (%.3d), or -1 */
980 1.45 chuck char sign; /* sign prefix (' ', '+', '-', or \0) */
981 1.45 chuck
982 1.45 chuck u_quad_t _uquad; /* integer arguments %[diouxX] */
983 1.45 chuck enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
984 1.45 chuck int dprec; /* a copy of prec if [diouxX], 0 otherwise */
985 1.45 chuck int realsz; /* field size expanded by dprec */
986 1.45 chuck int size; /* size of converted field or string */
987 1.45 chuck char *xdigs; /* digits for [xX] conversion */
988 1.45 chuck char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
989 1.61 explorer char *tailp; /* tail pointer for snprintf */
990 1.58 msaitoh
991 1.61 explorer tailp = NULL; /* XXX: shutup gcc */
992 1.60 explorer if (oflags == TOBUFONLY && (vp != NULL))
993 1.60 explorer tailp = *(char **)vp;
994 1.45 chuck
995 1.45 chuck cp = NULL; /* XXX: shutup gcc */
996 1.45 chuck size = 0; /* XXX: shutup gcc */
997 1.45 chuck
998 1.45 chuck fmt = (char *)fmt0;
999 1.45 chuck ret = 0;
1000 1.45 chuck
1001 1.45 chuck xdigs = NULL; /* XXX: shut up gcc warning */
1002 1.45 chuck
1003 1.45 chuck /*
1004 1.45 chuck * Scan the format for conversions (`%' character).
1005 1.45 chuck */
1006 1.45 chuck for (;;) {
1007 1.45 chuck while (*fmt != '%' && *fmt) {
1008 1.58 msaitoh ret++;
1009 1.45 chuck KPRINTF_PUTCHAR(*fmt++);
1010 1.45 chuck }
1011 1.45 chuck if (*fmt == 0)
1012 1.45 chuck goto done;
1013 1.45 chuck
1014 1.45 chuck fmt++; /* skip over '%' */
1015 1.45 chuck
1016 1.45 chuck flags = 0;
1017 1.45 chuck dprec = 0;
1018 1.45 chuck width = 0;
1019 1.45 chuck prec = -1;
1020 1.45 chuck sign = '\0';
1021 1.45 chuck
1022 1.45 chuck rflag: ch = *fmt++;
1023 1.45 chuck reswitch: switch (ch) {
1024 1.45 chuck case ' ':
1025 1.45 chuck /*
1026 1.45 chuck * ``If the space and + flags both appear, the space
1027 1.45 chuck * flag will be ignored.''
1028 1.45 chuck * -- ANSI X3J11
1029 1.45 chuck */
1030 1.45 chuck if (!sign)
1031 1.45 chuck sign = ' ';
1032 1.45 chuck goto rflag;
1033 1.45 chuck case '#':
1034 1.45 chuck flags |= ALT;
1035 1.45 chuck goto rflag;
1036 1.45 chuck case '*':
1037 1.45 chuck /*
1038 1.45 chuck * ``A negative field width argument is taken as a
1039 1.45 chuck * - flag followed by a positive field width.''
1040 1.45 chuck * -- ANSI X3J11
1041 1.45 chuck * They don't exclude field widths read from args.
1042 1.45 chuck */
1043 1.45 chuck if ((width = va_arg(ap, int)) >= 0)
1044 1.45 chuck goto rflag;
1045 1.45 chuck width = -width;
1046 1.45 chuck /* FALLTHROUGH */
1047 1.45 chuck case '-':
1048 1.45 chuck flags |= LADJUST;
1049 1.45 chuck goto rflag;
1050 1.45 chuck case '+':
1051 1.45 chuck sign = '+';
1052 1.45 chuck goto rflag;
1053 1.45 chuck case '.':
1054 1.45 chuck if ((ch = *fmt++) == '*') {
1055 1.45 chuck n = va_arg(ap, int);
1056 1.45 chuck prec = n < 0 ? -1 : n;
1057 1.45 chuck goto rflag;
1058 1.45 chuck }
1059 1.45 chuck n = 0;
1060 1.45 chuck while (is_digit(ch)) {
1061 1.45 chuck n = 10 * n + to_digit(ch);
1062 1.45 chuck ch = *fmt++;
1063 1.45 chuck }
1064 1.45 chuck prec = n < 0 ? -1 : n;
1065 1.45 chuck goto reswitch;
1066 1.45 chuck case '0':
1067 1.45 chuck /*
1068 1.45 chuck * ``Note that 0 is taken as a flag, not as the
1069 1.45 chuck * beginning of a field width.''
1070 1.45 chuck * -- ANSI X3J11
1071 1.45 chuck */
1072 1.45 chuck flags |= ZEROPAD;
1073 1.45 chuck goto rflag;
1074 1.45 chuck case '1': case '2': case '3': case '4':
1075 1.45 chuck case '5': case '6': case '7': case '8': case '9':
1076 1.45 chuck n = 0;
1077 1.45 chuck do {
1078 1.45 chuck n = 10 * n + to_digit(ch);
1079 1.45 chuck ch = *fmt++;
1080 1.45 chuck } while (is_digit(ch));
1081 1.45 chuck width = n;
1082 1.45 chuck goto reswitch;
1083 1.45 chuck case 'h':
1084 1.45 chuck flags |= SHORTINT;
1085 1.45 chuck goto rflag;
1086 1.76.2.3 nathanw case 'j':
1087 1.76.2.3 nathanw flags |= MAXINT;
1088 1.76.2.3 nathanw goto rflag;
1089 1.45 chuck case 'l':
1090 1.45 chuck if (*fmt == 'l') {
1091 1.45 chuck fmt++;
1092 1.45 chuck flags |= QUADINT;
1093 1.45 chuck } else {
1094 1.45 chuck flags |= LONGINT;
1095 1.45 chuck }
1096 1.45 chuck goto rflag;
1097 1.45 chuck case 'q':
1098 1.45 chuck flags |= QUADINT;
1099 1.45 chuck goto rflag;
1100 1.76.2.3 nathanw case 't':
1101 1.76.2.3 nathanw flags |= PTRINT;
1102 1.76.2.3 nathanw goto rflag;
1103 1.76.2.3 nathanw case 'z':
1104 1.76.2.3 nathanw flags |= SIZEINT;
1105 1.76.2.3 nathanw goto rflag;
1106 1.45 chuck case 'c':
1107 1.45 chuck *(cp = buf) = va_arg(ap, int);
1108 1.45 chuck size = 1;
1109 1.45 chuck sign = '\0';
1110 1.45 chuck break;
1111 1.45 chuck case 'D':
1112 1.45 chuck flags |= LONGINT;
1113 1.45 chuck /*FALLTHROUGH*/
1114 1.45 chuck case 'd':
1115 1.45 chuck case 'i':
1116 1.45 chuck _uquad = SARG();
1117 1.45 chuck if ((quad_t)_uquad < 0) {
1118 1.45 chuck _uquad = -_uquad;
1119 1.45 chuck sign = '-';
1120 1.45 chuck }
1121 1.45 chuck base = DEC;
1122 1.45 chuck goto number;
1123 1.45 chuck case 'n':
1124 1.76.2.3 nathanw if (flags & MAXINT)
1125 1.76.2.3 nathanw *va_arg(ap, intmax_t *) = ret;
1126 1.76.2.3 nathanw else if (flags & PTRINT)
1127 1.76.2.3 nathanw *va_arg(ap, intptr_t *) = ret;
1128 1.76.2.3 nathanw else if (flags & SIZEINT)
1129 1.76.2.3 nathanw *va_arg(ap, ssize_t *) = ret;
1130 1.76.2.3 nathanw else if (flags & QUADINT)
1131 1.45 chuck *va_arg(ap, quad_t *) = ret;
1132 1.45 chuck else if (flags & LONGINT)
1133 1.45 chuck *va_arg(ap, long *) = ret;
1134 1.45 chuck else if (flags & SHORTINT)
1135 1.45 chuck *va_arg(ap, short *) = ret;
1136 1.45 chuck else
1137 1.45 chuck *va_arg(ap, int *) = ret;
1138 1.45 chuck continue; /* no output */
1139 1.45 chuck case 'O':
1140 1.45 chuck flags |= LONGINT;
1141 1.45 chuck /*FALLTHROUGH*/
1142 1.45 chuck case 'o':
1143 1.45 chuck _uquad = UARG();
1144 1.45 chuck base = OCT;
1145 1.45 chuck goto nosign;
1146 1.45 chuck case 'p':
1147 1.45 chuck /*
1148 1.45 chuck * ``The argument shall be a pointer to void. The
1149 1.45 chuck * value of the pointer is converted to a sequence
1150 1.45 chuck * of printable characters, in an implementation-
1151 1.45 chuck * defined manner.''
1152 1.45 chuck * -- ANSI X3J11
1153 1.45 chuck */
1154 1.45 chuck /* NOSTRICT */
1155 1.45 chuck _uquad = (u_long)va_arg(ap, void *);
1156 1.45 chuck base = HEX;
1157 1.45 chuck xdigs = "0123456789abcdef";
1158 1.45 chuck flags |= HEXPREFIX;
1159 1.45 chuck ch = 'x';
1160 1.45 chuck goto nosign;
1161 1.45 chuck case 's':
1162 1.45 chuck if ((cp = va_arg(ap, char *)) == NULL)
1163 1.45 chuck cp = "(null)";
1164 1.45 chuck if (prec >= 0) {
1165 1.45 chuck /*
1166 1.45 chuck * can't use strlen; can only look for the
1167 1.45 chuck * NUL in the first `prec' characters, and
1168 1.45 chuck * strlen() will go further.
1169 1.45 chuck */
1170 1.45 chuck char *p = memchr(cp, 0, prec);
1171 1.45 chuck
1172 1.45 chuck if (p != NULL) {
1173 1.45 chuck size = p - cp;
1174 1.45 chuck if (size > prec)
1175 1.45 chuck size = prec;
1176 1.45 chuck } else
1177 1.45 chuck size = prec;
1178 1.45 chuck } else
1179 1.45 chuck size = strlen(cp);
1180 1.45 chuck sign = '\0';
1181 1.45 chuck break;
1182 1.45 chuck case 'U':
1183 1.45 chuck flags |= LONGINT;
1184 1.45 chuck /*FALLTHROUGH*/
1185 1.45 chuck case 'u':
1186 1.45 chuck _uquad = UARG();
1187 1.45 chuck base = DEC;
1188 1.45 chuck goto nosign;
1189 1.45 chuck case 'X':
1190 1.45 chuck xdigs = "0123456789ABCDEF";
1191 1.45 chuck goto hex;
1192 1.45 chuck case 'x':
1193 1.45 chuck xdigs = "0123456789abcdef";
1194 1.45 chuck hex: _uquad = UARG();
1195 1.45 chuck base = HEX;
1196 1.45 chuck /* leading 0x/X only if non-zero */
1197 1.45 chuck if (flags & ALT && _uquad != 0)
1198 1.45 chuck flags |= HEXPREFIX;
1199 1.45 chuck
1200 1.45 chuck /* unsigned conversions */
1201 1.45 chuck nosign: sign = '\0';
1202 1.45 chuck /*
1203 1.45 chuck * ``... diouXx conversions ... if a precision is
1204 1.45 chuck * specified, the 0 flag will be ignored.''
1205 1.45 chuck * -- ANSI X3J11
1206 1.45 chuck */
1207 1.45 chuck number: if ((dprec = prec) >= 0)
1208 1.45 chuck flags &= ~ZEROPAD;
1209 1.45 chuck
1210 1.45 chuck /*
1211 1.45 chuck * ``The result of converting a zero value with an
1212 1.45 chuck * explicit precision of zero is no characters.''
1213 1.45 chuck * -- ANSI X3J11
1214 1.45 chuck */
1215 1.45 chuck cp = buf + KPRINTF_BUFSIZE;
1216 1.45 chuck if (_uquad != 0 || prec != 0) {
1217 1.45 chuck /*
1218 1.45 chuck * Unsigned mod is hard, and unsigned mod
1219 1.45 chuck * by a constant is easier than that by
1220 1.45 chuck * a variable; hence this switch.
1221 1.45 chuck */
1222 1.45 chuck switch (base) {
1223 1.45 chuck case OCT:
1224 1.45 chuck do {
1225 1.45 chuck *--cp = to_char(_uquad & 7);
1226 1.45 chuck _uquad >>= 3;
1227 1.45 chuck } while (_uquad);
1228 1.45 chuck /* handle octal leading 0 */
1229 1.45 chuck if (flags & ALT && *cp != '0')
1230 1.45 chuck *--cp = '0';
1231 1.45 chuck break;
1232 1.45 chuck
1233 1.45 chuck case DEC:
1234 1.45 chuck /* many numbers are 1 digit */
1235 1.45 chuck while (_uquad >= 10) {
1236 1.45 chuck *--cp = to_char(_uquad % 10);
1237 1.45 chuck _uquad /= 10;
1238 1.45 chuck }
1239 1.45 chuck *--cp = to_char(_uquad);
1240 1.45 chuck break;
1241 1.45 chuck
1242 1.45 chuck case HEX:
1243 1.45 chuck do {
1244 1.45 chuck *--cp = xdigs[_uquad & 15];
1245 1.45 chuck _uquad >>= 4;
1246 1.45 chuck } while (_uquad);
1247 1.45 chuck break;
1248 1.45 chuck
1249 1.45 chuck default:
1250 1.45 chuck cp = "bug in kprintf: bad base";
1251 1.45 chuck size = strlen(cp);
1252 1.45 chuck goto skipsize;
1253 1.45 chuck }
1254 1.45 chuck }
1255 1.45 chuck size = buf + KPRINTF_BUFSIZE - cp;
1256 1.45 chuck skipsize:
1257 1.45 chuck break;
1258 1.45 chuck default: /* "%?" prints ?, unless ? is NUL */
1259 1.45 chuck if (ch == '\0')
1260 1.45 chuck goto done;
1261 1.45 chuck /* pretend it was %c with argument ch */
1262 1.45 chuck cp = buf;
1263 1.45 chuck *cp = ch;
1264 1.45 chuck size = 1;
1265 1.45 chuck sign = '\0';
1266 1.45 chuck break;
1267 1.45 chuck }
1268 1.45 chuck
1269 1.45 chuck /*
1270 1.45 chuck * All reasonable formats wind up here. At this point, `cp'
1271 1.45 chuck * points to a string which (if not flags&LADJUST) should be
1272 1.45 chuck * padded out to `width' places. If flags&ZEROPAD, it should
1273 1.45 chuck * first be prefixed by any sign or other prefix; otherwise,
1274 1.45 chuck * it should be blank padded before the prefix is emitted.
1275 1.45 chuck * After any left-hand padding and prefixing, emit zeroes
1276 1.45 chuck * required by a decimal [diouxX] precision, then print the
1277 1.45 chuck * string proper, then emit zeroes required by any leftover
1278 1.45 chuck * floating precision; finally, if LADJUST, pad with blanks.
1279 1.45 chuck *
1280 1.45 chuck * Compute actual size, so we know how much to pad.
1281 1.45 chuck * size excludes decimal prec; realsz includes it.
1282 1.45 chuck */
1283 1.45 chuck realsz = dprec > size ? dprec : size;
1284 1.45 chuck if (sign)
1285 1.45 chuck realsz++;
1286 1.45 chuck else if (flags & HEXPREFIX)
1287 1.45 chuck realsz+= 2;
1288 1.45 chuck
1289 1.58 msaitoh /* adjust ret */
1290 1.58 msaitoh ret += width > realsz ? width : realsz;
1291 1.58 msaitoh
1292 1.45 chuck /* right-adjusting blank padding */
1293 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == 0) {
1294 1.45 chuck n = width - realsz;
1295 1.45 chuck while (n-- > 0)
1296 1.45 chuck KPRINTF_PUTCHAR(' ');
1297 1.45 chuck }
1298 1.45 chuck
1299 1.45 chuck /* prefix */
1300 1.45 chuck if (sign) {
1301 1.45 chuck KPRINTF_PUTCHAR(sign);
1302 1.45 chuck } else if (flags & HEXPREFIX) {
1303 1.45 chuck KPRINTF_PUTCHAR('0');
1304 1.45 chuck KPRINTF_PUTCHAR(ch);
1305 1.45 chuck }
1306 1.45 chuck
1307 1.45 chuck /* right-adjusting zero padding */
1308 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1309 1.45 chuck n = width - realsz;
1310 1.45 chuck while (n-- > 0)
1311 1.45 chuck KPRINTF_PUTCHAR('0');
1312 1.45 chuck }
1313 1.45 chuck
1314 1.45 chuck /* leading zeroes from decimal precision */
1315 1.45 chuck n = dprec - size;
1316 1.45 chuck while (n-- > 0)
1317 1.45 chuck KPRINTF_PUTCHAR('0');
1318 1.45 chuck
1319 1.45 chuck /* the string or number proper */
1320 1.45 chuck while (size--)
1321 1.45 chuck KPRINTF_PUTCHAR(*cp++);
1322 1.45 chuck /* left-adjusting padding (always blank) */
1323 1.45 chuck if (flags & LADJUST) {
1324 1.45 chuck n = width - realsz;
1325 1.45 chuck while (n-- > 0)
1326 1.45 chuck KPRINTF_PUTCHAR(' ');
1327 1.45 chuck }
1328 1.58 msaitoh }
1329 1.45 chuck
1330 1.45 chuck done:
1331 1.58 msaitoh if ((oflags == TOBUFONLY) && (vp != NULL))
1332 1.58 msaitoh *(char **)vp = sbuf;
1333 1.58 msaitoh overflow:
1334 1.45 chuck return (ret);
1335 1.45 chuck /* NOTREACHED */
1336 1.12 cgd }
1337