ksm: more on default values
[linux-2.6.git] / Documentation / vm / page-types.c
1 /*
2  * page-types: Tool for querying page flags
3  *
4  * Copyright (C) 2009 Intel corporation
5  * Copyright (C) 2009 Wu Fengguang <fengguang.wu@intel.com>
6  */
7
8 #define _LARGEFILE64_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdint.h>
13 #include <stdarg.h>
14 #include <string.h>
15 #include <getopt.h>
16 #include <limits.h>
17 #include <assert.h>
18 #include <sys/types.h>
19 #include <sys/errno.h>
20 #include <sys/fcntl.h>
21
22
23 /*
24  * pagemap kernel ABI bits
25  */
26
27 #define PM_ENTRY_BYTES      sizeof(uint64_t)
28 #define PM_STATUS_BITS      3
29 #define PM_STATUS_OFFSET    (64 - PM_STATUS_BITS)
30 #define PM_STATUS_MASK      (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
31 #define PM_STATUS(nr)       (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
32 #define PM_PSHIFT_BITS      6
33 #define PM_PSHIFT_OFFSET    (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
34 #define PM_PSHIFT_MASK      (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
35 #define PM_PSHIFT(x)        (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
36 #define PM_PFRAME_MASK      ((1LL << PM_PSHIFT_OFFSET) - 1)
37 #define PM_PFRAME(x)        ((x) & PM_PFRAME_MASK)
38
39 #define PM_PRESENT          PM_STATUS(4LL)
40 #define PM_SWAP             PM_STATUS(2LL)
41
42
43 /*
44  * kernel page flags
45  */
46
47 #define KPF_BYTES               8
48 #define PROC_KPAGEFLAGS         "/proc/kpageflags"
49
50 /* copied from kpageflags_read() */
51 #define KPF_LOCKED              0
52 #define KPF_ERROR               1
53 #define KPF_REFERENCED          2
54 #define KPF_UPTODATE            3
55 #define KPF_DIRTY               4
56 #define KPF_LRU                 5
57 #define KPF_ACTIVE              6
58 #define KPF_SLAB                7
59 #define KPF_WRITEBACK           8
60 #define KPF_RECLAIM             9
61 #define KPF_BUDDY               10
62
63 /* [11-20] new additions in 2.6.31 */
64 #define KPF_MMAP                11
65 #define KPF_ANON                12
66 #define KPF_SWAPCACHE           13
67 #define KPF_SWAPBACKED          14
68 #define KPF_COMPOUND_HEAD       15
69 #define KPF_COMPOUND_TAIL       16
70 #define KPF_HUGE                17
71 #define KPF_UNEVICTABLE         18
72 #define KPF_NOPAGE              20
73
74 /* [32-] kernel hacking assistances */
75 #define KPF_RESERVED            32
76 #define KPF_MLOCKED             33
77 #define KPF_MAPPEDTODISK        34
78 #define KPF_PRIVATE             35
79 #define KPF_PRIVATE_2           36
80 #define KPF_OWNER_PRIVATE       37
81 #define KPF_ARCH                38
82 #define KPF_UNCACHED            39
83
84 /* [48-] take some arbitrary free slots for expanding overloaded flags
85  * not part of kernel API
86  */
87 #define KPF_READAHEAD           48
88 #define KPF_SLOB_FREE           49
89 #define KPF_SLUB_FROZEN         50
90 #define KPF_SLUB_DEBUG          51
91
92 #define KPF_ALL_BITS            ((uint64_t)~0ULL)
93 #define KPF_HACKERS_BITS        (0xffffULL << 32)
94 #define KPF_OVERLOADED_BITS     (0xffffULL << 48)
95 #define BIT(name)               (1ULL << KPF_##name)
96 #define BITS_COMPOUND           (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
97
98 static char *page_flag_names[] = {
99         [KPF_LOCKED]            = "L:locked",
100         [KPF_ERROR]             = "E:error",
101         [KPF_REFERENCED]        = "R:referenced",
102         [KPF_UPTODATE]          = "U:uptodate",
103         [KPF_DIRTY]             = "D:dirty",
104         [KPF_LRU]               = "l:lru",
105         [KPF_ACTIVE]            = "A:active",
106         [KPF_SLAB]              = "S:slab",
107         [KPF_WRITEBACK]         = "W:writeback",
108         [KPF_RECLAIM]           = "I:reclaim",
109         [KPF_BUDDY]             = "B:buddy",
110
111         [KPF_MMAP]              = "M:mmap",
112         [KPF_ANON]              = "a:anonymous",
113         [KPF_SWAPCACHE]         = "s:swapcache",
114         [KPF_SWAPBACKED]        = "b:swapbacked",
115         [KPF_COMPOUND_HEAD]     = "H:compound_head",
116         [KPF_COMPOUND_TAIL]     = "T:compound_tail",
117         [KPF_HUGE]              = "G:huge",
118         [KPF_UNEVICTABLE]       = "u:unevictable",
119         [KPF_NOPAGE]            = "n:nopage",
120
121         [KPF_RESERVED]          = "r:reserved",
122         [KPF_MLOCKED]           = "m:mlocked",
123         [KPF_MAPPEDTODISK]      = "d:mappedtodisk",
124         [KPF_PRIVATE]           = "P:private",
125         [KPF_PRIVATE_2]         = "p:private_2",
126         [KPF_OWNER_PRIVATE]     = "O:owner_private",
127         [KPF_ARCH]              = "h:arch",
128         [KPF_UNCACHED]          = "c:uncached",
129
130         [KPF_READAHEAD]         = "I:readahead",
131         [KPF_SLOB_FREE]         = "P:slob_free",
132         [KPF_SLUB_FROZEN]       = "A:slub_frozen",
133         [KPF_SLUB_DEBUG]        = "E:slub_debug",
134 };
135
136
137 /*
138  * data structures
139  */
140
141 static int              opt_raw;        /* for kernel developers */
142 static int              opt_list;       /* list pages (in ranges) */
143 static int              opt_no_summary; /* don't show summary */
144 static pid_t            opt_pid;        /* process to walk */
145
146 #define MAX_ADDR_RANGES 1024
147 static int              nr_addr_ranges;
148 static unsigned long    opt_offset[MAX_ADDR_RANGES];
149 static unsigned long    opt_size[MAX_ADDR_RANGES];
150
151 #define MAX_VMAS        10240
152 static int              nr_vmas;
153 static unsigned long    pg_start[MAX_VMAS];
154 static unsigned long    pg_end[MAX_VMAS];
155 static unsigned long    voffset;
156
157 static int              pagemap_fd;
158
159 #define MAX_BIT_FILTERS 64
160 static int              nr_bit_filters;
161 static uint64_t         opt_mask[MAX_BIT_FILTERS];
162 static uint64_t         opt_bits[MAX_BIT_FILTERS];
163
164 static int              page_size;
165
166 #define PAGES_BATCH     (64 << 10)      /* 64k pages */
167 static int              kpageflags_fd;
168
169 #define HASH_SHIFT      13
170 #define HASH_SIZE       (1 << HASH_SHIFT)
171 #define HASH_MASK       (HASH_SIZE - 1)
172 #define HASH_KEY(flags) (flags & HASH_MASK)
173
174 static unsigned long    total_pages;
175 static unsigned long    nr_pages[HASH_SIZE];
176 static uint64_t         page_flags[HASH_SIZE];
177
178
179 /*
180  * helper functions
181  */
182
183 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
184
185 #define min_t(type, x, y) ({                    \
186         type __min1 = (x);                      \
187         type __min2 = (y);                      \
188         __min1 < __min2 ? __min1 : __min2; })
189
190 #define max_t(type, x, y) ({                    \
191         type __max1 = (x);                      \
192         type __max2 = (y);                      \
193         __max1 > __max2 ? __max1 : __max2; })
194
195 static unsigned long pages2mb(unsigned long pages)
196 {
197         return (pages * page_size) >> 20;
198 }
199
200 static void fatal(const char *x, ...)
201 {
202         va_list ap;
203
204         va_start(ap, x);
205         vfprintf(stderr, x, ap);
206         va_end(ap);
207         exit(EXIT_FAILURE);
208 }
209
210
211 /*
212  * page flag names
213  */
214
215 static char *page_flag_name(uint64_t flags)
216 {
217         static char buf[65];
218         int present;
219         int i, j;
220
221         for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
222                 present = (flags >> i) & 1;
223                 if (!page_flag_names[i]) {
224                         if (present)
225                                 fatal("unkown flag bit %d\n", i);
226                         continue;
227                 }
228                 buf[j++] = present ? page_flag_names[i][0] : '_';
229         }
230
231         return buf;
232 }
233
234 static char *page_flag_longname(uint64_t flags)
235 {
236         static char buf[1024];
237         int i, n;
238
239         for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
240                 if (!page_flag_names[i])
241                         continue;
242                 if ((flags >> i) & 1)
243                         n += snprintf(buf + n, sizeof(buf) - n, "%s,",
244                                         page_flag_names[i] + 2);
245         }
246         if (n)
247                 n--;
248         buf[n] = '\0';
249
250         return buf;
251 }
252
253
254 /*
255  * page list and summary
256  */
257
258 static void show_page_range(unsigned long offset, uint64_t flags)
259 {
260         static uint64_t      flags0;
261         static unsigned long voff;
262         static unsigned long index;
263         static unsigned long count;
264
265         if (flags == flags0 && offset == index + count &&
266             (!opt_pid || voffset == voff + count)) {
267                 count++;
268                 return;
269         }
270
271         if (count) {
272                 if (opt_pid)
273                         printf("%lx\t", voff);
274                 printf("%lx\t%lx\t%s\n",
275                                 index, count, page_flag_name(flags0));
276         }
277
278         flags0 = flags;
279         index  = offset;
280         voff   = voffset;
281         count  = 1;
282 }
283
284 static void show_page(unsigned long offset, uint64_t flags)
285 {
286         if (opt_pid)
287                 printf("%lx\t", voffset);
288         printf("%lx\t%s\n", offset, page_flag_name(flags));
289 }
290
291 static void show_summary(void)
292 {
293         int i;
294
295         printf("             flags\tpage-count       MB"
296                 "  symbolic-flags\t\t\tlong-symbolic-flags\n");
297
298         for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
299                 if (nr_pages[i])
300                         printf("0x%016llx\t%10lu %8lu  %s\t%s\n",
301                                 (unsigned long long)page_flags[i],
302                                 nr_pages[i],
303                                 pages2mb(nr_pages[i]),
304                                 page_flag_name(page_flags[i]),
305                                 page_flag_longname(page_flags[i]));
306         }
307
308         printf("             total\t%10lu %8lu\n",
309                         total_pages, pages2mb(total_pages));
310 }
311
312
313 /*
314  * page flag filters
315  */
316
317 static int bit_mask_ok(uint64_t flags)
318 {
319         int i;
320
321         for (i = 0; i < nr_bit_filters; i++) {
322                 if (opt_bits[i] == KPF_ALL_BITS) {
323                         if ((flags & opt_mask[i]) == 0)
324                                 return 0;
325                 } else {
326                         if ((flags & opt_mask[i]) != opt_bits[i])
327                                 return 0;
328                 }
329         }
330
331         return 1;
332 }
333
334 static uint64_t expand_overloaded_flags(uint64_t flags)
335 {
336         /* SLOB/SLUB overload several page flags */
337         if (flags & BIT(SLAB)) {
338                 if (flags & BIT(PRIVATE))
339                         flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
340                 if (flags & BIT(ACTIVE))
341                         flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
342                 if (flags & BIT(ERROR))
343                         flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
344         }
345
346         /* PG_reclaim is overloaded as PG_readahead in the read path */
347         if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
348                 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
349
350         return flags;
351 }
352
353 static uint64_t well_known_flags(uint64_t flags)
354 {
355         /* hide flags intended only for kernel hacker */
356         flags &= ~KPF_HACKERS_BITS;
357
358         /* hide non-hugeTLB compound pages */
359         if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
360                 flags &= ~BITS_COMPOUND;
361
362         return flags;
363 }
364
365
366 /*
367  * page frame walker
368  */
369
370 static int hash_slot(uint64_t flags)
371 {
372         int k = HASH_KEY(flags);
373         int i;
374
375         /* Explicitly reserve slot 0 for flags 0: the following logic
376          * cannot distinguish an unoccupied slot from slot (flags==0).
377          */
378         if (flags == 0)
379                 return 0;
380
381         /* search through the remaining (HASH_SIZE-1) slots */
382         for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
383                 if (!k || k >= ARRAY_SIZE(page_flags))
384                         k = 1;
385                 if (page_flags[k] == 0) {
386                         page_flags[k] = flags;
387                         return k;
388                 }
389                 if (page_flags[k] == flags)
390                         return k;
391         }
392
393         fatal("hash table full: bump up HASH_SHIFT?\n");
394         exit(EXIT_FAILURE);
395 }
396
397 static void add_page(unsigned long offset, uint64_t flags)
398 {
399         flags = expand_overloaded_flags(flags);
400
401         if (!opt_raw)
402                 flags = well_known_flags(flags);
403
404         if (!bit_mask_ok(flags))
405                 return;
406
407         if (opt_list == 1)
408                 show_page_range(offset, flags);
409         else if (opt_list == 2)
410                 show_page(offset, flags);
411
412         nr_pages[hash_slot(flags)]++;
413         total_pages++;
414 }
415
416 static void walk_pfn(unsigned long index, unsigned long count)
417 {
418         unsigned long batch;
419         unsigned long n;
420         unsigned long i;
421
422         if (index > ULONG_MAX / KPF_BYTES)
423                 fatal("index overflow: %lu\n", index);
424
425         lseek(kpageflags_fd, index * KPF_BYTES, SEEK_SET);
426
427         while (count) {
428                 uint64_t kpageflags_buf[KPF_BYTES * PAGES_BATCH];
429
430                 batch = min_t(unsigned long, count, PAGES_BATCH);
431                 n = read(kpageflags_fd, kpageflags_buf, batch * KPF_BYTES);
432                 if (n == 0)
433                         break;
434                 if (n < 0) {
435                         perror(PROC_KPAGEFLAGS);
436                         exit(EXIT_FAILURE);
437                 }
438
439                 if (n % KPF_BYTES != 0)
440                         fatal("partial read: %lu bytes\n", n);
441                 n = n / KPF_BYTES;
442
443                 for (i = 0; i < n; i++)
444                         add_page(index + i, kpageflags_buf[i]);
445
446                 index += batch;
447                 count -= batch;
448         }
449 }
450
451
452 #define PAGEMAP_BATCH   4096
453 static unsigned long task_pfn(unsigned long pgoff)
454 {
455         static uint64_t buf[PAGEMAP_BATCH];
456         static unsigned long start;
457         static long count;
458         uint64_t pfn;
459
460         if (pgoff < start || pgoff >= start + count) {
461                 if (lseek64(pagemap_fd,
462                             (uint64_t)pgoff * PM_ENTRY_BYTES,
463                             SEEK_SET) < 0) {
464                         perror("pagemap seek");
465                         exit(EXIT_FAILURE);
466                 }
467                 count = read(pagemap_fd, buf, sizeof(buf));
468                 if (count == 0)
469                         return 0;
470                 if (count < 0) {
471                         perror("pagemap read");
472                         exit(EXIT_FAILURE);
473                 }
474                 if (count % PM_ENTRY_BYTES) {
475                         fatal("pagemap read not aligned.\n");
476                         exit(EXIT_FAILURE);
477                 }
478                 count /= PM_ENTRY_BYTES;
479                 start = pgoff;
480         }
481
482         pfn = buf[pgoff - start];
483         if (pfn & PM_PRESENT)
484                 pfn = PM_PFRAME(pfn);
485         else
486                 pfn = 0;
487
488         return pfn;
489 }
490
491 static void walk_task(unsigned long index, unsigned long count)
492 {
493         int i = 0;
494         const unsigned long end = index + count;
495
496         while (index < end) {
497
498                 while (pg_end[i] <= index)
499                         if (++i >= nr_vmas)
500                                 return;
501                 if (pg_start[i] >= end)
502                         return;
503
504                 voffset = max_t(unsigned long, pg_start[i], index);
505                 index   = min_t(unsigned long, pg_end[i], end);
506
507                 assert(voffset < index);
508                 for (; voffset < index; voffset++) {
509                         unsigned long pfn = task_pfn(voffset);
510                         if (pfn)
511                                 walk_pfn(pfn, 1);
512                 }
513         }
514 }
515
516 static void add_addr_range(unsigned long offset, unsigned long size)
517 {
518         if (nr_addr_ranges >= MAX_ADDR_RANGES)
519                 fatal("too many addr ranges\n");
520
521         opt_offset[nr_addr_ranges] = offset;
522         opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
523         nr_addr_ranges++;
524 }
525
526 static void walk_addr_ranges(void)
527 {
528         int i;
529
530         kpageflags_fd = open(PROC_KPAGEFLAGS, O_RDONLY);
531         if (kpageflags_fd < 0) {
532                 perror(PROC_KPAGEFLAGS);
533                 exit(EXIT_FAILURE);
534         }
535
536         if (!nr_addr_ranges)
537                 add_addr_range(0, ULONG_MAX);
538
539         for (i = 0; i < nr_addr_ranges; i++)
540                 if (!opt_pid)
541                         walk_pfn(opt_offset[i], opt_size[i]);
542                 else
543                         walk_task(opt_offset[i], opt_size[i]);
544
545         close(kpageflags_fd);
546 }
547
548
549 /*
550  * user interface
551  */
552
553 static const char *page_flag_type(uint64_t flag)
554 {
555         if (flag & KPF_HACKERS_BITS)
556                 return "(r)";
557         if (flag & KPF_OVERLOADED_BITS)
558                 return "(o)";
559         return "   ";
560 }
561
562 static void usage(void)
563 {
564         int i, j;
565
566         printf(
567 "page-types [options]\n"
568 "            -r|--raw                  Raw mode, for kernel developers\n"
569 "            -a|--addr    addr-spec    Walk a range of pages\n"
570 "            -b|--bits    bits-spec    Walk pages with specified bits\n"
571 "            -p|--pid     pid          Walk process address space\n"
572 #if 0 /* planned features */
573 "            -f|--file    filename     Walk file address space\n"
574 #endif
575 "            -l|--list                 Show page details in ranges\n"
576 "            -L|--list-each            Show page details one by one\n"
577 "            -N|--no-summary           Don't show summay info\n"
578 "            -h|--help                 Show this usage message\n"
579 "addr-spec:\n"
580 "            N                         one page at offset N (unit: pages)\n"
581 "            N+M                       pages range from N to N+M-1\n"
582 "            N,M                       pages range from N to M-1\n"
583 "            N,                        pages range from N to end\n"
584 "            ,M                        pages range from 0 to M-1\n"
585 "bits-spec:\n"
586 "            bit1,bit2                 (flags & (bit1|bit2)) != 0\n"
587 "            bit1,bit2=bit1            (flags & (bit1|bit2)) == bit1\n"
588 "            bit1,~bit2                (flags & (bit1|bit2)) == bit1\n"
589 "            =bit1,bit2                flags == (bit1|bit2)\n"
590 "bit-names:\n"
591         );
592
593         for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
594                 if (!page_flag_names[i])
595                         continue;
596                 printf("%16s%s", page_flag_names[i] + 2,
597                                  page_flag_type(1ULL << i));
598                 if (++j > 3) {
599                         j = 0;
600                         putchar('\n');
601                 }
602         }
603         printf("\n                                   "
604                 "(r) raw mode bits  (o) overloaded bits\n");
605 }
606
607 static unsigned long long parse_number(const char *str)
608 {
609         unsigned long long n;
610
611         n = strtoll(str, NULL, 0);
612
613         if (n == 0 && str[0] != '0')
614                 fatal("invalid name or number: %s\n", str);
615
616         return n;
617 }
618
619 static void parse_pid(const char *str)
620 {
621         FILE *file;
622         char buf[5000];
623
624         opt_pid = parse_number(str);
625
626         sprintf(buf, "/proc/%d/pagemap", opt_pid);
627         pagemap_fd = open(buf, O_RDONLY);
628         if (pagemap_fd < 0) {
629                 perror(buf);
630                 exit(EXIT_FAILURE);
631         }
632
633         sprintf(buf, "/proc/%d/maps", opt_pid);
634         file = fopen(buf, "r");
635         if (!file) {
636                 perror(buf);
637                 exit(EXIT_FAILURE);
638         }
639
640         while (fgets(buf, sizeof(buf), file) != NULL) {
641                 unsigned long vm_start;
642                 unsigned long vm_end;
643                 unsigned long long pgoff;
644                 int major, minor;
645                 char r, w, x, s;
646                 unsigned long ino;
647                 int n;
648
649                 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
650                            &vm_start,
651                            &vm_end,
652                            &r, &w, &x, &s,
653                            &pgoff,
654                            &major, &minor,
655                            &ino);
656                 if (n < 10) {
657                         fprintf(stderr, "unexpected line: %s\n", buf);
658                         continue;
659                 }
660                 pg_start[nr_vmas] = vm_start / page_size;
661                 pg_end[nr_vmas] = vm_end / page_size;
662                 if (++nr_vmas >= MAX_VMAS) {
663                         fprintf(stderr, "too many VMAs\n");
664                         break;
665                 }
666         }
667         fclose(file);
668 }
669
670 static void parse_file(const char *name)
671 {
672 }
673
674 static void parse_addr_range(const char *optarg)
675 {
676         unsigned long offset;
677         unsigned long size;
678         char *p;
679
680         p = strchr(optarg, ',');
681         if (!p)
682                 p = strchr(optarg, '+');
683
684         if (p == optarg) {
685                 offset = 0;
686                 size   = parse_number(p + 1);
687         } else if (p) {
688                 offset = parse_number(optarg);
689                 if (p[1] == '\0')
690                         size = ULONG_MAX;
691                 else {
692                         size = parse_number(p + 1);
693                         if (*p == ',') {
694                                 if (size < offset)
695                                         fatal("invalid range: %lu,%lu\n",
696                                                         offset, size);
697                                 size -= offset;
698                         }
699                 }
700         } else {
701                 offset = parse_number(optarg);
702                 size   = 1;
703         }
704
705         add_addr_range(offset, size);
706 }
707
708 static void add_bits_filter(uint64_t mask, uint64_t bits)
709 {
710         if (nr_bit_filters >= MAX_BIT_FILTERS)
711                 fatal("too much bit filters\n");
712
713         opt_mask[nr_bit_filters] = mask;
714         opt_bits[nr_bit_filters] = bits;
715         nr_bit_filters++;
716 }
717
718 static uint64_t parse_flag_name(const char *str, int len)
719 {
720         int i;
721
722         if (!*str || !len)
723                 return 0;
724
725         if (len <= 8 && !strncmp(str, "compound", len))
726                 return BITS_COMPOUND;
727
728         for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
729                 if (!page_flag_names[i])
730                         continue;
731                 if (!strncmp(str, page_flag_names[i] + 2, len))
732                         return 1ULL << i;
733         }
734
735         return parse_number(str);
736 }
737
738 static uint64_t parse_flag_names(const char *str, int all)
739 {
740         const char *p    = str;
741         uint64_t   flags = 0;
742
743         while (1) {
744                 if (*p == ',' || *p == '=' || *p == '\0') {
745                         if ((*str != '~') || (*str == '~' && all && *++str))
746                                 flags |= parse_flag_name(str, p - str);
747                         if (*p != ',')
748                                 break;
749                         str = p + 1;
750                 }
751                 p++;
752         }
753
754         return flags;
755 }
756
757 static void parse_bits_mask(const char *optarg)
758 {
759         uint64_t mask;
760         uint64_t bits;
761         const char *p;
762
763         p = strchr(optarg, '=');
764         if (p == optarg) {
765                 mask = KPF_ALL_BITS;
766                 bits = parse_flag_names(p + 1, 0);
767         } else if (p) {
768                 mask = parse_flag_names(optarg, 0);
769                 bits = parse_flag_names(p + 1, 0);
770         } else if (strchr(optarg, '~')) {
771                 mask = parse_flag_names(optarg, 1);
772                 bits = parse_flag_names(optarg, 0);
773         } else {
774                 mask = parse_flag_names(optarg, 0);
775                 bits = KPF_ALL_BITS;
776         }
777
778         add_bits_filter(mask, bits);
779 }
780
781
782 static struct option opts[] = {
783         { "raw"       , 0, NULL, 'r' },
784         { "pid"       , 1, NULL, 'p' },
785         { "file"      , 1, NULL, 'f' },
786         { "addr"      , 1, NULL, 'a' },
787         { "bits"      , 1, NULL, 'b' },
788         { "list"      , 0, NULL, 'l' },
789         { "list-each" , 0, NULL, 'L' },
790         { "no-summary", 0, NULL, 'N' },
791         { "help"      , 0, NULL, 'h' },
792         { NULL        , 0, NULL, 0 }
793 };
794
795 int main(int argc, char *argv[])
796 {
797         int c;
798
799         page_size = getpagesize();
800
801         while ((c = getopt_long(argc, argv,
802                                 "rp:f:a:b:lLNh", opts, NULL)) != -1) {
803                 switch (c) {
804                 case 'r':
805                         opt_raw = 1;
806                         break;
807                 case 'p':
808                         parse_pid(optarg);
809                         break;
810                 case 'f':
811                         parse_file(optarg);
812                         break;
813                 case 'a':
814                         parse_addr_range(optarg);
815                         break;
816                 case 'b':
817                         parse_bits_mask(optarg);
818                         break;
819                 case 'l':
820                         opt_list = 1;
821                         break;
822                 case 'L':
823                         opt_list = 2;
824                         break;
825                 case 'N':
826                         opt_no_summary = 1;
827                         break;
828                 case 'h':
829                         usage();
830                         exit(0);
831                 default:
832                         usage();
833                         exit(1);
834                 }
835         }
836
837         if (opt_list && opt_pid)
838                 printf("voffset\t");
839         if (opt_list == 1)
840                 printf("offset\tlen\tflags\n");
841         if (opt_list == 2)
842                 printf("offset\tflags\n");
843
844         walk_addr_ranges();
845
846         if (opt_list == 1)
847                 show_page_range(0, 0);  /* drain the buffer */
848
849         if (opt_no_summary)
850                 return 0;
851
852         if (opt_list)
853                 printf("\n\n");
854
855         show_summary();
856
857         return 0;
858 }