Skip to content

Commit

Permalink
runtime: record parent goroutine ID, and print it in stack traces
Browse files Browse the repository at this point in the history
Fixes #38651

Change-Id: Id46d684ee80e208c018791a06c26f304670ed159
Reviewed-on: https://go-review.googlesource.com/c/go/+/435337
Run-TryBot: Nick Ripley <nick.ripley@datadoghq.com>
Reviewed-by: Ethan Reesor <ethan.reesor@gmail.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
nsrip-dd authored and gopherbot committed Feb 21, 2023
1 parent 81eda3a commit 51225f6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/runtime/export_test.go
Expand Up @@ -540,6 +540,10 @@ func Getg() *G {
return getg()
}

func Goid() uint64 {
return getg().goid
}

func GIsWaitingOnMutex(gp *G) bool {
return readgstatus(gp) == _Gwaiting && gp.waitreason.isMutexWait()
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/proc.go
Expand Up @@ -4283,6 +4283,7 @@ func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g {
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
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/proc_test.go
Expand Up @@ -415,7 +415,10 @@ func TestNumGoroutine(t *testing.T) {
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
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/runtime2.go
Expand Up @@ -479,6 +479,7 @@ type g struct {
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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sizeof_test.go
Expand Up @@ -21,7 +21,7 @@ func TestSizeof(t *testing.T) {
_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
}

Expand Down
14 changes: 10 additions & 4 deletions src/runtime/traceback.go
Expand Up @@ -701,12 +701,16 @@ func printcreatedby(gp *g) {
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
Expand Down Expand Up @@ -806,7 +810,9 @@ func printAncestorTraceback(ancestor ancestorInfo) {
// 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)
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/runtime/traceback_test.go
Expand Up @@ -6,9 +6,12 @@ package runtime_test

import (
"bytes"
"fmt"
"internal/abi"
"internal/testenv"
"runtime"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -420,3 +423,23 @@ func testTracebackArgs11b(a, b, c, d int32) int {
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()
}

0 comments on commit 51225f6

Please sign in to comment.