]> Projects - linux-game-trainer.git/commitdiff Test
Parse /proc/pid/maps
authorOrfeas <redacted>
Mon, 15 Apr 2024 21:19:57 +0000 (00:19 +0300)
committerOrfeas <redacted>
Mon, 15 Apr 2024 21:26:01 +0000 (00:26 +0300)
src/main.c
src/util.c [new file with mode: 0644]
src/util.h [new file with mode: 0644]
src/vm.c [new file with mode: 0644]
src/vm.h [new file with mode: 0644]

index 1d23902cf376237daa9ae505b73b99efa8ef8d73..e255ec58da01cdc4a3159f45353ad0a9f55e48f0 100644 (file)
@@ -1,17 +1,12 @@
+#include <string.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/ptrace.h>
 #include <sys/wait.h>
 #include <unistd.h>
-
-#define ERROR(...) \
-  do { \
-    fprintf(stderr, __VA_ARGS__); \
-    exit(1); \
-  } while (0)
-
-#define LOG(...) \
-  fprintf(stderr, __VA_ARGS__)
+#include "util.h"
+#include "vm.h"
 
 int main(int argc, char *argv[])
 {
@@ -29,7 +24,7 @@ int main(int argc, char *argv[])
   LOG("Attached to process %d\n", pid);
 
   /* Do stuff ... */
-  sleep(5);
+  parse_vmmap(pid);
 
   ptrace(PTRACE_DETACH, pid, NULL, NULL);
   LOG("Detached from process %d\n", pid);
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..11796ce
--- /dev/null
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "util.h"
+
+void* xmalloc(size_t size)
+{
+  void *block = malloc(size);
+  if (!block) {
+    perror("malloc");
+    exit(1);
+  }
+  memset(block, 0, size);
+  return block;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..5fd680e
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef _UTIL_H_
+#define _UTIL_H_
+#include <stdlib.h>
+
+#define ERROR(...) \
+  do { \
+    fprintf(stderr, __VA_ARGS__); \
+    exit(1); \
+  } while (0)
+
+#define LOG(...) \
+  fprintf(stderr, __VA_ARGS__)
+
+void* xmalloc(size_t size);
+
+#endif // _UTIL_H_
diff --git a/src/vm.c b/src/vm.c
new file mode 100644 (file)
index 0000000..8e62426
--- /dev/null
+++ b/src/vm.c
@@ -0,0 +1,77 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdint.h>
+#include "vm.h"
+#include "util.h"
+
+static void strtrim (char *str)
+{
+  char *begin = str;
+  while (isspace(*begin)) {
+    begin++;
+  }
+  size_t len = strlen(begin);
+  char *end = begin + len;
+  while (end > begin && isspace(*end)) {
+    end++;
+  }
+
+  memmove(str, begin, end - begin + 1);
+}
+
+VMMapping* parse_vmmap (int pid)
+{
+  char fmaps_path[128];
+  sprintf(fmaps_path, "/proc/%d/maps", pid);
+  FILE *fmaps;
+
+  if ((fmaps = fopen(fmaps_path, "r")) == NULL) {
+    perror("fopen");
+    exit(1);
+  }
+
+  uint64_t begin, end;
+  char perms[16];
+  char filename[1024];
+  VMMapping *head = NULL;
+  VMMapping *cur = NULL;
+
+  while(
+    fscanf(fmaps, "%lx-%lx %15s %*x %*x:%*x %*u%1023[^\n]",
+          &begin, &end, perms, filename) != EOF) {
+    strtrim(filename);
+
+    VMMapping *new_mapping = xmalloc(sizeof(VMMapping));
+    *new_mapping = (VMMapping) {
+      .begin = begin,
+      .end = end,
+      .r = perms[0] == 'r',
+      .w = perms[1] == 'w',
+      .x = perms[2] == 'x',
+      .s = perms[3] == 's',
+      .p = perms[3] == 'p',
+      .name = strdup(filename),
+      .next = NULL,
+    };
+
+    if (head) {
+      cur->next = new_mapping;
+      cur = new_mapping;
+    } else {
+      head = cur = new_mapping;
+    }
+
+    LOG("%lx-%lx %c%c%c%c %s\n",
+       cur->begin,
+       cur->end,
+       cur->r ? 'r' : '-',
+       cur->w ? 'w' : '-',
+       cur->x ? 'x' : '-',
+       cur->s ? 's' : 'p',
+       cur->name);
+  }
+
+  return head;
+}
diff --git a/src/vm.h b/src/vm.h
new file mode 100644 (file)
index 0000000..25849e7
--- /dev/null
+++ b/src/vm.h
@@ -0,0 +1,19 @@
+#ifndef _VM_H_
+#define _VM_H_
+#include <stdint.h>
+
+typedef struct VMMapping {
+  uint64_t begin;
+  uint64_t end;
+  uint8_t r:1;
+  uint8_t w:1;
+  uint8_t x:1;
+  uint8_t s:1;
+  uint8_t p:1;
+  const char *name;
+  struct VMMapping *next;
+} VMMapping;
+
+VMMapping* parse_vmmap (int pid);
+
+#endif // _VM_H_