/proc/[pid]/pagemaps和/proc/[pid]/地图| Linux.[英] /proc/[pid]/pagemaps and /proc/[pid]/maps | linux

本文是小编为大家收集整理的关于/proc/[pid]/pagemaps和/proc/[pid]/地图| Linux.的处理方法,想解了/proc/[pid]/pagemaps和/proc/[pid]/地图| Linux.的问题怎么解决?/proc/[pid]/pagemaps和/proc/[pid]/地图| Linux.问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正试图围绕着名称中提到的两个文件. 我抬起了什么比特;但是,我未能了解如何从它们中提取有用的信息(或者我只是以错误的方式接近它).

让我解释:Pagemaps是一个相当更新的"功能"伪文件,其包含分配给当前[PID]的虚拟页面的物理帧信息.也就是说,给定以地址x开始的虚拟页面,为虚拟地址启动说'VAS',我可以使用VAS索引Pagemap文件来获取映射物理页面帧的64bits.这些位包含有关该虚拟页面的信息. 但是,当我提取比特并做一些转移时,我迷路了我所看到的.

比特表示如下:0-54是页面帧编号,55-60是页移,第63位是当前位,还有其他对我有何兴趣. 在我完成使用来自/proc/[pid]/地图的VAS地址的一点映射之后,似乎只有每个进程的页面被交换,即第63位始终为零. :(

我猜这个问题是,我应该如何有效地使用Pagemaps来获得/proc/[pid]/maps给出的地址的等效物理地址

是公平的,我发布了类似的问题,但这条方法几天前有点不同.

如果有人能够在这件事上揭示一些我会非常感谢.

===编辑===

解决以下评论: 我正在阅读来自/proc/[pid]/地图的行,行看起来像:

00400000-00401000 r-xp 00000000 08:01 8915461/home/janjust/my_programs/shared_mem 7FFFFF1B000-7FFFFEF3C000 RW-P 00000000 00:00 0 [堆叠]

然后我正在提取它触摸的虚拟页数并索引二进制文件/proc/[pid]/pagemaps,以及每个虚拟页面,我可以提取它分配给的物理页面.

输出看起来如:

00400000-00401000 r-xp 00000000 08:01 8915461/home/janjust/my_programs/shared_mem num_pages:1 :86000000001464C6

虚拟范围中的每个虚拟页面的一个物理地址.

用于读取线路并提取物理地址的代码是:

74     /* process /proc/pid/maps, by line*/
75     while(fgets(line, 256, in_map) != NULL){
76         unsigned long vas;
77         unsigned long vae;
78         int num_pages;
79 
80         //print line
81         printf("%s", line);
82 
83         /*scan for the virtual addresses*/
84         n = sscanf(line, "%lX-%lX", &vas, &vae);
85         if(n != 2){
86             printf("Involid line read from %s\n",maps);
87             continue;
88         }
89 
90         num_pages = (vae - vas) / PAGE_SIZE;
91         printf("num_pages: %d\n", num_pages);
92 
93         if(num_pages > 0){
94             long index  = (vas / PAGE_SIZE) * sizeof(unsigned long long);
95             off64_t o;
96             ssize_t t;
97 
98             /* seek to index in pagemaps */
99             o = lseek64(pm, index, SEEK_SET);
100             if (o != index){
101                 printf("Error seeking to o:%ld, index:%ld.\n", o, index);
102             }
103 
104             /* map the virtual to physical page */
105             while(num_pages > 0){
106                 unsigned long long pa;
107 
108                 /* Read a 64-bit word from each pagemap file... */
109                 t = read(pm, &pa, sizeof(unsigned long long));
110                 if(t < 0){
111                     printf("Error reading file \"%s\" \n", page_map);
112                     goto next_line;
113                 }
114                 printf(": %016llX\n", pa);
但是,虽然我认为我得到了正确的输出,但索引似乎是一种类型不匹配或其他事情正在进行中: 输出例如用于地图中的[共享MEM]行给出了错误的索引;然而,我仍然能够扫描二进制文件并获取物理页面地址.

该输出的示例低于:

969 7f7f08d58000-7f7f08d59000 rw-s 00000000 00:04 0    /SYSV00003039 (deleted)
970 num_pages: 1
971 Error seeking to o:-1081840960, index:273796065984.
972 : 8600000000148267

好的,现在,最后,我应该说这是在64位操作系统下,这个问题在32位操作系统中不会持续存在.

推荐答案

oOOh k,索引是正确的,但是比较64_t o(8bytes),长索引正在解释错误,因此为什么我正在得到那个错误. 哈!这是一个愚蠢的错误. 因此,添加适当的标题照顾.

缺失标题: -/ sigh 修复了与无符号长的64_t相比的问题.

其他推荐答案

f ** /proc/<pid>/pagemap + /proc/<pid>/maps转储示例程序**

这是一个pagemap将虚拟地址转换为物理:有没有用于确定Linux中的虚拟地址的物理地址的API?

以下程序使用/proc/<pid>/pagemap + /proc/<pid>/maps来转储页面表信息,以显示如何一起使用它们.用法:

sudo ./pagemap_dump.out <pid>

样品输出:

addr pfn soft-dirty file/shared swapped present library
400000 12845d 0 1 0 1 /bin/bash
401000 12845e 0 1 0 1 /bin/bash
402000 12845f 0 1 0 1 /bin/bash

这告诉我们例如虚拟地址0x400000映射到物理地址 0x12845d000.

为什么需要sudo: https://unix.stackexchange.com/questions/345915/how-to-change-permission-file/383838次>

此程序有两步起作用:

  • 从/proc/<pid>/maps解析人类可读线线.此文件包含表单行:

    7ffff7b6d000-7ffff7bdd000 r-xp 00000000 fe:00 658                        /lib/libuClibc-1.0.22.so
    

    给我们:

    • 7f8af99f8000-7f8af99ff000:属于过程的虚拟地址范围,可能包含多个页面.
    • /lib/libuClibc-1.0.22.so拥有该内存的库的名称.
  • 循环在每个地址范围的每个页面上,并询问/proc/<pid>/pagemap有关该页面的更多信息,包括物理地址.

pagemap_dump.c

#define _XOPEN_SOURCE 700
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct {
    uint64_t pfn : 55;
    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 << 55) - 1);
    entry->soft_dirty = (data >> 55) & 1;
    entry->file_page = (data >> 61) & 1;
    entry->swapped = (data >> 62) & 1;
    entry->present = (data >> 63) & 1;
    return 0;
}

/* Convert the given virtual address to physical using /proc/PID/pagemap.
 *
 * @param[out] paddr physical address
 * @param[in]  pid   process to convert for
 * @param[in] vaddr virtual address to get entry for
 * @return 0 for success, 1 for failure
 */
int virt_to_phys_user(uintptr_t *paddr, pid_t pid, uintptr_t vaddr)
{
    char pagemap_file[BUFSIZ];
    int pagemap_fd;

    snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t)pid);
    pagemap_fd = open(pagemap_file, O_RDONLY);
    if (pagemap_fd < 0) {
        return 1;
    }
    PagemapEntry entry;
    if (pagemap_get_entry(&entry, pagemap_fd, vaddr)) {
        return 1;
    }
    close(pagemap_fd);
    *paddr = (entry.pfn * sysconf(_SC_PAGE_SIZE)) + (vaddr % sysconf(_SC_PAGE_SIZE));
    return 0;
}

int main(int argc, char **argv)
{
    char buffer[BUFSIZ];
    char maps_file[BUFSIZ];
    char pagemap_file[BUFSIZ];
    int maps_fd;
    int offset = 0;
    int pagemap_fd;
    pid_t pid;

    if (argc < 2) {
        printf("Usage: %s pid\n", argv[0]);
        return EXIT_FAILURE;
    }
    pid = strtoull(argv[1], NULL, 0);
    snprintf(maps_file, sizeof(maps_file), "/proc/%ju/maps", (uintmax_t)pid);
    snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t)pid);
    maps_fd = open(maps_file, O_RDONLY);
    if (maps_fd < 0) {
        perror("open maps");
        return EXIT_FAILURE;
    }
    pagemap_fd = open(pagemap_file, O_RDONLY);
    if (pagemap_fd < 0) {
        perror("open pagemap");
        return EXIT_FAILURE;
    }
    printf("addr pfn soft-dirty file/shared swapped present library\n");
    for (;;) {
        ssize_t length = read(maps_fd, buffer + offset, sizeof buffer - offset);
        if (length <= 0) break;
        length += offset;
        for (size_t i = offset; i < (size_t)length; i++) {
            uintptr_t low = 0, high = 0;
            if (buffer[i] == '\n' && i) {
                const char *lib_name;
                size_t y;
                /* Parse a line from maps. Each line contains a range that contains many pages. */
                {
                    size_t x = i - 1;
                    while (x && buffer[x] != '\n') x--;
                    if (buffer[x] == '\n') x++;
                    while (buffer[x] != '-' && x < sizeof buffer) {
                        char c = buffer[x++];
                        low *= 16;
                        if (c >= '0' && c <= '9') {
                            low += c - '0';
                        } else if (c >= 'a' && c <= 'f') {
                            low += c - 'a' + 10;
                        } else {
                            break;
                        }
                    }
                    while (buffer[x] != '-' && x < sizeof buffer) x++;
                    if (buffer[x] == '-') x++;
                    while (buffer[x] != ' ' && x < sizeof buffer) {
                        char c = buffer[x++];
                        high *= 16;
                        if (c >= '0' && c <= '9') {
                            high += c - '0';
                        } else if (c >= 'a' && c <= 'f') {
                            high += c - 'a' + 10;
                        } else {
                            break;
                        }
                    }
                    lib_name = 0;
                    for (int field = 0; field < 4; field++) {
                        x++;
                        while(buffer[x] != ' ' && x < sizeof buffer) x++;
                    }
                    while (buffer[x] == ' ' && x < sizeof buffer) x++;
                    y = x;
                    while (buffer[y] != '\n' && y < sizeof buffer) y++;
                    buffer[y] = 0;
                    lib_name = buffer + x;
                }
                /* Get info about all pages in this page range with pagemap. */
                {
                    PagemapEntry entry;
                    for (uintptr_t addr = low; addr < high; addr += sysconf(_SC_PAGE_SIZE)) {
                        /* TODO always fails for the last page (vsyscall), why? pread returns 0. */
                        if (!pagemap_get_entry(&entry, pagemap_fd, addr)) {
                            printf("%jx %jx %u %u %u %u %s\n",
                                (uintmax_t)addr,
                                (uintmax_t)entry.pfn,
                                entry.soft_dirty,
                                entry.file_page,
                                entry.swapped,
                                entry.present,
                                lib_name
                            );
                        }
                    }
                }
                buffer[y] = '\n';
            }
        }
    }
    close(maps_fd);
    close(pagemap_fd);
    return EXIT_SUCCESS;
}

其他推荐答案

使用page-types.c作为您要查找的指南,它会解析Pagemap和Maps的内容: https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/plain/documentation/vm/page-types.cnc =linux -2.6.32.y

本文地址:https://www.itbaoku.cn/post/359029.html