Hacker News new | past | comments | ask | show | jobs | submit login

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();
  }



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()


That code won't compile because Greetings is undefined. Hence, you may be running some other program which seg faults.


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)


Replace: Greetings g = null; with: auto g = new Greetings();


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


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).


does this mean there is not a way to get stack trace on OSX?


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


I don't know about an automatic one, but running the debugger would still work there too. Or at least it should.


Try valgrind


Did you compile with debug info (-g I think)? It needs to read the DWARF info to get the line numbers.


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


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();
  }


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]


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.


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?


1) Where is Greetings defined ?

2) Spanish ?





Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: