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/timer.h"
21 #include "qemu/cutils.h"
23 #define CMD_NOFILE_OK 0x01
27 static cmdinfo_t
*cmdtab
;
30 static int compare_cmdname(const void *a
, const void *b
)
32 return strcmp(((const cmdinfo_t
*)a
)->name
,
33 ((const cmdinfo_t
*)b
)->name
);
36 void qemuio_add_command(const cmdinfo_t
*ci
)
38 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
39 * flags allow it not to be, so that combination is invalid.
40 * Catch it now rather than letting it manifest as a crash if a
41 * particular set of command line options are used.
43 assert(ci
->perm
== 0 ||
44 (ci
->flags
& (CMD_FLAG_GLOBAL
| CMD_NOFILE_OK
)) == 0);
45 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
46 cmdtab
[ncmds
- 1] = *ci
;
47 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
50 int qemuio_command_usage(const cmdinfo_t
*ci
)
52 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
);
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
);
704 return qemuio_command_usage(&read_cmd
);
708 if (optind
!= argc
- 2) {
709 return qemuio_command_usage(&read_cmd
);
712 offset
= cvtnum(argv
[optind
]);
714 print_cvtnum_err(offset
, argv
[optind
]);
719 count
= cvtnum(argv
[optind
]);
721 print_cvtnum_err(count
, argv
[optind
]);
723 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
724 printf("length cannot exceed %" PRIu64
", given %s\n",
725 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
729 if (!Pflag
&& (lflag
|| sflag
)) {
730 return qemuio_command_usage(&read_cmd
);
734 pattern_count
= count
- pattern_offset
;
737 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
738 printf("pattern verification range exceeds end of read data\n");
743 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
744 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
748 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
749 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
755 buf
= qemu_io_alloc(blk
, count
, 0xab);
757 gettimeofday(&t1
, NULL
);
759 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
761 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
763 gettimeofday(&t2
, NULL
);
766 printf("read failed: %s\n", strerror(-cnt
));
771 void *cmp_buf
= g_malloc(pattern_count
);
772 memset(cmp_buf
, pattern
, pattern_count
);
773 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
774 printf("Pattern verification failed at offset %"
775 PRId64
", %"PRId64
" bytes\n",
776 offset
+ pattern_offset
, pattern_count
);
786 dump_buffer(buf
, offset
, count
);
789 /* Finally, report back -- -C gives a parsable format */
791 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
799 static void readv_help(void)
803 " reads a range of bytes from the given offset into multiple buffers\n"
806 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
808 " Reads a segment of the currently open file, optionally dumping it to the\n"
809 " standard output stream (with -v option) for subsequent inspection.\n"
810 " Uses multiple iovec buffers if more than one byte range is specified.\n"
811 " -C, -- report statistics in a machine parsable format\n"
812 " -P, -- use a pattern to verify read data\n"
813 " -v, -- dump buffer to standard output\n"
814 " -q, -- quiet mode, do not show I/O statistics\n"
818 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
820 static const cmdinfo_t readv_cmd
= {
825 .args
= "[-Cqv] [-P pattern] off len [len..]",
826 .oneline
= "reads a number of bytes at a specified offset",
830 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
832 struct timeval t1
, t2
;
833 bool Cflag
= false, qflag
= false, vflag
= false;
837 /* Some compilers get confused and warn if this is not initialized. */
844 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
851 pattern
= parse_pattern(optarg
);
863 return qemuio_command_usage(&readv_cmd
);
867 if (optind
> argc
- 2) {
868 return qemuio_command_usage(&readv_cmd
);
872 offset
= cvtnum(argv
[optind
]);
874 print_cvtnum_err(offset
, argv
[optind
]);
879 nr_iov
= argc
- optind
;
880 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
885 gettimeofday(&t1
, NULL
);
886 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
887 gettimeofday(&t2
, NULL
);
890 printf("readv failed: %s\n", strerror(-cnt
));
895 void *cmp_buf
= g_malloc(qiov
.size
);
896 memset(cmp_buf
, pattern
, qiov
.size
);
897 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
898 printf("Pattern verification failed at offset %"
899 PRId64
", %zd bytes\n", offset
, qiov
.size
);
909 dump_buffer(buf
, offset
, qiov
.size
);
912 /* Finally, report back -- -C gives a parsable format */
914 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
917 qemu_iovec_destroy(&qiov
);
922 static void write_help(void)
926 " writes a range of bytes from the given offset\n"
929 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
931 " Writes into a segment of the currently open file, using a buffer\n"
932 " filled with a set pattern (0xcdcdcdcd).\n"
933 " -b, -- write to the VM state rather than the virtual disk\n"
934 " -c, -- write compressed data with blk_write_compressed\n"
935 " -f, -- use Force Unit Access semantics\n"
936 " -p, -- ignored for backwards compatibility\n"
937 " -P, -- use different pattern to fill file\n"
938 " -C, -- report statistics in a machine parsable format\n"
939 " -q, -- quiet mode, do not show I/O statistics\n"
940 " -u, -- with -z, allow unmapping\n"
941 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
945 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
947 static const cmdinfo_t write_cmd
= {
951 .perm
= BLK_PERM_WRITE
,
954 .args
= "[-bcCfquz] [-P pattern] off len",
955 .oneline
= "writes a number of bytes at a specified offset",
959 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
961 struct timeval t1
, t2
;
962 bool Cflag
= false, qflag
= false, bflag
= false;
963 bool Pflag
= false, zflag
= false, cflag
= false;
969 /* Some compilers get confused and warn if this is not initialized. */
973 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
985 flags
|= BDRV_REQ_FUA
;
988 /* Ignored for backwards compatibility */
992 pattern
= parse_pattern(optarg
);
1001 flags
|= BDRV_REQ_MAY_UNMAP
;
1007 return qemuio_command_usage(&write_cmd
);
1011 if (optind
!= argc
- 2) {
1012 return qemuio_command_usage(&write_cmd
);
1015 if (bflag
&& zflag
) {
1016 printf("-b and -z cannot be specified at the same time\n");
1020 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1021 printf("-f and -b or -c cannot be specified at the same time\n");
1025 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1026 printf("-u requires -z to be specified\n");
1030 if (zflag
&& Pflag
) {
1031 printf("-z and -P cannot be specified at the same time\n");
1035 offset
= cvtnum(argv
[optind
]);
1037 print_cvtnum_err(offset
, argv
[optind
]);
1042 count
= cvtnum(argv
[optind
]);
1044 print_cvtnum_err(count
, argv
[optind
]);
1046 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1047 printf("length cannot exceed %" PRIu64
", given %s\n",
1048 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1052 if (bflag
|| cflag
) {
1053 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1054 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1059 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1060 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1067 buf
= qemu_io_alloc(blk
, count
, pattern
);
1070 gettimeofday(&t1
, NULL
);
1072 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1074 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1076 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1078 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1080 gettimeofday(&t2
, NULL
);
1083 printf("write failed: %s\n", strerror(-cnt
));
1091 /* Finally, report back -- -C gives a parsable format */
1093 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1108 " writes a range of bytes from the given offset source from multiple buffers\n"
1111 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1113 " Writes into a segment of the currently open file, using a buffer\n"
1114 " filled with a set pattern (0xcdcdcdcd).\n"
1115 " -P, -- use different pattern to fill file\n"
1116 " -C, -- report statistics in a machine parsable format\n"
1117 " -f, -- use Force Unit Access semantics\n"
1118 " -q, -- quiet mode, do not show I/O statistics\n"
1122 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1124 static const cmdinfo_t writev_cmd
= {
1127 .perm
= BLK_PERM_WRITE
,
1130 .args
= "[-Cfq] [-P pattern] off len [len..]",
1131 .oneline
= "writes a number of bytes at a specified offset",
1132 .help
= writev_help
,
1135 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1137 struct timeval t1
, t2
;
1138 bool Cflag
= false, qflag
= false;
1143 /* Some compilers get confused and warn if this is not initialized. */
1149 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1155 flags
|= BDRV_REQ_FUA
;
1161 pattern
= parse_pattern(optarg
);
1167 return qemuio_command_usage(&writev_cmd
);
1171 if (optind
> argc
- 2) {
1172 return qemuio_command_usage(&writev_cmd
);
1175 offset
= cvtnum(argv
[optind
]);
1177 print_cvtnum_err(offset
, argv
[optind
]);
1182 nr_iov
= argc
- optind
;
1183 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1188 gettimeofday(&t1
, NULL
);
1189 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1190 gettimeofday(&t2
, NULL
);
1193 printf("writev failed: %s\n", strerror(-cnt
));
1201 /* Finally, report back -- -C gives a parsable format */
1203 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1205 qemu_iovec_destroy(&qiov
);
1220 BlockAcctCookie acct
;
1225 static void aio_write_done(void *opaque
, int ret
)
1227 struct aio_ctx
*ctx
= opaque
;
1230 gettimeofday(&t2
, NULL
);
1234 printf("aio_write failed: %s\n", strerror(-ret
));
1235 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1239 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1245 /* Finally, report back -- -C gives a parsable format */
1246 t2
= tsub(t2
, ctx
->t1
);
1247 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1248 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1251 qemu_io_free(ctx
->buf
);
1252 qemu_iovec_destroy(&ctx
->qiov
);
1257 static void aio_read_done(void *opaque
, int ret
)
1259 struct aio_ctx
*ctx
= opaque
;
1262 gettimeofday(&t2
, NULL
);
1265 printf("readv failed: %s\n", strerror(-ret
));
1266 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1271 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1273 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1274 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1275 printf("Pattern verification failed at offset %"
1276 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1281 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1288 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1291 /* Finally, report back -- -C gives a parsable format */
1292 t2
= tsub(t2
, ctx
->t1
);
1293 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1294 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1296 qemu_io_free(ctx
->buf
);
1297 qemu_iovec_destroy(&ctx
->qiov
);
1301 static void aio_read_help(void)
1305 " asynchronously reads a range of bytes from the given offset\n"
1308 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1310 " Reads a segment of the currently open file, optionally dumping it to the\n"
1311 " standard output stream (with -v option) for subsequent inspection.\n"
1312 " The read is performed asynchronously and the aio_flush command must be\n"
1313 " used to ensure all outstanding aio requests have been completed.\n"
1314 " -C, -- report statistics in a machine parsable format\n"
1315 " -P, -- use a pattern to verify read data\n"
1316 " -i, -- treat request as invalid, for exercising stats\n"
1317 " -v, -- dump buffer to standard output\n"
1318 " -q, -- quiet mode, do not show I/O statistics\n"
1322 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1324 static const cmdinfo_t aio_read_cmd
= {
1326 .cfunc
= aio_read_f
,
1329 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1330 .oneline
= "asynchronously reads a number of bytes",
1331 .help
= aio_read_help
,
1334 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1337 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1340 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1347 ctx
->pattern
= parse_pattern(optarg
);
1348 if (ctx
->pattern
< 0) {
1354 printf("injecting invalid read request\n");
1355 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1366 return qemuio_command_usage(&aio_read_cmd
);
1370 if (optind
> argc
- 2) {
1372 return qemuio_command_usage(&aio_read_cmd
);
1375 ctx
->offset
= cvtnum(argv
[optind
]);
1376 if (ctx
->offset
< 0) {
1377 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1383 nr_iov
= argc
- optind
;
1384 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1385 if (ctx
->buf
== NULL
) {
1386 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1391 gettimeofday(&ctx
->t1
, NULL
);
1392 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1394 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1398 static void aio_write_help(void)
1402 " asynchronously writes a range of bytes from the given offset source\n"
1403 " from multiple buffers\n"
1406 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1408 " Writes into a segment of the currently open file, using a buffer\n"
1409 " filled with a set pattern (0xcdcdcdcd).\n"
1410 " The write is performed asynchronously and the aio_flush command must be\n"
1411 " used to ensure all outstanding aio requests have been completed.\n"
1412 " -P, -- use different pattern to fill file\n"
1413 " -C, -- report statistics in a machine parsable format\n"
1414 " -f, -- use Force Unit Access semantics\n"
1415 " -i, -- treat request as invalid, for exercising stats\n"
1416 " -q, -- quiet mode, do not show I/O statistics\n"
1417 " -u, -- with -z, allow unmapping\n"
1418 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1422 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1424 static const cmdinfo_t aio_write_cmd
= {
1425 .name
= "aio_write",
1426 .cfunc
= aio_write_f
,
1427 .perm
= BLK_PERM_WRITE
,
1430 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1431 .oneline
= "asynchronously writes a number of bytes",
1432 .help
= aio_write_help
,
1435 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1439 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1443 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1449 flags
|= BDRV_REQ_FUA
;
1455 flags
|= BDRV_REQ_MAY_UNMAP
;
1458 pattern
= parse_pattern(optarg
);
1465 printf("injecting invalid write request\n");
1466 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1474 return qemuio_command_usage(&aio_write_cmd
);
1478 if (optind
> argc
- 2) {
1480 return qemuio_command_usage(&aio_write_cmd
);
1483 if (ctx
->zflag
&& optind
!= argc
- 2) {
1484 printf("-z supports only a single length parameter\n");
1489 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1490 printf("-u requires -z to be specified\n");
1495 if (ctx
->zflag
&& ctx
->Pflag
) {
1496 printf("-z and -P cannot be specified at the same time\n");
1501 ctx
->offset
= cvtnum(argv
[optind
]);
1502 if (ctx
->offset
< 0) {
1503 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1510 int64_t count
= cvtnum(argv
[optind
]);
1512 print_cvtnum_err(count
, argv
[optind
]);
1517 ctx
->qiov
.size
= count
;
1518 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1521 nr_iov
= argc
- optind
;
1522 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1524 if (ctx
->buf
== NULL
) {
1525 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1530 gettimeofday(&ctx
->t1
, NULL
);
1531 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1534 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1540 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1542 BlockAcctCookie cookie
;
1543 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1545 block_acct_done(blk_get_stats(blk
), &cookie
);
1549 static const cmdinfo_t aio_flush_cmd
= {
1550 .name
= "aio_flush",
1551 .cfunc
= aio_flush_f
,
1552 .oneline
= "completes all outstanding aio requests"
1555 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1561 static const cmdinfo_t flush_cmd
= {
1565 .oneline
= "flush all in-core file state to disk",
1568 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1570 Error
*local_err
= NULL
;
1574 offset
= cvtnum(argv
[1]);
1576 print_cvtnum_err(offset
, argv
[1]);
1580 ret
= blk_truncate(blk
, offset
, PREALLOC_MODE_OFF
, &local_err
);
1582 error_report_err(local_err
);
1589 static const cmdinfo_t truncate_cmd
= {
1592 .cfunc
= truncate_f
,
1593 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1597 .oneline
= "truncates the current file at the given offset",
1600 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1605 size
= blk_getlength(blk
);
1607 printf("getlength: %s\n", strerror(-size
));
1611 cvtstr(size
, s1
, sizeof(s1
));
1617 static const cmdinfo_t length_cmd
= {
1621 .oneline
= "gets the length of the current file",
1625 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1627 BlockDriverState
*bs
= blk_bs(blk
);
1628 BlockDriverInfo bdi
;
1629 ImageInfoSpecific
*spec_info
;
1630 char s1
[64], s2
[64];
1633 if (bs
->drv
&& bs
->drv
->format_name
) {
1634 printf("format name: %s\n", bs
->drv
->format_name
);
1636 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1637 printf("format name: %s\n", bs
->drv
->protocol_name
);
1640 ret
= bdrv_get_info(bs
, &bdi
);
1645 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1646 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1648 printf("cluster size: %s\n", s1
);
1649 printf("vm state offset: %s\n", s2
);
1651 spec_info
= bdrv_get_specific_info(bs
);
1653 printf("Format specific information:\n");
1654 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1655 qapi_free_ImageInfoSpecific(spec_info
);
1663 static const cmdinfo_t info_cmd
= {
1667 .oneline
= "prints information about the current file",
1670 static void discard_help(void)
1674 " discards a range of bytes from the given offset\n"
1677 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1679 " Discards a segment of the currently open file.\n"
1680 " -C, -- report statistics in a machine parsable format\n"
1681 " -q, -- quiet mode, do not show I/O statistics\n"
1685 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1687 static const cmdinfo_t discard_cmd
= {
1691 .perm
= BLK_PERM_WRITE
,
1694 .args
= "[-Cq] off len",
1695 .oneline
= "discards a number of bytes at a specified offset",
1696 .help
= discard_help
,
1699 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1701 struct timeval t1
, t2
;
1702 bool Cflag
= false, qflag
= false;
1704 int64_t offset
, bytes
;
1706 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1715 return qemuio_command_usage(&discard_cmd
);
1719 if (optind
!= argc
- 2) {
1720 return qemuio_command_usage(&discard_cmd
);
1723 offset
= cvtnum(argv
[optind
]);
1725 print_cvtnum_err(offset
, argv
[optind
]);
1730 bytes
= cvtnum(argv
[optind
]);
1732 print_cvtnum_err(bytes
, argv
[optind
]);
1734 } else if (bytes
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1735 printf("length cannot exceed %"PRIu64
", given %s\n",
1736 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1741 gettimeofday(&t1
, NULL
);
1742 ret
= blk_pdiscard(blk
, offset
, bytes
);
1743 gettimeofday(&t2
, NULL
);
1746 printf("discard failed: %s\n", strerror(-ret
));
1750 /* Finally, report back -- -C gives a parsable format */
1753 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1760 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1762 BlockDriverState
*bs
= blk_bs(blk
);
1763 int64_t offset
, start
, remaining
, count
;
1766 int64_t num
, sum_alloc
;
1768 start
= offset
= cvtnum(argv
[1]);
1770 print_cvtnum_err(offset
, argv
[1]);
1775 count
= cvtnum(argv
[2]);
1777 print_cvtnum_err(count
, argv
[2]);
1781 count
= BDRV_SECTOR_SIZE
;
1787 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1789 printf("is_allocated failed: %s\n", strerror(-ret
));
1803 cvtstr(start
, s1
, sizeof(s1
));
1805 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1806 sum_alloc
, count
, s1
);
1810 static const cmdinfo_t alloc_cmd
= {
1816 .args
= "offset [count]",
1817 .oneline
= "checks if offset is allocated in the file",
1821 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1822 int64_t bytes
, int64_t *pnum
)
1828 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1829 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1837 while (bytes
> 0 && ret
== firstret
) {
1841 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1842 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1843 if (ret
== firstret
&& num
) {
1853 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1855 int64_t offset
, bytes
;
1856 char s1
[64], s2
[64];
1862 bytes
= blk_getlength(blk
);
1864 error_report("Failed to query image length: %s", strerror(-bytes
));
1869 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
1871 error_report("Failed to get allocation status: %s", strerror(-ret
));
1874 error_report("Unexpected end of image");
1878 retstr
= ret
? " allocated" : "not allocated";
1879 cvtstr(num
, s1
, sizeof(s1
));
1880 cvtstr(offset
, s2
, sizeof(s2
));
1881 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
1882 s1
, num
, retstr
, s2
, offset
);
1891 static const cmdinfo_t map_cmd
= {
1897 .oneline
= "prints the allocated areas of a file",
1900 static void reopen_help(void)
1904 " Changes the open options of an already opened image\n"
1907 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1909 " -r, -- Reopen the image read-only\n"
1910 " -w, -- Reopen the image read-write\n"
1911 " -c, -- Change the cache mode to the given value\n"
1912 " -o, -- Changes block driver options (cf. 'open' command)\n"
1916 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1918 static QemuOptsList reopen_opts
= {
1920 .merge_lists
= true,
1921 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1923 /* no elements => accept any params */
1924 { /* end of list */ }
1928 static const cmdinfo_t reopen_cmd
= {
1933 .args
= "[(-r|-w)] [-c cache] [-o options]",
1934 .oneline
= "reopens an image with new options",
1935 .help
= reopen_help
,
1938 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1940 BlockDriverState
*bs
= blk_bs(blk
);
1944 int flags
= bs
->open_flags
;
1945 bool writethrough
= !blk_enable_write_cache(blk
);
1946 bool has_rw_option
= false;
1948 BlockReopenQueue
*brq
;
1949 Error
*local_err
= NULL
;
1951 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
1954 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1955 error_report("Invalid cache option: %s", optarg
);
1960 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1961 qemu_opts_reset(&reopen_opts
);
1966 if (has_rw_option
) {
1967 error_report("Only one -r/-w option may be given");
1970 flags
&= ~BDRV_O_RDWR
;
1971 has_rw_option
= true;
1974 if (has_rw_option
) {
1975 error_report("Only one -r/-w option may be given");
1978 flags
|= BDRV_O_RDWR
;
1979 has_rw_option
= true;
1982 qemu_opts_reset(&reopen_opts
);
1983 return qemuio_command_usage(&reopen_cmd
);
1987 if (optind
!= argc
) {
1988 qemu_opts_reset(&reopen_opts
);
1989 return qemuio_command_usage(&reopen_cmd
);
1992 if (writethrough
!= blk_enable_write_cache(blk
) &&
1993 blk_get_attached_dev(blk
))
1995 error_report("Cannot change cache.writeback: Device attached");
1996 qemu_opts_reset(&reopen_opts
);
2000 if (!(flags
& BDRV_O_RDWR
)) {
2001 uint64_t orig_perm
, orig_shared_perm
;
2005 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2007 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2012 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2013 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2014 qemu_opts_reset(&reopen_opts
);
2016 bdrv_subtree_drained_begin(bs
);
2017 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2018 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
2019 bdrv_subtree_drained_end(bs
);
2022 error_report_err(local_err
);
2024 blk_set_enable_write_cache(blk
, !writethrough
);
2030 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2034 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2036 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2042 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2046 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2048 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2054 static const cmdinfo_t break_cmd
= {
2059 .args
= "event tag",
2060 .oneline
= "sets a breakpoint on event and tags the stopped "
2064 static const cmdinfo_t remove_break_cmd
= {
2065 .name
= "remove_break",
2068 .cfunc
= remove_break_f
,
2070 .oneline
= "remove a breakpoint by tag",
2073 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2077 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2079 printf("Could not resume request: %s\n", strerror(-ret
));
2085 static const cmdinfo_t resume_cmd
= {
2091 .oneline
= "resumes the request tagged as tag",
2094 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2096 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2097 aio_poll(blk_get_aio_context(blk
), true);
2103 static const cmdinfo_t wait_break_cmd
= {
2104 .name
= "wait_break",
2107 .cfunc
= wait_break_f
,
2109 .oneline
= "waits for the suspension of a request",
2112 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2117 static const cmdinfo_t abort_cmd
= {
2120 .flags
= CMD_NOFILE_OK
,
2121 .oneline
= "simulate a program crash using abort(3)",
2124 static void sigraise_help(void)
2128 " raises the given signal\n"
2131 " 'sigraise %i' - raises SIGTERM\n"
2133 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2134 " given to sigraise.\n"
2138 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2140 static const cmdinfo_t sigraise_cmd
= {
2142 .cfunc
= sigraise_f
,
2145 .flags
= CMD_NOFILE_OK
,
2147 .oneline
= "raises a signal",
2148 .help
= sigraise_help
,
2151 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2153 int64_t sig
= cvtnum(argv
[1]);
2155 print_cvtnum_err(sig
, argv
[1]);
2157 } else if (sig
> NSIG
) {
2158 printf("signal argument '%s' is too large to be a valid signal\n",
2163 /* Using raise() to kill this process does not necessarily flush all open
2164 * streams. At least stdout and stderr (although the latter should be
2165 * non-buffered anyway) should be flushed, though. */
2173 static void sleep_cb(void *opaque
)
2175 bool *expired
= opaque
;
2179 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2183 struct QEMUTimer
*timer
;
2184 bool expired
= false;
2186 ms
= strtol(argv
[1], &endptr
, 0);
2187 if (ms
< 0 || *endptr
!= '\0') {
2188 printf("%s is not a valid number\n", argv
[1]);
2192 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2193 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2196 main_loop_wait(false);
2204 static const cmdinfo_t sleep_cmd
= {
2209 .flags
= CMD_NOFILE_OK
,
2210 .oneline
= "waits for the given value in milliseconds",
2213 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2218 printf("%s ", ct
->name
);
2220 printf("(or %s) ", ct
->altname
);
2225 printf("%s ", ct
->args
);
2227 printf("-- %s\n", ct
->oneline
);
2230 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2232 help_oneline(cmd
, ct
);
2238 static void help_all(void)
2240 const cmdinfo_t
*ct
;
2242 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2243 help_oneline(ct
->name
, ct
);
2245 printf("\nUse 'help commandname' for extended help.\n");
2248 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2250 const cmdinfo_t
*ct
;
2257 ct
= find_command(argv
[1]);
2259 printf("command %s not found\n", argv
[1]);
2263 help_onecmd(argv
[1], ct
);
2267 static const cmdinfo_t help_cmd
= {
2273 .flags
= CMD_FLAG_GLOBAL
,
2274 .args
= "[command]",
2275 .oneline
= "help for one or all commands",
2278 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2282 const cmdinfo_t
*ct
;
2287 input
= g_strdup(cmd
);
2288 v
= breakline(input
, &c
);
2290 ct
= find_command(v
[0]);
2292 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2293 aio_context_acquire(ctx
);
2294 done
= command(blk
, ct
, c
, v
);
2295 aio_context_release(ctx
);
2297 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2306 static void __attribute((constructor
)) init_qemuio_commands(void)
2308 /* initialize commands */
2309 qemuio_add_command(&help_cmd
);
2310 qemuio_add_command(&read_cmd
);
2311 qemuio_add_command(&readv_cmd
);
2312 qemuio_add_command(&write_cmd
);
2313 qemuio_add_command(&writev_cmd
);
2314 qemuio_add_command(&aio_read_cmd
);
2315 qemuio_add_command(&aio_write_cmd
);
2316 qemuio_add_command(&aio_flush_cmd
);
2317 qemuio_add_command(&flush_cmd
);
2318 qemuio_add_command(&truncate_cmd
);
2319 qemuio_add_command(&length_cmd
);
2320 qemuio_add_command(&info_cmd
);
2321 qemuio_add_command(&discard_cmd
);
2322 qemuio_add_command(&alloc_cmd
);
2323 qemuio_add_command(&map_cmd
);
2324 qemuio_add_command(&reopen_cmd
);
2325 qemuio_add_command(&break_cmd
);
2326 qemuio_add_command(&remove_break_cmd
);
2327 qemuio_add_command(&resume_cmd
);
2328 qemuio_add_command(&wait_break_cmd
);
2329 qemuio_add_command(&abort_cmd
);
2330 qemuio_add_command(&sleep_cmd
);
2331 qemuio_add_command(&sigraise_cmd
);