3 * @author Michael Beck, Sebastian Hack
12 #include "firm_config.h"
16 /* WITH DEBUG OUTPUT */
20 #define DBG(x) _LC_DBG(x)
21 #define DB(x) _LC_DB(x)
23 #include <libcore/lc_debug.h>
25 /* use the newer debug implementation in libcore */
26 typedef lc_dbg_module_t firm_dbg_module_t;
28 extern firm_dbg_module_t *firm_dbg_register(const char *name);
30 #define firm_dbg_set_mask(module, mask) lc_dbg_set_mask(module, mask)
31 #define firm_dbg_get_mask(module) lc_dbg_get_mask(module)
32 #define firm_dbg_set_file(module, file) lc_dbg_set_file(module, file)
34 #define LEVEL_DEFAULT LC_LEVEL_DEFAULT
35 #define LEVEL_1 LC_LEVEL_1
36 #define LEVEL_2 LC_LEVEL_2
37 #define LEVEL_3 LC_LEVEL_3
38 #define LEVEL_4 LC_LEVEL_4
39 #define LEVEL_5 LC_LEVEL_5
40 #define SET_LEVEL_0 LC_SET_LEVEL_0
41 #define SET_LEVEL_1 LC_SET_LEVEL_1
42 #define SET_LEVEL_2 LC_SET_LEVEL_2
43 #define SET_LEVEL_3 LC_SET_LEVEL_3
44 #define SET_LEVEL_4 LC_SET_LEVEL_4
45 #define SET_LEVEL_5 LC_SET_LEVEL_5
46 #define SET_LEVEL_ALL LC_SET_LEVEL_ALL
48 #else /* WITH_LIBCORE */
49 /* use the builtin debug implementation */
53 enum firm_dbg_level_t {
54 LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
61 SET_LEVEL_0 = 0, /**< use with firm_dbg_set_mask(). */
67 SET_LEVEL_ALL = SET_LEVEL_5
70 typedef struct _firm_dbg_module_t firm_dbg_module_t;
72 /* Internal function to the debug module. */
73 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
75 /* Internal function to the debug module. */
76 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
78 /* Internal function to the debug module. */
79 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
82 * Register a module to the firm debug facility.
83 * If the module has already been registered, no new module is allocated
84 * but the handle is returned. By default, all messages go to @c stderr
85 * and the debug mask is set to 0, i.e. the module is muted.
86 * @param name The name of the module to register.
87 * @return The module handle.
89 firm_dbg_module_t *firm_dbg_register(const char *name);
92 * Set the mask of a module.
93 * @param module The module.
94 * @param mask The new mask for the module.
96 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
99 * Get the mask of a module.
100 * @param module The module handle.
101 * @return The mask currently used by the module.
103 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
106 * Set the output file of a module.
107 * @param module The module handle.
108 * @param file The new file to use by this handle.
110 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
112 #define _DBG_MAIN(func,args) \
113 _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
115 /* If we have C99 use the __func__ variable for calling functions name. */
116 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
117 #define _DBG(args) _DBG_MAIN(__func__, args)
120 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
122 #define _DBG(args) _DBG_MAIN(__FUNCTION__, args)
125 /* Else go without the name of the calling function. */
126 #define _DBG(args) _DBG_MAIN("", args)
127 #endif /* __GNUC__ */
128 #endif /* __STD_VERSION__ ... */
130 #define _DB(args) _firm_dbg_print args
133 * Debug messages issued with this macro are always printed, even in
137 #define DBG_RETAIL(args) _DBG(args)
138 #define DB_RETAIL(args) _DB(args)
141 * Issue a debug message.
142 * @param args The arguments.
144 * The arguments is a list surrounded by parentheses. The items
146 * - The module handle as returned by firm_dbg_register().
147 * - The debug mask that you want associate with this message.
148 * - A format string for the message to pass to ir_printf().
149 * - Further optional arguments are passed to ir_printf().
151 * The mask is anded against the module's mask. If both have some bits
152 * in common, the message is issued. If the given mask is 0, the message
153 * is always dumped regardless of the module's mask. You can also use
154 * the mask in a level based manner, see firm_dbg_level_t.
156 * Here is an example:
158 * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
160 * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
163 #define DBG(args) _DBG(args)
164 #define DB(args) _DB(args)
166 #endif /* WITH_LIBCORE */
168 /** create a debug handle in debug mode */
169 #define FIRM_DBG_REGISTER(handle, name) handle = firm_dbg_register(name)
170 #define DEBUG_ONLY(code) code
171 #define RELEASE_ONLY(code)
173 #else /* ndef DEBUG_libfirm */
175 /* DEBUG OUTPUT IS COMPLETELY DISABLED */
180 /** create a debug handle in release mode */
181 #define FIRM_DBG_REGISTER(handle, name)
182 #define DEBUG_ONLY(code)
183 #define RELEASE_ONLY(code) code
185 #define firm_dbg_set_mask(module, mask)
186 #define firm_dbg_get_mask(module)
187 #define firm_dbg_set_file(module, file)
189 #endif /* DEBUG_libfirm */
191 #endif /* _FIRM_DEBUG_H */