Skip to content

Commit 8c3e98d

Browse files
authored
Merge pull request #181 from coroot/tls_go_memory_allocations_optimization
tls: avoid reading the entire `.text` section to reduce memory allocations (#175)
2 parents 5306dcd + aa5f2cd commit 8c3e98d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

ebpftracer/tls.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"debug/elf"
88
"errors"
99
"fmt"
10+
"io"
1011
"os"
1112
"regexp"
1213
"strings"
@@ -183,12 +184,7 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) ([]link.Link, bool) {
183184
log("no text section", nil)
184185
return nil, isGolangApp
185186
}
186-
textSectionData, err := textSection.Data()
187-
if err != nil {
188-
log("failed to read text section", err)
189-
return nil, isGolangApp
190-
}
191-
textSectionLen := uint64(len(textSectionData) - 1)
187+
textReader := textSection.Open()
192188

193189
exe, err := link.OpenExecutable(path)
194190
if err != nil {
@@ -233,11 +229,17 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) ([]link.Link, bool) {
233229
}
234230
links = append(links, l)
235231
sStart := s.Value - textSection.Addr
236-
sEnd := sStart + s.Size
237-
if sEnd > textSectionLen {
238-
continue
232+
_, err = textReader.Seek(int64(sStart), io.SeekStart)
233+
if err != nil {
234+
log("failed to seek", err)
235+
return nil, isGolangApp
236+
}
237+
sBytes := make([]byte, s.Size)
238+
_, err = textReader.Read(sBytes)
239+
if err != nil {
240+
log("failed to read", err)
241+
return nil, isGolangApp
239242
}
240-
sBytes := textSectionData[sStart:sEnd]
241243
returnOffsets := getReturnOffsets(ef.Machine, sBytes)
242244
if len(returnOffsets) == 0 {
243245
log("failed to attach read_exit uprobe", fmt.Errorf("no return offsets found"))

0 commit comments

Comments
 (0)