8c138bc77570d27ce22cd688f57e755879fb1e3d
[libfirm] / ir / debug / debugger.c
1 /*
2  * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Helper function for integrated debug support
23  * @author    Michael Beck
24  * @date      2005
25  */
26 #include "config.h"
27
28 #ifdef DEBUG_libfirm
29
30 #ifdef _WIN32
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33 #endif
34
35 #include "debugger.h"
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <signal.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <time.h>
43
44 #include <ctype.h>
45
46 #include "set.h"
47 #include "ident.h"
48 #include "irhooks.h"
49 #include "irgraph_t.h"
50 #include "entity_t.h"
51 #include "irprintf.h"
52 #include "irdump.h"
53 #include "iredges_t.h"
54 #include "debug.h"
55 #include "error.h"
56 #include "util.h"
57
58 #ifdef _WIN32
59 /* Break into the debugger. The Win32 way. */
60 void firm_debug_break(void)
61 {
62         DebugBreak();
63 }
64 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64))
65 /* Break into the debugger. The ia32/x86_64 way under GCC. */
66 void firm_debug_break(void)
67 {
68         __asm__ __volatile__("int3");
69 }
70 #else
71 /* Break into the debugger. Poor Unix way. */
72 void firm_debug_break(void)
73 {
74         raise(SIGINT);
75 }
76 #endif /* _WIN32 */
77
78 /** supported breakpoint kinds */
79 typedef enum {
80         BP_NR    = 'n',   /**< break on node number. */
81         BP_IDENT = 'i'    /**< break on ident. */
82 } bp_kind;
83
84 /**
85  * Reasons for node number breakpoints.
86  */
87 typedef enum bp_reasons_t {
88         BP_ON_NEW_THING,   /**< break if node, entity or type with number is created */
89         BP_ON_REPLACE,     /**< break if node with number is replaced */
90         BP_ON_LOWER,       /**< break if node with number is lowered */
91         BP_ON_REMIRG,      /**< break if an IRG is removed */
92         BP_ON_NEW_ENT,     /**< break if a new entity is created */
93         BP_MAX_REASON
94 } bp_reasons_t;
95
96 /** A breakpoint. */
97 typedef struct breakpoint {
98         bp_kind      kind;        /**< the kind of this break point */
99         unsigned     bpnr;        /**< break point number */
100         int          active;      /**< non-zero, if this break point is active */
101         bp_reasons_t reason;      /**< reason for the breakpoint */
102         struct breakpoint *next; /**< link to the next one */
103 } breakpoint;
104
105 /** A number breakpoint. */
106 typedef struct {
107         breakpoint   bp;       /**< the breakpoint data */
108         long         nr;       /**< the node number */
109 } bp_nr_t;
110
111 /** Calculate the hash value for a node breakpoint. */
112 #define HASH_NR_BP(key) (((key).nr << 2) ^ (key).bp.reason)
113
114 /** An ident breakpoint. */
115 typedef struct {
116         breakpoint   bp;       /**< the breakpoint data */
117         ident        *id;      /**< the ident */
118 } bp_ident_t;
119
120 /** Calculate the hash value for an ident breakpoint. */
121 #define HASH_IDENT_BP(key) (hash_ptr((key).id) ^ (key).bp.reason)
122
123 /** The set containing the breakpoints on node numbers. */
124 static set *bp_numbers;
125
126 /** The set containing the breakpoints on idents. */
127 static set *bp_idents;
128
129 /**< the list of all breakpoints */
130 static breakpoint *bp_list;
131
132 /** number of the current break point */
133 static unsigned bp_num = 0;
134
135 /** set if break on init command was issued. */
136 static int break_on_init = 0;
137
138 /** the hook entries for the Firm debugger module. */
139 static hook_entry_t debugger_hooks[hook_last];
140
141 /** number of active breakpoints to maintain hooks. */
142 static unsigned num_active_bp[BP_MAX_REASON];
143
144 /**
145  * The debug message buffer
146  */
147 static char firm_dbg_msg_buf[2048];
148
149 /**
150  * If set, the debug extension writes all output to the
151  * firm_dbg_msg_buf buffer
152  */
153 static int redir_output = 0;
154
155 /**
156  * Is set to one, if the debug extension is active
157  */
158 static int is_active = 0;
159
160 /** hook the hook h with function fkt. */
161 #define HOOK(h, fkt) \
162 do {                                      \
163         debugger_hooks[h].hook._##h = fkt;    \
164         register_hook(h, &debugger_hooks[h]); \
165 } while (0)
166
167 /** unhook the hook h */
168 #define UNHOOK(h) \
169 do {                                        \
170         unregister_hook(h, &debugger_hooks[h]); \
171         debugger_hooks[h].hook._##h = NULL;     \
172 } while (0)
173
174 /** returns non-zero if a entry hook h is used */
175 #define IS_HOOKED(h) (debugger_hooks[h].hook._##h != NULL)
176
177 /* some macros needed to create the info string */
178 #define _DBG_VERSION(major, minor)  #major "." #minor
179 #define DBG_VERSION(major, minor)   _DBG_VERSION(major, minor)
180 #define API_VERSION(major, minor)   "API:" DBG_VERSION(major, minor)
181
182 /* the API version: change if needed */
183 #define FIRM_DBG_MAJOR  1
184 #define FIRM_DBG_MINOR  0
185
186 /** for automatic detection of the debug extension */
187 static const char __attribute__((used)) firm_debug_info_string[] =
188         API_VERSION(FIRM_DBG_MAJOR, FIRM_DBG_MINOR);
189
190 int firm_debug_active(void)
191 {
192         return is_active;
193 }
194
195 /**
196  * Reset the debug text buffer.
197  */
198 static void reset_dbg_buf(void)
199 {
200         firm_dbg_msg_buf[0] = '\0';
201 }
202
203 static void add_to_dbg_buf(const char *buf)
204 {
205         strncat(firm_dbg_msg_buf, buf, sizeof(firm_dbg_msg_buf));
206 }
207
208 const char *firm_debug_text(void)
209 {
210         firm_dbg_msg_buf[sizeof(firm_dbg_msg_buf) - 1] = '\0';
211         return firm_dbg_msg_buf;
212 }
213
214 /**
215  * debug output
216  */
217 static void dbg_printf(const char *fmt, ...)
218 {
219         static char buf[2048];
220
221         va_list args;
222         va_start(args, fmt);
223
224         if (fmt[0] != '+')
225                 reset_dbg_buf();
226         else
227                 ++fmt;
228
229         ir_vsnprintf(buf, sizeof(buf), fmt, args);
230         va_end(args);
231
232         if (redir_output)
233                 add_to_dbg_buf(buf);
234         else
235                 puts(buf);
236 }
237
238 /**
239  * A new node is created.
240  *
241  * @param ctx   the hook context
242  * @param irg   the IR graph on which the node is created
243  * @param node  the new IR node that was created
244  */
245 static void dbg_new_node(void *ctx, ir_graph *irg, ir_node *node)
246 {
247         bp_nr_t key, *elem;
248         (void) ctx;
249         (void) irg;
250
251         key.nr        = get_irn_node_nr(node);
252         key.bp.reason = BP_ON_NEW_THING;
253
254         elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
255         if (elem && elem->bp.active) {
256                 dbg_printf("Firm BP %u reached, %+F created\n", elem->bp.bpnr, node);
257                 firm_debug_break();
258         }
259 }
260
261 /**
262  * A node is replaced.
263  *
264  * @param ctx   the hook context
265  * @param old   the IR node the is replaced
266  * @param nw    the new IR node that will replace old
267  */
268 static void dbg_replace(void *ctx, ir_node *old, ir_node *nw)
269 {
270         bp_nr_t key, *elem;
271         (void) ctx;
272
273         key.nr        = get_irn_node_nr(old);
274         key.bp.reason = BP_ON_REPLACE;
275
276         elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
277         if (elem && elem->bp.active) {
278                 dbg_printf("Firm BP %u reached, %+F will be replaced by %+F\n", elem->bp.bpnr, old, nw);
279                 firm_debug_break();
280         }
281 }
282
283 /**
284  * A new node is lowered.
285  *
286  * @param ctx   the hook context
287  * @param node  the new IR node that will be lowered
288  */
289 static void dbg_lower(void *ctx, ir_node *node)
290 {
291         bp_nr_t key, *elem;
292         (void) ctx;
293
294         key.nr        = get_irn_node_nr(node);
295         key.bp.reason = BP_ON_LOWER;
296
297         elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
298         if (elem && elem->bp.active) {
299                 dbg_printf("Firm BP %u reached, %+F will be lowered\n", elem->bp.bpnr, node);
300                 firm_debug_break();
301         }
302 }
303
304 /**
305  * A graph will be deleted.
306  *
307  * @param ctx   the hook context
308  * @param irg   the IR graph that will be deleted
309  */
310 static void dbg_free_graph(void *ctx, ir_graph *irg)
311 {
312         (void) ctx;
313         {
314                 bp_nr_t key, *elem;
315                 key.nr        = get_irg_graph_nr(irg);
316                 key.bp.reason = BP_ON_REMIRG;
317
318                 elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
319                 if (elem && elem->bp.active) {
320                         ir_printf("Firm BP %u reached, %+F will be deleted\n", elem->bp.bpnr, irg);
321                         firm_debug_break();
322                 }
323         }
324         {
325                 bp_ident_t key, *elem;
326                 ir_entity *ent = get_irg_entity(irg);
327
328                 if (! ent)
329                         return;
330
331                 key.id        = get_entity_ident(ent);
332                 key.bp.reason = BP_ON_REMIRG;
333
334                 elem = (bp_ident_t*)set_find(bp_idents, &key, sizeof(key), HASH_IDENT_BP(key));
335                 if (elem && elem->bp.active) {
336                         dbg_printf("Firm BP %u reached, %+F will be deleted\n", elem->bp.bpnr, ent);
337                         firm_debug_break();
338                 }
339         }
340 }
341
342 /**
343  * An entity was created.
344  *
345  * @param ctx   the hook context
346  * @param ent   the newly created entity
347  */
348 static void dbg_new_entity(void *ctx, ir_entity *ent)
349 {
350         (void) ctx;
351         {
352                 bp_ident_t key, *elem;
353
354                 key.id        = get_entity_ident(ent);
355                 key.bp.reason = BP_ON_NEW_ENT;
356
357                 elem = (bp_ident_t*)set_find(bp_idents, &key, sizeof(key), HASH_IDENT_BP(key));
358                 if (elem && elem->bp.active) {
359                         ir_printf("Firm BP %u reached, %+F was created\n", elem->bp.bpnr, ent);
360                         firm_debug_break();
361                 }
362         }
363         {
364                 bp_nr_t key, *elem;
365
366                 key.nr        = get_entity_nr(ent);
367                 key.bp.reason = BP_ON_NEW_THING;
368
369                 elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
370                 if (elem && elem->bp.active) {
371                         dbg_printf("Firm BP %u reached, %+F was created\n", elem->bp.bpnr, ent);
372                         firm_debug_break();
373                 }
374         }
375 }
376
377 /**
378  * A type was created.
379  *
380  * @param ctx   the hook context
381  * @param tp    the newly created type
382  */
383 static void dbg_new_type(void *ctx, ir_type *tp)
384 {
385         (void) ctx;
386         {
387                 bp_nr_t key, *elem;
388
389                 key.nr        = get_type_nr(tp);
390                 key.bp.reason = BP_ON_NEW_THING;
391
392                 elem = (bp_nr_t*)set_find(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
393                 if (elem && elem->bp.active) {
394                         ir_printf("Firm BP %u reached, %+F was created\n", elem->bp.bpnr, tp);
395                         firm_debug_break();
396                 }
397         }
398 }
399
400 /**
401  * Return the reason string.
402  */
403 static const char *reason_str(bp_reasons_t reason)
404 {
405         switch (reason) {
406         case BP_ON_NEW_THING: return "node, entity or type creation";
407         case BP_ON_REPLACE:   return "node replace";
408         case BP_ON_LOWER:     return "node lowering";
409         case BP_ON_REMIRG:    return "removing IRG";
410         case BP_ON_NEW_ENT:   return "entity creation";
411         case BP_MAX_REASON:   break;
412         }
413         panic("unsupported reason");
414 }
415
416 /**
417  * Compare two number breakpoints.
418  */
419 static int cmp_nr_bp(const void *elt, const void *key, size_t size)
420 {
421         const bp_nr_t *e1 = (const bp_nr_t*)elt;
422         const bp_nr_t *e2 = (const bp_nr_t*)key;
423         (void) size;
424
425         return (e1->nr - e2->nr) | (e1->bp.reason - e2->bp.reason);
426 }
427
428 /**
429  * Compare two ident breakpoints.
430  */
431 static int cmp_ident_bp(const void *elt, const void *key, size_t size)
432 {
433         const bp_ident_t *e1 = (const bp_ident_t*)elt;
434         const bp_ident_t *e2 = (const bp_ident_t*)key;
435         (void) size;
436
437         return (e1->id != e2->id) | (e1->bp.reason - e2->bp.reason);
438 }
439
440 /**
441  * Update the hooks.
442  */
443 static void update_hooks(breakpoint *bp)
444 {
445 #define CASE_ON(a, hook, handler)  case a: if (! IS_HOOKED(hook)) HOOK(hook, handler); break
446 #define CASE_OFF(a, hook) case a: if (IS_HOOKED(hook)) UNHOOK(hook); break
447
448         if (bp->active)
449                 ++num_active_bp[bp->reason];
450         else
451                 --num_active_bp[bp->reason];
452
453         if (num_active_bp[bp->reason] > 0) {
454                 /* register the hooks on demand */
455                 switch (bp->reason) {
456                 CASE_ON(BP_ON_REPLACE, hook_replace,    dbg_replace);
457                 CASE_ON(BP_ON_LOWER,   hook_lower,      dbg_lower);
458                 CASE_ON(BP_ON_REMIRG,  hook_free_graph, dbg_free_graph);
459                 CASE_ON(BP_ON_NEW_ENT, hook_new_entity, dbg_new_entity);
460                 case BP_ON_NEW_THING:
461                         if (!IS_HOOKED(hook_new_node))
462                                 HOOK(hook_new_node, dbg_new_node);
463                         if (!IS_HOOKED(hook_new_type))
464                                 HOOK(hook_new_type, dbg_new_type);
465                         if (!IS_HOOKED(hook_new_entity))
466                                 HOOK(hook_new_entity, dbg_new_entity);
467                         break;
468                 default:
469                         break;
470                 }
471         }
472         else {
473                 /* unregister the hook on demand */
474                 switch (bp->reason) {
475                 CASE_OFF(BP_ON_REPLACE,  hook_replace);
476                 CASE_OFF(BP_ON_LOWER,    hook_lower);
477                 CASE_OFF(BP_ON_REMIRG,   hook_free_graph);
478                 CASE_OFF(BP_ON_NEW_ENT,  hook_new_entity);
479                 case BP_ON_NEW_THING:
480                         if (IS_HOOKED(hook_new_node))
481                                 UNHOOK(hook_new_node);
482                         if (IS_HOOKED(hook_new_type))
483                                 UNHOOK(hook_new_type);
484                         if (IS_HOOKED(hook_new_entity))
485                                 UNHOOK(hook_new_entity);
486                         break;
487                 default:
488                         break;
489                 }
490         }
491 #undef CASE_ON
492 #undef CASE_OFF
493 }
494
495 /**
496  * Break if nr is reached.
497  */
498 static void break_on_nr(long nr, bp_reasons_t reason)
499 {
500         bp_nr_t key, *elem;
501
502         key.bp.kind   = BP_NR;
503         key.bp.bpnr   = 0;
504         key.bp.active = 1;
505         key.bp.reason = reason;
506         key.nr        = nr;
507
508         elem = (bp_nr_t*)set_insert(bp_numbers, &key, sizeof(key), HASH_NR_BP(key));
509
510         if (elem->bp.bpnr == 0) {
511                 /* new break point */
512                 elem->bp.bpnr = ++bp_num;
513                 elem->bp.next = bp_list;
514                 bp_list = &elem->bp;
515
516                 dbg_printf("Firm BP %u: %s of Nr %ld\n", elem->bp.bpnr, reason_str(reason), nr);
517
518                 update_hooks(&elem->bp);
519         }
520 }
521
522 /**
523  * Break if ident name is reached.
524  */
525 static void break_on_ident(const char *name, bp_reasons_t reason)
526 {
527         bp_ident_t key, *elem;
528
529         key.bp.kind   = BP_IDENT;
530         key.bp.bpnr   = 0;
531         key.bp.active = 1;
532         key.bp.reason = reason;
533         key.id        = new_id_from_str(name);
534
535         elem = (bp_ident_t*)set_insert(bp_idents, &key, sizeof(key), HASH_IDENT_BP(key));
536
537         if (elem->bp.bpnr == 0) {
538                 /* new break point */
539                 elem->bp.bpnr = ++bp_num;
540                 elem->bp.next = bp_list;
541                 bp_list = &elem->bp;
542
543                 dbg_printf("Firm BP %u: %s of ident \"%s\"\n", elem->bp.bpnr, reason_str(reason), name);
544
545                 update_hooks(&elem->bp);
546         }
547 }
548
549 /**
550  * Sets/resets the active flag of breakpoint bp.
551  */
552 static void bp_activate(unsigned bp, int active)
553 {
554         breakpoint *p;
555
556         for (p = bp_list; p; p = p->next) {
557                 if (p->bpnr == bp) {
558                         if (p->active != active) {
559                                 p->active = active;
560                                 update_hooks(p);
561                         }
562
563                         dbg_printf("Firm BP %u is now %s\n", bp, active ? "enabled" : "disabled");
564                         return;
565                 }
566         }
567         dbg_printf("Error: Firm BP %u not exists.\n", bp);
568 }
569
570
571 /**
572  * Show a list of supported commands
573  */
574 static void show_commands(void)
575 {
576         dbg_printf("Internal Firm debugger extension commands:\n"
577                 "init                  break after initialization\n"
578                 "create nr             break if node nr was created\n"
579                 "replace nr            break if node nr is replaced by another node\n"
580                 "lower nr              break before node nr is lowered\n"
581                 "remirg nr|name        break if the irg of nr or entity name is deleted\n"
582                 "newent nr|name        break if the entity nr or name was created\n"
583                 "newtype nr|name       break if the type nr or name was created\n"
584                 "bp                    show all breakpoints\n"
585                 "enable nr             enable breakpoint nr\n"
586                 "disable nr            disable breakpoint nr\n"
587                 "showtype nr|name      show content of the type nr or name\n"
588                 "showent nr|name       show content of the entity nr or name\n"
589                 "setmask name msk      sets the debug module name to mask msk\n"
590                 "setlvl  name lvl      sets the debug module name to level lvl\n"
591                 "setoutfile name file  redirects debug output of module name to file\n"
592                 "irgname name          prints address and graph number of a method given by its name\n"
593                 "irgldname ldname      prints address and graph number of a method given by its ldname\n"
594                 "randnodenr            randomize initial node number\n"
595                 "help                  list all commands\n"
596                 );
597 }
598
599 /**
600  * Shows all Firm breakpoints.
601  */
602 static void show_bp(void)
603 {
604         breakpoint *p;
605         bp_nr_t  *node_p;
606         bp_ident_t *ident_p;
607         int have_one = 0;
608
609         dbg_printf("Firm Breakpoints:");
610         for (p = bp_list; p; p = p->next) {
611                 have_one = 1;
612                 dbg_printf("+\n  BP %u: ", p->bpnr);
613
614                 switch (p->kind) {
615                 case BP_NR:
616                         node_p = (bp_nr_t *)p;
617                         dbg_printf("%s of Nr %ld ", reason_str(p->reason), node_p->nr);
618                         break;
619
620                 case BP_IDENT:
621                         ident_p = (bp_ident_t *)p;
622                         dbg_printf("+%s of ident \"%s\" ", reason_str(p->reason), get_id_str(ident_p->id));
623                         break;
624                 }
625
626                 dbg_printf(p->active ? "+enabled" : "+disabled");
627         }
628         dbg_printf(have_one ? "+\n" : "+ NONE\n");
629 }
630
631 /**
632  * firm_dbg_register() expects that the name is stored persistent.
633  * So we need this little helper function
634  */
635 static firm_dbg_module_t *dbg_register(const char *name)
636 {
637         ident *id = new_id_from_str(name);
638
639         return firm_dbg_register(get_id_str(id));
640 }
641
642 /**
643  * Sets the debug mask of module name to lvl
644  */
645 static void set_dbg_level(const char *name, unsigned lvl)
646 {
647         firm_dbg_module_t *module = dbg_register(name);
648
649         if (firm_dbg_get_mask(module) != lvl) {
650                 firm_dbg_set_mask(module, lvl);
651
652                 dbg_printf("Setting debug mask of module %s to %u\n", name, lvl);
653         }
654 }
655
656 /**
657  * Redirects the debug output of module name to fname
658  */
659 static void set_dbg_outfile(const char *name, const char *fname)
660 {
661         firm_dbg_module_t *module = dbg_register(name);
662         FILE *f = fopen(fname, "w");
663
664         if (! f) {
665                 perror(fname);
666                 return;
667         }
668
669         firm_dbg_set_file(module, f);
670         dbg_printf("Redirecting debug output of module %s to file %s\n", name, fname);
671 }
672
673 /**
674  * Show info about a firm thing.
675  */
676 static void show_firm_object(void *firm_thing)
677 {
678         FILE *f = stdout;
679
680         if (firm_thing == NULL) {
681                 fprintf(f, "<NULL>\n");
682                 return;
683         }
684         switch (get_kind(firm_thing)) {
685         case k_BAD:
686                 fprintf(f, "BAD: (%p)\n", firm_thing);
687                 break;
688         case k_entity:
689                 dump_entity_to_file(f, (ir_entity*)firm_thing);
690                 break;
691         case k_type:
692                 dump_type_to_file(f, (ir_type*)firm_thing);
693                 break;
694         case k_ir_graph:
695         case k_ir_node:
696         case k_ir_mode:
697         case k_ir_op:
698         case k_tarval:
699         case k_ir_loop:
700         case k_ir_compound_graph_path:
701         case k_ir_prog:
702                 fprintf(f, "NIY\n");
703                 break;
704         default:
705                 fprintf(f, "Cannot identify thing at (%p).\n", firm_thing);
706         }
707 }
708
709 /**
710  * Find a firm type by its number.
711  */
712 static ir_type *find_type_nr(long nr)
713 {
714         int i, n = get_irp_n_types();
715         ir_type *tp;
716
717         for (i = 0; i < n; ++i) {
718                 tp = get_irp_type(i);
719                 if (get_type_nr(tp) == nr)
720                         return tp;
721         }
722         tp = get_glob_type();
723         if (get_type_nr(tp) == nr)
724                 return tp;
725         return NULL;
726 }
727
728 /**
729  * Find a firm type by its name.
730  */
731 static ir_type *find_type_name(const char *name)
732 {
733         int i, n = get_irp_n_types();
734         ir_type *tp;
735
736         for (i = 0; i < n; ++i) {
737                 tp = get_irp_type(i);
738                 if (!is_compound_type(tp))
739                         continue;
740
741                 if (strcmp(get_compound_name(tp), name) == 0)
742                         return tp;
743         }
744         tp = get_glob_type();
745         if (strcmp(get_compound_name(tp), name) == 0)
746                 return tp;
747         return NULL;
748 }
749
750 /** The environment for the entity search functions. */
751 typedef struct find_env {
752         union {
753                 long        nr;   /**< the number that is searched for */
754                 const char *name; /**< the name that is searched for */
755         } u;
756         ir_entity *res;     /**< the result */
757 } find_env_t;
758
759 /**
760  * Type-walker: Find an entity with given number.
761  */
762 static void check_ent_nr(type_or_ent tore, void *ctx)
763 {
764         find_env_t *env = (find_env_t*)ctx;
765
766         if (is_entity(tore.ent)) {
767                 if (get_entity_nr(tore.ent) == env->u.nr) {
768                         env->res = tore.ent;
769                 }
770         }
771 }
772
773 /**
774  * Type-walker: Find an entity with given name.
775  */
776 static void check_ent_name(type_or_ent tore, void *ctx)
777 {
778         find_env_t *env = (find_env_t*)ctx;
779
780         if (is_entity(tore.ent))
781                 if (strcmp(get_entity_name(tore.ent), env->u.name) == 0) {
782                         env->res = tore.ent;
783                 }
784 }
785
786 /**
787  * Find a firm entity by its number.
788  */
789 static ir_entity *find_entity_nr(long nr)
790 {
791         find_env_t env;
792
793         env.u.nr = nr;
794         env.res  = NULL;
795         type_walk(check_ent_nr, NULL, &env);
796         return env.res;
797 }
798
799 /**
800  * Find a firm entity by its name.
801  */
802 static ir_entity *find_entity_name(const char *name)
803 {
804         find_env_t env;
805
806         env.u.name = name;
807         env.res    = NULL;
808         type_walk(check_ent_name, NULL, &env);
809         return env.res;
810 }
811
812 /**
813  * Search methods for a name.
814  */
815 static void show_by_name(type_or_ent tore, void *env)
816 {
817         ident *id = (ident *)env;
818
819         if (is_entity(tore.ent)) {
820                 ir_entity *ent = tore.ent;
821
822                 if (is_method_entity(ent)) {
823                         if (get_entity_ident(ent) == id) {
824                                 ir_type *owner = get_entity_owner(ent);
825                                 ir_graph *irg = get_entity_irg(ent);
826
827                                 if (owner != get_glob_type()) {
828                                         printf("%s::%s", get_compound_name(owner), get_id_str(id));
829                                 } else {
830                                         printf("%s", get_id_str(id));
831                                 }
832                                 if (irg)
833                                         printf("[%ld] (%p)\n", get_irg_graph_nr(irg), irg);
834                                 else
835                                         printf(" NULL\n");
836                         }
837                 }
838         }
839 }
840
841 /**
842  * Search methods for a ldname.
843  */
844 static void show_by_ldname(type_or_ent tore, void *env)
845 {
846         ident *id = (ident *)env;
847
848         if (is_entity(tore.ent)) {
849                 ir_entity *ent = tore.ent;
850
851                 if (is_method_entity(ent)) {
852                         if (get_entity_ld_ident(ent) == id) {
853                                 ir_type *owner = get_entity_owner(ent);
854                                 ir_graph *irg = get_entity_irg(ent);
855
856                                 if (owner != get_glob_type()) {
857                                         printf("%s::%s", get_compound_name(owner), get_id_str(id));
858                                 } else {
859                                         printf("%s", get_id_str(id));
860                                 }
861                                 if (irg)
862                                         printf("[%ld] (%p)\n", get_irg_graph_nr(irg), irg);
863                                 else
864                                         printf(" NULL\n");
865                         }
866                 }
867         }
868 }
869
870 /**
871  * prints the address and graph number of all irgs with given name
872  */
873 static void irg_name(const char *name)
874 {
875         ident *id = new_id_from_str(name);
876
877         type_walk(show_by_name, NULL, (void *)id);
878 }
879
880 /**
881  * prints the address and graph number of all irgs with given ld_name
882  */
883 static void irg_ld_name(const char *name)
884 {
885         ident *id = new_id_from_str(name);
886
887         type_walk(show_by_ldname, NULL, (void *)id);
888 }
889
890 enum tokens {
891         first_token = 256,
892         tok_bp = first_token,
893         tok_create,
894         tok_disable,
895         tok_dumpfilter,
896         tok_enable,
897         tok_help,
898         tok_init,
899         tok_irgldname,
900         tok_irgname,
901         tok_lower,
902         tok_newent,
903         tok_remirg,
904         tok_replace,
905         tok_setlvl,
906         tok_setmask,
907         tok_setoutfile,
908         tok_showent,
909         tok_showtype,
910         tok_randnodenr,
911         tok_identifier,
912         tok_number,
913         tok_eof,
914         tok_error
915 };
916
917 static const char *reserved[] = {
918         "bp",
919         "create",
920         "disable",
921         "dumpfilter",
922         "enable",
923         "help",
924         "init",
925         "irgldname",
926         "irgname",
927         "lower",
928         "newent",
929         "remirg",
930         "replace",
931         "setlvl",
932         "setmask",
933         "setoutfile",
934         "showent",
935         "showtype",
936         "randnodenr",
937 };
938
939 /**
940  * The Lexer data.
941  */
942 static struct lexer {
943         int has_token;        /**< set if a token is cached. */
944         unsigned cur_token;   /**< current token. */
945         unsigned number;      /**< current token attribute. */
946         const char *s;        /**< current token attribute. */
947         size_t len;           /**< current token attribute. */
948
949         const char *curr_pos;
950         const char *end_pos;
951         const char *tok_start;
952 } lexer;
953
954 /**
955  * Initialize the lexer.
956  */
957 static void init_lexer(const char *input)
958 {
959         lexer.has_token = 0;
960         lexer.curr_pos  = input;
961         lexer.end_pos   = input + strlen(input);
962 }
963
964
965 /**
966  * Get the next char from the input.
967  */
968 static char next_char(void)
969 {
970         if (lexer.curr_pos >= lexer.end_pos)
971                 return '\0';
972         return *lexer.curr_pos++;
973 }
974
975 #define unput()    if (lexer.curr_pos < lexer.end_pos) --lexer.curr_pos
976
977 #undef MIN
978 #define MIN(a, b) (a) < (b) ? (a) : (b)
979
980 /**
981  * The lexer.
982  */
983 static unsigned get_token(void)
984 {
985         char c;
986         size_t i;
987
988         /* skip white space */
989         do {
990                 c = next_char();
991         } while (c != '\0' && isspace((unsigned char)c));
992
993         lexer.tok_start = lexer.curr_pos - 1;
994         if (c == '.' || isalpha((unsigned char)c)) {
995                 /* command begins here */
996                 int         len = 0;
997                 const char* tok_start;
998
999                 do {
1000                         c = next_char();
1001                         ++len;
1002                 } while (isgraph((unsigned char)c));
1003                 unput();
1004
1005                 tok_start = lexer.tok_start;
1006                 if (*tok_start == '.') {
1007                         ++tok_start;
1008                         --len;
1009                 }
1010                 for (i = ARRAY_SIZE(reserved); i-- != 0;) {
1011                         if (strncasecmp(tok_start, reserved[i], len) == 0 && reserved[i][len] == '\0')
1012                                 return first_token + i;
1013                 }
1014
1015                 /* identifier */
1016                 lexer.s   = lexer.tok_start;
1017                 lexer.len = lexer.curr_pos - lexer.s;
1018                 return tok_identifier;
1019         } else if (isdigit((unsigned char)c) || c == '-') {
1020                 unsigned number = 0;
1021                 unsigned sign   = 0;
1022
1023                 /* we support negative numbers as well, so one can easily set all bits with -1 */
1024                 if (c == '-') {
1025                         sign = 1;
1026                         c    = next_char();
1027                 }
1028
1029                 if (c == '0') {
1030                         c = next_char();
1031
1032                         if (c == 'x' || c == 'X') {
1033                                 for (;;) {
1034                                         c = next_char();
1035
1036                                         if (! isxdigit((unsigned char)c))
1037                                                 break;
1038                                         if (isdigit((unsigned char)c))
1039                                                 number = (number << 4) | (c - '0');
1040                                         else
1041                                                 number = (number << 4) | (toupper((unsigned char)c) - 'A' + 10);
1042                                 }
1043                                 unput();
1044                                 lexer.number = number;
1045                                 return tok_number;
1046                         }
1047                 }
1048                 for (;;) {
1049                         if (! isdigit((unsigned char)c))
1050                                 break;
1051                         number = number * 10 + (c - '0');
1052                         c = next_char();
1053                 }
1054                 unput();
1055                 lexer.number = sign ? 0 - number : number;
1056                 return tok_number;
1057         }
1058         else if (c == '\0')
1059                 return tok_eof;
1060         return c;
1061 }
1062
1063 void firm_debug(const char *cmd)
1064 {
1065         char name[1024], fname[1024];
1066         size_t len;
1067
1068         init_lexer(cmd);
1069
1070         for (;;) {
1071                 unsigned token;
1072
1073                 token = get_token();
1074
1075                 switch (token) {
1076                 case tok_eof:
1077                         goto leave;
1078
1079                 case tok_create:
1080                         token = get_token();
1081                         if (token != tok_number)
1082                                 goto error;
1083                         break_on_nr(lexer.number, BP_ON_NEW_THING);
1084                         break;
1085
1086                 case tok_replace:
1087                         token = get_token();
1088                         if (token != tok_number)
1089                                 goto error;
1090                         break_on_nr(lexer.number, BP_ON_REPLACE);
1091                         break;
1092
1093                 case tok_lower:
1094                         token = get_token();
1095                         if (token != tok_number)
1096                                 goto error;
1097                         break_on_nr(lexer.number, BP_ON_LOWER);
1098                         break;
1099
1100                 case tok_remirg:
1101                         token = get_token();
1102
1103                         if (token == tok_number)
1104                                 break_on_nr(lexer.number, BP_ON_REMIRG);
1105                         else if (token == tok_identifier) {
1106                                 len = MIN(lexer.len, 1023);
1107                                 strncpy(name, lexer.s, len);
1108                                 name[len] = '\0';
1109                                 break_on_ident(name, BP_ON_REMIRG);
1110                         } else
1111                                 goto error;
1112                         break;
1113
1114                 case tok_newent:
1115                         token = get_token();
1116
1117                         if (token == tok_number)
1118                                 break_on_nr(lexer.number, BP_ON_NEW_THING);
1119                         else if (token == tok_identifier) {
1120                                 len = MIN(lexer.len, 1023);
1121                                 strncpy(name, lexer.s, len);
1122                                 name[len] = '\0';
1123                                 break_on_ident(name, BP_ON_NEW_ENT);
1124                         } else
1125                                 goto error;
1126                         break;
1127
1128                 case tok_showtype:
1129                         token = get_token();
1130
1131                         if (token == tok_number)
1132                                 show_firm_object(find_type_nr(lexer.number));
1133                         else if (token == tok_identifier) {
1134                                 len = MIN(lexer.len, 1023);
1135                                 strncpy(name, lexer.s, len);
1136                                 name[len] = '\0';
1137                                 show_firm_object(find_type_name(name));
1138                         } else
1139                                 goto error;
1140                         break;
1141
1142                 case tok_showent:
1143                         token = get_token();
1144
1145                         if (token == tok_number)
1146                                 show_firm_object(find_entity_nr(lexer.number));
1147                         else if (token == tok_identifier) {
1148                                 len = MIN(lexer.len, 1023);
1149                                 strncpy(name, lexer.s, len);
1150                                 name[len] = '\0';
1151                                 show_firm_object(find_entity_name(name));
1152                         } else
1153                                 goto error;
1154                         break;
1155
1156                 case tok_init:
1157                         break_on_init = 1;
1158                         break;
1159
1160                 case tok_bp:
1161                         show_bp();
1162                         break;
1163
1164                 case tok_enable:
1165                         token = get_token();
1166                         if (token != tok_number)
1167                                 goto error;
1168                         bp_activate(lexer.number, 1);
1169                         break;
1170
1171                 case tok_disable:
1172                         token = get_token();
1173                         if (token != tok_number)
1174                                 goto error;
1175                         bp_activate(lexer.number, 0);
1176                         break;
1177
1178                 case tok_setmask:
1179                         token = get_token();
1180                         if (token != tok_identifier)
1181                                 goto error;
1182                         len = MIN(lexer.len, 1023);
1183                         strncpy(name, lexer.s, len);
1184                         name[len] = '\0';
1185
1186                         token = get_token();
1187                         if (token != tok_number)
1188                                 goto error;
1189                         set_dbg_level(name, lexer.number);
1190                         break;
1191
1192                 case tok_setlvl:
1193                         token = get_token();
1194                         if (token != tok_identifier)
1195                                 goto error;
1196                         len = MIN(lexer.len, 1023);
1197                         strncpy(name, lexer.s, len);
1198                         name[len] = '\0';
1199
1200                         token = get_token();
1201                         if (token != tok_number)
1202                                 goto error;
1203                         set_dbg_level(name, (1 << lexer.number) - 1);
1204                         break;
1205
1206                 case tok_setoutfile:
1207                         token = get_token();
1208                         if (token != tok_identifier)
1209                                 goto error;
1210                         len = MIN(lexer.len, 1023);
1211                         strncpy(name, lexer.s, len);
1212                         name[len] = '\0';
1213
1214                         token = get_token();
1215                         if (token != tok_identifier)
1216                                 goto error;
1217                         len = MIN(lexer.len, 1023);
1218                         strncpy(fname, lexer.s, len);
1219                         fname[len] = '\0';
1220                         set_dbg_outfile(name, fname);
1221                         break;
1222
1223                 case tok_irgname:
1224                         token = get_token();
1225                         if (token != tok_identifier)
1226                                 goto error;
1227                         len = MIN(lexer.len, 1023);
1228                         strncpy(name, lexer.s, len);
1229                         name[len] = '\0';
1230                         irg_name(name);
1231                         break;
1232
1233                 case tok_randnodenr:
1234                         dbg_printf("Randomizing initial node number\n");
1235                         srand(time(0));
1236                         irp->max_node_nr += rand() % 6666;
1237                         break;
1238
1239                 case tok_irgldname:
1240                         token = get_token();
1241                         if (token != tok_identifier)
1242                                 goto error;
1243                         len = MIN(lexer.len, 1023);
1244                         strncpy(name, lexer.s, len);
1245                         name[len] = '\0';
1246                         irg_ld_name(name);
1247                         break;
1248
1249                 case tok_dumpfilter:
1250                         token = get_token();
1251                         if (token != tok_identifier)
1252                                 goto error;
1253                         len = MIN(lexer.len, 1023);
1254                         strncpy(name, lexer.s, len);
1255                         name[len] = '\0';
1256                         ir_set_dump_filter(name);
1257                         break;
1258
1259                 case tok_help:
1260                         show_commands();
1261                         break;
1262
1263                 case tok_error:
1264                 default:
1265 error:
1266                         printf("Error: before %s\n", lexer.tok_start);
1267                         show_commands();
1268                         goto leave;
1269                 }
1270
1271                 token = get_token();
1272                 if (token == tok_eof)
1273                         break;
1274                 if (token != ';')
1275                         goto error;
1276         }
1277 leave:
1278         ;
1279 }
1280
1281 void firm_init_debugger(void)
1282 {
1283         char *env;
1284
1285         bp_numbers = new_set(cmp_nr_bp, 8);
1286         bp_idents  = new_set(cmp_ident_bp, 8);
1287
1288         env = getenv("FIRMDBG");
1289
1290         is_active = 1;
1291
1292         if (env)
1293                 firm_debug(env);
1294
1295         if (break_on_init)
1296                 firm_debug_break();
1297 }
1298
1299 void firm_finish_debugger(void)
1300 {
1301         del_set(bp_numbers);
1302         del_set(bp_idents);
1303 }
1304
1305 /**
1306  * A gdb helper function to print firm objects.
1307  */
1308 const char *gdb_node_helper(void *firm_object)
1309 {
1310         static char buf[1024];
1311         ir_snprintf(buf, sizeof(buf), "%+F", firm_object);
1312         return buf;
1313 }
1314
1315 const char *gdb_tarval_helper(void *tv_object)
1316 {
1317         static char buf[1024];
1318         ir_snprintf(buf, sizeof(buf), "%+T", tv_object);
1319         return buf;
1320 }
1321
1322 const char *gdb_out_edge_helper(const ir_node *node)
1323 {
1324         static char buf[4*1024];
1325         char *b = buf;
1326         size_t l;
1327         size_t len = sizeof(buf);
1328         const ir_edge_t *edge;
1329         foreach_out_edge(node, edge) {
1330                 ir_node *n = get_edge_src_irn(edge);
1331
1332                 ir_snprintf(b, len, "%+F  ", n);
1333                 l = strlen(b);
1334                 len -= l;
1335                 b += l;
1336         }
1337
1338         return buf;
1339 }
1340
1341 #else
1342
1343 /* some picky compiler do not allow empty files */
1344 static int __attribute__((unused)) _firm_only_that_you_can_compile_with_NDEBUG_defined;
1345
1346 #endif /* NDEBUG */
1347
1348 /**
1349  * @page debugger   The Firm debugger extension
1350  *
1351  * Firm contains a debugger extension. This allows to set debugger breakpoints
1352  * an various events.
1353  * The extension uses a text interface which can be accessed from most debuggers.
1354  * More than one command can be given separated by ';'.
1355  *
1356  * @section sec_cmd Supported commands
1357  *
1358  * Historically all debugger commands start with a dot.  This isn't needed in newer
1359  * versions, but still supported, ie the commands ".init" and "init" are equal.
1360  * The following commands are currently supported:
1361  *
1362  * @b init
1363  *
1364  * Break immediately after the debugger extension was initialized.
1365  * Typically this command is used in the environment to stop the execution
1366  * of a Firm compiler right after the initialization, like this:
1367  *
1368  * $export FIRMDBG=".init"
1369  *
1370  * @b create nr
1371  *
1372  * Break if a new IR-node with node number nr was created.
1373  * Typically used to find the place where wrong nodes are created.
1374  *
1375  * @b replace nr
1376  *
1377  * Break before IR-node with node number nr is replaced by another node.
1378  *
1379  * @b lower nr
1380  *
1381  * Break before IR-node with node number nr is lowered.
1382  *
1383  * @b remirg nr
1384  *
1385  * Break if the irg with graph number nr is deleted.
1386  *
1387  * @b remirg name
1388  *
1389  * Break if the irg of entity name is deleted.
1390  *
1391  * @b newent nr
1392  *
1393  * Break if the entity with number nr was created.
1394  *
1395  * @b newent name
1396  *
1397  * Break if the entity name was created.
1398  *
1399  * @b newtype nr
1400  *
1401  * Break if the type with number nr was created.
1402  *
1403  * @b newtype name
1404  *
1405  * Break if the type name was created.
1406  *
1407  * @b bp
1408  *
1409  * Show all Firm internal breakpoints.
1410  *
1411  * @b enable nr
1412  *
1413  * Enables breakpoint nr.
1414  *
1415  * @b disable nr
1416  *
1417  * Disables breakpoint nr.
1418  *
1419  * @b showent nr
1420  *
1421  * Show the content of entity nr.
1422  *
1423  * @b showent name
1424  *
1425  * Show the content of entity name.
1426  *
1427  * @b showtype nr
1428  *
1429  * Show the content of type nr.
1430  *
1431  * @b showtype name
1432  *
1433  * Show the content of type name.
1434  *
1435  * @b setmask name msk
1436  *
1437  * Sets the debug module name to mask msk.
1438  *
1439  * @b setlvl name lvl
1440  *
1441  * Sets the debug module name to level lvl.
1442  *
1443  * @b setoutfile name file
1444  *
1445  * Redirects debug output of module name to file.
1446  *
1447  * @b irgname name
1448  *
1449  * Prints address and graph number of a method given by its name.
1450  *
1451  * @b irgldname name
1452  *
1453  * Prints address and graph number of a method given by its linker name.
1454  *
1455  * @b help
1456  *
1457  * List all commands.
1458  *
1459  *
1460  * The Firm debugger extension can be accessed using the function firm_debug().
1461  * The following example shows how to set a creation breakpoint in GDB when
1462  * node 2101 is created.
1463  *
1464  * -# set FIRMDBG="init"
1465  * -# start gdb with your compiler
1466  * -# after gdb breaks, issue
1467  *
1468  * call firm_debug("create 2101")
1469  *
1470  * On the console the following text should be issued:
1471  *
1472  * Firm BP 1: creation of Node 2101
1473  *
1474  *
1475  * @section gdb_macro GDB macro
1476  *
1477  * Add the following to your .gdbinit file:
1478  * @code
1479  #
1480  # define firm "cmd"  Firm debugger extension
1481  #
1482  define firm
1483  call firm_debug($arg0)
1484  end
1485  * @endcode
1486  *
1487  * Then, all Firm debugger extension commands can be accessed in the gdb
1488  * console using the firm prefix, eg.:
1489  *
1490  * firm "create 2101"
1491  *
1492  * firm "help"
1493  */