2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Debug facility.
9 * @author Michael Beck, Sebastian Hack
12 #ifndef FIRM_COMMON_DEBUG_H
13 #define FIRM_COMMON_DEBUG_H
15 /* WITH DEBUG OUTPUT */
20 enum firm_dbg_level_t {
21 LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
28 SET_LEVEL_0 = 0, /**< use with firm_dbg_set_mask(). */
34 SET_LEVEL_ALL = SET_LEVEL_5
37 typedef struct firm_dbg_module_t firm_dbg_module_t;
39 /* Internal function to the debug module. */
40 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
42 /* Internal function to the debug module. */
43 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
45 /* Internal function to the debug module. */
46 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
49 * Register a module to the firm debug facility.
50 * If the module has already been registered, no new module is allocated
51 * but the handle is returned. By default, all messages go to @c stderr
52 * and the debug mask is set to 0, i.e. the module is muted.
53 * @param name The name of the module to register.
54 * @return The module handle.
56 firm_dbg_module_t *firm_dbg_register(const char *name);
59 * Set the mask of a module.
60 * @param module The module.
61 * @param mask The new mask for the module.
63 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
66 * Get the mask of a module.
67 * @param module The module handle.
68 * @return The mask currently used by the module.
70 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
73 * Set the output file of a module.
74 * @param module The module handle.
75 * @param file The new file to use by this handle.
77 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
79 #define _DBG_MAIN(func,args) \
80 _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
82 #define _DBG(args) _DBG_MAIN(__func__, args)
83 #define _DB(args) _firm_dbg_print args
86 * Debug messages issued with this macro are always printed, even in
90 #define DBG_RETAIL(args) _DBG(args)
91 #define DB_RETAIL(args) _DB(args)
94 * Issue a debug message.
95 * @param args The arguments.
97 * The arguments is a list surrounded by parentheses. The items
99 * - The module handle as returned by firm_dbg_register().
100 * - The debug mask that you want associate with this message.
101 * - A format string for the message to pass to ir_printf().
102 * - Further optional arguments are passed to ir_printf().
104 * The mask is anded against the module's mask. If both have some bits
105 * in common, the message is issued. If the given mask is 0, the message
106 * is always dumped regardless of the module's mask. You can also use
107 * the mask in a level based manner, see firm_dbg_level_t.
109 * Here is an example:
111 * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
113 * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
116 #define DBG(args) _DBG(args)
117 #define DB(args) _DB(args)
119 /** create a debug handle in debug mode */
120 #define FIRM_DBG_REGISTER(handle, name) handle = firm_dbg_register(name)
121 #define DEBUG_ONLY(code) code
122 #define RELEASE_ONLY(code)
124 #else /* ndef DEBUG_libfirm */
126 /* DEBUG OUTPUT IS COMPLETELY DISABLED */
128 #define DBG(x) (void)0
129 #define DB(x) (void)0
131 /** create a debug handle in release mode */
132 #define FIRM_DBG_REGISTER(handle, name) (void)0
133 #define DEBUG_ONLY(code)
134 #define RELEASE_ONLY(code) code
136 #define firm_dbg_set_mask(module, mask) (void)0
137 #define firm_dbg_get_mask(module) (void)0
138 #define firm_dbg_set_file(module, file) (void)0
140 #endif /* DEBUG_libfirm */