aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vm.c')
-rw-r--r--src/vm.c77
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
9static 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
24VMMapping* 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}