comparison lwasm/debug.c @ 372:90de73ba0cac

Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
author lost@starbug
date Thu, 22 Apr 2010 18:19:06 -0600
parents 6b33faa21a0a
children eacdae8a1575
comparison
equal deleted inserted replaced
371:9c24d9d485b9 372:90de73ba0cac
30 /* 30 /*
31 31
32 Various debug utilities 32 Various debug utilities
33 33
34 */ 34 */
35 void dump_state(asmstate_t *as, FILE *fp) 35 void dump_state(asmstate_t *as)
36 { 36 {
37 line_t *cl; 37 line_t *cl;
38 exportlist_t *ex; 38 exportlist_t *ex;
39 struct symtabe *s; 39 struct symtabe *s;
40 importlist_t *im; 40 importlist_t *im;
41 struct line_expr_s *le; 41 struct line_expr_s *le;
42 lwasm_error_t *e; 42 lwasm_error_t *e;
43 43
44 fprintf(fp, "Lines:\n"); 44 debug_message(as, 100, "Lines:");
45 45
46 for (cl = as -> line_head; cl; cl = cl -> next) 46 for (cl = as -> line_head; cl; cl = cl -> next)
47 { 47 {
48 fprintf(fp, "%p ", cl); 48 debug_message(as, 100, "%p INSN %d (%s) LEN %d", cl, cl -> insn, (cl -> insn >= 0) ? instab[cl -> insn].opcode : "<none>", cl -> len);
49 if (cl -> insn >= 0) 49 debug_message(as, 100, " ADDR: %s", lw_expr_print(cl -> addr));
50 { 50
51 fprintf(fp, "INSN %d (%s) ", cl -> insn, instab[cl -> insn].opcode);
52 fprintf(fp, "LEN %d ", cl -> len);
53 }
54 fprintf(fp, "\n ADDR:");
55 lw_expr_print(cl -> addr, fp);
56 for (le = cl -> exprs; le; le = le -> next) 51 for (le = cl -> exprs; le; le = le -> next)
57 { 52 {
58 fprintf(fp, "\n EXPR %d:", le -> id); 53 debug_message(as, 100, " EXPR %d: %s", le -> id, lw_expr_print(le -> expr));
59 lw_expr_print(le -> expr, fp);
60 } 54 }
61 if (cl -> outputl > 0) 55 if (cl -> outputl > 0)
62 { 56 {
63 int i; 57 int i;
64 fprintf(fp, "\n OUTPUT:");
65 for (i = 0; i < cl -> outputl; i++) 58 for (i = 0; i < cl -> outputl; i++)
66 { 59 {
67 fprintf(fp, "%02X", cl -> output[i]); 60 debug_message(as, 100, " OBYTE %02X: %02X", i, cl -> output[i]);
68 } 61 }
69 } 62 }
70 for (e = cl -> err; e; e = e -> next) 63 for (e = cl -> err; e; e = e -> next)
71 { 64 {
72 fprintf(fp, "\n ERR: %s", e -> mess); 65 debug_message(as, 100, " ERR: %s", e -> mess);
73 } 66 }
74 for (e = cl -> warn; e; e = e -> next) 67 for (e = cl -> warn; e; e = e -> next)
75 { 68 {
76 fprintf(fp, "\n WARN: %s", e -> mess); 69 debug_message(as, 100, " WARN: %s", e -> mess);
77 } 70 }
78 fprintf(fp, "\n");
79 } 71 }
80 } 72 }
73
74 void debug_message(asmstate_t *as, int level, const char *fmt, ...)
75 {
76 va_list args;
77
78 if (as -> debug_level < level)
79 return;
80
81 if (as -> debug_file == NULL)
82 as -> debug_file = stderr;
83
84 va_start(args, fmt);
85
86 fprintf(as -> debug_file, "DEBUG %03d: ", level);
87 vfprintf(as -> debug_file, fmt, args);
88 fputc('\n', as -> debug_file);
89
90 va_end(args);
91 }