thread_info和内核栈的学习...

This commit is contained in:
gatieme
2016-06-02 13:41:37 +08:00
parent ebc704ec70
commit c0612f1981
3 changed files with 34 additions and 16 deletions
Binary file not shown.
@@ -0,0 +1,19 @@
/*************************************************************************
> File Name: 1.c
> Author: gatieme
> Created Time: Thu 02 Jun 2016 01:09:41 PM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
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);
}
@@ -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);
}