diff --git a/study/kernel/01-process/01-task/print_thread_info/1 b/study/kernel/01-process/01-task/print_thread_info/1 new file mode 100755 index 0000000..a3e7156 Binary files /dev/null and b/study/kernel/01-process/01-task/print_thread_info/1 differ diff --git a/study/kernel/01-process/01-task/print_thread_info/1.c b/study/kernel/01-process/01-task/print_thread_info/1.c new file mode 100644 index 0000000..3279f36 --- /dev/null +++ b/study/kernel/01-process/01-task/print_thread_info/1.c @@ -0,0 +1,19 @@ +/************************************************************************* + > File Name: 1.c + > Author: gatieme + > Created Time: Thu 02 Jun 2016 01:09:41 PM CST + ************************************************************************/ + +#include +#include + + + int main(void) +{ + + long a = 10; + long *pa = &a; + printf("0x%x, 0x%x\n", ++a, a); + printf("%p\n", pa); + printf("%p\n", ++pa); +} diff --git a/study/kernel/01-process/01-task/print_thread_info/print_thread_info.c b/study/kernel/01-process/01-task/print_thread_info/print_thread_info.c index 400bf79..e9c5364 100644 --- a/study/kernel/01-process/01-task/print_thread_info/print_thread_info.c +++ b/study/kernel/01-process/01-task/print_thread_info/print_thread_info.c @@ -25,13 +25,12 @@ module_param(PID, int, 0644); struct thread_info* get_thread_info(struct task_struct *ptask) { - printk(KERN_INFO "THREAD_SIZE : %dKB", THREAD_SIZE / 1024); struct thread_info *threadinfo = NULL; /* for the struct task_struct *task is the member of the struct thread_info * we can find the threadinfo by task use container_of */ threadinfo = container_of(ptask, struct thread_info, task); - printk(KERN_INFO "thread_info : %p", threadinfo); + printk(KERN_INFO "thread_info : %p", threadinfo); return threadinfo; @@ -44,26 +43,23 @@ union thread_union* get_thread_union(struct thread_info *threadinfo) * we can find the threadinfo or stack by thread_info */ threadunion = (union thread_union *)threadinfo; - printk(KERN_INFO "thread_union address : %p\n", threadunion); + printk(KERN_INFO "thread_union address : %p\n", threadunion); threadunion = NULL; threadunion = container_of(threadinfo, union thread_union, thread_info); - printk(KERN_INFO "thread_union address : %p\n", threadunion); + printk(KERN_INFO "thread_union address : %p\n", threadunion); return threadunion; } -unsigned long show_kstack(union thread_union *thread_union) +unsigned long show_kstack(union thread_union *threadunion) { - unsigned long *kstack = (unsigned long *)thread_union->stack; - unsigned length = THREAD_SIZE / sizeof(unsigned long) - 1; - unsigned long ksatck_start = kstack + sizeof(struct thread_info); - unsigned long kstack_end = kstack + length; - printk(KERN_INFO "sizeof(struct thread_info) : %d\n", sizeof(struct thread_info)); - printk(KERN_INFO "kstack start : %p\n", kstack + sizeof(struct thread_info)); - printk(KERN_INFO "kstack end : %p\n", kstack + THREAD_SIZE); - - + unsigned long kstack = (unsigned long)threadunion->stack; + printk(KERN_INFO "THREAD_SIZE : %d == %dKB", THREAD_SIZE, THREAD_SIZE / 1024); + printk(KERN_INFO "union_stack : %p\n", (unsigned long)kstack); + printk(KERN_INFO "union_stack align : %p\n", ((unsigned long)kstack & ~(THREAD_SIZE - 1))); + printk(KERN_INFO "kstack start : %p\n", (void *)(((unsigned long) kstack & ~(THREAD_SIZE - 1)) + THREAD_SIZE - 1)); + printk(KERN_INFO "kstack end : %p\n", (void *)(((unsigned long) kstack & ~(THREAD_SIZE - 1)) + sizeof(struct thread_info))); } static void print_thread_info(int pid) @@ -77,12 +73,15 @@ static void print_thread_info(int pid) k = find_vpid(pid); ptask = pid_task(k, PIDTYPE_PID); - printk(KERN_INFO "process : %s, pid : %d\n", ptask->comm, ptask->pid); - printk(KERN_INFO "stack : %p\n", ptask->stack); + printk(KERN_INFO "process : %s, pid : %d\n", ptask->comm, ptask->pid); threadinfo = get_thread_info(ptask); + threadunion = get_thread_union(threadinfo); + show_kstack(threadunion); + + printk(KERN_INFO "task stack : %p\n", ptask->stack); }