Obtaining stacktrace

97 views
Skip to first unread message

Dimitri Rakitine

unread,
Nov 13, 2009, 11:49:11 PM11/13/09
to golang-nuts
From the documentation I thought that runtime.Caller(frame) will
return false when it is not possible to obtain information - instead
it causes program to crash past call stack top :

package main

import (
"fmt";
"runtime";
"strings"
)

func main() {
foo()
}

func foo() {
bar()
}

func bar() {
for i := 0; ; i++ {
_, file, line, ok := runtime.Caller(i);
if ok {
path := strings.Split(file, "/", 0);
fmt.Printf("%s : %d\n", path[len(path)-1], line);
} else {
break;
}
}
}

results in

stacktrace.go : 19
stacktrace.go : 14
stacktrace.go : 10
asm.s : 81
proc.c : 135
Bus error

Also, Caller() doc says 'The argument is the number of stack frames to
ascend, with 1 identifying the the caller of Caller', while in fact,
it looks like 0 is the caller of Caller.

So, how do I obtain current thread stacktrace ?

TIA,
Dimitri

Russ Cox

unread,
Nov 14, 2009, 9:43:44 AM11/14/09
to Dimitri Rakitine, golang-nuts
Your code is fine; this is just a bug.

http://code.google.com/p/go/issues/detail?id=176

Russ
Reply all
Reply to author
Forward
0 new messages