-
Notifications
You must be signed in to change notification settings - Fork 4
/
pagemon.c
1781 lines (1615 loc) · 43 KB
/
pagemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Colin Ian King 2015-2024
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* colin.i.king@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include <libgen.h>
#include <inttypes.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>
#include <ncurses.h>
#include <dirent.h>
#include <libgen.h>
#include <ctype.h>
#include <setjmp.h>
#include "perf.h"
#define APP_NAME "pagemon"
#define MAX_MAPS (65536)
#define ADDR_OFFSET (17) /* Display x offset from address */
#define HEX_WIDTH (3) /* Width of each 2 hex digit value */
#define VIEW_PAGE (0) /* View pages in memory map */
#define VIEW_MEM (1) /* View memory in hex */
#define MIN_TICKS (1)
#define MAX_TICKS (1000)
#define MIN_ZOOM (1)
#define MAX_ZOOM (999)
#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b))
#define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
#define DEFAULT_UDELAY (15000) /* Delay between each refresh */
#define DEFAULT_TICKS (60) /* Ticks between dirty page checks */
#define PROCPATH_MAX (32) /* Size of proc pathnames */
#define BLINK_MASK (0x20) /* Cursor blink counter mask */
/*
* Memory size scaling
*/
#define KB (1024ULL)
#define MB (KB * KB)
#define GB (KB * KB * KB)
#define TB (KB * KB * KB * KB)
/*
* Error returns
*/
#define OK (0)
#define ERR_NO_MAP_INFO (-1)
#define ERR_NO_MEM_INFO (-2)
#define ERR_SMALL_WIN (-3)
#define ERR_ALLOC_NOMEM (-4)
#define ERR_TOO_MANY_PAGES (-5)
#define ERR_TOO_FEW_PAGES (-6)
#define ERR_RESIZE_FAIL (-7)
#define ERR_NO_PROCESS (-8)
#define ERR_FAULT (-9)
/*
* PTE bits from uint64_t in /proc/PID/pagemap
* for each mapped page
*/
#define PAGE_PTE_SOFT_DIRTY (1ULL << 55)
#define PAGE_EXCLUSIVE_MAPPED (1ULL << 56)
#define PAGE_FILE_SHARED_ANON (1ULL << 61)
#define PAGE_SWAPPED (1ULL << 62)
#define PAGE_PRESENT (1ULL << 63)
#define OPT_FLAG_READ_ALL_PAGES (0x00000001)
#define OPT_FLAG_PID (0x00000002)
enum {
WHITE_RED = 1,
WHITE_BLUE,
WHITE_YELLOW,
WHITE_CYAN,
WHITE_GREEN,
WHITE_BLACK,
RED_BLUE,
BLACK_WHITE,
BLACK_BLACK,
BLUE_WHITE,
};
/*
* Note that we use 64 bit addresses even for 32 bit systems since
* this allows pagemon to run in a 32 bit chroot and still access
* the 64 bit mapping info. Otherwise I'd use uintptr_t instead.
*/
typedef uint64_t addr_t; /* Addresses */
typedef int64_t index_t; /* Index into page tables */
typedef uint64_t pagemap_t; /* PTE page map bits */
typedef uint64_t checksum_t; /* Pagemap checksum */
/*
* Memory map info, represents 1 or more pages
*/
typedef struct {
addr_t begin; /* Start of mapping */
addr_t end; /* End of mapping */
char attr[5]; /* Map attributes */
char dev[6]; /* Map device, if any */
char name[NAME_MAX + 1]; /* Name of mapping */
} map_t;
/*
* Page info, 1 per page with map reference
* to the memory map it belongs to
*/
typedef struct {
addr_t addr; /* Address */
map_t *map; /* Mapping it is in */
index_t index; /* Index into map */
} page_t;
/*
* General memory mapping info, containing
* a fix set of memory maps, and pointer
* to an array of per page info.
*/
typedef struct {
map_t maps[MAX_MAPS]; /* Mappings */
uint32_t nmaps; /* Number of mappings */
page_t *pages; /* Pages */
addr_t npages; /* Number of pages */
addr_t last_addr; /* Last address */
} mem_info_t;
/*
* Cursor context, we have one each for the
* memory map and page contents views
*/
typedef struct {
int32_t xpos; /* Cursor x position */
int32_t ypos; /* Cursor y position */
int32_t xpos_prev; /* Previous x position */
int32_t ypos_prev; /* Previous y position */
int32_t ypos_max; /* Max y position */
int32_t xmax; /* Width */
int32_t ymax; /* Height */
} position_t;
/*
* Globals, stashed in a global struct
*/
typedef struct {
WINDOW *mainwin; /* curses main window */
sigjmp_buf env; /* terminate abort jmp */
addr_t max_pages; /* Max pages in system */
checksum_t checksum; /* Pagemap check sum */
checksum_t prev_checksum; /* Previous checksum */
uint32_t page_size; /* Page size in bytes */
pid_t pid; /* Process ID */
mem_info_t mem_info; /* Mapping and page info */
#if defined(PERF_ENABLED)
perf_t perf; /* Perf context */
#endif
bool curses_started; /* Are we in curses mode? */
bool tab_view; /* Page pop-up info */
bool vm_view; /* Process VM stats */
bool help_view; /* Help pop-up info */
bool resized; /* SIGWINCH occurred */
bool terminate; /* SIGSEGV termination */
bool auto_zoom; /* Automatic zoom */
#if defined(PERF_ENABLED)
bool perf_view; /* Perf statistics */
#endif
uint8_t view; /* Default page or memory view */
uint8_t opt_flags; /* User option flags */
char path_refs[PROCPATH_MAX]; /* /proc/$PID/clear_refs */
char path_pagemap[PROCPATH_MAX];/* /proc/$PID/pagemap */
char path_maps[PROCPATH_MAX]; /* /proc/$PID/maps */
char path_mem[PROCPATH_MAX]; /* /proc/$PID/mem */
char path_status[PROCPATH_MAX]; /* /proc/$PID/status */
char path_stat[PROCPATH_MAX]; /* /proc/$PID/stat */
char path_oom[PROCPATH_MAX]; /* /proc/$PID/oom_score */
} global_t;
static global_t g;
/*
* mem_to_str()
* report memory in different units
*/
static void mem_to_str(const addr_t addr, char *const buf, const size_t buflen)
{
uint64_t scaled, val = (uint64_t)addr;
char unit;
if (val < 99 * MB) {
scaled = val / KB;
unit = 'K';
} else if (val < 99 * GB) {
scaled = val / MB;
unit = 'M';
} else if (val < 99 * TB) {
scaled = val / GB;
unit = 'G';
} else {
scaled = val / TB;
unit = 'T';
}
(void)snprintf(buf, buflen, "%7" PRIu64 " %c", scaled, unit);
}
/*
* read_buf()
* read data into a buffer, just
* for one liner /proc file reading
*/
static int read_buf(
const char *path,
char *const buffer,
const size_t sz)
{
int fd;
ssize_t ret;
if ((fd = open(path, O_RDONLY)) < 0)
return -1;
ret = read(fd, buffer, sz);
(void)close(fd);
if ((ret < 1) || (ret > (ssize_t)sz))
return -1;
buffer[ret - 1] = '\0';
return 0;
}
/*
* proc_name_to_pid()
* find a process by name, return PID of
* first match found. Zero indicates error
*/
static pid_t proc_name_to_pid(char *const name)
{
char *ptr;
bool isnum = true;
DIR *dir;
struct dirent *d;
pid_t pid = 0;
/* Could be just a pid in numeric form */
for (ptr = name; *ptr; ptr++) {
if (!isdigit(*ptr)) {
isnum = false;
break;
}
}
if (isnum) {
pid = (pid_t)strtol(name, NULL, 10);
if (errno || (pid < 1)) {
(void)fprintf(stderr, "Invalid pid value '%s'\n", name);
return 0;
}
return pid;
}
/* No, search for process name */
dir = opendir("/proc");
if (!dir)
return pid;
while ((d = readdir(dir)) != NULL) {
char path[PATH_MAX], buf[4096], *bn;
if (!isdigit(d->d_name[0]))
continue;
(void)snprintf(path, sizeof(path), "/proc/%s/cmdline",
d->d_name);
if (read_buf(path, buf, sizeof(buf)) < 0)
continue;
bn = basename(buf);
if (!bn)
continue;
if (!strcmp(name, bn)) {
pid = (pid_t)strtol(d->d_name, NULL, 10);
if ((errno == 0) && (pid > 0))
break;
}
}
(void)closedir(dir);
if (!pid)
(void)fprintf(stderr, "Cannot find process '%s'\n", name);
return pid;
}
/*
* get_proc_self_stat_field()
* find nth field of /proc/$PID/stat data. This works around
* the problem that the comm field can contain spaces and
* multiple ) so sscanf on this field won't work. The returned
* pointer is the start of the Nth field and it is up to the
* caller to determine the end of the field
*/
static const char *get_proc_self_stat_field(const char *buf, const int num)
{
const char *ptr, *comm_end;
int n;
if (num < 1 || !buf || !*buf)
return NULL;
if (num == 1)
return buf;
if (num == 2)
return strstr(buf, "(");
comm_end = NULL;
for (ptr = buf; *ptr; ptr++) {
if (*ptr == ')')
comm_end = ptr;
}
if (!comm_end)
return NULL;
comm_end++;
n = num - 2;
ptr = comm_end;
while (*ptr) {
while (*ptr && *ptr == ' ')
ptr++;
n--;
if (n <= 0)
break;
while (*ptr && *ptr != ' ')
ptr++;
}
return ptr;
}
/*
* read_faults()
* read minor and major page faults
*/
static int read_faults(
uint64_t *const minor_flt,
uint64_t *const major_flt)
{
char buf[4096];
const char *ptr;
*minor_flt = 0;
*major_flt = 0;
if (read_buf(g.path_stat, buf, sizeof(buf)) < 0)
return -1;
ptr = get_proc_self_stat_field(buf, 10);
if (!ptr)
return -1;
if (sscanf(ptr, "%" SCNu64 " %*u %" SCNu64, minor_flt, major_flt) != 2)
return -1;
return 0;
}
/*
* read_oom_score()
* read the process oom score
*/
static int read_oom_score(uint64_t *const score)
{
char buf[4096];
*score = ~0ULL;
if (read_buf(g.path_oom, buf, sizeof(buf)) < 0)
return -1;
if (sscanf(buf, "%" SCNu64, score) != 1)
return -1;
return 0;
}
/*
* read_maps()
* read memory maps for a specific process
*/
static int read_maps(const bool force)
{
FILE *fp;
uint32_t i, j, n = 0;
char buffer[4096];
page_t *page;
checksum_t checksum = 0ULL;
map_t *map;
g.mem_info.npages = 0;
g.mem_info.last_addr = 0;
if (kill(g.pid, 0) < 0)
return ERR_NO_PROCESS;
if (force)
g.prev_checksum = 0;
fp = fopen(g.path_maps, "r");
if (fp == NULL)
return ERR_NO_MAP_INFO;
map = g.mem_info.maps;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
int ret;
intptr_t length;
map->name[0] = '\0';
ret = sscanf(buffer, "%" SCNx64 "-%" SCNx64
" %5s %*s %6s %*d %s",
&map->begin,
&map->end,
map->attr,
map->dev,
map->name);
if ((ret != 5) && (ret != 4))
continue;
if (ret == 4)
map->name[0]= '\0';
/* Simple sanity check */
if (map->end < map->begin)
continue;
length = map->end - map->begin;
/* Check for overflow */
if (g.mem_info.npages + length < g.mem_info.npages)
continue;
if (g.mem_info.last_addr < map->end)
g.mem_info.last_addr = map->end;
checksum ^= map->begin;
checksum <<= 1;
checksum ^= map->end;
checksum <<= 1;
checksum ^= map->attr[0];
checksum <<= 1;
checksum ^= map->attr[1];
checksum <<= 1;
checksum ^= map->attr[2];
checksum <<= 1;
checksum ^= map->attr[3];
checksum <<= 1;
checksum ^= length;
g.mem_info.npages += length / g.page_size;
n++;
map++;
if (n >= MAX_MAPS)
break;
}
(void)fclose(fp);
checksum += g.mem_info.npages;
checksum += n;
g.checksum = checksum;
/* No change in maps, so nothing to do */
if (g.checksum == g.prev_checksum)
return OK;
g.prev_checksum = checksum;
/* Unlikely, but need to keep Coverity Scan happy */
if (g.mem_info.npages > g.max_pages)
return ERR_TOO_MANY_PAGES;
if (g.mem_info.npages == 0)
return ERR_TOO_FEW_PAGES;
free(g.mem_info.pages);
g.mem_info.nmaps = n;
g.mem_info.pages = calloc(g.mem_info.npages, sizeof(page_t));
if (!g.mem_info.pages) {
g.mem_info.nmaps = 0;
return ERR_ALLOC_NOMEM;
}
map = g.mem_info.maps;
page = g.mem_info.pages;
for (i = 0; i < g.mem_info.nmaps; i++, map++) {
addr_t addr = map->begin;
addr_t count = (map->end - map->begin) / g.page_size;
for (j = 0; j < count; j++, page++) {
page->addr = addr;
page->map = map;
page->index = i;
addr += g.page_size;
}
}
return (n == 0) ? ERR_NO_MAP_INFO : OK;
}
/*
* handle_winch()
* handle SIGWINCH, flag a window resize
*/
static void handle_winch(int sig)
{
(void)sig;
g.resized = true;
}
/*
* handle_terminate()
* handle termination signals
*/
static void handle_terminate(int sig)
{
static bool already_handled = false;
(void)sig;
if (already_handled)
exit(EXIT_FAILURE);
g.terminate = true;
siglongjmp(g.env, 1);
}
/*
* show_usage()
* mini help info
*/
static void show_usage(void)
{
(void)printf(APP_NAME ", version " VERSION "\n\n"
"Usage: " APP_NAME " [options]\n"
" -a enable automatic zoom mode\n"
" -d delay in microseconds between refreshes, "
"default %u\n"
" -h help\n"
" -p pid process ID to monitor\n"
" -r read (page back in) pages at start\n"
" -t ticks ticks between dirty page checks\n"
" -v enable VM view\n"
" -z zoom set page zoom scale\n",
DEFAULT_UDELAY);
}
#if defined(PERF_ENABLED)
/*
* show_perf()
* show perf stats
*/
static void show_perf(void)
{
int y = LINES - 6;
const int x = 2;
static uint8_t perf_ticker = 0;
/* Don't hammer perf to death */
if (++perf_ticker > 10) {
perf_stop(&g.perf);
perf_start(&g.perf, g.pid);
perf_ticker = 0;
}
(void)wattrset(g.mainwin, COLOR_PAIR(WHITE_CYAN) | A_BOLD);
(void)mvwprintw(g.mainwin, y + 0, x,
" Page Faults (User Space): %15" PRIu64 " ",
perf_counter(&g.perf, PERF_TP_PAGE_FAULT_USER));
(void)mvwprintw(g.mainwin, y + 1, x,
" Page Faults (Kernel Space): %15" PRIu64 " ",
perf_counter(&g.perf, PERF_TP_PAGE_FAULT_KERNEL));
(void)mvwprintw(g.mainwin, y + 2, x,
" Kernel Page Allocate: %15" PRIu64 " ",
perf_counter(&g.perf, PERF_TP_MM_PAGE_ALLOC));
(void)mvwprintw(g.mainwin, y + 3, x,
" Kernel Page Free: %15" PRIu64 " ",
perf_counter(&g.perf, PERF_TP_MM_PAGE_FREE));
}
#endif
/*
* show_vm()
* show Virtual Memory stats
*/
static void show_vm(void)
{
FILE *fp;
char buffer[4096];
int y = 2;
const int x = COLS - 26;
uint64_t major, minor, score;
fp = fopen(g.path_status, "r");
if (fp == NULL)
return;
(void)wattrset(g.mainwin, COLOR_PAIR(WHITE_BLUE) | A_BOLD);
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
char vmname[9], size[8];
char state[6], longstate[13];
uint64_t sz;
if (sscanf(buffer, "State: %5s %12s", state, longstate) == 2) {
(void)mvwprintw(g.mainwin, y++, x,
" State: %-12.12s ", longstate);
continue;
}
if (sscanf(buffer, "Vm%8s %" SCNu64 "%7s",
vmname, &sz, size) == 3) {
(void)mvwprintw(g.mainwin, y++, x,
" Vm%-6.6s %10" PRIu64 " %s ",
vmname, sz, size);
continue;
}
}
(void)fclose(fp);
if (!read_faults(&minor, &major)) {
(void)mvwprintw(g.mainwin, y++, x, " %-23s", "Page Faults:");
(void)mvwprintw(g.mainwin, y++, x,
" Minor: %12" PRIu64 " ", minor);
(void)mvwprintw(g.mainwin, y++, x,
" Major: %12" PRIu64 " ", major);
}
if (!read_oom_score(&score)) {
(void)mvwprintw(g.mainwin, y, x,
" OOM Score: %8" PRIu64 " ", score);
}
}
/*
* show_page_bits()
* show info based on the page bit pattern
*/
static void show_page_bits(
const int fd,
map_t *const map,
const index_t idx)
{
pagemap_t pagemap_info;
off_t offset;
char buf[16];
const int x = 2;
int kfd;
mem_to_str(map->end - map->begin, buf, sizeof(buf) - 1);
(void)wattrset(g.mainwin, COLOR_PAIR(WHITE_BLUE) | A_BOLD);
(void)mvwprintw(g.mainwin, 2, x,
" Page: 0x%16.16" PRIx64 "%18s",
g.mem_info.pages[idx].addr, "");
(void)mvwprintw(g.mainwin, 3, x,
" Page Size: 0x%8.8" PRIx32 " bytes%20s",
g.page_size, "");
(void)mvwprintw(g.mainwin, 4, x,
" Map: 0x%16.16" PRIx64 "-%16.16" PRIx64 " ",
map->begin, map->end - 1);
(void)mvwprintw(g.mainwin, 5, x,
" Map Size: %s%27s", buf, "");
(void)mvwprintw(g.mainwin, 6, x,
" Device: %5.5s%31s",
map->dev, "");
(void)mvwprintw(g.mainwin, 7, x,
" Prot: %4.4s%32s",
map->attr, "");
(void)mvwprintw(g.mainwin, 8, x,
" Map Name: %-35.35s ", map->name[0] == '\0' ?
"[Anonymous]" : basename(map->name));
offset = sizeof(pagemap_t) *
(g.mem_info.pages[idx].addr / g.page_size);
if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
return;
if (read(fd, &pagemap_info, sizeof(pagemap_info)) != sizeof(pagemap_info))
return;
(void)mvwprintw(g.mainwin, 9, x,
" Flag: 0x%16.16" PRIx64 "%18s", pagemap_info, "");
if (pagemap_info & PAGE_SWAPPED) {
(void)mvwprintw(g.mainwin, 10, x,
" Swap Type: 0x%2.2" PRIx64 "%22s",
pagemap_info & 0x1f, "");
(void)mvwprintw(g.mainwin, 11, x,
" Swap Offset: 0x%16.16" PRIx64 "%8s",
(uint64_t)(pagemap_info & 0x00ffffffffffffffULL) >> 5, "");
} else {
(void)mvwprintw(g.mainwin, 10, x, "%48s", "");
if (pagemap_info & PAGE_PRESENT) {
(void)mvwprintw(g.mainwin, 11, x,
" Physical Address: 0x%16.16" PRIx64 "%8s",
(uint64_t)(pagemap_info & 0x00ffffffffffffffULL) * g.page_size, "");
} else {
(void)mvwprintw(g.mainwin, 11, x,
" Physical Address: 0x----------------%8s", "");
}
}
(void)mvwprintw(g.mainwin, 12, x,
" Soft-dirty PTE: %3s%23s",
(pagemap_info & PAGE_PTE_SOFT_DIRTY) ? "Yes" : "No ", "");
(void)mvwprintw(g.mainwin, 13, x,
" Exclusively Mapped: %3s%23s",
(pagemap_info & PAGE_EXCLUSIVE_MAPPED) ? "Yes" : "No ", "");
(void)mvwprintw(g.mainwin, 14, x,
" File or Shared Anon: %3s%23s",
(pagemap_info & PAGE_FILE_SHARED_ANON) ? "Yes" : "No ", "");
(void)mvwprintw(g.mainwin, 15, x,
" Present in Swap: %3s%23s",
(pagemap_info & PAGE_SWAPPED) ? "Yes" : "No ", "");
(void)mvwprintw(g.mainwin, 16, x,
" Present in RAM: %3s%23s",
(pagemap_info & PAGE_PRESENT) ? "Yes" : "No ", "");
kfd = open("/proc/kpagecount", O_RDONLY);
if (kfd > -1) {
uint64_t count;
offset = sizeof(count) *
(pagemap_info & 0x00ffffffffffffffULL);
if (lseek(kfd, offset, SEEK_SET) == (off_t)-1)
goto close_kfd;
if (read(kfd, &count, sizeof(count)) != sizeof(count))
goto close_kfd;
(void)mvwprintw(g.mainwin, 16, x,
" KPageCount: %-10" PRIu64 "%13s", count, "");
close_kfd:
(void)close(kfd);
}
}
/*
* banner()
* clear a banner across the window
*/
static inline void banner(const int y)
{
(void)mvwprintw(g.mainwin, y, 0, "%*s", COLS, "");
}
/*
* show_pages()
* show page mapping
*/
static int show_pages(
const index_t cursor_index,
const index_t page_index,
const position_t *const p,
const int32_t zoom)
{
int32_t i;
index_t idx;
const uint32_t shift = ((uint32_t)(8 * sizeof(pagemap_t) - 4 -
__builtin_clzll((g.page_size))));
int fd;
map_t *map;
const int32_t xmax = p->xmax, ymax = p->ymax;
pagemap_t pagemap_info_buf[xmax];
if ((fd = open(g.path_pagemap, O_RDONLY)) < 0)
return ERR_NO_MAP_INFO;
idx = page_index;
for (i = 1; i <= ymax; i++) {
int32_t j;
addr_t addr, offset;
const size_t sz = sizeof(pagemap_info_buf);
if (idx >= (index_t)g.mem_info.npages) {
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_BLACK));
(void)mvwprintw(g.mainwin, i, 0, "---------------- ");
} else {
addr = g.mem_info.pages[idx].addr;
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_WHITE));
(void)mvwprintw(g.mainwin, i, 0, "%16.16" PRIx64 " ", addr);
}
/*
* Slurp up an entire row
*/
(void)memset(pagemap_info_buf, 0, sz);
if (idx >= (index_t)g.mem_info.npages) {
map = NULL;
} else {
addr = g.mem_info.pages[idx].addr;
map = g.mem_info.pages[idx].map;
offset = (addr >> shift) & ~7ULL;
if (lseek(fd, offset, SEEK_SET) != (off_t)-1) {
ssize_t ret = read(fd, pagemap_info_buf, sz);
(void)ret;
}
}
for (j = 0; j < xmax; j++) {
char state = '.';
int attr;
if (idx >= (index_t)g.mem_info.npages) {
attr = COLOR_PAIR(BLACK_BLACK);
state = '~';
} else {
map_t *new_map;
register pagemap_t pagemap_info;
new_map = g.mem_info.pages[idx].map;
/*
* On a different mapping? If so, slurp up
* the new mappings from here to end
*/
if (new_map != map) {
map = new_map;
addr = g.mem_info.pages[idx].addr;
offset = (addr >> shift) & ~7;
if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
break;
if (read(fd, &pagemap_info_buf[j],
(xmax - j) * sizeof(pagemap_t)) < 0)
break;
}
__builtin_prefetch(&g.mem_info.pages[idx + zoom].addr, 1, 1);
pagemap_info = pagemap_info_buf[j];
attr = COLOR_PAIR(BLACK_WHITE);
if (pagemap_info & PAGE_PRESENT) {
attr = COLOR_PAIR(WHITE_YELLOW);
state = 'P';
}
if (pagemap_info & PAGE_SWAPPED) {
attr = COLOR_PAIR(WHITE_GREEN);
state = 'S';
}
if (pagemap_info & PAGE_FILE_SHARED_ANON) {
attr = COLOR_PAIR(WHITE_RED);
state = 'M';
}
if (pagemap_info & PAGE_PTE_SOFT_DIRTY) {
attr = COLOR_PAIR(WHITE_CYAN);
state = 'D';
}
idx += zoom;
}
(void)wattrset(g.mainwin, attr);
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET + j, "%c", state);
}
}
(void)wattrset(g.mainwin, A_NORMAL);
map = g.mem_info.pages[cursor_index].map;
if (map && g.tab_view)
show_page_bits(fd, map, cursor_index);
if (g.vm_view)
show_vm();
#if defined(PERF_ENABLED)
if (g.perf_view)
show_perf();
#endif
(void)close(fd);
return 0;
}
/*
* show_memory()
* show memory contents
*/
static int show_memory(
const index_t page_index,
index_t data_index,
const position_t *const p)
{
addr_t addr;
index_t idx = page_index;
int32_t i;
const int32_t xmax = p->xmax, ymax = p->ymax;
int fd;
if ((fd = open(g.path_mem, O_RDONLY)) < 0)
return ERR_NO_MEM_INFO;
for (i = 1; i <= ymax; i++) {
int32_t j;
uint8_t bytes[xmax];
ssize_t nread = 0;
addr = g.mem_info.pages[idx].addr + data_index;
if (lseek(fd, (off_t)addr, SEEK_SET) == (off_t)-1) {
nread = -1;
} else {
nread = read(fd, bytes, (size_t)xmax);
if (nread < 0)
nread = -1;
}
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_WHITE));
if (idx >= (index_t)g.mem_info.npages)
(void)mvwprintw(g.mainwin, i, 0, "---------------- ");
else
(void)mvwprintw(g.mainwin, i, 0, "%16.16" PRIx64 " ", addr);
(void)mvwprintw(g.mainwin, i, COLS - 3, " ");
for (j = 0; j < xmax; j++) {
uint8_t byte;
addr = g.mem_info.pages[idx].addr + data_index;
if ((idx >= (index_t)g.mem_info.npages) ||
(addr > g.mem_info.last_addr)) {
/* End of memory */
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_BLACK));
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * j), " ");
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * xmax) + j, " ");
goto do_border;
}
if (j > nread) {
/* Failed to read data */
(void)wattrset(g.mainwin, COLOR_PAIR(WHITE_BLUE));
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * j), "?? ");
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_WHITE));
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * xmax) + j, "?");
goto do_border;
}
/* We have some legimate data to display */
byte = bytes[j];
(void)wattrset(g.mainwin, COLOR_PAIR(WHITE_BLUE));
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * j), "%2.2" PRIx8 " ", byte);
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_WHITE));
byte &= 0x7f;
(void)mvwprintw(g.mainwin, i, ADDR_OFFSET +
(HEX_WIDTH * xmax) + j, "%c",
(byte < 32 || byte > 126) ? '.' : byte);
do_border:
(void)wattrset(g.mainwin, COLOR_PAIR(BLACK_WHITE));
(void)mvwprintw(g.mainwin, i, 16 + (HEX_WIDTH * xmax), " ");
data_index++;
if (data_index >= g.page_size) {
data_index -= g.page_size;
idx++;
}
}
}
(void)close(fd);
return 0;
}
/*
* read_all_pages()
* read in all pages into memory, this
* will force swapped out pages back into
* memory
*/
static int read_all_pages(void)
{
int fd;
index_t idx;
if ((fd = open(g.path_mem, O_RDONLY)) < 0)
return ERR_NO_MEM_INFO;
for (idx = 0; idx < (index_t)g.mem_info.npages; idx++) {
const off_t addr = g.mem_info.pages[idx].addr;
uint8_t byte;
if (lseek(fd, addr, SEEK_SET) == (off_t)-1)
continue;