output: call debug init from a central location
[nasm/sigaren-mirror.git] / output / outdbg.c
blob102e422a31c567770674f9b7fb425283e9010ec4
1 /* outdbg.c output routines for the Netwide Assembler to produce
2 * a debugging trace
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "outform.h"
22 #ifdef OF_DBG
24 struct Section {
25 struct Section *next;
26 int32_t number;
27 char *name;
28 } *dbgsect;
30 FILE *dbgf;
31 efunc dbgef;
33 struct ofmt of_dbg;
34 static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
36 (void)eval;
38 dbgf = fp;
39 dbgef = errfunc;
40 dbgsect = NULL;
41 (void)ldef;
42 fprintf(fp, "NASM Output format debug dump\n");
45 static void dbg_cleanup(int debuginfo)
47 (void)debuginfo;
48 of_dbg.current_dfmt->cleanup();
49 while (dbgsect) {
50 struct Section *tmp = dbgsect;
51 dbgsect = dbgsect->next;
52 nasm_free(tmp->name);
53 nasm_free(tmp);
55 fclose(dbgf);
58 static int32_t dbg_section_names(char *name, int pass, int *bits)
60 int seg;
63 * We must have an initial default: let's make it 16.
65 if (!name)
66 *bits = 16;
68 if (!name)
69 fprintf(dbgf, "section_name on init: returning %d\n",
70 seg = seg_alloc());
71 else {
72 int n = strcspn(name, " \t");
73 char *sname = nasm_strndup(name, n);
74 struct Section *s;
76 seg = NO_SEG;
77 for (s = dbgsect; s; s = s->next)
78 if (!strcmp(s->name, sname))
79 seg = s->number;
81 if (seg == NO_SEG) {
82 s = nasm_malloc(sizeof(*s));
83 s->name = sname;
84 s->number = seg = seg_alloc();
85 s->next = dbgsect;
86 dbgsect = s;
87 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
88 name, pass, seg);
91 return seg;
94 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
95 int is_global, char *special)
97 fprintf(dbgf, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
98 name, segment, offset,
99 is_global == 2 ? "common" : is_global ? "global" : "local",
100 is_global, special ? ": " : "", special);
103 static void dbg_out(int32_t segto, const void *data,
104 enum out_type type, uint64_t size,
105 int32_t segment, int32_t wrt)
107 int32_t ldata;
108 int id;
110 fprintf(dbgf, "out to %"PRIx32", len = %"PRIu64": ", segto, size);
112 switch (type) {
113 case OUT_RESERVE:
114 fprintf(dbgf, "reserved.\n");
115 break;
116 case OUT_RAWDATA:
117 fprintf(dbgf, "raw data = ");
118 while (size--) {
119 id = *(uint8_t *)data;
120 data = (char *)data + 1;
121 fprintf(dbgf, "%02x ", id);
123 fprintf(dbgf, "\n");
124 break;
125 case OUT_ADDRESS:
126 ldata = *(int64_t *)data;
127 fprintf(dbgf, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n", ldata,
128 segment, wrt);
129 break;
130 case OUT_REL2ADR:
131 fprintf(dbgf, "rel2adr %04x (seg %08"PRIx32")\n", (int)*(int16_t *)data,
132 segment);
133 break;
134 case OUT_REL4ADR:
135 fprintf(dbgf, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n", *(int32_t *)data,
136 segment);
137 break;
138 default:
139 fprintf(dbgf, "unknown\n");
140 break;
144 static int32_t dbg_segbase(int32_t segment)
146 return segment;
149 static int dbg_directive(char *directive, char *value, int pass)
151 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
152 directive, value, pass);
153 return 1;
156 static void dbg_filename(char *inname, char *outname, efunc error)
158 standard_extension(inname, outname, ".dbg", error);
161 static int dbg_set_info(enum geninfo type, char **val)
163 (void)type;
164 (void)val;
165 return 0;
168 char *types[] = {
169 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
171 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
173 (void)of;
174 (void)id;
175 (void)fp;
176 (void)error;
177 fprintf(fp, " With debug info\n");
179 static void dbgdbg_cleanup(void)
183 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
185 fprintf(dbgf, "dbglinenum %s(%"PRId32") := %08"PRIx32"\n",
186 lnfname, lineno, segto);
188 static void dbgdbg_deflabel(char *name, int32_t segment,
189 int64_t offset, int is_global, char *special)
191 fprintf(dbgf, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
192 name,
193 segment, offset,
194 is_global == 2 ? "common" : is_global ? "global" : "local",
195 is_global, special ? ": " : "", special);
197 static void dbgdbg_define(const char *type, const char *params)
199 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
201 static void dbgdbg_output(int output_type, void *param)
203 (void)output_type;
204 (void)param;
206 static void dbgdbg_typevalue(int32_t type)
208 fprintf(dbgf, "new type: %s(%"PRIX32")\n",
209 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
211 static struct dfmt debug_debug_form = {
212 "Trace of all info passed to debug stage",
213 "debug",
214 dbgdbg_init,
215 dbgdbg_linnum,
216 dbgdbg_deflabel,
217 dbgdbg_define,
218 dbgdbg_typevalue,
219 dbgdbg_output,
220 dbgdbg_cleanup,
223 static struct dfmt *debug_debug_arr[3] = {
224 &debug_debug_form,
225 &null_debug_form,
226 NULL
229 struct ofmt of_dbg = {
230 "Trace of all info passed to output stage",
231 "dbg",
232 NULL,
233 debug_debug_arr,
234 &debug_debug_form,
235 NULL,
236 dbg_init,
237 dbg_set_info,
238 dbg_out,
239 dbg_deflabel,
240 dbg_section_names,
241 dbg_segbase,
242 dbg_directive,
243 dbg_filename,
244 dbg_cleanup
247 #endif /* OF_DBG */