db_output.c revision 1.10 1 /* $NetBSD: db_output.c,v 1.10 1996/02/05 01:57:08 christos Exp $ */
2
3 /*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /*
30 * Printf and character output for debugger.
31 */
32 #include <sys/param.h>
33
34 #include <machine/stdarg.h>
35
36 #include <dev/cons.h>
37
38 #include <machine/db_machdep.h>
39
40 #include <ddb/db_command.h>
41 #include <ddb/db_output.h>
42 #include <ddb/db_interface.h>
43 #include <ddb/db_sym.h>
44 #include <ddb/db_extern.h>
45
46 /*
47 * Character output - tracks position in line.
48 * To do this correctly, we should know how wide
49 * the output device is - then we could zero
50 * the line position when the output device wraps
51 * around to the start of the next line.
52 *
53 * Instead, we count the number of spaces printed
54 * since the last printing character so that we
55 * don't print trailing spaces. This avoids most
56 * of the wraparounds.
57 */
58
59 #ifndef DB_MAX_LINE
60 #define DB_MAX_LINE 24 /* maximum line */
61 #define DB_MAX_WIDTH 80 /* maximum width */
62 #endif DB_MAX_LINE
63
64 #define DB_MIN_MAX_WIDTH 20 /* minimum max width */
65 #define DB_MIN_MAX_LINE 3 /* minimum max line */
66 #define CTRL(c) ((c) & 0xff)
67
68 int db_output_position = 0; /* output column */
69 int db_output_line = 0; /* output line number */
70 int db_last_non_space = 0; /* last non-space character */
71 int db_tab_stop_width = 8; /* how wide are tab stops? */
72 #define NEXT_TAB(i) \
73 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
74 int db_max_line = DB_MAX_LINE; /* output max lines */
75 int db_max_width = DB_MAX_WIDTH; /* output line width */
76
77 static void db_more __P((void));
78 static char *db_ksprintn __P((u_long, int, int *));
79 static void db_printf_guts __P((const char *, va_list));
80
81 /*
82 * Force pending whitespace.
83 */
84 void
85 db_force_whitespace()
86 {
87 register int last_print, next_tab;
88
89 last_print = db_last_non_space;
90 while (last_print < db_output_position) {
91 next_tab = NEXT_TAB(last_print);
92 if (next_tab <= db_output_position) {
93 while (last_print < next_tab) { /* DON'T send a tab!!! */
94 cnputc(' ');
95 last_print++;
96 }
97 }
98 else {
99 cnputc(' ');
100 last_print++;
101 }
102 }
103 db_last_non_space = db_output_position;
104 }
105
106 static void
107 db_more()
108 {
109 register char *p;
110 int quit_output = 0;
111
112 for (p = "--db_more--"; *p; p++)
113 cnputc(*p);
114 switch(cngetc()) {
115 case ' ':
116 db_output_line = 0;
117 break;
118 case 'q':
119 case CTRL('c'):
120 db_output_line = 0;
121 quit_output = 1;
122 break;
123 default:
124 db_output_line--;
125 break;
126 }
127 p = "\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b";
128 while (*p)
129 cnputc(*p++);
130 if (quit_output) {
131 db_error(0);
132 /* NOTREACHED */
133 }
134 }
135
136 /*
137 * Output character. Buffer whitespace.
138 */
139 void
140 db_putchar(c)
141 int c; /* character to output */
142 {
143 if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
144 db_more();
145 if (c > ' ' && c <= '~') {
146 /*
147 * Printing character.
148 * If we have spaces to print, print them first.
149 * Use tabs if possible.
150 */
151 db_force_whitespace();
152 cnputc(c);
153 db_output_position++;
154 if (db_max_width >= DB_MIN_MAX_WIDTH
155 && db_output_position >= db_max_width-1) {
156 /* auto new line */
157 cnputc('\n');
158 db_output_position = 0;
159 db_last_non_space = 0;
160 db_output_line++;
161 }
162 db_last_non_space = db_output_position;
163 }
164 else if (c == '\n') {
165 /* Return */
166 cnputc(c);
167 db_output_position = 0;
168 db_last_non_space = 0;
169 db_output_line++;
170 db_check_interrupt();
171 }
172 else if (c == '\t') {
173 /* assume tabs every 8 positions */
174 db_output_position = NEXT_TAB(db_output_position);
175 }
176 else if (c == ' ') {
177 /* space */
178 db_output_position++;
179 }
180 else if (c == '\007') {
181 /* bell */
182 cnputc(c);
183 }
184 /* other characters are assumed non-printing */
185 }
186
187 /*
188 * Return output position
189 */
190 int
191 db_print_position()
192 {
193 return (db_output_position);
194 }
195
196 /*
197 * Printing
198 */
199 extern int db_radix;
200
201 /*VARARGS1*/
202 void
203 #if __STDC__
204 db_printf(const char *fmt, ...)
205 #else
206 db_printf(fmt, va_alist)
207 /*###207 [cc] warning: type of `va_alist' defaults to `int'%%%*/
208 const char *fmt;
209 /*###208 [cc] parse error before `va_dcl'%%%*/
210 va_dcl
211 #endif
212 {
213 va_list listp;
214 va_start(listp, fmt);
215 db_printf_guts (fmt, listp);
216 va_end(listp);
217 }
218
219 /* alternate name */
220
221 /*VARARGS1*/
222 void
223 #if __STDC__
224 kdbprintf(const char *fmt, ...)
225 #else
226 kdbprintf(fmt, va_alist)
227 char *fmt;
228 va_dcl
229 #endif
230 {
231 va_list listp;
232 va_start(listp, fmt);
233 db_printf_guts (fmt, listp);
234 va_end(listp);
235 }
236
237 /*
238 * End line if too long.
239 */
240 void
241 db_end_line()
242 {
243 if (db_output_position >= db_max_width)
244 db_printf("\n");
245 }
246
247 /*
248 * Put a number (base <= 16) in a buffer in reverse order; return an
249 * optional length and a pointer to the NULL terminated (preceded?)
250 * buffer.
251 */
252 static char *
253 db_ksprintn(ul, base, lenp)
254 register u_long ul;
255 register int base, *lenp;
256 { /* A long in base 8, plus NULL. */
257 static char buf[sizeof(long) * NBBY / 3 + 2];
258 register char *p;
259
260 p = buf;
261 do {
262 *++p = "0123456789abcdef"[ul % base];
263 } while (ul /= base);
264 if (lenp)
265 *lenp = p - buf;
266 return (p);
267 }
268
269 static void
270 db_printf_guts(fmt, ap)
271 register const char *fmt;
272 va_list ap;
273 {
274 register char *p;
275 register int ch, n;
276 u_long ul;
277 int base, lflag, tmp, width;
278 char padc;
279 int ladjust;
280 int sharpflag;
281 int neg;
282
283 for (;;) {
284 padc = ' ';
285 width = 0;
286 while ((ch = *(u_char *)fmt++) != '%') {
287 if (ch == '\0')
288 return;
289 db_putchar(ch);
290 }
291 lflag = 0;
292 ladjust = 0;
293 sharpflag = 0;
294 neg = 0;
295 reswitch: switch (ch = *(u_char *)fmt++) {
296 case '0':
297 padc = '0';
298 goto reswitch;
299 case '1': case '2': case '3': case '4':
300 case '5': case '6': case '7': case '8': case '9':
301 for (width = 0;; ++fmt) {
302 width = width * 10 + ch - '0';
303 ch = *fmt;
304 if (ch < '0' || ch > '9')
305 break;
306 }
307 goto reswitch;
308 case 'l':
309 lflag = 1;
310 goto reswitch;
311 case '-':
312 ladjust = 1;
313 goto reswitch;
314 case '#':
315 sharpflag = 1;
316 goto reswitch;
317 case 'b':
318 ul = va_arg(ap, int);
319 p = va_arg(ap, char *);
320 for (p = db_ksprintn(ul, *p++, NULL);
321 (ch = *p--) !='\0';)
322 db_putchar(ch);
323
324 if (!ul)
325 break;
326
327 for (tmp = 0; (n = *p++) != '\0';) {
328 if (ul & (1 << (n - 1))) {
329 db_putchar(tmp ? ',' : '<');
330 for (; (n = *p) > ' '; ++p)
331 db_putchar(n);
332 tmp = 1;
333 } else
334 for (; *p > ' '; ++p);
335 }
336 if (tmp)
337 db_putchar('>');
338 break;
339 case '*':
340 width = va_arg (ap, int);
341 if (width < 0) {
342 ladjust = !ladjust;
343 width = -width;
344 }
345 goto reswitch;
346 case 'c':
347 db_putchar(va_arg(ap, int));
348 break;
349 case 's':
350 p = va_arg(ap, char *);
351 width -= strlen (p);
352 if (!ladjust && width > 0)
353 while (width--)
354 db_putchar (padc);
355 while ((ch = *p++) != '\0')
356 db_putchar(ch);
357 if (ladjust && width > 0)
358 while (width--)
359 db_putchar (padc);
360 break;
361 case 'r':
362 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
363 if ((long)ul < 0) {
364 neg = 1;
365 ul = -(long)ul;
366 }
367 base = db_radix;
368 if (base < 8 || base > 16)
369 base = 10;
370 goto number;
371 case 'n':
372 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
373 base = db_radix;
374 if (base < 8 || base > 16)
375 base = 10;
376 goto number;
377 case 'd':
378 ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
379 if ((long)ul < 0) {
380 neg = 1;
381 ul = -(long)ul;
382 }
383 base = 10;
384 goto number;
385 case 'o':
386 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
387 base = 8;
388 goto number;
389 case 'u':
390 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
391 base = 10;
392 goto number;
393 case 'z':
394 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
395 if ((long)ul < 0) {
396 neg = 1;
397 ul = -(long)ul;
398 }
399 base = 16;
400 goto number;
401 case 'x':
402 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
403 base = 16;
404 number: p = (char *)db_ksprintn(ul, base, &tmp);
405 if (sharpflag && ul != 0) {
406 if (base == 8)
407 tmp++;
408 else if (base == 16)
409 tmp += 2;
410 }
411 if (neg)
412 tmp++;
413
414 if (!ladjust && width && (width -= tmp) > 0)
415 while (width--)
416 db_putchar(padc);
417 if (neg)
418 db_putchar ('-');
419 if (sharpflag && ul != 0) {
420 if (base == 8) {
421 db_putchar ('0');
422 } else if (base == 16) {
423 db_putchar ('0');
424 db_putchar ('x');
425 }
426 }
427 if (ladjust && width && (width -= tmp) > 0)
428 while (width--)
429 db_putchar(padc);
430
431 while ((ch = *p--) != '\0')
432 db_putchar(ch);
433 break;
434 default:
435 db_putchar('%');
436 if (lflag)
437 db_putchar('l');
438 /* FALLTHROUGH */
439 case '%':
440 db_putchar(ch);
441 }
442 }
443 }
444
445