5

I have the following sample code in D:

import std.stdio;

int g(int i) {
    auto l = [1, 2, 3, 4];
    return l[i];
}

void f(int i) {
    writeln(g(i));
}

void main(string[] args) {
    f(1);
    f(10);
    f(2);
}

I compiled this code with DMD (using v2.056 on OS X). When I run it, it obviously crashes:

 core.exception.RangeError@test(5): Range violation
 ----------------
 5   test    0x000b823a _d_array_bounds + 30
 6   test    0x000aa44b D4test7__arrayZ + 27
 7   test    0x000aa4ae int test.g(int) + 94
 8   test    0x000aa4c7 void test.f(int) + 11
 9   test    0x000aa422 _Dmain + 26
 10  test    0x000b87b3 extern (C) int rt.dmain2.main(int, char**).void runMain() + 23
 11  test    0x000b835d extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29
 12  test    0x000b8800 extern (C) int rt.dmain2.main(int, char**).void runAll() + 64
 13  test    0x000b835d extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29
 14  test    0x000b82f7 main + 179
 15  test    0x000aa3fd start + 53
 16  ???     0x00000001 0x0 + 1
 ----------------

Is there a way to get line numbers in the backtrace instead of locations? I can see the line number of the crash (the 5 in core.exception.RangeError@test(5)), but it is only the top of the backtrace. Can I get the line numbers inside f() and main()?

gyim
  • 8,803
  • 2
  • 19
  • 21

1 Answers1

3

Because the D run-time doesn't implement enough debug information parsing to obtaining line numbers, you will need to use a debugger. It can be expected that what you want will be implemented at some point.

For now, compile your program with the -gc compiler flag, then run the program via gdb.

BCS
  • 75,627
  • 68
  • 187
  • 294
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
  • 1
    Why would you need a debugger? Compiling with debug information should be enough. – Trass3r Nov 21 '11 at 13:42
  • 1
    Because the runtime doesn't implement a debug information parser to the extent of obtaining line numbers? – Vladimir Panteleev Nov 21 '11 at 19:45
  • Guess it's a Linux problem then. The Windows stacktrace implementation I use gets the line numbers. – Trass3r Nov 22 '11 at 12:49
  • The Windows one uses Windows' debugging DLLs which can get line numbers. – Vladimir Panteleev Nov 22 '11 at 14:48
  • Actually, gdb does not work on OSX either :( (see http://goo.gl/v6z5S). It does not stop on unhandled exceptions, and I cannot even set breakpoints (http://pastebin.com/suvd4PiB). Any ideas? I also tried atos to find line numbers for addresses, but it does not work either. – gyim Nov 23 '11 at 21:34