1 /* $OpenBSD: db_output.c,v 1.26 2010/09/08 04:28:29 marco Exp $ */
2 /* $NetBSD: db_output.c,v 1.13 1996/04/01 17:27:14 christos Exp $ */
5 * Mach Operating System
6 * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie Mellon
27 * the rights to redistribute these changes.
31 * Printf and character output for debugger.
33 #include <sys/param.h>
35 #include <sys/stdarg.h>
36 #include <sys/systm.h>
40 #include <uvm/uvm_extern.h>
42 #include <machine/db_machdep.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_output.h>
46 #include <ddb/db_interface.h>
47 #include <ddb/db_sym.h>
48 #include <ddb/db_var.h>
49 #include <ddb/db_extern.h>
52 * Character output - tracks position in line.
53 * To do this correctly, we should know how wide
54 * the output device is - then we could zero
55 * the line position when the output device wraps
56 * around to the start of the next line.
58 * Instead, we count the number of spaces printed
59 * since the last printing character so that we
60 * don't print trailing spaces. This avoids most
65 #define DB_MAX_LINE 24 /* maximum line */
66 #define DB_MAX_WIDTH 80 /* maximum width */
67 #endif /* DB_MAX_LINE */
69 #define DB_MIN_MAX_WIDTH 20 /* minimum max width */
70 #define DB_MIN_MAX_LINE 3 /* minimum max line */
71 #define CTRL(c) ((c) & 0xff)
73 int db_output_position
= 0; /* output column */
74 int db_output_line
= 0; /* output line number */
75 int db_last_non_space
= 0; /* last non-space character */
76 int db_tab_stop_width
= 8; /* how wide are tab stops? */
78 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
79 int db_max_line
= DB_MAX_LINE
; /* output max lines */
80 int db_max_width
= DB_MAX_WIDTH
; /* output line width */
81 int db_radix
= 16; /* output numbers radix */
83 static void db_more(void);
86 * Force pending whitespace.
89 db_force_whitespace(void)
91 int last_print
, next_tab
;
93 last_print
= db_last_non_space
;
94 while (last_print
< db_output_position
) {
95 next_tab
= NEXT_TAB(last_print
);
96 if (next_tab
<= db_output_position
) {
97 while (last_print
< next_tab
) { /* DON'T send a tab!!! */
107 db_last_non_space
= db_output_position
;
116 for (p
= "--db_more--"; *p
; p
++)
131 p
= "\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b";
141 * Output character. Buffer whitespace.
146 if (db_max_line
>= DB_MIN_MAX_LINE
&& db_output_line
>= db_max_line
-1)
149 if (c
> ' ' && c
<= '~') {
151 * Printing character.
152 * If we have spaces to print, print them first.
153 * Use tabs if possible.
155 db_force_whitespace();
157 db_output_position
++;
158 if (db_max_width
>= DB_MIN_MAX_WIDTH
159 && db_output_position
>= db_max_width
-1) {
162 db_output_position
= 0;
163 db_last_non_space
= 0;
166 db_last_non_space
= db_output_position
;
168 else if (c
== '\n') {
171 db_output_position
= 0;
172 db_last_non_space
= 0;
175 else if (c
== '\t') {
176 /* assume tabs every 8 positions */
177 db_output_position
= NEXT_TAB(db_output_position
);
181 db_output_position
++;
183 else if (c
== '\007') {
187 /* other characters are assumed non-printing */
191 * Return output position
194 db_print_position(void)
196 return (db_output_position
);
200 * End line if too long.
203 db_end_line(int space
)
205 if (db_output_position
>= db_max_width
- space
)
210 db_format(char *buf
, size_t bufsize
, long val
, int format
, int alt
, int width
)
214 if (format
== DB_FORMAT_Z
|| db_radix
== 16)
215 fmt
= alt
? "-%#*lx" : "-%*lx";
216 else if (db_radix
== 8)
217 fmt
= alt
? "-%#*lo" : "-%*lo";
219 fmt
= alt
? "-%#*lu" : "-%*lu";
221 /* The leading '-' is a nasty (and beautiful) idea from NetBSD */
222 if (val
< 0 && format
!= DB_FORMAT_N
)
227 snprintf(buf
, bufsize
, fmt
, width
, val
);
238 printf("Faulted in traceback, aborting...\n");
243 printf("Starting stack trace...\n");
244 db_stack_trace_print((db_expr_t
)__builtin_frame_address(0), TRUE
,
245 256 /* low limit */, "", printf
);
246 printf("End of stack trace.\n");