Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf test sigtrap: Generalize the BTF routine to reuse it in this test

Move the part that loads the BTF info to a "btf__available()" that will
lazy load the BTF info so that if we need it for some other test, which
we will in the following cset, we can reuse it.

At some point this will move from this specific 'perf test' entry to be
used in other parts of perf, do it when needed.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kate Carcia <kcarcia@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20231129154718.326330-2-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+40 -20
+40 -20
tools/perf/tests/sigtrap.c
··· 57 57 #ifdef HAVE_BPF_SKEL 58 58 #include <bpf/btf.h> 59 59 60 + static struct btf *btf; 61 + 62 + static bool btf__available(void) 63 + { 64 + if (btf == NULL) 65 + btf = btf__load_vmlinux_btf(); 66 + 67 + return btf != NULL; 68 + } 69 + 70 + static void btf__exit(void) 71 + { 72 + btf__free(btf); 73 + btf = NULL; 74 + } 75 + 76 + static const struct btf_member *__btf_type__find_member_by_name(int type_id, const char *member_name) 77 + { 78 + const struct btf_type *t = btf__type_by_id(btf, type_id); 79 + const struct btf_member *m; 80 + int i; 81 + 82 + for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { 83 + const char *current_member_name = btf__name_by_offset(btf, m->name_off); 84 + if (!strcmp(current_member_name, member_name)) 85 + return m; 86 + } 87 + 88 + return NULL; 89 + } 90 + 60 91 static bool attr_has_sigtrap(void) 61 92 { 62 - bool ret = false; 63 - struct btf *btf; 64 - const struct btf_type *t; 65 - const struct btf_member *m; 66 - const char *name; 67 - int i, id; 93 + int id; 68 94 69 - btf = btf__load_vmlinux_btf(); 70 - if (btf == NULL) { 95 + if (!btf__available()) { 71 96 /* should be an old kernel */ 72 97 return false; 73 98 } 74 99 75 100 id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT); 76 101 if (id < 0) 77 - goto out; 102 + return false; 78 103 79 - t = btf__type_by_id(btf, id); 80 - for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { 81 - name = btf__name_by_offset(btf, m->name_off); 82 - if (!strcmp(name, "sigtrap")) { 83 - ret = true; 84 - break; 85 - } 86 - } 87 - out: 88 - btf__free(btf); 89 - return ret; 104 + return __btf_type__find_member_by_name(id, "sigtrap") != NULL; 90 105 } 91 106 #else /* !HAVE_BPF_SKEL */ 92 107 static bool attr_has_sigtrap(void) ··· 123 108 } 124 109 125 110 return ret; 111 + } 112 + 113 + static void btf__exit(void) 114 + { 126 115 } 127 116 #endif /* HAVE_BPF_SKEL */ 128 117 ··· 240 221 sigaction(SIGTRAP, &oldact, NULL); 241 222 out: 242 223 pthread_barrier_destroy(&barrier); 224 + btf__exit(); 243 225 return ret; 244 226 } 245 227