pagemap_dump.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Only tested in x86_64.
  3. Adapted from: https://github.com/dwks/pagemap/blob/8a25747bc79d6080c8b94eac80807a4dceeda57a/pagemap2.c
  4. - https://stackoverflow.com/questions/17021214/how-to-decode-proc-pid-pagemap-entries-in-linux/45126141#45126141
  5. - https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li
  6. - https://stackoverflow.com/questions/6284810/proc-pid-pagemaps-and-proc-pid-maps-linux/45500208#45500208
  7. Dump the page map of a given process PID.
  8. Data sources: /proc/PIC/{map,pagemap}
  9. This program works in two steps:
  10. - parse the human readable lines lines from `/proc/<pid>/maps`. This files contains lines of form:
  11. 7ffff7b6d000-7ffff7bdd000 r-xp 00000000 fe:00 658 /lib/libuClibc-1.0.22.so
  12. which gives us:
  13. - `7f8af99f8000-7f8af99ff000`: a virtual address range that belong to the process, possibly containing multiple pages.
  14. - `/lib/libuClibc-1.0.22.so` the name of the library that owns that memory.
  15. - loop over each page of each address range, and ask `/proc/<pid>/pagemap` for more information about that page, including the physical address.
  16. */
  17. #define _XOPEN_SOURCE 700
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #include "common.h" /* pagemap_get_entry */
  26. int main(int argc, char **argv)
  27. {
  28. char buffer[BUFSIZ];
  29. char maps_file[BUFSIZ];
  30. char pagemap_file[BUFSIZ];
  31. int maps_fd;
  32. int offset = 0;
  33. int pagemap_fd;
  34. pid_t pid;
  35. if (argc < 2) {
  36. printf("Usage: %s pid\n", argv[0]);
  37. return EXIT_FAILURE;
  38. }
  39. pid = strtoull(argv[1], NULL, 0);
  40. snprintf(maps_file, sizeof(maps_file), "/proc/%ju/maps", (uintmax_t)pid);
  41. snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t)pid);
  42. maps_fd = open(maps_file, O_RDONLY);
  43. if (maps_fd < 0) {
  44. perror("open maps");
  45. return EXIT_FAILURE;
  46. }
  47. pagemap_fd = open(pagemap_file, O_RDONLY);
  48. if (pagemap_fd < 0) {
  49. perror("open pagemap");
  50. return EXIT_FAILURE;
  51. }
  52. printf("addr pfn soft-dirty file/shared swapped present library\n");
  53. for (;;) {
  54. ssize_t length = read(maps_fd, buffer + offset, sizeof buffer - offset);
  55. if (length <= 0) break;
  56. length += offset;
  57. for (size_t i = offset; i < (size_t)length; i++) {
  58. uintptr_t low = 0, high = 0;
  59. if (buffer[i] == '\n' && i) {
  60. const char *lib_name;
  61. size_t y;
  62. /* Parse a line from maps. Each line contains a range that contains many pages. */
  63. {
  64. size_t x = i - 1;
  65. while (x && buffer[x] != '\n') x--;
  66. if (buffer[x] == '\n') x++;
  67. while (buffer[x] != '-' && x < sizeof buffer) {
  68. char c = buffer[x++];
  69. low *= 16;
  70. if (c >= '0' && c <= '9') {
  71. low += c - '0';
  72. } else if (c >= 'a' && c <= 'f') {
  73. low += c - 'a' + 10;
  74. } else {
  75. break;
  76. }
  77. }
  78. while (buffer[x] != '-' && x < sizeof buffer) x++;
  79. if (buffer[x] == '-') x++;
  80. while (buffer[x] != ' ' && x < sizeof buffer) {
  81. char c = buffer[x++];
  82. high *= 16;
  83. if (c >= '0' && c <= '9') {
  84. high += c - '0';
  85. } else if (c >= 'a' && c <= 'f') {
  86. high += c - 'a' + 10;
  87. } else {
  88. break;
  89. }
  90. }
  91. lib_name = 0;
  92. for (int field = 0; field < 4; field++) {
  93. x++;
  94. while(buffer[x] != ' ' && x < sizeof buffer) x++;
  95. }
  96. while (buffer[x] == ' ' && x < sizeof buffer) x++;
  97. y = x;
  98. while (buffer[y] != '\n' && y < sizeof buffer) y++;
  99. buffer[y] = 0;
  100. lib_name = buffer + x;
  101. }
  102. /* Get info about all pages in this page range with pagemap. */
  103. {
  104. PagemapEntry entry;
  105. for (uintptr_t addr = low; addr < high; addr += sysconf(_SC_PAGE_SIZE)) {
  106. /* TODO always fails for the last page (vsyscall), why? pread returns 0. */
  107. if (!pagemap_get_entry(&entry, pagemap_fd, addr)) {
  108. printf("%jx %jx %u %u %u %u %s\n",
  109. (uintmax_t)addr,
  110. (uintmax_t)entry.pfn,
  111. entry.soft_dirty,
  112. entry.file_page,
  113. entry.swapped,
  114. entry.present,
  115. lib_name
  116. );
  117. }
  118. }
  119. }
  120. buffer[y] = '\n';
  121. }
  122. }
  123. }
  124. close(maps_fd);
  125. close(pagemap_fd);
  126. return EXIT_SUCCESS;
  127. }