Skip to content

Commit

Permalink
tls: avoid reading the entire .text section to reduce memory alloca…
Browse files Browse the repository at this point in the history
…tions (#175)
  • Loading branch information
apetruhin committed Mar 3, 2025
1 parent 5306dcd commit aa5f2cd
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ebpftracer/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"debug/elf"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -183,12 +184,7 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) ([]link.Link, bool) {
log("no text section", nil)
return nil, isGolangApp
}
textSectionData, err := textSection.Data()
if err != nil {
log("failed to read text section", err)
return nil, isGolangApp
}
textSectionLen := uint64(len(textSectionData) - 1)
textReader := textSection.Open()

exe, err := link.OpenExecutable(path)
if err != nil {
Expand Down Expand Up @@ -233,11 +229,17 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) ([]link.Link, bool) {
}
links = append(links, l)
sStart := s.Value - textSection.Addr
sEnd := sStart + s.Size
if sEnd > textSectionLen {
continue
_, err = textReader.Seek(int64(sStart), io.SeekStart)
if err != nil {
log("failed to seek", err)
return nil, isGolangApp
}
sBytes := make([]byte, s.Size)
_, err = textReader.Read(sBytes)
if err != nil {
log("failed to read", err)
return nil, isGolangApp
}
sBytes := textSectionData[sStart:sEnd]
returnOffsets := getReturnOffsets(ef.Machine, sBytes)
if len(returnOffsets) == 0 {
log("failed to attach read_exit uprobe", fmt.Errorf("no return offsets found"))
Expand Down

0 comments on commit aa5f2cd

Please sign in to comment.