Skip to content

Commit

Permalink
libbpf-tools/trace_helpers: filter ksyms by regex
Browse files Browse the repository at this point in the history
This will serve as a basis to create a C implementation of a
functionality similar to what 'b.attach_kprobe(event_re=...)' offers in
Python.
  • Loading branch information
myhro committed Apr 15, 2024
1 parent 1f76c88 commit 82468f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions libbpf-tools/trace_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <sys/resource.h>
#include <time.h>
#include <bpf/bpf.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h>
#include <limits.h>
#include "string_helpers.h"
#include "trace_helpers.h"
#include "uprobe_helpers.h"

Expand Down Expand Up @@ -182,6 +184,42 @@ const struct ksym *ksyms__get_symbol(const struct ksyms *ksyms,
return NULL;
}

struct string_array *ksyms__get_symbols_re(const struct ksyms *ksyms, const char *pattern)
{
struct string_array *matches = NULL;
regex_t regex;
int err;

err = regcomp(&regex, pattern, REG_EXTENDED);
if (err) {
fprintf(stderr, "failed to compile regex\n");
goto err_out;
}

matches = string_array__init();
if (matches == NULL) {
goto err_out;
}

for (int i = 0; i < ksyms->syms_sz; i++) {
err = regexec(&regex, ksyms->syms[i].name, 0, NULL, 0);
if (!err) {
err = string_array__push(matches, ksyms->syms[i].name);
if (err) {
goto err_out;
}
}
}

regfree(&regex);
return matches;

err_out:
regfree(&regex);
string_array__free(matches);
return NULL;
}

struct load_range {
uint64_t start;
uint64_t end;
Expand Down
2 changes: 2 additions & 0 deletions libbpf-tools/trace_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define __TRACE_HELPERS_H

#include <stdbool.h>
#include "string_helpers.h"

#define NSEC_PER_SEC 1000000000ULL

Expand All @@ -19,6 +20,7 @@ const struct ksym *ksyms__map_addr(const struct ksyms *ksyms,
unsigned long addr);
const struct ksym *ksyms__get_symbol(const struct ksyms *ksyms,
const char *name);
struct string_array *ksyms__get_symbols_re(const struct ksyms *ksyms, const char *pattern);

struct sym {
const char *name;
Expand Down

0 comments on commit 82468f9

Please sign in to comment.