IOCTL_TGID

This commit is contained in:
zy
2023-12-05 02:42:07 -05:00
parent 520f8db7e5
commit ff9a379cbc
4 changed files with 107 additions and 26 deletions

View File

@@ -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;
}