Skip to content

Commit 95cb5c9

Browse files
authored
Merge pull request #104 from parca-dev/cgo-drop
cgo drop
2 parents 545afad + ef7836c commit 95cb5c9

File tree

26 files changed

+907
-526
lines changed

26 files changed

+907
-526
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $(error Unsupported architecture: $(TARGET_ARCH))
2525
endif
2626

2727
export TARGET_ARCH
28-
export CGO_ENABLED = 1
28+
export CGO_ENABLED = 0
2929
export GOARCH = $(TARGET_ARCH)
3030
export CC = $(ARCH_PREFIX)-linux-gnu-gcc
3131
export OBJCOPY = $(ARCH_PREFIX)-linux-gnu-objcopy
@@ -103,7 +103,8 @@ vanity-import-fix: $(PORTO)
103103
@porto --include-internal -w .
104104

105105
test: generate ebpf test-deps
106-
go test $(GO_FLAGS) -tags $(GO_TAGS) ./...
106+
# tools/coredump tests build ebpf C-code using CGO to test it against coredumps
107+
CGO_ENABLED=1 go test $(GO_FLAGS) -tags $(GO_TAGS) ./...
107108

108109
# This target isn't called from CI, it doesn't work for cross compile (ie TARGET_ARCH=arm64 on
109110
# amd64) and the CI kernel tests run them already. Useful for local testing.
@@ -130,7 +131,7 @@ support/golbls_1_24.test: ./interpreter/golabels/test/main.go
130131
CGO_ENABLED=0 GOTOOLCHAIN=go1.24.1 go build -tags $(GO_TAGS),nocgo -o $@ $<
131132

132133
support/golbls_cgo.test: ./interpreter/golabels/test/main-cgo.go
133-
GOTOOLCHAIN=go1.24.1 go build -ldflags '-extldflags "-static"' -tags $(GO_TAGS),usecgo -o $@ $<
134+
CGO_ENABLED=1 GOTOOLCHAIN=go1.24.1 go build -ldflags '-extldflags "-static"' -tags $(GO_TAGS),usecgo -o $@ $<
134135

135136
integration-test-binaries: generate ebpf rust-components support/golbls_1_23.test support/golbls_1_24.test support/golbls_cgo.test
136137
$(foreach test_name, $(TEST_INTEGRATION_BINARY_DIRS), \

interpreter/customlabels/customlabels.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package customlabels // import "go.opentelemetry.io/ebpf-profiler/interpreter/customlabels"
22

3-
// #include <stdlib.h>
4-
// #include "../../support/ebpf/types.h"
5-
import "C"
63
import (
74
"errors"
85
"fmt"
@@ -13,6 +10,7 @@ import (
1310
"go.opentelemetry.io/ebpf-profiler/libpf"
1411
"go.opentelemetry.io/ebpf-profiler/libpf/pfelf"
1512
"go.opentelemetry.io/ebpf-profiler/remotememory"
13+
"go.opentelemetry.io/ebpf-profiler/support"
1614
)
1715

1816
const (
@@ -127,10 +125,10 @@ func (d data) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID,
127125
currentHmTlsOffset = rm.Uint64(bias + d.currentHmTlsAddr + 8)
128126
}
129127

130-
procInfo := C.NativeCustomLabelsProcInfo{
131-
current_set_tls_offset: C.u64(currentSetTlsOffset),
132-
has_current_hm: C.bool(d.hasCurrentHm),
133-
current_hm_tls_offset: C.u64(currentHmTlsOffset),
128+
procInfo := support.NativeCustomLabelsProcInfo{
129+
Current_set_tls_offset: currentSetTlsOffset,
130+
Has_current_hm: d.hasCurrentHm,
131+
Current_hm_tls_offset: currentHmTlsOffset,
134132
}
135133
if err := ebpf.UpdateProcData(libpf.CustomLabels, pid, unsafe.Pointer(&procInfo)); err != nil {
136134
return nil, err

interpreter/customlabels/integrationtests/node_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ func TestIntegration(t *testing.T) {
176176

177177
// Really, there should be zero frames in the
178178
// `marked` workload that aren't under labels,
179-
// but accept a 1% slop because the unwinder
179+
// but accept a 5% slop because the unwinder
180180
// isn't perfect (e.g. it might interrupt the
181181
// process when the Node environment is in an
182182
// undefined state)
183-
require.Less(t, 100*unlabeledWorkloadFrames, totalWorkloadFrames)
183+
require.Less(t, 20*unlabeledWorkloadFrames, totalWorkloadFrames)
184184
})
185185
}
186186
}

interpreter/golabels/golabels.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import (
1313
"go.opentelemetry.io/ebpf-profiler/interpreter"
1414
"go.opentelemetry.io/ebpf-profiler/libpf"
1515
"go.opentelemetry.io/ebpf-profiler/remotememory"
16+
"go.opentelemetry.io/ebpf-profiler/support"
1617
)
1718

18-
// #include "../../support/ebpf/types.h"
19-
import "C"
20-
2119
type data struct {
2220
goVersion string
23-
offsets C.GoLabelsOffsets
21+
offsets support.GoLabelsOffsets
2422
interpreter.InstanceStubs
2523
}
2624

@@ -64,7 +62,7 @@ func Loader(_ interpreter.EbpfHandler, info *interpreter.LoaderInfo) (interprete
6462
if err != nil {
6563
return nil, fmt.Errorf("failed to extract TLS offset: %w", err)
6664
}
67-
offsets.tls_offset = C.s32(tlsOffset)
65+
offsets.Tls_offset = tlsOffset
6866

6967
return &data{
7068
goVersion: goVersion,

interpreter/golabels/runtime_data.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,49 @@
33

44
package golabels // import "go.opentelemetry.io/ebpf-profiler/interpreter/golabels"
55

6-
// #include "../../support/ebpf/types.h"
7-
import "C"
86
import (
97
"go/version"
8+
9+
"go.opentelemetry.io/ebpf-profiler/support"
1010
)
1111

1212
// Offsets come from DWARF debug information, use tools/gooffsets to extract them.
1313
// However since DWARF information can be stripped we record them here.
1414
// TODO: Should we look for DWARF information to support new versions
1515
// automatically when available?
16-
func getOffsets(vers string) C.GoLabelsOffsets {
17-
offsets := C.GoLabelsOffsets{
16+
func getOffsets(vers string) support.GoLabelsOffsets {
17+
offsets := support.GoLabelsOffsets{
1818
// https://github.com/golang/go/blob/80e2e474b8d9124d03b744f/src/runtime/runtime2.go#L410
19-
m_offset: 48,
19+
M_offset: 48,
2020
// https://github.com/golang/go/blob/80e2e474b8d9124d03b744f/src/runtime/runtime2.go#L541
21-
curg: 192,
21+
Curg: 192,
2222
// https://github.com/golang/go/blob/80e2e474b8d9124d03b744f/src/runtime/runtime2.go#L483
23-
labels: 0,
23+
Labels: 0,
2424
// https://github.com/golang/go/blob/6885bad7dd86880be6929c0/src/runtime/map.go#L112
25-
hmap_count: 0,
25+
Hmap_count: 0,
2626
// https://github.com/golang/go/blob/6885bad7dd86880be6929c0/src/runtime/map.go#L114
27-
hmap_log2_bucket_count: 0,
27+
Hmap_log2_bucket_count: 0,
2828
// https://github.com/golang/go/blob/6885bad7dd86880be6929c0/src/runtime/map.go#L118
29-
hmap_buckets: 0,
29+
Hmap_buckets: 0,
3030
}
3131

3232
// Version enforcement takes place in the Loader function.
3333
if version.Compare(vers, "go1.24") >= 0 {
34-
offsets.labels = 352
34+
offsets.Labels = 352
3535
return offsets
3636
}
3737

3838
// These are the same for all versions but we have to leave them zero for 1.24+ detection.
39-
offsets.hmap_log2_bucket_count = 9
40-
offsets.hmap_buckets = 16
39+
offsets.Hmap_log2_bucket_count = 9
40+
offsets.Hmap_buckets = 16
4141
if version.Compare(vers, "go1.23") >= 0 {
42-
offsets.labels = 352
42+
offsets.Labels = 352
4343
} else if version.Compare(vers, "go1.21") >= 0 {
44-
offsets.labels = 344
44+
offsets.Labels = 344
4545
} else if version.Compare(vers, "go1.17") >= 0 {
46-
offsets.labels = 360
46+
offsets.Labels = 360
4747
} else {
48-
offsets.labels = 344
48+
offsets.Labels = 344
4949
}
5050
return offsets
5151
}

interpreter/hotspot/instance.go

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ import (
3131
"go.opentelemetry.io/ebpf-profiler/util"
3232
)
3333

34-
// #include "../../support/ebpf/types.h"
35-
// #include "../../support/ebpf/frametypes.h"
36-
import "C"
37-
3834
// heapRange contains info for an individual heap.
3935
type heapRange struct {
4036
codeStart, codeEnd libpf.Address
@@ -237,9 +233,9 @@ func (d *hotspotInstance) getPoolSymbol(addr libpf.Address, ndx uint16) libpf.St
237233

238234
// getStubNameID read the stub name from the code blob at given address and generates a ID.
239235
func (d *hotspotInstance) getStubNameID(symbolReporter reporter.SymbolReporter, ripOrBci uint32,
240-
addr libpf.Address, _ uint32) (libpf.AddressOrLineno, error) {
236+
addr libpf.Address, _ uint32) libpf.AddressOrLineno {
241237
if value, ok := d.addrToStubNameID.Get(addr); ok {
242-
return value, nil
238+
return value
243239
}
244240
vms := &d.d.Get().vmStructs
245241
constStubNameAddr := d.rm.Ptr(addr + libpf.Address(vms.CodeBlob.Name))
@@ -263,7 +259,7 @@ func (d *hotspotInstance) getStubNameID(symbolReporter reporter.SymbolReporter,
263259
})
264260
d.addrToStubNameID.Add(addr, stubID)
265261

266-
return stubID, nil
262+
return stubID
267263
}
268264

269265
// getMethod reads and returns the interesting data from "class Method" at given address
@@ -745,31 +741,31 @@ func (d *hotspotInstance) populateMainMappings(vmd *hotspotVMData,
745741

746742
// Set up the main eBPF info structure.
747743
vms := &vmd.vmStructs
748-
procInfo := C.HotspotProcInfo{
749-
nmethod_deopt_offset: C.u16(vms.Nmethod.DeoptimizeOffset),
750-
nmethod_compileid: C.u16(vms.Nmethod.CompileID),
751-
nmethod_orig_pc_offset: C.u16(vms.Nmethod.OrigPcOffset),
752-
codeblob_name: C.u8(vms.CodeBlob.Name),
753-
codeblob_codestart: C.u8(vms.CodeBlob.CodeBegin),
754-
codeblob_codeend: C.u8(vms.CodeBlob.CodeEnd),
755-
codeblob_framecomplete: C.u8(vms.CodeBlob.FrameCompleteOffset),
756-
codeblob_framesize: C.u8(vms.CodeBlob.FrameSize),
757-
cmethod_size: C.u8(vms.ConstMethod.Sizeof),
758-
heapblock_size: C.u8(vms.HeapBlock.Sizeof),
759-
method_constmethod: C.u8(vms.Method.ConstMethod),
760-
jvm_version: C.u8(vmd.version >> 24),
761-
segment_shift: C.u8(heap.segmentShift),
762-
nmethod_uses_offsets: C.u8(vmd.nmethodUsesOffsets),
744+
procInfo := support.HotspotProcInfo{
745+
Nmethod_deopt_offset: uint16(vms.Nmethod.DeoptimizeOffset),
746+
Nmethod_compileid: uint16(vms.Nmethod.CompileID),
747+
Nmethod_orig_pc_offset: uint16(vms.Nmethod.OrigPcOffset),
748+
Codeblob_name: uint8(vms.CodeBlob.Name),
749+
Codeblob_codestart: uint8(vms.CodeBlob.CodeBegin),
750+
Codeblob_codeend: uint8(vms.CodeBlob.CodeEnd),
751+
Codeblob_framecomplete: uint8(vms.CodeBlob.FrameCompleteOffset),
752+
Codeblob_framesize: uint8(vms.CodeBlob.FrameSize),
753+
Cmethod_size: uint8(vms.ConstMethod.Sizeof),
754+
Heapblock_size: uint8(vms.HeapBlock.Sizeof),
755+
Method_constmethod: uint8(vms.Method.ConstMethod),
756+
Jvm_version: uint8(vmd.version >> 24),
757+
Segment_shift: uint8(heap.segmentShift),
758+
Nmethod_uses_offsets: vmd.nmethodUsesOffsets,
763759
}
764760

765761
if vms.CodeCache.LowBound == 0 {
766762
// JDK-8 has only one heap, use its bounds
767-
procInfo.codecache_start = C.u64(heap.ranges[0].codeStart)
768-
procInfo.codecache_end = C.u64(heap.ranges[0].codeEnd)
763+
procInfo.Codecache_start = uint64(heap.ranges[0].codeStart)
764+
procInfo.Codecache_end = uint64(heap.ranges[0].codeEnd)
769765
} else {
770766
// JDK9+ the VM tracks it separately
771-
procInfo.codecache_start = C.u64(d.rm.Ptr(vms.CodeCache.LowBound + d.bias))
772-
procInfo.codecache_end = C.u64(d.rm.Ptr(vms.CodeCache.HighBound + d.bias))
767+
procInfo.Codecache_start = uint64(d.rm.Ptr(vms.CodeCache.LowBound + d.bias))
768+
procInfo.Codecache_end = uint64(d.rm.Ptr(vms.CodeCache.HighBound + d.bias))
773769
}
774770

775771
if err = ebpf.UpdateProcData(libpf.HotSpot, pid, unsafe.Pointer(&procInfo)); err != nil {
@@ -875,22 +871,19 @@ func (d *hotspotInstance) Symbolize(symbolReporter reporter.SymbolReporter,
875871
sfCounter := successfailurecounter.New(&d.successCount, &d.failCount)
876872
defer sfCounter.DefaultToFailure()
877873

878-
switch subtype {
879-
case C.FRAME_HOTSPOT_STUB, C.FRAME_HOTSPOT_VTABLE:
874+
switch uint8(subtype) {
875+
case support.FrameHotspotStub, support.FrameHotspotVtable:
880876
// These are stub frames that may or may not be interesting
881877
// to be seen in the trace.
882-
stubID, err1 := d.getStubNameID(symbolReporter, ripOrBci, ptr, ptrCheck)
883-
if err1 != nil {
884-
return err
885-
}
878+
stubID := d.getStubNameID(symbolReporter, ripOrBci, ptr, ptrCheck)
886879
trace.AppendFrame(libpf.HotSpotFrame, hotspotStubsFileID, stubID)
887-
case C.FRAME_HOTSPOT_INTERPRETER:
880+
case support.FrameHotspotInterpreter:
888881
method, err1 := d.getMethod(ptr, ptrCheck)
889882
if err1 != nil {
890883
return err1
891884
}
892885
method.symbolize(symbolReporter, ripOrBci, d, trace)
893-
case C.FRAME_HOTSPOT_NATIVE:
886+
case support.FrameHotspotNative:
894887
jitinfo, err1 := d.getJITInfo(ptr, ptrCheck)
895888
if err1 != nil {
896889
return err1

interpreter/luajit/luajit.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ import (
3434
"go.opentelemetry.io/ebpf-profiler/util"
3535
)
3636

37-
// #include "../../support/ebpf/types.h"
38-
// #include "../../support/ebpf/luajit.h"
39-
import "C"
40-
41-
const LuaJITFFIFunc = C.LUAJIT_FFI_FUNC
42-
4337
// Records all the "global" pointers we've seen.
4438
type vmMap map[libpf.Address]struct{}
4539

@@ -92,10 +86,10 @@ var (
9286

9387
func (d *luajitData) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID, _ libpf.Address,
9488
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
95-
cdata := C.LuaJITProcInfo{
96-
g2dispatch: C.u16(d.g2Dispatch),
97-
cur_L_offset: C.u16(d.currentLOffset),
98-
cframe_size_jit: C.u16(cframeSizeJIT),
89+
cdata := support.LuaJITProcInfo{
90+
G2dispatch: d.g2Dispatch,
91+
Cur_L_offset: d.currentLOffset,
92+
Cframe_size_jit: uint16(cframeSizeJIT),
9993
}
10094
if err := ebpf.UpdateProcData(libpf.LuaJIT, pid, unsafe.Pointer(&cdata)); err != nil {
10195
return nil, err
@@ -210,7 +204,7 @@ func (l *luajitInstance) addJITRegion(ebpf interpreter.EbpfHandler, pid libpf.PI
210204
logf("lj: add JIT region pid(%v) %#x:%#x", pid, start, end)
211205
for _, prefix := range prefixes {
212206
// TODO: fix these: WARN[0267] Failed to lookup file ID 0x2a00000000
213-
fileID := uint64(C.LUAJIT_JIT_FILE_ID) << 32
207+
fileID := support.LJFileId << 32
214208
if err := ebpf.UpdatePidInterpreterMapping(pid, prefix, support.ProgUnwindLuaJIT,
215209
host.FileID(fileID), 0); err != nil {
216210
return err
@@ -231,7 +225,7 @@ func (l *luajitInstance) addTrace(ebpf interpreter.EbpfHandler, pid libpf.PID, t
231225
}
232226
logf("lj: add trace mapping for pid(%v) %x:%x", pid, start, end)
233227
for _, prefix := range prefixes {
234-
fileID := uint64(C.LUAJIT_JIT_FILE_ID)<<32 | spadjust
228+
fileID := support.LJFileId<<32 | spadjust
235229
if err := ebpf.UpdatePidInterpreterMapping(pid, prefix, support.ProgUnwindLuaJIT,
236230
host.FileID(fileID), g); err != nil {
237231
return nil, err
@@ -383,7 +377,7 @@ func (l *luajitInstance) symbolizeFrame(symbolReporter reporter.SymbolReporter,
383377
frameID libpf.FrameID) error {
384378
var line uint32
385379
var fileName string
386-
if ptAddr != C.LUAJIT_FFI_FUNC {
380+
if ptAddr != support.LJFFIFunc {
387381
pt, err := l.getGCproto(ptAddr)
388382
if err != nil {
389383
return err
@@ -438,7 +432,7 @@ func (l *luajitInstance) Symbolize(symbolReporter reporter.SymbolReporter, frame
438432
}
439433

440434
var funcName string
441-
if frame.File == C.LUAJIT_FFI_FUNC {
435+
if frame.File == support.LJFFIFunc {
442436
switch frame.Lineno & 7 {
443437
case 0:
444438
funcName = "lua-frame"

interpreter/nodev8/node_offsets_generated.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)