Skip to content
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

Custom labels 2 #4

Merged
merged 8 commits into from
Aug 7, 2024
Merged
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
1 change: 1 addition & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ type Trace struct {
PID util.PID
APMTraceID libpf.APMTraceID
APMTransactionID libpf.APMTransactionID
CustomLabels map[string]string
}
64 changes: 64 additions & 0 deletions interpreter/golang/golang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package golang

import (
"errors"
"fmt"
"unsafe"

log "github.com/sirupsen/logrus"

"github.com/elastic/otel-profiling-agent/interpreter"
"github.com/elastic/otel-profiling-agent/libpf"
"github.com/elastic/otel-profiling-agent/remotememory"
"github.com/elastic/otel-profiling-agent/util"
)

// #include <stdlib.h>
// #include "../../support/ebpf/types.h"
import "C"

type data struct {
goVersion string
offsets C.GoCustomLabelsOffsets
interpreter.InstanceStubs
}

func (d data) Attach(ebpf interpreter.EbpfHandler, pid util.PID,
_ libpf.Address, _ remotememory.RemoteMemory) (interpreter.Instance, error) {

if err := ebpf.UpdateProcData(libpf.Go, pid, unsafe.Pointer(&d.offsets)); err != nil {
return nil, err
}

return &d, nil
}

func (d data) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
return ebpf.DeleteProcData(libpf.Go, pid)
}

func Loader(_ interpreter.EbpfHandler, info *interpreter.LoaderInfo) (interpreter.Data, error) {
file, err := info.GetELF()
if err != nil {
return nil, err
}
goVersion, err := ReadGoVersion(file)
if errors.Is(err, ErrNoGoVersion) {
log.Debugf("file %s is not a Go binary", info.FileName())
return nil, nil
}
if err != nil {
return nil, err
}
log.Debugf("file %s detected as go version %s", info.FileName(), goVersion)

offsets, ok := allOffsets[goVersion]
if !ok {
return nil, fmt.Errorf("no offsets found for go version %s", goVersion)
}

return data{
goVersion: goVersion,
offsets: offsets,
}, nil
}
131 changes: 131 additions & 0 deletions interpreter/golang/readelf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package golang

import (
"bytes"
"debug/elf"
"encoding/binary"
"errors"
"io"

"github.com/elastic/otel-profiling-agent/libpf/pfelf"
)

func getVersionSection(f *pfelf.File) io.ReaderAt {
if sec := f.Section(".go.buildinfo"); sec != nil {
return sec
}
for _, seg := range f.Progs {
if seg.Type == elf.PT_LOAD && seg.Flags&(elf.PF_X|elf.PF_W) == elf.PF_W {
return &seg
}
}
return nil
}

var ErrNoGoVersion = errors.New("go version not found")
var buildInfoMagic = []byte("\xff Go buildinf:")

// readBuildInfo reads build info, failing if it's not
// in the first 1 MiB of the given stream.
func readBuildInfo(s io.ReaderAt) ([]byte, error) {
const (
buildInfoAlign = 16
buildInfoSize = 32
chunk = 1 << 20
)
buf := make([]byte, chunk)
n, err := s.ReadAt(buf, 0)
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
data := buf[:n]
for {
i := bytes.Index(data, buildInfoMagic)
if i < 0 || len(data)-i < buildInfoSize {
break
}
if i%buildInfoAlign == 0 && len(data)-i >= buildInfoSize {
data = data[i:]
return data, nil
}
data = data[(i+buildInfoAlign-1)&^(buildInfoAlign-1):]
}
return nil, ErrNoGoVersion
}

func decodeString(data []byte) string {
u, n := binary.Uvarint(data)
if n <= 0 || u > uint64(len(data)-n) {
return ""
}
return string(data[n : uint64(n)+u])
}

// readString returns the string at address addr in the executable x.
func readString(x *pfelf.File, ptrSize int,
readPtr func([]byte) uint64, addr uint64) (string, error) {
buf := make([]byte, 2*ptrSize)
n, err := x.ReadAt(buf, int64(addr))
if err != nil {
return "", err
}
if n != len(buf) {
return "", io.EOF
}
dataAddr := readPtr(buf)
dataLen := readPtr(buf[ptrSize:])
const maxSize = 64 // implausible that a Go version string is bigger than this
if dataLen > maxSize {
return "", ErrNoGoVersion
}
buf = make([]byte, dataLen)
n, err = x.ReadAt(buf, int64(dataAddr))
if err != nil {
return "", err
}
if n != len(buf) {
return "", io.EOF
}
return string(buf), nil
}

// ReadGoVersion returns the version of the Go toolchain that build the binary
// (for example, "go1.19.2").
//
// It is guaranteed not to consume more than 1 MiB of memory.
func ReadGoVersion(f *pfelf.File) (string, error) {
vs := getVersionSection(f)
if vs == nil {
return "", ErrNoGoVersion
}
data, err := readBuildInfo(vs)
if err != nil {
return "", err
}
ptrSize := int(data[14])
var vers string
if data[15]&2 != 0 {
vers = decodeString(data[32:])
} else {
bigEndian := data[15] != 0
var bo binary.ByteOrder
if bigEndian {
bo = binary.BigEndian
} else {
bo = binary.LittleEndian
}
var readPtr func([]byte) uint64
if ptrSize == 4 {
readPtr = func(b []byte) uint64 { return uint64(bo.Uint32(b)) }
} else if ptrSize == 8 {
readPtr = bo.Uint64
} else {
return "", ErrNoGoVersion
}
vers, err = readString(f, ptrSize, readPtr, readPtr(data[16:]))
if err != nil {
return "", err
}
}
return vers, nil
}
Loading
Loading