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
40 static struct obstack dbg_obst;
41 static set *module_set;
46 struct firm_dbg_module_t {
53 * Compares two modules by comparing their names
55 static int module_cmp(const void *p1, const void *p2, size_t size)
57 const firm_dbg_module_t *m1 = (const firm_dbg_module_t*)p1;
58 const firm_dbg_module_t *m2 = (const firm_dbg_module_t*)p2;
61 return strcmp(m1->name, m2->name);
65 * initialize the debug module
67 static void firm_dbg_init(void)
69 obstack_init(&dbg_obst);
70 module_set = new_set(module_cmp, 16);
73 firm_dbg_module_t *firm_dbg_register(const char *name)
75 firm_dbg_module_t mod;
83 return set_insert(firm_dbg_module_t, module_set, &mod, sizeof(mod), hash_str(name));
86 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask)
91 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module)
96 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file)
102 * A message info: a pair of debug handle and message
104 typedef struct msg_info_t {
106 const firm_dbg_module_t *mod;
110 * Formats a message given by a printf-like format and a va_list argument,
111 * puts the test on an obstack and return a msg_info.
113 static void *make_msg_info(const firm_dbg_module_t *mod, const char *fmt, va_list args)
115 static const char msg_header[] = "%s(%d) %s: ";
116 msg_info_t *res = OALLOC(&dbg_obst, msg_info_t);
118 obstack_grow(&dbg_obst, msg_header, sizeof(msg_header) - 1);
119 ir_obst_vprintf(&dbg_obst, fmt, args);
120 obstack_1grow(&dbg_obst, '\0');
122 res->msg = (const char*)obstack_finish(&dbg_obst);
127 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
131 if (mask == 0 || (mod->mask & mask)) {
134 res = make_msg_info(mod, fmt, args);
141 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *mi_ptr)
143 msg_info_t *mi = (msg_info_t*)mi_ptr;
145 fprintf(mi->mod->file, mi->msg, filename, line, func);
146 obstack_free(&dbg_obst, mi);
150 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
152 if (mask == 0 || (mod->mask & mask)) {
156 ir_obst_vprintf(&dbg_obst, fmt, args);
157 obstack_1grow(&dbg_obst, '\0');
158 res = (char*)obstack_finish(&dbg_obst);
159 fprintf(mod->file, "%s", res);
160 obstack_free(&dbg_obst, res);
165 #else /* DEBUG_libfirm */
167 /* some picky compiler don't allow empty files */
168 static int __attribute__((unused)) dummy;
170 #endif /* DEBUG_libfirm */