如何在Windows上强制转储Go程序?

I'm looking to do some kind of debugging on windows where the state of the service appears to be a deadlock. Perhaps the approach would be to do a dump. How can you do a program dump of a Go service? And then analyze that dump?

I've done this using the runtime.Stack function. Its output is equivalent to what you get from a panic(), but without halting the program. You can set up a signal handler to dump the stack to a file/stdout, or start a goroutine to dump the stack at regular intervals, if you prefer.

You could also check out the runtime/debug package.

Goroutine deadlock analysis is handled well using the trace command.

First, you need to get data to run a trace against, which you want at any point. You can get this by using the pprof package or the trace package.

https://golang.org/pkg/net/http/pprof/ https://golang.org/pkg/runtime/trace/

The pprof package will add its HTTP handlers to a web server and let you collect trace data while the app is running, while the trace package lets you write out a trace to a stream (e.g. a file), but I haven't tried that myself.

Once you've collected trace data, you can run the trace command against the data you've collected to produce a web view of the state:

https://golang.org/cmd/trace/

There's an example output here:

https://talks.golang.org/2015/dynamic-tools.slide#30