Linux的pagemap技术,How to decode /proc/pid/pagemap entries in Linux?

Linux kernel documentation

Linux kernel doc describing the format: https://github.com/torvalds/linux/blob/v4.9/Documentation/vm/pagemap.txt

* 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 parser function

GitHub upstream.

#define _XOPEN_SOURCE 700

#include /* open */

#include /* uint64_t */

#include /* size_t */

#include /* 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;

}

Example runnable programs using it:

convert one virtual address to physical: Is there any API for determining the physical address from virtual address in Linux?

dump information about all pages of a process: /proc/[pid]/pagemaps and /proc/[pid]/maps | linux

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值