summaryrefslogtreecommitdiff
path: root/output/outdbg.c
blob: 5000407bfcafcdab55d34dfeefd27c4984e96292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* outdbg.c	output routines for the Netwide Assembler to produce
 *		a debugging trace
 *
 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
 * Julian Hall. All rights reserved. The software is
 * redistributable under the licence given in the file "Licence"
 * distributed in the NASM archive.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "nasm.h"
#include "nasmlib.h"
#include "outform.h"

#ifdef OF_DBG

struct Section {
    struct Section *next;
    long number;
    char *name;
} *dbgsect;

FILE *dbgf;
efunc dbgef;

struct ofmt of_dbg;
static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
{
    (void)eval;

    dbgf = fp;
    dbgef = errfunc;
    dbgsect = NULL;
    (void)ldef;
    fprintf(fp, "NASM Output format debug dump\n");
    of_dbg.current_dfmt->init(&of_dbg, 0, fp, errfunc);

}

static void dbg_cleanup(int debuginfo)
{
    (void)debuginfo;
    of_dbg.current_dfmt->cleanup();
    while (dbgsect) {
        struct Section *tmp = dbgsect;
        dbgsect = dbgsect->next;
        nasm_free(tmp->name);
        nasm_free(tmp);
    }
    fclose(dbgf);
}

static long dbg_section_names(char *name, int pass, int *bits)
{
    int seg;

    /*
     * We must have an initial default: let's make it 16.
     */
    if (!name)
        *bits = 16;

    if (!name)
        fprintf(dbgf, "section_name on init: returning %d\n",
                seg = seg_alloc());
    else {
        int n = strcspn(name, " \t");
        char *sname = nasm_strndup(name, n);
        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(dbgf, "section_name %s (pass %d): returning %d\n",
                    name, pass, seg);
        }
    }
    return seg;
}

static void dbg_deflabel(char *name, long segment, long offset,
                         int is_global, char *special)
{
    fprintf(dbgf, "deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
            name, segment, offset,
            is_global == 2 ? "common" : is_global ? "global" : "local",
            is_global, special ? ": " : "", special);
}

static void dbg_out(long segto, void *data, unsigned long type,
                    long segment, long wrt)
{
    long realbytes = type & OUT_SIZMASK;
    long ldata;
    int id;

    type &= OUT_TYPMASK;

    fprintf(dbgf, "out to %lx, len = %ld: ", segto, realbytes);

    switch (type) {
    case OUT_RESERVE:
        fprintf(dbgf, "reserved.\n");
        break;
    case OUT_RAWDATA:
        fprintf(dbgf, "raw data = ");
        while (realbytes--) {
            id = *(unsigned char *)data;
            data = (char *)data + 1;
            fprintf(dbgf, "%02x ", id);
        }
        fprintf(dbgf, "\n");
        break;
    case OUT_ADDRESS:
        ldata = 0;              /* placate gcc */
        if (realbytes == 1)
            ldata = *((char *)data);
        else if (realbytes == 2)
            ldata = *((short *)data);
        else if (realbytes == 4)
            ldata = *((long *)data);
        fprintf(dbgf, "addr %08lx (seg %08lx, wrt %08lx)\n", ldata,
                segment, wrt);
        break;
    case OUT_REL2ADR:
        fprintf(dbgf, "rel2adr %04x (seg %08lx)\n", (int)*(short *)data,
                segment);
        break;
    case OUT_REL4ADR:
        fprintf(dbgf, "rel4adr %08lx (seg %08lx)\n", *(long *)data,
                segment);
        break;
    default:
        fprintf(dbgf, "unknown\n");
        break;
    }
}

static long dbg_segbase(long segment)
{
    return segment;
}

static int dbg_directive(char *directive, char *value, int pass)
{
    fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
            directive, value, pass);
    return 1;
}

static void dbg_filename(char *inname, char *outname, efunc error)
{
    standard_extension(inname, outname, ".dbg", error);
}

static int dbg_set_info(enum geninfo type, char **val)
{
    (void)type;
    (void)val;
    return 0;
}

char *types[] = {
    "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
};
void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
{
    (void)of;
    (void)id;
    (void)fp;
    (void)error;
    fprintf(fp, "   With debug info\n");
}
static void dbgdbg_cleanup(void)
{
}

static void dbgdbg_linnum(const char *lnfname, long lineno, long segto)
{
    fprintf(dbgf, "dbglinenum %s(%ld) := %08lx\n", lnfname, lineno, segto);
}
static void dbgdbg_deflabel(char *name, long segment,
                            long offset, int is_global, char *special)
{
    fprintf(dbgf, "dbglabel %s := %08lx:%08lx %s (%d)%s%s\n",
            name,
            segment, offset,
            is_global == 2 ? "common" : is_global ? "global" : "local",
            is_global, special ? ": " : "", special);
}
static void dbgdbg_define(const char *type, const char *params)
{
    fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
}
static void dbgdbg_output(int output_type, void *param)
{
    (void)output_type;
    (void)param;
}
static void dbgdbg_typevalue(long type)
{
    fprintf(dbgf, "new type: %s(%lX)\n",
            types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
}
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] = {
    &debug_debug_form,
    &null_debug_form,
    NULL
};
struct ofmt of_dbg = {
    "Trace of all info passed to output stage",
    "dbg",
    NULL,
    debug_debug_arr,
    &null_debug_form,
    NULL,
    dbg_init,
    dbg_set_info,
    dbg_out,
    dbg_deflabel,
    dbg_section_names,
    dbg_segbase,
    dbg_directive,
    dbg_filename,
    dbg_cleanup
};

#endif                          /* OF_DBG */