MCPcopy
hub / github.com/TarsCloud/TarsGo / SigNotifyStack

Function SigNotifyStack

tars/util/debug/debugtool.go:59–78  ·  view source on GitHub ↗

SigNotifyStack register os signals to be notified when to dumpstack For example, SigNotifyStack(SIGUSR1, true, "stackinfo"), can dump all goroutine stack when received SIGUSR1 signal by "kill -USR1 pid" sig: self defined os signals, like SIGUSR1 SIGUSR2 in linux and darwin but not supoorted in windo

(sig os.Signal, all bool, logname string)

Source from the content-addressed store, hash-verified

57// all: true means dumping all running goroutine stack, else only dumping the one that calls the func
58// logname: prefix of the file's name, which is like logname.20060102-150405
59func SigNotifyStack(sig os.Signal, all bool, logname string) {
60 buf := make([]byte, 1024)
61 c := make(chan os.Signal, 1)
62 signal.Notify(c, sig)
63
64 go func() {
65 for range c {
66 for {
67 //the buf is no more than 64M, because Stack dumps no more than 64M
68 n := runtime.Stack(buf, all)
69 if n < len(buf) {
70 buf = buf[:n] //trim unreadable characters
71 break
72 }
73 buf = make([]byte, 2*len(buf))
74 }
75 write2File(logname, "", buf)
76 }
77 }()
78}

Callers

nothing calls this directly

Calls 2

write2FileFunction · 0.85
NotifyMethod · 0.65

Tested by

no test coverage detected