2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009-2016 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
14 #include "sysemu/block-backend.h"
15 #include "block/block.h"
16 #include "block/block_int.h" /* for info_f() */
17 #include "block/qapi.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/timer.h"
21 #include "qemu/cutils.h"
23 #define CMD_NOFILE_OK 0x01
27 static cmdinfo_t
*cmdtab
;
30 static int compare_cmdname(const void *a
, const void *b
)
32 return strcmp(((const cmdinfo_t
*)a
)->name
,
33 ((const cmdinfo_t
*)b
)->name
);
36 void qemuio_add_command(const cmdinfo_t
*ci
)
38 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
39 cmdtab
[ncmds
- 1] = *ci
;
40 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
43 int qemuio_command_usage(const cmdinfo_t
*ci
)
45 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
49 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
51 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
54 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
55 fprintf(stderr
, "no file open, try 'help open'\n");
61 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
66 if (!init_check_command(blk
, ct
)) {
70 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
71 if (ct
->argmax
== -1) {
73 "bad argument count %d to %s, expected at least %d arguments\n",
74 argc
-1, cmd
, ct
->argmin
);
75 } else if (ct
->argmin
== ct
->argmax
) {
77 "bad argument count %d to %s, expected %d arguments\n",
78 argc
-1, cmd
, ct
->argmin
);
81 "bad argument count %d to %s, expected between %d and %d arguments\n",
82 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
87 /* Request additional permissions if necessary for this command. The caller
88 * is responsible for restoring the original permissions afterwards if this
89 * is what it wants. */
90 if (ct
->perm
&& blk_is_available(blk
)) {
91 uint64_t orig_perm
, orig_shared_perm
;
92 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
94 if (ct
->perm
& ~orig_perm
) {
96 Error
*local_err
= NULL
;
99 new_perm
= orig_perm
| ct
->perm
;
101 ret
= blk_set_perm(blk
, new_perm
, orig_shared_perm
, &local_err
);
103 error_report_err(local_err
);
110 return ct
->cfunc(blk
, argc
, argv
);
113 static const cmdinfo_t
*find_command(const char *cmd
)
117 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
118 if (strcmp(ct
->name
, cmd
) == 0 ||
119 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
121 return (const cmdinfo_t
*)ct
;
127 /* Invoke fn() for commands with a matching prefix */
128 void qemuio_complete_command(const char *input
,
129 void (*fn
)(const char *cmd
, void *opaque
),
133 size_t input_len
= strlen(input
);
135 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
136 if (strncmp(input
, ct
->name
, input_len
) == 0) {
137 fn(ct
->name
, opaque
);
142 static char **breakline(char *input
, int *count
)
146 char **rval
= g_new0(char *, 1);
148 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
153 rval
= g_renew(char *, rval
, (c
+ 1));
161 static int64_t cvtnum(const char *s
)
166 err
= qemu_strtosz(s
, NULL
, &value
);
170 if (value
> INT64_MAX
) {
176 static void print_cvtnum_err(int64_t rc
, const char *arg
)
180 printf("Parsing error: non-numeric argument,"
181 " or extraneous/unrecognized suffix -- %s\n", arg
);
184 printf("Parsing error: argument too large -- %s\n", arg
);
187 printf("Parsing error: %s\n", arg
);
191 #define EXABYTES(x) ((long long)(x) << 60)
192 #define PETABYTES(x) ((long long)(x) << 50)
193 #define TERABYTES(x) ((long long)(x) << 40)
194 #define GIGABYTES(x) ((long long)(x) << 30)
195 #define MEGABYTES(x) ((long long)(x) << 20)
196 #define KILOBYTES(x) ((long long)(x) << 10)
198 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
199 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
200 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
201 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
202 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
203 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
205 static void cvtstr(double value
, char *str
, size_t size
)
210 if (value
>= EXABYTES(1)) {
212 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
213 } else if (value
>= PETABYTES(1)) {
215 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
216 } else if (value
>= TERABYTES(1)) {
218 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
219 } else if (value
>= GIGABYTES(1)) {
221 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
222 } else if (value
>= MEGABYTES(1)) {
224 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
225 } else if (value
>= KILOBYTES(1)) {
227 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
230 snprintf(str
, size
- 6, "%f", value
);
233 trim
= strstr(str
, ".000");
235 strcpy(trim
, suffix
);
243 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
245 t1
.tv_usec
-= t2
.tv_usec
;
246 if (t1
.tv_usec
< 0) {
247 t1
.tv_usec
+= 1000000;
250 t1
.tv_sec
-= t2
.tv_sec
;
254 static double tdiv(double value
, struct timeval tv
)
256 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
259 #define HOURS(sec) ((sec) / (60 * 60))
260 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
261 #define SECONDS(sec) ((sec) % 60)
265 TERSE_FIXED_TIME
= 0x1,
266 VERBOSE_FIXED_TIME
= 0x2,
269 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
271 double usec
= (double)tv
->tv_usec
/ 1000000.0;
273 if (format
& TERSE_FIXED_TIME
) {
274 if (!HOURS(tv
->tv_sec
)) {
275 snprintf(ts
, size
, "%u:%02u.%02u",
276 (unsigned int) MINUTES(tv
->tv_sec
),
277 (unsigned int) SECONDS(tv
->tv_sec
),
278 (unsigned int) (usec
* 100));
281 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
284 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
285 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
286 (unsigned int) HOURS(tv
->tv_sec
),
287 (unsigned int) MINUTES(tv
->tv_sec
),
288 (unsigned int) SECONDS(tv
->tv_sec
),
289 (unsigned int) (usec
* 100));
291 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
296 * Parse the pattern argument to various sub-commands.
298 * Because the pattern is used as an argument to memset it must evaluate
299 * to an unsigned integer that fits into a single byte.
301 static int parse_pattern(const char *arg
)
306 pattern
= strtol(arg
, &endptr
, 0);
307 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
308 printf("%s is not a valid pattern byte\n", arg
);
316 * Memory allocation helpers.
318 * Make sure memory is aligned by default, or purposefully misaligned if
319 * that is specified on the command line.
322 #define MISALIGN_OFFSET 16
323 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
327 if (qemuio_misalign
) {
328 len
+= MISALIGN_OFFSET
;
330 buf
= blk_blockalign(blk
, len
);
331 memset(buf
, pattern
, len
);
332 if (qemuio_misalign
) {
333 buf
+= MISALIGN_OFFSET
;
338 static void qemu_io_free(void *p
)
340 if (qemuio_misalign
) {
341 p
-= MISALIGN_OFFSET
;
346 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
352 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
353 const uint8_t *s
= p
;
355 printf("%08" PRIx64
": ", offset
+ i
);
356 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
360 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
371 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
372 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
374 char s1
[64], s2
[64], ts
[64];
376 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
378 cvtstr((double)total
, s1
, sizeof(s1
));
379 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
380 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
381 op
, total
, count
, offset
);
382 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
383 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
384 } else {/* bytes,ops,time,bytes/sec,ops/sec */
385 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
387 tdiv((double)total
, *t
),
388 tdiv((double)cnt
, *t
));
393 * Parse multiple length statements for vectored I/O, and construct an I/O
394 * vector matching it.
397 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
400 size_t *sizes
= g_new0(size_t, nr_iov
);
406 for (i
= 0; i
< nr_iov
; i
++) {
412 print_cvtnum_err(len
, arg
);
416 if (len
> BDRV_REQUEST_MAX_BYTES
) {
417 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
418 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
422 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
423 printf("The total number of bytes exceed the maximum size %" PRIu64
424 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
432 qemu_iovec_init(qiov
, nr_iov
);
434 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
436 for (i
= 0; i
< nr_iov
; i
++) {
437 qemu_iovec_add(qiov
, p
, sizes
[i
]);
446 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
447 int64_t count
, int64_t *total
)
449 if (count
> INT_MAX
) {
453 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
460 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
461 int64_t count
, int flags
, int64_t *total
)
463 if (count
> INT_MAX
) {
467 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, flags
);
484 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
486 CoWriteZeroes
*data
= opaque
;
488 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->count
,
492 *data
->total
= data
->ret
;
496 *data
->total
= data
->count
;
499 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
500 int64_t count
, int flags
, int64_t *total
)
503 CoWriteZeroes data
= {
512 if (count
> INT_MAX
) {
516 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
517 qemu_coroutine_enter(co
);
519 aio_poll(blk_get_aio_context(blk
), true);
528 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
529 int64_t count
, int64_t *total
)
533 if (count
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
537 ret
= blk_pwrite_compressed(blk
, offset
, buf
, count
);
545 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
546 int64_t count
, int64_t *total
)
548 if (count
> INT_MAX
) {
552 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
559 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
560 int64_t count
, int64_t *total
)
562 if (count
> INT_MAX
) {
566 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
573 #define NOT_DONE 0x7fffffff
574 static void aio_rw_done(void *opaque
, int ret
)
576 *(int *)opaque
= ret
;
579 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
580 int64_t offset
, int *total
)
582 int async_ret
= NOT_DONE
;
584 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
585 while (async_ret
== NOT_DONE
) {
586 main_loop_wait(false);
590 return async_ret
< 0 ? async_ret
: 1;
593 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
594 int64_t offset
, int flags
, int *total
)
596 int async_ret
= NOT_DONE
;
598 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
599 while (async_ret
== NOT_DONE
) {
600 main_loop_wait(false);
604 return async_ret
< 0 ? async_ret
: 1;
607 static void read_help(void)
611 " reads a range of bytes from the given offset\n"
614 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
616 " Reads a segment of the currently open file, optionally dumping it to the\n"
617 " standard output stream (with -v option) for subsequent inspection.\n"
618 " -b, -- read from the VM state rather than the virtual disk\n"
619 " -C, -- report statistics in a machine parsable format\n"
620 " -l, -- length for pattern verification (only with -P)\n"
621 " -p, -- ignored for backwards compatibility\n"
622 " -P, -- use a pattern to verify read data\n"
623 " -q, -- quiet mode, do not show I/O statistics\n"
624 " -s, -- start offset for pattern verification (only with -P)\n"
625 " -v, -- dump buffer to standard output\n"
629 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
631 static const cmdinfo_t read_cmd
= {
637 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
638 .oneline
= "reads a number of bytes at a specified offset",
642 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
644 struct timeval t1
, t2
;
645 bool Cflag
= false, qflag
= false, vflag
= false;
646 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
651 /* Some compilers get confused and warn if this is not initialized. */
654 int64_t pattern_offset
= 0, pattern_count
= 0;
656 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
666 pattern_count
= cvtnum(optarg
);
667 if (pattern_count
< 0) {
668 print_cvtnum_err(pattern_count
, optarg
);
673 /* Ignored for backwards compatibility */
677 pattern
= parse_pattern(optarg
);
687 pattern_offset
= cvtnum(optarg
);
688 if (pattern_offset
< 0) {
689 print_cvtnum_err(pattern_offset
, optarg
);
697 return qemuio_command_usage(&read_cmd
);
701 if (optind
!= argc
- 2) {
702 return qemuio_command_usage(&read_cmd
);
705 offset
= cvtnum(argv
[optind
]);
707 print_cvtnum_err(offset
, argv
[optind
]);
712 count
= cvtnum(argv
[optind
]);
714 print_cvtnum_err(count
, argv
[optind
]);
716 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
717 printf("length cannot exceed %" PRIu64
", given %s\n",
718 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
722 if (!Pflag
&& (lflag
|| sflag
)) {
723 return qemuio_command_usage(&read_cmd
);
727 pattern_count
= count
- pattern_offset
;
730 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
731 printf("pattern verification range exceeds end of read data\n");
736 if (offset
& 0x1ff) {
737 printf("offset %" PRId64
" is not sector aligned\n",
742 printf("count %"PRId64
" is not sector aligned\n",
748 buf
= qemu_io_alloc(blk
, count
, 0xab);
750 gettimeofday(&t1
, NULL
);
752 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
754 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
756 gettimeofday(&t2
, NULL
);
759 printf("read failed: %s\n", strerror(-cnt
));
764 void *cmp_buf
= g_malloc(pattern_count
);
765 memset(cmp_buf
, pattern
, pattern_count
);
766 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
767 printf("Pattern verification failed at offset %"
768 PRId64
", %"PRId64
" bytes\n",
769 offset
+ pattern_offset
, pattern_count
);
779 dump_buffer(buf
, offset
, count
);
782 /* Finally, report back -- -C gives a parsable format */
784 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
792 static void readv_help(void)
796 " reads a range of bytes from the given offset into multiple buffers\n"
799 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
801 " Reads a segment of the currently open file, optionally dumping it to the\n"
802 " standard output stream (with -v option) for subsequent inspection.\n"
803 " Uses multiple iovec buffers if more than one byte range is specified.\n"
804 " -C, -- report statistics in a machine parsable format\n"
805 " -P, -- use a pattern to verify read data\n"
806 " -v, -- dump buffer to standard output\n"
807 " -q, -- quiet mode, do not show I/O statistics\n"
811 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
813 static const cmdinfo_t readv_cmd
= {
818 .args
= "[-Cqv] [-P pattern] off len [len..]",
819 .oneline
= "reads a number of bytes at a specified offset",
823 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
825 struct timeval t1
, t2
;
826 bool Cflag
= false, qflag
= false, vflag
= false;
830 /* Some compilers get confused and warn if this is not initialized. */
837 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
844 pattern
= parse_pattern(optarg
);
856 return qemuio_command_usage(&readv_cmd
);
860 if (optind
> argc
- 2) {
861 return qemuio_command_usage(&readv_cmd
);
865 offset
= cvtnum(argv
[optind
]);
867 print_cvtnum_err(offset
, argv
[optind
]);
872 nr_iov
= argc
- optind
;
873 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
878 gettimeofday(&t1
, NULL
);
879 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
880 gettimeofday(&t2
, NULL
);
883 printf("readv failed: %s\n", strerror(-cnt
));
888 void *cmp_buf
= g_malloc(qiov
.size
);
889 memset(cmp_buf
, pattern
, qiov
.size
);
890 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
891 printf("Pattern verification failed at offset %"
892 PRId64
", %zd bytes\n", offset
, qiov
.size
);
902 dump_buffer(buf
, offset
, qiov
.size
);
905 /* Finally, report back -- -C gives a parsable format */
907 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
910 qemu_iovec_destroy(&qiov
);
915 static void write_help(void)
919 " writes a range of bytes from the given offset\n"
922 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
924 " Writes into a segment of the currently open file, using a buffer\n"
925 " filled with a set pattern (0xcdcdcdcd).\n"
926 " -b, -- write to the VM state rather than the virtual disk\n"
927 " -c, -- write compressed data with blk_write_compressed\n"
928 " -f, -- use Force Unit Access semantics\n"
929 " -p, -- ignored for backwards compatibility\n"
930 " -P, -- use different pattern to fill file\n"
931 " -C, -- report statistics in a machine parsable format\n"
932 " -q, -- quiet mode, do not show I/O statistics\n"
933 " -u, -- with -z, allow unmapping\n"
934 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
938 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
940 static const cmdinfo_t write_cmd
= {
944 .perm
= BLK_PERM_WRITE
,
947 .args
= "[-bcCfquz] [-P pattern] off len",
948 .oneline
= "writes a number of bytes at a specified offset",
952 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
954 struct timeval t1
, t2
;
955 bool Cflag
= false, qflag
= false, bflag
= false;
956 bool Pflag
= false, zflag
= false, cflag
= false;
962 /* Some compilers get confused and warn if this is not initialized. */
966 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
978 flags
|= BDRV_REQ_FUA
;
981 /* Ignored for backwards compatibility */
985 pattern
= parse_pattern(optarg
);
994 flags
|= BDRV_REQ_MAY_UNMAP
;
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 ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1014 printf("-f and -b or -c cannot be specified at the same time\n");
1018 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1019 printf("-u requires -z to be specified\n");
1023 if (zflag
&& Pflag
) {
1024 printf("-z and -P cannot be specified at the same time\n");
1028 offset
= cvtnum(argv
[optind
]);
1030 print_cvtnum_err(offset
, argv
[optind
]);
1035 count
= cvtnum(argv
[optind
]);
1037 print_cvtnum_err(count
, argv
[optind
]);
1039 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1040 printf("length cannot exceed %" PRIu64
", given %s\n",
1041 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1045 if (bflag
|| cflag
) {
1046 if (offset
& 0x1ff) {
1047 printf("offset %" PRId64
" is not sector aligned\n",
1052 if (count
& 0x1ff) {
1053 printf("count %"PRId64
" is not sector aligned\n",
1060 buf
= qemu_io_alloc(blk
, count
, pattern
);
1063 gettimeofday(&t1
, NULL
);
1065 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1067 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1069 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1071 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1073 gettimeofday(&t2
, NULL
);
1076 printf("write failed: %s\n", strerror(-cnt
));
1084 /* Finally, report back -- -C gives a parsable format */
1086 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1101 " writes a range of bytes from the given offset source from multiple buffers\n"
1104 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1106 " Writes into a segment of the currently open file, using a buffer\n"
1107 " filled with a set pattern (0xcdcdcdcd).\n"
1108 " -P, -- use different pattern to fill file\n"
1109 " -C, -- report statistics in a machine parsable format\n"
1110 " -f, -- use Force Unit Access semantics\n"
1111 " -q, -- quiet mode, do not show I/O statistics\n"
1115 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1117 static const cmdinfo_t writev_cmd
= {
1120 .perm
= BLK_PERM_WRITE
,
1123 .args
= "[-Cfq] [-P pattern] off len [len..]",
1124 .oneline
= "writes a number of bytes at a specified offset",
1125 .help
= writev_help
,
1128 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1130 struct timeval t1
, t2
;
1131 bool Cflag
= false, qflag
= false;
1136 /* Some compilers get confused and warn if this is not initialized. */
1142 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1148 flags
|= BDRV_REQ_FUA
;
1154 pattern
= parse_pattern(optarg
);
1160 return qemuio_command_usage(&writev_cmd
);
1164 if (optind
> argc
- 2) {
1165 return qemuio_command_usage(&writev_cmd
);
1168 offset
= cvtnum(argv
[optind
]);
1170 print_cvtnum_err(offset
, argv
[optind
]);
1175 nr_iov
= argc
- optind
;
1176 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1181 gettimeofday(&t1
, NULL
);
1182 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1183 gettimeofday(&t2
, NULL
);
1186 printf("writev failed: %s\n", strerror(-cnt
));
1194 /* Finally, report back -- -C gives a parsable format */
1196 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1198 qemu_iovec_destroy(&qiov
);
1213 BlockAcctCookie acct
;
1218 static void aio_write_done(void *opaque
, int ret
)
1220 struct aio_ctx
*ctx
= opaque
;
1223 gettimeofday(&t2
, NULL
);
1227 printf("aio_write failed: %s\n", strerror(-ret
));
1228 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1232 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1238 /* Finally, report back -- -C gives a parsable format */
1239 t2
= tsub(t2
, ctx
->t1
);
1240 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1241 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1244 qemu_io_free(ctx
->buf
);
1245 qemu_iovec_destroy(&ctx
->qiov
);
1250 static void aio_read_done(void *opaque
, int ret
)
1252 struct aio_ctx
*ctx
= opaque
;
1255 gettimeofday(&t2
, NULL
);
1258 printf("readv failed: %s\n", strerror(-ret
));
1259 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1264 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1266 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1267 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1268 printf("Pattern verification failed at offset %"
1269 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1274 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1281 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1284 /* Finally, report back -- -C gives a parsable format */
1285 t2
= tsub(t2
, ctx
->t1
);
1286 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1287 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1289 qemu_io_free(ctx
->buf
);
1290 qemu_iovec_destroy(&ctx
->qiov
);
1294 static void aio_read_help(void)
1298 " asynchronously reads a range of bytes from the given offset\n"
1301 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1303 " Reads a segment of the currently open file, optionally dumping it to the\n"
1304 " standard output stream (with -v option) for subsequent inspection.\n"
1305 " The read is performed asynchronously and the aio_flush command must be\n"
1306 " used to ensure all outstanding aio requests have been completed.\n"
1307 " -C, -- report statistics in a machine parsable format\n"
1308 " -P, -- use a pattern to verify read data\n"
1309 " -i, -- treat request as invalid, for exercising stats\n"
1310 " -v, -- dump buffer to standard output\n"
1311 " -q, -- quiet mode, do not show I/O statistics\n"
1315 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1317 static const cmdinfo_t aio_read_cmd
= {
1319 .cfunc
= aio_read_f
,
1322 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1323 .oneline
= "asynchronously reads a number of bytes",
1324 .help
= aio_read_help
,
1327 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1330 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1333 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1340 ctx
->pattern
= parse_pattern(optarg
);
1341 if (ctx
->pattern
< 0) {
1347 printf("injecting invalid read request\n");
1348 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1359 return qemuio_command_usage(&aio_read_cmd
);
1363 if (optind
> argc
- 2) {
1365 return qemuio_command_usage(&aio_read_cmd
);
1368 ctx
->offset
= cvtnum(argv
[optind
]);
1369 if (ctx
->offset
< 0) {
1370 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1376 nr_iov
= argc
- optind
;
1377 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1378 if (ctx
->buf
== NULL
) {
1379 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1384 gettimeofday(&ctx
->t1
, NULL
);
1385 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1387 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1391 static void aio_write_help(void)
1395 " asynchronously writes a range of bytes from the given offset source\n"
1396 " from multiple buffers\n"
1399 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1401 " Writes into a segment of the currently open file, using a buffer\n"
1402 " filled with a set pattern (0xcdcdcdcd).\n"
1403 " The write is performed asynchronously and the aio_flush command must be\n"
1404 " used to ensure all outstanding aio requests have been completed.\n"
1405 " -P, -- use different pattern to fill file\n"
1406 " -C, -- report statistics in a machine parsable format\n"
1407 " -f, -- use Force Unit Access semantics\n"
1408 " -i, -- treat request as invalid, for exercising stats\n"
1409 " -q, -- quiet mode, do not show I/O statistics\n"
1410 " -u, -- with -z, allow unmapping\n"
1411 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1415 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1417 static const cmdinfo_t aio_write_cmd
= {
1418 .name
= "aio_write",
1419 .cfunc
= aio_write_f
,
1420 .perm
= BLK_PERM_WRITE
,
1423 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1424 .oneline
= "asynchronously writes a number of bytes",
1425 .help
= aio_write_help
,
1428 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1432 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1436 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1442 flags
|= BDRV_REQ_FUA
;
1448 flags
|= BDRV_REQ_MAY_UNMAP
;
1451 pattern
= parse_pattern(optarg
);
1458 printf("injecting invalid write request\n");
1459 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1467 return qemuio_command_usage(&aio_write_cmd
);
1471 if (optind
> argc
- 2) {
1473 return qemuio_command_usage(&aio_write_cmd
);
1476 if (ctx
->zflag
&& optind
!= argc
- 2) {
1477 printf("-z supports only a single length parameter\n");
1482 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1483 printf("-u requires -z to be specified\n");
1488 if (ctx
->zflag
&& ctx
->Pflag
) {
1489 printf("-z and -P cannot be specified at the same time\n");
1494 ctx
->offset
= cvtnum(argv
[optind
]);
1495 if (ctx
->offset
< 0) {
1496 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1503 int64_t count
= cvtnum(argv
[optind
]);
1505 print_cvtnum_err(count
, argv
[optind
]);
1510 ctx
->qiov
.size
= count
;
1511 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1514 nr_iov
= argc
- optind
;
1515 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1517 if (ctx
->buf
== NULL
) {
1518 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1523 gettimeofday(&ctx
->t1
, NULL
);
1524 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1527 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1533 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1535 BlockAcctCookie cookie
;
1536 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1538 block_acct_done(blk_get_stats(blk
), &cookie
);
1542 static const cmdinfo_t aio_flush_cmd
= {
1543 .name
= "aio_flush",
1544 .cfunc
= aio_flush_f
,
1545 .oneline
= "completes all outstanding aio requests"
1548 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1554 static const cmdinfo_t flush_cmd
= {
1558 .oneline
= "flush all in-core file state to disk",
1561 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1566 offset
= cvtnum(argv
[1]);
1568 print_cvtnum_err(offset
, argv
[1]);
1572 ret
= blk_truncate(blk
, offset
);
1574 printf("truncate: %s\n", strerror(-ret
));
1581 static const cmdinfo_t truncate_cmd
= {
1584 .cfunc
= truncate_f
,
1585 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1589 .oneline
= "truncates the current file at the given offset",
1592 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1597 size
= blk_getlength(blk
);
1599 printf("getlength: %s\n", strerror(-size
));
1603 cvtstr(size
, s1
, sizeof(s1
));
1609 static const cmdinfo_t length_cmd
= {
1613 .oneline
= "gets the length of the current file",
1617 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1619 BlockDriverState
*bs
= blk_bs(blk
);
1620 BlockDriverInfo bdi
;
1621 ImageInfoSpecific
*spec_info
;
1622 char s1
[64], s2
[64];
1625 if (bs
->drv
&& bs
->drv
->format_name
) {
1626 printf("format name: %s\n", bs
->drv
->format_name
);
1628 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1629 printf("format name: %s\n", bs
->drv
->protocol_name
);
1632 ret
= bdrv_get_info(bs
, &bdi
);
1637 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1638 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1640 printf("cluster size: %s\n", s1
);
1641 printf("vm state offset: %s\n", s2
);
1643 spec_info
= bdrv_get_specific_info(bs
);
1645 printf("Format specific information:\n");
1646 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1647 qapi_free_ImageInfoSpecific(spec_info
);
1655 static const cmdinfo_t info_cmd
= {
1659 .oneline
= "prints information about the current file",
1662 static void discard_help(void)
1666 " discards a range of bytes from the given offset\n"
1669 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1671 " Discards a segment of the currently open file.\n"
1672 " -C, -- report statistics in a machine parsable format\n"
1673 " -q, -- quiet mode, do not show I/O statistics\n"
1677 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1679 static const cmdinfo_t discard_cmd
= {
1683 .perm
= BLK_PERM_WRITE
,
1686 .args
= "[-Cq] off len",
1687 .oneline
= "discards a number of bytes at a specified offset",
1688 .help
= discard_help
,
1691 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1693 struct timeval t1
, t2
;
1694 bool Cflag
= false, qflag
= false;
1696 int64_t offset
, count
;
1698 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1707 return qemuio_command_usage(&discard_cmd
);
1711 if (optind
!= argc
- 2) {
1712 return qemuio_command_usage(&discard_cmd
);
1715 offset
= cvtnum(argv
[optind
]);
1717 print_cvtnum_err(offset
, argv
[optind
]);
1722 count
= cvtnum(argv
[optind
]);
1724 print_cvtnum_err(count
, argv
[optind
]);
1726 } else if (count
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1727 printf("length cannot exceed %"PRIu64
", given %s\n",
1728 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1733 gettimeofday(&t1
, NULL
);
1734 ret
= blk_pdiscard(blk
, offset
, count
);
1735 gettimeofday(&t2
, NULL
);
1738 printf("discard failed: %s\n", strerror(-ret
));
1742 /* Finally, report back -- -C gives a parsable format */
1745 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1752 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1754 BlockDriverState
*bs
= blk_bs(blk
);
1755 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1760 offset
= cvtnum(argv
[1]);
1762 print_cvtnum_err(offset
, argv
[1]);
1764 } else if (offset
& 0x1ff) {
1765 printf("offset %" PRId64
" is not sector aligned\n",
1771 nb_sectors
= cvtnum(argv
[2]);
1772 if (nb_sectors
< 0) {
1773 print_cvtnum_err(nb_sectors
, argv
[2]);
1775 } else if (nb_sectors
> INT_MAX
) {
1776 printf("length argument cannot exceed %d, given %s\n",
1784 remaining
= nb_sectors
;
1786 sector_num
= offset
>> 9;
1788 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1790 printf("is_allocated failed: %s\n", strerror(-ret
));
1799 nb_sectors
-= remaining
;
1804 cvtstr(offset
, s1
, sizeof(s1
));
1806 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1807 sum_alloc
, nb_sectors
, s1
);
1811 static const cmdinfo_t alloc_cmd
= {
1817 .args
= "off [sectors]",
1818 .oneline
= "checks if a sector is present in the file",
1822 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1823 int64_t nb_sectors
, int64_t *pnum
)
1825 int num
, num_checked
;
1828 num_checked
= MIN(nb_sectors
, INT_MAX
);
1829 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1837 while (nb_sectors
> 0 && ret
== firstret
) {
1841 num_checked
= MIN(nb_sectors
, INT_MAX
);
1842 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1843 if (ret
== firstret
&& num
) {
1853 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1856 int64_t nb_sectors
, total_sectors
;
1863 total_sectors
= blk_nb_sectors(blk
);
1864 if (total_sectors
< 0) {
1865 error_report("Failed to query image length: %s",
1866 strerror(-total_sectors
));
1870 nb_sectors
= total_sectors
;
1873 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1875 error_report("Failed to get allocation status: %s", strerror(-ret
));
1878 error_report("Unexpected end of image");
1882 retstr
= ret
? " allocated" : "not allocated";
1883 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1884 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1885 "at offset %s (%d)\n",
1886 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1890 } while (offset
< total_sectors
);
1895 static const cmdinfo_t map_cmd
= {
1901 .oneline
= "prints the allocated areas of a file",
1904 static void reopen_help(void)
1908 " Changes the open options of an already opened image\n"
1911 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1913 " -r, -- Reopen the image read-only\n"
1914 " -c, -- Change the cache mode to the given value\n"
1915 " -o, -- Changes block driver options (cf. 'open' command)\n"
1919 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1921 static QemuOptsList reopen_opts
= {
1923 .merge_lists
= true,
1924 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1926 /* no elements => accept any params */
1927 { /* end of list */ }
1931 static const cmdinfo_t reopen_cmd
= {
1936 .args
= "[-r] [-c cache] [-o options]",
1937 .oneline
= "reopens an image with new options",
1938 .help
= reopen_help
,
1941 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1943 BlockDriverState
*bs
= blk_bs(blk
);
1947 int flags
= bs
->open_flags
;
1948 bool writethrough
= !blk_enable_write_cache(blk
);
1950 BlockReopenQueue
*brq
;
1951 Error
*local_err
= NULL
;
1953 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
1956 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1957 error_report("Invalid cache option: %s", optarg
);
1962 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1963 qemu_opts_reset(&reopen_opts
);
1968 flags
&= ~BDRV_O_RDWR
;
1971 qemu_opts_reset(&reopen_opts
);
1972 return qemuio_command_usage(&reopen_cmd
);
1976 if (optind
!= argc
) {
1977 qemu_opts_reset(&reopen_opts
);
1978 return qemuio_command_usage(&reopen_cmd
);
1981 if (writethrough
!= blk_enable_write_cache(blk
) &&
1982 blk_get_attached_dev(blk
))
1984 error_report("Cannot change cache.writeback: Device attached");
1985 qemu_opts_reset(&reopen_opts
);
1989 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
1990 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
1991 qemu_opts_reset(&reopen_opts
);
1993 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
1994 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
1996 error_report_err(local_err
);
1998 blk_set_enable_write_cache(blk
, !writethrough
);
2004 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2008 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2010 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2016 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2020 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2022 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2028 static const cmdinfo_t break_cmd
= {
2033 .args
= "event tag",
2034 .oneline
= "sets a breakpoint on event and tags the stopped "
2038 static const cmdinfo_t remove_break_cmd
= {
2039 .name
= "remove_break",
2042 .cfunc
= remove_break_f
,
2044 .oneline
= "remove a breakpoint by tag",
2047 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2051 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2053 printf("Could not resume request: %s\n", strerror(-ret
));
2059 static const cmdinfo_t resume_cmd
= {
2065 .oneline
= "resumes the request tagged as tag",
2068 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2070 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2071 aio_poll(blk_get_aio_context(blk
), true);
2077 static const cmdinfo_t wait_break_cmd
= {
2078 .name
= "wait_break",
2081 .cfunc
= wait_break_f
,
2083 .oneline
= "waits for the suspension of a request",
2086 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2091 static const cmdinfo_t abort_cmd
= {
2094 .flags
= CMD_NOFILE_OK
,
2095 .oneline
= "simulate a program crash using abort(3)",
2098 static void sigraise_help(void)
2102 " raises the given signal\n"
2105 " 'sigraise %i' - raises SIGTERM\n"
2107 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2108 " given to sigraise.\n"
2112 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2114 static const cmdinfo_t sigraise_cmd
= {
2116 .cfunc
= sigraise_f
,
2119 .flags
= CMD_NOFILE_OK
,
2121 .oneline
= "raises a signal",
2122 .help
= sigraise_help
,
2125 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2127 int64_t sig
= cvtnum(argv
[1]);
2129 print_cvtnum_err(sig
, argv
[1]);
2131 } else if (sig
> NSIG
) {
2132 printf("signal argument '%s' is too large to be a valid signal\n",
2137 /* Using raise() to kill this process does not necessarily flush all open
2138 * streams. At least stdout and stderr (although the latter should be
2139 * non-buffered anyway) should be flushed, though. */
2147 static void sleep_cb(void *opaque
)
2149 bool *expired
= opaque
;
2153 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2157 struct QEMUTimer
*timer
;
2158 bool expired
= false;
2160 ms
= strtol(argv
[1], &endptr
, 0);
2161 if (ms
< 0 || *endptr
!= '\0') {
2162 printf("%s is not a valid number\n", argv
[1]);
2166 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2167 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2170 main_loop_wait(false);
2178 static const cmdinfo_t sleep_cmd
= {
2183 .flags
= CMD_NOFILE_OK
,
2184 .oneline
= "waits for the given value in milliseconds",
2187 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2192 printf("%s ", ct
->name
);
2194 printf("(or %s) ", ct
->altname
);
2199 printf("%s ", ct
->args
);
2201 printf("-- %s\n", ct
->oneline
);
2204 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2206 help_oneline(cmd
, ct
);
2212 static void help_all(void)
2214 const cmdinfo_t
*ct
;
2216 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2217 help_oneline(ct
->name
, ct
);
2219 printf("\nUse 'help commandname' for extended help.\n");
2222 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2224 const cmdinfo_t
*ct
;
2231 ct
= find_command(argv
[1]);
2233 printf("command %s not found\n", argv
[1]);
2237 help_onecmd(argv
[1], ct
);
2241 static const cmdinfo_t help_cmd
= {
2247 .flags
= CMD_FLAG_GLOBAL
,
2248 .args
= "[command]",
2249 .oneline
= "help for one or all commands",
2252 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2256 const cmdinfo_t
*ct
;
2261 input
= g_strdup(cmd
);
2262 v
= breakline(input
, &c
);
2264 ct
= find_command(v
[0]);
2266 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2267 aio_context_acquire(ctx
);
2268 done
= command(blk
, ct
, c
, v
);
2269 aio_context_release(ctx
);
2271 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2280 static void __attribute((constructor
)) init_qemuio_commands(void)
2282 /* initialize commands */
2283 qemuio_add_command(&help_cmd
);
2284 qemuio_add_command(&read_cmd
);
2285 qemuio_add_command(&readv_cmd
);
2286 qemuio_add_command(&write_cmd
);
2287 qemuio_add_command(&writev_cmd
);
2288 qemuio_add_command(&aio_read_cmd
);
2289 qemuio_add_command(&aio_write_cmd
);
2290 qemuio_add_command(&aio_flush_cmd
);
2291 qemuio_add_command(&flush_cmd
);
2292 qemuio_add_command(&truncate_cmd
);
2293 qemuio_add_command(&length_cmd
);
2294 qemuio_add_command(&info_cmd
);
2295 qemuio_add_command(&discard_cmd
);
2296 qemuio_add_command(&alloc_cmd
);
2297 qemuio_add_command(&map_cmd
);
2298 qemuio_add_command(&reopen_cmd
);
2299 qemuio_add_command(&break_cmd
);
2300 qemuio_add_command(&remove_break_cmd
);
2301 qemuio_add_command(&resume_cmd
);
2302 qemuio_add_command(&wait_break_cmd
);
2303 qemuio_add_command(&abort_cmd
);
2304 qemuio_add_command(&sleep_cmd
);
2305 qemuio_add_command(&sigraise_cmd
);