From 2fad7dbb893737a01c7ab4f382720a2b0440b736 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 28 Sep 2021 13:13:49 -0700 Subject: [PATCH] debug/gosym: add hook to disable recovers debug/gosym assumes throughout that bogus input means a malformed file. That's generally true, but not when you're changing the package. In that case, the panic usually indicates a newly introduced bug, and seeing the panic is really useful. Add a manually-enabled way to make panics panic. Change-Id: I07af6c7b982c4cf61180db29f07aa63576ba7837 Reviewed-on: https://go-review.googlesource.com/c/go/+/352949 Trust: Josh Bleecher Snyder Run-TryBot: Josh Bleecher Snyder TryBot-Result: Go Bot Reviewed-by: Cherry Mui --- src/debug/gosym/pclntab.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/debug/gosym/pclntab.go b/src/debug/gosym/pclntab.go index 555add67e30..5d65b7826ad 100644 --- a/src/debug/gosym/pclntab.go +++ b/src/debug/gosym/pclntab.go @@ -193,10 +193,12 @@ func (t *LineTable) parsePclnTab() { // Error paths through this code will default the version to 1.1. t.version = ver11 - defer func() { - // If we panic parsing, assume it's a Go 1.1 pclntab. - recover() - }() + if !disableRecover { + defer func() { + // If we panic parsing, assume it's a Go 1.1 pclntab. + recover() + }() + } // Check header: 4-byte magic, two zeros, pc quantum, pointer size. if len(t.Data) < 16 || t.Data[4] != 0 || t.Data[5] != 0 || @@ -265,9 +267,11 @@ func (t *LineTable) parsePclnTab() { // go12Funcs returns a slice of Funcs derived from the Go 1.2 pcln table. func (t *LineTable) go12Funcs() []Func { // Assume it is malformed and return nil on error. - defer func() { - recover() - }() + if !disableRecover { + defer func() { + recover() + }() + } n := len(t.functab) / int(t.ptrsize) / 2 funcs := make([]Func, n) @@ -441,7 +445,7 @@ func (t *LineTable) findFileLine(entry uint64, filetab, linetab uint32, filenum, // go12PCToLine maps program counter to line number for the Go 1.2 pcln table. func (t *LineTable) go12PCToLine(pc uint64) (line int) { defer func() { - if recover() != nil { + if !disableRecover && recover() != nil { line = -1 } }() @@ -458,7 +462,7 @@ func (t *LineTable) go12PCToLine(pc uint64) (line int) { // go12PCToFile maps program counter to file name for the Go 1.2 pcln table. func (t *LineTable) go12PCToFile(pc uint64) (file string) { defer func() { - if recover() != nil { + if !disableRecover && recover() != nil { file = "" } }() @@ -490,7 +494,7 @@ func (t *LineTable) go12PCToFile(pc uint64) (file string) { // go12LineToPC maps a (file, line) pair to a program counter for the Go 1.2/1.16 pcln table. func (t *LineTable) go12LineToPC(file string, line int) (pc uint64) { defer func() { - if recover() != nil { + if !disableRecover && recover() != nil { pc = 0 } }() @@ -552,12 +556,18 @@ func (t *LineTable) initFileMap() { // Every key maps to obj. That's not a very interesting map, but it provides // a way for callers to obtain the list of files in the program. func (t *LineTable) go12MapFiles(m map[string]*Obj, obj *Obj) { - defer func() { - recover() - }() + if !disableRecover { + defer func() { + recover() + }() + } t.initFileMap() for file := range t.fileMap { m[file] = obj } } + +// disableRecover causes this package not to swallow panics. +// This is useful when making changes. +const disableRecover = false