nasm/output/outdbg.c

284 lines
7.7 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2013 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2002-05-01 04:51:32 +08:00
*
* ----------------------------------------------------------------------- */
/*
* outdbg.c output routines for the Netwide Assembler to produce
* a debugging trace
2002-05-01 04:51:32 +08:00
*/
#include "compiler.h"
2002-05-01 04:51:32 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
2002-05-01 04:51:32 +08:00
#include "nasm.h"
#include "nasmlib.h"
#include "output/outform.h"
2002-05-01 04:51:32 +08:00
#ifdef OF_DBG
2002-05-01 04:52:49 +08:00
struct Section {
struct Section *next;
int32_t number;
char *name;
2002-05-01 04:52:49 +08:00
} *dbgsect;
2002-05-01 04:53:55 +08:00
struct ofmt of_dbg;
static void dbg_init(void)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:52:49 +08:00
dbgsect = NULL;
fprintf(ofile, "NASM Output format debug dump\n");
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:53:55 +08:00
static void dbg_cleanup(int debuginfo)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
(void)debuginfo;
2002-05-01 04:53:55 +08:00
of_dbg.current_dfmt->cleanup();
2002-05-01 04:52:49 +08:00
while (dbgsect) {
2005-01-16 06:15:51 +08:00
struct Section *tmp = dbgsect;
dbgsect = dbgsect->next;
nasm_free(tmp->name);
nasm_free(tmp);
2002-05-01 04:52:49 +08:00
}
2002-05-01 04:51:32 +08:00
}
static int32_t dbg_section_names(char *name, int pass, int *bits)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:52:49 +08:00
int seg;
2002-05-01 04:51:32 +08:00
/*
* We must have an initial default: let's make it 16.
*/
if (!name)
2005-01-16 06:15:51 +08:00
*bits = 16;
2002-05-01 04:51:32 +08:00
if (!name)
fprintf(ofile, "section_name on init: returning %d\n",
2005-01-16 06:15:51 +08:00
seg = seg_alloc());
2002-05-01 04:52:49 +08:00
else {
2005-01-16 06:15:51 +08:00
int n = strcspn(name, " \t");
char *sname = nasm_strndup(name, n);
2005-01-16 06:15:51 +08:00
struct Section *s;
seg = NO_SEG;
for (s = dbgsect; s; s = s->next)
if (!strcmp(s->name, sname))
seg = s->number;
if (seg == NO_SEG) {
s = nasm_malloc(sizeof(*s));
s->name = sname;
s->number = seg = seg_alloc();
s->next = dbgsect;
dbgsect = s;
fprintf(ofile, "section_name %s (pass %d): returning %d\n",
2005-01-16 06:15:51 +08:00
name, pass, seg);
}
2002-05-01 04:52:49 +08:00
}
return seg;
2002-05-01 04:51:32 +08:00
}
static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
int is_global, char *special)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
2005-01-16 06:15:51 +08:00
name, segment, offset,
is_global == 2 ? "common" : is_global ? "global" : "local",
is_global, special ? ": " : "", special);
2002-05-01 04:51:32 +08:00
}
static void dbg_out(int32_t segto, const void *data,
enum out_type type, uint64_t size,
int32_t segment, int32_t wrt)
2002-05-01 04:53:55 +08:00
{
int32_t ldata;
2002-05-01 04:52:49 +08:00
int id;
if (type == OUT_ADDRESS)
fprintf(ofile, "out to %"PRIx32", len = %d: ", segto, (int)abs((int)size));
else
fprintf(ofile, "out to %"PRIx32", len = %"PRIu64": ", segto, size);
2005-01-16 06:15:51 +08:00
switch (type) {
case OUT_RESERVE:
fprintf(ofile, "reserved.\n");
2005-01-16 06:15:51 +08:00
break;
case OUT_RAWDATA:
fprintf(ofile, "raw data = ");
while (size--) {
id = *(uint8_t *)data;
data = (char *)data + 1;
fprintf(ofile, "%02x ", id);
2005-01-16 06:15:51 +08:00
}
fprintf(ofile, "\n");
2005-01-16 06:15:51 +08:00
break;
case OUT_ADDRESS:
ldata = *(int64_t *)data;
fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
ldata, segment, wrt);
2005-01-16 06:15:51 +08:00
break;
case OUT_REL1ADR:
fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
(uint8_t)*(int64_t *)data, segment);
break;
2005-01-16 06:15:51 +08:00
case OUT_REL2ADR:
fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
(uint16_t)*(int64_t *)data, segment);
2005-01-16 06:15:51 +08:00
break;
case OUT_REL4ADR:
fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
(uint32_t)*(int64_t *)data,
2005-01-16 06:15:51 +08:00
segment);
break;
case OUT_REL8ADR:
fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
(uint64_t)*(int64_t *)data, segment);
break;
2005-01-16 06:15:51 +08:00
default:
fprintf(ofile, "unknown\n");
2005-01-16 06:15:51 +08:00
break;
2002-05-01 04:51:32 +08:00
}
}
static void dbg_sectalign(int32_t seg, unsigned int value)
{
fprintf(ofile, "set alignment (%d) for segment (%u)\n",
seg, value);
}
static int32_t dbg_segbase(int32_t segment)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:52:49 +08:00
return segment;
2002-05-01 04:51:32 +08:00
}
static int dbg_directive(enum directives directive, char *value, int pass)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
directives[directive], value, pass);
2002-05-01 04:52:49 +08:00
return 1;
2002-05-01 04:51:32 +08:00
}
static void dbg_filename(char *inname, char *outname)
2002-05-01 04:53:55 +08:00
{
standard_extension(inname, outname, ".dbg");
2002-05-01 04:51:32 +08:00
}
static int dbg_set_info(enum geninfo type, char **val)
2002-05-01 04:53:55 +08:00
{
2005-01-16 06:15:51 +08:00
(void)type;
(void)val;
2002-05-01 04:53:55 +08:00
return 0;
}
2005-01-16 06:15:51 +08:00
char *types[] = {
2005-01-16 06:15:51 +08:00
"unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
2002-05-01 04:53:55 +08:00
};
static void dbgdbg_init(void)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, " With debug info\n");
2002-05-01 04:53:55 +08:00
}
static void dbgdbg_cleanup(void)
{
}
static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "dbglinenum %s(%"PRId32") := %08"PRIx32"\n",
lnfname, lineno, segto);
2002-05-01 04:53:55 +08:00
}
static void dbgdbg_deflabel(char *name, int32_t segment,
int64_t offset, int is_global, char *special)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
2005-01-16 06:15:51 +08:00
name,
2002-05-01 04:53:55 +08:00
segment, offset,
2005-01-16 06:15:51 +08:00
is_global == 2 ? "common" : is_global ? "global" : "local",
is_global, special ? ": " : "", special);
2002-05-01 04:53:55 +08:00
}
static void dbgdbg_define(const char *type, const char *params)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
2002-05-01 04:53:55 +08:00
}
2005-01-16 06:15:51 +08:00
static void dbgdbg_output(int output_type, void *param)
2002-05-01 04:53:55 +08:00
{
2005-01-16 06:15:51 +08:00
(void)output_type;
(void)param;
2002-05-01 04:53:55 +08:00
}
static void dbgdbg_typevalue(int32_t type)
2002-05-01 04:53:55 +08:00
{
fprintf(ofile, "new type: %s(%"PRIX32")\n",
2005-01-16 06:15:51 +08:00
types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
2002-05-01 04:53:55 +08:00
}
static struct dfmt debug_debug_form = {
"Trace of all info passed to debug stage",
"debug",
dbgdbg_init,
dbgdbg_linnum,
dbgdbg_deflabel,
dbgdbg_define,
dbgdbg_typevalue,
dbgdbg_output,
dbgdbg_cleanup,
};
static struct dfmt *debug_debug_arr[3] = {
2005-01-16 06:15:51 +08:00
&debug_debug_form,
&null_debug_form,
NULL
2002-05-01 04:53:55 +08:00
};
2002-05-01 04:51:32 +08:00
struct ofmt of_dbg = {
"Trace of all info passed to output stage",
"dbg",
OFMT_TEXT,
64,
2002-05-01 04:53:55 +08:00
debug_debug_arr,
&debug_debug_form,
2002-05-01 04:53:55 +08:00
NULL,
2002-05-01 04:51:32 +08:00
dbg_init,
2002-05-01 04:53:55 +08:00
dbg_set_info,
2002-05-01 04:51:32 +08:00
dbg_out,
dbg_deflabel,
dbg_section_names,
dbg_sectalign,
2002-05-01 04:51:32 +08:00
dbg_segbase,
dbg_directive,
dbg_filename,
dbg_cleanup
};
2005-01-16 06:15:51 +08:00
#endif /* OF_DBG */