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