📄 diag.cxx
字号:
/*========================================================================
//
// diag.c
//
// Infrastructure diagnostic output code
//
//========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
// Copyright (C) 2002 Gary Thomas
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): nickg,gthomas,jlarmour
// Contributors:
// Date: 1999-02-22
// Purpose: Infrastructure diagnostic output
// Description: Implementations of infrastructure diagnostic routines.
//
//####DESCRIPTIONEND####
//
//======================================================================*/
#include <pkgconf/system.h>
#include <pkgconf/hal.h>
#include <pkgconf/infra.h>
#include <cyg/infra/cyg_type.h> // base types
#include <cyg/infra/diag.h> // HAL polled output
#include <cyg/hal/hal_arch.h> // architectural stuff for...
#include <cyg/hal/hal_intr.h> // interrupt control
#include <cyg/hal/hal_diag.h> // diagnostic output routines
#include <stdarg.h>
#include <limits.h>
#ifdef CYG_HAL_DIAG_LOCK_DATA_DEFN
CYG_HAL_DIAG_LOCK_DATA_DEFN;
#endif
/*----------------------------------------------------------------------*/
externC void diag_write_num(
cyg_uint32 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
);
class Cyg_dummy_diag_init_class {
public:
Cyg_dummy_diag_init_class() {
HAL_DIAG_INIT();
}
};
// Initialize after HAL.
static Cyg_dummy_diag_init_class cyg_dummy_diag_init_obj
CYGBLD_ATTRIB_INIT_AFTER(CYG_INIT_HAL);
/*----------------------------------------------------------------------*/
/* Write single char to output */
externC void diag_write_char(char c)
{
/* Translate LF into CRLF */
if( c == '\n' )
{
HAL_DIAG_WRITE_CHAR('\r');
}
HAL_DIAG_WRITE_CHAR(c);
}
// Default wrapper function used by diag_printf
static void
_diag_write_char(char c, void **param)
{
diag_write_char(c);
}
/*----------------------------------------------------------------------*/
/* Initialize. Call to pull in diag initializing constructor */
externC void diag_init(void)
{
}
//
// This routine is used to send characters during 'printf()' functions.
// It can be replaced by providing a replacement via diag_init_putc().
//
static void (*_putc)(char c, void **param) = _diag_write_char;
void
diag_init_putc(void (*putc)(char c, void **param))
{
_putc = putc;
}
/*----------------------------------------------------------------------*/
/* Write zero terminated string */
externC void diag_write_string(const char *psz)
{
while( *psz ) diag_write_char( *psz++ );
}
/*----------------------------------------------------------------------*/
/* Write decimal value */
externC void diag_write_dec( cyg_int32 n)
{
cyg_ucount8 sign;
if( n < 0 ) n = -n, sign = '-';
else sign = '+';
diag_write_num( n, 10, sign, false, 0);
}
/*----------------------------------------------------------------------*/
/* Write hexadecimal value */
externC void diag_write_hex( cyg_uint32 n)
{
diag_write_num( n, 16, '+', false, 0);
}
/*----------------------------------------------------------------------*/
/* Generic number writing function */
/* The parameters determine what radix is used, the signed-ness of the */
/* number, its minimum width and whether it is zero or space filled on */
/* the left. */
externC void diag_write_long_num(
cyg_uint64 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
)
{
char buf[32];
cyg_count8 bpos;
char bufinit = pfzero?'0':' ';
char *digits = "0123456789ABCDEF";
/* init buffer to padding char: space or zero */
for( bpos = 0; bpos < (cyg_count8)sizeof(buf); bpos++ ) buf[bpos] = bufinit;
/* Set pos to start */
bpos = 0;
/* construct digits into buffer in reverse order */
if( n == 0 ) buf[bpos++] = '0';
else while( n != 0 )
{
cyg_ucount8 d = n % base;
buf[bpos++] = digits[d];
n /= base;
}
/* set pos to width if less. */
if( (cyg_count8)width > bpos ) bpos = width;
/* set sign if negative. */
if( sign == '-' )
{
if( buf[bpos-1] == bufinit ) bpos--;
buf[bpos] = sign;
}
else bpos--;
/* Now write it out in correct order. */
while( bpos >= 0 )
diag_write_char(buf[bpos--]);
}
externC void diag_write_num(
cyg_uint32 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
)
{
diag_write_long_num((long long)n, base, sign, pfzero, width);
}
/*----------------------------------------------------------------------*/
/* perform some simple sanity checks on a string to ensure that it */
/* consists of printable characters and is of reasonable length. */
static cyg_bool diag_check_string( const char *str )
{
cyg_bool result = true;
const char *s;
if( str == NULL ) return false;
for( s = str ; result && *s ; s++ )
{
char c = *s;
/* Check for a reasonable length string. */
if( s-str > 2048 ) result = false;
/* We only really support CR, NL, tab and backspace at present.
* If we want to use other special chars, this test will
* have to be expanded. */
if( c == '\n' || c == '\r' || c == '\b' || c == '\t' )
continue;
/* Check for printable chars. This assumes ASCII */
if( c < ' ' || c > '~' )
result = false;
}
return result;
}
/*----------------------------------------------------------------------*/
static int
_cvt(unsigned long long val, char *buf, long radix, char *digits)
{
char temp[80];
char *cp = temp;
int length = 0;
if (val == 0) {
/* Special case */
*cp++ = '0';
} else {
while (val) {
*cp++ = digits[val % radix];
val /= radix;
}
}
while (cp != temp) {
*buf++ = *--cp;
length++;
}
*buf = '\0';
return (length);
}
#define is_digit(c) ((c >= '0') && (c <= '9'))
static int
_vprintf(void (*putc)(char c, void **param), void **param, const char *fmt, va_list ap)
{
char buf[sizeof(long long)*8];
char c, sign, *cp=buf;
int left_prec, right_prec, zero_fill, pad, pad_on_right,
i, islong, islonglong;
long long val = 0;
int res = 0, length = 0;
if (!diag_check_string(fmt)) {
diag_write_string("<Bad format string: ");
diag_write_hex((cyg_uint32)fmt);
diag_write_string(" :");
for( i = 0; i < 8; i++ ) {
diag_write_char(' ');
val = va_arg(ap, unsigned long);
diag_write_hex(val);
}
diag_write_string(">\n");
return 0;
}
while ((c = *fmt++) != '\0') {
if (c == '%') {
c = *fmt++;
left_prec = right_prec = pad_on_right = islong = islonglong = 0;
if (c == '-') {
c = *fmt++;
pad_on_right++;
}
if (c == '0') {
zero_fill = true;
c = *fmt++;
} else {
zero_fill = false;
}
while (is_digit(c)) {
left_prec = (left_prec * 10) + (c - '0');
c = *fmt++;
}
if (c == '.') {
c = *fmt++;
zero_fill++;
while (is_digit(c)) {
right_prec = (right_prec * 10) + (c - '0');
c = *fmt++;
}
} else {
right_prec = left_prec;
}
sign = '\0';
if (c == 'l') {
// 'long' qualifier
c = *fmt++;
islong = 1;
if (c == 'l') {
// long long qualifier
c = *fmt++;
islonglong = 1;
}
}
if (c == 'z') {
c = *fmt++;
islong = sizeof(size_t) == sizeof(long);
}
// Fetch value [numeric descriptors only]
switch (c) {
case 'p':
islong = 1;
case 'd':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -