xref: /netbsd-src/sys/ddb/db_output.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: db_output.c,v 1.30 2009/03/07 22:02:17 ad 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 "AS IS"
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@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 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: db_output.c,v 1.30 2009/03/07 22:02:17 ad Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 
39 #include <machine/stdarg.h>
40 
41 #include <dev/cons.h>
42 
43 #include <ddb/ddb.h>
44 
45 /*
46  *	Character output - tracks position in line.
47  *	To do this correctly, we should know how wide
48  *	the output device is - then we could zero
49  *	the line position when the output device wraps
50  *	around to the start of the next line.
51  *
52  *	Instead, we count the number of spaces printed
53  *	since the last printing character so that we
54  *	don't print trailing spaces.  This avoids most
55  *	of the wraparounds.
56  */
57 
58 #ifndef	DB_MAX_LINE
59 #define	DB_MAX_LINE		24	/* maximum line */
60 #endif	/* DB_MAX_LINE */
61 #ifndef	DB_MAX_WIDTH
62 #define DB_MAX_WIDTH		80	/* maximum width */
63 #endif	/* DB_MAX_WIDTH */
64 
65 #define DB_MIN_MAX_WIDTH	20	/* minimum max width */
66 #define DB_MIN_MAX_LINE		3	/* minimum max line */
67 #define CTRL(c)			((c) & 0xff)
68 
69 int	db_output_line = 0;			/* output line number */
70 int	db_tab_stop_width = 8;			/* how wide are tab stops? */
71 int	db_max_line = DB_MAX_LINE;		/* output max lines */
72 int	db_max_width = DB_MAX_WIDTH;		/* output line width */
73 
74 static int	db_output_position = 0;		/* output column */
75 static int	db_last_non_space = 0;		/* last non-space character */
76 
77 static void db_more(void);
78 
79 /*
80  * Force pending whitespace.
81  */
82 void
83 db_force_whitespace(void)
84 {
85 	int last_print, next_tab;
86 
87 	last_print = db_last_non_space;
88 	while (last_print < db_output_position) {
89 		next_tab = DB_NEXT_TAB(last_print);
90 		if (next_tab <= db_output_position) {
91 			while (last_print < next_tab) { /* DON'T send a tab!! */
92 				cnputc(' ');
93 				last_print++;
94 			}
95 		} else {
96 			cnputc(' ');
97 			last_print++;
98 		}
99 	}
100 	db_last_non_space = db_output_position;
101 }
102 
103 static void
104 db_more(void)
105 {
106 	const char *p;
107 	int quit_output = 0;
108 
109 	for (p = "--db_more--"; *p; p++)
110 		cnputc(*p);
111 	switch(cngetc()) {
112 	case ' ':
113 		db_output_line = 0;
114 		break;
115 	case 'q':
116 	case CTRL('c'):
117 		db_output_line = 0;
118 		quit_output = 1;
119 		break;
120 	default:
121 		db_output_line--;
122 		break;
123 	}
124 	p = "\b\b\b\b\b\b\b\b\b\b\b           \b\b\b\b\b\b\b\b\b\b\b";
125 	while (*p)
126 		cnputc(*p++);
127 	if (quit_output) {
128 		db_error(0);
129 		/* NOTREACHED */
130 	}
131 }
132 
133 /*
134  * Output character.  Buffer whitespace.
135  */
136 void
137 db_putchar(int c)
138 {
139 	if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
140 		db_more();
141 	if (c > ' ' && c <= '~') {
142 		/*
143 		 * Printing character.
144 		 * If we have spaces to print, print them first.
145 		 * Use tabs if possible.
146 		 */
147 		db_force_whitespace();
148 		cnputc(c);
149 		db_output_position++;
150 		if (db_max_width >= DB_MIN_MAX_WIDTH
151 		    && db_output_position >= db_max_width) {
152 			/* auto new line */
153 			cnputc('\n');
154 			db_output_position = 0;
155 			db_last_non_space = 0;
156 			db_output_line++;
157 		}
158 		db_last_non_space = db_output_position;
159 	} else if (c == '\n') {
160 		/* Return */
161 		cnputc(c);
162 		db_output_position = 0;
163 		db_last_non_space = 0;
164 		db_output_line++;
165 		db_check_interrupt();
166 	} else if (c == '\t') {
167 		/* assume tabs every 8 positions */
168 		db_output_position = DB_NEXT_TAB(db_output_position);
169 	} else if (c == ' ') {
170 		/* space */
171 		db_output_position++;
172 	} else if (c == '\007') {
173 		/* bell */
174 		cnputc(c);
175 	}
176 	/* other characters are assumed non-printing */
177 }
178 
179 /*
180  * Return output position
181  */
182 int
183 db_print_position(void)
184 {
185 
186 	return (db_output_position);
187 }
188 
189 /*
190  * End line if too long.
191  */
192 void
193 db_end_line(void)
194 {
195 
196 	if (db_output_position >= db_max_width)
197 		db_printf("\n");
198 }
199 
200 /*
201  * Replacement for old '%r' kprintf format.
202  */
203 void
204 db_format_radix(char *buf, size_t bufsiz, quad_t val, int altflag)
205 {
206 	const char *fmt;
207 
208 	if (db_radix == 16) {
209 		db_format_hex(buf, bufsiz, val, altflag);
210 		return;
211 	}
212 
213 	if (db_radix == 8)
214 		fmt = altflag ? "-%#qo" : "-%qo";
215 	else
216 		fmt = altflag ? "-%#qu" : "-%qu";
217 
218 	if (val < 0)
219 		val = -val;
220 	else
221 		++fmt;
222 
223 	snprintf(buf, bufsiz, fmt, val);
224 }
225 
226 /*
227  * Replacement for old '%z' kprintf format.
228  */
229 void
230 db_format_hex(char *buf, size_t bufsiz, quad_t val, int altflag)
231 {
232 	/* Only use alternate form if val is nonzero. */
233 	const char *fmt = (altflag && val) ? "-%#qx" : "-%qx";
234 
235 	if (val < 0)
236 		val = -val;
237 	else
238 		++fmt;
239 
240 	snprintf(buf, bufsiz, fmt, val);
241 }
242