perf btf: Make the sigtrap test helper to find a member by name widely available

By introducing a tools/perf/util/btf.c to collect utilities not yet
available via libbpf, the first being a way to find a member by name
once we get the type_id for the struct.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2024-12-09 16:00:39 -03:00
parent bfb9467535
commit dcf900429d
4 changed files with 41 additions and 17 deletions

View File

@ -56,6 +56,7 @@ static struct perf_event_attr make_event_attr(void)
#ifdef HAVE_BPF_SKEL
#include <bpf/btf.h>
#include <util/btf.h>
static struct btf *btf;
@ -73,21 +74,6 @@ static void btf__exit(void)
btf = NULL;
}
static const struct btf_member *__btf_type__find_member_by_name(int type_id, const char *member_name)
{
const struct btf_type *t = btf__type_by_id(btf, type_id);
const struct btf_member *m;
int i;
for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
const char *current_member_name = btf__name_by_offset(btf, m->name_off);
if (!strcmp(current_member_name, member_name))
return m;
}
return NULL;
}
static bool attr_has_sigtrap(void)
{
int id;
@ -101,7 +87,7 @@ static bool attr_has_sigtrap(void)
if (id < 0)
return false;
return __btf_type__find_member_by_name(id, "sigtrap") != NULL;
return __btf_type__find_member_by_name(btf, id, "sigtrap") != NULL;
}
static bool kernel_with_sleepable_spinlocks(void)
@ -119,7 +105,7 @@ static bool kernel_with_sleepable_spinlocks(void)
return false;
// Only RT has a "lock" member for "struct spinlock"
member = __btf_type__find_member_by_name(id, "lock");
member = __btf_type__find_member_by_name(btf, id, "lock");
if (member == NULL)
return false;

View File

@ -168,6 +168,7 @@ perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-flex.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-bison.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += btf.o
ifeq ($(CONFIG_LIBTRACEEVENT),y)
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_lock_contention.o

27
tools/perf/util/btf.c Normal file
View File

@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Arnaldo Carvalho de Melo <acme@redhat.com>
*
* Copyright (C) 2024, Red Hat, Inc
*/
#include <bpf/btf.h>
#include <util/btf.h>
#include <string.h>
const struct btf_member *__btf_type__find_member_by_name(struct btf *btf,
int type_id, const char *member_name)
{
const struct btf_type *t = btf__type_by_id(btf, type_id);
const struct btf_member *m;
int i;
for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
const char *current_member_name = btf__name_by_offset(btf, m->name_off);
if (!strcmp(current_member_name, member_name))
return m;
}
return NULL;
}

10
tools/perf/util/btf.h Normal file
View File

@ -0,0 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __PERF_UTIL_BTF
#define __PERF_UTIL_BTF 1
struct btf;
struct btf_member;
const struct btf_member *__btf_type__find_member_by_name(struct btf *btf,
int type_id, const char *member_name);
#endif // __PERF_UTIL_BTF