diff --git a/link/uprobe.go b/link/uprobe.go index 896d9e157..159fe5eca 100644 --- a/link/uprobe.go +++ b/link/uprobe.go @@ -6,7 +6,7 @@ import ( "fmt" "os" "path/filepath" - "regexp" + "strings" "sync" "github.com/cilium/ebpf" @@ -16,10 +16,6 @@ import ( var ( uprobeEventsPath = filepath.Join(tracefsPath, "uprobe_events") - // rgxEventSymbol is used to strip invalid characters from the [k,u]probe symbol - // as they are not allowed to be used as the EVENT token in tracefs. - rgxEventSymbol = regexp.MustCompile("[^a-zA-Z0-9]+") - uprobeRetprobeBit = struct { once sync.Once value uint64 @@ -316,8 +312,28 @@ func tracefsUprobe(args probeArgs) (*perfEvent, error) { } // sanitizedSymbol replaces every invalid character for the tracefs api with an underscore. -func sanitizedSymbol(symbol string) string { - return rgxEventSymbol.ReplaceAllString(symbol, "_") +// It is equivalent to calling regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString("_"). +func sanitizedSymbol(s string) string { + var b strings.Builder + b.Grow(len(s)) + var skip bool + for _, c := range []byte(s) { + switch { + case c >= 'a' && c <= 'z', + c >= 'A' && c <= 'Z', + c >= '0' && c <= '9': + skip = false + b.WriteByte(c) + + default: + if !skip { + b.WriteByte('_') + skip = true + } + } + } + + return b.String() } // uprobeToken creates the PATH:OFFSET(REF_CTR_OFFSET) token for the tracefs api. diff --git a/link/uprobe_test.go b/link/uprobe_test.go index ae37894a6..fd5334929 100644 --- a/link/uprobe_test.go +++ b/link/uprobe_test.go @@ -280,9 +280,11 @@ func TestUprobeSanitizedSymbol(t *testing.T) { expected string }{ {"readline", "readline"}, - {"main.Func", "main_Func"}, + {"main.Func123", "main_Func123"}, {"a.....a", "a_a"}, {"./;'{}[]a", "_a"}, + {"***xx**xx###", "_xx_xx_"}, + {`@P#r$i%v^3*+t)i&k++--`, "_P_r_i_v_3_t_i_k_"}, } for i, tt := range tests {