msvc.mak: (hopefully) make external dependencies work with NMAKE
[nasm.git] / output / outdbg.c
blobb7b6ef0441d533f4b6e9bc260c7bf1eeb380a1c3
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;
62 static bool section_labels = true;
64 const struct ofmt of_dbg;
65 static void dbg_init(void)
67 dbgsect = NULL;
68 fprintf(ofile, "NASM Output format debug dump\n");
71 static void dbg_cleanup(void)
73 dfmt->cleanup();
74 while (dbgsect) {
75 struct Section *tmp = dbgsect;
76 dbgsect = dbgsect->next;
77 nasm_free(tmp->name);
78 nasm_free(tmp);
82 static int32_t dbg_add_section(char *name, int pass, int *bits,
83 const char *whatwecallit)
85 int seg;
88 * We must have an initial default: let's make it 16.
90 if (!name)
91 *bits = 16;
93 if (!name) {
94 fprintf(ofile, "section_name on init: returning %d\n",
95 seg = seg_alloc());
96 } else {
97 int n = strcspn(name, " \t");
98 char *sname = nasm_strndup(name, n);
99 char *tail = nasm_skip_spaces(name+n);
100 struct Section *s;
102 seg = NO_SEG;
103 for (s = dbgsect; s; s = s->next)
104 if (!strcmp(s->name, sname))
105 seg = s->number;
107 if (seg == NO_SEG) {
108 s = nasm_malloc(sizeof(*s));
109 s->name = sname;
110 s->number = seg = seg_alloc();
111 s->next = dbgsect;
112 dbgsect = s;
113 fprintf(ofile, "%s %s (%s) pass %d: returning %d\n",
114 whatwecallit, name, tail, pass, seg);
116 if (section_labels)
117 define_label(s->name, s->number + 1, 0, NULL, false, false);
120 return seg;
123 static int32_t dbg_section_names(char *name, int pass, int *bits)
125 return dbg_add_section(name, pass, bits, "section_names");
128 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
129 int is_global, char *special)
131 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
132 name, segment, offset,
133 is_global == 2 ? "common" : is_global ? "global" : "local",
134 is_global, special ? ": " : "", special);
137 static const char *out_type(enum out_type type)
139 static const char *out_types[] = {
140 "rawdata",
141 "reserve",
142 "zerodata",
143 "address",
144 "reladdr",
145 "segment"
147 static char invalid_buf[64];
149 if (type >= sizeof(out_types)/sizeof(out_types[0])) {
150 sprintf(invalid_buf, "[invalid type %d]", type);
151 return invalid_buf;
154 return out_types[type];
157 static const char *out_sign(enum out_sign sign)
159 static const char *out_signs[] = {
160 "wrap",
161 "signed",
162 "unsigned"
164 static char invalid_buf[64];
166 if (sign >= sizeof(out_signs)/sizeof(out_signs[0])) {
167 sprintf(invalid_buf, "[invalid sign %d]", sign);
168 return invalid_buf;
171 return out_signs[sign];
174 static void dbg_out(const struct out_data *data)
176 fprintf(ofile,
177 "out to %"PRIx32":%"PRIx64" %s %s bits %d insoffs %d/%d "
178 "size %"PRIu64,
179 data->segment, data->offset,
180 out_type(data->type), out_sign(data->sign),
181 data->bits, data->insoffs, data->inslen, data->size);
182 if (data->itemp) {
183 fprintf(ofile, " ins %s(%d)",
184 nasm_insn_names[data->itemp->opcode], data->itemp->operands);
185 } else {
186 fprintf(ofile, " no ins (plain data)");
189 if (data->type == OUT_ADDRESS || data->type == OUT_RELADDR ||
190 data->type == OUT_SEGMENT) {
191 fprintf(ofile, " target %"PRIx32":%"PRIx64,
192 data->tsegment, data->toffset);
193 if (data->twrt != NO_SEG)
194 fprintf(ofile, " wrt %"PRIx32, data->twrt);
196 if (data->type == OUT_RELADDR)
197 fprintf(ofile, " relbase %"PRIx64, data->relbase);
199 putc('\n', ofile);
201 if (data->type == OUT_RAWDATA) {
202 if ((size_t)data->size != data->size) {
203 fprintf(ofile, " data: <error: impossible size>\n");
204 } else if (!data->data) {
205 fprintf(ofile, " data: <error: null pointer>\n");
206 } else if (dbg_max_data_dump != -1UL &&
207 data->size > dbg_max_data_dump) {
208 fprintf(ofile, " data: <%"PRIu64" bytes>\n", data->size);
209 } else {
210 size_t i, j;
211 const uint8_t *bytes = data->data;
212 for (i = 0; i < data->size; i += 16) {
213 fprintf(ofile, " data:");
214 for (j = 0; j < 16; j++) {
215 if (i+j >= data->size)
216 fprintf(ofile, " ");
217 else
218 fprintf(ofile, "%c%02x",
219 (j == 8) ? '-' : ' ', bytes[i+j]);
221 fprintf(ofile," ");
222 for (j = 0; j < 16; j++) {
223 if (i+j >= data->size) {
224 putc(' ', ofile);
225 } else {
226 if (bytes[i+j] >= 32 && bytes[i+j] <= 126)
227 putc(bytes[i+j], ofile);
228 else
229 putc('.', ofile);
232 putc('\n', ofile);
237 /* This is probably the only place were we'll call this this way... */
238 nasm_do_legacy_output(data);
241 static void dbg_legacy_out(int32_t segto, const void *data,
242 enum out_type type, uint64_t size,
243 int32_t segment, int32_t wrt)
245 int32_t ldata;
247 if (type == OUT_ADDRESS)
248 fprintf(ofile, " legacy: out to %"PRIx32", len = %d: ",
249 segto, (int)abs((int)size));
250 else
251 fprintf(ofile, " legacy: out to %"PRIx32", len = %"PRId64" (0x%"PRIx64"): ",
252 segto, (int64_t)size, size);
254 switch (type) {
255 case OUT_RESERVE:
256 fprintf(ofile, "reserved.\n");
257 break;
258 case OUT_RAWDATA:
259 fprintf(ofile, "rawdata\n"); /* Already have a data dump */
260 break;
261 case OUT_ADDRESS:
262 ldata = *(int64_t *)data;
263 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
264 ldata, segment, wrt);
265 break;
266 case OUT_REL1ADR:
267 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
268 (uint8_t)*(int64_t *)data, segment);
269 break;
270 case OUT_REL2ADR:
271 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
272 (uint16_t)*(int64_t *)data, segment);
273 break;
274 case OUT_REL4ADR:
275 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
276 (uint32_t)*(int64_t *)data,
277 segment);
278 break;
279 case OUT_REL8ADR:
280 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
281 (uint64_t)*(int64_t *)data, segment);
282 break;
283 default:
284 fprintf(ofile, "unknown\n");
285 break;
289 static void dbg_sectalign(int32_t seg, unsigned int value)
291 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
292 seg, value);
295 static int32_t dbg_segbase(int32_t segment)
297 return segment;
300 static enum directive_result
301 dbg_directive(enum directive directive, char *value, int pass)
303 switch (directive) {
305 * The .obj GROUP directive is nontrivial to emulate in a macro.
306 * It effectively creates a "pseudo-section" containing the first
307 * space-separated argument; the rest we ignore.
309 case D_GROUP:
311 int dummy;
312 dbg_add_section(value, pass, &dummy, "directive:group");
313 break;
316 default:
317 break;
320 fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
321 directive_dname(directive), value, pass);
322 return DIRR_OK;
325 static enum directive_result
326 dbg_pragma(const struct pragma *pragma);
328 static const struct pragma_facility dbg_pragma_list[] = {
329 { NULL, dbg_pragma }
332 static enum directive_result
333 dbg_pragma(const struct pragma *pragma)
335 fprintf(ofile, "pragma %s(%s) %s[%s] %s\n",
336 pragma->facility_name,
337 pragma->facility->name ? pragma->facility->name : "<default>",
338 pragma->opname, directive_dname(pragma->opcode),
339 pragma->tail);
341 if (pragma->facility == &dbg_pragma_list[0]) {
342 switch (pragma->opcode) {
343 case D_MAXDUMP:
344 if (!nasm_stricmp(pragma->tail, "unlimited")) {
345 dbg_max_data_dump = -1UL;
346 } else {
347 char *ep;
348 unsigned long arg;
350 errno = 0;
351 arg = strtoul(pragma->tail, &ep, 0);
352 if (errno || *nasm_skip_spaces(ep)) {
353 nasm_error(ERR_WARNING | ERR_WARN_BAD_PRAGMA | ERR_PASS2,
354 "invalid %%pragma dbg maxdump argument");
355 return DIRR_ERROR;
356 } else {
357 dbg_max_data_dump = arg;
360 break;
361 case D_NOSECLABELS:
362 section_labels = false;
363 break;
365 default:
366 break;
369 return DIRR_OK;
372 static void dbg_filename(char *inname, char *outname)
374 standard_extension(inname, outname, ".dbg");
377 static const char * const types[] = {
378 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
380 static void dbgdbg_init(void)
382 fprintf(ofile, " With debug info\n");
384 static void dbgdbg_cleanup(void)
388 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
390 fprintf(ofile, "dbglinenum %s(%"PRId32") segment %"PRIx32"\n",
391 lnfname, lineno, segto);
393 static void dbgdbg_deflabel(char *name, int32_t segment,
394 int64_t offset, int is_global, char *special)
396 fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
397 name,
398 segment, offset,
399 is_global == 2 ? "common" : is_global ? "global" : "local",
400 is_global, special ? ": " : "", special);
402 static void dbgdbg_define(const char *type, const char *params)
404 fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
406 static void dbgdbg_output(int output_type, void *param)
408 (void)output_type;
409 (void)param;
411 static void dbgdbg_typevalue(int32_t type)
413 fprintf(ofile, "new type: %s(%"PRIX32")\n",
414 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
417 static const struct pragma_facility dbgdbg_pragma_list[] = {
418 { "dbgdbg", dbg_pragma },
419 { NULL, dbg_pragma } /* Won't trigger, "debug" is a reserved ns */
422 static const struct dfmt debug_debug_form = {
423 "Trace of all info passed to debug stage",
424 "debug",
425 dbgdbg_init,
426 dbgdbg_linnum,
427 dbgdbg_deflabel,
428 dbgdbg_define,
429 dbgdbg_typevalue,
430 dbgdbg_output,
431 dbgdbg_cleanup,
432 dbgdbg_pragma_list
435 static const struct dfmt * const debug_debug_arr[3] = {
436 &debug_debug_form,
437 &null_debug_form,
438 NULL
441 extern macros_t dbg_stdmac[];
443 const struct ofmt of_dbg = {
444 "Trace of all info passed to output stage",
445 "dbg",
446 OFMT_TEXT,
448 debug_debug_arr,
449 &debug_debug_form,
450 dbg_stdmac,
451 dbg_init,
452 dbg_out,
453 dbg_legacy_out,
454 dbg_deflabel,
455 dbg_section_names,
456 dbg_sectalign,
457 dbg_segbase,
458 dbg_directive,
459 dbg_filename,
460 dbg_cleanup,
461 dbg_pragma_list
464 #endif /* OF_DBG */