]> git.itanic.dy.fi Git - scan-pagemap/blob - pagemap.h
Add support for listing mappings that are shared between processes
[scan-pagemap] / pagemap.h
1 #ifndef _PAGEMAP_H
2 #define _PAGEMAP_H
3
4 #include "utils.h"
5 #include "list.h"
6 #include "bintree.h"
7
8 #define PAGE_SHIFT      12
9 #define PAGE_SIZE       (1 << PAGE_SHIFT)
10
11 struct maps;
12
13 struct maps_list {
14         struct maps *map;
15         struct list_head list;
16 };
17 #define list_to_maps_list(list_head)                            \
18         container_of((list_head), struct maps_list, list)
19
20 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
21
22 struct pageframe {
23         struct bintree tree;
24         struct list_head ml;    /* List of mappings which refer to this pfn */
25
26         unsigned long long pf;  /* page frame entry from /proc/pid/pagemap */
27
28         int refcount;
29 };
30
31 #define tree_to_pageframe(tree_struct)                          \
32         container_of((tree_struct), struct pageframe, tree)
33
34 static inline void clear_pageframe(struct pageframe *pf)
35 {
36         memset(pf, 0, sizeof(*pf));
37         INIT_LIST_HEAD(&pf->ml);
38 }
39
40 static inline int page_present(struct pageframe *p)
41 {
42         return !!(BITRANGE(63, 63) & p->pf);
43 }
44
45 static inline int page_swapped(struct pageframe *p)
46 {
47         return !!(BITRANGE(62, 62) & p->pf);
48 }
49
50 static inline int page_shift(struct pageframe *p)
51 {
52         return (BITRANGE(55, 60) & p->pf) >> 55;
53 }
54
55 static inline long int pfn(struct pageframe *p)
56 {
57         return (BITRANGE(0, 54) & p->pf);
58 }
59
60 static inline int swap_type(struct pageframe *p)
61 {
62         return (BITRANGE(0, 4) & p->pf);
63 }
64
65 static inline int swap_offset(struct pageframe *p)
66 {
67         return (BITRANGE(5, 54) & p->pf) >> 5;
68 }
69
70 struct maps {
71         struct list_head list;
72
73         /* Memory segment of a mapping */
74         unsigned long start;
75         unsigned long end;
76         unsigned long size;
77
78         long int pages_present;
79         long int pages_swapped;
80
81         /* Name of the mapping, such as library name or something else */
82         char name[128];
83         int pid; /* Process which address space the mapping belongs to */
84         int tid; /* thread id */
85 };
86
87 #define list_to_maps(list_head)                         \
88         container_of((list_head), struct maps, list)
89
90 struct process {
91         struct maps *maps;
92         struct list_head list;
93         int pid;
94         int tid;
95         char name[256];
96
97         long int pages_present;
98         long int pages_swapped;
99
100         int is_initial_pid;
101 };
102
103 #define PARSE_PID               0x1
104 #define PARSE_MAP_NAME          0x2
105 #define PARSE_DUMP              0x8
106 #define PARSE_NOADD_TREE        0x10
107 #define PARSE_SHARED_MAPPING    0x20
108
109 struct pidlist {
110         struct list_head list;
111
112         int pid;
113 };
114
115 struct parse_opts {
116         struct list_head pidlist;
117
118         int parse_mask;
119         char *name;
120         int with_threads;
121 };
122
123 static inline void init_parse_opts(struct parse_opts *p)
124 {
125         memset(p, 0, sizeof(*p));
126         INIT_LIST_HEAD(&p->pidlist);
127 }
128
129 static inline struct pidlist *alloc_pidlist(void)
130 {
131         struct pidlist *p = malloc(sizeof(*p));
132
133         if (p == NULL)
134                 return p;
135
136         memset(p, 0, sizeof(*p));
137         INIT_LIST_HEAD(&p->list);
138
139         return p;
140 }
141
142 #define is_parse_option(parse_opts, flag) \
143         (!!((parse_opts)->parse_mask & (flag)))
144
145 #endif