Hatena::Groupcprogramming

Going My C Way このページをアンテナに追加 RSSフィード

2010年11月07日(日)

デバッグ用マクロ put_d()

| 09:02 |  デバッグ用マクロ put_d() - Going My C Way を含むブックマーク はてなブックマーク -  デバッグ用マクロ put_d() - Going My C Way  デバッグ用マクロ put_d() - Going My C Way のブックマークコメント

今まで使っていたデバッグマクロ put_d()とその仲間をヘッダにまとめました。

今後はこのヘッダをインクルードして使います。

ヘッダ ezput.h。

#ifndef INCLUDED_EZPUT_H
#define INCLUDED_EZPUT_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdio.h>  /* printf()  */
#include <ctype.h>  /* isprint() */

/*
    macro
*/
#define put_d(v_)       (printf(#v_ " = %d\n",  (v_)))
#define put_i(v_)       (printf(#v_ " = %+i\n", (v_)))
#define put_u(v_)       (printf(#v_ " = %u\n",  (v_)))
#define put_o(v_)       (printf(#v_ " = %#o\n", (v_)))
#define put_x(v_)       (printf(#v_ " = %#x\n", (v_)))

#define put_hd(v_)      (printf(#v_ " = %hd\n",  (v_)))
#define put_hi(v_)      (printf(#v_ " = %+hi\n", (v_)))
#define put_hu(v_)      (printf(#v_ " = %hu\n",  (v_)))
#define put_ho(v_)      (printf(#v_ " = %#ho\n", (v_)))
#define put_hx(v_)      (printf(#v_ " = %#hx\n", (v_)))

#define put_ld(v_)      (printf(#v_ " = %ld\n",  (v_)))
#define put_li(v_)      (printf(#v_ " = %+li\n", (v_)))
#define put_lu(v_)      (printf(#v_ " = %lu\n",  (v_)))
#define put_lo(v_)      (printf(#v_ " = %#lo\n", (v_)))
#define put_lx(v_)      (printf(#v_ " = %#lx\n", (v_)))

#define put_02x(v_)     (printf(#v_ " = 0x%02x\n", (v_)))
#define put_04x(v_)     (printf(#v_ " = 0x%04x\n", (v_)))
#define put_08x(v_)     (printf(#v_ " = 0x%08x\n", (v_)))

#define put_02hx(v_)    (printf(#v_ " = 0x%02hx\n", (v_)))
#define put_04hx(v_)    (printf(#v_ " = 0x%04hx\n", (v_)))
#define put_08hx(v_)    (printf(#v_ " = 0x%08hx\n", (v_)))

#define put_02lx(v_)    (printf(#v_ " = 0x%02lx\n", (v_)))
#define put_04lx(v_)    (printf(#v_ " = 0x%04lx\n", (v_)))
#define put_08lx(v_)    (printf(#v_ " = 0x%08lx\n", (v_)))

#define put_p(v_)       (printf(#v_ " = (%p)\n", (v_)))

#define put_s(v_)       (printf(#v_ " = %s%s%s\n"       , \
                                (v_) ? "\"" : ""     ,\
                                (v_) ? (v_) : "NULL" ,\
                                (v_) ? "\"" : ""     ))

#define put_c(v_)       (printf(#v_ " = %s%c%s[%#02x]\n"  ,\
                                isprint(v_) ? "'"  : " "  ,\
                                isprint(v_) ? (v_) : '.'  ,\
                                isprint(v_) ? "'"  : " "  ,\
                                (v_) & ((unsigned char)~0U)))

#define put_bool(v_)    (printf(#v_ " = %s\n", (v_) ? "true" : "false"))

#define put_8bit(v_)    (printf(#v_ " = %u%u%u%u:%u%u%u%u\n",   \
                                        ((v_) >> 7) & 1U,       \
                                        ((v_) >> 6) & 1U,       \
                                        ((v_) >> 5) & 1U,       \
                                        ((v_) >> 4) & 1U,       \
                                        ((v_) >> 3) & 1U,       \
                                        ((v_) >> 2) & 1U,       \
                                        ((v_) >> 1) & 1U,       \
                                         (v_)       & 1U        ))

#define put_16bit(v_)   (printf(#v_ " = %u%u%u%u:%u%u%u%u"      \
                                      " %u%u%u%u:%u%u%u%u\n",   \
                                        ((v_) >> 15) & 1U,      \
                                        ((v_) >> 14) & 1U,      \
                                        ((v_) >> 13) & 1U,      \
                                        ((v_) >> 12) & 1U,      \
                                        ((v_) >> 11) & 1U,      \
                                        ((v_) >> 10) & 1U,      \
                                        ((v_) >>  9) & 1U,      \
                                        ((v_) >>  8) & 1U,      \
                                        ((v_) >>  7) & 1U,      \
                                        ((v_) >>  6) & 1U,      \
                                        ((v_) >>  5) & 1U,      \
                                        ((v_) >>  4) & 1U,      \
                                        ((v_) >>  3) & 1U,      \
                                        ((v_) >>  2) & 1U,      \
                                        ((v_) >>  1) & 1U,      \
                                         (v_)        & 1U       ))

#define put_32bit(v_)   (printf(#v_ " = %u%u%u%u:%u%u%u%u"      \
                                      " %u%u%u%u:%u%u%u%u"      \
                                      " %u%u%u%u:%u%u%u%u"      \
                                      " %u%u%u%u:%u%u%u%u\n",   \
                                        ((v_) >> 31) & 1U,      \
                                        ((v_) >> 30) & 1U,      \
                                        ((v_) >> 29) & 1U,      \
                                        ((v_) >> 28) & 1U,      \
                                        ((v_) >> 27) & 1U,      \
                                        ((v_) >> 26) & 1U,      \
                                        ((v_) >> 25) & 1U,      \
                                        ((v_) >> 24) & 1U,      \
                                        ((v_) >> 23) & 1U,      \
                                        ((v_) >> 22) & 1U,      \
                                        ((v_) >> 21) & 1U,      \
                                        ((v_) >> 20) & 1U,      \
                                        ((v_) >> 19) & 1U,      \
                                        ((v_) >> 18) & 1U,      \
                                        ((v_) >> 17) & 1U,      \
                                        ((v_) >> 16) & 1U,      \
                                        ((v_) >> 15) & 1U,      \

                                        ((v_) >> 14) & 1U,      \
                                        ((v_) >> 13) & 1U,      \
                                        ((v_) >> 12) & 1U,      \
                                        ((v_) >> 11) & 1U,      \
                                        ((v_) >> 10) & 1U,      \
                                        ((v_) >>  9) & 1U,      \
                                        ((v_) >>  8) & 1U,      \
                                        ((v_) >>  7) & 1U,      \
                                        ((v_) >>  6) & 1U,      \
                                        ((v_) >>  5) & 1U,      \
                                        ((v_) >>  4) & 1U,      \
                                        ((v_) >>  3) & 1U,      \
                                        ((v_) >>  2) & 1U,      \
                                        ((v_) >>  1) & 1U,      \
                                         (v_)        & 1U       ))

#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) || defined(__GNUC__)
    #define put_func()      (printf("%s()\n", __func__))
#else
    #define put_func()      (printf("function:\n"))
#endif

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* ! INCLUDED_EZPUT_H */

put_*() マクロは値のダンプが目的です。マクロの中で引数を期待する方にキャストする処置は基本的に行ないません。

必要な場合は引数で渡す時にキャストしましょう。(下記テスト用ソース参照)


テスト用ソース ezput.h。

#include <stdio.h>  /* puts() */
#include <stdlib.h> /* EXIT_SUCCESS */
#include "ezput.h"

int main(void)
{
    int            i  = 10;
    int            m  = -20;
    short          h  = 10;
    long           l  = 10L;
    unsigned       u  = 10U;
    unsigned short hu = 10;
    unsigned long  lu = 10L;

    char *s = "hello";
    char *n = NULL;

    char c   = 'A';
    char sp  = ' ';
    char tab = '\t';
    char period = '.';

    int t = !0;
    int f = 0;

    int b8  = 0x12;
    int b16 = 0x1234;
    int b32 = 0x12345678;

    puts("----");
    put_func();

    puts("----");

    put_d(i);
    put_i(i);
    put_d(m);
    put_i(m);

    puts("----");

    put_hd(h);
    put_hi(h);
    put_ld(l);
    put_li(l);

    puts("----");
    put_u(u);
    put_o(u);
    put_x(u);

    puts("----");
    put_hu(hu);
    put_ho(hu);
    put_hx(hu);

    puts("----");

    put_lu(lu);
    put_lo(lu);
    put_lx(lu);

    puts("----");

    put_02x(b8);
    put_04x(b16);
    put_08x(b32);

    puts("----");

    put_02hx((unsigned short)b8);
    put_04hx((unsigned short)b16);
    put_08hx((unsigned short)b32);

    puts("----");

    put_02lx((unsigned long)b8);
    put_04lx((unsigned long)b16);
    put_08lx((unsigned long)b32);

    puts("----");

    put_s(s);
    put_s(n);

    puts("----");
    put_p(s);
    put_p(n);

    puts("----");

    put_c(c);
    put_c(sp);
    put_c(tab);
    put_c(period);

    puts("----");

    put_bool(t);
    put_bool(f);

    puts("----");
    put_8bit(b8);
    puts("----");
    put_16bit(b16);
    puts("----");
    put_32bit(b32);

    return EXIT_SUCCESS;
}

テスト。

$ make ezput    # Makefile はいつも使っているもの
   :

$ ./ezput 
----
main()
----
i = 10
i = +10
m = -20
m = -20
----
h = 10
h = +10
l = 10
l = +10
----
u = 10
u = 012
u = 0xa
----
hu = 10
hu = 012
hu = 0xa
----
lu = 10
lu = 012
lu = 0xa
----
b8 = 0x12
b16 = 0x1234
b32 = 0x12345678
----
(unsigned short)b8 = 0x12
(unsigned short)b16 = 0x1234
(unsigned short)b32 = 0x00005678
----
(unsigned long)b8 = 0x12
(unsigned long)b16 = 0x1234
(unsigned long)b32 = 0x12345678
----
s = "hello"
n = NULL
----
s = (0x8049410)
n = ((nil))
----
c = 'A'[0x41]
sp = ' '[0x20]
tab =  . [0x9]
period = '.'[0x2e]
----
t = true
f = false
----
b8 = 0001:0010
----
b16 = 0001:0010 0011:0100
----
b32 = 0001:0010 0011:0100 0101:0110 0111:1000