News for hackers. Stuff that matters.
Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @09:47AM (#10506884)
Not sure what I do wrong every time I try D again, I wanted to see the new backtraces worked but I still get:

  /bin/bash: line 1: 82501 Segmentation fault: 11  ./main
while I try to call a method on a null variable, which is not that friendly to newcomers.

Sample code:

  import std.stdio;
  
  void main()
  {
  	Greetings g = null;
  	g.hola();
  }
Re:D 2.069.0 released, compiler automatically ported from C+... by dom96 on Wednesday November 04, 2015 @12:15PM (#10507989)
I had the same problem with D. As an example of what I feel stack traces should look like, compare it to the stack traces that Nim gives.

  ~/temp_code » ./greetings
  Traceback (most recent call last)
  greetings.nim(9)        greetings
  greetings.nim(6)        hola
  SIGSEGV: Illegal storage access. (Attempt to read from nil?)
For the code:

  type
    Greetings = ref object
      name: string

  proc hola(g: Greetings) =
    echo g.name

  var g: Greetings
  g.hola()
Re:D 2.069.0 released, compiler automatically ported from C+... by WalterBright on Wednesday November 04, 2015 @11:11AM (#10507521)
That code won't compile because Greetings is undefined. Hence, you may be running some other program which seg faults.
Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @11:53AM (#10507821)
ohh, yeah, I omitted the definition of the class because I thought it'd be obvious, here it is:

  $ cat main.d
  import std.stdio;

  class Greetings {
	void hello() {
		writeln("hello");
	}
  }

  void main()
  {
	Greetings g = null;
	g.hello();
  }
  $ dmd -g main.d && ./main
  Segmentation fault: 11
  $ uname -a
  Darwin Johan-Ride-Mac.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
  $ dmd --version
  DMD64 D Compiler v2.069.0
  Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
(EDIT: added osx and dmd versions)
Re:D 2.069.0 released, compiler automatically ported from C+... by WalterBright on Wednesday November 04, 2015 @12:02PM (#10507909)
Replace: Greetings g = null; with: auto g = new Greetings();
Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @12:18PM (#10508015)
I'm intentionally making it null to see how the new backtraces work.

I know it's because I'm trying to call a method on "null", but If I'm working on a huge code base, how do I debug this kind of issues if I don't have stack trace with line and error number of the error?

I'd like to get at least the stack trace and the error number with the line of the source code that caused the problem, like in golang:

  $ cat main.go
  package main
  
  type Greetings struct {
  }
  
  func (g Greetings) hello() {

  }

  func main() {
	g := &Greetings{} // heap instance
	g = nil           // intentionally shooting myself in the foot like I did in D
	g.hello()
  }
  $ go run main.go
  panic: runtime error: invalid memory address or nil pointer dereference
  [signal 0xb code=0x1 addr=0x0 pc=0x2025]

  goroutine 1 [running]:
  main.main()
	/Users/ride/Documents/main.go:13 +0x15

  goroutine 2 [runnable]:
  runtime.forcegchelper()
	/opt/boxen/homebrew/Cellar/go/1.4.2/libexec/src/runtime/proc.go:90
  runtime.goexit()
	/opt/boxen/homebrew/Cellar/go/1.4.2/libexec/src/runtime/asm_amd64.s:2232 +0x1
  exit status 2
Re:D 2.069.0 released, compiler automatically ported from C+... by destructionator on Wednesday November 04, 2015 @01:48PM (#10508603)
null deference in D doesn't throw exceptions by default on Unix so you won't see the backtrace there.

If you are working in a big codebase and have this come up, you can enable core dumps and run it in a debugger to get far more information than just a line number (and line numbers are there too at least if compiled in debug mode).

Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @02:31PM (#10508978)
does this mean there is not a way to get stack trace on OSX?
Re:D 2.069.0 released, compiler automatically ported from C+... by EvenThisAcronym on Wednesday November 04, 2015 @03:20PM (#10509362)
I have not tried it myself but there seem to be ways to get a stack trace on Linux.

http://vibed.org/docs#handling-segmentation-faults

Re:D 2.069.0 released, compiler automatically ported from C+... by destructionator on Wednesday November 04, 2015 @06:24PM (#10510543)
I don't know about an automatic one, but running the debugger would still work there too. Or at least it should.
Re:D 2.069.0 released, compiler automatically ported from C+... by asdfaoeu on Wednesday November 04, 2015 @04:07PM (#10509773)
Try valgrind
Re:D 2.069.0 released, compiler automatically ported from C+... by trishume on Wednesday November 04, 2015 @10:08AM (#10507048)
Did you compile with debug info (-g I think)? It needs to read the DWARF info to get the line numbers.
Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @10:11AM (#10507067)
I think so:

  $ dmd -g main.d && ./main
  Segmentation fault: 11
is there anything else I can try?

I'm OSX El Capitan 10.11.1

Re:D 2.069.0 released, compiler automatically ported from C+... by lultimouomo on Wednesday November 04, 2015 @10:42AM (#10507296)
On Unix segfaults by default don't generate stack traces. This makes it work on linux, don't know about OSX (put it in your main module):

  import etc.linux.memoryerror;
  
  shared static this() {
   	static if (is(typeof(registerMemoryErrorHandler)))
   		registerMemoryErrorHandler();
  }
Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @11:47AM (#10507773)
The handler works in a real ubuntu VM but not in docker (the image is probably missing glibc https://github.com/D-Programming-Language/druntime/blob/mast... so that explains "static if" in your snippet)

to be honest, it's better than nothing but still kinda difficult to work with:

  etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)
  ----------------
  ??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) [0x439045]
  ??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x438f92]
  ??:? _Dmain [0x435c17]
  ??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x4376da]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x437630]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x437696]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x437630]
  ??:? _d_run_main [0x43758d]
  ??:? main [0x436075]
  ??:? __libc_start_main [0xb0fa1ec4]
Re:D 2.069.0 released, compiler automatically ported from C+... by destructionator on Wednesday November 04, 2015 @01:49PM (#10508613)
Are you using the new version and compiling with -g there? Those addresses should be translateable into line numbers.

You can also do it manually btw with `addr2line -e your_executable 0x435c17` and the sort.

Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @02:28PM (#10508955)
Thanks, I tried again and it seems to be working:

  $ dmd -g main.d && ./main
  etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)
  ----------------
  ??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) [0x423321]
  ??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x42326e]
  main.d:12 _Dmain [0x4204e5]
  ??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x421b8a]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x421ae0]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x421b46]
  ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x421ae0]
  ??:? _d_run_main [0x421a3d]
  ??:? main [0x420597]
  ??:? __libc_start_main [0xdb825ec4]
this is linux, I use mac as development machine, what can I do for mac? is there a error handler for mac too?
Re:D 2.069.0 released, compiler automatically ported from C+... by Zardoz84 on Wednesday November 04, 2015 @11:33AM (#10507684)
1) Where is Greetings defined ?

2) Spanish ?

Re:D 2.069.0 released, compiler automatically ported from C+... by thepumpkin1979 on Wednesday November 04, 2015 @11:53AM (#10507828)