Skip to content

Commit c9f4fb0

Browse files
committed
runtime: resolve #52093
1 parent 8ab131f commit c9f4fb0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/runtime/debug.go

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import (
99
"unsafe"
1010
)
1111

12+
// TracebackAncestors sets the limits the number of ancestor goroutines to report.
13+
// It extends tracebacks with the stacks at which goroutines were created.
14+
// This also extends the information returned by runtime.Stack. Ancestor's goroutine
15+
// IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
16+
// ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.
17+
func TracebackAncestors(n int32) {
18+
debug.tracebackancestors = n
19+
}
20+
1221
// GOMAXPROCS sets the maximum number of CPUs that can be executing
1322
// simultaneously and returns the previous setting. It defaults to
1423
// the value of [runtime.NumCPU]. If n < 1, it does not change the current setting.

src/runtime/stack_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,37 @@ func TestTracebackAncestors(t *testing.T) {
863863
}
864864
}
865865

866+
func TestSetTracebackAncestors(t *testing.T) {
867+
wait := make(chan struct{})
868+
869+
go func() {
870+
<-wait
871+
}()
872+
873+
TracebackAncestors(3)
874+
t.Cleanup(func() {
875+
TracebackAncestors(0)
876+
})
877+
878+
// GetStack of current runtime
879+
getStack := func() string {
880+
for i := 1024; ; i *= 2 {
881+
buf := make([]byte, i)
882+
if n := Stack(buf, true); n < i {
883+
return string(buf[:n-1])
884+
}
885+
}
886+
}
887+
888+
output := getStack()
889+
890+
if !strings.Contains(output, "originating from goroutine") {
891+
t.Errorf("output does not contain ancestor trace:\n%s", output)
892+
}
893+
894+
close(wait)
895+
}
896+
866897
// Test that defer closure is correctly scanned when the stack is scanned.
867898
func TestDeferLiveness(t *testing.T) {
868899
output := runTestProg(t, "testprog", "DeferLiveness", "GODEBUG=clobberfree=1")

0 commit comments

Comments
 (0)