Debian Codegraph @

output/outdbg.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2020 The NASM Authors - All Rights Reserved */
3
4 /*
5 * outdbg.c output routines for the Netwide Assembler to produce
6 * a debugging trace
7 */
8
9 #include "compiler.h"
10
11 #include "nctype.h"
12 #include <errno.h>
13
14 #include "nasm.h"
15 #include "nasmlib.h"
16 #include "outform.h"
17 #include "outlib.h"
18 #include "insns.h"
19 #include "dbginfo.h"
20
21 #ifdef OF_DBG
22
23 struct Section {
24 struct Section *next;
25 int32_t number;
26 char *name;
27 } *dbgsect;
28
29 static unsigned long dbg_max_data_dump = 128;
30 static bool section_labels = true;
31 static bool subsections_via_symbols = false;
32 static int32_t init_seg;
33
34 const struct ofmt of_dbg;
35 static void dbg_init(void)
36 {
37 dbgsect = NULL;
38 fprintf(ofile, "NASM Output format debug dump\n");
39 fprintf(ofile, "input file = %s\n", inname);
40 fprintf(ofile, "output file = %s\n", outname);
41 init_seg = seg_alloc();
42 }
43
44 static void dbg_reset(void)
45 {
46 fprintf(ofile, "*** pass reset: pass = %"PRId64" (%s)\n",
47 pass_count(), pass_type_name());
48 }
49
50 static void dbg_cleanup(void)
51 {
52 dfmt->cleanup();
53 while (dbgsect) {
54 struct Section *tmp = dbgsect;
55 dbgsect = dbgsect->next;
56 nasm_free(tmp->name);
57 nasm_free(tmp);
58 }
59 }
60
61 static int32_t dbg_add_section(char *name, int *bits, const char *whatwecallit)
62 {
63 int seg;
64
65 /*
66 * We must have an initial default: let's make it 16.
67 */
68 if (!name)
69 *bits = 16;
70
71 if (!name) {
72 fprintf(ofile, "section_name on init: returning %d\n", init_seg);
73 seg = init_seg;
74 } else {
75 int n = strcspn(name, " \t");
76 char *sname = nasm_strndup(name, n);
77 char *tail = nasm_skip_spaces(name+n);
78 struct Section *s;
79
80 seg = NO_SEG;
81 for (s = dbgsect; s; s = s->next)
82 if (!strcmp(s->name, sname))
83 seg = s->number;
84
85 if (seg == NO_SEG) {
86 s = nasm_malloc(sizeof(*s));
87 s->name = sname;
88 s->number = seg = seg_alloc();
89 s->next = dbgsect;
90 dbgsect = s;
91 fprintf(ofile, "%s %s (%s) pass %"PRId64" (%s) : returning %d\n",
92 whatwecallit, name, tail, pass_count(), pass_type_name(), seg);
93
94 if (section_labels)
95 backend_label(s->name, s->number + 1, 0);
96 }
97 }
98 return seg;
99 }
100
101 static int32_t dbg_section_names(char *name, int *bits)
102 {
103 return dbg_add_section(name, bits, "section_names");
104 }
105
106 static int32_t dbg_herelabel(const char *name, enum label_type type,
107 int32_t oldseg, int32_t *subsection,
108 bool *copyoffset)
109 {
110 int32_t newseg = oldseg;
111
112 if (subsections_via_symbols && type != LBL_LOCAL) {
113 newseg = *subsection;
114 if (newseg == NO_SEG) {
115 newseg = *subsection = seg_alloc();
116 *copyoffset = true; /* Minic MachO for now */
117 }
118 }
119 fprintf(ofile, "herelabel %s type %d (seg %08x) -> %08x\n",
120 name, type, oldseg, newseg);
121
122 return newseg;
123 }
124
125 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
126 int is_global, char *special)
127 {
128 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
129 name, segment, offset,
130 is_global == 2 ? "common" : is_global ? "global" : "local",
131 is_global, special ? ": " : "", special);
132 }
133
134 static const char *out_type(enum out_type type)
135 {
136 static const char *out_types[] = {
137 "rawdata",
138 "reserve",
139 "zerodata",
140 "address",
141 "reladdr",
142 "segment"
143 };
144 static char invalid_buf[64];
145
146 if (type >= sizeof(out_types)/sizeof(out_types[0])) {
147 sprintf(invalid_buf, "[invalid type %d]", type);
148 return invalid_buf;
149 }
150
151 return out_types[type];
152 }
153
154 static const char *out_flags(enum out_flags flags)
155 {
156 static const char *out_flags[] = {
157 "signed",
158 "unsigned"
159 };
160 static char flags_buf[1024];
161 unsigned long flv = flags;
162 size_t n;
163 size_t left = sizeof flags_buf - 1;
164 char *p = flags_buf;
165 unsigned int i;
166
167 for (i = 0; flv; flv >>= 1, i++) {
168 if (flv & 1) {
169 if (i < ARRAY_SIZE(out_flags))
170 n = snprintf(p, left, "%s,", out_flags[i]);
171 else
172 n = snprintf(p, left, "%u,", i);
173 if (n >= left)
174 break;
175 left -= n;
176 p += n;
177 }
178 }
179 if (p > flags_buf)
180 p--; /* Delete final comma */
181 *p = '\0';
182
183 return flags_buf;
184 }
185
186 static void dbg_legacy_out(const struct out_data *out);
187
188 static void dbg_out(const struct out_data *data)
189 {
190 fprintf(ofile,
191 "out to %"PRIx32":%"PRIx64" %s(%s) bits %d insoffs %d/%d "
192 "size %"PRIu64,
193 data->loc.segment, data->loc.offset,
194 out_type(data->type), out_flags(data->flags),
195 data->bits, data->insoffs, data->inslen, data->size);
196 if (data->itemp) {
197 fprintf(ofile, " ins %s#%u(%d)",
198 nasm_insn_names[data->itemp->opcode],
199 (unsigned int)
200 (data->itemp - nasm_instructions[data->itemp->opcode].temp),
201 data->itemp->operands);
202 } else {
203 fprintf(ofile, " no ins (plain data)");
204 }
205
206 if (data->type == OUT_ADDRESS || data->type == OUT_RELADDR ||
207 data->type == OUT_SEGMENT) {
208 fprintf(ofile, " target %"PRIx32":%"PRIx64,
209 data->tsegment, data->toffset);
210 if (data->twrt != NO_SEG)
211 fprintf(ofile, " wrt %"PRIx32, data->twrt);
212 }
213 if (data->type == OUT_RELADDR)
214 fprintf(ofile, " relbase %"PRIx64, data->relbase);
215
216 putc('\n', ofile);
217
218 if (data->type == OUT_RAWDATA) {
219 if ((size_t)data->size != data->size) {
220 fprintf(ofile, " data: <error: impossible size>\n");
221 } else if (!data->data) {
222 fprintf(ofile, " data: <error: null pointer>\n");
223 } else if (dbg_max_data_dump != -1UL &&
224 data->size > dbg_max_data_dump) {
225 fprintf(ofile, " data: <%"PRIu64" bytes>\n", data->size);
226 } else {
227 size_t i, j;
228 const uint8_t *bytes = data->data;
229 for (i = 0; i < data->size; i += 16) {
230 fprintf(ofile, " data:");
231 for (j = 0; j < 16; j++) {
232 if (i+j >= data->size)
233 fprintf(ofile, " ");
234 else
235 fprintf(ofile, "%c%02x",
236 (j == 8) ? '-' : ' ', bytes[i+j]);
237 }
238 fprintf(ofile," ");
239 for (j = 0; j < 16; j++) {
240 if (i+j >= data->size) {
241 putc(' ', ofile);
242 } else {
243 if (bytes[i+j] >= 32 && bytes[i+j] <= 126)
244 putc(bytes[i+j], ofile);
245 else
246 putc('.', ofile);
247 }
248 }
249 putc('\n', ofile);
250 }
251 }
252 }
253
254 /* Show the legacy format data, too. */
255 dbg_legacy_out(data);
256 }
257
258 static void dbg_legacy_out(const struct out_data *out)
259 {
260 OUT_LEGACY(out,segto,data,type,size,segment,wrt);
261 int32_t ldata;
262
263 if (type == OUT_ADDRESS)
264 fprintf(ofile, " legacy: out to %"PRIx32", len = %d: ",
265 segto, (int)abs((int)size));
266 else
267 fprintf(ofile, " legacy: out to %"PRIx32", len = %"PRId64" (0x%"PRIx64"): ",
268 segto, (int64_t)size, size);
269
270 switch (type) {
271 case OUT_RESERVE:
272 fprintf(ofile, "reserved.\n");
273 break;
274 case OUT_RAWDATA:
275 fprintf(ofile, "rawdata\n"); /* Already have a data dump */
276 break;
277 case OUT_ADDRESS:
278 ldata = *(int64_t *)data;
279 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
280 ldata, segment, wrt);
281 break;
282 case OUT_REL1ADR:
283 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
284 (uint8_t)*(int64_t *)data, segment);
285 break;
286 case OUT_REL2ADR:
287 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
288 (uint16_t)*(int64_t *)data, segment);
289 break;
290 case OUT_REL4ADR:
291 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
292 (uint32_t)*(int64_t *)data,
293 segment);
294 break;
295 case OUT_REL8ADR:
296 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
297 (uint64_t)*(int64_t *)data, segment);
298 break;
299 default:
300 fprintf(ofile, "unknown\n");
301 break;
302 }
303 }
304
305 static void dbg_sectalign(int32_t seg, unsigned int value)
306 {
307 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
308 seg, value);
309 }
310
311 static enum directive_result
312 dbg_directive(enum directive directive, char *value)
313 {
314 switch (directive) {
315 /*
316 * The .obj GROUP directive is nontrivial to emulate in a macro.
317 * It effectively creates a "pseudo-section" containing the first
318 * space-separated argument; the rest we ignore.
319 */
320 case D_GROUP:
321 {
322 int dummy;
323 dbg_add_section(value, &dummy, "directive:group");
324 break;
325 }
326
327 default:
328 break;
329 }
330
331 fprintf(ofile, "directive [%s] value [%s] pass %"PRId64" (%s)\n",
332 directive_dname(directive), value, pass_count(), pass_type_name());
333
334 /* The debug output format accepts all known directives */
335 return DIRR_OK;
336 }
337
338 static enum directive_result
339 dbg_pragma(const struct pragma *pragma);
340
341 static const struct pragma_facility dbg_pragma_list[] = {
342 { NULL, dbg_pragma }
343 };
344
345 static enum directive_result
346 dbg_pragma(const struct pragma *pragma)
347 {
348 fprintf(ofile, "pragma %s(%s) %s[%s] %s\n",
349 pragma->facility_name,
350 pragma->facility->name ? pragma->facility->name : "<default>",
351 pragma->opname, directive_dname(pragma->opcode),
352 pragma->tail);
353
354 if (pragma->facility == &dbg_pragma_list[0]) {
355 switch (pragma->opcode) {
356 case D_MAXDUMP:
357 if (!nasm_stricmp(pragma->tail, "unlimited")) {
358 dbg_max_data_dump = -1UL;
359 } else {
360 char *ep;
361 unsigned long arg;
362
363 errno = 0;
364 arg = strtoul(pragma->tail, &ep, 0);
365 if (errno || *nasm_skip_spaces(ep)) {
366 nasm_warn(WARN_PRAGMA_BAD | ERR_PASS2,
367 "invalid %%pragma dbg maxdump argument");
368 return DIRR_ERROR;
369 } else {
370 dbg_max_data_dump = arg;
371 }
372 }
373 break;
374 case D_NOSECLABELS:
375 section_labels = false;
376 break;
377 case D_SUBSECTIONS_VIA_SYMBOLS:
378 subsections_via_symbols = true;
379 break;
380 default:
381 break;
382 }
383 }
384 return DIRR_OK;
385 }
386
387 static const char *type_name(uint32_t type)
388 {
389 switch (TYM_TYPE(type)) {
390 case TY_UNKNOWN:
391 return "unknown";
392 case TY_LABEL:
393 return "label";
394 case TY_BYTE:
395 return "byte";
396 case TY_WORD:
397 return "word";
398 case TY_DWORD:
399 return "dword";
400 case TY_FLOAT:
401 return "float";
402 case TY_QWORD:
403 return "qword";
404 case TY_TBYTE:
405 return "tbyte";
406 case TY_OWORD:
407 return "oword";
408 case TY_YWORD:
409 return "yword";
410 case TY_ZWORD:
411 return "zword";
412 case TY_COMMON:
413 return "common";
414 case TY_SEG:
415 return "seg";
416 case TY_EXTERN:
417 return "extern";
418 case TY_EQU:
419 return "equ";
420 default:
421 return "<invalid type code>";
422 }
423 }
424
425 static void dbgdbg_init(void)
426 {
427 fprintf(ofile, "dbg init: debug information enabled\n");
428 }
429 static void dbgdbg_cleanup(void)
430 {
431 fprintf(ofile, "dbg cleanup: called\n");
432 }
433
434 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
435 {
436 fprintf(ofile, "dbg linenum: %s(%"PRId32") segment %"PRIx32"\n",
437 lnfname, lineno, segto);
438 }
439 static void dbgdbg_deflabel(char *name, int32_t segment,
440 int64_t offset, int is_global, char *special)
441 {
442 fprintf(ofile, "dbg deflabel: %s = %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
443 name,
444 segment, offset,
445 is_global == 2 ? "common" : is_global ? "global" : "local",
446 is_global, special ? ": " : "", special);
447 }
448
449 static void dbgdbg_debug_smacros(bool define, const char *def)
450 {
451 fprintf(ofile, "dbg define: %s [%s]\n",
452 define ? "define " : "undef ", def);
453 }
454 static void dbgdbg_debug_include(bool start, struct src_location outer,
455 struct src_location inner)
456 {
457 fprintf(ofile, "dbg include: %s include: %s:%"PRId32" %s %s:%"PRId32"\n",
458 start ? "start" : "end", outer.filename, outer.lineno,
459 start ? "->" : "<-", inner.filename, inner.lineno);
460 }
461
462 static void dbgdbg_output(int output_type, void *param)
463 {
464 (void)output_type;
465 (void)param;
466 fprintf(ofile, "dbg output: called\n");
467 }
468 static void dbgdbg_typevalue(int32_t type)
469 {
470 fprintf(ofile, "dbg typevalue: %s(%"PRIX32")\n",
471 type_name(type), TYM_ELEMENTS(type));
472 }
473
474 static void
475 write_macro_inv_list(const struct debug_macro_inv *inv, int level)
476 {
477 int indent = (level+1) << 1;
478
479 while (inv) {
480 const struct rbtree *rb;
481
482 fprintf(ofile, "%*smacro: %s, invoked at %s:%"PRId32
483 ", %"PRIu32" ranges\n",
484 indent, "", inv->def->name, inv->where.filename,
485 inv->where.lineno, inv->naddr);
486
487 for (rb = rb_first(inv->addr.tree); rb; rb = rb_next(rb)) {
488 const struct debug_macro_addr *addr =
489 (const struct debug_macro_addr *)rb;
490 if (!addr->len) {
491 fprintf(ofile, "%*s%08"PRIx32": empty\n",
492 indent+2, "", debug_macro_seg(addr));
493 } else {
494 fprintf(ofile,
495 "%*s%08"PRIx32":[%016"PRIx64" ... %016"PRIx64"] "
496 "len %"PRIu64"\n",
497 indent+2, "",
498 debug_macro_seg(addr), addr->start,
499 addr->start + addr->len - 1, addr->len);
500 }
501 }
502
503 write_macro_inv_list(inv->down.l, level+1);
504 inv = inv->next;
505 }
506 }
507
508 static void dbgdbg_debug_mmacros(const struct debug_macro_info *dmi)
509 {
510 const struct debug_macro_def *def;
511
512 fprintf(ofile, "dbg macros: %llu macros defined\n",
513 (unsigned long long)dmi->def.n);
514
515 fprintf(ofile, " macro definitions:\n");
516 list_for_each(def, dmi->def.l) {
517 fprintf(ofile, " macro: %s, count %llu, defined at %s:%"PRId32"\n",
518 def->name, (unsigned long long)def->ninv,
519 def->where.filename, def->where.lineno);
520 }
521
522 fprintf(ofile, " macro invocations:\n");
523 write_macro_inv_list(dmi->inv.l, 1);
524
525 fprintf(ofile, " end macro debug information\n");
526 }
527
528 static void dbgdbg_debug_directive(const char *id, const char *value)
529 {
530 fprintf(ofile, "dbg directive: id [%s] value [%s] pass %"PRId64" (%s)\n",
531 id, value, pass_count(), pass_type_name());
532 }
533
534
535 static const struct pragma_facility dbgdbg_pragma_list[] = {
536 { "dbgdbg", dbg_pragma },
537 { NULL, dbg_pragma } /* Won't trigger, "debug" is a reserved ns */
538 };
539
540 static const struct dfmt debug_debug_form = {
541 "Trace of all info passed to debug stage",
542 "debug",
543 dbgdbg_init,
544 dbgdbg_linnum,
545 dbgdbg_deflabel,
546 dbgdbg_debug_smacros,
547 dbgdbg_debug_include,
548 dbgdbg_debug_mmacros,
549 dbgdbg_debug_directive,
550 dbgdbg_typevalue,
551 dbgdbg_output,
552 dbgdbg_cleanup,
553 dbgdbg_pragma_list
554 };
555
556 static const struct dfmt * const debug_debug_arr[3] = {
557 &debug_debug_form,
558 &null_debug_form,
559 NULL
560 };
561
562 extern macros_t dbg_stdmac[];
563
564 const struct ofmt of_dbg = {
565 "Trace of all info passed to output stage",
566 "dbg",
567 ".dbg",
568 OFMT_TEXT|OFMT_KEEP_ADDR,
569 64,
570 debug_debug_arr,
571 &debug_debug_form,
572 dbg_stdmac,
573 dbg_init,
574 dbg_reset,
575 dbg_out,
576 dbg_deflabel,
577 dbg_section_names,
578 dbg_herelabel,
579 dbg_sectalign,
580 null_segbase,
581 dbg_directive,
582 dbg_cleanup,
583 dbg_pragma_list
584 };
585
586 #endif /* OF_DBG */