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
)
141 ret
= qemu_strtosz_suffix(s
, &end
, QEMU_STRTOSZ_DEFSUFFIX_B
);
143 /* Detritus at the end of the string */
149 static void print_cvtnum_err(int64_t rc
, const char *arg
)
153 printf("Parsing error: non-numeric argument,"
154 " or extraneous/unrecognized suffix -- %s\n", arg
);
157 printf("Parsing error: argument too large -- %s\n", arg
);
160 printf("Parsing error: %s\n", arg
);
164 #define EXABYTES(x) ((long long)(x) << 60)
165 #define PETABYTES(x) ((long long)(x) << 50)
166 #define TERABYTES(x) ((long long)(x) << 40)
167 #define GIGABYTES(x) ((long long)(x) << 30)
168 #define MEGABYTES(x) ((long long)(x) << 20)
169 #define KILOBYTES(x) ((long long)(x) << 10)
171 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
172 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
173 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
174 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
175 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
176 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
178 static void cvtstr(double value
, char *str
, size_t size
)
183 if (value
>= EXABYTES(1)) {
185 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
186 } else if (value
>= PETABYTES(1)) {
188 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
189 } else if (value
>= TERABYTES(1)) {
191 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
192 } else if (value
>= GIGABYTES(1)) {
194 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
195 } else if (value
>= MEGABYTES(1)) {
197 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
198 } else if (value
>= KILOBYTES(1)) {
200 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
203 snprintf(str
, size
- 6, "%f", value
);
206 trim
= strstr(str
, ".000");
208 strcpy(trim
, suffix
);
216 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
218 t1
.tv_usec
-= t2
.tv_usec
;
219 if (t1
.tv_usec
< 0) {
220 t1
.tv_usec
+= 1000000;
223 t1
.tv_sec
-= t2
.tv_sec
;
227 static double tdiv(double value
, struct timeval tv
)
229 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
232 #define HOURS(sec) ((sec) / (60 * 60))
233 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
234 #define SECONDS(sec) ((sec) % 60)
238 TERSE_FIXED_TIME
= 0x1,
239 VERBOSE_FIXED_TIME
= 0x2,
242 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
244 double usec
= (double)tv
->tv_usec
/ 1000000.0;
246 if (format
& TERSE_FIXED_TIME
) {
247 if (!HOURS(tv
->tv_sec
)) {
248 snprintf(ts
, size
, "%u:%02u.%02u",
249 (unsigned int) MINUTES(tv
->tv_sec
),
250 (unsigned int) SECONDS(tv
->tv_sec
),
251 (unsigned int) (usec
* 100));
254 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
257 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
258 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
259 (unsigned int) HOURS(tv
->tv_sec
),
260 (unsigned int) MINUTES(tv
->tv_sec
),
261 (unsigned int) SECONDS(tv
->tv_sec
),
262 (unsigned int) (usec
* 100));
264 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
269 * Parse the pattern argument to various sub-commands.
271 * Because the pattern is used as an argument to memset it must evaluate
272 * to an unsigned integer that fits into a single byte.
274 static int parse_pattern(const char *arg
)
279 pattern
= strtol(arg
, &endptr
, 0);
280 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
281 printf("%s is not a valid pattern byte\n", arg
);
289 * Memory allocation helpers.
291 * Make sure memory is aligned by default, or purposefully misaligned if
292 * that is specified on the command line.
295 #define MISALIGN_OFFSET 16
296 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
300 if (qemuio_misalign
) {
301 len
+= MISALIGN_OFFSET
;
303 buf
= blk_blockalign(blk
, len
);
304 memset(buf
, pattern
, len
);
305 if (qemuio_misalign
) {
306 buf
+= MISALIGN_OFFSET
;
311 static void qemu_io_free(void *p
)
313 if (qemuio_misalign
) {
314 p
-= MISALIGN_OFFSET
;
319 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
325 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
326 const uint8_t *s
= p
;
328 printf("%08" PRIx64
": ", offset
+ i
);
329 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
333 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
344 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
345 int64_t count
, int64_t total
, int cnt
, int Cflag
)
347 char s1
[64], s2
[64], ts
[64];
349 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
351 cvtstr((double)total
, s1
, sizeof(s1
));
352 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
353 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
354 op
, total
, count
, offset
);
355 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
356 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
357 } else {/* bytes,ops,time,bytes/sec,ops/sec */
358 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
360 tdiv((double)total
, *t
),
361 tdiv((double)cnt
, *t
));
366 * Parse multiple length statements for vectored I/O, and construct an I/O
367 * vector matching it.
370 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
373 size_t *sizes
= g_new0(size_t, nr_iov
);
379 for (i
= 0; i
< nr_iov
; i
++) {
385 print_cvtnum_err(len
, arg
);
389 /* should be SIZE_T_MAX, but that doesn't exist */
391 printf("Argument '%s' exceeds maximum size %d\n", arg
, INT_MAX
);
396 printf("length argument %" PRId64
397 " is not sector aligned\n", len
);
405 qemu_iovec_init(qiov
, nr_iov
);
407 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
409 for (i
= 0; i
< nr_iov
; i
++) {
410 qemu_iovec_add(qiov
, p
, sizes
[i
]);
419 static int do_read(BlockBackend
*blk
, char *buf
, int64_t offset
, int64_t count
,
424 if (count
>> 9 > INT_MAX
) {
428 ret
= blk_read(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
436 static int do_write(BlockBackend
*blk
, char *buf
, int64_t offset
, int64_t count
,
441 if (count
>> 9 > INT_MAX
) {
445 ret
= blk_write(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
453 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
454 int64_t count
, int64_t *total
)
456 if (count
> INT_MAX
) {
460 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
467 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
468 int64_t count
, int64_t *total
)
470 if (count
> INT_MAX
) {
474 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
);
490 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
492 CoWriteZeroes
*data
= opaque
;
494 data
->ret
= blk_co_write_zeroes(data
->blk
, data
->offset
/ BDRV_SECTOR_SIZE
,
495 data
->count
/ BDRV_SECTOR_SIZE
, 0);
498 *data
->total
= data
->ret
;
502 *data
->total
= data
->count
;
505 static int do_co_write_zeroes(BlockBackend
*blk
, int64_t offset
, int64_t count
,
509 CoWriteZeroes data
= {
517 if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
521 co
= qemu_coroutine_create(co_write_zeroes_entry
);
522 qemu_coroutine_enter(co
, &data
);
524 aio_poll(blk_get_aio_context(blk
), true);
533 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
534 int64_t count
, int64_t *total
)
538 if (count
>> 9 > INT_MAX
) {
542 ret
= blk_write_compressed(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
550 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
551 int64_t count
, int64_t *total
)
553 if (count
> INT_MAX
) {
557 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
564 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
565 int64_t count
, int64_t *total
)
567 if (count
> INT_MAX
) {
571 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
578 #define NOT_DONE 0x7fffffff
579 static void aio_rw_done(void *opaque
, int ret
)
581 *(int *)opaque
= ret
;
584 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
585 int64_t offset
, int *total
)
587 int async_ret
= NOT_DONE
;
589 blk_aio_readv(blk
, offset
>> 9, qiov
, qiov
->size
>> 9,
590 aio_rw_done
, &async_ret
);
591 while (async_ret
== NOT_DONE
) {
592 main_loop_wait(false);
596 return async_ret
< 0 ? async_ret
: 1;
599 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
600 int64_t offset
, int *total
)
602 int async_ret
= NOT_DONE
;
604 blk_aio_writev(blk
, offset
>> 9, qiov
, qiov
->size
>> 9,
605 aio_rw_done
, &async_ret
);
606 while (async_ret
== NOT_DONE
) {
607 main_loop_wait(false);
611 return async_ret
< 0 ? async_ret
: 1;
614 struct multiwrite_async_ret
{
619 static void multiwrite_cb(void *opaque
, int ret
)
621 struct multiwrite_async_ret
*async_ret
= opaque
;
623 async_ret
->num_done
++;
625 async_ret
->error
= ret
;
629 static int do_aio_multiwrite(BlockBackend
*blk
, BlockRequest
* reqs
,
630 int num_reqs
, int *total
)
633 struct multiwrite_async_ret async_ret
= {
639 for (i
= 0; i
< num_reqs
; i
++) {
640 reqs
[i
].cb
= multiwrite_cb
;
641 reqs
[i
].opaque
= &async_ret
;
642 *total
+= reqs
[i
].qiov
->size
;
645 ret
= blk_aio_multiwrite(blk
, reqs
, num_reqs
);
650 while (async_ret
.num_done
< num_reqs
) {
651 main_loop_wait(false);
654 return async_ret
.error
< 0 ? async_ret
.error
: 1;
657 static void read_help(void)
661 " reads a range of bytes from the given offset\n"
664 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
666 " Reads a segment of the currently open file, optionally dumping it to the\n"
667 " standard output stream (with -v option) for subsequent inspection.\n"
668 " -b, -- read from the VM state rather than the virtual disk\n"
669 " -C, -- report statistics in a machine parsable format\n"
670 " -l, -- length for pattern verification (only with -P)\n"
671 " -p, -- use blk_pread to read the file\n"
672 " -P, -- use a pattern to verify read data\n"
673 " -q, -- quiet mode, do not show I/O statistics\n"
674 " -s, -- start offset for pattern verification (only with -P)\n"
675 " -v, -- dump buffer to standard output\n"
679 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
681 static const cmdinfo_t read_cmd
= {
687 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
688 .oneline
= "reads a number of bytes at a specified offset",
692 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
694 struct timeval t1
, t2
;
695 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
696 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
701 /* Some compilers get confused and warn if this is not initialized. */
704 int64_t pattern_offset
= 0, pattern_count
= 0;
706 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
716 pattern_count
= cvtnum(optarg
);
717 if (pattern_count
< 0) {
718 print_cvtnum_err(pattern_count
, optarg
);
727 pattern
= parse_pattern(optarg
);
737 pattern_offset
= cvtnum(optarg
);
738 if (pattern_offset
< 0) {
739 print_cvtnum_err(pattern_offset
, optarg
);
747 return qemuio_command_usage(&read_cmd
);
751 if (optind
!= argc
- 2) {
752 return qemuio_command_usage(&read_cmd
);
755 if (bflag
&& pflag
) {
756 printf("-b and -p cannot be specified at the same time\n");
760 offset
= cvtnum(argv
[optind
]);
762 print_cvtnum_err(offset
, argv
[optind
]);
767 count
= cvtnum(argv
[optind
]);
769 print_cvtnum_err(count
, argv
[optind
]);
771 } else if (count
> SIZE_MAX
) {
772 printf("length cannot exceed %" PRIu64
", given %s\n",
773 (uint64_t) SIZE_MAX
, argv
[optind
]);
777 if (!Pflag
&& (lflag
|| sflag
)) {
778 return qemuio_command_usage(&read_cmd
);
782 pattern_count
= count
- pattern_offset
;
785 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
786 printf("pattern verification range exceeds end of read data\n");
791 if (offset
& 0x1ff) {
792 printf("offset %" PRId64
" is not sector aligned\n",
797 printf("count %"PRId64
" is not sector aligned\n",
803 buf
= qemu_io_alloc(blk
, count
, 0xab);
805 gettimeofday(&t1
, NULL
);
807 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
809 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
811 cnt
= do_read(blk
, buf
, offset
, count
, &total
);
813 gettimeofday(&t2
, NULL
);
816 printf("read failed: %s\n", strerror(-cnt
));
821 void *cmp_buf
= g_malloc(pattern_count
);
822 memset(cmp_buf
, pattern
, pattern_count
);
823 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
824 printf("Pattern verification failed at offset %"
825 PRId64
", %"PRId64
" bytes\n",
826 offset
+ pattern_offset
, pattern_count
);
836 dump_buffer(buf
, offset
, count
);
839 /* Finally, report back -- -C gives a parsable format */
841 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
849 static void readv_help(void)
853 " reads a range of bytes from the given offset into multiple buffers\n"
856 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
858 " Reads a segment of the currently open file, optionally dumping it to the\n"
859 " standard output stream (with -v option) for subsequent inspection.\n"
860 " Uses multiple iovec buffers if more than one byte range is specified.\n"
861 " -C, -- report statistics in a machine parsable format\n"
862 " -P, -- use a pattern to verify read data\n"
863 " -v, -- dump buffer to standard output\n"
864 " -q, -- quiet mode, do not show I/O statistics\n"
868 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
870 static const cmdinfo_t readv_cmd
= {
875 .args
= "[-Cqv] [-P pattern ] off len [len..]",
876 .oneline
= "reads a number of bytes at a specified offset",
880 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
882 struct timeval t1
, t2
;
883 int Cflag
= 0, qflag
= 0, vflag
= 0;
887 /* Some compilers get confused and warn if this is not initialized. */
894 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
901 pattern
= parse_pattern(optarg
);
913 return qemuio_command_usage(&readv_cmd
);
917 if (optind
> argc
- 2) {
918 return qemuio_command_usage(&readv_cmd
);
922 offset
= cvtnum(argv
[optind
]);
924 print_cvtnum_err(offset
, argv
[optind
]);
929 if (offset
& 0x1ff) {
930 printf("offset %" PRId64
" is not sector aligned\n",
935 nr_iov
= argc
- optind
;
936 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
941 gettimeofday(&t1
, NULL
);
942 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
943 gettimeofday(&t2
, NULL
);
946 printf("readv failed: %s\n", strerror(-cnt
));
951 void *cmp_buf
= g_malloc(qiov
.size
);
952 memset(cmp_buf
, pattern
, qiov
.size
);
953 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
954 printf("Pattern verification failed at offset %"
955 PRId64
", %zd bytes\n", offset
, qiov
.size
);
965 dump_buffer(buf
, offset
, qiov
.size
);
968 /* Finally, report back -- -C gives a parsable format */
970 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
973 qemu_iovec_destroy(&qiov
);
978 static void write_help(void)
982 " writes a range of bytes from the given offset\n"
985 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
987 " Writes into a segment of the currently open file, using a buffer\n"
988 " filled with a set pattern (0xcdcdcdcd).\n"
989 " -b, -- write to the VM state rather than the virtual disk\n"
990 " -c, -- write compressed data with blk_write_compressed\n"
991 " -p, -- use blk_pwrite to write the file\n"
992 " -P, -- use different pattern to fill file\n"
993 " -C, -- report statistics in a machine parsable format\n"
994 " -q, -- quiet mode, do not show I/O statistics\n"
995 " -z, -- write zeroes using blk_co_write_zeroes\n"
999 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
1001 static const cmdinfo_t write_cmd
= {
1007 .args
= "[-bcCpqz] [-P pattern ] off len",
1008 .oneline
= "writes a number of bytes at a specified offset",
1012 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
1014 struct timeval t1
, t2
;
1015 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
1021 /* Some compilers get confused and warn if this is not initialized. */
1025 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != -1) {
1041 pattern
= parse_pattern(optarg
);
1053 return qemuio_command_usage(&write_cmd
);
1057 if (optind
!= argc
- 2) {
1058 return qemuio_command_usage(&write_cmd
);
1061 if (bflag
+ pflag
+ zflag
> 1) {
1062 printf("-b, -p, or -z cannot be specified at the same time\n");
1066 if (zflag
&& Pflag
) {
1067 printf("-z and -P cannot be specified at the same time\n");
1071 offset
= cvtnum(argv
[optind
]);
1073 print_cvtnum_err(offset
, argv
[optind
]);
1078 count
= cvtnum(argv
[optind
]);
1080 print_cvtnum_err(count
, argv
[optind
]);
1082 } else if (count
> SIZE_MAX
) {
1083 printf("length cannot exceed %" PRIu64
", given %s\n",
1084 (uint64_t) SIZE_MAX
, argv
[optind
]);
1089 if (offset
& 0x1ff) {
1090 printf("offset %" PRId64
" is not sector aligned\n",
1095 if (count
& 0x1ff) {
1096 printf("count %"PRId64
" is not sector aligned\n",
1103 buf
= qemu_io_alloc(blk
, count
, pattern
);
1106 gettimeofday(&t1
, NULL
);
1108 cnt
= do_pwrite(blk
, buf
, offset
, count
, &total
);
1110 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1112 cnt
= do_co_write_zeroes(blk
, offset
, count
, &total
);
1114 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1116 cnt
= do_write(blk
, buf
, offset
, count
, &total
);
1118 gettimeofday(&t2
, NULL
);
1121 printf("write failed: %s\n", strerror(-cnt
));
1129 /* Finally, report back -- -C gives a parsable format */
1131 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1146 " writes a range of bytes from the given offset source from multiple buffers\n"
1149 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1151 " Writes into a segment of the currently open file, using a buffer\n"
1152 " filled with a set pattern (0xcdcdcdcd).\n"
1153 " -P, -- use different pattern to fill file\n"
1154 " -C, -- report statistics in a machine parsable format\n"
1155 " -q, -- quiet mode, do not show I/O statistics\n"
1159 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1161 static const cmdinfo_t writev_cmd
= {
1166 .args
= "[-Cq] [-P pattern ] off len [len..]",
1167 .oneline
= "writes a number of bytes at a specified offset",
1168 .help
= writev_help
,
1171 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1173 struct timeval t1
, t2
;
1174 int Cflag
= 0, qflag
= 0;
1178 /* Some compilers get confused and warn if this is not initialized. */
1184 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1193 pattern
= parse_pattern(optarg
);
1199 return qemuio_command_usage(&writev_cmd
);
1203 if (optind
> argc
- 2) {
1204 return qemuio_command_usage(&writev_cmd
);
1207 offset
= cvtnum(argv
[optind
]);
1209 print_cvtnum_err(offset
, argv
[optind
]);
1214 if (offset
& 0x1ff) {
1215 printf("offset %" PRId64
" is not sector aligned\n",
1220 nr_iov
= argc
- optind
;
1221 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1226 gettimeofday(&t1
, NULL
);
1227 cnt
= do_aio_writev(blk
, &qiov
, offset
, &total
);
1228 gettimeofday(&t2
, NULL
);
1231 printf("writev failed: %s\n", strerror(-cnt
));
1239 /* Finally, report back -- -C gives a parsable format */
1241 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1243 qemu_iovec_destroy(&qiov
);
1248 static void multiwrite_help(void)
1252 " writes a range of bytes from the given offset source from multiple buffers,\n"
1253 " in a batch of requests that may be merged by qemu\n"
1256 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1257 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1259 " Writes into a segment of the currently open file, using a buffer\n"
1260 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1261 " by one for each request contained in the multiwrite command.\n"
1262 " -P, -- use different pattern to fill file\n"
1263 " -C, -- report statistics in a machine parsable format\n"
1264 " -q, -- quiet mode, do not show I/O statistics\n"
1268 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
);
1270 static const cmdinfo_t multiwrite_cmd
= {
1271 .name
= "multiwrite",
1272 .cfunc
= multiwrite_f
,
1275 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1276 .oneline
= "issues multiple write requests at once",
1277 .help
= multiwrite_help
,
1280 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
)
1282 struct timeval t1
, t2
;
1283 int Cflag
= 0, qflag
= 0;
1286 int64_t offset
, first_offset
= 0;
1287 /* Some compilers get confused and warn if this is not initialized. */
1292 QEMUIOVector
*qiovs
;
1296 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1305 pattern
= parse_pattern(optarg
);
1311 return qemuio_command_usage(&writev_cmd
);
1315 if (optind
> argc
- 2) {
1316 return qemuio_command_usage(&writev_cmd
);
1320 for (i
= optind
; i
< argc
; i
++) {
1321 if (!strcmp(argv
[i
], ";")) {
1326 reqs
= g_new0(BlockRequest
, nr_reqs
);
1327 buf
= g_new0(char *, nr_reqs
);
1328 qiovs
= g_new(QEMUIOVector
, nr_reqs
);
1330 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1333 /* Read the offset of the request */
1334 offset
= cvtnum(argv
[optind
]);
1336 print_cvtnum_err(offset
, argv
[optind
]);
1341 if (offset
& 0x1ff) {
1342 printf("offset %lld is not sector aligned\n",
1348 first_offset
= offset
;
1351 /* Read lengths for qiov entries */
1352 for (j
= optind
; j
< argc
; j
++) {
1353 if (!strcmp(argv
[j
], ";")) {
1358 nr_iov
= j
- optind
;
1361 buf
[i
] = create_iovec(blk
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1362 if (buf
[i
] == NULL
) {
1366 reqs
[i
].qiov
= &qiovs
[i
];
1367 reqs
[i
].sector
= offset
>> 9;
1368 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1375 /* If there were empty requests at the end, ignore them */
1378 gettimeofday(&t1
, NULL
);
1379 cnt
= do_aio_multiwrite(blk
, reqs
, nr_reqs
, &total
);
1380 gettimeofday(&t2
, NULL
);
1383 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1391 /* Finally, report back -- -C gives a parsable format */
1393 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1395 for (i
= 0; i
< nr_reqs
; i
++) {
1396 qemu_io_free(buf
[i
]);
1397 if (reqs
[i
].qiov
!= NULL
) {
1398 qemu_iovec_destroy(&qiovs
[i
]);
1416 BlockAcctCookie acct
;
1421 static void aio_write_done(void *opaque
, int ret
)
1423 struct aio_ctx
*ctx
= opaque
;
1426 gettimeofday(&t2
, NULL
);
1430 printf("aio_write failed: %s\n", strerror(-ret
));
1431 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1435 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1441 /* Finally, report back -- -C gives a parsable format */
1442 t2
= tsub(t2
, ctx
->t1
);
1443 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1444 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1446 qemu_io_free(ctx
->buf
);
1447 qemu_iovec_destroy(&ctx
->qiov
);
1451 static void aio_read_done(void *opaque
, int ret
)
1453 struct aio_ctx
*ctx
= opaque
;
1456 gettimeofday(&t2
, NULL
);
1459 printf("readv failed: %s\n", strerror(-ret
));
1460 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1465 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1467 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1468 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1469 printf("Pattern verification failed at offset %"
1470 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1475 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1482 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1485 /* Finally, report back -- -C gives a parsable format */
1486 t2
= tsub(t2
, ctx
->t1
);
1487 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1488 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1490 qemu_io_free(ctx
->buf
);
1491 qemu_iovec_destroy(&ctx
->qiov
);
1495 static void aio_read_help(void)
1499 " asynchronously reads a range of bytes from the given offset\n"
1502 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1504 " Reads a segment of the currently open file, optionally dumping it to the\n"
1505 " standard output stream (with -v option) for subsequent inspection.\n"
1506 " The read is performed asynchronously and the aio_flush command must be\n"
1507 " used to ensure all outstanding aio requests have been completed.\n"
1508 " -C, -- report statistics in a machine parsable format\n"
1509 " -P, -- use a pattern to verify read data\n"
1510 " -v, -- dump buffer to standard output\n"
1511 " -q, -- quiet mode, do not show I/O statistics\n"
1515 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1517 static const cmdinfo_t aio_read_cmd
= {
1519 .cfunc
= aio_read_f
,
1522 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1523 .oneline
= "asynchronously reads a number of bytes",
1524 .help
= aio_read_help
,
1527 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1530 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1533 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
1540 ctx
->pattern
= parse_pattern(optarg
);
1541 if (ctx
->pattern
< 0) {
1554 return qemuio_command_usage(&aio_read_cmd
);
1558 if (optind
> argc
- 2) {
1560 return qemuio_command_usage(&aio_read_cmd
);
1563 ctx
->offset
= cvtnum(argv
[optind
]);
1564 if (ctx
->offset
< 0) {
1565 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1571 if (ctx
->offset
& 0x1ff) {
1572 printf("offset %" PRId64
" is not sector aligned\n",
1574 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1579 nr_iov
= argc
- optind
;
1580 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1581 if (ctx
->buf
== NULL
) {
1582 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1587 gettimeofday(&ctx
->t1
, NULL
);
1588 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1590 blk_aio_readv(blk
, ctx
->offset
>> 9, &ctx
->qiov
,
1591 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1595 static void aio_write_help(void)
1599 " asynchronously writes a range of bytes from the given offset source\n"
1600 " from multiple buffers\n"
1603 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1605 " Writes into a segment of the currently open file, using a buffer\n"
1606 " filled with a set pattern (0xcdcdcdcd).\n"
1607 " The write is performed asynchronously and the aio_flush command must be\n"
1608 " used to ensure all outstanding aio requests have been completed.\n"
1609 " -P, -- use different pattern to fill file\n"
1610 " -C, -- report statistics in a machine parsable format\n"
1611 " -q, -- quiet mode, do not show I/O statistics\n"
1615 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1617 static const cmdinfo_t aio_write_cmd
= {
1618 .name
= "aio_write",
1619 .cfunc
= aio_write_f
,
1622 .args
= "[-Cq] [-P pattern ] off len [len..]",
1623 .oneline
= "asynchronously writes a number of bytes",
1624 .help
= aio_write_help
,
1627 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1631 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1634 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1643 pattern
= parse_pattern(optarg
);
1651 return qemuio_command_usage(&aio_write_cmd
);
1655 if (optind
> argc
- 2) {
1657 return qemuio_command_usage(&aio_write_cmd
);
1660 ctx
->offset
= cvtnum(argv
[optind
]);
1661 if (ctx
->offset
< 0) {
1662 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1668 if (ctx
->offset
& 0x1ff) {
1669 printf("offset %" PRId64
" is not sector aligned\n",
1671 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1676 nr_iov
= argc
- optind
;
1677 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1678 if (ctx
->buf
== NULL
) {
1679 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1684 gettimeofday(&ctx
->t1
, NULL
);
1685 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1687 blk_aio_writev(blk
, ctx
->offset
>> 9, &ctx
->qiov
,
1688 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1692 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1694 BlockAcctCookie cookie
;
1695 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1697 block_acct_done(blk_get_stats(blk
), &cookie
);
1701 static const cmdinfo_t aio_flush_cmd
= {
1702 .name
= "aio_flush",
1703 .cfunc
= aio_flush_f
,
1704 .oneline
= "completes all outstanding aio requests"
1707 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1713 static const cmdinfo_t flush_cmd
= {
1717 .oneline
= "flush all in-core file state to disk",
1720 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1725 offset
= cvtnum(argv
[1]);
1727 print_cvtnum_err(offset
, argv
[1]);
1731 ret
= blk_truncate(blk
, offset
);
1733 printf("truncate: %s\n", strerror(-ret
));
1740 static const cmdinfo_t truncate_cmd
= {
1743 .cfunc
= truncate_f
,
1747 .oneline
= "truncates the current file at the given offset",
1750 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1755 size
= blk_getlength(blk
);
1757 printf("getlength: %s\n", strerror(-size
));
1761 cvtstr(size
, s1
, sizeof(s1
));
1767 static const cmdinfo_t length_cmd
= {
1771 .oneline
= "gets the length of the current file",
1775 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1777 BlockDriverState
*bs
= blk_bs(blk
);
1778 BlockDriverInfo bdi
;
1779 ImageInfoSpecific
*spec_info
;
1780 char s1
[64], s2
[64];
1783 if (bs
->drv
&& bs
->drv
->format_name
) {
1784 printf("format name: %s\n", bs
->drv
->format_name
);
1786 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1787 printf("format name: %s\n", bs
->drv
->protocol_name
);
1790 ret
= bdrv_get_info(bs
, &bdi
);
1795 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1796 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1798 printf("cluster size: %s\n", s1
);
1799 printf("vm state offset: %s\n", s2
);
1801 spec_info
= bdrv_get_specific_info(bs
);
1803 printf("Format specific information:\n");
1804 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1805 qapi_free_ImageInfoSpecific(spec_info
);
1813 static const cmdinfo_t info_cmd
= {
1817 .oneline
= "prints information about the current file",
1820 static void discard_help(void)
1824 " discards a range of bytes from the given offset\n"
1827 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1829 " Discards a segment of the currently open file.\n"
1830 " -C, -- report statistics in a machine parsable format\n"
1831 " -q, -- quiet mode, do not show I/O statistics\n"
1835 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1837 static const cmdinfo_t discard_cmd
= {
1843 .args
= "[-Cq] off len",
1844 .oneline
= "discards a number of bytes at a specified offset",
1845 .help
= discard_help
,
1848 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1850 struct timeval t1
, t2
;
1851 int Cflag
= 0, qflag
= 0;
1853 int64_t offset
, count
;
1855 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1864 return qemuio_command_usage(&discard_cmd
);
1868 if (optind
!= argc
- 2) {
1869 return qemuio_command_usage(&discard_cmd
);
1872 offset
= cvtnum(argv
[optind
]);
1874 print_cvtnum_err(offset
, argv
[optind
]);
1879 count
= cvtnum(argv
[optind
]);
1881 print_cvtnum_err(count
, argv
[optind
]);
1883 } else if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
1884 printf("length cannot exceed %"PRIu64
", given %s\n",
1885 (uint64_t)INT_MAX
<< BDRV_SECTOR_BITS
,
1890 gettimeofday(&t1
, NULL
);
1891 ret
= blk_discard(blk
, offset
>> BDRV_SECTOR_BITS
,
1892 count
>> BDRV_SECTOR_BITS
);
1893 gettimeofday(&t2
, NULL
);
1896 printf("discard failed: %s\n", strerror(-ret
));
1900 /* Finally, report back -- -C gives a parsable format */
1903 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1910 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1912 BlockDriverState
*bs
= blk_bs(blk
);
1913 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1918 offset
= cvtnum(argv
[1]);
1920 print_cvtnum_err(offset
, argv
[1]);
1922 } else if (offset
& 0x1ff) {
1923 printf("offset %" PRId64
" is not sector aligned\n",
1929 nb_sectors
= cvtnum(argv
[2]);
1930 if (nb_sectors
< 0) {
1931 print_cvtnum_err(nb_sectors
, argv
[2]);
1933 } else if (nb_sectors
> INT_MAX
) {
1934 printf("length argument cannot exceed %d, given %s\n",
1942 remaining
= nb_sectors
;
1944 sector_num
= offset
>> 9;
1946 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1948 printf("is_allocated failed: %s\n", strerror(-ret
));
1957 nb_sectors
-= remaining
;
1962 cvtstr(offset
, s1
, sizeof(s1
));
1964 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1965 sum_alloc
, nb_sectors
, s1
);
1969 static const cmdinfo_t alloc_cmd
= {
1975 .args
= "off [sectors]",
1976 .oneline
= "checks if a sector is present in the file",
1980 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1981 int64_t nb_sectors
, int64_t *pnum
)
1983 int num
, num_checked
;
1986 num_checked
= MIN(nb_sectors
, INT_MAX
);
1987 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1995 while (nb_sectors
> 0 && ret
== firstret
) {
1999 num_checked
= MIN(nb_sectors
, INT_MAX
);
2000 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
2001 if (ret
== firstret
&& num
) {
2011 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
2014 int64_t nb_sectors
, total_sectors
;
2021 total_sectors
= blk_nb_sectors(blk
);
2022 if (total_sectors
< 0) {
2023 error_report("Failed to query image length: %s",
2024 strerror(-total_sectors
));
2028 nb_sectors
= total_sectors
;
2031 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
2033 error_report("Failed to get allocation status: %s", strerror(-ret
));
2036 error_report("Unexpected end of image");
2040 retstr
= ret
? " allocated" : "not allocated";
2041 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
2042 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
2043 "at offset %s (%d)\n",
2044 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
2048 } while (offset
< total_sectors
);
2053 static const cmdinfo_t map_cmd
= {
2059 .oneline
= "prints the allocated areas of a file",
2062 static void reopen_help(void)
2066 " Changes the open options of an already opened image\n"
2069 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2071 " -r, -- Reopen the image read-only\n"
2072 " -c, -- Change the cache mode to the given value\n"
2073 " -o, -- Changes block driver options (cf. 'open' command)\n"
2077 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2079 static QemuOptsList reopen_opts
= {
2081 .merge_lists
= true,
2082 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2084 /* no elements => accept any params */
2085 { /* end of list */ }
2089 static const cmdinfo_t reopen_cmd
= {
2094 .args
= "[-r] [-c cache] [-o options]",
2095 .oneline
= "reopens an image with new options",
2096 .help
= reopen_help
,
2099 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2101 BlockDriverState
*bs
= blk_bs(blk
);
2105 int flags
= bs
->open_flags
;
2107 BlockReopenQueue
*brq
;
2108 Error
*local_err
= NULL
;
2110 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
2113 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
2114 error_report("Invalid cache option: %s", optarg
);
2119 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2120 qemu_opts_reset(&reopen_opts
);
2125 flags
&= ~BDRV_O_RDWR
;
2128 qemu_opts_reset(&reopen_opts
);
2129 return qemuio_command_usage(&reopen_cmd
);
2133 if (optind
!= argc
) {
2134 qemu_opts_reset(&reopen_opts
);
2135 return qemuio_command_usage(&reopen_cmd
);
2138 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2139 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2140 qemu_opts_reset(&reopen_opts
);
2142 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2143 bdrv_reopen_multiple(brq
, &local_err
);
2145 error_report_err(local_err
);
2151 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2155 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2157 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2163 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2167 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2169 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2175 static const cmdinfo_t break_cmd
= {
2180 .args
= "event tag",
2181 .oneline
= "sets a breakpoint on event and tags the stopped "
2185 static const cmdinfo_t remove_break_cmd
= {
2186 .name
= "remove_break",
2189 .cfunc
= remove_break_f
,
2191 .oneline
= "remove a breakpoint by tag",
2194 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2198 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2200 printf("Could not resume request: %s\n", strerror(-ret
));
2206 static const cmdinfo_t resume_cmd
= {
2212 .oneline
= "resumes the request tagged as tag",
2215 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2217 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2218 aio_poll(blk_get_aio_context(blk
), true);
2224 static const cmdinfo_t wait_break_cmd
= {
2225 .name
= "wait_break",
2228 .cfunc
= wait_break_f
,
2230 .oneline
= "waits for the suspension of a request",
2233 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2238 static const cmdinfo_t abort_cmd
= {
2241 .flags
= CMD_NOFILE_OK
,
2242 .oneline
= "simulate a program crash using abort(3)",
2245 static void sigraise_help(void)
2249 " raises the given signal\n"
2252 " 'sigraise %i' - raises SIGTERM\n"
2254 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2255 " given to sigraise.\n"
2259 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2261 static const cmdinfo_t sigraise_cmd
= {
2263 .cfunc
= sigraise_f
,
2266 .flags
= CMD_NOFILE_OK
,
2268 .oneline
= "raises a signal",
2269 .help
= sigraise_help
,
2272 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2274 int64_t sig
= cvtnum(argv
[1]);
2276 print_cvtnum_err(sig
, argv
[1]);
2278 } else if (sig
> NSIG
) {
2279 printf("signal argument '%s' is too large to be a valid signal\n",
2284 /* Using raise() to kill this process does not necessarily flush all open
2285 * streams. At least stdout and stderr (although the latter should be
2286 * non-buffered anyway) should be flushed, though. */
2294 static void sleep_cb(void *opaque
)
2296 bool *expired
= opaque
;
2300 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2304 struct QEMUTimer
*timer
;
2305 bool expired
= false;
2307 ms
= strtol(argv
[1], &endptr
, 0);
2308 if (ms
< 0 || *endptr
!= '\0') {
2309 printf("%s is not a valid number\n", argv
[1]);
2313 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2314 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2317 main_loop_wait(false);
2325 static const cmdinfo_t sleep_cmd
= {
2330 .flags
= CMD_NOFILE_OK
,
2331 .oneline
= "waits for the given value in milliseconds",
2334 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2339 printf("%s ", ct
->name
);
2341 printf("(or %s) ", ct
->altname
);
2346 printf("%s ", ct
->args
);
2348 printf("-- %s\n", ct
->oneline
);
2351 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2353 help_oneline(cmd
, ct
);
2359 static void help_all(void)
2361 const cmdinfo_t
*ct
;
2363 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2364 help_oneline(ct
->name
, ct
);
2366 printf("\nUse 'help commandname' for extended help.\n");
2369 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2371 const cmdinfo_t
*ct
;
2378 ct
= find_command(argv
[1]);
2380 printf("command %s not found\n", argv
[1]);
2384 help_onecmd(argv
[1], ct
);
2388 static const cmdinfo_t help_cmd
= {
2394 .flags
= CMD_FLAG_GLOBAL
,
2395 .args
= "[command]",
2396 .oneline
= "help for one or all commands",
2399 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2402 const cmdinfo_t
*ct
;
2407 input
= g_strdup(cmd
);
2408 v
= breakline(input
, &c
);
2410 ct
= find_command(v
[0]);
2412 done
= command(blk
, ct
, c
, v
);
2414 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2423 static void __attribute((constructor
)) init_qemuio_commands(void)
2425 /* initialize commands */
2426 qemuio_add_command(&help_cmd
);
2427 qemuio_add_command(&read_cmd
);
2428 qemuio_add_command(&readv_cmd
);
2429 qemuio_add_command(&write_cmd
);
2430 qemuio_add_command(&writev_cmd
);
2431 qemuio_add_command(&multiwrite_cmd
);
2432 qemuio_add_command(&aio_read_cmd
);
2433 qemuio_add_command(&aio_write_cmd
);
2434 qemuio_add_command(&aio_flush_cmd
);
2435 qemuio_add_command(&flush_cmd
);
2436 qemuio_add_command(&truncate_cmd
);
2437 qemuio_add_command(&length_cmd
);
2438 qemuio_add_command(&info_cmd
);
2439 qemuio_add_command(&discard_cmd
);
2440 qemuio_add_command(&alloc_cmd
);
2441 qemuio_add_command(&map_cmd
);
2442 qemuio_add_command(&reopen_cmd
);
2443 qemuio_add_command(&break_cmd
);
2444 qemuio_add_command(&remove_break_cmd
);
2445 qemuio_add_command(&resume_cmd
);
2446 qemuio_add_command(&wait_break_cmd
);
2447 qemuio_add_command(&abort_cmd
);
2448 qemuio_add_command(&sleep_cmd
);
2449 qemuio_add_command(&sigraise_cmd
);