* See the COPYING file in the top-level directory.
*/
-#include "qemu-common.h"
+#include "qemu-io.h"
#include "block/block_int.h"
-#include "cmd.h"
+#include "block/qapi.h"
+#include "qemu/main-loop.h"
+#include "qemu/timer.h"
#define CMD_NOFILE_OK 0x01
-int qemuio_misalign;
+bool qemuio_misalign;
+
+static cmdinfo_t *cmdtab;
+static int ncmds;
+
+static int compare_cmdname(const void *a, const void *b)
+{
+ return strcmp(((const cmdinfo_t *)a)->name,
+ ((const cmdinfo_t *)b)->name);
+}
+
+void qemuio_add_command(const cmdinfo_t *ci)
+{
+ cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
+ cmdtab[ncmds - 1] = *ci;
+ qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
+}
+
+int qemuio_command_usage(const cmdinfo_t *ci)
+{
+ printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
+ return 0;
+}
+
+static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
+{
+ if (ct->flags & CMD_FLAG_GLOBAL) {
+ return 1;
+ }
+ if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
+ fprintf(stderr, "no file open, try 'help open'\n");
+ return 0;
+ }
+ return 1;
+}
+
+static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc,
+ char **argv)
+{
+ char *cmd = argv[0];
+
+ if (!init_check_command(bs, ct)) {
+ return 0;
+ }
+
+ if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
+ if (ct->argmax == -1) {
+ fprintf(stderr,
+ "bad argument count %d to %s, expected at least %d arguments\n",
+ argc-1, cmd, ct->argmin);
+ } else if (ct->argmin == ct->argmax) {
+ fprintf(stderr,
+ "bad argument count %d to %s, expected %d arguments\n",
+ argc-1, cmd, ct->argmin);
+ } else {
+ fprintf(stderr,
+ "bad argument count %d to %s, expected between %d and %d arguments\n",
+ argc-1, cmd, ct->argmin, ct->argmax);
+ }
+ return 0;
+ }
+ optind = 0;
+ return ct->cfunc(bs, argc, argv);
+}
+
+static const cmdinfo_t *find_command(const char *cmd)
+{
+ cmdinfo_t *ct;
+
+ for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
+ if (strcmp(ct->name, cmd) == 0 ||
+ (ct->altname && strcmp(ct->altname, cmd) == 0))
+ {
+ return (const cmdinfo_t *)ct;
+ }
+ }
+ return NULL;
+}
+
+/* Invoke fn() for commands with a matching prefix */
+void qemuio_complete_command(const char *input,
+ void (*fn)(const char *cmd, void *opaque),
+ void *opaque)
+{
+ cmdinfo_t *ct;
+ size_t input_len = strlen(input);
+
+ for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
+ if (strncmp(input, ct->name, input_len) == 0) {
+ fn(ct->name, opaque);
+ }
+ }
+}
+
+static char **breakline(char *input, int *count)
+{
+ int c = 0;
+ char *p;
+ char **rval = g_new0(char *, 1);
+
+ while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
+ if (!*p) {
+ continue;
+ }
+ c++;
+ rval = g_renew(char *, rval, (c + 1));
+ rval[c - 1] = p;
+ rval[c] = NULL;
+ }
+ *count = c;
+ return rval;
+}
static int64_t cvtnum(const char *s)
{
return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
}
+#define EXABYTES(x) ((long long)(x) << 60)
+#define PETABYTES(x) ((long long)(x) << 50)
+#define TERABYTES(x) ((long long)(x) << 40)
+#define GIGABYTES(x) ((long long)(x) << 30)
+#define MEGABYTES(x) ((long long)(x) << 20)
+#define KILOBYTES(x) ((long long)(x) << 10)
+
+#define TO_EXABYTES(x) ((x) / EXABYTES(1))
+#define TO_PETABYTES(x) ((x) / PETABYTES(1))
+#define TO_TERABYTES(x) ((x) / TERABYTES(1))
+#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
+#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
+#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
+
+static void cvtstr(double value, char *str, size_t size)
+{
+ char *trim;
+ const char *suffix;
+
+ if (value >= EXABYTES(1)) {
+ suffix = " EiB";
+ snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
+ } else if (value >= PETABYTES(1)) {
+ suffix = " PiB";
+ snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
+ } else if (value >= TERABYTES(1)) {
+ suffix = " TiB";
+ snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
+ } else if (value >= GIGABYTES(1)) {
+ suffix = " GiB";
+ snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
+ } else if (value >= MEGABYTES(1)) {
+ suffix = " MiB";
+ snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
+ } else if (value >= KILOBYTES(1)) {
+ suffix = " KiB";
+ snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
+ } else {
+ suffix = " bytes";
+ snprintf(str, size - 6, "%f", value);
+ }
+
+ trim = strstr(str, ".000");
+ if (trim) {
+ strcpy(trim, suffix);
+ } else {
+ strcat(str, suffix);
+ }
+}
+
+
+
+static struct timeval tsub(struct timeval t1, struct timeval t2)
+{
+ t1.tv_usec -= t2.tv_usec;
+ if (t1.tv_usec < 0) {
+ t1.tv_usec += 1000000;
+ t1.tv_sec--;
+ }
+ t1.tv_sec -= t2.tv_sec;
+ return t1;
+}
+
+static double tdiv(double value, struct timeval tv)
+{
+ return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
+}
+
+#define HOURS(sec) ((sec) / (60 * 60))
+#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
+#define SECONDS(sec) ((sec) % 60)
+
+enum {
+ DEFAULT_TIME = 0x0,
+ TERSE_FIXED_TIME = 0x1,
+ VERBOSE_FIXED_TIME = 0x2,
+};
+
+static void timestr(struct timeval *tv, char *ts, size_t size, int format)
+{
+ double usec = (double)tv->tv_usec / 1000000.0;
+
+ if (format & TERSE_FIXED_TIME) {
+ if (!HOURS(tv->tv_sec)) {
+ snprintf(ts, size, "%u:%02u.%02u",
+ (unsigned int) MINUTES(tv->tv_sec),
+ (unsigned int) SECONDS(tv->tv_sec),
+ (unsigned int) (usec * 100));
+ return;
+ }
+ format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
+ }
+
+ if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
+ snprintf(ts, size, "%u:%02u:%02u.%02u",
+ (unsigned int) HOURS(tv->tv_sec),
+ (unsigned int) MINUTES(tv->tv_sec),
+ (unsigned int) SECONDS(tv->tv_sec),
+ (unsigned int) (usec * 100));
+ } else {
+ snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
+ }
+}
+
/*
* Parse the pattern argument to various sub-commands.
*
CoWriteZeroes *data = opaque;
data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
- data->count / BDRV_SECTOR_SIZE);
+ data->count / BDRV_SECTOR_SIZE, 0);
data->done = true;
if (data->ret < 0) {
*data->total = data->ret;
co = qemu_coroutine_create(co_write_zeroes_entry);
qemu_coroutine_enter(co, &data);
while (!data.done) {
- qemu_aio_wait();
+ aio_poll(bdrv_get_aio_context(bs), true);
}
if (data.ret < 0) {
return data.ret;
vflag = 1;
break;
default:
- return command_usage(&read_cmd);
+ return qemuio_command_usage(&read_cmd);
}
}
if (optind != argc - 2) {
- return command_usage(&read_cmd);
+ return qemuio_command_usage(&read_cmd);
}
if (bflag && pflag) {
}
if (!Pflag && (lflag || sflag)) {
- return command_usage(&read_cmd);
+ return qemuio_command_usage(&read_cmd);
}
if (!lflag) {
vflag = 1;
break;
default:
- return command_usage(&readv_cmd);
+ return qemuio_command_usage(&readv_cmd);
}
}
if (optind > argc - 2) {
- return command_usage(&readv_cmd);
+ return qemuio_command_usage(&readv_cmd);
}
zflag = 1;
break;
default:
- return command_usage(&write_cmd);
+ return qemuio_command_usage(&write_cmd);
}
}
if (optind != argc - 2) {
- return command_usage(&write_cmd);
+ return qemuio_command_usage(&write_cmd);
}
if (bflag + pflag + zflag > 1) {
" writes a range of bytes from the given offset source from multiple buffers\n"
"\n"
" Example:\n"
-" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
+" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
"\n"
" Writes into a segment of the currently open file, using a buffer\n"
" filled with a set pattern (0xcdcdcdcd).\n"
}
break;
default:
- return command_usage(&writev_cmd);
+ return qemuio_command_usage(&writev_cmd);
}
}
if (optind > argc - 2) {
- return command_usage(&writev_cmd);
+ return qemuio_command_usage(&writev_cmd);
}
offset = cvtnum(argv[optind]);
}
break;
default:
- return command_usage(&writev_cmd);
+ return qemuio_command_usage(&writev_cmd);
}
}
if (optind > argc - 2) {
- return command_usage(&writev_cmd);
+ return qemuio_command_usage(&writev_cmd);
}
nr_reqs = 1;
}
}
- reqs = g_malloc0(nr_reqs * sizeof(*reqs));
- buf = g_malloc0(nr_reqs * sizeof(*buf));
- qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
+ reqs = g_new0(BlockRequest, nr_reqs);
+ buf = g_new0(char *, nr_reqs);
+ qiovs = g_new(QEMUIOVector, nr_reqs);
for (i = 0; i < nr_reqs && optind < argc; i++) {
int j;
break;
default:
g_free(ctx);
- return command_usage(&aio_read_cmd);
+ return qemuio_command_usage(&aio_read_cmd);
}
}
if (optind > argc - 2) {
g_free(ctx);
- return command_usage(&aio_read_cmd);
+ return qemuio_command_usage(&aio_read_cmd);
}
ctx->offset = cvtnum(argv[optind]);
break;
default:
g_free(ctx);
- return command_usage(&aio_write_cmd);
+ return qemuio_command_usage(&aio_write_cmd);
}
}
if (optind > argc - 2) {
g_free(ctx);
- return command_usage(&aio_write_cmd);
+ return qemuio_command_usage(&aio_write_cmd);
}
ctx->offset = cvtnum(argv[optind]);
static int info_f(BlockDriverState *bs, int argc, char **argv)
{
BlockDriverInfo bdi;
+ ImageInfoSpecific *spec_info;
char s1[64], s2[64];
int ret;
printf("cluster size: %s\n", s1);
printf("vm state offset: %s\n", s2);
+ spec_info = bdrv_get_specific_info(bs);
+ if (spec_info) {
+ printf("Format specific information:\n");
+ bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
+ qapi_free_ImageInfoSpecific(spec_info);
+ }
+
return 0;
}
qflag = 1;
break;
default:
- return command_usage(&discard_cmd);
+ return qemuio_command_usage(&discard_cmd);
}
}
if (optind != argc - 2) {
- return command_usage(&discard_cmd);
+ return qemuio_command_usage(&discard_cmd);
}
offset = cvtnum(argv[optind]);
sector_num = offset >> 9;
while (remaining) {
ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
+ if (ret < 0) {
+ printf("is_allocated failed: %s\n", strerror(-ret));
+ return 0;
+ }
sector_num += num;
remaining -= num;
if (ret) {
return 0;
}
+static int remove_break_f(BlockDriverState *bs, int argc, char **argv)
+{
+ int ret;
+
+ ret = bdrv_debug_remove_breakpoint(bs, argv[1]);
+ if (ret < 0) {
+ printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
+ }
+
+ return 0;
+}
+
static const cmdinfo_t break_cmd = {
.name = "break",
.argmin = 2,
"request as tag",
};
+static const cmdinfo_t remove_break_cmd = {
+ .name = "remove_break",
+ .argmin = 1,
+ .argmax = 1,
+ .cfunc = remove_break_f,
+ .args = "tag",
+ .oneline = "remove a breakpoint by tag",
+};
+
static int resume_f(BlockDriverState *bs, int argc, char **argv)
{
int ret;
static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
{
while (!bdrv_debug_is_suspended(bs, argv[1])) {
- qemu_aio_wait();
+ aio_poll(bdrv_get_aio_context(bs), true);
}
return 0;
.oneline = "simulate a program crash using abort(3)",
};
+static void sleep_cb(void *opaque)
+{
+ bool *expired = opaque;
+ *expired = true;
+}
+
+static int sleep_f(BlockDriverState *bs, int argc, char **argv)
+{
+ char *endptr;
+ long ms;
+ struct QEMUTimer *timer;
+ bool expired = false;
+
+ ms = strtol(argv[1], &endptr, 0);
+ if (ms < 0 || *endptr != '\0') {
+ printf("%s is not a valid number\n", argv[1]);
+ return 0;
+ }
+
+ timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
+ timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
+
+ while (!expired) {
+ main_loop_wait(false);
+ }
+
+ timer_free(timer);
+
+ return 0;
+}
+
+static const cmdinfo_t sleep_cmd = {
+ .name = "sleep",
+ .argmin = 1,
+ .argmax = 1,
+ .cfunc = sleep_f,
+ .flags = CMD_NOFILE_OK,
+ .oneline = "waits for the given value in milliseconds",
+};
+
static void help_oneline(const char *cmd, const cmdinfo_t *ct)
{
if (cmd) {
.oneline = "help for one or all commands",
};
-static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
-{
- if (ct->flags & CMD_FLAG_GLOBAL) {
- return 1;
- }
- if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
- fprintf(stderr, "no file open, try 'help open'\n");
- return 0;
- }
- return 1;
-}
-
-bool qemuio_command(const char *cmd)
+bool qemuio_command(BlockDriverState *bs, const char *cmd)
{
char *input;
const cmdinfo_t *ct;
if (c) {
ct = find_command(v[0]);
if (ct) {
- done = command(ct, c, v);
+ done = command(bs, ct, c, v);
} else {
fprintf(stderr, "command \"%s\" not found\n", v[0]);
}
static void __attribute((constructor)) init_qemuio_commands(void)
{
/* initialize commands */
- add_command(&help_cmd);
- add_command(&read_cmd);
- add_command(&readv_cmd);
- add_command(&write_cmd);
- add_command(&writev_cmd);
- add_command(&multiwrite_cmd);
- add_command(&aio_read_cmd);
- add_command(&aio_write_cmd);
- add_command(&aio_flush_cmd);
- add_command(&flush_cmd);
- add_command(&truncate_cmd);
- add_command(&length_cmd);
- add_command(&info_cmd);
- add_command(&discard_cmd);
- add_command(&alloc_cmd);
- add_command(&map_cmd);
- add_command(&break_cmd);
- add_command(&resume_cmd);
- add_command(&wait_break_cmd);
- add_command(&abort_cmd);
-
- add_check_command(init_check_command);
+ qemuio_add_command(&help_cmd);
+ qemuio_add_command(&read_cmd);
+ qemuio_add_command(&readv_cmd);
+ qemuio_add_command(&write_cmd);
+ qemuio_add_command(&writev_cmd);
+ qemuio_add_command(&multiwrite_cmd);
+ qemuio_add_command(&aio_read_cmd);
+ qemuio_add_command(&aio_write_cmd);
+ qemuio_add_command(&aio_flush_cmd);
+ qemuio_add_command(&flush_cmd);
+ qemuio_add_command(&truncate_cmd);
+ qemuio_add_command(&length_cmd);
+ qemuio_add_command(&info_cmd);
+ qemuio_add_command(&discard_cmd);
+ qemuio_add_command(&alloc_cmd);
+ qemuio_add_command(&map_cmd);
+ qemuio_add_command(&break_cmd);
+ qemuio_add_command(&remove_break_cmd);
+ qemuio_add_command(&resume_cmd);
+ qemuio_add_command(&wait_break_cmd);
+ qemuio_add_command(&abort_cmd);
+ qemuio_add_command(&sleep_cmd);
}