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;
353 static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
359 for (i = 0, p = buffer; i < len; i += 16) {
360 const uint8_t *s = p;
362 printf("%08" PRIx64 ": ", offset + i);
363 for (j = 0; j < 16 && i + j < len; j++, p++) {
367 for (j = 0; j < 16 && i + j < len; j++, s++) {
378 static void print_report(const char *op, struct timespec *t, int64_t offset,
379 int64_t count, int64_t total, int cnt, bool Cflag)
381 char s1[64], s2[64], ts[64];
383 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
385 cvtstr((double)total, s1, sizeof(s1));
386 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
387 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
388 op, total, count, offset);
389 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
390 s1, cnt, ts, s2, tdiv((double)cnt, *t));
391 } else {/* bytes,ops,time,bytes/sec,ops/sec */
392 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
394 tdiv((double)total, *t),
395 tdiv((double)cnt, *t));
400 * Parse multiple length statements for vectored I/O, and construct an I/O
401 * vector matching it.
404 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
407 size_t *sizes = g_new0(size_t, nr_iov);
413 for (i = 0; i < nr_iov; i++) {
419 print_cvtnum_err(len, arg);
423 if (len > BDRV_REQUEST_MAX_BYTES) {
424 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
425 (uint64_t)BDRV_REQUEST_MAX_BYTES);
429 if (count > BDRV_REQUEST_MAX_BYTES - len) {
430 printf("The total number of bytes exceed the maximum size %" PRIu64
431 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
439 qemu_iovec_init(qiov, nr_iov);
441 buf = p = qemu_io_alloc(blk, count, pattern);
443 for (i = 0; i < nr_iov; i++) {
444 qemu_iovec_add(qiov, p, sizes[i]);
453 static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
454 int64_t bytes, int64_t *total)
456 if (bytes > INT_MAX) {
460 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
467 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
468 int64_t bytes, int flags, int64_t *total)
470 if (bytes > INT_MAX) {
474 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
491 static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
493 CoWriteZeroes *data = opaque;
495 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
499 *data->total = data->ret;
503 *data->total = data->bytes;
506 static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
507 int64_t bytes, int flags, int64_t *total)
510 CoWriteZeroes data = {
519 if (bytes > INT_MAX) {
523 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
524 bdrv_coroutine_enter(blk_bs(blk), co);
526 aio_poll(blk_get_aio_context(blk), true);
535 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
536 int64_t bytes, int64_t *total)
540 if (bytes > BDRV_REQUEST_MAX_BYTES) {
544 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
552 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
553 int64_t count, int64_t *total)
555 if (count > INT_MAX) {
559 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
566 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
567 int64_t count, int64_t *total)
569 if (count > INT_MAX) {
573 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
580 #define NOT_DONE 0x7fffffff
581 static void aio_rw_done(void *opaque, int ret)
583 *(int *)opaque = ret;
586 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
587 int64_t offset, int *total)
589 int async_ret = NOT_DONE;
591 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
592 while (async_ret == NOT_DONE) {
593 main_loop_wait(false);
597 return async_ret < 0 ? async_ret : 1;
600 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
601 int64_t offset, int flags, int *total)
603 int async_ret = NOT_DONE;
605 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
606 while (async_ret == NOT_DONE) {
607 main_loop_wait(false);
611 return async_ret < 0 ? async_ret : 1;
614 static void read_help(void)
618 " reads a range of bytes from the given offset\n"
621 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
623 " Reads a segment of the currently open file, optionally dumping it to the\n"
624 " standard output stream (with -v option) for subsequent inspection.\n"
625 " -b, -- read from the VM state rather than the virtual disk\n"
626 " -C, -- report statistics in a machine parsable format\n"
627 " -l, -- length for pattern verification (only with -P)\n"
628 " -p, -- ignored for backwards compatibility\n"
629 " -P, -- use a pattern to verify read data\n"
630 " -q, -- quiet mode, do not show I/O statistics\n"
631 " -s, -- start offset for pattern verification (only with -P)\n"
632 " -v, -- dump buffer to standard output\n"
636 static int read_f(BlockBackend *blk, int argc, char **argv);
638 static const cmdinfo_t read_cmd = {
644 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
645 .oneline = "reads a number of bytes at a specified offset",
649 static int read_f(BlockBackend *blk, int argc, char **argv)
651 struct timespec t1, t2;
652 bool Cflag = false, qflag = false, vflag = false;
653 bool Pflag = false, sflag = false, lflag = false, bflag = false;
658 /* Some compilers get confused and warn if this is not initialized. */
661 int64_t pattern_offset = 0, pattern_count = 0;
663 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
673 pattern_count = cvtnum(optarg);
674 if (pattern_count < 0) {
675 print_cvtnum_err(pattern_count, optarg);
676 return pattern_count;
680 /* Ignored for backwards compatibility */
684 pattern = parse_pattern(optarg);
694 pattern_offset = cvtnum(optarg);
695 if (pattern_offset < 0) {
696 print_cvtnum_err(pattern_offset, optarg);
697 return pattern_offset;
704 qemuio_command_usage(&read_cmd);
709 if (optind != argc - 2) {
710 qemuio_command_usage(&read_cmd);
714 offset = cvtnum(argv[optind]);
716 print_cvtnum_err(offset, argv[optind]);
721 count = cvtnum(argv[optind]);
723 print_cvtnum_err(count, argv[optind]);
725 } else if (count > BDRV_REQUEST_MAX_BYTES) {
726 printf("length cannot exceed %" PRIu64 ", given %s\n",
727 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
731 if (!Pflag && (lflag || sflag)) {
732 qemuio_command_usage(&read_cmd);
737 pattern_count = count - pattern_offset;
740 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
741 printf("pattern verification range exceeds end of read data\n");
746 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
747 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
751 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
752 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
758 buf = qemu_io_alloc(blk, count, 0xab);
760 clock_gettime(CLOCK_MONOTONIC, &t1);
762 ret = do_load_vmstate(blk, buf, offset, count, &total);
764 ret = do_pread(blk, buf, offset, count, &total);
766 clock_gettime(CLOCK_MONOTONIC, &t2);
769 printf("read failed: %s\n", strerror(-ret));
777 void *cmp_buf = g_malloc(pattern_count);
778 memset(cmp_buf, pattern, pattern_count);
779 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
780 printf("Pattern verification failed at offset %"
781 PRId64 ", %"PRId64" bytes\n",
782 offset + pattern_offset, pattern_count);
793 dump_buffer(buf, offset, count);
796 /* Finally, report back -- -C gives a parsable format */
798 print_report("read", &t2, offset, count, total, cnt, Cflag);
805 static void readv_help(void)
809 " reads a range of bytes from the given offset into multiple buffers\n"
812 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
814 " Reads a segment of the currently open file, optionally dumping it to the\n"
815 " standard output stream (with -v option) for subsequent inspection.\n"
816 " Uses multiple iovec buffers if more than one byte range is specified.\n"
817 " -C, -- report statistics in a machine parsable format\n"
818 " -P, -- use a pattern to verify read data\n"
819 " -v, -- dump buffer to standard output\n"
820 " -q, -- quiet mode, do not show I/O statistics\n"
824 static int readv_f(BlockBackend *blk, int argc, char **argv);
826 static const cmdinfo_t readv_cmd = {
831 .args = "[-Cqv] [-P pattern] off len [len..]",
832 .oneline = "reads a number of bytes at a specified offset",
836 static int readv_f(BlockBackend *blk, int argc, char **argv)
838 struct timespec t1, t2;
839 bool Cflag = false, qflag = false, vflag = false;
843 /* Some compilers get confused and warn if this is not initialized. */
850 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
857 pattern = parse_pattern(optarg);
869 qemuio_command_usage(&readv_cmd);
874 if (optind > argc - 2) {
875 qemuio_command_usage(&readv_cmd);
880 offset = cvtnum(argv[optind]);
882 print_cvtnum_err(offset, argv[optind]);
887 nr_iov = argc - optind;
888 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
893 clock_gettime(CLOCK_MONOTONIC, &t1);
894 ret = do_aio_readv(blk, &qiov, offset, &total);
895 clock_gettime(CLOCK_MONOTONIC, &t2);
898 printf("readv failed: %s\n", strerror(-ret));
906 void *cmp_buf = g_malloc(qiov.size);
907 memset(cmp_buf, pattern, qiov.size);
908 if (memcmp(buf, cmp_buf, qiov.size)) {
909 printf("Pattern verification failed at offset %"
910 PRId64 ", %zu bytes\n", offset, qiov.size);
921 dump_buffer(buf, offset, qiov.size);
924 /* Finally, report back -- -C gives a parsable format */
926 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
929 qemu_iovec_destroy(&qiov);
934 static void write_help(void)
938 " writes a range of bytes from the given offset\n"
941 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
943 " Writes into a segment of the currently open file, using a buffer\n"
944 " filled with a set pattern (0xcdcdcdcd).\n"
945 " -b, -- write to the VM state rather than the virtual disk\n"
946 " -c, -- write compressed data with blk_write_compressed\n"
947 " -f, -- use Force Unit Access semantics\n"
948 " -n, -- with -z, don't allow slow fallback\n"
949 " -p, -- ignored for backwards compatibility\n"
950 " -P, -- use different pattern to fill file\n"
951 " -C, -- report statistics in a machine parsable format\n"
952 " -q, -- quiet mode, do not show I/O statistics\n"
953 " -u, -- with -z, allow unmapping\n"
954 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
958 static int write_f(BlockBackend *blk, int argc, char **argv);
960 static const cmdinfo_t write_cmd = {
964 .perm = BLK_PERM_WRITE,
967 .args = "[-bcCfnquz] [-P pattern] off len",
968 .oneline = "writes a number of bytes at a specified offset",
972 static int write_f(BlockBackend *blk, int argc, char **argv)
974 struct timespec t1, t2;
975 bool Cflag = false, qflag = false, bflag = false;
976 bool Pflag = false, zflag = false, cflag = false;
982 /* Some compilers get confused and warn if this is not initialized. */
986 while ((c = getopt(argc, argv, "bcCfnpP:quz")) != -1) {
998 flags |= BDRV_REQ_FUA;
1001 flags |= BDRV_REQ_NO_FALLBACK;
1004 /* Ignored for backwards compatibility */
1008 pattern = parse_pattern(optarg);
1017 flags |= BDRV_REQ_MAY_UNMAP;
1023 qemuio_command_usage(&write_cmd);
1028 if (optind != argc - 2) {
1029 qemuio_command_usage(&write_cmd);
1033 if (bflag && zflag) {
1034 printf("-b and -z cannot be specified at the same time\n");
1038 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1039 printf("-f and -b or -c cannot be specified at the same time\n");
1043 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1044 printf("-n requires -z to be specified\n");
1048 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1049 printf("-u requires -z to be specified\n");
1053 if (zflag && Pflag) {
1054 printf("-z and -P cannot be specified at the same time\n");
1058 offset = cvtnum(argv[optind]);
1060 print_cvtnum_err(offset, argv[optind]);
1065 count = cvtnum(argv[optind]);
1067 print_cvtnum_err(count, argv[optind]);
1069 } else if (count > BDRV_REQUEST_MAX_BYTES) {
1070 printf("length cannot exceed %" PRIu64 ", given %s\n",
1071 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1075 if (bflag || cflag) {
1076 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1077 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
1082 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1083 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
1090 buf = qemu_io_alloc(blk, count, pattern);
1093 clock_gettime(CLOCK_MONOTONIC, &t1);
1095 ret = do_save_vmstate(blk, buf, offset, count, &total);
1097 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1099 ret = do_write_compressed(blk, buf, offset, count, &total);
1101 ret = do_pwrite(blk, buf, offset, count, flags, &total);
1103 clock_gettime(CLOCK_MONOTONIC, &t2);
1106 printf("write failed: %s\n", strerror(-ret));
1117 /* Finally, report back -- -C gives a parsable format */
1119 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1133 " writes a range of bytes from the given offset source from multiple buffers\n"
1136 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1138 " Writes into a segment of the currently open file, using a buffer\n"
1139 " filled with a set pattern (0xcdcdcdcd).\n"
1140 " -P, -- use different pattern to fill file\n"
1141 " -C, -- report statistics in a machine parsable format\n"
1142 " -f, -- use Force Unit Access semantics\n"
1143 " -q, -- quiet mode, do not show I/O statistics\n"
1147 static int writev_f(BlockBackend *blk, int argc, char **argv);
1149 static const cmdinfo_t writev_cmd = {
1152 .perm = BLK_PERM_WRITE,
1155 .args = "[-Cfq] [-P pattern] off len [len..]",
1156 .oneline = "writes a number of bytes at a specified offset",
1157 .help = writev_help,
1160 static int writev_f(BlockBackend *blk, int argc, char **argv)
1162 struct timespec t1, t2;
1163 bool Cflag = false, qflag = false;
1168 /* Some compilers get confused and warn if this is not initialized. */
1174 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1180 flags |= BDRV_REQ_FUA;
1186 pattern = parse_pattern(optarg);
1192 qemuio_command_usage(&writev_cmd);
1197 if (optind > argc - 2) {
1198 qemuio_command_usage(&writev_cmd);
1202 offset = cvtnum(argv[optind]);
1204 print_cvtnum_err(offset, argv[optind]);
1209 nr_iov = argc - optind;
1210 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1215 clock_gettime(CLOCK_MONOTONIC, &t1);
1216 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
1217 clock_gettime(CLOCK_MONOTONIC, &t2);
1220 printf("writev failed: %s\n", strerror(-ret));
1231 /* Finally, report back -- -C gives a parsable format */
1233 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1235 qemu_iovec_destroy(&qiov);
1250 BlockAcctCookie acct;
1255 static void aio_write_done(void *opaque, int ret)
1257 struct aio_ctx *ctx = opaque;
1260 clock_gettime(CLOCK_MONOTONIC, &t2);
1264 printf("aio_write failed: %s\n", strerror(-ret));
1265 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1269 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1275 /* Finally, report back -- -C gives a parsable format */
1276 t2 = tsub(t2, ctx->t1);
1277 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1278 ctx->qiov.size, 1, ctx->Cflag);
1281 qemu_io_free(ctx->buf);
1282 qemu_iovec_destroy(&ctx->qiov);
1287 static void aio_read_done(void *opaque, int ret)
1289 struct aio_ctx *ctx = opaque;
1292 clock_gettime(CLOCK_MONOTONIC, &t2);
1295 printf("readv failed: %s\n", strerror(-ret));
1296 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1301 void *cmp_buf = g_malloc(ctx->qiov.size);
1303 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1304 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1305 printf("Pattern verification failed at offset %"
1306 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
1311 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1318 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1321 /* Finally, report back -- -C gives a parsable format */
1322 t2 = tsub(t2, ctx->t1);
1323 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1324 ctx->qiov.size, 1, ctx->Cflag);
1326 qemu_io_free(ctx->buf);
1327 qemu_iovec_destroy(&ctx->qiov);
1331 static void aio_read_help(void)
1335 " asynchronously reads a range of bytes from the given offset\n"
1338 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1340 " Reads a segment of the currently open file, optionally dumping it to the\n"
1341 " standard output stream (with -v option) for subsequent inspection.\n"
1342 " The read is performed asynchronously and the aio_flush command must be\n"
1343 " used to ensure all outstanding aio requests have been completed.\n"
1344 " Note that due to its asynchronous nature, this command will be\n"
1345 " considered successful once the request is submitted, independently\n"
1346 " of potential I/O errors or pattern mismatches.\n"
1347 " -C, -- report statistics in a machine parsable format\n"
1348 " -P, -- use a pattern to verify read data\n"
1349 " -i, -- treat request as invalid, for exercising stats\n"
1350 " -v, -- dump buffer to standard output\n"
1351 " -q, -- quiet mode, do not show I/O statistics\n"
1355 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1357 static const cmdinfo_t aio_read_cmd = {
1359 .cfunc = aio_read_f,
1362 .args = "[-Ciqv] [-P pattern] off len [len..]",
1363 .oneline = "asynchronously reads a number of bytes",
1364 .help = aio_read_help,
1367 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1370 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1373 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1380 ctx->pattern = parse_pattern(optarg);
1381 if (ctx->pattern < 0) {
1387 printf("injecting invalid read request\n");
1388 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1399 qemuio_command_usage(&aio_read_cmd);
1404 if (optind > argc - 2) {
1406 qemuio_command_usage(&aio_read_cmd);
1410 ctx->offset = cvtnum(argv[optind]);
1411 if (ctx->offset < 0) {
1412 int ret = ctx->offset;
1413 print_cvtnum_err(ret, argv[optind]);
1419 nr_iov = argc - optind;
1420 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1421 if (ctx->buf == NULL) {
1422 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1427 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1428 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1430 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1434 static void aio_write_help(void)
1438 " asynchronously writes a range of bytes from the given offset source\n"
1439 " from multiple buffers\n"
1442 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1444 " Writes into a segment of the currently open file, using a buffer\n"
1445 " filled with a set pattern (0xcdcdcdcd).\n"
1446 " The write is performed asynchronously and the aio_flush command must be\n"
1447 " used to ensure all outstanding aio requests have been completed.\n"
1448 " Note that due to its asynchronous nature, this command will be\n"
1449 " considered successful once the request is submitted, independently\n"
1450 " of potential I/O errors or pattern mismatches.\n"
1451 " -P, -- use different pattern to fill file\n"
1452 " -C, -- report statistics in a machine parsable format\n"
1453 " -f, -- use Force Unit Access semantics\n"
1454 " -i, -- treat request as invalid, for exercising stats\n"
1455 " -q, -- quiet mode, do not show I/O statistics\n"
1456 " -u, -- with -z, allow unmapping\n"
1457 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1461 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1463 static const cmdinfo_t aio_write_cmd = {
1464 .name = "aio_write",
1465 .cfunc = aio_write_f,
1466 .perm = BLK_PERM_WRITE,
1469 .args = "[-Cfiquz] [-P pattern] off len [len..]",
1470 .oneline = "asynchronously writes a number of bytes",
1471 .help = aio_write_help,
1474 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1478 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1482 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1488 flags |= BDRV_REQ_FUA;
1494 flags |= BDRV_REQ_MAY_UNMAP;
1497 pattern = parse_pattern(optarg);
1504 printf("injecting invalid write request\n");
1505 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1513 qemuio_command_usage(&aio_write_cmd);
1518 if (optind > argc - 2) {
1520 qemuio_command_usage(&aio_write_cmd);
1524 if (ctx->zflag && optind != argc - 2) {
1525 printf("-z supports only a single length parameter\n");
1530 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1531 printf("-u requires -z to be specified\n");
1536 if (ctx->zflag && ctx->Pflag) {
1537 printf("-z and -P cannot be specified at the same time\n");
1542 ctx->offset = cvtnum(argv[optind]);
1543 if (ctx->offset < 0) {
1544 int ret = ctx->offset;
1545 print_cvtnum_err(ret, argv[optind]);
1552 int64_t count = cvtnum(argv[optind]);
1554 print_cvtnum_err(count, argv[optind]);
1559 ctx->qiov.size = count;
1560 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1563 nr_iov = argc - optind;
1564 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1566 if (ctx->buf == NULL) {
1567 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1572 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
1573 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1576 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1583 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1585 BlockAcctCookie cookie;
1586 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
1588 block_acct_done(blk_get_stats(blk), &cookie);
1592 static const cmdinfo_t aio_flush_cmd = {
1593 .name = "aio_flush",
1594 .cfunc = aio_flush_f,
1595 .oneline = "completes all outstanding aio requests"
1598 static int flush_f(BlockBackend *blk, int argc, char **argv)
1600 return blk_flush(blk);
1603 static const cmdinfo_t flush_cmd = {
1607 .oneline = "flush all in-core file state to disk",
1610 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1612 Error *local_err = NULL;
1616 offset = cvtnum(argv[1]);
1618 print_cvtnum_err(offset, argv[1]);
1622 ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
1624 error_report_err(local_err);
1631 static const cmdinfo_t truncate_cmd = {
1634 .cfunc = truncate_f,
1635 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1639 .oneline = "truncates the current file at the given offset",
1642 static int length_f(BlockBackend *blk, int argc, char **argv)
1647 size = blk_getlength(blk);
1649 printf("getlength: %s\n", strerror(-size));
1653 cvtstr(size, s1, sizeof(s1));
1659 static const cmdinfo_t length_cmd = {
1663 .oneline = "gets the length of the current file",
1667 static int info_f(BlockBackend *blk, int argc, char **argv)
1669 BlockDriverState *bs = blk_bs(blk);
1670 BlockDriverInfo bdi;
1671 ImageInfoSpecific *spec_info;
1672 Error *local_err = NULL;
1673 char s1[64], s2[64];
1676 if (bs->drv && bs->drv->format_name) {
1677 printf("format name: %s\n", bs->drv->format_name);
1679 if (bs->drv && bs->drv->protocol_name) {
1680 printf("format name: %s\n", bs->drv->protocol_name);
1683 ret = bdrv_get_info(bs, &bdi);
1688 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1689 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1691 printf("cluster size: %s\n", s1);
1692 printf("vm state offset: %s\n", s2);
1694 spec_info = bdrv_get_specific_info(bs, &local_err);
1696 error_report_err(local_err);
1700 printf("Format specific information:\n");
1701 bdrv_image_info_specific_dump(spec_info);
1702 qapi_free_ImageInfoSpecific(spec_info);
1710 static const cmdinfo_t info_cmd = {
1714 .oneline = "prints information about the current file",
1717 static void discard_help(void)
1721 " discards a range of bytes from the given offset\n"
1724 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1726 " Discards a segment of the currently open file.\n"
1727 " -C, -- report statistics in a machine parsable format\n"
1728 " -q, -- quiet mode, do not show I/O statistics\n"
1732 static int discard_f(BlockBackend *blk, int argc, char **argv);
1734 static const cmdinfo_t discard_cmd = {
1738 .perm = BLK_PERM_WRITE,
1741 .args = "[-Cq] off len",
1742 .oneline = "discards a number of bytes at a specified offset",
1743 .help = discard_help,
1746 static int discard_f(BlockBackend *blk, int argc, char **argv)
1748 struct timespec t1, t2;
1749 bool Cflag = false, qflag = false;
1751 int64_t offset, bytes;
1753 while ((c = getopt(argc, argv, "Cq")) != -1) {
1762 qemuio_command_usage(&discard_cmd);
1767 if (optind != argc - 2) {
1768 qemuio_command_usage(&discard_cmd);
1772 offset = cvtnum(argv[optind]);
1774 print_cvtnum_err(offset, argv[optind]);
1779 bytes = cvtnum(argv[optind]);
1781 print_cvtnum_err(bytes, argv[optind]);
1783 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
1784 printf("length cannot exceed %"PRIu64", given %s\n",
1785 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1789 clock_gettime(CLOCK_MONOTONIC, &t1);
1790 ret = blk_pdiscard(blk, offset, bytes);
1791 clock_gettime(CLOCK_MONOTONIC, &t2);
1794 printf("discard failed: %s\n", strerror(-ret));
1798 /* Finally, report back -- -C gives a parsable format */
1801 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
1807 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1809 BlockDriverState *bs = blk_bs(blk);
1810 int64_t offset, start, remaining, count;
1813 int64_t num, sum_alloc;
1815 start = offset = cvtnum(argv[1]);
1817 print_cvtnum_err(offset, argv[1]);
1822 count = cvtnum(argv[2]);
1824 print_cvtnum_err(count, argv[2]);
1828 count = BDRV_SECTOR_SIZE;
1834 ret = bdrv_is_allocated(bs, offset, remaining, &num);
1836 printf("is_allocated failed: %s\n", strerror(-ret));
1850 cvtstr(start, s1, sizeof(s1));
1852 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
1853 sum_alloc, count, s1);
1857 static const cmdinfo_t alloc_cmd = {
1863 .args = "offset [count]",
1864 .oneline = "checks if offset is allocated in the file",
1868 static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1869 int64_t bytes, int64_t *pnum)
1875 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1876 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1884 while (bytes > 0 && ret == firstret) {
1888 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1889 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1890 if (ret == firstret && num) {
1900 static int map_f(BlockBackend *blk, int argc, char **argv)
1902 int64_t offset, bytes;
1903 char s1[64], s2[64];
1909 bytes = blk_getlength(blk);
1911 error_report("Failed to query image length: %s", strerror(-bytes));
1916 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
1918 error_report("Failed to get allocation status: %s", strerror(-ret));
1921 error_report("Unexpected end of image");
1925 retstr = ret ? " allocated" : "not allocated";
1926 cvtstr(num, s1, sizeof(s1));
1927 cvtstr(offset, s2, sizeof(s2));
1928 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
1929 s1, num, retstr, s2, offset);
1938 static const cmdinfo_t map_cmd = {
1944 .oneline = "prints the allocated areas of a file",
1947 static void reopen_help(void)
1951 " Changes the open options of an already opened image\n"
1954 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1956 " -r, -- Reopen the image read-only\n"
1957 " -w, -- Reopen the image read-write\n"
1958 " -c, -- Change the cache mode to the given value\n"
1959 " -o, -- Changes block driver options (cf. 'open' command)\n"
1963 static int reopen_f(BlockBackend *blk, int argc, char **argv);
1965 static QemuOptsList reopen_opts = {
1967 .merge_lists = true,
1968 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
1970 /* no elements => accept any params */
1971 { /* end of list */ }
1975 static const cmdinfo_t reopen_cmd = {
1980 .args = "[(-r|-w)] [-c cache] [-o options]",
1981 .oneline = "reopens an image with new options",
1982 .help = reopen_help,
1985 static int reopen_f(BlockBackend *blk, int argc, char **argv)
1987 BlockDriverState *bs = blk_bs(blk);
1991 int flags = bs->open_flags;
1992 bool writethrough = !blk_enable_write_cache(blk);
1993 bool has_rw_option = false;
1994 bool has_cache_option = false;
1996 BlockReopenQueue *brq;
1997 Error *local_err = NULL;
1999 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
2002 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
2003 error_report("Invalid cache option: %s", optarg);
2006 has_cache_option = true;
2009 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2010 qemu_opts_reset(&reopen_opts);
2015 if (has_rw_option) {
2016 error_report("Only one -r/-w option may be given");
2019 flags &= ~BDRV_O_RDWR;
2020 has_rw_option = true;
2023 if (has_rw_option) {
2024 error_report("Only one -r/-w option may be given");
2027 flags |= BDRV_O_RDWR;
2028 has_rw_option = true;
2031 qemu_opts_reset(&reopen_opts);
2032 qemuio_command_usage(&reopen_cmd);
2037 if (optind != argc) {
2038 qemu_opts_reset(&reopen_opts);
2039 qemuio_command_usage(&reopen_cmd);
2043 if (!writethrough != blk_enable_write_cache(blk) &&
2044 blk_get_attached_dev(blk))
2046 error_report("Cannot change cache.writeback: Device attached");
2047 qemu_opts_reset(&reopen_opts);
2051 if (!(flags & BDRV_O_RDWR)) {
2052 uint64_t orig_perm, orig_shared_perm;
2056 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2058 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2063 qopts = qemu_opts_find(&reopen_opts, NULL);
2064 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
2065 qemu_opts_reset(&reopen_opts);
2067 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2068 if (has_rw_option) {
2069 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2070 qobject_unref(opts);
2074 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2077 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2078 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2079 if (has_cache_option) {
2080 error_report("Cannot set both -c and the cache options");
2081 qobject_unref(opts);
2085 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2086 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2089 bdrv_subtree_drained_begin(bs);
2090 brq = bdrv_reopen_queue(NULL, bs, opts, true);
2091 bdrv_reopen_multiple(brq, &local_err);
2092 bdrv_subtree_drained_end(bs);
2095 error_report_err(local_err);
2099 blk_set_enable_write_cache(blk, !writethrough);
2103 static int break_f(BlockBackend *blk, int argc, char **argv)
2107 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2109 printf("Could not set breakpoint: %s\n", strerror(-ret));
2116 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
2120 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2122 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2129 static const cmdinfo_t break_cmd = {
2134 .args = "event tag",
2135 .oneline = "sets a breakpoint on event and tags the stopped "
2139 static const cmdinfo_t remove_break_cmd = {
2140 .name = "remove_break",
2143 .cfunc = remove_break_f,
2145 .oneline = "remove a breakpoint by tag",
2148 static int resume_f(BlockBackend *blk, int argc, char **argv)
2152 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2154 printf("Could not resume request: %s\n", strerror(-ret));
2161 static const cmdinfo_t resume_cmd = {
2167 .oneline = "resumes the request tagged as tag",
2170 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2172 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2173 aio_poll(blk_get_aio_context(blk), true);
2178 static const cmdinfo_t wait_break_cmd = {
2179 .name = "wait_break",
2182 .cfunc = wait_break_f,
2184 .oneline = "waits for the suspension of a request",
2187 static int abort_f(BlockBackend *blk, int argc, char **argv)
2192 static const cmdinfo_t abort_cmd = {
2195 .flags = CMD_NOFILE_OK,
2196 .oneline = "simulate a program crash using abort(3)",
2199 static void sigraise_help(void)
2203 " raises the given signal\n"
2206 " 'sigraise %i' - raises SIGTERM\n"
2208 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2209 " given to sigraise.\n"
2213 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2215 static const cmdinfo_t sigraise_cmd = {
2217 .cfunc = sigraise_f,
2220 .flags = CMD_NOFILE_OK,
2222 .oneline = "raises a signal",
2223 .help = sigraise_help,
2226 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2228 int64_t sig = cvtnum(argv[1]);
2230 print_cvtnum_err(sig, argv[1]);
2232 } else if (sig > NSIG) {
2233 printf("signal argument '%s' is too large to be a valid signal\n",
2238 /* Using raise() to kill this process does not necessarily flush all open
2239 * streams. At least stdout and stderr (although the latter should be
2240 * non-buffered anyway) should be flushed, though. */
2249 static void sleep_cb(void *opaque)
2251 bool *expired = opaque;
2255 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2259 struct QEMUTimer *timer;
2260 bool expired = false;
2262 ms = strtol(argv[1], &endptr, 0);
2263 if (ms < 0 || *endptr != '\0') {
2264 printf("%s is not a valid number\n", argv[1]);
2268 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2269 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2272 main_loop_wait(false);
2279 static const cmdinfo_t sleep_cmd = {
2284 .flags = CMD_NOFILE_OK,
2285 .oneline = "waits for the given value in milliseconds",
2288 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2293 printf("%s ", ct->name);
2295 printf("(or %s) ", ct->altname);
2300 printf("%s ", ct->args);
2302 printf("-- %s\n", ct->oneline);
2305 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2307 help_oneline(cmd, ct);
2313 static void help_all(void)
2315 const cmdinfo_t *ct;
2317 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2318 help_oneline(ct->name, ct);
2320 printf("\nUse 'help commandname' for extended help.\n");
2323 static int help_f(BlockBackend *blk, int argc, char **argv)
2325 const cmdinfo_t *ct;
2332 ct = find_command(argv[1]);
2334 printf("command %s not found\n", argv[1]);
2338 help_onecmd(argv[1], ct);
2342 static const cmdinfo_t help_cmd = {
2348 .flags = CMD_FLAG_GLOBAL,
2349 .args = "[command]",
2350 .oneline = "help for one or all commands",
2353 int qemuio_command(BlockBackend *blk, const char *cmd)
2357 const cmdinfo_t *ct;
2362 input = g_strdup(cmd);
2363 v = breakline(input, &c);
2365 ct = find_command(v[0]);
2367 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2368 aio_context_acquire(ctx);
2369 ret = command(blk, ct, c, v);
2370 aio_context_release(ctx);
2372 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2382 static void __attribute((constructor)) init_qemuio_commands(void)
2384 /* initialize commands */
2385 qemuio_add_command(&help_cmd);
2386 qemuio_add_command(&read_cmd);
2387 qemuio_add_command(&readv_cmd);
2388 qemuio_add_command(&write_cmd);
2389 qemuio_add_command(&writev_cmd);
2390 qemuio_add_command(&aio_read_cmd);
2391 qemuio_add_command(&aio_write_cmd);
2392 qemuio_add_command(&aio_flush_cmd);
2393 qemuio_add_command(&flush_cmd);
2394 qemuio_add_command(&truncate_cmd);
2395 qemuio_add_command(&length_cmd);
2396 qemuio_add_command(&info_cmd);
2397 qemuio_add_command(&discard_cmd);
2398 qemuio_add_command(&alloc_cmd);
2399 qemuio_add_command(&map_cmd);
2400 qemuio_add_command(&reopen_cmd);
2401 qemuio_add_command(&break_cmd);
2402 qemuio_add_command(&remove_break_cmd);
2403 qemuio_add_command(&resume_cmd);
2404 qemuio_add_command(&wait_break_cmd);
2405 qemuio_add_command(&abort_cmd);
2406 qemuio_add_command(&sleep_cmd);
2407 qemuio_add_command(&sigraise_cmd);