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