1
0
mirror of https://github.com/DataDog/go-profiler-notes.git synced 2025-07-15 23:54:16 +02:00
This commit is contained in:
Felix Geisendörfer
2021-03-30 14:51:41 +02:00
parent 4e4a55a286
commit 1f4c6f0691
4 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,23 @@
//+build darwin
package main
import (
"debug/macho"
"errors"
"fmt"
"os"
)
func gopclntab() ([]byte, error) {
file, err := macho.Open(os.Args[0])
if err != nil {
return nil, fmt.Errorf("elf.Open: %w", err)
}
for _, s := range file.Sections {
if s.Name == "__gopclntab" {
return s.Data()
}
}
return nil, errors.New("could not find .gopclntab")
}

3
examples/pclnttab/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/felixge/go-profiler-notes/examples/pclnttab
go 1.16

View File

@ -0,0 +1,23 @@
//+build linux
package main
import (
"debug/elf"
"errors"
"fmt"
"os"
)
func gopclntab() ([]byte, error) {
file, err := elf.Open(os.Args[0])
if err != nil {
return nil, fmt.Errorf("elf.Open: %w", err)
}
for _, s := range file.Sections {
if s.Name == ".gopclntab" {
return s.Data()
}
}
return nil, errors.New("could not find .gopclntab")
}

54
examples/pclnttab/main.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"debug/gosym"
"fmt"
"os"
"runtime"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
data, err := gopclntab()
if err != nil {
return fmt.Errorf("gopclntab: %w", err)
}
return debugSymtab(data)
}
type StackTrace struct {
Frames []StackFrame
}
type StackFrame struct {
PC int
Function string
File string
Line int
}
func callers() []uintptr {
pcs := make([]uintptr, 32)
n := runtime.Callers(2, pcs)
pcs = pcs[0:n]
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("%s() %s:%d\n", fn.Name, file, line)
}
return nil
}