www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Debugging

reply Graham St Jack <Graham.StJack internode.on.net> writes:
I have jumped back onto the D2 band-wagon after a long absence, read 
Andrei's book, and am having a great time cutting heaps of D code. My 
increase in productivity and happiness when compared to working in C++ is 
enormous. 

Bug thumbs up to everyone involved with D2, Phobos and the book.

One area I am having a bit of trouble with is debugging. Can anyone help 
me out with how to debug a D2 program in Linux? Currently I am reduced to 
printing out heaps of debug text.

Specifically:

Stack Trace:
------------

I can't get the D2 stack-trace to work properly. All I get is something 
like this, which isn't helpful:

Segmentation fault

The code I used to generate this was:
import std.stdio;
import std.file;
void foo(File file) {
    file.flush;
}
void main(string[] args) {
    File file;
    foo(file);
}
Is stack-trace support broken, or do I have to do something to enable it?

GDB:
----

What is the status of D support in GDB? The last post I saw was back in 
April. It is currently hard work to debug with gdb when it doesn't 
understand D name mangling.

Profiling:
----------

How do I profile a D2 program? When I try -profile in dmd, the resultant 
executable crashes with a segfault when I try to run it. When I try 
sysprof, I get a handful of mangled names, and no call graph.
Jul 12 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Graham St Jack:
 How do I profile a D2 program? When I try -profile in dmd, the resultant 
 executable crashes with a segfault when I try to run it.
Are you able to produce a small test case of this bug? Bye, bearophile
Jul 13 2010
parent reply Graham St Jack <Graham.StJack internode.on.net> writes:
On Tue, 13 Jul 2010 03:55:08 -0400, bearophile wrote:

 Graham St Jack:
 How do I profile a D2 program? When I try -profile in dmd, the
 resultant executable crashes with a segfault when I try to run it.
Are you able to produce a small test case of this bug? Bye, bearophile
Small test cases don't crash. I will spend a bit of time experimenting with my way-too-big test case to find out why it crashes when run after being compiled with -profile, and post my results here. One thing I noticed was that the trace.log file doesn't demangle the names properly either.
Jul 13 2010
parent reply Graham St Jack <Graham.StJack internode.on.net> writes:
On Tue, 13 Jul 2010 21:38:09 +0000, Graham St Jack wrote:

 On Tue, 13 Jul 2010 03:55:08 -0400, bearophile wrote:
 
 Graham St Jack:
 How do I profile a D2 program? When I try -profile in dmd, the
 resultant executable crashes with a segfault when I try to run it.
Are you able to produce a small test case of this bug? Bye, bearophile
Small test cases don't crash. I will spend a bit of time experimenting with my way-too-big test case to find out why it crashes when run after being compiled with -profile, and post my results here. One thing I noticed was that the trace.log file doesn't demangle the names properly either.
Here is a gdb stacktrace from a program that was compiled with -profile and -gc. The program is too big to post here, but it works just fine without the -profile. Note that the problem seems to be something to do with variant and/or concurrency. I don't like my chances, but I will dig deeper. Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xf7521b70 (LWP 32218)] 0x080834cc in trace_epi () (gdb) bt _D3std7variant17__T8VariantNVk24Z8VariantN59__T7handlerTS3std8typecons24__T5TupleTC3bob7NewFileZ5TupleZ7handlerFE3std7variant17__T8VariantNVk24Z8Va iantN4OpIDPG24hPvZi (parm=0xf7520eac, pStore=0x0, selector=3) at /home/grahams/local/dmd2/linux/bin/../../src/phobos/ std/variant.d:248 _D3std7variant17__T8VariantNVk24Z8VariantN62__T10convertsToTS3std8typecons23__T5TupleTC3bob6ActionZ5Tu leZ10convertsToMFZb (this=0xf7520ec8) at /home/grahams/local/dmd2/linux/bin/../../src/phobos/std/ variant.d:584 _D3std11concurrency10MessageBox130__T3getTDFS3std11concurrency3TidZvTDFC3bob6ActionZvTDFC3bob7NewFileZvTDFAyaZvTDFC3bob11UpdatedFileZvTDFC3bob11ScannedFileZvTDFbZvZ3getMFDFS3std11concurrency3TidZvDFC3bob6ActionZvDFC3bob7NewFileZvDFAyaZvDFC3bob11UpdatedFileZvDFC3bob11ScannedFileZvDFbZvZv9onUserMsgMFS3std11c ncurrency7MessageZb (this=0xf7521038, msg=...) at /home/grahams/local/dmd2/linux/bin/../../src/phobos/std/ concurrency.d:360 _D3std11concurrency10MessageBox130__T3getTDFS3std11concurrency3TidZvTDFC3bob6ActionZvTDFC3bob7NewFileZvTDFAyaZvTDFC3bob11UpdatedFileZvTDFC3bob11ScannedFileZvTDFbZvZ3getMFDFS3std11concurrency3TidZvDFC3bob6ActionZvDFC3bob7NewFileZvDFAyaZvDFC3bob11UpdatedFileZvDFC3bob11ScannedFileZvDFbZvZv4scanMFKS3std11concurrency36__T4ListTS3std11concurr ncy7MessageZ4ListZb (this=0xf7521038, list=0xf7d26dc8) at /home/grahams/local/dmd2/linux/bin/../../src/phobos/std/ concurrency.d:449 _D3std11concurrency10MessageBox130__T3getTDFS3std11concurrency3TidZvTDFC3bob6ActionZvTDFC3bob7NewFileZvTDFAyaZvTDFC3bob11UpdatedFileZvTDFC3bob11ScannedFileZvTDFbZvZ3getMFDFS3std11concurrency3TidZvDFC3bob6ActionZvDFC3bob7NewFileZvDFAyaZvDFC3bob11UpdatedFileZvDFC3bob11 cannedFileZvDFbZvZv (this=0xf7d26dc0, _param_6=577956023555583664, _param_5=577955439440031408, _param_4=577954855324479152, _param_3=577954408647880368, _param_2=577953807352458928, _param_1=577952742200569520, _param_0=577952226804494000) at /home/ grahams/local/dmd2/linux/bin/../../src/phobos/std/concurrency.d:463 _D3std11concurrency134__T7receiveTDFS3std11concurrency3TidZvTDFC3bob6ActionZvTDFC3bob7NewFileZvTDFAyaZvTDFC3bob11UpdatedFileZvTDFC3bob11ScannedFileZvTDFbZvZ7receiveFDFS3std11concurrency3TidZvDFC3bob6ActionZvDFC3bob7NewFileZvDFAyaZvDFC3bob11UpdatedFileZvDFC3bob11 cannedFileZvDFbZvZv (_param_6=577956023555583664, _param_5=577955439440031408, _param_4=577954855324479152, _param_3=577954408647880368, _param_2=577953807352458928, _param_1=577952742200569520, _param_0=577952226804494000) at /home/grahams/local/dmd2/linux/bin/../../ src/phobos/std/concurrency.d:228 (done_tid=..., print_deps=false) at /home/grahams/source/squid/open/ bedrock/build-tool/util/bob.d:2127 _D3std11concurrency35__T5spawnTbTS3std11concurrency3TidZ5spawnFPFbS3std11concurrency3TidZvbS3std11concurrency3TidZS3std11conc rrency3Tid4execMFZv (this=0xf7d27b80) at /home/grahams/local/dmd2/linux/bin/../../src/phobos/std/ concurrency.d:154
Jul 13 2010
parent reply Sean Kelly <sean invisibleduck.org> writes:
-profile currently doesn't work with multithreaded apps.
Jul 16 2010
parent reply Graham St Jack <graham.stjack internode.on.net> writes:
On Fri, 16 Jul 2010 21:47:50 +0000, Sean Kelly wrote:

 -profile currently doesn't work with multithreaded apps.
Darn. Are there plans to sort that out?
Jul 17 2010
parent reply Sean Kelly <sean invisibleduck.org> writes:
Graham St Jack Wrote:

 On Fri, 16 Jul 2010 21:47:50 +0000, Sean Kelly wrote:
 
 -profile currently doesn't work with multithreaded apps.
Darn. Are there plans to sort that out?
It's been on my "to do" list for ages. Timing data is less/differently useful for multithreaded code, but it should at least not crash, unlike now.
Jul 17 2010
parent Graham St Jack <graham.stjack internode.on.net> writes:
On Sat, 17 Jul 2010 14:43:38 -0400, Sean Kelly wrote:

 Graham St Jack Wrote:
 
 On Fri, 16 Jul 2010 21:47:50 +0000, Sean Kelly wrote:
 
 -profile currently doesn't work with multithreaded apps.
Darn. Are there plans to sort that out?
It's been on my "to do" list for ages. Timing data is less/differently useful for multithreaded code, but it should at least not crash, unlike now.
I find profiling very useful in multi-threaded programs for assisting with optimisation, and am keen for it to be possible. I use C++ in my day job (with gcc), and recently went through a lot of pain trying to get gprof to work, only to discover that it fundamentally doesn't work on multi-threaded code. I guess that is fair enough given that instrumented code can't use something as simple as a high-resolution clock to measure how long things take. What I ended up using was sysprof, which is an external program that does statistical sampling of the whole system. It works really well, and produces a respectable call graph that shows very clearly where the time is being spent. However, when I try it with a D2 program, I don't get a call graph - presumably because it doesn't understand the stack frames and so can't work out the call graph. This renders it almost useless for D programs.
Jul 17 2010
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 13 Jul 2010 02:26:22 -0400, Graham St Jack  
<Graham.StJack internode.on.net> wrote:

 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++ is
 enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.

 One area I am having a bit of trouble with is debugging. Can anyone help
 me out with how to debug a D2 program in Linux? Currently I am reduced to
 printing out heaps of debug text.

 Specifically:

 Stack Trace:
 ------------

 I can't get the D2 stack-trace to work properly. All I get is something
 like this, which isn't helpful:

 Segmentation fault

 The code I used to generate this was:
 import std.stdio;
 import std.file;
 void foo(File file) {
     file.flush;
 }
 void main(string[] args) {
     File file;
     foo(file);
 }
 Is stack-trace support broken, or do I have to do something to enable it?
Seg faults do not generate stack traces in Linux/MacOS. This is because a segmentation fault is generated by a signal, and it's unsafe to throw exceptions from signals. I believe seg faults can generate exceptions in Windows, but I'm not sure. Your best bet is to get a debugger working, and it will halt on the signal. As I understand it, a lot of good work was done recently on dmd (can't remember who did it) to get it working better with gdb. -Steve
Jul 13 2010
parent Graham St Jack <Graham.StJack internode.on.net> writes:
On Tue, 13 Jul 2010 08:48:50 -0400, Steven Schveighoffer wrote:

 On Tue, 13 Jul 2010 02:26:22 -0400, Graham St Jack
 <Graham.StJack internode.on.net> wrote:
 
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++
 is enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.

 One area I am having a bit of trouble with is debugging. Can anyone
 help me out with how to debug a D2 program in Linux? Currently I am
 reduced to printing out heaps of debug text.

 Specifically:

 Stack Trace:
 ------------

 I can't get the D2 stack-trace to work properly. All I get is something
 like this, which isn't helpful:

 Segmentation fault

 The code I used to generate this was: import std.stdio;
 import std.file;
 void foo(File file) {
     file.flush;
 }
 void main(string[] args) {
     File file;
     foo(file);
 }
 Is stack-trace support broken, or do I have to do something to enable
 it?
Seg faults do not generate stack traces in Linux/MacOS. This is because a segmentation fault is generated by a signal, and it's unsafe to throw exceptions from signals. I believe seg faults can generate exceptions in Windows, but I'm not sure. Your best bet is to get a debugger working, and it will halt on the signal. As I understand it, a lot of good work was done recently on dmd (can't remember who did it) to get it working better with gdb. -Steve
Thanks. I will persist with gdb and look forward to the D support coming through.
Jul 13 2010
prev sibling next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++ is
 enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
Jul 13 2010
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 07/13/2010 10:34 AM, Andrei Alexandrescu wrote:
 On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++ is
 enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
I guess the more recent site is at http://www.zero-bugs.com/2.0/product_intro.html Andrei
Jul 13 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 13 Jul 2010 11:35:55 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 On 07/13/2010 10:34 AM, Andrei Alexandrescu wrote:
 On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++  
 is
 enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
I guess the more recent site is at http://www.zero-bugs.com/2.0/product_intro.html
Does that support D? The faq says: "I have started some preliminary work to support Walter Bright's D Programming Language, and a D demangler has been contributed by the D community. However, this work is in an experimental stage." -Steve
Jul 13 2010
parent Jason House <jason.james.house gmail.com> writes:
Steven Schveighoffer Wrote:

 On Tue, 13 Jul 2010 11:35:55 -0400, Andrei Alexandrescu  
 <SeeWebsiteForEmail erdani.org> wrote:
 
 On 07/13/2010 10:34 AM, Andrei Alexandrescu wrote:
 On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++  
 is
 enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
I guess the more recent site is at http://www.zero-bugs.com/2.0/product_intro.html
Does that support D? The faq says: "I have started some preliminary work to support Walter Bright's D Programming Language, and a D demangler has been contributed by the D community. However, this work is in an experimental stage." -Steve
I never got it to work. It's very picky about the environment setup.
Jul 13 2010
prev sibling parent reply Graham St Jack <Graham.StJack internode.on.net> writes:
On Tue, 13 Jul 2010 10:34:41 -0500, Andrei Alexandrescu wrote:

 On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++
 is enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
Freudian slip... I love the book by the way - I couldn't put it down. D is totally awesome now, and there were heaps of cool new features that I didn't know about. Thanks again.
Jul 13 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 07/13/2010 04:41 PM, Graham St Jack wrote:
 On Tue, 13 Jul 2010 10:34:41 -0500, Andrei Alexandrescu wrote:

 On 07/13/2010 01:26 AM, Graham St Jack wrote:
 I have jumped back onto the D2 band-wagon after a long absence, read
 Andrei's book, and am having a great time cutting heaps of D code. My
 increase in productivity and happiness when compared to working in C++
 is enormous.

 Bug thumbs up to everyone involved with D2, Phobos and the book.
[snip] Thanks. Bug thumbs up? Clearly you had debugging on your mind :o). Check http://www.zerobugs.org/. Andrei
Freudian slip... I love the book by the way - I couldn't put it down. D is totally awesome now, and there were heaps of cool new features that I didn't know about. Thanks again.
*cough* amazon *cough* review *cough* Andrei
Jul 13 2010
prev sibling next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Graham St Jack Wrote:

 Specifically:
 
 Stack Trace:
 ------------
 
 I can't get the D2 stack-trace to work properly. All I get is something 
 like this, which isn't helpful:
 
 Segmentation fault
 
 The code I used to generate this was:
 import std.stdio;
 import std.file;
 void foo(File file) {
     file.flush;
 }
 void main(string[] args) {
     File file;
     foo(file);
 }
 Is stack-trace support broken, or do I have to do something to enable it?
While you aren't asking why, 'file' is initialized to null and is not a stack allocated class as it would be in C++. The first step to debugging is to use the -gc flag instead of -g. The unreleased version of GDB has the patch for D mangling, otherwise you need the symbols to mimic C. Then you can run your program with GDB. As you should be familiar with. You can get the stack trace form GDB if you enable core dumps: $ ulimit -c 5000 && dmd -gc test.d && ./test && gdb ./test core
Jul 13 2010
parent reply Graham St Jack <Graham.StJack internode.on.net> writes:
On Tue, 13 Jul 2010 15:29:35 -0400, Jesse Phillips wrote:

 Graham St Jack Wrote:
 
 Specifically:
 
 Stack Trace:
 ------------
 
 I can't get the D2 stack-trace to work properly. All I get is something
 like this, which isn't helpful:
 
 Segmentation fault
 
 The code I used to generate this was: import std.stdio;
 import std.file;
 void foo(File file) {
     file.flush;
 }
 void main(string[] args) {
     File file;
     foo(file);
 }
 Is stack-trace support broken, or do I have to do something to enable
 it?
While you aren't asking why, 'file' is initialized to null and is not a stack allocated class as it would be in C++.
Yes - it was a contrived example designed to deliberately segfault.
 
 The first step to debugging is to use the -gc flag instead of -g. The
 unreleased version of GDB has the patch for D mangling, otherwise you
 need the symbols to mimic C. Then you can run your program with GDB.
Thanks - I have been doing that, but like I said, it is hard work mentally demangling the names.
 
 As you should be familiar with. You can get the stack trace form GDB if
 you enable core dumps: $ ulimit -c 5000 && dmd -gc test.d && ./test &&
 gdb ./test core
I didn't know about that, thanks. It looks like a more convenient way of debugging segfaults than running the program in gdb directly. I will give it a go.
Jul 13 2010
parent Leandro Lucarella <luca llucax.com.ar> writes:
Graham St Jack, el 13 de julio a las 21:47 me escribiste:
 As you should be familiar with. You can get the stack trace form GDB if
 you enable core dumps: $ ulimit -c 5000 && dmd -gc test.d && ./test &&
 gdb ./test core
I didn't know about that, thanks. It looks like a more convenient way of debugging segfaults than running the program in gdb directly. I will give it a go.
``ulimit -c 5000`` will not generate a core dump if the dump is bigger than 5000 bytes (or kb?), if you want no limits, use ``ulimit -c unlimited`` instead. -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- Y tuve amores, que fue uno sólo El que me dejó de a pie y me enseñó todo...
Jul 13 2010
prev sibling parent Leandro Lucarella <luca llucax.com.ar> writes:
Graham St Jack, el 13 de julio a las 06:26 me escribiste:
 GDB:
 ----
 
 What is the status of D support in GDB? The last post I saw was back in 
 April. It is currently hard work to debug with gdb when it doesn't 
 understand D name mangling.
The patch was accepted a while ago and the next GDB version will heve them (you can try CVS's GDB if you need it now). I'm not sure if it support D2 new stuff though.
 Profiling:
 ----------
 
 How do I profile a D2 program? When I try -profile in dmd, the resultant 
 executable crashes with a segfault when I try to run it. When I try 
 sysprof, I get a handful of mangled names, and no call graph.
Same here, I use valgrind's callgrind + kcachegrind, is slooooow, but very nice. -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- No debemos temer a la muerte, porque es la mejor recompensa de la vida. -- Ren & Stimpy
Jul 13 2010