Skip to content

Commit e3650ee

Browse files
authored
Merge pull request #160 from parca-dev/fix-str
Fix string termination
2 parents 9a64189 + 0d7cfdd commit e3650ee

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

parcagpu/parcagpu.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go.opentelemetry.io/ebpf-profiler/host"
1616
"go.opentelemetry.io/ebpf-profiler/libpf"
1717
"go.opentelemetry.io/ebpf-profiler/support"
18+
"go.opentelemetry.io/ebpf-profiler/util"
1819
)
1920

2021
type mapKey struct {
@@ -93,7 +94,7 @@ func prepTrace(tr *host.Trace, ev *kernelTimingEvent) {
9394
}
9495
if len(ev.kernelName) > 0 {
9596
// TODO: is there a better way to pass this through?
96-
tr.CustomLabels["_temp_cuda_kernel"] = string(ev.kernelName[:])
97+
tr.CustomLabels["_temp_cuda_kernel"] = util.GoString(ev.kernelName[:])
9798
// ConvertTrace will add a pseudo-frame for the kernel.
9899
tr.Frames = append([]host.Frame{{
99100
Type: libpf.CUDAKernelFrame,

tracer/tracer.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package tracer // import "go.opentelemetry.io/ebpf-profiler/tracer"
66

77
import (
88
"bufio"
9-
"bytes"
109
"context"
1110
"errors"
1211
"fmt"
@@ -178,15 +177,6 @@ type progLoaderHelper struct {
178177
noTailCallTarget bool
179178
}
180179

181-
// Convert a C-string to Go string.
182-
func goString(cstr []byte) string {
183-
index := bytes.IndexByte(cstr, byte(0))
184-
if index < 0 {
185-
index = len(cstr)
186-
}
187-
return strings.Clone(unsafe.String(unsafe.SliceData(cstr), index))
188-
}
189-
190180
// schedProcessFreeHookName returns the name of the tracepoint hook to use.
191181
// This function requires that only one of (schedProcessFreeV1, schedProcessFreeV2)
192182
// be present in progNames.
@@ -916,7 +906,7 @@ func (t *Tracer) loadBpfTrace(raw []byte, cpu int) *host.Trace {
916906
pid := libpf.PID(ptr.Pid)
917907
procMeta := t.processManager.MetaForPID(pid)
918908
trace := &host.Trace{
919-
Comm: goString(ptr.Comm[:]),
909+
Comm: util.GoString(ptr.Comm[:]),
920910
ExecutablePath: procMeta.Executable,
921911
ContainerID: procMeta.ContainerID,
922912
ProcessName: procMeta.Name,
@@ -947,8 +937,8 @@ func (t *Tracer) loadBpfTrace(raw []byte, cpu int) *host.Trace {
947937
trace.CustomLabels = make(map[string]string, clLen)
948938
for i := 0; i < clLen; i++ {
949939
lbl := ptr.Custom_labels.Labels[i]
950-
key := goString(lbl.Key[:])
951-
val := goString(lbl.Val[:])
940+
key := util.GoString(lbl.Key[:])
941+
val := util.GoString(lbl.Val[:])
952942
trace.CustomLabels[key] = val
953943
}
954944
}

util/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"bytes"
88
"fmt"
99
"math/bits"
10+
"strings"
1011
"sync"
1112
"sync/atomic"
1213
"unicode"
1314
"unicode/utf8"
15+
"unsafe"
1416

1517
"github.com/cilium/ebpf"
1618
"github.com/cilium/ebpf/asm"
@@ -181,3 +183,12 @@ func ProgArrayReferences(perfTailCallMapFD int, insns asm.Instructions) []int {
181183
}
182184
return insNos
183185
}
186+
187+
// Convert a C-string to Go string.
188+
func GoString(cstr []byte) string {
189+
index := bytes.IndexByte(cstr, byte(0))
190+
if index < 0 {
191+
index = len(cstr)
192+
}
193+
return strings.Clone(unsafe.String(unsafe.SliceData(cstr), index))
194+
}

0 commit comments

Comments
 (0)