Repository: go
Issue: x/debug/cmd/viewcore: NPE in readDebugInfo
Labels: NeedsInvestigation

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.15.5 linux/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tobias/.cache/go-build"
GOENV="/home/tobias/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/tobias/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tobias/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/tobias/go/src/github.com/cockroachdb/cockroach/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build548952027=/tmp/go-build -gno-record-gcc-switches"

</pre></details>

### What did you do?

Use `viewcore` as of https://github.com/golang/debug/commit/6a9ae255c05850beaca2ff230809261f6a4f189b on [these files](https://drive.google.com/drive/folders/1o64R2w0i8RsYftDd1tmorzoSpWzDrWFO?usp=sharing)

```
$ viewcore core.1082827 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x7017e2]
goroutine 1 [running]:
golang.org/x/debug/internal/core.(*Process).readDebugInfo(0xc00020c000, 0xc00019e088, 0x0)
	/home/tobias/go/src/golang.org/x/debug/internal/core/process.go:660 +0x6a2
golang.org/x/debug/internal/core.Core(0x7ffdb23b1232, 0xc, 0x0, 0x0, 0x0, 0x0, 0x20, 0x7f90c5588e98, 0xc000190460)
	/home/tobias/go/src/golang.org/x/debug/internal/core/process.go:161 +0x1f0
main.readCore(0xc0001f7c90, 0x7686c6, 0x828840, 0xc000183080)
	/home/tobias/go/src/golang.org/x/debug/cmd/viewcore/main.go:262 +0xb2
main.runRoot(0xb6b920, 0xba5130, 0x0, 0x0)
	/home/tobias/go/src/golang.org/x/debug/cmd/viewcore/main.go:288 +0x55
github.com/spf13/cobra.(*Command).execute(0xb6b920, 0xc000190030, 0x0, 0x0, 0xb6b920, 0xc000190030)
	/home/tobias/go/src/github.com/spf13/cobra/command.go:859 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0xb6b920, 0x89a4b2, 0x1, 0xc0001f7f00)
	/home/tobias/go/src/github.com/spf13/cobra/command.go:964 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
	/home/tobias/go/src/github.com/spf13/cobra/command.go:900
main.main()
	/home/tobias/go/src/golang.org/x/debug/cmd/viewcore/main.go:244 +0x11f
```

I instrumented `readDebugInfo` and basically it was looking up the empty string in the map here and then crashing:

```
	if exe == nil {
		f := p.files[p.mainExecName]
		fmt.Println(p.mainExecName)
		fmt.Println(f)
		fmt.Println(p.files)
		if f.err != nil {
			p.dwarfErr = f.err
			return nil
		}
```

It works with this invocation:

```
viewcore core.kvserver.test --exe=/home/tobias/go/src/github.com/cockroachdb/cockroach/kvserver.test
```

(I got that string from `fmt.Println(p.files)` above.



### What did you expect to see?

I would've either expected an error asking me to set `--exe` or, better, it auto-inferring the executable name correctly.

### What did you see instead?

An opaque NPE.