2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 * Author: David B. Golub, Carnegie Mellon University
32 * Printf and character output for debugger.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
47 #include <machine/stdarg.h>
50 #include <ddb/db_output.h>
52 struct dbputchar_arg {
60 * Character output - tracks position in line.
61 * To do this correctly, we should know how wide
62 * the output device is - then we could zero
63 * the line position when the output device wraps
64 * around to the start of the next line.
66 * Instead, we count the number of spaces printed
67 * since the last printing character so that we
68 * don't print trailing spaces. This avoids most
71 static int db_output_position = 0; /* output column */
72 static int db_last_non_space = 0; /* last non-space character */
73 db_expr_t db_tab_stop_width = 8; /* how wide are tab stops? */
75 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
76 db_expr_t db_max_width = 79; /* output line width */
77 db_expr_t db_lines_per_page = 20; /* lines per page */
78 volatile int db_pager_quit; /* user requested quit */
79 static int db_newlines; /* # lines this page */
80 static int db_maxlines; /* max lines/page when paging */
81 static int ddb_use_printf = 0;
82 SYSCTL_INT(_debug, OID_AUTO, ddb_use_printf, CTLFLAG_RW, &ddb_use_printf, 0,
83 "use printf for all ddb output");
85 static void db_putc(int c);
86 static void db_puts(const char *str);
87 static void db_putchar(int c, void *arg);
88 static void db_pager(void);
91 * Force pending whitespace.
94 db_force_whitespace(void)
96 register int last_print, next_tab;
98 last_print = db_last_non_space;
99 while (last_print < db_output_position) {
100 next_tab = NEXT_TAB(last_print);
101 if (next_tab <= db_output_position) {
102 while (last_print < next_tab) { /* DON'T send a tab!!! */
104 db_capture_writech(' ');
110 db_capture_writech(' ');
114 db_last_non_space = db_output_position;
118 * Output character. Buffer whitespace.
121 db_putchar(int c, void *arg)
123 struct dbputchar_arg *dap = arg;
125 if (dap->da_pbufr == NULL) {
127 /* No bufferized output is provided. */
131 *dap->da_pnext++ = c;
134 /* Leave always the buffer 0 terminated. */
135 *dap->da_pnext = '\0';
137 /* Check if the buffer needs to be flushed. */
138 if (dap->da_remain < 2 || c == '\n') {
139 db_puts(dap->da_pbufr);
140 dap->da_pnext = dap->da_pbufr;
141 dap->da_remain = dap->da_nbufr;
142 *dap->da_pnext = '\0';
152 * If not in the debugger or the user requests it, output data to
153 * both the console and the message buffer.
155 if (!kdb_active || ddb_use_printf) {
159 if (c == '\r' || c == '\n')
160 db_check_interrupt();
161 if (c == '\n' && db_maxlines > 0) {
163 if (db_newlines >= db_maxlines)
169 /* Otherwise, output data directly to the console. */
170 if (c > ' ' && c <= '~') {
172 * Printing character.
173 * If we have spaces to print, print them first.
174 * Use tabs if possible.
176 db_force_whitespace();
178 db_capture_writech(c);
179 db_output_position++;
180 db_last_non_space = db_output_position;
182 else if (c == '\n') {
185 db_capture_writech(c);
186 db_output_position = 0;
187 db_last_non_space = 0;
188 db_check_interrupt();
189 if (db_maxlines > 0) {
191 if (db_newlines >= db_maxlines)
195 else if (c == '\r') {
198 db_capture_writech(c);
199 db_output_position = 0;
200 db_last_non_space = 0;
201 db_check_interrupt();
203 else if (c == '\t') {
204 /* assume tabs every 8 positions */
205 db_output_position = NEXT_TAB(db_output_position);
209 db_output_position++;
211 else if (c == '\007') {
214 /* No need to beep in a log: db_capture_writech(c); */
216 /* other characters are assumed non-printing */
220 db_puts(const char *str)
224 for (i = 0; str[i] != '\0'; i++)
232 db_enable_pager(void)
234 if (db_maxlines == 0) {
235 db_maxlines = db_lines_per_page;
242 * Turn off the pager.
245 db_disable_pager(void)
251 * A simple paging callout function. It supports several simple more(1)-like
252 * commands as well as a quit command that sets db_pager_quit which db
253 * commands can poll to see if they should terminate early.
260 db_capture_enterpager();
261 db_printf("--More--\r");
269 /* Just one more line. */
275 db_maxlines = db_lines_per_page / 2;
281 db_maxlines = db_lines_per_page;
301 db_force_whitespace();
304 db_capture_exitpager();
308 * Return output position
311 db_print_position(void)
313 return (db_output_position);
320 db_printf(const char *fmt, ...)
323 char bufr[DDB_BUFR_SIZE];
325 struct dbputchar_arg dca;
331 dca.da_pnext = dca.da_pbufr;
332 dca.da_nbufr = sizeof(bufr);
333 dca.da_remain = sizeof(bufr);
334 *dca.da_pnext = '\0';
339 va_start(listp, fmt);
340 retval = kvprintf (fmt, db_putchar, &dca, db_radix, listp);
344 if (*dca.da_pbufr != '\0')
345 db_puts(dca.da_pbufr);
353 db_iprintf(const char *fmt,...)
356 char bufr[DDB_BUFR_SIZE];
358 struct dbputchar_arg dca;
362 for (i = db_indent; i >= 8; i -= 8)
369 dca.da_pnext = dca.da_pbufr;
370 dca.da_nbufr = sizeof(bufr);
371 dca.da_remain = sizeof(bufr);
372 *dca.da_pnext = '\0';
377 va_start(listp, fmt);
378 kvprintf (fmt, db_putchar, &dca, db_radix, listp);
382 if (*dca.da_pbufr != '\0')
383 db_puts(dca.da_pbufr);
388 * End line if too long.
391 db_end_line(int field_width)
393 if (db_output_position + field_width > db_max_width)