2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
34 * Printf and character output for debugger.
37 #include <sys/cdefs.h>
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? */
74 #define NEXT_TAB(i) rounddown((i) + db_tab_stop_width, db_tab_stop_width)
75 db_expr_t db_max_width = 79; /* output line width */
76 db_expr_t db_lines_per_page = 20; /* lines per page */
77 volatile int db_pager_quit; /* user requested quit */
78 static int db_newlines; /* # lines this page */
79 static int db_maxlines; /* max lines/page when paging */
80 static int ddb_use_printf = 0;
81 SYSCTL_INT(_debug, OID_AUTO, ddb_use_printf, CTLFLAG_RW, &ddb_use_printf, 0,
82 "use printf for all ddb output");
84 static void db_putc(int c);
85 static void db_puts(const char *str);
86 static void db_putchar(int c, void *arg);
87 static void db_pager(void);
90 * Force pending whitespace.
93 db_force_whitespace(void)
95 int last_print, next_tab;
97 last_print = db_last_non_space;
98 while (last_print < db_output_position) {
99 next_tab = NEXT_TAB(last_print);
100 if (next_tab <= db_output_position) {
101 while (last_print < next_tab) { /* DON'T send a tab!!! */
103 db_capture_writech(' ');
109 db_capture_writech(' ');
113 db_last_non_space = db_output_position;
117 * Output character. Buffer whitespace.
120 db_putchar(int c, void *arg)
122 struct dbputchar_arg *dap = arg;
124 if (dap->da_pbufr == NULL) {
125 /* No bufferized output is provided. */
128 *dap->da_pnext++ = c;
131 /* Leave always the buffer 0 terminated. */
132 *dap->da_pnext = '\0';
134 /* Check if the buffer needs to be flushed. */
135 if (dap->da_remain < 2 || c == '\n') {
136 db_puts(dap->da_pbufr);
137 dap->da_pnext = dap->da_pbufr;
138 dap->da_remain = dap->da_nbufr;
139 *dap->da_pnext = '\0';
149 * If not in the debugger or the user requests it, output data to
150 * both the console and the message buffer.
152 if (!kdb_active || ddb_use_printf) {
156 if (c == '\r' || c == '\n')
157 db_check_interrupt();
158 if (c == '\n' && db_maxlines > 0) {
160 if (db_newlines >= db_maxlines)
166 /* Otherwise, output data directly to the console. */
167 if (c > ' ' && c <= '~') {
169 * Printing character.
170 * If we have spaces to print, print them first.
171 * Use tabs if possible.
173 db_force_whitespace();
175 db_capture_writech(c);
176 db_output_position++;
177 db_last_non_space = db_output_position;
179 else if (c == '\n') {
182 db_capture_writech(c);
183 db_output_position = 0;
184 db_last_non_space = 0;
185 db_check_interrupt();
186 if (db_maxlines > 0) {
188 if (db_newlines >= db_maxlines)
192 else if (c == '\r') {
195 db_capture_writech(c);
196 db_output_position = 0;
197 db_last_non_space = 0;
198 db_check_interrupt();
200 else if (c == '\t') {
201 /* assume tabs every 8 positions */
202 db_output_position = NEXT_TAB(db_output_position);
206 db_output_position++;
208 else if (c == '\007') {
211 /* No need to beep in a log: db_capture_writech(c); */
213 /* other characters are assumed non-printing */
217 db_puts(const char *str)
221 for (i = 0; str[i] != '\0'; i++)
229 db_enable_pager(void)
231 if (db_maxlines == 0) {
232 db_maxlines = db_lines_per_page;
239 * Turn off the pager.
242 db_disable_pager(void)
248 * A simple paging callout function. It supports several simple more(1)-like
249 * commands as well as a quit command that sets db_pager_quit which db
250 * commands can poll to see if they should terminate early.
257 db_capture_enterpager();
258 db_printf("--More--\r");
266 /* Just one more line. */
272 db_maxlines = db_lines_per_page / 2;
278 db_maxlines = db_lines_per_page;
298 db_force_whitespace();
301 db_capture_exitpager();
305 * Return output position
308 db_print_position(void)
310 return (db_output_position);
317 db_printf(const char *fmt, ...)
320 char bufr[DDB_BUFR_SIZE];
322 struct dbputchar_arg dca;
328 dca.da_pnext = dca.da_pbufr;
329 dca.da_nbufr = sizeof(bufr);
330 dca.da_remain = sizeof(bufr);
331 *dca.da_pnext = '\0';
336 va_start(listp, fmt);
337 retval = kvprintf (fmt, db_putchar, &dca, db_radix, listp);
341 if (*dca.da_pbufr != '\0')
342 db_puts(dca.da_pbufr);
350 db_iprintf(const char *fmt,...)
353 char bufr[DDB_BUFR_SIZE];
355 struct dbputchar_arg dca;
359 for (i = db_indent; i >= 8; i -= 8)
366 dca.da_pnext = dca.da_pbufr;
367 dca.da_nbufr = sizeof(bufr);
368 dca.da_remain = sizeof(bufr);
369 *dca.da_pnext = '\0';
374 va_start(listp, fmt);
375 kvprintf (fmt, db_putchar, &dca, db_radix, listp);
379 if (*dca.da_pbufr != '\0')
380 db_puts(dca.da_pbufr);
385 * End line if too long.
388 db_end_line(int field_width)
390 if (db_output_position + field_width > db_max_width)