Created
August 16, 2019 08:39
-
-
Save yougg/c7d23a7b130c92bb59a2f8c01d005ca2 to your computer and use it in GitHub Desktop.
Dump goroutine stack by signal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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