232f29fc71ba9986fbcf7b51ce25735e97b730d7
[libfirm] / ir / common / debug.h
1 /**
2  * Debug facility.
3  * @author Michael Beck, Sebastian Hack
4  * @date 15.12.2004
5  */
6
7 #ifndef _FIRM_DEBUG_H
8 #define _FIRM_DEBUG_H
9
10 #include "firm_config.h"
11
12 #ifdef WITH_LIBCORE
13
14 #define DBG(x) _LC_DBG(x)
15 #include <libcore/lc_debug.h>
16
17 /* use the newer debug implementation in libcore */
18 typedef lc_dbg_module_t firm_dbg_module_t;
19
20 extern firm_dbg_module_t *firm_dbg_register(const char *name);
21
22 #define firm_dbg_set_mask(module, mask) lc_dbg_set_mask(module, mask)
23 #define firm_dbg_get_mask(module)       lc_dbg_get_mask(module)
24 #define firm_dbg_set_file(module, file) lc_dbg_set_file(module, file)
25
26 #define LEVEL_DEFAULT    LC_LEVEL_DEFAULT
27 #define LEVEL_1          LC_LEVEL_1
28 #define LEVEL_2          LC_LEVEL_2
29 #define LEVEL_3          LC_LEVEL_3
30 #define LEVEL_4          LC_LEVEL_4
31 #define LEVEL_5          LC_LEVEL_5
32 #define SET_LEVEL_0      LC_SET_LEVEL_0
33 #define SET_LEVEL_1      LC_SET_LEVEL_1
34 #define SET_LEVEL_2      LC_SET_LEVEL_2
35 #define SET_LEVEL_3      LC_SET_LEVEL_3
36 #define SET_LEVEL_4      LC_SET_LEVEL_4
37 #define SET_LEVEL_5      LC_SET_LEVEL_5
38 #define SET_LEVEL_ALL    LC_SET_LEVEL_ALL
39
40 #else
41 /* use the builtin debug implementation */
42
43 #include <stdio.h>
44
45 enum firm_dbg_level_t {
46         LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
47         LEVEL_1 = 1,
48         LEVEL_2 = 2,
49         LEVEL_3 = 4,
50         LEVEL_4 = 8,
51         LEVEL_5 = 16,
52
53         SET_LEVEL_1 = 1,   /**< use with firm_dbg_set_mask(). */
54         SET_LEVEL_2 = 3,
55         SET_LEVEL_3 = 7,
56         SET_LEVEL_4 = 15,
57         SET_LEVEL_5 = 31,
58   SET_LEVEL_ALL = SET_LEVEL_5
59 };
60
61 typedef struct _firm_dbg_module_t firm_dbg_module_t;
62
63 /* Internal function to the debug module. */
64 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
65
66 /* Internal function to the debug module. */
67 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
68
69 /**
70  * Register a module to the firm debug facility.
71  * If the module has already been registered, no new module is allocated
72  * but the handle is returned. By default, all messages go to @c stderr
73  * and the debug mask is set to 0, i.e. the module is muted.
74  * @param name The name of the module to register.
75  * @return The module handle.
76  */
77 firm_dbg_module_t *firm_dbg_register(const char *name);
78
79 /**
80  * Set the mask of a module.
81  * @param module The module.
82  * @param mask The new mask for the module.
83  */
84 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
85
86 /**
87  * Get the mask of a module.
88  * @param module The module handle.
89  * @return The mask currently used by the module.
90  */
91 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
92
93 /**
94  * Set the output file of a module.
95  * @param module The module handle.
96  * @param file The new file to use by this handle.
97  */
98 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
99
100 #define _DBG_MAIN(func,args) \
101         _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
102
103 /* If we have C99 use the __func__ variable for calling functions name. */
104 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
105 #define _DBG(args)      _DBG_MAIN(__func__, args)
106 #else
107
108 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
109 #ifdef __GNUC__
110 #define _DBG(args)      _DBG_MAIN(__FUNCTION__, args)
111 #else
112
113 /* Else go without the name of the calling function. */
114 #define _DBG(args)      _DBG_MAIN("", args)
115 #endif
116 #endif
117
118 /**
119  * Debug messages issued with this macro are always printed, even in
120  * retail versions.
121  * @see DBG()
122  */
123 #define DBG_RETAIL(args)                _DBG(args)
124
125 #ifdef DEBUG_libfirm
126
127 /**
128  * Issue a debug message.
129  * @param args The arguments.
130  *
131  * The arguments is a list surrounded by parentheses. The items
132  * of the list are:
133  * - The module handle as returned by firm_dbg_register().
134  * - The debug mask that you want associate with this message.
135  * - A format string for the message to pass to ir_printf().
136  * - Further optional arguments are passed to ir_printf().
137  *
138  * The mask is anded against the module's mask. If both have some bits
139  * in common, the message is issued. If the given mask is 0, the message
140  * is always dumped regardless of the module's mask. You can also use
141  * the mask in a level based manner, see firm_dbg_level_t.
142  *
143  * Here is an example:
144  * @code
145  * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
146  * ...
147  * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
148  * @endcode
149  */
150 #define DBG(args)                                       _DBG(args)
151
152 #else
153 #define DBG(args)
154 #endif
155
156 #endif /* WITH_LIBCORE */
157
158 #endif /* _FIRM_DEBUG_H */