mirror of
https://github.com/DataDog/go-profiler-notes.git
synced 2026-06-22 17:36:52 +08:00
wip
This commit is contained in:
parent
4e4a55a286
commit
1f4c6f0691
4 changed files with 103 additions and 0 deletions
23
examples/pclnttab/darwin.go
Normal file
23
examples/pclnttab/darwin.go
Normal 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
3
examples/pclnttab/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module github.com/felixge/go-profiler-notes/examples/pclnttab
|
||||||
|
|
||||||
|
go 1.16
|
||||||
23
examples/pclnttab/linux.go
Normal file
23
examples/pclnttab/linux.go
Normal 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
54
examples/pclnttab/main.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue