OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [infra/] [v2_0/] [src/] [diag.cxx] - Blame information for rev 201

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*========================================================================
2
//
3
//      diag.c
4
//
5
//      Infrastructure diagnostic output code
6
//
7
//========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    nickg,gthomas,jlarmour
45
// Contributors:
46
// Date:         1999-02-22
47
// Purpose:      Infrastructure diagnostic output
48
// Description:  Implementations of infrastructure diagnostic routines.
49
//
50
//####DESCRIPTIONEND####
51
//
52
//======================================================================*/
53
 
54
#include <pkgconf/system.h>
55
#include <pkgconf/hal.h>
56
#include <pkgconf/infra.h>
57
 
58
#include <cyg/infra/cyg_type.h>         // base types
59
 
60
#include <cyg/infra/diag.h>             // HAL polled output
61
#include <cyg/hal/hal_arch.h>           // architectural stuff for...
62
#include <cyg/hal/hal_intr.h>           // interrupt control
63
#include <cyg/hal/hal_diag.h>           // diagnostic output routines
64
#include <stdarg.h>
65
#include <limits.h>
66
 
67
#ifdef CYG_HAL_DIAG_LOCK_DATA_DEFN
68
CYG_HAL_DIAG_LOCK_DATA_DEFN;
69
#endif
70
 
71
/*----------------------------------------------------------------------*/
72
 
73
externC void diag_write_num(
74
    cyg_uint32  n,              /* number to write              */
75
    cyg_ucount8 base,           /* radix to write to            */
76
    cyg_ucount8 sign,           /* sign, '-' if -ve, '+' if +ve */
77
    cyg_bool    pfzero,         /* prefix with zero ?           */
78
    cyg_ucount8 width           /* min width of number          */
79
    );
80
 
81
class Cyg_dummy_diag_init_class {
82
public:
83
    Cyg_dummy_diag_init_class() {
84
        HAL_DIAG_INIT();
85
    }
86
};
87
 
88
// Initialize after HAL.
89
static Cyg_dummy_diag_init_class cyg_dummy_diag_init_obj
90
                                      CYGBLD_ATTRIB_INIT_AFTER(CYG_INIT_HAL);
91
 
92
/*----------------------------------------------------------------------*/
93
/* Write single char to output                                          */
94
 
95
externC void diag_write_char(char c)
96
{
97
    /* Translate LF into CRLF */
98
 
99
    if( c == '\n' )
100
    {
101
        HAL_DIAG_WRITE_CHAR('\r');
102
    }
103
 
104
    HAL_DIAG_WRITE_CHAR(c);
105
}
106
 
107
// Default wrapper function used by diag_printf
108
static void
109
_diag_write_char(char c, void **param)
110
{
111
    diag_write_char(c);
112
}
113
 
114
/*----------------------------------------------------------------------*/
115
/* Initialize. Call to pull in diag initializing constructor            */
116
 
117
externC void diag_init(void)
118
{
119
}
120
 
121
//
122
// This routine is used to send characters during 'printf()' functions.
123
// It can be replaced by providing a replacement via diag_init_putc().
124
//
125
static void (*_putc)(char c, void **param) = _diag_write_char;
126
 
127
void
128
diag_init_putc(void (*putc)(char c, void **param))
129
{
130
    _putc = putc;
131
}
132
 
133
/*----------------------------------------------------------------------*/
134
/* Write zero terminated string                                         */
135
 
136
externC void diag_write_string(const char *psz)
137
{
138
    while( *psz ) diag_write_char( *psz++ );
139
}
140
 
141
/*----------------------------------------------------------------------*/
142
/* Write decimal value                                                  */
143
 
144
externC void diag_write_dec( cyg_int32 n)
145
{
146
    cyg_ucount8 sign;
147
 
148
    if( n < 0 ) n = -n, sign = '-';
149
    else sign = '+';
150
 
151
    diag_write_num( n, 10, sign, false, 0);
152
}
153
 
154
/*----------------------------------------------------------------------*/
155
/* Write hexadecimal value                                              */
156
 
157
externC void diag_write_hex( cyg_uint32 n)
158
{
159
    diag_write_num( n, 16, '+', false, 0);
160
}
161
 
162
/*----------------------------------------------------------------------*/
163
/* Generic number writing function                                      */
164
/* The parameters determine what radix is used, the signed-ness of the  */
165
/* number, its minimum width and whether it is zero or space filled on  */
166
/* the left.                                                            */
167
 
168
externC void diag_write_long_num(
169
    cyg_uint64  n,              /* number to write              */
170
    cyg_ucount8 base,           /* radix to write to            */
171
    cyg_ucount8 sign,           /* sign, '-' if -ve, '+' if +ve */
172
    cyg_bool    pfzero,         /* prefix with zero ?           */
173
    cyg_ucount8 width           /* min width of number          */
174
    )
175
{
176
    char buf[32];
177
    cyg_count8 bpos;
178
    char bufinit = pfzero?'0':' ';
179
    char *digits = "0123456789ABCDEF";
180
 
181
    /* init buffer to padding char: space or zero */
182
    for( bpos = 0; bpos < (cyg_count8)sizeof(buf); bpos++ ) buf[bpos] = bufinit;
183
 
184
    /* Set pos to start */
185
    bpos = 0;
186
 
187
    /* construct digits into buffer in reverse order */
188
    if( n == 0 ) buf[bpos++] = '0';
189
    else while( n != 0 )
190
    {
191
        cyg_ucount8 d = n % base;
192
        buf[bpos++] = digits[d];
193
        n /= base;
194
    }
195
 
196
    /* set pos to width if less. */
197
    if( (cyg_count8)width > bpos ) bpos = width;
198
 
199
    /* set sign if negative. */
200
    if( sign == '-' )
201
    {
202
        if( buf[bpos-1] == bufinit ) bpos--;
203
        buf[bpos] = sign;
204
    }
205
    else bpos--;
206
 
207
    /* Now write it out in correct order. */
208
    while( bpos >= 0 )
209
        diag_write_char(buf[bpos--]);
210
}
211
 
212
externC void diag_write_num(
213
    cyg_uint32  n,              /* number to write              */
214
    cyg_ucount8 base,           /* radix to write to            */
215
    cyg_ucount8 sign,           /* sign, '-' if -ve, '+' if +ve */
216
    cyg_bool    pfzero,         /* prefix with zero ?           */
217
    cyg_ucount8 width           /* min width of number          */
218
    )
219
{
220
    diag_write_long_num((long long)n, base, sign, pfzero, width);
221
}
222
 
223
/*----------------------------------------------------------------------*/
224
/* perform some simple sanity checks on a string to ensure that it      */
225
/* consists of printable characters and is of reasonable length.        */
226
 
227
static cyg_bool diag_check_string( const char *str )
228
{
229
    cyg_bool result = true;
230
    const char *s;
231
 
232
    if( str == NULL ) return false;
233
 
234
    for( s = str ; result && *s ; s++ )
235
    {
236
        char c = *s;
237
 
238
        /* Check for a reasonable length string. */
239
 
240
        if( s-str > 2048 ) result = false;
241
 
242
        /* We only really support CR, NL, and backspace at present. If
243
         * we want to use tabs or other special chars, this test will
244
         * have to be expanded.  */
245
 
246
        if( c == '\n' || c == '\r' || c == '\b' )
247
            continue;
248
 
249
        /* Check for printable chars. This assumes ASCII */
250
 
251
        if( c < ' ' || c > '~' )
252
            result = false;
253
 
254
    }
255
 
256
    return result;
257
}
258
 
259
/*----------------------------------------------------------------------*/
260
 
261
static int
262
_cvt(unsigned long long val, char *buf, long radix, char *digits)
263
{
264
    char temp[80];
265
    char *cp = temp;
266
    int length = 0;
267
 
268
    if (val == 0) {
269
        /* Special case */
270
        *cp++ = '0';
271
    } else {
272
        while (val) {
273
            *cp++ = digits[val % radix];
274
            val /= radix;
275
        }
276
    }
277
    while (cp != temp) {
278
        *buf++ = *--cp;
279
        length++;
280
    }
281
    *buf = '\0';
282
    return (length);
283
}
284
 
285
#define is_digit(c) ((c >= '0') && (c <= '9'))
286
 
287
static int
288
_vprintf(void (*putc)(char c, void **param), void **param, const char *fmt, va_list ap)
289
{
290
    char buf[sizeof(long long)*8];
291
    char c, sign, *cp=buf;
292
    int left_prec, right_prec, zero_fill, pad, pad_on_right,
293
        i, islong, islonglong;
294
    long long val = 0;
295
    int res = 0, length = 0;
296
 
297
    if (!diag_check_string(fmt)) {
298
        diag_write_string("<Bad format string: ");
299
        diag_write_hex((cyg_uint32)fmt);
300
        diag_write_string(" :");
301
        for( i = 0; i < 8; i++ ) {
302
            diag_write_char(' ');
303
            val = va_arg(ap, unsigned long);
304
            diag_write_hex(val);
305
        }
306
        diag_write_string(">\n");
307
        return 0;
308
    }
309
    while ((c = *fmt++) != '\0') {
310
        if (c == '%') {
311
            c = *fmt++;
312
            left_prec = right_prec = pad_on_right = islong = islonglong = 0;
313
            if (c == '-') {
314
                c = *fmt++;
315
                pad_on_right++;
316
            }
317
            if (c == '0') {
318
                zero_fill = true;
319
                c = *fmt++;
320
            } else {
321
                zero_fill = false;
322
            }
323
            while (is_digit(c)) {
324
                left_prec = (left_prec * 10) + (c - '0');
325
                c = *fmt++;
326
            }
327
            if (c == '.') {
328
                c = *fmt++;
329
                zero_fill++;
330
                while (is_digit(c)) {
331
                    right_prec = (right_prec * 10) + (c - '0');
332
                    c = *fmt++;
333
                }
334
            } else {
335
                right_prec = left_prec;
336
            }
337
            sign = '\0';
338
            if (c == 'l') {
339
                // 'long' qualifier
340
                c = *fmt++;
341
                islong = 1;
342
                if (c == 'l') {
343
                    // long long qualifier
344
                    c = *fmt++;
345
                    islonglong = 1;
346
                }
347
            }
348
            // Fetch value [numeric descriptors only]
349
            switch (c) {
350
            case 'p':
351
                islong = 1;
352
            case 'd':
353
            case 'D':
354
            case 'x':
355
            case 'X':
356
            case 'u':
357
            case 'U':
358
            case 'b':
359
            case 'B':
360
                if (islonglong) {
361
                    val = va_arg(ap, long long);
362
                } else if (islong) {
363
                    val = (long long)va_arg(ap, long);
364
                } else{
365
                    val = (long long)va_arg(ap, int);
366
                }
367
                if ((c == 'd') || (c == 'D')) {
368
                    if (val < 0) {
369
                        sign = '-';
370
                        val = -val;
371
                    }
372
                } else {
373
                    // Mask to unsigned, sized quantity
374
                    if (islong) {
375
                        val &= ((long long)1 << (sizeof(long) * 8)) - 1;
376
                    } else{
377
                        val &= ((long long)1 << (sizeof(int) * 8)) - 1;
378
                    }
379
                }
380
                break;
381
            default:
382
                break;
383
            }
384
            // Process output
385
            switch (c) {
386
            case 'p':  // Pointer
387
                (*putc)('0', param);
388
                (*putc)('x', param);
389
                zero_fill = true;
390
                left_prec = sizeof(unsigned long)*2;
391
            case 'd':
392
            case 'D':
393
            case 'u':
394
            case 'U':
395
            case 'x':
396
            case 'X':
397
                switch (c) {
398
                case 'd':
399
                case 'D':
400
                case 'u':
401
                case 'U':
402
                    length = _cvt(val, buf, 10, "0123456789");
403
                    break;
404
                case 'p':
405
                case 'x':
406
                    length = _cvt(val, buf, 16, "0123456789abcdef");
407
                    break;
408
                case 'X':
409
                    length = _cvt(val, buf, 16, "0123456789ABCDEF");
410
                    break;
411
                }
412
                cp = buf;
413
                break;
414
            case 's':
415
            case 'S':
416
                cp = va_arg(ap, char *);
417
                if (cp == NULL)
418
                    cp = "<null>";
419
                else if (!diag_check_string(cp)) {
420
                    diag_write_string("<Not a string: 0x");
421
                    diag_write_hex((cyg_uint32)cp);
422
                    cp = ">";
423
                }
424
                length = 0;
425
                while (cp[length] != '\0') length++;
426
                break;
427
            case 'c':
428
            case 'C':
429
                c = va_arg(ap, int /*char*/);
430
                (*putc)(c, param);
431
                res++;
432
                continue;
433
            case 'b':
434
            case 'B':
435
                length = left_prec;
436
                if (left_prec == 0) {
437
                    if (islonglong)
438
                        length = sizeof(long long)*8;
439
                    else if (islong)
440
                        length = sizeof(long)*8;
441
                    else
442
                        length = sizeof(int)*8;
443
                }
444
                for (i = 0;  i < length-1;  i++) {
445
                    buf[i] = ((val & ((long long)1<<i)) ? '1' : '.');
446
                }
447
                cp = buf;
448
                break;
449
            case '%':
450
                (*putc)('%', param);
451
                break;
452
            default:
453
                (*putc)('%', param);
454
                (*putc)(c, param);
455
                res += 2;
456
            }
457
            pad = left_prec - length;
458
            if (sign != '\0') {
459
                pad--;
460
            }
461
            if (zero_fill) {
462
                c = '0';
463
                if (sign != '\0') {
464
                    (*putc)(sign, param);
465
                    res++;
466
                    sign = '\0';
467
                }
468
            } else {
469
                c = ' ';
470
            }
471
            if (!pad_on_right) {
472
                while (pad-- > 0) {
473
                    (*putc)(c, param);
474
                    res++;
475
                }
476
            }
477
            if (sign != '\0') {
478
                (*putc)(sign, param);
479
                res++;
480
            }
481
            while (length-- > 0) {
482
                c = *cp++;
483
                (*putc)(c, param);
484
                res++;
485
            }
486
            if (pad_on_right) {
487
                while (pad-- > 0) {
488
                    (*putc)(' ', param);
489
                    res++;
490
                }
491
            }
492
        } else {
493
            (*putc)(c, param);
494
            res++;
495
        }
496
    }
497
    return (res);
498
}
499
 
500
struct _sputc_info {
501
    char *ptr;
502
    int max, len;
503
};
504
 
505
static void
506
_sputc(char c, void **param)
507
{
508
    struct _sputc_info *info = (struct _sputc_info *)param;
509
 
510
    if (info->len < info->max) {
511
        *(info->ptr)++ = c;
512
        *(info->ptr) = '\0';
513
        info->len++;
514
    }
515
}
516
 
517
int
518
diag_sprintf(char *buf, const char *fmt, ...)
519
{
520
    int ret;
521
    va_list ap;
522
    struct _sputc_info info;
523
 
524
    va_start(ap, fmt);
525
    info.ptr = buf;
526
    info.max = 1024;  // Unlimited
527
    info.len = 0;
528
    ret = _vprintf(_sputc, (void **)&info, fmt, ap);
529
    va_end(ap);
530
    return (info.len);
531
}
532
 
533
int
534
diag_snprintf(char *buf, size_t len, const char *fmt, ...)
535
{
536
    int ret;
537
    va_list ap;
538
    struct _sputc_info info;
539
 
540
    va_start(ap, fmt);
541
    info.ptr = buf;
542
    info.len = 0;
543
    info.max = len;
544
    ret = _vprintf(_sputc, (void **)&info, fmt, ap);
545
    va_end(ap);
546
    return (info.len);
547
}
548
 
549
int
550
diag_vsprintf(char *buf, const char *fmt, va_list ap)
551
{
552
    int ret;
553
    struct _sputc_info info;
554
 
555
    info.ptr = buf;
556
    info.max = 1024;  // Unlimited
557
    info.len = 0;
558
    ret = _vprintf(_sputc, (void **)&info, fmt, ap);
559
    return (info.len);
560
}
561
 
562
int
563
diag_printf(const char *fmt, ...)
564
{
565
    va_list ap;
566
    int ret;
567
 
568
    va_start(ap, fmt);
569
    ret = _vprintf(_putc, (void **)0, fmt, ap);
570
    va_end(ap);
571
    return (ret);
572
}
573
 
574
int
575
diag_vprintf(const char *fmt, va_list ap)
576
{
577
    int ret;
578
 
579
    ret = _vprintf(_putc, (void **)0, fmt, ap);
580
    return (ret);
581
}
582
 
583
void
584
diag_vdump_buf_with_offset(__printf_fun *pf,
585
                           cyg_uint8     *p,
586
                           CYG_ADDRWORD   s,
587
                           cyg_uint8     *base)
588
{
589
    int i, c;
590
    if ((CYG_ADDRWORD)s > (CYG_ADDRWORD)p) {
591
        s = (CYG_ADDRWORD)s - (CYG_ADDRWORD)p;
592
    }
593
    while ((int)s > 0) {
594
        if (base) {
595
            (*pf)("%08X: ", (CYG_ADDRWORD)p - (CYG_ADDRWORD)base);
596
        } else {
597
            (*pf)("%08X: ", p);
598
        }
599
        for (i = 0;  i < 16;  i++) {
600
            if (i < (int)s) {
601
                (*pf)("%02X ", p[i] & 0xFF);
602
            } else {
603
                (*pf)("   ");
604
            }
605
            if (i == 7) (*pf)(" ");
606
        }
607
        (*pf)(" |");
608
        for (i = 0;  i < 16;  i++) {
609
            if (i < (int)s) {
610
                c = p[i] & 0xFF;
611
                if ((c < 0x20) || (c >= 0x7F)) c = '.';
612
            } else {
613
                c = ' ';
614
            }
615
            (*pf)("%c", c);
616
        }
617
        (*pf)("|\n");
618
        s -= 16;
619
        p += 16;
620
    }
621
}
622
 
623
void
624
diag_dump_buf_with_offset(cyg_uint8     *p,
625
                          CYG_ADDRWORD   s,
626
                          cyg_uint8     *base)
627
{
628
    diag_vdump_buf_with_offset(diag_printf, p, s, base);
629
}
630
 
631
void
632
diag_dump_buf(void *p, CYG_ADDRWORD s)
633
{
634
   diag_dump_buf_with_offset((cyg_uint8 *)p, s, 0);
635
}
636
 
637
void
638
diag_dump_buf_with_offset_32bit(cyg_uint32   *p,
639
                                CYG_ADDRWORD  s,
640
                                cyg_uint32   *base)
641
{
642
    int i;
643
    if ((CYG_ADDRWORD)s > (CYG_ADDRWORD)p) {
644
        s = (CYG_ADDRWORD)s - (CYG_ADDRWORD)p;
645
    }
646
    while ((int)s > 0) {
647
        if (base) {
648
            diag_printf("%08X: ", (CYG_ADDRWORD)p - (CYG_ADDRWORD)base);
649
        } else {
650
            diag_printf("%08X: ", p);
651
        }
652
        for (i = 0;  i < 4;  i++) {
653
            if (i < (int)s/4) {
654
                diag_printf("%08X ", p[i] );
655
            } else {
656
                diag_printf("         ");
657
            }
658
        }
659
        diag_printf("\n");
660
        s -= 16;
661
        p += 4;
662
    }
663
}
664
 
665
externC void
666
diag_dump_buf_32bit(void *p, CYG_ADDRWORD s)
667
{
668
   diag_dump_buf_with_offset_32bit((cyg_uint32 *)p, s, 0);
669
}
670
 
671
void
672
diag_dump_buf_with_offset_16bit(cyg_uint16   *p,
673
                                CYG_ADDRWORD  s,
674
                                cyg_uint16   *base)
675
{
676
    int i;
677
    if ((CYG_ADDRWORD)s > (CYG_ADDRWORD)p) {
678
        s = (CYG_ADDRWORD)s - (CYG_ADDRWORD)p;
679
    }
680
    while ((int)s > 0) {
681
        if (base) {
682
            diag_printf("%08X: ", (CYG_ADDRWORD)p - (CYG_ADDRWORD)base);
683
        } else {
684
            diag_printf("%08X: ", p);
685
        }
686
        for (i = 0;  i < 8;  i++) {
687
            if (i < (int)s/2) {
688
              diag_printf("%04X ", p[i] );
689
              if (i == 3) diag_printf(" ");
690
            } else {
691
              diag_printf("     ");
692
            }
693
        }
694
        diag_printf("\n");
695
        s -= 16;
696
        p += 8;
697
    }
698
}
699
 
700
externC void
701
diag_dump_buf_16bit(void *p, CYG_ADDRWORD s)
702
{
703
   diag_dump_buf_with_offset_16bit((cyg_uint16 *)p, s, 0);
704
}
705
 
706
/*-----------------------------------------------------------------------*/
707
/* EOF infra/diag.c */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.