11 static struct maps_list *alloc_maplist(void)
13 struct maps_list *map;
15 map = malloc(sizeof *map);
19 memset(map, 0, sizeof(*map));
20 INIT_LIST_HEAD(&map->list);
25 static struct maps *alloc_map(void)
29 map = malloc(sizeof *map);
33 memset(map, 0, sizeof(*map));
34 INIT_LIST_HEAD(&map->list);
39 static struct maps *parse_maps(FILE *file, int pid)
41 struct maps *the_map = NULL;
45 while(fgets(line, sizeof(line), file)) {
46 struct maps *map = alloc_map();
47 unsigned long start, end;
56 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
59 printf("%x, start %lx, end %lx, str: %s\n",
60 ret, start, end, ret >= 3 ? name : "");
63 printf("Error reading input: %s\n", line);
69 map->size = end - start;
73 strncpy(map->name, name, sizeof(map->name));
75 list_add_tail(&map->list, &the_map->list);
81 static void clear_pageframe(struct pageframe *pf)
83 memset(pf, 0, sizeof(*pf));
86 static struct pageframe *alloc_pageframe(void)
88 struct pageframe *pageframe;
90 pageframe = malloc(sizeof *pageframe);
91 if (pageframe == NULL)
94 clear_pageframe(pageframe);
99 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
101 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
103 pf->page_present = !!(BITRANGE(63, 63) & p);
104 pf->page_swapped = !!(BITRANGE(62, 62) & p);
105 pf->page_shift = (BITRANGE(55, 60) & p) >> 55;
106 pf->pfn = (BITRANGE(0, 54) & p);
107 pf->swap_type = (BITRANGE(0, 4) & p);
108 pf->swap_offset = (BITRANGE(5, 54) & p) >> 5;
110 printf("pfn: %lx shift: %d present: %d swapped %d\n",
111 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
115 static int compare_pageframe(struct bintree *at, struct bintree *bt)
117 struct pageframe *a, *b;
118 a = tree_to_pageframe(at);
119 b = tree_to_pageframe(bt);
121 return a->pfn - b->pfn;
124 struct bintree_ops pageframe_ops = {
125 .compare = compare_pageframe,
128 /* Read data from the /proc/pid/pagemap file */
129 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
133 struct maps_list *tmp;
134 struct pageframe *match, *pageframe;
136 unsigned long long pf;
139 /* Go through the list of allocated memory areas */
140 list_for_each_entry(map, &maps->list, list) {
141 start = map->start >> (PAGE_SHIFT - 3);
142 len = map->size >> (PAGE_SHIFT - 3);
144 printf("start: %lx len %lx: name: %s\n",
145 start, len, map->name);
147 ret = fseek(file, start, SEEK_SET);
150 fprintf(stderr, "Error seeking to %lx: %s\n", start,
156 for (i = 0; i < len; i++) {
157 ret = fread(&pf, 1, sizeof(pf), file);
158 if (ret != sizeof(pf)) {
161 "Error reading %ld bytes, got %d: %s\n",
162 sizeof(pf), ret, strerror(errno));
166 pageframe = alloc_pageframe();
167 pageframe_to_struct(pf, pageframe);
168 match = tree_to_pageframe(
169 bintree_add(&pf_tree->tree,
170 &pageframe->tree, &pageframe_ops));
172 if (match != pageframe)
176 * Add a link from the physical page to this
180 match->ml = alloc_maplist();
181 match->ml->map = map;
183 tmp = alloc_maplist();
185 list_add(&match->ml->list, &tmp->list);
193 void read_pageframe(int pid, struct pageframe *pageframe)
200 snprintf(path, sizeof(path), "/proc/%d/maps", pid);
201 file = fopen(path, "rb");
206 maps = parse_maps(file, pid);
208 snprintf(path, sizeof(path), "/proc/%d/pagemap", pid);
209 file = fopen(path, "rb");
214 parse_pageframe(file, pageframe, maps);
219 fprintf(stderr, "Failed to open file %s: %s\n", path, strerror(error));
224 static int get_next_pid(void)
226 static DIR *dir = NULL;
227 struct dirent *dirent;
231 dir = opendir("/proc");
234 printf("Failed to open /proc directory: %s\n",
241 dirent = readdir(dir);
249 printf("Failed to read directory: %s\n", strerror(error));
253 printf("%s\n", dirent->d_name);
254 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
257 return atoi(dirent->d_name);
260 void scan_all_pids(struct pageframe *pf)
264 pid = get_next_pid();
267 read_pageframe(pid, pf);