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