Skip to content

Commit

Permalink
runtime: resolve #52093
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed May 22, 2024
1 parent 8ab131f commit c9f4fb0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/runtime/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"unsafe"
)

// TracebackAncestors sets the limits the number of ancestor goroutines to report.
// It extends tracebacks with the stacks at which goroutines were created.
// This also extends the information returned by runtime.Stack. Ancestor's goroutine
// IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
// ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.
func TracebackAncestors(n int32) {
debug.tracebackancestors = n
}

// GOMAXPROCS sets the maximum number of CPUs that can be executing
// simultaneously and returns the previous setting. It defaults to
// the value of [runtime.NumCPU]. If n < 1, it does not change the current setting.
Expand Down
31 changes: 31 additions & 0 deletions src/runtime/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,37 @@ func TestTracebackAncestors(t *testing.T) {
}
}

func TestSetTracebackAncestors(t *testing.T) {
wait := make(chan struct{})

go func() {
<-wait
}()

TracebackAncestors(3)
t.Cleanup(func() {
TracebackAncestors(0)
})

// GetStack of current runtime
getStack := func() string {
for i := 1024; ; i *= 2 {
buf := make([]byte, i)
if n := Stack(buf, true); n < i {
return string(buf[:n-1])
}
}
}

output := getStack()

if !strings.Contains(output, "originating from goroutine") {
t.Errorf("output does not contain ancestor trace:\n%s", output)
}

close(wait)
}

// Test that defer closure is correctly scanned when the stack is scanned.
func TestDeferLiveness(t *testing.T) {
output := runTestProg(t, "testprog", "DeferLiveness", "GODEBUG=clobberfree=1")
Expand Down

0 comments on commit c9f4fb0

Please sign in to comment.