1 /* $OpenBSD: db_output.c,v 1.29 2015/03/14 03:38:46 jsg 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 37 #include <dev/cons.h> 38 39 #include <machine/db_machdep.h> 40 41 #include <ddb/db_command.h> 42 #include <ddb/db_output.h> 43 #include <ddb/db_interface.h> 44 #include <ddb/db_sym.h> 45 #include <ddb/db_var.h> 46 47 /* 48 * Character output - tracks position in line. 49 * To do this correctly, we should know how wide 50 * the output device is - then we could zero 51 * the line position when the output device wraps 52 * around to the start of the next line. 53 * 54 * Instead, we count the number of spaces printed 55 * since the last printing character so that we 56 * don't print trailing spaces. This avoids most 57 * of the wraparounds. 58 */ 59 60 #ifndef DB_MAX_LINE 61 #define DB_MAX_LINE 24 /* maximum line */ 62 #define DB_MAX_WIDTH 80 /* maximum width */ 63 #endif /* DB_MAX_LINE */ 64 65 #define DB_MIN_MAX_WIDTH 20 /* minimum max width */ 66 #define DB_MIN_MAX_LINE 3 /* minimum max line */ 67 #define CTRL(c) ((c) & 0xff) 68 69 int db_output_position = 0; /* output column */ 70 int db_output_line = 0; /* output line number */ 71 int db_last_non_space = 0; /* last non-space character */ 72 int db_tab_stop_width = 8; /* how wide are tab stops? */ 73 #define NEXT_TAB(i) \ 74 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width) 75 int db_max_line = DB_MAX_LINE; /* output max lines */ 76 int db_max_width = DB_MAX_WIDTH; /* output line width */ 77 int db_radix = 16; /* output numbers radix */ 78 79 static void db_more(void); 80 81 /* 82 * Force pending whitespace. 83 */ 84 void 85 db_force_whitespace(void) 86 { 87 int last_print, next_tab; 88 89 last_print = db_last_non_space; 90 while (last_print < db_output_position) { 91 next_tab = NEXT_TAB(last_print); 92 if (next_tab <= db_output_position) { 93 while (last_print < next_tab) { /* DON'T send a tab!!! */ 94 cnputc(' '); 95 last_print++; 96 } 97 } 98 else { 99 cnputc(' '); 100 last_print++; 101 } 102 } 103 db_last_non_space = db_output_position; 104 } 105 106 static void 107 db_more(void) 108 { 109 char *p; 110 int quit_output = 0; 111 112 for (p = "--db_more--"; *p; p++) 113 cnputc(*p); 114 switch(cngetc()) { 115 case ' ': 116 db_output_line = 0; 117 break; 118 case 'q': 119 case CTRL('c'): 120 db_output_line = 0; 121 quit_output = 1; 122 break; 123 default: 124 db_output_line--; 125 break; 126 } 127 p = "\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b"; 128 while (*p) 129 cnputc(*p++); 130 if (quit_output) { 131 db_error(0); 132 /* NOTREACHED */ 133 } 134 } 135 136 /* 137 * Output character. Buffer whitespace. 138 */ 139 void 140 db_putchar(int c) 141 { 142 if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1) 143 db_more(); 144 145 if (c > ' ' && c <= '~') { 146 /* 147 * Printing character. 148 * If we have spaces to print, print them first. 149 * Use tabs if possible. 150 */ 151 db_force_whitespace(); 152 cnputc(c); 153 db_output_position++; 154 if (db_max_width >= DB_MIN_MAX_WIDTH 155 && db_output_position >= db_max_width-1) { 156 /* auto new line */ 157 cnputc('\n'); 158 db_output_position = 0; 159 db_last_non_space = 0; 160 db_output_line++; 161 } 162 db_last_non_space = db_output_position; 163 } 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 } 171 else if (c == '\t') { 172 /* assume tabs every 8 positions */ 173 db_output_position = NEXT_TAB(db_output_position); 174 } 175 else if (c == ' ') { 176 /* space */ 177 db_output_position++; 178 } 179 else if (c == '\007') { 180 /* bell */ 181 cnputc(c); 182 } 183 /* other characters are assumed non-printing */ 184 } 185 186 /* 187 * Return output position 188 */ 189 int 190 db_print_position(void) 191 { 192 return (db_output_position); 193 } 194 195 /* 196 * End line if too long. 197 */ 198 void 199 db_end_line(int space) 200 { 201 if (db_output_position >= db_max_width - space) 202 db_printf("\n"); 203 } 204 205 char * 206 db_format(char *buf, size_t bufsize, long val, int format, int alt, int width) 207 { 208 const char *fmt; 209 210 if (format == DB_FORMAT_Z || db_radix == 16) 211 fmt = alt ? "-%#*lx" : "-%*lx"; 212 else if (db_radix == 8) 213 fmt = alt ? "-%#*lo" : "-%*lo"; 214 else 215 fmt = alt ? "-%#*lu" : "-%*lu"; 216 217 /* The leading '-' is a nasty (and beautiful) idea from NetBSD */ 218 if (val < 0 && format != DB_FORMAT_N) 219 val = -val; 220 else 221 fmt++; 222 223 snprintf(buf, bufsize, fmt, width, val); 224 225 return (buf); 226 } 227 228 void 229 db_stack_dump(void) 230 { 231 static int intrace; 232 233 if (intrace) { 234 printf("Faulted in traceback, aborting...\n"); 235 return; 236 } 237 238 intrace = 1; 239 printf("Starting stack trace...\n"); 240 db_stack_trace_print((db_expr_t)__builtin_frame_address(0), TRUE, 241 256 /* low limit */, "", printf); 242 printf("End of stack trace.\n"); 243 intrace = 0; 244 } 245