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)
| 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 |
| 59 | func 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 | } |
nothing calls this directly
no test coverage detected