The following
func a() { b() }
func b() { c() }
func c() { d() }
func d() {
pc := make([]uintptr, 10)
n := runtime.Callers(1, pc)
if n == 0 {
return
}
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
fmt.Println(frame.Function)
if !more {
break
}
}
}
func main() {
a()
// The reason for this sleep will become apparent in an incoming example
// where we spawn a goroutine and do not explicitely wait for it to finish
time.Sleep(time.Second)
}
outputs
main.d
main.c
main.b
main.a
main.main
runtime.main
runtime.goexit
as I expected. I am facing a situation where function c
is called in a separate goroutine
func b() { go c() }
In such cas it outputs
main.d
main.c
runtime.goexit
, while I wished it to outpout the same as above.
Is there a solution to this problem? Maybe something to capture a call stack and pass it to function d
? Note that performance will matter and I wish to avoid processing the stack beforehand as in my real life example, d
might or might not need to trigger the call to runtime.Callers.