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"
14 #include "sysemu/block-backend.h"
15 #include "block/block.h"
16 #include "block/block_int.h" /* for info_f() */
17 #include "block/qapi.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/option.h"
21 #include "qemu/timer.h"
22 #include "qemu/cutils.h"
24 #define CMD_NOFILE_OK 0x01
28 static cmdinfo_t
*cmdtab
;
31 static int compare_cmdname(const void *a
, const void *b
)
33 return strcmp(((const cmdinfo_t
*)a
)->name
,
34 ((const cmdinfo_t
*)b
)->name
);
37 void qemuio_add_command(const cmdinfo_t
*ci
)
39 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
40 * flags allow it not to be, so that combination is invalid.
41 * Catch it now rather than letting it manifest as a crash if a
42 * particular set of command line options are used.
44 assert(ci
->perm
== 0 ||
45 (ci
->flags
& (CMD_FLAG_GLOBAL
| CMD_NOFILE_OK
)) == 0);
46 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
47 cmdtab
[ncmds
- 1] = *ci
;
48 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
51 void qemuio_command_usage(const cmdinfo_t
*ci
)
53 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
56 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
58 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
61 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
62 fprintf(stderr
, "no file open, try 'help open'\n");
68 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
73 if (!init_check_command(blk
, ct
)) {
77 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
78 if (ct
->argmax
== -1) {
80 "bad argument count %d to %s, expected at least %d arguments\n",
81 argc
-1, cmd
, ct
->argmin
);
82 } else if (ct
->argmin
== ct
->argmax
) {
84 "bad argument count %d to %s, expected %d arguments\n",
85 argc
-1, cmd
, ct
->argmin
);
88 "bad argument count %d to %s, expected between %d and %d arguments\n",
89 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
94 /* Request additional permissions if necessary for this command. The caller
95 * is responsible for restoring the original permissions afterwards if this
96 * is what it wants. */
97 if (ct
->perm
&& blk_is_available(blk
)) {
98 uint64_t orig_perm
, orig_shared_perm
;
99 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
101 if (ct
->perm
& ~orig_perm
) {
103 Error
*local_err
= NULL
;
106 new_perm
= orig_perm
| ct
->perm
;
108 ret
= blk_set_perm(blk
, new_perm
, orig_shared_perm
, &local_err
);
110 error_report_err(local_err
);
117 return ct
->cfunc(blk
, argc
, argv
);
120 static const cmdinfo_t
*find_command(const char *cmd
)
124 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
125 if (strcmp(ct
->name
, cmd
) == 0 ||
126 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
128 return (const cmdinfo_t
*)ct
;
134 /* Invoke fn() for commands with a matching prefix */
135 void qemuio_complete_command(const char *input
,
136 void (*fn
)(const char *cmd
, void *opaque
),
140 size_t input_len
= strlen(input
);
142 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
143 if (strncmp(input
, ct
->name
, input_len
) == 0) {
144 fn(ct
->name
, opaque
);
149 static char **breakline(char *input
, int *count
)
153 char **rval
= g_new0(char *, 1);
155 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
160 rval
= g_renew(char *, rval
, (c
+ 1));
168 static int64_t cvtnum(const char *s
)
173 err
= qemu_strtosz(s
, NULL
, &value
);
177 if (value
> INT64_MAX
) {
183 static void print_cvtnum_err(int64_t rc
, const char *arg
)
187 printf("Parsing error: non-numeric argument,"
188 " or extraneous/unrecognized suffix -- %s\n", arg
);
191 printf("Parsing error: argument too large -- %s\n", arg
);
194 printf("Parsing error: %s\n", arg
);
198 #define EXABYTES(x) ((long long)(x) << 60)
199 #define PETABYTES(x) ((long long)(x) << 50)
200 #define TERABYTES(x) ((long long)(x) << 40)
201 #define GIGABYTES(x) ((long long)(x) << 30)
202 #define MEGABYTES(x) ((long long)(x) << 20)
203 #define KILOBYTES(x) ((long long)(x) << 10)
205 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
206 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
207 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
208 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
209 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
210 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
212 static void cvtstr(double value
, char *str
, size_t size
)
217 if (value
>= EXABYTES(1)) {
219 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
220 } else if (value
>= PETABYTES(1)) {
222 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
223 } else if (value
>= TERABYTES(1)) {
225 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
226 } else if (value
>= GIGABYTES(1)) {
228 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
229 } else if (value
>= MEGABYTES(1)) {
231 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
232 } else if (value
>= KILOBYTES(1)) {
234 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
237 snprintf(str
, size
- 6, "%f", value
);
240 trim
= strstr(str
, ".000");
242 strcpy(trim
, suffix
);
250 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
252 t1
.tv_usec
-= t2
.tv_usec
;
253 if (t1
.tv_usec
< 0) {
254 t1
.tv_usec
+= 1000000;
257 t1
.tv_sec
-= t2
.tv_sec
;
261 static double tdiv(double value
, struct timeval tv
)
263 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
266 #define HOURS(sec) ((sec) / (60 * 60))
267 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
268 #define SECONDS(sec) ((sec) % 60)
272 TERSE_FIXED_TIME
= 0x1,
273 VERBOSE_FIXED_TIME
= 0x2,
276 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
278 double usec
= (double)tv
->tv_usec
/ 1000000.0;
280 if (format
& TERSE_FIXED_TIME
) {
281 if (!HOURS(tv
->tv_sec
)) {
282 snprintf(ts
, size
, "%u:%02u.%02u",
283 (unsigned int) MINUTES(tv
->tv_sec
),
284 (unsigned int) SECONDS(tv
->tv_sec
),
285 (unsigned int) (usec
* 100));
288 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
291 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
292 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
293 (unsigned int) HOURS(tv
->tv_sec
),
294 (unsigned int) MINUTES(tv
->tv_sec
),
295 (unsigned int) SECONDS(tv
->tv_sec
),
296 (unsigned int) (usec
* 100));
298 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
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 timeval
*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
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
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 timeval 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 gettimeofday(&t1
, NULL
);
762 ret
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
764 ret
= do_pread(blk
, buf
, offset
, count
, &total
);
766 gettimeofday(&t2
, NULL
);
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 timeval 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 gettimeofday(&t1
, NULL
);
894 ret
= do_aio_readv(blk
, &qiov
, offset
, &total
);
895 gettimeofday(&t2
, NULL
);
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
", %zd 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 " -p, -- ignored for backwards compatibility\n"
949 " -P, -- use different pattern to fill file\n"
950 " -C, -- report statistics in a machine parsable format\n"
951 " -q, -- quiet mode, do not show I/O statistics\n"
952 " -u, -- with -z, allow unmapping\n"
953 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
957 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
959 static const cmdinfo_t write_cmd
= {
963 .perm
= BLK_PERM_WRITE
,
966 .args
= "[-bcCfquz] [-P pattern] off len",
967 .oneline
= "writes a number of bytes at a specified offset",
971 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
973 struct timeval t1
, t2
;
974 bool Cflag
= false, qflag
= false, bflag
= false;
975 bool Pflag
= false, zflag
= false, cflag
= false;
981 /* Some compilers get confused and warn if this is not initialized. */
985 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
997 flags
|= BDRV_REQ_FUA
;
1000 /* Ignored for backwards compatibility */
1004 pattern
= parse_pattern(optarg
);
1013 flags
|= BDRV_REQ_MAY_UNMAP
;
1019 qemuio_command_usage(&write_cmd
);
1024 if (optind
!= argc
- 2) {
1025 qemuio_command_usage(&write_cmd
);
1029 if (bflag
&& zflag
) {
1030 printf("-b and -z cannot be specified at the same time\n");
1034 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1035 printf("-f and -b or -c cannot be specified at the same time\n");
1039 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1040 printf("-u requires -z to be specified\n");
1044 if (zflag
&& Pflag
) {
1045 printf("-z and -P cannot be specified at the same time\n");
1049 offset
= cvtnum(argv
[optind
]);
1051 print_cvtnum_err(offset
, argv
[optind
]);
1056 count
= cvtnum(argv
[optind
]);
1058 print_cvtnum_err(count
, argv
[optind
]);
1060 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1061 printf("length cannot exceed %" PRIu64
", given %s\n",
1062 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1066 if (bflag
|| cflag
) {
1067 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1068 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1073 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1074 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1081 buf
= qemu_io_alloc(blk
, count
, pattern
);
1084 gettimeofday(&t1
, NULL
);
1086 ret
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1088 ret
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1090 ret
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1092 ret
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1094 gettimeofday(&t2
, NULL
);
1097 printf("write failed: %s\n", strerror(-ret
));
1108 /* Finally, report back -- -C gives a parsable format */
1110 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1124 " writes a range of bytes from the given offset source from multiple buffers\n"
1127 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1129 " Writes into a segment of the currently open file, using a buffer\n"
1130 " filled with a set pattern (0xcdcdcdcd).\n"
1131 " -P, -- use different pattern to fill file\n"
1132 " -C, -- report statistics in a machine parsable format\n"
1133 " -f, -- use Force Unit Access semantics\n"
1134 " -q, -- quiet mode, do not show I/O statistics\n"
1138 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1140 static const cmdinfo_t writev_cmd
= {
1143 .perm
= BLK_PERM_WRITE
,
1146 .args
= "[-Cfq] [-P pattern] off len [len..]",
1147 .oneline
= "writes a number of bytes at a specified offset",
1148 .help
= writev_help
,
1151 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1153 struct timeval t1
, t2
;
1154 bool Cflag
= false, qflag
= false;
1159 /* Some compilers get confused and warn if this is not initialized. */
1165 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1171 flags
|= BDRV_REQ_FUA
;
1177 pattern
= parse_pattern(optarg
);
1183 qemuio_command_usage(&writev_cmd
);
1188 if (optind
> argc
- 2) {
1189 qemuio_command_usage(&writev_cmd
);
1193 offset
= cvtnum(argv
[optind
]);
1195 print_cvtnum_err(offset
, argv
[optind
]);
1200 nr_iov
= argc
- optind
;
1201 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1206 gettimeofday(&t1
, NULL
);
1207 ret
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1208 gettimeofday(&t2
, NULL
);
1211 printf("writev failed: %s\n", strerror(-ret
));
1222 /* Finally, report back -- -C gives a parsable format */
1224 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1226 qemu_iovec_destroy(&qiov
);
1241 BlockAcctCookie acct
;
1246 static void aio_write_done(void *opaque
, int ret
)
1248 struct aio_ctx
*ctx
= opaque
;
1251 gettimeofday(&t2
, NULL
);
1255 printf("aio_write failed: %s\n", strerror(-ret
));
1256 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1260 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1266 /* Finally, report back -- -C gives a parsable format */
1267 t2
= tsub(t2
, ctx
->t1
);
1268 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1269 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1272 qemu_io_free(ctx
->buf
);
1273 qemu_iovec_destroy(&ctx
->qiov
);
1278 static void aio_read_done(void *opaque
, int ret
)
1280 struct aio_ctx
*ctx
= opaque
;
1283 gettimeofday(&t2
, NULL
);
1286 printf("readv failed: %s\n", strerror(-ret
));
1287 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1292 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1294 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1295 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1296 printf("Pattern verification failed at offset %"
1297 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1302 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1309 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1312 /* Finally, report back -- -C gives a parsable format */
1313 t2
= tsub(t2
, ctx
->t1
);
1314 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1315 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1317 qemu_io_free(ctx
->buf
);
1318 qemu_iovec_destroy(&ctx
->qiov
);
1322 static void aio_read_help(void)
1326 " asynchronously reads a range of bytes from the given offset\n"
1329 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1331 " Reads a segment of the currently open file, optionally dumping it to the\n"
1332 " standard output stream (with -v option) for subsequent inspection.\n"
1333 " The read is performed asynchronously and the aio_flush command must be\n"
1334 " used to ensure all outstanding aio requests have been completed.\n"
1335 " Note that due to its asynchronous nature, this command will be\n"
1336 " considered successful once the request is submitted, independently\n"
1337 " of potential I/O errors or pattern mismatches.\n"
1338 " -C, -- report statistics in a machine parsable format\n"
1339 " -P, -- use a pattern to verify read data\n"
1340 " -i, -- treat request as invalid, for exercising stats\n"
1341 " -v, -- dump buffer to standard output\n"
1342 " -q, -- quiet mode, do not show I/O statistics\n"
1346 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1348 static const cmdinfo_t aio_read_cmd
= {
1350 .cfunc
= aio_read_f
,
1353 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1354 .oneline
= "asynchronously reads a number of bytes",
1355 .help
= aio_read_help
,
1358 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1361 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1364 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1371 ctx
->pattern
= parse_pattern(optarg
);
1372 if (ctx
->pattern
< 0) {
1378 printf("injecting invalid read request\n");
1379 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1390 qemuio_command_usage(&aio_read_cmd
);
1395 if (optind
> argc
- 2) {
1397 qemuio_command_usage(&aio_read_cmd
);
1401 ctx
->offset
= cvtnum(argv
[optind
]);
1402 if (ctx
->offset
< 0) {
1403 int ret
= ctx
->offset
;
1404 print_cvtnum_err(ret
, argv
[optind
]);
1410 nr_iov
= argc
- optind
;
1411 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1412 if (ctx
->buf
== NULL
) {
1413 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1418 gettimeofday(&ctx
->t1
, NULL
);
1419 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1421 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1425 static void aio_write_help(void)
1429 " asynchronously writes a range of bytes from the given offset source\n"
1430 " from multiple buffers\n"
1433 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1435 " Writes into a segment of the currently open file, using a buffer\n"
1436 " filled with a set pattern (0xcdcdcdcd).\n"
1437 " The write is performed asynchronously and the aio_flush command must be\n"
1438 " used to ensure all outstanding aio requests have been completed.\n"
1439 " Note that due to its asynchronous nature, this command will be\n"
1440 " considered successful once the request is submitted, independently\n"
1441 " of potential I/O errors or pattern mismatches.\n"
1442 " -P, -- use different pattern to fill file\n"
1443 " -C, -- report statistics in a machine parsable format\n"
1444 " -f, -- use Force Unit Access semantics\n"
1445 " -i, -- treat request as invalid, for exercising stats\n"
1446 " -q, -- quiet mode, do not show I/O statistics\n"
1447 " -u, -- with -z, allow unmapping\n"
1448 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1452 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1454 static const cmdinfo_t aio_write_cmd
= {
1455 .name
= "aio_write",
1456 .cfunc
= aio_write_f
,
1457 .perm
= BLK_PERM_WRITE
,
1460 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1461 .oneline
= "asynchronously writes a number of bytes",
1462 .help
= aio_write_help
,
1465 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1469 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1473 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1479 flags
|= BDRV_REQ_FUA
;
1485 flags
|= BDRV_REQ_MAY_UNMAP
;
1488 pattern
= parse_pattern(optarg
);
1495 printf("injecting invalid write request\n");
1496 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1504 qemuio_command_usage(&aio_write_cmd
);
1509 if (optind
> argc
- 2) {
1511 qemuio_command_usage(&aio_write_cmd
);
1515 if (ctx
->zflag
&& optind
!= argc
- 2) {
1516 printf("-z supports only a single length parameter\n");
1521 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1522 printf("-u requires -z to be specified\n");
1527 if (ctx
->zflag
&& ctx
->Pflag
) {
1528 printf("-z and -P cannot be specified at the same time\n");
1533 ctx
->offset
= cvtnum(argv
[optind
]);
1534 if (ctx
->offset
< 0) {
1535 int ret
= ctx
->offset
;
1536 print_cvtnum_err(ret
, argv
[optind
]);
1543 int64_t count
= cvtnum(argv
[optind
]);
1545 print_cvtnum_err(count
, argv
[optind
]);
1550 ctx
->qiov
.size
= count
;
1551 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1554 nr_iov
= argc
- optind
;
1555 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1557 if (ctx
->buf
== NULL
) {
1558 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1563 gettimeofday(&ctx
->t1
, NULL
);
1564 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1567 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1574 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1576 BlockAcctCookie cookie
;
1577 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1579 block_acct_done(blk_get_stats(blk
), &cookie
);
1583 static const cmdinfo_t aio_flush_cmd
= {
1584 .name
= "aio_flush",
1585 .cfunc
= aio_flush_f
,
1586 .oneline
= "completes all outstanding aio requests"
1589 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1591 return blk_flush(blk
);
1594 static const cmdinfo_t flush_cmd
= {
1598 .oneline
= "flush all in-core file state to disk",
1601 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1603 Error
*local_err
= NULL
;
1607 offset
= cvtnum(argv
[1]);
1609 print_cvtnum_err(offset
, argv
[1]);
1613 ret
= blk_truncate(blk
, offset
, PREALLOC_MODE_OFF
, &local_err
);
1615 error_report_err(local_err
);
1622 static const cmdinfo_t truncate_cmd
= {
1625 .cfunc
= truncate_f
,
1626 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1630 .oneline
= "truncates the current file at the given offset",
1633 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1638 size
= blk_getlength(blk
);
1640 printf("getlength: %s\n", strerror(-size
));
1644 cvtstr(size
, s1
, sizeof(s1
));
1650 static const cmdinfo_t length_cmd
= {
1654 .oneline
= "gets the length of the current file",
1658 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1660 BlockDriverState
*bs
= blk_bs(blk
);
1661 BlockDriverInfo bdi
;
1662 ImageInfoSpecific
*spec_info
;
1663 char s1
[64], s2
[64];
1666 if (bs
->drv
&& bs
->drv
->format_name
) {
1667 printf("format name: %s\n", bs
->drv
->format_name
);
1669 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1670 printf("format name: %s\n", bs
->drv
->protocol_name
);
1673 ret
= bdrv_get_info(bs
, &bdi
);
1678 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1679 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1681 printf("cluster size: %s\n", s1
);
1682 printf("vm state offset: %s\n", s2
);
1684 spec_info
= bdrv_get_specific_info(bs
);
1686 printf("Format specific information:\n");
1687 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1688 qapi_free_ImageInfoSpecific(spec_info
);
1696 static const cmdinfo_t info_cmd
= {
1700 .oneline
= "prints information about the current file",
1703 static void discard_help(void)
1707 " discards a range of bytes from the given offset\n"
1710 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1712 " Discards a segment of the currently open file.\n"
1713 " -C, -- report statistics in a machine parsable format\n"
1714 " -q, -- quiet mode, do not show I/O statistics\n"
1718 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1720 static const cmdinfo_t discard_cmd
= {
1724 .perm
= BLK_PERM_WRITE
,
1727 .args
= "[-Cq] off len",
1728 .oneline
= "discards a number of bytes at a specified offset",
1729 .help
= discard_help
,
1732 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1734 struct timeval t1
, t2
;
1735 bool Cflag
= false, qflag
= false;
1737 int64_t offset
, bytes
;
1739 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1748 qemuio_command_usage(&discard_cmd
);
1753 if (optind
!= argc
- 2) {
1754 qemuio_command_usage(&discard_cmd
);
1758 offset
= cvtnum(argv
[optind
]);
1760 print_cvtnum_err(offset
, argv
[optind
]);
1765 bytes
= cvtnum(argv
[optind
]);
1767 print_cvtnum_err(bytes
, argv
[optind
]);
1769 } else if (bytes
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1770 printf("length cannot exceed %"PRIu64
", given %s\n",
1771 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1776 gettimeofday(&t1
, NULL
);
1777 ret
= blk_pdiscard(blk
, offset
, bytes
);
1778 gettimeofday(&t2
, NULL
);
1781 printf("discard failed: %s\n", strerror(-ret
));
1785 /* Finally, report back -- -C gives a parsable format */
1788 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1794 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1796 BlockDriverState
*bs
= blk_bs(blk
);
1797 int64_t offset
, start
, remaining
, count
;
1800 int64_t num
, sum_alloc
;
1802 start
= offset
= cvtnum(argv
[1]);
1804 print_cvtnum_err(offset
, argv
[1]);
1809 count
= cvtnum(argv
[2]);
1811 print_cvtnum_err(count
, argv
[2]);
1815 count
= BDRV_SECTOR_SIZE
;
1821 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1823 printf("is_allocated failed: %s\n", strerror(-ret
));
1837 cvtstr(start
, s1
, sizeof(s1
));
1839 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1840 sum_alloc
, count
, s1
);
1844 static const cmdinfo_t alloc_cmd
= {
1850 .args
= "offset [count]",
1851 .oneline
= "checks if offset is allocated in the file",
1855 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1856 int64_t bytes
, int64_t *pnum
)
1862 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1863 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1871 while (bytes
> 0 && ret
== firstret
) {
1875 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1876 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1877 if (ret
== firstret
&& num
) {
1887 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1889 int64_t offset
, bytes
;
1890 char s1
[64], s2
[64];
1896 bytes
= blk_getlength(blk
);
1898 error_report("Failed to query image length: %s", strerror(-bytes
));
1903 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
1905 error_report("Failed to get allocation status: %s", strerror(-ret
));
1908 error_report("Unexpected end of image");
1912 retstr
= ret
? " allocated" : "not allocated";
1913 cvtstr(num
, s1
, sizeof(s1
));
1914 cvtstr(offset
, s2
, sizeof(s2
));
1915 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
1916 s1
, num
, retstr
, s2
, offset
);
1925 static const cmdinfo_t map_cmd
= {
1931 .oneline
= "prints the allocated areas of a file",
1934 static void reopen_help(void)
1938 " Changes the open options of an already opened image\n"
1941 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1943 " -r, -- Reopen the image read-only\n"
1944 " -w, -- Reopen the image read-write\n"
1945 " -c, -- Change the cache mode to the given value\n"
1946 " -o, -- Changes block driver options (cf. 'open' command)\n"
1950 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1952 static QemuOptsList reopen_opts
= {
1954 .merge_lists
= true,
1955 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1957 /* no elements => accept any params */
1958 { /* end of list */ }
1962 static const cmdinfo_t reopen_cmd
= {
1967 .args
= "[(-r|-w)] [-c cache] [-o options]",
1968 .oneline
= "reopens an image with new options",
1969 .help
= reopen_help
,
1972 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1974 BlockDriverState
*bs
= blk_bs(blk
);
1978 int flags
= bs
->open_flags
;
1979 bool writethrough
= !blk_enable_write_cache(blk
);
1980 bool has_rw_option
= false;
1982 BlockReopenQueue
*brq
;
1983 Error
*local_err
= NULL
;
1985 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
1988 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1989 error_report("Invalid cache option: %s", optarg
);
1994 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1995 qemu_opts_reset(&reopen_opts
);
2000 if (has_rw_option
) {
2001 error_report("Only one -r/-w option may be given");
2004 flags
&= ~BDRV_O_RDWR
;
2005 has_rw_option
= true;
2008 if (has_rw_option
) {
2009 error_report("Only one -r/-w option may be given");
2012 flags
|= BDRV_O_RDWR
;
2013 has_rw_option
= true;
2016 qemu_opts_reset(&reopen_opts
);
2017 qemuio_command_usage(&reopen_cmd
);
2022 if (optind
!= argc
) {
2023 qemu_opts_reset(&reopen_opts
);
2024 qemuio_command_usage(&reopen_cmd
);
2028 if (!writethrough
!= blk_enable_write_cache(blk
) &&
2029 blk_get_attached_dev(blk
))
2031 error_report("Cannot change cache.writeback: Device attached");
2032 qemu_opts_reset(&reopen_opts
);
2036 if (!(flags
& BDRV_O_RDWR
)) {
2037 uint64_t orig_perm
, orig_shared_perm
;
2041 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2043 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2048 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2049 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2050 qemu_opts_reset(&reopen_opts
);
2052 bdrv_subtree_drained_begin(bs
);
2053 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2054 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
2055 bdrv_subtree_drained_end(bs
);
2058 error_report_err(local_err
);
2062 blk_set_enable_write_cache(blk
, !writethrough
);
2066 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2070 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2072 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2079 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2083 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2085 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2092 static const cmdinfo_t break_cmd
= {
2097 .args
= "event tag",
2098 .oneline
= "sets a breakpoint on event and tags the stopped "
2102 static const cmdinfo_t remove_break_cmd
= {
2103 .name
= "remove_break",
2106 .cfunc
= remove_break_f
,
2108 .oneline
= "remove a breakpoint by tag",
2111 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2115 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2117 printf("Could not resume request: %s\n", strerror(-ret
));
2124 static const cmdinfo_t resume_cmd
= {
2130 .oneline
= "resumes the request tagged as tag",
2133 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2135 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2136 aio_poll(blk_get_aio_context(blk
), true);
2141 static const cmdinfo_t wait_break_cmd
= {
2142 .name
= "wait_break",
2145 .cfunc
= wait_break_f
,
2147 .oneline
= "waits for the suspension of a request",
2150 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2155 static const cmdinfo_t abort_cmd
= {
2158 .flags
= CMD_NOFILE_OK
,
2159 .oneline
= "simulate a program crash using abort(3)",
2162 static void sigraise_help(void)
2166 " raises the given signal\n"
2169 " 'sigraise %i' - raises SIGTERM\n"
2171 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2172 " given to sigraise.\n"
2176 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2178 static const cmdinfo_t sigraise_cmd
= {
2180 .cfunc
= sigraise_f
,
2183 .flags
= CMD_NOFILE_OK
,
2185 .oneline
= "raises a signal",
2186 .help
= sigraise_help
,
2189 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2191 int64_t sig
= cvtnum(argv
[1]);
2193 print_cvtnum_err(sig
, argv
[1]);
2195 } else if (sig
> NSIG
) {
2196 printf("signal argument '%s' is too large to be a valid signal\n",
2201 /* Using raise() to kill this process does not necessarily flush all open
2202 * streams. At least stdout and stderr (although the latter should be
2203 * non-buffered anyway) should be flushed, though. */
2212 static void sleep_cb(void *opaque
)
2214 bool *expired
= opaque
;
2218 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2222 struct QEMUTimer
*timer
;
2223 bool expired
= false;
2225 ms
= strtol(argv
[1], &endptr
, 0);
2226 if (ms
< 0 || *endptr
!= '\0') {
2227 printf("%s is not a valid number\n", argv
[1]);
2231 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2232 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2235 main_loop_wait(false);
2242 static const cmdinfo_t sleep_cmd
= {
2247 .flags
= CMD_NOFILE_OK
,
2248 .oneline
= "waits for the given value in milliseconds",
2251 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2256 printf("%s ", ct
->name
);
2258 printf("(or %s) ", ct
->altname
);
2263 printf("%s ", ct
->args
);
2265 printf("-- %s\n", ct
->oneline
);
2268 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2270 help_oneline(cmd
, ct
);
2276 static void help_all(void)
2278 const cmdinfo_t
*ct
;
2280 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2281 help_oneline(ct
->name
, ct
);
2283 printf("\nUse 'help commandname' for extended help.\n");
2286 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2288 const cmdinfo_t
*ct
;
2295 ct
= find_command(argv
[1]);
2297 printf("command %s not found\n", argv
[1]);
2301 help_onecmd(argv
[1], ct
);
2305 static const cmdinfo_t help_cmd
= {
2311 .flags
= CMD_FLAG_GLOBAL
,
2312 .args
= "[command]",
2313 .oneline
= "help for one or all commands",
2316 int qemuio_command(BlockBackend
*blk
, const char *cmd
)
2320 const cmdinfo_t
*ct
;
2325 input
= g_strdup(cmd
);
2326 v
= breakline(input
, &c
);
2328 ct
= find_command(v
[0]);
2330 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2331 aio_context_acquire(ctx
);
2332 ret
= command(blk
, ct
, c
, v
);
2333 aio_context_release(ctx
);
2335 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2345 static void __attribute((constructor
)) init_qemuio_commands(void)
2347 /* initialize commands */
2348 qemuio_add_command(&help_cmd
);
2349 qemuio_add_command(&read_cmd
);
2350 qemuio_add_command(&readv_cmd
);
2351 qemuio_add_command(&write_cmd
);
2352 qemuio_add_command(&writev_cmd
);
2353 qemuio_add_command(&aio_read_cmd
);
2354 qemuio_add_command(&aio_write_cmd
);
2355 qemuio_add_command(&aio_flush_cmd
);
2356 qemuio_add_command(&flush_cmd
);
2357 qemuio_add_command(&truncate_cmd
);
2358 qemuio_add_command(&length_cmd
);
2359 qemuio_add_command(&info_cmd
);
2360 qemuio_add_command(&discard_cmd
);
2361 qemuio_add_command(&alloc_cmd
);
2362 qemuio_add_command(&map_cmd
);
2363 qemuio_add_command(&reopen_cmd
);
2364 qemuio_add_command(&break_cmd
);
2365 qemuio_add_command(&remove_break_cmd
);
2366 qemuio_add_command(&resume_cmd
);
2367 qemuio_add_command(&wait_break_cmd
);
2368 qemuio_add_command(&abort_cmd
);
2369 qemuio_add_command(&sleep_cmd
);
2370 qemuio_add_command(&sigraise_cmd
);