Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
This commit is contained in:
Wenchao Hao 2024-03-28 19:10:06 +08:00
parent 66986deed1
commit 3d4e51ff8c
3 changed files with 115 additions and 2 deletions

View File

@ -1,12 +1,13 @@
KDIR=/usr/src/kernels/$(shell uname -r) KDIR=/mnt/mainline
#KDIR=/usr/src/kernels/$(shell uname -r)
obj-m += demo.o obj-m += demo.o
obj-m += blk_kprobe.o obj-m += blk_kprobe.o
obj-m += kobject-example.o obj-m += kobject-example.o
obj-m += kprobe_trace.o
all: all:
make -C $(KDIR) M=$(shell pwd) modules make -C $(KDIR) M=$(shell pwd) modules
clean: clean:
make -C $(KDIR) M=$(shell pwd) clean make -C $(KDIR) M=$(shell pwd) clean

86
kprobe_trace.c Normal file
View File

@ -0,0 +1,86 @@
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
struct test_struct {
int val;
int arr[3];
int *parr;
char name[8];
char *pname;
};
void test_func1(struct test_struct *p);
void test_func2(struct test_struct p);
void test_func1(struct test_struct *p)
{
printk("%s\n", p->name);
printk("%s\n", p->pname);
printk("%d\n", p->arr[1]);
printk("%d\n", p->parr[1]);
}
void test_func2(struct test_struct p)
{
printk("%s\n", p.name);
printk("%s\n", p.pname);
printk("%d\n", p.arr[1]);
printk("%d\n", p.parr[1]);
}
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct test_struct a;
a.val = 10;
sprintf(a.name, "hwc");
a.pname="njit";
for (int i = 0; i < 3; i++)
a.arr[i] = i;
a.parr = a.arr;
test_func1(&a);
test_func2(a);
return 0;
}
static struct kobj_attribute foo_attribute =
__ATTR(foo, 0444, foo_show, NULL);
static struct attribute *attrs[] = {
&foo_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
example_kobj = kobject_create_and_add("kprobe_ftrace", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");

26
probe.sh Normal file
View File

@ -0,0 +1,26 @@
#!/bin/sh
echo 0 > /sys/kernel/debug/tracing/tracing_on
if [ -f /sys/kernel/debug/tracing/events/kprobes/enable ]; then
echo 0 > /sys/kernel/debug/tracing/events/kprobes/enable
fi
echo > /sys/kernel/debug/tracing/kprobe_events
# struct test_struct {
# int val;
# int arr[3];
# int *parr;
# char name[8];
# char *pname;
# };
# void test_func1(struct test_struct *p);
# void test_func2(struct test_struct p);
echo 'p:test_func1 test_func1 val=+0($arg1):s32 arr=+4($arg1):s32[3] parr=+0(+16($arg1)):s32[3] name=+24($arg1):string pname=+0(+32($arg1)):string' >> /sys/kernel/debug/tracing/kprobe_events
echo 'p:test_func2 test_func2 val=+0($arg1):s32 arr=+4($arg1):s32[3] parr=+0(+16($arg1)):s32[3] name=+24($arg1):string pname=+0(+32($arg1)):string' >> /sys/kernel/debug/tracing/kprobe_events
echo 1 > /sys/kernel/debug/tracing/options/sym-offset
echo 0 > /sys/kernel/debug/tracing/options/stacktrace
echo 1 > /sys/kernel/debug/tracing/events/kprobes/enable
echo 1 > /sys/kernel/debug/tracing/tracing_on