debug/gosym

Guided tour · Runtime & Debug · pkg.go.dev →

Parse the Go symbol and line tables embedded in Go-built binaries. Used by profilers and crash dumpers.

Usage

Resolve PC to file:line

elfFile, _ := elf.Open("a.out")
pclntab, _ := elfFile.Section(".gopclntab").Data()
text, _ := elfFile.Section(".text").Data()
_ = text
line := gosym.NewLineTable(pclntab, elfFile.Section(".text").Addr)
tab, _ := gosym.NewTable(nil, line)
file, lineNum, fn := tab.PCToLine(pc)
fmt.Println(file, lineNum, fn.Name)