Fix memory management issues with expanded %include
[nasm/avx512.git] / output / outdbg.c
blob1646b4b7e941d37607dadc4b3a9c93c6d2ca43d6
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");
43 of_dbg.current_dfmt->init(&of_dbg, 0, fp, errfunc);
47 static void dbg_cleanup(int debuginfo)
49 (void)debuginfo;
50 of_dbg.current_dfmt->cleanup();
51 while (dbgsect) {
52 struct Section *tmp = dbgsect;
53 dbgsect = dbgsect->next;
54 nasm_free(tmp->name);
55 nasm_free(tmp);
57 fclose(dbgf);
60 static int32_t dbg_section_names(char *name, int pass, int *bits)
62 int seg;
65 * We must have an initial default: let's make it 16.
67 if (!name)
68 *bits = 16;
70 if (!name)
71 fprintf(dbgf, "section_name on init: returning %d\n",
72 seg = seg_alloc());
73 else {
74 int n = strcspn(name, " \t");
75 char *sname = nasm_strndup(name, n);
76 struct Section *s;
78 seg = NO_SEG;
79 for (s = dbgsect; s; s = s->next)
80 if (!strcmp(s->name, sname))
81 seg = s->number;
83 if (seg == NO_SEG) {
84 s = nasm_malloc(sizeof(*s));
85 s->name = sname;
86 s->number = seg = seg_alloc();
87 s->next = dbgsect;
88 dbgsect = s;
89 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
90 name, pass, seg);
93 return seg;
96 static void dbg_deflabel(char *name, int32_t segment, int32_t offset,
97 int is_global, char *special)
99 fprintf(dbgf, "deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
100 name, segment, offset,
101 is_global == 2 ? "common" : is_global ? "global" : "local",
102 is_global, special ? ": " : "", special);
105 static void dbg_out(int32_t segto, const void *data,
106 enum out_type type, uint64_t size,
107 int32_t segment, int32_t wrt)
109 int32_t ldata;
110 int id;
112 fprintf(dbgf, "out to %lx, len = %ld: ", segto, size);
114 switch (type) {
115 case OUT_RESERVE:
116 fprintf(dbgf, "reserved.\n");
117 break;
118 case OUT_RAWDATA:
119 fprintf(dbgf, "raw data = ");
120 while (size--) {
121 id = *(uint8_t *)data;
122 data = (char *)data + 1;
123 fprintf(dbgf, "%02x ", id);
125 fprintf(dbgf, "\n");
126 break;
127 case OUT_ADDRESS:
128 ldata = *(int64_t *)data;
129 fprintf(dbgf, "addr %08lx (seg %08lx, wrt %08lx)\n", ldata,
130 segment, wrt);
131 break;
132 case OUT_REL2ADR:
133 fprintf(dbgf, "rel2adr %04x (seg %08lx)\n", (int)*(int16_t *)data,
134 segment);
135 break;
136 case OUT_REL4ADR:
137 fprintf(dbgf, "rel4adr %08lx (seg %08lx)\n", *(int32_t *)data,
138 segment);
139 break;
140 default:
141 fprintf(dbgf, "unknown\n");
142 break;
146 static int32_t dbg_segbase(int32_t segment)
148 return segment;
151 static int dbg_directive(char *directive, char *value, int pass)
153 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
154 directive, value, pass);
155 return 1;
158 static void dbg_filename(char *inname, char *outname, efunc error)
160 standard_extension(inname, outname, ".dbg", error);
163 static int dbg_set_info(enum geninfo type, char **val)
165 (void)type;
166 (void)val;
167 return 0;
170 char *types[] = {
171 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
173 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
175 (void)of;
176 (void)id;
177 (void)fp;
178 (void)error;
179 fprintf(fp, " With debug info\n");
181 static void dbgdbg_cleanup(void)
185 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
187 fprintf(dbgf, "dbglinenum %s(%ld) := %08lx\n", lnfname, lineno, segto);
189 static void dbgdbg_deflabel(char *name, int32_t segment,
190 int32_t offset, int is_global, char *special)
192 fprintf(dbgf, "dbglabel %s := %08lx:%08lx %s (%d)%s%s\n",
193 name,
194 segment, offset,
195 is_global == 2 ? "common" : is_global ? "global" : "local",
196 is_global, special ? ": " : "", special);
198 static void dbgdbg_define(const char *type, const char *params)
200 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
202 static void dbgdbg_output(int output_type, void *param)
204 (void)output_type;
205 (void)param;
207 static void dbgdbg_typevalue(int32_t type)
209 fprintf(dbgf, "new type: %s(%lX)\n",
210 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
212 static struct dfmt debug_debug_form = {
213 "Trace of all info passed to debug stage",
214 "debug",
215 dbgdbg_init,
216 dbgdbg_linnum,
217 dbgdbg_deflabel,
218 dbgdbg_define,
219 dbgdbg_typevalue,
220 dbgdbg_output,
221 dbgdbg_cleanup,
224 static struct dfmt *debug_debug_arr[3] = {
225 &debug_debug_form,
226 &null_debug_form,
227 NULL
229 struct ofmt of_dbg = {
230 "Trace of all info passed to output stage",
231 "dbg",
232 NULL,
233 debug_debug_arr,
234 &null_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 */