Discussion:
D globals in gdb
Martin Krejcirik
2012-03-31 14:58:47 UTC
Permalink
Hi all,

is there any way how to print a content of a D global variable in GDB ?
--
mk
Mihail Zenkov
2012-04-02 10:56:48 UTC
Permalink
On Saturday, 31 March 2012 at 14:58:48 UTC, Martin Krejcirik
Post by Martin Krejcirik
Hi all,
is there any way how to print a content of a D global variable
in GDB ?
Try in gdb:

p 'module_name.glob_var'

P.S. You can also use autocomplete: p 'module_name.<TAB><TAB>
Martin Krejcirik
2012-04-02 17:36:27 UTC
Permalink
Post by Mihail Zenkov
p 'module_name.glob_var'
argh, the single quotes. Thanks, now it works if the global is
declared shared.
But if it isn't, print shows just the init value, no changed
data. Any idea ?

--
mk
simendsjo
2012-04-02 17:56:07 UTC
Permalink
On Mon, 02 Apr 2012 19:36:27 +0200, Martin Krejcirik <mk-junk at i-line.cz>
Post by Mihail Zenkov
p 'module_name.glob_var'
argh, the single quotes. Thanks, now it works if the global is declared
shared.
But if it isn't, print shows just the init value, no changed data. Any
idea ?
--
mk
Unless it's shared or __gshared, every thread gets it's own copy. D
defaults to thread local storage (TLS).
Martin Krejcirik
2012-04-02 22:24:34 UTC
Permalink
Post by simendsjo
Unless it's shared or __gshared, every thread gets it's own copy. D
defaults to thread local storage (TLS).
I know but the problem is, that gdb doesn't show the changed value of
TLS variable. Example:

import std.stdio;

shared int shrgl = 1;
int tlsgl = 10;

void main()
{
writefln("%s %s", shrgl, tlsgl); // prints 1,10
shrgl++; tlsgl++;
writefln("%s %s", shrgl, tlsgl); // prints 2,11
==> gdb p 'tls.shrgl' ==> 2
==> gdb p 'tls.tlslg' ==> 10
}

--
mk
Mihail Zenkov
2012-04-03 05:47:40 UTC
Permalink
Post by Martin Krejcirik
Post by simendsjo
Unless it's shared or __gshared, every thread gets it's own
copy. D
defaults to thread local storage (TLS).
I know but the problem is, that gdb doesn't show the changed
value of
import std.stdio;
shared int shrgl = 1;
int tlsgl = 10;
void main()
{
writefln("%s %s", shrgl, tlsgl); // prints 1,10
shrgl++; tlsgl++;
writefln("%s %s", shrgl, tlsgl); // prints 2,11
==> gdb p 'tls.shrgl' ==> 2
==> gdb p 'tls.tlslg' ==> 10
}
--
mk
Can't reproduce. With gdc and dmd-2.058 I have correct result.
GDB-7.3
simendsjo
2012-04-03 07:40:05 UTC
Permalink
On Tue, 03 Apr 2012 07:47:40 +0200, Mihail Zenkov
Post by Martin Krejcirik
Post by simendsjo
Unless it's shared or __gshared, every thread gets it's own copy. D
defaults to thread local storage (TLS).
I know but the problem is, that gdb doesn't show the changed value of
import std.stdio;
shared int shrgl = 1;
int tlsgl = 10;
void main()
{
writefln("%s %s", shrgl, tlsgl); // prints 1,10
shrgl++; tlsgl++;
writefln("%s %s", shrgl, tlsgl); // prints 2,11
==> gdb p 'tls.shrgl' ==> 2
==> gdb p 'tls.tlslg' ==> 10
}
--
mk
Can't reproduce. With gdc and dmd-2.058 I have correct result. GDB-7.3
I cannot reproduce this on dmd-2.059 trunk either.
Martin Krejcirik
2012-04-04 01:23:33 UTC
Permalink
Post by simendsjo
Can't reproduce. With gdc and dmd-2.058 I have correct result. GDB-7.3
I cannot reproduce this on dmd-2.059 trunk either.
Interesting. I'm using dmd 2.058 binary (.zip) on debian squeeze 32bit,
gdb-minimal 7.3-1~bpo60+1 from backports. Even tried to compile gdb 7.4,
but still the same behavior.
--
mk
Mihail Zenkov
2012-04-04 07:01:37 UTC
Permalink
On Wednesday, 4 April 2012 at 01:23:34 UTC, Martin Krejcirik
Post by Martin Krejcirik
Post by simendsjo
Post by Mihail Zenkov
Can't reproduce. With gdc and dmd-2.058 I have correct
result. GDB-7.3
I cannot reproduce this on dmd-2.059 trunk either.
Interesting. I'm using dmd 2.058 binary (.zip) on debian
squeeze 32bit,
gdb-minimal 7.3-1~bpo60+1 from backports. Even tried to compile gdb 7.4,
but still the same behavior.
I also use dmd-2.058/linux/bin32/dmd.
1. Try compile without options, just 'dmd tls.d'
2. How you set break point in the end of program? Try add this
hack:
int *p = null;
*p = 5;
It segfault you program and you can check backtrace and variable
state.
Martin Krejcirik
2012-04-04 10:44:59 UTC
Permalink
Post by Mihail Zenkov
1. Try compile without options, just 'dmd tls.d'
I'm not sure what that should accomplish, as I wouldn't get the
debug info.
Post by Mihail Zenkov
2. How you set break point in the end of program? Try add this
breakpoints are ok. In fact everything is ok, except it looks
like that gdb is accessing (in print, set var) a "shadow" non-tls
copy of my tls variable.

Consider this example:
import std.stdio;

__gshared int shrvar = 1;
int tlsvar = 5;

void main()
{
writefln("%d %d | %#x %#x", shrvar, tlsvar, &shrvar, &tlsvar);
// 1, 5
shrvar++; tlsvar++;
writefln("%d %d | %#x %#x", shrvar, tlsvar, &shrvar, &tlsvar);
// gdb p 'tls.tlsvar' = 5
}

Now running GDB:

(gdb) b 10
Breakpoint 1 at 0x806b7ba: file tls.d, line 10.
(gdb) r
Starting program: /home/mk/dmd/tls
[Thread debugging using libthread_db enabled]
Using host libthread_db library
"/lib/i686/cmov/libthread_db.so.1".
1 5 | 0x808f308 0xf7e466c8

Breakpoint 1, D main () at tls.d:10
10 writefln("%d %d | %#x %#x", shrvar, tlsvar, &shrvar,
&tlsvar); // gdb p 'tls.tlslg' = 5
(gdb) n
2 6 | 0x808f308 0xf7e466c8
11 }
(gdb) info address tls.shrvar
Symbol "tls.shrvar()" is static storage at address 0x808f308.
(gdb) info address tls.tlsvar
Symbol "tls.tlsvar()" is static storage at address 0x808f004.
(gdb) x 0xf7e466c8
0xf7e466c8: 0x00000006
(gdb) x 0x808f004
0x808f004: 0x00000005

As you can see, the address of tlsvar in gdb is different then
the one from writeln.

Trying similar C program in gdb, it correctly recognize the TLS
variable:
(gdb) info address shrvar
Symbol "shrvar" is static storage at address 0x8049658.
(gdb) info address tlsvar
Symbol "tlsvar" is a thread-local variable at offset 0x0 in the
thread-local storage for `/home/mk/dmd/glob'.

I read somwhere that it is necessary to link libpthread, not sure
if that applies to DMD too, but it doesn't seem to have any
effect anyway.
Mihail Zenkov
2012-04-04 13:50:01 UTC
Permalink
On Wednesday, 4 April 2012 at 10:45:00 UTC, Martin Krejcirik
Post by Martin Krejcirik
Post by Mihail Zenkov
1. Try compile without options, just 'dmd tls.d'
I'm not sure what that should accomplish, as I wouldn't get the
debug info.
DMD without any flags produce enough debug info to see global
variable.
I reproduce this problem: only dmd with flag -g have it. Without
this flag or with -gc gdb show correct value.
Martin Krejcirik
2012-04-04 14:02:02 UTC
Permalink
Post by Mihail Zenkov
DMD without any flags produce enough debug info to see global
variable.
I reproduce this problem: only dmd with flag -g have it.
Without this flag or with -gc gdb show correct value.
Thanks, -gc works indeed.

Continue reading on narkive:
Loading...