2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 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.
12 #include "sysemu/block-backend.h"
13 #include "block/block.h"
14 #include "block/block_int.h" /* for info_f() */
15 #include "block/qapi.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "sysemu/block-backend.h"
21 #define CMD_NOFILE_OK 0x01
25 static cmdinfo_t
*cmdtab
;
28 static int compare_cmdname(const void *a
, const void *b
)
30 return strcmp(((const cmdinfo_t
*)a
)->name
,
31 ((const cmdinfo_t
*)b
)->name
);
34 void qemuio_add_command(const cmdinfo_t
*ci
)
36 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
37 cmdtab
[ncmds
- 1] = *ci
;
38 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
41 int qemuio_command_usage(const cmdinfo_t
*ci
)
43 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
47 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
49 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
52 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
53 fprintf(stderr
, "no file open, try 'help open'\n");
59 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
64 if (!init_check_command(blk
, ct
)) {
68 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
69 if (ct
->argmax
== -1) {
71 "bad argument count %d to %s, expected at least %d arguments\n",
72 argc
-1, cmd
, ct
->argmin
);
73 } else if (ct
->argmin
== ct
->argmax
) {
75 "bad argument count %d to %s, expected %d arguments\n",
76 argc
-1, cmd
, ct
->argmin
);
79 "bad argument count %d to %s, expected between %d and %d arguments\n",
80 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
85 return ct
->cfunc(blk
, argc
, argv
);
88 static const cmdinfo_t
*find_command(const char *cmd
)
92 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
93 if (strcmp(ct
->name
, cmd
) == 0 ||
94 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
96 return (const cmdinfo_t
*)ct
;
102 /* Invoke fn() for commands with a matching prefix */
103 void qemuio_complete_command(const char *input
,
104 void (*fn
)(const char *cmd
, void *opaque
),
108 size_t input_len
= strlen(input
);
110 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
111 if (strncmp(input
, ct
->name
, input_len
) == 0) {
112 fn(ct
->name
, opaque
);
117 static char **breakline(char *input
, int *count
)
121 char **rval
= g_new0(char *, 1);
123 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
128 rval
= g_renew(char *, rval
, (c
+ 1));
136 static int64_t cvtnum(const char *s
)
139 return qemu_strtosz_suffix(s
, &end
, QEMU_STRTOSZ_DEFSUFFIX_B
);
142 #define EXABYTES(x) ((long long)(x) << 60)
143 #define PETABYTES(x) ((long long)(x) << 50)
144 #define TERABYTES(x) ((long long)(x) << 40)
145 #define GIGABYTES(x) ((long long)(x) << 30)
146 #define MEGABYTES(x) ((long long)(x) << 20)
147 #define KILOBYTES(x) ((long long)(x) << 10)
149 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
150 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
151 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
152 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
153 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
154 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
156 static void cvtstr(double value
, char *str
, size_t size
)
161 if (value
>= EXABYTES(1)) {
163 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
164 } else if (value
>= PETABYTES(1)) {
166 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
167 } else if (value
>= TERABYTES(1)) {
169 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
170 } else if (value
>= GIGABYTES(1)) {
172 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
173 } else if (value
>= MEGABYTES(1)) {
175 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
176 } else if (value
>= KILOBYTES(1)) {
178 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
181 snprintf(str
, size
- 6, "%f", value
);
184 trim
= strstr(str
, ".000");
186 strcpy(trim
, suffix
);
194 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
196 t1
.tv_usec
-= t2
.tv_usec
;
197 if (t1
.tv_usec
< 0) {
198 t1
.tv_usec
+= 1000000;
201 t1
.tv_sec
-= t2
.tv_sec
;
205 static double tdiv(double value
, struct timeval tv
)
207 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
210 #define HOURS(sec) ((sec) / (60 * 60))
211 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
212 #define SECONDS(sec) ((sec) % 60)
216 TERSE_FIXED_TIME
= 0x1,
217 VERBOSE_FIXED_TIME
= 0x2,
220 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
222 double usec
= (double)tv
->tv_usec
/ 1000000.0;
224 if (format
& TERSE_FIXED_TIME
) {
225 if (!HOURS(tv
->tv_sec
)) {
226 snprintf(ts
, size
, "%u:%02u.%02u",
227 (unsigned int) MINUTES(tv
->tv_sec
),
228 (unsigned int) SECONDS(tv
->tv_sec
),
229 (unsigned int) (usec
* 100));
232 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
235 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
236 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
237 (unsigned int) HOURS(tv
->tv_sec
),
238 (unsigned int) MINUTES(tv
->tv_sec
),
239 (unsigned int) SECONDS(tv
->tv_sec
),
240 (unsigned int) (usec
* 100));
242 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
247 * Parse the pattern argument to various sub-commands.
249 * Because the pattern is used as an argument to memset it must evaluate
250 * to an unsigned integer that fits into a single byte.
252 static int parse_pattern(const char *arg
)
257 pattern
= strtol(arg
, &endptr
, 0);
258 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
259 printf("%s is not a valid pattern byte\n", arg
);
267 * Memory allocation helpers.
269 * Make sure memory is aligned by default, or purposefully misaligned if
270 * that is specified on the command line.
273 #define MISALIGN_OFFSET 16
274 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
278 if (qemuio_misalign
) {
279 len
+= MISALIGN_OFFSET
;
281 buf
= blk_blockalign(blk
, len
);
282 memset(buf
, pattern
, len
);
283 if (qemuio_misalign
) {
284 buf
+= MISALIGN_OFFSET
;
289 static void qemu_io_free(void *p
)
291 if (qemuio_misalign
) {
292 p
-= MISALIGN_OFFSET
;
297 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
303 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
304 const uint8_t *s
= p
;
306 printf("%08" PRIx64
": ", offset
+ i
);
307 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
311 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
322 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
323 int64_t count
, int64_t total
, int cnt
, int Cflag
)
325 char s1
[64], s2
[64], ts
[64];
327 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
329 cvtstr((double)total
, s1
, sizeof(s1
));
330 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
331 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
332 op
, total
, count
, offset
);
333 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
334 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
335 } else {/* bytes,ops,time,bytes/sec,ops/sec */
336 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
338 tdiv((double)total
, *t
),
339 tdiv((double)cnt
, *t
));
344 * Parse multiple length statements for vectored I/O, and construct an I/O
345 * vector matching it.
348 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
351 size_t *sizes
= g_new0(size_t, nr_iov
);
357 for (i
= 0; i
< nr_iov
; i
++) {
363 printf("non-numeric length argument -- %s\n", arg
);
367 /* should be SIZE_T_MAX, but that doesn't exist */
369 printf("too large length argument -- %s\n", arg
);
374 printf("length argument %" PRId64
375 " is not sector aligned\n", len
);
383 qemu_iovec_init(qiov
, nr_iov
);
385 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
387 for (i
= 0; i
< nr_iov
; i
++) {
388 qemu_iovec_add(qiov
, p
, sizes
[i
]);
397 static int do_read(BlockBackend
*blk
, char *buf
, int64_t offset
, int64_t count
,
402 if (count
>> 9 > INT_MAX
) {
406 ret
= blk_read(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
414 static int do_write(BlockBackend
*blk
, char *buf
, int64_t offset
, int64_t count
,
419 if (count
>> 9 > INT_MAX
) {
423 ret
= blk_write(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
431 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
432 int64_t count
, int64_t *total
)
434 if (count
> INT_MAX
) {
438 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
445 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
446 int64_t count
, int64_t *total
)
448 if (count
> INT_MAX
) {
452 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
);
468 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
470 CoWriteZeroes
*data
= opaque
;
472 data
->ret
= blk_co_write_zeroes(data
->blk
, data
->offset
/ BDRV_SECTOR_SIZE
,
473 data
->count
/ BDRV_SECTOR_SIZE
, 0);
476 *data
->total
= data
->ret
;
480 *data
->total
= data
->count
;
483 static int do_co_write_zeroes(BlockBackend
*blk
, int64_t offset
, int64_t count
,
487 CoWriteZeroes data
= {
495 if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
499 co
= qemu_coroutine_create(co_write_zeroes_entry
);
500 qemu_coroutine_enter(co
, &data
);
502 aio_poll(blk_get_aio_context(blk
), true);
511 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
512 int64_t count
, int64_t *total
)
516 if (count
>> 9 > INT_MAX
) {
520 ret
= blk_write_compressed(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
528 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
529 int64_t count
, int64_t *total
)
531 if (count
> INT_MAX
) {
535 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
542 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
543 int64_t count
, int64_t *total
)
545 if (count
> INT_MAX
) {
549 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
556 #define NOT_DONE 0x7fffffff
557 static void aio_rw_done(void *opaque
, int ret
)
559 *(int *)opaque
= ret
;
562 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
563 int64_t offset
, int *total
)
565 int async_ret
= NOT_DONE
;
567 blk_aio_readv(blk
, offset
>> 9, qiov
, qiov
->size
>> 9,
568 aio_rw_done
, &async_ret
);
569 while (async_ret
== NOT_DONE
) {
570 main_loop_wait(false);
574 return async_ret
< 0 ? async_ret
: 1;
577 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
578 int64_t offset
, int *total
)
580 int async_ret
= NOT_DONE
;
582 blk_aio_writev(blk
, offset
>> 9, qiov
, qiov
->size
>> 9,
583 aio_rw_done
, &async_ret
);
584 while (async_ret
== NOT_DONE
) {
585 main_loop_wait(false);
589 return async_ret
< 0 ? async_ret
: 1;
592 struct multiwrite_async_ret
{
597 static void multiwrite_cb(void *opaque
, int ret
)
599 struct multiwrite_async_ret
*async_ret
= opaque
;
601 async_ret
->num_done
++;
603 async_ret
->error
= ret
;
607 static int do_aio_multiwrite(BlockBackend
*blk
, BlockRequest
* reqs
,
608 int num_reqs
, int *total
)
611 struct multiwrite_async_ret async_ret
= {
617 for (i
= 0; i
< num_reqs
; i
++) {
618 reqs
[i
].cb
= multiwrite_cb
;
619 reqs
[i
].opaque
= &async_ret
;
620 *total
+= reqs
[i
].qiov
->size
;
623 ret
= blk_aio_multiwrite(blk
, reqs
, num_reqs
);
628 while (async_ret
.num_done
< num_reqs
) {
629 main_loop_wait(false);
632 return async_ret
.error
< 0 ? async_ret
.error
: 1;
635 static void read_help(void)
639 " reads a range of bytes from the given offset\n"
642 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
644 " Reads a segment of the currently open file, optionally dumping it to the\n"
645 " standard output stream (with -v option) for subsequent inspection.\n"
646 " -b, -- read from the VM state rather than the virtual disk\n"
647 " -C, -- report statistics in a machine parsable format\n"
648 " -l, -- length for pattern verification (only with -P)\n"
649 " -p, -- use blk_pread to read the file\n"
650 " -P, -- use a pattern to verify read data\n"
651 " -q, -- quiet mode, do not show I/O statistics\n"
652 " -s, -- start offset for pattern verification (only with -P)\n"
653 " -v, -- dump buffer to standard output\n"
657 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
659 static const cmdinfo_t read_cmd
= {
665 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
666 .oneline
= "reads a number of bytes at a specified offset",
670 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
672 struct timeval t1
, t2
;
673 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
674 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
679 /* Some compilers get confused and warn if this is not initialized. */
682 int64_t pattern_offset
= 0, pattern_count
= 0;
684 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
694 pattern_count
= cvtnum(optarg
);
695 if (pattern_count
< 0) {
696 printf("non-numeric length argument -- %s\n", optarg
);
705 pattern
= parse_pattern(optarg
);
715 pattern_offset
= cvtnum(optarg
);
716 if (pattern_offset
< 0) {
717 printf("non-numeric length argument -- %s\n", optarg
);
725 return qemuio_command_usage(&read_cmd
);
729 if (optind
!= argc
- 2) {
730 return qemuio_command_usage(&read_cmd
);
733 if (bflag
&& pflag
) {
734 printf("-b and -p cannot be specified at the same time\n");
738 offset
= cvtnum(argv
[optind
]);
740 printf("non-numeric length argument -- %s\n", argv
[optind
]);
745 count
= cvtnum(argv
[optind
]);
747 printf("non-numeric length argument -- %s\n", argv
[optind
]);
749 } else if (count
> SIZE_MAX
) {
750 printf("length cannot exceed %" PRIu64
", given %s\n",
751 (uint64_t) SIZE_MAX
, argv
[optind
]);
755 if (!Pflag
&& (lflag
|| sflag
)) {
756 return qemuio_command_usage(&read_cmd
);
760 pattern_count
= count
- pattern_offset
;
763 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
764 printf("pattern verification range exceeds end of read data\n");
769 if (offset
& 0x1ff) {
770 printf("offset %" PRId64
" is not sector aligned\n",
775 printf("count %"PRId64
" is not sector aligned\n",
781 buf
= qemu_io_alloc(blk
, count
, 0xab);
783 gettimeofday(&t1
, NULL
);
785 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
787 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
789 cnt
= do_read(blk
, buf
, offset
, count
, &total
);
791 gettimeofday(&t2
, NULL
);
794 printf("read failed: %s\n", strerror(-cnt
));
799 void *cmp_buf
= g_malloc(pattern_count
);
800 memset(cmp_buf
, pattern
, pattern_count
);
801 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
802 printf("Pattern verification failed at offset %"
803 PRId64
", %"PRId64
" bytes\n",
804 offset
+ pattern_offset
, pattern_count
);
814 dump_buffer(buf
, offset
, count
);
817 /* Finally, report back -- -C gives a parsable format */
819 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
827 static void readv_help(void)
831 " reads a range of bytes from the given offset into multiple buffers\n"
834 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
836 " Reads a segment of the currently open file, optionally dumping it to the\n"
837 " standard output stream (with -v option) for subsequent inspection.\n"
838 " Uses multiple iovec buffers if more than one byte range is specified.\n"
839 " -C, -- report statistics in a machine parsable format\n"
840 " -P, -- use a pattern to verify read data\n"
841 " -v, -- dump buffer to standard output\n"
842 " -q, -- quiet mode, do not show I/O statistics\n"
846 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
848 static const cmdinfo_t readv_cmd
= {
853 .args
= "[-Cqv] [-P pattern ] off len [len..]",
854 .oneline
= "reads a number of bytes at a specified offset",
858 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
860 struct timeval t1
, t2
;
861 int Cflag
= 0, qflag
= 0, vflag
= 0;
865 /* Some compilers get confused and warn if this is not initialized. */
872 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
879 pattern
= parse_pattern(optarg
);
891 return qemuio_command_usage(&readv_cmd
);
895 if (optind
> argc
- 2) {
896 return qemuio_command_usage(&readv_cmd
);
900 offset
= cvtnum(argv
[optind
]);
902 printf("non-numeric length argument -- %s\n", argv
[optind
]);
907 if (offset
& 0x1ff) {
908 printf("offset %" PRId64
" is not sector aligned\n",
913 nr_iov
= argc
- optind
;
914 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
919 gettimeofday(&t1
, NULL
);
920 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
921 gettimeofday(&t2
, NULL
);
924 printf("readv failed: %s\n", strerror(-cnt
));
929 void *cmp_buf
= g_malloc(qiov
.size
);
930 memset(cmp_buf
, pattern
, qiov
.size
);
931 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
932 printf("Pattern verification failed at offset %"
933 PRId64
", %zd bytes\n", offset
, qiov
.size
);
943 dump_buffer(buf
, offset
, qiov
.size
);
946 /* Finally, report back -- -C gives a parsable format */
948 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
951 qemu_iovec_destroy(&qiov
);
956 static void write_help(void)
960 " writes a range of bytes from the given offset\n"
963 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
965 " Writes into a segment of the currently open file, using a buffer\n"
966 " filled with a set pattern (0xcdcdcdcd).\n"
967 " -b, -- write to the VM state rather than the virtual disk\n"
968 " -c, -- write compressed data with blk_write_compressed\n"
969 " -p, -- use blk_pwrite to write the file\n"
970 " -P, -- use different pattern to fill file\n"
971 " -C, -- report statistics in a machine parsable format\n"
972 " -q, -- quiet mode, do not show I/O statistics\n"
973 " -z, -- write zeroes using blk_co_write_zeroes\n"
977 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
979 static const cmdinfo_t write_cmd
= {
985 .args
= "[-bcCpqz] [-P pattern ] off len",
986 .oneline
= "writes a number of bytes at a specified offset",
990 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
992 struct timeval t1
, t2
;
993 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
999 /* Some compilers get confused and warn if this is not initialized. */
1003 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != -1) {
1019 pattern
= parse_pattern(optarg
);
1031 return qemuio_command_usage(&write_cmd
);
1035 if (optind
!= argc
- 2) {
1036 return qemuio_command_usage(&write_cmd
);
1039 if (bflag
+ pflag
+ zflag
> 1) {
1040 printf("-b, -p, or -z cannot be specified at the same time\n");
1044 if (zflag
&& Pflag
) {
1045 printf("-z and -P cannot be specified at the same time\n");
1049 offset
= cvtnum(argv
[optind
]);
1051 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1056 count
= cvtnum(argv
[optind
]);
1058 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1060 } else if (count
> SIZE_MAX
) {
1061 printf("length cannot exceed %" PRIu64
", given %s\n",
1062 (uint64_t) SIZE_MAX
, argv
[optind
]);
1067 if (offset
& 0x1ff) {
1068 printf("offset %" PRId64
" is not sector aligned\n",
1073 if (count
& 0x1ff) {
1074 printf("count %"PRId64
" is not sector aligned\n",
1081 buf
= qemu_io_alloc(blk
, count
, pattern
);
1084 gettimeofday(&t1
, NULL
);
1086 cnt
= do_pwrite(blk
, buf
, offset
, count
, &total
);
1088 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1090 cnt
= do_co_write_zeroes(blk
, offset
, count
, &total
);
1092 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1094 cnt
= do_write(blk
, buf
, offset
, count
, &total
);
1096 gettimeofday(&t2
, NULL
);
1099 printf("write failed: %s\n", strerror(-cnt
));
1107 /* Finally, report back -- -C gives a parsable format */
1109 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1124 " writes a range of bytes from the given offset source from multiple buffers\n"
1127 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1129 " Writes into a segment of the currently open file, using a buffer\n"
1130 " filled with a set pattern (0xcdcdcdcd).\n"
1131 " -P, -- use different pattern to fill file\n"
1132 " -C, -- report statistics in a machine parsable format\n"
1133 " -q, -- quiet mode, do not show I/O statistics\n"
1137 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1139 static const cmdinfo_t writev_cmd
= {
1144 .args
= "[-Cq] [-P pattern ] off len [len..]",
1145 .oneline
= "writes a number of bytes at a specified offset",
1146 .help
= writev_help
,
1149 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1151 struct timeval t1
, t2
;
1152 int Cflag
= 0, qflag
= 0;
1156 /* Some compilers get confused and warn if this is not initialized. */
1162 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1171 pattern
= parse_pattern(optarg
);
1177 return qemuio_command_usage(&writev_cmd
);
1181 if (optind
> argc
- 2) {
1182 return qemuio_command_usage(&writev_cmd
);
1185 offset
= cvtnum(argv
[optind
]);
1187 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1192 if (offset
& 0x1ff) {
1193 printf("offset %" PRId64
" is not sector aligned\n",
1198 nr_iov
= argc
- optind
;
1199 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1204 gettimeofday(&t1
, NULL
);
1205 cnt
= do_aio_writev(blk
, &qiov
, offset
, &total
);
1206 gettimeofday(&t2
, NULL
);
1209 printf("writev failed: %s\n", strerror(-cnt
));
1217 /* Finally, report back -- -C gives a parsable format */
1219 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1221 qemu_iovec_destroy(&qiov
);
1226 static void multiwrite_help(void)
1230 " writes a range of bytes from the given offset source from multiple buffers,\n"
1231 " in a batch of requests that may be merged by qemu\n"
1234 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1235 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1237 " Writes into a segment of the currently open file, using a buffer\n"
1238 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1239 " by one for each request contained in the multiwrite command.\n"
1240 " -P, -- use different pattern to fill file\n"
1241 " -C, -- report statistics in a machine parsable format\n"
1242 " -q, -- quiet mode, do not show I/O statistics\n"
1246 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
);
1248 static const cmdinfo_t multiwrite_cmd
= {
1249 .name
= "multiwrite",
1250 .cfunc
= multiwrite_f
,
1253 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1254 .oneline
= "issues multiple write requests at once",
1255 .help
= multiwrite_help
,
1258 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
)
1260 struct timeval t1
, t2
;
1261 int Cflag
= 0, qflag
= 0;
1264 int64_t offset
, first_offset
= 0;
1265 /* Some compilers get confused and warn if this is not initialized. */
1270 QEMUIOVector
*qiovs
;
1274 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1283 pattern
= parse_pattern(optarg
);
1289 return qemuio_command_usage(&writev_cmd
);
1293 if (optind
> argc
- 2) {
1294 return qemuio_command_usage(&writev_cmd
);
1298 for (i
= optind
; i
< argc
; i
++) {
1299 if (!strcmp(argv
[i
], ";")) {
1304 reqs
= g_new0(BlockRequest
, nr_reqs
);
1305 buf
= g_new0(char *, nr_reqs
);
1306 qiovs
= g_new(QEMUIOVector
, nr_reqs
);
1308 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1311 /* Read the offset of the request */
1312 offset
= cvtnum(argv
[optind
]);
1314 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1319 if (offset
& 0x1ff) {
1320 printf("offset %lld is not sector aligned\n",
1326 first_offset
= offset
;
1329 /* Read lengths for qiov entries */
1330 for (j
= optind
; j
< argc
; j
++) {
1331 if (!strcmp(argv
[j
], ";")) {
1336 nr_iov
= j
- optind
;
1339 buf
[i
] = create_iovec(blk
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1340 if (buf
[i
] == NULL
) {
1344 reqs
[i
].qiov
= &qiovs
[i
];
1345 reqs
[i
].sector
= offset
>> 9;
1346 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1353 /* If there were empty requests at the end, ignore them */
1356 gettimeofday(&t1
, NULL
);
1357 cnt
= do_aio_multiwrite(blk
, reqs
, nr_reqs
, &total
);
1358 gettimeofday(&t2
, NULL
);
1361 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1369 /* Finally, report back -- -C gives a parsable format */
1371 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1373 for (i
= 0; i
< nr_reqs
; i
++) {
1374 qemu_io_free(buf
[i
]);
1375 if (reqs
[i
].qiov
!= NULL
) {
1376 qemu_iovec_destroy(&qiovs
[i
]);
1394 BlockAcctCookie acct
;
1399 static void aio_write_done(void *opaque
, int ret
)
1401 struct aio_ctx
*ctx
= opaque
;
1404 gettimeofday(&t2
, NULL
);
1408 printf("aio_write failed: %s\n", strerror(-ret
));
1412 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1418 /* Finally, report back -- -C gives a parsable format */
1419 t2
= tsub(t2
, ctx
->t1
);
1420 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1421 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1423 qemu_io_free(ctx
->buf
);
1424 qemu_iovec_destroy(&ctx
->qiov
);
1428 static void aio_read_done(void *opaque
, int ret
)
1430 struct aio_ctx
*ctx
= opaque
;
1433 gettimeofday(&t2
, NULL
);
1436 printf("readv failed: %s\n", strerror(-ret
));
1441 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1443 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1444 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1445 printf("Pattern verification failed at offset %"
1446 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1451 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1458 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1461 /* Finally, report back -- -C gives a parsable format */
1462 t2
= tsub(t2
, ctx
->t1
);
1463 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1464 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1466 qemu_io_free(ctx
->buf
);
1467 qemu_iovec_destroy(&ctx
->qiov
);
1471 static void aio_read_help(void)
1475 " asynchronously reads a range of bytes from the given offset\n"
1478 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1480 " Reads a segment of the currently open file, optionally dumping it to the\n"
1481 " standard output stream (with -v option) for subsequent inspection.\n"
1482 " The read is performed asynchronously and the aio_flush command must be\n"
1483 " used to ensure all outstanding aio requests have been completed.\n"
1484 " -C, -- report statistics in a machine parsable format\n"
1485 " -P, -- use a pattern to verify read data\n"
1486 " -v, -- dump buffer to standard output\n"
1487 " -q, -- quiet mode, do not show I/O statistics\n"
1491 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1493 static const cmdinfo_t aio_read_cmd
= {
1495 .cfunc
= aio_read_f
,
1498 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1499 .oneline
= "asynchronously reads a number of bytes",
1500 .help
= aio_read_help
,
1503 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1506 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1509 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
1516 ctx
->pattern
= parse_pattern(optarg
);
1517 if (ctx
->pattern
< 0) {
1530 return qemuio_command_usage(&aio_read_cmd
);
1534 if (optind
> argc
- 2) {
1536 return qemuio_command_usage(&aio_read_cmd
);
1539 ctx
->offset
= cvtnum(argv
[optind
]);
1540 if (ctx
->offset
< 0) {
1541 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1547 if (ctx
->offset
& 0x1ff) {
1548 printf("offset %" PRId64
" is not sector aligned\n",
1554 nr_iov
= argc
- optind
;
1555 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1556 if (ctx
->buf
== NULL
) {
1561 gettimeofday(&ctx
->t1
, NULL
);
1562 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1564 blk_aio_readv(blk
, ctx
->offset
>> 9, &ctx
->qiov
,
1565 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1569 static void aio_write_help(void)
1573 " asynchronously writes a range of bytes from the given offset source\n"
1574 " from multiple buffers\n"
1577 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1579 " Writes into a segment of the currently open file, using a buffer\n"
1580 " filled with a set pattern (0xcdcdcdcd).\n"
1581 " The write is performed asynchronously and the aio_flush command must be\n"
1582 " used to ensure all outstanding aio requests have been completed.\n"
1583 " -P, -- use different pattern to fill file\n"
1584 " -C, -- report statistics in a machine parsable format\n"
1585 " -q, -- quiet mode, do not show I/O statistics\n"
1589 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1591 static const cmdinfo_t aio_write_cmd
= {
1592 .name
= "aio_write",
1593 .cfunc
= aio_write_f
,
1596 .args
= "[-Cq] [-P pattern ] off len [len..]",
1597 .oneline
= "asynchronously writes a number of bytes",
1598 .help
= aio_write_help
,
1601 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1605 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1608 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1617 pattern
= parse_pattern(optarg
);
1625 return qemuio_command_usage(&aio_write_cmd
);
1629 if (optind
> argc
- 2) {
1631 return qemuio_command_usage(&aio_write_cmd
);
1634 ctx
->offset
= cvtnum(argv
[optind
]);
1635 if (ctx
->offset
< 0) {
1636 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1642 if (ctx
->offset
& 0x1ff) {
1643 printf("offset %" PRId64
" is not sector aligned\n",
1649 nr_iov
= argc
- optind
;
1650 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1651 if (ctx
->buf
== NULL
) {
1656 gettimeofday(&ctx
->t1
, NULL
);
1657 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1659 blk_aio_writev(blk
, ctx
->offset
>> 9, &ctx
->qiov
,
1660 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1664 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1670 static const cmdinfo_t aio_flush_cmd
= {
1671 .name
= "aio_flush",
1672 .cfunc
= aio_flush_f
,
1673 .oneline
= "completes all outstanding aio requests"
1676 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1682 static const cmdinfo_t flush_cmd
= {
1686 .oneline
= "flush all in-core file state to disk",
1689 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1694 offset
= cvtnum(argv
[1]);
1696 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1700 ret
= blk_truncate(blk
, offset
);
1702 printf("truncate: %s\n", strerror(-ret
));
1709 static const cmdinfo_t truncate_cmd
= {
1712 .cfunc
= truncate_f
,
1716 .oneline
= "truncates the current file at the given offset",
1719 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1724 size
= blk_getlength(blk
);
1726 printf("getlength: %s\n", strerror(-size
));
1730 cvtstr(size
, s1
, sizeof(s1
));
1736 static const cmdinfo_t length_cmd
= {
1740 .oneline
= "gets the length of the current file",
1744 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1746 BlockDriverState
*bs
= blk_bs(blk
);
1747 BlockDriverInfo bdi
;
1748 ImageInfoSpecific
*spec_info
;
1749 char s1
[64], s2
[64];
1752 if (bs
->drv
&& bs
->drv
->format_name
) {
1753 printf("format name: %s\n", bs
->drv
->format_name
);
1755 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1756 printf("format name: %s\n", bs
->drv
->protocol_name
);
1759 ret
= bdrv_get_info(bs
, &bdi
);
1764 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1765 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1767 printf("cluster size: %s\n", s1
);
1768 printf("vm state offset: %s\n", s2
);
1770 spec_info
= bdrv_get_specific_info(bs
);
1772 printf("Format specific information:\n");
1773 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1774 qapi_free_ImageInfoSpecific(spec_info
);
1782 static const cmdinfo_t info_cmd
= {
1786 .oneline
= "prints information about the current file",
1789 static void discard_help(void)
1793 " discards a range of bytes from the given offset\n"
1796 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1798 " Discards a segment of the currently open file.\n"
1799 " -C, -- report statistics in a machine parsable format\n"
1800 " -q, -- quiet mode, do not show I/O statistics\n"
1804 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1806 static const cmdinfo_t discard_cmd
= {
1812 .args
= "[-Cq] off len",
1813 .oneline
= "discards a number of bytes at a specified offset",
1814 .help
= discard_help
,
1817 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1819 struct timeval t1
, t2
;
1820 int Cflag
= 0, qflag
= 0;
1822 int64_t offset
, count
;
1824 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1833 return qemuio_command_usage(&discard_cmd
);
1837 if (optind
!= argc
- 2) {
1838 return qemuio_command_usage(&discard_cmd
);
1841 offset
= cvtnum(argv
[optind
]);
1843 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1848 count
= cvtnum(argv
[optind
]);
1850 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1852 } else if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
1853 printf("length cannot exceed %"PRIu64
", given %s\n",
1854 (uint64_t)INT_MAX
<< BDRV_SECTOR_BITS
,
1859 gettimeofday(&t1
, NULL
);
1860 ret
= blk_discard(blk
, offset
>> BDRV_SECTOR_BITS
,
1861 count
>> BDRV_SECTOR_BITS
);
1862 gettimeofday(&t2
, NULL
);
1865 printf("discard failed: %s\n", strerror(-ret
));
1869 /* Finally, report back -- -C gives a parsable format */
1872 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1879 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1881 BlockDriverState
*bs
= blk_bs(blk
);
1882 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1887 offset
= cvtnum(argv
[1]);
1889 printf("non-numeric offset argument -- %s\n", argv
[1]);
1891 } else if (offset
& 0x1ff) {
1892 printf("offset %" PRId64
" is not sector aligned\n",
1898 nb_sectors
= cvtnum(argv
[2]);
1899 if (nb_sectors
< 0) {
1900 printf("non-numeric length argument -- %s\n", argv
[2]);
1902 } else if (nb_sectors
> INT_MAX
) {
1903 printf("length argument cannot exceed %d, given %s\n",
1911 remaining
= nb_sectors
;
1913 sector_num
= offset
>> 9;
1915 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1917 printf("is_allocated failed: %s\n", strerror(-ret
));
1926 nb_sectors
-= remaining
;
1931 cvtstr(offset
, s1
, sizeof(s1
));
1933 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1934 sum_alloc
, nb_sectors
, s1
);
1938 static const cmdinfo_t alloc_cmd
= {
1944 .args
= "off [sectors]",
1945 .oneline
= "checks if a sector is present in the file",
1949 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1950 int64_t nb_sectors
, int64_t *pnum
)
1952 int num
, num_checked
;
1955 num_checked
= MIN(nb_sectors
, INT_MAX
);
1956 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1964 while (nb_sectors
> 0 && ret
== firstret
) {
1968 num_checked
= MIN(nb_sectors
, INT_MAX
);
1969 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1970 if (ret
== firstret
&& num
) {
1980 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1983 int64_t nb_sectors
, total_sectors
;
1990 total_sectors
= blk_nb_sectors(blk
);
1991 if (total_sectors
< 0) {
1992 error_report("Failed to query image length: %s",
1993 strerror(-total_sectors
));
1997 nb_sectors
= total_sectors
;
2000 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
2002 error_report("Failed to get allocation status: %s", strerror(-ret
));
2005 error_report("Unexpected end of image");
2009 retstr
= ret
? " allocated" : "not allocated";
2010 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
2011 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
2012 "at offset %s (%d)\n",
2013 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
2017 } while (offset
< total_sectors
);
2022 static const cmdinfo_t map_cmd
= {
2028 .oneline
= "prints the allocated areas of a file",
2031 static void reopen_help(void)
2035 " Changes the open options of an already opened image\n"
2038 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2040 " -r, -- Reopen the image read-only\n"
2041 " -c, -- Change the cache mode to the given value\n"
2042 " -o, -- Changes block driver options (cf. 'open' command)\n"
2046 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2048 static QemuOptsList reopen_opts
= {
2050 .merge_lists
= true,
2051 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2053 /* no elements => accept any params */
2054 { /* end of list */ }
2058 static const cmdinfo_t reopen_cmd
= {
2063 .args
= "[-r] [-c cache] [-o options]",
2064 .oneline
= "reopens an image with new options",
2065 .help
= reopen_help
,
2068 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2070 BlockDriverState
*bs
= blk_bs(blk
);
2074 int flags
= bs
->open_flags
;
2076 BlockReopenQueue
*brq
;
2077 Error
*local_err
= NULL
;
2079 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
2082 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
2083 error_report("Invalid cache option: %s", optarg
);
2088 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2089 qemu_opts_reset(&reopen_opts
);
2094 flags
&= ~BDRV_O_RDWR
;
2097 qemu_opts_reset(&reopen_opts
);
2098 return qemuio_command_usage(&reopen_cmd
);
2102 if (optind
!= argc
) {
2103 qemu_opts_reset(&reopen_opts
);
2104 return qemuio_command_usage(&reopen_cmd
);
2107 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2108 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2109 qemu_opts_reset(&reopen_opts
);
2111 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2112 bdrv_reopen_multiple(brq
, &local_err
);
2114 error_report_err(local_err
);
2120 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2124 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2126 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2132 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2136 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2138 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2144 static const cmdinfo_t break_cmd
= {
2149 .args
= "event tag",
2150 .oneline
= "sets a breakpoint on event and tags the stopped "
2154 static const cmdinfo_t remove_break_cmd
= {
2155 .name
= "remove_break",
2158 .cfunc
= remove_break_f
,
2160 .oneline
= "remove a breakpoint by tag",
2163 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2167 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2169 printf("Could not resume request: %s\n", strerror(-ret
));
2175 static const cmdinfo_t resume_cmd
= {
2181 .oneline
= "resumes the request tagged as tag",
2184 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2186 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2187 aio_poll(blk_get_aio_context(blk
), true);
2193 static const cmdinfo_t wait_break_cmd
= {
2194 .name
= "wait_break",
2197 .cfunc
= wait_break_f
,
2199 .oneline
= "waits for the suspension of a request",
2202 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2207 static const cmdinfo_t abort_cmd
= {
2210 .flags
= CMD_NOFILE_OK
,
2211 .oneline
= "simulate a program crash using abort(3)",
2214 static void sigraise_help(void)
2218 " raises the given signal\n"
2221 " 'sigraise %i' - raises SIGTERM\n"
2223 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2224 " given to sigraise.\n"
2228 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2230 static const cmdinfo_t sigraise_cmd
= {
2232 .cfunc
= sigraise_f
,
2235 .flags
= CMD_NOFILE_OK
,
2237 .oneline
= "raises a signal",
2238 .help
= sigraise_help
,
2241 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2243 int64_t sig
= cvtnum(argv
[1]);
2245 printf("non-numeric signal number argument -- %s\n", argv
[1]);
2247 } else if (sig
> NSIG
) {
2248 printf("signal argument '%s' is too large to be a valid signal\n",
2253 /* Using raise() to kill this process does not necessarily flush all open
2254 * streams. At least stdout and stderr (although the latter should be
2255 * non-buffered anyway) should be flushed, though. */
2263 static void sleep_cb(void *opaque
)
2265 bool *expired
= opaque
;
2269 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2273 struct QEMUTimer
*timer
;
2274 bool expired
= false;
2276 ms
= strtol(argv
[1], &endptr
, 0);
2277 if (ms
< 0 || *endptr
!= '\0') {
2278 printf("%s is not a valid number\n", argv
[1]);
2282 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2283 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2286 main_loop_wait(false);
2294 static const cmdinfo_t sleep_cmd
= {
2299 .flags
= CMD_NOFILE_OK
,
2300 .oneline
= "waits for the given value in milliseconds",
2303 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2308 printf("%s ", ct
->name
);
2310 printf("(or %s) ", ct
->altname
);
2315 printf("%s ", ct
->args
);
2317 printf("-- %s\n", ct
->oneline
);
2320 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2322 help_oneline(cmd
, ct
);
2328 static void help_all(void)
2330 const cmdinfo_t
*ct
;
2332 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2333 help_oneline(ct
->name
, ct
);
2335 printf("\nUse 'help commandname' for extended help.\n");
2338 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2340 const cmdinfo_t
*ct
;
2347 ct
= find_command(argv
[1]);
2349 printf("command %s not found\n", argv
[1]);
2353 help_onecmd(argv
[1], ct
);
2357 static const cmdinfo_t help_cmd
= {
2363 .flags
= CMD_FLAG_GLOBAL
,
2364 .args
= "[command]",
2365 .oneline
= "help for one or all commands",
2368 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2371 const cmdinfo_t
*ct
;
2376 input
= g_strdup(cmd
);
2377 v
= breakline(input
, &c
);
2379 ct
= find_command(v
[0]);
2381 done
= command(blk
, ct
, c
, v
);
2383 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2392 static void __attribute((constructor
)) init_qemuio_commands(void)
2394 /* initialize commands */
2395 qemuio_add_command(&help_cmd
);
2396 qemuio_add_command(&read_cmd
);
2397 qemuio_add_command(&readv_cmd
);
2398 qemuio_add_command(&write_cmd
);
2399 qemuio_add_command(&writev_cmd
);
2400 qemuio_add_command(&multiwrite_cmd
);
2401 qemuio_add_command(&aio_read_cmd
);
2402 qemuio_add_command(&aio_write_cmd
);
2403 qemuio_add_command(&aio_flush_cmd
);
2404 qemuio_add_command(&flush_cmd
);
2405 qemuio_add_command(&truncate_cmd
);
2406 qemuio_add_command(&length_cmd
);
2407 qemuio_add_command(&info_cmd
);
2408 qemuio_add_command(&discard_cmd
);
2409 qemuio_add_command(&alloc_cmd
);
2410 qemuio_add_command(&map_cmd
);
2411 qemuio_add_command(&reopen_cmd
);
2412 qemuio_add_command(&break_cmd
);
2413 qemuio_add_command(&remove_break_cmd
);
2414 qemuio_add_command(&resume_cmd
);
2415 qemuio_add_command(&wait_break_cmd
);
2416 qemuio_add_command(&abort_cmd
);
2417 qemuio_add_command(&sleep_cmd
);
2418 qemuio_add_command(&sigraise_cmd
);