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 "sysemu/block-backend.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 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
40 cmdtab
[ncmds
- 1] = *ci
;
41 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
44 int qemuio_command_usage(const cmdinfo_t
*ci
)
46 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
50 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
52 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
55 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
56 fprintf(stderr
, "no file open, try 'help open'\n");
62 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
67 if (!init_check_command(blk
, ct
)) {
71 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
72 if (ct
->argmax
== -1) {
74 "bad argument count %d to %s, expected at least %d arguments\n",
75 argc
-1, cmd
, ct
->argmin
);
76 } else if (ct
->argmin
== ct
->argmax
) {
78 "bad argument count %d to %s, expected %d arguments\n",
79 argc
-1, cmd
, ct
->argmin
);
82 "bad argument count %d to %s, expected between %d and %d arguments\n",
83 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
88 return ct
->cfunc(blk
, argc
, argv
);
91 static const cmdinfo_t
*find_command(const char *cmd
)
95 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
96 if (strcmp(ct
->name
, cmd
) == 0 ||
97 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
99 return (const cmdinfo_t
*)ct
;
105 /* Invoke fn() for commands with a matching prefix */
106 void qemuio_complete_command(const char *input
,
107 void (*fn
)(const char *cmd
, void *opaque
),
111 size_t input_len
= strlen(input
);
113 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
114 if (strncmp(input
, ct
->name
, input_len
) == 0) {
115 fn(ct
->name
, opaque
);
120 static char **breakline(char *input
, int *count
)
124 char **rval
= g_new0(char *, 1);
126 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
131 rval
= g_renew(char *, rval
, (c
+ 1));
139 static int64_t cvtnum(const char *s
)
144 ret
= qemu_strtosz_suffix(s
, &end
, QEMU_STRTOSZ_DEFSUFFIX_B
);
146 /* Detritus at the end of the string */
152 static void print_cvtnum_err(int64_t rc
, const char *arg
)
156 printf("Parsing error: non-numeric argument,"
157 " or extraneous/unrecognized suffix -- %s\n", arg
);
160 printf("Parsing error: argument too large -- %s\n", arg
);
163 printf("Parsing error: %s\n", arg
);
167 #define EXABYTES(x) ((long long)(x) << 60)
168 #define PETABYTES(x) ((long long)(x) << 50)
169 #define TERABYTES(x) ((long long)(x) << 40)
170 #define GIGABYTES(x) ((long long)(x) << 30)
171 #define MEGABYTES(x) ((long long)(x) << 20)
172 #define KILOBYTES(x) ((long long)(x) << 10)
174 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
175 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
176 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
177 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
178 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
179 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
181 static void cvtstr(double value
, char *str
, size_t size
)
186 if (value
>= EXABYTES(1)) {
188 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
189 } else if (value
>= PETABYTES(1)) {
191 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
192 } else if (value
>= TERABYTES(1)) {
194 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
195 } else if (value
>= GIGABYTES(1)) {
197 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
198 } else if (value
>= MEGABYTES(1)) {
200 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
201 } else if (value
>= KILOBYTES(1)) {
203 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
206 snprintf(str
, size
- 6, "%f", value
);
209 trim
= strstr(str
, ".000");
211 strcpy(trim
, suffix
);
219 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
221 t1
.tv_usec
-= t2
.tv_usec
;
222 if (t1
.tv_usec
< 0) {
223 t1
.tv_usec
+= 1000000;
226 t1
.tv_sec
-= t2
.tv_sec
;
230 static double tdiv(double value
, struct timeval tv
)
232 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
235 #define HOURS(sec) ((sec) / (60 * 60))
236 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
237 #define SECONDS(sec) ((sec) % 60)
241 TERSE_FIXED_TIME
= 0x1,
242 VERBOSE_FIXED_TIME
= 0x2,
245 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
247 double usec
= (double)tv
->tv_usec
/ 1000000.0;
249 if (format
& TERSE_FIXED_TIME
) {
250 if (!HOURS(tv
->tv_sec
)) {
251 snprintf(ts
, size
, "%u:%02u.%02u",
252 (unsigned int) MINUTES(tv
->tv_sec
),
253 (unsigned int) SECONDS(tv
->tv_sec
),
254 (unsigned int) (usec
* 100));
257 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
260 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
261 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
262 (unsigned int) HOURS(tv
->tv_sec
),
263 (unsigned int) MINUTES(tv
->tv_sec
),
264 (unsigned int) SECONDS(tv
->tv_sec
),
265 (unsigned int) (usec
* 100));
267 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
272 * Parse the pattern argument to various sub-commands.
274 * Because the pattern is used as an argument to memset it must evaluate
275 * to an unsigned integer that fits into a single byte.
277 static int parse_pattern(const char *arg
)
282 pattern
= strtol(arg
, &endptr
, 0);
283 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
284 printf("%s is not a valid pattern byte\n", arg
);
292 * Memory allocation helpers.
294 * Make sure memory is aligned by default, or purposefully misaligned if
295 * that is specified on the command line.
298 #define MISALIGN_OFFSET 16
299 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
303 if (qemuio_misalign
) {
304 len
+= MISALIGN_OFFSET
;
306 buf
= blk_blockalign(blk
, len
);
307 memset(buf
, pattern
, len
);
308 if (qemuio_misalign
) {
309 buf
+= MISALIGN_OFFSET
;
314 static void qemu_io_free(void *p
)
316 if (qemuio_misalign
) {
317 p
-= MISALIGN_OFFSET
;
322 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
328 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
329 const uint8_t *s
= p
;
331 printf("%08" PRIx64
": ", offset
+ i
);
332 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
336 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
347 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
348 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
350 char s1
[64], s2
[64], ts
[64];
352 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
354 cvtstr((double)total
, s1
, sizeof(s1
));
355 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
356 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
357 op
, total
, count
, offset
);
358 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
359 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
360 } else {/* bytes,ops,time,bytes/sec,ops/sec */
361 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
363 tdiv((double)total
, *t
),
364 tdiv((double)cnt
, *t
));
369 * Parse multiple length statements for vectored I/O, and construct an I/O
370 * vector matching it.
373 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
376 size_t *sizes
= g_new0(size_t, nr_iov
);
382 for (i
= 0; i
< nr_iov
; i
++) {
388 print_cvtnum_err(len
, arg
);
392 /* should be SIZE_T_MAX, but that doesn't exist */
394 printf("Argument '%s' exceeds maximum size %d\n", arg
, INT_MAX
);
402 qemu_iovec_init(qiov
, nr_iov
);
404 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
406 for (i
= 0; i
< nr_iov
; i
++) {
407 qemu_iovec_add(qiov
, p
, sizes
[i
]);
416 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
417 int64_t count
, int64_t *total
)
419 if (count
> INT_MAX
) {
423 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
430 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
431 int64_t count
, int64_t *total
)
433 if (count
> INT_MAX
) {
437 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, 0);
453 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
455 CoWriteZeroes
*data
= opaque
;
457 data
->ret
= blk_co_write_zeroes(data
->blk
, data
->offset
, data
->count
, 0);
460 *data
->total
= data
->ret
;
464 *data
->total
= data
->count
;
467 static int do_co_write_zeroes(BlockBackend
*blk
, int64_t offset
, int64_t count
,
471 CoWriteZeroes data
= {
479 if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
483 co
= qemu_coroutine_create(co_write_zeroes_entry
);
484 qemu_coroutine_enter(co
, &data
);
486 aio_poll(blk_get_aio_context(blk
), true);
495 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
496 int64_t count
, int64_t *total
)
500 if (count
>> 9 > INT_MAX
) {
504 ret
= blk_write_compressed(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
512 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
513 int64_t count
, int64_t *total
)
515 if (count
> INT_MAX
) {
519 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
526 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
527 int64_t count
, int64_t *total
)
529 if (count
> INT_MAX
) {
533 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
540 #define NOT_DONE 0x7fffffff
541 static void aio_rw_done(void *opaque
, int ret
)
543 *(int *)opaque
= ret
;
546 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
547 int64_t offset
, int *total
)
549 int async_ret
= NOT_DONE
;
551 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
552 while (async_ret
== NOT_DONE
) {
553 main_loop_wait(false);
557 return async_ret
< 0 ? async_ret
: 1;
560 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
561 int64_t offset
, int *total
)
563 int async_ret
= NOT_DONE
;
565 blk_aio_pwritev(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
566 while (async_ret
== NOT_DONE
) {
567 main_loop_wait(false);
571 return async_ret
< 0 ? async_ret
: 1;
574 struct multiwrite_async_ret
{
579 static void multiwrite_cb(void *opaque
, int ret
)
581 struct multiwrite_async_ret
*async_ret
= opaque
;
583 async_ret
->num_done
++;
585 async_ret
->error
= ret
;
589 static int do_aio_multiwrite(BlockBackend
*blk
, BlockRequest
* reqs
,
590 int num_reqs
, int *total
)
593 struct multiwrite_async_ret async_ret
= {
599 for (i
= 0; i
< num_reqs
; i
++) {
600 reqs
[i
].cb
= multiwrite_cb
;
601 reqs
[i
].opaque
= &async_ret
;
602 *total
+= reqs
[i
].qiov
->size
;
605 ret
= blk_aio_multiwrite(blk
, reqs
, num_reqs
);
610 while (async_ret
.num_done
< num_reqs
) {
611 main_loop_wait(false);
614 return async_ret
.error
< 0 ? async_ret
.error
: 1;
617 static void read_help(void)
621 " reads a range of bytes from the given offset\n"
624 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
626 " Reads a segment of the currently open file, optionally dumping it to the\n"
627 " standard output stream (with -v option) for subsequent inspection.\n"
628 " -b, -- read from the VM state rather than the virtual disk\n"
629 " -C, -- report statistics in a machine parsable format\n"
630 " -l, -- length for pattern verification (only with -P)\n"
631 " -p, -- ignored for backwards compatibility\n"
632 " -P, -- use a pattern to verify read data\n"
633 " -q, -- quiet mode, do not show I/O statistics\n"
634 " -s, -- start offset for pattern verification (only with -P)\n"
635 " -v, -- dump buffer to standard output\n"
639 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
641 static const cmdinfo_t read_cmd
= {
647 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
648 .oneline
= "reads a number of bytes at a specified offset",
652 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
654 struct timeval t1
, t2
;
655 bool Cflag
= false, qflag
= false, vflag
= false;
656 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
661 /* Some compilers get confused and warn if this is not initialized. */
664 int64_t pattern_offset
= 0, pattern_count
= 0;
666 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
676 pattern_count
= cvtnum(optarg
);
677 if (pattern_count
< 0) {
678 print_cvtnum_err(pattern_count
, optarg
);
683 /* Ignored for backwards compatibility */
687 pattern
= parse_pattern(optarg
);
697 pattern_offset
= cvtnum(optarg
);
698 if (pattern_offset
< 0) {
699 print_cvtnum_err(pattern_offset
, optarg
);
707 return qemuio_command_usage(&read_cmd
);
711 if (optind
!= argc
- 2) {
712 return qemuio_command_usage(&read_cmd
);
715 offset
= cvtnum(argv
[optind
]);
717 print_cvtnum_err(offset
, argv
[optind
]);
722 count
= cvtnum(argv
[optind
]);
724 print_cvtnum_err(count
, argv
[optind
]);
726 } else if (count
> SIZE_MAX
) {
727 printf("length cannot exceed %" PRIu64
", given %s\n",
728 (uint64_t) SIZE_MAX
, argv
[optind
]);
732 if (!Pflag
&& (lflag
|| sflag
)) {
733 return 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 (offset
& 0x1ff) {
747 printf("offset %" PRId64
" is not sector aligned\n",
752 printf("count %"PRId64
" is not sector aligned\n",
758 buf
= qemu_io_alloc(blk
, count
, 0xab);
760 gettimeofday(&t1
, NULL
);
762 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
764 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
766 gettimeofday(&t2
, NULL
);
769 printf("read failed: %s\n", strerror(-cnt
));
774 void *cmp_buf
= g_malloc(pattern_count
);
775 memset(cmp_buf
, pattern
, pattern_count
);
776 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
777 printf("Pattern verification failed at offset %"
778 PRId64
", %"PRId64
" bytes\n",
779 offset
+ pattern_offset
, pattern_count
);
789 dump_buffer(buf
, offset
, count
);
792 /* Finally, report back -- -C gives a parsable format */
794 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
802 static void readv_help(void)
806 " reads a range of bytes from the given offset into multiple buffers\n"
809 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
811 " Reads a segment of the currently open file, optionally dumping it to the\n"
812 " standard output stream (with -v option) for subsequent inspection.\n"
813 " Uses multiple iovec buffers if more than one byte range is specified.\n"
814 " -C, -- report statistics in a machine parsable format\n"
815 " -P, -- use a pattern to verify read data\n"
816 " -v, -- dump buffer to standard output\n"
817 " -q, -- quiet mode, do not show I/O statistics\n"
821 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
823 static const cmdinfo_t readv_cmd
= {
828 .args
= "[-Cqv] [-P pattern] off len [len..]",
829 .oneline
= "reads a number of bytes at a specified offset",
833 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
835 struct timeval t1
, t2
;
836 bool Cflag
= false, qflag
= false, vflag
= false;
840 /* Some compilers get confused and warn if this is not initialized. */
847 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
854 pattern
= parse_pattern(optarg
);
866 return qemuio_command_usage(&readv_cmd
);
870 if (optind
> argc
- 2) {
871 return qemuio_command_usage(&readv_cmd
);
875 offset
= cvtnum(argv
[optind
]);
877 print_cvtnum_err(offset
, argv
[optind
]);
882 nr_iov
= argc
- optind
;
883 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
888 gettimeofday(&t1
, NULL
);
889 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
890 gettimeofday(&t2
, NULL
);
893 printf("readv failed: %s\n", strerror(-cnt
));
898 void *cmp_buf
= g_malloc(qiov
.size
);
899 memset(cmp_buf
, pattern
, qiov
.size
);
900 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
901 printf("Pattern verification failed at offset %"
902 PRId64
", %zd bytes\n", offset
, qiov
.size
);
912 dump_buffer(buf
, offset
, qiov
.size
);
915 /* Finally, report back -- -C gives a parsable format */
917 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
920 qemu_iovec_destroy(&qiov
);
925 static void write_help(void)
929 " writes a range of bytes from the given offset\n"
932 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
934 " Writes into a segment of the currently open file, using a buffer\n"
935 " filled with a set pattern (0xcdcdcdcd).\n"
936 " -b, -- write to the VM state rather than the virtual disk\n"
937 " -c, -- write compressed data with blk_write_compressed\n"
938 " -p, -- ignored for backwards compatibility\n"
939 " -P, -- use different pattern to fill file\n"
940 " -C, -- report statistics in a machine parsable format\n"
941 " -q, -- quiet mode, do not show I/O statistics\n"
942 " -z, -- write zeroes using blk_co_write_zeroes\n"
946 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
948 static const cmdinfo_t write_cmd
= {
954 .args
= "[-bcCqz] [-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;
968 /* Some compilers get confused and warn if this is not initialized. */
972 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != -1) {
984 /* Ignored for backwards compatibility */
988 pattern
= parse_pattern(optarg
);
1000 return qemuio_command_usage(&write_cmd
);
1004 if (optind
!= argc
- 2) {
1005 return qemuio_command_usage(&write_cmd
);
1008 if (bflag
&& zflag
) {
1009 printf("-b and -z cannot be specified at the same time\n");
1013 if (zflag
&& Pflag
) {
1014 printf("-z and -P cannot be specified at the same time\n");
1018 offset
= cvtnum(argv
[optind
]);
1020 print_cvtnum_err(offset
, argv
[optind
]);
1025 count
= cvtnum(argv
[optind
]);
1027 print_cvtnum_err(count
, argv
[optind
]);
1029 } else if (count
> SIZE_MAX
) {
1030 printf("length cannot exceed %" PRIu64
", given %s\n",
1031 (uint64_t) SIZE_MAX
, argv
[optind
]);
1035 if (bflag
|| cflag
) {
1036 if (offset
& 0x1ff) {
1037 printf("offset %" PRId64
" is not sector aligned\n",
1042 if (count
& 0x1ff) {
1043 printf("count %"PRId64
" is not sector aligned\n",
1050 buf
= qemu_io_alloc(blk
, count
, pattern
);
1053 gettimeofday(&t1
, NULL
);
1055 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1057 cnt
= do_co_write_zeroes(blk
, offset
, count
, &total
);
1059 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1061 cnt
= do_pwrite(blk
, buf
, offset
, count
, &total
);
1063 gettimeofday(&t2
, NULL
);
1066 printf("write failed: %s\n", strerror(-cnt
));
1074 /* Finally, report back -- -C gives a parsable format */
1076 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1091 " writes a range of bytes from the given offset source from multiple buffers\n"
1094 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1096 " Writes into a segment of the currently open file, using a buffer\n"
1097 " filled with a set pattern (0xcdcdcdcd).\n"
1098 " -P, -- use different pattern to fill file\n"
1099 " -C, -- report statistics in a machine parsable format\n"
1100 " -q, -- quiet mode, do not show I/O statistics\n"
1104 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1106 static const cmdinfo_t writev_cmd
= {
1111 .args
= "[-Cq] [-P pattern] off len [len..]",
1112 .oneline
= "writes a number of bytes at a specified offset",
1113 .help
= writev_help
,
1116 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1118 struct timeval t1
, t2
;
1119 bool Cflag
= false, qflag
= false;
1123 /* Some compilers get confused and warn if this is not initialized. */
1129 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1138 pattern
= parse_pattern(optarg
);
1144 return qemuio_command_usage(&writev_cmd
);
1148 if (optind
> argc
- 2) {
1149 return qemuio_command_usage(&writev_cmd
);
1152 offset
= cvtnum(argv
[optind
]);
1154 print_cvtnum_err(offset
, argv
[optind
]);
1159 nr_iov
= argc
- optind
;
1160 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1165 gettimeofday(&t1
, NULL
);
1166 cnt
= do_aio_writev(blk
, &qiov
, offset
, &total
);
1167 gettimeofday(&t2
, NULL
);
1170 printf("writev failed: %s\n", strerror(-cnt
));
1178 /* Finally, report back -- -C gives a parsable format */
1180 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1182 qemu_iovec_destroy(&qiov
);
1187 static void multiwrite_help(void)
1191 " writes a range of bytes from the given offset source from multiple buffers,\n"
1192 " in a batch of requests that may be merged by qemu\n"
1195 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1196 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1198 " Writes into a segment of the currently open file, using a buffer\n"
1199 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1200 " by one for each request contained in the multiwrite command.\n"
1201 " -P, -- use different pattern to fill file\n"
1202 " -C, -- report statistics in a machine parsable format\n"
1203 " -q, -- quiet mode, do not show I/O statistics\n"
1207 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
);
1209 static const cmdinfo_t multiwrite_cmd
= {
1210 .name
= "multiwrite",
1211 .cfunc
= multiwrite_f
,
1214 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1215 .oneline
= "issues multiple write requests at once",
1216 .help
= multiwrite_help
,
1219 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
)
1221 struct timeval t1
, t2
;
1222 bool Cflag
= false, qflag
= false;
1225 int64_t offset
, first_offset
= 0;
1226 /* Some compilers get confused and warn if this is not initialized. */
1231 QEMUIOVector
*qiovs
;
1235 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1244 pattern
= parse_pattern(optarg
);
1250 return qemuio_command_usage(&writev_cmd
);
1254 if (optind
> argc
- 2) {
1255 return qemuio_command_usage(&writev_cmd
);
1259 for (i
= optind
; i
< argc
; i
++) {
1260 if (!strcmp(argv
[i
], ";")) {
1265 reqs
= g_new0(BlockRequest
, nr_reqs
);
1266 buf
= g_new0(char *, nr_reqs
);
1267 qiovs
= g_new(QEMUIOVector
, nr_reqs
);
1269 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1272 /* Read the offset of the request */
1273 offset
= cvtnum(argv
[optind
]);
1275 print_cvtnum_err(offset
, argv
[optind
]);
1280 if (offset
& 0x1ff) {
1281 printf("offset %lld is not sector aligned\n",
1287 first_offset
= offset
;
1290 /* Read lengths for qiov entries */
1291 for (j
= optind
; j
< argc
; j
++) {
1292 if (!strcmp(argv
[j
], ";")) {
1297 nr_iov
= j
- optind
;
1300 buf
[i
] = create_iovec(blk
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1301 if (buf
[i
] == NULL
) {
1305 reqs
[i
].qiov
= &qiovs
[i
];
1306 reqs
[i
].sector
= offset
>> 9;
1307 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1314 /* If there were empty requests at the end, ignore them */
1317 gettimeofday(&t1
, NULL
);
1318 cnt
= do_aio_multiwrite(blk
, reqs
, nr_reqs
, &total
);
1319 gettimeofday(&t2
, NULL
);
1322 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1330 /* Finally, report back -- -C gives a parsable format */
1332 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1334 for (i
= 0; i
< nr_reqs
; i
++) {
1335 qemu_io_free(buf
[i
]);
1336 if (reqs
[i
].qiov
!= NULL
) {
1337 qemu_iovec_destroy(&qiovs
[i
]);
1356 BlockAcctCookie acct
;
1361 static void aio_write_done(void *opaque
, int ret
)
1363 struct aio_ctx
*ctx
= opaque
;
1366 gettimeofday(&t2
, NULL
);
1370 printf("aio_write failed: %s\n", strerror(-ret
));
1371 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1375 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1381 /* Finally, report back -- -C gives a parsable format */
1382 t2
= tsub(t2
, ctx
->t1
);
1383 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1384 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1387 qemu_io_free(ctx
->buf
);
1388 qemu_iovec_destroy(&ctx
->qiov
);
1393 static void aio_read_done(void *opaque
, int ret
)
1395 struct aio_ctx
*ctx
= opaque
;
1398 gettimeofday(&t2
, NULL
);
1401 printf("readv failed: %s\n", strerror(-ret
));
1402 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1407 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1409 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1410 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1411 printf("Pattern verification failed at offset %"
1412 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1417 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1424 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1427 /* Finally, report back -- -C gives a parsable format */
1428 t2
= tsub(t2
, ctx
->t1
);
1429 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1430 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1432 qemu_io_free(ctx
->buf
);
1433 qemu_iovec_destroy(&ctx
->qiov
);
1437 static void aio_read_help(void)
1441 " asynchronously reads a range of bytes from the given offset\n"
1444 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1446 " Reads a segment of the currently open file, optionally dumping it to the\n"
1447 " standard output stream (with -v option) for subsequent inspection.\n"
1448 " The read is performed asynchronously and the aio_flush command must be\n"
1449 " used to ensure all outstanding aio requests have been completed.\n"
1450 " -C, -- report statistics in a machine parsable format\n"
1451 " -P, -- use a pattern to verify read data\n"
1452 " -v, -- dump buffer to standard output\n"
1453 " -q, -- quiet mode, do not show I/O statistics\n"
1457 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1459 static const cmdinfo_t aio_read_cmd
= {
1461 .cfunc
= aio_read_f
,
1464 .args
= "[-Cqv] [-P pattern] off len [len..]",
1465 .oneline
= "asynchronously reads a number of bytes",
1466 .help
= aio_read_help
,
1469 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1472 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1475 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
1482 ctx
->pattern
= parse_pattern(optarg
);
1483 if (ctx
->pattern
< 0) {
1496 return qemuio_command_usage(&aio_read_cmd
);
1500 if (optind
> argc
- 2) {
1502 return qemuio_command_usage(&aio_read_cmd
);
1505 ctx
->offset
= cvtnum(argv
[optind
]);
1506 if (ctx
->offset
< 0) {
1507 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1513 nr_iov
= argc
- optind
;
1514 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1515 if (ctx
->buf
== NULL
) {
1516 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1521 gettimeofday(&ctx
->t1
, NULL
);
1522 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1524 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1528 static void aio_write_help(void)
1532 " asynchronously writes a range of bytes from the given offset source\n"
1533 " from multiple buffers\n"
1536 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1538 " Writes into a segment of the currently open file, using a buffer\n"
1539 " filled with a set pattern (0xcdcdcdcd).\n"
1540 " The write is performed asynchronously and the aio_flush command must be\n"
1541 " used to ensure all outstanding aio requests have been completed.\n"
1542 " -P, -- use different pattern to fill file\n"
1543 " -C, -- report statistics in a machine parsable format\n"
1544 " -q, -- quiet mode, do not show I/O statistics\n"
1545 " -z, -- write zeroes using blk_aio_write_zeroes\n"
1549 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1551 static const cmdinfo_t aio_write_cmd
= {
1552 .name
= "aio_write",
1553 .cfunc
= aio_write_f
,
1556 .args
= "[-Cqz] [-P pattern] off len [len..]",
1557 .oneline
= "asynchronously writes a number of bytes",
1558 .help
= aio_write_help
,
1561 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1565 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1568 while ((c
= getopt(argc
, argv
, "CqP:z")) != -1) {
1577 pattern
= parse_pattern(optarg
);
1588 return qemuio_command_usage(&aio_write_cmd
);
1592 if (optind
> argc
- 2) {
1594 return qemuio_command_usage(&aio_write_cmd
);
1597 if (ctx
->zflag
&& optind
!= argc
- 2) {
1598 printf("-z supports only a single length parameter\n");
1603 if (ctx
->zflag
&& ctx
->Pflag
) {
1604 printf("-z and -P cannot be specified at the same time\n");
1609 ctx
->offset
= cvtnum(argv
[optind
]);
1610 if (ctx
->offset
< 0) {
1611 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1618 int64_t count
= cvtnum(argv
[optind
]);
1620 print_cvtnum_err(count
, argv
[optind
]);
1625 ctx
->qiov
.size
= count
;
1626 blk_aio_write_zeroes(blk
, ctx
->offset
, count
, 0, aio_write_done
, ctx
);
1628 nr_iov
= argc
- optind
;
1629 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1631 if (ctx
->buf
== NULL
) {
1632 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1637 gettimeofday(&ctx
->t1
, NULL
);
1638 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1641 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_write_done
, ctx
);
1646 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1648 BlockAcctCookie cookie
;
1649 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1651 block_acct_done(blk_get_stats(blk
), &cookie
);
1655 static const cmdinfo_t aio_flush_cmd
= {
1656 .name
= "aio_flush",
1657 .cfunc
= aio_flush_f
,
1658 .oneline
= "completes all outstanding aio requests"
1661 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1667 static const cmdinfo_t flush_cmd
= {
1671 .oneline
= "flush all in-core file state to disk",
1674 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1679 offset
= cvtnum(argv
[1]);
1681 print_cvtnum_err(offset
, argv
[1]);
1685 ret
= blk_truncate(blk
, offset
);
1687 printf("truncate: %s\n", strerror(-ret
));
1694 static const cmdinfo_t truncate_cmd
= {
1697 .cfunc
= truncate_f
,
1701 .oneline
= "truncates the current file at the given offset",
1704 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1709 size
= blk_getlength(blk
);
1711 printf("getlength: %s\n", strerror(-size
));
1715 cvtstr(size
, s1
, sizeof(s1
));
1721 static const cmdinfo_t length_cmd
= {
1725 .oneline
= "gets the length of the current file",
1729 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1731 BlockDriverState
*bs
= blk_bs(blk
);
1732 BlockDriverInfo bdi
;
1733 ImageInfoSpecific
*spec_info
;
1734 char s1
[64], s2
[64];
1737 if (bs
->drv
&& bs
->drv
->format_name
) {
1738 printf("format name: %s\n", bs
->drv
->format_name
);
1740 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1741 printf("format name: %s\n", bs
->drv
->protocol_name
);
1744 ret
= bdrv_get_info(bs
, &bdi
);
1749 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1750 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1752 printf("cluster size: %s\n", s1
);
1753 printf("vm state offset: %s\n", s2
);
1755 spec_info
= bdrv_get_specific_info(bs
);
1757 printf("Format specific information:\n");
1758 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1759 qapi_free_ImageInfoSpecific(spec_info
);
1767 static const cmdinfo_t info_cmd
= {
1771 .oneline
= "prints information about the current file",
1774 static void discard_help(void)
1778 " discards a range of bytes from the given offset\n"
1781 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1783 " Discards a segment of the currently open file.\n"
1784 " -C, -- report statistics in a machine parsable format\n"
1785 " -q, -- quiet mode, do not show I/O statistics\n"
1789 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1791 static const cmdinfo_t discard_cmd
= {
1797 .args
= "[-Cq] off len",
1798 .oneline
= "discards a number of bytes at a specified offset",
1799 .help
= discard_help
,
1802 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1804 struct timeval t1
, t2
;
1805 bool Cflag
= false, qflag
= false;
1807 int64_t offset
, count
;
1809 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1818 return qemuio_command_usage(&discard_cmd
);
1822 if (optind
!= argc
- 2) {
1823 return qemuio_command_usage(&discard_cmd
);
1826 offset
= cvtnum(argv
[optind
]);
1828 print_cvtnum_err(offset
, argv
[optind
]);
1833 count
= cvtnum(argv
[optind
]);
1835 print_cvtnum_err(count
, argv
[optind
]);
1837 } else if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
1838 printf("length cannot exceed %"PRIu64
", given %s\n",
1839 (uint64_t)INT_MAX
<< BDRV_SECTOR_BITS
,
1844 gettimeofday(&t1
, NULL
);
1845 ret
= blk_discard(blk
, offset
>> BDRV_SECTOR_BITS
,
1846 count
>> BDRV_SECTOR_BITS
);
1847 gettimeofday(&t2
, NULL
);
1850 printf("discard failed: %s\n", strerror(-ret
));
1854 /* Finally, report back -- -C gives a parsable format */
1857 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1864 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1866 BlockDriverState
*bs
= blk_bs(blk
);
1867 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1872 offset
= cvtnum(argv
[1]);
1874 print_cvtnum_err(offset
, argv
[1]);
1876 } else if (offset
& 0x1ff) {
1877 printf("offset %" PRId64
" is not sector aligned\n",
1883 nb_sectors
= cvtnum(argv
[2]);
1884 if (nb_sectors
< 0) {
1885 print_cvtnum_err(nb_sectors
, argv
[2]);
1887 } else if (nb_sectors
> INT_MAX
) {
1888 printf("length argument cannot exceed %d, given %s\n",
1896 remaining
= nb_sectors
;
1898 sector_num
= offset
>> 9;
1900 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1902 printf("is_allocated failed: %s\n", strerror(-ret
));
1911 nb_sectors
-= remaining
;
1916 cvtstr(offset
, s1
, sizeof(s1
));
1918 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1919 sum_alloc
, nb_sectors
, s1
);
1923 static const cmdinfo_t alloc_cmd
= {
1929 .args
= "off [sectors]",
1930 .oneline
= "checks if a sector is present in the file",
1934 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1935 int64_t nb_sectors
, int64_t *pnum
)
1937 int num
, num_checked
;
1940 num_checked
= MIN(nb_sectors
, INT_MAX
);
1941 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1949 while (nb_sectors
> 0 && ret
== firstret
) {
1953 num_checked
= MIN(nb_sectors
, INT_MAX
);
1954 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1955 if (ret
== firstret
&& num
) {
1965 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1968 int64_t nb_sectors
, total_sectors
;
1975 total_sectors
= blk_nb_sectors(blk
);
1976 if (total_sectors
< 0) {
1977 error_report("Failed to query image length: %s",
1978 strerror(-total_sectors
));
1982 nb_sectors
= total_sectors
;
1985 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1987 error_report("Failed to get allocation status: %s", strerror(-ret
));
1990 error_report("Unexpected end of image");
1994 retstr
= ret
? " allocated" : "not allocated";
1995 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1996 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1997 "at offset %s (%d)\n",
1998 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
2002 } while (offset
< total_sectors
);
2007 static const cmdinfo_t map_cmd
= {
2013 .oneline
= "prints the allocated areas of a file",
2016 static void reopen_help(void)
2020 " Changes the open options of an already opened image\n"
2023 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2025 " -r, -- Reopen the image read-only\n"
2026 " -c, -- Change the cache mode to the given value\n"
2027 " -o, -- Changes block driver options (cf. 'open' command)\n"
2031 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2033 static QemuOptsList reopen_opts
= {
2035 .merge_lists
= true,
2036 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2038 /* no elements => accept any params */
2039 { /* end of list */ }
2043 static const cmdinfo_t reopen_cmd
= {
2048 .args
= "[-r] [-c cache] [-o options]",
2049 .oneline
= "reopens an image with new options",
2050 .help
= reopen_help
,
2053 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2055 BlockDriverState
*bs
= blk_bs(blk
);
2059 int flags
= bs
->open_flags
;
2060 bool writethrough
= !blk_enable_write_cache(blk
);
2062 BlockReopenQueue
*brq
;
2063 Error
*local_err
= NULL
;
2065 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
2068 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2069 error_report("Invalid cache option: %s", optarg
);
2074 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2075 qemu_opts_reset(&reopen_opts
);
2080 flags
&= ~BDRV_O_RDWR
;
2083 qemu_opts_reset(&reopen_opts
);
2084 return qemuio_command_usage(&reopen_cmd
);
2088 if (optind
!= argc
) {
2089 qemu_opts_reset(&reopen_opts
);
2090 return qemuio_command_usage(&reopen_cmd
);
2093 if (writethrough
!= blk_enable_write_cache(blk
) &&
2094 blk_get_attached_dev(blk
))
2096 error_report("Cannot change cache.writeback: Device attached");
2097 qemu_opts_reset(&reopen_opts
);
2101 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2102 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2103 qemu_opts_reset(&reopen_opts
);
2105 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2106 bdrv_reopen_multiple(brq
, &local_err
);
2108 error_report_err(local_err
);
2110 blk_set_enable_write_cache(blk
, !writethrough
);
2116 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2120 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2122 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2128 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2132 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2134 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2140 static const cmdinfo_t break_cmd
= {
2145 .args
= "event tag",
2146 .oneline
= "sets a breakpoint on event and tags the stopped "
2150 static const cmdinfo_t remove_break_cmd
= {
2151 .name
= "remove_break",
2154 .cfunc
= remove_break_f
,
2156 .oneline
= "remove a breakpoint by tag",
2159 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2163 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2165 printf("Could not resume request: %s\n", strerror(-ret
));
2171 static const cmdinfo_t resume_cmd
= {
2177 .oneline
= "resumes the request tagged as tag",
2180 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2182 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2183 aio_poll(blk_get_aio_context(blk
), true);
2189 static const cmdinfo_t wait_break_cmd
= {
2190 .name
= "wait_break",
2193 .cfunc
= wait_break_f
,
2195 .oneline
= "waits for the suspension of a request",
2198 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2203 static const cmdinfo_t abort_cmd
= {
2206 .flags
= CMD_NOFILE_OK
,
2207 .oneline
= "simulate a program crash using abort(3)",
2210 static void sigraise_help(void)
2214 " raises the given signal\n"
2217 " 'sigraise %i' - raises SIGTERM\n"
2219 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2220 " given to sigraise.\n"
2224 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2226 static const cmdinfo_t sigraise_cmd
= {
2228 .cfunc
= sigraise_f
,
2231 .flags
= CMD_NOFILE_OK
,
2233 .oneline
= "raises a signal",
2234 .help
= sigraise_help
,
2237 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2239 int64_t sig
= cvtnum(argv
[1]);
2241 print_cvtnum_err(sig
, argv
[1]);
2243 } else if (sig
> NSIG
) {
2244 printf("signal argument '%s' is too large to be a valid signal\n",
2249 /* Using raise() to kill this process does not necessarily flush all open
2250 * streams. At least stdout and stderr (although the latter should be
2251 * non-buffered anyway) should be flushed, though. */
2259 static void sleep_cb(void *opaque
)
2261 bool *expired
= opaque
;
2265 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2269 struct QEMUTimer
*timer
;
2270 bool expired
= false;
2272 ms
= strtol(argv
[1], &endptr
, 0);
2273 if (ms
< 0 || *endptr
!= '\0') {
2274 printf("%s is not a valid number\n", argv
[1]);
2278 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2279 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2282 main_loop_wait(false);
2290 static const cmdinfo_t sleep_cmd
= {
2295 .flags
= CMD_NOFILE_OK
,
2296 .oneline
= "waits for the given value in milliseconds",
2299 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2304 printf("%s ", ct
->name
);
2306 printf("(or %s) ", ct
->altname
);
2311 printf("%s ", ct
->args
);
2313 printf("-- %s\n", ct
->oneline
);
2316 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2318 help_oneline(cmd
, ct
);
2324 static void help_all(void)
2326 const cmdinfo_t
*ct
;
2328 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2329 help_oneline(ct
->name
, ct
);
2331 printf("\nUse 'help commandname' for extended help.\n");
2334 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2336 const cmdinfo_t
*ct
;
2343 ct
= find_command(argv
[1]);
2345 printf("command %s not found\n", argv
[1]);
2349 help_onecmd(argv
[1], ct
);
2353 static const cmdinfo_t help_cmd
= {
2359 .flags
= CMD_FLAG_GLOBAL
,
2360 .args
= "[command]",
2361 .oneline
= "help for one or all commands",
2364 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2367 const cmdinfo_t
*ct
;
2372 input
= g_strdup(cmd
);
2373 v
= breakline(input
, &c
);
2375 ct
= find_command(v
[0]);
2377 done
= command(blk
, ct
, c
, v
);
2379 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2388 static void __attribute((constructor
)) init_qemuio_commands(void)
2390 /* initialize commands */
2391 qemuio_add_command(&help_cmd
);
2392 qemuio_add_command(&read_cmd
);
2393 qemuio_add_command(&readv_cmd
);
2394 qemuio_add_command(&write_cmd
);
2395 qemuio_add_command(&writev_cmd
);
2396 qemuio_add_command(&multiwrite_cmd
);
2397 qemuio_add_command(&aio_read_cmd
);
2398 qemuio_add_command(&aio_write_cmd
);
2399 qemuio_add_command(&aio_flush_cmd
);
2400 qemuio_add_command(&flush_cmd
);
2401 qemuio_add_command(&truncate_cmd
);
2402 qemuio_add_command(&length_cmd
);
2403 qemuio_add_command(&info_cmd
);
2404 qemuio_add_command(&discard_cmd
);
2405 qemuio_add_command(&alloc_cmd
);
2406 qemuio_add_command(&map_cmd
);
2407 qemuio_add_command(&reopen_cmd
);
2408 qemuio_add_command(&break_cmd
);
2409 qemuio_add_command(&remove_break_cmd
);
2410 qemuio_add_command(&resume_cmd
);
2411 qemuio_add_command(&wait_break_cmd
);
2412 qemuio_add_command(&abort_cmd
);
2413 qemuio_add_command(&sleep_cmd
);
2414 qemuio_add_command(&sigraise_cmd
);