Skip to content

debug/gosym/v2: WIP gosym v2 implementation #74960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions src/debug/gosym/v2/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package main

import (
"debug/gosym/v2"
"flag"
"fmt"
"os"
"strconv"
)

var (
inlines = flag.Bool("inlines", false, "List inline functions")
lines = flag.Bool("lines", false, "List function lines")
)

func functions(table *gosym.Table) error {
for f := range table.Functions() {
name, err := f.Name()
if err != nil {
return err
}
entry, err := f.Entry()
if err != nil {
return err
}
end, err := f.End()
if err != nil {
return err
}
deferreturn, err := f.DeferReturn()
if err != nil {
return err
}
file, err := f.File()
if err != nil {
return err
}
startLine, err := f.StartLine()
if err != nil {
return err
}
fmt.Printf("%s@%s:%d pc=[0x%x, 0x%x) deferreturn=0x%x\n", name.Value(), file.Value(), startLine, entry, end, deferreturn)
}
return nil
}

func describe(table *gosym.Table, pc uint64, inlines, lines bool) error {
f, err := table.ResolveFunction(pc)
name, err := f.Name()
if err != nil {
return err
}
entry, err := f.Entry()
if err != nil {
return err
}
end, err := f.End()
if err != nil {
return err
}
deferreturn, err := f.DeferReturn()
if err != nil {
return err
}
file, err := f.File()
if err != nil {
return err
}
startLine, err := f.StartLine()
if err != nil {
return err
}
fmt.Printf("%s@%s:%d pc=[0x%x, 0x%x) deferreturn=0x%x\n",
name.Value(), file.Value(), startLine, entry, end, deferreturn)

if inlines {
inlines, err := f.InlineFunctions(nil)
if err != nil {
return err
}
if len(inlines) == 0 {
fmt.Println(" (no inlines)")
}
for _, inline := range inlines {
fmt.Printf(" %s@%s:%d\n",
inline.Name.Value(), inline.File.Value(), inline.StartLine)
}
}
if lines {
r, err := f.Lines(gosym.LinesResult{})
if err != nil {
return err
}
if len(r.FunctionLines) == 0 {
fmt.Println(" (no lines)")
}
for _, line := range r.FunctionLines {
fmt.Printf(" [0x%x, 0x%x) %s@%s:%d parentPC=0x%x\n",
line.PCLo, line.PCHi, line.Name.Value(), line.File.Value(), line.Line, line.ParentPC)
}
}
return nil
}

func locate(table *gosym.Table, pc uint64) error {
locs, err := table.ResolveLocations(pc, nil)
if err != nil {
return err
}
for _, loc := range locs {
fmt.Printf("%s@%s:%d\n", loc.Function.Value(), loc.File.Value(), loc.Line)
}
return nil
}

func main() {
usage := func() {
fmt.Printf(`Usage:
$ %s functions <binary> # List all function symbols in this binary
$ %s describe <binary> <pc> (--inlines)? (--lines)? # Describe a function at a specific pc
$ %s locate <binary> <pc> # Locate a pc
`, os.Args[0], os.Args[0], os.Args[0])
os.Exit(1)
}
if len(os.Args) < 3 {
fmt.Printf("missing subcommand or binary argument\n")
usage()
os.Exit(1)
}

binary := os.Args[2]
reader, err := os.Open(binary)
if err != nil {
fmt.Printf("failed to open binary: %v\n", err)
os.Exit(1)
}
defer reader.Close()
table, err := gosym.NewMagic(reader)
if err != nil {
fmt.Printf("failed to parse binary: %v\n", err)
os.Exit(1)
}

var pc uint64
if len(os.Args) > 3 {
pc, err = strconv.ParseUint(os.Args[3], 0, 64)
if err != nil {
fmt.Printf("failed to parse pc: %v\n", err)
os.Exit(1)
}
}

subcommand := os.Args[1]
switch subcommand {
case "functions":
if len(os.Args) != 3 {
fmt.Printf("functions expects 1 argument, got %d\n", len(os.Args)-2)
usage()
os.Exit(1)
}
err := functions(table)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "describe":
if len(os.Args) < 4 {
fmt.Printf("describe expects 2 arguments, got %d\n", len(os.Args)-2)
usage()
os.Exit(1)
}
flag.CommandLine.Parse(os.Args[4:])
err := describe(table, pc, *inlines, *lines)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "locate":
if len(os.Args) < 4 {
fmt.Printf("locate expects 2 arguments, got %d\n", len(os.Args)-2)
usage()
os.Exit(1)
}
err := locate(table, pc)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
default:
fmt.Printf("unknown subcommand: %s\n", subcommand)
usage()
os.Exit(1)
}
}
42 changes: 42 additions & 0 deletions src/debug/gosym/v2/elf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gosym

import (
"debug/elf"
"encoding/binary"
"fmt"
)

type elfObject struct {
*elf.File
}

func (o elfObject) Endian() binary.ByteOrder {
return o.ByteOrder
}

func (o elfObject) Sections() (headers []SectionHeader, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%w: failed to parse section headers: %v", ErrCorrupted, r)
}
}()
headers = make([]SectionHeader, 0, len(o.File.Sections))
for _, s := range o.File.Sections {
headers = append(headers, SectionHeader{
Name: s.Name,
Addr: s.Addr,
Size: s.Size,
})
}
return
}

func (o elfObject) SectionData(i int8) (data []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%w: failed to read section: %d: %v", ErrCorrupted, i, r)
}
}()
data, err = o.File.Sections[i].Data()
return
}
146 changes: 146 additions & 0 deletions src/debug/gosym/v2/funcdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package gosym

import (
"encoding/binary"
"fmt"
"math"
)

func funcdataEntryOffsetSize(version version, ptrSize uint8) uint64 {
switch version {
case ver12, ver116:
return uint64(ptrSize)
default:
return 4
}
}

func (t *Table) funcdataFieldOffset(field uint64) uint64 {
return funcdataEntryOffsetSize(t.version, t.ptrSize) + (field-1)*4
}

func (t *Table) funcdataField(funcOff uint64, field uint64) (uint32, error) {
offset := t.funcdata[0] + funcOff + t.funcdataFieldOffset(field)
if offset+4 > t.funcdata[1] {
return 0, fmt.Errorf("%w: function data out of bounds: off=%d field=%d", ErrCorrupted, offset, field)
}
return binary.LittleEndian.Uint32(t.pclntab[offset:]), nil
}

func (t *Table) funcdataNameOff(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 1)
}

func (t *Table) funcdataDeferReturn(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 3)
}

func (t *Table) funcdataPcfile(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 5)
}

func (t *Table) funcdataPcln(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 6)
}

func (t *Table) funcdataNpcdata(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 7)
}

func (t *Table) funcdataCuOffset(funcOff uint64) (uint32, error) {
return t.funcdataField(funcOff, 8)
}

func (t *Table) funcdataStartLine(funcOff uint64) (uint32, error) {
line, err := t.funcdataField(funcOff, 9)
if err != nil {
return 0, err
}
return line, nil
}

func (t *Table) funcdataFuncID(funcOff uint64) (uint8, error) {
funcIDOff := t.funcdata[0] + funcOff + t.funcdataFieldOffset(10)
if funcIDOff >= t.funcdata[1] {
return 0, fmt.Errorf("%w: function data out of bounds, off=%d", ErrCorrupted, funcIDOff)
}
return t.pclntab[funcIDOff], nil
}

func (t *Table) funcdataFileNoToOff(funcOff uint64, fno uint32) (uint32, error) {
if t.version == ver12 {
return uint32(fno * 4), nil
}
cuOff, err := t.funcdataCuOffset(funcOff)
if err != nil {
return 0, err
}
offOff := t.cutab[0] + uint64(cuOff)*4 + uint64(fno)*4
if offOff+4 > t.cutab[1] {
return 0, fmt.Errorf("%w: file offset out of bounds", ErrCorrupted)
}
off := binary.LittleEndian.Uint32(t.pclntab[offOff:])
if off == math.MaxUint32 {
// Valid for non-function entries. We skip them at higher levels,
// so here we can return an error.
return 0, fmt.Errorf("%w: no file entry", ErrCorrupted)
}
return off, nil
}

func (t *Table) funcdataFileNo(funcOff uint64) (uint32, error) {
pcfile, err := t.funcdataPcfile(funcOff)
if err != nil {
return 0, err
}
fno, err := t.firstPcValue(pcfile)
if err != nil {
return 0, err
}
return uint32(fno), nil
}

func (t *Table) funcdataFileOff(funcOff uint64) (uint32, error) {
fno, err := t.funcdataFileNo(funcOff)
if err != nil {
return 0, err
}
return t.funcdataFileNoToOff(funcOff, uint32(fno))
}

func (t *Table) funcdataInlTreeIndex(funcOff uint64) (uint32, error) {
npcdata, err := t.funcdataNpcdata(funcOff)
if err != nil {
return 0, err
}
const pcdataInlTreeIndex = 2
if pcdataInlTreeIndex >= npcdata {
return 0, fmt.Errorf("%w: inl tree index out of bounds, off=%d", ErrCorrupted, funcOff)
}
return t.funcdataField(funcOff, uint64(11+pcdataInlTreeIndex))
}

func (t *Table) funcdataInlTree(funcOff uint64) ([]byte, error) {
npcdata, err := t.funcdataNpcdata(funcOff)
if err != nil {
return nil, err
}
nfuncdataOff := t.funcdata[0] + funcOff + t.funcdataFieldOffset(11)
if nfuncdataOff >= t.funcdata[1] {
return nil, fmt.Errorf("%w: function data out of bounds, off=%d", ErrCorrupted, nfuncdataOff)
}
nfuncdata := t.pclntab[nfuncdataOff]
const funcdataInlTree = 3
if funcdataInlTree >= nfuncdata {
return nil, fmt.Errorf("%w: inl tree out of bounds, off=%d", ErrCorrupted, funcOff)
}
inlTreeOff, err := t.funcdataField(funcOff, uint64(11+npcdata+funcdataInlTree))
if err != nil {
return nil, err
}
if inlTreeOff == math.MaxUint32 {
// Valid case - this function has no inline functions
return nil, nil
}
return t.gofunc[inlTreeOff:], nil
}
Loading