如何查看栈、数据、堆、代码等内存段的起始地址和结束地址?

ren*_*ack 2 linux memory

由于内存段是 Linux 操作系统的一部分,是否可以使用简单的 C 程序或任何 Linux 命令来查看它们各自的地址。

Mar*_*lli 5

如果您编写了一个程序,并且希望在运行时查看其内存映射,您可以在类似 的调试器中运行它,gdb然后启动它,并使用命令查看内存映射info proc mappings,输出将如下所示:

(gdb) info proc mappings
process 6520
Mapped address spaces:

    Start Addr   End Addr       Size     Offset objfile
       0x10000    0x15000     0x5000        0x0 /bin/true
       0x24000    0x25000     0x1000     0x4000 /bin/true
       0x25000    0x26000     0x1000     0x5000 /bin/true
    0x76e6e000 0x76f98000   0x12a000        0x0 /lib/arm-linux-gnueabihf/libc-2.24.so
    ... etc ...
    0x7efdf000 0x7f000000    0x21000        0x0 [stack]
    0xffff0000 0xffff1000     0x1000        0x0 [vectors]
Run Code Online (Sandbox Code Playgroud)

如果您想查看已运行进程的映射,假设您具有正确的权限,则可以执行cat /proc/<pid>/maps。或者,您可以附加到该流程并gdb -p <pid>执行我上面解释的操作。

  • 不存在“整个操作系统堆栈”这样的东西。每个进程都有自己的堆栈映射在其虚拟内存空间中。 (2认同)