strange stack trace when panic

167 views
Skip to first unread message

winlin

unread,
Jun 7, 2017, 2:37:26 AM6/7/17
to golang-nuts
Hi, I'm confused by the stack trace when panic, and I have searched both in history topics and google.
If I use panic, the stack is fine, code is https://play.golang.org/p/Yb7fYW9ro3 , output is:
panic: Panic from user

goroutine 1 [running]:
main.causedPanic()
	/tmp/sandbox078566511/main.go:11 +0x60
main.run()
	/tmp/sandbox078566511/main.go:7 +0x20
main.main()
	/tmp/sandbox078566511/main.go:4 +0x20

But if there is a runtime panic, the stack is completely unreadable, code is https://play.golang.org/p/MHKPG5uFFn , output is:
panic: runtime error: invalid memory address or nil pointer dereference

goroutine 1 [running]:
main.main()
	/tmp/sandbox277759147/main.go:4 +0x4

If I add a defer to the last function, the stack comes back, code is https://play.golang.org/p/V7qDdyt_4Z , output is:
panic: runtime error: invalid memory address or nil pointer dereference

goroutine 1 [running]:
main.causedPanic()
	/tmp/sandbox416089181/main.go:13 +0x48
main.run()
	/tmp/sandbox416089181/main.go:7 +0x20
main.main()
	/tmp/sandbox416089181/main.go:4 +0x20

Why it's diferent when there is a empty defer? It confused me.

Dave Cheney

unread,
Jun 7, 2017, 4:25:35 AM6/7/17
to golang-nuts
Try building your program with -gcflags="-l" to disable inlining. If that restores the stacktrace, then it's inlining. The good news is this should be fixed with Go 1.9

winlin

unread,
Jun 7, 2017, 4:52:08 AM6/7/17
to golang-nuts
When I run with -gcflags="-l", it works~

winlin$ go run -gcflags="-l" t.go 

panic: runtime error: invalid memory address or nil pointer dereference

[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x104bf82]


goroutine 1 [running]:

main.causedPanic()

/Users/winlin/git/t.go:13 +0x2

main.run()

/Users/winlin/git/t.go:7 +0x20

main.main()

/Users/winlin/git/t.go:4 +0x20

exit status 2 

It seems it's caused by inling.
Thanks a lot!

binl...@gmail.com

unread,
Jun 7, 2017, 8:46:26 AM6/7/17
to golang-nuts
awsome

在 2017年6月7日星期三 UTC+8下午4:25:35,Dave Cheney写道:
Reply all
Reply to author
Forward
0 new messages