]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_output.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / ddb / db_output.c
1 /*
2 * Copyright (c) 2000-2004 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 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60 /*
61 * Author: David B. Golub, Carnegie Mellon University
62 * Date: 7/90
63 */
64
65 /*
66 * Printf and character output for debugger.
67 */
68
69 #include <mach/boolean.h>
70 #include <kern/misc_protos.h>
71 #include <stdarg.h>
72 #include <machine/db_machdep.h>
73 #include <ddb/db_command.h>
74 #include <ddb/db_lex.h>
75 #include <ddb/db_input.h>
76 #include <ddb/db_output.h>
77 #include <ddb/db_task_thread.h>
78
79 /*
80 * Character output - tracks position in line.
81 * To do this correctly, we should know how wide
82 * the output device is - then we could zero
83 * the line position when the output device wraps
84 * around to the start of the next line.
85 *
86 * Instead, we count the number of spaces printed
87 * since the last printing character so that we
88 * don't print trailing spaces. This avoids most
89 * of the wraparounds.
90 */
91
92 #ifndef DB_MAX_LINE
93 #define DB_MAX_LINE 43 /* maximum line */
94 #define DB_MAX_WIDTH 132 /* maximum width */
95 #endif /* DB_MAX_LINE */
96
97 #define DB_MIN_MAX_WIDTH 20 /* minimum max width */
98 #define DB_MIN_MAX_LINE 3 /* minimum max line */
99 #define CTRL(c) ((c) & 0xff)
100
101 int db_output_position = 0; /* output column */
102 int db_output_line = 0; /* output line number */
103 int db_last_non_space = 0; /* last non-space character */
104 int db_last_gen_return = 0; /* last character generated return */
105 int db_auto_wrap = 1; /* auto wrap at end of line ? */
106 int db_tab_stop_width = 8; /* how wide are tab stops? */
107 #define NEXT_TAB(i) \
108 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
109 int db_max_line = DB_MAX_LINE; /* output max lines */
110 int db_max_width = DB_MAX_WIDTH; /* output line width */
111
112
113 /* Prototypes for functions local to this file. XXX -- should be static!
114 */
115 static void db_more(void);
116 void db_advance_output_position(int new_output_position,
117 int blank);
118
119
120 /*
121 * Force pending whitespace.
122 */
123 void
124 db_force_whitespace(void)
125 {
126 register int last_print, next_tab;
127
128 last_print = db_last_non_space;
129 while (last_print < db_output_position) {
130 next_tab = NEXT_TAB(last_print);
131 if (next_tab <= db_output_position) {
132 cnputc('\t');
133 last_print = next_tab;
134 }
135 else {
136 cnputc(' ');
137 last_print++;
138 }
139 }
140 db_last_non_space = db_output_position;
141 }
142
143 void
144 db_reset_more()
145 {
146 db_output_line = 0;
147 }
148
149 static void
150 db_more(void)
151 {
152 register char *p;
153 boolean_t quit_output = FALSE;
154
155 for (p = "--db_more--"; *p; p++)
156 cnputc(*p);
157 switch(cngetc()) {
158 case ' ':
159 db_output_line = 0;
160 break;
161 case 'q':
162 case CTRL('c'):
163 db_output_line = 0;
164 quit_output = TRUE;
165 break;
166 default:
167 db_output_line--;
168 break;
169 }
170 p = "\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b";
171 while (*p)
172 cnputc(*p++);
173 if (quit_output) {
174 db_error((char *) 0);
175 /* NOTREACHED */
176 }
177 }
178
179 void
180 db_advance_output_position(int new_output_position,
181 int blank)
182 {
183 if (db_max_width >= DB_MIN_MAX_WIDTH
184 && new_output_position >= db_max_width) {
185 /* auto new line */
186 if (!db_auto_wrap || blank)
187 cnputc('\n');
188 db_output_position = 0;
189 db_last_non_space = 0;
190 db_last_gen_return = 1;
191 db_output_line++;
192 } else {
193 db_output_position = new_output_position;
194 }
195 }
196
197 boolean_t
198 db_reserve_output_position(int increment)
199 {
200 if (db_max_width >= DB_MIN_MAX_WIDTH
201 && db_output_position + increment >= db_max_width) {
202 /* auto new line */
203 if (!db_auto_wrap || db_last_non_space != db_output_position)
204 cnputc('\n');
205 db_output_position = 0;
206 db_last_non_space = 0;
207 db_last_gen_return = 1;
208 db_output_line++;
209 return TRUE;
210 }
211 return FALSE;
212 }
213
214 /*
215 * Output character. Buffer whitespace.
216 */
217 void
218 db_putchar(char c)
219 {
220 if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
221 db_more();
222 if (c > ' ' && c <= '~') {
223 /*
224 * Printing character.
225 * If we have spaces to print, print them first.
226 * Use tabs if possible.
227 */
228 db_force_whitespace();
229 cnputc(c);
230 db_last_gen_return = 0;
231 db_advance_output_position(db_output_position+1, 0);
232 db_last_non_space = db_output_position;
233 }
234 else if (c == '\n') {
235 /* Return */
236 if (db_last_gen_return) {
237 db_last_gen_return = 0;
238 } else {
239 cnputc(c);
240 db_output_position = 0;
241 db_last_non_space = 0;
242 db_output_line++;
243 db_check_interrupt();
244 }
245 }
246 else if (c == '\t') {
247 /* assume tabs every 8 positions */
248 db_advance_output_position(NEXT_TAB(db_output_position), 1);
249 }
250 else if (c == ' ') {
251 /* space */
252 db_advance_output_position(db_output_position+1, 1);
253 }
254 else if (c == '\007') {
255 /* bell */
256 cnputc(c);
257 }
258 /* other characters are assumed non-printing */
259 }
260
261 /*
262 * Return output position
263 */
264 int
265 db_print_position(void)
266 {
267 return (db_output_position);
268 }
269
270 /*
271 * End line if too long.
272 */
273 void
274 db_end_line(void)
275 {
276 if (db_output_position >= db_max_width-1) {
277 /* auto new line */
278 if (!db_auto_wrap)
279 cnputc('\n');
280 db_output_position = 0;
281 db_last_non_space = 0;
282 db_last_gen_return = 1;
283 db_output_line++;
284 }
285 }
286
287 /*
288 * Printing
289 */
290
291 void
292 db_printf(const char *fmt, ...)
293 {
294 va_list listp;
295
296 va_start(listp, fmt);
297 _doprnt(fmt, &listp, db_putchar, db_radix);
298 va_end(listp);
299 }
300
301 /* alternate name */
302
303 void
304 kdbprintf(const char *fmt, ...)
305 {
306 va_list listp;
307
308 va_start(listp, fmt);
309 _doprnt(fmt, &listp, db_putchar, db_radix);
310 va_end(listp);
311 }
312
313 int db_indent = 0;
314
315 /*
316 * Printing (to console) with indentation.
317 */
318 void
319 iprintf(const char *fmt, ...)
320 {
321 va_list listp;
322 register int i;
323
324 for (i = db_indent; i > 0; ){
325 if (i >= 8) {
326 kdbprintf("\t");
327 i -= 8;
328 }
329 else {
330 kdbprintf(" ");
331 i--;
332 }
333 }
334
335 va_start(listp, fmt);
336 _doprnt(fmt, &listp, db_putchar, db_radix);
337 va_end(listp);
338 }
339
340 void
341 db_output_prompt(void)
342 {
343 db_printf("db%s", (db_default_act) ? "t": "");
344 db_printf("{%d}", cpu_number());
345 db_printf("> ");
346 }
347