]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
parser: Populate pageframe structure completely
[scan-pagemap] / parse.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "parse.h"
7 #include "pagemap.h"
8
9 static struct maps *alloc_map(void)
10 {
11         struct maps *map;
12
13         map = malloc(sizeof *map);
14         if (map == NULL)
15                 goto err;
16
17         memset(map, 0, sizeof(*map));
18         INIT_LIST_HEAD(&map->list);
19 err:
20         return map;
21 }
22
23 static struct maps *parse_maps(FILE *file)
24 {
25         struct maps *the_map = NULL;
26         char line[1024];
27         int ret;
28
29         while(fgets(line, sizeof(line), file)) {
30                 struct maps *map = alloc_map();
31                 unsigned long start, end;
32                 char name[1024];
33
34                 if (map == NULL)
35                         return 0;
36
37                 if (the_map == NULL)
38                         the_map = map;
39
40                 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
41                              &start, &end, name);
42
43                 printf("%x, start %lx, end %lx, str: %s\n",
44                         ret, start, end, ret >= 3 ? name : "");
45
46                 if (ret < 2) {
47                         printf("Error reading input: %s\n", line);
48                         break;
49                 }
50
51                 map->start = start;
52                 map->end = end;
53                 map->size = end - start;
54
55                 if (ret >= 3)
56                         strncpy(map->name, name, sizeof(map->name));
57
58                 list_add_tail(&map->list, &the_map->list);
59         }
60
61         return the_map;
62 }
63
64 static void clear_pageframe(struct pageframe *pf)
65 {
66         memset(pf, 0, sizeof(*pf));
67 }
68
69 static struct pageframe *alloc_pageframe(void)
70 {
71         struct pageframe *pageframe;
72
73         pageframe = malloc(sizeof *pageframe);
74         if (pageframe == NULL)
75                 goto err;
76
77         clear_pageframe(pageframe);
78 err:
79         return pageframe;
80 }
81
82 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
83
84 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
85 {
86         pf->page_present = !!(BITRANGE(63, 63) & p);
87         pf->page_swapped = !!(BITRANGE(62, 62) & p);
88         pf->page_shift   =   (BITRANGE(55, 60) & p) >> 55;
89         pf->pfn          =   (BITRANGE(0, 54) & p);
90         pf->swap_type    =   (BITRANGE(0, 4) & p);
91         pf->swap_offset  =   (BITRANGE(5, 54) & p) >> 5;
92 #if 0
93         printf("pfn: %lx shift: %d present: %d swapped %d\n",
94                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
95 #endif
96 }
97
98 static int compare_pageframe(struct bintree *at, struct bintree *bt)
99 {
100         struct pageframe *a, *b;
101         a = tree_to_pageframe(at);
102         b = tree_to_pageframe(bt);
103
104         return a->pfn - b->pfn;
105 }
106
107 struct bintree_ops pageframe_ops = {
108         .compare = compare_pageframe,
109 };
110
111 /* Read data from the /proc/pid/pagemap file */
112 static int parse_pageframe(FILE *file, struct pageframe **pageframe_tree,
113                         struct maps *maps)
114 {
115         struct maps *map, *tmp;
116         struct pageframe *match, *pageframe = alloc_pageframe();
117         long start, len, i;
118         unsigned long long pf;
119         int ret, error;
120
121         /* Go through the list of allocated memory areas */
122         list_for_each_entry(map, &maps->list, list) {
123                 start = map->start >> (PAGE_SHIFT - 3);
124                 len = map->size >> (PAGE_SHIFT - 3);
125
126                 printf("start: %lx len %lx: name: %s\n",
127                         start, len, map->name);
128
129                 ret = fseek(file, start, SEEK_SET);
130                 if (ret) {
131                         error = errno;
132                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
133                                 strerror(error));
134                         continue;
135                         return -1;
136                 }
137
138                 for (i = 0; i < len; i++) {
139                         ret = fread(&pf, 1, sizeof(pf), file);
140                         if (ret != sizeof(pf)) {
141                                 error = errno;
142                                 fprintf(stderr, 
143                                         "Error reading %ld bytes, got %d: %s\n",
144                                         sizeof(pf), ret, strerror(errno));
145                                 continue;
146                                 return -1;
147                         }
148
149                         pageframe_to_struct(pf, pageframe);
150                         match = tree_to_pageframe(
151                                 bintree_add(&(*pageframe_tree)->tree,
152                                         &pageframe->tree, &pageframe_ops));
153
154                         if (*pageframe_tree == NULL)
155                                 *pageframe_tree = match;
156
157                         /*
158                          * Add a link from the physical page to this
159                          * process's page map
160                          */
161                         if (!match->maps) {
162                                 match->maps = alloc_map();
163                                 *match->maps = *map;
164                                 INIT_LIST_HEAD(&match->maps->list);
165                         } else {
166                                 tmp = alloc_map();
167                                 *tmp = *map;
168                                 list_add(&match->maps->list, &tmp->list);
169                         }
170                 }
171         }
172
173         return 0;
174 }
175
176 struct pageframe *read_pageframe(int pid, struct pageframe *pageframe)
177 {
178         struct maps *maps;
179         FILE *file;
180         int error;
181         char path[512];
182
183         snprintf(path, sizeof(path), "/proc/%d/maps", pid);
184         file = fopen(path, "rb");
185
186         if (!file)
187                 goto err_out;
188
189         maps = parse_maps(file);
190
191         snprintf(path, sizeof(path), "/proc/%d/pagemap", pid);
192         file = fopen(path, "rb");
193
194         if (!file)
195                 goto err_out;
196
197         parse_pageframe(file, &pageframe, maps);
198
199         return pageframe;
200 err_out:
201         error = errno;
202         fprintf(stderr, "Failed to open file %s: %s\n", path, strerror(error));
203
204         return NULL;
205 }