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);
406 if (len > pattern_len) {
411 size_t len_to_copy = MIN(pattern_len, len);
413 memcpy(buf, buf_origin, len_to_copy);
423 qemu_io_free(buf_origin);
430 static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
436 for (i = 0, p = buffer; i < len; i += 16) {
437 const uint8_t *s = p;
439 printf("%08" PRIx64 ": ", offset + i);
440 for (j = 0; j < 16 && i + j < len; j++, p++) {
444 for (j = 0; j < 16 && i + j < len; j++, s++) {
455 static void print_report(const char *op, struct timespec *t, int64_t offset,
456 int64_t count, int64_t total, int cnt, bool Cflag)
458 char s1[64], s2[64], ts[64];
460 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
462 cvtstr((double)total, s1, sizeof(s1));
463 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
464 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
465 op, total, count, offset);
466 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
467 s1, cnt, ts, s2, tdiv((double)cnt, *t));
468 } else {/* bytes,ops,time,bytes/sec,ops/sec */
469 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
471 tdiv((double)total, *t),
472 tdiv((double)cnt, *t));
477 * Parse multiple length statements for vectored I/O, and construct an I/O
478 * vector matching it.
481 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
484 size_t *sizes = g_new0(size_t, nr_iov);
490 for (i = 0; i < nr_iov; i++) {
496 print_cvtnum_err(len, arg);
500 if (len > BDRV_REQUEST_MAX_BYTES) {
501 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
502 (uint64_t)BDRV_REQUEST_MAX_BYTES);
506 if (count > BDRV_REQUEST_MAX_BYTES - len) {
507 printf("The total number of bytes exceed the maximum size %" PRIu64
508 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
516 qemu_iovec_init(qiov, nr_iov);
518 buf = p = qemu_io_alloc(blk, count, pattern);
520 for (i = 0; i < nr_iov; i++) {
521 qemu_iovec_add(qiov, p, sizes[i]);
530 static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
531 int64_t bytes, int64_t *total)
533 if (bytes > INT_MAX) {
537 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
544 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
545 int64_t bytes, int flags, int64_t *total)
547 if (bytes > INT_MAX) {
551 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
568 static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
570 CoWriteZeroes *data = opaque;
572 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
576 *data->total = data->ret;
580 *data->total = data->bytes;
583 static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
584 int64_t bytes, int flags, int64_t *total)
587 CoWriteZeroes data = {
596 if (bytes > INT_MAX) {
600 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
601 bdrv_coroutine_enter(blk_bs(blk), co);
603 aio_poll(blk_get_aio_context(blk), true);
612 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
613 int64_t bytes, int64_t *total)
617 if (bytes > BDRV_REQUEST_MAX_BYTES) {
621 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
629 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
630 int64_t count, int64_t *total)
632 if (count > INT_MAX) {
636 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
643 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
644 int64_t count, int64_t *total)
646 if (count > INT_MAX) {
650 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
657 #define NOT_DONE 0x7fffffff
658 static void aio_rw_done(void *opaque, int ret)
660 *(int *)opaque = ret;
663 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
664 int64_t offset, int *total)
666 int async_ret = NOT_DONE;
668 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
669 while (async_ret == NOT_DONE) {
670 main_loop_wait(false);
674 return async_ret < 0 ? async_ret : 1;
677 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
678 int64_t offset, int flags, int *total)
680 int async_ret = NOT_DONE;
682 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
683 while (async_ret == NOT_DONE) {
684 main_loop_wait(false);
688 return async_ret < 0 ? async_ret : 1;
691 static void read_help(void)
695 " reads a range of bytes from the given offset\n"
698 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
700 " Reads a segment of the currently open file, optionally dumping it to the\n"
701 " standard output stream (with -v option) for subsequent inspection.\n"
702 " -b, -- read from the VM state rather than the virtual disk\n"
703 " -C, -- report statistics in a machine parsable format\n"
704 " -l, -- length for pattern verification (only with -P)\n"
705 " -p, -- ignored for backwards compatibility\n"
706 " -P, -- use a pattern to verify read data\n"
707 " -q, -- quiet mode, do not show I/O statistics\n"
708 " -s, -- start offset for pattern verification (only with -P)\n"
709 " -v, -- dump buffer to standard output\n"
713 static int read_f(BlockBackend *blk, int argc, char **argv);
715 static const cmdinfo_t read_cmd = {
721 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
722 .oneline = "reads a number of bytes at a specified offset",
726 static int read_f(BlockBackend *blk, int argc, char **argv)
728 struct timespec t1, t2;
729 bool Cflag = false, qflag = false, vflag = false;
730 bool Pflag = false, sflag = false, lflag = false, bflag = false;
735 /* Some compilers get confused and warn if this is not initialized. */
738 int64_t pattern_offset = 0, pattern_count = 0;
740 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
750 pattern_count = cvtnum(optarg);
751 if (pattern_count < 0) {
752 print_cvtnum_err(pattern_count, optarg);
753 return pattern_count;
757 /* Ignored for backwards compatibility */
761 pattern = parse_pattern(optarg);
771 pattern_offset = cvtnum(optarg);
772 if (pattern_offset < 0) {
773 print_cvtnum_err(pattern_offset, optarg);
774 return pattern_offset;
781 qemuio_command_usage(&read_cmd);
786 if (optind != argc - 2) {
787 qemuio_command_usage(&read_cmd);
791 offset = cvtnum(argv[optind]);
793 print_cvtnum_err(offset, argv[optind]);
798 count = cvtnum(argv[optind]);
800 print_cvtnum_err(count, argv[optind]);
802 } else if (count > BDRV_REQUEST_MAX_BYTES) {
803 printf("length cannot exceed %" PRIu64 ", given %s\n",
804 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
808 if (!Pflag && (lflag || sflag)) {
809 qemuio_command_usage(&read_cmd);
814 pattern_count = count - pattern_offset;
817 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
818 printf("pattern verification range exceeds end of read data\n");
823 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
824 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
828 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
829 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
835 buf = qemu_io_alloc(blk, count, 0xab);
837 clock_gettime(CLOCK_MONOTONIC, &t1);
839 ret = do_load_vmstate(blk, buf, offset, count, &total);
841 ret = do_pread(blk, buf, offset, count, &total);
843 clock_gettime(CLOCK_MONOTONIC, &t2);
846 printf("read failed: %s\n", strerror(-ret));
854 void *cmp_buf = g_malloc(pattern_count);
855 memset(cmp_buf, pattern, pattern_count);
856 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
857 printf("Pattern verification failed at offset %"
858 PRId64 ", %"PRId64" bytes\n",
859 offset + pattern_offset, pattern_count);
870 dump_buffer(buf, offset, count);
873 /* Finally, report back -- -C gives a parsable format */
875 print_report("read", &t2, offset, count, total, cnt, Cflag);
882 static void readv_help(void)
886 " reads a range of bytes from the given offset into multiple buffers\n"
889 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
891 " Reads a segment of the currently open file, optionally dumping it to the\n"
892 " standard output stream (with -v option) for subsequent inspection.\n"
893 " Uses multiple iovec buffers if more than one byte range is specified.\n"
894 " -C, -- report statistics in a machine parsable format\n"
895 " -P, -- use a pattern to verify read data\n"
896 " -v, -- dump buffer to standard output\n"
897 " -q, -- quiet mode, do not show I/O statistics\n"
901 static int readv_f(BlockBackend *blk, int argc, char **argv);
903 static const cmdinfo_t readv_cmd = {
908 .args = "[-Cqv] [-P pattern] off len [len..]",
909 .oneline = "reads a number of bytes at a specified offset",
913 static int readv_f(BlockBackend *blk, int argc, char **argv)
915 struct timespec t1, t2;
916 bool Cflag = false, qflag = false, vflag = false;
920 /* Some compilers get confused and warn if this is not initialized. */
927 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
934 pattern = parse_pattern(optarg);
946 qemuio_command_usage(&readv_cmd);
951 if (optind > argc - 2) {
952 qemuio_command_usage(&readv_cmd);
957 offset = cvtnum(argv[optind]);
959 print_cvtnum_err(offset, argv[optind]);
964 nr_iov = argc - optind;
965 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
970 clock_gettime(CLOCK_MONOTONIC, &t1);
971 ret = do_aio_readv(blk, &qiov, offset, &total);
972 clock_gettime(CLOCK_MONOTONIC, &t2);
975 printf("readv failed: %s\n", strerror(-ret));
983 void *cmp_buf = g_malloc(qiov.size);
984 memset(cmp_buf, pattern, qiov.size);
985 if (memcmp(buf, cmp_buf, qiov.size)) {
986 printf("Pattern verification failed at offset %"
987 PRId64 ", %zu bytes\n", offset, qiov.size);
998 dump_buffer(buf, offset, qiov.size);
1001 /* Finally, report back -- -C gives a parsable format */
1003 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1006 qemu_iovec_destroy(&qiov);
1011 static void write_help(void)
1015 " writes a range of bytes from the given offset\n"
1018 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1020 " Writes into a segment of the currently open file, using a buffer\n"
1021 " filled with a set pattern (0xcdcdcdcd).\n"
1022 " -b, -- write to the VM state rather than the virtual disk\n"
1023 " -c, -- write compressed data with blk_write_compressed\n"
1024 " -f, -- use Force Unit Access semantics\n"
1025 " -n, -- with -z, don't allow slow fallback\n"
1026 " -p, -- ignored for backwards compatibility\n"
1027 " -P, -- use different pattern to fill file\n"
1028 " -s, -- use a pattern file to fill the write buffer\n"
1029 " -C, -- report statistics in a machine parsable format\n"
1030 " -q, -- quiet mode, do not show I/O statistics\n"
1031 " -u, -- with -z, allow unmapping\n"
1032 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
1036 static int write_f(BlockBackend *blk, int argc, char **argv);
1038 static const cmdinfo_t write_cmd = {
1042 .perm = BLK_PERM_WRITE,
1045 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
1046 .oneline = "writes a number of bytes at a specified offset",
1050 static int write_f(BlockBackend *blk, int argc, char **argv)
1052 struct timespec t1, t2;
1053 bool Cflag = false, qflag = false, bflag = false;
1054 bool Pflag = false, zflag = false, cflag = false, sflag = false;
1060 /* Some compilers get confused and warn if this is not initialized. */
1063 const char *file_name = NULL;
1065 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
1077 flags |= BDRV_REQ_FUA;
1080 flags |= BDRV_REQ_NO_FALLBACK;
1083 /* Ignored for backwards compatibility */
1087 pattern = parse_pattern(optarg);
1100 flags |= BDRV_REQ_MAY_UNMAP;
1106 qemuio_command_usage(&write_cmd);
1111 if (optind != argc - 2) {
1112 qemuio_command_usage(&write_cmd);
1116 if (bflag && zflag) {
1117 printf("-b and -z cannot be specified at the same time\n");
1121 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1122 printf("-f and -b or -c cannot be specified at the same time\n");
1126 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1127 printf("-n requires -z to be specified\n");
1131 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1132 printf("-u requires -z to be specified\n");
1136 if (zflag + Pflag + sflag > 1) {
1137 printf("Only one of -z, -P, and -s "
1138 "can be specified at the same time\n");
1142 offset = cvtnum(argv[optind]);
1144 print_cvtnum_err(offset, argv[optind]);
1149 count = cvtnum(argv[optind]);
1151 print_cvtnum_err(count, argv[optind]);
1153 } else if (count > BDRV_REQUEST_MAX_BYTES) {
1154 printf("length cannot exceed %" PRIu64 ", given %s\n",
1155 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1159 if (bflag || cflag) {
1160 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1161 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
1166 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1167 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
1175 buf = qemu_io_alloc_from_file(blk, count, file_name);
1180 buf = qemu_io_alloc(blk, count, pattern);
1184 clock_gettime(CLOCK_MONOTONIC, &t1);
1186 ret = do_save_vmstate(blk, buf, offset, count, &total);
1188 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1190 ret = do_write_compressed(blk, buf, offset, count, &total);
1192 ret = do_pwrite(blk, buf, offset, count, flags, &total);
1194 clock_gettime(CLOCK_MONOTONIC, &t2);
1197 printf("write failed: %s\n", strerror(-ret));
1208 /* Finally, report back -- -C gives a parsable format */
1210 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1224 " writes a range of bytes from the given offset source from multiple buffers\n"
1227 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1229 " Writes into a segment of the currently open file, using a buffer\n"
1230 " filled with a set pattern (0xcdcdcdcd).\n"
1231 " -P, -- use different pattern to fill file\n"
1232 " -C, -- report statistics in a machine parsable format\n"
1233 " -f, -- use Force Unit Access semantics\n"
1234 " -q, -- quiet mode, do not show I/O statistics\n"
1238 static int writev_f(BlockBackend *blk, int argc, char **argv);
1240 static const cmdinfo_t writev_cmd = {
1243 .perm = BLK_PERM_WRITE,
1246 .args = "[-Cfq] [-P pattern] off len [len..]",
1247 .oneline = "writes a number of bytes at a specified offset",
1248 .help = writev_help,
1251 static int writev_f(BlockBackend *blk, int argc, char **argv)
1253 struct timespec t1, t2;
1254 bool Cflag = false, qflag = false;
1259 /* Some compilers get confused and warn if this is not initialized. */
1265 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1271 flags |= BDRV_REQ_FUA;
1277 pattern = parse_pattern(optarg);
1283 qemuio_command_usage(&writev_cmd);
1288 if (optind > argc - 2) {
1289 qemuio_command_usage(&writev_cmd);
1293 offset = cvtnum(argv[optind]);
1295 print_cvtnum_err(offset, argv[optind]);
1300 nr_iov = argc - optind;
1301 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1306 clock_gettime(CLOCK_MONOTONIC, &t1);
1307 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
1308 clock_gettime(CLOCK_MONOTONIC, &t2);
1311 printf("writev failed: %s\n", strerror(-ret));
1322 /* Finally, report back -- -C gives a parsable format */
1324 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1326 qemu_iovec_destroy(&qiov);
1341 BlockAcctCookie acct;
1346 static void aio_write_done(void *opaque, int ret)
1348 struct aio_ctx *ctx = opaque;
1351 clock_gettime(CLOCK_MONOTONIC, &t2);
1355 printf("aio_write failed: %s\n", strerror(-ret));
1356 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1360 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1366 /* Finally, report back -- -C gives a parsable format */
1367 t2 = tsub(t2, ctx->t1);
1368 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1369 ctx->qiov.size, 1, ctx->Cflag);
1372 qemu_io_free(ctx->buf);
1373 qemu_iovec_destroy(&ctx->qiov);
1378 static void aio_read_done(void *opaque, int ret)
1380 struct aio_ctx *ctx = opaque;
1383 clock_gettime(CLOCK_MONOTONIC, &t2);
1386 printf("readv failed: %s\n", strerror(-ret));
1387 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1392 void *cmp_buf = g_malloc(ctx->qiov.size);
1394 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1395 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1396 printf("Pattern verification failed at offset %"
1397 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
1402 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1409 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1412 /* Finally, report back -- -C gives a parsable format */
1413 t2 = tsub(t2, ctx->t1);
1414 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1415 ctx->qiov.size, 1, ctx->Cflag);
1417 qemu_io_free(ctx->buf);
1418 qemu_iovec_destroy(&ctx->qiov);
1422 static void aio_read_help(void)
1426 " asynchronously reads a range of bytes from the given offset\n"
1429 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1431 " Reads a segment of the currently open file, optionally dumping it to the\n"
1432 " standard output stream (with -v option) for subsequent inspection.\n"
1433 " The read is performed asynchronously and the aio_flush command must be\n"
1434 " used to ensure all outstanding aio requests have been completed.\n"
1435 " Note that due to its asynchronous nature, this command will be\n"
1436 " considered successful once the request is submitted, independently\n"
1437 " of potential I/O errors or pattern mismatches.\n"
1438 " -C, -- report statistics in a machine parsable format\n"
1439 " -P, -- use a pattern to verify read data\n"
1440 " -i, -- treat request as invalid, for exercising stats\n"
1441 " -v, -- dump buffer to standard output\n"
1442 " -q, -- quiet mode, do not show I/O statistics\n"
1446 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1448 static const cmdinfo_t aio_read_cmd = {
1450 .cfunc = aio_read_f,
1453 .args = "[-Ciqv] [-P pattern] off len [len..]",
1454 .oneline = "asynchronously reads a number of bytes",
1455 .help = aio_read_help,
1458 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1461 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1464 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1471 ctx->pattern = parse_pattern(optarg);
1472 if (ctx->pattern < 0) {
1478 printf("injecting invalid read request\n");
1479 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1490 qemuio_command_usage(&aio_read_cmd);
1495 if (optind > argc - 2) {
1497 qemuio_command_usage(&aio_read_cmd);
1501 ctx->offset = cvtnum(argv[optind]);
1502 if (ctx->offset < 0) {
1503 int ret = ctx->offset;
1504 print_cvtnum_err(ret, argv[optind]);
1510 nr_iov = argc - optind;
1511 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1512 if (ctx->buf == NULL) {
1513 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1518 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1519 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1521 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1525 static void aio_write_help(void)
1529 " asynchronously writes a range of bytes from the given offset source\n"
1530 " from multiple buffers\n"
1533 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1535 " Writes into a segment of the currently open file, using a buffer\n"
1536 " filled with a set pattern (0xcdcdcdcd).\n"
1537 " The write is performed asynchronously and the aio_flush command must be\n"
1538 " used to ensure all outstanding aio requests have been completed.\n"
1539 " Note that due to its asynchronous nature, this command will be\n"
1540 " considered successful once the request is submitted, independently\n"
1541 " of potential I/O errors or pattern mismatches.\n"
1542 " -P, -- use different pattern to fill file\n"
1543 " -C, -- report statistics in a machine parsable format\n"
1544 " -f, -- use Force Unit Access semantics\n"
1545 " -i, -- treat request as invalid, for exercising stats\n"
1546 " -q, -- quiet mode, do not show I/O statistics\n"
1547 " -u, -- with -z, allow unmapping\n"
1548 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1552 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1554 static const cmdinfo_t aio_write_cmd = {
1555 .name = "aio_write",
1556 .cfunc = aio_write_f,
1557 .perm = BLK_PERM_WRITE,
1560 .args = "[-Cfiquz] [-P pattern] off len [len..]",
1561 .oneline = "asynchronously writes a number of bytes",
1562 .help = aio_write_help,
1565 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1569 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1573 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1579 flags |= BDRV_REQ_FUA;
1585 flags |= BDRV_REQ_MAY_UNMAP;
1588 pattern = parse_pattern(optarg);
1595 printf("injecting invalid write request\n");
1596 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1604 qemuio_command_usage(&aio_write_cmd);
1609 if (optind > argc - 2) {
1611 qemuio_command_usage(&aio_write_cmd);
1615 if (ctx->zflag && optind != argc - 2) {
1616 printf("-z supports only a single length parameter\n");
1621 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1622 printf("-u requires -z to be specified\n");
1627 if (ctx->zflag && ctx->Pflag) {
1628 printf("-z and -P cannot be specified at the same time\n");
1633 ctx->offset = cvtnum(argv[optind]);
1634 if (ctx->offset < 0) {
1635 int ret = ctx->offset;
1636 print_cvtnum_err(ret, argv[optind]);
1643 int64_t count = cvtnum(argv[optind]);
1645 print_cvtnum_err(count, argv[optind]);
1650 ctx->qiov.size = count;
1651 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1654 nr_iov = argc - optind;
1655 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1657 if (ctx->buf == NULL) {
1658 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1663 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1664 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1667 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1674 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1676 BlockAcctCookie cookie;
1677 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
1679 block_acct_done(blk_get_stats(blk), &cookie);
1683 static const cmdinfo_t aio_flush_cmd = {
1684 .name = "aio_flush",
1685 .cfunc = aio_flush_f,
1686 .oneline = "completes all outstanding aio requests"
1689 static int flush_f(BlockBackend *blk, int argc, char **argv)
1691 return blk_flush(blk);
1694 static const cmdinfo_t flush_cmd = {
1698 .oneline = "flush all in-core file state to disk",
1701 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1703 Error *local_err = NULL;
1707 offset = cvtnum(argv[1]);
1709 print_cvtnum_err(offset, argv[1]);
1714 * qemu-io is a debugging tool, so let us be strict here and pass
1715 * exact=true. It is better to err on the "emit more errors" side
1716 * than to be overly permissive.
1718 ret = blk_truncate(blk, offset, true, PREALLOC_MODE_OFF, &local_err);
1720 error_report_err(local_err);
1727 static const cmdinfo_t truncate_cmd = {
1730 .cfunc = truncate_f,
1731 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1735 .oneline = "truncates the current file at the given offset",
1738 static int length_f(BlockBackend *blk, int argc, char **argv)
1743 size = blk_getlength(blk);
1745 printf("getlength: %s\n", strerror(-size));
1749 cvtstr(size, s1, sizeof(s1));
1755 static const cmdinfo_t length_cmd = {
1759 .oneline = "gets the length of the current file",
1763 static int info_f(BlockBackend *blk, int argc, char **argv)
1765 BlockDriverState *bs = blk_bs(blk);
1766 BlockDriverInfo bdi;
1767 ImageInfoSpecific *spec_info;
1768 Error *local_err = NULL;
1769 char s1[64], s2[64];
1772 if (bs->drv && bs->drv->format_name) {
1773 printf("format name: %s\n", bs->drv->format_name);
1775 if (bs->drv && bs->drv->protocol_name) {
1776 printf("format name: %s\n", bs->drv->protocol_name);
1779 ret = bdrv_get_info(bs, &bdi);
1784 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1785 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1787 printf("cluster size: %s\n", s1);
1788 printf("vm state offset: %s\n", s2);
1790 spec_info = bdrv_get_specific_info(bs, &local_err);
1792 error_report_err(local_err);
1796 printf("Format specific information:\n");
1797 bdrv_image_info_specific_dump(spec_info);
1798 qapi_free_ImageInfoSpecific(spec_info);
1806 static const cmdinfo_t info_cmd = {
1810 .oneline = "prints information about the current file",
1813 static void discard_help(void)
1817 " discards a range of bytes from the given offset\n"
1820 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1822 " Discards a segment of the currently open file.\n"
1823 " -C, -- report statistics in a machine parsable format\n"
1824 " -q, -- quiet mode, do not show I/O statistics\n"
1828 static int discard_f(BlockBackend *blk, int argc, char **argv);
1830 static const cmdinfo_t discard_cmd = {
1834 .perm = BLK_PERM_WRITE,
1837 .args = "[-Cq] off len",
1838 .oneline = "discards a number of bytes at a specified offset",
1839 .help = discard_help,
1842 static int discard_f(BlockBackend *blk, int argc, char **argv)
1844 struct timespec t1, t2;
1845 bool Cflag = false, qflag = false;
1847 int64_t offset, bytes;
1849 while ((c = getopt(argc, argv, "Cq")) != -1) {
1858 qemuio_command_usage(&discard_cmd);
1863 if (optind != argc - 2) {
1864 qemuio_command_usage(&discard_cmd);
1868 offset = cvtnum(argv[optind]);
1870 print_cvtnum_err(offset, argv[optind]);
1875 bytes = cvtnum(argv[optind]);
1877 print_cvtnum_err(bytes, argv[optind]);
1879 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
1880 printf("length cannot exceed %"PRIu64", given %s\n",
1881 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1885 clock_gettime(CLOCK_MONOTONIC, &t1);
1886 ret = blk_pdiscard(blk, offset, bytes);
1887 clock_gettime(CLOCK_MONOTONIC, &t2);
1890 printf("discard failed: %s\n", strerror(-ret));
1894 /* Finally, report back -- -C gives a parsable format */
1897 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
1903 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1905 BlockDriverState *bs = blk_bs(blk);
1906 int64_t offset, start, remaining, count;
1909 int64_t num, sum_alloc;
1911 start = offset = cvtnum(argv[1]);
1913 print_cvtnum_err(offset, argv[1]);
1918 count = cvtnum(argv[2]);
1920 print_cvtnum_err(count, argv[2]);
1924 count = BDRV_SECTOR_SIZE;
1930 ret = bdrv_is_allocated(bs, offset, remaining, &num);
1932 printf("is_allocated failed: %s\n", strerror(-ret));
1946 cvtstr(start, s1, sizeof(s1));
1948 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
1949 sum_alloc, count, s1);
1953 static const cmdinfo_t alloc_cmd = {
1959 .args = "offset [count]",
1960 .oneline = "checks if offset is allocated in the file",
1964 static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1965 int64_t bytes, int64_t *pnum)
1971 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1972 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1980 while (bytes > 0 && ret == firstret) {
1984 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1985 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1986 if (ret == firstret && num) {
1996 static int map_f(BlockBackend *blk, int argc, char **argv)
1998 int64_t offset, bytes;
1999 char s1[64], s2[64];
2005 bytes = blk_getlength(blk);
2007 error_report("Failed to query image length: %s", strerror(-bytes));
2012 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
2014 error_report("Failed to get allocation status: %s", strerror(-ret));
2017 error_report("Unexpected end of image");
2021 retstr = ret ? " allocated" : "not allocated";
2022 cvtstr(num, s1, sizeof(s1));
2023 cvtstr(offset, s2, sizeof(s2));
2024 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
2025 s1, num, retstr, s2, offset);
2034 static const cmdinfo_t map_cmd = {
2040 .oneline = "prints the allocated areas of a file",
2043 static void reopen_help(void)
2047 " Changes the open options of an already opened image\n"
2050 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2052 " -r, -- Reopen the image read-only\n"
2053 " -w, -- Reopen the image read-write\n"
2054 " -c, -- Change the cache mode to the given value\n"
2055 " -o, -- Changes block driver options (cf. 'open' command)\n"
2059 static int reopen_f(BlockBackend *blk, int argc, char **argv);
2061 static QemuOptsList reopen_opts = {
2063 .merge_lists = true,
2064 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2066 /* no elements => accept any params */
2067 { /* end of list */ }
2071 static const cmdinfo_t reopen_cmd = {
2076 .args = "[(-r|-w)] [-c cache] [-o options]",
2077 .oneline = "reopens an image with new options",
2078 .help = reopen_help,
2081 static int reopen_f(BlockBackend *blk, int argc, char **argv)
2083 BlockDriverState *bs = blk_bs(blk);
2087 int flags = bs->open_flags;
2088 bool writethrough = !blk_enable_write_cache(blk);
2089 bool has_rw_option = false;
2090 bool has_cache_option = false;
2092 BlockReopenQueue *brq;
2093 Error *local_err = NULL;
2095 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
2098 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
2099 error_report("Invalid cache option: %s", optarg);
2102 has_cache_option = true;
2105 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2106 qemu_opts_reset(&reopen_opts);
2111 if (has_rw_option) {
2112 error_report("Only one -r/-w option may be given");
2115 flags &= ~BDRV_O_RDWR;
2116 has_rw_option = true;
2119 if (has_rw_option) {
2120 error_report("Only one -r/-w option may be given");
2123 flags |= BDRV_O_RDWR;
2124 has_rw_option = true;
2127 qemu_opts_reset(&reopen_opts);
2128 qemuio_command_usage(&reopen_cmd);
2133 if (optind != argc) {
2134 qemu_opts_reset(&reopen_opts);
2135 qemuio_command_usage(&reopen_cmd);
2139 if (!writethrough != blk_enable_write_cache(blk) &&
2140 blk_get_attached_dev(blk))
2142 error_report("Cannot change cache.writeback: Device attached");
2143 qemu_opts_reset(&reopen_opts);
2147 if (!(flags & BDRV_O_RDWR)) {
2148 uint64_t orig_perm, orig_shared_perm;
2152 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2154 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2159 qopts = qemu_opts_find(&reopen_opts, NULL);
2160 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
2161 qemu_opts_reset(&reopen_opts);
2163 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2164 if (has_rw_option) {
2165 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2166 qobject_unref(opts);
2170 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2173 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2174 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2175 if (has_cache_option) {
2176 error_report("Cannot set both -c and the cache options");
2177 qobject_unref(opts);
2181 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2182 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2185 bdrv_subtree_drained_begin(bs);
2186 brq = bdrv_reopen_queue(NULL, bs, opts, true);
2187 bdrv_reopen_multiple(brq, &local_err);
2188 bdrv_subtree_drained_end(bs);
2191 error_report_err(local_err);
2195 blk_set_enable_write_cache(blk, !writethrough);
2199 static int break_f(BlockBackend *blk, int argc, char **argv)
2203 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2205 printf("Could not set breakpoint: %s\n", strerror(-ret));
2212 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
2216 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2218 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2225 static const cmdinfo_t break_cmd = {
2230 .args = "event tag",
2231 .oneline = "sets a breakpoint on event and tags the stopped "
2235 static const cmdinfo_t remove_break_cmd = {
2236 .name = "remove_break",
2239 .cfunc = remove_break_f,
2241 .oneline = "remove a breakpoint by tag",
2244 static int resume_f(BlockBackend *blk, int argc, char **argv)
2248 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2250 printf("Could not resume request: %s\n", strerror(-ret));
2257 static const cmdinfo_t resume_cmd = {
2263 .oneline = "resumes the request tagged as tag",
2266 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2268 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2269 aio_poll(blk_get_aio_context(blk), true);
2274 static const cmdinfo_t wait_break_cmd = {
2275 .name = "wait_break",
2278 .cfunc = wait_break_f,
2280 .oneline = "waits for the suspension of a request",
2283 static int abort_f(BlockBackend *blk, int argc, char **argv)
2288 static const cmdinfo_t abort_cmd = {
2291 .flags = CMD_NOFILE_OK,
2292 .oneline = "simulate a program crash using abort(3)",
2295 static void sigraise_help(void)
2299 " raises the given signal\n"
2302 " 'sigraise %i' - raises SIGTERM\n"
2304 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2305 " given to sigraise.\n"
2309 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2311 static const cmdinfo_t sigraise_cmd = {
2313 .cfunc = sigraise_f,
2316 .flags = CMD_NOFILE_OK,
2318 .oneline = "raises a signal",
2319 .help = sigraise_help,
2322 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2324 int64_t sig = cvtnum(argv[1]);
2326 print_cvtnum_err(sig, argv[1]);
2328 } else if (sig > NSIG) {
2329 printf("signal argument '%s' is too large to be a valid signal\n",
2334 /* Using raise() to kill this process does not necessarily flush all open
2335 * streams. At least stdout and stderr (although the latter should be
2336 * non-buffered anyway) should be flushed, though. */
2345 static void sleep_cb(void *opaque)
2347 bool *expired = opaque;
2351 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2355 struct QEMUTimer *timer;
2356 bool expired = false;
2358 ms = strtol(argv[1], &endptr, 0);
2359 if (ms < 0 || *endptr != '\0') {
2360 printf("%s is not a valid number\n", argv[1]);
2364 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2365 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2368 main_loop_wait(false);
2375 static const cmdinfo_t sleep_cmd = {
2380 .flags = CMD_NOFILE_OK,
2381 .oneline = "waits for the given value in milliseconds",
2384 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2389 printf("%s ", ct->name);
2391 printf("(or %s) ", ct->altname);
2396 printf("%s ", ct->args);
2398 printf("-- %s\n", ct->oneline);
2401 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2403 help_oneline(cmd, ct);
2409 static void help_all(void)
2411 const cmdinfo_t *ct;
2413 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2414 help_oneline(ct->name, ct);
2416 printf("\nUse 'help commandname' for extended help.\n");
2419 static int help_f(BlockBackend *blk, int argc, char **argv)
2421 const cmdinfo_t *ct;
2428 ct = find_command(argv[1]);
2430 printf("command %s not found\n", argv[1]);
2434 help_onecmd(argv[1], ct);
2438 static const cmdinfo_t help_cmd = {
2444 .flags = CMD_FLAG_GLOBAL,
2445 .args = "[command]",
2446 .oneline = "help for one or all commands",
2449 int qemuio_command(BlockBackend *blk, const char *cmd)
2453 const cmdinfo_t *ct;
2458 input = g_strdup(cmd);
2459 v = breakline(input, &c);
2461 ct = find_command(v[0]);
2463 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2464 aio_context_acquire(ctx);
2465 ret = command(blk, ct, c, v);
2466 aio_context_release(ctx);
2468 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2478 static void __attribute((constructor)) init_qemuio_commands(void)
2480 /* initialize commands */
2481 qemuio_add_command(&help_cmd);
2482 qemuio_add_command(&read_cmd);
2483 qemuio_add_command(&readv_cmd);
2484 qemuio_add_command(&write_cmd);
2485 qemuio_add_command(&writev_cmd);
2486 qemuio_add_command(&aio_read_cmd);
2487 qemuio_add_command(&aio_write_cmd);
2488 qemuio_add_command(&aio_flush_cmd);
2489 qemuio_add_command(&flush_cmd);
2490 qemuio_add_command(&truncate_cmd);
2491 qemuio_add_command(&length_cmd);
2492 qemuio_add_command(&info_cmd);
2493 qemuio_add_command(&discard_cmd);
2494 qemuio_add_command(&alloc_cmd);
2495 qemuio_add_command(&map_cmd);
2496 qemuio_add_command(&reopen_cmd);
2497 qemuio_add_command(&break_cmd);
2498 qemuio_add_command(&remove_break_cmd);
2499 qemuio_add_command(&resume_cmd);
2500 qemuio_add_command(&wait_break_cmd);
2501 qemuio_add_command(&abort_cmd);
2502 qemuio_add_command(&sleep_cmd);
2503 qemuio_add_command(&sigraise_cmd);