Home >Backend Development >Golang >How Can I Dump Go Process Stacks Without Code Modifications or Termination?
Dumping Go Process Stacks Non-Invasively
A running Go process can be analyzed without modifying its code or terminating it by leveraging its built-in stack dumping capability.
Method:
When sending a SIGQUIT signal to the process (e.g., using ctrl \ on Windows or kill -SIGQUIT process_pid on Linux), the defined signal handler will intercept it and invoke the stack trace capture routine. The captured traces will be printed to the standard output, providing a detailed snapshot of the process's goroutine stacks.
Code Example:
<code class="go">import ( "fmt" "os" "os/signal" "runtime" "syscall" ) func main() { sigChan := make(chan os.Signal) go func() { stacktrace := make([]byte, 8192) for _ = range sigChan { length := runtime.Stack(stacktrace, true) fmt.Println(string(stacktrace[:length])) } }() signal.Notify(sigChan, syscall.SIGQUIT) // ... Process Logic }</code>
The above is the detailed content of How Can I Dump Go Process Stacks Without Code Modifications or Termination?. For more information, please follow other related articles on the PHP Chinese website!