]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
Initial commit
[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
91         printf("pfn: %lx shift: %x present: %d swapped %d\n",
92                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
93 }
94
95 static int compare_pageframe(struct bintree *at, struct bintree *bt)
96 {
97         struct pageframe *a, *b;
98         a = tree_to_pageframe(at);
99         b = tree_to_pageframe(bt);
100
101         return a->pfn - b->pfn;
102 }
103
104 struct bintree_ops pageframe_ops = {
105         .compare = compare_pageframe,
106 };
107
108 /* Read data from the /proc/pid/pagemap file */
109 static int parse_pageframe(FILE *file, struct pageframe **pageframe_tree,
110                         struct maps *maps)
111 {
112         struct maps *map, *tmp;
113         struct pageframe *match, *pageframe = alloc_pageframe();
114         long start, len, i;
115         unsigned long long pf;
116         int ret, error;
117
118         /* Go through the list of allocated memory areas */
119         list_for_each_entry(map, &maps->list, list) {
120                 start = map->start >> (PAGE_SHIFT - 3);
121                 len = map->size >> (PAGE_SHIFT - 3);
122
123                 printf("start: %lx len %lx: name: %s\n",
124                         start, len, map->name);
125
126                 ret = fseek(file, start, SEEK_SET);
127                 if (ret) {
128                         error = errno;
129                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
130                                 strerror(error));
131                         continue;
132                         return -1;
133                 }
134
135                 for (i = 0; i < len; i++) {
136                         ret = fread(&pf, 1, sizeof(pf), file);
137                         if (ret != sizeof(pf)) {
138                                 error = errno;
139                                 fprintf(stderr, 
140                                         "Error reading %ld bytes, got %d: %s\n",
141                                         sizeof(pf), ret, strerror(errno));
142                                 continue;
143                                 return -1;
144                         }
145
146                         pageframe_to_struct(pf, pageframe);
147                         match = tree_to_pageframe(
148                                 bintree_add(&(*pageframe_tree)->tree,
149                                         &pageframe->tree, &pageframe_ops));
150
151                         if (*pageframe_tree == NULL)
152                                 *pageframe_tree = match;
153
154                         /*
155                          * Add a link from the physical page to this
156                          * process's page map
157                          */
158                         if (!match->maps) {
159                                 match->maps = alloc_map();
160                                 *match->maps = *map;
161                                 INIT_LIST_HEAD(&match->maps->list);
162                         } else {
163                                 tmp = alloc_map();
164                                 *tmp = *map;
165                                 list_add(&match->maps->list, &tmp->list);
166                         }
167                 }
168         }
169
170         return 0;
171 }
172
173 struct pageframe *read_pageframe(int pid, struct pageframe *pageframe)
174 {
175         struct maps *maps;
176         FILE *file;
177         int error;
178         char path[512];
179
180         snprintf(path, sizeof(path), "/proc/%d/maps", pid);
181         file = fopen(path, "rb");
182
183         if (!file)
184                 goto err_out;
185
186         maps = parse_maps(file);
187
188         snprintf(path, sizeof(path), "/proc/%d/pagemap", pid);
189         file = fopen(path, "rb");
190
191         if (!file)
192                 goto err_out;
193
194         parse_pageframe(file, &pageframe, maps);
195
196         return pageframe;
197 err_out:
198         error = errno;
199         fprintf(stderr, "Failed to open file %s: %s\n", path, strerror(error));
200
201         return NULL;
202 }