output: remove the completely unused set_info method
[nasm.git] / output / outdbg.c
blob5608b446fba2ae089a97a92af60573a389146ceb
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>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "outform.h"
49 #include "outlib.h"
50 #include "insns.h"
52 #ifdef OF_DBG
54 struct Section {
55 struct Section *next;
56 int32_t number;
57 char *name;
58 } *dbgsect;
60 static unsigned long dbg_max_data_dump = 128;
62 const struct ofmt of_dbg;
63 static void dbg_init(void)
65 dbgsect = NULL;
66 fprintf(ofile, "NASM Output format debug dump\n");
69 static void dbg_cleanup(void)
71 dfmt->cleanup();
72 while (dbgsect) {
73 struct Section *tmp = dbgsect;
74 dbgsect = dbgsect->next;
75 nasm_free(tmp->name);
76 nasm_free(tmp);
80 static int32_t dbg_section_names(char *name, int pass, int *bits)
82 int seg;
85 * We must have an initial default: let's make it 16.
87 if (!name)
88 *bits = 16;
90 if (!name)
91 fprintf(ofile, "section_name on init: returning %d\n",
92 seg = seg_alloc());
93 else {
94 int n = strcspn(name, " \t");
95 char *sname = nasm_strndup(name, n);
96 struct Section *s;
98 seg = NO_SEG;
99 for (s = dbgsect; s; s = s->next)
100 if (!strcmp(s->name, sname))
101 seg = s->number;
103 if (seg == NO_SEG) {
104 s = nasm_malloc(sizeof(*s));
105 s->name = sname;
106 s->number = seg = seg_alloc();
107 s->next = dbgsect;
108 dbgsect = s;
109 fprintf(ofile, "section_name %s (pass %d): returning %d\n",
110 name, pass, seg);
113 return seg;
116 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
117 int is_global, char *special)
119 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
120 name, segment, offset,
121 is_global == 2 ? "common" : is_global ? "global" : "local",
122 is_global, special ? ": " : "", special);
125 static const char *out_type(enum out_type type)
127 static const char *out_types[] = {
128 "rawdata",
129 "reserve",
130 "address",
131 "reladdr",
132 "segment"
134 static char invalid_buf[64];
136 if (type >= sizeof(out_types)/sizeof(out_types[0])) {
137 sprintf(invalid_buf, "[invalid type %d]", type);
138 return invalid_buf;
141 return out_types[type];
144 static const char *out_sign(enum out_sign sign)
146 static const char *out_signs[] = {
147 "wrap",
148 "signed",
149 "unsigned"
151 static char invalid_buf[64];
153 if (sign >= sizeof(out_signs)/sizeof(out_signs[0])) {
154 sprintf(invalid_buf, "[invalid sign %d]", sign);
155 return invalid_buf;
158 return out_signs[sign];
161 static void dbg_out(const struct out_data *data)
163 fprintf(ofile,
164 "out to %"PRIx32":%"PRIx64" %s %s bits %d insoffs %d/%d "
165 "size %"PRIu64,
166 data->segment, data->offset,
167 out_type(data->type), out_sign(data->type),
168 data->bits, data->insoffs, data->inslen, data->size);
169 if (data->itemp) {
170 fprintf(ofile, " ins %s(%d)",
171 nasm_insn_names[data->itemp->opcode], data->itemp->operands);
172 } else {
173 fprintf(ofile, " (data)");
176 if (data->type == OUT_ADDRESS || data->type == OUT_RELADDR ||
177 data->type == OUT_SEGMENT) {
178 fprintf(ofile, " target %"PRIx32":%"PRIx64,
179 data->tsegment, data->toffset);
180 if (data->twrt != NO_SEG)
181 fprintf(ofile, " wrt %"PRIx32, data->twrt);
183 if (data->type == OUT_RELADDR)
184 fprintf(ofile, " relbase %"PRId64, data->relbase);
186 putc('\n', ofile);
188 if (data->type == OUT_RAWDATA) {
189 if ((size_t)data->size != data->size) {
190 fprintf(ofile, " data: <error: impossible size>\n");
191 } else if (!data->data) {
192 fprintf(ofile, " data: <error: null pointer>\n");
193 } else if (dbg_max_data_dump != -1UL &&
194 data->size > dbg_max_data_dump) {
195 fprintf(ofile, " data: <%"PRIu64" bytes>\n", data->size);
196 } else {
197 size_t i, j;
198 const uint8_t *bytes = data->data;
199 for (i = 0; i < data->size; i += 16) {
200 fprintf(ofile, " data:");
201 for (j = 0; j < 16; j++) {
202 if (i+j >= data->size)
203 fprintf(ofile, " ");
204 else
205 fprintf(ofile, "%c%02x",
206 (j == 8) ? '-' : ' ', bytes[i+j]);
208 fprintf(ofile," ");
209 for (j = 0; j < 16; j++) {
210 if (i+j >= data->size) {
211 putc(' ', ofile);
212 } else {
213 if (bytes[i+j] >= 32 && bytes[i+j] <= 126)
214 putc(bytes[i+j], ofile);
215 else
216 putc('.', ofile);
219 putc('\n', ofile);
224 /* This is probably the only place were we'll call this this way... */
225 nasm_do_legacy_output(data);
228 static void dbg_legacy_out(int32_t segto, const void *data,
229 enum out_type type, uint64_t size,
230 int32_t segment, int32_t wrt)
232 int32_t ldata;
234 if (type == OUT_ADDRESS)
235 fprintf(ofile, " legacy: out to %"PRIx32", len = %d: ",
236 segto, (int)abs((int)size));
237 else
238 fprintf(ofile, " legacy: out to %"PRIx32", len = %"PRIu64": ",
239 segto, size);
241 switch (type) {
242 case OUT_RESERVE:
243 fprintf(ofile, "reserved.\n");
244 break;
245 case OUT_RAWDATA:
246 fprintf(ofile, "rawdata\n"); /* Already have a data dump */
247 break;
248 case OUT_ADDRESS:
249 ldata = *(int64_t *)data;
250 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
251 ldata, segment, wrt);
252 break;
253 case OUT_REL1ADR:
254 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
255 (uint8_t)*(int64_t *)data, segment);
256 break;
257 case OUT_REL2ADR:
258 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
259 (uint16_t)*(int64_t *)data, segment);
260 break;
261 case OUT_REL4ADR:
262 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
263 (uint32_t)*(int64_t *)data,
264 segment);
265 break;
266 case OUT_REL8ADR:
267 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
268 (uint64_t)*(int64_t *)data, segment);
269 break;
270 default:
271 fprintf(ofile, "unknown\n");
272 break;
276 static void dbg_sectalign(int32_t seg, unsigned int value)
278 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
279 seg, value);
282 static int32_t dbg_segbase(int32_t segment)
284 return segment;
287 static const char *directive_name(enum directives directive)
289 static char invalid_buf[64];
291 if (directive >= DIRECTIVE_END || !directives[directive]) {
292 sprintf(invalid_buf, "%d", directive);
293 return invalid_buf;
296 return directives[directive];
299 static enum directive_result
300 dbg_directive(enum directives directive, char *value, int pass)
302 fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
303 directive_name(directive), value, pass);
304 return DIRR_OK;
307 static enum directive_result
308 dbg_pragma(const struct pragma *pragma)
310 fprintf(ofile, "pragma %s(%s) %s[%s] %s\n",
311 pragma->facility_name,
312 pragma->facility->name ? pragma->facility->name : "<default>",
313 pragma->opname, directive_name(pragma->opcode),
314 pragma->tail);
316 return DIRR_OK;
319 static void dbg_filename(char *inname, char *outname)
321 standard_extension(inname, outname, ".dbg");
324 static const char * const types[] = {
325 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
327 static void dbgdbg_init(void)
329 fprintf(ofile, " With debug info\n");
331 static void dbgdbg_cleanup(void)
335 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
337 fprintf(ofile, "dbglinenum %s(%"PRId32") segment %"PRIx32"\n",
338 lnfname, lineno, segto);
340 static void dbgdbg_deflabel(char *name, int32_t segment,
341 int64_t offset, int is_global, char *special)
343 fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
344 name,
345 segment, offset,
346 is_global == 2 ? "common" : is_global ? "global" : "local",
347 is_global, special ? ": " : "", special);
349 static void dbgdbg_define(const char *type, const char *params)
351 fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
353 static void dbgdbg_output(int output_type, void *param)
355 (void)output_type;
356 (void)param;
358 static void dbgdbg_typevalue(int32_t type)
360 fprintf(ofile, "new type: %s(%"PRIX32")\n",
361 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
364 static const struct pragma_facility dbgdbg_pragma_list[] = {
365 { "dbgdbg", dbg_pragma },
366 { NULL, dbg_pragma } /* Won't trigger, "debug" is a reserved ns */
369 static const struct dfmt debug_debug_form = {
370 "Trace of all info passed to debug stage",
371 "debug",
372 dbgdbg_init,
373 dbgdbg_linnum,
374 dbgdbg_deflabel,
375 dbgdbg_define,
376 dbgdbg_typevalue,
377 dbgdbg_output,
378 dbgdbg_cleanup,
379 dbgdbg_pragma_list
382 static const struct dfmt * const debug_debug_arr[3] = {
383 &debug_debug_form,
384 &null_debug_form,
385 NULL
388 static const struct pragma_facility dbg_pragma_list[] = {
389 { NULL, dbg_pragma }
392 const struct ofmt of_dbg = {
393 "Trace of all info passed to output stage",
394 "dbg",
395 OFMT_TEXT,
397 debug_debug_arr,
398 &debug_debug_form,
399 NULL,
400 dbg_init,
401 dbg_out,
402 dbg_legacy_out,
403 dbg_deflabel,
404 dbg_section_names,
405 dbg_sectalign,
406 dbg_segbase,
407 dbg_directive,
408 dbg_filename,
409 dbg_cleanup,
410 dbg_pragma_list
413 #endif /* OF_DBG */