Cleanup: kallsyms wrapper refactoring
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 21 Dec 2023 20:10:22 +0000 (15:10 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 8 Jan 2024 17:06:43 +0000 (12:06 -0500)
Move the Christmas tree of #ifdef which was implemented in
do_get_kallsyms() into separate functions:

- kallsyms_pre_arch_adjust(),
- kallsyms_get_arch_call_addr().

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I1bfdb43682e466f446d376ba7a122e295a2f0ea1

src/wrapper/kallsyms.c

index e5b5e3101f42313b60fc2fd83bf507d6aaf44724..2535c2a527faed90f20909ce809334adaae28169 100644 (file)
 # error "LTTng-modules requires CONFIG_KPROBES on kernels >= 5.7.0"
 #endif
 
-#ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V1
-#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
-#include <asm/elf.h>
-
-#define LTTNG_FUNC_DESC_TYPE struct func_desc
-#define LTTNG_FUNC_DESC_ADDR_NAME addr
-
-#else
-
-#include <asm/types.h>
-
-#define LTTNG_FUNC_DESC_TYPE func_descr_t
-#define LTTNG_FUNC_DESC_ADDR_NAME entry
-#endif
-
-static
-LTTNG_FUNC_DESC_TYPE kallsyms_lookup_name_func_desc;
-#endif
-
 static
 unsigned long (*kallsyms_lookup_name_sym)(const char *name);
 
@@ -48,17 +29,10 @@ int dummy_kprobe_handler(struct kprobe *p, struct pt_regs *regs)
        return 0;
 }
 
+#ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V2
 static
-unsigned long do_get_kallsyms(void)
+void kallsyms_pre_arch_adjust(struct kprobe *probe)
 {
-       struct kprobe probe;
-       int ret;
-       unsigned long addr;
-
-       memset(&probe, 0, sizeof(probe));
-       probe.pre_handler = dummy_kprobe_handler;
-       probe.symbol_name = "kallsyms_lookup_name";
-#ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V2
        /*
         * With powerpc64 ABIv2, we need the global entry point of
         * kallsyms_lookup_name to call it later, while kprobe_register would
@@ -67,15 +41,40 @@ unsigned long do_get_kallsyms(void)
         * which is the length of one instruction to kallsyms_lookup_name to
         * avoid the adjustment.
         */
-       probe.offset = 4;
+       probe->offset = 4;
+}
+#else
+static
+void kallsyms_pre_arch_adjust(struct kprobe *probe)
+{
+}
 #endif
-       ret = register_kprobe(&probe);
-       if (ret)
-               return 0;
+
 #ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V2
+static
+unsigned long kallsyms_get_arch_call_addr(const struct kprobe *probe)
+{
        /* Substract 4 bytes to get what we originally want */
-       addr = (unsigned long)(((char *)probe.addr) - 4);
+       return (unsigned long)(((char *)probe->addr) - 4);
+}
+
 #elif defined(LTTNG_CONFIG_PPC64_ELF_ABI_V1)
+# if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
+# include <asm/elf.h>
+# define LTTNG_FUNC_DESC_TYPE struct func_desc
+# define LTTNG_FUNC_DESC_ADDR_NAME addr
+# else
+# include <asm/types.h>
+# define LTTNG_FUNC_DESC_TYPE func_descr_t
+# define LTTNG_FUNC_DESC_ADDR_NAME entry
+
+static
+LTTNG_FUNC_DESC_TYPE kallsyms_lookup_name_func_desc;
+# endif
+
+static
+unsigned long kallsyms_get_arch_call_addr(const struct kprobe *probe)
+{
        /*
         * Build a function descriptor from the address of
         * 'kallsyms_lookup_name' returned by kprobe and the toc of
@@ -84,16 +83,41 @@ unsigned long do_get_kallsyms(void)
         */
        kallsyms_lookup_name_func_desc.LTTNG_FUNC_DESC_ADDR_NAME = (unsigned long) probe.addr;
        kallsyms_lookup_name_func_desc.toc = ((LTTNG_FUNC_DESC_TYPE *) &sprint_symbol)->toc;
-       addr = (unsigned long) &kallsyms_lookup_name_func_desc;
-#else
-       addr = (unsigned long)probe.addr;
-#endif
-#ifdef CONFIG_ARM
-#ifdef CONFIG_THUMB2_KERNEL
+       return (unsigned long) &kallsyms_lookup_name_func_desc;
+}
+#elif defined(CONFIG_ARM) && defined(CONFIG_THUMB2_KERNEL)
+static
+unsigned long kallsyms_get_arch_call_addr(const struct kprobe *probe)
+{
+       unsigned long addr = (unsigned long)probe->addr;
+
        if (addr)
                addr |= 1; /* set bit 0 in address for thumb mode */
+       return addr;
+}
+#else
+static
+unsigned long kallsyms_get_arch_call_addr(const struct kprobe *probe)
+{
+       return (unsigned long)probe->addr;
+}
 #endif
-#endif
+
+static
+unsigned long do_get_kallsyms(void)
+{
+       struct kprobe probe;
+       int ret;
+       unsigned long addr;
+
+       memset(&probe, 0, sizeof(probe));
+       probe.pre_handler = dummy_kprobe_handler;
+       probe.symbol_name = "kallsyms_lookup_name";
+       kallsyms_pre_arch_adjust(&probe);
+       ret = register_kprobe(&probe);
+       if (ret)
+               return 0;
+       addr = kallsyms_get_arch_call_addr(&probe);
        unregister_kprobe(&probe);
        return addr;
 }
This page took 0.027838 seconds and 4 git commands to generate.