return getg()
}
+func Goid() uint64 {
+ return getg().goid
+}
+
func GIsWaitingOnMutex(gp *G) bool {
return readgstatus(gp) == _Gwaiting && gp.waitreason.isMutexWait()
}
newg.sched.pc = abi.FuncPCABI0(goexit) + sys.PCQuantum // +PCQuantum so that previous instruction is in same function
newg.sched.g = guintptr(unsafe.Pointer(newg))
gostartcallfn(&newg.sched, fn)
+ newg.parentGoid = callergp.goid
newg.gopc = callerpc
newg.ancestors = saveAncestors(callergp)
newg.startpc = fn.fn
n := runtime.NumGoroutine()
buf = buf[:runtime.Stack(buf, true)]
- nstk := strings.Count(string(buf), "goroutine ")
+ // To avoid double-counting "goroutine" in "goroutine $m [running]:"
+ // and "created by $func in goroutine $n", remove the latter
+ output := strings.ReplaceAll(string(buf), "in goroutine", "")
+ nstk := strings.Count(output, "goroutine ")
if n == nstk {
break
}
sigcode0 uintptr
sigcode1 uintptr
sigpc uintptr
+ parentGoid uint64 // goid of goroutine that created this goroutine
gopc uintptr // pc of go statement that created this goroutine
ancestors *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors)
startpc uintptr // pc of goroutine function
_32bit uintptr // size on 32bit platforms
_64bit uintptr // size on 64bit platforms
}{
- {runtime.G{}, 240, 392}, // g, but exported for testing
+ {runtime.G{}, 248, 400}, // g, but exported for testing
{runtime.Sudog{}, 56, 88}, // sudog, but exported for testing
}
pc := gp.gopc
f := findfunc(pc)
if f.valid() && showframe(f, gp, false, funcID_normal, funcID_normal) && gp.goid != 1 {
- printcreatedby1(f, pc)
+ printcreatedby1(f, pc, gp.parentGoid)
}
}
-func printcreatedby1(f funcInfo, pc uintptr) {
- print("created by ", funcname(f), "\n")
+func printcreatedby1(f funcInfo, pc uintptr, goid uint64) {
+ print("created by ", funcname(f))
+ if goid != 0 {
+ print(" in goroutine ", goid)
+ }
+ print("\n")
tracepc := pc // back up to CALL instruction for funcline.
if pc > f.entry() {
tracepc -= sys.PCQuantum
// Show what created goroutine, except main goroutine (goid 1).
f := findfunc(ancestor.gopc)
if f.valid() && showfuncinfo(f, false, funcID_normal, funcID_normal) && ancestor.goid != 1 {
- printcreatedby1(f, ancestor.gopc)
+ // In ancestor mode, we'll already print the goroutine ancestor.
+ // Pass 0 for the goid parameter so we don't print it again.
+ printcreatedby1(f, ancestor.gopc, 0)
}
}
import (
"bytes"
+ "fmt"
"internal/abi"
"internal/testenv"
"runtime"
+ "strings"
+ "sync"
"testing"
)
func poisonStack() [20]int {
return [20]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
}
+
+func TestTracebackParentChildGoroutines(t *testing.T) {
+ parent := fmt.Sprintf("goroutine %d", runtime.Goid())
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ buf := make([]byte, 1<<10)
+ // We collect the stack only for this goroutine (by passing
+ // false to runtime.Stack). We expect to see the current
+ // goroutine ID, and the parent goroutine ID in a message like
+ // "created by ... in goroutine N".
+ stack := string(buf[:runtime.Stack(buf, false)])
+ child := fmt.Sprintf("goroutine %d", runtime.Goid())
+ if !strings.Contains(stack, parent) || !strings.Contains(stack, child) {
+ t.Errorf("did not see parent (%s) and child (%s) IDs in stack, got %s", parent, child, stack)
+ }
+ }()
+ wg.Wait()
+}