具有分层输出的堆栈跟踪

I am debugging a go program with a lot of goroutines which seem to deadlock.

I would like to have a stack trace when it freezes. But the output is long and doesn't fit my terminal buffer. So I want to output it to a file. However, if I output stderr to a file, I can't (easily) detect when it freezes.

I expected tee would be good for this:

go test ./foo -v |& tee log.txt

However, when I hit Ctrl + Alt + \, no stack trace output is shown either in terminal or in file. Somehow, it seems it doesn't survive the trip through tee.

Is there a better way to do it?

As per comment from Mark Plotnick , trapping the SIGQUIT signal before the tee works (at least for me):

go test ./foo -v |& { trap "" 3; tee log.txt; }