xref: /openbsd/sys/ddb/db_output.c (revision 09467b48)
1 /*	$OpenBSD: db_output.c,v 1.34 2020/01/20 15:58:23 visa Exp $	*/
2 /*	$NetBSD: db_output.c,v 1.13 1996/04/01 17:27:14 christos Exp $	*/
3 
4 /*
5  * Mach Operating System
6  * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
7  * All Rights Reserved.
8  *
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.
14  *
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.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie Mellon
27  * the rights to redistribute these changes.
28  */
29 
30 /*
31  * Printf and character output for debugger.
32  */
33 #include <sys/param.h>
34 #include <sys/stdarg.h>
35 #include <sys/systm.h>
36 #include <sys/stacktrace.h>
37 
38 #include <dev/cons.h>
39 
40 #include <machine/db_machdep.h>
41 
42 #include <ddb/db_command.h>
43 #include <ddb/db_output.h>
44 #include <ddb/db_access.h>
45 #include <ddb/db_interface.h>
46 #include <ddb/db_sym.h>
47 #include <ddb/db_var.h>
48 
49 /*
50  *	Character output - tracks position in line.
51  *	To do this correctly, we should know how wide
52  *	the output device is - then we could zero
53  *	the line position when the output device wraps
54  *	around to the start of the next line.
55  *
56  *	Instead, we count the number of spaces printed
57  *	since the last printing character so that we
58  *	don't print trailing spaces.  This avoids most
59  *	of the wraparounds.
60  */
61 
62 #ifndef	DB_MAX_LINE
63 #define	DB_MAX_LINE		24	/* maximum line */
64 #define DB_MAX_WIDTH		80	/* maximum width */
65 #endif	/* DB_MAX_LINE */
66 
67 #define DB_MIN_MAX_WIDTH	20	/* minimum max width */
68 #define DB_MIN_MAX_LINE		3	/* minimum max line */
69 #define CTRL(c)			((c) & 0xff)
70 
71 int	db_output_position = 0;		/* output column */
72 int	db_output_line = 0;		/* output line number */
73 int	db_last_non_space = 0;		/* last non-space character */
74 int	db_tab_stop_width = 8;		/* how wide are tab stops? */
75 #define	NEXT_TAB(i) \
76 	((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
77 int	db_max_line = DB_MAX_LINE;	/* output max lines */
78 int	db_max_width = DB_MAX_WIDTH;	/* output line width */
79 int	db_radix = 16;			/* output numbers radix */
80 
81 static void db_more(void);
82 
83 /*
84  * Force pending whitespace.
85  */
86 void
87 db_force_whitespace(void)
88 {
89 	int last_print, next_tab;
90 
91 	last_print = db_last_non_space;
92 	while (last_print < db_output_position) {
93 	    next_tab = NEXT_TAB(last_print);
94 	    if (next_tab <= db_output_position) {
95 		while (last_print < next_tab) { /* DON'T send a tab!!! */
96 			cnputc(' ');
97 			last_print++;
98 		}
99 	    }
100 	    else {
101 		cnputc(' ');
102 		last_print++;
103 	    }
104 	}
105 	db_last_non_space = db_output_position;
106 }
107 
108 static void
109 db_more(void)
110 {
111 	char *p;
112 	int quit_output = 0;
113 
114 	for (p = "--db_more--"; *p; p++)
115 	    cnputc(*p);
116 	switch(cngetc()) {
117 	case ' ':
118 	    db_output_line = 0;
119 	    break;
120 	case 'q':
121 	case CTRL('c'):
122 	    db_output_line = 0;
123 	    quit_output = 1;
124 	    break;
125 	default:
126 	    db_output_line--;
127 	    break;
128 	}
129 	p = "\b\b\b\b\b\b\b\b\b\b\b           \b\b\b\b\b\b\b\b\b\b\b";
130 	while (*p)
131 	    cnputc(*p++);
132 	if (quit_output) {
133 	    db_error(0);
134 	    /* NOTREACHED */
135 	}
136 }
137 
138 /*
139  * Output character.  Buffer whitespace.
140  */
141 void
142 db_putchar(int c)
143 {
144 	if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
145 	    db_more();
146 
147 	if (c > ' ' && c <= '~') {
148 	    /*
149 	     * Printing character.
150 	     * If we have spaces to print, print them first.
151 	     * Use tabs if possible.
152 	     */
153 	    db_force_whitespace();
154 	    cnputc(c);
155 	    db_output_position++;
156 	    if (db_max_width >= DB_MIN_MAX_WIDTH
157 		&& db_output_position >= db_max_width-1) {
158 		/* auto new line */
159 		cnputc('\n');
160 		db_output_position = 0;
161 		db_last_non_space = 0;
162 		db_output_line++;
163 	    }
164 	    db_last_non_space = db_output_position;
165 	}
166 	else if (c == '\n') {
167 	    /* Return */
168 	    cnputc(c);
169 	    db_output_position = 0;
170 	    db_last_non_space = 0;
171 	    db_output_line++;
172 	}
173 	else if (c == '\t') {
174 	    /* assume tabs every 8 positions */
175 	    db_output_position = NEXT_TAB(db_output_position);
176 	}
177 	else if (c == ' ') {
178 	    /* space */
179 	    db_output_position++;
180 	}
181 	else if (c == '\007') {
182 	    /* bell */
183 	    cnputc(c);
184 	}
185 	/* other characters are assumed non-printing */
186 }
187 
188 /*
189  * Return output position
190  */
191 int
192 db_print_position(void)
193 {
194 	return (db_output_position);
195 }
196 
197 /*
198  * End line if too long.
199  */
200 void
201 db_end_line(int space)
202 {
203 	if (db_output_position >= db_max_width - space)
204 	    db_printf("\n");
205 }
206 
207 char *
208 db_format(char *buf, size_t bufsize, long val, int format, int alt, int width)
209 {
210 	const char *fmt;
211 
212 	if (format == DB_FORMAT_Z || db_radix == 16)
213 		fmt = alt ? "-%#*lx" : "-%*lx";
214 	else if (db_radix == 8)
215 		fmt = alt ? "-%#*lo" : "-%*lo";
216 	else
217 		fmt = alt ? "-%#*lu" : "-%*lu";
218 
219 	/* The leading '-' is a nasty (and beautiful) idea from NetBSD */
220 	if (val < 0 && format != DB_FORMAT_N)
221 		val = -val;
222 	else
223 		fmt++;
224 
225 	snprintf(buf, bufsize, fmt, width, val);
226 
227 	return (buf);
228 }
229 
230 void
231 db_stack_dump(void)
232 {
233 	static volatile int intrace;
234 
235 	if (intrace) {
236 		printf("Faulted in traceback, aborting...\n");
237 		return;
238 	}
239 
240 	intrace = 1;
241 	printf("Starting stack trace...\n");
242 	db_stack_trace_print((db_expr_t)__builtin_frame_address(0), 1,
243 	    256 /* low limit */, "", printf);
244 	printf("End of stack trace.\n");
245 	intrace = 0;
246 }
247 
248 void
249 stacktrace_print(struct stacktrace *st, int (*pr)(const char *, ...))
250 {
251 	unsigned int i;
252 
253 	for (i = 0; i < st->st_count; i++) {
254 		(*pr)("#%-2u ", i);
255 		db_printsym(st->st_pc[i], DB_STGY_PROC, pr);
256 		(*pr)("\n");
257 	}
258 	if (st->st_count == 0)
259 		(*pr)("<empty stack trace>\n");
260 }
261