[英]Start and end address in virtual memory of a process
I have a C program I have written and I am attempting to make a memory map of it. 我有一个已编写的C程序,正在尝试对其进行内存映射。
How do I print to the screen the starting address and end address (within virtual memory) of the environment of the process. 如何将进程环境的开始地址和结束地址(在虚拟内存中)打印到屏幕上。
The program is and always will be run on linux, not sure if that makes a difference, but it doesn't need any kind of portability. 该程序将始终在Linux上运行,不确定是否会有所不同,但是它不需要任何可移植性。
I am looking for something that would preferably just give me a hexadecimal address of the start and end. 我正在寻找的东西最好只是给我一个开始和结束的十六进制地址。
Take a look at this example in C programming. 看一下C编程中的这个例子。
user@linux:~$ cat hello.c
#include<stdio.h>
int main()
{
printf("Hello World\n");
getchar();
}
user@linux:~$ gcc hello.c -o hello
user@linux:~$ ./hello
Hello World
Open another console to find out the process id (pid). 打开另一个控制台以查找进程ID(pid)。 Run
ps
command as follows; 运行
ps
命令,如下所示; Change hello
with your program name. 用您的程序名称更改
hello
。
In this case, the process id (pid) is 2011
在这种情况下,进程ID(pid)为
2011
user@linux:~$ ps -ef | grep hello | grep -v grep
user 2011 1864 0 17:21 pts/2 00:00:00 ./hello
user@linux:~$
Then, check your /proc/[pid]/maps
file with cat
command 然后,使用
cat
命令检查/proc/[pid]/maps
文件
user@linux:~$ cat /proc/2011/maps | grep stack
bffeb000-c0000000 rw-p 00000000 00:00 0 [stack]
user@linux:~$
Alternatively, you can also use pmap
command 或者,您也可以使用
pmap
命令
user@linux:~$ pmap 2011 | grep stack
bffeb000 84K rw--- [ stack ]
user@linux:~$
In both cases, we can see that stack start at address bffeb000
在这两种情况下,我们都可以看到堆栈从地址
bffeb000
开始
As per your requirement i think you can get the memory map of a particular process if you know its PID. 根据您的要求,如果您知道特定进程的PID,我认为您可以获取该进程的内存映射。 Use the command " pmap " for more info http://linux.die.net/man/1/pmap .
使用命令“ pmap ”获取更多信息http://linux.die.net/man/1/pmap 。
& if u want to print it in ur program then try to get pid with the help of function getpid() ; &如果您想在程序中打印它,请尝试使用getpid()函数获取pid; and then use system() call to execute the pmap command.
然后使用system()调用执行pmap命令。
您可以简单地打印出/proc/[PID]/maps
文件的内容,其中[PID]
是您的进程的进程标识符的值(调用getpid()
找出该值是什么)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.