1 /* $OpenBSD: db_output.c,v 1.35 2020/10/15 03:14:00 deraadt 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 } else { 100 cnputc(' '); 101 last_print++; 102 } 103 } 104 db_last_non_space = db_output_position; 105 } 106 107 static void 108 db_more(void) 109 { 110 char *p; 111 int quit_output = 0; 112 113 for (p = "--db_more--"; *p; p++) 114 cnputc(*p); 115 switch(cngetc()) { 116 case ' ': 117 db_output_line = 0; 118 break; 119 case 'q': 120 case CTRL('c'): 121 db_output_line = 0; 122 quit_output = 1; 123 break; 124 default: 125 db_output_line--; 126 break; 127 } 128 p = "\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b"; 129 while (*p) 130 cnputc(*p++); 131 if (quit_output) { 132 db_error(0); 133 /* NOTREACHED */ 134 } 135 } 136 137 /* 138 * Output character. Buffer whitespace. 139 */ 140 void 141 db_putchar(int c) 142 { 143 if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1) 144 db_more(); 145 146 if (c > ' ' && c <= '~') { 147 /* 148 * Printing character. 149 * If we have spaces to print, print them first. 150 * Use tabs if possible. 151 */ 152 db_force_whitespace(); 153 cnputc(c); 154 db_output_position++; 155 if (db_max_width >= DB_MIN_MAX_WIDTH && 156 db_output_position >= db_max_width-1) { 157 /* auto new line */ 158 cnputc('\n'); 159 db_output_position = 0; 160 db_last_non_space = 0; 161 db_output_line++; 162 } 163 db_last_non_space = db_output_position; 164 } else if (c == '\n') { 165 /* Return */ 166 cnputc(c); 167 db_output_position = 0; 168 db_last_non_space = 0; 169 db_output_line++; 170 } else if (c == '\t') { 171 /* assume tabs every 8 positions */ 172 db_output_position = NEXT_TAB(db_output_position); 173 } else if (c == ' ') { 174 /* space */ 175 db_output_position++; 176 } else if (c == '\007') { 177 /* bell */ 178 cnputc(c); 179 } 180 /* other characters are assumed non-printing */ 181 } 182 183 /* 184 * Return output position 185 */ 186 int 187 db_print_position(void) 188 { 189 return (db_output_position); 190 } 191 192 /* 193 * End line if too long. 194 */ 195 void 196 db_end_line(int space) 197 { 198 if (db_output_position >= db_max_width - space) 199 db_printf("\n"); 200 } 201 202 char * 203 db_format(char *buf, size_t bufsize, long val, int format, int alt, int width) 204 { 205 const char *fmt; 206 207 if (format == DB_FORMAT_Z || db_radix == 16) 208 fmt = alt ? "-%#*lx" : "-%*lx"; 209 else if (db_radix == 8) 210 fmt = alt ? "-%#*lo" : "-%*lo"; 211 else 212 fmt = alt ? "-%#*lu" : "-%*lu"; 213 214 /* The leading '-' is a nasty (and beautiful) idea from NetBSD */ 215 if (val < 0 && format != DB_FORMAT_N) 216 val = -val; 217 else 218 fmt++; 219 220 snprintf(buf, bufsize, fmt, width, val); 221 return (buf); 222 } 223 224 void 225 db_stack_dump(void) 226 { 227 static volatile int intrace; 228 229 if (intrace) { 230 printf("Faulted in traceback, aborting...\n"); 231 return; 232 } 233 234 intrace = 1; 235 printf("Starting stack trace...\n"); 236 db_stack_trace_print((db_expr_t)__builtin_frame_address(0), 1, 237 256 /* low limit */, "", printf); 238 printf("End of stack trace.\n"); 239 intrace = 0; 240 } 241 242 void 243 stacktrace_print(struct stacktrace *st, int (*pr)(const char *, ...)) 244 { 245 unsigned int i; 246 247 for (i = 0; i < st->st_count; i++) { 248 (*pr)("#%-2u ", i); 249 db_printsym(st->st_pc[i], DB_STGY_PROC, pr); 250 (*pr)("\n"); 251 } 252 if (st->st_count == 0) 253 (*pr)("<empty stack trace>\n"); 254 } 255