Task 133: .DBG File Format

Task 133: .DBG File Format

File Format Specifications for the .DBG File Format

Based on available information, the .DBG file format is not a single standardized specification but is commonly used for debug information in various contexts, such as those generated by the ca65 assembler in the cc65 suite for 6502-based systems. In this context, it is a text-based file containing debug directives that provide information about source files, lines, symbols, types, and segments. The structure consists of lines starting with ".dbg" followed by a sub-directive and comma-separated parameters, with strings in double quotes and numbers in decimal form. The known sub-directives are "version", "file", "line", "sym", "type", and "cseg".

1. List of All Properties of This File Format Intrinsic to Its File System

The properties are the fields associated with the directives in the .DBG file. These include:

  • Version: An integer indicating the version of the debug information.
  • Path: A string representing the path to the source file.
  • Size: An integer representing the size of the source file in bytes.
  • Mtime: An integer representing the modification time of the source file (Unix timestamp).
  • File: A string representing the file identifier for line or symbol directives.
  • Line: An integer representing the line number in the source file.
  • Pos: An integer representing the position in the file.
  • Type: An integer or string representing the type for line, symbol, or type directives.
  • Span: An integer representing the span for line or symbol directives.
  • Name: A string representing the name for symbol, type, or code segment directives.
  • Value: An integer representing the value for symbol directives.
  • Def: An integer representing the definition count for symbol directives.
  • Ref: An integer representing the reference count for symbol directives.
  • Addrsize: An integer representing the address size for symbol directives.
  • Bytes: A list of integers representing byte values for type directives.

Despite extensive searches across web resources, GitHub repositories, and related documentation, I was unable to locate direct download links for sample .DBG files. These files are typically generated during compilation processes and are not commonly hosted for public download.

3. Ghost Blog Embedded HTML JavaScript for Drag and Drop .DBG File Dump

The following is an HTML snippet with embedded JavaScript that can be embedded in a Ghost blog post. It allows users to drag and drop a .DBG file onto a designated area, parses the file, extracts the properties from the directives, and displays them on the screen in a structured list.

Drag and drop a .DBG file here

4. Python Class for .DBG File Handling

The following Python class can open a .DBG file, decode and read its directives, print the properties to the console, and write the parsed directives to a new file.

class DBGFile:
    def __init__(self, filename):
        self.filename = filename
        self.directives = []

    def read(self):
        with open(self.filename, 'r') as f:
            for line in f:
                line = line.strip()
                if line.startswith('.dbg'):
                    parts = [p.strip().replace('"', '') for p in line[4:].split(',')]
                    self.directives.append(parts)

    def print_properties(self):
        print("Extracted Properties from .DBG File:")
        for directive in self.directives:
            print(', '.join(directive))

    def write(self, new_filename):
        with open(new_filename, 'w') as f:
            for directive in self.directives:
                params = [f'"{p}"' if not p.isdigit() else p for p in directive[1:]]
                f.write(f'.dbg {directive[0]}, {", ".join(params)}\n')

5. Java Class for .DBG File Handling

The following Java class can open a .DBG file, decode and read its directives, print the properties to the console, and write the parsed directives to a new file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class DBGFile {
    private String filename;
    private List<String[]> directives = new ArrayList<>();

    public DBGFile(String filename) {
        this.filename = filename;
    }

    public void read() throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.startsWith(".dbg")) {
                    String[] parts = Arrays.stream(line.substring(4).trim().split(","))
                        .map(p -> p.trim().replace("\"", ""))
                        .toArray(String[]::new);
                    directives.add(parts);
                }
            }
        }
    }

    public void printProperties() {
        System.out.println("Extracted Properties from .DBG File:");
        for (String[] directive : directives) {
            System.out.println(String.join(", ", directive));
        }
    }

    public void write(String newFilename) throws IOException {
        try (FileWriter writer = new FileWriter(newFilename)) {
            for (String[] directive : directives) {
                String[] params = new String[directive.length - 1];
                for (int i = 1; i < directive.length; i++) {
                    params[i - 1] = directive[i].matches("\\d+") ? directive[i] : "\"" + directive[i] + "\"";
                }
                writer.write(".dbg " + directive[0] + ", " + String.join(", ", params) + "\n");
            }
        }
    }
}

6. JavaScript Class for .DBG File Handling

The following JavaScript class can open a .DBG file (using Node.js fs module), decode and read its directives, print the properties to the console, and write the parsed directives to a new file.

const fs = require('fs');

class DBGFile {
    constructor(filename) {
        this.filename = filename;
        this.directives = [];
    }

    read() {
        const content = fs.readFileSync(this.filename, 'utf8');
        const lines = content.split('\n');
        lines.forEach(line => {
            line = line.trim();
            if (line.startsWith('.dbg')) {
                const parts = line.substring(4).trim().split(',').map(p => p.trim().replace(/"/g, ''));
                this.directives.push(parts);
            }
        });
    }

    printProperties() {
        console.log('Extracted Properties from .DBG File:');
        this.directives.forEach(directive => {
            console.log(directive.join(', '));
        });
    }

    write(newFilename) {
        let output = '';
        this.directives.forEach(directive => {
            const params = directive.slice(1).map(p => isNaN(p) ? `"${p}"` : p);
            output += `.dbg ${directive[0]}, ${params.join(', ')}\n`;
        });
        fs.writeFileSync(newFilename, output);
    }
}

7. C Class for .DBG File Handling

The following C code defines a struct acting as a class to open a .DBG file, decode and read its directives, print the properties to the console, and write the parsed directives to a new file. (Note: This uses dynamic memory allocation for simplicity; error handling is minimal for brevity.)

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

#define MAX_LINE 1024
#define MAX_DIRECTIVES 1000
#define MAX_PARTS 20

typedef struct {
    char *filename;
    char **directives[MAX_DIRECTIVES];
    int num_directives;
} DBGFile;

DBGFile* dbgfile_create(const char *filename) {
    DBGFile *dbg = malloc(sizeof(DBGFile));
    dbg->filename = strdup(filename);
    dbg->num_directives = 0;
    return dbg;
}

void dbgfile_read(DBGFile *dbg) {
    FILE *f = fopen(dbg->filename, "r");
    if (!f) return;
    char line[MAX_LINE];
    while (fgets(line, MAX_LINE, f)) {
        if (strstr(line, ".dbg") == line) {
            char *parts[MAX_PARTS];
            int count = 0;
            char *token = strtok(line + 4, ",");
            while (token && count < MAX_PARTS) {
                parts[count] = strdup(token);
                token = strtok(NULL, ",");
                count++;
            }
            dbg->directives[dbg->num_directives] = malloc(count * sizeof(char*));
            for (int i = 0; i < count; i++) {
                // Trim spaces and quotes
                char *p = parts[i];
                while (*p == ' ') p++;
                if (*p == '"') p++;
                char *end = p + strlen(p) - 1;
                if (*end == '"') *end = '\0';
                dbg->directives[dbg->num_directives][i] = strdup(p);
            }
            dbg->num_directives++;
        }
    }
    fclose(f);
}

void dbgfile_print_properties(DBGFile *dbg) {
    printf("Extracted Properties from .DBG File:\n");
    for (int i = 0; i < dbg->num_directives; i++) {
        for (int j = 0; dbg->directives[i][j]; j++) {
            printf("%s", dbg->directives[i][j]);
            if (dbg->directives[i][j+1]) printf(", ");
        }
        printf("\n");
    }
}

void dbgfile_write(DBGFile *dbg, const char *new_filename) {
    FILE *f = fopen(new_filename, "w");
    if (!f) return;
    for (int i = 0; i < dbg->num_directives; i++) {
        fprintf(f, ".dbg %s", dbg->directives[i][0]);
        for (int j = 1; dbg->directives[i][j]; j++) {
            int is_num = 1;
            for (char *p = dbg->directives[i][j]; *p; p++) {
                if (!isdigit(*p)) is_num = 0;
            }
            if (is_num) {
                fprintf(f, ", %s", dbg->directives[i][j]);
            } else {
                fprintf(f, ", \"%s\"", dbg->directives[i][j]);
            }
        }
        fprintf(f, "\n");
    }
    fclose(f);
}

void dbgfile_destroy(DBGFile *dbg) {
    for (int i = 0; i < dbg->num_directives; i++) {
        for (int j = 0; dbg->directives[i][j]; j++) {
            free(dbg->directives[i][j]);
        }
        free(dbg->directives[i]);
    }
    free(dbg->filename);
    free(dbg);
}