iflag: automatically assign values, saner handling of CPU levels
[nasm.git] / output / outdbg.c
blobe74193b0e5bb320fe48854dafefc8f84b39d99c3
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 <errno.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "outform.h"
50 #include "outlib.h"
51 #include "insns.h"
53 #ifdef OF_DBG
55 struct Section {
56 struct Section *next;
57 int32_t number;
58 char *name;
59 } *dbgsect;
61 static unsigned long dbg_max_data_dump = 128;
63 const struct ofmt of_dbg;
64 static void dbg_init(void)
66 dbgsect = NULL;
67 fprintf(ofile, "NASM Output format debug dump\n");
70 static void dbg_cleanup(void)
72 dfmt->cleanup();
73 while (dbgsect) {
74 struct Section *tmp = dbgsect;
75 dbgsect = dbgsect->next;
76 nasm_free(tmp->name);
77 nasm_free(tmp);
81 static int32_t dbg_section_names(char *name, int pass, int *bits)
83 int seg;
86 * We must have an initial default: let's make it 16.
88 if (!name)
89 *bits = 16;
91 if (!name)
92 fprintf(ofile, "section_name on init: returning %d\n",
93 seg = seg_alloc());
94 else {
95 int n = strcspn(name, " \t");
96 char *sname = nasm_strndup(name, n);
97 struct Section *s;
99 seg = NO_SEG;
100 for (s = dbgsect; s; s = s->next)
101 if (!strcmp(s->name, sname))
102 seg = s->number;
104 if (seg == NO_SEG) {
105 s = nasm_malloc(sizeof(*s));
106 s->name = sname;
107 s->number = seg = seg_alloc();
108 s->next = dbgsect;
109 dbgsect = s;
110 fprintf(ofile, "section_name %s (pass %d): returning %d\n",
111 name, pass, seg);
114 return seg;
117 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
118 int is_global, char *special)
120 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
121 name, segment, offset,
122 is_global == 2 ? "common" : is_global ? "global" : "local",
123 is_global, special ? ": " : "", special);
126 static const char *out_type(enum out_type type)
128 static const char *out_types[] = {
129 "rawdata",
130 "reserve",
131 "address",
132 "reladdr",
133 "segment"
135 static char invalid_buf[64];
137 if (type >= sizeof(out_types)/sizeof(out_types[0])) {
138 sprintf(invalid_buf, "[invalid type %d]", type);
139 return invalid_buf;
142 return out_types[type];
145 static const char *out_sign(enum out_sign sign)
147 static const char *out_signs[] = {
148 "wrap",
149 "signed",
150 "unsigned"
152 static char invalid_buf[64];
154 if (sign >= sizeof(out_signs)/sizeof(out_signs[0])) {
155 sprintf(invalid_buf, "[invalid sign %d]", sign);
156 return invalid_buf;
159 return out_signs[sign];
162 static void dbg_out(const struct out_data *data)
164 fprintf(ofile,
165 "out to %"PRIx32":%"PRIx64" %s %s bits %d insoffs %d/%d "
166 "size %"PRIu64,
167 data->segment, data->offset,
168 out_type(data->type), out_sign(data->sign),
169 data->bits, data->insoffs, data->inslen, data->size);
170 if (data->itemp) {
171 fprintf(ofile, " ins %s(%d)",
172 nasm_insn_names[data->itemp->opcode], data->itemp->operands);
173 } else {
174 fprintf(ofile, " no ins (plain data)");
177 if (data->type == OUT_ADDRESS || data->type == OUT_RELADDR ||
178 data->type == OUT_SEGMENT) {
179 fprintf(ofile, " target %"PRIx32":%"PRIx64,
180 data->tsegment, data->toffset);
181 if (data->twrt != NO_SEG)
182 fprintf(ofile, " wrt %"PRIx32, data->twrt);
184 if (data->type == OUT_RELADDR)
185 fprintf(ofile, " relbase %"PRIx64, data->relbase);
187 putc('\n', ofile);
189 if (data->type == OUT_RAWDATA) {
190 if ((size_t)data->size != data->size) {
191 fprintf(ofile, " data: <error: impossible size>\n");
192 } else if (!data->data) {
193 fprintf(ofile, " data: <error: null pointer>\n");
194 } else if (dbg_max_data_dump != -1UL &&
195 data->size > dbg_max_data_dump) {
196 fprintf(ofile, " data: <%"PRIu64" bytes>\n", data->size);
197 } else {
198 size_t i, j;
199 const uint8_t *bytes = data->data;
200 for (i = 0; i < data->size; i += 16) {
201 fprintf(ofile, " data:");
202 for (j = 0; j < 16; j++) {
203 if (i+j >= data->size)
204 fprintf(ofile, " ");
205 else
206 fprintf(ofile, "%c%02x",
207 (j == 8) ? '-' : ' ', bytes[i+j]);
209 fprintf(ofile," ");
210 for (j = 0; j < 16; j++) {
211 if (i+j >= data->size) {
212 putc(' ', ofile);
213 } else {
214 if (bytes[i+j] >= 32 && bytes[i+j] <= 126)
215 putc(bytes[i+j], ofile);
216 else
217 putc('.', ofile);
220 putc('\n', ofile);
225 /* This is probably the only place were we'll call this this way... */
226 nasm_do_legacy_output(data);
229 static void dbg_legacy_out(int32_t segto, const void *data,
230 enum out_type type, uint64_t size,
231 int32_t segment, int32_t wrt)
233 int32_t ldata;
235 if (type == OUT_ADDRESS)
236 fprintf(ofile, " legacy: out to %"PRIx32", len = %d: ",
237 segto, (int)abs((int)size));
238 else
239 fprintf(ofile, " legacy: out to %"PRIx32", len = %"PRId64" (0x%"PRIx64"): ",
240 segto, (int64_t)size, size);
242 switch (type) {
243 case OUT_RESERVE:
244 fprintf(ofile, "reserved.\n");
245 break;
246 case OUT_RAWDATA:
247 fprintf(ofile, "rawdata\n"); /* Already have a data dump */
248 break;
249 case OUT_ADDRESS:
250 ldata = *(int64_t *)data;
251 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
252 ldata, segment, wrt);
253 break;
254 case OUT_REL1ADR:
255 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
256 (uint8_t)*(int64_t *)data, segment);
257 break;
258 case OUT_REL2ADR:
259 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
260 (uint16_t)*(int64_t *)data, segment);
261 break;
262 case OUT_REL4ADR:
263 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
264 (uint32_t)*(int64_t *)data,
265 segment);
266 break;
267 case OUT_REL8ADR:
268 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
269 (uint64_t)*(int64_t *)data, segment);
270 break;
271 default:
272 fprintf(ofile, "unknown\n");
273 break;
277 static void dbg_sectalign(int32_t seg, unsigned int value)
279 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
280 seg, value);
283 static int32_t dbg_segbase(int32_t segment)
285 return segment;
288 static enum directive_result
289 dbg_directive(enum directive directive, char *value, int pass)
291 fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
292 directive_dname(directive), value, pass);
293 return DIRR_OK;
296 static enum directive_result
297 dbg_pragma(const struct pragma *pragma);
299 static const struct pragma_facility dbg_pragma_list[] = {
300 { NULL, dbg_pragma }
303 static enum directive_result
304 dbg_pragma(const struct pragma *pragma)
306 fprintf(ofile, "pragma %s(%s) %s[%s] %s\n",
307 pragma->facility_name,
308 pragma->facility->name ? pragma->facility->name : "<default>",
309 pragma->opname, directive_dname(pragma->opcode),
310 pragma->tail);
312 if (pragma->facility == &dbg_pragma_list[0] &&
313 pragma->opcode == D_MAXDUMP) {
314 if (!nasm_stricmp(pragma->tail, "unlimited")) {
315 dbg_max_data_dump = -1UL;
316 } else {
317 char *ep;
318 unsigned long arg;
320 errno = 0;
321 arg = strtoul(pragma->tail, &ep, 0);
322 if (errno || *nasm_skip_spaces(ep)) {
323 nasm_error(ERR_WARNING | ERR_WARN_BAD_PRAGMA | ERR_PASS2,
324 "invalid %%pragma dbg maxdump argument");
325 return DIRR_ERROR;
326 } else {
327 dbg_max_data_dump = arg;
332 return DIRR_OK;
335 static void dbg_filename(char *inname, char *outname)
337 standard_extension(inname, outname, ".dbg");
340 static const char * const types[] = {
341 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
343 static void dbgdbg_init(void)
345 fprintf(ofile, " With debug info\n");
347 static void dbgdbg_cleanup(void)
351 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
353 fprintf(ofile, "dbglinenum %s(%"PRId32") segment %"PRIx32"\n",
354 lnfname, lineno, segto);
356 static void dbgdbg_deflabel(char *name, int32_t segment,
357 int64_t offset, int is_global, char *special)
359 fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
360 name,
361 segment, offset,
362 is_global == 2 ? "common" : is_global ? "global" : "local",
363 is_global, special ? ": " : "", special);
365 static void dbgdbg_define(const char *type, const char *params)
367 fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
369 static void dbgdbg_output(int output_type, void *param)
371 (void)output_type;
372 (void)param;
374 static void dbgdbg_typevalue(int32_t type)
376 fprintf(ofile, "new type: %s(%"PRIX32")\n",
377 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
380 static const struct pragma_facility dbgdbg_pragma_list[] = {
381 { "dbgdbg", dbg_pragma },
382 { NULL, dbg_pragma } /* Won't trigger, "debug" is a reserved ns */
385 static const struct dfmt debug_debug_form = {
386 "Trace of all info passed to debug stage",
387 "debug",
388 dbgdbg_init,
389 dbgdbg_linnum,
390 dbgdbg_deflabel,
391 dbgdbg_define,
392 dbgdbg_typevalue,
393 dbgdbg_output,
394 dbgdbg_cleanup,
395 dbgdbg_pragma_list
398 static const struct dfmt * const debug_debug_arr[3] = {
399 &debug_debug_form,
400 &null_debug_form,
401 NULL
404 const struct ofmt of_dbg = {
405 "Trace of all info passed to output stage",
406 "dbg",
407 OFMT_TEXT,
409 debug_debug_arr,
410 &debug_debug_form,
411 NULL,
412 dbg_init,
413 dbg_out,
414 dbg_legacy_out,
415 dbg_deflabel,
416 dbg_section_names,
417 dbg_sectalign,
418 dbg_segbase,
419 dbg_directive,
420 dbg_filename,
421 dbg_cleanup,
422 dbg_pragma_list
425 #endif /* OF_DBG */