Home | History | Annotate | Line # | Download | only in ddb
db_output.c revision 1.31
      1  1.31     joerg /*	$NetBSD: db_output.c,v 1.31 2011/07/17 20:54:50 joerg Exp $	*/
      2   1.7       cgd 
      3  1.27    simonb /*
      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.27    simonb  *
      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.27    simonb  *
     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.27    simonb  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.27    simonb  *
     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.27    simonb  *
     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.26     lukem 
     33  1.26     lukem #include <sys/cdefs.h>
     34  1.31     joerg __KERNEL_RCSID(0, "$NetBSD: db_output.c,v 1.31 2011/07/17 20:54:50 joerg Exp $");
     35  1.26     lukem 
     36   1.6   mycroft #include <sys/param.h>
     37  1.16       cgd #include <sys/systm.h>
     38  1.31     joerg #include <sys/stdarg.h>
     39   1.1       cgd 
     40  1.10  christos #include <dev/cons.h>
     41  1.10  christos 
     42  1.30        ad #include <ddb/ddb.h>
     43  1.10  christos 
     44   1.1       cgd /*
     45   1.1       cgd  *	Character output - tracks position in line.
     46   1.1       cgd  *	To do this correctly, we should know how wide
     47   1.1       cgd  *	the output device is - then we could zero
     48   1.1       cgd  *	the line position when the output device wraps
     49   1.1       cgd  *	around to the start of the next line.
     50   1.1       cgd  *
     51   1.1       cgd  *	Instead, we count the number of spaces printed
     52   1.1       cgd  *	since the last printing character so that we
     53   1.1       cgd  *	don't print trailing spaces.  This avoids most
     54   1.1       cgd  *	of the wraparounds.
     55   1.1       cgd  */
     56   1.4    brezak 
     57   1.4    brezak #ifndef	DB_MAX_LINE
     58   1.4    brezak #define	DB_MAX_LINE		24	/* maximum line */
     59  1.25       mrg #endif	/* DB_MAX_LINE */
     60  1.25       mrg #ifndef	DB_MAX_WIDTH
     61   1.4    brezak #define DB_MAX_WIDTH		80	/* maximum width */
     62  1.25       mrg #endif	/* DB_MAX_WIDTH */
     63   1.4    brezak 
     64   1.4    brezak #define DB_MIN_MAX_WIDTH	20	/* minimum max width */
     65   1.4    brezak #define DB_MIN_MAX_LINE		3	/* minimum max line */
     66   1.4    brezak #define CTRL(c)			((c) & 0xff)
     67   1.4    brezak 
     68  1.27    simonb int	db_output_line = 0;			/* output line number */
     69  1.27    simonb int	db_tab_stop_width = 8;			/* how wide are tab stops? */
     70  1.27    simonb int	db_max_line = DB_MAX_LINE;		/* output max lines */
     71  1.27    simonb int	db_max_width = DB_MAX_WIDTH;		/* output line width */
     72  1.27    simonb 
     73  1.27    simonb static int	db_output_position = 0;		/* output column */
     74  1.27    simonb static int	db_last_non_space = 0;		/* last non-space character */
     75   1.1       cgd 
     76  1.27    simonb static void db_more(void);
     77   1.1       cgd 
     78   1.1       cgd /*
     79   1.1       cgd  * Force pending whitespace.
     80   1.1       cgd  */
     81   1.1       cgd void
     82  1.27    simonb db_force_whitespace(void)
     83   1.1       cgd {
     84  1.22  augustss 	int last_print, next_tab;
     85   1.1       cgd 
     86   1.1       cgd 	last_print = db_last_non_space;
     87   1.1       cgd 	while (last_print < db_output_position) {
     88  1.27    simonb 		next_tab = DB_NEXT_TAB(last_print);
     89  1.27    simonb 		if (next_tab <= db_output_position) {
     90  1.27    simonb 			while (last_print < next_tab) { /* DON'T send a tab!! */
     91  1.27    simonb 				cnputc(' ');
     92  1.27    simonb 				last_print++;
     93  1.27    simonb 			}
     94  1.27    simonb 		} else {
     95   1.2       cgd 			cnputc(' ');
     96   1.2       cgd 			last_print++;
     97   1.2       cgd 		}
     98   1.1       cgd 	}
     99   1.1       cgd 	db_last_non_space = db_output_position;
    100   1.1       cgd }
    101   1.1       cgd 
    102   1.4    brezak static void
    103  1.27    simonb db_more(void)
    104   1.4    brezak {
    105  1.28  christos 	const char *p;
    106   1.4    brezak 	int quit_output = 0;
    107   1.4    brezak 
    108   1.4    brezak 	for (p = "--db_more--"; *p; p++)
    109  1.27    simonb 		cnputc(*p);
    110   1.4    brezak 	switch(cngetc()) {
    111   1.4    brezak 	case ' ':
    112  1.27    simonb 		db_output_line = 0;
    113  1.27    simonb 		break;
    114   1.4    brezak 	case 'q':
    115   1.4    brezak 	case CTRL('c'):
    116  1.27    simonb 		db_output_line = 0;
    117  1.27    simonb 		quit_output = 1;
    118  1.27    simonb 		break;
    119   1.4    brezak 	default:
    120  1.27    simonb 		db_output_line--;
    121  1.27    simonb 		break;
    122   1.4    brezak 	}
    123   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";
    124   1.4    brezak 	while (*p)
    125  1.27    simonb 		cnputc(*p++);
    126   1.4    brezak 	if (quit_output) {
    127  1.27    simonb 		db_error(0);
    128  1.27    simonb 		/* NOTREACHED */
    129   1.4    brezak 	}
    130   1.4    brezak }
    131   1.4    brezak 
    132   1.1       cgd /*
    133   1.1       cgd  * Output character.  Buffer whitespace.
    134   1.1       cgd  */
    135  1.10  christos void
    136  1.27    simonb db_putchar(int c)
    137   1.1       cgd {
    138   1.4    brezak 	if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
    139  1.27    simonb 		db_more();
    140   1.1       cgd 	if (c > ' ' && c <= '~') {
    141  1.27    simonb 		/*
    142  1.27    simonb 		 * Printing character.
    143  1.27    simonb 		 * If we have spaces to print, print them first.
    144  1.27    simonb 		 * Use tabs if possible.
    145  1.27    simonb 		 */
    146  1.27    simonb 		db_force_whitespace();
    147  1.27    simonb 		cnputc(c);
    148  1.27    simonb 		db_output_position++;
    149  1.27    simonb 		if (db_max_width >= DB_MIN_MAX_WIDTH
    150  1.27    simonb 		    && db_output_position >= db_max_width) {
    151  1.27    simonb 			/* auto new line */
    152  1.27    simonb 			cnputc('\n');
    153  1.27    simonb 			db_output_position = 0;
    154  1.27    simonb 			db_last_non_space = 0;
    155  1.27    simonb 			db_output_line++;
    156  1.27    simonb 		}
    157  1.27    simonb 		db_last_non_space = db_output_position;
    158  1.27    simonb 	} else if (c == '\n') {
    159  1.27    simonb 		/* Return */
    160  1.27    simonb 		cnputc(c);
    161   1.4    brezak 		db_output_position = 0;
    162   1.4    brezak 		db_last_non_space = 0;
    163   1.4    brezak 		db_output_line++;
    164  1.27    simonb 		db_check_interrupt();
    165  1.27    simonb 	} else if (c == '\t') {
    166  1.27    simonb 		/* assume tabs every 8 positions */
    167  1.27    simonb 		db_output_position = DB_NEXT_TAB(db_output_position);
    168  1.27    simonb 	} else if (c == ' ') {
    169  1.27    simonb 		/* space */
    170  1.27    simonb 		db_output_position++;
    171  1.27    simonb 	} else if (c == '\007') {
    172  1.27    simonb 		/* bell */
    173  1.27    simonb 		cnputc(c);
    174   1.1       cgd 	}
    175   1.1       cgd 	/* other characters are assumed non-printing */
    176   1.1       cgd }
    177   1.1       cgd 
    178   1.1       cgd /*
    179   1.1       cgd  * Return output position
    180   1.1       cgd  */
    181   1.1       cgd int
    182  1.27    simonb db_print_position(void)
    183   1.1       cgd {
    184  1.27    simonb 
    185   1.1       cgd 	return (db_output_position);
    186   1.1       cgd }
    187   1.1       cgd 
    188   1.1       cgd /*
    189   1.4    brezak  * End line if too long.
    190   1.4    brezak  */
    191   1.4    brezak void
    192  1.27    simonb db_end_line(void)
    193   1.4    brezak {
    194  1.27    simonb 
    195   1.4    brezak 	if (db_output_position >= db_max_width)
    196  1.27    simonb 		db_printf("\n");
    197  1.23        tv }
    198  1.23        tv 
    199  1.23        tv /*
    200  1.23        tv  * Replacement for old '%r' kprintf format.
    201  1.23        tv  */
    202  1.23        tv void
    203  1.27    simonb db_format_radix(char *buf, size_t bufsiz, quad_t val, int altflag)
    204  1.23        tv {
    205  1.23        tv 	const char *fmt;
    206  1.23        tv 
    207  1.23        tv 	if (db_radix == 16) {
    208  1.23        tv 		db_format_hex(buf, bufsiz, val, altflag);
    209  1.23        tv 		return;
    210  1.23        tv 	}
    211  1.23        tv 
    212  1.23        tv 	if (db_radix == 8)
    213  1.23        tv 		fmt = altflag ? "-%#qo" : "-%qo";
    214  1.23        tv 	else
    215  1.23        tv 		fmt = altflag ? "-%#qu" : "-%qu";
    216  1.23        tv 
    217  1.23        tv 	if (val < 0)
    218  1.23        tv 		val = -val;
    219  1.23        tv 	else
    220  1.23        tv 		++fmt;
    221  1.23        tv 
    222  1.23        tv 	snprintf(buf, bufsiz, fmt, val);
    223  1.23        tv }
    224  1.23        tv 
    225  1.23        tv /*
    226  1.23        tv  * Replacement for old '%z' kprintf format.
    227  1.23        tv  */
    228  1.23        tv void
    229  1.27    simonb db_format_hex(char *buf, size_t bufsiz, quad_t val, int altflag)
    230  1.23        tv {
    231  1.23        tv 	/* Only use alternate form if val is nonzero. */
    232  1.23        tv 	const char *fmt = (altflag && val) ? "-%#qx" : "-%qx";
    233  1.23        tv 
    234  1.23        tv 	if (val < 0)
    235  1.23        tv 		val = -val;
    236  1.23        tv 	else
    237  1.23        tv 		++fmt;
    238  1.23        tv 
    239  1.23        tv 	snprintf(buf, bufsiz, fmt, val);
    240   1.1       cgd }
    241