Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools: add string helpers, filter ksyms by regex #4964

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ SIGSNOOP_ALIAS = killsnoop
APP_ALIASES = $(FSDIST_ALIASES) $(FSSLOWER_ALIASES) ${SIGSNOOP_ALIAS}

COMMON_OBJ = \
$(OUTPUT)/string_helpers.o \
$(OUTPUT)/trace_helpers.o \
$(OUTPUT)/syscall_helpers.o \
$(OUTPUT)/errno_helpers.o \
Expand Down
68 changes: 68 additions & 0 deletions libbpf-tools/string_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
// Copyright (c) 2024 Tiago Ilieve
//
// 15-Apr-2024 Tiago Ilieve Created this.
#include <stdlib.h>
#include <string.h>
#include "string_helpers.h"

struct string_array *string_array__init() {
struct string_array *arr;

arr = (struct string_array *) malloc(sizeof(struct string_array));
if (arr == NULL) {
return NULL;
}

arr->data = (char **) malloc(sizeof(char *));
if (arr->data == NULL) {
free(arr);
return NULL;
}

arr->size = 0;
arr->cap = 1;

return arr;
}

void string_array__free(struct string_array *arr) {
if (arr == NULL || arr->data == NULL) {
return;
}

for (int i = 0; i < arr->size; i++) {
free(arr->data[i]);
}

free(arr->data);
free(arr);
arr = NULL;
}

int string_array__push(struct string_array *arr, const char *s) {
char **data;
char *str;
int cap;

if (arr->size == arr->cap) {
cap = arr->cap * 2;
data = (char **) realloc(arr->data, cap * sizeof(char *));
if (data == NULL) {
return -1;
}

arr->cap = cap;
arr->data = data;
}

str = strdup(s);
if (str == NULL) {
return -1;
}

arr->data[arr->size] = str;
arr->size++;

return 0;
}
17 changes: 17 additions & 0 deletions libbpf-tools/string_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#ifndef __STRING_HELPERS_H
#define __STRING_HELPERS_H

struct string_array {
char **data;
int size;
int cap;
};

struct string_array *string_array__init();

void string_array__free(struct string_array *arr);

int string_array__push(struct string_array *arr, const char *s);

#endif /* __STRING_HELPERS_H */
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
Loading