mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-08-19 01:22:36 +08:00
thread_info和内核栈stack的学习...
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
obj-m := print_vmarea.o
|
||||
obj-m := print_thread_info.o
|
||||
|
||||
else
|
||||
|
||||
@@ -14,5 +14,15 @@ all:
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
|
||||
insmod:
|
||||
sudo insmod ./print_thread_info.ko
|
||||
|
||||
rmmod:
|
||||
sudo rmmod print_thread_info
|
||||
|
||||
reinsmod:
|
||||
make rmmod
|
||||
make insmod
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -18,49 +18,83 @@
|
||||
#endif
|
||||
|
||||
|
||||
static int pid = 1;
|
||||
static int PID = 1;
|
||||
|
||||
module_param(pid,int,0644);
|
||||
module_param(PID, int, 0644);
|
||||
|
||||
static void print_thread_info_list(void)
|
||||
{
|
||||
struct task_struct *ptask;
|
||||
struct pid *k;
|
||||
struct vm_area_struct *tmp;
|
||||
|
||||
/* find tak by pid */
|
||||
k = find_vpid(pid);
|
||||
ptask = pid_task(k, PIDTYPE_PID);
|
||||
|
||||
printk("process:%s,pid:%d\n",p->comm,p->pid);
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct thread_info* get_threaf_info_by_task(struct task_struct *ptask)
|
||||
|
||||
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);
|
||||
|
||||
/* for the struct task_struct *task is the member of the struct thread_info
|
||||
* we can find the threadinfo or stack by
|
||||
*/
|
||||
threadinfo = (struct thread_info *)ptask;
|
||||
printk(KERN_INFO "thread_info : %p", threadinfo);
|
||||
|
||||
|
||||
return threadinfo;
|
||||
}
|
||||
|
||||
|
||||
void print_thread_info(struct thread_info *pthreadinfo)
|
||||
union thread_union* get_thread_union(struct thread_info *threadinfo)
|
||||
{
|
||||
printk(KERN_INFO "THREAD_SIZE : %d\n", THREAD_SZIE)
|
||||
union thread_union *threadunion = NULL;
|
||||
/* for the struct thread_info is the first member of the struct thread_info
|
||||
* we can find the threadinfo or stack by thread_info
|
||||
*/
|
||||
threadunion = (union thread_union *)threadinfo;
|
||||
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);
|
||||
|
||||
return threadunion;
|
||||
}
|
||||
|
||||
|
||||
unsigned long show_kstack(union thread_union *thread_union)
|
||||
{
|
||||
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 + length);
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void print_thread_info(int pid)
|
||||
{
|
||||
struct task_struct *ptask;
|
||||
struct pid *k;
|
||||
struct vm_area_struct *tmp;
|
||||
struct thread_info *threadinfo = NULL;
|
||||
union thread_union *threadunion = NULL;
|
||||
/* find tak by 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);
|
||||
|
||||
threadinfo = get_thread_info(ptask);
|
||||
threadunion = get_thread_union(threadinfo);
|
||||
show_kstack(threadunion);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int __init print_thread_info_init(void)
|
||||
{
|
||||
print_thread_info(PID);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit print_thread_info_exit(void)
|
||||
{
|
||||
printk(KERN_INFO "exit ---------------------!\n");
|
||||
|
||||
Reference in New Issue
Block a user