package main
import (
"os/signal"
"os"
"syscall"
"fmt"
"runtime"
"time"
)
// kill -SIGUSR1 pid
func main() {
time.Sleep(time.Hour)
}
func dumpStacks() {
buf := make([]byte, 16384)
buf = buf[:runtime.Stack(buf, true)]
fmt.Printf("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
}
func setupSigusr1Trap() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
go func() {
for range c {
dumpStacks()
}
}()
}
func init() {
setupSigusr1Trap()
}
go调试小技巧,源码出自docker源码
通过命令发送指定信号即可打印出线程堆栈信息
有疑问加站长微信联系(非本文作者)