gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:55:11 25 4
gpt4 key购买 nike

我正在尝试破译如何使用/proc/pid/pagemap 来获取给定页面集的物理地址。假设从/proc/pid/maps 中,我得到了与堆对应的虚拟地址 afa2d000-afa42000。我的问题是如何使用此信息遍历页面映射文件并找到与地址 afa2d000-afa42000 对应的物理页面框架。

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

最佳答案

Linux 内核文档

描述格式的 Linux 内核文档: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 解析器函数

GitHub upstream .

#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;
}

使用它的示例可运行程序:

关于linux - 如何在 Linux 中解码/proc/pid/pagemap 条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17021214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com