]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
Correct the usage of linked lists
[scan-pagemap] / parse.c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <libgen.h>
8
9 #include "parse.h"
10 #include "pagemap.h"
11
12 static struct maps_list *alloc_maplist(void)
13 {
14         struct maps_list *map;
15
16         map = malloc(sizeof *map);
17         if (map == NULL)
18                 goto err;
19
20         memset(map, 0, sizeof(*map));
21         INIT_LIST_HEAD(&map->list);
22 err:
23         return map;
24 }
25
26 static struct maps *alloc_map(void)
27 {
28         struct maps *map;
29
30         map = malloc(sizeof *map);
31         if (map == NULL)
32                 goto err;
33
34         memset(map, 0, sizeof(*map));
35         INIT_LIST_HEAD(&map->list);
36 err:
37         return map;
38 }
39
40 static struct maps *parse_maps(FILE *file, int pid, int tid)
41 {
42         struct maps *the_map = NULL;
43         char line[1024];
44         int ret;
45
46         while (fgets(line, sizeof(line), file)) {
47                 struct maps *map = alloc_map();
48                 unsigned long start, end;
49                 char name[1024];
50
51                 if (map == NULL)
52                         return 0;
53
54                 if (the_map == NULL)
55                         the_map = map;
56
57                 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
58                              &start, &end, name);
59
60                 if (ret < 2) {
61                         printf("Error reading input: %s\n", line);
62                         break;
63                 }
64
65                 map->start = start;
66                 map->end = end;
67                 map->size = end - start;
68                 map->pid = pid;
69                 map->tid = tid;
70
71                 if (ret >= 3)
72                         strncpy(map->name, name, sizeof(map->name));
73
74                 list_add_tail(&map->list, &the_map->list);
75         }
76
77         return the_map;
78 }
79
80 static void clear_pageframe(struct pageframe *pf)
81 {
82         memset(pf, 0, sizeof(*pf));
83 }
84
85 static struct pageframe *alloc_pageframe(void)
86 {
87         struct pageframe *pageframe;
88
89         pageframe = malloc(sizeof *pageframe);
90         if (pageframe == NULL)
91                 goto err;
92
93         clear_pageframe(pageframe);
94         INIT_LIST_HEAD(&pageframe->ml.list);
95 err:
96         return pageframe;
97 }
98
99 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
100
101 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
102 {
103         /* Refer Documentation/vm/pagemap.txt for the format */
104         pf->page_present = !!(BITRANGE(63, 63) & p);
105         pf->page_swapped = !!(BITRANGE(62, 62) & p);
106         pf->page_shift   =   (BITRANGE(55, 60) & p) >> 55;
107         pf->pfn          =   (BITRANGE(0, 54) & p);
108         pf->swap_type    =   (BITRANGE(0, 4) & p);
109         pf->swap_offset  =   (BITRANGE(5, 54) & p) >> 5;
110 #if 0
111         printf("pfn: %lx shift: %d present: %d swapped %d\n",
112                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
113 #endif
114 }
115
116 static int compare_pageframe(struct bintree *at, struct bintree *bt)
117 {
118         struct pageframe *a, *b;
119         a = tree_to_pageframe(at);
120         b = tree_to_pageframe(bt);
121
122         return a->pfn - b->pfn;
123 }
124
125 struct bintree_ops pageframe_ops = {
126         .compare = compare_pageframe,
127 };
128
129 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
130 {
131         FILE *file;
132         char path[512];
133         int ret;
134
135         snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
136         file = fopen(path, "rb");
137
138         if (!file)
139                 return -1;
140
141         ret = fread(cmdline, 1, len, file);
142         if (ret > 0)
143                 cmdline[ret - 1] = 0;
144         fclose(file);
145
146         return ret > 0 ? 0 : -1;
147 }
148
149 static char *get_name_by_pid(int pid)
150 {
151         static int last_pid;
152         static char cmdline[128];
153         static char *bname;
154
155         if (last_pid == pid)
156                 return bname;
157
158         if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
159                 bname = NULL;
160                 return NULL;
161         }
162
163         bname = basename(cmdline);
164
165         last_pid = pid;
166         return bname;
167 }
168
169 static int should_scan_process(struct parse_opts *opts, struct process *process)
170 {
171         int match = 0;
172         char *name;
173
174         if (opts->parse_mask & PARSE_PROCESS_NAME) {
175                 name = get_name_by_pid(process->pid);
176                 if (!strcmp(opts->name, name ? name : ""))
177                         match = 1;
178         }
179
180         if (opts->parse_mask & PARSE_PID) {
181                 if (opts->pid == process->pid)
182                         match = 1;
183         }
184
185         if (opts->parse_mask & PARSE_NOADD_TREE)
186                 match = !match;
187
188         return match;
189 }
190
191 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
192 {
193         int match = 0;
194
195         if (opts->parse_mask & PARSE_MAP_NAME) {
196                 if (!strcmp(opts->name, map->name))
197                         match = 1;
198
199                 if (opts->parse_mask & PARSE_NOADD_TREE)
200                         match = !match;
201         } else
202                 match = 1;
203
204         return match;
205 }
206
207 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
208                 struct maps *map)
209 {
210         if (opts->parse_mask & PARSE_NOADD_TREE)
211                 return 0;
212
213         return 1;
214 }
215
216 /* Read data from the /proc/pid/pagemap file */
217 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
218                         struct maps *maps, struct parse_opts *opts)
219 {
220         struct maps *map;
221         struct maps_list *tmp;
222         struct pageframe *match, *pageframe = NULL;
223         long start, len, i;
224         unsigned long long pf[10240];
225         int ret, error;
226
227         if (maps == NULL)
228                 return 0;
229
230         /* Go through the list of allocated memory areas */
231         list_for_each_entry(map, &maps->list, list) {
232                 start = map->start >> (PAGE_SHIFT - 3);
233                 len = map->size >> (PAGE_SHIFT);
234
235                 if (!should_scan_mapping(opts, map))
236                         continue;
237
238                 ret = fseek(file, start, SEEK_SET);
239                 if (ret) {
240                         error = errno;
241                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
242                                 strerror(error));
243                         continue;
244                 }
245
246                 for (i = 0; i < len; i++) {
247                         if (!ret) {
248                                 ret = fread(&pf, 1,
249                                         MIN(sizeof(pf), (len - i) * 8), file);
250                         }
251                         if (ret < 0) {
252                                 error = errno;
253                                 continue;
254                         }
255                         if (!pageframe)
256                                 pageframe = alloc_pageframe();
257                         ret -= sizeof(pf[0]);
258
259                         /* ignore unused pages */
260                         if (!pf[ret / sizeof(pf[0])])
261                                 continue;
262
263                         pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
264
265                         /* ignore unused pages */
266                         if (!(pageframe->page_swapped ||
267                                         pageframe->page_present))
268                                 continue;
269
270                         if (should_add_to_tree(opts, pageframe, map)) {
271                                 match = tree_to_pageframe(
272                                         bintree_add(&pf_tree->tree,
273                                                 &pageframe->tree,
274                                                 &pageframe_ops));
275                         } else {
276                                 match = tree_to_pageframe(
277                                         bintree_find(&pf_tree->tree,
278                                                 &pageframe->tree,
279                                                 &pageframe_ops));
280                         }
281
282                         if (match == NULL)
283                                 continue;
284
285                         if (match == pageframe)
286                                 pageframe = NULL;
287
288                         match->refcount++;
289                         /*
290                          * Add a link from the physical page to this
291                          * process's page map
292                          */
293                         tmp = alloc_maplist();
294                         tmp->map = map;
295                         list_add(&tmp->list, &match->ml.list);
296
297                         if (match->page_present)
298                                 map->pages_present++;
299                         else if (match->page_swapped)
300                                 map->pages_swapped++;
301                 }
302         }
303
304         return 0;
305 }
306
307 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
308                         struct process *process_list, struct parse_opts *opts)
309 {
310         struct maps *maps;
311         struct process *process;
312         FILE *file;
313         char path[512];
314
315         process = malloc(sizeof(*process));
316         memset(process, 0, sizeof(*process));
317         INIT_LIST_HEAD(&process->list);
318
319         process->pid = pid;
320         process->tid = tid;
321
322         if (!should_scan_process(opts, process))
323                 goto free;
324
325         snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
326         file = fopen(path, "rb");
327
328         if (!file)
329                 goto free;
330
331         maps = parse_maps(file, pid, tid);
332         fclose(file);
333         process->maps = maps;
334
335         snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
336         file = fopen(path, "rb");
337
338         if (!file)
339                 goto free;
340
341         parse_pageframe(file, pageframe, maps, opts);
342         fclose(file);
343
344         if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
345                 goto free;
346
347         if (maps != NULL) {
348                 list_for_each_entry(maps, &process->maps->list, list) {
349                         process->pages_present += maps->pages_present;
350                         process->pages_swapped += maps->pages_swapped;
351                 }
352         }
353
354         list_add_tail(&process->list, &process_list->list);
355
356         return 1;
357 free:
358         free(process);
359
360         return 0;
361 }
362
363 static int parse_pid(DIR **dir)
364 {
365         struct dirent *dirent;
366         int error;
367
368 restart:
369         dirent = readdir(*dir);
370         if (!dirent) {
371                 if (errno == 0) {
372                         closedir(*dir);
373                         *dir = NULL;
374                         return 0;
375                 }
376                 error = errno;
377                 printf("Failed to read /proc directory: %s\n", strerror(error));
378                 return -1;
379         }
380
381         if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
382                 goto restart;
383
384         return atoi(dirent->d_name);
385 }
386
387 static int opendir_check(DIR **dir, const char *path)
388 {
389         int error;
390
391         if (!*dir) {
392                 *dir = opendir(path);
393                 if (!dir) {
394                         error = errno;
395                         fprintf(stderr, "Failed to open %s directory: %s\n",
396                                 path, strerror(error));
397                         return -1;
398                 }
399         }
400
401         return 0;
402 }
403
404 static int get_next_tid(int pid, DIR **dir)
405 {
406         if (*dir == NULL) {
407                 char path[64];
408
409                 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
410                 if (opendir_check(dir, path))
411                         return -1;
412         }
413
414         return parse_pid(dir);
415 }
416
417 static int get_next_pid(DIR **dir)
418 {
419         if (opendir_check(dir, "/proc"))
420                 return -1;
421
422         return parse_pid(dir);
423 }
424
425 static int get_next_pid_by_name(DIR **dir, char *name)
426 {
427         int pid;
428         char *pname;
429
430         if (opendir_check(dir, "/proc"))
431                 return -1;
432
433         while (1) {
434                 pid = parse_pid(dir);
435                 if (pid <= 0)
436                         break;
437
438                 pname = get_name_by_pid(pid);
439                 if (pname == NULL)
440                         continue;
441                 if (strcmp(pname, name))
442                         continue;
443
444                 return pid;
445         }
446
447         return 0;
448 }
449
450 static int read_pageframe_with_threads(int pid,
451                                 struct pageframe *pageframe,
452                                 struct process *process_list,
453                                 struct parse_opts *opts)
454 {
455         DIR *dir = NULL;
456         int tid;
457         int count = 0;
458
459         while (1) {
460                 if (opts->with_threads)
461                         tid = get_next_tid(pid, &dir);
462                 else
463                         tid = pid;
464
465                 if (tid <= 0)
466                         return count;
467
468                 count += read_pageframe(pid, tid, pageframe, process_list,
469                                         opts);
470
471                 if (!opts->with_threads)
472                         break;
473         }
474
475         return count;
476 }
477
478 int scan_all_pids(struct pageframe *pf, struct process *process_list,
479                 struct parse_opts *opts)
480 {
481         DIR *dir = NULL;
482         int pid;
483         int count = 0;
484
485         if (opts->parse_mask & PARSE_PROCESS_NAME) {
486                 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
487                         count += read_pageframe_with_threads(pid, pf,
488                                                         process_list,
489                                                         opts);
490                 }
491                 dir = NULL;
492         }
493
494         if (opts->parse_mask & PARSE_PID)
495                 count = read_pageframe_with_threads(opts->pid, pf, process_list,
496                                                 opts);
497
498         if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
499                 printf("Failed to find any matching processes "
500                         "with given arguments\n");
501                 return -1;
502         }
503
504         if (opts->parse_mask & PARSE_DUMP)
505                 return 0;
506
507         /* Do not add new pages in the tree after the initial scan */
508         opts->parse_mask |= PARSE_NOADD_TREE;
509
510         while (1) {
511                 pid = get_next_pid(&dir);
512                 if (pid <= 0)
513                         break;
514                 read_pageframe_with_threads(pid, pf, process_list, opts);
515         }
516
517         return 0;
518 }