Beginnings of a crude utility to dump the contents of an OMF file
[nasm/avx512.git] / output / outdbg.c
blob2c6921e1d139c066722f617192a7584968a6a598
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outdbg.c output routines for the Netwide Assembler to produce
36 * a debugging trace
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "output/outform.h"
51 #ifdef OF_DBG
53 struct Section {
54 struct Section *next;
55 int32_t number;
56 char *name;
57 } *dbgsect;
59 FILE *dbgf;
60 efunc dbgef;
62 struct ofmt of_dbg;
63 static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
65 (void)eval;
67 dbgf = fp;
68 dbgef = errfunc;
69 dbgsect = NULL;
70 (void)ldef;
71 fprintf(fp, "NASM Output format debug dump\n");
74 static void dbg_cleanup(int debuginfo)
76 (void)debuginfo;
77 of_dbg.current_dfmt->cleanup();
78 while (dbgsect) {
79 struct Section *tmp = dbgsect;
80 dbgsect = dbgsect->next;
81 nasm_free(tmp->name);
82 nasm_free(tmp);
84 fclose(dbgf);
87 static int32_t dbg_section_names(char *name, int pass, int *bits)
89 int seg;
92 * We must have an initial default: let's make it 16.
94 if (!name)
95 *bits = 16;
97 if (!name)
98 fprintf(dbgf, "section_name on init: returning %d\n",
99 seg = seg_alloc());
100 else {
101 int n = strcspn(name, " \t");
102 char *sname = nasm_strndup(name, n);
103 struct Section *s;
105 seg = NO_SEG;
106 for (s = dbgsect; s; s = s->next)
107 if (!strcmp(s->name, sname))
108 seg = s->number;
110 if (seg == NO_SEG) {
111 s = nasm_malloc(sizeof(*s));
112 s->name = sname;
113 s->number = seg = seg_alloc();
114 s->next = dbgsect;
115 dbgsect = s;
116 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
117 name, pass, seg);
120 return seg;
123 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
124 int is_global, char *special)
126 fprintf(dbgf, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
127 name, segment, offset,
128 is_global == 2 ? "common" : is_global ? "global" : "local",
129 is_global, special ? ": " : "", special);
132 static void dbg_out(int32_t segto, const void *data,
133 enum out_type type, uint64_t size,
134 int32_t segment, int32_t wrt)
136 int32_t ldata;
137 int id;
139 fprintf(dbgf, "out to %"PRIx32", len = %"PRIu64": ", segto, size);
141 switch (type) {
142 case OUT_RESERVE:
143 fprintf(dbgf, "reserved.\n");
144 break;
145 case OUT_RAWDATA:
146 fprintf(dbgf, "raw data = ");
147 while (size--) {
148 id = *(uint8_t *)data;
149 data = (char *)data + 1;
150 fprintf(dbgf, "%02x ", id);
152 fprintf(dbgf, "\n");
153 break;
154 case OUT_ADDRESS:
155 ldata = *(int64_t *)data;
156 fprintf(dbgf, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n", ldata,
157 segment, wrt);
158 break;
159 case OUT_REL2ADR:
160 fprintf(dbgf, "rel2adr %04x (seg %08"PRIx32")\n", (int)*(int16_t *)data,
161 segment);
162 break;
163 case OUT_REL4ADR:
164 fprintf(dbgf, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n", *(int32_t *)data,
165 segment);
166 break;
167 default:
168 fprintf(dbgf, "unknown\n");
169 break;
173 static int32_t dbg_segbase(int32_t segment)
175 return segment;
178 static int dbg_directive(char *directive, char *value, int pass)
180 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
181 directive, value, pass);
182 return 1;
185 static void dbg_filename(char *inname, char *outname, efunc error)
187 standard_extension(inname, outname, ".dbg", error);
190 static int dbg_set_info(enum geninfo type, char **val)
192 (void)type;
193 (void)val;
194 return 0;
197 char *types[] = {
198 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
200 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
202 (void)of;
203 (void)id;
204 (void)fp;
205 (void)error;
206 fprintf(fp, " With debug info\n");
208 static void dbgdbg_cleanup(void)
212 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
214 fprintf(dbgf, "dbglinenum %s(%"PRId32") := %08"PRIx32"\n",
215 lnfname, lineno, segto);
217 static void dbgdbg_deflabel(char *name, int32_t segment,
218 int64_t offset, int is_global, char *special)
220 fprintf(dbgf, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
221 name,
222 segment, offset,
223 is_global == 2 ? "common" : is_global ? "global" : "local",
224 is_global, special ? ": " : "", special);
226 static void dbgdbg_define(const char *type, const char *params)
228 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
230 static void dbgdbg_output(int output_type, void *param)
232 (void)output_type;
233 (void)param;
235 static void dbgdbg_typevalue(int32_t type)
237 fprintf(dbgf, "new type: %s(%"PRIX32")\n",
238 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
240 static struct dfmt debug_debug_form = {
241 "Trace of all info passed to debug stage",
242 "debug",
243 dbgdbg_init,
244 dbgdbg_linnum,
245 dbgdbg_deflabel,
246 dbgdbg_define,
247 dbgdbg_typevalue,
248 dbgdbg_output,
249 dbgdbg_cleanup,
252 static struct dfmt *debug_debug_arr[3] = {
253 &debug_debug_form,
254 &null_debug_form,
255 NULL
258 struct ofmt of_dbg = {
259 "Trace of all info passed to output stage",
260 "dbg",
261 NULL,
262 debug_debug_arr,
263 &debug_debug_form,
264 NULL,
265 dbg_init,
266 dbg_set_info,
267 dbg_out,
268 dbg_deflabel,
269 dbg_section_names,
270 dbg_segbase,
271 dbg_directive,
272 dbg_filename,
273 dbg_cleanup
276 #endif /* OF_DBG */