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
47 static void firm_dbg_default_printer(struct obstack *obst, const char *fmt, va_list args)
49 static lc_arg_env_t *env = NULL;
52 env = firm_get_arg_env();
54 lc_evoprintf(env, obst, fmt, args);
58 firm_dbg_module_t *firm_dbg_register(const char *name)
60 return lc_dbg_register_with_printer(name, firm_dbg_default_printer);
65 static struct obstack dbg_obst;
66 static set *module_set;
71 struct _firm_dbg_module_t {
78 * Compares two modules by comparing it's names
80 static int module_cmp(const void *p1, const void *p2, size_t size)
82 const firm_dbg_module_t *m1 = p1;
83 const firm_dbg_module_t *m2 = p2;
84 return strcmp(m1->name, m2->name);
88 * initialize the debug module
90 static void firm_dbg_init(void)
92 obstack_init(&dbg_obst);
93 module_set = new_set(module_cmp, 16);
96 firm_dbg_module_t *firm_dbg_register(const char *name)
98 firm_dbg_module_t mod;
106 return set_insert(module_set, &mod, sizeof(mod), HASH_STR(name, strlen(name)));
109 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask)
114 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module)
119 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file)
125 * A message info: a pair of debug handle and message
127 typedef struct _msg_info_t {
129 const firm_dbg_module_t *mod;
133 * Formats a message given by a printf-like format and a va_list argument,
134 * puts the test on an obstack and return a msg_info.
136 static void *make_msg_info(const firm_dbg_module_t *mod, const char *fmt, va_list args)
138 static const char msg_header[] = "%s(%d) %s: ";
139 msg_info_t *res = obstack_alloc(&dbg_obst, sizeof(*res));
141 obstack_grow(&dbg_obst, msg_header, sizeof(msg_header) - 1);
142 ir_obst_vprintf(&dbg_obst, fmt, args);
143 obstack_1grow(&dbg_obst, '\0');
145 res->msg = obstack_finish(&dbg_obst);
150 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
154 if(mask == 0 || (mod->mask & mask)) {
157 res = make_msg_info(mod, fmt, args);
164 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *mi_ptr)
166 msg_info_t *mi = mi_ptr;
168 fprintf(mi->mod->file, mi->msg, filename, line, func);
169 obstack_free(&dbg_obst, mi);
173 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
175 if(mask == 0 || (mod->mask & mask)) {
179 ir_obst_vprintf(&dbg_obst, fmt, args);
180 obstack_1grow(&dbg_obst, '\0');
181 res = obstack_finish(&dbg_obst);
182 fprintf(mod->file, res);
183 obstack_free(&dbg_obst, res);
188 #endif /* WITH_LIBCORE */
190 #else /* DEBUG_libfirm */
192 /* some picky compiler don't allow empty files */
195 #endif /* DEBUG_libfirm */