首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Linux中解码/proc/pid/pagemap条目?

如何在Linux中解码/proc/pid/pagemap条目?
EN

Stack Overflow用户
提问于 2013-06-10 09:57:54
回答 4查看 14.6K关注 0票数 18

我试图破译如何使用/proc/pid/pagemap来获取给定一组页面的物理地址。假设从/proc/pid/map中得到与堆对应的虚拟地址afa2d000afa42000。我的问题是如何使用此信息遍历页面文件并找到与地址afa2d000afa42000相对应的物理页帧。

/proc/pid/pagemap条目采用二进制格式。有什么工具可以帮助解析这个文件吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-02 06:45:12

试试这个http://www.eqware.net/Articles/CapturingProcessMemoryUsageUnderLinux/,它可以为您解析页面地图,例如,如果您感兴趣的虚拟地址是堆,它是0x055468:=0004c0000005a000 rw-p 00000000 00:00 :00堆: 86000000000FD6D6 : 0600000000000000。

:0600000000000000

:86000000000FE921

:86000000000FE922

:0600000000000000

:86000000000FD5AD

:86000000000FD6D4

:86000000000FD5F8

:86000000000FD5FA =>9th

假设页面大小为4KB,并且(0x055468 - 0x4c000) mod 4K = 9,那么页面的页帧号是第9页帧==>:86000000000FD5FA,因此物理pfn是0xFD5FA000 (取最后55位和55次页大小)加上偏移量:( 0x055468 -0x4c0009*4K)= 0x468 ==>物理附件为0xFD5FA000 + 0x468 =0xFD5468 take。

票数 2
EN

Stack Overflow用户

发布于 2017-07-16 07:29:27

Linux内核文档

描述格式的Linux内核文档:https://github.com/torvalds/linux/blob/v4.9/Documentation/vm/pagemap.txt

代码语言:javascript
运行
复制
* Bits 0-54  page frame number (PFN) if present
* Bits 0-4   swap type if swapped
* Bits 5-54  swap offset if swapped
* Bit  55    pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
* Bit  56    page exclusively mapped (since 4.2)
* Bits 57-60 zero
* Bit  61    page is file-page or shared-anon (since 3.5)
* Bit  62    page swapped
* Bit  63    page present

C解析器函数

GitHub上游

代码语言:javascript
运行
复制
#define _XOPEN_SOURCE 700
#include <fcntl.h> /* open */
#include <stdint.h> /* uint64_t  */
#include <stdlib.h> /* size_t */
#include <unistd.h> /* pread, sysconf */

typedef struct {
    uint64_t pfn : 54;
    unsigned int soft_dirty : 1;
    unsigned int file_page : 1;
    unsigned int swapped : 1;
    unsigned int present : 1;
} PagemapEntry;

/* Parse the pagemap entry for the given virtual address.
 *
 * @param[out] entry      the parsed entry
 * @param[in]  pagemap_fd file descriptor to an open /proc/pid/pagemap file
 * @param[in]  vaddr      virtual address to get entry for
 * @return 0 for success, 1 for failure
 */
int pagemap_get_entry(PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
{
    size_t nread;
    ssize_t ret;
    uint64_t data;

    nread = 0;
    while (nread < sizeof(data)) {
        ret = pread(pagemap_fd, ((uint8_t*)&data) + nread, sizeof(data) - nread,
                (vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread);
        nread += ret;
        if (ret <= 0) {
            return 1;
        }
    }
    entry->pfn = data & (((uint64_t)1 << 54) - 1);
    entry->soft_dirty = (data >> 54) & 1;
    entry->file_page = (data >> 61) & 1;
    entry->swapped = (data >> 62) & 1;
    entry->present = (data >> 63) & 1;
    return 0;
}

使用它运行的程序示例:

票数 9
EN

Stack Overflow用户

发布于 2014-03-01 08:48:30

我希望这个联系会有所帮助。这是一个非常简单的工具,确定您需要访问的地址非常简单:http://fivelinesofcode.blogspot.com/2014/03/how-to-translate-virtual-to-physical.html

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17021214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档