跳转至

amd64下基于per_cpu变量current_task获取task_struct

  1. 获取current_task地址

    nm vmlinux | grep current_task
    

    current_task

  2. 获取task_struct指针

    获取当前正在运行的cpu所属线程:

    info threads
    

    current_task2

    从上图看到有8个cpu,可以改动qemu虚拟化时-smp 8参数调整cpu个数。

    (struct task_struct*)(*(unsigned long*)((char*)__per_cpu_offset[4] + 0x15cc0))
    

    __per_cpu_offset[4]中的下标4需要与info threads对应上。

    0x15cc0nm vmlinux | grep current_task获取的current_task地址。

  3. 获取进程号

    p ((struct task_struct*)(*(unsigned long*)((char*)__per_cpu_offset[4] + 0x15cc0)))->pid
    
  4. 获取进程名

    p ((struct task_struct*)(*(unsigned long*)((char*)__per_cpu_offset[4] + 0x15cc0)))->comm
    

    current_task3

    如果上述__per_cpu_offset[4]下标不对,获取的都是内核0号idle进程。

    current_task4

More


💬 评论