[英]VirtualQueryEx alternative for Linux - how to get virtual memory range of another process
I went through this blog and this videocast . 我浏览了这个博客和该视频广播 。 In Windows if I want to retrieve information about a range of pages within the virtual address space of a specified process, I can use WinAPI
VirtualQueryEx
method : 在Windows中,如果我想检索有关指定进程的虚拟地址空间内的一系列页面的信息,可以使用WinAPI
VirtualQueryEx
方法 :
MEMORY_BASIC_INFORMATION meminfo;
unsigned char *addr = 0;
for(;;)
{
if(!VirtualQueryEx(hProc, addr, &meminfo, sizeof(meminfo)))
break;
if(meminfo.State & MEM_COMMIT)
{
//collect some data from meminfo
}
addr = (unsigned char*)meminfo.BaseAddress + meminfo.RegionSize;
}
I wondered how to get similar set of information in Linux using syscalls, but it is not clear for me how using C/C++ can I gather such a data under Linux. 我想知道如何在Linux中使用syscalls获得类似的信息,但是对于我来说,目前尚不清楚如何在Linux下使用C / C ++收集此类数据。 I went through this thread when there are suggestions to take a look at
/proc/<pid>/mem
or /proc/<pid>/maps
files. 当有人建议查看
/proc/<pid>/mem
或/proc/<pid>/maps
文件时,我经历了此线程 。 Is it the good direction? 这是个好方向吗? How should look the closest implementation to this one provided here, but for Linux?
对于Linux,应该怎么看最接近此处提供的实现?
Yes, the proc
filesystem is part of the Linux API, so this is the way to go. 是的,
proc
文件系统是Linux API的一部分,因此这是必须的方法。 A lot of data in that filesystem is usually accessed using a library wrapper, but that's where the data lie. 通常使用库包装器访问该文件系统中的许多数据,但这就是数据所在的地方。
As far as I know /proc/<pid>/maps
is the only reliable and supported way to do it. 据我所知,
/proc/<pid>/maps
是唯一可靠且受支持的方法。 Even libunwind
is using it : 甚至
libunwind
也正在使用它 :
if (maps_init (&mi, getpid()) < 0)
return -1;
unsigned long offset;
while (maps_next (&mi, &low, &hi, &offset)) {
struct dl_phdr_info info;
info.dlpi_name = mi.path;
info.dlpi_addr = low;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.