Skip to content

Commit

Permalink
link/kprobe: Sanitize kprobe symbol for tracefs
Browse files Browse the repository at this point in the history
Since we allow `.` in symbol names, we need to sanitize
those symbols to please tracefs.

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
  • Loading branch information
chenhengqi committed Mar 24, 2022
1 parent 34be694 commit 30211c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions link/kprobe.go
Expand Up @@ -321,7 +321,7 @@ func tracefsProbe(typ probeType, args probeArgs) (*perfEvent, error) {
// check if an event with the same group and name already exists.
// Kernels 4.x and earlier don't return os.ErrExist on writing a duplicate
// entry, so we need to rely on reads for detecting uniqueness.
_, err = getTraceEventID(group, args.symbol)
_, err = getTraceEventID(group, sanitizedSymbol(args.symbol))
if err == nil {
return nil, fmt.Errorf("trace event already exists: %s/%s", group, args.symbol)
}
Expand All @@ -335,7 +335,7 @@ func tracefsProbe(typ probeType, args probeArgs) (*perfEvent, error) {
}

// Get the newly-created trace event's id.
tid, err := getTraceEventID(group, args.symbol)
tid, err := getTraceEventID(group, sanitizedSymbol(args.symbol))
if err != nil {
return nil, fmt.Errorf("getting trace event id: %w", err)
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func createTraceFSProbeEvent(typ probeType, args probeArgs) error {
// subsampling or rate limiting logic can be more accurately implemented in
// the eBPF program itself.
// See Documentation/kprobes.txt for more details.
pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(args.ret), args.group, args.symbol, args.symbol)
pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(args.ret), args.group, sanitizedSymbol(args.symbol), args.symbol)
case uprobeType:
// The uprobe_events syntax is as follows:
// p[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] : Set a probe
Expand Down
12 changes: 6 additions & 6 deletions link/uprobe.go
Expand Up @@ -16,9 +16,9 @@ import (
var (
uprobeEventsPath = filepath.Join(tracefsPath, "uprobe_events")

// rgxUprobeSymbol is used to strip invalid characters from the uprobe symbol
// 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.
rgxUprobeSymbol = regexp.MustCompile("[^a-zA-Z0-9]+")
rgxEventSymbol = regexp.MustCompile("[^a-zA-Z0-9]+")

uprobeRetprobeBit = struct {
once sync.Once
Expand Down Expand Up @@ -296,7 +296,7 @@ func (ex *Executable) uprobe(symbol string, prog *ebpf.Program, opts *UprobeOpti
}

// Use tracefs if uprobe PMU is missing.
args.symbol = uprobeSanitizedSymbol(symbol)
args.symbol = sanitizedSymbol(symbol)
tp, err = tracefsUprobe(args)
if err != nil {
return nil, fmt.Errorf("creating trace event '%s:%s' in tracefs: %w", ex.path, symbol, err)
Expand All @@ -315,9 +315,9 @@ func tracefsUprobe(args probeArgs) (*perfEvent, error) {
return tracefsProbe(uprobeType, args)
}

// uprobeSanitizedSymbol replaces every invalid characted for the tracefs api with an underscore.
func uprobeSanitizedSymbol(symbol string) string {
return rgxUprobeSymbol.ReplaceAllString(symbol, "_")
// sanitizedSymbol replaces every invalid characted for the tracefs api with an underscore.
func sanitizedSymbol(symbol string) string {
return rgxEventSymbol.ReplaceAllString(symbol, "_")
}

// uprobeToken creates the PATH:OFFSET(REF_CTR_OFFSET) token for the tracefs api.
Expand Down

0 comments on commit 30211c6

Please sign in to comment.