2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Debug facility.
9 * @author Michael Beck, Sebastian Hack
26 static struct obstack dbg_obst;
27 static set *module_set;
32 struct firm_dbg_module_t {
39 * Compares two modules by comparing their names
41 static int module_cmp(const void *p1, const void *p2, size_t size)
43 const firm_dbg_module_t *m1 = (const firm_dbg_module_t*)p1;
44 const firm_dbg_module_t *m2 = (const firm_dbg_module_t*)p2;
47 return strcmp(m1->name, m2->name);
51 * initialize the debug module
53 static void firm_dbg_init(void)
55 obstack_init(&dbg_obst);
56 module_set = new_set(module_cmp, 16);
59 firm_dbg_module_t *firm_dbg_register(const char *name)
61 firm_dbg_module_t mod;
69 return set_insert(firm_dbg_module_t, module_set, &mod, sizeof(mod), hash_str(name));
72 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask)
77 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module)
82 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file)
88 * A message info: a pair of debug handle and message
90 typedef struct msg_info_t {
92 const firm_dbg_module_t *mod;
96 * Formats a message given by a printf-like format and a va_list argument,
97 * puts the test on an obstack and return a msg_info.
99 static void *make_msg_info(const firm_dbg_module_t *mod, const char *fmt, va_list args)
101 static const char msg_header[] = "%s(%d) %s: ";
102 msg_info_t *res = OALLOC(&dbg_obst, msg_info_t);
104 obstack_grow(&dbg_obst, msg_header, sizeof(msg_header) - 1);
105 ir_obst_vprintf(&dbg_obst, fmt, args);
106 obstack_1grow(&dbg_obst, '\0');
108 res->msg = (const char*)obstack_finish(&dbg_obst);
113 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
117 if (mask == 0 || (mod->mask & mask)) {
120 res = make_msg_info(mod, fmt, args);
127 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *mi_ptr)
129 msg_info_t *mi = (msg_info_t*)mi_ptr;
131 fprintf(mi->mod->file, mi->msg, filename, line, func);
132 obstack_free(&dbg_obst, mi);
136 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
138 if (mask == 0 || (mod->mask & mask)) {
142 ir_obst_vprintf(&dbg_obst, fmt, args);
143 obstack_1grow(&dbg_obst, '\0');
144 res = (char*)obstack_finish(&dbg_obst);
145 fprintf(mod->file, "%s", res);
146 obstack_free(&dbg_obst, res);
151 #else /* DEBUG_libfirm */
153 /* some picky compiler don't allow empty files */
154 static int __attribute__((unused)) dummy;
156 #endif /* DEBUG_libfirm */