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