[BACK]Return to dbg.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/dbg.c, Revision 1.1

1.1     ! ratchov     1: #ifdef DEBUG
        !             2: /*
        !             3:  * Copyright (c) 2003-2007 Alexandre Ratchov <alex@caoua.org>
        !             4:  * All rights reserved.
        !             5:  *
        !             6:  * Redistribution and use in source and binary forms, with or without
        !             7:  * modification, are permitted provided that the following conditions
        !             8:  * are met:
        !             9:  *
        !            10:  *     - Redistributions of source code must retain the above
        !            11:  *       copyright notice, this list of conditions and the
        !            12:  *       following disclaimer.
        !            13:  *
        !            14:  *     - Redistributions in binary form must reproduce the above
        !            15:  *       copyright notice, this list of conditions and the
        !            16:  *       following disclaimer in the documentation and/or other
        !            17:  *       materials provided with the distribution.
        !            18:  *
        !            19:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
        !            20:  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
        !            21:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
        !            22:  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
        !            23:  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
        !            24:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
        !            25:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        !            26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        !            27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        !            29:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            30:  */
        !            31: /*
        !            32:  * dbg_xxx() routines are used to quickly store traces into a trace buffer.
        !            33:  * This allows trances to be collected during time sensitive operations without
        !            34:  * disturbing them. The buffer can be flushed on standard error later, when
        !            35:  * slow syscalls are no longer disruptive, e.g. at the end of the poll() loop.
        !            36:  */
        !            37: #include <stdlib.h>
        !            38: #include <unistd.h>
        !            39: #include <fcntl.h>
        !            40: #include "dbg.h"
        !            41:
        !            42: /*
        !            43:  * size of the buffer where traces are stored
        !            44:  */
        !            45: #define DBG_BUFSZ      8192
        !            46:
        !            47: /*
        !            48:  * store a character in the trace buffer
        !            49:  */
        !            50: #define DBG_PUTC(c) do {                       \
        !            51:        if (dbg_used < DBG_BUFSZ)               \
        !            52:                dbg_buf[dbg_used++] = (c);      \
        !            53: } while (0)
        !            54:
        !            55: char dbg_buf[DBG_BUFSZ];       /* buffer where traces are stored */
        !            56: unsigned dbg_used = 0;         /* bytes used in the buffer */
        !            57: unsigned dbg_sync = 1;         /* if true, flush after each '\n' */
        !            58:
        !            59: /*
        !            60:  * write debug info buffer on stderr
        !            61:  */
        !            62: void
        !            63: dbg_flush(void)
        !            64: {
        !            65:        if (dbg_used ==  0)
        !            66:                return;
        !            67:        write(STDERR_FILENO, dbg_buf, dbg_used);
        !            68:        dbg_used = 0;
        !            69: }
        !            70:
        !            71: /*
        !            72:  * store a string in the debug buffer
        !            73:  */
        !            74: void
        !            75: dbg_puts(char *msg)
        !            76: {
        !            77:        char *p = msg;
        !            78:        int c;
        !            79:
        !            80:        while ((c = *p++) != '\0') {
        !            81:                DBG_PUTC(c);
        !            82:                if (dbg_sync && c == '\n')
        !            83:                        dbg_flush();
        !            84:        }
        !            85: }
        !            86:
        !            87: /*
        !            88:  * store a hex in the debug buffer
        !            89:  */
        !            90: void
        !            91: dbg_putx(unsigned long num)
        !            92: {
        !            93:        char dig[sizeof(num) * 2], *p = dig, c;
        !            94:        unsigned ndig;
        !            95:
        !            96:        if (num != 0) {
        !            97:                for (ndig = 0; num != 0; ndig++) {
        !            98:                        *p++ = num & 0xf;
        !            99:                        num >>= 4;
        !           100:                }
        !           101:                for (; ndig != 0; ndig--) {
        !           102:                        c = *(--p);
        !           103:                        c += (c < 10) ? '0' : 'a' - 10;
        !           104:                        DBG_PUTC(c);
        !           105:                }
        !           106:        } else
        !           107:                DBG_PUTC('0');
        !           108: }
        !           109:
        !           110: /*
        !           111:  * store a decimal in the debug buffer
        !           112:  */
        !           113: void
        !           114: dbg_putu(unsigned long num)
        !           115: {
        !           116:        char dig[sizeof(num) * 3], *p = dig;
        !           117:        unsigned ndig;
        !           118:
        !           119:        if (num != 0) {
        !           120:                for (ndig = 0; num != 0; ndig++) {
        !           121:                        *p++ = num % 10;
        !           122:                        num /= 10;
        !           123:                }
        !           124:                for (; ndig != 0; ndig--)
        !           125:                        DBG_PUTC(*(--p) + '0');
        !           126:        } else
        !           127:                DBG_PUTC('0');
        !           128: }
        !           129:
        !           130: /*
        !           131:  * store a signed integer in the trace buffer
        !           132:  */
        !           133: void
        !           134: dbg_puti(long num)
        !           135: {
        !           136:        if (num < 0) {
        !           137:                DBG_PUTC('-');
        !           138:                num = -num;
        !           139:        }
        !           140:        dbg_putu(num);
        !           141: }
        !           142:
        !           143: /*
        !           144:  * abort program execution after a fatal error, we should
        !           145:  * put code here to backup user data
        !           146:  */
        !           147: void
        !           148: dbg_panic(void)
        !           149: {
        !           150:        dbg_flush();
        !           151:        abort();
        !           152: }
        !           153: #endif