Skip to contentSkip to conversation

: Initial QObject conversion

PATCH Superseded Luiz Capitulino 1 participant 17 messages Oct 7, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 cover : Initial QObject conversion

Hi there,

This is a respin to address Anthony's comments.

Changelog:

V2 -> V3:

- Coding style changes in QList
- Better patch split
- Add an union to store info and command handlers
- Minor changes

V1 -> V2:

- Drop error handling code
- More handlers converted

V0 -> V1:

- Introduced MonitorError
- More handlers converted
- Improved patches descriptions
- Minor changes

diffstat:

 Makefile        |    3 +-
 check-qlist.c   |  153 ++++++++++++++
 configure       |    2 +-
 monitor.c       |  550 +++++++++++++++++++++++++++++++++++++++----------
 qemu-monitor.hx |  620 ++++++++++++++++++++++++++++++++++++++++++++-----------
 qlist.c         |  100 +++++++++
 qlist.h         |   38 ++++
 qobject.h       |    8 +-
 8 files changed, 1232 insertions(+), 242 deletions(-)
#1 lore raw

PATCH v3 1/18 QObject: Accept NULL

It is convenient that QDECREF() and QINCREF() accept the QObject parameter to be NULL, so that we don't duplicate 'if' tests in the callers.

---
 qobject.h |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
qobject.h +3 -4
index 39b8649..dcc8c63 100644
@@ -63,12 +63,10 @@ typedef struct QObject {
 
 /* High-level interface for qobject_incref() */
 #define QINCREF(obj)      \
-    assert(obj != NULL);  \
     qobject_incref(QOBJECT(obj))
 
 /* High-level interface for qobject_decref() */
 #define QDECREF(obj)              \
-    assert(obj != NULL);          \
     qobject_decref(QOBJECT(obj))
 
 /* Initialize an object to default values */
@@ -81,7 +79,8 @@ typedef struct QObject {
  */
 static inline void qobject_incref(QObject *obj)
 {
-    obj->refcnt++;
+    if (obj)
+        obj->refcnt++;
 }
 
 /**
@@ -90,7 +89,7 @@ static inline void qobject_incref(QObject *obj)
  */
 static inline void qobject_decref(QObject *obj)
 {
-    if (--obj->refcnt == 0) {
+    if (obj && --obj->refcnt == 0) {
         assert(obj->type != NULL);
         assert(obj->type->destroy != NULL);
         obj->type->destroy(obj);
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#2 lore raw

PATCH v3 2/18 Introduce QList

In reply to Luiz Capitulino

QList is a high-level data type that can be used to store QObjects in a singly-linked list.

The following functions are available:

- qlist_new() Create a new QList
- qlist_append() Append a QObject to the list
- qlist_iter() Iterate over stored QObjects

---
 Makefile  |    2 +-
 qlist.c   |  100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qlist.h   |   38 +++++++++++++++++++++++
 qobject.h |    1 +
 4 files changed, 140 insertions(+), 1 deletions(-)
 create mode 100644 qlist.c
 create mode 100644 qlist.h
Makefile +1 -1
index c552739..17bcbe4 100644
@@ -100,7 +100,7 @@ obj-y += buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o ssi.o
-obj-y += qint.o qstring.o qdict.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 obj-$(CONFIG_WIN32) += tap-win32.o
qlist.c new file +100 -0

100 lines not on this page · raw message

qlist.h new file +38 -0
new file mode 100644
index 0000000..3eb1eb8
@@ -0,0 +1,38 @@
+/*
+ * QList data type header.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef QLIST_H
+#define QLIST_H
+
+#include "qobject.h"
+#include "qemu-queue.h"
+#include "qemu-common.h"
+
+typedef struct QListEntry {
+    QObject *value;
+    QTAILQ_ENTRY(QListEntry) next;
+} QListEntry;
+
+typedef struct QList {
+    QObject_HEAD;
+    QTAILQ_HEAD(,QListEntry) head;
+} QList;
+
+#define qlist_append(qlist, obj) \
+        qlist_append_obj(qlist, QOBJECT(obj))
+
+QList *qlist_new(void);
+void qlist_append_obj(QList *qlist, QObject *obj);
+void qlist_iter(const QList *qlist,
+                void (*iter)(QObject *obj, void *opaque), void *opaque);
+QList *qobject_to_qlist(const QObject *obj);
+
+#endif /* QLIST_H */
qobject.h +1 -0
index dcc8c63..4cc9287 100644
@@ -40,6 +40,7 @@ typedef enum {
     QTYPE_QINT,
     QTYPE_QSTRING,
     QTYPE_QDICT,
+    QTYPE_QLIST,
 } qtype_code;
 
 struct QObject;
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#3 lore raw

PATCH v3 3/18 Introduce QList unit-tests

In reply to Luiz Capitulino

This suite contains tests to assure that QList API works as expected.

To execute it you should have check installed and build QEMU with check support enabled (--enable-check-utests) and then run:

$ ./check-qlist
---
 Makefile      |    1 +
 check-qlist.c |  153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure     |    2 +-
 3 files changed, 155 insertions(+), 1 deletions(-)
 create mode 100644 check-qlist.c
Makefile +1 -0
index 17bcbe4..d96fb4b 100644
@@ -187,6 +187,7 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 check-qint: check-qint.o qint.o qemu-malloc.o
 check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
+check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-qlist.c new file +153 -0

153 lines not on this page · raw message

configure +1 -1
index ad04880..4ea5483 100755
@@ -2017,7 +2017,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then
   if [ "$linux" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools"
     if [ "$check_utests" = "yes" ]; then
-      tools="check-qint check-qstring check-qdict $tools"
+      tools="check-qint check-qstring check-qdict check-qlist $tools"
     fi
   elif test "$mingw32" = "yes" ; then
       tools="qemu-io\$(EXESUF) $tools"
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#4 lore raw

PATCH v3 4/18 monitor: Convert mon_cmd_t initializations to C99 style

In reply to Luiz Capitulino
---
 monitor.c       |  318 ++++++++++++++++++++++-------
 qemu-monitor.hx |  613 +++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 736 insertions(+), 195 deletions(-)
monitor.c +248 -70
index f79b833..ae0354a 100644
@@ -1813,84 +1813,262 @@ static const mon_cmd_t mon_cmds[] = {
 
 /* Please update qemu-monitor.hx when adding or changing commands */
 static const mon_cmd_t info_cmds[] = {
-    { "version", "", do_info_version,
-      "", "show the version of QEMU" },
-    { "network", "", do_info_network,
-      "", "show the network state" },
-    { "chardev", "", qemu_chr_info,
-      "", "show the character devices" },
-    { "block", "", bdrv_info,
-      "", "show the block devices" },
-    { "blockstats", "", bdrv_info_stats,
-      "", "show block device statistics" },
-    { "registers", "", do_info_registers,
-      "", "show the cpu registers" },
-    { "cpus", "", do_info_cpus,
-      "", "show infos for each CPU" },
-    { "history", "", do_info_history,
-      "", "show the command line history", },
-    { "irq", "", irq_info,
-      "", "show the interrupts statistics (if available)", },
-    { "pic", "", pic_info,
-      "", "show i8259 (PIC) state", },
-    { "pci", "", pci_info,
-      "", "show PCI info", },
+    {
+        .name       = "version",
+        .args_type  = "",
+        .handler    = do_info_version,
+        .params     = "",
+        .help       = "show the version of QEMU",
+    },
+    {
+        .name       = "network",
+        .args_type  = "",
+        .handler    = do_info_network,
+        .params     = "",
+        .help       = "show the network state",
+    },
+    {
+        .name       = "chardev",
+        .args_type  = "",
+        .handler    = qemu_chr_info,
+        .params     = "",
+        .help       = "show the character devices",
+    },
+    {
+        .name       = "block",
+        .args_type  = "",
+        .handler    = bdrv_info,
+        .params     = "",
+        .help       = "show the block devices",
+    },
+    {
+        .name       = "blockstats",
+        .args_type  = "",
+        .handler    = bdrv_info_stats,
+        .params     = "",
+        .help       = "show block device statistics",
+    },
+    {
+        .name       = "registers",
+        .args_type  = "",
+        .handler    = do_info_registers,
+        .params     = "",
+        .help       = "show the cpu registers",
+    },
+    {
+        .name       = "cpus",
+        .args_type  = "",
+        .handler    = do_info_cpus,
+        .params     = "",
+        .help       = "show infos for each CPU",
+    },
+    {
+        .name       = "history",
+        .args_type  = "",
+        .handler    = do_info_history,
+        .params     = "",
+        .help       = "show the command line history",
+    },
+    {
+        .name       = "irq",
+        .args_type  = "",
+        .handler    = irq_info,
+        .params     = "",
+        .help       = "show the interrupts statistics (if available)",
+    },
+    {
+        .name       = "pic",
+        .args_type  = "",
+        .handler    = pic_info,
+        .params     = "",
+        .help       = "show i8259 (PIC) state",
+    },
+    {
+        .name       = "pci",
+        .args_type  = "",
+        .handler    = pci_info,
+        .params     = "",
+        .help       = "show PCI info",
+    },
 #if defined(TARGET_I386) || defined(TARGET_SH4)
-    { "tlb", "", tlb_info,
-      "", "show virtual to physical memory mappings", },
+    {
+        .name       = "tlb",
+        .args_type  = "",
+        .handler    = tlb_info,
+        .params     = "",
+        .help       = "show virtual to physical memory mappings",
+    },
 #endif
 #if defined(TARGET_I386)
-    { "mem", "", mem_info,
-      "", "show the active virtual memory mappings", },
-    { "hpet", "", do_info_hpet,
-      "", "show state of HPET", },
+    {
+        .name       = "mem",
+        .args_type  = "",
+        .handler    = mem_info,
+        .params     = "",
+        .help       = "show the active virtual memory mappings",
+    },
+    {
+        .name       = "hpet",
+        .args_type  = "",
+        .handler    = do_info_hpet,
+        .params     = "",
+        .help       = "show state of HPET",
+    },
 #endif
-    { "jit", "", do_info_jit,
-      "", "show dynamic compiler info", },
-    { "kvm", "", do_info_kvm,
-      "", "show KVM information", },
-    { "numa", "", do_info_numa,
-      "", "show NUMA information", },
-    { "usb", "", usb_info,
-      "", "show guest USB devices", },
-    { "usbhost", "", usb_host_info,
-      "", "show host USB devices", },
-    { "profile", "", do_info_profile,
-      "", "show profiling information", },
-    { "capture", "", do_info_capture,
-      "", "show capture information" },
-    { "snapshots", "", do_info_snapshots,
-      "", "show the currently saved VM snapshots" },
-    { "status", "", do_info_status,
-      "", "show the current VM status (running|paused)" },
-    { "pcmcia", "", pcmcia_info,
-      "", "show guest PCMCIA status" },
-    { "mice", "", do_info_mice,
-      "", "show which guest mouse is receiving events" },
-    { "vnc", "", do_info_vnc,
-      "", "show the vnc server status"},
-    { "name", "", do_info_name,
-      "", "show the current VM name" },
-    { "uuid", "", do_info_uuid,
-      "", "show the current VM UUID" },
+    {
+        .name       = "jit",
+        .args_type  = "",
+        .handler    = do_info_jit,
+        .params     = "",
+        .help       = "show dynamic compiler info",
+    },
+    {
+        .name       = "kvm",
+        .args_type  = "",
+        .handler    = do_info_kvm,
+        .params     = "",
+        .help       = "show KVM information",
+    },
+    {
+        .name       = "numa",
+        .args_type  = "",
+        .handler    = do_info_numa,
+        .params     = "",
+        .help       = "show NUMA information",
+    },
+    {
+        .name       = "usb",
+        .args_type  = "",
+        .handler    = usb_info,
+        .params     = "",
+        .help       = "show guest USB devices",
+    },
+    {
+        .name       = "usbhost",
+        .args_type  = "",
+        .handler    = usb_host_info,
+        .params     = "",
+        .help       = "show host USB devices",
+    },
+    {
+        .name       = "profile",
+        .args_type  = "",
+        .handler    = do_info_profile,
+        .params     = "",
+        .help       = "show profiling information",
+    },
+    {
+        .name       = "capture",
+        .args_type  = "",
+        .handler    = do_info_capture,
+        .params     = "",
+        .help       = "show capture information",
+    },
+    {
+        .name       = "snapshots",
+        .args_type  = "",
+        .handler    = do_info_snapshots,
+        .params     = "",
+        .help       = "show the currently saved VM snapshots",
+    },
+    {
+        .name       = "status",
+        .args_type  = "",
+        .handler    = do_info_status,
+        .params     = "",
+        .help       = "show the current VM status (running|paused)",
+    },
+    {
+        .name       = "pcmcia",
+        .args_type  = "",
+        .handler    = pcmcia_info,
+        .params     = "",
+        .help       = "show guest PCMCIA status",
+    },
+    {
+        .name       = "mice",
+        .args_type  = "",
+        .handler    = do_info_mice,
+        .params     = "",
+        .help       = "show which guest mouse is receiving events",
+    },
+    {
+        .name       = "vnc",
+        .args_type  = "",
+        .handler    = do_info_vnc,
+        .params     = "",
+        .help       = "show the vnc server status",
+    },
+    {
+        .name       = "name",
+        .args_type  = "",
+        .handler    = do_info_name,
+        .params     = "",
+        .help       = "show the current VM name",
+    },
+    {
+        .name       = "uuid",
+        .args_type  = "",
+        .handler    = do_info_uuid,
+        .params     = "",
+        .help       = "show the current VM UUID",
+    },
 #if defined(TARGET_PPC)
-    { "cpustats", "", do_info_cpu_stats,
-      "", "show CPU statistics", },
+    {
+        .name       = "cpustats",
+        .args_type  = "",
+        .handler    = do_info_cpu_stats,
+        .params     = "",
+        .help       = "show CPU statistics",
+    },
 #endif
 #if defined(CONFIG_SLIRP)
-    { "usernet", "", do_info_usernet,
-      "", "show user network stack connection states", },
+    {
+        .name       = "usernet",
+        .args_type  = "",
+        .handler    = do_info_usernet,
+        .params     = "",
+        .help       = "show user network stack connection states",
+    },
 #endif
-    { "migrate", "", do_info_migrate, "", "show migration status" },
-    { "balloon", "", do_info_balloon,
-      "", "show balloon information" },
-    { "qtree", "", do_info_qtree,
-      "", "show device tree" },
-    { "qdm", "", do_info_qdm,
-      "", "show qdev device model list" },
-    { "roms", "", do_info_roms,
-      "", "show roms" },
-    { NULL, NULL, },
+    {
+        .name       = "migrate",
+        .args_type  = "",
+        .handler    = do_info_migrate,
+        .params     = "",
+        .help       = "show migration status",
+    },
+    {
+        .name       = "balloon",
+        .args_type  = "",
+        .handler    = do_info_balloon,
+        .params     = "",
+        .help       = "show balloon information",
+    },
+    {
+        .name       = "qtree",
+        .args_type  = "",
+        .handler    = do_info_qtree,
+        .params     = "",
+        .help       = "show device tree",
+    },
+    {
+        .name       = "qdm",
+        .args_type  = "",
+        .handler    = do_info_qdm,
+        .params     = "",
+        .help       = "show qdev device model list",
+    },
+    {
+        .name       = "roms",
+        .args_type  = "",
+        .handler    = do_info_roms,
+        .params     = "",
+        .help       = "show roms",
+    },
+    {
+        .name       = NULL,
+    },
 };
 
 /*******************************************************************/
qemu-monitor.hx +488 -125

947 lines not on this page · raw message

Signature · 1 line
1.6.5.rc2.17.gdbc1b
#5 lore raw

PATCH v3 5/18 monitor: union for info handlers

In reply to Luiz Capitulino

This commit adds a union to mon_cmd_t for info handlers and converts do_info() and info_cmds[] array to use it.

This improves type safety.

Next commit will convert command handlers.

---
 monitor.c |   77 +++++++++++++++++++++++++++++++------------------------------
 1 files changed, 39 insertions(+), 38 deletions(-)
monitor.c +39 -38
index ae0354a..c0569d5 100644
@@ -74,6 +74,9 @@ typedef struct mon_cmd_t {
     void *handler;
     const char *params;
     const char *help;
+    union {
+        void (*info)(Monitor *mon);
+    } mhandler;
 } mon_cmd_t;
 
 /* file descriptors passed via SCM_RIGHTS */
@@ -283,7 +286,6 @@ static void do_info(Monitor *mon, const QDict *qdict)
 {
     const mon_cmd_t *cmd;
     const char *item = qdict_get_try_str(qdict, "item");
-    void (*handler)(Monitor *);
 
     if (!item)
         goto help;
@@ -295,8 +297,7 @@ static void do_info(Monitor *mon, const QDict *qdict)
     help_cmd(mon, "info");
     return;
  found:
-    handler = cmd->handler;
-    handler(mon);
+    cmd->mhandler.info(mon);
 }
 
 static void do_info_version(Monitor *mon)
@@ -1816,255 +1817,255 @@ static const mon_cmd_t info_cmds[] = {
     {
         .name       = "version",
         .args_type  = "",
-        .handler    = do_info_version,
         .params     = "",
         .help       = "show the version of QEMU",
+        .mhandler.info = do_info_version,
     },
     {
         .name       = "network",
         .args_type  = "",
-        .handler    = do_info_network,
         .params     = "",
         .help       = "show the network state",
+        .mhandler.info = do_info_network,
     },
     {
         .name       = "chardev",
         .args_type  = "",
-        .handler    = qemu_chr_info,
         .params     = "",
         .help       = "show the character devices",
+        .mhandler.info = qemu_chr_info,
     },
     {
         .name       = "block",
         .args_type  = "",
-        .handler    = bdrv_info,
         .params     = "",
         .help       = "show the block devices",
+        .mhandler.info = bdrv_info,
     },
     {
         .name       = "blockstats",
         .args_type  = "",
-        .handler    = bdrv_info_stats,
         .params     = "",
         .help       = "show block device statistics",
+        .mhandler.info = bdrv_info_stats,
     },
     {
         .name       = "registers",
         .args_type  = "",
-        .handler    = do_info_registers,
         .params     = "",
         .help       = "show the cpu registers",
+        .mhandler.info = do_info_registers,
     },
     {
         .name       = "cpus",
         .args_type  = "",
-        .handler    = do_info_cpus,
         .params     = "",
         .help       = "show infos for each CPU",
+        .mhandler.info = do_info_cpus,
     },
     {
         .name       = "history",
         .args_type  = "",
-        .handler    = do_info_history,
         .params     = "",
         .help       = "show the command line history",
+        .mhandler.info = do_info_history,
     },
     {
         .name       = "irq",
         .args_type  = "",
-        .handler    = irq_info,
         .params     = "",
         .help       = "show the interrupts statistics (if available)",
+        .mhandler.info = irq_info,
     },
     {
         .name       = "pic",
         .args_type  = "",
-        .handler    = pic_info,
         .params     = "",
         .help       = "show i8259 (PIC) state",
+        .mhandler.info = pic_info,
     },
     {
         .name       = "pci",
         .args_type  = "",
-        .handler    = pci_info,
         .params     = "",
         .help       = "show PCI info",
+        .mhandler.info = pci_info,
     },
 #if defined(TARGET_I386) || defined(TARGET_SH4)
     {
         .name       = "tlb",
         .args_type  = "",
-        .handler    = tlb_info,
         .params     = "",
         .help       = "show virtual to physical memory mappings",
+        .mhandler.info = tlb_info,
     },
 #endif
 #if defined(TARGET_I386)
     {
         .name       = "mem",
         .args_type  = "",
-        .handler    = mem_info,
         .params     = "",
         .help       = "show the active virtual memory mappings",
+        .mhandler.info = mem_info,
     },
     {
         .name       = "hpet",
         .args_type  = "",
-        .handler    = do_info_hpet,
         .params     = "",
         .help       = "show state of HPET",
+        .mhandler.info = do_info_hpet,
     },
 #endif
     {
         .name       = "jit",
         .args_type  = "",
-        .handler    = do_info_jit,
         .params     = "",
         .help       = "show dynamic compiler info",
+        .mhandler.info = do_info_jit,
     },
     {
         .name       = "kvm",
         .args_type  = "",
-        .handler    = do_info_kvm,
         .params     = "",
         .help       = "show KVM information",
+        .mhandler.info = do_info_kvm,
     },
     {
         .name       = "numa",
         .args_type  = "",
-        .handler    = do_info_numa,
         .params     = "",
         .help       = "show NUMA information",
+        .mhandler.info = do_info_numa,
     },
     {
         .name       = "usb",
         .args_type  = "",
-        .handler    = usb_info,
         .params     = "",
         .help       = "show guest USB devices",
+        .mhandler.info = usb_info,
     },
     {
         .name       = "usbhost",
         .args_type  = "",
-        .handler    = usb_host_info,
         .params     = "",
         .help       = "show host USB devices",
+        .mhandler.info = usb_host_info,
     },
     {
         .name       = "profile",
         .args_type  = "",
-        .handler    = do_info_profile,
         .params     = "",
         .help       = "show profiling information",
+        .mhandler.info = do_info_profile,
     },
     {
         .name       = "capture",
         .args_type  = "",
-        .handler    = do_info_capture,
         .params     = "",
         .help       = "show capture information",
+        .mhandler.info = do_info_capture,
     },
     {
         .name       = "snapshots",
         .args_type  = "",
-        .handler    = do_info_snapshots,
         .params     = "",
         .help       = "show the currently saved VM snapshots",
+        .mhandler.info = do_info_snapshots,
     },
     {
         .name       = "status",
         .args_type  = "",
-        .handler    = do_info_status,
         .params     = "",
         .help       = "show the current VM status (running|paused)",
+        .mhandler.info = do_info_status,
     },
     {
         .name       = "pcmcia",
         .args_type  = "",
-        .handler    = pcmcia_info,
         .params     = "",
         .help       = "show guest PCMCIA status",
+        .mhandler.info = pcmcia_info,
     },
     {
         .name       = "mice",
         .args_type  = "",
-        .handler    = do_info_mice,
         .params     = "",
         .help       = "show which guest mouse is receiving events",
+        .mhandler.info = do_info_mice,
     },
     {
         .name       = "vnc",
         .args_type  = "",
-        .handler    = do_info_vnc,
         .params     = "",
         .help       = "show the vnc server status",
+        .mhandler.info = do_info_vnc,
     },
     {
         .name       = "name",
         .args_type  = "",
-        .handler    = do_info_name,
         .params     = "",
         .help       = "show the current VM name",
+        .mhandler.info = do_info_name,
     },
     {
         .name       = "uuid",
         .args_type  = "",
-        .handler    = do_info_uuid,
         .params     = "",
         .help       = "show the current VM UUID",
+        .mhandler.info = do_info_uuid,
     },
 #if defined(TARGET_PPC)
     {
         .name       = "cpustats",
         .args_type  = "",
-        .handler    = do_info_cpu_stats,
         .params     = "",
         .help       = "show CPU statistics",
+        .mhandler.info = do_info_cpu_stats,
     },
 #endif
 #if defined(CONFIG_SLIRP)
     {
         .name       = "usernet",
         .args_type  = "",
-        .handler    = do_info_usernet,
         .params     = "",
         .help       = "show user network stack connection states",
+        .mhandler.info = do_info_usernet,
     },
 #endif
     {
         .name       = "migrate",
         .args_type  = "",
-        .handler    = do_info_migrate,
         .params     = "",
         .help       = "show migration status",
+        .mhandler.info = do_info_migrate,
     },
     {
         .name       = "balloon",
         .args_type  = "",
-        .handler    = do_info_balloon,
         .params     = "",
         .help       = "show balloon information",
+        .mhandler.info = do_info_balloon,
     },
     {
         .name       = "qtree",
         .args_type  = "",
-        .handler    = do_info_qtree,
         .params     = "",
         .help       = "show device tree",
+        .mhandler.info = do_info_qtree,
     },
     {
         .name       = "qdm",
         .args_type  = "",
-        .handler    = do_info_qdm,
         .params     = "",
         .help       = "show qdev device model list",
+        .mhandler.info = do_info_qdm,
     },
     {
         .name       = "roms",
         .args_type  = "",
-        .handler    = do_info_roms,
         .params     = "",
         .help       = "show roms",
+        .mhandler.info = do_info_roms,
     },
     {
         .name       = NULL,
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#6 lore raw

PATCH v3 6/18 monitor: union for command handlers

In reply to Luiz Capitulino

This commits adds a new union member to mon_cmd_t for command handlers and convert monitor_handle_command() and qemu-monitor.hx to use it.

This improves type safety.

---
 monitor.c       |    9 +---
 qemu-monitor.hx |  122 +++++++++++++++++++++++++++---------------------------
 2 files changed, 63 insertions(+), 68 deletions(-)
monitor.c +2 -7
index c0569d5..2bf82f3 100644
@@ -71,11 +71,11 @@
 typedef struct mon_cmd_t {
     const char *name;
     const char *args_type;
-    void *handler;
     const char *params;
     const char *help;
     union {
         void (*info)(Monitor *mon);
+        void (*cmd)(Monitor *mon, const QDict *qdict);
     } mhandler;
 } mon_cmd_t;
 
@@ -3014,13 +3014,8 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
     if (cmd) {
-        void (*handler)(Monitor *mon, const QDict *qdict);
-
         qemu_errors_to_mon(mon);
-
-        handler = cmd->handler;
-        handler(mon, qdict);
-
+        cmd->mhandler.cmd(mon, qdict);
         qemu_errors_to_previous();
     }
 
qemu-monitor.hx +61 -61

613 lines not on this page · raw message

Signature · 1 line
1.6.5.rc2.17.gdbc1b
#7 lore raw

PATCH v3 7/18 monitor: Add user_print() to mon_cmd_t

In reply to Luiz Capitulino

This new struct member will store a pointer to a function that should be used to output data in the user protocol format.

It will also serve as a flag to say if a given handler has already been converted to the new QObject style.

---
 monitor.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
monitor.c +1 -0
index 2bf82f3..b589d98 100644
@@ -73,6 +73,7 @@ typedef struct mon_cmd_t {
     const char *args_type;
     const char *params;
     const char *help;
+    void (*user_print)(Monitor *mon, const QObject *data);
     union {
         void (*info)(Monitor *mon);
         void (*cmd)(Monitor *mon, const QDict *qdict);
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#8 lore raw

PATCH v3 8/18 monitor: Handle new and old style handlers

In reply to Luiz Capitulino

This commit changes monitor_handle_command() to support old style _and_ new style handlers.

New style handlers are protocol independent, they return their data to the Monitor, which in turn decides how to print them (ie. user protocol vs. machine protocol).

Converted handlers will use the 'user_print' member of 'mon_cmd_t' to define its user protocol function, which will be called to print data in the user protocol format.

Handlers which don't have 'user_print' defined are not converted and are handled as usual.

---
 monitor.c |   26 +++++++++++++++++++++++---
 1 files changed, 23 insertions(+), 3 deletions(-)
monitor.c +23 -3
index b589d98..d545124 100644
@@ -77,6 +77,7 @@ typedef struct mon_cmd_t {
     union {
         void (*info)(Monitor *mon);
         void (*cmd)(Monitor *mon, const QDict *qdict);
+        void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
     } mhandler;
 } mon_cmd_t;
 
@@ -214,6 +215,11 @@ static int monitor_fprintf(FILE *stream, const char *fmt, ...)
     return 0;
 }
 
+static inline int monitor_handler_ported(const mon_cmd_t *cmd)
+{
+    return cmd->user_print != NULL;
+}
+
 static int compare_cmd(const char *name, const char *list)
 {
     const char *p, *pstart;
@@ -3014,12 +3020,26 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
     qdict = qdict_new();
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
-    if (cmd) {
-        qemu_errors_to_mon(mon);
+    if (!cmd)
+        goto out;
+
+    qemu_errors_to_mon(mon);
+
+    if (monitor_handler_ported(cmd)) {
+        QObject *data = NULL;
+
+        cmd->mhandler.cmd_new(mon, qdict, &data);
+        if (data)
+            cmd->user_print(mon, data);
+
+        qobject_decref(data);
+    } else {
         cmd->mhandler.cmd(mon, qdict);
-        qemu_errors_to_previous();
     }
 
+   qemu_errors_to_previous();
+
+out:
     QDECREF(qdict);
 }
 
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#9 lore raw

PATCH v3 9/18 monitor: do_info(): handle new and old info handlers

In reply to Luiz Capitulino

do_info() is special, its job is to call 'info handlers'. This is similar to what monitor_handle_command() does, therefore do_info() also has to distinguish among new and old style info handlers.

This commit converts do_info() to the new QObject style and makes the appropriate changes so that it can handle both info handlers styles.

In the future, when all handlers are converted to QObject's style, it will be possible to share more code with monitor_handle_command().

This commit also introduces a new function called monitor_user_noop(), it should be used by handlers which do not have data to print.

This is the case of do_info().

---
 monitor.c       |   29 ++++++++++++++++++++++-------
 qemu-monitor.hx |    3 ++-
 2 files changed, 24 insertions(+), 8 deletions(-)
monitor.c +22 -7
index d545124..cc1b501 100644
@@ -76,6 +76,7 @@ typedef struct mon_cmd_t {
     void (*user_print)(Monitor *mon, const QObject *data);
     union {
         void (*info)(Monitor *mon);
+        void (*info_new)(Monitor *mon, QObject **ret_data);
         void (*cmd)(Monitor *mon, const QDict *qdict);
         void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
     } mhandler;
@@ -215,6 +216,8 @@ static int monitor_fprintf(FILE *stream, const char *fmt, ...)
     return 0;
 }
 
+static void monitor_user_noop(Monitor *mon, const QObject *data) { }
+
 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
 {
     return cmd->user_print != NULL;
@@ -289,22 +292,34 @@ static void do_commit(Monitor *mon, const QDict *qdict)
     }
 }
 
-static void do_info(Monitor *mon, const QDict *qdict)
+static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     const mon_cmd_t *cmd;
     const char *item = qdict_get_try_str(qdict, "item");
 
     if (!item)
         goto help;
-    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
+
+    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
         if (compare_cmd(item, cmd->name))
-            goto found;
+            break;
     }
- help:
-    help_cmd(mon, "info");
+
+    if (cmd->name == NULL)
+        goto help;
+
+    if (monitor_handler_ported(cmd)) {
+        cmd->mhandler.info_new(mon, ret_data);
+        if (*ret_data)
+            cmd->user_print(mon, *ret_data);
+    } else {
+        cmd->mhandler.info(mon);
+    }
+
     return;
- found:
-    cmd->mhandler.info(mon);
+
+help:
+    help_cmd(mon, "info");
 }
 
 static void do_info_version(Monitor *mon)
qemu-monitor.hx +2 -1
index c689a07..c37b0cb 100644
@@ -40,7 +40,8 @@ ETEXI
         .args_type  = "item:s?",
         .params     = "[subcommand]",
         .help       = "show various information about the system state",
-        .mhandler.cmd = do_info,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_info,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#10 lore raw

PATCH v3 10/18 monitor: Convert do_quit() do QObject

In reply to Luiz Capitulino
---
 monitor.c       |    5 ++++-
 qemu-monitor.hx |    3 ++-
 2 files changed, 6 insertions(+), 2 deletions(-)
monitor.c +4 -1
index cc1b501..4ba415c 100644
@@ -457,7 +457,10 @@ static void do_info_cpu_stats(Monitor *mon)
 }
 #endif
 
-static void do_quit(Monitor *mon, const QDict *qdict)
+/**
+ * do_quit(): Quit QEMU execution
+ */
+static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     exit(0);
 }
qemu-monitor.hx +2 -1
index c37b0cb..51c7673 100644
@@ -119,7 +119,8 @@ ETEXI
         .args_type  = "",
         .params     = "",
         .help       = "quit the emulator",
-        .mhandler.cmd = do_quit,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_quit,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#11 lore raw

PATCH v3 11/18 monitor: Convert do_stop() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    5 ++++-
 qemu-monitor.hx |    3 ++-
 2 files changed, 6 insertions(+), 2 deletions(-)
monitor.c +4 -1
index 4ba415c..ae0a567 100644
@@ -599,7 +599,10 @@ static void do_singlestep(Monitor *mon, const QDict *qdict)
     }
 }
 
-static void do_stop(Monitor *mon, const QDict *qdict)
+/**
+ * do_stop(): Stop VM execution
+ */
+static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     vm_stop(EXCP_INTERRUPT);
 }
qemu-monitor.hx +2 -1
index 51c7673..8fcba24 100644
@@ -287,7 +287,8 @@ ETEXI
         .args_type  = "",
         .params     = "",
         .help       = "stop emulation",
-        .mhandler.cmd = do_stop,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_stop,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#12 lore raw

PATCH v3 12/18 monitor: Convert do_system_reset() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    6 +++++-
 qemu-monitor.hx |    3 ++-
 2 files changed, 7 insertions(+), 2 deletions(-)
monitor.c +5 -1
index ae0a567..3209875 100644
@@ -1292,7 +1292,11 @@ static void do_boot_set(Monitor *mon, const QDict *qdict)
     }
 }
 
-static void do_system_reset(Monitor *mon, const QDict *qdict)
+/**
+ * do_system_reset(): Issue a machine reset
+ */
+static void do_system_reset(Monitor *mon, const QDict *qdict,
+                            QObject **ret_data)
 {
     qemu_system_reset_request();
 }
qemu-monitor.hx +2 -1
index 8fcba24..b116b5e 100644
@@ -467,7 +467,8 @@ ETEXI
         .args_type  = "",
         .params     = "",
         .help       = "reset the system",
-        .mhandler.cmd = do_system_reset,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_system_reset,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#13 lore raw

PATCH v3 13/18 monitor: Convert do_system_powerdown() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    6 +++++-
 qemu-monitor.hx |    3 ++-
 2 files changed, 7 insertions(+), 2 deletions(-)
monitor.c +5 -1
index 3209875..ab15289 100644
@@ -1301,7 +1301,11 @@ static void do_system_reset(Monitor *mon, const QDict *qdict,
     qemu_system_reset_request();
 }
 
-static void do_system_powerdown(Monitor *mon, const QDict *qdict)
+/**
+ * do_system_powerdown(): Issue a machine powerdown
+ */
+static void do_system_powerdown(Monitor *mon, const QDict *qdict,
+                                QObject **ret_data)
 {
     qemu_system_powerdown_request();
 }
qemu-monitor.hx +2 -1
index b116b5e..3efc716 100644
@@ -482,7 +482,8 @@ ETEXI
         .args_type  = "",
         .params     = "",
         .help       = "send system power down event",
-        .mhandler.cmd = do_system_powerdown,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_system_powerdown,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#14 lore raw

PATCH v3 14/18 monitor: Convert do_cont() to QObject

In reply to Luiz Capitulino

Appropriate error handling support will be needed to have encrypted images working under the future machine protocol, but this initial conversion will work with the current user protocol.

---
 monitor.c       |    7 +++++--
 qemu-monitor.hx |    3 ++-
 2 files changed, 7 insertions(+), 3 deletions(-)
monitor.c +5 -2
index ab15289..d393ad5 100644
@@ -614,7 +614,10 @@ struct bdrv_iterate_context {
     int err;
 };
 
-static void do_cont(Monitor *mon, const QDict *qdict)
+/**
+ * do_cont(): Resume emulation.
+ */
+static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     struct bdrv_iterate_context context = { mon, 0 };
 
@@ -630,7 +633,7 @@ static void bdrv_key_cb(void *opaque, int err)
 
     /* another key was set successfully, retry to continue */
     if (!err)
-        do_cont(mon, NULL);
+        do_cont(mon, NULL, NULL);
 }
 
 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
qemu-monitor.hx +2 -1
index 3efc716..8dca4f1 100644
@@ -301,7 +301,8 @@ ETEXI
         .args_type  = "",
         .params     = "",
         .help       = "resume emulation",
-        .mhandler.cmd = do_cont,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_cont,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#15 lore raw

PATCH v3 15/18 monitor: Convert do_balloon() to QObject

In reply to Luiz Capitulino

It is important to note that it never fails, as big refactoring of the virtio code would be needed to get the proper error code.

---
 monitor.c       |    6 ++++--
 qemu-monitor.hx |    3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
monitor.c +4 -2
index d393ad5..22e87ab 100644
@@ -1602,8 +1602,10 @@ static void do_info_status(Monitor *mon)
        monitor_printf(mon, "VM status: paused\n");
 }
 
-
-static void do_balloon(Monitor *mon, const QDict *qdict)
+/**
+ * do_balloon(): Request VM to change its memory allocation
+ */
+static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     int value = qdict_get_int(qdict, "value");
     ram_addr_t target = value;
qemu-monitor.hx +2 -1
index 8dca4f1..29999c6 100644
@@ -874,7 +874,8 @@ ETEXI
         .args_type  = "value:i",
         .params     = "target",
         .help       = "request VM to change it's memory allocation (in MB)",
-        .mhandler.cmd = do_balloon,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_balloon,
     },
 
 STEXI
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#16 lore raw

PATCH v3 16/18 monitor: Convert do_info_version() to QObject

In reply to Luiz Capitulino

The returned data is always a QString.

Also introduces monitor_print_qobject(), which can be used as a standard way to print QObjects in the user protocol format.

---
 monitor.c |   28 +++++++++++++++++++++++++---
 1 files changed, 25 insertions(+), 3 deletions(-)
monitor.c +25 -3
index 22e87ab..8c9cc9b 100644
@@ -223,6 +223,24 @@ static inline int monitor_handler_ported(const mon_cmd_t *cmd)
     return cmd->user_print != NULL;
 }
 
+static void monitor_print_qobject(Monitor *mon, const QObject *data)
+{
+    switch (qobject_type(data)) {
+        case QTYPE_QSTRING:
+            monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
+            break;
+        case QTYPE_QINT:
+            monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
+            break;
+        default:
+            monitor_printf(mon, "ERROR: unsupported type: %d",
+                                                        qobject_type(data));
+            break;
+    }
+
+    monitor_puts(mon, "\n");
+}
+
 static int compare_cmd(const char *name, const char *list)
 {
     const char *p, *pstart;
@@ -322,9 +340,12 @@ help:
     help_cmd(mon, "info");
 }
 
-static void do_info_version(Monitor *mon)
+/**
+ * do_info_version(): Show QEMU version
+ */
+static void do_info_version(Monitor *mon, QObject **ret_data)
 {
-    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
+    *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
 }
 
 static void do_info_name(Monitor *mon)
@@ -1860,7 +1881,8 @@ static const mon_cmd_t info_cmds[] = {
         .args_type  = "",
         .params     = "",
         .help       = "show the version of QEMU",
-        .mhandler.info = do_info_version,
+        .user_print = monitor_print_qobject,
+        .mhandler.info_new = do_info_version,
     },
     {
         .name       = "network",
Signature · 1 line
1.6.5.rc2.17.gdbc1b