Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kprobes: Make kprobe register via symbol name #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/kprobes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ typedef int (*kretprobe_handler_t) (struct kprobe *kp, uint32_t *stack,
uint32_t *kp_regs);

struct kprobe {
/* location of the probe point */
void *addr;

/* Allow user to indicate symbol name of the probe point */
const char *symbol_name;

kprobe_pre_handler_t pre_handler;
kprobe_post_handler_t post_handler;
struct breakpoint *bkpt;
Expand Down
1 change: 1 addition & 0 deletions include/ksym.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ int ksym_total(void);
int ksym_lookup(void *addr);
char *ksym_id2name(int symid);
void *ksym_id2addr(int symid);
void *ksym_lookup_name(const char *name);

#endif /* KSYM_H_ */
32 changes: 31 additions & 1 deletion kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* found in the LICENSE file.
*/

#include <ksym.h>
#include <kprobes.h>
#include <platform/armv7m.h>
#include <platform/breakpoint.h>
Expand Down Expand Up @@ -52,9 +53,38 @@ void kplist_del(struct kprobe *kp)
}
}

static void *kprobe_addr(struct kprobe *kp)
{
void *addr = kp->addr;

/*
* Cannot use address and symbol name at the same time.
* And, you should provide at least one of them.
*/
if ((kp->addr && kp->symbol_name) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rewrite by following to comply with consistent coding style:

    if ((kp->addr && kp->symbol_name) ||
        (!kp->addr && !kp->symbol_name))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll need to make a script to check this kind of hard find coding style problem......

(!kp->addr && !kp->symbol_name))
return NULL;

if (kp->symbol_name)
addr = ksym_lookup_name(kp->symbol_name);

addr = (void *)((uint32_t) addr & ~(1UL));
if (addr)
return addr;

/* Return NULL when can't found the address of symbol */
return NULL;
}

int kprobe_register(struct kprobe *kp)
{
kp->addr = (void *) ((uint32_t) kp->addr & ~(1UL));
void *addr;

addr = kprobe_addr(kp);
if (!addr)
return -1;

kp->addr = addr;
if (is_thumb32(*(uint16_t *) kp->addr))
kp->step_addr = kp->addr + 4;
else
Expand Down
13 changes: 13 additions & 0 deletions kernel/ksym.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ksym.h>
#include <types.h>
#include <lib/stdlib.h>
#include <lib/string.h>
#include <init_hook.h>
#include <platform/link.h>

Expand Down Expand Up @@ -78,3 +79,15 @@ void *ksym_id2addr(int symid)
{
return __ksym_tbl[symid].addr;
}

/* Lookup the address for this symbol. Returns 0 if not found. */
void *ksym_lookup_name(const char *name)
{
for (int i = 0; i < __ksym_count; ++i) {
if (strcmp(name, ksym_id2name(i)) == 0) {
return ksym_id2addr(i);
}
}

return NULL;
}
1 change: 1 addition & 0 deletions kernel/lib/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
lib-str-y += \
memcpy.o \
memset.o \
strcmp.o


kernel-lib-y += \
Expand Down