/ proc / [pid] / pagemaps和/ proc / [pid] / maps | Linux的

时间:2011-06-08 20:25:00

标签: c linux memory-management linux-kernel

我正试图了解标题中提到的两个文件。 我抬头看了些什么东西;但是,我无法理解如何从中提取有用的信息(或者我只是以错误的方式接近它)。

让我解释一下:页面地图是一个相当新的“特征”伪文件,它包含分配给当前[pid]的虚拟页面的物理帧信息。也就是说,给定一个从地址x开始的虚拟页面,比如用于虚拟地址开始的'vas',我可以使用vas索引页面映射文件以获得映射的物理页面帧的64位。这些位包含有关该虚拟页面的信息。 然而,当我提取这些位并进行一些移动时,我迷失了我所看到的东西。

这些位表示如下:0-54是页面帧号,55-60是页面移位,第63位是当前位,还有其他位对我不感兴趣。 在使用来自/ proc / [pid] / maps的vas地址进行一些映射之后,似乎几乎每个进程的页面都被交换,即第63位始终为零。 :(

我想问题是,如何有效地使用页面地图获取/ proc / [pid] / maps

给出的地址的等效物理地址

公平地说,我发布了一个类似的问题,但几天之前的方法有点不同。

如果有人能对这件事情有所了解,我将非常感激。

=== EDIT ===

要解决以下评论: 我正在从/ proc / [pid] / maps读取一行,其行如下:

  

00400000-00401000 r-xp 00000000 08:01 8915461 / home / janjust / my_programs / shared_mem   7ffffef1b000-7ffffef3c000 rw-p 00000000 00:00 0 [stack]

然后我将提取它接触的虚拟页面数并索引二进制文件/ 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);

然而,虽然我认为我得到了正确的输出,但索引似乎是类型不匹配或其他正在发生的事情: 例如,对于map中的[shared 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位操作系统中持续存在。

3 个答案:

答案 0 :(得分:3)

Oooh K,索引是正确的,但是将off64_t o(8bytes)与长索引进行比较是在解释o错误因此我得到了那个错误。 哈!这是一个愚蠢的错误。 因此,添加适当的标题可以解决这个问题。

缺少标题: - / 叹息修复了将off64_t与无符号长整数进行比较的问题。

答案 1 :(得分:3)

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

以下是一个pagemap示例,它将虚拟地址转换为物理地址:Is there any API for determining the physical address from virtual address in Linux

以下程序使用/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

为什么需要sudohttps://unix.stackexchange.com/questions/345915/how-to-change-permission-of-proc-self-pagemap-file/383838#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 : 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, &data, sizeof(data),
                (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;
}

/* 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;
}

答案 2 :(得分:1)

使用page-types.c作为您所寻找内容的指南,它解析了pagemap和maps的内容: https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/plain/Documentation/vm/page-types.c?h=linux-2.6.32.y