I am a complete beginner in golang, in fact I am debugging someone else's program to find out the cause of an unexpected exit.
I want to know how can I set a breakpoint in gdb
at the "exit" routine called just before the program shuts down?
I have so far tried
gdb <program name>
run
<...wait for program to quit>
break 'runtime.goexit'
run
<...wait for program to break>
But it does not break, instead it just exits.
If you have access to source code you can add a defer
to handle exit from arbitrary point like:
https://play.golang.org/p/uliAc3j7f-
package main
import (
"fmt"
)
func main() {
defer func() {
fmt.Println("Place breakpoint here")
if recovered := recover(); recovered != nil {
fmt.Println("Handled panic:", recovered)
}
}()
fmt.Println("Hello, playground")
panic("Something went wrong")
}
Also Go is case sensitive so try break 'runtime.Goexit'
- see https://golang.org/pkg/runtime/#Goexit
But it does not break, instead it just exits.
You can use catch syscall exit
(or catch sycall exit_group
if you are on Linux).
That is guaranteed to stop the program if it really exists (as opposed to being terminated by a signal).