gpt4 book ai didi

c - 关于/proc/xx/map 和 vm_area_struct

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:01 25 4
gpt4 key购买 nike

内核模块代码:

static int __init module(void)
{
struct pid *current_pid;
struct task_struct *current_task;
struct mm_struct *mymm;
struct vm_area_struct *pos = NULL;

current_pid = find_vpid(7887);
current_task = pid_task(current_pid, PIDTYPE_PID);
mymm = current_task->mm;
for(pos = mymm->mmap; pos; pos = pos->vm_next) {
printk("0x%hx-0x%hx\t%ld\t", pos->vm_start, pos->vm_end,pos->rb_subtree_gap);
if(pos->vm_flags & VM_READ) {
printk("r");
} else {
printk("-");
}
if(pos->vm_flags & VM_WRITE) {
printk("w");
} else {
printk("-");
}
if(pos->vm_flags & VM_EXEC) {
printk("x");
} else {
printk("-");
}

printk("\n");
}
return 0;
}

static void __exit rmodule(void)
{
printk(KERN_ALERT"Goodbye,world\n");
}

module_init(module);
module_exit(rmodule);

结果:

Oct  9 18:57:43 shui kernel: [14625.861352] 0x400000-0x401000   4194304 r-x
Oct 9 18:57:43 shui kernel: [14625.861366] 0x600000-0x601000 273183399936 r--
Oct 9 18:57:43 shui kernel: [14625.861373] 0x601000-0x602000 0 rw-
Oct 9 18:57:43 shui kernel: [14625.861379] 0x3f9b600000-0x3f9b620000 273183399936 r-x
Oct 9 18:57:43 shui kernel: [14625.861386] 0x3f9b820000-0x3f9b821000 140214666838016 r--
Oct 9 18:57:43 shui kernel: [14625.861391] 0x3f9b821000-0x3f9b822000 0 rw-
Oct 9 18:57:43 shui kernel: [14625.861397] 0x3f9b822000-0x3f9b823000 0 rw-
Oct 9 18:57:43 shui kernel: [14625.861403] 0x3f9ba00000-0x3f9bbad000 1953792 r-x
Oct 9 18:57:43 shui kernel: [14625.861408] 0x3f9bbad000-0x3f9bdad000 0 ---
Oct 9 18:57:43 shui kernel: [14625.861414] 0x3f9bdad000-0x3f9bdb1000 140214666838016 r--
Oct 9 18:57:43 shui kernel: [14625.861419] 0x3f9bdb1000-0x3f9bdb3000 0 rw-
Oct 9 18:57:43 shui kernel: [14625.861425] 0x3f9bdb3000-0x3f9bdb8000 140214666838016 rw-
Oct 9 18:57:43 shui kernel: [14625.861431] 0x7fc5e1444000-0x7fc5e1447000 140214666838016 rw-
Oct 9 18:57:43 shui kernel: [14625.861436] 0x7fc5e146f000-0x7fc5e1470000 140214666838016 rw-
Oct 9 18:57:43 shui kernel: [14625.861442] 0x7fffe4c32000-0x7fffe4c54000 249166569472 rw-
Oct 9 18:57:43 shui kernel: [14625.861448] 0x7fffe4d79000-0x7fffe4d7b000 1200128 r-x

但使用 cat/proc/7887/maps:

shui @ kernel @ 19:00]$ cat /proc/7887/maps 
00400000-00401000 r-xp 00000000 00:20 1826951 /tmp/a.out

00600000-00601000 r--p 00000000 00:20 1826951 /tmp/a.out

00601000-00602000 rw-p 00001000 00:20 1826951 /tmp/a.out

3f9b600000-3f9b620000 r-xp 00000000 fd:01 2228400 /usr/lib64
/ld-2.16.so

3f9b820000-3f9b821000 r--p 00020000 fd:01 2228400 /usr/lib64/ld-2.16.so

3f9b821000-3f9b822000 rw-p 00021000 fd:01 2228400 /usr/lib64/ld-2.16.so

3f9b822000-3f9b823000 rw-p 00000000 00:00 0

3f9ba00000-3f9bbad000 r-xp 00000000 fd:01 2228403 /usr/lib64/libc-2.16.so

3f9bbad000-3f9bdad000 ---p 001ad000 fd:01 2228403 /usr/lib64/libc-2.16.so

3f9bdad000-3f9bdb1000 r--p 001ad000 fd:01 2228403 /usr/lib64/libc-2.16.so

3f9bdb1000-3f9bdb3000 rw-p 001b1000 fd:01 2228403 /usr/lib64/libc-2.16.so

3f9bdb3000-3f9bdb8000 rw-p 00000000 00:00 0

7fc5e1444000-7fc5e1447000 rw-p 00000000 00:00 0

7fc5e146f000-7fc5e1470000 rw-p 00000000 00:00 0

7fffe4c33000-7fffe4c54000 rw-p 00000000 00:00 0 [stack]

7fffe4d79000-7fffe4d7b000 r-xp 00000000 00:00 0 [vdso]

ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

关于堆栈地址的vm_area_struct是7fffe4c32000,但是映射是7fffe4c33000,这有什么不同? vsyscall 是为什么不在 vm_area_struct 上打印?

最佳答案

这是正常的,Linux 内核使用堆栈保护,procfs 不显示页面。

查看fs/proc/task_mmu.c中的函数show_map_vma

关于c - 关于/proc/xx/map 和 vm_area_struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com