From 4b85e901019f13e4bd6ffefe950bbf66f01d2245 Mon Sep 17 00:00:00 2001 From: Alex Lyashko Date: Tue, 24 Oct 2023 14:11:08 -0400 Subject: [PATCH] add stack-legacy command to dump call traces in legacy format Unfortunately legacy format is more helpful in debug as it dumps goroutine labels. 'gops stack' doesn't dump them. Golang issue: https://github.com/golang/go/issues/63712 --- agent/agent.go | 2 ++ internal/cmd/shared.go | 9 +++++++++ signal/signal.go | 3 +++ 3 files changed, 14 insertions(+) diff --git a/agent/agent.go b/agent/agent.go index 07a3e80..7e5b63b 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -208,6 +208,8 @@ func formatBytes(val uint64) string { func handle(conn io.ReadWriter, msg []byte) error { switch msg[0] { + case signal.StackTraceLegacy: + return pprof.Lookup("goroutine").WriteTo(conn, 1) case signal.StackTrace: return pprof.Lookup("goroutine").WriteTo(conn, 2) case signal.GC: diff --git a/internal/cmd/shared.go b/internal/cmd/shared.go index fc11e6b..1b1b19c 100644 --- a/internal/cmd/shared.go +++ b/internal/cmd/shared.go @@ -35,6 +35,11 @@ func AgentCommands() []*cobra.Command { short: "Prints the stack trace.", fn: stackTrace, }, + { + name: "stack-legacy", + short: "Prints the stack trace in legacy mode but with labels.", + fn: stackTraceLegacy, + }, { name: "gc", short: "Runs the garbage collector and blocks until successful.", @@ -147,6 +152,10 @@ func stackTrace(addr net.TCPAddr, _ []string) error { return cmdWithPrint(addr, signal.StackTrace) } +func stackTraceLegacy(addr net.TCPAddr, _ []string) error { + return cmdWithPrint(addr, signal.StackTraceLegacy) +} + func gc(addr net.TCPAddr, _ []string) error { _, err := cmd(addr, signal.GC) return err diff --git a/signal/signal.go b/signal/signal.go index c70764a..b414b1d 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -35,4 +35,7 @@ const ( // SetGCPercent sets the garbage collection target percentage. SetGCPercent = byte(0x10) + + // StackTraceLegacy represents a command to print stack trace in a legacy format (but it includes labels). + StackTraceLegacy = byte(0x11) )