2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009-2016 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qapi/qmp/qdict.h"
15 #include "sysemu/block-backend.h"
16 #include "block/block.h"
17 #include "block/block_int.h" /* for info_f() */
18 #include "block/qapi.h"
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/option.h"
22 #include "qemu/timer.h"
23 #include "qemu/cutils.h"
25 #define CMD_NOFILE_OK 0x01
29 static cmdinfo_t *cmdtab;
32 static int compare_cmdname(const void *a, const void *b)
34 return strcmp(((const cmdinfo_t *)a)->name,
35 ((const cmdinfo_t *)b)->name);
38 void qemuio_add_command(const cmdinfo_t *ci)
40 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
41 * flags allow it not to be, so that combination is invalid.
42 * Catch it now rather than letting it manifest as a crash if a
43 * particular set of command line options are used.
45 assert(ci->perm == 0 ||
46 (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
47 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
48 cmdtab[ncmds - 1] = *ci;
49 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
52 void qemuio_command_usage(const cmdinfo_t *ci)
54 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
57 static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
59 if (ct->flags & CMD_FLAG_GLOBAL) {
62 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
63 fprintf(stderr, "no file open, try 'help open'\n");
69 static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
74 if (!init_check_command(blk, ct)) {
78 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
79 if (ct->argmax == -1) {
81 "bad argument count %d to %s, expected at least %d arguments\n",
82 argc-1, cmd, ct->argmin);
83 } else if (ct->argmin == ct->argmax) {
85 "bad argument count %d to %s, expected %d arguments\n",
86 argc-1, cmd, ct->argmin);
89 "bad argument count %d to %s, expected between %d and %d arguments\n",
90 argc-1, cmd, ct->argmin, ct->argmax);
95 /* Request additional permissions if necessary for this command. The caller
96 * is responsible for restoring the original permissions afterwards if this
97 * is what it wants. */
98 if (ct->perm && blk_is_available(blk)) {
99 uint64_t orig_perm, orig_shared_perm;
100 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
102 if (ct->perm & ~orig_perm) {
104 Error *local_err = NULL;
107 new_perm = orig_perm | ct->perm;
109 ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
111 error_report_err(local_err);
118 return ct->cfunc(blk, argc, argv);
121 static const cmdinfo_t *find_command(const char *cmd)
125 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
126 if (strcmp(ct->name, cmd) == 0 ||
127 (ct->altname && strcmp(ct->altname, cmd) == 0))
129 return (const cmdinfo_t *)ct;
135 /* Invoke fn() for commands with a matching prefix */
136 void qemuio_complete_command(const char *input,
137 void (*fn)(const char *cmd, void *opaque),
141 size_t input_len = strlen(input);
143 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
144 if (strncmp(input, ct->name, input_len) == 0) {
145 fn(ct->name, opaque);
150 static char **breakline(char *input, int *count)
154 char **rval = g_new0(char *, 1);
156 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
161 rval = g_renew(char *, rval, (c + 1));
169 static int64_t cvtnum(const char *s)
174 err = qemu_strtosz(s, NULL, &value);
178 if (value > INT64_MAX) {
184 static void print_cvtnum_err(int64_t rc, const char *arg)
188 printf("Parsing error: non-numeric argument,"
189 " or extraneous/unrecognized suffix -- %s\n", arg);
192 printf("Parsing error: argument too large -- %s\n", arg);
195 printf("Parsing error: %s\n", arg);
199 #define EXABYTES(x) ((long long)(x) << 60)
200 #define PETABYTES(x) ((long long)(x) << 50)
201 #define TERABYTES(x) ((long long)(x) << 40)
202 #define GIGABYTES(x) ((long long)(x) << 30)
203 #define MEGABYTES(x) ((long long)(x) << 20)
204 #define KILOBYTES(x) ((long long)(x) << 10)
206 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
207 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
208 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
209 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
210 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
211 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
213 static void cvtstr(double value, char *str, size_t size)
218 if (value >= EXABYTES(1)) {
220 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
221 } else if (value >= PETABYTES(1)) {
223 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
224 } else if (value >= TERABYTES(1)) {
226 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
227 } else if (value >= GIGABYTES(1)) {
229 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
230 } else if (value >= MEGABYTES(1)) {
232 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
233 } else if (value >= KILOBYTES(1)) {
235 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
238 snprintf(str, size - 6, "%f", value);
241 trim = strstr(str, ".000");
243 strcpy(trim, suffix);
251 static struct timespec tsub(struct timespec t1, struct timespec t2)
253 t1.tv_nsec -= t2.tv_nsec;
254 if (t1.tv_nsec < 0) {
255 t1.tv_nsec += NANOSECONDS_PER_SECOND;
258 t1.tv_sec -= t2.tv_sec;
262 static double tdiv(double value, struct timespec tv)
264 double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
265 return value / seconds;
268 #define HOURS(sec) ((sec) / (60 * 60))
269 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
270 #define SECONDS(sec) ((sec) % 60)
274 TERSE_FIXED_TIME = 0x1,
275 VERBOSE_FIXED_TIME = 0x2,
278 static void timestr(struct timespec *tv, char *ts, size_t size, int format)
280 double frac_sec = tv->tv_nsec / 1e9;
282 if (format & TERSE_FIXED_TIME) {
283 if (!HOURS(tv->tv_sec)) {
284 snprintf(ts, size, "%u:%05.2f",
285 (unsigned int) MINUTES(tv->tv_sec),
286 SECONDS(tv->tv_sec) + frac_sec);
289 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
292 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
293 snprintf(ts, size, "%u:%02u:%05.2f",
294 (unsigned int) HOURS(tv->tv_sec),
295 (unsigned int) MINUTES(tv->tv_sec),
296 SECONDS(tv->tv_sec) + frac_sec);
298 snprintf(ts, size, "%05.2f sec", frac_sec);
303 * Parse the pattern argument to various sub-commands.
305 * Because the pattern is used as an argument to memset it must evaluate
306 * to an unsigned integer that fits into a single byte.
308 static int parse_pattern(const char *arg)
313 pattern = strtol(arg, &endptr, 0);
314 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
315 printf("%s is not a valid pattern byte\n", arg);
323 * Memory allocation helpers.
325 * Make sure memory is aligned by default, or purposefully misaligned if
326 * that is specified on the command line.
329 #define MISALIGN_OFFSET 16
330 static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
334 if (qemuio_misalign) {
335 len += MISALIGN_OFFSET;
337 buf = blk_blockalign(blk, len);
338 memset(buf, pattern, len);
339 if (qemuio_misalign) {
340 buf += MISALIGN_OFFSET;
345 static void qemu_io_free(void *p)
347 if (qemuio_misalign) {
348 p -= MISALIGN_OFFSET;
354 * qemu_io_alloc_from_file()
356 * Allocates the buffer and populates it with the content of the given file
357 * up to @len bytes. If the file length is less than @len, then the buffer
358 * is populated with the file content cyclically.
360 * @blk - the block backend where the buffer content is going to be written to
361 * @len - the buffer length
362 * @file_name - the file to read the content from
364 * Returns: the buffer pointer on success
367 static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
368 const char *file_name)
370 char *buf, *buf_origin;
371 FILE *f = fopen(file_name, "r");
379 if (qemuio_misalign) {
380 len += MISALIGN_OFFSET;
383 buf_origin = buf = blk_blockalign(blk, len);
385 if (qemuio_misalign) {
386 buf_origin += MISALIGN_OFFSET;
387 buf += MISALIGN_OFFSET;
388 len -= MISALIGN_OFFSET;
391 pattern_len = fread(buf_origin, 1, len, f);
398 if (pattern_len == 0) {
399 fprintf(stderr, "%s: file is empty\n", file_name);
405 if (len > pattern_len) {
410 size_t len_to_copy = MIN(pattern_len, len);
412 memcpy(buf, buf_origin, len_to_copy);
422 qemu_io_free(buf_origin);
426 static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
432 for (i = 0, p = buffer; i < len; i += 16) {
433 const uint8_t *s = p;
435 printf("%08" PRIx64 ": ", offset + i);
436 for (j = 0; j < 16 && i + j < len; j++, p++) {
440 for (j = 0; j < 16 && i + j < len; j++, s++) {
451 static void print_report(const char *op, struct timespec *t, int64_t offset,
452 int64_t count, int64_t total, int cnt, bool Cflag)
454 char s1[64], s2[64], ts[64];
456 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
458 cvtstr((double)total, s1, sizeof(s1));
459 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
460 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
461 op, total, count, offset);
462 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
463 s1, cnt, ts, s2, tdiv((double)cnt, *t));
464 } else {/* bytes,ops,time,bytes/sec,ops/sec */
465 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
467 tdiv((double)total, *t),
468 tdiv((double)cnt, *t));
473 * Parse multiple length statements for vectored I/O, and construct an I/O
474 * vector matching it.
477 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
480 size_t *sizes = g_new0(size_t, nr_iov);
486 for (i = 0; i < nr_iov; i++) {
492 print_cvtnum_err(len, arg);
496 if (len > BDRV_REQUEST_MAX_BYTES) {
497 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
498 (uint64_t)BDRV_REQUEST_MAX_BYTES);
502 if (count > BDRV_REQUEST_MAX_BYTES - len) {
503 printf("The total number of bytes exceed the maximum size %" PRIu64
504 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
512 qemu_iovec_init(qiov, nr_iov);
514 buf = p = qemu_io_alloc(blk, count, pattern);
516 for (i = 0; i < nr_iov; i++) {
517 qemu_iovec_add(qiov, p, sizes[i]);
526 static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
527 int64_t bytes, int64_t *total)
529 if (bytes > INT_MAX) {
533 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
540 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
541 int64_t bytes, int flags, int64_t *total)
543 if (bytes > INT_MAX) {
547 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
564 static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
566 CoWriteZeroes *data = opaque;
568 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
572 *data->total = data->ret;
576 *data->total = data->bytes;
579 static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
580 int64_t bytes, int flags, int64_t *total)
583 CoWriteZeroes data = {
592 if (bytes > INT_MAX) {
596 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
597 bdrv_coroutine_enter(blk_bs(blk), co);
599 aio_poll(blk_get_aio_context(blk), true);
608 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
609 int64_t bytes, int64_t *total)
613 if (bytes > BDRV_REQUEST_MAX_BYTES) {
617 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
625 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
626 int64_t count, int64_t *total)
628 if (count > INT_MAX) {
632 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
639 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
640 int64_t count, int64_t *total)
642 if (count > INT_MAX) {
646 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
653 #define NOT_DONE 0x7fffffff
654 static void aio_rw_done(void *opaque, int ret)
656 *(int *)opaque = ret;
659 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
660 int64_t offset, int *total)
662 int async_ret = NOT_DONE;
664 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
665 while (async_ret == NOT_DONE) {
666 main_loop_wait(false);
670 return async_ret < 0 ? async_ret : 1;
673 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
674 int64_t offset, int flags, int *total)
676 int async_ret = NOT_DONE;
678 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
679 while (async_ret == NOT_DONE) {
680 main_loop_wait(false);
684 return async_ret < 0 ? async_ret : 1;
687 static void read_help(void)
691 " reads a range of bytes from the given offset\n"
694 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
696 " Reads a segment of the currently open file, optionally dumping it to the\n"
697 " standard output stream (with -v option) for subsequent inspection.\n"
698 " -b, -- read from the VM state rather than the virtual disk\n"
699 " -C, -- report statistics in a machine parsable format\n"
700 " -l, -- length for pattern verification (only with -P)\n"
701 " -p, -- ignored for backwards compatibility\n"
702 " -P, -- use a pattern to verify read data\n"
703 " -q, -- quiet mode, do not show I/O statistics\n"
704 " -s, -- start offset for pattern verification (only with -P)\n"
705 " -v, -- dump buffer to standard output\n"
709 static int read_f(BlockBackend *blk, int argc, char **argv);
711 static const cmdinfo_t read_cmd = {
717 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
718 .oneline = "reads a number of bytes at a specified offset",
722 static int read_f(BlockBackend *blk, int argc, char **argv)
724 struct timespec t1, t2;
725 bool Cflag = false, qflag = false, vflag = false;
726 bool Pflag = false, sflag = false, lflag = false, bflag = false;
731 /* Some compilers get confused and warn if this is not initialized. */
734 int64_t pattern_offset = 0, pattern_count = 0;
736 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
746 pattern_count = cvtnum(optarg);
747 if (pattern_count < 0) {
748 print_cvtnum_err(pattern_count, optarg);
749 return pattern_count;
753 /* Ignored for backwards compatibility */
757 pattern = parse_pattern(optarg);
767 pattern_offset = cvtnum(optarg);
768 if (pattern_offset < 0) {
769 print_cvtnum_err(pattern_offset, optarg);
770 return pattern_offset;
777 qemuio_command_usage(&read_cmd);
782 if (optind != argc - 2) {
783 qemuio_command_usage(&read_cmd);
787 offset = cvtnum(argv[optind]);
789 print_cvtnum_err(offset, argv[optind]);
794 count = cvtnum(argv[optind]);
796 print_cvtnum_err(count, argv[optind]);
798 } else if (count > BDRV_REQUEST_MAX_BYTES) {
799 printf("length cannot exceed %" PRIu64 ", given %s\n",
800 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
804 if (!Pflag && (lflag || sflag)) {
805 qemuio_command_usage(&read_cmd);
810 pattern_count = count - pattern_offset;
813 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
814 printf("pattern verification range exceeds end of read data\n");
819 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
820 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
824 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
825 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
831 buf = qemu_io_alloc(blk, count, 0xab);
833 clock_gettime(CLOCK_MONOTONIC, &t1);
835 ret = do_load_vmstate(blk, buf, offset, count, &total);
837 ret = do_pread(blk, buf, offset, count, &total);
839 clock_gettime(CLOCK_MONOTONIC, &t2);
842 printf("read failed: %s\n", strerror(-ret));
850 void *cmp_buf = g_malloc(pattern_count);
851 memset(cmp_buf, pattern, pattern_count);
852 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
853 printf("Pattern verification failed at offset %"
854 PRId64 ", %"PRId64" bytes\n",
855 offset + pattern_offset, pattern_count);
866 dump_buffer(buf, offset, count);
869 /* Finally, report back -- -C gives a parsable format */
871 print_report("read", &t2, offset, count, total, cnt, Cflag);
878 static void readv_help(void)
882 " reads a range of bytes from the given offset into multiple buffers\n"
885 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
887 " Reads a segment of the currently open file, optionally dumping it to the\n"
888 " standard output stream (with -v option) for subsequent inspection.\n"
889 " Uses multiple iovec buffers if more than one byte range is specified.\n"
890 " -C, -- report statistics in a machine parsable format\n"
891 " -P, -- use a pattern to verify read data\n"
892 " -v, -- dump buffer to standard output\n"
893 " -q, -- quiet mode, do not show I/O statistics\n"
897 static int readv_f(BlockBackend *blk, int argc, char **argv);
899 static const cmdinfo_t readv_cmd = {
904 .args = "[-Cqv] [-P pattern] off len [len..]",
905 .oneline = "reads a number of bytes at a specified offset",
909 static int readv_f(BlockBackend *blk, int argc, char **argv)
911 struct timespec t1, t2;
912 bool Cflag = false, qflag = false, vflag = false;
916 /* Some compilers get confused and warn if this is not initialized. */
923 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
930 pattern = parse_pattern(optarg);
942 qemuio_command_usage(&readv_cmd);
947 if (optind > argc - 2) {
948 qemuio_command_usage(&readv_cmd);
953 offset = cvtnum(argv[optind]);
955 print_cvtnum_err(offset, argv[optind]);
960 nr_iov = argc - optind;
961 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
966 clock_gettime(CLOCK_MONOTONIC, &t1);
967 ret = do_aio_readv(blk, &qiov, offset, &total);
968 clock_gettime(CLOCK_MONOTONIC, &t2);
971 printf("readv failed: %s\n", strerror(-ret));
979 void *cmp_buf = g_malloc(qiov.size);
980 memset(cmp_buf, pattern, qiov.size);
981 if (memcmp(buf, cmp_buf, qiov.size)) {
982 printf("Pattern verification failed at offset %"
983 PRId64 ", %zu bytes\n", offset, qiov.size);
994 dump_buffer(buf, offset, qiov.size);
997 /* Finally, report back -- -C gives a parsable format */
999 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1002 qemu_iovec_destroy(&qiov);
1007 static void write_help(void)
1011 " writes a range of bytes from the given offset\n"
1014 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1016 " Writes into a segment of the currently open file, using a buffer\n"
1017 " filled with a set pattern (0xcdcdcdcd).\n"
1018 " -b, -- write to the VM state rather than the virtual disk\n"
1019 " -c, -- write compressed data with blk_write_compressed\n"
1020 " -f, -- use Force Unit Access semantics\n"
1021 " -n, -- with -z, don't allow slow fallback\n"
1022 " -p, -- ignored for backwards compatibility\n"
1023 " -P, -- use different pattern to fill file\n"
1024 " -s, -- use a pattern file to fill the write buffer\n"
1025 " -C, -- report statistics in a machine parsable format\n"
1026 " -q, -- quiet mode, do not show I/O statistics\n"
1027 " -u, -- with -z, allow unmapping\n"
1028 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
1032 static int write_f(BlockBackend *blk, int argc, char **argv);
1034 static const cmdinfo_t write_cmd = {
1038 .perm = BLK_PERM_WRITE,
1041 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
1042 .oneline = "writes a number of bytes at a specified offset",
1046 static int write_f(BlockBackend *blk, int argc, char **argv)
1048 struct timespec t1, t2;
1049 bool Cflag = false, qflag = false, bflag = false;
1050 bool Pflag = false, zflag = false, cflag = false, sflag = false;
1056 /* Some compilers get confused and warn if this is not initialized. */
1059 const char *file_name = NULL;
1061 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
1073 flags |= BDRV_REQ_FUA;
1076 flags |= BDRV_REQ_NO_FALLBACK;
1079 /* Ignored for backwards compatibility */
1083 pattern = parse_pattern(optarg);
1096 flags |= BDRV_REQ_MAY_UNMAP;
1102 qemuio_command_usage(&write_cmd);
1107 if (optind != argc - 2) {
1108 qemuio_command_usage(&write_cmd);
1112 if (bflag && zflag) {
1113 printf("-b and -z cannot be specified at the same time\n");
1117 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1118 printf("-f and -b or -c cannot be specified at the same time\n");
1122 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1123 printf("-n requires -z to be specified\n");
1127 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1128 printf("-u requires -z to be specified\n");
1132 if (zflag + Pflag + sflag > 1) {
1133 printf("Only one of -z, -P, and -s "
1134 "can be specified at the same time\n");
1138 offset = cvtnum(argv[optind]);
1140 print_cvtnum_err(offset, argv[optind]);
1145 count = cvtnum(argv[optind]);
1147 print_cvtnum_err(count, argv[optind]);
1149 } else if (count > BDRV_REQUEST_MAX_BYTES) {
1150 printf("length cannot exceed %" PRIu64 ", given %s\n",
1151 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1155 if (bflag || cflag) {
1156 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1157 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
1162 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1163 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
1171 buf = qemu_io_alloc_from_file(blk, count, file_name);
1176 buf = qemu_io_alloc(blk, count, pattern);
1180 clock_gettime(CLOCK_MONOTONIC, &t1);
1182 ret = do_save_vmstate(blk, buf, offset, count, &total);
1184 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1186 ret = do_write_compressed(blk, buf, offset, count, &total);
1188 ret = do_pwrite(blk, buf, offset, count, flags, &total);
1190 clock_gettime(CLOCK_MONOTONIC, &t2);
1193 printf("write failed: %s\n", strerror(-ret));
1204 /* Finally, report back -- -C gives a parsable format */
1206 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1220 " writes a range of bytes from the given offset source from multiple buffers\n"
1223 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1225 " Writes into a segment of the currently open file, using a buffer\n"
1226 " filled with a set pattern (0xcdcdcdcd).\n"
1227 " -P, -- use different pattern to fill file\n"
1228 " -C, -- report statistics in a machine parsable format\n"
1229 " -f, -- use Force Unit Access semantics\n"
1230 " -q, -- quiet mode, do not show I/O statistics\n"
1234 static int writev_f(BlockBackend *blk, int argc, char **argv);
1236 static const cmdinfo_t writev_cmd = {
1239 .perm = BLK_PERM_WRITE,
1242 .args = "[-Cfq] [-P pattern] off len [len..]",
1243 .oneline = "writes a number of bytes at a specified offset",
1244 .help = writev_help,
1247 static int writev_f(BlockBackend *blk, int argc, char **argv)
1249 struct timespec t1, t2;
1250 bool Cflag = false, qflag = false;
1255 /* Some compilers get confused and warn if this is not initialized. */
1261 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1267 flags |= BDRV_REQ_FUA;
1273 pattern = parse_pattern(optarg);
1279 qemuio_command_usage(&writev_cmd);
1284 if (optind > argc - 2) {
1285 qemuio_command_usage(&writev_cmd);
1289 offset = cvtnum(argv[optind]);
1291 print_cvtnum_err(offset, argv[optind]);
1296 nr_iov = argc - optind;
1297 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1302 clock_gettime(CLOCK_MONOTONIC, &t1);
1303 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
1304 clock_gettime(CLOCK_MONOTONIC, &t2);
1307 printf("writev failed: %s\n", strerror(-ret));
1318 /* Finally, report back -- -C gives a parsable format */
1320 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1322 qemu_iovec_destroy(&qiov);
1337 BlockAcctCookie acct;
1342 static void aio_write_done(void *opaque, int ret)
1344 struct aio_ctx *ctx = opaque;
1347 clock_gettime(CLOCK_MONOTONIC, &t2);
1351 printf("aio_write failed: %s\n", strerror(-ret));
1352 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1356 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1362 /* Finally, report back -- -C gives a parsable format */
1363 t2 = tsub(t2, ctx->t1);
1364 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1365 ctx->qiov.size, 1, ctx->Cflag);
1368 qemu_io_free(ctx->buf);
1369 qemu_iovec_destroy(&ctx->qiov);
1374 static void aio_read_done(void *opaque, int ret)
1376 struct aio_ctx *ctx = opaque;
1379 clock_gettime(CLOCK_MONOTONIC, &t2);
1382 printf("readv failed: %s\n", strerror(-ret));
1383 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1388 void *cmp_buf = g_malloc(ctx->qiov.size);
1390 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1391 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1392 printf("Pattern verification failed at offset %"
1393 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
1398 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1405 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1408 /* Finally, report back -- -C gives a parsable format */
1409 t2 = tsub(t2, ctx->t1);
1410 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1411 ctx->qiov.size, 1, ctx->Cflag);
1413 qemu_io_free(ctx->buf);
1414 qemu_iovec_destroy(&ctx->qiov);
1418 static void aio_read_help(void)
1422 " asynchronously reads a range of bytes from the given offset\n"
1425 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1427 " Reads a segment of the currently open file, optionally dumping it to the\n"
1428 " standard output stream (with -v option) for subsequent inspection.\n"
1429 " The read is performed asynchronously and the aio_flush command must be\n"
1430 " used to ensure all outstanding aio requests have been completed.\n"
1431 " Note that due to its asynchronous nature, this command will be\n"
1432 " considered successful once the request is submitted, independently\n"
1433 " of potential I/O errors or pattern mismatches.\n"
1434 " -C, -- report statistics in a machine parsable format\n"
1435 " -P, -- use a pattern to verify read data\n"
1436 " -i, -- treat request as invalid, for exercising stats\n"
1437 " -v, -- dump buffer to standard output\n"
1438 " -q, -- quiet mode, do not show I/O statistics\n"
1442 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1444 static const cmdinfo_t aio_read_cmd = {
1446 .cfunc = aio_read_f,
1449 .args = "[-Ciqv] [-P pattern] off len [len..]",
1450 .oneline = "asynchronously reads a number of bytes",
1451 .help = aio_read_help,
1454 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1457 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1460 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1467 ctx->pattern = parse_pattern(optarg);
1468 if (ctx->pattern < 0) {
1474 printf("injecting invalid read request\n");
1475 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1486 qemuio_command_usage(&aio_read_cmd);
1491 if (optind > argc - 2) {
1493 qemuio_command_usage(&aio_read_cmd);
1497 ctx->offset = cvtnum(argv[optind]);
1498 if (ctx->offset < 0) {
1499 int ret = ctx->offset;
1500 print_cvtnum_err(ret, argv[optind]);
1506 nr_iov = argc - optind;
1507 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1508 if (ctx->buf == NULL) {
1509 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1514 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1515 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1517 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1521 static void aio_write_help(void)
1525 " asynchronously writes a range of bytes from the given offset source\n"
1526 " from multiple buffers\n"
1529 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1531 " Writes into a segment of the currently open file, using a buffer\n"
1532 " filled with a set pattern (0xcdcdcdcd).\n"
1533 " The write is performed asynchronously and the aio_flush command must be\n"
1534 " used to ensure all outstanding aio requests have been completed.\n"
1535 " Note that due to its asynchronous nature, this command will be\n"
1536 " considered successful once the request is submitted, independently\n"
1537 " of potential I/O errors or pattern mismatches.\n"
1538 " -P, -- use different pattern to fill file\n"
1539 " -C, -- report statistics in a machine parsable format\n"
1540 " -f, -- use Force Unit Access semantics\n"
1541 " -i, -- treat request as invalid, for exercising stats\n"
1542 " -q, -- quiet mode, do not show I/O statistics\n"
1543 " -u, -- with -z, allow unmapping\n"
1544 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1548 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1550 static const cmdinfo_t aio_write_cmd = {
1551 .name = "aio_write",
1552 .cfunc = aio_write_f,
1553 .perm = BLK_PERM_WRITE,
1556 .args = "[-Cfiquz] [-P pattern] off len [len..]",
1557 .oneline = "asynchronously writes a number of bytes",
1558 .help = aio_write_help,
1561 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1565 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1569 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1575 flags |= BDRV_REQ_FUA;
1581 flags |= BDRV_REQ_MAY_UNMAP;
1584 pattern = parse_pattern(optarg);
1591 printf("injecting invalid write request\n");
1592 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1600 qemuio_command_usage(&aio_write_cmd);
1605 if (optind > argc - 2) {
1607 qemuio_command_usage(&aio_write_cmd);
1611 if (ctx->zflag && optind != argc - 2) {
1612 printf("-z supports only a single length parameter\n");
1617 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1618 printf("-u requires -z to be specified\n");
1623 if (ctx->zflag && ctx->Pflag) {
1624 printf("-z and -P cannot be specified at the same time\n");
1629 ctx->offset = cvtnum(argv[optind]);
1630 if (ctx->offset < 0) {
1631 int ret = ctx->offset;
1632 print_cvtnum_err(ret, argv[optind]);
1639 int64_t count = cvtnum(argv[optind]);
1641 print_cvtnum_err(count, argv[optind]);
1646 ctx->qiov.size = count;
1647 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1650 nr_iov = argc - optind;
1651 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1653 if (ctx->buf == NULL) {
1654 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1659 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1660 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1663 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1670 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1672 BlockAcctCookie cookie;
1673 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
1675 block_acct_done(blk_get_stats(blk), &cookie);
1679 static const cmdinfo_t aio_flush_cmd = {
1680 .name = "aio_flush",
1681 .cfunc = aio_flush_f,
1682 .oneline = "completes all outstanding aio requests"
1685 static int flush_f(BlockBackend *blk, int argc, char **argv)
1687 return blk_flush(blk);
1690 static const cmdinfo_t flush_cmd = {
1694 .oneline = "flush all in-core file state to disk",
1697 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1699 Error *local_err = NULL;
1703 offset = cvtnum(argv[1]);
1705 print_cvtnum_err(offset, argv[1]);
1709 ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
1711 error_report_err(local_err);
1718 static const cmdinfo_t truncate_cmd = {
1721 .cfunc = truncate_f,
1722 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1726 .oneline = "truncates the current file at the given offset",
1729 static int length_f(BlockBackend *blk, int argc, char **argv)
1734 size = blk_getlength(blk);
1736 printf("getlength: %s\n", strerror(-size));
1740 cvtstr(size, s1, sizeof(s1));
1746 static const cmdinfo_t length_cmd = {
1750 .oneline = "gets the length of the current file",
1754 static int info_f(BlockBackend *blk, int argc, char **argv)
1756 BlockDriverState *bs = blk_bs(blk);
1757 BlockDriverInfo bdi;
1758 ImageInfoSpecific *spec_info;
1759 Error *local_err = NULL;
1760 char s1[64], s2[64];
1763 if (bs->drv && bs->drv->format_name) {
1764 printf("format name: %s\n", bs->drv->format_name);
1766 if (bs->drv && bs->drv->protocol_name) {
1767 printf("format name: %s\n", bs->drv->protocol_name);
1770 ret = bdrv_get_info(bs, &bdi);
1775 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1776 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1778 printf("cluster size: %s\n", s1);
1779 printf("vm state offset: %s\n", s2);
1781 spec_info = bdrv_get_specific_info(bs, &local_err);
1783 error_report_err(local_err);
1787 printf("Format specific information:\n");
1788 bdrv_image_info_specific_dump(spec_info);
1789 qapi_free_ImageInfoSpecific(spec_info);
1797 static const cmdinfo_t info_cmd = {
1801 .oneline = "prints information about the current file",
1804 static void discard_help(void)
1808 " discards a range of bytes from the given offset\n"
1811 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1813 " Discards a segment of the currently open file.\n"
1814 " -C, -- report statistics in a machine parsable format\n"
1815 " -q, -- quiet mode, do not show I/O statistics\n"
1819 static int discard_f(BlockBackend *blk, int argc, char **argv);
1821 static const cmdinfo_t discard_cmd = {
1825 .perm = BLK_PERM_WRITE,
1828 .args = "[-Cq] off len",
1829 .oneline = "discards a number of bytes at a specified offset",
1830 .help = discard_help,
1833 static int discard_f(BlockBackend *blk, int argc, char **argv)
1835 struct timespec t1, t2;
1836 bool Cflag = false, qflag = false;
1838 int64_t offset, bytes;
1840 while ((c = getopt(argc, argv, "Cq")) != -1) {
1849 qemuio_command_usage(&discard_cmd);
1854 if (optind != argc - 2) {
1855 qemuio_command_usage(&discard_cmd);
1859 offset = cvtnum(argv[optind]);
1861 print_cvtnum_err(offset, argv[optind]);
1866 bytes = cvtnum(argv[optind]);
1868 print_cvtnum_err(bytes, argv[optind]);
1870 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
1871 printf("length cannot exceed %"PRIu64", given %s\n",
1872 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1876 clock_gettime(CLOCK_MONOTONIC, &t1);
1877 ret = blk_pdiscard(blk, offset, bytes);
1878 clock_gettime(CLOCK_MONOTONIC, &t2);
1881 printf("discard failed: %s\n", strerror(-ret));
1885 /* Finally, report back -- -C gives a parsable format */
1888 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
1894 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1896 BlockDriverState *bs = blk_bs(blk);
1897 int64_t offset, start, remaining, count;
1900 int64_t num, sum_alloc;
1902 start = offset = cvtnum(argv[1]);
1904 print_cvtnum_err(offset, argv[1]);
1909 count = cvtnum(argv[2]);
1911 print_cvtnum_err(count, argv[2]);
1915 count = BDRV_SECTOR_SIZE;
1921 ret = bdrv_is_allocated(bs, offset, remaining, &num);
1923 printf("is_allocated failed: %s\n", strerror(-ret));
1937 cvtstr(start, s1, sizeof(s1));
1939 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
1940 sum_alloc, count, s1);
1944 static const cmdinfo_t alloc_cmd = {
1950 .args = "offset [count]",
1951 .oneline = "checks if offset is allocated in the file",
1955 static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1956 int64_t bytes, int64_t *pnum)
1962 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1963 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1971 while (bytes > 0 && ret == firstret) {
1975 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1976 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1977 if (ret == firstret && num) {
1987 static int map_f(BlockBackend *blk, int argc, char **argv)
1989 int64_t offset, bytes;
1990 char s1[64], s2[64];
1996 bytes = blk_getlength(blk);
1998 error_report("Failed to query image length: %s", strerror(-bytes));
2003 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
2005 error_report("Failed to get allocation status: %s", strerror(-ret));
2008 error_report("Unexpected end of image");
2012 retstr = ret ? " allocated" : "not allocated";
2013 cvtstr(num, s1, sizeof(s1));
2014 cvtstr(offset, s2, sizeof(s2));
2015 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
2016 s1, num, retstr, s2, offset);
2025 static const cmdinfo_t map_cmd = {
2031 .oneline = "prints the allocated areas of a file",
2034 static void reopen_help(void)
2038 " Changes the open options of an already opened image\n"
2041 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2043 " -r, -- Reopen the image read-only\n"
2044 " -w, -- Reopen the image read-write\n"
2045 " -c, -- Change the cache mode to the given value\n"
2046 " -o, -- Changes block driver options (cf. 'open' command)\n"
2050 static int reopen_f(BlockBackend *blk, int argc, char **argv);
2052 static QemuOptsList reopen_opts = {
2054 .merge_lists = true,
2055 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2057 /* no elements => accept any params */
2058 { /* end of list */ }
2062 static const cmdinfo_t reopen_cmd = {
2067 .args = "[(-r|-w)] [-c cache] [-o options]",
2068 .oneline = "reopens an image with new options",
2069 .help = reopen_help,
2072 static int reopen_f(BlockBackend *blk, int argc, char **argv)
2074 BlockDriverState *bs = blk_bs(blk);
2078 int flags = bs->open_flags;
2079 bool writethrough = !blk_enable_write_cache(blk);
2080 bool has_rw_option = false;
2081 bool has_cache_option = false;
2083 BlockReopenQueue *brq;
2084 Error *local_err = NULL;
2086 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
2089 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
2090 error_report("Invalid cache option: %s", optarg);
2093 has_cache_option = true;
2096 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2097 qemu_opts_reset(&reopen_opts);
2102 if (has_rw_option) {
2103 error_report("Only one -r/-w option may be given");
2106 flags &= ~BDRV_O_RDWR;
2107 has_rw_option = true;
2110 if (has_rw_option) {
2111 error_report("Only one -r/-w option may be given");
2114 flags |= BDRV_O_RDWR;
2115 has_rw_option = true;
2118 qemu_opts_reset(&reopen_opts);
2119 qemuio_command_usage(&reopen_cmd);
2124 if (optind != argc) {
2125 qemu_opts_reset(&reopen_opts);
2126 qemuio_command_usage(&reopen_cmd);
2130 if (!writethrough != blk_enable_write_cache(blk) &&
2131 blk_get_attached_dev(blk))
2133 error_report("Cannot change cache.writeback: Device attached");
2134 qemu_opts_reset(&reopen_opts);
2138 if (!(flags & BDRV_O_RDWR)) {
2139 uint64_t orig_perm, orig_shared_perm;
2143 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2145 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2150 qopts = qemu_opts_find(&reopen_opts, NULL);
2151 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
2152 qemu_opts_reset(&reopen_opts);
2154 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2155 if (has_rw_option) {
2156 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2157 qobject_unref(opts);
2161 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2164 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2165 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2166 if (has_cache_option) {
2167 error_report("Cannot set both -c and the cache options");
2168 qobject_unref(opts);
2172 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2173 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2176 bdrv_subtree_drained_begin(bs);
2177 brq = bdrv_reopen_queue(NULL, bs, opts, true);
2178 bdrv_reopen_multiple(brq, &local_err);
2179 bdrv_subtree_drained_end(bs);
2182 error_report_err(local_err);
2186 blk_set_enable_write_cache(blk, !writethrough);
2190 static int break_f(BlockBackend *blk, int argc, char **argv)
2194 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2196 printf("Could not set breakpoint: %s\n", strerror(-ret));
2203 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
2207 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2209 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2216 static const cmdinfo_t break_cmd = {
2221 .args = "event tag",
2222 .oneline = "sets a breakpoint on event and tags the stopped "
2226 static const cmdinfo_t remove_break_cmd = {
2227 .name = "remove_break",
2230 .cfunc = remove_break_f,
2232 .oneline = "remove a breakpoint by tag",
2235 static int resume_f(BlockBackend *blk, int argc, char **argv)
2239 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2241 printf("Could not resume request: %s\n", strerror(-ret));
2248 static const cmdinfo_t resume_cmd = {
2254 .oneline = "resumes the request tagged as tag",
2257 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2259 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2260 aio_poll(blk_get_aio_context(blk), true);
2265 static const cmdinfo_t wait_break_cmd = {
2266 .name = "wait_break",
2269 .cfunc = wait_break_f,
2271 .oneline = "waits for the suspension of a request",
2274 static int abort_f(BlockBackend *blk, int argc, char **argv)
2279 static const cmdinfo_t abort_cmd = {
2282 .flags = CMD_NOFILE_OK,
2283 .oneline = "simulate a program crash using abort(3)",
2286 static void sigraise_help(void)
2290 " raises the given signal\n"
2293 " 'sigraise %i' - raises SIGTERM\n"
2295 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2296 " given to sigraise.\n"
2300 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2302 static const cmdinfo_t sigraise_cmd = {
2304 .cfunc = sigraise_f,
2307 .flags = CMD_NOFILE_OK,
2309 .oneline = "raises a signal",
2310 .help = sigraise_help,
2313 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2315 int64_t sig = cvtnum(argv[1]);
2317 print_cvtnum_err(sig, argv[1]);
2319 } else if (sig > NSIG) {
2320 printf("signal argument '%s' is too large to be a valid signal\n",
2325 /* Using raise() to kill this process does not necessarily flush all open
2326 * streams. At least stdout and stderr (although the latter should be
2327 * non-buffered anyway) should be flushed, though. */
2336 static void sleep_cb(void *opaque)
2338 bool *expired = opaque;
2342 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2346 struct QEMUTimer *timer;
2347 bool expired = false;
2349 ms = strtol(argv[1], &endptr, 0);
2350 if (ms < 0 || *endptr != '\0') {
2351 printf("%s is not a valid number\n", argv[1]);
2355 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2356 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2359 main_loop_wait(false);
2366 static const cmdinfo_t sleep_cmd = {
2371 .flags = CMD_NOFILE_OK,
2372 .oneline = "waits for the given value in milliseconds",
2375 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2380 printf("%s ", ct->name);
2382 printf("(or %s) ", ct->altname);
2387 printf("%s ", ct->args);
2389 printf("-- %s\n", ct->oneline);
2392 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2394 help_oneline(cmd, ct);
2400 static void help_all(void)
2402 const cmdinfo_t *ct;
2404 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2405 help_oneline(ct->name, ct);
2407 printf("\nUse 'help commandname' for extended help.\n");
2410 static int help_f(BlockBackend *blk, int argc, char **argv)
2412 const cmdinfo_t *ct;
2419 ct = find_command(argv[1]);
2421 printf("command %s not found\n", argv[1]);
2425 help_onecmd(argv[1], ct);
2429 static const cmdinfo_t help_cmd = {
2435 .flags = CMD_FLAG_GLOBAL,
2436 .args = "[command]",
2437 .oneline = "help for one or all commands",
2440 int qemuio_command(BlockBackend *blk, const char *cmd)
2444 const cmdinfo_t *ct;
2449 input = g_strdup(cmd);
2450 v = breakline(input, &c);
2452 ct = find_command(v[0]);
2454 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2455 aio_context_acquire(ctx);
2456 ret = command(blk, ct, c, v);
2457 aio_context_release(ctx);
2459 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2469 static void __attribute((constructor)) init_qemuio_commands(void)
2471 /* initialize commands */
2472 qemuio_add_command(&help_cmd);
2473 qemuio_add_command(&read_cmd);
2474 qemuio_add_command(&readv_cmd);
2475 qemuio_add_command(&write_cmd);
2476 qemuio_add_command(&writev_cmd);
2477 qemuio_add_command(&aio_read_cmd);
2478 qemuio_add_command(&aio_write_cmd);
2479 qemuio_add_command(&aio_flush_cmd);
2480 qemuio_add_command(&flush_cmd);
2481 qemuio_add_command(&truncate_cmd);
2482 qemuio_add_command(&length_cmd);
2483 qemuio_add_command(&info_cmd);
2484 qemuio_add_command(&discard_cmd);
2485 qemuio_add_command(&alloc_cmd);
2486 qemuio_add_command(&map_cmd);
2487 qemuio_add_command(&reopen_cmd);
2488 qemuio_add_command(&break_cmd);
2489 qemuio_add_command(&remove_break_cmd);
2490 qemuio_add_command(&resume_cmd);
2491 qemuio_add_command(&wait_break_cmd);
2492 qemuio_add_command(&abort_cmd);
2493 qemuio_add_command(&sleep_cmd);
2494 qemuio_add_command(&sigraise_cmd);