Skip to content

Instantly share code, notes, and snippets.

@yougg
Created August 16, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yougg/c7d23a7b130c92bb59a2f8c01d005ca2 to your computer and use it in GitHub Desktop.
Save yougg/c7d23a7b130c92bb59a2f8c01d005ca2 to your computer and use it in GitHub Desktop.
Dump goroutine stack by signal
package dump
import (
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
)
func init() {
c := make(chan os.Signal, 1)
// handle signal: SIGUSR1 10 0xa, see 'kill -l'
// kill -10 <pid>
// kill -SIGUSR1 <pid>
// kill -s SIGUSR1 <pid>
signal.Notify(c, syscall.Signal(0xa))
go func() {
for range c {
Stacks()
}
}()
}
// Dump full stacks in current process
func Stacks() {
buf := make([]byte, 1<<24)
buf = buf[:runtime.Stack(buf, true)]
fmt.Printf("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment