diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c index 53b056c..77ac231 100644 --- a/source/module/monitor_kernel.c +++ b/source/module/monitor_kernel.c @@ -44,15 +44,15 @@ static int device_release(struct inode *inode, struct file *file) { } typedef struct { - int pid; -} ioctl_pid; + int id; +} ioctl_id; static long device_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param) { int ret = 0; watch_arg warg; ioctl_dump_param dump_param; - ioctl_pid wpid; + ioctl_id wid; printk(KERN_INFO "variable_monitor fun: %s with ioctl_num %d\n", __FUNCTION__, ioctl_num); @@ -91,8 +91,13 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, break; case IOCTL_PID: printk(KERN_INFO "variable_monitor PID\n"); - ret = copy_from_user(&wpid, (ioctl_pid *)ioctl_param, sizeof(ioctl_pid)); - diag_pid(wpid.pid); + ret = copy_from_user(&wid, (ioctl_id *)ioctl_param, sizeof(ioctl_id)); + diag_pid(wid.id); + break; + case IOCTL_TGID: + printk(KERN_INFO "variable_monitor TGID\n"); + ret = copy_from_user(&wid, (ioctl_id *)ioctl_param, sizeof(ioctl_id)); + diag_tgid(wid.id); break; default: break; diff --git a/source/module/monitor_kernel.h b/source/module/monitor_kernel.h index f20665f..d2bbdc6 100644 --- a/source/module/monitor_kernel.h +++ b/source/module/monitor_kernel.h @@ -11,6 +11,7 @@ #define IOCTL_WATCH_VARIABLE 0 #define IOCTL_DUMP_LOG 1 #define IOCTL_PID _IOWR(IOCTL_MAGIC_NUMBER, 2, int) +#define IOCTL_TGID _IOWR(IOCTL_MAGIC_NUMBER, 3, int) // default value extern int def_interval_ns; @@ -28,4 +29,5 @@ void clear_watch(pid_t pid); // for release enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // hrtimer callback -int diag_pid(int pid); // for test \ No newline at end of file +int diag_pid(int id); // for test +int diag_tgid(int id); // for test \ No newline at end of file diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index c2d937b..c816427 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -76,8 +76,8 @@ static void k_w_arg2threshold(kernel_watch_arg *k_watch_arg, threshold->ptr = k_watch_arg->ptr; threshold->threshold = k_watch_arg->threshold; // read true value - // threshold->true_value = convert_to_longlong(k_watch_arg->ptr, k_watch_arg->length_byte, k_watch_arg->is_unsigned); - + // threshold->true_value = convert_to_longlong(k_watch_arg->ptr, + // k_watch_arg->length_byte, k_watch_arg->is_unsigned); } static void init_mm_tree(mm_tree *mm_tree) { @@ -109,7 +109,7 @@ static int init_buffer(unsigned int buf_size) { static void diag_tsk(struct task_struct *p, variable_monitor_task *tsk_info) { unsigned int nr_bt; // printk(KERN_INFO "diag_tsk\n"); - diag_task_brief(p, &tsk_info->task); // task brief + diag_task_brief(p, &tsk_info->task); // task brief // diag_task_user_stack(p, &tsk_info->user_stack); // user stack nr_bt = diag_task_kern_stack(p, &tsk_info->kern_stack); // kernel stack dump_proc_chains_argv(1, p, &mm_tree_struct, @@ -192,7 +192,7 @@ void diag_task_info_work(struct work_struct *work) { .name, // Assuming name is a null-terminated string vm_record.threshold_record[i].ptr, vm_record.threshold_record[i].threshold); - // vm_record.threshold_record[i].true_value); + // vm_record.threshold_record[i].true_value); } rcu_read_lock(); @@ -238,7 +238,7 @@ int monitor_init(void) { ret = init_orig_fun(); // init orig_X if (ret) return ret; - //!todo + //! todo ret = init_buffer(256 * 1024 * 1024); // 512M if (ret) return -1; @@ -313,7 +313,8 @@ void init_work_all_hrTimer(void) { // init work INIT_WORK(&timer->wk, diag_task_info_work); } - // printk(KERN_INFO "HrTimer work init,module keep %d hrtimer for now\n", kernel_wtimer_num); + // printk(KERN_INFO "HrTimer work init,module keep %d hrtimer for now\n", + // kernel_wtimer_num); } /** @@ -327,7 +328,7 @@ void clear_watch(pid_t pid) { del_all_kwarg_by_pid(pid); // delete all kwarg with pid free_page_list(pid); // free page with pid init_work_all_hrTimer(); - start_all_hrTimer(); // restart timer + start_all_hrTimer(); // restart timer } /** @@ -345,8 +346,9 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { // check all watched kernel_watch_arg for (i = 0; i < k_watch_timer->sentinel; i++) { kwarg = &k_watch_timer->k_watch_args[i]; - if (read_and_compare(kwarg->kptr, kwarg->length_byte, kwarg->above_threshold, - kwarg->is_unsigned, kwarg->threshold)) { + if (read_and_compare(kwarg->kptr, kwarg->length_byte, + kwarg->above_threshold, kwarg->is_unsigned, + kwarg->threshold)) { k_watch_timer->threshold_buffer[j] = i; j++; } @@ -366,7 +368,7 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { return HRTIMER_RESTART; // restart timer } -int diag_pid(int pid) { +int diag_pid(int id) { struct task_struct *tsk; int ret; unsigned long flags; @@ -375,17 +377,17 @@ int diag_pid(int pid) { static variable_monitor_task tsk_info = {0}; static variable_monitor_record vm_record = {0}; - pid_t id = (pid_t)pid; + pid_t pid = (pid_t)id; 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; - } + tsk = NULL; + if (orig_find_task_by_vpid) + tsk = orig_find_task_by_vpid(pid); + if (!tsk) { + ret = -EINVAL; + rcu_read_unlock(); + return ret; + } rcu_read_unlock(); get_task_struct(tsk); // count +1 @@ -398,5 +400,54 @@ int diag_pid(int pid) { push_tskinfo_2_buffer(&tsk_info, &flags); // push to buffer + return 0; +} + +int diag_tgid(int id) { + struct task_struct *tsk; + int ret; + unsigned long flags; + unsigned long event_id = get_cycles(); + + static variable_monitor_task tsk_info = {0}; + static variable_monitor_record vm_record = {0}; + + pid_t tgid = (pid_t)id; + + rcu_read_lock(); + tsk = NULL; + if (orig_find_task_by_vpid) + tsk = orig_find_task_by_vpid(tgid); + if (!tsk) { + ret = -EINVAL; + rcu_read_unlock(); + return ret; + } + rcu_read_unlock(); + + struct task_struct *thread = tsk; + + while_each_thread(tsk, thread) { + // save_task_info(thread, &detail); + + // diag_variant_buffer_spin_lock(&pupil_variant_buffer, flags); + // diag_variant_buffer_reserve(&pupil_variant_buffer, + // sizeof(struct pupil_task_detail)); + // diag_variant_buffer_write_nolock(&pupil_variant_buffer, &detail, + // sizeof(struct pupil_task_detail)); + // diag_variant_buffer_seal(&pupil_variant_buffer); + // diag_variant_buffer_spin_unlock(&pupil_variant_buffer, flags); + + // get_task_struct(thread); // count +1 + + tsk_info.et_type = VARIABLE_MONITOR_TASK_TYPE; + tsk_info.id = event_id; + tsk_info.tv = vm_record.tv; + diag_tsk(thread, &tsk_info); + // put_task_struct(thread); // count -1 + + push_tskinfo_2_buffer(&tsk_info, &flags); // push to buffer + } + return 0; } \ No newline at end of file diff --git a/source/ucli/ucli.cc b/source/ucli/ucli.cc index 5a8e556..cda768d 100644 --- a/source/ucli/ucli.cc +++ b/source/ucli/ucli.cc @@ -13,6 +13,7 @@ // !todo move to a header file #define IOCTL_MAGIC_NUMBER 'k' #define IOCTL_PID _IOWR(IOCTL_MAGIC_NUMBER, 2, int) +#define IOCTL_TGID _IOWR(IOCTL_MAGIC_NUMBER, 3, int) static int task_info_extract(void *buf, unsigned int len, void *) { int *et_type; @@ -122,6 +123,25 @@ static void do_pid(char *arg) { do_dump(""); } +static void do_tgid(char *arg) { + int pid = 0; + int ret; + + sscanf(optarg, "%d", &pid); + if (pid <= 0) { + printf("参数错误\n"); + return; + } + printf("获取 tgid 信息: %d\n", pid); + ret = diag_call_ioctl(IOCTL_TGID, (long)&pid); + if (ret) { + printf("获取线程信息错误: %d\n", ret); + } + sleep(3); + + do_dump(""); +} + int main(int argc, char *argv[]) { run_in_host = check_in_host(); @@ -151,7 +171,7 @@ int main(int argc, char *argv[]) { break; } - option_index = 2; + // option_index = 2; switch (option_index) { case 0: // help @@ -163,6 +183,9 @@ int main(int argc, char *argv[]) { case 2: // pid do_pid(optarg); break; + case 3: // tgid + do_tgid(optarg); + break; default: // usage_pupil(); break;