/proc/[pid]/pagemaps 和/proc/[pid]/maps |linux [英] /proc/[pid]/pagemaps and /proc/[pid]/maps | linux

查看:37
本文介绍了/proc/[pid]/pagemaps 和/proc/[pid]/maps |linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解标题中提到的两个文件.我查了一下这些位是什么;然而,我无法理解如何从他们那里提取有用的信息(或者我只是以错误的方式接近它).

I'm trying to get my head around the two files mentioned in the title. I've looked up what the bits are; however, I'm failing to understand how to extract useful info from them (or I'm simply approaching it the wrong way).

让我解释一下:页面映射是一个相当新的功能"伪文件,其中包含分配给当前 [pid] 的虚拟页面的物理帧信息.也就是说,给定一个从地址 x 开始的虚拟页面,比如虚拟地址开始的vas",我可以使用 vas 索引页面映射文件以获得映射的物理页面框架的 64 位.这些位包含有关该虚拟页面的信息.然而,当我提取这些位并进行一些移位时,我会迷失于我所看到的.

Let me explain: The pagemaps is a rather newer "feature" pseudo file that contains the physical frame information of virtual pages assigned to a current [pid]. That is, given a virtual page that starts at address x, say 'vas' for virtual address start, i can index the pagemap file using vas to get the 64bits of the mapped physical page frame. These bits contain info about that virtual page. However, when I extract the bits and do a bit of shifting I'm getting lost with what I'm seeing.

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

The bits are represented as follows: 0-54 is the page frame number, 55-60 is the page shift, 63rd bit is the present bit, there are other bits of little interest to me. After I do a bit of mapping using vas addresses from /proc/[pid]/maps, it seems that just about every process' page is swapped, i.e. the 63rd bit is always a zero. :(

我想问题是,我应该如何有效地使用页面映射来获取/proc/[pid]/maps 给出的地址的等效物理地址

I guess the question would be, how should I go about effectively using pagemaps to get the equivalent physical address of the address given by /proc/[pid]/maps

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

To be fair, I've posted a similar question but the approach was a bit different a few days earlier.

如果有人能对此事有所了解,我将不胜感激.

If anyone can shed some light on this matter I would be greatly appreciative.

====编辑===

解决以下评论:我正在从/proc/[pid]/maps 中读取一行,这些行看起来像:

To address the comment below: I'm reading a line from /proc/[pid]/maps and the lines look like:

00400000-00401000 r-xp 00000000 08:01 8915461/home/janjust/my_programs/shared_mem7ffffef1b000-7ffffef3c000 rw-p 00000000 00:00 0 [堆栈]

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 ,对于每个虚拟页面,我可以提取它分配给的物理页面.

Then I'm extracting the number of virtual pages it touches and indexing a binary file /proc/[pid]/pagemaps , and for each virtual page I can extract the physical page it is assigned to.

输出如下:

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

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

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

One physical address for each virtual page in the virtual range.

读取行并提取物理地址的代码为:

The code for reading the line and extracting the physical address is:

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
",maps);
87             continue;
88         }
89 
90         num_pages = (vae - vas) / PAGE_SIZE;
91         printf("num_pages: %d
", 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.
", 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" 
", page_map);
112                     goto next_line;
113                 }
114                 printf(": %016llX
", pa);

然而,虽然我认为我得到了正确的输出,但索引似乎是类型不匹配或其他原因:例如,地图中 [shared mem] 行的输出给出了错误的索引;但我仍然能够扫描二进制文件并获取物理页面地址.

However, although I think I'm getting the right output, the index seems to be either a type mismatch or something else is going on: The output say for instance for the [shared mem] line in maps gives a wrong index; yet I'm still able to scan through the binary file and get the physical page address.

该输出的示例如下:

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 位操作系统中不存在.

Ok, now, lastly I should say that this is under a 64bit OS, and this problem doesn't persist in a 32bit OS.

推荐答案

Oooh K,索引是正确的,但是将 off64_t o (8bytes) 与 long index 进行比较时解释错误,因此为什么我会收到该错误.哈!这是一个愚蠢的错误.因此,添加适当的标头即可解决这一问题.

Oooh K, the index was correct but comparing off64_t o (8bytes) with long index was interpreting o wrong hence why I was getting that error. Ha! this was a stupid mistake. So adding the appropriate header took care of that.

缺少标头 :-/sigh 修复了将 off64_t 与 unsigned long 进行比较的问题.

Missing header :-/ sigh fixes the issue of comparing off64_t with a unsigned long.

这篇关于/proc/[pid]/pagemaps 和/proc/[pid]/maps |linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆