diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c index 0d59d0e..aa5754d 100644 --- a/source/module/monitor_kernel.c +++ b/source/module/monitor_kernel.c @@ -47,6 +47,8 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, int ret = 0; watch_arg warg; ioctl_dump_param dump_param; + int wpid; + printk(KERN_INFO "variable_monitor fun: %s with ioctl_num %d\n", __FUNCTION__, ioctl_num); @@ -79,7 +81,11 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, // printk(KERN_INFO "ret %d, %lu\n", ret, dump_param.user_buf_len); } printk(KERN_INFO "copy_to_user \n"); - + break; + case 2: + printk(KERN_INFO "variable_monitor test 2\n"); + ret = copy_from_user(&wpid, (int *)ioctl_param, sizeof(int)); + diag_test(wpid); /* code */ break; default: diff --git a/source/module/monitor_kernel.h b/source/module/monitor_kernel.h index de394c7..e796d2a 100644 --- a/source/module/monitor_kernel.h +++ b/source/module/monitor_kernel.h @@ -13,3 +13,5 @@ int start_watch_variable(watch_arg warg); void clear_watch(pid_t pid); enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // callback + +int diag_test(int nid); // for test diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index f203f5c..cff51d7 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -229,6 +229,7 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { k_w_arg2threshold(kwarg, &vm_record.threshold_record[i]); } rcu_read_lock(); + diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags); diag_variant_buffer_reserve(&load_monitor_variant_buffer, sizeof(variable_monitor_record)); @@ -237,6 +238,8 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { diag_variant_buffer_seal(&load_monitor_variant_buffer); diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags); + rcu_read_unlock(); + do_each_thread(g, p) { if (p->__state == TASK_RUNNING || __task_contributes_to_load(p) || p->__state == TASK_IDLE || 1) { @@ -262,8 +265,6 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { } while_each_thread(g, p); - rcu_read_unlock(); - // print_task_stack(); // restart timer after 5s hrtimer_forward(timer, timer->base->get_time(), ktime_set(5, 0)); //! todo @@ -273,4 +274,64 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt); } return HRTIMER_RESTART; // restart timer +} + +static void test(struct task_struct *p, variable_monitor_task *tsk_info, unsigned long *flags){ + unsigned int nr_bt; + printk(KERN_INFO "diag_tsk\n"); + diag_task_brief(p, &tsk_info->task); // task brief + // printk("1\n"); + diag_task_user_stack(p, &tsk_info->user_stack); // user stack + diag_task_raw_stack(p, &tsk_info->raw_stack); // raw stack + nr_bt = diag_task_kern_stack(p, &tsk_info->kern_stack); // kernel stack + dump_proc_chains_argv(1, p, &mm_tree_struct, + &tsk_info->proc_chains); // proc chains + + put_task_struct(p); + + diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags); + diag_variant_buffer_reserve(&load_monitor_variant_buffer,sizeof(variable_monitor_task)); + diag_variant_buffer_write_nolock(&load_monitor_variant_buffer, &tsk_info, + sizeof(variable_monitor_task)); + diag_variant_buffer_seal(&load_monitor_variant_buffer); + diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags); +} + +static int diag_test(int nid){ + struct task_struct *tsk; + struct task_struct *leader; + variable_monitor_task tsk_info; + // unsigned int nr_bt; + + int ret; + unsigned long flags; + pid_t id = (pid_t)nid; + + rcu_read_lock(); + tsk = NULL; + if (orig_find_task_by_vpid) + tsk = orig_find_task_by_vpid(id); + if (!tsk) { + ret = -EINVAL; + rcu_read_unlock(); + return ret; + } + + leader = tsk->group_leader; + if (leader == NULL || leader->exit_state == EXIT_ZOMBIE){ + ret = -EINVAL; + rcu_read_unlock(); + return ret; + } + + get_task_struct(tsk); + rcu_read_unlock(); + + // test(tsk, &tsk_info); + + put_task_struct(tsk); + + // test2(&tsk_info, &flags); + + return 0; } \ No newline at end of file diff --git a/source/module/monitor_trace.c b/source/module/monitor_trace.c index 20024d2..3455204 100644 --- a/source/module/monitor_trace.c +++ b/source/module/monitor_trace.c @@ -256,10 +256,20 @@ static int diagnose_task_raw_stack_remote(struct task_struct *tsk, void *to, int ret; struct mm_struct *mm; - if (in_atomic() || irqs_disabled()) { + if (in_atomic()) { + printk("task_raw_stack_remote %d in_atomic\n", tsk->pid); return 0; } + if (irqs_disabled()) { + printk("task_raw_stack_remote %d irqs_disabled\n", tsk->pid); + return 0; + } + + // if (in_atomic() || irqs_disabled()) { + // return 0; + // } + mm = get_task_mm(tsk); if (!mm) return 0; @@ -267,6 +277,8 @@ static int diagnose_task_raw_stack_remote(struct task_struct *tsk, void *to, ret = orig_access_remote_vm(mm, (unsigned long)from, to, n, 0); mmput(mm); + printk("task_raw_stack_remote %d access_remote_vm %d\n", tsk->pid, ret); + return ret < 0 ? ret : 0; } diff --git a/source/module/monitor_trace.h b/source/module/monitor_trace.h index 1938862..0d8420b 100644 --- a/source/module/monitor_trace.h +++ b/source/module/monitor_trace.h @@ -113,7 +113,6 @@ void dump_proc_chains_argv( int style, struct task_struct *tsk, mm_tree *mm_tree, proc_chains_detail *detail); // get process chains argv -void diag_test(int nid); // for test // print // void diag_printf_kern_stack(kern_stack_detail *kern_stack);