2 * Copyright (C) 1995-2007 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
31 #include "firm_config.h"
49 static void firm_dbg_default_printer(struct obstack *obst, const char *fmt, va_list args)
51 static lc_arg_env_t *env = NULL;
54 env = firm_get_arg_env();
56 lc_evoprintf(env, obst, fmt, args);
60 firm_dbg_module_t *firm_dbg_register(const char *name)
62 return lc_dbg_register_with_printer(name, firm_dbg_default_printer);
67 static struct obstack dbg_obst;
68 static set *module_set;
73 struct _firm_dbg_module_t {
80 * Compares two modules by comparing it's names
82 static int module_cmp(const void *p1, const void *p2, size_t size)
84 const firm_dbg_module_t *m1 = p1;
85 const firm_dbg_module_t *m2 = p2;
86 return strcmp(m1->name, m2->name);
90 * initialize the debug module
92 static void firm_dbg_init(void)
94 obstack_init(&dbg_obst);
95 module_set = new_set(module_cmp, 16);
98 firm_dbg_module_t *firm_dbg_register(const char *name)
100 firm_dbg_module_t mod;
108 return set_insert(module_set, &mod, sizeof(mod), HASH_STR(name, strlen(name)));
111 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask)
116 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module)
121 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file)
127 * A message info: a pair of debug handle and message
129 typedef struct _msg_info_t {
131 const firm_dbg_module_t *mod;
135 * Formats a message given by a printf-like format and a va_list argument,
136 * puts the test on an obstack and return a msg_info.
138 static void *make_msg_info(const firm_dbg_module_t *mod, const char *fmt, va_list args)
140 static const char msg_header[] = "%s(%d) %s: ";
141 msg_info_t *res = obstack_alloc(&dbg_obst, sizeof(*res));
143 obstack_grow(&dbg_obst, msg_header, sizeof(msg_header) - 1);
144 ir_obst_vprintf(&dbg_obst, fmt, args);
145 obstack_1grow(&dbg_obst, '\0');
147 res->msg = obstack_finish(&dbg_obst);
152 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
156 if(mask == 0 || (mod->mask & mask)) {
159 res = make_msg_info(mod, fmt, args);
166 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *mi_ptr)
168 msg_info_t *mi = mi_ptr;
170 fprintf(mi->mod->file, mi->msg, filename, line, func);
171 obstack_free(&dbg_obst, mi);
175 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
177 if(mask == 0 || (mod->mask & mask)) {
181 ir_obst_vprintf(&dbg_obst, fmt, args);
182 obstack_1grow(&dbg_obst, '\0');
183 res = obstack_finish(&dbg_obst);
184 fprintf(mod->file, res);
185 obstack_free(&dbg_obst, res);
190 #endif /* WITH_LIBCORE */
192 #else /* DEBUG_libfirm */
194 /* some picky compiler don't allow empty files */
195 static int __attribute__((unused)) dummy;
197 #endif /* DEBUG_libfirm */