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