Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2020-05-07 20:06:46

bbbbbr
Member
Registered: 2019-03-04
Posts: 104

GBDK C Source debugging in BGB - sorta

The recent post about using BGB debug messages in GBDK lead me to spend a little time exploring SDCC+LCC symbol output and compatibility with BGB.

The result is a useful abomination: merging C Source into the symbol labels. It's not the "Real Thing" and looks a little messy in BGB, but does work and can give a little more clarity when stepping in the debugger (or figuring out where to set your breakpoint without having to consult the .sym + .asm output).

The labels get truncated if they're too long.
Using the Tab key you can easily turn the symbol view on/off.

Two questions:
* Does anyone else have GBDK + BGB debugging tips or a setup that works for them?

* BGB has a "Local Symbols" option. I wasn't able to find documentation about this, does anyone have more information about it?

https://i.postimg.cc/fT6nPHGm/gbdk-bgb-source-symbol-source-debugging.png

EDIT: add short summary...

These are the flags added :
SDCC: --debug
LCC: -Wl-j

Then the main symbol file has the "A$..." lines are stripped, and the "C$..." (C source) lines are used to pull in the source from the .asm files generated during compile time. It's a short perl script for now.

Last edited by bbbbbr (2020-05-07 20:13:37)

Offline

 

#2 2020-05-08 14:44:26

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

i have. not tips, the subject for the research. SDCC has the support for profiling: --profile switch. that makes SDCC to emit this code:

Code:

ld a, #3
rst 0x08

to enter profiler and

Code:

ld a, #4
rst 0x08

to exit.

there is a special byte sequence in crt by the absolute address 0x08 for some unknown (to me) emulator or a hardware simulator. this point may be patched to make this work with BGB. i can fix either the code emitter as i did here: https://github.com/untoxa/experiments_w … _and_rgbds or the library. the only question is: what exactly we should do here? generate messages to "debug messages" window?

Last edited by toxa (2020-05-08 14:45:21)

Offline

 

#3 2020-05-10 04:24:18

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

i made this: https://github.com/untoxa/gbdk-2020/com … daf65ef1a5
and this is the statistics script and example: https://gist.github.com/untoxa/76b5243d … 56cafb6f3c
and the output:

Code:

_main:_test_profiler:_test_profile2                MIN:      10 AVG:     76.50 MAX:     143
_main:_test_profiler                               MIN:    7080 AVG:   7080.00 MAX:    7080
_main                                              MIN:    9405 AVG:   9405.00 MAX:    9405

Last edited by toxa (2020-05-10 04:49:40)

Offline

 

#4 2020-05-15 08:14:47

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

i made some kind of a toolkit out of all above: https://github.com/untoxa/bgb_profiling_toolkit

with a help of it, you can make this kind of stat for future analysis
https://d.radikal.ru/d43/2005/20/26966ce79ae6.png

Last edited by toxa (2020-05-15 08:17:39)

Offline

 

#5 2020-06-15 01:00:08

bbbbbr
Member
Registered: 2019-03-04
Posts: 104

Re: GBDK C Source debugging in BGB - sorta

Thanks for sharing this info, and for posting the source. We are seeing some good improvements in the tool set lately.


With regard to the original post on this thread, I never posted the short script that reprocesses the symbols to include the C source. So here it is (src2sym).

It can be called like this:

Code:

# if this is a debug build, reprocess the mainsymbol output to include C source
ifdef CDEBUG
    @echo Reprocessing Symbols to include C Source
    mv $(OBJDIR)/$(PROJECT_NAME).sym $(OBJDIR)/$(PROJECT_NAME)_raw.sym
    $(SRC2SYM) $(OBJDIR)/$(PROJECT_NAME)_raw.sym > $(OBJDIR)/$(PROJECT_NAME).sym
endif

src2sym:

Code:

#!/usr/bin/perl -w

use strict;
use warnings;
use File::Basename;

sub FindSourceLine;

my $MainSymbolFileName = "";
if ($ARGV[0]) {
    $MainSymbolFileName = $ARGV[0];
    open(MainSymbolFile, $MainSymbolFileName) or die("File $MainSymbolFileName not found");

    my ($SymbolFileName, $SymbolPath, $SymbolPathSuffix) = fileparse($MainSymbolFileName);

    while (my $FileLine = <MainSymbolFile>) {

        # First convert C source references to labels that include the source
        if ($FileLine =~ /(\d\d\:\w\w\w\w )(C\$)(\w*)\.c\$(\d*).*/) {
            # $1 : Symbol Address info
            # $2 : Label start ("C$")
            # $3 : C source file name *without* file extension
            # $4 : C source line number
            my $CLabelFileNameNoExt = $3;
            my $CLabelSourceLineNum = $4;

            my $CSourceLine = FindSourceLine($SymbolPath, $CLabelFileNameNoExt, $CLabelSourceLineNum);
            if ($CSourceLine) {
                # Output Reformatted address and truncated label
                print $1.$2.$3."\@".$CLabelSourceLineNum.":";
                # Append C source code line to output
                print $CSourceLine."\n";
            }

        } elsif ($FileLine =~ /(\d\d\:\w\w\w\w )A\$.*\n/) {
            # strip out ASM lines
            # print $FileLine." -> (ASM, REMOVE)\n";
        } else {
            # Pass the remainder on through
            print $FileLine;
        }

    }
    close(MainSymbolFile);
} else {
    print "Please specify main symbol file (located in same dir as individual .asm files)\n";
}


sub FindSourceLine {
    my ($Directory, $FileName, $CLineNum) = @_;
    # print "Checking: $Directory, $FileName, $CLineNum\n";

    if ($Directory and $FileName and $CLineNum) {
        my $SourceFileFullPath = $Directory.$FileName."\.asm";

        if (open(SourceFile, $SourceFileFullPath)) {

            while (my $FileLine = <SourceFile>) {

                # First convert C source references to labels that include the source
                if($FileLine =~ /\;src\/$FileName\.c\:$CLineNum\: (.*)/) {
                    # $1 : C source line

                    my $CSourceLine = $1;
                    $CSourceLine =~ s/\n//g; # remove newlines
                    # $CSourceLine =~ s/ /_/g; # convert spaces to underscores
                    $CSourceLine =~ s/ //g; # strip spaces
                    $CSourceLine =~ s/\;/\:/g; # convert semicolons to colons
                    # print "FOUND: $FileLine ---> $CSourceLine \n";
                    return $CSourceLine;
                }

            }
            close(SourceFile);
        } else {
            # print "file not found: $SourceFileFullPath\n";
        }

    } else {
        # print "Please specify main symbol file (located in same dir as individual .asm files)\n";
    }
}

Offline

 

#6 2020-06-15 12:15:01

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

btw, there is a problem with linker-embedded far-fixer with that --debug sdcc switch. i reported Zalo, hope'll he fix that soon.

Offline

 

#7 2020-06-23 18:04:04

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

there is also a bug in SDCC: https://sourceforge.net/p/sdcc/bugs/3068/

Offline

 

#8 2020-07-01 12:36:19

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

That SDCC bug is fixed: https://sourceforge.net/p/sdcc/bugs/3068/

Now, if you also take an updated gbdk-2020 from "develop" branch, and SDCC from snapshot: http://sdcc.sourceforge.net/snap.php you can use that --debug switch without any problems!

Last edited by toxa (2020-07-01 12:37:27)

Offline

 

#9 2020-08-03 16:09:11

toxa
Member
Registered: 2020-02-13
Posts: 279

Re: GBDK C Source debugging in BGB - sorta

native sdcc toolset sdldgb and makebin does not support no$gmb symbol files. instead it emits NoICE .noi format files that may be easily translated into *.sym, or, which is even more interesting, *.cdb format files with exitended metadata.

compiler should be called with --debug switch and sdldgb with either -j or -y or both.

Code:

  -j   NoICE Debug output as (out)file[.noi]
  -y   SDCDB Debug output as (out)file[.cdb]

Last edited by toxa (2020-08-03 16:10:32)

Offline

 

#10 2020-10-06 19:10:17

bbbbbr
Member
Registered: 2019-03-04
Posts: 104

Re: GBDK C Source debugging in BGB - sorta

Minor update for this script to work with GBDK 4.0. Might have bugs around ROM banking since I haven't tried it with anything other than a 32k unbanked ROM.

Code:

# if this is a debug build, reprocess the symbol output to include C source
ifdef CDEBUG
    @echo Reprocessing Symbols to include C Source
    $(SRC2SYM) $(OBJDIR)/$(PROJECT_NAME).noi > $(OBJDIR)/$(PROJECT_NAME).sym
endif

Code:

#!/usr/bin/perl -w

# == Source-ish debugging in BGB ==
#
# Merges C Source into the symbol labels.
# This isn't the "Real Thing" and looks messy in BGB,
# but does work and can give a little more clarity when
# stepping in the debugger (or figuring out where to set
# your breakpoint without having to consult the .sym + .asm output).

# The labels get truncated if they're too long.
# Using the Tab key you can easily turn the symbol view on/off.
#
# These are the flags added :
# SDCC: --debug
# LCC: -Wl-j
#
# Then the main symbol file has the "A$..." lines stripped,
# and the "C$..." (C source) lines are used to pull in the
# source from the .asm files generated during compile time.
#
# Example use in a makefile:
# ifdef CDEBUG
#     @echo Reprocessing Symbols to include C Source
#     mv $(OBJDIR)/$(PROJECT_NAME).sym $(OBJDIR)/$(PROJECT_NAME)_raw.sym
#     $(SRC2SYM) $(OBJDIR)/$(PROJECT_NAME)_raw.sym > $(OBJDIR)/$(PROJECT_NAME).sym
# endif



use strict;
use warnings;
use File::Basename;

sub FindSourceLine;

my $MainSymbolFileName = "";
if ($ARGV[0]) {
    $MainSymbolFileName = $ARGV[0];
    open(MainSymbolFile, $MainSymbolFileName) or die("File $MainSymbolFileName not found");

    my ($SymbolFileName, $SymbolPath, $SymbolPathSuffix) = fileparse($MainSymbolFileName);

    while (my $FileLine = <MainSymbolFile>) {

        # FROM: DEF C$player_hinting.c$119$1_0$194 0x5677
        #   TO: 01:5677 C$player_hinting@119:spr_x=(player_x*BRD_UNIT_SIZE)+BRD_PIECE_X_OFFSET:
        # First convert C source references to labels that include the source
        if ($FileLine =~ /DEF (C\$)(\w*)\.c\$(\d*)\$.*0x(\w*).*/) {
            # $1 : Label start ("C$")
            # $2 : C source file name *without* file extension
            # $3 : C source line number
            # $4 : Symbol Address info
            my $CLabelFileNameNoExt = $2;
            my $CLabelSourceLineNum = $3;

            my $CSourceLine = FindSourceLine($SymbolPath, $CLabelFileNameNoExt, $CLabelSourceLineNum);
            if ($CSourceLine) {
                # Output Reformatted address and truncated label
                print "00\:".$4." ".$1.$2."\@".$CLabelSourceLineNum.":";
                # Append C source code line to output
                print $CSourceLine."\n";
            }

        } elsif ($FileLine =~ /DEF (A\$.*)\n/) {
            #ex: DEF A$player_hinting$320 0x5677
            # strip out ASM lines
            # print $FileLine." -> (ASM, REMOVE)\n";
        } elsif ($FileLine =~ /DEF (.*) 0x(\w*).*/) {
            #ex: DEF _joypad 0x6E97
            # This was not used in old gbdk/sdcc version
            # $1 : symbol identifier
            # $2 : symbol address
            # reformat other symbol lines
            print "00\:".$2." ".$1."\n";
        } else {
            # strip out other unknown lines
            # print $FileLine." -> (OTHER, REMOVE)\n";
        }

    }
    close(MainSymbolFile);
} else {
    print "Please specify main symbol file (located in same dir as individual .asm files)\n";
}


sub FindSourceLine {
    my ($Directory, $FileName, $CLineNum) = @_;
    # print "Checking: $Directory, $FileName, $CLineNum\n";

    if ($Directory and $FileName and $CLineNum) {
        my $SourceFileFullPath = $Directory.$FileName."\.asm";

        if (open(SourceFile, $SourceFileFullPath)) {

            while (my $FileLine = <SourceFile>) {

                # First convert C source references to labels that include the source
                if($FileLine =~ /\;src\/$FileName\.c\:$CLineNum\: (.*)/) {
                    # $1 : C source line

                    my $CSourceLine = $1;
                    $CSourceLine =~ s/\n//g; # remove newlines
                    # $CSourceLine =~ s/ /_/g; # convert spaces to underscores
                    $CSourceLine =~ s/ //g; # strip spaces
                    $CSourceLine =~ s/\;/\:/g; # convert semicolons to colons
                    # print "FOUND: $FileLine ---> $CSourceLine \n";
                    return $CSourceLine;
                }

            }
            close(SourceFile);
        } else {
            # print "Source file scan: File not found: $SourceFileFullPath\n";
        }

    } else {
        # print "Source file scan: Missing one of the following: Directory, Filename or C Line Number\n";
    }
}

Last edited by bbbbbr (2020-10-06 19:11:19)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson