DumpStack used to dump stack info to file all: true means dumping all running goroutine stack, else only dumping the one that calls the func logname: prefix of the file's name, which is like logname.20060102-150405 msg: description message
(all bool, logname string, desc string)
| 37 | // logname: prefix of the file's name, which is like logname.20060102-150405 |
| 38 | // msg: description message |
| 39 | func DumpStack(all bool, logname string, desc string) { |
| 40 | buf := make([]byte, 1024) |
| 41 | for { |
| 42 | //the buf is no more than 64M, because Stack dumps no more than 64M |
| 43 | n := runtime.Stack(buf, all) |
| 44 | if n < len(buf) { |
| 45 | buf = buf[:n] //trim unreadable characters |
| 46 | break |
| 47 | } |
| 48 | buf = make([]byte, 2*len(buf)) |
| 49 | } |
| 50 | write2File(logname, desc, buf) |
| 51 | } |
| 52 | |
| 53 | // SigNotifyStack register os signals to be notified when to dumpstack |
| 54 | // For example, SigNotifyStack(SIGUSR1, true, "stackinfo"), can dump all goroutine stack |