简体   繁体   English

Linux的VirtualQueryEx替代品-如何获取另一个进程的虚拟内存范围

[英]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.

相关问题 Linux中单个进程的总/可用虚拟内存 - Total/Free virtual memory for a single process in Linux 如何在虚拟内存 - Linux 中查看每个进程维护的页表? - How can I see a page-table maintained by each process in Virtual Memory - Linux? 如何在没有root权限的情况下获取linux中进程的内存使用情况 - How to get the memory usage of a process in linux without root permission 在程序中获取AIX中进程的虚拟内存大小 - get virtual memory size of process in AIX in program 如何将C / C ++格式字符串中的填充存储在Linux的虚拟内存中? - How do paddings in format string of C/C++ get stored in virtual memory of Linux? 如何使用C / C ++系统调用获取Linux中进程的堆内存的当前大小? - How do I get the current size of the heap memory of a process in Linux using C/C++ system calls? 有什么方法可以确定一个(多)线程/任务的堆栈地址在 Linux 上的进程的虚拟 memory 中开始? - Is there any way to determine a (multi) thread/task's stack address start in the virtual memory of a process on Linux? 如何更改另一个进程的内存空间中的值 - How to change a value in memory space of another process 调试器如何窥视另一个进程的内存? - How does a debugger peek into another process' memory? Linux进程分配的内存使用情况 - Linux process allocated memory usage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM