Guest User

Untitled

a guest
Feb 26th, 2023
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #define _POSIX_C_SOURCE 200809L
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #define PAGE_SIZE 0x1000
  10.  
  11. #define FIND_LIB_NAME
  12.  
  13. static void print_page(uint64_t address, uint64_t data,
  14.     const char *lib_name) {
  15.  
  16.     printf("0x%-16lx : pfn %-16lx soft-dirty %ld file/shared %ld "
  17.         "swapped %ld present %ld library %s\n",
  18.         address,
  19.         data & 0x7fffffffffffff,
  20.         (data >> 55) & 1,
  21.         (data >> 61) & 1,
  22.         (data >> 62) & 1,
  23.         (data >> 63) & 1,
  24.         lib_name);
  25. }
  26.  
  27. void handle_virtual_range(int pagemap, uint64_t start_address,
  28.     uint64_t end_address, const char *lib_name) {
  29.  
  30.     for(uint64_t i = start_address; i < end_address; i += 0x1000) {
  31.         uint64_t data;
  32.         uint64_t index = (i / PAGE_SIZE) * sizeof(data);
  33.         if(pread(pagemap, &data, sizeof(data), index) != sizeof(data)) {
  34.             if(errno) perror("pread");
  35.             break;
  36.         }
  37.  
  38.         print_page(i, data, lib_name);
  39.     }
  40. }
  41.  
  42. void parse_maps(const char *maps_file, const char *pagemap_file) {
  43.     int maps = open(maps_file, O_RDONLY);
  44.     if(maps < 0) return;
  45.  
  46.     int pagemap = open(pagemap_file, O_RDONLY);
  47.     if(pagemap < 0) {
  48.         close(maps);
  49.         return;
  50.     }
  51.  
  52.     char buffer[BUFSIZ];
  53.     int offset = 0;
  54.  
  55.     for(;;) {
  56.         ssize_t length = read(maps, buffer + offset, sizeof buffer - offset);
  57.         if(length <= 0) break;
  58.  
  59.         length += offset;
  60.  
  61.         for(size_t i = offset; i < (size_t)length; i ++) {
  62.             uint64_t low = 0, high = 0;
  63.             if(buffer[i] == '\n' && i) {
  64.                 size_t x = i - 1;
  65.                 while(x && buffer[x] != '\n') x --;
  66.                 if(buffer[x] == '\n') x ++;
  67.                 size_t beginning = x;
  68.  
  69.                 while(buffer[x] != '-' && x+1 < sizeof buffer) {
  70.                     char c = buffer[x ++];
  71.                     low *= 16;
  72.                     if(c >= '0' && c <= '9') {
  73.                         low += c - '0';
  74.                     }
  75.                     else if(c >= 'a' && c <= 'f') {
  76.                         low += c - 'a' + 10;
  77.                     }
  78.                     else break;
  79.                 }
  80.  
  81.                 while(buffer[x] != '-' && x+1 < sizeof buffer) x ++;
  82.                 if(buffer[x] == '-') x ++;
  83.  
  84.                 while(buffer[x] != ' ' && x+1 < sizeof buffer) {
  85.                     char c = buffer[x ++];
  86.                     high *= 16;
  87.                     if(c >= '0' && c <= '9') {
  88.                         high += c - '0';
  89.                     }
  90.                     else if(c >= 'a' && c <= 'f') {
  91.                         high += c - 'a' + 10;
  92.                     }
  93.                     else break;
  94.                 }
  95.  
  96.                 const char *lib_name = 0;
  97. #ifdef FIND_LIB_NAME
  98.                 for(int field = 0; field < 4; field ++) {
  99.                     x ++;  // skip space
  100.                     while(buffer[x] != ' ' && x+1 < sizeof buffer) x ++;
  101.                 }
  102.                 while(buffer[x] == ' ' && x+1 < sizeof buffer) x ++;
  103.  
  104.                 size_t y = x;
  105.                 while(buffer[y] != '\n' && y+1 < sizeof buffer) y ++;
  106.                 buffer[y] = 0;
  107.  
  108.                 lib_name = buffer + x;
  109. #endif
  110.  
  111.                 handle_virtual_range(pagemap, low, high, lib_name);
  112.  
  113. #ifdef FIND_LIB_NAME
  114.                 buffer[y] = '\n';
  115. #endif
  116.             }
  117.         }
  118.     }
  119.  
  120.     close(maps);
  121.     close(pagemap);
  122. }
  123.  
  124.  
  125. void process_pid(pid_t pid) {
  126.     char maps_file[BUFSIZ];
  127.     char pagemap_file[BUFSIZ];
  128.     snprintf(maps_file, sizeof(maps_file),
  129.         "/proc/%lu/maps", (uint64_t)pid);
  130.     snprintf(pagemap_file, sizeof(pagemap_file),
  131.         "/proc/%lu/pagemap", (uint64_t)pid);
  132.  
  133.     parse_maps(maps_file, pagemap_file);
  134. }
  135.  
  136. int main(int argc, char *argv[]) {
  137.     if(argc < 2) {
  138.         printf("Usage: %s pid1 [pid2...]\n", argv[0]);
  139.         return 1;
  140.     }
  141.  
  142.     for(int i = 1; i < argc; i ++) {
  143.         pid_t pid = (pid_t)strtoul(argv[i], NULL, 0);
  144.  
  145.         printf("=== Maps for pid %d\n", (int)pid);
  146.         process_pid(pid);
  147.     }
  148.  
  149.     return 0;
  150. }
  151.  
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment