Skip to contentSkip to conversation

: Initial QObject conversion

PATCH Superseded Luiz Capitulino 2 participants 24 messages Oct 7, 2009 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover : Initial QObject conversion

Hi there,

Here goes another new version of my initial QObject conversion work.

I'm taking a different approach in this series. Instead of introducing
the error handling code along with the initial conversions, I'm only doing the QObject conversion part.

This means that error information is still directly printed by the
command handler, but 'regular' data is returned to the Monitor in the form of QObjects.

This approach makes the series easier to:

1. Review, as we can concentrate only on the QObject conversion
2. Convert & merge, as we can do only one thing at a time
3. Test, for the same reason as in '2'

The disadvantage is that we will need a 'second pass conversion' to
convert the error handling part, which can only be done when I finish the QError data type I'm writing.

This is not a big issue anyway, as only two of nine conversions in
this series can fail.

The following handlers are being converted: do_quit(), do_stop(),
do_system_reset(), do_system_powerdown(), do_cont(), do_balloon(), do_info_version(), do_info_balloon(), do_info_cpus().

Also, I'm introducing QList as it's needed by the do_info_cpus()
conversion.

I've done a full build of this series on F11 (x86_64) and
Debian Lenny (i386), manually tested all conversions and some other commands.

Changelog:

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       |  599 +++++++++++++++++++++++++++++++++++++++---------
 qemu-monitor.hx |  674 ++++++++++++++++++++++++++++++++++++++++++++----------
 qlist.c         |  100 ++++++++
 qlist.h         |   27 +++
 qobject.h       |    8 +-
 8 files changed, 1322 insertions(+), 244 deletions(-)
#1 lore raw

PATCH v2 1/15 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 v2 2/15 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   |   27 ++++++++++++++++
 qobject.h |    1 +
 4 files changed, 129 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 +27 -0
new file mode 100644
index 0000000..b38786e
@@ -0,0 +1,27 @@
+#ifndef QLIST
+#define QLIST
+
+#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 */
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

[Qemu-devel] Re: [PATCH 02/15] Introduce QList

Quoted 19 lines · 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   |   27 ++++++++++++++++
 qobject.h |    1 +
 4 files changed, 129 insertions(+), 1 deletions(-)
 create mode 100644 qlist.c
 create mode 100644 qlist.h

<snip>

Quoted 16 lines
qlist.c new file +15 -0
+/**
+ * qlist_iter(): Iterate over all the list's stored values.
+ *
+ * This function allows the user to provide an iterator, which will be
+ * called for each stored value in the list.
+ */
+void qlist_iter(const QList *qlist,
+                void (*iter)(QObject *obj, void *opaque), void *opaque)
+{
+    QListEntry *entry;
+
+    QTAILQ_FOREACH(entry, &qlist->head, next)
+        iter(entry->value, opaque);
+}
+

qlist.c line 75

Quoted 10 lines
qlist.c new file +10 -0
+/**
+ * qobject_to_qlist(): Convert a QObject into a QList
+ */
+QList *qobject_to_qlist(const QObject *obj)
+{
+    if (qobject_type(obj) != QTYPE_QLIST)
+        return NULL;
+
+    return container_of(obj, QList, base);
+}

Missing {} around ifs.

qlist.c line 85

Quoted 11 lines
qlist.c new file +10 -0
+
+/**
+ * qlist_destroy_obj(): Free all the memory allocated by a QList
+ */
+static void qlist_destroy_obj(QObject *obj)
+{
+    QList *qlist;
+    QListEntry *entry, *next_entry;
+
+    assert(obj != NULL);

Usually accepting NULL in a free function makes for nicer exit paths in function.

Quoted 6 lines
qlist.c new file +5 -0
+    qlist = qobject_to_qlist(obj);
+
+    QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
+        QTAILQ_REMOVE(&qlist->head, entry, next);
+        qobject_decref(entry->value);

qlist.c line 100

Quoted 10 lines
qlist.c new file +10 -0
+        qemu_free(entry);
+    }
+
+    qemu_free(qlist);
+}
+
+static const QType qlist_type = {
+    .code = QTYPE_QLIST,
+    .destroy = qlist_destroy_obj,
+};

Forward definitions of statics usually indicate code motion is in order. In this case, I think it's probably a good idea as my first reaction to this was, gees, this has to be dead code since it's a static at the end of a file.

qlist.h

Quoted 7 lines
qlist.h new file +0 -0
new file mode 100644
index 0000000..b38786e
@@ -0,0 +1,27 @@

Missing copyright/license.

qlist.h line 2

Quoted 3 lines
qlist.h new file +2 -0
+#ifndef QLIST
+#define QLIST

Should probably at least do QLIST_H to avoid namespace polution.

Signature · 3 lines
Regards,

Anthony Liguori
author Applied
#4 lore raw

[Qemu-devel] Re: [PATCH 02/15] Introduce QList

Quoted 1 line · On Tue, 06 Oct 2009 20:37:43 -0500 Anthony Liguori <[email protected]> wrote:

Luiz Capitulino wrote:

[...]

Quoted 5 lines
Quoted 5 lines
qlist.c new file +5 -0
+/**
+ * qlist_destroy_obj(): Free all the memory allocated by a QList
+ */
+static void qlist_destroy_obj(QObject *obj)
+{
Quoted 8 lines

qlist.c line 85

Quoted 5 lines
qlist.c new file +4 -0
+    QList *qlist;
+    QListEntry *entry, *next_entry;
+
+    assert(obj != NULL);

Usually accepting NULL in a free function makes for nicer exit paths in function.

The destroy_obj() functions are always called by qobject_decref()
and it will never pass a NULL qobject.

But qobject_decref() accepts NULL.

I've applied the other changes, some of them also need to be done
in the others data types (will send patches).

#5 lore raw

PATCH v2 3/15 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
#6 lore raw

PATCH v2 4/15 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.

Additionally, this commit converts mon_cmd_t static initializations to the C99 way.

---
 monitor.c       |  359 ++++++++++++++++++++++++------
 qemu-monitor.hx |  674 ++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 838 insertions(+), 195 deletions(-)
monitor.c +289 -70
index f79b833..9d4c168 100644
@@ -72,6 +72,7 @@ typedef struct mon_cmd_t {
     const char *name;
     const char *args_type;
     void *handler;
+    void (*user_print)(Monitor *mon, const QObject *data);
     const char *params;
     const char *help;
 } mon_cmd_t;
@@ -1813,84 +1814,302 @@ 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,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the version of QEMU"
+    },
+    {
+        .name       = "network",
+        .args_type  = "",
+        .handler    = do_info_network,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the network state"
+    },
+    {
+        .name       = "chardev",
+        .args_type  = "",
+        .handler    = qemu_chr_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the character devices"
+    },
+    {
+        .name       = "block",
+        .args_type  = "",
+        .handler    = bdrv_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the block devices"
+    },
+    {
+        .name       = "blockstats",
+        .args_type  = "",
+        .handler    = bdrv_info_stats,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show block device statistics"
+    },
+    {
+        .name       = "registers",
+        .args_type  = "",
+        .handler    = do_info_registers,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the cpu registers"
+    },
+    {
+        .name       = "cpus",
+        .args_type  = "",
+        .handler    = do_info_cpus,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show infos for each CPU"
+    },
+    {
+        .name       = "history",
+        .args_type  = "",
+        .handler    = do_info_history,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the command line history"
+    },
+    {
+        .name       = "irq",
+        .args_type  = "",
+        .handler    = irq_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the interrupts statistics (if available)"
+    },
+    {
+        .name       = "pic",
+        .args_type  = "",
+        .handler    = pic_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show i8259 (PIC) state"
+    },
+    {
+        .name       = "pci",
+        .args_type  = "",
+        .handler    = pci_info,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the active virtual memory mappings"
+    },
+    {
+        .name       = "hpet",
+        .args_type  = "",
+        .handler    = do_info_hpet,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show dynamic compiler info"
+    },
+    {
+        .name       = "kvm",
+        .args_type  = "",
+        .handler    = do_info_kvm,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show KVM information"
+    },
+    {
+        .name       = "numa",
+        .args_type  = "",
+        .handler    = do_info_numa,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show NUMA information"
+    },
+    {
+        .name       = "usb",
+        .args_type  = "",
+        .handler    = usb_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show guest USB devices"
+    },
+    {
+        .name       = "usbhost",
+        .args_type  = "",
+        .handler    = usb_host_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show host USB devices"
+    },
+    {
+        .name       = "profile",
+        .args_type  = "",
+        .handler    = do_info_profile,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show profiling information"
+    },
+    {
+        .name       = "capture",
+        .args_type  = "",
+        .handler    = do_info_capture,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show capture information"
+    },
+    {
+        .name       = "snapshots",
+        .args_type  = "",
+        .handler    = do_info_snapshots,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the currently saved VM snapshots"
+    },
+    {
+        .name       = "status",
+        .args_type  = "",
+        .handler    = do_info_status,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the current VM status (running|paused)"
+    },
+    {
+        .name       = "pcmcia",
+        .args_type  = "",
+        .handler    = pcmcia_info,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show guest PCMCIA status"
+    },
+    {
+        .name       = "mice",
+        .args_type  = "",
+        .handler    = do_info_mice,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show which guest mouse is receiving events"
+    },
+    {
+        .name       = "vnc",
+        .args_type  = "",
+        .handler    = do_info_vnc,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the vnc server status"
+    },
+    {
+        .name       = "name",
+        .args_type  = "",
+        .handler    = do_info_name,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the current VM name"
+    },
+    {
+        .name       = "uuid",
+        .args_type  = "",
+        .handler    = do_info_uuid,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .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,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show migration status"
+    },
+    {
+        .name       = "balloon",
+        .args_type  = "",
+        .handler    = do_info_balloon,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show balloon information"
+    },
+    {
+        .name       = "qtree",
+        .args_type  = "",
+        .handler    = do_info_qtree,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show device tree"
+    },
+    {
+        .name       = "qdm",
+        .args_type  = "",
+        .handler    = do_info_qdm,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show qdev device model list"
+    },
+    {
+        .name       = "roms",
+        .args_type  = "",
+        .handler    = do_info_roms,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show roms"
+    },
+    {
+        .name       = NULL,
+        .args_type  = NULL,
+        .handler    = NULL,
+        .user_print = NULL,
+        .params     = NULL,
+        .help       = NULL
+    },
 };
 
 /*******************************************************************/
qemu-monitor.hx +549 -125

1,008 lines not on this page · raw message

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

[Qemu-devel] Re: [PATCH 04/15] monitor: Add user_print() to mon_cmd_t

Quoted 9 lines · 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.

Additionally, this commit converts mon_cmd_t static initializations to the C99 way.

This should really be two patches. One that does the mon_cmd_t init conversion and then another that introduces user_print. This would be easier if...

<snip>

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1824

Quoted 9 lines
monitor.c +8 -0
+    {
+        .name       = "version",
+        .args_type  = "",
+        .handler    = do_info_version,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the version of QEMU"
+    },

You didn't explicitly initialize things to NULL and made sure that args_types = "" worked the same as args_type = NULL (and likewise for params. This would help de-uglify what's happening in this file :-)

Signature · 3 lines
Regards,

Anthony Liguori
#8 lore raw

[Qemu-devel] Re: [PATCH 04/15] monitor: Add user_print() to mon_cmd_t

Quoted 7 lines · On Tue, 06 Oct 2009 20:40:31 -0500 Anthony Liguori <[email protected]> wrote:
Quoted 6 lines · 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.

Quoted 7 lines
Quoted 3 lines

Additionally, this commit converts mon_cmd_t static initializations to the C99 way.

This should really be two patches. One that does the mon_cmd_t init conversion and then another that introduces user_print. This would be easier if...

Ok, will split.

Quoted 11 lines

<snip>

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1824

Quoted 9 lines
monitor.c +8 -0
+    {
+        .name       = "version",
+        .args_type  = "",
+        .handler    = do_info_version,
+        .user_print = NULL,
+        .params     = "",
+        .help       = "show the version of QEMU"
+    },
Quoted 3 lines

You didn't explicitly initialize things to NULL and made sure that args_types = "" worked the same as args_type = NULL (and likewise for params. This would help de-uglify what's happening in this file :-)

I think this work is part of the patch series which will change
args_type to be an array, as suggested by Markus.

This is series is just an initial help.

#9 lore raw

PATCH v2 5/15 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 |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)
monitor.c +26 -6
index 9d4c168..c0ba5ee 100644
@@ -211,6 +211,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;
@@ -3053,17 +3058,32 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
     qdict = qdict_new();
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
-    if (cmd) {
-        void (*handler)(Monitor *mon, const QDict *qdict);
+    if (!cmd)
+        goto out;
+
+    qemu_errors_to_mon(mon);
 
-        qemu_errors_to_mon(mon);
+    if (monitor_handler_ported(cmd)) {
+        QObject *data = NULL;
+        void (*handler_new)(Monitor *mon, const QDict *params,
+                            QObject **ret_data);
 
-        handler = cmd->handler;
-        handler(mon, qdict);
+        handler_new = cmd->handler;
+        handler_new(mon, qdict, &data);
 
-        qemu_errors_to_previous();
+        if (data)
+            cmd->user_print(mon, data);
+
+        qobject_decref(data);
+    } else {
+        void (*handler_old)(Monitor *mon, const QDict *qdict);
+        handler_old = cmd->handler;
+        handler_old(mon, qdict);
     }
 
+   qemu_errors_to_previous();
+
+out:
     QDECREF(qdict);
 }
 
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#10 lore raw

[Qemu-devel] Re: [PATCH 05/15] monitor: Handle new and old style handlers

Quoted 42 lines · 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 |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)
monitor.c +6 -2
index 9d4c168..c0ba5ee 100644
@@ -211,6 +211,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;
@@ -3053,17 +3058,32 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
     qdict = qdict_new();
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
-    if (cmd) {
-        void (*handler)(Monitor *mon, const QDict *qdict);
+    if (!cmd)

monitor.c static void monitor_handle_command(Monitor *mon, const char *cmdline) · line 3070

Quoted 10 lines
monitor.c +7 -1
+        goto out;
+
+    qemu_errors_to_mon(mon);
 
-        qemu_errors_to_mon(mon);
+    if (monitor_handler_ported(cmd)) {
+        QObject *data = NULL;
+        void (*handler_new)(Monitor *mon, const QDict *params,
+                            QObject **ret_data);
 

Screams to be a typedef.

monitor.c static void monitor_handle_command(Monitor *mon, const char *cmdline) · line 3073

Quoted 5 lines
monitor.c +2 -2
-        handler = cmd->handler;
-        handler(mon, qdict);
+        handler_new = cmd->handler;
+        handler_new(mon, qdict, &data);
 

At this point, couldn't cmd->handler be a union so we had a bit more type safety?

Signature · 3 lines
Regards,

Anthony Liguori
#11 lore raw

[Qemu-devel] Re: [PATCH 05/15] monitor: Handle new and old style handlers

Quoted 56 lines · On Tue, 06 Oct 2009 20:42:47 -0500 Anthony Liguori <[email protected]> wrote:
Quoted 42 lines · 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 |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)
monitor.c +6 -2
index 9d4c168..c0ba5ee 100644
@@ -211,6 +211,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;
@@ -3053,17 +3058,32 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
     qdict = qdict_new();
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
-    if (cmd) {
-        void (*handler)(Monitor *mon, const QDict *qdict);
+    if (!cmd)

monitor.c static void monitor_handle_command(Monitor *mon, const char *cmdline) · line 3070

Quoted 10 lines
monitor.c +7 -1
+        goto out;
+
+    qemu_errors_to_mon(mon);
 
-        qemu_errors_to_mon(mon);
+    if (monitor_handler_ported(cmd)) {
+        QObject *data = NULL;
+        void (*handler_new)(Monitor *mon, const QDict *params,
+                            QObject **ret_data);
 

Screams to be a typedef.

Quoted 8 lines

monitor.c static void monitor_handle_command(Monitor *mon, const char *cmdline) · line 3073

Quoted 5 lines
monitor.c +2 -2
-        handler = cmd->handler;
-        handler(mon, qdict);
+        handler_new = cmd->handler;
+        handler_new(mon, qdict, &data);
 

At this point, couldn't cmd->handler be a union so we had a bit more type safety?

Yes, will do.

#12 lore raw

PATCH v2 6/15 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       |   36 +++++++++++++++++++++++++++---------
 qemu-monitor.hx |    2 +-
 2 files changed, 28 insertions(+), 10 deletions(-)
monitor.c +27 -9
index c0ba5ee..81f3785 100644
@@ -211,6 +211,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;
@@ -285,24 +287,40 @@ 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");
-    void (*handler)(Monitor *);
 
     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)) {
+        void (*handler_new)(Monitor *mon, QObject **ret_data);
+
+        handler_new = cmd->handler;
+        handler_new(mon, ret_data);
+
+        if (*ret_data)
+            cmd->user_print(mon, *ret_data);
+    } else {
+        void (*handler_old)(Monitor *mon);
+        handler_old = cmd->handler;
+        handler_old(mon);
+    }
+
     return;
- found:
-    handler = cmd->handler;
-    handler(mon);
+
+help:
+    help_cmd(mon, "info");
 }
 
 static void do_info_version(Monitor *mon)
qemu-monitor.hx +1 -1
index dfd0951..170c6ae 100644
@@ -41,7 +41,7 @@ ETEXI
         .name       = "info",
         .args_type  = "item:s?",
         .handler    = do_info,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "[subcommand]",
         .help       = "show various information about the system state"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#13 lore raw

PATCH v2 7/15 monitor: Convert do_quit() do QObject

In reply to Luiz Capitulino
---
 monitor.c       |    5 ++++-
 qemu-monitor.hx |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
monitor.c +4 -1
index 81f3785..9046c5c 100644
@@ -458,7 +458,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 +1 -1
index 170c6ae..eb5e728 100644
@@ -120,7 +120,7 @@ ETEXI
         .name       = "q|quit",
         .args_type  = "",
         .handler    = do_quit,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "",
         .help       = "quit the emulator"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#14 lore raw

PATCH v2 8/15 monitor: Convert do_stop() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    5 ++++-
 qemu-monitor.hx |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
monitor.c +4 -1
index 9046c5c..71e659d 100644
@@ -600,7 +600,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 +1 -1
index eb5e728..a3e4b54 100644
@@ -297,7 +297,7 @@ ETEXI
         .name       = "stop",
         .args_type  = "",
         .handler    = do_stop,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "",
         .help       = "stop emulation",
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#15 lore raw

PATCH v2 9/15 monitor: Convert do_system_reset() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    6 +++++-
 qemu-monitor.hx |    2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
monitor.c +5 -1
index 71e659d..d1396c3 100644
@@ -1293,7 +1293,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 +1 -1
index a3e4b54..4b18680 100644
@@ -485,7 +485,7 @@ ETEXI
         .name       = "system_reset",
         .args_type  = "",
         .handler    = do_system_reset,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "",
         .help       = "reset the system"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#16 lore raw

PATCH v2 10/15 monitor: Convert do_system_powerdown() to QObject

In reply to Luiz Capitulino
---
 monitor.c       |    6 +++++-
 qemu-monitor.hx |    2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
monitor.c +5 -1
index d1396c3..198f0fc 100644
@@ -1302,7 +1302,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 +1 -1
index 4b18680..eef0c8e 100644
@@ -500,7 +500,7 @@ ETEXI
         .name       = "system_powerdown",
         .args_type  = "",
         .handler    = do_system_powerdown,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "",
         .help       = "send system power down event"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#17 lore raw

PATCH v2 11/15 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 |    2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)
monitor.c +5 -2
index 198f0fc..094b090 100644
@@ -615,7 +615,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 };
 
@@ -631,7 +634,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 +1 -1
index eef0c8e..935e8ea 100644
@@ -311,7 +311,7 @@ ETEXI
         .name       = "c|cont",
         .args_type  = "",
         .handler    = do_cont,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "",
         .help       = "resume emulation",
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#18 lore raw

PATCH v2 12/15 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 |    2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
monitor.c +4 -2
index 094b090..cb28ffb 100644
@@ -1603,8 +1603,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 +1 -1
index 935e8ea..efaabea 100644
@@ -917,7 +917,7 @@ ETEXI
         .name       = "balloon",
         .args_type  = "value:i",
         .handler    = do_balloon,
-        .user_print = NULL,
+        .user_print = monitor_user_noop,
         .params     = "target",
         .help       = "request VM to change it's memory allocation (in MB)"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#19 lore raw

PATCH v2 13/15 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 |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)
monitor.c +24 -3
index cb28ffb..2a1e4d7 100644
@@ -218,6 +218,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;
@@ -323,9 +341,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,7 @@ static const mon_cmd_t info_cmds[] = {
         .name       = "version",
         .args_type  = "",
         .handler    = do_info_version,
-        .user_print = NULL,
+        .user_print = monitor_print_qobject,
         .params     = "",
         .help       = "show the version of QEMU"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#20 lore raw

PATCH v2 14/15 monitor: Convert do_info_balloon() to QObject

In reply to Luiz Capitulino

On success return a QInt with the balloon's value.

This also introduces monitor_print_balloon() to print the balloon information in the user protocol.

Please, note that errors are not being converted yet.

---
 monitor.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
monitor.c +12 -3
index 2a1e4d7..0dcf478 100644
@@ -1634,7 +1634,16 @@ static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
     qemu_balloon(target << 20);
 }
 
-static void do_info_balloon(Monitor *mon)
+static void monitor_print_balloon(Monitor *mon, const QObject *data)
+{
+    monitor_printf(mon, "balloon: actual=%d\n",
+                                     (int)qint_get_int(qobject_to_qint(data)));
+}
+
+/**
+ * do_info_balloon(): Balloon information
+ */
+static void do_info_balloon(Monitor *mon, QObject **ret_data)
 {
     ram_addr_t actual;
 
@@ -1645,7 +1654,7 @@ static void do_info_balloon(Monitor *mon)
     else if (actual == 0)
         monitor_printf(mon, "Ballooning not activated in VM\n");
     else
-        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
+        *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
 }
 
 static qemu_acl *find_acl(Monitor *mon, const char *name)
@@ -2137,7 +2146,7 @@ static const mon_cmd_t info_cmds[] = {
         .name       = "balloon",
         .args_type  = "",
         .handler    = do_info_balloon,
-        .user_print = NULL,
+        .user_print = monitor_print_balloon,
         .params     = "",
         .help       = "show balloon information"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#21 lore raw

PATCH v2 15/15 monitor: Convert do_info_cpus() to QObject

In reply to Luiz Capitulino

Each CPU information is stored in a QDict and the returned QObject is a QList of all CPUs.

The QDict contains the following information:

- "CPU": cpu index
- "current": "yes" or "no"
- "pc": current PC
- "halted": "yes" or "no"

The user output in the Monitor should not change and the future monitor protocol is expected to emit something like:

[ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" }, { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]

which corresponds to the following user output:

* CPU #0: pc=0x00000000fffffff0
CPU #1: pc=0x00000000fffffff0 (halted)

---
 monitor.c |   94 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 80 insertions(+), 14 deletions(-)
monitor.c +80 -14
index 0dcf478..1487b72 100644
@@ -46,6 +46,7 @@
 #include "kvm.h"
 #include "acl.h"
 #include "qint.h"
+#include "qlist.h"
 #include "qdict.h"
 #include "qstring.h"
 
@@ -410,33 +411,98 @@ static void do_info_registers(Monitor *mon)
 #endif
 }
 
-static void do_info_cpus(Monitor *mon)
+static void print_cpu_iter(QObject *obj, void *opaque)
+{
+    QDict *cpu;
+    int active = ' ';
+    Monitor *mon = opaque;
+
+    assert(qobject_type(obj) == QTYPE_QDICT);
+    cpu = qobject_to_qdict(obj);
+
+    if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
+        active = '*';
+
+    monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
+
+#if defined(TARGET_I386)
+    monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
+                   (target_ulong) qdict_get_int(cpu, "pc"));
+#elif defined(TARGET_PPC)
+    monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "nip"));
+#elif defined(TARGET_SPARC)
+    monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "pc"));
+    monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "npc"));
+#elif defined(TARGET_MIPS)
+    monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "PC"));
+#endif
+
+    if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
+        monitor_printf(mon, " (halted)");
+
+    monitor_printf(mon, "\n");
+}
+
+static void monitor_print_cpus(Monitor *mon, const QObject *data)
+{
+    QList *cpu_list;
+
+    assert(qobject_type(data) == QTYPE_QLIST);
+    cpu_list = qobject_to_qlist(data);
+    qlist_iter(cpu_list, print_cpu_iter, mon);
+}
+
+/**
+ * do_info_cpus(): Show CPU information
+ *
+ * Return a QList with a QDict for each CPU.
+ *
+ * For example:
+ *
+ * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
+ *   { "CPU": 1, "current": "no",  "pc": 0x..., "halted": "yes" } ]
+ */
+static void do_info_cpus(Monitor *mon, QObject **ret_data)
 {
     CPUState *env;
+    QList *cpu_list;
+
+    cpu_list = qlist_new();
 
     /* just to set the default cpu if not already done */
     mon_get_cpu();
 
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
+        const char *answer;
+        QDict *cpu = qdict_new();
+
         cpu_synchronize_state(env);
-        monitor_printf(mon, "%c CPU #%d:",
-                       (env == mon->mon_cpu) ? '*' : ' ',
-                       env->cpu_index);
+
+        qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
+        answer = (env == mon->mon_cpu) ? "yes" : "no";
+        qdict_put(cpu, "current", qstring_from_str(answer));
+
 #if defined(TARGET_I386)
-        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
-                       env->eip + env->segs[R_CS].base);
+        qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
 #elif defined(TARGET_PPC)
-        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
+        qdict_put(cpu, "nip", qint_from_int(env->nip));
 #elif defined(TARGET_SPARC)
-        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
-                       env->pc, env->npc);
+        qdict_put(cpu, "pc", qint_from_int(env->pc));
+        qdict_put(cpu, "npc", qint_from_int(env->npc));
 #elif defined(TARGET_MIPS)
-        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
+        qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
 #endif
-        if (env->halted)
-            monitor_printf(mon, " (halted)");
-        monitor_printf(mon, "\n");
+        answer = env->halted ? "yes" : "no";
+        qdict_put(cpu, "halted", qstring_from_str(answer));
+
+        qlist_append(cpu_list, cpu);
     }
+
+    *ret_data = QOBJECT(cpu_list);
 }
 
 static void do_cpu_set(Monitor *mon, const QDict *qdict)
@@ -1938,7 +2004,7 @@ static const mon_cmd_t info_cmds[] = {
         .name       = "cpus",
         .args_type  = "",
         .handler    = do_info_cpus,
-        .user_print = NULL,
+        .user_print = monitor_print_cpus,
         .params     = "",
         .help       = "show infos for each CPU"
     },
Signature · 1 line
1.6.5.rc2.17.gdbc1b
#22 lore raw

[Qemu-devel] Re: [PATCH 15/15] monitor: Convert do_info_cpus() to QObject

Quoted 109 lines · Luiz Capitulino

Each CPU information is stored in a QDict and the returned QObject is a QList of all CPUs.

The QDict contains the following information:

- "CPU": cpu index
- "current": "yes" or "no"
- "pc": current PC
- "halted": "yes" or "no"

The user output in the Monitor should not change and the future monitor protocol is expected to emit something like:

[ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" }, { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]

which corresponds to the following user output:

* CPU #0: pc=0x00000000fffffff0
CPU #1: pc=0x00000000fffffff0 (halted)

---
 monitor.c |   94 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 80 insertions(+), 14 deletions(-)
monitor.c +60 -1
index 0dcf478..1487b72 100644
@@ -46,6 +46,7 @@
 #include "kvm.h"
 #include "acl.h"
 #include "qint.h"
+#include "qlist.h"
 #include "qdict.h"
 #include "qstring.h"
 
@@ -410,33 +411,98 @@ static void do_info_registers(Monitor *mon)
 #endif
 }
 
-static void do_info_cpus(Monitor *mon)
+static void print_cpu_iter(QObject *obj, void *opaque)
+{
+    QDict *cpu;
+    int active = ' ';
+    Monitor *mon = opaque;
+
+    assert(qobject_type(obj) == QTYPE_QDICT);
+    cpu = qobject_to_qdict(obj);
+
+    if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
+        active = '*';
+
+    monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
+
+#if defined(TARGET_I386)
+    monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
+                   (target_ulong) qdict_get_int(cpu, "pc"));
+#elif defined(TARGET_PPC)
+    monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "nip"));
+#elif defined(TARGET_SPARC)
+    monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "pc"));
+    monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "npc"));
+#elif defined(TARGET_MIPS)
+    monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "PC"));
+#endif
+
+    if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
+        monitor_printf(mon, " (halted)");
+
+    monitor_printf(mon, "\n");
+}
+
+static void monitor_print_cpus(Monitor *mon, const QObject *data)
+{
+    QList *cpu_list;
+
+    assert(qobject_type(data) == QTYPE_QLIST);
+    cpu_list = qobject_to_qlist(data);
+    qlist_iter(cpu_list, print_cpu_iter, mon);
+}
+
+/**
+ * do_info_cpus(): Show CPU information
+ *
+ * Return a QList with a QDict for each CPU.
+ *
+ * For example:
+ *
+ * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
+ *   { "CPU": 1, "current": "no",  "pc": 0x..., "halted": "yes" } ]
+ */
+static void do_info_cpus(Monitor *mon, QObject **ret_data)
 {
     CPUState *env;
+    QList *cpu_list;
+
+    cpu_list = qlist_new();
 
     /* just to set the default cpu if not already done */
     mon_get_cpu();
 
     for(env = first_cpu; env != NULL; env = env->next_cpu) {

monitor.c static void do_info_registers(Monitor *mon) · line 486

Quoted 10 lines
monitor.c +5 -3
+        const char *answer;
+        QDict *cpu = qdict_new();
+
         cpu_synchronize_state(env);
-        monitor_printf(mon, "%c CPU #%d:",
-                       (env == mon->mon_cpu) ? '*' : ' ',
-                       env->cpu_index);
+
+        qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
 

I'd almost think that a json parser that supported varadics would make this much easier..

Signature · 3 lines
Regards,

Anthony Liguori
#23 lore raw

[Qemu-devel] Re: [PATCH 15/15] monitor: Convert do_info_cpus() to QObject

Quoted 109 lines · On Tue, 06 Oct 2009 20:46:53 -0500 Anthony Liguori <[email protected]> wrote:
Quoted 108 lines · Luiz Capitulino

Each CPU information is stored in a QDict and the returned QObject is a QList of all CPUs.

The QDict contains the following information:

- "CPU": cpu index
- "current": "yes" or "no"
- "pc": current PC
- "halted": "yes" or "no"

The user output in the Monitor should not change and the future monitor protocol is expected to emit something like:

[ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" }, { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]

which corresponds to the following user output:

* CPU #0: pc=0x00000000fffffff0
CPU #1: pc=0x00000000fffffff0 (halted)

---
 monitor.c |   94 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 80 insertions(+), 14 deletions(-)
monitor.c +60 -1
index 0dcf478..1487b72 100644
@@ -46,6 +46,7 @@
 #include "kvm.h"
 #include "acl.h"
 #include "qint.h"
+#include "qlist.h"
 #include "qdict.h"
 #include "qstring.h"
 
@@ -410,33 +411,98 @@ static void do_info_registers(Monitor *mon)
 #endif
 }
 
-static void do_info_cpus(Monitor *mon)
+static void print_cpu_iter(QObject *obj, void *opaque)
+{
+    QDict *cpu;
+    int active = ' ';
+    Monitor *mon = opaque;
+
+    assert(qobject_type(obj) == QTYPE_QDICT);
+    cpu = qobject_to_qdict(obj);
+
+    if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
+        active = '*';
+
+    monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
+
+#if defined(TARGET_I386)
+    monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
+                   (target_ulong) qdict_get_int(cpu, "pc"));
+#elif defined(TARGET_PPC)
+    monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "nip"));
+#elif defined(TARGET_SPARC)
+    monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "pc"));
+    monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "npc"));
+#elif defined(TARGET_MIPS)
+    monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
+                   (target_long) qdict_get_int(cpu, "PC"));
+#endif
+
+    if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
+        monitor_printf(mon, " (halted)");
+
+    monitor_printf(mon, "\n");
+}
+
+static void monitor_print_cpus(Monitor *mon, const QObject *data)
+{
+    QList *cpu_list;
+
+    assert(qobject_type(data) == QTYPE_QLIST);
+    cpu_list = qobject_to_qlist(data);
+    qlist_iter(cpu_list, print_cpu_iter, mon);
+}
+
+/**
+ * do_info_cpus(): Show CPU information
+ *
+ * Return a QList with a QDict for each CPU.
+ *
+ * For example:
+ *
+ * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
+ *   { "CPU": 1, "current": "no",  "pc": 0x..., "halted": "yes" } ]
+ */
+static void do_info_cpus(Monitor *mon, QObject **ret_data)
 {
     CPUState *env;
+    QList *cpu_list;
+
+    cpu_list = qlist_new();
 
     /* just to set the default cpu if not already done */
     mon_get_cpu();
 
Quoted 14 lines

monitor.c static void do_info_registers(Monitor *mon) · line 507

Quoted 11 lines
monitor.c +5 -3
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
+        const char *answer;
+        QDict *cpu = qdict_new();
+
         cpu_synchronize_state(env);
-        monitor_printf(mon, "%c CPU #%d:",
-                       (env == mon->mon_cpu) ? '*' : ' ',
-                       env->cpu_index);
+
+        qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
 

I'd almost think that a json parser that supported varadics would make this much easier..

In this specific case most values are conditionally inserted into the dict,
so I think it's easier to insert them separately.

But I've a function called qobject_from_fmt() which allows one to
do something like:

QObject *obj = qobject_from_fmt("{ s: [ i, s ], s: i }", ... );

I'm not sure I've implemented it correctly, but will submit as
part of the series that will use it.