From 6397e0da75473c75a15695dbf8b27b3dac43fe07 Mon Sep 17 00:00:00 2001 From: zy Date: Thu, 16 Nov 2023 21:08:27 -0500 Subject: [PATCH] init && exit --- source/module/Makefile_bak | 3 --- source/module/monitor_kallsyms.c | 32 ++++++++++++++++++++---------- source/module/monitor_kernel.c | 4 ++-- source/module/monitor_kernel_lib.c | 27 +++++++++++++------------ source/module/monitor_timer.c | 2 +- 5 files changed, 39 insertions(+), 29 deletions(-) delete mode 100644 source/module/Makefile_bak diff --git a/source/module/Makefile_bak b/source/module/Makefile_bak deleted file mode 100644 index d3ede68..0000000 --- a/source/module/Makefile_bak +++ /dev/null @@ -1,3 +0,0 @@ -KMOD = variable_monitor -obj-m := $(KMOD).o -$(KMOD)-objs := monitor_kernel.o diff --git a/source/module/monitor_kallsyms.c b/source/module/monitor_kallsyms.c index 9815f79..ae2ea95 100644 --- a/source/module/monitor_kallsyms.c +++ b/source/module/monitor_kallsyms.c @@ -17,7 +17,7 @@ int (*orig_kernfs_name)(struct kernfs_node *kn, char *buf, size_t buflen); int (*orig_access_remote_vm)(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); -/// @brief init kallsyms_lookup_name +/// @brief init kallsyms_lookup_name,all orig_fun are depend on it /// @param /// @return 0 is success static int fn_kallsyms_lookup_name_init(void) { @@ -25,7 +25,7 @@ static int fn_kallsyms_lookup_name_init(void) { diag_kallsyms_lookup_name = (void *)kprobe_kallsyms_lookup_name.addr; unregister_kprobe(&kprobe_kallsyms_lookup_name); - printk("xby-debug, diag_kallsyms_lookup_name is %p\n", + printk(KERN_INFO "diag_kallsyms_lookup_name is %p\n", diag_kallsyms_lookup_name); if (!diag_kallsyms_lookup_name) { @@ -33,15 +33,27 @@ static int fn_kallsyms_lookup_name_init(void) { } return 0; } - +/** + * @brief get all orig_fun + * + * @return int 0 is success + */ int init_orig_fun(void) { - fn_kallsyms_lookup_name_init(); // init kallsyms_lookup_name - LOOKUP_SYMS(stack_trace_save_tsk); // stack_trace_save_tsk - LOOKUP_SYMS(show_stack); // show_stack - LOOKUP_SYMS(idle_sched_class); // idle_sched_class - LOOKUP_SYMS(access_remote_vm); // access_remote_vm + int ret = fn_kallsyms_lookup_name_init(); // init kallsyms_lookup_name - LOOKUP_SYMS_NORET(get_task_type); // get_task_type - LOOKUP_SYMS_NORET(kernfs_name); // kernfs_name + if (ret) + { + printk(KERN_INFO "init kallsyms_lookup_name failed\n"); + return ret; + } + + LOOKUP_SYMS(stack_trace_save_tsk); // stack_trace_save_tsk + LOOKUP_SYMS(show_stack); // show_stack + LOOKUP_SYMS(idle_sched_class); // idle_sched_class + LOOKUP_SYMS(access_remote_vm); // access_remote_vm + + LOOKUP_SYMS_NORET( + get_task_type); // get_task_type | this fun is not available on 5.17.15 + LOOKUP_SYMS_NORET(kernfs_name); // kernfs_name return 0; } diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c index b79d131..b6cd892 100644 --- a/source/module/monitor_kernel.c +++ b/source/module/monitor_kernel.c @@ -64,7 +64,7 @@ static struct file_operations fops = { }; int init_module(void) { - printk(KERN_INFO "%s\n", __FUNCTION__); + printk(KERN_INFO "variable_monitor fun: %s\n", __FUNCTION__); if (alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME) < 0) { printk(KERN_ALERT "Failed to register device number\n"); return -1; @@ -110,7 +110,7 @@ int init_module(void) { } void cleanup_module(void) { - printk(KERN_INFO "%s\n", __FUNCTION__); + printk(KERN_INFO "variable_monitor fun: %s\n", __FUNCTION__); // clear all watch | free buffer monitor_exit(); diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index 38fdfd4..b2b0260 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -12,8 +12,8 @@ static unsigned char w_arg2k_w_arg(void *ptr, watch_arg warg, kernel_watch_arg *k_watch_arg) { // k_watch_arg init k_watch_arg->task_id = warg.task_id; - strncpy(k_watch_arg->name, warg.name, MAX_NAME_LEN + 1); // name - k_watch_arg->name[MAX_NAME_LEN + 1] = '\0'; // just in case + strncpy(k_watch_arg->name, warg.name, MAX_NAME_LEN + 1); // name + k_watch_arg->name[MAX_NAME_LEN + 1] = '\0'; // just in case k_watch_arg->kptr = ptr; k_watch_arg->length_byte = warg.length_byte; k_watch_arg->threshold = warg.threshold; @@ -28,7 +28,7 @@ static void init_mm_tree(mm_tree *mm_tree) { } static int init_buffer(unsigned int buf_size) { - init_mm_tree(&mm_tree_struct); // init mm_tree + init_mm_tree(&mm_tree_struct); // init mm_tree init_diag_variant_buffer(&load_monitor_variant_buffer, buf_size); int ret = 0; ret = alloc_diag_variant_buffer(&load_monitor_variant_buffer); @@ -56,10 +56,10 @@ static void clear_all_watch(void) { int monitor_init(void) { int ret = 0; - init_orig_fun(); // init orig_X - ret = init_buffer(50 * 1024 * 1024); // 50M - if (ret) - return 0; + ret = init_orig_fun(); // init orig_X + if (ret) return ret; + ret = init_buffer(50 * 1024 * 1024); // 50M + if (ret) return -1; return 0; } @@ -72,6 +72,7 @@ void monitor_exit() { clear_all_watch(); // free buffer destroy_diag_variant_buffer(&load_monitor_variant_buffer); + printk(KERN_INFO "clear all buffer\n"); } /** * @brief start watch variable @@ -99,7 +100,7 @@ int start_watch_variable(watch_arg warg) { } // k_watch_arg init w_arg2k_w_arg(kptr, warg, &k_watch_arg); - timer = get_timer(warg.time_ns); // get a valuable timer + timer = get_timer(warg.time_ns); // get a valuable timer printk(KERN_INFO "ptr transform kptr: %p\n", kptr); printk(KERN_INFO "timer: %p\n", timer); @@ -107,7 +108,7 @@ int start_watch_variable(watch_arg warg) { timer->sentinel, timer->time_ns); printk(KERN_INFO "timer->hr_timer: %p\n", &timer->hr_timer); - TIMER_CANCEL(timer); // just in case + TIMER_CANCEL(timer); // just in case timer_add_watch(timer, k_watch_arg); TIMER_START(timer); @@ -122,8 +123,8 @@ int start_watch_variable(watch_arg warg) { */ void clear_watch(pid_t pid) { printk(KERN_INFO "clear pid %d 's watch variable\n", pid); - cancel_all_hrTimer(); // just in case - del_all_kwarg_by_pid(pid); // delete all kwarg with pid - free_page_list(pid); // free page with pid - start_all_hrTimer(); // restart timer + cancel_all_hrTimer(); // just in case + del_all_kwarg_by_pid(pid); // delete all kwarg with pid + free_page_list(pid); // free page with pid + start_all_hrTimer(); // restart timer } \ No newline at end of file diff --git a/source/module/monitor_timer.c b/source/module/monitor_timer.c index 96152a3..2bef95d 100644 --- a/source/module/monitor_timer.c +++ b/source/module/monitor_timer.c @@ -200,5 +200,5 @@ void cancel_all_hrTimer(void) { TIMER_CANCEL(timer); } - printk("HrTimer cancel,module keep %d hrtimer for now\n", kernel_wtimer_num); + printk(KERN_INFO "HrTimer cancel,module keep %d hrtimer for now\n", kernel_wtimer_num); } \ No newline at end of file