From c065ac86cfd9153e4fb71bbc06d440a41834b878 Mon Sep 17 00:00:00 2001 From: Zhen Lei Date: Thu, 15 Dec 2022 20:12:38 +0800 Subject: [PATCH v2] kallsyms: debug m68k Signed-off-by: Zhen Lei --- kernel/kallsyms.c | 57 ++++++++++++++++++++++++++++++++++++++ kernel/kallsyms_selftest.c | 3 ++ 2 files changed, 60 insertions(+) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 83f499182c9aa31..80251da9952b957 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -267,6 +267,63 @@ static int kallsyms_lookup_names(const char *name, return 0; } +void tst_kallsyms_lookup_name(const char *name, void *addr) +{ + int i; + int ret; + int low, mid, high; + unsigned int seq, off; + char namebuf[KSYM_NAME_LEN]; + + ret = kallsyms_lookup_names(name, &i, NULL); + if (!ret) { + pr_warn("tst: kallsyms_lookup_names() is OK, name=%s, i=%d\n", name, i); + if (addr == (void *)kallsyms_sym_address(get_symbol_seq(i))) + pr_warn("tst: lookup address is OK\n"); + return; + } + + + for (i = 0; i < kallsyms_num_syms; i++) { + seq = get_symbol_seq(i); + off = get_symbol_offset(seq); + kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); + ret = compare_symbol_name(name, namebuf); + if (!ret) { + pr_warn("tst: found %s at index=%d\n", name, i); + if (addr == (void *)kallsyms_sym_address(get_symbol_seq(i))) + pr_warn("tst: lookup address is OK\n"); + break; + } + } + + if (i == kallsyms_num_syms) { + pr_warn("tst: found %s failed\n", name); + return; + } + + low = 0; + high = kallsyms_num_syms - 1; + + while (low <= high) { + mid = low + (high - low) / 2; + seq = get_symbol_seq(mid); + off = get_symbol_offset(seq); + kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); + pr_warn("tst: [%d] = %s, seq=%d, offset=%d\n", mid, namebuf, seq, off); + ret = compare_symbol_name(name, namebuf); + if (ret > 0) + low = mid + 1; + else if (ret < 0) + high = mid - 1; + else + break; + } + + if (mid == i) + pr_warn("tst: binary search is OK\n"); +} + /* Lookup the address for this symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name) { diff --git a/kernel/kallsyms_selftest.c b/kernel/kallsyms_selftest.c index f35d9cc1aab1544..f0f7c9fc72c5d53 100644 --- a/kernel/kallsyms_selftest.c +++ b/kernel/kallsyms_selftest.c @@ -293,6 +293,9 @@ static int test_kallsyms_basic_function(void) unsigned long addr, lookup_addr; char namebuf[KSYM_NAME_LEN]; struct test_stat *stat, *stat2; + extern void tst_kallsyms_lookup_name(const char *name, void *addr); + + tst_kallsyms_lookup_name("kallsyms_test_func", kallsyms_test_func); stat = kmalloc(sizeof(*stat) * 2, GFP_KERNEL); if (!stat) -- 2.25.1