Created
August 10, 2011 08:04
-
-
Save domcleal/1136338 to your computer and use it in GitHub Desktop.
reads the page size for a virtual memory address of a given pid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pagesize: reads the page size for a virtual memory address of a given pid */ | |
#include <errno.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int main(int argc, char** argv) { | |
if (argc != 3) { | |
printf("Usage: %s <pid> <page>\n", argv[0]); | |
exit(1); | |
} | |
unsigned long page_addr; | |
if (!sscanf(argv[2], "%lx", &page_addr)) | |
errx(1, "can't parse page address"); | |
long page_size = sysconf(_SC_PAGESIZE); | |
if (page_addr % page_size != 0) | |
errx(1, "page address must be a multiple of the page size"); | |
char pmap_path[32]; | |
snprintf(pmap_path, sizeof(pmap_path), "/proc/%s/pagemap", argv[1]); | |
FILE* pagemap = fopen(pmap_path, "r"); | |
if (pagemap == NULL) | |
err(1, "%s", pmap_path); | |
/* pagemaps contains 64-bit entries per page */ | |
unsigned long page_offset = page_addr / page_size * 8; | |
lseek64(fileno(pagemap), page_offset, SEEK_SET); | |
uint64_t page; | |
fread(&page, sizeof(page), 1, pagemap); | |
fclose(pagemap); | |
/* bits 55-60 contain the page shift */ | |
int page_shift = page >> 55; | |
page_shift &= 0x40-1; | |
printf("page size = %ld bytes\n", 1 << page_shift); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment