Skip to contentSkip to conversation

Convert device_add to QObject / QError

RFC PATCH In review Markus Armbruster 4 participants 77 messages Feb 24, 2010 – Mar 2, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Convert device_add to QObject / QError

Why this is such a big job? There are two issues with a naive conversion:

* Error message degradation

The error messages are worded for -device. They aren't so hot to begin with: we typically have many -device options, and to which one a message applies is often not obvious.

Now, QMP wants relatively generic errors. For instance, "-device:
no driver specified" becomes "Parameter 'driver' is missing". Emitting such an error with our lengthy command lines is just too mean to users.

However, if you know *where* the parameter is missing, the generic message is perfectly adequate: "-device a=b: Parameter 'driver' is missing". In fact, it's even superior to our current message.

So the first part of the patch series is about error locations. I feel it's very useful all by itself. I can split it off into its own patch series. But then the rest of this series depends on it, so I'm not sure splitting is all that useful.

We may still encounter cases where a generic message is not adequate even with precise location information. Let's solve that problem when we actually encounter it.

* String argument with option syntax, i.e. NAME=VALUE,...

QMP uses JSON to encode collections of name/value pairs. Adding a second encoding for the same thing would be a mistake, in my opinion.

Note that we already have two competing encodings in our code: QDict and QemuOpts. But we should not permit that to leak into an external interface like QMP.

QemuOpts originated in the command line and spread from there into a few monitor commands, including device_add, and a few internal interfaces.

QDict originated in the monitor. It sits right at the interface between monitor core and command handlers.

My proposed solution is modest and pragmatic:

* Lift the parsing of arguments into QemuOpts from monitor handlers up into the human monitor core. This removes QemuOpts from the handler interface, and thus avoids leaking it into QMP. It's exactly what we did for other argument types with syntax inappropriate for QMP, such as arguments of migrate_set_speed and migrate_set_downtime (commits 9da92c49..b0fbf7d3).

* Monitor handlers that need to pass their arguments in QemuOpts-form to internal interfaces use a converter function to translate from QDict to QemuOpts.

This is what the last part of the patch series is about. If you'd prefer a different solution, let's talk.

I can split this part off into its own patch series if that helps. However, the patches before it aren't all that useful without it, so I'm not sure splitting buys us much.

A possible alternative is to add the concept of optional named arguments to the monitor. Instead of encoding multiple optional named arguments in a single positional argument, we encode them as multiple named arguments. For instance, "device_add ide-drive,drive=hda,bus=ide.0,unit=0" becomes "device_add ide-drive drive=hda bus=ide.0 unit=0".

Of course, if you think that adding a second encoding for collections of name/value pairs to QMP is fine, then this last part can be dropped.

So, the series starts with error locations (part I), and ends with keeping QemuOpts out of QMP (part III). Wedged in between is the conversion of device_add to QError (part II). In more detail:

Part I: Error Locations

[01-07] Preliminary cleanup & fixes
[08] Separate "default monitor" and "current monitor" cleanly
[09-16] More cleanup
[17-21] Error Locations

Part II: Convert device_add to QError

[22-25] Preliminary qdev cleanup & fixes
[26-42] Convert device_add to QError

Part III

[43] Conversions between QDict and QemuOpts
[44-46] New monitor argument type O
[47-48] Convert device_add to QObject

I cut a few corners clearly marked in commit messages and code. I'll fix them up for the non-RFC submit.

Markus Armbruster (48):
monitor: Factor monitor_set_error() out of qemu_error_internal()
error: Move qemu_error() & friends from monitor.c to own file
usb: Remove disabled monitor_printf() in usb_read_file()
savevm: Fix -loadvm to report errors to stderr, not the monitor
pc: Fix error reporting for -boot once
pc: Factor common code out of pc_boot_set() and cmos_init()
tools: Remove unused cur_mon from qemu-tool.c
monitor: Separate "default monitor" and "current monitor" cleanly
block: Simplify usb_msd_initfn() test for "can read bdrv key"
error: Simplify error sink setup
error: Move qemu_error & friends into their own header
error: New error_printf() and error_vprintf()
error: Make qemu_error() add a newline, strip it from arguments
error: Don't abuse qemu_error() for non-error in scsi_hot_add()
error: Don't abuse qemu_error() for non-error in qdev_device_help()
error: Don't abuse qemu_error() for non-error in qbus_find()
error: Infrastructure to track locations for error reporting
error: Include the program name in error messages to stderr
error: Track locations in configuration files
QemuOpts: Fix qemu_config_parse() to catch file read errors
error: Track locations on command line
qdev: Fix -device and device_add to handle unsuitable bus gracefully
qdev: Factor qdev_create_from_info() out of qdev_create()
qdev: Hide "no_user" devices from users
qdev: Hide "ptr" properties from users
error: Polish human-readable error descriptions
error: New QERR_PROPERTY_NOT_FOUND
error: New QERR_PROPERTY_VALUE_BAD
qdev: convert setting device properties to QError
qdev: Relax parsing of bus option
error: New QERR_BUS_NOT_FOUND
error: New QERR_DEVICE_MULTIPLE_BUSSES
error: New QERR_DEVICE_NO_BUS
qdev: Convert qbus_find() to QError
monitor: New in_qmp_mon()
error: New error_printf_unless_qmp()
error: New QERR_BAD_BUS_FOR_DEVICE
error: New QERR_BUS_NO_HOTPLUG
error: New QERR_DEVICE_INIT_FAILED
error: New QERR_NO_BUS_FOR_DEVICE
Revert "qdev: Use QError for 'device not found' error"
error: Convert do_device_add() to QError
qemu-option: Functions to convert to/from QDict.
qemu-option: Move the implied first name into QemuOptsList
qemu-option: Rename find_list() to qemu_find_opts() & external
linkage
monitor: New argument type 'O'
monitor: Use argument type 'O' for device_add
monitor: convert do_device_add() to QObject

 Makefile.target        |    1 +
 audio/audio.c          |    4 +-
 hw/pc.c                |   35 +++-----
 hw/pci-hotplug.c       |   13 ++--
 hw/pci.c               |    8 +-
 hw/qdev-properties.c   |   27 ++----
 hw/qdev.c              |  221 +++++++++++++++++++++++++++--------------------
 hw/qdev.h              |    2 +-
 hw/scsi-bus.c          |    4 +-
 hw/scsi-disk.c         |    5 +-
 hw/scsi-generic.c      |    9 +-
 hw/usb-bus.c           |    4 +-
 hw/usb-msd.c           |    4 +-
 hw/usb-net.c           |    2 +-
 hw/usb-serial.c        |    9 +-
 hw/virtio-net.c        |    5 +-
 hw/virtio-pci.c        |    4 +-
 hw/virtio-serial-bus.c |    2 +-
 monitor.c              |  191 ++++++++++++++++++------------------------
 monitor.h              |    7 ++
 net.c                  |   28 +++---
 net/dump.c             |    5 +-
 net/slirp.c            |   20 ++--
 net/socket.c           |   12 ++--
 net/tap-bsd.c          |    3 +-
 net/tap-linux.c        |    5 +-
 net/tap-win32.c        |    2 +-
 net/tap.c              |    3 +-
 qemu-config.c          |   54 ++++++++----
 qemu-config.h          |    3 +-
 qemu-error.c           |  222 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-error.h           |   47 ++++++++++
 qemu-monitor.hx        |    7 +-
 qemu-option.c          |   68 +++++++++++++++-
 qemu-option.h          |    6 +-
 qemu-tool.c            |   28 ++++++-
 qerror.c               |   73 ++++++++++++----
 qerror.h               |   45 ++++++++--
 savevm.c               |   27 +++---
 slirp/misc.c           |    2 +-
 sysemu.h               |   13 +---
 usb-linux.c            |    8 --
 vl.c                   |   44 ++++++----
 vnc.c                  |    5 +-
 44 files changed, 858 insertions(+), 429 deletions(-)
 create mode 100644 qemu-error.c
 create mode 100644 qemu-error.h
#1 lore raw

RFC PATCH 1/48 monitor: Factor monitor_set_error() out of qemu_error_internal()

This separates the monitor part from the QError part.

---
 monitor.c |   21 +++++++++++++--------
 monitor.h |    3 +++
 2 files changed, 16 insertions(+), 8 deletions(-)
monitor.c +13 -8
index b1a6edc..fcd8718 100644
@@ -3856,6 +3856,18 @@ fail:
     return NULL;
 }
 
+void monitor_set_error(Monitor *mon, QError *qerror)
+{
+    /* report only the first error */
+    if (!mon->error) {
+        mon->error = qerror;
+    } else {
+        MON_DEBUG("Additional error report at %s:%d\n",
+                  qerror->file, qerror->linenr);
+        QDECREF(qerror);
+    }
+}
+
 static void monitor_print_error(Monitor *mon)
 {
     qerror_print(mon->error);
@@ -4757,14 +4769,7 @@ void qemu_error_internal(const char *file, int linenr, const char *func,
         QDECREF(qerror);
         break;
     case ERR_SINK_MONITOR:
-        /* report only the first error */
-        if (!qemu_error_sink->mon->error) {
-            qemu_error_sink->mon->error = qerror;
-        } else {
-            MON_DEBUG("Additional error report at %s:%d\n", qerror->file,
-                      qerror->linenr);
-            QDECREF(qerror);
-        }
+        monitor_set_error(qemu_error_sink->mon, qerror);
         break;
     }
 }
monitor.h +3 -0
index fc09505..1c72615 100644
@@ -3,6 +3,7 @@
 
 #include "qemu-common.h"
 #include "qemu-char.h"
+#include "qerror.h"
 #include "qdict.h"
 #include "block.h"
 
@@ -47,4 +48,6 @@ void monitor_flush(Monitor *mon);
 
 typedef void (MonitorCompletion)(void *opaque, QObject *ret_data);
 
+void monitor_set_error(Monitor *mon, QError *qerror);
+
 #endif /* !MONITOR_H */
Signature · 1 line
1.6.6
#2 lore raw

RFC PATCH 2/48 error: Move qemu_error() & friends from monitor.c to own file

In reply to Markus Armbruster

They're about reporting errors, not about the monitor.

---
 Makefile.target |    1 +
 monitor.c       |   89 -----------------------------------------------------
 qemu-error.c    |   92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 89 deletions(-)
 create mode 100644 qemu-error.c
Makefile.target +1 -0
index 4c4d397..8cf0cb5 100644
@@ -170,6 +170,7 @@ endif #CONFIG_BSD_USER
 ifdef CONFIG_SOFTMMU
 
 obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
+obj-y += qemu-error.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
 obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o
monitor.c +0 -89

92 lines not on this page · raw message

qemu-error.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
1.6.6
#3 lore raw

RFC PATCH 3/48 usb: Remove disabled monitor_printf() in usb_read_file()

In reply to Markus Armbruster

The monitor_printf() reports failure. Printing is wrong, because the caller tries various arguments, and expects the function to fail for some or all.

Disabled since commit 26a9e82a. Remove it.

---
 usb-linux.c |    8 --------
 1 files changed, 0 insertions(+), 8 deletions(-)
usb-linux.c +0 -8
index a9c15c6..d0d7cff 100644
@@ -1188,9 +1188,6 @@ static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
  */
 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
 {
-#if 0
-    Monitor *mon = cur_mon;
-#endif
     FILE *f;
     int ret = 0;
     char filename[PATH_MAX];
@@ -1201,11 +1198,6 @@ static int usb_host_read_file(char *line, size_t line_size, const char *device_f
     if (f) {
         ret = fgets(line, line_size, f) != NULL;
         fclose(f);
-#if 0
-    } else {
-        if (mon)
-            monitor_printf(mon, "husb: could not open %s\n", filename);
-#endif
     }
 
     return ret;
Signature · 1 line
1.6.6
#4 lore raw

RFC PATCH 4/48 savevm: Fix -loadvm to report errors to stderr, not the monitor

In reply to Markus Armbruster

A monitor may not even exist.

Change load_vmstate() to use qemu_error() instead of monitor_printf(). Parameter mon is now unused, remove it.

---
 monitor.c |    2 +-
 savevm.c  |   24 +++++++++++-------------
 sysemu.h  |    2 +-
 vl.c      |    2 +-
 4 files changed, 14 insertions(+), 16 deletions(-)
monitor.c +1 -1
index 9ed3ec4..9a21dfb 100644
@@ -2532,7 +2532,7 @@ static void do_loadvm(Monitor *mon, const QDict *qdict)
 
     vm_stop(0);
 
-    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
+    if (load_vmstate(name) >= 0 && saved_vm_running)
         vm_start();
 }
 
savevm.c +11 -13
index 4b58663..1f6c25e 100644
@@ -1733,7 +1733,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         vm_start();
 }
 
-int load_vmstate(Monitor *mon, const char *name)
+int load_vmstate(const char *name)
 {
     DriveInfo *dinfo;
     BlockDriverState *bs, *bs1;
@@ -1743,7 +1743,7 @@ int load_vmstate(Monitor *mon, const char *name)
 
     bs = get_bs_snapshots();
     if (!bs) {
-        monitor_printf(mon, "No block device supports snapshots\n");
+        qemu_error("No block device supports snapshots\n");
         return -EINVAL;
     }
 
@@ -1756,21 +1756,19 @@ int load_vmstate(Monitor *mon, const char *name)
             ret = bdrv_snapshot_goto(bs1, name);
             if (ret < 0) {
                 if (bs != bs1)
-                    monitor_printf(mon, "Warning: ");
+                    qemu_error("Warning: ");
                 switch(ret) {
                 case -ENOTSUP:
-                    monitor_printf(mon,
-                                   "Snapshots not supported on device '%s'\n",
-                                   bdrv_get_device_name(bs1));
+                    qemu_error("Snapshots not supported on device '%s'\n",
+                               bdrv_get_device_name(bs1));
                     break;
                 case -ENOENT:
-                    monitor_printf(mon, "Could not find snapshot '%s' on "
-                                   "device '%s'\n",
-                                   name, bdrv_get_device_name(bs1));
+                    qemu_error("Could not find snapshot '%s' on device '%s'\n",
+                               name, bdrv_get_device_name(bs1));
                     break;
                 default:
-                    monitor_printf(mon, "Error %d while activating snapshot on"
-                                   " '%s'\n", ret, bdrv_get_device_name(bs1));
+                    qemu_error("Error %d while activating snapshot on '%s'\n",
+                               ret, bdrv_get_device_name(bs1));
                     break;
                 }
                 /* fatal on snapshot block device */
@@ -1788,13 +1786,13 @@ int load_vmstate(Monitor *mon, const char *name)
     /* restore the VM state */
     f = qemu_fopen_bdrv(bs, 0);
     if (!f) {
-        monitor_printf(mon, "Could not open VM state file\n");
+        qemu_error("Could not open VM state file\n");
         return -EINVAL;
     }
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
     if (ret < 0) {
-        monitor_printf(mon, "Error %d while loading VM state\n", ret);
+        qemu_error("Error %d while loading VM state\n", ret);
         return ret;
     }
     return 0;
sysemu.h +1 -1
index 8ba618e..4d8b7a2 100644
@@ -54,7 +54,7 @@ extern qemu_irq qemu_system_powerdown;
 void qemu_system_reset(void);
 
 void do_savevm(Monitor *mon, const QDict *qdict);
-int load_vmstate(Monitor *mon, const char *name);
+int load_vmstate(const char *name);
 void do_delvm(Monitor *mon, const QDict *qdict);
 void do_info_snapshots(Monitor *mon);
 
vl.c +1 -1
index db7a178..8a588c7 100644
@@ -6023,7 +6023,7 @@ int main(int argc, char **argv, char **envp)
 
     qemu_system_reset();
     if (loadvm) {
-        if (load_vmstate(cur_mon, loadvm) < 0) {
+        if (load_vmstate(loadvm) < 0) {
             autostart = 0;
         }
     }
Signature · 1 line
1.6.6
#5 lore raw

RFC PATCH 5/48 pc: Fix error reporting for -boot once

In reply to Markus Armbruster

Commit 0ecdffbb created pc_boot_set() for use from monitor command "boot_set", via qemu_boot_set(). pc_boot_set() reports errors to cur_mon, which works fine for monitor code.

Commit e0f084bf reused the function int reset handler restore_boot_devices(). Use of cur_mon is problematic in that context. For instance, the "Too many boot devices for PC" error for "-boot order=abcdefgh,once=c" goes to the monitor instead of stderr. The monitor may not even exist.

Fix by switching to qemu_error().

---
 hw/pc.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
hw/pc.c +3 -4
index 4f6a522..d94980b 100644
@@ -234,7 +234,6 @@ static int boot_device2nibble(char boot_device)
  and used there as well */
 static int pc_boot_set(void *opaque, const char *boot_device)
 {
-    Monitor *mon = cur_mon;
 #define PC_MAX_BOOT_DEVICES 3
     RTCState *s = (RTCState *)opaque;
     int nbds, bds[3] = { 0, };
@@ -242,14 +241,14 @@ static int pc_boot_set(void *opaque, const char *boot_device)
 
     nbds = strlen(boot_device);
     if (nbds > PC_MAX_BOOT_DEVICES) {
-        monitor_printf(mon, "Too many boot devices for PC\n");
+        qemu_error("Too many boot devices for PC\n");
         return(1);
     }
     for (i = 0; i < nbds; i++) {
         bds[i] = boot_device2nibble(boot_device[i]);
         if (bds[i] == 0) {
-            monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
-                           boot_device[i]);
+            qemu_error("Invalid boot device for PC: '%c'\n",
+                       boot_device[i]);
             return(1);
         }
     }
Signature · 1 line
1.6.6
#6 lore raw

RFC PATCH 6/48 pc: Factor common code out of pc_boot_set() and cmos_init()

In reply to Markus Armbruster

Code duplicated in commit 0ecdffbb. The two versions are similar, but not identical:

* cmos_init() reports errors to stderr, pc_boot_set() via qemu_error(). The latter is fine for both, so pick that for the common code.

* cmos_init() obeys fd_bootchk, pc_boot_set() ignores it. Make it a parameter of the common code.

---
 hw/pc.c |   28 ++++++++--------------------
 1 files changed, 8 insertions(+), 20 deletions(-)
hw/pc.c +8 -20
index d94980b..1595283 100644
@@ -230,12 +230,9 @@ static int boot_device2nibble(char boot_device)
     return 0;
 }
 
-/* copy/pasted from cmos_init, should be made a general function
- and used there as well */
-static int pc_boot_set(void *opaque, const char *boot_device)
+static int set_boot_dev(RTCState *s, const char *boot_device, int fd_bootchk)
 {
 #define PC_MAX_BOOT_DEVICES 3
-    RTCState *s = (RTCState *)opaque;
     int nbds, bds[3] = { 0, };
     int i;
 
@@ -253,16 +250,20 @@ static int pc_boot_set(void *opaque, const char *boot_device)
         }
     }
     rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
-    rtc_set_memory(s, 0x38, (bds[2] << 4));
+    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
     return(0);
 }
 
+static int pc_boot_set(void *opaque, const char *boot_device)
+{
+    return set_boot_dev(opaque, boot_device, 0);
+}
+
 /* hd_table must contain 4 block drivers */
 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
                       const char *boot_device, DriveInfo **hd_table)
 {
     RTCState *s = rtc_state;
-    int nbds, bds[3] = { 0, };
     int val;
     int fd0, fd1, nb;
     int i;
@@ -301,22 +302,9 @@ static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
     rtc_set_memory(s, 0x5f, smp_cpus - 1);
 
     /* set boot devices, and disable floppy signature check if requested */
-#define PC_MAX_BOOT_DEVICES 3
-    nbds = strlen(boot_device);
-    if (nbds > PC_MAX_BOOT_DEVICES) {
-        fprintf(stderr, "Too many boot devices for PC\n");
+    if (set_boot_dev(s, boot_device, fd_bootchk)) {
         exit(1);
     }
-    for (i = 0; i < nbds; i++) {
-        bds[i] = boot_device2nibble(boot_device[i]);
-        if (bds[i] == 0) {
-            fprintf(stderr, "Invalid boot device for PC: '%c'\n",
-                    boot_device[i]);
-            exit(1);
-        }
-    }
-    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
-    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ?  0x0 : 0x1));
 
     /* floppy type */
 
Signature · 1 line
1.6.6
#7 lore raw

RFC PATCH 7/48 tools: Remove unused cur_mon from qemu-tool.c

In reply to Markus Armbruster
---
 qemu-tool.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
qemu-tool.c +0 -2
index 18b48af..bbce99f 100644
@@ -33,8 +33,6 @@ void qemu_service_io(void)
 {
 }
 
-Monitor *cur_mon;
-
 void monitor_printf(Monitor *mon, const char *fmt, ...)
 {
 }
Signature · 1 line
1.6.6
#8 lore raw

RFC PATCH 8/48 monitor: Separate "default monitor" and "current monitor" cleanly

In reply to Markus Armbruster

Commits 376253ec..731b0364 introduced global variable cur_mon, which points to the "default monitor" (if any), except during execution of monitor_read() or monitor_control_read() it points to the monitor from which we're reading instead (the "current monitor"). Monitor command handlers run within monitor_read() or monitor_control_read().

Default monitor and current monitor are really separate things, and squashing them together is confusing and error-prone.

For instance, usb_host_scan() can run both in "info usbhost" and periodically via usb_host_auto_check(). It prints to cur_mon, which is what we want in the former case: the monitor executing "info usbhost". But since that's the default monitor in the latter case, it periodically spams the default monitor there.

A few places use cur_mon to log stuff to the default monitor. If we ever log something while cur_mon points to current monitor instead of default monitor, the log temporarily "jumps" to another monitor. Whether that can or cannot happen isn't always obvious.

Maybe logging to the default monitor (which may not even exist) is a bad idea, and we should log to stderr or a logfile instead. But that's outside the scope of this commit.

Change cur_mon to point to the current monitor. Create new default_mon to point to the default monitor. Update users of cur_mon accordingly.

This fixes the periodical spamming of the default monitor by usb_host_scan(). It also stops "log jumping", should that problem exist.

---
 audio/audio.c |    4 ++--
 monitor.c     |    7 ++++---
 monitor.h     |    1 +
 slirp/misc.c  |    2 +-
 vnc.c         |    5 ++---
 5 files changed, 10 insertions(+), 9 deletions(-)
audio/audio.c +2 -2
index 2a20e5b..dbf0b96 100644
@@ -330,10 +330,10 @@ void AUD_vlog (const char *cap, const char *fmt, va_list ap)
 {
     if (conf.log_to_monitor) {
         if (cap) {
-            monitor_printf(cur_mon, "%s: ", cap);
+            monitor_printf(default_mon, "%s: ", cap);
         }
 
-        monitor_vprintf(cur_mon, fmt, ap);
+        monitor_vprintf(default_mon, fmt, ap);
     }
     else {
         if (cap) {
monitor.c +4 -3
index 9a21dfb..4e94cf3 100644
@@ -177,7 +177,8 @@ static QLIST_HEAD(mon_list, Monitor) mon_list;
 static const mon_cmd_t mon_cmds[];
 static const mon_cmd_t info_cmds[];
 
-Monitor *cur_mon = NULL;
+Monitor *cur_mon;
+Monitor *default_mon;
 
 static void monitor_command_cb(Monitor *mon, const char *cmdline,
                                void *opaque);
@@ -4635,8 +4636,8 @@ void monitor_init(CharDriverState *chr, int flags)
     }
 
     QLIST_INSERT_HEAD(&mon_list, mon, entry);
-    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
-        cur_mon = mon;
+    if (!default_mon || (flags & MONITOR_IS_DEFAULT))
+        default_mon = mon;
 }
 
 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
monitor.h +1 -0
index 1c72615..6d7969c 100644
@@ -8,6 +8,7 @@
 #include "block.h"
 
 extern Monitor *cur_mon;
+extern Monitor *default_mon;
 
 /* flags for monitor_init */
 #define MONITOR_IS_DEFAULT    0x01
slirp/misc.c +1 -1
index dcb1dc1..1aeb401 100644
@@ -260,7 +260,7 @@ void lprint(const char *format, ...)
     va_list args;
 
     va_start(args, format);
-    monitor_vprintf(cur_mon, format, args);
+    monitor_vprintf(default_mon, format, args);
     va_end(args);
 }
 
vnc.c +2 -3
index db34b0e..712043f 100644
@@ -1046,11 +1046,10 @@ static void audio_capture(void *opaque, void *buf, int size)
 
 static void audio_add(VncState *vs)
 {
-    Monitor *mon = cur_mon;
     struct audio_capture_ops ops;
 
     if (vs->audio_cap) {
-        monitor_printf(mon, "audio already running\n");
+        monitor_printf(default_mon, "audio already running\n");
         return;
     }
 
@@ -1060,7 +1059,7 @@ static void audio_add(VncState *vs)
 
     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
     if (!vs->audio_cap) {
-        monitor_printf(mon, "Failed to add audio capture\n");
+        monitor_printf(default_mon, "Failed to add audio capture\n");
     }
 }
 
Signature · 1 line
1.6.6
#9 lore raw

RFC PATCH 9/48 block: Simplify usb_msd_initfn() test for "can read bdrv key"

In reply to Markus Armbruster

The old test assumes that "hotplugged" implies "we have a current monitor for reading the key". This is in fact true, but it's not obviously true.

Aside: if it were false, we could pass a null pointer to monitor_read_bdrv_key_start(), which would then crash.

The previous commit permits us to check for "we have a current monitor" directly, so do that.

---
 hw/usb-msd.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/usb-msd.c +1 -1
index 36991f8..1e3e101 100644
@@ -535,7 +535,7 @@ static int usb_msd_initfn(USBDevice *dev)
     usb_msd_handle_reset(dev);
 
     if (bdrv_key_required(s->conf.dinfo->bdrv)) {
-        if (s->dev.qdev.hotplugged) {
+        if (cur_mon) {
             monitor_read_bdrv_key_start(cur_mon, s->conf.dinfo->bdrv,
                                         usb_msd_password_cb, s);
             s->dev.auto_attach = 0;
Signature · 1 line
1.6.6
#10 lore raw

RFC PATCH 10/48 error: Simplify error sink setup

In reply to Markus Armbruster

qemu_error_sink can either point to a monitor or a file. In practice, it always points to the current monitor if we have one, else to stderr. Simply route errors to the current monitor or else to stderr, and remove qemu_error_sink along with the functions to control it.

Actually, the old code switches the sink slightly later, in handle_user_command() and handle_qmp_command(), than it gets switched now, implicitly, by setting the current monitor in monitor_read() and monitor_control_read(). Likewise, it switches back slightly earlier (same places). Doesn't make a difference, because there are no calls of qemu_error() in between.

---
 monitor.c    |    6 ----
 qemu-error.c |   76 +++++++---------------------------------------------------
 sysemu.h     |    3 --
 vl.c         |    1 -
 4 files changed, 9 insertions(+), 77 deletions(-)
monitor.c +0 -6
index 4e94cf3..a4263af 100644
@@ -3970,8 +3970,6 @@ static void handle_user_command(Monitor *mon, const char *cmdline)
     if (!cmd)
         goto out;
 
-    qemu_errors_to_mon(mon);
-
     if (monitor_handler_is_async(cmd)) {
         user_async_cmd_handler(mon, cmd, qdict);
     } else if (monitor_handler_ported(cmd)) {
@@ -3983,8 +3981,6 @@ static void handle_user_command(Monitor *mon, const char *cmdline)
     if (monitor_has_error(mon))
         monitor_print_error(mon);
 
-    qemu_errors_to_previous();
-
 out:
     QDECREF(qdict);
 }
@@ -4386,7 +4382,6 @@ static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
     const char *cmd_name, *info_item;
 
     args = NULL;
-    qemu_errors_to_mon(mon);
 
     obj = json_parser_parse(tokens, NULL);
     if (!obj) {
@@ -4467,7 +4462,6 @@ err_out:
     monitor_protocol_emitter(mon, NULL);
 out:
     QDECREF(args);
-    qemu_errors_to_previous();
 }
 
 /**
qemu-error.c +9 -67

98 lines not on this page · raw message

sysemu.h +0 -3
index 4d8b7a2..cd84ee8 100644
@@ -69,9 +69,6 @@ int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f);
 void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f);
 int qemu_loadvm_state(QEMUFile *f);
 
-void qemu_errors_to_file(FILE *fp);
-void qemu_errors_to_mon(Monitor *mon);
-void qemu_errors_to_previous(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
vl.c +0 -1
index 8a588c7..c4c8c1f 100644
@@ -4824,7 +4824,6 @@ int main(int argc, char **argv, char **envp)
 
     init_clocks();
 
-    qemu_errors_to_file(stderr);
     qemu_cache_utils_init(envp);
 
     QLIST_INIT (&vm_change_state_head);
Signature · 1 line
1.6.6
#11 lore raw

RFC PATCH 11/48 error: Move qemu_error & friends into their own header

In reply to Markus Armbruster
---
 hw/scsi-bus.c     |    2 +-
 hw/scsi-disk.c    |    3 +--
 hw/scsi-generic.c |    1 +
 hw/usb-serial.c   |    1 +
 hw/virtio-net.c   |    1 +
 hw/virtio-pci.c   |    2 +-
 monitor.h         |    1 +
 net/dump.c        |    1 +
 net/socket.c      |    2 +-
 net/tap-linux.c   |    1 +
 net/tap.c         |    1 +
 qemu-config.c     |    1 +
 qemu-error.h      |   12 ++++++++++++
 qemu-tool.c       |    2 +-
 qerror.c          |    2 +-
 sysemu.h          |    8 --------
 16 files changed, 26 insertions(+), 15 deletions(-)
 create mode 100644 qemu-error.h
hw/scsi-bus.c +1 -1
index a2f9cc1..c41ce9b 100644
@@ -1,5 +1,5 @@
 #include "hw.h"
-#include "sysemu.h"
+#include "qemu-error.h"
 #include "scsi.h"
 #include "scsi-defs.h"
 #include "block.h"
hw/scsi-disk.c +1 -2
index b2f61fe..b1424de 100644
@@ -19,8 +19,6 @@
  * the host adapter emulator.
  */
 
-#include <qemu-common.h>
-#include <sysemu.h>
 //#define DEBUG_SCSI
 
 #ifdef DEBUG_SCSI
@@ -34,6 +32,7 @@ do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "block.h"
 #include "scsi.h"
 #include "scsi-defs.h"
hw/scsi-generic.c +1 -0
index de778ef..cfd9903 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "block.h"
 #include "scsi.h"
 
hw/usb-serial.c +1 -0
index c3f3401..e78c6c0 100644
@@ -9,6 +9,7 @@
  */
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "usb.h"
 #include "qemu-char.h"
 
hw/virtio-net.c +1 -0
index 5c0093e..8359be6 100644
@@ -15,6 +15,7 @@
 #include "net.h"
 #include "net/checksum.h"
 #include "net/tap.h"
+#include "qemu-error.h"
 #include "qemu-timer.h"
 #include "virtio-net.h"
 
hw/virtio-pci.c +1 -1
index bcd40f7..7781328 100644
@@ -19,7 +19,7 @@
 #include "virtio-blk.h"
 #include "virtio-net.h"
 #include "pci.h"
-#include "sysemu.h"
+#include "qemu-error.h"
 #include "msix.h"
 #include "net.h"
 #include "block_int.h"
monitor.h +1 -0
index 6d7969c..1222ff0 100644
@@ -3,6 +3,7 @@
 
 #include "qemu-common.h"
 #include "qemu-char.h"
+#include "qemu-error.h"
 #include "qerror.h"
 #include "qdict.h"
 #include "block.h"
net/dump.c +1 -0
index d50b4ee..e702830 100644
@@ -25,6 +25,7 @@
 #include "dump.h"
 #include "qemu-common.h"
 #include "sysemu.h"
+#include "qemu-error.h"
 #include "qemu-log.h"
 
 typedef struct DumpState {
net/socket.c +1 -1
index 442a9c7..474d573 100644
@@ -28,9 +28,9 @@
 #include "net.h"
 #include "qemu-char.h"
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 #include "qemu_socket.h"
-#include "sysemu.h"
 
 typedef struct NetSocketState {
     VLANClientState nc;
net/tap-linux.c +1 -0
index 6af9e82..c5748e6 100644
@@ -31,6 +31,7 @@
 
 #include "sysemu.h"
 #include "qemu-common.h"
+#include "qemu-error.h"
 
 int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
 {
net/tap.c +1 -0
index 7a7320c..9ba9b4a 100644
@@ -38,6 +38,7 @@
 #include "sysemu.h"
 #include "qemu-char.h"
 #include "qemu-common.h"
+#include "qemu-error.h"
 
 #include "net/tap-linux.h"
 
qemu-config.c +1 -0
index 246fae6..2c9a7a5 100644
@@ -1,4 +1,5 @@
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 #include "qemu-config.h"
 #include "sysemu.h"
qemu-error.h new file +12 -0
new file mode 100644
index 0000000..fa16113
@@ -0,0 +1,12 @@
+#ifndef QEMU_ERROR_H
+#define QEMU_ERROR_H
+
+void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void qemu_error_internal(const char *file, int linenr, const char *func,
+                         const char *fmt, ...)
+                         __attribute__ ((format(printf, 4, 5)));
+
+#define qemu_error_new(fmt, ...) \
+    qemu_error_internal(__FILE__, __LINE__, __func__, fmt, ## __VA_ARGS__)
+
+#endif
qemu-tool.c +1 -1
index bbce99f..26f46eb 100644
@@ -13,9 +13,9 @@
 
 #include "qemu-common.h"
 #include "monitor.h"
-#include "sysemu.h"
 #include "qemu-timer.h"
 #include "qemu-log.h"
+#include "qemu-error.h"
 
 #include <sys/time.h>
 
qerror.c +1 -1
index 2f657f4..a418bde 100644
@@ -12,8 +12,8 @@
 #include "qjson.h"
 #include "qerror.h"
 #include "qstring.h"
-#include "sysemu.h"
 #include "qemu-common.h"
+#include "qemu-error.h"
 
 static void qerror_destroy_obj(QObject *obj);
 
sysemu.h +0 -8
index cd84ee8..6a2dd93 100644
@@ -69,14 +69,6 @@ int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f);
 void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f);
 int qemu_loadvm_state(QEMUFile *f);
 
-void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
-void qemu_error_internal(const char *file, int linenr, const char *func,
-                         const char *fmt, ...)
-                         __attribute__ ((format(printf, 4, 5)));
-
-#define qemu_error_new(fmt, ...) \
-    qemu_error_internal(__FILE__, __LINE__, __func__, fmt, ## __VA_ARGS__)
-
 #ifdef _WIN32
 /* Polling handling */
 
Signature · 1 line
1.6.6
#12 lore raw

Re: [Qemu-devel] [PATCH RFC 11/48] error: Move qemu_error & friends into their own header

Quoted 13 lines · On Wed, 24 Feb 2010 18:55:23 +0100 Markus Armbruster <[email protected]> wrote:
---
 hw/scsi-bus.c     |    2 +-
 hw/scsi-disk.c    |    3 +--
 hw/scsi-generic.c |    1 +
 hw/usb-serial.c   |    1 +
 hw/virtio-net.c   |    1 +
 hw/virtio-pci.c   |    2 +-
 monitor.h         |    1 +
 net/dump.c        |    1 +
 net/socket.c      |    2 +-
 net/tap-linux.c   |    1 +
Quoted 8 lines
 net/tap.c         |    1 +
 qemu-config.c     |    1 +
 qemu-error.h      |   12 ++++++++++++
 qemu-tool.c       |    2 +-
 qerror.c          |    2 +-
 sysemu.h          |    8 --------
 16 files changed, 26 insertions(+), 15 deletions(-)
 create mode 100644 qemu-error.h

Minor: isn't this change related to patch 02/48? I would merge them or
move this one closer to 02.

#13 lore raw

Re: [Qemu-devel] [PATCH RFC 11/48] error: Move qemu_error & friends into their own header

Quoted 25 lines · Luiz Capitulino
Quoted 13 lines · On Wed, 24 Feb 2010 18:55:23 +0100 Markus Armbruster <[email protected]> wrote:
---
 hw/scsi-bus.c     |    2 +-
 hw/scsi-disk.c    |    3 +--
 hw/scsi-generic.c |    1 +
 hw/usb-serial.c   |    1 +
 hw/virtio-net.c   |    1 +
 hw/virtio-pci.c   |    2 +-
 monitor.h         |    1 +
 net/dump.c        |    1 +
 net/socket.c      |    2 +-
 net/tap-linux.c   |    1 +
Quoted 8 lines
 net/tap.c         |    1 +
 qemu-config.c     |    1 +
 qemu-error.h      |   12 ++++++++++++
 qemu-tool.c       |    2 +-
 qerror.c          |    2 +-
 sysemu.h          |    8 --------
 16 files changed, 26 insertions(+), 15 deletions(-)
 create mode 100644 qemu-error.h
Quoted 2 lines

Minor: isn't this change related to patch 02/48? I would merge them or
move this one closer to 02.

I went for minimal patches, as close to their motivation as possible. I kept these two separate, because in each of them the code motion is a bit more obvious than it would be in a merged patch.

01-02/48 are motivated by the desire to keep general error handling out of a specific customer, the monitor. I could move it down some, say between 9/48 and 10/48.

11/48 is motivated by the desire not to further pollute sysemu.h with the new error stuff, which starts in 12/48. I could squash it into 02/48.

I like it the way it is well enough, but I'm not particular about it, so if you really prefer one of the above moves or squashes, let me know.

#14 lore raw

RFC PATCH 12/48 error: New error_printf() and error_vprintf()

In reply to Markus Armbruster

FIXME They should return int, so callers can calculate width.

---
 qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 qemu-error.h |   14 ++++++++++++++
 2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +42 -7
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include "monitor.h"
 #include "sysemu.h"
 
-void qemu_error(const char *fmt, ...)
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
-
-    va_start(args, fmt);
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
     } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
     }
-    va_end(args);
+}
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}
+
+void qemu_error(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
 }
 
 void qemu_error_internal(const char *file, int linenr, const char *func,
qemu-error.h +14 -0
index fa16113..d90f1da 100644
@@ -1,6 +1,20 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #ifndef QEMU_ERROR_H
 #define QEMU_ERROR_H
 
+void error_vprintf(const char *fmt, va_list ap);
+void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
Signature · 1 line
1.6.6
#15 lore raw

Re: [Qemu-devel] [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

Quoted 39 lines · On Wed, 24 Feb 2010 18:55:24 +0100 Markus Armbruster <[email protected]> wrote:

FIXME They should return int, so callers can calculate width.

---
 qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 qemu-error.h |   14 ++++++++++++++
 2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +18 -3
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include "monitor.h"
 #include "sysemu.h"
 
-void qemu_error(const char *fmt, ...)
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
-

qemu-error.c line 29

Quoted 10 lines
qemu-error.c +3 -4
-    va_start(args, fmt);
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
     } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
     }
-    va_end(args);
+}

This can be static.

qemu-error.c line 42

Quoted 13 lines
qemu-error.c +13 -0
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}

This function's name is inconsistent with qemu_error() and
qemu_error_new().

#16 lore raw

Re: [Qemu-devel] [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

Quoted 46 lines · Luiz Capitulino
Quoted 43 lines · On Wed, 24 Feb 2010 18:55:24 +0100 Markus Armbruster <[email protected]> wrote:

FIXME They should return int, so callers can calculate width.

---
 qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 qemu-error.h |   14 ++++++++++++++
 2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +19 -5
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include "monitor.h"
 #include "sysemu.h"
 
-void qemu_error(const char *fmt, ...)
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
-
-    va_start(args, fmt);
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
Quoted 8 lines

qemu-error.c line 29

Quoted 6 lines
qemu-error.c +2 -2
     } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
     }
-    va_end(args);
+}

This can be static.

Yes. But why would that be useful? It's neither a name space pollution nor does it poke a hole into an abstraction.

Quoted 8 lines
Quoted 8 lines
qemu-error.c +8 -0
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
Quoted 8 lines

qemu-error.c line 42

Quoted 5 lines
qemu-error.c +5 -0
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}

This function's name is inconsistent with qemu_error() and
qemu_error_new().

I'm fond of prepending qemu_ to random symbols left and right. Yes, I know I'm reading QEMU source code, thank you :)

If the names here are really important: What about stripping qemu_ from qemu_error() & friends?

#17 lore raw

[Qemu-devel] Re: [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

Quoted 77 lines · Markus Armbruster
Quoted 46 lines · Luiz Capitulino
Quoted 43 lines · On Wed, 24 Feb 2010 18:55:24 +0100 Markus Armbruster<[email protected]> wrote:

FIXME They should return int, so callers can calculate width.

---
  qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
  qemu-error.h |   14 ++++++++++++++
  2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +19 -5
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster<[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
  #include<stdio.h>
  #include "monitor.h"
  #include "sysemu.h"
 
-void qemu_error(const char *fmt, ...)
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
  {
-    va_list args;
-
-    va_start(args, fmt);
      if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
Quoted 8 lines

qemu-error.c line 29

Quoted 6 lines
qemu-error.c +2 -2
      } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
      }
-    va_end(args);
+}

This can be static.

Yes. But why would that be useful? It's neither a name space pollution nor does it poke a hole into an abstraction.

Quoted 8 lines
Quoted 8 lines
qemu-error.c +8 -0
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
Quoted 8 lines

qemu-error.c line 42

Quoted 5 lines
qemu-error.c +5 -0
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}

This function's name is inconsistent with qemu_error() and
qemu_error_new().

Quoted 5 lines

I'm fond of prepending qemu_ to random symbols left and right. Yes, I know I'm reading QEMU source code, thank you :)

If the names here are really important: What about stripping qemu_ from qemu_error()& friends?

Since we are at it, qemu_error_new could be renamed to qerror_raise or error_raise (since it returns void). Not worthwhile if you're not changing the name already, but in that case...

Paolo

#18 lore raw

Re: [Qemu-devel] [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

In reply to Markus Armbruster
Quoted 57 lines · On Mon, 01 Mar 2010 09:54:32 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 46 lines · Luiz Capitulino
Quoted 43 lines · On Wed, 24 Feb 2010 18:55:24 +0100 Markus Armbruster <[email protected]> wrote:

FIXME They should return int, so callers can calculate width.

---
 qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 qemu-error.h |   14 ++++++++++++++
 2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +19 -5
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@
+/*
+ * Error reporting
+ *
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include "monitor.h"
 #include "sysemu.h"
 
-void qemu_error(const char *fmt, ...)
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
-
-    va_start(args, fmt);
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
Quoted 8 lines

qemu-error.c line 29

Quoted 6 lines
qemu-error.c +2 -2
     } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
     }
-    va_end(args);
+}

This can be static.

Quoted 2 lines

Yes. But why would that be useful? It's neither a name space pollution nor does it poke a hole into an abstraction.

Well, IMHO unused public symbols serve only one purpose: to pollute the
global namespace :)

So, I think the question is: if it doesn't have any user and if you
don't expect it to be used anytime soon: why make it public?

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
qemu-error.c +8 -0
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
Quoted 8 lines

qemu-error.c line 42

Quoted 5 lines
qemu-error.c +5 -0
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}

This function's name is inconsistent with qemu_error() and
qemu_error_new().

Quoted 5 lines

I'm fond of prepending qemu_ to random symbols left and right. Yes, I know I'm reading QEMU source code, thank you :)

If the names here are really important: What about stripping qemu_ from qemu_error() & friends?

I'm ok with that (and Paolo gave some suggestions), but I hope you
submit a patch soon. It's ok to criticize/improve bad consistency policies, but it's not ok to break them.

#19 lore raw

Re: [Qemu-devel] [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

Quoted 21 lines · Luiz Capitulino
Quoted 18 lines · On Mon, 01 Mar 2010 09:54:32 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 16 lines · Luiz Capitulino
Quoted 13 lines · On Wed, 24 Feb 2010 18:55:24 +0100 Markus Armbruster <[email protected]> wrote:

FIXME They should return int, so callers can calculate width.

---
 qemu-error.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 qemu-error.h |   14 ++++++++++++++
 2 files changed, 56 insertions(+), 7 deletions(-)
qemu-error.c +0 -0
index 63bcdcf..76c660a 100644
@@ -1,18 +1,53 @@

[...]

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 9 lines
qemu-error.c +6 -2
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME should return int, so callers can calculate width, but that
+ * requires surgery to monitor_printf().  Left for another day.
+ */
+void error_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
-

qemu-error.c line 29

Quoted 10 lines
qemu-error.c +3 -4
-    va_start(args, fmt);
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, args);
+        monitor_vprintf(cur_mon, fmt, ap);
     } else {
-        vfprintf(stderr, fmt, args);
+        vfprintf(stderr, fmt, ap);
     }
-    va_end(args);
+}

This can be static.

Quoted 8 lines
Quoted 2 lines

Yes. But why would that be useful? It's neither a name space pollution nor does it poke a hole into an abstraction.

Well, IMHO unused public symbols serve only one purpose: to pollute the
global namespace :)

So, I think the question is: if it doesn't have any user and if you
don't expect it to be used anytime soon: why make it public?

It's a basic building block. Uses will come.

Quoted 20 lines
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
qemu-error.c +8 -0
+
+/*
+ * Print to current monitor if we have one, else to stderr.
+ * FIXME just like error_vprintf()
+ */
+void error_printf(const char *fmt, ...)
+{
+    va_list ap;
Quoted 8 lines

qemu-error.c line 42

Quoted 5 lines
qemu-error.c +5 -0
+
+    va_start(ap, fmt);
+    error_vprintf(fmt, ap);
+    va_end(ap);
+}

This function's name is inconsistent with qemu_error() and
qemu_error_new().

I'm fond of prepending qemu_ to random symbols left and right. Yes, I know I'm reading QEMU source code, thank you :)

Quoted 6 lines
Quoted 2 lines

If the names here are really important: What about stripping qemu_ from qemu_error() & friends?

I'm ok with that (and Paolo gave some suggestions), but I hope you
submit a patch soon. It's ok to criticize/improve bad consistency policies, but it's not ok to break them.

Paolo's error_raise() works for me, although I like error_report() a bit better.

But we need two names, one for simple, direct error reporting (now qemu_error()), and one for QMP-compatible error reporting (now qemu_error_new()).

Call them error_report() and qerror_report()? Or is that too similar?

#20 lore raw

[Qemu-devel] Re: [PATCH RFC 12/48] error: New error_printf() and error_vprintf()

Quoted 2 lines · Markus Armbruster

Paolo's error_raise() works for me, although I like error_report() a bit better.

error_report is also fine, of course.

Quoted 5 lines

But we need two names, one for simple, direct error reporting (now qemu_error()), and one for QMP-compatible error reporting (now qemu_error_new()).

Call them error_report() and qerror_report()? Or is that too similar?

Actually I like it.

Paolo

#21 lore raw

RFC PATCH 13/48 error: Make qemu_error() add a newline, strip it from arguments

In reply to Markus Armbruster

This fixes a few error messages lacking a newline:
net_handle_fd_param()'s "No file descriptor named %s found", and tap_open()'s "vnet_hdr=1 requested, but no kernel support for IFF_VNET_HDR available" (all three versions).

There's one place that passes arguments without newlines
intentionally: load_vmstate(). Fix it up.

---
 hw/pc.c                |    4 ++--
 hw/pci-hotplug.c       |    4 ++--
 hw/pci.c               |    8 ++++----
 hw/qdev.c              |   36 ++++++++++++++++++------------------
 hw/scsi-bus.c          |    2 +-
 hw/scsi-disk.c         |    2 +-
 hw/scsi-generic.c      |    8 ++++----
 hw/usb-bus.c           |    4 ++--
 hw/usb-msd.c           |    2 +-
 hw/usb-serial.c        |    8 ++++----
 hw/virtio-net.c        |    4 ++--
 hw/virtio-pci.c        |    2 +-
 hw/virtio-serial-bus.c |    2 +-
 net.c                  |   24 ++++++++++++------------
 net/dump.c             |    4 ++--
 net/slirp.c            |   20 ++++++++++----------
 net/socket.c           |   10 +++++-----
 net/tap-bsd.c          |    3 ++-
 net/tap-linux.c        |    4 ++--
 net/tap-win32.c        |    2 +-
 net/tap.c              |    2 +-
 qemu-config.c          |    8 ++++----
 qemu-error.c           |    1 +
 qerror.c               |    2 +-
 savevm.c               |   17 +++++++++--------
 vl.c                   |    4 ++--
 26 files changed, 95 insertions(+), 92 deletions(-)
hw/pc.c +2 -2
index 1595283..e9d66d4 100644
@@ -238,13 +238,13 @@ static int set_boot_dev(RTCState *s, const char *boot_device, int fd_bootchk)
 
     nbds = strlen(boot_device);
     if (nbds > PC_MAX_BOOT_DEVICES) {
-        qemu_error("Too many boot devices for PC\n");
+        qemu_error("Too many boot devices for PC");
         return(1);
     }
     for (i = 0; i < nbds; i++) {
         bds[i] = boot_device2nibble(boot_device[i]);
         if (bds[i] == 0) {
-            qemu_error("Invalid boot device for PC: '%c'\n",
+            qemu_error("Invalid boot device for PC: '%c'",
                        boot_device[i]);
             return(1);
         }
hw/pci-hotplug.c +2 -2
index bd82c6a..215dc42 100644
@@ -80,7 +80,7 @@ static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
 
     scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
     if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
-        qemu_error("Device is not a SCSI adapter\n");
+        qemu_error("Device is not a SCSI adapter");
         return -1;
     }
 
@@ -97,7 +97,7 @@ static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
     dinfo->unit = scsidev->id;
 
     if (printinfo)
-        qemu_error("OK bus %d, unit %d\n", scsibus->busnr, scsidev->id);
+        qemu_error("OK bus %d, unit %d", scsibus->busnr, scsidev->id);
     return 0;
 }
 
hw/pci.c +4 -4
index eb2043e..a9aad5b 100644
@@ -589,11 +589,11 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
             if (!bus->devices[devfn])
                 goto found;
         }
-        qemu_error("PCI: no devfn available for %s, all in use\n", name);
+        qemu_error("PCI: no devfn available for %s, all in use", name);
         return NULL;
     found: ;
     } else if (bus->devices[devfn]) {
-        qemu_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
+        qemu_error("PCI: devfn %d not available for %s, in use by %s", devfn,
                  name, bus->devices[devfn]->name);
         return NULL;
     }
@@ -1476,7 +1476,7 @@ PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
 
     bus = pci_get_bus_devfn(&devfn, devaddr);
     if (!bus) {
-        qemu_error("Invalid PCI device address %s for device %s\n",
+        qemu_error("Invalid PCI device address %s for device %s",
                    devaddr, pci_nic_names[i]);
         return NULL;
     }
@@ -1768,7 +1768,7 @@ static int pci_add_option_rom(PCIDevice *pdev)
 
     size = get_image_size(path);
     if (size < 0) {
-        qemu_error("%s: failed to find romfile \"%s\"\n", __FUNCTION__,
+        qemu_error("%s: failed to find romfile \"%s\"", __FUNCTION__,
                    pdev->romfile);
         return -1;
     }
hw/qdev.c +18 -18
index d0052d4..a992479 100644
@@ -146,7 +146,7 @@ static int set_property(const char *name, const char *value, void *opaque)
         return 0;
 
     if (qdev_prop_parse(dev, name, value) == -1) {
-        qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
+        qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"",
                    name, value, dev->info->name);
         return -1;
     }
@@ -164,7 +164,7 @@ int qdev_device_help(QemuOpts *opts)
     if (driver && !strcmp(driver, "?")) {
         for (info = device_info_list; info != NULL; info = info->next) {
             qdev_print_devinfo(info, msg, sizeof(msg));
-            qemu_error("%s\n", msg);
+            qemu_error("%s", msg);
         }
         return 1;
     }
@@ -179,7 +179,7 @@ int qdev_device_help(QemuOpts *opts)
     }
 
     for (prop = info->props; prop && prop->name; prop++) {
-        qemu_error("%s.%s=%s\n", info->name, prop->name, prop->info->name);
+        qemu_error("%s.%s=%s", info->name, prop->name, prop->info->name);
     }
     return 1;
 }
@@ -193,7 +193,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
-        qemu_error("-device: no driver specified\n");
+        qemu_error("-device: no driver specified");
         return NULL;
     }
 
@@ -204,7 +204,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         return NULL;
     }
     if (info->no_user) {
-        qemu_error("device \"%s\" can't be added via command line\n",
+        qemu_error("device \"%s\" can't be added via command line",
                    info->name);
         return NULL;
     }
@@ -217,12 +217,12 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
     }
     if (!bus) {
-        qemu_error("Did not find %s bus for %s\n",
+        qemu_error("Did not find %s bus for %s",
                    path ? path : info->bus_info->name, info->name);
         return NULL;
     }
     if (qdev_hotplug && !bus->allow_hotplug) {
-        qemu_error("Bus %s does not support hotplugging\n",
+        qemu_error("Bus %s does not support hotplugging",
                    bus->name);
         return NULL;
     }
@@ -238,7 +238,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         return NULL;
     }
     if (qdev_init(qdev) < 0) {
-        qemu_error("Error initializing device %s\n", driver);
+        qemu_error("Error initializing device %s", driver);
         return NULL;
     }
     qdev->opts = opts;
@@ -277,7 +277,7 @@ int qdev_init(DeviceState *dev)
 int qdev_unplug(DeviceState *dev)
 {
     if (!dev->parent_bus->allow_hotplug) {
-        qemu_error("Bus %s does not support hotplugging\n",
+        qemu_error("Bus %s does not support hotplugging",
                    dev->parent_bus->name);
         return -1;
     }
@@ -549,12 +549,12 @@ static BusState *qbus_find(const char *path)
         pos = 0;
     } else {
         if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\")\n", path);
+            qemu_error("path parse error (\"%s\")", path);
             return NULL;
         }
         bus = qbus_find_recursive(main_system_bus, elem, NULL);
         if (!bus) {
-            qemu_error("bus \"%s\" not found\n", elem);
+            qemu_error("bus \"%s\" not found", elem);
             return NULL;
         }
         pos = len;
@@ -568,14 +568,14 @@ static BusState *qbus_find(const char *path)
 
         /* find device */
         if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
+            qemu_error("path parse error (\"%s\" pos %d)", path, pos);
             return NULL;
         }
         pos += len;
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
             qbus_list_dev(bus, msg, sizeof(msg));
-            qemu_error("device \"%s\" not found\n%s\n", elem, msg);
+            qemu_error("device \"%s\" not found\n%s", elem, msg);
             return NULL;
         }
         if (path[pos] == '\0') {
@@ -583,13 +583,13 @@ static BusState *qbus_find(const char *path)
              * one child bus accept it nevertheless */
             switch (dev->num_child_bus) {
             case 0:
-                qemu_error("device has no child bus (%s)\n", path);
+                qemu_error("device has no child bus (%s)", path);
                 return NULL;
             case 1:
                 return QLIST_FIRST(&dev->child_bus);
             default:
                 qbus_list_bus(dev, msg, sizeof(msg));
-                qemu_error("device has multiple child busses (%s)\n%s\n",
+                qemu_error("device has multiple child busses (%s)\n%s",
                            path, msg);
                 return NULL;
             }
@@ -597,14 +597,14 @@ static BusState *qbus_find(const char *path)
 
         /* find bus */
         if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
+            qemu_error("path parse error (\"%s\" pos %d)", path, pos);
             return NULL;
         }
         pos += len;
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
             qbus_list_bus(dev, msg, sizeof(msg));
-            qemu_error("child bus \"%s\" not found\n%s\n", elem, msg);
+            qemu_error("child bus \"%s\" not found\n%s", elem, msg);
             return NULL;
         }
     }
@@ -763,7 +763,7 @@ void do_device_del(Monitor *mon, const QDict *qdict)
 
     dev = qdev_find_recursive(main_system_bus, id);
     if (NULL == dev) {
-        qemu_error("Device '%s' not found\n", id);
+        qemu_error("Device '%s' not found", id);
         return;
     }
     qdev_unplug(dev);
hw/scsi-bus.c +1 -1
index c41ce9b..0aaf068 100644
@@ -41,7 +41,7 @@ static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
         }
     }
     if (dev->id >= bus->ndev) {
-        qemu_error("bad scsi device id: %d\n", dev->id);
+        qemu_error("bad scsi device id: %d", dev->id);
         goto err;
     }
 
hw/scsi-disk.c +1 -1
index b1424de..93ece93 100644
@@ -1021,7 +1021,7 @@ static int scsi_disk_initfn(SCSIDevice *dev)
     uint64_t nb_sectors;
 
     if (!s->qdev.conf.dinfo || !s->qdev.conf.dinfo->bdrv) {
-        qemu_error("scsi-disk: drive property not set\n");
+        qemu_error("scsi-disk: drive property not set");
         return -1;
     }
     s->bs = s->qdev.conf.dinfo->bdrv;
hw/scsi-generic.c +4 -4
index cfd9903..f3f101f 100644
@@ -464,27 +464,27 @@ static int scsi_generic_initfn(SCSIDevice *dev)
     struct sg_scsi_id scsiid;
 
     if (!s->qdev.conf.dinfo || !s->qdev.conf.dinfo->bdrv) {
-        qemu_error("scsi-generic: drive property not set\n");
+        qemu_error("scsi-generic: drive property not set");
         return -1;
     }
     s->bs = s->qdev.conf.dinfo->bdrv;
 
     /* check we are really using a /dev/sg* file */
     if (!bdrv_is_sg(s->bs)) {
-        qemu_error("scsi-generic: not /dev/sg*\n");
+        qemu_error("scsi-generic: not /dev/sg*");
         return -1;
     }
 
     /* check we are using a driver managing SG_IO (version 3 and after */
     if (bdrv_ioctl(s->bs, SG_GET_VERSION_NUM, &sg_version) < 0 ||
         sg_version < 30000) {
-        qemu_error("scsi-generic: scsi generic interface too old\n");
+        qemu_error("scsi-generic: scsi generic interface too old");
         return -1;
     }
 
     /* get LUN of the /dev/sg? */
     if (bdrv_ioctl(s->bs, SG_GET_SCSI_ID, &scsiid)) {
-        qemu_error("scsi-generic: SG_GET_SCSI_ID ioctl failed\n");
+        qemu_error("scsi-generic: SG_GET_SCSI_ID ioctl failed");
         return -1;
     }
 
hw/usb-bus.c +2 -2
index 54027df..2eba8a0 100644
@@ -288,14 +288,14 @@ USBDevice *usbdevice_create(const char *cmdline)
     if (info == NULL) {
 #if 0
         /* no error because some drivers are not converted (yet) */
-        qemu_error("usbdevice %s not found\n", driver);
+        qemu_error("usbdevice %s not found", driver);
 #endif
         return NULL;
     }
 
     if (!usb->usbdevice_init) {
         if (params) {
-            qemu_error("usbdevice %s accepts no params\n", driver);
+            qemu_error("usbdevice %s accepts no params", driver);
             return NULL;
         }
         return usb_create_simple(bus, usb->qdev.name);
hw/usb-msd.c +1 -1
index 1e3e101..db79f71 100644
@@ -524,7 +524,7 @@ static int usb_msd_initfn(USBDevice *dev)
     MSDState *s = DO_UPCAST(MSDState, dev, dev);
 
     if (!s->conf.dinfo || !s->conf.dinfo->bdrv) {
-        qemu_error("usb-msd: drive property not set\n");
+        qemu_error("usb-msd: drive property not set");
         return -1;
     }
 
hw/usb-serial.c +4 -4
index e78c6c0..b724de6 100644
@@ -565,26 +565,26 @@ static USBDevice *usb_serial_init(const char *filename)
         if (strstart(filename, "vendorid=", &p)) {
             vendorid = strtol(p, &e, 16);
             if (e == p || (*e && *e != ',' && *e != ':')) {
-                qemu_error("bogus vendor ID %s\n", p);
+                qemu_error("bogus vendor ID %s", p);
                 return NULL;
             }
             filename = e;
         } else if (strstart(filename, "productid=", &p)) {
             productid = strtol(p, &e, 16);
             if (e == p || (*e && *e != ',' && *e != ':')) {
-                qemu_error("bogus product ID %s\n", p);
+                qemu_error("bogus product ID %s", p);
                 return NULL;
             }
             filename = e;
         } else {
-            qemu_error("unrecognized serial USB option %s\n", filename);
+            qemu_error("unrecognized serial USB option %s", filename);
             return NULL;
         }
         while(*filename == ',')
             filename++;
     }
     if (!*filename) {
-        qemu_error("character device specification needed\n");
+        qemu_error("character device specification needed");
         return NULL;
     }
     filename++;
hw/virtio-net.c +2 -2
index 8359be6..a26eb21 100644
@@ -765,7 +765,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
 
     if (version_id >= 7) {
         if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
-            qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
+            qemu_error("virtio-net: saved image requires vnet_hdr=on");
             return -1;
         }
 
@@ -794,7 +794,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
 
     if (version_id >= 11) {
         if (qemu_get_byte(f) && !peer_has_ufo(n)) {
-            qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
+            qemu_error("virtio-net: saved image requires TUN_F_UFO support");
             return -1;
         }
     }
hw/virtio-pci.c +1 -1
index 7781328..4e0d5a2 100644
@@ -459,7 +459,7 @@ static int virtio_blk_init_pci(PCIDevice *pci_dev)
         proxy->class_code = PCI_CLASS_STORAGE_SCSI;
 
     if (!proxy->block.dinfo) {
-        qemu_error("virtio-blk-pci: drive property not set\n");
+        qemu_error("virtio-blk-pci: drive property not set");
         return -1;
     }
     vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block);
hw/virtio-serial-bus.c +1 -1
index d0e0219..2ba4c4a 100644
@@ -485,7 +485,7 @@ static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
     plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
 
     if (port->vser->config.nr_ports == bus->max_nr_ports && !plugging_port0) {
-        qemu_error("virtio-serial-bus: Maximum device limit reached\n");
+        qemu_error("virtio-serial-bus: Maximum device limit reached");
         return -1;
     }
     dev->info = info;
net.c +12 -12
index a1bf49f..2479e65 100644
@@ -731,7 +731,7 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models,
             return i;
     }
 
-    qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
+    qemu_error("qemu: Unsupported NIC model: %s", nd->model);
     return -1;
 }
 
@@ -763,7 +763,7 @@ static int net_init_nic(QemuOpts *opts,
 
     idx = nic_get_free_idx();
     if (idx == -1 || nb_nics >= MAX_NICS) {
-        qemu_error("Too Many NICs\n");
+        qemu_error("Too Many NICs");
         return -1;
     }
 
@@ -774,7 +774,7 @@ static int net_init_nic(QemuOpts *opts,
     if ((netdev = qemu_opt_get(opts, "netdev"))) {
         nd->netdev = qemu_find_netdev(netdev);
         if (!nd->netdev) {
-            qemu_error("netdev '%s' not found\n", netdev);
+            qemu_error("netdev '%s' not found", netdev);
             return -1;
         }
     } else {
@@ -800,14 +800,14 @@ static int net_init_nic(QemuOpts *opts,
 
     if (qemu_opt_get(opts, "macaddr") &&
         net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
-        qemu_error("invalid syntax for ethernet address\n");
+        qemu_error("invalid syntax for ethernet address");
         return -1;
     }
 
     nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
     if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
         (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
-        qemu_error("invalid # of vectors: %d\n", nd->nvectors);
+        qemu_error("invalid # of vectors: %d", nd->nvectors);
         return -1;
     }
 
@@ -1057,12 +1057,12 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
 
     if (!is_netdev) {
         if (!type) {
-            qemu_error("No type specified for -net\n");
+            qemu_error("No type specified for -net");
             return -1;
         }
     } else {
         if (!type) {
-            qemu_error("No type specified for -netdev\n");
+            qemu_error("No type specified for -netdev");
             return -1;
         }
 
@@ -1074,21 +1074,21 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
             strcmp(type, "vde") != 0 &&
 #endif
             strcmp(type, "socket") != 0) {
-            qemu_error("The '%s' network backend type is not valid with -netdev\n",
+            qemu_error("The '%s' network backend type is not valid with -netdev",
                        type);
             return -1;
         }
 
         if (qemu_opt_get(opts, "vlan")) {
-            qemu_error("The 'vlan' parameter is not valid with -netdev\n");
+            qemu_error("The 'vlan' parameter is not valid with -netdev");
             return -1;
         }
         if (qemu_opt_get(opts, "name")) {
-            qemu_error("The 'name' parameter is not valid with -netdev\n");
+            qemu_error("The 'name' parameter is not valid with -netdev");
             return -1;
         }
         if (!qemu_opts_id(opts)) {
-            qemu_error("The id= parameter is required with -netdev\n");
+            qemu_error("The id= parameter is required with -netdev");
             return -1;
         }
     }
@@ -1121,7 +1121,7 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
         }
     }
 
-    qemu_error("Invalid -net type '%s'\n", type);
+    qemu_error("Invalid -net type '%s'", type);
     return -1;
 }
 
net/dump.c +2 -2
index e702830..9c7bab4 100644
@@ -108,7 +108,7 @@ static int net_dump_init(VLANState *vlan, const char *device,
 
     fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
     if (fd < 0) {
-        qemu_error("-net dump: can't open %s\n", filename);
+        qemu_error("-net dump: can't open %s", filename);
         return -1;
     }
 
@@ -121,7 +121,7 @@ static int net_dump_init(VLANState *vlan, const char *device,
     hdr.linktype = 1;
 
     if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
-        qemu_error("-net dump write error: %s\n", strerror(errno));
+        qemu_error("-net dump write error: %s", strerror(errno));
         close(fd);
         return -1;
     }
net/slirp.c +10 -10
index 317cca7..b7ee3ab 100644
@@ -413,14 +413,14 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str,
 
     if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
                           guest_port) < 0) {
-        qemu_error("could not set up host forwarding rule '%s'\n",
+        qemu_error("could not set up host forwarding rule '%s'",
                    redir_str);
         return -1;
     }
     return 0;
 
  fail_syntax:
-    qemu_error("invalid host forwarding rule '%s'\n", redir_str);
+    qemu_error("invalid host forwarding rule '%s'", redir_str);
     return -1;
 }
 
@@ -473,9 +473,9 @@ static void slirp_smb_cleanup(SlirpState *s)
         snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
         ret = system(cmd);
         if (!WIFEXITED(ret)) {
-            qemu_error("'%s' failed.\n", cmd);
+            qemu_error("'%s' failed.", cmd);
         } else if (WEXITSTATUS(ret)) {
-            qemu_error("'%s' failed. Error code: %d\n",
+            qemu_error("'%s' failed. Error code: %d",
                     cmd, WEXITSTATUS(ret));
         }
         s->smb_dir[0] = '\0';
@@ -493,7 +493,7 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
     snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
              (long)getpid(), instance++);
     if (mkdir(s->smb_dir, 0700) < 0) {
-        qemu_error("could not create samba server dir '%s'\n", s->smb_dir);
+        qemu_error("could not create samba server dir '%s'", s->smb_dir);
         return -1;
     }
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
@@ -501,7 +501,7 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
     f = fopen(smb_conf, "w");
     if (!f) {
         slirp_smb_cleanup(s);
-        qemu_error("could not create samba server configuration file '%s'\n",
+        qemu_error("could not create samba server configuration file '%s'",
                    smb_conf);
         return -1;
     }
@@ -533,7 +533,7 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
 
     if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) {
         slirp_smb_cleanup(s);
-        qemu_error("conflicting/invalid smbserver address\n");
+        qemu_error("conflicting/invalid smbserver address");
         return -1;
     }
     return 0;
@@ -618,14 +618,14 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
     snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port);
     fwd->hd = qemu_chr_open(buf, p, NULL);
     if (!fwd->hd) {
-        qemu_error("could not open guest forwarding device '%s'\n", buf);
+        qemu_error("could not open guest forwarding device '%s'", buf);
         qemu_free(fwd);
         return -1;
     }
 
     if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
         qemu_error("conflicting/invalid host:port in guest forwarding "
-                   "rule '%s'\n", config_str);
+                   "rule '%s'", config_str);
         qemu_free(fwd);
         return -1;
     }
@@ -638,7 +638,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
     return 0;
 
  fail_syntax:
-    qemu_error("invalid guest forwarding rule '%s'\n", config_str);
+    qemu_error("invalid guest forwarding rule '%s'", config_str);
     return -1;
 }
 
net/socket.c +5 -5
index 474d573..09d6ad8 100644
@@ -506,7 +506,7 @@ int net_init_socket(QemuOpts *opts,
         if (qemu_opt_get(opts, "listen") ||
             qemu_opt_get(opts, "connect") ||
             qemu_opt_get(opts, "mcast")) {
-            qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
+            qemu_error("listen=, connect= and mcast= is invalid with fd=");
             return -1;
         }
 
@@ -525,7 +525,7 @@ int net_init_socket(QemuOpts *opts,
         if (qemu_opt_get(opts, "fd") ||
             qemu_opt_get(opts, "connect") ||
             qemu_opt_get(opts, "mcast")) {
-            qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
+            qemu_error("fd=, connect= and mcast= is invalid with listen=");
             return -1;
         }
 
@@ -540,7 +540,7 @@ int net_init_socket(QemuOpts *opts,
         if (qemu_opt_get(opts, "fd") ||
             qemu_opt_get(opts, "listen") ||
             qemu_opt_get(opts, "mcast")) {
-            qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
+            qemu_error("fd=, listen= and mcast= is invalid with connect=");
             return -1;
         }
 
@@ -555,7 +555,7 @@ int net_init_socket(QemuOpts *opts,
         if (qemu_opt_get(opts, "fd") ||
             qemu_opt_get(opts, "connect") ||
             qemu_opt_get(opts, "listen")) {
-            qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
+            qemu_error("fd=, connect= and listen= is invalid with mcast=");
             return -1;
         }
 
@@ -565,7 +565,7 @@ int net_init_socket(QemuOpts *opts,
             return -1;
         }
     } else {
-        qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
+        qemu_error("-socket requires fd=, listen=, connect= or mcast=");
         return -1;
     }
 
net/tap-bsd.c +2 -1
index 815997d..5102829 100644
@@ -69,7 +69,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required
         }
     }
     if (fd < 0) {
-        qemu_error("warning: could not open %s (%s): no virtual network emulation\n", dname, strerror(errno));
+        qemu_error("warning: could not open %s (%s): no virtual network emulation",
+                   dname, strerror(errno));
         return -1;
     }
 #else
net/tap-linux.c +2 -2
index c5748e6..646c707 100644
@@ -97,7 +97,7 @@ int tap_set_sndbuf(int fd, QemuOpts *opts)
     }
 
     if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) {
-        qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno));
+        qemu_error("TUNSETSNDBUF ioctl failed: %s", strerror(errno));
         return -1;
     }
     return 0;
@@ -108,7 +108,7 @@ int tap_probe_vnet_hdr(int fd)
     struct ifreq ifr;
 
     if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
-        qemu_error("TUNGETIFF ioctl() failed: %s\n", strerror(errno));
+        qemu_error("TUNGETIFF ioctl() failed: %s", strerror(errno));
         return 0;
     }
 
net/tap-win32.c +1 -1
index 8370c80..f1c489e 100644
@@ -706,7 +706,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
     ifname = qemu_opt_get(opts, "ifname");
 
     if (!ifname) {
-        qemu_error("tap: no interface name\n");
+        qemu_error("tap: no interface name");
         return -1;
     }
 
net/tap.c +1 -1
index 9ba9b4a..b2ba29b 100644
@@ -394,7 +394,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
             qemu_opt_get(opts, "script") ||
             qemu_opt_get(opts, "downscript") ||
             qemu_opt_get(opts, "vnet_hdr")) {
-            qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
+            qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
             return -1;
         }
 
qemu-config.c +4 -4
index 2c9a7a5..194a653 100644
@@ -313,7 +313,7 @@ static QemuOptsList *find_list(const char *group)
             break;
     }
     if (lists[i] == NULL) {
-        qemu_error("there is no option group \"%s\"\n", group);
+        qemu_error("there is no option group \"%s\"", group);
     }
     return lists[i];
 }
@@ -327,7 +327,7 @@ int qemu_set_option(const char *str)
 
     rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
     if (rc < 3 || str[offset] != '=') {
-        qemu_error("can't parse: \"%s\"\n", str);
+        qemu_error("can't parse: \"%s\"", str);
         return -1;
     }
 
@@ -338,7 +338,7 @@ int qemu_set_option(const char *str)
 
     opts = qemu_opts_find(list, id);
     if (!opts) {
-        qemu_error("there is no %s \"%s\" defined\n",
+        qemu_error("there is no %s \"%s\" defined",
                    list->name, id);
         return -1;
     }
@@ -357,7 +357,7 @@ int qemu_global_option(const char *str)
 
     rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
     if (rc < 2 || str[offset] != '=') {
-        qemu_error("can't parse: \"%s\"\n", str);
+        qemu_error("can't parse: \"%s\"", str);
         return -1;
     }
 
qemu-error.c +1 -0
index 76c660a..075c64d 100644
@@ -48,6 +48,7 @@ void qemu_error(const char *fmt, ...)
     va_start(ap, fmt);
     error_vprintf(fmt, ap);
     va_end(ap);
+    error_printf("\n");
 }
 
 void qemu_error_internal(const char *file, int linenr, const char *func,
qerror.c +1 -1
index a418bde..c09c3b8 100644
@@ -324,7 +324,7 @@ QString *qerror_human(const QError *qerror)
 void qerror_print(const QError *qerror)
 {
     QString *qstring = qerror_human(qerror);
-    qemu_error("%s\n", qstring_get_str(qstring));
+    qemu_error("%s", qstring_get_str(qstring));
     QDECREF(qstring);
 }
 
savevm.c +9 -8
index 1f6c25e..93026f7 100644
@@ -1743,7 +1743,7 @@ int load_vmstate(const char *name)
 
     bs = get_bs_snapshots();
     if (!bs) {
-        qemu_error("No block device supports snapshots\n");
+        qemu_error("No block device supports snapshots");
         return -EINVAL;
     }
 
@@ -1755,19 +1755,20 @@ int load_vmstate(const char *name)
         if (bdrv_has_snapshot(bs1)) {
             ret = bdrv_snapshot_goto(bs1, name);
             if (ret < 0) {
-                if (bs != bs1)
-                    qemu_error("Warning: ");
                 switch(ret) {
                 case -ENOTSUP:
-                    qemu_error("Snapshots not supported on device '%s'\n",
+                    qemu_error("%sSnapshots not supported on device '%s'",
+                               bs != bs1 ? "Warning: " : "",
                                bdrv_get_device_name(bs1));
                     break;
                 case -ENOENT:
-                    qemu_error("Could not find snapshot '%s' on device '%s'\n",
+                    qemu_error("%sCould not find snapshot '%s' on device '%s'",
+                               bs != bs1 ? "Warning: " : "",
                                name, bdrv_get_device_name(bs1));
                     break;
                 default:
-                    qemu_error("Error %d while activating snapshot on '%s'\n",
+                    qemu_error("%sError %d while activating snapshot on '%s'",
+                               bs != bs1 ? "Warning: " : "",
                                ret, bdrv_get_device_name(bs1));
                     break;
                 }
@@ -1786,13 +1787,13 @@ int load_vmstate(const char *name)
     /* restore the VM state */
     f = qemu_fopen_bdrv(bs, 0);
     if (!f) {
-        qemu_error("Could not open VM state file\n");
+        qemu_error("Could not open VM state file");
         return -EINVAL;
     }
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
     if (ret < 0) {
-        qemu_error("Error %d while loading VM state\n", ret);
+        qemu_error("Error %d while loading VM state", ret);
         return ret;
     }
     return 0;
vl.c +2 -2
index c4c8c1f..7d4b234 100644
@@ -2526,7 +2526,7 @@ void do_usb_add(Monitor *mon, const QDict *qdict)
 {
     const char *devname = qdict_get_str(qdict, "devname");
     if (usb_device_add(devname, 1) < 0) {
-        qemu_error("could not add USB device '%s'\n", devname);
+        qemu_error("could not add USB device '%s'", devname);
     }
 }
 
@@ -2534,7 +2534,7 @@ void do_usb_del(Monitor *mon, const QDict *qdict)
 {
     const char *devname = qdict_get_str(qdict, "devname");
     if (usb_device_del(devname) < 0) {
-        qemu_error("could not delete USB device '%s'\n", devname);
+        qemu_error("could not delete USB device '%s'", devname);
     }
 }
 
Signature · 1 line
1.6.6
#22 lore raw

Re: [Qemu-devel] [PATCH RFC 13/48] error: Make qemu_error() add a newline, strip it from arguments

Quoted 7 lines · On Wed, 24 Feb 2010 18:55:25 +0100 Markus Armbruster <[email protected]> wrote:

This fixes a few error messages lacking a newline:
net_handle_fd_param()'s "No file descriptor named %s found", and tap_open()'s "vnet_hdr=1 requested, but no kernel support for IFF_VNET_HDR available" (all three versions).

There's one place that passes arguments without newlines
intentionally: load_vmstate(). Fix it up.

I'm not a big fun of printing functions that add automatic newline,
specially in C. But I understand that you're doing this because you're going to prefix some info to the printed string.

In this case, you could rename the function to qemu_error_nl(). Or at
least document it properly in the function definition.

#23 lore raw

Re: [Qemu-devel] [PATCH RFC 13/48] error: Make qemu_error() add a newline, strip it from arguments

Quoted 11 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 24 Feb 2010 18:55:25 +0100 Markus Armbruster <[email protected]> wrote:

This fixes a few error messages lacking a newline:
net_handle_fd_param()'s "No file descriptor named %s found", and tap_open()'s "vnet_hdr=1 requested, but no kernel support for IFF_VNET_HDR available" (all three versions).

There's one place that passes arguments without newlines
intentionally: load_vmstate(). Fix it up.

Quoted 6 lines

I'm not a big fun of printing functions that add automatic newline,
specially in C. But I understand that you're doing this because you're going to prefix some info to the printed string.

In this case, you could rename the function to qemu_error_nl(). Or at
least document it properly in the function definition.

Fair enough, I'll document it.

#24 lore raw

RFC PATCH 14/48 error: Don't abuse qemu_error() for non-error in scsi_hot_add()

In reply to Markus Armbruster

Commit 30d335d6 converted an informational message from monitor_printf() to qemu_error(), probably because the latter doesn't need a mon argument. A later commit will make qemu_error() print additional stuff that is only appropriate for proper errors, and then this will break. Clean it up.

---
 hw/pci-hotplug.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
hw/pci-hotplug.c +5 -4
index 215dc42..aa7e870 100644
@@ -73,7 +73,8 @@ static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
     return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
 }
 
-static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
+static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
+                        DriveInfo *dinfo, int printinfo)
 {
     SCSIBus *scsibus;
     SCSIDevice *scsidev;
@@ -97,7 +98,7 @@ static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
     dinfo->unit = scsidev->id;
 
     if (printinfo)
-        qemu_error("OK bus %d, unit %d", scsibus->busnr, scsidev->id);
+        monitor_printf(mon, "OK bus %d, unit %d", scsibus->busnr, scsidev->id);
     return 0;
 }
 
@@ -131,7 +132,7 @@ void drive_hot_add(Monitor *mon, const QDict *qdict)
             monitor_printf(mon, "no pci device with address %s\n", pci_addr);
             goto err;
         }
-        if (scsi_hot_add(&dev->qdev, dinfo, 1) != 0) {
+        if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
             goto err;
         }
         break;
@@ -203,7 +204,7 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
         if (qdev_init(&dev->qdev) < 0)
             dev = NULL;
         if (dev && dinfo) {
-            if (scsi_hot_add(&dev->qdev, dinfo, 0) != 0) {
+            if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
                 qdev_unplug(&dev->qdev);
                 dev = NULL;
             }
Signature · 1 line
1.6.6
#25 lore raw

RFC PATCH 15/48 error: Don't abuse qemu_error() for non-error in qdev_device_help()

In reply to Markus Armbruster

qdev_device_help() prints device information with qemu_error(). A later commit will make qemu_error() print additional stuff that is only appropriate for proper errors, and then this will break. Use error_printf() instead.

While there, simplify: instead of printing a buffer filled by qdev_print_devinfo() in one go, make qdev_print_devinfo() print it.

---
 hw/qdev.c |   31 ++++++++++---------------------
 1 files changed, 10 insertions(+), 21 deletions(-)
hw/qdev.c +10 -21
index a992479..377e327 100644
@@ -113,27 +113,20 @@ DeviceState *qdev_create(BusState *bus, const char *name)
     return dev;
 }
 
-static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
+static void qdev_print_devinfo(DeviceInfo *info)
 {
-    int pos = 0;
-    int ret;
-
-    ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
-                   info->name, info->bus_info->name);
-    pos += MIN(len-pos,ret);
+    error_printf("name \"%s\", bus %s",
+                 info->name, info->bus_info->name);
     if (info->alias) {
-        ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
-        pos += MIN(len-pos,ret);
+        error_printf(", alias \"%s\"", info->alias);
     }
     if (info->desc) {
-        ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
-        pos += MIN(len-pos,ret);
+        error_printf(", desc \"%s\"", info->desc);
     }
     if (info->no_user) {
-        ret = snprintf(dest+pos, len-pos, ", no-user");
-        pos += MIN(len-pos,ret);
+        error_printf(", no-user");
     }
-    return pos;
+    error_printf("\n");
 }
 
 static int set_property(const char *name, const char *value, void *opaque)
@@ -157,14 +150,12 @@ int qdev_device_help(QemuOpts *opts)
 {
     const char *driver;
     DeviceInfo *info;
-    char msg[256];
     Property *prop;
 
     driver = qemu_opt_get(opts, "driver");
     if (driver && !strcmp(driver, "?")) {
         for (info = device_info_list; info != NULL; info = info->next) {
-            qdev_print_devinfo(info, msg, sizeof(msg));
-            qemu_error("%s", msg);
+            qdev_print_devinfo(info);
         }
         return 1;
     }
@@ -179,7 +170,7 @@ int qdev_device_help(QemuOpts *opts)
     }
 
     for (prop = info->props; prop && prop->name; prop++) {
-        qemu_error("%s.%s=%s", info->name, prop->name, prop->info->name);
+        error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
     }
     return 1;
 }
@@ -735,11 +726,9 @@ void do_info_qtree(Monitor *mon)
 void do_info_qdm(Monitor *mon)
 {
     DeviceInfo *info;
-    char msg[256];
 
     for (info = device_info_list; info != NULL; info = info->next) {
-        qdev_print_devinfo(info, msg, sizeof(msg));
-        monitor_printf(mon, "%s\n", msg);
+        qdev_print_devinfo(info);
     }
 }
 
Signature · 1 line
1.6.6
#26 lore raw

Re: [Qemu-devel] [PATCH RFC 15/48] error: Don't abuse qemu_error() for non-error in qdev_device_help()

Quoted 4 lines · On Wed, 24 Feb 2010 18:55:27 +0100 Markus Armbruster <[email protected]> wrote:

qdev_device_help() prints device information with qemu_error(). A later commit will make qemu_error() print additional stuff that is only appropriate for proper errors, and then this will break. Use error_printf() instead.

Aren't you abusing the error function just like the current code? Also,
I think that this information should be printed to stdout.

#27 lore raw

Re: [Qemu-devel] [PATCH RFC 15/48] error: Don't abuse qemu_error() for non-error in qdev_device_help()

Quoted 9 lines · Luiz Capitulino
Quoted 4 lines · On Wed, 24 Feb 2010 18:55:27 +0100 Markus Armbruster <[email protected]> wrote:

qdev_device_help() prints device information with qemu_error(). A later commit will make qemu_error() print additional stuff that is only appropriate for proper errors, and then this will break. Use error_printf() instead.

Aren't you abusing the error function just like the current code?

error_printf() isn't a function to report an error. It's a function to print to the error destination, which is either the current monitor or stderr. It serves as a building block for functions that report an error.

Quoted 2 lines

Also,
I think that this information should be printed to stdout.

Agree in principle. Existing code is sloppy about stdout vs. stderr, however. If it's important to start cleaning that up *now*, we could add functions to print to the "user destination", which is either the current monitor or stdout.

In any case, it needs to be a separate commit, because this commit should do just one thing: fix the qemu_error() abuse. Proper use of stderr and stdout is a separate issue.

#28 lore raw

Re: [Qemu-devel] [PATCH RFC 15/48] error: Don't abuse qemu_error() for non-error in qdev_device_help()

Quoted 10 lines · On Mon, 01 Mar 2010 10:05:18 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 8 lines · Luiz Capitulino
Quoted 4 lines · On Wed, 24 Feb 2010 18:55:27 +0100 Markus Armbruster <[email protected]> wrote:

qdev_device_help() prints device information with qemu_error(). A later commit will make qemu_error() print additional stuff that is only appropriate for proper errors, and then this will break. Use error_printf() instead.

Quoted 6 lines
Quoted 1 line

Aren't you abusing the error function just like the current code?

error_printf() isn't a function to report an error. It's a function to print to the error destination, which is either the current monitor or stderr. It serves as a building block for functions that report an error.

Makes sense.

Quoted 10 lines
Quoted 1 line

I think that this information should be printed to stdout.

Agree in principle. Existing code is sloppy about stdout vs. stderr, however. If it's important to start cleaning that up *now*, we could add functions to print to the "user destination", which is either the current monitor or stdout.

In any case, it needs to be a separate commit, because this commit should do just one thing: fix the qemu_error() abuse. Proper use of stderr and stdout is a separate issue.

Okay.

#29 lore raw

RFC PATCH 16/48 error: Don't abuse qemu_error() for non-error in qbus_find()

In reply to Markus Armbruster

qbus_find() adds an informational line to error messages, and prints both lines with one qemu_error(). Use error_printf() for the informational line instead.

While there, simplify: instead of printing buffers filled by qbus_list_bus() and qbus_list_dev() in one go, make them print it.

---
 hw/qdev.c |   37 +++++++++++++++++--------------------
 1 files changed, 17 insertions(+), 20 deletions(-)
hw/qdev.c +17 -20
index 377e327..38fc4e7 100644
@@ -456,35 +456,33 @@ static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
     return NULL;
 }
 
-static void qbus_list_bus(DeviceState *dev, char *dest, int len)
+static void qbus_list_bus(DeviceState *dev)
 {
     BusState *child;
     const char *sep = " ";
-    int pos = 0;
 
-    pos += snprintf(dest+pos, len-pos,"child busses at \"%s\":",
-                    dev->id ? dev->id : dev->info->name);
+    error_printf("child busses at \"%s\":",
+                 dev->id ? dev->id : dev->info->name);
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
-        pos += snprintf(dest+pos, len-pos, "%s\"%s\"", sep, child->name);
+        error_printf("%s\"%s\"", sep, child->name);
         sep = ", ";
     }
+    error_printf("\n");
 }
 
-static void qbus_list_dev(BusState *bus, char *dest, int len)
+static void qbus_list_dev(BusState *bus)
 {
     DeviceState *dev;
     const char *sep = " ";
-    int pos = 0;
 
-    pos += snprintf(dest+pos, len-pos, "devices at \"%s\":",
-                    bus->name);
+    error_printf("devices at \"%s\":", bus->name);
     QLIST_FOREACH(dev, &bus->children, sibling) {
-        pos += snprintf(dest+pos, len-pos, "%s\"%s\"",
-                        sep, dev->info->name);
+        error_printf("%s\"%s\"", sep, dev->info->name);
         if (dev->id)
-            pos += snprintf(dest+pos, len-pos, "/\"%s\"", dev->id);
+            error_printf("/\"%s\"", dev->id);
         sep = ", ";
     }
+    error_printf("\n");
 }
 
 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
@@ -531,7 +529,7 @@ static BusState *qbus_find(const char *path)
 {
     DeviceState *dev;
     BusState *bus;
-    char elem[128], msg[256];
+    char elem[128];
     int pos, len;
 
     /* find start element */
@@ -565,8 +563,8 @@ static BusState *qbus_find(const char *path)
         pos += len;
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
-            qbus_list_dev(bus, msg, sizeof(msg));
-            qemu_error("device \"%s\" not found\n%s", elem, msg);
+            qemu_error("device \"%s\" not found", elem);
+            qbus_list_dev(bus);
             return NULL;
         }
         if (path[pos] == '\0') {
@@ -579,9 +577,8 @@ static BusState *qbus_find(const char *path)
             case 1:
                 return QLIST_FIRST(&dev->child_bus);
             default:
-                qbus_list_bus(dev, msg, sizeof(msg));
-                qemu_error("device has multiple child busses (%s)\n%s",
-                           path, msg);
+                qemu_error("device has multiple child busses (%s)", path);
+                qbus_list_bus(dev);
                 return NULL;
             }
         }
@@ -594,8 +591,8 @@ static BusState *qbus_find(const char *path)
         pos += len;
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
-            qbus_list_bus(dev, msg, sizeof(msg));
-            qemu_error("child bus \"%s\" not found\n%s", elem, msg);
+            qemu_error("child bus \"%s\" not found", elem);
+            qbus_list_bus(dev);
             return NULL;
         }
     }
Signature · 1 line
1.6.6
#30 lore raw

RFC PATCH 17/48 error: Infrastructure to track locations for error reporting

In reply to Markus Armbruster

New struct Location holds a location. So far, the only location is LOC_NONE, so this doesn't do anything useful yet.

Passing the current location all over the place would be too cumbersome. Hide it away in static cur_loc instead, and provide accessors. Print it in qemu_error().

Store it in QError, and print it in qerror_print().

Store it in QemuOpt, for use by qemu_opts_foreach(). This makes qemu_error() do the right thing when it runs within qemu_opts_foreach().

We may still have to store it in other data structures holding user input for better error messages. Left for another day.

---
 qemu-error.c  |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-error.h  |   16 +++++++++++
 qemu-option.c |    7 +++++
 qemu-tool.c   |   24 ++++++++++++++++
 qerror.c      |    5 +++-
 qerror.h      |    4 ++-
 6 files changed, 137 insertions(+), 2 deletions(-)
qemu-error.c +83 -0
index 075c64d..0778001 100644
@@ -41,10 +41,93 @@ void error_printf(const char *fmt, ...)
     va_end(ap);
 }
 
+static Location std_loc = {
+    .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+    assert(!loc->prev);
+    loc->prev = cur_loc;
+    cur_loc = loc;
+    return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+    loc->kind = LOC_NONE;
+    loc->prev = NULL;
+    return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+    assert(cur_loc == loc && loc->prev);
+    cur_loc = loc->prev;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+    *loc = *cur_loc;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+    Location *prev = cur_loc->prev;
+    assert(!loc->prev);
+    *cur_loc = *loc;
+    cur_loc->prev = prev;
+}
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+    cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Print current location to current monitor if we have one, else to stderr.
+ */
+void error_print_loc(void)
+{
+    switch (cur_loc->kind) {
+    default: ;
+    }
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list ap;
 
+    error_print_loc();
     va_start(ap, fmt);
     error_vprintf(fmt, ap);
     va_end(ap);
qemu-error.h +16 -0
index d90f1da..ebf4bf9 100644
@@ -13,8 +13,24 @@
 #ifndef QEMU_ERROR_H
 #define QEMU_ERROR_H
 
+typedef struct Location {
+    /* all members are private to qemu-error.c */
+    enum { LOC_NONE } kind;
+    int n;
+    const void *ptr;
+    struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_print_loc(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
qemu-option.c +7 -0
index de40bff..ab488e4 100644
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 
 /*
@@ -483,6 +484,7 @@ struct QemuOpt {
 struct QemuOpts {
     char *id;
     QemuOptsList *list;
+    Location loc;
     QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
     QTAILQ_ENTRY(QemuOpts) next;
 };
@@ -653,6 +655,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist
         opts->id = qemu_strdup(id);
     }
     opts->list = list;
+    loc_save(&opts->loc);
     QTAILQ_INIT(&opts->head);
     QTAILQ_INSERT_TAIL(&list->head, opts, next);
     return opts;
@@ -810,13 +813,17 @@ int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure)
 {
+    Location loc;
     QemuOpts *opts;
     int rc = 0;
 
+    loc_push_none(&loc);
     QTAILQ_FOREACH(opts, &list->head, next) {
+        loc_restore(&opts->loc);
         rc |= func(opts, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;
     }
+    loc_pop(&loc);
     return rc;
 }
qemu-tool.c +24 -0
index 26f46eb..4bbd9e6 100644
@@ -104,6 +104,30 @@ int64_t qemu_get_clock(QEMUClock *clock)
     return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
 }
 
+Location *loc_push_restore(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_push_none(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_pop(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_save(Location *loc)
+{
+    return loc;
+}
+
+void loc_restore(Location *loc)
+{
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list args;
qerror.c +4 -1
index c09c3b8..92b05eb 100644
@@ -224,6 +224,7 @@ QError *qerror_from_info(const char *file, int linenr, const char *func,
     QError *qerr;
 
     qerr = qerror_new();
+    loc_save(&qerr->loc);
     qerr->linenr = linenr;
     qerr->file = file;
     qerr->func = func;
@@ -321,10 +322,12 @@ QString *qerror_human(const QError *qerror)
  * it uses qemu_error() for this, so that the output is routed to the right
  * place (ie. stderr or Monitor's device).
  */
-void qerror_print(const QError *qerror)
+void qerror_print(QError *qerror)
 {
     QString *qstring = qerror_human(qerror);
+    loc_push_restore(&qerror->loc);
     qemu_error("%s", qstring_get_str(qstring));
+    loc_pop(&qerror->loc);
     QDECREF(qstring);
 }
 
qerror.h +3 -1
index ee59615..f25be1b 100644
@@ -14,6 +14,7 @@
 
 #include "qdict.h"
 #include "qstring.h"
+#include "qemu-error.h"
 #include <stdarg.h>
 
 typedef struct QErrorStringTable {
@@ -24,6 +25,7 @@ typedef struct QErrorStringTable {
 typedef struct QError {
     QObject_HEAD;
     QDict *error;
+    Location loc;
     int linenr;
     const char *file;
     const char *func;
@@ -34,7 +36,7 @@ QError *qerror_new(void);
 QError *qerror_from_info(const char *file, int linenr, const char *func,
                          const char *fmt, va_list *va);
 QString *qerror_human(const QError *qerror);
-void qerror_print(const QError *qerror);
+void qerror_print(QError *qerror);
 QError *qobject_to_qerror(const QObject *obj);
 
 /*
Signature · 1 line
1.6.6
#31 lore raw

Re: [Qemu-devel] [PATCH RFC 17/48] error: Infrastructure to track locations for error reporting

Quoted 7 lines · On Wed, 24 Feb 2010 18:55:29 +0100 Markus Armbruster <[email protected]> wrote:

New struct Location holds a location. So far, the only location is LOC_NONE, so this doesn't do anything useful yet.

Passing the current location all over the place would be too cumbersome. Hide it away in static cur_loc instead, and provide accessors. Print it in qemu_error().

Quoted 8 lines

Store it in QError, and print it in qerror_print().

Store it in QemuOpt, for use by qemu_opts_foreach(). This makes qemu_error() do the right thing when it runs within qemu_opts_foreach().

We may still have to store it in other data structures holding user input for better error messages. Left for another day.

General implementation looks good to me, minor comments follow.

Quoted 71 lines
---
 qemu-error.c  |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-error.h  |   16 +++++++++++
 qemu-option.c |    7 +++++
 qemu-tool.c   |   24 ++++++++++++++++
 qerror.c      |    5 +++-
 qerror.h      |    4 ++-
 6 files changed, 137 insertions(+), 2 deletions(-)
qemu-error.c +53 -0
index 075c64d..0778001 100644
@@ -41,10 +41,93 @@ void error_printf(const char *fmt, ...)
     va_end(ap);
 }
 
+static Location std_loc = {
+    .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+    assert(!loc->prev);
+    loc->prev = cur_loc;
+    cur_loc = loc;
+    return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+    loc->kind = LOC_NONE;
+    loc->prev = NULL;
+    return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+    assert(cur_loc == loc && loc->prev);
+    cur_loc = loc->prev;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+    *loc = *cur_loc;
+    loc->prev = NULL;
+    return loc;
+}
+

qemu-error.c void error_printf(const char *fmt, ...) · line 106

Quoted 10 lines
qemu-error.c +10 -0
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+    Location *prev = cur_loc->prev;
+    assert(!loc->prev);
+    *cur_loc = *loc;
+    cur_loc->prev = prev;
+}

Are you sure that using a established interface like qemu-queue.h
isn't better? Maybe we could add an API for stacks.. We have stack support in QList, but then Location would have to be a QObject (not sure if it's worth it).

Quoted 32 lines
qemu-error.c +20 -0
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+    cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Print current location to current monitor if we have one, else to stderr.
+ */
+void error_print_loc(void)
+{
+    switch (cur_loc->kind) {
+    default: ;
+    }
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list ap;
 
+    error_print_loc();
     va_start(ap, fmt);
     error_vprintf(fmt, ap);
     va_end(ap);
qemu-error.h +0 -0
index d90f1da..ebf4bf9 100644

qemu-error.h line 19

Quoted 7 lines
qemu-error.h +4 -0
@@ -13,8 +13,24 @@
 #ifndef QEMU_ERROR_H
 #define QEMU_ERROR_H
 
+typedef struct Location {
+    /* all members are private to qemu-error.c */
+    enum { LOC_NONE } kind;
+    int n;

Better to choose a better name for 'n'.

Quoted 113 lines
qemu-error.h +12 -0
+    const void *ptr;
+    struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_print_loc(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
qemu-option.c +7 -0
index de40bff..ab488e4 100644
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 
 /*
@@ -483,6 +484,7 @@ struct QemuOpt {
 struct QemuOpts {
     char *id;
     QemuOptsList *list;
+    Location loc;
     QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
     QTAILQ_ENTRY(QemuOpts) next;
 };
@@ -653,6 +655,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist
         opts->id = qemu_strdup(id);
     }
     opts->list = list;
+    loc_save(&opts->loc);
     QTAILQ_INIT(&opts->head);
     QTAILQ_INSERT_TAIL(&list->head, opts, next);
     return opts;
@@ -810,13 +813,17 @@ int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure)
 {
+    Location loc;
     QemuOpts *opts;
     int rc = 0;
 
+    loc_push_none(&loc);
     QTAILQ_FOREACH(opts, &list->head, next) {
+        loc_restore(&opts->loc);
         rc |= func(opts, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;
     }
+    loc_pop(&loc);
     return rc;
 }
qemu-tool.c +24 -0
index 26f46eb..4bbd9e6 100644
@@ -104,6 +104,30 @@ int64_t qemu_get_clock(QEMUClock *clock)
     return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
 }
 
+Location *loc_push_restore(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_push_none(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_pop(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_save(Location *loc)
+{
+    return loc;
+}
+
+void loc_restore(Location *loc)
+{
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list args;
qerror.c +1 -0
index c09c3b8..92b05eb 100644
@@ -224,6 +224,7 @@ QError *qerror_from_info(const char *file, int linenr, const char *func,
     QError *qerr;
 
     qerr = qerror_new();
+    loc_save(&qerr->loc);
     qerr->linenr = linenr;
     qerr->file = file;
     qerr->func = func;
@@ -321,10 +322,12 @@ QString *qerror_human(const QError *qerror)
  * it uses qemu_error() for this, so that the output is routed to the right
  * place (ie. stderr or Monitor's device).

qerror.c QString *qerror_human(const QError *qerror) · line 332

Quoted 10 lines
qerror.c +3 -1
  */
-void qerror_print(const QError *qerror)
+void qerror_print(QError *qerror)
 {
     QString *qstring = qerror_human(qerror);
+    loc_push_restore(&qerror->loc);
     qemu_error("%s", qstring_get_str(qstring));
+    loc_pop(&qerror->loc);
     QDECREF(qstring);
 }

The new behavior should be documented here and/or in qemu_error().

Quoted 30 lines
qerror.h +3 -1
index ee59615..f25be1b 100644
@@ -14,6 +14,7 @@
 
 #include "qdict.h"
 #include "qstring.h"
+#include "qemu-error.h"
 #include <stdarg.h>
 
 typedef struct QErrorStringTable {
@@ -24,6 +25,7 @@ typedef struct QErrorStringTable {
 typedef struct QError {
     QObject_HEAD;
     QDict *error;
+    Location loc;
     int linenr;
     const char *file;
     const char *func;
@@ -34,7 +36,7 @@ QError *qerror_new(void);
 QError *qerror_from_info(const char *file, int linenr, const char *func,
                          const char *fmt, va_list *va);
 QString *qerror_human(const QError *qerror);
-void qerror_print(const QError *qerror);
+void qerror_print(QError *qerror);
 QError *qobject_to_qerror(const QObject *obj);
 
 /*
#32 lore raw

Re: [Qemu-devel] [PATCH RFC 17/48] error: Infrastructure to track locations for error reporting

Quoted 99 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 24 Feb 2010 18:55:29 +0100 Markus Armbruster <[email protected]> wrote:

New struct Location holds a location. So far, the only location is LOC_NONE, so this doesn't do anything useful yet.

Passing the current location all over the place would be too cumbersome. Hide it away in static cur_loc instead, and provide accessors. Print it in qemu_error().

Quoted 8 lines

Store it in QError, and print it in qerror_print().

Store it in QemuOpt, for use by qemu_opts_foreach(). This makes qemu_error() do the right thing when it runs within qemu_opts_foreach().

We may still have to store it in other data structures holding user input for better error messages. Left for another day.

General implementation looks good to me, minor comments follow.

Quoted 78 lines
---
 qemu-error.c  |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-error.h  |   16 +++++++++++
 qemu-option.c |    7 +++++
 qemu-tool.c   |   24 ++++++++++++++++
 qerror.c      |    5 +++-
 qerror.h      |    4 ++-
 6 files changed, 137 insertions(+), 2 deletions(-)
qemu-error.c +60 -0
index 075c64d..0778001 100644
@@ -41,10 +41,93 @@ void error_printf(const char *fmt, ...)
     va_end(ap);
 }
 
+static Location std_loc = {
+    .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+    assert(!loc->prev);
+    loc->prev = cur_loc;
+    cur_loc = loc;
+    return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+    loc->kind = LOC_NONE;
+    loc->prev = NULL;
+    return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+    assert(cur_loc == loc && loc->prev);
+    cur_loc = loc->prev;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+    *loc = *cur_loc;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+    Location *prev = cur_loc->prev;
+    assert(!loc->prev);
Quoted 8 lines

qemu-error.c void error_printf(const char *fmt, ...) · line 106

Quoted 3 lines
qemu-error.c +3 -0
+    *cur_loc = *loc;
+    cur_loc->prev = prev;
+}

Are you sure that using a established interface like qemu-queue.h
isn't better? Maybe we could add an API for stacks.. We have stack support in QList, but then Location would have to be a QObject (not sure if it's worth it).

Yes, I am pretty sure.

Chasing list pointers by hand for the millionth time sucks, and that's why we have qemu-queue.h. But we're not chasing pointers here. We're threading together a stack of current locations, and it takes us all of five trivial assignments (two in loc_push_restore(), one in loc_push_none(), two in loc_pop()). Eight if you count loc_save() and loc_restore().

I tried to sketch how the same thing would look using sys-queue.h, and gave up because it made my head hurt.

Quoted 159 lines
Quoted 32 lines
qemu-error.c +20 -0
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+    cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Print current location to current monitor if we have one, else to stderr.
+ */
+void error_print_loc(void)
+{
+    switch (cur_loc->kind) {
+    default: ;
+    }
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list ap;
 
+    error_print_loc();
     va_start(ap, fmt);
     error_vprintf(fmt, ap);
     va_end(ap);
qemu-error.h +0 -0
index d90f1da..ebf4bf9 100644

qemu-error.h line 19

Quoted 7 lines
qemu-error.h +4 -0
@@ -13,8 +13,24 @@
 #ifndef QEMU_ERROR_H
 #define QEMU_ERROR_H
 
+typedef struct Location {
+    /* all members are private to qemu-error.c */
+    enum { LOC_NONE } kind;
+    int n;

Better to choose a better name for 'n'.

Quoted 117 lines
qemu-error.h +12 -0
+    const void *ptr;
+    struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_print_loc(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
qemu-option.c +7 -0
index de40bff..ab488e4 100644
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 
 /*
@@ -483,6 +484,7 @@ struct QemuOpt {
 struct QemuOpts {
     char *id;
     QemuOptsList *list;
+    Location loc;
     QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
     QTAILQ_ENTRY(QemuOpts) next;
 };
@@ -653,6 +655,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist
         opts->id = qemu_strdup(id);
     }
     opts->list = list;
+    loc_save(&opts->loc);
     QTAILQ_INIT(&opts->head);
     QTAILQ_INSERT_TAIL(&list->head, opts, next);
     return opts;
@@ -810,13 +813,17 @@ int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure)
 {
+    Location loc;
     QemuOpts *opts;
     int rc = 0;
 
+    loc_push_none(&loc);
     QTAILQ_FOREACH(opts, &list->head, next) {
+        loc_restore(&opts->loc);
         rc |= func(opts, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;
     }
+    loc_pop(&loc);
     return rc;
 }
qemu-tool.c +24 -0
index 26f46eb..4bbd9e6 100644
@@ -104,6 +104,30 @@ int64_t qemu_get_clock(QEMUClock *clock)
     return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
 }
 
+Location *loc_push_restore(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_push_none(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_pop(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_save(Location *loc)
+{
+    return loc;
+}
+
+void loc_restore(Location *loc)
+{
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list args;
qerror.c +2 -1
index c09c3b8..92b05eb 100644
@@ -224,6 +224,7 @@ QError *qerror_from_info(const char *file, int linenr, const char *func,
     QError *qerr;
 
     qerr = qerror_new();
+    loc_save(&qerr->loc);
     qerr->linenr = linenr;
     qerr->file = file;
     qerr->func = func;
@@ -321,10 +322,12 @@ QString *qerror_human(const QError *qerror)
  * it uses qemu_error() for this, so that the output is routed to the right
  * place (ie. stderr or Monitor's device).
  */
-void qerror_print(const QError *qerror)
+void qerror_print(QError *qerror)
 {
Quoted 8 lines

qerror.c QString *qerror_human(const QError *qerror) · line 332

Quoted 6 lines
qerror.c +2 -0
     QString *qstring = qerror_human(qerror);
+    loc_push_restore(&qerror->loc);
     qemu_error("%s", qstring_get_str(qstring));
+    loc_pop(&qerror->loc);
     QDECREF(qstring);
 }

The new behavior should be documented here and/or in qemu_error().

What exactly would you like me to document? That qerror_print() doesn't ignore the location stored in its argument? Probably not. That qemu_error_new() uses the current location just like qemu_error()?

Quoted 29 lines
Quoted 29 lines
qerror.h +3 -1
index ee59615..f25be1b 100644
@@ -14,6 +14,7 @@
 
 #include "qdict.h"
 #include "qstring.h"
+#include "qemu-error.h"
 #include <stdarg.h>
 
 typedef struct QErrorStringTable {
@@ -24,6 +25,7 @@ typedef struct QErrorStringTable {
 typedef struct QError {
     QObject_HEAD;
     QDict *error;
+    Location loc;
     int linenr;
     const char *file;
     const char *func;
@@ -34,7 +36,7 @@ QError *qerror_new(void);
 QError *qerror_from_info(const char *file, int linenr, const char *func,
                          const char *fmt, va_list *va);
 QString *qerror_human(const QError *qerror);
-void qerror_print(const QError *qerror);
+void qerror_print(QError *qerror);
 QError *qobject_to_qerror(const QObject *obj);
 
 /*
#33 lore raw

Re: [Qemu-devel] [PATCH RFC 17/48] error: Infrastructure to track locations for error reporting

Quoted 290 lines · On Mon, 01 Mar 2010 10:19:22 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 99 lines · Luiz Capitulino
Quoted 7 lines · On Wed, 24 Feb 2010 18:55:29 +0100 Markus Armbruster <[email protected]> wrote:

New struct Location holds a location. So far, the only location is LOC_NONE, so this doesn't do anything useful yet.

Passing the current location all over the place would be too cumbersome. Hide it away in static cur_loc instead, and provide accessors. Print it in qemu_error().

Quoted 8 lines

Store it in QError, and print it in qerror_print().

Store it in QemuOpt, for use by qemu_opts_foreach(). This makes qemu_error() do the right thing when it runs within qemu_opts_foreach().

We may still have to store it in other data structures holding user input for better error messages. Left for another day.

General implementation looks good to me, minor comments follow.

Quoted 78 lines
---
 qemu-error.c  |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-error.h  |   16 +++++++++++
 qemu-option.c |    7 +++++
 qemu-tool.c   |   24 ++++++++++++++++
 qerror.c      |    5 +++-
 qerror.h      |    4 ++-
 6 files changed, 137 insertions(+), 2 deletions(-)
qemu-error.c +60 -0
index 075c64d..0778001 100644
@@ -41,10 +41,93 @@ void error_printf(const char *fmt, ...)
     va_end(ap);
 }
 
+static Location std_loc = {
+    .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+    assert(!loc->prev);
+    loc->prev = cur_loc;
+    cur_loc = loc;
+    return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+    loc->kind = LOC_NONE;
+    loc->prev = NULL;
+    return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+    assert(cur_loc == loc && loc->prev);
+    cur_loc = loc->prev;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+    *loc = *cur_loc;
+    loc->prev = NULL;
+    return loc;
+}
+
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+    Location *prev = cur_loc->prev;
+    assert(!loc->prev);
Quoted 8 lines

qemu-error.c void error_printf(const char *fmt, ...) · line 106

Quoted 3 lines
qemu-error.c +3 -0
+    *cur_loc = *loc;
+    cur_loc->prev = prev;
+}

Are you sure that using a established interface like qemu-queue.h
isn't better? Maybe we could add an API for stacks.. We have stack support in QList, but then Location would have to be a QObject (not sure if it's worth it).

Yes, I am pretty sure.

Chasing list pointers by hand for the millionth time sucks, and that's why we have qemu-queue.h. But we're not chasing pointers here. We're threading together a stack of current locations, and it takes us all of five trivial assignments (two in loc_push_restore(), one in loc_push_none(), two in loc_pop()). Eight if you count loc_save() and loc_restore().

I tried to sketch how the same thing would look using sys-queue.h, and gave up because it made my head hurt.

Quoted 159 lines
Quoted 32 lines
qemu-error.c +20 -0
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+    cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Print current location to current monitor if we have one, else to stderr.
+ */
+void error_print_loc(void)
+{
+    switch (cur_loc->kind) {
+    default: ;
+    }
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list ap;
 
+    error_print_loc();
     va_start(ap, fmt);
     error_vprintf(fmt, ap);
     va_end(ap);
qemu-error.h +0 -0
index d90f1da..ebf4bf9 100644

qemu-error.h line 19

Quoted 7 lines
qemu-error.h +4 -0
@@ -13,8 +13,24 @@
 #ifndef QEMU_ERROR_H
 #define QEMU_ERROR_H
 
+typedef struct Location {
+    /* all members are private to qemu-error.c */
+    enum { LOC_NONE } kind;
+    int n;

Better to choose a better name for 'n'.

Quoted 117 lines
qemu-error.h +12 -0
+    const void *ptr;
+    struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_print_loc(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
qemu-option.c +7 -0
index de40bff..ab488e4 100644
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "qemu-common.h"
+#include "qemu-error.h"
 #include "qemu-option.h"
 
 /*
@@ -483,6 +484,7 @@ struct QemuOpt {
 struct QemuOpts {
     char *id;
     QemuOptsList *list;
+    Location loc;
     QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
     QTAILQ_ENTRY(QemuOpts) next;
 };
@@ -653,6 +655,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist
         opts->id = qemu_strdup(id);
     }
     opts->list = list;
+    loc_save(&opts->loc);
     QTAILQ_INIT(&opts->head);
     QTAILQ_INSERT_TAIL(&list->head, opts, next);
     return opts;
@@ -810,13 +813,17 @@ int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure)
 {
+    Location loc;
     QemuOpts *opts;
     int rc = 0;
 
+    loc_push_none(&loc);
     QTAILQ_FOREACH(opts, &list->head, next) {
+        loc_restore(&opts->loc);
         rc |= func(opts, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;
     }
+    loc_pop(&loc);
     return rc;
 }
qemu-tool.c +24 -0
index 26f46eb..4bbd9e6 100644
@@ -104,6 +104,30 @@ int64_t qemu_get_clock(QEMUClock *clock)
     return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
 }
 
+Location *loc_push_restore(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_push_none(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_pop(Location *loc)
+{
+    return loc;
+}
+
+Location *loc_save(Location *loc)
+{
+    return loc;
+}
+
+void loc_restore(Location *loc)
+{
+}
+
 void qemu_error(const char *fmt, ...)
 {
     va_list args;
qerror.c +2 -1
index c09c3b8..92b05eb 100644
@@ -224,6 +224,7 @@ QError *qerror_from_info(const char *file, int linenr, const char *func,
     QError *qerr;
 
     qerr = qerror_new();
+    loc_save(&qerr->loc);
     qerr->linenr = linenr;
     qerr->file = file;
     qerr->func = func;
@@ -321,10 +322,12 @@ QString *qerror_human(const QError *qerror)
  * it uses qemu_error() for this, so that the output is routed to the right
  * place (ie. stderr or Monitor's device).
  */
-void qerror_print(const QError *qerror)
+void qerror_print(QError *qerror)
 {
Quoted 8 lines

qerror.c QString *qerror_human(const QError *qerror) · line 332

Quoted 6 lines
qerror.c +2 -0
     QString *qstring = qerror_human(qerror);
+    loc_push_restore(&qerror->loc);
     qemu_error("%s", qstring_get_str(qstring));
+    loc_pop(&qerror->loc);
     QDECREF(qstring);
 }

The new behavior should be documented here and/or in qemu_error().

Quoted 1 line

What exactly would you like me to document?

The format of the output, specially the automatic stuff (which is
probably only worth for qemu_error()).

#34 lore raw

RFC PATCH 18/48 error: Include the program name in error messages to stderr

In reply to Markus Armbruster
---
 qemu-error.c |   20 +++++++++++++++++++-
 qemu-error.h |    1 +
 vl.c         |    2 ++
 3 files changed, 22 insertions(+), 1 deletions(-)
qemu-error.c +19 -1
index 0778001..b2c0a80 100644
@@ -113,13 +113,31 @@ void loc_set_none(void)
     cur_loc->kind = LOC_NONE;
 }
 
+static const char *progname;
+
+/*
+ * Set the program name for error_print_loc().
+ */
+void error_set_progname(const char *argv0)
+{
+    const char *p = strrchr(argv0, '/');
+    progname = p ? p + 1 : argv0;
+}
+
 /*
  * Print current location to current monitor if we have one, else to stderr.
  */
 void error_print_loc(void)
 {
+    const char *sep = "";
+
+    if (!cur_mon) {
+        fprintf(stderr, "%s:", progname);
+        sep = " ";
+    }
     switch (cur_loc->kind) {
-    default: ;
+    default:
+        error_printf(sep);
     }
 }
 
qemu-error.h +1 -0
index ebf4bf9..22f3c79 100644
@@ -31,6 +31,7 @@ void loc_set_none(void);
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void error_print_loc(void);
+void error_set_progname(const char *argv0);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
 void qemu_error_internal(const char *file, int linenr, const char *func,
                          const char *fmt, ...)
vl.c +2 -0
index 7d4b234..ac1db72 100644
@@ -4822,6 +4822,8 @@ int main(int argc, char **argv, char **envp)
     int show_vnc_port = 0;
     int defconfig = 1;
 
+    error_set_progname(argv[0]);
+
     init_clocks();
 
     qemu_cache_utils_init(envp);
Signature · 1 line
1.6.6
#35 lore raw

RFC PATCH 19/48 error: Track locations in configuration files

In reply to Markus Armbruster

New LOC_FILE. Use it for tracking file name and line number in qemu_config_parse(). We now report errors like

qemu:foo.conf:42: Did not find I2C bus for smbus-eeprom

In particular, gems like this message:

-device: no driver specified

become almost nice now:

qemu:foo.conf:44: -device: no driver specified

(A later commit will get rid of the bogus -device:)

---
 qemu-config.c |   28 +++++++++++++++++-----------
 qemu-config.h |    2 +-
 qemu-error.c  |   20 ++++++++++++++++++++
 qemu-error.h  |    3 ++-
 vl.c          |   14 +++++++++-----
 5 files changed, 49 insertions(+), 18 deletions(-)
qemu-config.c +17 -11
index 194a653..78e5c67 100644
@@ -425,13 +425,17 @@ void qemu_config_write(FILE *fp)
     }
 }
 
-int qemu_config_parse(FILE *fp)
+int qemu_config_parse(FILE *fp, const char *fname)
 {
     char line[1024], group[64], id[64], arg[64], value[1024];
+    Location loc;
     QemuOptsList *list = NULL;
     QemuOpts *opts = NULL;
+    int res = -1, lno = 0;
 
+    loc_push_none(&loc);
     while (fgets(line, sizeof(line), fp) != NULL) {
+        loc_set_file(fname, ++lno);
         if (line[0] == '\n') {
             /* skip empty lines */
             continue;
@@ -444,7 +448,7 @@ int qemu_config_parse(FILE *fp)
             /* group with id */
             list = find_list(group);
             if (list == NULL)
-                return -1;
+                goto out;
             opts = qemu_opts_create(list, id, 1);
             continue;
         }
@@ -452,25 +456,27 @@ int qemu_config_parse(FILE *fp)
             /* group without id */
             list = find_list(group);
             if (list == NULL)
-                return -1;
+                goto out;
             opts = qemu_opts_create(list, NULL, 0);
             continue;
         }
         if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
             /* arg = value */
             if (opts == NULL) {
-                fprintf(stderr, "no group defined\n");
-                return -1;
+                qemu_error("no group defined");
+                goto out;
             }
             if (qemu_opt_set(opts, arg, value) != 0) {
-                fprintf(stderr, "failed to set \"%s\" for %s\n",
-                        arg, group);
-                return -1;
+                qemu_error("failed to set \"%s\" for %s", arg, group);
+                goto out;
             }
             continue;
         }
-        fprintf(stderr, "parse error: %s\n", line);
-        return -1;
+        qemu_error("parse error");
+        goto out;
     }
-    return 0;
+    res = 0;
+out:
+    loc_pop(&loc);
+    return res;
 }
qemu-config.h +1 -1
index b335c42..c507687 100644
@@ -16,6 +16,6 @@ int qemu_global_option(const char *str);
 void qemu_add_globals(void);
 
 void qemu_config_write(FILE *fp);
-int qemu_config_parse(FILE *fp);
+int qemu_config_parse(FILE *fp, const char *fname);
 
 #endif /* QEMU_CONFIG_H */
qemu-error.c +20 -0
index b2c0a80..5debb71 100644
@@ -113,6 +113,19 @@ void loc_set_none(void)
     cur_loc->kind = LOC_NONE;
 }
 
+/*
+ * Change the current location to file FNAME, line LNO.
+ */
+void loc_set_file(const char *fname, int lno)
+{
+    assert (fname || cur_loc->kind == LOC_FILE);
+    cur_loc->kind = LOC_FILE;
+    cur_loc->n = lno;
+    if (fname) {
+        cur_loc->ptr = fname;
+    }
+}
+
 static const char *progname;
 
 /*
@@ -136,6 +149,13 @@ void error_print_loc(void)
         sep = " ";
     }
     switch (cur_loc->kind) {
+    case LOC_FILE:
+        error_printf("%s:", (const char *)cur_loc->ptr);
+        if (cur_loc->n) {
+            error_printf("%d:", cur_loc->n);
+        }
+        error_printf(" ");
+        break;
     default:
         error_printf(sep);
     }
qemu-error.h +2 -1
index 22f3c79..32b54e1 100644
@@ -15,7 +15,7 @@
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
-    enum { LOC_NONE } kind;
+    enum { LOC_NONE, LOC_FILE } kind;
     int n;
     const void *ptr;
     struct Location *prev;
@@ -27,6 +27,7 @@ Location *loc_pop(Location *loc);
 Location *loc_save(Location *loc);
 void loc_restore(Location *loc);
 void loc_set_none(void);
+void loc_set_file(const char *fname, int lno);
 
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
vl.c +9 -5
index ac1db72..83e3b82 100644
@@ -4901,18 +4901,22 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (defconfig) {
+        const char *fname;
         FILE *fp;
-        fp = fopen(CONFIG_QEMU_CONFDIR "/qemu.conf", "r");
+
+        fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
+        fp = fopen(fname, "r");
         if (fp) {
-            if (qemu_config_parse(fp) != 0) {
+            if (qemu_config_parse(fp, fname) != 0) {
                 exit(1);
             }
             fclose(fp);
         }
 
-        fp = fopen(CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", "r");
+        fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
+        fp = fopen(fname, "r");
         if (fp) {
-            if (qemu_config_parse(fp) != 0) {
+            if (qemu_config_parse(fp, fname) != 0) {
                 exit(1);
             }
             fclose(fp);
@@ -5585,7 +5589,7 @@ int main(int argc, char **argv, char **envp)
                         fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
                         exit(1);
                     }
-                    if (qemu_config_parse(fp) != 0) {
+                    if (qemu_config_parse(fp, optarg) != 0) {
                         exit(1);
                     }
                     fclose(fp);
Signature · 1 line
1.6.6
#36 lore raw

RFC PATCH 20/48 QemuOpts: Fix qemu_config_parse() to catch file read errors

In reply to Markus Armbruster
---
 qemu-config.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
qemu-config.c +4 -0
index 78e5c67..acd4db7 100644
@@ -475,6 +475,10 @@ int qemu_config_parse(FILE *fp, const char *fname)
         qemu_error("parse error");
         goto out;
     }
+    if (ferror(fp)) {
+        qemu_error("error reading file");
+        goto out;
+    }
     res = 0;
 out:
     loc_pop(&loc);
Signature · 1 line
1.6.6
#37 lore raw

RFC PATCH 21/48 error: Track locations on command line

In reply to Markus Armbruster

New LOC_CMDLINE. Use it for tracking option with argument in lookup_opt(). We now report errors like this

qemu: -device smbus-eeprom: Did not find I2C bus for smbus-eeprom

---
 qemu-error.c |   20 ++++++++++++++++++++
 qemu-error.h |    3 ++-
 vl.c         |    9 +++++----
 3 files changed, 27 insertions(+), 5 deletions(-)
qemu-error.c +20 -0
index 5debb71..a726d32 100644
@@ -114,6 +114,16 @@ void loc_set_none(void)
 }
 
 /*
+ * Change the current location to argument ARGV[IDX..IDX+CNT-1].
+ */
+void loc_set_cmdline(char **argv, int idx, int cnt)
+{
+    cur_loc->kind = LOC_CMDLINE;
+    cur_loc->n = cnt;
+    cur_loc->ptr = argv + idx;
+}
+
+/*
  * Change the current location to file FNAME, line LNO.
  */
 void loc_set_file(const char *fname, int lno)
@@ -143,12 +153,22 @@ void error_set_progname(const char *argv0)
 void error_print_loc(void)
 {
     const char *sep = "";
+    int i;
+    const char *const *argp;
 
     if (!cur_mon) {
         fprintf(stderr, "%s:", progname);
         sep = " ";
     }
     switch (cur_loc->kind) {
+    case LOC_CMDLINE:
+        argp = cur_loc->ptr;
+        for (i = 0; i < cur_loc->n; i++) {
+            error_printf("%s%s", sep, argp[i]);
+            sep = " ";
+        }
+        error_printf(": ");
+        break;
     case LOC_FILE:
         error_printf("%s:", (const char *)cur_loc->ptr);
         if (cur_loc->n) {
qemu-error.h +2 -1
index 32b54e1..75bc4bf 100644
@@ -15,7 +15,7 @@
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
-    enum { LOC_NONE, LOC_FILE } kind;
+    enum { LOC_NONE, LOC_CMDLINE, LOC_FILE } kind;
     int n;
     const void *ptr;
     struct Location *prev;
@@ -27,6 +27,7 @@ Location *loc_pop(Location *loc);
 Location *loc_save(Location *loc);
 void loc_restore(Location *loc);
 void loc_set_none(void);
+void loc_set_cmdline(char **argv, int idx, int cnt);
 void loc_set_file(const char *fname, int lno);
 
 void error_vprintf(const char *fmt, va_list ap);
vl.c +5 -4
index 83e3b82..fa2898d 100644
@@ -4756,6 +4756,7 @@ static const QEMUOption *lookup_opt(int argc, char **argv,
     char *r = argv[optind];
     const char *optarg;
 
+    loc_set_cmdline(argv, optind, 1);
     optind++;
     /* Treat --foo the same as -foo.  */
     if (r[1] == '-')
@@ -4763,8 +4764,7 @@ static const QEMUOption *lookup_opt(int argc, char **argv,
     popt = qemu_options;
     for(;;) {
         if (!popt->name) {
-            fprintf(stderr, "%s: invalid option -- '%s'\n",
-                    argv[0], r);
+            qemu_error("invalid option");
             exit(1);
         }
         if (!strcmp(popt->name, r + 1))
@@ -4773,11 +4773,11 @@ static const QEMUOption *lookup_opt(int argc, char **argv,
     }
     if (popt->flags & HAS_ARG) {
         if (optind >= argc) {
-            fprintf(stderr, "%s: option '%s' requires an argument\n",
-                    argv[0], r);
+            qemu_error("requires an argument");
             exit(1);
         }
         optarg = argv[optind++];
+        loc_set_cmdline(argv, optind - 2, 2);
     } else {
         optarg = NULL;
     }
@@ -5614,6 +5614,7 @@ int main(int argc, char **argv, char **envp)
             }
         }
     }
+    loc_set_none();
 
     /* If no data_dir is specified then try to find it relative to the
        executable path.  */
Signature · 1 line
1.6.6
#38 lore raw

RFC PATCH 22/48 qdev: Fix -device and device_add to handle unsuitable bus gracefully

In reply to Markus Armbruster

"device_add isa-serial,bus=pci.0" kills QEMU. Not good.

---
 hw/qdev.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
hw/qdev.c +5 -0
index 38fc4e7..7def97d 100644
@@ -204,6 +204,11 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     path = qemu_opt_get(opts, "bus");
     if (path != NULL) {
         bus = qbus_find(path);
+        if (bus && bus->info != info->bus_info) {
+            qemu_error("Device '%s' can't go on a %s bus",
+                       driver, bus->info->name);
+            return NULL;
+        }
     } else {
         bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
     }
Signature · 1 line
1.6.6
#39 lore raw

RFC PATCH 23/48 qdev: Factor qdev_create_from_info() out of qdev_create()

In reply to Markus Armbruster

To make it obvious that -device and device_add can't die in hw_error().

---
 hw/qdev.c |   37 ++++++++++++++++++++++---------------
 1 files changed, 22 insertions(+), 15 deletions(-)
hw/qdev.c +22 -15
index 7def97d..e5f6820 100644
@@ -78,13 +78,32 @@ static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
     return NULL;
 }
 
+static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
+{
+    DeviceState *dev;
+
+    assert(bus->info == info->bus_info);
+    dev = qemu_mallocz(info->size);
+    dev->info = info;
+    dev->parent_bus = bus;
+    qdev_prop_set_defaults(dev, dev->info->props);
+    qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
+    qdev_prop_set_globals(dev);
+    QLIST_INSERT_HEAD(&bus->children, dev, sibling);
+    if (qdev_hotplug) {
+        assert(bus->allow_hotplug);
+        dev->hotplugged = 1;
+    }
+    dev->state = DEV_STATE_CREATED;
+    return dev;
+}
+
 /* Create a new device.  This only initializes the device state structure
    and allows properties to be set.  qdev_init should be called to
    initialize the actual device emulation.  */
 DeviceState *qdev_create(BusState *bus, const char *name)
 {
     DeviceInfo *info;
-    DeviceState *dev;
 
     if (!bus) {
         if (!main_system_bus) {
@@ -98,19 +117,7 @@ DeviceState *qdev_create(BusState *bus, const char *name)
         hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
     }
 
-    dev = qemu_mallocz(info->size);
-    dev->info = info;
-    dev->parent_bus = bus;
-    qdev_prop_set_defaults(dev, dev->info->props);
-    qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
-    qdev_prop_set_globals(dev);
-    QLIST_INSERT_HEAD(&bus->children, dev, sibling);
-    if (qdev_hotplug) {
-        assert(bus->allow_hotplug);
-        dev->hotplugged = 1;
-    }
-    dev->state = DEV_STATE_CREATED;
-    return dev;
+    return qdev_create_from_info(bus, info);
 }
 
 static void qdev_print_devinfo(DeviceInfo *info)
@@ -224,7 +231,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     }
 
     /* create device, set properties */
-    qdev = qdev_create(bus, driver);
+    qdev = qdev_create_from_info(bus, info);
     id = qemu_opts_id(opts);
     if (id) {
         qdev->id = id;
Signature · 1 line
1.6.6
#40 lore raw

RFC PATCH 24/48 qdev: Hide "no_user" devices from users

In reply to Markus Armbruster

Users can't create them, so qdev_device_help() shouldn't list them. Fix that.

Also make qdev_device_add() pretend they don't exist. Before, it rejected them with a "can't be added via command line" message, which wasn't quite right for monitor command device_add.

---
 hw/qdev.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
hw/qdev.c +4 -6
index e5f6820..274b052 100644
@@ -162,6 +162,9 @@ int qdev_device_help(QemuOpts *opts)
     driver = qemu_opt_get(opts, "driver");
     if (driver && !strcmp(driver, "?")) {
         for (info = device_info_list; info != NULL; info = info->next) {
+            if (info->no_user) {
+                continue;       /* not available, don't show */
+            }
             qdev_print_devinfo(info);
         }
         return 1;
@@ -197,15 +200,10 @@ DeviceState *qdev_device_add(QemuOpts *opts)
 
     /* find driver */
     info = qdev_find_info(NULL, driver);
-    if (!info) {
+    if (!info || info->no_user) {
         qemu_error_new(QERR_DEVICE_NOT_FOUND, driver);
         return NULL;
     }
-    if (info->no_user) {
-        qemu_error("device \"%s\" can't be added via command line",
-                   info->name);
-        return NULL;
-    }
 
     /* find bus */
     path = qemu_opt_get(opts, "bus");
Signature · 1 line
1.6.6
#41 lore raw

RFC PATCH 25/48 qdev: Hide "ptr" properties from users

In reply to Markus Armbruster

Users can't set them, so qdev_device_help() shouldn't list them. Fix that. Also make qdev_prop_parse() hide them instead of printing a meaningless "has no parser" error message.

Their value means nothing to users, so qdev_print_props() shouldn't print it. Fix by removing their print method.

Their only use is dirty hacks. Document that.

---
 hw/qdev-properties.c |   21 ++++++++-------------
 hw/qdev.c            |   15 +++++++++++++++
 2 files changed, 23 insertions(+), 13 deletions(-)
hw/qdev-properties.c +8 -13
index 277ff9e..8e96805 100644
@@ -399,17 +399,11 @@ PropertyInfo qdev_prop_vlan = {
 
 /* --- pointer --- */
 
-static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    void **ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "<%p>", *ptr);
-}
-
+/* Not a proper property, just for dirty hacks.  FIXME Remove it!  */
 PropertyInfo qdev_prop_ptr = {
     .name  = "ptr",
     .type  = PROP_TYPE_PTR,
     .size  = sizeof(void*),
-    .print = print_ptr,
 };
 
 /* --- mac address --- */
@@ -543,16 +537,17 @@ int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
     Property *prop;
 
     prop = qdev_prop_find(dev, name);
-    if (!prop) {
+    /*
+     * TODO Properties without a parse method are just for dirty
+     * hacks.  qdev_prop_ptr is the only such PropertyInfo.  It's
+     * marked for removal.  The test !prop->info->parse should be
+     * removed along with it.
+     */
+    if (!prop || !prop->info->parse) {
         fprintf(stderr, "property \"%s.%s\" not found\n",
                 dev->info->name, name);
         return -1;
     }
-    if (!prop->info->parse) {
-        fprintf(stderr, "property \"%s.%s\" has no parser\n",
-                dev->info->name, name);
-        return -1;
-    }
     if (prop->info->parse(dev, prop, value) != 0) {
         fprintf(stderr, "property \"%s.%s\": failed to parse \"%s\"\n",
                 dev->info->name, name, value);
hw/qdev.c +15 -0
index 274b052..4d5ede7 100644
@@ -180,6 +180,15 @@ int qdev_device_help(QemuOpts *opts)
     }
 
     for (prop = info->props; prop && prop->name; prop++) {
+        /*
+         * TODO Properties without a parser are just for dirty hacks.
+         * qdev_prop_ptr is the only such PropertyInfo.  It's marked
+         * for removal.  This conditional should be removed along with
+         * it.
+         */
+        if (!prop->info->parse) {
+            continue;           /* no way to set it, don't show */
+        }
         error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
     }
     return 1;
@@ -682,6 +691,12 @@ static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
     if (!props)
         return;
     while (props->name) {
+        /*
+         * TODO Properties without a print method are just for dirty
+         * hacks.  qdev_prop_ptr is the only such PropertyInfo.  It's
+         * marked for removal.  The test props->info->print should be
+         * removed along with it.
+         */
         if (props->info->print) {
             props->info->print(dev, props, buf, sizeof(buf));
             qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
Signature · 1 line
1.6.6
#42 lore raw

RFC PATCH 26/48 error: Polish human-readable error descriptions

In reply to Markus Armbruster

Also put error definitions in alphabetical order

---
 qerror.c |   28 ++++++++++++++--------------
 qerror.h |   14 +++++++-------
 2 files changed, 21 insertions(+), 21 deletions(-)
qerror.c +14 -14
index 92b05eb..45df41e 100644
@@ -46,43 +46,39 @@ static const QErrorStringTable qerror_table[] = {
     },
     {
         .error_fmt = QERR_DEVICE_ENCRYPTED,
-        .desc      = "The %(device) is encrypted",
+        .desc      = "Device '%(device)' is encrypted",
     },
     {
         .error_fmt = QERR_DEVICE_LOCKED,
-        .desc      = "Device %(device) is locked",
+        .desc      = "Device '%(device)' is locked",
     },
     {
         .error_fmt = QERR_DEVICE_NOT_ACTIVE,
-        .desc      = "The %(device) device has not been activated by the guest",
+        .desc      = "Device '%(device)' has not been activated by the guest",
     },
     {
         .error_fmt = QERR_DEVICE_NOT_FOUND,
-        .desc      = "The %(device) device has not been found",
+        .desc      = "Device '%(device)' not found",
     },
     {
         .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
-        .desc      = "Device %(device) is not removable",
+        .desc      = "Device '%(device)' is not removable",
     },
     {
         .error_fmt = QERR_FD_NOT_FOUND,
-        .desc      = "Failed to find file descriptor named %(name)",
+        .desc      = "File descriptor named '%(name)' not found",
     },
     {
         .error_fmt = QERR_FD_NOT_SUPPLIED,
         .desc      = "No file descriptor supplied via SCM_RIGHTS",
     },
     {
-        .error_fmt = QERR_OPEN_FILE_FAILED,
-        .desc      = "Could not open '%(filename)'",
-    },
-    {
         .error_fmt = QERR_INVALID_BLOCK_FORMAT,
-        .desc      = "Invalid block format %(name)",
+        .desc      = "Invalid block format '%(name)'",
     },
     {
         .error_fmt = QERR_INVALID_PARAMETER,
-        .desc      = "Invalid parameter %(name)",
+        .desc      = "Invalid parameter '%(name)'",
     },
     {
         .error_fmt = QERR_INVALID_PARAMETER_TYPE,
@@ -90,7 +86,7 @@ static const QErrorStringTable qerror_table[] = {
     },
     {
         .error_fmt = QERR_INVALID_PASSWORD,
-        .desc      = "The entered password is invalid",
+        .desc      = "Password incorrect",
     },
     {
         .error_fmt = QERR_JSON_PARSING,
@@ -102,7 +98,11 @@ static const QErrorStringTable qerror_table[] = {
     },
     {
         .error_fmt = QERR_MISSING_PARAMETER,
-        .desc      = "Parameter %(name) is missing",
+        .desc      = "Parameter '%(name)' is missing",
+    },
+    {
+        .error_fmt = QERR_OPEN_FILE_FAILED,
+        .desc      = "Could not open '%(filename)'",
     },
     {
         .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
qerror.h +7 -7
index f25be1b..8060fe0 100644
@@ -48,7 +48,7 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_DEVICE_ENCRYPTED \
     "{ 'class': 'DeviceEncrypted', 'data': { 'device': %s } }"
 
-#define QERR_DEVICE_LOCKED                                      \
+#define QERR_DEVICE_LOCKED \
     "{ 'class': 'DeviceLocked', 'data': { 'device': %s } }"
 
 #define QERR_DEVICE_NOT_ACTIVE \
@@ -66,9 +66,6 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_FD_NOT_SUPPLIED \
     "{ 'class': 'FdNotSupplied', 'data': {} }"
 
-#define QERR_OPEN_FILE_FAILED \
-    "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
-
 #define QERR_INVALID_BLOCK_FORMAT \
     "{ 'class': 'InvalidBlockFormat', 'data': { 'name': %s } }"
 
@@ -90,18 +87,21 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_MISSING_PARAMETER \
     "{ 'class': 'MissingParameter', 'data': { 'name': %s } }"
 
+#define QERR_OPEN_FILE_FAILED \
+    "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
+
 #define QERR_QMP_BAD_INPUT_OBJECT \
     "{ 'class': 'QMPBadInputObject', 'data': { 'expected': %s } }"
 
 #define QERR_SET_PASSWD_FAILED \
     "{ 'class': 'SetPasswdFailed', 'data': {} }"
 
-#define QERR_UNDEFINED_ERROR \
-    "{ 'class': 'UndefinedError', 'data': {} }"
-
 #define QERR_TOO_MANY_FILES \
     "{ 'class': 'TooManyFiles', 'data': {} }"
 
+#define QERR_UNDEFINED_ERROR \
+    "{ 'class': 'UndefinedError', 'data': {} }"
+
 #define QERR_VNC_SERVER_FAILED \
     "{ 'class': 'VNCServerFailed', 'data': { 'target': %s } }"
 
Signature · 1 line
1.6.6
#43 lore raw

RFC PATCH 27/48 error: New QERR_PROPERTY_NOT_FOUND

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 45df41e..e555c33 100644
@@ -105,6 +105,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Could not open '%(filename)'",
     },
     {
+        .error_fmt = QERR_PROPERTY_NOT_FOUND,
+        .desc      = "Property '%(device).%(property)' not found",
+    },
+    {
         .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
         .desc      = "Bad QMP input object",
     },
qerror.h +3 -0
index 8060fe0..e36336e 100644
@@ -90,6 +90,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_OPEN_FILE_FAILED \
     "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
 
+#define QERR_PROPERTY_NOT_FOUND \
+    "{ 'class': 'PropertyNotFound', 'data': { 'device': %s, 'property': %s } }"
+
 #define QERR_QMP_BAD_INPUT_OBJECT \
     "{ 'class': 'QMPBadInputObject', 'data': { 'expected': %s } }"
 
Signature · 1 line
1.6.6
#44 lore raw

RFC PATCH 28/48 error: New QERR_PROPERTY_VALUE_BAD

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index e555c33..ea3971e 100644
@@ -109,6 +109,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Property '%(device).%(property)' not found",
     },
     {
+        .error_fmt = QERR_PROPERTY_VALUE_BAD,
+        .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
+    },
+    {
         .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
         .desc      = "Bad QMP input object",
     },
qerror.h +3 -0
index e36336e..d5f0e9d 100644
@@ -93,6 +93,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_PROPERTY_NOT_FOUND \
     "{ 'class': 'PropertyNotFound', 'data': { 'device': %s, 'property': %s } }"
 
+#define QERR_PROPERTY_VALUE_BAD \
+    "{ 'class': 'PropertyValueBad', 'data': { 'device': %s, 'property': %s, 'value': %s } }"
+
 #define QERR_QMP_BAD_INPUT_OBJECT \
     "{ 'class': 'QMPBadInputObject', 'data': { 'expected': %s } }"
 
Signature · 1 line
1.6.6
#45 lore raw

RFC PATCH 29/48 qdev: convert setting device properties to QError

In reply to Markus Armbruster
---
 hw/qdev-properties.c |    6 ++----
 hw/qdev.c            |    2 --
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/qdev-properties.c +2 -4
index 8e96805..a3572b0 100644
@@ -544,13 +544,11 @@ int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
      * removed along with it.
      */
     if (!prop || !prop->info->parse) {
-        fprintf(stderr, "property \"%s.%s\" not found\n",
-                dev->info->name, name);
+        qemu_error_new(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
         return -1;
     }
     if (prop->info->parse(dev, prop, value) != 0) {
-        fprintf(stderr, "property \"%s.%s\": failed to parse \"%s\"\n",
-                dev->info->name, name, value);
+        qemu_error_new(QERR_PROPERTY_VALUE_BAD, dev->info->name, name, value);
         return -1;
     }
     return 0;
hw/qdev.c +0 -2
index 4d5ede7..b170081 100644
@@ -146,8 +146,6 @@ static int set_property(const char *name, const char *value, void *opaque)
         return 0;
 
     if (qdev_prop_parse(dev, name, value) == -1) {
-        qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"",
-                   name, value, dev->info->name);
         return -1;
     }
     return 0;
Signature · 1 line
1.6.6
#46 lore raw

RFC PATCH 30/48 qdev: Relax parsing of bus option

In reply to Markus Armbruster

Treat multiple successive slashes as a one slash. Ignore trailing slashes. This is how POSIX pathnames work.

---
 hw/qdev.c |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
hw/qdev.c +17 -9
index b170081..6495894 100644
@@ -555,8 +555,8 @@ static BusState *qbus_find(const char *path)
         pos = 0;
     } else {
         if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\")", path);
-            return NULL;
+            assert(!path[0]);
+            elem[0] = len = 0;
         }
         bus = qbus_find_recursive(main_system_bus, elem, NULL);
         if (!bus) {
@@ -567,15 +567,18 @@ static BusState *qbus_find(const char *path)
     }
 
     for (;;) {
+        assert(path[pos] == '/' || !path[pos]);
+        while (path[pos] == '/') {
+            pos++;
+        }
         if (path[pos] == '\0') {
-            /* we are done */
             return bus;
         }
 
         /* find device */
-        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)", path, pos);
-            return NULL;
+        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+            assert(0);
+            elem[0] = len = 0;
         }
         pos += len;
         dev = qbus_find_dev(bus, elem);
@@ -584,6 +587,11 @@ static BusState *qbus_find(const char *path)
             qbus_list_dev(bus);
             return NULL;
         }
+
+        assert(path[pos] == '/' || !path[pos]);
+        while (path[pos] == '/') {
+            pos++;
+        }
         if (path[pos] == '\0') {
             /* last specified element is a device.  If it has exactly
              * one child bus accept it nevertheless */
@@ -601,9 +609,9 @@ static BusState *qbus_find(const char *path)
         }
 
         /* find bus */
-        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)", path, pos);
-            return NULL;
+        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+            assert(0);
+            elem[0] = len = 0;
         }
         pos += len;
         bus = qbus_find_bus(dev, elem);
Signature · 1 line
1.6.6
#47 lore raw

RFC PATCH 31/48 error: New QERR_BUS_NOT_FOUND

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index ea3971e..51b68bc 100644
@@ -41,6 +41,10 @@ static const QType qerror_type = {
  */
 static const QErrorStringTable qerror_table[] = {
     {
+        .error_fmt = QERR_BUS_NOT_FOUND,
+        .desc      = "Bus '%(bus)' not found",
+    },
+    {
         .error_fmt = QERR_COMMAND_NOT_FOUND,
         .desc      = "The command %(name) has not been found",
     },
qerror.h +3 -0
index d5f0e9d..2f06dc7 100644
@@ -42,6 +42,9 @@ QError *qobject_to_qerror(const QObject *obj);
 /*
  * QError class list
  */
+#define QERR_BUS_NOT_FOUND \
+    "{ 'class': 'BusNotFound', 'data': { 'bus': %s } }"
+
 #define QERR_COMMAND_NOT_FOUND \
     "{ 'class': 'CommandNotFound', 'data': { 'name': %s } }"
 
Signature · 1 line
1.6.6
#48 lore raw

RFC PATCH 32/48 error: New QERR_DEVICE_MULTIPLE_BUSSES

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 51b68bc..56407be 100644
@@ -57,6 +57,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Device '%(device)' is locked",
     },
     {
+        .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
+        .desc      = "Device '%(device)' has multiple child busses",
+    },
+    {
         .error_fmt = QERR_DEVICE_NOT_ACTIVE,
         .desc      = "Device '%(device)' has not been activated by the guest",
     },
qerror.h +3 -0
index 2f06dc7..2b31a8d 100644
@@ -54,6 +54,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_DEVICE_LOCKED \
     "{ 'class': 'DeviceLocked', 'data': { 'device': %s } }"
 
+#define QERR_DEVICE_MULTIPLE_BUSSES \
+    "{ 'class': 'DeviceMultipleBusses', 'data': { 'device': %s } }"
+
 #define QERR_DEVICE_NOT_ACTIVE \
     "{ 'class': 'DeviceNotActive', 'data': { 'device': %s } }"
 
Signature · 1 line
1.6.6
#49 lore raw

RFC PATCH 33/48 error: New QERR_DEVICE_NO_BUS

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 56407be..7ebc2bb 100644
@@ -73,6 +73,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Device '%(device)' is not removable",
     },
     {
+        .error_fmt = QERR_DEVICE_NO_BUS,
+        .desc      = "Device '%(device)' has no child bus",
+    },
+    {
         .error_fmt = QERR_FD_NOT_FOUND,
         .desc      = "File descriptor named '%(name)' not found",
     },
qerror.h +3 -0
index 2b31a8d..e65f1f2 100644
@@ -66,6 +66,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_DEVICE_NOT_REMOVABLE \
     "{ 'class': 'DeviceNotRemovable', 'data': { 'device': %s } }"
 
+#define QERR_DEVICE_NO_BUS \
+    "{ 'class': 'DeviceNoBus', 'data': { 'device': %s } }"
+
 #define QERR_FD_NOT_FOUND \
     "{ 'class': 'FdNotFound', 'data': { 'name': %s } }"
 
Signature · 1 line
1.6.6
#50 lore raw

RFC PATCH 34/48 qdev: Convert qbus_find() to QError

In reply to Markus Armbruster
---
 hw/qdev.c |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)
hw/qdev.c +14 -11
index 6495894..1887dde 100644
@@ -216,18 +216,21 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     path = qemu_opt_get(opts, "bus");
     if (path != NULL) {
         bus = qbus_find(path);
-        if (bus && bus->info != info->bus_info) {
+        if (!bus) {
+            return NULL;
+        }
+        if (bus->info != info->bus_info) {
             qemu_error("Device '%s' can't go on a %s bus",
                        driver, bus->info->name);
             return NULL;
         }
     } else {
         bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
-    }
-    if (!bus) {
-        qemu_error("Did not find %s bus for %s",
-                   path ? path : info->bus_info->name, info->name);
-        return NULL;
+        if (!bus) {
+            qemu_error("Did not find %s bus for %s",
+                       info->bus_info->name, info->name);
+            return NULL;
+        }
     }
     if (qdev_hotplug && !bus->allow_hotplug) {
         qemu_error("Bus %s does not support hotplugging",
@@ -560,7 +563,7 @@ static BusState *qbus_find(const char *path)
         }
         bus = qbus_find_recursive(main_system_bus, elem, NULL);
         if (!bus) {
-            qemu_error("bus \"%s\" not found", elem);
+            qemu_error_new(QERR_BUS_NOT_FOUND, elem);
             return NULL;
         }
         pos = len;
@@ -583,7 +586,7 @@ static BusState *qbus_find(const char *path)
         pos += len;
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
-            qemu_error("device \"%s\" not found", elem);
+            qemu_error_new(QERR_DEVICE_NOT_FOUND, elem);
             qbus_list_dev(bus);
             return NULL;
         }
@@ -597,12 +600,12 @@ static BusState *qbus_find(const char *path)
              * one child bus accept it nevertheless */
             switch (dev->num_child_bus) {
             case 0:
-                qemu_error("device has no child bus (%s)", path);
+                qemu_error_new(QERR_DEVICE_NO_BUS, elem);
                 return NULL;
             case 1:
                 return QLIST_FIRST(&dev->child_bus);
             default:
-                qemu_error("device has multiple child busses (%s)", path);
+                qemu_error_new(QERR_DEVICE_MULTIPLE_BUSSES, elem);
                 qbus_list_bus(dev);
                 return NULL;
             }
@@ -616,7 +619,7 @@ static BusState *qbus_find(const char *path)
         pos += len;
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
-            qemu_error("child bus \"%s\" not found", elem);
+            qemu_error_new(QERR_BUS_NOT_FOUND, elem);
             qbus_list_bus(dev);
             return NULL;
         }
Signature · 1 line
1.6.6
#51 lore raw

RFC PATCH 35/48 monitor: New in_qmp_mon()

In reply to Markus Armbruster
---
 monitor.c |    5 +++++
 monitor.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
monitor.c +5 -0
index a4263af..5c87a98 100644
@@ -194,6 +194,11 @@ static inline int monitor_ctrl_mode(const Monitor *mon)
     return (mon->flags & MONITOR_USE_CONTROL);
 }
 
+int in_qmp_mon(void)
+{
+    return cur_mon && monitor_ctrl_mode(cur_mon);
+}
+
 static void monitor_read_command(Monitor *mon, int show_prompt)
 {
     if (!mon->rs)
monitor.h +2 -0
index 1222ff0..9fc9639 100644
@@ -30,6 +30,8 @@ typedef enum MonitorEvent {
     QEVENT_MAX,
 } MonitorEvent;
 
+int in_qmp_mon(void);
+
 void monitor_protocol_event(MonitorEvent event, QObject *data);
 void monitor_init(CharDriverState *chr, int flags);
 
Signature · 1 line
1.6.6
#52 lore raw

Re: [Qemu-devel] [PATCH RFC 35/48] monitor: New in_qmp_mon()

Quoted 12 lines · On Wed, 24 Feb 2010 18:55:47 +0100 Markus Armbruster <[email protected]> wrote:
---
 monitor.c |    5 +++++
 monitor.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
monitor.c +0 -0
index a4263af..5c87a98 100644

monitor.c static inline int monitor_ctrl_mode(const Monitor *mon) · line 201

Quoted 8 lines
monitor.c +5 -0
@@ -194,6 +194,11 @@ static inline int monitor_ctrl_mode(const Monitor *mon)
     return (mon->flags & MONITOR_USE_CONTROL);
 }
 
+int in_qmp_mon(void)
+{
+    return cur_mon && monitor_ctrl_mode(cur_mon);
+}
+

Afaik, all public monitor functions begin with 'monitor_'. While it's
debatable if it's a good name, let's keep the consistency.

Also, I'm going to rename monitor_ctrl_mode() to something like
monitor_qmp_mode() or monitor_is_qmp(). In this case the difference with in_qmp_mon() is not clear.

#53 lore raw

Re: [Qemu-devel] [PATCH RFC 35/48] monitor: New in_qmp_mon()

Quoted 27 lines · Luiz Capitulino
Quoted 12 lines · On Wed, 24 Feb 2010 18:55:47 +0100 Markus Armbruster <[email protected]> wrote:
---
 monitor.c |    5 +++++
 monitor.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
monitor.c +0 -0
index a4263af..5c87a98 100644

monitor.c static inline int monitor_ctrl_mode(const Monitor *mon) · line 201

Quoted 8 lines
monitor.c +5 -0
@@ -194,6 +194,11 @@ static inline int monitor_ctrl_mode(const Monitor *mon)
     return (mon->flags & MONITOR_USE_CONTROL);
 }
 
+int in_qmp_mon(void)
+{
+    return cur_mon && monitor_ctrl_mode(cur_mon);
+}
+

Afaik, all public monitor functions begin with 'monitor_'. While it's
debatable if it's a good name, let's keep the consistency.

Quoted 3 lines

Also, I'm going to rename monitor_ctrl_mode() to something like
monitor_qmp_mode() or monitor_is_qmp(). In this case the difference with in_qmp_mon() is not clear.

Care to suggest a name?

#54 lore raw

Re: [Qemu-devel] [PATCH RFC 35/48] monitor: New in_qmp_mon()

Quoted 29 lines · On Mon, 01 Mar 2010 10:19:49 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 27 lines · Luiz Capitulino
Quoted 12 lines · On Wed, 24 Feb 2010 18:55:47 +0100 Markus Armbruster <[email protected]> wrote:
---
 monitor.c |    5 +++++
 monitor.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
monitor.c +0 -0
index a4263af..5c87a98 100644

monitor.c static inline int monitor_ctrl_mode(const Monitor *mon) · line 201

Quoted 8 lines
monitor.c +5 -0
@@ -194,6 +194,11 @@ static inline int monitor_ctrl_mode(const Monitor *mon)
     return (mon->flags & MONITOR_USE_CONTROL);
 }
 
+int in_qmp_mon(void)
+{
+    return cur_mon && monitor_ctrl_mode(cur_mon);
+}
+

Afaik, all public monitor functions begin with 'monitor_'. While it's
debatable if it's a good name, let's keep the consistency.

Quoted 5 lines
Quoted 3 lines

Also, I'm going to rename monitor_ctrl_mode() to something like
monitor_qmp_mode() or monitor_is_qmp(). In this case the difference with in_qmp_mon() is not clear.

Care to suggest a name?

Maybe, monitor_ctrl_mode() should be monitor_is_qmp() and the new one
monitor_cur_is_qmp()?

Or should we start exporting monitor names as 'mon_' to make them short?

#55 lore raw

Re: [Qemu-devel] [PATCH RFC 35/48] monitor: New in_qmp_mon()

Quoted 32 lines · Luiz Capitulino
Quoted 29 lines · On Mon, 01 Mar 2010 10:19:49 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 27 lines · Luiz Capitulino
Quoted 12 lines · On Wed, 24 Feb 2010 18:55:47 +0100 Markus Armbruster <[email protected]> wrote:
---
 monitor.c |    5 +++++
 monitor.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
monitor.c +0 -0
index a4263af..5c87a98 100644

monitor.c static inline int monitor_ctrl_mode(const Monitor *mon) · line 201

Quoted 8 lines
monitor.c +5 -0
@@ -194,6 +194,11 @@ static inline int monitor_ctrl_mode(const Monitor *mon)
     return (mon->flags & MONITOR_USE_CONTROL);
 }
 
+int in_qmp_mon(void)
+{
+    return cur_mon && monitor_ctrl_mode(cur_mon);
+}
+

Afaik, all public monitor functions begin with 'monitor_'. While it's
debatable if it's a good name, let's keep the consistency.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Also, I'm going to rename monitor_ctrl_mode() to something like
monitor_qmp_mode() or monitor_is_qmp(). In this case the difference with in_qmp_mon() is not clear.

Care to suggest a name?

Maybe, monitor_ctrl_mode() should be monitor_is_qmp() and the new one
monitor_cur_is_qmp()?

Works for me.

Quoted 1 line

Or should we start exporting monitor names as 'mon_' to make them short?

Fine with me, but not in my patch series, it's plenty long already :)

#56 lore raw

RFC PATCH 36/48 error: New error_printf_unless_qmp()

In reply to Markus Armbruster
---
 qemu-error.c |   11 +++++++++++
 qemu-error.h |    2 ++
 2 files changed, 13 insertions(+), 0 deletions(-)
qemu-error.c +11 -0
index a726d32..95e87c8 100644
@@ -41,6 +41,17 @@ void error_printf(const char *fmt, ...)
     va_end(ap);
 }
 
+void error_printf_unless_qmp(const char *fmt, ...)
+{
+    va_list ap;
+
+    if (!in_qmp_mon()) {
+        va_start(ap, fmt);
+        error_vprintf(fmt, ap);
+        va_end(ap);
+    }
+}
+
 static Location std_loc = {
     .kind = LOC_NONE
 };
qemu-error.h +2 -0
index 75bc4bf..3ea3ba5 100644
@@ -32,6 +32,8 @@ void loc_set_file(const char *fname, int lno);
 
 void error_vprintf(const char *fmt, va_list ap);
 void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_printf_unless_qmp(const char *fmt, ...)
+    __attribute__ ((format(printf, 1, 2)));
 void error_print_loc(void);
 void error_set_progname(const char *argv0);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
Signature · 1 line
1.6.6
#57 lore raw

RFC PATCH 37/48 error: New QERR_BAD_BUS_FOR_DEVICE

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 7ebc2bb..4761ae5 100644
@@ -41,6 +41,10 @@ static const QType qerror_type = {
  */
 static const QErrorStringTable qerror_table[] = {
     {
+        .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
+        .desc      = "Device '%(device)' can't go on a %(bad_bus_type) bus",
+    },
+    {
         .error_fmt = QERR_BUS_NOT_FOUND,
         .desc      = "Bus '%(bus)' not found",
     },
qerror.h +3 -0
index e65f1f2..8433d84 100644
@@ -42,6 +42,9 @@ QError *qobject_to_qerror(const QObject *obj);
 /*
  * QError class list
  */
+#define QERR_BAD_BUS_FOR_DEVICE \
+    "{ 'class': 'BadBusForDevice', 'data': { 'device': %s, 'bad_bus_type': %s } }"
+
 #define QERR_BUS_NOT_FOUND \
     "{ 'class': 'BusNotFound', 'data': { 'bus': %s } }"
 
Signature · 1 line
1.6.6
#58 lore raw

RFC PATCH 38/48 error: New QERR_BUS_NO_HOTPLUG

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 4761ae5..38db6b2 100644
@@ -49,6 +49,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Bus '%(bus)' not found",
     },
     {
+        .error_fmt = QERR_BUS_NO_HOTPLUG,
+        .desc      = "Bus '%(bus)' does not support hotplugging",
+    },
+    {
         .error_fmt = QERR_COMMAND_NOT_FOUND,
         .desc      = "The command %(name) has not been found",
     },
qerror.h +3 -0
index 8433d84..9db909a 100644
@@ -48,6 +48,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_BUS_NOT_FOUND \
     "{ 'class': 'BusNotFound', 'data': { 'bus': %s } }"
 
+#define QERR_BUS_NO_HOTPLUG \
+    "{ 'class': 'BusNoHotplug', 'data': { 'bus': %s } }"
+
 #define QERR_COMMAND_NOT_FOUND \
     "{ 'class': 'CommandNotFound', 'data': { 'name': %s } }"
 
Signature · 1 line
1.6.6
#59 lore raw

RFC PATCH 39/48 error: New QERR_DEVICE_INIT_FAILED

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 38db6b2..8a38e5d 100644
@@ -61,6 +61,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Device '%(device)' is encrypted",
     },
     {
+        .error_fmt = QERR_DEVICE_INIT_FAILED,
+        .desc      = "Device '%(device)' could not be initialized",
+    },
+    {
         .error_fmt = QERR_DEVICE_LOCKED,
         .desc      = "Device '%(device)' is locked",
     },
qerror.h +3 -0
index 9db909a..173b9f8 100644
@@ -57,6 +57,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_DEVICE_ENCRYPTED \
     "{ 'class': 'DeviceEncrypted', 'data': { 'device': %s } }"
 
+#define QERR_DEVICE_INIT_FAILED \
+    "{ 'class': 'DeviceInitFailed', 'data': { 'device': %s } }"
+
 #define QERR_DEVICE_LOCKED \
     "{ 'class': 'DeviceLocked', 'data': { 'device': %s } }"
 
Signature · 1 line
1.6.6
#60 lore raw

RFC PATCH 40/48 error: New QERR_NO_BUS_FOR_DEVICE

In reply to Markus Armbruster
---
 qerror.c |    4 ++++
 qerror.h |    3 +++
 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
index 8a38e5d..2f3b89c 100644
@@ -125,6 +125,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "Parameter '%(name)' is missing",
     },
     {
+        .error_fmt = QERR_NO_BUS_FOR_DEVICE,
+        .desc      = "No '%(bus)' bus found for device '%(device)'",
+    },
+    {
         .error_fmt = QERR_OPEN_FILE_FAILED,
         .desc      = "Could not open '%(filename)'",
     },
qerror.h +3 -0
index 173b9f8..b46d4df 100644
@@ -105,6 +105,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_MISSING_PARAMETER \
     "{ 'class': 'MissingParameter', 'data': { 'name': %s } }"
 
+#define QERR_NO_BUS_FOR_DEVICE \
+    "{ 'class': 'NoBusForDevice', 'data': { 'device': %s, 'bus': %s } }"
+
 #define QERR_OPEN_FILE_FAILED \
     "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
 
Signature · 1 line
1.6.6
#61 lore raw

RFC PATCH 41/48 Revert "qdev: Use QError for 'device not found' error"

In reply to Markus Armbruster

This reverts commit 3ced9f7a36189aed94d8bf86f3f5087a53012455.

The next commit will convert all of qdev_device_add() to QError, and it'll be clearer with this partial conversion reverted.

---
 hw/qdev.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/qdev.c +2 -2
index 1887dde..4e348fd 100644
@@ -29,7 +29,6 @@
 #include "qdev.h"
 #include "sysemu.h"
 #include "monitor.h"
-#include "qerror.h"
 
 static int qdev_hotplug = 0;
 
@@ -208,7 +207,8 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     /* find driver */
     info = qdev_find_info(NULL, driver);
     if (!info || info->no_user) {
-        qemu_error_new(QERR_DEVICE_NOT_FOUND, driver);
+        qemu_error("Device \"%s\" not found.  Try -device '?' for a list.",
+                   driver);
         return NULL;
     }
 
Signature · 1 line
1.6.6
#62 lore raw

RFC PATCH 42/48 error: Convert do_device_add() to QError

In reply to Markus Armbruster

Conversion to QObject is still missing.

---
 hw/qdev.c |   19 +++++++++----------
 1 files changed, 9 insertions(+), 10 deletions(-)
hw/qdev.c +9 -10
index 4e348fd..a028e9c 100644
@@ -200,15 +200,15 @@ DeviceState *qdev_device_add(QemuOpts *opts)
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
-        qemu_error("-device: no driver specified");
+        qemu_error_new(QERR_MISSING_PARAMETER, "driver");
         return NULL;
     }
 
     /* find driver */
     info = qdev_find_info(NULL, driver);
     if (!info || info->no_user) {
-        qemu_error("Device \"%s\" not found.  Try -device '?' for a list.",
-                   driver);
+        qemu_error_new(QERR_INVALID_PARAMETER, "driver");
+        error_printf_unless_qmp("Try with argument '?' for a list.\n");
         return NULL;
     }
 
@@ -220,21 +220,20 @@ DeviceState *qdev_device_add(QemuOpts *opts)
             return NULL;
         }
         if (bus->info != info->bus_info) {
-            qemu_error("Device '%s' can't go on a %s bus",
-                       driver, bus->info->name);
+            qemu_error_new(QERR_BAD_BUS_FOR_DEVICE,
+                           driver, bus->info->name);
             return NULL;
         }
     } else {
         bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
         if (!bus) {
-            qemu_error("Did not find %s bus for %s",
-                       info->bus_info->name, info->name);
+            qemu_error_new(QERR_NO_BUS_FOR_DEVICE,
+                           info->name, info->bus_info->name);
             return NULL;
         }
     }
     if (qdev_hotplug && !bus->allow_hotplug) {
-        qemu_error("Bus %s does not support hotplugging",
-                   bus->name);
+        qemu_error_new(QERR_BUS_NO_HOTPLUG, bus->name);
         return NULL;
     }
 
@@ -249,7 +248,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         return NULL;
     }
     if (qdev_init(qdev) < 0) {
-        qemu_error("Error initializing device %s", driver);
+        qemu_error_new(QERR_DEVICE_INIT_FAILED, driver);
         return NULL;
     }
     qdev->opts = opts;
Signature · 1 line
1.6.6
#63 lore raw

RFC PATCH 43/48 qemu-option: Functions to convert to/from QDict.

In reply to Markus Armbruster

FIXME Only string options are implemented.

---
 qemu-option.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |    3 +++
 2 files changed, 57 insertions(+), 0 deletions(-)
qemu-option.c +54 -0
index ab488e4..f86aac0 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu-error.h"
 #include "qemu-option.h"
+#include "qobject.h"
+#include "qstring.h"
 
 /*
  * Extracts the name of an option from the parameter string (p points at the
@@ -777,6 +779,58 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
     return opts;
 }
 
+static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
+{
+    const char *value;
+
+    if (!strcmp(key, "id")) {
+        return;
+    }
+
+    switch (qobject_type(obj)) {
+    case QTYPE_QSTRING:
+        value = qstring_get_str(qobject_to_qstring(obj));
+        break;
+    default:
+        abort(); // FIXME implement
+    }
+    qemu_opt_set(opaque, key, value);
+}
+
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
+{
+    const char *id;
+    QemuOpts *opts;
+
+    id = qdict_haskey(qdict, "id") ? qdict_get_str(qdict, "id") : NULL;
+    opts = qemu_opts_create(list, id, 1);
+    if (opts == NULL)
+        return NULL;
+
+    qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
+    return opts;
+}
+
+static int qemu_opts_to_qdict_1(const char *name, const char *value,
+                                void *opaque)
+{
+    qdict_put(opaque, name, qstring_from_str(value));
+    // FIXME obey QemuOptType
+    return 0;
+}
+
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
+{
+    if (!qdict) {
+        qdict = qdict_new();
+    }
+    if (opts->id) {
+        qdict_put(qdict, "id", qstring_from_str(opts->id));
+    }
+    qemu_opt_foreach(opts, qemu_opts_to_qdict_1, qdict, 0);
+    return qdict;
+}
+
 /* Validate parsed opts against descriptions where no
  * descriptions were provided in the QemuOptsList.
  */
qemu-option.h +3 -0
index f3f1de7..d735386 100644
@@ -28,6 +28,7 @@
 
 #include <stdint.h>
 #include "qemu-queue.h"
+#include "qdict.h"
 
 enum QEMUOptionParType {
     OPT_FLAG,
@@ -118,6 +119,8 @@ void qemu_opts_del(QemuOpts *opts);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict);
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 
 typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
 int qemu_opts_print(QemuOpts *opts, void *dummy);
Signature · 1 line
1.6.6
#64 lore raw

Re: [Qemu-devel] [PATCH RFC 43/48] qemu-option: Functions to convert to/from QDict.

Quoted 31 lines · On Wed, 24 Feb 2010 18:55:55 +0100 Markus Armbruster <[email protected]> wrote:

FIXME Only string options are implemented.

---
 qemu-option.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |    3 +++
 2 files changed, 57 insertions(+), 0 deletions(-)
qemu-option.c +8 -0
index ab488e4..f86aac0 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu-error.h"
 #include "qemu-option.h"
+#include "qobject.h"
+#include "qstring.h"
 
 /*
  * Extracts the name of an option from the parameter string (p points at the
@@ -777,6 +779,58 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
     return opts;
 }
 
+static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
+{
+    const char *value;
+
+    if (!strcmp(key, "id")) {
+        return;

qemu-option.c QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi · line 797

Quoted 10 lines
qemu-option.c +10 -0
+    }
+
+    switch (qobject_type(obj)) {
+    case QTYPE_QSTRING:
+        value = qstring_get_str(qobject_to_qstring(obj));
+        break;
+    default:
+        abort(); // FIXME implement
+    }
+    qemu_opt_set(opaque, key, value);

qstring_get_str() returns a pointer to the stored string, are you sure
you want to pass it down without cloning it?

We allow this violation in QString because most handlers that get a string
wants only temporary read-only access. In this case the violation is very convenient, as otherwise we would have to add tons of free()s around.

qemu-option.c QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi · line 805

Quoted 8 lines
qemu-option.c +8 -0
+}
+
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
+{
+    const char *id;
+    QemuOpts *opts;
+
+    id = qdict_haskey(qdict, "id") ? qdict_get_str(qdict, "id") : NULL;

You should use qdict_get_try_str() here.

Quoted 52 lines
qemu-option.c +28 -0
+    opts = qemu_opts_create(list, id, 1);
+    if (opts == NULL)
+        return NULL;
+
+    qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
+    return opts;
+}
+
+static int qemu_opts_to_qdict_1(const char *name, const char *value,
+                                void *opaque)
+{
+    qdict_put(opaque, name, qstring_from_str(value));
+    // FIXME obey QemuOptType
+    return 0;
+}
+
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
+{
+    if (!qdict) {
+        qdict = qdict_new();
+    }
+    if (opts->id) {
+        qdict_put(qdict, "id", qstring_from_str(opts->id));
+    }
+    qemu_opt_foreach(opts, qemu_opts_to_qdict_1, qdict, 0);
+    return qdict;
+}
+
 /* Validate parsed opts against descriptions where no
  * descriptions were provided in the QemuOptsList.
  */
qemu-option.h +3 -0
index f3f1de7..d735386 100644
@@ -28,6 +28,7 @@
 
 #include <stdint.h>
 #include "qemu-queue.h"
+#include "qdict.h"
 
 enum QEMUOptionParType {
     OPT_FLAG,
@@ -118,6 +119,8 @@ void qemu_opts_del(QemuOpts *opts);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict);
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 
 typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
 int qemu_opts_print(QemuOpts *opts, void *dummy);
#65 lore raw

Re: [Qemu-devel] [PATCH RFC 43/48] qemu-option: Functions to convert to/from QDict.

Quoted 39 lines · Luiz Capitulino
Quoted 36 lines · On Wed, 24 Feb 2010 18:55:55 +0100 Markus Armbruster <[email protected]> wrote:

FIXME Only string options are implemented.

---
 qemu-option.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |    3 +++
 2 files changed, 57 insertions(+), 0 deletions(-)
qemu-option.c +13 -0
index ab488e4..f86aac0 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu-error.h"
 #include "qemu-option.h"
+#include "qobject.h"
+#include "qstring.h"
 
 /*
  * Extracts the name of an option from the parameter string (p points at the
@@ -777,6 +779,58 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
     return opts;
 }
 
+static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
+{
+    const char *value;
+
+    if (!strcmp(key, "id")) {
+        return;
+    }
+
+    switch (qobject_type(obj)) {
+    case QTYPE_QSTRING:
+        value = qstring_get_str(qobject_to_qstring(obj));
Quoted 8 lines

qemu-option.c QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi · line 797

Quoted 5 lines
qemu-option.c +5 -0
+        break;
+    default:
+        abort(); // FIXME implement
+    }
+    qemu_opt_set(opaque, key, value);

qstring_get_str() returns a pointer to the stored string, are you sure
you want to pass it down without cloning it?

qemu_opt_set() strdups both its name and its value argument.

Quoted 4 lines

We allow this violation in QString because most handlers that get a string
wants only temporary read-only access. In this case the violation is very convenient, as otherwise we would have to add tons of free()s around.

Quoted 10 lines

qemu-option.c QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi · line 805

Quoted 8 lines
qemu-option.c +8 -0
+}
+
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
+{
+    const char *id;
+    QemuOpts *opts;
+
+    id = qdict_haskey(qdict, "id") ? qdict_get_str(qdict, "id") : NULL;

You should use qdict_get_try_str() here.

Yes, thanks.

Quoted 52 lines
Quoted 52 lines
qemu-option.c +28 -0
+    opts = qemu_opts_create(list, id, 1);
+    if (opts == NULL)
+        return NULL;
+
+    qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
+    return opts;
+}
+
+static int qemu_opts_to_qdict_1(const char *name, const char *value,
+                                void *opaque)
+{
+    qdict_put(opaque, name, qstring_from_str(value));
+    // FIXME obey QemuOptType
+    return 0;
+}
+
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
+{
+    if (!qdict) {
+        qdict = qdict_new();
+    }
+    if (opts->id) {
+        qdict_put(qdict, "id", qstring_from_str(opts->id));
+    }
+    qemu_opt_foreach(opts, qemu_opts_to_qdict_1, qdict, 0);
+    return qdict;
+}
+
 /* Validate parsed opts against descriptions where no
  * descriptions were provided in the QemuOptsList.
  */
qemu-option.h +3 -0
index f3f1de7..d735386 100644
@@ -28,6 +28,7 @@
 
 #include <stdint.h>
 #include "qemu-queue.h"
+#include "qdict.h"
 
 enum QEMUOptionParType {
     OPT_FLAG,
@@ -118,6 +119,8 @@ void qemu_opts_del(QemuOpts *opts);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
+QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict);
+QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 
 typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
 int qemu_opts_print(QemuOpts *opts, void *dummy);
#66 lore raw

RFC PATCH 44/48 qemu-option: Move the implied first name into QemuOptsList

In reply to Markus Armbruster

We sometimes permit omitting the first option name, for example -device foo is short for -device driver=foo. The name to use ("driver" in the example) is passed as argument to qemu_opts_parse(). For each QemuOptsList, we use at most one such name.

Move the name into QemuOptsList, and pass whether to permit the abbreviation. This ensures continued consistency, and simplifies the commit after next in this series.

---
 hw/pci-hotplug.c |    2 +-
 hw/qdev.c        |    2 +-
 hw/usb-net.c     |    2 +-
 net.c            |    4 ++--
 qemu-config.c    |    5 +++++
 qemu-option.c    |    7 ++++++-
 qemu-option.h    |    3 ++-
 vl.c             |   12 ++++++------
 8 files changed, 24 insertions(+), 13 deletions(-)
hw/pci-hotplug.c +1 -1
index aa7e870..e863002 100644
@@ -54,7 +54,7 @@ static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
         return NULL;
     }
 
-    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
+    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", 0);
     if (!opts) {
         monitor_printf(mon, "parsing network options '%s' failed\n",
                        opts_str ? opts_str : "");
hw/qdev.c +1 -1
index a028e9c..80a027a 100644
@@ -767,7 +767,7 @@ void do_device_add(Monitor *mon, const QDict *qdict)
     QemuOpts *opts;
 
     opts = qemu_opts_parse(&qemu_device_opts,
-                           qdict_get_str(qdict, "config"), "driver");
+                           qdict_get_str(qdict, "config"), 1);
     if (opts) {
         if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
             qemu_opts_del(opts);
hw/usb-net.c +1 -1
index cfd2f62..56fdd9b 100644
@@ -1478,7 +1478,7 @@ static USBDevice *usb_net_init(const char *cmdline)
     QemuOpts *opts;
     int idx;
 
-    opts = qemu_opts_parse(&qemu_net_opts, cmdline, NULL);
+    opts = qemu_opts_parse(&qemu_net_opts, cmdline, 0);
     if (!opts) {
         return NULL;
     }
net.c +2 -2
index 2479e65..c97787f 100644
@@ -1156,7 +1156,7 @@ void net_host_device_add(Monitor *mon, const QDict *qdict)
         return;
     }
 
-    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
+    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", 0);
     if (!opts) {
         monitor_printf(mon, "parsing network options '%s' failed\n",
                        opts_str ? opts_str : "");
@@ -1361,7 +1361,7 @@ int net_client_parse(QemuOptsList *opts_list, const char *optarg)
     }
 #endif
 
-    if (!qemu_opts_parse(opts_list, optarg, "type")) {
+    if (!qemu_opts_parse(opts_list, optarg, 1)) {
         return -1;
     }
 
qemu-config.c +5 -0
index acd4db7..72b6279 100644
@@ -86,6 +86,7 @@ QemuOptsList qemu_drive_opts = {
 
 QemuOptsList qemu_chardev_opts = {
     .name = "chardev",
+    .implied_opt_name = "backend",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
     .desc = {
         {
@@ -152,6 +153,7 @@ QemuOptsList qemu_chardev_opts = {
 
 QemuOptsList qemu_device_opts = {
     .name = "device",
+    .implied_opt_name = "driver",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
     .desc = {
         /*
@@ -165,6 +167,7 @@ QemuOptsList qemu_device_opts = {
 
 QemuOptsList qemu_netdev_opts = {
     .name = "netdev",
+    .implied_opt_name = "type",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
     .desc = {
         /*
@@ -177,6 +180,7 @@ QemuOptsList qemu_netdev_opts = {
 
 QemuOptsList qemu_net_opts = {
     .name = "net",
+    .implied_opt_name = "type",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
     .desc = {
         /*
@@ -227,6 +231,7 @@ QemuOptsList qemu_global_opts = {
 
 QemuOptsList qemu_mon_opts = {
     .name = "mon",
+    .implied_opt_name = "chardev",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
     .desc = {
         {
qemu-option.c +6 -1
index f86aac0..ec2a918 100644
@@ -754,12 +754,17 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname
     return 0;
 }
 
-QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname)
+QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
+                          int permit_abbrev)
 {
+    const char *firstname;
     char value[1024], *id = NULL;
     const char *p;
     QemuOpts *opts;
 
+    assert(!permit_abbrev || list->implied_opt_name);
+    firstname = permit_abbrev ? list->implied_opt_name : NULL;
+
     if (strncmp(params, "id=", 3) == 0) {
         get_opt_value(value, sizeof(value), params+3);
         id = qemu_strdup(value);
qemu-option.h +2 -1
index d735386..58136f3 100644
@@ -97,6 +97,7 @@ typedef struct QemuOptDesc {
 
 struct QemuOptsList {
     const char *name;
+    const char *implied_opt_name;
     QTAILQ_HEAD(, QemuOpts) head;
     QemuOptDesc desc[];
 };
@@ -118,7 +119,7 @@ const char *qemu_opts_id(QemuOpts *opts);
 void qemu_opts_del(QemuOpts *opts);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
-QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
+QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, int permit_abbrev);
 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict);
 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 
vl.c +6 -6
index fa2898d..6f96cde 100644
@@ -1800,7 +1800,7 @@ QemuOpts *drive_add(const char *file, const char *fmt, ...)
     vsnprintf(optstr, sizeof(optstr), fmt, ap);
     va_end(ap);
 
-    opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
+    opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
     if (!opts) {
         fprintf(stderr, "%s: huh? duplicate? (%s)\n",
                 __FUNCTION__, optstr);
@@ -4330,7 +4330,7 @@ static int balloon_parse(const char *arg)
     if (!strncmp(arg, "virtio", 6)) {
         if (arg[6] == ',') {
             /* have params -> parse them */
-            opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
+            opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
             if (!opts)
                 return  -1;
         } else {
@@ -5317,7 +5317,7 @@ int main(int argc, char **argv, char **envp)
                 default_monitor = 0;
                 break;
             case QEMU_OPTION_mon:
-                opts = qemu_opts_parse(&qemu_mon_opts, optarg, "chardev");
+                opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
                 if (!opts) {
                     fprintf(stderr, "parse error: %s\n", optarg);
                     exit(1);
@@ -5325,7 +5325,7 @@ int main(int argc, char **argv, char **envp)
                 default_monitor = 0;
                 break;
             case QEMU_OPTION_chardev:
-                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
+                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
                 if (!opts) {
                     fprintf(stderr, "parse error: %s\n", optarg);
                     exit(1);
@@ -5419,7 +5419,7 @@ int main(int argc, char **argv, char **envp)
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
-                if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
+                if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
                     exit(1);
                 }
                 break;
@@ -5528,7 +5528,7 @@ int main(int argc, char **argv, char **envp)
                 configure_rtc_date_offset(optarg, 1);
                 break;
             case QEMU_OPTION_rtc:
-                opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
+                opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
                 if (!opts) {
                     fprintf(stderr, "parse error: %s\n", optarg);
                     exit(1);
Signature · 1 line
1.6.6
#67 lore raw

RFC PATCH 45/48 qemu-option: Rename find_list() to qemu_find_opts() & external linkage

In reply to Markus Armbruster

Next commit wants to use it.

---
 qemu-config.c |    8 ++++----
 qemu-config.h |    1 +
 2 files changed, 5 insertions(+), 4 deletions(-)
qemu-config.c +4 -4
index 72b6279..7346aa0 100644
@@ -309,7 +309,7 @@ static QemuOptsList *lists[] = {
     NULL,
 };
 
-static QemuOptsList *find_list(const char *group)
+QemuOptsList *qemu_find_opts(const char *group)
 {
     int i;
 
@@ -336,7 +336,7 @@ int qemu_set_option(const char *str)
         return -1;
     }
 
-    list = find_list(group);
+    list = qemu_find_opts(group);
     if (list == NULL) {
         return -1;
     }
@@ -451,7 +451,7 @@ int qemu_config_parse(FILE *fp, const char *fname)
         }
         if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
             /* group with id */
-            list = find_list(group);
+            list = qemu_find_opts(group);
             if (list == NULL)
                 goto out;
             opts = qemu_opts_create(list, id, 1);
@@ -459,7 +459,7 @@ int qemu_config_parse(FILE *fp, const char *fname)
         }
         if (sscanf(line, "[%63[^]]]", group) == 1) {
             /* group without id */
-            list = find_list(group);
+            list = qemu_find_opts(group);
             if (list == NULL)
                 goto out;
             opts = qemu_opts_create(list, NULL, 0);
qemu-config.h +1 -0
index c507687..f217c58 100644
@@ -11,6 +11,7 @@ extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 extern QemuOptsList qemu_cpudef_opts;
 
+QemuOptsList *qemu_find_opts(const char *group);
 int qemu_set_option(const char *str);
 int qemu_global_option(const char *str);
 void qemu_add_globals(void);
Signature · 1 line
1.6.6
#68 lore raw

RFC PATCH 46/48 monitor: New argument type 'O'

In reply to Markus Armbruster

In the human monitor, it declares a single optional argument to be parsed according to the QemuOptsList given by its name.

In QMP, it declares an optional argument for each member of the QemuOptsList.

FIXME Only string options are implemented.

---
 monitor.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 59 insertions(+), 4 deletions(-)
monitor.c +59 -4
index 5c87a98..c478c27 100644
@@ -67,6 +67,9 @@
  * 'F'          filename
  * 'B'          block device name
  * 's'          string (accept optional quote)
+ * 'O'          option string of the form NAME=VALUE,...
+ *              parsed according to QemuOptsList given by its name
+ *              Example: 'device:O' uses qemu_device_opts.
  * 'i'          32 bit integer
  * 'l'          target long (32 or 64 bit)
  * 'M'          just like 'l', except in user mode the value is
@@ -3643,6 +3646,31 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon,
                 qdict_put(qdict, key, qstring_from_str(buf));
             }
             break;
+        case 'O':
+            {
+                QemuOptsList *opts_list;
+                QemuOpts *opts;
+
+                opts_list = qemu_find_opts(key);
+                if (!opts_list) {
+                    goto bad_type;
+                }
+                while (qemu_isspace(*p)) {
+                    p++;
+                }
+                if (!*p)
+                    break;
+                if (get_str(buf, sizeof(buf), &p) < 0) {
+                    goto fail;
+                }
+                opts = qemu_opts_parse(opts_list, buf, 1);
+                if (!opts) {
+                    goto fail;
+                }
+                qemu_opts_to_qdict(opts, qdict);
+                qemu_opts_del(opts);
+            }
+            break;
         case '/':
             {
                 int count, format, size;
@@ -4303,6 +4331,7 @@ static int check_arg(const CmdArgs *cmd_args, QDict *args)
                          qint_from_int(qbool_get_int(qobject_to_qbool(value))));
             }
             break;
+        case 'O':
         default:
             /* impossible */
             abort();
@@ -4317,6 +4346,23 @@ static void cmd_args_init(CmdArgs *cmd_args)
     cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
 }
 
+static int check_opts(QemuOptsList *opts_list, QDict *args)
+{
+    QemuOptDesc *desc;
+    CmdArgs cmd_args;
+
+    for (desc = opts_list->desc; desc->name; desc++) {
+        cmd_args_init(&cmd_args);
+        cmd_args.type = 's';        // FIXME obey desc->type
+        cmd_args.optional = 1;
+        qstring_append(cmd_args.name, desc->name);
+        if (check_arg(&cmd_args, args) < 0) {
+            return -1;
+        }
+    }
+    return 0;
+}
+
 /*
  * This is not trivial, we have to parse Monitor command's argument
  * type syntax to be able to check the arguments provided by clients.
@@ -4329,6 +4375,7 @@ static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
     int err;
     const char *p;
     CmdArgs cmd_args;
+    QemuOptsList *opts_list;
 
     if (cmd->args_type == NULL) {
         return (qdict_size(args) == 0 ? 0 : -1);
@@ -4336,6 +4383,7 @@ static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
 
     err = 0;
     cmd_args_init(&cmd_args);
+    opts_list = NULL;
 
     for (p = cmd->args_type;; p++) {
         if (*p == ':') {
@@ -4344,16 +4392,23 @@ static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
             if (cmd_args.type == '-') {
                 cmd_args.flag = *p++;
                 cmd_args.optional = 1;
+            } else if (cmd_args.type == 'O') {
+                opts_list = qemu_find_opts(qstring_get_str(cmd_args.name));
+                assert(opts_list);
             } else if (*p == '?') {
                 cmd_args.optional = 1;
                 p++;
             }
 
             assert(*p == ',' || *p == '\0');
-            err = check_arg(&cmd_args, args);
-
-            QDECREF(cmd_args.name);
-            cmd_args_init(&cmd_args);
+            if (opts_list) {
+                err = check_opts(opts_list, args);
+                opts_list = NULL;
+            } else {
+                err = check_arg(&cmd_args, args);
+                QDECREF(cmd_args.name);
+                cmd_args_init(&cmd_args);
+            }
 
             if (err < 0) {
                 break;
Signature · 1 line
1.6.6
#69 lore raw

RFC PATCH 47/48 monitor: Use argument type 'O' for device_add

In reply to Markus Armbruster

While there, improve the params help text.

---
 hw/qdev.c       |    3 +--
 qemu-monitor.hx |    4 ++--
 2 files changed, 3 insertions(+), 4 deletions(-)
hw/qdev.c +1 -2
index 80a027a..bcd989c 100644
@@ -766,8 +766,7 @@ void do_device_add(Monitor *mon, const QDict *qdict)
 {
     QemuOpts *opts;
 
-    opts = qemu_opts_parse(&qemu_device_opts,
-                           qdict_get_str(qdict, "config"), 1);
+    opts = qemu_opts_from_qdict(&qemu_device_opts, qdict);
     if (opts) {
         if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
             qemu_opts_del(opts);
qemu-monitor.hx +2 -2
index 7f9d261..641a8ef 100644
@@ -570,8 +570,8 @@ ETEXI
 
     {
         .name       = "device_add",
-        .args_type  = "config:s",
-        .params     = "device",
+        .args_type  = "device:O",
+        .params     = "driver[,prop=value],[,...]",
         .help       = "add device, like -device on the command line",
         .mhandler.cmd = do_device_add,
     },
Signature · 1 line
1.6.6
#70 lore raw

RFC PATCH 48/48 monitor: convert do_device_add() to QObject

In reply to Markus Armbruster
---
 hw/qdev.c       |   29 +++++++++++++++++++++--------
 hw/qdev.h       |    2 +-
 qemu-monitor.hx |    3 ++-
 3 files changed, 24 insertions(+), 10 deletions(-)
hw/qdev.c +21 -8
index bcd989c..a631492 100644
@@ -586,7 +586,9 @@ static BusState *qbus_find(const char *path)
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
             qemu_error_new(QERR_DEVICE_NOT_FOUND, elem);
-            qbus_list_dev(bus);
+            if (!in_qmp_mon()) {
+                qbus_list_dev(bus);
+            }
             return NULL;
         }
 
@@ -605,7 +607,9 @@ static BusState *qbus_find(const char *path)
                 return QLIST_FIRST(&dev->child_bus);
             default:
                 qemu_error_new(QERR_DEVICE_MULTIPLE_BUSSES, elem);
-                qbus_list_bus(dev);
+                if (!in_qmp_mon()) {
+                    qbus_list_bus(dev);
+                }
                 return NULL;
             }
         }
@@ -619,7 +623,9 @@ static BusState *qbus_find(const char *path)
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
             qemu_error_new(QERR_BUS_NOT_FOUND, elem);
-            qbus_list_bus(dev);
+            if (!in_qmp_mon()) {
+                qbus_list_bus(dev);
+            }
             return NULL;
         }
     }
@@ -762,16 +768,23 @@ void do_info_qdm(Monitor *mon)
     }
 }
 
-void do_device_add(Monitor *mon, const QDict *qdict)
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     QemuOpts *opts;
 
     opts = qemu_opts_from_qdict(&qemu_device_opts, qdict);
-    if (opts) {
-        if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
-            qemu_opts_del(opts);
-        }
+    if (!opts) {
+        return -1;
     }
+    if (!in_qmp_mon() && qdev_device_help(opts)) {
+        qemu_opts_del(opts);
+        return 0;
+    }
+    if (!qdev_device_add(opts)) {
+        qemu_opts_del(opts);
+        return -1;
+    }
+    return 0;
 }
 
 void do_device_del(Monitor *mon, const QDict *qdict)
hw/qdev.h +1 -1
index 0eb45b0..c86a59d 100644
@@ -171,7 +171,7 @@ void qbus_free(BusState *bus);
 
 void do_info_qtree(Monitor *mon);
 void do_info_qdm(Monitor *mon);
-void do_device_add(Monitor *mon, const QDict *qdict);
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 void do_device_del(Monitor *mon, const QDict *qdict);
 
 /*** qdev-properties.c ***/
qemu-monitor.hx +2 -1
index 641a8ef..e67ab20 100644
@@ -573,7 +573,8 @@ ETEXI
         .args_type  = "device:O",
         .params     = "driver[,prop=value],[,...]",
         .help       = "add device, like -device on the command line",
-        .mhandler.cmd = do_device_add,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_device_add,
     },
 
 STEXI
Signature · 1 line
1.6.6
#71 lore raw

Re: [Qemu-devel] [PATCH RFC 48/48] monitor: convert do_device_add() to QObject

Quoted 62 lines · On Wed, 24 Feb 2010 18:56:00 +0100 Markus Armbruster <[email protected]> wrote:
---
 hw/qdev.c       |   29 +++++++++++++++++++++--------
 hw/qdev.h       |    2 +-
 qemu-monitor.hx |    3 ++-
 3 files changed, 24 insertions(+), 10 deletions(-)
hw/qdev.c +12 -8
index bcd989c..a631492 100644
@@ -586,7 +586,9 @@ static BusState *qbus_find(const char *path)
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
             qemu_error_new(QERR_DEVICE_NOT_FOUND, elem);
-            qbus_list_dev(bus);
+            if (!in_qmp_mon()) {
+                qbus_list_dev(bus);
+            }
             return NULL;
         }
 
@@ -605,7 +607,9 @@ static BusState *qbus_find(const char *path)
                 return QLIST_FIRST(&dev->child_bus);
             default:
                 qemu_error_new(QERR_DEVICE_MULTIPLE_BUSSES, elem);
-                qbus_list_bus(dev);
+                if (!in_qmp_mon()) {
+                    qbus_list_bus(dev);
+                }
                 return NULL;
             }
         }
@@ -619,7 +623,9 @@ static BusState *qbus_find(const char *path)
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
             qemu_error_new(QERR_BUS_NOT_FOUND, elem);
-            qbus_list_bus(dev);
+            if (!in_qmp_mon()) {
+                qbus_list_bus(dev);
+            }
             return NULL;
         }
     }
@@ -762,16 +768,23 @@ void do_info_qdm(Monitor *mon)
     }
 }
 
-void do_device_add(Monitor *mon, const QDict *qdict)
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     QemuOpts *opts;
 
     opts = qemu_opts_from_qdict(&qemu_device_opts, qdict);
-    if (opts) {
-        if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
-            qemu_opts_del(opts);
-        }
+    if (!opts) {
+        return -1;
     }

hw/qdev.c void do_info_qdm(Monitor *mon) · line 788

Quoted 10 lines
hw/qdev.c +9 -0
+    if (!in_qmp_mon() && qdev_device_help(opts)) {
+        qemu_opts_del(opts);
+        return 0;
+    }
+    if (!qdev_device_add(opts)) {
+        qemu_opts_del(opts);
+        return -1;
+    }
+    return 0;
 }

We need at least basic documentation of the accepted parameters and
an example, just like all others QMP-enabled handlers.

Quoted 29 lines
hw/qdev.c +0 -0
 void do_device_del(Monitor *mon, const QDict *qdict)
hw/qdev.h +1 -1
index 0eb45b0..c86a59d 100644
@@ -171,7 +171,7 @@ void qbus_free(BusState *bus);
 
 void do_info_qtree(Monitor *mon);
 void do_info_qdm(Monitor *mon);
-void do_device_add(Monitor *mon, const QDict *qdict);
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 void do_device_del(Monitor *mon, const QDict *qdict);
 
 /*** qdev-properties.c ***/
qemu-monitor.hx +2 -1
index 641a8ef..e67ab20 100644
@@ -573,7 +573,8 @@ ETEXI
         .args_type  = "device:O",
         .params     = "driver[,prop=value],[,...]",
         .help       = "add device, like -device on the command line",
-        .mhandler.cmd = do_device_add,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_device_add,
     },
 
 STEXI
#72 lore raw

Re: [Qemu-devel] [PATCH RFC 48/48] monitor: convert do_device_add() to QObject

Quoted 70 lines · Luiz Capitulino
Quoted 67 lines · On Wed, 24 Feb 2010 18:56:00 +0100 Markus Armbruster <[email protected]> wrote:
---
 hw/qdev.c       |   29 +++++++++++++++++++++--------
 hw/qdev.h       |    2 +-
 qemu-monitor.hx |    3 ++-
 3 files changed, 24 insertions(+), 10 deletions(-)
hw/qdev.c +17 -8
index bcd989c..a631492 100644
@@ -586,7 +586,9 @@ static BusState *qbus_find(const char *path)
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
             qemu_error_new(QERR_DEVICE_NOT_FOUND, elem);
-            qbus_list_dev(bus);
+            if (!in_qmp_mon()) {
+                qbus_list_dev(bus);
+            }
             return NULL;
         }
 
@@ -605,7 +607,9 @@ static BusState *qbus_find(const char *path)
                 return QLIST_FIRST(&dev->child_bus);
             default:
                 qemu_error_new(QERR_DEVICE_MULTIPLE_BUSSES, elem);
-                qbus_list_bus(dev);
+                if (!in_qmp_mon()) {
+                    qbus_list_bus(dev);
+                }
                 return NULL;
             }
         }
@@ -619,7 +623,9 @@ static BusState *qbus_find(const char *path)
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
             qemu_error_new(QERR_BUS_NOT_FOUND, elem);
-            qbus_list_bus(dev);
+            if (!in_qmp_mon()) {
+                qbus_list_bus(dev);
+            }
             return NULL;
         }
     }
@@ -762,16 +768,23 @@ void do_info_qdm(Monitor *mon)
     }
 }
 
-void do_device_add(Monitor *mon, const QDict *qdict)
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     QemuOpts *opts;
 
     opts = qemu_opts_from_qdict(&qemu_device_opts, qdict);
-    if (opts) {
-        if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
-            qemu_opts_del(opts);
-        }
+    if (!opts) {
+        return -1;
     }
+    if (!in_qmp_mon() && qdev_device_help(opts)) {
+        qemu_opts_del(opts);
+        return 0;
+    }
+    if (!qdev_device_add(opts)) {
Quoted 8 lines

hw/qdev.c void do_info_qdm(Monitor *mon) · line 788

Quoted 5 lines
hw/qdev.c +4 -0
+        qemu_opts_del(opts);
+        return -1;
+    }
+    return 0;
 }

We need at least basic documentation of the accepted parameters and
an example, just like all others QMP-enabled handlers.

Fair enough.

[...]

#73 lore raw
In reply to Markus Armbruster
Quoted 91 lines · Markus Armbruster

Why this is such a big job? There are two issues with a naive conversion:

* Error message degradation

The error messages are worded for -device. They aren't so hot to begin with: we typically have many -device options, and to which one a message applies is often not obvious.

Now, QMP wants relatively generic errors. For instance, "-device:
no driver specified" becomes "Parameter 'driver' is missing". Emitting such an error with our lengthy command lines is just too mean to users.

However, if you know *where* the parameter is missing, the generic message is perfectly adequate: "-device a=b: Parameter 'driver' is missing". In fact, it's even superior to our current message.

So the first part of the patch series is about error locations. I feel it's very useful all by itself. I can split it off into its own patch series. But then the rest of this series depends on it, so I'm not sure splitting is all that useful.

We may still encounter cases where a generic message is not adequate even with precise location information. Let's solve that problem when we actually encounter it.

* String argument with option syntax, i.e. NAME=VALUE,...

QMP uses JSON to encode collections of name/value pairs. Adding a second encoding for the same thing would be a mistake, in my opinion.

Note that we already have two competing encodings in our code: QDict and QemuOpts. But we should not permit that to leak into an external interface like QMP.

QemuOpts originated in the command line and spread from there into a few monitor commands, including device_add, and a few internal interfaces.

QDict originated in the monitor. It sits right at the interface between monitor core and command handlers.

My proposed solution is modest and pragmatic:

* Lift the parsing of arguments into QemuOpts from monitor handlers up into the human monitor core. This removes QemuOpts from the handler interface, and thus avoids leaking it into QMP. It's exactly what we did for other argument types with syntax inappropriate for QMP, such as arguments of migrate_set_speed and migrate_set_downtime (commits 9da92c49..b0fbf7d3).

* Monitor handlers that need to pass their arguments in QemuOpts-form to internal interfaces use a converter function to translate from QDict to QemuOpts.

This is what the last part of the patch series is about. If you'd prefer a different solution, let's talk.

I can split this part off into its own patch series if that helps. However, the patches before it aren't all that useful without it, so I'm not sure splitting buys us much.

A possible alternative is to add the concept of optional named arguments to the monitor. Instead of encoding multiple optional named arguments in a single positional argument, we encode them as multiple named arguments. For instance, "device_add ide-drive,drive=hda,bus=ide.0,unit=0" becomes "device_add ide-drive drive=hda bus=ide.0 unit=0".

Of course, if you think that adding a second encoding for collections of name/value pairs to QMP is fine, then this last part can be dropped.

So, the series starts with error locations (part I), and ends with keeping QemuOpts out of QMP (part III). Wedged in between is the conversion of device_add to QError (part II). In more detail:

Part I: Error Locations

[01-07] Preliminary cleanup & fixes
[08] Separate "default monitor" and "current monitor" cleanly
[09-16] More cleanup
[17-21] Error Locations

Part II: Convert device_add to QError

[22-25] Preliminary qdev cleanup & fixes
[26-42] Convert device_add to QError

Quoted 8 lines

Part III

[43] Conversions between QDict and QemuOpts
[44-46] New monitor argument type O
[47-48] Convert device_add to QObject

I cut a few corners clearly marked in commit messages and code. I'll fix them up for the non-RFC submit.

I only read the patches in parts I and II, these look very good. There are some FIXME's there, but you know that yourself.

Signature · 1 line
MST
#74 lore raw
In reply to Markus Armbruster
Quoted 14 lines · On Wed, 24 Feb 2010 18:55:12 +0100 Markus Armbruster <[email protected]> wrote:

Why this is such a big job? There are two issues with a naive conversion:

* Error message degradation

The error messages are worded for -device. They aren't so hot to begin with: we typically have many -device options, and to which one a message applies is often not obvious.

Now, QMP wants relatively generic errors. For instance, "-device:
no driver specified" becomes "Parameter 'driver' is missing". Emitting such an error with our lengthy command lines is just too mean to users.

Quoted 8 lines

However, if you know *where* the parameter is missing, the generic message is perfectly adequate: "-device a=b: Parameter 'driver' is missing". In fact, it's even superior to our current message.

So the first part of the patch series is about error locations. I feel it's very useful all by itself. I can split it off into its own patch series. But then the rest of this series depends on it, so I'm not sure splitting is all that useful.

This looks reasonable to me.

When we started discussing possible solutions, we considered moving the
description table to its own module and supporting error message override.

The solution in this series is a bit simpler, but it does the job and
adding message override support later shouldn't be difficult, if needed.

Quoted 30 lines

We may still encounter cases where a generic message is not adequate even with precise location information. Let's solve that problem when we actually encounter it.

* String argument with option syntax, i.e. NAME=VALUE,...

QMP uses JSON to encode collections of name/value pairs. Adding a second encoding for the same thing would be a mistake, in my opinion.

Note that we already have two competing encodings in our code: QDict and QemuOpts. But we should not permit that to leak into an external interface like QMP.

QemuOpts originated in the command line and spread from there into a few monitor commands, including device_add, and a few internal interfaces.

QDict originated in the monitor. It sits right at the interface between monitor core and command handlers.

My proposed solution is modest and pragmatic:

* Lift the parsing of arguments into QemuOpts from monitor handlers up into the human monitor core. This removes QemuOpts from the handler interface, and thus avoids leaking it into QMP. It's exactly what we did for other argument types with syntax inappropriate for QMP, such as arguments of migrate_set_speed and migrate_set_downtime (commits 9da92c49..b0fbf7d3).

Quoted 6 lines

* Monitor handlers that need to pass their arguments in QemuOpts-form to internal interfaces use a converter function to translate from QDict to QemuOpts.

This is what the last part of the patch series is about. If you'd prefer a different solution, let's talk.

I can't think of anything better, at least not for the short term.

However, I'm wondering if exposing a monster command like device_add is
the right thing to do for QMP. We didn't expose 'info', for example, because having a command (or 'remote procedure') returning all sorts of possible data is no good.

We mapped each 'info foo' command to a 'query' command instead. This is also
natural for info commands, as they implemented separately.

So, I'm wondering if the same argument applies for device_add, as we're
going to have a large number of devices, each of them with their own properties.

The other possible solution is to introduce sets of commands to add
specific devices, but this might not be doable at all as we're moving away from this approach with qdev...

We do need a way to add devices in the system through QMP ASAP, so we'll
have to use device_add, anyway.

Two questions:

1. Don't we need a 'query-devices' command?

2. How are we going to document all the accepted parameters? Note that as we're exposing them through QMP, they obviusly must not change (although this probably a requirement for the CLI as well)

#75 lore raw
Quoted 7 lines · Luiz Capitulino
Quoted 4 lines · On Wed, 24 Feb 2010 18:55:12 +0100 Markus Armbruster <[email protected]> wrote:

Why this is such a big job? There are two issues with a naive conversion:

* Error message degradation

[...]

Quoted 30 lines
Quoted 30 lines

* String argument with option syntax, i.e. NAME=VALUE,...

QMP uses JSON to encode collections of name/value pairs. Adding a second encoding for the same thing would be a mistake, in my opinion.

Note that we already have two competing encodings in our code: QDict and QemuOpts. But we should not permit that to leak into an external interface like QMP.

QemuOpts originated in the command line and spread from there into a few monitor commands, including device_add, and a few internal interfaces.

QDict originated in the monitor. It sits right at the interface between monitor core and command handlers.

My proposed solution is modest and pragmatic:

* Lift the parsing of arguments into QemuOpts from monitor handlers up into the human monitor core. This removes QemuOpts from the handler interface, and thus avoids leaking it into QMP. It's exactly what we did for other argument types with syntax inappropriate for QMP, such as arguments of migrate_set_speed and migrate_set_downtime (commits 9da92c49..b0fbf7d3).

* Monitor handlers that need to pass their arguments in QemuOpts-form to internal interfaces use a converter function to translate from QDict to QemuOpts.

Quoted 7 lines
Quoted 2 lines

This is what the last part of the patch series is about. If you'd prefer a different solution, let's talk.

I can't think of anything better, at least not for the short term.

However, I'm wondering if exposing a monster command like device_add is
the right thing to do for QMP.

Conceptually, device_add is as simple as it gets: add a device. Any device. With properties configured. Any properties.

Quoted 3 lines

We didn't expose 'info', for example, because
having a command (or 'remote procedure') returning all sorts of possible data is no good.

Why? What's the fundamental difference between having to enumerate all the possible arguments of "info" and their replies, and having to enumerate all the possible FOOs valid with "query-FOO", and their replies?

For what it's worth, "info" returns much richer data than device_add.

Quoted 2 lines

We mapped each 'info foo' command to a 'query' command instead. This is also
natural for info commands, as they implemented separately.

Fine with me :)

Quoted 10 lines

So, I'm wondering if the same argument applies for device_add, as we're
going to have a large number of devices, each of them with their own properties.

The other possible solution is to introduce sets of commands to add
specific devices, but this might not be doable at all as we're moving away from this approach with qdev...

We do need a way to add devices in the system through QMP ASAP, so we'll
have to use device_add, anyway.

Indeed.

Quoted 3 lines

Two questions:

1. Don't we need a 'query-devices' command?

There's "info qdm". It doesn't show properties, but it could. Its output is already plenty long, so for humans we better provide a way to ask for a single driver's properties.

There's also "device_add ?" and "device_add DRIVER,?", but that's for humans. For QMP, we want to keep queries and actions neatly separate.

Quoted 3 lines

2. How are we going to document all the accepted parameters? Note that as we're exposing them through QMP, they obviusly must not change (although this probably a requirement for the CLI as well)

We want command self-documentation to enumerate all accepted parameters. For device_add, I'd make it say "accepts any". That's a lie, as it really accepts only device properties. Thus, Clients can't learn device properties from the device_add command, they need to query the device model. I'm fine with that.

#76 lore raw
Quoted 1 line · On Mon, 01 Mar 2010 08:59:38 +0100 Markus Armbruster <[email protected]> wrote:

Luiz Capitulino <[email protected]> writes:

[...]

Quoted 8 lines
Quoted 3 lines

We didn't expose 'info', for example, because
having a command (or 'remote procedure') returning all sorts of possible data is no good.

Why? What's the fundamental difference between having to enumerate all the possible arguments of "info" and their replies, and having to enumerate all the possible FOOs valid with "query-FOO", and their replies?

It's not about enumeration, it's about design.

All 'info' commands are actually individual functions that are exposed in
the user Monitor as 'info foo' commands for _user_ convenience: we group them together under the 'info' namespace. Less commands into the 'global' namespace, easier to get a listing and so on. Also, all the user sees is text related to the command typed, so it makes sense.

Now, these arguments don't apply for a machine protocol. Here, what's
important is a clear and consistent command set. User's 'info' command is neither from a low-level POV, as it returns too much unrelated data.

Quoted 1 line

For what it's worth, "info" returns much richer data than device_add.

Yes, but the problem here is input.

On the other hand, as I said somewhere else, this is somewhat beyond
the protocol design decisions because device_add is the direction qemu is taking wrt its device model.

Quoted 7 lines
Quoted 3 lines

Two questions:

1. Don't we need a 'query-devices' command?

There's "info qdm". It doesn't show properties, but it could. Its output is already plenty long, so for humans we better provide a way to ask for a single driver's properties.

Ok.

Quoted 2 lines

There's also "device_add ?" and "device_add DRIVER,?", but that's for humans. For QMP, we want to keep queries and actions neatly separate.

Yes.

Quoted 9 lines
Quoted 3 lines

2. How are we going to document all the accepted parameters? Note that as we're exposing them through QMP, they obviusly must not change (although this probably a requirement for the CLI as well)

We want command self-documentation to enumerate all accepted parameters. For device_add, I'd make it say "accepts any". That's a lie, as it really accepts only device properties. Thus, Clients can't learn device properties from the device_add command, they need to query the device model. I'm fine with that.

Yes.