2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Debug facility.
23 * @author Michael Beck, Sebastian Hack
27 #ifndef FIRM_COMMON_DEBUG_H
28 #define FIRM_COMMON_DEBUG_H
30 /* WITH DEBUG OUTPUT */
35 enum firm_dbg_level_t {
36 LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
43 SET_LEVEL_0 = 0, /**< use with firm_dbg_set_mask(). */
49 SET_LEVEL_ALL = SET_LEVEL_5
52 typedef struct _firm_dbg_module_t firm_dbg_module_t;
54 /* Internal function to the debug module. */
55 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
57 /* Internal function to the debug module. */
58 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
60 /* Internal function to the debug module. */
61 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
64 * Register a module to the firm debug facility.
65 * If the module has already been registered, no new module is allocated
66 * but the handle is returned. By default, all messages go to @c stderr
67 * and the debug mask is set to 0, i.e. the module is muted.
68 * @param name The name of the module to register.
69 * @return The module handle.
71 firm_dbg_module_t *firm_dbg_register(const char *name);
74 * Set the mask of a module.
75 * @param module The module.
76 * @param mask The new mask for the module.
78 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
81 * Get the mask of a module.
82 * @param module The module handle.
83 * @return The mask currently used by the module.
85 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
88 * Set the output file of a module.
89 * @param module The module handle.
90 * @param file The new file to use by this handle.
92 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
94 #define _DBG_MAIN(func,args) \
95 _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
97 /* If we have C99 use the __func__ variable for calling functions name. */
98 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
99 #define _DBG(args) _DBG_MAIN(__func__, args)
102 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
104 #define _DBG(args) _DBG_MAIN(__FUNCTION__, args)
107 /* Else go without the name of the calling function. */
108 #define _DBG(args) _DBG_MAIN("", args)
109 #endif /* __GNUC__ */
110 #endif /* __STD_VERSION__ ... */
112 #define _DB(args) _firm_dbg_print args
115 * Debug messages issued with this macro are always printed, even in
119 #define DBG_RETAIL(args) _DBG(args)
120 #define DB_RETAIL(args) _DB(args)
123 * Issue a debug message.
124 * @param args The arguments.
126 * The arguments is a list surrounded by parentheses. The items
128 * - The module handle as returned by firm_dbg_register().
129 * - The debug mask that you want associate with this message.
130 * - A format string for the message to pass to ir_printf().
131 * - Further optional arguments are passed to ir_printf().
133 * The mask is anded against the module's mask. If both have some bits
134 * in common, the message is issued. If the given mask is 0, the message
135 * is always dumped regardless of the module's mask. You can also use
136 * the mask in a level based manner, see firm_dbg_level_t.
138 * Here is an example:
140 * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
142 * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
145 #define DBG(args) _DBG(args)
146 #define DB(args) _DB(args)
148 /** create a debug handle in debug mode */
149 #define FIRM_DBG_REGISTER(handle, name) handle = firm_dbg_register(name)
150 #define DEBUG_ONLY(code) code
151 #define RELEASE_ONLY(code)
153 #else /* ndef DEBUG_libfirm */
155 /* DEBUG OUTPUT IS COMPLETELY DISABLED */
157 #define DBG(x) (void)0
158 #define DB(x) (void)0
160 /** create a debug handle in release mode */
161 #define FIRM_DBG_REGISTER(handle, name) (void)0
162 #define DEBUG_ONLY(code)
163 #define RELEASE_ONLY(code) code
165 #define firm_dbg_set_mask(module, mask) (void)0
166 #define firm_dbg_get_mask(module) (void)0
167 #define firm_dbg_set_file(module, file) (void)0
169 #endif /* DEBUG_libfirm */