Home | History | Annotate | Line # | Download | only in ddb
db_output.c revision 1.24.2.3
      1  1.24.2.3   nathanw /*	$NetBSD: db_output.c,v 1.24.2.3 2002/02/28 04:13:06 nathanw Exp $	*/
      2       1.7       cgd 
      3  1.24.2.3   nathanw /*
      4       1.1       cgd  * Mach Operating System
      5       1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6       1.1       cgd  * All Rights Reserved.
      7  1.24.2.3   nathanw  *
      8       1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9       1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10       1.1       cgd  * notice and this permission notice appear in all copies of the
     11       1.1       cgd  * software, derivative works or modified versions, and any portions
     12       1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13  1.24.2.3   nathanw  *
     14      1.19        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15       1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16       1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.24.2.3   nathanw  *
     18       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.24.2.3   nathanw  *
     20       1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21       1.1       cgd  *  School of Computer Science
     22       1.1       cgd  *  Carnegie Mellon University
     23       1.1       cgd  *  Pittsburgh PA 15213-3890
     24  1.24.2.3   nathanw  *
     25       1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26       1.1       cgd  * rights to redistribute these changes.
     27       1.1       cgd  */
     28       1.1       cgd 
     29       1.1       cgd /*
     30       1.1       cgd  * Printf and character output for debugger.
     31       1.1       cgd  */
     32  1.24.2.2   nathanw 
     33  1.24.2.2   nathanw #include <sys/cdefs.h>
     34  1.24.2.3   nathanw __KERNEL_RCSID(0, "$NetBSD: db_output.c,v 1.24.2.3 2002/02/28 04:13:06 nathanw Exp $");
     35  1.24.2.2   nathanw 
     36       1.6   mycroft #include <sys/param.h>
     37      1.16       cgd #include <sys/systm.h>
     38       1.1       cgd 
     39       1.1       cgd #include <machine/stdarg.h>
     40       1.1       cgd 
     41      1.10  christos #include <dev/cons.h>
     42      1.10  christos 
     43      1.10  christos #include <machine/db_machdep.h>
     44      1.10  christos 
     45      1.10  christos #include <ddb/db_command.h>
     46      1.10  christos #include <ddb/db_output.h>
     47      1.10  christos #include <ddb/db_interface.h>
     48      1.10  christos #include <ddb/db_sym.h>
     49      1.10  christos #include <ddb/db_extern.h>
     50      1.10  christos 
     51       1.1       cgd /*
     52       1.1       cgd  *	Character output - tracks position in line.
     53       1.1       cgd  *	To do this correctly, we should know how wide
     54       1.1       cgd  *	the output device is - then we could zero
     55       1.1       cgd  *	the line position when the output device wraps
     56       1.1       cgd  *	around to the start of the next line.
     57       1.1       cgd  *
     58       1.1       cgd  *	Instead, we count the number of spaces printed
     59       1.1       cgd  *	since the last printing character so that we
     60       1.1       cgd  *	don't print trailing spaces.  This avoids most
     61       1.1       cgd  *	of the wraparounds.
     62       1.1       cgd  */
     63       1.4    brezak 
     64       1.4    brezak #ifndef	DB_MAX_LINE
     65       1.4    brezak #define	DB_MAX_LINE		24	/* maximum line */
     66      1.24       cgd #endif	/* DB_MAX_LINE */
     67  1.24.2.1   nathanw #ifndef	DB_MAX_WIDTH
     68  1.24.2.1   nathanw #define DB_MAX_WIDTH		80	/* maximum width */
     69  1.24.2.1   nathanw #endif	/* DB_MAX_WIDTH */
     70       1.4    brezak 
     71       1.4    brezak #define DB_MIN_MAX_WIDTH	20	/* minimum max width */
     72       1.4    brezak #define DB_MIN_MAX_LINE		3	/* minimum max line */
     73       1.4    brezak #define CTRL(c)			((c) & 0xff)
     74       1.4    brezak 
     75  1.24.2.3   nathanw int	db_output_line = 0;			/* output line number */
     76  1.24.2.3   nathanw int	db_tab_stop_width = 8;			/* how wide are tab stops? */
     77  1.24.2.3   nathanw int	db_max_line = DB_MAX_LINE;		/* output max lines */
     78  1.24.2.3   nathanw int	db_max_width = DB_MAX_WIDTH;		/* output line width */
     79  1.24.2.3   nathanw 
     80  1.24.2.3   nathanw static int	db_output_position = 0;		/* output column */
     81  1.24.2.3   nathanw static int	db_last_non_space = 0;		/* last non-space character */
     82       1.1       cgd 
     83  1.24.2.3   nathanw static void db_more(void);
     84       1.1       cgd 
     85       1.1       cgd /*
     86       1.1       cgd  * Force pending whitespace.
     87       1.1       cgd  */
     88       1.1       cgd void
     89  1.24.2.3   nathanw db_force_whitespace(void)
     90       1.1       cgd {
     91      1.22  augustss 	int last_print, next_tab;
     92       1.1       cgd 
     93       1.1       cgd 	last_print = db_last_non_space;
     94       1.1       cgd 	while (last_print < db_output_position) {
     95  1.24.2.3   nathanw 		next_tab = DB_NEXT_TAB(last_print);
     96  1.24.2.3   nathanw 		if (next_tab <= db_output_position) {
     97  1.24.2.3   nathanw 			while (last_print < next_tab) { /* DON'T send a tab!! */
     98  1.24.2.3   nathanw 				cnputc(' ');
     99  1.24.2.3   nathanw 				last_print++;
    100  1.24.2.3   nathanw 			}
    101  1.24.2.3   nathanw 		} else {
    102       1.2       cgd 			cnputc(' ');
    103       1.2       cgd 			last_print++;
    104       1.2       cgd 		}
    105       1.1       cgd 	}
    106       1.1       cgd 	db_last_non_space = db_output_position;
    107       1.1       cgd }
    108       1.1       cgd 
    109       1.4    brezak static void
    110  1.24.2.3   nathanw db_more(void)
    111       1.4    brezak {
    112      1.22  augustss 	char *p;
    113       1.4    brezak 	int quit_output = 0;
    114       1.4    brezak 
    115       1.4    brezak 	for (p = "--db_more--"; *p; p++)
    116  1.24.2.3   nathanw 		cnputc(*p);
    117       1.4    brezak 	switch(cngetc()) {
    118       1.4    brezak 	case ' ':
    119  1.24.2.3   nathanw 		db_output_line = 0;
    120  1.24.2.3   nathanw 		break;
    121       1.4    brezak 	case 'q':
    122       1.4    brezak 	case CTRL('c'):
    123  1.24.2.3   nathanw 		db_output_line = 0;
    124  1.24.2.3   nathanw 		quit_output = 1;
    125  1.24.2.3   nathanw 		break;
    126       1.4    brezak 	default:
    127  1.24.2.3   nathanw 		db_output_line--;
    128  1.24.2.3   nathanw 		break;
    129       1.4    brezak 	}
    130       1.4    brezak 	p = "\b\b\b\b\b\b\b\b\b\b\b           \b\b\b\b\b\b\b\b\b\b\b";
    131       1.4    brezak 	while (*p)
    132  1.24.2.3   nathanw 		cnputc(*p++);
    133       1.4    brezak 	if (quit_output) {
    134  1.24.2.3   nathanw 		db_error(0);
    135  1.24.2.3   nathanw 		/* NOTREACHED */
    136       1.4    brezak 	}
    137       1.4    brezak }
    138       1.4    brezak 
    139       1.1       cgd /*
    140       1.1       cgd  * Output character.  Buffer whitespace.
    141       1.1       cgd  */
    142      1.10  christos void
    143  1.24.2.3   nathanw db_putchar(int c)
    144       1.1       cgd {
    145       1.4    brezak 	if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
    146  1.24.2.3   nathanw 		db_more();
    147       1.1       cgd 	if (c > ' ' && c <= '~') {
    148  1.24.2.3   nathanw 		/*
    149  1.24.2.3   nathanw 		 * Printing character.
    150  1.24.2.3   nathanw 		 * If we have spaces to print, print them first.
    151  1.24.2.3   nathanw 		 * Use tabs if possible.
    152  1.24.2.3   nathanw 		 */
    153  1.24.2.3   nathanw 		db_force_whitespace();
    154  1.24.2.3   nathanw 		cnputc(c);
    155  1.24.2.3   nathanw 		db_output_position++;
    156  1.24.2.3   nathanw 		if (db_max_width >= DB_MIN_MAX_WIDTH
    157  1.24.2.3   nathanw 		    && db_output_position >= db_max_width) {
    158  1.24.2.3   nathanw 			/* auto new line */
    159  1.24.2.3   nathanw 			cnputc('\n');
    160  1.24.2.3   nathanw 			db_output_position = 0;
    161  1.24.2.3   nathanw 			db_last_non_space = 0;
    162  1.24.2.3   nathanw 			db_output_line++;
    163  1.24.2.3   nathanw 		}
    164  1.24.2.3   nathanw 		db_last_non_space = db_output_position;
    165  1.24.2.3   nathanw 	} else if (c == '\n') {
    166  1.24.2.3   nathanw 		/* Return */
    167  1.24.2.3   nathanw 		cnputc(c);
    168       1.4    brezak 		db_output_position = 0;
    169       1.4    brezak 		db_last_non_space = 0;
    170       1.4    brezak 		db_output_line++;
    171  1.24.2.3   nathanw 		db_check_interrupt();
    172  1.24.2.3   nathanw 	} else if (c == '\t') {
    173  1.24.2.3   nathanw 		/* assume tabs every 8 positions */
    174  1.24.2.3   nathanw 		db_output_position = DB_NEXT_TAB(db_output_position);
    175  1.24.2.3   nathanw 	} else if (c == ' ') {
    176  1.24.2.3   nathanw 		/* space */
    177  1.24.2.3   nathanw 		db_output_position++;
    178  1.24.2.3   nathanw 	} else if (c == '\007') {
    179  1.24.2.3   nathanw 		/* bell */
    180  1.24.2.3   nathanw 		cnputc(c);
    181       1.1       cgd 	}
    182       1.1       cgd 	/* other characters are assumed non-printing */
    183       1.1       cgd }
    184       1.1       cgd 
    185       1.1       cgd /*
    186       1.1       cgd  * Return output position
    187       1.1       cgd  */
    188       1.1       cgd int
    189  1.24.2.3   nathanw db_print_position(void)
    190       1.1       cgd {
    191  1.24.2.3   nathanw 
    192       1.1       cgd 	return (db_output_position);
    193       1.1       cgd }
    194       1.1       cgd 
    195       1.1       cgd /*
    196       1.4    brezak  * End line if too long.
    197       1.4    brezak  */
    198       1.4    brezak void
    199  1.24.2.3   nathanw db_end_line(void)
    200       1.4    brezak {
    201  1.24.2.3   nathanw 
    202       1.4    brezak 	if (db_output_position >= db_max_width)
    203  1.24.2.3   nathanw 		db_printf("\n");
    204      1.23        tv }
    205      1.23        tv 
    206      1.23        tv /*
    207      1.23        tv  * Replacement for old '%r' kprintf format.
    208      1.23        tv  */
    209      1.23        tv void
    210  1.24.2.3   nathanw db_format_radix(char *buf, size_t bufsiz, quad_t val, int altflag)
    211      1.23        tv {
    212      1.23        tv 	const char *fmt;
    213      1.23        tv 
    214      1.23        tv 	if (db_radix == 16) {
    215      1.23        tv 		db_format_hex(buf, bufsiz, val, altflag);
    216      1.23        tv 		return;
    217      1.23        tv 	}
    218      1.23        tv 
    219      1.23        tv 	if (db_radix == 8)
    220      1.23        tv 		fmt = altflag ? "-%#qo" : "-%qo";
    221      1.23        tv 	else
    222      1.23        tv 		fmt = altflag ? "-%#qu" : "-%qu";
    223      1.23        tv 
    224      1.23        tv 	if (val < 0)
    225      1.23        tv 		val = -val;
    226      1.23        tv 	else
    227      1.23        tv 		++fmt;
    228      1.23        tv 
    229      1.23        tv 	snprintf(buf, bufsiz, fmt, val);
    230      1.23        tv }
    231      1.23        tv 
    232      1.23        tv /*
    233      1.23        tv  * Replacement for old '%z' kprintf format.
    234      1.23        tv  */
    235      1.23        tv void
    236  1.24.2.3   nathanw db_format_hex(char *buf, size_t bufsiz, quad_t val, int altflag)
    237      1.23        tv {
    238      1.23        tv 	/* Only use alternate form if val is nonzero. */
    239      1.23        tv 	const char *fmt = (altflag && val) ? "-%#qx" : "-%qx";
    240      1.23        tv 
    241      1.23        tv 	if (val < 0)
    242      1.23        tv 		val = -val;
    243      1.23        tv 	else
    244      1.23        tv 		++fmt;
    245      1.23        tv 
    246      1.23        tv 	snprintf(buf, bufsiz, fmt, val);
    247       1.1       cgd }
    248