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 int qemuio_command_usage(const cmdinfo_t
*ci
)
53 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 timeval
tsub(struct timeval t1
, struct timeval t2
)
253 t1
.tv_usec
-= t2
.tv_usec
;
254 if (t1
.tv_usec
< 0) {
255 t1
.tv_usec
+= 1000000;
258 t1
.tv_sec
-= t2
.tv_sec
;
262 static double tdiv(double value
, struct timeval tv
)
264 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
267 #define HOURS(sec) ((sec) / (60 * 60))
268 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
269 #define SECONDS(sec) ((sec) % 60)
273 TERSE_FIXED_TIME
= 0x1,
274 VERBOSE_FIXED_TIME
= 0x2,
277 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
279 double usec
= (double)tv
->tv_usec
/ 1000000.0;
281 if (format
& TERSE_FIXED_TIME
) {
282 if (!HOURS(tv
->tv_sec
)) {
283 snprintf(ts
, size
, "%u:%02u.%02u",
284 (unsigned int) MINUTES(tv
->tv_sec
),
285 (unsigned int) SECONDS(tv
->tv_sec
),
286 (unsigned int) (usec
* 100));
289 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
292 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
293 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
294 (unsigned int) HOURS(tv
->tv_sec
),
295 (unsigned int) MINUTES(tv
->tv_sec
),
296 (unsigned int) SECONDS(tv
->tv_sec
),
297 (unsigned int) (usec
* 100));
299 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
304 * Parse the pattern argument to various sub-commands.
306 * Because the pattern is used as an argument to memset it must evaluate
307 * to an unsigned integer that fits into a single byte.
309 static int parse_pattern(const char *arg
)
314 pattern
= strtol(arg
, &endptr
, 0);
315 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
316 printf("%s is not a valid pattern byte\n", arg
);
324 * Memory allocation helpers.
326 * Make sure memory is aligned by default, or purposefully misaligned if
327 * that is specified on the command line.
330 #define MISALIGN_OFFSET 16
331 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
335 if (qemuio_misalign
) {
336 len
+= MISALIGN_OFFSET
;
338 buf
= blk_blockalign(blk
, len
);
339 memset(buf
, pattern
, len
);
340 if (qemuio_misalign
) {
341 buf
+= MISALIGN_OFFSET
;
346 static void qemu_io_free(void *p
)
348 if (qemuio_misalign
) {
349 p
-= MISALIGN_OFFSET
;
354 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
360 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
361 const uint8_t *s
= p
;
363 printf("%08" PRIx64
": ", offset
+ i
);
364 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
368 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
379 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
380 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
382 char s1
[64], s2
[64], ts
[64];
384 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
386 cvtstr((double)total
, s1
, sizeof(s1
));
387 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
388 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
389 op
, total
, count
, offset
);
390 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
391 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
392 } else {/* bytes,ops,time,bytes/sec,ops/sec */
393 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
395 tdiv((double)total
, *t
),
396 tdiv((double)cnt
, *t
));
401 * Parse multiple length statements for vectored I/O, and construct an I/O
402 * vector matching it.
405 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
408 size_t *sizes
= g_new0(size_t, nr_iov
);
414 for (i
= 0; i
< nr_iov
; i
++) {
420 print_cvtnum_err(len
, arg
);
424 if (len
> BDRV_REQUEST_MAX_BYTES
) {
425 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
426 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
430 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
431 printf("The total number of bytes exceed the maximum size %" PRIu64
432 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
440 qemu_iovec_init(qiov
, nr_iov
);
442 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
444 for (i
= 0; i
< nr_iov
; i
++) {
445 qemu_iovec_add(qiov
, p
, sizes
[i
]);
454 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
455 int64_t bytes
, int64_t *total
)
457 if (bytes
> INT_MAX
) {
461 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, bytes
);
468 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
469 int64_t bytes
, int flags
, int64_t *total
)
471 if (bytes
> INT_MAX
) {
475 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, bytes
, flags
);
492 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
494 CoWriteZeroes
*data
= opaque
;
496 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->bytes
,
500 *data
->total
= data
->ret
;
504 *data
->total
= data
->bytes
;
507 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
508 int64_t bytes
, int flags
, int64_t *total
)
511 CoWriteZeroes data
= {
520 if (bytes
> INT_MAX
) {
524 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
525 bdrv_coroutine_enter(blk_bs(blk
), co
);
527 aio_poll(blk_get_aio_context(blk
), true);
536 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
537 int64_t bytes
, int64_t *total
)
541 if (bytes
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
545 ret
= blk_pwrite_compressed(blk
, offset
, buf
, bytes
);
553 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
554 int64_t count
, int64_t *total
)
556 if (count
> INT_MAX
) {
560 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
567 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
568 int64_t count
, int64_t *total
)
570 if (count
> INT_MAX
) {
574 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
581 #define NOT_DONE 0x7fffffff
582 static void aio_rw_done(void *opaque
, int ret
)
584 *(int *)opaque
= ret
;
587 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
588 int64_t offset
, int *total
)
590 int async_ret
= NOT_DONE
;
592 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
593 while (async_ret
== NOT_DONE
) {
594 main_loop_wait(false);
598 return async_ret
< 0 ? async_ret
: 1;
601 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
602 int64_t offset
, int flags
, int *total
)
604 int async_ret
= NOT_DONE
;
606 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
607 while (async_ret
== NOT_DONE
) {
608 main_loop_wait(false);
612 return async_ret
< 0 ? async_ret
: 1;
615 static void read_help(void)
619 " reads a range of bytes from the given offset\n"
622 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
624 " Reads a segment of the currently open file, optionally dumping it to the\n"
625 " standard output stream (with -v option) for subsequent inspection.\n"
626 " -b, -- read from the VM state rather than the virtual disk\n"
627 " -C, -- report statistics in a machine parsable format\n"
628 " -l, -- length for pattern verification (only with -P)\n"
629 " -p, -- ignored for backwards compatibility\n"
630 " -P, -- use a pattern to verify read data\n"
631 " -q, -- quiet mode, do not show I/O statistics\n"
632 " -s, -- start offset for pattern verification (only with -P)\n"
633 " -v, -- dump buffer to standard output\n"
637 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
639 static const cmdinfo_t read_cmd
= {
645 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
646 .oneline
= "reads a number of bytes at a specified offset",
650 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
652 struct timeval t1
, t2
;
653 bool Cflag
= false, qflag
= false, vflag
= false;
654 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
659 /* Some compilers get confused and warn if this is not initialized. */
662 int64_t pattern_offset
= 0, pattern_count
= 0;
664 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
674 pattern_count
= cvtnum(optarg
);
675 if (pattern_count
< 0) {
676 print_cvtnum_err(pattern_count
, optarg
);
681 /* Ignored for backwards compatibility */
685 pattern
= parse_pattern(optarg
);
695 pattern_offset
= cvtnum(optarg
);
696 if (pattern_offset
< 0) {
697 print_cvtnum_err(pattern_offset
, optarg
);
705 return qemuio_command_usage(&read_cmd
);
709 if (optind
!= argc
- 2) {
710 return qemuio_command_usage(&read_cmd
);
713 offset
= cvtnum(argv
[optind
]);
715 print_cvtnum_err(offset
, argv
[optind
]);
720 count
= cvtnum(argv
[optind
]);
722 print_cvtnum_err(count
, argv
[optind
]);
724 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
725 printf("length cannot exceed %" PRIu64
", given %s\n",
726 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
730 if (!Pflag
&& (lflag
|| sflag
)) {
731 return qemuio_command_usage(&read_cmd
);
735 pattern_count
= count
- pattern_offset
;
738 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
739 printf("pattern verification range exceeds end of read data\n");
744 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
745 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
749 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
750 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
756 buf
= qemu_io_alloc(blk
, count
, 0xab);
758 gettimeofday(&t1
, NULL
);
760 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
762 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
764 gettimeofday(&t2
, NULL
);
767 printf("read failed: %s\n", strerror(-cnt
));
772 void *cmp_buf
= g_malloc(pattern_count
);
773 memset(cmp_buf
, pattern
, pattern_count
);
774 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
775 printf("Pattern verification failed at offset %"
776 PRId64
", %"PRId64
" bytes\n",
777 offset
+ pattern_offset
, pattern_count
);
787 dump_buffer(buf
, offset
, count
);
790 /* Finally, report back -- -C gives a parsable format */
792 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
800 static void readv_help(void)
804 " reads a range of bytes from the given offset into multiple buffers\n"
807 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
809 " Reads a segment of the currently open file, optionally dumping it to the\n"
810 " standard output stream (with -v option) for subsequent inspection.\n"
811 " Uses multiple iovec buffers if more than one byte range is specified.\n"
812 " -C, -- report statistics in a machine parsable format\n"
813 " -P, -- use a pattern to verify read data\n"
814 " -v, -- dump buffer to standard output\n"
815 " -q, -- quiet mode, do not show I/O statistics\n"
819 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
821 static const cmdinfo_t readv_cmd
= {
826 .args
= "[-Cqv] [-P pattern] off len [len..]",
827 .oneline
= "reads a number of bytes at a specified offset",
831 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
833 struct timeval t1
, t2
;
834 bool Cflag
= false, qflag
= false, vflag
= false;
838 /* Some compilers get confused and warn if this is not initialized. */
845 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
852 pattern
= parse_pattern(optarg
);
864 return qemuio_command_usage(&readv_cmd
);
868 if (optind
> argc
- 2) {
869 return qemuio_command_usage(&readv_cmd
);
873 offset
= cvtnum(argv
[optind
]);
875 print_cvtnum_err(offset
, argv
[optind
]);
880 nr_iov
= argc
- optind
;
881 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
886 gettimeofday(&t1
, NULL
);
887 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
888 gettimeofday(&t2
, NULL
);
891 printf("readv failed: %s\n", strerror(-cnt
));
896 void *cmp_buf
= g_malloc(qiov
.size
);
897 memset(cmp_buf
, pattern
, qiov
.size
);
898 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
899 printf("Pattern verification failed at offset %"
900 PRId64
", %zd bytes\n", offset
, qiov
.size
);
910 dump_buffer(buf
, offset
, qiov
.size
);
913 /* Finally, report back -- -C gives a parsable format */
915 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
918 qemu_iovec_destroy(&qiov
);
923 static void write_help(void)
927 " writes a range of bytes from the given offset\n"
930 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
932 " Writes into a segment of the currently open file, using a buffer\n"
933 " filled with a set pattern (0xcdcdcdcd).\n"
934 " -b, -- write to the VM state rather than the virtual disk\n"
935 " -c, -- write compressed data with blk_write_compressed\n"
936 " -f, -- use Force Unit Access semantics\n"
937 " -p, -- ignored for backwards compatibility\n"
938 " -P, -- use different pattern to fill file\n"
939 " -C, -- report statistics in a machine parsable format\n"
940 " -q, -- quiet mode, do not show I/O statistics\n"
941 " -u, -- with -z, allow unmapping\n"
942 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
946 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
948 static const cmdinfo_t write_cmd
= {
952 .perm
= BLK_PERM_WRITE
,
955 .args
= "[-bcCfquz] [-P pattern] off len",
956 .oneline
= "writes a number of bytes at a specified offset",
960 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
962 struct timeval t1
, t2
;
963 bool Cflag
= false, qflag
= false, bflag
= false;
964 bool Pflag
= false, zflag
= false, cflag
= false;
970 /* Some compilers get confused and warn if this is not initialized. */
974 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
986 flags
|= BDRV_REQ_FUA
;
989 /* Ignored for backwards compatibility */
993 pattern
= parse_pattern(optarg
);
1002 flags
|= BDRV_REQ_MAY_UNMAP
;
1008 return qemuio_command_usage(&write_cmd
);
1012 if (optind
!= argc
- 2) {
1013 return qemuio_command_usage(&write_cmd
);
1016 if (bflag
&& zflag
) {
1017 printf("-b and -z cannot be specified at the same time\n");
1021 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1022 printf("-f and -b or -c cannot be specified at the same time\n");
1026 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1027 printf("-u requires -z to be specified\n");
1031 if (zflag
&& Pflag
) {
1032 printf("-z and -P cannot be specified at the same time\n");
1036 offset
= cvtnum(argv
[optind
]);
1038 print_cvtnum_err(offset
, argv
[optind
]);
1043 count
= cvtnum(argv
[optind
]);
1045 print_cvtnum_err(count
, argv
[optind
]);
1047 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1048 printf("length cannot exceed %" PRIu64
", given %s\n",
1049 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1053 if (bflag
|| cflag
) {
1054 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1055 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1060 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1061 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1068 buf
= qemu_io_alloc(blk
, count
, pattern
);
1071 gettimeofday(&t1
, NULL
);
1073 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1075 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1077 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1079 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1081 gettimeofday(&t2
, NULL
);
1084 printf("write failed: %s\n", strerror(-cnt
));
1092 /* Finally, report back -- -C gives a parsable format */
1094 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1109 " writes a range of bytes from the given offset source from multiple buffers\n"
1112 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1114 " Writes into a segment of the currently open file, using a buffer\n"
1115 " filled with a set pattern (0xcdcdcdcd).\n"
1116 " -P, -- use different pattern to fill file\n"
1117 " -C, -- report statistics in a machine parsable format\n"
1118 " -f, -- use Force Unit Access semantics\n"
1119 " -q, -- quiet mode, do not show I/O statistics\n"
1123 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1125 static const cmdinfo_t writev_cmd
= {
1128 .perm
= BLK_PERM_WRITE
,
1131 .args
= "[-Cfq] [-P pattern] off len [len..]",
1132 .oneline
= "writes a number of bytes at a specified offset",
1133 .help
= writev_help
,
1136 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1138 struct timeval t1
, t2
;
1139 bool Cflag
= false, qflag
= false;
1144 /* Some compilers get confused and warn if this is not initialized. */
1150 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1156 flags
|= BDRV_REQ_FUA
;
1162 pattern
= parse_pattern(optarg
);
1168 return qemuio_command_usage(&writev_cmd
);
1172 if (optind
> argc
- 2) {
1173 return qemuio_command_usage(&writev_cmd
);
1176 offset
= cvtnum(argv
[optind
]);
1178 print_cvtnum_err(offset
, argv
[optind
]);
1183 nr_iov
= argc
- optind
;
1184 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1189 gettimeofday(&t1
, NULL
);
1190 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1191 gettimeofday(&t2
, NULL
);
1194 printf("writev failed: %s\n", strerror(-cnt
));
1202 /* Finally, report back -- -C gives a parsable format */
1204 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1206 qemu_iovec_destroy(&qiov
);
1221 BlockAcctCookie acct
;
1226 static void aio_write_done(void *opaque
, int ret
)
1228 struct aio_ctx
*ctx
= opaque
;
1231 gettimeofday(&t2
, NULL
);
1235 printf("aio_write failed: %s\n", strerror(-ret
));
1236 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1240 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1246 /* Finally, report back -- -C gives a parsable format */
1247 t2
= tsub(t2
, ctx
->t1
);
1248 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1249 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1252 qemu_io_free(ctx
->buf
);
1253 qemu_iovec_destroy(&ctx
->qiov
);
1258 static void aio_read_done(void *opaque
, int ret
)
1260 struct aio_ctx
*ctx
= opaque
;
1263 gettimeofday(&t2
, NULL
);
1266 printf("readv failed: %s\n", strerror(-ret
));
1267 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1272 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1274 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1275 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1276 printf("Pattern verification failed at offset %"
1277 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1282 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1289 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1292 /* Finally, report back -- -C gives a parsable format */
1293 t2
= tsub(t2
, ctx
->t1
);
1294 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1295 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1297 qemu_io_free(ctx
->buf
);
1298 qemu_iovec_destroy(&ctx
->qiov
);
1302 static void aio_read_help(void)
1306 " asynchronously reads a range of bytes from the given offset\n"
1309 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1311 " Reads a segment of the currently open file, optionally dumping it to the\n"
1312 " standard output stream (with -v option) for subsequent inspection.\n"
1313 " The read is performed asynchronously and the aio_flush command must be\n"
1314 " used to ensure all outstanding aio requests have been completed.\n"
1315 " -C, -- report statistics in a machine parsable format\n"
1316 " -P, -- use a pattern to verify read data\n"
1317 " -i, -- treat request as invalid, for exercising stats\n"
1318 " -v, -- dump buffer to standard output\n"
1319 " -q, -- quiet mode, do not show I/O statistics\n"
1323 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1325 static const cmdinfo_t aio_read_cmd
= {
1327 .cfunc
= aio_read_f
,
1330 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1331 .oneline
= "asynchronously reads a number of bytes",
1332 .help
= aio_read_help
,
1335 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1338 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1341 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1348 ctx
->pattern
= parse_pattern(optarg
);
1349 if (ctx
->pattern
< 0) {
1355 printf("injecting invalid read request\n");
1356 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1367 return qemuio_command_usage(&aio_read_cmd
);
1371 if (optind
> argc
- 2) {
1373 return qemuio_command_usage(&aio_read_cmd
);
1376 ctx
->offset
= cvtnum(argv
[optind
]);
1377 if (ctx
->offset
< 0) {
1378 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1384 nr_iov
= argc
- optind
;
1385 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1386 if (ctx
->buf
== NULL
) {
1387 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1392 gettimeofday(&ctx
->t1
, NULL
);
1393 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1395 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1399 static void aio_write_help(void)
1403 " asynchronously writes a range of bytes from the given offset source\n"
1404 " from multiple buffers\n"
1407 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1409 " Writes into a segment of the currently open file, using a buffer\n"
1410 " filled with a set pattern (0xcdcdcdcd).\n"
1411 " The write is performed asynchronously and the aio_flush command must be\n"
1412 " used to ensure all outstanding aio requests have been completed.\n"
1413 " -P, -- use different pattern to fill file\n"
1414 " -C, -- report statistics in a machine parsable format\n"
1415 " -f, -- use Force Unit Access semantics\n"
1416 " -i, -- treat request as invalid, for exercising stats\n"
1417 " -q, -- quiet mode, do not show I/O statistics\n"
1418 " -u, -- with -z, allow unmapping\n"
1419 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1423 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1425 static const cmdinfo_t aio_write_cmd
= {
1426 .name
= "aio_write",
1427 .cfunc
= aio_write_f
,
1428 .perm
= BLK_PERM_WRITE
,
1431 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1432 .oneline
= "asynchronously writes a number of bytes",
1433 .help
= aio_write_help
,
1436 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1440 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1444 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1450 flags
|= BDRV_REQ_FUA
;
1456 flags
|= BDRV_REQ_MAY_UNMAP
;
1459 pattern
= parse_pattern(optarg
);
1466 printf("injecting invalid write request\n");
1467 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1475 return qemuio_command_usage(&aio_write_cmd
);
1479 if (optind
> argc
- 2) {
1481 return qemuio_command_usage(&aio_write_cmd
);
1484 if (ctx
->zflag
&& optind
!= argc
- 2) {
1485 printf("-z supports only a single length parameter\n");
1490 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1491 printf("-u requires -z to be specified\n");
1496 if (ctx
->zflag
&& ctx
->Pflag
) {
1497 printf("-z and -P cannot be specified at the same time\n");
1502 ctx
->offset
= cvtnum(argv
[optind
]);
1503 if (ctx
->offset
< 0) {
1504 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1511 int64_t count
= cvtnum(argv
[optind
]);
1513 print_cvtnum_err(count
, argv
[optind
]);
1518 ctx
->qiov
.size
= count
;
1519 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1522 nr_iov
= argc
- optind
;
1523 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1525 if (ctx
->buf
== NULL
) {
1526 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1531 gettimeofday(&ctx
->t1
, NULL
);
1532 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1535 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1541 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1543 BlockAcctCookie cookie
;
1544 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1546 block_acct_done(blk_get_stats(blk
), &cookie
);
1550 static const cmdinfo_t aio_flush_cmd
= {
1551 .name
= "aio_flush",
1552 .cfunc
= aio_flush_f
,
1553 .oneline
= "completes all outstanding aio requests"
1556 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1562 static const cmdinfo_t flush_cmd
= {
1566 .oneline
= "flush all in-core file state to disk",
1569 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1571 Error
*local_err
= NULL
;
1575 offset
= cvtnum(argv
[1]);
1577 print_cvtnum_err(offset
, argv
[1]);
1581 ret
= blk_truncate(blk
, offset
, PREALLOC_MODE_OFF
, &local_err
);
1583 error_report_err(local_err
);
1590 static const cmdinfo_t truncate_cmd
= {
1593 .cfunc
= truncate_f
,
1594 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1598 .oneline
= "truncates the current file at the given offset",
1601 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1606 size
= blk_getlength(blk
);
1608 printf("getlength: %s\n", strerror(-size
));
1612 cvtstr(size
, s1
, sizeof(s1
));
1618 static const cmdinfo_t length_cmd
= {
1622 .oneline
= "gets the length of the current file",
1626 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1628 BlockDriverState
*bs
= blk_bs(blk
);
1629 BlockDriverInfo bdi
;
1630 ImageInfoSpecific
*spec_info
;
1631 char s1
[64], s2
[64];
1634 if (bs
->drv
&& bs
->drv
->format_name
) {
1635 printf("format name: %s\n", bs
->drv
->format_name
);
1637 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1638 printf("format name: %s\n", bs
->drv
->protocol_name
);
1641 ret
= bdrv_get_info(bs
, &bdi
);
1646 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1647 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1649 printf("cluster size: %s\n", s1
);
1650 printf("vm state offset: %s\n", s2
);
1652 spec_info
= bdrv_get_specific_info(bs
);
1654 printf("Format specific information:\n");
1655 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1656 qapi_free_ImageInfoSpecific(spec_info
);
1664 static const cmdinfo_t info_cmd
= {
1668 .oneline
= "prints information about the current file",
1671 static void discard_help(void)
1675 " discards a range of bytes from the given offset\n"
1678 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1680 " Discards a segment of the currently open file.\n"
1681 " -C, -- report statistics in a machine parsable format\n"
1682 " -q, -- quiet mode, do not show I/O statistics\n"
1686 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1688 static const cmdinfo_t discard_cmd
= {
1692 .perm
= BLK_PERM_WRITE
,
1695 .args
= "[-Cq] off len",
1696 .oneline
= "discards a number of bytes at a specified offset",
1697 .help
= discard_help
,
1700 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1702 struct timeval t1
, t2
;
1703 bool Cflag
= false, qflag
= false;
1705 int64_t offset
, bytes
;
1707 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1716 return qemuio_command_usage(&discard_cmd
);
1720 if (optind
!= argc
- 2) {
1721 return qemuio_command_usage(&discard_cmd
);
1724 offset
= cvtnum(argv
[optind
]);
1726 print_cvtnum_err(offset
, argv
[optind
]);
1731 bytes
= cvtnum(argv
[optind
]);
1733 print_cvtnum_err(bytes
, argv
[optind
]);
1735 } else if (bytes
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1736 printf("length cannot exceed %"PRIu64
", given %s\n",
1737 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1742 gettimeofday(&t1
, NULL
);
1743 ret
= blk_pdiscard(blk
, offset
, bytes
);
1744 gettimeofday(&t2
, NULL
);
1747 printf("discard failed: %s\n", strerror(-ret
));
1751 /* Finally, report back -- -C gives a parsable format */
1754 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1761 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1763 BlockDriverState
*bs
= blk_bs(blk
);
1764 int64_t offset
, start
, remaining
, count
;
1767 int64_t num
, sum_alloc
;
1769 start
= offset
= cvtnum(argv
[1]);
1771 print_cvtnum_err(offset
, argv
[1]);
1776 count
= cvtnum(argv
[2]);
1778 print_cvtnum_err(count
, argv
[2]);
1782 count
= BDRV_SECTOR_SIZE
;
1788 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1790 printf("is_allocated failed: %s\n", strerror(-ret
));
1804 cvtstr(start
, s1
, sizeof(s1
));
1806 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1807 sum_alloc
, count
, s1
);
1811 static const cmdinfo_t alloc_cmd
= {
1817 .args
= "offset [count]",
1818 .oneline
= "checks if offset is allocated in the file",
1822 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1823 int64_t bytes
, int64_t *pnum
)
1829 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1830 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1838 while (bytes
> 0 && ret
== firstret
) {
1842 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
1843 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1844 if (ret
== firstret
&& num
) {
1854 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1856 int64_t offset
, bytes
;
1857 char s1
[64], s2
[64];
1863 bytes
= blk_getlength(blk
);
1865 error_report("Failed to query image length: %s", strerror(-bytes
));
1870 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
1872 error_report("Failed to get allocation status: %s", strerror(-ret
));
1875 error_report("Unexpected end of image");
1879 retstr
= ret
? " allocated" : "not allocated";
1880 cvtstr(num
, s1
, sizeof(s1
));
1881 cvtstr(offset
, s2
, sizeof(s2
));
1882 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
1883 s1
, num
, retstr
, s2
, offset
);
1892 static const cmdinfo_t map_cmd
= {
1898 .oneline
= "prints the allocated areas of a file",
1901 static void reopen_help(void)
1905 " Changes the open options of an already opened image\n"
1908 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1910 " -r, -- Reopen the image read-only\n"
1911 " -w, -- Reopen the image read-write\n"
1912 " -c, -- Change the cache mode to the given value\n"
1913 " -o, -- Changes block driver options (cf. 'open' command)\n"
1917 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1919 static QemuOptsList reopen_opts
= {
1921 .merge_lists
= true,
1922 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1924 /* no elements => accept any params */
1925 { /* end of list */ }
1929 static const cmdinfo_t reopen_cmd
= {
1934 .args
= "[(-r|-w)] [-c cache] [-o options]",
1935 .oneline
= "reopens an image with new options",
1936 .help
= reopen_help
,
1939 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1941 BlockDriverState
*bs
= blk_bs(blk
);
1945 int flags
= bs
->open_flags
;
1946 bool writethrough
= !blk_enable_write_cache(blk
);
1947 bool has_rw_option
= false;
1949 BlockReopenQueue
*brq
;
1950 Error
*local_err
= NULL
;
1952 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
1955 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1956 error_report("Invalid cache option: %s", optarg
);
1961 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1962 qemu_opts_reset(&reopen_opts
);
1967 if (has_rw_option
) {
1968 error_report("Only one -r/-w option may be given");
1971 flags
&= ~BDRV_O_RDWR
;
1972 has_rw_option
= true;
1975 if (has_rw_option
) {
1976 error_report("Only one -r/-w option may be given");
1979 flags
|= BDRV_O_RDWR
;
1980 has_rw_option
= true;
1983 qemu_opts_reset(&reopen_opts
);
1984 return qemuio_command_usage(&reopen_cmd
);
1988 if (optind
!= argc
) {
1989 qemu_opts_reset(&reopen_opts
);
1990 return qemuio_command_usage(&reopen_cmd
);
1993 if (writethrough
!= blk_enable_write_cache(blk
) &&
1994 blk_get_attached_dev(blk
))
1996 error_report("Cannot change cache.writeback: Device attached");
1997 qemu_opts_reset(&reopen_opts
);
2001 if (!(flags
& BDRV_O_RDWR
)) {
2002 uint64_t orig_perm
, orig_shared_perm
;
2006 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2008 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2013 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2014 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2015 qemu_opts_reset(&reopen_opts
);
2017 bdrv_subtree_drained_begin(bs
);
2018 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2019 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
2020 bdrv_subtree_drained_end(bs
);
2023 error_report_err(local_err
);
2025 blk_set_enable_write_cache(blk
, !writethrough
);
2031 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2035 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2037 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2043 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2047 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2049 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2055 static const cmdinfo_t break_cmd
= {
2060 .args
= "event tag",
2061 .oneline
= "sets a breakpoint on event and tags the stopped "
2065 static const cmdinfo_t remove_break_cmd
= {
2066 .name
= "remove_break",
2069 .cfunc
= remove_break_f
,
2071 .oneline
= "remove a breakpoint by tag",
2074 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2078 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2080 printf("Could not resume request: %s\n", strerror(-ret
));
2086 static const cmdinfo_t resume_cmd
= {
2092 .oneline
= "resumes the request tagged as tag",
2095 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2097 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2098 aio_poll(blk_get_aio_context(blk
), true);
2104 static const cmdinfo_t wait_break_cmd
= {
2105 .name
= "wait_break",
2108 .cfunc
= wait_break_f
,
2110 .oneline
= "waits for the suspension of a request",
2113 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2118 static const cmdinfo_t abort_cmd
= {
2121 .flags
= CMD_NOFILE_OK
,
2122 .oneline
= "simulate a program crash using abort(3)",
2125 static void sigraise_help(void)
2129 " raises the given signal\n"
2132 " 'sigraise %i' - raises SIGTERM\n"
2134 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2135 " given to sigraise.\n"
2139 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2141 static const cmdinfo_t sigraise_cmd
= {
2143 .cfunc
= sigraise_f
,
2146 .flags
= CMD_NOFILE_OK
,
2148 .oneline
= "raises a signal",
2149 .help
= sigraise_help
,
2152 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2154 int64_t sig
= cvtnum(argv
[1]);
2156 print_cvtnum_err(sig
, argv
[1]);
2158 } else if (sig
> NSIG
) {
2159 printf("signal argument '%s' is too large to be a valid signal\n",
2164 /* Using raise() to kill this process does not necessarily flush all open
2165 * streams. At least stdout and stderr (although the latter should be
2166 * non-buffered anyway) should be flushed, though. */
2174 static void sleep_cb(void *opaque
)
2176 bool *expired
= opaque
;
2180 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2184 struct QEMUTimer
*timer
;
2185 bool expired
= false;
2187 ms
= strtol(argv
[1], &endptr
, 0);
2188 if (ms
< 0 || *endptr
!= '\0') {
2189 printf("%s is not a valid number\n", argv
[1]);
2193 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2194 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2197 main_loop_wait(false);
2205 static const cmdinfo_t sleep_cmd
= {
2210 .flags
= CMD_NOFILE_OK
,
2211 .oneline
= "waits for the given value in milliseconds",
2214 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2219 printf("%s ", ct
->name
);
2221 printf("(or %s) ", ct
->altname
);
2226 printf("%s ", ct
->args
);
2228 printf("-- %s\n", ct
->oneline
);
2231 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2233 help_oneline(cmd
, ct
);
2239 static void help_all(void)
2241 const cmdinfo_t
*ct
;
2243 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2244 help_oneline(ct
->name
, ct
);
2246 printf("\nUse 'help commandname' for extended help.\n");
2249 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2251 const cmdinfo_t
*ct
;
2258 ct
= find_command(argv
[1]);
2260 printf("command %s not found\n", argv
[1]);
2264 help_onecmd(argv
[1], ct
);
2268 static const cmdinfo_t help_cmd
= {
2274 .flags
= CMD_FLAG_GLOBAL
,
2275 .args
= "[command]",
2276 .oneline
= "help for one or all commands",
2279 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2283 const cmdinfo_t
*ct
;
2288 input
= g_strdup(cmd
);
2289 v
= breakline(input
, &c
);
2291 ct
= find_command(v
[0]);
2293 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2294 aio_context_acquire(ctx
);
2295 done
= command(blk
, ct
, c
, v
);
2296 aio_context_release(ctx
);
2298 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2307 static void __attribute((constructor
)) init_qemuio_commands(void)
2309 /* initialize commands */
2310 qemuio_add_command(&help_cmd
);
2311 qemuio_add_command(&read_cmd
);
2312 qemuio_add_command(&readv_cmd
);
2313 qemuio_add_command(&write_cmd
);
2314 qemuio_add_command(&writev_cmd
);
2315 qemuio_add_command(&aio_read_cmd
);
2316 qemuio_add_command(&aio_write_cmd
);
2317 qemuio_add_command(&aio_flush_cmd
);
2318 qemuio_add_command(&flush_cmd
);
2319 qemuio_add_command(&truncate_cmd
);
2320 qemuio_add_command(&length_cmd
);
2321 qemuio_add_command(&info_cmd
);
2322 qemuio_add_command(&discard_cmd
);
2323 qemuio_add_command(&alloc_cmd
);
2324 qemuio_add_command(&map_cmd
);
2325 qemuio_add_command(&reopen_cmd
);
2326 qemuio_add_command(&break_cmd
);
2327 qemuio_add_command(&remove_break_cmd
);
2328 qemuio_add_command(&resume_cmd
);
2329 qemuio_add_command(&wait_break_cmd
);
2330 qemuio_add_command(&abort_cmd
);
2331 qemuio_add_command(&sleep_cmd
);
2332 qemuio_add_command(&sigraise_cmd
);