Skip to content

Commit 8d7055d

Browse files
committed
Add support for page size other than 4kB
1 parent b58f2b8 commit 8d7055d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

memcr.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ static int checksum;
126126
static int service;
127127
static unsigned int timeout;
128128

129+
static unsigned int page_size;
129130

130131
#define BIT(x) (1ULL << x)
131132

@@ -150,7 +151,7 @@ static int nr_threads;
150151
static struct vm_area vmas[MAX_VMAS];
151152
static int nr_vmas;
152153

153-
#define MAX_VM_REGION_SIZE (256 * PAGE_SIZE)
154+
#define MAX_VM_REGION_SIZE (1 * 1024 * 1024)
154155

155156
#ifdef COMPRESS_LZ4
156157
#define MAX_LZ4_DST_SIZE LZ4_compressBound(MAX_VM_REGION_SIZE)
@@ -1006,13 +1007,13 @@ static int is_checkpoint_aborted(void)
10061007

10071008
static int vm_region_valid(const struct vm_region *vmr)
10081009
{
1009-
if (vmr->addr & (PAGE_SIZE - 1)) {
1010-
fprintf(stderr, "[-] vm region addr %lx is not page aligned (off by 0x%lx)\n", vmr->addr, vmr->addr & (PAGE_SIZE - 1));
1010+
if (vmr->addr & (page_size - 1)) {
1011+
fprintf(stderr, "[-] vm region addr %lx is not page aligned (off by 0x%lx)\n", vmr->addr, vmr->addr & (page_size - 1));
10111012
return 0;
10121013
}
10131014

1014-
if (vmr->len % PAGE_SIZE) {
1015-
fprintf(stderr, "[-] vm region len %ld is not multiple of page size %d\n", vmr->len, (int)PAGE_SIZE);
1015+
if (vmr->len % page_size) {
1016+
fprintf(stderr, "[-] vm region len %ld is not multiple of page size %u\n", vmr->len, page_size);
10161017
return 0;
10171018
}
10181019

@@ -1267,7 +1268,7 @@ static int scan_target_vmas(pid_t pid, struct vm_area vmas[], int *nr_vmas)
12671268
}
12681269

12691270
/* parasite vma */
1270-
if (should_skip_range(start, end + PAGE_SIZE))
1271+
if (should_skip_range(start, end + page_size))
12711272
continue;
12721273

12731274
if (file_path[0] == '/') {
@@ -1383,7 +1384,7 @@ static int get_vm_region(int md, int cd, unsigned long addr, unsigned long len,
13831384
if (proc_mem) { /* read region from /proc/pid/mem */
13841385
off = lseek(md, addr, SEEK_SET);
13851386
if (off != addr) {
1386-
fprintf(stderr, "lseek() off %lu: %m\n", (unsigned long)off);
1387+
fprintf(stderr, "lseek() off %lu: %m\n", off);
13871388
return -1;
13881389
}
13891390

@@ -1454,9 +1455,9 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
14541455
unsigned long region_start = 0;
14551456
unsigned long region_length = 0;
14561457

1457-
nrpages = (vma->end - vma->start) / PAGE_SIZE;
1458+
nrpages = (vma->end - vma->start) / page_size;
14581459

1459-
idx = vma->start / PAGE_SIZE;
1460+
idx = vma->start / page_size;
14601461
off = idx * sizeof(uint64_t);
14611462
off = lseek(pd, off, SEEK_SET);
14621463
if (off != idx * sizeof(uint64_t)) {
@@ -1468,7 +1469,7 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
14681469
uint64_t map;
14691470
unsigned long addr;
14701471

1471-
addr = vma->start + idx * PAGE_SIZE;
1472+
addr = vma->start + idx * page_size;
14721473

14731474
ret = read(pd, &map, sizeof(map));
14741475
if (ret != sizeof(map)) {
@@ -1493,7 +1494,7 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
14931494
if (!region_start)
14941495
region_start = addr;
14951496

1496-
region_length += PAGE_SIZE;
1497+
region_length += page_size;
14971498

14981499
if ((idx + 1) < nrpages && region_length < MAX_VM_REGION_SIZE)
14991500
continue;
@@ -1518,7 +1519,7 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
15181519
if (!region_start)
15191520
region_start = addr;
15201521

1521-
region_length += PAGE_SIZE;
1522+
region_length += page_size;
15221523

15231524
if ((idx + 1) < nrpages)
15241525
continue;
@@ -1539,9 +1540,9 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
15391540
}
15401541

15411542
if (nrpages_dumpable) {
1542-
fprintf(stdout, "[i] %0*lx..%0*lx %s %6ld kB", 2 * (int)sizeof(unsigned long), vma->start, 2 * (int)sizeof(unsigned long), vma->end, flag_desc[vma->flags], (nrpages_dumpable * PAGE_SIZE) / 1024);
1543+
fprintf(stdout, "[i] %0*lx..%0*lx %s %6ld kB", 2 * (int)sizeof(unsigned long), vma->start, 2 * (int)sizeof(unsigned long), vma->end, flag_desc[vma->flags], (nrpages_dumpable * page_size) / 1024);
15431544
if (nrpages_unevictable)
1544-
fprintf(stdout, " (unevictable %ld kB)", (nrpages_unevictable * PAGE_SIZE) / 1024);
1545+
fprintf(stdout, " (unevictable %ld kB)", (nrpages_unevictable * page_size) / 1024);
15451546
fprintf(stdout, "\n");
15461547
}
15471548

@@ -2151,7 +2152,7 @@ static int ctx_save(pid_t pid)
21512152
* beginning of the page, there should be at least one page of
21522153
* space. Determine the position and save the original code.
21532154
*/
2154-
ctx.pc = (void *)round_down((unsigned long)get_cpu_regs_pc(&regs), PAGE_SIZE);
2155+
ctx.pc = (void *)round_down((unsigned long)get_cpu_regs_pc(&regs), page_size);
21552156
peek(ctx.pid, ctx.pc, ctx.code, ctx.code_size);
21562157

21572158
/*
@@ -2993,6 +2994,8 @@ int main(int argc, char *argv[])
29932994

29942995
print_version();
29952996

2997+
page_size = getpagesize();
2998+
29962999
ret = access("/proc/self/pagemap", F_OK);
29973000
if (ret)
29983001
die("/proc/self/pagemap not present (depends on CONFIG_PROC_PAGE_MONITOR)\n");

0 commit comments

Comments
 (0)