summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthe lemons <citrons@mondecitronne.com>2023-02-07 15:15:00 -0600
committerthe lemons <citrons@mondecitronne.com>2023-02-07 15:15:58 -0600
commit285025ee086b7631cb102c20300c8da7f2d9a52e (patch)
tree199de44aa15d3147eb15f38a42519b2f3ed91f1f
parent2f4f1462488a867dddae15802ba7dff94279fb9f (diff)
change behavior of jumping between maps
-rw-r--r--core.c36
-rw-r--r--procfs.c31
-rw-r--r--procfs.h2
3 files changed, 44 insertions, 25 deletions
diff --git a/core.c b/core.c
index 97953b0..b72f104 100644
--- a/core.c
+++ b/core.c
@@ -50,25 +50,18 @@ static void goto_addr(uintptr_t addr) {
pos_y -= (double) screen_height / 2 * scale;
}
-static size_t closest_map(struct procfs_map *maps, size_t n, uintptr_t addr) {
- uintptr_t min = -1;
- size_t closest;
- for (size_t i = 0; i < n; i++) {
- uintptr_t base = maps[i].base;
- uintptr_t diff = base > addr ? base - addr : addr - base;
- if (diff < min) {
- min = diff; closest = i;
- }
- }
- return closest;
-}
+static size_t current_map = 0;
static void next_map() {
struct procfs_map *maps;
int n = procfs_maps(pid, &maps);
if (n < 1) return;
- size_t m = closest_map(maps, n, current_addr());
- goto_addr(maps[(m + 1) % n].base);
+ for (int i = 0; i < n; i++) {
+ current_map = (current_map + 1) % n;
+ if (maps[current_map].prot & PROT_READ)
+ break;
+ }
+ goto_addr(maps[current_map].base);
free(maps);
}
@@ -76,11 +69,12 @@ static void prev_map() {
struct procfs_map *maps;
int n = procfs_maps(pid, &maps);
if (n < 1) return;
- size_t m = closest_map(maps, n, current_addr());
- if (m != 0)
- goto_addr(maps[m - 1].base);
- else
- goto_addr(maps[n - 1].base);
+ for (int i = 0; i < n; i++) {
+ current_map = current_map > 1 ? current_map - 1 : n - 1;
+ if (maps[current_map].prot & PROT_READ)
+ break;
+ }
+ goto_addr(maps[current_map].base);
free(maps);
}
@@ -184,12 +178,12 @@ int main(int argc, char *argv[]) {
char title[1024];
snprintf(title, 1024, "%s [%d]", argv[0], pid);
- init_sdl(argv[0]);
+ init_sdl(title);
viewer = create_viewer(mem_fd, renderer);
if (!viewer) abort();
atexit(cleanup);
- next_map();
+ prev_map();
int last_time = 0;
int current_time = 0;
diff --git a/procfs.c b/procfs.c
index e3bfb2c..2558f03 100644
--- a/procfs.c
+++ b/procfs.c
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
@@ -27,12 +28,33 @@ ssize_t procfs_maps(pid_t pid, struct procfs_map **maps) {
if (!*maps) return -1;
while (1) {
void *base, *max;
- int matches = fscanf(f, "%p-%p", &base, &max);
+ char prot_str[4];
+ int matches = fscanf(f, "%p-%p %4c", &base, &max, prot_str);
if (matches == EOF) goto eof;
- if (matches < 2) goto err;
+ if (matches < 3) goto err;
+
+ int prot = 0;
+ for (int i = 0; i < 4; i++) {
+ switch (prot_str[i]) {
+ case 'r':
+ prot |= PROT_READ;
+ break;
+ case 'w':
+ prot |= PROT_WRITE;
+ break;
+ case 'x':
+ prot |= PROT_EXEC;
+ break;
+ default:
+ break;
+ }
+ }
+ if (prot == 0) prot = PROT_NONE;
+
int c;
while ((c = getc(f)) != '\n')
if (c == EOF) goto eof;
+
n++;
if (n > capacity) {
capacity <<= 1;
@@ -40,8 +62,9 @@ ssize_t procfs_maps(pid_t pid, struct procfs_map **maps) {
if (!r) goto err;
*maps = r;
}
- (*maps)[n - 1].base = (uintptr_t) base;
- (*maps)[n - 1].max = (uintptr_t) max;
+ (*maps)[n - 1] = (struct procfs_map) {
+ (uintptr_t) base, (uintptr_t) max, prot
+ };
}
eof:
if (ferror(f)) goto err;
diff --git a/procfs.h b/procfs.h
index b36c242..28ef784 100644
--- a/procfs.h
+++ b/procfs.h
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
+#include <sys/mman.h>
#ifndef PAGE_SIZE
#define PAGE_SIZE (4096)
@@ -15,6 +16,7 @@ int read_page(int fd, uintptr_t index, uint8_t *data);
struct procfs_map {
uintptr_t base;
uintptr_t max;
+ int prot;
};
ssize_t procfs_maps(pid_t pid, struct procfs_map **maps);