Debugging D code with GDB

Eduard Staniloiu edi33416 at gmail.com
Sat Nov 27 14:17:11 UTC 2021


Hello,

I'm trying to use `gdb` to debug D binaries, but I'm having 
trouble accessing the methods of a struct or class. It seems that 
`gdb` doesn't see them.

Given the following simple example
```
// test.d
struct S
{
     int x;

     void myPrint() { writefln("x is %s\n", x); }
}

void main(string[] args)
{
     S s;
}
```
Compile the source file with debug simbols (`dmd -g test.d 
-of=test`) and open the binary with gdb (`gdb test`) and run the 
following

```
> break _Dmain # break at D entry point
> run
> ptype s
type = struct test.S {
     int x;
}
> print s.myPrint()
Structure has no component named myPrint.
```

As you can see, when I try to access the `myPrint()` method I get 
the error
"Structure has no component named myPrint."

Looking up gdb's bugzilla, I've found this issue [0] that 
basically says that gdb treats D as C code (and that would 
explain why it doesn't look for function definitions inside D 
structs).

A simple "fix"/"hack" to this problem is to define a helper 
function that will take my symbol as a parameter and internally 
call the function that I need, like below:
```
extern (C) void helper(ref S s) { s.myPrint(); }
```

While this does the job for this trivial example, it doesn't 
scale for a real project.

Are there any solutions to this problem?

Looking forward to your answers,
Edi

[0] - https://sourceware.org/bugzilla/show_bug.cgi?id=22480





More information about the Digitalmars-d-learn mailing list