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 | |
| parent | Attaching & Detaching from process (diff) | |
| download | linux-game-trainer-5369407082db53b68a1100e7b5391a1e9c36e621.tar.gz linux-game-trainer-5369407082db53b68a1100e7b5391a1e9c36e621.zip | |
Parse /proc/pid/maps
| -rw-r--r-- | src/main.c | 15 | ||||
| -rw-r--r-- | src/util.c | 15 | ||||
| -rw-r--r-- | src/util.h | 16 | ||||
| -rw-r--r-- | src/vm.c | 77 | ||||
| -rw-r--r-- | src/vm.h | 19 |
5 files changed, 132 insertions, 10 deletions
| @@ -1,17 +1,12 @@ | |||
| 1 | #include <string.h> | ||
| 2 | #include <stdint.h> | ||
| 1 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 2 | #include <stdio.h> | 4 | #include <stdio.h> |
| 3 | #include <sys/ptrace.h> | 5 | #include <sys/ptrace.h> |
| 4 | #include <sys/wait.h> | 6 | #include <sys/wait.h> |
| 5 | #include <unistd.h> | 7 | #include <unistd.h> |
| 6 | 8 | #include "util.h" | |
| 7 | #define ERROR(...) \ | 9 | #include "vm.h" |
| 8 | do { \ | ||
| 9 | fprintf(stderr, __VA_ARGS__); \ | ||
| 10 | exit(1); \ | ||
| 11 | } while (0) | ||
| 12 | |||
| 13 | #define LOG(...) \ | ||
| 14 | fprintf(stderr, __VA_ARGS__) | ||
| 15 | 10 | ||
| 16 | int main(int argc, char *argv[]) | 11 | int main(int argc, char *argv[]) |
| 17 | { | 12 | { |
| @@ -29,7 +24,7 @@ int main(int argc, char *argv[]) | |||
| 29 | LOG("Attached to process %d\n", pid); | 24 | LOG("Attached to process %d\n", pid); |
| 30 | 25 | ||
| 31 | /* Do stuff ... */ | 26 | /* Do stuff ... */ |
| 32 | sleep(5); | 27 | parse_vmmap(pid); |
| 33 | 28 | ||
| 34 | ptrace(PTRACE_DETACH, pid, NULL, NULL); | 29 | ptrace(PTRACE_DETACH, pid, NULL, NULL); |
| 35 | LOG("Detached from process %d\n", pid); | 30 | LOG("Detached from process %d\n", pid); |
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..11796ce --- /dev/null +++ b/src/util.c | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include "util.h" | ||
| 5 | |||
| 6 | void* xmalloc(size_t size) | ||
| 7 | { | ||
| 8 | void *block = malloc(size); | ||
| 9 | if (!block) { | ||
| 10 | perror("malloc"); | ||
| 11 | exit(1); | ||
| 12 | } | ||
| 13 | memset(block, 0, size); | ||
| 14 | return block; | ||
| 15 | } | ||
diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..5fd680e --- /dev/null +++ b/src/util.h | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #ifndef _UTIL_H_ | ||
| 2 | #define _UTIL_H_ | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 5 | #define ERROR(...) \ | ||
| 6 | do { \ | ||
| 7 | fprintf(stderr, __VA_ARGS__); \ | ||
| 8 | exit(1); \ | ||
| 9 | } while (0) | ||
| 10 | |||
| 11 | #define LOG(...) \ | ||
| 12 | fprintf(stderr, __VA_ARGS__) | ||
| 13 | |||
| 14 | void* xmalloc(size_t size); | ||
| 15 | |||
| 16 | #endif // _UTIL_H_ | ||
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 | } | ||
diff --git a/src/vm.h b/src/vm.h new file mode 100644 index 0000000..25849e7 --- /dev/null +++ b/src/vm.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #ifndef _VM_H_ | ||
| 2 | #define _VM_H_ | ||
| 3 | #include <stdint.h> | ||
| 4 | |||
| 5 | typedef struct VMMapping { | ||
| 6 | uint64_t begin; | ||
| 7 | uint64_t end; | ||
| 8 | uint8_t r:1; | ||
| 9 | uint8_t w:1; | ||
| 10 | uint8_t x:1; | ||
| 11 | uint8_t s:1; | ||
| 12 | uint8_t p:1; | ||
| 13 | const char *name; | ||
| 14 | struct VMMapping *next; | ||
| 15 | } VMMapping; | ||
| 16 | |||
| 17 | VMMapping* parse_vmmap (int pid); | ||
| 18 | |||
| 19 | #endif // _VM_H_ | ||
