Linux Kernel Explore With GDB
Prepare the kernel
The kernel need be compiled with CONFIG_DEBUG_INFO. Without it, vmlinux is pretty useless because it isn’t equipped with symbol table. Enabling CONFIG_DEBUG_INFO will compile vmlinux with -g
And the kernel need be compiled with CONFIG_PROC_KCORE to got the /proc/kcore
Running the GDB
gdb vmlinux /proc/kcore
(gdb) p init_task
Refresh the dynamically changed data
GDB will caches the result of print commnad. But kcore is dynamically changed, so need to re-load the core file to refresh the data:
(gdb) core-file /proc/kcore
GDB macro for get the task_struct of a pid
define get_task
set $t = &init_task
set $t = (struct task_struct *)$t
set $offset = ((char*)&$t->tasks - (char*)$t)
set $t=(struct task_struct*)((char*)$t->tasks.next - (char*)$offset)
while (&init_task != $t && $t->pid != (unsigned)$arg0)
set $t = (struct task_struct *)$t
set $offset = ((char*)&$t->tasks - (char*)$t)
set $t=(struct task_struct*)((char*)$t->tasks.next - (char*)$offset)
end
if ($t == &init_task)
printf "Could not find task: using init_task\n"
end
print *$t
end
document get_task
get the task_struct of specific pid.
end