]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
read_pageframe: Do not add empty entries in process list
[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 err:
95         return pageframe;
96 }
97
98 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
99
100 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
101 {
102         /* Refer Documentation/vm/pagemap.txt for the format */
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;
109 #if 0
110         printf("pfn: %lx shift: %d present: %d swapped %d\n",
111                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
112 #endif
113 }
114
115 static int compare_pageframe(struct bintree *at, struct bintree *bt)
116 {
117         struct pageframe *a, *b;
118         a = tree_to_pageframe(at);
119         b = tree_to_pageframe(bt);
120
121         return a->pfn - b->pfn;
122 }
123
124 struct bintree_ops pageframe_ops = {
125         .compare = compare_pageframe,
126 };
127
128 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
129 {
130         FILE *file;
131         char path[512];
132         int ret;
133
134         snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
135         file = fopen(path, "rb");
136
137         if (!file)
138                 return -1;
139
140         ret = fread(cmdline, 1, len, file);
141         if (ret > 0)
142                 cmdline[ret - 1] = 0;
143         fclose(file);
144
145         return ret > 0 ? 0 : -1;
146 }
147
148 static char *get_name_by_pid(int pid)
149 {
150         static int last_pid;
151         static char cmdline[128];
152         static char *bname;
153
154         if (last_pid == pid)
155                 return bname;
156
157         if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
158                 bname = NULL;
159                 return NULL;
160         }
161
162         bname = basename(cmdline);
163
164         last_pid = pid;
165         return bname;
166 }
167
168 static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
169                 struct maps *map)
170 {
171         if (opts->parse_mask & PARSE_PID) {
172                 if (opts->pid == map->pid)
173                         return 1;
174         }
175
176         if (opts->parse_mask & PARSE_MAP_NAME) {
177                 if (!strcmp(opts->name, map->name))
178                         return 1;
179         }
180
181         if (opts->parse_mask & PARSE_PROCESS_NAME) {
182                 if (!strcmp(opts->name, get_name_by_pid(map->pid)))
183                         return 1;
184         }
185
186         return 0;
187 }
188
189 /* Read data from the /proc/pid/pagemap file */
190 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
191                         struct maps *maps, struct parse_opts *opts)
192 {
193         struct maps *map;
194         struct maps_list *tmp;
195         struct pageframe *match, *pageframe = NULL;
196         long start, len, i;
197         unsigned long long pf[10240];
198         int ret, error;
199
200         if (maps == NULL)
201                 return 0;
202
203         /* Go through the list of allocated memory areas */
204         list_for_each_entry(map, &maps->list, list) {
205                 start = map->start >> (PAGE_SHIFT - 3);
206                 len = map->size >> (PAGE_SHIFT);
207
208                 ret = fseek(file, start, SEEK_SET);
209                 if (ret) {
210                         error = errno;
211                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
212                                 strerror(error));
213                         continue;
214                 }
215
216                 for (i = 0; i < len; i++) {
217                         if (!ret) {
218                                 ret = fread(&pf, 1,
219                                         MIN(sizeof(pf), (len - i) * 8), file);
220                         }
221                         if (ret < 0) {
222                                 error = errno;
223                                 continue;
224                         }
225                         if (!pageframe)
226                                 pageframe = alloc_pageframe();
227                         ret -= sizeof(pf[0]);
228
229                         /* ignore unused pages */
230                         if (!pf[ret / sizeof(pf[0])])
231                                 continue;
232
233                         pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
234
235                         /* ignore unused pages */
236                         if (!(pageframe->page_swapped ||
237                                         pageframe->page_present))
238                                 continue;
239
240                         if (check_parse_opts(opts, pageframe, map)) {
241                                 match = tree_to_pageframe(
242                                         bintree_add(&pf_tree->tree,
243                                                 &pageframe->tree,
244                                                 &pageframe_ops));
245                         } else {
246                                 match = tree_to_pageframe(
247                                         bintree_find(&pf_tree->tree,
248                                                 &pageframe->tree,
249                                                 &pageframe_ops));
250                         }
251
252                         if (match == NULL)
253                                 continue;
254
255                         if (match == pageframe)
256                                 pageframe = NULL;
257
258                         match->refcount++;
259                         /*
260                          * Add a link from the physical page to this
261                          * process's page map
262                          */
263                         if (!match->ml) {
264                                 match->ml = alloc_maplist();
265                                 match->ml->map = map;
266                         } else {
267                                 tmp = alloc_maplist();
268                                 tmp->map = map;
269                                 list_add(&tmp->list, &match->ml->list);
270                         }
271
272                         if (match->page_present)
273                                 map->pages_present++;
274                         else if (match->page_swapped)
275                                 map->pages_swapped++;
276                 }
277         }
278
279         return 0;
280 }
281
282 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
283                         struct process **process_list, struct parse_opts *opts)
284 {
285         struct maps *maps;
286         struct process *process;
287         FILE *file;
288         char path[512];
289
290         process = malloc(sizeof(*process));
291         memset(process, 0, sizeof(*process));
292         INIT_LIST_HEAD(&process->list);
293
294         if (*process_list == NULL)
295                 *process_list = process;
296
297         process->pid = pid;
298         process->tid = tid;
299
300
301         snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
302         file = fopen(path, "rb");
303
304         if (!file)
305                 goto free;
306
307         maps = parse_maps(file, pid, tid);
308         fclose(file);
309         process->maps = maps;
310
311         snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
312         file = fopen(path, "rb");
313
314         if (!file)
315                 goto free;
316
317         parse_pageframe(file, pageframe, maps, opts);
318         fclose(file);
319
320         if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
321                 goto free;
322
323         if (maps != NULL) {
324                 list_for_each_entry(maps, &process->maps->list, list) {
325                         process->pages_present += maps->pages_present;
326                         process->pages_swapped += maps->pages_swapped;
327                 }
328         }
329
330         list_add_tail(&process->list, &(*process_list)->list);
331
332         return 1;
333 free:
334         free(process);
335
336         return 0;
337 }
338
339 static int parse_pid(DIR **dir)
340 {
341         struct dirent *dirent;
342         int error;
343
344 restart:
345         dirent = readdir(*dir);
346         if (!dirent) {
347                 if (errno == 0) {
348                         closedir(*dir);
349                         *dir = NULL;
350                         return 0;
351                 }
352                 error = errno;
353                 printf("Failed to read /proc directory: %s\n", strerror(error));
354                 return -1;
355         }
356
357         if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
358                 goto restart;
359
360         return atoi(dirent->d_name);
361 }
362
363 static int opendir_check(DIR **dir, const char *path)
364 {
365         int error;
366
367         if (!*dir) {
368                 *dir = opendir(path);
369                 if (!dir) {
370                         error = errno;
371                         fprintf(stderr, "Failed to open %s directory: %s\n",
372                                 path, strerror(error));
373                         return -1;
374                 }
375         }
376
377         return 0;
378 }
379
380 static int get_next_tid(int pid, DIR **dir)
381 {
382         if (*dir == NULL) {
383                 char path[64];
384
385                 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
386                 if (opendir_check(dir, path))
387                         return -1;
388         }
389
390         return parse_pid(dir);
391 }
392
393 static int get_next_pid(DIR **dir)
394 {
395         if (opendir_check(dir, "/proc"))
396                 return -1;
397
398         return parse_pid(dir);
399 }
400
401 static int get_next_pid_by_name(DIR **dir, char *name)
402 {
403         int pid;
404         char *pname;
405
406         if (opendir_check(dir, "/proc"))
407                 return -1;
408
409         while (1) {
410                 pid = parse_pid(dir);
411                 if (pid <= 0)
412                         break;
413
414                 pname = get_name_by_pid(pid);
415                 if (pname == NULL)
416                         continue;
417                 if (strcmp(pname, name))
418                         continue;
419
420                 return pid;
421         }
422
423         return 0;
424 }
425
426 static int read_pageframe_with_threads(int pid,
427                                 struct pageframe *pageframe,
428                                 struct process **process_list,
429                                 struct parse_opts *opts)
430 {
431         DIR *dir = NULL;
432         int tid;
433         int count = 0;
434
435         while (1) {
436                 if (opts->with_threads)
437                         tid = get_next_tid(pid, &dir);
438                 else
439                         tid = pid;
440
441                 if (tid <= 0)
442                         return count;
443
444                 count += read_pageframe(pid, tid, pageframe, process_list,
445                                         opts);
446
447                 if (!opts->with_threads)
448                         break;
449         }
450
451         return count;
452 }
453
454 int scan_all_pids(struct pageframe *pf, struct process **process_list,
455                 struct parse_opts *opts)
456 {
457         DIR *dir = NULL;
458         int pid;
459         int count = 0;
460
461         if (opts->parse_mask & PARSE_PROCESS_NAME) {
462                 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
463                         count += read_pageframe_with_threads(pid, pf,
464                                                         process_list,
465                                                         opts);
466                 }
467                 dir = NULL;
468         }
469
470         if (opts->parse_mask & PARSE_PID)
471                 count = read_pageframe_with_threads(opts->pid, pf, process_list,
472                                                 opts);
473
474         if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
475                 printf("Failed to find any matching processes "
476                         "with given arguments\n");
477                 return -1;
478         }
479
480         if (opts->parse_mask & PARSE_DUMP)
481                 return 0;
482
483         while (1) {
484                 pid = get_next_pid(&dir);
485                 if (pid <= 0)
486                         break;
487                 read_pageframe_with_threads(pid, pf, process_list, opts);
488         }
489
490         return 0;
491 }