diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-16 00:19:57 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-16 00:26:01 +0300 |
| commit | 5369407082db53b68a1100e7b5391a1e9c36e621 (patch) | |
| tree | 7a54bcf8d0c7b1737a65201a09fa7e06f1a4a8c0 /src/vm.c | |
| parent | Attaching & Detaching from process (diff) | |
| download | linux-game-trainer-5369407082db53b68a1100e7b5391a1e9c36e621.tar.gz linux-game-trainer-5369407082db53b68a1100e7b5391a1e9c36e621.zip | |
Parse /proc/pid/maps
Diffstat (limited to 'src/vm.c')
| -rw-r--r-- | src/vm.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/vm.c b/src/vm.c new file mode 100644 index 0000000..8e62426 --- /dev/null +++ b/src/vm.c | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <ctype.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "vm.h" | ||
| 7 | #include "util.h" | ||
| 8 | |||
| 9 | static void strtrim (char *str) | ||
| 10 | { | ||
| 11 | char *begin = str; | ||
| 12 | while (isspace(*begin)) { | ||
| 13 | begin++; | ||
| 14 | } | ||
| 15 | size_t len = strlen(begin); | ||
| 16 | char *end = begin + len; | ||
| 17 | while (end > begin && isspace(*end)) { | ||
| 18 | end++; | ||
| 19 | } | ||
| 20 | |||
| 21 | memmove(str, begin, end - begin + 1); | ||
| 22 | } | ||
| 23 | |||
| 24 | VMMapping* parse_vmmap (int pid) | ||
| 25 | { | ||
| 26 | char fmaps_path[128]; | ||
| 27 | sprintf(fmaps_path, "/proc/%d/maps", pid); | ||
| 28 | FILE *fmaps; | ||
| 29 | |||
| 30 | if ((fmaps = fopen(fmaps_path, "r")) == NULL) { | ||
| 31 | perror("fopen"); | ||
| 32 | exit(1); | ||
| 33 | } | ||
| 34 | |||
| 35 | uint64_t begin, end; | ||
| 36 | char perms[16]; | ||
| 37 | char filename[1024]; | ||
| 38 | VMMapping *head = NULL; | ||
| 39 | VMMapping *cur = NULL; | ||
| 40 | |||
| 41 | while( | ||
| 42 | fscanf(fmaps, "%lx-%lx %15s %*x %*x:%*x %*u%1023[^\n]", | ||
| 43 | &begin, &end, perms, filename) != EOF) { | ||
| 44 | strtrim(filename); | ||
| 45 | |||
| 46 | VMMapping *new_mapping = xmalloc(sizeof(VMMapping)); | ||
| 47 | *new_mapping = (VMMapping) { | ||
| 48 | .begin = begin, | ||
| 49 | .end = end, | ||
| 50 | .r = perms[0] == 'r', | ||
| 51 | .w = perms[1] == 'w', | ||
| 52 | .x = perms[2] == 'x', | ||
| 53 | .s = perms[3] == 's', | ||
| 54 | .p = perms[3] == 'p', | ||
| 55 | .name = strdup(filename), | ||
| 56 | .next = NULL, | ||
| 57 | }; | ||
| 58 | |||
| 59 | if (head) { | ||
| 60 | cur->next = new_mapping; | ||
| 61 | cur = new_mapping; | ||
| 62 | } else { | ||
| 63 | head = cur = new_mapping; | ||
| 64 | } | ||
| 65 | |||
| 66 | LOG("%lx-%lx %c%c%c%c %s\n", | ||
| 67 | cur->begin, | ||
| 68 | cur->end, | ||
| 69 | cur->r ? 'r' : '-', | ||
| 70 | cur->w ? 'w' : '-', | ||
| 71 | cur->x ? 'x' : '-', | ||
| 72 | cur->s ? 's' : 'p', | ||
| 73 | cur->name); | ||
| 74 | } | ||
| 75 | |||
| 76 | return head; | ||
| 77 | } | ||
