qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 19/19] Add a -capabilities argument to allow easy query for static QEMU info
Date: Mon,  7 Jun 2010 15:42:32 +0100	[thread overview]
Message-ID: <1275921752-29420-20-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1275921752-29420-1-git-send-email-berrange@redhat.com>

The QMP monitor provides a number of commands for querying info about
the QEMU binary capabilities. Given that these commands don't take
any options and just return static data, requiring the use of QMP is
unnecessarily onerous. This adds a new '-capabilities' command line
argument as a syntactic sugar for accessing the QMP commands that
just return static QEMU binary capabilities.

Setting the '-capabilities' argument causes QEMU to output the requested
data on stdout, pretty printed in JSON format. The argument expects an
associated value to identify the data to be printed. This can be one of
the strings version|machines|devices|cputypes|target|commands|argv|netdev

To query all possible data at once, the shorthand 'all' is allowed.

The output is a QDict where the key is the type of data requested, and
the value is the JSON data from the associated monitor command. For
example:

  $ qemu -capabilities all
  {
    "machines": [
        {
            "name": "pc-0.13",
            "description": "Standard PC",
            "default": 0
        },
        {
            "name": "pc",
            ....
        }
        ....
     }
     "version": {
        "qemu": {
            "micro": 50,
            "minor": 12,
            "major": 0
        },
        "package": ""
     },
     "target": {
        "arch": "i386",
        "wordsize": 32,
     ...
   }

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 monitor.c       |    4 +-
 monitor.h       |    2 +
 qemu-options.hx |    9 +++++
 vl.c            |  100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/monitor.c b/monitor.c
index 6203f75..401a27a 100644
--- a/monitor.c
+++ b/monitor.c
@@ -711,7 +711,7 @@ static void do_info_version_print(Monitor *mon, const QObject *data)
  *
  * { qemu: { "major": 0, "minor": 11, "micro": 5 }, "package": "" }
  */
-static void do_info_version(Monitor *mon, QObject **ret_data)
+void do_info_version(Monitor *mon, QObject **ret_data)
 {
     const char *version = QEMU_VERSION;
     int major = 0, minor = 0, micro = 0;
@@ -760,7 +760,7 @@ static QObject *get_cmd_dict(const char *name)
     return qobject_from_jsonf("{ 'name': %s }", p);
 }
 
-static void do_info_commands(Monitor *mon, QObject **ret_data)
+void do_info_commands(Monitor *mon, QObject **ret_data)
 {
     QList *cmd_list;
     const mon_cmd_t *cmd;
diff --git a/monitor.h b/monitor.h
index 733485f..dc376af 100644
--- a/monitor.h
+++ b/monitor.h
@@ -57,5 +57,7 @@ typedef void (MonitorCompletion)(void *opaque, QObject *ret_data);
 void monitor_set_error(Monitor *mon, QError *qerror);
 
 void do_info_argv(Monitor *mon, QObject **data);
+void do_info_version(Monitor *mon, QObject **ret_data);
+void do_info_commands(Monitor *mon, QObject **ret_data);
 
 #endif /* !MONITOR_H */
diff --git a/qemu-options.hx b/qemu-options.hx
index a6928b7..5f82347 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -27,6 +27,15 @@ STEXI
 Display version information and exit
 ETEXI
 
+DEF("capabilities", HAS_ARG, QEMU_OPTION_capabilities,
+    "-capabilities   all|version|machines|devices|cputypes|target|commands|argv|netdev\n"
+    "                display capabilities of the QEMU binary and exit\n", QEMU_ARCH_ALL)
+STEXI
+@item -capabilities  all|version|machines|devices|cputypes|target|commands|argv|netdev
+@findex -capabilities
+Display capabilities of the QEMU binary and exit
+ETEXI
+
 DEF("M", HAS_ARG, QEMU_OPTION_M,
     "-M machine      select emulated machine (-M ? for list)\n", QEMU_ARCH_ALL)
 STEXI
diff --git a/vl.c b/vl.c
index de010cc..1f165a1 100644
--- a/vl.c
+++ b/vl.c
@@ -1988,6 +1988,97 @@ static void version(void)
 }
 
 
+enum {
+    QEMU_CAPS_VERSION,
+    QEMU_CAPS_MACHINES,
+    QEMU_CAPS_DEVICES,
+    QEMU_CAPS_CPUTYPES,
+    QEMU_CAPS_TARGET,
+    QEMU_CAPS_COMMANDS,
+    QEMU_CAPS_ARGV,
+    QEMU_CAPS_NETDEV,
+    QEMU_CAPS_CONFIG,
+
+    QEMU_CAPS_LAST
+};
+
+QEMU_ENUM_DECL(qemu_caps_flag);
+QEMU_ENUM_IMPL(qemu_caps_flag, QEMU_CAPS_LAST,
+               "version",
+               "machines",
+               "devices",
+               "cputypes",
+               "target",
+               "commands",
+               "argv",
+               "netdev",
+               "config");
+
+static int qemu_caps_flags_from_string(const char *flagsstr)
+{
+    if (strcmp(flagsstr, "all") == 0)
+        return ~0;
+    else {
+        int i = qemu_caps_flag_from_string(flagsstr);
+        if (i < 0)
+            return 0;
+        return (1 << i);
+    }
+
+    return 0;
+}
+typedef void (*qemu_caps_handler)(Monitor *mon, QObject **);
+
+/* Binding capabilities to a subset of monitor commands.
+ * The commands must only use static binary state, no
+ * VM runtime state and must accept mon=NULL
+ */
+static const qemu_caps_handler qemu_caps_handlers[] = {
+    do_info_version,
+    do_info_machines,
+    do_info_devices,
+    do_info_cpu_types,
+    do_info_target,
+    do_info_commands,
+    do_info_argv,
+    do_info_netdev,
+    do_info_config,
+};
+verify(ARRAY_SIZE(qemu_caps_handlers) == QEMU_CAPS_LAST);
+
+static void qemu_show_caps(const char *flagsstr)
+{
+    QDict *data = qdict_new();
+    QString *json;
+    int i;
+    int flags;
+
+    flags = qemu_caps_flags_from_string(flagsstr);
+    if (flags == 0) {
+        fprintf(stderr, "Unknown capabilities '%s'\n", flagsstr);
+        exit(1);
+    }
+
+    for (i = 0 ; i < QEMU_CAPS_LAST ; i++) {
+        if (flags & (1 << i)) {
+            QObject *val;
+            qemu_caps_handler handler = qemu_caps_handlers[i];
+            const char *name = qemu_caps_flag_to_string(i);
+            (*handler)(NULL, &val);
+            qdict_put_obj(data, name, val);
+        }
+    }
+    json = qobject_to_json_pretty(QOBJECT(data));
+    assert(json != NULL);
+
+    qstring_append_chr(json, '\n');
+    fprintf(stdout, qstring_get_str(json));
+
+    QDECREF(json);
+
+}
+
+
 /**
  * do_info_argv():
  *
@@ -2647,6 +2738,7 @@ int main(int argc, char **argv, char **envp)
 #endif
     int show_vnc_port = 0;
     int defconfig = 1;
+    const char *showcaps = NULL;
 
     error_set_progname(argv[0]);
 
@@ -3020,6 +3112,9 @@ int main(int argc, char **argv, char **envp)
                 version();
                 exit(0);
                 break;
+            case QEMU_OPTION_capabilities:
+                showcaps = optarg;
+                break;
             case QEMU_OPTION_m: {
                 uint64_t value;
                 char *ptr;
@@ -3785,6 +3880,11 @@ int main(int argc, char **argv, char **envp)
     if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
         exit(0);
 
+    if (showcaps) {
+        qemu_show_caps(showcaps);
+        exit(0);
+    }
+
     if (watchdog) {
         i = select_watchdog(watchdog);
         if (i > 0)
-- 
1.6.6.1

  parent reply	other threads:[~2010-06-07 14:44 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-07 14:42 [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 01/19] Add support for JSON pretty printing Daniel P. Berrange
2010-06-09 19:51   ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 02/19] Add support for compile time assertions Daniel P. Berrange
2010-06-07 15:35   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 03/19] Add enum handlers for easy & efficient string <-> int conversion Daniel P. Berrange
2010-06-09 19:52   ` Luiz Capitulino
2010-06-26  6:52   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 04/19] Add support for a option parameter as an enum Daniel P. Berrange
2010-06-26  6:59   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 05/19] Ensure that QEMU exits if drive_add parsing fails Daniel P. Berrange
2010-06-26  7:04   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 06/19] Convert drive options to use enumeration data type Daniel P. Berrange
2010-06-09 19:52   ` Luiz Capitulino
2010-06-26  7:07   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 07/19] Convert netdev client types to use an enumeration Daniel P. Berrange
2010-06-07 15:09   ` Anthony Liguori
2010-06-07 15:13     ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 08/19] Convert RTC to use enumerations for configuration parameters Daniel P. Berrange
2010-06-09 19:54   ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 09/19] Change 'query-version' to output broken down version string Daniel P. Berrange
2010-06-07 15:11   ` Anthony Liguori
2010-06-09 20:04     ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 10/19] Add a query-machines command to QMP Daniel P. Berrange
2010-06-07 15:13   ` Anthony Liguori
2010-06-07 16:44     ` Daniel P. Berrange
2010-06-07 17:07       ` Anthony Liguori
2010-06-07 17:14         ` Daniel P. Berrange
2010-06-09 20:06         ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 11/19] Add a query-devices " Daniel P. Berrange
2010-06-07 15:14   ` Anthony Liguori
2010-06-07 16:03     ` Daniel P. Berrange
2010-06-26  7:12       ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 12/19] Add a query-cputypes " Daniel P. Berrange
2010-06-26  7:18   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 13/19] Add a query-target " Daniel P. Berrange
2010-06-07 15:43   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 14/19] Add a query-argv " Daniel P. Berrange
2010-06-07 15:01   ` Anthony Liguori
2010-06-10 15:34     ` [Qemu-devel] " Paolo Bonzini
2010-06-26  7:30     ` [Qemu-devel] " Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 15/19] Expand query-argv to include help string Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 16/19] Add a query-netdev command to QMP Daniel P. Berrange
2010-06-07 15:15   ` Anthony Liguori
2010-06-26  7:32     ` Markus Armbruster
2010-06-07 19:13   ` Miguel Di Ciurcio Filho
2010-06-08  8:38     ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 17/19] Add a query-config " Daniel P. Berrange
2010-06-26  7:34   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 18/19] Add option to turn on JSON pretty printing in monitor Daniel P. Berrange
2010-06-07 14:42 ` Daniel P. Berrange [this message]
2010-06-07 16:04   ` [Qemu-devel] Re: [PATCH 19/19] Add a -capabilities argument to allow easy query for static QEMU info Paolo Bonzini
2010-06-07 16:09     ` Daniel P. Berrange
2010-06-07 16:04   ` [Qemu-devel] " Anthony Liguori
2010-06-07 14:58 ` [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Anthony Liguori
2010-06-07 15:10   ` Daniel P. Berrange
2010-06-07 15:18     ` Anthony Liguori
2010-06-07 16:02   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:03     ` Anthony Liguori
2010-06-07 16:07 ` [Qemu-devel] " Anthony Liguori
2010-06-09 20:25   ` Luiz Capitulino
2010-06-26  6:45 ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1275921752-29420-20-git-send-email-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).