ppc: Print HSRR0/HSRR1 in "info registers"
[qemu.git] / qemu-io.c
blobdb129eac5ffca9c21e83ad3917400770cbc33932
1 /*
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.
9 */
10 #include "qemu/osdep.h"
11 #include <getopt.h>
12 #include <libgen.h>
14 #include "qapi/error.h"
15 #include "qemu-io.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
21 #include "qemu/log.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qom/object_interfaces.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.h"
27 #include "crypto/init.h"
29 #define CMD_NOFILE_OK 0x01
31 static char *progname;
33 static BlockBackend *qemuio_blk;
35 /* qemu-io commands passed using -c */
36 static int ncmdline;
37 static char **cmdline;
38 static bool imageOpts;
40 static ReadLineState *readline_state;
42 static int close_f(BlockBackend *blk, int argc, char **argv)
44 blk_unref(qemuio_blk);
45 qemuio_blk = NULL;
46 return 0;
49 static const cmdinfo_t close_cmd = {
50 .name = "close",
51 .altname = "c",
52 .cfunc = close_f,
53 .oneline = "close the current open file",
56 static int openfile(char *name, int flags, bool writethrough, QDict *opts)
58 Error *local_err = NULL;
59 BlockDriverState *bs;
61 if (qemuio_blk) {
62 error_report("file open already, try 'help close'");
63 QDECREF(opts);
64 return 1;
67 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
68 if (!qemuio_blk) {
69 error_reportf_err(local_err, "can't open%s%s: ",
70 name ? " device " : "", name ?: "");
71 return 1;
74 bs = blk_bs(qemuio_blk);
75 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
76 char password[256];
77 printf("Disk image '%s' is encrypted.\n", name);
78 if (qemu_read_password(password, sizeof(password)) < 0) {
79 error_report("No password given");
80 goto error;
82 if (bdrv_set_key(bs, password) < 0) {
83 error_report("invalid password");
84 goto error;
88 blk_set_enable_write_cache(qemuio_blk, !writethrough);
90 return 0;
92 error:
93 blk_unref(qemuio_blk);
94 qemuio_blk = NULL;
95 return 1;
98 static void open_help(void)
100 printf(
101 "\n"
102 " opens a new file in the requested mode\n"
103 "\n"
104 " Example:\n"
105 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
106 "\n"
107 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
108 " -r, -- open file read-only\n"
109 " -s, -- use snapshot file\n"
110 " -n, -- disable host cache, short for -t none\n"
111 " -k, -- use kernel AIO implementation (on Linux only)\n"
112 " -t, -- use the given cache mode for the image\n"
113 " -d, -- use the given discard mode for the image\n"
114 " -o, -- options to be given to the block driver"
115 "\n");
118 static int open_f(BlockBackend *blk, int argc, char **argv);
120 static const cmdinfo_t open_cmd = {
121 .name = "open",
122 .altname = "o",
123 .cfunc = open_f,
124 .argmin = 1,
125 .argmax = -1,
126 .flags = CMD_NOFILE_OK,
127 .args = "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
128 .oneline = "open the file specified by path",
129 .help = open_help,
132 static QemuOptsList empty_opts = {
133 .name = "drive",
134 .merge_lists = true,
135 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
136 .desc = {
137 /* no elements => accept any params */
138 { /* end of list */ }
142 static int open_f(BlockBackend *blk, int argc, char **argv)
144 int flags = BDRV_O_UNMAP;
145 int readonly = 0;
146 bool writethrough = true;
147 int c;
148 QemuOpts *qopts;
149 QDict *opts;
151 while ((c = getopt(argc, argv, "snro:kt:d:")) != -1) {
152 switch (c) {
153 case 's':
154 flags |= BDRV_O_SNAPSHOT;
155 break;
156 case 'n':
157 flags |= BDRV_O_NOCACHE;
158 writethrough = false;
159 break;
160 case 'r':
161 readonly = 1;
162 break;
163 case 'k':
164 flags |= BDRV_O_NATIVE_AIO;
165 break;
166 case 't':
167 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
168 error_report("Invalid cache option: %s", optarg);
169 qemu_opts_reset(&empty_opts);
170 return 0;
172 break;
173 case 'd':
174 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
175 error_report("Invalid discard option: %s", optarg);
176 qemu_opts_reset(&empty_opts);
177 return 0;
179 break;
180 case 'o':
181 if (imageOpts) {
182 printf("--image-opts and 'open -o' are mutually exclusive\n");
183 qemu_opts_reset(&empty_opts);
184 return 0;
186 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
187 qemu_opts_reset(&empty_opts);
188 return 0;
190 break;
191 default:
192 qemu_opts_reset(&empty_opts);
193 return qemuio_command_usage(&open_cmd);
197 if (!readonly) {
198 flags |= BDRV_O_RDWR;
201 if (imageOpts && (optind == argc - 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
203 qemu_opts_reset(&empty_opts);
204 return 0;
206 optind++;
209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
213 if (optind == argc - 1) {
214 return openfile(argv[optind], flags, writethrough, opts);
215 } else if (optind == argc) {
216 return openfile(NULL, flags, writethrough, opts);
217 } else {
218 QDECREF(opts);
219 return qemuio_command_usage(&open_cmd);
223 static int quit_f(BlockBackend *blk, int argc, char **argv)
225 return 1;
228 static const cmdinfo_t quit_cmd = {
229 .name = "quit",
230 .altname = "q",
231 .cfunc = quit_f,
232 .argmin = -1,
233 .argmax = -1,
234 .flags = CMD_FLAG_GLOBAL,
235 .oneline = "exit the program",
238 static void usage(const char *name)
240 printf(
241 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
242 "QEMU Disk exerciser\n"
243 "\n"
244 " --object OBJECTDEF define an object such as 'secret' for\n"
245 " passwords and/or encryption keys\n"
246 " --image-opts treat file as option string\n"
247 " -c, --cmd STRING execute command with its arguments\n"
248 " from the given string\n"
249 " -f, --format FMT specifies the block driver to use\n"
250 " -r, --read-only export read-only\n"
251 " -s, --snapshot use snapshot file\n"
252 " -n, --nocache disable host cache, short for -t none\n"
253 " -m, --misalign misalign allocations for O_DIRECT\n"
254 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
255 " -t, --cache=MODE use the given cache mode for the image\n"
256 " -d, --discard=MODE use the given discard mode for the image\n"
257 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
258 " specify tracing options\n"
259 " see qemu-img(1) man page for full description\n"
260 " -h, --help display this help and exit\n"
261 " -V, --version output version information and exit\n"
262 "\n"
263 "See '%s -c help' for information on available commands."
264 "\n",
265 name, name);
268 static char *get_prompt(void)
270 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
272 if (!prompt[0]) {
273 snprintf(prompt, sizeof(prompt), "%s> ", progname);
276 return prompt;
279 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
280 const char *fmt, ...)
282 va_list ap;
283 va_start(ap, fmt);
284 vprintf(fmt, ap);
285 va_end(ap);
288 static void readline_flush_func(void *opaque)
290 fflush(stdout);
293 static void readline_func(void *opaque, const char *str, void *readline_opaque)
295 char **line = readline_opaque;
296 *line = g_strdup(str);
299 static void completion_match(const char *cmd, void *opaque)
301 readline_add_completion(readline_state, cmd);
304 static void readline_completion_func(void *opaque, const char *str)
306 readline_set_completion_index(readline_state, strlen(str));
307 qemuio_complete_command(str, completion_match, NULL);
310 static char *fetchline_readline(void)
312 char *line = NULL;
314 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
315 while (!line) {
316 int ch = getchar();
317 if (ch == EOF) {
318 break;
320 readline_handle_byte(readline_state, ch);
322 return line;
325 #define MAXREADLINESZ 1024
326 static char *fetchline_fgets(void)
328 char *p, *line = g_malloc(MAXREADLINESZ);
330 if (!fgets(line, MAXREADLINESZ, stdin)) {
331 g_free(line);
332 return NULL;
335 p = line + strlen(line);
336 if (p != line && p[-1] == '\n') {
337 p[-1] = '\0';
340 return line;
343 static char *fetchline(void)
345 if (readline_state) {
346 return fetchline_readline();
347 } else {
348 return fetchline_fgets();
352 static void prep_fetchline(void *opaque)
354 int *fetchable = opaque;
356 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
357 *fetchable= 1;
360 static void command_loop(void)
362 int i, done = 0, fetchable = 0, prompted = 0;
363 char *input;
365 for (i = 0; !done && i < ncmdline; i++) {
366 done = qemuio_command(qemuio_blk, cmdline[i]);
368 if (cmdline) {
369 g_free(cmdline);
370 return;
373 while (!done) {
374 if (!prompted) {
375 printf("%s", get_prompt());
376 fflush(stdout);
377 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
378 prompted = 1;
381 main_loop_wait(false);
383 if (!fetchable) {
384 continue;
387 input = fetchline();
388 if (input == NULL) {
389 break;
391 done = qemuio_command(qemuio_blk, input);
392 g_free(input);
394 prompted = 0;
395 fetchable = 0;
397 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
400 static void add_user_command(char *optarg)
402 cmdline = g_renew(char *, cmdline, ++ncmdline);
403 cmdline[ncmdline-1] = optarg;
406 static void reenable_tty_echo(void)
408 qemu_set_tty_echo(STDIN_FILENO, true);
411 enum {
412 OPTION_OBJECT = 256,
413 OPTION_IMAGE_OPTS = 257,
416 static QemuOptsList qemu_object_opts = {
417 .name = "object",
418 .implied_opt_name = "qom-type",
419 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
420 .desc = {
426 static QemuOptsList file_opts = {
427 .name = "file",
428 .implied_opt_name = "file",
429 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
430 .desc = {
431 /* no elements => accept any params */
432 { /* end of list */ }
436 int main(int argc, char **argv)
438 int readonly = 0;
439 const char *sopt = "hVc:d:f:rsnmkt:T:";
440 const struct option lopt[] = {
441 { "help", no_argument, NULL, 'h' },
442 { "version", no_argument, NULL, 'V' },
443 { "cmd", required_argument, NULL, 'c' },
444 { "format", required_argument, NULL, 'f' },
445 { "read-only", no_argument, NULL, 'r' },
446 { "snapshot", no_argument, NULL, 's' },
447 { "nocache", no_argument, NULL, 'n' },
448 { "misalign", no_argument, NULL, 'm' },
449 { "native-aio", no_argument, NULL, 'k' },
450 { "discard", required_argument, NULL, 'd' },
451 { "cache", required_argument, NULL, 't' },
452 { "trace", required_argument, NULL, 'T' },
453 { "object", required_argument, NULL, OPTION_OBJECT },
454 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
455 { NULL, 0, NULL, 0 }
457 int c;
458 int opt_index = 0;
459 int flags = BDRV_O_UNMAP;
460 bool writethrough = true;
461 Error *local_error = NULL;
462 QDict *opts = NULL;
463 const char *format = NULL;
464 char *trace_file = NULL;
466 #ifdef CONFIG_POSIX
467 signal(SIGPIPE, SIG_IGN);
468 #endif
470 progname = basename(argv[0]);
471 qemu_init_exec_dir(argv[0]);
473 qcrypto_init(&error_fatal);
475 module_call_init(MODULE_INIT_QOM);
476 qemu_add_opts(&qemu_object_opts);
477 qemu_add_opts(&qemu_trace_opts);
478 bdrv_init();
480 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
481 switch (c) {
482 case 's':
483 flags |= BDRV_O_SNAPSHOT;
484 break;
485 case 'n':
486 flags |= BDRV_O_NOCACHE;
487 writethrough = false;
488 break;
489 case 'd':
490 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
491 error_report("Invalid discard option: %s", optarg);
492 exit(1);
494 break;
495 case 'f':
496 format = optarg;
497 break;
498 case 'c':
499 add_user_command(optarg);
500 break;
501 case 'r':
502 readonly = 1;
503 break;
504 case 'm':
505 qemuio_misalign = true;
506 break;
507 case 'k':
508 flags |= BDRV_O_NATIVE_AIO;
509 break;
510 case 't':
511 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
512 error_report("Invalid cache option: %s", optarg);
513 exit(1);
515 break;
516 case 'T':
517 g_free(trace_file);
518 trace_file = trace_opt_parse(optarg);
519 break;
520 case 'V':
521 printf("%s version %s\n", progname, QEMU_VERSION);
522 exit(0);
523 case 'h':
524 usage(progname);
525 exit(0);
526 case OPTION_OBJECT: {
527 QemuOpts *qopts;
528 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
529 optarg, true);
530 if (!qopts) {
531 exit(1);
533 } break;
534 case OPTION_IMAGE_OPTS:
535 imageOpts = true;
536 break;
537 default:
538 usage(progname);
539 exit(1);
543 if ((argc - optind) > 1) {
544 usage(progname);
545 exit(1);
548 if (format && imageOpts) {
549 error_report("--image-opts and -f are mutually exclusive");
550 exit(1);
553 if (qemu_init_main_loop(&local_error)) {
554 error_report_err(local_error);
555 exit(1);
558 if (qemu_opts_foreach(&qemu_object_opts,
559 user_creatable_add_opts_foreach,
560 NULL, NULL)) {
561 exit(1);
564 if (!trace_init_backends()) {
565 exit(1);
567 trace_init_file(trace_file);
568 qemu_set_log(LOG_TRACE);
570 /* initialize commands */
571 qemuio_add_command(&quit_cmd);
572 qemuio_add_command(&open_cmd);
573 qemuio_add_command(&close_cmd);
575 if (isatty(STDIN_FILENO)) {
576 readline_state = readline_init(readline_printf_func,
577 readline_flush_func,
578 NULL,
579 readline_completion_func);
580 qemu_set_tty_echo(STDIN_FILENO, false);
581 atexit(reenable_tty_echo);
584 /* open the device */
585 if (!readonly) {
586 flags |= BDRV_O_RDWR;
589 if ((argc - optind) == 1) {
590 if (imageOpts) {
591 QemuOpts *qopts = NULL;
592 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
593 if (!qopts) {
594 exit(1);
596 opts = qemu_opts_to_qdict(qopts, NULL);
597 openfile(NULL, flags, writethrough, opts);
598 } else {
599 if (format) {
600 opts = qdict_new();
601 qdict_put(opts, "driver", qstring_from_str(format));
603 openfile(argv[optind], flags, writethrough, opts);
606 command_loop();
609 * Make sure all outstanding requests complete before the program exits.
611 bdrv_drain_all();
613 blk_unref(qemuio_blk);
614 g_free(readline_state);
615 return 0;