I wrote a simple kernel module that loops through all processes and extracts their registers saved when these were descheduled (especially EIP).
If I'm not wrong, what I need is saved on the kernel stack pointed by sp0 in the thread_struct of every process. This is what I do:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
int init_module(void){
struct task_struct *t;
struct pt_regs regs;
for_each_process(t){
memcpy(®s, (unsigned long*)(t->thread.sp0-sizeof(struct pt_regs)), sizeof(struct pt_regs));
printk(KERN_INFO "%s eip: %lx\n", t->comm, regs.ip);
}
return 0;
}
void cleanup_module(void){
}
MODULE_LICENSE("GPL");
Now, the output about user-level processes seems legit:
[ 3558.322088] bash eip: b770b430
BUT all I get from kernel threads is always 0.
[ 3558.322095] kworker/0:0 eip: 0
I don't get it.
Does the kernel save registers somewhere else when it comes to kernel threads?
Is it by chance related to kernel preemption?
I'm on a 3.14-1-486 kernel.
Thank you in advance.