Skip to content

Commit d1f0258

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
selftests/bpf: Add {load,search}_kallsyms_custom_local()
These two functions allow selftests to do loading/searching kallsyms based on their specific compare functions. Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9475dac commit d1f0258

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tools/testing/selftests/bpf/trace_helpers.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ struct ksyms *load_kallsyms_local(void)
114114
return load_kallsyms_local_common(ksym_cmp);
115115
}
116116

117+
struct ksyms *load_kallsyms_custom_local(ksym_cmp_t cmp_cb)
118+
{
119+
return load_kallsyms_local_common(cmp_cb);
120+
}
121+
117122
int load_kallsyms(void)
118123
{
119124
pthread_mutex_lock(&ksyms_mutex);
@@ -153,6 +158,28 @@ struct ksym *ksym_search_local(struct ksyms *ksyms, long key)
153158
return &ksyms->syms[0];
154159
}
155160

161+
struct ksym *search_kallsyms_custom_local(struct ksyms *ksyms, const void *p,
162+
ksym_search_cmp_t cmp_cb)
163+
{
164+
int start = 0, mid, end = ksyms->sym_cnt;
165+
struct ksym *ks;
166+
int result;
167+
168+
while (start < end) {
169+
mid = start + (end - start) / 2;
170+
ks = &ksyms->syms[mid];
171+
result = cmp_cb(p, ks);
172+
if (result < 0)
173+
end = mid;
174+
else if (result > 0)
175+
start = mid + 1;
176+
else
177+
return ks;
178+
}
179+
180+
return NULL;
181+
}
182+
156183
struct ksym *ksym_search(long key)
157184
{
158185
if (!ksyms)

tools/testing/selftests/bpf/trace_helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct ksym {
1414
struct ksyms;
1515

1616
typedef int (*ksym_cmp_t)(const void *p1, const void *p2);
17+
typedef int (*ksym_search_cmp_t)(const void *p1, const struct ksym *p2);
1718

1819
int load_kallsyms(void);
1920
struct ksym *ksym_search(long key);
@@ -24,6 +25,10 @@ struct ksym *ksym_search_local(struct ksyms *ksyms, long key);
2425
long ksym_get_addr_local(struct ksyms *ksyms, const char *name);
2526
void free_kallsyms_local(struct ksyms *ksyms);
2627

28+
struct ksyms *load_kallsyms_custom_local(ksym_cmp_t cmp_cb);
29+
struct ksym *search_kallsyms_custom_local(struct ksyms *ksyms, const void *p1,
30+
ksym_search_cmp_t cmp_cb);
31+
2732
/* open kallsyms and find addresses on the fly, faster than load + search. */
2833
int kallsyms_find(const char *sym, unsigned long long *addr);
2934

0 commit comments

Comments
 (0)