summaryrefslogtreecommitdiff
path: root/src/cli
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2018-10-21 16:23:23 +0300
committerJari Vetoniemi <mailroxas@gmail.com>2018-10-21 16:23:23 +0300
commit75f9922f6d3c1e5bbbe7b90ca170392cc0f5efbc (patch)
treed79e445e87ae9a55e146c6c3b46fef6f238d23e7 /src/cli
parentd81411896f140981400e4fbf4aafacdbabad96cd (diff)
Refactor io utils, add *-address-rw tools
Namespace io_ stuff into mem_io_ to be less likely to collision with anything else. Add io-stream utility for working with streams instead of direct buffers. Add address-rw tools for simple memory read/write, where regions aren't needed.
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/cli.h12
-rw-r--r--src/cli/proc-address-rw.c77
-rw-r--r--src/cli/proc-region-rw.c93
-rw-r--r--src/cli/proc-region-rw.h9
4 files changed, 132 insertions, 59 deletions
diff --git a/src/cli/cli.h b/src/cli/cli.h
new file mode 100644
index 0000000..6262304
--- /dev/null
+++ b/src/cli/cli.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <stdbool.h>
+#include <sys/types.h> // pid_t
+
+struct mem_io;
+
+int
+proc_address_rw(int argc, const char *argv[], bool (*)(struct mem_io*, const pid_t));
+
+int
+proc_region_rw(int argc, const char *argv[], bool (*)(struct mem_io*, const pid_t));
diff --git a/src/cli/proc-address-rw.c b/src/cli/proc-address-rw.c
new file mode 100644
index 0000000..6318795
--- /dev/null
+++ b/src/cli/proc-address-rw.c
@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "mem/io.h"
+#include "mem/io-stream.h"
+#include "util.h"
+
+static void
+usage(const char *argv0)
+{
+ fprintf(stderr, "usage: %s pid write offset [len] < data\n"
+ " %s pid read offset len\n", argv0, argv0);
+ exit(EXIT_FAILURE);
+}
+
+struct options {
+ size_t offset, len;
+ enum {
+ MODE_WRITE,
+ MODE_READ
+ } mode;
+ bool has_len;
+};
+
+static inline void
+options_init(struct options *opt, size_t argc, const char *argv[])
+{
+ size_t arg = 0;
+ *opt = (struct options){0};
+
+ {
+ bool w = false, r = false;
+ const char *mode = argv[arg++];
+ if (!(w = !strcmp(mode, "write")) && !(r = !strcmp(mode, "read")))
+ errx(EXIT_FAILURE, "mode must be write or read");
+
+ opt->mode = (w ? MODE_WRITE : MODE_READ);
+ }
+
+ opt->offset = hexdecstrtoull(argv[arg++], NULL);
+
+ if (argc >= arg + 1) {
+ opt->len = hexdecstrtoull(argv[arg++], NULL);
+ opt->has_len = true;
+ }
+
+ if (opt->mode == MODE_READ && !opt->has_len)
+ usage(argv[0]);
+}
+
+int
+proc_address_rw(int argc, const char *argv[], bool (*mem_io_init)(struct mem_io*, const pid_t))
+{
+ if (argc < 4)
+ usage(argv[0]);
+
+ const pid_t pid = strtoull(argv[1], NULL, 10);
+
+ struct options opt;
+ options_init(&opt, argc - 2, argv + 2);
+
+ struct mem_io io;
+ if (!mem_io_init(&io, pid))
+ return EXIT_FAILURE;
+
+ size_t trw = 0;
+ if (opt.mode == MODE_WRITE) {
+ struct mem_io_istream stream = mem_io_istream_from_file(stdin);
+ trw = mem_io_write_from_stream(&io, &stream, opt.offset, (opt.has_len ? opt.len : (size_t)~0));
+ } else {
+ struct mem_io_ostream stream = mem_io_ostream_from_file(stdout);
+ trw = mem_io_read_to_stream(&io, &stream, opt.offset, opt.len);
+ }
+
+ mem_io_release(&io);
+ return trw;
+}
diff --git a/src/cli/proc-region-rw.c b/src/cli/proc-region-rw.c
index 5dedf7c..360f7ac 100644
--- a/src/cli/proc-region-rw.c
+++ b/src/cli/proc-region-rw.c
@@ -1,31 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "io/io.h"
+#include "mem/io.h"
+#include "mem/io-stream.h"
#include "util.h"
static void
usage(const char *argv0)
{
- fprintf(stderr, "usage: %s pid map file [offset] [len] < regions\n"
- " %s pid write file [offset] [len] < regions\n"
- " %s pid read [offset] [len] < regions\n"
+ fprintf(stderr, "usage: %s pid map regions data [offset] [len]\n"
+ " %s pid write regions data [offset] [len]\n"
+ " %s pid read regions [offset] [len]\n"
" regions must be in /proc/<pid>/maps format", argv0, argv0, argv0);
exit(EXIT_FAILURE);
}
struct context {
- void *buf;
-
struct {
size_t offset, len;
bool has_offset, has_len;
- struct {
- FILE *src;
- size_t size;
- } wm; // write/map
-
enum {
MODE_MAP,
MODE_WRITE,
@@ -33,7 +27,9 @@ struct context {
} mode;
} op;
- struct io io;
+ struct mem_io io;
+ FILE *regions, *data;
+ size_t data_len, trw;
};
static inline void
@@ -46,47 +42,47 @@ context_init(struct context *ctx, size_t argc, const char *argv[])
bool m = false, w = false, r = false;
const char *mode = argv[arg++];
if (!(m = !strcmp(mode, "map")) && !(w = !strcmp(mode, "write")) && !(r = !strcmp(mode, "read")))
- errx(EXIT_FAILURE, "mode must be write or read");
+ errx(EXIT_FAILURE, "mode must be map, write or read");
ctx->op.mode = (m ? MODE_MAP : (w ? MODE_WRITE : MODE_READ));
}
- const char *wmname = NULL;
- if (ctx->op.mode == MODE_MAP || ctx->op.mode == MODE_WRITE) {
- if (argc < arg + 1)
- usage(argv[0]);
+ const char *regions_fname = argv[arg++], *data_fname = NULL;
- wmname = argv[arg++];
- }
+ if (ctx->op.mode != MODE_READ && argc >= arg + 1)
+ data_fname = argv[arg++];
if (argc >= arg + 1) {
- ctx->op.offset = strtoull(argv[arg++], NULL, 10);
+ ctx->op.offset = hexdecstrtoull(argv[arg++], NULL);
ctx->op.has_offset = true;
}
if (argc >= arg + 1) {
- ctx->op.len = strtoull(argv[arg++], NULL, 10);
+ ctx->op.len = hexdecstrtoull(argv[arg++], NULL);
ctx->op.has_len = true;
}
- if (wmname) {
- if (!(ctx->op.wm.src = fopen(wmname, "rb")))
- err(EXIT_FAILURE, "fopen(%s)", wmname);
+ if (!(ctx->regions = fopen(regions_fname, "rb")))
+ err(EXIT_FAILURE, "fopen(%s)", regions_fname);
+
+ if (data_fname) {
+ if (!(ctx->data = fopen(data_fname, "rb")))
+ err(EXIT_FAILURE, "fopen(%s)", data_fname);
- if (fseek(ctx->op.wm.src, 0, SEEK_END) != 0)
+ if (fseek(ctx->data, 0, SEEK_END) != 0)
err(EXIT_FAILURE, "fseek");
- ctx->op.wm.size = ftell(ctx->op.wm.src);
+ ctx->data_len = ftell(ctx->data);
}
}
static void
context_release(struct context *ctx)
{
- if (ctx->op.wm.src)
- fclose(ctx->op.wm.src);
-
- free(ctx->buf);
+ if (ctx->regions)
+ fclose(ctx->regions);
+ if (ctx->data)
+ fclose(ctx->data);
*ctx = (struct context){0};
}
@@ -101,32 +97,28 @@ region_cb(const char *line, void *data)
warnx("%s", line);
region.start += ctx->op.offset;
+ region.offset *= (ctx->op.mode == MODE_MAP);
if (region.start > region.end) {
warnx("write offset 0x%zx is out of bounds", region.start);
return;
}
- region.offset = (ctx->op.mode == MODE_MAP ? region.offset : 0);
-
// requested write/read
- const size_t rlen = (ctx->op.has_len ? ctx->op.len : (ctx->op.mode == MODE_READ ? region.end - region.start : ctx->op.wm.size));
-
+ const size_t rlen = (ctx->op.has_len ? ctx->op.len : (ctx->op.mode == MODE_READ ? region.end - region.start : ctx->data_len));
// actual write/read
const size_t len = (rlen > region.end - region.start ? region.end - region.start : rlen);
if (!len)
return;
- if (!(ctx->buf = realloc(ctx->buf, len)))
- err(EXIT_FAILURE, "realloc");
-
if (ctx->op.mode == MODE_MAP || ctx->op.mode == MODE_WRITE) {
- if (fseek(ctx->op.wm.src, region.offset, SEEK_SET) != 0)
+ if (fseek(ctx->data, region.offset, SEEK_SET) != 0)
err(EXIT_FAILURE, "fseek");
- const size_t rd = fread(ctx->buf, 1, len, ctx->op.wm.src);
- const size_t wd = ctx->io.write(&ctx->io, ctx->buf, region.start, rd);
+ struct mem_io_istream stream = mem_io_istream_from_file(ctx->data);
+ const size_t wd = mem_io_write_from_stream(&ctx->io, &stream, region.start, len);
+ ctx->trw += wd;
if (ctx->op.mode == MODE_WRITE) {
if (rlen > wd) {
@@ -142,28 +134,29 @@ region_cb(const char *line, void *data)
}
}
} else {
- const size_t rd = ctx->io.read(&ctx->io, ctx->buf, region.start, len);
-
- if (fwrite(ctx->buf, 1, rd, stdout) != rd)
- err(EXIT_FAILURE, "fwrite");
+ struct mem_io_ostream stream = mem_io_ostream_from_file(stdout);
+ ctx->trw += mem_io_read_to_stream(&ctx->io, &stream, region.start, len);
}
}
int
-proc_region_rw(int argc, const char *argv[], bool (*io_init)(struct io*, const pid_t))
+proc_region_rw(int argc, const char *argv[], bool (*mem_io_init)(struct mem_io*, const pid_t))
{
- if (argc < 3)
+ if (argc < 4)
usage(argv[0]);
const pid_t pid = strtoull(argv[1], NULL, 10);
+
struct context ctx;
context_init(&ctx, argc - 2, argv + 2);
- if (!io_init(&ctx.io, pid))
+ if (!mem_io_init(&ctx.io, pid))
return EXIT_FAILURE;
- for_each_line_in_file(stdin, region_cb, &ctx);
- io_release(&ctx.io);
+ for_each_line_in_file(ctx.regions, region_cb, &ctx);
+ const size_t trw = ctx.trw;
+
+ mem_io_release(&ctx.io);
context_release(&ctx);
- return EXIT_SUCCESS;
+ return trw;
}
diff --git a/src/cli/proc-region-rw.h b/src/cli/proc-region-rw.h
deleted file mode 100644
index 7ebae67..0000000
--- a/src/cli/proc-region-rw.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#pragma once
-
-#include <stdbool.h>
-#include <sys/types.h> // pid_t
-
-struct io;
-
-int
-proc_region_rw(int argc, const char *argv[], bool (*io_init)(struct io*, const pid_t));