1
0
mirror of https://github.com/DataDog/go-profiler-notes.git synced 2025-07-12 23:50:13 +02:00

fix pclntab example

This commit is contained in:
Felix Geisendörfer
2022-08-19 22:07:20 +02:00
parent 027b8dabc8
commit ea39719244
3 changed files with 56 additions and 48 deletions

View File

@ -1,23 +1,37 @@
//+build darwin //go:build darwin
// +build darwin
package main package main
import ( import (
"debug/gosym"
"debug/macho" "debug/macho"
"errors"
"fmt"
"os" "os"
) )
func gopclntab() ([]byte, error) { // from https://github.com/lizrice/debugger-from-scratch/blob/master/symbols.go
file, err := macho.Open(os.Args[0]) func goSymTable() (*gosym.Table, error) {
exe, err := macho.Open(os.Args[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("elf.Open: %w", err) return nil, nil
} }
for _, s := range file.Sections { defer exe.Close()
if s.Name == "__gopclntab" {
return s.Data() addr := exe.Section("__text").Addr
}
lineTableData, err := exe.Section("__gopclntab").Data()
if err != nil {
return nil, nil
} }
return nil, errors.New("could not find .gopclntab") lineTable := gosym.NewLineTable(lineTableData, addr)
if err != nil {
return nil, nil
}
symTableData, err := exe.Section("__gosymtab").Data()
if err != nil {
return nil, nil
}
return gosym.NewTable(symTableData, lineTable)
} }

View File

@ -1,4 +1,5 @@
//+build linux //go:build linux
// +build linux
package main package main
@ -9,15 +10,29 @@ import (
"os" "os"
) )
func gopclntab() ([]byte, error) { // from https://github.com/lizrice/debugger-from-scratch/blob/master/symbols.go
file, err := elf.Open(os.Args[0]) func goSymTable() (*gosym.Table, error) {
exe, err := elf.Open(os.Args[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("elf.Open: %w", err) return nil, nil
} }
for _, s := range file.Sections { defer exe.Close()
if s.Name == ".gopclntab" {
return s.Data() addr := exe.Section(".text").Addr
}
lineTableData, err := exe.Section(".gopclntab").Data()
if err != nil {
return nil, nil
} }
return nil, errors.New("could not find .gopclntab") lineTable := gosym.NewLineTable(lineTableData, addr)
if err != nil {
return nil, nil
}
symTableData, err := exe.Section(".gosymtab").Data()
if err != nil {
return nil, nil
}
return gosym.NewTable(symTableData, lineTable)
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"debug/gosym"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@ -15,22 +14,15 @@ func main() {
} }
func run() error { func run() error {
data, err := gopclntab() symTable, err := goSymTable()
if err != nil { if err != nil {
return fmt.Errorf("gopclntab: %w", err) return err
} }
return debugSymtab(data) for _, pc := range callers() {
} file, line, fn := symTable.PCToLine(uint64(pc))
fmt.Printf("%x: %s() %s:%d\n", pc, fn.Name, file, line)
type StackTrace struct { }
Frames []StackFrame return nil
}
type StackFrame struct {
PC int
Function string
File string
Line int
} }
func callers() []uintptr { func callers() []uintptr {
@ -39,16 +31,3 @@ func callers() []uintptr {
pcs = pcs[0:n] pcs = pcs[0:n]
return pcs return pcs
} }
func debugSymtab(gopclntab []byte) error {
table, err := gosym.NewTable(nil, gosym.NewLineTable(gopclntab, 0))
if err != nil {
return fmt.Errorf("gosym.NewTable: %w", err)
}
for _, pc := range callers() {
file, line, fn := table.PCToLine(uint64(pc))
fmt.Printf("%x: %s() %s:%d\n", pc, fn.Name, file, line)
}
return nil
}